Skip to content

Commit 6f29478

Browse files
committed
Add a test for the legacy TLV field logic
1 parent 6ef9bae commit 6f29478

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

lightning/src/util/ser_macros.rs

+29
Original file line numberDiff line numberDiff line change
@@ -1872,4 +1872,33 @@ mod tests {
18721872
assert_eq!(TuplesOnly::read(&mut none_data_read).unwrap(), None);
18731873
assert_eq!(none_data_read.position(), unknown_data_variant.len() as u64);
18741874
}
1875+
1876+
#[derive(Debug, PartialEq, Eq)]
1877+
struct ExpandedField {
1878+
// Old versions of LDK are presumed to have had something like:
1879+
// old_field: u8,
1880+
new_field: (u8, u8),
1881+
}
1882+
impl_writeable_tlv_based!(ExpandedField, {
1883+
(0, old_field, (legacy, u8, {
1884+
// Sadly the type-checker needs some help
1885+
let _: &(u8, u8) = &new_field;
1886+
if let Some(old_field) = old_field {
1887+
new_field.0 = old_field;
1888+
}
1889+
}, |us: &ExpandedField| Some(us.new_field.0))),
1890+
(1, new_field, required),
1891+
});
1892+
1893+
#[test]
1894+
fn test_legacy_conversion() {
1895+
let mut encoded = ExpandedField { new_field: (42, 43) }.encode();
1896+
assert_eq!(encoded, <Vec<u8>>::from_hex("0700012a01022a2b").unwrap());
1897+
1898+
// On read, the post-read action will run, using the old_field value to overwrite the
1899+
// new_field.
1900+
encoded[3] = 10;
1901+
let read = <ExpandedField as Readable>::read(&mut &encoded[..]).unwrap();
1902+
assert_eq!(read, ExpandedField { new_field: (10, 43) });
1903+
}
18751904
}

0 commit comments

Comments
 (0)