From: Matt Corallo Date: Tue, 2 Nov 2021 22:33:07 +0000 (+0000) Subject: Require Writeable for all `Score` and impl `Writeable` for `LockedScore` X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=refs%2Fheads%2F2021-10-wip-103-c-bindings;p=rust-lightning Require Writeable for all `Score` and impl `Writeable` for `LockedScore` Ultimately we likely need to wrap the locked `Score` in a struct that exposes writeable somehow, but because all traits have to be fully concretized for C bindings we'll still need `Writeable` on all `Score` in order to expose `Writeable` on the locked score. --- diff --git a/lightning-invoice/src/payment.rs b/lightning-invoice/src/payment.rs index e7b162b38..62fea7947 100644 --- a/lightning-invoice/src/payment.rs +++ b/lightning-invoice/src/payment.rs @@ -67,6 +67,9 @@ //! # } //! # //! # struct FakeScorer {}; +//! # impl lightning::util::ser::Writeable for FakeScorer { +//! # fn write(&self, _: &mut W) -> Result<(), std::io::Error> { unreachable!(); } +//! # } //! # impl routing::Score for FakeScorer { //! # fn channel_penalty_msat( //! # &self, _short_channel_id: u64, _source: &NodeId, _target: &NodeId @@ -1086,6 +1089,9 @@ mod tests { } } + impl lightning::util::ser::Writeable for TestScorer { + fn write(&self, _: &mut W) -> Result<(), std::io::Error> { unreachable!(); } + } impl routing::Score for TestScorer { fn channel_penalty_msat( &self, _short_channel_id: u64, _source: &NodeId, _target: &NodeId diff --git a/lightning/src/routing/mod.rs b/lightning/src/routing/mod.rs index 0282ee93e..74cddfad5 100644 --- a/lightning/src/routing/mod.rs +++ b/lightning/src/routing/mod.rs @@ -18,10 +18,13 @@ use routing::router::RouteHop; use sync::{Mutex, MutexGuard}; +use util::ser::{Writeable, Writer}; +use io; + /// An interface used to score payment channels for path finding. /// /// Scoring is in terms of fees willing to be paid in order to avoid routing through a channel. -pub trait Score { +pub trait Score : Writeable { /// Returns the fee in msats willing to be paid to avoid routing through the given channel /// in the direction from `source` to `target`. fn channel_penalty_msat(&self, short_channel_id: u64, source: &NodeId, target: &NodeId) -> u64; @@ -53,3 +56,10 @@ impl LockableScore { self.scorer.lock().unwrap() } } + +impl Writeable for LockableScore { + #[inline] + fn write(&self, w: &mut W) -> Result<(), io::Error> { + self.scorer.lock().unwrap().write(w) + } +} diff --git a/lightning/src/routing/router.rs b/lightning/src/routing/router.rs index 974ae74e4..549278b80 100644 --- a/lightning/src/routing/router.rs +++ b/lightning/src/routing/router.rs @@ -4549,6 +4549,9 @@ mod tests { short_channel_id: u64, } + impl Writeable for BadChannelScorer { + fn write(&self, _: &mut W) -> Result<(), crate::io::Error> { unreachable!(); } + } impl routing::Score for BadChannelScorer { fn channel_penalty_msat(&self, short_channel_id: u64, _source: &NodeId, _target: &NodeId) -> u64 { if short_channel_id == self.short_channel_id { u64::max_value() } else { 0 } @@ -4561,6 +4564,9 @@ mod tests { node_id: NodeId, } + impl Writeable for BadNodeScorer { + fn write(&self, _: &mut W) -> Result<(), crate::io::Error> { unreachable!(); } + } impl routing::Score for BadNodeScorer { fn channel_penalty_msat(&self, _short_channel_id: u64, _source: &NodeId, target: &NodeId) -> u64 { if *target == self.node_id { u64::max_value() } else { 0 }