From 0ea7dd8bfccf0369af8d8e7011997ced6df06c7a Mon Sep 17 00:00:00 2001 From: Antoine Riard Date: Mon, 15 Jun 2020 20:11:01 -0400 Subject: [PATCH] Add package.rs file Package.rs aims to gather interfaces to communicate between onchain channel transactions parser (ChannelMonitor) and outputs claiming logic (OnchainTxHandler). These interfaces are data structures, generated per-case by ChannelMonitor and consumed blindly by OnchainTxHandler. --- lightning/src/chain/channelmonitor.rs | 14 ++-- lightning/src/ln/mod.rs | 1 + lightning/src/ln/onchaintx.rs | 96 ++------------------------- lightning/src/ln/package.rs | 90 +++++++++++++++++++++++++ 4 files changed, 105 insertions(+), 96 deletions(-) create mode 100644 lightning/src/ln/package.rs diff --git a/lightning/src/chain/channelmonitor.rs b/lightning/src/chain/channelmonitor.rs index a7af1a01..a7629c20 100644 --- a/lightning/src/chain/channelmonitor.rs +++ b/lightning/src/chain/channelmonitor.rs @@ -39,7 +39,8 @@ use ln::msgs::DecodeError; use ln::chan_utils; use ln::chan_utils::{CounterpartyCommitmentSecrets, HTLCOutputInCommitment, HTLCType, ChannelTransactionParameters, HolderCommitmentTransaction}; use ln::channelmanager::{BestBlock, HTLCSource}; -use ln::onchaintx::{OnchainTxHandler, InputDescriptors}; +use ln::onchaintx::OnchainTxHandler; +use ln::package::InputDescriptors; use chain; use chain::WatchedOutput; use chain::chaininterface::{BroadcasterInterface, FeeEstimator}; @@ -352,7 +353,7 @@ pub(crate) enum InputMaterial { } } -impl Writeable for InputMaterial { +impl Writeable for InputMaterial { fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { match self { &InputMaterial::Revoked { ref per_commitment_point, ref counterparty_delayed_payment_base_key, ref counterparty_htlc_base_key, ref per_commitment_key, ref input_descriptor, ref amount, ref htlc, ref on_counterparty_tx_csv} => { @@ -3049,7 +3050,8 @@ mod tests { use chain::transaction::OutPoint; use ln::{PaymentPreimage, PaymentHash}; use ln::channelmanager::BestBlock; - use ln::onchaintx::{OnchainTxHandler, InputDescriptors}; + use ln::package; + use ln::package::InputDescriptors; use ln::chan_utils; use ln::chan_utils::{HTLCOutputInCommitment, ChannelPublicKeys, ChannelTransactionParameters, HolderCommitmentTransaction, CounterpartyChannelTransactionParameters}; use util::test_utils::{TestLogger, TestBroadcaster, TestFeeEstimator}; @@ -3263,7 +3265,7 @@ mod tests { sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs); } } - assert_eq!(base_weight + OnchainTxHandler::::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() - sum_actual_sigs)); + assert_eq!(base_weight + package::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() - sum_actual_sigs)); // Claim tx with 1 offered HTLCs, 3 received HTLCs claim_tx.input.clear(); @@ -3287,7 +3289,7 @@ mod tests { sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs); } } - assert_eq!(base_weight + OnchainTxHandler::::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() - sum_actual_sigs)); + assert_eq!(base_weight + package::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() - sum_actual_sigs)); // Justice tx with 1 revoked HTLC-Success tx output claim_tx.input.clear(); @@ -3309,7 +3311,7 @@ mod tests { sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs); } } - assert_eq!(base_weight + OnchainTxHandler::::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_isg */ (73 * inputs_des.len() - sum_actual_sigs)); + assert_eq!(base_weight + package::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_isg */ (73 * inputs_des.len() - sum_actual_sigs)); } // Further testing is done in the ChannelManager integration tests. diff --git a/lightning/src/ln/mod.rs b/lightning/src/ln/mod.rs index d093b849..3cf580a3 100644 --- a/lightning/src/ln/mod.rs +++ b/lightning/src/ln/mod.rs @@ -28,6 +28,7 @@ pub mod peer_handler; pub mod chan_utils; pub mod features; pub(crate) mod onchaintx; +pub(crate) mod package; #[cfg(feature = "fuzztarget")] pub mod peer_channel_encryptor; diff --git a/lightning/src/ln/onchaintx.rs b/lightning/src/ln/onchaintx.rs index 16bb646e..a593c2c7 100644 --- a/lightning/src/ln/onchaintx.rs +++ b/lightning/src/ln/onchaintx.rs @@ -25,6 +25,8 @@ use ln::msgs::DecodeError; use ln::PaymentPreimage; use ln::chan_utils; use ln::chan_utils::{TxCreationKeys, ChannelTransactionParameters, HolderCommitmentTransaction}; +use ln::package::InputDescriptors; +use ln::package; use chain::chaininterface::{FeeEstimator, BroadcasterInterface, ConfirmationTarget, MIN_RELAY_FEE_SAT_PER_1000_WEIGHT}; use chain::channelmonitor::{ANTI_REORG_DELAY, CLTV_SHARED_CLAIM_BUFFER, InputMaterial, ClaimRequest}; use chain::keysinterface::{Sign, KeysInterface}; @@ -123,62 +125,6 @@ impl Readable for ClaimTxBumpMaterial { } } -#[derive(PartialEq, Clone, Copy)] -pub(crate) enum InputDescriptors { - RevokedOfferedHTLC, - RevokedReceivedHTLC, - OfferedHTLC, - ReceivedHTLC, - RevokedOutput, // either a revoked to_holder output on commitment tx, a revoked HTLC-Timeout output or a revoked HTLC-Success output -} - -impl Writeable for InputDescriptors { - fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { - match self { - &InputDescriptors::RevokedOfferedHTLC => { - writer.write_all(&[0; 1])?; - }, - &InputDescriptors::RevokedReceivedHTLC => { - writer.write_all(&[1; 1])?; - }, - &InputDescriptors::OfferedHTLC => { - writer.write_all(&[2; 1])?; - }, - &InputDescriptors::ReceivedHTLC => { - writer.write_all(&[3; 1])?; - } - &InputDescriptors::RevokedOutput => { - writer.write_all(&[4; 1])?; - } - } - Ok(()) - } -} - -impl Readable for InputDescriptors { - fn read(reader: &mut R) -> Result { - let input_descriptor = match ::read(reader)? { - 0 => { - InputDescriptors::RevokedOfferedHTLC - }, - 1 => { - InputDescriptors::RevokedReceivedHTLC - }, - 2 => { - InputDescriptors::OfferedHTLC - }, - 3 => { - InputDescriptors::ReceivedHTLC - }, - 4 => { - InputDescriptors::RevokedOutput - } - _ => return Err(DecodeError::InvalidValue), - }; - Ok(input_descriptor) - } -} - macro_rules! subtract_high_prio_fee { ($logger: ident, $fee_estimator: expr, $value: expr, $predicted_weight: expr, $used_feerate: expr) => { { @@ -271,7 +217,7 @@ pub struct OnchainTxHandler { prev_holder_commitment: Option, prev_holder_htlc_sigs: Option>>, - signer: ChannelSigner, + pub(super) signer: ChannelSigner, pub(crate) channel_transaction_parameters: ChannelTransactionParameters, // Used to track claiming requests. If claim tx doesn't confirm before height timer expiration we need to bump @@ -305,7 +251,7 @@ pub struct OnchainTxHandler { latest_height: u32, - secp_ctx: Secp256k1, + pub(super) secp_ctx: Secp256k1, } const SERIALIZATION_VERSION: u8 = 1; @@ -471,36 +417,6 @@ impl OnchainTxHandler { } } - pub(crate) fn get_witnesses_weight(inputs: &[InputDescriptors]) -> usize { - let mut tx_weight = 2; // count segwit flags - for inp in inputs { - // We use expected weight (and not actual) as signatures and time lock delays may vary - tx_weight += match inp { - // number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script - &InputDescriptors::RevokedOfferedHTLC => { - 1 + 1 + 73 + 1 + 33 + 1 + 133 - }, - // number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script - &InputDescriptors::RevokedReceivedHTLC => { - 1 + 1 + 73 + 1 + 33 + 1 + 139 - }, - // number_of_witness_elements + sig_length + counterpartyhtlc_sig + preimage_length + preimage + witness_script_length + witness_script - &InputDescriptors::OfferedHTLC => { - 1 + 1 + 73 + 1 + 32 + 1 + 133 - }, - // number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script - &InputDescriptors::ReceivedHTLC => { - 1 + 1 + 73 + 1 + 1 + 1 + 139 - }, - // number_of_witness_elements + sig_length + revocation_sig + true_length + op_true + witness_script_length + witness_script - &InputDescriptors::RevokedOutput => { - 1 + 1 + 73 + 1 + 1 + 1 + 77 - }, - }; - } - tx_weight - } - /// In LN, output claimed are time-sensitive, which means we have to spend them before reaching some timelock expiration. At in-channel /// output detection, we generate a first version of a claim tx and associate to it a height timer. A height timer is an absolute block /// height than once reached we should generate a new bumped "version" of the claim tx to be sure than we safely claim outputs before @@ -592,11 +508,11 @@ impl OnchainTxHandler { for per_outp_material in cached_claim_datas.per_input_material.values() { match per_outp_material { &InputMaterial::Revoked { ref input_descriptor, ref amount, .. } => { - inputs_witnesses_weight += Self::get_witnesses_weight(&[*input_descriptor]); + inputs_witnesses_weight += package::get_witnesses_weight(&[*input_descriptor]); amt += *amount; }, &InputMaterial::CounterpartyHTLC { ref preimage, ref htlc, .. } => { - inputs_witnesses_weight += Self::get_witnesses_weight(if preimage.is_some() { &[InputDescriptors::OfferedHTLC] } else { &[InputDescriptors::ReceivedHTLC] }); + inputs_witnesses_weight += package::get_witnesses_weight(if preimage.is_some() { &[InputDescriptors::OfferedHTLC] } else { &[InputDescriptors::ReceivedHTLC] }); amt += htlc.amount_msat / 1000; }, &InputMaterial::HolderHTLC { .. } => { diff --git a/lightning/src/ln/package.rs b/lightning/src/ln/package.rs new file mode 100644 index 00000000..c0d735a0 --- /dev/null +++ b/lightning/src/ln/package.rs @@ -0,0 +1,90 @@ +//! Utilities for computing witnesses weight and feerate computation for onchain operation + +use ln::msgs::DecodeError; +use util::ser::{Readable, Writer, Writeable}; + +#[derive(PartialEq, Clone, Copy)] +pub(crate) enum InputDescriptors { + RevokedOfferedHTLC, + RevokedReceivedHTLC, + OfferedHTLC, + ReceivedHTLC, + RevokedOutput, // either a revoked to_holder output on commitment tx, a revoked HTLC-Timeout output or a revoked HTLC-Success output +} + +impl Writeable for InputDescriptors { + fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { + match self { + &InputDescriptors::RevokedOfferedHTLC => { + writer.write_all(&[0; 1])?; + }, + &InputDescriptors::RevokedReceivedHTLC => { + writer.write_all(&[1; 1])?; + }, + &InputDescriptors::OfferedHTLC => { + writer.write_all(&[2; 1])?; + }, + &InputDescriptors::ReceivedHTLC => { + writer.write_all(&[3; 1])?; + } + &InputDescriptors::RevokedOutput => { + writer.write_all(&[4; 1])?; + } + } + Ok(()) + } +} + +impl Readable for InputDescriptors { + fn read(reader: &mut R) -> Result { + let input_descriptor = match ::read(reader)? { + 0 => { + InputDescriptors::RevokedOfferedHTLC + }, + 1 => { + InputDescriptors::RevokedReceivedHTLC + }, + 2 => { + InputDescriptors::OfferedHTLC + }, + 3 => { + InputDescriptors::ReceivedHTLC + }, + 4 => { + InputDescriptors::RevokedOutput + } + _ => return Err(DecodeError::InvalidValue), + }; + Ok(input_descriptor) + } +} + +pub(crate) fn get_witnesses_weight(inputs: &[InputDescriptors]) -> usize { + let mut tx_weight = 2; // count segwit flags + for inp in inputs { + // We use expected weight (and not actual) as signatures and time lock delays may vary + tx_weight += match inp { + // number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script + &InputDescriptors::RevokedOfferedHTLC => { + 1 + 1 + 73 + 1 + 33 + 1 + 133 + }, + // number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script + &InputDescriptors::RevokedReceivedHTLC => { + 1 + 1 + 73 + 1 + 33 + 1 + 139 + }, + // number_of_witness_elements + sig_length + counterpartyhtlc_sig + preimage_length + preimage + witness_script_length + witness_script + &InputDescriptors::OfferedHTLC => { + 1 + 1 + 73 + 1 + 32 + 1 + 133 + }, + // number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script + &InputDescriptors::ReceivedHTLC => { + 1 + 1 + 73 + 1 + 1 + 1 + 139 + }, + // number_of_witness_elements + sig_length + revocation_sig + true_length + op_true + witness_script_length + witness_script + &InputDescriptors::RevokedOutput => { + 1 + 1 + 73 + 1 + 1 + 1 + 77 + }, + }; + } + tx_weight +} -- 2.30.2