Skip to content

Commit 93be6cd

Browse files
committed
Impl Base AMP in the receive pipeline and expose payment_secret
Base AMP is centered around the concept of a 'payment_secret` - an opaque 32-byte random string which is used to authenticate the sender to the recipient as well as tie the various HTLCs which make up one payment together. This new field gets exposed in a number of places, though sadly only as an Option for backwards compatibility when sending to a receiver/receiving from a sender which does not support Base AMP. Sadly a huge diff here, but almost all of it is changing the method signatures for sending/receiving/failing HTLCs and the PaymentReceived event, which all now need to expose an Option<[u8; 32]> for the payment_secret. It doesn't yet properly fail back pending HTLCs when the full AMP payment is never received (which should result in accidental channel force-closures). Further, as sending AMP payments is not yet supported, the only test here is a simple single-path payment with a payment_secret in it.
1 parent 30a2049 commit 93be6cd

11 files changed

+302
-181
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ pub fn do_test(data: &[u8]) {
417417
fee_msat: 5000000,
418418
cltv_expiry_delta: 200,
419419
}],
420-
}, PaymentHash(payment_hash.into_inner())) {
420+
}, PaymentHash(payment_hash.into_inner()), &None) {
421421
// Probably ran out of funds
422422
test_return!();
423423
}
@@ -441,7 +441,7 @@ pub fn do_test(data: &[u8]) {
441441
fee_msat: 5000000,
442442
cltv_expiry_delta: 200,
443443
}],
444-
}, PaymentHash(payment_hash.into_inner())) {
444+
}, PaymentHash(payment_hash.into_inner()), &None) {
445445
// Probably ran out of funds
446446
test_return!();
447447
}
@@ -602,9 +602,9 @@ pub fn do_test(data: &[u8]) {
602602
events::Event::PaymentReceived { payment_hash, .. } => {
603603
if claim_set.insert(payment_hash.0) {
604604
if $fail {
605-
assert!(nodes[$node].fail_htlc_backwards(&payment_hash));
605+
assert!(nodes[$node].fail_htlc_backwards(&payment_hash, &None));
606606
} else {
607-
assert!(nodes[$node].claim_funds(PaymentPreimage(payment_hash.0), 5_000_000));
607+
assert!(nodes[$node].claim_funds(PaymentPreimage(payment_hash.0), &None, 5_000_000));
608608
}
609609
}
610610
},

fuzz/src/full_stack.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use lightning::chain::chaininterface::{BroadcasterInterface,ConfirmationTarget,C
2222
use lightning::chain::transaction::OutPoint;
2323
use lightning::chain::keysinterface::{InMemoryChannelKeys, KeysInterface};
2424
use lightning::ln::channelmonitor;
25-
use lightning::ln::channelmanager::{ChannelManager, PaymentHash, PaymentPreimage};
25+
use lightning::ln::channelmanager::{ChannelManager, PaymentHash, PaymentPreimage, PaymentSecret};
2626
use lightning::ln::peer_handler::{MessageHandler,PeerManager,SocketDescriptor};
2727
use lightning::ln::router::Router;
2828
use lightning::util::events::{EventsProvider,Event};
@@ -343,7 +343,7 @@ pub fn do_test(data: &[u8], logger: &Arc<dyn Logger>) {
343343
}, 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)));
344344

345345
let mut should_forward = false;
346-
let mut payments_received: Vec<(PaymentHash, u64)> = Vec::new();
346+
let mut payments_received: Vec<(PaymentHash, Option<PaymentSecret>, u64)> = Vec::new();
347347
let mut payments_sent = 0;
348348
let mut pending_funding_generation: Vec<([u8; 32], u64, Script)> = Vec::new();
349349
let mut pending_funding_signatures = HashMap::new();
@@ -401,7 +401,7 @@ pub fn do_test(data: &[u8], logger: &Arc<dyn Logger>) {
401401
sha.input(&payment_hash.0[..]);
402402
payment_hash.0 = Sha256::from_engine(sha).into_inner();
403403
payments_sent += 1;
404-
match channelmanager.send_payment(route, payment_hash) {
404+
match channelmanager.send_payment(route, payment_hash, &None) {
405405
Ok(_) => {},
406406
Err(_) => return,
407407
}
@@ -428,23 +428,23 @@ pub fn do_test(data: &[u8], logger: &Arc<dyn Logger>) {
428428
}
429429
},
430430
8 => {
431-
for (payment, amt) in payments_received.drain(..) {
431+
for (payment, payment_secret, amt) in payments_received.drain(..) {
432432
// SHA256 is defined as XOR of all input bytes placed in the first byte, and 0s
433433
// for the remaining bytes. Thus, if not all remaining bytes are 0s we cannot
434434
// fulfill this HTLC, but if they are, we can just take the first byte and
435435
// place that anywhere in our preimage.
436436
if &payment.0[1..] != &[0; 31] {
437-
channelmanager.fail_htlc_backwards(&payment);
437+
channelmanager.fail_htlc_backwards(&payment, &payment_secret);
438438
} else {
439439
let mut payment_preimage = PaymentPreimage([0; 32]);
440440
payment_preimage.0[0] = payment.0[0];
441-
channelmanager.claim_funds(payment_preimage, amt);
441+
channelmanager.claim_funds(payment_preimage, &payment_secret, amt);
442442
}
443443
}
444444
},
445445
9 => {
446-
for (payment, _) in payments_received.drain(..) {
447-
channelmanager.fail_htlc_backwards(&payment);
446+
for (payment, payment_secret, _) in payments_received.drain(..) {
447+
channelmanager.fail_htlc_backwards(&payment, &payment_secret);
448448
}
449449
},
450450
10 => {
@@ -524,9 +524,9 @@ pub fn do_test(data: &[u8], logger: &Arc<dyn Logger>) {
524524
Event::FundingBroadcastSafe { funding_txo, .. } => {
525525
pending_funding_relay.push(pending_funding_signatures.remove(&funding_txo).unwrap());
526526
},
527-
Event::PaymentReceived { payment_hash, amt } => {
527+
Event::PaymentReceived { payment_hash, payment_secret, amt } => {
528528
//TODO: enhance by fetching random amounts from fuzz input?
529-
payments_received.push((payment_hash, amt));
529+
payments_received.push((payment_hash, payment_secret, amt));
530530
},
531531
Event::PaymentSent {..} => {},
532532
Event::PaymentFailed {..} => {},

0 commit comments

Comments
 (0)