Skip to content

Commit 037e0e6

Browse files
committed
staticaddr: exclude close-to-expiry depositis from SelectDeposits
1 parent 9f57c8e commit 037e0e6

File tree

2 files changed

+66
-6
lines changed

2 files changed

+66
-6
lines changed

staticaddr/loopin/manager.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -862,8 +862,31 @@ func (m *Manager) GetAllSwaps(ctx context.Context) ([]*StaticAddressLoopIn,
862862
// are needed to cover the amount requested without leaving a dust change. It
863863
// returns an error if the sum of deposits minus dust is less than the requested
864864
// amount.
865-
func SelectDeposits(targetAmount btcutil.Amount, deposits []*deposit.Deposit,
866-
csvExpiry uint32, blockHeight uint32) ([]*deposit.Deposit, error) {
865+
func SelectDeposits(targetAmount btcutil.Amount,
866+
unfilteredDeposits []*deposit.Deposit, csvExpiry uint32,
867+
blockHeight uint32) ([]*deposit.Deposit, error) {
868+
869+
// Filter out deposits that are too close to expiry to be swapped.
870+
var deposits []*deposit.Deposit
871+
for _, d := range unfilteredDeposits {
872+
depositExpiryHeight := d.ConfirmationHeight + int64(csvExpiry)
873+
874+
// The htlc expiry height is the current height plus the htlc
875+
// cltv delta.
876+
htlcExpiryHeight := blockHeight + DefaultLoopInOnChainCltvDelta
877+
878+
// Ensure that the deposit doesn't expire before the htlc.
879+
if depositExpiryHeight < int64(htlcExpiryHeight+
880+
DepositHtlcDelta) {
881+
882+
log.Debugf("Skipping deposit %s as it expires before "+
883+
"the htlc", d.OutPoint.String())
884+
885+
continue
886+
}
887+
888+
deposits = append(deposits, d)
889+
}
867890

868891
// Sort the deposits by amount in descending order, then by
869892
// blocks-until-expiry in ascending order.

staticaddr/loopin/manager_test.go

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ type testCase struct {
2929
func TestSelectDeposits(t *testing.T) {
3030
d1, d2, d3, d4 := &deposit.Deposit{
3131
Value: 1_000_000,
32-
ConfirmationHeight: 1000,
32+
ConfirmationHeight: 5_000,
3333
}, &deposit.Deposit{
3434
Value: 2_000_000,
35-
ConfirmationHeight: 2000,
35+
ConfirmationHeight: 5_001,
3636
}, &deposit.Deposit{
3737
Value: 3_000_000,
38-
ConfirmationHeight: 3000,
38+
ConfirmationHeight: 5_002,
3939
}, &deposit.Deposit{
4040
Value: 3_000_000,
41-
ConfirmationHeight: 3001,
41+
ConfirmationHeight: 5_003,
4242
}
4343
d1.Hash = chainhash.Hash{1}
4444
d1.Index = 0
@@ -121,6 +121,43 @@ func TestSelectDeposits(t *testing.T) {
121121
expected: []*deposit.Deposit{d3},
122122
expectedErr: "",
123123
},
124+
{
125+
name: "prefilter filters deposits close to expiry",
126+
deposits: func() []*deposit.Deposit {
127+
// dClose expires before
128+
// htlcExpiry+DepositHtlcDelta and must be
129+
// filtered out. dOK expires exactly at the
130+
// threshold and must be eligible.
131+
dClose := &deposit.Deposit{
132+
Value: 3_000_000,
133+
ConfirmationHeight: 3000,
134+
}
135+
dClose.Hash = chainhash.Hash{5}
136+
dClose.Index = 0
137+
dOK := &deposit.Deposit{
138+
Value: 2_000_000,
139+
ConfirmationHeight: 3050,
140+
}
141+
dOK.Hash = chainhash.Hash{6}
142+
dOK.Index = 0
143+
return []*deposit.Deposit{dClose, dOK}
144+
}(),
145+
targetValue: 1_000_000,
146+
csvExpiry: 1000,
147+
blockHeight: 3000,
148+
expected: func() []*deposit.Deposit {
149+
// Only dOK should be considered.
150+
// dClose is filtered.
151+
dOK := &deposit.Deposit{
152+
Value: 2_000_000,
153+
ConfirmationHeight: 3050,
154+
}
155+
dOK.Hash = chainhash.Hash{6}
156+
dOK.Index = 0
157+
return []*deposit.Deposit{dOK}
158+
}(),
159+
expectedErr: "",
160+
},
124161
}
125162

126163
for _, tc := range testCases {

0 commit comments

Comments
 (0)