Skip to content

Commit 3a33693

Browse files
committed
Expose impl_writeable_tlv_based macro
Every exported macro needed to have all the macros used inside it: 1- to be exported as well. 2- be called from the `$crate` namespace so it works in other crates. Some structs in `lightning::util::ser` needed to be made public as they were used inside the exported macros. Use the macros like this: ```Rust lightning::impl_writeable_tlv_based!(...) ```
1 parent b79ff71 commit 3a33693

File tree

8 files changed

+317
-129
lines changed

8 files changed

+317
-129
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6049,7 +6049,7 @@ impl Writeable for ChannelDetails {
60496049

60506050
impl Readable for ChannelDetails {
60516051
fn read<R: Read>(reader: &mut R) -> Result<Self, DecodeError> {
6052-
init_and_read_tlv_fields!(reader, {
6052+
_init_and_read_tlv_fields!(reader, {
60536053
(1, inbound_scid_alias, option),
60546054
(2, channel_id, required),
60556055
(3, channel_type, option),

lightning/src/ln/msgs.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -821,8 +821,8 @@ pub struct CommitmentUpdate {
821821
}
822822

823823
/// Messages could have optional fields to use with extended features
824-
/// As we wish to serialize these differently from Option<T>s (Options get a tag byte, but
825-
/// OptionalFeild simply gets Present if there are enough bytes to read into it), we have a
824+
/// As we wish to serialize these differently from `Option<T>`s (`Options` get a tag byte, but
825+
/// [`OptionalField`] simply gets `Present` if there are enough bytes to read into it), we have a
826826
/// separate enum type for them.
827827
/// (C-not exported) due to a free generic in T
828828
#[derive(Clone, Debug, PartialEq, Eq)]
@@ -1455,14 +1455,14 @@ impl Writeable for OnionHopData {
14551455
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
14561456
match self.format {
14571457
OnionHopDataFormat::NonFinalNode { short_channel_id } => {
1458-
encode_varint_length_prefixed_tlv!(w, {
1458+
_encode_varint_length_prefixed_tlv!(w, {
14591459
(2, HighZeroBytesDroppedBigSize(self.amt_to_forward), required),
14601460
(4, HighZeroBytesDroppedBigSize(self.outgoing_cltv_value), required),
14611461
(6, short_channel_id, required)
14621462
});
14631463
},
14641464
OnionHopDataFormat::FinalNode { ref payment_data, ref keysend_preimage } => {
1465-
encode_varint_length_prefixed_tlv!(w, {
1465+
_encode_varint_length_prefixed_tlv!(w, {
14661466
(2, HighZeroBytesDroppedBigSize(self.amt_to_forward), required),
14671467
(4, HighZeroBytesDroppedBigSize(self.outgoing_cltv_value), required),
14681468
(8, payment_data, option),
@@ -2875,7 +2875,7 @@ mod tests {
28752875
let mut encoded_payload = Vec::new();
28762876
let test_bytes = vec![42u8; 1000];
28772877
if let OnionHopDataFormat::NonFinalNode { short_channel_id } = payload.format {
2878-
encode_varint_length_prefixed_tlv!(&mut encoded_payload, {
2878+
_encode_varint_length_prefixed_tlv!(&mut encoded_payload, {
28792879
(1, test_bytes, vec_type),
28802880
(2, HighZeroBytesDroppedBigSize(payload.amt_to_forward), required),
28812881
(4, HighZeroBytesDroppedBigSize(payload.outgoing_cltv_value), required),

lightning/src/onion_message/packet.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,30 +166,30 @@ impl<T: CustomOnionMessageContents> Writeable for (Payload<T>, [u8; 32]) {
166166
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
167167
match &self.0 {
168168
Payload::Forward(ForwardControlTlvs::Blinded(encrypted_bytes)) => {
169-
encode_varint_length_prefixed_tlv!(w, {
169+
_encode_varint_length_prefixed_tlv!(w, {
170170
(4, *encrypted_bytes, vec_type)
171171
})
172172
},
173173
Payload::Receive {
174174
control_tlvs: ReceiveControlTlvs::Blinded(encrypted_bytes), reply_path, message,
175175
} => {
176-
encode_varint_length_prefixed_tlv!(w, {
176+
_encode_varint_length_prefixed_tlv!(w, {
177177
(2, reply_path, option),
178178
(4, *encrypted_bytes, vec_type),
179179
(message.tlv_type(), message, required)
180180
})
181181
},
182182
Payload::Forward(ForwardControlTlvs::Unblinded(control_tlvs)) => {
183183
let write_adapter = ChaChaPolyWriteAdapter::new(self.1, &control_tlvs);
184-
encode_varint_length_prefixed_tlv!(w, {
184+
_encode_varint_length_prefixed_tlv!(w, {
185185
(4, write_adapter, required)
186186
})
187187
},
188188
Payload::Receive {
189189
control_tlvs: ReceiveControlTlvs::Unblinded(control_tlvs), reply_path, message,
190190
} => {
191191
let write_adapter = ChaChaPolyWriteAdapter::new(self.1, &control_tlvs);
192-
encode_varint_length_prefixed_tlv!(w, {
192+
_encode_varint_length_prefixed_tlv!(w, {
193193
(2, reply_path, option),
194194
(4, write_adapter, required),
195195
(message.tlv_type(), message, required)
@@ -212,7 +212,7 @@ impl<H: CustomOnionMessageHandler> ReadableArgs<(SharedSecret, &H)> for Payload<
212212
let rho = onion_utils::gen_rho_from_shared_secret(&encrypted_tlvs_ss.secret_bytes());
213213
let mut message_type: Option<u64> = None;
214214
let mut message = None;
215-
decode_tlv_stream!(&mut rd, {
215+
decode_tlv_stream_with_custom_tlv_decode!(&mut rd, {
216216
(2, reply_path, option),
217217
(4, read_adapter, (option: LengthReadableArgs, rho)),
218218
}, |msg_type, msg_reader| {

lightning/src/routing/gossip.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -673,13 +673,13 @@ impl Writeable for ChannelUpdateInfo {
673673

674674
impl Readable for ChannelUpdateInfo {
675675
fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
676-
init_tlv_field_var!(last_update, required);
677-
init_tlv_field_var!(enabled, required);
678-
init_tlv_field_var!(cltv_expiry_delta, required);
679-
init_tlv_field_var!(htlc_minimum_msat, required);
680-
init_tlv_field_var!(htlc_maximum_msat, option);
681-
init_tlv_field_var!(fees, required);
682-
init_tlv_field_var!(last_update_message, required);
676+
_init_tlv_field_var!(last_update, required);
677+
_init_tlv_field_var!(enabled, required);
678+
_init_tlv_field_var!(cltv_expiry_delta, required);
679+
_init_tlv_field_var!(htlc_minimum_msat, required);
680+
_init_tlv_field_var!(htlc_maximum_msat, option);
681+
_init_tlv_field_var!(fees, required);
682+
_init_tlv_field_var!(last_update_message, required);
683683

684684
read_tlv_fields!(reader, {
685685
(0, last_update, required),
@@ -693,13 +693,13 @@ impl Readable for ChannelUpdateInfo {
693693

694694
if let Some(htlc_maximum_msat) = htlc_maximum_msat {
695695
Ok(ChannelUpdateInfo {
696-
last_update: init_tlv_based_struct_field!(last_update, required),
697-
enabled: init_tlv_based_struct_field!(enabled, required),
698-
cltv_expiry_delta: init_tlv_based_struct_field!(cltv_expiry_delta, required),
699-
htlc_minimum_msat: init_tlv_based_struct_field!(htlc_minimum_msat, required),
696+
last_update: _init_tlv_based_struct_field!(last_update, required),
697+
enabled: _init_tlv_based_struct_field!(enabled, required),
698+
cltv_expiry_delta: _init_tlv_based_struct_field!(cltv_expiry_delta, required),
699+
htlc_minimum_msat: _init_tlv_based_struct_field!(htlc_minimum_msat, required),
700700
htlc_maximum_msat,
701-
fees: init_tlv_based_struct_field!(fees, required),
702-
last_update_message: init_tlv_based_struct_field!(last_update_message, required),
701+
fees: _init_tlv_based_struct_field!(fees, required),
702+
last_update_message: _init_tlv_based_struct_field!(last_update_message, required),
703703
})
704704
} else {
705705
Err(DecodeError::InvalidValue)
@@ -820,14 +820,14 @@ impl MaybeReadable for ChannelUpdateInfoDeserWrapper {
820820

821821
impl Readable for ChannelInfo {
822822
fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
823-
init_tlv_field_var!(features, required);
824-
init_tlv_field_var!(announcement_received_time, (default_value, 0));
825-
init_tlv_field_var!(node_one, required);
823+
_init_tlv_field_var!(features, required);
824+
_init_tlv_field_var!(announcement_received_time, (default_value, 0));
825+
_init_tlv_field_var!(node_one, required);
826826
let mut one_to_two_wrap: Option<ChannelUpdateInfoDeserWrapper> = None;
827-
init_tlv_field_var!(node_two, required);
827+
_init_tlv_field_var!(node_two, required);
828828
let mut two_to_one_wrap: Option<ChannelUpdateInfoDeserWrapper> = None;
829-
init_tlv_field_var!(capacity_sats, required);
830-
init_tlv_field_var!(announcement_message, required);
829+
_init_tlv_field_var!(capacity_sats, required);
830+
_init_tlv_field_var!(announcement_message, required);
831831
read_tlv_fields!(reader, {
832832
(0, features, required),
833833
(1, announcement_received_time, (default_value, 0)),
@@ -840,14 +840,14 @@ impl Readable for ChannelInfo {
840840
});
841841

842842
Ok(ChannelInfo {
843-
features: init_tlv_based_struct_field!(features, required),
844-
node_one: init_tlv_based_struct_field!(node_one, required),
843+
features: _init_tlv_based_struct_field!(features, required),
844+
node_one: _init_tlv_based_struct_field!(node_one, required),
845845
one_to_two: one_to_two_wrap.map(|w| w.0).unwrap_or(None),
846-
node_two: init_tlv_based_struct_field!(node_two, required),
846+
node_two: _init_tlv_based_struct_field!(node_two, required),
847847
two_to_one: two_to_one_wrap.map(|w| w.0).unwrap_or(None),
848-
capacity_sats: init_tlv_based_struct_field!(capacity_sats, required),
849-
announcement_message: init_tlv_based_struct_field!(announcement_message, required),
850-
announcement_received_time: init_tlv_based_struct_field!(announcement_received_time, (default_value, 0)),
848+
capacity_sats: _init_tlv_based_struct_field!(capacity_sats, required),
849+
announcement_message: _init_tlv_based_struct_field!(announcement_message, required),
850+
announcement_received_time: _init_tlv_based_struct_field!(announcement_received_time, (default_value, 0)),
851851
})
852852
}
853853
}
@@ -1103,9 +1103,9 @@ impl MaybeReadable for NodeAnnouncementInfoDeserWrapper {
11031103

11041104
impl Readable for NodeInfo {
11051105
fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
1106-
init_tlv_field_var!(lowest_inbound_channel_fees, option);
1106+
_init_tlv_field_var!(lowest_inbound_channel_fees, option);
11071107
let mut announcement_info_wrap: Option<NodeAnnouncementInfoDeserWrapper> = None;
1108-
init_tlv_field_var!(channels, vec_type);
1108+
_init_tlv_field_var!(channels, vec_type);
11091109

11101110
read_tlv_fields!(reader, {
11111111
(0, lowest_inbound_channel_fees, option),
@@ -1114,9 +1114,9 @@ impl Readable for NodeInfo {
11141114
});
11151115

11161116
Ok(NodeInfo {
1117-
lowest_inbound_channel_fees: init_tlv_based_struct_field!(lowest_inbound_channel_fees, option),
1117+
lowest_inbound_channel_fees: _init_tlv_based_struct_field!(lowest_inbound_channel_fees, option),
11181118
announcement_info: announcement_info_wrap.map(|w| w.0),
1119-
channels: init_tlv_based_struct_field!(channels, vec_type),
1119+
channels: _init_tlv_based_struct_field!(channels, vec_type),
11201120
})
11211121
}
11221122
}

lightning/src/util/chacha20poly1305rfc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ mod tests {
411411
#[test]
412412
fn chacha_stream_adapters_ser_macros() {
413413
// Test that our stream adapters work as expected with the TLV macros.
414-
// This also serves to test the `option: $trait` variant of the `decode_tlv` ser macro.
414+
// This also serves to test the `option: $trait` variant of the `_decode_tlv` ser macro.
415415
do_chacha_stream_adapters_ser_macros().unwrap()
416416
}
417417
}

lightning/src/util/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
pub(crate) mod fuzz_wrappers;
1414

1515
#[macro_use]
16-
pub(crate) mod ser_macros;
16+
pub mod ser_macros;
1717

1818
pub mod events;
1919
pub mod errors;

lightning/src/util/ser.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl Writer for VecWriter {
8484

8585
/// Writer that only tracks the amount of data written - useful if you need to calculate the length
8686
/// of some data when serialized but don't yet need the full data.
87-
pub(crate) struct LengthCalculatingWriter(pub usize);
87+
pub struct LengthCalculatingWriter(pub usize);
8888
impl Writer for LengthCalculatingWriter {
8989
#[inline]
9090
fn write_all(&mut self, buf: &[u8]) -> Result<(), io::Error> {
@@ -93,23 +93,26 @@ impl Writer for LengthCalculatingWriter {
9393
}
9494
}
9595

96-
/// Essentially std::io::Take but a bit simpler and with a method to walk the underlying stream
96+
/// Essentially [`std::io::Take`] but a bit simpler and with a method to walk the underlying stream
9797
/// forward to ensure we always consume exactly the fixed length specified.
98-
pub(crate) struct FixedLengthReader<R: Read> {
98+
pub struct FixedLengthReader<R: Read> {
9999
read: R,
100100
bytes_read: u64,
101101
total_bytes: u64,
102102
}
103103
impl<R: Read> FixedLengthReader<R> {
104+
/// Returns a new [`FixedLengthReader`].
104105
pub fn new(read: R, total_bytes: u64) -> Self {
105106
Self { read, bytes_read: 0, total_bytes }
106107
}
107108

109+
/// Returns whether some bytes are remaining or not.
108110
#[inline]
109111
pub fn bytes_remain(&mut self) -> bool {
110112
self.bytes_read != self.total_bytes
111113
}
112114

115+
/// Consumes the remaining bytes.
113116
#[inline]
114117
pub fn eat_remaining(&mut self) -> Result<(), DecodeError> {
115118
copy(self, &mut sink()).unwrap();
@@ -145,13 +148,15 @@ impl<R: Read> LengthRead for FixedLengthReader<R> {
145148
}
146149
}
147150

148-
/// A Read which tracks whether any bytes have been read at all. This allows us to distinguish
151+
/// A [`Read`] implementation which tracks whether any bytes have been read at all. This allows us to distinguish
149152
/// between "EOF reached before we started" and "EOF reached mid-read".
150-
pub(crate) struct ReadTrackingReader<R: Read> {
153+
pub struct ReadTrackingReader<R: Read> {
151154
read: R,
155+
/// Returns whether we have read from this reader or not yet.
152156
pub have_read: bool,
153157
}
154158
impl<R: Read> ReadTrackingReader<R> {
159+
/// Returns a new [`ReadTrackingReader`].
155160
pub fn new(read: R) -> Self {
156161
Self { read, have_read: false }
157162
}
@@ -278,7 +283,8 @@ impl<T: Readable> MaybeReadable for T {
278283
}
279284
}
280285

281-
pub(crate) struct OptionDeserWrapper<T: Readable>(pub Option<T>);
286+
/// Wrapper to read a required (non-optional) TLV record.
287+
pub struct OptionDeserWrapper<T: Readable>(pub Option<T>);
282288
impl<T: Readable> Readable for OptionDeserWrapper<T> {
283289
#[inline]
284290
fn read<R: Read>(reader: &mut R) -> Result<Self, DecodeError> {
@@ -528,7 +534,7 @@ impl Readable for [u16; 8] {
528534

529535
/// For variable-length values within TLV record where the length is encoded as part of the record.
530536
/// Used to prevent encoding the length twice.
531-
pub(crate) struct WithoutLength<T>(pub T);
537+
pub struct WithoutLength<T>(pub T);
532538

533539
impl Writeable for WithoutLength<&String> {
534540
#[inline]

0 commit comments

Comments
 (0)