Skip to content

Commit d782de8

Browse files
committed
Store EntropySource in DefaultRouter instead of passing it
...as an arg to `Router`. Passing an `EntropySource` around all the time is a bit strange as the `Router` may or may not actually use it, and the `DefaultRouter` can just as easily store it.
1 parent dee3b31 commit d782de8

File tree

9 files changed

+72
-86
lines changed

9 files changed

+72
-86
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,9 @@ impl Router for FuzzRouter {
103103
})
104104
}
105105

106-
fn create_blinded_payment_paths<
107-
ES: EntropySource + ?Sized, T: secp256k1::Signing + secp256k1::Verification
108-
>(
106+
fn create_blinded_payment_paths<T: secp256k1::Signing + secp256k1::Verification>(
109107
&self, _recipient: PublicKey, _first_hops: Vec<ChannelDetails>, _tlvs: ReceiveTlvs,
110-
_amount_msats: u64, _entropy_source: &ES, _secp_ctx: &Secp256k1<T>
108+
_amount_msats: u64, _secp_ctx: &Secp256k1<T>,
111109
) -> Result<Vec<(BlindedPayInfo, BlindedPath)>, ()> {
112110
unreachable!()
113111
}
@@ -120,11 +118,8 @@ impl MessageRouter for FuzzRouter {
120118
unreachable!()
121119
}
122120

123-
fn create_blinded_paths<
124-
ES: EntropySource + ?Sized, T: secp256k1::Signing + secp256k1::Verification
125-
>(
126-
&self, _recipient: PublicKey, _peers: Vec<PublicKey>, _entropy_source: &ES,
127-
_secp_ctx: &Secp256k1<T>
121+
fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
122+
&self, _recipient: PublicKey, _peers: Vec<PublicKey>, _secp_ctx: &Secp256k1<T>,
128123
) -> Result<Vec<BlindedPath>, ()> {
129124
unreachable!()
130125
}

fuzz/src/full_stack.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,9 @@ impl Router for FuzzRouter {
146146
})
147147
}
148148

149-
fn create_blinded_payment_paths<
150-
ES: EntropySource + ?Sized, T: secp256k1::Signing + secp256k1::Verification
151-
>(
149+
fn create_blinded_payment_paths<T: secp256k1::Signing + secp256k1::Verification>(
152150
&self, _recipient: PublicKey, _first_hops: Vec<ChannelDetails>, _tlvs: ReceiveTlvs,
153-
_amount_msats: u64, _entropy_source: &ES, _secp_ctx: &Secp256k1<T>
151+
_amount_msats: u64, _secp_ctx: &Secp256k1<T>,
154152
) -> Result<Vec<(BlindedPayInfo, BlindedPath)>, ()> {
155153
unreachable!()
156154
}
@@ -163,11 +161,8 @@ impl MessageRouter for FuzzRouter {
163161
unreachable!()
164162
}
165163

166-
fn create_blinded_paths<
167-
ES: EntropySource + ?Sized, T: secp256k1::Signing + secp256k1::Verification
168-
>(
169-
&self, _recipient: PublicKey, _peers: Vec<PublicKey>, _entropy_source: &ES,
170-
_secp_ctx: &Secp256k1<T>
164+
fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
165+
&self, _recipient: PublicKey, _peers: Vec<PublicKey>, _secp_ctx: &Secp256k1<T>,
171166
) -> Result<Vec<BlindedPath>, ()> {
172167
unreachable!()
173168
}

fuzz/src/onion_message.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,8 @@ impl MessageRouter for TestMessageRouter {
8686
})
8787
}
8888

89-
fn create_blinded_paths<
90-
ES: EntropySource + ?Sized, T: secp256k1::Signing + secp256k1::Verification
91-
>(
92-
&self, _recipient: PublicKey, _peers: Vec<PublicKey>, _entropy_source: &ES,
93-
_secp_ctx: &Secp256k1<T>
89+
fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
90+
&self, _recipient: PublicKey, _peers: Vec<PublicKey>, _secp_ctx: &Secp256k1<T>,
9491
) -> Result<Vec<BlindedPath>, ()> {
9592
unreachable!()
9693
}

