Skip to content

Commit

Permalink
Added lsps1 message handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Srg213 committed Jul 27, 2023
1 parent 6f5c631 commit 18b66fb
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 164 deletions.
145 changes: 79 additions & 66 deletions src/channel_request/channel_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use lightning::util::errors::APIError;
use lightning::util::logger::{Level, Logger};

use crate::channel_request::msgs::{CreateOrderRequest, Message, Order, Request};
use crate::channel_request::utils::check_if_valid;
use crate::transport::message_handler::ProtocolMessageHandler;
use crate::transport::msgs::{LSPSMessage, RequestId};
use crate::utils;
Expand Down Expand Up @@ -64,12 +65,12 @@ struct CRChannel {
pub struct CRManager<
ES: Deref,
Descriptor: SocketDescriptor + Send + Sync + 'static,
L: Deref + Send + Sync + 'static,
RM: Deref + Send + Sync + 'static,
CM: Deref + Send + Sync + 'static,
OM: Deref + Send + Sync + 'static,
CMH: Deref + Send + Sync + 'static,
NS: Deref + Send + Sync + 'static,
L: Deref,
RM: Deref,
CM: Deref,
OM: Deref,
CMH: Deref,
NS: Deref,
> where
ES::Target: EntropySource,
L::Target: Logger,
Expand Down Expand Up @@ -157,12 +158,16 @@ where
}

