|
| 1 | +use crate::transport::msgs::{LSPSMessage, RawLSPSMessage, LSPS_MESSAGE_TYPE}; |
| 2 | +use crate::transport::protocol::LSPS0MessageHandler; |
| 3 | + |
| 4 | +use bitcoin::secp256k1::PublicKey; |
| 5 | +use lightning::ln::features::{InitFeatures, NodeFeatures}; |
| 6 | +use lightning::ln::msgs::{ErrorAction, LightningError}; |
| 7 | +use lightning::ln::peer_handler::CustomMessageHandler; |
| 8 | +use lightning::ln::wire::CustomMessageReader; |
| 9 | +use lightning::sign::EntropySource; |
| 10 | +use lightning::util::logger::Level; |
| 11 | +use lightning::util::ser::Readable; |
| 12 | +use std::collections::HashMap; |
| 13 | +use std::convert::TryFrom; |
| 14 | +use std::io; |
| 15 | +use std::ops::Deref; |
| 16 | +use std::sync::{Arc, Mutex}; |
| 17 | + |
| 18 | +const LSPS_FEATURE_BIT: usize = 729; |
| 19 | + |
| 20 | +/// A trait used to implement a specific LSPS protocol. |
| 21 | +/// |
| 22 | +/// The messages the protocol uses need to be able to be mapped |
| 23 | +/// from and into [`LSPSMessage`]. |
| 24 | +pub(crate) trait ProtocolMessageHandler { |
| 25 | + type ProtocolMessage: TryFrom<LSPSMessage> + Into<LSPSMessage>; |
| 26 | + const PROTOCOL_NUMBER: Option<u16>; |
| 27 | + |
| 28 | + fn handle_message( |
| 29 | + &self, message: Self::ProtocolMessage, counterparty_node_id: &PublicKey, |
| 30 | + ) -> Result<(), LightningError>; |
| 31 | +} |
| 32 | + |
| 33 | +/// A configuration for [`LiquidityManager`]. |
| 34 | +/// |
| 35 | +/// Allows end-user to configure options when using the [`LiquidityManager`] |
| 36 | +/// to provide liquidity services to clients. |
| 37 | +pub struct LiquidityProviderConfig; |
| 38 | + |
| 39 | +/// The main interface into LSP functionality. |
| 40 | +/// |
| 41 | +/// Should be used as a [`CustomMessageHandler`] for your |
| 42 | +/// [`lightning::ln::peer_handler::PeerManager`]'s [`lightning::ln::peer_handler::MessageHandler`]. |
| 43 | +pub struct LiquidityManager<ES: Deref> |
| 44 | +where |
| 45 | + ES::Target: EntropySource, |
| 46 | +{ |
| 47 | + pending_messages: Arc<Mutex<Vec<(PublicKey, LSPSMessage)>>>, |
| 48 | + request_id_to_method_map: Mutex<HashMap<String, String>>, |
| 49 | + lsps0_message_handler: LSPS0MessageHandler<ES>, |
| 50 | + provider_config: Option<LiquidityProviderConfig>, |
| 51 | +} |
| 52 | + |
| 53 | +impl<ES: Deref> LiquidityManager<ES> |
| 54 | +where |
| 55 | + ES::Target: EntropySource, |
| 56 | +{ |
| 57 | + /// Constructor for the LiquidityManager |
| 58 | + /// |
| 59 | + /// Sets up the required protocol message handlers based on the given [`LiquidityProviderConfig`]. |
| 60 | + pub fn new(entropy_source: ES, provider_config: Option<LiquidityProviderConfig>) -> Self { |
| 61 | + let pending_messages = Arc::new(Mutex::new(vec![])); |
| 62 | + |
| 63 | + let lsps0_message_handler = |
| 64 | + LSPS0MessageHandler::new(entropy_source, vec![], Arc::clone(&pending_messages)); |
| 65 | + |
| 66 | + Self { |
| 67 | + pending_messages, |
| 68 | + request_id_to_method_map: Mutex::new(HashMap::new()), |
| 69 | + lsps0_message_handler, |
| 70 | + provider_config, |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + fn handle_lsps_message( |
| 75 | + &self, msg: LSPSMessage, sender_node_id: &PublicKey, |
| 76 | + ) -> Result<(), lightning::ln::msgs::LightningError> { |
| 77 | + match msg { |
| 78 | + LSPSMessage::Invalid => { |
| 79 | + return Err(LightningError { err: format!("{} did not understand a message we previously sent, maybe they don't support a protocol we are trying to use?", sender_node_id), action: ErrorAction::IgnoreAndLog(Level::Error)}); |
| 80 | + } |
| 81 | + LSPSMessage::LSPS0(msg) => { |
| 82 | + self.lsps0_message_handler.handle_message(msg, sender_node_id)?; |
| 83 | + } |
| 84 | + } |
| 85 | + Ok(()) |
| 86 | + } |
| 87 | + |
| 88 | + fn enqueue_message(&self, node_id: PublicKey, msg: LSPSMessage) { |
| 89 | + let mut pending_msgs = self.pending_messages.lock().unwrap(); |
| 90 | + pending_msgs.push((node_id, msg)); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +impl<ES: Deref> CustomMessageReader for LiquidityManager<ES> |
| 95 | +where |
| 96 | + ES::Target: EntropySource, |
| 97 | +{ |
| 98 | + type CustomMessage = RawLSPSMessage; |
| 99 | + |
| 100 | + fn read<R: io::Read>( |
| 101 | + &self, message_type: u16, buffer: &mut R, |
| 102 | + ) -> Result<Option<Self::CustomMessage>, lightning::ln::msgs::DecodeError> { |
| 103 | + match message_type { |
| 104 | + LSPS_MESSAGE_TYPE => Ok(Some(RawLSPSMessage::read(buffer)?)), |
| 105 | + _ => Ok(None), |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +impl<ES: Deref> CustomMessageHandler for LiquidityManager<ES> |
| 111 | +where |
| 112 | + ES::Target: EntropySource, |
| 113 | +{ |
| 114 | + fn handle_custom_message( |
| 115 | + &self, msg: Self::CustomMessage, sender_node_id: &PublicKey, |
| 116 | + ) -> Result<(), lightning::ln::msgs::LightningError> { |
| 117 | + let mut request_id_to_method_map = self.request_id_to_method_map.lock().unwrap(); |
| 118 | + |
| 119 | + match LSPSMessage::from_str_with_id_map(&msg.payload, &mut request_id_to_method_map) { |
| 120 | + Ok(msg) => self.handle_lsps_message(msg, sender_node_id), |
| 121 | + Err(_) => { |
| 122 | + self.enqueue_message(*sender_node_id, LSPSMessage::Invalid); |
| 123 | + Ok(()) |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + fn get_and_clear_pending_msg(&self) -> Vec<(PublicKey, Self::CustomMessage)> { |
| 129 | + let mut request_id_to_method_map = self.request_id_to_method_map.lock().unwrap(); |
| 130 | + self.pending_messages |
| 131 | + .lock() |
| 132 | + .unwrap() |
| 133 | + .drain(..) |
| 134 | + .map(|(public_key, lsps_message)| { |
| 135 | + if let Some((request_id, method_name)) = lsps_message.get_request_id_and_method() { |
| 136 | + request_id_to_method_map.insert(request_id, method_name); |
| 137 | + } |
| 138 | + ( |
| 139 | + public_key, |
| 140 | + RawLSPSMessage { payload: serde_json::to_string(&lsps_message).unwrap() }, |
| 141 | + ) |
| 142 | + }) |
| 143 | + .collect() |
| 144 | + } |
| 145 | + |
| 146 | + fn provided_node_features(&self) -> NodeFeatures { |
| 147 | + let mut features = NodeFeatures::empty(); |
| 148 | + |
| 149 | + if self.provider_config.is_some() { |
| 150 | + features.set_optional_custom_bit(LSPS_FEATURE_BIT).unwrap(); |
| 151 | + } |
| 152 | + |
| 153 | + features |
| 154 | + } |
| 155 | + |
| 156 | + fn provided_init_features(&self, _their_node_id: &PublicKey) -> InitFeatures { |
| 157 | + let mut features = InitFeatures::empty(); |
| 158 | + |
| 159 | + if self.provider_config.is_some() { |
| 160 | + features.set_optional_custom_bit(LSPS_FEATURE_BIT).unwrap(); |
| 161 | + } |
| 162 | + |
| 163 | + features |
| 164 | + } |
| 165 | +} |
0 commit comments