lightning-background-processor/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,7 @@ mod tests {
984984
Arc<DefaultRouter<
985985
Arc<NetworkGraph<Arc<test_utils::TestLogger>>>,
986986
Arc<test_utils::TestLogger>,
987+
Arc<KeysManager>,
987988
Arc<LockingWrapper<TestScorer>>,
988989
(),
989990
TestScorer>
@@ -1263,12 +1264,12 @@ mod tests {
12631264
let genesis_block = genesis_block(network);
12641265
let network_graph = Arc::new(NetworkGraph::new(network, logger.clone()));
12651266
let scorer = Arc::new(LockingWrapper::new(TestScorer::new()));
1267+
let now = Duration::from_secs(genesis_block.header.time as u64);
12661268
let seed = [i as u8; 32];
1267-
let router = Arc::new(DefaultRouter::new(network_graph.clone(), logger.clone(), seed, scorer.clone(), Default::default()));
1269+
let keys_manager = Arc::new(KeysManager::new(&seed, now.as_secs(), now.subsec_nanos()));
1270+
let router = Arc::new(DefaultRouter::new(network_graph.clone(), logger.clone(), Arc::clone(&keys_manager), scorer.clone(), Default::default()));
12681271
let chain_source = Arc::new(test_utils::TestChainSource::new(Network::Bitcoin));
12691272
let kv_store = Arc::new(FilesystemStore::new(format!("{}_persister_{}", &persist_dir, i).into()));
1270-
let now = Duration::from_secs(genesis_block.header.time as u64);
1271-
let keys_manager = Arc::new(KeysManager::new(&seed, now.as_secs(), now.subsec_nanos()));
12721273
let chain_monitor = Arc::new(chainmonitor::ChainMonitor::new(Some(chain_source.clone()), tx_broadcaster.clone(), logger.clone(), fee_estimator.clone(), kv_store.clone()));
12731274
let best_block = BestBlock::from_network(network);
12741275
let params = ChainParameters { network, best_block };

lightning/src/ln/channelmanager.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,7 @@ pub type SimpleArcChannelManager<M, T, F, L> = ChannelManager<
964964
Arc<DefaultRouter<
965965
Arc<NetworkGraph<Arc<L>>>,
966966
Arc<L>,
967+
Arc<KeysManager>,
967968
Arc<RwLock<ProbabilisticScorer<Arc<NetworkGraph<Arc<L>>>, Arc<L>>>>,
968969
ProbabilisticScoringFeeParameters,
969970
ProbabilisticScorer<Arc<NetworkGraph<Arc<L>>>, Arc<L>>,
@@ -994,6 +995,7 @@ pub type SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, M, T, F, L> =
994995
&'e DefaultRouter<
995996
&'f NetworkGraph<&'g L>,
996997
&'g L,
998+
&'c KeysManager,
997999
&'h RwLock<ProbabilisticScorer<&'f NetworkGraph<&'g L>, &'g L>>,
9981000
ProbabilisticScoringFeeParameters,
9991001
ProbabilisticScorer<&'f NetworkGraph<&'g L>, &'g L>
@@ -7933,7 +7935,6 @@ where
79337935
/// Errors if the `MessageRouter` errors or returns an empty `Vec`.
79347936
fn create_blinded_path(&self) -> Result<BlindedPath, ()> {
79357937
let recipient = self.get_our_node_id();
7936-
let entropy_source = self.entropy_source.deref();
79377938
let secp_ctx = &self.secp_ctx;
79387939

79397940
let peers = self.per_peer_state.read().unwrap()
@@ -7943,7 +7944,7 @@ where
79437944
.collect::<Vec<_>>();
79447945

79457946
self.router
7946-
.create_blinded_paths(recipient, peers, entropy_source, secp_ctx)
7947+
.create_blinded_paths(recipient, peers, secp_ctx)
79477948
.and_then(|paths| paths.into_iter().next().ok_or(()))
79487949
}
79497950

@@ -7952,7 +7953,6 @@ where
79527953
fn create_blinded_payment_paths(
79537954
&self, amount_msats: u64, payment_secret: PaymentSecret
79547955
) -> Result<Vec<(BlindedPayInfo, BlindedPath)>, ()> {
7955-
let entropy_source = self.entropy_source.deref();
79567956
let secp_ctx = &self.secp_ctx;
79577957

79587958
let first_hops = self.list_usable_channels();
@@ -7967,7 +7967,7 @@ where
79677967
},
79687968
};
79697969
self.router.create_blinded_payment_paths(
7970-
payee_node_id, first_hops, payee_tlvs, amount_msats, entropy_source, secp_ctx
7970+
payee_node_id, first_hops, payee_tlvs, amount_msats, secp_ctx
79717971
)
79727972
}
79737973

