X-Git-Url: http://git.bitcoin.ninja/index.cgi?p=rust-lightning;a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Fchannelmonitor.rs;h=da8ba03a7271a7d359f7e58dcf51851c617ee790;hp=373f811e9ae4ea8ec77f104edff12fef1c0bca08;hb=359b3d5702b585a67e4e6d4044c35caf86bd787d;hpb=f930fc1886dbfdc388be79579ef02e788e2478f5 diff --git a/lightning/src/ln/channelmonitor.rs b/lightning/src/ln/channelmonitor.rs index 373f811e..da8ba03a 100644 --- a/lightning/src/ln/channelmonitor.rs +++ b/lightning/src/ln/channelmonitor.rs @@ -12,13 +12,12 @@ //! ChannelMonitors to get out of the HSM and onto monitoring devices. use bitcoin::blockdata::block::BlockHeader; -use bitcoin::blockdata::transaction::{TxIn,TxOut,SigHashType,Transaction}; +use bitcoin::blockdata::transaction::{TxOut,Transaction}; use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint; use bitcoin::blockdata::script::{Script, Builder}; use bitcoin::blockdata::opcodes; use bitcoin::consensus::encode; use bitcoin::util::hash::BitcoinHash; -use bitcoin::util::bip143; use bitcoin_hashes::Hash; use bitcoin_hashes::sha256::Hash as Sha256; @@ -33,14 +32,15 @@ use ln::msgs::DecodeError; use ln::chan_utils; use ln::chan_utils::{CounterpartyCommitmentSecrets, HTLCOutputInCommitment, LocalCommitmentTransaction, HTLCType}; use ln::channelmanager::{HTLCSource, PaymentPreimage, PaymentHash}; -use chain::chaininterface::{ChainListener, ChainWatchInterface, BroadcasterInterface, FeeEstimator, ConfirmationTarget, MIN_RELAY_FEE_SAT_PER_1000_WEIGHT}; +use ln::onchaintx::OnchainTxHandler; +use chain::chaininterface::{ChainListener, ChainWatchInterface, BroadcasterInterface, FeeEstimator}; use chain::transaction::OutPoint; use chain::keysinterface::{SpendableOutputDescriptor, ChannelKeys}; use util::logger::Logger; -use util::ser::{ReadableArgs, Readable, Writer, Writeable, U48}; +use util::ser::{ReadableArgs, Readable, MaybeReadable, Writer, Writeable, U48}; use util::{byte_utils, events}; -use std::collections::{HashMap, hash_map, HashSet}; +use std::collections::{HashMap, hash_map}; use std::sync::{Arc,Mutex}; use std::{hash,cmp, mem}; use std::ops::Deref; @@ -72,8 +72,8 @@ impl Writeable for ChannelMonitorUpdate { Ok(()) } } -impl Readable for ChannelMonitorUpdate { - fn read(r: &mut R) -> Result { +impl Readable for ChannelMonitorUpdate { + fn read(r: &mut R) -> Result { let update_id: u64 = Readable::read(r)?; let len: u64 = Readable::read(r)?; let mut updates = Vec::with_capacity(cmp::min(len as usize, MAX_ALLOC_SIZE / ::std::mem::size_of::())); @@ -124,16 +124,18 @@ pub enum ChannelMonitorUpdateErr { TemporaryFailure, /// Used to indicate no further channel monitor updates will be allowed (eg we've moved on to a /// different watchtower and cannot update with all watchtowers that were previously informed - /// of this channel). This will force-close the channel in question. + /// of this channel). This will force-close the channel in question (which will generate one + /// final ChannelMonitorUpdate which must be delivered to at least one ChannelMonitor copy). /// - /// Should also be used to indicate a failure to update the local copy of the channel monitor. + /// Should also be used to indicate a failure to update the local persisted copy of the channel + /// monitor. PermanentFailure, } /// General Err type for ChannelMonitor actions. Generally, this implies that the data provided is -/// inconsistent with the ChannelMonitor being called. eg for ChannelMonitor::insert_combine this -/// means you tried to merge two monitors for different channels or for a channel which was -/// restored from a backup and then generated new commitment updates. +/// inconsistent with the ChannelMonitor being called. eg for ChannelMonitor::update_monitor this +/// means you tried to update a monitor for a different channel or the ChannelMonitorUpdate was +/// corrupted. /// Contains a human-readable error message. #[derive(Debug)] pub struct MonitorUpdateError(pub &'static str); @@ -150,9 +152,16 @@ 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 -/// events to it, while also taking any add_update_monitor events and passing them to some remote +/// events to it, while also taking any add/update_monitor events and passing them to some remote /// server(s). /// +/// In general, you must always have at least one local copy in memory, which must never fail to +/// update (as it is responsible for broadcasting the latest state in case the channel is closed), +/// and then persist it to various on-disk locations. If, for some reason, the in-memory copy fails +/// to update (eg out-of-memory or some other condition), you must immediately shut down without +/// taking any further action such as writing the current state to disk. This should likely be +/// accomplished via panic!() or abort(). +/// /// Note that any updates to a channel's monitor *must* be applied to each instance of the /// 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 @@ -164,7 +173,7 @@ impl_writeable!(HTLCUpdate, 0, { payment_hash, payment_preimage, source }); /// 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`. + /// Adds a monitor for the given `funding_txo`. /// /// 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 @@ -176,7 +185,7 @@ pub trait ManyChannelMonitor: Send + Sync { /// /// 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>; + fn add_monitor(&self, funding_txo: OutPoint, monitor: ChannelMonitor) -> Result<(), ChannelMonitorUpdateErr>; /// Updates a monitor for the given `funding_txo`. /// @@ -212,33 +221,31 @@ 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 where T::Target: BroadcasterInterface { +pub struct SimpleManyChannelMonitor + where T::Target: BroadcasterInterface, + F::Target: FeeEstimator +{ #[cfg(test)] // Used in ChannelManager tests to manipulate channels directly pub monitors: Mutex>>, #[cfg(not(test))] monitors: Mutex>>, chain_monitor: Arc, broadcaster: T, - pending_events: Mutex>, logger: Arc, - fee_estimator: Arc + fee_estimator: F } -impl<'a, Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys, T: Deref + Sync + Send> ChainListener for SimpleManyChannelMonitor - where T::Target: BroadcasterInterface +impl<'a, Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys, T: Deref + Sync + Send, F: Deref + Sync + Send> + ChainListener for SimpleManyChannelMonitor + where T::Target: BroadcasterInterface, + F::Target: FeeEstimator { 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 monitors = self.monitors.lock().unwrap(); for monitor in monitors.values_mut() { - 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, - }); - } + let txn_outputs = monitor.block_connected(txn_matched, height, &block_hash, &*self.broadcaster, &*self.fee_estimator); for (ref txid, ref outputs) in txn_outputs { for (idx, output) in outputs.iter().enumerate() { @@ -247,8 +254,6 @@ impl<'a, Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys, T: Deref + } } } - let mut pending_events = self.pending_events.lock().unwrap(); - pending_events.append(&mut new_events); } fn block_disconnected(&self, header: &BlockHeader, disconnected_height: u32) { @@ -260,17 +265,17 @@ impl<'a, Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys, T: Deref + } } -impl SimpleManyChannelMonitor - where T::Target: BroadcasterInterface +impl SimpleManyChannelMonitor + where T::Target: BroadcasterInterface, + F::Target: FeeEstimator { /// 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: T, logger: Arc, feeest: Arc) -> SimpleManyChannelMonitor { + pub fn new(chain_monitor: Arc, broadcaster: T, logger: Arc, feeest: F) -> SimpleManyChannelMonitor { let res = SimpleManyChannelMonitor { monitors: Mutex::new(HashMap::new()), chain_monitor, broadcaster, - pending_events: Mutex::new(Vec::new()), logger, fee_estimator: feeest, }; @@ -279,38 +284,21 @@ impl) -> Result<(), MonitorUpdateError> { + pub fn add_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) => { - log_trace!(self, "Updating Channel Monitor for channel {}", log_funding_info!(monitor.key_storage)); - return orig_monitor.insert_combine(monitor); - }, - None => {} + let entry = match monitors.entry(key) { + hash_map::Entry::Occupied(_) => return Err(MonitorUpdateError("Channel monitor for given key is already present")), + hash_map::Entry::Vacant(e) => e, }; - match monitor.key_storage { - Storage::Local { ref funding_info, .. } => { - match funding_info { - &None => { - return Err(MonitorUpdateError("Try to update a useless monitor without funding_txo !")); - }, - &Some((ref outpoint, ref script)) => { - log_trace!(self, "Got new Channel Monitor for channel {}", log_bytes!(outpoint.to_channel_id()[..])); - self.chain_monitor.install_watch_tx(&outpoint.txid, script); - self.chain_monitor.install_watch_outpoint((outpoint.txid, outpoint.index as u32), script); - }, - } - }, - Storage::Watchtower { .. } => { - self.chain_monitor.watch_all_txn(); - } - } + log_trace!(self, "Got new Channel Monitor for channel {}", log_bytes!(monitor.funding_info.0.to_channel_id()[..])); + self.chain_monitor.install_watch_tx(&monitor.funding_info.0.txid, &monitor.funding_info.1); + self.chain_monitor.install_watch_outpoint((monitor.funding_info.0.txid, monitor.funding_info.0.index as u32), &monitor.funding_info.1); 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); + entry.insert(monitor); Ok(()) } @@ -319,19 +307,20 @@ impl { - log_trace!(self, "Updating Channel Monitor for channel {}", log_funding_info!(orig_monitor.key_storage)); - orig_monitor.update_monitor(update) + log_trace!(self, "Updating Channel Monitor for channel {}", log_funding_info!(orig_monitor)); + orig_monitor.update_monitor(update, &self.broadcaster) }, None => Err(MonitorUpdateError("No such monitor registered")) } } } -impl ManyChannelMonitor for SimpleManyChannelMonitor - where T::Target: BroadcasterInterface +impl ManyChannelMonitor for SimpleManyChannelMonitor + where T::Target: BroadcasterInterface, + F::Target: FeeEstimator { - fn add_update_monitor(&self, funding_txo: OutPoint, monitor: ChannelMonitor) -> Result<(), ChannelMonitorUpdateErr> { - match self.add_update_monitor_by_key(funding_txo, monitor) { + fn add_monitor(&self, funding_txo: OutPoint, monitor: ChannelMonitor) -> Result<(), ChannelMonitorUpdateErr> { + match self.add_monitor_by_key(funding_txo, monitor) { Ok(_) => Ok(()), Err(_) => Err(ChannelMonitorUpdateErr::PermanentFailure), } @@ -353,20 +342,22 @@ impl ManyChannelMonitor events::EventsProvider for SimpleManyChannelMonitor - where T::Target: BroadcasterInterface +impl events::EventsProvider for SimpleManyChannelMonitor + where T::Target: BroadcasterInterface, + F::Target: FeeEstimator { fn get_and_clear_pending_events(&self) -> Vec { - let mut pending_events = self.pending_events.lock().unwrap(); - let mut ret = Vec::new(); - mem::swap(&mut ret, &mut *pending_events); - ret + let mut pending_events = Vec::new(); + for chan in self.monitors.lock().unwrap().values_mut() { + pending_events.append(&mut chan.get_and_clear_pending_events()); + } + pending_events } } /// If an HTLC expires within this many blocks, don't try to claim it in a shared transaction, /// instead claiming it in its own individual transaction. -const CLTV_SHARED_CLAIM_BUFFER: u32 = 12; +pub(crate) const CLTV_SHARED_CLAIM_BUFFER: u32 = 12; /// If an HTLC expires within this many blocks, force-close the channel to broadcast the /// HTLC-Success transaction. /// In other words, this is an upper bound on how many blocks we think it can take us to get a @@ -392,55 +383,33 @@ pub(crate) const LATENCY_GRACE_PERIOD_BLOCKS: u32 = 3; /// solved by a previous claim tx. What we want to avoid is reorg evicting our claim tx and us not /// keeping bumping another claim tx to solve the outpoint. pub(crate) const ANTI_REORG_DELAY: u32 = 6; - -#[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, - funding_info: Option<(OutPoint, Script)>, - current_remote_commitment_txid: Option, - prev_remote_commitment_txid: Option, - }, - Watchtower { - revocation_base_key: PublicKey, - htlc_base_key: PublicKey, - } -} - -#[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, - } - }, - } - } -} +/// Number of blocks before confirmation at which we fail back an un-relayed HTLC or at which we +/// refuse to accept a new HTLC. +/// +/// This is used for a few separate purposes: +/// 1) if we've received an MPP HTLC to us and it expires within this many blocks and we are +/// waiting on additional parts (or waiting on the preimage for any HTLC from the user), we will +/// fail this HTLC, +/// 2) if we receive an HTLC within this many blocks of its expiry (plus one to avoid a race +/// condition with the above), we will fail this HTLC without telling the user we received it, +/// 3) if we are waiting on a connection or a channel state update to send an HTLC to a peer, and +/// that HTLC expires within this many blocks, we will simply fail the HTLC instead. +/// +/// (1) is all about protecting us - we need enough time to update the channel state before we hit +/// CLTV_CLAIM_BUFFER, at which point we'd go on chain to claim the HTLC with the preimage. +/// +/// (2) is the same, but with an additional buffer to avoid accepting an HTLC which is immediately +/// in a race condition between the user connecting a block (which would fail it) and the user +/// providing us the preimage (which would claim it). +/// +/// (3) is about our counterparty - we don't want to relay an HTLC to a counterparty when they may +/// end up force-closing the channel on us to claim it. +pub(crate) const HTLC_FAIL_BACK_BUFFER: u32 = CLTV_CLAIM_BUFFER + LATENCY_GRACE_PERIOD_BLOCKS; #[derive(Clone, PartialEq)] struct LocalSignedTx { /// txid of the transaction in tx, just used to make comparison faster txid: Sha256dHash, - tx: LocalCommitmentTransaction, revocation_key: PublicKey, a_htlc_key: PublicKey, b_htlc_key: PublicKey, @@ -450,93 +419,75 @@ struct LocalSignedTx { htlc_outputs: Vec<(HTLCOutputInCommitment, Option, Option)>, } -#[derive(PartialEq)] -enum InputDescriptors { - RevokedOfferedHTLC, - RevokedReceivedHTLC, - OfferedHTLC, - ReceivedHTLC, - RevokedOutput, // either a revoked to_local output on commitment tx, a revoked HTLC-Timeout output or a revoked HTLC-Success output -} - /// When ChannelMonitor discovers an onchain outpoint being a step of a channel and that it needs /// 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 InputMaterial { +pub(crate) enum InputMaterial { Revoked { - script: Script, + witness_script: Script, pubkey: Option, key: SecretKey, is_htlc: bool, amount: u64, }, RemoteHTLC { - script: Script, + witness_script: Script, key: SecretKey, preimage: Option, amount: u64, locktime: u32, }, LocalHTLC { - script: Script, - sigs: (Signature, Signature), preimage: Option, amount: u64, - } + }, + Funding {} } 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} => { + &InputMaterial::Revoked { ref witness_script, ref pubkey, ref key, ref is_htlc, ref amount} => { writer.write_all(&[0; 1])?; - script.write(writer)?; + witness_script.write(writer)?; pubkey.write(writer)?; writer.write_all(&key[..])?; - if *is_htlc { - writer.write_all(&[0; 1])?; - } else { - writer.write_all(&[1; 1])?; - } + is_htlc.write(writer)?; writer.write_all(&byte_utils::be64_to_array(*amount))?; }, - &InputMaterial::RemoteHTLC { ref script, ref key, ref preimage, ref amount, ref locktime } => { + &InputMaterial::RemoteHTLC { ref witness_script, ref key, ref preimage, ref amount, ref locktime } => { writer.write_all(&[1; 1])?; - script.write(writer)?; + witness_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 } => { + &InputMaterial::LocalHTLC { 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))?; + }, + &InputMaterial::Funding {} => { + writer.write_all(&[3; 1])?; } } Ok(()) } } -impl Readable for InputMaterial { - fn read(reader: &mut R) -> Result { - let input_material = match >::read(reader)? { +impl Readable for InputMaterial { + fn read(reader: &mut R) -> Result { + let input_material = match ::read(reader)? { 0 => { - let script = Readable::read(reader)?; + let witness_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 is_htlc = Readable::read(reader)?; let amount = Readable::read(reader)?; InputMaterial::Revoked { - script, + witness_script, pubkey, key, is_htlc, @@ -544,13 +495,13 @@ impl Readable for InputMaterial { } }, 1 => { - let script = Readable::read(reader)?; + let witness_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, + witness_script, key, preimage, amount, @@ -558,17 +509,15 @@ impl Readable for InputMaterial { } }, 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 + amount, } + }, + 3 => { + InputMaterial::Funding {} } _ => return Err(DecodeError::InvalidValue), }; @@ -576,73 +525,41 @@ impl Readable for InputMaterial { } } +/// ClaimRequest is a descriptor structure to communicate between detection +/// and reaction module. They are generated by ChannelMonitor while parsing +/// onchain txn leaked from a channel and handed over to OnchainTxHandler which +/// is responsible for opportunistic aggregation, selecting and enforcing +/// bumping logic, building and signing transactions. +pub(crate) struct ClaimRequest { + // Block height before which claiming is exclusive to one party, + // after reaching it, claiming may be contentious. + pub(crate) absolute_timelock: u32, + // Timeout tx must have nLocktime set which means aggregating multiple + // ones must take the higher nLocktime among them to satisfy all of them. + // Sadly it has few pitfalls, a) it takes longuer to get fund back b) CLTV_DELTA + // of a sooner-HTLC could be swallowed by the highest nLocktime of the HTLC set. + // Do simplify we mark them as non-aggregable. + pub(crate) aggregable: bool, + // Basic bitcoin outpoint (txid, vout) + pub(crate) outpoint: BitcoinOutPoint, + // Following outpoint type, set of data needed to generate transaction digest + // and satisfy witness program. + pub(crate) witness_data: InputMaterial +} + /// 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)] 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 { - 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 /// only win from it, so it's never an 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 }) - } + MaturingOutput { + descriptor: SpendableOutputDescriptor, + }, } const SERIALIZATION_VERSION: u8 = 1; @@ -652,12 +569,7 @@ const MIN_SERIALIZATION_VERSION: u8 = 1; #[derive(Clone)] pub(super) enum ChannelMonitorUpdateStep { LatestLocalCommitmentTXInfo { - // TODO: We really need to not be generating a fully-signed transaction in Channel and - // passing it here, we need to hold off so that the ChanSigner can enforce a - // only-sign-local-state-for-broadcast once invariant: commitment_tx: LocalCommitmentTransaction, - local_keys: chan_utils::TxCreationKeys, - feerate_per_kw: u64, htlc_outputs: Vec<(HTLCOutputInCommitment, Option, Option)>, }, LatestRemoteCommitmentTXInfo { @@ -678,16 +590,21 @@ pub(super) enum ChannelMonitorUpdateStep { RescueRemoteCommitmentTXInfo { their_current_per_commitment_point: PublicKey, }, + /// Used to indicate that the no future updates will occur, and likely that the latest local + /// commitment transaction(s) should be broadcast, as the channel has been force-closed. + ChannelForceClosed { + /// If set to false, we shouldn't broadcast the latest local commitment transaction as we + /// think we've fallen behind! + should_broadcast: bool, + }, } impl Writeable for ChannelMonitorUpdateStep { fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { match self { - &ChannelMonitorUpdateStep::LatestLocalCommitmentTXInfo { ref commitment_tx, ref local_keys, ref feerate_per_kw, ref htlc_outputs } => { + &ChannelMonitorUpdateStep::LatestLocalCommitmentTXInfo { ref commitment_tx, ref htlc_outputs } => { 0u8.write(w)?; commitment_tx.write(w)?; - local_keys.write(w)?; - feerate_per_kw.write(w)?; (htlc_outputs.len() as u64).write(w)?; for &(ref output, ref signature, ref source) in htlc_outputs.iter() { output.write(w)?; @@ -703,13 +620,7 @@ impl Writeable for ChannelMonitorUpdateStep { (htlc_outputs.len() as u64).write(w)?; for &(ref output, ref source) in htlc_outputs.iter() { output.write(w)?; - match source { - &None => 0u8.write(w)?, - &Some(ref s) => { - 1u8.write(w)?; - s.write(w)?; - }, - } + source.as_ref().map(|b| b.as_ref()).write(w)?; } }, &ChannelMonitorUpdateStep::PaymentPreimage { ref payment_preimage } => { @@ -725,18 +636,20 @@ impl Writeable for ChannelMonitorUpdateStep { 4u8.write(w)?; their_current_per_commitment_point.write(w)?; }, + &ChannelMonitorUpdateStep::ChannelForceClosed { ref should_broadcast } => { + 5u8.write(w)?; + should_broadcast.write(w)?; + }, } Ok(()) } } -impl Readable for ChannelMonitorUpdateStep { - fn read(r: &mut R) -> Result { +impl Readable for ChannelMonitorUpdateStep { + fn read(r: &mut R) -> Result { match Readable::read(r)? { 0u8 => { Ok(ChannelMonitorUpdateStep::LatestLocalCommitmentTXInfo { commitment_tx: Readable::read(r)?, - local_keys: Readable::read(r)?, - feerate_per_kw: Readable::read(r)?, htlc_outputs: { let len: u64 = Readable::read(r)?; let mut res = Vec::new(); @@ -756,7 +669,7 @@ impl Readable for ChannelMonitorUpdateStep { let len: u64 = Readable::read(r)?; let mut res = Vec::new(); for _ in 0..len { - res.push((Readable::read(r)?, as Readable>::read(r)?.map(|o| Box::new(o)))); + res.push((Readable::read(r)?, as Readable>::read(r)?.map(|o| Box::new(o)))); } res }, @@ -778,6 +691,11 @@ impl Readable for ChannelMonitorUpdateStep { their_current_per_commitment_point: Readable::read(r)?, }) }, + 5u8 => { + Ok(ChannelMonitorUpdateStep::ChannelForceClosed { + should_broadcast: Readable::read(r)? + }) + }, _ => Err(DecodeError::InvalidValue), } } @@ -788,21 +706,34 @@ impl Readable for ChannelMonitorUpdateStep { /// /// You MUST ensure that no ChannelMonitors for a given channel anywhere contain out-of-date /// information and are actively monitoring the chain. -#[derive(Clone)] +/// +/// Pending Events or updated HTLCs which have not yet been read out by +/// get_and_clear_pending_htlcs_updated or get_and_clear_pending_events are serialized to disk and +/// reloaded at deserialize-time. Thus, you must ensure that, when handling events, all events +/// gotten are fully handled before re-serializing the new state. pub struct ChannelMonitor { latest_update_id: u64, commitment_transaction_number_obscure_factor: u64, - key_storage: Storage, - their_htlc_base_key: Option, - their_delayed_payment_base_key: Option, - funding_redeemscript: Option