X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Fchannelmonitor.rs;h=f32d2fbdb9d2b38cf225adfb313e9b4eb1a3ab5b;hb=b3dd229711bc095645ebbc6b291005e7f8c43895;hp=72e8e210a31f23879c40ecfe9e132ada2df433c7;hpb=0011713d2cee8060883fb2039ff5cbd32b5c9677;p=rust-lightning diff --git a/lightning/src/ln/channelmonitor.rs b/lightning/src/ln/channelmonitor.rs index 72e8e210..f32d2fbd 100644 --- a/lightning/src/ln/channelmonitor.rs +++ b/lightning/src/ln/channelmonitor.rs @@ -16,7 +16,7 @@ use bitcoin::blockdata::transaction::{TxIn,TxOut,SigHashType,Transaction}; use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint; use bitcoin::blockdata::script::{Script, Builder}; use bitcoin::blockdata::opcodes; -use bitcoin::consensus::encode::{self, Decodable, Encodable}; +use bitcoin::consensus::encode; use bitcoin::util::hash::BitcoinHash; use bitcoin::util::bip143; @@ -31,17 +31,16 @@ use secp256k1; use ln::msgs::DecodeError; use ln::chan_utils; -use ln::chan_utils::HTLCOutputInCommitment; +use ln::chan_utils::{HTLCOutputInCommitment, LocalCommitmentTransaction, HTLCType}; use ln::channelmanager::{HTLCSource, PaymentPreimage, PaymentHash}; -use ln::channel::{ACCEPTED_HTLC_SCRIPT_WEIGHT, OFFERED_HTLC_SCRIPT_WEIGHT}; -use chain::chaininterface::{ChainListener, ChainWatchInterface, BroadcasterInterface, FeeEstimator, ConfirmationTarget}; +use chain::chaininterface::{ChainListener, ChainWatchInterface, BroadcasterInterface, FeeEstimator, ConfirmationTarget, MIN_RELAY_FEE_SAT_PER_1000_WEIGHT}; use chain::transaction::OutPoint; use chain::keysinterface::SpendableOutputDescriptor; use util::logger::Logger; -use util::ser::{ReadableArgs, Readable, Writer, Writeable, WriterWriteAdaptor, U48}; +use util::ser::{ReadableArgs, Readable, Writer, Writeable, U48}; use util::{byte_utils, events}; -use std::collections::{HashMap, hash_map}; +use std::collections::{HashMap, hash_map, HashSet}; use std::sync::{Arc,Mutex}; use std::{hash,cmp, mem}; @@ -153,7 +152,6 @@ pub struct SimpleManyChannelMonitor { } impl<'a, Key : Send + cmp::Eq + hash::Hash> ChainListener for SimpleManyChannelMonitor { - fn block_connected(&self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], _indexes_of_txn_matched: &[u32]) { let block_hash = header.bitcoin_hash(); let mut new_events: Vec = Vec::with_capacity(0); @@ -212,7 +210,7 @@ impl<'a, Key : Send + cmp::Eq + hash::Hash> ChainListener for SimpleManyChannelM let block_hash = header.bitcoin_hash(); let mut monitors = self.monitors.lock().unwrap(); for monitor in monitors.values_mut() { - monitor.block_disconnected(disconnected_height, &block_hash); + monitor.block_disconnected(disconnected_height, &block_hash, &*self.broadcaster, &*self.fee_estimator); } } } @@ -220,8 +218,8 @@ impl<'a, Key : Send + cmp::Eq + hash::Hash> ChainListener for SimpleManyChannelM impl SimpleManyChannelMonitor { /// Creates a new object which can be used to monitor several channels given the chain /// interface with which to register to receive notifications. - pub fn new(chain_monitor: Arc, broadcaster: Arc, logger: Arc, feeest: Arc) -> Arc> { - let res = Arc::new(SimpleManyChannelMonitor { + pub fn new(chain_monitor: Arc, broadcaster: Arc, logger: Arc, feeest: Arc) -> SimpleManyChannelMonitor { + let res = SimpleManyChannelMonitor { monitors: Mutex::new(HashMap::new()), chain_monitor, broadcaster, @@ -229,7 +227,7 @@ impl SimpleManyChannelMonitor pending_htlc_updated: Mutex::new(HashMap::new()), logger, fee_estimator: feeest, - }); + }; res } @@ -331,13 +329,12 @@ pub(crate) const ANTI_REORG_DELAY: u32 = 6; #[derive(Clone, PartialEq)] enum Storage { Local { + funding_key: SecretKey, revocation_base_key: SecretKey, htlc_base_key: SecretKey, delayed_payment_base_key: SecretKey, payment_base_key: SecretKey, shutdown_pubkey: PublicKey, - prev_latest_per_commitment_point: Option, - latest_per_commitment_point: Option, funding_info: Option<(OutPoint, Script)>, current_remote_commitment_txid: Option, prev_remote_commitment_txid: Option, @@ -352,13 +349,14 @@ enum Storage { struct LocalSignedTx { /// txid of the transaction in tx, just used to make comparison faster txid: Sha256dHash, - tx: Transaction, + tx: LocalCommitmentTransaction, revocation_key: PublicKey, a_htlc_key: PublicKey, b_htlc_key: PublicKey, delayed_payment_key: PublicKey, + per_commitment_point: PublicKey, feerate_per_kw: u64, - htlc_outputs: Vec<(HTLCOutputInCommitment, Option<(Signature, Signature)>, Option)>, + htlc_outputs: Vec<(HTLCOutputInCommitment, Option, Option)>, } #[derive(PartialEq)] @@ -387,6 +385,7 @@ enum InputMaterial { key: SecretKey, preimage: Option, amount: u64, + locktime: u32, }, LocalHTLC { script: Script, @@ -411,12 +410,13 @@ impl Writeable for InputMaterial { } writer.write_all(&byte_utils::be64_to_array(*amount))?; }, - &InputMaterial::RemoteHTLC { ref script, ref key, ref preimage, ref amount } => { + &InputMaterial::RemoteHTLC { ref script, ref key, ref preimage, ref amount, ref locktime } => { writer.write_all(&[1; 1])?; script.write(writer)?; key.write(writer)?; preimage.write(writer)?; writer.write_all(&byte_utils::be64_to_array(*amount))?; + writer.write_all(&byte_utils::be32_to_array(*locktime))?; }, &InputMaterial::LocalHTLC { ref script, ref sigs, ref preimage, ref amount } => { writer.write_all(&[2; 1])?; @@ -457,11 +457,13 @@ impl Readable for InputMaterial { let key = Readable::read(reader)?; let preimage = Readable::read(reader)?; let amount = Readable::read(reader)?; + let locktime = Readable::read(reader)?; InputMaterial::RemoteHTLC { script, key, preimage, - amount + amount, + locktime } }, 2 => { @@ -498,11 +500,18 @@ enum OnchainEvent { HTLCUpdate { htlc_update: (HTLCSource, PaymentHash), }, + /// Claim tx aggregate multiple claimable outpoints. One of the outpoint may be claimed by a remote party tx. + /// In this case, we need to drop the outpoint and regenerate a new claim tx. By safety, we keep tracking + /// the outpoint to be sure to resurect it back to the claim tx if reorgs happen. + ContentiousOutpoint { + outpoint: BitcoinOutPoint, + input_material: InputMaterial, + } } /// Higher-level cache structure needed to re-generate bumped claim txn if needed #[derive(Clone, PartialEq)] -struct ClaimTxBumpMaterial { +pub struct ClaimTxBumpMaterial { // At every block tick, used to check if pending claiming tx is taking too // much time for confirmation and we need to bump it. height_timer: u32, @@ -560,6 +569,8 @@ pub struct ChannelMonitor { key_storage: Storage, their_htlc_base_key: Option, their_delayed_payment_base_key: Option, + funding_redeemscript: Option