Skip to content

Commit 72e9dd5

Browse files
committed
Cache fields instead of storing a ChannelKeys in ChannelMonitor
We only actually use two of the fields in ChannelKeys inside a ChannelMonitor - the holder revocation_basepoint and the derivation parameters. Both are relatively small, so there isn't a lot of reason to hold a full copy of the ChannelKeys (with most of the interaction with it being inside the OnchainTxHandler). Further, this will avoid calling read on a `ChannelKeys` twice, which is a somewhat strange API quirk.
1 parent a008464 commit 72e9dd5

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,8 @@ pub struct ChannelMonitor<ChanSigner: ChannelKeys> {
626626
counterparty_payment_script: Script,
627627
shutdown_script: Script,
628628

629-
keys: ChanSigner,
629+
key_derivation_params: (u64, u64),
630+
holder_revocation_basepoint: PublicKey,
630631
funding_info: (OutPoint, Script),
631632
current_counterparty_commitment_txid: Option<Txid>,
632633
prev_counterparty_commitment_txid: Option<Txid>,
@@ -721,7 +722,8 @@ impl<ChanSigner: ChannelKeys> PartialEq for ChannelMonitor<ChanSigner> {
721722
self.destination_script != other.destination_script ||
722723
self.broadcasted_holder_revokable_script != other.broadcasted_holder_revokable_script ||
723724
self.counterparty_payment_script != other.counterparty_payment_script ||
724-
self.keys.pubkeys() != other.keys.pubkeys() ||
725+
self.key_derivation_params != other.key_derivation_params ||
726+
self.holder_revocation_basepoint != other.holder_revocation_basepoint ||
725727
self.funding_info != other.funding_info ||
726728
self.current_counterparty_commitment_txid != other.current_counterparty_commitment_txid ||
727729
self.prev_counterparty_commitment_txid != other.prev_counterparty_commitment_txid ||
@@ -785,7 +787,8 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
785787
self.counterparty_payment_script.write(writer)?;
786788
self.shutdown_script.write(writer)?;
787789

788-
self.keys.write(writer)?;
790+
self.key_derivation_params.write(writer)?;
791+
self.holder_revocation_basepoint.write(writer)?;
789792
writer.write_all(&self.funding_info.0.txid[..])?;
790793
writer.write_all(&byte_utils::be16_to_array(self.funding_info.0.index))?;
791794
self.funding_info.1.write(writer)?;
@@ -962,7 +965,9 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
962965

963966
let counterparty_tx_cache = CounterpartyCommitmentTransaction { counterparty_delayed_payment_base_key: *counterparty_delayed_payment_base_key, counterparty_htlc_base_key: *counterparty_htlc_base_key, on_counterparty_tx_csv, per_htlc: HashMap::new() };
964967

965-
let mut onchain_tx_handler = OnchainTxHandler::new(destination_script.clone(), keys.clone(), on_holder_tx_csv);
968+
let key_derivation_params = keys.key_derivation_params();
969+
let holder_revocation_basepoint = keys.pubkeys().revocation_basepoint;
970+
let mut onchain_tx_handler = OnchainTxHandler::new(destination_script.clone(), keys, on_holder_tx_csv);
966971

967972
let holder_tx_sequence = initial_holder_commitment_tx.unsigned_tx.input[0].sequence as u64;
968973
let holder_tx_locktime = initial_holder_commitment_tx.unsigned_tx.lock_time as u64;
@@ -990,7 +995,8 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
990995
counterparty_payment_script,
991996
shutdown_script,
992997

993-
keys,
998+
key_derivation_params,
999+
holder_revocation_basepoint,
9941000
funding_info,
9951001
current_counterparty_commitment_txid: None,
9961002
prev_counterparty_commitment_txid: None,
@@ -1361,7 +1367,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
13611367
let secret = self.get_secret(commitment_number).unwrap();
13621368
let per_commitment_key = ignore_error!(SecretKey::from_slice(&secret));
13631369
let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key);
1364-
let revocation_pubkey = ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &self.keys.pubkeys().revocation_basepoint));
1370+
let revocation_pubkey = ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &self.holder_revocation_basepoint));
13651371
let delayed_key = ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key), &self.counterparty_tx_cache.counterparty_delayed_payment_base_key));
13661372

13671373
let revokeable_redeemscript = chan_utils::get_revokeable_redeemscript(&revocation_pubkey, self.counterparty_tx_cache.on_counterparty_tx_csv, &delayed_key);
@@ -2193,7 +2199,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
21932199
per_commitment_point: broadcasted_holder_revokable_script.1,
21942200
to_self_delay: self.on_holder_tx_csv,
21952201
output: outp.clone(),
2196-
key_derivation_params: self.keys.key_derivation_params(),
2202+
key_derivation_params: self.key_derivation_params,
21972203
revocation_pubkey: broadcasted_holder_revokable_script.2.clone(),
21982204
});
21992205
break;
@@ -2202,7 +2208,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
22022208
spendable_output = Some(SpendableOutputDescriptor::StaticOutputCounterpartyPayment {
22032209
outpoint: OutPoint { txid: tx.txid(), index: i as u16 },
22042210
output: outp.clone(),
2205-
key_derivation_params: self.keys.key_derivation_params(),
2211+
key_derivation_params: self.key_derivation_params,
22062212
});
22072213
break;
22082214
} else if outp.script_pubkey == self.shutdown_script {
@@ -2318,7 +2324,8 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for (BlockHash, ChannelMonitor
23182324
let counterparty_payment_script = Readable::read(reader)?;
23192325
let shutdown_script = Readable::read(reader)?;
23202326

2321-
let keys = Readable::read(reader)?;
2327+
let key_derivation_params = Readable::read(reader)?;
2328+
let holder_revocation_basepoint = Readable::read(reader)?;
23222329
// Technically this can fail and serialize fail a round-trip, but only for serialization of
23232330
// barely-init'd ChannelMonitors that we can't do anything with.
23242331
let outpoint = OutPoint {
@@ -2532,7 +2539,8 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for (BlockHash, ChannelMonitor
25322539
counterparty_payment_script,
25332540
shutdown_script,
25342541

2535-
keys,
2542+
key_derivation_params,
2543+
holder_revocation_basepoint,
25362544
funding_info,
25372545
current_counterparty_commitment_txid,
25382546
prev_counterparty_commitment_txid,

0 commit comments

Comments
 (0)