Skip to content

Commit 4e5a813

Browse files
committed
Provide a reset_on_reload TLV field serialization type
This is useful in the type serialization definition macros to avoid writing or reading a field at all, simply using a static value on each reload.
1 parent fc9a4c2 commit 4e5a813

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

lightning/src/util/ser_macros.rs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ macro_rules! encode_tlv {
1111
($stream: expr, $type: expr, $field: expr, (default_value, $default: expr)) => {
1212
encode_tlv!($stream, $type, $field, required)
1313
};
14+
($stream: expr, $type: expr, $field: expr, (reset_on_reload, $value: expr)) => {
15+
let _ = &$field; // Ensure we "use" the $field
16+
};
1417
($stream: expr, $type: expr, $field: expr, required) => {
1518
BigSize($type).write($stream)?;
1619
BigSize($field.serialized_length() as u64).write($stream)?;
@@ -28,6 +31,17 @@ macro_rules! encode_tlv {
2831
};
2932
}
3033

34+
35+
macro_rules! _check_tlv_ordering {
36+
($last_type: expr, $type: expr, (reset_on_reload, $value: expr)) => { };
37+
($last_type: expr, $type: expr, $fieldty: tt) => {
38+
if let Some(t) = $last_type {
39+
debug_assert!(t <= $type);
40+
}
41+
$last_type = Some($type);
42+
};
43+
}
44+
3145
macro_rules! encode_tlv_stream {
3246
($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt)),* $(,)*}) => { {
3347
#[allow(unused_imports)]
@@ -46,10 +60,7 @@ macro_rules! encode_tlv_stream {
4660
{
4761
let mut last_seen: Option<u64> = None;
4862
$(
49-
if let Some(t) = last_seen {
50-
debug_assert!(t <= $type);
51-
}
52-
last_seen = Some($type);
63+
_check_tlv_ordering!(last_seen, $type, $fieldty);
5364
)*
5465
}
5566
} }
@@ -59,6 +70,8 @@ macro_rules! get_varint_length_prefixed_tlv_length {
5970
($len: expr, $type: expr, $field: expr, (default_value, $default: expr)) => {
6071
get_varint_length_prefixed_tlv_length!($len, $type, $field, required)
6172
};
73+
($len: expr, $type: expr, $field: expr, (reset_on_reload, $value: expr)) => {
74+
};
6275
($len: expr, $type: expr, $field: expr, required) => {
6376
BigSize($type).write(&mut $len).expect("No in-memory data may fail to serialize");
6477
let field_len = $field.serialized_length();
@@ -102,6 +115,8 @@ macro_rules! check_tlv_order {
102115
$field = $default.into();
103116
}
104117
}};
118+
($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, (reset_on_reload, $value: expr)) => {
119+
};
105120
($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, required) => {{
106121
#[allow(unused_comparisons)] // Note that $type may be 0 making the second comparison always true
107122
let invalid_order = ($last_seen_type.is_none() || $last_seen_type.unwrap() < $type) && $typ.0 > $type;
@@ -131,6 +146,9 @@ macro_rules! check_missing_tlv {
131146
$field = $default.into();
132147
}
133148
}};
149+
($last_seen_type: expr, $type: expr, $field: expr, (reset_on_reload, $value: expr)) => {
150+
$field = $value;
151+
};
134152
($last_seen_type: expr, $type: expr, $field: ident, required) => {{
135153
#[allow(unused_comparisons)] // Note that $type may be 0 making the second comparison always true
136154
let missing_req_type = $last_seen_type.is_none() || $last_seen_type.unwrap() < $type;
@@ -156,6 +174,8 @@ macro_rules! decode_tlv {
156174
($reader: expr, $field: ident, (default_value, $default: expr)) => {{
157175
decode_tlv!($reader, $field, required)
158176
}};
177+
($reader: expr, $field: ident, (reset_on_reload, $value: expr)) => {{
178+
}};
159179
($reader: expr, $field: ident, required) => {{
160180
$field = $crate::util::ser::Readable::read(&mut $reader)?;
161181
}};
@@ -174,6 +194,11 @@ macro_rules! decode_tlv {
174194
}};
175195
}
176196

197+
macro_rules! _decode_tlv_stream_match_check {
198+
($val: ident, $type: expr, (reset_on_reload, $value: expr)) => { false };
199+
($val: ident, $type: expr, $fieldty: tt) => { $val == $type }
200+
}
201+
177202
// `$decode_custom_tlv` is a closure that may be optionally provided to handle custom message types.
178203
// If it is provided, it will be called with the custom type and the `FixedLengthReader` containing
179204
// the message contents. It should return `Ok(true)` if the custom message is successfully parsed,
@@ -224,7 +249,7 @@ macro_rules! decode_tlv_stream {
224249
let length: ser::BigSize = $crate::util::ser::Readable::read(&mut stream_ref)?;
225250
let mut s = ser::FixedLengthReader::new(&mut stream_ref, length.0);
226251
match typ.0 {
227-
$($type => {
252+
$(_t if _decode_tlv_stream_match_check!(_t, $type, $fieldty) => {
228253
decode_tlv!(s, $field, $fieldty);
229254
if s.bytes_remain() {
230255
s.eat_remaining()?; // Return ShortRead if there's actually not enough bytes
@@ -365,6 +390,9 @@ macro_rules! init_tlv_based_struct_field {
365390
($field: ident, (default_value, $default: expr)) => {
366391
$field.0.unwrap()
367392
};
393+
($field: ident, (reset_on_reload, $value: expr)) => {
394+
$field
395+
};
368396
($field: ident, option) => {
369397
$field
370398
};
@@ -380,6 +408,9 @@ macro_rules! init_tlv_field_var {
380408
($field: ident, (default_value, $default: expr)) => {
381409
let mut $field = $crate::util::ser::OptionDeserWrapper(None);
382410
};
411+
($field: ident, (reset_on_reload, $value: expr)) => {
412+
let $field;
413+
};
383414
($field: ident, required) => {
384415
let mut $field = $crate::util::ser::OptionDeserWrapper(None);
385416
};

0 commit comments

Comments
 (0)