From 8432e706d35535f091a9f31e9c69fe4746b41d42 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Mon, 10 Feb 2025 19:05:52 -0800 Subject: [PATCH] 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. --- channeldb/channel.go | 28 ++++++++++++++++++++++++++++ contractcourt/chain_watcher.go | 30 +----------------------------- lnwallet/channel.go | 8 ++++++++ 3 files changed, 37 insertions(+), 29 deletions(-) diff --git a/channeldb/channel.go b/channeldb/channel.go index 6bf20373c..e16537f1c 100644 --- a/channeldb/channel.go +++ b/channeldb/channel.go @@ -4104,6 +4104,34 @@ func (c *OpenChannel) AbsoluteThawHeight() (uint32, error) { return c.ThawHeight, nil } +// DeriveHeightHint derives the block height for the channel opening. +func (c *OpenChannel) DeriveHeightHint() uint32 { + // As a height hint, we'll try to use the opening height, but if the + // channel isn't yet open, then we'll use the height it was broadcast + // at. This may be an unconfirmed zero-conf channel. + heightHint := c.ShortChanID().BlockHeight + if heightHint == 0 { + heightHint = c.BroadcastHeight() + } + + // Since no zero-conf state is stored in a channel backup, the below + // logic will not be triggered for restored, zero-conf channels. Set + // the height hint for zero-conf channels. + if c.IsZeroConf() { + if c.ZeroConfConfirmed() { + // If the zero-conf channel is confirmed, we'll use the + // confirmed SCID's block height. + heightHint = c.ZeroConfRealScid().BlockHeight + } else { + // The zero-conf channel is unconfirmed. We'll need to + // use the FundingBroadcastHeight. + heightHint = c.BroadcastHeight() + } + } + + return heightHint +} + func putChannelCloseSummary(tx kvdb.RwTx, chanID []byte, summary *ChannelCloseSummary, lastChanState *OpenChannel) error { diff --git a/contractcourt/chain_watcher.go b/contractcourt/chain_watcher.go index fef70b945..06665af29 100644 --- a/contractcourt/chain_watcher.go +++ b/contractcourt/chain_watcher.go @@ -281,7 +281,7 @@ func newChainWatcher(cfg chainWatcherConfig) (*chainWatcher, error) { } // Get the channel opening block height. - heightHint := deriveHeightHint(chanState) + heightHint := chanState.DeriveHeightHint() // We'll register for a notification to be dispatched if the funding // output is spent. @@ -1328,34 +1328,6 @@ func deriveFundingPkScript(chanState *channeldb.OpenChannel) ([]byte, error) { return fundingPkScript, nil } -// deriveHeightHint derives the block height for the channel opening. -func deriveHeightHint(chanState *channeldb.OpenChannel) uint32 { - // As a height hint, we'll try to use the opening height, but if the - // channel isn't yet open, then we'll use the height it was broadcast - // at. This may be an unconfirmed zero-conf channel. - heightHint := chanState.ShortChanID().BlockHeight - if heightHint == 0 { - heightHint = chanState.BroadcastHeight() - } - - // Since no zero-conf state is stored in a channel backup, the below - // logic will not be triggered for restored, zero-conf channels. Set - // the height hint for zero-conf channels. - if chanState.IsZeroConf() { - if chanState.ZeroConfConfirmed() { - // If the zero-conf channel is confirmed, we'll use the - // confirmed SCID's block height. - heightHint = chanState.ZeroConfRealScid().BlockHeight - } else { - // The zero-conf channel is unconfirmed. We'll need to - // use the FundingBroadcastHeight. - heightHint = chanState.BroadcastHeight() - } - } - - return heightHint -} - // handleCommitSpend takes a spending tx of the funding output and handles the // channel close based on the closure type. func (c *chainWatcher) handleCommitSpend( diff --git a/lnwallet/channel.go b/lnwallet/channel.go index bc9820e8f..aa8e21e76 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -9874,6 +9874,14 @@ func (lc *LightningChannel) FundingTxOut() *wire.TxOut { return &lc.fundingOutput } +// DeriveHeightHint derives the block height for the channel opening. +func (lc *LightningChannel) DeriveHeightHint() uint32 { + lc.RLock() + defer lc.RUnlock() + + return lc.channelState.DeriveHeightHint() +} + // MultiSigKeys returns the set of multi-sig keys for an channel. func (lc *LightningChannel) MultiSigKeys() (keychain.KeyDescriptor, keychain.KeyDescriptor) {