Skip to content

Commit fd26091

Browse files
authored
Merge pull request #309 from TheBlueMatt/2019-02-306-nits
Explicit tests for update_*_htlc message BOLT2 requirements
2 parents 5764634 + e354dc2 commit fd26091

File tree

1 file changed

+301
-1
lines changed

1 file changed

+301
-1
lines changed

src/ln/functional_tests.rs

Lines changed: 301 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4806,7 +4806,7 @@ fn test_update_add_htlc_bolt2_sender_exceed_max_htlc_num_and_htlc_id_increment()
48064806
for i in 0..max_accepted_htlcs {
48074807
let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap();
48084808
let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
4809-
let mut payment_event = {
4809+
let payment_event = {
48104810
nodes[0].node.send_payment(route, our_payment_hash).unwrap();
48114811
check_added_monitors!(nodes[0], 1);
48124812

@@ -5052,3 +5052,303 @@ fn test_update_add_htlc_bolt2_receiver_check_repeated_id_ignore() {
50525052
assert!(nodes[1].node.list_channels().is_empty());
50535053
check_closed_broadcast!(nodes[1]);
50545054
}
5055+
5056+
#[test]
5057+
fn test_update_fulfill_htlc_bolt2_update_fulfill_htlc_before_commitment() {
5058+
//BOLT 2 Requirement: until the corresponding HTLC is irrevocably committed in both sides' commitment transactions: MUST NOT send an update_fulfill_htlc, update_fail_htlc, or update_fail_malformed_htlc.
5059+
5060+
let mut nodes = create_network(2);
5061+
let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
5062+
5063+
let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV).unwrap();
5064+
let (our_payment_preimage, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
5065+
nodes[0].node.send_payment(route, our_payment_hash).unwrap();
5066+
check_added_monitors!(nodes[0], 1);
5067+
let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
5068+
nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]).unwrap();
5069+
5070+
let update_msg = msgs::UpdateFulfillHTLC{
5071+
channel_id: chan.2,
5072+
htlc_id: 0,
5073+
payment_preimage: our_payment_preimage,
5074+
};
5075+
5076+
let err = nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_msg);
5077+
5078+
if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
5079+
assert_eq!(err, "Remote tried to fulfill/fail HTLC before it had been committed");
5080+
} else {
5081+
assert!(false);
5082+
}
5083+
5084+
assert!(nodes[0].node.list_channels().is_empty());
5085+
check_closed_broadcast!(nodes[0]);
5086+
}
5087+
5088+
#[test]
5089+
fn test_update_fulfill_htlc_bolt2_update_fail_htlc_before_commitment() {
5090+
//BOLT 2 Requirement: until the corresponding HTLC is irrevocably committed in both sides' commitment transactions: MUST NOT send an update_fulfill_htlc, update_fail_htlc, or update_fail_malformed_htlc.
5091+
5092+
let mut nodes = create_network(2);
5093+
let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
5094+
5095+
let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV).unwrap();
5096+
let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
5097+
nodes[0].node.send_payment(route, our_payment_hash).unwrap();
5098+
check_added_monitors!(nodes[0], 1);
5099+
let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
5100+
nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]).unwrap();
5101+
5102+
let update_msg = msgs::UpdateFailHTLC{
5103+
channel_id: chan.2,
5104+
htlc_id: 0,
5105+
reason: msgs::OnionErrorPacket { data: Vec::new()},
5106+
};
5107+
5108+
let err = nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_msg);
5109+
5110+
if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
5111+
assert_eq!(err, "Remote tried to fulfill/fail HTLC before it had been committed");
5112+
} else {
5113+
assert!(false);
5114+
}
5115+
5116+
assert!(nodes[0].node.list_channels().is_empty());
5117+
check_closed_broadcast!(nodes[0]);
5118+
}
5119+
5120+
#[test]
5121+
fn test_update_fulfill_htlc_bolt2_update_fail_malformed_htlc_before_commitment() {
5122+
//BOLT 2 Requirement: until the corresponding HTLC is irrevocably committed in both sides' commitment transactions: MUST NOT send an update_fulfill_htlc, update_fail_htlc, or update_fail_malformed_htlc.
5123+
5124+
let mut nodes = create_network(2);
5125+
let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
5126+
5127+
let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV).unwrap();
5128+
let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
5129+
nodes[0].node.send_payment(route, our_payment_hash).unwrap();
5130+
check_added_monitors!(nodes[0], 1);
5131+
let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
5132+
nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]).unwrap();
5133+
5134+
let update_msg = msgs::UpdateFailMalformedHTLC{
5135+
channel_id: chan.2,
5136+
htlc_id: 0,
5137+
sha256_of_onion: [1; 32],
5138+
failure_code: 0x8000,
5139+
};
5140+
5141+
let err = nodes[0].node.handle_update_fail_malformed_htlc(&nodes[1].node.get_our_node_id(), &update_msg);
5142+
5143+
if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
5144+
assert_eq!(err, "Remote tried to fulfill/fail HTLC before it had been committed");
5145+
} else {
5146+
assert!(false);
5147+
}
5148+
5149+
assert!(nodes[0].node.list_channels().is_empty());
5150+
check_closed_broadcast!(nodes[0]);
5151+
}
5152+
5153+
#[test]
5154+
fn test_update_fulfill_htlc_bolt2_incorrect_htlc_id() {
5155+
//BOLT 2 Requirement: A receiving node: if the id does not correspond to an HTLC in its current commitment transaction MUST fail the channel.
5156+
5157+
let nodes = create_network(2);
5158+
create_announced_chan_between_nodes(&nodes, 0, 1);
5159+
5160+
let our_payment_preimage = route_payment(&nodes[0], &[&nodes[1]], 100000).0;
5161+
5162+
nodes[1].node.claim_funds(our_payment_preimage);
5163+
check_added_monitors!(nodes[1], 1);
5164+
5165+
let events = nodes[1].node.get_and_clear_pending_msg_events();
5166+
assert_eq!(events.len(), 1);
5167+
let mut update_fulfill_msg: msgs::UpdateFulfillHTLC = {
5168+
match events[0] {
5169+
MessageSendEvent::UpdateHTLCs { node_id: _ , updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, .. } } => {
5170+
assert!(update_add_htlcs.is_empty());
5171+
assert_eq!(update_fulfill_htlcs.len(), 1);
5172+
assert!(update_fail_htlcs.is_empty());
5173+
assert!(update_fail_malformed_htlcs.is_empty());
5174+
assert!(update_fee.is_none());
5175+
update_fulfill_htlcs[0].clone()
5176+
},
5177+
_ => panic!("Unexpected event"),
5178+
}
5179+
};
5180+
5181+
update_fulfill_msg.htlc_id = 1;
5182+
5183+
let err = nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_msg);
5184+
if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
5185+
assert_eq!(err, "Remote tried to fulfill/fail an HTLC we couldn't find");
5186+
} else {
5187+
assert!(false);
5188+
}
5189+
5190+
assert!(nodes[0].node.list_channels().is_empty());
5191+
check_closed_broadcast!(nodes[0]);
5192+
}
5193+
5194+
#[test]
5195+
fn test_update_fulfill_htlc_bolt2_wrong_preimage() {
5196+
//BOLT 2 Requirement: A receiving node: if the payment_preimage value in update_fulfill_htlc doesn't SHA256 hash to the corresponding HTLC payment_hash MUST fail the channel.
5197+
5198+
let nodes = create_network(2);
5199+
create_announced_chan_between_nodes(&nodes, 0, 1);
5200+
5201+
let our_payment_preimage = route_payment(&nodes[0], &[&nodes[1]], 100000).0;
5202+
5203+
nodes[1].node.claim_funds(our_payment_preimage);
5204+
check_added_monitors!(nodes[1], 1);
5205+
5206+
let events = nodes[1].node.get_and_clear_pending_msg_events();
5207+
assert_eq!(events.len(), 1);
5208+
let mut update_fulfill_msg: msgs::UpdateFulfillHTLC = {
5209+
match events[0] {
5210+
MessageSendEvent::UpdateHTLCs { node_id: _ , updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, .. } } => {
5211+
assert!(update_add_htlcs.is_empty());
5212+
assert_eq!(update_fulfill_htlcs.len(), 1);
5213+
assert!(update_fail_htlcs.is_empty());
5214+
assert!(update_fail_malformed_htlcs.is_empty());
5215+
assert!(update_fee.is_none());
5216+
update_fulfill_htlcs[0].clone()
5217+
},
5218+
_ => panic!("Unexpected event"),
5219+
}
5220+
};
5221+
5222+
update_fulfill_msg.payment_preimage = PaymentPreimage([1; 32]);
5223+
5224+
let err = nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_msg);
5225+
if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
5226+
assert_eq!(err, "Remote tried to fulfill HTLC with an incorrect preimage");
5227+
} else {
5228+
assert!(false);
5229+
}
5230+
5231+
assert!(nodes[0].node.list_channels().is_empty());
5232+
check_closed_broadcast!(nodes[0]);
5233+
}
5234+
5235+
5236+
#[test]
5237+
fn test_update_fulfill_htlc_bolt2_missing_badonion_bit_for_malformed_htlc_message() {
5238+
//BOLT 2 Requirement: A receiving node: if the BADONION bit in failure_code is not set for update_fail_malformed_htlc MUST fail the channel.
5239+
5240+
let mut nodes = create_network(2);
5241+
create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000);
5242+
let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV).unwrap();
5243+
let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
5244+
nodes[0].node.send_payment(route, our_payment_hash).unwrap();
5245+
check_added_monitors!(nodes[0], 1);
5246+
5247+
let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
5248+
updates.update_add_htlcs[0].onion_routing_packet.version = 1; //Produce a malformed HTLC message
5249+
5250+
nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]).unwrap();
5251+
check_added_monitors!(nodes[1], 0);
5252+
commitment_signed_dance!(nodes[1], nodes[0], updates.commitment_signed, false, true);
5253+
5254+
let events = nodes[1].node.get_and_clear_pending_msg_events();
5255+
5256+
let mut update_msg: msgs::UpdateFailMalformedHTLC = {
5257+
match events[0] {
5258+
MessageSendEvent::UpdateHTLCs { node_id: _ , updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, .. } } => {
5259+
assert!(update_add_htlcs.is_empty());
5260+
assert!(update_fulfill_htlcs.is_empty());
5261+
assert!(update_fail_htlcs.is_empty());
5262+
assert_eq!(update_fail_malformed_htlcs.len(), 1);
5263+
assert!(update_fee.is_none());
5264+
update_fail_malformed_htlcs[0].clone()
5265+
},
5266+
_ => panic!("Unexpected event"),
5267+
}
5268+
};
5269+
update_msg.failure_code &= !0x8000;
5270+
let err = nodes[0].node.handle_update_fail_malformed_htlc(&nodes[1].node.get_our_node_id(), &update_msg);
5271+
if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
5272+
assert_eq!(err, "Got update_fail_malformed_htlc with BADONION not set");
5273+
} else {
5274+
assert!(false);
5275+
}
5276+
5277+
assert!(nodes[0].node.list_channels().is_empty());
5278+
check_closed_broadcast!(nodes[0]);
5279+
}
5280+
5281+
#[test]
5282+
fn test_update_fulfill_htlc_bolt2_after_malformed_htlc_message_must_forward_update_fail_htlc() {
5283+
//BOLT 2 Requirement: a receiving node which has an outgoing HTLC canceled by update_fail_malformed_htlc:
5284+
// * MUST return an error in the update_fail_htlc sent to the link which originally sent the HTLC, using the failure_code given and setting the data to sha256_of_onion.
5285+
5286+
let mut nodes = create_network(3);
5287+
create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000);
5288+
create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1000000, 1000000);
5289+
5290+
let route = nodes[0].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 100000, TEST_FINAL_CLTV).unwrap();
5291+
let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
5292+
5293+
//First hop
5294+
let mut payment_event = {
5295+
nodes[0].node.send_payment(route, our_payment_hash).unwrap();
5296+
check_added_monitors!(nodes[0], 1);
5297+
let mut events = nodes[0].node.get_and_clear_pending_msg_events();
5298+
assert_eq!(events.len(), 1);
5299+
SendEvent::from_event(events.remove(0))
5300+
};
5301+
nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
5302+
check_added_monitors!(nodes[1], 0);
5303+
commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
5304+
expect_pending_htlcs_forwardable!(nodes[1]);
5305+
let mut events_2 = nodes[1].node.get_and_clear_pending_msg_events();
5306+
assert_eq!(events_2.len(), 1);
5307+
check_added_monitors!(nodes[1], 1);
5308+
payment_event = SendEvent::from_event(events_2.remove(0));
5309+
assert_eq!(payment_event.msgs.len(), 1);
5310+
5311+
//Second Hop
5312+
payment_event.msgs[0].onion_routing_packet.version = 1; //Produce a malformed HTLC message
5313+
nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
5314+
check_added_monitors!(nodes[2], 0);
5315+
commitment_signed_dance!(nodes[2], nodes[1], payment_event.commitment_msg, false, true);
5316+
5317+
let events_3 = nodes[2].node.get_and_clear_pending_msg_events();
5318+
assert_eq!(events_3.len(), 1);
5319+
let update_msg : (msgs::UpdateFailMalformedHTLC, msgs::CommitmentSigned) = {
5320+
match events_3[0] {
5321+
MessageSendEvent::UpdateHTLCs { node_id: _ , updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => {
5322+
assert!(update_add_htlcs.is_empty());
5323+
assert!(update_fulfill_htlcs.is_empty());
5324+
assert!(update_fail_htlcs.is_empty());
5325+
assert_eq!(update_fail_malformed_htlcs.len(), 1);
5326+
assert!(update_fee.is_none());
5327+
(update_fail_malformed_htlcs[0].clone(), commitment_signed.clone())
5328+
},
5329+
_ => panic!("Unexpected event"),
5330+
}
5331+
};
5332+
5333+
nodes[1].node.handle_update_fail_malformed_htlc(&nodes[2].node.get_our_node_id(), &update_msg.0).unwrap();
5334+
5335+
check_added_monitors!(nodes[1], 0);
5336+
commitment_signed_dance!(nodes[1], nodes[2], update_msg.1, false, true);
5337+
expect_pending_htlcs_forwardable!(nodes[1]);
5338+
let events_4 = nodes[1].node.get_and_clear_pending_msg_events();
5339+
assert_eq!(events_4.len(), 1);
5340+
5341+
//Confirm that handlinge the update_malformed_htlc message produces an update_fail_htlc message to be forwarded back along the route
5342+
match events_4[0] {
5343+
MessageSendEvent::UpdateHTLCs { node_id: _ , updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, .. } } => {
5344+
assert!(update_add_htlcs.is_empty());
5345+
assert!(update_fulfill_htlcs.is_empty());
5346+
assert_eq!(update_fail_htlcs.len(), 1);
5347+
assert!(update_fail_malformed_htlcs.is_empty());
5348+
assert!(update_fee.is_none());
5349+
},
5350+
_ => panic!("Unexpected event"),
5351+
};
5352+
5353+
check_added_monitors!(nodes[1], 1);
5354+
}

0 commit comments

Comments
 (0)