fn handle_get_info_request(
&self, request_id: RequestId, counterparty_node_id: PublicKey, request: GetInfoRequest
) -> Result<(), LightningError> {
&self, request_id: RequestId, counterparty_node_id: PublicKey, request: GetInfoRequest,
) -> Result<(), LightningError> {
self.enqueue_response(
counterparty_node_id,
request_id,
Response::GetInfo(GetInfoResponse { supported_versions: vec![1], website: request.website, options: request.options }),
Response::GetInfo(GetInfoResponse {
supported_versions: vec![1],
website: request.website,
options: request.options,
}),
);
Ok(())
}
Expand Down Expand Up @@ -238,17 +243,19 @@ where
Some(peer_state_mutex) => {
// TODO- For optimization; one variable is set to immuatable reference for pattern matching,
// other variable is mutable to change it.
let state = & *peer_state_mutex.lock().unwrap();
let state = &*peer_state_mutex.lock().unwrap();
let peer_state = &mut *peer_state_mutex.lock().unwrap();


match state {
CRState::InfoRequested { request_id, counterparty_node_id } => {
CRState::InfoRequested { request_id, counterparty_node_id } => {
// Should the request_id change for a new request message?
// or should it be passed to different state of enum
if request_id == &req_id {
// TODO: optimization- remove the clone
*peer_state = CRState::OrderRequested { request_id: request_id.clone(), order: order.clone() };
*peer_state = CRState::OrderRequested {
request_id: request_id.clone(),
order: order.clone(),
};

{
let mut pending_messages = self.pending_messages.lock().unwrap();
Expand Down Expand Up @@ -292,50 +299,53 @@ where
// Again by client in create order response.
// To implement the error codes.
fn is_valid_order(
request_id: RequestId ,order: &Order, options: &OptionsSupported,
&self, request_id: RequestId ,order: &Order, options: &OptionsSupported, counterparty_node_id: &PublicKey
) -> Result<(), LightningError> {
let per_peer_state = self.per_peer_state.read().unwrap();
let mut per_peer_state = self.per_new_peer_state.read().unwrap();
match per_peer_state.get(&counterparty_node_id) {
Some(peer_state_mutex) => {
let peer_state = peer_state_mutex.lock().unwrap();
let mut peer_state = peer_state_mutex.lock().unwrap();
if let Some(channel) = peer_state.channels_by_id.get_mut(&channel_id) {
if channel.state == ChannelState::OrderRequested {
match &*peer_state {
CRState::InfoRequested { request_id, counterparty_node_id } => {
check_if_valid(
order.lsp_balance_sat.into(),
options.max_initial_lsp_balance_sat.into(),
options.min_initial_lsp_balance_sat.into(),
order.lsp_balance_sat.into(),
options.max_initial_lsp_balance_sat.into(),
options.min_initial_lsp_balance_sat.into(),
);
check_if_valid(
order.client_balance_sat.into(),
options.max_initial_client_balance_sat.into(),
options.min_initial_client_balance_sat.into(),
order.client_balance_sat.into(),
options.max_initial_client_balance_sat.into(),
options.min_initial_client_balance_sat.into(),
);
check_if_valid(
order.channel_expiry_blocks.into(),
options.max_channel_expiry_blocks.into(),
0,
order.channel_expiry_blocks.into(),
options.max_channel_expiry_blocks.into(),
0,
);
} else if channel.state == ChannelState::PendingPayment {
}
}
}
&CRState::OrderRequested { request_id, order } => {
}
}
}
None => {
}
None => {}
}
// Check the condition for supported version
// Check the condition for supported version
// Other measures to implement
// The client MUST check if option_support_large_channel is enabled
// before they order a channel larger than 16,777,216 satoshi.
// Other measures to implement
// The client MUST check if option_support_large_channel is enabled
// before they order a channel larger than 16,777,216 satoshi.
// Token validity to check.
// Token validity to check.
Ok(())
}
*/

// Enqueue the event of CreateChannel as LSP would need to calculate the fees.
fn handle_create_order_request(
&self, request_id: RequestId, counterparty_node_id: &PublicKey, request: CreateOrderRequest,
Expand Down Expand Up @@ -412,10 +422,9 @@ where
let per_peer_state = self.per_new_peer_state.read().unwrap();
match per_peer_state.get(counterparty_node_id) {
Some(peer_state_mutex) => {
let state = & *peer_state_mutex.lock().unwrap();
let state = &*peer_state_mutex.lock().unwrap();
let peer_state = &mut *peer_state_mutex.lock().unwrap();


match state {
CRState::OrderRequested { request_id, order } => {
if request_id == &requestid {
Expand Down Expand Up @@ -507,7 +516,7 @@ where
} else {
// err, high fees
// abort and remove the CRState associated with this requestid

// Call an Event to abort the flow
}
}
Expand Down Expand Up @@ -536,21 +545,28 @@ where
let per_peer_state = self.per_new_peer_state.read().unwrap();
match per_peer_state.get(&counterparty_node_id) {
Some(peer_state_mutex) => {
let state = & *peer_state_mutex.lock().unwrap();
let state = &*peer_state_mutex.lock().unwrap();
let peer_state = &mut *peer_state_mutex.lock().unwrap();


match state {
CRState::PendingSelection { request_id, order_id, order } => {
if request_id == &requestid {
let res = response.clone();

*peer_state = CRState::PendingPayment {
request_id: request_id.clone(),
order_id: response.order_id,
payment: response.payment,
order: response.order,
};

self.enqueue_event(Event::LSPS1(super::event::Event::PayforChannel {
request_id: request_id.clone(),
counterparty_node_id: *counterparty_node_id,
order: response.order,
payment: response.payment,
channel: response.channel,
}));
}
}
_ => {
Expand Down Expand Up @@ -614,15 +630,18 @@ where
let per_peer_state = self.per_new_peer_state.read().unwrap();
match per_peer_state.get(&counterparty_node_id) {
Some(peer_state_mutex) => {
let state = & *peer_state_mutex.lock().unwrap();
let state = &*peer_state_mutex.lock().unwrap();
let peer_state = &mut *peer_state_mutex.lock().unwrap();

match state {
CRState::PendingPayment { request_id, order_id, order, payment } => {
if order_id == &orderid {
let request_id = self.generate_request_id();
*peer_state =
CRState::PendingConfirmation { request_id: request_id.clone(), order_id: order_id.clone(), invoice: payment.clone() };
*peer_state = CRState::PendingConfirmation {
request_id: request_id.clone(),
order_id: order_id.clone(),
invoice: payment.clone(),
};

{
let mut pending_messages = self.pending_messages.lock().unwrap();
Expand Down Expand Up @@ -664,7 +683,7 @@ where
fn handle_get_order_request(
&self, request_id: RequestId, counterparty_node_id: &PublicKey, request: GetOrderRequest,
) -> Result<(), APIError> {
let mut pending_orders = self.pending_orders.read().unwrap();
let mut pending_orders = self.pending_orders.read().unwrap();

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

match pending_orders.get(&request_id){
Some(order_id) => {
self.enqueue_response(
counterparty_node_id,
request_id,
Response::GetOrder(GetOrderResponse { response: params.order_id }),
)
}
None => {

}
match pending_orders.get(&request_id) {
Some(order_id) => self.enqueue_response(
counterparty_node_id,
request_id,
Response::GetOrder(GetOrderResponse { response: params.order_id }),
),
None => {}
}
Ok(())
}

// Just to show the client about the status, no event or change in state
fn handle_get_order_response(&self, request_id: RequestId, counterparty_node_id: &PublicKey, response: GetOrderResponse)
-> Result<(), LightningError> {

/* // Just to show the client about the status, no event or change in state
fn handle_get_order_response(
&self, request_id: RequestId, counterparty_node_id: &PublicKey, response: GetOrderResponse,
) -> Result<(), LightningError> {
// Check for different conditions
// If payment is confirmed or refund is initiated
Ok(())
}
/*
} */
/*
fn channel_ready() {
todo!()
}
Expand Down Expand Up @@ -759,7 +774,6 @@ where
}
}


impl<
ES: Deref,
Descriptor: SocketDescriptor + Send + Sync + 'static,
Expand Down Expand Up @@ -818,7 +832,6 @@ where
}
}


// Order of functions called
// new
// set peer
Expand Down
80 changes: 38 additions & 42 deletions src/channel_request/event.rs
Original file line number Diff line number Diff line change
@@ -1,66 +1,62 @@
use bitcoin::secp256k1::PublicKey;

use super::msgs::{ChannelInfo, OptionsSupported, Order, OrderId, Payment};
use crate::transport::msgs::RequestId;
use super::msgs::{OptionsSupported, ChannelInfo, Order, Payment, OrderId};

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Event {

// Information from the LSP regarding fees and channel parameters.
// The client should validate if the LSP's parameters supported match
// requirements.
// used by LSP to provide options supported by it to client
GetInfoResponse {
request_id: RequestId,

// Information from the LSP regarding fees and channel parameters.
// The client should validate if the LSP's parameters supported match
// requirements.
// used by LSP to provide options supported by it to client
GetInfoResponse {
request_id: RequestId,

/// The node id of the LSP that provided this response.
counterparty_node_id: PublicKey,

version: Vec<u16>,

website: String,
version: Vec<u16>,

options_supported: OptionsSupported
},
website: String,

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

CreateInvoice {
request_id: RequestId,
// Channel opening request from the client after selecting the LSP with desired parameters.
// Client selects the fee and channel parameters and requests the LSP to create a channel.
// LSP should check the validity of the create channel order.
CreateInvoice {
request_id: RequestId,

counterparty_node_id: PublicKey,

order: Order,
order: Order,

order_id: OrderId,
},
order_id: OrderId,
},

// LSP accepts the request parameters and sends an onchain address and invoice along with channel
// parameters to the client. After payment by the client this event should be updated,
// to show the LSP to poll for the payment now.
PayforChaPennnel {

request_id: RequestId,
// LSP accepts the request parameters and sends an onchain address and invoice along with channel
// parameters to the client. After payment by the client this event should be updated,
// to show the LSP to poll for the payment now.
PayforChaPennnel {
request_id: RequestId,
counterparty_node_id: PublicKey,
order: Order,
payment: Payment,
channel: Option<ChannelInfo>,

},
},

CheckPaymentConfirmation {
request_id: RequestId,
counterparty_node_id: PublicKey,
order_id: OrderId,
},
CheckPaymentConfirmation {
request_id: RequestId,
counterparty_node_id: PublicKey,
order_id: OrderId,
},

// On payment confirmation, channel is opened. After payment confirms,
// LSP should open a channel and open to client.
OpenChannel {},
// On payment confirmation, channel is opened. After payment confirms,
// LSP should open a channel and open to client.
OpenChannel {},

// If order fails, refund is initiated.
//
Refund {},
}
// If order fails, refund is initiated.
//
Refund {},
}

0 comments on commit 18b66fb

Please sign in to comment.