@@ -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
} ,
@@ -1720,6 +1728,7 @@ macro_rules! fail_unbroadcast_htlcs {
1720
1728
source: ( * * source) . clone( ) ,
1721
1729
payment_hash: htlc. payment_hash. clone( ) ,
1722
1730
htlc_value_satoshis: Some ( htlc. amount_msat / 1000 ) ,
1731
+ onchain_value_satoshis: None ,
1723
1732
commitment_tx_output_idx: None ,
1724
1733
} ,
1725
1734
} ;
@@ -2619,7 +2628,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2619
2628
// Produce actionable events from on-chain events having reached their threshold.
2620
2629
for entry in onchain_events_reaching_threshold_conf. drain ( ..) {
2621
2630
match entry. event {
2622
- OnchainEvent :: HTLCUpdate { ref source, payment_hash, htlc_value_satoshis, commitment_tx_output_idx } => {
2631
+ OnchainEvent :: HTLCUpdate { ref source, payment_hash, htlc_value_satoshis, commitment_tx_output_idx, .. } => {
2623
2632
// Check for duplicate HTLC resolutions.
2624
2633
#[ cfg( debug_assertions) ]
2625
2634
{
@@ -2850,6 +2859,14 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2850
2859
#[ cfg( not( fuzzing) ) ]
2851
2860
let offered_timeout_claim = witness_items == 5 && htlctype == Some ( HTLCType :: OfferedHTLC ) ;
2852
2861
2862
+ let claim_via_htlc_tx = accepted_preimage_claim || offered_timeout_claim;
2863
+ if claim_via_htlc_tx {
2864
+ // If the claim was via an HTLC-Timeout/HTLC-Success transaction, it must be a
2865
+ // 1-in-1-out transaction as its pre-signed.
2866
+ debug_assert_eq ! ( tx. input. len( ) , 1 ) ;
2867
+ debug_assert_eq ! ( tx. output. len( ) , 1 ) ; // If anchors are enabled this should be 2
2868
+ }
2869
+
2853
2870
let mut payment_preimage = PaymentPreimage ( [ 0 ; 32 ] ) ;
2854
2871
if accepted_preimage_claim {
2855
2872
payment_preimage. 0 . copy_from_slice ( input. witness . second_to_last ( ) . unwrap ( ) ) ;
@@ -2888,13 +2905,14 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2888
2905
}
2889
2906
2890
2907
macro_rules! check_htlc_valid_counterparty {
2891
- ( $counterparty_txid: expr, $htlc_output: expr) => {
2908
+ ( $counterparty_txid: expr, $htlc_output: expr, $htlc_value_sats : expr ) => {
2892
2909
if let Some ( txid) = $counterparty_txid {
2893
2910
for & ( ref pending_htlc, ref pending_source) in self . counterparty_claimable_outpoints. get( & txid) . unwrap( ) {
2894
2911
if pending_htlc. payment_hash == $htlc_output. payment_hash && pending_htlc. amount_msat == $htlc_output. amount_msat {
2895
2912
if let & Some ( ref source) = pending_source {
2896
2913
log_claim!( "revoked counterparty commitment tx" , false , pending_htlc, true ) ;
2897
- payment_data = Some ( ( ( * * source) . clone( ) , $htlc_output. payment_hash, $htlc_output. amount_msat) ) ;
2914
+ payment_data = Some ( ( ( * * source) . clone( ) , $htlc_output. payment_hash,
2915
+ pending_htlc. amount_msat, $htlc_value_sats) ) ;
2898
2916
break ;
2899
2917
}
2900
2918
}
@@ -2907,18 +2925,24 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2907
2925
( $htlcs: expr, $tx_info: expr, $holder_tx: expr) => {
2908
2926
for ( ref htlc_output, source_option) in $htlcs {
2909
2927
if Some ( input. previous_output. vout) == htlc_output. transaction_output_index {
2928
+ let htlc_value_sats = if claim_via_htlc_tx {
2929
+ tx. output. iter( ) . map( |txout| txout. value) . sum:: <u64 >( )
2930
+ } else { htlc_output. amount_msat / 1000 } ;
2910
2931
if let Some ( ref source) = source_option {
2911
2932
log_claim!( $tx_info, $holder_tx, htlc_output, true ) ;
2912
2933
// We have a resolution of an HTLC either from one of our latest
2913
2934
// holder commitment transactions or an unrevoked counterparty commitment
2914
2935
// transaction. This implies we either learned a preimage, the HTLC
2915
2936
// has timed out, or we screwed up. In any case, we should now
2916
2937
// resolve the source HTLC with the original sender.
2917
- payment_data = Some ( ( ( * source) . clone( ) , htlc_output. payment_hash, htlc_output. amount_msat) ) ;
2938
+ payment_data = Some ( ( ( * source) . clone( ) , htlc_output. payment_hash,
2939
+ htlc_output. amount_msat, htlc_value_sats) ) ;
2918
2940
} else if !$holder_tx {
2919
- check_htlc_valid_counterparty!( self . current_counterparty_commitment_txid, htlc_output) ;
2941
+ check_htlc_valid_counterparty!( self . current_counterparty_commitment_txid,
2942
+ htlc_output, htlc_value_sats) ;
2920
2943
if payment_data. is_none( ) {
2921
- check_htlc_valid_counterparty!( self . prev_counterparty_commitment_txid, htlc_output) ;
2944
+ check_htlc_valid_counterparty!( self . prev_counterparty_commitment_txid,
2945
+ htlc_output, htlc_value_sats) ;
2922
2946
}
2923
2947
}
2924
2948
if payment_data. is_none( ) {
@@ -2929,6 +2953,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2929
2953
txid: tx. txid( ) , height, transaction: Some ( tx. clone( ) ) ,
2930
2954
event: OnchainEvent :: HTLCSpendConfirmation {
2931
2955
commitment_tx_output_idx: input. previous_output. vout,
2956
+ onchain_value_satoshis: Some ( htlc_value_sats) ,
2932
2957
preimage: if accepted_preimage_claim || offered_preimage_claim {
2933
2958
Some ( payment_preimage) } else { None } ,
2934
2959
// If this is a payment to us (!outbound_htlc, above),
@@ -2971,7 +2996,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2971
2996
2972
2997
// Check that scan_commitment, above, decided there is some source worth relaying an
2973
2998
// HTLC resolution backwards to and figure out whether we learned a preimage from it.
2974
- if let Some ( ( source, payment_hash, amount_msat) ) = payment_data {
2999
+ if let Some ( ( source, payment_hash, amount_msat, onchain_amount_sat ) ) = payment_data {
2975
3000
if accepted_preimage_claim {
2976
3001
if !self . pending_monitor_events . iter ( ) . any (
2977
3002
|update| if let & MonitorEvent :: HTLCEvent ( ref upd) = update { upd. source == source } else { false } ) {
@@ -2983,6 +3008,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2983
3008
commitment_tx_output_idx : input. previous_output . vout ,
2984
3009
preimage : Some ( payment_preimage) ,
2985
3010
on_to_local_output_csv : None ,
3011
+ onchain_value_satoshis : Some ( onchain_amount_sat) ,
2986
3012
} ,
2987
3013
} ) ;
2988
3014
self . pending_monitor_events . push ( MonitorEvent :: HTLCEvent ( HTLCUpdate {
@@ -3005,6 +3031,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
3005
3031
commitment_tx_output_idx : input. previous_output . vout ,
3006
3032
preimage : Some ( payment_preimage) ,
3007
3033
on_to_local_output_csv : None ,
3034
+ onchain_value_satoshis : Some ( onchain_amount_sat) ,
3008
3035
} ,
3009
3036
} ) ;
3010
3037
self . pending_monitor_events . push ( MonitorEvent :: HTLCEvent ( HTLCUpdate {
@@ -3032,6 +3059,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
3032
3059
source, payment_hash,
3033
3060
htlc_value_satoshis : Some ( amount_msat / 1000 ) ,
3034
3061
commitment_tx_output_idx : Some ( input. previous_output . vout ) ,
3062
+ onchain_value_satoshis : Some ( onchain_amount_sat) ,
3035
3063
} ,
3036
3064
} ;
3037
3065
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