Skip to content

Commit 3a509b0

Browse files
committed
Update ChannelConfig serialization to be TLV-based
This was missed prior to 0.0.98, so requires a backwards-compatibility wrapper inside the `Channel` serialization logic, but it's not very complicated to do so.
1 parent 0c57018 commit 3a509b0

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

lightning/src/ln/channel.rs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4439,7 +4439,7 @@ fn is_unsupported_shutdown_script(their_features: &InitFeatures, script: &Script
44394439
return !script.is_p2pkh() && !script.is_p2sh() && !script.is_v0_p2wpkh() && !script.is_v0_p2wsh()
44404440
}
44414441

4442-
const SERIALIZATION_VERSION: u8 = 1;
4442+
const SERIALIZATION_VERSION: u8 = 2;
44434443
const MIN_SERIALIZATION_VERSION: u8 = 1;
44444444

44454445
impl_writeable_tlv_based_enum!(InboundHTLCRemovalReason,;
@@ -4481,7 +4481,13 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
44814481
write_ver_prefix!(writer, SERIALIZATION_VERSION, MIN_SERIALIZATION_VERSION);
44824482

44834483
self.user_id.write(writer)?;
4484-
self.config.write(writer)?;
4484+
4485+
// Write out the old serialization for the config object. This is read by version-1
4486+
// deserializers, but we will read the version in the TLV at the end instead.
4487+
self.config.fee_proportional_millionths.write(writer)?;
4488+
self.config.cltv_expiry_delta.write(writer)?;
4489+
self.config.announced_channel.write(writer)?;
4490+
self.config.commit_upfront_shutdown_pubkey.write(writer)?;
44854491

44864492
self.channel_id.write(writer)?;
44874493
(self.channel_state | ChannelState::PeerDisconnected as u32).write(writer)?;
@@ -4669,7 +4675,10 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
46694675

46704676
self.channel_update_status.write(writer)?;
46714677

4672-
write_tlv_fields!(writer, {(0, self.announcement_sigs, option)});
4678+
write_tlv_fields!(writer, {
4679+
(0, self.announcement_sigs, option),
4680+
(1, self.config, required),
4681+
});
46734682

46744683
Ok(())
46754684
}
@@ -4679,10 +4688,21 @@ const MAX_ALLOC_SIZE: usize = 64*1024;
46794688
impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
46804689
where K::Target: KeysInterface<Signer = Signer> {
46814690
fn read<R : ::std::io::Read>(reader: &mut R, keys_source: &'a K) -> Result<Self, DecodeError> {
4682-
let _ver = read_ver_prefix!(reader, SERIALIZATION_VERSION);
4691+
let ver = read_ver_prefix!(reader, SERIALIZATION_VERSION);
46834692

46844693
let user_id = Readable::read(reader)?;
4685-
let config: ChannelConfig = Readable::read(reader)?;
4694+
4695+
let mut config = Some(ChannelConfig::default());
4696+
if ver == 1 {
4697+
// Read the old serialization of the ChannelConfig from version 0.0.98.
4698+
config.as_mut().unwrap().fee_proportional_millionths = Readable::read(reader)?;
4699+
config.as_mut().unwrap().cltv_expiry_delta = Readable::read(reader)?;
4700+
config.as_mut().unwrap().announced_channel = Readable::read(reader)?;
4701+
config.as_mut().unwrap().commit_upfront_shutdown_pubkey = Readable::read(reader)?;
4702+
} else {
4703+
// Read the 8 bytes of backwards-compatibility ChannelConfig data.
4704+
let mut _val: u64 = Readable::read(reader)?;
4705+
}
46864706

46874707
let channel_id = Readable::read(reader)?;
46884708
let channel_state = Readable::read(reader)?;
@@ -4842,15 +4862,18 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
48424862
let channel_update_status = Readable::read(reader)?;
48434863

48444864
let mut announcement_sigs = None;
4845-
read_tlv_fields!(reader, {(0, announcement_sigs, option)});
4865+
read_tlv_fields!(reader, {
4866+
(0, announcement_sigs, option),
4867+
(1, config, option), // Note that if none is provided we will *not* overwrite the existing one.
4868+
});
48464869

48474870
let mut secp_ctx = Secp256k1::new();
48484871
secp_ctx.seeded_randomize(&keys_source.get_secure_random_bytes());
48494872

48504873
Ok(Channel {
48514874
user_id,
48524875

4853-
config,
4876+
config: config.unwrap(),
48544877
channel_id,
48554878
channel_state,
48564879
secp_ctx,

lightning/src/util/config.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,11 @@ impl Default for ChannelConfig {
205205
}
206206

207207
//Add write and readable traits to channelconfig
208-
impl_writeable!(ChannelConfig, 4+2+1+1, {
209-
fee_proportional_millionths,
210-
cltv_expiry_delta,
211-
announced_channel,
212-
commit_upfront_shutdown_pubkey
208+
impl_writeable_tlv_based!(ChannelConfig, {
209+
(0, fee_proportional_millionths, required),
210+
(2, cltv_expiry_delta, required),
211+
(4, announced_channel, required),
212+
(6, commit_upfront_shutdown_pubkey, required),
213213
});
214214

215215
/// Top-level config which holds ChannelHandshakeLimits and ChannelConfig.

lightning/src/util/ser_macros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ macro_rules! decode_tlv_stream {
173173
last_seen_type = Some(typ.0);
174174

175175
// Finally, read the length and value itself:
176-
let length: ser::BigSize = Readable::read($stream)?;
176+
let length: ser::BigSize = ser::Readable::read($stream)?;
177177
let mut s = ser::FixedLengthReader::new($stream, length.0);
178178
match typ.0 {
179179
$($type => {
@@ -503,7 +503,7 @@ mod tests {
503503
use prelude::*;
504504
use std::io::Cursor;
505505
use ln::msgs::DecodeError;
506-
use util::ser::{Readable, Writeable, HighZeroBytesDroppedVarInt, VecWriter};
506+
use util::ser::{Writeable, HighZeroBytesDroppedVarInt, VecWriter};
507507
use bitcoin::secp256k1::PublicKey;
508508

509509
// The BOLT TLV test cases don't include any tests which use our "required-value" logic since

0 commit comments

Comments
 (0)