Skip to content

Commit 51a3353

Browse files
committed
Move chain::Access to routing and rename it UtxoLookup
The `chain::Access` trait (and the `chain::AccessError` enum) is a bit strange - it only really makes sense if users import it via the `chain` module, otherwise they're left with a trait just called `Access`. Worse, for bindings users its always just called `Access`, in part because many downstream languages don't have a mechanism to import a module and then refer to it. Further, its stuck dangling in the `chain` top-level mod.rs file, sitting in a module that doesn't use it at all (it's only used in `routing::gossip`). Instead, we give it its full name - `UtxoLookup` (and rename the error enum `UtxoLookupError`) and put it in the a new `routing::utxo` module, next to `routing::gossip`.
1 parent 56146e7 commit 51a3353

File tree

11 files changed

+118
-102
lines changed

11 files changed

+118
-102
lines changed

ARCH.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ At a high level, some of the common interfaces fit together as follows:
5151
--------------- / (as EventsProvider) ^ | |
5252
| PeerManager |- \ | | |
5353
--------------- \ | (is-a) | |
54-
| ----------------- \ _---------------- / /
55-
| | chain::Access | \ / | ChainMonitor |---------------
56-
| ----------------- \ / ----------------
54+
| -------------- \ _---------------- / /
55+
| | UtxoLookup | \ / | ChainMonitor |---------------
56+
| -------------- \ / ----------------
5757
| ^ \ / |
5858
(as RoutingMessageHandler) | v v
5959
\ ----------------- --------- -----------------

