Skip to content

Commit 997dc5f

Browse files
committed
Convert most P2P msg serialization to a new macro with TLV suffixes
The network serialization format for all messages was changed some time ago to include a TLV suffix for all messages, however we never bothered to implement it as there isn't a lot of use validating a TLV stream with nothing to do with it. However, messages are increasingly utilizing the TLV suffix feature, and there are some compatibility concerns with messages written as a part of other structs having their format changed (see previous commit). Thus, here we go ahead and convert most message serialization to a new macro which includes a TLV suffix after a series of fields, simplifying several serialization implementations in the process.
1 parent f60da31 commit 997dc5f

File tree

2 files changed

+76
-114
lines changed

2 files changed

+76
-114
lines changed

lightning/src/ln/msgs.rs

+53-114
Original file line numberDiff line numberDiff line change
@@ -1053,7 +1053,7 @@ impl Readable for OptionalField<u64> {
10531053
}
10541054

10551055

1056-
impl_writeable!(AcceptChannel, {
1056+
impl_writeable_msg!(AcceptChannel, {
10571057
temporary_channel_id,
10581058
dust_limit_satoshis,
10591059
max_htlc_value_in_flight_msat,
@@ -1069,14 +1069,14 @@ impl_writeable!(AcceptChannel, {
10691069
htlc_basepoint,
10701070
first_per_commitment_point,
10711071
shutdown_scriptpubkey
1072-
});
1072+
}, {});
10731073

1074-
impl_writeable!(AnnouncementSignatures, {
1074+
impl_writeable_msg!(AnnouncementSignatures, {
10751075
channel_id,
10761076
short_channel_id,
10771077
node_signature,
10781078
bitcoin_signature
1079-
});
1079+
}, {});
10801080

10811081
impl Writeable for ChannelReestablish {
10821082
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
@@ -1115,64 +1115,44 @@ impl Readable for ChannelReestablish{
11151115
}
11161116
}
11171117

1118-
impl Writeable for ClosingSigned {
1119-
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
1120-
self.channel_id.write(w)?;
1121-
self.fee_satoshis.write(w)?;
1122-
self.signature.write(w)?;
1123-
encode_tlv_stream!(w, {
1124-
(1, self.fee_range, option),
1125-
});
1126-
Ok(())
1127-
}
1128-
}
1129-
1130-
impl Readable for ClosingSigned {
1131-
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
1132-
let channel_id = Readable::read(r)?;
1133-
let fee_satoshis = Readable::read(r)?;
1134-
let signature = Readable::read(r)?;
1135-
let mut fee_range = None;
1136-
decode_tlv_stream!(r, {
1137-
(1, fee_range, option),
1138-
});
1139-
Ok(Self { channel_id, fee_satoshis, signature, fee_range })
1140-
}
1141-
}
1118+
impl_writeable_msg!(ClosingSigned,
1119+
{ channel_id, fee_satoshis, signature },
1120+
{ (1, fee_range, option) }
1121+
);
11421122

11431123
impl_writeable!(ClosingSignedFeeRange, {
11441124
min_fee_satoshis,
11451125
max_fee_satoshis
11461126
});
11471127

1148-
impl_writeable!(CommitmentSigned, {
1128+
impl_writeable_msg!(CommitmentSigned, {
11491129
channel_id,
11501130
signature,
11511131
htlc_signatures
1152-
});
1132+
}, {});
11531133

11541134
impl_writeable!(DecodedOnionErrorPacket, {
11551135
hmac,
11561136
failuremsg,
11571137
pad
11581138
});
11591139

1160-
impl_writeable!(FundingCreated, {
1140+
impl_writeable_msg!(FundingCreated, {
11611141
temporary_channel_id,
11621142
funding_txid,
11631143
funding_output_index,
11641144
signature
1165-
});
1145+
}, {});
11661146

1167-
impl_writeable!(FundingSigned, {
1147+
impl_writeable_msg!(FundingSigned, {
11681148
channel_id,
11691149
signature
1170-
});
1150+
}, {});
11711151

