Skip to content

Commit a5df41f

Browse files
committed
Add a test for the legacy TLV field logic
1 parent 1a09194 commit a5df41f

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

lightning/src/util/ser_macros.rs

+28
Original file line numberDiff line numberDiff line change
@@ -1850,4 +1850,32 @@ mod tests {
18501850
assert_eq!(TuplesOnly::read(&mut none_data_read).unwrap(), None);
18511851
assert_eq!(none_data_read.position(), unknown_data_variant.len() as u64);
18521852
}
1853+
1854+
#[derive(Debug, PartialEq, Eq)]
1855+
struct ExpandedField {
1856+
// Old versions of LDK are presumed to have had something like:
1857+
// old_field: u8,
1858+
new_field: (u8, u8),
1859+
}
1860+
impl_writeable_tlv_based!(ExpandedField, {
1861+
(0, old_field, (legacy, u8, |us: &ExpandedField| Some(us.new_field.0))),
1862+
(1, new_field, (default_value, (old_field.ok_or(DecodeError::InvalidValue)?, 0))),
1863+
});
1864+
1865+
#[test]
1866+
fn test_legacy_conversion() {
1867+
let mut encoded = ExpandedField { new_field: (43, 42) }.encode();
1868+
assert_eq!(encoded, <Vec<u8>>::from_hex("0700012b01022b2a").unwrap());
1869+
1870+
// On read, we'll read a `new_field` which means we won't bother looking at `old_field`.
1871+
encoded[3] = 10;
1872+
let read = <ExpandedField as Readable>::read(&mut &encoded[..]).unwrap();
1873+
assert_eq!(read, ExpandedField { new_field: (43, 42) });
1874+
1875+
// On read, if we read an old `ExpandedField` that just has a type-0 `old_field` entry,
1876+
// we'll copy that into the first position of `new_field`.
1877+
let encoded = <Vec<u8>>::from_hex("0300012a").unwrap();
1878+
let read = <ExpandedField as Readable>::read(&mut &encoded[..]).unwrap();
1879+
assert_eq!(read, ExpandedField { new_field: (42, 0) });
1880+
}
18531881
}

0 commit comments

Comments
 (0)