@@ -332,14 +332,15 @@ impl Readable for CounterpartyCommitmentParameters {
332
332
}
333
333
}
334
334
335
- /// An entry for an [`OnchainEvent`], stating the block height when the event was observed and the
336
- /// transaction causing it.
335
+ /// An entry for an [`OnchainEvent`], stating the block height and hash when the event was
336
+ /// observed, as well as the transaction causing it.
337
337
///
338
338
/// Used to determine when the on-chain event can be considered safe from a chain reorganization.
339
339
#[ derive( PartialEq , Eq ) ]
340
340
struct OnchainEventEntry {
341
341
txid : Txid ,
342
342
height : u32 ,
343
+ block_hash : Option < BlockHash > , // Added as optional, will be filled in for any entry generated on 0.0.113 or after
343
344
event : OnchainEvent ,
344
345
transaction : Option < Transaction > , // Added as optional, but always filled in, in LDK 0.0.110
345
346
}
@@ -440,6 +441,7 @@ impl Writeable for OnchainEventEntry {
440
441
( 0 , self . txid, required) ,
441
442
( 1 , self . transaction, option) ,
442
443
( 2 , self . height, required) ,
444
+ ( 3 , self . block_hash, option) ,
443
445
( 4 , self . event, required) ,
444
446
} ) ;
445
447
Ok ( ( ) )
@@ -450,16 +452,18 @@ impl MaybeReadable for OnchainEventEntry {
450
452
fn read < R : io:: Read > ( reader : & mut R ) -> Result < Option < Self > , DecodeError > {
451
453
let mut txid = Txid :: all_zeros ( ) ;
452
454
let mut transaction = None ;
455
+ let mut block_hash = None ;
453
456
let mut height = 0 ;
454
457
let mut event = None ;
455
458
read_tlv_fields ! ( reader, {
456
459
( 0 , txid, required) ,
457
460
( 1 , transaction, option) ,
458
461
( 2 , height, required) ,
462
+ ( 3 , block_hash, option) ,
459
463
( 4 , event, ignorable) ,
460
464
} ) ;
461
465
if let Some ( ev) = event {
462
- Ok ( Some ( Self { txid, transaction, height, event : ev } ) )
466
+ Ok ( Some ( Self { txid, transaction, height, block_hash , event : ev } ) )
463
467
} else {
464
468
Ok ( None )
465
469
}
@@ -1482,11 +1486,11 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
1482
1486
}
1483
1487
1484
1488
/// Returns the set of txids that should be monitored for re-organization out of the chain.
1485
- pub fn get_relevant_txids ( & self ) -> Vec < Txid > {
1489
+ pub fn get_relevant_txids ( & self ) -> Vec < ( Txid , Option < BlockHash > ) > {
1486
1490
let inner = self . inner . lock ( ) . unwrap ( ) ;
1487
- let mut txids: Vec < Txid > = inner. onchain_events_awaiting_threshold_conf
1491
+ let mut txids: Vec < ( Txid , Option < BlockHash > ) > = inner. onchain_events_awaiting_threshold_conf
1488
1492
. iter ( )
1489
- . map ( |entry| entry. txid )
1493
+ . map ( |entry| ( entry. txid , entry . block_hash ) )
1490
1494
. chain ( inner. onchain_tx_handler . get_relevant_txids ( ) . into_iter ( ) )
1491
1495
. collect ( ) ;
1492
1496
txids. sort_unstable ( ) ;
@@ -1939,7 +1943,7 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
1939
1943
/// been revoked yet, the previous one, we we will never "forget" to resolve an HTLC.
1940
1944
macro_rules! fail_unbroadcast_htlcs {
1941
1945
( $self: expr, $commitment_tx_type: expr, $commitment_txid_confirmed: expr, $commitment_tx_confirmed: expr,
1942
- $commitment_tx_conf_height: expr, $confirmed_htlcs_list: expr, $logger: expr) => { {
1946
+ $commitment_tx_conf_height: expr, $commitment_tx_conf_hash : expr , $ confirmed_htlcs_list: expr, $logger: expr) => { {
1943
1947
debug_assert_eq!( $commitment_tx_confirmed. txid( ) , $commitment_txid_confirmed) ;
1944
1948
1945
1949
macro_rules! check_htlc_fails {
@@ -1983,6 +1987,7 @@ macro_rules! fail_unbroadcast_htlcs {
1983
1987
txid: $commitment_txid_confirmed,
1984
1988
transaction: Some ( $commitment_tx_confirmed. clone( ) ) ,
1985
1989
height: $commitment_tx_conf_height,
1990
+ block_hash: Some ( * $commitment_tx_conf_hash) ,
1986
1991
event: OnchainEvent :: HTLCUpdate {
1987
1992
source: ( * * source) . clone( ) ,
1988
1993
payment_hash: htlc. payment_hash. clone( ) ,
@@ -2401,7 +2406,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2401
2406
/// Returns packages to claim the revoked output(s), as well as additional outputs to watch and
2402
2407
/// general information about the output that is to the counterparty in the commitment
2403
2408
/// transaction.
2404
- fn check_spend_counterparty_transaction < L : Deref > ( & mut self , tx : & Transaction , height : u32 , logger : & L )
2409
+ fn check_spend_counterparty_transaction < L : Deref > ( & mut self , tx : & Transaction , height : u32 , block_hash : & BlockHash , logger : & L )
2405
2410
-> ( Vec < PackageTemplate > , TransactionOutputs , CommitmentTxCounterpartyOutputInfo )
2406
2411
where L :: Target : Logger {
2407
2412
// Most secp and related errors trying to create keys means we have no hope of constructing
@@ -2472,13 +2477,13 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2472
2477
2473
2478
if let Some ( per_commitment_data) = per_commitment_option {
2474
2479
fail_unbroadcast_htlcs ! ( self , "revoked_counterparty" , commitment_txid, tx, height,
2475
- per_commitment_data. iter( ) . map( |( htlc, htlc_source) |
2480
+ block_hash , per_commitment_data. iter( ) . map( |( htlc, htlc_source) |
2476
2481
( htlc, htlc_source. as_ref( ) . map( |htlc_source| htlc_source. as_ref( ) ) )
2477
2482
) , logger) ;
2478
2483
} else {
2479
2484
debug_assert ! ( false , "We should have per-commitment option for any recognized old commitment txn" ) ;
2480
2485
fail_unbroadcast_htlcs ! ( self , "revoked counterparty" , commitment_txid, tx, height,
2481
- [ ] . iter( ) . map( |reference| * reference) , logger) ;
2486
+ block_hash , [ ] . iter( ) . map( |reference| * reference) , logger) ;
2482
2487
}
2483
2488
}
2484
2489
} else if let Some ( per_commitment_data) = per_commitment_option {
@@ -2495,7 +2500,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2495
2500
self . counterparty_commitment_txn_on_chain . insert ( commitment_txid, commitment_number) ;
2496
2501
2497
2502
log_info ! ( logger, "Got broadcast of non-revoked counterparty commitment transaction {}" , commitment_txid) ;
2498
- fail_unbroadcast_htlcs ! ( self , "counterparty" , commitment_txid, tx, height,
2503
+ fail_unbroadcast_htlcs ! ( self , "counterparty" , commitment_txid, tx, height, block_hash ,
2499
2504
per_commitment_data. iter( ) . map( |( htlc, htlc_source) |
2500
2505
( htlc, htlc_source. as_ref( ) . map( |htlc_source| htlc_source. as_ref( ) ) )
2501
2506
) , logger) ;
@@ -2631,7 +2636,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2631
2636
( claimable_outpoints, Some ( ( htlc_txid, outputs) ) )
2632
2637
}
2633
2638
2634
- // Returns (1) `PackageTemplate`s that can be given to the OnChainTxHandler , so that the handler can
2639
+ // Returns (1) `PackageTemplate`s that can be given to the OnchainTxHandler , so that the handler can
2635
2640
// broadcast transactions claiming holder HTLC commitment outputs and (2) a holder revokable
2636
2641
// script so we can detect whether a holder transaction has been seen on-chain.
2637
2642
fn get_broadcasted_holder_claims ( & self , holder_tx : & HolderSignedTx , conf_height : u32 ) -> ( Vec < PackageTemplate > , Option < ( Script , PublicKey , PublicKey ) > ) {
@@ -2676,7 +2681,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2676
2681
/// revoked using data in holder_claimable_outpoints.
2677
2682
/// Should not be used if check_spend_revoked_transaction succeeds.
2678
2683
/// Returns None unless the transaction is definitely one of our commitment transactions.
2679
- fn check_spend_holder_transaction < L : Deref > ( & mut self , tx : & Transaction , height : u32 , logger : & L ) -> Option < ( Vec < PackageTemplate > , TransactionOutputs ) > where L :: Target : Logger {
2684
+ fn check_spend_holder_transaction < L : Deref > ( & mut self , tx : & Transaction , height : u32 , block_hash : & BlockHash , logger : & L ) -> Option < ( Vec < PackageTemplate > , TransactionOutputs ) > where L :: Target : Logger {
2680
2685
let commitment_txid = tx. txid ( ) ;
2681
2686
let mut claim_requests = Vec :: new ( ) ;
2682
2687
let mut watch_outputs = Vec :: new ( ) ;
@@ -2699,7 +2704,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2699
2704
let mut to_watch = self . get_broadcasted_holder_watch_outputs ( & self . current_holder_commitment_tx , tx) ;
2700
2705
append_onchain_update ! ( res, to_watch) ;
2701
2706
fail_unbroadcast_htlcs ! ( self , "latest holder" , commitment_txid, tx, height,
2702
- self . current_holder_commitment_tx. htlc_outputs. iter( )
2707
+ block_hash , self . current_holder_commitment_tx. htlc_outputs. iter( )
2703
2708
. map( |( htlc, _, htlc_source) | ( htlc, htlc_source. as_ref( ) ) ) , logger) ;
2704
2709
} else if let & Some ( ref holder_tx) = & self . prev_holder_signed_commitment_tx {
2705
2710
if holder_tx. txid == commitment_txid {
@@ -2708,7 +2713,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2708
2713
let res = self . get_broadcasted_holder_claims ( holder_tx, height) ;
2709
2714
let mut to_watch = self . get_broadcasted_holder_watch_outputs ( holder_tx, tx) ;
2710
2715
append_onchain_update ! ( res, to_watch) ;
2711
- fail_unbroadcast_htlcs ! ( self , "previous holder" , commitment_txid, tx, height,
2716
+ fail_unbroadcast_htlcs ! ( self , "previous holder" , commitment_txid, tx, height, block_hash ,
2712
2717
holder_tx. htlc_outputs. iter( ) . map( |( htlc, _, htlc_source) | ( htlc, htlc_source. as_ref( ) ) ) ,
2713
2718
logger) ;
2714
2719
}
@@ -2816,7 +2821,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2816
2821
2817
2822
if height > self . best_block . height ( ) {
2818
2823
self . best_block = BestBlock :: new ( block_hash, height) ;
2819
- self . block_confirmed ( height, vec ! [ ] , vec ! [ ] , vec ! [ ] , & broadcaster, & fee_estimator, & logger)
2824
+ self . block_confirmed ( height, block_hash , vec ! [ ] , vec ! [ ] , vec ! [ ] , & broadcaster, & fee_estimator, & logger)
2820
2825
} else if block_hash != self . best_block . block_hash ( ) {
2821
2826
self . best_block = BestBlock :: new ( block_hash, height) ;
2822
2827
self . onchain_events_awaiting_threshold_conf . retain ( |ref entry| entry. height <= height) ;
@@ -2868,14 +2873,14 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2868
2873
let mut commitment_tx_to_counterparty_output = None ;
2869
2874
if ( tx. input [ 0 ] . sequence . 0 >> 8 * 3 ) as u8 == 0x80 && ( tx. lock_time . 0 >> 8 * 3 ) as u8 == 0x20 {
2870
2875
let ( mut new_outpoints, new_outputs, counterparty_output_idx_sats) =
2871
- self . check_spend_counterparty_transaction ( & tx, height, & logger) ;
2876
+ self . check_spend_counterparty_transaction ( & tx, height, & block_hash , & logger) ;
2872
2877
commitment_tx_to_counterparty_output = counterparty_output_idx_sats;
2873
2878
if !new_outputs. 1 . is_empty ( ) {
2874
2879
watch_outputs. push ( new_outputs) ;
2875
2880
}
2876
2881
claimable_outpoints. append ( & mut new_outpoints) ;
2877
2882
if new_outpoints. is_empty ( ) {
2878
- if let Some ( ( mut new_outpoints, new_outputs) ) = self . check_spend_holder_transaction ( & tx, height, & logger) {
2883
+ if let Some ( ( mut new_outpoints, new_outputs) ) = self . check_spend_holder_transaction ( & tx, height, & block_hash , & logger) {
2879
2884
debug_assert ! ( commitment_tx_to_counterparty_output. is_none( ) ,
2880
2885
"A commitment transaction matched as both a counterparty and local commitment tx?" ) ;
2881
2886
if !new_outputs. 1 . is_empty ( ) {
@@ -2891,6 +2896,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2891
2896
txid,
2892
2897
transaction : Some ( ( * tx) . clone ( ) ) ,
2893
2898
height,
2899
+ block_hash : Some ( block_hash) ,
2894
2900
event : OnchainEvent :: FundingSpendConfirmation {
2895
2901
on_local_output_csv : balance_spendable_csv,
2896
2902
commitment_tx_to_counterparty_output,
@@ -2909,16 +2915,16 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2909
2915
// While all commitment/HTLC-Success/HTLC-Timeout transactions have one input, HTLCs
2910
2916
// can also be resolved in a few other ways which can have more than one output. Thus,
2911
2917
// we call is_resolving_htlc_output here outside of the tx.input.len() == 1 check.
2912
- self . is_resolving_htlc_output ( & tx, height, & logger) ;
2918
+ self . is_resolving_htlc_output ( & tx, height, & block_hash , & logger) ;
2913
2919
2914
- self . is_paying_spendable_output ( & tx, height, & logger) ;
2920
+ self . is_paying_spendable_output ( & tx, height, & block_hash , & logger) ;
2915
2921
}
2916
2922
2917
2923
if height > self . best_block . height ( ) {
2918
2924
self . best_block = BestBlock :: new ( block_hash, height) ;
2919
2925
}
2920
2926
2921
- self . block_confirmed ( height, txn_matched, watch_outputs, claimable_outpoints, & broadcaster, & fee_estimator, & logger)
2927
+ self . block_confirmed ( height, block_hash , txn_matched, watch_outputs, claimable_outpoints, & broadcaster, & fee_estimator, & logger)
2922
2928
}
2923
2929
2924
2930
/// Update state for new block(s)/transaction(s) confirmed. Note that the caller must update
@@ -2931,6 +2937,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2931
2937
fn block_confirmed < B : Deref , F : Deref , L : Deref > (
2932
2938
& mut self ,
2933
2939
conf_height : u32 ,
2940
+ conf_hash : BlockHash ,
2934
2941
txn_matched : Vec < & Transaction > ,
2935
2942
mut watch_outputs : Vec < TransactionOutputs > ,
2936
2943
mut claimable_outpoints : Vec < PackageTemplate > ,
@@ -3235,7 +3242,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
3235
3242
3236
3243
/// Check if any transaction broadcasted is resolving HTLC output by a success or timeout on a holder
3237
3244
/// or counterparty commitment tx, if so send back the source, preimage if found and payment_hash of resolved HTLC
3238
- fn is_resolving_htlc_output < L : Deref > ( & mut self , tx : & Transaction , height : u32 , logger : & L ) where L :: Target : Logger {
3245
+ fn is_resolving_htlc_output < L : Deref > ( & mut self , tx : & Transaction , height : u32 , block_hash : & BlockHash , logger : & L ) where L :: Target : Logger {
3239
3246
' outer_loop: for input in & tx. input {
3240
3247
let mut payment_data = None ;
3241
3248
let htlc_claim = HTLCClaim :: from_witness ( & input. witness ) ;
@@ -3320,7 +3327,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
3320
3327
log_claim!( $tx_info, $holder_tx, htlc_output, false ) ;
3321
3328
let outbound_htlc = $holder_tx == htlc_output. offered;
3322
3329
self . onchain_events_awaiting_threshold_conf. push( OnchainEventEntry {
3323
- txid: tx. txid( ) , height, transaction: Some ( tx. clone( ) ) ,
3330
+ txid: tx. txid( ) , height, block_hash : Some ( * block_hash ) , transaction: Some ( tx. clone( ) ) ,
3324
3331
event: OnchainEvent :: HTLCSpendConfirmation {
3325
3332
commitment_tx_output_idx: input. previous_output. vout,
3326
3333
preimage: if accepted_preimage_claim || offered_preimage_claim {
@@ -3364,6 +3371,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
3364
3371
self . onchain_events_awaiting_threshold_conf . push ( OnchainEventEntry {
3365
3372
txid : tx. txid ( ) ,
3366
3373
height,
3374
+ block_hash : Some ( * block_hash) ,
3367
3375
transaction : Some ( tx. clone ( ) ) ,
3368
3376
event : OnchainEvent :: HTLCSpendConfirmation {
3369
3377
commitment_tx_output_idx : input. previous_output . vout ,
@@ -3387,6 +3395,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
3387
3395
txid : tx. txid ( ) ,
3388
3396
transaction : Some ( tx. clone ( ) ) ,
3389
3397
height,
3398
+ block_hash : Some ( * block_hash) ,
3390
3399
event : OnchainEvent :: HTLCSpendConfirmation {
3391
3400
commitment_tx_output_idx : input. previous_output . vout ,
3392
3401
preimage : Some ( payment_preimage) ,
@@ -3414,6 +3423,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
3414
3423
txid : tx. txid ( ) ,
3415
3424
transaction : Some ( tx. clone ( ) ) ,
3416
3425
height,
3426
+ block_hash : Some ( * block_hash) ,
3417
3427
event : OnchainEvent :: HTLCUpdate {
3418
3428
source, payment_hash,
3419
3429
htlc_value_satoshis : Some ( amount_msat / 1000 ) ,
@@ -3428,7 +3438,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
3428
3438
}
3429
3439
3430
3440
/// Check if any transaction broadcasted is paying fund back to some address we can assume to own
3431
- fn is_paying_spendable_output < L : Deref > ( & mut self , tx : & Transaction , height : u32 , logger : & L ) where L :: Target : Logger {
3441
+ fn is_paying_spendable_output < L : Deref > ( & mut self , tx : & Transaction , height : u32 , block_hash : & BlockHash , logger : & L ) where L :: Target : Logger {
3432
3442
let mut spendable_output = None ;
3433
3443
for ( i, outp) in tx. output . iter ( ) . enumerate ( ) { // There is max one spendable output for any channel tx, including ones generated by us
3434
3444
if i > :: core:: u16:: MAX as usize {
@@ -3488,6 +3498,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
3488
3498
txid : tx. txid ( ) ,
3489
3499
transaction : Some ( tx. clone ( ) ) ,
3490
3500
height,
3501
+ block_hash : Some ( * block_hash) ,
3491
3502
event : OnchainEvent :: MaturingOutput { descriptor : spendable_output. clone ( ) } ,
3492
3503
} ;
3493
3504
log_info ! ( logger, "Received spendable output {}, spendable at height {}" , log_spendable!( spendable_output) , entry. confirmation_threshold( ) ) ;
@@ -3529,7 +3540,7 @@ where
3529
3540
self . 0 . best_block_updated ( header, height, & * self . 1 , & * self . 2 , & * self . 3 ) ;
3530
3541
}
3531
3542
3532
- fn get_relevant_txids ( & self ) -> Vec < Txid > {
3543
+ fn get_relevant_txids ( & self ) -> Vec < ( Txid , Option < BlockHash > ) > {
3533
3544
self . 0 . get_relevant_txids ( )
3534
3545
}
3535
3546
}
0 commit comments