Skip to content

Commit 7b4c89b

Browse files
committed
Add a test for the legacy TLV field logic
1 parent a11920f commit 7b4c89b

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

lightning/src/util/ser_macros.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,4 +1851,33 @@ mod tests {
18511851
assert_eq!(TuplesOnly::read(&mut none_data_read).unwrap(), None);
18521852
assert_eq!(none_data_read.position(), unknown_data_variant.len() as u64);
18531853
}
1854+
1855+
#[derive(Debug, PartialEq, Eq)]
1856+
struct ExpandedField {
1857+
// Old versions of LDK are presumed to have had something like:
1858+
// old_field: u8,
1859+
new_field: (u8, u8),
1860+
}
1861+
impl_writeable_tlv_based!(ExpandedField, {
1862+
(0, old_field, (legacy, u8, {
1863+
// Sadly the type-checker needs some help
1864+
let _: &(u8, u8) = &new_field;
1865+
if let Some(old_field) = old_field {
1866+
new_field.0 = old_field;
1867+
}
1868+
}, |us: &ExpandedField| Some(us.new_field.0))),
1869+
(1, new_field, required),
1870+
});
1871+
1872+
#[test]
1873+
fn test_legacy_conversion() {
1874+
let mut encoded = ExpandedField { new_field: (42, 43) }.encode();
1875+
assert_eq!(encoded, <Vec<u8>>::from_hex("0700012a01022a2b").unwrap());
1876+
1877+
// On read, the post-read action will run, using the old_field value to overwrite the
1878+
// new_field.
1879+
encoded[3] = 10;
1880+
let read = <ExpandedField as Readable>::read(&mut &encoded[..]).unwrap();
1881+
assert_eq!(read, ExpandedField { new_field: (10, 43) });
1882+
}
18541883
}

0 commit comments

Comments
 (0)