Skip to content

Require payment secrets and track them in ChannelManager #893

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
3180c43
Set payment_secret to required in features flags
TheBlueMatt Apr 22, 2021
7bf6bd2
Add payment secret and preimage tracking in ChannelManager
TheBlueMatt Apr 22, 2021
7d8dc7a
DRY the get_route_and_payment_hash!() macro duplicated in tests
TheBlueMatt Apr 22, 2021
6e5cf5e
Pipe through PaymentSecrets in tests during payment hash creation
TheBlueMatt Apr 22, 2021
73a3bb3
Use known InvoiceFeatures for routing in tests
TheBlueMatt Apr 23, 2021
a708290
Use payment_secrets in all sends in functional tests
TheBlueMatt Apr 23, 2021
8bf3d8d
Req+check payment secrets for inbound payments pre-PaymentReceived
TheBlueMatt Apr 23, 2021
533a003
[fuzz] Always use PaymentSecrets in chanmon_consistency
TheBlueMatt Apr 23, 2021
25e4f3e
Drop dead code for handling non-MPP payments in claim_funds
TheBlueMatt Apr 23, 2021
210b887
Add a `user_payment_id` to `get_payment_secret`+`PaymentReceived`
TheBlueMatt Apr 27, 2021
5a14048
Drop now-useless PaymentSecret parameters when claiming/failing-back
TheBlueMatt Apr 23, 2021
5e96811
Drop the amount parameter to claim_funds
TheBlueMatt Apr 26, 2021
ecaeddc
Make the PaymentSecret in `PaymentReceived` events non-Optional
TheBlueMatt Apr 23, 2021
3b8ac13
Give users who use `get_payment_secret_preimage` the PaymentPreimage
TheBlueMatt Apr 23, 2021
f9a6cb2
Fail PendingInboundPayments after their expiry time is reached
TheBlueMatt Apr 23, 2021
fd0ebcf
Add some simple tests of payment secret tracking
TheBlueMatt Apr 28, 2021
615ef7d
Add a const and docs for the min `min_final_cltv_expiry` we allow
TheBlueMatt Apr 27, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 36 additions & 68 deletions fuzz/src/chanmon_consistency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,45 +261,58 @@ fn check_payment_err(send_err: PaymentSendFailure) {

type ChanMan = ChannelManager<EnforcingSigner, Arc<TestChainMonitor>, Arc<TestBroadcaster>, Arc<KeyProvider>, Arc<FuzzEstimator>, Arc<dyn Logger>>;

#[inline]
fn get_payment_secret_hash(dest: &ChanMan, payment_id: &mut u8) -> Option<(PaymentSecret, PaymentHash)> {
let mut payment_hash;
for _ in 0..256 {
payment_hash = PaymentHash(Sha256::hash(&[*payment_id; 1]).into_inner());
if let Ok(payment_secret) = dest.create_inbound_payment_for_hash(payment_hash, None, 7200, 0) {
return Some((payment_secret, payment_hash));
}
*payment_id = payment_id.wrapping_add(1);
}
None
}

#[inline]
fn send_payment(source: &ChanMan, dest: &ChanMan, dest_chan_id: u64, amt: u64, payment_id: &mut u8) -> bool {
let payment_hash = Sha256::hash(&[*payment_id; 1]);
*payment_id = payment_id.wrapping_add(1);
let (payment_secret, payment_hash) =
if let Some((secret, hash)) = get_payment_secret_hash(dest, payment_id) { (secret, hash) } else { return true; };
if let Err(err) = source.send_payment(&Route {
paths: vec![vec![RouteHop {
pubkey: dest.get_our_node_id(),
node_features: NodeFeatures::empty(),
node_features: NodeFeatures::known(),
short_channel_id: dest_chan_id,
channel_features: ChannelFeatures::empty(),
channel_features: ChannelFeatures::known(),
fee_msat: amt,
cltv_expiry_delta: 200,
}]],
}, PaymentHash(payment_hash.into_inner()), &None) {
}, payment_hash, &Some(payment_secret)) {
check_payment_err(err);
false
} else { true }
}
#[inline]
fn send_hop_payment(source: &ChanMan, middle: &ChanMan, middle_chan_id: u64, dest: &ChanMan, dest_chan_id: u64, amt: u64, payment_id: &mut u8) -> bool {
let payment_hash = Sha256::hash(&[*payment_id; 1]);
*payment_id = payment_id.wrapping_add(1);
let (payment_secret, payment_hash) =
if let Some((secret, hash)) = get_payment_secret_hash(dest, payment_id) { (secret, hash) } else { return true; };
if let Err(err) = source.send_payment(&Route {
paths: vec![vec![RouteHop {
pubkey: middle.get_our_node_id(),
node_features: NodeFeatures::empty(),
node_features: NodeFeatures::known(),
short_channel_id: middle_chan_id,
channel_features: ChannelFeatures::empty(),
channel_features: ChannelFeatures::known(),
fee_msat: 50000,
cltv_expiry_delta: 100,
},RouteHop {
pubkey: dest.get_our_node_id(),
node_features: NodeFeatures::empty(),
node_features: NodeFeatures::known(),
short_channel_id: dest_chan_id,
channel_features: ChannelFeatures::empty(),
channel_features: ChannelFeatures::known(),
fee_msat: amt,
cltv_expiry_delta: 200,
}]],
}, PaymentHash(payment_hash.into_inner()), &None) {
}, payment_hash, &Some(payment_secret)) {
check_payment_err(err);
false
} else { true }
Expand Down Expand Up @@ -523,48 +536,6 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
}

