Skip to content

Commit b7e88d4

Browse files
committed
WIP: Add a public Type trait
1 parent 817c6bb commit b7e88d4

File tree

1 file changed

+60
-36
lines changed

1 file changed

+60
-36
lines changed

teos-common/src/lightning_messages.rs

Lines changed: 60 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1+
//! Watchtower custom lightning messages that implement LDK's Readable & Writeable traits.
2+
3+
use crate::appointment::{Locator, LOCATOR_LEN};
14
use bitcoin::secp256k1::PublicKey;
25
use bitcoin::Txid;
3-
use lightning::ln::msgs::DecodeError;
4-
use lightning::ln::peer_handler::{CustomMessageHandler, PeerManager};
5-
use lightning::ln::wire::{CustomMessageReader, Type};
6+
use lightning::ln::msgs;
7+
use lightning::ln::wire;
68
use lightning::util::ser;
7-
8-
// To use (de)serialization macros.
9-
use lightning::*;
10-
11-
use crate::appointment::{Locator, LOCATOR_LEN};
9+
use std::io;
1210

1311
/// A wrapper around a vector inside a lightning message.
1412
/// This wrapper mainly exists because we cannot implement LDK's (de)serialization traits
@@ -24,13 +22,27 @@ impl<T> std::ops::Deref for LightningVec<T> {
2422
}
2523
}
2624

27-
// fmt the inner Vec.
25+
// Only format the inner Vec.
2826
impl<T: std::fmt::Debug> std::fmt::Debug for LightningVec<T> {
2927
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3028
self.0.fmt(f)
3129
}
3230
}
3331

