|
| 1 | +//! Contains the main LSPS2 client-side object, [`LSPS0ClientHandler`]. |
| 2 | +//! |
| 3 | +//! Please refer to the [LSPS0 |
| 4 | +//! specifcation](https://github.com/BitcoinAndLightningLayerSpecs/lsp/tree/main/LSPS0) for more |
| 5 | +//! information. |
| 6 | +
|
| 7 | +use crate::events::{Event, EventQueue}; |
| 8 | +use crate::lsps0::event::LSPS0ClientEvent; |
| 9 | +use crate::lsps0::msgs::{ |
| 10 | + LSPS0Message, LSPS0Request, LSPS0Response, ListProtocolsRequest, ListProtocolsResponse, |
| 11 | + ProtocolMessageHandler, ResponseError, |
| 12 | +}; |
| 13 | +use crate::message_queue::MessageQueue; |
| 14 | +use crate::sync::Arc; |
| 15 | +use crate::utils; |
| 16 | + |
| 17 | +use lightning::ln::msgs::{ErrorAction, LightningError}; |
| 18 | +use lightning::sign::EntropySource; |
| 19 | +use lightning::util::logger::Level; |
| 20 | + |
| 21 | +use bitcoin::secp256k1::PublicKey; |
| 22 | + |
| 23 | +use core::ops::Deref; |
| 24 | + |
| 25 | +/// A message handler capable of sending and handling LSPS0 messages. |
| 26 | +pub struct LSPS0ClientHandler<ES: Deref, MQ: Deref> |
| 27 | +where |
| 28 | + ES::Target: EntropySource, |
| 29 | + MQ::Target: MessageQueue, |
| 30 | +{ |
| 31 | + entropy_source: ES, |
| 32 | + pending_messages: MQ, |
| 33 | + pending_events: Arc<EventQueue>, |
| 34 | +} |
| 35 | + |
| 36 | +impl<ES: Deref, MQ: Deref> LSPS0ClientHandler<ES, MQ> |
| 37 | +where |
| 38 | + ES::Target: EntropySource, |
| 39 | + MQ::Target: MessageQueue, |
| 40 | +{ |
| 41 | + /// Returns a new instance of [`LSPS0ClientHandler`]. |
| 42 | + pub(crate) fn new( |
| 43 | + entropy_source: ES, pending_messages: MQ, pending_events: Arc<EventQueue>, |
| 44 | + ) -> Self { |
| 45 | + Self { entropy_source, pending_messages, pending_events } |
| 46 | + } |
| 47 | + |
| 48 | + /// Calls LSPS0's `list_protocols`. |
| 49 | + /// |
| 50 | + /// Please refer to the [LSPS0 |
| 51 | + /// specifcation](https://github.com/BitcoinAndLightningLayerSpecs/lsp/tree/main/LSPS0#lsps-specification-support-query) |
| 52 | + /// for more information. |
| 53 | + pub fn list_protocols(&self, counterparty_node_id: &PublicKey) { |
| 54 | + let msg = LSPS0Message::Request( |
| 55 | + utils::generate_request_id(&self.entropy_source), |
| 56 | + LSPS0Request::ListProtocols(ListProtocolsRequest {}), |
| 57 | + ); |
| 58 | + |
| 59 | + self.pending_messages.enqueue(counterparty_node_id, msg.into()); |
| 60 | + } |
| 61 | + |
| 62 | + fn handle_response( |
| 63 | + &self, response: LSPS0Response, counterparty_node_id: &PublicKey, |
| 64 | + ) -> Result<(), LightningError> { |
| 65 | + match response { |
| 66 | + LSPS0Response::ListProtocols(ListProtocolsResponse { protocols }) => { |
| 67 | + self.pending_events.enqueue(Event::LSPS0Client( |
| 68 | + LSPS0ClientEvent::ListProtocolsResponse { |
| 69 | + counterparty_node_id: *counterparty_node_id, |
| 70 | + protocols, |
| 71 | + }, |
| 72 | + )); |
| 73 | + Ok(()) |
| 74 | + } |
| 75 | + LSPS0Response::ListProtocolsError(ResponseError { code, message, data, .. }) => { |
| 76 | + Err(LightningError { |
| 77 | + err: format!( |
| 78 | + "ListProtocols error received. code = {}, message = {}, data = {:?}", |
| 79 | + code, message, data |
| 80 | + ), |
| 81 | + action: ErrorAction::IgnoreAndLog(Level::Info), |
| 82 | + }) |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +impl<ES: Deref, MQ: Deref> ProtocolMessageHandler for LSPS0ClientHandler<ES, MQ> |
| 89 | +where |
| 90 | + ES::Target: EntropySource, |
| 91 | + MQ::Target: MessageQueue, |
| 92 | +{ |
| 93 | + type ProtocolMessage = LSPS0Message; |
| 94 | + const PROTOCOL_NUMBER: Option<u16> = None; |
| 95 | + |
| 96 | + fn handle_message( |
| 97 | + &self, message: Self::ProtocolMessage, counterparty_node_id: &PublicKey, |
| 98 | + ) -> Result<(), LightningError> { |
| 99 | + match message { |
| 100 | + LSPS0Message::Response(_, response) => { |
| 101 | + self.handle_response(response, counterparty_node_id) |
| 102 | + } |
| 103 | + LSPS0Message::Request(..) => { |
| 104 | + debug_assert!( |
| 105 | + false, |
| 106 | + "Client handler received LSPS0 request message. This should never happen." |
| 107 | + ); |
| 108 | + Err(LightningError { err: format!("Client handler received LSPS0 request message from node {:?}. This should never happen.", counterparty_node_id), action: ErrorAction::IgnoreAndLog(Level::Info)}) |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +#[cfg(test)] |
| 115 | +mod tests { |
| 116 | + |
| 117 | + use alloc::string::ToString; |
| 118 | + use alloc::sync::Arc; |
| 119 | + |
| 120 | + use crate::lsps0::msgs::{LSPSMessage, RequestId}; |
| 121 | + use crate::tests::utils::{TestEntropy, TestMessageQueue}; |
| 122 | + |
| 123 | + use super::*; |
| 124 | + |
| 125 | + #[test] |
| 126 | + fn test_list_protocols() { |
| 127 | + let pending_messages = Arc::new(TestMessageQueue::new()); |
| 128 | + let entropy_source = Arc::new(TestEntropy {}); |
| 129 | + let event_queue = Arc::new(EventQueue::new()); |
| 130 | + |
| 131 | + let lsps0_handler = Arc::new(LSPS0ClientHandler::new( |
| 132 | + entropy_source, |
| 133 | + Arc::clone(&pending_messages), |
| 134 | + event_queue, |
| 135 | + )); |
| 136 | + |
| 137 | + let counterparty_node_id = utils::parse_pubkey( |
| 138 | + "027100442c3b79f606f80f322d98d499eefcb060599efc5d4ecb00209c2cb54190", |
| 139 | + ) |
| 140 | + .unwrap(); |
| 141 | + |
| 142 | + lsps0_handler.list_protocols(&counterparty_node_id); |
| 143 | + let pending_messages = pending_messages.get_and_clear_pending_msgs(); |
| 144 | + |
| 145 | + assert_eq!(pending_messages.len(), 1); |
| 146 | + |
| 147 | + let (pubkey, message) = &pending_messages[0]; |
| 148 | + |
| 149 | + assert_eq!(*pubkey, counterparty_node_id); |
| 150 | + assert_eq!( |
| 151 | + *message, |
| 152 | + LSPSMessage::LSPS0(LSPS0Message::Request( |
| 153 | + RequestId("00000000000000000000000000000000".to_string()), |
| 154 | + LSPS0Request::ListProtocols(ListProtocolsRequest {}) |
| 155 | + )) |
| 156 | + ); |
| 157 | + } |
| 158 | +} |
0 commit comments