Skip to content

Commit e8b67b2

Browse files
committed
Add some simple tests of payment secret tracking
1 parent 62dcf01 commit e8b67b2

File tree

1 file changed

+159
-1
lines changed

1 file changed

+159
-1
lines changed

lightning/src/ln/functional_tests.rs

Lines changed: 159 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use chain::channelmonitor::{ChannelMonitor, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PER
1919
use chain::transaction::OutPoint;
2020
use chain::keysinterface::{KeysInterface, BaseSign};
2121
use ln::channel::{COMMITMENT_TX_BASE_WEIGHT, COMMITMENT_TX_WEIGHT_PER_HTLC};
22-
use ln::channelmanager::{ChannelManager, ChannelManagerReadArgs, RAACommitmentOrder, PaymentPreimage, PaymentHash, PaymentSendFailure, BREAKDOWN_TIMEOUT};
22+
use ln::channelmanager::{ChannelManager, ChannelManagerReadArgs, RAACommitmentOrder, PaymentPreimage, PaymentSecret, PaymentHash, PaymentSendFailure, BREAKDOWN_TIMEOUT};
2323
use ln::channel::{Channel, ChannelError};
2424
use ln::{chan_utils, onion_utils};
2525
use routing::router::{Route, RouteHop, get_route};
@@ -8127,6 +8127,164 @@ fn test_simple_mpp() {
81278127
claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], false, payment_preimage);
81288128
}
81298129

