Skip to content

Commit 867f168

Browse files
committed
f! remove new ser version + allow downgrade in channel
1 parent 517d4f2 commit 867f168

File tree

1 file changed

+33
-30
lines changed

1 file changed

+33
-30
lines changed

lightning/src/ln/channel.rs

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use crate::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, Channel
3636
use crate::chain::transaction::{OutPoint, TransactionData};
3737
use crate::chain::keysinterface::{Sign, KeysInterface, BaseSign};
3838
use crate::util::events::ClosureReason;
39-
use crate::util::ser::{Readable, ReadableArgs, Writeable, Writer};
39+
use crate::util::ser::{Readable, ReadableArgs, Writeable, Writer, VecWriter};
4040
use crate::util::logger::Logger;
4141
use crate::util::errors::APIError;
4242
use crate::util::config::{UserConfig, ChannelConfig, LegacyChannelConfig, ChannelHandshakeConfig, ChannelHandshakeLimits};
@@ -5971,7 +5971,7 @@ impl<Signer: Sign> Channel<Signer> {
59715971
}
59725972
}
59735973

5974-
const SERIALIZATION_VERSION: u8 = 3;
5974+
const SERIALIZATION_VERSION: u8 = 2;
59755975
const MIN_SERIALIZATION_VERSION: u8 = 2;
59765976

59775977
impl_writeable_tlv_based_enum!(InboundHTLCRemovalReason,;
@@ -6052,6 +6052,13 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
60526052

60536053
self.latest_monitor_update_id.write(writer)?;
60546054

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+
60556062
// Write out the old serialization for shutdown_pubkey for backwards compatibility, if
60566063
// deserialized from that format.
60576064
match self.shutdown_scriptpubkey.as_ref().and_then(|script| script.as_legacy_pubkey()) {
@@ -6285,7 +6292,7 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
62856292
let user_id_high_opt = Some((self.user_id >> 64) as u64);
62866293

62876294
// `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.
62896296
let channel_keys_id = Some(self._channel_keys_id);
62906297

62916298
write_tlv_fields!(writer, {
@@ -6303,7 +6310,6 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
63036310
(5, self.config, required),
63046311
(6, serialized_holder_htlc_max_in_flight, option),
63056312
(7, self.shutdown_scriptpubkey, option),
6306-
(8, channel_keys_id, option),
63076313
(9, self.target_closing_feerate_sats_per_kw, option),
63086314
(11, self.monitor_pending_finalized_fulfills, vec_type),
63096315
(13, self.channel_creation_height, required),
@@ -6313,6 +6319,7 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
63136319
(21, self.outbound_scid_alias, required),
63146320
(23, channel_ready_event_emitted, option),
63156321
(25, user_id_high_opt, option),
6322+
(27, channel_keys_id, option),
63166323
});
63176324

63186325
Ok(())
@@ -6349,18 +6356,16 @@ impl<'a, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<<K::Target as KeysInte
63496356

63506357
let latest_monitor_update_id = Readable::read(reader)?;
63516358

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);
63646369
}
63656370

63666371
// 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
65906595
(5, config, option), // Note that if none is provided we will *not* overwrite the existing one.
65916596
(6, holder_max_htlc_value_in_flight_msat, option),
65926597
(7, shutdown_scriptpubkey, option),
6593-
(8, channel_keys_id, option),
65946598
(9, target_closing_feerate_sats_per_kw, option),
65956599
(11, monitor_pending_finalized_fulfills, vec_type),
65966600
(13, channel_creation_height, option),
@@ -6600,23 +6604,22 @@ impl<'a, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<<K::Target as KeysInte
66006604
(21, outbound_scid_alias, option),
66016605
(23, channel_ready_event_emitted, option),
66026606
(25, user_id_high_opt, option),
6607+
(27, channel_keys_id, option),
66036608
});
66046609

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)
66096619
} 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)
66136622
};
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-
}
66206623

66216624
if let Some(preimages) = preimages_opt {
66226625
let mut iter = preimages.into_iter();

0 commit comments

Comments
 (0)