@@ -1851,4 +1851,33 @@ mod tests {
1851
1851
assert_eq ! ( TuplesOnly :: read( & mut none_data_read) . unwrap( ) , None ) ;
1852
1852
assert_eq ! ( none_data_read. position( ) , unknown_data_variant. len( ) as u64 ) ;
1853
1853
}
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
+ }
1854
1883
}
0 commit comments