Merge pull request #10387 from ziggie1984/fix-scanning-issue

fix scanning issue
This commit is contained in:
Olaoluwa Osuntokun authored and GitHub committed 2025-11-24 10:09:48 -08:00
commit 7e70e3dbd8
4 files changed
+43 -78

No files matched your search

+3 -3
View File
@@ -278,9 +278,9 @@ func assertResolversEqual(t *testing.T, originalResolver ContractResolver,
t.Fatalf("expected %v, got %v", ogRes.resolved.Load(), t.Fatalf("expected %v, got %v", ogRes.resolved.Load(),
diskRes.resolved.Load()) diskRes.resolved.Load())
} }
if ogRes.broadcastHeight != diskRes.broadcastHeight { if ogRes.confirmHeight != diskRes.confirmHeight {
t.Fatalf("expected %v, got %v", t.Fatalf("expected %v, got %v",
ogRes.broadcastHeight, diskRes.broadcastHeight) ogRes.confirmHeight, diskRes.confirmHeight)
} }
if ogRes.chanPoint != diskRes.chanPoint { if ogRes.chanPoint != diskRes.chanPoint {
t.Fatalf("expected %v, got %v", ogRes.chanPoint, t.Fatalf("expected %v, got %v", ogRes.chanPoint,
@@ -341,7 +341,7 @@ func TestContractInsertionRetrieval(t *testing.T) {
SelfOutputSignDesc: testSignDesc, SelfOutputSignDesc: testSignDesc,
MaturityDelay: 99, MaturityDelay: 99,
}, },
broadcastHeight: 109, confirmHeight: 109,
chanPoint: testChanPoint1, chanPoint: testChanPoint1,
} }
commitResolver.resolved.Store(false) commitResolver.resolved.Store(false)
+12 -48
View File
@@ -38,10 +38,10 @@ type commitSweepResolver struct {
// this HTLC on-chain. // this HTLC on-chain.
commitResolution lnwallet.CommitOutputResolution commitResolution lnwallet.CommitOutputResolution
// broadcastHeight is the height that the original contract was // confirmHeight is the block height that the commitment transaction was
// broadcast to the main-chain at. We'll use this value to bound any // confirmed at. We'll use this value to bound any historical queries to
// historical queries to the chain for spends/confirmations. // the chain for spends/confirmations.
broadcastHeight uint32 confirmHeight uint32
// chanPoint is the channel point of the original contract. // chanPoint is the channel point of the original contract.
chanPoint wire.OutPoint chanPoint wire.OutPoint
@@ -74,13 +74,13 @@ type commitSweepResolver struct {
// newCommitSweepResolver instantiates a new direct commit output resolver. // newCommitSweepResolver instantiates a new direct commit output resolver.
func newCommitSweepResolver(res lnwallet.CommitOutputResolution, func newCommitSweepResolver(res lnwallet.CommitOutputResolution,
broadcastHeight uint32, chanPoint wire.OutPoint, confirmHeight uint32, chanPoint wire.OutPoint,
resCfg ResolverConfig) *commitSweepResolver { resCfg ResolverConfig) *commitSweepResolver {
r := &commitSweepResolver{ r := &commitSweepResolver{
contractResolverKit: *newContractResolverKit(resCfg), contractResolverKit: *newContractResolverKit(resCfg),
commitResolution: res, commitResolution: res,
broadcastHeight: broadcastHeight, confirmHeight: confirmHeight,
chanPoint: chanPoint, chanPoint: chanPoint,
} }
@@ -123,37 +123,6 @@ func waitForSpend(op *wire.OutPoint, pkScript []byte, heightHint uint32,
} }
} }
// getCommitTxConfHeight waits for confirmation of the commitment tx and
// returns the confirmation height.
func (c *commitSweepResolver) getCommitTxConfHeight() (uint32, error) {
txID := c.commitResolution.SelfOutPoint.Hash
signDesc := c.commitResolution.SelfOutputSignDesc
pkScript := signDesc.Output.PkScript
const confDepth = 1
confChan, err := c.Notifier.RegisterConfirmationsNtfn(
&txID, pkScript, confDepth, c.broadcastHeight,
)
if err != nil {
return 0, err
}
defer confChan.Cancel()
select {
case txConfirmation, ok := <-confChan.Confirmed:
if !ok {
return 0, fmt.Errorf("cannot get confirmation "+
"for commit tx %v", txID)
}
return txConfirmation.BlockHeight, nil
case <-c.quit:
return 0, errResolverShuttingDown
}
}
// Resolve instructs the contract resolver to resolve the output on-chain. Once // Resolve instructs the contract resolver to resolve the output on-chain. Once
// the output has been *fully* resolved, the function should return immediately // the output has been *fully* resolved, the function should return immediately
// with a nil ContractResolver value for the first return value. In the case // 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 {
if err := binary.Write(w, endian, c.IsResolved()); err != nil { if err := binary.Write(w, endian, c.IsResolved()); err != nil {
return err return err
} }
if err := binary.Write(w, endian, c.broadcastHeight); err != nil { if err := binary.Write(w, endian, c.confirmHeight); err != nil {
return err return err
} }
if _, err := w.Write(c.chanPoint.Hash[:]); err != nil { if _, err := w.Write(c.chanPoint.Hash[:]); err != nil {
@@ -308,7 +277,7 @@ func newCommitSweepResolverFromReader(r io.Reader, resCfg ResolverConfig) (
c.markResolved() c.markResolved()
} }
if err := binary.Read(r, endian, &c.broadcastHeight); err != nil { if err := binary.Read(r, endian, &c.confirmHeight); err != nil {
return nil, err return nil, err
} }
_, err := io.ReadFull(r, c.chanPoint.Hash[:]) _, err := io.ReadFull(r, c.chanPoint.Hash[:])
@@ -381,19 +350,14 @@ func (c *commitSweepResolver) Launch() error {
return nil return nil
} }
confHeight, err := c.getCommitTxConfHeight()
if err != nil {
return err
}
// Wait up until the CSV expires, unless we also have a CLTV that // Wait up until the CSV expires, unless we also have a CLTV that
// expires after. // expires after.
unlockHeight := confHeight + c.commitResolution.MaturityDelay unlockHeight := c.confirmHeight + c.commitResolution.MaturityDelay
if c.hasCLTV() { if c.hasCLTV() {
unlockHeight = max(unlockHeight, c.leaseExpiry) unlockHeight = max(unlockHeight, c.leaseExpiry)
} }
// Update report now that we learned the confirmation height. // Update report with the calculated maturity height.
c.reportLock.Lock() c.reportLock.Lock()
c.currentReport.MaturityHeight = unlockHeight c.currentReport.MaturityHeight = unlockHeight
c.reportLock.Unlock() c.reportLock.Unlock()
@@ -412,7 +376,7 @@ func (c *commitSweepResolver) Launch() error {
inp = input.NewCsvInputWithCltv( inp = input.NewCsvInputWithCltv(
&c.commitResolution.SelfOutPoint, witnessType, &c.commitResolution.SelfOutPoint, witnessType,
&c.commitResolution.SelfOutputSignDesc, &c.commitResolution.SelfOutputSignDesc,
c.broadcastHeight, c.commitResolution.MaturityDelay, c.confirmHeight, c.commitResolution.MaturityDelay,
c.leaseExpiry, input.WithResolutionBlob( c.leaseExpiry, input.WithResolutionBlob(
c.commitResolution.ResolutionBlob, c.commitResolution.ResolutionBlob,
), ),
@@ -421,7 +385,7 @@ func (c *commitSweepResolver) Launch() error {
inp = input.NewCsvInput( inp = input.NewCsvInput(
&c.commitResolution.SelfOutPoint, witnessType, &c.commitResolution.SelfOutPoint, witnessType,
&c.commitResolution.SelfOutputSignDesc, &c.commitResolution.SelfOutputSignDesc,
c.broadcastHeight, c.commitResolution.MaturityDelay, c.confirmHeight, c.commitResolution.MaturityDelay,
input.WithResolutionBlob( input.WithResolutionBlob(
c.commitResolution.ResolutionBlob, c.commitResolution.ResolutionBlob,
), ),
+24 -27
View File
@@ -18,6 +18,10 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
const (
testCommitSweepConfHeight = 99
)
type commitSweepResolverTestContext struct { type commitSweepResolverTestContext struct {
resolver *commitSweepResolver resolver *commitSweepResolver
notifier *mock.ChainNotifier notifier *mock.ChainNotifier
@@ -27,7 +31,8 @@ type commitSweepResolverTestContext struct {
} }
func newCommitSweepResolverTestContext(t *testing.T, func newCommitSweepResolverTestContext(t *testing.T,
resolution *lnwallet.CommitOutputResolution) *commitSweepResolverTestContext { resolution *lnwallet.CommitOutputResolution,
confirmHeight uint32) *commitSweepResolverTestContext {
notifier := &mock.ChainNotifier{ notifier := &mock.ChainNotifier{
EpochChan: make(chan *chainntnfs.BlockEpoch), EpochChan: make(chan *chainntnfs.BlockEpoch),
@@ -68,7 +73,7 @@ func newCommitSweepResolverTestContext(t *testing.T,
} }
resolver := newCommitSweepResolver( resolver := newCommitSweepResolver(
*resolution, 0, wire.OutPoint{}, cfg, *resolution, confirmHeight, wire.OutPoint{}, cfg,
) )
return &commitSweepResolverTestContext{ return &commitSweepResolverTestContext{
@@ -178,7 +183,9 @@ func TestCommitSweepResolverNoDelay(t *testing.T) {
}, },
} }
ctx := newCommitSweepResolverTestContext(t, &res) ctx := newCommitSweepResolverTestContext(
t, &res, testCommitSweepConfHeight,
)
// Replace our checkpoint with one which will push reports into a // Replace our checkpoint with one which will push reports into a
// channel for us to consume. We replace this function on the resolver // channel for us to consume. We replace this function on the resolver
@@ -197,15 +204,12 @@ func TestCommitSweepResolverNoDelay(t *testing.T) {
ctx.resolve() ctx.resolve()
spendTx := &wire.MsgTx{}
spendHash := spendTx.TxHash()
ctx.notifier.ConfChan <- &chainntnfs.TxConfirmation{
Tx: spendTx,
}
// No csv delay, so the input should be swept immediately. // No csv delay, so the input should be swept immediately.
<-ctx.sweeper.sweptInputs <-ctx.sweeper.sweptInputs
spendTx := &wire.MsgTx{}
spendHash := spendTx.TxHash()
amt := btcutil.Amount(res.SelfOutputSignDesc.Output.Value) amt := btcutil.Amount(res.SelfOutputSignDesc.Output.Value)
expectedReport := &channeldb.ResolverReport{ expectedReport := &channeldb.ResolverReport{
OutPoint: wire.OutPoint{}, OutPoint: wire.OutPoint{},
@@ -242,7 +246,10 @@ func testCommitSweepResolverDelay(t *testing.T, sweepErr error) {
SelfOutPoint: outpoint, SelfOutPoint: outpoint,
} }
ctx := newCommitSweepResolverTestContext(t, &res) // Use confirmHeight = 99, so maturityHeight = 99 + 3 = 102.
ctx := newCommitSweepResolverTestContext(
t, &res, testCommitSweepConfHeight,
)
// Replace our checkpoint with one which will push reports into a // Replace our checkpoint with one which will push reports into a
// channel for us to consume. We replace this function on the resolver // channel for us to consume. We replace this function on the resolver
@@ -270,25 +277,18 @@ func testCommitSweepResolverDelay(t *testing.T, sweepErr error) {
Amount: btcutil.Amount(amt), Amount: btcutil.Amount(amt),
LimboBalance: btcutil.Amount(amt), LimboBalance: btcutil.Amount(amt),
} }
if *report != expectedReport { require.Equal(t, expectedReport, *report)
t.Fatalf("unexpected resolver report. want=%v got=%v",
expectedReport, report)
}
ctx.resolve() ctx.resolve()
ctx.notifier.ConfChan <- &chainntnfs.TxConfirmation{ // Allow resolver to launch and update the report.
BlockHeight: testInitialBlockHeight - 1,
}
// Allow resolver to process confirmation.
time.Sleep(sweepProcessInterval) time.Sleep(sweepProcessInterval)
// Expect report to be updated. // Expect report to be updated.
// confirmHeight(99) + maturityDelay(3) = 102.
report = ctx.resolver.report() report = ctx.resolver.report()
if report.MaturityHeight != testInitialBlockHeight+2 { expectedMaturity := testCommitSweepConfHeight + res.MaturityDelay
t.Fatal("report maturity height incorrect") require.Equal(t, expectedMaturity, report.MaturityHeight)
}
// Notify initial block height. Although the csv lock is still in // Notify initial block height. Although the csv lock is still in
// effect, we expect the input being sent to the sweeper before the csv // effect, we expect the input being sent to the sweeper before the csv
@@ -325,13 +325,10 @@ func testCommitSweepResolverDelay(t *testing.T, sweepErr error) {
Outpoint: outpoint, Outpoint: outpoint,
Type: ReportOutputUnencumbered, Type: ReportOutputUnencumbered,
Amount: btcutil.Amount(amt), Amount: btcutil.Amount(amt),
MaturityHeight: testInitialBlockHeight + 2, MaturityHeight: testCommitSweepConfHeight + res.MaturityDelay,
RecoveredBalance: expectedRecoveredBalance, RecoveredBalance: expectedRecoveredBalance,
} }
if *report != expectedReport { require.Equal(t, expectedReport, *report)
t.Fatalf("unexpected resolver report. want=%v got=%v",
expectedReport, report)
}
} }
// TestCommitSweepResolverDelay tests resolution of a direct commitment output // TestCommitSweepResolverDelay tests resolution of a direct commitment output
@@ -24,6 +24,10 @@
* Fix bug where channels with both [policies disabled at startup could never * Fix bug where channels with both [policies disabled at startup could never
be used for routing](https://github.com/lightningnetwork/lnd/pull/10378) be used for routing](https://github.com/lightningnetwork/lnd/pull/10378)
* [Fix a case where resolving the
to_local/to_remote output](https://github.com/lightningnetwork/lnd/pull/10387)
might take too long.
# New Features # New Features
## Functional Enhancements ## Functional Enhancements