Commit Graph
680 Commits
Author SHA1 Message Date
Olaoluwa Osuntokun ab4f89128f contractcourt: unify+simplify new re-org aware logic 2026-01-15 16:22:26 -08:00
Olaoluwa Osuntokun ebefe63f8e 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 f6f716ab7)
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.
2026-01-15 16:22:26 -08:00
Olaoluwa Osuntokun 8f5656e6e2 contractcourt: add generic close re-org tests
In this commit, we add a set of generic close re-org tests. The most
important test is the property based test, they will randomly confirm
transactions, generate a re-org, then assert that eventually we dtect
the final version.
2026-01-15 16:22:26 -08:00
Olaoluwa Osuntokun b6b4cefd08 contractcourt: add unit tests for rbf re-org cases
This set of new tests ensures that if have created N RBF variants of the
coop close transaction, that any of then can confirm, and be re-org'd,
with us detecting the final spend once it confirms deeploy enough.
2026-01-15 16:22:26 -08:00
Olaoluwa Osuntokun e0f7b0a033 contractcourt: update existing chain watcher tests due to new logic
All the tests need to send a confirmation _after_ the spend is detected
now.
2026-01-15 16:22:26 -08:00
Olaoluwa Osuntokun ce779a9707 contractcourt: add new chainWatcherTestHarness
We'll use this for all the upcoming tests.
2026-01-15 16:22:26 -08:00
Olaoluwa Osuntokun 12668b8af6 contractcourt: update close logic to handle re-orgs of depth n-1, where n is num confs
In this commit, we update the close logic to handle re-ogs up to the
final amount of confirmations. This is done generically, so we're able
to handle events such as: coop close confirm, re-org, breach confirm,
re-org, force close confirm, re-org, etc.

The upcoming set of new tests will exercise all of these cases.

We modify the block beat handling to unify the control flow. As it's
possible we get the beat, then see the spend, or the oher way around.
2026-01-15 16:22:26 -08:00
Olaoluwa Osuntokun c0f48d23b4 multi: add new ChannelCloseConfs param, thread thru as needed
In this commit, we add a new param that'll allow us to scale up the
number of confirmations before we act on a new close. We'll use this
later to improve the current on chain handling logic.
2026-01-15 16:22:26 -08:00
George Tsagkarelis 9a88999c41 contractcourt+lnwallet: move aux close finalization to chain watcher
We now execute the aux chan closer finalization within the chain
watcher. This is better as we don't need to rely on the remote party
being online and sending us a message. Instead we do the finalization
once the on-chain transaction has been confirmed.
2025-12-04 12:18:14 +01:00
George Tsagkarelis fd772d45b8 contractcourt: add aux chan closer to chain watcher 2025-12-04 12:17:40 +01:00
Yong c746aeed31 Merge pull request #10391 from ffranr/wip/add-field-committxblockheight-to-resolutionreq
lnwallet: add field `CommitTxBlockHeight` to `ResolutionReq`
2025-11-26 10:50:53 +08:00
ffranr 3d2b0d703e lnwallet: add field CommitTxBlockHeight to ResolutionReq
Introduce `CommitTxBlockHeight` field to the `ResolutionReq` structure
and related methods. This field records the block height where a
commitment transaction has confirmed.
2025-11-25 17:06:18 +00:00
ziggie 79b92a9fa3 contractcourt: use confheight instead of rescanning the chain 2025-11-22 01:19:43 +01:00
ziggie 5538803252 contracourt: fix comment 2025-11-22 01:17:38 +01:00
ziggie 8824244656 contracourt: rename broadcastHeight to confirmHeight
The broadcastHeight was misleading because the commit resolver
is only created when the commitment transaction is confirmed.
2025-11-22 01:17:34 +01:00
ziggie bed021fc4a contractcourt: patch 0 timelock baby outputs
Older LND versions had a bug which would create HTLCs with
0 locktime. The utxonursery will have problems dealing with such
htlc outputs because we do not allow height hints of 0. Now we
will fetch the closeSummary of the channel and will add a
conservative height for rescanning.
2025-10-13 12:50:04 +02:00
Olaoluwa Osuntokun 77f2d8a572 contractcourt+sweep+watchtower: use lnutils.SpewLogClosure for debug logging
In this commit, we update the contractcourt, sweep, and watchtower
packages to use lnutils.SpewLogClosure instead of directly calling
spew.Sdump in logging statements.
2025-09-05 18:20:51 -07:00
Boris Nagaev dee8ad3754 multi: context.Background() -> t.Context()
Use the new feature of Go 1.24, fix linter warnings.

This change was produced by:
 - running golangci-lint run --fix
 - sed 's/context.Background/t.Context/' -i `git grep -l context.Background | grep test.go`
 - manually fixing broken tests
 - itest, lntest: use ht.Context() where ht or hn is available
 - in HarnessNode.Stop() we keep using context.Background(), because it is
   called from a cleanup handler in which t.Context() is canceled already.
