Skip to content

Commit c669158

Browse files
committed
replace HolderCommitmentTransaction with HolderCommitmentTransactionInfo, except for signing API
1 parent 863b95c commit c669158

File tree

5 files changed

+233
-120
lines changed

5 files changed

+233
-120
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 53 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use bitcoin::secp256k1;
3939

4040
use ln::msgs::DecodeError;
4141
use ln::chan_utils;
42-
use ln::chan_utils::{CounterpartyCommitmentSecrets, HTLCOutputInCommitment, HolderCommitmentTransaction, HTLCType, ChannelPublicKeys};
42+
use ln::chan_utils::{CounterpartyCommitmentSecrets, HTLCOutputInCommitment, HTLCType, ChannelPublicKeys, ChannelStaticInfo, HolderCommitmentTransactionInfo};
4343
use ln::channelmanager::{HTLCSource, PaymentPreimage, PaymentHash};
4444
use ln::onchaintx::{OnchainTxHandler, InputDescriptors};
4545
use chain::chaininterface::{BroadcasterInterface, FeeEstimator};
@@ -474,7 +474,7 @@ const MIN_SERIALIZATION_VERSION: u8 = 1;
474474
#[derive(Clone)]
475475
pub(crate) enum ChannelMonitorUpdateStep {
476476
LatestHolderCommitmentTXInfo {
477-
commitment_tx: HolderCommitmentTransaction,
477+
commitment_info: HolderCommitmentTransactionInfo,
478478
htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Signature>, Option<HTLCSource>)>,
479479
},
480480
LatestCounterpartyCommitmentTXInfo {
@@ -502,7 +502,7 @@ pub(crate) enum ChannelMonitorUpdateStep {
502502
impl Writeable for ChannelMonitorUpdateStep {
503503
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
504504
match self {
505-
&ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo { ref commitment_tx, ref htlc_outputs } => {
505+
&ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo { commitment_info: ref commitment_tx, ref htlc_outputs } => {
506506
0u8.write(w)?;
507507
commitment_tx.write(w)?;
508508
(htlc_outputs.len() as u64).write(w)?;
@@ -545,7 +545,7 @@ impl Readable for ChannelMonitorUpdateStep {
545545
match Readable::read(r)? {
546546
0u8 => {
547547
Ok(ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo {
548-
commitment_tx: Readable::read(r)?,
548+
commitment_info: Readable::read(r)?,
549549
htlc_outputs: {
550550
let len: u64 = Readable::read(r)?;
551551
let mut res = Vec::new();
@@ -936,46 +936,48 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
936936

937937
impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
938938
pub(crate) fn new(keys: ChanSigner, shutdown_pubkey: &PublicKey,
939-
on_counterparty_tx_csv: u16, destination_script: &Script, funding_info: (OutPoint, Script),
940-
counterparty_pubkeys: &ChannelPublicKeys,
941-
on_holder_tx_csv: u16, funding_redeemscript: Script, channel_value_satoshis: u64,
942-
commitment_transaction_number_obscure_factor: u64,
943-
is_outbound: bool,
944-
initial_holder_commitment_tx: HolderCommitmentTransaction) -> ChannelMonitor<ChanSigner> {
939+
on_counterparty_tx_csv: u16, destination_script: &Script, funding_info: (OutPoint, Script),
940+
channel_static_info: &ChannelStaticInfo,
941+
funding_redeemscript: Script, channel_value_satoshis: u64,
942+
commitment_transaction_number_obscure_factor: u64,
943+
initial_holder_commitment_info: HolderCommitmentTransactionInfo) -> ChannelMonitor<ChanSigner> {
945944

946945
assert!(commitment_transaction_number_obscure_factor <= (1 << 48));
947946
let our_channel_close_key_hash = WPubkeyHash::hash(&shutdown_pubkey.serialize());
948947
let shutdown_script = Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(&our_channel_close_key_hash[..]).into_script();
949948
let payment_key_hash = WPubkeyHash::hash(&keys.pubkeys().payment_point.serialize());
950949
let counterparty_payment_script = Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(&payment_key_hash[..]).into_script();
951950

951+
let counterparty_pubkeys = &channel_static_info.counterparty_pubkeys;
952952
let counterparty_delayed_payment_base_key = counterparty_pubkeys.delayed_payment_basepoint;
953953
let counterparty_htlc_base_key = counterparty_pubkeys.htlc_basepoint;
954954
let counterparty_tx_cache = CounterpartyCommitmentTransaction { counterparty_delayed_payment_base_key, counterparty_htlc_base_key, on_counterparty_tx_csv, per_htlc: HashMap::new() };
955955

956-
let mut onchain_tx_handler = OnchainTxHandler::new(destination_script.clone(), keys.clone(), on_holder_tx_csv);
956+
let mut onchain_tx_handler = OnchainTxHandler::new(destination_script.clone(), keys.clone(), channel_static_info.clone());
957957

958-
let holder_tx_sequence = initial_holder_commitment_tx.unsigned_tx.input[0].sequence as u64;
959-
let holder_tx_locktime = initial_holder_commitment_tx.unsigned_tx.lock_time as u64;
958+
let current_holder_commitment_number = initial_holder_commitment_info.info.commitment_number;
959+
let secp_ctx = Secp256k1::new();
960+
961+
let txid = initial_holder_commitment_info.txid(channel_static_info, &secp_ctx);
960962
let holder_commitment_tx = HolderSignedTx {
961-
txid: initial_holder_commitment_tx.txid(),
962-
revocation_key: initial_holder_commitment_tx.keys.revocation_key,
963-
a_htlc_key: initial_holder_commitment_tx.keys.broadcaster_htlc_key,
964-
b_htlc_key: initial_holder_commitment_tx.keys.countersignatory_htlc_key,
965-
delayed_payment_key: initial_holder_commitment_tx.keys.broadcaster_delayed_payment_key,
966-
per_commitment_point: initial_holder_commitment_tx.keys.per_commitment_point,
967-
feerate_per_kw: initial_holder_commitment_tx.feerate_per_kw,
963+
txid,
964+
revocation_key: initial_holder_commitment_info.info.keys.revocation_key,
965+
a_htlc_key: initial_holder_commitment_info.info.keys.broadcaster_htlc_key,
966+
b_htlc_key: initial_holder_commitment_info.info.keys.countersignatory_htlc_key,
967+
delayed_payment_key: initial_holder_commitment_info.info.keys.broadcaster_delayed_payment_key,
968+
per_commitment_point: initial_holder_commitment_info.info.keys.per_commitment_point,
969+
feerate_per_kw: initial_holder_commitment_info.info.feerate_per_kw,
968970
htlc_outputs: Vec::new(), // There are never any HTLCs in the initial commitment transactions
969971
};
970-
onchain_tx_handler.provide_latest_holder_tx(initial_holder_commitment_tx);
972+
onchain_tx_handler.provide_latest_holder_tx(initial_holder_commitment_info);
971973

972974
let mut outputs_to_watch = HashMap::new();
973975
outputs_to_watch.insert(funding_info.0.txid, vec![funding_info.1.clone()]);
974976

975977
ChannelMonitor {
976978
latest_update_id: 0,
977979
commitment_transaction_number_obscure_factor,
978-
is_outbound,
980+
is_outbound: channel_static_info.is_outbound_from_holder,
979981

980982
destination_script: destination_script.clone(),
981983
broadcasted_holder_revokable_script: None,
@@ -993,7 +995,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
993995
channel_value_satoshis,
994996
their_cur_revocation_points: None,
995997

996-
on_holder_tx_csv,
998+
on_holder_tx_csv: channel_static_info.counterparty_selected_contest_delay,
997999

9981000
commitment_secrets: CounterpartyCommitmentSecrets::new(),
9991001
counterparty_claimable_outpoints: HashMap::new(),
@@ -1003,7 +1005,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
10031005
prev_holder_signed_commitment_tx: None,
10041006
current_holder_commitment_tx: holder_commitment_tx,
10051007
current_counterparty_commitment_number: 1 << 48,
1006-
current_holder_commitment_number: 0xffff_ffff_ffff - ((((holder_tx_sequence & 0xffffff) << 3*8) | (holder_tx_locktime as u64 & 0xffffff)) ^ commitment_transaction_number_obscure_factor),
1008+
current_holder_commitment_number,
10071009

10081010
payment_preimages: HashMap::new(),
10091011
pending_monitor_events: Vec::new(),
@@ -1018,7 +1020,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
10181020
holder_tx_signed: false,
10191021

10201022
last_block_hash: Default::default(),
1021-
secp_ctx: Secp256k1::new(),
1023+
secp_ctx: secp_ctx,
10221024
}
10231025
}
10241026

@@ -1126,22 +1128,20 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
11261128
/// is important that any clones of this channel monitor (including remote clones) by kept
11271129
/// up-to-date as our holder commitment transaction is updated.
11281130
/// Panics if set_on_holder_tx_csv has never been called.
1129-
fn provide_latest_holder_commitment_tx_info(&mut self, commitment_tx: HolderCommitmentTransaction, htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Signature>, Option<HTLCSource>)>) -> Result<(), MonitorUpdateError> {
1130-
let txid = commitment_tx.txid();
1131-
let sequence = commitment_tx.unsigned_tx.input[0].sequence as u64;
1132-
let locktime = commitment_tx.unsigned_tx.lock_time as u64;
1131+
fn provide_latest_holder_commitment_tx_info(&mut self, commitment_info: HolderCommitmentTransactionInfo, htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Signature>, Option<HTLCSource>)>) -> Result<(), MonitorUpdateError> {
1132+
let txid = commitment_info.txid(&self.onchain_tx_handler.channel_static_info, &self.secp_ctx);
11331133
let mut new_holder_commitment_tx = HolderSignedTx {
11341134
txid,
1135-
revocation_key: commitment_tx.keys.revocation_key,
1136-
a_htlc_key: commitment_tx.keys.broadcaster_htlc_key,
1137-
b_htlc_key: commitment_tx.keys.countersignatory_htlc_key,
1138-
delayed_payment_key: commitment_tx.keys.broadcaster_delayed_payment_key,
1139-
per_commitment_point: commitment_tx.keys.per_commitment_point,
1140-
feerate_per_kw: commitment_tx.feerate_per_kw,
1135+
revocation_key: commitment_info.info.keys.revocation_key,
1136+
a_htlc_key: commitment_info.info.keys.broadcaster_htlc_key,
1137+
b_htlc_key: commitment_info.info.keys.countersignatory_htlc_key,
1138+
delayed_payment_key: commitment_info.info.keys.broadcaster_delayed_payment_key,
1139+
per_commitment_point: commitment_info.info.keys.per_commitment_point,
1140+
feerate_per_kw: commitment_info.info.feerate_per_kw,
11411141
htlc_outputs,
11421142
};
1143-
self.onchain_tx_handler.provide_latest_holder_tx(commitment_tx);
1144-
self.current_holder_commitment_number = 0xffff_ffff_ffff - ((((sequence & 0xffffff) << 3*8) | (locktime as u64 & 0xffffff)) ^ self.commitment_transaction_number_obscure_factor);
1143+
self.current_holder_commitment_number = commitment_info.info.commitment_number;
1144+
self.onchain_tx_handler.provide_latest_holder_tx(commitment_info);
11451145
mem::swap(&mut new_holder_commitment_tx, &mut self.current_holder_commitment_tx);
11461146
self.prev_holder_signed_commitment_tx = Some(new_holder_commitment_tx);
11471147
if self.holder_tx_signed {
@@ -1179,7 +1179,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
11791179
}
11801180
for update in updates.updates.drain(..) {
11811181
match update {
1182-
ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo { commitment_tx, htlc_outputs } => {
1182+
ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo { commitment_info: commitment_tx, htlc_outputs } => {
11831183
if self.lockdown_from_offchain { panic!(); }
11841184
self.provide_latest_holder_commitment_tx_info(commitment_tx, htlc_outputs)?
11851185
},
@@ -2412,7 +2412,7 @@ mod tests {
24122412
use ln::channelmanager::{PaymentPreimage, PaymentHash};
24132413
use ln::onchaintx::{OnchainTxHandler, InputDescriptors};
24142414
use ln::chan_utils;
2415-
use ln::chan_utils::{HTLCOutputInCommitment, HolderCommitmentTransaction, ChannelPublicKeys};
2415+
use ln::chan_utils::{HTLCOutputInCommitment, ChannelPublicKeys, ChannelStaticInfo, HolderCommitmentTransactionInfo};
24162416
use util::test_utils::TestLogger;
24172417
use bitcoin::secp256k1::key::{SecretKey,PublicKey};
24182418
use bitcoin::secp256k1::Secp256k1;
@@ -2490,16 +2490,24 @@ mod tests {
24902490
delayed_payment_basepoint: PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[47; 32]).unwrap()),
24912491
htlc_basepoint: PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[48; 32]).unwrap())
24922492
};
2493+
let channel_static_info = ChannelStaticInfo {
2494+
holder_pubkeys: keys.holder_channel_pubkeys.clone(),
2495+
counterparty_pubkeys,
2496+
holder_selected_contest_delay: 66,
2497+
counterparty_selected_contest_delay: 67,
2498+
funding_outpoint: Default::default(),
2499+
is_outbound_from_holder: true
2500+
};
24932501
// Prune with one old state and a holder commitment tx holding a few overlaps with the
24942502
// old state.
24952503
let mut monitor = ChannelMonitor::new(keys,
24962504
&PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap()), 0, &Script::new(),
24972505
(OutPoint { txid: Txid::from_slice(&[43; 32]).unwrap(), index: 0 }, Script::new()),
2498-
&counterparty_pubkeys,
2499-
10, Script::new(), 46, 0,
2500-
true, HolderCommitmentTransaction::dummy());
2506+
&channel_static_info,
2507+
Script::new(), 46, 0,
2508+
HolderCommitmentTransactionInfo::dummy());
25012509

2502-
monitor.provide_latest_holder_commitment_tx_info(HolderCommitmentTransaction::dummy(), preimages_to_holder_htlcs!(preimages[0..10])).unwrap();
2510+
monitor.provide_latest_holder_commitment_tx_info(HolderCommitmentTransactionInfo::dummy(), preimages_to_holder_htlcs!(preimages[0..10])).unwrap();
25032511
monitor.provide_latest_counterparty_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[5..15]), 281474976710655, dummy_key, &logger);
25042512
monitor.provide_latest_counterparty_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[15..20]), 281474976710654, dummy_key, &logger);
25052513
monitor.provide_latest_counterparty_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[17..20]), 281474976710653, dummy_key, &logger);
@@ -2525,15 +2533,15 @@ mod tests {
25252533

25262534
// Now update holder commitment tx info, pruning only element 18 as we still care about the
25272535
// previous commitment tx's preimages too
2528-
monitor.provide_latest_holder_commitment_tx_info(HolderCommitmentTransaction::dummy(), preimages_to_holder_htlcs!(preimages[0..5])).unwrap();
2536+
monitor.provide_latest_holder_commitment_tx_info(HolderCommitmentTransactionInfo::dummy(), preimages_to_holder_htlcs!(preimages[0..5])).unwrap();
25292537
secret[0..32].clone_from_slice(&hex::decode("2273e227a5b7449b6e70f1fb4652864038b1cbf9cd7c043a7d6456b7fc275ad8").unwrap());
25302538
monitor.provide_secret(281474976710653, secret.clone()).unwrap();
25312539
assert_eq!(monitor.payment_preimages.len(), 12);
25322540
test_preimages_exist!(&preimages[0..10], monitor);
25332541
test_preimages_exist!(&preimages[18..20], monitor);
25342542

25352543
// But if we do it again, we'll prune 5-10
2536-
monitor.provide_latest_holder_commitment_tx_info(HolderCommitmentTransaction::dummy(), preimages_to_holder_htlcs!(preimages[0..3])).unwrap();
2544+
monitor.provide_latest_holder_commitment_tx_info(HolderCommitmentTransactionInfo::dummy(), preimages_to_holder_htlcs!(preimages[0..3])).unwrap();
25372545
secret[0..32].clone_from_slice(&hex::decode("27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116").unwrap());
25382546
monitor.provide_secret(281474976710652, secret.clone()).unwrap();
25392547
assert_eq!(monitor.payment_preimages.len(), 5);

0 commit comments

Comments
 (0)