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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content
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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content
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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content
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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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