-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
237 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
use crate::types::{LiquidityManager, PeerManager}; | ||
|
||
use lightning::ln::features::{InitFeatures, NodeFeatures}; | ||
use lightning::ln::peer_handler::CustomMessageHandler; | ||
use lightning::ln::wire::CustomMessageReader; | ||
use lightning::util::persist::KVStore; | ||
use lightning_liquidity::events::Event; | ||
use lightning_liquidity::lsps0::msgs::RawLSPSMessage; | ||
use lightning_liquidity::lsps2::event::LSPS2ClientEvent; | ||
|
||
use bitcoin::secp256k1::PublicKey; | ||
|
||
use std::sync::Arc; | ||
|
||
pub(crate) enum LiquiditySource<K: KVStore + Sync + Send + 'static> { | ||
None, | ||
LSPS2 { | ||
liquidity_manager: Arc<LiquidityManager<K>>, | ||
service_url: String, | ||
service_node_id: PublicKey, | ||
token: String, | ||
}, | ||
} | ||
|
||
impl<K: KVStore + Sync + Send> LiquiditySource<K> { | ||
pub(crate) fn new_lsps2( | ||
liquidity_manager: Arc<LiquidityManager<K>>, service_url: String, | ||
service_node_id: PublicKey, token: String, | ||
) -> Self { | ||
Self::LSPS2 { liquidity_manager, service_url, service_node_id, token } | ||
} | ||
|
||
pub(crate) fn new_none() -> Self { | ||
Self::None | ||
} | ||
|
||
pub(crate) fn set_peer_manager(&self, peer_manager: Arc<PeerManager<K>>) { | ||
match self { | ||
Self::LSPS2 { liquidity_manager, .. } => { | ||
let process_msgs_callback = move || peer_manager.process_events(); | ||
liquidity_manager.set_process_msgs_callback(process_msgs_callback); | ||
} | ||
Self::None => {} | ||
} | ||
} | ||
|
||
pub(crate) async fn handle_next_event(&self) { | ||
match self { | ||
Self::LSPS2 { liquidity_manager, service_url, service_node_id, token } => { | ||
match liquidity_manager.next_event_async().await { | ||
Event::LSPS2Client(LSPS2ClientEvent::GetInfoResponse { | ||
jit_channel_id: _, | ||
counterparty_node_id: _, | ||
opening_fee_params_menu: _, | ||
min_payment_size_msat: _, | ||
max_payment_size_msat: _, | ||
user_channel_id: _, | ||
}) => {} | ||
Event::LSPS2Client(LSPS2ClientEvent::InvoiceGenerationReady { | ||
counterparty_node_id: _, | ||
scid: _, | ||
cltv_expiry_delta: _, | ||
payment_size_msat: _, | ||
client_trusts_lsp: _, | ||
user_channel_id: _, | ||
}) => {} | ||
_ => {} | ||
} | ||
} | ||
Self::None => {} | ||
} | ||
} | ||
} | ||
|
||
impl<K: KVStore + Sync + Send + 'static> CustomMessageReader for LiquiditySource<K> { | ||
type CustomMessage = RawLSPSMessage; | ||
|
||
fn read<RD: lightning::io::Read>( | ||
&self, message_type: u16, buffer: &mut RD, | ||
) -> Result<Option<Self::CustomMessage>, lightning::ln::msgs::DecodeError> { | ||
match self { | ||
Self::LSPS2 { liquidity_manager, .. } => liquidity_manager.read(message_type, buffer), | ||
Self::None => Ok(None), | ||
} | ||
} | ||
} | ||
|
||
impl<K: KVStore + Sync + Send + 'static> CustomMessageHandler for LiquiditySource<K> { | ||
fn handle_custom_message( | ||
&self, msg: Self::CustomMessage, sender_node_id: &PublicKey, | ||
) -> Result<(), lightning::ln::msgs::LightningError> { | ||
match self { | ||
Self::LSPS2 { liquidity_manager, .. } => { | ||
liquidity_manager.handle_custom_message(msg, sender_node_id) | ||
} | ||
Self::None => Ok(()), // Should be unreachable!() as the reader will return `None` | ||
} | ||
} | ||
|
||
fn get_and_clear_pending_msg(&self) -> Vec<(PublicKey, Self::CustomMessage)> { | ||
match self { | ||
Self::LSPS2 { liquidity_manager, .. } => liquidity_manager.get_and_clear_pending_msg(), | ||
Self::None => Vec::new(), | ||
} | ||
} | ||
|
||
fn provided_node_features(&self) -> NodeFeatures { | ||
match self { | ||
Self::LSPS2 { liquidity_manager, .. } => liquidity_manager.provided_node_features(), | ||
Self::None => NodeFeatures::empty(), | ||
} | ||
} | ||
|
||
fn provided_init_features(&self, their_node_id: &PublicKey) -> InitFeatures { | ||
match self { | ||
Self::LSPS2 { liquidity_manager, .. } => { | ||
liquidity_manager.provided_init_features(their_node_id) | ||
} | ||
Self::None => InitFeatures::empty(), | ||
} | ||
} | ||
} |
Oops, something went wrong.