12
12
use super :: msgs:: {
13
13
ChannelInfo , CreateOrderRequest , CreateOrderResponse , GetInfoRequest , GetInfoResponse ,
14
14
GetOrderRequest , GetOrderResponse , LSPS1Message , LSPS1Request , LSPS1Response , OptionsSupported ,
15
- Order , OrderId , OrderState , Payment , LSPS1_CREATE_ORDER_REQUEST_INVALID_VERSION_ERROR_CODE ,
15
+ OrderId , OrderParams , OrderPayment , OrderState ,
16
+ LSPS1_CREATE_ORDER_REQUEST_INVALID_VERSION_ERROR_CODE ,
16
17
LSPS1_CREATE_ORDER_REQUEST_ORDER_MISMATCH_ERROR_CODE ,
17
18
} ;
18
19
use super :: utils:: is_valid;
@@ -40,10 +41,15 @@ use core::ops::Deref;
40
41
41
42
const SUPPORTED_SPEC_VERSIONS : [ u16 ; 1 ] = [ 1 ] ;
42
43
44
+ /// Configuration options for LSPS1 channel requests.
43
45
pub struct LSPS1Config {
46
+ /// A token to be send with each channel request.
44
47
pub token : Option < String > ,
45
- pub max_fees : Option < u64 > ,
48
+ /// The maximally allowed channel fees.
49
+ pub max_channel_fees_msat : Option < u64 > ,
50
+ /// The options supported by the LSP.
46
51
pub options_supported : Option < OptionsSupported > ,
52
+ /// The LSP's website.
47
53
pub website : Option < String > ,
48
54
}
49
55
@@ -59,7 +65,7 @@ impl From<ChannelStateError> for LightningError {
59
65
enum InboundRequestState {
60
66
InfoRequested ,
61
67
OptionsSupport { version : u16 , options_supported : OptionsSupported } ,
62
- OrderRequested { version : u16 , order : Order } ,
68
+ OrderRequested { version : u16 , order : OrderParams } ,
63
69
PendingPayment { order_id : OrderId } ,
64
70
AwaitingConfirmation { id : u128 , order_id : OrderId } ,
65
71
}
@@ -90,7 +96,7 @@ impl InboundRequestState {
90
96
}
91
97
}
92
98
93
- pub fn order_requested ( & self , order : Order ) -> Result < Self , ChannelStateError > {
99
+ fn order_requested ( & self , order : OrderParams ) -> Result < Self , ChannelStateError > {
94
100
match self {
95
101
InboundRequestState :: OptionsSupport { version, options_supported } => {
96
102
if is_valid ( & order, options_supported) {
@@ -109,8 +115,8 @@ impl InboundRequestState {
109
115
}
110
116
}
111
117
112
- pub fn order_received (
113
- & self , response_order : & Order , order_id : OrderId ,
118
+ fn order_received (
119
+ & self , response_order : & OrderParams , order_id : OrderId ,
114
120
) -> Result < Self , ChannelStateError > {
115
121
match self {
116
122
InboundRequestState :: OrderRequested { version, order } => {
@@ -130,7 +136,7 @@ impl InboundRequestState {
130
136
}
131
137
}
132
138
133
- pub fn pay_for_channel ( & self , channel_id : u128 ) -> Result < Self , ChannelStateError > {
139
+ fn pay_for_channel ( & self , channel_id : u128 ) -> Result < Self , ChannelStateError > {
134
140
match self {
135
141
InboundRequestState :: PendingPayment { order_id } => {
136
142
Ok ( InboundRequestState :: AwaitingConfirmation {
@@ -152,11 +158,11 @@ struct InboundCRChannel {
152
158
}
153
159
154
160
impl InboundCRChannel {
155
- pub fn new ( id : u128 ) -> Self {
161
+ fn new ( id : u128 ) -> Self {
156
162
Self { id, state : InboundRequestState :: InfoRequested }
157
163
}
158
164
159
- pub fn info_received (
165
+ fn info_received (
160
166
& mut self , versions : Vec < u16 > , options : OptionsSupported ,
161
167
) -> Result < u16 , LightningError > {
162
168
self . state = self . state . info_received ( versions, options) ?;
@@ -170,7 +176,7 @@ impl InboundCRChannel {
170
176
}
171
177
}
172
178
173
- pub fn order_requested ( & mut self , order : Order ) -> Result < u16 , LightningError > {
179
+ fn order_requested ( & mut self , order : OrderParams ) -> Result < u16 , LightningError > {
174
180
self . state = self . state . order_requested ( order) ?;
175
181
176
182
match self . state {
@@ -184,14 +190,14 @@ impl InboundCRChannel {
184
190
}
185
191
}
186
192
187
- pub fn order_received (
188
- & mut self , order : & Order , order_id : OrderId ,
193
+ fn order_received (
194
+ & mut self , order : & OrderParams , order_id : OrderId ,
189
195
) -> Result < ( ) , LightningError > {
190
196
self . state = self . state . order_received ( order, order_id) ?;
191
197
Ok ( ( ) )
192
198
}
193
199
194
- pub fn pay_for_channel ( & mut self , channel_id : u128 ) -> Result < ( ) , LightningError > {
200
+ fn pay_for_channel ( & mut self , channel_id : u128 ) -> Result < ( ) , LightningError > {
195
201
self . state = self . state . pay_for_channel ( channel_id) ?;
196
202
Ok ( ( ) )
197
203
}
@@ -205,7 +211,7 @@ enum OutboundRequestState {
205
211
}
206
212
207
213
impl OutboundRequestState {
208
- pub fn create_payment_invoice ( & self ) -> Result < Self , ChannelStateError > {
214
+ fn create_payment_invoice ( & self ) -> Result < Self , ChannelStateError > {
209
215
match self {
210
216
OutboundRequestState :: OrderCreated { order_id } => {
211
217
Ok ( OutboundRequestState :: WaitingPayment { order_id : order_id. clone ( ) } )
@@ -219,10 +225,10 @@ impl OutboundRequestState {
219
225
}
220
226
221
227
struct OutboundLSPS1Config {
222
- order : Order ,
228
+ order : OrderParams ,
223
229
created_at : chrono:: DateTime < Utc > ,
224
230
expires_at : chrono:: DateTime < Utc > ,
225
- payment : Payment ,
231
+ payment : OrderPayment ,
226
232
}
227
233
228
234
struct OutboundCRChannel {
@@ -231,21 +237,21 @@ struct OutboundCRChannel {
231
237
}
232
238
233
239
impl OutboundCRChannel {
234
- pub fn new (
235
- order : Order , created_at : chrono:: DateTime < Utc > , expires_at : chrono:: DateTime < Utc > ,
236
- order_id : OrderId , payment : Payment ,
240
+ fn new (
241
+ order : OrderParams , created_at : chrono:: DateTime < Utc > , expires_at : chrono:: DateTime < Utc > ,
242
+ order_id : OrderId , payment : OrderPayment ,
237
243
) -> Self {
238
244
Self {
239
245
state : OutboundRequestState :: OrderCreated { order_id } ,
240
246
config : OutboundLSPS1Config { order, created_at, expires_at, payment } ,
241
247
}
242
248
}
243
- pub fn create_payment_invoice ( & mut self ) -> Result < ( ) , LightningError > {
249
+ fn create_payment_invoice ( & mut self ) -> Result < ( ) , LightningError > {
244
250
self . state = self . state . create_payment_invoice ( ) ?;
245
251
Ok ( ( ) )
246
252
}
247
253
248
- pub fn check_order_validity ( & self , options_supported : & OptionsSupported ) -> bool {
254
+ fn check_order_validity ( & self , options_supported : & OptionsSupported ) -> bool {
249
255
let order = & self . config . order ;
250
256
251
257
is_valid ( order, options_supported)
@@ -261,27 +267,28 @@ struct PeerState {
261
267
}
262
268
263
269
impl PeerState {
264
- pub fn insert_inbound_channel ( & mut self , id : u128 , channel : InboundCRChannel ) {
270
+ fn insert_inbound_channel ( & mut self , id : u128 , channel : InboundCRChannel ) {
265
271
self . inbound_channels_by_id . insert ( id, channel) ;
266
272
}
267
273
268
- pub fn insert_outbound_channel ( & mut self , order_id : OrderId , channel : OutboundCRChannel ) {
274
+ fn insert_outbound_channel ( & mut self , order_id : OrderId , channel : OutboundCRChannel ) {
269
275
self . outbound_channels_by_order_id . insert ( order_id, channel) ;
270
276
}
271
277
272
- pub fn insert_request ( & mut self , request_id : RequestId , channel_id : u128 ) {
278
+ fn insert_request ( & mut self , request_id : RequestId , channel_id : u128 ) {
273
279
self . request_to_cid . insert ( request_id, channel_id) ;
274
280
}
275
281
276
- pub fn remove_inbound_channel ( & mut self , id : u128 ) {
282
+ fn remove_inbound_channel ( & mut self , id : u128 ) {
277
283
self . inbound_channels_by_id . remove ( & id) ;
278
284
}
279
285
280
- pub fn remove_outbound_channel ( & mut self , order_id : OrderId ) {
286
+ fn remove_outbound_channel ( & mut self , order_id : OrderId ) {
281
287
self . outbound_channels_by_order_id . remove ( & order_id) ;
282
288
}
283
289
}
284
290
291
+ /// The main object allowing to send and receive LSPS1 messages.
285
292
pub struct LSPS1MessageHandler < ES : Deref , CM : Deref + Clone , PM : Deref + Clone , C : Deref >
286
293
where
287
294
ES :: Target : EntropySource ,
@@ -298,7 +305,7 @@ where
298
305
per_peer_state : RwLock < HashMap < PublicKey , Mutex < PeerState > > > ,
299
306
options_config : Option < OptionsSupported > ,
300
307
website : Option < String > ,
301
- max_fees : Option < u64 > ,
308
+ max_channel_fees_msat : Option < u64 > ,
302
309
}
303
310
304
311
impl < ES : Deref , CM : Deref + Clone , PM : Deref + Clone , C : Deref > LSPS1MessageHandler < ES , CM , PM , C >
@@ -324,15 +331,25 @@ where
324
331
per_peer_state : RwLock :: new ( HashMap :: new ( ) ) ,
325
332
options_config : config. options_supported . clone ( ) ,
326
333
website : config. website . clone ( ) ,
327
- max_fees : config. max_fees ,
334
+ max_channel_fees_msat : config. max_channel_fees_msat ,
328
335
}
329
336
}
330
337
338
+ /// Set a [`PeerManager`] reference for the message handler.
339
+ ///
340
+ /// This allows the message handler to wake the [`PeerManager`] by calling
341
+ /// [`PeerManager::process_events`] after enqueing messages to be sent.
342
+ ///
343
+ /// Without this the messages will be sent based on whatever polling interval
344
+ /// your background processor uses.
345
+ ///
346
+ /// [`PeerManager`]: lightning::ln::peer_handler::PeerManager
347
+ /// [`PeerManager::process_events`]: lightning::ln::peer_handler::PeerManager::process_events
331
348
pub fn set_peer_manager ( & self , peer_manager : PM ) {
332
349
* self . peer_manager . lock ( ) . unwrap ( ) = Some ( peer_manager) ;
333
350
}
334
351
335
- pub fn request_for_info ( & self , counterparty_node_id : PublicKey , channel_id : u128 ) {
352
+ fn request_for_info ( & self , counterparty_node_id : PublicKey , channel_id : u128 ) {
336
353
let channel = InboundCRChannel :: new ( channel_id) ;
337
354
338
355
let mut outer_state_lock = self . per_peer_state . write ( ) . unwrap ( ) ;
@@ -439,8 +456,8 @@ where
439
456
Ok ( ( ) )
440
457
}
441
458
442
- pub fn place_order (
443
- & self , channel_id : u128 , counterparty_node_id : & PublicKey , order : Order ,
459
+ fn place_order (
460
+ & self , channel_id : u128 , counterparty_node_id : & PublicKey , order : OrderParams ,
444
461
) -> Result < ( ) , APIError > {
445
462
let outer_state_lock = self . per_peer_state . write ( ) . unwrap ( ) ;
446
463
@@ -548,8 +565,8 @@ where
548
565
Ok ( ( ) )
549
566
}
550
567
551
- pub fn send_invoice_for_order (
552
- & self , request_id : RequestId , counterparty_node_id : & PublicKey , payment : Payment ,
568
+ fn send_invoice_for_order (
569
+ & self , request_id : RequestId , counterparty_node_id : & PublicKey , payment : OrderPayment ,
553
570
created_at : chrono:: DateTime < Utc > , expires_at : chrono:: DateTime < Utc > ,
554
571
) -> Result < ( ) , APIError > {
555
572
let outer_state_lock = self . per_peer_state . read ( ) . unwrap ( ) ;
@@ -643,9 +660,11 @@ where
643
660
}
644
661
645
662
let total_fees = response. payment . fee_total_sat + response. order . client_balance_sat ;
646
- let max_fees = self . max_fees . unwrap_or ( u64:: MAX ) ;
663
+ let max_channel_fees_msat = self . max_channel_fees_msat . unwrap_or ( u64:: MAX ) ;
647
664
648
- if total_fees == response. payment . order_total_sat && total_fees < max_fees {
665
+ if total_fees == response. payment . order_total_sat
666
+ && total_fees < max_channel_fees_msat
667
+ {
649
668
self . enqueue_event ( Event :: LSPS1 ( super :: event:: Event :: DisplayOrder {
650
669
id : channel_id,
651
670
counterparty_node_id : * counterparty_node_id,
@@ -710,7 +729,7 @@ where
710
729
}
711
730
}
712
731
713
- pub fn check_order_status (
732
+ fn check_order_status (
714
733
& self , counterparty_node_id : & PublicKey , order_id : OrderId , channel_id : u128 ,
715
734
) -> Result < ( ) , APIError > {
716
735
let outer_state_lock = self . per_peer_state . write ( ) . unwrap ( ) ;
@@ -811,7 +830,7 @@ where
811
830
Ok ( ( ) )
812
831
}
813
832
814
- pub fn update_order_status (
833
+ fn update_order_status (
815
834
& self , request_id : RequestId , counterparty_node_id : PublicKey , order_id : OrderId ,
816
835
order_state : OrderState , channel : Option < ChannelInfo > ,
817
836
) -> Result < ( ) , APIError > {
0 commit comments