@@ -36,7 +36,7 @@ use crate::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, Channel
36
36
use crate :: chain:: transaction:: { OutPoint , TransactionData } ;
37
37
use crate :: chain:: keysinterface:: { Sign , KeysInterface , BaseSign } ;
38
38
use crate :: util:: events:: ClosureReason ;
39
- use crate :: util:: ser:: { Readable , ReadableArgs , Writeable , Writer } ;
39
+ use crate :: util:: ser:: { Readable , ReadableArgs , Writeable , Writer , VecWriter } ;
40
40
use crate :: util:: logger:: Logger ;
41
41
use crate :: util:: errors:: APIError ;
42
42
use crate :: util:: config:: { UserConfig , ChannelConfig , LegacyChannelConfig , ChannelHandshakeConfig , ChannelHandshakeLimits } ;
@@ -5971,7 +5971,7 @@ impl<Signer: Sign> Channel<Signer> {
5971
5971
}
5972
5972
}
5973
5973
5974
- const SERIALIZATION_VERSION : u8 = 3 ;
5974
+ const SERIALIZATION_VERSION : u8 = 2 ;
5975
5975
const MIN_SERIALIZATION_VERSION : u8 = 2 ;
5976
5976
5977
5977
impl_writeable_tlv_based_enum ! ( InboundHTLCRemovalReason , ;
@@ -6052,6 +6052,13 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
6052
6052
6053
6053
self . latest_monitor_update_id . write ( writer) ?;
6054
6054
6055
+ let mut key_data = VecWriter ( Vec :: new ( ) ) ;
6056
+ self . holder_signer . write ( & mut key_data) ?;
6057
+ assert ! ( key_data. 0 . len( ) < core:: usize :: MAX ) ;
6058
+ assert ! ( key_data. 0 . len( ) < core:: u32 :: MAX as usize ) ;
6059
+ ( key_data. 0 . len ( ) as u32 ) . write ( writer) ?;
6060
+ writer. write_all ( & key_data. 0 [ ..] ) ?;
6061
+
6055
6062
// Write out the old serialization for shutdown_pubkey for backwards compatibility, if
6056
6063
// deserialized from that format.
6057
6064
match self . shutdown_scriptpubkey . as_ref ( ) . and_then ( |script| script. as_legacy_pubkey ( ) ) {
@@ -6285,7 +6292,7 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
6285
6292
let user_id_high_opt = Some ( ( self . user_id >> 64 ) as u64 ) ;
6286
6293
6287
6294
// `channel_keys_id` is serialized as an option to remain backwards compatible until we bump
6288
- // `MIN_SERIALIZATION_VERSION ` to 3.
6295
+ // `SERIALIZATION_VERSION ` to 3.
6289
6296
let channel_keys_id = Some ( self . _channel_keys_id ) ;
6290
6297
6291
6298
write_tlv_fields ! ( writer, {
@@ -6303,7 +6310,6 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
6303
6310
( 5 , self . config, required) ,
6304
6311
( 6 , serialized_holder_htlc_max_in_flight, option) ,
6305
6312
( 7 , self . shutdown_scriptpubkey, option) ,
6306
- ( 8 , channel_keys_id, option) ,
6307
6313
( 9 , self . target_closing_feerate_sats_per_kw, option) ,
6308
6314
( 11 , self . monitor_pending_finalized_fulfills, vec_type) ,
6309
6315
( 13 , self . channel_creation_height, required) ,
@@ -6313,6 +6319,7 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
6313
6319
( 21 , self . outbound_scid_alias, required) ,
6314
6320
( 23 , channel_ready_event_emitted, option) ,
6315
6321
( 25 , user_id_high_opt, option) ,
6322
+ ( 27 , channel_keys_id, option) ,
6316
6323
} ) ;
6317
6324
6318
6325
Ok ( ( ) )
@@ -6349,18 +6356,16 @@ impl<'a, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<<K::Target as KeysInte
6349
6356
6350
6357
let latest_monitor_update_id = Readable :: read ( reader) ?;
6351
6358
6352
- let mut holder_signer = None ;
6353
- if ver <= 2 {
6354
- let keys_len: u32 = Readable :: read ( reader) ?;
6355
- let mut keys_data = Vec :: with_capacity ( cmp:: min ( keys_len as usize , MAX_ALLOC_SIZE ) ) ;
6356
- while keys_data. len ( ) != keys_len as usize {
6357
- // Read 1KB at a time to avoid accidentally allocating 4GB on corrupted channel keys
6358
- let mut data = [ 0 ; 1024 ] ;
6359
- let read_slice = & mut data[ 0 ..cmp:: min ( 1024 , keys_len as usize - keys_data. len ( ) ) ] ;
6360
- reader. read_exact ( read_slice) ?;
6361
- keys_data. extend_from_slice ( read_slice) ;
6362
- }
6363
- holder_signer = Some ( keys_source. read_chan_signer ( & keys_data) ?) ;
6359
+ // Read the serialize signer bytes. We'll choose to deserialize them or not based on whether
6360
+ // the `channel_keys_id` TLV is present below.
6361
+ let keys_len: u32 = Readable :: read ( reader) ?;
6362
+ let mut keys_data = Vec :: with_capacity ( cmp:: min ( keys_len as usize , MAX_ALLOC_SIZE ) ) ;
6363
+ while keys_data. len ( ) != keys_len as usize {
6364
+ // Read 1KB at a time to avoid accidentally allocating 4GB on corrupted channel keys
6365
+ let mut data = [ 0 ; 1024 ] ;
6366
+ let read_slice = & mut data[ 0 ..cmp:: min ( 1024 , keys_len as usize - keys_data. len ( ) ) ] ;
6367
+ reader. read_exact ( read_slice) ?;
6368
+ keys_data. extend_from_slice ( read_slice) ;
6364
6369
}
6365
6370
6366
6371
// Read the old serialization for shutdown_pubkey, preferring the TLV field later if set.
@@ -6590,7 +6595,6 @@ impl<'a, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<<K::Target as KeysInte
6590
6595
( 5 , config, option) , // Note that if none is provided we will *not* overwrite the existing one.
6591
6596
( 6 , holder_max_htlc_value_in_flight_msat, option) ,
6592
6597
( 7 , shutdown_scriptpubkey, option) ,
6593
- ( 8 , channel_keys_id, option) ,
6594
6598
( 9 , target_closing_feerate_sats_per_kw, option) ,
6595
6599
( 11 , monitor_pending_finalized_fulfills, vec_type) ,
6596
6600
( 13 , channel_creation_height, option) ,
@@ -6600,23 +6604,22 @@ impl<'a, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<<K::Target as KeysInte
6600
6604
( 21 , outbound_scid_alias, option) ,
6601
6605
( 23 , channel_ready_event_emitted, option) ,
6602
6606
( 25 , user_id_high_opt, option) ,
6607
+ ( 27 , channel_keys_id, option) ,
6603
6608
} ) ;
6604
6609
6605
- let ( channel_keys_id, mut holder_signer) = if ver <= 2 {
6606
- assert ! ( holder_signer. is_some( ) && channel_keys_id. is_none( ) ) ;
6607
- let holder_signer = holder_signer. unwrap ( ) ;
6608
- ( holder_signer. channel_keys_id ( ) , holder_signer)
6610
+ let ( channel_keys_id, holder_signer) = if channel_keys_id. is_some ( ) {
6611
+ let mut holder_signer = keys_source. derive_channel_signer ( channel_value_satoshis, channel_keys_id) ;
6612
+ // If we've gotten to the funding stage of the channel, populate the signer with its
6613
+ // required channel parameters.
6614
+ let non_shutdown_state = channel_state & ( !MULTI_STATE_FLAGS ) ;
6615
+ if non_shutdown_state >= ( ChannelState :: FundingCreated as u32 ) {
6616
+ holder_signer. ready_channel ( & channel_parameters) ;
6617
+ }
6618
+ ( channel_keys_id. unwrap ( ) , holder_signer)
6609
6619
} else {
6610
- assert ! ( holder_signer. is_none( ) && channel_keys_id. is_some( ) ) ;
6611
- let channel_keys_id = channel_keys_id. unwrap ( ) ;
6612
- ( channel_keys_id, keys_source. derive_channel_signer ( channel_value_satoshis, channel_keys_id) )
6620
+ let holder_signer = keys_source. read_chan_signer ( & keys_data) ?;
6621
+ ( holder_signer. channel_keys_id ( ) , holder_signer)
6613
6622
} ;
6614
- // If we've gotten to the funding stage of the channel, populate the signer with its
6615
- // required channel parameters.
6616
- let non_shutdown_state = channel_state & ( !MULTI_STATE_FLAGS ) ;
6617
- if non_shutdown_state >= ( ChannelState :: FundingCreated as u32 ) {
6618
- holder_signer. ready_channel ( & channel_parameters) ;
6619
- }
6620
6623
6621
6624
if let Some ( preimages) = preimages_opt {
6622
6625
let mut iter = preimages. into_iter ( ) ;
0 commit comments