Skip to content

Commit cae7fa5

Browse files
committed
Move PeerState and related types to peer_state.rs module
We move the `PeerState` related types to a new module. In the following commits we'll bit-by-bit drop the `pub(super)`s introduced here, asserting better separation of state and logic going forward.
1 parent 24219ba commit cae7fa5

File tree

3 files changed

+87
-64
lines changed

3 files changed

+87
-64
lines changed

lightning-liquidity/src/lsps1/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ pub mod client;
1313
pub mod event;
1414
pub mod msgs;
1515
#[cfg(lsps1_service)]
16+
mod peer_state;
17+
#[cfg(lsps1_service)]
1618
pub mod service;
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// This file is Copyright its original authors, visible in version control
2+
// history.
3+
//
4+
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5+
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7+
// You may not use this file except in accordance with one or both of these
8+
// licenses.
9+
10+
//! Contains peer state objects that are used by `LSPS1ServiceHandler`.
11+
12+
use super::msgs::{LSPS1OrderId, LSPS1OrderParams, LSPS1PaymentInfo, LSPS1Request};
13+
14+
use crate::lsps0::ser::{LSPSDateTime, LSPSRequestId};
15+
use crate::prelude::HashMap;
16+
17+
use lightning::ln::msgs::{ErrorAction, LightningError};
18+
use lightning::util::logger::Level;
19+
20+
#[derive(Default)]
21+
pub(super) struct PeerState {
22+
pub(super) outbound_channels_by_order_id: HashMap<LSPS1OrderId, OutboundCRChannel>,
23+
pub(super) pending_requests: HashMap<LSPSRequestId, LSPS1Request>,
24+
}
25+
26+
impl PeerState {
27+
pub(super) fn insert_outbound_channel(
28+
&mut self, order_id: LSPS1OrderId, channel: OutboundCRChannel,
29+
) {
30+
self.outbound_channels_by_order_id.insert(order_id, channel);
31+
}
32+
}
33+
34+
struct ChannelStateError(String);
35+
36+
impl From<ChannelStateError> for LightningError {
37+
fn from(value: ChannelStateError) -> Self {
38+
LightningError { err: value.0, action: ErrorAction::IgnoreAndLog(Level::Info) }
39+
}
40+
}
41+
42+
#[derive(PartialEq, Debug)]
43+
pub(super) enum OutboundRequestState {
44+
OrderCreated { order_id: LSPS1OrderId },
45+
WaitingPayment { order_id: LSPS1OrderId },
46+
}
47+
48+
impl OutboundRequestState {
49+
fn awaiting_payment(&self) -> Result<Self, ChannelStateError> {
50+
match self {
51+
OutboundRequestState::OrderCreated { order_id } => {
52+
Ok(OutboundRequestState::WaitingPayment { order_id: order_id.clone() })
53+
},
54+
state => Err(ChannelStateError(format!("TODO. JIT Channel was in state: {:?}", state))),
55+
}
56+
}
57+
}
58+
59+
pub(super) struct OutboundLSPS1Config {
60+
pub(super) order: LSPS1OrderParams,
61+
pub(super) created_at: LSPSDateTime,
62+
pub(super) payment: LSPS1PaymentInfo,
63+
}
64+
65+
pub(super) struct OutboundCRChannel {
66+
pub(super) state: OutboundRequestState,
67+
pub(super) config: OutboundLSPS1Config,
68+
}
69+
70+
impl OutboundCRChannel {
71+
pub(super) fn new(
72+
order: LSPS1OrderParams, created_at: LSPSDateTime, order_id: LSPS1OrderId,
73+
payment: LSPS1PaymentInfo,
74+
) -> Self {
75+
Self {
76+
state: OutboundRequestState::OrderCreated { order_id },
77+
config: OutboundLSPS1Config { order, created_at, payment },
78+
}
79+
}
80+
pub(super) fn awaiting_payment(&mut self) -> Result<(), LightningError> {
81+
self.state = self.state.awaiting_payment()?;
82+
Ok(())
83+
}
84+
}

lightning-liquidity/src/lsps1/service.rs

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use super::msgs::{
2020
LSPS1OrderState, LSPS1PaymentInfo, LSPS1Request, LSPS1Response,
2121
LSPS1_CREATE_ORDER_REQUEST_ORDER_MISMATCH_ERROR_CODE,
2222
};
23+
use super::peer_state::{OutboundCRChannel, PeerState};
2324
use crate::message_queue::MessageQueue;
2425

2526
use crate::events::EventQueue;
@@ -48,70 +49,6 @@ pub struct LSPS1ServiceConfig {
4849
pub supported_options: Option<LSPS1Options>,
4950
}
5051

51-
struct ChannelStateError(String);
52-
53-
impl From<ChannelStateError> for LightningError {
54-
fn from(value: ChannelStateError) -> Self {
55-
LightningError { err: value.0, action: ErrorAction::IgnoreAndLog(Level::Info) }
56-
}
57-
}
58-
59-
#[derive(PartialEq, Debug)]
60-
enum OutboundRequestState {
61-
OrderCreated { order_id: LSPS1OrderId },
62-
WaitingPayment { order_id: LSPS1OrderId },
63-
}
64-
65-
impl OutboundRequestState {
66-
fn awaiting_payment(&self) -> Result<Self, ChannelStateError> {
67-
match self {
68-
OutboundRequestState::OrderCreated { order_id } => {
69-
Ok(OutboundRequestState::WaitingPayment { order_id: order_id.clone() })
70-
},
71-
state => Err(ChannelStateError(format!("TODO. JIT Channel was in state: {:?}", state))),
72-
}
73-
}
74-
}
75-
76-
struct OutboundLSPS1Config {
77-
order: LSPS1OrderParams,
78-
created_at: LSPSDateTime,
79-
payment: LSPS1PaymentInfo,
80-
}
81-
82-
struct OutboundCRChannel {
83-
state: OutboundRequestState,
84-
config: OutboundLSPS1Config,
85-
}
86-
87-
impl OutboundCRChannel {
88-
fn new(
89-
order: LSPS1OrderParams, created_at: LSPSDateTime, order_id: LSPS1OrderId,
90-
payment: LSPS1PaymentInfo,
91-
) -> Self {
92-
Self {
93-
state: OutboundRequestState::OrderCreated { order_id },
94-
config: OutboundLSPS1Config { order, created_at, payment },
95-
}
96-
}
97-
fn awaiting_payment(&mut self) -> Result<(), LightningError> {
98-
self.state = self.state.awaiting_payment()?;
99-
Ok(())
100-
}
101-
}
102-
103-
#[derive(Default)]
104-
struct PeerState {
105-
outbound_channels_by_order_id: HashMap<LSPS1OrderId, OutboundCRChannel>,
106-
pending_requests: HashMap<LSPSRequestId, LSPS1Request>,
107-
}
108-
109-
impl PeerState {
110-
fn insert_outbound_channel(&mut self, order_id: LSPS1OrderId, channel: OutboundCRChannel) {
111-
self.outbound_channels_by_order_id.insert(order_id, channel);
112-
}
113-
}
114-
11552
/// The main object allowing to send and receive bLIP-51 / LSPS1 messages.
11653
pub struct LSPS1ServiceHandler<ES: Deref, CM: Deref + Clone, K: Deref + Clone>
11754
where

0 commit comments

Comments
 (0)