Skip to content

Commit edbc30c

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 431f807 commit edbc30c

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

lightning/src/ln/channel.rs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4460,7 +4460,7 @@ fn is_unsupported_shutdown_script(their_features: &InitFeatures, script: &Script
44604460
return !script.is_p2pkh() && !script.is_p2sh() && !script.is_v0_p2wpkh() && !script.is_v0_p2wsh()
44614461
}
44624462

4463-
const SERIALIZATION_VERSION: u8 = 1;
4463+
const SERIALIZATION_VERSION: u8 = 2;
44644464
const MIN_SERIALIZATION_VERSION: u8 = 1;
44654465

44664466
impl_writeable_tlv_based_enum!(InboundHTLCRemovalReason,;
@@ -4502,7 +4502,13 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
45024502
write_ver_prefix!(writer, SERIALIZATION_VERSION, MIN_SERIALIZATION_VERSION);
45034503

45044504
self.user_id.write(writer)?;
4505-
self.config.write(writer)?;
4505+
4506+
// Write out the old serialization for the config object. This is read by version-1
4507+
// deserializers, but we will read the version in the TLV at the end instead.
4508+
self.config.fee_proportional_millionths.write(writer)?;
4509+
self.config.cltv_expiry_delta.write(writer)?;
4510+
self.config.announced_channel.write(writer)?;
4511+
self.config.commit_upfront_shutdown_pubkey.write(writer)?;
45064512

45074513
self.channel_id.write(writer)?;
45084514
(self.channel_state | ChannelState::PeerDisconnected as u32).write(writer)?;
@@ -4700,6 +4706,7 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
47004706
// override that.
47014707
(1, self.minimum_depth, option),
47024708
(3, self.counterparty_selected_channel_reserve_satoshis, option),
4709+
(5, self.config, required),
47034710
});
47044711

47054712
Ok(())
@@ -4710,10 +4717,21 @@ const MAX_ALLOC_SIZE: usize = 64*1024;
47104717
impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
47114718
where K::Target: KeysInterface<Signer = Signer> {
47124719
fn read<R : ::std::io::Read>(reader: &mut R, keys_source: &'a K) -> Result<Self, DecodeError> {
4713-
let _ver = read_ver_prefix!(reader, SERIALIZATION_VERSION);
4720+
let ver = read_ver_prefix!(reader, SERIALIZATION_VERSION);
47144721

47154722
let user_id = Readable::read(reader)?;
4716-
let config: ChannelConfig = Readable::read(reader)?;
4723+
4724+
let mut config = Some(ChannelConfig::default());
4725+
if ver == 1 {
4726+
// Read the old serialization of the ChannelConfig from version 0.0.98.
4727+
config.as_mut().unwrap().fee_proportional_millionths = Readable::read(reader)?;
4728+
config.as_mut().unwrap().cltv_expiry_delta = Readable::read(reader)?;
4729+
config.as_mut().unwrap().announced_channel = Readable::read(reader)?;
4730+
config.as_mut().unwrap().commit_upfront_shutdown_pubkey = Readable::read(reader)?;
4731+
} else {
4732+
// Read the 8 bytes of backwards-compatibility ChannelConfig data.
4733+
let mut _val: u64 = Readable::read(reader)?;
4734+
}
47174735

47184736
let channel_id = Readable::read(reader)?;
47194737
let channel_state = Readable::read(reader)?;
@@ -4887,6 +4905,7 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
48874905
(0, announcement_sigs, option),
48884906
(1, minimum_depth, option),
48894907
(3, counterparty_selected_channel_reserve_satoshis, option),
4908+
(5, config, option), // Note that if none is provided we will *not* overwrite the existing one.
48904909
});
48914910

48924911
let mut secp_ctx = Secp256k1::new();
@@ -4895,7 +4914,7 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
48954914
Ok(Channel {
48964915
user_id,
48974916

4898-
config,
4917+
config: config.unwrap(),
48994918
channel_id,
49004919
channel_state,
49014920
secp_ctx,

lightning/src/util/config.rs

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

207-
//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
207+
impl_writeable_tlv_based!(ChannelConfig, {
208+
(0, fee_proportional_millionths, required),
209+
(2, cltv_expiry_delta, required),
210+
(4, announced_channel, required),
211+
(6, commit_upfront_shutdown_pubkey, required),
213212
});
214213

215214
/// 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)