Skip to content

Commit 7e70e3d

Browse files
authored
Merge pull request #10387 from ziggie1984/fix-scanning-issue
fix scanning issue
2 parents 8c8662c + db3add1 commit 7e70e3d

File tree

4 files changed

+44
-79
lines changed

4 files changed

+44
-79
lines changed

contractcourt/briefcase_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,9 @@ func assertResolversEqual(t *testing.T, originalResolver ContractResolver,
278278
t.Fatalf("expected %v, got %v", ogRes.resolved.Load(),
279279
diskRes.resolved.Load())
280280
}
281-
if ogRes.broadcastHeight != diskRes.broadcastHeight {
281+
if ogRes.confirmHeight != diskRes.confirmHeight {
282282
t.Fatalf("expected %v, got %v",
283-
ogRes.broadcastHeight, diskRes.broadcastHeight)
283+
ogRes.confirmHeight, diskRes.confirmHeight)
284284
}
285285
if ogRes.chanPoint != diskRes.chanPoint {
286286
t.Fatalf("expected %v, got %v", ogRes.chanPoint,
@@ -341,8 +341,8 @@ func TestContractInsertionRetrieval(t *testing.T) {
341341
SelfOutputSignDesc: testSignDesc,
342342
MaturityDelay: 99,
343343
},
344-
broadcastHeight: 109,
345-
chanPoint: testChanPoint1,
344+
confirmHeight: 109,
345+
chanPoint: testChanPoint1,
346346
}
347347
commitResolver.resolved.Store(false)
348348

contractcourt/commit_sweep_resolver.go

Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ type commitSweepResolver struct {
3838
// this HTLC on-chain.
3939
commitResolution lnwallet.CommitOutputResolution
4040

41-
// broadcastHeight is the height that the original contract was
42-
// broadcast to the main-chain at. We'll use this value to bound any
43-
// historical queries to the chain for spends/confirmations.
44-
broadcastHeight uint32
41+
// confirmHeight is the block height that the commitment transaction was
42+
// confirmed at. We'll use this value to bound any historical queries to
43+
// the chain for spends/confirmations.
44+
confirmHeight uint32
4545

4646
// chanPoint is the channel point of the original contract.
4747
chanPoint wire.OutPoint
@@ -74,13 +74,13 @@ type commitSweepResolver struct {
7474

7575
// newCommitSweepResolver instantiates a new direct commit output resolver.
7676
func newCommitSweepResolver(res lnwallet.CommitOutputResolution,
77-
broadcastHeight uint32, chanPoint wire.OutPoint,
77+
confirmHeight uint32, chanPoint wire.OutPoint,
7878
resCfg ResolverConfig) *commitSweepResolver {
7979

8080
r := &commitSweepResolver{
8181
contractResolverKit: *newContractResolverKit(resCfg),
8282
commitResolution: res,
83-
broadcastHeight: broadcastHeight,
83+
confirmHeight: confirmHeight,
8484
chanPoint: chanPoint,
8585
}
8686

@@ -123,37 +123,6 @@ func waitForSpend(op *wire.OutPoint, pkScript []byte, heightHint uint32,
123123
}
124124
}
125125

126-
// getCommitTxConfHeight waits for confirmation of the commitment tx and
127-
// returns the confirmation height.
128-
func (c *commitSweepResolver) getCommitTxConfHeight() (uint32, error) {
129-
txID := c.commitResolution.SelfOutPoint.Hash
130-
signDesc := c.commitResolution.SelfOutputSignDesc
131-
pkScript := signDesc.Output.PkScript
132-
133-
const confDepth = 1
134-
135-
confChan, err := c.Notifier.RegisterConfirmationsNtfn(
136-
&txID, pkScript, confDepth, c.broadcastHeight,
137-
)
138-
if err != nil {
139-
return 0, err
140-
}
141-
defer confChan.Cancel()
142-
143-
select {
144-
case txConfirmation, ok := <-confChan.Confirmed:
145-
if !ok {
146-
return 0, fmt.Errorf("cannot get confirmation "+
147-
"for commit tx %v", txID)
148-
}
149-
150-
return txConfirmation.BlockHeight, nil
151-
152-
case <-c.quit:
153-
return 0, errResolverShuttingDown
154-
}
155-
}
156-
157126
// Resolve instructs the contract resolver to resolve the output on-chain. Once
158127
// the output has been *fully* resolved, the function should return immediately
159128
// with a nil ContractResolver value for the first return value. In the case
@@ -268,7 +237,7 @@ func (c *commitSweepResolver) Encode(w io.Writer) error {
268237
if err := binary.Write(w, endian, c.IsResolved()); err != nil {
269238
return err
270239
}
271-
if err := binary.Write(w, endian, c.broadcastHeight); err != nil {
240+
if err := binary.Write(w, endian, c.confirmHeight); err != nil {
272241
return err
273242
}
274243
if _, err := w.Write(c.chanPoint.Hash[:]); err != nil {
@@ -308,7 +277,7 @@ func newCommitSweepResolverFromReader(r io.Reader, resCfg ResolverConfig) (
308277
c.markResolved()
309278
}
310279

311-
if err := binary.Read(r, endian, &c.broadcastHeight); err != nil {
280+
if err := binary.Read(r, endian, &c.confirmHeight); err != nil {
312281
return nil, err
313282
}
314283
_, err := io.ReadFull(r, c.chanPoint.Hash[:])
@@ -381,19 +350,14 @@ func (c *commitSweepResolver) Launch() error {
381350
return nil
382351
}
383352

384-
confHeight, err := c.getCommitTxConfHeight()
385-
if err != nil {
386-
return err
387-
}
388-
389353
// Wait up until the CSV expires, unless we also have a CLTV that
390354
// expires after.
391-
unlockHeight := confHeight + c.commitResolution.MaturityDelay
355+
unlockHeight := c.confirmHeight + c.commitResolution.MaturityDelay
392356
if c.hasCLTV() {
393357
unlockHeight = max(unlockHeight, c.leaseExpiry)
394358
}
395359

396-
// Update report now that we learned the confirmation height.
360+
// Update report with the calculated maturity height.
397361
c.reportLock.Lock()
398362
c.currentReport.MaturityHeight = unlockHeight
399363
c.reportLock.Unlock()
@@ -412,7 +376,7 @@ func (c *commitSweepResolver) Launch() error {
412376
inp = input.NewCsvInputWithCltv(
413377
&c.commitResolution.SelfOutPoint, witnessType,
414378
&c.commitResolution.SelfOutputSignDesc,
415-
c.broadcastHeight, c.commitResolution.MaturityDelay,
379+
c.confirmHeight, c.commitResolution.MaturityDelay,
416380
c.leaseExpiry, input.WithResolutionBlob(
417381
c.commitResolution.ResolutionBlob,
418382
),
@@ -421,7 +385,7 @@ func (c *commitSweepResolver) Launch() error {
421385
inp = input.NewCsvInput(
422386
&c.commitResolution.SelfOutPoint, witnessType,
423387
&c.commitResolution.SelfOutputSignDesc,
424-
c.broadcastHeight, c.commitResolution.MaturityDelay,
388+
c.confirmHeight, c.commitResolution.MaturityDelay,
425389
input.WithResolutionBlob(
426390
c.commitResolution.ResolutionBlob,
427391
),

contractcourt/commit_sweep_resolver_test.go

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ import (
1818
"github.com/stretchr/testify/require"
1919
)
2020

21+
const (
22+
testCommitSweepConfHeight = 99
23+
)
24+
2125
type commitSweepResolverTestContext struct {
2226
resolver *commitSweepResolver
2327
notifier *mock.ChainNotifier
@@ -27,7 +31,8 @@ type commitSweepResolverTestContext struct {
2731
}
2832

2933
func newCommitSweepResolverTestContext(t *testing.T,
30-
resolution *lnwallet.CommitOutputResolution) *commitSweepResolverTestContext {
34+
resolution *lnwallet.CommitOutputResolution,
35+
confirmHeight uint32) *commitSweepResolverTestContext {
3136

3237
notifier := &mock.ChainNotifier{
3338
EpochChan: make(chan *chainntnfs.BlockEpoch),
@@ -68,7 +73,7 @@ func newCommitSweepResolverTestContext(t *testing.T,
6873
}
6974

7075
resolver := newCommitSweepResolver(
71-
*resolution, 0, wire.OutPoint{}, cfg,
76+
*resolution, confirmHeight, wire.OutPoint{}, cfg,
7277
)
7378

7479
return &commitSweepResolverTestContext{
@@ -178,7 +183,9 @@ func TestCommitSweepResolverNoDelay(t *testing.T) {
178183
},
179184
}
180185

181-
ctx := newCommitSweepResolverTestContext(t, &res)
186+
ctx := newCommitSweepResolverTestContext(
187+
t, &res, testCommitSweepConfHeight,
188+
)
182189

183190
// Replace our checkpoint with one which will push reports into a
184191
// channel for us to consume. We replace this function on the resolver
@@ -197,15 +204,12 @@ func TestCommitSweepResolverNoDelay(t *testing.T) {
197204

198205
ctx.resolve()
199206

200-
spendTx := &wire.MsgTx{}
201-
spendHash := spendTx.TxHash()
202-
ctx.notifier.ConfChan <- &chainntnfs.TxConfirmation{
203-
Tx: spendTx,
204-
}
205-
206207
// No csv delay, so the input should be swept immediately.
207208
<-ctx.sweeper.sweptInputs
208209

210+
spendTx := &wire.MsgTx{}
211+
spendHash := spendTx.TxHash()
212+
209213
amt := btcutil.Amount(res.SelfOutputSignDesc.Output.Value)
210214
expectedReport := &channeldb.ResolverReport{
211215
OutPoint: wire.OutPoint{},
@@ -242,7 +246,10 @@ func testCommitSweepResolverDelay(t *testing.T, sweepErr error) {
242246
SelfOutPoint: outpoint,
243247
}
244248

245-
ctx := newCommitSweepResolverTestContext(t, &res)
249+
// Use confirmHeight = 99, so maturityHeight = 99 + 3 = 102.
250+
ctx := newCommitSweepResolverTestContext(
251+
t, &res, testCommitSweepConfHeight,
252+
)
246253

247254
// Replace our checkpoint with one which will push reports into a
248255
// channel for us to consume. We replace this function on the resolver
@@ -270,25 +277,18 @@ func testCommitSweepResolverDelay(t *testing.T, sweepErr error) {
270277
Amount: btcutil.Amount(amt),
271278
LimboBalance: btcutil.Amount(amt),
272279
}
273-
if *report != expectedReport {
274-
t.Fatalf("unexpected resolver report. want=%v got=%v",
275-
expectedReport, report)
276-
}
280+
require.Equal(t, expectedReport, *report)
277281

278282
ctx.resolve()
279283

280-
ctx.notifier.ConfChan <- &chainntnfs.TxConfirmation{
281-
BlockHeight: testInitialBlockHeight - 1,
282-
}
283-
284-
// Allow resolver to process confirmation.
284+
// Allow resolver to launch and update the report.
285285
time.Sleep(sweepProcessInterval)
286286

287287
// Expect report to be updated.
288+
// confirmHeight(99) + maturityDelay(3) = 102.
288289
report = ctx.resolver.report()
289-
if report.MaturityHeight != testInitialBlockHeight+2 {
290-
t.Fatal("report maturity height incorrect")
291-
}
290+
expectedMaturity := testCommitSweepConfHeight + res.MaturityDelay
291+
require.Equal(t, expectedMaturity, report.MaturityHeight)
292292

293293
// Notify initial block height. Although the csv lock is still in
294294
// effect, we expect the input being sent to the sweeper before the csv
@@ -325,13 +325,10 @@ func testCommitSweepResolverDelay(t *testing.T, sweepErr error) {
325325
Outpoint: outpoint,
326326
Type: ReportOutputUnencumbered,
327327
Amount: btcutil.Amount(amt),
328-
MaturityHeight: testInitialBlockHeight + 2,
328+
MaturityHeight: testCommitSweepConfHeight + res.MaturityDelay,
329329
RecoveredBalance: expectedRecoveredBalance,
330330
}
331-
if *report != expectedReport {
332-
t.Fatalf("unexpected resolver report. want=%v got=%v",
333-
expectedReport, report)
334-
}
331+
require.Equal(t, expectedReport, *report)
335332
}
336333

337334
// TestCommitSweepResolverDelay tests resolution of a direct commitment output

docs/release-notes/release-notes-0.20.1.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
* Fix bug where channels with both [policies disabled at startup could never
2525
be used for routing](https://github.com/lightningnetwork/lnd/pull/10378)
2626

27+
* [Fix a case where resolving the
28+
to_local/to_remote output](https://github.com/lightningnetwork/lnd/pull/10387)
29+
might take too long.
30+
2731
# New Features
2832

2933
## Functional Enhancements

0 commit comments

Comments
 (0)