@@ -6604,15 +6604,16 @@ fn test_onion_failure() {
6604
6604
}
6605
6605
6606
6606
#[ test]
6607
- fn test_update_add_htlc_bolt2 ( ) {
6607
+ fn test_update_add_htlc_bolt2_sender ( ) {
6608
6608
use util:: rng;
6609
6609
use std:: sync:: atomic:: Ordering ;
6610
6610
use super :: channelmanager:: HTLCSource ;
6611
6611
use super :: channel:: ChannelError ;
6612
6612
6613
6613
let secp_ctx = Secp256k1 :: new ( ) ;
6614
6614
6615
- // BOLT 2 Requirements for Sender
6615
+ // BOLT 2 Requirements for the Sender when constructing and sending an update_add_htlc message.
6616
+
6616
6617
// BOLT 2 Requirement: MUST NOT offer amount_msat it cannot pay for in the remote commitment transaction at the current feerate_per_kw (see "Updating Fees") while maintaining its channel reserve.
6617
6618
//TODO: I don't believe this is explicitly enforced when sending an HTLC but as the Fee aspect of the BOLT specs is in flux leaving this as a TODO.
6618
6619
@@ -6637,11 +6638,11 @@ fn test_update_add_htlc_bolt2() {
6637
6638
let err = nodes[ 0 ] . node . channel_state . lock ( ) . unwrap ( ) . by_id . get_mut ( & chan. 2 ) . unwrap ( ) . send_htlc ( 0 , our_payment_hash, TEST_FINAL_CLTV , HTLCSource :: OutboundRoute {
6638
6639
route : route. clone ( ) ,
6639
6640
session_priv : session_priv. clone ( ) ,
6640
- first_hop_htlc_msat : 100000 ,
6641
+ first_hop_htlc_msat : 0 ,
6641
6642
} , onion_packet) ;
6642
6643
6643
- if let Err ( ChannelError :: Ignore ( _ ) ) = err {
6644
- assert ! ( true ) ;
6644
+ if let Err ( ChannelError :: Ignore ( msg ) ) = err {
6645
+ assert_eq ! ( msg , "Cannot send less than their minimum HTLC value" ) ;
6645
6646
} else {
6646
6647
assert ! ( false ) ;
6647
6648
}
@@ -6683,17 +6684,13 @@ fn test_update_add_htlc_bolt2() {
6683
6684
first_hop_htlc_msat : 0 ,
6684
6685
} , onion_packet) ;
6685
6686
6686
- if let Err ( ChannelError :: Ignore ( _ ) ) = err {
6687
- assert ! ( true ) ;
6687
+ if let Err ( ChannelError :: Ignore ( msg ) ) = err {
6688
+ assert_eq ! ( msg , "Cannot push more than their max accepted HTLCs" ) ;
6688
6689
} else {
6689
6690
assert ! ( false ) ;
6690
6691
}
6691
- //Clear any unhandled msg events.
6692
- let _ = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
6693
- let _ = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
6694
6692
6695
6693
// BOLT 2 Requirement: if the sum of total offered HTLCs would exceed the remote's max_htlc_value_in_flight_msat: MUST NOT add an HTLC.
6696
- let mut nodes = create_network ( 2 ) ;
6697
6694
let chan = create_announced_chan_between_nodes_with_value ( & nodes, 0 , 1 , 100000 , 0 ) ;
6698
6695
let route = nodes[ 0 ] . router . get_route ( & nodes[ 1 ] . node . get_our_node_id ( ) , None , & [ ] , 100000 , TEST_FINAL_CLTV ) . unwrap ( ) ;
6699
6696
let ( _, our_payment_hash) = get_payment_preimage_hash ! ( nodes[ 0 ] ) ;
@@ -6715,15 +6712,14 @@ fn test_update_add_htlc_bolt2() {
6715
6712
first_hop_htlc_msat : 0 ,
6716
6713
} , onion_packet) ;
6717
6714
6718
- if let Err ( ChannelError :: Ignore ( _ ) ) = err {
6719
- assert ! ( true ) ;
6715
+ if let Err ( ChannelError :: Ignore ( msg ) ) = err {
6716
+ assert_eq ! ( msg , "Cannot send value that would put us over our max HTLC value in flight" ) ;
6720
6717
} else {
6721
6718
assert ! ( false ) ;
6722
6719
}
6723
6720
6724
6721
// BOLT 2 Requirement: if the sum of total offered HTLCs would exceed the remote's max_htlc_value_in_flight_msat: MUST NOT add an HTLC.
6725
6722
// BOLT 2 Requirement: MUST increase the value of id by 1 for each successive offer.
6726
- let mut nodes = create_network ( 2 ) ;
6727
6723
let chan = create_announced_chan_between_nodes_with_value ( & nodes, 0 , 1 , 100000 , 0 ) ;
6728
6724
let route = nodes[ 0 ] . router . get_route ( & nodes[ 1 ] . node . get_our_node_id ( ) , None , & [ ] , 100000 , TEST_FINAL_CLTV ) . unwrap ( ) ;
6729
6725
let ( _, our_payment_hash) = get_payment_preimage_hash ! ( nodes[ 0 ] ) ;
@@ -6752,8 +6748,11 @@ fn test_update_add_htlc_bolt2() {
6752
6748
assert ! ( false ) ;
6753
6749
}
6754
6750
}
6751
+ }
6755
6752
6756
- // BOLT 2 Requirements for Receiver
6753
+ #[ test]
6754
+ fn test_update_add_htlc_bolt2_receiver_check_amount_received_more_than_min ( ) {
6755
+ use super :: msgs:: HandleError ;
6757
6756
6758
6757
//BOLT2 Requirement: receiving an amount_msat equal to 0, OR less than its own htlc_minimum_msat -> SHOULD fail the channel.
6759
6758
let mut nodes = create_network ( 2 ) ;
@@ -6772,13 +6771,24 @@ fn test_update_add_htlc_bolt2() {
6772
6771
6773
6772
updates. update_add_htlcs [ 0 ] . amount_msat = htlc_minimum_msat-1 ;
6774
6773
let err = nodes[ 1 ] . node . handle_update_add_htlc ( & nodes[ 0 ] . node . get_our_node_id ( ) , & updates. update_add_htlcs [ 0 ] ) ;
6775
- assert ! ( err. is_err( ) ) ;
6774
+
6775
+ if let Err ( HandleError { err, action : _} ) = err {
6776
+ assert_eq ! ( err, "Remote side tried to send less than our minimum HTLC value" ) ;
6777
+ } else {
6778
+ assert ! ( false ) ;
6779
+ }
6780
+
6776
6781
//Confirm the channel was closed
6777
6782
{
6778
6783
assert_eq ! ( nodes[ 1 ] . node. channel_state. lock( ) . unwrap( ) . by_id. len( ) , 0 ) ;
6779
6784
}
6780
6785
//Clear unhandled msg events.
6781
6786
let _ = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
6787
+ }
6788
+
6789
+ #[ test]
6790
+ fn test_update_add_htlc_bolt2_receiver_sender_can_afford_amount_sent ( ) {
6791
+ use super :: msgs:: HandleError ;
6782
6792
6783
6793
//BOLT2 Requirement: receiving an amount_msat that the sending node cannot afford at the current feerate_per_kw (while maintaining its channel reserve): SHOULD fail the channel
6784
6794
let mut nodes = create_network ( 2 ) ;
@@ -6791,13 +6801,26 @@ fn test_update_add_htlc_bolt2() {
6791
6801
6792
6802
updates. update_add_htlcs [ 0 ] . amount_msat = 4000001 ;
6793
6803
let err = nodes[ 1 ] . node . handle_update_add_htlc ( & nodes[ 0 ] . node . get_our_node_id ( ) , & updates. update_add_htlcs [ 0 ] ) ;
6794
- assert ! ( err. is_err( ) ) ;
6804
+
6805
+ if let Err ( HandleError { err, action : _} ) = err {
6806
+ assert_eq ! ( err, "Remote HTLC add would put them over their reserve value" ) ;
6807
+ } else {
6808
+ assert ! ( false ) ;
6809
+ }
6810
+
6795
6811
//Confirm the channel was closed
6796
6812
{
6797
6813
assert_eq ! ( nodes[ 1 ] . node. channel_state. lock( ) . unwrap( ) . by_id. len( ) , 0 ) ;
6798
6814
}
6799
6815
//Clear unhandled msg events.
6800
6816
let _ = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
6817
+ }
6818
+
6819
+ #[ test]
6820
+ fn test_update_add_htlc_bolt2_receiver_check_max_htlc_limit ( ) {
6821
+ use util:: rng;
6822
+ let secp_ctx = Secp256k1 :: new ( ) ;
6823
+ use super :: msgs:: HandleError ;
6801
6824
6802
6825
//BOLT 2 Requirement: if a sending node adds more than its max_accepted_htlcs HTLCs to its local commitment transaction: SHOULD fail the channel
6803
6826
//BOLT 2 Requirement: MUST allow multiple HTLCs with the same payment_hash.
@@ -6832,13 +6855,24 @@ fn test_update_add_htlc_bolt2() {
6832
6855
}
6833
6856
msg. htlc_id = ( super :: channel:: OUR_MAX_HTLCS + 1 ) as u64 ;
6834
6857
let err = nodes[ 1 ] . node . handle_update_add_htlc ( & nodes[ 0 ] . node . get_our_node_id ( ) , & msg) ;
6835
- assert ! ( err. is_err( ) ) ;
6858
+
6859
+ if let Err ( HandleError { err, action : _} ) = err {
6860
+ assert_eq ! ( err, "Remote tried to push more than our max accepted HTLCs" ) ;
6861
+ } else {
6862
+ assert ! ( false ) ;
6863
+ }
6864
+
6836
6865
//Confirm the channel was closed
6837
6866
{
6838
6867
assert_eq ! ( nodes[ 1 ] . node. channel_state. lock( ) . unwrap( ) . by_id. len( ) , 0 ) ;
6839
6868
}
6840
6869
//Clear unhandled msg events.
6841
6870
let _ = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
6871
+ }
6872
+
6873
+ #[ test]
6874
+ fn test_update_add_htlc_bolt2_receiver_check_max_in_flight_msat ( ) {
6875
+ use super :: msgs:: HandleError ;
6842
6876
6843
6877
//OR adds more than its max_htlc_value_in_flight_msat worth of offered HTLCs to its local commitment transaction: SHOULD fail the channel
6844
6878
let mut nodes = create_network ( 2 ) ;
@@ -6850,13 +6884,24 @@ fn test_update_add_htlc_bolt2() {
6850
6884
let mut updates = get_htlc_update_msgs ! ( nodes[ 0 ] , nodes[ 1 ] . node. get_our_node_id( ) ) ;
6851
6885
updates. update_add_htlcs [ 0 ] . amount_msat = nodes[ 1 ] . node . channel_state . lock ( ) . unwrap ( ) . by_id . get ( & chan. 2 ) . unwrap ( ) . their_max_htlc_value_in_flight_msat + 1 ;
6852
6886
let err = nodes[ 1 ] . node . handle_update_add_htlc ( & nodes[ 0 ] . node . get_our_node_id ( ) , & updates. update_add_htlcs [ 0 ] ) ;
6853
- assert ! ( err. is_err( ) ) ;
6887
+
6888
+ if let Err ( HandleError { err, action : _} ) = err {
6889
+ assert_eq ! ( err, "Remote HTLC add would put them over their max HTLC value in flight" ) ;
6890
+ } else {
6891
+ assert ! ( false ) ;
6892
+ }
6893
+
6854
6894
//Confirm the channel was closed
6855
6895
{
6856
6896
assert_eq ! ( nodes[ 1 ] . node. channel_state. lock( ) . unwrap( ) . by_id. len( ) , 0 ) ;
6857
6897
}
6858
6898
//Clear unhandled msg events.
6859
6899
let _ = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
6900
+ }
6901
+
6902
+ #[ test]
6903
+ fn test_update_add_htlc_bolt2_receiver_check_cltv_expiry ( ) {
6904
+ use super :: msgs:: HandleError ;
6860
6905
6861
6906
//BOLT2 Requirement: if sending node sets cltv_expiry to greater or equal to 500000000: SHOULD fail the channel.
6862
6907
let mut nodes = create_network ( 2 ) ;
@@ -6868,13 +6913,24 @@ fn test_update_add_htlc_bolt2() {
6868
6913
let mut updates = get_htlc_update_msgs ! ( nodes[ 0 ] , nodes[ 1 ] . node. get_our_node_id( ) ) ;
6869
6914
updates. update_add_htlcs [ 0 ] . cltv_expiry = 500000000 ;
6870
6915
let err = nodes[ 1 ] . node . handle_update_add_htlc ( & nodes[ 0 ] . node . get_our_node_id ( ) , & updates. update_add_htlcs [ 0 ] ) ;
6871
- assert ! ( err. is_err( ) ) ;
6916
+
6917
+ if let Err ( HandleError { err, action : _} ) = err {
6918
+ assert_eq ! ( err, "Remote provided CLTV expiry in seconds instead of block height" ) ;
6919
+ } else {
6920
+ assert ! ( false ) ;
6921
+ }
6922
+
6872
6923
//Confirm the channel was closed
6873
6924
{
6874
6925
assert_eq ! ( nodes[ 1 ] . node. channel_state. lock( ) . unwrap( ) . by_id. len( ) , 0 ) ;
6875
6926
}
6876
6927
//Clear unhandled msg events.
6877
6928
let _ = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
6929
+ }
6930
+
6931
+ #[ test]
6932
+ fn test_update_add_htlc_bolt2_receiver_check_repeated_id_ignore ( ) {
6933
+ use super :: msgs:: HandleError ;
6878
6934
6879
6935
//BOLT 2 requirement: if the sender did not previously acknowledge the commitment of that HTLC: MUST ignore a repeated id value after a reconnection.
6880
6936
let mut nodes = create_network ( 2 ) ;
0 commit comments