@@ -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
@@ -1530,16 +1520,21 @@ impl ChannelManager {
1530
1520
events. append ( & mut new_events) ;
1531
1521
}
1532
1522
1533
- /// Indicates that the preimage for payment_hash is unknown or the received amount is incorrect after a PaymentReceived event.
1534
- pub fn fail_htlc_backwards ( & self , payment_hash : & PaymentHash , reason : PaymentFailReason ) -> bool {
1523
+ /// Indicates that the preimage for payment_hash is unknown or the received amount is incorrect
1524
+ /// after a PaymentReceived event.
1525
+ /// expected_value is the value you expected the payment to be for (not the amount it actually
1526
+ /// was for from the PaymentReceived event).
1527
+ pub fn fail_htlc_backwards ( & self , payment_hash : & PaymentHash , expected_value : u64 ) -> bool {
1535
1528
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
1536
1529
1537
1530
let mut channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ;
1538
1531
let removed_source = channel_state. as_mut ( ) . unwrap ( ) . claimable_htlcs . remove ( payment_hash) ;
1539
1532
if let Some ( mut sources) = removed_source {
1540
1533
for htlc_with_hash in sources. drain ( ..) {
1541
1534
if channel_state. is_none ( ) { channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ; }
1542
- 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 ( ) } ) ;
1535
+ self . fail_htlc_backwards_internal ( channel_state. take ( ) . unwrap ( ) ,
1536
+ HTLCSource :: PreviousHopData ( htlc_with_hash) , payment_hash,
1537
+ HTLCFailReason :: Reason { failure_code : 0x4000 | 15 , data : byte_utils:: be64_to_array ( expected_value) . to_vec ( ) } ) ;
1543
1538
}
1544
1539
true
1545
1540
} else { false }
@@ -3363,7 +3358,7 @@ mod tests {
3363
3358
use chain:: keysinterface:: { KeysInterface , SpendableOutputDescriptor } ;
3364
3359
use chain:: keysinterface;
3365
3360
use ln:: channel:: { COMMITMENT_TX_BASE_WEIGHT , COMMITMENT_TX_WEIGHT_PER_HTLC } ;
3366
- use ln:: channelmanager:: { ChannelManager , ChannelManagerReadArgs , OnionKeys , PaymentFailReason , RAACommitmentOrder , PaymentPreimage , PaymentHash } ;
3361
+ use ln:: channelmanager:: { ChannelManager , ChannelManagerReadArgs , OnionKeys , RAACommitmentOrder , PaymentPreimage , PaymentHash } ;
3367
3362
use ln:: channelmonitor:: { ChannelMonitor , ChannelMonitorUpdateErr , CLTV_CLAIM_BUFFER , HTLC_FAIL_TIMEOUT_BLOCKS , ManyChannelMonitor } ;
3368
3363
use ln:: channel:: { ACCEPTED_HTLC_SCRIPT_WEIGHT , OFFERED_HTLC_SCRIPT_WEIGHT } ;
3369
3364
use ln:: router:: { Route , RouteHop , Router } ;
@@ -4221,7 +4216,7 @@ mod tests {
4221
4216
}
4222
4217
4223
4218
fn fail_payment_along_route ( origin_node : & Node , expected_route : & [ & Node ] , skip_last : bool , our_payment_hash : PaymentHash ) {
4224
- assert ! ( expected_route. last( ) . unwrap( ) . node. fail_htlc_backwards( & our_payment_hash, PaymentFailReason :: PreimageUnknown ) ) ;
4219
+ assert ! ( expected_route. last( ) . unwrap( ) . node. fail_htlc_backwards( & our_payment_hash, 0 ) ) ;
4225
4220
check_added_monitors ! ( expected_route. last( ) . unwrap( ) , 1 ) ;
4226
4221
4227
4222
let mut next_msgs: Option < ( msgs:: UpdateFailHTLC , msgs:: CommitmentSigned ) > = None ;
@@ -6348,7 +6343,7 @@ mod tests {
6348
6343
// Brodacast legit commitment tx from C on B's chain
6349
6344
let commitment_tx = nodes[ 2 ] . node . channel_state . lock ( ) . unwrap ( ) . by_id . get ( & chan_2. 2 ) . unwrap ( ) . last_local_commitment_txn . clone ( ) ;
6350
6345
check_spends ! ( commitment_tx[ 0 ] , chan_2. 3 . clone( ) ) ;
6351
- nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash, PaymentFailReason :: PreimageUnknown ) ;
6346
+ nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash, 0 ) ;
6352
6347
{
6353
6348
let mut added_monitors = nodes[ 2 ] . chan_monitor . added_monitors . lock ( ) . unwrap ( ) ;
6354
6349
assert_eq ! ( added_monitors. len( ) , 1 ) ;
@@ -6533,7 +6528,7 @@ mod tests {
6533
6528
let ( _, second_payment_hash) = route_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] , & nodes[ 2 ] ] , 3000000 ) ;
6534
6529
let ( _, third_payment_hash) = route_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] , & nodes[ 2 ] ] , 3000000 ) ;
6535
6530
6536
- assert ! ( nodes[ 2 ] . node. fail_htlc_backwards( & first_payment_hash, PaymentFailReason :: PreimageUnknown ) ) ;
6531
+ assert ! ( nodes[ 2 ] . node. fail_htlc_backwards( & first_payment_hash, 0 ) ) ;
6537
6532
check_added_monitors ! ( nodes[ 2 ] , 1 ) ;
6538
6533
let updates = get_htlc_update_msgs ! ( nodes[ 2 ] , nodes[ 1 ] . node. get_our_node_id( ) ) ;
6539
6534
assert ! ( updates. update_add_htlcs. is_empty( ) ) ;
@@ -6545,7 +6540,7 @@ mod tests {
6545
6540
let bs_raa = commitment_signed_dance ! ( nodes[ 1 ] , nodes[ 2 ] , updates. commitment_signed, false , true , false , true ) ;
6546
6541
// Drop the last RAA from 3 -> 2
6547
6542
6548
- assert ! ( nodes[ 2 ] . node. fail_htlc_backwards( & second_payment_hash, PaymentFailReason :: PreimageUnknown ) ) ;
6543
+ assert ! ( nodes[ 2 ] . node. fail_htlc_backwards( & second_payment_hash, 0 ) ) ;
6549
6544
check_added_monitors ! ( nodes[ 2 ] , 1 ) ;
6550
6545
let updates = get_htlc_update_msgs ! ( nodes[ 2 ] , nodes[ 1 ] . node. get_our_node_id( ) ) ;
6551
6546
assert ! ( updates. update_add_htlcs. is_empty( ) ) ;
@@ -6561,7 +6556,7 @@ mod tests {
6561
6556
nodes[ 2 ] . node . handle_revoke_and_ack ( & nodes[ 1 ] . node . get_our_node_id ( ) , & as_raa) . unwrap ( ) ;
6562
6557
check_added_monitors ! ( nodes[ 2 ] , 1 ) ;
6563
6558
6564
- assert ! ( nodes[ 2 ] . node. fail_htlc_backwards( & third_payment_hash, PaymentFailReason :: PreimageUnknown ) ) ;
6559
+ assert ! ( nodes[ 2 ] . node. fail_htlc_backwards( & third_payment_hash, 0 ) ) ;
6565
6560
check_added_monitors ! ( nodes[ 2 ] , 1 ) ;
6566
6561
let updates = get_htlc_update_msgs ! ( nodes[ 2 ] , nodes[ 1 ] . node. get_our_node_id( ) ) ;
6567
6562
assert ! ( updates. update_add_htlcs. is_empty( ) ) ;
@@ -8156,7 +8151,7 @@ mod tests {
8156
8151
let ( _, payment_hash_1) = route_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] , & nodes[ 2 ] ] , 1000000 ) ;
8157
8152
8158
8153
// Fail the payment backwards, failing the monitor update on nodes[1]'s receipt of the RAA
8159
- assert ! ( nodes[ 2 ] . node. fail_htlc_backwards( & payment_hash_1, PaymentFailReason :: PreimageUnknown ) ) ;
8154
+ assert ! ( nodes[ 2 ] . node. fail_htlc_backwards( & payment_hash_1, 0 ) ) ;
8160
8155
check_added_monitors ! ( nodes[ 2 ] , 1 ) ;
8161
8156
8162
8157
let updates = get_htlc_update_msgs ! ( nodes[ 2 ] , nodes[ 1 ] . node. get_our_node_id( ) ) ;
@@ -9721,7 +9716,7 @@ mod tests {
9721
9716
let onion_keys = ChannelManager :: construct_onion_keys ( & Secp256k1 :: new ( ) , & route, & session_priv) . unwrap ( ) ;
9722
9717
msg. reason = ChannelManager :: build_first_hop_failure_packet ( & onion_keys[ 1 ] . shared_secret [ ..] , NODE |2 , & [ 0 ; 0 ] ) ;
9723
9718
} , ||{
9724
- nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash, PaymentFailReason :: PreimageUnknown ) ;
9719
+ nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash, 0 ) ;
9725
9720
} , true , Some ( NODE |2 ) , Some ( msgs:: HTLCFailChannelUpdate :: NodeFailure { node_id : route. hops [ 1 ] . pubkey , is_permanent : false } ) ) ;
9726
9721
9727
9722
// intermediate node failure
@@ -9739,7 +9734,7 @@ mod tests {
9739
9734
let onion_keys = ChannelManager :: construct_onion_keys ( & Secp256k1 :: new ( ) , & route, & session_priv) . unwrap ( ) ;
9740
9735
msg. reason = ChannelManager :: build_first_hop_failure_packet ( & onion_keys[ 1 ] . shared_secret [ ..] , PERM |NODE |2 , & [ 0 ; 0 ] ) ;
9741
9736
} , ||{
9742
- nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash, PaymentFailReason :: PreimageUnknown ) ;
9737
+ nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash, 0 ) ;
9743
9738
} , false , Some ( PERM |NODE |2 ) , Some ( msgs:: HTLCFailChannelUpdate :: NodeFailure { node_id : route. hops [ 1 ] . pubkey , is_permanent : true } ) ) ;
9744
9739
9745
9740
// intermediate node failure
@@ -9750,7 +9745,7 @@ mod tests {
9750
9745
let onion_keys = ChannelManager :: construct_onion_keys ( & Secp256k1 :: new ( ) , & route, & session_priv) . unwrap ( ) ;
9751
9746
msg. reason = ChannelManager :: build_first_hop_failure_packet ( & onion_keys[ 0 ] . shared_secret [ ..] , PERM |NODE |3 , & [ 0 ; 0 ] ) ;
9752
9747
} , ||{
9753
- nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash, PaymentFailReason :: PreimageUnknown ) ;
9748
+ nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash, 0 ) ;
9754
9749
} , true , Some ( PERM |NODE |3 ) , Some ( msgs:: HTLCFailChannelUpdate :: NodeFailure { node_id : route. hops [ 0 ] . pubkey , is_permanent : true } ) ) ;
9755
9750
9756
9751
// final node failure
@@ -9759,7 +9754,7 @@ mod tests {
9759
9754
let onion_keys = ChannelManager :: construct_onion_keys ( & Secp256k1 :: new ( ) , & route, & session_priv) . unwrap ( ) ;
9760
9755
msg. reason = ChannelManager :: build_first_hop_failure_packet ( & onion_keys[ 1 ] . shared_secret [ ..] , PERM |NODE |3 , & [ 0 ; 0 ] ) ;
9761
9756
} , ||{
9762
- nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash, PaymentFailReason :: PreimageUnknown ) ;
9757
+ nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash, 0 ) ;
9763
9758
} , false , Some ( PERM |NODE |3 ) , Some ( msgs:: HTLCFailChannelUpdate :: NodeFailure { node_id : route. hops [ 1 ] . pubkey , is_permanent : true } ) ) ;
9764
9759
9765
9760
run_onion_failure_test ( "invalid_onion_version" , 0 , & nodes, & route, & payment_hash, |msg| { msg. onion_routing_packet . version = 1 ; } , ||{ } , true ,
@@ -9826,13 +9821,9 @@ mod tests {
9826
9821
} , ||{ } , true , Some ( UPDATE |14 ) , Some ( msgs:: HTLCFailChannelUpdate :: ChannelUpdateMessage { msg : ChannelUpdate :: dummy ( ) } ) ) ;
9827
9822
9828
9823
run_onion_failure_test ( "unknown_payment_hash" , 2 , & nodes, & route, & payment_hash, |_| { } , || {
9829
- nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash, PaymentFailReason :: PreimageUnknown ) ;
9824
+ nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash, 0 ) ;
9830
9825
} , false , Some ( PERM |15 ) , None ) ;
9831
9826
9832
- run_onion_failure_test ( "incorrect_payment_amount" , 2 , & nodes, & route, & payment_hash, |_| { } , || {
9833
- nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash, PaymentFailReason :: AmountMismatch ) ;
9834
- } , false , Some ( PERM |16 ) , None ) ;
9835
-
9836
9827
run_onion_failure_test ( "final_expiry_too_soon" , 1 , & nodes, & route, & payment_hash, |msg| {
9837
9828
let height = msg. cltv_expiry - CLTV_CLAIM_BUFFER - HTLC_FAIL_TIMEOUT_BLOCKS + 1 ;
9838
9829
let header = BlockHeader { version : 0x20000000 , prev_blockhash : Default :: default ( ) , merkle_root : Default :: default ( ) , time : 42 , bits : 42 , nonce : 42 } ;
0 commit comments