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

Commit b3a4a99

Browse files
committed
Added events and channel_manager
1 parent f38897d commit b3a4a99

File tree

4 files changed

+167
-2
lines changed

4 files changed

+167
-2
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
use std::collections::HashMap;
2+
use std::convert::TryInto;
3+
use std::ops::Deref;
4+
use std::sync::{Arc, Mutex, RwLock};
5+
6+
use bitcoin::secp256k1::PublicKey;
7+
use lightning::ln::msgs::{
8+
ChannelMessageHandler, ErrorAction, LightningError, OnionMessageHandler, RoutingMessageHandler,
9+
};
10+
use lightning::ln::peer_handler::{
11+
APeerManager, CustomMessageHandler, PeerManager, SocketDescriptor,
12+
};
13+
use lightning::routing::gossip::NetworkGraph;
14+
use lightning::sign::{EntropySource, NodeSigner};
15+
use lightning::util::logger::{Level, Logger};
16+
17+
use crate::transport::msgs::LSPSMessage;
18+
19+
const SUPPORTED_SPEC_VERSION: u16 = 1;
20+
21+
struct LiquidityChannel {
22+
id: u128,
23+
user_id: u128,
24+
state: ChannelState,
25+
api_version: u16,
26+
lsp_balance_sat: u64,
27+
client_balance_sat: u64,
28+
confirms_within_blocks: u32,
29+
channel_expiry_blocks: u32,
30+
token: String,
31+
created_at: String,
32+
expires_at: String,
33+
counterparty_node_id: PublicKey,
34+
scid: Option<u64>,
35+
}
36+
37+
enum ChannelState{
38+
InfoRequested,
39+
PendingOrder,
40+
OrderRequested,
41+
PendingPayment,
42+
Ready,
43+
}
44+
45+
46+
struct ChannelRequest {
47+
48+
}
49+
50+
pub struct LiquidityManager<
51+
ES: Deref,
52+
Descriptor: SocketDescriptor + Send + Sync + 'static,
53+
L: Deref + Send + Sync + 'static,
54+
RM: Deref + Send + Sync + 'static,
55+
CM: Deref + Send + Sync + 'static,
56+
OM: Deref + Send + Sync + 'static,
57+
CMH: Deref + Send + Sync + 'static,
58+
NS: Deref + Send + Sync + 'static,
59+
> where
60+
ES::Target: EntropySource,
61+
L::Target: Logger,
62+
RM::Target: RoutingMessageHandler,
63+
CM::Target: ChannelMessageHandler,
64+
OM::Target: OnionMessageHandler,
65+
CMH::Target: CustomMessageHandler,
66+
NS::Target: NodeSigner,
67+
{
68+
entropy_source: ES,
69+
peer_manager: Mutex<Option<Arc<PeerManager<Descriptor, CM, RM, OM, L, CMH, NS>>>>,
70+
pending_messages: Arc<Mutex<Vec<(PublicKey, LSPSMessage)>>>,
71+
pending_events: Arc<Mutex<Vec<Event>>>,
72+
//per_peer_state: RwLock<HashMap<PublicKey, Mutex<PeerState>>>,
73+
channels_by_scid: RwLock<HashMap<u64, LiquidityChannel>>,
74+
}
75+
76+
77+
impl<
78+
ES: Deref,
79+
Descriptor: SocketDescriptor + Send + Sync + 'static,
80+
L: Deref + Send + Sync + 'static,
81+
RM: Deref + Send + Sync + 'static,
82+
CM: Deref + Send + Sync + 'static,
83+
OM: Deref + Send + Sync + 'static,
84+
CMH: Deref + Send + Sync + 'static,
85+
NS: Deref + Send + Sync + 'static,
86+
> LiquidityManager <ES, Descriptor, L, RM, CM, OM, CMH, NS>
87+
where
88+
ES::Target: EntropySource,
89+
L::Target: Logger,
90+
RM::Target: RoutingMessageHandler,
91+
CM::Target: ChannelMessageHandler,
92+
OM::Target: OnionMessageHandler,
93+
CMH::Target: CustomMessageHandler,
94+
NS::Target: NodeSigner,
95+
{
96+
97+
fn handle_get_info_request() {}
98+
99+
fn handle_get_info_response() {}
100+
101+
fn liquidity_source_selected() {}
102+
103+
fn generate_order() {}
104+
105+
fn handle_create_order_request() {}
106+
107+
fn handle_create_order_response() {}
108+
109+
fn handle_create_order_error() {}
110+
111+
fn handle_get_order_request() {}
112+
113+
fn handle_get_order_response() {}
114+
115+
fn payment_for_channel() {}
116+
117+
fn onchain_payment() {}
118+
119+
fn lighnting_payment() {}
120+
121+
fn check_payment_status() {}
122+
123+
fn update_payment_status() {}
124+
125+
fn channel_ready() {}
126+
127+
fn update_order_status() {}
128+
129+
fn channel_error() {}
130+
131+
}

src/channel_request/event.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
3+
#[derive(Clone, Debug, PartialEq, Eq)]
4+
pub enum Event {
5+
// Request from the client for server info.
6+
GetInfo {},
7+
8+
// Information from the LSP regarding fees and channel parameters.
9+
GetInfoResponse {},
10+
11+
// Channel opening request from the client after selecting the LSP with desired parameters.
12+
// Client selects the fee and channel parameters and requests the LSP to create a channel.
13+
CreateChannel {},
14+
15+
// LSP accepts the request parameters and sends an onchain address and invoice along with channel
16+
// parameters to the client.
17+
Order {},
18+
19+
// Client confirms the parameters and pays the LSP through onchain or lightning payment.
20+
PaymentforChannel {},
21+
22+
// Waits for the payment to confirm and till then the order status can be retrived.
23+
GetOrderStatus {},
24+
25+
// Checks whether the payment is confirmed and updates the order.
26+
ConfirmPayment {},
27+
28+
// On payment confirmation, channel is opened.
29+
OpenChannel {},
30+
31+
// If order fails, refund is initiated.
32+
Refund {},
33+
}

src/channel_request/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@
88
// licenses.
99

1010
//! Types and primitives that implement the LSPS1: Channel Request specification.
11+
pub mod channel_manager;
12+
pub mod event;
1113
pub mod msgs;

src/channel_request/msgs.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ pub struct GetOrderRequest {
158158

159159
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
160160
pub struct GetOrderResponse {
161-
pub response: OrderState,
161+
pub response: Order,
162162
}
163163

164164
#[derive(Clone, Debug, PartialEq, Eq)]
@@ -188,7 +188,6 @@ pub enum Response {
188188
GetOrderError(ResponseError),
189189
}
190190

191-
192191
#[derive(Clone, Debug, PartialEq, Eq)]
193192
pub enum Message {
194193
Request(RequestId, Request),

0 commit comments

Comments
 (0)