@@ -370,8 +370,8 @@ impl OnchainEventEntry {
370370 conf_threshold
371371 }
372372
373- fn has_reached_confirmation_threshold ( & self , height : u32 ) -> bool {
374- height >= self . confirmation_threshold ( )
373+ fn has_reached_confirmation_threshold ( & self , best_block : & BestBlock ) -> bool {
374+ best_block . height ( ) >= self . confirmation_threshold ( )
375375 }
376376}
377377
@@ -1331,7 +1331,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
13311331 macro_rules! claim_htlcs {
13321332 ( $commitment_number: expr, $txid: expr) => {
13331333 let htlc_claim_reqs = self . get_counterparty_htlc_output_claim_reqs( $commitment_number, $txid, None ) ;
1334- self . onchain_tx_handler. update_claims_view( & Vec :: new( ) , htlc_claim_reqs, self . best_block. height( ) , broadcaster, fee_estimator, logger) ;
1334+ self . onchain_tx_handler. update_claims_view( & Vec :: new( ) , htlc_claim_reqs, self . best_block. height( ) , self . best_block . height ( ) , broadcaster, fee_estimator, logger) ;
13351335 }
13361336 }
13371337 if let Some ( txid) = self . current_counterparty_commitment_txid {
@@ -1353,11 +1353,14 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
13531353 // *we* sign a holder commitment transaction, not when e.g. a watchtower broadcasts one of our
13541354 // holder commitment transactions.
13551355 if self . broadcasted_holder_revokable_script . is_some ( ) {
1356- let ( claim_reqs, _) = self . get_broadcasted_holder_claims ( & self . current_holder_commitment_tx , 0 ) ;
1357- self . onchain_tx_handler . update_claims_view ( & Vec :: new ( ) , claim_reqs, self . best_block . height ( ) , broadcaster, fee_estimator, logger) ;
1356+ // Assume that the broadcasted commitment transaction confirmed in the current best
1357+ // block. Even if not, its a reasonable metric for the bump criteria on the HTLC
1358+ // transactions.
1359+ let ( claim_reqs, _) = self . get_broadcasted_holder_claims ( & self . current_holder_commitment_tx , self . best_block . height ( ) ) ;
1360+ self . onchain_tx_handler . update_claims_view ( & Vec :: new ( ) , claim_reqs, self . best_block . height ( ) , self . best_block . height ( ) , broadcaster, fee_estimator, logger) ;
13581361 if let Some ( ref tx) = self . prev_holder_signed_commitment_tx {
1359- let ( claim_reqs, _) = self . get_broadcasted_holder_claims ( & tx, 0 ) ;
1360- self . onchain_tx_handler . update_claims_view ( & Vec :: new ( ) , claim_reqs, self . best_block . height ( ) , broadcaster, fee_estimator, logger) ;
1362+ let ( claim_reqs, _) = self . get_broadcasted_holder_claims ( & tx, self . best_block . height ( ) ) ;
1363+ self . onchain_tx_handler . update_claims_view ( & Vec :: new ( ) , claim_reqs, self . best_block . height ( ) , self . best_block . height ( ) , broadcaster, fee_estimator, logger) ;
13611364 }
13621365 }
13631366 }
@@ -1724,7 +1727,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
17241727 // Returns (1) `PackageTemplate`s that can be given to the OnChainTxHandler, so that the handler can
17251728 // broadcast transactions claiming holder HTLC commitment outputs and (2) a holder revokable
17261729 // script so we can detect whether a holder transaction has been seen on-chain.
1727- fn get_broadcasted_holder_claims ( & self , holder_tx : & HolderSignedTx , height : u32 ) -> ( Vec < PackageTemplate > , Option < ( Script , PublicKey , PublicKey ) > ) {
1730+ fn get_broadcasted_holder_claims ( & self , holder_tx : & HolderSignedTx , conf_height : u32 ) -> ( Vec < PackageTemplate > , Option < ( Script , PublicKey , PublicKey ) > ) {
17281731 let mut claim_requests = Vec :: with_capacity ( holder_tx. htlc_outputs . len ( ) ) ;
17291732
17301733 let redeemscript = chan_utils:: get_revokeable_redeemscript ( & holder_tx. revocation_key , self . on_holder_tx_csv , & holder_tx. delayed_payment_key ) ;
@@ -1743,7 +1746,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
17431746 } ;
17441747 HolderHTLCOutput :: build_accepted ( payment_preimage, htlc. amount_msat )
17451748 } ;
1746- let htlc_package = PackageTemplate :: build_package ( holder_tx. txid , transaction_output_index, PackageSolvingData :: HolderHTLCOutput ( htlc_output) , height , false , height ) ;
1749+ let htlc_package = PackageTemplate :: build_package ( holder_tx. txid , transaction_output_index, PackageSolvingData :: HolderHTLCOutput ( htlc_output) , htlc . cltv_expiry , false , conf_height ) ;
17471750 claim_requests. push ( htlc_package) ;
17481751 }
17491752 }
@@ -1856,7 +1859,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
18561859 } else if htlc. 0 . cltv_expiry > self . best_block . height ( ) + 1 {
18571860 // Don't broadcast HTLC-Timeout transactions immediately as they don't meet the
18581861 // current locktime requirements on-chain. We will broadcast them in
1859- // `block_confirmed` when `would_broadcast_at_height ` returns true.
1862+ // `block_confirmed` when `should_broadcast_holder_commitment_txn ` returns true.
18601863 // Note that we add + 1 as transactions are broadcastable when they can be
18611864 // confirmed in the next block.
18621865 continue ;
@@ -1926,13 +1929,13 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
19261929
19271930 if height > self . best_block . height ( ) {
19281931 self . best_block = BestBlock :: new ( block_hash, height) ;
1929- self . block_confirmed ( height, vec ! [ ] , vec ! [ ] , vec ! [ ] , broadcaster, fee_estimator, logger)
1930- } else {
1932+ self . block_confirmed ( height, vec ! [ ] , vec ! [ ] , vec ! [ ] , & broadcaster, & fee_estimator, & logger)
1933+ } else if block_hash != self . best_block . block_hash ( ) {
19311934 self . best_block = BestBlock :: new ( block_hash, height) ;
19321935 self . onchain_events_awaiting_threshold_conf . retain ( |ref entry| entry. height <= height) ;
19331936 self . onchain_tx_handler . block_disconnected ( height + 1 , broadcaster, fee_estimator, logger) ;
19341937 Vec :: new ( )
1935- }
1938+ } else { Vec :: new ( ) }
19361939 }
19371940
19381941 fn transactions_confirmed < B : Deref , F : Deref , L : Deref > (
@@ -2004,33 +2007,49 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
20042007 self . is_paying_spendable_output ( & tx, height, & logger) ;
20052008 }
20062009
2007- self . block_confirmed ( height, txn_matched, watch_outputs, claimable_outpoints, broadcaster, fee_estimator, logger)
2010+ if height > self . best_block . height ( ) {
2011+ self . best_block = BestBlock :: new ( block_hash, height) ;
2012+ }
2013+
2014+ self . block_confirmed ( height, txn_matched, watch_outputs, claimable_outpoints, & broadcaster, & fee_estimator, & logger)
20082015 }
20092016
2017+ /// Update state for new block(s)/transaction(s) confirmed. Note that the caller must update
2018+ /// `self.best_block` before calling if a new best blockchain tip is available. More
2019+ /// concretely, `self.best_block` must never be at a lower height than `conf_height`, avoiding
2020+ /// complexity especially in `OnchainTx::update_claims_view`.
2021+ ///
2022+ /// `conf_height` should be set to the height at which any new transaction(s)/block(s) were
2023+ /// confirmed at, even if it is not the current best height.
20102024 fn block_confirmed < B : Deref , F : Deref , L : Deref > (
20112025 & mut self ,
2012- height : u32 ,
2026+ conf_height : u32 ,
20132027 txn_matched : Vec < & Transaction > ,
20142028 mut watch_outputs : Vec < TransactionOutputs > ,
20152029 mut claimable_outpoints : Vec < PackageTemplate > ,
2016- broadcaster : B ,
2017- fee_estimator : F ,
2018- logger : L ,
2030+ broadcaster : & B ,
2031+ fee_estimator : & F ,
2032+ logger : & L ,
20192033 ) -> Vec < TransactionOutputs >
20202034 where
20212035 B :: Target : BroadcasterInterface ,
20222036 F :: Target : FeeEstimator ,
20232037 L :: Target : Logger ,
20242038 {
2025- let should_broadcast = self . would_broadcast_at_height ( height, & logger) ;
2039+ debug_assert ! ( self . best_block. height( ) >= conf_height) ;
2040+
2041+ let should_broadcast = self . should_broadcast_holder_commitment_txn ( logger) ;
20262042 if should_broadcast {
20272043 let funding_outp = HolderFundingOutput :: build ( self . funding_redeemscript . clone ( ) ) ;
2028- let commitment_package = PackageTemplate :: build_package ( self . funding_info . 0 . txid . clone ( ) , self . funding_info . 0 . index as u32 , PackageSolvingData :: HolderFundingOutput ( funding_outp) , height, false , height) ;
2044+ let commitment_package = PackageTemplate :: build_package ( self . funding_info . 0 . txid . clone ( ) , self . funding_info . 0 . index as u32 , PackageSolvingData :: HolderFundingOutput ( funding_outp) , self . best_block . height ( ) , false , self . best_block . height ( ) ) ;
20292045 claimable_outpoints. push ( commitment_package) ;
20302046 self . pending_monitor_events . push ( MonitorEvent :: CommitmentTxBroadcasted ( self . funding_info . 0 ) ) ;
20312047 let commitment_tx = self . onchain_tx_handler . get_fully_signed_holder_tx ( & self . funding_redeemscript ) ;
20322048 self . holder_tx_signed = true ;
2033- let ( mut new_outpoints, _) = self . get_broadcasted_holder_claims ( & self . current_holder_commitment_tx , height) ;
2049+ // Because we're broadcasting a commitment transaction, we should construct the package
2050+ // assuming it gets confirmed in the next block. Sadly, we have code which considers
2051+ // "not yet confirmed" things as discardable, so we cannot do that here.
2052+ let ( mut new_outpoints, _) = self . get_broadcasted_holder_claims ( & self . current_holder_commitment_tx , self . best_block . height ( ) ) ;
20342053 let new_outputs = self . get_broadcasted_holder_watch_outputs ( & self . current_holder_commitment_tx , & commitment_tx) ;
20352054 if !new_outputs. is_empty ( ) {
20362055 watch_outputs. push ( ( self . current_holder_commitment_tx . txid . clone ( ) , new_outputs) ) ;
@@ -2043,7 +2062,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
20432062 self . onchain_events_awaiting_threshold_conf . drain ( ..) . collect :: < Vec < _ > > ( ) ;
20442063 let mut onchain_events_reaching_threshold_conf = Vec :: new ( ) ;
20452064 for entry in onchain_events_awaiting_threshold_conf {
2046- if entry. has_reached_confirmation_threshold ( height ) {
2065+ if entry. has_reached_confirmation_threshold ( & self . best_block ) {
20472066 onchain_events_reaching_threshold_conf. push ( entry) ;
20482067 } else {
20492068 self . onchain_events_awaiting_threshold_conf . push ( entry) ;
@@ -2098,7 +2117,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
20982117 }
20992118 }
21002119
2101- self . onchain_tx_handler . update_claims_view ( & txn_matched, claimable_outpoints, height, & & * broadcaster, & & * fee_estimator, & & * logger) ;
2120+ self . onchain_tx_handler . update_claims_view ( & txn_matched, claimable_outpoints, conf_height , self . best_block . height ( ) , broadcaster, fee_estimator, logger) ;
21022121
21032122 // Determine new outputs to watch by comparing against previously known outputs to watch,
21042123 // updating the latter in the process.
@@ -2200,7 +2219,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
22002219 false
22012220 }
22022221
2203- fn would_broadcast_at_height < L : Deref > ( & self , height : u32 , logger : & L ) -> bool where L :: Target : Logger {
2222+ fn should_broadcast_holder_commitment_txn < L : Deref > ( & self , logger : & L ) -> bool where L :: Target : Logger {
22042223 // We need to consider all HTLCs which are:
22052224 // * in any unrevoked counterparty commitment transaction, as they could broadcast said
22062225 // transactions and we'd end up in a race, or
@@ -2211,6 +2230,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
22112230 // to the source, and if we don't fail the channel we will have to ensure that the next
22122231 // updates that peer sends us are update_fails, failing the channel if not. It's probably
22132232 // easier to just fail the channel as this case should be rare enough anyway.
2233+ let height = self . best_block . height ( ) ;
22142234 macro_rules! scan_commitment {
22152235 ( $htlcs: expr, $holder_tx: expr) => {
22162236 for ref htlc in $htlcs {
0 commit comments