From 493ffb81e197fefe11f821f19c15a0eac8fbd3d6 Mon Sep 17 00:00:00 2001 From: Antoine Riard Date: Tue, 24 Mar 2020 19:26:28 -0400 Subject: [PATCH] Cache current local commitment number in ChannelMonitor. By caching current local commitment number instead of deciphering it from local commitment tx, we may remove local commitment tx from ChannelMonitor in next commit. --- lightning/src/ln/channelmonitor.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/lightning/src/ln/channelmonitor.rs b/lightning/src/ln/channelmonitor.rs index b3cff4c2..ea95f229 100644 --- a/lightning/src/ln/channelmonitor.rs +++ b/lightning/src/ln/channelmonitor.rs @@ -780,6 +780,9 @@ pub struct ChannelMonitor { // Used just for ChannelManager to make sure it has the latest channel data during // deserialization current_remote_commitment_number: u64, + // Used just for ChannelManager to make sure it has the latest channel data during + // deserialization + current_local_commitment_number: u64, payment_preimages: HashMap, @@ -836,6 +839,7 @@ impl PartialEq for ChannelMonitor { self.remote_hash_commitment_number != other.remote_hash_commitment_number || self.prev_local_signed_commitment_tx != other.prev_local_signed_commitment_tx || self.current_remote_commitment_number != other.current_remote_commitment_number || + self.current_local_commitment_number != other.current_local_commitment_number || self.current_local_signed_commitment_tx != other.current_local_signed_commitment_tx || self.payment_preimages != other.payment_preimages || self.pending_htlcs_updated != other.pending_htlcs_updated || @@ -1008,6 +1012,12 @@ impl ChannelMonitor { writer.write_all(&byte_utils::be48_to_array(0))?; } + if for_local_storage { + writer.write_all(&byte_utils::be48_to_array(self.current_local_commitment_number))?; + } else { + writer.write_all(&byte_utils::be48_to_array(0))?; + } + writer.write_all(&byte_utils::be64_to_array(self.payment_preimages.len() as u64))?; for payment_preimage in self.payment_preimages.values() { writer.write_all(&payment_preimage.0[..])?; @@ -1126,6 +1136,7 @@ impl ChannelMonitor { prev_local_signed_commitment_tx: None, current_local_signed_commitment_tx: None, current_remote_commitment_number: 1 << 48, + current_local_commitment_number: 0xffff_ffff_ffff, payment_preimages: HashMap::new(), pending_htlcs_updated: Vec::new(), @@ -1262,6 +1273,7 @@ impl ChannelMonitor { if let Err(_) = self.onchain_tx_handler.provide_latest_local_tx(commitment_tx.clone()) { return Err(MonitorUpdateError("Local commitment signed has already been signed, no further update of LOCAL commitment transaction is allowed")); } + self.current_local_commitment_number = 0xffff_ffff_ffff - ((((commitment_tx.without_valid_witness().input[0].sequence as u64 & 0xffffff) << 3*8) | (commitment_tx.without_valid_witness().lock_time as u64 & 0xffffff)) ^ self.commitment_transaction_number_obscure_factor); self.prev_local_signed_commitment_tx = self.current_local_signed_commitment_tx.take(); self.current_local_signed_commitment_tx = Some(LocalSignedTx { txid: commitment_tx.txid(), @@ -1415,9 +1427,7 @@ impl ChannelMonitor { } pub(super) fn get_cur_local_commitment_number(&self) -> u64 { - if let &Some(ref local_tx) = &self.current_local_signed_commitment_tx { - 0xffff_ffff_ffff - ((((local_tx.tx.without_valid_witness().input[0].sequence as u64 & 0xffffff) << 3*8) | (local_tx.tx.without_valid_witness().lock_time as u64 & 0xffffff)) ^ self.commitment_transaction_number_obscure_factor) - } else { 0xffff_ffff_ffff } + self.current_local_commitment_number } /// Attempts to claim a remote commitment transaction's outputs using the revocation key and @@ -2415,6 +2425,7 @@ impl ReadableArgs> for (Sha256dH }; let current_remote_commitment_number = ::read(reader)?.0; + let current_local_commitment_number = ::read(reader)?.0; let payment_preimages_len: u64 = Readable::read(reader)?; let mut payment_preimages = HashMap::with_capacity(cmp::min(payment_preimages_len as usize, MAX_ALLOC_SIZE / 32)); @@ -2512,6 +2523,7 @@ impl ReadableArgs> for (Sha256dH prev_local_signed_commitment_tx, current_local_signed_commitment_tx, current_remote_commitment_number, + current_local_commitment_number, payment_preimages, pending_htlcs_updated, -- 2.30.2