Skip to content

Commit 8b565d6

Browse files
committed
contractcourt: add sync dispatch fast-path for single confirmation closes
In this commit, we add a fast-path optimization to the chain watcher's closeObserver that immediately dispatches close events when only a single confirmation is required (numConfs == 1). This addresses a timing issue with integration tests that were designed around the old synchronous blockbeat behavior, where close events were dispatched immediately upon spend detection. The recent async confirmation architecture (introduced in commit f6f716a) properly handles reorgs by waiting for N confirmations before dispatching close events. However, this created a race condition in integration tests that mine blocks synchronously and expect immediate close notifications. With the build tag setting numConfs to 1 for itests, the async confirmation notification could arrive after the test already started waiting for the close event, causing timeouts. We introduce a new handleSpendDispatch method that checks if numConfs == 1 and, if so, immediately calls handleCommitSpend to dispatch the close event synchronously, then returns true to skip the async state machine. This preserves the old behavior for integration tests while maintaining the full async reorg protection for production (where numConfs >= 3). The implementation adds the fast-path check in both spend detection paths (blockbeat and spend notification) to ensure consistent behavior regardless of which detects the spend first. We also update the affected unit tests to remove their expectation of confirmation registration, since the fast-path bypasses that step entirely. This approach optimizes for the integration test scenario without compromising production safety, as the fast-path only activates when a single confirmation is sufficient - a configuration that only exists in the controlled test environment.
1 parent 29454c2 commit 8b565d6

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

contractcourt/chain_watcher.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,12 @@ func newChainSet(chanState *channeldb.OpenChannel) (*chainSet, error) {
693693
// - Pending (confNtfn != nil): Spend detected, waiting for N confirmations
694694
//
695695
// - Confirmed: Spend confirmed with N blocks, close has been processed
696+
//
697+
// For single-confirmation scenarios (numConfs == 1), we bypass the async state
698+
// machine and immediately dispatch close events upon spend detection. This
699+
// provides synchronous behavior for integration tests which expect immediate
700+
// notifications. For multi-confirmation scenarios (production with numConfs >= 3),
701+
// we use the full async state machine with reorg protection.
696702
func (c *chainWatcher) closeObserver() {
697703
defer c.wg.Done()
698704

@@ -803,6 +809,13 @@ func (c *chainWatcher) closeObserver() {
803809
continue
804810
}
805811

812+
// FAST PATH: Check if we should dispatch immediately for
813+
// single-confirmation scenarios.
814+
if c.handleSpendDispatch(spend, "blockbeat") {
815+
continue
816+
}
817+
818+
// ASYNC PATH: Multiple confirmations (production).
806819
// STATE TRANSITION: None -> Pending (from blockbeat).
807820
log.Infof("ChannelPoint(%v): detected spend from "+
808821
"blockbeat, transitioning to %v",
@@ -826,6 +839,13 @@ func (c *chainWatcher) closeObserver() {
826839
return
827840
}
828841

842+
// FAST PATH: Check if we should dispatch immediately for
843+
// single-confirmation scenarios.
844+
if c.handleSpendDispatch(spend, "spend notification") {
845+
continue
846+
}
847+
848+
// ASYNC PATH: Multiple confirmations (production).
829849
log.Infof("ChannelPoint(%v): detected spend from "+
830850
"notification, transitioning to %v",
831851
c.cfg.chanState.FundingOutpoint,
@@ -1582,6 +1602,30 @@ func deriveFundingPkScript(chanState *channeldb.OpenChannel) ([]byte, error) {
15821602
return fundingPkScript, nil
15831603
}
15841604

1605+
// handleSpendDispatch processes a detected spend. For single-confirmation
1606+
// scenarios (numConfs == 1), it immediately dispatches the close event and
1607+
// returns true. For multi-confirmation scenarios, it returns false, indicating
1608+
// the caller should proceed with the async state machine.
1609+
func (c *chainWatcher) handleSpendDispatch(spend *chainntnfs.SpendDetail,
1610+
source string) bool {
1611+
1612+
numConfs := c.requiredConfsForSpend()
1613+
if numConfs == 1 {
1614+
log.Infof("ChannelPoint(%v): single confirmation mode, "+
1615+
"dispatching immediately from %s",
1616+
c.cfg.chanState.FundingOutpoint, source)
1617+
1618+
err := c.handleCommitSpend(spend)
1619+
if err != nil {
1620+
log.Errorf("Failed to handle commit spend: %v", err)
1621+
}
1622+
1623+
return true
1624+
}
1625+
1626+
return false
1627+
}
1628+
15851629
// handleCommitSpend takes a spending tx of the funding output and handles the
15861630
// channel close based on the closure type.
15871631
func (c *chainWatcher) handleCommitSpend(

contractcourt/chain_watcher_test.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,9 @@ func TestChainWatcherRemoteUnilateralClose(t *testing.T) {
9494
t.Fatalf("unable to send blockbeat")
9595
}
9696

97-
// Wait for the chain watcher to register for confirmations and send
98-
// the confirmation. Since we set chanCloseConfs to 1, one confirmation
99-
// is sufficient.
100-
aliceNotifier.WaitForConfRegistrationAndSend(t)
97+
// With chanCloseConfs set to 1, the fast-path dispatches immediately
98+
// without confirmation registration. The close event should arrive
99+
// directly after processing the blockbeat.
101100

102101
// We should get a new spend event over the remote unilateral close
103102
// event channel.
@@ -231,10 +230,9 @@ func TestChainWatcherRemoteUnilateralClosePendingCommit(t *testing.T) {
231230
t.Fatalf("unable to send blockbeat")
232231
}
233232

234-
// Wait for the chain watcher to register for confirmations and send
235-
// the confirmation. Since we set chanCloseConfs to 1, one confirmation
236-
// is sufficient.
237-
aliceNotifier.WaitForConfRegistrationAndSend(t)
233+
// With chanCloseConfs set to 1, the fast-path dispatches immediately
234+
// without confirmation registration. The close event should arrive
235+
// directly after processing the blockbeat.
238236

239237
// We should get a new spend event over the remote unilateral close
240238
// event channel.

0 commit comments

Comments
 (0)