Skip to content

Commit 351bd3c

Browse files
committed
ln/test: add test coverage for accountable signal propagation
1 parent d3b58e3 commit 351bd3c

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// This file is Copyright its original authors, visible in version control
2+
// history.
3+
//
4+
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5+
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7+
// You may not use this file except in accordance with one or both of these
8+
// licenses.
9+
10+
//! Tests for verifying the correct relay of accountable signals between nodes.
11+
12+
use crate::ln::channelmanager::{
13+
HTLCForwardInfo, PaymentId, PendingAddHTLCInfo, PendingHTLCInfo, RecipientOnionFields, Retry,
14+
};
15+
use crate::ln::functional_test_utils::*;
16+
use crate::ln::msgs::ChannelMessageHandler;
17+
use crate::routing::router::{PaymentParameters, RouteParameters};
18+
19+
fn test_accountable_forwarding_with_override(
20+
override_accountable: Option<bool>, expected_forwarded: bool,
21+
) {
22+
let chanmon_cfgs = create_chanmon_cfgs(3);
23+
let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
24+
let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
25+
let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
26+
27+
let _chan_ab = create_announced_chan_between_nodes(&nodes, 0, 1);
28+
let _chan_bc = create_announced_chan_between_nodes(&nodes, 1, 2);
29+
30+
let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash!(nodes[2]);
31+
let route_params = RouteParameters::from_payment_params_and_value(
32+
PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), TEST_FINAL_CLTV),
33+
100_000,
34+
);
35+
let onion_fields = RecipientOnionFields::secret_only(payment_secret);
36+
let payment_id = PaymentId(payment_hash.0);
37+
nodes[0]
38+
.node
39+
.send_payment(payment_hash, onion_fields, payment_id, route_params, Retry::Attempts(0))
40+
.unwrap();
41+
check_added_monitors(&nodes[0], 1);
42+
43+
let updates_ab = get_htlc_update_msgs(&nodes[0], &nodes[1].node.get_our_node_id());
44+
assert_eq!(updates_ab.update_add_htlcs.len(), 1);
45+
let mut htlc_ab = updates_ab.update_add_htlcs[0].clone();
46+
assert_eq!(htlc_ab.accountable, Some(false));
47+
48+
// Override accountable value if requested
49+
if let Some(override_value) = override_accountable {
50+
htlc_ab.accountable = Some(override_value);
51+
}
52+
53+
nodes[1].node.handle_update_add_htlc(nodes[0].node.get_our_node_id(), &htlc_ab);
54+
do_commitment_signed_dance(&nodes[1], &nodes[0], &updates_ab.commitment_signed, false, false);
55+
expect_and_process_pending_htlcs(&nodes[1], false);
56+
check_added_monitors(&nodes[1], 1);
57+
58+
let updates_bc = get_htlc_update_msgs(&nodes[1], &nodes[2].node.get_our_node_id());
59+
assert_eq!(updates_bc.update_add_htlcs.len(), 1);
60+
let htlc_bc = &updates_bc.update_add_htlcs[0];
61+
assert_eq!(
62+
htlc_bc.accountable,
63+
Some(expected_forwarded),
64+
"B -> C should have accountable = {:?}",
65+
expected_forwarded
66+
);
67+
68+
nodes[2].node.handle_update_add_htlc(nodes[1].node.get_our_node_id(), htlc_bc);
69+
do_commitment_signed_dance(&nodes[2], &nodes[1], &updates_bc.commitment_signed, false, false);
70+
71+
// Accountable signal is not surfaced in PaymentClaimable, so we do our next-best and check
72+
// that the received htlcs that will be processed has the signal set as we expect. We manually
73+
// process pending update adds so that we can access the htlc in forward_htlcs.
74+
nodes[2].node.test_process_pending_update_add_htlcs();
75+
{
76+
let fwds_lock = nodes[2].node.forward_htlcs.lock().unwrap();
77+
let recvs = fwds_lock.get(&0).unwrap();
78+
assert_eq!(recvs.len(), 1);
79+
match recvs[0] {
80+
HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
81+
forward_info: PendingHTLCInfo { incoming_accountable, .. },
82+
..
83+
}) => {
84+
assert_eq!(incoming_accountable, expected_forwarded)
85+
},
86+
_ => panic!("Unexpected forward"),
87+
}
88+
}
89+
90+
expect_and_process_pending_htlcs(&nodes[2], false);
91+
check_added_monitors(&nodes[2], 0);
92+
expect_payment_claimable!(nodes[2], payment_hash, payment_secret, 100_000);
93+
claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage);
94+
}
95+
96+
#[test]
97+
fn test_accountable_signal() {
98+
// Tests forwarding of accountable signal for various incoming signal values.
99+
test_accountable_forwarding_with_override(None, false);
100+
test_accountable_forwarding_with_override(Some(true), true);
101+
test_accountable_forwarding_with_override(Some(false), false);
102+
}

lightning/src/ln/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ pub(crate) mod interactivetxs;
5252
// without the node parameter being mut. This is incorrect, and thus newer rustcs will complain
5353
// about an unnecessary mut. Thus, we silence the unused_mut warning in two test modules below.
5454

55+
#[cfg(test)]
56+
mod accountable_tests;
5557
#[cfg(test)]
5658
#[allow(unused_mut)]
5759
mod async_payments_tests;

0 commit comments

Comments
 (0)