Skip to content

Commit 03a1b52

Browse files
committed
Test basic AMP payments in chanmon_consistency
1 parent 3512d66 commit 03a1b52

File tree

2 files changed

+70
-4
lines changed

2 files changed

+70
-4
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use lightning::chain::chaininterface::{BroadcasterInterface,ConfirmationTarget,C
2727
use lightning::chain::keysinterface::{KeysInterface, InMemoryChannelKeys};
2828
use lightning::ln::channelmonitor;
2929
use lightning::ln::channelmonitor::{ChannelMonitor, ChannelMonitorUpdateErr, HTLCUpdate};
30-
use lightning::ln::channelmanager::{ChannelManager, PaymentHash, PaymentPreimage, ChannelManagerReadArgs};
30+
use lightning::ln::channelmanager::{ChannelManager, PaymentHash, PaymentPreimage, PaymentSecret, ChannelManagerReadArgs};
3131
use lightning::ln::router::{Route, RouteHop};
3232
use lightning::ln::features::{ChannelFeatures, InitFeatures, NodeFeatures};
3333
use lightning::ln::msgs::{CommitmentUpdate, ChannelMessageHandler, ErrorAction, UpdateAddHTLC, Init};
@@ -447,6 +447,48 @@ pub fn do_test(data: &[u8]) {
447447
}
448448
} }
449449
}
450+
macro_rules! send_payment_with_secret {
451+
($source: expr, $middle: expr, $dest: expr) => { {
452+
let payment_hash = Sha256::hash(&[payment_id; 1]);
453+
payment_id = payment_id.wrapping_add(1);
454+
let payment_secret = Sha256::hash(&[payment_id; 1]);
455+
payment_id = payment_id.wrapping_add(1);
456+
if let Err(_) = $source.send_payment(Route {
457+
paths: vec![vec![RouteHop {
458+
pubkey: $middle.0.get_our_node_id(),
459+
node_features: NodeFeatures::empty(),
460+
short_channel_id: $middle.1,
461+
channel_features: ChannelFeatures::empty(),
462+
fee_msat: 50000,
463+
cltv_expiry_delta: 100,
464+
},RouteHop {
465+
pubkey: $dest.0.get_our_node_id(),
466+
node_features: NodeFeatures::empty(),
467+
short_channel_id: $dest.1,
468+
channel_features: ChannelFeatures::empty(),
469+
fee_msat: 5000000,
470+
cltv_expiry_delta: 200,
471+
}],vec![RouteHop {
472+
pubkey: $middle.0.get_our_node_id(),
473+
node_features: NodeFeatures::empty(),
474+
short_channel_id: $middle.1,
475+
channel_features: ChannelFeatures::empty(),
476+
fee_msat: 50000,
477+
cltv_expiry_delta: 100,
478+
},RouteHop {
479+
pubkey: $dest.0.get_our_node_id(),
480+
node_features: NodeFeatures::empty(),
481+
short_channel_id: $dest.1,
482+
channel_features: ChannelFeatures::empty(),
483+
fee_msat: 5000000,
484+
cltv_expiry_delta: 200,
485+
}]],
486+
}, PaymentHash(payment_hash.into_inner()), &Some(PaymentSecret(payment_secret.into_inner()))) {
487+
// Probably ran out of funds
488+
test_return!();
489+
}
490+
} }
491+
}
450492

451493
macro_rules! process_msg_events {
452494
($node: expr, $corrupt_forward: expr) => { {
@@ -599,12 +641,12 @@ pub fn do_test(data: &[u8]) {
599641
});
600642
for event in events.drain(..) {
601643
match event {
602-
events::Event::PaymentReceived { payment_hash, .. } => {
644+
events::Event::PaymentReceived { payment_hash, payment_secret, .. } => {
603645
if claim_set.insert(payment_hash.0) {
604646
if $fail {
605-
assert!(nodes[$node].fail_htlc_backwards(&payment_hash, &None));
647+
assert!(nodes[$node].fail_htlc_backwards(&payment_hash, &payment_secret));
606648
} else {
607-
assert!(nodes[$node].claim_funds(PaymentPreimage(payment_hash.0), &None, 5_000_000));
649+
assert!(nodes[$node].claim_funds(PaymentPreimage(payment_hash.0), &payment_secret, 5_000_000));
608650
}
609651
}
610652
},
@@ -734,6 +776,8 @@ pub fn do_test(data: &[u8]) {
734776
nodes[2] = node_c.clone();
735777
monitor_c = new_monitor_c;
736778
},
779+
0x22 => send_payment_with_secret!(nodes[0], (&nodes[1], chan_a), (&nodes[2], chan_b)),
780+
0x23 => send_payment_with_secret!(nodes[2], (&nodes[1], chan_b), (&nodes[0], chan_a)),
737781
// 0x24 defined above
738782
_ => test_return!(),
739783
}

fuzz/src/full_stack.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,27 @@ pub fn do_test(data: &[u8], logger: &Arc<dyn Logger>) {
406406
Err(_) => return,
407407
}
408408
},
409+
15 => {
410+
let value = slice_to_be24(get_slice!(3)) as u64;
411+
let mut route = match router.get_route(&get_pubkey!(), None, &Vec::new(), value, 42) {
412+
Ok(route) => route,
413+
Err(_) => return,
414+
};
415+
route.paths.push(route.paths[0].clone());
416+
let mut payment_hash = PaymentHash([0; 32]);
417+
payment_hash.0[0..8].copy_from_slice(&be64_to_array(payments_sent));
418+
let mut sha = Sha256::engine();
419+
sha.input(&payment_hash.0[..]);
420+
payment_hash.0 = Sha256::from_engine(sha).into_inner();
421+
payments_sent += 1;
422+
let mut payment_secret = PaymentSecret([0; 32]);
423+
payment_secret.0[0..8].copy_from_slice(&be64_to_array(payments_sent));
424+
payments_sent += 1;
425+
match channelmanager.send_payment(&route, payment_hash, &Some(payment_secret)) {
426+
Ok(_) => {},
427+
Err(_) => return,
428+
}
429+
},
409430
5 => {
410431
let peer_id = get_slice!(1)[0];
411432
if !peers.borrow()[peer_id as usize] { return; }
@@ -513,6 +534,7 @@ pub fn do_test(data: &[u8], logger: &Arc<dyn Logger>) {
513534
channels.sort_by(|a, b| { a.channel_id.cmp(&b.channel_id) });
514535
channelmanager.force_close_channel(&channels[channel_id].channel_id);
515536
},
537+
// 15 is above
516538
_ => return,
517539
}
518540
loss_detector.handler.process_events();

0 commit comments

Comments
 (0)