Skip to content

Commit eabecfa

Browse files
committed
Add ResourceManager trait
Trait that will be used by the `ChannelManager` to mitigate slow jamming. Core responsibility will be to track resource usage to evaluate HTLC forwarding decisions.
1 parent e1225d5 commit eabecfa

1 file changed

Lines changed: 67 additions & 1 deletion

File tree

lightning/src/ln/resource_manager.rs

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
#![allow(dead_code)]
1111

12-
use core::time::Duration;
12+
use core::{fmt::Display, time::Duration};
1313

1414
use crate::{
1515
crypto::chacha20::ChaCha20,
@@ -18,6 +18,72 @@ use crate::{
1818
sign::EntropySource,
1919
};
2020

21+
/// A trait for managing channel resources and making HTLC forwarding decisions.
22+
pub trait ResourceManager {
23+
/// Registers a new channel with the resource manager for tracking.
24+
///
25+
/// This should be called when a channel becomes ready for forwarding
26+
fn add_channel(
27+
&self, channel_id: u64, max_htlc_value_in_flight_msat: u64, max_accepted_htlcs: u16,
28+
timestamp_unix_secs: u64,
29+
) -> Result<(), ()>;
30+
31+
/// Removes a channel from the resource manager.
32+
///
33+
/// This should be called when a channel is closing.
34+
fn remove_channel(&self, channel_id: u64) -> Result<(), ()>;
35+
36+
/// Evaluates whether an HTLC should be forwarded and updates resource tracking.
37+
///
38+
/// This is called when deciding whether to accept and forward an incoming HTLC. The
39+
/// implementation determines if sufficient resources are available on the incoming
40+
/// channel and whether the outgoing channel is suitable for forwarding.
41+
///
42+
/// Returns a [`ForwardingOutcome`] indicating the forwarding decision:
43+
/// - `ForwardingOutcome::Forward(accountable)`: The HTLC should be forwarded. The boolean
44+
/// flag indicates the accountable signal to use for the outgoing HTLC.
45+
/// - `ForwardingOutcome::Fail`: The HTLC should be failed back to the sender.
46+
fn add_htlc<ES: EntropySource>(
47+
&self, incoming_channel_id: u64, incoming_amount_msat: u64, incoming_cltv_expiry: u32,
48+
outgoing_channel_id: u64, outgoing_amount_msat: u64, incoming_accountable: bool,
49+
htlc_id: u64, height_added: u32, added_at: u64, entropy_source: &ES,
50+
) -> Result<ForwardingOutcome, ()>;
51+
52+
/// Records the resolution of a forwarded HTLC.
53+
///
54+
/// This must be called for HTLCs where [`add_htlc`] returned [`ForwardingOutcome::Forward`].
55+
/// It reports if the HTLC was successfully settled or failed. This allows the implementation
56+
/// to release resources and update any internal tracking state.
57+
///
58+
/// [`add_htlc`]: ResourceManager::add_htlc
59+
fn resolve_htlc(
60+
&self, incoming_channel_id: u64, htlc_id: u64, outgoing_channel_id: u64, settled: bool,
61+
resolved_at: u64,
62+
) -> Result<(), ()>;
63+
}
64+
65+
/// The outcome of an HTLC forwarding decision.
66+
#[derive(PartialEq, Eq, Debug)]
67+
pub enum ForwardingOutcome {
68+
/// Forward the HTLC with the specified accountable signal.
69+
Forward(bool),
70+
/// Fail to forward the HTLC.
71+
Fail,
72+
}
73+
74+
impl Display for ForwardingOutcome {
75+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
76+
match self {
77+
ForwardingOutcome::Forward(signal) => {
78+
write!(f, "Forward as {}", if *signal { "accountable" } else { "unaccountable" })
79+
},
80+
ForwardingOutcome::Fail => {
81+
write!(f, "Fail")
82+
},
83+
}
84+
}
85+
}
86+
2187
#[derive(Clone, PartialEq, Eq, Debug)]
2288
enum BucketAssigned {
2389
General,

0 commit comments

Comments
 (0)