@@ -212,16 +212,6 @@ impl MsgHandleErrInternal {
212
212
}
213
213
}
214
214
215
- /// Pass to fail_htlc_backwwards to indicate the reason to fail the payment
216
- /// after a PaymentReceived event.
217
- #[ derive( PartialEq ) ]
218
- pub enum PaymentFailReason {
219
- /// Indicate the preimage for payment_hash is not known after a PaymentReceived event
220
- PreimageUnknown ,
221
- /// Indicate the payment amount is incorrect ( received is < expected or > 2*expected ) after a PaymentReceived event
222
- AmountMismatch ,
223
- }
224
-
225
215
/// We hold back HTLCs we intend to relay for a random interval in the range (this, 5*this). This
226
216
/// provides some limited amount of privacy. Ideally this would range from somewhere like 1 second
227
217
/// to 30 seconds, but people expect lightning to be, you know, kinda fast, sadly. We could
@@ -1532,16 +1522,21 @@ impl ChannelManager {
1532
1522
events. append ( & mut new_events) ;
1533
1523
}
1534
1524
1535
- /// Indicates that the preimage for payment_hash is unknown or the received amount is incorrect after a PaymentReceived event.
1536
- pub fn fail_htlc_backwards ( & self , payment_hash : & PaymentHash , reason : PaymentFailReason ) -> bool {
1525
+ /// Indicates that the preimage for payment_hash is unknown or the received amount is incorrect
1526
+ /// after a PaymentReceived event.
1527
+ /// expected_value is the value you expected the payment to be for (not the amount it actually
1528
+ /// was for from the PaymentReceived event).
1529
+ pub fn fail_htlc_backwards ( & self , payment_hash : & PaymentHash , expected_value : u64 ) -> bool {
1537
1530
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
1538
1531
1539
1532
let mut channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ;
1540
1533
let removed_source = channel_state. as_mut ( ) . unwrap ( ) . claimable_htlcs . remove ( payment_hash) ;
1541
1534
if let Some ( mut sources) = removed_source {
1542
1535
for htlc_with_hash in sources. drain ( ..) {
1543
1536
if channel_state. is_none ( ) { channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ; }
1544
- self . fail_htlc_backwards_internal ( channel_state. take ( ) . unwrap ( ) , HTLCSource :: PreviousHopData ( htlc_with_hash) , payment_hash, HTLCFailReason :: Reason { failure_code : if reason == PaymentFailReason :: PreimageUnknown { 0x4000 | 15 } else { 0x4000 | 16 } , data : Vec :: new ( ) } ) ;
1537
+ self . fail_htlc_backwards_internal ( channel_state. take ( ) . unwrap ( ) ,
1538
+ HTLCSource :: PreviousHopData ( htlc_with_hash) , payment_hash,
1539
+ HTLCFailReason :: Reason { failure_code : 0x4000 | 15 , data : byte_utils:: be64_to_array ( expected_value) . to_vec ( ) } ) ;
1545
1540
}
1546
1541
true
1547
1542
} else { false }
@@ -3366,7 +3361,7 @@ mod tests {
3366
3361
use chain:: keysinterface:: { KeysInterface , SpendableOutputDescriptor } ;
3367
3362
use chain:: keysinterface;
3368
3363
use ln:: channel:: { COMMITMENT_TX_BASE_WEIGHT , COMMITMENT_TX_WEIGHT_PER_HTLC } ;
3369
- use ln:: channelmanager:: { ChannelManager , ChannelManagerReadArgs , OnionKeys , PaymentFailReason , RAACommitmentOrder , PaymentPreimage , PaymentHash } ;
3364
+ use ln:: channelmanager:: { ChannelManager , ChannelManagerReadArgs , OnionKeys , RAACommitmentOrder , PaymentPreimage , PaymentHash } ;
3370
3365
use ln:: channelmonitor:: { ChannelMonitor , ChannelMonitorUpdateErr , CLTV_CLAIM_BUFFER , HTLC_FAIL_TIMEOUT_BLOCKS , ManyChannelMonitor } ;
3371
3366
use ln:: channel:: { ACCEPTED_HTLC_SCRIPT_WEIGHT , OFFERED_HTLC_SCRIPT_WEIGHT } ;
3372
3367
use ln:: router:: { Route , RouteHop , Router } ;
@@ -4224,7 +4219,7 @@ mod tests {
4224
4219
}
4225
4220
4226
4221
fn fail_payment_along_route ( origin_node : & Node , expected_route : & [ & Node ] , skip_last : bool , our_payment_hash : PaymentHash ) {
4227
- assert ! ( expected_route. last( ) . unwrap( ) . node. fail_htlc_backwards( & our_payment_hash, PaymentFailReason :: PreimageUnknown ) ) ;
4222
+ assert ! ( expected_route. last( ) . unwrap( ) . node. fail_htlc_backwards( & our_payment_hash, 0 ) ) ;
4228
4223
check_added_monitors ! ( expected_route. last( ) . unwrap( ) , 1 ) ;
4229
4224
4230
4225
let mut next_msgs: Option < ( msgs:: UpdateFailHTLC , msgs:: CommitmentSigned ) > = None ;
@@ -6351,7 +6346,7 @@ mod tests {
6351
6346
// Brodacast legit commitment tx from C on B's chain
6352
6347
let commitment_tx = nodes[ 2 ] . node . channel_state . lock ( ) . unwrap ( ) . by_id . get ( & chan_2. 2 ) . unwrap ( ) . last_local_commitment_txn . clone ( ) ;
6353
6348
check_spends ! ( commitment_tx[ 0 ] , chan_2. 3 . clone( ) ) ;
6354
- nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash, PaymentFailReason :: PreimageUnknown ) ;
6349
+ nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash, 0 ) ;
6355
6350
{
6356
6351
let mut added_monitors = nodes[ 2 ] . chan_monitor . added_monitors . lock ( ) . unwrap ( ) ;
6357
6352
assert_eq ! ( added_monitors. len( ) , 1 ) ;
@@ -6536,7 +6531,7 @@ mod tests {
6536
6531
let ( _, second_payment_hash) = route_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] , & nodes[ 2 ] ] , 3000000 ) ;
6537
6532
let ( _, third_payment_hash) = route_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] , & nodes[ 2 ] ] , 3000000 ) ;
6538
6533
6539
- assert ! ( nodes[ 2 ] . node. fail_htlc_backwards( & first_payment_hash, PaymentFailReason :: PreimageUnknown ) ) ;
6534
+ assert ! ( nodes[ 2 ] . node. fail_htlc_backwards( & first_payment_hash, 0 ) ) ;
6540
6535
check_added_monitors ! ( nodes[ 2 ] , 1 ) ;
6541
6536
let updates = get_htlc_update_msgs ! ( nodes[ 2 ] , nodes[ 1 ] . node. get_our_node_id( ) ) ;
6542
6537
assert ! ( updates. update_add_htlcs. is_empty( ) ) ;
@@ -6548,7 +6543,7 @@ mod tests {
6548
6543
let bs_raa = commitment_signed_dance ! ( nodes[ 1 ] , nodes[ 2 ] , updates. commitment_signed, false , true , false , true ) ;
6549
6544
// Drop the last RAA from 3 -> 2
6550
6545
6551
- assert ! ( nodes[ 2 ] . node. fail_htlc_backwards( & second_payment_hash, PaymentFailReason :: PreimageUnknown ) ) ;
6546
+ assert ! ( nodes[ 2 ] . node. fail_htlc_backwards( & second_payment_hash, 0 ) ) ;
6552
6547
check_added_monitors ! ( nodes[ 2 ] , 1 ) ;
6553
6548
let updates = get_htlc_update_msgs ! ( nodes[ 2 ] , nodes[ 1 ] . node. get_our_node_id( ) ) ;
6554
6549
assert ! ( updates. update_add_htlcs. is_empty( ) ) ;
@@ -6564,7 +6559,7 @@ mod tests {
6564
6559
nodes[ 2 ] . node . handle_revoke_and_ack ( & nodes[ 1 ] . node . get_our_node_id ( ) , & as_raa) . unwrap ( ) ;
6565
6560
check_added_monitors ! ( nodes[ 2 ] , 1 ) ;
6566
6561
6567
- assert ! ( nodes[ 2 ] . node. fail_htlc_backwards( & third_payment_hash, PaymentFailReason :: PreimageUnknown ) ) ;
6562
+ assert ! ( nodes[ 2 ] . node. fail_htlc_backwards( & third_payment_hash, 0 ) ) ;
6568
6563
check_added_monitors ! ( nodes[ 2 ] , 1 ) ;
6569
6564
let updates = get_htlc_update_msgs ! ( nodes[ 2 ] , nodes[ 1 ] . node. get_our_node_id( ) ) ;
6570
6565
assert ! ( updates. update_add_htlcs. is_empty( ) ) ;
@@ -8159,7 +8154,7 @@ mod tests {
8159
8154
let ( _, payment_hash_1) = route_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] , & nodes[ 2 ] ] , 1000000 ) ;
8160
8155
8161
8156
// Fail the payment backwards, failing the monitor update on nodes[1]'s receipt of the RAA
8162
- assert ! ( nodes[ 2 ] . node. fail_htlc_backwards( & payment_hash_1, PaymentFailReason :: PreimageUnknown ) ) ;
8157
+ assert ! ( nodes[ 2 ] . node. fail_htlc_backwards( & payment_hash_1, 0 ) ) ;
8163
8158
check_added_monitors ! ( nodes[ 2 ] , 1 ) ;
8164
8159
8165
8160
let updates = get_htlc_update_msgs ! ( nodes[ 2 ] , nodes[ 1 ] . node. get_our_node_id( ) ) ;
@@ -9724,7 +9719,7 @@ mod tests {
9724
9719
let onion_keys = ChannelManager :: construct_onion_keys ( & Secp256k1 :: new ( ) , & route, & session_priv) . unwrap ( ) ;
9725
9720
msg. reason = ChannelManager :: build_first_hop_failure_packet ( & onion_keys[ 1 ] . shared_secret [ ..] , NODE |2 , & [ 0 ; 0 ] ) ;
9726
9721
} , ||{
9727
- nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash, PaymentFailReason :: PreimageUnknown ) ;
9722
+ nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash, 0 ) ;
9728
9723
} , true , Some ( NODE |2 ) , Some ( msgs:: HTLCFailChannelUpdate :: NodeFailure { node_id : route. hops [ 1 ] . pubkey , is_permanent : false } ) ) ;
9729
9724
9730
9725
// intermediate node failure
@@ -9742,7 +9737,7 @@ mod tests {
9742
9737
let onion_keys = ChannelManager :: construct_onion_keys ( & Secp256k1 :: new ( ) , & route, & session_priv) . unwrap ( ) ;
9743
9738
msg. reason = ChannelManager :: build_first_hop_failure_packet ( & onion_keys[ 1 ] . shared_secret [ ..] , PERM |NODE |2 , & [ 0 ; 0 ] ) ;
9744
9739
} , ||{
9745
- nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash, PaymentFailReason :: PreimageUnknown ) ;
9740
+ nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash, 0 ) ;
9746
9741
} , false , Some ( PERM |NODE |2 ) , Some ( msgs:: HTLCFailChannelUpdate :: NodeFailure { node_id : route. hops [ 1 ] . pubkey , is_permanent : true } ) ) ;
9747
9742
9748
9743
// intermediate node failure
@@ -9753,7 +9748,7 @@ mod tests {
9753
9748
let onion_keys = ChannelManager :: construct_onion_keys ( & Secp256k1 :: new ( ) , & route, & session_priv) . unwrap ( ) ;
9754
9749
msg. reason = ChannelManager :: build_first_hop_failure_packet ( & onion_keys[ 0 ] . shared_secret [ ..] , PERM |NODE |3 , & [ 0 ; 0 ] ) ;
9755
9750
} , ||{
9756
- nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash, PaymentFailReason :: PreimageUnknown ) ;
9751
+ nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash, 0 ) ;
9757
9752
} , true , Some ( PERM |NODE |3 ) , Some ( msgs:: HTLCFailChannelUpdate :: NodeFailure { node_id : route. hops [ 0 ] . pubkey , is_permanent : true } ) ) ;
9758
9753
9759
9754
// final node failure
@@ -9762,7 +9757,7 @@ mod tests {
9762
9757
let onion_keys = ChannelManager :: construct_onion_keys ( & Secp256k1 :: new ( ) , & route, & session_priv) . unwrap ( ) ;
9763
9758
msg. reason = ChannelManager :: build_first_hop_failure_packet ( & onion_keys[ 1 ] . shared_secret [ ..] , PERM |NODE |3 , & [ 0 ; 0 ] ) ;
9764
9759
} , ||{
9765
- nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash, PaymentFailReason :: PreimageUnknown ) ;
9760
+ nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash, 0 ) ;
9766
9761
} , false , Some ( PERM |NODE |3 ) , Some ( msgs:: HTLCFailChannelUpdate :: NodeFailure { node_id : route. hops [ 1 ] . pubkey , is_permanent : true } ) ) ;
9767
9762
9768
9763
run_onion_failure_test ( "invalid_onion_version" , 0 , & nodes, & route, & payment_hash, |msg| { msg. onion_routing_packet . version = 1 ; } , ||{ } , true ,
@@ -9829,13 +9824,9 @@ mod tests {
9829
9824
} , ||{ } , true , Some ( UPDATE |14 ) , Some ( msgs:: HTLCFailChannelUpdate :: ChannelUpdateMessage { msg : ChannelUpdate :: dummy ( ) } ) ) ;
9830
9825
9831
9826
run_onion_failure_test ( "unknown_payment_hash" , 2 , & nodes, & route, & payment_hash, |_| { } , || {
9832
- nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash, PaymentFailReason :: PreimageUnknown ) ;
9827
+ nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash, 0 ) ;
9833
9828
} , false , Some ( PERM |15 ) , None ) ;
9834
9829
9835
- run_onion_failure_test ( "incorrect_payment_amount" , 2 , & nodes, & route, & payment_hash, |_| { } , || {
9836
- nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash, PaymentFailReason :: AmountMismatch ) ;
9837
- } , false , Some ( PERM |16 ) , None ) ;
9838
-
9839
9830
run_onion_failure_test ( "final_expiry_too_soon" , 1 , & nodes, & route, & payment_hash, |msg| {
9840
9831
let height = msg. cltv_expiry - CLTV_CLAIM_BUFFER - HTLC_FAIL_TIMEOUT_BLOCKS + 1 ;
9841
9832
let header = BlockHeader { version : 0x20000000 , prev_blockhash : Default :: default ( ) , merkle_root : Default :: default ( ) , time : 42 , bits : 42 , nonce : 42 } ;
0 commit comments