fuzz/src/full_stack.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ use lightning::ln::peer_handler::{MessageHandler,PeerManager,SocketDescriptor,Ig
4141
use lightning::ln::msgs::{self, DecodeError};
4242
use lightning::ln::script::ShutdownScript;
4343
use lightning::routing::gossip::{P2PGossipSync, NetworkGraph};
44+
use lightning::routing::utxo::UtxoLookup;
4445
use lightning::routing::router::{find_route, InFlightHtlcs, PaymentParameters, Route, RouteHop, RouteParameters, Router};
4546
use lightning::routing::scoring::FixedPenaltyScorer;
4647
use lightning::util::config::UserConfig;
@@ -183,7 +184,7 @@ impl<'a> std::hash::Hash for Peer<'a> {
183184
type ChannelMan<'a> = ChannelManager<
184185
Arc<chainmonitor::ChainMonitor<EnforcingSigner, Arc<dyn chain::Filter>, Arc<TestBroadcaster>, Arc<FuzzEstimator>, Arc<dyn Logger>, Arc<TestPersister>>>,
185186
Arc<TestBroadcaster>, Arc<KeyProvider>, Arc<KeyProvider>, Arc<KeyProvider>, Arc<FuzzEstimator>, &'a FuzzRouter, Arc<dyn Logger>>;
186-
type PeerMan<'a> = PeerManager<Peer<'a>, Arc<ChannelMan<'a>>, Arc<P2PGossipSync<Arc<NetworkGraph<Arc<dyn Logger>>>, Arc<dyn chain::Access>, Arc<dyn Logger>>>, IgnoringMessageHandler, Arc<dyn Logger>, IgnoringMessageHandler, Arc<KeyProvider>>;
187+
type PeerMan<'a> = PeerManager<Peer<'a>, Arc<ChannelMan<'a>>, Arc<P2PGossipSync<Arc<NetworkGraph<Arc<dyn Logger>>>, Arc<dyn UtxoLookup>, Arc<dyn Logger>>>, IgnoringMessageHandler, Arc<dyn Logger>, IgnoringMessageHandler, Arc<KeyProvider>>;
187188

188189
struct MoneyLossDetector<'a> {
189190
manager: Arc<ChannelMan<'a>>,

fuzz/src/router.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ use bitcoin::blockdata::script::Builder;
1111
use bitcoin::blockdata::transaction::TxOut;
1212
use bitcoin::hash_types::BlockHash;
1313

14-
use lightning::chain;
1514
use lightning::chain::transaction::OutPoint;
1615
use lightning::ln::channelmanager::{self, ChannelDetails, ChannelCounterparty};
1716
use lightning::ln::msgs;
1817
use lightning::routing::gossip::{NetworkGraph, RoutingFees};
18+
use lightning::routing::utxo::{UtxoLookup, UtxoLookupError};
1919
use lightning::routing::router::{find_route, PaymentParameters, RouteHint, RouteHintHop, RouteParameters};
2020
use lightning::routing::scoring::FixedPenaltyScorer;
2121
use lightning::util::config::UserConfig;
@@ -84,13 +84,13 @@ impl InputData {
8484
struct FuzzChainSource {
8585
input: Arc<InputData>,
8686
}
87-
impl chain::Access for FuzzChainSource {
88-
fn get_utxo(&self, _genesis_hash: &BlockHash, _short_channel_id: u64) -> Result<TxOut, chain::AccessError> {
87+
impl UtxoLookup for FuzzChainSource {
88+
fn get_utxo(&self, _genesis_hash: &BlockHash, _short_channel_id: u64) -> Result<TxOut, UtxoLookupError> {
8989
match self.input.get_slice(2) {
90-
Some(&[0, _]) => Err(chain::AccessError::UnknownChain),
91-
Some(&[1, _]) => Err(chain::AccessError::UnknownTx),
90+
Some(&[0, _]) => Err(UtxoLookupError::UnknownChain),
91+
Some(&[1, _]) => Err(UtxoLookupError::UnknownTx),
9292
Some(&[_, x]) => Ok(TxOut { value: 0, script_pubkey: Builder::new().push_int(x as i64).into_script().to_v0_p2wsh() }),
93-
None => Err(chain::AccessError::UnknownTx),
93+
None => Err(UtxoLookupError::UnknownTx),
9494
_ => unreachable!(),
9595
}
9696
}

lightning-background-processor/src/lib.rs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use lightning::ln::channelmanager::ChannelManager;
3030
use lightning::ln::msgs::{ChannelMessageHandler, OnionMessageHandler, RoutingMessageHandler};
3131
use lightning::ln::peer_handler::{CustomMessageHandler, PeerManager, SocketDescriptor};
3232
use lightning::routing::gossip::{NetworkGraph, P2PGossipSync};
33+
use lightning::routing::utxo::UtxoLookup;
3334
use lightning::routing::router::Router;
3435
use lightning::routing::scoring::{Score, WriteableScore};
3536
use lightning::util::events::{Event, EventHandler, EventsProvider};
@@ -116,13 +117,13 @@ const FIRST_NETWORK_PRUNE_TIMER: u64 = 1;
116117

117118
/// Either [`P2PGossipSync`] or [`RapidGossipSync`].
118119
pub enum GossipSync<
119-
P: Deref<Target = P2PGossipSync<G, A, L>>,
120+
P: Deref<Target = P2PGossipSync<G, U, L>>,
120121
R: Deref<Target = RapidGossipSync<G, L>>,
121122
G: Deref<Target = NetworkGraph<L>>,
122-
A: Deref,
123+
U: Deref,
123124
L: Deref,
124125
>
125-
where A::Target: chain::Access, L::Target: Logger {
126+
where U::Target: UtxoLookup, L::Target: Logger {
126127
/// Gossip sync via the lightning peer-to-peer network as defined by BOLT 7.
127128
P2P(P),
128129
/// Rapid gossip sync from a trusted server.
@@ -132,13 +133,13 @@ where A::Target: chain::Access, L::Target: Logger {
132133
}
133134

134135
impl<
135-
P: Deref<Target = P2PGossipSync<G, A, L>>,
136+
P: Deref<Target = P2PGossipSync<G, U, L>>,
136137
R: Deref<Target = RapidGossipSync<G, L>>,
137138
G: Deref<Target = NetworkGraph<L>>,
138-
A: Deref,
139+
U: Deref,
139140
L: Deref,
140-
> GossipSync<P, R, G, A, L>
141-
where A::Target: chain::Access, L::Target: Logger {
141+
> GossipSync<P, R, G, U, L>
142+
where U::Target: UtxoLookup, L::Target: Logger {
142143
fn network_graph(&self) -> Option<&G> {
143144
match self {
144145
GossipSync::P2P(gossip_sync) => Some(gossip_sync.network_graph()),
@@ -163,10 +164,10 @@ where A::Target: chain::Access, L::Target: Logger {
163164
}
164165

165166
/// (C-not exported) as the bindings concretize everything and have constructors for us
166-
impl<P: Deref<Target = P2PGossipSync<G, A, L>>, G: Deref<Target = NetworkGraph<L>>, A: Deref, L: Deref>
167-
GossipSync<P, &RapidGossipSync<G, L>, G, A, L>
167+
impl<P: Deref<Target = P2PGossipSync<G, U, L>>, G: Deref<Target = NetworkGraph<L>>, U: Deref, L: Deref>
168+
GossipSync<P, &RapidGossipSync<G, L>, G, U, L>
168169
where
169-
A::Target: chain::Access,
170+
U::Target: UtxoLookup,
170171
L::Target: Logger,
171172
{
172173
/// Initializes a new [`GossipSync::P2P`] variant.
@@ -178,10 +179,10 @@ where
178179
/// (C-not exported) as the bindings concretize everything and have constructors for us
179180
impl<'a, R: Deref<Target = RapidGossipSync<G, L>>, G: Deref<Target = NetworkGraph<L>>, L: Deref>
180181
GossipSync<
181-
&P2PGossipSync<G, &'a (dyn chain::Access + Send + Sync), L>,
182+
&P2PGossipSync<G, &'a (dyn UtxoLookup + Send + Sync), L>,
182183
R,
183184
G,
184-
&'a (dyn chain::Access + Send + Sync),
185+
&'a (dyn UtxoLookup + Send + Sync),
185186
L,
186187
>
187188
where
@@ -196,10 +197,10 @@ where
196197
/// (C-not exported) as the bindings concretize everything and have constructors for us
197198
impl<'a, L: Deref>
198199
GossipSync<
199-
&P2PGossipSync<&'a NetworkGraph<L>, &'a (dyn chain::Access + Send + Sync), L>,
200+
&P2PGossipSync<&'a NetworkGraph<L>, &'a (dyn UtxoLookup + Send + Sync), L>,
200201
&RapidGossipSync<&'a NetworkGraph<L>, L>,
201202
&'a NetworkGraph<L>,
202-
&'a (dyn chain::Access + Send + Sync),
203+
&'a (dyn UtxoLookup + Send + Sync),
203204
L,
204205
>
205206
where
@@ -397,7 +398,7 @@ macro_rules! define_run_body {
397398
#[cfg(feature = "futures")]
398399
pub async fn process_events_async<
399400
'a,
400-
CA: 'static + Deref + Send + Sync,
401+
UL: 'static + Deref + Send + Sync,
401402
CF: 'static + Deref + Send + Sync,
402403
CW: 'static + Deref + Send + Sync,
403404
T: 'static + Deref + Send + Sync,
@@ -418,7 +419,7 @@ pub async fn process_events_async<
418419
PS: 'static + Deref + Send,
419420
M: 'static + Deref<Target = ChainMonitor<<SP::Target as SignerProvider>::Signer, CF, T, F, L, P>> + Send + Sync,
420421
CM: 'static + Deref<Target = ChannelManager<CW, T, ES, NS, SP, F, R, L>> + Send + Sync,
421-
PGS: 'static + Deref<Target = P2PGossipSync<G, CA, L>> + Send + Sync,
422+
PGS: 'static + Deref<Target = P2PGossipSync<G, UL, L>> + Send + Sync,
422423
RGS: 'static + Deref<Target = RapidGossipSync<G, L>> + Send,
423424
UMH: 'static + Deref + Send + Sync,
424425
PM: 'static + Deref<Target = PeerManager<Descriptor, CMH, RMH, OMH, L, UMH, NS>> + Send + Sync,
@@ -428,11 +429,11 @@ pub async fn process_events_async<
428429
Sleeper: Fn(Duration) -> SleepFuture
429430
>(
430431
persister: PS, event_handler: EventHandler, chain_monitor: M, channel_manager: CM,
431-
gossip_sync: GossipSync<PGS, RGS, G, CA, L>, peer_manager: PM, logger: L, scorer: Option<S>,
432+
gossip_sync: GossipSync<PGS, RGS, G, UL, L>, peer_manager: PM, logger: L, scorer: Option<S>,
432433
sleeper: Sleeper,
433434
) -> Result<(), io::Error>
434435
where
435-
CA::Target: 'static + chain::Access,
436+
UL::Target: 'static + UtxoLookup,
436437
CF::Target: 'static + chain::Filter,
437438
CW::Target: 'static + chain::Watch<<SP::Target as SignerProvider>::Signer>,
438439
T::Target: 'static + BroadcasterInterface,
@@ -531,7 +532,7 @@ impl BackgroundProcessor {
531532
/// [`NetworkGraph::write`]: lightning::routing::gossip::NetworkGraph#impl-Writeable
532533
pub fn start<
533534
'a,
534-
CA: 'static + Deref + Send + Sync,
535+
UL: 'static + Deref + Send + Sync,
535536
CF: 'static + Deref + Send + Sync,
536537
CW: 'static + Deref + Send + Sync,
537538
T: 'static + Deref + Send + Sync,
@@ -551,18 +552,18 @@ impl BackgroundProcessor {
551552
PS: 'static + Deref + Send,
552553
M: 'static + Deref<Target = ChainMonitor<<SP::Target as SignerProvider>::Signer, CF, T, F, L, P>> + Send + Sync,
553554
CM: 'static + Deref<Target = ChannelManager<CW, T, ES, NS, SP, F, R, L>> + Send + Sync,
554-
PGS: 'static + Deref<Target = P2PGossipSync<G, CA, L>> + Send + Sync,
555+
PGS: 'static + Deref<Target = P2PGossipSync<G, UL, L>> + Send + Sync,
555556
RGS: 'static + Deref<Target = RapidGossipSync<G, L>> + Send,
556557
UMH: 'static + Deref + Send + Sync,
557558
PM: 'static + Deref<Target = PeerManager<Descriptor, CMH, RMH, OMH, L, UMH, NS>> + Send + Sync,
558559
S: 'static + Deref<Target = SC> + Send + Sync,
559560
SC: for <'b> WriteableScore<'b>,
560561
>(
561562
persister: PS, event_handler: EH, chain_monitor: M, channel_manager: CM,
562-
gossip_sync: GossipSync<PGS, RGS, G, CA, L>, peer_manager: PM, logger: L, scorer: Option<S>,
563+
gossip_sync: GossipSync<PGS, RGS, G, UL, L>, peer_manager: PM, logger: L, scorer: Option<S>,
563564
) -> Self
564565
where
565-
CA::Target: 'static + chain::Access,
566+
UL::Target: 'static + UtxoLookup,
566567
CF::Target: 'static + chain::Filter,
567568
CW::Target: 'static + chain::Watch<<SP::Target as SignerProvider>::Signer>,
568569
T::Target: 'static + BroadcasterInterface,

lightning-net-tokio/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@
3333
//! type FeeEstimator = dyn lightning::chain::chaininterface::FeeEstimator + Send + Sync;
3434
//! type Logger = dyn lightning::util::logger::Logger + Send + Sync;
3535
//! type NodeSigner = dyn lightning::chain::keysinterface::NodeSigner + Send + Sync;
36-
//! type ChainAccess = dyn lightning::chain::Access + Send + Sync;
36+
//! type UtxoLookup = dyn lightning::routing::utxo::UtxoLookup + Send + Sync;
3737
//! type ChainFilter = dyn lightning::chain::Filter + Send + Sync;
3838
//! type DataPersister = dyn lightning::chain::chainmonitor::Persist<lightning::chain::keysinterface::InMemorySigner> + Send + Sync;
3939
//! type ChainMonitor = lightning::chain::chainmonitor::ChainMonitor<lightning::chain::keysinterface::InMemorySigner, Arc<ChainFilter>, Arc<TxBroadcaster>, Arc<FeeEstimator>, Arc<Logger>, Arc<DataPersister>>;
4040
//! type ChannelManager = Arc<lightning::ln::channelmanager::SimpleArcChannelManager<ChainMonitor, TxBroadcaster, FeeEstimator, Logger>>;
41-
//! type PeerManager = Arc<lightning::ln::peer_handler::SimpleArcPeerManager<lightning_net_tokio::SocketDescriptor, ChainMonitor, TxBroadcaster, FeeEstimator, ChainAccess, Logger>>;
41+
//! type PeerManager = Arc<lightning::ln::peer_handler::SimpleArcPeerManager<lightning_net_tokio::SocketDescriptor, ChainMonitor, TxBroadcaster, FeeEstimator, UtxoLookup, Logger>>;
4242
//!
4343
//! // Connect to node with pubkey their_node_id at addr:
4444
//! async fn connect_to_node(peer_manager: PeerManager, chain_monitor: Arc<ChainMonitor>, channel_manager: ChannelManager, their_node_id: PublicKey, addr: SocketAddr) {

lightning/src/chain/mod.rs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use bitcoin::blockdata::block::{Block, BlockHeader};
1313
use bitcoin::blockdata::constants::genesis_block;
1414
use bitcoin::blockdata::script::Script;
15-
use bitcoin::blockdata::transaction::TxOut;
1615
use bitcoin::hash_types::{BlockHash, Txid};
1716
use bitcoin::network::constants::Network;
1817
use bitcoin::secp256k1::PublicKey;
@@ -60,26 +59,6 @@ impl BestBlock {
6059
pub fn height(&self) -> u32 { self.height }
6160
}
6261

63-
/// An error when accessing the chain via [`Access`].
64-
#[derive(Clone, Debug)]
65-
pub enum AccessError {
66-
/// The requested chain is unknown.
67-
UnknownChain,
68-
69-
/// The requested transaction doesn't exist or hasn't confirmed.
70-
UnknownTx,
71-
}
72-
73-
/// The `Access` trait defines behavior for accessing chain data and state, such as blocks and
74-
/// UTXOs.
75-
pub trait Access {
76-
/// Returns the transaction output of a funding transaction encoded by [`short_channel_id`].
77-
/// Returns an error if `genesis_hash` is for a different chain or if such a transaction output
78-
/// is unknown.
79-
///
80-
/// [`short_channel_id`]: https://github.com/lightning/bolts/blob/master/07-routing-gossip.md#definition-of-short_channel_id
81-
fn get_utxo(&self, genesis_hash: &BlockHash, short_channel_id: u64) -> Result<TxOut, AccessError>;
82-
}
8362

8463
/// The `Listen` trait is used to notify when blocks have been connected or disconnected from the
8564
/// chain.

0 commit comments

Comments
 (0)