Skip to content

Commit 79b92a9

Browse files
committed
contractcourt: use confheight instead of rescanning the chain
1 parent 5538803 commit 79b92a9

File tree

2 files changed

+25
-64
lines changed

2 files changed

+25
-64
lines changed

contractcourt/commit_sweep_resolver.go

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -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.confirmHeight,
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
@@ -381,14 +350,9 @@ 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
}

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

0 commit comments

Comments
 (0)