Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit 18b66fb

Browse files
committed
Added lsps1 message handler
1 parent 6f5c631 commit 18b66fb

File tree

9 files changed

+221
-164
lines changed

9 files changed

+221
-164
lines changed

src/channel_request/channel_manager.rs

+79-66
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use lightning::util::errors::APIError;
1515
use lightning::util::logger::{Level, Logger};
1616

1717
use crate::channel_request::msgs::{CreateOrderRequest, Message, Order, Request};
18+
use crate::channel_request::utils::check_if_valid;
1819
use crate::transport::message_handler::ProtocolMessageHandler;
1920
use crate::transport::msgs::{LSPSMessage, RequestId};
2021
use crate::utils;
@@ -64,12 +65,12 @@ struct CRChannel {
6465
pub struct CRManager<
6566
ES: Deref,
6667
Descriptor: SocketDescriptor + Send + Sync + 'static,
67-
L: Deref + Send + Sync + 'static,
68-
RM: Deref + Send + Sync + 'static,
69-
CM: Deref + Send + Sync + 'static,
70-
OM: Deref + Send + Sync + 'static,
71-
CMH: Deref + Send + Sync + 'static,
72-
NS: Deref + Send + Sync + 'static,
68+
L: Deref,
69+
RM: Deref,
70+
CM: Deref,
71+
OM: Deref,
72+
CMH: Deref,
73+
NS: Deref,
7374
> where
7475
ES::Target: EntropySource,
7576
L::Target: Logger,
@@ -157,12 +158,16 @@ where
157158
}
158159

