X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Fchannelmonitor.rs;h=20e8a2fdabe4cbad97cd816fd2b0d35e7e8526a9;hb=d768cc234e1b0e6ee14d87f6cf72a1e20bba531a;hp=a31c5bc504d02d49e6c155adf885e4d2fa737c1b;hpb=06091cee0fd29549e5e24c673bf361ab3a562529;p=rust-lightning diff --git a/lightning/src/ln/channelmonitor.rs b/lightning/src/ln/channelmonitor.rs index a31c5bc5..20e8a2fd 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,19 +31,19 @@ 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 chain::keysinterface::{SpendableOutputDescriptor, ChannelKeys}; 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}; +use std::ops::Deref; /// An error enum representing a failure to persist a channel monitor update. #[derive(Clone)] @@ -94,11 +94,13 @@ pub struct MonitorUpdateError(pub &'static str); /// Simple structure send back by ManyChannelMonitor in case of HTLC detected onchain from a /// forward channel and from which info are needed to update HTLC in a backward channel. +#[derive(Clone, PartialEq)] pub struct HTLCUpdate { pub(super) payment_hash: PaymentHash, pub(super) payment_preimage: Option, pub(super) source: HTLCSource } +impl_writeable!(HTLCUpdate, 0, { payment_hash, payment_preimage, source }); /// Simple trait indicating ability to track a set of ChannelMonitors and multiplex events between /// them. Generally should be implemented by keeping a local SimpleManyChannelMonitor and passing @@ -109,17 +111,34 @@ pub struct HTLCUpdate { /// channel's monitor everywhere (including remote watchtowers) *before* this function returns. If /// an update occurs and a remote watchtower is left with old state, it may broadcast transactions /// which we have revoked, allowing our counterparty to claim all funds in the channel! -pub trait ManyChannelMonitor: Send + Sync { +/// +/// User needs to notify implementors of ManyChannelMonitor when a new block is connected or +/// disconnected using their `block_connected` and `block_disconnected` methods. However, rather +/// than calling these methods directly, the user should register implementors as listeners to the +/// BlockNotifier and call the BlockNotifier's `block_(dis)connected` methods, which will notify +/// all registered listeners in one go. +pub trait ManyChannelMonitor: Send + Sync { /// Adds or updates a monitor for the given `funding_txo`. /// - /// Implementor must also ensure that the funding_txo outpoint is registered with any relevant - /// ChainWatchInterfaces such that the provided monitor receives block_connected callbacks with - /// any spends of it. - fn add_update_monitor(&self, funding_txo: OutPoint, monitor: ChannelMonitor) -> Result<(), ChannelMonitorUpdateErr>; + /// Implementer must also ensure that the funding_txo txid *and* outpoint are registered with + /// any relevant ChainWatchInterfaces such that the provided monitor receives block_connected + /// callbacks with the funding transaction, or any spends of it. + /// + /// Further, the implementer must also ensure that each output returned in + /// monitor.get_outputs_to_watch() is registered to ensure that the provided monitor learns about + /// any spends of any of the outputs. + /// + /// Any spends of outputs which should have been registered which aren't passed to + /// ChannelMonitors via block_connected may result in funds loss. + fn add_update_monitor(&self, funding_txo: OutPoint, monitor: ChannelMonitor) -> Result<(), ChannelMonitorUpdateErr>; /// Used by ChannelManager to get list of HTLC resolved onchain and which needed to be updated - /// with success or failure backward - fn fetch_pending_htlc_updated(&self) -> Vec; + /// with success or failure. + /// + /// You should probably just call through to + /// ChannelMonitor::get_and_clear_pending_htlcs_updated() for each ChannelMonitor and return + /// the full list. + fn get_and_clear_pending_htlcs_updated(&self) -> Vec; } /// A simple implementation of a ManyChannelMonitor and ChainListener. Can be used to create a @@ -133,28 +152,28 @@ pub trait ManyChannelMonitor: Send + Sync { /// /// If you're using this for local monitoring of your own channels, you probably want to use /// `OutPoint` as the key, which will give you a ManyChannelMonitor implementation. -pub struct SimpleManyChannelMonitor { +pub struct SimpleManyChannelMonitor where T::Target: BroadcasterInterface { #[cfg(test)] // Used in ChannelManager tests to manipulate channels directly - pub monitors: Mutex>, + pub monitors: Mutex>>, #[cfg(not(test))] - monitors: Mutex>, + monitors: Mutex>>, chain_monitor: Arc, - broadcaster: Arc, + broadcaster: T, pending_events: Mutex>, - pending_htlc_updated: Mutex)>>>, logger: Arc, fee_estimator: Arc } -impl ChainListener for SimpleManyChannelMonitor { +impl<'a, Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys, T: Deref + Sync + Send> ChainListener for SimpleManyChannelMonitor + where T::Target: BroadcasterInterface +{ 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); - let mut htlc_updated_infos = Vec::new(); { let mut monitors = self.monitors.lock().unwrap(); for monitor in monitors.values_mut() { - let (txn_outputs, spendable_outputs, mut htlc_updated) = monitor.block_connected(txn_matched, height, &block_hash, &*self.broadcaster, &*self.fee_estimator); + let (txn_outputs, spendable_outputs) = monitor.block_connected(txn_matched, height, &block_hash, &*self.broadcaster, &*self.fee_estimator); if spendable_outputs.len() > 0 { new_events.push(events::Event::SpendableOutputs { outputs: spendable_outputs, @@ -166,35 +185,6 @@ impl ChainListener for SimpleManyChannelMonit self.chain_monitor.install_watch_outpoint((txid.clone(), idx as u32), &output.script_pubkey); } } - htlc_updated_infos.append(&mut htlc_updated); - } - } - { - // ChannelManager will just need to fetch pending_htlc_updated and pass state backward - let mut pending_htlc_updated = self.pending_htlc_updated.lock().unwrap(); - for htlc in htlc_updated_infos.drain(..) { - match pending_htlc_updated.entry(htlc.2) { - hash_map::Entry::Occupied(mut e) => { - // In case of reorg we may have htlc outputs solved in a different way so - // we prefer to keep claims but don't store duplicate updates for a given - // (payment_hash, HTLCSource) pair. - let mut existing_claim = false; - e.get_mut().retain(|htlc_data| { - if htlc.0 == htlc_data.0 { - if htlc_data.1.is_some() { - existing_claim = true; - true - } else { false } - } else { true } - }); - if !existing_claim { - e.get_mut().push((htlc.0, htlc.1)); - } - } - hash_map::Entry::Vacant(e) => { - e.insert(vec![(htlc.0, htlc.1)]); - } - } } } let mut pending_events = self.pending_events.lock().unwrap(); @@ -205,31 +195,31 @@ impl ChainListener for SimpleManyChannelMonit 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); } } } -impl SimpleManyChannelMonitor { +impl SimpleManyChannelMonitor + where T::Target: BroadcasterInterface +{ /// 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: T, logger: Arc, feeest: Arc) -> SimpleManyChannelMonitor { + let res = SimpleManyChannelMonitor { monitors: Mutex::new(HashMap::new()), chain_monitor, broadcaster, pending_events: Mutex::new(Vec::new()), - pending_htlc_updated: Mutex::new(HashMap::new()), logger, fee_estimator: feeest, - }); - let weak_res = Arc::downgrade(&res); - res.chain_monitor.register_listener(weak_res); + }; + res } /// Adds or updates the monitor which monitors the channel referred to by the given key. - pub fn add_update_monitor_by_key(&self, key: Key, monitor: ChannelMonitor) -> Result<(), MonitorUpdateError> { + pub fn add_update_monitor_by_key(&self, key: Key, monitor: ChannelMonitor) -> Result<(), MonitorUpdateError> { let mut monitors = self.monitors.lock().unwrap(); match monitors.get_mut(&key) { Some(orig_monitor) => { @@ -255,36 +245,38 @@ impl SimpleManyChannelMonitor self.chain_monitor.watch_all_txn(); } } + for (txid, outputs) in monitor.get_outputs_to_watch().iter() { + for (idx, script) in outputs.iter().enumerate() { + self.chain_monitor.install_watch_outpoint((*txid, idx as u32), script); + } + } monitors.insert(key, monitor); Ok(()) } } -impl ManyChannelMonitor for SimpleManyChannelMonitor { - fn add_update_monitor(&self, funding_txo: OutPoint, monitor: ChannelMonitor) -> Result<(), ChannelMonitorUpdateErr> { +impl ManyChannelMonitor for SimpleManyChannelMonitor + where T::Target: BroadcasterInterface +{ + fn add_update_monitor(&self, funding_txo: OutPoint, monitor: ChannelMonitor) -> Result<(), ChannelMonitorUpdateErr> { match self.add_update_monitor_by_key(funding_txo, monitor) { Ok(_) => Ok(()), Err(_) => Err(ChannelMonitorUpdateErr::PermanentFailure), } } - fn fetch_pending_htlc_updated(&self) -> Vec { - let mut updated = self.pending_htlc_updated.lock().unwrap(); - let mut pending_htlcs_updated = Vec::with_capacity(updated.len()); - for (k, v) in updated.drain() { - for htlc_data in v { - pending_htlcs_updated.push(HTLCUpdate { - payment_hash: k, - payment_preimage: htlc_data.1, - source: htlc_data.0, - }); - } + fn get_and_clear_pending_htlcs_updated(&self) -> Vec { + let mut pending_htlcs_updated = Vec::new(); + for chan in self.monitors.lock().unwrap().values_mut() { + pending_htlcs_updated.append(&mut chan.get_and_clear_pending_htlcs_updated()); } pending_htlcs_updated } } -impl events::EventsProvider for SimpleManyChannelMonitor { +impl events::EventsProvider for SimpleManyChannelMonitor + where T::Target: BroadcasterInterface +{ fn get_and_clear_pending_events(&self) -> Vec { let mut pending_events = self.pending_events.lock().unwrap(); let mut ret = Vec::new(); @@ -322,16 +314,16 @@ pub(crate) const LATENCY_GRACE_PERIOD_BLOCKS: u32 = 3; /// keeping bumping another claim tx to solve the outpoint. pub(crate) const ANTI_REORG_DELAY: u32 = 6; -#[derive(Clone, PartialEq)] -enum Storage { +#[derive(Clone)] +enum Storage { Local { + keys: ChanSigner, + 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, @@ -342,17 +334,41 @@ enum Storage { } } +#[cfg(any(test, feature = "fuzztarget"))] +impl PartialEq for Storage { + fn eq(&self, other: &Self) -> bool { + match *self { + Storage::Local { ref keys, .. } => { + let k = keys; + match *other { + Storage::Local { ref keys, .. } => keys.pubkeys() == k.pubkeys(), + Storage::Watchtower { .. } => false, + } + }, + Storage::Watchtower {ref revocation_base_key, ref htlc_base_key} => { + let (rbk, hbk) = (revocation_base_key, htlc_base_key); + match *other { + Storage::Local { .. } => false, + Storage::Watchtower {ref revocation_base_key, ref htlc_base_key} => + revocation_base_key == rbk && htlc_base_key == hbk, + } + }, + } + } +} + #[derive(Clone, PartialEq)] 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)] @@ -368,7 +384,7 @@ enum InputDescriptors { /// to generate a tx to push channel state forward, we cache outpoint-solving tx material to build /// a new bumped one in case of lenghty confirmation delay #[derive(Clone, PartialEq)] -enum TxMaterial { +enum InputMaterial { Revoked { script: Script, pubkey: Option, @@ -381,6 +397,7 @@ enum TxMaterial { key: SecretKey, preimage: Option, amount: u64, + locktime: u32, }, LocalHTLC { script: Script, @@ -390,6 +407,96 @@ enum TxMaterial { } } +impl Writeable for InputMaterial { + fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { + match self { + &InputMaterial::Revoked { ref script, ref pubkey, ref key, ref is_htlc, ref amount} => { + writer.write_all(&[0; 1])?; + script.write(writer)?; + pubkey.write(writer)?; + writer.write_all(&key[..])?; + if *is_htlc { + writer.write_all(&[0; 1])?; + } else { + writer.write_all(&[1; 1])?; + } + writer.write_all(&byte_utils::be64_to_array(*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])?; + script.write(writer)?; + sigs.0.write(writer)?; + sigs.1.write(writer)?; + preimage.write(writer)?; + writer.write_all(&byte_utils::be64_to_array(*amount))?; + } + } + Ok(()) + } +} + +impl Readable for InputMaterial { + fn read(reader: &mut R) -> Result { + let input_material = match >::read(reader)? { + 0 => { + let script = Readable::read(reader)?; + let pubkey = Readable::read(reader)?; + let key = Readable::read(reader)?; + let is_htlc = match >::read(reader)? { + 0 => true, + 1 => false, + _ => return Err(DecodeError::InvalidValue), + }; + let amount = Readable::read(reader)?; + InputMaterial::Revoked { + script, + pubkey, + key, + is_htlc, + amount + } + }, + 1 => { + let script = Readable::read(reader)?; + 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, + locktime + } + }, + 2 => { + let script = Readable::read(reader)?; + let their_sig = Readable::read(reader)?; + let our_sig = Readable::read(reader)?; + let preimage = Readable::read(reader)?; + let amount = Readable::read(reader)?; + InputMaterial::LocalHTLC { + script, + sigs: (their_sig, our_sig), + preimage, + amount + } + } + _ => return Err(DecodeError::InvalidValue), + }; + Ok(input_material) + } +} + /// Upon discovering of some classes of onchain tx by ChannelMonitor, we may have to take actions on it /// once they mature to enough confirmations (ANTI_REORG_DELAY) #[derive(Clone, PartialEq)] @@ -397,7 +504,7 @@ enum OnchainEvent { /// Outpoint under claim process by our own tx, once this one get enough confirmations, we remove it from /// bump-txn candidate buffer. Claim { - outpoint: BitcoinOutPoint, + claim_request: Sha256dHash, }, /// HTLC output getting solved by a timeout, at maturation we pass upstream payment source information to solve /// inbound HTLC in backward channel. Note, in case of preimage, we pass info to upstream without delay as we can @@ -405,6 +512,58 @@ 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)] +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, + // Tracked in case of reorg to wipe out now-superflous bump material + feerate_previous: u64, + // Soonest timelocks among set of outpoints claimed, used to compute + // a priority of not feerate + soonest_timelock: u32, + // Cache of script, pubkey, sig or key to solve claimable outputs scriptpubkey. + per_input_material: HashMap, +} + +impl Writeable for ClaimTxBumpMaterial { + fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { + writer.write_all(&byte_utils::be32_to_array(self.height_timer))?; + writer.write_all(&byte_utils::be64_to_array(self.feerate_previous))?; + writer.write_all(&byte_utils::be32_to_array(self.soonest_timelock))?; + writer.write_all(&byte_utils::be64_to_array(self.per_input_material.len() as u64))?; + for (outp, tx_material) in self.per_input_material.iter() { + outp.write(writer)?; + tx_material.write(writer)?; + } + Ok(()) + } +} + +impl Readable for ClaimTxBumpMaterial { + fn read(reader: &mut R) -> Result { + let height_timer = Readable::read(reader)?; + let feerate_previous = Readable::read(reader)?; + let soonest_timelock = Readable::read(reader)?; + let per_input_material_len: u64 = Readable::read(reader)?; + let mut per_input_material = HashMap::with_capacity(cmp::min(per_input_material_len as usize, MAX_ALLOC_SIZE / 128)); + for _ in 0 ..per_input_material_len { + let outpoint = Readable::read(reader)?; + let input_material = Readable::read(reader)?; + per_input_material.insert(outpoint, input_material); + } + Ok(Self { height_timer, feerate_previous, soonest_timelock, per_input_material }) + } } const SERIALIZATION_VERSION: u8 = 1; @@ -416,12 +575,14 @@ const MIN_SERIALIZATION_VERSION: u8 = 1; /// You MUST ensure that no ChannelMonitors for a given channel anywhere contain out-of-date /// information and are actively monitoring the chain. #[derive(Clone)] -pub struct ChannelMonitor { +pub struct ChannelMonitor { commitment_transaction_number_obscure_factor: u64, - key_storage: Storage, + key_storage: Storage, their_htlc_base_key: Option, their_delayed_payment_base_key: Option, + funding_redeemscript: Option