Skip to content

Commit 7e8e4bd

Browse files
Add new _init_and_decode_tlv_stream ser macro
Useful for when you want to use _init_and_read_tlv_fields but there is no length byte at the start of the TLV stream.
1 parent edfe9c8 commit 7e8e4bd

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

lightning/src/onion_message/packet.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use bitcoin::secp256k1::ecdh::SharedSecret;
1414

1515
use crate::blinded_path::BlindedPath;
1616
use crate::blinded_path::message::{ForwardTlvs, ReceiveTlvs};
17-
use crate::blinded_path::utils::Padding;
1817
use crate::ln::msgs::DecodeError;
1918
use crate::ln::onion_utils;
2019
use super::messenger::CustomOnionMessageHandler;
@@ -274,13 +273,8 @@ pub(crate) enum ControlTlvs {
274273
}
275274

276275
impl Readable for ControlTlvs {
277-
fn read<R: Read>(mut r: &mut R) -> Result<Self, DecodeError> {
278-
let mut _padding: Option<Padding> = None;
279-
let mut _short_channel_id: Option<u64> = None;
280-
let mut next_node_id: Option<PublicKey> = None;
281-
let mut path_id: Option<[u8; 32]> = None;
282-
let mut next_blinding_override: Option<PublicKey> = None;
283-
decode_tlv_stream!(&mut r, {
276+
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
277+
_init_and_decode_tlv_stream!(r, {
284278
(1, _padding, option),
285279
(2, _short_channel_id, option),
286280
(4, next_node_id, option),

lightning/src/util/ser_macros.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,23 @@ macro_rules! _init_and_read_tlv_fields {
776776
}
777777
}
778778

779+
/// Equivalent to running [`_init_tlv_field_var`] then [`decode_tlv_stream`].
780+
///
781+
/// This is exported for use by other exported macros, do not use directly.
782+
#[doc(hidden)]
783+
#[macro_export]
784+
macro_rules! _init_and_decode_tlv_stream {
785+
($reader: ident, {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}) => {
786+
$(
787+
$crate::_init_tlv_field_var!($field, $fieldty);
788+
)*
789+
790+
$crate::decode_tlv_stream!($reader, {
791+
$(($type, $field, $fieldty)),*
792+
});
793+
}
794+
}
795+
779796
/// Implements [`Readable`]/[`Writeable`] for a struct storing it as a set of TLVs
780797
/// If `$fieldty` is `required`, then `$field` is a required field that is not an [`Option`] nor a [`Vec`].
781798
/// If `$fieldty` is `(default_value, $default)`, then `$field` will be set to `$default` if not present.

0 commit comments

Comments
 (0)