Skip to content

Commit b427f7b

Browse files
committed
Use counterparty txid instead of tx in channelmonitor update
1 parent 4f1a9ff commit b427f7b

File tree

3 files changed

+32
-34
lines changed

3 files changed

+32
-34
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ use bitcoin::blockdata::transaction::{TxOut,Transaction};
2727
use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
2828
use bitcoin::blockdata::script::{Script, Builder};
2929
use bitcoin::blockdata::opcodes;
30-
use bitcoin::consensus::encode;
3130

3231
use bitcoin::hashes::Hash;
3332
use bitcoin::hashes::sha256::Hash as Sha256;
@@ -493,7 +492,7 @@ pub(crate) enum ChannelMonitorUpdateStep {
493492
htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Signature>, Option<HTLCSource>)>,
494493
},
495494
LatestCounterpartyCommitmentTXInfo {
496-
unsigned_commitment_tx: Transaction, // TODO: We should actually only need the txid here
495+
commitment_txid: Txid,
497496
htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)>,
498497
commitment_number: u64,
499498
their_revocation_point: PublicKey,
@@ -527,9 +526,9 @@ impl Writeable for ChannelMonitorUpdateStep {
527526
source.write(w)?;
528527
}
529528
}
530-
&ChannelMonitorUpdateStep::LatestCounterpartyCommitmentTXInfo { ref unsigned_commitment_tx, ref htlc_outputs, ref commitment_number, ref their_revocation_point } => {
529+
&ChannelMonitorUpdateStep::LatestCounterpartyCommitmentTXInfo { commitment_txid, ref htlc_outputs, ref commitment_number, ref their_revocation_point } => {
531530
1u8.write(w)?;
532-
unsigned_commitment_tx.write(w)?;
531+
commitment_txid.write(w)?;
533532
commitment_number.write(w)?;
534533
their_revocation_point.write(w)?;
535534
(htlc_outputs.len() as u64).write(w)?;
@@ -573,7 +572,7 @@ impl Readable for ChannelMonitorUpdateStep {
573572
},
574573
1u8 => {
575574
Ok(ChannelMonitorUpdateStep::LatestCounterpartyCommitmentTXInfo {
576-
unsigned_commitment_tx: Readable::read(r)?,
575+
commitment_txid: Readable::read(r)?,
577576
commitment_number: Readable::read(r)?,
578577
their_revocation_point: Readable::read(r)?,
579578
htlc_outputs: {
@@ -1097,7 +1096,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
10971096
/// The monitor watches for it to be broadcasted and then uses the HTLC information (and
10981097
/// possibly future revocation/preimage information) to claim outputs where possible.
10991098
/// We cache also the mapping hash:commitment number to lighten pruning of old preimages by watchtowers.
1100-
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 {
1099+
pub(crate) fn provide_latest_counterparty_commitment_tx<L: Deref>(&mut self, txid: Txid, htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)>, commitment_number: u64, their_revocation_point: PublicKey, logger: &L) where L::Target: Logger {
11011100
// TODO: Encrypt the htlc_outputs data with the single-hash of the commitment transaction
11021101
// so that a remote monitor doesn't learn anything unless there is a malicious close.
11031102
// (only maybe, sadly we cant do the same for local info, as we need to be aware of
@@ -1106,12 +1105,12 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
11061105
self.counterparty_hash_commitment_number.insert(htlc.payment_hash, commitment_number);
11071106
}
11081107

1109-
let new_txid = unsigned_commitment_tx.txid();
1110-
log_trace!(logger, "Tracking new counterparty commitment transaction with txid {} at commitment number {} with {} HTLC outputs", new_txid, commitment_number, htlc_outputs.len());
1111-
log_trace!(logger, "New potential counterparty commitment transaction: {}", encode::serialize_hex(unsigned_commitment_tx));
1108+
log_trace!(logger, "Tracking new counterparty commitment transaction with txid {} at commitment number {} with {} HTLC outputs", txid, commitment_number, htlc_outputs.len());
1109+
// TODO do we really want to log the transaction hex?
1110+
// log_trace!(logger, "New potential counterparty commitment transaction: {}", encode::serialize_hex(unsigned_commitment_tx));
11121111
self.prev_counterparty_commitment_txid = self.current_counterparty_commitment_txid.take();
1113-
self.current_counterparty_commitment_txid = Some(new_txid);
1114-
self.counterparty_claimable_outpoints.insert(new_txid, htlc_outputs.clone());
1112+
self.current_counterparty_commitment_txid = Some(txid);
1113+
self.counterparty_claimable_outpoints.insert(txid, htlc_outputs.clone());
11151114
self.current_counterparty_commitment_number = commitment_number;
11161115
//TODO: Merge this into the other per-counterparty-transaction output storage stuff
11171116
match self.their_cur_revocation_points {
@@ -1138,7 +1137,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
11381137
htlcs.push(htlc.0);
11391138
}
11401139
}
1141-
self.counterparty_tx_cache.per_htlc.insert(new_txid, htlcs);
1140+
self.counterparty_tx_cache.per_htlc.insert(txid, htlcs);
11421141
}
11431142

11441143
/// Informs this monitor of the latest holder (ie broadcastable) commitment transaction. The
@@ -1258,9 +1257,9 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
12581257
if self.lockdown_from_offchain { panic!(); }
12591258
self.provide_latest_holder_commitment_tx(commitment_info.clone(), htlc_outputs.clone())?
12601259
},
1261-
ChannelMonitorUpdateStep::LatestCounterpartyCommitmentTXInfo { unsigned_commitment_tx, htlc_outputs, commitment_number, their_revocation_point } => {
1260+
ChannelMonitorUpdateStep::LatestCounterpartyCommitmentTXInfo { commitment_txid, htlc_outputs, commitment_number, their_revocation_point } => {
12621261
log_trace!(logger, "Updating ChannelMonitor with latest counterparty commitment transaction info");
1263-
self.provide_latest_counterparty_commitment_tx(&unsigned_commitment_tx, htlc_outputs.clone(), *commitment_number, *their_revocation_point, logger)
1262+
self.provide_latest_counterparty_commitment_tx(*commitment_txid, htlc_outputs.clone(), *commitment_number, *their_revocation_point, logger)
12641263
},
12651264
ChannelMonitorUpdateStep::PaymentPreimage { payment_preimage } => {
12661265
log_trace!(logger, "Updating ChannelMonitor with payment preimage");
@@ -2707,10 +2706,11 @@ mod tests {
27072706
HolderCommitmentTransaction::dummy());
27082707

27092708
monitor.provide_latest_holder_commitment_tx(HolderCommitmentTransaction::dummy(), preimages_to_holder_htlcs!(preimages[0..10])).unwrap();
2710-
monitor.provide_latest_counterparty_commitment_tx(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[5..15]), 281474976710655, dummy_key, &logger);
2711-
monitor.provide_latest_counterparty_commitment_tx(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[15..20]), 281474976710654, dummy_key, &logger);
2712-
monitor.provide_latest_counterparty_commitment_tx(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[17..20]), 281474976710653, dummy_key, &logger);
2713-
monitor.provide_latest_counterparty_commitment_tx(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[18..20]), 281474976710652, dummy_key, &logger);
2709+
let dummy_txid = dummy_tx.txid();
2710+
monitor.provide_latest_counterparty_commitment_tx(dummy_txid, preimages_slice_to_htlc_outputs!(preimages[5..15]), 281474976710655, dummy_key, &logger);
2711+
monitor.provide_latest_counterparty_commitment_tx(dummy_txid, preimages_slice_to_htlc_outputs!(preimages[15..20]), 281474976710654, dummy_key, &logger);
2712+
monitor.provide_latest_counterparty_commitment_tx(dummy_txid, preimages_slice_to_htlc_outputs!(preimages[17..20]), 281474976710653, dummy_key, &logger);
2713+
monitor.provide_latest_counterparty_commitment_tx(dummy_txid, preimages_slice_to_htlc_outputs!(preimages[18..20]), 281474976710652, dummy_key, &logger);
27142714
for &(ref preimage, ref hash) in preimages.iter() {
27152715
monitor.provide_payment_preimage(hash, preimage, &broadcaster, &fee_estimator, &logger);
27162716
}

lightning/src/ln/chan_utils.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -656,8 +656,6 @@ impl<'a> DirectedChannelTransactionParameters<'a> {
656656

657657
/// This class tracks the per-transaction information needed to build a holder's commitment transaction.
658658
/// It is used for holder transactions that we sign only when needed.
659-
///
660-
/// TODO(devrandom): take over signing functionality from HolderCommitmentTransactionForSigning
661659
#[derive(Clone)]
662660
pub struct HolderCommitmentTransaction {
663661
/// The non-party-specific transaction information

lightning/src/ln/channel.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,7 +1460,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
14601460
Ok(())
14611461
}
14621462

1463-
fn funding_created_signature<L: Deref>(&mut self, sig: &Signature, logger: &L) -> Result<(Transaction, CommitmentTransaction, Signature), ChannelError> where L::Target: Logger {
1463+
fn funding_created_signature<L: Deref>(&mut self, sig: &Signature, logger: &L) -> Result<(Txid, CommitmentTransaction, Signature), ChannelError> where L::Target: Logger {
14641464
let funding_script = self.get_funding_redeemscript();
14651465

14661466
let keys = self.build_holder_transaction_keys(self.cur_holder_commitment_transaction_number)?;
@@ -1474,12 +1474,12 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
14741474
let counterparty_keys = self.build_remote_transaction_keys()?;
14751475
let counterparty_initial_commitment_tx = self.build_commitment_transaction(self.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, false, self.feerate_per_kw, logger).0;
14761476

1477-
let counterparty_initial_commitment_bitcoin_tx = counterparty_initial_commitment_tx.build(&self.get_channel_parameters(false), &self.secp_ctx).unwrap().0;
1477+
let counterparty_initial_commitment_txid = counterparty_initial_commitment_tx.calculate_txid(&self.get_channel_parameters(false), &self.secp_ctx);
14781478
let counterparty_signature = self.holder_keys.sign_counterparty_commitment(&counterparty_initial_commitment_tx, &self.secp_ctx)
14791479
.map_err(|_| ChannelError::Close("Failed to get signatures for new commitment_signed".to_owned()))?.0;
14801480

14811481
// We sign "counterparty" commitment transaction, allowing them to broadcast the tx if they wish.
1482-
Ok((counterparty_initial_commitment_bitcoin_tx, initial_commitment_tx, counterparty_signature))
1482+
Ok((counterparty_initial_commitment_txid, initial_commitment_tx, counterparty_signature))
14831483
}
14841484

14851485
// Create a directed version of the channel static fields for the holder or the counterparty.
@@ -1512,7 +1512,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
15121512
self.channel_transaction_parameters.funding_outpoint = Some(funding_txo);
15131513
self.holder_keys.ready_channel(&self.channel_transaction_parameters);
15141514

1515-
let (counterparty_initial_commitment_bitcoin_tx, initial_commitment_tx, signature) = match self.funding_created_signature(&msg.signature, logger) {
1515+
let (counterparty_initial_commitment_txid, initial_commitment_tx, signature) = match self.funding_created_signature(&msg.signature, logger) {
15161516
Ok(res) => res,
15171517
Err(e) => {
15181518
self.channel_transaction_parameters.funding_outpoint = None;
@@ -1540,7 +1540,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
15401540
self.get_commitment_transaction_number_obscure_factor(),
15411541
holder_commitment_tx);
15421542

1543-
channel_monitor.provide_latest_counterparty_commitment_tx(&counterparty_initial_commitment_bitcoin_tx, Vec::new(), self.cur_counterparty_commitment_transaction_number, self.counterparty_cur_commitment_point.unwrap(), logger);
1543+
channel_monitor.provide_latest_counterparty_commitment_tx(counterparty_initial_commitment_txid, Vec::new(), self.cur_counterparty_commitment_transaction_number, self.counterparty_cur_commitment_point.unwrap(), logger);
15441544

15451545
self.channel_state = ChannelState::FundingSent as u32;
15461546
self.channel_id = funding_txo.to_channel_id();
@@ -1573,7 +1573,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
15731573
let counterparty_keys = self.build_remote_transaction_keys()?;
15741574
let counterparty_initial_commitment_tx = self.build_commitment_transaction(self.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, false, self.feerate_per_kw, logger).0;
15751575
// TODO this is not integration tested
1576-
let counterparty_initial_commitment_bitcoin_tx = counterparty_initial_commitment_tx.build(&self.get_channel_parameters(false), &self.secp_ctx).unwrap().0;
1576+
let counterparty_initial_commitment_txid = counterparty_initial_commitment_tx.calculate_txid(&self.get_channel_parameters(false), &self.secp_ctx);
15771577

15781578
let holder_keys = self.build_holder_transaction_keys(self.cur_holder_commitment_transaction_number)?;
15791579
let initial_commitment_tx = self.build_commitment_transaction(self.cur_holder_commitment_transaction_number, &holder_keys, true, false, self.feerate_per_kw, logger).0;
@@ -1602,7 +1602,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
16021602
self.get_commitment_transaction_number_obscure_factor(),
16031603
holder_commitment_tx);
16041604

1605-
channel_monitor.provide_latest_counterparty_commitment_tx(&counterparty_initial_commitment_bitcoin_tx, Vec::new(), self.cur_counterparty_commitment_transaction_number, self.counterparty_cur_commitment_point.unwrap(), logger);
1605+
channel_monitor.provide_latest_counterparty_commitment_tx(counterparty_initial_commitment_txid, Vec::new(), self.cur_counterparty_commitment_transaction_number, self.counterparty_cur_commitment_point.unwrap(), logger);
16061606

16071607
assert_eq!(self.channel_state & (ChannelState::MonitorUpdateFailed as u32), 0); // We have no had any monitor(s) yet to fail update!
16081608
self.channel_state = ChannelState::FundingSent as u32;
@@ -3849,7 +3849,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
38493849
}
38503850
self.resend_order = RAACommitmentOrder::RevokeAndACKFirst;
38513851

3852-
let (res, counterparty_commitment_tx, htlcs) = match self.send_commitment_no_state_update(logger) {
3852+
let (res, counterparty_commitment_txid, htlcs) = match self.send_commitment_no_state_update(logger) {
38533853
Ok((res, (counterparty_commitment_tx, mut htlcs))) => {
38543854
// Update state now that we've passed all the can-fail calls...
38553855
let htlcs_no_ref: Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)> =
@@ -3863,7 +3863,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
38633863
let monitor_update = ChannelMonitorUpdate {
38643864
update_id: self.latest_monitor_update_id,
38653865
updates: vec![ChannelMonitorUpdateStep::LatestCounterpartyCommitmentTXInfo {
3866-
unsigned_commitment_tx: counterparty_commitment_tx.clone(),
3866+
commitment_txid: counterparty_commitment_txid,
38673867
htlc_outputs: htlcs.clone(),
38683868
commitment_number: self.cur_counterparty_commitment_transaction_number,
38693869
their_revocation_point: self.counterparty_cur_commitment_point.unwrap()
@@ -3875,7 +3875,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
38753875

38763876
/// Only fails in case of bad keys. Used for channel_reestablish commitment_signed generation
38773877
/// when we shouldn't change HTLC/channel state.
3878-
fn send_commitment_no_state_update<L: Deref>(&self, logger: &L) -> Result<(msgs::CommitmentSigned, (Transaction, Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)>)), ChannelError> where L::Target: Logger {
3878+
fn send_commitment_no_state_update<L: Deref>(&self, logger: &L) -> Result<(msgs::CommitmentSigned, (Txid, Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)>)), ChannelError> where L::Target: Logger {
38793879
let mut feerate_per_kw = self.feerate_per_kw;
38803880
if let Some(feerate) = self.pending_update_fee {
38813881
if self.is_outbound() {
@@ -3885,7 +3885,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
38853885

38863886
let counterparty_keys = self.build_remote_transaction_keys()?;
38873887
let counterparty_commitment_tx = self.build_commitment_transaction(self.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, true, feerate_per_kw, logger);
3888-
let counterparty_commitment_bitcoin_tx = counterparty_commitment_tx.0.build(&self.get_channel_parameters(false), &self.secp_ctx).unwrap().0;
3888+
let counterparty_commitment_txid = counterparty_commitment_tx.0.calculate_txid(&self.get_channel_parameters(false), &self.secp_ctx);
38893889
let (signature, htlc_signatures);
38903890

38913891
{
@@ -3900,13 +3900,13 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
39003900
htlc_signatures = res.1;
39013901

39023902
log_trace!(logger, "Signed remote commitment tx {} with redeemscript {} -> {}",
3903-
encode::serialize_hex(&counterparty_commitment_bitcoin_tx),
3903+
&counterparty_commitment_txid,
39043904
encode::serialize_hex(&self.get_funding_redeemscript()),
39053905
log_bytes!(signature.serialize_compact()[..]));
39063906

39073907
for (ref htlc_sig, ref htlc) in htlc_signatures.iter().zip(htlcs) {
39083908
log_trace!(logger, "Signed remote HTLC tx {} with redeemscript {} with pubkey {} -> {}",
3909-
encode::serialize_hex(&chan_utils::build_htlc_transaction(&counterparty_commitment_bitcoin_tx.txid(), feerate_per_kw, self.get_holder_selected_contest_delay(), htlc, &counterparty_keys.broadcaster_delayed_payment_key, &counterparty_keys.revocation_key)),
3909+
encode::serialize_hex(&chan_utils::build_htlc_transaction(&counterparty_commitment_txid, feerate_per_kw, self.get_holder_selected_contest_delay(), htlc, &counterparty_keys.broadcaster_delayed_payment_key, &counterparty_keys.revocation_key)),
39103910
encode::serialize_hex(&chan_utils::get_htlc_redeemscript(&htlc, &counterparty_keys)),
39113911
log_bytes!(counterparty_keys.broadcaster_htlc_key.serialize()),
39123912
log_bytes!(htlc_sig.serialize_compact()[..]));
@@ -3917,7 +3917,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
39173917
channel_id: self.channel_id,
39183918
signature,
39193919
htlc_signatures,
3920-
}, (counterparty_commitment_bitcoin_tx, counterparty_commitment_tx.2)))
3920+
}, (counterparty_commitment_txid, counterparty_commitment_tx.2)))
39213921
}
39223922

39233923
/// Adds a pending outbound HTLC to this channel, and creates a signed commitment transaction

0 commit comments

Comments
 (0)