Skip to content

Commit a41334d

Browse files
committed
staticaddr: add swap hash to deposit structs
1 parent 98fece4 commit a41334d

File tree

7 files changed

+504
-384
lines changed

7 files changed

+504
-384
lines changed

loopd/swapclient_server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1924,6 +1924,7 @@ func filter(deposits []*deposit.Deposit, f filterFunc) []*looprpc.Deposit {
19241924
Outpoint: outpoint,
19251925
Value: int64(d.Value),
19261926
ConfirmationHeight: d.ConfirmationHeight,
1927+
SwapHash: d.SwapHash[:],
19271928
}
19281929

19291930
clientDeposits = append(clientDeposits, deposit)

looprpc/client.pb.go

Lines changed: 396 additions & 384 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

looprpc/client.proto

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1910,6 +1910,12 @@ message Deposit {
19101910
loop-in swap anymore.
19111911
*/
19121912
int64 blocks_until_expiry = 6;
1913+
1914+
/*
1915+
The swap hash of the swap that this deposit is part of. This field is only
1916+
set if the deposit is part of a loop-in swap.
1917+
*/
1918+
bytes swap_hash = 7;
19131919
}
19141920

19151921
message StaticAddressWithdrawal {

looprpc/client.swagger.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,11 @@
860860
"type": "string",
861861
"format": "int64",
862862
"description": "The number of blocks that are left until the deposit cannot be used for a\nloop-in swap anymore."
863+
},
864+
"swap_hash": {
865+
"type": "string",
866+
"format": "byte",
867+
"description": "The swap hash of the swap that this deposit is part of. This field is only\nset if the deposit is part of a loop-in swap."
863868
}
864869
}
865870
},

staticaddr/deposit/deposit.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/btcsuite/btcd/chaincfg/chainhash"
1010
"github.com/btcsuite/btcd/wire"
1111
"github.com/lightninglabs/loop/fsm"
12+
"github.com/lightningnetwork/lnd/lntypes"
1213
)
1314

1415
// ID is a unique identifier for a deposit.
@@ -54,6 +55,10 @@ type Deposit struct {
5455
// ExpirySweepTxid is the transaction id of the expiry sweep.
5556
ExpirySweepTxid chainhash.Hash
5657

58+
// SwapHash is an optional reference to a static address loop-in swap
59+
// that used this deposit.
60+
SwapHash *lntypes.Hash
61+
5762
// FinalizedWithdrawalTx is the coop-signed withdrawal transaction. It
5863
// is republished on new block arrivals and on client restarts.
5964
FinalizedWithdrawalTx *wire.MsgTx

staticaddr/deposit/sql_store.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/lightninglabs/loop/loopdb"
1414
"github.com/lightninglabs/loop/loopdb/sqlc"
1515
"github.com/lightningnetwork/lnd/clock"
16+
"github.com/lightningnetwork/lnd/lntypes"
1617
)
1718

1819
// SqlStore is the backing store for static address deposits.
@@ -270,6 +271,16 @@ func ToDeposit(row sqlc.Deposit, lastUpdate sqlc.DepositUpdate) (*Deposit,
270271
}
271272
}
272273

274+
var swapHash *lntypes.Hash
275+
if row.SwapHash != nil {
276+
hash, err := lntypes.MakeHash(row.SwapHash)
277+
if err != nil {
278+
return nil, err
279+
}
280+
281+
swapHash = &hash
282+
}
283+
273284
return &Deposit{
274285
ID: id,
275286
state: fsm.StateType(lastUpdate.UpdateState),
@@ -281,6 +292,7 @@ func ToDeposit(row sqlc.Deposit, lastUpdate sqlc.DepositUpdate) (*Deposit,
281292
ConfirmationHeight: row.ConfirmationHeight,
282293
TimeOutSweepPkScript: row.TimeoutSweepPkScript,
283294
ExpirySweepTxid: expirySweepTxid,
295+
SwapHash: swapHash,
284296
FinalizedWithdrawalTx: finalizedWithdrawalTx,
285297
}, nil
286298
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package deposit
2+
3+
import (
4+
"testing"
5+
6+
"github.com/btcsuite/btcd/wire"
7+
"github.com/lightninglabs/loop/fsm"
8+
"github.com/lightninglabs/loop/loopdb/sqlc"
9+
"github.com/lightningnetwork/lnd/lntypes"
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
func TestToDeposit(t *testing.T) {
14+
depositID, err := GetRandomDepositID()
15+
require.NoError(t, err)
16+
17+
swapHash, err := lntypes.MakeHash(dummyTxHashBytes())
18+
require.NoError(t, err)
19+
20+
tx := wire.NewMsgTx(2)
21+
txHash := tx.TxHash()
22+
23+
tests := []struct {
24+
name string
25+
row sqlc.Deposit
26+
lastUpdate sqlc.DepositUpdate
27+
expectErr bool
28+
}{
29+
{
30+
name: "fully valid data",
31+
row: sqlc.Deposit{
32+
DepositID: depositID[:],
33+
TxHash: txHash[:],
34+
Amount: 100000000,
35+
ConfirmationHeight: 123456,
36+
SwapHash: swapHash[:],
37+
},
38+
lastUpdate: sqlc.DepositUpdate{
39+
UpdateState: "completed",
40+
},
41+
expectErr: false,
42+
},
43+
{
44+
name: "fully valid data",
45+
row: sqlc.Deposit{
46+
DepositID: depositID[:],
47+
TxHash: txHash[:],
48+
Amount: 100000000,
49+
ConfirmationHeight: 123456,
50+
},
51+
lastUpdate: sqlc.DepositUpdate{
52+
UpdateState: "completed",
53+
},
54+
expectErr: false,
55+
},
56+
}
57+
58+
for _, test := range tests {
59+
t.Run(test.name, func(t *testing.T) {
60+
result, err := ToDeposit(test.row, test.lastUpdate)
61+
if test.expectErr {
62+
require.Error(t, err)
63+
require.Nil(t, result)
64+
} else {
65+
require.NoError(t, err)
66+
require.NotNil(t, result)
67+
require.Equal(t, fsm.StateType(test.lastUpdate.UpdateState), result.state)
68+
require.NotNil(t, result.SwapHash)
69+
}
70+
})
71+
}
72+
}
73+
74+
func dummyTxHashBytes() []byte {
75+
return []byte{0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
76+
0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
77+
0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
78+
0x20, 0x21, 0x22, 0x23}
79+
}

0 commit comments

Comments
 (0)