Prefix general LSPS message types to avoid naming collisions

This commit is contained in:
Elias Rohrer 2025-02-02 09:47:41 +01:00
parent cfc636c174
commit 8526b9fca3
No known key found for this signature in database
GPG key ID: 36153082BDF676FD
14 changed files with 143 additions and 136 deletions

View file

@ -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<ES: Deref> ProtocolMessageHandler for LSPS0ClientHandler<ES>
impl<ES: Deref> LSPSProtocolMessageHandler for LSPS0ClientHandler<ES>
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 {})
))
);

View file

@ -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<LSPSMessage> 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();

View file

@ -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<LSPSMessage> + Into<LSPSMessage>;
const PROTOCOL_NUMBER: Option<u16>;
@ -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<RequestId, LSPSMethod>,
json_str: &str, request_id_to_method_map: &mut HashMap<LSPSRequestId, LSPSMethod>,
) -> Result<Self, serde_json::Error> {
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<RequestId, LSPSMethod>,
request_id_to_method_map: &'a mut HashMap<LSPSRequestId, LSPSMethod>,
}
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<RequestId> = None;
let mut id: Option<LSPSRequestId> = None;
let mut method: Option<LSPSMethod> = None;
let mut params = None;
let mut result = None;
let mut error: Option<ResponseError> = None;
let mut error: Option<LSPSResponseError> = None;
while let Some(key) = map.next_key()? {
match key {

View file

@ -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<u16> = 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![] })
))
);

View file

@ -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<RequestId>,
pending_create_order_requests: HashSet<RequestId>,
pending_get_order_requests: HashSet<RequestId>,
pending_get_info_requests: HashSet<LSPSRequestId>,
pending_create_order_requests: HashSet<LSPSRequestId>,
pending_get_order_requests: HashSet<LSPSRequestId>,
}
/// 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<Address>,
) -> 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<ES: Deref> ProtocolMessageHandler for LSPS1ClientHandler<ES>
impl<ES: Deref> LSPSProtocolMessageHandler for LSPS1ClientHandler<ES>
where
ES::Target: EntropySource,
{

View file

@ -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.

View file

@ -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<LSPSMessage> for LSPS1Message {

View file

@ -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<LSPS1OrderId, OutboundCRChannel>,
request_to_cid: HashMap<RequestId, u128>,
pending_requests: HashMap<RequestId, LSPS1Request>,
request_to_cid: HashMap<LSPSRequestId, u128>,
pending_requests: HashMap<LSPSRequestId, LSPS1Request>,
}
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(&params.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<Utc>,
&self, request_id: LSPSRequestId, counterparty_node_id: &PublicKey,
payment: LSPS1PaymentInfo, created_at: chrono::DateTime<Utc>,
) -> 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<LSPS1ChannelInfo>,
) -> Result<(), APIError> {
let (result, response) = {
@ -420,7 +420,7 @@ where
}
}
impl<ES: Deref, CM: Deref + Clone, C: Deref> ProtocolMessageHandler
impl<ES: Deref, CM: Deref + Clone, C: Deref> LSPSProtocolMessageHandler
for LSPS1ServiceHandler<ES, CM, C>
where
ES::Target: EntropySource,

View file

@ -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<RequestId>,
pending_buy_requests: HashMap<RequestId, InboundJITChannel>,
pending_get_info_requests: HashSet<LSPSRequestId>,
pending_buy_requests: HashMap<LSPSRequestId, InboundJITChannel>,
}
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<String>,
) -> 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<u64>,
opening_fee_params: LSPS2OpeningFeeParams,
) -> Result<RequestId, APIError> {
) -> Result<LSPSRequestId, APIError> {
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<ES: Deref> ProtocolMessageHandler for LSPS2ClientHandler<ES>
impl<ES: Deref> LSPSProtocolMessageHandler for LSPS2ClientHandler<ES>
where
ES::Target: EntropySource,
{

View file

@ -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.

View file

@ -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<LSPSMessage> for LSPS2Message {

View file

@ -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<u64, OutboundJITChannel>,
intercept_scid_by_user_channel_id: HashMap<u128, u64>,
intercept_scid_by_channel_id: HashMap<ChannelId, u64>,
pending_requests: HashMap<RequestId, LSPS2Request>,
pending_requests: HashMap<LSPSRequestId, LSPS2Request>,
}
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<LSPS2RawOpeningFeeParams>,
) -> 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(&params.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<LSPSMessage>) {
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<LSPS2Request> {
match peer_state_lock.pending_requests.remove(request_id) {
Some(req) => {
@ -1346,7 +1347,7 @@ where
}
}
impl<CM: Deref + Clone> ProtocolMessageHandler for LSPS2ServiceHandler<CM>
impl<CM: Deref + Clone> LSPSProtocolMessageHandler for LSPS2ServiceHandler<CM>
where
CM::Target: AChannelManager,
{

View file

@ -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<MessageQueue>,
pending_events: Arc<EventQueue>,
request_id_to_method_map: Mutex<HashMap<RequestId, LSPSMethod>>,
request_id_to_method_map: Mutex<HashMap<LSPSRequestId, LSPSMethod>>,
// We ignore peers if they send us bogus data.
ignored_peers: RwLock<HashSet<PublicKey>>,
lsps0_client_handler: LSPS0ClientHandler<ES>,
@ -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) =
<LSPS2ServiceHandler<CM> as ProtocolMessageHandler>::PROTOCOL_NUMBER
<LSPS2ServiceHandler<CM> 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) =
<LSPS1ServiceHandler<ES, CM, C> as ProtocolMessageHandler>::PROTOCOL_NUMBER
<LSPS1ServiceHandler<ES, CM, C> 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,

View file

@ -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<u64, ()> {
@ -14,12 +14,12 @@ pub fn scid_from_human_readable_string(human_readable_scid: &str) -> Result<u64,
Ok((block << 40) | (tx_index << 16) | vout_index)
}
pub(crate) fn generate_request_id<ES: Deref>(entropy_source: &ES) -> RequestId
pub(crate) fn generate_request_id<ES: Deref>(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]