Skip to content

Commit 97f5f40

Browse files
author
Antoine Riard
committed
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.
1 parent ad17bed commit 97f5f40

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

lightning/src/ln/channelmonitor.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,9 @@ pub struct ChannelMonitor<ChanSigner: ChannelKeys> {
780780
// Used just for ChannelManager to make sure it has the latest channel data during
781781
// deserialization
782782
current_remote_commitment_number: u64,
783+
// Used just for ChannelManager to make sure it has the latest channel data during
784+
// deserialization
785+
current_local_commitment_number: u64,
783786

784787
payment_preimages: HashMap<PaymentHash, PaymentPreimage>,
785788

@@ -836,6 +839,7 @@ impl<ChanSigner: ChannelKeys> PartialEq for ChannelMonitor<ChanSigner> {
836839
self.remote_hash_commitment_number != other.remote_hash_commitment_number ||
837840
self.prev_local_signed_commitment_tx != other.prev_local_signed_commitment_tx ||
838841
self.current_remote_commitment_number != other.current_remote_commitment_number ||
842+
self.current_local_commitment_number != other.current_local_commitment_number ||
839843
self.current_local_signed_commitment_tx != other.current_local_signed_commitment_tx ||
840844
self.payment_preimages != other.payment_preimages ||
841845
self.pending_htlcs_updated != other.pending_htlcs_updated ||
@@ -1008,6 +1012,12 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
10081012
writer.write_all(&byte_utils::be48_to_array(0))?;
10091013
}
10101014

1015+
if for_local_storage {
1016+
writer.write_all(&byte_utils::be48_to_array(self.current_local_commitment_number))?;
1017+
} else {
1018+
writer.write_all(&byte_utils::be48_to_array(0))?;
1019+
}
1020+
10111021
writer.write_all(&byte_utils::be64_to_array(self.payment_preimages.len() as u64))?;
10121022
for payment_preimage in self.payment_preimages.values() {
10131023
writer.write_all(&payment_preimage.0[..])?;
@@ -1126,6 +1136,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
11261136
prev_local_signed_commitment_tx: None,
11271137
current_local_signed_commitment_tx: None,
11281138
current_remote_commitment_number: 1 << 48,
1139+
current_local_commitment_number: 0xffff_ffff_ffff,
11291140

11301141
payment_preimages: HashMap::new(),
11311142
pending_htlcs_updated: Vec::new(),
@@ -1262,6 +1273,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
12621273
if let Err(_) = self.onchain_tx_handler.provide_latest_local_tx(commitment_tx.clone()) {
12631274
return Err(MonitorUpdateError("Local commitment signed has already been signed, no further update of LOCAL commitment transaction is allowed"));
12641275
}
1276+
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);
12651277
self.prev_local_signed_commitment_tx = self.current_local_signed_commitment_tx.take();
12661278
self.current_local_signed_commitment_tx = Some(LocalSignedTx {
12671279
txid: commitment_tx.txid(),
@@ -1415,9 +1427,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
14151427
}
14161428

14171429
pub(super) fn get_cur_local_commitment_number(&self) -> u64 {
1418-
if let &Some(ref local_tx) = &self.current_local_signed_commitment_tx {
1419-
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)
1420-
} else { 0xffff_ffff_ffff }
1430+
self.current_local_commitment_number
14211431
}
14221432

14231433
/// Attempts to claim a remote commitment transaction's outputs using the revocation key and
@@ -2415,6 +2425,7 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for (Sha256dH
24152425
};
24162426

24172427
let current_remote_commitment_number = <U48 as Readable>::read(reader)?.0;
2428+
let current_local_commitment_number = <U48 as Readable>::read(reader)?.0;
24182429

24192430
let payment_preimages_len: u64 = Readable::read(reader)?;
24202431
let mut payment_preimages = HashMap::with_capacity(cmp::min(payment_preimages_len as usize, MAX_ALLOC_SIZE / 32));
@@ -2512,6 +2523,7 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for (Sha256dH
25122523
prev_local_signed_commitment_tx,
25132524
current_local_signed_commitment_tx,
25142525
current_remote_commitment_number,
2526+
current_local_commitment_number,
25152527

25162528
payment_preimages,
25172529
pending_htlcs_updated,

0 commit comments

Comments
 (0)