Skip to content

Commit aa9de66

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 add0d7b commit aa9de66

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

lightning/src/ln/chan_utils.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,9 +522,14 @@ pub struct LocalCommitmentTransaction {
522522
impl LocalCommitmentTransaction {
523523
#[cfg(test)]
524524
pub fn dummy() -> Self {
525+
let outpoint = OutPoint {
526+
txid: Default::default(),
527+
vout: u32::max_value()
528+
};
529+
let dummy_input = TxIn { previous_output: outpoint, script_sig: Script::new(), sequence: u32::max_value(), witness: Vec::new() };
525530
Self { tx: Transaction {
526531
version: 2,
527-
input: Vec::new(),
532+
input: vec![dummy_input],
528533
output: Vec::new(),
529534
lock_time: 0,
530535
} }

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(),
@@ -1266,6 +1277,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
12661277
feerate_per_kw,
12671278
htlc_outputs,
12681279
});
1280+
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);
12691281
self.onchain_tx_handler.provide_latest_local_tx(commitment_tx);
12701282
Ok(())
12711283
}
@@ -1408,9 +1420,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
14081420
}
14091421

14101422
pub(super) fn get_cur_local_commitment_number(&self) -> u64 {
1411-
if let &Some(ref local_tx) = &self.current_local_signed_commitment_tx {
1412-
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)
1413-
} else { 0xffff_ffff_ffff }
1423+
self.current_local_commitment_number
14141424
}
14151425

14161426
/// Attempts to claim a remote commitment transaction's outputs using the revocation key and
@@ -2408,6 +2418,7 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for (Sha256dH
24082418
};
24092419

24102420
let current_remote_commitment_number = <U48 as Readable>::read(reader)?.0;
2421+
let current_local_commitment_number = <U48 as Readable>::read(reader)?.0;
24112422

24122423
let payment_preimages_len: u64 = Readable::read(reader)?;
24132424
let mut payment_preimages = HashMap::with_capacity(cmp::min(payment_preimages_len as usize, MAX_ALLOC_SIZE / 32));
@@ -2505,6 +2516,7 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for (Sha256dH
25052516
prev_local_signed_commitment_tx,
25062517
current_local_signed_commitment_tx,
25072518
current_remote_commitment_number,
2519+
current_local_commitment_number,
25082520

25092521
payment_preimages,
25102522
pending_htlcs_updated,

0 commit comments

Comments
 (0)