Add package.rs file
[rust-lightning] / lightning / src / ln / onchaintx.rs
index e0aa6eddd9e5ea7ccc60dc6b4f358838128ae58a..a593c2c76ef8cfd69631851da5d4d89b49e11017 100644 (file)
@@ -22,9 +22,11 @@ use bitcoin::secp256k1::{Secp256k1, Signature};
 use bitcoin::secp256k1;
 
 use ln::msgs::DecodeError;
-use ln::channelmanager::PaymentPreimage;
+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};
@@ -33,9 +35,9 @@ use util::ser::{Readable, ReadableArgs, Writer, Writeable, VecWriter};
 use util::byte_utils;
 
 use std::collections::HashMap;
-use std::cmp;
-use std::ops::Deref;
-use std::mem::replace;
+use core::cmp;
+use core::ops::Deref;
+use core::mem::replace;
 
 const MAX_ALLOC_SIZE: usize = 64*1024;
 
@@ -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<W: Writer>(&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<R: ::std::io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
-               let input_descriptor = match <u8 as Readable>::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) => {
                {
@@ -220,7 +166,7 @@ impl Readable for Option<Vec<Option<(usize, Signature)>>> {
                        0u8 => Ok(None),
                        1u8 => {
                                let vlen: u64 = Readable::read(reader)?;
-                               let mut ret = Vec::with_capacity(cmp::min(vlen as usize, MAX_ALLOC_SIZE / ::std::mem::size_of::<Option<(usize, Signature)>>()));
+                               let mut ret = Vec::with_capacity(cmp::min(vlen as usize, MAX_ALLOC_SIZE / ::core::mem::size_of::<Option<(usize, Signature)>>()));
                                for _ in 0..vlen {
                                        ret.push(match Readable::read(reader)? {
                                                0u8 => None,
@@ -271,7 +217,7 @@ pub struct OnchainTxHandler<ChannelSigner: Sign> {
        prev_holder_commitment: Option<HolderCommitmentTransaction>,
        prev_holder_htlc_sigs: Option<Vec<Option<(usize, Signature)>>>,
 
-       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
@@ -301,15 +247,20 @@ pub struct OnchainTxHandler<ChannelSigner: Sign> {
        #[cfg(not(test))]
        claimable_outpoints: HashMap<BitcoinOutPoint, (Txid, u32)>,
 
-       onchain_events_waiting_threshold_conf: Vec<OnchainEventEntry>,
+       onchain_events_awaiting_threshold_conf: Vec<OnchainEventEntry>,
 
        latest_height: u32,
 
-       secp_ctx: Secp256k1<secp256k1::All>,
+       pub(super) secp_ctx: Secp256k1<secp256k1::All>,
 }
 
+const SERIALIZATION_VERSION: u8 = 1;
+const MIN_SERIALIZATION_VERSION: u8 = 1;
+
 impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
        pub(crate) fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
+               write_ver_prefix!(writer, SERIALIZATION_VERSION, MIN_SERIALIZATION_VERSION);
+
                self.destination_script.write(writer)?;
                self.holder_commitment.write(writer)?;
                self.holder_htlc_sigs.write(writer)?;
@@ -320,8 +271,8 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
 
                let mut key_data = VecWriter(Vec::new());
                self.signer.write(&mut key_data)?;
-               assert!(key_data.0.len() < std::usize::MAX);
-               assert!(key_data.0.len() < std::u32::MAX as usize);
+               assert!(key_data.0.len() < core::usize::MAX);
+               assert!(key_data.0.len() < core::u32::MAX as usize);
                (key_data.0.len() as u32).write(writer)?;
                writer.write_all(&key_data.0[..])?;
 
@@ -338,8 +289,8 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
                        claim_and_height.1.write(writer)?;
                }
 
-               writer.write_all(&byte_utils::be64_to_array(self.onchain_events_waiting_threshold_conf.len() as u64))?;
-               for ref entry in self.onchain_events_waiting_threshold_conf.iter() {
+               writer.write_all(&byte_utils::be64_to_array(self.onchain_events_awaiting_threshold_conf.len() as u64))?;
+               for ref entry in self.onchain_events_awaiting_threshold_conf.iter() {
                        entry.txid.write(writer)?;
                        writer.write_all(&byte_utils::be32_to_array(entry.height))?;
                        match entry.event {
@@ -355,12 +306,16 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
                        }
                }
                self.latest_height.write(writer)?;
+
+               write_tlv_fields!(writer, {}, {});
                Ok(())
        }
 }
 
 impl<'a, K: KeysInterface> ReadableArgs<&'a K> for OnchainTxHandler<K::Signer> {
        fn read<R: ::std::io::Read>(reader: &mut R, keys_manager: &'a K) -> Result<Self, DecodeError> {
+               let _ver = read_ver_prefix!(reader, SERIALIZATION_VERSION);
+
                let destination_script = Readable::read(reader)?;
 
                let holder_commitment = Readable::read(reader)?;
@@ -396,7 +351,7 @@ impl<'a, K: KeysInterface> ReadableArgs<&'a K> for OnchainTxHandler<K::Signer> {
                        claimable_outpoints.insert(outpoint, (ancestor_claim_txid, height));
                }
                let waiting_threshold_conf_len: u64 = Readable::read(reader)?;
-               let mut onchain_events_waiting_threshold_conf = Vec::with_capacity(cmp::min(waiting_threshold_conf_len as usize, MAX_ALLOC_SIZE / 128));
+               let mut onchain_events_awaiting_threshold_conf = Vec::with_capacity(cmp::min(waiting_threshold_conf_len as usize, MAX_ALLOC_SIZE / 128));
                for _ in 0..waiting_threshold_conf_len {
                        let txid = Readable::read(reader)?;
                        let height = Readable::read(reader)?;
@@ -417,10 +372,12 @@ impl<'a, K: KeysInterface> ReadableArgs<&'a K> for OnchainTxHandler<K::Signer> {
                                }
                                _ => return Err(DecodeError::InvalidValue),
                        };
-                       onchain_events_waiting_threshold_conf.push(OnchainEventEntry { txid, height, event });
+                       onchain_events_awaiting_threshold_conf.push(OnchainEventEntry { txid, height, event });
                }
                let latest_height = Readable::read(reader)?;
 
+               read_tlv_fields!(reader, {}, {});
+
                let mut secp_ctx = Secp256k1::new();
                secp_ctx.seeded_randomize(&keys_manager.get_secure_random_bytes());
 
@@ -434,7 +391,7 @@ impl<'a, K: KeysInterface> ReadableArgs<&'a K> for OnchainTxHandler<K::Signer> {
                        channel_transaction_parameters: channel_parameters,
                        claimable_outpoints,
                        pending_claim_requests,
-                       onchain_events_waiting_threshold_conf,
+                       onchain_events_awaiting_threshold_conf,
                        latest_height,
                        secp_ctx,
                })
@@ -453,43 +410,13 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
                        channel_transaction_parameters: channel_parameters,
                        pending_claim_requests: HashMap::new(),
                        claimable_outpoints: HashMap::new(),
-                       onchain_events_waiting_threshold_conf: Vec::new(),
+                       onchain_events_awaiting_threshold_conf: Vec::new(),
                        latest_height: 0,
 
                        secp_ctx,
                }
        }
 
-       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
@@ -581,11 +508,11 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
                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 { .. } => {
@@ -628,7 +555,11 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
                                                                chan_utils::get_revokeable_redeemscript(&tx_keys.revocation_key, *on_counterparty_tx_csv, &tx_keys.broadcaster_delayed_payment_key)
                                                        };
 
-                                                       let sig = self.signer.sign_justice_transaction(&bumped_tx, i, *amount, &per_commitment_key, htlc, &self.secp_ctx).expect("sign justice tx");
+                                                       let sig = if let Some(ref htlc) = *htlc {
+                                                               self.signer.sign_justice_revoked_htlc(&bumped_tx, i, *amount, &per_commitment_key, &htlc, &self.secp_ctx).expect("sign justice tx")
+                                                       } else {
+                                                               self.signer.sign_justice_revoked_output(&bumped_tx, i, *amount, &per_commitment_key, &self.secp_ctx).expect("sign justice tx")
+                                                       };
                                                        bumped_tx.input[i].witness.push(sig.serialize_der().to_vec());
                                                        bumped_tx.input[i].witness[0].push(SigHashType::All as u8);
                                                        if htlc.is_some() {
@@ -707,7 +638,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
                log_trace!(logger, "Updating claims view at height {} with {} matched transactions and {} claim requests", height, txn_matched.len(), claimable_outpoints.len());
                let mut new_claims = Vec::new();
                let mut aggregated_claim = HashMap::new();
-               let mut aggregated_soonest = ::std::u32::MAX;
+               let mut aggregated_soonest = ::core::u32::MAX;
 
                // Try to aggregate outputs if their timelock expiration isn't imminent (absolute_timelock
                // <= CLTV_SHARED_CLAIM_BUFFER) and they don't require an immediate nLockTime (aggregable).
@@ -742,7 +673,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
                                        self.claimable_outpoints.insert(k.clone(), (txid, height));
                                }
                                self.pending_claim_requests.insert(txid, claim_material);
-                               log_trace!(logger, "Broadcast onchain {}", log_tx!(tx));
+                               log_info!(logger, "Broadcasting onchain {}", log_tx!(tx));
                                broadcaster.broadcast_transaction(&tx);
                        }
                }
@@ -776,8 +707,8 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
                                                                        height,
                                                                        event: OnchainEvent::Claim { claim_request: first_claim_txid_height.0.clone() }
                                                                };
-                                                               if !self.onchain_events_waiting_threshold_conf.contains(&entry) {
-                                                                       self.onchain_events_waiting_threshold_conf.push(entry);
+                                                               if !self.onchain_events_awaiting_threshold_conf.contains(&entry) {
+                                                                       self.onchain_events_awaiting_threshold_conf.push(entry);
                                                                }
                                                        }
                                                }
