Skip to content

Commit a57f64d

Browse files
refactor(payment): add _init_and_read_tlv_stream_with_custom_tlv_decode macro
Add a new _init_and_read_tlv_stream_with_custom_tlv_decode macro that combines _init_tlv_field_var and decode_tlv_stream_with_custom_tlv_decode, mirroring how _init_and_read_tlv_stream combines init with decode_tlv_stream. This avoids manually breaking out field var initialization from the TLV stream decoding in BlindedPaymentTlvs and BlindedTrampolineTlvs. Signed-off-by: Asish Kumar <officialasishkumar@gmail.com>
1 parent 27f6289 commit a57f64d

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

lightning/src/blinded_path/payment.rs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -632,15 +632,7 @@ impl<'a> Writeable for BlindedPaymentTlvsRef<'a> {
632632

633633
impl Readable for BlindedPaymentTlvs {
634634
fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
635-
_init_tlv_field_var!(scid, option);
636-
_init_tlv_field_var!(next_blinding_override, option);
637-
_init_tlv_field_var!(payment_relay, option);
638-
_init_tlv_field_var!(payment_constraints, required);
639-
_init_tlv_field_var!(features, (option, encoding: (BlindedHopFeatures, WithoutLength)));
640-
_init_tlv_field_var!(payment_secret, option);
641-
_init_tlv_field_var!(payment_context, option);
642-
_init_tlv_field_var!(is_dummy, option);
643-
decode_tlv_stream_with_custom_tlv_decode!(r, {
635+
_init_and_read_tlv_stream_with_custom_tlv_decode!(r, {
644636
// Reasoning: Padding refers to filler data added to a packet to increase
645637
// its size and obscure its actual length. Since padding contains no meaningful
646638
// information, we can safely omit reading it here.
@@ -701,14 +693,7 @@ impl Readable for BlindedPaymentTlvs {
701693

702694
impl Readable for BlindedTrampolineTlvs {
703695
fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
704-
_init_tlv_field_var!(next_trampoline, option);
705-
_init_tlv_field_var!(next_blinding_override, option);
706-
_init_tlv_field_var!(payment_relay, option);
707-
_init_tlv_field_var!(payment_constraints, required);
708-
_init_tlv_field_var!(features, (option, encoding: (BlindedHopFeatures, WithoutLength)));
709-
_init_tlv_field_var!(payment_secret, option);
710-
_init_tlv_field_var!(payment_context, option);
711-
decode_tlv_stream_with_custom_tlv_decode!(r, {
696+
_init_and_read_tlv_stream_with_custom_tlv_decode!(r, {
712697
(4, next_trampoline, option),
713698
(8, next_blinding_override, option),
714699
(10, payment_relay, option),

lightning/src/util/ser_macros.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,23 @@ macro_rules! _init_and_read_tlv_stream {
10151015
}
10161016
}
10171017

1018+
/// Equivalent to running [`_init_tlv_field_var`] then
1019+
/// [`decode_tlv_stream_with_custom_tlv_decode`].
1020+
///
1021+
/// If any unused values are read, their type MUST be specified or else `rustc` will read them as an
1022+
/// `i64`.
1023+
macro_rules! _init_and_read_tlv_stream_with_custom_tlv_decode {
1024+
($reader: ident, {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}
1025+
$(, $decode_custom_tlv: expr)?) => {
1026+
$(
1027+
$crate::_init_tlv_field_var!($field, $fieldty);
1028+
)*
1029+
decode_tlv_stream_with_custom_tlv_decode!($reader, {
1030+
$(($type, $field, $fieldty)),*
1031+
} $(, $decode_custom_tlv)?);
1032+
}
1033+
}
1034+
10181035
/// Reads a TLV stream with the given fields to build a struct/enum variant of type `$thing`
10191036
#[doc(hidden)]
10201037
#[macro_export]

0 commit comments

Comments
 (0)