10
10
//! The router finds paths within a [`NetworkGraph`] for a payment.
11
11
12
12
use bitcoin:: secp256k1:: { PublicKey , Secp256k1 , self } ;
13
- use bitcoin:: hashes:: Hash ;
14
- use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
15
13
16
14
use crate :: blinded_path:: { BlindedHop , BlindedPath } ;
17
15
use crate :: blinded_path:: payment:: { ForwardNode , ForwardTlvs , PaymentConstraints , PaymentRelay , ReceiveTlvs } ;
@@ -30,39 +28,40 @@ use crate::crypto::chacha20::ChaCha20;
30
28
31
29
use crate :: io;
32
30
use crate :: prelude:: * ;
33
- use crate :: sync:: Mutex ;
34
31
use alloc:: collections:: BinaryHeap ;
35
32
use core:: { cmp, fmt} ;
36
33
use core:: ops:: Deref ;
37
34
38
35
/// A [`Router`] implemented using [`find_route`].
39
- pub struct DefaultRouter < G : Deref < Target = NetworkGraph < L > > + Clone , L : Deref , S : Deref , SP : Sized , Sc : ScoreLookUp < ScoreParams = SP > > where
36
+ pub struct DefaultRouter < G : Deref < Target = NetworkGraph < L > > + Clone , L : Deref , ES : Deref , S : Deref , SP : Sized , Sc : ScoreLookUp < ScoreParams = SP > > where
40
37
L :: Target : Logger ,
41
38
S :: Target : for < ' a > LockableScore < ' a , ScoreLookUp = Sc > ,
39
+ ES :: Target : EntropySource ,
42
40
{
43
41
network_graph : G ,
44
42
logger : L ,
45
- random_seed_bytes : Mutex < [ u8 ; 32 ] > ,
43
+ entropy_source : ES ,
46
44
scorer : S ,
47
45
score_params : SP ,
48
- message_router : DefaultMessageRouter < G , L > ,
46
+ message_router : DefaultMessageRouter < G , L , ES > ,
49
47
}
50
48
51
- impl < G : Deref < Target = NetworkGraph < L > > + Clone , L : Deref , S : Deref , SP : Sized , Sc : ScoreLookUp < ScoreParams = SP > > DefaultRouter < G , L , S , SP , Sc > where
49
+ impl < G : Deref < Target = NetworkGraph < L > > + Clone , L : Deref , ES : Deref + Clone , S : Deref , SP : Sized , Sc : ScoreLookUp < ScoreParams = SP > > DefaultRouter < G , L , ES , S , SP , Sc > where
52
50
L :: Target : Logger ,
53
51
S :: Target : for < ' a > LockableScore < ' a , ScoreLookUp = Sc > ,
52
+ ES :: Target : EntropySource ,
54
53
{
55
54
/// Creates a new router.
56
- pub fn new ( network_graph : G , logger : L , random_seed_bytes : [ u8 ; 32 ] , scorer : S , score_params : SP ) -> Self {
57
- let random_seed_bytes = Mutex :: new ( random_seed_bytes) ;
58
- let message_router = DefaultMessageRouter :: new ( network_graph. clone ( ) ) ;
59
- Self { network_graph, logger, random_seed_bytes, scorer, score_params, message_router }
55
+ pub fn new ( network_graph : G , logger : L , entropy_source : ES , scorer : S , score_params : SP ) -> Self {
56
+ let message_router = DefaultMessageRouter :: new ( network_graph. clone ( ) , entropy_source. clone ( ) ) ;
57
+ Self { network_graph, logger, entropy_source, scorer, score_params, message_router }
60
58
}
61
59
}
62
60
63
- impl < G : Deref < Target = NetworkGraph < L > > + Clone , L : Deref , S : Deref , SP : Sized , Sc : ScoreLookUp < ScoreParams = SP > > Router for DefaultRouter < G , L , S , SP , Sc > where
61
+ impl < G : Deref < Target = NetworkGraph < L > > + Clone , L : Deref , ES : Deref , S : Deref , SP : Sized , Sc : ScoreLookUp < ScoreParams = SP > > Router for DefaultRouter < G , L , ES , S , SP , Sc > where
64
62
L :: Target : Logger ,
65
63
S :: Target : for < ' a > LockableScore < ' a , ScoreLookUp = Sc > ,
64
+ ES :: Target : EntropySource ,
66
65
{
67
66
fn find_route (
68
67
& self ,
@@ -71,11 +70,7 @@ impl<G: Deref<Target = NetworkGraph<L>> + Clone, L: Deref, S: Deref, SP: Sized,
71
70
first_hops : Option < & [ & ChannelDetails ] > ,
72
71
inflight_htlcs : InFlightHtlcs
73
72
) -> Result < Route , LightningError > {
74
- let random_seed_bytes = {
75
- let mut locked_random_seed_bytes = self . random_seed_bytes . lock ( ) . unwrap ( ) ;
76
- * locked_random_seed_bytes = Sha256 :: hash ( & * locked_random_seed_bytes) . to_byte_array ( ) ;
77
- * locked_random_seed_bytes
78
- } ;
73
+ let random_seed_bytes = self . entropy_source . get_secure_random_bytes ( ) ;
79
74
find_route (
80
75
payer, params, & self . network_graph , first_hops, & * self . logger ,
81
76
& ScorerAccountingForInFlightHtlcs :: new ( self . scorer . read_lock ( ) , & inflight_htlcs) ,
@@ -85,10 +80,10 @@ impl<G: Deref<Target = NetworkGraph<L>> + Clone, L: Deref, S: Deref, SP: Sized,
85
80
}
86
81
87
82
fn create_blinded_payment_paths <
88
- ES : EntropySource + ? Sized , T : secp256k1:: Signing + secp256k1:: Verification
89
- > (
83
+ T : secp256k1:: Signing + secp256k1:: Verification
84
+ > (
90
85
& self , recipient : PublicKey , first_hops : Vec < ChannelDetails > , tlvs : ReceiveTlvs ,
91
- amount_msats : u64 , entropy_source : & ES , secp_ctx : & Secp256k1 < T >
86
+ amount_msats : u64 , secp_ctx : & Secp256k1 < T >
92
87
) -> Result < Vec < ( BlindedPayInfo , BlindedPath ) > , ( ) > {
93
88
// Limit the number of blinded paths that are computed.
94
89
const MAX_PAYMENT_PATHS : usize = 3 ;
@@ -139,7 +134,7 @@ impl<G: Deref<Target = NetworkGraph<L>> + Clone, L: Deref, S: Deref, SP: Sized,
139
134
} )
140
135
. map ( |forward_node| {
141
136
BlindedPath :: new_for_payment (
142
- & [ forward_node] , recipient, tlvs. clone ( ) , u64:: MAX , entropy_source, secp_ctx
137
+ & [ forward_node] , recipient, tlvs. clone ( ) , u64:: MAX , & * self . entropy_source , secp_ctx
143
138
)
144
139
} )
145
140
. take ( MAX_PAYMENT_PATHS )
@@ -149,7 +144,7 @@ impl<G: Deref<Target = NetworkGraph<L>> + Clone, L: Deref, S: Deref, SP: Sized,
149
144
Ok ( paths) if !paths. is_empty ( ) => Ok ( paths) ,
150
145
_ => {
151
146
if network_graph. nodes ( ) . contains_key ( & NodeId :: from_pubkey ( & recipient) ) {
152
- BlindedPath :: one_hop_for_payment ( recipient, tlvs, entropy_source, secp_ctx)
147
+ BlindedPath :: one_hop_for_payment ( recipient, tlvs, & * self . entropy_source , secp_ctx)
153
148
. map ( |path| vec ! [ path] )
154
149
} else {
155
150
Err ( ( ) )
@@ -159,9 +154,10 @@ impl<G: Deref<Target = NetworkGraph<L>> + Clone, L: Deref, S: Deref, SP: Sized,
159
154
}
160
155
}
161
156
162
- impl < G : Deref < Target = NetworkGraph < L > > + Clone , L : Deref , S : Deref , SP : Sized , Sc : ScoreLookUp < ScoreParams = SP > > MessageRouter for DefaultRouter < G , L , S , SP , Sc > where
157
+ impl < G : Deref < Target = NetworkGraph < L > > + Clone , L : Deref , ES : Deref , S : Deref , SP : Sized , Sc : ScoreLookUp < ScoreParams = SP > > MessageRouter for DefaultRouter < G , L , ES , S , SP , Sc > where
163
158
L :: Target : Logger ,
164
159
S :: Target : for < ' a > LockableScore < ' a , ScoreLookUp = Sc > ,
160
+ ES :: Target : EntropySource ,
165
161
{
166
162
fn find_path (
167
163
& self , sender : PublicKey , peers : Vec < PublicKey > , destination : Destination
@@ -170,12 +166,11 @@ impl< G: Deref<Target = NetworkGraph<L>> + Clone, L: Deref, S: Deref, SP: Sized,
170
166
}
171
167
172
168
fn create_blinded_paths <
173
- ES : EntropySource + ?Sized , T : secp256k1:: Signing + secp256k1:: Verification
174
- > (
175
- & self , recipient : PublicKey , peers : Vec < PublicKey > , entropy_source : & ES ,
176
- secp_ctx : & Secp256k1 < T >
169
+ T : secp256k1:: Signing + secp256k1:: Verification
170
+ > (
171
+ & self , recipient : PublicKey , peers : Vec < PublicKey > , secp_ctx : & Secp256k1 < T > ,
177
172
) -> Result < Vec < BlindedPath > , ( ) > {
178
- self . message_router . create_blinded_paths ( recipient, peers, entropy_source , secp_ctx)
173
+ self . message_router . create_blinded_paths ( recipient, peers, secp_ctx)
179
174
}
180
175
}
181
176
@@ -209,10 +204,10 @@ pub trait Router: MessageRouter {
209
204
/// are assumed to be with the `recipient`'s peers. The payment secret and any constraints are
210
205
/// given in `tlvs`.
211
206
fn create_blinded_payment_paths <
212
- ES : EntropySource + ? Sized , T : secp256k1:: Signing + secp256k1:: Verification
213
- > (
207
+ T : secp256k1:: Signing + secp256k1:: Verification
208
+ > (
214
209
& self , recipient : PublicKey , first_hops : Vec < ChannelDetails > , tlvs : ReceiveTlvs ,
215
- amount_msats : u64 , entropy_source : & ES , secp_ctx : & Secp256k1 < T >
210
+ amount_msats : u64 , secp_ctx : & Secp256k1 < T >
216
211
) -> Result < Vec < ( BlindedPayInfo , BlindedPath ) > , ( ) > ;
217
212
}
218
213
0 commit comments