Skip to content

Commit 7781086

Browse files
committed
Clean up ChannelMonitor object serialization to use TLVs/versions
1 parent 409c7f3 commit 7781086

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use chain::onchaintx::OnchainTxHandler;
4747
use chain::package::{CounterpartyOfferedHTLCOutput, CounterpartyReceivedHTLCOutput, HolderFundingOutput, HolderHTLCOutput, PackageSolvingData, PackageTemplate, RevokedOutput, RevokedHTLCOutput};
4848
use chain::Filter;
4949
use util::logger::Logger;
50-
use util::ser::{Readable, ReadableArgs, MaybeReadable, Writer, Writeable, U48};
50+
use util::ser::{Readable, ReadableArgs, MaybeReadable, Writer, Writeable, U48, OptionDeserWrapper};
5151
use util::byte_utils;
5252
use util::events::Event;
5353

@@ -90,22 +90,26 @@ pub const CLOSED_CHANNEL_UPDATE_ID: u64 = core::u64::MAX;
9090

9191
impl Writeable for ChannelMonitorUpdate {
9292
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
93+
write_ver_prefix!(w, SERIALIZATION_VERSION, MIN_SERIALIZATION_VERSION);
9394
self.update_id.write(w)?;
9495
(self.updates.len() as u64).write(w)?;
9596
for update_step in self.updates.iter() {
9697
update_step.write(w)?;
9798
}
99+
write_tlv_fields!(w, {}, {});
98100
Ok(())
99101
}
100102
}
101103
impl Readable for ChannelMonitorUpdate {
102104
fn read<R: ::std::io::Read>(r: &mut R) -> Result<Self, DecodeError> {
105+
let _ver = read_ver_prefix!(r, SERIALIZATION_VERSION);
103106
let update_id: u64 = Readable::read(r)?;
104107
let len: u64 = Readable::read(r)?;
105108
let mut updates = Vec::with_capacity(cmp::min(len as usize, MAX_ALLOC_SIZE / ::core::mem::size_of::<ChannelMonitorUpdateStep>()));
106109
for _ in 0..len {
107110
updates.push(Readable::read(r)?);
108111
}
112+
read_tlv_fields!(r, {}, {});
109113
Ok(Self { update_id, updates })
110114
}
111115
}
@@ -293,9 +297,6 @@ struct CounterpartyCommitmentTransaction {
293297

294298
impl Writeable for CounterpartyCommitmentTransaction {
295299
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
296-
self.counterparty_delayed_payment_base_key.write(w)?;
297-
self.counterparty_htlc_base_key.write(w)?;
298-
w.write_all(&byte_utils::be16_to_array(self.on_counterparty_tx_csv))?;
299300
w.write_all(&byte_utils::be64_to_array(self.per_htlc.len() as u64))?;
300301
for (ref txid, ref htlcs) in self.per_htlc.iter() {
301302
w.write_all(&txid[..])?;
@@ -304,15 +305,17 @@ impl Writeable for CounterpartyCommitmentTransaction {
304305
htlc.write(w)?;
305306
}
306307
}
308+
write_tlv_fields!(w, {
309+
(0, self.counterparty_delayed_payment_base_key),
310+
(2, self.counterparty_htlc_base_key),
311+
(4, self.on_counterparty_tx_csv),
312+
}, {});
307313
Ok(())
308314
}
309315
}
310316
impl Readable for CounterpartyCommitmentTransaction {
311317
fn read<R: ::std::io::Read>(r: &mut R) -> Result<Self, DecodeError> {
312318
let counterparty_commitment_transaction = {
313-
let counterparty_delayed_payment_base_key = Readable::read(r)?;
314-
let counterparty_htlc_base_key = Readable::read(r)?;
315-
let on_counterparty_tx_csv: u16 = Readable::read(r)?;
316319
let per_htlc_len: u64 = Readable::read(r)?;
317320
let mut per_htlc = HashMap::with_capacity(cmp::min(per_htlc_len as usize, MAX_ALLOC_SIZE / 64));
318321
for _ in 0..per_htlc_len {
@@ -327,9 +330,17 @@ impl Readable for CounterpartyCommitmentTransaction {
327330
return Err(DecodeError::InvalidValue);
328331
}
329332
}
333+
let mut counterparty_delayed_payment_base_key = OptionDeserWrapper(None);
334+
let mut counterparty_htlc_base_key = OptionDeserWrapper(None);
335+
let mut on_counterparty_tx_csv: u16 = 0;
336+
read_tlv_fields!(r, {
337+
(0, counterparty_delayed_payment_base_key),
338+
(2, counterparty_htlc_base_key),
339+
(4, on_counterparty_tx_csv),
340+
}, {});
330341
CounterpartyCommitmentTransaction {
331-
counterparty_delayed_payment_base_key,
332-
counterparty_htlc_base_key,
342+
counterparty_delayed_payment_base_key: counterparty_delayed_payment_base_key.0.unwrap(),
343+
counterparty_htlc_base_key: counterparty_htlc_base_key.0.unwrap(),
333344
on_counterparty_tx_csv,
334345
per_htlc,
335346
}
@@ -627,6 +638,7 @@ impl<Signer: Sign> Writeable for ChannelMonitor<Signer> {
627638
}
628639
}
629640

641+
// These are also used for ChannelMonitorUpdate, above.
630642
const SERIALIZATION_VERSION: u8 = 1;
631643
const MIN_SERIALIZATION_VERSION: u8 = 1;
632644

lightning/src/chain/transaction.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,6 @@ impl OutPoint {
7373
vout: self.index as u32,
7474
}
7575
}
76-
77-
/// Creates a dummy BitcoinOutPoint, useful for deserializing into.
78-
pub(crate) fn null() -> Self {
79-
Self {
80-
txid: Default::default(),
81-
index: 0
82-
}
83-
}
8476
}
8577

8678
impl_writeable!(OutPoint, 0, { txid, index });

0 commit comments

Comments
 (0)