@@ -361,6 +361,10 @@ 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 (ie when the
365
+ /// HTLC exists on chain), this is the amount of the output of that transaction (ie
366
+ /// `htlc_value_satoshis` minus fees).
367
+ onchain_value_satoshis : Option < u64 > ,
364
368
htlc_value_satoshis : Option < u64 > ,
365
369
/// None in the second case, above, ie when there is no relevant output in the commitment
366
370
/// transaction which appeared on chain.
@@ -392,6 +396,10 @@ enum OnchainEvent {
392
396
/// signature.
393
397
HTLCSpendConfirmation {
394
398
commitment_tx_output_idx : u32 ,
399
+ /// If the HTLC was claimed by an HTLC-Success or HTLC-Timeout transaction (ie when the
400
+ /// HTLC was not claimed via the revocation path), this is the amount of the output of that
401
+ /// transaction (ie `htlc_value_satoshis` minus fees).
402
+ onchain_value_satoshis : Option < u64 > ,
395
403
/// If the claim was made by either party with a preimage, this is filled in
396
404
preimage : Option < PaymentPreimage > ,
397
405
/// If the claim was made by us on an inbound HTLC against a local commitment transaction,
@@ -439,6 +447,7 @@ impl_writeable_tlv_based_enum_upgradable!(OnchainEvent,
439
447
( 1 , htlc_value_satoshis, option) ,
440
448
( 2 , payment_hash, required) ,
441
449
( 3 , commitment_tx_output_idx, option) ,
450
+ ( 5 , onchain_value_satoshis, option) ,
442
451
} ,
443
452
( 1 , MaturingOutput ) => {
444
453
( 0 , descriptor, required) ,
@@ -449,6 +458,7 @@ impl_writeable_tlv_based_enum_upgradable!(OnchainEvent,
449
458
} ,
450
459
( 5 , HTLCSpendConfirmation ) => {
451
460
( 0 , commitment_tx_output_idx, required) ,
461
+ ( 1 , onchain_value_satoshis, option) ,
452
462
( 2 , preimage, option) ,
453
463
( 4 , on_to_local_output_csv, option) ,
454
464
} ,
@@ -1734,6 +1744,7 @@ macro_rules! fail_unbroadcast_htlcs {
1734
1744
source: ( * * source) . clone( ) ,
1735
1745
payment_hash: htlc. payment_hash. clone( ) ,
1736
1746
htlc_value_satoshis: Some ( htlc. amount_msat / 1000 ) ,
1747
+ onchain_value_satoshis: None ,
1737
1748
commitment_tx_output_idx: None ,
1738
1749
} ,
1739
1750
} ;
@@ -2666,7 +2677,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2666
2677
// Produce actionable events from on-chain events having reached their threshold.
2667
2678
for entry in onchain_events_reaching_threshold_conf. drain ( ..) {
2668
2679
match entry. event {
2669
- OnchainEvent :: HTLCUpdate { ref source, payment_hash, htlc_value_satoshis, commitment_tx_output_idx } => {
2680
+ OnchainEvent :: HTLCUpdate { ref source, payment_hash, htlc_value_satoshis, commitment_tx_output_idx, .. } => {
2670
2681
// Check for duplicate HTLC resolutions.
2671
2682
#[ cfg( debug_assertions) ]
2672
2683
{
@@ -2907,6 +2918,15 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2907
2918
#[ cfg( not( fuzzing) ) ]
2908
2919
let offered_timeout_claim = witness_items == 5 && htlctype == Some ( HTLCType :: OfferedHTLC ) ;
2909
2920
2921
+ let claim_via_htlc_tx = accepted_preimage_claim || offered_timeout_claim;
2922
+ if claim_via_htlc_tx {
2923
+ // If the claim was via an HTLC-Timeout/HTLC-Success transaction, it must be a
2924
+ // 1-in-1-out transaction as its pre-signed.
2925
+ // Note that if anchors are available these both may vary.
2926
+ debug_assert_eq ! ( tx. input. len( ) , 1 ) ;
2927
+ debug_assert_eq ! ( tx. output. len( ) , 1 ) ;
2928
+ }
2929
+
2910
2930
let mut payment_preimage = PaymentPreimage ( [ 0 ; 32 ] ) ;
2911
2931
if accepted_preimage_claim {
2912
2932
payment_preimage. 0 . copy_from_slice ( input. witness . second_to_last ( ) . unwrap ( ) ) ;
@@ -2945,13 +2965,14 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2945
2965
}
2946
2966
2947
2967
macro_rules! check_htlc_valid_counterparty {
2948
- ( $counterparty_txid: expr, $htlc_output: expr) => {
2968
+ ( $counterparty_txid: expr, $htlc_output: expr, $htlc_value_sats : expr ) => {
2949
2969
if let Some ( txid) = $counterparty_txid {
2950
2970
for & ( ref pending_htlc, ref pending_source) in self . counterparty_claimable_outpoints. get( & txid) . unwrap( ) {
2951
2971
if pending_htlc. payment_hash == $htlc_output. payment_hash && pending_htlc. amount_msat == $htlc_output. amount_msat {
2952
2972
if let & Some ( ref source) = pending_source {
2953
2973
log_claim!( "revoked counterparty commitment tx" , false , pending_htlc, true ) ;
2954
- payment_data = Some ( ( ( * * source) . clone( ) , $htlc_output. payment_hash, $htlc_output. amount_msat) ) ;
2974
+ payment_data = Some ( ( ( * * source) . clone( ) , $htlc_output. payment_hash,
2975
+ pending_htlc. amount_msat, $htlc_value_sats) ) ;
2955
2976
break ;
2956
2977
}
2957
2978
}
@@ -2964,18 +2985,24 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2964
2985
( $htlcs: expr, $tx_info: expr, $holder_tx: expr) => {
2965
2986
for ( ref htlc_output, source_option) in $htlcs {
2966
2987
if Some ( input. previous_output. vout) == htlc_output. transaction_output_index {
2988
+ let htlc_value_sats = if claim_via_htlc_tx {
2989
+ tx. output. iter( ) . map( |txout| txout. value) . sum:: <u64 >( )
2990
+ } else { htlc_output. amount_msat / 1000 } ;
2967
2991
if let Some ( ref source) = source_option {
2968
2992
log_claim!( $tx_info, $holder_tx, htlc_output, true ) ;
2969
2993
// We have a resolution of an HTLC either from one of our latest
2970
2994
// holder commitment transactions or an unrevoked counterparty commitment
2971
2995
// transaction. This implies we either learned a preimage, the HTLC
2972
2996
// has timed out, or we screwed up. In any case, we should now
2973
2997
// resolve the source HTLC with the original sender.
2974
- payment_data = Some ( ( ( * source) . clone( ) , htlc_output. payment_hash, htlc_output. amount_msat) ) ;
2998
+ payment_data = Some ( ( ( * source) . clone( ) , htlc_output. payment_hash,
2999
+ htlc_output. amount_msat, htlc_value_sats) ) ;
2975
3000
} else if !$holder_tx {
2976
- check_htlc_valid_counterparty!( self . current_counterparty_commitment_txid, htlc_output) ;
3001
+ check_htlc_valid_counterparty!( self . current_counterparty_commitment_txid,
3002
+ htlc_output, htlc_value_sats) ;
2977
3003
if payment_data. is_none( ) {
2978
- check_htlc_valid_counterparty!( self . prev_counterparty_commitment_txid, htlc_output) ;
3004
+ check_htlc_valid_counterparty!( self . prev_counterparty_commitment_txid,
3005
+ htlc_output, htlc_value_sats) ;
2979
3006
}
2980
3007
}
2981
3008
if payment_data. is_none( ) {
@@ -2986,6 +3013,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2986
3013
txid: tx. txid( ) , height, transaction: Some ( tx. clone( ) ) ,
2987
3014
event: OnchainEvent :: HTLCSpendConfirmation {
2988
3015
commitment_tx_output_idx: input. previous_output. vout,
3016
+ onchain_value_satoshis: Some ( htlc_value_sats) ,
2989
3017
preimage: if accepted_preimage_claim || offered_preimage_claim {
2990
3018
Some ( payment_preimage) } else { None } ,
2991
3019
// If this is a payment to us (!outbound_htlc, above),
@@ -3028,7 +3056,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
3028
3056
3029
3057
// Check that scan_commitment, above, decided there is some source worth relaying an
3030
3058
// HTLC resolution backwards to and figure out whether we learned a preimage from it.
3031
- if let Some ( ( source, payment_hash, amount_msat) ) = payment_data {
3059
+ if let Some ( ( source, payment_hash, amount_msat, onchain_amount_sat ) ) = payment_data {
3032
3060
if accepted_preimage_claim {
3033
3061
if !self . pending_monitor_events . iter ( ) . any (
3034
3062
|update| if let & MonitorEvent :: HTLCEvent ( ref upd) = update { upd. source == source } else { false } ) {
@@ -3040,6 +3068,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
3040
3068
commitment_tx_output_idx : input. previous_output . vout ,
3041
3069
preimage : Some ( payment_preimage) ,
3042
3070
on_to_local_output_csv : None ,
3071
+ onchain_value_satoshis : Some ( onchain_amount_sat) ,
3043
3072
} ,
3044
3073
} ) ;
3045
3074
self . pending_monitor_events . push ( MonitorEvent :: HTLCEvent ( HTLCUpdate {
@@ -3062,6 +3091,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
3062
3091
commitment_tx_output_idx : input. previous_output . vout ,
3063
3092
preimage : Some ( payment_preimage) ,
3064
3093
on_to_local_output_csv : None ,
3094
+ onchain_value_satoshis : Some ( onchain_amount_sat) ,
3065
3095
} ,
3066
3096
} ) ;
3067
3097
self . pending_monitor_events . push ( MonitorEvent :: HTLCEvent ( HTLCUpdate {
@@ -3089,6 +3119,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
3089
3119
source, payment_hash,
3090
3120
htlc_value_satoshis : Some ( amount_msat / 1000 ) ,
3091
3121
commitment_tx_output_idx : Some ( input. previous_output . vout ) ,
3122
+ onchain_value_satoshis : Some ( onchain_amount_sat) ,
3092
3123
} ,
3093
3124
} ;
3094
3125
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