Skip to content

Commit 7afe504

Browse files
committed
Generate TLV write length impls
Add a write-only TLV helper macro that emits both write and serialized_length from the same field list. Reuse the shared TLV length helper from impl_writeable_tlv_based so the existing generated writer path and the new custom-read path stay aligned. Use the new helper for the hot channel funding and commitment transaction TLV writers while leaving their custom read implementations unchanged.
1 parent 2a7f7dd commit 7afe504

3 files changed

Lines changed: 88 additions & 65 deletions

File tree

lightning/src/ln/chan_utils.rs

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,24 +1169,22 @@ impl_writeable_tlv_based!(CounterpartyChannelTransactionParameters, {
11691169
(2, selected_contest_delay, required),
11701170
});
11711171

1172-
impl Writeable for ChannelTransactionParameters {
1173-
#[rustfmt::skip]
1174-
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
1175-
let legacy_deserialization_prevention_marker = legacy_deserialization_prevention_marker_for_channel_type_features(&self.channel_type_features);
1176-
write_tlv_fields!(writer, {
1177-
(0, self.holder_pubkeys, required),
1178-
(2, self.holder_selected_contest_delay, required),
1179-
(4, self.is_outbound_from_holder, required),
1180-
(6, self.counterparty_parameters, option),
1181-
(8, self.funding_outpoint, option),
1182-
(10, legacy_deserialization_prevention_marker, option),
1183-
(11, self.channel_type_features, required),
1184-
(12, self.splice_parent_funding_txid, option),
1185-
(13, self.channel_value_satoshis, required),
1186-
});
1187-
Ok(())
1188-
}
1189-
}
1172+
impl_writeable_tlv_fields!(ChannelTransactionParameters, self, {
1173+
let legacy_deserialization_prevention_marker =
1174+
legacy_deserialization_prevention_marker_for_channel_type_features(
1175+
&self.channel_type_features,
1176+
);
1177+
}, {
1178+
(0, self.holder_pubkeys, required),
1179+
(2, self.holder_selected_contest_delay, required),
1180+
(4, self.is_outbound_from_holder, required),
1181+
(6, self.counterparty_parameters, option),
1182+
(8, self.funding_outpoint, option),
1183+
(10, legacy_deserialization_prevention_marker, option),
1184+
(11, self.channel_type_features, required),
1185+
(12, self.splice_parent_funding_txid, option),
1186+
(13, self.channel_value_satoshis, required),
1187+
});
11901188

11911189
impl ReadableArgs<Option<u64>> for ChannelTransactionParameters {
11921190
#[rustfmt::skip]
@@ -1634,25 +1632,23 @@ impl PartialEq for CommitmentTransaction {
16341632
}
16351633
}
16361634

1637-
impl Writeable for CommitmentTransaction {
1638-
#[rustfmt::skip]
1639-
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
1640-
let legacy_deserialization_prevention_marker = legacy_deserialization_prevention_marker_for_channel_type_features(&self.channel_type_features);
1641-
write_tlv_fields!(writer, {
1642-
(0, self.commitment_number, required),
1643-
(1, self.to_broadcaster_delay, option),
1644-
(2, self.to_broadcaster_value_sat, required),
1645-
(4, self.to_countersignatory_value_sat, required),
1646-
(6, self.feerate_per_kw, required),
1647-
(8, self.keys, required),
1648-
(10, self.built, required),
1649-
(12, self.nondust_htlcs, required_vec),
1650-
(14, legacy_deserialization_prevention_marker, option),
1651-
(15, self.channel_type_features, required),
1652-
});
1653-
Ok(())
1654-
}
1655-
}
1635+
impl_writeable_tlv_fields!(CommitmentTransaction, self, {
1636+
let legacy_deserialization_prevention_marker =
1637+
legacy_deserialization_prevention_marker_for_channel_type_features(
1638+
&self.channel_type_features,
1639+
);
1640+
}, {
1641+
(0, self.commitment_number, required),
1642+
(1, self.to_broadcaster_delay, option),
1643+
(2, self.to_broadcaster_value_sat, required),
1644+
(4, self.to_countersignatory_value_sat, required),
1645+
(6, self.feerate_per_kw, required),
1646+
(8, self.keys, required),
1647+
(10, self.built, required),
1648+
(12, self.nondust_htlcs, required_vec),
1649+
(14, legacy_deserialization_prevention_marker, option),
1650+
(15, self.channel_type_features, required),
1651+
});
16561652

