X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Fchannelmonitor.rs;h=d24a5f528c0a2c45339bd8c537c29a051970a72a;hb=ba75b3ecd7f88a79ff6392a5229c4ab6c14a8591;hp=a8fdb32aa3f7c1cda9ac20299cc48f839e4251be;hpb=3d640da5c343111f538f006996c13c9a98e0d9e6;p=rust-lightning diff --git a/lightning/src/ln/channelmonitor.rs b/lightning/src/ln/channelmonitor.rs index a8fdb32a..d24a5f52 100644 --- a/lightning/src/ln/channelmonitor.rs +++ b/lightning/src/ln/channelmonitor.rs @@ -124,9 +124,11 @@ 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, } @@ -153,6 +155,13 @@ impl_writeable!(HTLCUpdate, 0, { payment_hash, payment_preimage, source }); /// 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 @@ -281,23 +290,9 @@ impl 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); @@ -312,8 +307,8 @@ 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")) } @@ -389,53 +384,10 @@ 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; -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, - } - }, - } - } -} - #[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, @@ -451,53 +403,55 @@ struct LocalSignedTx { #[derive(Clone, PartialEq)] 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 { + channel_value: u64, } } 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[..])?; 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 { ref channel_value } => { + writer.write_all(&[3; 1])?; + channel_value.write(writer)?; } } Ok(()) @@ -508,13 +462,13 @@ 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 = Readable::read(reader)?; let amount = Readable::read(reader)?; InputMaterial::Revoked { - script, + witness_script, pubkey, key, is_htlc, @@ -522,13 +476,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, @@ -536,16 +490,17 @@ 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 => { + let channel_value = Readable::read(reader)?; + InputMaterial::Funding { + channel_value } } _ => return Err(DecodeError::InvalidValue), @@ -554,6 +509,28 @@ 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)] @@ -564,6 +541,9 @@ enum OnchainEvent { HTLCUpdate { htlc_update: (HTLCSource, PaymentHash), }, + MaturingOutput { + descriptor: SpendableOutputDescriptor, + }, } const SERIALIZATION_VERSION: u8 = 1; @@ -599,6 +579,13 @@ 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 { @@ -640,6 +627,10 @@ 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(()) } @@ -693,6 +684,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), } } @@ -712,16 +708,25 @@ 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