|
1 | 1 | use bitcoin::hashes::{sha256d::Hash as Sha256dHash, Hash}; |
2 | | -use core::time::Duration; |
| 2 | +use core::{fmt::Display, time::Duration}; |
3 | 3 | use hashbrown::hash_map::Entry; |
4 | 4 | use std::time::{SystemTime, UNIX_EPOCH}; |
5 | 5 |
|
6 | 6 | use crate::prelude::{new_hash_map, HashMap}; |
7 | 7 |
|
| 8 | +/// A trait for managing channel resources and making HTLC forwarding decisions. |
| 9 | +pub trait ResourceManager { |
| 10 | + /// Registers a new channel with the resource manager for tracking. |
| 11 | + /// |
| 12 | + /// This should be called when a channel becomes ready for forwarding |
| 13 | + fn add_channel( |
| 14 | + &self, channel_id: u64, max_htlc_value_in_flight_msat: u64, max_accepted_htlcs: u16, |
| 15 | + ) -> Result<(), ()>; |
| 16 | + |
| 17 | + /// Removes a channel from the resource manager. |
| 18 | + /// |
| 19 | + /// This should be called when a channel is closing. |
| 20 | + fn remove_channel(&self, channel_id: u64) -> Result<(), ()>; |
| 21 | + |
| 22 | + /// Evaluates whether an HTLC should be forwarded and updates resource tracking. |
| 23 | + /// |
| 24 | + /// This is called when deciding whether to accept and forward an incoming HTLC. The |
| 25 | + /// implementation determines if sufficient resources are available on the incoming |
| 26 | + /// channel and whether the outgoing channel is suitable for forwarding. |
| 27 | + /// |
| 28 | + /// Returns a [`ForwardingOutcome`] indicating the forwarding decision: |
| 29 | + /// - `ForwardingOutcome::Forward(accountable)`: The HTLC should be forwarded. The boolean |
| 30 | + /// flag indicates the accountable signal to use for the outgoing HTLC. |
| 31 | + /// - `ForwardingOutcome::Fail`: The HTLC should be failed back to the sender. |
| 32 | + fn add_htlc<ES: EntropySource>( |
| 33 | + &self, incoming_channel_id: u64, incoming_amount_msat: u64, incoming_cltv_expiry: u32, |
| 34 | + outgoing_channel_id: u64, outgoing_amount_msat: u64, incoming_accountable: bool, |
| 35 | + htlc_id: u64, height_added: u32, added_at: u64, entropy_source: &ES, |
| 36 | + ) -> Result<ForwardingOutcome, ()>; |
| 37 | + |
| 38 | + /// Records the resolution of a forwarded HTLC. |
| 39 | + /// |
| 40 | + /// This must be called when an HTLC previously accepted via [`add_htlc`] is resolved, |
| 41 | + /// either successfully settled or failed. This allows the implementation to release |
| 42 | + /// resources and update any internal tracking state. |
| 43 | + /// |
| 44 | + /// [`add_htlc`]: ResourceManager::add_htlc |
| 45 | + fn resolve_htlc( |
| 46 | + &self, incoming_channel_id: u64, htlc_id: u64, outgoing_channel_id: u64, settled: bool, |
| 47 | + resolved_at: u64, |
| 48 | + ) -> Result<(), ()>; |
| 49 | +} |
| 50 | + |
| 51 | +/// The outcome of an HTLC forwarding decision. |
| 52 | +#[derive(PartialEq, Eq, Debug)] |
| 53 | +pub enum ForwardingOutcome { |
| 54 | + /// Forward the HTLC with the specified accountable signal. |
| 55 | + Forward(bool), |
| 56 | + /// Fail to forward the HTLC. |
| 57 | + Fail, |
| 58 | +} |
| 59 | + |
| 60 | +impl Display for ForwardingOutcome { |
| 61 | + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 62 | + match self { |
| 63 | + ForwardingOutcome::Forward(signal) => { |
| 64 | + write!(f, "Forward as {}", if *signal { "accountable " } else { "unaccountable" }) |
| 65 | + }, |
| 66 | + ForwardingOutcome::Fail => { |
| 67 | + write!(f, "Fail") |
| 68 | + }, |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
8 | 73 | #[derive(Clone, PartialEq, Eq, Debug)] |
9 | 74 | enum BucketAssigned { |
10 | 75 | General, |
|
0 commit comments