Skip to content

Commit c5fca8c

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 9c9c881 commit c5fca8c

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)?;
@@ -965,7 +968,9 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
965968
let counterparty_htlc_base_key = counterparty_channel_parameters.pubkeys.htlc_basepoint;
966969
let counterparty_tx_cache = CounterpartyCommitmentTransaction { counterparty_delayed_payment_base_key, counterparty_htlc_base_key, on_counterparty_tx_csv, per_htlc: HashMap::new() };
967970

968-
let mut onchain_tx_handler = OnchainTxHandler::new(destination_script.clone(), keys.clone(), channel_parameters.clone());
971+
let key_derivation_params = keys.key_derivation_params();
972+
let holder_revocation_basepoint = keys.pubkeys().revocation_basepoint;
973+
let mut onchain_tx_handler = OnchainTxHandler::new(destination_script.clone(), keys, channel_parameters.clone());
969974

970975
let secp_ctx = Secp256k1::new();
971976

@@ -1001,7 +1006,8 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
10011006
counterparty_payment_script,
10021007
shutdown_script,
10031008

1004-
keys,
1009+
key_derivation_params,
1010+
holder_revocation_basepoint,
10051011
funding_info,
10061012
current_counterparty_commitment_txid: None,
10071013
prev_counterparty_commitment_txid: None,
@@ -1373,7 +1379,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
13731379
let secret = self.get_secret(commitment_number).unwrap();
13741380
let per_commitment_key = ignore_error!(SecretKey::from_slice(&secret));
13751381
let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key);
1376-
let revocation_pubkey = ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &self.keys.pubkeys().revocation_basepoint));
1382+
let revocation_pubkey = ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &self.holder_revocation_basepoint));
13771383
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));
13781384

13791385
let revokeable_redeemscript = chan_utils::get_revokeable_redeemscript(&revocation_pubkey, self.counterparty_tx_cache.on_counterparty_tx_csv, &delayed_key);
@@ -2205,7 +2211,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
22052211
per_commitment_point: broadcasted_holder_revokable_script.1,
22062212
to_self_delay: self.on_holder_tx_csv,
22072213
output: outp.clone(),
2208-
key_derivation_params: self.keys.key_derivation_params(),
2214+
key_derivation_params: self.key_derivation_params,
22092215
revocation_pubkey: broadcasted_holder_revokable_script.2.clone(),
22102216
});
22112217
break;
@@ -2214,7 +2220,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
22142220
spendable_output = Some(SpendableOutputDescriptor::StaticOutputCounterpartyPayment {
22152221
outpoint: OutPoint { txid: tx.txid(), index: i as u16 },
22162222
output: outp.clone(),
2217-
key_derivation_params: self.keys.key_derivation_params(),
2223+
key_derivation_params: self.key_derivation_params,
22182224
});
22192225
break;
22202226
} else if outp.script_pubkey == self.shutdown_script {
@@ -2330,7 +2336,8 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for (BlockHash, ChannelMonitor
23302336
let counterparty_payment_script = Readable::read(reader)?;
23312337
let shutdown_script = Readable::read(reader)?;
23322338

2333-
let keys = Readable::read(reader)?;
2339+
let key_derivation_params = Readable::read(reader)?;
2340+
let holder_revocation_basepoint = Readable::read(reader)?;
23342341
// Technically this can fail and serialize fail a round-trip, but only for serialization of
23352342
// barely-init'd ChannelMonitors that we can't do anything with.
23362343
let outpoint = OutPoint {
@@ -2544,7 +2551,8 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for (BlockHash, ChannelMonitor
25442551
counterparty_payment_script,
25452552
shutdown_script,
25462553

2547-
keys,
2554+
key_derivation_params,
2555+
holder_revocation_basepoint,
25482556
funding_info,
25492557
current_counterparty_commitment_txid,
25502558
prev_counterparty_commitment_txid,

0 commit comments

Comments
 (0)