From: Matt Corallo Date: Fri, 7 May 2021 22:36:45 +0000 (+0000) Subject: Add version and TLV suffix for more user-facing "major" structs X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=ea1924df3e18840492291aac9698edde89f056e7;p=rust-lightning Add version and TLV suffix for more user-facing "major" structs --- diff --git a/lightning/src/chain/keysinterface.rs b/lightning/src/chain/keysinterface.rs index 114ceec21..d6fea17d4 100644 --- a/lightning/src/chain/keysinterface.rs +++ b/lightning/src/chain/keysinterface.rs @@ -704,10 +704,15 @@ impl BaseSign for InMemorySigner { } } +const SERIALIZATION_VERSION: u8 = 1; +const MIN_SERIALIZATION_VERSION: u8 = 1; + impl Sign for InMemorySigner {} impl Writeable for InMemorySigner { fn write(&self, writer: &mut W) -> Result<(), Error> { + write_ver_prefix!(writer, SERIALIZATION_VERSION, MIN_SERIALIZATION_VERSION); + self.funding_key.write(writer)?; self.revocation_base_key.write(writer)?; self.payment_key.write(writer)?; @@ -718,12 +723,16 @@ impl Writeable for InMemorySigner { self.channel_value_satoshis.write(writer)?; self.channel_keys_id.write(writer)?; + write_tlv_fields!(writer, {}, {}); + Ok(()) } } impl Readable for InMemorySigner { fn read(reader: &mut R) -> Result { + let _ver = read_ver_prefix!(reader, SERIALIZATION_VERSION); + let funding_key = Readable::read(reader)?; let revocation_base_key = Readable::read(reader)?; let payment_key = Readable::read(reader)?; @@ -739,6 +748,8 @@ impl Readable for InMemorySigner { &htlc_base_key); let keys_id = Readable::read(reader)?; + read_tlv_fields!(reader, {}, {}); + Ok(InMemorySigner { funding_key, revocation_base_key, diff --git a/lightning/src/routing/network_graph.rs b/lightning/src/routing/network_graph.rs index ee73dc0e8..16e0a978a 100644 --- a/lightning/src/routing/network_graph.rs +++ b/lightning/src/routing/network_graph.rs @@ -660,8 +660,13 @@ impl Readable for NodeInfo { } } +const SERIALIZATION_VERSION: u8 = 1; +const MIN_SERIALIZATION_VERSION: u8 = 1; + impl Writeable for NetworkGraph { fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { + write_ver_prefix!(writer, SERIALIZATION_VERSION, MIN_SERIALIZATION_VERSION); + self.genesis_hash.write(writer)?; (self.channels.len() as u64).write(writer)?; for (ref chan_id, ref chan_info) in self.channels.iter() { @@ -673,12 +678,16 @@ impl Writeable for NetworkGraph { node_id.write(writer)?; node_info.write(writer)?; } + + write_tlv_fields!(writer, {}, {}); Ok(()) } } impl Readable for NetworkGraph { fn read(reader: &mut R) -> Result { + let _ver = read_ver_prefix!(reader, SERIALIZATION_VERSION); + let genesis_hash: BlockHash = Readable::read(reader)?; let channels_count: u64 = Readable::read(reader)?; let mut channels = BTreeMap::new(); @@ -694,6 +703,8 @@ impl Readable for NetworkGraph { let node_info = Readable::read(reader)?; nodes.insert(node_id, node_info); } + read_tlv_fields!(reader, {}, {}); + Ok(NetworkGraph { genesis_hash, channels, diff --git a/lightning/src/routing/router.rs b/lightning/src/routing/router.rs index ba964d734..489b90060 100644 --- a/lightning/src/routing/router.rs +++ b/lightning/src/routing/router.rs @@ -95,23 +95,30 @@ pub struct Route { pub paths: Vec>, } +const SERIALIZATION_VERSION: u8 = 1; +const MIN_SERIALIZATION_VERSION: u8 = 1; + impl Writeable for Route { fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { + write_ver_prefix!(writer, SERIALIZATION_VERSION, MIN_SERIALIZATION_VERSION); (self.paths.len() as u64).write(writer)?; for hops in self.paths.iter() { hops.write(writer)?; } + write_tlv_fields!(writer, {}, {}); Ok(()) } } impl Readable for Route { fn read(reader: &mut R) -> Result { + let _ver = read_ver_prefix!(reader, SERIALIZATION_VERSION); let path_count: u64 = Readable::read(reader)?; let mut paths = Vec::with_capacity(cmp::min(path_count, 128) as usize); for _ in 0..path_count { paths.push(Readable::read(reader)?); } + read_tlv_fields!(reader, {}, {}); Ok(Route { paths }) } } diff --git a/lightning/src/util/enforcing_trait_impls.rs b/lightning/src/util/enforcing_trait_impls.rs index 32c17af64..55e45d309 100644 --- a/lightning/src/util/enforcing_trait_impls.rs +++ b/lightning/src/util/enforcing_trait_impls.rs @@ -38,6 +38,9 @@ pub const INITIAL_REVOKED_COMMITMENT_NUMBER: u64 = 1 << 48; /// /// Eventually we will probably want to expose a variant of this which would essentially /// be what you'd want to run on a hardware wallet. +/// +/// Note that before we do so we should ensure its serialization format has backwards- and +/// forwards-compatibility prefix/suffixes! #[derive(Clone)] pub struct EnforcingSigner { pub inner: InMemorySigner,