Skip to content

Commit 8304783

Browse files
committed
staticaddr/loopin: fix autoloop unconfirmed expiry
Unconfirmed deposits do not have a running CSV timer, so autoloop selection should not treat them as earliest expiring candidates. Reusing different expiry math from loop-in selection can make dynamic programming prefer mempool deposits in tie cases. Reuse static loop-in expiry calculations for autoloop candidates, treat unconfirmed deposits as not yet aging in selection tie-breaks, and add a selector test for confirmed versus unconfirmed ties.
1 parent 094c803 commit 8304783

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

staticaddr/loopin/autoloop_dp.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,10 @@ func filterAutoloopCandidateDeposits(maxAmount btcutil.Amount,
246246
continue
247247
}
248248

249-
residualLife := candidateDeposit.ConfirmationHeight +
250-
int64(csvExpiry) - int64(blockHeight)
249+
residualLife := int64(blocksUntilDepositExpiry(
250+
uint32(candidateDeposit.ConfirmationHeight),
251+
blockHeight, csvExpiry,
252+
))
251253

252254
eligibleDeposits = append(
253255
eligibleDeposits, autoloopCandidateDeposit{

staticaddr/loopin/autoloop_dp_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,28 @@ func TestSelectNoChangeDepositsWithMemoryBudget(t *testing.T) {
8080
}
8181
}
8282

83+
// TestSelectNoChangeDepositsPrefersConfirmedTie verifies unconfirmed deposits
84+
// are not treated as earlier-expiring than confirmed deposits. Their CSV timer
85+
// has not started yet, so a same-value confirmed deposit should win the expiry
86+
// tie-break.
87+
func TestSelectNoChangeDepositsPrefersConfirmedTie(t *testing.T) {
88+
t.Parallel()
89+
90+
unconfirmed := makeDeposit(34, 0, 5_000, 0)
91+
confirmed := makeDeposit(35, 0, 5_000, 200)
92+
93+
deposits, err := selectNoChangeDeposits(
94+
5_000, 5_000, []*deposit.Deposit{
95+
unconfirmed, confirmed,
96+
}, 1_000, 100, nil,
97+
)
98+
require.NoError(t, err)
99+
require.Equal(
100+
t, []string{confirmed.OutPoint.String()},
101+
depositOutpoints(deposits),
102+
)
103+
}
104+
83105
// TestAutoloopDPSizing verifies the bucket sizing math. These cases are easier
84106
// to understand directly than by inferring the step from a larger selector
85107
// behavior test.

0 commit comments

Comments
 (0)