Skip to content

Commit 1378ae2

Browse files
authored
Merge pull request #3891 from tnull/2025-06-batch-forwarding-delays
Let `BackgroundProcessor` drive HTLC forwarding
2 parents b87a8e0 + 6985f53 commit 1378ae2

27 files changed

+1150
-915
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ use bitcoin::secp256k1::{self, Message, PublicKey, Scalar, Secp256k1, SecretKey}
8686
use lightning::util::dyn_signer::DynSigner;
8787

8888
use std::cell::RefCell;
89-
use std::cmp::{self, Ordering};
89+
use std::cmp;
9090
use std::mem;
9191
use std::sync::atomic;
9292
use std::sync::{Arc, Mutex};
@@ -1309,28 +1309,6 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
13091309
// deduplicate the calls here.
13101310
let mut claim_set = new_hash_map();
13111311
let mut events = nodes[$node].get_and_clear_pending_events();
1312-
// Sort events so that PendingHTLCsForwardable get processed last. This avoids a
1313-
// case where we first process a PendingHTLCsForwardable, then claim/fail on a
1314-
// PaymentClaimable, claiming/failing two HTLCs, but leaving a just-generated
1315-
// PaymentClaimable event for the second HTLC in our pending_events (and breaking
1316-
// our claim_set deduplication).
1317-
events.sort_by(|a, b| {
1318-
if let events::Event::PaymentClaimable { .. } = a {
1319-
if let events::Event::PendingHTLCsForwardable { .. } = b {
1320-
Ordering::Less
1321-
} else {
1322-
Ordering::Equal
1323-
}
1324-
} else if let events::Event::PendingHTLCsForwardable { .. } = a {
1325-
if let events::Event::PaymentClaimable { .. } = b {
1326-
Ordering::Greater
1327-
} else {
1328-
Ordering::Equal
1329-
}
1330-
} else {
1331-
Ordering::Equal
1332-
}
1333-
});
13341312
let had_events = !events.is_empty();
13351313
for event in events.drain(..) {
13361314
match event {
@@ -1357,9 +1335,6 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
13571335
},
13581336
events::Event::PaymentForwarded { .. } if $node == 1 => {},
13591337
events::Event::ChannelReady { .. } => {},
1360-
events::Event::PendingHTLCsForwardable { .. } => {
1361-
nodes[$node].process_pending_htlc_forwards();
1362-
},
13631338
events::Event::HTLCHandlingFailed { .. } => {},
13641339
_ => {
13651340
if out.may_fail.load(atomic::Ordering::Acquire) {
@@ -1370,6 +1345,9 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
13701345
},
13711346
}
13721347
}
1348+
while nodes[$node].needs_pending_htlc_processing() {
1349+
nodes[$node].process_pending_htlc_forwards();
1350+
}
13731351
had_events
13741352
}};
13751353
}
@@ -1811,8 +1789,7 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
18111789
last_pass_no_updates = false;
18121790
continue;
18131791
}
1814-
// ...making sure any pending PendingHTLCsForwardable events are handled and
1815-
// payments claimed.
1792+
// ...making sure any payments are claimed.
18161793
if process_events!(0, false) {
18171794
last_pass_no_updates = false;
18181795
continue;

fuzz/src/full_stack.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,6 @@ pub fn do_test(mut data: &[u8], logger: &Arc<dyn Logger>) {
640640
let mut loss_detector =
641641
MoneyLossDetector::new(&peers, channelmanager.clone(), monitor.clone(), peer_manager);
642642

643-
let mut should_forward = false;
644643
let mut payments_received: Vec<PaymentHash> = Vec::new();
645644
let mut intercepted_htlcs: Vec<InterceptId> = Vec::new();
646645
let mut payments_sent: u16 = 0;
@@ -790,10 +789,7 @@ pub fn do_test(mut data: &[u8], logger: &Arc<dyn Logger>) {
790789
}
791790
},
792791
7 => {
793-
if should_forward {
794-
channelmanager.process_pending_htlc_forwards();
795-
should_forward = false;
796-
}
792+
channelmanager.process_pending_htlc_forwards();
797793
},
798794
8 => {
799795
for payment in payments_received.drain(..) {
@@ -1009,9 +1005,6 @@ pub fn do_test(mut data: &[u8], logger: &Arc<dyn Logger>) {
10091005
//TODO: enhance by fetching random amounts from fuzz input?
10101006
payments_received.push(payment_hash);
10111007
},
1012-
Event::PendingHTLCsForwardable { .. } => {
1013-
should_forward = true;
1014-
},
10151008
Event::HTLCIntercepted { intercept_id, .. } => {
10161009
if !intercepted_htlcs.contains(&intercept_id) {
10171010
intercepted_htlcs.push(intercept_id);

lightning-background-processor/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ bitcoin-io = { version = "0.1.2", default-features = false }
2525
lightning = { version = "0.2.0", path = "../lightning", default-features = false }
2626
lightning-rapid-gossip-sync = { version = "0.2.0", path = "../lightning-rapid-gossip-sync", default-features = false }
2727
lightning-liquidity = { version = "0.2.0", path = "../lightning-liquidity", default-features = false }
28+
possiblyrandom = { version = "0.2", path = "../possiblyrandom", default-features = false }
2829

2930
[dev-dependencies]
3031
tokio = { version = "1.35", features = [ "macros", "rt", "rt-multi-thread", "sync", "time" ] }

lightning-background-processor/src/fwd_batch.rs

Lines changed: 485 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)