1172-
impl_writeable!(FundingLocked, {
1152+
impl_writeable_msg!(FundingLocked, {
11731153
channel_id,
1174-
next_per_commitment_point
1175-
});
1154+
next_per_commitment_point,
1155+
}, {});
11761156

11771157
impl Writeable for Init {
11781158
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
@@ -1193,7 +1173,7 @@ impl Readable for Init {
11931173
}
11941174
}
11951175

1196-
impl_writeable!(OpenChannel, {
1176+
impl_writeable_msg!(OpenChannel, {
11971177
chain_hash,
11981178
temporary_channel_id,
11991179
funding_satoshis,
@@ -1213,47 +1193,53 @@ impl_writeable!(OpenChannel, {
12131193
first_per_commitment_point,
12141194
channel_flags,
12151195
shutdown_scriptpubkey
1216-
});
1196+
}, {});
12171197

1218-
impl_writeable!(RevokeAndACK, {
1198+
impl_writeable_msg!(RevokeAndACK, {
12191199
channel_id,
12201200
per_commitment_secret,
12211201
next_per_commitment_point
1222-
});
1202+
}, {});
12231203

1224-
impl_writeable!(Shutdown, {
1204+
impl_writeable_msg!(Shutdown, {
12251205
channel_id,
12261206
scriptpubkey
1227-
});
1207+
}, {});
12281208

1229-
impl_writeable!(UpdateFailHTLC, {
1209+
impl_writeable_msg!(UpdateFailHTLC, {
12301210
channel_id,
12311211
htlc_id,
12321212
reason
1233-
});
1213+
}, {});
12341214

1235-
impl_writeable!(UpdateFailMalformedHTLC, {
1215+
impl_writeable_msg!(UpdateFailMalformedHTLC, {
12361216
channel_id,
12371217
htlc_id,
12381218
sha256_of_onion,
12391219
failure_code
1240-
});
1220+
}, {});
12411221

1242-
impl_writeable!(UpdateFee, {
1222+
impl_writeable_msg!(UpdateFee, {
12431223
channel_id,
12441224
feerate_per_kw
1245-
});
1225+
}, {});
12461226

1247-
impl_writeable!(UpdateFulfillHTLC, {
1227+
impl_writeable_msg!(UpdateFulfillHTLC, {
12481228
channel_id,
12491229
htlc_id,
12501230
payment_preimage
1251-
});
1231+
}, {});
12521232

1233+
// Note that this is written as a part of ChannelManager objects, and thus cannot change its
1234+
// serialization format in a way which assumes we know the total serialized length/message end
1235+
// position.
12531236
impl_writeable!(OnionErrorPacket, {
12541237
data
12551238
});
12561239

1240+
// Note that this is written as a part of ChannelManager objects, and thus cannot change its
1241+
// serialization format in a way which assumes we know the total serialized length/message end
1242+
// position.
12571243
impl Writeable for OnionPacket {
12581244
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
12591245
self.version.write(w)?;
@@ -1282,14 +1268,14 @@ impl Readable for OnionPacket {
12821268
}
12831269
}
12841270

1285-
impl_writeable!(UpdateAddHTLC, {
1271+
impl_writeable_msg!(UpdateAddHTLC, {
12861272
channel_id,
12871273
htlc_id,
12881274
amount_msat,
12891275
payment_hash,
12901276
cltv_expiry,
12911277
onion_routing_packet
1292-
});
1278+
}, {});
12931279

12941280
impl Writeable for FinalOnionHopData {
12951281
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
@@ -1700,24 +1686,10 @@ impl Writeable for QueryShortChannelIds {
17001686
}
17011687
}
17021688

1703-
impl Readable for ReplyShortChannelIdsEnd {
1704-
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
1705-
let chain_hash: BlockHash = Readable::read(r)?;
1706-
let full_information: bool = Readable::read(r)?;
1707-
Ok(ReplyShortChannelIdsEnd {
1708-
chain_hash,
1709-
full_information,
1710-
})
1711-
}
1712-
}
1713-
1714-
impl Writeable for ReplyShortChannelIdsEnd {
1715-
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
1716-
self.chain_hash.write(w)?;
1717-
self.full_information.write(w)?;
1718-
Ok(())
1719-
}
1720-
}
1689+
impl_writeable_msg!(ReplyShortChannelIdsEnd, {
1690+
chain_hash,
1691+
full_information,
1692+
}, {});
17211693