159160
fn handle_get_info_request(
160-
&self, request_id: RequestId, counterparty_node_id: PublicKey, request: GetInfoRequest
161-
) -> Result<(), LightningError> {
161+
&self, request_id: RequestId, counterparty_node_id: PublicKey, request: GetInfoRequest,
162+
) -> Result<(), LightningError> {
162163
self.enqueue_response(
163164
counterparty_node_id,
164165
request_id,
165-
Response::GetInfo(GetInfoResponse { supported_versions: vec![1], website: request.website, options: request.options }),
166+
Response::GetInfo(GetInfoResponse {
167+
supported_versions: vec![1],
168+
website: request.website,
169+
options: request.options,
170+
}),
166171
);
167172
Ok(())
168173
}
@@ -238,17 +243,19 @@ where
238243
Some(peer_state_mutex) => {
239244
// TODO- For optimization; one variable is set to immuatable reference for pattern matching,
240245
// other variable is mutable to change it.
241-
let state = & *peer_state_mutex.lock().unwrap();
246+
let state = &*peer_state_mutex.lock().unwrap();
242247
let peer_state = &mut *peer_state_mutex.lock().unwrap();
243248

244-
245249
match state {
246-
CRState::InfoRequested { request_id, counterparty_node_id } => {
250+
CRState::InfoRequested { request_id, counterparty_node_id } => {
247251
// Should the request_id change for a new request message?
248252
// or should it be passed to different state of enum
249253
if request_id == &req_id {
250254
// TODO: optimization- remove the clone
251-
*peer_state = CRState::OrderRequested { request_id: request_id.clone(), order: order.clone() };
255+
*peer_state = CRState::OrderRequested {
256+
request_id: request_id.clone(),
257+
order: order.clone(),
258+
};
252259

253260
{
254261
let mut pending_messages = self.pending_messages.lock().unwrap();
@@ -292,50 +299,53 @@ where
292299
// Again by client in create order response.
293300
// To implement the error codes.
294301
fn is_valid_order(
295-
request_id: RequestId ,order: &Order, options: &OptionsSupported,
302+
&self, request_id: RequestId ,order: &Order, options: &OptionsSupported, counterparty_node_id: &PublicKey
296303
) -> Result<(), LightningError> {
297-
let per_peer_state = self.per_peer_state.read().unwrap();
304+
let mut per_peer_state = self.per_new_peer_state.read().unwrap();
298305
match per_peer_state.get(&counterparty_node_id) {
299306
Some(peer_state_mutex) => {
300-
let peer_state = peer_state_mutex.lock().unwrap();
307+
let mut peer_state = peer_state_mutex.lock().unwrap();
301308
302-
if let Some(channel) = peer_state.channels_by_id.get_mut(&channel_id) {
303-
if channel.state == ChannelState::OrderRequested {
309+
match &*peer_state {
310+
CRState::InfoRequested { request_id, counterparty_node_id } => {
304311
check_if_valid(
305-
order.lsp_balance_sat.into(),
306-
options.max_initial_lsp_balance_sat.into(),
307-
options.min_initial_lsp_balance_sat.into(),
312+
order.lsp_balance_sat.into(),
313+
options.max_initial_lsp_balance_sat.into(),
314+
options.min_initial_lsp_balance_sat.into(),
308315
);
309316
310317
check_if_valid(
311-
order.client_balance_sat.into(),
312-
options.max_initial_client_balance_sat.into(),
313-
options.min_initial_client_balance_sat.into(),
318+
order.client_balance_sat.into(),
319+
options.max_initial_client_balance_sat.into(),
320+
options.min_initial_client_balance_sat.into(),
314321
);
315322
316323
check_if_valid(
317-
order.channel_expiry_blocks.into(),
318-
options.max_channel_expiry_blocks.into(),
319-
0,
324+
order.channel_expiry_blocks.into(),
325+
options.max_channel_expiry_blocks.into(),
326+
0,
320327
);
321-
} else if channel.state == ChannelState::PendingPayment {
322-
}
323-
}
328+
}
329+
&CRState::OrderRequested { request_id, order } => {
330+
331+
}
332+
}
333+
}
334+
None => {
335+
324336
}
325-
None => {}
326337
}
327-
// Check the condition for supported version
338+
// Check the condition for supported version
328339
329-
// Other measures to implement
330-
// The client MUST check if option_support_large_channel is enabled
331-
// before they order a channel larger than 16,777,216 satoshi.
340+
// Other measures to implement
341+
// The client MUST check if option_support_large_channel is enabled
342+
// before they order a channel larger than 16,777,216 satoshi.
332343
333-
// Token validity to check.
344+
// Token validity to check.
334345
335346
Ok(())
336347
}
337348
*/
338-
339349
// Enqueue the event of CreateChannel as LSP would need to calculate the fees.
340350
fn handle_create_order_request(
341351
&self, request_id: RequestId, counterparty_node_id: &PublicKey, request: CreateOrderRequest,
@@ -412,10 +422,9 @@ where
412422
let per_peer_state = self.per_new_peer_state.read().unwrap();
413423
match per_peer_state.get(counterparty_node_id) {
414424
Some(peer_state_mutex) => {
415-
let state = & *peer_state_mutex.lock().unwrap();
425+
let state = &*peer_state_mutex.lock().unwrap();
416426
let peer_state = &mut *peer_state_mutex.lock().unwrap();
417427

418-
419428
match state {
420429
CRState::OrderRequested { request_id, order } => {
421430
if request_id == &requestid {
@@ -507,7 +516,7 @@ where
507516
} else {
508517
// err, high fees
509518
// abort and remove the CRState associated with this requestid
510-
519+
511520
// Call an Event to abort the flow
512521
}
513522
}
@@ -536,21 +545,28 @@ where
536545
let per_peer_state = self.per_new_peer_state.read().unwrap();
537546
match per_peer_state.get(&counterparty_node_id) {
538547
Some(peer_state_mutex) => {
539-
let state = & *peer_state_mutex.lock().unwrap();
548+
let state = &*peer_state_mutex.lock().unwrap();
540549
let peer_state = &mut *peer_state_mutex.lock().unwrap();
541550

542-
543551
match state {
544552
CRState::PendingSelection { request_id, order_id, order } => {
545553
if request_id == &requestid {
546554
let res = response.clone();
547-
555+
548556
*peer_state = CRState::PendingPayment {
549557
request_id: request_id.clone(),
550558
order_id: response.order_id,
551559
payment: response.payment,
552560
order: response.order,
553561
};
562+
563+
self.enqueue_event(Event::LSPS1(super::event::Event::PayforChannel {
564+
request_id: request_id.clone(),
565+
counterparty_node_id: *counterparty_node_id,
566+
order: response.order,
567+
payment: response.payment,
568+
channel: response.channel,
569+
}));
554570
}
555571
}
556572
_ => {
@@ -614,15 +630,18 @@ where
614630
let per_peer_state = self.per_new_peer_state.read().unwrap();
615631
match per_peer_state.get(&counterparty_node_id) {
616632
Some(peer_state_mutex) => {
617-
let state = & *peer_state_mutex.lock().unwrap();
633+
let state = &*peer_state_mutex.lock().unwrap();
618634
let peer_state = &mut *peer_state_mutex.lock().unwrap();
619635

620636
match state {
621637
CRState::PendingPayment { request_id, order_id, order, payment } => {
622638
if order_id == &orderid {
623639
let request_id = self.generate_request_id();
624-
*peer_state =
625-
CRState::PendingConfirmation { request_id: request_id.clone(), order_id: order_id.clone(), invoice: payment.clone() };
640+
*peer_state = CRState::PendingConfirmation {
641+
request_id: request_id.clone(),
642+
order_id: order_id.clone(),
643+
invoice: payment.clone(),
644+
};
626645

627646
{
628647
let mut pending_messages = self.pending_messages.lock().unwrap();
@@ -664,7 +683,7 @@ where
664683
fn handle_get_order_request(
665684
&self, request_id: RequestId, counterparty_node_id: &PublicKey, request: GetOrderRequest,
666685
) -> Result<(), APIError> {
667-
let mut pending_orders = self.pending_orders.read().unwrap();
686+
let mut pending_orders = self.pending_orders.read().unwrap();
668687

669688
match pending_orders.get(&request_id) {
670689
Some(orderid) => {
@@ -688,30 +707,26 @@ where
688707
) -> Result<(), APIError> {
689708
let mut pending_orders = self.pending_orders.read().unwrap();
690709

691-
match pending_orders.get(&request_id){
692-
Some(order_id) => {
693-
self.enqueue_response(
694-
counterparty_node_id,
695-
request_id,
696-
Response::GetOrder(GetOrderResponse { response: params.order_id }),
697-
)
698-
}
699-
None => {
700-
701-
}
710+
match pending_orders.get(&request_id) {
711+
Some(order_id) => self.enqueue_response(
712+
counterparty_node_id,
713+
request_id,
714+
Response::GetOrder(GetOrderResponse { response: params.order_id }),
715+
),
716+
None => {}
702717
}
703718
Ok(())
704719
}
705720

706-
// Just to show the client about the status, no event or change in state
707-
fn handle_get_order_response(&self, request_id: RequestId, counterparty_node_id: &PublicKey, response: GetOrderResponse)
708-
-> Result<(), LightningError> {
709-
721+
/* // Just to show the client about the status, no event or change in state
722+
fn handle_get_order_response(
723+
&self, request_id: RequestId, counterparty_node_id: &PublicKey, response: GetOrderResponse,
724+
) -> Result<(), LightningError> {
710725
// Check for different conditions
711726
// If payment is confirmed or refund is initiated
712727
Ok(())
713-
}
714-
/*
728+
} */
729+
/*
715730
fn channel_ready() {
716731
todo!()
717732
}
@@ -759,7 +774,6 @@ where
759774
}
760775
}
761776

762-
763777
impl<
764778
ES: Deref,
765779
Descriptor: SocketDescriptor + Send + Sync + 'static,
@@ -818,7 +832,6 @@ where
818832
}
819833
}
820834

821-
822835
// Order of functions called
823836
// new
824837
// set peer

src/channel_request/event.rs

+38-42
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,62 @@
11
use bitcoin::secp256k1::PublicKey;
22

3+
use super::msgs::{ChannelInfo, OptionsSupported, Order, OrderId, Payment};
34
use crate::transport::msgs::RequestId;
4-
use super::msgs::{OptionsSupported, ChannelInfo, Order, Payment, OrderId};
55

66
#[derive(Clone, Debug, PartialEq, Eq)]
77
pub enum Event {
8-
9-
// Information from the LSP regarding fees and channel parameters.
10-
// The client should validate if the LSP's parameters supported match
11-
// requirements.
12-
// used by LSP to provide options supported by it to client
13-
GetInfoResponse {
14-
request_id: RequestId,
15-
8+
// Information from the LSP regarding fees and channel parameters.
9+
// The client should validate if the LSP's parameters supported match
10+
// requirements.
11+
// used by LSP to provide options supported by it to client
12+
GetInfoResponse {
13+
request_id: RequestId,
14+
1615
/// The node id of the LSP that provided this response.
1716
counterparty_node_id: PublicKey,
1817

19-
version: Vec<u16>,
20-
21-
website: String,
18+
version: Vec<u16>,
2219

23-
options_supported: OptionsSupported
24-
},
20+
website: String,
2521

26-
// Channel opening request from the client after selecting the LSP with desired parameters.
27-
// Client selects the fee and channel parameters and requests the LSP to create a channel.
28-
// LSP should check the validity of the create channel order.
22+
options_supported: OptionsSupported,
23+
},
2924

30-
CreateInvoice {
31-
request_id: RequestId,
25+
// Channel opening request from the client after selecting the LSP with desired parameters.
26+
// Client selects the fee and channel parameters and requests the LSP to create a channel.
27+
// LSP should check the validity of the create channel order.
28+
CreateInvoice {
29+
request_id: RequestId,
3230

3331
counterparty_node_id: PublicKey,
3432

35-
order: Order,
33+
order: Order,
3634

37-
order_id: OrderId,
38-
},
35+
order_id: OrderId,
36+
},
3937

40-
// LSP accepts the request parameters and sends an onchain address and invoice along with channel
41-
// parameters to the client. After payment by the client this event should be updated,
42-
// to show the LSP to poll for the payment now.
43-
PayforChaPennnel {
44-
45-
request_id: RequestId,
38+
// LSP accepts the request parameters and sends an onchain address and invoice along with channel
39+
// parameters to the client. After payment by the client this event should be updated,
40+
// to show the LSP to poll for the payment now.
41+
PayforChaPennnel {
42+
request_id: RequestId,
4643
counterparty_node_id: PublicKey,
4744
order: Order,
4845
payment: Payment,
4946
channel: Option<ChannelInfo>,
50-
51-
},
47+
},
5248

53-
CheckPaymentConfirmation {
54-
request_id: RequestId,
55-
counterparty_node_id: PublicKey,
56-
order_id: OrderId,
57-
},
49+
CheckPaymentConfirmation {
50+
request_id: RequestId,
51+
counterparty_node_id: PublicKey,
52+
order_id: OrderId,
53+
},
5854

59-
// On payment confirmation, channel is opened. After payment confirms,
60-
// LSP should open a channel and open to client.
61-
OpenChannel {},
55+
// On payment confirmation, channel is opened. After payment confirms,
56+
// LSP should open a channel and open to client.
57+
OpenChannel {},
6258

63-
// If order fails, refund is initiated.
64-
//
65-
Refund {},
66-
}
59+
// If order fails, refund is initiated.
60+
//
61+
Refund {},
62+
}

0 commit comments

Comments
 (0)