@@ -370,8 +370,8 @@ impl OnchainEventEntry {
370
370
conf_threshold
371
371
}
372
372
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 ( )
375
375
}
376
376
}
377
377
@@ -1331,7 +1331,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
1331
1331
macro_rules! claim_htlcs {
1332
1332
( $commitment_number: expr, $txid: expr) => {
1333
1333
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) ;
1335
1335
}
1336
1336
}
1337
1337
if let Some ( txid) = self . current_counterparty_commitment_txid {
@@ -1353,11 +1353,14 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
1353
1353
// *we* sign a holder commitment transaction, not when e.g. a watchtower broadcasts one of our
1354
1354
// holder commitment transactions.
1355
1355
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) ;
1358
1361
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) ;
1361
1364
}
1362
1365
}
1363
1366
}
@@ -1724,7 +1727,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
1724
1727
// Returns (1) `PackageTemplate`s that can be given to the OnChainTxHandler, so that the handler can
1725
1728
// broadcast transactions claiming holder HTLC commitment outputs and (2) a holder revokable
1726
1729
// 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 ) > ) {
1728
1731
let mut claim_requests = Vec :: with_capacity ( holder_tx. htlc_outputs . len ( ) ) ;
1729
1732
1730
1733
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> {
1743
1746
} ;
1744
1747
HolderHTLCOutput :: build_accepted ( payment_preimage, htlc. amount_msat )
1745
1748
} ;
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 ) ;
1747
1750
claim_requests. push ( htlc_package) ;
1748
1751
}
1749
1752
}
@@ -1856,7 +1859,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
1856
1859
} else if htlc. 0 . cltv_expiry > self . best_block . height ( ) + 1 {
1857
1860
// Don't broadcast HTLC-Timeout transactions immediately as they don't meet the
1858
1861
// 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.
1860
1863
// Note that we add + 1 as transactions are broadcastable when they can be
1861
1864
// confirmed in the next block.
1862
1865
continue ;
@@ -1926,13 +1929,13 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
1926
1929
1927
1930
if height > self . best_block . height ( ) {
1928
1931
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 ( ) {
1931
1934
self . best_block = BestBlock :: new ( block_hash, height) ;
1932
1935
self . onchain_events_awaiting_threshold_conf . retain ( |ref entry| entry. height <= height) ;
1933
1936
self . onchain_tx_handler . block_disconnected ( height + 1 , broadcaster, fee_estimator, logger) ;
1934
1937
Vec :: new ( )
1935
- }
1938
+ } else { Vec :: new ( ) }
1936
1939
}
1937
1940
1938
1941
fn transactions_confirmed < B : Deref , F : Deref , L : Deref > (
@@ -2004,33 +2007,49 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2004
2007
self . is_paying_spendable_output ( & tx, height, & logger) ;
2005
2008
}
2006
2009
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)
2008
2015
}
2009
2016
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.
2010
2024
fn block_confirmed < B : Deref , F : Deref , L : Deref > (
2011
2025
& mut self ,
2012
- height : u32 ,
2026
+ conf_height : u32 ,
2013
2027
txn_matched : Vec < & Transaction > ,
2014
2028
mut watch_outputs : Vec < TransactionOutputs > ,
2015
2029
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 ,
2019
2033
) -> Vec < TransactionOutputs >
2020
2034
where
2021
2035
B :: Target : BroadcasterInterface ,
2022
2036
F :: Target : FeeEstimator ,
2023
2037
L :: Target : Logger ,
2024
2038
{
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) ;
2026
2042
if should_broadcast {
2027
2043
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 ( ) ) ;
2029
2045
claimable_outpoints. push ( commitment_package) ;
2030
2046
self . pending_monitor_events . push ( MonitorEvent :: CommitmentTxBroadcasted ( self . funding_info . 0 ) ) ;
2031
2047
let commitment_tx = self . onchain_tx_handler . get_fully_signed_holder_tx ( & self . funding_redeemscript ) ;
2032
2048
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 ( ) ) ;
2034
2053
let new_outputs = self . get_broadcasted_holder_watch_outputs ( & self . current_holder_commitment_tx , & commitment_tx) ;
2035
2054
if !new_outputs. is_empty ( ) {
2036
2055
watch_outputs. push ( ( self . current_holder_commitment_tx . txid . clone ( ) , new_outputs) ) ;
@@ -2043,7 +2062,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2043
2062
self . onchain_events_awaiting_threshold_conf . drain ( ..) . collect :: < Vec < _ > > ( ) ;
2044
2063
let mut onchain_events_reaching_threshold_conf = Vec :: new ( ) ;
2045
2064
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 ) {
2047
2066
onchain_events_reaching_threshold_conf. push ( entry) ;
2048
2067
} else {
2049
2068
self . onchain_events_awaiting_threshold_conf . push ( entry) ;
@@ -2098,7 +2117,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2098
2117
}
2099
2118
}
2100
2119
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) ;
2102
2121
2103
2122
// Determine new outputs to watch by comparing against previously known outputs to watch,
2104
2123
// updating the latter in the process.
@@ -2200,7 +2219,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2200
2219
false
2201
2220
}
2202
2221
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 {
2204
2223
// We need to consider all HTLCs which are:
2205
2224
// * in any unrevoked counterparty commitment transaction, as they could broadcast said
2206
2225
// transactions and we'd end up in a race, or
@@ -2211,6 +2230,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2211
2230
// to the source, and if we don't fail the channel we will have to ensure that the next
2212
2231
// updates that peer sends us are update_fails, failing the channel if not. It's probably
2213
2232
// easier to just fail the channel as this case should be rare enough anyway.
2233
+ let height = self . best_block . height ( ) ;
2214
2234
macro_rules! scan_commitment {
2215
2235
( $htlcs: expr, $holder_tx: expr) => {
2216
2236
for ref htlc in $htlcs {
0 commit comments