8130+
#[test]
8131+
fn test_preimage_storage() {
8132+
// Simple test of payment preimage storage allowing no client-side storage to claim payments
8133+
let chanmon_cfgs = create_chanmon_cfgs(2);
8134+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8135+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8136+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8137+
8138+
create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
8139+
8140+
{
8141+
let (payment_hash, payment_secret) = nodes[1].node.get_payment_secret_preimage(Some(100_000), 1008, 42);
8142+
8143+
let logger = test_utils::TestLogger::new();
8144+
let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
8145+
let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 100_000, TEST_FINAL_CLTV, &logger).unwrap();
8146+
nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
8147+
check_added_monitors!(nodes[0], 1);
8148+
let mut events = nodes[0].node.get_and_clear_pending_msg_events();
8149+
let mut payment_event = SendEvent::from_event(events.pop().unwrap());
8150+
nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
8151+
commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
8152+
}
8153+
// Note that after leaving the above scope we have no knowledge of any arguments or return
8154+
// values from previous calls.
8155+
expect_pending_htlcs_forwardable!(nodes[1]);
8156+
let events = nodes[1].node.get_and_clear_pending_events();
8157+
assert_eq!(events.len(), 1);
8158+
match events[0] {
8159+
Event::PaymentReceived { payment_preimage, user_payment_id, .. } => {
8160+
assert_eq!(user_payment_id, 42);
8161+
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage.unwrap());
8162+
},
8163+
_ => panic!("Unexpected event"),
8164+
}
8165+
}
8166+
8167+
#[test]
8168+
fn test_secret_timeout() {
8169+
// Simple test of payment secret storage time outs
8170+
let chanmon_cfgs = create_chanmon_cfgs(2);
8171+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8172+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8173+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8174+
8175+
create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
8176+
8177+
let (payment_hash, payment_secret_1) = nodes[1].node.get_payment_secret_preimage(Some(100_000), 2, 0);
8178+
8179+
// We should fail to register the same payment hash twice, at least until we've connected two
8180+
// blocks.
8181+
if let Err(APIError::APIMisuseError { err }) = nodes[1].node.get_payment_secret(payment_hash, Some(100_000), 2, 0) {
8182+
assert_eq!(err, "Duplicate payment hash");
8183+
} else { panic!(); }
8184+
connect_blocks(&nodes[1], 1);
8185+
if let Err(APIError::APIMisuseError { err }) = nodes[1].node.get_payment_secret(payment_hash, Some(100_000), 2, 0) {
8186+
assert_eq!(err, "Duplicate payment hash");
8187+
} else { panic!(); }
8188+
8189+
// If we then connect the second block, we should be able to register the same payment hash
8190+
// again with a different user_payment_id (this time getting a new payment secret).
8191+
connect_blocks(&nodes[1], 1);
8192+
let our_payment_secret = nodes[1].node.get_payment_secret(payment_hash, Some(100_000), 2, 42).unwrap();
8193+
assert_ne!(payment_secret_1, our_payment_secret);
8194+
8195+
{
8196+
let logger = test_utils::TestLogger::new();
8197+
let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
8198+
let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 100_000, TEST_FINAL_CLTV, &logger).unwrap();
8199+
nodes[0].node.send_payment(&route, payment_hash, &Some(our_payment_secret)).unwrap();
8200+
check_added_monitors!(nodes[0], 1);
8201+
let mut events = nodes[0].node.get_and_clear_pending_msg_events();
8202+
let mut payment_event = SendEvent::from_event(events.pop().unwrap());
8203+
nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
8204+
commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
8205+
}
8206+
// Note that after leaving the above scope we have no knowledge of any arguments or return
8207+
// values from previous calls.
8208+
expect_pending_htlcs_forwardable!(nodes[1]);
8209+
let events = nodes[1].node.get_and_clear_pending_events();
8210+
assert_eq!(events.len(), 1);
8211+
match events[0] {
8212+
Event::PaymentReceived { payment_preimage, payment_secret, user_payment_id, .. } => {
8213+
assert!(payment_preimage.is_none());
8214+
assert_eq!(user_payment_id, 42);
8215+
assert_eq!(payment_secret, our_payment_secret);
8216+
// We don't actually have the payment preimage with which to claim this payment!
8217+
},
8218+
_ => panic!("Unexpected event"),
8219+
}
8220+
}
8221+
8222+
#[test]
8223+
fn test_bad_secret_hash() {
8224+
// Simple test of unregistered payment hash/invalid payment secret handling
8225+
let chanmon_cfgs = create_chanmon_cfgs(2);
8226+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8227+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8228+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8229+
8230+
create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
8231+
8232+
let random_payment_hash = PaymentHash([42; 32]);
8233+
let random_payment_secret = PaymentSecret([43; 32]);
8234+
let (our_payment_hash, our_payment_secret) = nodes[1].node.get_payment_secret_preimage(Some(100_000), 2, 0);
8235+
8236+
let logger = test_utils::TestLogger::new();
8237+
let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
8238+
let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 100_000, TEST_FINAL_CLTV, &logger).unwrap();
8239+
8240+
// All the below cases should end up being handled exactly identically, so we macro the
8241+
// resulting events.
8242+
macro_rules! handle_unknown_invalid_payment_data {
8243+
() => {
8244+
check_added_monitors!(nodes[0], 1);
8245+
let mut events = nodes[0].node.get_and_clear_pending_msg_events();
8246+
let payment_event = SendEvent::from_event(events.pop().unwrap());
8247+
nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
8248+
commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
8249+
8250+
// We have to forward pending HTLCs once to process the receipt of the HTLC and then
8251+
// again to process the pending backwards-failure of the HTLC
8252+
expect_pending_htlcs_forwardable!(nodes[1]);
8253+
expect_pending_htlcs_forwardable!(nodes[1]);
8254+
check_added_monitors!(nodes[1], 1);
8255+
8256+
// We should fail the payment back
8257+
let mut events = nodes[1].node.get_and_clear_pending_msg_events();
8258+
match events.pop().unwrap() {
8259+
MessageSendEvent::UpdateHTLCs { node_id: _, updates: msgs::CommitmentUpdate { update_fail_htlcs, commitment_signed, .. } } => {
8260+
nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[0]);
8261+
commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, false);
8262+
},
8263+
_ => panic!("Unexpected event"),
8264+
}
8265+
}
8266+
}
8267+
8268+
let expected_error_code = 0x4000|15; // incorrect_or_unknown_payment_details
8269+
// Error data is the HTLC value (100,000) and current block height
8270+
let expected_error_data = [0, 0, 0, 0, 0, 1, 0x86, 0xa0, 0, 0, 0, CHAN_CONFIRM_DEPTH as u8];
8271+
8272+
// Send a payment with the right payment hash but the wrong payment secret
8273+
nodes[0].node.send_payment(&route, our_payment_hash, &Some(random_payment_secret)).unwrap();
8274+
handle_unknown_invalid_payment_data!();
8275+
expect_payment_failed!(nodes[0], our_payment_hash, true, expected_error_code, expected_error_data);
8276+
8277+
// Send a payment with a random payment hash, but the right payment secret
8278+
nodes[0].node.send_payment(&route, random_payment_hash, &Some(our_payment_secret)).unwrap();
8279+
handle_unknown_invalid_payment_data!();
8280+
expect_payment_failed!(nodes[0], random_payment_hash, true, expected_error_code, expected_error_data);
8281+
8282+
// Send a payment with a random payment hash and random payment secret
8283+
nodes[0].node.send_payment(&route, random_payment_hash, &Some(random_payment_secret)).unwrap();
8284+
handle_unknown_invalid_payment_data!();
8285+
expect_payment_failed!(nodes[0], random_payment_hash, true, expected_error_code, expected_error_data);
8286+
}
8287+
81308288
#[test]
81318289
fn test_update_err_monitor_lockdown() {
81328290
// Our monitor will lock update of local commitment transaction if a broadcastion condition

0 commit comments

Comments
 (0)