diff --git a/lightning-liquidity/src/lsps0/client.rs b/lightning-liquidity/src/lsps0/client.rs index 56e7d3a5a..7b049e655 100644 --- a/lightning-liquidity/src/lsps0/client.rs +++ b/lightning-liquidity/src/lsps0/client.rs @@ -10,7 +10,7 @@ use crate::lsps0::msgs::{ LSPS0ListProtocolsRequest, LSPS0ListProtocolsResponse, LSPS0Message, LSPS0Request, LSPS0Response, }; -use crate::lsps0::ser::{ProtocolMessageHandler, ResponseError}; +use crate::lsps0::ser::{LSPSProtocolMessageHandler, LSPSResponseError}; use crate::message_queue::MessageQueue; use crate::sync::Arc; use crate::utils; @@ -69,20 +69,20 @@ where }); Ok(()) }, - LSPS0Response::ListProtocolsError(ResponseError { code, message, data, .. }) => { - Err(LightningError { - err: format!( - "ListProtocols error received. code = {}, message = {}, data = {:?}", - code, message, data - ), - action: ErrorAction::IgnoreAndLog(Level::Info), - }) - }, + LSPS0Response::ListProtocolsError(LSPSResponseError { + code, message, data, .. + }) => Err(LightningError { + err: format!( + "ListProtocols error received. code = {}, message = {}, data = {:?}", + code, message, data + ), + action: ErrorAction::IgnoreAndLog(Level::Info), + }), } } } -impl ProtocolMessageHandler for LSPS0ClientHandler +impl LSPSProtocolMessageHandler for LSPS0ClientHandler where ES::Target: EntropySource, { @@ -113,7 +113,7 @@ mod tests { use alloc::string::ToString; use alloc::sync::Arc; - use crate::lsps0::ser::{LSPSMessage, RequestId}; + use crate::lsps0::ser::{LSPSMessage, LSPSRequestId}; use crate::tests::utils::{self, TestEntropy}; use super::*; @@ -146,7 +146,7 @@ mod tests { assert_eq!( *message, LSPSMessage::LSPS0(LSPS0Message::Request( - RequestId("00000000000000000000000000000000".to_string()), + LSPSRequestId("00000000000000000000000000000000".to_string()), LSPS0Request::ListProtocols(LSPS0ListProtocolsRequest {}) )) ); diff --git a/lightning-liquidity/src/lsps0/msgs.rs b/lightning-liquidity/src/lsps0/msgs.rs index 5c522af91..91ec28ca1 100644 --- a/lightning-liquidity/src/lsps0/msgs.rs +++ b/lightning-liquidity/src/lsps0/msgs.rs @@ -1,6 +1,6 @@ //! Message, request, and other primitive types used to implement LSPS0. -use crate::lsps0::ser::{LSPSMessage, RequestId, ResponseError}; +use crate::lsps0::ser::{LSPSMessage, LSPSRequestId, LSPSResponseError}; use crate::prelude::Vec; use serde::{Deserialize, Serialize}; @@ -58,7 +58,7 @@ pub enum LSPS0Response { /// A response to a `list_protocols` request. ListProtocols(LSPS0ListProtocolsResponse), /// An error response to a `list_protocols` request. - ListProtocolsError(ResponseError), + ListProtocolsError(LSPSResponseError), } /// An bLIP-50 / LSPS0 protocol message. @@ -69,9 +69,9 @@ pub enum LSPS0Response { #[derive(Clone, Debug, PartialEq, Eq)] pub enum LSPS0Message { /// A request variant. - Request(RequestId, LSPS0Request), + Request(LSPSRequestId, LSPS0Request), /// A response variant. - Response(RequestId, LSPS0Response), + Response(LSPSRequestId, LSPS0Response), } impl TryFrom for LSPS0Message { @@ -117,7 +117,7 @@ mod tests { assert_eq!( msg, LSPSMessage::LSPS0(LSPS0Message::Request( - RequestId("request:id:xyz123".to_string()), + LSPSRequestId("request:id:xyz123".to_string()), LSPS0Request::ListProtocols(LSPS0ListProtocolsRequest {}) )) ); @@ -126,7 +126,7 @@ mod tests { #[test] fn serializes_request() { let request = LSPSMessage::LSPS0(LSPS0Message::Request( - RequestId("request:id:xyz123".to_string()), + LSPSRequestId("request:id:xyz123".to_string()), LSPS0Request::ListProtocols(LSPS0ListProtocolsRequest {}), )); let json = serde_json::to_string(&request).unwrap(); @@ -147,7 +147,7 @@ mod tests { }"#; let mut request_id_to_method_map = new_hash_map(); request_id_to_method_map - .insert(RequestId("request:id:xyz123".to_string()), LSPSMethod::LSPS0ListProtocols); + .insert(LSPSRequestId("request:id:xyz123".to_string()), LSPSMethod::LSPS0ListProtocols); let response = LSPSMessage::from_str_with_id_map(json, &mut request_id_to_method_map).unwrap(); @@ -155,7 +155,7 @@ mod tests { assert_eq!( response, LSPSMessage::LSPS0(LSPS0Message::Response( - RequestId("request:id:xyz123".to_string()), + LSPSRequestId("request:id:xyz123".to_string()), LSPS0Response::ListProtocols(LSPS0ListProtocolsResponse { protocols: vec![1, 2, 3] }) @@ -175,7 +175,7 @@ mod tests { }"#; let mut request_id_to_method_map = new_hash_map(); request_id_to_method_map - .insert(RequestId("request:id:xyz123".to_string()), LSPSMethod::LSPS0ListProtocols); + .insert(LSPSRequestId("request:id:xyz123".to_string()), LSPSMethod::LSPS0ListProtocols); let response = LSPSMessage::from_str_with_id_map(json, &mut request_id_to_method_map).unwrap(); @@ -183,8 +183,8 @@ mod tests { assert_eq!( response, LSPSMessage::LSPS0(LSPS0Message::Response( - RequestId("request:id:xyz123".to_string()), - LSPS0Response::ListProtocolsError(ResponseError { + LSPSRequestId("request:id:xyz123".to_string()), + LSPS0Response::ListProtocolsError(LSPSResponseError { code: -32617, message: "Unknown Error".to_string(), data: None @@ -204,7 +204,7 @@ mod tests { }"#; let mut request_id_to_method_map = new_hash_map(); request_id_to_method_map - .insert(RequestId("request:id:xyz123".to_string()), LSPSMethod::LSPS0ListProtocols); + .insert(LSPSRequestId("request:id:xyz123".to_string()), LSPSMethod::LSPS0ListProtocols); let response = LSPSMessage::from_str_with_id_map(json, &mut request_id_to_method_map); assert!(response.is_err()); @@ -213,7 +213,7 @@ mod tests { #[test] fn serializes_response() { let response = LSPSMessage::LSPS0(LSPS0Message::Response( - RequestId("request:id:xyz123".to_string()), + LSPSRequestId("request:id:xyz123".to_string()), LSPS0Response::ListProtocols(LSPS0ListProtocolsResponse { protocols: vec![1, 2, 3] }), )); let json = serde_json::to_string(&response).unwrap(); diff --git a/lightning-liquidity/src/lsps0/ser.rs b/lightning-liquidity/src/lsps0/ser.rs index 525226231..e97a01580 100644 --- a/lightning-liquidity/src/lsps0/ser.rs +++ b/lightning-liquidity/src/lsps0/ser.rs @@ -138,7 +138,7 @@ pub const LSPS_MESSAGE_TYPE_ID: u16 = 37913; /// /// The messages the protocol uses need to be able to be mapped /// from and into [`LSPSMessage`]. -pub(crate) trait ProtocolMessageHandler { +pub(crate) trait LSPSProtocolMessageHandler { type ProtocolMessage: TryFrom + Into; const PROTOCOL_NUMBER: Option; @@ -184,14 +184,14 @@ impl wire::Type for RawLSPSMessage { /// more information. #[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)] #[serde(transparent)] -pub struct RequestId(pub String); +pub struct LSPSRequestId(pub String); /// An error returned in response to an JSON-RPC request. /// /// Please refer to the [JSON-RPC 2.0 specification](https://www.jsonrpc.org/specification#error_object) for /// more information. #[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)] -pub struct ResponseError { +pub struct LSPSResponseError { /// A number that indicates the error type that occurred. pub code: i32, /// A string providing a short description of the error. @@ -204,7 +204,7 @@ pub struct ResponseError { #[derive(Clone, Debug, PartialEq, Eq)] pub enum LSPSMessage { /// An invalid variant. - Invalid(ResponseError), + Invalid(LSPSResponseError), /// An LSPS0 message. LSPS0(LSPS0Message), /// An LSPS1 message. @@ -219,7 +219,7 @@ impl LSPSMessage { /// The given `request_id_to_method` associates request ids with method names, as response objects /// don't carry the latter. pub(crate) fn from_str_with_id_map( - json_str: &str, request_id_to_method_map: &mut HashMap, + json_str: &str, request_id_to_method_map: &mut HashMap, ) -> Result { let deserializer = &mut serde_json::Deserializer::from_str(json_str); let visitor = LSPSMessageVisitor { request_id_to_method_map }; @@ -227,16 +227,16 @@ impl LSPSMessage { } /// Returns the request id and the method. - pub(crate) fn get_request_id_and_method(&self) -> Option<(RequestId, LSPSMethod)> { + pub(crate) fn get_request_id_and_method(&self) -> Option<(LSPSRequestId, LSPSMethod)> { match self { LSPSMessage::LSPS0(LSPS0Message::Request(request_id, request)) => { - Some((RequestId(request_id.0.clone()), request.into())) + Some((LSPSRequestId(request_id.0.clone()), request.into())) }, LSPSMessage::LSPS1(LSPS1Message::Request(request_id, request)) => { - Some((RequestId(request_id.0.clone()), request.into())) + Some((LSPSRequestId(request_id.0.clone()), request.into())) }, LSPSMessage::LSPS2(LSPS2Message::Request(request_id, request)) => { - Some((RequestId(request_id.0.clone()), request.into())) + Some((LSPSRequestId(request_id.0.clone()), request.into())) }, _ => None, } @@ -361,7 +361,7 @@ impl Serialize for LSPSMessage { } struct LSPSMessageVisitor<'a> { - request_id_to_method_map: &'a mut HashMap, + request_id_to_method_map: &'a mut HashMap, } impl<'de, 'a> Visitor<'de> for LSPSMessageVisitor<'a> { @@ -375,11 +375,11 @@ impl<'de, 'a> Visitor<'de> for LSPSMessageVisitor<'a> { where A: MapAccess<'de>, { - let mut id: Option = None; + let mut id: Option = None; let mut method: Option = None; let mut params = None; let mut result = None; - let mut error: Option = None; + let mut error: Option = None; while let Some(key) = map.next_key()? { match key { diff --git a/lightning-liquidity/src/lsps0/service.rs b/lightning-liquidity/src/lsps0/service.rs index 8a0d8676c..189a2cd09 100644 --- a/lightning-liquidity/src/lsps0/service.rs +++ b/lightning-liquidity/src/lsps0/service.rs @@ -14,7 +14,7 @@ //! information. use crate::lsps0::msgs::{LSPS0ListProtocolsResponse, LSPS0Message, LSPS0Request, LSPS0Response}; -use crate::lsps0::ser::{ProtocolMessageHandler, RequestId}; +use crate::lsps0::ser::{LSPSProtocolMessageHandler, LSPSRequestId}; use crate::message_queue::MessageQueue; use crate::prelude::Vec; use crate::sync::Arc; @@ -37,7 +37,7 @@ impl LSPS0ServiceHandler { } fn handle_request( - &self, request_id: RequestId, request: LSPS0Request, counterparty_node_id: &PublicKey, + &self, request_id: LSPSRequestId, request: LSPS0Request, counterparty_node_id: &PublicKey, ) -> Result<(), lightning::ln::msgs::LightningError> { match request { LSPS0Request::ListProtocols(_) => { @@ -54,7 +54,7 @@ impl LSPS0ServiceHandler { } } -impl ProtocolMessageHandler for LSPS0ServiceHandler { +impl LSPSProtocolMessageHandler for LSPS0ServiceHandler { type ProtocolMessage = LSPS0Message; const PROTOCOL_NUMBER: Option = None; @@ -95,7 +95,7 @@ mod tests { let lsps0_handler = Arc::new(LSPS0ServiceHandler::new(protocols, pending_messages.clone())); let list_protocols_request = LSPS0Message::Request( - RequestId("xyz123".to_string()), + LSPSRequestId("xyz123".to_string()), LSPS0Request::ListProtocols(LSPS0ListProtocolsRequest {}), ); let counterparty_node_id = utils::parse_pubkey( @@ -114,7 +114,7 @@ mod tests { assert_eq!( *message, LSPSMessage::LSPS0(LSPS0Message::Response( - RequestId("xyz123".to_string()), + LSPSRequestId("xyz123".to_string()), LSPS0Response::ListProtocols(LSPS0ListProtocolsResponse { protocols: vec![] }) )) ); diff --git a/lightning-liquidity/src/lsps1/client.rs b/lightning-liquidity/src/lsps1/client.rs index e354ffa9d..88b3abc5c 100644 --- a/lightning-liquidity/src/lsps1/client.rs +++ b/lightning-liquidity/src/lsps1/client.rs @@ -18,7 +18,7 @@ use super::msgs::{ use crate::message_queue::MessageQueue; use crate::events::EventQueue; -use crate::lsps0::ser::{ProtocolMessageHandler, RequestId, ResponseError}; +use crate::lsps0::ser::{LSPSProtocolMessageHandler, LSPSRequestId, LSPSResponseError}; use crate::prelude::{new_hash_map, HashMap, HashSet}; use crate::sync::{Arc, Mutex, RwLock}; @@ -40,9 +40,9 @@ pub struct LSPS1ClientConfig { #[derive(Default)] struct PeerState { - pending_get_info_requests: HashSet, - pending_create_order_requests: HashSet, - pending_get_order_requests: HashSet, + pending_get_info_requests: HashSet, + pending_create_order_requests: HashSet, + pending_get_order_requests: HashSet, } /// The main object allowing to send and receive bLIP-51 / LSPS1 messages. @@ -81,10 +81,10 @@ where /// /// `counterparty_node_id` is the `node_id` of the LSP you would like to use. /// - /// Returns the used [`RequestId`], which will be returned via [`SupportedOptionsReady`]. + /// Returns the used [`LSPSRequestId`], which will be returned via [`SupportedOptionsReady`]. /// /// [`SupportedOptionsReady`]: crate::lsps1::event::LSPS1ClientEvent::SupportedOptionsReady - pub fn request_supported_options(&self, counterparty_node_id: PublicKey) -> RequestId { + pub fn request_supported_options(&self, counterparty_node_id: PublicKey) -> LSPSRequestId { let request_id = crate::utils::generate_request_id(&self.entropy_source); { let mut outer_state_lock = self.per_peer_state.write().unwrap(); @@ -102,7 +102,7 @@ where } fn handle_get_info_response( - &self, request_id: RequestId, counterparty_node_id: &PublicKey, + &self, request_id: LSPSRequestId, counterparty_node_id: &PublicKey, result: LSPS1GetInfoResponse, ) -> Result<(), LightningError> { let outer_state_lock = self.per_peer_state.write().unwrap(); @@ -139,7 +139,8 @@ where } fn handle_get_info_error( - &self, request_id: RequestId, counterparty_node_id: &PublicKey, error: ResponseError, + &self, request_id: LSPSRequestId, counterparty_node_id: &PublicKey, + error: LSPSResponseError, ) -> Result<(), LightningError> { let outer_state_lock = self.per_peer_state.read().unwrap(); match outer_state_lock.get(counterparty_node_id) { @@ -188,7 +189,7 @@ where pub fn create_order( &self, counterparty_node_id: &PublicKey, order: LSPS1OrderParams, refund_onchain_address: Option
, - ) -> RequestId { + ) -> LSPSRequestId { let (request_id, request_msg) = { let mut outer_state_lock = self.per_peer_state.write().unwrap(); let inner_state_lock = outer_state_lock @@ -215,7 +216,7 @@ where } fn handle_create_order_response( - &self, request_id: RequestId, counterparty_node_id: &PublicKey, + &self, request_id: LSPSRequestId, counterparty_node_id: &PublicKey, response: LSPS1CreateOrderResponse, ) -> Result<(), LightningError> { let outer_state_lock = self.per_peer_state.read().unwrap(); @@ -257,7 +258,8 @@ where } fn handle_create_order_error( - &self, request_id: RequestId, counterparty_node_id: &PublicKey, error: ResponseError, + &self, request_id: LSPSRequestId, counterparty_node_id: &PublicKey, + error: LSPSResponseError, ) -> Result<(), LightningError> { let outer_state_lock = self.per_peer_state.read().unwrap(); match outer_state_lock.get(counterparty_node_id) { @@ -307,7 +309,7 @@ where /// [`LSPS1ClientEvent::OrderStatus`]: crate::lsps1::event::LSPS1ClientEvent::OrderStatus pub fn check_order_status( &self, counterparty_node_id: &PublicKey, order_id: LSPS1OrderId, - ) -> RequestId { + ) -> LSPSRequestId { let (request_id, request_msg) = { let mut outer_state_lock = self.per_peer_state.write().unwrap(); let inner_state_lock = outer_state_lock @@ -333,7 +335,7 @@ where } fn handle_get_order_response( - &self, request_id: RequestId, counterparty_node_id: &PublicKey, + &self, request_id: LSPSRequestId, counterparty_node_id: &PublicKey, response: LSPS1CreateOrderResponse, ) -> Result<(), LightningError> { let outer_state_lock = self.per_peer_state.read().unwrap(); @@ -375,7 +377,8 @@ where } fn handle_get_order_error( - &self, request_id: RequestId, counterparty_node_id: &PublicKey, error: ResponseError, + &self, request_id: LSPSRequestId, counterparty_node_id: &PublicKey, + error: LSPSResponseError, ) -> Result<(), LightningError> { let outer_state_lock = self.per_peer_state.read().unwrap(); match outer_state_lock.get(counterparty_node_id) { @@ -419,7 +422,7 @@ where } } -impl ProtocolMessageHandler for LSPS1ClientHandler +impl LSPSProtocolMessageHandler for LSPS1ClientHandler where ES::Target: EntropySource, { diff --git a/lightning-liquidity/src/lsps1/event.rs b/lightning-liquidity/src/lsps1/event.rs index 91e3160d2..508a5a42a 100644 --- a/lightning-liquidity/src/lsps1/event.rs +++ b/lightning-liquidity/src/lsps1/event.rs @@ -12,7 +12,7 @@ use super::msgs::LSPS1OrderId; use super::msgs::{LSPS1ChannelInfo, LSPS1Options, LSPS1OrderParams, LSPS1PaymentInfo}; -use crate::lsps0::ser::{RequestId, ResponseError}; +use crate::lsps0::ser::{LSPSRequestId, LSPSResponseError}; use bitcoin::secp256k1::PublicKey; @@ -34,7 +34,7 @@ pub enum LSPS1ClientEvent { /// This can be used to track which request this event corresponds to. /// /// [`LSPS1ClientHandler::request_supported_options`]: crate::lsps1::client::LSPS1ClientHandler::request_supported_options - request_id: RequestId, + request_id: LSPSRequestId, /// The node id of the LSP that provided this response. counterparty_node_id: PublicKey, /// All options supported by the LSP. @@ -51,11 +51,11 @@ pub enum LSPS1ClientEvent { /// This can be used to track which request this event corresponds to. /// /// [`LSPS1ClientHandler::request_supported_options`]: crate::lsps1::client::LSPS1ClientHandler::request_supported_options - request_id: RequestId, + request_id: LSPSRequestId, /// The node id of the LSP that provided this response. counterparty_node_id: PublicKey, /// The error that was returned. - error: ResponseError, + error: LSPSResponseError, }, /// Confirmation from the LSP about the order created by the client. /// @@ -74,7 +74,7 @@ pub enum LSPS1ClientEvent { /// This can be used to track which request this event corresponds to. /// /// [`LSPS1ClientHandler::create_order`]: crate::lsps1::client::LSPS1ClientHandler::create_order - request_id: RequestId, + request_id: LSPSRequestId, /// The node id of the LSP. counterparty_node_id: PublicKey, /// The id of the channel order. @@ -98,7 +98,7 @@ pub enum LSPS1ClientEvent { /// This can be used to track which request this event corresponds to. /// /// [`LSPS1ClientHandler::check_order_status`]: crate::lsps1::client::LSPS1ClientHandler::check_order_status - request_id: RequestId, + request_id: LSPSRequestId, /// The node id of the LSP. counterparty_node_id: PublicKey, /// The id of the channel order. @@ -123,11 +123,11 @@ pub enum LSPS1ClientEvent { /// /// [`LSPS1ClientHandler::create_order`]: crate::lsps1::client::LSPS1ClientHandler::create_order /// [`LSPS1ClientHandler::check_order_status`]: crate::lsps1::client::LSPS1ClientHandler::check_order_status - request_id: RequestId, + request_id: LSPSRequestId, /// The node id of the LSP. counterparty_node_id: PublicKey, /// The error that was returned. - error: ResponseError, + error: LSPSResponseError, }, } @@ -147,7 +147,7 @@ pub enum LSPS1ServiceEvent { /// An identifier that must be passed to [`LSPS1ServiceHandler::send_payment_details`]. /// /// [`LSPS1ServiceHandler::send_payment_details`]: crate::lsps1::service::LSPS1ServiceHandler::send_payment_details - request_id: RequestId, + request_id: LSPSRequestId, /// The node id of the client making the information request. counterparty_node_id: PublicKey, /// The order requested by the client. @@ -165,7 +165,7 @@ pub enum LSPS1ServiceEvent { /// An identifier that must be passed to [`LSPS1ServiceHandler::update_order_status`]. /// /// [`LSPS1ServiceHandler::update_order_status`]: crate::lsps1::service::LSPS1ServiceHandler::update_order_status - request_id: RequestId, + request_id: LSPSRequestId, /// The node id of the client making the information request. counterparty_node_id: PublicKey, /// The order id of order with pending payment. @@ -174,7 +174,7 @@ pub enum LSPS1ServiceEvent { /// If error is encountered, refund the amount if paid by the client. Refund { /// An identifier. - request_id: RequestId, + request_id: LSPSRequestId, /// The node id of the client making the information request. counterparty_node_id: PublicKey, /// The order id of the refunded order. diff --git a/lightning-liquidity/src/lsps1/msgs.rs b/lightning-liquidity/src/lsps1/msgs.rs index db46a21ba..86c63e710 100644 --- a/lightning-liquidity/src/lsps1/msgs.rs +++ b/lightning-liquidity/src/lsps1/msgs.rs @@ -2,7 +2,7 @@ use crate::lsps0::ser::{ string_amount, u32_fee_rate, unchecked_address, unchecked_address_option, LSPSMessage, - RequestId, ResponseError, + LSPSRequestId, LSPSResponseError, }; use crate::prelude::String; @@ -272,24 +272,24 @@ pub enum LSPS1Response { /// A successful response to a [`LSPS1GetInfoRequest`]. GetInfo(LSPS1GetInfoResponse), /// An error response to a [`LSPS1GetInfoRequest`]. - GetInfoError(ResponseError), + GetInfoError(LSPSResponseError), /// A successful response to a [`LSPS1CreateOrderRequest`]. CreateOrder(LSPS1CreateOrderResponse), /// An error response to a [`LSPS1CreateOrderRequest`]. - CreateOrderError(ResponseError), + CreateOrderError(LSPSResponseError), /// A successful response to a [`LSPS1GetOrderRequest`]. GetOrder(LSPS1CreateOrderResponse), /// An error response to a [`LSPS1GetOrderRequest`]. - GetOrderError(ResponseError), + GetOrderError(LSPSResponseError), } /// An enum that captures all valid JSON-RPC messages in the bLIP-51 / LSPS1 protocol. #[derive(Clone, Debug, PartialEq, Eq)] pub enum LSPS1Message { /// An LSPS1 JSON-RPC request. - Request(RequestId, LSPS1Request), + Request(LSPSRequestId, LSPS1Request), /// An LSPS1 JSON-RPC response. - Response(RequestId, LSPS1Response), + Response(LSPSRequestId, LSPS1Response), } impl TryFrom for LSPS1Message { diff --git a/lightning-liquidity/src/lsps1/service.rs b/lightning-liquidity/src/lsps1/service.rs index b51721eb5..836bebed5 100644 --- a/lightning-liquidity/src/lsps1/service.rs +++ b/lightning-liquidity/src/lsps1/service.rs @@ -19,7 +19,7 @@ use super::msgs::{ use crate::message_queue::MessageQueue; use crate::events::EventQueue; -use crate::lsps0::ser::{ProtocolMessageHandler, RequestId, ResponseError}; +use crate::lsps0::ser::{LSPSProtocolMessageHandler, LSPSRequestId, LSPSResponseError}; use crate::prelude::{new_hash_map, HashMap, String}; use crate::sync::{Arc, Mutex, RwLock}; use crate::utils; @@ -107,8 +107,8 @@ impl OutboundCRChannel { #[derive(Default)] struct PeerState { outbound_channels_by_order_id: HashMap, - request_to_cid: HashMap, - pending_requests: HashMap, + request_to_cid: HashMap, + pending_requests: HashMap, } impl PeerState { @@ -116,7 +116,7 @@ impl PeerState { self.outbound_channels_by_order_id.insert(order_id, channel); } - fn insert_request(&mut self, request_id: RequestId, channel_id: u128) { + fn insert_request(&mut self, request_id: LSPSRequestId, channel_id: u128) { self.request_to_cid.insert(request_id, channel_id); } @@ -165,7 +165,7 @@ where } fn handle_get_info_request( - &self, request_id: RequestId, counterparty_node_id: &PublicKey, + &self, request_id: LSPSRequestId, counterparty_node_id: &PublicKey, ) -> Result<(), LightningError> { let response = LSPS1Response::GetInfo(LSPS1GetInfoResponse { options: self @@ -185,11 +185,11 @@ where } fn handle_create_order_request( - &self, request_id: RequestId, counterparty_node_id: &PublicKey, + &self, request_id: LSPSRequestId, counterparty_node_id: &PublicKey, params: LSPS1CreateOrderRequest, ) -> Result<(), LightningError> { if !is_valid(¶ms.order, &self.config.supported_options.as_ref().unwrap()) { - let response = LSPS1Response::CreateOrderError(ResponseError { + let response = LSPS1Response::CreateOrderError(LSPSResponseError { code: LSPS1_CREATE_ORDER_REQUEST_ORDER_MISMATCH_ERROR_CODE, message: format!("Order does not match options supported by LSP server"), data: Some(format!( @@ -236,8 +236,8 @@ where /// /// [`LSPS1ServiceEvent::RequestForPaymentDetails`]: crate::lsps1::event::LSPS1ServiceEvent::RequestForPaymentDetails pub fn send_payment_details( - &self, request_id: RequestId, counterparty_node_id: &PublicKey, payment: LSPS1PaymentInfo, - created_at: chrono::DateTime, + &self, request_id: LSPSRequestId, counterparty_node_id: &PublicKey, + payment: LSPS1PaymentInfo, created_at: chrono::DateTime, ) -> Result<(), APIError> { let (result, response) = { let outer_state_lock = self.per_peer_state.read().unwrap(); @@ -302,7 +302,7 @@ where } fn handle_get_order_request( - &self, request_id: RequestId, counterparty_node_id: &PublicKey, + &self, request_id: LSPSRequestId, counterparty_node_id: &PublicKey, params: LSPS1GetOrderRequest, ) -> Result<(), LightningError> { let outer_state_lock = self.per_peer_state.read().unwrap(); @@ -361,7 +361,7 @@ where /// /// [`LSPS1ServiceEvent::CheckPaymentConfirmation`]: crate::lsps1::event::LSPS1ServiceEvent::CheckPaymentConfirmation pub fn update_order_status( - &self, request_id: RequestId, counterparty_node_id: PublicKey, order_id: LSPS1OrderId, + &self, request_id: LSPSRequestId, counterparty_node_id: PublicKey, order_id: LSPS1OrderId, order_state: LSPS1OrderState, channel: Option, ) -> Result<(), APIError> { let (result, response) = { @@ -420,7 +420,7 @@ where } } -impl ProtocolMessageHandler +impl LSPSProtocolMessageHandler for LSPS1ServiceHandler where ES::Target: EntropySource, diff --git a/lightning-liquidity/src/lsps2/client.rs b/lightning-liquidity/src/lsps2/client.rs index f0157b139..f822e119a 100644 --- a/lightning-liquidity/src/lsps2/client.rs +++ b/lightning-liquidity/src/lsps2/client.rs @@ -9,7 +9,7 @@ //! Contains the main bLIP-52 / LSPS2 client object, [`LSPS2ClientHandler`]. use crate::events::EventQueue; -use crate::lsps0::ser::{ProtocolMessageHandler, RequestId, ResponseError}; +use crate::lsps0::ser::{LSPSProtocolMessageHandler, LSPSRequestId, LSPSResponseError}; use crate::lsps2::event::LSPS2ClientEvent; use crate::message_queue::MessageQueue; use crate::prelude::{new_hash_map, new_hash_set, HashMap, HashSet, String, ToString}; @@ -45,8 +45,8 @@ impl InboundJITChannel { } struct PeerState { - pending_get_info_requests: HashSet, - pending_buy_requests: HashMap, + pending_get_info_requests: HashSet, + pending_buy_requests: HashMap, } impl PeerState { @@ -105,12 +105,12 @@ where /// `token` is an optional `String` that will be provided to the LSP. /// It can be used by the LSP as an API key, coupon code, or some other way to identify a user. /// - /// Returns the used [`RequestId`], which will be returned via [`OpeningParametersReady`]. + /// Returns the used [`LSPSRequestId`], which will be returned via [`OpeningParametersReady`]. /// /// [`OpeningParametersReady`]: crate::lsps2::event::LSPS2ClientEvent::OpeningParametersReady pub fn request_opening_params( &self, counterparty_node_id: PublicKey, token: Option, - ) -> RequestId { + ) -> LSPSRequestId { let request_id = crate::utils::generate_request_id(&self.entropy_source); { @@ -150,7 +150,7 @@ where pub fn select_opening_params( &self, counterparty_node_id: PublicKey, payment_size_msat: Option, opening_fee_params: LSPS2OpeningFeeParams, - ) -> Result { + ) -> Result { let request_id = crate::utils::generate_request_id(&self.entropy_source); { @@ -181,7 +181,7 @@ where } fn handle_get_info_response( - &self, request_id: RequestId, counterparty_node_id: &PublicKey, + &self, request_id: LSPSRequestId, counterparty_node_id: &PublicKey, result: LSPS2GetInfoResponse, ) -> Result<(), LightningError> { let outer_state_lock = self.per_peer_state.read().unwrap(); @@ -220,7 +220,8 @@ where } fn handle_get_info_error( - &self, request_id: RequestId, counterparty_node_id: &PublicKey, _error: ResponseError, + &self, request_id: LSPSRequestId, counterparty_node_id: &PublicKey, + _error: LSPSResponseError, ) -> Result<(), LightningError> { let outer_state_lock = self.per_peer_state.read().unwrap(); match outer_state_lock.get(counterparty_node_id) { @@ -246,7 +247,8 @@ where } fn handle_buy_response( - &self, request_id: RequestId, counterparty_node_id: &PublicKey, result: LSPS2BuyResponse, + &self, request_id: LSPSRequestId, counterparty_node_id: &PublicKey, + result: LSPS2BuyResponse, ) -> Result<(), LightningError> { let outer_state_lock = self.per_peer_state.read().unwrap(); match outer_state_lock.get(counterparty_node_id) { @@ -294,7 +296,8 @@ where } fn handle_buy_error( - &self, request_id: RequestId, counterparty_node_id: &PublicKey, _error: ResponseError, + &self, request_id: LSPSRequestId, counterparty_node_id: &PublicKey, + _error: LSPSResponseError, ) -> Result<(), LightningError> { let outer_state_lock = self.per_peer_state.read().unwrap(); match outer_state_lock.get(counterparty_node_id) { @@ -315,7 +318,7 @@ where } } -impl ProtocolMessageHandler for LSPS2ClientHandler +impl LSPSProtocolMessageHandler for LSPS2ClientHandler where ES::Target: EntropySource, { diff --git a/lightning-liquidity/src/lsps2/event.rs b/lightning-liquidity/src/lsps2/event.rs index 41dcb9c74..6ae09a417 100644 --- a/lightning-liquidity/src/lsps2/event.rs +++ b/lightning-liquidity/src/lsps2/event.rs @@ -10,7 +10,7 @@ //! Contains bLIP-52 / LSPS2 event types use super::msgs::LSPS2OpeningFeeParams; -use crate::lsps0::ser::RequestId; +use crate::lsps0::ser::LSPSRequestId; use crate::prelude::{String, Vec}; use bitcoin::secp256k1::PublicKey; @@ -31,7 +31,7 @@ pub enum LSPS2ClientEvent { /// This can be used to track which request this event corresponds to. /// /// [`LSPS2ClientHandler::request_opening_params`]: crate::lsps2::client::LSPS2ClientHandler::request_opening_params - request_id: RequestId, + request_id: LSPSRequestId, /// The node id of the LSP that provided this response. counterparty_node_id: PublicKey, /// The menu of fee parameters the LSP is offering at this time. @@ -50,7 +50,7 @@ pub enum LSPS2ClientEvent { /// This can be used to track which request this event corresponds to. /// /// [`LSPS2ClientHandler::select_opening_params`]: crate::lsps2::client::LSPS2ClientHandler::select_opening_params - request_id: RequestId, + request_id: LSPSRequestId, /// The node id of the LSP. counterparty_node_id: PublicKey, /// The intercept short channel id to use in the route hint. @@ -79,7 +79,7 @@ pub enum LSPS2ServiceEvent { /// An identifier that must be passed to [`LSPS2ServiceHandler::opening_fee_params_generated`]. /// /// [`LSPS2ServiceHandler::opening_fee_params_generated`]: crate::lsps2::service::LSPS2ServiceHandler::opening_fee_params_generated - request_id: RequestId, + request_id: LSPSRequestId, /// The node id of the client making the information request. counterparty_node_id: PublicKey, /// An optional token that can be used as an API key, coupon code, etc. @@ -99,7 +99,7 @@ pub enum LSPS2ServiceEvent { /// An identifier that must be passed into [`LSPS2ServiceHandler::invoice_parameters_generated`]. /// /// [`LSPS2ServiceHandler::invoice_parameters_generated`]: crate::lsps2::service::LSPS2ServiceHandler::invoice_parameters_generated - request_id: RequestId, + request_id: LSPSRequestId, /// The client node id that is making this request. counterparty_node_id: PublicKey, /// The channel parameters they have selected. diff --git a/lightning-liquidity/src/lsps2/msgs.rs b/lightning-liquidity/src/lsps2/msgs.rs index ab8114c38..3df9e0212 100644 --- a/lightning-liquidity/src/lsps2/msgs.rs +++ b/lightning-liquidity/src/lsps2/msgs.rs @@ -11,7 +11,7 @@ use serde::{Deserialize, Serialize}; use lightning::util::scid_utils; use crate::lsps0::ser::{ - string_amount, string_amount_option, LSPSMessage, RequestId, ResponseError, + string_amount, string_amount_option, LSPSMessage, LSPSRequestId, LSPSResponseError, }; use crate::prelude::{String, Vec}; use crate::utils; @@ -177,20 +177,20 @@ pub enum LSPS2Response { /// A successful response to a [`LSPS2Request::GetInfo`] request. GetInfo(LSPS2GetInfoResponse), /// An error response to a [`LSPS2Request::GetInfo`] request. - GetInfoError(ResponseError), + GetInfoError(LSPSResponseError), /// A successful response to a [`LSPS2Request::Buy`] request. Buy(LSPS2BuyResponse), /// An error response to a [`LSPS2Request::Buy`] request. - BuyError(ResponseError), + BuyError(LSPSResponseError), } #[derive(Clone, Debug, PartialEq, Eq)] /// An enum that captures all valid JSON-RPC messages in the bLIP-52 / LSPS2 protocol. pub enum LSPS2Message { /// An LSPS2 JSON-RPC request. - Request(RequestId, LSPS2Request), + Request(LSPSRequestId, LSPS2Request), /// An LSPS2 JSON-RPC response. - Response(RequestId, LSPS2Response), + Response(LSPSRequestId, LSPS2Response), } impl TryFrom for LSPS2Message { diff --git a/lightning-liquidity/src/lsps2/service.rs b/lightning-liquidity/src/lsps2/service.rs index 3b53fe42f..60b6c499e 100644 --- a/lightning-liquidity/src/lsps2/service.rs +++ b/lightning-liquidity/src/lsps2/service.rs @@ -11,7 +11,7 @@ use crate::events::EventQueue; use crate::lsps0::ser::{ - LSPSMessage, ProtocolMessageHandler, RequestId, ResponseError, + LSPSMessage, LSPSProtocolMessageHandler, LSPSRequestId, LSPSResponseError, JSONRPC_INTERNAL_ERROR_ERROR_CODE, JSONRPC_INTERNAL_ERROR_ERROR_MESSAGE, LSPS0_CLIENT_REJECTED_ERROR_CODE, }; @@ -457,7 +457,7 @@ struct PeerState { outbound_channels_by_intercept_scid: HashMap, intercept_scid_by_user_channel_id: HashMap, intercept_scid_by_channel_id: HashMap, - pending_requests: HashMap, + pending_requests: HashMap, } impl PeerState { @@ -523,7 +523,7 @@ macro_rules! get_or_insert_peer_state_entry { match $outer_state_lock.entry(*$counterparty_node_id) { Entry::Vacant(e) => { if is_limited_by_max_total_peers { - let error_response = ResponseError { + let error_response = LSPSResponseError { code: JSONRPC_INTERNAL_ERROR_ERROR_CODE, message: JSONRPC_INTERNAL_ERROR_ERROR_MESSAGE.to_string(), data: None, }; @@ -592,7 +592,7 @@ where /// /// [`LSPS2ServiceEvent::GetInfo`]: crate::lsps2::event::LSPS2ServiceEvent::GetInfo pub fn invalid_token_provided( - &self, counterparty_node_id: &PublicKey, request_id: RequestId, + &self, counterparty_node_id: &PublicKey, request_id: LSPSRequestId, ) -> Result<(), APIError> { let (result, response) = { let outer_state_lock = self.per_peer_state.read().unwrap(); @@ -603,7 +603,7 @@ where match self.remove_pending_request(&mut peer_state_lock, &request_id) { Some(LSPS2Request::GetInfo(_)) => { - let response = LSPS2Response::GetInfoError(ResponseError { + let response = LSPS2Response::GetInfoError(LSPSResponseError { code: LSPS2_GET_INFO_REQUEST_UNRECOGNIZED_OR_STALE_TOKEN_ERROR_CODE, message: "an unrecognized or stale token was provided".to_string(), data: None, @@ -647,7 +647,7 @@ where /// /// [`LSPS2ServiceEvent::GetInfo`]: crate::lsps2::event::LSPS2ServiceEvent::GetInfo pub fn opening_fee_params_generated( - &self, counterparty_node_id: &PublicKey, request_id: RequestId, + &self, counterparty_node_id: &PublicKey, request_id: LSPSRequestId, opening_fee_params_menu: Vec, ) -> Result<(), APIError> { let (result, response) = { @@ -706,7 +706,7 @@ where /// /// [`LSPS2ServiceEvent::BuyRequest`]: crate::lsps2::event::LSPS2ServiceEvent::BuyRequest pub fn invoice_parameters_generated( - &self, counterparty_node_id: &PublicKey, request_id: RequestId, intercept_scid: u64, + &self, counterparty_node_id: &PublicKey, request_id: LSPSRequestId, intercept_scid: u64, cltv_expiry_delta: u32, client_trusts_lsp: bool, user_channel_id: u128, ) -> Result<(), APIError> { let (result, response) = { @@ -1077,7 +1077,8 @@ where } fn handle_get_info_request( - &self, request_id: RequestId, counterparty_node_id: &PublicKey, params: LSPS2GetInfoRequest, + &self, request_id: LSPSRequestId, counterparty_node_id: &PublicKey, + params: LSPS2GetInfoRequest, ) -> Result<(), LightningError> { let (result, response) = { let mut outer_state_lock = self.per_peer_state.write().unwrap(); @@ -1113,11 +1114,11 @@ where } fn handle_buy_request( - &self, request_id: RequestId, counterparty_node_id: &PublicKey, params: LSPS2BuyRequest, + &self, request_id: LSPSRequestId, counterparty_node_id: &PublicKey, params: LSPS2BuyRequest, ) -> Result<(), LightningError> { if let Some(payment_size_msat) = params.payment_size_msat { if payment_size_msat < params.opening_fee_params.min_payment_size_msat { - let response = LSPS2Response::BuyError(ResponseError { + let response = LSPS2Response::BuyError(LSPSResponseError { code: LSPS2_BUY_REQUEST_PAYMENT_SIZE_TOO_SMALL_ERROR_CODE, message: "payment size is below our minimum supported payment size".to_string(), data: None, @@ -1132,7 +1133,7 @@ where } if payment_size_msat > params.opening_fee_params.max_payment_size_msat { - let response = LSPS2Response::BuyError(ResponseError { + let response = LSPS2Response::BuyError(LSPSResponseError { code: LSPS2_BUY_REQUEST_PAYMENT_SIZE_TOO_LARGE_ERROR_CODE, message: "payment size is above our maximum supported payment size".to_string(), data: None, @@ -1152,7 +1153,7 @@ where ) { Some(opening_fee) => { if opening_fee >= payment_size_msat { - let response = LSPS2Response::BuyError(ResponseError { + let response = LSPS2Response::BuyError(LSPSResponseError { code: LSPS2_BUY_REQUEST_PAYMENT_SIZE_TOO_SMALL_ERROR_CODE, message: "payment size is too small to cover the opening fee" .to_string(), @@ -1167,7 +1168,7 @@ where } }, None => { - let response = LSPS2Response::BuyError(ResponseError { + let response = LSPS2Response::BuyError(LSPSResponseError { code: LSPS2_BUY_REQUEST_PAYMENT_SIZE_TOO_LARGE_ERROR_CODE, message: "overflow error when calculating opening_fee".to_string(), data: None, @@ -1184,7 +1185,7 @@ where // TODO: if payment_size_msat is specified, make sure our node has sufficient incoming liquidity from public network to receive it. if !is_valid_opening_fee_params(¶ms.opening_fee_params, &self.config.promise_secret) { - let response = LSPS2Response::BuyError(ResponseError { + let response = LSPS2Response::BuyError(LSPSResponseError { code: LSPS2_BUY_REQUEST_INVALID_OPENING_FEE_PARAMS_ERROR_CODE, message: "valid_until is already past OR the promise did not match the provided parameters".to_string(), data: None, @@ -1233,11 +1234,11 @@ where } fn insert_pending_request<'a>( - &self, peer_state_lock: &mut MutexGuard<'a, PeerState>, request_id: RequestId, + &self, peer_state_lock: &mut MutexGuard<'a, PeerState>, request_id: LSPSRequestId, counterparty_node_id: PublicKey, request: LSPS2Request, ) -> (Result<(), LightningError>, Option) { if self.total_pending_requests.load(Ordering::Relaxed) >= MAX_TOTAL_PENDING_REQUESTS { - let response = LSPS2Response::BuyError(ResponseError { + let response = LSPS2Response::BuyError(LSPSResponseError { code: LSPS0_CLIENT_REJECTED_ERROR_CODE, message: "Reached maximum number of pending requests. Please try again later." .to_string(), @@ -1259,7 +1260,7 @@ where self.total_pending_requests.fetch_add(1, Ordering::Relaxed); (Ok(()), None) } else { - let response = LSPS2Response::BuyError(ResponseError { + let response = LSPS2Response::BuyError(LSPSResponseError { code: LSPS0_CLIENT_REJECTED_ERROR_CODE, message: "Reached maximum number of pending requests. Please try again later." .to_string(), @@ -1279,7 +1280,7 @@ where } fn remove_pending_request<'a>( - &self, peer_state_lock: &mut MutexGuard<'a, PeerState>, request_id: &RequestId, + &self, peer_state_lock: &mut MutexGuard<'a, PeerState>, request_id: &LSPSRequestId, ) -> Option { match peer_state_lock.pending_requests.remove(request_id) { Some(req) => { @@ -1346,7 +1347,7 @@ where } } -impl ProtocolMessageHandler for LSPS2ServiceHandler +impl LSPSProtocolMessageHandler for LSPS2ServiceHandler where CM::Target: AChannelManager, { diff --git a/lightning-liquidity/src/manager.rs b/lightning-liquidity/src/manager.rs index 31d7c2d35..afaac25cd 100644 --- a/lightning-liquidity/src/manager.rs +++ b/lightning-liquidity/src/manager.rs @@ -2,8 +2,8 @@ use crate::events::{EventQueue, LiquidityEvent}; use crate::lsps0::client::LSPS0ClientHandler; use crate::lsps0::msgs::LSPS0Message; use crate::lsps0::ser::{ - LSPSMessage, LSPSMethod, ProtocolMessageHandler, RawLSPSMessage, RequestId, ResponseError, - JSONRPC_INVALID_MESSAGE_ERROR_CODE, JSONRPC_INVALID_MESSAGE_ERROR_MESSAGE, + LSPSMessage, LSPSMethod, LSPSProtocolMessageHandler, LSPSRequestId, LSPSResponseError, + RawLSPSMessage, JSONRPC_INVALID_MESSAGE_ERROR_CODE, JSONRPC_INVALID_MESSAGE_ERROR_MESSAGE, LSPS_MESSAGE_TYPE_ID, }; use crate::lsps0::service::LSPS0ServiceHandler; @@ -95,7 +95,7 @@ where { pending_messages: Arc, pending_events: Arc, - request_id_to_method_map: Mutex>, + request_id_to_method_map: Mutex>, // We ignore peers if they send us bogus data. ignored_peers: RwLock>, lsps0_client_handler: LSPS0ClientHandler, @@ -146,7 +146,7 @@ where { let lsps2_service_handler = service_config.as_ref().and_then(|config| { config.lsps2_service_config.as_ref().map(|config| { if let Some(number) = - as ProtocolMessageHandler>::PROTOCOL_NUMBER + as LSPSProtocolMessageHandler>::PROTOCOL_NUMBER { supported_protocols.push(number); } @@ -173,7 +173,7 @@ where { #[cfg(lsps1_service)] let lsps1_service_handler = service_config.as_ref().and_then(|config| { if let Some(number) = - as ProtocolMessageHandler>::PROTOCOL_NUMBER + as LSPSProtocolMessageHandler>::PROTOCOL_NUMBER { supported_protocols.push(number); } @@ -485,7 +485,7 @@ where LSPSMessage::from_str_with_id_map(&msg.payload, &mut request_id_to_method_map) } .map_err(|_| { - let error = ResponseError { + let error = LSPSResponseError { code: JSONRPC_INVALID_MESSAGE_ERROR_CODE, message: JSONRPC_INVALID_MESSAGE_ERROR_MESSAGE.to_string(), data: None, diff --git a/lightning-liquidity/src/utils.rs b/lightning-liquidity/src/utils.rs index e355c72eb..8f961e726 100644 --- a/lightning-liquidity/src/utils.rs +++ b/lightning-liquidity/src/utils.rs @@ -1,7 +1,7 @@ use core::{fmt::Write, ops::Deref}; use lightning::sign::EntropySource; -use crate::lsps0::ser::RequestId; +use crate::lsps0::ser::LSPSRequestId; use crate::prelude::String; pub fn scid_from_human_readable_string(human_readable_scid: &str) -> Result { @@ -14,12 +14,12 @@ pub fn scid_from_human_readable_string(human_readable_scid: &str) -> Result(entropy_source: &ES) -> RequestId +pub(crate) fn generate_request_id(entropy_source: &ES) -> LSPSRequestId where ES::Target: EntropySource, { let bytes = entropy_source.get_secure_random_bytes(); - RequestId(hex_str(&bytes[0..16])) + LSPSRequestId(hex_str(&bytes[0..16])) } #[inline]