Skip to content

Commit e96658c

Browse files
committed
more cleanup
1 parent 68c371e commit e96658c

File tree

3 files changed

+86
-98
lines changed

3 files changed

+86
-98
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -502,9 +502,9 @@ 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 { commitment_info: ref commitment_tx, ref htlc_outputs } => {
505+
&ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo { ref commitment_info, ref htlc_outputs } => {
506506
0u8.write(w)?;
507-
commitment_tx.write(w)?;
507+
commitment_info.write(w)?;
508508
(htlc_outputs.len() as u64).write(w)?;
509509
for &(ref output, ref signature, ref source) in htlc_outputs.iter() {
510510
output.write(w)?;
@@ -947,26 +947,32 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
947947
let payment_key_hash = WPubkeyHash::hash(&keys.pubkeys().payment_point.serialize());
948948
let counterparty_payment_script = Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(&payment_key_hash[..]).into_script();
949949

950-
let counterparty_pubkeys = &channel_static_info.counterparty_pubkeys;
951-
let counterparty_delayed_payment_base_key = counterparty_pubkeys.delayed_payment_basepoint;
952-
let counterparty_htlc_base_key = counterparty_pubkeys.htlc_basepoint;
950+
let counterparty_delayed_payment_base_key = channel_static_info.counterparty_pubkeys.delayed_payment_basepoint;
951+
let counterparty_htlc_base_key = channel_static_info.counterparty_pubkeys.htlc_basepoint;
953952
let counterparty_tx_cache = CounterpartyCommitmentTransaction { counterparty_delayed_payment_base_key, counterparty_htlc_base_key, on_counterparty_tx_csv, per_htlc: HashMap::new() };
954953

955954
let mut onchain_tx_handler = OnchainTxHandler::new(destination_script.clone(), keys.clone(), channel_static_info.clone());
956955

957-
let current_holder_commitment_number = initial_holder_commitment_info.info.commitment_number;
958956
let secp_ctx = Secp256k1::new();
959957

960958
let txid = initial_holder_commitment_info.txid(channel_static_info, &secp_ctx);
961-
let holder_commitment_tx = HolderSignedTx {
962-
txid,
963-
revocation_key: initial_holder_commitment_info.info.keys.revocation_key,
964-
a_htlc_key: initial_holder_commitment_info.info.keys.broadcaster_htlc_key,
965-
b_htlc_key: initial_holder_commitment_info.info.keys.countersignatory_htlc_key,
966-
delayed_payment_key: initial_holder_commitment_info.info.keys.broadcaster_delayed_payment_key,
967-
per_commitment_point: initial_holder_commitment_info.info.keys.per_commitment_point,
968-
feerate_per_kw: initial_holder_commitment_info.info.feerate_per_kw,
969-
htlc_outputs: Vec::new(), // There are never any HTLCs in the initial commitment transactions
959+
960+
// block for Rust 1.34 compat
961+
let (holder_commitment_tx, current_holder_commitment_number) = {
962+
let info = &initial_holder_commitment_info.info;
963+
let tx_keys = &info.keys;
964+
let current_holder_commitment_number = info.commitment_number;
965+
let holder_commitment_tx = HolderSignedTx {
966+
txid,
967+
revocation_key: tx_keys.revocation_key,
968+
a_htlc_key: tx_keys.broadcaster_htlc_key,
969+
b_htlc_key: tx_keys.countersignatory_htlc_key,
970+
delayed_payment_key: tx_keys.broadcaster_delayed_payment_key,
971+
per_commitment_point: tx_keys.per_commitment_point,
972+
feerate_per_kw: info.feerate_per_kw,
973+
htlc_outputs: Vec::new(), // There are never any HTLCs in the initial commitment transactions
974+
};
975+
(holder_commitment_tx, current_holder_commitment_number)
970976
};
971977
onchain_tx_handler.provide_latest_holder_tx(initial_holder_commitment_info);
972978

@@ -1018,7 +1024,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
10181024
holder_tx_signed: false,
10191025

10201026
last_block_hash: Default::default(),
1021-
secp_ctx: secp_ctx,
1027+
secp_ctx,
10221028
}
10231029
}
10241030

@@ -1128,17 +1134,23 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
11281134
/// Panics if set_on_holder_tx_csv has never been called.
11291135
fn provide_latest_holder_commitment_tx_info(&mut self, commitment_info: HolderCommitmentTransactionInfo, htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Signature>, Option<HTLCSource>)>) -> Result<(), MonitorUpdateError> {
11301136
let txid = commitment_info.txid(&self.onchain_tx_handler.channel_static_info, &self.secp_ctx);
1131-
let mut new_holder_commitment_tx = HolderSignedTx {
1132-
txid,
1133-
revocation_key: commitment_info.info.keys.revocation_key,
1134-
a_htlc_key: commitment_info.info.keys.broadcaster_htlc_key,
1135-
b_htlc_key: commitment_info.info.keys.countersignatory_htlc_key,
1136-
delayed_payment_key: commitment_info.info.keys.broadcaster_delayed_payment_key,
1137-
per_commitment_point: commitment_info.info.keys.per_commitment_point,
1138-
feerate_per_kw: commitment_info.info.feerate_per_kw,
1139-
htlc_outputs,
1137+
1138+
// block for Rust 1.34 compat
1139+
let mut new_holder_commitment_tx = {
1140+
let info = &commitment_info.info;
1141+
let tx_keys = &info.keys;
1142+
self.current_holder_commitment_number = info.commitment_number;
1143+
HolderSignedTx {
1144+
txid,
1145+
revocation_key: tx_keys.revocation_key,
1146+
a_htlc_key: tx_keys.broadcaster_htlc_key,
1147+
b_htlc_key: tx_keys.countersignatory_htlc_key,
1148+
delayed_payment_key: tx_keys.broadcaster_delayed_payment_key,
1149+
per_commitment_point: tx_keys.per_commitment_point,
1150+
feerate_per_kw: info.feerate_per_kw,
1151+
htlc_outputs,
1152+
}
11401153
};
1141-
self.current_holder_commitment_number = commitment_info.info.commitment_number;
11421154
self.onchain_tx_handler.provide_latest_holder_tx(commitment_info);
11431155
mem::swap(&mut new_holder_commitment_tx, &mut self.current_holder_commitment_tx);
11441156
self.prev_holder_signed_commitment_tx = Some(new_holder_commitment_tx);
@@ -1177,9 +1189,9 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
11771189
}
11781190
for update in updates.updates.iter() {
11791191
match update {
1180-
ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo { commitment_info: commitment_tx, htlc_outputs } => {
1192+
ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo { commitment_info, htlc_outputs } => {
11811193
if self.lockdown_from_offchain { panic!(); }
1182-
self.provide_latest_holder_commitment_tx_info(commitment_tx.clone(), htlc_outputs.clone())?
1194+
self.provide_latest_holder_commitment_tx_info(commitment_info.clone(), htlc_outputs.clone())?
11831195
},
11841196
ChannelMonitorUpdateStep::LatestCounterpartyCommitmentTXInfo { unsigned_commitment_tx, htlc_outputs, commitment_number, their_revocation_point } =>
11851197
self.provide_latest_counterparty_commitment_tx_info(&unsigned_commitment_tx, htlc_outputs.clone(), *commitment_number, *their_revocation_point, logger),

lightning/src/ln/chan_utils.rs

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -604,16 +604,18 @@ impl ChannelStaticInfo {
604604
}
605605
}
606606

607-
/// This class tracks the information needed to build a holder's commitment transaction and to actually
608-
/// build and sign. It is used for holder transactions that we sign only when needed.
607+
/// This class tracks the information needed to build a holder's commitment transaction.
608+
/// It is used for holder transactions that we sign only when needed.
609+
///
610+
/// TODO(devrandom): take over signing functionality from HolderCommitmentTransaction
609611
#[derive(Clone)]
610612
pub struct HolderCommitmentTransactionInfo {
611613
/// The non-party-specific transaction information
612614
pub info: CommitmentTransactionInfo,
613615
/// Our counterparty's signature for the transaction
614616
pub counterparty_sig: Signature,
615617
/// All non-dust counterparty HTLC signatures, in the order they appear in the transaction
616-
pub htlc_sigs: Vec<Signature>,
618+
pub counterparty_htlc_sigs: Vec<Signature>,
617619
}
618620

619621
impl PartialEq for HolderCommitmentTransactionInfo {
@@ -624,7 +626,7 @@ impl PartialEq for HolderCommitmentTransactionInfo {
624626
}
625627

626628
impl_writeable!(HolderCommitmentTransactionInfo, 0, {
627-
info, counterparty_sig, htlc_sigs
629+
info, counterparty_sig, counterparty_htlc_sigs
628630
});
629631

630632
impl HolderCommitmentTransactionInfo {
@@ -649,7 +651,7 @@ impl HolderCommitmentTransactionInfo {
649651
}
650652
},
651653
counterparty_sig: dummy_sig,
652-
htlc_sigs: Vec::new()
654+
counterparty_htlc_sigs: Vec::new()
653655
}
654656
}
655657
pub(crate) fn txid<T: secp256k1::Signing + secp256k1::Verification>(&self, channel_static_info: &ChannelStaticInfo, secp_ctx: &Secp256k1<T>) -> Txid {
@@ -659,7 +661,7 @@ impl HolderCommitmentTransactionInfo {
659661

660662
impl HolderCommitmentTransactionInfo {
661663
pub(crate) fn to_holder_commitment_tx<T: secp256k1::Signing + secp256k1::Verification>(&self, channel_static_info: &ChannelStaticInfo, secp_ctx: &Secp256k1<T>) -> HolderCommitmentTransaction {
662-
let opt_sigs: Vec<Option<Signature>> = self.htlc_sigs.iter().map(|s| Some(s.clone())).collect();
664+
let opt_sigs: Vec<Option<Signature>> = self.counterparty_htlc_sigs.iter().map(|s| Some(s.clone())).collect();
663665
let mut htlcs = self.info.htlcs.clone();
664666
let htlcs_with_sig = htlcs.drain(..).zip(opt_sigs).collect();
665667
self.info.to_holder_commitment_tx(self.counterparty_sig, htlcs_with_sig, channel_static_info, secp_ctx)
@@ -683,9 +685,11 @@ pub struct CommitmentTransactionInfo {
683685
pub to_countersignatory_value_sat: u64,
684686
/// The feerate paid per 1000-weight-unit in this commitment transaction.
685687
pub feerate_per_kw: u32,
686-
/// The HTLCs which were included in this commitment transaction in output order.
688+
/// The non-dust HTLCs (direction, amt, height expiration, hash, transaction output index)
689+
/// which were included in this commitment transaction in output order.
690+
/// The transaction index is always populated.
687691
pub htlcs: Vec<HTLCOutputInCommitment>,
688-
// A cache of pubkeys required to construct the transaction
692+
// A cache of the parties' pubkeys required to construct the transaction
689693
pub(crate) keys: TxCreationKeys,
690694
}
691695

@@ -728,21 +732,12 @@ impl_writeable!(CommitmentTransactionInfo, 0, {
728732
impl CommitmentTransactionInfo {
729733
/// Construct an object of the class while assigning transaction output indices to HTLCs.
730734
///
731-
/// Also keeps track of associated HTLC data and returns it along with the mutated HTLCs.
732-
pub fn new_with_auxiliary_htlc_data<T: Copy>(
733-
commitment_number: u64,
734-
to_broadcaster_value_sat: u64,
735-
to_countersignatory_value_sat: u64,
736-
keys: TxCreationKeys,
737-
feerate_per_kw: u32,
738-
htlcs_with_aux: Vec<(HTLCOutputInCommitment, T)>,
739-
broadcaster_pubkeys: &ChannelPublicKeys,
740-
countersignatory_pubkeys: &ChannelPublicKeys,
741-
contest_delay: u16,
742-
secp_ctx: &Secp256k1<secp256k1::All>
743-
) -> (CommitmentTransactionInfo, Vec<(HTLCOutputInCommitment, T)>) {
744-
// Populate output indices while keeping track of the auxiliary data
745-
let mut txouts = Self::do_build_outputs(&keys, to_broadcaster_value_sat, to_countersignatory_value_sat, &htlcs_with_aux, broadcaster_pubkeys, countersignatory_pubkeys, contest_delay, &secp_ctx).unwrap();
735+
/// Also keeps track of auxiliary HTLC data and returns it along with the mutated and sorted HTLCs.
736+
/// This allows the caller to match the HTLC output index with the auxiliary data.
737+
/// This auxiliary data is not stored in this object.
738+
pub fn new_with_auxiliary_htlc_data<T: Copy>(commitment_number: u64, to_broadcaster_value_sat: u64, to_countersignatory_value_sat: u64, keys: TxCreationKeys, feerate_per_kw: u32, htlcs_with_aux: Vec<(HTLCOutputInCommitment, T)>, holder: bool, channel_static_info: &ChannelStaticInfo, secp_ctx: &Secp256k1<secp256k1::All>) -> (CommitmentTransactionInfo, Vec<(HTLCOutputInCommitment, T)>) {
739+
// Sort outputs and populate output indices while keeping track of the auxiliary data
740+
let mut txouts = Self::do_build_outputs(&keys, to_broadcaster_value_sat, to_countersignatory_value_sat, &htlcs_with_aux, holder, channel_static_info, &secp_ctx).unwrap();
746741
let mut result_htlcs_with_aux = Vec::new();
747742
let mut htlcs = Vec::new();
748743
for (idx, mut out) in txouts.drain(..).enumerate() {
@@ -769,15 +764,8 @@ impl CommitmentTransactionInfo {
769764
tx.txid()
770765
}
771766

772-
/// Build the Bitcoin transaction.
773-
///
774-
/// Required channel-static fields are provided by the caller.
775-
pub fn build<T: secp256k1::Signing + secp256k1::Verification>(
776-
&self,
777-
holder: bool,
778-
channel_static_info: &ChannelStaticInfo,
779-
secp_ctx: &Secp256k1<T>,
780-
) -> Result<(bitcoin::Transaction, Vec<HTLCOutputInCommitment>, Vec<Script>), ()> {
767+
/// Build the Bitcoin transaction
768+
pub fn build<T: secp256k1::Signing + secp256k1::Verification>(&self, holder: bool, channel_static_info: &ChannelStaticInfo, secp_ctx: &Secp256k1<T>) -> Result<(bitcoin::Transaction, Vec<HTLCOutputInCommitment>, Vec<Script>), ()> {
781769
let (obscured_commitment_transaction_number, txins) = self.build_inputs(holder, channel_static_info);
782770

783771
let mut txouts = self.build_outputs(holder, channel_static_info, secp_ctx)?;
@@ -808,18 +796,15 @@ impl CommitmentTransactionInfo {
808796

809797
fn build_outputs<T: secp256k1::Signing + secp256k1::Verification>(&self, holder: bool, channel_static_info: &ChannelStaticInfo, secp_ctx: &Secp256k1<T>) -> Result<Vec<(TxOut, (Script, Option<HTLCOutputInCommitment>))>, ()> {
810798
let htlcs = self.htlcs.iter().map(|h| (h.clone(), ())).collect();
811-
let (broadcaster_pubkeys, countersignatory_pubkeys) = channel_static_info.pubkeys(holder);
812-
let mut txouts = Self::do_build_outputs(&self.keys, self.to_broadcaster_value_sat, self.to_countersignatory_value_sat, &htlcs, broadcaster_pubkeys, countersignatory_pubkeys, channel_static_info.contest_delay(holder), secp_ctx)?;
799+
let mut txouts = Self::do_build_outputs(&self.keys, self.to_broadcaster_value_sat, self.to_countersignatory_value_sat, &htlcs, holder, channel_static_info, secp_ctx)?;
813800
let outs = txouts.drain(..).map(|(out, (s, extra))| (out, (s, extra.map(|(p, _)| p)))).collect();
814801
Ok(outs)
815802
}
816803

817-
fn do_build_outputs<T: Copy, S: secp256k1::Signing + secp256k1::Verification>(
818-
keys: &TxCreationKeys,
819-
to_broadcaster_value_sat: u64,
820-
to_countersignatory_value_sat: u64,
821-
htlcs: &Vec<(HTLCOutputInCommitment, T)>,
822-
broadcaster_pubkeys: &ChannelPublicKeys, countersignatory_pubkeys: &ChannelPublicKeys, contest_delay: u16, secp_ctx: &Secp256k1<S>) -> Result<Vec<(TxOut, (Script, Option<(HTLCOutputInCommitment, T)>))>, ()> {
804+
fn do_build_outputs<T: Copy, S: secp256k1::Signing + secp256k1::Verification>(keys: &TxCreationKeys, to_broadcaster_value_sat: u64, to_countersignatory_value_sat: u64, htlcs: &Vec<(HTLCOutputInCommitment, T)>, holder: bool, channel_static_info: &ChannelStaticInfo, secp_ctx: &Secp256k1<S>) -> Result<Vec<(TxOut, (Script, Option<(HTLCOutputInCommitment, T)>))>, ()> {
805+
let (broadcaster_pubkeys, countersignatory_pubkeys) = channel_static_info.pubkeys(holder);
806+
let contest_delay = channel_static_info.contest_delay(holder);
807+
823808
let per_commitment_point = &keys.per_commitment_point;
824809
let to_broadcaster_delayed_pubkey = derive_public_key(
825810
&secp_ctx,
@@ -928,6 +913,7 @@ impl CommitmentTransactionInfo {
928913
(sighash, unsigned_tx)
929914
}
930915

916+
// TODO(devrandom): remove this and subsume the HolderCommitmentTransaction signing functionality
931917
pub(crate) fn to_holder_commitment_tx<T: secp256k1::Signing + secp256k1::Verification>(&self, counterparty_sig: Signature, htlcs_with_sig: Vec<(HTLCOutputInCommitment, Option<Signature>)>, channel_static_info: &ChannelStaticInfo, secp_ctx: &Secp256k1<T>) -> HolderCommitmentTransaction {
932918
let (broadcaster_pubkeys, countersignatory_pubkeys) = channel_static_info.pubkeys(true);
933919
let (tx, _, _) = self.build(true, channel_static_info, secp_ctx).unwrap();

0 commit comments

Comments
 (0)