32+
/// A trait that associates a u16 [`Type::TYPE`] constant with a lightning message.
33+
pub trait Type {
34+
/// The type identifying the message payload.
35+
const TYPE: u16;
36+
}
37+
38+
macro_rules! set_msg_type {
39+
($st: ident, $type: expr) => {
40+
impl Type for $st {
41+
const TYPE: u16 = $type;
42+
}
43+
};
44+
}
45+
3446
/// The register message sent by the user to subscribe for the watching service.
3547
#[derive(Debug)]
3648
pub struct Register {
@@ -122,11 +134,11 @@ pub struct UserSubscriptionInfo {
122134

123135
// Deserialization for a Locator inside a lightning message.
124136
impl ser::Readable for Locator {
125-
fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
137+
fn read<R: io::Read>(reader: &mut R) -> Result<Self, msgs::DecodeError> {
126138
let mut buf = [0; LOCATOR_LEN];
127139
reader.read_exact(&mut buf)?;
128140
let locator =
129-
Self::deserialize(&buf).map_err(|_| DecodeError::Io(io::ErrorKind::InvalidData))?;
141+
Self::deserialize(&buf).map_err(|_| msgs::DecodeError::Io(io::ErrorKind::InvalidData))?;
130142
Ok(locator)
131143
}
132144
}
@@ -141,7 +153,7 @@ impl ser::Writeable for Locator {
141153

142154
// Deserialization for a vector of Locators inside a lightning message.
143155
impl ser::Readable for LightningVec<Locator> {
144-
fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
156+
fn read<R: io::Read>(reader: &mut R) -> Result<Self, msgs::DecodeError> {
145157
let len: u16 = ser::Readable::read(reader)?;
146158
let mut locators = Vec::with_capacity(len as usize);
147159
for _ in 0..len {
@@ -162,70 +174,83 @@ impl ser::Writeable for LightningVec<Locator> {
162174
}
163175
}
164176

165-
impl_writeable_msg!(Register, {
177+
lightning::impl_writeable_msg!(Register, {
166178
pubkey,
167179
appointment_slots,
168180
subscription_period
169181
}, {});
170182

171-
impl_writeable_msg!(SubscriptionDetails, {
183+
lightning::impl_writeable_msg!(SubscriptionDetails, {
172184
appointment_max_size,
173185
amount_msat,
174186
}, {
175187
(1, signature, option),
176188
});
177189

178-
impl_writeable_msg!(AddUpdateAppointment, {
190+
lightning::impl_writeable_msg!(AddUpdateAppointment, {
179191
locator,
180192
encrypted_blob,
181193
signature,
182194
}, {
183195
(1, to_self_delay, option),
184196
});
185197

186-
impl_writeable_msg!(AppointmentAccepted, {
198+
lightning::impl_writeable_msg!(AppointmentAccepted, {
187199
locator,
188200
start_block,
189201
}, {
190202
(1, receipt_signature, option),
191203
});
192204

193-
impl_writeable_msg!(AppointmentRejected, {
205+
lightning::impl_writeable_msg!(AppointmentRejected, {
194206
locator,
195207
rcode,
196208
reason,
197209
}, {});
198210

199-
impl_writeable_msg!(GetAppointment, {
211+
lightning::impl_writeable_msg!(GetAppointment, {
200212
locator,
201213
signature,
202214
}, {});
203215

204-
impl_writeable_msg!(AppointmentFound, {
216+
lightning::impl_writeable_msg!(AppointmentFound, {
205217
locator,
206218
encrypted_blob,
207219
}, {});
208220

209-
impl_writeable_msg!(TrackerFound, {
221+
lightning::impl_writeable_msg!(TrackerFound, {
210222
dispute_txid,
211223
penalty_txid,
212224
penalty_rawtx,
213225
}, {});
214226

215-
impl_writeable_msg!(AppointmentNotFound, {
227+
lightning::impl_writeable_msg!(AppointmentNotFound, {
216228
locator,
217229
}, {});
218230

219-
impl_writeable_msg!(GetSubscriptionInfo, {
231+
lightning::impl_writeable_msg!(GetSubscriptionInfo, {
220232
signature,
221233
}, {});
222234

223-
impl_writeable_msg!(UserSubscriptionInfo, {
235+
lightning::impl_writeable_msg!(UserSubscriptionInfo, {
224236
available_slots,
225237
subscription_expiry,
226238
locators,
227239
}, {});
228240

241+
set_msg_type!(Register, 48848);
242+
set_msg_type!(SubscriptionDetails, 48850);
243+
set_msg_type!(AddUpdateAppointment, 48852);
244+
set_msg_type!(AppointmentAccepted, 48854);
245+
set_msg_type!(AppointmentRejected, 48856);
246+
set_msg_type!(GetAppointment, 48858);
247+
set_msg_type!(AppointmentFound, 48860);
248+
set_msg_type!(TrackerFound, 48862);
249+
set_msg_type!(AppointmentNotFound, 48864);
250+
// Let these messages get odd types since they are auxiliary messages.
251+
set_msg_type!(GetSubscriptionInfo, 48865);
252+
set_msg_type!(UserSubscriptionInfo, 48867);
253+
229254
#[derive(Debug)]
230255
pub enum TowerMessage {
231256
// Register messages
@@ -245,21 +270,20 @@ pub enum TowerMessage {
245270
UserSubscriptionInfo(UserSubscriptionInfo),
246271
}
247272

248-
impl Type for TowerMessage {
273+
impl wire::Type for TowerMessage {
249274
fn type_id(&self) -> u16 {
250275
match self {
251-
TowerMessage::Register(..) => 48848,
252-
TowerMessage::SubscriptionDetails(..) => 48850,
253-
TowerMessage::AddUpdateAppointment(..) => 48852,
254-
TowerMessage::AppointmentAccepted(..) => 48854,
255-
TowerMessage::AppointmentRejected(..) => 48856,
256-
TowerMessage::GetAppointment(..) => 48858,
257-
TowerMessage::AppointmentFound(..) => 48860,
258-
TowerMessage::TrackerFound(..) => 48862,
259-
TowerMessage::AppointmentNotFound(..) => 48864,
260-
// Let these messages get odd types since they are auxiliary messages.
261-
TowerMessage::GetSubscriptionInfo(..) => 48865,
262-
TowerMessage::UserSubscriptionInfo(..) => 48867,
276+
TowerMessage::Register(..) => Register::TYPE,
277+
TowerMessage::SubscriptionDetails(..) => SubscriptionDetails::TYPE,
278+
TowerMessage::AddUpdateAppointment(..) => AddUpdateAppointment::TYPE,
279+
TowerMessage::AppointmentAccepted(..) => AppointmentAccepted::TYPE,
280+
TowerMessage::AppointmentRejected(..) => AppointmentRejected::TYPE,
281+
TowerMessage::GetAppointment(..) => GetAppointment::TYPE,
282+
TowerMessage::AppointmentFound(..) => AppointmentFound::TYPE,
283+
TowerMessage::TrackerFound(..) => TrackerFound::TYPE,
284+
TowerMessage::AppointmentNotFound(..) => AppointmentNotFound::TYPE,
285+
TowerMessage::GetSubscriptionInfo(..) => GetSubscriptionInfo::TYPE,
286+
TowerMessage::UserSubscriptionInfo(..) => UserSubscriptionInfo::TYPE,
263287
}
264288
}
265289
}

0 commit comments

Comments
 (0)