loop {
macro_rules! send_payment_with_secret {
($source: expr, $middle: expr, $dest: expr) => { {
let payment_hash = Sha256::hash(&[payment_id; 1]);
payment_id = payment_id.wrapping_add(1);
let payment_secret = Sha256::hash(&[payment_id; 1]);
payment_id = payment_id.wrapping_add(1);
if let Err(err) = $source.send_payment(&Route {
paths: vec![vec![RouteHop {
pubkey: $middle.0.get_our_node_id(),
node_features: NodeFeatures::empty(),
short_channel_id: $middle.1,
channel_features: ChannelFeatures::empty(),
fee_msat: 50_000,
cltv_expiry_delta: 100,
},RouteHop {
pubkey: $dest.0.get_our_node_id(),
node_features: NodeFeatures::empty(),
short_channel_id: $dest.1,
channel_features: ChannelFeatures::empty(),
fee_msat: 10_000_000,
cltv_expiry_delta: 200,
}],vec![RouteHop {
pubkey: $middle.0.get_our_node_id(),
node_features: NodeFeatures::empty(),
short_channel_id: $middle.1,
channel_features: ChannelFeatures::empty(),
fee_msat: 50_000,
cltv_expiry_delta: 100,
},RouteHop {
pubkey: $dest.0.get_our_node_id(),
node_features: NodeFeatures::empty(),
short_channel_id: $dest.1,
channel_features: ChannelFeatures::empty(),
fee_msat: 10_000_000,
cltv_expiry_delta: 200,
}]],
}, PaymentHash(payment_hash.into_inner()), &Some(PaymentSecret(payment_secret.into_inner()))) {
check_payment_err(err);
}
} }
}

macro_rules! process_msg_events {
($node: expr, $corrupt_forward: expr) => { {
let events = if $node == 1 {
Expand Down Expand Up @@ -716,12 +687,12 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
let had_events = !events.is_empty();
for event in events.drain(..) {
match event {
events::Event::PaymentReceived { payment_hash, payment_secret, amt } => {
events::Event::PaymentReceived { payment_hash, .. } => {
if claim_set.insert(payment_hash.0) {
if $fail {
assert!(nodes[$node].fail_htlc_backwards(&payment_hash, &payment_secret));
assert!(nodes[$node].fail_htlc_backwards(&payment_hash));
} else {
assert!(nodes[$node].claim_funds(PaymentPreimage(payment_hash.0), &payment_secret, amt));
assert!(nodes[$node].claim_funds(PaymentPreimage(payment_hash.0)));
}
}
},
Expand Down Expand Up @@ -788,15 +759,15 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
},
0x0e => {
if chan_a_disconnected {
nodes[0].peer_connected(&nodes[1].get_our_node_id(), &Init { features: InitFeatures::empty() });
nodes[1].peer_connected(&nodes[0].get_our_node_id(), &Init { features: InitFeatures::empty() });
nodes[0].peer_connected(&nodes[1].get_our_node_id(), &Init { features: InitFeatures::known() });
nodes[1].peer_connected(&nodes[0].get_our_node_id(), &Init { features: InitFeatures::known() });
chan_a_disconnected = false;
}
},
0x0f => {
if chan_b_disconnected {
nodes[1].peer_connected(&nodes[2].get_our_node_id(), &Init { features: InitFeatures::empty() });
nodes[2].peer_connected(&nodes[1].get_our_node_id(), &Init { features: InitFeatures::empty() });
nodes[1].peer_connected(&nodes[2].get_our_node_id(), &Init { features: InitFeatures::known() });
nodes[2].peer_connected(&nodes[1].get_our_node_id(), &Init { features: InitFeatures::known() });
chan_b_disconnected = false;
}
},
Expand Down Expand Up @@ -860,9 +831,6 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
0x24 => { send_hop_payment(&nodes[0], &nodes[1], chan_a, &nodes[2], chan_b, 10_000_000, &mut payment_id); },
0x25 => { send_hop_payment(&nodes[2], &nodes[1], chan_b, &nodes[0], chan_a, 10_000_000, &mut payment_id); },

0x26 => { send_payment_with_secret!(nodes[0], (&nodes[1], chan_a), (&nodes[2], chan_b)); },
0x27 => { send_payment_with_secret!(nodes[2], (&nodes[1], chan_b), (&nodes[0], chan_a)); },

0x28 => { send_payment(&nodes[0], &nodes[1], chan_a, 1_000_000, &mut payment_id); },
0x29 => { send_payment(&nodes[1], &nodes[0], chan_a, 1_000_000, &mut payment_id); },
0x2a => { send_payment(&nodes[1], &nodes[2], chan_b, 1_000_000, &mut payment_id); },
Expand Down Expand Up @@ -936,13 +904,13 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {

// Next, make sure peers are all connected to each other
if chan_a_disconnected {
nodes[0].peer_connected(&nodes[1].get_our_node_id(), &Init { features: InitFeatures::empty() });
nodes[1].peer_connected(&nodes[0].get_our_node_id(), &Init { features: InitFeatures::empty() });
nodes[0].peer_connected(&nodes[1].get_our_node_id(), &Init { features: InitFeatures::known() });
nodes[1].peer_connected(&nodes[0].get_our_node_id(), &Init { features: InitFeatures::known() });
chan_a_disconnected = false;
}
if chan_b_disconnected {
nodes[1].peer_connected(&nodes[2].get_our_node_id(), &Init { features: InitFeatures::empty() });
nodes[2].peer_connected(&nodes[1].get_our_node_id(), &Init { features: InitFeatures::empty() });
nodes[1].peer_connected(&nodes[2].get_our_node_id(), &Init { features: InitFeatures::known() });
nodes[2].peer_connected(&nodes[1].get_our_node_id(), &Init { features: InitFeatures::known() });
chan_b_disconnected = false;
}

Expand Down
25 changes: 17 additions & 8 deletions fuzz/src/full_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ pub fn do_test(data: &[u8], logger: &Arc<dyn Logger>) {
}, our_network_key, &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0], Arc::clone(&logger)));

let mut should_forward = false;
let mut payments_received: Vec<(PaymentHash, Option<PaymentSecret>, u64)> = Vec::new();
let mut payments_received: Vec<PaymentHash> = Vec::new();
let mut payments_sent = 0;
let mut pending_funding_generation: Vec<([u8; 32], u64, Script)> = Vec::new();
let mut pending_funding_signatures = HashMap::new();
Expand Down Expand Up @@ -476,23 +476,32 @@ pub fn do_test(data: &[u8], logger: &Arc<dyn Logger>) {
}
},
8 => {
for (payment, payment_secret, amt) in payments_received.drain(..) {
for payment in payments_received.drain(..) {
// SHA256 is defined as XOR of all input bytes placed in the first byte, and 0s
// for the remaining bytes. Thus, if not all remaining bytes are 0s we cannot
// fulfill this HTLC, but if they are, we can just take the first byte and
// place that anywhere in our preimage.
if &payment.0[1..] != &[0; 31] {
channelmanager.fail_htlc_backwards(&payment, &payment_secret);
channelmanager.fail_htlc_backwards(&payment);
} else {
let mut payment_preimage = PaymentPreimage([0; 32]);
payment_preimage.0[0] = payment.0[0];
channelmanager.claim_funds(payment_preimage, &payment_secret, amt);
channelmanager.claim_funds(payment_preimage);
}
}
},
16 => {
let payment_preimage = PaymentPreimage(keys_manager.get_secure_random_bytes());
let mut sha = Sha256::engine();
sha.input(&payment_preimage.0[..]);
let payment_hash = PaymentHash(Sha256::from_engine(sha).into_inner());
// Note that this may fail - our hashes may collide and we'll end up trying to
// double-register the same payment_hash.
let _ = channelmanager.create_inbound_payment_for_hash(payment_hash, None, 1, 0);
},
9 => {
for (payment, payment_secret, _) in payments_received.drain(..) {
channelmanager.fail_htlc_backwards(&payment, &payment_secret);
for payment in payments_received.drain(..) {
channelmanager.fail_htlc_backwards(&payment);
}
},
10 => {
Expand Down Expand Up @@ -571,9 +580,9 @@ pub fn do_test(data: &[u8], logger: &Arc<dyn Logger>) {
Event::FundingGenerationReady { temporary_channel_id, channel_value_satoshis, output_script, .. } => {
pending_funding_generation.push((temporary_channel_id, channel_value_satoshis, output_script));
},
Event::PaymentReceived { payment_hash, payment_secret, amt } => {
Event::PaymentReceived { payment_hash, .. } => {
//TODO: enhance by fetching random amounts from fuzz input?
payments_received.push((payment_hash, payment_secret, amt));
payments_received.push(payment_hash);
},
Event::PaymentSent {..} => {},
Event::PaymentFailed {..} => {},
Expand Down
4 changes: 2 additions & 2 deletions lightning-persister/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,9 @@ mod tests {
check_persisted_data!(0);

// Send a few payments and make sure the monitors are updated to the latest.
send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000, 8_000_000);
send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
check_persisted_data!(5);
send_payment(&nodes[1], &vec!(&nodes[0])[..], 4000000, 4_000_000);
send_payment(&nodes[1], &vec!(&nodes[0])[..], 4000000);
check_persisted_data!(10);

// Force close because cooperative close doesn't result in any persisted
Expand Down
2 changes: 1 addition & 1 deletion lightning/src/chain/chainmonitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ mod tests {
let (commitment_tx, htlc_tx) = {
let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 5_000_000).0;
let mut txn = get_local_commitment_txn!(nodes[0], channel.2);
claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage, 5_000_000);
claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);

assert_eq!(txn.len(), 2);
(txn.remove(0), txn.remove(0))
Expand Down
Loading