From e0fb71889dc19d3cff780d6d8acdf7558cf933e7 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Mon, 31 May 2021 16:48:20 +0000 Subject: [PATCH] Clean up ChannelMonitor object serialization to use TLVs/versions --- lightning/src/chain/channelmonitor.rs | 30 +++++++++++++++++++-------- lightning/src/chain/transaction.rs | 8 ------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/lightning/src/chain/channelmonitor.rs b/lightning/src/chain/channelmonitor.rs index d69f2baf5..fe581ac65 100644 --- a/lightning/src/chain/channelmonitor.rs +++ b/lightning/src/chain/channelmonitor.rs @@ -47,7 +47,7 @@ use chain::onchaintx::OnchainTxHandler; use chain::package::{CounterpartyOfferedHTLCOutput, CounterpartyReceivedHTLCOutput, HolderFundingOutput, HolderHTLCOutput, PackageSolvingData, PackageTemplate, RevokedOutput, RevokedHTLCOutput}; use chain::Filter; use util::logger::Logger; -use util::ser::{Readable, ReadableArgs, MaybeReadable, Writer, Writeable, U48}; +use util::ser::{Readable, ReadableArgs, MaybeReadable, Writer, Writeable, U48, OptionDeserWrapper}; use util::byte_utils; use util::events::Event; @@ -90,22 +90,26 @@ pub const CLOSED_CHANNEL_UPDATE_ID: u64 = core::u64::MAX; impl Writeable for ChannelMonitorUpdate { fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { + write_ver_prefix!(w, SERIALIZATION_VERSION, MIN_SERIALIZATION_VERSION); self.update_id.write(w)?; (self.updates.len() as u64).write(w)?; for update_step in self.updates.iter() { update_step.write(w)?; } + write_tlv_fields!(w, {}, {}); Ok(()) } } impl Readable for ChannelMonitorUpdate { fn read(r: &mut R) -> Result { + let _ver = read_ver_prefix!(r, SERIALIZATION_VERSION); let update_id: u64 = Readable::read(r)?; let len: u64 = Readable::read(r)?; let mut updates = Vec::with_capacity(cmp::min(len as usize, MAX_ALLOC_SIZE / ::core::mem::size_of::())); for _ in 0..len { updates.push(Readable::read(r)?); } + read_tlv_fields!(r, {}, {}); Ok(Self { update_id, updates }) } } @@ -293,9 +297,6 @@ struct CounterpartyCommitmentTransaction { impl Writeable for CounterpartyCommitmentTransaction { fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { - self.counterparty_delayed_payment_base_key.write(w)?; - self.counterparty_htlc_base_key.write(w)?; - w.write_all(&byte_utils::be16_to_array(self.on_counterparty_tx_csv))?; w.write_all(&byte_utils::be64_to_array(self.per_htlc.len() as u64))?; for (ref txid, ref htlcs) in self.per_htlc.iter() { w.write_all(&txid[..])?; @@ -304,15 +305,17 @@ impl Writeable for CounterpartyCommitmentTransaction { htlc.write(w)?; } } + write_tlv_fields!(w, { + (0, self.counterparty_delayed_payment_base_key), + (2, self.counterparty_htlc_base_key), + (4, self.on_counterparty_tx_csv), + }, {}); Ok(()) } } impl Readable for CounterpartyCommitmentTransaction { fn read(r: &mut R) -> Result { let counterparty_commitment_transaction = { - let counterparty_delayed_payment_base_key = Readable::read(r)?; - let counterparty_htlc_base_key = Readable::read(r)?; - let on_counterparty_tx_csv: u16 = Readable::read(r)?; let per_htlc_len: u64 = Readable::read(r)?; let mut per_htlc = HashMap::with_capacity(cmp::min(per_htlc_len as usize, MAX_ALLOC_SIZE / 64)); for _ in 0..per_htlc_len { @@ -327,9 +330,17 @@ impl Readable for CounterpartyCommitmentTransaction { return Err(DecodeError::InvalidValue); } } + let mut counterparty_delayed_payment_base_key = OptionDeserWrapper(None); + let mut counterparty_htlc_base_key = OptionDeserWrapper(None); + let mut on_counterparty_tx_csv: u16 = 0; + read_tlv_fields!(r, { + (0, counterparty_delayed_payment_base_key), + (2, counterparty_htlc_base_key), + (4, on_counterparty_tx_csv), + }, {}); CounterpartyCommitmentTransaction { - counterparty_delayed_payment_base_key, - counterparty_htlc_base_key, + counterparty_delayed_payment_base_key: counterparty_delayed_payment_base_key.0.unwrap(), + counterparty_htlc_base_key: counterparty_htlc_base_key.0.unwrap(), on_counterparty_tx_csv, per_htlc, } @@ -627,6 +638,7 @@ impl Writeable for ChannelMonitor { } } +// These are also used for ChannelMonitorUpdate, above. const SERIALIZATION_VERSION: u8 = 1; const MIN_SERIALIZATION_VERSION: u8 = 1; diff --git a/lightning/src/chain/transaction.rs b/lightning/src/chain/transaction.rs index 4db5fbed6..502eb895b 100644 --- a/lightning/src/chain/transaction.rs +++ b/lightning/src/chain/transaction.rs @@ -73,14 +73,6 @@ impl OutPoint { vout: self.index as u32, } } - - /// Creates a dummy BitcoinOutPoint, useful for deserializing into. - pub(crate) fn null() -> Self { - Self { - txid: Default::default(), - index: 0 - } - } } impl_writeable!(OutPoint, 0, { txid, index }); -- 2.39.5