Skip to content

Commit 32846d3

Browse files
committed
Add a required_vec TLV deserialization type
Historically, we used `vec_type` for all TLV Vec reads/writes, but it is asymmetric and thus somewhat confusing - on the write side it always writes a TLV entry, even if there are zero elements. On the read side, it happily accepts a missing TLV, providing a zero-length vector. In 85b573d a new `optional_vec` TLV format was added which was symmetric, but only supports optional vecs. This adds the corresponding required form, always writing a TLV and ensuring it is present.
1 parent 46913da commit 32846d3

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

lightning/src/util/ser_macros.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ macro_rules! _encode_tlv {
2929
BigSize($field.serialized_length() as u64).write($stream)?;
3030
$field.write($stream)?;
3131
};
32+
($stream: expr, $type: expr, $field: expr, required_vec) => {
33+
$crate::_encode_tlv!($stream, $type, $crate::util::ser::WithoutLength(&$field), required);
34+
};
3235
($stream: expr, $type: expr, $field: expr, vec_type) => {
3336
$crate::_encode_tlv!($stream, $type, $crate::util::ser::WithoutLength(&$field), required);
3437
};
@@ -41,7 +44,7 @@ macro_rules! _encode_tlv {
4144
};
4245
($stream: expr, $type: expr, $field: expr, optional_vec) => {
4346
if !$field.is_empty() {
44-
$crate::_encode_tlv!($stream, $type, $field, vec_type);
47+
$crate::_encode_tlv!($stream, $type, $field, required_vec);
4548
}
4649
};
4750
($stream: expr, $type: expr, $field: expr, upgradable_required) => {
@@ -159,6 +162,9 @@ macro_rules! _get_varint_length_prefixed_tlv_length {
159162
BigSize(field_len as u64).write(&mut $len).expect("No in-memory data may fail to serialize");
160163
$len.0 += field_len;
161164
};
165+
($len: expr, $type: expr, $field: expr, required_vec) => {
166+
$crate::_get_varint_length_prefixed_tlv_length!($len, $type, $crate::util::ser::WithoutLength(&$field), required);
167+
};
162168
($len: expr, $type: expr, $field: expr, vec_type) => {
163169
$crate::_get_varint_length_prefixed_tlv_length!($len, $type, $crate::util::ser::WithoutLength(&$field), required);
164170
};
@@ -172,7 +178,7 @@ macro_rules! _get_varint_length_prefixed_tlv_length {
172178
};
173179
($len: expr, $type: expr, $field: expr, optional_vec) => {
174180
if !$field.is_empty() {
175-
$crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, vec_type);
181+
$crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, required_vec);
176182
}
177183
};
178184
($len: expr, $type: expr, $field: expr, (option: $trait: ident $(, $read_arg: expr)?)) => {
@@ -236,6 +242,9 @@ macro_rules! _check_decoded_tlv_order {
236242
($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, option) => {{
237243
// no-op
238244
}};
245+
($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, required_vec) => {{
246+
$crate::_check_decoded_tlv_order!($last_seen_type, $typ, $type, $field, required);
247+
}};
239248
($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, vec_type) => {{
240249
// no-op
241250
}};
@@ -281,6 +290,9 @@ macro_rules! _check_missing_tlv {
281290
($last_seen_type: expr, $type: expr, $field: ident, (required: $trait: ident $(, $read_arg: expr)?)) => {{
282291
$crate::_check_missing_tlv!($last_seen_type, $type, $field, required);
283292
}};
293+
($last_seen_type: expr, $type: expr, $field: ident, required_vec) => {{
294+
$crate::_check_missing_tlv!($last_seen_type, $type, $field, required);
295+
}};
284296
($last_seen_type: expr, $type: expr, $field: ident, vec_type) => {{
285297
// no-op
286298
}};
@@ -320,6 +332,10 @@ macro_rules! _decode_tlv {
320332
($reader: expr, $field: ident, (required: $trait: ident $(, $read_arg: expr)?)) => {{
321333
$field = $trait::read(&mut $reader $(, $read_arg)*)?;
322334
}};
335+
($reader: expr, $field: ident, required_vec) => {{
336+
let f: $crate::util::ser::WithoutLength<Vec<_>> = $crate::util::ser::Readable::read(&mut $reader)?;
337+
$field = f.0;
338+
}};
323339
($reader: expr, $field: ident, vec_type) => {{
324340
let f: $crate::util::ser::WithoutLength<Vec<_>> = $crate::util::ser::Readable::read(&mut $reader)?;
325341
$field = Some(f.0);
@@ -694,6 +710,9 @@ macro_rules! _init_tlv_based_struct_field {
694710
($field: ident, required) => {
695711
$field.0.unwrap()
696712
};
713+
($field: ident, required_vec) => {
714+
$field
715+
};
697716
($field: ident, vec_type) => {
698717
$field.unwrap()
699718
};
@@ -720,6 +739,9 @@ macro_rules! _init_tlv_field_var {
720739
($field: ident, (required: $trait: ident $(, $read_arg: expr)?)) => {
721740
$crate::_init_tlv_field_var!($field, required);
722741
};
742+
($field: ident, required_vec) => {
743+
let mut $field = Vec::new();
744+
};
723745
($field: ident, vec_type) => {
724746
let mut $field = Some(Vec::new());
725747
};

0 commit comments

Comments
 (0)