@@ -361,6 +361,9 @@ enum OnchainEvent {
361
361
HTLCUpdate {
362
362
source : HTLCSource ,
363
363
payment_hash : PaymentHash ,
364
+ /// If the HTLC was claimed by an HTLC-Success or HTLC-Timeout transaction, this is the
365
+ /// amount of the output of that transaction (ie `htlc_value_satoshis` minus fees).
366
+ onchain_value_satoshis : Option < u64 > ,
364
367
htlc_value_satoshis : Option < u64 > ,
365
368
/// None in the second case, above, ie when there is no relevant output in the commitment
366
369
/// transaction which appeared on chain.
@@ -392,6 +395,9 @@ enum OnchainEvent {
392
395
/// signature.
393
396
HTLCSpendConfirmation {
394
397
commitment_tx_output_idx : u32 ,
398
+ /// If the HTLC was claimed by an HTLC-Success or HTLC-Timeout transaction, this is the
399
+ /// amount of the output of that transaction (ie `htlc_value_satoshis` minus fees).
400
+ onchain_value_satoshis : Option < u64 > ,
395
401
/// If the claim was made by either party with a preimage, this is filled in
396
402
preimage : Option < PaymentPreimage > ,
397
403
/// If the claim was made by us on an inbound HTLC against a local commitment transaction,
@@ -439,6 +445,7 @@ impl_writeable_tlv_based_enum_upgradable!(OnchainEvent,
439
445
( 1 , htlc_value_satoshis, option) ,
440
446
( 2 , payment_hash, required) ,
441
447
( 3 , commitment_tx_output_idx, option) ,
448
+ ( 5 , onchain_value_satoshis, option) ,
442
449
} ,
443
450
( 1 , MaturingOutput ) => {
444
451
( 0 , descriptor, required) ,
@@ -449,6 +456,7 @@ impl_writeable_tlv_based_enum_upgradable!(OnchainEvent,
449
456
} ,
450
457
( 5 , HTLCSpendConfirmation ) => {
451
458
( 0 , commitment_tx_output_idx, required) ,
459
+ ( 1 , onchain_value_satoshis, option) ,
452
460
( 2 , preimage, option) ,
453
461
( 4 , on_to_local_output_csv, option) ,
454
462
} ,
@@ -1729,6 +1737,7 @@ macro_rules! fail_unbroadcast_htlcs {
1729
1737
source: ( * * source) . clone( ) ,
1730
1738
payment_hash: htlc. payment_hash. clone( ) ,
1731
1739
htlc_value_satoshis: Some ( htlc. amount_msat / 1000 ) ,
1740
+ onchain_value_satoshis: None ,
1732
1741
commitment_tx_output_idx: None ,
1733
1742
} ,
1734
1743
} ;
@@ -2661,7 +2670,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2661
2670
// Produce actionable events from on-chain events having reached their threshold.
2662
2671
for entry in onchain_events_reaching_threshold_conf. drain ( ..) {
2663
2672
match entry. event {
2664
- OnchainEvent :: HTLCUpdate { ref source, payment_hash, htlc_value_satoshis, commitment_tx_output_idx } => {
2673
+ OnchainEvent :: HTLCUpdate { ref source, payment_hash, htlc_value_satoshis, commitment_tx_output_idx, .. } => {
2665
2674
// Check for duplicate HTLC resolutions.
2666
2675
#[ cfg( debug_assertions) ]
2667
2676
{
@@ -2902,6 +2911,14 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2902
2911
#[ cfg( not( fuzzing) ) ]
2903
2912
let offered_timeout_claim = witness_items == 5 && htlctype == Some ( HTLCType :: OfferedHTLC ) ;
2904
2913
2914
+ let claim_via_htlc_tx = accepted_preimage_claim || offered_timeout_claim;
2915
+ if claim_via_htlc_tx {
2916
+ // If the claim was via an HTLC-Timeout/HTLC-Success transaction, it must be a
2917
+ // 1-in-1-out transaction as its pre-signed.
2918
+ debug_assert_eq ! ( tx. input. len( ) , 1 ) ;
2919
+ debug_assert_eq ! ( tx. output. len( ) , 1 ) ; // If anchors are enabled this should be 2
2920
+ }
2921
+
2905
2922
let mut payment_preimage = PaymentPreimage ( [ 0 ; 32 ] ) ;
2906
2923
if accepted_preimage_claim {
2907
2924
payment_preimage. 0 . copy_from_slice ( input. witness . second_to_last ( ) . unwrap ( ) ) ;
@@ -2940,13 +2957,14 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2940
2957
}
2941
2958
2942
2959
macro_rules! check_htlc_valid_counterparty {
2943
- ( $counterparty_txid: expr, $htlc_output: expr) => {
2960
+ ( $counterparty_txid: expr, $htlc_output: expr, $htlc_value_sats : expr ) => {
2944
2961
if let Some ( txid) = $counterparty_txid {
2945
2962
for & ( ref pending_htlc, ref pending_source) in self . counterparty_claimable_outpoints. get( & txid) . unwrap( ) {
2946
2963
if pending_htlc. payment_hash == $htlc_output. payment_hash && pending_htlc. amount_msat == $htlc_output. amount_msat {
2947
2964
if let & Some ( ref source) = pending_source {
2948
2965
log_claim!( "revoked counterparty commitment tx" , false , pending_htlc, true ) ;
2949
- payment_data = Some ( ( ( * * source) . clone( ) , $htlc_output. payment_hash, $htlc_output. amount_msat) ) ;
2966
+ payment_data = Some ( ( ( * * source) . clone( ) , $htlc_output. payment_hash,
2967
+ pending_htlc. amount_msat, $htlc_value_sats) ) ;
2950
2968
break ;
2951
2969
}
2952
2970
}
@@ -2959,18 +2977,24 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2959
2977
( $htlcs: expr, $tx_info: expr, $holder_tx: expr) => {
2960
2978
for ( ref htlc_output, source_option) in $htlcs {
2961
2979
if Some ( input. previous_output. vout) == htlc_output. transaction_output_index {
2980
+ let htlc_value_sats = if claim_via_htlc_tx {
2981
+ tx. output. iter( ) . map( |txout| txout. value) . sum:: <u64 >( )
2982
+ } else { htlc_output. amount_msat / 1000 } ;
2962
2983
if let Some ( ref source) = source_option {
2963
2984
log_claim!( $tx_info, $holder_tx, htlc_output, true ) ;
2964
2985
// We have a resolution of an HTLC either from one of our latest
2965
2986
// holder commitment transactions or an unrevoked counterparty commitment
2966
2987
// transaction. This implies we either learned a preimage, the HTLC
2967
2988
// has timed out, or we screwed up. In any case, we should now
2968
2989
// resolve the source HTLC with the original sender.
2969
- payment_data = Some ( ( ( * source) . clone( ) , htlc_output. payment_hash, htlc_output. amount_msat) ) ;
2990
+ payment_data = Some ( ( ( * source) . clone( ) , htlc_output. payment_hash,
2991
+ htlc_output. amount_msat, htlc_value_sats) ) ;
2970
2992
} else if !$holder_tx {
2971
- check_htlc_valid_counterparty!( self . current_counterparty_commitment_txid, htlc_output) ;
2993
+ check_htlc_valid_counterparty!( self . current_counterparty_commitment_txid,
2994
+ htlc_output, htlc_value_sats) ;
2972
2995
if payment_data. is_none( ) {
2973
- check_htlc_valid_counterparty!( self . prev_counterparty_commitment_txid, htlc_output) ;
2996
+ check_htlc_valid_counterparty!( self . prev_counterparty_commitment_txid,
2997
+ htlc_output, htlc_value_sats) ;
2974
2998
}
2975
2999
}
2976
3000
if payment_data. is_none( ) {
@@ -2981,6 +3005,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2981
3005
txid: tx. txid( ) , height, transaction: Some ( tx. clone( ) ) ,
2982
3006
event: OnchainEvent :: HTLCSpendConfirmation {
2983
3007
commitment_tx_output_idx: input. previous_output. vout,
3008
+ onchain_value_satoshis: Some ( htlc_value_sats) ,
2984
3009
preimage: if accepted_preimage_claim || offered_preimage_claim {
2985
3010
Some ( payment_preimage) } else { None } ,
2986
3011
// If this is a payment to us (!outbound_htlc, above),
@@ -3023,7 +3048,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
3023
3048
3024
3049
// Check that scan_commitment, above, decided there is some source worth relaying an
3025
3050
// HTLC resolution backwards to and figure out whether we learned a preimage from it.
3026
- if let Some ( ( source, payment_hash, amount_msat) ) = payment_data {
3051
+ if let Some ( ( source, payment_hash, amount_msat, onchain_amount_sat ) ) = payment_data {
3027
3052
if accepted_preimage_claim {
3028
3053
if !self . pending_monitor_events . iter ( ) . any (
3029
3054
|update| if let & MonitorEvent :: HTLCEvent ( ref upd) = update { upd. source == source } else { false } ) {
@@ -3035,6 +3060,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
3035
3060
commitment_tx_output_idx : input. previous_output . vout ,
3036
3061
preimage : Some ( payment_preimage) ,
3037
3062
on_to_local_output_csv : None ,
3063
+ onchain_value_satoshis : Some ( onchain_amount_sat) ,
3038
3064
} ,
3039
3065
} ) ;
3040
3066
self . pending_monitor_events . push ( MonitorEvent :: HTLCEvent ( HTLCUpdate {
@@ -3057,6 +3083,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
3057
3083
commitment_tx_output_idx : input. previous_output . vout ,
3058
3084
preimage : Some ( payment_preimage) ,
3059
3085
on_to_local_output_csv : None ,
3086
+ onchain_value_satoshis : Some ( onchain_amount_sat) ,
3060
3087
} ,
3061
3088
} ) ;
3062
3089
self . pending_monitor_events . push ( MonitorEvent :: HTLCEvent ( HTLCUpdate {
@@ -3084,6 +3111,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
3084
3111
source, payment_hash,
3085
3112
htlc_value_satoshis : Some ( amount_msat / 1000 ) ,
3086
3113
commitment_tx_output_idx : Some ( input. previous_output . vout ) ,
3114
+ onchain_value_satoshis : Some ( onchain_amount_sat) ,
3087
3115
} ,
3088
3116
} ;
3089
3117
log_info ! ( logger, "Failing HTLC with payment_hash {} timeout by a spend tx, waiting for confirmation (at height {})" , log_bytes!( payment_hash. 0 ) , entry. confirmation_threshold( ) ) ;
0 commit comments