Skip to content

Expose impl_writeable_tlv_based macro #1823

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6049,7 +6049,7 @@ impl Writeable for ChannelDetails {

impl Readable for ChannelDetails {
fn read<R: Read>(reader: &mut R) -> Result<Self, DecodeError> {
init_and_read_tlv_fields!(reader, {
_init_and_read_tlv_fields!(reader, {
(1, inbound_scid_alias, option),
(2, channel_id, required),
(3, channel_type, option),
Expand Down
10 changes: 5 additions & 5 deletions lightning/src/ln/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -821,8 +821,8 @@ pub struct CommitmentUpdate {
}

/// Messages could have optional fields to use with extended features
/// As we wish to serialize these differently from Option<T>s (Options get a tag byte, but
/// OptionalFeild simply gets Present if there are enough bytes to read into it), we have a
/// As we wish to serialize these differently from `Option<T>`s (`Options` get a tag byte, but
/// [`OptionalField`] simply gets `Present` if there are enough bytes to read into it), we have a
/// separate enum type for them.
/// (C-not exported) due to a free generic in T
#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -1455,14 +1455,14 @@ impl Writeable for OnionHopData {
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
match self.format {
OnionHopDataFormat::NonFinalNode { short_channel_id } => {
encode_varint_length_prefixed_tlv!(w, {
_encode_varint_length_prefixed_tlv!(w, {
(2, HighZeroBytesDroppedBigSize(self.amt_to_forward), required),
(4, HighZeroBytesDroppedBigSize(self.outgoing_cltv_value), required),
(6, short_channel_id, required)
});
},
OnionHopDataFormat::FinalNode { ref payment_data, ref keysend_preimage } => {
encode_varint_length_prefixed_tlv!(w, {
_encode_varint_length_prefixed_tlv!(w, {
(2, HighZeroBytesDroppedBigSize(self.amt_to_forward), required),
(4, HighZeroBytesDroppedBigSize(self.outgoing_cltv_value), required),
(8, payment_data, option),
Expand Down Expand Up @@ -2875,7 +2875,7 @@ mod tests {
let mut encoded_payload = Vec::new();
let test_bytes = vec![42u8; 1000];
if let OnionHopDataFormat::NonFinalNode { short_channel_id } = payload.format {
encode_varint_length_prefixed_tlv!(&mut encoded_payload, {
_encode_varint_length_prefixed_tlv!(&mut encoded_payload, {
(1, test_bytes, vec_type),
(2, HighZeroBytesDroppedBigSize(payload.amt_to_forward), required),
(4, HighZeroBytesDroppedBigSize(payload.outgoing_cltv_value), required),
Expand Down
10 changes: 5 additions & 5 deletions lightning/src/onion_message/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,30 +166,30 @@ impl<T: CustomOnionMessageContents> Writeable for (Payload<T>, [u8; 32]) {
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
match &self.0 {
Payload::Forward(ForwardControlTlvs::Blinded(encrypted_bytes)) => {
encode_varint_length_prefixed_tlv!(w, {
_encode_varint_length_prefixed_tlv!(w, {
(4, *encrypted_bytes, vec_type)
})
},
Payload::Receive {
control_tlvs: ReceiveControlTlvs::Blinded(encrypted_bytes), reply_path, message,
} => {
encode_varint_length_prefixed_tlv!(w, {
_encode_varint_length_prefixed_tlv!(w, {
(2, reply_path, option),
(4, *encrypted_bytes, vec_type),
(message.tlv_type(), message, required)
})
},
Payload::Forward(ForwardControlTlvs::Unblinded(control_tlvs)) => {
let write_adapter = ChaChaPolyWriteAdapter::new(self.1, &control_tlvs);
encode_varint_length_prefixed_tlv!(w, {
_encode_varint_length_prefixed_tlv!(w, {
(4, write_adapter, required)
})
},
Payload::Receive {
control_tlvs: ReceiveControlTlvs::Unblinded(control_tlvs), reply_path, message,
} => {
let write_adapter = ChaChaPolyWriteAdapter::new(self.1, &control_tlvs);
encode_varint_length_prefixed_tlv!(w, {
_encode_varint_length_prefixed_tlv!(w, {
(2, reply_path, option),
(4, write_adapter, required),
(message.tlv_type(), message, required)
Expand All @@ -212,7 +212,7 @@ impl<H: CustomOnionMessageHandler> ReadableArgs<(SharedSecret, &H)> for Payload<
let rho = onion_utils::gen_rho_from_shared_secret(&encrypted_tlvs_ss.secret_bytes());
let mut message_type: Option<u64> = None;
let mut message = None;
decode_tlv_stream!(&mut rd, {
decode_tlv_stream_with_custom_tlv_decode!(&mut rd, {
(2, reply_path, option),
(4, read_adapter, (option: LengthReadableArgs, rho)),
}, |msg_type, msg_reader| {
Expand Down
58 changes: 29 additions & 29 deletions lightning/src/routing/gossip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,13 +673,13 @@ impl Writeable for ChannelUpdateInfo {

impl Readable for ChannelUpdateInfo {
fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
init_tlv_field_var!(last_update, required);
init_tlv_field_var!(enabled, required);
init_tlv_field_var!(cltv_expiry_delta, required);
init_tlv_field_var!(htlc_minimum_msat, required);
init_tlv_field_var!(htlc_maximum_msat, option);
init_tlv_field_var!(fees, required);
init_tlv_field_var!(last_update_message, required);
_init_tlv_field_var!(last_update, required);
_init_tlv_field_var!(enabled, required);
_init_tlv_field_var!(cltv_expiry_delta, required);
_init_tlv_field_var!(htlc_minimum_msat, required);
_init_tlv_field_var!(htlc_maximum_msat, option);
_init_tlv_field_var!(fees, required);
_init_tlv_field_var!(last_update_message, required);

read_tlv_fields!(reader, {
(0, last_update, required),
Expand All @@ -693,13 +693,13 @@ impl Readable for ChannelUpdateInfo {

if let Some(htlc_maximum_msat) = htlc_maximum_msat {
Ok(ChannelUpdateInfo {
last_update: init_tlv_based_struct_field!(last_update, required),
enabled: init_tlv_based_struct_field!(enabled, required),
cltv_expiry_delta: init_tlv_based_struct_field!(cltv_expiry_delta, required),
htlc_minimum_msat: init_tlv_based_struct_field!(htlc_minimum_msat, required),
last_update: _init_tlv_based_struct_field!(last_update, required),
enabled: _init_tlv_based_struct_field!(enabled, required),
cltv_expiry_delta: _init_tlv_based_struct_field!(cltv_expiry_delta, required),
htlc_minimum_msat: _init_tlv_based_struct_field!(htlc_minimum_msat, required),
htlc_maximum_msat,
fees: init_tlv_based_struct_field!(fees, required),
last_update_message: init_tlv_based_struct_field!(last_update_message, required),
fees: _init_tlv_based_struct_field!(fees, required),
last_update_message: _init_tlv_based_struct_field!(last_update_message, required),
})
} else {
Err(DecodeError::InvalidValue)
Expand Down Expand Up @@ -820,14 +820,14 @@ impl MaybeReadable for ChannelUpdateInfoDeserWrapper {

impl Readable for ChannelInfo {
fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
init_tlv_field_var!(features, required);
init_tlv_field_var!(announcement_received_time, (default_value, 0));
init_tlv_field_var!(node_one, required);
_init_tlv_field_var!(features, required);
_init_tlv_field_var!(announcement_received_time, (default_value, 0));
_init_tlv_field_var!(node_one, required);
let mut one_to_two_wrap: Option<ChannelUpdateInfoDeserWrapper> = None;
init_tlv_field_var!(node_two, required);
_init_tlv_field_var!(node_two, required);
let mut two_to_one_wrap: Option<ChannelUpdateInfoDeserWrapper> = None;
init_tlv_field_var!(capacity_sats, required);
init_tlv_field_var!(announcement_message, required);
_init_tlv_field_var!(capacity_sats, required);
_init_tlv_field_var!(announcement_message, required);
read_tlv_fields!(reader, {
(0, features, required),
(1, announcement_received_time, (default_value, 0)),
Expand All @@ -840,14 +840,14 @@ impl Readable for ChannelInfo {
});

Ok(ChannelInfo {
features: init_tlv_based_struct_field!(features, required),
node_one: init_tlv_based_struct_field!(node_one, required),
features: _init_tlv_based_struct_field!(features, required),
node_one: _init_tlv_based_struct_field!(node_one, required),
one_to_two: one_to_two_wrap.map(|w| w.0).unwrap_or(None),
node_two: init_tlv_based_struct_field!(node_two, required),
node_two: _init_tlv_based_struct_field!(node_two, required),
two_to_one: two_to_one_wrap.map(|w| w.0).unwrap_or(None),
capacity_sats: init_tlv_based_struct_field!(capacity_sats, required),
announcement_message: init_tlv_based_struct_field!(announcement_message, required),
announcement_received_time: init_tlv_based_struct_field!(announcement_received_time, (default_value, 0)),
capacity_sats: _init_tlv_based_struct_field!(capacity_sats, required),
announcement_message: _init_tlv_based_struct_field!(announcement_message, required),
announcement_received_time: _init_tlv_based_struct_field!(announcement_received_time, (default_value, 0)),
})
}
}
Expand Down Expand Up @@ -1103,9 +1103,9 @@ impl MaybeReadable for NodeAnnouncementInfoDeserWrapper {

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

read_tlv_fields!(reader, {
(0, lowest_inbound_channel_fees, option),
Expand All @@ -1114,9 +1114,9 @@ impl Readable for NodeInfo {
});

Ok(NodeInfo {
lowest_inbound_channel_fees: init_tlv_based_struct_field!(lowest_inbound_channel_fees, option),
lowest_inbound_channel_fees: _init_tlv_based_struct_field!(lowest_inbound_channel_fees, option),
announcement_info: announcement_info_wrap.map(|w| w.0),
channels: init_tlv_based_struct_field!(channels, vec_type),
channels: _init_tlv_based_struct_field!(channels, vec_type),
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion lightning/src/util/chacha20poly1305rfc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ mod tests {
#[test]
fn chacha_stream_adapters_ser_macros() {
// Test that our stream adapters work as expected with the TLV macros.
// This also serves to test the `option: $trait` variant of the `decode_tlv` ser macro.
// This also serves to test the `option: $trait` variant of the `_decode_tlv` ser macro.
do_chacha_stream_adapters_ser_macros().unwrap()
}
}
2 changes: 1 addition & 1 deletion lightning/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
pub(crate) mod fuzz_wrappers;

#[macro_use]
pub(crate) mod ser_macros;
pub mod ser_macros;

pub mod events;
pub mod errors;
Expand Down
20 changes: 13 additions & 7 deletions lightning/src/util/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl Writer for VecWriter {

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

/// Essentially std::io::Take but a bit simpler and with a method to walk the underlying stream
/// Essentially [`std::io::Take`] but a bit simpler and with a method to walk the underlying stream
/// forward to ensure we always consume exactly the fixed length specified.
pub(crate) struct FixedLengthReader<R: Read> {
pub struct FixedLengthReader<R: Read> {
read: R,
bytes_read: u64,
total_bytes: u64,
}
impl<R: Read> FixedLengthReader<R> {
/// Returns a new [`FixedLengthReader`].
pub fn new(read: R, total_bytes: u64) -> Self {
Self { read, bytes_read: 0, total_bytes }
}

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

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

/// A Read which tracks whether any bytes have been read at all. This allows us to distinguish
/// A [`Read`] implementation which tracks whether any bytes have been read at all. This allows us to distinguish
/// between "EOF reached before we started" and "EOF reached mid-read".
pub(crate) struct ReadTrackingReader<R: Read> {
pub struct ReadTrackingReader<R: Read> {
read: R,
/// Returns whether we have read from this reader or not yet.
pub have_read: bool,
}
impl<R: Read> ReadTrackingReader<R> {
/// Returns a new [`ReadTrackingReader`].
pub fn new(read: R) -> Self {
Self { read, have_read: false }
}
Expand Down Expand Up @@ -278,7 +283,8 @@ impl<T: Readable> MaybeReadable for T {
}
}

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

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

impl Writeable for WithoutLength<&String> {
#[inline]
Expand Down
Loading