17221694
impl QueryChannelRange {
17231695
/**
@@ -1732,27 +1704,11 @@ impl QueryChannelRange {
17321704
}
17331705
}
17341706

1735-
impl Readable for QueryChannelRange {
1736-
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
1737-
let chain_hash: BlockHash = Readable::read(r)?;
1738-
let first_blocknum: u32 = Readable::read(r)?;
1739-
let number_of_blocks: u32 = Readable::read(r)?;
1740-
Ok(QueryChannelRange {
1741-
chain_hash,
1742-
first_blocknum,
1743-
number_of_blocks
1744-
})
1745-
}
1746-
}
1747-
1748-
impl Writeable for QueryChannelRange {
1749-
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
1750-
self.chain_hash.write(w)?;
1751-
self.first_blocknum.write(w)?;
1752-
self.number_of_blocks.write(w)?;
1753-
Ok(())
1754-
}
1755-
}
1707+
impl_writeable_msg!(QueryChannelRange, {
1708+
chain_hash,
1709+
first_blocknum,
1710+
number_of_blocks
1711+
}, {});
17561712

17571713
impl Readable for ReplyChannelRange {
17581714
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
@@ -1812,28 +1768,11 @@ impl Writeable for ReplyChannelRange {
18121768
}
18131769
}
18141770

1815-
impl Readable for GossipTimestampFilter {
1816-
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
1817-
let chain_hash: BlockHash = Readable::read(r)?;
1818-
let first_timestamp: u32 = Readable::read(r)?;
1819-
let timestamp_range: u32 = Readable::read(r)?;
1820-
Ok(GossipTimestampFilter {
1821-
chain_hash,
1822-
first_timestamp,
1823-
timestamp_range,
1824-
})
1825-
}
1826-
}
1827-
1828-
impl Writeable for GossipTimestampFilter {
1829-
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
1830-
self.chain_hash.write(w)?;
1831-
self.first_timestamp.write(w)?;
1832-
self.timestamp_range.write(w)?;
1833-
Ok(())
1834-
}
1835-
}
1836-
1771+
impl_writeable_msg!(GossipTimestampFilter, {
1772+
chain_hash,
1773+
first_timestamp,
1774+
timestamp_range,
1775+
}, {});
18371776

18381777
#[cfg(test)]
18391778
mod tests {

lightning/src/util/ser_macros.rs

+23
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,29 @@ macro_rules! decode_tlv_stream {
230230
} }
231231
}
232232

233+
macro_rules! impl_writeable_msg {
234+
($st:ident, {$($field:ident),* $(,)*}, {$(($type: expr, $tlvfield: ident, $fieldty: tt)),* $(,)*}) => {
235+
impl ::util::ser::Writeable for $st {
236+
fn write<W: ::util::ser::Writer>(&self, w: &mut W) -> Result<(), $crate::io::Error> {
237+
$( self.$field.write(w)?; )*
238+
encode_tlv_stream!(w, {$(($type, self.$tlvfield, $fieldty)),*});
239+
Ok(())
240+
}
241+
}
242+
impl ::util::ser::Readable for $st {
243+
fn read<R: $crate::io::Read>(r: &mut R) -> Result<Self, ::ln::msgs::DecodeError> {
244+
$(let $field = ::util::ser::Readable::read(r)?;)*
245+
$(init_tlv_field_var!($tlvfield, $fieldty);)*
246+
decode_tlv_stream!(r, {$(($type, $tlvfield, $fieldty)),*});
247+
Ok(Self {
248+
$($field),*,
249+
$($tlvfield),*
250+
})
251+
}
252+
}
253+
}
254+
}
255+
233256
macro_rules! impl_writeable {
234257
($st:ident, {$($field:ident),*}) => {
235258
impl ::util::ser::Writeable for $st {

0 commit comments

Comments
 (0)