-/// A [`Router`] implemented using [`find_route`].
-pub struct DefaultRouter<G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref> where
- L::Target: Logger,
- S::Target: for <'a> LockableScore<'a>,
-{
- network_graph: G,
- logger: L,
- random_seed_bytes: Mutex<[u8; 32]>,
- scorer: S
-}
-
-impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref> DefaultRouter<G, L, S> where
- L::Target: Logger,
- S::Target: for <'a> LockableScore<'a>,
-{
- /// Creates a new router using the given [`NetworkGraph`], a [`Logger`], and a randomness source
- /// `random_seed_bytes`.
- pub fn new(network_graph: G, logger: L, random_seed_bytes: [u8; 32], scorer: S) -> Self {
- let random_seed_bytes = Mutex::new(random_seed_bytes);
- Self { network_graph, logger, random_seed_bytes, scorer }
- }
-}
-
-impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref> Router for DefaultRouter<G, L, S> where
- L::Target: Logger,
- S::Target: for <'a> LockableScore<'a>,
-{
- fn find_route(
- &self, payer: &PublicKey, params: &RouteParameters, first_hops: Option<&[&ChannelDetails]>,
- inflight_htlcs: InFlightHtlcs
- ) -> Result<Route, LightningError> {
- let random_seed_bytes = {
- let mut locked_random_seed_bytes = self.random_seed_bytes.lock().unwrap();
- *locked_random_seed_bytes = sha256::Hash::hash(&*locked_random_seed_bytes).into_inner();
- *locked_random_seed_bytes
- };
-
- find_route(
- payer, params, &self.network_graph, first_hops, &*self.logger,
- &ScorerAccountingForInFlightHtlcs::new(&mut self.scorer.lock(), inflight_htlcs),
- &random_seed_bytes
- )
- }
-}
-
-impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref> ScoringRouter for DefaultRouter<G, L, S> where
- L::Target: Logger,
- S::Target: for <'a> LockableScore<'a>,
-{
- fn notify_payment_path_failed(&self, path: &[&RouteHop], short_channel_id: u64) {
- self.scorer.lock().payment_path_failed(path, short_channel_id);
- }
-
- fn notify_payment_path_successful(&self, path: &[&RouteHop]) {
- self.scorer.lock().payment_path_successful(path);
- }
-
- fn notify_payment_probe_successful(&self, path: &[&RouteHop]) {
- self.scorer.lock().probe_successful(path);
- }
-
- fn notify_payment_probe_failed(&self, path: &[&RouteHop], short_channel_id: u64) {
- self.scorer.lock().probe_failed(path, short_channel_id);
- }
-}
-
-impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> Payer for ChannelManager<M, T, K, F, L>
-where
- M::Target: chain::Watch<<K::Target as KeysInterface>::Signer>,
- T::Target: BroadcasterInterface,
- K::Target: KeysInterface,
- F::Target: FeeEstimator,
- L::Target: Logger,
-{
- fn node_id(&self) -> PublicKey {
- self.get_our_node_id()
- }
-
- fn first_hops(&self) -> Vec<ChannelDetails> {
- self.list_usable_channels()
- }
-
- fn send_payment(
- &self, route: &Route, payment_hash: PaymentHash, payment_secret: &Option<PaymentSecret>,
- payment_id: PaymentId
- ) -> Result<(), PaymentSendFailure> {
- self.send_payment(route, payment_hash, payment_secret, payment_id)
- }
-
- fn send_spontaneous_payment(
- &self, route: &Route, payment_preimage: PaymentPreimage, payment_id: PaymentId,
- ) -> Result<(), PaymentSendFailure> {
- self.send_spontaneous_payment(route, Some(payment_preimage), payment_id).map(|_| ())
- }
-
- fn retry_payment(
- &self, route: &Route, payment_id: PaymentId
- ) -> Result<(), PaymentSendFailure> {
- self.retry_payment(route, payment_id)
- }
-
- fn abandon_payment(&self, payment_id: PaymentId) {
- self.abandon_payment(payment_id)
- }
-
- fn inflight_htlcs(&self) -> InFlightHtlcs { self.compute_inflight_htlcs() }
-}
-
-
-/// Used to store information about all the HTLCs that are inflight across all payment attempts.
-pub(crate) struct ScorerAccountingForInFlightHtlcs<'a, S: Score> {
- scorer: &'a mut S,
- /// Maps a channel's short channel id and its direction to the liquidity used up.
- inflight_htlcs: InFlightHtlcs,
-}
-
-impl<'a, S: Score> ScorerAccountingForInFlightHtlcs<'a, S> {
- pub(crate) fn new(scorer: &'a mut S, inflight_htlcs: InFlightHtlcs) -> Self {
- ScorerAccountingForInFlightHtlcs {
- scorer,
- inflight_htlcs
- }
- }
-}
-
-#[cfg(c_bindings)]
-impl<'a, S:Score> lightning::util::ser::Writeable for ScorerAccountingForInFlightHtlcs<'a, S> {
- fn write<W: lightning::util::ser::Writer>(&self, writer: &mut W) -> Result<(), lightning::io::Error> { self.scorer.write(writer) }
-}
-
-impl<'a, S: Score> Score for ScorerAccountingForInFlightHtlcs<'a, S> {
- fn channel_penalty_msat(&self, short_channel_id: u64, source: &NodeId, target: &NodeId, usage: ChannelUsage) -> u64 {
- if let Some(used_liqudity) = self.inflight_htlcs.used_liquidity_msat(
- source, target, short_channel_id
- ) {
- let usage = ChannelUsage {
- inflight_htlc_msat: usage.inflight_htlc_msat + used_liqudity,
- ..usage
- };
-
- self.scorer.channel_penalty_msat(short_channel_id, source, target, usage)
- } else {
- self.scorer.channel_penalty_msat(short_channel_id, source, target, usage)
- }
- }
-
- fn payment_path_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) { unreachable!() }
-
- fn payment_path_successful(&mut self, _path: &[&RouteHop]) { unreachable!() }
-
- fn probe_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) { unreachable!() }
-
- fn probe_successful(&mut self, _path: &[&RouteHop]) { unreachable!() }
-}
-
-