2025-08-30 14:13:44 -03:00
Boris Nagaev 2a599bedc0 multi: don't pass err.Error() to Fatalf
It resulted in interpreting the error message as a format string.
Use Fatal(err) instead.
2025-08-30 14:13:44 -03:00
yyforyongyu ae08a75410 contractcourt+sweep: make anchor inputs exclusive
We now make sure to sweep each anchor input in its own sweeping tx, if
economically feasible.
2025-08-04 16:07:33 +08:00
yyforyongyu 10b4ed6c28 contractcourt: replace MarkChannelClosed with NotifyChannelResolved 2025-07-25 17:13:43 +08:00
yyforyongyu d3501197be contractcourt: add a new chan resolvedChan to handle resolved channels
Thus we can mark channels as resolved in an async way to avoid deadlock.
2025-07-25 17:13:43 +08:00
ziggie 1b675273fe contractcourt: fix encoding 2025-07-08 19:16:28 +02:00
yyforyongyu 2b4c28204a contractcourt: only close quit in Stop
Make sure we don't prematurely close the `quit` chans.
2025-07-07 15:46:47 +03:00
Elle Mouton 8cf567b948 multi: use the "errors" package everywhere
Replace all usages of the "github.com/go-errors/errors" and
"github.com/pkg/errors" packages with the standard lib's "errors"
package. This ensures that error wrapping and `errors.Is` checks will
work as expected.
2025-06-30 09:46:55 +02:00
ziggie 490347b056 multi: update walletdb package 2025-04-09 18:47:53 +02:00
yyforyongyu 5a72d5258f htlcswitch+itest: catch link quit signal when processing hodlQueue
This commit makes sure when processing resolutions, e.g, settling
invoices, when the link is already broken, the process would exit with
an error. This fixes the issue we found in the itest, where an
unexpected empty remote pending commitment was created although the
remote peer is already offline.
2025-03-21 03:25:24 +08:00
yyforyongyu be4c4cc8ff multi: improve logging 2025-03-21 03:24:26 +08:00
yyforyongyu 66c94cf51f contractcourt: add String to CommitSet for logging 2025-03-21 03:24:26 +08:00
Olaoluwa Osuntokun 8432e706d3 multi: extract new DeriveHeightHint() function, use for new rbf closer
With this commit, we make sure we set the right height hint, even if the
channel is a zero conf channel.
2025-03-18 11:44:20 -05:00
Oliver Gugger 0e9b7c5fa2 contractcourt: fix rebase issue with removed variadic option
The optional variadic functional parameters probably got lost during a
rebase.
2025-03-10 21:52:02 +01:00
fuyangpengqi 150f72414a refactor: use a more straightforward return value
Signed-off-by: fuyangpengqi <995764973@qq.com>
2025-02-28 17:09:56 +08:00
yyforyongyu 9f7e2bfd96 contractcourt: fix errorlint 2025-02-20 23:14:12 +08:00
yyforyongyu b184afe227 sweep: handle missing inputs during fee bumping
This commit handles the case when the input is missing during the RBF
process, which could happen when the bumped tx has inputs being spent by
a third party. Normally we should be able to catch the spend early via
the spending notification and never attempt to fee bump the record.
However, due to the possible race between block notification and spend
notification, this cannot be guaranteed. Thus, we need to handle the
case during the RBF when seeing a `ErrMissingInputs`, which can only
happen when the inputs are spent by others.
2025-02-20 14:40:53 +08:00
Eng Zer Jun e56a7945be refactor: replace math.Min and math.Max with built-in min/max
Reference: https://github.com/lightningnetwork/lnd/pull/9451#pullrequestreview-2580942691
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2025-02-06 09:46:40 +08:00
Elle e9dd01b60d Merge pull request #9274 from ziggie1984/remove-2x-value
The sweeper subsystem uses now also the configured budget values for HTLCs
2025-01-13 19:09:11 +02:00
Oliver Gugger b958667811 Merge pull request #9355 from Roasbeef/rapid-fuzz-htlc-blobs
contractcourt: add rapid derived fuzz test for HtlcAuxBlob
2025-01-13 10:35:53 -06:00
ziggie 5cd88f7e48 contractcourt: remove 2xamount requirement for the sweeper. 2025-01-08 08:57:50 +01:00
chloefeal 852a8d8746 chore: fix typo 2025-01-05 20:45:35 +08:00
yyforyongyu ecd82a3bcb contractcourt: include custom records on replayed htlc
Add another case in addition to #9357.
2024-12-20 17:54:13 +08:00
yyforyongyu 4806b2fda7 multi: optimize loggings around changes from blockbeat 2024-12-20 17:54:08 +08:00
yyforyongyu fecd5ac735 contractcourt: make sure launchResolvers is called on new blockbeat
This is an oversight from addressing this commment:
https://github.com/lightningnetwork/lnd/pull/9277#discussion_r1882410396

where we should focus on skipping the close events but not the
resolvers.
2024-12-20 17:54:08 +08:00
yyforyongyu ea7d6a509b contractcourt: register conf notification once and cancel when confirmed 2024-12-20 17:54:08 +08:00
yyforyongyu a6d3a0fa99 contractcourt: process channel close event on new beat 2024-12-20 17:54:07 +08:00
yyforyongyu c5b3033427 contractcourt: add close event handlers in ChannelArbitrator
To prepare the next commit where we would handle the event upon
receiving a blockbeat.
2024-12-20 17:54:07 +08:00
yyforyongyu 6eb9bb1ed6 multi: add new method ChainArbitrator.RedispatchBlockbeat
This commit adds a new method to enable us resending the blockbeat in
`ChainArbitrator`, which is needed for the channel restore as the chain
watcher and channel arbitrator are added after the start of the chain
arbitrator.
2024-12-20 17:54:07 +08:00
yyforyongyu 4d765668cc contractcourt: use close height instead of best height
This commit adds the closing height to the logging and fixes a wrong
height used in handling the breach event.
2024-12-20 17:54:07 +08:00
yyforyongyu 3822c23833 contractcourt: notify blockbeat for chainWatcher
We now start notifying the blockbeat from the ChainArbitrator to the
chainWatcher.
2024-12-20 17:54:07 +08:00
yyforyongyu 8237598ed1 contractcourt: handle blockbeat in chainWatcher 2024-12-20 17:54:07 +08:00
yyforyongyu 4e30598263 contractcourt: add method handleCommitSpend
To prepare for the blockbeat handler.
2024-12-20 17:54:07 +08:00