Skip to content

Commit e034a82

Browse files
committed
Implement serialization for event queue types
1 parent b424f57 commit e034a82

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

lightning-liquidity/src/events/mod.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ use crate::lsps1;
2525
use crate::lsps2;
2626
use crate::lsps5;
2727

28+
use lightning::io;
29+
use lightning::ln::msgs::DecodeError;
30+
use lightning::util::ser::{
31+
BigSize, FixedLengthReader, MaybeReadable, Readable, Writeable, Writer,
32+
};
33+
2834
/// An event which you should probably take some action in response to.
2935
#[derive(Debug, Clone, PartialEq, Eq)]
3036
pub enum LiquidityEvent {
@@ -87,3 +93,53 @@ impl From<lsps5::event::LSPS5ServiceEvent> for LiquidityEvent {
8793
Self::LSPS5Service(event)
8894
}
8995
}
96+
97+
impl Writeable for LiquidityEvent {
98+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
99+
match self {
100+
Self::LSPS0Client(_) => {},
101+
Self::LSPS1Client(_) => {},
102+
#[cfg(lsps1_service)]
103+
Self::LSPS1Service(_) => {},
104+
Self::LSPS2Client(_) => {},
105+
Self::LSPS2Service(event) => {
106+
0u8.write(writer)?;
107+
event.write(writer)?;
108+
},
109+
Self::LSPS5Client(_) => {},
110+
Self::LSPS5Service(event) => {
111+
2u8.write(writer)?;
112+
event.write(writer)?;
113+
},
114+
}
115+
Ok(())
116+
}
117+
}
118+
119+
impl MaybeReadable for LiquidityEvent {
120+
fn read<R: io::Read>(reader: &mut R) -> Result<Option<Self>, DecodeError> {
121+
match Readable::read(reader)? {
122+
0u8 => {
123+
let event = Readable::read(reader)?;
124+
Ok(Some(LiquidityEvent::LSPS2Service(event)))
125+
},
126+
2u8 => {
127+
let event = Readable::read(reader)?;
128+
Ok(Some(LiquidityEvent::LSPS5Service(event)))
129+
},
130+
x if x % 2 == 1 => {
131+
// If the event is of unknown type, assume it was written with `write_tlv_fields`,
132+
// which prefixes the whole thing with a length BigSize. Because the event is
133+
// odd-type unknown, we should treat it as `Ok(None)` even if it has some TLV
134+
// fields that are even. Thus, we avoid using `read_tlv_fields` and simply read
135+
// exactly the number of bytes specified, ignoring them entirely.
136+
let tlv_len: BigSize = Readable::read(reader)?;
137+
FixedLengthReader::new(reader, tlv_len.0)
138+
.eat_remaining()
139+
.map_err(|_| DecodeError::ShortRead)?;
140+
Ok(None)
141+
},
142+
_ => Err(DecodeError::InvalidValue),
143+
}
144+
}
145+
}

lightning-liquidity/src/lsps2/event.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ use alloc::vec::Vec;
1616

1717
use bitcoin::secp256k1::PublicKey;
1818

19+
use lightning::impl_writeable_tlv_based_enum;
20+
1921
/// An event which an LSPS2 client should take some action in response to.
2022
#[derive(Clone, Debug, PartialEq, Eq)]
2123
pub enum LSPS2ClientEvent {
@@ -161,3 +163,24 @@ pub enum LSPS2ServiceEvent {
161163
intercept_scid: u64,
162164
},
163165
}
166+
167+
impl_writeable_tlv_based_enum!(LSPS2ServiceEvent,
168+
(0, GetInfo) => {
169+
(0, request_id, required),
170+
(2, counterparty_node_id, required),
171+
(4, token, option),
172+
},
173+
(2, BuyRequest) => {
174+
(0, request_id, required),
175+
(2, counterparty_node_id, required),
176+
(4, opening_fee_params, required),
177+
(6, payment_size_msat, option),
178+
},
179+
(4, OpenChannel) => {
180+
(0, their_network_key, required),
181+
(2, amt_to_forward_msat, required),
182+
(4, opening_fee_msat, required),
183+
(6, user_channel_id, required),
184+
(8, intercept_scid, required),
185+
}
186+
);

lightning-liquidity/src/lsps5/event.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ use crate::lsps0::ser::LSPSRequestId;
1313
use alloc::string::String;
1414
use alloc::vec::Vec;
1515
use bitcoin::secp256k1::PublicKey;
16+
17+
use lightning::impl_writeable_tlv_based_enum;
1618
use lightning::util::hash_tables::HashMap;
1719

1820
use super::msgs::LSPS5AppName;
@@ -70,6 +72,16 @@ pub enum LSPS5ServiceEvent {
7072
},
7173
}
7274

75+
impl_writeable_tlv_based_enum!(LSPS5ServiceEvent,
76+
(0, SendWebhookNotification) => {
77+
(0, counterparty_node_id, required),
78+
(2, app_name, required),
79+
(4, url, required),
80+
(6, notification, required),
81+
(8, headers, required),
82+
}
83+
);
84+
7385
/// An event which an LSPS5 client should take some action in response to.
7486
#[derive(Debug, Clone, PartialEq, Eq)]
7587
pub enum LSPS5ClientEvent {

lightning-liquidity/src/lsps5/msgs.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use super::url_utils::LSPSUrl;
1818

1919
use lightning::ln::msgs::DecodeError;
2020
use lightning::util::ser::{Readable, Writeable};
21+
use lightning::{impl_writeable_tlv_based, impl_writeable_tlv_based_enum};
2122
use lightning_types::string::UntrustedString;
2223

2324
use serde::de::{self, Deserializer, MapAccess, Visitor};
@@ -522,6 +523,16 @@ pub enum WebhookNotificationMethod {
522523
LSPS5OnionMessageIncoming,
523524
}
524525

526+
impl_writeable_tlv_based_enum!(WebhookNotificationMethod,
527+
(0, LSPS5WebhookRegistered) => {},
528+
(2, LSPS5PaymentIncoming) => {},
529+
(4, LSPS5ExpirySoon) => {
530+
(0, timeout, required),
531+
},
532+
(6, LSPS5LiquidityManagementRequest) => {},
533+
(8, LSPS5OnionMessageIncoming) => {},
534+
);
535+
525536
/// Webhook notification payload.
526537
#[derive(Debug, Clone, PartialEq, Eq)]
527538
pub struct WebhookNotification {
@@ -672,6 +683,10 @@ impl<'de> Deserialize<'de> for WebhookNotification {
672683
}
673684
}
674685

686+
impl_writeable_tlv_based!(WebhookNotification, {
687+
(0, method, required),
688+
});
689+
675690
/// An LSPS5 protocol request.
676691
#[derive(Clone, Debug, PartialEq, Eq)]
677692
pub enum LSPS5Request {

0 commit comments

Comments
 (0)