]> git.bitcoin.ninja Git - rust-lightning/blob - lightning/src/ln/channelmanager.rs
450d014033a895656d39cec81f7daf0caeb142ab
[rust-lightning] / lightning / src / ln / channelmanager.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 top-level channel management and payment tracking stuff lives here.
11 //!
12 //! The ChannelManager is the main chunk of logic implementing the lightning protocol and is
13 //! responsible for tracking which channels are open, HTLCs are in flight and reestablishing those
14 //! upon reconnect to the relevant peer(s).
15 //!
16 //! It does not manage routing logic (see routing::router::get_route for that) nor does it manage constructing
17 //! on-chain transactions (it only monitors the chain to watch for any force-closes that might
18 //! imply it needs to fail HTLCs/payments/channels it manages).
19 //!
20
21 use bitcoin::blockdata::block::{Block, BlockHeader};
22 use bitcoin::blockdata::constants::genesis_block;
23 use bitcoin::network::constants::Network;
24
25 use bitcoin::hashes::{Hash, HashEngine};
26 use bitcoin::hashes::hmac::{Hmac, HmacEngine};
27 use bitcoin::hashes::sha256::Hash as Sha256;
28 use bitcoin::hashes::sha256d::Hash as Sha256dHash;
29 use bitcoin::hashes::cmp::fixed_time_eq;
30 use bitcoin::hash_types::BlockHash;
31
32 use bitcoin::secp256k1::key::{SecretKey,PublicKey};
33 use bitcoin::secp256k1::Secp256k1;
34 use bitcoin::secp256k1::ecdh::SharedSecret;
35 use bitcoin::secp256k1;
36
37 use chain;
38 use chain::Watch;
39 use chain::chaininterface::{BroadcasterInterface, FeeEstimator};
40 use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, ChannelMonitorUpdateErr, HTLC_FAIL_BACK_BUFFER, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ANTI_REORG_DELAY, MonitorEvent, CLOSED_CHANNEL_UPDATE_ID};
41 use chain::transaction::{OutPoint, TransactionData};
42 use ln::channel::{Channel, ChannelError};
43 use ln::features::{InitFeatures, NodeFeatures};
44 use routing::router::{Route, RouteHop};
45 use ln::msgs;
46 use ln::msgs::NetAddress;
47 use ln::onion_utils;
48 use ln::msgs::{ChannelMessageHandler, DecodeError, LightningError, OptionalField};
49 use chain::keysinterface::{Sign, KeysInterface, KeysManager, InMemorySigner};
50 use util::config::UserConfig;
51 use util::events::{Event, EventsProvider, MessageSendEvent, MessageSendEventsProvider};
52 use util::{byte_utils, events};
53 use util::ser::{Readable, ReadableArgs, MaybeReadable, Writeable, Writer};
54 use util::chacha20::{ChaCha20, ChaChaReader};
55 use util::logger::Logger;
56 use util::errors::APIError;
57
58 use std::{cmp, mem};
59 use std::collections::{HashMap, hash_map, HashSet};
60 use std::io::{Cursor, Read};
61 use std::sync::{Arc, Condvar, Mutex, MutexGuard, RwLock, RwLockReadGuard};
62 use std::sync::atomic::{AtomicUsize, Ordering};
63 use std::time::Duration;
64 #[cfg(any(test, feature = "allow_wallclock_use"))]
65 use std::time::Instant;
66 use std::marker::{Sync, Send};
67 use std::ops::Deref;
68 use bitcoin::hashes::hex::ToHex;
69
70 // We hold various information about HTLC relay in the HTLC objects in Channel itself:
71 //
72 // Upon receipt of an HTLC from a peer, we'll give it a PendingHTLCStatus indicating if it should
73 // forward the HTLC with information it will give back to us when it does so, or if it should Fail
74 // the HTLC with the relevant message for the Channel to handle giving to the remote peer.
75 //
76 // Once said HTLC is committed in the Channel, if the PendingHTLCStatus indicated Forward, the
77 // Channel will return the PendingHTLCInfo back to us, and we will create an HTLCForwardInfo
78 // with it to track where it came from (in case of onwards-forward error), waiting a random delay
79 // before we forward it.
80 //
81 // We will then use HTLCForwardInfo's PendingHTLCInfo to construct an outbound HTLC, with a
82 // relevant HTLCSource::PreviousHopData filled in to indicate where it came from (which we can use
83 // to either fail-backwards or fulfill the HTLC backwards along the relevant path).
84 // Alternatively, we can fill an outbound HTLC with a HTLCSource::OutboundRoute indicating this is
85 // our payment, which we can use to decode errors or inform the user that the payment was sent.
86
87 #[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
88 enum PendingHTLCRouting {
89         Forward {
90                 onion_packet: msgs::OnionPacket,
91                 short_channel_id: u64, // This should be NonZero<u64> eventually when we bump MSRV
92         },
93         Receive {
94                 payment_data: Option<msgs::FinalOnionHopData>,
95                 incoming_cltv_expiry: u32, // Used to track when we should expire pending HTLCs that go unclaimed
96         },
97 }
98
99 #[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
100 pub(super) struct PendingHTLCInfo {
101         routing: PendingHTLCRouting,
102         incoming_shared_secret: [u8; 32],
103         payment_hash: PaymentHash,
104         pub(super) amt_to_forward: u64,
105         pub(super) outgoing_cltv_value: u32,
106 }
107
108 #[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
109 pub(super) enum HTLCFailureMsg {
110         Relay(msgs::UpdateFailHTLC),
111         Malformed(msgs::UpdateFailMalformedHTLC),
112 }
113
114 /// Stores whether we can't forward an HTLC or relevant forwarding info
115 #[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
116 pub(super) enum PendingHTLCStatus {
117         Forward(PendingHTLCInfo),
118         Fail(HTLCFailureMsg),
119 }
120
121 pub(super) enum HTLCForwardInfo {
122         AddHTLC {
123                 forward_info: PendingHTLCInfo,
124
125                 // These fields are produced in `forward_htlcs()` and consumed in
126                 // `process_pending_htlc_forwards()` for constructing the
127                 // `HTLCSource::PreviousHopData` for failed and forwarded
128                 // HTLCs.
129                 prev_short_channel_id: u64,
130                 prev_htlc_id: u64,
131                 prev_funding_outpoint: OutPoint,
132         },
133         FailHTLC {
134                 htlc_id: u64,
135                 err_packet: msgs::OnionErrorPacket,
136         },
137 }
138
139 /// Tracks the inbound corresponding to an outbound HTLC
140 #[derive(Clone, PartialEq)]
141 pub(crate) struct HTLCPreviousHopData {
142         short_channel_id: u64,
143         htlc_id: u64,
144         incoming_packet_shared_secret: [u8; 32],
145
146         // This field is consumed by `claim_funds_from_hop()` when updating a force-closed backwards
147         // channel with a preimage provided by the forward channel.
148         outpoint: OutPoint,
149 }
150
151 struct ClaimableHTLC {
152         prev_hop: HTLCPreviousHopData,
153         value: u64,
154         /// Filled in when the HTLC was received with a payment_secret packet, which contains a
155         /// total_msat (which may differ from value if this is a Multi-Path Payment) and a
156         /// payment_secret which prevents path-probing attacks and can associate different HTLCs which
157         /// are part of the same payment.
158         payment_data: Option<msgs::FinalOnionHopData>,
159         cltv_expiry: u32,
160 }
161
162 /// Tracks the inbound corresponding to an outbound HTLC
163 #[derive(Clone, PartialEq)]
164 pub(crate) enum HTLCSource {
165         PreviousHopData(HTLCPreviousHopData),
166         OutboundRoute {
167                 path: Vec<RouteHop>,
168                 session_priv: SecretKey,
169                 /// Technically we can recalculate this from the route, but we cache it here to avoid
170                 /// doing a double-pass on route when we get a failure back
171                 first_hop_htlc_msat: u64,
172         },
173 }
174 #[cfg(test)]
175 impl HTLCSource {
176         pub fn dummy() -> Self {
177                 HTLCSource::OutboundRoute {
178                         path: Vec::new(),
179                         session_priv: SecretKey::from_slice(&[1; 32]).unwrap(),
180                         first_hop_htlc_msat: 0,
181                 }
182         }
183 }
184
185 #[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
186 pub(super) enum HTLCFailReason {
187         LightningError {
188                 err: msgs::OnionErrorPacket,
189         },
190         Reason {
191                 failure_code: u16,
192                 data: Vec<u8>,
193         }
194 }
195
196 /// payment_hash type, use to cross-lock hop
197 /// (C-not exported) as we just use [u8; 32] directly
198 #[derive(Hash, Copy, Clone, PartialEq, Eq, Debug)]
199 pub struct PaymentHash(pub [u8;32]);
200 /// payment_preimage type, use to route payment between hop
201 /// (C-not exported) as we just use [u8; 32] directly
202 #[derive(Hash, Copy, Clone, PartialEq, Eq, Debug)]
203 pub struct PaymentPreimage(pub [u8;32]);
204 /// payment_secret type, use to authenticate sender to the receiver and tie MPP HTLCs together
205 /// (C-not exported) as we just use [u8; 32] directly
206 #[derive(Hash, Copy, Clone, PartialEq, Eq, Debug)]
207 pub struct PaymentSecret(pub [u8;32]);
208
209 type ShutdownResult = (Option<(OutPoint, ChannelMonitorUpdate)>, Vec<(HTLCSource, PaymentHash)>);
210
211 /// Error type returned across the channel_state mutex boundary. When an Err is generated for a
212 /// Channel, we generally end up with a ChannelError::Close for which we have to close the channel
213 /// immediately (ie with no further calls on it made). Thus, this step happens inside a
214 /// channel_state lock. We then return the set of things that need to be done outside the lock in
215 /// this struct and call handle_error!() on it.
216
217 struct MsgHandleErrInternal {
218         err: msgs::LightningError,
219         shutdown_finish: Option<(ShutdownResult, Option<msgs::ChannelUpdate>)>,
220 }
221 impl MsgHandleErrInternal {
222         #[inline]
223         fn send_err_msg_no_close(err: String, channel_id: [u8; 32]) -> Self {
224                 Self {
225                         err: LightningError {
226                                 err: err.clone(),
227                                 action: msgs::ErrorAction::SendErrorMessage {
228                                         msg: msgs::ErrorMessage {
229                                                 channel_id,
230                                                 data: err
231                                         },
232                                 },
233                         },
234                         shutdown_finish: None,
235                 }
236         }
237         #[inline]
238         fn ignore_no_close(err: String) -> Self {
239                 Self {
240                         err: LightningError {
241                                 err,
242                                 action: msgs::ErrorAction::IgnoreError,
243                         },
244                         shutdown_finish: None,
245                 }
246         }
247         #[inline]
248         fn from_no_close(err: msgs::LightningError) -> Self {
249                 Self { err, shutdown_finish: None }
250         }
251         #[inline]
252         fn from_finish_shutdown(err: String, channel_id: [u8; 32], shutdown_res: ShutdownResult, channel_update: Option<msgs::ChannelUpdate>) -> Self {
253                 Self {
254                         err: LightningError {
255                                 err: err.clone(),
256                                 action: msgs::ErrorAction::SendErrorMessage {
257                                         msg: msgs::ErrorMessage {
258                                                 channel_id,
259                                                 data: err
260                                         },
261                                 },
262                         },
263                         shutdown_finish: Some((shutdown_res, channel_update)),
264                 }
265         }
266         #[inline]
267         fn from_chan_no_close(err: ChannelError, channel_id: [u8; 32]) -> Self {
268                 Self {
269                         err: match err {
270                                 ChannelError::Ignore(msg) => LightningError {
271                                         err: msg,
272                                         action: msgs::ErrorAction::IgnoreError,
273                                 },
274                                 ChannelError::Close(msg) => LightningError {
275                                         err: msg.clone(),
276                                         action: msgs::ErrorAction::SendErrorMessage {
277                                                 msg: msgs::ErrorMessage {
278                                                         channel_id,
279                                                         data: msg
280                                                 },
281                                         },
282                                 },
283                                 ChannelError::CloseDelayBroadcast(msg) => LightningError {
284                                         err: msg.clone(),
285                                         action: msgs::ErrorAction::SendErrorMessage {
286                                                 msg: msgs::ErrorMessage {
287                                                         channel_id,
288                                                         data: msg
289                                                 },
290                                         },
291                                 },
292                         },
293                         shutdown_finish: None,
294                 }
295         }
296 }
297
298 /// We hold back HTLCs we intend to relay for a random interval greater than this (see
299 /// Event::PendingHTLCsForwardable for the API guidelines indicating how long should be waited).
300 /// This provides some limited amount of privacy. Ideally this would range from somewhere like one
301 /// second to 30 seconds, but people expect lightning to be, you know, kinda fast, sadly.
302 const MIN_HTLC_RELAY_HOLDING_CELL_MILLIS: u64 = 100;
303
304 /// For events which result in both a RevokeAndACK and a CommitmentUpdate, by default they should
305 /// be sent in the order they appear in the return value, however sometimes the order needs to be
306 /// variable at runtime (eg Channel::channel_reestablish needs to re-send messages in the order
307 /// they were originally sent). In those cases, this enum is also returned.
308 #[derive(Clone, PartialEq)]
309 pub(super) enum RAACommitmentOrder {
310         /// Send the CommitmentUpdate messages first
311         CommitmentFirst,
312         /// Send the RevokeAndACK message first
313         RevokeAndACKFirst,
314 }
315
316 // Note this is only exposed in cfg(test):
317 pub(super) struct ChannelHolder<Signer: Sign> {
318         pub(super) by_id: HashMap<[u8; 32], Channel<Signer>>,
319         pub(super) short_to_id: HashMap<u64, [u8; 32]>,
320         /// short channel id -> forward infos. Key of 0 means payments received
321         /// Note that while this is held in the same mutex as the channels themselves, no consistency
322         /// guarantees are made about the existence of a channel with the short id here, nor the short
323         /// ids in the PendingHTLCInfo!
324         pub(super) forward_htlcs: HashMap<u64, Vec<HTLCForwardInfo>>,
325         /// (payment_hash, payment_secret) -> Vec<HTLCs> for tracking HTLCs that
326         /// were to us and can be failed/claimed by the user
327         /// Note that while this is held in the same mutex as the channels themselves, no consistency
328         /// guarantees are made about the channels given here actually existing anymore by the time you
329         /// go to read them!
330         claimable_htlcs: HashMap<(PaymentHash, Option<PaymentSecret>), Vec<ClaimableHTLC>>,
331         /// Messages to send to peers - pushed to in the same lock that they are generated in (except
332         /// for broadcast messages, where ordering isn't as strict).
333         pub(super) pending_msg_events: Vec<MessageSendEvent>,
334 }
335
336 /// Events which we process internally but cannot be procsesed immediately at the generation site
337 /// for some reason. They are handled in timer_chan_freshness_every_min, so may be processed with
338 /// quite some time lag.
339 enum BackgroundEvent {
340         /// Handle a ChannelMonitorUpdate that closes a channel, broadcasting its current latest holder
341         /// commitment transaction.
342         ClosingMonitorUpdate((OutPoint, ChannelMonitorUpdate)),
343 }
344
345 /// State we hold per-peer. In the future we should put channels in here, but for now we only hold
346 /// the latest Init features we heard from the peer.
347 struct PeerState {
348         latest_features: InitFeatures,
349 }
350
351 #[cfg(not(any(target_pointer_width = "32", target_pointer_width = "64")))]
352 const ERR: () = "You need at least 32 bit pointers (well, usize, but we'll assume they're the same) for ChannelManager::latest_block_height";
353
354 /// SimpleArcChannelManager is useful when you need a ChannelManager with a static lifetime, e.g.
355 /// when you're using lightning-net-tokio (since tokio::spawn requires parameters with static
356 /// lifetimes). Other times you can afford a reference, which is more efficient, in which case
357 /// SimpleRefChannelManager is the more appropriate type. Defining these type aliases prevents
358 /// issues such as overly long function definitions. Note that the ChannelManager can take any
359 /// type that implements KeysInterface for its keys manager, but this type alias chooses the
360 /// concrete type of the KeysManager.
361 pub type SimpleArcChannelManager<M, T, F, L> = ChannelManager<InMemorySigner, Arc<M>, Arc<T>, Arc<KeysManager>, Arc<F>, Arc<L>>;
362
363 /// SimpleRefChannelManager is a type alias for a ChannelManager reference, and is the reference
364 /// counterpart to the SimpleArcChannelManager type alias. Use this type by default when you don't
365 /// need a ChannelManager with a static lifetime. You'll need a static lifetime in cases such as
366 /// usage of lightning-net-tokio (since tokio::spawn requires parameters with static lifetimes).
367 /// But if this is not necessary, using a reference is more efficient. Defining these type aliases
368 /// helps with issues such as long function definitions. Note that the ChannelManager can take any
369 /// type that implements KeysInterface for its keys manager, but this type alias chooses the
370 /// concrete type of the KeysManager.
371 pub type SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, M, T, F, L> = ChannelManager<InMemorySigner, &'a M, &'b T, &'c KeysManager, &'d F, &'e L>;
372
373 /// Manager which keeps track of a number of channels and sends messages to the appropriate
374 /// channel, also tracking HTLC preimages and forwarding onion packets appropriately.
375 ///
376 /// Implements ChannelMessageHandler, handling the multi-channel parts and passing things through
377 /// to individual Channels.
378 ///
379 /// Implements Writeable to write out all channel state to disk. Implies peer_disconnected() for
380 /// all peers during write/read (though does not modify this instance, only the instance being
381 /// serialized). This will result in any channels which have not yet exchanged funding_created (ie
382 /// called funding_transaction_generated for outbound channels).
383 ///
384 /// Note that you can be a bit lazier about writing out ChannelManager than you can be with
385 /// ChannelMonitors. With ChannelMonitors you MUST write each monitor update out to disk before
386 /// returning from chain::Watch::watch_/update_channel, with ChannelManagers, writing updates
387 /// happens out-of-band (and will prevent any other ChannelManager operations from occurring during
388 /// the serialization process). If the deserialized version is out-of-date compared to the
389 /// ChannelMonitors passed by reference to read(), those channels will be force-closed based on the
390 /// ChannelMonitor state and no funds will be lost (mod on-chain transaction fees).
391 ///
392 /// Note that the deserializer is only implemented for (BlockHash, ChannelManager), which
393 /// tells you the last block hash which was block_connect()ed. You MUST rescan any blocks along
394 /// the "reorg path" (ie call block_disconnected() until you get to a common block and then call
395 /// block_connected() to step towards your best block) upon deserialization before using the
396 /// object!
397 ///
398 /// Note that ChannelManager is responsible for tracking liveness of its channels and generating
399 /// ChannelUpdate messages informing peers that the channel is temporarily disabled. To avoid
400 /// spam due to quick disconnection/reconnection, updates are not sent until the channel has been
401 /// offline for a full minute. In order to track this, you must call
402 /// timer_chan_freshness_every_min roughly once per minute, though it doesn't have to be perfect.
403 ///
404 /// Rather than using a plain ChannelManager, it is preferable to use either a SimpleArcChannelManager
405 /// a SimpleRefChannelManager, for conciseness. See their documentation for more details, but
406 /// essentially you should default to using a SimpleRefChannelManager, and use a
407 /// SimpleArcChannelManager when you require a ChannelManager with a static lifetime, such as when
408 /// you're using lightning-net-tokio.
409 pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
410         where M::Target: chain::Watch<Signer>,
411         T::Target: BroadcasterInterface,
412         K::Target: KeysInterface<Signer = Signer>,
413         F::Target: FeeEstimator,
414                                 L::Target: Logger,
415 {
416         default_configuration: UserConfig,
417         genesis_hash: BlockHash,
418         fee_estimator: F,
419         chain_monitor: M,
420         tx_broadcaster: T,
421
422         #[cfg(test)]
423         pub(super) latest_block_height: AtomicUsize,
424         #[cfg(not(test))]
425         latest_block_height: AtomicUsize,
426         last_block_hash: RwLock<BlockHash>,
427         secp_ctx: Secp256k1<secp256k1::All>,
428
429         #[cfg(any(test, feature = "_test_utils"))]
430         pub(super) channel_state: Mutex<ChannelHolder<Signer>>,
431         #[cfg(not(any(test, feature = "_test_utils")))]
432         channel_state: Mutex<ChannelHolder<Signer>>,
433         our_network_key: SecretKey,
434
435         /// Used to track the last value sent in a node_announcement "timestamp" field. We ensure this
436         /// value increases strictly since we don't assume access to a time source.
437         last_node_announcement_serial: AtomicUsize,
438
439         /// The bulk of our storage will eventually be here (channels and message queues and the like).
440         /// If we are connected to a peer we always at least have an entry here, even if no channels
441         /// are currently open with that peer.
442         /// Because adding or removing an entry is rare, we usually take an outer read lock and then
443         /// operate on the inner value freely. Sadly, this prevents parallel operation when opening a
444         /// new channel.
445         per_peer_state: RwLock<HashMap<PublicKey, Mutex<PeerState>>>,
446
447         pending_events: Mutex<Vec<events::Event>>,
448         pending_background_events: Mutex<Vec<BackgroundEvent>>,
449         /// Used when we have to take a BIG lock to make sure everything is self-consistent.
450         /// Essentially just when we're serializing ourselves out.
451         /// Taken first everywhere where we are making changes before any other locks.
452         /// When acquiring this lock in read mode, rather than acquiring it directly, call
453         /// `PersistenceNotifierGuard::new(..)` and pass the lock to it, to ensure the PersistenceNotifier
454         /// the lock contains sends out a notification when the lock is released.
455         total_consistency_lock: RwLock<()>,
456
457         persistence_notifier: PersistenceNotifier,
458
459         keys_manager: K,
460
461         logger: L,
462 }
463
464 /// Chain-related parameters used to construct a new `ChannelManager`.
465 ///
466 /// Typically, the block-specific parameters are derived from the best block hash for the network,
467 /// as a newly constructed `ChannelManager` will not have created any channels yet. These parameters
468 /// are not needed when deserializing a previously constructed `ChannelManager`.
469 pub struct ChainParameters {
470         /// The network for determining the `chain_hash` in Lightning messages.
471         pub network: Network,
472
473         /// The hash of the latest block successfully connected.
474         pub latest_hash: BlockHash,
475
476         /// The height of the latest block successfully connected.
477         ///
478         /// Used to track on-chain channel funding outputs and send payments with reliable timelocks.
479         pub latest_height: usize,
480 }
481
482 /// Whenever we release the `ChannelManager`'s `total_consistency_lock`, from read mode, it is
483 /// desirable to notify any listeners on `wait_timeout`/`wait` that new updates are available for
484 /// persistence. Therefore, this struct is responsible for locking the total consistency lock and,
485 /// upon going out of scope, sending the aforementioned notification (since the lock being released
486 /// indicates that the updates are ready for persistence).
487 struct PersistenceNotifierGuard<'a> {
488         persistence_notifier: &'a PersistenceNotifier,
489         // We hold onto this result so the lock doesn't get released immediately.
490         _read_guard: RwLockReadGuard<'a, ()>,
491 }
492
493 impl<'a> PersistenceNotifierGuard<'a> {
494         fn new(lock: &'a RwLock<()>, notifier: &'a PersistenceNotifier) -> Self {
495                 let read_guard = lock.read().unwrap();
496
497                 Self {
498                         persistence_notifier: notifier,
499                         _read_guard: read_guard,
500                 }
501         }
502 }
503
504 impl<'a> Drop for PersistenceNotifierGuard<'a> {
505         fn drop(&mut self) {
506                 self.persistence_notifier.notify();
507         }
508 }
509
510 /// The amount of time we require our counterparty wait to claim their money (ie time between when
511 /// we, or our watchtower, must check for them having broadcast a theft transaction).
512 pub(crate) const BREAKDOWN_TIMEOUT: u16 = 6 * 24;
513 /// The amount of time we're willing to wait to claim money back to us
514 pub(crate) const MAX_LOCAL_BREAKDOWN_TIMEOUT: u16 = 6 * 24 * 7;
515
516 /// The minimum number of blocks between an inbound HTLC's CLTV and the corresponding outbound
517 /// HTLC's CLTV. This should always be a few blocks greater than channelmonitor::CLTV_CLAIM_BUFFER,
518 /// ie the node we forwarded the payment on to should always have enough room to reliably time out
519 /// the HTLC via a full update_fail_htlc/commitment_signed dance before we hit the
520 /// CLTV_CLAIM_BUFFER point (we static assert that it's at least 3 blocks more).
521 const CLTV_EXPIRY_DELTA: u16 = 6 * 12; //TODO?
522 pub(super) const CLTV_FAR_FAR_AWAY: u32 = 6 * 24 * 7; //TODO?
523
524 // Check that our CLTV_EXPIRY is at least CLTV_CLAIM_BUFFER + ANTI_REORG_DELAY + LATENCY_GRACE_PERIOD_BLOCKS,
525 // ie that if the next-hop peer fails the HTLC within
526 // LATENCY_GRACE_PERIOD_BLOCKS then we'll still have CLTV_CLAIM_BUFFER left to timeout it onchain,
527 // then waiting ANTI_REORG_DELAY to be reorg-safe on the outbound HLTC and
528 // failing the corresponding htlc backward, and us now seeing the last block of ANTI_REORG_DELAY before
529 // LATENCY_GRACE_PERIOD_BLOCKS.
530 #[deny(const_err)]
531 #[allow(dead_code)]
532 const CHECK_CLTV_EXPIRY_SANITY: u32 = CLTV_EXPIRY_DELTA as u32 - LATENCY_GRACE_PERIOD_BLOCKS - CLTV_CLAIM_BUFFER - ANTI_REORG_DELAY - LATENCY_GRACE_PERIOD_BLOCKS;
533
534 // Check for ability of an attacker to make us fail on-chain by delaying inbound claim. See
535 // ChannelMontior::would_broadcast_at_height for a description of why this is needed.
536 #[deny(const_err)]
537 #[allow(dead_code)]
538 const CHECK_CLTV_EXPIRY_SANITY_2: u32 = CLTV_EXPIRY_DELTA as u32 - LATENCY_GRACE_PERIOD_BLOCKS - 2*CLTV_CLAIM_BUFFER;
539
540 /// Details of a channel, as returned by ChannelManager::list_channels and ChannelManager::list_usable_channels
541 #[derive(Clone)]
542 pub struct ChannelDetails {
543         /// The channel's ID (prior to funding transaction generation, this is a random 32 bytes,
544         /// thereafter this is the txid of the funding transaction xor the funding transaction output).
545         /// Note that this means this value is *not* persistent - it can change once during the
546         /// lifetime of the channel.
547         pub channel_id: [u8; 32],
548         /// The position of the funding transaction in the chain. None if the funding transaction has
549         /// not yet been confirmed and the channel fully opened.
550         pub short_channel_id: Option<u64>,
551         /// The node_id of our counterparty
552         pub remote_network_id: PublicKey,
553         /// The Features the channel counterparty provided upon last connection.
554         /// Useful for routing as it is the most up-to-date copy of the counterparty's features and
555         /// many routing-relevant features are present in the init context.
556         pub counterparty_features: InitFeatures,
557         /// The value, in satoshis, of this channel as appears in the funding output
558         pub channel_value_satoshis: u64,
559         /// The user_id passed in to create_channel, or 0 if the channel was inbound.
560         pub user_id: u64,
561         /// The available outbound capacity for sending HTLCs to the remote peer. This does not include
562         /// any pending HTLCs which are not yet fully resolved (and, thus, who's balance is not
563         /// available for inclusion in new outbound HTLCs). This further does not include any pending
564         /// outgoing HTLCs which are awaiting some other resolution to be sent.
565         pub outbound_capacity_msat: u64,
566         /// The available inbound capacity for the remote peer to send HTLCs to us. This does not
567         /// include any pending HTLCs which are not yet fully resolved (and, thus, who's balance is not
568         /// available for inclusion in new inbound HTLCs).
569         /// Note that there are some corner cases not fully handled here, so the actual available
570         /// inbound capacity may be slightly higher than this.
571         pub inbound_capacity_msat: u64,
572         /// True if the channel is (a) confirmed and funding_locked messages have been exchanged, (b)
573         /// the peer is connected, and (c) no monitor update failure is pending resolution.
574         pub is_live: bool,
575 }
576
577 /// If a payment fails to send, it can be in one of several states. This enum is returned as the
578 /// Err() type describing which state the payment is in, see the description of individual enum
579 /// states for more.
580 #[derive(Clone, Debug)]
581 pub enum PaymentSendFailure {
582         /// A parameter which was passed to send_payment was invalid, preventing us from attempting to
583         /// send the payment at all. No channel state has been changed or messages sent to peers, and
584         /// once you've changed the parameter at error, you can freely retry the payment in full.
585         ParameterError(APIError),
586         /// A parameter in a single path which was passed to send_payment was invalid, preventing us
587         /// from attempting to send the payment at all. No channel state has been changed or messages
588         /// sent to peers, and once you've changed the parameter at error, you can freely retry the
589         /// payment in full.
590         ///
591         /// The results here are ordered the same as the paths in the route object which was passed to
592         /// send_payment.
593         PathParameterError(Vec<Result<(), APIError>>),
594         /// All paths which were attempted failed to send, with no channel state change taking place.
595         /// You can freely retry the payment in full (though you probably want to do so over different
596         /// paths than the ones selected).
597         AllFailedRetrySafe(Vec<APIError>),
598         /// Some paths which were attempted failed to send, though possibly not all. At least some
599         /// paths have irrevocably committed to the HTLC and retrying the payment in full would result
600         /// in over-/re-payment.
601         ///
602         /// The results here are ordered the same as the paths in the route object which was passed to
603         /// send_payment, and any Errs which are not APIError::MonitorUpdateFailed can be safely
604         /// retried (though there is currently no API with which to do so).
605         ///
606         /// Any entries which contain Err(APIError::MonitorUpdateFailed) or Ok(()) MUST NOT be retried
607         /// as they will result in over-/re-payment. These HTLCs all either successfully sent (in the
608         /// case of Ok(())) or will send once channel_monitor_updated is called on the next-hop channel
609         /// with the latest update_id.
610         PartialFailure(Vec<Result<(), APIError>>),
611 }
612
613 macro_rules! handle_error {
614         ($self: ident, $internal: expr, $counterparty_node_id: expr) => {
615                 match $internal {
616                         Ok(msg) => Ok(msg),
617                         Err(MsgHandleErrInternal { err, shutdown_finish }) => {
618                                 #[cfg(debug_assertions)]
619                                 {
620                                         // In testing, ensure there are no deadlocks where the lock is already held upon
621                                         // entering the macro.
622                                         assert!($self.channel_state.try_lock().is_ok());
623                                 }
624
625                                 let mut msg_events = Vec::with_capacity(2);
626
627                                 if let Some((shutdown_res, update_option)) = shutdown_finish {
628                                         $self.finish_force_close_channel(shutdown_res);
629                                         if let Some(update) = update_option {
630                                                 msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
631                                                         msg: update
632                                                 });
633                                         }
634                                 }
635
636                                 log_error!($self.logger, "{}", err.err);
637                                 if let msgs::ErrorAction::IgnoreError = err.action {
638                                 } else {
639                                         msg_events.push(events::MessageSendEvent::HandleError {
640                                                 node_id: $counterparty_node_id,
641                                                 action: err.action.clone()
642                                         });
643                                 }
644
645                                 if !msg_events.is_empty() {
646                                         $self.channel_state.lock().unwrap().pending_msg_events.append(&mut msg_events);
647                                 }
648
649                                 // Return error in case higher-API need one
650                                 Err(err)
651                         },
652                 }
653         }
654 }
655
656 macro_rules! break_chan_entry {
657         ($self: ident, $res: expr, $channel_state: expr, $entry: expr) => {
658                 match $res {
659                         Ok(res) => res,
660                         Err(ChannelError::Ignore(msg)) => {
661                                 break Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), $entry.key().clone()))
662                         },
663                         Err(ChannelError::Close(msg)) => {
664                                 log_trace!($self.logger, "Closing channel {} due to Close-required error: {}", log_bytes!($entry.key()[..]), msg);
665                                 let (channel_id, mut chan) = $entry.remove_entry();
666                                 if let Some(short_id) = chan.get_short_channel_id() {
667                                         $channel_state.short_to_id.remove(&short_id);
668                                 }
669                                 break Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, chan.force_shutdown(true), $self.get_channel_update(&chan).ok()))
670                         },
671                         Err(ChannelError::CloseDelayBroadcast(_)) => { panic!("Wait is only generated on receipt of channel_reestablish, which is handled by try_chan_entry, we don't bother to support it here"); }
672                 }
673         }
674 }
675
676 macro_rules! try_chan_entry {
677         ($self: ident, $res: expr, $channel_state: expr, $entry: expr) => {
678                 match $res {
679                         Ok(res) => res,
680                         Err(ChannelError::Ignore(msg)) => {
681                                 return Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), $entry.key().clone()))
682                         },
683                         Err(ChannelError::Close(msg)) => {
684                                 log_trace!($self.logger, "Closing channel {} due to Close-required error: {}", log_bytes!($entry.key()[..]), msg);
685                                 let (channel_id, mut chan) = $entry.remove_entry();
686                                 if let Some(short_id) = chan.get_short_channel_id() {
687                                         $channel_state.short_to_id.remove(&short_id);
688                                 }
689                                 return Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, chan.force_shutdown(true), $self.get_channel_update(&chan).ok()))
690                         },
691                         Err(ChannelError::CloseDelayBroadcast(msg)) => {
692                                 log_error!($self.logger, "Channel {} need to be shutdown but closing transactions not broadcast due to {}", log_bytes!($entry.key()[..]), msg);
693                                 let (channel_id, mut chan) = $entry.remove_entry();
694                                 if let Some(short_id) = chan.get_short_channel_id() {
695                                         $channel_state.short_to_id.remove(&short_id);
696                                 }
697                                 let shutdown_res = chan.force_shutdown(false);
698                                 return Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, shutdown_res, $self.get_channel_update(&chan).ok()))
699                         }
700                 }
701         }
702 }
703
704 macro_rules! handle_monitor_err {
705         ($self: ident, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr) => {
706                 handle_monitor_err!($self, $err, $channel_state, $entry, $action_type, $resend_raa, $resend_commitment, Vec::new(), Vec::new())
707         };
708         ($self: ident, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr, $failed_forwards: expr, $failed_fails: expr) => {
709                 match $err {
710                         ChannelMonitorUpdateErr::PermanentFailure => {
711                                 log_error!($self.logger, "Closing channel {} due to monitor update PermanentFailure", log_bytes!($entry.key()[..]));
712                                 let (channel_id, mut chan) = $entry.remove_entry();
713                                 if let Some(short_id) = chan.get_short_channel_id() {
714                                         $channel_state.short_to_id.remove(&short_id);
715                                 }
716                                 // TODO: $failed_fails is dropped here, which will cause other channels to hit the
717                                 // chain in a confused state! We need to move them into the ChannelMonitor which
718                                 // will be responsible for failing backwards once things confirm on-chain.
719                                 // It's ok that we drop $failed_forwards here - at this point we'd rather they
720                                 // broadcast HTLC-Timeout and pay the associated fees to get their funds back than
721                                 // us bother trying to claim it just to forward on to another peer. If we're
722                                 // splitting hairs we'd prefer to claim payments that were to us, but we haven't
723                                 // given up the preimage yet, so might as well just wait until the payment is
724                                 // retried, avoiding the on-chain fees.
725                                 let res: Result<(), _> = Err(MsgHandleErrInternal::from_finish_shutdown("ChannelMonitor storage failure".to_owned(), channel_id, chan.force_shutdown(true), $self.get_channel_update(&chan).ok()));
726                                 res
727                         },
728                         ChannelMonitorUpdateErr::TemporaryFailure => {
729                                 log_info!($self.logger, "Disabling channel {} due to monitor update TemporaryFailure. On restore will send {} and process {} forwards and {} fails",
730                                                 log_bytes!($entry.key()[..]),
731                                                 if $resend_commitment && $resend_raa {
732                                                                 match $action_type {
733                                                                         RAACommitmentOrder::CommitmentFirst => { "commitment then RAA" },
734                                                                         RAACommitmentOrder::RevokeAndACKFirst => { "RAA then commitment" },
735                                                                 }
736                                                         } else if $resend_commitment { "commitment" }
737                                                         else if $resend_raa { "RAA" }
738                                                         else { "nothing" },
739                                                 (&$failed_forwards as &Vec<(PendingHTLCInfo, u64)>).len(),
740                                                 (&$failed_fails as &Vec<(HTLCSource, PaymentHash, HTLCFailReason)>).len());
741                                 if !$resend_commitment {
742                                         debug_assert!($action_type == RAACommitmentOrder::RevokeAndACKFirst || !$resend_raa);
743                                 }
744                                 if !$resend_raa {
745                                         debug_assert!($action_type == RAACommitmentOrder::CommitmentFirst || !$resend_commitment);
746                                 }
747                                 $entry.get_mut().monitor_update_failed($resend_raa, $resend_commitment, $failed_forwards, $failed_fails);
748                                 Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore("Failed to update ChannelMonitor".to_owned()), *$entry.key()))
749                         },
750                 }
751         }
752 }
753
754 macro_rules! return_monitor_err {
755         ($self: ident, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr) => {
756                 return handle_monitor_err!($self, $err, $channel_state, $entry, $action_type, $resend_raa, $resend_commitment);
757         };
758         ($self: ident, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr, $failed_forwards: expr, $failed_fails: expr) => {
759                 return handle_monitor_err!($self, $err, $channel_state, $entry, $action_type, $resend_raa, $resend_commitment, $failed_forwards, $failed_fails);
760         }
761 }
762
763 // Does not break in case of TemporaryFailure!
764 macro_rules! maybe_break_monitor_err {
765         ($self: ident, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr) => {
766                 match (handle_monitor_err!($self, $err, $channel_state, $entry, $action_type, $resend_raa, $resend_commitment), $err) {
767                         (e, ChannelMonitorUpdateErr::PermanentFailure) => {
768                                 break e;
769                         },
770                         (_, ChannelMonitorUpdateErr::TemporaryFailure) => { },
771                 }
772         }
773 }
774
775 impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<Signer, M, T, K, F, L>
776         where M::Target: chain::Watch<Signer>,
777         T::Target: BroadcasterInterface,
778         K::Target: KeysInterface<Signer = Signer>,
779         F::Target: FeeEstimator,
780         L::Target: Logger,
781 {
782         /// Constructs a new ChannelManager to hold several channels and route between them.
783         ///
784         /// This is the main "logic hub" for all channel-related actions, and implements
785         /// ChannelMessageHandler.
786         ///
787         /// Non-proportional fees are fixed according to our risk using the provided fee estimator.
788         ///
789         /// panics if channel_value_satoshis is >= `MAX_FUNDING_SATOSHIS`!
790         ///
791         /// Users need to notify the new ChannelManager when a new block is connected or
792         /// disconnected using its `block_connected` and `block_disconnected` methods, starting
793         /// from after `params.latest_hash`.
794         pub fn new(fee_est: F, chain_monitor: M, tx_broadcaster: T, logger: L, keys_manager: K, config: UserConfig, params: ChainParameters) -> Self {
795                 let mut secp_ctx = Secp256k1::new();
796                 secp_ctx.seeded_randomize(&keys_manager.get_secure_random_bytes());
797
798                 ChannelManager {
799                         default_configuration: config.clone(),
800                         genesis_hash: genesis_block(params.network).header.block_hash(),
801                         fee_estimator: fee_est,
802                         chain_monitor,
803                         tx_broadcaster,
804
805                         latest_block_height: AtomicUsize::new(params.latest_height),
806                         last_block_hash: RwLock::new(params.latest_hash),
807                         secp_ctx,
808
809                         channel_state: Mutex::new(ChannelHolder{
810                                 by_id: HashMap::new(),
811                                 short_to_id: HashMap::new(),
812                                 forward_htlcs: HashMap::new(),
813                                 claimable_htlcs: HashMap::new(),
814                                 pending_msg_events: Vec::new(),
815                         }),
816                         our_network_key: keys_manager.get_node_secret(),
817
818                         last_node_announcement_serial: AtomicUsize::new(0),
819
820                         per_peer_state: RwLock::new(HashMap::new()),
821
822                         pending_events: Mutex::new(Vec::new()),
823                         pending_background_events: Mutex::new(Vec::new()),
824                         total_consistency_lock: RwLock::new(()),
825                         persistence_notifier: PersistenceNotifier::new(),
826
827                         keys_manager,
828
829                         logger,
830                 }
831         }
832
833         /// Creates a new outbound channel to the given remote node and with the given value.
834         ///
835         /// user_id will be provided back as user_channel_id in FundingGenerationReady and
836         /// FundingBroadcastSafe events to allow tracking of which events correspond with which
837         /// create_channel call. Note that user_channel_id defaults to 0 for inbound channels, so you
838         /// may wish to avoid using 0 for user_id here.
839         ///
840         /// If successful, will generate a SendOpenChannel message event, so you should probably poll
841         /// PeerManager::process_events afterwards.
842         ///
843         /// Raises APIError::APIMisuseError when channel_value_satoshis > 2**24 or push_msat is
844         /// greater than channel_value_satoshis * 1k or channel_value_satoshis is < 1000.
845         pub fn create_channel(&self, their_network_key: PublicKey, channel_value_satoshis: u64, push_msat: u64, user_id: u64, override_config: Option<UserConfig>) -> Result<(), APIError> {
846                 if channel_value_satoshis < 1000 {
847                         return Err(APIError::APIMisuseError { err: format!("Channel value must be at least 1000 satoshis. It was {}", channel_value_satoshis) });
848                 }
849
850                 let config = if override_config.is_some() { override_config.as_ref().unwrap() } else { &self.default_configuration };
851                 let channel = Channel::new_outbound(&self.fee_estimator, &self.keys_manager, their_network_key, channel_value_satoshis, push_msat, user_id, config)?;
852                 let res = channel.get_open_channel(self.genesis_hash.clone());
853
854                 let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
855                 // We want to make sure the lock is actually acquired by PersistenceNotifierGuard.
856                 debug_assert!(&self.total_consistency_lock.try_write().is_err());
857
858                 let mut channel_state = self.channel_state.lock().unwrap();
859                 match channel_state.by_id.entry(channel.channel_id()) {
860                         hash_map::Entry::Occupied(_) => {
861                                 if cfg!(feature = "fuzztarget") {
862                                         return Err(APIError::APIMisuseError { err: "Fuzzy bad RNG".to_owned() });
863                                 } else {
864                                         panic!("RNG is bad???");
865                                 }
866                         },
867                         hash_map::Entry::Vacant(entry) => { entry.insert(channel); }
868                 }
869                 channel_state.pending_msg_events.push(events::MessageSendEvent::SendOpenChannel {
870                         node_id: their_network_key,
871                         msg: res,
872                 });
873                 Ok(())
874         }
875
876         fn list_channels_with_filter<Fn: FnMut(&(&[u8; 32], &Channel<Signer>)) -> bool>(&self, f: Fn) -> Vec<ChannelDetails> {
877                 let mut res = Vec::new();
878                 {
879                         let channel_state = self.channel_state.lock().unwrap();
880                         res.reserve(channel_state.by_id.len());
881                         for (channel_id, channel) in channel_state.by_id.iter().filter(f) {
882                                 let (inbound_capacity_msat, outbound_capacity_msat) = channel.get_inbound_outbound_available_balance_msat();
883                                 res.push(ChannelDetails {
884                                         channel_id: (*channel_id).clone(),
885                                         short_channel_id: channel.get_short_channel_id(),
886                                         remote_network_id: channel.get_counterparty_node_id(),
887                                         counterparty_features: InitFeatures::empty(),
888                                         channel_value_satoshis: channel.get_value_satoshis(),
889                                         inbound_capacity_msat,
890                                         outbound_capacity_msat,
891                                         user_id: channel.get_user_id(),
892                                         is_live: channel.is_live(),
893                                 });
894                         }
895                 }
896                 let per_peer_state = self.per_peer_state.read().unwrap();
897                 for chan in res.iter_mut() {
898                         if let Some(peer_state) = per_peer_state.get(&chan.remote_network_id) {
899                                 chan.counterparty_features = peer_state.lock().unwrap().latest_features.clone();
900                         }
901                 }
902                 res
903         }
904
905         /// Gets the list of open channels, in random order. See ChannelDetail field documentation for
906         /// more information.
907         pub fn list_channels(&self) -> Vec<ChannelDetails> {
908                 self.list_channels_with_filter(|_| true)
909         }
910
911         /// Gets the list of usable channels, in random order. Useful as an argument to
912         /// get_route to ensure non-announced channels are used.
913         ///
914         /// These are guaranteed to have their is_live value set to true, see the documentation for
915         /// ChannelDetails::is_live for more info on exactly what the criteria are.
916         pub fn list_usable_channels(&self) -> Vec<ChannelDetails> {
917                 // Note we use is_live here instead of usable which leads to somewhat confused
918                 // internal/external nomenclature, but that's ok cause that's probably what the user
919                 // really wanted anyway.
920                 self.list_channels_with_filter(|&(_, ref channel)| channel.is_live())
921         }
922
923         /// Begins the process of closing a channel. After this call (plus some timeout), no new HTLCs
924         /// will be accepted on the given channel, and after additional timeout/the closing of all
925         /// pending HTLCs, the channel will be closed on chain.
926         ///
927         /// May generate a SendShutdown message event on success, which should be relayed.
928         pub fn close_channel(&self, channel_id: &[u8; 32]) -> Result<(), APIError> {
929                 let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
930
931                 let (mut failed_htlcs, chan_option) = {
932                         let mut channel_state_lock = self.channel_state.lock().unwrap();
933                         let channel_state = &mut *channel_state_lock;
934                         match channel_state.by_id.entry(channel_id.clone()) {
935                                 hash_map::Entry::Occupied(mut chan_entry) => {
936                                         let (shutdown_msg, failed_htlcs) = chan_entry.get_mut().get_shutdown()?;
937                                         channel_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
938                                                 node_id: chan_entry.get().get_counterparty_node_id(),
939                                                 msg: shutdown_msg
940                                         });
941                                         if chan_entry.get().is_shutdown() {
942                                                 if let Some(short_id) = chan_entry.get().get_short_channel_id() {
943                                                         channel_state.short_to_id.remove(&short_id);
944                                                 }
945                                                 (failed_htlcs, Some(chan_entry.remove_entry().1))
946                                         } else { (failed_htlcs, None) }
947                                 },
948                                 hash_map::Entry::Vacant(_) => return Err(APIError::ChannelUnavailable{err: "No such channel".to_owned()})
949                         }
950                 };
951                 for htlc_source in failed_htlcs.drain(..) {
952                         self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), htlc_source.0, &htlc_source.1, HTLCFailReason::Reason { failure_code: 0x4000 | 8, data: Vec::new() });
953                 }
954                 let chan_update = if let Some(chan) = chan_option {
955                         if let Ok(update) = self.get_channel_update(&chan) {
956                                 Some(update)
957                         } else { None }
958                 } else { None };
959
960                 if let Some(update) = chan_update {
961                         let mut channel_state = self.channel_state.lock().unwrap();
962                         channel_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
963                                 msg: update
964                         });
965                 }
966
967                 Ok(())
968         }
969
970         #[inline]
971         fn finish_force_close_channel(&self, shutdown_res: ShutdownResult) {
972                 let (monitor_update_option, mut failed_htlcs) = shutdown_res;
973                 log_trace!(self.logger, "Finishing force-closure of channel {} HTLCs to fail", failed_htlcs.len());
974                 for htlc_source in failed_htlcs.drain(..) {
975                         self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), htlc_source.0, &htlc_source.1, HTLCFailReason::Reason { failure_code: 0x4000 | 8, data: Vec::new() });
976                 }
977                 if let Some((funding_txo, monitor_update)) = monitor_update_option {
978                         // There isn't anything we can do if we get an update failure - we're already
979                         // force-closing. The monitor update on the required in-memory copy should broadcast
980                         // the latest local state, which is the best we can do anyway. Thus, it is safe to
981                         // ignore the result here.
982                         let _ = self.chain_monitor.update_channel(funding_txo, monitor_update);
983                 }
984         }
985
986         fn force_close_channel_with_peer(&self, channel_id: &[u8; 32], peer_node_id: Option<&PublicKey>) -> Result<(), APIError> {
987                 let mut chan = {
988                         let mut channel_state_lock = self.channel_state.lock().unwrap();
989                         let channel_state = &mut *channel_state_lock;
990                         if let hash_map::Entry::Occupied(chan) = channel_state.by_id.entry(channel_id.clone()) {
991                                 if let Some(node_id) = peer_node_id {
992                                         if chan.get().get_counterparty_node_id() != *node_id {
993                                                 // Error or Ok here doesn't matter - the result is only exposed publicly
994                                                 // when peer_node_id is None anyway.
995                                                 return Ok(());
996                                         }
997                                 }
998                                 if let Some(short_id) = chan.get().get_short_channel_id() {
999                                         channel_state.short_to_id.remove(&short_id);
1000                                 }
1001                                 chan.remove_entry().1
1002                         } else {
1003                                 return Err(APIError::ChannelUnavailable{err: "No such channel".to_owned()});
1004                         }
1005                 };
1006                 log_trace!(self.logger, "Force-closing channel {}", log_bytes!(channel_id[..]));
1007                 self.finish_force_close_channel(chan.force_shutdown(true));
1008                 if let Ok(update) = self.get_channel_update(&chan) {
1009                         let mut channel_state = self.channel_state.lock().unwrap();
1010                         channel_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
1011                                 msg: update
1012                         });
1013                 }
1014
1015                 Ok(())
1016         }
1017
1018         /// Force closes a channel, immediately broadcasting the latest local commitment transaction to
1019         /// the chain and rejecting new HTLCs on the given channel. Fails if channel_id is unknown to the manager.
1020         pub fn force_close_channel(&self, channel_id: &[u8; 32]) -> Result<(), APIError> {
1021                 let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
1022                 self.force_close_channel_with_peer(channel_id, None)
1023         }
1024
1025         /// Force close all channels, immediately broadcasting the latest local commitment transaction
1026         /// for each to the chain and rejecting new HTLCs on each.
1027         pub fn force_close_all_channels(&self) {
1028                 for chan in self.list_channels() {
1029                         let _ = self.force_close_channel(&chan.channel_id);
1030                 }
1031         }
1032
1033         fn decode_update_add_htlc_onion(&self, msg: &msgs::UpdateAddHTLC) -> (PendingHTLCStatus, MutexGuard<ChannelHolder<Signer>>) {
1034                 macro_rules! return_malformed_err {
1035                         ($msg: expr, $err_code: expr) => {
1036                                 {
1037                                         log_info!(self.logger, "Failed to accept/forward incoming HTLC: {}", $msg);
1038                                         return (PendingHTLCStatus::Fail(HTLCFailureMsg::Malformed(msgs::UpdateFailMalformedHTLC {
1039                                                 channel_id: msg.channel_id,
1040                                                 htlc_id: msg.htlc_id,
1041                                                 sha256_of_onion: Sha256::hash(&msg.onion_routing_packet.hop_data).into_inner(),
1042                                                 failure_code: $err_code,
1043                                         })), self.channel_state.lock().unwrap());
1044                                 }
1045                         }
1046                 }
1047
1048                 if let Err(_) = msg.onion_routing_packet.public_key {
1049                         return_malformed_err!("invalid ephemeral pubkey", 0x8000 | 0x4000 | 6);
1050                 }
1051
1052                 let shared_secret = {
1053                         let mut arr = [0; 32];
1054                         arr.copy_from_slice(&SharedSecret::new(&msg.onion_routing_packet.public_key.unwrap(), &self.our_network_key)[..]);
1055                         arr
1056                 };
1057                 let (rho, mu) = onion_utils::gen_rho_mu_from_shared_secret(&shared_secret);
1058
1059                 if msg.onion_routing_packet.version != 0 {
1060                         //TODO: Spec doesn't indicate if we should only hash hop_data here (and in other
1061                         //sha256_of_onion error data packets), or the entire onion_routing_packet. Either way,
1062                         //the hash doesn't really serve any purpose - in the case of hashing all data, the
1063                         //receiving node would have to brute force to figure out which version was put in the
1064                         //packet by the node that send us the message, in the case of hashing the hop_data, the
1065                         //node knows the HMAC matched, so they already know what is there...
1066                         return_malformed_err!("Unknown onion packet version", 0x8000 | 0x4000 | 4);
1067                 }
1068
1069                 let mut hmac = HmacEngine::<Sha256>::new(&mu);
1070                 hmac.input(&msg.onion_routing_packet.hop_data);
1071                 hmac.input(&msg.payment_hash.0[..]);
1072                 if !fixed_time_eq(&Hmac::from_engine(hmac).into_inner(), &msg.onion_routing_packet.hmac) {
1073                         return_malformed_err!("HMAC Check failed", 0x8000 | 0x4000 | 5);
1074                 }
1075
1076                 let mut channel_state = None;
1077                 macro_rules! return_err {
1078                         ($msg: expr, $err_code: expr, $data: expr) => {
1079                                 {
1080                                         log_info!(self.logger, "Failed to accept/forward incoming HTLC: {}", $msg);
1081                                         if channel_state.is_none() {
1082                                                 channel_state = Some(self.channel_state.lock().unwrap());
1083                                         }
1084                                         return (PendingHTLCStatus::Fail(HTLCFailureMsg::Relay(msgs::UpdateFailHTLC {
1085                                                 channel_id: msg.channel_id,
1086                                                 htlc_id: msg.htlc_id,
1087                                                 reason: onion_utils::build_first_hop_failure_packet(&shared_secret, $err_code, $data),
1088                                         })), channel_state.unwrap());
1089                                 }
1090                         }
1091                 }
1092
1093                 let mut chacha = ChaCha20::new(&rho, &[0u8; 8]);
1094                 let mut chacha_stream = ChaChaReader { chacha: &mut chacha, read: Cursor::new(&msg.onion_routing_packet.hop_data[..]) };
1095                 let (next_hop_data, next_hop_hmac) = {
1096                         match msgs::OnionHopData::read(&mut chacha_stream) {
1097                                 Err(err) => {
1098                                         let error_code = match err {
1099                                                 msgs::DecodeError::UnknownVersion => 0x4000 | 1, // unknown realm byte
1100                                                 msgs::DecodeError::UnknownRequiredFeature|
1101                                                 msgs::DecodeError::InvalidValue|
1102                                                 msgs::DecodeError::ShortRead => 0x4000 | 22, // invalid_onion_payload
1103                                                 _ => 0x2000 | 2, // Should never happen
1104                                         };
1105                                         return_err!("Unable to decode our hop data", error_code, &[0;0]);
1106                                 },
1107                                 Ok(msg) => {
1108                                         let mut hmac = [0; 32];
1109                                         if let Err(_) = chacha_stream.read_exact(&mut hmac[..]) {
1110                                                 return_err!("Unable to decode hop data", 0x4000 | 22, &[0;0]);
1111                                         }
1112                                         (msg, hmac)
1113                                 },
1114                         }
1115                 };
1116
1117                 let pending_forward_info = if next_hop_hmac == [0; 32] {
1118                                 #[cfg(test)]
1119                                 {
1120                                         // In tests, make sure that the initial onion pcket data is, at least, non-0.
1121                                         // We could do some fancy randomness test here, but, ehh, whatever.
1122                                         // This checks for the issue where you can calculate the path length given the
1123                                         // onion data as all the path entries that the originator sent will be here
1124                                         // as-is (and were originally 0s).
1125                                         // Of course reverse path calculation is still pretty easy given naive routing
1126                                         // algorithms, but this fixes the most-obvious case.
1127                                         let mut next_bytes = [0; 32];
1128                                         chacha_stream.read_exact(&mut next_bytes).unwrap();
1129                                         assert_ne!(next_bytes[..], [0; 32][..]);
1130                                         chacha_stream.read_exact(&mut next_bytes).unwrap();
1131                                         assert_ne!(next_bytes[..], [0; 32][..]);
1132                                 }
1133
1134                                 // OUR PAYMENT!
1135                                 // final_expiry_too_soon
1136                                 // We have to have some headroom to broadcast on chain if we have the preimage, so make sure we have at least
1137                                 // HTLC_FAIL_BACK_BUFFER blocks to go.
1138                                 // Also, ensure that, in the case of an unknown payment hash, our payment logic has enough time to fail the HTLC backward
1139                                 // before our onchain logic triggers a channel closure (see HTLC_FAIL_BACK_BUFFER rational).
1140                                 if (msg.cltv_expiry as u64) <= self.latest_block_height.load(Ordering::Acquire) as u64 + HTLC_FAIL_BACK_BUFFER as u64 + 1 {
1141                                         return_err!("The final CLTV expiry is too soon to handle", 17, &[0;0]);
1142                                 }
1143                                 // final_incorrect_htlc_amount
1144                                 if next_hop_data.amt_to_forward > msg.amount_msat {
1145                                         return_err!("Upstream node sent less than we were supposed to receive in payment", 19, &byte_utils::be64_to_array(msg.amount_msat));
1146                                 }
1147                                 // final_incorrect_cltv_expiry
1148                                 if next_hop_data.outgoing_cltv_value != msg.cltv_expiry {
1149                                         return_err!("Upstream node set CLTV to the wrong value", 18, &byte_utils::be32_to_array(msg.cltv_expiry));
1150                                 }
1151
1152                                 let payment_data = match next_hop_data.format {
1153                                         msgs::OnionHopDataFormat::Legacy { .. } => None,
1154                                         msgs::OnionHopDataFormat::NonFinalNode { .. } => return_err!("Got non final data with an HMAC of 0", 0x4000 | 22, &[0;0]),
1155                                         msgs::OnionHopDataFormat::FinalNode { payment_data } => payment_data,
1156                                 };
1157
1158                                 // Note that we could obviously respond immediately with an update_fulfill_htlc
1159                                 // message, however that would leak that we are the recipient of this payment, so
1160                                 // instead we stay symmetric with the forwarding case, only responding (after a
1161                                 // delay) once they've send us a commitment_signed!
1162
1163                                 PendingHTLCStatus::Forward(PendingHTLCInfo {
1164                                         routing: PendingHTLCRouting::Receive {
1165                                                 payment_data,
1166                                                 incoming_cltv_expiry: msg.cltv_expiry,
1167                                         },
1168                                         payment_hash: msg.payment_hash.clone(),
1169                                         incoming_shared_secret: shared_secret,
1170                                         amt_to_forward: next_hop_data.amt_to_forward,
1171                                         outgoing_cltv_value: next_hop_data.outgoing_cltv_value,
1172                                 })
1173                         } else {
1174                                 let mut new_packet_data = [0; 20*65];
1175                                 let read_pos = chacha_stream.read(&mut new_packet_data).unwrap();
1176                                 #[cfg(debug_assertions)]
1177                                 {
1178                                         // Check two things:
1179                                         // a) that the behavior of our stream here will return Ok(0) even if the TLV
1180                                         //    read above emptied out our buffer and the unwrap() wont needlessly panic
1181                                         // b) that we didn't somehow magically end up with extra data.
1182                                         let mut t = [0; 1];
1183                                         debug_assert!(chacha_stream.read(&mut t).unwrap() == 0);
1184                                 }
1185                                 // Once we've emptied the set of bytes our peer gave us, encrypt 0 bytes until we
1186                                 // fill the onion hop data we'll forward to our next-hop peer.
1187                                 chacha_stream.chacha.process_in_place(&mut new_packet_data[read_pos..]);
1188
1189                                 let mut new_pubkey = msg.onion_routing_packet.public_key.unwrap();
1190
1191                                 let blinding_factor = {
1192                                         let mut sha = Sha256::engine();
1193                                         sha.input(&new_pubkey.serialize()[..]);
1194                                         sha.input(&shared_secret);
1195                                         Sha256::from_engine(sha).into_inner()
1196                                 };
1197
1198                                 let public_key = if let Err(e) = new_pubkey.mul_assign(&self.secp_ctx, &blinding_factor[..]) {
1199                                         Err(e)
1200                                 } else { Ok(new_pubkey) };
1201
1202                                 let outgoing_packet = msgs::OnionPacket {
1203                                         version: 0,
1204                                         public_key,
1205                                         hop_data: new_packet_data,
1206                                         hmac: next_hop_hmac.clone(),
1207                                 };
1208
1209                                 let short_channel_id = match next_hop_data.format {
1210                                         msgs::OnionHopDataFormat::Legacy { short_channel_id } => short_channel_id,
1211                                         msgs::OnionHopDataFormat::NonFinalNode { short_channel_id } => short_channel_id,
1212                                         msgs::OnionHopDataFormat::FinalNode { .. } => {
1213                                                 return_err!("Final Node OnionHopData provided for us as an intermediary node", 0x4000 | 22, &[0;0]);
1214                                         },
1215                                 };
1216
1217                                 PendingHTLCStatus::Forward(PendingHTLCInfo {
1218                                         routing: PendingHTLCRouting::Forward {
1219                                                 onion_packet: outgoing_packet,
1220                                                 short_channel_id,
1221                                         },
1222                                         payment_hash: msg.payment_hash.clone(),
1223                                         incoming_shared_secret: shared_secret,
1224                                         amt_to_forward: next_hop_data.amt_to_forward,
1225                                         outgoing_cltv_value: next_hop_data.outgoing_cltv_value,
1226                                 })
1227                         };
1228
1229                 channel_state = Some(self.channel_state.lock().unwrap());
1230                 if let &PendingHTLCStatus::Forward(PendingHTLCInfo { ref routing, ref amt_to_forward, ref outgoing_cltv_value, .. }) = &pending_forward_info {
1231                         // If short_channel_id is 0 here, we'll reject the HTLC as there cannot be a channel
1232                         // with a short_channel_id of 0. This is important as various things later assume
1233                         // short_channel_id is non-0 in any ::Forward.
1234                         if let &PendingHTLCRouting::Forward { ref short_channel_id, .. } = routing {
1235                                 let id_option = channel_state.as_ref().unwrap().short_to_id.get(&short_channel_id).cloned();
1236                                 let forwarding_id = match id_option {
1237                                         None => { // unknown_next_peer
1238                                                 return_err!("Don't have available channel for forwarding as requested.", 0x4000 | 10, &[0;0]);
1239                                         },
1240                                         Some(id) => id.clone(),
1241                                 };
1242                                 if let Some((err, code, chan_update)) = loop {
1243                                         let chan = channel_state.as_mut().unwrap().by_id.get_mut(&forwarding_id).unwrap();
1244
1245                                         // Note that we could technically not return an error yet here and just hope
1246                                         // that the connection is reestablished or monitor updated by the time we get
1247                                         // around to doing the actual forward, but better to fail early if we can and
1248                                         // hopefully an attacker trying to path-trace payments cannot make this occur
1249                                         // on a small/per-node/per-channel scale.
1250                                         if !chan.is_live() { // channel_disabled
1251                                                 break Some(("Forwarding channel is not in a ready state.", 0x1000 | 20, Some(self.get_channel_update(chan).unwrap())));
1252                                         }
1253                                         if *amt_to_forward < chan.get_counterparty_htlc_minimum_msat() { // amount_below_minimum
1254                                                 break Some(("HTLC amount was below the htlc_minimum_msat", 0x1000 | 11, Some(self.get_channel_update(chan).unwrap())));
1255                                         }
1256                                         let fee = amt_to_forward.checked_mul(chan.get_fee_proportional_millionths() as u64).and_then(|prop_fee| { (prop_fee / 1000000).checked_add(chan.get_holder_fee_base_msat(&self.fee_estimator) as u64) });
1257                                         if fee.is_none() || msg.amount_msat < fee.unwrap() || (msg.amount_msat - fee.unwrap()) < *amt_to_forward { // fee_insufficient
1258                                                 break Some(("Prior hop has deviated from specified fees parameters or origin node has obsolete ones", 0x1000 | 12, Some(self.get_channel_update(chan).unwrap())));
1259                                         }
1260                                         if (msg.cltv_expiry as u64) < (*outgoing_cltv_value) as u64 + CLTV_EXPIRY_DELTA as u64 { // incorrect_cltv_expiry
1261                                                 break Some(("Forwarding node has tampered with the intended HTLC values or origin node has an obsolete cltv_expiry_delta", 0x1000 | 13, Some(self.get_channel_update(chan).unwrap())));
1262                                         }
1263                                         let cur_height = self.latest_block_height.load(Ordering::Acquire) as u32 + 1;
1264                                         // Theoretically, channel counterparty shouldn't send us a HTLC expiring now, but we want to be robust wrt to counterparty
1265                                         // packet sanitization (see HTLC_FAIL_BACK_BUFFER rational)
1266                                         if msg.cltv_expiry <= cur_height + HTLC_FAIL_BACK_BUFFER as u32 { // expiry_too_soon
1267                                                 break Some(("CLTV expiry is too close", 0x1000 | 14, Some(self.get_channel_update(chan).unwrap())));
1268                                         }
1269                                         if msg.cltv_expiry > cur_height + CLTV_FAR_FAR_AWAY as u32 { // expiry_too_far
1270                                                 break Some(("CLTV expiry is too far in the future", 21, None));
1271                                         }
1272                                         // In theory, we would be safe against unitentional channel-closure, if we only required a margin of LATENCY_GRACE_PERIOD_BLOCKS.
1273                                         // But, to be safe against policy reception, we use a longuer delay.
1274                                         if (*outgoing_cltv_value) as u64 <= (cur_height + HTLC_FAIL_BACK_BUFFER) as u64 {
1275                                                 break Some(("Outgoing CLTV value is too soon", 0x1000 | 14, Some(self.get_channel_update(chan).unwrap())));
1276                                         }
1277
1278                                         break None;
1279                                 }
1280                                 {
1281                                         let mut res = Vec::with_capacity(8 + 128);
1282                                         if let Some(chan_update) = chan_update {
1283                                                 if code == 0x1000 | 11 || code == 0x1000 | 12 {
1284                                                         res.extend_from_slice(&byte_utils::be64_to_array(msg.amount_msat));
1285                                                 }
1286                                                 else if code == 0x1000 | 13 {
1287                                                         res.extend_from_slice(&byte_utils::be32_to_array(msg.cltv_expiry));
1288                                                 }
1289                                                 else if code == 0x1000 | 20 {
1290                                                         // TODO: underspecified, follow https://github.com/lightningnetwork/lightning-rfc/issues/791
1291                                                         res.extend_from_slice(&byte_utils::be16_to_array(0));
1292                                                 }
1293                                                 res.extend_from_slice(&chan_update.encode_with_len()[..]);
1294                                         }
1295                                         return_err!(err, code, &res[..]);
1296                                 }
1297                         }
1298                 }
1299
1300                 (pending_forward_info, channel_state.unwrap())
1301         }
1302
1303         /// only fails if the channel does not yet have an assigned short_id
1304         /// May be called with channel_state already locked!
1305         fn get_channel_update(&self, chan: &Channel<Signer>) -> Result<msgs::ChannelUpdate, LightningError> {
1306                 let short_channel_id = match chan.get_short_channel_id() {
1307                         None => return Err(LightningError{err: "Channel not yet established".to_owned(), action: msgs::ErrorAction::IgnoreError}),
1308                         Some(id) => id,
1309                 };
1310
1311                 let were_node_one = PublicKey::from_secret_key(&self.secp_ctx, &self.our_network_key).serialize()[..] < chan.get_counterparty_node_id().serialize()[..];
1312
1313                 let unsigned = msgs::UnsignedChannelUpdate {
1314                         chain_hash: self.genesis_hash,
1315                         short_channel_id,
1316                         timestamp: chan.get_update_time_counter(),
1317                         flags: (!were_node_one) as u8 | ((!chan.is_live() as u8) << 1),
1318                         cltv_expiry_delta: CLTV_EXPIRY_DELTA,
1319                         htlc_minimum_msat: chan.get_counterparty_htlc_minimum_msat(),
1320                         htlc_maximum_msat: OptionalField::Present(chan.get_announced_htlc_max_msat()),
1321                         fee_base_msat: chan.get_holder_fee_base_msat(&self.fee_estimator),
1322                         fee_proportional_millionths: chan.get_fee_proportional_millionths(),
1323                         excess_data: Vec::new(),
1324                 };
1325
1326                 let msg_hash = Sha256dHash::hash(&unsigned.encode()[..]);
1327                 let sig = self.secp_ctx.sign(&hash_to_message!(&msg_hash[..]), &self.our_network_key);
1328
1329                 Ok(msgs::ChannelUpdate {
1330                         signature: sig,
1331                         contents: unsigned
1332                 })
1333         }
1334
1335         // Only public for testing, this should otherwise never be called direcly
1336         pub(crate) fn send_payment_along_path(&self, path: &Vec<RouteHop>, payment_hash: &PaymentHash, payment_secret: &Option<PaymentSecret>, total_value: u64, cur_height: u32) -> Result<(), APIError> {
1337                 log_trace!(self.logger, "Attempting to send payment for path with next hop {}", path.first().unwrap().short_channel_id);
1338                 let prng_seed = self.keys_manager.get_secure_random_bytes();
1339                 let session_priv = SecretKey::from_slice(&self.keys_manager.get_secure_random_bytes()[..]).expect("RNG is busted");
1340
1341                 let onion_keys = onion_utils::construct_onion_keys(&self.secp_ctx, &path, &session_priv)
1342                         .map_err(|_| APIError::RouteError{err: "Pubkey along hop was maliciously selected"})?;
1343                 let (onion_payloads, htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(path, total_value, payment_secret, cur_height)?;
1344                 if onion_utils::route_size_insane(&onion_payloads) {
1345                         return Err(APIError::RouteError{err: "Route size too large considering onion data"});
1346                 }
1347                 let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, prng_seed, payment_hash);
1348
1349                 let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
1350
1351                 let err: Result<(), _> = loop {
1352                         let mut channel_lock = self.channel_state.lock().unwrap();
1353                         let id = match channel_lock.short_to_id.get(&path.first().unwrap().short_channel_id) {
1354                                 None => return Err(APIError::ChannelUnavailable{err: "No channel available with first hop!".to_owned()}),
1355                                 Some(id) => id.clone(),
1356                         };
1357
1358                         let channel_state = &mut *channel_lock;
1359                         if let hash_map::Entry::Occupied(mut chan) = channel_state.by_id.entry(id) {
1360                                 match {
1361                                         if chan.get().get_counterparty_node_id() != path.first().unwrap().pubkey {
1362                                                 return Err(APIError::RouteError{err: "Node ID mismatch on first hop!"});
1363                                         }
1364                                         if !chan.get().is_live() {
1365                                                 return Err(APIError::ChannelUnavailable{err: "Peer for first hop currently disconnected/pending monitor update!".to_owned()});
1366                                         }
1367                                         break_chan_entry!(self, chan.get_mut().send_htlc_and_commit(htlc_msat, payment_hash.clone(), htlc_cltv, HTLCSource::OutboundRoute {
1368                                                 path: path.clone(),
1369                                                 session_priv: session_priv.clone(),
1370                                                 first_hop_htlc_msat: htlc_msat,
1371                                         }, onion_packet, &self.logger), channel_state, chan)
1372                                 } {
1373                                         Some((update_add, commitment_signed, monitor_update)) => {
1374                                                 if let Err(e) = self.chain_monitor.update_channel(chan.get().get_funding_txo().unwrap(), monitor_update) {
1375                                                         maybe_break_monitor_err!(self, e, channel_state, chan, RAACommitmentOrder::CommitmentFirst, false, true);
1376                                                         // Note that MonitorUpdateFailed here indicates (per function docs)
1377                                                         // that we will resend the commitment update once monitor updating
1378                                                         // is restored. Therefore, we must return an error indicating that
1379                                                         // it is unsafe to retry the payment wholesale, which we do in the
1380                                                         // send_payment check for MonitorUpdateFailed, below.
1381                                                         return Err(APIError::MonitorUpdateFailed);
1382                                                 }
1383
1384                                                 channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
1385                                                         node_id: path.first().unwrap().pubkey,
1386                                                         updates: msgs::CommitmentUpdate {
1387                                                                 update_add_htlcs: vec![update_add],
1388                                                                 update_fulfill_htlcs: Vec::new(),
1389                                                                 update_fail_htlcs: Vec::new(),
1390                                                                 update_fail_malformed_htlcs: Vec::new(),
1391                                                                 update_fee: None,
1392                                                                 commitment_signed,
1393                                                         },
1394                                                 });
1395                                         },
1396                                         None => {},
1397                                 }
1398                         } else { unreachable!(); }
1399                         return Ok(());
1400                 };
1401
1402                 match handle_error!(self, err, path.first().unwrap().pubkey) {
1403                         Ok(_) => unreachable!(),
1404                         Err(e) => {
1405                                 Err(APIError::ChannelUnavailable { err: e.err })
1406                         },
1407                 }
1408         }
1409
1410         /// Sends a payment along a given route.
1411         ///
1412         /// Value parameters are provided via the last hop in route, see documentation for RouteHop
1413         /// fields for more info.
1414         ///
1415         /// Note that if the payment_hash already exists elsewhere (eg you're sending a duplicative
1416         /// payment), we don't do anything to stop you! We always try to ensure that if the provided
1417         /// next hop knows the preimage to payment_hash they can claim an additional amount as
1418         /// specified in the last hop in the route! Thus, you should probably do your own
1419         /// payment_preimage tracking (which you should already be doing as they represent "proof of
1420         /// payment") and prevent double-sends yourself.
1421         ///
1422         /// May generate SendHTLCs message(s) event on success, which should be relayed.
1423         ///
1424         /// Each path may have a different return value, and PaymentSendValue may return a Vec with
1425         /// each entry matching the corresponding-index entry in the route paths, see
1426         /// PaymentSendFailure for more info.
1427         ///
1428         /// In general, a path may raise:
1429         ///  * APIError::RouteError when an invalid route or forwarding parameter (cltv_delta, fee,
1430         ///    node public key) is specified.
1431         ///  * APIError::ChannelUnavailable if the next-hop channel is not available for updates
1432         ///    (including due to previous monitor update failure or new permanent monitor update
1433         ///    failure).
1434         ///  * APIError::MonitorUpdateFailed if a new monitor update failure prevented sending the
1435         ///    relevant updates.
1436         ///
1437         /// Note that depending on the type of the PaymentSendFailure the HTLC may have been
1438         /// irrevocably committed to on our end. In such a case, do NOT retry the payment with a
1439         /// different route unless you intend to pay twice!
1440         ///
1441         /// payment_secret is unrelated to payment_hash (or PaymentPreimage) and exists to authenticate
1442         /// the sender to the recipient and prevent payment-probing (deanonymization) attacks. For
1443         /// newer nodes, it will be provided to you in the invoice. If you do not have one, the Route
1444         /// must not contain multiple paths as multi-path payments require a recipient-provided
1445         /// payment_secret.
1446         /// If a payment_secret *is* provided, we assume that the invoice had the payment_secret feature
1447         /// bit set (either as required or as available). If multiple paths are present in the Route,
1448         /// we assume the invoice had the basic_mpp feature set.
1449         pub fn send_payment(&self, route: &Route, payment_hash: PaymentHash, payment_secret: &Option<PaymentSecret>) -> Result<(), PaymentSendFailure> {
1450                 if route.paths.len() < 1 {
1451                         return Err(PaymentSendFailure::ParameterError(APIError::RouteError{err: "There must be at least one path to send over"}));
1452                 }
1453                 if route.paths.len() > 10 {
1454                         // This limit is completely arbitrary - there aren't any real fundamental path-count
1455                         // limits. After we support retrying individual paths we should likely bump this, but
1456                         // for now more than 10 paths likely carries too much one-path failure.
1457                         return Err(PaymentSendFailure::ParameterError(APIError::RouteError{err: "Sending over more than 10 paths is not currently supported"}));
1458                 }
1459                 let mut total_value = 0;
1460                 let our_node_id = self.get_our_node_id();
1461                 let mut path_errs = Vec::with_capacity(route.paths.len());
1462                 'path_check: for path in route.paths.iter() {
1463                         if path.len() < 1 || path.len() > 20 {
1464                                 path_errs.push(Err(APIError::RouteError{err: "Path didn't go anywhere/had bogus size"}));
1465                                 continue 'path_check;
1466                         }
1467                         for (idx, hop) in path.iter().enumerate() {
1468                                 if idx != path.len() - 1 && hop.pubkey == our_node_id {
1469                                         path_errs.push(Err(APIError::RouteError{err: "Path went through us but wasn't a simple rebalance loop to us"}));
1470                                         continue 'path_check;
1471                                 }
1472                         }
1473                         total_value += path.last().unwrap().fee_msat;
1474                         path_errs.push(Ok(()));
1475                 }
1476                 if path_errs.iter().any(|e| e.is_err()) {
1477                         return Err(PaymentSendFailure::PathParameterError(path_errs));
1478                 }
1479
1480                 let cur_height = self.latest_block_height.load(Ordering::Acquire) as u32 + 1;
1481                 let mut results = Vec::new();
1482                 for path in route.paths.iter() {
1483                         results.push(self.send_payment_along_path(&path, &payment_hash, payment_secret, total_value, cur_height));
1484                 }
1485                 let mut has_ok = false;
1486                 let mut has_err = false;
1487                 for res in results.iter() {
1488                         if res.is_ok() { has_ok = true; }
1489                         if res.is_err() { has_err = true; }
1490                         if let &Err(APIError::MonitorUpdateFailed) = res {
1491                                 // MonitorUpdateFailed is inherently unsafe to retry, so we call it a
1492                                 // PartialFailure.
1493                                 has_err = true;
1494                                 has_ok = true;
1495                                 break;
1496                         }
1497                 }
1498                 if has_err && has_ok {
1499                         Err(PaymentSendFailure::PartialFailure(results))
1500                 } else if has_err {
1501                         Err(PaymentSendFailure::AllFailedRetrySafe(results.drain(..).map(|r| r.unwrap_err()).collect()))
1502                 } else {
1503                         Ok(())
1504                 }
1505         }
1506
1507         /// Call this upon creation of a funding transaction for the given channel.
1508         ///
1509         /// Note that ALL inputs in the transaction pointed to by funding_txo MUST spend SegWit outputs
1510         /// or your counterparty can steal your funds!
1511         ///
1512         /// Panics if a funding transaction has already been provided for this channel.
1513         ///
1514         /// May panic if the funding_txo is duplicative with some other channel (note that this should
1515         /// be trivially prevented by using unique funding transaction keys per-channel).
1516         pub fn funding_transaction_generated(&self, temporary_channel_id: &[u8; 32], funding_txo: OutPoint) {
1517                 let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
1518
1519                 let (chan, msg) = {
1520                         let (res, chan) = match self.channel_state.lock().unwrap().by_id.remove(temporary_channel_id) {
1521                                 Some(mut chan) => {
1522                                         (chan.get_outbound_funding_created(funding_txo, &self.logger)
1523                                                 .map_err(|e| if let ChannelError::Close(msg) = e {
1524                                                         MsgHandleErrInternal::from_finish_shutdown(msg, chan.channel_id(), chan.force_shutdown(true), None)
1525                                                 } else { unreachable!(); })
1526                                         , chan)
1527                                 },
1528                                 None => return
1529                         };
1530                         match handle_error!(self, res, chan.get_counterparty_node_id()) {
1531                                 Ok(funding_msg) => {
1532                                         (chan, funding_msg)
1533                                 },
1534                                 Err(_) => { return; }
1535                         }
1536                 };
1537
1538                 let mut channel_state = self.channel_state.lock().unwrap();
1539                 channel_state.pending_msg_events.push(events::MessageSendEvent::SendFundingCreated {
1540                         node_id: chan.get_counterparty_node_id(),
1541                         msg,
1542                 });
1543                 match channel_state.by_id.entry(chan.channel_id()) {
1544                         hash_map::Entry::Occupied(_) => {
1545                                 panic!("Generated duplicate funding txid?");
1546                         },
1547                         hash_map::Entry::Vacant(e) => {
1548                                 e.insert(chan);
1549                         }
1550                 }
1551         }
1552
1553         fn get_announcement_sigs(&self, chan: &Channel<Signer>) -> Option<msgs::AnnouncementSignatures> {
1554                 if !chan.should_announce() {
1555                         log_trace!(self.logger, "Can't send announcement_signatures for private channel {}", log_bytes!(chan.channel_id()));
1556                         return None
1557                 }
1558
1559                 let (announcement, our_bitcoin_sig) = match chan.get_channel_announcement(self.get_our_node_id(), self.genesis_hash.clone()) {
1560                         Ok(res) => res,
1561                         Err(_) => return None, // Only in case of state precondition violations eg channel is closing
1562                 };
1563                 let msghash = hash_to_message!(&Sha256dHash::hash(&announcement.encode()[..])[..]);
1564                 let our_node_sig = self.secp_ctx.sign(&msghash, &self.our_network_key);
1565
1566                 Some(msgs::AnnouncementSignatures {
1567                         channel_id: chan.channel_id(),
1568                         short_channel_id: chan.get_short_channel_id().unwrap(),
1569                         node_signature: our_node_sig,
1570                         bitcoin_signature: our_bitcoin_sig,
1571                 })
1572         }
1573
1574         #[allow(dead_code)]
1575         // Messages of up to 64KB should never end up more than half full with addresses, as that would
1576         // be absurd. We ensure this by checking that at least 500 (our stated public contract on when
1577         // broadcast_node_announcement panics) of the maximum-length addresses would fit in a 64KB
1578         // message...
1579         const HALF_MESSAGE_IS_ADDRS: u32 = ::std::u16::MAX as u32 / (NetAddress::MAX_LEN as u32 + 1) / 2;
1580         #[deny(const_err)]
1581         #[allow(dead_code)]
1582         // ...by failing to compile if the number of addresses that would be half of a message is
1583         // smaller than 500:
1584         const STATIC_ASSERT: u32 = Self::HALF_MESSAGE_IS_ADDRS - 500;
1585
1586         /// Generates a signed node_announcement from the given arguments and creates a
1587         /// BroadcastNodeAnnouncement event. Note that such messages will be ignored unless peers have
1588         /// seen a channel_announcement from us (ie unless we have public channels open).
1589         ///
1590         /// RGB is a node "color" and alias is a printable human-readable string to describe this node
1591         /// to humans. They carry no in-protocol meaning.
1592         ///
1593         /// addresses represent the set (possibly empty) of socket addresses on which this node accepts
1594         /// incoming connections. These will be broadcast to the network, publicly tying these
1595         /// addresses together. If you wish to preserve user privacy, addresses should likely contain
1596         /// only Tor Onion addresses.
1597         ///
1598         /// Panics if addresses is absurdly large (more than 500).
1599         pub fn broadcast_node_announcement(&self, rgb: [u8; 3], alias: [u8; 32], addresses: Vec<NetAddress>) {
1600                 let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
1601
1602                 if addresses.len() > 500 {
1603                         panic!("More than half the message size was taken up by public addresses!");
1604                 }
1605
1606                 let announcement = msgs::UnsignedNodeAnnouncement {
1607                         features: NodeFeatures::known(),
1608                         timestamp: self.last_node_announcement_serial.fetch_add(1, Ordering::AcqRel) as u32,
1609                         node_id: self.get_our_node_id(),
1610                         rgb, alias, addresses,
1611                         excess_address_data: Vec::new(),
1612                         excess_data: Vec::new(),
1613                 };
1614                 let msghash = hash_to_message!(&Sha256dHash::hash(&announcement.encode()[..])[..]);
1615
1616                 let mut channel_state = self.channel_state.lock().unwrap();
1617                 channel_state.pending_msg_events.push(events::MessageSendEvent::BroadcastNodeAnnouncement {
1618                         msg: msgs::NodeAnnouncement {
1619                                 signature: self.secp_ctx.sign(&msghash, &self.our_network_key),
1620                                 contents: announcement
1621                         },
1622                 });
1623         }
1624
1625         /// Processes HTLCs which are pending waiting on random forward delay.
1626         ///
1627         /// Should only really ever be called in response to a PendingHTLCsForwardable event.
1628         /// Will likely generate further events.
1629         pub fn process_pending_htlc_forwards(&self) {
1630                 let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
1631
1632                 let mut new_events = Vec::new();
1633                 let mut failed_forwards = Vec::new();
1634                 let mut handle_errors = Vec::new();
1635                 {
1636                         let mut channel_state_lock = self.channel_state.lock().unwrap();
1637                         let channel_state = &mut *channel_state_lock;
1638
1639                         for (short_chan_id, mut pending_forwards) in channel_state.forward_htlcs.drain() {
1640                                 if short_chan_id != 0 {
1641                                         let forward_chan_id = match channel_state.short_to_id.get(&short_chan_id) {
1642                                                 Some(chan_id) => chan_id.clone(),
1643                                                 None => {
1644                                                         failed_forwards.reserve(pending_forwards.len());
1645                                                         for forward_info in pending_forwards.drain(..) {
1646                                                                 match forward_info {
1647                                                                         HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info,
1648                                                                                                    prev_funding_outpoint } => {
1649                                                                                 let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
1650                                                                                         short_channel_id: prev_short_channel_id,
1651                                                                                         outpoint: prev_funding_outpoint,
1652                                                                                         htlc_id: prev_htlc_id,
1653                                                                                         incoming_packet_shared_secret: forward_info.incoming_shared_secret,
1654                                                                                 });
1655                                                                                 failed_forwards.push((htlc_source, forward_info.payment_hash,
1656                                                                                         HTLCFailReason::Reason { failure_code: 0x4000 | 10, data: Vec::new() }
1657                                                                                 ));
1658                                                                         },
1659                                                                         HTLCForwardInfo::FailHTLC { .. } => {
1660                                                                                 // Channel went away before we could fail it. This implies
1661                                                                                 // the channel is now on chain and our counterparty is
1662                                                                                 // trying to broadcast the HTLC-Timeout, but that's their
1663                                                                                 // problem, not ours.
1664                                                                         }
1665                                                                 }
1666                                                         }
1667                                                         continue;
1668                                                 }
1669                                         };
1670                                         if let hash_map::Entry::Occupied(mut chan) = channel_state.by_id.entry(forward_chan_id) {
1671                                                 let mut add_htlc_msgs = Vec::new();
1672                                                 let mut fail_htlc_msgs = Vec::new();
1673                                                 for forward_info in pending_forwards.drain(..) {
1674                                                         match forward_info {
1675                                                                 HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info: PendingHTLCInfo {
1676                                                                                 routing: PendingHTLCRouting::Forward {
1677                                                                                         onion_packet, ..
1678                                                                                 }, incoming_shared_secret, payment_hash, amt_to_forward, outgoing_cltv_value },
1679                                                                                 prev_funding_outpoint } => {
1680                                                                         log_trace!(self.logger, "Adding HTLC from short id {} with payment_hash {} to channel with short id {} after delay", log_bytes!(payment_hash.0), prev_short_channel_id, short_chan_id);
1681                                                                         let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
1682                                                                                 short_channel_id: prev_short_channel_id,
1683                                                                                 outpoint: prev_funding_outpoint,
1684                                                                                 htlc_id: prev_htlc_id,
1685                                                                                 incoming_packet_shared_secret: incoming_shared_secret,
1686                                                                         });
1687                                                                         match chan.get_mut().send_htlc(amt_to_forward, payment_hash, outgoing_cltv_value, htlc_source.clone(), onion_packet) {
1688                                                                                 Err(e) => {
1689                                                                                         if let ChannelError::Ignore(msg) = e {
1690                                                                                                 log_trace!(self.logger, "Failed to forward HTLC with payment_hash {}: {}", log_bytes!(payment_hash.0), msg);
1691                                                                                         } else {
1692                                                                                                 panic!("Stated return value requirements in send_htlc() were not met");
1693                                                                                         }
1694                                                                                         let chan_update = self.get_channel_update(chan.get()).unwrap();
1695                                                                                         failed_forwards.push((htlc_source, payment_hash,
1696                                                                                                 HTLCFailReason::Reason { failure_code: 0x1000 | 7, data: chan_update.encode_with_len() }
1697                                                                                         ));
1698                                                                                         continue;
1699                                                                                 },
1700                                                                                 Ok(update_add) => {
1701                                                                                         match update_add {
1702                                                                                                 Some(msg) => { add_htlc_msgs.push(msg); },
1703                                                                                                 None => {
1704                                                                                                         // Nothing to do here...we're waiting on a remote
1705                                                                                                         // revoke_and_ack before we can add anymore HTLCs. The Channel
1706                                                                                                         // will automatically handle building the update_add_htlc and
1707                                                                                                         // commitment_signed messages when we can.
1708                                                                                                         // TODO: Do some kind of timer to set the channel as !is_live()
1709                                                                                                         // as we don't really want others relying on us relaying through
1710                                                                                                         // this channel currently :/.
1711                                                                                                 }
1712                                                                                         }
1713                                                                                 }
1714                                                                         }
1715                                                                 },
1716                                                                 HTLCForwardInfo::AddHTLC { .. } => {
1717                                                                         panic!("short_channel_id != 0 should imply any pending_forward entries are of type Forward");
1718                                                                 },
1719                                                                 HTLCForwardInfo::FailHTLC { htlc_id, err_packet } => {
1720                                                                         log_trace!(self.logger, "Failing HTLC back to channel with short id {} after delay", short_chan_id);
1721                                                                         match chan.get_mut().get_update_fail_htlc(htlc_id, err_packet) {
1722                                                                                 Err(e) => {
1723                                                                                         if let ChannelError::Ignore(msg) = e {
1724                                                                                                 log_trace!(self.logger, "Failed to fail backwards to short_id {}: {}", short_chan_id, msg);
1725                                                                                         } else {
1726                                                                                                 panic!("Stated return value requirements in get_update_fail_htlc() were not met");
1727                                                                                         }
1728                                                                                         // fail-backs are best-effort, we probably already have one
1729                                                                                         // pending, and if not that's OK, if not, the channel is on
1730                                                                                         // the chain and sending the HTLC-Timeout is their problem.
1731                                                                                         continue;
1732                                                                                 },
1733                                                                                 Ok(Some(msg)) => { fail_htlc_msgs.push(msg); },
1734                                                                                 Ok(None) => {
1735                                                                                         // Nothing to do here...we're waiting on a remote
1736                                                                                         // revoke_and_ack before we can update the commitment
1737                                                                                         // transaction. The Channel will automatically handle
1738                                                                                         // building the update_fail_htlc and commitment_signed
1739                                                                                         // messages when we can.
1740                                                                                         // We don't need any kind of timer here as they should fail
1741                                                                                         // the channel onto the chain if they can't get our
1742                                                                                         // update_fail_htlc in time, it's not our problem.
1743                                                                                 }
1744                                                                         }
1745                                                                 },
1746                                                         }
1747                                                 }
1748
1749                                                 if !add_htlc_msgs.is_empty() || !fail_htlc_msgs.is_empty() {
1750                                                         let (commitment_msg, monitor_update) = match chan.get_mut().send_commitment(&self.logger) {
1751                                                                 Ok(res) => res,
1752                                                                 Err(e) => {
1753                                                                         // We surely failed send_commitment due to bad keys, in that case
1754                                                                         // close channel and then send error message to peer.
1755                                                                         let counterparty_node_id = chan.get().get_counterparty_node_id();
1756                                                                         let err: Result<(), _>  = match e {
1757                                                                                 ChannelError::Ignore(_) => {
1758                                                                                         panic!("Stated return value requirements in send_commitment() were not met");
1759                                                                                 },
1760                                                                                 ChannelError::Close(msg) => {
1761                                                                                         log_trace!(self.logger, "Closing channel {} due to Close-required error: {}", log_bytes!(chan.key()[..]), msg);
1762                                                                                         let (channel_id, mut channel) = chan.remove_entry();
1763                                                                                         if let Some(short_id) = channel.get_short_channel_id() {
1764                                                                                                 channel_state.short_to_id.remove(&short_id);
1765                                                                                         }
1766                                                                                         Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, channel.force_shutdown(true), self.get_channel_update(&channel).ok()))
1767                                                                                 },
1768                                                                                 ChannelError::CloseDelayBroadcast(_) => { panic!("Wait is only generated on receipt of channel_reestablish, which is handled by try_chan_entry, we don't bother to support it here"); }
1769                                                                         };
1770                                                                         handle_errors.push((counterparty_node_id, err));
1771                                                                         continue;
1772                                                                 }
1773                                                         };
1774                                                         if let Err(e) = self.chain_monitor.update_channel(chan.get().get_funding_txo().unwrap(), monitor_update) {
1775                                                                 handle_errors.push((chan.get().get_counterparty_node_id(), handle_monitor_err!(self, e, channel_state, chan, RAACommitmentOrder::CommitmentFirst, false, true)));
1776                                                                 continue;
1777                                                         }
1778                                                         channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
1779                                                                 node_id: chan.get().get_counterparty_node_id(),
1780                                                                 updates: msgs::CommitmentUpdate {
1781                                                                         update_add_htlcs: add_htlc_msgs,
1782                                                                         update_fulfill_htlcs: Vec::new(),
1783                                                                         update_fail_htlcs: fail_htlc_msgs,
1784                                                                         update_fail_malformed_htlcs: Vec::new(),
1785                                                                         update_fee: None,
1786                                                                         commitment_signed: commitment_msg,
1787                                                                 },
1788                                                         });
1789                                                 }
1790                                         } else {
1791                                                 unreachable!();
1792                                         }
1793                                 } else {
1794                                         for forward_info in pending_forwards.drain(..) {
1795                                                 match forward_info {
1796                                                         HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info: PendingHTLCInfo {
1797                                                                         routing: PendingHTLCRouting::Receive { payment_data, incoming_cltv_expiry },
1798                                                                         incoming_shared_secret, payment_hash, amt_to_forward, .. },
1799                                                                         prev_funding_outpoint } => {
1800                                                                 let prev_hop = HTLCPreviousHopData {
1801                                                                         short_channel_id: prev_short_channel_id,
1802                                                                         outpoint: prev_funding_outpoint,
1803                                                                         htlc_id: prev_htlc_id,
1804                                                                         incoming_packet_shared_secret: incoming_shared_secret,
1805                                                                 };
1806
1807                                                                 let mut total_value = 0;
1808                                                                 let payment_secret_opt =
1809                                                                         if let &Some(ref data) = &payment_data { Some(data.payment_secret.clone()) } else { None };
1810                                                                 let htlcs = channel_state.claimable_htlcs.entry((payment_hash, payment_secret_opt))
1811                                                                         .or_insert(Vec::new());
1812                                                                 htlcs.push(ClaimableHTLC {
1813                                                                         prev_hop,
1814                                                                         value: amt_to_forward,
1815                                                                         payment_data: payment_data.clone(),
1816                                                                         cltv_expiry: incoming_cltv_expiry,
1817                                                                 });
1818                                                                 if let &Some(ref data) = &payment_data {
1819                                                                         for htlc in htlcs.iter() {
1820                                                                                 total_value += htlc.value;
1821                                                                                 if htlc.payment_data.as_ref().unwrap().total_msat != data.total_msat {
1822                                                                                         total_value = msgs::MAX_VALUE_MSAT;
1823                                                                                 }
1824                                                                                 if total_value >= msgs::MAX_VALUE_MSAT { break; }
1825                                                                         }
1826                                                                         if total_value >= msgs::MAX_VALUE_MSAT || total_value > data.total_msat  {
1827                                                                                 for htlc in htlcs.iter() {
1828                                                                                         let mut htlc_msat_height_data = byte_utils::be64_to_array(htlc.value).to_vec();
1829                                                                                         htlc_msat_height_data.extend_from_slice(
1830                                                                                                 &byte_utils::be32_to_array(
1831                                                                                                         self.latest_block_height.load(Ordering::Acquire)
1832                                                                                                                 as u32,
1833                                                                                                 ),
1834                                                                                         );
1835                                                                                         failed_forwards.push((HTLCSource::PreviousHopData(HTLCPreviousHopData {
1836                                                                                                         short_channel_id: htlc.prev_hop.short_channel_id,
1837                                                                                                         outpoint: prev_funding_outpoint,
1838                                                                                                         htlc_id: htlc.prev_hop.htlc_id,
1839                                                                                                         incoming_packet_shared_secret: htlc.prev_hop.incoming_packet_shared_secret,
1840                                                                                                 }), payment_hash,
1841                                                                                                 HTLCFailReason::Reason { failure_code: 0x4000 | 15, data: htlc_msat_height_data }
1842                                                                                         ));
1843                                                                                 }
1844                                                                         } else if total_value == data.total_msat {
1845                                                                                 new_events.push(events::Event::PaymentReceived {
1846                                                                                         payment_hash,
1847                                                                                         payment_secret: Some(data.payment_secret),
1848                                                                                         amt: total_value,
1849                                                                                 });
1850                                                                         }
1851                                                                 } else {
1852                                                                         new_events.push(events::Event::PaymentReceived {
1853                                                                                 payment_hash,
1854                                                                                 payment_secret: None,
1855                                                                                 amt: amt_to_forward,
1856                                                                         });
1857                                                                 }
1858                                                         },
1859                                                         HTLCForwardInfo::AddHTLC { .. } => {
1860                                                                 panic!("short_channel_id == 0 should imply any pending_forward entries are of type Receive");
1861                                                         },
1862                                                         HTLCForwardInfo::FailHTLC { .. } => {
1863                                                                 panic!("Got pending fail of our own HTLC");
1864                                                         }
1865                                                 }
1866                                         }
1867                                 }
1868                         }
1869                 }
1870
1871                 for (htlc_source, payment_hash, failure_reason) in failed_forwards.drain(..) {
1872                         self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), htlc_source, &payment_hash, failure_reason);
1873                 }
1874
1875                 for (counterparty_node_id, err) in handle_errors.drain(..) {
1876                         let _ = handle_error!(self, err, counterparty_node_id);
1877                 }
1878
1879                 if new_events.is_empty() { return }
1880                 let mut events = self.pending_events.lock().unwrap();
1881                 events.append(&mut new_events);
1882         }
1883
1884         /// Free the background events, generally called from timer_chan_freshness_every_min.
1885         ///
1886         /// Exposed for testing to allow us to process events quickly without generating accidental
1887         /// BroadcastChannelUpdate events in timer_chan_freshness_every_min.
1888         ///
1889         /// Expects the caller to have a total_consistency_lock read lock.
1890         fn process_background_events(&self) {
1891                 let mut background_events = Vec::new();
1892                 mem::swap(&mut *self.pending_background_events.lock().unwrap(), &mut background_events);
1893                 for event in background_events.drain(..) {
1894                         match event {
1895                                 BackgroundEvent::ClosingMonitorUpdate((funding_txo, update)) => {
1896                                         // The channel has already been closed, so no use bothering to care about the
1897                                         // monitor updating completing.
1898                                         let _ = self.chain_monitor.update_channel(funding_txo, update);
1899                                 },
1900                         }
1901                 }
1902         }
1903
1904         #[cfg(any(test, feature = "_test_utils"))]
1905         pub(crate) fn test_process_background_events(&self) {
1906                 self.process_background_events();
1907         }
1908
1909         /// If a peer is disconnected we mark any channels with that peer as 'disabled'.
1910         /// After some time, if channels are still disabled we need to broadcast a ChannelUpdate
1911         /// to inform the network about the uselessness of these channels.
1912         ///
1913         /// This method handles all the details, and must be called roughly once per minute.
1914         ///
1915         /// Note that in some rare cases this may generate a `chain::Watch::update_channel` call.
1916         pub fn timer_chan_freshness_every_min(&self) {
1917                 let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
1918                 self.process_background_events();
1919
1920                 let mut channel_state_lock = self.channel_state.lock().unwrap();
1921                 let channel_state = &mut *channel_state_lock;
1922                 for (_, chan) in channel_state.by_id.iter_mut() {
1923                         if chan.is_disabled_staged() && !chan.is_live() {
1924                                 if let Ok(update) = self.get_channel_update(&chan) {
1925                                         channel_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
1926                                                 msg: update
1927                                         });
1928                                 }
1929                                 chan.to_fresh();
1930                         } else if chan.is_disabled_staged() && chan.is_live() {
1931                                 chan.to_fresh();
1932                         } else if chan.is_disabled_marked() {
1933                                 chan.to_disabled_staged();
1934                         }
1935                 }
1936         }
1937
1938         /// Indicates that the preimage for payment_hash is unknown or the received amount is incorrect
1939         /// after a PaymentReceived event, failing the HTLC back to its origin and freeing resources
1940         /// along the path (including in our own channel on which we received it).
1941         /// Returns false if no payment was found to fail backwards, true if the process of failing the
1942         /// HTLC backwards has been started.
1943         pub fn fail_htlc_backwards(&self, payment_hash: &PaymentHash, payment_secret: &Option<PaymentSecret>) -> bool {
1944                 let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
1945
1946                 let mut channel_state = Some(self.channel_state.lock().unwrap());
1947                 let removed_source = channel_state.as_mut().unwrap().claimable_htlcs.remove(&(*payment_hash, *payment_secret));
1948                 if let Some(mut sources) = removed_source {
1949                         for htlc in sources.drain(..) {
1950                                 if channel_state.is_none() { channel_state = Some(self.channel_state.lock().unwrap()); }
1951                                 let mut htlc_msat_height_data = byte_utils::be64_to_array(htlc.value).to_vec();
1952                                 htlc_msat_height_data.extend_from_slice(&byte_utils::be32_to_array(
1953                                         self.latest_block_height.load(Ordering::Acquire) as u32,
1954                                 ));
1955                                 self.fail_htlc_backwards_internal(channel_state.take().unwrap(),
1956                                                 HTLCSource::PreviousHopData(htlc.prev_hop), payment_hash,
1957                                                 HTLCFailReason::Reason { failure_code: 0x4000 | 15, data: htlc_msat_height_data });
1958                         }
1959                         true
1960                 } else { false }
1961         }
1962
1963         // Fail a list of HTLCs that were just freed from the holding cell. The HTLCs need to be
1964         // failed backwards or, if they were one of our outgoing HTLCs, then their failure needs to
1965         // be surfaced to the user.
1966         fn fail_holding_cell_htlcs(&self, mut htlcs_to_fail: Vec<(HTLCSource, PaymentHash)>, channel_id: [u8; 32]) {
1967                 for (htlc_src, payment_hash) in htlcs_to_fail.drain(..) {
1968                         match htlc_src {
1969                                 HTLCSource::PreviousHopData(HTLCPreviousHopData { .. }) => {
1970                                         let (failure_code, onion_failure_data) =
1971                                                 match self.channel_state.lock().unwrap().by_id.entry(channel_id) {
1972                                                         hash_map::Entry::Occupied(chan_entry) => {
1973                                                                 if let Ok(upd) = self.get_channel_update(&chan_entry.get()) {
1974                                                                         (0x1000|7, upd.encode_with_len())
1975                                                                 } else {
1976                                                                         (0x4000|10, Vec::new())
1977                                                                 }
1978                                                         },
1979                                                         hash_map::Entry::Vacant(_) => (0x4000|10, Vec::new())
1980                                                 };
1981                                         let channel_state = self.channel_state.lock().unwrap();
1982                                         self.fail_htlc_backwards_internal(channel_state,
1983                                                 htlc_src, &payment_hash, HTLCFailReason::Reason { failure_code, data: onion_failure_data});
1984                                 },
1985                                 HTLCSource::OutboundRoute { .. } => {
1986                                         self.pending_events.lock().unwrap().push(
1987                                                 events::Event::PaymentFailed {
1988                                                         payment_hash,
1989                                                         rejected_by_dest: false,
1990 #[cfg(test)]
1991                                                         error_code: None,
1992 #[cfg(test)]
1993                                                         error_data: None,
1994                                                 }
1995                                         )
1996                                 },
1997                         };
1998                 }
1999         }
2000
2001         /// Fails an HTLC backwards to the sender of it to us.
2002         /// Note that while we take a channel_state lock as input, we do *not* assume consistency here.
2003         /// There are several callsites that do stupid things like loop over a list of payment_hashes
2004         /// to fail and take the channel_state lock for each iteration (as we take ownership and may
2005         /// drop it). In other words, no assumptions are made that entries in claimable_htlcs point to
2006         /// still-available channels.
2007         fn fail_htlc_backwards_internal(&self, mut channel_state_lock: MutexGuard<ChannelHolder<Signer>>, source: HTLCSource, payment_hash: &PaymentHash, onion_error: HTLCFailReason) {
2008                 //TODO: There is a timing attack here where if a node fails an HTLC back to us they can
2009                 //identify whether we sent it or not based on the (I presume) very different runtime
2010                 //between the branches here. We should make this async and move it into the forward HTLCs
2011                 //timer handling.
2012
2013                 // Note that we MUST NOT end up calling methods on self.chain_monitor here - we're called
2014                 // from block_connected which may run during initialization prior to the chain_monitor
2015                 // being fully configured. See the docs for `ChannelManagerReadArgs` for more.
2016                 match source {
2017                         HTLCSource::OutboundRoute { ref path, .. } => {
2018                                 log_trace!(self.logger, "Failing outbound payment HTLC with payment_hash {}", log_bytes!(payment_hash.0));
2019                                 mem::drop(channel_state_lock);
2020                                 match &onion_error {
2021                                         &HTLCFailReason::LightningError { ref err } => {
2022 #[cfg(test)]
2023                                                 let (channel_update, payment_retryable, onion_error_code, onion_error_data) = onion_utils::process_onion_failure(&self.secp_ctx, &self.logger, &source, err.data.clone());
2024 #[cfg(not(test))]
2025                                                 let (channel_update, payment_retryable, _, _) = onion_utils::process_onion_failure(&self.secp_ctx, &self.logger, &source, err.data.clone());
2026                                                 // TODO: If we decided to blame ourselves (or one of our channels) in
2027                                                 // process_onion_failure we should close that channel as it implies our
2028                                                 // next-hop is needlessly blaming us!
2029                                                 if let Some(update) = channel_update {
2030                                                         self.channel_state.lock().unwrap().pending_msg_events.push(
2031                                                                 events::MessageSendEvent::PaymentFailureNetworkUpdate {
2032                                                                         update,
2033                                                                 }
2034                                                         );
2035                                                 }
2036                                                 self.pending_events.lock().unwrap().push(
2037                                                         events::Event::PaymentFailed {
2038                                                                 payment_hash: payment_hash.clone(),
2039                                                                 rejected_by_dest: !payment_retryable,
2040 #[cfg(test)]
2041                                                                 error_code: onion_error_code,
2042 #[cfg(test)]
2043                                                                 error_data: onion_error_data
2044                                                         }
2045                                                 );
2046                                         },
2047                                         &HTLCFailReason::Reason {
2048 #[cfg(test)]
2049                                                         ref failure_code,
2050 #[cfg(test)]
2051                                                         ref data,
2052                                                         .. } => {
2053                                                 // we get a fail_malformed_htlc from the first hop
2054                                                 // TODO: We'd like to generate a PaymentFailureNetworkUpdate for temporary
2055                                                 // failures here, but that would be insufficient as get_route
2056                                                 // generally ignores its view of our own channels as we provide them via
2057                                                 // ChannelDetails.
2058                                                 // TODO: For non-temporary failures, we really should be closing the
2059                                                 // channel here as we apparently can't relay through them anyway.
2060                                                 self.pending_events.lock().unwrap().push(
2061                                                         events::Event::PaymentFailed {
2062                                                                 payment_hash: payment_hash.clone(),
2063                                                                 rejected_by_dest: path.len() == 1,
2064 #[cfg(test)]
2065                                                                 error_code: Some(*failure_code),
2066 #[cfg(test)]
2067                                                                 error_data: Some(data.clone()),
2068                                                         }
2069                                                 );
2070                                         }
2071                                 }
2072                         },
2073                         HTLCSource::PreviousHopData(HTLCPreviousHopData { short_channel_id, htlc_id, incoming_packet_shared_secret, .. }) => {
2074                                 let err_packet = match onion_error {
2075                                         HTLCFailReason::Reason { failure_code, data } => {
2076                                                 log_trace!(self.logger, "Failing HTLC with payment_hash {} backwards from us with code {}", log_bytes!(payment_hash.0), failure_code);
2077                                                 let packet = onion_utils::build_failure_packet(&incoming_packet_shared_secret, failure_code, &data[..]).encode();
2078                                                 onion_utils::encrypt_failure_packet(&incoming_packet_shared_secret, &packet)
2079                                         },
2080                                         HTLCFailReason::LightningError { err } => {
2081                                                 log_trace!(self.logger, "Failing HTLC with payment_hash {} backwards with pre-built LightningError", log_bytes!(payment_hash.0));
2082                                                 onion_utils::encrypt_failure_packet(&incoming_packet_shared_secret, &err.data)
2083                                         }
2084                                 };
2085
2086                                 let mut forward_event = None;
2087                                 if channel_state_lock.forward_htlcs.is_empty() {
2088                                         forward_event = Some(Duration::from_millis(MIN_HTLC_RELAY_HOLDING_CELL_MILLIS));
2089                                 }
2090                                 match channel_state_lock.forward_htlcs.entry(short_channel_id) {
2091                                         hash_map::Entry::Occupied(mut entry) => {
2092                                                 entry.get_mut().push(HTLCForwardInfo::FailHTLC { htlc_id, err_packet });
2093                                         },
2094                                         hash_map::Entry::Vacant(entry) => {
2095                                                 entry.insert(vec!(HTLCForwardInfo::FailHTLC { htlc_id, err_packet }));
2096                                         }
2097                                 }
2098                                 mem::drop(channel_state_lock);
2099                                 if let Some(time) = forward_event {
2100                                         let mut pending_events = self.pending_events.lock().unwrap();
2101                                         pending_events.push(events::Event::PendingHTLCsForwardable {
2102                                                 time_forwardable: time
2103                                         });
2104                                 }
2105                         },
2106                 }
2107         }
2108
2109         /// Provides a payment preimage in response to a PaymentReceived event, returning true and
2110         /// generating message events for the net layer to claim the payment, if possible. Thus, you
2111         /// should probably kick the net layer to go send messages if this returns true!
2112         ///
2113         /// You must specify the expected amounts for this HTLC, and we will only claim HTLCs
2114         /// available within a few percent of the expected amount. This is critical for several
2115         /// reasons : a) it avoids providing senders with `proof-of-payment` (in the form of the
2116         /// payment_preimage without having provided the full value and b) it avoids certain
2117         /// privacy-breaking recipient-probing attacks which may reveal payment activity to
2118         /// motivated attackers.
2119         ///
2120         /// Note that the privacy concerns in (b) are not relevant in payments with a payment_secret
2121         /// set. Thus, for such payments we will claim any payments which do not under-pay.
2122         ///
2123         /// May panic if called except in response to a PaymentReceived event.
2124         pub fn claim_funds(&self, payment_preimage: PaymentPreimage, payment_secret: &Option<PaymentSecret>, expected_amount: u64) -> bool {
2125                 let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0).into_inner());
2126
2127                 let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
2128
2129                 let mut channel_state = Some(self.channel_state.lock().unwrap());
2130                 let removed_source = channel_state.as_mut().unwrap().claimable_htlcs.remove(&(payment_hash, *payment_secret));
2131                 if let Some(mut sources) = removed_source {
2132                         assert!(!sources.is_empty());
2133
2134                         // If we are claiming an MPP payment, we have to take special care to ensure that each
2135                         // channel exists before claiming all of the payments (inside one lock).
2136                         // Note that channel existance is sufficient as we should always get a monitor update
2137                         // which will take care of the real HTLC claim enforcement.
2138                         //
2139                         // If we find an HTLC which we would need to claim but for which we do not have a
2140                         // channel, we will fail all parts of the MPP payment. While we could wait and see if
2141                         // the sender retries the already-failed path(s), it should be a pretty rare case where
2142                         // we got all the HTLCs and then a channel closed while we were waiting for the user to
2143                         // provide the preimage, so worrying too much about the optimal handling isn't worth
2144                         // it.
2145
2146                         let (is_mpp, mut valid_mpp) = if let &Some(ref data) = &sources[0].payment_data {
2147                                 assert!(payment_secret.is_some());
2148                                 (true, data.total_msat >= expected_amount)
2149                         } else {
2150                                 assert!(payment_secret.is_none());
2151                                 (false, false)
2152                         };
2153
2154                         for htlc in sources.iter() {
2155                                 if !is_mpp || !valid_mpp { break; }
2156                                 if let None = channel_state.as_ref().unwrap().short_to_id.get(&htlc.prev_hop.short_channel_id) {
2157                                         valid_mpp = false;
2158                                 }
2159                         }
2160
2161                         let mut errs = Vec::new();
2162                         let mut claimed_any_htlcs = false;
2163                         for htlc in sources.drain(..) {
2164                                 if channel_state.is_none() { channel_state = Some(self.channel_state.lock().unwrap()); }
2165                                 if (is_mpp && !valid_mpp) || (!is_mpp && (htlc.value < expected_amount || htlc.value > expected_amount * 2)) {
2166                                         let mut htlc_msat_height_data = byte_utils::be64_to_array(htlc.value).to_vec();
2167                                         htlc_msat_height_data.extend_from_slice(&byte_utils::be32_to_array(
2168                                                 self.latest_block_height.load(Ordering::Acquire) as u32,
2169                                         ));
2170                                         self.fail_htlc_backwards_internal(channel_state.take().unwrap(),
2171                                                                          HTLCSource::PreviousHopData(htlc.prev_hop), &payment_hash,
2172                                                                          HTLCFailReason::Reason { failure_code: 0x4000|15, data: htlc_msat_height_data });
2173                                 } else {
2174                                         match self.claim_funds_from_hop(channel_state.as_mut().unwrap(), htlc.prev_hop, payment_preimage) {
2175                                                 Err(Some(e)) => {
2176                                                         if let msgs::ErrorAction::IgnoreError = e.1.err.action {
2177                                                                 // We got a temporary failure updating monitor, but will claim the
2178                                                                 // HTLC when the monitor updating is restored (or on chain).
2179                                                                 log_error!(self.logger, "Temporary failure claiming HTLC, treating as success: {}", e.1.err.err);
2180                                                                 claimed_any_htlcs = true;
2181                                                         } else { errs.push(e); }
2182                                                 },
2183                                                 Err(None) if is_mpp => unreachable!("We already checked for channel existence, we can't fail here!"),
2184                                                 Err(None) => {
2185                                                         log_warn!(self.logger, "Channel we expected to claim an HTLC from was closed.");
2186                                                 },
2187                                                 Ok(()) => claimed_any_htlcs = true,
2188                                         }
2189                                 }
2190                         }
2191
2192                         // Now that we've done the entire above loop in one lock, we can handle any errors
2193                         // which were generated.
2194                         channel_state.take();
2195
2196                         for (counterparty_node_id, err) in errs.drain(..) {
2197                                 let res: Result<(), _> = Err(err);
2198                                 let _ = handle_error!(self, res, counterparty_node_id);
2199                         }
2200
2201                         claimed_any_htlcs
2202                 } else { false }
2203         }
2204
2205         fn claim_funds_from_hop(&self, channel_state_lock: &mut MutexGuard<ChannelHolder<Signer>>, prev_hop: HTLCPreviousHopData, payment_preimage: PaymentPreimage) -> Result<(), Option<(PublicKey, MsgHandleErrInternal)>> {
2206                 //TODO: Delay the claimed_funds relaying just like we do outbound relay!
2207                 let channel_state = &mut **channel_state_lock;
2208                 let chan_id = match channel_state.short_to_id.get(&prev_hop.short_channel_id) {
2209                         Some(chan_id) => chan_id.clone(),
2210                         None => {
2211                                 return Err(None)
2212                         }
2213                 };
2214
2215                 if let hash_map::Entry::Occupied(mut chan) = channel_state.by_id.entry(chan_id) {
2216                         let was_frozen_for_monitor = chan.get().is_awaiting_monitor_update();
2217                         match chan.get_mut().get_update_fulfill_htlc_and_commit(prev_hop.htlc_id, payment_preimage, &self.logger) {
2218                                 Ok((msgs, monitor_option)) => {
2219                                         if let Some(monitor_update) = monitor_option {
2220                                                 if let Err(e) = self.chain_monitor.update_channel(chan.get().get_funding_txo().unwrap(), monitor_update) {
2221                                                         if was_frozen_for_monitor {
2222                                                                 assert!(msgs.is_none());
2223                                                         } else {
2224                                                                 return Err(Some((chan.get().get_counterparty_node_id(), handle_monitor_err!(self, e, channel_state, chan, RAACommitmentOrder::CommitmentFirst, false, msgs.is_some()).unwrap_err())));
2225                                                         }
2226                                                 }
2227                                         }
2228                                         if let Some((msg, commitment_signed)) = msgs {
2229                                                 channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
2230                                                         node_id: chan.get().get_counterparty_node_id(),
2231                                                         updates: msgs::CommitmentUpdate {
2232                                                                 update_add_htlcs: Vec::new(),
2233                                                                 update_fulfill_htlcs: vec![msg],
2234                                                                 update_fail_htlcs: Vec::new(),
2235                                                                 update_fail_malformed_htlcs: Vec::new(),
2236                                                                 update_fee: None,
2237                                                                 commitment_signed,
2238                                                         }
2239                                                 });
2240                                         }
2241                                         return Ok(())
2242                                 },
2243                                 Err(e) => {
2244                                         // TODO: Do something with e?
2245                                         // This should only occur if we are claiming an HTLC at the same time as the
2246                                         // HTLC is being failed (eg because a block is being connected and this caused
2247                                         // an HTLC to time out). This should, of course, only occur if the user is the
2248                                         // one doing the claiming (as it being a part of a peer claim would imply we're
2249                                         // about to lose funds) and only if the lock in claim_funds was dropped as a
2250                                         // previous HTLC was failed (thus not for an MPP payment).
2251                                         debug_assert!(false, "This shouldn't be reachable except in absurdly rare cases between monitor updates and HTLC timeouts: {:?}", e);
2252                                         return Err(None)
2253                                 },
2254                         }
2255                 } else { unreachable!(); }
2256         }
2257
2258         fn claim_funds_internal(&self, mut channel_state_lock: MutexGuard<ChannelHolder<Signer>>, source: HTLCSource, payment_preimage: PaymentPreimage) {
2259                 match source {
2260                         HTLCSource::OutboundRoute { .. } => {
2261                                 mem::drop(channel_state_lock);
2262                                 let mut pending_events = self.pending_events.lock().unwrap();
2263                                 pending_events.push(events::Event::PaymentSent {
2264                                         payment_preimage
2265                                 });
2266                         },
2267                         HTLCSource::PreviousHopData(hop_data) => {
2268                                 let prev_outpoint = hop_data.outpoint;
2269                                 if let Err((counterparty_node_id, err)) = match self.claim_funds_from_hop(&mut channel_state_lock, hop_data, payment_preimage) {
2270                                         Ok(()) => Ok(()),
2271                                         Err(None) => {
2272                                                 let preimage_update = ChannelMonitorUpdate {
2273                                                         update_id: CLOSED_CHANNEL_UPDATE_ID,
2274                                                         updates: vec![ChannelMonitorUpdateStep::PaymentPreimage {
2275                                                                 payment_preimage: payment_preimage.clone(),
2276                                                         }],
2277                                                 };
2278                                                 // We update the ChannelMonitor on the backward link, after
2279                                                 // receiving an offchain preimage event from the forward link (the
2280                                                 // event being update_fulfill_htlc).
2281                                                 if let Err(e) = self.chain_monitor.update_channel(prev_outpoint, preimage_update) {
2282                                                         log_error!(self.logger, "Critical error: failed to update channel monitor with preimage {:?}: {:?}",
2283                                                                    payment_preimage, e);
2284                                                 }
2285                                                 Ok(())
2286                                         },
2287                                         Err(Some(res)) => Err(res),
2288                                 } {
2289                                         mem::drop(channel_state_lock);
2290                                         let res: Result<(), _> = Err(err);
2291                                         let _ = handle_error!(self, res, counterparty_node_id);
2292                                 }
2293                         },
2294                 }
2295         }
2296
2297         /// Gets the node_id held by this ChannelManager
2298         pub fn get_our_node_id(&self) -> PublicKey {
2299                 PublicKey::from_secret_key(&self.secp_ctx, &self.our_network_key)
2300         }
2301
2302         /// Restores a single, given channel to normal operation after a
2303         /// ChannelMonitorUpdateErr::TemporaryFailure was returned from a channel monitor update
2304         /// operation.
2305         ///
2306         /// All ChannelMonitor updates up to and including highest_applied_update_id must have been
2307         /// fully committed in every copy of the given channels' ChannelMonitors.
2308         ///
2309         /// Note that there is no effect to calling with a highest_applied_update_id other than the
2310         /// current latest ChannelMonitorUpdate and one call to this function after multiple
2311         /// ChannelMonitorUpdateErr::TemporaryFailures is fine. The highest_applied_update_id field
2312         /// exists largely only to prevent races between this and concurrent update_monitor calls.
2313         ///
2314         /// Thus, the anticipated use is, at a high level:
2315         ///  1) You register a chain::Watch with this ChannelManager,
2316         ///  2) it stores each update to disk, and begins updating any remote (eg watchtower) copies of
2317         ///     said ChannelMonitors as it can, returning ChannelMonitorUpdateErr::TemporaryFailures
2318         ///     any time it cannot do so instantly,
2319         ///  3) update(s) are applied to each remote copy of a ChannelMonitor,
2320         ///  4) once all remote copies are updated, you call this function with the update_id that
2321         ///     completed, and once it is the latest the Channel will be re-enabled.
2322         pub fn channel_monitor_updated(&self, funding_txo: &OutPoint, highest_applied_update_id: u64) {
2323                 let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
2324
2325                 let mut close_results = Vec::new();
2326                 let mut htlc_forwards = Vec::new();
2327                 let mut htlc_failures = Vec::new();
2328                 let mut pending_events = Vec::new();
2329
2330                 {
2331                         let mut channel_lock = self.channel_state.lock().unwrap();
2332                         let channel_state = &mut *channel_lock;
2333                         let short_to_id = &mut channel_state.short_to_id;
2334                         let pending_msg_events = &mut channel_state.pending_msg_events;
2335                         let channel = match channel_state.by_id.get_mut(&funding_txo.to_channel_id()) {
2336                                 Some(chan) => chan,
2337                                 None => return,
2338                         };
2339                         if !channel.is_awaiting_monitor_update() || channel.get_latest_monitor_update_id() != highest_applied_update_id {
2340                                 return;
2341                         }
2342
2343                         let (raa, commitment_update, order, pending_forwards, mut pending_failures, needs_broadcast_safe, funding_locked) = channel.monitor_updating_restored(&self.logger);
2344                         if !pending_forwards.is_empty() {
2345                                 htlc_forwards.push((channel.get_short_channel_id().expect("We can't have pending forwards before funding confirmation"), funding_txo.clone(), pending_forwards));
2346                         }
2347                         htlc_failures.append(&mut pending_failures);
2348
2349                         macro_rules! handle_cs { () => {
2350                                 if let Some(update) = commitment_update {
2351                                         pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
2352                                                 node_id: channel.get_counterparty_node_id(),
2353                                                 updates: update,
2354                                         });
2355                                 }
2356                         } }
2357                         macro_rules! handle_raa { () => {
2358                                 if let Some(revoke_and_ack) = raa {
2359                                         pending_msg_events.push(events::MessageSendEvent::SendRevokeAndACK {
2360                                                 node_id: channel.get_counterparty_node_id(),
2361                                                 msg: revoke_and_ack,
2362                                         });
2363                                 }
2364                         } }
2365                         match order {
2366                                 RAACommitmentOrder::CommitmentFirst => {
2367                                         handle_cs!();
2368                                         handle_raa!();
2369                                 },
2370                                 RAACommitmentOrder::RevokeAndACKFirst => {
2371                                         handle_raa!();
2372                                         handle_cs!();
2373                                 },
2374                         }
2375                         if needs_broadcast_safe {
2376                                 pending_events.push(events::Event::FundingBroadcastSafe {
2377                                         funding_txo: channel.get_funding_txo().unwrap(),
2378                                         user_channel_id: channel.get_user_id(),
2379                                 });
2380                         }
2381                         if let Some(msg) = funding_locked {
2382                                 pending_msg_events.push(events::MessageSendEvent::SendFundingLocked {
2383                                         node_id: channel.get_counterparty_node_id(),
2384                                         msg,
2385                                 });
2386                                 if let Some(announcement_sigs) = self.get_announcement_sigs(channel) {
2387                                         pending_msg_events.push(events::MessageSendEvent::SendAnnouncementSignatures {
2388                                                 node_id: channel.get_counterparty_node_id(),
2389                                                 msg: announcement_sigs,
2390                                         });
2391                                 }
2392                                 short_to_id.insert(channel.get_short_channel_id().unwrap(), channel.channel_id());
2393                         }
2394                 }
2395
2396                 self.pending_events.lock().unwrap().append(&mut pending_events);
2397
2398                 for failure in htlc_failures.drain(..) {
2399                         self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), failure.0, &failure.1, failure.2);
2400                 }
2401                 self.forward_htlcs(&mut htlc_forwards[..]);
2402
2403                 for res in close_results.drain(..) {
2404                         self.finish_force_close_channel(res);
2405                 }
2406         }
2407
2408         fn internal_open_channel(&self, counterparty_node_id: &PublicKey, their_features: InitFeatures, msg: &msgs::OpenChannel) -> Result<(), MsgHandleErrInternal> {
2409                 if msg.chain_hash != self.genesis_hash {
2410                         return Err(MsgHandleErrInternal::send_err_msg_no_close("Unknown genesis block hash".to_owned(), msg.temporary_channel_id.clone()));
2411                 }
2412
2413                 let channel = Channel::new_from_req(&self.fee_estimator, &self.keys_manager, counterparty_node_id.clone(), their_features, msg, 0, &self.default_configuration)
2414                         .map_err(|e| MsgHandleErrInternal::from_chan_no_close(e, msg.temporary_channel_id))?;
2415                 let mut channel_state_lock = self.channel_state.lock().unwrap();
2416                 let channel_state = &mut *channel_state_lock;
2417                 match channel_state.by_id.entry(channel.channel_id()) {
2418                         hash_map::Entry::Occupied(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("temporary_channel_id collision!".to_owned(), msg.temporary_channel_id.clone())),
2419                         hash_map::Entry::Vacant(entry) => {
2420                                 channel_state.pending_msg_events.push(events::MessageSendEvent::SendAcceptChannel {
2421                                         node_id: counterparty_node_id.clone(),
2422                                         msg: channel.get_accept_channel(),
2423                                 });
2424                                 entry.insert(channel);
2425                         }
2426                 }
2427                 Ok(())
2428         }
2429
2430         fn internal_accept_channel(&self, counterparty_node_id: &PublicKey, their_features: InitFeatures, msg: &msgs::AcceptChannel) -> Result<(), MsgHandleErrInternal> {
2431                 let (value, output_script, user_id) = {
2432                         let mut channel_lock = self.channel_state.lock().unwrap();
2433                         let channel_state = &mut *channel_lock;
2434                         match channel_state.by_id.entry(msg.temporary_channel_id) {
2435                                 hash_map::Entry::Occupied(mut chan) => {
2436                                         if chan.get().get_counterparty_node_id() != *counterparty_node_id {
2437                                                 return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!".to_owned(), msg.temporary_channel_id));
2438                                         }
2439                                         try_chan_entry!(self, chan.get_mut().accept_channel(&msg, &self.default_configuration, their_features), channel_state, chan);
2440                                         (chan.get().get_value_satoshis(), chan.get().get_funding_redeemscript().to_v0_p2wsh(), chan.get().get_user_id())
2441                                 },
2442                                 hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.temporary_channel_id))
2443                         }
2444                 };
2445                 let mut pending_events = self.pending_events.lock().unwrap();
2446                 pending_events.push(events::Event::FundingGenerationReady {
2447                         temporary_channel_id: msg.temporary_channel_id,
2448                         channel_value_satoshis: value,
2449                         output_script,
2450                         user_channel_id: user_id,
2451                 });
2452                 Ok(())
2453         }
2454
2455         fn internal_funding_created(&self, counterparty_node_id: &PublicKey, msg: &msgs::FundingCreated) -> Result<(), MsgHandleErrInternal> {
2456                 let ((funding_msg, monitor), mut chan) = {
2457                         let last_block_hash = *self.last_block_hash.read().unwrap();
2458                         let mut channel_lock = self.channel_state.lock().unwrap();
2459                         let channel_state = &mut *channel_lock;
2460                         match channel_state.by_id.entry(msg.temporary_channel_id.clone()) {
2461                                 hash_map::Entry::Occupied(mut chan) => {
2462                                         if chan.get().get_counterparty_node_id() != *counterparty_node_id {
2463                                                 return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!".to_owned(), msg.temporary_channel_id));
2464                                         }
2465                                         (try_chan_entry!(self, chan.get_mut().funding_created(msg, last_block_hash, &self.logger), channel_state, chan), chan.remove())
2466                                 },
2467                                 hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.temporary_channel_id))
2468                         }
2469                 };
2470                 // Because we have exclusive ownership of the channel here we can release the channel_state
2471                 // lock before watch_channel
2472                 if let Err(e) = self.chain_monitor.watch_channel(monitor.get_funding_txo().0, monitor) {
2473                         match e {
2474                                 ChannelMonitorUpdateErr::PermanentFailure => {
2475                                         // Note that we reply with the new channel_id in error messages if we gave up on the
2476                                         // channel, not the temporary_channel_id. This is compatible with ourselves, but the
2477                                         // spec is somewhat ambiguous here. Not a huge deal since we'll send error messages for
2478                                         // any messages referencing a previously-closed channel anyway.
2479                                         // We do not do a force-close here as that would generate a monitor update for
2480                                         // a monitor that we didn't manage to store (and that we don't care about - we
2481                                         // don't respond with the funding_signed so the channel can never go on chain).
2482                                         let (_monitor_update, failed_htlcs) = chan.force_shutdown(true);
2483                                         assert!(failed_htlcs.is_empty());
2484                                         return Err(MsgHandleErrInternal::send_err_msg_no_close("ChannelMonitor storage failure".to_owned(), funding_msg.channel_id));
2485                                 },
2486                                 ChannelMonitorUpdateErr::TemporaryFailure => {
2487                                         // There's no problem signing a counterparty's funding transaction if our monitor
2488                                         // hasn't persisted to disk yet - we can't lose money on a transaction that we haven't
2489                                         // accepted payment from yet. We do, however, need to wait to send our funding_locked
2490                                         // until we have persisted our monitor.
2491                                         chan.monitor_update_failed(false, false, Vec::new(), Vec::new());
2492                                 },
2493                         }
2494                 }
2495                 let mut channel_state_lock = self.channel_state.lock().unwrap();
2496                 let channel_state = &mut *channel_state_lock;
2497                 match channel_state.by_id.entry(funding_msg.channel_id) {
2498                         hash_map::Entry::Occupied(_) => {
2499                                 return Err(MsgHandleErrInternal::send_err_msg_no_close("Already had channel with the new channel_id".to_owned(), funding_msg.channel_id))
2500                         },
2501                         hash_map::Entry::Vacant(e) => {
2502                                 channel_state.pending_msg_events.push(events::MessageSendEvent::SendFundingSigned {
2503                                         node_id: counterparty_node_id.clone(),
2504                                         msg: funding_msg,
2505                                 });
2506                                 e.insert(chan);
2507                         }
2508                 }
2509                 Ok(())
2510         }
2511
2512         fn internal_funding_signed(&self, counterparty_node_id: &PublicKey, msg: &msgs::FundingSigned) -> Result<(), MsgHandleErrInternal> {
2513                 let (funding_txo, user_id) = {
2514                         let last_block_hash = *self.last_block_hash.read().unwrap();
2515                         let mut channel_lock = self.channel_state.lock().unwrap();
2516                         let channel_state = &mut *channel_lock;
2517                         match channel_state.by_id.entry(msg.channel_id) {
2518                                 hash_map::Entry::Occupied(mut chan) => {
2519                                         if chan.get().get_counterparty_node_id() != *counterparty_node_id {
2520                                                 return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!".to_owned(), msg.channel_id));
2521                                         }
2522                                         let monitor = match chan.get_mut().funding_signed(&msg, last_block_hash, &self.logger) {
2523                                                 Ok(update) => update,
2524                                                 Err(e) => try_chan_entry!(self, Err(e), channel_state, chan),
2525                                         };
2526                                         if let Err(e) = self.chain_monitor.watch_channel(chan.get().get_funding_txo().unwrap(), monitor) {
2527                                                 return_monitor_err!(self, e, channel_state, chan, RAACommitmentOrder::RevokeAndACKFirst, false, false);
2528                                         }
2529                                         (chan.get().get_funding_txo().unwrap(), chan.get().get_user_id())
2530                                 },
2531                                 hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id))
2532                         }
2533                 };
2534                 let mut pending_events = self.pending_events.lock().unwrap();
2535                 pending_events.push(events::Event::FundingBroadcastSafe {
2536                         funding_txo,
2537                         user_channel_id: user_id,
2538                 });
2539                 Ok(())
2540         }
2541
2542         fn internal_funding_locked(&self, counterparty_node_id: &PublicKey, msg: &msgs::FundingLocked) -> Result<(), MsgHandleErrInternal> {
2543                 let mut channel_state_lock = self.channel_state.lock().unwrap();
2544                 let channel_state = &mut *channel_state_lock;
2545                 match channel_state.by_id.entry(msg.channel_id) {
2546                         hash_map::Entry::Occupied(mut chan) => {
2547                                 if chan.get().get_counterparty_node_id() != *counterparty_node_id {
2548                                         return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!".to_owned(), msg.channel_id));
2549                                 }
2550                                 try_chan_entry!(self, chan.get_mut().funding_locked(&msg), channel_state, chan);
2551                                 if let Some(announcement_sigs) = self.get_announcement_sigs(chan.get()) {
2552                                         log_trace!(self.logger, "Sending announcement_signatures for {} in response to funding_locked", log_bytes!(chan.get().channel_id()));
2553                                         // If we see locking block before receiving remote funding_locked, we broadcast our
2554                                         // announcement_sigs at remote funding_locked reception. If we receive remote
2555                                         // funding_locked before seeing locking block, we broadcast our announcement_sigs at locking
2556                                         // block connection. We should guanrantee to broadcast announcement_sigs to our peer whatever
2557                                         // the order of the events but our peer may not receive it due to disconnection. The specs
2558                                         // lacking an acknowledgement for announcement_sigs we may have to re-send them at peer
2559                                         // connection in the future if simultaneous misses by both peers due to network/hardware
2560                                         // failures is an issue. Note, to achieve its goal, only one of the announcement_sigs needs
2561                                         // to be received, from then sigs are going to be flood to the whole network.
2562                                         channel_state.pending_msg_events.push(events::MessageSendEvent::SendAnnouncementSignatures {
2563                                                 node_id: counterparty_node_id.clone(),
2564                                                 msg: announcement_sigs,
2565                                         });
2566                                 }
2567                                 Ok(())
2568                         },
2569                         hash_map::Entry::Vacant(_) => Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id))
2570                 }
2571         }
2572
2573         fn internal_shutdown(&self, counterparty_node_id: &PublicKey, their_features: &InitFeatures, msg: &msgs::Shutdown) -> Result<(), MsgHandleErrInternal> {
2574                 let (mut dropped_htlcs, chan_option) = {
2575                         let mut channel_state_lock = self.channel_state.lock().unwrap();
2576                         let channel_state = &mut *channel_state_lock;
2577
2578                         match channel_state.by_id.entry(msg.channel_id.clone()) {
2579                                 hash_map::Entry::Occupied(mut chan_entry) => {
2580                                         if chan_entry.get().get_counterparty_node_id() != *counterparty_node_id {
2581                                                 return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!".to_owned(), msg.channel_id));
2582                                         }
2583                                         let (shutdown, closing_signed, dropped_htlcs) = try_chan_entry!(self, chan_entry.get_mut().shutdown(&self.fee_estimator, &their_features, &msg), channel_state, chan_entry);
2584                                         if let Some(msg) = shutdown {
2585                                                 channel_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
2586                                                         node_id: counterparty_node_id.clone(),
2587                                                         msg,
2588                                                 });
2589                                         }
2590                                         if let Some(msg) = closing_signed {
2591                                                 channel_state.pending_msg_events.push(events::MessageSendEvent::SendClosingSigned {
2592                                                         node_id: counterparty_node_id.clone(),
2593                                                         msg,
2594                                                 });
2595                                         }
2596                                         if chan_entry.get().is_shutdown() {
2597                                                 if let Some(short_id) = chan_entry.get().get_short_channel_id() {
2598                                                         channel_state.short_to_id.remove(&short_id);
2599                                                 }
2600                                                 (dropped_htlcs, Some(chan_entry.remove_entry().1))
2601                                         } else { (dropped_htlcs, None) }
2602                                 },
2603                                 hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id))
2604                         }
2605                 };
2606                 for htlc_source in dropped_htlcs.drain(..) {
2607                         self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), htlc_source.0, &htlc_source.1, HTLCFailReason::Reason { failure_code: 0x4000 | 8, data: Vec::new() });
2608                 }
2609                 if let Some(chan) = chan_option {
2610                         if let Ok(update) = self.get_channel_update(&chan) {
2611                                 let mut channel_state = self.channel_state.lock().unwrap();
2612                                 channel_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
2613                                         msg: update
2614                                 });
2615                         }
2616                 }
2617                 Ok(())
2618         }
2619
2620         fn internal_closing_signed(&self, counterparty_node_id: &PublicKey, msg: &msgs::ClosingSigned) -> Result<(), MsgHandleErrInternal> {
2621                 let (tx, chan_option) = {
2622                         let mut channel_state_lock = self.channel_state.lock().unwrap();
2623                         let channel_state = &mut *channel_state_lock;
2624                         match channel_state.by_id.entry(msg.channel_id.clone()) {
2625                                 hash_map::Entry::Occupied(mut chan_entry) => {
2626                                         if chan_entry.get().get_counterparty_node_id() != *counterparty_node_id {
2627                                                 return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!".to_owned(), msg.channel_id));
2628                                         }
2629                                         let (closing_signed, tx) = try_chan_entry!(self, chan_entry.get_mut().closing_signed(&self.fee_estimator, &msg), channel_state, chan_entry);
2630                                         if let Some(msg) = closing_signed {
2631                                                 channel_state.pending_msg_events.push(events::MessageSendEvent::SendClosingSigned {
2632                                                         node_id: counterparty_node_id.clone(),
2633                                                         msg,
2634                                                 });
2635                                         }
2636                                         if tx.is_some() {
2637                                                 // We're done with this channel, we've got a signed closing transaction and
2638                                                 // will send the closing_signed back to the remote peer upon return. This
2639                                                 // also implies there are no pending HTLCs left on the channel, so we can
2640                                                 // fully delete it from tracking (the channel monitor is still around to
2641                                                 // watch for old state broadcasts)!
2642                                                 if let Some(short_id) = chan_entry.get().get_short_channel_id() {
2643                                                         channel_state.short_to_id.remove(&short_id);
2644                                                 }
2645                                                 (tx, Some(chan_entry.remove_entry().1))
2646                                         } else { (tx, None) }
2647                                 },
2648                                 hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id))
2649                         }
2650                 };
2651                 if let Some(broadcast_tx) = tx {
2652                         log_trace!(self.logger, "Broadcast onchain {}", log_tx!(broadcast_tx));
2653                         self.tx_broadcaster.broadcast_transaction(&broadcast_tx);
2654                 }
2655                 if let Some(chan) = chan_option {
2656                         if let Ok(update) = self.get_channel_update(&chan) {
2657                                 let mut channel_state = self.channel_state.lock().unwrap();
2658                                 channel_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
2659                                         msg: update
2660                                 });
2661                         }
2662                 }
2663                 Ok(())
2664         }
2665
2666         fn internal_update_add_htlc(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateAddHTLC) -> Result<(), MsgHandleErrInternal> {
2667                 //TODO: BOLT 4 points out a specific attack where a peer may re-send an onion packet and
2668                 //determine the state of the payment based on our response/if we forward anything/the time
2669                 //we take to respond. We should take care to avoid allowing such an attack.
2670                 //
2671                 //TODO: There exists a further attack where a node may garble the onion data, forward it to
2672                 //us repeatedly garbled in different ways, and compare our error messages, which are
2673                 //encrypted with the same key. It's not immediately obvious how to usefully exploit that,
2674                 //but we should prevent it anyway.
2675
2676                 let (pending_forward_info, mut channel_state_lock) = self.decode_update_add_htlc_onion(msg);
2677                 let channel_state = &mut *channel_state_lock;
2678
2679                 match channel_state.by_id.entry(msg.channel_id) {
2680                         hash_map::Entry::Occupied(mut chan) => {
2681                                 if chan.get().get_counterparty_node_id() != *counterparty_node_id {
2682                                         return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!".to_owned(), msg.channel_id));
2683                                 }
2684
2685                                 let create_pending_htlc_status = |chan: &Channel<Signer>, pending_forward_info: PendingHTLCStatus, error_code: u16| {
2686                                         // Ensure error_code has the UPDATE flag set, since by default we send a
2687                                         // channel update along as part of failing the HTLC.
2688                                         assert!((error_code & 0x1000) != 0);
2689                                         // If the update_add is completely bogus, the call will Err and we will close,
2690                                         // but if we've sent a shutdown and they haven't acknowledged it yet, we just
2691                                         // want to reject the new HTLC and fail it backwards instead of forwarding.
2692                                         match pending_forward_info {
2693                                                 PendingHTLCStatus::Forward(PendingHTLCInfo { ref incoming_shared_secret, .. }) => {
2694                                                         let reason = if let Ok(upd) = self.get_channel_update(chan) {
2695                                                                 onion_utils::build_first_hop_failure_packet(incoming_shared_secret, error_code, &{
2696                                                                         let mut res = Vec::with_capacity(8 + 128);
2697                                                                         // TODO: underspecified, follow https://github.com/lightningnetwork/lightning-rfc/issues/791
2698                                                                         res.extend_from_slice(&byte_utils::be16_to_array(0));
2699                                                                         res.extend_from_slice(&upd.encode_with_len()[..]);
2700                                                                         res
2701                                                                 }[..])
2702                                                         } else {
2703                                                                 // The only case where we'd be unable to
2704                                                                 // successfully get a channel update is if the
2705                                                                 // channel isn't in the fully-funded state yet,
2706                                                                 // implying our counterparty is trying to route
2707                                                                 // payments over the channel back to themselves
2708                                                                 // (cause no one else should know the short_id
2709                                                                 // is a lightning channel yet). We should have
2710                                                                 // no problem just calling this
2711                                                                 // unknown_next_peer (0x4000|10).
2712                                                                 onion_utils::build_first_hop_failure_packet(incoming_shared_secret, 0x4000|10, &[])
2713                                                         };
2714                                                         let msg = msgs::UpdateFailHTLC {
2715                                                                 channel_id: msg.channel_id,
2716                                                                 htlc_id: msg.htlc_id,
2717                                                                 reason
2718                                                         };
2719                                                         PendingHTLCStatus::Fail(HTLCFailureMsg::Relay(msg))
2720                                                 },
2721                                                 _ => pending_forward_info
2722                                         }
2723                                 };
2724                                 try_chan_entry!(self, chan.get_mut().update_add_htlc(&msg, pending_forward_info, create_pending_htlc_status, &self.logger), channel_state, chan);
2725                         },
2726                         hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id))
2727                 }
2728                 Ok(())
2729         }
2730
2731         fn internal_update_fulfill_htlc(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateFulfillHTLC) -> Result<(), MsgHandleErrInternal> {
2732                 let mut channel_lock = self.channel_state.lock().unwrap();
2733                 let htlc_source = {
2734                         let channel_state = &mut *channel_lock;
2735                         match channel_state.by_id.entry(msg.channel_id) {
2736                                 hash_map::Entry::Occupied(mut chan) => {
2737                                         if chan.get().get_counterparty_node_id() != *counterparty_node_id {
2738                                                 return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!".to_owned(), msg.channel_id));
2739                                         }
2740                                         try_chan_entry!(self, chan.get_mut().update_fulfill_htlc(&msg), channel_state, chan)
2741                                 },
2742                                 hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id))
2743                         }
2744                 };
2745                 self.claim_funds_internal(channel_lock, htlc_source, msg.payment_preimage.clone());
2746                 Ok(())
2747         }
2748
2749         fn internal_update_fail_htlc(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateFailHTLC) -> Result<(), MsgHandleErrInternal> {
2750                 let mut channel_lock = self.channel_state.lock().unwrap();
2751                 let channel_state = &mut *channel_lock;
2752                 match channel_state.by_id.entry(msg.channel_id) {
2753                         hash_map::Entry::Occupied(mut chan) => {
2754                                 if chan.get().get_counterparty_node_id() != *counterparty_node_id {
2755                                         return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!".to_owned(), msg.channel_id));
2756                                 }
2757                                 try_chan_entry!(self, chan.get_mut().update_fail_htlc(&msg, HTLCFailReason::LightningError { err: msg.reason.clone() }), channel_state, chan);
2758                         },
2759                         hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id))
2760                 }
2761                 Ok(())
2762         }
2763
2764         fn internal_update_fail_malformed_htlc(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateFailMalformedHTLC) -> Result<(), MsgHandleErrInternal> {
2765                 let mut channel_lock = self.channel_state.lock().unwrap();
2766                 let channel_state = &mut *channel_lock;
2767                 match channel_state.by_id.entry(msg.channel_id) {
2768                         hash_map::Entry::Occupied(mut chan) => {
2769                                 if chan.get().get_counterparty_node_id() != *counterparty_node_id {
2770                                         return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!".to_owned(), msg.channel_id));
2771                                 }
2772                                 if (msg.failure_code & 0x8000) == 0 {
2773                                         let chan_err: ChannelError = ChannelError::Close("Got update_fail_malformed_htlc with BADONION not set".to_owned());
2774                                         try_chan_entry!(self, Err(chan_err), channel_state, chan);
2775                                 }
2776                                 try_chan_entry!(self, chan.get_mut().update_fail_malformed_htlc(&msg, HTLCFailReason::Reason { failure_code: msg.failure_code, data: Vec::new() }), channel_state, chan);
2777                                 Ok(())
2778                         },
2779                         hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id))
2780                 }
2781         }
2782
2783         fn internal_commitment_signed(&self, counterparty_node_id: &PublicKey, msg: &msgs::CommitmentSigned) -> Result<(), MsgHandleErrInternal> {
2784                 let mut channel_state_lock = self.channel_state.lock().unwrap();
2785                 let channel_state = &mut *channel_state_lock;
2786                 match channel_state.by_id.entry(msg.channel_id) {
2787                         hash_map::Entry::Occupied(mut chan) => {
2788                                 if chan.get().get_counterparty_node_id() != *counterparty_node_id {
2789                                         return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!".to_owned(), msg.channel_id));
2790                                 }
2791                                 let (revoke_and_ack, commitment_signed, closing_signed, monitor_update) =
2792                                         match chan.get_mut().commitment_signed(&msg, &self.fee_estimator, &self.logger) {
2793                                                 Err((None, e)) => try_chan_entry!(self, Err(e), channel_state, chan),
2794                                                 Err((Some(update), e)) => {
2795                                                         assert!(chan.get().is_awaiting_monitor_update());
2796                                                         let _ = self.chain_monitor.update_channel(chan.get().get_funding_txo().unwrap(), update);
2797                                                         try_chan_entry!(self, Err(e), channel_state, chan);
2798                                                         unreachable!();
2799                                                 },
2800                                                 Ok(res) => res
2801                                         };
2802                                 if let Err(e) = self.chain_monitor.update_channel(chan.get().get_funding_txo().unwrap(), monitor_update) {
2803                                         return_monitor_err!(self, e, channel_state, chan, RAACommitmentOrder::RevokeAndACKFirst, true, commitment_signed.is_some());
2804                                         //TODO: Rebroadcast closing_signed if present on monitor update restoration
2805                                 }
2806                                 channel_state.pending_msg_events.push(events::MessageSendEvent::SendRevokeAndACK {
2807                                         node_id: counterparty_node_id.clone(),
2808                                         msg: revoke_and_ack,
2809                                 });
2810                                 if let Some(msg) = commitment_signed {
2811                                         channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
2812                                                 node_id: counterparty_node_id.clone(),
2813                                                 updates: msgs::CommitmentUpdate {
2814                                                         update_add_htlcs: Vec::new(),
2815                                                         update_fulfill_htlcs: Vec::new(),
2816                                                         update_fail_htlcs: Vec::new(),
2817                                                         update_fail_malformed_htlcs: Vec::new(),
2818                                                         update_fee: None,
2819                                                         commitment_signed: msg,
2820                                                 },
2821                                         });
2822                                 }
2823                                 if let Some(msg) = closing_signed {
2824                                         channel_state.pending_msg_events.push(events::MessageSendEvent::SendClosingSigned {
2825                                                 node_id: counterparty_node_id.clone(),
2826                                                 msg,
2827                                         });
2828                                 }
2829                                 Ok(())
2830                         },
2831                         hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id))
2832                 }
2833         }
2834
2835         #[inline]
2836         fn forward_htlcs(&self, per_source_pending_forwards: &mut [(u64, OutPoint, Vec<(PendingHTLCInfo, u64)>)]) {
2837                 for &mut (prev_short_channel_id, prev_funding_outpoint, ref mut pending_forwards) in per_source_pending_forwards {
2838                         let mut forward_event = None;
2839                         if !pending_forwards.is_empty() {
2840                                 let mut channel_state = self.channel_state.lock().unwrap();
2841                                 if channel_state.forward_htlcs.is_empty() {
2842                                         forward_event = Some(Duration::from_millis(MIN_HTLC_RELAY_HOLDING_CELL_MILLIS))
2843                                 }
2844                                 for (forward_info, prev_htlc_id) in pending_forwards.drain(..) {
2845                                         match channel_state.forward_htlcs.entry(match forward_info.routing {
2846                                                         PendingHTLCRouting::Forward { short_channel_id, .. } => short_channel_id,
2847                                                         PendingHTLCRouting::Receive { .. } => 0,
2848                                         }) {
2849                                                 hash_map::Entry::Occupied(mut entry) => {
2850                                                         entry.get_mut().push(HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_funding_outpoint,
2851                                                                                                         prev_htlc_id, forward_info });
2852                                                 },
2853                                                 hash_map::Entry::Vacant(entry) => {
2854                                                         entry.insert(vec!(HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_funding_outpoint,
2855                                                                                                      prev_htlc_id, forward_info }));
2856                                                 }
2857                                         }
2858                                 }
2859                         }
2860                         match forward_event {
2861                                 Some(time) => {
2862                                         let mut pending_events = self.pending_events.lock().unwrap();
2863                                         pending_events.push(events::Event::PendingHTLCsForwardable {
2864                                                 time_forwardable: time
2865                                         });
2866                                 }
2867                                 None => {},
2868                         }
2869                 }
2870         }
2871
2872         fn internal_revoke_and_ack(&self, counterparty_node_id: &PublicKey, msg: &msgs::RevokeAndACK) -> Result<(), MsgHandleErrInternal> {
2873                 let mut htlcs_to_fail = Vec::new();
2874                 let res = loop {
2875                         let mut channel_state_lock = self.channel_state.lock().unwrap();
2876                         let channel_state = &mut *channel_state_lock;
2877                         match channel_state.by_id.entry(msg.channel_id) {
2878                                 hash_map::Entry::Occupied(mut chan) => {
2879                                         if chan.get().get_counterparty_node_id() != *counterparty_node_id {
2880                                                 break Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!".to_owned(), msg.channel_id));
2881                                         }
2882                                         let was_frozen_for_monitor = chan.get().is_awaiting_monitor_update();
2883                                         let (commitment_update, pending_forwards, pending_failures, closing_signed, monitor_update, htlcs_to_fail_in) =
2884                                                 break_chan_entry!(self, chan.get_mut().revoke_and_ack(&msg, &self.fee_estimator, &self.logger), channel_state, chan);
2885                                         htlcs_to_fail = htlcs_to_fail_in;
2886                                         if let Err(e) = self.chain_monitor.update_channel(chan.get().get_funding_txo().unwrap(), monitor_update) {
2887                                                 if was_frozen_for_monitor {
2888                                                         assert!(commitment_update.is_none() && closing_signed.is_none() && pending_forwards.is_empty() && pending_failures.is_empty());
2889                                                         break Err(MsgHandleErrInternal::ignore_no_close("Previous monitor update failure prevented responses to RAA".to_owned()));
2890                                                 } else {
2891                                                         if let Err(e) = handle_monitor_err!(self, e, channel_state, chan, RAACommitmentOrder::CommitmentFirst, false, commitment_update.is_some(), pending_forwards, pending_failures) {
2892                                                                 break Err(e);
2893                                                         } else { unreachable!(); }
2894                                                 }
2895                                         }
2896                                         if let Some(updates) = commitment_update {
2897                                                 channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
2898                                                         node_id: counterparty_node_id.clone(),
2899                                                         updates,
2900                                                 });
2901                                         }
2902                                         if let Some(msg) = closing_signed {
2903                                                 channel_state.pending_msg_events.push(events::MessageSendEvent::SendClosingSigned {
2904                                                         node_id: counterparty_node_id.clone(),
2905                                                         msg,
2906                                                 });
2907                                         }
2908                                         break Ok((pending_forwards, pending_failures, chan.get().get_short_channel_id().expect("RAA should only work on a short-id-available channel"), chan.get().get_funding_txo().unwrap()))
2909                                 },
2910                                 hash_map::Entry::Vacant(_) => break Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id))
2911                         }
2912                 };
2913                 self.fail_holding_cell_htlcs(htlcs_to_fail, msg.channel_id);
2914                 match res {
2915                         Ok((pending_forwards, mut pending_failures, short_channel_id, channel_outpoint)) => {
2916                                 for failure in pending_failures.drain(..) {
2917                                         self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), failure.0, &failure.1, failure.2);
2918                                 }
2919                                 self.forward_htlcs(&mut [(short_channel_id, channel_outpoint, pending_forwards)]);
2920                                 Ok(())
2921                         },
2922                         Err(e) => Err(e)
2923                 }
2924         }
2925
2926         fn internal_update_fee(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateFee) -> Result<(), MsgHandleErrInternal> {
2927                 let mut channel_lock = self.channel_state.lock().unwrap();
2928                 let channel_state = &mut *channel_lock;
2929                 match channel_state.by_id.entry(msg.channel_id) {
2930                         hash_map::Entry::Occupied(mut chan) => {
2931                                 if chan.get().get_counterparty_node_id() != *counterparty_node_id {
2932                                         return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!".to_owned(), msg.channel_id));
2933                                 }
2934                                 try_chan_entry!(self, chan.get_mut().update_fee(&self.fee_estimator, &msg), channel_state, chan);
2935                         },
2936                         hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id))
2937                 }
2938                 Ok(())
2939         }
2940
2941         fn internal_announcement_signatures(&self, counterparty_node_id: &PublicKey, msg: &msgs::AnnouncementSignatures) -> Result<(), MsgHandleErrInternal> {
2942                 let mut channel_state_lock = self.channel_state.lock().unwrap();
2943                 let channel_state = &mut *channel_state_lock;
2944
2945                 match channel_state.by_id.entry(msg.channel_id) {
2946                         hash_map::Entry::Occupied(mut chan) => {
2947                                 if chan.get().get_counterparty_node_id() != *counterparty_node_id {
2948                                         return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!".to_owned(), msg.channel_id));
2949                                 }
2950                                 if !chan.get().is_usable() {
2951                                         return Err(MsgHandleErrInternal::from_no_close(LightningError{err: "Got an announcement_signatures before we were ready for it".to_owned(), action: msgs::ErrorAction::IgnoreError}));
2952                                 }
2953
2954                                 let our_node_id = self.get_our_node_id();
2955                                 let (announcement, our_bitcoin_sig) =
2956                                         try_chan_entry!(self, chan.get_mut().get_channel_announcement(our_node_id.clone(), self.genesis_hash.clone()), channel_state, chan);
2957
2958                                 let were_node_one = announcement.node_id_1 == our_node_id;
2959                                 let msghash = hash_to_message!(&Sha256dHash::hash(&announcement.encode()[..])[..]);
2960                                 {
2961                                         let their_node_key = if were_node_one { &announcement.node_id_2 } else { &announcement.node_id_1 };
2962                                         let their_bitcoin_key = if were_node_one { &announcement.bitcoin_key_2 } else { &announcement.bitcoin_key_1 };
2963                                         match (self.secp_ctx.verify(&msghash, &msg.node_signature, their_node_key),
2964                                                    self.secp_ctx.verify(&msghash, &msg.bitcoin_signature, their_bitcoin_key)) {
2965                                                 (Err(e), _) => {
2966                                                         let chan_err: ChannelError = ChannelError::Close(format!("Bad announcement_signatures. Failed to verify node_signature: {:?}. Maybe using different node_secret for transport and routing msg? UnsignedChannelAnnouncement used for verification is {:?}. their_node_key is {:?}", e, &announcement, their_node_key));
2967                                                         try_chan_entry!(self, Err(chan_err), channel_state, chan);
2968                                                 },
2969                                                 (_, Err(e)) => {
2970                                                         let chan_err: ChannelError = ChannelError::Close(format!("Bad announcement_signatures. Failed to verify bitcoin_signature: {:?}. UnsignedChannelAnnouncement used for verification is {:?}. their_bitcoin_key is ({:?})", e, &announcement, their_bitcoin_key));
2971                                                         try_chan_entry!(self, Err(chan_err), channel_state, chan);
2972                                                 },
2973                                                 _ => {}
2974                                         }
2975                                 }
2976
2977                                 let our_node_sig = self.secp_ctx.sign(&msghash, &self.our_network_key);
2978
2979                                 channel_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelAnnouncement {
2980                                         msg: msgs::ChannelAnnouncement {
2981                                                 node_signature_1: if were_node_one { our_node_sig } else { msg.node_signature },
2982                                                 node_signature_2: if were_node_one { msg.node_signature } else { our_node_sig },
2983                                                 bitcoin_signature_1: if were_node_one { our_bitcoin_sig } else { msg.bitcoin_signature },
2984                                                 bitcoin_signature_2: if were_node_one { msg.bitcoin_signature } else { our_bitcoin_sig },
2985                                                 contents: announcement,
2986                                         },
2987                                         update_msg: self.get_channel_update(chan.get()).unwrap(), // can only fail if we're not in a ready state
2988                                 });
2989                         },
2990                         hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id))
2991                 }
2992                 Ok(())
2993         }
2994
2995         fn internal_channel_reestablish(&self, counterparty_node_id: &PublicKey, msg: &msgs::ChannelReestablish) -> Result<(), MsgHandleErrInternal> {
2996                 let mut channel_state_lock = self.channel_state.lock().unwrap();
2997                 let channel_state = &mut *channel_state_lock;
2998
2999                 match channel_state.by_id.entry(msg.channel_id) {
3000                         hash_map::Entry::Occupied(mut chan) => {
3001                                 if chan.get().get_counterparty_node_id() != *counterparty_node_id {
3002                                         return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!".to_owned(), msg.channel_id));
3003                                 }
3004                                 // Currently, we expect all holding cell update_adds to be dropped on peer
3005                                 // disconnect, so Channel's reestablish will never hand us any holding cell
3006                                 // freed HTLCs to fail backwards. If in the future we no longer drop pending
3007                                 // add-HTLCs on disconnect, we may be handed HTLCs to fail backwards here.
3008                                 let (funding_locked, revoke_and_ack, commitment_update, monitor_update_opt, mut order, shutdown) =
3009                                         try_chan_entry!(self, chan.get_mut().channel_reestablish(msg, &self.logger), channel_state, chan);
3010                                 if let Some(monitor_update) = monitor_update_opt {
3011                                         if let Err(e) = self.chain_monitor.update_channel(chan.get().get_funding_txo().unwrap(), monitor_update) {
3012                                                 // channel_reestablish doesn't guarantee the order it returns is sensical
3013                                                 // for the messages it returns, but if we're setting what messages to
3014                                                 // re-transmit on monitor update success, we need to make sure it is sane.
3015                                                 if revoke_and_ack.is_none() {
3016                                                         order = RAACommitmentOrder::CommitmentFirst;
3017                                                 }
3018                                                 if commitment_update.is_none() {
3019                                                         order = RAACommitmentOrder::RevokeAndACKFirst;
3020                                                 }
3021                                                 return_monitor_err!(self, e, channel_state, chan, order, revoke_and_ack.is_some(), commitment_update.is_some());
3022                                                 //TODO: Resend the funding_locked if needed once we get the monitor running again
3023                                         }
3024                                 }
3025                                 if let Some(msg) = funding_locked {
3026                                         channel_state.pending_msg_events.push(events::MessageSendEvent::SendFundingLocked {
3027                                                 node_id: counterparty_node_id.clone(),
3028                                                 msg
3029                                         });
3030                                 }
3031                                 macro_rules! send_raa { () => {
3032                                         if let Some(msg) = revoke_and_ack {
3033                                                 channel_state.pending_msg_events.push(events::MessageSendEvent::SendRevokeAndACK {
3034                                                         node_id: counterparty_node_id.clone(),
3035                                                         msg
3036                                                 });
3037                                         }
3038                                 } }
3039                                 macro_rules! send_cu { () => {
3040                                         if let Some(updates) = commitment_update {
3041                                                 channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
3042                                                         node_id: counterparty_node_id.clone(),
3043                                                         updates
3044                                                 });
3045                                         }
3046                                 } }
3047                                 match order {
3048                                         RAACommitmentOrder::RevokeAndACKFirst => {
3049                                                 send_raa!();
3050                                                 send_cu!();
3051                                         },
3052                                         RAACommitmentOrder::CommitmentFirst => {
3053                                                 send_cu!();
3054                                                 send_raa!();
3055                                         },
3056                                 }
3057                                 if let Some(msg) = shutdown {
3058                                         channel_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
3059                                                 node_id: counterparty_node_id.clone(),
3060                                                 msg,
3061                                         });
3062                                 }
3063                                 Ok(())
3064                         },
3065                         hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id))
3066                 }
3067         }
3068
3069         /// Begin Update fee process. Allowed only on an outbound channel.
3070         /// If successful, will generate a UpdateHTLCs event, so you should probably poll
3071         /// PeerManager::process_events afterwards.
3072         /// Note: This API is likely to change!
3073         /// (C-not exported) Cause its doc(hidden) anyway
3074         #[doc(hidden)]
3075         pub fn update_fee(&self, channel_id: [u8;32], feerate_per_kw: u32) -> Result<(), APIError> {
3076                 let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
3077                 let counterparty_node_id;
3078                 let err: Result<(), _> = loop {
3079                         let mut channel_state_lock = self.channel_state.lock().unwrap();
3080                         let channel_state = &mut *channel_state_lock;
3081
3082                         match channel_state.by_id.entry(channel_id) {
3083                                 hash_map::Entry::Vacant(_) => return Err(APIError::APIMisuseError{err: format!("Failed to find corresponding channel for id {}", channel_id.to_hex())}),
3084                                 hash_map::Entry::Occupied(mut chan) => {
3085                                         if !chan.get().is_outbound() {
3086                                                 return Err(APIError::APIMisuseError{err: "update_fee cannot be sent for an inbound channel".to_owned()});
3087                                         }
3088                                         if chan.get().is_awaiting_monitor_update() {
3089                                                 return Err(APIError::MonitorUpdateFailed);
3090                                         }
3091                                         if !chan.get().is_live() {
3092                                                 return Err(APIError::ChannelUnavailable{err: "Channel is either not yet fully established or peer is currently disconnected".to_owned()});
3093                                         }
3094                                         counterparty_node_id = chan.get().get_counterparty_node_id();
3095                                         if let Some((update_fee, commitment_signed, monitor_update)) =
3096                                                         break_chan_entry!(self, chan.get_mut().send_update_fee_and_commit(feerate_per_kw, &self.logger), channel_state, chan)
3097                                         {
3098                                                 if let Err(_e) = self.chain_monitor.update_channel(chan.get().get_funding_txo().unwrap(), monitor_update) {
3099                                                         unimplemented!();
3100                                                 }
3101                                                 channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
3102                                                         node_id: chan.get().get_counterparty_node_id(),
3103                                                         updates: msgs::CommitmentUpdate {
3104                                                                 update_add_htlcs: Vec::new(),
3105                                                                 update_fulfill_htlcs: Vec::new(),
3106                                                                 update_fail_htlcs: Vec::new(),
3107                                                                 update_fail_malformed_htlcs: Vec::new(),
3108                                                                 update_fee: Some(update_fee),
3109                                                                 commitment_signed,
3110                                                         },
3111                                                 });
3112                                         }
3113                                 },
3114                         }
3115                         return Ok(())
3116                 };
3117
3118                 match handle_error!(self, err, counterparty_node_id) {
3119                         Ok(_) => unreachable!(),
3120                         Err(e) => { Err(APIError::APIMisuseError { err: e.err })}
3121                 }
3122         }
3123
3124         /// Process pending events from the `chain::Watch`.
3125         fn process_pending_monitor_events(&self) {
3126                 let mut failed_channels = Vec::new();
3127                 {
3128                         for monitor_event in self.chain_monitor.release_pending_monitor_events() {
3129                                 match monitor_event {
3130                                         MonitorEvent::HTLCEvent(htlc_update) => {
3131                                                 if let Some(preimage) = htlc_update.payment_preimage {
3132                                                         log_trace!(self.logger, "Claiming HTLC with preimage {} from our monitor", log_bytes!(preimage.0));
3133                                                         self.claim_funds_internal(self.channel_state.lock().unwrap(), htlc_update.source, preimage);
3134                                                 } else {
3135                                                         log_trace!(self.logger, "Failing HTLC with hash {} from our monitor", log_bytes!(htlc_update.payment_hash.0));
3136                                                         self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), htlc_update.source, &htlc_update.payment_hash, HTLCFailReason::Reason { failure_code: 0x4000 | 8, data: Vec::new() });
3137                                                 }
3138                                         },
3139                                         MonitorEvent::CommitmentTxBroadcasted(funding_outpoint) => {
3140                                                 let mut channel_lock = self.channel_state.lock().unwrap();
3141                                                 let channel_state = &mut *channel_lock;
3142                                                 let by_id = &mut channel_state.by_id;
3143                                                 let short_to_id = &mut channel_state.short_to_id;
3144                                                 let pending_msg_events = &mut channel_state.pending_msg_events;
3145                                                 if let Some(mut chan) = by_id.remove(&funding_outpoint.to_channel_id()) {
3146                                                         if let Some(short_id) = chan.get_short_channel_id() {
3147                                                                 short_to_id.remove(&short_id);
3148                                                         }
3149                                                         failed_channels.push(chan.force_shutdown(false));
3150                                                         if let Ok(update) = self.get_channel_update(&chan) {
3151                                                                 pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
3152                                                                         msg: update
3153                                                                 });
3154                                                         }
3155                                                 }
3156                                         },
3157                                 }
3158                         }
3159                 }
3160
3161                 for failure in failed_channels.drain(..) {
3162                         self.finish_force_close_channel(failure);
3163                 }
3164         }
3165
3166         /// Handle a list of channel failures during a block_connected or block_disconnected call,
3167         /// pushing the channel monitor update (if any) to the background events queue and removing the
3168         /// Channel object.
3169         fn handle_init_event_channel_failures(&self, mut failed_channels: Vec<ShutdownResult>) {
3170                 for mut failure in failed_channels.drain(..) {
3171                         // Either a commitment transactions has been confirmed on-chain or
3172                         // Channel::block_disconnected detected that the funding transaction has been
3173                         // reorganized out of the main chain.
3174                         // We cannot broadcast our latest local state via monitor update (as
3175                         // Channel::force_shutdown tries to make us do) as we may still be in initialization,
3176                         // so we track the update internally and handle it when the user next calls
3177                         // timer_chan_freshness_every_min, guaranteeing we're running normally.
3178                         if let Some((funding_txo, update)) = failure.0.take() {
3179                                 assert_eq!(update.updates.len(), 1);
3180                                 if let ChannelMonitorUpdateStep::ChannelForceClosed { should_broadcast } = update.updates[0] {
3181                                         assert!(should_broadcast);
3182                                 } else { unreachable!(); }
3183                                 self.pending_background_events.lock().unwrap().push(BackgroundEvent::ClosingMonitorUpdate((funding_txo, update)));
3184                         }
3185                         self.finish_force_close_channel(failure);
3186                 }
3187         }
3188 }
3189
3190 impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> MessageSendEventsProvider for ChannelManager<Signer, M, T, K, F, L>
3191         where M::Target: chain::Watch<Signer>,
3192         T::Target: BroadcasterInterface,
3193         K::Target: KeysInterface<Signer = Signer>,
3194         F::Target: FeeEstimator,
3195                                 L::Target: Logger,
3196 {
3197         fn get_and_clear_pending_msg_events(&self) -> Vec<MessageSendEvent> {
3198                 //TODO: This behavior should be documented. It's non-intuitive that we query
3199                 // ChannelMonitors when clearing other events.
3200                 self.process_pending_monitor_events();
3201
3202                 let mut ret = Vec::new();
3203                 let mut channel_state = self.channel_state.lock().unwrap();
3204                 mem::swap(&mut ret, &mut channel_state.pending_msg_events);
3205                 ret
3206         }
3207 }
3208
3209 impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> EventsProvider for ChannelManager<Signer, M, T, K, F, L>
3210         where M::Target: chain::Watch<Signer>,
3211         T::Target: BroadcasterInterface,
3212         K::Target: KeysInterface<Signer = Signer>,
3213         F::Target: FeeEstimator,
3214                                 L::Target: Logger,
3215 {
3216         fn get_and_clear_pending_events(&self) -> Vec<Event> {
3217                 //TODO: This behavior should be documented. It's non-intuitive that we query
3218                 // ChannelMonitors when clearing other events.
3219                 self.process_pending_monitor_events();
3220
3221                 let mut ret = Vec::new();
3222                 let mut pending_events = self.pending_events.lock().unwrap();
3223                 mem::swap(&mut ret, &mut *pending_events);
3224                 ret
3225         }
3226 }
3227
3228 impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> chain::Listen for ChannelManager<Signer, M, T, K, F, L>
3229 where
3230         M::Target: chain::Watch<Signer>,
3231         T::Target: BroadcasterInterface,
3232         K::Target: KeysInterface<Signer = Signer>,
3233         F::Target: FeeEstimator,
3234         L::Target: Logger,
3235 {
3236         fn block_connected(&self, block: &Block, height: u32) {
3237                 let txdata: Vec<_> = block.txdata.iter().enumerate().collect();
3238                 ChannelManager::block_connected(self, &block.header, &txdata, height);
3239         }
3240
3241         fn block_disconnected(&self, header: &BlockHeader, _height: u32) {
3242                 ChannelManager::block_disconnected(self, header);
3243         }
3244 }
3245
3246 impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<Signer, M, T, K, F, L>
3247         where M::Target: chain::Watch<Signer>,
3248         T::Target: BroadcasterInterface,
3249         K::Target: KeysInterface<Signer = Signer>,
3250         F::Target: FeeEstimator,
3251         L::Target: Logger,
3252 {
3253         /// Updates channel state based on transactions seen in a connected block.
3254         pub fn block_connected(&self, header: &BlockHeader, txdata: &TransactionData, height: u32) {
3255                 // Note that we MUST NOT end up calling methods on self.chain_monitor here - we're called
3256                 // during initialization prior to the chain_monitor being fully configured in some cases.
3257                 // See the docs for `ChannelManagerReadArgs` for more.
3258                 let block_hash = header.block_hash();
3259                 log_trace!(self.logger, "Block {} at height {} connected", block_hash, height);
3260
3261                 let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
3262
3263                 self.latest_block_height.store(height as usize, Ordering::Release);
3264                 *self.last_block_hash.write().unwrap() = block_hash;
3265
3266                 let mut failed_channels = Vec::new();
3267                 let mut timed_out_htlcs = Vec::new();
3268                 {
3269                         let mut channel_lock = self.channel_state.lock().unwrap();
3270                         let channel_state = &mut *channel_lock;
3271                         let short_to_id = &mut channel_state.short_to_id;
3272                         let pending_msg_events = &mut channel_state.pending_msg_events;
3273                         channel_state.by_id.retain(|_, channel| {
3274                                 let res = channel.block_connected(header, txdata, height);
3275                                 if let Ok((chan_res, mut timed_out_pending_htlcs)) = res {
3276                                         for (source, payment_hash) in timed_out_pending_htlcs.drain(..) {
3277                                                 let chan_update = self.get_channel_update(&channel).map(|u| u.encode_with_len()).unwrap(); // Cannot add/recv HTLCs before we have a short_id so unwrap is safe
3278                                                 timed_out_htlcs.push((source, payment_hash,  HTLCFailReason::Reason {
3279                                                         failure_code: 0x1000 | 14, // expiry_too_soon, or at least it is now
3280                                                         data: chan_update,
3281                                                 }));
3282                                         }
3283                                         if let Some(funding_locked) = chan_res {
3284                                                 pending_msg_events.push(events::MessageSendEvent::SendFundingLocked {
3285                                                         node_id: channel.get_counterparty_node_id(),
3286                                                         msg: funding_locked,
3287                                                 });
3288                                                 if let Some(announcement_sigs) = self.get_announcement_sigs(channel) {
3289                                                         log_trace!(self.logger, "Sending funding_locked and announcement_signatures for {}", log_bytes!(channel.channel_id()));
3290                                                         pending_msg_events.push(events::MessageSendEvent::SendAnnouncementSignatures {
3291                                                                 node_id: channel.get_counterparty_node_id(),
3292                                                                 msg: announcement_sigs,
3293                                                         });
3294                                                 } else {
3295                                                         log_trace!(self.logger, "Sending funding_locked WITHOUT announcement_signatures for {}", log_bytes!(channel.channel_id()));
3296                                                 }
3297                                                 short_to_id.insert(channel.get_short_channel_id().unwrap(), channel.channel_id());
3298                                         }
3299                                 } else if let Err(e) = res {
3300                                         pending_msg_events.push(events::MessageSendEvent::HandleError {
3301                                                 node_id: channel.get_counterparty_node_id(),
3302                                                 action: msgs::ErrorAction::SendErrorMessage { msg: e },
3303                                         });
3304                                         return false;
3305                                 }
3306                                 if let Some(funding_txo) = channel.get_funding_txo() {
3307                                         for &(_, tx) in txdata.iter() {
3308                                                 for inp in tx.input.iter() {
3309                                                         if inp.previous_output == funding_txo.into_bitcoin_outpoint() {
3310                                                                 log_trace!(self.logger, "Detected channel-closing tx {} spending {}:{}, closing channel {}", tx.txid(), inp.previous_output.txid, inp.previous_output.vout, log_bytes!(channel.channel_id()));
3311                                                                 if let Some(short_id) = channel.get_short_channel_id() {
3312                                                                         short_to_id.remove(&short_id);
3313                                                                 }
3314                                                                 // It looks like our counterparty went on-chain. Close the channel.
3315                                                                 failed_channels.push(channel.force_shutdown(true));
3316                                                                 if let Ok(update) = self.get_channel_update(&channel) {
3317                                                                         pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
3318                                                                                 msg: update
3319                                                                         });
3320                                                                 }
3321                                                                 return false;
3322                                                         }
3323                                                 }
3324                                         }
3325                                 }
3326                                 true
3327                         });
3328
3329                         channel_state.claimable_htlcs.retain(|&(ref payment_hash, _), htlcs| {
3330                                 htlcs.retain(|htlc| {
3331                                         // If height is approaching the number of blocks we think it takes us to get
3332                                         // our commitment transaction confirmed before the HTLC expires, plus the
3333                                         // number of blocks we generally consider it to take to do a commitment update,
3334                                         // just give up on it and fail the HTLC.
3335                                         if height >= htlc.cltv_expiry - HTLC_FAIL_BACK_BUFFER {
3336                                                 let mut htlc_msat_height_data = byte_utils::be64_to_array(htlc.value).to_vec();
3337                                                 htlc_msat_height_data.extend_from_slice(&byte_utils::be32_to_array(height));
3338                                                 timed_out_htlcs.push((HTLCSource::PreviousHopData(htlc.prev_hop.clone()), payment_hash.clone(), HTLCFailReason::Reason {
3339                                                         failure_code: 0x4000 | 15,
3340                                                         data: htlc_msat_height_data
3341                                                 }));
3342                                                 false
3343                                         } else { true }
3344                                 });
3345                                 !htlcs.is_empty() // Only retain this entry if htlcs has at least one entry.
3346                         });
3347                 }
3348
3349                 self.handle_init_event_channel_failures(failed_channels);
3350
3351                 for (source, payment_hash, reason) in timed_out_htlcs.drain(..) {
3352                         self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), source, &payment_hash, reason);
3353                 }
3354
3355                 loop {
3356                         // Update last_node_announcement_serial to be the max of its current value and the
3357                         // block timestamp. This should keep us close to the current time without relying on
3358                         // having an explicit local time source.
3359                         // Just in case we end up in a race, we loop until we either successfully update
3360                         // last_node_announcement_serial or decide we don't need to.
3361                         let old_serial = self.last_node_announcement_serial.load(Ordering::Acquire);
3362                         if old_serial >= header.time as usize { break; }
3363                         if self.last_node_announcement_serial.compare_exchange(old_serial, header.time as usize, Ordering::AcqRel, Ordering::Relaxed).is_ok() {
3364                                 break;
3365                         }
3366                 }
3367         }
3368
3369         /// Updates channel state based on a disconnected block.
3370         ///
3371         /// If necessary, the channel may be force-closed without letting the counterparty participate
3372         /// in the shutdown.
3373         pub fn block_disconnected(&self, header: &BlockHeader) {
3374                 // Note that we MUST NOT end up calling methods on self.chain_monitor here - we're called
3375                 // during initialization prior to the chain_monitor being fully configured in some cases.
3376                 // See the docs for `ChannelManagerReadArgs` for more.
3377                 let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
3378
3379                 self.latest_block_height.fetch_sub(1, Ordering::AcqRel);
3380                 *self.last_block_hash.write().unwrap() = header.block_hash();
3381
3382                 let mut failed_channels = Vec::new();
3383                 {
3384                         let mut channel_lock = self.channel_state.lock().unwrap();
3385                         let channel_state = &mut *channel_lock;
3386                         let short_to_id = &mut channel_state.short_to_id;
3387                         let pending_msg_events = &mut channel_state.pending_msg_events;
3388                         channel_state.by_id.retain(|_,  v| {
3389                                 if v.block_disconnected(header) {
3390                                         if let Some(short_id) = v.get_short_channel_id() {
3391                                                 short_to_id.remove(&short_id);
3392                                         }
3393                                         failed_channels.push(v.force_shutdown(true));
3394                                         if let Ok(update) = self.get_channel_update(&v) {
3395                                                 pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
3396                                                         msg: update
3397                                                 });
3398                                         }
3399                                         false
3400                                 } else {
3401                                         true
3402                                 }
3403                         });
3404                 }
3405
3406                 self.handle_init_event_channel_failures(failed_channels);
3407         }
3408
3409         /// Blocks until ChannelManager needs to be persisted or a timeout is reached. It returns a bool
3410         /// indicating whether persistence is necessary. Only one listener on `wait_timeout` is
3411         /// guaranteed to be woken up.
3412         /// Note that the feature `allow_wallclock_use` must be enabled to use this function.
3413         #[cfg(any(test, feature = "allow_wallclock_use"))]
3414         pub fn wait_timeout(&self, max_wait: Duration) -> bool {
3415                 self.persistence_notifier.wait_timeout(max_wait)
3416         }
3417
3418         /// Blocks until ChannelManager needs to be persisted. Only one listener on `wait` is
3419         /// guaranteed to be woken up.
3420         pub fn wait(&self) {
3421                 self.persistence_notifier.wait()
3422         }
3423
3424         #[cfg(any(test, feature = "_test_utils"))]
3425         pub fn get_persistence_condvar_value(&self) -> bool {
3426                 let mutcond = &self.persistence_notifier.persistence_lock;
3427                 let &(ref mtx, _) = mutcond;
3428                 let guard = mtx.lock().unwrap();
3429                 *guard
3430         }
3431 }
3432
3433 impl<Signer: Sign, M: Deref + Sync + Send, T: Deref + Sync + Send, K: Deref + Sync + Send, F: Deref + Sync + Send, L: Deref + Sync + Send>
3434         ChannelMessageHandler for ChannelManager<Signer, M, T, K, F, L>
3435         where M::Target: chain::Watch<Signer>,
3436         T::Target: BroadcasterInterface,
3437         K::Target: KeysInterface<Signer = Signer>,
3438         F::Target: FeeEstimator,
3439         L::Target: Logger,
3440 {
3441         fn handle_open_channel(&self, counterparty_node_id: &PublicKey, their_features: InitFeatures, msg: &msgs::OpenChannel) {
3442                 let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
3443                 let _ = handle_error!(self, self.internal_open_channel(counterparty_node_id, their_features, msg), *counterparty_node_id);
3444         }
3445
3446         fn handle_accept_channel(&self, counterparty_node_id: &PublicKey, their_features: InitFeatures, msg: &msgs::AcceptChannel) {
3447                 let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
3448                 let _ = handle_error!(self, self.internal_accept_channel(counterparty_node_id, their_features, msg), *counterparty_node_id);
3449         }
3450
3451         fn handle_funding_created(&self, counterparty_node_id: &PublicKey, msg: &msgs::FundingCreated) {
3452                 let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
3453                 let _ = handle_error!(self, self.internal_funding_created(counterparty_node_id, msg), *counterparty_node_id);
3454         }
3455
3456         fn handle_funding_signed(&self, counterparty_node_id: &PublicKey, msg: &msgs::FundingSigned) {
3457                 let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
3458                 let _ = handle_error!(self, self.internal_funding_signed(counterparty_node_id, msg), *counterparty_node_id);
3459         }
3460
3461         fn handle_funding_locked(&self, counterparty_node_id: &PublicKey, msg: &msgs::FundingLocked) {
3462                 let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
3463                 let _ = handle_error!(self, self.internal_funding_locked(counterparty_node_id, msg), *counterparty_node_id);
3464         }
3465
3466         fn handle_shutdown(&self, counterparty_node_id: &PublicKey, their_features: &InitFeatures, msg: &msgs::Shutdown) {
3467                 let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
3468                 let _ = handle_error!(self, self.internal_shutdown(counterparty_node_id, their_features, msg), *counterparty_node_id);
3469         }
3470
3471         fn handle_closing_signed(&self, counterparty_node_id: &PublicKey, msg: &msgs::ClosingSigned) {
3472                 let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
3473                 let _ = handle_error!(self, self.internal_closing_signed(counterparty_node_id, msg), *counterparty_node_id);
3474         }
3475
3476         fn handle_update_add_htlc(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateAddHTLC) {
3477                 let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
3478                 let _ = handle_error!(self, self.internal_update_add_htlc(counterparty_node_id, msg), *counterparty_node_id);
3479         }
3480
3481         fn handle_update_fulfill_htlc(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateFulfillHTLC) {
3482                 let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
3483                 let _ = handle_error!(self, self.internal_update_fulfill_htlc(counterparty_node_id, msg), *counterparty_node_id);
3484         }
3485
3486         fn handle_update_fail_htlc(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateFailHTLC) {
3487                 let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
3488                 let _ = handle_error!(self, self.internal_update_fail_htlc(counterparty_node_id, msg), *counterparty_node_id);
3489         }
3490
3491         fn handle_update_fail_malformed_htlc(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateFailMalformedHTLC) {
3492                 let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
3493                 let _ = handle_error!(self, self.internal_update_fail_malformed_htlc(counterparty_node_id, msg), *counterparty_node_id);
3494         }
3495
3496         fn handle_commitment_signed(&self, counterparty_node_id: &PublicKey, msg: &msgs::CommitmentSigned) {
3497                 let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
3498                 let _ = handle_error!(self, self.internal_commitment_signed(counterparty_node_id, msg), *counterparty_node_id);
3499         }
3500
3501         fn handle_revoke_and_ack(&self, counterparty_node_id: &PublicKey, msg: &msgs::RevokeAndACK) {
3502                 let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
3503                 let _ = handle_error!(self, self.internal_revoke_and_ack(counterparty_node_id, msg), *counterparty_node_id);
3504         }
3505
3506         fn handle_update_fee(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateFee) {
3507                 let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
3508                 let _ = handle_error!(self, self.internal_update_fee(counterparty_node_id, msg), *counterparty_node_id);
3509         }
3510
3511         fn handle_announcement_signatures(&self, counterparty_node_id: &PublicKey, msg: &msgs::AnnouncementSignatures) {
3512                 let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
3513                 let _ = handle_error!(self, self.internal_announcement_signatures(counterparty_node_id, msg), *counterparty_node_id);
3514         }
3515
3516         fn handle_channel_reestablish(&self, counterparty_node_id: &PublicKey, msg: &msgs::ChannelReestablish) {
3517                 let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
3518                 let _ = handle_error!(self, self.internal_channel_reestablish(counterparty_node_id, msg), *counterparty_node_id);
3519         }
3520
3521         fn peer_disconnected(&self, counterparty_node_id: &PublicKey, no_connection_possible: bool) {
3522                 let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
3523                 let mut failed_channels = Vec::new();
3524                 let mut failed_payments = Vec::new();
3525                 let mut no_channels_remain = true;
3526                 {
3527                         let mut channel_state_lock = self.channel_state.lock().unwrap();
3528                         let channel_state = &mut *channel_state_lock;
3529                         let short_to_id = &mut channel_state.short_to_id;
3530                         let pending_msg_events = &mut channel_state.pending_msg_events;
3531                         if no_connection_possible {
3532                                 log_debug!(self.logger, "Failing all channels with {} due to no_connection_possible", log_pubkey!(counterparty_node_id));
3533                                 channel_state.by_id.retain(|_, chan| {
3534                                         if chan.get_counterparty_node_id() == *counterparty_node_id {
3535                                                 if let Some(short_id) = chan.get_short_channel_id() {
3536                                                         short_to_id.remove(&short_id);
3537                                                 }
3538                                                 failed_channels.push(chan.force_shutdown(true));
3539                                                 if let Ok(update) = self.get_channel_update(&chan) {
3540                                                         pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
3541                                                                 msg: update
3542                                                         });
3543                                                 }
3544                                                 false
3545                                         } else {
3546                                                 true
3547                                         }
3548                                 });
3549                         } else {
3550                                 log_debug!(self.logger, "Marking channels with {} disconnected and generating channel_updates", log_pubkey!(counterparty_node_id));
3551                                 channel_state.by_id.retain(|_, chan| {
3552                                         if chan.get_counterparty_node_id() == *counterparty_node_id {
3553                                                 // Note that currently on channel reestablish we assert that there are no
3554                                                 // holding cell add-HTLCs, so if in the future we stop removing uncommitted HTLCs
3555                                                 // on peer disconnect here, there will need to be corresponding changes in
3556                                                 // reestablish logic.
3557                                                 let failed_adds = chan.remove_uncommitted_htlcs_and_mark_paused(&self.logger);
3558                                                 chan.to_disabled_marked();
3559                                                 if !failed_adds.is_empty() {
3560                                                         let chan_update = self.get_channel_update(&chan).map(|u| u.encode_with_len()).unwrap(); // Cannot add/recv HTLCs before we have a short_id so unwrap is safe
3561                                                         failed_payments.push((chan_update, failed_adds));
3562                                                 }
3563                                                 if chan.is_shutdown() {
3564                                                         if let Some(short_id) = chan.get_short_channel_id() {
3565                                                                 short_to_id.remove(&short_id);
3566                                                         }
3567                                                         return false;
3568                                                 } else {
3569                                                         no_channels_remain = false;
3570                                                 }
3571                                         }
3572                                         true
3573                                 })
3574                         }
3575                         pending_msg_events.retain(|msg| {
3576                                 match msg {
3577                                         &events::MessageSendEvent::SendAcceptChannel { ref node_id, .. } => node_id != counterparty_node_id,
3578                                         &events::MessageSendEvent::SendOpenChannel { ref node_id, .. } => node_id != counterparty_node_id,
3579                                         &events::MessageSendEvent::SendFundingCreated { ref node_id, .. } => node_id != counterparty_node_id,
3580                                         &events::MessageSendEvent::SendFundingSigned { ref node_id, .. } => node_id != counterparty_node_id,
3581                                         &events::MessageSendEvent::SendFundingLocked { ref node_id, .. } => node_id != counterparty_node_id,
3582                                         &events::MessageSendEvent::SendAnnouncementSignatures { ref node_id, .. } => node_id != counterparty_node_id,
3583                                         &events::MessageSendEvent::UpdateHTLCs { ref node_id, .. } => node_id != counterparty_node_id,
3584                                         &events::MessageSendEvent::SendRevokeAndACK { ref node_id, .. } => node_id != counterparty_node_id,
3585                                         &events::MessageSendEvent::SendClosingSigned { ref node_id, .. } => node_id != counterparty_node_id,
3586                                         &events::MessageSendEvent::SendShutdown { ref node_id, .. } => node_id != counterparty_node_id,
3587                                         &events::MessageSendEvent::SendChannelReestablish { ref node_id, .. } => node_id != counterparty_node_id,
3588                                         &events::MessageSendEvent::BroadcastChannelAnnouncement { .. } => true,
3589                                         &events::MessageSendEvent::BroadcastNodeAnnouncement { .. } => true,
3590                                         &events::MessageSendEvent::BroadcastChannelUpdate { .. } => true,
3591                                         &events::MessageSendEvent::HandleError { ref node_id, .. } => node_id != counterparty_node_id,
3592                                         &events::MessageSendEvent::PaymentFailureNetworkUpdate { .. } => true,
3593                                         &events::MessageSendEvent::SendChannelRangeQuery { .. } => false,
3594                                         &events::MessageSendEvent::SendShortIdsQuery { .. } => false,
3595                                 }
3596                         });
3597                 }
3598                 if no_channels_remain {
3599                         self.per_peer_state.write().unwrap().remove(counterparty_node_id);
3600                 }
3601
3602                 for failure in failed_channels.drain(..) {
3603                         self.finish_force_close_channel(failure);
3604                 }
3605                 for (chan_update, mut htlc_sources) in failed_payments {
3606                         for (htlc_source, payment_hash) in htlc_sources.drain(..) {
3607                                 self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), htlc_source, &payment_hash, HTLCFailReason::Reason { failure_code: 0x1000 | 7, data: chan_update.clone() });
3608                         }
3609                 }
3610         }
3611
3612         fn peer_connected(&self, counterparty_node_id: &PublicKey, init_msg: &msgs::Init) {
3613                 log_debug!(self.logger, "Generating channel_reestablish events for {}", log_pubkey!(counterparty_node_id));
3614
3615                 let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
3616
3617                 {
3618                         let mut peer_state_lock = self.per_peer_state.write().unwrap();
3619                         match peer_state_lock.entry(counterparty_node_id.clone()) {
3620                                 hash_map::Entry::Vacant(e) => {
3621                                         e.insert(Mutex::new(PeerState {
3622                                                 latest_features: init_msg.features.clone(),
3623                                         }));
3624                                 },
3625                                 hash_map::Entry::Occupied(e) => {
3626                                         e.get().lock().unwrap().latest_features = init_msg.features.clone();
3627                                 },
3628                         }
3629                 }
3630
3631                 let mut channel_state_lock = self.channel_state.lock().unwrap();
3632                 let channel_state = &mut *channel_state_lock;
3633                 let pending_msg_events = &mut channel_state.pending_msg_events;
3634                 channel_state.by_id.retain(|_, chan| {
3635                         if chan.get_counterparty_node_id() == *counterparty_node_id {
3636                                 if !chan.have_received_message() {
3637                                         // If we created this (outbound) channel while we were disconnected from the
3638                                         // peer we probably failed to send the open_channel message, which is now
3639                                         // lost. We can't have had anything pending related to this channel, so we just
3640                                         // drop it.
3641                                         false
3642                                 } else {
3643                                         pending_msg_events.push(events::MessageSendEvent::SendChannelReestablish {
3644                                                 node_id: chan.get_counterparty_node_id(),
3645                                                 msg: chan.get_channel_reestablish(&self.logger),
3646                                         });
3647                                         true
3648                                 }
3649                         } else { true }
3650                 });
3651                 //TODO: Also re-broadcast announcement_signatures
3652         }
3653
3654         fn handle_error(&self, counterparty_node_id: &PublicKey, msg: &msgs::ErrorMessage) {
3655                 let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
3656
3657                 if msg.channel_id == [0; 32] {
3658                         for chan in self.list_channels() {
3659                                 if chan.remote_network_id == *counterparty_node_id {
3660                                         // Untrusted messages from peer, we throw away the error if id points to a non-existent channel
3661                                         let _ = self.force_close_channel_with_peer(&chan.channel_id, Some(counterparty_node_id));
3662                                 }
3663                         }
3664                 } else {
3665                         // Untrusted messages from peer, we throw away the error if id points to a non-existent channel
3666                         let _ = self.force_close_channel_with_peer(&msg.channel_id, Some(counterparty_node_id));
3667                 }
3668         }
3669 }
3670
3671 /// Used to signal to the ChannelManager persister that the manager needs to be re-persisted to
3672 /// disk/backups, through `wait_timeout` and `wait`.
3673 struct PersistenceNotifier {
3674         /// Users won't access the persistence_lock directly, but rather wait on its bool using
3675         /// `wait_timeout` and `wait`.
3676         persistence_lock: (Mutex<bool>, Condvar),
3677 }
3678
3679 impl PersistenceNotifier {
3680         fn new() -> Self {
3681                 Self {
3682                         persistence_lock: (Mutex::new(false), Condvar::new()),
3683                 }
3684         }
3685
3686         fn wait(&self) {
3687                 loop {
3688                         let &(ref mtx, ref cvar) = &self.persistence_lock;
3689                         let mut guard = mtx.lock().unwrap();
3690                         guard = cvar.wait(guard).unwrap();
3691                         let result = *guard;
3692                         if result {
3693                                 *guard = false;
3694                                 return
3695                         }
3696                 }
3697         }
3698
3699         #[cfg(any(test, feature = "allow_wallclock_use"))]
3700         fn wait_timeout(&self, max_wait: Duration) -> bool {
3701                 let current_time = Instant::now();
3702                 loop {
3703                         let &(ref mtx, ref cvar) = &self.persistence_lock;
3704                         let mut guard = mtx.lock().unwrap();
3705                         guard = cvar.wait_timeout(guard, max_wait).unwrap().0;
3706                         // Due to spurious wakeups that can happen on `wait_timeout`, here we need to check if the
3707                         // desired wait time has actually passed, and if not then restart the loop with a reduced wait
3708                         // time. Note that this logic can be highly simplified through the use of
3709                         // `Condvar::wait_while` and `Condvar::wait_timeout_while`, if and when our MSRV is raised to
3710                         // 1.42.0.
3711                         let elapsed = current_time.elapsed();
3712                         let result = *guard;
3713                         if result || elapsed >= max_wait {
3714                                 *guard = false;
3715                                 return result;
3716                         }
3717                         match max_wait.checked_sub(elapsed) {
3718                                 None => return result,
3719                                 Some(_) => continue
3720                         }
3721                 }
3722         }
3723
3724         // Signal to the ChannelManager persister that there are updates necessitating persisting to disk.
3725         fn notify(&self) {
3726                 let &(ref persist_mtx, ref cnd) = &self.persistence_lock;
3727                 let mut persistence_lock = persist_mtx.lock().unwrap();
3728                 *persistence_lock = true;
3729                 mem::drop(persistence_lock);
3730                 cnd.notify_all();
3731         }
3732 }
3733
3734 const SERIALIZATION_VERSION: u8 = 1;
3735 const MIN_SERIALIZATION_VERSION: u8 = 1;
3736
3737 impl Writeable for PendingHTLCInfo {
3738         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
3739                 match &self.routing {
3740                         &PendingHTLCRouting::Forward { ref onion_packet, ref short_channel_id } => {
3741                                 0u8.write(writer)?;
3742                                 onion_packet.write(writer)?;
3743                                 short_channel_id.write(writer)?;
3744                         },
3745                         &PendingHTLCRouting::Receive { ref payment_data, ref incoming_cltv_expiry } => {
3746                                 1u8.write(writer)?;
3747                                 payment_data.write(writer)?;
3748                                 incoming_cltv_expiry.write(writer)?;
3749                         },
3750                 }
3751                 self.incoming_shared_secret.write(writer)?;
3752                 self.payment_hash.write(writer)?;
3753                 self.amt_to_forward.write(writer)?;
3754                 self.outgoing_cltv_value.write(writer)?;
3755                 Ok(())
3756         }
3757 }
3758
3759 impl Readable for PendingHTLCInfo {
3760         fn read<R: ::std::io::Read>(reader: &mut R) -> Result<PendingHTLCInfo, DecodeError> {
3761                 Ok(PendingHTLCInfo {
3762                         routing: match Readable::read(reader)? {
3763                                 0u8 => PendingHTLCRouting::Forward {
3764                                         onion_packet: Readable::read(reader)?,
3765                                         short_channel_id: Readable::read(reader)?,
3766                                 },
3767                                 1u8 => PendingHTLCRouting::Receive {
3768                                         payment_data: Readable::read(reader)?,
3769                                         incoming_cltv_expiry: Readable::read(reader)?,
3770                                 },
3771                                 _ => return Err(DecodeError::InvalidValue),
3772                         },
3773                         incoming_shared_secret: Readable::read(reader)?,
3774                         payment_hash: Readable::read(reader)?,
3775                         amt_to_forward: Readable::read(reader)?,
3776                         outgoing_cltv_value: Readable::read(reader)?,
3777                 })
3778         }
3779 }
3780
3781 impl Writeable for HTLCFailureMsg {
3782         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
3783                 match self {
3784                         &HTLCFailureMsg::Relay(ref fail_msg) => {
3785                                 0u8.write(writer)?;
3786                                 fail_msg.write(writer)?;
3787                         },
3788                         &HTLCFailureMsg::Malformed(ref fail_msg) => {
3789                                 1u8.write(writer)?;
3790                                 fail_msg.write(writer)?;
3791                         }
3792                 }
3793                 Ok(())
3794         }
3795 }
3796
3797 impl Readable for HTLCFailureMsg {
3798         fn read<R: ::std::io::Read>(reader: &mut R) -> Result<HTLCFailureMsg, DecodeError> {
3799                 match <u8 as Readable>::read(reader)? {
3800                         0 => Ok(HTLCFailureMsg::Relay(Readable::read(reader)?)),
3801                         1 => Ok(HTLCFailureMsg::Malformed(Readable::read(reader)?)),
3802                         _ => Err(DecodeError::InvalidValue),
3803                 }
3804         }
3805 }
3806
3807 impl Writeable for PendingHTLCStatus {
3808         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
3809                 match self {
3810                         &PendingHTLCStatus::Forward(ref forward_info) => {
3811                                 0u8.write(writer)?;
3812                                 forward_info.write(writer)?;
3813                         },
3814                         &PendingHTLCStatus::Fail(ref fail_msg) => {
3815                                 1u8.write(writer)?;
3816                                 fail_msg.write(writer)?;
3817                         }
3818                 }
3819                 Ok(())
3820         }
3821 }
3822
3823 impl Readable for PendingHTLCStatus {
3824         fn read<R: ::std::io::Read>(reader: &mut R) -> Result<PendingHTLCStatus, DecodeError> {
3825                 match <u8 as Readable>::read(reader)? {
3826                         0 => Ok(PendingHTLCStatus::Forward(Readable::read(reader)?)),
3827                         1 => Ok(PendingHTLCStatus::Fail(Readable::read(reader)?)),
3828                         _ => Err(DecodeError::InvalidValue),
3829                 }
3830         }
3831 }
3832
3833 impl_writeable!(HTLCPreviousHopData, 0, {
3834         short_channel_id,
3835         outpoint,
3836         htlc_id,
3837         incoming_packet_shared_secret
3838 });
3839
3840 impl_writeable!(ClaimableHTLC, 0, {
3841         prev_hop,
3842         value,
3843         payment_data,
3844         cltv_expiry
3845 });
3846
3847 impl Writeable for HTLCSource {
3848         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
3849                 match self {
3850                         &HTLCSource::PreviousHopData(ref hop_data) => {
3851                                 0u8.write(writer)?;
3852                                 hop_data.write(writer)?;
3853                         },
3854                         &HTLCSource::OutboundRoute { ref path, ref session_priv, ref first_hop_htlc_msat } => {
3855                                 1u8.write(writer)?;
3856                                 path.write(writer)?;
3857                                 session_priv.write(writer)?;
3858                                 first_hop_htlc_msat.write(writer)?;
3859                         }
3860                 }
3861                 Ok(())
3862         }
3863 }
3864
3865 impl Readable for HTLCSource {
3866         fn read<R: ::std::io::Read>(reader: &mut R) -> Result<HTLCSource, DecodeError> {
3867                 match <u8 as Readable>::read(reader)? {
3868                         0 => Ok(HTLCSource::PreviousHopData(Readable::read(reader)?)),
3869                         1 => Ok(HTLCSource::OutboundRoute {
3870                                 path: Readable::read(reader)?,
3871                                 session_priv: Readable::read(reader)?,
3872                                 first_hop_htlc_msat: Readable::read(reader)?,
3873                         }),
3874                         _ => Err(DecodeError::InvalidValue),
3875                 }
3876         }
3877 }
3878
3879 impl Writeable for HTLCFailReason {
3880         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
3881                 match self {
3882                         &HTLCFailReason::LightningError { ref err } => {
3883                                 0u8.write(writer)?;
3884                                 err.write(writer)?;
3885                         },
3886                         &HTLCFailReason::Reason { ref failure_code, ref data } => {
3887                                 1u8.write(writer)?;
3888                                 failure_code.write(writer)?;
3889                                 data.write(writer)?;
3890                         }
3891                 }
3892                 Ok(())
3893         }
3894 }
3895
3896 impl Readable for HTLCFailReason {
3897         fn read<R: ::std::io::Read>(reader: &mut R) -> Result<HTLCFailReason, DecodeError> {
3898                 match <u8 as Readable>::read(reader)? {
3899                         0 => Ok(HTLCFailReason::LightningError { err: Readable::read(reader)? }),
3900                         1 => Ok(HTLCFailReason::Reason {
3901                                 failure_code: Readable::read(reader)?,
3902                                 data: Readable::read(reader)?,
3903                         }),
3904                         _ => Err(DecodeError::InvalidValue),
3905                 }
3906         }
3907 }
3908
3909 impl Writeable for HTLCForwardInfo {
3910         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
3911                 match self {
3912                         &HTLCForwardInfo::AddHTLC { ref prev_short_channel_id, ref prev_funding_outpoint, ref prev_htlc_id, ref forward_info } => {
3913                                 0u8.write(writer)?;
3914                                 prev_short_channel_id.write(writer)?;
3915                                 prev_funding_outpoint.write(writer)?;
3916                                 prev_htlc_id.write(writer)?;
3917                                 forward_info.write(writer)?;
3918                         },
3919                         &HTLCForwardInfo::FailHTLC { ref htlc_id, ref err_packet } => {
3920                                 1u8.write(writer)?;
3921                                 htlc_id.write(writer)?;
3922                                 err_packet.write(writer)?;
3923                         },
3924                 }
3925                 Ok(())
3926         }
3927 }
3928
3929 impl Readable for HTLCForwardInfo {
3930         fn read<R: ::std::io::Read>(reader: &mut R) -> Result<HTLCForwardInfo, DecodeError> {
3931                 match <u8 as Readable>::read(reader)? {
3932                         0 => Ok(HTLCForwardInfo::AddHTLC {
3933                                 prev_short_channel_id: Readable::read(reader)?,
3934                                 prev_funding_outpoint: Readable::read(reader)?,
3935                                 prev_htlc_id: Readable::read(reader)?,
3936                                 forward_info: Readable::read(reader)?,
3937                         }),
3938                         1 => Ok(HTLCForwardInfo::FailHTLC {
3939                                 htlc_id: Readable::read(reader)?,
3940                                 err_packet: Readable::read(reader)?,
3941                         }),
3942                         _ => Err(DecodeError::InvalidValue),
3943                 }
3944         }
3945 }
3946
3947 impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> Writeable for ChannelManager<Signer, M, T, K, F, L>
3948         where M::Target: chain::Watch<Signer>,
3949         T::Target: BroadcasterInterface,
3950         K::Target: KeysInterface<Signer = Signer>,
3951         F::Target: FeeEstimator,
3952         L::Target: Logger,
3953 {
3954         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
3955                 let _consistency_lock = self.total_consistency_lock.write().unwrap();
3956
3957                 writer.write_all(&[SERIALIZATION_VERSION; 1])?;
3958                 writer.write_all(&[MIN_SERIALIZATION_VERSION; 1])?;
3959
3960                 self.genesis_hash.write(writer)?;
3961                 (self.latest_block_height.load(Ordering::Acquire) as u32).write(writer)?;
3962                 self.last_block_hash.read().unwrap().write(writer)?;
3963
3964                 let channel_state = self.channel_state.lock().unwrap();
3965                 let mut unfunded_channels = 0;
3966                 for (_, channel) in channel_state.by_id.iter() {
3967                         if !channel.is_funding_initiated() {
3968                                 unfunded_channels += 1;
3969                         }
3970                 }
3971                 ((channel_state.by_id.len() - unfunded_channels) as u64).write(writer)?;
3972                 for (_, channel) in channel_state.by_id.iter() {
3973                         if channel.is_funding_initiated() {
3974                                 channel.write(writer)?;
3975                         }
3976                 }
3977
3978                 (channel_state.forward_htlcs.len() as u64).write(writer)?;
3979                 for (short_channel_id, pending_forwards) in channel_state.forward_htlcs.iter() {
3980                         short_channel_id.write(writer)?;
3981                         (pending_forwards.len() as u64).write(writer)?;
3982                         for forward in pending_forwards {
3983                                 forward.write(writer)?;
3984                         }
3985                 }
3986
3987                 (channel_state.claimable_htlcs.len() as u64).write(writer)?;
3988                 for (payment_hash, previous_hops) in channel_state.claimable_htlcs.iter() {
3989                         payment_hash.write(writer)?;
3990                         (previous_hops.len() as u64).write(writer)?;
3991                         for htlc in previous_hops.iter() {
3992                                 htlc.write(writer)?;
3993                         }
3994                 }
3995
3996                 let per_peer_state = self.per_peer_state.write().unwrap();
3997                 (per_peer_state.len() as u64).write(writer)?;
3998                 for (peer_pubkey, peer_state_mutex) in per_peer_state.iter() {
3999                         peer_pubkey.write(writer)?;
4000                         let peer_state = peer_state_mutex.lock().unwrap();
4001                         peer_state.latest_features.write(writer)?;
4002                 }
4003
4004                 let events = self.pending_events.lock().unwrap();
4005                 (events.len() as u64).write(writer)?;
4006                 for event in events.iter() {
4007                         event.write(writer)?;
4008                 }
4009
4010                 let background_events = self.pending_background_events.lock().unwrap();
4011                 (background_events.len() as u64).write(writer)?;
4012                 for event in background_events.iter() {
4013                         match event {
4014                                 BackgroundEvent::ClosingMonitorUpdate((funding_txo, monitor_update)) => {
4015                                         0u8.write(writer)?;
4016                                         funding_txo.write(writer)?;
4017                                         monitor_update.write(writer)?;
4018                                 },
4019                         }
4020                 }
4021
4022                 (self.last_node_announcement_serial.load(Ordering::Acquire) as u32).write(writer)?;
4023
4024                 Ok(())
4025         }
4026 }
4027
4028 /// Arguments for the creation of a ChannelManager that are not deserialized.
4029 ///
4030 /// At a high-level, the process for deserializing a ChannelManager and resuming normal operation
4031 /// is:
4032 /// 1) Deserialize all stored ChannelMonitors.
4033 /// 2) Deserialize the ChannelManager by filling in this struct and calling:
4034 ///    <(BlockHash, ChannelManager)>::read(reader, args)
4035 ///    This may result in closing some Channels if the ChannelMonitor is newer than the stored
4036 ///    ChannelManager state to ensure no loss of funds. Thus, transactions may be broadcasted.
4037 /// 3) If you are not fetching full blocks, register all relevant ChannelMonitor outpoints the same
4038 ///    way you would handle a `chain::Filter` call using ChannelMonitor::get_outputs_to_watch() and
4039 ///    ChannelMonitor::get_funding_txo().
4040 /// 4) Reconnect blocks on your ChannelMonitors.
4041 /// 5) Disconnect/connect blocks on the ChannelManager.
4042 /// 6) Move the ChannelMonitors into your local chain::Watch.
4043 ///
4044 /// Note that the ordering of #4-6 is not of importance, however all three must occur before you
4045 /// call any other methods on the newly-deserialized ChannelManager.
4046 ///
4047 /// Note that because some channels may be closed during deserialization, it is critical that you
4048 /// always deserialize only the latest version of a ChannelManager and ChannelMonitors available to
4049 /// you. If you deserialize an old ChannelManager (during which force-closure transactions may be
4050 /// broadcast), and then later deserialize a newer version of the same ChannelManager (which will
4051 /// not force-close the same channels but consider them live), you may end up revoking a state for
4052 /// which you've already broadcasted the transaction.
4053 pub struct ChannelManagerReadArgs<'a, Signer: 'a + Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
4054         where M::Target: chain::Watch<Signer>,
4055         T::Target: BroadcasterInterface,
4056         K::Target: KeysInterface<Signer = Signer>,
4057         F::Target: FeeEstimator,
4058         L::Target: Logger,
4059 {
4060         /// The keys provider which will give us relevant keys. Some keys will be loaded during
4061         /// deserialization and KeysInterface::read_chan_signer will be used to read per-Channel
4062         /// signing data.
4063         pub keys_manager: K,
4064
4065         /// The fee_estimator for use in the ChannelManager in the future.
4066         ///
4067         /// No calls to the FeeEstimator will be made during deserialization.
4068         pub fee_estimator: F,
4069         /// The chain::Watch for use in the ChannelManager in the future.
4070         ///
4071         /// No calls to the chain::Watch will be made during deserialization. It is assumed that
4072         /// you have deserialized ChannelMonitors separately and will add them to your
4073         /// chain::Watch after deserializing this ChannelManager.
4074         pub chain_monitor: M,
4075
4076         /// The BroadcasterInterface which will be used in the ChannelManager in the future and may be
4077         /// used to broadcast the latest local commitment transactions of channels which must be
4078         /// force-closed during deserialization.
4079         pub tx_broadcaster: T,
4080         /// The Logger for use in the ChannelManager and which may be used to log information during
4081         /// deserialization.
4082         pub logger: L,
4083         /// Default settings used for new channels. Any existing channels will continue to use the
4084         /// runtime settings which were stored when the ChannelManager was serialized.
4085         pub default_config: UserConfig,
4086
4087         /// A map from channel funding outpoints to ChannelMonitors for those channels (ie
4088         /// value.get_funding_txo() should be the key).
4089         ///
4090         /// If a monitor is inconsistent with the channel state during deserialization the channel will
4091         /// be force-closed using the data in the ChannelMonitor and the channel will be dropped. This
4092         /// is true for missing channels as well. If there is a monitor missing for which we find
4093         /// channel data Err(DecodeError::InvalidValue) will be returned.
4094         ///
4095         /// In such cases the latest local transactions will be sent to the tx_broadcaster included in
4096         /// this struct.
4097         ///
4098         /// (C-not exported) because we have no HashMap bindings
4099         pub channel_monitors: HashMap<OutPoint, &'a mut ChannelMonitor<Signer>>,
4100 }
4101
4102 impl<'a, Signer: 'a + Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
4103                 ChannelManagerReadArgs<'a, Signer, M, T, K, F, L>
4104         where M::Target: chain::Watch<Signer>,
4105                 T::Target: BroadcasterInterface,
4106                 K::Target: KeysInterface<Signer = Signer>,
4107                 F::Target: FeeEstimator,
4108                 L::Target: Logger,
4109         {
4110         /// Simple utility function to create a ChannelManagerReadArgs which creates the monitor
4111         /// HashMap for you. This is primarily useful for C bindings where it is not practical to
4112         /// populate a HashMap directly from C.
4113         pub fn new(keys_manager: K, fee_estimator: F, chain_monitor: M, tx_broadcaster: T, logger: L, default_config: UserConfig,
4114                         mut channel_monitors: Vec<&'a mut ChannelMonitor<Signer>>) -> Self {
4115                 Self {
4116                         keys_manager, fee_estimator, chain_monitor, tx_broadcaster, logger, default_config,
4117                         channel_monitors: channel_monitors.drain(..).map(|monitor| { (monitor.get_funding_txo().0, monitor) }).collect()
4118                 }
4119         }
4120 }
4121
4122 // Implement ReadableArgs for an Arc'd ChannelManager to make it a bit easier to work with the
4123 // SipmleArcChannelManager type:
4124 impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
4125         ReadableArgs<ChannelManagerReadArgs<'a, Signer, M, T, K, F, L>> for (BlockHash, Arc<ChannelManager<Signer, M, T, K, F, L>>)
4126         where M::Target: chain::Watch<Signer>,
4127         T::Target: BroadcasterInterface,
4128         K::Target: KeysInterface<Signer = Signer>,
4129         F::Target: FeeEstimator,
4130         L::Target: Logger,
4131 {
4132         fn read<R: ::std::io::Read>(reader: &mut R, args: ChannelManagerReadArgs<'a, Signer, M, T, K, F, L>) -> Result<Self, DecodeError> {
4133                 let (blockhash, chan_manager) = <(BlockHash, ChannelManager<Signer, M, T, K, F, L>)>::read(reader, args)?;
4134                 Ok((blockhash, Arc::new(chan_manager)))
4135         }
4136 }
4137
4138 impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
4139         ReadableArgs<ChannelManagerReadArgs<'a, Signer, M, T, K, F, L>> for (BlockHash, ChannelManager<Signer, M, T, K, F, L>)
4140         where M::Target: chain::Watch<Signer>,
4141         T::Target: BroadcasterInterface,
4142         K::Target: KeysInterface<Signer = Signer>,
4143         F::Target: FeeEstimator,
4144         L::Target: Logger,
4145 {
4146         fn read<R: ::std::io::Read>(reader: &mut R, mut args: ChannelManagerReadArgs<'a, Signer, M, T, K, F, L>) -> Result<Self, DecodeError> {
4147                 let _ver: u8 = Readable::read(reader)?;
4148                 let min_ver: u8 = Readable::read(reader)?;
4149                 if min_ver > SERIALIZATION_VERSION {
4150                         return Err(DecodeError::UnknownVersion);
4151                 }
4152
4153                 let genesis_hash: BlockHash = Readable::read(reader)?;
4154                 let latest_block_height: u32 = Readable::read(reader)?;
4155                 let last_block_hash: BlockHash = Readable::read(reader)?;
4156
4157                 let mut failed_htlcs = Vec::new();
4158
4159                 let channel_count: u64 = Readable::read(reader)?;
4160                 let mut funding_txo_set = HashSet::with_capacity(cmp::min(channel_count as usize, 128));
4161                 let mut by_id = HashMap::with_capacity(cmp::min(channel_count as usize, 128));
4162                 let mut short_to_id = HashMap::with_capacity(cmp::min(channel_count as usize, 128));
4163                 for _ in 0..channel_count {
4164                         let mut channel: Channel<Signer> = Channel::read(reader, &args.keys_manager)?;
4165                         let funding_txo = channel.get_funding_txo().ok_or(DecodeError::InvalidValue)?;
4166                         funding_txo_set.insert(funding_txo.clone());
4167                         if let Some(ref mut monitor) = args.channel_monitors.get_mut(&funding_txo) {
4168                                 if channel.get_cur_holder_commitment_transaction_number() < monitor.get_cur_holder_commitment_number() ||
4169                                                 channel.get_revoked_counterparty_commitment_transaction_number() < monitor.get_min_seen_secret() ||
4170                                                 channel.get_cur_counterparty_commitment_transaction_number() < monitor.get_cur_counterparty_commitment_number() ||
4171                                                 channel.get_latest_monitor_update_id() > monitor.get_latest_update_id() {
4172                                         // If the channel is ahead of the monitor, return InvalidValue:
4173                                         return Err(DecodeError::InvalidValue);
4174                                 } else if channel.get_cur_holder_commitment_transaction_number() > monitor.get_cur_holder_commitment_number() ||
4175                                                 channel.get_revoked_counterparty_commitment_transaction_number() > monitor.get_min_seen_secret() ||
4176                                                 channel.get_cur_counterparty_commitment_transaction_number() > monitor.get_cur_counterparty_commitment_number() ||
4177                                                 channel.get_latest_monitor_update_id() < monitor.get_latest_update_id() {
4178                                         // But if the channel is behind of the monitor, close the channel:
4179                                         let (_, mut new_failed_htlcs) = channel.force_shutdown(true);
4180                                         failed_htlcs.append(&mut new_failed_htlcs);
4181                                         monitor.broadcast_latest_holder_commitment_txn(&args.tx_broadcaster, &args.logger);
4182                                 } else {
4183                                         if let Some(short_channel_id) = channel.get_short_channel_id() {
4184                                                 short_to_id.insert(short_channel_id, channel.channel_id());
4185                                         }
4186                                         by_id.insert(channel.channel_id(), channel);
4187                                 }
4188                         } else {
4189                                 return Err(DecodeError::InvalidValue);
4190                         }
4191                 }
4192
4193                 for (ref funding_txo, ref mut monitor) in args.channel_monitors.iter_mut() {
4194                         if !funding_txo_set.contains(funding_txo) {
4195                                 monitor.broadcast_latest_holder_commitment_txn(&args.tx_broadcaster, &args.logger);
4196                         }
4197                 }
4198
4199                 const MAX_ALLOC_SIZE: usize = 1024 * 64;
4200                 let forward_htlcs_count: u64 = Readable::read(reader)?;
4201                 let mut forward_htlcs = HashMap::with_capacity(cmp::min(forward_htlcs_count as usize, 128));
4202                 for _ in 0..forward_htlcs_count {
4203                         let short_channel_id = Readable::read(reader)?;
4204                         let pending_forwards_count: u64 = Readable::read(reader)?;
4205                         let mut pending_forwards = Vec::with_capacity(cmp::min(pending_forwards_count as usize, MAX_ALLOC_SIZE/mem::size_of::<HTLCForwardInfo>()));
4206                         for _ in 0..pending_forwards_count {
4207                                 pending_forwards.push(Readable::read(reader)?);
4208                         }
4209                         forward_htlcs.insert(short_channel_id, pending_forwards);
4210                 }
4211
4212                 let claimable_htlcs_count: u64 = Readable::read(reader)?;
4213                 let mut claimable_htlcs = HashMap::with_capacity(cmp::min(claimable_htlcs_count as usize, 128));
4214                 for _ in 0..claimable_htlcs_count {
4215                         let payment_hash = Readable::read(reader)?;
4216                         let previous_hops_len: u64 = Readable::read(reader)?;
4217                         let mut previous_hops = Vec::with_capacity(cmp::min(previous_hops_len as usize, MAX_ALLOC_SIZE/mem::size_of::<ClaimableHTLC>()));
4218                         for _ in 0..previous_hops_len {
4219                                 previous_hops.push(Readable::read(reader)?);
4220                         }
4221                         claimable_htlcs.insert(payment_hash, previous_hops);
4222                 }
4223
4224                 let peer_count: u64 = Readable::read(reader)?;
4225                 let mut per_peer_state = HashMap::with_capacity(cmp::min(peer_count as usize, MAX_ALLOC_SIZE/mem::size_of::<(PublicKey, Mutex<PeerState>)>()));
4226                 for _ in 0..peer_count {
4227                         let peer_pubkey = Readable::read(reader)?;
4228                         let peer_state = PeerState {
4229                                 latest_features: Readable::read(reader)?,
4230                         };
4231                         per_peer_state.insert(peer_pubkey, Mutex::new(peer_state));
4232                 }
4233
4234                 let event_count: u64 = Readable::read(reader)?;
4235                 let mut pending_events_read: Vec<events::Event> = Vec::with_capacity(cmp::min(event_count as usize, MAX_ALLOC_SIZE/mem::size_of::<events::Event>()));
4236                 for _ in 0..event_count {
4237                         match MaybeReadable::read(reader)? {
4238                                 Some(event) => pending_events_read.push(event),
4239                                 None => continue,
4240                         }
4241                 }
4242
4243                 let background_event_count: u64 = Readable::read(reader)?;
4244                 let mut pending_background_events_read: Vec<BackgroundEvent> = Vec::with_capacity(cmp::min(background_event_count as usize, MAX_ALLOC_SIZE/mem::size_of::<BackgroundEvent>()));
4245                 for _ in 0..background_event_count {
4246                         match <u8 as Readable>::read(reader)? {
4247                                 0 => pending_background_events_read.push(BackgroundEvent::ClosingMonitorUpdate((Readable::read(reader)?, Readable::read(reader)?))),
4248                                 _ => return Err(DecodeError::InvalidValue),
4249                         }
4250                 }
4251
4252                 let last_node_announcement_serial: u32 = Readable::read(reader)?;
4253
4254                 let mut secp_ctx = Secp256k1::new();
4255                 secp_ctx.seeded_randomize(&args.keys_manager.get_secure_random_bytes());
4256
4257                 let channel_manager = ChannelManager {
4258                         genesis_hash,
4259                         fee_estimator: args.fee_estimator,
4260                         chain_monitor: args.chain_monitor,
4261                         tx_broadcaster: args.tx_broadcaster,
4262
4263                         latest_block_height: AtomicUsize::new(latest_block_height as usize),
4264                         last_block_hash: RwLock::new(last_block_hash),
4265                         secp_ctx,
4266
4267                         channel_state: Mutex::new(ChannelHolder {
4268                                 by_id,
4269                                 short_to_id,
4270                                 forward_htlcs,
4271                                 claimable_htlcs,
4272                                 pending_msg_events: Vec::new(),
4273                         }),
4274                         our_network_key: args.keys_manager.get_node_secret(),
4275
4276                         last_node_announcement_serial: AtomicUsize::new(last_node_announcement_serial as usize),
4277
4278                         per_peer_state: RwLock::new(per_peer_state),
4279
4280                         pending_events: Mutex::new(pending_events_read),
4281                         pending_background_events: Mutex::new(pending_background_events_read),
4282                         total_consistency_lock: RwLock::new(()),
4283                         persistence_notifier: PersistenceNotifier::new(),
4284
4285                         keys_manager: args.keys_manager,
4286                         logger: args.logger,
4287                         default_configuration: args.default_config,
4288                 };
4289
4290                 for htlc_source in failed_htlcs.drain(..) {
4291                         channel_manager.fail_htlc_backwards_internal(channel_manager.channel_state.lock().unwrap(), htlc_source.0, &htlc_source.1, HTLCFailReason::Reason { failure_code: 0x4000 | 8, data: Vec::new() });
4292                 }
4293
4294                 //TODO: Broadcast channel update for closed channels, but only after we've made a
4295                 //connection or two.
4296
4297                 Ok((last_block_hash.clone(), channel_manager))
4298         }
4299 }
4300
4301 #[cfg(test)]
4302 mod tests {
4303         use ln::channelmanager::PersistenceNotifier;
4304         use std::sync::Arc;
4305         use std::sync::atomic::{AtomicBool, Ordering};
4306         use std::thread;
4307         use std::time::Duration;
4308
4309         #[test]
4310         fn test_wait_timeout() {
4311                 let persistence_notifier = Arc::new(PersistenceNotifier::new());
4312                 let thread_notifier = Arc::clone(&persistence_notifier);
4313
4314                 let exit_thread = Arc::new(AtomicBool::new(false));
4315                 let exit_thread_clone = exit_thread.clone();
4316                 thread::spawn(move || {
4317                         loop {
4318                                 let &(ref persist_mtx, ref cnd) = &thread_notifier.persistence_lock;
4319                                 let mut persistence_lock = persist_mtx.lock().unwrap();
4320                                 *persistence_lock = true;
4321                                 cnd.notify_all();
4322
4323                                 if exit_thread_clone.load(Ordering::SeqCst) {
4324                                         break
4325                                 }
4326                         }
4327                 });
4328
4329                 // Check that we can block indefinitely until updates are available.
4330                 let _ = persistence_notifier.wait();
4331
4332                 // Check that the PersistenceNotifier will return after the given duration if updates are
4333                 // available.
4334                 loop {
4335                         if persistence_notifier.wait_timeout(Duration::from_millis(100)) {
4336                                 break
4337                         }
4338                 }
4339
4340                 exit_thread.store(true, Ordering::SeqCst);
4341
4342                 // Check that the PersistenceNotifier will return after the given duration even if no updates
4343                 // are available.
4344                 loop {
4345                         if !persistence_notifier.wait_timeout(Duration::from_millis(100)) {
4346                                 break
4347                         }
4348                 }
4349         }
4350 }