@@ -816,16 +747,16 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
                                        height,
                                        event: OnchainEvent::ContentiousOutpoint { outpoint, input_material },
                                };
-                               if !self.onchain_events_waiting_threshold_conf.contains(&entry) {
-                                       self.onchain_events_waiting_threshold_conf.push(entry);
+                               if !self.onchain_events_awaiting_threshold_conf.contains(&entry) {
+                                       self.onchain_events_awaiting_threshold_conf.push(entry);
                                }
                        }
                }
 
                // After security delay, either our claim tx got enough confs or outpoint is definetely out of reach
-               let onchain_events_waiting_threshold_conf =
-                       self.onchain_events_waiting_threshold_conf.drain(..).collect::<Vec<_>>();
-               for entry in onchain_events_waiting_threshold_conf {
+               let onchain_events_awaiting_threshold_conf =
+                       self.onchain_events_awaiting_threshold_conf.drain(..).collect::<Vec<_>>();
+               for entry in onchain_events_awaiting_threshold_conf {
                        if entry.has_reached_confirmation_threshold(height) {
                                match entry.event {
                                        OnchainEvent::Claim { claim_request } => {
@@ -842,7 +773,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
                                        }
                                }
                        } else {
-                               self.onchain_events_waiting_threshold_conf.push(entry);
+                               self.onchain_events_awaiting_threshold_conf.push(entry);
                        }
                }
 
@@ -859,7 +790,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
                log_trace!(logger, "Bumping {} candidates", bump_candidates.len());
                for (first_claim_txid, claim_material) in bump_candidates.iter() {
                        if let Some((new_timer, new_feerate, bump_tx)) = self.generate_claim_tx(height, &claim_material, &*fee_estimator, &*logger) {
-                               log_trace!(logger, "Broadcast onchain {}", log_tx!(bump_tx));
+                               log_info!(logger, "Broadcasting onchain {}", log_tx!(bump_tx));
                                broadcaster.broadcast_transaction(&bump_tx);
                                if let Some(claim_material) = self.pending_claim_requests.get_mut(first_claim_txid) {
                                        claim_material.height_timer = new_timer;
@@ -881,7 +812,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
                L::Target: Logger,
        {
                let mut height = None;
-               for entry in self.onchain_events_waiting_threshold_conf.iter() {
+               for entry in self.onchain_events_awaiting_threshold_conf.iter() {
                        if entry.txid == *txid {
                                height = Some(entry.height);
                                break;
@@ -899,9 +830,9 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
                                        L::Target: Logger,
        {
                let mut bump_candidates = HashMap::new();
-               let onchain_events_waiting_threshold_conf =
-                       self.onchain_events_waiting_threshold_conf.drain(..).collect::<Vec<_>>();
-               for entry in onchain_events_waiting_threshold_conf {
+               let onchain_events_awaiting_threshold_conf =
+                       self.onchain_events_awaiting_threshold_conf.drain(..).collect::<Vec<_>>();
+               for entry in onchain_events_awaiting_threshold_conf {
                        if entry.height >= height {
                                //- our claim tx on a commitment tx output
                                //- resurect outpoint back in its claimable set and regenerate tx
@@ -919,13 +850,14 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
                                        _ => {},
                                }
                        } else {
-                               self.onchain_events_waiting_threshold_conf.push(entry);
+                               self.onchain_events_awaiting_threshold_conf.push(entry);
                        }
                }
                for (_, claim_material) in bump_candidates.iter_mut() {
                        if let Some((new_timer, new_feerate, bump_tx)) = self.generate_claim_tx(height, &claim_material, &&*fee_estimator, &&*logger) {
                                claim_material.height_timer = new_timer;
                                claim_material.feerate_previous = new_feerate;
+                               log_info!(logger, "Broadcasting onchain {}", log_tx!(bump_tx));
                                broadcaster.broadcast_transaction(&bump_tx);
                        }
                }
@@ -945,6 +877,16 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
                }
        }
 
+       pub(crate) fn get_relevant_txids(&self) -> Vec<Txid> {
+               let mut txids: Vec<Txid> = self.onchain_events_awaiting_threshold_conf
+                       .iter()
+                       .map(|entry| entry.txid)
+                       .collect();
+               txids.sort_unstable();
+               txids.dedup();
+               txids
+       }
+
        pub(crate) fn provide_latest_holder_tx(&mut self, tx: HolderCommitmentTransaction) {
                self.prev_holder_commitment = Some(replace(&mut self.holder_commitment, tx));
                self.holder_htlc_sigs = None;