Skip to content

Commit 0719055

Browse files
committed
Addressed PR comments
1 parent 5a5fb9c commit 0719055

File tree

1 file changed

+76
-20
lines changed

1 file changed

+76
-20
lines changed

src/ln/functional_tests.rs

Lines changed: 76 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6604,15 +6604,16 @@ fn test_onion_failure() {
66046604
}
66056605

66066606
#[test]
6607-
fn test_update_add_htlc_bolt2() {
6607+
fn test_update_add_htlc_bolt2_sender() {
66086608
use util::rng;
66096609
use std::sync::atomic::Ordering;
66106610
use super::channelmanager::HTLCSource;
66116611
use super::channel::ChannelError;
66126612

66136613
let secp_ctx = Secp256k1::new();
66146614

6615-
// BOLT 2 Requirements for Sender
6615+
// BOLT 2 Requirements for the Sender when constructing and sending an update_add_htlc message.
6616+
66166617
// 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.
66176618
//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.
66186619

@@ -6637,11 +6638,11 @@ fn test_update_add_htlc_bolt2() {
66376638
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 {
66386639
route: route.clone(),
66396640
session_priv: session_priv.clone(),
6640-
first_hop_htlc_msat: 100000,
6641+
first_hop_htlc_msat: 0,
66416642
}, onion_packet);
66426643

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");
66456646
} else {
66466647
assert!(false);
66476648
}
@@ -6683,17 +6684,13 @@ fn test_update_add_htlc_bolt2() {
66836684
first_hop_htlc_msat: 0,
66846685
}, onion_packet);
66856686

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");
66886689
} else {
66896690
assert!(false);
66906691
}
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();
66946692

66956693
// 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);
66976694
let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 0);
66986695
let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap();
66996696
let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
@@ -6715,15 +6712,14 @@ fn test_update_add_htlc_bolt2() {
67156712
first_hop_htlc_msat: 0,
67166713
}, onion_packet);
67176714

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");
67206717
} else {
67216718
assert!(false);
67226719
}
67236720

67246721
// 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.
67256722
// BOLT 2 Requirement: MUST increase the value of id by 1 for each successive offer.
6726-
let mut nodes = create_network(2);
67276723
let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 0);
67286724
let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap();
67296725
let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
@@ -6752,8 +6748,11 @@ fn test_update_add_htlc_bolt2() {
67526748
assert!(false);
67536749
}
67546750
}
6751+
}
67556752

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;
67576756

67586757
//BOLT2 Requirement: receiving an amount_msat equal to 0, OR less than its own htlc_minimum_msat -> SHOULD fail the channel.
67596758
let mut nodes = create_network(2);
@@ -6772,13 +6771,24 @@ fn test_update_add_htlc_bolt2() {
67726771

67736772
updates.update_add_htlcs[0].amount_msat = htlc_minimum_msat-1;
67746773
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+
67766781
//Confirm the channel was closed
67776782
{
67786783
assert_eq!(nodes[1].node.channel_state.lock().unwrap().by_id.len(), 0);
67796784
}
67806785
//Clear unhandled msg events.
67816786
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;
67826792

67836793
//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
67846794
let mut nodes = create_network(2);
@@ -6791,13 +6801,26 @@ fn test_update_add_htlc_bolt2() {
67916801

67926802
updates.update_add_htlcs[0].amount_msat = 4000001;
67936803
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+
67956811
//Confirm the channel was closed
67966812
{
67976813
assert_eq!(nodes[1].node.channel_state.lock().unwrap().by_id.len(), 0);
67986814
}
67996815
//Clear unhandled msg events.
68006816
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;
68016824

68026825
//BOLT 2 Requirement: if a sending node adds more than its max_accepted_htlcs HTLCs to its local commitment transaction: SHOULD fail the channel
68036826
//BOLT 2 Requirement: MUST allow multiple HTLCs with the same payment_hash.
@@ -6832,13 +6855,24 @@ fn test_update_add_htlc_bolt2() {
68326855
}
68336856
msg.htlc_id = (super::channel::OUR_MAX_HTLCS + 1) as u64;
68346857
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+
68366865
//Confirm the channel was closed
68376866
{
68386867
assert_eq!(nodes[1].node.channel_state.lock().unwrap().by_id.len(), 0);
68396868
}
68406869
//Clear unhandled msg events.
68416870
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;
68426876

68436877
//OR adds more than its max_htlc_value_in_flight_msat worth of offered HTLCs to its local commitment transaction: SHOULD fail the channel
68446878
let mut nodes = create_network(2);
@@ -6850,13 +6884,24 @@ fn test_update_add_htlc_bolt2() {
68506884
let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
68516885
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;
68526886
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+
68546894
//Confirm the channel was closed
68556895
{
68566896
assert_eq!(nodes[1].node.channel_state.lock().unwrap().by_id.len(), 0);
68576897
}
68586898
//Clear unhandled msg events.
68596899
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;
68606905

68616906
//BOLT2 Requirement: if sending node sets cltv_expiry to greater or equal to 500000000: SHOULD fail the channel.
68626907
let mut nodes = create_network(2);
@@ -6868,13 +6913,24 @@ fn test_update_add_htlc_bolt2() {
68686913
let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
68696914
updates.update_add_htlcs[0].cltv_expiry = 500000000;
68706915
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+
68726923
//Confirm the channel was closed
68736924
{
68746925
assert_eq!(nodes[1].node.channel_state.lock().unwrap().by_id.len(), 0);
68756926
}
68766927
//Clear unhandled msg events.
68776928
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;
68786934

68796935
//BOLT 2 requirement: if the sender did not previously acknowledge the commitment of that HTLC: MUST ignore a repeated id value after a reconnection.
68806936
let mut nodes = create_network(2);

0 commit comments

Comments
 (0)