1212use super :: msgs:: {
1313 ChannelInfo , CreateOrderRequest , CreateOrderResponse , GetInfoRequest , GetInfoResponse ,
1414 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 ,
1617 LSPS1_CREATE_ORDER_REQUEST_ORDER_MISMATCH_ERROR_CODE ,
1718} ;
1819use super :: utils:: is_valid;
@@ -40,10 +41,15 @@ use core::ops::Deref;
4041
4142const SUPPORTED_SPEC_VERSIONS : [ u16 ; 1 ] = [ 1 ] ;
4243
44+ /// Configuration options for LSPS1 channel requests.
4345pub struct LSPS1Config {
46+ /// A token to be send with each channel request.
4447 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.
4651 pub options_supported : Option < OptionsSupported > ,
52+ /// The LSP's website.
4753 pub website : Option < String > ,
4854}
4955
@@ -59,7 +65,7 @@ impl From<ChannelStateError> for LightningError {
5965enum InboundRequestState {
6066 InfoRequested ,
6167 OptionsSupport { version : u16 , options_supported : OptionsSupported } ,
62- OrderRequested { version : u16 , order : Order } ,
68+ OrderRequested { version : u16 , order : OrderParams } ,
6369 PendingPayment { order_id : OrderId } ,
6470 AwaitingConfirmation { id : u128 , order_id : OrderId } ,
6571}
@@ -90,7 +96,7 @@ impl InboundRequestState {
9096 }
9197 }
9298
93- pub fn order_requested ( & self , order : Order ) -> Result < Self , ChannelStateError > {
99+ fn order_requested ( & self , order : OrderParams ) -> Result < Self , ChannelStateError > {
94100 match self {
95101 InboundRequestState :: OptionsSupport { version, options_supported } => {
96102 if is_valid ( & order, options_supported) {
@@ -109,8 +115,8 @@ impl InboundRequestState {
109115 }
110116 }
111117
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 ,
114120 ) -> Result < Self , ChannelStateError > {
115121 match self {
116122 InboundRequestState :: OrderRequested { version, order } => {
@@ -130,7 +136,7 @@ impl InboundRequestState {
130136 }
131137 }
132138
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 > {
134140 match self {
135141 InboundRequestState :: PendingPayment { order_id } => {
136142 Ok ( InboundRequestState :: AwaitingConfirmation {
@@ -152,11 +158,11 @@ struct InboundCRChannel {
152158}
153159
154160impl InboundCRChannel {
155- pub fn new ( id : u128 ) -> Self {
161+ fn new ( id : u128 ) -> Self {
156162 Self { id, state : InboundRequestState :: InfoRequested }
157163 }
158164
159- pub fn info_received (
165+ fn info_received (
160166 & mut self , versions : Vec < u16 > , options : OptionsSupported ,
161167 ) -> Result < u16 , LightningError > {
162168 self . state = self . state . info_received ( versions, options) ?;
@@ -170,7 +176,7 @@ impl InboundCRChannel {
170176 }
171177 }
172178
173- pub fn order_requested ( & mut self , order : Order ) -> Result < u16 , LightningError > {
179+ fn order_requested ( & mut self , order : OrderParams ) -> Result < u16 , LightningError > {
174180 self . state = self . state . order_requested ( order) ?;
175181
176182 match self . state {
@@ -184,14 +190,14 @@ impl InboundCRChannel {
184190 }
185191 }
186192
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 ,
189195 ) -> Result < ( ) , LightningError > {
190196 self . state = self . state . order_received ( order, order_id) ?;
191197 Ok ( ( ) )
192198 }
193199
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 > {
195201 self . state = self . state . pay_for_channel ( channel_id) ?;
196202 Ok ( ( ) )
197203 }
@@ -205,7 +211,7 @@ enum OutboundRequestState {
205211}
206212
207213impl OutboundRequestState {
208- pub fn create_payment_invoice ( & self ) -> Result < Self , ChannelStateError > {
214+ fn create_payment_invoice ( & self ) -> Result < Self , ChannelStateError > {
209215 match self {
210216 OutboundRequestState :: OrderCreated { order_id } => {
211217 Ok ( OutboundRequestState :: WaitingPayment { order_id : order_id. clone ( ) } )
@@ -219,10 +225,10 @@ impl OutboundRequestState {
219225}
220226
221227struct OutboundLSPS1Config {
222- order : Order ,
228+ order : OrderParams ,
223229 created_at : chrono:: DateTime < Utc > ,
224230 expires_at : chrono:: DateTime < Utc > ,
225- payment : Payment ,
231+ payment : OrderPayment ,
226232}
227233
228234struct OutboundCRChannel {
@@ -231,21 +237,21 @@ struct OutboundCRChannel {
231237}
232238
233239impl 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 ,
237243 ) -> Self {
238244 Self {
239245 state : OutboundRequestState :: OrderCreated { order_id } ,
240246 config : OutboundLSPS1Config { order, created_at, expires_at, payment } ,
241247 }
242248 }
243- pub fn create_payment_invoice ( & mut self ) -> Result < ( ) , LightningError > {
249+ fn create_payment_invoice ( & mut self ) -> Result < ( ) , LightningError > {
244250 self . state = self . state . create_payment_invoice ( ) ?;
245251 Ok ( ( ) )
246252 }
247253
248- pub fn check_order_validity ( & self , options_supported : & OptionsSupported ) -> bool {
254+ fn check_order_validity ( & self , options_supported : & OptionsSupported ) -> bool {
249255 let order = & self . config . order ;
250256
251257 is_valid ( order, options_supported)
@@ -261,27 +267,28 @@ struct PeerState {
261267}
262268
263269impl PeerState {
264- pub fn insert_inbound_channel ( & mut self , id : u128 , channel : InboundCRChannel ) {
270+ fn insert_inbound_channel ( & mut self , id : u128 , channel : InboundCRChannel ) {
265271 self . inbound_channels_by_id . insert ( id, channel) ;
266272 }
267273
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 ) {
269275 self . outbound_channels_by_order_id . insert ( order_id, channel) ;
270276 }
271277
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 ) {
273279 self . request_to_cid . insert ( request_id, channel_id) ;
274280 }
275281
276- pub fn remove_inbound_channel ( & mut self , id : u128 ) {
282+ fn remove_inbound_channel ( & mut self , id : u128 ) {
277283 self . inbound_channels_by_id . remove ( & id) ;
278284 }
279285
280- pub fn remove_outbound_channel ( & mut self , order_id : OrderId ) {
286+ fn remove_outbound_channel ( & mut self , order_id : OrderId ) {
281287 self . outbound_channels_by_order_id . remove ( & order_id) ;
282288 }
283289}
284290
291+ /// The main object allowing to send and receive LSPS1 messages.
285292pub struct LSPS1MessageHandler < ES : Deref , CM : Deref + Clone , PM : Deref + Clone , C : Deref >
286293where
287294 ES :: Target : EntropySource ,
@@ -298,7 +305,7 @@ where
298305 per_peer_state : RwLock < HashMap < PublicKey , Mutex < PeerState > > > ,
299306 options_config : Option < OptionsSupported > ,
300307 website : Option < String > ,
301- max_fees : Option < u64 > ,
308+ max_channel_fees_msat : Option < u64 > ,
302309}
303310
304311impl < ES : Deref , CM : Deref + Clone , PM : Deref + Clone , C : Deref > LSPS1MessageHandler < ES , CM , PM , C >
@@ -324,15 +331,25 @@ where
324331 per_peer_state : RwLock :: new ( HashMap :: new ( ) ) ,
325332 options_config : config. options_supported . clone ( ) ,
326333 website : config. website . clone ( ) ,
327- max_fees : config. max_fees ,
334+ max_channel_fees_msat : config. max_channel_fees_msat ,
328335 }
329336 }
330337
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
331348 pub fn set_peer_manager ( & self , peer_manager : PM ) {
332349 * self . peer_manager . lock ( ) . unwrap ( ) = Some ( peer_manager) ;
333350 }
334351
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 ) {
336353 let channel = InboundCRChannel :: new ( channel_id) ;
337354
338355 let mut outer_state_lock = self . per_peer_state . write ( ) . unwrap ( ) ;
@@ -439,8 +456,8 @@ where
439456 Ok ( ( ) )
440457 }
441458
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 ,
444461 ) -> Result < ( ) , APIError > {
445462 let outer_state_lock = self . per_peer_state . write ( ) . unwrap ( ) ;
446463
@@ -548,8 +565,8 @@ where
548565 Ok ( ( ) )
549566 }
550567
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 ,
553570 created_at : chrono:: DateTime < Utc > , expires_at : chrono:: DateTime < Utc > ,
554571 ) -> Result < ( ) , APIError > {
555572 let outer_state_lock = self . per_peer_state . read ( ) . unwrap ( ) ;
@@ -643,9 +660,11 @@ where
643660 }
644661
645662 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 ) ;
647664
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+ {
649668 self . enqueue_event ( Event :: LSPS1 ( super :: event:: Event :: DisplayOrder {
650669 id : channel_id,
651670 counterparty_node_id : * counterparty_node_id,
@@ -710,7 +729,7 @@ where
710729 }
711730 }
712731
713- pub fn check_order_status (
732+ fn check_order_status (
714733 & self , counterparty_node_id : & PublicKey , order_id : OrderId , channel_id : u128 ,
715734 ) -> Result < ( ) , APIError > {
716735 let outer_state_lock = self . per_peer_state . write ( ) . unwrap ( ) ;
@@ -811,7 +830,7 @@ where
811830 Ok ( ( ) )
812831 }
813832
814- pub fn update_order_status (
833+ fn update_order_status (
815834 & self , request_id : RequestId , counterparty_node_id : PublicKey , order_id : OrderId ,
816835 order_state : OrderState , channel : Option < ChannelInfo > ,
817836 ) -> Result < ( ) , APIError > {
0 commit comments