Skip to content

Commit 85d9e70

Browse files
committed
Add bigger test for failing HTLCs claimed through revocation
1 parent efee8c6 commit 85d9e70

File tree

1 file changed

+222
-1
lines changed

1 file changed

+222
-1
lines changed

src/ln/channelmanager.rs

Lines changed: 222 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3388,7 +3388,7 @@ mod tests {
33883388
use rand::{thread_rng,Rng};
33893389

33903390
use std::cell::RefCell;
3391-
use std::collections::{BTreeSet, HashMap};
3391+
use std::collections::{BTreeSet, HashMap, HashSet};
33923392
use std::default::Default;
33933393
use std::rc::Rc;
33943394
use std::sync::{Arc, Mutex};
@@ -6473,6 +6473,227 @@ mod tests {
64736473
}
64746474
}
64756475

6476+
fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool) {
6477+
// Test that if our counterparty broadcasts a revoked commitment transaction we fail all
6478+
// pending HTLCs on that channel backwards even if the HTLCs aren't present in our latest
6479+
// commitment transaction anymore.
6480+
// To do this, we have the peer which will broadcast a revoked commitment transaction send
6481+
// a number of update_fail/commitment_signed updates without ever sending the RAA in
6482+
// response to our commitment_signed. This is somewhat misbehavior-y, though not
6483+
// technically disallowed and we should probably handle it reasonably.
6484+
// Note that this is pretty exhaustive as an outbound HTLC which we haven't yet
6485+
// failed/fulfilled backwards must be in at least one of the latest two remote commitment
6486+
// transactions:
6487+
// * Once we move it out of our holding cell/add it, we will immediately include it in a
6488+
// commitment_signed (implying it will be in the latest remote commitment transaction).
6489+
// * Once they remove it, we will send a (the first) commitment_signed without the HTLC,
6490+
// and once they revoke the previous commitment transaction (allowing us to send a new
6491+
// commitment_signed) we will be free to fail/fulfill the HTLC backwards.
6492+
let mut nodes = create_network(3);
6493+
6494+
// Create some initial channels
6495+
create_announced_chan_between_nodes(&nodes, 0, 1);
6496+
let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
6497+
6498+
let (payment_preimage, _payment_hash) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3000000);
6499+
// Get the will-be-revoked local txn from nodes[2]
6500+
let revoked_local_txn = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone();
6501+
// Revoke the old state
6502+
claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage);
6503+
6504+
let (_, first_payment_hash) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3000000);
6505+
let (_, second_payment_hash) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3000000);
6506+
let (_, third_payment_hash) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3000000);
6507+
6508+
assert!(nodes[2].node.fail_htlc_backwards(&first_payment_hash, PaymentFailReason::PreimageUnknown));
6509+
check_added_monitors!(nodes[2], 1);
6510+
let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
6511+
assert!(updates.update_add_htlcs.is_empty());
6512+
assert!(updates.update_fulfill_htlcs.is_empty());
6513+
assert!(updates.update_fail_malformed_htlcs.is_empty());
6514+
assert_eq!(updates.update_fail_htlcs.len(), 1);
6515+
assert!(updates.update_fee.is_none());
6516+
nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]).unwrap();
6517+
let bs_raa = commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false, true, false, true);
6518+
// Drop the last RAA from 3 -> 2
6519+
6520+
assert!(nodes[2].node.fail_htlc_backwards(&second_payment_hash, PaymentFailReason::PreimageUnknown));
6521+
check_added_monitors!(nodes[2], 1);
6522+
let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
6523+
assert!(updates.update_add_htlcs.is_empty());
6524+
assert!(updates.update_fulfill_htlcs.is_empty());
6525+
assert!(updates.update_fail_malformed_htlcs.is_empty());
6526+
assert_eq!(updates.update_fail_htlcs.len(), 1);
6527+
assert!(updates.update_fee.is_none());
6528+
nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]).unwrap();
6529+
nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &updates.commitment_signed).unwrap();
6530+
check_added_monitors!(nodes[1], 1);
6531+
// Note that nodes[1] is in AwaitingRAA, so won't send a CS
6532+
let as_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
6533+
nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_raa).unwrap();
6534+
check_added_monitors!(nodes[2], 1);
6535+
6536+
assert!(nodes[2].node.fail_htlc_backwards(&third_payment_hash, PaymentFailReason::PreimageUnknown));
6537+
check_added_monitors!(nodes[2], 1);
6538+
let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
6539+
assert!(updates.update_add_htlcs.is_empty());
6540+
assert!(updates.update_fulfill_htlcs.is_empty());
6541+
assert!(updates.update_fail_malformed_htlcs.is_empty());
6542+
assert_eq!(updates.update_fail_htlcs.len(), 1);
6543+
assert!(updates.update_fee.is_none());
6544+
nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]).unwrap();
6545+
// At this point first_payment_hash has dropped out of the latest two commitment
6546+
// transactions that nodes[1] is tracking...
6547+
nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &updates.commitment_signed).unwrap();
6548+
check_added_monitors!(nodes[1], 1);
6549+
// Note that nodes[1] is (still) in AwaitingRAA, so won't send a CS
6550+
let as_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
6551+
nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_raa).unwrap();
6552+
check_added_monitors!(nodes[2], 1);
6553+
6554+
// Add a fourth HTLC, this one will get sequestered away in nodes[1]'s holding cell waiting
6555+
// on nodes[2]'s RAA.
6556+
let route = nodes[1].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
6557+
let (_, fourth_payment_hash) = get_payment_preimage_hash!(nodes[0]);
6558+
nodes[1].node.send_payment(route, fourth_payment_hash).unwrap();
6559+
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
6560+
assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
6561+
check_added_monitors!(nodes[1], 0);
6562+
6563+
if deliver_bs_raa {
6564+
nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_raa).unwrap();
6565+
// One monitor for the new revocation preimage, one as we generate a commitment for
6566+
// nodes[0] to fail first_payment_hash backwards.
6567+
check_added_monitors!(nodes[1], 2);
6568+
}
6569+
6570+
let mut failed_htlcs = HashSet::new();
6571+
assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
6572+
6573+
let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42};
6574+
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
6575+
6576+
let events = nodes[1].node.get_and_clear_pending_events();
6577+
assert_eq!(events.len(), 1);
6578+
match events[0] {
6579+
Event::PaymentFailed { ref payment_hash, .. } => {
6580+
assert_eq!(*payment_hash, fourth_payment_hash);
6581+
},
6582+
_ => panic!("Unexpected event"),
6583+
}
6584+
6585+
if !deliver_bs_raa {
6586+
// If we delivered the RAA already then we already failed first_payment_hash backwards.
6587+
check_added_monitors!(nodes[1], 1);
6588+
}
6589+
6590+
let events = nodes[1].node.get_and_clear_pending_msg_events();
6591+
assert_eq!(events.len(), if deliver_bs_raa { 3 } else { 2 });
6592+
match events[if deliver_bs_raa { 2 } else { 0 }] {
6593+
MessageSendEvent::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
6594+
_ => panic!("Unexpected event"),
6595+
}
6596+
if deliver_bs_raa {
6597+
match events[0] {
6598+
MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, ref update_fulfill_htlcs, ref update_fail_malformed_htlcs, .. } } => {
6599+
assert_eq!(nodes[2].node.get_our_node_id(), *node_id);
6600+
assert_eq!(update_add_htlcs.len(), 1);
6601+
assert!(update_fulfill_htlcs.is_empty());
6602+
assert!(update_fail_htlcs.is_empty());
6603+
assert!(update_fail_malformed_htlcs.is_empty());
6604+
},
6605+
_ => panic!("Unexpected event"),
6606+
}
6607+
}
6608+
// Due to the way backwards-failing occurs we do the updates in two steps.
6609+
let updates = match events[1] {
6610+
MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, ref update_fulfill_htlcs, ref update_fail_malformed_htlcs, ref commitment_signed, .. } } => {
6611+
assert!(update_add_htlcs.is_empty());
6612+
assert_eq!(update_fail_htlcs.len(), 1);
6613+
assert!(update_fulfill_htlcs.is_empty());
6614+
assert!(update_fail_malformed_htlcs.is_empty());
6615+
assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
6616+
6617+
nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[0]).unwrap();
6618+
nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), commitment_signed).unwrap();
6619+
check_added_monitors!(nodes[0], 1);
6620+
let (as_revoke_and_ack, as_commitment_signed) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6621+
nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_and_ack).unwrap();
6622+
check_added_monitors!(nodes[1], 1);
6623+
let bs_second_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
6624+
nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_commitment_signed).unwrap();
6625+
check_added_monitors!(nodes[1], 1);
6626+
let bs_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
6627+
nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
6628+
check_added_monitors!(nodes[0], 1);
6629+
6630+
if !deliver_bs_raa {
6631+
// If we delievered B's RAA we got an unknown preimage error, not something
6632+
// that we should update our routing table for.
6633+
let events = nodes[0].node.get_and_clear_pending_msg_events();
6634+
assert_eq!(events.len(), 1);
6635+
match events[0] {
6636+
MessageSendEvent::PaymentFailureNetworkUpdate { .. } => {},
6637+
_ => panic!("Unexpected event"),
6638+
}
6639+
}
6640+
let events = nodes[0].node.get_and_clear_pending_events();
6641+
assert_eq!(events.len(), 1);
6642+
match events[0] {
6643+
Event::PaymentFailed { ref payment_hash, .. } => {
6644+
assert!(failed_htlcs.insert(*payment_hash));
6645+
},
6646+
_ => panic!("Unexpected event"),
6647+
}
6648+
6649+
bs_second_update
6650+
},
6651+
_ => panic!("Unexpected event"),
6652+
};
6653+
6654+
assert!(updates.update_add_htlcs.is_empty());
6655+
assert_eq!(updates.update_fail_htlcs.len(), 2);
6656+
assert!(updates.update_fulfill_htlcs.is_empty());
6657+
assert!(updates.update_fail_malformed_htlcs.is_empty());
6658+
nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]).unwrap();
6659+
nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[1]).unwrap();
6660+
commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, false, true);
6661+
6662+
let events = nodes[0].node.get_and_clear_pending_msg_events();
6663+
assert_eq!(events.len(), 2);
6664+
for event in events {
6665+
match event {
6666+
MessageSendEvent::PaymentFailureNetworkUpdate { .. } => {},
6667+
_ => panic!("Unexpected event"),
6668+
}
6669+
}
6670+
6671+
let events = nodes[0].node.get_and_clear_pending_events();
6672+
assert_eq!(events.len(), 2);
6673+
match events[0] {
6674+
Event::PaymentFailed { ref payment_hash, .. } => {
6675+
assert!(failed_htlcs.insert(*payment_hash));
6676+
},
6677+
_ => panic!("Unexpected event"),
6678+
}
6679+
match events[1] {
6680+
Event::PaymentFailed { ref payment_hash, .. } => {
6681+
assert!(failed_htlcs.insert(*payment_hash));
6682+
},
6683+
_ => panic!("Unexpected event"),
6684+
}
6685+
6686+
assert!(failed_htlcs.contains(&first_payment_hash));
6687+
assert!(failed_htlcs.contains(&second_payment_hash));
6688+
assert!(failed_htlcs.contains(&third_payment_hash));
6689+
}
6690+
6691+
#[test]
6692+
fn test_commitment_revoked_fail_backward_exhaustive() {
6693+
do_test_commitment_revoked_fail_backward_exhaustive(false);
6694+
do_test_commitment_revoked_fail_backward_exhaustive(true);
6695+
}
6696+
64766697
#[test]
64776698
fn test_htlc_ignore_latest_remote_commitment() {
64786699
// Test that HTLC transactions spending the latest remote commitment transaction are simply

0 commit comments

Comments
 (0)