Skip to content

Commit 91af0bc

Browse files
committed
Use CommitmentTransaction in ChannelKeys API
1 parent 6e6d1dc commit 91af0bc

File tree

7 files changed

+401
-572
lines changed

7 files changed

+401
-572
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ const MIN_SERIALIZATION_VERSION: u8 = 1;
489489
#[derive(Clone)]
490490
pub(crate) enum ChannelMonitorUpdateStep {
491491
LatestHolderCommitmentTXInfo {
492-
commitment_info: HolderCommitmentTransaction,
492+
commitment_tx: HolderCommitmentTransaction,
493493
htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Signature>, Option<HTLCSource>)>,
494494
},
495495
LatestCounterpartyCommitmentTXInfo {
@@ -517,9 +517,9 @@ pub(crate) enum ChannelMonitorUpdateStep {
517517
impl Writeable for ChannelMonitorUpdateStep {
518518
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
519519
match self {
520-
&ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo { ref commitment_info, ref htlc_outputs } => {
520+
&ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo { ref commitment_tx, ref htlc_outputs } => {
521521
0u8.write(w)?;
522-
commitment_info.write(w)?;
522+
commitment_tx.write(w)?;
523523
(htlc_outputs.len() as u64).write(w)?;
524524
for &(ref output, ref signature, ref source) in htlc_outputs.iter() {
525525
output.write(w)?;
@@ -560,7 +560,7 @@ impl Readable for ChannelMonitorUpdateStep {
560560
match Readable::read(r)? {
561561
0u8 => {
562562
Ok(ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo {
563-
commitment_info: Readable::read(r)?,
563+
commitment_tx: Readable::read(r)?,
564564
htlc_outputs: {
565565
let len: u64 = Readable::read(r)?;
566566
let mut res = Vec::new();
@@ -952,7 +952,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
952952
channel_parameters: &ChannelTransactionParameters,
953953
funding_redeemscript: Script, channel_value_satoshis: u64,
954954
commitment_transaction_number_obscure_factor: u64,
955-
initial_holder_commitment_info: HolderCommitmentTransaction) -> ChannelMonitor<ChanSigner> {
955+
initial_holder_commitment_tx: HolderCommitmentTransaction) -> ChannelMonitor<ChanSigner> {
956956

957957
assert!(commitment_transaction_number_obscure_factor <= (1 << 48));
958958
let our_channel_close_key_hash = WPubkeyHash::hash(&shutdown_pubkey.serialize());
@@ -969,26 +969,26 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
969969
let secp_ctx = Secp256k1::new();
970970

971971
let directed_channel_parameters = channel_parameters.to_directed(true);
972-
let txid = initial_holder_commitment_info.calculate_txid(&directed_channel_parameters, &secp_ctx);
972+
let txid = initial_holder_commitment_tx.calculate_txid(&directed_channel_parameters, &secp_ctx);
973973

974974
// block for Rust 1.34 compat
975975
let (holder_commitment_tx, current_holder_commitment_number) = {
976-
let info = &initial_holder_commitment_info.info;
977-
let tx_keys = &info.keys;
978-
let current_holder_commitment_number = info.commitment_number;
976+
let commitment_tx = &initial_holder_commitment_tx.inner;
977+
let tx_keys = &commitment_tx.keys;
978+
let current_holder_commitment_number = commitment_tx.commitment_number;
979979
let holder_commitment_tx = HolderSignedTx {
980980
txid,
981981
revocation_key: tx_keys.revocation_key,
982982
a_htlc_key: tx_keys.broadcaster_htlc_key,
983983
b_htlc_key: tx_keys.countersignatory_htlc_key,
984984
delayed_payment_key: tx_keys.broadcaster_delayed_payment_key,
985985
per_commitment_point: tx_keys.per_commitment_point,
986-
feerate_per_kw: info.feerate_per_kw,
986+
feerate_per_kw: commitment_tx.feerate_per_kw,
987987
htlc_outputs: Vec::new(), // There are never any HTLCs in the initial commitment transactions
988988
};
989989
(holder_commitment_tx, current_holder_commitment_number)
990990
};
991-
onchain_tx_handler.provide_latest_holder_tx(initial_holder_commitment_info);
991+
onchain_tx_handler.provide_latest_holder_tx(initial_holder_commitment_tx);
992992

993993
let mut outputs_to_watch = HashMap::new();
994994
outputs_to_watch.insert(funding_info.0.txid, vec![(funding_info.0.index as u32, funding_info.1.clone())]);
@@ -1096,7 +1096,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
10961096
/// The monitor watches for it to be broadcasted and then uses the HTLC information (and
10971097
/// possibly future revocation/preimage information) to claim outputs where possible.
10981098
/// We cache also the mapping hash:commitment number to lighten pruning of old preimages by watchtowers.
1099-
pub(crate) fn provide_latest_counterparty_commitment_tx_info<L: Deref>(&mut self, unsigned_commitment_tx: &Transaction, htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)>, commitment_number: u64, their_revocation_point: PublicKey, logger: &L) where L::Target: Logger {
1099+
pub(crate) fn provide_latest_counterparty_commitment_tx<L: Deref>(&mut self, unsigned_commitment_tx: &Transaction, htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)>, commitment_number: u64, their_revocation_point: PublicKey, logger: &L) where L::Target: Logger {
11001100
// TODO: Encrypt the htlc_outputs data with the single-hash of the commitment transaction
11011101
// so that a remote monitor doesn't learn anything unless there is a malicious close.
11021102
// (only maybe, sadly we cant do the same for local info, as we need to be aware of
@@ -1145,26 +1145,26 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
11451145
/// is important that any clones of this channel monitor (including remote clones) by kept
11461146
/// up-to-date as our holder commitment transaction is updated.
11471147
/// Panics if set_on_holder_tx_csv has never been called.
1148-
fn provide_latest_holder_commitment_tx_info(&mut self, commitment_info: HolderCommitmentTransaction, htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Signature>, Option<HTLCSource>)>) -> Result<(), MonitorUpdateError> {
1149-
let txid = commitment_info.calculate_txid(&self.onchain_tx_handler.channel_transaction_parameters.to_directed(true), &self.secp_ctx);
1148+
fn provide_latest_holder_commitment_tx(&mut self, holder_commitment_tx: HolderCommitmentTransaction, htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Signature>, Option<HTLCSource>)>) -> Result<(), MonitorUpdateError> {
1149+
let txid = holder_commitment_tx.calculate_txid(&self.onchain_tx_handler.channel_transaction_parameters.to_directed(true), &self.secp_ctx);
11501150

11511151
// block for Rust 1.34 compat
11521152
let mut new_holder_commitment_tx = {
1153-
let info = &commitment_info.info;
1154-
let tx_keys = &info.keys;
1155-
self.current_holder_commitment_number = info.commitment_number;
1153+
let commitment_tx = &holder_commitment_tx.inner;
1154+
let tx_keys = &commitment_tx.keys;
1155+
self.current_holder_commitment_number = commitment_tx.commitment_number;
11561156
HolderSignedTx {
11571157
txid,
11581158
revocation_key: tx_keys.revocation_key,
11591159
a_htlc_key: tx_keys.broadcaster_htlc_key,
11601160
b_htlc_key: tx_keys.countersignatory_htlc_key,
11611161
delayed_payment_key: tx_keys.broadcaster_delayed_payment_key,
11621162
per_commitment_point: tx_keys.per_commitment_point,
1163-
feerate_per_kw: info.feerate_per_kw,
1163+
feerate_per_kw: commitment_tx.feerate_per_kw,
11641164
htlc_outputs,
11651165
}
11661166
};
1167-
self.onchain_tx_handler.provide_latest_holder_tx(commitment_info);
1167+
self.onchain_tx_handler.provide_latest_holder_tx(holder_commitment_tx);
11681168
mem::swap(&mut new_holder_commitment_tx, &mut self.current_holder_commitment_tx);
11691169
self.prev_holder_signed_commitment_tx = Some(new_holder_commitment_tx);
11701170
if self.holder_tx_signed {
@@ -1252,14 +1252,14 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
12521252
}
12531253
for update in updates.updates.iter() {
12541254
match update {
1255-
ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo { commitment_info, htlc_outputs } => {
1255+
ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo { commitment_tx: commitment_info, htlc_outputs } => {
12561256
log_trace!(logger, "Updating ChannelMonitor with latest holder commitment transaction info");
12571257
if self.lockdown_from_offchain { panic!(); }
1258-
self.provide_latest_holder_commitment_tx_info(commitment_info.clone(), htlc_outputs.clone())?
1258+
self.provide_latest_holder_commitment_tx(commitment_info.clone(), htlc_outputs.clone())?
12591259
},
12601260
ChannelMonitorUpdateStep::LatestCounterpartyCommitmentTXInfo { unsigned_commitment_tx, htlc_outputs, commitment_number, their_revocation_point } => {
12611261
log_trace!(logger, "Updating ChannelMonitor with latest counterparty commitment transaction info");
1262-
self.provide_latest_counterparty_commitment_tx_info(&unsigned_commitment_tx, htlc_outputs.clone(), *commitment_number, *their_revocation_point, logger)
1262+
self.provide_latest_counterparty_commitment_tx(&unsigned_commitment_tx, htlc_outputs.clone(), *commitment_number, *their_revocation_point, logger)
12631263
},
12641264
ChannelMonitorUpdateStep::PaymentPreimage { payment_preimage } => {
12651265
log_trace!(logger, "Updating ChannelMonitor with payment preimage");
@@ -2702,11 +2702,11 @@ mod tests {
27022702
Script::new(), 46, 0,
27032703
HolderCommitmentTransaction::dummy());
27042704

2705-
monitor.provide_latest_holder_commitment_tx_info(HolderCommitmentTransaction::dummy(), preimages_to_holder_htlcs!(preimages[0..10])).unwrap();
2706-
monitor.provide_latest_counterparty_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[5..15]), 281474976710655, dummy_key, &logger);
2707-
monitor.provide_latest_counterparty_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[15..20]), 281474976710654, dummy_key, &logger);
2708-
monitor.provide_latest_counterparty_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[17..20]), 281474976710653, dummy_key, &logger);
2709-
monitor.provide_latest_counterparty_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[18..20]), 281474976710652, dummy_key, &logger);
2705+
monitor.provide_latest_holder_commitment_tx(HolderCommitmentTransaction::dummy(), preimages_to_holder_htlcs!(preimages[0..10])).unwrap();
2706+
monitor.provide_latest_counterparty_commitment_tx(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[5..15]), 281474976710655, dummy_key, &logger);
2707+
monitor.provide_latest_counterparty_commitment_tx(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[15..20]), 281474976710654, dummy_key, &logger);
2708+
monitor.provide_latest_counterparty_commitment_tx(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[17..20]), 281474976710653, dummy_key, &logger);
2709+
monitor.provide_latest_counterparty_commitment_tx(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[18..20]), 281474976710652, dummy_key, &logger);
27102710
for &(ref preimage, ref hash) in preimages.iter() {
27112711
monitor.provide_payment_preimage(hash, preimage, &broadcaster, &fee_estimator, &logger);
27122712
}
@@ -2728,15 +2728,15 @@ mod tests {
27282728

27292729
// Now update holder commitment tx info, pruning only element 18 as we still care about the
27302730
// previous commitment tx's preimages too
2731-
monitor.provide_latest_holder_commitment_tx_info(HolderCommitmentTransaction::dummy(), preimages_to_holder_htlcs!(preimages[0..5])).unwrap();
2731+
monitor.provide_latest_holder_commitment_tx(HolderCommitmentTransaction::dummy(), preimages_to_holder_htlcs!(preimages[0..5])).unwrap();
27322732
secret[0..32].clone_from_slice(&hex::decode("2273e227a5b7449b6e70f1fb4652864038b1cbf9cd7c043a7d6456b7fc275ad8").unwrap());
27332733
monitor.provide_secret(281474976710653, secret.clone()).unwrap();
27342734
assert_eq!(monitor.payment_preimages.len(), 12);
27352735
test_preimages_exist!(&preimages[0..10], monitor);
27362736
test_preimages_exist!(&preimages[18..20], monitor);
27372737

27382738
// But if we do it again, we'll prune 5-10
2739-
monitor.provide_latest_holder_commitment_tx_info(HolderCommitmentTransaction::dummy(), preimages_to_holder_htlcs!(preimages[0..3])).unwrap();
2739+
monitor.provide_latest_holder_commitment_tx(HolderCommitmentTransaction::dummy(), preimages_to_holder_htlcs!(preimages[0..3])).unwrap();
27402740
secret[0..32].clone_from_slice(&hex::decode("27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116").unwrap());
27412741
monitor.provide_secret(281474976710652, secret.clone()).unwrap();
27422742
assert_eq!(monitor.payment_preimages.len(), 5);

0 commit comments

Comments
 (0)