lightning/src/onion_message/functional_tests.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ use crate::sync::{Arc, Mutex};
3030

3131
use crate::prelude::*;
3232

33+
use core::ops::Deref;
34+
3335
struct MessengerNode {
3436
node_id: PublicKey,
3537
entropy_source: Arc<test_utils::TestKeysInterface>,
@@ -59,10 +61,9 @@ impl MessageRouter for TestMessageRouter {
5961
}
6062

6163
fn create_blinded_paths<
62-
ES: EntropySource + ?Sized, T: secp256k1::Signing + secp256k1::Verification
64+
T: secp256k1::Signing + secp256k1::Verification
6365
>(
64-
&self, _recipient: PublicKey, _peers: Vec<PublicKey>, _entropy_source: &ES,
65-
_secp_ctx: &Secp256k1<T>
66+
&self, _recipient: PublicKey, _peers: Vec<PublicKey>, _secp_ctx: &Secp256k1<T>,
6667
) -> Result<Vec<BlindedPath>, ()> {
6768
unreachable!()
6869
}

lightning/src/onion_message/messenger.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ pub(super) const MAX_TIMER_TICKS: usize = 2;
9797
/// # first_node_addresses: None,
9898
/// # })
9999
/// # }
100-
/// # fn create_blinded_paths<ES: EntropySource + ?Sized, T: secp256k1::Signing + secp256k1::Verification>(
101-
/// # &self, _recipient: PublicKey, _peers: Vec<PublicKey>, _entropy_source: &ES, _secp_ctx: &Secp256k1<T>
100+
/// # fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
101+
/// # &self, _recipient: PublicKey, _peers: Vec<PublicKey>, _secp_ctx: &Secp256k1<T>
102102
/// # ) -> Result<Vec<BlindedPath>, ()> {
103103
/// # unreachable!()
104104
/// # }
@@ -286,34 +286,37 @@ pub trait MessageRouter {
286286
/// Creates [`BlindedPath`]s to the `recipient` node. The nodes in `peers` are assumed to be
287287
/// direct peers with the `recipient`.
288288
fn create_blinded_paths<
289-
ES: EntropySource + ?Sized, T: secp256k1::Signing + secp256k1::Verification
289+
T: secp256k1::Signing + secp256k1::Verification
290290
>(
291-
&self, recipient: PublicKey, peers: Vec<PublicKey>, entropy_source: &ES,
292-
secp_ctx: &Secp256k1<T>
291+
&self, recipient: PublicKey, peers: Vec<PublicKey>, secp_ctx: &Secp256k1<T>,
293292
) -> Result<Vec<BlindedPath>, ()>;
294293
}
295294

296295
/// A [`MessageRouter`] that can only route to a directly connected [`Destination`].
297-
pub struct DefaultMessageRouter<G: Deref<Target=NetworkGraph<L>>, L: Deref>
296+
pub struct DefaultMessageRouter<G: Deref<Target=NetworkGraph<L>>, L: Deref, ES: Deref>
298297
where
299298
L::Target: Logger,
299+
ES::Target: EntropySource,
300300
{
301301
network_graph: G,
302+
entropy_source: ES,
302303
}
303304

304-
impl<G: Deref<Target=NetworkGraph<L>>, L: Deref> DefaultMessageRouter<G, L>
305+
impl<G: Deref<Target=NetworkGraph<L>>, L: Deref, ES: Deref> DefaultMessageRouter<G, L, ES>
305306
where
306307
L::Target: Logger,
308+
ES::Target: EntropySource,
307309
{
308310
/// Creates a [`DefaultMessageRouter`] using the given [`NetworkGraph`].
309-
pub fn new(network_graph: G) -> Self {
310-
Self { network_graph }
311+
pub fn new(network_graph: G, entropy_source: ES) -> Self {
312+
Self { network_graph, entropy_source }
311313
}
312314
}
313315

314-
impl<G: Deref<Target=NetworkGraph<L>>, L: Deref> MessageRouter for DefaultMessageRouter<G, L>
316+
impl<G: Deref<Target=NetworkGraph<L>>, L: Deref, ES: Deref> MessageRouter for DefaultMessageRouter<G, L, ES>
315317
where
316318
L::Target: Logger,
319+
ES::Target: EntropySource,
317320
{
318321
fn find_path(
319322
&self, _sender: PublicKey, peers: Vec<PublicKey>, destination: Destination
@@ -344,10 +347,9 @@ where
344347
}
345348

346349
fn create_blinded_paths<
347-
ES: EntropySource + ?Sized, T: secp256k1::Signing + secp256k1::Verification
350+
T: secp256k1::Signing + secp256k1::Verification
348351
>(
349-
&self, recipient: PublicKey, peers: Vec<PublicKey>, entropy_source: &ES,
350-
secp_ctx: &Secp256k1<T>
352+
&self, recipient: PublicKey, peers: Vec<PublicKey>, secp_ctx: &Secp256k1<T>,
351353
) -> Result<Vec<BlindedPath>, ()> {
352354
// Limit the number of blinded paths that are computed.
353355
const MAX_PATHS: usize = 3;
@@ -367,15 +369,15 @@ where
367369
.unwrap_or(false)
368370
)
369371
.map(|pubkey| vec![*pubkey, recipient])
370-
.map(|node_pks| BlindedPath::new_for_message(&node_pks, entropy_source, secp_ctx))
372+
.map(|node_pks| BlindedPath::new_for_message(&node_pks, &*self.entropy_source, secp_ctx))
371373
.take(MAX_PATHS)
372374
.collect::<Result<Vec<_>, _>>();
373375

374376
match paths {
375377
Ok(paths) if !paths.is_empty() => Ok(paths),
376378
_ => {
377379
if network_graph.nodes().contains_key(&NodeId::from_pubkey(&recipient)) {
378-
BlindedPath::one_hop_for_message(recipient, entropy_source, secp_ctx)
380+
BlindedPath::one_hop_for_message(recipient, &*self.entropy_source, secp_ctx)
379381
.map(|path| vec![path])
380382
} else {
381383
Err(())
@@ -1059,7 +1061,7 @@ pub type SimpleArcOnionMessenger<M, T, F, L> = OnionMessenger<
10591061
Arc<KeysManager>,
10601062
Arc<KeysManager>,
10611063
Arc<L>,
1062-
Arc<DefaultMessageRouter<Arc<NetworkGraph<Arc<L>>>, Arc<L>>>,
1064+
Arc<DefaultMessageRouter<Arc<NetworkGraph<Arc<L>>>, Arc<L>, Arc<KeysManager>>>,
10631065
Arc<SimpleArcChannelManager<M, T, F, L>>,
10641066
IgnoringMessageHandler
10651067
>;
@@ -1078,7 +1080,7 @@ pub type SimpleRefOnionMessenger<
10781080
&'a KeysManager,
10791081
&'a KeysManager,
10801082
&'b L,
1081-
&'i DefaultMessageRouter<&'g NetworkGraph<&'b L>, &'b L>,
1083+
&'i DefaultMessageRouter<&'g NetworkGraph<&'b L>, &'b L, &'a KeysManager>,
10821084
&'j SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, M, T, F, L>,
10831085
IgnoringMessageHandler
10841086
>;

0 commit comments

Comments
 (0)