Convert most chain::* inner structs and enums to TLV-based ser macros
[rust-lightning] / lightning / src / chain / channelmonitor.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
8 // licenses.
9
10 //! The logic to monitor for on-chain transactions and create the relevant claim responses lives
11 //! here.
12 //!
13 //! ChannelMonitor objects are generated by ChannelManager in response to relevant
14 //! messages/actions, and MUST be persisted to disk (and, preferably, remotely) before progress can
15 //! be made in responding to certain messages, see [`chain::Watch`] for more.
16 //!
17 //! Note that ChannelMonitors are an important part of the lightning trust model and a copy of the
18 //! latest ChannelMonitor must always be actively monitoring for chain updates (and no out-of-date
19 //! ChannelMonitors should do so). Thus, if you're building rust-lightning into an HSM or other
20 //! security-domain-separated system design, you should consider having multiple paths for
21 //! ChannelMonitors to get out of the HSM and onto monitoring devices.
22
23 use bitcoin::blockdata::block::{Block, BlockHeader};
24 use bitcoin::blockdata::transaction::{TxOut,Transaction};
25 use bitcoin::blockdata::script::{Script, Builder};
26 use bitcoin::blockdata::opcodes;
27
28 use bitcoin::hashes::Hash;
29 use bitcoin::hashes::sha256::Hash as Sha256;
30 use bitcoin::hash_types::{Txid, BlockHash, WPubkeyHash};
31
32 use bitcoin::secp256k1::{Secp256k1,Signature};
33 use bitcoin::secp256k1::key::{SecretKey,PublicKey};
34 use bitcoin::secp256k1;
35
36 use ln::{PaymentHash, PaymentPreimage};
37 use ln::msgs::DecodeError;
38 use ln::chan_utils;
39 use ln::chan_utils::{CounterpartyCommitmentSecrets, HTLCOutputInCommitment, HTLCType, ChannelTransactionParameters, HolderCommitmentTransaction};
40 use ln::channelmanager::{BestBlock, HTLCSource};
41 use chain;
42 use chain::WatchedOutput;
43 use chain::chaininterface::{BroadcasterInterface, FeeEstimator};
44 use chain::transaction::{OutPoint, TransactionData};
45 use chain::keysinterface::{SpendableOutputDescriptor, StaticPaymentOutputDescriptor, DelayedPaymentOutputDescriptor, Sign, KeysInterface};
46 use chain::onchaintx::OnchainTxHandler;
47 use chain::package::{CounterpartyOfferedHTLCOutput, CounterpartyReceivedHTLCOutput, HolderFundingOutput, HolderHTLCOutput, PackageSolvingData, PackageTemplate, RevokedOutput, RevokedHTLCOutput};
48 use chain::Filter;
49 use util::logger::Logger;
50 use util::ser::{Readable, ReadableArgs, MaybeReadable, Writer, Writeable, U48};
51 use util::byte_utils;
52 use util::events::Event;
53
54 use prelude::*;
55 use std::collections::{HashMap, HashSet};
56 use core::{cmp, mem};
57 use std::io::Error;
58 use core::ops::Deref;
59 use std::sync::Mutex;
60
61 /// An update generated by the underlying Channel itself which contains some new information the
62 /// ChannelMonitor should be made aware of.
63 #[cfg_attr(any(test, feature = "fuzztarget", feature = "_test_utils"), derive(PartialEq))]
64 #[derive(Clone)]
65 #[must_use]
66 pub struct ChannelMonitorUpdate {
67         pub(crate) updates: Vec<ChannelMonitorUpdateStep>,
68         /// The sequence number of this update. Updates *must* be replayed in-order according to this
69         /// sequence number (and updates may panic if they are not). The update_id values are strictly
70         /// increasing and increase by one for each new update, with one exception specified below.
71         ///
72         /// This sequence number is also used to track up to which points updates which returned
73         /// ChannelMonitorUpdateErr::TemporaryFailure have been applied to all copies of a given
74         /// ChannelMonitor when ChannelManager::channel_monitor_updated is called.
75         ///
76         /// The only instance where update_id values are not strictly increasing is the case where we
77         /// allow post-force-close updates with a special update ID of [`CLOSED_CHANNEL_UPDATE_ID`]. See
78         /// its docs for more details.
79         pub update_id: u64,
80 }
81
82 /// If:
83 ///    (1) a channel has been force closed and
84 ///    (2) we receive a preimage from a forward link that allows us to spend an HTLC output on
85 ///        this channel's (the backward link's) broadcasted commitment transaction
86 /// then we allow the `ChannelManager` to send a `ChannelMonitorUpdate` with this update ID,
87 /// with the update providing said payment preimage. No other update types are allowed after
88 /// force-close.
89 pub const CLOSED_CHANNEL_UPDATE_ID: u64 = core::u64::MAX;
90
91 impl Writeable for ChannelMonitorUpdate {
92         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
93                 self.update_id.write(w)?;
94                 (self.updates.len() as u64).write(w)?;
95                 for update_step in self.updates.iter() {
96                         update_step.write(w)?;
97                 }
98                 Ok(())
99         }
100 }
101 impl Readable for ChannelMonitorUpdate {
102         fn read<R: ::std::io::Read>(r: &mut R) -> Result<Self, DecodeError> {
103                 let update_id: u64 = Readable::read(r)?;
104                 let len: u64 = Readable::read(r)?;
105                 let mut updates = Vec::with_capacity(cmp::min(len as usize, MAX_ALLOC_SIZE / ::core::mem::size_of::<ChannelMonitorUpdateStep>()));
106                 for _ in 0..len {
107                         updates.push(Readable::read(r)?);
108                 }
109                 Ok(Self { update_id, updates })
110         }
111 }
112
113 /// An error enum representing a failure to persist a channel monitor update.
114 #[derive(Clone, Debug)]
115 pub enum ChannelMonitorUpdateErr {
116         /// Used to indicate a temporary failure (eg connection to a watchtower or remote backup of
117         /// our state failed, but is expected to succeed at some point in the future).
118         ///
119         /// Such a failure will "freeze" a channel, preventing us from revoking old states or
120         /// submitting new commitment transactions to the counterparty. Once the update(s) which failed
121         /// have been successfully applied, ChannelManager::channel_monitor_updated can be used to
122         /// restore the channel to an operational state.
123         ///
124         /// Note that a given ChannelManager will *never* re-generate a given ChannelMonitorUpdate. If
125         /// you return a TemporaryFailure you must ensure that it is written to disk safely before
126         /// writing out the latest ChannelManager state.
127         ///
128         /// Even when a channel has been "frozen" updates to the ChannelMonitor can continue to occur
129         /// (eg if an inbound HTLC which we forwarded was claimed upstream resulting in us attempting
130         /// to claim it on this channel) and those updates must be applied wherever they can be. At
131         /// least one such updated ChannelMonitor must be persisted otherwise PermanentFailure should
132         /// be returned to get things on-chain ASAP using only the in-memory copy. Obviously updates to
133         /// the channel which would invalidate previous ChannelMonitors are not made when a channel has
134         /// been "frozen".
135         ///
136         /// Note that even if updates made after TemporaryFailure succeed you must still call
137         /// channel_monitor_updated to ensure you have the latest monitor and re-enable normal channel
138         /// operation.
139         ///
140         /// Note that the update being processed here will not be replayed for you when you call
141         /// ChannelManager::channel_monitor_updated, so you must store the update itself along
142         /// with the persisted ChannelMonitor on your own local disk prior to returning a
143         /// TemporaryFailure. You may, of course, employ a journaling approach, storing only the
144         /// ChannelMonitorUpdate on disk without updating the monitor itself, replaying the journal at
145         /// reload-time.
146         ///
147         /// For deployments where a copy of ChannelMonitors and other local state are backed up in a
148         /// remote location (with local copies persisted immediately), it is anticipated that all
149         /// updates will return TemporaryFailure until the remote copies could be updated.
150         TemporaryFailure,
151         /// Used to indicate no further channel monitor updates will be allowed (eg we've moved on to a
152         /// different watchtower and cannot update with all watchtowers that were previously informed
153         /// of this channel).
154         ///
155         /// At reception of this error, ChannelManager will force-close the channel and return at
156         /// least a final ChannelMonitorUpdate::ChannelForceClosed which must be delivered to at
157         /// least one ChannelMonitor copy. Revocation secret MUST NOT be released and offchain channel
158         /// update must be rejected.
159         ///
160         /// This failure may also signal a failure to update the local persisted copy of one of
161         /// the channel monitor instance.
162         ///
163         /// Note that even when you fail a holder commitment transaction update, you must store the
164         /// update to ensure you can claim from it in case of a duplicate copy of this ChannelMonitor
165         /// broadcasts it (e.g distributed channel-monitor deployment)
166         ///
167         /// In case of distributed watchtowers deployment, the new version must be written to disk, as
168         /// state may have been stored but rejected due to a block forcing a commitment broadcast. This
169         /// storage is used to claim outputs of rejected state confirmed onchain by another watchtower,
170         /// lagging behind on block processing.
171         PermanentFailure,
172 }
173
174 /// General Err type for ChannelMonitor actions. Generally, this implies that the data provided is
175 /// inconsistent with the ChannelMonitor being called. eg for ChannelMonitor::update_monitor this
176 /// means you tried to update a monitor for a different channel or the ChannelMonitorUpdate was
177 /// corrupted.
178 /// Contains a developer-readable error message.
179 #[derive(Clone, Debug)]
180 pub struct MonitorUpdateError(pub &'static str);
181
182 /// An event to be processed by the ChannelManager.
183 #[derive(Clone, PartialEq)]
184 pub enum MonitorEvent {
185         /// A monitor event containing an HTLCUpdate.
186         HTLCEvent(HTLCUpdate),
187
188         /// A monitor event that the Channel's commitment transaction was broadcasted.
189         CommitmentTxBroadcasted(OutPoint),
190 }
191
192 /// Simple structure sent back by `chain::Watch` when an HTLC from a forward channel is detected on
193 /// chain. Used to update the corresponding HTLC in the backward channel. Failing to pass the
194 /// preimage claim backward will lead to loss of funds.
195 #[derive(Clone, PartialEq)]
196 pub struct HTLCUpdate {
197         pub(crate) payment_hash: PaymentHash,
198         pub(crate) payment_preimage: Option<PaymentPreimage>,
199         pub(crate) source: HTLCSource
200 }
201 impl_writeable_tlv_based!(HTLCUpdate, {
202         (0, payment_hash),
203         (2, source),
204 }, {
205         (4, payment_preimage)
206 }, {});
207
208 /// If an HTLC expires within this many blocks, don't try to claim it in a shared transaction,
209 /// instead claiming it in its own individual transaction.
210 pub(crate) const CLTV_SHARED_CLAIM_BUFFER: u32 = 12;
211 /// If an HTLC expires within this many blocks, force-close the channel to broadcast the
212 /// HTLC-Success transaction.
213 /// In other words, this is an upper bound on how many blocks we think it can take us to get a
214 /// transaction confirmed (and we use it in a few more, equivalent, places).
215 pub(crate) const CLTV_CLAIM_BUFFER: u32 = 18;
216 /// Number of blocks by which point we expect our counterparty to have seen new blocks on the
217 /// network and done a full update_fail_htlc/commitment_signed dance (+ we've updated all our
218 /// copies of ChannelMonitors, including watchtowers). We could enforce the contract by failing
219 /// at CLTV expiration height but giving a grace period to our peer may be profitable for us if he
220 /// can provide an over-late preimage. Nevertheless, grace period has to be accounted in our
221 /// CLTV_EXPIRY_DELTA to be secure. Following this policy we may decrease the rate of channel failures
222 /// due to expiration but increase the cost of funds being locked longuer in case of failure.
223 /// This delay also cover a low-power peer being slow to process blocks and so being behind us on
224 /// accurate block height.
225 /// In case of onchain failure to be pass backward we may see the last block of ANTI_REORG_DELAY
226 /// with at worst this delay, so we are not only using this value as a mercy for them but also
227 /// us as a safeguard to delay with enough time.
228 pub(crate) const LATENCY_GRACE_PERIOD_BLOCKS: u32 = 3;
229 /// Number of blocks we wait on seeing a HTLC output being solved before we fail corresponding inbound
230 /// HTLCs. This prevents us from failing backwards and then getting a reorg resulting in us losing money.
231 // We also use this delay to be sure we can remove our in-flight claim txn from bump candidates buffer.
232 // It may cause spurious generation of bumped claim txn but that's alright given the outpoint is already
233 // solved by a previous claim tx. What we want to avoid is reorg evicting our claim tx and us not
234 // keep bumping another claim tx to solve the outpoint.
235 pub const ANTI_REORG_DELAY: u32 = 6;
236 /// Number of blocks before confirmation at which we fail back an un-relayed HTLC or at which we
237 /// refuse to accept a new HTLC.
238 ///
239 /// This is used for a few separate purposes:
240 /// 1) if we've received an MPP HTLC to us and it expires within this many blocks and we are
241 ///    waiting on additional parts (or waiting on the preimage for any HTLC from the user), we will
242 ///    fail this HTLC,
243 /// 2) if we receive an HTLC within this many blocks of its expiry (plus one to avoid a race
244 ///    condition with the above), we will fail this HTLC without telling the user we received it,
245 /// 3) if we are waiting on a connection or a channel state update to send an HTLC to a peer, and
246 ///    that HTLC expires within this many blocks, we will simply fail the HTLC instead.
247 ///
248 /// (1) is all about protecting us - we need enough time to update the channel state before we hit
249 /// CLTV_CLAIM_BUFFER, at which point we'd go on chain to claim the HTLC with the preimage.
250 ///
251 /// (2) is the same, but with an additional buffer to avoid accepting an HTLC which is immediately
252 /// in a race condition between the user connecting a block (which would fail it) and the user
253 /// providing us the preimage (which would claim it).
254 ///
255 /// (3) is about our counterparty - we don't want to relay an HTLC to a counterparty when they may
256 /// end up force-closing the channel on us to claim it.
257 pub(crate) const HTLC_FAIL_BACK_BUFFER: u32 = CLTV_CLAIM_BUFFER + LATENCY_GRACE_PERIOD_BLOCKS;
258
259 // TODO(devrandom) replace this with HolderCommitmentTransaction
260 #[derive(Clone, PartialEq)]
261 struct HolderSignedTx {
262         /// txid of the transaction in tx, just used to make comparison faster
263         txid: Txid,
264         revocation_key: PublicKey,
265         a_htlc_key: PublicKey,
266         b_htlc_key: PublicKey,
267         delayed_payment_key: PublicKey,
268         per_commitment_point: PublicKey,
269         feerate_per_kw: u32,
270         htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Signature>, Option<HTLCSource>)>,
271 }
272 impl_writeable_tlv_based!(HolderSignedTx, {
273         (0, txid),
274         (2, revocation_key),
275         (4, a_htlc_key),
276         (6, b_htlc_key),
277         (8, delayed_payment_key),
278         (10, per_commitment_point),
279         (12, feerate_per_kw),
280 }, {}, {
281         (14, htlc_outputs)
282 });
283
284 /// We use this to track counterparty commitment transactions and htlcs outputs and
285 /// use it to generate any justice or 2nd-stage preimage/timeout transactions.
286 #[derive(PartialEq)]
287 struct CounterpartyCommitmentTransaction {
288         counterparty_delayed_payment_base_key: PublicKey,
289         counterparty_htlc_base_key: PublicKey,
290         on_counterparty_tx_csv: u16,
291         per_htlc: HashMap<Txid, Vec<HTLCOutputInCommitment>>
292 }
293
294 impl Writeable for CounterpartyCommitmentTransaction {
295         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
296                 self.counterparty_delayed_payment_base_key.write(w)?;
297                 self.counterparty_htlc_base_key.write(w)?;
298                 w.write_all(&byte_utils::be16_to_array(self.on_counterparty_tx_csv))?;
299                 w.write_all(&byte_utils::be64_to_array(self.per_htlc.len() as u64))?;
300                 for (ref txid, ref htlcs) in self.per_htlc.iter() {
301                         w.write_all(&txid[..])?;
302                         w.write_all(&byte_utils::be64_to_array(htlcs.len() as u64))?;
303                         for &ref htlc in htlcs.iter() {
304                                 htlc.write(w)?;
305                         }
306                 }
307                 Ok(())
308         }
309 }
310 impl Readable for CounterpartyCommitmentTransaction {
311         fn read<R: ::std::io::Read>(r: &mut R) -> Result<Self, DecodeError> {
312                 let counterparty_commitment_transaction = {
313                         let counterparty_delayed_payment_base_key = Readable::read(r)?;
314                         let counterparty_htlc_base_key = Readable::read(r)?;
315                         let on_counterparty_tx_csv: u16 = Readable::read(r)?;
316                         let per_htlc_len: u64 = Readable::read(r)?;
317                         let mut per_htlc = HashMap::with_capacity(cmp::min(per_htlc_len as usize, MAX_ALLOC_SIZE / 64));
318                         for _  in 0..per_htlc_len {
319                                 let txid: Txid = Readable::read(r)?;
320                                 let htlcs_count: u64 = Readable::read(r)?;
321                                 let mut htlcs = Vec::with_capacity(cmp::min(htlcs_count as usize, MAX_ALLOC_SIZE / 32));
322                                 for _ in 0..htlcs_count {
323                                         let htlc = Readable::read(r)?;
324                                         htlcs.push(htlc);
325                                 }
326                                 if let Some(_) = per_htlc.insert(txid, htlcs) {
327                                         return Err(DecodeError::InvalidValue);
328                                 }
329                         }
330                         CounterpartyCommitmentTransaction {
331                                 counterparty_delayed_payment_base_key,
332                                 counterparty_htlc_base_key,
333                                 on_counterparty_tx_csv,
334                                 per_htlc,
335                         }
336                 };
337                 Ok(counterparty_commitment_transaction)
338         }
339 }
340
341 /// An entry for an [`OnchainEvent`], stating the block height when the event was observed and the
342 /// transaction causing it.
343 ///
344 /// Used to determine when the on-chain event can be considered safe from a chain reorganization.
345 #[derive(PartialEq)]
346 struct OnchainEventEntry {
347         txid: Txid,
348         height: u32,
349         event: OnchainEvent,
350 }
351
352 impl OnchainEventEntry {
353         fn confirmation_threshold(&self) -> u32 {
354                 self.height + ANTI_REORG_DELAY - 1
355         }
356
357         fn has_reached_confirmation_threshold(&self, height: u32) -> bool {
358                 height >= self.confirmation_threshold()
359         }
360 }
361
362 /// Upon discovering of some classes of onchain tx by ChannelMonitor, we may have to take actions on it
363 /// once they mature to enough confirmations (ANTI_REORG_DELAY)
364 #[derive(PartialEq)]
365 enum OnchainEvent {
366         /// HTLC output getting solved by a timeout, at maturation we pass upstream payment source information to solve
367         /// inbound HTLC in backward channel. Note, in case of preimage, we pass info to upstream without delay as we can
368         /// only win from it, so it's never an OnchainEvent
369         HTLCUpdate {
370                 source: HTLCSource,
371                 payment_hash: PaymentHash,
372         },
373         MaturingOutput {
374                 descriptor: SpendableOutputDescriptor,
375         },
376 }
377
378 impl_writeable_tlv_based!(OnchainEventEntry, {
379         (0, txid),
380         (2, height),
381         (4, event),
382 }, {}, {});
383
384 impl_writeable_tlv_based_enum!(OnchainEvent,
385         (0, HTLCUpdate) => {
386                 (0, source),
387                 (2, payment_hash),
388         }, {}, {},
389         (1, MaturingOutput) => {
390                 (0, descriptor),
391         }, {}, {},
392 ;);
393
394 #[cfg_attr(any(test, feature = "fuzztarget", feature = "_test_utils"), derive(PartialEq))]
395 #[derive(Clone)]
396 pub(crate) enum ChannelMonitorUpdateStep {
397         LatestHolderCommitmentTXInfo {
398                 commitment_tx: HolderCommitmentTransaction,
399                 htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Signature>, Option<HTLCSource>)>,
400         },
401         LatestCounterpartyCommitmentTXInfo {
402                 commitment_txid: Txid,
403                 htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)>,
404                 commitment_number: u64,
405                 their_revocation_point: PublicKey,
406         },
407         PaymentPreimage {
408                 payment_preimage: PaymentPreimage,
409         },
410         CommitmentSecret {
411                 idx: u64,
412                 secret: [u8; 32],
413         },
414         /// Used to indicate that the no future updates will occur, and likely that the latest holder
415         /// commitment transaction(s) should be broadcast, as the channel has been force-closed.
416         ChannelForceClosed {
417                 /// If set to false, we shouldn't broadcast the latest holder commitment transaction as we
418                 /// think we've fallen behind!
419                 should_broadcast: bool,
420         },
421 }
422
423 impl_writeable_tlv_based_enum!(ChannelMonitorUpdateStep,
424         (0, LatestHolderCommitmentTXInfo) => {
425                 (0, commitment_tx),
426         }, {}, {
427                 (2, htlc_outputs),
428         },
429         (1, LatestCounterpartyCommitmentTXInfo) => {
430                 (0, commitment_txid),
431                 (2, commitment_number),
432                 (4, their_revocation_point),
433         }, {}, {
434                 (6, htlc_outputs),
435         },
436         (2, PaymentPreimage) => {
437                 (0, payment_preimage),
438         }, {}, {},
439         (3, CommitmentSecret) => {
440                 (0, idx),
441                 (2, secret),
442         }, {}, {},
443         (4, ChannelForceClosed) => {
444                 (0, should_broadcast),
445         }, {}, {},
446 ;);
447
448 /// A ChannelMonitor handles chain events (blocks connected and disconnected) and generates
449 /// on-chain transactions to ensure no loss of funds occurs.
450 ///
451 /// You MUST ensure that no ChannelMonitors for a given channel anywhere contain out-of-date
452 /// information and are actively monitoring the chain.
453 ///
454 /// Pending Events or updated HTLCs which have not yet been read out by
455 /// get_and_clear_pending_monitor_events or get_and_clear_pending_events are serialized to disk and
456 /// reloaded at deserialize-time. Thus, you must ensure that, when handling events, all events
457 /// gotten are fully handled before re-serializing the new state.
458 ///
459 /// Note that the deserializer is only implemented for (BlockHash, ChannelMonitor), which
460 /// tells you the last block hash which was block_connect()ed. You MUST rescan any blocks along
461 /// the "reorg path" (ie disconnecting blocks until you find a common ancestor from both the
462 /// returned block hash and the the current chain and then reconnecting blocks to get to the
463 /// best chain) upon deserializing the object!
464 pub struct ChannelMonitor<Signer: Sign> {
465         #[cfg(test)]
466         pub(crate) inner: Mutex<ChannelMonitorImpl<Signer>>,
467         #[cfg(not(test))]
468         inner: Mutex<ChannelMonitorImpl<Signer>>,
469 }
470
471 pub(crate) struct ChannelMonitorImpl<Signer: Sign> {
472         latest_update_id: u64,
473         commitment_transaction_number_obscure_factor: u64,
474
475         destination_script: Script,
476         broadcasted_holder_revokable_script: Option<(Script, PublicKey, PublicKey)>,
477         counterparty_payment_script: Script,
478         shutdown_script: Script,
479
480         channel_keys_id: [u8; 32],
481         holder_revocation_basepoint: PublicKey,
482         funding_info: (OutPoint, Script),
483         current_counterparty_commitment_txid: Option<Txid>,
484         prev_counterparty_commitment_txid: Option<Txid>,
485
486         counterparty_tx_cache: CounterpartyCommitmentTransaction,
487         funding_redeemscript: Script,
488         channel_value_satoshis: u64,
489         // first is the idx of the first of the two revocation points
490         their_cur_revocation_points: Option<(u64, PublicKey, Option<PublicKey>)>,
491
492         on_holder_tx_csv: u16,
493
494         commitment_secrets: CounterpartyCommitmentSecrets,
495         counterparty_claimable_outpoints: HashMap<Txid, Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)>>,
496         /// We cannot identify HTLC-Success or HTLC-Timeout transactions by themselves on the chain.
497         /// Nor can we figure out their commitment numbers without the commitment transaction they are
498         /// spending. Thus, in order to claim them via revocation key, we track all the counterparty
499         /// commitment transactions which we find on-chain, mapping them to the commitment number which
500         /// can be used to derive the revocation key and claim the transactions.
501         counterparty_commitment_txn_on_chain: HashMap<Txid, u64>,
502         /// Cache used to make pruning of payment_preimages faster.
503         /// Maps payment_hash values to commitment numbers for counterparty transactions for non-revoked
504         /// counterparty transactions (ie should remain pretty small).
505         /// Serialized to disk but should generally not be sent to Watchtowers.
506         counterparty_hash_commitment_number: HashMap<PaymentHash, u64>,
507
508         // We store two holder commitment transactions to avoid any race conditions where we may update
509         // some monitors (potentially on watchtowers) but then fail to update others, resulting in the
510         // various monitors for one channel being out of sync, and us broadcasting a holder
511         // transaction for which we have deleted claim information on some watchtowers.
512         prev_holder_signed_commitment_tx: Option<HolderSignedTx>,
513         current_holder_commitment_tx: HolderSignedTx,
514
515         // Used just for ChannelManager to make sure it has the latest channel data during
516         // deserialization
517         current_counterparty_commitment_number: u64,
518         // Used just for ChannelManager to make sure it has the latest channel data during
519         // deserialization
520         current_holder_commitment_number: u64,
521
522         payment_preimages: HashMap<PaymentHash, PaymentPreimage>,
523
524         pending_monitor_events: Vec<MonitorEvent>,
525         pending_events: Vec<Event>,
526
527         // Used to track on-chain events (i.e., transactions part of channels confirmed on chain) on
528         // which to take actions once they reach enough confirmations. Each entry includes the
529         // transaction's id and the height when the transaction was confirmed on chain.
530         onchain_events_awaiting_threshold_conf: Vec<OnchainEventEntry>,
531
532         // If we get serialized out and re-read, we need to make sure that the chain monitoring
533         // interface knows about the TXOs that we want to be notified of spends of. We could probably
534         // be smart and derive them from the above storage fields, but its much simpler and more
535         // Obviously Correct (tm) if we just keep track of them explicitly.
536         outputs_to_watch: HashMap<Txid, Vec<(u32, Script)>>,
537
538         #[cfg(test)]
539         pub onchain_tx_handler: OnchainTxHandler<Signer>,
540         #[cfg(not(test))]
541         onchain_tx_handler: OnchainTxHandler<Signer>,
542
543         // This is set when the Channel[Manager] generated a ChannelMonitorUpdate which indicated the
544         // channel has been force-closed. After this is set, no further holder commitment transaction
545         // updates may occur, and we panic!() if one is provided.
546         lockdown_from_offchain: bool,
547
548         // Set once we've signed a holder commitment transaction and handed it over to our
549         // OnchainTxHandler. After this is set, no future updates to our holder commitment transactions
550         // may occur, and we fail any such monitor updates.
551         //
552         // In case of update rejection due to a locally already signed commitment transaction, we
553         // nevertheless store update content to track in case of concurrent broadcast by another
554         // remote monitor out-of-order with regards to the block view.
555         holder_tx_signed: bool,
556
557         // We simply modify best_block in Channel's block_connected so that serialization is
558         // consistent but hopefully the users' copy handles block_connected in a consistent way.
559         // (we do *not*, however, update them in update_monitor to ensure any local user copies keep
560         // their best_block from its state and not based on updated copies that didn't run through
561         // the full block_connected).
562         best_block: BestBlock,
563
564         secp_ctx: Secp256k1<secp256k1::All>, //TODO: dedup this a bit...
565 }
566
567 /// Transaction outputs to watch for on-chain spends.
568 pub type TransactionOutputs = (Txid, Vec<(u32, TxOut)>);
569
570 #[cfg(any(test, feature = "fuzztarget", feature = "_test_utils"))]
571 /// Used only in testing and fuzztarget to check serialization roundtrips don't change the
572 /// underlying object
573 impl<Signer: Sign> PartialEq for ChannelMonitor<Signer> {
574         fn eq(&self, other: &Self) -> bool {
575                 let inner = self.inner.lock().unwrap();
576                 let other = other.inner.lock().unwrap();
577                 inner.eq(&other)
578         }
579 }
580
581 #[cfg(any(test, feature = "fuzztarget", feature = "_test_utils"))]
582 /// Used only in testing and fuzztarget to check serialization roundtrips don't change the
583 /// underlying object
584 impl<Signer: Sign> PartialEq for ChannelMonitorImpl<Signer> {
585         fn eq(&self, other: &Self) -> bool {
586                 if self.latest_update_id != other.latest_update_id ||
587                         self.commitment_transaction_number_obscure_factor != other.commitment_transaction_number_obscure_factor ||
588                         self.destination_script != other.destination_script ||
589                         self.broadcasted_holder_revokable_script != other.broadcasted_holder_revokable_script ||
590                         self.counterparty_payment_script != other.counterparty_payment_script ||
591                         self.channel_keys_id != other.channel_keys_id ||
592                         self.holder_revocation_basepoint != other.holder_revocation_basepoint ||
593                         self.funding_info != other.funding_info ||
594                         self.current_counterparty_commitment_txid != other.current_counterparty_commitment_txid ||
595                         self.prev_counterparty_commitment_txid != other.prev_counterparty_commitment_txid ||
596                         self.counterparty_tx_cache != other.counterparty_tx_cache ||
597                         self.funding_redeemscript != other.funding_redeemscript ||
598                         self.channel_value_satoshis != other.channel_value_satoshis ||
599                         self.their_cur_revocation_points != other.their_cur_revocation_points ||
600                         self.on_holder_tx_csv != other.on_holder_tx_csv ||
601                         self.commitment_secrets != other.commitment_secrets ||
602                         self.counterparty_claimable_outpoints != other.counterparty_claimable_outpoints ||
603                         self.counterparty_commitment_txn_on_chain != other.counterparty_commitment_txn_on_chain ||
604                         self.counterparty_hash_commitment_number != other.counterparty_hash_commitment_number ||
605                         self.prev_holder_signed_commitment_tx != other.prev_holder_signed_commitment_tx ||
606                         self.current_counterparty_commitment_number != other.current_counterparty_commitment_number ||
607                         self.current_holder_commitment_number != other.current_holder_commitment_number ||
608                         self.current_holder_commitment_tx != other.current_holder_commitment_tx ||
609                         self.payment_preimages != other.payment_preimages ||
610                         self.pending_monitor_events != other.pending_monitor_events ||
611                         self.pending_events.len() != other.pending_events.len() || // We trust events to round-trip properly
612                         self.onchain_events_awaiting_threshold_conf != other.onchain_events_awaiting_threshold_conf ||
613                         self.outputs_to_watch != other.outputs_to_watch ||
614                         self.lockdown_from_offchain != other.lockdown_from_offchain ||
615                         self.holder_tx_signed != other.holder_tx_signed
616                 {
617                         false
618                 } else {
619                         true
620                 }
621         }
622 }
623
624 impl<Signer: Sign> Writeable for ChannelMonitor<Signer> {
625         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), Error> {
626                 self.inner.lock().unwrap().write(writer)
627         }
628 }
629
630 const SERIALIZATION_VERSION: u8 = 1;
631 const MIN_SERIALIZATION_VERSION: u8 = 1;
632
633 impl<Signer: Sign> Writeable for ChannelMonitorImpl<Signer> {
634         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), Error> {
635                 write_ver_prefix!(writer, SERIALIZATION_VERSION, MIN_SERIALIZATION_VERSION);
636
637                 self.latest_update_id.write(writer)?;
638
639                 // Set in initial Channel-object creation, so should always be set by now:
640                 U48(self.commitment_transaction_number_obscure_factor).write(writer)?;
641
642                 self.destination_script.write(writer)?;
643                 if let Some(ref broadcasted_holder_revokable_script) = self.broadcasted_holder_revokable_script {
644                         writer.write_all(&[0; 1])?;
645                         broadcasted_holder_revokable_script.0.write(writer)?;
646                         broadcasted_holder_revokable_script.1.write(writer)?;
647                         broadcasted_holder_revokable_script.2.write(writer)?;
648                 } else {
649                         writer.write_all(&[1; 1])?;
650                 }
651
652                 self.counterparty_payment_script.write(writer)?;
653                 self.shutdown_script.write(writer)?;
654
655                 self.channel_keys_id.write(writer)?;
656                 self.holder_revocation_basepoint.write(writer)?;
657                 writer.write_all(&self.funding_info.0.txid[..])?;
658                 writer.write_all(&byte_utils::be16_to_array(self.funding_info.0.index))?;
659                 self.funding_info.1.write(writer)?;
660                 self.current_counterparty_commitment_txid.write(writer)?;
661                 self.prev_counterparty_commitment_txid.write(writer)?;
662
663                 self.counterparty_tx_cache.write(writer)?;
664                 self.funding_redeemscript.write(writer)?;
665                 self.channel_value_satoshis.write(writer)?;
666
667                 match self.their_cur_revocation_points {
668                         Some((idx, pubkey, second_option)) => {
669                                 writer.write_all(&byte_utils::be48_to_array(idx))?;
670                                 writer.write_all(&pubkey.serialize())?;
671                                 match second_option {
672                                         Some(second_pubkey) => {
673                                                 writer.write_all(&second_pubkey.serialize())?;
674                                         },
675                                         None => {
676                                                 writer.write_all(&[0; 33])?;
677                                         },
678                                 }
679                         },
680                         None => {
681                                 writer.write_all(&byte_utils::be48_to_array(0))?;
682                         },
683                 }
684
685                 writer.write_all(&byte_utils::be16_to_array(self.on_holder_tx_csv))?;
686
687                 self.commitment_secrets.write(writer)?;
688
689                 macro_rules! serialize_htlc_in_commitment {
690                         ($htlc_output: expr) => {
691                                 writer.write_all(&[$htlc_output.offered as u8; 1])?;
692                                 writer.write_all(&byte_utils::be64_to_array($htlc_output.amount_msat))?;
693                                 writer.write_all(&byte_utils::be32_to_array($htlc_output.cltv_expiry))?;
694                                 writer.write_all(&$htlc_output.payment_hash.0[..])?;
695                                 $htlc_output.transaction_output_index.write(writer)?;
696                         }
697                 }
698
699                 writer.write_all(&byte_utils::be64_to_array(self.counterparty_claimable_outpoints.len() as u64))?;
700                 for (ref txid, ref htlc_infos) in self.counterparty_claimable_outpoints.iter() {
701                         writer.write_all(&txid[..])?;
702                         writer.write_all(&byte_utils::be64_to_array(htlc_infos.len() as u64))?;
703                         for &(ref htlc_output, ref htlc_source) in htlc_infos.iter() {
704                                 serialize_htlc_in_commitment!(htlc_output);
705                                 htlc_source.as_ref().map(|b| b.as_ref()).write(writer)?;
706                         }
707                 }
708
709                 writer.write_all(&byte_utils::be64_to_array(self.counterparty_commitment_txn_on_chain.len() as u64))?;
710                 for (ref txid, commitment_number) in self.counterparty_commitment_txn_on_chain.iter() {
711                         writer.write_all(&txid[..])?;
712                         writer.write_all(&byte_utils::be48_to_array(*commitment_number))?;
713                 }
714
715                 writer.write_all(&byte_utils::be64_to_array(self.counterparty_hash_commitment_number.len() as u64))?;
716                 for (ref payment_hash, commitment_number) in self.counterparty_hash_commitment_number.iter() {
717                         writer.write_all(&payment_hash.0[..])?;
718                         writer.write_all(&byte_utils::be48_to_array(*commitment_number))?;
719                 }
720
721                 if let Some(ref prev_holder_tx) = self.prev_holder_signed_commitment_tx {
722                         writer.write_all(&[1; 1])?;
723                         prev_holder_tx.write(writer)?;
724                 } else {
725                         writer.write_all(&[0; 1])?;
726                 }
727
728                 self.current_holder_commitment_tx.write(writer)?;
729
730                 writer.write_all(&byte_utils::be48_to_array(self.current_counterparty_commitment_number))?;
731                 writer.write_all(&byte_utils::be48_to_array(self.current_holder_commitment_number))?;
732
733                 writer.write_all(&byte_utils::be64_to_array(self.payment_preimages.len() as u64))?;
734                 for payment_preimage in self.payment_preimages.values() {
735                         writer.write_all(&payment_preimage.0[..])?;
736                 }
737
738                 writer.write_all(&byte_utils::be64_to_array(self.pending_monitor_events.len() as u64))?;
739                 for event in self.pending_monitor_events.iter() {
740                         match event {
741                                 MonitorEvent::HTLCEvent(upd) => {
742                                         0u8.write(writer)?;
743                                         upd.write(writer)?;
744                                 },
745                                 MonitorEvent::CommitmentTxBroadcasted(_) => 1u8.write(writer)?
746                         }
747                 }
748
749                 writer.write_all(&byte_utils::be64_to_array(self.pending_events.len() as u64))?;
750                 for event in self.pending_events.iter() {
751                         event.write(writer)?;
752                 }
753
754                 self.best_block.block_hash().write(writer)?;
755                 writer.write_all(&byte_utils::be32_to_array(self.best_block.height()))?;
756
757                 writer.write_all(&byte_utils::be64_to_array(self.onchain_events_awaiting_threshold_conf.len() as u64))?;
758                 for ref entry in self.onchain_events_awaiting_threshold_conf.iter() {
759                         entry.write(writer)?;
760                 }
761
762                 (self.outputs_to_watch.len() as u64).write(writer)?;
763                 for (txid, idx_scripts) in self.outputs_to_watch.iter() {
764                         txid.write(writer)?;
765                         (idx_scripts.len() as u64).write(writer)?;
766                         for (idx, script) in idx_scripts.iter() {
767                                 idx.write(writer)?;
768                                 script.write(writer)?;
769                         }
770                 }
771                 self.onchain_tx_handler.write(writer)?;
772
773                 self.lockdown_from_offchain.write(writer)?;
774                 self.holder_tx_signed.write(writer)?;
775
776                 write_tlv_fields!(writer, {}, {});
777
778                 Ok(())
779         }
780 }
781
782 impl<Signer: Sign> ChannelMonitor<Signer> {
783         pub(crate) fn new(secp_ctx: Secp256k1<secp256k1::All>, keys: Signer, shutdown_pubkey: &PublicKey,
784                           on_counterparty_tx_csv: u16, destination_script: &Script, funding_info: (OutPoint, Script),
785                           channel_parameters: &ChannelTransactionParameters,
786                           funding_redeemscript: Script, channel_value_satoshis: u64,
787                           commitment_transaction_number_obscure_factor: u64,
788                           initial_holder_commitment_tx: HolderCommitmentTransaction,
789                           best_block: BestBlock) -> ChannelMonitor<Signer> {
790
791                 assert!(commitment_transaction_number_obscure_factor <= (1 << 48));
792                 let our_channel_close_key_hash = WPubkeyHash::hash(&shutdown_pubkey.serialize());
793                 let shutdown_script = Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(&our_channel_close_key_hash[..]).into_script();
794                 let payment_key_hash = WPubkeyHash::hash(&keys.pubkeys().payment_point.serialize());
795                 let counterparty_payment_script = Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(&payment_key_hash[..]).into_script();
796
797                 let counterparty_channel_parameters = channel_parameters.counterparty_parameters.as_ref().unwrap();
798                 let counterparty_delayed_payment_base_key = counterparty_channel_parameters.pubkeys.delayed_payment_basepoint;
799                 let counterparty_htlc_base_key = counterparty_channel_parameters.pubkeys.htlc_basepoint;
800                 let counterparty_tx_cache = CounterpartyCommitmentTransaction { counterparty_delayed_payment_base_key, counterparty_htlc_base_key, on_counterparty_tx_csv, per_htlc: HashMap::new() };
801
802                 let channel_keys_id = keys.channel_keys_id();
803                 let holder_revocation_basepoint = keys.pubkeys().revocation_basepoint;
804
805                 // block for Rust 1.34 compat
806                 let (holder_commitment_tx, current_holder_commitment_number) = {
807                         let trusted_tx = initial_holder_commitment_tx.trust();
808                         let txid = trusted_tx.txid();
809
810                         let tx_keys = trusted_tx.keys();
811                         let holder_commitment_tx = HolderSignedTx {
812                                 txid,
813                                 revocation_key: tx_keys.revocation_key,
814                                 a_htlc_key: tx_keys.broadcaster_htlc_key,
815                                 b_htlc_key: tx_keys.countersignatory_htlc_key,
816                                 delayed_payment_key: tx_keys.broadcaster_delayed_payment_key,
817                                 per_commitment_point: tx_keys.per_commitment_point,
818                                 feerate_per_kw: trusted_tx.feerate_per_kw(),
819                                 htlc_outputs: Vec::new(), // There are never any HTLCs in the initial commitment transactions
820                         };
821                         (holder_commitment_tx, trusted_tx.commitment_number())
822                 };
823
824                 let onchain_tx_handler =
825                         OnchainTxHandler::new(destination_script.clone(), keys,
826                         channel_parameters.clone(), initial_holder_commitment_tx, secp_ctx.clone());
827
828                 let mut outputs_to_watch = HashMap::new();
829                 outputs_to_watch.insert(funding_info.0.txid, vec![(funding_info.0.index as u32, funding_info.1.clone())]);
830
831                 ChannelMonitor {
832                         inner: Mutex::new(ChannelMonitorImpl {
833                                 latest_update_id: 0,
834                                 commitment_transaction_number_obscure_factor,
835
836                                 destination_script: destination_script.clone(),
837                                 broadcasted_holder_revokable_script: None,
838                                 counterparty_payment_script,
839                                 shutdown_script,
840
841                                 channel_keys_id,
842                                 holder_revocation_basepoint,
843                                 funding_info,
844                                 current_counterparty_commitment_txid: None,
845                                 prev_counterparty_commitment_txid: None,
846
847                                 counterparty_tx_cache,
848                                 funding_redeemscript,
849                                 channel_value_satoshis,
850                                 their_cur_revocation_points: None,
851
852                                 on_holder_tx_csv: counterparty_channel_parameters.selected_contest_delay,
853
854                                 commitment_secrets: CounterpartyCommitmentSecrets::new(),
855                                 counterparty_claimable_outpoints: HashMap::new(),
856                                 counterparty_commitment_txn_on_chain: HashMap::new(),
857                                 counterparty_hash_commitment_number: HashMap::new(),
858
859                                 prev_holder_signed_commitment_tx: None,
860                                 current_holder_commitment_tx: holder_commitment_tx,
861                                 current_counterparty_commitment_number: 1 << 48,
862                                 current_holder_commitment_number,
863
864                                 payment_preimages: HashMap::new(),
865                                 pending_monitor_events: Vec::new(),
866                                 pending_events: Vec::new(),
867
868                                 onchain_events_awaiting_threshold_conf: Vec::new(),
869                                 outputs_to_watch,
870
871                                 onchain_tx_handler,
872
873                                 lockdown_from_offchain: false,
874                                 holder_tx_signed: false,
875
876                                 best_block,
877
878                                 secp_ctx,
879                         }),
880                 }
881         }
882
883         #[cfg(test)]
884         fn provide_secret(&self, idx: u64, secret: [u8; 32]) -> Result<(), MonitorUpdateError> {
885                 self.inner.lock().unwrap().provide_secret(idx, secret)
886         }
887
888         /// Informs this monitor of the latest counterparty (ie non-broadcastable) commitment transaction.
889         /// The monitor watches for it to be broadcasted and then uses the HTLC information (and
890         /// possibly future revocation/preimage information) to claim outputs where possible.
891         /// We cache also the mapping hash:commitment number to lighten pruning of old preimages by watchtowers.
892         pub(crate) fn provide_latest_counterparty_commitment_tx<L: Deref>(
893                 &self,
894                 txid: Txid,
895                 htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)>,
896                 commitment_number: u64,
897                 their_revocation_point: PublicKey,
898                 logger: &L,
899         ) where L::Target: Logger {
900                 self.inner.lock().unwrap().provide_latest_counterparty_commitment_tx(
901                         txid, htlc_outputs, commitment_number, their_revocation_point, logger)
902         }
903
904         #[cfg(test)]
905         fn provide_latest_holder_commitment_tx(
906                 &self,
907                 holder_commitment_tx: HolderCommitmentTransaction,
908                 htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Signature>, Option<HTLCSource>)>,
909         ) -> Result<(), MonitorUpdateError> {
910                 self.inner.lock().unwrap().provide_latest_holder_commitment_tx(
911                         holder_commitment_tx, htlc_outputs)
912         }
913
914         #[cfg(test)]
915         pub(crate) fn provide_payment_preimage<B: Deref, F: Deref, L: Deref>(
916                 &self,
917                 payment_hash: &PaymentHash,
918                 payment_preimage: &PaymentPreimage,
919                 broadcaster: &B,
920                 fee_estimator: &F,
921                 logger: &L,
922         ) where
923                 B::Target: BroadcasterInterface,
924                 F::Target: FeeEstimator,
925                 L::Target: Logger,
926         {
927                 self.inner.lock().unwrap().provide_payment_preimage(
928                         payment_hash, payment_preimage, broadcaster, fee_estimator, logger)
929         }
930
931         pub(crate) fn broadcast_latest_holder_commitment_txn<B: Deref, L: Deref>(
932                 &self,
933                 broadcaster: &B,
934                 logger: &L,
935         ) where
936                 B::Target: BroadcasterInterface,
937                 L::Target: Logger,
938         {
939                 self.inner.lock().unwrap().broadcast_latest_holder_commitment_txn(broadcaster, logger)
940         }
941
942         /// Updates a ChannelMonitor on the basis of some new information provided by the Channel
943         /// itself.
944         ///
945         /// panics if the given update is not the next update by update_id.
946         pub fn update_monitor<B: Deref, F: Deref, L: Deref>(
947                 &self,
948                 updates: &ChannelMonitorUpdate,
949                 broadcaster: &B,
950                 fee_estimator: &F,
951                 logger: &L,
952         ) -> Result<(), MonitorUpdateError>
953         where
954                 B::Target: BroadcasterInterface,
955                 F::Target: FeeEstimator,
956                 L::Target: Logger,
957         {
958                 self.inner.lock().unwrap().update_monitor(updates, broadcaster, fee_estimator, logger)
959         }
960
961         /// Gets the update_id from the latest ChannelMonitorUpdate which was applied to this
962         /// ChannelMonitor.
963         pub fn get_latest_update_id(&self) -> u64 {
964                 self.inner.lock().unwrap().get_latest_update_id()
965         }
966
967         /// Gets the funding transaction outpoint of the channel this ChannelMonitor is monitoring for.
968         pub fn get_funding_txo(&self) -> (OutPoint, Script) {
969                 self.inner.lock().unwrap().get_funding_txo().clone()
970         }
971
972         /// Gets a list of txids, with their output scripts (in the order they appear in the
973         /// transaction), which we must learn about spends of via block_connected().
974         pub fn get_outputs_to_watch(&self) -> Vec<(Txid, Vec<(u32, Script)>)> {
975                 self.inner.lock().unwrap().get_outputs_to_watch()
976                         .iter().map(|(txid, outputs)| (*txid, outputs.clone())).collect()
977         }
978
979         /// Loads the funding txo and outputs to watch into the given `chain::Filter` by repeatedly
980         /// calling `chain::Filter::register_output` and `chain::Filter::register_tx` until all outputs
981         /// have been registered.
982         pub fn load_outputs_to_watch<F: Deref>(&self, filter: &F) where F::Target: chain::Filter {
983                 let lock = self.inner.lock().unwrap();
984                 filter.register_tx(&lock.get_funding_txo().0.txid, &lock.get_funding_txo().1);
985                 for (txid, outputs) in lock.get_outputs_to_watch().iter() {
986                         for (index, script_pubkey) in outputs.iter() {
987                                 assert!(*index <= u16::max_value() as u32);
988                                 filter.register_output(WatchedOutput {
989                                         block_hash: None,
990                                         outpoint: OutPoint { txid: *txid, index: *index as u16 },
991                                         script_pubkey: script_pubkey.clone(),
992                                 });
993                         }
994                 }
995         }
996
997         /// Get the list of HTLCs who's status has been updated on chain. This should be called by
998         /// ChannelManager via [`chain::Watch::release_pending_monitor_events`].
999         pub fn get_and_clear_pending_monitor_events(&self) -> Vec<MonitorEvent> {
1000                 self.inner.lock().unwrap().get_and_clear_pending_monitor_events()
1001         }
1002
1003         /// Gets the list of pending events which were generated by previous actions, clearing the list
1004         /// in the process.
1005         ///
1006         /// This is called by ChainMonitor::get_and_clear_pending_events() and is equivalent to
1007         /// EventsProvider::get_and_clear_pending_events() except that it requires &mut self as we do
1008         /// no internal locking in ChannelMonitors.
1009         pub fn get_and_clear_pending_events(&self) -> Vec<Event> {
1010                 self.inner.lock().unwrap().get_and_clear_pending_events()
1011         }
1012
1013         pub(crate) fn get_min_seen_secret(&self) -> u64 {
1014                 self.inner.lock().unwrap().get_min_seen_secret()
1015         }
1016
1017         pub(crate) fn get_cur_counterparty_commitment_number(&self) -> u64 {
1018                 self.inner.lock().unwrap().get_cur_counterparty_commitment_number()
1019         }
1020
1021         pub(crate) fn get_cur_holder_commitment_number(&self) -> u64 {
1022                 self.inner.lock().unwrap().get_cur_holder_commitment_number()
1023         }
1024
1025         /// Used by ChannelManager deserialization to broadcast the latest holder state if its copy of
1026         /// the Channel was out-of-date. You may use it to get a broadcastable holder toxic tx in case of
1027         /// fallen-behind, i.e when receiving a channel_reestablish with a proof that our counterparty side knows
1028         /// a higher revocation secret than the holder commitment number we are aware of. Broadcasting these
1029         /// transactions are UNSAFE, as they allow counterparty side to punish you. Nevertheless you may want to
1030         /// broadcast them if counterparty don't close channel with his higher commitment transaction after a
1031         /// substantial amount of time (a month or even a year) to get back funds. Best may be to contact
1032         /// out-of-band the other node operator to coordinate with him if option is available to you.
1033         /// In any-case, choice is up to the user.
1034         pub fn get_latest_holder_commitment_txn<L: Deref>(&self, logger: &L) -> Vec<Transaction>
1035         where L::Target: Logger {
1036                 self.inner.lock().unwrap().get_latest_holder_commitment_txn(logger)
1037         }
1038
1039         /// Unsafe test-only version of get_latest_holder_commitment_txn used by our test framework
1040         /// to bypass HolderCommitmentTransaction state update lockdown after signature and generate
1041         /// revoked commitment transaction.
1042         #[cfg(any(test, feature = "unsafe_revoked_tx_signing"))]
1043         pub fn unsafe_get_latest_holder_commitment_txn<L: Deref>(&self, logger: &L) -> Vec<Transaction>
1044         where L::Target: Logger {
1045                 self.inner.lock().unwrap().unsafe_get_latest_holder_commitment_txn(logger)
1046         }
1047
1048         /// Processes transactions in a newly connected block, which may result in any of the following:
1049         /// - update the monitor's state against resolved HTLCs
1050         /// - punish the counterparty in the case of seeing a revoked commitment transaction
1051         /// - force close the channel and claim/timeout incoming/outgoing HTLCs if near expiration
1052         /// - detect settled outputs for later spending
1053         /// - schedule and bump any in-flight claims
1054         ///
1055         /// Returns any new outputs to watch from `txdata`; after called, these are also included in
1056         /// [`get_outputs_to_watch`].
1057         ///
1058         /// [`get_outputs_to_watch`]: #method.get_outputs_to_watch
1059         pub fn block_connected<B: Deref, F: Deref, L: Deref>(
1060                 &self,
1061                 header: &BlockHeader,
1062                 txdata: &TransactionData,
1063                 height: u32,
1064                 broadcaster: B,
1065                 fee_estimator: F,
1066                 logger: L,
1067         ) -> Vec<TransactionOutputs>
1068         where
1069                 B::Target: BroadcasterInterface,
1070                 F::Target: FeeEstimator,
1071                 L::Target: Logger,
1072         {
1073                 self.inner.lock().unwrap().block_connected(
1074                         header, txdata, height, broadcaster, fee_estimator, logger)
1075         }
1076
1077         /// Determines if the disconnected block contained any transactions of interest and updates
1078         /// appropriately.
1079         pub fn block_disconnected<B: Deref, F: Deref, L: Deref>(
1080                 &self,
1081                 header: &BlockHeader,
1082                 height: u32,
1083                 broadcaster: B,
1084                 fee_estimator: F,
1085                 logger: L,
1086         ) where
1087                 B::Target: BroadcasterInterface,
1088                 F::Target: FeeEstimator,
1089                 L::Target: Logger,
1090         {
1091                 self.inner.lock().unwrap().block_disconnected(
1092                         header, height, broadcaster, fee_estimator, logger)
1093         }
1094
1095         /// Processes transactions confirmed in a block with the given header and height, returning new
1096         /// outputs to watch. See [`block_connected`] for details.
1097         ///
1098         /// Used instead of [`block_connected`] by clients that are notified of transactions rather than
1099         /// blocks. See [`chain::Confirm`] for calling expectations.
1100         ///
1101         /// [`block_connected`]: Self::block_connected
1102         pub fn transactions_confirmed<B: Deref, F: Deref, L: Deref>(
1103                 &self,
1104                 header: &BlockHeader,
1105                 txdata: &TransactionData,
1106                 height: u32,
1107                 broadcaster: B,
1108                 fee_estimator: F,
1109                 logger: L,
1110         ) -> Vec<TransactionOutputs>
1111         where
1112                 B::Target: BroadcasterInterface,
1113                 F::Target: FeeEstimator,
1114                 L::Target: Logger,
1115         {
1116                 self.inner.lock().unwrap().transactions_confirmed(
1117                         header, txdata, height, broadcaster, fee_estimator, logger)
1118         }
1119
1120         /// Processes a transaction that was reorganized out of the chain.
1121         ///
1122         /// Used instead of [`block_disconnected`] by clients that are notified of transactions rather
1123         /// than blocks. See [`chain::Confirm`] for calling expectations.
1124         ///
1125         /// [`block_disconnected`]: Self::block_disconnected
1126         pub fn transaction_unconfirmed<B: Deref, F: Deref, L: Deref>(
1127                 &self,
1128                 txid: &Txid,
1129                 broadcaster: B,
1130                 fee_estimator: F,
1131                 logger: L,
1132         ) where
1133                 B::Target: BroadcasterInterface,
1134                 F::Target: FeeEstimator,
1135                 L::Target: Logger,
1136         {
1137                 self.inner.lock().unwrap().transaction_unconfirmed(
1138                         txid, broadcaster, fee_estimator, logger);
1139         }
1140
1141         /// Updates the monitor with the current best chain tip, returning new outputs to watch. See
1142         /// [`block_connected`] for details.
1143         ///
1144         /// Used instead of [`block_connected`] by clients that are notified of transactions rather than
1145         /// blocks. See [`chain::Confirm`] for calling expectations.
1146         ///
1147         /// [`block_connected`]: Self::block_connected
1148         pub fn best_block_updated<B: Deref, F: Deref, L: Deref>(
1149                 &self,
1150                 header: &BlockHeader,
1151                 height: u32,
1152                 broadcaster: B,
1153                 fee_estimator: F,
1154                 logger: L,
1155         ) -> Vec<TransactionOutputs>
1156         where
1157                 B::Target: BroadcasterInterface,
1158                 F::Target: FeeEstimator,
1159                 L::Target: Logger,
1160         {
1161                 self.inner.lock().unwrap().best_block_updated(
1162                         header, height, broadcaster, fee_estimator, logger)
1163         }
1164
1165         /// Returns the set of txids that should be monitored for re-organization out of the chain.
1166         pub fn get_relevant_txids(&self) -> Vec<Txid> {
1167                 let inner = self.inner.lock().unwrap();
1168                 let mut txids: Vec<Txid> = inner.onchain_events_awaiting_threshold_conf
1169                         .iter()
1170                         .map(|entry| entry.txid)
1171                         .chain(inner.onchain_tx_handler.get_relevant_txids().into_iter())
1172                         .collect();
1173                 txids.sort_unstable();
1174                 txids.dedup();
1175                 txids
1176         }
1177 }
1178
1179 impl<Signer: Sign> ChannelMonitorImpl<Signer> {
1180         /// Inserts a revocation secret into this channel monitor. Prunes old preimages if neither
1181         /// needed by holder commitment transactions HTCLs nor by counterparty ones. Unless we haven't already seen
1182         /// counterparty commitment transaction's secret, they are de facto pruned (we can use revocation key).
1183         fn provide_secret(&mut self, idx: u64, secret: [u8; 32]) -> Result<(), MonitorUpdateError> {
1184                 if let Err(()) = self.commitment_secrets.provide_secret(idx, secret) {
1185                         return Err(MonitorUpdateError("Previous secret did not match new one"));
1186                 }
1187
1188                 // Prune HTLCs from the previous counterparty commitment tx so we don't generate failure/fulfill
1189                 // events for now-revoked/fulfilled HTLCs.
1190                 if let Some(txid) = self.prev_counterparty_commitment_txid.take() {
1191                         for &mut (_, ref mut source) in self.counterparty_claimable_outpoints.get_mut(&txid).unwrap() {
1192                                 *source = None;
1193                         }
1194                 }
1195
1196                 if !self.payment_preimages.is_empty() {
1197                         let cur_holder_signed_commitment_tx = &self.current_holder_commitment_tx;
1198                         let prev_holder_signed_commitment_tx = self.prev_holder_signed_commitment_tx.as_ref();
1199                         let min_idx = self.get_min_seen_secret();
1200                         let counterparty_hash_commitment_number = &mut self.counterparty_hash_commitment_number;
1201
1202                         self.payment_preimages.retain(|&k, _| {
1203                                 for &(ref htlc, _, _) in cur_holder_signed_commitment_tx.htlc_outputs.iter() {
1204                                         if k == htlc.payment_hash {
1205                                                 return true
1206                                         }
1207                                 }
1208                                 if let Some(prev_holder_commitment_tx) = prev_holder_signed_commitment_tx {
1209                                         for &(ref htlc, _, _) in prev_holder_commitment_tx.htlc_outputs.iter() {
1210                                                 if k == htlc.payment_hash {
1211                                                         return true
1212                                                 }
1213                                         }
1214                                 }
1215                                 let contains = if let Some(cn) = counterparty_hash_commitment_number.get(&k) {
1216                                         if *cn < min_idx {
1217                                                 return true
1218                                         }
1219                                         true
1220                                 } else { false };
1221                                 if contains {
1222                                         counterparty_hash_commitment_number.remove(&k);
1223                                 }
1224                                 false
1225                         });
1226                 }
1227
1228                 Ok(())
1229         }
1230
1231         pub(crate) fn provide_latest_counterparty_commitment_tx<L: Deref>(&mut self, txid: Txid, htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)>, commitment_number: u64, their_revocation_point: PublicKey, logger: &L) where L::Target: Logger {
1232                 // TODO: Encrypt the htlc_outputs data with the single-hash of the commitment transaction
1233                 // so that a remote monitor doesn't learn anything unless there is a malicious close.
1234                 // (only maybe, sadly we cant do the same for local info, as we need to be aware of
1235                 // timeouts)
1236                 for &(ref htlc, _) in &htlc_outputs {
1237                         self.counterparty_hash_commitment_number.insert(htlc.payment_hash, commitment_number);
1238                 }
1239
1240                 log_trace!(logger, "Tracking new counterparty commitment transaction with txid {} at commitment number {} with {} HTLC outputs", txid, commitment_number, htlc_outputs.len());
1241                 self.prev_counterparty_commitment_txid = self.current_counterparty_commitment_txid.take();
1242                 self.current_counterparty_commitment_txid = Some(txid);
1243                 self.counterparty_claimable_outpoints.insert(txid, htlc_outputs.clone());
1244                 self.current_counterparty_commitment_number = commitment_number;
1245                 //TODO: Merge this into the other per-counterparty-transaction output storage stuff
1246                 match self.their_cur_revocation_points {
1247                         Some(old_points) => {
1248                                 if old_points.0 == commitment_number + 1 {
1249                                         self.their_cur_revocation_points = Some((old_points.0, old_points.1, Some(their_revocation_point)));
1250                                 } else if old_points.0 == commitment_number + 2 {
1251                                         if let Some(old_second_point) = old_points.2 {
1252                                                 self.their_cur_revocation_points = Some((old_points.0 - 1, old_second_point, Some(their_revocation_point)));
1253                                         } else {
1254                                                 self.their_cur_revocation_points = Some((commitment_number, their_revocation_point, None));
1255                                         }
1256                                 } else {
1257                                         self.their_cur_revocation_points = Some((commitment_number, their_revocation_point, None));
1258                                 }
1259                         },
1260                         None => {
1261                                 self.their_cur_revocation_points = Some((commitment_number, their_revocation_point, None));
1262                         }
1263                 }
1264                 let mut htlcs = Vec::with_capacity(htlc_outputs.len());
1265                 for htlc in htlc_outputs {
1266                         if htlc.0.transaction_output_index.is_some() {
1267                                 htlcs.push(htlc.0);
1268                         }
1269                 }
1270                 self.counterparty_tx_cache.per_htlc.insert(txid, htlcs);
1271         }
1272
1273         /// Informs this monitor of the latest holder (ie broadcastable) commitment transaction. The
1274         /// monitor watches for timeouts and may broadcast it if we approach such a timeout. Thus, it
1275         /// is important that any clones of this channel monitor (including remote clones) by kept
1276         /// up-to-date as our holder commitment transaction is updated.
1277         /// Panics if set_on_holder_tx_csv has never been called.
1278         fn provide_latest_holder_commitment_tx(&mut self, holder_commitment_tx: HolderCommitmentTransaction, htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Signature>, Option<HTLCSource>)>) -> Result<(), MonitorUpdateError> {
1279                 // block for Rust 1.34 compat
1280                 let mut new_holder_commitment_tx = {
1281                         let trusted_tx = holder_commitment_tx.trust();
1282                         let txid = trusted_tx.txid();
1283                         let tx_keys = trusted_tx.keys();
1284                         self.current_holder_commitment_number = trusted_tx.commitment_number();
1285                         HolderSignedTx {
1286                                 txid,
1287                                 revocation_key: tx_keys.revocation_key,
1288                                 a_htlc_key: tx_keys.broadcaster_htlc_key,
1289                                 b_htlc_key: tx_keys.countersignatory_htlc_key,
1290                                 delayed_payment_key: tx_keys.broadcaster_delayed_payment_key,
1291                                 per_commitment_point: tx_keys.per_commitment_point,
1292                                 feerate_per_kw: trusted_tx.feerate_per_kw(),
1293                                 htlc_outputs,
1294                         }
1295                 };
1296                 self.onchain_tx_handler.provide_latest_holder_tx(holder_commitment_tx);
1297                 mem::swap(&mut new_holder_commitment_tx, &mut self.current_holder_commitment_tx);
1298                 self.prev_holder_signed_commitment_tx = Some(new_holder_commitment_tx);
1299                 if self.holder_tx_signed {
1300                         return Err(MonitorUpdateError("Latest holder commitment signed has already been signed, update is rejected"));
1301                 }
1302                 Ok(())
1303         }
1304
1305         /// Provides a payment_hash->payment_preimage mapping. Will be automatically pruned when all
1306         /// commitment_tx_infos which contain the payment hash have been revoked.
1307         fn provide_payment_preimage<B: Deref, F: Deref, L: Deref>(&mut self, payment_hash: &PaymentHash, payment_preimage: &PaymentPreimage, broadcaster: &B, fee_estimator: &F, logger: &L)
1308         where B::Target: BroadcasterInterface,
1309                     F::Target: FeeEstimator,
1310                     L::Target: Logger,
1311         {
1312                 self.payment_preimages.insert(payment_hash.clone(), payment_preimage.clone());
1313
1314                 // If the channel is force closed, try to claim the output from this preimage.
1315                 // First check if a counterparty commitment transaction has been broadcasted:
1316                 macro_rules! claim_htlcs {
1317                         ($commitment_number: expr, $txid: expr) => {
1318                                 let htlc_claim_reqs = self.get_counterparty_htlc_output_claim_reqs($commitment_number, $txid, None);
1319                                 self.onchain_tx_handler.update_claims_view(&Vec::new(), htlc_claim_reqs, self.best_block.height(), broadcaster, fee_estimator, logger);
1320                         }
1321                 }
1322                 if let Some(txid) = self.current_counterparty_commitment_txid {
1323                         if let Some(commitment_number) = self.counterparty_commitment_txn_on_chain.get(&txid) {
1324                                 claim_htlcs!(*commitment_number, txid);
1325                                 return;
1326                         }
1327                 }
1328                 if let Some(txid) = self.prev_counterparty_commitment_txid {
1329                         if let Some(commitment_number) = self.counterparty_commitment_txn_on_chain.get(&txid) {
1330                                 claim_htlcs!(*commitment_number, txid);
1331                                 return;
1332                         }
1333                 }
1334
1335                 // Then if a holder commitment transaction has been seen on-chain, broadcast transactions
1336                 // claiming the HTLC output from each of the holder commitment transactions.
1337                 // Note that we can't just use `self.holder_tx_signed`, because that only covers the case where
1338                 // *we* sign a holder commitment transaction, not when e.g. a watchtower broadcasts one of our
1339                 // holder commitment transactions.
1340                 if self.broadcasted_holder_revokable_script.is_some() {
1341                         let (claim_reqs, _) = self.get_broadcasted_holder_claims(&self.current_holder_commitment_tx, 0);
1342                         self.onchain_tx_handler.update_claims_view(&Vec::new(), claim_reqs, self.best_block.height(), broadcaster, fee_estimator, logger);
1343                         if let Some(ref tx) = self.prev_holder_signed_commitment_tx {
1344                                 let (claim_reqs, _) = self.get_broadcasted_holder_claims(&tx, 0);
1345                                 self.onchain_tx_handler.update_claims_view(&Vec::new(), claim_reqs, self.best_block.height(), broadcaster, fee_estimator, logger);
1346                         }
1347                 }
1348         }
1349
1350         pub(crate) fn broadcast_latest_holder_commitment_txn<B: Deref, L: Deref>(&mut self, broadcaster: &B, logger: &L)
1351                 where B::Target: BroadcasterInterface,
1352                                         L::Target: Logger,
1353         {
1354                 for tx in self.get_latest_holder_commitment_txn(logger).iter() {
1355                         log_info!(logger, "Broadcasting local {}", log_tx!(tx));
1356                         broadcaster.broadcast_transaction(tx);
1357                 }
1358                 self.pending_monitor_events.push(MonitorEvent::CommitmentTxBroadcasted(self.funding_info.0));
1359         }
1360
1361         pub fn update_monitor<B: Deref, F: Deref, L: Deref>(&mut self, updates: &ChannelMonitorUpdate, broadcaster: &B, fee_estimator: &F, logger: &L) -> Result<(), MonitorUpdateError>
1362         where B::Target: BroadcasterInterface,
1363                     F::Target: FeeEstimator,
1364                     L::Target: Logger,
1365         {
1366                 // ChannelMonitor updates may be applied after force close if we receive a
1367                 // preimage for a broadcasted commitment transaction HTLC output that we'd
1368                 // like to claim on-chain. If this is the case, we no longer have guaranteed
1369                 // access to the monitor's update ID, so we use a sentinel value instead.
1370                 if updates.update_id == CLOSED_CHANNEL_UPDATE_ID {
1371                         match updates.updates[0] {
1372                                 ChannelMonitorUpdateStep::PaymentPreimage { .. } => {},
1373                                 _ => panic!("Attempted to apply post-force-close ChannelMonitorUpdate that wasn't providing a payment preimage"),
1374                         }
1375                         assert_eq!(updates.updates.len(), 1);
1376                 } else if self.latest_update_id + 1 != updates.update_id {
1377                         panic!("Attempted to apply ChannelMonitorUpdates out of order, check the update_id before passing an update to update_monitor!");
1378                 }
1379                 for update in updates.updates.iter() {
1380                         match update {
1381                                 ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo { commitment_tx, htlc_outputs } => {
1382                                         log_trace!(logger, "Updating ChannelMonitor with latest holder commitment transaction info");
1383                                         if self.lockdown_from_offchain { panic!(); }
1384                                         self.provide_latest_holder_commitment_tx(commitment_tx.clone(), htlc_outputs.clone())?
1385                                 }
1386                                 ChannelMonitorUpdateStep::LatestCounterpartyCommitmentTXInfo { commitment_txid, htlc_outputs, commitment_number, their_revocation_point } => {
1387                                         log_trace!(logger, "Updating ChannelMonitor with latest counterparty commitment transaction info");
1388                                         self.provide_latest_counterparty_commitment_tx(*commitment_txid, htlc_outputs.clone(), *commitment_number, *their_revocation_point, logger)
1389                                 },
1390                                 ChannelMonitorUpdateStep::PaymentPreimage { payment_preimage } => {
1391                                         log_trace!(logger, "Updating ChannelMonitor with payment preimage");
1392                                         self.provide_payment_preimage(&PaymentHash(Sha256::hash(&payment_preimage.0[..]).into_inner()), &payment_preimage, broadcaster, fee_estimator, logger)
1393                                 },
1394                                 ChannelMonitorUpdateStep::CommitmentSecret { idx, secret } => {
1395                                         log_trace!(logger, "Updating ChannelMonitor with commitment secret");
1396                                         self.provide_secret(*idx, *secret)?
1397                                 },
1398                                 ChannelMonitorUpdateStep::ChannelForceClosed { should_broadcast } => {
1399                                         log_trace!(logger, "Updating ChannelMonitor: channel force closed, should broadcast: {}", should_broadcast);
1400                                         self.lockdown_from_offchain = true;
1401                                         if *should_broadcast {
1402                                                 self.broadcast_latest_holder_commitment_txn(broadcaster, logger);
1403                                         } else if !self.holder_tx_signed {
1404                                                 log_error!(logger, "You have a toxic holder commitment transaction avaible in channel monitor, read comment in ChannelMonitor::get_latest_holder_commitment_txn to be informed of manual action to take");
1405                                         } else {
1406                                                 // If we generated a MonitorEvent::CommitmentTxBroadcasted, the ChannelManager
1407                                                 // will still give us a ChannelForceClosed event with !should_broadcast, but we
1408                                                 // shouldn't print the scary warning above.
1409                                                 log_info!(logger, "Channel off-chain state closed after we broadcasted our latest commitment transaction.");
1410                                         }
1411                                 }
1412                         }
1413                 }
1414                 self.latest_update_id = updates.update_id;
1415                 Ok(())
1416         }
1417
1418         pub fn get_latest_update_id(&self) -> u64 {
1419                 self.latest_update_id
1420         }
1421
1422         pub fn get_funding_txo(&self) -> &(OutPoint, Script) {
1423                 &self.funding_info
1424         }
1425
1426         pub fn get_outputs_to_watch(&self) -> &HashMap<Txid, Vec<(u32, Script)>> {
1427                 // If we've detected a counterparty commitment tx on chain, we must include it in the set
1428                 // of outputs to watch for spends of, otherwise we're likely to lose user funds. Because
1429                 // its trivial to do, double-check that here.
1430                 for (txid, _) in self.counterparty_commitment_txn_on_chain.iter() {
1431                         self.outputs_to_watch.get(txid).expect("Counterparty commitment txn which have been broadcast should have outputs registered");
1432                 }
1433                 &self.outputs_to_watch
1434         }
1435
1436         pub fn get_and_clear_pending_monitor_events(&mut self) -> Vec<MonitorEvent> {
1437                 let mut ret = Vec::new();
1438                 mem::swap(&mut ret, &mut self.pending_monitor_events);
1439                 ret
1440         }
1441
1442         pub fn get_and_clear_pending_events(&mut self) -> Vec<Event> {
1443                 let mut ret = Vec::new();
1444                 mem::swap(&mut ret, &mut self.pending_events);
1445                 ret
1446         }
1447
1448         /// Can only fail if idx is < get_min_seen_secret
1449         fn get_secret(&self, idx: u64) -> Option<[u8; 32]> {
1450                 self.commitment_secrets.get_secret(idx)
1451         }
1452
1453         pub(crate) fn get_min_seen_secret(&self) -> u64 {
1454                 self.commitment_secrets.get_min_seen_secret()
1455         }
1456
1457         pub(crate) fn get_cur_counterparty_commitment_number(&self) -> u64 {
1458                 self.current_counterparty_commitment_number
1459         }
1460
1461         pub(crate) fn get_cur_holder_commitment_number(&self) -> u64 {
1462                 self.current_holder_commitment_number
1463         }
1464
1465         /// Attempts to claim a counterparty commitment transaction's outputs using the revocation key and
1466         /// data in counterparty_claimable_outpoints. Will directly claim any HTLC outputs which expire at a
1467         /// height > height + CLTV_SHARED_CLAIM_BUFFER. In any case, will install monitoring for
1468         /// HTLC-Success/HTLC-Timeout transactions.
1469         /// Return updates for HTLC pending in the channel and failed automatically by the broadcast of
1470         /// revoked counterparty commitment tx
1471         fn check_spend_counterparty_transaction<L: Deref>(&mut self, tx: &Transaction, height: u32, logger: &L) -> (Vec<PackageTemplate>, TransactionOutputs) where L::Target: Logger {
1472                 // Most secp and related errors trying to create keys means we have no hope of constructing
1473                 // a spend transaction...so we return no transactions to broadcast
1474                 let mut claimable_outpoints = Vec::new();
1475                 let mut watch_outputs = Vec::new();
1476
1477                 let commitment_txid = tx.txid(); //TODO: This is gonna be a performance bottleneck for watchtowers!
1478                 let per_commitment_option = self.counterparty_claimable_outpoints.get(&commitment_txid);
1479
1480                 macro_rules! ignore_error {
1481                         ( $thing : expr ) => {
1482                                 match $thing {
1483                                         Ok(a) => a,
1484                                         Err(_) => return (claimable_outpoints, (commitment_txid, watch_outputs))
1485                                 }
1486                         };
1487                 }
1488
1489                 let commitment_number = 0xffffffffffff - ((((tx.input[0].sequence as u64 & 0xffffff) << 3*8) | (tx.lock_time as u64 & 0xffffff)) ^ self.commitment_transaction_number_obscure_factor);
1490                 if commitment_number >= self.get_min_seen_secret() {
1491                         let secret = self.get_secret(commitment_number).unwrap();
1492                         let per_commitment_key = ignore_error!(SecretKey::from_slice(&secret));
1493                         let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key);
1494                         let revocation_pubkey = ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &self.holder_revocation_basepoint));
1495                         let delayed_key = ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key), &self.counterparty_tx_cache.counterparty_delayed_payment_base_key));
1496
1497                         let revokeable_redeemscript = chan_utils::get_revokeable_redeemscript(&revocation_pubkey, self.counterparty_tx_cache.on_counterparty_tx_csv, &delayed_key);
1498                         let revokeable_p2wsh = revokeable_redeemscript.to_v0_p2wsh();
1499
1500                         // First, process non-htlc outputs (to_holder & to_counterparty)
1501                         for (idx, outp) in tx.output.iter().enumerate() {
1502                                 if outp.script_pubkey == revokeable_p2wsh {
1503                                         let revk_outp = RevokedOutput::build(per_commitment_point, self.counterparty_tx_cache.counterparty_delayed_payment_base_key, self.counterparty_tx_cache.counterparty_htlc_base_key, per_commitment_key, outp.value, self.counterparty_tx_cache.on_counterparty_tx_csv);
1504                                         let justice_package = PackageTemplate::build_package(commitment_txid, idx as u32, PackageSolvingData::RevokedOutput(revk_outp), height + self.counterparty_tx_cache.on_counterparty_tx_csv as u32, true, height);
1505                                         claimable_outpoints.push(justice_package);
1506                                 }
1507                         }
1508
1509                         // Then, try to find revoked htlc outputs
1510                         if let Some(ref per_commitment_data) = per_commitment_option {
1511                                 for (_, &(ref htlc, _)) in per_commitment_data.iter().enumerate() {
1512                                         if let Some(transaction_output_index) = htlc.transaction_output_index {
1513                                                 if transaction_output_index as usize >= tx.output.len() ||
1514                                                                 tx.output[transaction_output_index as usize].value != htlc.amount_msat / 1000 {
1515                                                         return (claimable_outpoints, (commitment_txid, watch_outputs)); // Corrupted per_commitment_data, fuck this user
1516                                                 }
1517                                                 let revk_htlc_outp = RevokedHTLCOutput::build(per_commitment_point, self.counterparty_tx_cache.counterparty_delayed_payment_base_key, self.counterparty_tx_cache.counterparty_htlc_base_key, per_commitment_key, htlc.amount_msat / 1000, htlc.clone());
1518                                                 let justice_package = PackageTemplate::build_package(commitment_txid, transaction_output_index, PackageSolvingData::RevokedHTLCOutput(revk_htlc_outp), htlc.cltv_expiry, true, height);
1519                                                 claimable_outpoints.push(justice_package);
1520                                         }
1521                                 }
1522                         }
1523
1524                         // Last, track onchain revoked commitment transaction and fail backward outgoing HTLCs as payment path is broken
1525                         if !claimable_outpoints.is_empty() || per_commitment_option.is_some() { // ie we're confident this is actually ours
1526                                 // We're definitely a counterparty commitment transaction!
1527                                 log_trace!(logger, "Got broadcast of revoked counterparty commitment transaction, going to generate general spend tx with {} inputs", claimable_outpoints.len());
1528                                 for (idx, outp) in tx.output.iter().enumerate() {
1529                                         watch_outputs.push((idx as u32, outp.clone()));
1530                                 }
1531                                 self.counterparty_commitment_txn_on_chain.insert(commitment_txid, commitment_number);
1532
1533                                 macro_rules! check_htlc_fails {
1534                                         ($txid: expr, $commitment_tx: expr) => {
1535                                                 if let Some(ref outpoints) = self.counterparty_claimable_outpoints.get($txid) {
1536                                                         for &(ref htlc, ref source_option) in outpoints.iter() {
1537                                                                 if let &Some(ref source) = source_option {
1538                                                                         self.onchain_events_awaiting_threshold_conf.retain(|ref entry| {
1539                                                                                 if entry.height != height { return true; }
1540                                                                                 match entry.event {
1541                                                                                         OnchainEvent::HTLCUpdate { source: ref update_source, .. } => {
1542                                                                                                 *update_source != **source
1543                                                                                         },
1544                                                                                         _ => true,
1545                                                                                 }
1546                                                                         });
1547                                                                         let entry = OnchainEventEntry {
1548                                                                                 txid: *$txid,
1549                                                                                 height,
1550                                                                                 event: OnchainEvent::HTLCUpdate {
1551                                                                                         source: (**source).clone(),
1552                                                                                         payment_hash: htlc.payment_hash.clone(),
1553                                                                                 },
1554                                                                         };
1555                                                                         log_info!(logger, "Failing HTLC with payment_hash {} from {} counterparty commitment tx due to broadcast of revoked counterparty commitment transaction, waiting for confirmation (at height {})", log_bytes!(htlc.payment_hash.0), $commitment_tx, entry.confirmation_threshold());
1556                                                                         self.onchain_events_awaiting_threshold_conf.push(entry);
1557                                                                 }
1558                                                         }
1559                                                 }
1560                                         }
1561                                 }
1562                                 if let Some(ref txid) = self.current_counterparty_commitment_txid {
1563                                         check_htlc_fails!(txid, "current");
1564                                 }
1565                                 if let Some(ref txid) = self.prev_counterparty_commitment_txid {
1566                                         check_htlc_fails!(txid, "counterparty");
1567                                 }
1568                                 // No need to check holder commitment txn, symmetric HTLCSource must be present as per-htlc data on counterparty commitment tx
1569                         }
1570                 } else if let Some(per_commitment_data) = per_commitment_option {
1571                         // While this isn't useful yet, there is a potential race where if a counterparty
1572                         // revokes a state at the same time as the commitment transaction for that state is
1573                         // confirmed, and the watchtower receives the block before the user, the user could
1574                         // upload a new ChannelMonitor with the revocation secret but the watchtower has
1575                         // already processed the block, resulting in the counterparty_commitment_txn_on_chain entry
1576                         // not being generated by the above conditional. Thus, to be safe, we go ahead and
1577                         // insert it here.
1578                         for (idx, outp) in tx.output.iter().enumerate() {
1579                                 watch_outputs.push((idx as u32, outp.clone()));
1580                         }
1581                         self.counterparty_commitment_txn_on_chain.insert(commitment_txid, commitment_number);
1582
1583                         log_trace!(logger, "Got broadcast of non-revoked counterparty commitment transaction {}", commitment_txid);
1584
1585                         macro_rules! check_htlc_fails {
1586                                 ($txid: expr, $commitment_tx: expr, $id: tt) => {
1587                                         if let Some(ref latest_outpoints) = self.counterparty_claimable_outpoints.get($txid) {
1588                                                 $id: for &(ref htlc, ref source_option) in latest_outpoints.iter() {
1589                                                         if let &Some(ref source) = source_option {
1590                                                                 // Check if the HTLC is present in the commitment transaction that was
1591                                                                 // broadcast, but not if it was below the dust limit, which we should
1592                                                                 // fail backwards immediately as there is no way for us to learn the
1593                                                                 // payment_preimage.
1594                                                                 // Note that if the dust limit were allowed to change between
1595                                                                 // commitment transactions we'd want to be check whether *any*
1596                                                                 // broadcastable commitment transaction has the HTLC in it, but it
1597                                                                 // cannot currently change after channel initialization, so we don't
1598                                                                 // need to here.
1599                                                                 for &(ref broadcast_htlc, ref broadcast_source) in per_commitment_data.iter() {
1600                                                                         if broadcast_htlc.transaction_output_index.is_some() && Some(source) == broadcast_source.as_ref() {
1601                                                                                 continue $id;
1602                                                                         }
1603                                                                 }
1604                                                                 log_trace!(logger, "Failing HTLC with payment_hash {} from {} counterparty commitment tx due to broadcast of counterparty commitment transaction", log_bytes!(htlc.payment_hash.0), $commitment_tx);
1605                                                                 self.onchain_events_awaiting_threshold_conf.retain(|ref entry| {
1606                                                                         if entry.height != height { return true; }
1607                                                                         match entry.event {
1608                                                                                 OnchainEvent::HTLCUpdate { source: ref update_source, .. } => {
1609                                                                                         *update_source != **source
1610                                                                                 },
1611                                                                                 _ => true,
1612                                                                         }
1613                                                                 });
1614                                                                 self.onchain_events_awaiting_threshold_conf.push(OnchainEventEntry {
1615                                                                         txid: *$txid,
1616                                                                         height,
1617                                                                         event: OnchainEvent::HTLCUpdate {
1618                                                                                 source: (**source).clone(),
1619                                                                                 payment_hash: htlc.payment_hash.clone(),
1620                                                                         },
1621                                                                 });
1622                                                         }
1623                                                 }
1624                                         }
1625                                 }
1626                         }
1627                         if let Some(ref txid) = self.current_counterparty_commitment_txid {
1628                                 check_htlc_fails!(txid, "current", 'current_loop);
1629                         }
1630                         if let Some(ref txid) = self.prev_counterparty_commitment_txid {
1631                                 check_htlc_fails!(txid, "previous", 'prev_loop);
1632                         }
1633
1634                         let htlc_claim_reqs = self.get_counterparty_htlc_output_claim_reqs(commitment_number, commitment_txid, Some(tx));
1635                         for req in htlc_claim_reqs {
1636                                 claimable_outpoints.push(req);
1637                         }
1638
1639                 }
1640                 (claimable_outpoints, (commitment_txid, watch_outputs))
1641         }
1642
1643         fn get_counterparty_htlc_output_claim_reqs(&self, commitment_number: u64, commitment_txid: Txid, tx: Option<&Transaction>) -> Vec<PackageTemplate> {
1644                 let mut claimable_outpoints = Vec::new();
1645                 if let Some(htlc_outputs) = self.counterparty_claimable_outpoints.get(&commitment_txid) {
1646                         if let Some(revocation_points) = self.their_cur_revocation_points {
1647                                 let revocation_point_option =
1648                                         // If the counterparty commitment tx is the latest valid state, use their latest
1649                                         // per-commitment point
1650                                         if revocation_points.0 == commitment_number { Some(&revocation_points.1) }
1651                                         else if let Some(point) = revocation_points.2.as_ref() {
1652                                                 // If counterparty commitment tx is the state previous to the latest valid state, use
1653                                                 // their previous per-commitment point (non-atomicity of revocation means it's valid for
1654                                                 // them to temporarily have two valid commitment txns from our viewpoint)
1655                                                 if revocation_points.0 == commitment_number + 1 { Some(point) } else { None }
1656                                         } else { None };
1657                                 if let Some(revocation_point) = revocation_point_option {
1658                                         for (_, &(ref htlc, _)) in htlc_outputs.iter().enumerate() {
1659                                                 if let Some(transaction_output_index) = htlc.transaction_output_index {
1660                                                         if let Some(transaction) = tx {
1661                                                                 if transaction_output_index as usize >= transaction.output.len() ||
1662                                                                         transaction.output[transaction_output_index as usize].value != htlc.amount_msat / 1000 {
1663                                                                                 return claimable_outpoints; // Corrupted per_commitment_data, fuck this user
1664                                                                         }
1665                                                         }
1666                                                         let preimage = if htlc.offered { if let Some(p) = self.payment_preimages.get(&htlc.payment_hash) { Some(*p) } else { None } } else { None };
1667                                                         if preimage.is_some() || !htlc.offered {
1668                                                                 let counterparty_htlc_outp = if htlc.offered { PackageSolvingData::CounterpartyOfferedHTLCOutput(CounterpartyOfferedHTLCOutput::build(*revocation_point, self.counterparty_tx_cache.counterparty_delayed_payment_base_key, self.counterparty_tx_cache.counterparty_htlc_base_key, preimage.unwrap(), htlc.clone())) } else { PackageSolvingData::CounterpartyReceivedHTLCOutput(CounterpartyReceivedHTLCOutput::build(*revocation_point, self.counterparty_tx_cache.counterparty_delayed_payment_base_key, self.counterparty_tx_cache.counterparty_htlc_base_key, htlc.clone())) };
1669                                                                 let aggregation = if !htlc.offered { false } else { true };
1670                                                                 let counterparty_package = PackageTemplate::build_package(commitment_txid, transaction_output_index, counterparty_htlc_outp, htlc.cltv_expiry,aggregation, 0);
1671                                                                 claimable_outpoints.push(counterparty_package);
1672                                                         }
1673                                                 }
1674                                         }
1675                                 }
1676                         }
1677                 }
1678                 claimable_outpoints
1679         }
1680
1681         /// Attempts to claim a counterparty HTLC-Success/HTLC-Timeout's outputs using the revocation key
1682         fn check_spend_counterparty_htlc<L: Deref>(&mut self, tx: &Transaction, commitment_number: u64, height: u32, logger: &L) -> (Vec<PackageTemplate>, Option<TransactionOutputs>) where L::Target: Logger {
1683                 let htlc_txid = tx.txid();
1684                 if tx.input.len() != 1 || tx.output.len() != 1 || tx.input[0].witness.len() != 5 {
1685                         return (Vec::new(), None)
1686                 }
1687
1688                 macro_rules! ignore_error {
1689                         ( $thing : expr ) => {
1690                                 match $thing {
1691                                         Ok(a) => a,
1692                                         Err(_) => return (Vec::new(), None)
1693                                 }
1694                         };
1695                 }
1696
1697                 let secret = if let Some(secret) = self.get_secret(commitment_number) { secret } else { return (Vec::new(), None); };
1698                 let per_commitment_key = ignore_error!(SecretKey::from_slice(&secret));
1699                 let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key);
1700
1701                 log_trace!(logger, "Counterparty HTLC broadcast {}:{}", htlc_txid, 0);
1702                 let revk_outp = RevokedOutput::build(per_commitment_point, self.counterparty_tx_cache.counterparty_delayed_payment_base_key, self.counterparty_tx_cache.counterparty_htlc_base_key, per_commitment_key, tx.output[0].value, self.counterparty_tx_cache.on_counterparty_tx_csv);
1703                 let justice_package = PackageTemplate::build_package(htlc_txid, 0, PackageSolvingData::RevokedOutput(revk_outp), height + self.counterparty_tx_cache.on_counterparty_tx_csv as u32, true, height);
1704                 let claimable_outpoints = vec!(justice_package);
1705                 let outputs = vec![(0, tx.output[0].clone())];
1706                 (claimable_outpoints, Some((htlc_txid, outputs)))
1707         }
1708
1709         // Returns (1) `PackageTemplate`s that can be given to the OnChainTxHandler, so that the handler can
1710         // broadcast transactions claiming holder HTLC commitment outputs and (2) a holder revokable
1711         // script so we can detect whether a holder transaction has been seen on-chain.
1712         fn get_broadcasted_holder_claims(&self, holder_tx: &HolderSignedTx, height: u32) -> (Vec<PackageTemplate>, Option<(Script, PublicKey, PublicKey)>) {
1713                 let mut claim_requests = Vec::with_capacity(holder_tx.htlc_outputs.len());
1714
1715                 let redeemscript = chan_utils::get_revokeable_redeemscript(&holder_tx.revocation_key, self.on_holder_tx_csv, &holder_tx.delayed_payment_key);
1716                 let broadcasted_holder_revokable_script = Some((redeemscript.to_v0_p2wsh(), holder_tx.per_commitment_point.clone(), holder_tx.revocation_key.clone()));
1717
1718                 for &(ref htlc, _, _) in holder_tx.htlc_outputs.iter() {
1719                         if let Some(transaction_output_index) = htlc.transaction_output_index {
1720                                 let htlc_output = if htlc.offered {
1721                                                 HolderHTLCOutput::build_offered(htlc.amount_msat, htlc.cltv_expiry)
1722                                         } else {
1723                                                 let payment_preimage = if let Some(preimage) = self.payment_preimages.get(&htlc.payment_hash) {
1724                                                         preimage.clone()
1725                                                 } else {
1726                                                         // We can't build an HTLC-Success transaction without the preimage
1727                                                         continue;
1728                                                 };
1729                                                 HolderHTLCOutput::build_accepted(payment_preimage, htlc.amount_msat)
1730                                         };
1731                                 let htlc_package = PackageTemplate::build_package(holder_tx.txid, transaction_output_index, PackageSolvingData::HolderHTLCOutput(htlc_output), height, false, height);
1732                                 claim_requests.push(htlc_package);
1733                         }
1734                 }
1735
1736                 (claim_requests, broadcasted_holder_revokable_script)
1737         }
1738
1739         // Returns holder HTLC outputs to watch and react to in case of spending.
1740         fn get_broadcasted_holder_watch_outputs(&self, holder_tx: &HolderSignedTx, commitment_tx: &Transaction) -> Vec<(u32, TxOut)> {
1741                 let mut watch_outputs = Vec::with_capacity(holder_tx.htlc_outputs.len());
1742                 for &(ref htlc, _, _) in holder_tx.htlc_outputs.iter() {
1743                         if let Some(transaction_output_index) = htlc.transaction_output_index {
1744                                 watch_outputs.push((transaction_output_index, commitment_tx.output[transaction_output_index as usize].clone()));
1745                         }
1746                 }
1747                 watch_outputs
1748         }
1749
1750         /// Attempts to claim any claimable HTLCs in a commitment transaction which was not (yet)
1751         /// revoked using data in holder_claimable_outpoints.
1752         /// Should not be used if check_spend_revoked_transaction succeeds.
1753         fn check_spend_holder_transaction<L: Deref>(&mut self, tx: &Transaction, height: u32, logger: &L) -> (Vec<PackageTemplate>, TransactionOutputs) where L::Target: Logger {
1754                 let commitment_txid = tx.txid();
1755                 let mut claim_requests = Vec::new();
1756                 let mut watch_outputs = Vec::new();
1757
1758                 macro_rules! wait_threshold_conf {
1759                         ($source: expr, $commitment_tx: expr, $payment_hash: expr) => {
1760                                 self.onchain_events_awaiting_threshold_conf.retain(|ref entry| {
1761                                         if entry.height != height { return true; }
1762                                         match entry.event {
1763                                                 OnchainEvent::HTLCUpdate { source: ref update_source, .. } => {
1764                                                         *update_source != $source
1765                                                 },
1766                                                 _ => true,
1767                                         }
1768                                 });
1769                                 let entry = OnchainEventEntry {
1770                                         txid: commitment_txid,
1771                                         height,
1772                                         event: OnchainEvent::HTLCUpdate { source: $source, payment_hash: $payment_hash },
1773                                 };
1774                                 log_trace!(logger, "Failing HTLC with payment_hash {} from {} holder commitment tx due to broadcast of transaction, waiting confirmation (at height{})", log_bytes!($payment_hash.0), $commitment_tx, entry.confirmation_threshold());
1775                                 self.onchain_events_awaiting_threshold_conf.push(entry);
1776                         }
1777                 }
1778
1779                 macro_rules! append_onchain_update {
1780                         ($updates: expr, $to_watch: expr) => {
1781                                 claim_requests = $updates.0;
1782                                 self.broadcasted_holder_revokable_script = $updates.1;
1783                                 watch_outputs.append(&mut $to_watch);
1784                         }
1785                 }
1786
1787                 // HTLCs set may differ between last and previous holder commitment txn, in case of one them hitting chain, ensure we cancel all HTLCs backward
1788                 let mut is_holder_tx = false;
1789
1790                 if self.current_holder_commitment_tx.txid == commitment_txid {
1791                         is_holder_tx = true;
1792                         log_trace!(logger, "Got latest holder commitment tx broadcast, searching for available HTLCs to claim");
1793                         let res = self.get_broadcasted_holder_claims(&self.current_holder_commitment_tx, height);
1794                         let mut to_watch = self.get_broadcasted_holder_watch_outputs(&self.current_holder_commitment_tx, tx);
1795                         append_onchain_update!(res, to_watch);
1796                 } else if let &Some(ref holder_tx) = &self.prev_holder_signed_commitment_tx {
1797                         if holder_tx.txid == commitment_txid {
1798                                 is_holder_tx = true;
1799                                 log_trace!(logger, "Got previous holder commitment tx broadcast, searching for available HTLCs to claim");
1800                                 let res = self.get_broadcasted_holder_claims(holder_tx, height);
1801                                 let mut to_watch = self.get_broadcasted_holder_watch_outputs(holder_tx, tx);
1802                                 append_onchain_update!(res, to_watch);
1803                         }
1804                 }
1805
1806                 macro_rules! fail_dust_htlcs_after_threshold_conf {
1807                         ($holder_tx: expr) => {
1808                                 for &(ref htlc, _, ref source) in &$holder_tx.htlc_outputs {
1809                                         if htlc.transaction_output_index.is_none() {
1810                                                 if let &Some(ref source) = source {
1811                                                         wait_threshold_conf!(source.clone(), "lastest", htlc.payment_hash.clone());
1812                                                 }
1813                                         }
1814                                 }
1815                         }
1816                 }
1817
1818                 if is_holder_tx {
1819                         fail_dust_htlcs_after_threshold_conf!(self.current_holder_commitment_tx);
1820                         if let &Some(ref holder_tx) = &self.prev_holder_signed_commitment_tx {
1821                                 fail_dust_htlcs_after_threshold_conf!(holder_tx);
1822                         }
1823                 }
1824
1825                 (claim_requests, (commitment_txid, watch_outputs))
1826         }
1827
1828         pub fn get_latest_holder_commitment_txn<L: Deref>(&mut self, logger: &L) -> Vec<Transaction> where L::Target: Logger {
1829                 log_trace!(logger, "Getting signed latest holder commitment transaction!");
1830                 self.holder_tx_signed = true;
1831                 let commitment_tx = self.onchain_tx_handler.get_fully_signed_holder_tx(&self.funding_redeemscript);
1832                 let txid = commitment_tx.txid();
1833                 let mut holder_transactions = vec![commitment_tx];
1834                 for htlc in self.current_holder_commitment_tx.htlc_outputs.iter() {
1835                         if let Some(vout) = htlc.0.transaction_output_index {
1836                                 let preimage = if !htlc.0.offered {
1837                                         if let Some(preimage) = self.payment_preimages.get(&htlc.0.payment_hash) { Some(preimage.clone()) } else {
1838                                                 // We can't build an HTLC-Success transaction without the preimage
1839                                                 continue;
1840                                         }
1841                                 } else if htlc.0.cltv_expiry > self.best_block.height() + 1 {
1842                                         // Don't broadcast HTLC-Timeout transactions immediately as they don't meet the
1843                                         // current locktime requirements on-chain. We will broadcast them in
1844                                         // `block_confirmed` when `would_broadcast_at_height` returns true.
1845                                         // Note that we add + 1 as transactions are broadcastable when they can be
1846                                         // confirmed in the next block.
1847                                         continue;
1848                                 } else { None };
1849                                 if let Some(htlc_tx) = self.onchain_tx_handler.get_fully_signed_htlc_tx(
1850                                         &::bitcoin::OutPoint { txid, vout }, &preimage) {
1851                                         holder_transactions.push(htlc_tx);
1852                                 }
1853                         }
1854                 }
1855                 // We throw away the generated waiting_first_conf data as we aren't (yet) confirmed and we don't actually know what the caller wants to do.
1856                 // The data will be re-generated and tracked in check_spend_holder_transaction if we get a confirmation.
1857                 holder_transactions
1858         }
1859
1860         #[cfg(any(test,feature = "unsafe_revoked_tx_signing"))]
1861         /// Note that this includes possibly-locktimed-in-the-future transactions!
1862         fn unsafe_get_latest_holder_commitment_txn<L: Deref>(&mut self, logger: &L) -> Vec<Transaction> where L::Target: Logger {
1863                 log_trace!(logger, "Getting signed copy of latest holder commitment transaction!");
1864                 let commitment_tx = self.onchain_tx_handler.get_fully_signed_copy_holder_tx(&self.funding_redeemscript);
1865                 let txid = commitment_tx.txid();
1866                 let mut holder_transactions = vec![commitment_tx];
1867                 for htlc in self.current_holder_commitment_tx.htlc_outputs.iter() {
1868                         if let Some(vout) = htlc.0.transaction_output_index {
1869                                 let preimage = if !htlc.0.offered {
1870                                         if let Some(preimage) = self.payment_preimages.get(&htlc.0.payment_hash) { Some(preimage.clone()) } else {
1871                                                 // We can't build an HTLC-Success transaction without the preimage
1872                                                 continue;
1873                                         }
1874                                 } else { None };
1875                                 if let Some(htlc_tx) = self.onchain_tx_handler.unsafe_get_fully_signed_htlc_tx(
1876                                         &::bitcoin::OutPoint { txid, vout }, &preimage) {
1877                                         holder_transactions.push(htlc_tx);
1878                                 }
1879                         }
1880                 }
1881                 holder_transactions
1882         }
1883
1884         pub fn block_connected<B: Deref, F: Deref, L: Deref>(&mut self, header: &BlockHeader, txdata: &TransactionData, height: u32, broadcaster: B, fee_estimator: F, logger: L) -> Vec<TransactionOutputs>
1885                 where B::Target: BroadcasterInterface,
1886                       F::Target: FeeEstimator,
1887                                         L::Target: Logger,
1888         {
1889                 let block_hash = header.block_hash();
1890                 log_trace!(logger, "New best block {} at height {}", block_hash, height);
1891                 self.best_block = BestBlock::new(block_hash, height);
1892
1893                 self.transactions_confirmed(header, txdata, height, broadcaster, fee_estimator, logger)
1894         }
1895
1896         fn best_block_updated<B: Deref, F: Deref, L: Deref>(
1897                 &mut self,
1898                 header: &BlockHeader,
1899                 height: u32,
1900                 broadcaster: B,
1901                 fee_estimator: F,
1902                 logger: L,
1903         ) -> Vec<TransactionOutputs>
1904         where
1905                 B::Target: BroadcasterInterface,
1906                 F::Target: FeeEstimator,
1907                 L::Target: Logger,
1908         {
1909                 let block_hash = header.block_hash();
1910                 log_trace!(logger, "New best block {} at height {}", block_hash, height);
1911
1912                 if height > self.best_block.height() {
1913                         self.best_block = BestBlock::new(block_hash, height);
1914                         self.block_confirmed(height, vec![], vec![], vec![], broadcaster, fee_estimator, logger)
1915                 } else {
1916                         self.best_block = BestBlock::new(block_hash, height);
1917                         self.onchain_events_awaiting_threshold_conf.retain(|ref entry| entry.height <= height);
1918                         self.onchain_tx_handler.block_disconnected(height + 1, broadcaster, fee_estimator, logger);
1919                         Vec::new()
1920                 }
1921         }
1922
1923         fn transactions_confirmed<B: Deref, F: Deref, L: Deref>(
1924                 &mut self,
1925                 header: &BlockHeader,
1926                 txdata: &TransactionData,
1927                 height: u32,
1928                 broadcaster: B,
1929                 fee_estimator: F,
1930                 logger: L,
1931         ) -> Vec<TransactionOutputs>
1932         where
1933                 B::Target: BroadcasterInterface,
1934                 F::Target: FeeEstimator,
1935                 L::Target: Logger,
1936         {
1937                 let txn_matched = self.filter_block(txdata);
1938                 for tx in &txn_matched {
1939                         let mut output_val = 0;
1940                         for out in tx.output.iter() {
1941                                 if out.value > 21_000_000_0000_0000 { panic!("Value-overflowing transaction provided to block connected"); }
1942                                 output_val += out.value;
1943                                 if output_val > 21_000_000_0000_0000 { panic!("Value-overflowing transaction provided to block connected"); }
1944                         }
1945                 }
1946
1947                 let block_hash = header.block_hash();
1948                 log_trace!(logger, "Block {} at height {} connected with {} txn matched", block_hash, height, txn_matched.len());
1949
1950                 let mut watch_outputs = Vec::new();
1951                 let mut claimable_outpoints = Vec::new();
1952                 for tx in &txn_matched {
1953                         if tx.input.len() == 1 {
1954                                 // Assuming our keys were not leaked (in which case we're screwed no matter what),
1955                                 // commitment transactions and HTLC transactions will all only ever have one input,
1956                                 // which is an easy way to filter out any potential non-matching txn for lazy
1957                                 // filters.
1958                                 let prevout = &tx.input[0].previous_output;
1959                                 if prevout.txid == self.funding_info.0.txid && prevout.vout == self.funding_info.0.index as u32 {
1960                                         if (tx.input[0].sequence >> 8*3) as u8 == 0x80 && (tx.lock_time >> 8*3) as u8 == 0x20 {
1961                                                 let (mut new_outpoints, new_outputs) = self.check_spend_counterparty_transaction(&tx, height, &logger);
1962                                                 if !new_outputs.1.is_empty() {
1963                                                         watch_outputs.push(new_outputs);
1964                                                 }
1965                                                 if new_outpoints.is_empty() {
1966                                                         let (mut new_outpoints, new_outputs) = self.check_spend_holder_transaction(&tx, height, &logger);
1967                                                         if !new_outputs.1.is_empty() {
1968                                                                 watch_outputs.push(new_outputs);
1969                                                         }
1970                                                         claimable_outpoints.append(&mut new_outpoints);
1971                                                 }
1972                                                 claimable_outpoints.append(&mut new_outpoints);
1973                                         }
1974                                 } else {
1975                                         if let Some(&commitment_number) = self.counterparty_commitment_txn_on_chain.get(&prevout.txid) {
1976                                                 let (mut new_outpoints, new_outputs_option) = self.check_spend_counterparty_htlc(&tx, commitment_number, height, &logger);
1977                                                 claimable_outpoints.append(&mut new_outpoints);
1978                                                 if let Some(new_outputs) = new_outputs_option {
1979                                                         watch_outputs.push(new_outputs);
1980                                                 }
1981                                         }
1982                                 }
1983                         }
1984                         // While all commitment/HTLC-Success/HTLC-Timeout transactions have one input, HTLCs
1985                         // can also be resolved in a few other ways which can have more than one output. Thus,
1986                         // we call is_resolving_htlc_output here outside of the tx.input.len() == 1 check.
1987                         self.is_resolving_htlc_output(&tx, height, &logger);
1988
1989                         self.is_paying_spendable_output(&tx, height, &logger);
1990                 }
1991
1992                 self.block_confirmed(height, txn_matched, watch_outputs, claimable_outpoints, broadcaster, fee_estimator, logger)
1993         }
1994
1995         fn block_confirmed<B: Deref, F: Deref, L: Deref>(
1996                 &mut self,
1997                 height: u32,
1998                 txn_matched: Vec<&Transaction>,
1999                 mut watch_outputs: Vec<TransactionOutputs>,
2000                 mut claimable_outpoints: Vec<PackageTemplate>,
2001                 broadcaster: B,
2002                 fee_estimator: F,
2003                 logger: L,
2004         ) -> Vec<TransactionOutputs>
2005         where
2006                 B::Target: BroadcasterInterface,
2007                 F::Target: FeeEstimator,
2008                 L::Target: Logger,
2009         {
2010                 let should_broadcast = self.would_broadcast_at_height(height, &logger);
2011                 if should_broadcast {
2012                         let funding_outp = HolderFundingOutput::build(self.funding_redeemscript.clone());
2013                         let commitment_package = PackageTemplate::build_package(self.funding_info.0.txid.clone(), self.funding_info.0.index as u32, PackageSolvingData::HolderFundingOutput(funding_outp), height, false, height);
2014                         claimable_outpoints.push(commitment_package);
2015                         self.pending_monitor_events.push(MonitorEvent::CommitmentTxBroadcasted(self.funding_info.0));
2016                         let commitment_tx = self.onchain_tx_handler.get_fully_signed_holder_tx(&self.funding_redeemscript);
2017                         self.holder_tx_signed = true;
2018                         let (mut new_outpoints, _) = self.get_broadcasted_holder_claims(&self.current_holder_commitment_tx, height);
2019                         let new_outputs = self.get_broadcasted_holder_watch_outputs(&self.current_holder_commitment_tx, &commitment_tx);
2020                         if !new_outputs.is_empty() {
2021                                 watch_outputs.push((self.current_holder_commitment_tx.txid.clone(), new_outputs));
2022                         }
2023                         claimable_outpoints.append(&mut new_outpoints);
2024                 }
2025
2026                 // Find which on-chain events have reached their confirmation threshold.
2027                 let onchain_events_awaiting_threshold_conf =
2028                         self.onchain_events_awaiting_threshold_conf.drain(..).collect::<Vec<_>>();
2029                 let mut onchain_events_reaching_threshold_conf = Vec::new();
2030                 for entry in onchain_events_awaiting_threshold_conf {
2031                         if entry.has_reached_confirmation_threshold(height) {
2032                                 onchain_events_reaching_threshold_conf.push(entry);
2033                         } else {
2034                                 self.onchain_events_awaiting_threshold_conf.push(entry);
2035                         }
2036                 }
2037
2038                 // Used to check for duplicate HTLC resolutions.
2039                 #[cfg(debug_assertions)]
2040                 let unmatured_htlcs: Vec<_> = self.onchain_events_awaiting_threshold_conf
2041                         .iter()
2042                         .filter_map(|entry| match &entry.event {
2043                                 OnchainEvent::HTLCUpdate { source, .. } => Some(source),
2044                                 OnchainEvent::MaturingOutput { .. } => None,
2045                         })
2046                         .collect();
2047                 #[cfg(debug_assertions)]
2048                 let mut matured_htlcs = Vec::new();
2049
2050                 // Produce actionable events from on-chain events having reached their threshold.
2051                 for entry in onchain_events_reaching_threshold_conf.drain(..) {
2052                         match entry.event {
2053                                 OnchainEvent::HTLCUpdate { ref source, payment_hash } => {
2054                                         // Check for duplicate HTLC resolutions.
2055                                         #[cfg(debug_assertions)]
2056                                         {
2057                                                 debug_assert!(
2058                                                         unmatured_htlcs.iter().find(|&htlc| htlc == &source).is_none(),
2059                                                         "An unmature HTLC transaction conflicts with a maturing one; failed to \
2060                                                          call either transaction_unconfirmed for the conflicting transaction \
2061                                                          or block_disconnected for a block containing it.");
2062                                                 debug_assert!(
2063                                                         matured_htlcs.iter().find(|&htlc| htlc == source).is_none(),
2064                                                         "A matured HTLC transaction conflicts with a maturing one; failed to \
2065                                                          call either transaction_unconfirmed for the conflicting transaction \
2066                                                          or block_disconnected for a block containing it.");
2067                                                 matured_htlcs.push(source.clone());
2068                                         }
2069
2070                                         log_trace!(logger, "HTLC {} failure update has got enough confirmations to be passed upstream", log_bytes!(payment_hash.0));
2071                                         self.pending_monitor_events.push(MonitorEvent::HTLCEvent(HTLCUpdate {
2072                                                 payment_hash: payment_hash,
2073                                                 payment_preimage: None,
2074                                                 source: source.clone(),
2075                                         }));
2076                                 },
2077                                 OnchainEvent::MaturingOutput { descriptor } => {
2078                                         log_trace!(logger, "Descriptor {} has got enough confirmations to be passed upstream", log_spendable!(descriptor));
2079                                         self.pending_events.push(Event::SpendableOutputs {
2080                                                 outputs: vec![descriptor]
2081                                         });
2082                                 }
2083                         }
2084                 }
2085
2086                 self.onchain_tx_handler.update_claims_view(&txn_matched, claimable_outpoints, height, &&*broadcaster, &&*fee_estimator, &&*logger);
2087
2088                 // Determine new outputs to watch by comparing against previously known outputs to watch,
2089                 // updating the latter in the process.
2090                 watch_outputs.retain(|&(ref txid, ref txouts)| {
2091                         let idx_and_scripts = txouts.iter().map(|o| (o.0, o.1.script_pubkey.clone())).collect();
2092                         self.outputs_to_watch.insert(txid.clone(), idx_and_scripts).is_none()
2093                 });
2094                 #[cfg(test)]
2095                 {
2096                         // If we see a transaction for which we registered outputs previously,
2097                         // make sure the registered scriptpubkey at the expected index match
2098                         // the actual transaction output one. We failed this case before #653.
2099                         for tx in &txn_matched {
2100                                 if let Some(outputs) = self.get_outputs_to_watch().get(&tx.txid()) {
2101                                         for idx_and_script in outputs.iter() {
2102                                                 assert!((idx_and_script.0 as usize) < tx.output.len());
2103                                                 assert_eq!(tx.output[idx_and_script.0 as usize].script_pubkey, idx_and_script.1);
2104                                         }
2105                                 }
2106                         }
2107                 }
2108                 watch_outputs
2109         }
2110
2111         pub fn block_disconnected<B: Deref, F: Deref, L: Deref>(&mut self, header: &BlockHeader, height: u32, broadcaster: B, fee_estimator: F, logger: L)
2112                 where B::Target: BroadcasterInterface,
2113                       F::Target: FeeEstimator,
2114                       L::Target: Logger,
2115         {
2116                 log_trace!(logger, "Block {} at height {} disconnected", header.block_hash(), height);
2117
2118                 //We may discard:
2119                 //- htlc update there as failure-trigger tx (revoked commitment tx, non-revoked commitment tx, HTLC-timeout tx) has been disconnected
2120                 //- maturing spendable output has transaction paying us has been disconnected
2121                 self.onchain_events_awaiting_threshold_conf.retain(|ref entry| entry.height < height);
2122
2123                 self.onchain_tx_handler.block_disconnected(height, broadcaster, fee_estimator, logger);
2124
2125                 self.best_block = BestBlock::new(header.prev_blockhash, height - 1);
2126         }
2127
2128         fn transaction_unconfirmed<B: Deref, F: Deref, L: Deref>(
2129                 &mut self,
2130                 txid: &Txid,
2131                 broadcaster: B,
2132                 fee_estimator: F,
2133                 logger: L,
2134         ) where
2135                 B::Target: BroadcasterInterface,
2136                 F::Target: FeeEstimator,
2137                 L::Target: Logger,
2138         {
2139                 self.onchain_events_awaiting_threshold_conf.retain(|ref entry| entry.txid != *txid);
2140                 self.onchain_tx_handler.transaction_unconfirmed(txid, broadcaster, fee_estimator, logger);
2141         }
2142
2143         /// Filters a block's `txdata` for transactions spending watched outputs or for any child
2144         /// transactions thereof.
2145         fn filter_block<'a>(&self, txdata: &TransactionData<'a>) -> Vec<&'a Transaction> {
2146                 let mut matched_txn = HashSet::new();
2147                 txdata.iter().filter(|&&(_, tx)| {
2148                         let mut matches = self.spends_watched_output(tx);
2149                         for input in tx.input.iter() {
2150                                 if matches { break; }
2151                                 if matched_txn.contains(&input.previous_output.txid) {
2152                                         matches = true;
2153                                 }
2154                         }
2155                         if matches {
2156                                 matched_txn.insert(tx.txid());
2157                         }
2158                         matches
2159                 }).map(|(_, tx)| *tx).collect()
2160         }
2161
2162         /// Checks if a given transaction spends any watched outputs.
2163         fn spends_watched_output(&self, tx: &Transaction) -> bool {
2164                 for input in tx.input.iter() {
2165                         if let Some(outputs) = self.get_outputs_to_watch().get(&input.previous_output.txid) {
2166                                 for (idx, _script_pubkey) in outputs.iter() {
2167                                         if *idx == input.previous_output.vout {
2168                                                 #[cfg(test)]
2169                                                 {
2170                                                         // If the expected script is a known type, check that the witness
2171                                                         // appears to be spending the correct type (ie that the match would
2172                                                         // actually succeed in BIP 158/159-style filters).
2173                                                         if _script_pubkey.is_v0_p2wsh() {
2174                                                                 assert_eq!(&bitcoin::Address::p2wsh(&Script::from(input.witness.last().unwrap().clone()), bitcoin::Network::Bitcoin).script_pubkey(), _script_pubkey);
2175                                                         } else if _script_pubkey.is_v0_p2wpkh() {
2176                                                                 assert_eq!(&bitcoin::Address::p2wpkh(&bitcoin::PublicKey::from_slice(&input.witness.last().unwrap()).unwrap(), bitcoin::Network::Bitcoin).unwrap().script_pubkey(), _script_pubkey);
2177                                                         } else { panic!(); }
2178                                                 }
2179                                                 return true;
2180                                         }
2181                                 }
2182                         }
2183                 }
2184
2185                 false
2186         }
2187
2188         fn would_broadcast_at_height<L: Deref>(&self, height: u32, logger: &L) -> bool where L::Target: Logger {
2189                 // We need to consider all HTLCs which are:
2190                 //  * in any unrevoked counterparty commitment transaction, as they could broadcast said
2191                 //    transactions and we'd end up in a race, or
2192                 //  * are in our latest holder commitment transaction, as this is the thing we will
2193                 //    broadcast if we go on-chain.
2194                 // Note that we consider HTLCs which were below dust threshold here - while they don't
2195                 // strictly imply that we need to fail the channel, we need to go ahead and fail them back
2196                 // to the source, and if we don't fail the channel we will have to ensure that the next
2197                 // updates that peer sends us are update_fails, failing the channel if not. It's probably
2198                 // easier to just fail the channel as this case should be rare enough anyway.
2199                 macro_rules! scan_commitment {
2200                         ($htlcs: expr, $holder_tx: expr) => {
2201                                 for ref htlc in $htlcs {
2202                                         // For inbound HTLCs which we know the preimage for, we have to ensure we hit the
2203                                         // chain with enough room to claim the HTLC without our counterparty being able to
2204                                         // time out the HTLC first.
2205                                         // For outbound HTLCs which our counterparty hasn't failed/claimed, our primary
2206                                         // concern is being able to claim the corresponding inbound HTLC (on another
2207                                         // channel) before it expires. In fact, we don't even really care if our
2208                                         // counterparty here claims such an outbound HTLC after it expired as long as we
2209                                         // can still claim the corresponding HTLC. Thus, to avoid needlessly hitting the
2210                                         // chain when our counterparty is waiting for expiration to off-chain fail an HTLC
2211                                         // we give ourselves a few blocks of headroom after expiration before going
2212                                         // on-chain for an expired HTLC.
2213                                         // Note that, to avoid a potential attack whereby a node delays claiming an HTLC
2214                                         // from us until we've reached the point where we go on-chain with the
2215                                         // corresponding inbound HTLC, we must ensure that outbound HTLCs go on chain at
2216                                         // least CLTV_CLAIM_BUFFER blocks prior to the inbound HTLC.
2217                                         //  aka outbound_cltv + LATENCY_GRACE_PERIOD_BLOCKS == height - CLTV_CLAIM_BUFFER
2218                                         //      inbound_cltv == height + CLTV_CLAIM_BUFFER
2219                                         //      outbound_cltv + LATENCY_GRACE_PERIOD_BLOCKS + CLTV_CLAIM_BUFFER <= inbound_cltv - CLTV_CLAIM_BUFFER
2220                                         //      LATENCY_GRACE_PERIOD_BLOCKS + 2*CLTV_CLAIM_BUFFER <= inbound_cltv - outbound_cltv
2221                                         //      CLTV_EXPIRY_DELTA <= inbound_cltv - outbound_cltv (by check in ChannelManager::decode_update_add_htlc_onion)
2222                                         //      LATENCY_GRACE_PERIOD_BLOCKS + 2*CLTV_CLAIM_BUFFER <= CLTV_EXPIRY_DELTA
2223                                         //  The final, above, condition is checked for statically in channelmanager
2224                                         //  with CHECK_CLTV_EXPIRY_SANITY_2.
2225                                         let htlc_outbound = $holder_tx == htlc.offered;
2226                                         if ( htlc_outbound && htlc.cltv_expiry + LATENCY_GRACE_PERIOD_BLOCKS <= height) ||
2227                                            (!htlc_outbound && htlc.cltv_expiry <= height + CLTV_CLAIM_BUFFER && self.payment_preimages.contains_key(&htlc.payment_hash)) {
2228                                                 log_info!(logger, "Force-closing channel due to {} HTLC timeout, HTLC expiry is {}", if htlc_outbound { "outbound" } else { "inbound "}, htlc.cltv_expiry);
2229                                                 return true;
2230                                         }
2231                                 }
2232                         }
2233                 }
2234
2235                 scan_commitment!(self.current_holder_commitment_tx.htlc_outputs.iter().map(|&(ref a, _, _)| a), true);
2236
2237                 if let Some(ref txid) = self.current_counterparty_commitment_txid {
2238                         if let Some(ref htlc_outputs) = self.counterparty_claimable_outpoints.get(txid) {
2239                                 scan_commitment!(htlc_outputs.iter().map(|&(ref a, _)| a), false);
2240                         }
2241                 }
2242                 if let Some(ref txid) = self.prev_counterparty_commitment_txid {
2243                         if let Some(ref htlc_outputs) = self.counterparty_claimable_outpoints.get(txid) {
2244                                 scan_commitment!(htlc_outputs.iter().map(|&(ref a, _)| a), false);
2245                         }
2246                 }
2247
2248                 false
2249         }
2250
2251         /// Check if any transaction broadcasted is resolving HTLC output by a success or timeout on a holder
2252         /// or counterparty commitment tx, if so send back the source, preimage if found and payment_hash of resolved HTLC
2253         fn is_resolving_htlc_output<L: Deref>(&mut self, tx: &Transaction, height: u32, logger: &L) where L::Target: Logger {
2254                 'outer_loop: for input in &tx.input {
2255                         let mut payment_data = None;
2256                         let revocation_sig_claim = (input.witness.len() == 3 && HTLCType::scriptlen_to_htlctype(input.witness[2].len()) == Some(HTLCType::OfferedHTLC) && input.witness[1].len() == 33)
2257                                 || (input.witness.len() == 3 && HTLCType::scriptlen_to_htlctype(input.witness[2].len()) == Some(HTLCType::AcceptedHTLC) && input.witness[1].len() == 33);
2258                         let accepted_preimage_claim = input.witness.len() == 5 && HTLCType::scriptlen_to_htlctype(input.witness[4].len()) == Some(HTLCType::AcceptedHTLC);
2259                         let offered_preimage_claim = input.witness.len() == 3 && HTLCType::scriptlen_to_htlctype(input.witness[2].len()) == Some(HTLCType::OfferedHTLC);
2260
2261                         macro_rules! log_claim {
2262                                 ($tx_info: expr, $holder_tx: expr, $htlc: expr, $source_avail: expr) => {
2263                                         // We found the output in question, but aren't failing it backwards
2264                                         // as we have no corresponding source and no valid counterparty commitment txid
2265                                         // to try a weak source binding with same-hash, same-value still-valid offered HTLC.
2266                                         // This implies either it is an inbound HTLC or an outbound HTLC on a revoked transaction.
2267                                         let outbound_htlc = $holder_tx == $htlc.offered;
2268                                         if ($holder_tx && revocation_sig_claim) ||
2269                                                         (outbound_htlc && !$source_avail && (accepted_preimage_claim || offered_preimage_claim)) {
2270                                                 log_error!(logger, "Input spending {} ({}:{}) in {} resolves {} HTLC with payment hash {} with {}!",
2271                                                         $tx_info, input.previous_output.txid, input.previous_output.vout, tx.txid(),
2272                                                         if outbound_htlc { "outbound" } else { "inbound" }, log_bytes!($htlc.payment_hash.0),
2273                                                         if revocation_sig_claim { "revocation sig" } else { "preimage claim after we'd passed the HTLC resolution back" });
2274                                         } else {
2275                                                 log_info!(logger, "Input spending {} ({}:{}) in {} resolves {} HTLC with payment hash {} with {}",
2276                                                         $tx_info, input.previous_output.txid, input.previous_output.vout, tx.txid(),
2277                                                         if outbound_htlc { "outbound" } else { "inbound" }, log_bytes!($htlc.payment_hash.0),
2278                                                         if revocation_sig_claim { "revocation sig" } else if accepted_preimage_claim || offered_preimage_claim { "preimage" } else { "timeout" });
2279                                         }
2280                                 }
2281                         }
2282
2283                         macro_rules! check_htlc_valid_counterparty {
2284                                 ($counterparty_txid: expr, $htlc_output: expr) => {
2285                                         if let Some(txid) = $counterparty_txid {
2286                                                 for &(ref pending_htlc, ref pending_source) in self.counterparty_claimable_outpoints.get(&txid).unwrap() {
2287                                                         if pending_htlc.payment_hash == $htlc_output.payment_hash && pending_htlc.amount_msat == $htlc_output.amount_msat {
2288                                                                 if let &Some(ref source) = pending_source {
2289                                                                         log_claim!("revoked counterparty commitment tx", false, pending_htlc, true);
2290                                                                         payment_data = Some(((**source).clone(), $htlc_output.payment_hash));
2291                                                                         break;
2292                                                                 }
2293                                                         }
2294                                                 }
2295                                         }
2296                                 }
2297                         }
2298
2299                         macro_rules! scan_commitment {
2300                                 ($htlcs: expr, $tx_info: expr, $holder_tx: expr) => {
2301                                         for (ref htlc_output, source_option) in $htlcs {
2302                                                 if Some(input.previous_output.vout) == htlc_output.transaction_output_index {
2303                                                         if let Some(ref source) = source_option {
2304                                                                 log_claim!($tx_info, $holder_tx, htlc_output, true);
2305                                                                 // We have a resolution of an HTLC either from one of our latest
2306                                                                 // holder commitment transactions or an unrevoked counterparty commitment
2307                                                                 // transaction. This implies we either learned a preimage, the HTLC
2308                                                                 // has timed out, or we screwed up. In any case, we should now
2309                                                                 // resolve the source HTLC with the original sender.
2310                                                                 payment_data = Some(((*source).clone(), htlc_output.payment_hash));
2311                                                         } else if !$holder_tx {
2312                                                                         check_htlc_valid_counterparty!(self.current_counterparty_commitment_txid, htlc_output);
2313                                                                 if payment_data.is_none() {
2314                                                                         check_htlc_valid_counterparty!(self.prev_counterparty_commitment_txid, htlc_output);
2315                                                                 }
2316                                                         }
2317                                                         if payment_data.is_none() {
2318                                                                 log_claim!($tx_info, $holder_tx, htlc_output, false);
2319                                                                 continue 'outer_loop;
2320                                                         }
2321                                                 }
2322                                         }
2323                                 }
2324                         }
2325
2326                         if input.previous_output.txid == self.current_holder_commitment_tx.txid {
2327                                 scan_commitment!(self.current_holder_commitment_tx.htlc_outputs.iter().map(|&(ref a, _, ref b)| (a, b.as_ref())),
2328                                         "our latest holder commitment tx", true);
2329                         }
2330                         if let Some(ref prev_holder_signed_commitment_tx) = self.prev_holder_signed_commitment_tx {
2331                                 if input.previous_output.txid == prev_holder_signed_commitment_tx.txid {
2332                                         scan_commitment!(prev_holder_signed_commitment_tx.htlc_outputs.iter().map(|&(ref a, _, ref b)| (a, b.as_ref())),
2333                                                 "our previous holder commitment tx", true);
2334                                 }
2335                         }
2336                         if let Some(ref htlc_outputs) = self.counterparty_claimable_outpoints.get(&input.previous_output.txid) {
2337                                 scan_commitment!(htlc_outputs.iter().map(|&(ref a, ref b)| (a, (b.as_ref().clone()).map(|boxed| &**boxed))),
2338                                         "counterparty commitment tx", false);
2339                         }
2340
2341                         // Check that scan_commitment, above, decided there is some source worth relaying an
2342                         // HTLC resolution backwards to and figure out whether we learned a preimage from it.
2343                         if let Some((source, payment_hash)) = payment_data {
2344                                 let mut payment_preimage = PaymentPreimage([0; 32]);
2345                                 if accepted_preimage_claim {
2346                                         if !self.pending_monitor_events.iter().any(
2347                                                 |update| if let &MonitorEvent::HTLCEvent(ref upd) = update { upd.source == source } else { false }) {
2348                                                 payment_preimage.0.copy_from_slice(&input.witness[3]);
2349                                                 self.pending_monitor_events.push(MonitorEvent::HTLCEvent(HTLCUpdate {
2350                                                         source,
2351                                                         payment_preimage: Some(payment_preimage),
2352                                                         payment_hash
2353                                                 }));
2354                                         }
2355                                 } else if offered_preimage_claim {
2356                                         if !self.pending_monitor_events.iter().any(
2357                                                 |update| if let &MonitorEvent::HTLCEvent(ref upd) = update {
2358                                                         upd.source == source
2359                                                 } else { false }) {
2360                                                 payment_preimage.0.copy_from_slice(&input.witness[1]);
2361                                                 self.pending_monitor_events.push(MonitorEvent::HTLCEvent(HTLCUpdate {
2362                                                         source,
2363                                                         payment_preimage: Some(payment_preimage),
2364                                                         payment_hash
2365                                                 }));
2366                                         }
2367                                 } else {
2368                                         self.onchain_events_awaiting_threshold_conf.retain(|ref entry| {
2369                                                 if entry.height != height { return true; }
2370                                                 match entry.event {
2371                                                         OnchainEvent::HTLCUpdate { source: ref htlc_source, .. } => {
2372                                                                 *htlc_source != source
2373                                                         },
2374                                                         _ => true,
2375                                                 }
2376                                         });
2377                                         let entry = OnchainEventEntry {
2378                                                 txid: tx.txid(),
2379                                                 height,
2380                                                 event: OnchainEvent::HTLCUpdate { source: source, payment_hash: payment_hash },
2381                                         };
2382                                         log_info!(logger, "Failing HTLC with payment_hash {} timeout by a spend tx, waiting for confirmation (at height{})", log_bytes!(payment_hash.0), entry.confirmation_threshold());
2383                                         self.onchain_events_awaiting_threshold_conf.push(entry);
2384                                 }
2385                         }
2386                 }
2387         }
2388
2389         /// Check if any transaction broadcasted is paying fund back to some address we can assume to own
2390         fn is_paying_spendable_output<L: Deref>(&mut self, tx: &Transaction, height: u32, logger: &L) where L::Target: Logger {
2391                 let mut spendable_output = None;
2392                 for (i, outp) in tx.output.iter().enumerate() { // There is max one spendable output for any channel tx, including ones generated by us
2393                         if i > ::core::u16::MAX as usize {
2394                                 // While it is possible that an output exists on chain which is greater than the
2395                                 // 2^16th output in a given transaction, this is only possible if the output is not
2396                                 // in a lightning transaction and was instead placed there by some third party who
2397                                 // wishes to give us money for no reason.
2398                                 // Namely, any lightning transactions which we pre-sign will never have anywhere
2399                                 // near 2^16 outputs both because such transactions must have ~2^16 outputs who's
2400                                 // scripts are not longer than one byte in length and because they are inherently
2401                                 // non-standard due to their size.
2402                                 // Thus, it is completely safe to ignore such outputs, and while it may result in
2403                                 // us ignoring non-lightning fund to us, that is only possible if someone fills
2404                                 // nearly a full block with garbage just to hit this case.
2405                                 continue;
2406                         }
2407                         if outp.script_pubkey == self.destination_script {
2408                                 spendable_output =  Some(SpendableOutputDescriptor::StaticOutput {
2409                                         outpoint: OutPoint { txid: tx.txid(), index: i as u16 },
2410                                         output: outp.clone(),
2411                                 });
2412                                 break;
2413                         } else if let Some(ref broadcasted_holder_revokable_script) = self.broadcasted_holder_revokable_script {
2414                                 if broadcasted_holder_revokable_script.0 == outp.script_pubkey {
2415                                         spendable_output =  Some(SpendableOutputDescriptor::DelayedPaymentOutput(DelayedPaymentOutputDescriptor {
2416                                                 outpoint: OutPoint { txid: tx.txid(), index: i as u16 },
2417                                                 per_commitment_point: broadcasted_holder_revokable_script.1,
2418                                                 to_self_delay: self.on_holder_tx_csv,
2419                                                 output: outp.clone(),
2420                                                 revocation_pubkey: broadcasted_holder_revokable_script.2.clone(),
2421                                                 channel_keys_id: self.channel_keys_id,
2422                                                 channel_value_satoshis: self.channel_value_satoshis,
2423                                         }));
2424                                         break;
2425                                 }
2426                         } else if self.counterparty_payment_script == outp.script_pubkey {
2427                                 spendable_output = Some(SpendableOutputDescriptor::StaticPaymentOutput(StaticPaymentOutputDescriptor {
2428                                         outpoint: OutPoint { txid: tx.txid(), index: i as u16 },
2429                                         output: outp.clone(),
2430                                         channel_keys_id: self.channel_keys_id,
2431                                         channel_value_satoshis: self.channel_value_satoshis,
2432                                 }));
2433                                 break;
2434                         } else if outp.script_pubkey == self.shutdown_script {
2435                                 spendable_output = Some(SpendableOutputDescriptor::StaticOutput {
2436                                         outpoint: OutPoint { txid: tx.txid(), index: i as u16 },
2437                                         output: outp.clone(),
2438                                 });
2439                         }
2440                 }
2441                 if let Some(spendable_output) = spendable_output {
2442                         let entry = OnchainEventEntry {
2443                                 txid: tx.txid(),
2444                                 height: height,
2445                                 event: OnchainEvent::MaturingOutput { descriptor: spendable_output.clone() },
2446                         };
2447                         log_trace!(logger, "Maturing {} until {}", log_spendable!(spendable_output), entry.confirmation_threshold());
2448                         self.onchain_events_awaiting_threshold_conf.push(entry);
2449                 }
2450         }
2451 }
2452
2453 /// `Persist` defines behavior for persisting channel monitors: this could mean
2454 /// writing once to disk, and/or uploading to one or more backup services.
2455 ///
2456 /// Note that for every new monitor, you **must** persist the new `ChannelMonitor`
2457 /// to disk/backups. And, on every update, you **must** persist either the
2458 /// `ChannelMonitorUpdate` or the updated monitor itself. Otherwise, there is risk
2459 /// of situations such as revoking a transaction, then crashing before this
2460 /// revocation can be persisted, then unintentionally broadcasting a revoked
2461 /// transaction and losing money. This is a risk because previous channel states
2462 /// are toxic, so it's important that whatever channel state is persisted is
2463 /// kept up-to-date.
2464 pub trait Persist<ChannelSigner: Sign> {
2465         /// Persist a new channel's data. The data can be stored any way you want, but
2466         /// the identifier provided by Rust-Lightning is the channel's outpoint (and
2467         /// it is up to you to maintain a correct mapping between the outpoint and the
2468         /// stored channel data). Note that you **must** persist every new monitor to
2469         /// disk. See the `Persist` trait documentation for more details.
2470         ///
2471         /// See [`ChannelMonitor::write`] for writing out a `ChannelMonitor`,
2472         /// and [`ChannelMonitorUpdateErr`] for requirements when returning errors.
2473         fn persist_new_channel(&self, id: OutPoint, data: &ChannelMonitor<ChannelSigner>) -> Result<(), ChannelMonitorUpdateErr>;
2474
2475         /// Update one channel's data. The provided `ChannelMonitor` has already
2476         /// applied the given update.
2477         ///
2478         /// Note that on every update, you **must** persist either the
2479         /// `ChannelMonitorUpdate` or the updated monitor itself to disk/backups. See
2480         /// the `Persist` trait documentation for more details.
2481         ///
2482         /// If an implementer chooses to persist the updates only, they need to make
2483         /// sure that all the updates are applied to the `ChannelMonitors` *before*
2484         /// the set of channel monitors is given to the `ChannelManager`
2485         /// deserialization routine. See [`ChannelMonitor::update_monitor`] for
2486         /// applying a monitor update to a monitor. If full `ChannelMonitors` are
2487         /// persisted, then there is no need to persist individual updates.
2488         ///
2489         /// Note that there could be a performance tradeoff between persisting complete
2490         /// channel monitors on every update vs. persisting only updates and applying
2491         /// them in batches. The size of each monitor grows `O(number of state updates)`
2492         /// whereas updates are small and `O(1)`.
2493         ///
2494         /// See [`ChannelMonitor::write`] for writing out a `ChannelMonitor`,
2495         /// [`ChannelMonitorUpdate::write`] for writing out an update, and
2496         /// [`ChannelMonitorUpdateErr`] for requirements when returning errors.
2497         fn update_persisted_channel(&self, id: OutPoint, update: &ChannelMonitorUpdate, data: &ChannelMonitor<ChannelSigner>) -> Result<(), ChannelMonitorUpdateErr>;
2498 }
2499
2500 impl<Signer: Sign, T: Deref, F: Deref, L: Deref> chain::Listen for (ChannelMonitor<Signer>, T, F, L)
2501 where
2502         T::Target: BroadcasterInterface,
2503         F::Target: FeeEstimator,
2504         L::Target: Logger,
2505 {
2506         fn block_connected(&self, block: &Block, height: u32) {
2507                 let txdata: Vec<_> = block.txdata.iter().enumerate().collect();
2508                 self.0.block_connected(&block.header, &txdata, height, &*self.1, &*self.2, &*self.3);
2509         }
2510
2511         fn block_disconnected(&self, header: &BlockHeader, height: u32) {
2512                 self.0.block_disconnected(header, height, &*self.1, &*self.2, &*self.3);
2513         }
2514 }
2515
2516 impl<Signer: Sign, T: Deref, F: Deref, L: Deref> chain::Confirm for (ChannelMonitor<Signer>, T, F, L)
2517 where
2518         T::Target: BroadcasterInterface,
2519         F::Target: FeeEstimator,
2520         L::Target: Logger,
2521 {
2522         fn transactions_confirmed(&self, header: &BlockHeader, txdata: &TransactionData, height: u32) {
2523                 self.0.transactions_confirmed(header, txdata, height, &*self.1, &*self.2, &*self.3);
2524         }
2525
2526         fn transaction_unconfirmed(&self, txid: &Txid) {
2527                 self.0.transaction_unconfirmed(txid, &*self.1, &*self.2, &*self.3);
2528         }
2529
2530         fn best_block_updated(&self, header: &BlockHeader, height: u32) {
2531                 self.0.best_block_updated(header, height, &*self.1, &*self.2, &*self.3);
2532         }
2533
2534         fn get_relevant_txids(&self) -> Vec<Txid> {
2535                 self.0.get_relevant_txids()
2536         }
2537 }
2538
2539 const MAX_ALLOC_SIZE: usize = 64*1024;
2540
2541 impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
2542                 for (BlockHash, ChannelMonitor<Signer>) {
2543         fn read<R: ::std::io::Read>(reader: &mut R, keys_manager: &'a K) -> Result<Self, DecodeError> {
2544                 macro_rules! unwrap_obj {
2545                         ($key: expr) => {
2546                                 match $key {
2547                                         Ok(res) => res,
2548                                         Err(_) => return Err(DecodeError::InvalidValue),
2549                                 }
2550                         }
2551                 }
2552
2553                 let _ver = read_ver_prefix!(reader, SERIALIZATION_VERSION);
2554
2555                 let latest_update_id: u64 = Readable::read(reader)?;
2556                 let commitment_transaction_number_obscure_factor = <U48 as Readable>::read(reader)?.0;
2557
2558                 let destination_script = Readable::read(reader)?;
2559                 let broadcasted_holder_revokable_script = match <u8 as Readable>::read(reader)? {
2560                         0 => {
2561                                 let revokable_address = Readable::read(reader)?;
2562                                 let per_commitment_point = Readable::read(reader)?;
2563                                 let revokable_script = Readable::read(reader)?;
2564                                 Some((revokable_address, per_commitment_point, revokable_script))
2565                         },
2566                         1 => { None },
2567                         _ => return Err(DecodeError::InvalidValue),
2568                 };
2569                 let counterparty_payment_script = Readable::read(reader)?;
2570                 let shutdown_script = Readable::read(reader)?;
2571
2572                 let channel_keys_id = Readable::read(reader)?;
2573                 let holder_revocation_basepoint = Readable::read(reader)?;
2574                 // Technically this can fail and serialize fail a round-trip, but only for serialization of
2575                 // barely-init'd ChannelMonitors that we can't do anything with.
2576                 let outpoint = OutPoint {
2577                         txid: Readable::read(reader)?,
2578                         index: Readable::read(reader)?,
2579                 };
2580                 let funding_info = (outpoint, Readable::read(reader)?);
2581                 let current_counterparty_commitment_txid = Readable::read(reader)?;
2582                 let prev_counterparty_commitment_txid = Readable::read(reader)?;
2583
2584                 let counterparty_tx_cache = Readable::read(reader)?;
2585                 let funding_redeemscript = Readable::read(reader)?;
2586                 let channel_value_satoshis = Readable::read(reader)?;
2587
2588                 let their_cur_revocation_points = {
2589                         let first_idx = <U48 as Readable>::read(reader)?.0;
2590                         if first_idx == 0 {
2591                                 None
2592                         } else {
2593                                 let first_point = Readable::read(reader)?;
2594                                 let second_point_slice: [u8; 33] = Readable::read(reader)?;
2595                                 if second_point_slice[0..32] == [0; 32] && second_point_slice[32] == 0 {
2596                                         Some((first_idx, first_point, None))
2597                                 } else {
2598                                         Some((first_idx, first_point, Some(unwrap_obj!(PublicKey::from_slice(&second_point_slice)))))
2599                                 }
2600                         }
2601                 };
2602
2603                 let on_holder_tx_csv: u16 = Readable::read(reader)?;
2604
2605                 let commitment_secrets = Readable::read(reader)?;
2606
2607                 macro_rules! read_htlc_in_commitment {
2608                         () => {
2609                                 {
2610                                         let offered: bool = Readable::read(reader)?;
2611                                         let amount_msat: u64 = Readable::read(reader)?;
2612                                         let cltv_expiry: u32 = Readable::read(reader)?;
2613                                         let payment_hash: PaymentHash = Readable::read(reader)?;
2614                                         let transaction_output_index: Option<u32> = Readable::read(reader)?;
2615
2616                                         HTLCOutputInCommitment {
2617                                                 offered, amount_msat, cltv_expiry, payment_hash, transaction_output_index
2618                                         }
2619                                 }
2620                         }
2621                 }
2622
2623                 let counterparty_claimable_outpoints_len: u64 = Readable::read(reader)?;
2624                 let mut counterparty_claimable_outpoints = HashMap::with_capacity(cmp::min(counterparty_claimable_outpoints_len as usize, MAX_ALLOC_SIZE / 64));
2625                 for _ in 0..counterparty_claimable_outpoints_len {
2626                         let txid: Txid = Readable::read(reader)?;
2627                         let htlcs_count: u64 = Readable::read(reader)?;
2628                         let mut htlcs = Vec::with_capacity(cmp::min(htlcs_count as usize, MAX_ALLOC_SIZE / 32));
2629                         for _ in 0..htlcs_count {
2630                                 htlcs.push((read_htlc_in_commitment!(), <Option<HTLCSource> as Readable>::read(reader)?.map(|o: HTLCSource| Box::new(o))));
2631                         }
2632                         if let Some(_) = counterparty_claimable_outpoints.insert(txid, htlcs) {
2633                                 return Err(DecodeError::InvalidValue);
2634                         }
2635                 }
2636
2637                 let counterparty_commitment_txn_on_chain_len: u64 = Readable::read(reader)?;
2638                 let mut counterparty_commitment_txn_on_chain = HashMap::with_capacity(cmp::min(counterparty_commitment_txn_on_chain_len as usize, MAX_ALLOC_SIZE / 32));
2639                 for _ in 0..counterparty_commitment_txn_on_chain_len {
2640                         let txid: Txid = Readable::read(reader)?;
2641                         let commitment_number = <U48 as Readable>::read(reader)?.0;
2642                         if let Some(_) = counterparty_commitment_txn_on_chain.insert(txid, commitment_number) {
2643                                 return Err(DecodeError::InvalidValue);
2644                         }
2645                 }
2646
2647                 let counterparty_hash_commitment_number_len: u64 = Readable::read(reader)?;
2648                 let mut counterparty_hash_commitment_number = HashMap::with_capacity(cmp::min(counterparty_hash_commitment_number_len as usize, MAX_ALLOC_SIZE / 32));
2649                 for _ in 0..counterparty_hash_commitment_number_len {
2650                         let payment_hash: PaymentHash = Readable::read(reader)?;
2651                         let commitment_number = <U48 as Readable>::read(reader)?.0;
2652                         if let Some(_) = counterparty_hash_commitment_number.insert(payment_hash, commitment_number) {
2653                                 return Err(DecodeError::InvalidValue);
2654                         }
2655                 }
2656
2657                 let prev_holder_signed_commitment_tx = match <u8 as Readable>::read(reader)? {
2658                         0 => None,
2659                         1 => {
2660                                 Some(Readable::read(reader)?)
2661                         },
2662                         _ => return Err(DecodeError::InvalidValue),
2663                 };
2664                 let current_holder_commitment_tx = Readable::read(reader)?;
2665
2666                 let current_counterparty_commitment_number = <U48 as Readable>::read(reader)?.0;
2667                 let current_holder_commitment_number = <U48 as Readable>::read(reader)?.0;
2668
2669                 let payment_preimages_len: u64 = Readable::read(reader)?;
2670                 let mut payment_preimages = HashMap::with_capacity(cmp::min(payment_preimages_len as usize, MAX_ALLOC_SIZE / 32));
2671                 for _ in 0..payment_preimages_len {
2672                         let preimage: PaymentPreimage = Readable::read(reader)?;
2673                         let hash = PaymentHash(Sha256::hash(&preimage.0[..]).into_inner());
2674                         if let Some(_) = payment_preimages.insert(hash, preimage) {
2675                                 return Err(DecodeError::InvalidValue);
2676                         }
2677                 }
2678
2679                 let pending_monitor_events_len: u64 = Readable::read(reader)?;
2680                 let mut pending_monitor_events = Vec::with_capacity(cmp::min(pending_monitor_events_len as usize, MAX_ALLOC_SIZE / (32 + 8*3)));
2681                 for _ in 0..pending_monitor_events_len {
2682                         let ev = match <u8 as Readable>::read(reader)? {
2683                                 0 => MonitorEvent::HTLCEvent(Readable::read(reader)?),
2684                                 1 => MonitorEvent::CommitmentTxBroadcasted(funding_info.0),
2685                                 _ => return Err(DecodeError::InvalidValue)
2686                         };
2687                         pending_monitor_events.push(ev);
2688                 }
2689
2690                 let pending_events_len: u64 = Readable::read(reader)?;
2691                 let mut pending_events = Vec::with_capacity(cmp::min(pending_events_len as usize, MAX_ALLOC_SIZE / mem::size_of::<Event>()));
2692                 for _ in 0..pending_events_len {
2693                         if let Some(event) = MaybeReadable::read(reader)? {
2694                                 pending_events.push(event);
2695                         }
2696                 }
2697
2698                 let best_block = BestBlock::new(Readable::read(reader)?, Readable::read(reader)?);
2699
2700                 let waiting_threshold_conf_len: u64 = Readable::read(reader)?;
2701                 let mut onchain_events_awaiting_threshold_conf = Vec::with_capacity(cmp::min(waiting_threshold_conf_len as usize, MAX_ALLOC_SIZE / 128));
2702                 for _ in 0..waiting_threshold_conf_len {
2703                         onchain_events_awaiting_threshold_conf.push(Readable::read(reader)?);
2704                 }
2705
2706                 let outputs_to_watch_len: u64 = Readable::read(reader)?;
2707                 let mut outputs_to_watch = HashMap::with_capacity(cmp::min(outputs_to_watch_len as usize, MAX_ALLOC_SIZE / (mem::size_of::<Txid>() + mem::size_of::<u32>() + mem::size_of::<Vec<Script>>())));
2708                 for _ in 0..outputs_to_watch_len {
2709                         let txid = Readable::read(reader)?;
2710                         let outputs_len: u64 = Readable::read(reader)?;
2711                         let mut outputs = Vec::with_capacity(cmp::min(outputs_len as usize, MAX_ALLOC_SIZE / (mem::size_of::<u32>() + mem::size_of::<Script>())));
2712                         for _ in 0..outputs_len {
2713                                 outputs.push((Readable::read(reader)?, Readable::read(reader)?));
2714                         }
2715                         if let Some(_) = outputs_to_watch.insert(txid, outputs) {
2716                                 return Err(DecodeError::InvalidValue);
2717                         }
2718                 }
2719                 let onchain_tx_handler = ReadableArgs::read(reader, keys_manager)?;
2720
2721                 let lockdown_from_offchain = Readable::read(reader)?;
2722                 let holder_tx_signed = Readable::read(reader)?;
2723
2724                 read_tlv_fields!(reader, {}, {});
2725
2726                 let mut secp_ctx = Secp256k1::new();
2727                 secp_ctx.seeded_randomize(&keys_manager.get_secure_random_bytes());
2728
2729                 Ok((best_block.block_hash(), ChannelMonitor {
2730                         inner: Mutex::new(ChannelMonitorImpl {
2731                                 latest_update_id,
2732                                 commitment_transaction_number_obscure_factor,
2733
2734                                 destination_script,
2735                                 broadcasted_holder_revokable_script,
2736                                 counterparty_payment_script,
2737                                 shutdown_script,
2738
2739                                 channel_keys_id,
2740                                 holder_revocation_basepoint,
2741                                 funding_info,
2742                                 current_counterparty_commitment_txid,
2743                                 prev_counterparty_commitment_txid,
2744
2745                                 counterparty_tx_cache,
2746                                 funding_redeemscript,
2747                                 channel_value_satoshis,
2748                                 their_cur_revocation_points,
2749
2750                                 on_holder_tx_csv,
2751
2752                                 commitment_secrets,
2753                                 counterparty_claimable_outpoints,
2754                                 counterparty_commitment_txn_on_chain,
2755                                 counterparty_hash_commitment_number,
2756
2757                                 prev_holder_signed_commitment_tx,
2758                                 current_holder_commitment_tx,
2759                                 current_counterparty_commitment_number,
2760                                 current_holder_commitment_number,
2761
2762                                 payment_preimages,
2763                                 pending_monitor_events,
2764                                 pending_events,
2765
2766                                 onchain_events_awaiting_threshold_conf,
2767                                 outputs_to_watch,
2768
2769                                 onchain_tx_handler,
2770
2771                                 lockdown_from_offchain,
2772                                 holder_tx_signed,
2773
2774                                 best_block,
2775
2776                                 secp_ctx,
2777                         }),
2778                 }))
2779         }
2780 }
2781
2782 #[cfg(test)]
2783 mod tests {
2784         use bitcoin::blockdata::script::{Script, Builder};
2785         use bitcoin::blockdata::opcodes;
2786         use bitcoin::blockdata::transaction::{Transaction, TxIn, TxOut, SigHashType};
2787         use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
2788         use bitcoin::util::bip143;
2789         use bitcoin::hashes::Hash;
2790         use bitcoin::hashes::sha256::Hash as Sha256;
2791         use bitcoin::hashes::hex::FromHex;
2792         use bitcoin::hash_types::Txid;
2793         use bitcoin::network::constants::Network;
2794         use hex;
2795         use chain::channelmonitor::ChannelMonitor;
2796         use chain::package::{WEIGHT_OFFERED_HTLC, WEIGHT_RECEIVED_HTLC, WEIGHT_REVOKED_OFFERED_HTLC, WEIGHT_REVOKED_RECEIVED_HTLC, WEIGHT_REVOKED_OUTPUT};
2797         use chain::transaction::OutPoint;
2798         use ln::{PaymentPreimage, PaymentHash};
2799         use ln::channelmanager::BestBlock;
2800         use ln::chan_utils;
2801         use ln::chan_utils::{HTLCOutputInCommitment, ChannelPublicKeys, ChannelTransactionParameters, HolderCommitmentTransaction, CounterpartyChannelTransactionParameters};
2802         use util::test_utils::{TestLogger, TestBroadcaster, TestFeeEstimator};
2803         use bitcoin::secp256k1::key::{SecretKey,PublicKey};
2804         use bitcoin::secp256k1::Secp256k1;
2805         use std::sync::{Arc, Mutex};
2806         use chain::keysinterface::InMemorySigner;
2807         use prelude::*;
2808
2809         #[test]
2810         fn test_prune_preimages() {
2811                 let secp_ctx = Secp256k1::new();
2812                 let logger = Arc::new(TestLogger::new());
2813                 let broadcaster = Arc::new(TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new()), blocks: Arc::new(Mutex::new(Vec::new()))});
2814                 let fee_estimator = Arc::new(TestFeeEstimator { sat_per_kw: 253 });
2815
2816                 let dummy_key = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
2817                 let dummy_tx = Transaction { version: 0, lock_time: 0, input: Vec::new(), output: Vec::new() };
2818
2819                 let mut preimages = Vec::new();
2820                 {
2821                         for i in 0..20 {
2822                                 let preimage = PaymentPreimage([i; 32]);
2823                                 let hash = PaymentHash(Sha256::hash(&preimage.0[..]).into_inner());
2824                                 preimages.push((preimage, hash));
2825                         }
2826                 }
2827
2828                 macro_rules! preimages_slice_to_htlc_outputs {
2829                         ($preimages_slice: expr) => {
2830                                 {
2831                                         let mut res = Vec::new();
2832                                         for (idx, preimage) in $preimages_slice.iter().enumerate() {
2833                                                 res.push((HTLCOutputInCommitment {
2834                                                         offered: true,
2835                                                         amount_msat: 0,
2836                                                         cltv_expiry: 0,
2837                                                         payment_hash: preimage.1.clone(),
2838                                                         transaction_output_index: Some(idx as u32),
2839                                                 }, None));
2840                                         }
2841                                         res
2842                                 }
2843                         }
2844                 }
2845                 macro_rules! preimages_to_holder_htlcs {
2846                         ($preimages_slice: expr) => {
2847                                 {
2848                                         let mut inp = preimages_slice_to_htlc_outputs!($preimages_slice);
2849                                         let res: Vec<_> = inp.drain(..).map(|e| { (e.0, None, e.1) }).collect();
2850                                         res
2851                                 }
2852                         }
2853                 }
2854
2855                 macro_rules! test_preimages_exist {
2856                         ($preimages_slice: expr, $monitor: expr) => {
2857                                 for preimage in $preimages_slice {
2858                                         assert!($monitor.inner.lock().unwrap().payment_preimages.contains_key(&preimage.1));
2859                                 }
2860                         }
2861                 }
2862
2863                 let keys = InMemorySigner::new(
2864                         &secp_ctx,
2865                         SecretKey::from_slice(&[41; 32]).unwrap(),
2866                         SecretKey::from_slice(&[41; 32]).unwrap(),
2867                         SecretKey::from_slice(&[41; 32]).unwrap(),
2868                         SecretKey::from_slice(&[41; 32]).unwrap(),
2869                         SecretKey::from_slice(&[41; 32]).unwrap(),
2870                         [41; 32],
2871                         0,
2872                         [0; 32]
2873                 );
2874
2875                 let counterparty_pubkeys = ChannelPublicKeys {
2876                         funding_pubkey: PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[44; 32]).unwrap()),
2877                         revocation_basepoint: PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()),
2878                         payment_point: PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[46; 32]).unwrap()),
2879                         delayed_payment_basepoint: PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[47; 32]).unwrap()),
2880                         htlc_basepoint: PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[48; 32]).unwrap())
2881                 };
2882                 let funding_outpoint = OutPoint { txid: Default::default(), index: u16::max_value() };
2883                 let channel_parameters = ChannelTransactionParameters {
2884                         holder_pubkeys: keys.holder_channel_pubkeys.clone(),
2885                         holder_selected_contest_delay: 66,
2886                         is_outbound_from_holder: true,
2887                         counterparty_parameters: Some(CounterpartyChannelTransactionParameters {
2888                                 pubkeys: counterparty_pubkeys,
2889                                 selected_contest_delay: 67,
2890                         }),
2891                         funding_outpoint: Some(funding_outpoint),
2892                 };
2893                 // Prune with one old state and a holder commitment tx holding a few overlaps with the
2894                 // old state.
2895                 let best_block = BestBlock::from_genesis(Network::Testnet);
2896                 let monitor = ChannelMonitor::new(Secp256k1::new(), keys,
2897                                                   &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap()), 0, &Script::new(),
2898                                                   (OutPoint { txid: Txid::from_slice(&[43; 32]).unwrap(), index: 0 }, Script::new()),
2899                                                   &channel_parameters,
2900                                                   Script::new(), 46, 0,
2901                                                   HolderCommitmentTransaction::dummy(), best_block);
2902
2903                 monitor.provide_latest_holder_commitment_tx(HolderCommitmentTransaction::dummy(), preimages_to_holder_htlcs!(preimages[0..10])).unwrap();
2904                 let dummy_txid = dummy_tx.txid();
2905                 monitor.provide_latest_counterparty_commitment_tx(dummy_txid, preimages_slice_to_htlc_outputs!(preimages[5..15]), 281474976710655, dummy_key, &logger);
2906                 monitor.provide_latest_counterparty_commitment_tx(dummy_txid, preimages_slice_to_htlc_outputs!(preimages[15..20]), 281474976710654, dummy_key, &logger);
2907                 monitor.provide_latest_counterparty_commitment_tx(dummy_txid, preimages_slice_to_htlc_outputs!(preimages[17..20]), 281474976710653, dummy_key, &logger);
2908                 monitor.provide_latest_counterparty_commitment_tx(dummy_txid, preimages_slice_to_htlc_outputs!(preimages[18..20]), 281474976710652, dummy_key, &logger);
2909                 for &(ref preimage, ref hash) in preimages.iter() {
2910                         monitor.provide_payment_preimage(hash, preimage, &broadcaster, &fee_estimator, &logger);
2911                 }
2912
2913                 // Now provide a secret, pruning preimages 10-15
2914                 let mut secret = [0; 32];
2915                 secret[0..32].clone_from_slice(&hex::decode("7cc854b54e3e0dcdb010d7a3fee464a9687be6e8db3be6854c475621e007a5dc").unwrap());
2916                 monitor.provide_secret(281474976710655, secret.clone()).unwrap();
2917                 assert_eq!(monitor.inner.lock().unwrap().payment_preimages.len(), 15);
2918                 test_preimages_exist!(&preimages[0..10], monitor);
2919                 test_preimages_exist!(&preimages[15..20], monitor);
2920
2921                 // Now provide a further secret, pruning preimages 15-17
2922                 secret[0..32].clone_from_slice(&hex::decode("c7518c8ae4660ed02894df8976fa1a3659c1a8b4b5bec0c4b872abeba4cb8964").unwrap());
2923                 monitor.provide_secret(281474976710654, secret.clone()).unwrap();
2924                 assert_eq!(monitor.inner.lock().unwrap().payment_preimages.len(), 13);
2925                 test_preimages_exist!(&preimages[0..10], monitor);
2926                 test_preimages_exist!(&preimages[17..20], monitor);
2927
2928                 // Now update holder commitment tx info, pruning only element 18 as we still care about the
2929                 // previous commitment tx's preimages too
2930                 monitor.provide_latest_holder_commitment_tx(HolderCommitmentTransaction::dummy(), preimages_to_holder_htlcs!(preimages[0..5])).unwrap();
2931                 secret[0..32].clone_from_slice(&hex::decode("2273e227a5b7449b6e70f1fb4652864038b1cbf9cd7c043a7d6456b7fc275ad8").unwrap());
2932                 monitor.provide_secret(281474976710653, secret.clone()).unwrap();
2933                 assert_eq!(monitor.inner.lock().unwrap().payment_preimages.len(), 12);
2934                 test_preimages_exist!(&preimages[0..10], monitor);
2935                 test_preimages_exist!(&preimages[18..20], monitor);
2936
2937                 // But if we do it again, we'll prune 5-10
2938                 monitor.provide_latest_holder_commitment_tx(HolderCommitmentTransaction::dummy(), preimages_to_holder_htlcs!(preimages[0..3])).unwrap();
2939                 secret[0..32].clone_from_slice(&hex::decode("27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116").unwrap());
2940                 monitor.provide_secret(281474976710652, secret.clone()).unwrap();
2941                 assert_eq!(monitor.inner.lock().unwrap().payment_preimages.len(), 5);
2942                 test_preimages_exist!(&preimages[0..5], monitor);
2943         }
2944
2945         #[test]
2946         fn test_claim_txn_weight_computation() {
2947                 // We test Claim txn weight, knowing that we want expected weigth and
2948                 // not actual case to avoid sigs and time-lock delays hell variances.
2949
2950                 let secp_ctx = Secp256k1::new();
2951                 let privkey = SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap();
2952                 let pubkey = PublicKey::from_secret_key(&secp_ctx, &privkey);
2953                 let mut sum_actual_sigs = 0;
2954
2955                 macro_rules! sign_input {
2956                         ($sighash_parts: expr, $idx: expr, $amount: expr, $weight: expr, $sum_actual_sigs: expr) => {
2957                                 let htlc = HTLCOutputInCommitment {
2958                                         offered: if *$weight == WEIGHT_REVOKED_OFFERED_HTLC || *$weight == WEIGHT_OFFERED_HTLC { true } else { false },
2959                                         amount_msat: 0,
2960                                         cltv_expiry: 2 << 16,
2961                                         payment_hash: PaymentHash([1; 32]),
2962                                         transaction_output_index: Some($idx as u32),
2963                                 };
2964                                 let redeem_script = if *$weight == WEIGHT_REVOKED_OUTPUT { chan_utils::get_revokeable_redeemscript(&pubkey, 256, &pubkey) } else { chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, &pubkey, &pubkey, &pubkey) };
2965                                 let sighash = hash_to_message!(&$sighash_parts.signature_hash($idx, &redeem_script, $amount, SigHashType::All)[..]);
2966                                 let sig = secp_ctx.sign(&sighash, &privkey);
2967                                 $sighash_parts.access_witness($idx).push(sig.serialize_der().to_vec());
2968                                 $sighash_parts.access_witness($idx)[0].push(SigHashType::All as u8);
2969                                 sum_actual_sigs += $sighash_parts.access_witness($idx)[0].len();
2970                                 if *$weight == WEIGHT_REVOKED_OUTPUT {
2971                                         $sighash_parts.access_witness($idx).push(vec!(1));
2972                                 } else if *$weight == WEIGHT_REVOKED_OFFERED_HTLC || *$weight == WEIGHT_REVOKED_RECEIVED_HTLC {
2973                                         $sighash_parts.access_witness($idx).push(pubkey.clone().serialize().to_vec());
2974                                 } else if *$weight == WEIGHT_RECEIVED_HTLC {
2975                                         $sighash_parts.access_witness($idx).push(vec![0]);
2976                                 } else {
2977                                         $sighash_parts.access_witness($idx).push(PaymentPreimage([1; 32]).0.to_vec());
2978                                 }
2979                                 $sighash_parts.access_witness($idx).push(redeem_script.into_bytes());
2980                                 println!("witness[0] {}", $sighash_parts.access_witness($idx)[0].len());
2981                                 println!("witness[1] {}", $sighash_parts.access_witness($idx)[1].len());
2982                                 println!("witness[2] {}", $sighash_parts.access_witness($idx)[2].len());
2983                         }
2984                 }
2985
2986                 let script_pubkey = Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script();
2987                 let txid = Txid::from_hex("56944c5d3f98413ef45cf54545538103cc9f298e0575820ad3591376e2e0f65d").unwrap();
2988
2989                 // Justice tx with 1 to_holder, 2 revoked offered HTLCs, 1 revoked received HTLCs
2990                 let mut claim_tx = Transaction { version: 0, lock_time: 0, input: Vec::new(), output: Vec::new() };
2991                 for i in 0..4 {
2992                         claim_tx.input.push(TxIn {
2993                                 previous_output: BitcoinOutPoint {
2994                                         txid,
2995                                         vout: i,
2996                                 },
2997                                 script_sig: Script::new(),
2998                                 sequence: 0xfffffffd,
2999                                 witness: Vec::new(),
3000                         });
3001                 }
3002                 claim_tx.output.push(TxOut {
3003                         script_pubkey: script_pubkey.clone(),
3004                         value: 0,
3005                 });
3006                 let base_weight = claim_tx.get_weight();
3007                 let inputs_weight = vec![WEIGHT_REVOKED_OUTPUT, WEIGHT_REVOKED_OFFERED_HTLC, WEIGHT_REVOKED_OFFERED_HTLC, WEIGHT_REVOKED_RECEIVED_HTLC];
3008                 let mut inputs_total_weight = 2; // count segwit flags
3009                 {
3010                         let mut sighash_parts = bip143::SigHashCache::new(&mut claim_tx);
3011                         for (idx, inp) in inputs_weight.iter().enumerate() {
3012                                 sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs);
3013                                 inputs_total_weight += inp;
3014                         }
3015                 }
3016                 assert_eq!(base_weight + inputs_total_weight as usize,  claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_weight.len() - sum_actual_sigs));
3017
3018                 // Claim tx with 1 offered HTLCs, 3 received HTLCs
3019                 claim_tx.input.clear();
3020                 sum_actual_sigs = 0;
3021                 for i in 0..4 {
3022                         claim_tx.input.push(TxIn {
3023                                 previous_output: BitcoinOutPoint {
3024                                         txid,
3025                                         vout: i,
3026                                 },
3027                                 script_sig: Script::new(),
3028                                 sequence: 0xfffffffd,
3029                                 witness: Vec::new(),
3030                         });
3031                 }
3032                 let base_weight = claim_tx.get_weight();
3033                 let inputs_weight = vec![WEIGHT_OFFERED_HTLC, WEIGHT_RECEIVED_HTLC, WEIGHT_RECEIVED_HTLC, WEIGHT_RECEIVED_HTLC];
3034                 let mut inputs_total_weight = 2; // count segwit flags
3035                 {
3036                         let mut sighash_parts = bip143::SigHashCache::new(&mut claim_tx);
3037                         for (idx, inp) in inputs_weight.iter().enumerate() {
3038                                 sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs);
3039                                 inputs_total_weight += inp;
3040                         }
3041                 }
3042                 assert_eq!(base_weight + inputs_total_weight as usize,  claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_weight.len() - sum_actual_sigs));
3043
3044                 // Justice tx with 1 revoked HTLC-Success tx output
3045                 claim_tx.input.clear();
3046                 sum_actual_sigs = 0;
3047                 claim_tx.input.push(TxIn {
3048                         previous_output: BitcoinOutPoint {
3049                                 txid,
3050                                 vout: 0,
3051                         },
3052                         script_sig: Script::new(),
3053                         sequence: 0xfffffffd,
3054                         witness: Vec::new(),
3055                 });
3056                 let base_weight = claim_tx.get_weight();
3057                 let inputs_weight = vec![WEIGHT_REVOKED_OUTPUT];
3058                 let mut inputs_total_weight = 2; // count segwit flags
3059                 {
3060                         let mut sighash_parts = bip143::SigHashCache::new(&mut claim_tx);
3061                         for (idx, inp) in inputs_weight.iter().enumerate() {
3062                                 sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs);
3063                                 inputs_total_weight += inp;
3064                         }
3065                 }
3066                 assert_eq!(base_weight + inputs_total_weight as usize, claim_tx.get_weight() + /* max_length_isg */ (73 * inputs_weight.len() - sum_actual_sigs));
3067         }
3068
3069         // Further testing is done in the ChannelManager integration tests.
3070 }