16571653
impl Readable for CommitmentTransaction {
16581654
#[rustfmt::skip]

lightning/src/ln/channel.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2596,22 +2596,17 @@ pub(super) struct FundingScope {
25962596
minimum_depth_override: Option<u32>,
25972597
}
25982598

2599-
impl Writeable for FundingScope {
2600-
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
2601-
write_tlv_fields!(writer, {
2602-
(1, self.value_to_self_msat, required),
2603-
(3, self.counterparty_selected_channel_reserve_satoshis, option),
2604-
(5, self.holder_selected_channel_reserve_satoshis, required),
2605-
(7, self.channel_transaction_parameters, (required: ReadableArgs, None)),
2606-
(9, self.funding_transaction, option),
2607-
(11, self.funding_tx_confirmed_in, option),
2608-
(13, self.funding_tx_confirmation_height, required),
2609-
(15, self.short_channel_id, option),
2610-
(17, self.minimum_depth_override, option),
2611-
});
2612-
Ok(())
2613-
}
2614-
}
2599+
impl_writeable_tlv_fields!(FundingScope, self, {
2600+
(1, self.value_to_self_msat, required),
2601+
(3, self.counterparty_selected_channel_reserve_satoshis, option),
2602+
(5, self.holder_selected_channel_reserve_satoshis, required),
2603+
(7, self.channel_transaction_parameters, (required: ReadableArgs, None)),
2604+
(9, self.funding_transaction, option),
2605+
(11, self.funding_tx_confirmed_in, option),
2606+
(13, self.funding_tx_confirmation_height, required),
2607+
(15, self.short_channel_id, option),
2608+
(17, self.minimum_depth_override, option),
2609+
});
26152610

26162611
impl Readable for FundingScope {
26172612
#[rustfmt::skip]

lightning/src/util/ser_macros.rs

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,47 @@ macro_rules! write_tlv_fields {
854854
}
855855
}
856856

857+
#[doc(hidden)]
858+
#[macro_export]
859+
macro_rules! _tlv_fields_serialized_length {
860+
({$(($type: expr, $field: expr, $fieldty: tt $(, $self: ident)?)),* $(,)*}) => { {
861+
let len = {
862+
#[allow(unused_mut)]
863+
let mut len = $crate::util::ser::LengthCalculatingWriter(0);
864+
$(
865+
$crate::_get_varint_length_prefixed_tlv_length!(len, $type, &$field, $fieldty $(, $self)?);
866+
)*
867+
len.0
868+
};
869+
$crate::_bigsize_serialized_length!(len as u64) + len
870+
} }
871+
}
872+
873+
macro_rules! impl_writeable_tlv_fields {
874+
($st: ty, $self: ident, {$($type:tt)*}) => {
875+
impl_writeable_tlv_fields!($st, $self, {}, {$($type)*});
876+
};
877+
($st: ty, $self: ident, {$($init: tt)*}, {$(($type: expr, $field: expr, $fieldty: tt)),* $(,)*}) => {
878+
impl $crate::util::ser::Writeable for $st {
879+
fn write<W: $crate::util::ser::Writer>(&$self, writer: &mut W) -> Result<(), $crate::io::Error> {
880+
$($init)*
881+
write_tlv_fields!(writer, {
882+
$(($type, $field, $fieldty)),*
883+
});
884+
Ok(())
885+
}
886+
887+
#[inline]
888+
fn serialized_length(&$self) -> usize {
889+
$($init)*
890+
$crate::_tlv_fields_serialized_length!({
891+
$(($type, $field, $fieldty)),*
892+
})
893+
}
894+
}
895+
}
896+
}
897+
857898
/// Reads a prefix added by [`write_ver_prefix`], above. Takes the current version of the
858899
/// serialization logic for this object. This is compared against the
859900
/// `$min_version_that_can_read_this` added by [`write_ver_prefix`].
@@ -1110,18 +1151,9 @@ macro_rules! impl_writeable_tlv_based {
11101151

11111152
#[inline]
11121153
fn serialized_length(&self) -> usize {
1113-
use $crate::util::ser::BigSize;
1114-
let len = {
1115-
#[allow(unused_mut)]
1116-
let mut len = $crate::util::ser::LengthCalculatingWriter(0);
1117-
$(
1118-
$crate::_get_varint_length_prefixed_tlv_length!(len, $type, &self.$field, $fieldty, self);
1119-
)*
1120-
len.0
1121-
};
1122-
let mut len_calc = $crate::util::ser::LengthCalculatingWriter(0);
1123-
BigSize(len as u64).write(&mut len_calc).expect("No in-memory data may fail to serialize");
1124-
len + len_calc.0
1154+
$crate::_tlv_fields_serialized_length!({
1155+
$(($type, self.$field, $fieldty, self)),*
1156+
})
11251157
}
11261158
}
11271159

0 commit comments

Comments
 (0)