Skip to content

Commit 91d1a3e

Browse files
authored
Merge pull request lightninglabs#1096 from hieblmi/revert-dyn-conf
staticaddr: revert dynamic conf
2 parents 798d86a + c8b59be commit 91d1a3e

6 files changed

Lines changed: 102 additions & 396 deletions

File tree

staticaddr/deposit/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const (
2020
// MinConfs is the minimum number of confirmations we require for a
2121
// deposit to be considered available for loop-ins, coop-spends and
2222
// timeouts.
23-
MinConfs = 3
23+
MinConfs = 6
2424

2525
// MaxConfs is unset since we don't require a max number of
2626
// confirmations for deposits.

staticaddr/loopin/actions.go

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
"github.com/lightningnetwork/lnd/lnwallet"
3131
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
3232
"github.com/lightningnetwork/lnd/lnwire"
33-
"google.golang.org/grpc/status"
3433
)
3534

3635
const (
@@ -147,10 +146,6 @@ func (f *FSM) InitHtlcAction(ctx context.Context,
147146
ctx, loopInReq,
148147
)
149148
if err != nil {
150-
// Check if this is an insufficient confirmations error and log
151-
// the details to help the user understand what's needed.
152-
logInsufficientConfirmationsDetails(err)
153-
154149
err = fmt.Errorf("unable to initiate the loop-in with the "+
155150
"server: %w", err)
156151

@@ -919,30 +914,3 @@ func byteSliceTo66ByteSlice(b []byte) ([musig2.PubNonceSize]byte, error) {
919914

920915
return res, nil
921916
}
922-
923-
// logInsufficientConfirmationsDetails extracts and logs the per-deposit
924-
// confirmation details from a gRPC error if present.
925-
func logInsufficientConfirmationsDetails(err error) {
926-
st, ok := status.FromError(err)
927-
if !ok {
928-
return
929-
}
930-
931-
for _, detail := range st.Details() {
932-
confDetails, ok :=
933-
detail.(*swapserverrpc.InsufficientConfirmationsDetails)
934-
935-
if !ok {
936-
continue
937-
}
938-
939-
log.Warnf("Insufficient deposit confirmations, max wait: %d blocks",
940-
confDetails.MaxBlocksToWait)
941-
942-
for _, dep := range confDetails.Deposits {
943-
log.Warnf(" Deposit %s: %d/%d confirmations (need %d more blocks)",
944-
dep.Outpoint, dep.CurrentConfirmations,
945-
dep.RequiredConfirmations, dep.BlocksToWait)
946-
}
947-
}
948-
}

staticaddr/loopin/manager.go

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -850,13 +850,11 @@ func (m *Manager) GetAllSwaps(ctx context.Context) ([]*StaticAddressLoopIn,
850850
return swaps, nil
851851
}
852852

853-
// SelectDeposits sorts the deposits to optimize for successful swaps with
854-
// dynamic confirmation requirements: 1) more confirmations first (higher chance
855-
// of server acceptance), 2) larger amounts first (to minimize number of deposits
856-
// used), 3) expiring sooner first (to use time-sensitive deposits). It then
857-
// selects the deposits that are needed to cover the amount requested without
858-
// leaving a dust change. It returns an error if the sum of deposits minus dust
859-
// is less than the requested amount.
853+
// SelectDeposits sorts the deposits by amount in descending order, then by
854+
// blocks-until-expiry in ascending order. It then selects the deposits that
855+
// are needed to cover the amount requested without leaving a dust change. It
856+
// returns an error if the sum of deposits minus dust is less than the requested
857+
// amount.
860858
func SelectDeposits(targetAmount btcutil.Amount,
861859
unfilteredDeposits []*deposit.Deposit, csvExpiry uint32,
862860
blockHeight uint32) ([]*deposit.Deposit, error) {
@@ -877,28 +875,17 @@ func SelectDeposits(targetAmount btcutil.Amount,
877875
deposits = append(deposits, d)
878876
}
879877

880-
// Sort deposits to optimize for successful swaps with dynamic
881-
// confirmation requirements:
882-
// 1. More confirmations first (higher chance of server acceptance)
883-
// 2. Larger amounts first (to minimize number of deposits used)
878+
// Sort the deposits by amount in descending order, then by
879+
// blocks-until-expiry in ascending order.
884880
sort.Slice(deposits, func(i, j int) bool {
885-
// Primary: more confirmations first. Guard against the
886-
// theoretical case where ConfirmationHeight > blockHeight
887-
// (e.g. during a transient reorg inconsistency).
888-
var iConfs, jConfs uint32
889-
if blockHeight > uint32(deposits[i].ConfirmationHeight) {
890-
iConfs = blockHeight -
891-
uint32(deposits[i].ConfirmationHeight)
892-
}
893-
if blockHeight > uint32(deposits[j].ConfirmationHeight) {
894-
jConfs = blockHeight -
895-
uint32(deposits[j].ConfirmationHeight)
896-
}
897-
if iConfs != jConfs {
898-
return iConfs > jConfs
899-
}
881+
if deposits[i].Value == deposits[j].Value {
882+
iExp := uint32(deposits[i].ConfirmationHeight) +
883+
csvExpiry - blockHeight
884+
jExp := uint32(deposits[j].ConfirmationHeight) +
885+
csvExpiry - blockHeight
900886

901-
// Secondary: larger amounts first.
887+
return iExp < jExp
888+
}
902889
return deposits[i].Value > deposits[j].Value
903890
})
904891

staticaddr/loopin/manager_test.go

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,10 @@ type testCase struct {
2626

2727
// TestSelectDeposits tests the selectDeposits function, which selects
2828
// deposits that can cover a target value while respecting the dust limit.
29-
// Sorting priority: 1) more confirmations first, 2) larger amounts first,
30-
// 3) expiring sooner first.
3129
func TestSelectDeposits(t *testing.T) {
32-
// Note: confirmations = blockHeight - ConfirmationHeight
33-
// Lower ConfirmationHeight means more confirmations at a given block.
3430
d1, d2, d3, d4 := &deposit.Deposit{
3531
Value: 1_000_000,
36-
ConfirmationHeight: 5_000, // most confs at height 5100
32+
ConfirmationHeight: 5_000,
3733
}, &deposit.Deposit{
3834
Value: 2_000_000,
3935
ConfirmationHeight: 5_001,
@@ -42,7 +38,7 @@ func TestSelectDeposits(t *testing.T) {
4238
ConfirmationHeight: 5_002,
4339
}, &deposit.Deposit{
4440
Value: 3_000_000,
45-
ConfirmationHeight: 5_003, // fewest confs at height 5100
41+
ConfirmationHeight: 5_003,
4642
}
4743
d1.Hash = chainhash.Hash{1}
4844
d1.Index = 0
@@ -53,108 +49,75 @@ func TestSelectDeposits(t *testing.T) {
5349
d4.Hash = chainhash.Hash{4}
5450
d4.Index = 0
5551

56-
// Use a realistic block height and csv expiry for all standard
57-
// test cases. csvExpiry must be large enough that deposits remain
58-
// swappable at this block height.
59-
const (
60-
testBlockHeight uint32 = 5_100
61-
testCsvExpiry uint32 = 2_500
62-
)
63-
6452
testCases := []testCase{
6553
{
6654
name: "single deposit exact target",
6755
deposits: []*deposit.Deposit{d1},
6856
targetValue: 1_000_000,
69-
csvExpiry: testCsvExpiry,
70-
blockHeight: testBlockHeight,
7157
expected: []*deposit.Deposit{d1},
7258
expectedErr: "",
7359
},
7460
{
75-
// d1 has more confirmations, so it's preferred even
76-
// though d2 is larger.
77-
name: "prefer more confirmed deposit over larger",
61+
name: "prefer larger deposit when both cover",
7862
deposits: []*deposit.Deposit{d1, d2},
7963
targetValue: 1_000_000,
80-
csvExpiry: testCsvExpiry,
81-
blockHeight: testBlockHeight,
82-
expected: []*deposit.Deposit{d1},
64+
expected: []*deposit.Deposit{d2},
8365
expectedErr: "",
8466
},
8567
{
86-
// d1 has the most confirmations among d1, d2, d3.
87-
name: "prefer most confirmed among three",
68+
name: "prefer largest among three when one is enough",
8869
deposits: []*deposit.Deposit{d1, d2, d3},
8970
targetValue: 1_000_000,
90-
csvExpiry: testCsvExpiry,
91-
blockHeight: testBlockHeight,
92-
expected: []*deposit.Deposit{d1},
71+
expected: []*deposit.Deposit{d3},
9372
expectedErr: "",
9473
},
9574
{
9675
name: "single deposit insufficient by 1",
9776
deposits: []*deposit.Deposit{d1},
9877
targetValue: 1_000_001,
99-
csvExpiry: testCsvExpiry,
100-
blockHeight: testBlockHeight,
10178
expected: []*deposit.Deposit{},
10279
expectedErr: "not enough deposits to cover",
10380
},
10481
{
10582
name: "target leaves exact dust limit change",
10683
deposits: []*deposit.Deposit{d1},
10784
targetValue: 1_000_000 - dustLimit,
108-
csvExpiry: testCsvExpiry,
109-
blockHeight: testBlockHeight,
11085
expected: []*deposit.Deposit{d1},
11186
expectedErr: "",
11287
},
11388
{
11489
name: "target leaves dust change (just over)",
11590
deposits: []*deposit.Deposit{d1},
11691
targetValue: 1_000_000 - dustLimit + 1,
117-
csvExpiry: testCsvExpiry,
118-
blockHeight: testBlockHeight,
11992
expected: []*deposit.Deposit{},
12093
expectedErr: "not enough deposits to cover",
12194
},
12295
{
12396
name: "all deposits exactly match target",
12497
deposits: []*deposit.Deposit{d1, d2, d3},
12598
targetValue: d1.Value + d2.Value + d3.Value,
126-
csvExpiry: testCsvExpiry,
127-
blockHeight: testBlockHeight,
12899
expected: []*deposit.Deposit{d1, d2, d3},
129100
expectedErr: "",
130101
},
131102
{
132103
name: "sum minus dust limit is allowed (change == dust)",
133104
deposits: []*deposit.Deposit{d1, d2, d3},
134105
targetValue: d1.Value + d2.Value + d3.Value - dustLimit,
135-
csvExpiry: testCsvExpiry,
136-
blockHeight: testBlockHeight,
137106
expected: []*deposit.Deposit{d1, d2, d3},
138107
expectedErr: "",
139108
},
140109
{
141110
name: "sum minus dust limit plus 1 is not allowed (dust change)",
142111
deposits: []*deposit.Deposit{d1, d2, d3},
143112
targetValue: d1.Value + d2.Value + d3.Value - dustLimit + 1,
144-
csvExpiry: testCsvExpiry,
145-
blockHeight: testBlockHeight,
146113
expected: []*deposit.Deposit{},
147114
expectedErr: "not enough deposits to cover",
148115
},
149116
{
150-
// d3 and d4 have the same value but d3 has more
151-
// confirmations (lower ConfirmationHeight), so it
152-
// wins at the primary sort level.
153-
name: "same value, prefer more confirmed",
117+
name: "tie by value, prefer earlier expiry",
154118
deposits: []*deposit.Deposit{d3, d4},
155-
targetValue: d4.Value - dustLimit,
156-
csvExpiry: testCsvExpiry,
157-
blockHeight: testBlockHeight,
119+
targetValue: d4.Value - dustLimit, // d3/d4 have the
120+
// same value but different expiration.
158121
expected: []*deposit.Deposit{d3},
159122
expectedErr: "",
160123
},

0 commit comments

Comments
 (0)