Use workspaces to separate crates
authorRJ Rybarczyk <rj@rybar.tech>
Fri, 15 Nov 2019 02:44:30 +0000 (02:44 +0000)
committerRJ Rybarczyk <rj@rybar.tech>
Fri, 15 Nov 2019 02:44:30 +0000 (02:44 +0000)
149 files changed:
.travis.yml
Cargo.toml
fuzz/.gitignore [deleted file]
fuzz/Cargo.toml [deleted file]
fuzz/fuzz_targets/chanmon_deser_target.rs [deleted file]
fuzz/fuzz_targets/chanmon_fail_consistency.rs [deleted file]
fuzz/fuzz_targets/full_stack_target.rs [deleted file]
fuzz/fuzz_targets/msg_targets/gen_target.sh [deleted file]
fuzz/fuzz_targets/msg_targets/msg_accept_channel_target.rs [deleted file]
fuzz/fuzz_targets/msg_targets/msg_announcement_signatures_target.rs [deleted file]
fuzz/fuzz_targets/msg_targets/msg_channel_announcement_target.rs [deleted file]
fuzz/fuzz_targets/msg_targets/msg_channel_reestablish_target.rs [deleted file]
fuzz/fuzz_targets/msg_targets/msg_channel_update_target.rs [deleted file]
fuzz/fuzz_targets/msg_targets/msg_closing_signed_target.rs [deleted file]
fuzz/fuzz_targets/msg_targets/msg_commitment_signed_target.rs [deleted file]
fuzz/fuzz_targets/msg_targets/msg_decoded_onion_error_packet_target.rs [deleted file]
fuzz/fuzz_targets/msg_targets/msg_error_message_target.rs [deleted file]
fuzz/fuzz_targets/msg_targets/msg_funding_created_target.rs [deleted file]
fuzz/fuzz_targets/msg_targets/msg_funding_locked_target.rs [deleted file]
fuzz/fuzz_targets/msg_targets/msg_funding_signed_target.rs [deleted file]
fuzz/fuzz_targets/msg_targets/msg_init_target.rs [deleted file]
fuzz/fuzz_targets/msg_targets/msg_node_announcement_target.rs [deleted file]
fuzz/fuzz_targets/msg_targets/msg_onion_hop_data_target.rs [deleted file]
fuzz/fuzz_targets/msg_targets/msg_open_channel_target.rs [deleted file]
fuzz/fuzz_targets/msg_targets/msg_ping_target.rs [deleted file]
fuzz/fuzz_targets/msg_targets/msg_pong_target.rs [deleted file]
fuzz/fuzz_targets/msg_targets/msg_revoke_and_ack_target.rs [deleted file]
fuzz/fuzz_targets/msg_targets/msg_shutdown_target.rs [deleted file]
fuzz/fuzz_targets/msg_targets/msg_target_template.txt [deleted file]
fuzz/fuzz_targets/msg_targets/msg_update_add_htlc_target.rs [deleted file]
fuzz/fuzz_targets/msg_targets/msg_update_fail_htlc_target.rs [deleted file]
fuzz/fuzz_targets/msg_targets/msg_update_fail_malformed_htlc_target.rs [deleted file]
fuzz/fuzz_targets/msg_targets/msg_update_fee_target.rs [deleted file]
fuzz/fuzz_targets/msg_targets/msg_update_fulfill_htlc_target.rs [deleted file]
fuzz/fuzz_targets/msg_targets/utils.rs [deleted file]
fuzz/fuzz_targets/peer_crypt_target.rs [deleted file]
fuzz/fuzz_targets/router_target.rs [deleted file]
fuzz/fuzz_targets/utils/mod.rs [deleted file]
fuzz/fuzz_targets/utils/test_logger.rs [deleted file]
fuzz/travis-fuzz.sh [deleted file]
lightning-net-tokio/Cargo.toml [new file with mode: 0644]
lightning-net-tokio/src/lib.rs [new file with mode: 0644]
lightning/Cargo.toml [new file with mode: 0644]
lightning/fuzz/.gitignore [new file with mode: 0644]
lightning/fuzz/Cargo.toml [new file with mode: 0644]
lightning/fuzz/fuzz_targets/chanmon_deser_target.rs [new file with mode: 0644]
lightning/fuzz/fuzz_targets/chanmon_fail_consistency.rs [new file with mode: 0644]
lightning/fuzz/fuzz_targets/full_stack_target.rs [new file with mode: 0644]
lightning/fuzz/fuzz_targets/msg_targets/gen_target.sh [new file with mode: 0755]
lightning/fuzz/fuzz_targets/msg_targets/msg_accept_channel_target.rs [new file with mode: 0644]
lightning/fuzz/fuzz_targets/msg_targets/msg_announcement_signatures_target.rs [new file with mode: 0644]
lightning/fuzz/fuzz_targets/msg_targets/msg_channel_announcement_target.rs [new file with mode: 0644]
lightning/fuzz/fuzz_targets/msg_targets/msg_channel_reestablish_target.rs [new file with mode: 0644]
lightning/fuzz/fuzz_targets/msg_targets/msg_channel_update_target.rs [new file with mode: 0644]
lightning/fuzz/fuzz_targets/msg_targets/msg_closing_signed_target.rs [new file with mode: 0644]
lightning/fuzz/fuzz_targets/msg_targets/msg_commitment_signed_target.rs [new file with mode: 0644]
lightning/fuzz/fuzz_targets/msg_targets/msg_decoded_onion_error_packet_target.rs [new file with mode: 0644]
lightning/fuzz/fuzz_targets/msg_targets/msg_error_message_target.rs [new file with mode: 0644]
lightning/fuzz/fuzz_targets/msg_targets/msg_funding_created_target.rs [new file with mode: 0644]
lightning/fuzz/fuzz_targets/msg_targets/msg_funding_locked_target.rs [new file with mode: 0644]
lightning/fuzz/fuzz_targets/msg_targets/msg_funding_signed_target.rs [new file with mode: 0644]
lightning/fuzz/fuzz_targets/msg_targets/msg_init_target.rs [new file with mode: 0644]
lightning/fuzz/fuzz_targets/msg_targets/msg_node_announcement_target.rs [new file with mode: 0644]
lightning/fuzz/fuzz_targets/msg_targets/msg_onion_hop_data_target.rs [new file with mode: 0644]
lightning/fuzz/fuzz_targets/msg_targets/msg_open_channel_target.rs [new file with mode: 0644]
lightning/fuzz/fuzz_targets/msg_targets/msg_ping_target.rs [new file with mode: 0644]
lightning/fuzz/fuzz_targets/msg_targets/msg_pong_target.rs [new file with mode: 0644]
lightning/fuzz/fuzz_targets/msg_targets/msg_revoke_and_ack_target.rs [new file with mode: 0644]
lightning/fuzz/fuzz_targets/msg_targets/msg_shutdown_target.rs [new file with mode: 0644]
lightning/fuzz/fuzz_targets/msg_targets/msg_target_template.txt [new file with mode: 0644]
lightning/fuzz/fuzz_targets/msg_targets/msg_update_add_htlc_target.rs [new file with mode: 0644]
lightning/fuzz/fuzz_targets/msg_targets/msg_update_fail_htlc_target.rs [new file with mode: 0644]
lightning/fuzz/fuzz_targets/msg_targets/msg_update_fail_malformed_htlc_target.rs [new file with mode: 0644]
lightning/fuzz/fuzz_targets/msg_targets/msg_update_fee_target.rs [new file with mode: 0644]
lightning/fuzz/fuzz_targets/msg_targets/msg_update_fulfill_htlc_target.rs [new file with mode: 0644]
lightning/fuzz/fuzz_targets/msg_targets/utils.rs [new file with mode: 0644]
lightning/fuzz/fuzz_targets/peer_crypt_target.rs [new file with mode: 0644]
lightning/fuzz/fuzz_targets/router_target.rs [new file with mode: 0644]
lightning/fuzz/fuzz_targets/utils/mod.rs [new file with mode: 0644]
lightning/fuzz/fuzz_targets/utils/test_logger.rs [new file with mode: 0644]
lightning/fuzz/travis-fuzz.sh [new file with mode: 0755]
lightning/src/chain/chaininterface.rs [new file with mode: 0644]
lightning/src/chain/keysinterface.rs [new file with mode: 0644]
lightning/src/chain/mod.rs [new file with mode: 0644]
lightning/src/chain/transaction.rs [new file with mode: 0644]
lightning/src/lib.rs [new file with mode: 0644]
lightning/src/ln/chan_utils.rs [new file with mode: 0644]
lightning/src/ln/chanmon_update_fail_tests.rs [new file with mode: 0644]
lightning/src/ln/channel.rs [new file with mode: 0644]
lightning/src/ln/channelmanager.rs [new file with mode: 0644]
lightning/src/ln/channelmonitor.rs [new file with mode: 0644]
lightning/src/ln/functional_test_utils.rs [new file with mode: 0644]
lightning/src/ln/functional_tests.rs [new file with mode: 0644]
lightning/src/ln/mod.rs [new file with mode: 0644]
lightning/src/ln/msgs.rs [new file with mode: 0644]
lightning/src/ln/onion_utils.rs [new file with mode: 0644]
lightning/src/ln/peer_channel_encryptor.rs [new file with mode: 0644]
lightning/src/ln/peer_handler.rs [new file with mode: 0644]
lightning/src/ln/router.rs [new file with mode: 0644]
lightning/src/util/byte_utils.rs [new file with mode: 0644]
lightning/src/util/chacha20.rs [new file with mode: 0644]
lightning/src/util/chacha20poly1305rfc.rs [new file with mode: 0644]
lightning/src/util/config.rs [new file with mode: 0644]
lightning/src/util/errors.rs [new file with mode: 0644]
lightning/src/util/events.rs [new file with mode: 0644]
lightning/src/util/fuzz_wrappers.rs [new file with mode: 0644]
lightning/src/util/logger.rs [new file with mode: 0644]
lightning/src/util/macro_logger.rs [new file with mode: 0644]
lightning/src/util/mod.rs [new file with mode: 0644]
lightning/src/util/poly1305.rs [new file with mode: 0644]
lightning/src/util/ser.rs [new file with mode: 0644]
lightning/src/util/ser_macros.rs [new file with mode: 0644]
lightning/src/util/test_utils.rs [new file with mode: 0644]
lightning/src/util/transaction_utils.rs [new file with mode: 0644]
net-tokio/Cargo.toml [deleted file]
net-tokio/src/lib.rs [deleted file]
src/chain/chaininterface.rs [deleted file]
src/chain/keysinterface.rs [deleted file]
src/chain/mod.rs [deleted file]
src/chain/transaction.rs [deleted file]
src/lib.rs [deleted file]
src/ln/chan_utils.rs [deleted file]
src/ln/chanmon_update_fail_tests.rs [deleted file]
src/ln/channel.rs [deleted file]
src/ln/channelmanager.rs [deleted file]
src/ln/channelmonitor.rs [deleted file]
src/ln/functional_test_utils.rs [deleted file]
src/ln/functional_tests.rs [deleted file]
src/ln/mod.rs [deleted file]
src/ln/msgs.rs [deleted file]
src/ln/onion_utils.rs [deleted file]
src/ln/peer_channel_encryptor.rs [deleted file]
src/ln/peer_handler.rs [deleted file]
src/ln/router.rs [deleted file]
src/util/byte_utils.rs [deleted file]
src/util/chacha20.rs [deleted file]
src/util/chacha20poly1305rfc.rs [deleted file]
src/util/config.rs [deleted file]
src/util/errors.rs [deleted file]
src/util/events.rs [deleted file]
src/util/fuzz_wrappers.rs [deleted file]
src/util/logger.rs [deleted file]
src/util/macro_logger.rs [deleted file]
src/util/mod.rs [deleted file]
src/util/poly1305.rs [deleted file]
src/util/ser.rs [deleted file]
src/util/ser_macros.rs [deleted file]
src/util/test_utils.rs [deleted file]
src/util/transaction_utils.rs [deleted file]

index ef0aa839cf51195cb0304a482a9df86b74b4f008..e4b831db462aac4cf5ea5b37e277d26c0f6bc158 100644 (file)
@@ -14,8 +14,8 @@ script:
   - RUSTFLAGS="-C link-dead-code" cargo build --verbose
   - rm -f target/debug/lightning-* # Make sure we drop old test binaries
   - RUSTFLAGS="-C link-dead-code" cargo test --verbose
-  - if [ "$(rustup show | grep default | grep 1.34.2)" != "" ]; then cd fuzz && cargo test --verbose && ./travis-fuzz.sh; fi
-  - if [ "$(rustup show | grep default | grep stable)" != "" ]; then cd net-tokio && cargo build --verbose && cd ..; fi
+  - if [ "$(rustup show | grep default | grep 1.34.2)" != "" ]; then cd lightning/fuzz && cargo test --verbose && ./travis-fuzz.sh; fi
+  - if [ "$(rustup show | grep default | grep stable)" != "" ]; then cd lightning-net-tokio && cargo build --verbose && cd ..; fi
   - if [ "$(rustup show | grep default | grep stable)" != "" ]; then
       wget https://github.com/SimonKagstrom/kcov/archive/master.tar.gz &&
       tar xzf master.tar.gz &&
index 004e6d25bbbebdcb0151a72a13912856cf59e18d..6ec1776c93383db7a9419bbe2d3511372134bfc0 100644 (file)
@@ -1,38 +1,6 @@
-[package]
-name = "lightning"
-version = "0.0.9"
-authors = ["Matt Corallo"]
-license = "Apache-2.0"
-repository = "https://github.com/rust-bitcoin/rust-lightning/"
-description = """
-A Bitcoin Lightning library in Rust.
-Does most of the hard work, without implying a specific runtime, requiring clients implement basic network logic, chain interactions and disk storage.
-Still missing tons of error-handling. See GitHub issues for suggested projects if you want to contribute. Don't have to bother telling you not to use this for anything serious, because you'd have to build a client around it to even try.
-"""
+[workspace]
 
-[features]
-# Supports tracking channels with a non-bitcoin chain hashes. Currently enables all kinds of fun DoS attacks.
-non_bitcoin_chain_hash_routing = []
-fuzztarget = ["secp256k1/fuzztarget", "bitcoin/fuzztarget", "bitcoin_hashes/fuzztarget"]
-# Unlog messages superior at targeted level.
-max_level_off = []
-max_level_error = []
-max_level_warn = []
-max_level_info = []
-max_level_debug = []
-
-[dependencies]
-bitcoin = "0.20"
-bitcoin_hashes = "0.7"
-secp256k1 = "0.15"
-
-[dev-dependencies.bitcoin]
-version = "0.20"
-features = ["bitcoinconsensus"]
-
-[dev-dependencies]
-hex = "0.3"
-rand = "0.4"
-
-[profile.dev]
-opt-level = 1
+members = [
+    "lightning",
+    "lightning-net-tokio",
+]
diff --git a/fuzz/.gitignore b/fuzz/.gitignore
deleted file mode 100644 (file)
index 8bf27ec..0000000
+++ /dev/null
@@ -1,3 +0,0 @@
-hfuzz_target
-target
-hfuzz_workspace
diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml
deleted file mode 100644 (file)
index 19d832e..0000000
+++ /dev/null
@@ -1,158 +0,0 @@
-[package]
-name = "lightning-fuzz"
-version = "0.0.1"
-authors = ["Automatically generated"]
-publish = false
-# Because the function is unused it gets dropped before we link lightning, so
-# we have to duplicate build.rs here. Note that this is only required for
-# fuzztarget mode.
-
-[package.metadata]
-cargo-fuzz = true
-
-[features]
-afl_fuzz = ["afl"]
-honggfuzz_fuzz = ["honggfuzz"]
-libfuzzer_fuzz = ["libfuzzer-sys"]
-
-[dependencies]
-afl = { version = "0.4", optional = true }
-lightning = { path = "..", features = ["fuzztarget"] }
-bitcoin = { version = "0.20", features = ["fuzztarget"] }
-bitcoin_hashes = { version = "0.7", features = ["fuzztarget"] }
-hex = "0.3"
-honggfuzz = { version = "0.5", optional = true }
-secp256k1 = { version = "0.15", features=["fuzztarget"] }
-libfuzzer-sys = { git = "https://github.com/rust-fuzz/libfuzzer-sys.git", optional = true }
-
-[build-dependencies]
-cc = "1.0"
-
-# Prevent this from interfering with workspaces
-[workspace]
-members = ["."]
-
-[profile.release]
-lto = true
-codegen-units = 1
-
-[[bin]]
-name = "peer_crypt_target"
-path = "fuzz_targets/peer_crypt_target.rs"
-
-[[bin]]
-name = "full_stack_target"
-path = "fuzz_targets/full_stack_target.rs"
-
-[[bin]]
-name = "chanmon_fail_consistency"
-path = "fuzz_targets/chanmon_fail_consistency.rs"
-
-[[bin]]
-name = "router_target"
-path = "fuzz_targets/router_target.rs"
-
-[[bin]]
-name = "chanmon_deser_target"
-path = "fuzz_targets/chanmon_deser_target.rs"
-
-# message fuzz targets
-[[bin]]
-name = "msg_ping_target"
-path = "fuzz_targets/msg_targets/msg_ping_target.rs"
-
-[[bin]]
-name = "msg_pong_target"
-path = "fuzz_targets/msg_targets/msg_pong_target.rs"
-
-[[bin]]
-name = "msg_error_message_target"
-path = "fuzz_targets/msg_targets/msg_error_message_target.rs"
-
-[[bin]]
-name = "msg_update_add_htlc_target"
-path = "fuzz_targets/msg_targets/msg_update_add_htlc_target.rs"
-
-[[bin]]
-name = "msg_accept_channel_target"
-path = "fuzz_targets/msg_targets/msg_accept_channel_target.rs"
-
-[[bin]]
-name = "msg_closing_signed_target"
-path = "fuzz_targets/msg_targets/msg_closing_signed_target.rs"
-
-[[bin]]
-name = "msg_commitment_signed_target"
-path = "fuzz_targets/msg_targets/msg_commitment_signed_target.rs"
-
-[[bin]]
-name = "msg_funding_created_target"
-path = "fuzz_targets/msg_targets/msg_funding_created_target.rs"
-
-[[bin]]
-name = "msg_funding_locked_target"
-path = "fuzz_targets/msg_targets/msg_funding_locked_target.rs"
-
-[[bin]]
-name = "msg_funding_signed_target"
-path = "fuzz_targets/msg_targets/msg_funding_signed_target.rs"
-
-[[bin]]
-name = "msg_open_channel_target"
-path = "fuzz_targets/msg_targets/msg_open_channel_target.rs"
-
-[[bin]]
-name = "msg_revoke_and_ack_target"
-path = "fuzz_targets/msg_targets/msg_revoke_and_ack_target.rs"
-
-[[bin]]
-name = "msg_shutdown_target"
-path = "fuzz_targets/msg_targets/msg_shutdown_target.rs"
-
-[[bin]]
-name = "msg_update_fail_malformed_htlc_target"
-path = "fuzz_targets/msg_targets/msg_update_fail_malformed_htlc_target.rs"
-
-[[bin]]
-name = "msg_update_fee_target"
-path = "fuzz_targets/msg_targets/msg_update_fee_target.rs"
-
-[[bin]]
-name = "msg_update_fulfill_htlc_target"
-path = "fuzz_targets/msg_targets/msg_update_fulfill_htlc_target.rs"
-
-[[bin]]
-name = "msg_update_fail_htlc_target"
-path = "fuzz_targets/msg_targets/msg_update_fail_htlc_target.rs"
-
-[[bin]]
-name = "msg_channel_reestablish_target"
-path = "fuzz_targets/msg_targets/msg_channel_reestablish_target.rs"
-
-[[bin]]
-name = "msg_announcement_signatures_target"
-path = "fuzz_targets/msg_targets/msg_announcement_signatures_target.rs"
-
-[[bin]]
-name = "msg_channel_announcement_target"
-path = "fuzz_targets/msg_targets/msg_channel_announcement_target.rs"
-
-[[bin]]
-name = "msg_channel_update_target"
-path = "fuzz_targets/msg_targets/msg_channel_update_target.rs"
-
-[[bin]]
-name = "msg_decoded_onion_error_packet_target"
-path = "fuzz_targets/msg_targets/msg_decoded_onion_error_packet_target.rs"
-
-[[bin]]
-name = "msg_init_target"
-path = "fuzz_targets/msg_targets/msg_init_target.rs"
-
-[[bin]]
-name = "msg_node_announcement_target"
-path = "fuzz_targets/msg_targets/msg_node_announcement_target.rs"
-
-[[bin]]
-name = "msg_onion_hop_data_target"
-path = "fuzz_targets/msg_targets/msg_onion_hop_data_target.rs"
diff --git a/fuzz/fuzz_targets/chanmon_deser_target.rs b/fuzz/fuzz_targets/chanmon_deser_target.rs
deleted file mode 100644 (file)
index f741832..0000000
+++ /dev/null
@@ -1,72 +0,0 @@
-// This file is auto-generated by gen_target.sh based on msg_target_template.txt
-// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
-
-extern crate bitcoin;
-extern crate bitcoin_hashes;
-extern crate lightning;
-
-use bitcoin_hashes::sha256d::Hash as Sha256dHash;
-
-use lightning::ln::channelmonitor;
-use lightning::util::ser::{ReadableArgs, Writer};
-
-mod utils;
-use utils::test_logger;
-
-use std::io::Cursor;
-use std::sync::Arc;
-
-struct VecWriter(Vec<u8>);
-impl Writer for VecWriter {
-       fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
-               self.0.extend_from_slice(buf);
-               Ok(())
-       }
-       fn size_hint(&mut self, size: usize) {
-               self.0.reserve_exact(size);
-       }
-}
-
-#[inline]
-pub fn do_test(data: &[u8]) {
-       let logger = Arc::new(test_logger::TestLogger::new("".to_owned()));
-       if let Ok((latest_block_hash, monitor)) = <(Sha256dHash, channelmonitor::ChannelMonitor)>::read(&mut Cursor::new(data), logger.clone()) {
-               let mut w = VecWriter(Vec::new());
-               monitor.write_for_disk(&mut w).unwrap();
-               let deserialized_copy = <(Sha256dHash, channelmonitor::ChannelMonitor)>::read(&mut Cursor::new(&w.0), logger.clone()).unwrap();
-               assert!(latest_block_hash == deserialized_copy.0);
-               assert!(monitor == deserialized_copy.1);
-               w.0.clear();
-               monitor.write_for_watchtower(&mut w).unwrap();
-       }
-}
-
-#[cfg(feature = "afl")]
-#[macro_use] extern crate afl;
-#[cfg(feature = "afl")]
-fn main() {
-       fuzz!(|data| {
-               do_test(data);
-       });
-}
-
-#[cfg(feature = "honggfuzz")]
-#[macro_use] extern crate honggfuzz;
-#[cfg(feature = "honggfuzz")]
-fn main() {
-       loop {
-               fuzz!(|data| {
-                       do_test(data);
-               });
-       }
-}
-
-extern crate hex;
-#[cfg(test)]
-mod tests {
-
-       #[test]
-       fn duplicate_crash() {
-               super::do_test(&::hex::decode("00").unwrap());
-       }
-}
diff --git a/fuzz/fuzz_targets/chanmon_fail_consistency.rs b/fuzz/fuzz_targets/chanmon_fail_consistency.rs
deleted file mode 100644 (file)
index 3610b1c..0000000
+++ /dev/null
@@ -1,783 +0,0 @@
-//! Test that monitor update failures don't get our channel state out of sync.
-//! One of the biggest concern with the monitor update failure handling code is that messages
-//! resent after monitor updating is restored are delivered out-of-order, resulting in
-//! commitment_signed messages having "invalid signatures".
-//! To test this we stand up a network of three nodes and read bytes from the fuzz input to denote
-//! actions such as sending payments, handling events, or changing monitor update return values on
-//! a per-node basis. This should allow it to find any cases where the ordering of actions results
-//! in us getting out of sync with ourselves, and, assuming at least one of our recieve- or
-//! send-side handling is correct, other peers. We consider it a failure if any action results in a
-//! channel being force-closed.
-
-//Uncomment this for libfuzzer builds:
-//#![no_main]
-
-extern crate bitcoin;
-extern crate bitcoin_hashes;
-extern crate lightning;
-extern crate secp256k1;
-
-use bitcoin::BitcoinHash;
-use bitcoin::blockdata::block::BlockHeader;
-use bitcoin::blockdata::transaction::{Transaction, TxOut};
-use bitcoin::blockdata::script::{Builder, Script};
-use bitcoin::blockdata::opcodes;
-use bitcoin::network::constants::Network;
-
-use bitcoin_hashes::Hash as TraitImport;
-use bitcoin_hashes::hash160::Hash as Hash160;
-use bitcoin_hashes::sha256::Hash as Sha256;
-use bitcoin_hashes::sha256d::Hash as Sha256d;
-
-use lightning::chain::chaininterface;
-use lightning::chain::transaction::OutPoint;
-use lightning::chain::chaininterface::{BroadcasterInterface,ConfirmationTarget,ChainListener,FeeEstimator,ChainWatchInterfaceUtil};
-use lightning::chain::keysinterface::{ChannelKeys, KeysInterface};
-use lightning::ln::channelmonitor;
-use lightning::ln::channelmonitor::{ChannelMonitor, ChannelMonitorUpdateErr, HTLCUpdate};
-use lightning::ln::channelmanager::{ChannelManager, PaymentHash, PaymentPreimage, ChannelManagerReadArgs};
-use lightning::ln::router::{Route, RouteHop};
-use lightning::ln::msgs::{CommitmentUpdate, ChannelMessageHandler, ErrorAction, HandleError, UpdateAddHTLC, LocalFeatures};
-use lightning::util::events;
-use lightning::util::logger::Logger;
-use lightning::util::config::UserConfig;
-use lightning::util::events::{EventsProvider, MessageSendEventsProvider};
-use lightning::util::ser::{Readable, ReadableArgs, Writeable, Writer};
-
-mod utils;
-use utils::test_logger;
-
-use secp256k1::key::{PublicKey,SecretKey};
-use secp256k1::Secp256k1;
-
-use std::mem;
-use std::cmp::Ordering;
-use std::collections::{HashSet, hash_map, HashMap};
-use std::sync::{Arc,Mutex};
-use std::sync::atomic;
-use std::io::Cursor;
-
-struct FuzzEstimator {}
-impl FeeEstimator for FuzzEstimator {
-       fn get_est_sat_per_1000_weight(&self, _: ConfirmationTarget) -> u64 {
-               253
-       }
-}
-
-pub struct TestBroadcaster {}
-impl BroadcasterInterface for TestBroadcaster {
-       fn broadcast_transaction(&self, _tx: &Transaction) { }
-}
-
-pub struct VecWriter(pub Vec<u8>);
-impl Writer for VecWriter {
-       fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
-               self.0.extend_from_slice(buf);
-               Ok(())
-       }
-       fn size_hint(&mut self, size: usize) {
-               self.0.reserve_exact(size);
-       }
-}
-
-static mut IN_RESTORE: bool = false;
-pub struct TestChannelMonitor {
-       pub simple_monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint>>,
-       pub update_ret: Mutex<Result<(), channelmonitor::ChannelMonitorUpdateErr>>,
-       pub latest_good_update: Mutex<HashMap<OutPoint, Vec<u8>>>,
-       pub latest_update_good: Mutex<HashMap<OutPoint, bool>>,
-       pub latest_updates_good_at_last_ser: Mutex<HashMap<OutPoint, bool>>,
-       pub should_update_manager: atomic::AtomicBool,
-}
-impl TestChannelMonitor {
-       pub fn new(chain_monitor: Arc<chaininterface::ChainWatchInterface>, broadcaster: Arc<chaininterface::BroadcasterInterface>, logger: Arc<Logger>, feeest: Arc<chaininterface::FeeEstimator>) -> Self {
-               Self {
-                       simple_monitor: channelmonitor::SimpleManyChannelMonitor::new(chain_monitor, broadcaster, logger, feeest),
-                       update_ret: Mutex::new(Ok(())),
-                       latest_good_update: Mutex::new(HashMap::new()),
-                       latest_update_good: Mutex::new(HashMap::new()),
-                       latest_updates_good_at_last_ser: Mutex::new(HashMap::new()),
-                       should_update_manager: atomic::AtomicBool::new(false),
-               }
-       }
-}
-impl channelmonitor::ManyChannelMonitor for TestChannelMonitor {
-       fn add_update_monitor(&self, funding_txo: OutPoint, monitor: channelmonitor::ChannelMonitor) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> {
-               let ret = self.update_ret.lock().unwrap().clone();
-               if let Ok(()) = ret {
-                       let mut ser = VecWriter(Vec::new());
-                       monitor.write_for_disk(&mut ser).unwrap();
-                       self.latest_good_update.lock().unwrap().insert(funding_txo, ser.0);
-                       match self.latest_update_good.lock().unwrap().entry(funding_txo) {
-                               hash_map::Entry::Vacant(mut e) => { e.insert(true); },
-                               hash_map::Entry::Occupied(mut e) => {
-                                       if !e.get() && unsafe { IN_RESTORE } {
-                                               // Technically we can't consider an update to be "good" unless we're doing
-                                               // it in response to a test_restore_channel_monitor as the channel may
-                                               // still be waiting on such a call, so only set us to good if we're in the
-                                               // middle of a restore call.
-                                               e.insert(true);
-                                       }
-                               },
-                       }
-                       self.should_update_manager.store(true, atomic::Ordering::Relaxed);
-               } else {
-                       self.latest_update_good.lock().unwrap().insert(funding_txo, false);
-               }
-               assert!(self.simple_monitor.add_update_monitor(funding_txo, monitor).is_ok());
-               ret
-       }
-
-       fn fetch_pending_htlc_updated(&self) -> Vec<HTLCUpdate> {
-               return self.simple_monitor.fetch_pending_htlc_updated();
-       }
-}
-
-struct KeyProvider {
-       node_id: u8,
-       session_id: atomic::AtomicU8,
-       channel_id: atomic::AtomicU8,
-}
-impl KeysInterface for KeyProvider {
-       fn get_node_secret(&self) -> SecretKey {
-               SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, self.node_id]).unwrap()
-       }
-
-       fn get_destination_script(&self) -> Script {
-               let secp_ctx = Secp256k1::signing_only();
-               let channel_monitor_claim_key = SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, self.node_id]).unwrap();
-               let our_channel_monitor_claim_key_hash = Hash160::hash(&PublicKey::from_secret_key(&secp_ctx, &channel_monitor_claim_key).serialize());
-               Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(&our_channel_monitor_claim_key_hash[..]).into_script()
-       }
-
-       fn get_shutdown_pubkey(&self) -> PublicKey {
-               let secp_ctx = Secp256k1::signing_only();
-               PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, self.node_id]).unwrap())
-       }
-
-       fn get_channel_keys(&self, _inbound: bool) -> ChannelKeys {
-               ChannelKeys {
-                       funding_key:               SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, self.node_id]).unwrap(),
-                       revocation_base_key:       SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, self.node_id]).unwrap(),
-                       payment_base_key:          SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, self.node_id]).unwrap(),
-                       delayed_payment_base_key:  SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, self.node_id]).unwrap(),
-                       htlc_base_key:             SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, self.node_id]).unwrap(),
-                       commitment_seed: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, self.node_id],
-               }
-       }
-
-       fn get_session_key(&self) -> SecretKey {
-               let id = self.session_id.fetch_add(1, atomic::Ordering::Relaxed);
-               SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, id, 10, self.node_id]).unwrap()
-       }
-
-       fn get_channel_id(&self) -> [u8; 32] {
-               let id = self.channel_id.fetch_add(1, atomic::Ordering::Relaxed);
-               [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, id, 11, self.node_id]
-       }
-}
-
-#[inline]
-pub fn do_test(data: &[u8]) {
-       let fee_est = Arc::new(FuzzEstimator{});
-       let broadcast = Arc::new(TestBroadcaster{});
-
-       macro_rules! make_node {
-               ($node_id: expr) => { {
-                       let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new($node_id.to_string()));
-                       let watch = Arc::new(ChainWatchInterfaceUtil::new(Network::Bitcoin, Arc::clone(&logger)));
-                       let monitor = Arc::new(TestChannelMonitor::new(watch.clone(), broadcast.clone(), logger.clone(), fee_est.clone()));
-
-                       let keys_manager = Arc::new(KeyProvider { node_id: $node_id, session_id: atomic::AtomicU8::new(0), channel_id: atomic::AtomicU8::new(0) });
-                       let mut config = UserConfig::new();
-                       config.channel_options.fee_proportional_millionths = 0;
-                       config.channel_options.announced_channel = true;
-                       config.peer_channel_config_limits.min_dust_limit_satoshis = 0;
-                       (ChannelManager::new(Network::Bitcoin, fee_est.clone(), monitor.clone(), watch.clone(), broadcast.clone(), Arc::clone(&logger), keys_manager.clone(), config).unwrap(),
-                       monitor)
-               } }
-       }
-
-       macro_rules! reload_node {
-               ($ser: expr, $node_id: expr, $old_monitors: expr) => { {
-                       let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new($node_id.to_string()));
-                       let watch = Arc::new(ChainWatchInterfaceUtil::new(Network::Bitcoin, Arc::clone(&logger)));
-                       let monitor = Arc::new(TestChannelMonitor::new(watch.clone(), broadcast.clone(), logger.clone(), fee_est.clone()));
-
-                       let keys_manager = Arc::new(KeyProvider { node_id: $node_id, session_id: atomic::AtomicU8::new(0), channel_id: atomic::AtomicU8::new(0) });
-                       let mut config = UserConfig::new();
-                       config.channel_options.fee_proportional_millionths = 0;
-                       config.channel_options.announced_channel = true;
-                       config.peer_channel_config_limits.min_dust_limit_satoshis = 0;
-
-                       let mut monitors = HashMap::new();
-                       let mut old_monitors = $old_monitors.latest_good_update.lock().unwrap();
-                       for (outpoint, monitor_ser) in old_monitors.drain() {
-                               monitors.insert(outpoint, <(Sha256d, ChannelMonitor)>::read(&mut Cursor::new(&monitor_ser), Arc::clone(&logger)).expect("Failed to read monitor").1);
-                               monitor.latest_good_update.lock().unwrap().insert(outpoint, monitor_ser);
-                       }
-                       let mut monitor_refs = HashMap::new();
-                       for (outpoint, monitor) in monitors.iter() {
-                               monitor_refs.insert(*outpoint, monitor);
-                       }
-
-                       let read_args = ChannelManagerReadArgs {
-                               keys_manager,
-                               fee_estimator: fee_est.clone(),
-                               monitor: monitor.clone(),
-                               chain_monitor: watch,
-                               tx_broadcaster: broadcast.clone(),
-                               logger,
-                               default_config: config,
-                               channel_monitors: &monitor_refs,
-                       };
-
-                       let res = (<(Sha256d, ChannelManager)>::read(&mut Cursor::new(&$ser.0), read_args).expect("Failed to read manager").1, monitor);
-                       for (_, was_good) in $old_monitors.latest_updates_good_at_last_ser.lock().unwrap().iter() {
-                               if !was_good {
-                                       // If the last time we updated a monitor we didn't successfully update (and we
-                                       // have sense updated our serialized copy of the ChannelManager) we may
-                                       // force-close the channel on our counterparty cause we know we're missing
-                                       // something. Thus, we just return here since we can't continue to test.
-                                       return;
-                               }
-                       }
-                       res
-               } }
-       }
-
-
-       let mut channel_txn = Vec::new();
-       macro_rules! make_channel {
-               ($source: expr, $dest: expr, $chan_id: expr) => { {
-                       $source.create_channel($dest.get_our_node_id(), 10000000, 42, 0).unwrap();
-                       let open_channel = {
-                               let events = $source.get_and_clear_pending_msg_events();
-                               assert_eq!(events.len(), 1);
-                               if let events::MessageSendEvent::SendOpenChannel { ref msg, .. } = events[0] {
-                                       msg.clone()
-                               } else { panic!("Wrong event type"); }
-                       };
-
-                       $dest.handle_open_channel(&$source.get_our_node_id(), LocalFeatures::new(), &open_channel).unwrap();
-                       let accept_channel = {
-                               let events = $dest.get_and_clear_pending_msg_events();
-                               assert_eq!(events.len(), 1);
-                               if let events::MessageSendEvent::SendAcceptChannel { ref msg, .. } = events[0] {
-                                       msg.clone()
-                               } else { panic!("Wrong event type"); }
-                       };
-
-                       $source.handle_accept_channel(&$dest.get_our_node_id(), LocalFeatures::new(), &accept_channel).unwrap();
-                       {
-                               let events = $source.get_and_clear_pending_events();
-                               assert_eq!(events.len(), 1);
-                               if let events::Event::FundingGenerationReady { ref temporary_channel_id, ref channel_value_satoshis, ref output_script, .. } = events[0] {
-                                       let tx = Transaction { version: $chan_id, lock_time: 0, input: Vec::new(), output: vec![TxOut {
-                                               value: *channel_value_satoshis, script_pubkey: output_script.clone(),
-                                       }]};
-                                       let funding_output = OutPoint::new(tx.txid(), 0);
-                                       $source.funding_transaction_generated(&temporary_channel_id, funding_output);
-                                       channel_txn.push(tx);
-                               } else { panic!("Wrong event type"); }
-                       }
-
-                       let funding_created = {
-                               let events = $source.get_and_clear_pending_msg_events();
-                               assert_eq!(events.len(), 1);
-                               if let events::MessageSendEvent::SendFundingCreated { ref msg, .. } = events[0] {
-                                       msg.clone()
-                               } else { panic!("Wrong event type"); }
-                       };
-                       $dest.handle_funding_created(&$source.get_our_node_id(), &funding_created).unwrap();
-
-                       let funding_signed = {
-                               let events = $dest.get_and_clear_pending_msg_events();
-                               assert_eq!(events.len(), 1);
-                               if let events::MessageSendEvent::SendFundingSigned { ref msg, .. } = events[0] {
-                                       msg.clone()
-                               } else { panic!("Wrong event type"); }
-                       };
-                       $source.handle_funding_signed(&$dest.get_our_node_id(), &funding_signed).unwrap();
-
-                       {
-                               let events = $source.get_and_clear_pending_events();
-                               assert_eq!(events.len(), 1);
-                               if let events::Event::FundingBroadcastSafe { .. } = events[0] {
-                               } else { panic!("Wrong event type"); }
-                       }
-               } }
-       }
-
-       macro_rules! confirm_txn {
-               ($node: expr) => { {
-                       let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-                       let mut txn = Vec::with_capacity(channel_txn.len());
-                       let mut posn = Vec::with_capacity(channel_txn.len());
-                       for i in 0..channel_txn.len() {
-                               txn.push(&channel_txn[i]);
-                               posn.push(i as u32 + 1);
-                       }
-                       $node.block_connected(&header, 1, &txn, &posn);
-                       for i in 2..100 {
-                               header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-                               $node.block_connected(&header, i, &Vec::new(), &[0; 0]);
-                       }
-               } }
-       }
-
-       macro_rules! lock_fundings {
-               ($nodes: expr) => { {
-                       let mut node_events = Vec::new();
-                       for node in $nodes.iter() {
-                               node_events.push(node.get_and_clear_pending_msg_events());
-                       }
-                       for (idx, node_event) in node_events.iter().enumerate() {
-                               for event in node_event {
-                                       if let events::MessageSendEvent::SendFundingLocked { ref node_id, ref msg } = event {
-                                               for node in $nodes.iter() {
-                                                       if node.get_our_node_id() == *node_id {
-                                                               node.handle_funding_locked(&$nodes[idx].get_our_node_id(), msg).unwrap();
-                                                       }
-                                               }
-                                       } else { panic!("Wrong event type"); }
-                               }
-                       }
-
-                       for node in $nodes.iter() {
-                               let events = node.get_and_clear_pending_msg_events();
-                               for event in events {
-                                       if let events::MessageSendEvent::SendAnnouncementSignatures { .. } = event {
-                                       } else { panic!("Wrong event type"); }
-                               }
-                       }
-               } }
-       }
-
-       // 3 nodes is enough to hit all the possible cases, notably unknown-source-unknown-dest
-       // forwarding.
-       let (mut node_a, mut monitor_a) = make_node!(0);
-       let (mut node_b, mut monitor_b) = make_node!(1);
-       let (mut node_c, mut monitor_c) = make_node!(2);
-
-       let mut nodes = [node_a, node_b, node_c];
-
-       make_channel!(nodes[0], nodes[1], 0);
-       make_channel!(nodes[1], nodes[2], 1);
-
-       for node in nodes.iter() {
-               confirm_txn!(node);
-       }
-
-       lock_fundings!(nodes);
-
-       let chan_a = nodes[0].list_usable_channels()[0].short_channel_id.unwrap();
-       let chan_b = nodes[2].list_usable_channels()[0].short_channel_id.unwrap();
-
-       let mut payment_id = 0;
-
-       let mut chan_a_disconnected = false;
-       let mut chan_b_disconnected = false;
-       let mut ba_events = Vec::new();
-       let mut bc_events = Vec::new();
-
-       let mut node_a_ser = VecWriter(Vec::new());
-       nodes[0].write(&mut node_a_ser).unwrap();
-       let mut node_b_ser = VecWriter(Vec::new());
-       nodes[1].write(&mut node_b_ser).unwrap();
-       let mut node_c_ser = VecWriter(Vec::new());
-       nodes[2].write(&mut node_c_ser).unwrap();
-
-       macro_rules! test_err {
-               ($res: expr) => {
-                       match $res {
-                               Ok(()) => {},
-                               Err(HandleError { action: Some(ErrorAction::IgnoreError), .. }) => { },
-                               _ => { $res.unwrap() },
-                       }
-               }
-       }
-
-       macro_rules! test_return {
-               () => { {
-                       assert_eq!(nodes[0].list_channels().len(), 1);
-                       assert_eq!(nodes[1].list_channels().len(), 2);
-                       assert_eq!(nodes[2].list_channels().len(), 1);
-                       return;
-               } }
-       }
-
-       let mut read_pos = 0;
-       macro_rules! get_slice {
-               ($len: expr) => {
-                       {
-                               let slice_len = $len as usize;
-                               if data.len() < read_pos + slice_len {
-                                       test_return!();
-                               }
-                               read_pos += slice_len;
-                               &data[read_pos - slice_len..read_pos]
-                       }
-               }
-       }
-
-       loop {
-               macro_rules! send_payment {
-                       ($source: expr, $dest: expr) => { {
-                               let payment_hash = Sha256::hash(&[payment_id; 1]);
-                               payment_id = payment_id.wrapping_add(1);
-                               if let Err(_) = $source.send_payment(Route {
-                                       hops: vec![RouteHop {
-                                               pubkey: $dest.0.get_our_node_id(),
-                                               short_channel_id: $dest.1,
-                                               fee_msat: 5000000,
-                                               cltv_expiry_delta: 200,
-                                       }],
-                               }, PaymentHash(payment_hash.into_inner())) {
-                                       // Probably ran out of funds
-                                       test_return!();
-                               }
-                       } };
-                       ($source: expr, $middle: expr, $dest: expr) => { {
-                               let payment_hash = Sha256::hash(&[payment_id; 1]);
-                               payment_id = payment_id.wrapping_add(1);
-                               if let Err(_) = $source.send_payment(Route {
-                                       hops: vec![RouteHop {
-                                               pubkey: $middle.0.get_our_node_id(),
-                                               short_channel_id: $middle.1,
-                                               fee_msat: 50000,
-                                               cltv_expiry_delta: 100,
-                                       },RouteHop {
-                                               pubkey: $dest.0.get_our_node_id(),
-                                               short_channel_id: $dest.1,
-                                               fee_msat: 5000000,
-                                               cltv_expiry_delta: 200,
-                                       }],
-                               }, PaymentHash(payment_hash.into_inner())) {
-                                       // Probably ran out of funds
-                                       test_return!();
-                               }
-                       } }
-               }
-
-               macro_rules! process_msg_events {
-                       ($node: expr, $corrupt_forward: expr) => { {
-                               let events = if $node == 1 {
-                                       let mut new_events = Vec::new();
-                                       mem::swap(&mut new_events, &mut ba_events);
-                                       new_events.extend_from_slice(&bc_events[..]);
-                                       bc_events.clear();
-                                       new_events
-                               } else { Vec::new() };
-                               for event in events.iter().chain(nodes[$node].get_and_clear_pending_msg_events().iter()) {
-                                       match event {
-                                               events::MessageSendEvent::UpdateHTLCs { ref node_id, updates: CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, ref update_fulfill_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => {
-                                                       for dest in nodes.iter() {
-                                                               if dest.get_our_node_id() == *node_id {
-                                                                       assert!(update_fee.is_none());
-                                                                       for update_add in update_add_htlcs {
-                                                                               if !$corrupt_forward {
-                                                                                       test_err!(dest.handle_update_add_htlc(&nodes[$node].get_our_node_id(), &update_add));
-                                                                               } else {
-                                                                                       // Corrupt the update_add_htlc message so that its HMAC
-                                                                                       // check will fail and we generate a
-                                                                                       // update_fail_malformed_htlc instead of an
-                                                                                       // update_fail_htlc as we do when we reject a payment.
-                                                                                       let mut msg_ser = update_add.encode();
-                                                                                       msg_ser[1000] ^= 0xff;
-                                                                                       let new_msg = UpdateAddHTLC::read(&mut Cursor::new(&msg_ser)).unwrap();
-                                                                                       test_err!(dest.handle_update_add_htlc(&nodes[$node].get_our_node_id(), &new_msg));
-                                                                               }
-                                                                       }
-                                                                       for update_fulfill in update_fulfill_htlcs {
-                                                                               test_err!(dest.handle_update_fulfill_htlc(&nodes[$node].get_our_node_id(), &update_fulfill));
-                                                                       }
-                                                                       for update_fail in update_fail_htlcs {
-                                                                               test_err!(dest.handle_update_fail_htlc(&nodes[$node].get_our_node_id(), &update_fail));
-                                                                       }
-                                                                       for update_fail_malformed in update_fail_malformed_htlcs {
-                                                                               test_err!(dest.handle_update_fail_malformed_htlc(&nodes[$node].get_our_node_id(), &update_fail_malformed));
-                                                                       }
-                                                                       test_err!(dest.handle_commitment_signed(&nodes[$node].get_our_node_id(), &commitment_signed));
-                                                               }
-                                                       }
-                                               },
-                                               events::MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
-                                                       for dest in nodes.iter() {
-                                                               if dest.get_our_node_id() == *node_id {
-                                                                       test_err!(dest.handle_revoke_and_ack(&nodes[$node].get_our_node_id(), msg));
-                                                               }
-                                                       }
-                                               },
-                                               events::MessageSendEvent::SendChannelReestablish { ref node_id, ref msg } => {
-                                                       for dest in nodes.iter() {
-                                                               if dest.get_our_node_id() == *node_id {
-                                                                       test_err!(dest.handle_channel_reestablish(&nodes[$node].get_our_node_id(), msg));
-                                                               }
-                                                       }
-                                               },
-                                               events::MessageSendEvent::SendFundingLocked { .. } => {
-                                                       // Can be generated as a reestablish response
-                                               },
-                                               events::MessageSendEvent::PaymentFailureNetworkUpdate { .. } => {
-                                                       // Can be generated due to a payment forward being rejected due to a
-                                                       // channel having previously failed a monitor update
-                                               },
-                                               _ => panic!("Unhandled message event"),
-                                       }
-                               }
-                       } }
-               }
-
-               macro_rules! drain_msg_events_on_disconnect {
-                       ($counterparty_id: expr) => { {
-                               if $counterparty_id == 0 {
-                                       for event in nodes[0].get_and_clear_pending_msg_events() {
-                                               match event {
-                                                       events::MessageSendEvent::UpdateHTLCs { .. } => {},
-                                                       events::MessageSendEvent::SendRevokeAndACK { .. } => {},
-                                                       events::MessageSendEvent::SendChannelReestablish { .. } => {},
-                                                       events::MessageSendEvent::SendFundingLocked { .. } => {},
-                                                       events::MessageSendEvent::PaymentFailureNetworkUpdate { .. } => {},
-                                                       _ => panic!("Unhandled message event"),
-                                               }
-                                       }
-                                       ba_events.clear();
-                               } else {
-                                       for event in nodes[2].get_and_clear_pending_msg_events() {
-                                               match event {
-                                                       events::MessageSendEvent::UpdateHTLCs { .. } => {},
-                                                       events::MessageSendEvent::SendRevokeAndACK { .. } => {},
-                                                       events::MessageSendEvent::SendChannelReestablish { .. } => {},
-                                                       events::MessageSendEvent::SendFundingLocked { .. } => {},
-                                                       events::MessageSendEvent::PaymentFailureNetworkUpdate { .. } => {},
-                                                       _ => panic!("Unhandled message event"),
-                                               }
-                                       }
-                                       bc_events.clear();
-                               }
-                               let mut events = nodes[1].get_and_clear_pending_msg_events();
-                               let drop_node_id = if $counterparty_id == 0 { nodes[0].get_our_node_id() } else { nodes[2].get_our_node_id() };
-                               let msg_sink = if $counterparty_id == 0 { &mut bc_events } else { &mut ba_events };
-                               for event in events.drain(..) {
-                                       let push = match event {
-                                               events::MessageSendEvent::UpdateHTLCs { ref node_id, .. } => {
-                                                       if *node_id != drop_node_id { true } else { false }
-                                               },
-                                               events::MessageSendEvent::SendRevokeAndACK { ref node_id, .. } => {
-                                                       if *node_id != drop_node_id { true } else { false }
-                                               },
-                                               events::MessageSendEvent::SendChannelReestablish { ref node_id, .. } => {
-                                                       if *node_id != drop_node_id { true } else { false }
-                                               },
-                                               events::MessageSendEvent::SendFundingLocked { .. } => false,
-                                               events::MessageSendEvent::PaymentFailureNetworkUpdate { .. } => false,
-                                               _ => panic!("Unhandled message event"),
-                                       };
-                                       if push { msg_sink.push(event); }
-                               }
-                       } }
-               }
-
-               macro_rules! process_events {
-                       ($node: expr, $fail: expr) => { {
-                               // In case we get 256 payments we may have a hash collision, resulting in the
-                               // second claim/fail call not finding the duplicate-hash HTLC, so we have to
-                               // deduplicate the calls here.
-                               let mut claim_set = HashSet::new();
-                               let mut events = nodes[$node].get_and_clear_pending_events();
-                               // Sort events so that PendingHTLCsForwardable get processed last. This avoids a
-                               // case where we first process a PendingHTLCsForwardable, then claim/fail on a
-                               // PaymentReceived, claiming/failing two HTLCs, but leaving a just-generated
-                               // PaymentReceived event for the second HTLC in our pending_events (and breaking
-                               // our claim_set deduplication).
-                               events.sort_by(|a, b| {
-                                       if let events::Event::PaymentReceived { .. } = a {
-                                               if let events::Event::PendingHTLCsForwardable { .. } = b {
-                                                       Ordering::Less
-                                               } else { Ordering::Equal }
-                                       } else if let events::Event::PendingHTLCsForwardable { .. } = a {
-                                               if let events::Event::PaymentReceived { .. } = b {
-                                                       Ordering::Greater
-                                               } else { Ordering::Equal }
-                                       } else { Ordering::Equal }
-                               });
-                               for event in events.drain(..) {
-                                       match event {
-                                               events::Event::PaymentReceived { payment_hash, .. } => {
-                                                       if claim_set.insert(payment_hash.0) {
-                                                               if $fail {
-                                                                       assert!(nodes[$node].fail_htlc_backwards(&payment_hash));
-                                                               } else {
-                                                                       assert!(nodes[$node].claim_funds(PaymentPreimage(payment_hash.0)));
-                                                               }
-                                                       }
-                                               },
-                                               events::Event::PaymentSent { .. } => {},
-                                               events::Event::PaymentFailed { .. } => {},
-                                               events::Event::PendingHTLCsForwardable { .. } => {
-                                                       nodes[$node].process_pending_htlc_forwards();
-                                               },
-                                               _ => panic!("Unhandled event"),
-                                       }
-                               }
-                       } }
-               }
-
-               match get_slice!(1)[0] {
-                       0x00 => *monitor_a.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure),
-                       0x01 => *monitor_b.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure),
-                       0x02 => *monitor_c.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure),
-                       0x03 => *monitor_a.update_ret.lock().unwrap() = Ok(()),
-                       0x04 => *monitor_b.update_ret.lock().unwrap() = Ok(()),
-                       0x05 => *monitor_c.update_ret.lock().unwrap() = Ok(()),
-                       0x06 => { unsafe { IN_RESTORE = true }; nodes[0].test_restore_channel_monitor(); unsafe { IN_RESTORE = false }; },
-                       0x07 => { unsafe { IN_RESTORE = true }; nodes[1].test_restore_channel_monitor(); unsafe { IN_RESTORE = false }; },
-                       0x08 => { unsafe { IN_RESTORE = true }; nodes[2].test_restore_channel_monitor(); unsafe { IN_RESTORE = false }; },
-                       0x09 => send_payment!(nodes[0], (&nodes[1], chan_a)),
-                       0x0a => send_payment!(nodes[1], (&nodes[0], chan_a)),
-                       0x0b => send_payment!(nodes[1], (&nodes[2], chan_b)),
-                       0x0c => send_payment!(nodes[2], (&nodes[1], chan_b)),
-                       0x0d => send_payment!(nodes[0], (&nodes[1], chan_a), (&nodes[2], chan_b)),
-                       0x0e => send_payment!(nodes[2], (&nodes[1], chan_b), (&nodes[0], chan_a)),
-                       0x0f => {
-                               if !chan_a_disconnected {
-                                       nodes[0].peer_disconnected(&nodes[1].get_our_node_id(), false);
-                                       nodes[1].peer_disconnected(&nodes[0].get_our_node_id(), false);
-                                       chan_a_disconnected = true;
-                                       drain_msg_events_on_disconnect!(0);
-                               }
-                       },
-                       0x10 => {
-                               if !chan_b_disconnected {
-                                       nodes[1].peer_disconnected(&nodes[2].get_our_node_id(), false);
-                                       nodes[2].peer_disconnected(&nodes[1].get_our_node_id(), false);
-                                       chan_b_disconnected = true;
-                                       drain_msg_events_on_disconnect!(2);
-                               }
-                       },
-                       0x11 => {
-                               if chan_a_disconnected {
-                                       nodes[0].peer_connected(&nodes[1].get_our_node_id());
-                                       nodes[1].peer_connected(&nodes[0].get_our_node_id());
-                                       chan_a_disconnected = false;
-                               }
-                       },
-                       0x12 => {
-                               if chan_b_disconnected {
-                                       nodes[1].peer_connected(&nodes[2].get_our_node_id());
-                                       nodes[2].peer_connected(&nodes[1].get_our_node_id());
-                                       chan_b_disconnected = false;
-                               }
-                       },
-                       0x13 => process_msg_events!(0, true),
-                       0x14 => process_msg_events!(0, false),
-                       0x15 => process_events!(0, true),
-                       0x16 => process_events!(0, false),
-                       0x17 => process_msg_events!(1, true),
-                       0x18 => process_msg_events!(1, false),
-                       0x19 => process_events!(1, true),
-                       0x1a => process_events!(1, false),
-                       0x1b => process_msg_events!(2, true),
-                       0x1c => process_msg_events!(2, false),
-                       0x1d => process_events!(2, true),
-                       0x1e => process_events!(2, false),
-                       0x1f => {
-                               if !chan_a_disconnected {
-                                       nodes[1].peer_disconnected(&nodes[0].get_our_node_id(), false);
-                                       chan_a_disconnected = true;
-                                       drain_msg_events_on_disconnect!(0);
-                               }
-                               let (new_node_a, new_monitor_a) = reload_node!(node_a_ser, 0, monitor_a);
-                               node_a = Arc::new(new_node_a);
-                               nodes[0] = node_a.clone();
-                               monitor_a = new_monitor_a;
-                       },
-                       0x20 => {
-                               if !chan_a_disconnected {
-                                       nodes[0].peer_disconnected(&nodes[1].get_our_node_id(), false);
-                                       chan_a_disconnected = true;
-                                       nodes[0].get_and_clear_pending_msg_events();
-                                       ba_events.clear();
-                               }
-                               if !chan_b_disconnected {
-                                       nodes[2].peer_disconnected(&nodes[1].get_our_node_id(), false);
-                                       chan_b_disconnected = true;
-                                       nodes[2].get_and_clear_pending_msg_events();
-                                       bc_events.clear();
-                               }
-                               let (new_node_b, new_monitor_b) = reload_node!(node_b_ser, 1, monitor_b);
-                               node_b = Arc::new(new_node_b);
-                               nodes[1] = node_b.clone();
-                               monitor_b = new_monitor_b;
-                       },
-                       0x21 => {
-                               if !chan_b_disconnected {
-                                       nodes[1].peer_disconnected(&nodes[2].get_our_node_id(), false);
-                                       chan_b_disconnected = true;
-                                       drain_msg_events_on_disconnect!(2);
-                               }
-                               let (new_node_c, new_monitor_c) = reload_node!(node_c_ser, 2, monitor_c);
-                               node_c = Arc::new(new_node_c);
-                               nodes[2] = node_c.clone();
-                               monitor_c = new_monitor_c;
-                       },
-                       _ => test_return!(),
-               }
-
-               if monitor_a.should_update_manager.load(atomic::Ordering::Relaxed) {
-                       node_a_ser.0.clear();
-                       nodes[0].write(&mut node_a_ser).unwrap();
-                       monitor_a.should_update_manager.store(false, atomic::Ordering::Relaxed);
-                       *monitor_a.latest_updates_good_at_last_ser.lock().unwrap() = monitor_a.latest_update_good.lock().unwrap().clone();
-               }
-               if monitor_b.should_update_manager.load(atomic::Ordering::Relaxed) {
-                       node_b_ser.0.clear();
-                       nodes[1].write(&mut node_b_ser).unwrap();
-                       monitor_b.should_update_manager.store(false, atomic::Ordering::Relaxed);
-                       *monitor_b.latest_updates_good_at_last_ser.lock().unwrap() = monitor_b.latest_update_good.lock().unwrap().clone();
-               }
-               if monitor_c.should_update_manager.load(atomic::Ordering::Relaxed) {
-                       node_c_ser.0.clear();
-                       nodes[2].write(&mut node_c_ser).unwrap();
-                       monitor_c.should_update_manager.store(false, atomic::Ordering::Relaxed);
-                       *monitor_c.latest_updates_good_at_last_ser.lock().unwrap() = monitor_c.latest_update_good.lock().unwrap().clone();
-               }
-       }
-}
-
-#[cfg(feature = "afl")]
-#[macro_use] extern crate afl;
-#[cfg(feature = "afl")]
-fn main() {
-       fuzz!(|data| {
-               do_test(data);
-       });
-}
-
-#[cfg(feature = "honggfuzz")]
-#[macro_use] extern crate honggfuzz;
-#[cfg(feature = "honggfuzz")]
-fn main() {
-       loop {
-               fuzz!(|data| {
-                       do_test(data);
-               });
-       }
-}
-
-#[cfg(feature = "libfuzzer_fuzz")]
-#[macro_use] extern crate libfuzzer_sys;
-#[cfg(feature = "libfuzzer_fuzz")]
-fuzz_target!(|data: &[u8]| {
-       do_test(data);
-});
-
-extern crate hex;
-#[cfg(test)]
-mod tests {
-       #[test]
-       fn duplicate_crash() {
-               super::do_test(&::hex::decode("00").unwrap());
-       }
-}
diff --git a/fuzz/fuzz_targets/full_stack_target.rs b/fuzz/fuzz_targets/full_stack_target.rs
deleted file mode 100644 (file)
index 6145d00..0000000
+++ /dev/null
@@ -1,890 +0,0 @@
-//! Test that no series of bytes received over the wire/connections created/payments sent can
-//! result in a crash. We do this by standing up a node and then reading bytes from input to denote
-//! actions such as creating new inbound/outbound connections, bytes to be read from a connection,
-//! or payments to send/ways to handle events generated.
-//! This test has been very useful, though due to its complexity good starting inputs are critical.
-
-//Uncomment this for libfuzzer builds:
-//#![no_main]
-
-extern crate bitcoin;
-extern crate bitcoin_hashes;
-extern crate lightning;
-extern crate secp256k1;
-
-use bitcoin::blockdata::block::BlockHeader;
-use bitcoin::blockdata::transaction::{Transaction, TxOut};
-use bitcoin::blockdata::script::{Builder, Script};
-use bitcoin::blockdata::opcodes;
-use bitcoin::consensus::encode::deserialize;
-use bitcoin::network::constants::Network;
-use bitcoin::util::hash::BitcoinHash;
-
-use bitcoin_hashes::Hash as TraitImport;
-use bitcoin_hashes::HashEngine as TraitImportEngine;
-use bitcoin_hashes::sha256::Hash as Sha256;
-use bitcoin_hashes::hash160::Hash as Hash160;
-use bitcoin_hashes::sha256d::Hash as Sha256dHash;
-
-use lightning::chain::chaininterface::{BroadcasterInterface,ConfirmationTarget,ChainListener,FeeEstimator,ChainWatchInterfaceUtil};
-use lightning::chain::transaction::OutPoint;
-use lightning::chain::keysinterface::{ChannelKeys, KeysInterface};
-use lightning::ln::channelmonitor;
-use lightning::ln::channelmanager::{ChannelManager, PaymentHash, PaymentPreimage};
-use lightning::ln::peer_handler::{MessageHandler,PeerManager,SocketDescriptor};
-use lightning::ln::router::Router;
-use lightning::util::events::{EventsProvider,Event};
-use lightning::util::logger::Logger;
-use lightning::util::config::UserConfig;
-
-mod utils;
-
-use utils::test_logger;
-
-use secp256k1::key::{PublicKey,SecretKey};
-use secp256k1::Secp256k1;
-
-use std::cell::RefCell;
-use std::collections::{HashMap, hash_map};
-use std::cmp;
-use std::hash::Hash;
-use std::sync::Arc;
-use std::sync::atomic::{AtomicU64,AtomicUsize,Ordering};
-
-#[inline]
-pub fn slice_to_be16(v: &[u8]) -> u16 {
-       ((v[0] as u16) << 8*1) |
-       ((v[1] as u16) << 8*0)
-}
-
-#[inline]
-pub fn slice_to_be24(v: &[u8]) -> u32 {
-       ((v[0] as u32) << 8*2) |
-       ((v[1] as u32) << 8*1) |
-       ((v[2] as u32) << 8*0)
-}
-
-#[inline]
-pub fn slice_to_be32(v: &[u8]) -> u32 {
-       ((v[0] as u32) << 8*3) |
-       ((v[1] as u32) << 8*2) |
-       ((v[2] as u32) << 8*1) |
-       ((v[3] as u32) << 8*0)
-}
-
-#[inline]
-pub fn be64_to_array(u: u64) -> [u8; 8] {
-       let mut v = [0; 8];
-       v[0] = ((u >> 8*7) & 0xff) as u8;
-       v[1] = ((u >> 8*6) & 0xff) as u8;
-       v[2] = ((u >> 8*5) & 0xff) as u8;
-       v[3] = ((u >> 8*4) & 0xff) as u8;
-       v[4] = ((u >> 8*3) & 0xff) as u8;
-       v[5] = ((u >> 8*2) & 0xff) as u8;
-       v[6] = ((u >> 8*1) & 0xff) as u8;
-       v[7] = ((u >> 8*0) & 0xff) as u8;
-       v
-}
-
-struct InputData {
-       data: Vec<u8>,
-       read_pos: AtomicUsize,
-}
-impl InputData {
-       fn get_slice(&self, len: usize) -> Option<&[u8]> {
-               let old_pos = self.read_pos.fetch_add(len, Ordering::AcqRel);
-               if self.data.len() < old_pos + len {
-                       return None;
-               }
-               Some(&self.data[old_pos..old_pos + len])
-       }
-}
-
-struct FuzzEstimator {
-       input: Arc<InputData>,
-}
-impl FeeEstimator for FuzzEstimator {
-       fn get_est_sat_per_1000_weight(&self, _: ConfirmationTarget) -> u64 {
-               //TODO: We should actually be testing at least much more than 64k...
-               match self.input.get_slice(2) {
-                       Some(slice) => cmp::max(slice_to_be16(slice) as u64, 253),
-                       None => 0
-               }
-       }
-}
-
-struct TestBroadcaster {}
-impl BroadcasterInterface for TestBroadcaster {
-       fn broadcast_transaction(&self, _tx: &Transaction) {}
-}
-
-#[derive(Clone)]
-struct Peer<'a> {
-       id: u8,
-       peers_connected: &'a RefCell<[bool; 256]>,
-}
-impl<'a> SocketDescriptor for Peer<'a> {
-       fn send_data(&mut self, data: &[u8], _resume_read: bool) -> usize {
-               data.len()
-       }
-       fn disconnect_socket(&mut self) {
-               assert!(self.peers_connected.borrow()[self.id as usize]);
-               self.peers_connected.borrow_mut()[self.id as usize] = false;
-       }
-}
-impl<'a> PartialEq for Peer<'a> {
-       fn eq(&self, other: &Self) -> bool {
-               self.id == other.id
-       }
-}
-impl<'a> Eq for Peer<'a> {}
-impl<'a> Hash for Peer<'a> {
-       fn hash<H : std::hash::Hasher>(&self, h: &mut H) {
-               self.id.hash(h)
-       }
-}
-
-struct MoneyLossDetector<'a> {
-       manager: Arc<ChannelManager>,
-       monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint>>,
-       handler: PeerManager<Peer<'a>>,
-
-       peers: &'a RefCell<[bool; 256]>,
-       funding_txn: Vec<Transaction>,
-       txids_confirmed: HashMap<Sha256dHash, usize>,
-       header_hashes: Vec<Sha256dHash>,
-       height: usize,
-       max_height: usize,
-       blocks_connected: u32,
-}
-impl<'a> MoneyLossDetector<'a> {
-       pub fn new(peers: &'a RefCell<[bool; 256]>, manager: Arc<ChannelManager>, monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint>>, handler: PeerManager<Peer<'a>>) -> Self {
-               MoneyLossDetector {
-                       manager,
-                       monitor,
-                       handler,
-
-                       peers,
-                       funding_txn: Vec::new(),
-                       txids_confirmed: HashMap::new(),
-                       header_hashes: vec![Default::default()],
-                       height: 0,
-                       max_height: 0,
-                       blocks_connected: 0,
-               }
-       }
-
-       fn connect_block(&mut self, all_txn: &[Transaction]) {
-               let mut txn = Vec::with_capacity(all_txn.len());
-               let mut txn_idxs = Vec::with_capacity(all_txn.len());
-               for (idx, tx) in all_txn.iter().enumerate() {
-                       let txid = tx.txid();
-                       match self.txids_confirmed.entry(txid) {
-                               hash_map::Entry::Vacant(e) => {
-                                       e.insert(self.height);
-                                       txn.push(tx);
-                                       txn_idxs.push(idx as u32 + 1);
-                               },
-                               _ => {},
-                       }
-               }
-
-               let header = BlockHeader { version: 0x20000000, prev_blockhash: self.header_hashes[self.height], merkle_root: Default::default(), time: self.blocks_connected, bits: 42, nonce: 42 };
-               self.height += 1;
-               self.blocks_connected += 1;
-               self.manager.block_connected(&header, self.height as u32, &txn[..], &txn_idxs[..]);
-               (*self.monitor).block_connected(&header, self.height as u32, &txn[..], &txn_idxs[..]);
-               if self.header_hashes.len() > self.height {
-                       self.header_hashes[self.height] = header.bitcoin_hash();
-               } else {
-                       assert_eq!(self.header_hashes.len(), self.height);
-                       self.header_hashes.push(header.bitcoin_hash());
-               }
-               self.max_height = cmp::max(self.height, self.max_height);
-       }
-
-       fn disconnect_block(&mut self) {
-               if self.height > 0 && (self.max_height < 6 || self.height >= self.max_height - 6) {
-                       self.height -= 1;
-                       let header = BlockHeader { version: 0x20000000, prev_blockhash: self.header_hashes[self.height], merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-                       self.manager.block_disconnected(&header, self.height as u32);
-                       self.monitor.block_disconnected(&header, self.height as u32);
-                       let removal_height = self.height;
-                       self.txids_confirmed.retain(|_, height| {
-                               removal_height != *height
-                       });
-               }
-       }
-}
-
-impl<'a> Drop for MoneyLossDetector<'a> {
-       fn drop(&mut self) {
-               if !::std::thread::panicking() {
-                       // Disconnect all peers
-                       for (idx, peer) in self.peers.borrow().iter().enumerate() {
-                               if *peer {
-                                       self.handler.disconnect_event(&Peer{id: idx as u8, peers_connected: &self.peers});
-                               }
-                       }
-
-                       // Force all channels onto the chain (and time out claim txn)
-                       self.manager.force_close_all_channels();
-               }
-       }
-}
-
-struct KeyProvider {
-       node_secret: SecretKey,
-       counter: AtomicU64,
-}
-impl KeysInterface for KeyProvider {
-       fn get_node_secret(&self) -> SecretKey {
-               self.node_secret.clone()
-       }
-
-       fn get_destination_script(&self) -> Script {
-               let secp_ctx = Secp256k1::signing_only();
-               let channel_monitor_claim_key = SecretKey::from_slice(&hex::decode("0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap()[..]).unwrap();
-               let our_channel_monitor_claim_key_hash = <Hash160 as bitcoin_hashes::Hash>::hash(&PublicKey::from_secret_key(&secp_ctx, &channel_monitor_claim_key).serialize());
-               Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(&our_channel_monitor_claim_key_hash[..]).into_script()
-       }
-
-       fn get_shutdown_pubkey(&self) -> PublicKey {
-               let secp_ctx = Secp256k1::signing_only();
-               PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]).unwrap())
-       }
-
-       fn get_channel_keys(&self, inbound: bool) -> ChannelKeys {
-               let ctr = self.counter.fetch_add(1, Ordering::Relaxed) as u8;
-               if inbound {
-                       ChannelKeys {
-                               funding_key:               SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, ctr]).unwrap(),
-                               revocation_base_key:       SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, ctr]).unwrap(),
-                               payment_base_key:          SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, ctr]).unwrap(),
-                               delayed_payment_base_key:  SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, ctr]).unwrap(),
-                               htlc_base_key:             SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, ctr]).unwrap(),
-                               commitment_seed: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, ctr],
-                       }
-               } else {
-                       ChannelKeys {
-                               funding_key:               SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, ctr]).unwrap(),
-                               revocation_base_key:       SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, ctr]).unwrap(),
-                               payment_base_key:          SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, ctr]).unwrap(),
-                               delayed_payment_base_key:  SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, ctr]).unwrap(),
-                               htlc_base_key:             SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, ctr]).unwrap(),
-                               commitment_seed: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, ctr],
-                       }
-               }
-       }
-
-       fn get_session_key(&self) -> SecretKey {
-               let ctr = self.counter.fetch_add(1, Ordering::Relaxed) as u8;
-               SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, ctr]).unwrap()
-       }
-
-       fn get_channel_id(&self) -> [u8; 32] {
-               let ctr = self.counter.fetch_add(1, Ordering::Relaxed);
-               [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-               (ctr >> 8*7) as u8, (ctr >> 8*6) as u8, (ctr >> 8*5) as u8, (ctr >> 8*4) as u8, (ctr >> 8*3) as u8, (ctr >> 8*2) as u8, (ctr >> 8*1) as u8, 14, (ctr >> 8*0) as u8]
-       }
-}
-
-#[inline]
-pub fn do_test(data: &[u8], logger: &Arc<Logger>) {
-       let input = Arc::new(InputData {
-               data: data.to_vec(),
-               read_pos: AtomicUsize::new(0),
-       });
-       let fee_est = Arc::new(FuzzEstimator {
-               input: input.clone(),
-       });
-
-       macro_rules! get_slice {
-               ($len: expr) => {
-                       match input.get_slice($len as usize) {
-                               Some(slice) => slice,
-                               None => return,
-                       }
-               }
-       }
-
-       macro_rules! get_pubkey {
-               () => {
-                       match PublicKey::from_slice(get_slice!(33)) {
-                               Ok(key) => key,
-                               Err(_) => return,
-                       }
-               }
-       }
-
-       let our_network_key = match SecretKey::from_slice(get_slice!(32)) {
-               Ok(key) => key,
-               Err(_) => return,
-       };
-
-       let watch = Arc::new(ChainWatchInterfaceUtil::new(Network::Bitcoin, Arc::clone(&logger)));
-       let broadcast = Arc::new(TestBroadcaster{});
-       let monitor = channelmonitor::SimpleManyChannelMonitor::new(watch.clone(), broadcast.clone(), Arc::clone(&logger), fee_est.clone());
-
-       let keys_manager = Arc::new(KeyProvider { node_secret: our_network_key.clone(), counter: AtomicU64::new(0) });
-       let mut config = UserConfig::new();
-       config.channel_options.fee_proportional_millionths =  slice_to_be32(get_slice!(4));
-       config.channel_options.announced_channel = get_slice!(1)[0] != 0;
-       config.peer_channel_config_limits.min_dust_limit_satoshis = 0;
-       let channelmanager = ChannelManager::new(Network::Bitcoin, fee_est.clone(), monitor.clone(), watch.clone(), broadcast.clone(), Arc::clone(&logger), keys_manager.clone(), config).unwrap();
-       let router = Arc::new(Router::new(PublicKey::from_secret_key(&Secp256k1::signing_only(), &keys_manager.get_node_secret()), watch.clone(), Arc::clone(&logger)));
-
-       let peers = RefCell::new([false; 256]);
-       let mut loss_detector = MoneyLossDetector::new(&peers, channelmanager.clone(), monitor.clone(), PeerManager::new(MessageHandler {
-               chan_handler: channelmanager.clone(),
-               route_handler: router.clone(),
-       }, our_network_key, &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0], Arc::clone(&logger)));
-
-       let mut should_forward = false;
-       let mut payments_received: Vec<PaymentHash> = Vec::new();
-       let mut payments_sent = 0;
-       let mut pending_funding_generation: Vec<([u8; 32], u64, Script)> = Vec::new();
-       let mut pending_funding_signatures = HashMap::new();
-       let mut pending_funding_relay = Vec::new();
-
-       loop {
-               match get_slice!(1)[0] {
-                       0 => {
-                               let mut new_id = 0;
-                               for i in 1..256 {
-                                       if !peers.borrow()[i-1] {
-                                               new_id = i;
-                                               break;
-                                       }
-                               }
-                               if new_id == 0 { return; }
-                               loss_detector.handler.new_outbound_connection(get_pubkey!(), Peer{id: (new_id - 1) as u8, peers_connected: &peers}).unwrap();
-                               peers.borrow_mut()[new_id - 1] = true;
-                       },
-                       1 => {
-                               let mut new_id = 0;
-                               for i in 1..256 {
-                                       if !peers.borrow()[i-1] {
-                                               new_id = i;
-                                               break;
-                                       }
-                               }
-                               if new_id == 0 { return; }
-                               loss_detector.handler.new_inbound_connection(Peer{id: (new_id - 1) as u8, peers_connected: &peers}).unwrap();
-                               peers.borrow_mut()[new_id - 1] = true;
-                       },
-                       2 => {
-                               let peer_id = get_slice!(1)[0];
-                               if !peers.borrow()[peer_id as usize] { return; }
-                               loss_detector.handler.disconnect_event(&Peer{id: peer_id, peers_connected: &peers});
-                               peers.borrow_mut()[peer_id as usize] = false;
-                       },
-                       3 => {
-                               let peer_id = get_slice!(1)[0];
-                               if !peers.borrow()[peer_id as usize] { return; }
-                               match loss_detector.handler.read_event(&mut Peer{id: peer_id, peers_connected: &peers}, get_slice!(get_slice!(1)[0]).to_vec()) {
-                                       Ok(res) => assert!(!res),
-                                       Err(_) => { peers.borrow_mut()[peer_id as usize] = false; }
-                               }
-                       },
-                       4 => {
-                               let value = slice_to_be24(get_slice!(3)) as u64;
-                               let route = match router.get_route(&get_pubkey!(), None, &Vec::new(), value, 42) {
-                                       Ok(route) => route,
-                                       Err(_) => return,
-                               };
-                               let mut payment_hash = PaymentHash([0; 32]);
-                               payment_hash.0[0..8].copy_from_slice(&be64_to_array(payments_sent));
-                               let mut sha = Sha256::engine();
-                               sha.input(&payment_hash.0[..]);
-                               payment_hash.0 = Sha256::from_engine(sha).into_inner();
-                               payments_sent += 1;
-                               match channelmanager.send_payment(route, payment_hash) {
-                                       Ok(_) => {},
-                                       Err(_) => return,
-                               }
-                       },
-                       5 => {
-                               let peer_id = get_slice!(1)[0];
-                               if !peers.borrow()[peer_id as usize] { return; }
-                               let their_key = get_pubkey!();
-                               let chan_value = slice_to_be24(get_slice!(3)) as u64;
-                               let push_msat_value = slice_to_be24(get_slice!(3)) as u64;
-                               if channelmanager.create_channel(their_key, chan_value, push_msat_value, 0).is_err() { return; }
-                       },
-                       6 => {
-                               let mut channels = channelmanager.list_channels();
-                               let channel_id = get_slice!(1)[0] as usize;
-                               if channel_id >= channels.len() { return; }
-                               channels.sort_by(|a, b| { a.channel_id.cmp(&b.channel_id) });
-                               if channelmanager.close_channel(&channels[channel_id].channel_id).is_err() { return; }
-                       },
-                       7 => {
-                               if should_forward {
-                                       channelmanager.process_pending_htlc_forwards();
-                                       should_forward = false;
-                               }
-                       },
-                       8 => {
-                               for payment in payments_received.drain(..) {
-                                       // SHA256 is defined as XOR of all input bytes placed in the first byte, and 0s
-                                       // for the remaining bytes. Thus, if not all remaining bytes are 0s we cannot
-                                       // fulfill this HTLC, but if they are, we can just take the first byte and
-                                       // place that anywhere in our preimage.
-                                       if &payment.0[1..] != &[0; 31] {
-                                               channelmanager.fail_htlc_backwards(&payment);
-                                       } else {
-                                               let mut payment_preimage = PaymentPreimage([0; 32]);
-                                               payment_preimage.0[0] = payment.0[0];
-                                               channelmanager.claim_funds(payment_preimage);
-                                       }
-                               }
-                       },
-                       9 => {
-                               for payment in payments_received.drain(..) {
-                                       channelmanager.fail_htlc_backwards(&payment);
-                               }
-                       },
-                       10 => {
-                               'outer_loop: for funding_generation in pending_funding_generation.drain(..) {
-                                       let mut tx = Transaction { version: 0, lock_time: 0, input: Vec::new(), output: vec![TxOut {
-                                                       value: funding_generation.1, script_pubkey: funding_generation.2,
-                                               }] };
-                                       let funding_output = 'search_loop: loop {
-                                               let funding_txid = tx.txid();
-                                               if let None = loss_detector.txids_confirmed.get(&funding_txid) {
-                                                       let outpoint = OutPoint::new(funding_txid, 0);
-                                                       for chan in channelmanager.list_channels() {
-                                                               if chan.channel_id == outpoint.to_channel_id() {
-                                                                       tx.version += 1;
-                                                                       continue 'search_loop;
-                                                               }
-                                                       }
-                                                       break outpoint;
-                                               }
-                                               tx.version += 1;
-                                               if tx.version > 0xff {
-                                                       continue 'outer_loop;
-                                               }
-                                       };
-                                       channelmanager.funding_transaction_generated(&funding_generation.0, funding_output.clone());
-                                       pending_funding_signatures.insert(funding_output, tx);
-                               }
-                       },
-                       11 => {
-                               if !pending_funding_relay.is_empty() {
-                                       loss_detector.connect_block(&pending_funding_relay[..]);
-                                       for _ in 2..100 {
-                                               loss_detector.connect_block(&[]);
-                                       }
-                               }
-                               for tx in pending_funding_relay.drain(..) {
-                                       loss_detector.funding_txn.push(tx);
-                               }
-                       },
-                       12 => {
-                               let txlen = slice_to_be16(get_slice!(2));
-                               if txlen == 0 {
-                                       loss_detector.connect_block(&[]);
-                               } else {
-                                       let txres: Result<Transaction, _> = deserialize(get_slice!(txlen));
-                                       if let Ok(tx) = txres {
-                                               loss_detector.connect_block(&[tx]);
-                                       } else {
-                                               return;
-                                       }
-                               }
-                       },
-                       13 => {
-                               loss_detector.disconnect_block();
-                       },
-                       14 => {
-                               let mut channels = channelmanager.list_channels();
-                               let channel_id = get_slice!(1)[0] as usize;
-                               if channel_id >= channels.len() { return; }
-                               channels.sort_by(|a, b| { a.channel_id.cmp(&b.channel_id) });
-                               channelmanager.force_close_channel(&channels[channel_id].channel_id);
-                       },
-                       _ => return,
-               }
-               loss_detector.handler.process_events();
-               for event in loss_detector.manager.get_and_clear_pending_events() {
-                       match event {
-                               Event::FundingGenerationReady { temporary_channel_id, channel_value_satoshis, output_script, .. } => {
-                                       pending_funding_generation.push((temporary_channel_id, channel_value_satoshis, output_script));
-                               },
-                               Event::FundingBroadcastSafe { funding_txo, .. } => {
-                                       pending_funding_relay.push(pending_funding_signatures.remove(&funding_txo).unwrap());
-                               },
-                               Event::PaymentReceived { payment_hash, .. } => {
-                                       payments_received.push(payment_hash);
-                               },
-                               Event::PaymentSent {..} => {},
-                               Event::PaymentFailed {..} => {},
-                               Event::PendingHTLCsForwardable {..} => {
-                                       should_forward = true;
-                               },
-                               Event::SpendableOutputs {..} => {},
-                       }
-               }
-       }
-}
-
-#[cfg(feature = "afl")]
-#[macro_use] extern crate afl;
-#[cfg(feature = "afl")]
-fn main() {
-       fuzz!(|data| {
-               let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new("".to_owned()));
-               do_test(data, &logger);
-       });
-}
-
-#[cfg(feature = "honggfuzz")]
-#[macro_use] extern crate honggfuzz;
-#[cfg(feature = "honggfuzz")]
-fn main() {
-       loop {
-               fuzz!(|data| {
-                       let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new("".to_owned()));
-                       do_test(data, &logger);
-               });
-       }
-}
-
-#[cfg(feature = "libfuzzer_fuzz")]
-#[macro_use] extern crate libfuzzer_sys;
-#[cfg(feature = "libfuzzer_fuzz")]
-fuzz_target!(|data: &[u8]| {
-       let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new("".to_owned()));
-       do_test(data, &logger);
-});
-
-extern crate hex;
-#[cfg(test)]
-mod tests {
-       use utils::test_logger;
-       use lightning::util::logger::{Logger, Record};
-       use std::collections::HashMap;
-       use std::sync::{Arc, Mutex};
-
-       #[test]
-       fn duplicate_crash() {
-               let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new("".to_owned()));
-               super::do_test(&::hex::decode("00").unwrap(), &logger);
-       }
-
-       struct TrackingLogger {
-               /// (module, message) -> count
-               pub lines: Mutex<HashMap<(String, String), usize>>,
-       }
-       impl Logger for TrackingLogger {
-               fn log(&self, record: &Record) {
-                       *self.lines.lock().unwrap().entry((record.module_path.to_string(), format!("{}", record.args))).or_insert(0) += 1;
-                       println!("{:<5} [{} : {}, {}] {}", record.level.to_string(), record.module_path, record.file, record.line, record.args);
-               }
-       }
-
-       #[test]
-       fn test_no_existing_test_breakage() {
-               // To avoid accidentally causing all existing fuzz test cases to be useless by making minor
-               // changes (such as requesting feerate info in a new place), we run a pretty full
-               // step-through with two peers and HTLC forwarding here. Obviously this is pretty finicky,
-               // so this should be updated pretty liberally, but at least we'll know when changes occur.
-               // If nothing else, this test serves as a pretty great initial full_stack_target seed.
-
-               // What each byte represents is broken down below, and then everything is concatenated into
-               // one large test at the end (you want %s/ -.*//g %s/\n\| \|\t\|\///g).
-
-               // Following BOLT 8, lightning message on the wire are: 2-byte encrypted message length + 
-               // 16-byte MAC of the encrypted message length + encrypted Lightning message + 16-byte MAC
-               // of the Lightning message
-               // I.e 2nd inbound read, len 18 : 0006 (encrypted message length) + 03000000000000000000000000000000 (MAC of the encrypted message length)
-               // Len 22 : 0010 00000000 (encrypted lightning message) + 03000000000000000000000000000000 (MAC of the Lightning message)
-
-               // 0000000000000000000000000000000000000000000000000000000000000000 - our network key
-               // 00000000 - fee_proportional_millionths
-               // 01 - announce_channels_publicly
-               //
-               // 00 - new outbound connection with id 0
-               // 030000000000000000000000000000000000000000000000000000000000000000 - peer's pubkey
-               // 030032 - inbound read from peer id 0 of len 50
-               // 00 030000000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - noise act two (0||pubkey||mac)
-               //
-               // 030012 - inbound read from peer id 0 of len 18
-               // 0006 03000000000000000000000000000000 - message header indicating message length 6
-               // 030016 - inbound read from peer id 0 of len 22
-               // 0010 00000000 03000000000000000000000000000000 - init message with no features (type 16) and mac
-               //
-               // 030012 - inbound read from peer id 0 of len 18
-               // 0141 03000000000000000000000000000000 - message header indicating message length 321
-               // 0300fe - inbound read from peer id 0 of len 254
-               // 0020 7500000000000000000000000000000000000000000000000000000000000000 ff4f00f805273c1b203bb5ebf8436bfde57b3be8c2f5e95d9491dbb181909679 000000000000c350 0000000000000000 0000000000000222 ffffffffffffffff 0000000000000222 0000000000000000 000000fd 0006 01e3 030000000000000000000000000000000000000000000000000000000000000001 030000000000000000000000000000000000000000000000000000000000000002 030000000000000000000000000000000000000000000000000000000000000003 030000000000000000000000000000000000000000000000000000000000000004 - beginning of open_channel message
-               // 030053 - inbound read from peer id 0 of len 83
-               // 030000000000000000000000000000000000000000000000000000000000000005 030000000000000000000000000000000000000000000000000000000000000000 01 03000000000000000000000000000000 - rest of open_channel and mac
-               //
-               // 00fd00fd00fd - Three feerate requests (all returning min feerate, which our open_channel also uses) (gonna be ingested by FuzzEstimator)
-               // - client should now respond with accept_channel (CHECK 1: type 33 to peer 03000000)
-               //
-               // 030012 - inbound read from peer id 0 of len 18
-               // 0084 03000000000000000000000000000000 - message header indicating message length 132
-               // 030094 - inbound read from peer id 0 of len 148
-               // 0022 ff4f00f805273c1b203bb5ebf8436bfde57b3be8c2f5e95d9491dbb181909679 3d00000000000000000000000000000000000000000000000000000000000000 0000 5c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 03000000000000000000000000000000 - funding_created and mac
-               // - client should now respond with funding_signed (CHECK 2: type 35 to peer 03000000)
-               //
-               // 0c005e - connect a block with one transaction of len 94
-               // 020000000100000000000000000000000000000000000000000000000000000000000000000000000000ffffffff0150c3000000000000220020ae0000000000000000000000000000000000000000000000000000000000000000000000 - the funding transaction
-               // 0c0000 - connect a block with no transactions
-               // 0c0000 - connect a block with no transactions
-               // 0c0000 - connect a block with no transactions
-               // 0c0000 - connect a block with no transactions
-               // 0c0000 - connect a block with no transactions
-               // 0c0000 - connect a block with no transactions
-               // 0c0000 - connect a block with no transactions
-               // 0c0000 - connect a block with no transactions
-               // 0c0000 - connect a block with no transactions
-               // 0c0000 - connect a block with no transactions
-               // 0c0000 - connect a block with no transactions
-               // 0c0000 - connect a block with no transactions
-               // - by now client should have sent a funding_locked (CHECK 3: SendFundingLocked to 03000000 for chan 3d000000)
-               //
-               // 030012 - inbound read from peer id 0 of len 18
-               // 0043 03000000000000000000000000000000 - message header indicating message length 67
-               // 030053 - inbound read from peer id 0 of len 83
-               // 0024 3d00000000000000000000000000000000000000000000000000000000000000 030100000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - funding_locked and mac
-               //
-               // 01 - new inbound connection with id 1
-               // 030132 - inbound read from peer id 1 of len 50
-               // 0003000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000 - inbound noise act 1
-               // 030142 - inbound read from peer id 1 of len 66
-               // 000302000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003000000000000000000000000000000 - inbound noise act 3
-               //
-               // 030112 - inbound read from peer id 1 of len 18
-               // 0006 01000000000000000000000000000000 - message header indicating message length 6
-               // 030116 - inbound read from peer id 1 of len 22
-               // 0010 00000000 01000000000000000000000000000000 - init message with no features (type 16)
-               //
-               // 05 01 030200000000000000000000000000000000000000000000000000000000000000 00c350 0003e8 - create outbound channel to peer 1 for 50k sat
-               // 00fd00fd00fd - Three feerate requests (all returning min feerate) (gonna be ingested by FuzzEstimator)
-               //
-               // 030112 - inbound read from peer id 1 of len 18
-               // 0110 01000000000000000000000000000000 - message header indicating message length 272
-               // 0301ff - inbound read from peer id 1 of len 255
-               // 0021 0000000000000000000000000000000000000000000000000000000000000e02 000000000000001a 00000000004c4b40 00000000000003e8 00000000000003e8 00000002 03f0 0005 030000000000000000000000000000000000000000000000000000000000000100 030000000000000000000000000000000000000000000000000000000000000200 030000000000000000000000000000000000000000000000000000000000000300 030000000000000000000000000000000000000000000000000000000000000400 030000000000000000000000000000000000000000000000000000000000000500 03000000000000000000000000000000 - beginning of accept_channel
-               // 030121 - inbound read from peer id 1 of len 33
-               // 0000000000000000000000000000000000 01000000000000000000000000000000 - rest of accept_channel and mac
-               //
-               // 0a - create the funding transaction (client should send funding_created now)
-               //
-               // 030112 - inbound read from peer id 1 of len 18
-               // 0062 01000000000000000000000000000000 - message header indicating message length 98
-               // 030172 - inbound read from peer id 1 of len 114
-               // 0023 3900000000000000000000000000000000000000000000000000000000000000 f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100 01000000000000000000000000000000 - funding_signed message and mac
-               //
-               // 0b - broadcast funding transaction
-               // - by now client should have sent a funding_locked (CHECK 4: SendFundingLocked to 03020000 for chan 3f000000)
-               //
-               // 030112 - inbound read from peer id 1 of len 18
-               // 0043 01000000000000000000000000000000 - message header indicating message length 67
-               // 030153 - inbound read from peer id 1 of len 83
-               // 0024 3900000000000000000000000000000000000000000000000000000000000000 030100000000000000000000000000000000000000000000000000000000000000 01000000000000000000000000000000 - funding_locked and mac
-               //
-               // 030012 - inbound read from peer id 0 of len 18
-               // 05ac 03000000000000000000000000000000 - message header indicating message length 1452
-               // 0300ff - inbound read from peer id 0 of len 255
-               // 0080 3d00000000000000000000000000000000000000000000000000000000000000 0000000000000000 0000000000003e80 ff00000000000000000000000000000000000000000000000000000000000000 00000121 00 030000000000000000000000000000000000000000000000000000000000000555 0000000e000001000000000000000003e8000000010000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - beginning of update_add_htlc from 0 to 1 via client
-               // 0300ff - inbound read from peer id 0 of len 255
-               // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
-               // 0300ff - inbound read from peer id 0 of len 255
-               // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
-               // 0300ff - inbound read from peer id 0 of len 255
-               // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
-               // 0300ff - inbound read from peer id 0 of len 255
-               // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
-               // 0300c1 - inbound read from peer id 0 of len 193
-               // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ef00000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - end of update_add_htlc from 0 to 1 via client and mac
-               //
-               // 00fd - A feerate request (returning min feerate, which our open_channel also uses) (gonna be ingested by FuzzEstimator)
-               //
-               // 030012 - inbound read from peer id 0 of len 18
-               // 0064 03000000000000000000000000000000 - message header indicating message length 100
-               // 030074 - inbound read from peer id 0 of len 116
-               // 0084 3d00000000000000000000000000000000000000000000000000000000000000 4d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 0000 03000000000000000000000000000000 - commitment_signed and mac
-               // - client should now respond with revoke_and_ack and commitment_signed (CHECK 5/6: types 133 and 132 to peer 03000000)
-               //
-               // 030012 - inbound read from peer id 0 of len 18
-               // 0063 03000000000000000000000000000000 - message header indicating message length 99
-               // 030073 - inbound read from peer id 0 of len 115
-               // 0085 3d00000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 030200000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - revoke_and_ack and mac
-               //
-               // 07 - process the now-pending HTLC forward
-               // - client now sends id 1 update_add_htlc and commitment_signed (CHECK 7: SendHTLCs event for node 03020000 with 1 HTLCs for channel 3f000000)
-               //
-               // - we respond with commitment_signed then revoke_and_ack (a weird, but valid, order)
-               // 030112 - inbound read from peer id 1 of len 18
-               // 0064 01000000000000000000000000000000 - message header indicating message length 100
-               // 030174 - inbound read from peer id 1 of len 116
-               // 0084 3900000000000000000000000000000000000000000000000000000000000000 f1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100 0000 01000000000000000000000000000000 - commitment_signed and mac
-               //
-               // 030112 - inbound read from peer id 1 of len 18
-               // 0063 01000000000000000000000000000000 - message header indicating message length 99
-               // 030173 - inbound read from peer id 1 of len 115
-               // 0085 3900000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 030200000000000000000000000000000000000000000000000000000000000000 01000000000000000000000000000000 - revoke_and_ack and mac
-               //
-               // 030112 - inbound read from peer id 1 of len 18
-               // 004a 01000000000000000000000000000000 - message header indicating message length 74
-               // 03015a - inbound read from peer id 1 of len 90
-               // 0082 3900000000000000000000000000000000000000000000000000000000000000 0000000000000000 ff00888888888888888888888888888888888888888888888888888888888888 01000000000000000000000000000000 - update_fulfill_htlc and mac
-               // - client should immediately claim the pending HTLC from peer 0 (CHECK 8: SendFulfillHTLCs for node 03000000 with preimage ff00888888 for channel 3d000000)
-               //
-               // 030112 - inbound read from peer id 1 of len 18
-               // 0064 01000000000000000000000000000000 - message header indicating message length 100
-               // 030174 - inbound read from peer id 1 of len 116
-               // 0084 3900000000000000000000000000000000000000000000000000000000000000 fd000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100 0000 01000000000000000000000000000000 - commitment_signed and mac
-               //
-               // 030112 - inbound read from peer id 1 of len 18
-               // 0063 01000000000000000000000000000000 - message header indicating message length 99
-               // 030173 - inbound read from peer id 1 of len 115
-               // 0085 3900000000000000000000000000000000000000000000000000000000000000 0100000000000000000000000000000000000000000000000000000000000000 030300000000000000000000000000000000000000000000000000000000000000 01000000000000000000000000000000 - revoke_and_ack and mac
-               //
-               // - before responding to the commitment_signed generated above, send a new HTLC
-               // 030012 - inbound read from peer id 0 of len 18
-               // 05ac 03000000000000000000000000000000 - message header indicating message length 1452
-               // 0300ff - inbound read from peer id 0 of len 255
-               // 0080 3d00000000000000000000000000000000000000000000000000000000000000 0000000000000001 0000000000003e80 ff00000000000000000000000000000000000000000000000000000000000000 00000121 00 030000000000000000000000000000000000000000000000000000000000000555 0000000e000001000000000000000003e8000000010000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - beginning of update_add_htlc from 0 to 1 via client
-               // 0300ff - inbound read from peer id 0 of len 255
-               // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
-               // 0300ff - inbound read from peer id 0 of len 255
-               // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
-               // 0300ff - inbound read from peer id 0 of len 255
-               // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
-               // 0300ff - inbound read from peer id 0 of len 255
-               // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
-               // 0300c1 - inbound read from peer id 0 of len 193
-               // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ef00000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - end of update_add_htlc from 0 to 1 via client and mac
-               //
-               // 00fd - A feerate request (returning min feerate, which our open_channel also uses) (gonna be ingested by FuzzEstimator)
-               //
-               // - now respond to the update_fulfill_htlc+commitment_signed messages the client sent to peer 0
-               // 030012 - inbound read from peer id 0 of len 18
-               // 0063 03000000000000000000000000000000 - message header indicating message length 99
-               // 030073 - inbound read from peer id 0 of len 115
-               // 0085 3d00000000000000000000000000000000000000000000000000000000000000 0100000000000000000000000000000000000000000000000000000000000000 030300000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - revoke_and_ack and mac
-               // - client should now respond with revoke_and_ack and commitment_signed (CHECK 5/6 duplicates)
-               //
-               // 030012 - inbound read from peer id 0 of len 18
-               // 0064 03000000000000000000000000000000 - message header indicating message length 100
-               // 030074 - inbound read from peer id 0 of len 116
-               // 0084 3d00000000000000000000000000000000000000000000000000000000000000 be000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 0000 03000000000000000000000000000000 - commitment_signed and mac
-               //
-               // 030012 - inbound read from peer id 0 of len 18
-               // 0063 03000000000000000000000000000000 - message header indicating message length 99
-               // 030073 - inbound read from peer id 0 of len 115
-               // 0085 3d00000000000000000000000000000000000000000000000000000000000000 0200000000000000000000000000000000000000000000000000000000000000 030400000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - revoke_and_ack and mac
-               //
-               // 07 - process the now-pending HTLC forward
-               // - client now sends id 1 update_add_htlc and commitment_signed (CHECK 7 duplicate)
-               // - we respond with revoke_and_ack, then commitment_signed, then update_fail_htlc
-               //
-               // 030112 - inbound read from peer id 1 of len 18
-               // 0064 01000000000000000000000000000000 - message header indicating message length 100
-               // 030174 - inbound read from peer id 1 of len 116
-               // 0084 3900000000000000000000000000000000000000000000000000000000000000 fc000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100 0000 01000000000000000000000000000000 - commitment_signed and mac
-               //
-               // 030112 - inbound read from peer id 1 of len 18
-               // 0063 01000000000000000000000000000000 - message header indicating message length 99
-               // 030173 - inbound read from peer id 1 of len 115
-               // 0085 3900000000000000000000000000000000000000000000000000000000000000 0200000000000000000000000000000000000000000000000000000000000000 030400000000000000000000000000000000000000000000000000000000000000 01000000000000000000000000000000 - revoke_and_ack and mac
-               //
-               // 030112 - inbound read from peer id 1 of len 18
-               // 002c 01000000000000000000000000000000 - message header indicating message length 44
-               // 03013c - inbound read from peer id 1 of len 60
-               // 0083 3900000000000000000000000000000000000000000000000000000000000000 0000000000000001 0000 01000000000000000000000000000000 - update_fail_htlc and mac
-               //
-               // 030112 - inbound read from peer id 1 of len 18
-               // 0064 01000000000000000000000000000000 - message header indicating message length 100
-               // 030174 - inbound read from peer id 1 of len 116
-               // 0084 3900000000000000000000000000000000000000000000000000000000000000 fb000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100 0000 01000000000000000000000000000000 - commitment_signed and mac
-               //
-               // 030112 - inbound read from peer id 1 of len 18
-               // 0063 01000000000000000000000000000000 - message header indicating message length 99
-               // 030173 - inbound read from peer id 1 of len 115
-               // 0085 3900000000000000000000000000000000000000000000000000000000000000 0300000000000000000000000000000000000000000000000000000000000000 030500000000000000000000000000000000000000000000000000000000000000 01000000000000000000000000000000 - revoke_and_ack and mac
-               //
-               // 07 - process the now-pending HTLC forward
-               // - client now sends id 0 update_fail_htlc and commitment_signed (CHECK 9)
-               // - now respond to the update_fail_htlc+commitment_signed messages the client sent to peer 0
-               //
-               // 030012 - inbound read from peer id 0 of len 18
-               // 0063 03000000000000000000000000000000 - message header indicating message length 99
-               // 030073 - inbound read from peer id 0 of len 115
-               // 0085 3d00000000000000000000000000000000000000000000000000000000000000 0300000000000000000000000000000000000000000000000000000000000000 030500000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - revoke_and_ack and mac
-               //
-               // 030012 - inbound read from peer id 0 of len 18
-               // 0064 03000000000000000000000000000000 - message header indicating message length 100
-               // 030074 - inbound read from peer id 0 of len 116
-               // 0084 3d00000000000000000000000000000000000000000000000000000000000000 4f000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 0000 03000000000000000000000000000000 - commitment_signed and mac
-               // - client should now respond with revoke_and_ack (CHECK 5 duplicate)
-               //
-               // 030012 - inbound read from peer id 0 of len 18
-               // 05ac 03000000000000000000000000000000 - message header indicating message length 1452
-               // 0300ff - inbound read from peer id 0 of len 255
-               // 0080 3d00000000000000000000000000000000000000000000000000000000000000 0000000000000002 00000000000b0838 ff00000000000000000000000000000000000000000000000000000000000000 00000121 00 030000000000000000000000000000000000000000000000000000000000000555 0000000e0000010000000000000003e800000000010000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - beginning of update_add_htlc from 0 to 1 via client
-               // 0300ff - inbound read from peer id 0 of len 255
-               // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
-               // 0300ff - inbound read from peer id 0 of len 255
-               // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
-               // 0300ff - inbound read from peer id 0 of len 255
-               // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
-               // 0300ff - inbound read from peer id 0 of len 255
-               // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
-               // 0300c1 - inbound read from peer id 0 of len 193
-               // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ef00000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - end of update_add_htlc from 0 to 1 via client and mac
-               //
-               // 00fd - A feerate request (returning min feerate, which our open_channel also uses) (gonna be ingested by FuzzEstimator)
-               //
-               // 030012 - inbound read from peer id 0 of len 18
-               // 00a4 03000000000000000000000000000000 - message header indicating message length 164
-               // 0300b4 - inbound read from peer id 0 of len 180
-               // 0084 3d00000000000000000000000000000000000000000000000000000000000000 07000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 0001 c8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007f00000000000000 03000000000000000000000000000000 - commitment_signed and mac
-               // - client should now respond with revoke_and_ack and commitment_signed (CHECK 5/6 duplicates)
-               //
-               // 030012 - inbound read from peer id 0 of len 18
-               // 0063 03000000000000000000000000000000 - message header indicating message length 99
-               // 030073 - inbound read from peer id 0 of len 115
-               // 0085 3d00000000000000000000000000000000000000000000000000000000000000 0400000000000000000000000000000000000000000000000000000000000000 030600000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - revoke_and_ack and mac
-               //
-               // 07 - process the now-pending HTLC forward
-               // - client now sends id 1 update_add_htlc and commitment_signed (CHECK 7 duplicate)
-               //
-               // 0c007d - connect a block with one transaction of len 125
-               // 0200000001390000000000000000000000000000000000000000000000000000000000000000000000000000008002000100000000000022002090000000000000000000000000000000000000000000000000000000000000006cc10000000000001600145c0000000000000000000000000000000000000005000020 - the commitment transaction for channel 3f00000000000000000000000000000000000000000000000000000000000000
-               // 00fd - A feerate request (returning min feerate, which our open_channel also uses) (gonna be ingested by FuzzEstimator)
-               // 00fd - A feerate request (returning min feerate, which our open_channel also uses) (gonna be ingested by FuzzEstimator)
-               // 0c005e - connect a block with one transaction of len 94
-               // 0200000001fd00000000000000000000000000000000000000000000000000000000000000000000000000000000014f00000000000000220020f60000000000000000000000000000000000000000000000000000000000000000000000 - the funding transaction
-               // 0c0000 - connect a block with no transactions
-               // 0c0000 - connect a block with no transactions
-               // 0c0000 - connect a block with no transactions
-               // 0c0000 - connect a block with no transactions
-               // 0c0000 - connect a block with no transactions
-               //
-               // 07 - process the now-pending HTLC forward
-               // - client now fails the HTLC backwards as it was unable to extract the payment preimage (CHECK 9 duplicate and CHECK 10)
-
-               let logger = Arc::new(TrackingLogger { lines: Mutex::new(HashMap::new()) });
-               super::do_test(&::hex::decode("00000000000000000000000000000000000000000000000000000000000000000000000001000300000000000000000000000000000000000000000000000000000000000000000300320003000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000030012000603000000000000000000000000000000030016001000000000030000000000000000000000000000000300120141030000000000000000000000000000000300fe00207500000000000000000000000000000000000000000000000000000000000000ff4f00f805273c1b203bb5ebf8436bfde57b3be8c2f5e95d9491dbb181909679000000000000c35000000000000000000000000000000222ffffffffffffffff00000000000002220000000000000000000000fd000601e3030000000000000000000000000000000000000000000000000000000000000001030000000000000000000000000000000000000000000000000000000000000002030000000000000000000000000000000000000000000000000000000000000003030000000000000000000000000000000000000000000000000000000000000004030053030000000000000000000000000000000000000000000000000000000000000005030000000000000000000000000000000000000000000000000000000000000000010300000000000000000000000000000000fd00fd00fd0300120084030000000000000000000000000000000300940022ff4f00f805273c1b203bb5ebf8436bfde57b3be8c2f5e95d9491dbb1819096793d0000000000000000000000000000000000000000000000000000000000000000005c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001030000000000000000000000000000000c005e020000000100000000000000000000000000000000000000000000000000000000000000000000000000ffffffff0150c3000000000000220020ae00000000000000000000000000000000000000000000000000000000000000000000000c00000c00000c00000c00000c00000c00000c00000c00000c00000c00000c00000c000003001200430300000000000000000000000000000003005300243d000000000000000000000000000000000000000000000000000000000000000301000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001030132000300000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003014200030200000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000300000000000000000000000000000003011200060100000000000000000000000000000003011600100000000001000000000000000000000000000000050103020000000000000000000000000000000000000000000000000000000000000000c3500003e800fd00fd00fd0301120110010000000000000000000000000000000301ff00210000000000000000000000000000000000000000000000000000000000000e02000000000000001a00000000004c4b4000000000000003e800000000000003e80000000203f00005030000000000000000000000000000000000000000000000000000000000000100030000000000000000000000000000000000000000000000000000000000000200030000000000000000000000000000000000000000000000000000000000000300030000000000000000000000000000000000000000000000000000000000000400030000000000000000000000000000000000000000000000000000000000000500030000000000000000000000000000000301210000000000000000000000000000000000010000000000000000000000000000000a03011200620100000000000000000000000000000003017200233900000000000000000000000000000000000000000000000000000000000000f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100010000000000000000000000000000000b030112004301000000000000000000000000000000030153002439000000000000000000000000000000000000000000000000000000000000000301000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000003001205ac030000000000000000000000000000000300ff00803d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003e80ff0000000000000000000000000000000000000000000000000000000000000000000121000300000000000000000000000000000000000000000000000000000000000005550000000e000001000000000000000003e8000000010000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300c1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffef000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000fd03001200640300000000000000000000000000000003007400843d000000000000000000000000000000000000000000000000000000000000004d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000300000000000000000000000000000003001200630300000000000000000000000000000003007300853d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030200000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000703011200640100000000000000000000000000000003017400843900000000000000000000000000000000000000000000000000000000000000f100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000100000000000000000000000000000003011200630100000000000000000000000000000003017300853900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003020000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000030112004a0100000000000000000000000000000003015a008239000000000000000000000000000000000000000000000000000000000000000000000000000000ff008888888888888888888888888888888888888888888888888888888888880100000000000000000000000000000003011200640100000000000000000000000000000003017400843900000000000000000000000000000000000000000000000000000000000000fd0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000010000000000000000000000000000000301120063010000000000000000000000000000000301730085390000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000303000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000003001205ac030000000000000000000000000000000300ff00803d0000000000000000000000000000000000000000000000000000000000000000000000000000010000000000003e80ff0000000000000000000000000000000000000000000000000000000000000000000121000300000000000000000000000000000000000000000000000000000000000005550000000e000001000000000000000003e8000000010000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300c1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffef000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000fd03001200630300000000000000000000000000000003007300853d0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000303000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003001200640300000000000000000000000000000003007400843d00000000000000000000000000000000000000000000000000000000000000be00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000300000000000000000000000000000003001200630300000000000000000000000000000003007300853d000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000030400000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000703011200640100000000000000000000000000000003017400843900000000000000000000000000000000000000000000000000000000000000fc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000100000000000000000000000000000003011200630100000000000000000000000000000003017300853900000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003040000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000030112002c0100000000000000000000000000000003013c00833900000000000000000000000000000000000000000000000000000000000000000000000000000100000100000000000000000000000000000003011200640100000000000000000000000000000003017400843900000000000000000000000000000000000000000000000000000000000000fb000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000001000000000000000000000000000000030112006301000000000000000000000000000000030173008539000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000030500000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000703001200630300000000000000000000000000000003007300853d0000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000305000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003001200640300000000000000000000000000000003007400843d000000000000000000000000000000000000000000000000000000000000004f00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000300000000000000000000000000000003001205ac030000000000000000000000000000000300ff00803d00000000000000000000000000000000000000000000000000000000000000000000000000000200000000000b0838ff0000000000000000000000000000000000000000000000000000000000000000000121000300000000000000000000000000000000000000000000000000000000000005550000000e0000010000000000000003e800000000010000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300c1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffef000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000fd03001200a4030000000000000000000000000000000300b400843d00000000000000000000000000000000000000000000000000000000000000070000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010001c8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007f000000000000000300000000000000000000000000000003001200630300000000000000000000000000000003007300853d00000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000003060000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000070c007d0200000001390000000000000000000000000000000000000000000000000000000000000000000000000000008002000100000000000022002090000000000000000000000000000000000000000000000000000000000000006cc10000000000001600145c000000000000000000000000000000000000000500002000fd00fd0c005e0200000001fd00000000000000000000000000000000000000000000000000000000000000000000000000000000014f00000000000000220020f600000000000000000000000000000000000000000000000000000000000000000000000c00000c00000c00000c00000c000007").unwrap(), &(Arc::clone(&logger) as Arc<Logger>));
-
-               let log_entries = logger.lines.lock().unwrap();
-               assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendAcceptChannel event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000000 for channel ff4f00f805273c1b203bb5ebf8436bfde57b3be8c2f5e95d9491dbb181909679".to_string())), Some(&1)); // 1
-               assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendFundingSigned event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000000 for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1)); // 2
-               assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendFundingLocked event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000000 for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1)); // 3
-               assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendFundingLocked event in peer_handler for node 030200000000000000000000000000000000000000000000000000000000000000 for channel 3900000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1)); // 4
-               assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendRevokeAndACK event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000000 for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&4)); // 5
-               assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling UpdateHTLCs event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000000 with 0 adds, 0 fulfills, 0 fails for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&3)); // 6
-               assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling UpdateHTLCs event in peer_handler for node 030200000000000000000000000000000000000000000000000000000000000000 with 1 adds, 0 fulfills, 0 fails for channel 3900000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&3)); // 7
-               assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling UpdateHTLCs event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000000 with 0 adds, 1 fulfills, 0 fails for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1)); // 8
-               assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling UpdateHTLCs event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000000 with 0 adds, 0 fulfills, 1 fails for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&2)); // 9
-               assert_eq!(log_entries.get(&("lightning::ln::channelmonitor".to_string(), "Input spending remote commitment tx (00000000000000000000000000000000000000000000000000000000000000fd:0) in 0000000000000000000000000000000000000000000000000000000000000044 resolves outbound HTLC with payment hash ff00000000000000000000000000000000000000000000000000000000000000 with timeout".to_string())), Some(&1)); // 10
-       }
-}
diff --git a/fuzz/fuzz_targets/msg_targets/gen_target.sh b/fuzz/fuzz_targets/msg_targets/gen_target.sh
deleted file mode 100755 (executable)
index cfd100f..0000000
+++ /dev/null
@@ -1,36 +0,0 @@
-#!/bin/sh
-
-GEN_TEST() {
-       tn=$(echo $1 | sed 's/\([a-z0-9]\)\([A-Z]\)/\1_\2/g')
-       fn=msg_$(echo $tn | tr '[:upper:]' '[:lower:]')_target.rs
-       cat msg_target_template.txt | sed s/MSG_TARGET/$1/ | sed "s/TEST_MSG/$2/" | sed "s/EXTRA_ARGS/$3/" > $fn
-}
-
-GEN_TEST AcceptChannel test_msg ""
-GEN_TEST AnnouncementSignatures test_msg ""
-GEN_TEST ChannelReestablish test_msg ""
-GEN_TEST ClosingSigned test_msg ""
-GEN_TEST CommitmentSigned test_msg ""
-GEN_TEST DecodedOnionErrorPacket test_msg ""
-GEN_TEST FundingCreated test_msg ""
-GEN_TEST FundingLocked test_msg ""
-GEN_TEST FundingSigned test_msg ""
-GEN_TEST Init test_msg ""
-GEN_TEST OpenChannel test_msg ""
-GEN_TEST RevokeAndACK test_msg ""
-GEN_TEST Shutdown test_msg ""
-GEN_TEST UpdateFailHTLC test_msg ""
-GEN_TEST UpdateFailMalformedHTLC test_msg ""
-GEN_TEST UpdateFee test_msg ""
-GEN_TEST UpdateFulfillHTLC test_msg ""
-
-GEN_TEST ChannelAnnouncement test_msg_exact ""
-GEN_TEST ChannelUpdate test_msg_exact ""
-GEN_TEST NodeAnnouncement test_msg_exact ""
-
-GEN_TEST UpdateAddHTLC test_msg_hole ", 85, 33"
-GEN_TEST ErrorMessage test_msg_hole ", 32, 2"
-GEN_TEST OnionHopData test_msg_hole ", 1+8+8+4, 12"
-
-GEN_TEST Ping test_msg_simple ""
-GEN_TEST Pong test_msg_simple ""
diff --git a/fuzz/fuzz_targets/msg_targets/msg_accept_channel_target.rs b/fuzz/fuzz_targets/msg_targets/msg_accept_channel_target.rs
deleted file mode 100644 (file)
index 0f18d02..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-// This file is auto-generated by gen_target.sh based on msg_target_template.txt
-// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
-
-extern crate lightning;
-
-use lightning::ln::msgs;
-
-mod utils;
-use utils::VecWriter;
-
-#[inline]
-pub fn do_test(data: &[u8]) {
-       test_msg!(msgs::AcceptChannel, data);
-}
-
-#[cfg(feature = "afl")]
-#[macro_use] extern crate afl;
-#[cfg(feature = "afl")]
-fn main() {
-       fuzz!(|data| {
-               do_test(data);
-       });
-}
-
-#[cfg(feature = "honggfuzz")]
-#[macro_use] extern crate honggfuzz;
-#[cfg(feature = "honggfuzz")]
-fn main() {
-       loop {
-               fuzz!(|data| {
-                       do_test(data);
-               });
-       }
-}
-
-extern crate hex;
-#[cfg(test)]
-mod tests {
-       #[test]
-       fn duplicate_crash() {
-               super::do_test(&::hex::decode("00").unwrap());
-       }
-}
diff --git a/fuzz/fuzz_targets/msg_targets/msg_announcement_signatures_target.rs b/fuzz/fuzz_targets/msg_targets/msg_announcement_signatures_target.rs
deleted file mode 100644 (file)
index 226028e..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-// This file is auto-generated by gen_target.sh based on msg_target_template.txt
-// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
-
-extern crate lightning;
-
-use lightning::ln::msgs;
-
-mod utils;
-use utils::VecWriter;
-
-#[inline]
-pub fn do_test(data: &[u8]) {
-       test_msg!(msgs::AnnouncementSignatures, data);
-}
-
-#[cfg(feature = "afl")]
-#[macro_use] extern crate afl;
-#[cfg(feature = "afl")]
-fn main() {
-       fuzz!(|data| {
-               do_test(data);
-       });
-}
-
-#[cfg(feature = "honggfuzz")]
-#[macro_use] extern crate honggfuzz;
-#[cfg(feature = "honggfuzz")]
-fn main() {
-       loop {
-               fuzz!(|data| {
-                       do_test(data);
-               });
-       }
-}
-
-extern crate hex;
-#[cfg(test)]
-mod tests {
-       #[test]
-       fn duplicate_crash() {
-               super::do_test(&::hex::decode("00").unwrap());
-       }
-}
diff --git a/fuzz/fuzz_targets/msg_targets/msg_channel_announcement_target.rs b/fuzz/fuzz_targets/msg_targets/msg_channel_announcement_target.rs
deleted file mode 100644 (file)
index 0bdc10e..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-// This file is auto-generated by gen_target.sh based on msg_target_template.txt
-// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
-
-extern crate lightning;
-
-use lightning::ln::msgs;
-
-mod utils;
-use utils::VecWriter;
-
-#[inline]
-pub fn do_test(data: &[u8]) {
-       test_msg_exact!(msgs::ChannelAnnouncement, data);
-}
-
-#[cfg(feature = "afl")]
-#[macro_use] extern crate afl;
-#[cfg(feature = "afl")]
-fn main() {
-       fuzz!(|data| {
-               do_test(data);
-       });
-}
-
-#[cfg(feature = "honggfuzz")]
-#[macro_use] extern crate honggfuzz;
-#[cfg(feature = "honggfuzz")]
-fn main() {
-       loop {
-               fuzz!(|data| {
-                       do_test(data);
-               });
-       }
-}
-
-extern crate hex;
-#[cfg(test)]
-mod tests {
-       #[test]
-       fn duplicate_crash() {
-               super::do_test(&::hex::decode("00").unwrap());
-       }
-}
diff --git a/fuzz/fuzz_targets/msg_targets/msg_channel_reestablish_target.rs b/fuzz/fuzz_targets/msg_targets/msg_channel_reestablish_target.rs
deleted file mode 100644 (file)
index 4af5937..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-// This file is auto-generated by gen_target.sh based on msg_target_template.txt
-// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
-
-extern crate lightning;
-
-use lightning::ln::msgs;
-
-mod utils;
-use utils::VecWriter;
-
-#[inline]
-pub fn do_test(data: &[u8]) {
-       test_msg!(msgs::ChannelReestablish, data);
-}
-
-#[cfg(feature = "afl")]
-#[macro_use] extern crate afl;
-#[cfg(feature = "afl")]
-fn main() {
-       fuzz!(|data| {
-               do_test(data);
-       });
-}
-
-#[cfg(feature = "honggfuzz")]
-#[macro_use] extern crate honggfuzz;
-#[cfg(feature = "honggfuzz")]
-fn main() {
-       loop {
-               fuzz!(|data| {
-                       do_test(data);
-               });
-       }
-}
-
-extern crate hex;
-#[cfg(test)]
-mod tests {
-       #[test]
-       fn duplicate_crash() {
-               super::do_test(&::hex::decode("00").unwrap());
-       }
-}
diff --git a/fuzz/fuzz_targets/msg_targets/msg_channel_update_target.rs b/fuzz/fuzz_targets/msg_targets/msg_channel_update_target.rs
deleted file mode 100644 (file)
index 724dca4..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-// This file is auto-generated by gen_target.sh based on msg_target_template.txt
-// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
-
-extern crate lightning;
-
-use lightning::ln::msgs;
-
-mod utils;
-use utils::VecWriter;
-
-#[inline]
-pub fn do_test(data: &[u8]) {
-       test_msg_exact!(msgs::ChannelUpdate, data);
-}
-
-#[cfg(feature = "afl")]
-#[macro_use] extern crate afl;
-#[cfg(feature = "afl")]
-fn main() {
-       fuzz!(|data| {
-               do_test(data);
-       });
-}
-
-#[cfg(feature = "honggfuzz")]
-#[macro_use] extern crate honggfuzz;
-#[cfg(feature = "honggfuzz")]
-fn main() {
-       loop {
-               fuzz!(|data| {
-                       do_test(data);
-               });
-       }
-}
-
-extern crate hex;
-#[cfg(test)]
-mod tests {
-       #[test]
-       fn duplicate_crash() {
-               super::do_test(&::hex::decode("00").unwrap());
-       }
-}
diff --git a/fuzz/fuzz_targets/msg_targets/msg_closing_signed_target.rs b/fuzz/fuzz_targets/msg_targets/msg_closing_signed_target.rs
deleted file mode 100644 (file)
index faeeae3..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-// This file is auto-generated by gen_target.sh based on msg_target_template.txt
-// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
-
-extern crate lightning;
-
-use lightning::ln::msgs;
-
-mod utils;
-use utils::VecWriter;
-
-#[inline]
-pub fn do_test(data: &[u8]) {
-       test_msg!(msgs::ClosingSigned, data);
-}
-
-#[cfg(feature = "afl")]
-#[macro_use] extern crate afl;
-#[cfg(feature = "afl")]
-fn main() {
-       fuzz!(|data| {
-               do_test(data);
-       });
-}
-
-#[cfg(feature = "honggfuzz")]
-#[macro_use] extern crate honggfuzz;
-#[cfg(feature = "honggfuzz")]
-fn main() {
-       loop {
-               fuzz!(|data| {
-                       do_test(data);
-               });
-       }
-}
-
-extern crate hex;
-#[cfg(test)]
-mod tests {
-       #[test]
-       fn duplicate_crash() {
-               super::do_test(&::hex::decode("00").unwrap());
-       }
-}
diff --git a/fuzz/fuzz_targets/msg_targets/msg_commitment_signed_target.rs b/fuzz/fuzz_targets/msg_targets/msg_commitment_signed_target.rs
deleted file mode 100644 (file)
index 97c4b30..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-// This file is auto-generated by gen_target.sh based on msg_target_template.txt
-// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
-
-extern crate lightning;
-
-use lightning::ln::msgs;
-
-mod utils;
-use utils::VecWriter;
-
-#[inline]
-pub fn do_test(data: &[u8]) {
-       test_msg!(msgs::CommitmentSigned, data);
-}
-
-#[cfg(feature = "afl")]
-#[macro_use] extern crate afl;
-#[cfg(feature = "afl")]
-fn main() {
-       fuzz!(|data| {
-               do_test(data);
-       });
-}
-
-#[cfg(feature = "honggfuzz")]
-#[macro_use] extern crate honggfuzz;
-#[cfg(feature = "honggfuzz")]
-fn main() {
-       loop {
-               fuzz!(|data| {
-                       do_test(data);
-               });
-       }
-}
-
-extern crate hex;
-#[cfg(test)]
-mod tests {
-       #[test]
-       fn duplicate_crash() {
-               super::do_test(&::hex::decode("00").unwrap());
-       }
-}
diff --git a/fuzz/fuzz_targets/msg_targets/msg_decoded_onion_error_packet_target.rs b/fuzz/fuzz_targets/msg_targets/msg_decoded_onion_error_packet_target.rs
deleted file mode 100644 (file)
index 9b190b0..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-// This file is auto-generated by gen_target.sh based on msg_target_template.txt
-// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
-
-extern crate lightning;
-
-use lightning::ln::msgs;
-
-mod utils;
-use utils::VecWriter;
-
-#[inline]
-pub fn do_test(data: &[u8]) {
-       test_msg!(msgs::DecodedOnionErrorPacket, data);
-}
-
-#[cfg(feature = "afl")]
-#[macro_use] extern crate afl;
-#[cfg(feature = "afl")]
-fn main() {
-       fuzz!(|data| {
-               do_test(data);
-       });
-}
-
-#[cfg(feature = "honggfuzz")]
-#[macro_use] extern crate honggfuzz;
-#[cfg(feature = "honggfuzz")]
-fn main() {
-       loop {
-               fuzz!(|data| {
-                       do_test(data);
-               });
-       }
-}
-
-extern crate hex;
-#[cfg(test)]
-mod tests {
-       #[test]
-       fn duplicate_crash() {
-               super::do_test(&::hex::decode("00").unwrap());
-       }
-}
diff --git a/fuzz/fuzz_targets/msg_targets/msg_error_message_target.rs b/fuzz/fuzz_targets/msg_targets/msg_error_message_target.rs
deleted file mode 100644 (file)
index d749dc9..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-// This file is auto-generated by gen_target.sh based on msg_target_template.txt
-// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
-
-extern crate lightning;
-
-use lightning::ln::msgs;
-
-mod utils;
-use utils::VecWriter;
-
-#[inline]
-pub fn do_test(data: &[u8]) {
-       test_msg_hole!(msgs::ErrorMessage, data, 32, 2);
-}
-
-#[cfg(feature = "afl")]
-#[macro_use] extern crate afl;
-#[cfg(feature = "afl")]
-fn main() {
-       fuzz!(|data| {
-               do_test(data);
-       });
-}
-
-#[cfg(feature = "honggfuzz")]
-#[macro_use] extern crate honggfuzz;
-#[cfg(feature = "honggfuzz")]
-fn main() {
-       loop {
-               fuzz!(|data| {
-                       do_test(data);
-               });
-       }
-}
-
-extern crate hex;
-#[cfg(test)]
-mod tests {
-       #[test]
-       fn duplicate_crash() {
-               super::do_test(&::hex::decode("00").unwrap());
-       }
-}
diff --git a/fuzz/fuzz_targets/msg_targets/msg_funding_created_target.rs b/fuzz/fuzz_targets/msg_targets/msg_funding_created_target.rs
deleted file mode 100644 (file)
index 45b257b..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-// This file is auto-generated by gen_target.sh based on msg_target_template.txt
-// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
-
-extern crate lightning;
-
-use lightning::ln::msgs;
-
-mod utils;
-use utils::VecWriter;
-
-#[inline]
-pub fn do_test(data: &[u8]) {
-       test_msg!(msgs::FundingCreated, data);
-}
-
-#[cfg(feature = "afl")]
-#[macro_use] extern crate afl;
-#[cfg(feature = "afl")]
-fn main() {
-       fuzz!(|data| {
-               do_test(data);
-       });
-}
-
-#[cfg(feature = "honggfuzz")]
-#[macro_use] extern crate honggfuzz;
-#[cfg(feature = "honggfuzz")]
-fn main() {
-       loop {
-               fuzz!(|data| {
-                       do_test(data);
-               });
-       }
-}
-
-extern crate hex;
-#[cfg(test)]
-mod tests {
-       #[test]
-       fn duplicate_crash() {
-               super::do_test(&::hex::decode("00").unwrap());
-       }
-}
diff --git a/fuzz/fuzz_targets/msg_targets/msg_funding_locked_target.rs b/fuzz/fuzz_targets/msg_targets/msg_funding_locked_target.rs
deleted file mode 100644 (file)
index cd1e897..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-// This file is auto-generated by gen_target.sh based on msg_target_template.txt
-// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
-
-extern crate lightning;
-
-use lightning::ln::msgs;
-
-mod utils;
-use utils::VecWriter;
-
-#[inline]
-pub fn do_test(data: &[u8]) {
-       test_msg!(msgs::FundingLocked, data);
-}
-
-#[cfg(feature = "afl")]
-#[macro_use] extern crate afl;
-#[cfg(feature = "afl")]
-fn main() {
-       fuzz!(|data| {
-               do_test(data);
-       });
-}
-
-#[cfg(feature = "honggfuzz")]
-#[macro_use] extern crate honggfuzz;
-#[cfg(feature = "honggfuzz")]
-fn main() {
-       loop {
-               fuzz!(|data| {
-                       do_test(data);
-               });
-       }
-}
-
-extern crate hex;
-#[cfg(test)]
-mod tests {
-       #[test]
-       fn duplicate_crash() {
-               super::do_test(&::hex::decode("00").unwrap());
-       }
-}
diff --git a/fuzz/fuzz_targets/msg_targets/msg_funding_signed_target.rs b/fuzz/fuzz_targets/msg_targets/msg_funding_signed_target.rs
deleted file mode 100644 (file)
index 5992d69..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-// This file is auto-generated by gen_target.sh based on msg_target_template.txt
-// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
-
-extern crate lightning;
-
-use lightning::ln::msgs;
-
-mod utils;
-use utils::VecWriter;
-
-#[inline]
-pub fn do_test(data: &[u8]) {
-       test_msg!(msgs::FundingSigned, data);
-}
-
-#[cfg(feature = "afl")]
-#[macro_use] extern crate afl;
-#[cfg(feature = "afl")]
-fn main() {
-       fuzz!(|data| {
-               do_test(data);
-       });
-}
-
-#[cfg(feature = "honggfuzz")]
-#[macro_use] extern crate honggfuzz;
-#[cfg(feature = "honggfuzz")]
-fn main() {
-       loop {
-               fuzz!(|data| {
-                       do_test(data);
-               });
-       }
-}
-
-extern crate hex;
-#[cfg(test)]
-mod tests {
-       #[test]
-       fn duplicate_crash() {
-               super::do_test(&::hex::decode("00").unwrap());
-       }
-}
diff --git a/fuzz/fuzz_targets/msg_targets/msg_init_target.rs b/fuzz/fuzz_targets/msg_targets/msg_init_target.rs
deleted file mode 100644 (file)
index cdca848..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-// This file is auto-generated by gen_target.sh based on msg_target_template.txt
-// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
-
-extern crate lightning;
-
-use lightning::ln::msgs;
-
-mod utils;
-use utils::VecWriter;
-
-#[inline]
-pub fn do_test(data: &[u8]) {
-       test_msg!(msgs::Init, data);
-}
-
-#[cfg(feature = "afl")]
-#[macro_use] extern crate afl;
-#[cfg(feature = "afl")]
-fn main() {
-       fuzz!(|data| {
-               do_test(data);
-       });
-}
-
-#[cfg(feature = "honggfuzz")]
-#[macro_use] extern crate honggfuzz;
-#[cfg(feature = "honggfuzz")]
-fn main() {
-       loop {
-               fuzz!(|data| {
-                       do_test(data);
-               });
-       }
-}
-
-extern crate hex;
-#[cfg(test)]
-mod tests {
-       #[test]
-       fn duplicate_crash() {
-               super::do_test(&::hex::decode("00").unwrap());
-       }
-}
diff --git a/fuzz/fuzz_targets/msg_targets/msg_node_announcement_target.rs b/fuzz/fuzz_targets/msg_targets/msg_node_announcement_target.rs
deleted file mode 100644 (file)
index f0a7a4c..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-// This file is auto-generated by gen_target.sh based on msg_target_template.txt
-// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
-
-extern crate lightning;
-
-use lightning::ln::msgs;
-
-mod utils;
-use utils::VecWriter;
-
-#[inline]
-pub fn do_test(data: &[u8]) {
-       test_msg_exact!(msgs::NodeAnnouncement, data);
-}
-
-#[cfg(feature = "afl")]
-#[macro_use] extern crate afl;
-#[cfg(feature = "afl")]
-fn main() {
-       fuzz!(|data| {
-               do_test(data);
-       });
-}
-
-#[cfg(feature = "honggfuzz")]
-#[macro_use] extern crate honggfuzz;
-#[cfg(feature = "honggfuzz")]
-fn main() {
-       loop {
-               fuzz!(|data| {
-                       do_test(data);
-               });
-       }
-}
-
-extern crate hex;
-#[cfg(test)]
-mod tests {
-       #[test]
-       fn duplicate_crash() {
-               super::do_test(&::hex::decode("00").unwrap());
-       }
-}
diff --git a/fuzz/fuzz_targets/msg_targets/msg_onion_hop_data_target.rs b/fuzz/fuzz_targets/msg_targets/msg_onion_hop_data_target.rs
deleted file mode 100644 (file)
index 058c050..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-// This file is auto-generated by gen_target.sh based on msg_target_template.txt
-// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
-
-extern crate lightning;
-
-use lightning::ln::msgs;
-
-mod utils;
-use utils::VecWriter;
-
-#[inline]
-pub fn do_test(data: &[u8]) {
-       test_msg_hole!(msgs::OnionHopData, data, 1+8+8+4, 12);
-}
-
-#[cfg(feature = "afl")]
-#[macro_use] extern crate afl;
-#[cfg(feature = "afl")]
-fn main() {
-       fuzz!(|data| {
-               do_test(data);
-       });
-}
-
-#[cfg(feature = "honggfuzz")]
-#[macro_use] extern crate honggfuzz;
-#[cfg(feature = "honggfuzz")]
-fn main() {
-       loop {
-               fuzz!(|data| {
-                       do_test(data);
-               });
-       }
-}
-
-extern crate hex;
-#[cfg(test)]
-mod tests {
-       #[test]
-       fn duplicate_crash() {
-               super::do_test(&::hex::decode("00").unwrap());
-       }
-}
diff --git a/fuzz/fuzz_targets/msg_targets/msg_open_channel_target.rs b/fuzz/fuzz_targets/msg_targets/msg_open_channel_target.rs
deleted file mode 100644 (file)
index aa13e96..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-// This file is auto-generated by gen_target.sh based on msg_target_template.txt
-// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
-
-extern crate lightning;
-
-use lightning::ln::msgs;
-
-mod utils;
-use utils::VecWriter;
-
-#[inline]
-pub fn do_test(data: &[u8]) {
-       test_msg!(msgs::OpenChannel, data);
-}
-
-#[cfg(feature = "afl")]
-#[macro_use] extern crate afl;
-#[cfg(feature = "afl")]
-fn main() {
-       fuzz!(|data| {
-               do_test(data);
-       });
-}
-
-#[cfg(feature = "honggfuzz")]
-#[macro_use] extern crate honggfuzz;
-#[cfg(feature = "honggfuzz")]
-fn main() {
-       loop {
-               fuzz!(|data| {
-                       do_test(data);
-               });
-       }
-}
-
-extern crate hex;
-#[cfg(test)]
-mod tests {
-       #[test]
-       fn duplicate_crash() {
-               super::do_test(&::hex::decode("00").unwrap());
-       }
-}
diff --git a/fuzz/fuzz_targets/msg_targets/msg_ping_target.rs b/fuzz/fuzz_targets/msg_targets/msg_ping_target.rs
deleted file mode 100644 (file)
index d2ea913..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-// This file is auto-generated by gen_target.sh based on msg_target_template.txt
-// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
-
-extern crate lightning;
-
-use lightning::ln::msgs;
-
-mod utils;
-use utils::VecWriter;
-
-#[inline]
-pub fn do_test(data: &[u8]) {
-       test_msg_simple!(msgs::Ping, data);
-}
-
-#[cfg(feature = "afl")]
-#[macro_use] extern crate afl;
-#[cfg(feature = "afl")]
-fn main() {
-       fuzz!(|data| {
-               do_test(data);
-       });
-}
-
-#[cfg(feature = "honggfuzz")]
-#[macro_use] extern crate honggfuzz;
-#[cfg(feature = "honggfuzz")]
-fn main() {
-       loop {
-               fuzz!(|data| {
-                       do_test(data);
-               });
-       }
-}
-
-extern crate hex;
-#[cfg(test)]
-mod tests {
-       #[test]
-       fn duplicate_crash() {
-               super::do_test(&::hex::decode("00").unwrap());
-       }
-}
diff --git a/fuzz/fuzz_targets/msg_targets/msg_pong_target.rs b/fuzz/fuzz_targets/msg_targets/msg_pong_target.rs
deleted file mode 100644 (file)
index 18120e2..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-// This file is auto-generated by gen_target.sh based on msg_target_template.txt
-// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
-
-extern crate lightning;
-
-use lightning::ln::msgs;
-
-mod utils;
-use utils::VecWriter;
-
-#[inline]
-pub fn do_test(data: &[u8]) {
-       test_msg_simple!(msgs::Pong, data);
-}
-
-#[cfg(feature = "afl")]
-#[macro_use] extern crate afl;
-#[cfg(feature = "afl")]
-fn main() {
-       fuzz!(|data| {
-               do_test(data);
-       });
-}
-
-#[cfg(feature = "honggfuzz")]
-#[macro_use] extern crate honggfuzz;
-#[cfg(feature = "honggfuzz")]
-fn main() {
-       loop {
-               fuzz!(|data| {
-                       do_test(data);
-               });
-       }
-}
-
-extern crate hex;
-#[cfg(test)]
-mod tests {
-       #[test]
-       fn duplicate_crash() {
-               super::do_test(&::hex::decode("00").unwrap());
-       }
-}
diff --git a/fuzz/fuzz_targets/msg_targets/msg_revoke_and_ack_target.rs b/fuzz/fuzz_targets/msg_targets/msg_revoke_and_ack_target.rs
deleted file mode 100644 (file)
index d82268d..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-// This file is auto-generated by gen_target.sh based on msg_target_template.txt
-// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
-
-extern crate lightning;
-
-use lightning::ln::msgs;
-
-mod utils;
-use utils::VecWriter;
-
-#[inline]
-pub fn do_test(data: &[u8]) {
-       test_msg!(msgs::RevokeAndACK, data);
-}
-
-#[cfg(feature = "afl")]
-#[macro_use] extern crate afl;
-#[cfg(feature = "afl")]
-fn main() {
-       fuzz!(|data| {
-               do_test(data);
-       });
-}
-
-#[cfg(feature = "honggfuzz")]
-#[macro_use] extern crate honggfuzz;
-#[cfg(feature = "honggfuzz")]
-fn main() {
-       loop {
-               fuzz!(|data| {
-                       do_test(data);
-               });
-       }
-}
-
-extern crate hex;
-#[cfg(test)]
-mod tests {
-       #[test]
-       fn duplicate_crash() {
-               super::do_test(&::hex::decode("00").unwrap());
-       }
-}
diff --git a/fuzz/fuzz_targets/msg_targets/msg_shutdown_target.rs b/fuzz/fuzz_targets/msg_targets/msg_shutdown_target.rs
deleted file mode 100644 (file)
index 34d4d20..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-// This file is auto-generated by gen_target.sh based on msg_target_template.txt
-// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
-
-extern crate lightning;
-
-use lightning::ln::msgs;
-
-mod utils;
-use utils::VecWriter;
-
-#[inline]
-pub fn do_test(data: &[u8]) {
-       test_msg!(msgs::Shutdown, data);
-}
-
-#[cfg(feature = "afl")]
-#[macro_use] extern crate afl;
-#[cfg(feature = "afl")]
-fn main() {
-       fuzz!(|data| {
-               do_test(data);
-       });
-}
-
-#[cfg(feature = "honggfuzz")]
-#[macro_use] extern crate honggfuzz;
-#[cfg(feature = "honggfuzz")]
-fn main() {
-       loop {
-               fuzz!(|data| {
-                       do_test(data);
-               });
-       }
-}
-
-extern crate hex;
-#[cfg(test)]
-mod tests {
-       #[test]
-       fn duplicate_crash() {
-               super::do_test(&::hex::decode("00").unwrap());
-       }
-}
diff --git a/fuzz/fuzz_targets/msg_targets/msg_target_template.txt b/fuzz/fuzz_targets/msg_targets/msg_target_template.txt
deleted file mode 100644 (file)
index 2704bcd..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-// This file is auto-generated by gen_target.sh based on msg_target_template.txt
-// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
-
-extern crate lightning;
-
-use lightning::ln::msgs;
-
-mod utils;
-use utils::VecWriter;
-
-#[inline]
-pub fn do_test(data: &[u8]) {
-       TEST_MSG!(msgs::MSG_TARGET, dataEXTRA_ARGS);
-}
-
-#[cfg(feature = "afl")]
-#[macro_use] extern crate afl;
-#[cfg(feature = "afl")]
-fn main() {
-       fuzz!(|data| {
-               do_test(data);
-       });
-}
-
-#[cfg(feature = "honggfuzz")]
-#[macro_use] extern crate honggfuzz;
-#[cfg(feature = "honggfuzz")]
-fn main() {
-       loop {
-               fuzz!(|data| {
-                       do_test(data);
-               });
-       }
-}
-
-extern crate hex;
-#[cfg(test)]
-mod tests {
-       #[test]
-       fn duplicate_crash() {
-               super::do_test(&::hex::decode("00").unwrap());
-       }
-}
diff --git a/fuzz/fuzz_targets/msg_targets/msg_update_add_htlc_target.rs b/fuzz/fuzz_targets/msg_targets/msg_update_add_htlc_target.rs
deleted file mode 100644 (file)
index e64a5c2..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-// This file is auto-generated by gen_target.sh based on msg_target_template.txt
-// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
-
-extern crate lightning;
-
-use lightning::ln::msgs;
-
-mod utils;
-use utils::VecWriter;
-
-#[inline]
-pub fn do_test(data: &[u8]) {
-       test_msg_hole!(msgs::UpdateAddHTLC, data, 85, 33);
-}
-
-#[cfg(feature = "afl")]
-#[macro_use] extern crate afl;
-#[cfg(feature = "afl")]
-fn main() {
-       fuzz!(|data| {
-               do_test(data);
-       });
-}
-
-#[cfg(feature = "honggfuzz")]
-#[macro_use] extern crate honggfuzz;
-#[cfg(feature = "honggfuzz")]
-fn main() {
-       loop {
-               fuzz!(|data| {
-                       do_test(data);
-               });
-       }
-}
-
-extern crate hex;
-#[cfg(test)]
-mod tests {
-       #[test]
-       fn duplicate_crash() {
-               super::do_test(&::hex::decode("00").unwrap());
-       }
-}
diff --git a/fuzz/fuzz_targets/msg_targets/msg_update_fail_htlc_target.rs b/fuzz/fuzz_targets/msg_targets/msg_update_fail_htlc_target.rs
deleted file mode 100644 (file)
index fedce56..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-// This file is auto-generated by gen_target.sh based on msg_target_template.txt
-// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
-
-extern crate lightning;
-
-use lightning::ln::msgs;
-
-mod utils;
-use utils::VecWriter;
-
-#[inline]
-pub fn do_test(data: &[u8]) {
-       test_msg!(msgs::UpdateFailHTLC, data);
-}
-
-#[cfg(feature = "afl")]
-#[macro_use] extern crate afl;
-#[cfg(feature = "afl")]
-fn main() {
-       fuzz!(|data| {
-               do_test(data);
-       });
-}
-
-#[cfg(feature = "honggfuzz")]
-#[macro_use] extern crate honggfuzz;
-#[cfg(feature = "honggfuzz")]
-fn main() {
-       loop {
-               fuzz!(|data| {
-                       do_test(data);
-               });
-       }
-}
-
-extern crate hex;
-#[cfg(test)]
-mod tests {
-       #[test]
-       fn duplicate_crash() {
-               super::do_test(&::hex::decode("00").unwrap());
-       }
-}
diff --git a/fuzz/fuzz_targets/msg_targets/msg_update_fail_malformed_htlc_target.rs b/fuzz/fuzz_targets/msg_targets/msg_update_fail_malformed_htlc_target.rs
deleted file mode 100644 (file)
index 377378f..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-// This file is auto-generated by gen_target.sh based on msg_target_template.txt
-// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
-
-extern crate lightning;
-
-use lightning::ln::msgs;
-
-mod utils;
-use utils::VecWriter;
-
-#[inline]
-pub fn do_test(data: &[u8]) {
-       test_msg!(msgs::UpdateFailMalformedHTLC, data);
-}
-
-#[cfg(feature = "afl")]
-#[macro_use] extern crate afl;
-#[cfg(feature = "afl")]
-fn main() {
-       fuzz!(|data| {
-               do_test(data);
-       });
-}
-
-#[cfg(feature = "honggfuzz")]
-#[macro_use] extern crate honggfuzz;
-#[cfg(feature = "honggfuzz")]
-fn main() {
-       loop {
-               fuzz!(|data| {
-                       do_test(data);
-               });
-       }
-}
-
-extern crate hex;
-#[cfg(test)]
-mod tests {
-       #[test]
-       fn duplicate_crash() {
-               super::do_test(&::hex::decode("00").unwrap());
-       }
-}
diff --git a/fuzz/fuzz_targets/msg_targets/msg_update_fee_target.rs b/fuzz/fuzz_targets/msg_targets/msg_update_fee_target.rs
deleted file mode 100644 (file)
index 56b9ac4..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-// This file is auto-generated by gen_target.sh based on msg_target_template.txt
-// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
-
-extern crate lightning;
-
-use lightning::ln::msgs;
-
-mod utils;
-use utils::VecWriter;
-
-#[inline]
-pub fn do_test(data: &[u8]) {
-       test_msg!(msgs::UpdateFee, data);
-}
-
-#[cfg(feature = "afl")]
-#[macro_use] extern crate afl;
-#[cfg(feature = "afl")]
-fn main() {
-       fuzz!(|data| {
-               do_test(data);
-       });
-}
-
-#[cfg(feature = "honggfuzz")]
-#[macro_use] extern crate honggfuzz;
-#[cfg(feature = "honggfuzz")]
-fn main() {
-       loop {
-               fuzz!(|data| {
-                       do_test(data);
-               });
-       }
-}
-
-extern crate hex;
-#[cfg(test)]
-mod tests {
-       #[test]
-       fn duplicate_crash() {
-               super::do_test(&::hex::decode("00").unwrap());
-       }
-}
diff --git a/fuzz/fuzz_targets/msg_targets/msg_update_fulfill_htlc_target.rs b/fuzz/fuzz_targets/msg_targets/msg_update_fulfill_htlc_target.rs
deleted file mode 100644 (file)
index f0c936d..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-// This file is auto-generated by gen_target.sh based on msg_target_template.txt
-// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
-
-extern crate lightning;
-
-use lightning::ln::msgs;
-
-mod utils;
-use utils::VecWriter;
-
-#[inline]
-pub fn do_test(data: &[u8]) {
-       test_msg!(msgs::UpdateFulfillHTLC, data);
-}
-
-#[cfg(feature = "afl")]
-#[macro_use] extern crate afl;
-#[cfg(feature = "afl")]
-fn main() {
-       fuzz!(|data| {
-               do_test(data);
-       });
-}
-
-#[cfg(feature = "honggfuzz")]
-#[macro_use] extern crate honggfuzz;
-#[cfg(feature = "honggfuzz")]
-fn main() {
-       loop {
-               fuzz!(|data| {
-                       do_test(data);
-               });
-       }
-}
-
-extern crate hex;
-#[cfg(test)]
-mod tests {
-       #[test]
-       fn duplicate_crash() {
-               super::do_test(&::hex::decode("00").unwrap());
-       }
-}
diff --git a/fuzz/fuzz_targets/msg_targets/utils.rs b/fuzz/fuzz_targets/msg_targets/utils.rs
deleted file mode 100644 (file)
index a5257ba..0000000
+++ /dev/null
@@ -1,81 +0,0 @@
-#![macro_use]
-
-use lightning::util::ser::Writer;
-pub struct VecWriter(pub Vec<u8>);
-impl Writer for VecWriter {
-       fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
-               assert!(self.0.capacity() >= self.0.len() + buf.len());
-               self.0.extend_from_slice(buf);
-               Ok(())
-       }
-       fn size_hint(&mut self, size: usize) {
-               self.0.reserve_exact(size);
-       }
-}
-
-#[macro_export]
-macro_rules! test_msg {
-       ($MsgType: path, $data: ident) => {
-               {
-                       use lightning::util::ser::{Writeable, Readable};
-                       let mut r = ::std::io::Cursor::new($data);
-                       if let Ok(msg) = <$MsgType as Readable<::std::io::Cursor<&[u8]>>>::read(&mut r) {
-                               let p = r.position() as usize;
-                               let mut w = VecWriter(Vec::new());
-                               msg.write(&mut w).unwrap();
-
-                               assert_eq!(w.0.len(), p);
-                               assert_eq!(&r.into_inner()[..p], &w.0[..p]);
-                       }
-               }
-       }
-}
-
-#[macro_export]
-macro_rules! test_msg_simple {
-       ($MsgType: path, $data: ident) => {
-               {
-                       use lightning::util::ser::{Writeable, Readable};
-                       let mut r = ::std::io::Cursor::new($data);
-                       if let Ok(msg) = <$MsgType as Readable<::std::io::Cursor<&[u8]>>>::read(&mut r) {
-                               let mut w = VecWriter(Vec::new());
-                               msg.write(&mut w).unwrap();
-                       }
-               }
-       }
-}
-
-#[macro_export]
-macro_rules! test_msg_exact {
-       ($MsgType: path, $data: ident) => {
-               {
-                       use lightning::util::ser::{Writeable, Readable};
-                       let mut r = ::std::io::Cursor::new($data);
-                       if let Ok(msg) = <$MsgType as Readable<::std::io::Cursor<&[u8]>>>::read(&mut r) {
-                               let mut w = VecWriter(Vec::new());
-                               msg.write(&mut w).unwrap();
-
-                               assert_eq!(&r.into_inner()[..], &w.0[..]);
-                       }
-               }
-       }
-}
-
-#[macro_export]
-macro_rules! test_msg_hole {
-       ($MsgType: path, $data: ident, $hole: expr, $hole_len: expr) => {
-               {
-                       use lightning::util::ser::{Writeable, Readable};
-                       let mut r = ::std::io::Cursor::new($data);
-                       if let Ok(msg) = <$MsgType as Readable<::std::io::Cursor<&[u8]>>>::read(&mut r) {
-                               let mut w = VecWriter(Vec::new());
-                               msg.write(&mut w).unwrap();
-                               let p = w.0.len() as usize;
-
-                               assert_eq!(w.0.len(), p);
-                               assert_eq!(&r.get_ref()[..$hole], &w.0[..$hole]);
-                               assert_eq!(&r.get_ref()[$hole+$hole_len..p], &w.0[$hole+$hole_len..]);
-                       }
-               }
-       }
-}
diff --git a/fuzz/fuzz_targets/peer_crypt_target.rs b/fuzz/fuzz_targets/peer_crypt_target.rs
deleted file mode 100644 (file)
index 0b82303..0000000
+++ /dev/null
@@ -1,109 +0,0 @@
-extern crate lightning;
-extern crate secp256k1;
-
-use lightning::ln::peer_channel_encryptor::PeerChannelEncryptor;
-
-use secp256k1::key::{PublicKey,SecretKey};
-
-#[inline]
-fn slice_to_be16(v: &[u8]) -> u16 {
-       ((v[0] as u16) << 8*1) |
-       ((v[1] as u16) << 8*0)
-}
-
-#[inline]
-pub fn do_test(data: &[u8]) {
-       let mut read_pos = 0;
-       macro_rules! get_slice {
-               ($len: expr) => {
-                       {
-                               let slice_len = $len as usize;
-                               if data.len() < read_pos + slice_len {
-                                       return;
-                               }
-                               read_pos += slice_len;
-                               &data[read_pos - slice_len..read_pos]
-                       }
-               }
-       }
-
-       let our_network_key = match SecretKey::from_slice(get_slice!(32)) {
-               Ok(key) => key,
-               Err(_) => return,
-       };
-       let ephemeral_key = match SecretKey::from_slice(get_slice!(32)) {
-               Ok(key) => key,
-               Err(_) => return,
-       };
-
-       let mut crypter = if get_slice!(1)[0] != 0 {
-               let their_pubkey = match PublicKey::from_slice(get_slice!(33)) {
-                       Ok(key) => key,
-                       Err(_) => return,
-               };
-               let mut crypter = PeerChannelEncryptor::new_outbound(their_pubkey, ephemeral_key);
-               crypter.get_act_one();
-               match crypter.process_act_two(get_slice!(50), &our_network_key) {
-                       Ok(_) => {},
-                       Err(_) => return,
-               }
-               assert!(crypter.is_ready_for_encryption());
-               crypter
-       } else {
-               let mut crypter = PeerChannelEncryptor::new_inbound(&our_network_key);
-               match crypter.process_act_one_with_keys(get_slice!(50), &our_network_key, ephemeral_key) {
-                       Ok(_) => {},
-                       Err(_) => return,
-               }
-               match crypter.process_act_three(get_slice!(66)) {
-                       Ok(_) => {},
-                       Err(_) => return,
-               }
-               assert!(crypter.is_ready_for_encryption());
-               crypter
-       };
-       loop {
-               if get_slice!(1)[0] == 0 {
-                       crypter.encrypt_message(get_slice!(slice_to_be16(get_slice!(2))));
-               } else {
-                       let len = match crypter.decrypt_length_header(get_slice!(16+2)) {
-                               Ok(len) => len,
-                               Err(_) => return,
-                       };
-                       match crypter.decrypt_message(get_slice!(len as usize + 16)) {
-                               Ok(_) => {},
-                               Err(_) => return,
-                       }
-               }
-       }
-}
-
-#[cfg(feature = "afl")]
-#[macro_use] extern crate afl;
-#[cfg(feature = "afl")]
-fn main() {
-       fuzz!(|data| {
-               do_test(data);
-       });
-}
-
-#[cfg(feature = "honggfuzz")]
-#[macro_use] extern crate honggfuzz;
-#[cfg(feature = "honggfuzz")]
-fn main() {
-       loop {
-               fuzz!(|data| {
-                       do_test(data);
-               });
-       }
-}
-
-extern crate hex;
-#[cfg(test)]
-mod tests {
-
-       #[test]
-       fn duplicate_crash() {
-               super::do_test(&::hex::decode("01").unwrap());
-       }
-}
diff --git a/fuzz/fuzz_targets/router_target.rs b/fuzz/fuzz_targets/router_target.rs
deleted file mode 100644 (file)
index 25bf382..0000000
+++ /dev/null
@@ -1,264 +0,0 @@
-extern crate bitcoin;
-extern crate bitcoin_hashes;
-extern crate lightning;
-extern crate secp256k1;
-
-use bitcoin_hashes::sha256d::Hash as Sha256dHash;
-use bitcoin::blockdata::script::{Script, Builder};
-
-use lightning::chain::chaininterface::{ChainError,ChainWatchInterface, ChainListener};
-use lightning::ln::channelmanager::ChannelDetails;
-use lightning::ln::msgs;
-use lightning::ln::msgs::{RoutingMessageHandler};
-use lightning::ln::router::{Router, RouteHint};
-use lightning::util::logger::Logger;
-use lightning::util::ser::Readable;
-
-use secp256k1::key::PublicKey;
-
-mod utils;
-
-use utils::test_logger;
-
-use std::sync::{Weak, Arc};
-use std::sync::atomic::{AtomicUsize, Ordering};
-
-#[inline]
-pub fn slice_to_be16(v: &[u8]) -> u16 {
-       ((v[0] as u16) << 8*1) |
-       ((v[1] as u16) << 8*0)
-}
-
-#[inline]
-pub fn slice_to_be32(v: &[u8]) -> u32 {
-       ((v[0] as u32) << 8*3) |
-       ((v[1] as u32) << 8*2) |
-       ((v[2] as u32) << 8*1) |
-       ((v[3] as u32) << 8*0)
-}
-
-#[inline]
-pub fn slice_to_be64(v: &[u8]) -> u64 {
-       ((v[0] as u64) << 8*7) |
-       ((v[1] as u64) << 8*6) |
-       ((v[2] as u64) << 8*5) |
-       ((v[3] as u64) << 8*4) |
-       ((v[4] as u64) << 8*3) |
-       ((v[5] as u64) << 8*2) |
-       ((v[6] as u64) << 8*1) |
-       ((v[7] as u64) << 8*0)
-}
-
-
-struct InputData {
-       data: Vec<u8>,
-       read_pos: AtomicUsize,
-}
-impl InputData {
-       fn get_slice(&self, len: usize) -> Option<&[u8]> {
-               let old_pos = self.read_pos.fetch_add(len, Ordering::AcqRel);
-               if self.data.len() < old_pos + len {
-                       return None;
-               }
-               Some(&self.data[old_pos..old_pos + len])
-       }
-       fn get_slice_nonadvancing(&self, len: usize) -> Option<&[u8]> {
-               let old_pos = self.read_pos.load(Ordering::Acquire);
-               if self.data.len() < old_pos + len {
-                       return None;
-               }
-               Some(&self.data[old_pos..old_pos + len])
-       }
-}
-
-struct DummyChainWatcher {
-       input: Arc<InputData>,
-}
-
-impl ChainWatchInterface for DummyChainWatcher {
-       fn install_watch_tx(&self, _txid: &Sha256dHash, _script_pub_key: &Script) { }
-       fn install_watch_outpoint(&self, _outpoint: (Sha256dHash, u32), _out_script: &Script) { }
-       fn watch_all_txn(&self) { }
-       fn register_listener(&self, _listener: Weak<ChainListener>) { }
-
-       fn get_chain_utxo(&self, _genesis_hash: Sha256dHash, _unspent_tx_output_identifier: u64) -> Result<(Script, u64), ChainError> {
-               match self.input.get_slice(2) {
-                       Some(&[0, _]) => Err(ChainError::NotSupported),
-                       Some(&[1, _]) => Err(ChainError::NotWatched),
-                       Some(&[2, _]) => Err(ChainError::UnknownTx),
-                       Some(&[_, x]) => Ok((Builder::new().push_int(x as i64).into_script().to_v0_p2wsh(), 0)),
-                       None => Err(ChainError::UnknownTx),
-                       _ => unreachable!(),
-               }
-       }
-}
-
-#[inline]
-pub fn do_test(data: &[u8]) {
-       let input = Arc::new(InputData {
-               data: data.to_vec(),
-               read_pos: AtomicUsize::new(0),
-       });
-       macro_rules! get_slice_nonadvancing {
-               ($len: expr) => {
-                       match input.get_slice_nonadvancing($len as usize) {
-                               Some(slice) => slice,
-                               None => return,
-                       }
-               }
-       }
-       macro_rules! get_slice {
-               ($len: expr) => {
-                       match input.get_slice($len as usize) {
-                               Some(slice) => slice,
-                               None => return,
-                       }
-               }
-       }
-
-       macro_rules! decode_msg {
-               ($MsgType: path, $len: expr) => {{
-                       let mut reader = ::std::io::Cursor::new(get_slice!($len));
-                       match <($MsgType)>::read(&mut reader) {
-                               Ok(msg) => msg,
-                               Err(e) => match e {
-                                       msgs::DecodeError::UnknownVersion => return,
-                                       msgs::DecodeError::UnknownRequiredFeature => return,
-                                       msgs::DecodeError::InvalidValue => return,
-                                       msgs::DecodeError::ExtraAddressesPerType => return,
-                                       msgs::DecodeError::BadLengthDescriptor => return,
-                                       msgs::DecodeError::ShortRead => panic!("We picked the length..."),
-                                       msgs::DecodeError::Io(e) => panic!(format!("{}", e)),
-                               }
-                       }
-               }}
-       }
-
-       macro_rules! decode_msg_with_len16 {
-               ($MsgType: path, $begin_len: expr, $excess: expr) => {
-                       {
-                               let extra_len = slice_to_be16(&get_slice_nonadvancing!($begin_len as usize + 2)[$begin_len..$begin_len + 2]);
-                               decode_msg!($MsgType, $begin_len as usize + 2 + (extra_len as usize) + $excess)
-                       }
-               }
-       }
-
-       macro_rules! get_pubkey {
-               () => {
-                       match PublicKey::from_slice(get_slice!(33)) {
-                               Ok(key) => key,
-                               Err(_) => return,
-                       }
-               }
-       }
-
-       let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new("".to_owned()));
-       let chain_monitor = Arc::new(DummyChainWatcher {
-               input: Arc::clone(&input),
-       });
-
-       let our_pubkey = get_pubkey!();
-       let router = Router::new(our_pubkey.clone(), chain_monitor, Arc::clone(&logger));
-
-       loop {
-               match get_slice!(1)[0] {
-                       0 => {
-                               let start_len = slice_to_be16(&get_slice_nonadvancing!(64 + 2)[64..64 + 2]) as usize;
-                               let addr_len = slice_to_be16(&get_slice_nonadvancing!(64+start_len+2 + 74)[64+start_len+2 + 72..64+start_len+2 + 74]);
-                               if addr_len > (37+1)*4 {
-                                       return;
-                               }
-                               let _ = router.handle_node_announcement(&decode_msg_with_len16!(msgs::NodeAnnouncement, 64, 288));
-                       },
-                       1 => {
-                               let _ = router.handle_channel_announcement(&decode_msg_with_len16!(msgs::ChannelAnnouncement, 64*4, 32+8+33*4));
-                       },
-                       2 => {
-                               let _ = router.handle_channel_update(&decode_msg!(msgs::ChannelUpdate, 128));
-                       },
-                       3 => {
-                               match get_slice!(1)[0] {
-                                       0 => {
-                                               router.handle_htlc_fail_channel_update(&msgs::HTLCFailChannelUpdate::ChannelUpdateMessage {msg: decode_msg!(msgs::ChannelUpdate, 128)});
-                                       },
-                                       1 => {
-                                               let short_channel_id = slice_to_be64(get_slice!(8));
-                                               router.handle_htlc_fail_channel_update(&msgs::HTLCFailChannelUpdate::ChannelClosed {short_channel_id, is_permanent: false});
-                                       },
-                                       _ => return,
-                               }
-                       },
-                       4 => {
-                               let target = get_pubkey!();
-                               let mut first_hops_vec = Vec::new();
-                               let first_hops = match get_slice!(1)[0] {
-                                       0 => None,
-                                       1 => {
-                                               let count = slice_to_be16(get_slice!(2));
-                                               for _ in 0..count {
-                                                       first_hops_vec.push(ChannelDetails {
-                                                               channel_id: [0; 32],
-                                                               short_channel_id: Some(slice_to_be64(get_slice!(8))),
-                                                               remote_network_id: get_pubkey!(),
-                                                               channel_value_satoshis: slice_to_be64(get_slice!(8)),
-                                                               user_id: 0,
-                                                               inbound_capacity_msat: 0,
-                                                               is_live: true,
-                                                               outbound_capacity_msat: 0,
-                                                       });
-                                               }
-                                               Some(&first_hops_vec[..])
-                                       },
-                                       _ => return,
-                               };
-                               let mut last_hops_vec = Vec::new();
-                               let last_hops = {
-                                       let count = slice_to_be16(get_slice!(2));
-                                       for _ in 0..count {
-                                               last_hops_vec.push(RouteHint {
-                                                       src_node_id: get_pubkey!(),
-                                                       short_channel_id: slice_to_be64(get_slice!(8)),
-                                                       fee_base_msat: slice_to_be32(get_slice!(4)),
-                                                       fee_proportional_millionths: slice_to_be32(get_slice!(4)),
-                                                       cltv_expiry_delta: slice_to_be16(get_slice!(2)),
-                                                       htlc_minimum_msat: slice_to_be64(get_slice!(8)),
-                                               });
-                                       }
-                                       &last_hops_vec[..]
-                               };
-                               let _ = router.get_route(&target, first_hops, last_hops, slice_to_be64(get_slice!(8)), slice_to_be32(get_slice!(4)));
-                       },
-                       _ => return,
-               }
-       }
-}
-
-#[cfg(feature = "afl")]
-#[macro_use] extern crate afl;
-#[cfg(feature = "afl")]
-fn main() {
-       fuzz!(|data| {
-               do_test(data);
-       });
-}
-
-#[cfg(feature = "honggfuzz")]
-#[macro_use] extern crate honggfuzz;
-#[cfg(feature = "honggfuzz")]
-fn main() {
-       loop {
-               fuzz!(|data| {
-                       do_test(data);
-               });
-       }
-}
-
-extern crate hex;
-#[cfg(test)]
-mod tests {
-
-       #[test]
-       fn duplicate_crash() {
-               super::do_test(&::hex::decode("00").unwrap());
-       }
-}
diff --git a/fuzz/fuzz_targets/utils/mod.rs b/fuzz/fuzz_targets/utils/mod.rs
deleted file mode 100644 (file)
index a7d7c32..0000000
+++ /dev/null
@@ -1 +0,0 @@
-pub(crate) mod test_logger;
diff --git a/fuzz/fuzz_targets/utils/test_logger.rs b/fuzz/fuzz_targets/utils/test_logger.rs
deleted file mode 100644 (file)
index 097d001..0000000
+++ /dev/null
@@ -1,23 +0,0 @@
-use lightning::util::logger::{Logger, Record};
-pub struct TestLogger {
-       #[cfg(test)]
-       id: String,
-}
-
-impl TestLogger {
-       pub fn new(_id: String) -> TestLogger {
-               TestLogger {
-                       #[cfg(test)]
-                       id: _id
-               }
-       }
-}
-
-impl Logger for TestLogger {
-       fn log(&self, record: &Record) {
-               #[cfg(test)]
-               println!("{:<5} {} [{} : {}, {}] {}", record.level.to_string(), self.id, record.module_path, record.file, record.line, record.args);
-               #[cfg(not(test))]
-               let _ = format!("{}", record.args);
-       }
-}
diff --git a/fuzz/travis-fuzz.sh b/fuzz/travis-fuzz.sh
deleted file mode 100755 (executable)
index e602e95..0000000
+++ /dev/null
@@ -1,29 +0,0 @@
-#!/bin/bash
-set -e
-
-pushd fuzz_targets/msg_targets
-rm *_target.rs
-./gen_target.sh
-[ "$(git diff)" != "" ] && exit 1
-popd
-
-cargo install --force honggfuzz
-for TARGET in fuzz_targets/*.rs fuzz_targets/msg_targets/*_target.rs; do
-       FILENAME=$(basename $TARGET)
-       FILE="${FILENAME%.*}"
-       HFUZZ_RUN_ARGS="--exit_upon_crash -v -n2"
-       if [ "$FILE" = "chanmon_fail_consistency" ]; then
-               HFUZZ_RUN_ARGS="$HFUZZ_RUN_ARGS -F 64 -N100000"
-       else
-               HFUZZ_RUN_ARGS="$HFUZZ_RUN_ARGS -N1000000"
-       fi
-       export HFUZZ_RUN_ARGS
-       HFUZZ_BUILD_ARGS="--features honggfuzz_fuzz" cargo hfuzz run $FILE
-       if [ -f hfuzz_workspace/$FILE/HONGGFUZZ.REPORT.TXT ]; then
-               cat hfuzz_workspace/$FILE/HONGGFUZZ.REPORT.TXT
-               for CASE in hfuzz_workspace/$FILE/SIG*; do
-                       cat $CASE | xxd -p
-               done
-               exit 1
-       fi
-done
diff --git a/lightning-net-tokio/Cargo.toml b/lightning-net-tokio/Cargo.toml
new file mode 100644 (file)
index 0000000..f6fa03b
--- /dev/null
@@ -0,0 +1,19 @@
+[package]
+name = "lightning-net-tokio"
+version = "0.0.1"
+authors = ["Matt Corallo"]
+license = "Apache-2.0"
+description = """
+Implementation of the rust-lightning network stack using Tokio.
+For Rust-Lightning clients which wish to make direct connections to Lightning P2P nodes, this is a simple alternative to implementing the nerequired network stack, especially for those already using Tokio.
+"""
+
+[dependencies]
+bitcoin = "0.20"
+bitcoin_hashes = "0.7"
+lightning = { version = "0.0.9", path = "../lightning" }
+secp256k1 = "0.15"
+tokio-codec = "0.1"
+futures = "0.1"
+tokio = "0.1"
+bytes = "0.4"
diff --git a/lightning-net-tokio/src/lib.rs b/lightning-net-tokio/src/lib.rs
new file mode 100644 (file)
index 0000000..0bc36b2
--- /dev/null
@@ -0,0 +1,270 @@
+extern crate bytes;
+extern crate tokio;
+extern crate tokio_codec;
+extern crate futures;
+extern crate lightning;
+extern crate secp256k1;
+
+use bytes::BufMut;
+
+use futures::future;
+use futures::future::Future;
+use futures::{AsyncSink, Stream, Sink};
+use futures::sync::mpsc;
+
+use secp256k1::key::PublicKey;
+
+use tokio::timer::Delay;
+use tokio::net::TcpStream;
+
+use lightning::ln::peer_handler;
+use lightning::ln::peer_handler::SocketDescriptor as LnSocketTrait;
+
+use std::mem;
+use std::net::SocketAddr;
+use std::sync::{Arc, Mutex};
+use std::sync::atomic::{AtomicU64, Ordering};
+use std::time::{Duration, Instant};
+use std::vec::Vec;
+use std::hash::Hash;
+
+static ID_COUNTER: AtomicU64 = AtomicU64::new(0);
+
+/// A connection to a remote peer. Can be constructed either as a remote connection using
+/// Connection::setup_outbound o
+pub struct Connection {
+       writer: Option<mpsc::Sender<bytes::Bytes>>,
+       event_notify: mpsc::Sender<()>,
+       pending_read: Vec<u8>,
+       read_blocker: Option<futures::sync::oneshot::Sender<Result<(), ()>>>,
+       read_paused: bool,
+       need_disconnect: bool,
+       id: u64,
+}
+impl Connection {
+       fn schedule_read(peer_manager: Arc<peer_handler::PeerManager<SocketDescriptor>>, us: Arc<Mutex<Self>>, reader: futures::stream::SplitStream<tokio_codec::Framed<TcpStream, tokio_codec::BytesCodec>>) {
+               let us_ref = us.clone();
+               let us_close_ref = us.clone();
+               let peer_manager_ref = peer_manager.clone();
+               tokio::spawn(reader.for_each(move |b| {
+                       let pending_read = b.to_vec();
+                       {
+                               let mut lock = us_ref.lock().unwrap();
+                               assert!(lock.pending_read.is_empty());
+                               if lock.read_paused {
+                                       lock.pending_read = pending_read;
+                                       let (sender, blocker) = futures::sync::oneshot::channel();
+                                       lock.read_blocker = Some(sender);
+                                       return future::Either::A(blocker.then(|_| { Ok(()) }));
+                               }
+                       }
+                       //TODO: There's a race where we don't meet the requirements of disconnect_socket if its
+                       //called right here, after we release the us_ref lock in the scope above, but before we
+                       //call read_event!
+                       match peer_manager.read_event(&mut SocketDescriptor::new(us_ref.clone(), peer_manager.clone()), pending_read) {
+                               Ok(pause_read) => {
+                                       if pause_read {
+                                               let mut lock = us_ref.lock().unwrap();
+                                               lock.read_paused = true;
+                                       }
+                               },
+                               Err(e) => {
+                                       us_ref.lock().unwrap().need_disconnect = false;
+                                       return future::Either::B(future::result(Err(std::io::Error::new(std::io::ErrorKind::InvalidData, e))));
+                               }
+                       }
+
+                       if let Err(e) = us_ref.lock().unwrap().event_notify.try_send(()) {
+                               // Ignore full errors as we just need them to poll after this point, so if the user
+                               // hasn't received the last send yet, it doesn't matter.
+                               assert!(e.is_full());
+                       }
+
+                       future::Either::B(future::result(Ok(())))
+               }).then(move |_| {
+                       if us_close_ref.lock().unwrap().need_disconnect {
+                               peer_manager_ref.disconnect_event(&SocketDescriptor::new(us_close_ref, peer_manager_ref.clone()));
+                               println!("Peer disconnected!");
+                       } else {
+                               println!("We disconnected peer!");
+                       }
+                       Ok(())
+               }));
+       }
+
+       fn new(event_notify: mpsc::Sender<()>, stream: TcpStream) -> (futures::stream::SplitStream<tokio_codec::Framed<TcpStream, tokio_codec::BytesCodec>>, Arc<Mutex<Self>>) {
+               let (writer, reader) = tokio_codec::Framed::new(stream, tokio_codec::BytesCodec::new()).split();
+               let (send_sink, send_stream) = mpsc::channel(3);
+               tokio::spawn(writer.send_all(send_stream.map_err(|_| -> std::io::Error {
+                       unreachable!();
+               })).then(|_| {
+                       future::result(Ok(()))
+               }));
+               let us = Arc::new(Mutex::new(Self { writer: Some(send_sink), event_notify, pending_read: Vec::new(), read_blocker: None, read_paused: false, need_disconnect: true, id: ID_COUNTER.fetch_add(1, Ordering::AcqRel) }));
+
+               (reader, us)
+       }
+
+       /// Process incoming messages and feed outgoing messages on the provided socket generated by
+       /// accepting an incoming connection (by scheduling futures with tokio::spawn).
+       ///
+       /// You should poll the Receive end of event_notify and call get_and_clear_pending_events() on
+       /// ChannelManager and ChannelMonitor objects.
+       pub fn setup_inbound(peer_manager: Arc<peer_handler::PeerManager<SocketDescriptor>>, event_notify: mpsc::Sender<()>, stream: TcpStream) {
+               let (reader, us) = Self::new(event_notify, stream);
+
+               if let Ok(_) = peer_manager.new_inbound_connection(SocketDescriptor::new(us.clone(), peer_manager.clone())) {
+                       Self::schedule_read(peer_manager, us, reader);
+               }
+       }
+
+       /// Process incoming messages and feed outgoing messages on the provided socket generated by
+       /// making an outbound connection which is expected to be accepted by a peer with the given
+       /// public key (by scheduling futures with tokio::spawn).
+       ///
+       /// You should poll the Receive end of event_notify and call get_and_clear_pending_events() on
+       /// ChannelManager and ChannelMonitor objects.
+       pub fn setup_outbound(peer_manager: Arc<peer_handler::PeerManager<SocketDescriptor>>, event_notify: mpsc::Sender<()>, their_node_id: PublicKey, stream: TcpStream) {
+               let (reader, us) = Self::new(event_notify, stream);
+
+               if let Ok(initial_send) = peer_manager.new_outbound_connection(their_node_id, SocketDescriptor::new(us.clone(), peer_manager.clone())) {
+                       if SocketDescriptor::new(us.clone(), peer_manager.clone()).send_data(&initial_send, true) == initial_send.len() {
+                               Self::schedule_read(peer_manager, us, reader);
+                       } else {
+                               println!("Failed to write first full message to socket!");
+                       }
+               }
+       }
+
+       /// Process incoming messages and feed outgoing messages on a new connection made to the given
+       /// socket address which is expected to be accepted by a peer with the given public key (by
+       /// scheduling futures with tokio::spawn).
+       ///
+       /// You should poll the Receive end of event_notify and call get_and_clear_pending_events() on
+       /// ChannelManager and ChannelMonitor objects.
+       pub fn connect_outbound(peer_manager: Arc<peer_handler::PeerManager<SocketDescriptor>>, event_notify: mpsc::Sender<()>, their_node_id: PublicKey, addr: SocketAddr) {
+               let connect_timeout = Delay::new(Instant::now() + Duration::from_secs(10)).then(|_| {
+                       future::err(std::io::Error::new(std::io::ErrorKind::TimedOut, "timeout reached"))
+               });
+               tokio::spawn(TcpStream::connect(&addr).select(connect_timeout)
+                       .and_then(move |stream| {
+                               Connection::setup_outbound(peer_manager, event_notify, their_node_id, stream.0);
+                               future::ok(())
+                       }).or_else(|_| {
+                               //TODO: return errors somehow
+                               future::ok(())
+                       }));
+       }
+}
+
+#[derive(Clone)]
+pub struct SocketDescriptor {
+       conn: Arc<Mutex<Connection>>,
+       id: u64,
+       peer_manager: Arc<peer_handler::PeerManager<SocketDescriptor>>,
+}
+impl SocketDescriptor {
+       fn new(conn: Arc<Mutex<Connection>>, peer_manager: Arc<peer_handler::PeerManager<SocketDescriptor>>) -> Self {
+               let id = conn.lock().unwrap().id;
+               Self { conn, id, peer_manager }
+       }
+}
+impl peer_handler::SocketDescriptor for SocketDescriptor {
+       fn send_data(&mut self, data: &[u8], resume_read: bool) -> usize {
+               macro_rules! schedule_read {
+                       ($us_ref: expr) => {
+                               tokio::spawn(future::lazy(move || -> Result<(), ()> {
+                                       let mut read_data = Vec::new();
+                                       {
+                                               let mut us = $us_ref.conn.lock().unwrap();
+                                               mem::swap(&mut read_data, &mut us.pending_read);
+                                       }
+                                       if !read_data.is_empty() {
+                                               let mut us_clone = $us_ref.clone();
+                                               match $us_ref.peer_manager.read_event(&mut us_clone, read_data) {
+                                                       Ok(pause_read) => {
+                                                               if pause_read { return Ok(()); }
+                                                       },
+                                                       Err(_) => {
+                                                               //TODO: Not actually sure how to do this
+                                                               return Ok(());
+                                                       }
+                                               }
+                                       }
+                                       let mut us = $us_ref.conn.lock().unwrap();
+                                       if let Some(sender) = us.read_blocker.take() {
+                                               sender.send(Ok(())).unwrap();
+                                       }
+                                       us.read_paused = false;
+                                       if let Err(e) = us.event_notify.try_send(()) {
+                                               // Ignore full errors as we just need them to poll after this point, so if the user
+                                               // hasn't received the last send yet, it doesn't matter.
+                                               assert!(e.is_full());
+                                       }
+                                       Ok(())
+                               }));
+                       }
+               }
+
+               let mut us = self.conn.lock().unwrap();
+               if resume_read {
+                       let us_ref = self.clone();
+                       schedule_read!(us_ref);
+               }
+               if data.is_empty() { return 0; }
+               if us.writer.is_none() {
+                       us.read_paused = true;
+                       return 0;
+               }
+
+               let mut bytes = bytes::BytesMut::with_capacity(data.len());
+               bytes.put(data);
+               let write_res = us.writer.as_mut().unwrap().start_send(bytes.freeze());
+               match write_res {
+                       Ok(res) => {
+                               match res {
+                                       AsyncSink::Ready => {
+                                               data.len()
+                                       },
+                                       AsyncSink::NotReady(_) => {
+                                               us.read_paused = true;
+                                               let us_ref = self.clone();
+                                               tokio::spawn(us.writer.take().unwrap().flush().then(move |writer_res| -> Result<(), ()> {
+                                                       if let Ok(writer) = writer_res {
+                                                               {
+                                                                       let mut us = us_ref.conn.lock().unwrap();
+                                                                       us.writer = Some(writer);
+                                                               }
+                                                               schedule_read!(us_ref);
+                                                       } // we'll fire the disconnect event on the socket reader end
+                                                       Ok(())
+                                               }));
+                                               0
+                                       }
+                               }
+                       },
+                       Err(_) => {
+                               // We'll fire the disconnected event on the socket reader end
+                               0
+                       },
+               }
+       }
+
+       fn disconnect_socket(&mut self) {
+               let mut us = self.conn.lock().unwrap();
+               us.need_disconnect = true;
+               us.read_paused = true;
+       }
+}
+impl Eq for SocketDescriptor {}
+impl PartialEq for SocketDescriptor {
+       fn eq(&self, o: &Self) -> bool {
+               self.id == o.id
+       }
+}
+impl Hash for SocketDescriptor {
+       fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
+               self.id.hash(state);
+       }
+}
+
diff --git a/lightning/Cargo.toml b/lightning/Cargo.toml
new file mode 100644 (file)
index 0000000..004e6d2
--- /dev/null
@@ -0,0 +1,38 @@
+[package]
+name = "lightning"
+version = "0.0.9"
+authors = ["Matt Corallo"]
+license = "Apache-2.0"
+repository = "https://github.com/rust-bitcoin/rust-lightning/"
+description = """
+A Bitcoin Lightning library in Rust.
+Does most of the hard work, without implying a specific runtime, requiring clients implement basic network logic, chain interactions and disk storage.
+Still missing tons of error-handling. See GitHub issues for suggested projects if you want to contribute. Don't have to bother telling you not to use this for anything serious, because you'd have to build a client around it to even try.
+"""
+
+[features]
+# Supports tracking channels with a non-bitcoin chain hashes. Currently enables all kinds of fun DoS attacks.
+non_bitcoin_chain_hash_routing = []
+fuzztarget = ["secp256k1/fuzztarget", "bitcoin/fuzztarget", "bitcoin_hashes/fuzztarget"]
+# Unlog messages superior at targeted level.
+max_level_off = []
+max_level_error = []
+max_level_warn = []
+max_level_info = []
+max_level_debug = []
+
+[dependencies]
+bitcoin = "0.20"
+bitcoin_hashes = "0.7"
+secp256k1 = "0.15"
+
+[dev-dependencies.bitcoin]
+version = "0.20"
+features = ["bitcoinconsensus"]
+
+[dev-dependencies]
+hex = "0.3"
+rand = "0.4"
+
+[profile.dev]
+opt-level = 1
diff --git a/lightning/fuzz/.gitignore b/lightning/fuzz/.gitignore
new file mode 100644 (file)
index 0000000..8bf27ec
--- /dev/null
@@ -0,0 +1,3 @@
+hfuzz_target
+target
+hfuzz_workspace
diff --git a/lightning/fuzz/Cargo.toml b/lightning/fuzz/Cargo.toml
new file mode 100644 (file)
index 0000000..19d832e
--- /dev/null
@@ -0,0 +1,158 @@
+[package]
+name = "lightning-fuzz"
+version = "0.0.1"
+authors = ["Automatically generated"]
+publish = false
+# Because the function is unused it gets dropped before we link lightning, so
+# we have to duplicate build.rs here. Note that this is only required for
+# fuzztarget mode.
+
+[package.metadata]
+cargo-fuzz = true
+
+[features]
+afl_fuzz = ["afl"]
+honggfuzz_fuzz = ["honggfuzz"]
+libfuzzer_fuzz = ["libfuzzer-sys"]
+
+[dependencies]
+afl = { version = "0.4", optional = true }
+lightning = { path = "..", features = ["fuzztarget"] }
+bitcoin = { version = "0.20", features = ["fuzztarget"] }
+bitcoin_hashes = { version = "0.7", features = ["fuzztarget"] }
+hex = "0.3"
+honggfuzz = { version = "0.5", optional = true }
+secp256k1 = { version = "0.15", features=["fuzztarget"] }
+libfuzzer-sys = { git = "https://github.com/rust-fuzz/libfuzzer-sys.git", optional = true }
+
+[build-dependencies]
+cc = "1.0"
+
+# Prevent this from interfering with workspaces
+[workspace]
+members = ["."]
+
+[profile.release]
+lto = true
+codegen-units = 1
+
+[[bin]]
+name = "peer_crypt_target"
+path = "fuzz_targets/peer_crypt_target.rs"
+
+[[bin]]
+name = "full_stack_target"
+path = "fuzz_targets/full_stack_target.rs"
+
+[[bin]]
+name = "chanmon_fail_consistency"
+path = "fuzz_targets/chanmon_fail_consistency.rs"
+
+[[bin]]
+name = "router_target"
+path = "fuzz_targets/router_target.rs"
+
+[[bin]]
+name = "chanmon_deser_target"
+path = "fuzz_targets/chanmon_deser_target.rs"
+
+# message fuzz targets
+[[bin]]
+name = "msg_ping_target"
+path = "fuzz_targets/msg_targets/msg_ping_target.rs"
+
+[[bin]]
+name = "msg_pong_target"
+path = "fuzz_targets/msg_targets/msg_pong_target.rs"
+
+[[bin]]
+name = "msg_error_message_target"
+path = "fuzz_targets/msg_targets/msg_error_message_target.rs"
+
+[[bin]]
+name = "msg_update_add_htlc_target"
+path = "fuzz_targets/msg_targets/msg_update_add_htlc_target.rs"
+
+[[bin]]
+name = "msg_accept_channel_target"
+path = "fuzz_targets/msg_targets/msg_accept_channel_target.rs"
+
+[[bin]]
+name = "msg_closing_signed_target"
+path = "fuzz_targets/msg_targets/msg_closing_signed_target.rs"
+
+[[bin]]
+name = "msg_commitment_signed_target"
+path = "fuzz_targets/msg_targets/msg_commitment_signed_target.rs"
+
+[[bin]]
+name = "msg_funding_created_target"
+path = "fuzz_targets/msg_targets/msg_funding_created_target.rs"
+
+[[bin]]
+name = "msg_funding_locked_target"
+path = "fuzz_targets/msg_targets/msg_funding_locked_target.rs"
+
+[[bin]]
+name = "msg_funding_signed_target"
+path = "fuzz_targets/msg_targets/msg_funding_signed_target.rs"
+
+[[bin]]
+name = "msg_open_channel_target"
+path = "fuzz_targets/msg_targets/msg_open_channel_target.rs"
+
+[[bin]]
+name = "msg_revoke_and_ack_target"
+path = "fuzz_targets/msg_targets/msg_revoke_and_ack_target.rs"
+
+[[bin]]
+name = "msg_shutdown_target"
+path = "fuzz_targets/msg_targets/msg_shutdown_target.rs"
+
+[[bin]]
+name = "msg_update_fail_malformed_htlc_target"
+path = "fuzz_targets/msg_targets/msg_update_fail_malformed_htlc_target.rs"
+
+[[bin]]
+name = "msg_update_fee_target"
+path = "fuzz_targets/msg_targets/msg_update_fee_target.rs"
+
+[[bin]]
+name = "msg_update_fulfill_htlc_target"
+path = "fuzz_targets/msg_targets/msg_update_fulfill_htlc_target.rs"
+
+[[bin]]
+name = "msg_update_fail_htlc_target"
+path = "fuzz_targets/msg_targets/msg_update_fail_htlc_target.rs"
+
+[[bin]]
+name = "msg_channel_reestablish_target"
+path = "fuzz_targets/msg_targets/msg_channel_reestablish_target.rs"
+
+[[bin]]
+name = "msg_announcement_signatures_target"
+path = "fuzz_targets/msg_targets/msg_announcement_signatures_target.rs"
+
+[[bin]]
+name = "msg_channel_announcement_target"
+path = "fuzz_targets/msg_targets/msg_channel_announcement_target.rs"
+
+[[bin]]
+name = "msg_channel_update_target"
+path = "fuzz_targets/msg_targets/msg_channel_update_target.rs"
+
+[[bin]]
+name = "msg_decoded_onion_error_packet_target"
+path = "fuzz_targets/msg_targets/msg_decoded_onion_error_packet_target.rs"
+
+[[bin]]
+name = "msg_init_target"
+path = "fuzz_targets/msg_targets/msg_init_target.rs"
+
+[[bin]]
+name = "msg_node_announcement_target"
+path = "fuzz_targets/msg_targets/msg_node_announcement_target.rs"
+
+[[bin]]
+name = "msg_onion_hop_data_target"
+path = "fuzz_targets/msg_targets/msg_onion_hop_data_target.rs"
diff --git a/lightning/fuzz/fuzz_targets/chanmon_deser_target.rs b/lightning/fuzz/fuzz_targets/chanmon_deser_target.rs
new file mode 100644 (file)
index 0000000..f741832
--- /dev/null
@@ -0,0 +1,72 @@
+// This file is auto-generated by gen_target.sh based on msg_target_template.txt
+// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
+
+extern crate bitcoin;
+extern crate bitcoin_hashes;
+extern crate lightning;
+
+use bitcoin_hashes::sha256d::Hash as Sha256dHash;
+
+use lightning::ln::channelmonitor;
+use lightning::util::ser::{ReadableArgs, Writer};
+
+mod utils;
+use utils::test_logger;
+
+use std::io::Cursor;
+use std::sync::Arc;
+
+struct VecWriter(Vec<u8>);
+impl Writer for VecWriter {
+       fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
+               self.0.extend_from_slice(buf);
+               Ok(())
+       }
+       fn size_hint(&mut self, size: usize) {
+               self.0.reserve_exact(size);
+       }
+}
+
+#[inline]
+pub fn do_test(data: &[u8]) {
+       let logger = Arc::new(test_logger::TestLogger::new("".to_owned()));
+       if let Ok((latest_block_hash, monitor)) = <(Sha256dHash, channelmonitor::ChannelMonitor)>::read(&mut Cursor::new(data), logger.clone()) {
+               let mut w = VecWriter(Vec::new());
+               monitor.write_for_disk(&mut w).unwrap();
+               let deserialized_copy = <(Sha256dHash, channelmonitor::ChannelMonitor)>::read(&mut Cursor::new(&w.0), logger.clone()).unwrap();
+               assert!(latest_block_hash == deserialized_copy.0);
+               assert!(monitor == deserialized_copy.1);
+               w.0.clear();
+               monitor.write_for_watchtower(&mut w).unwrap();
+       }
+}
+
+#[cfg(feature = "afl")]
+#[macro_use] extern crate afl;
+#[cfg(feature = "afl")]
+fn main() {
+       fuzz!(|data| {
+               do_test(data);
+       });
+}
+
+#[cfg(feature = "honggfuzz")]
+#[macro_use] extern crate honggfuzz;
+#[cfg(feature = "honggfuzz")]
+fn main() {
+       loop {
+               fuzz!(|data| {
+                       do_test(data);
+               });
+       }
+}
+
+extern crate hex;
+#[cfg(test)]
+mod tests {
+
+       #[test]
+       fn duplicate_crash() {
+               super::do_test(&::hex::decode("00").unwrap());
+       }
+}
diff --git a/lightning/fuzz/fuzz_targets/chanmon_fail_consistency.rs b/lightning/fuzz/fuzz_targets/chanmon_fail_consistency.rs
new file mode 100644 (file)
index 0000000..3610b1c
--- /dev/null
@@ -0,0 +1,783 @@
+//! Test that monitor update failures don't get our channel state out of sync.
+//! One of the biggest concern with the monitor update failure handling code is that messages
+//! resent after monitor updating is restored are delivered out-of-order, resulting in
+//! commitment_signed messages having "invalid signatures".
+//! To test this we stand up a network of three nodes and read bytes from the fuzz input to denote
+//! actions such as sending payments, handling events, or changing monitor update return values on
+//! a per-node basis. This should allow it to find any cases where the ordering of actions results
+//! in us getting out of sync with ourselves, and, assuming at least one of our recieve- or
+//! send-side handling is correct, other peers. We consider it a failure if any action results in a
+//! channel being force-closed.
+
+//Uncomment this for libfuzzer builds:
+//#![no_main]
+
+extern crate bitcoin;
+extern crate bitcoin_hashes;
+extern crate lightning;
+extern crate secp256k1;
+
+use bitcoin::BitcoinHash;
+use bitcoin::blockdata::block::BlockHeader;
+use bitcoin::blockdata::transaction::{Transaction, TxOut};
+use bitcoin::blockdata::script::{Builder, Script};
+use bitcoin::blockdata::opcodes;
+use bitcoin::network::constants::Network;
+
+use bitcoin_hashes::Hash as TraitImport;
+use bitcoin_hashes::hash160::Hash as Hash160;
+use bitcoin_hashes::sha256::Hash as Sha256;
+use bitcoin_hashes::sha256d::Hash as Sha256d;
+
+use lightning::chain::chaininterface;
+use lightning::chain::transaction::OutPoint;
+use lightning::chain::chaininterface::{BroadcasterInterface,ConfirmationTarget,ChainListener,FeeEstimator,ChainWatchInterfaceUtil};
+use lightning::chain::keysinterface::{ChannelKeys, KeysInterface};
+use lightning::ln::channelmonitor;
+use lightning::ln::channelmonitor::{ChannelMonitor, ChannelMonitorUpdateErr, HTLCUpdate};
+use lightning::ln::channelmanager::{ChannelManager, PaymentHash, PaymentPreimage, ChannelManagerReadArgs};
+use lightning::ln::router::{Route, RouteHop};
+use lightning::ln::msgs::{CommitmentUpdate, ChannelMessageHandler, ErrorAction, HandleError, UpdateAddHTLC, LocalFeatures};
+use lightning::util::events;
+use lightning::util::logger::Logger;
+use lightning::util::config::UserConfig;
+use lightning::util::events::{EventsProvider, MessageSendEventsProvider};
+use lightning::util::ser::{Readable, ReadableArgs, Writeable, Writer};
+
+mod utils;
+use utils::test_logger;
+
+use secp256k1::key::{PublicKey,SecretKey};
+use secp256k1::Secp256k1;
+
+use std::mem;
+use std::cmp::Ordering;
+use std::collections::{HashSet, hash_map, HashMap};
+use std::sync::{Arc,Mutex};
+use std::sync::atomic;
+use std::io::Cursor;
+
+struct FuzzEstimator {}
+impl FeeEstimator for FuzzEstimator {
+       fn get_est_sat_per_1000_weight(&self, _: ConfirmationTarget) -> u64 {
+               253
+       }
+}
+
+pub struct TestBroadcaster {}
+impl BroadcasterInterface for TestBroadcaster {
+       fn broadcast_transaction(&self, _tx: &Transaction) { }
+}
+
+pub struct VecWriter(pub Vec<u8>);
+impl Writer for VecWriter {
+       fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
+               self.0.extend_from_slice(buf);
+               Ok(())
+       }
+       fn size_hint(&mut self, size: usize) {
+               self.0.reserve_exact(size);
+       }
+}
+
+static mut IN_RESTORE: bool = false;
+pub struct TestChannelMonitor {
+       pub simple_monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint>>,
+       pub update_ret: Mutex<Result<(), channelmonitor::ChannelMonitorUpdateErr>>,
+       pub latest_good_update: Mutex<HashMap<OutPoint, Vec<u8>>>,
+       pub latest_update_good: Mutex<HashMap<OutPoint, bool>>,
+       pub latest_updates_good_at_last_ser: Mutex<HashMap<OutPoint, bool>>,
+       pub should_update_manager: atomic::AtomicBool,
+}
+impl TestChannelMonitor {
+       pub fn new(chain_monitor: Arc<chaininterface::ChainWatchInterface>, broadcaster: Arc<chaininterface::BroadcasterInterface>, logger: Arc<Logger>, feeest: Arc<chaininterface::FeeEstimator>) -> Self {
+               Self {
+                       simple_monitor: channelmonitor::SimpleManyChannelMonitor::new(chain_monitor, broadcaster, logger, feeest),
+                       update_ret: Mutex::new(Ok(())),
+                       latest_good_update: Mutex::new(HashMap::new()),
+                       latest_update_good: Mutex::new(HashMap::new()),
+                       latest_updates_good_at_last_ser: Mutex::new(HashMap::new()),
+                       should_update_manager: atomic::AtomicBool::new(false),
+               }
+       }
+}
+impl channelmonitor::ManyChannelMonitor for TestChannelMonitor {
+       fn add_update_monitor(&self, funding_txo: OutPoint, monitor: channelmonitor::ChannelMonitor) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> {
+               let ret = self.update_ret.lock().unwrap().clone();
+               if let Ok(()) = ret {
+                       let mut ser = VecWriter(Vec::new());
+                       monitor.write_for_disk(&mut ser).unwrap();
+                       self.latest_good_update.lock().unwrap().insert(funding_txo, ser.0);
+                       match self.latest_update_good.lock().unwrap().entry(funding_txo) {
+                               hash_map::Entry::Vacant(mut e) => { e.insert(true); },
+                               hash_map::Entry::Occupied(mut e) => {
+                                       if !e.get() && unsafe { IN_RESTORE } {
+                                               // Technically we can't consider an update to be "good" unless we're doing
+                                               // it in response to a test_restore_channel_monitor as the channel may
+                                               // still be waiting on such a call, so only set us to good if we're in the
+                                               // middle of a restore call.
+                                               e.insert(true);
+                                       }
+                               },
+                       }
+                       self.should_update_manager.store(true, atomic::Ordering::Relaxed);
+               } else {
+                       self.latest_update_good.lock().unwrap().insert(funding_txo, false);
+               }
+               assert!(self.simple_monitor.add_update_monitor(funding_txo, monitor).is_ok());
+               ret
+       }
+
+       fn fetch_pending_htlc_updated(&self) -> Vec<HTLCUpdate> {
+               return self.simple_monitor.fetch_pending_htlc_updated();
+       }
+}
+
+struct KeyProvider {
+       node_id: u8,
+       session_id: atomic::AtomicU8,
+       channel_id: atomic::AtomicU8,
+}
+impl KeysInterface for KeyProvider {
+       fn get_node_secret(&self) -> SecretKey {
+               SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, self.node_id]).unwrap()
+       }
+
+       fn get_destination_script(&self) -> Script {
+               let secp_ctx = Secp256k1::signing_only();
+               let channel_monitor_claim_key = SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, self.node_id]).unwrap();
+               let our_channel_monitor_claim_key_hash = Hash160::hash(&PublicKey::from_secret_key(&secp_ctx, &channel_monitor_claim_key).serialize());
+               Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(&our_channel_monitor_claim_key_hash[..]).into_script()
+       }
+
+       fn get_shutdown_pubkey(&self) -> PublicKey {
+               let secp_ctx = Secp256k1::signing_only();
+               PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, self.node_id]).unwrap())
+       }
+
+       fn get_channel_keys(&self, _inbound: bool) -> ChannelKeys {
+               ChannelKeys {
+                       funding_key:               SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, self.node_id]).unwrap(),
+                       revocation_base_key:       SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, self.node_id]).unwrap(),
+                       payment_base_key:          SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, self.node_id]).unwrap(),
+                       delayed_payment_base_key:  SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, self.node_id]).unwrap(),
+                       htlc_base_key:             SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, self.node_id]).unwrap(),
+                       commitment_seed: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, self.node_id],
+               }
+       }
+
+       fn get_session_key(&self) -> SecretKey {
+               let id = self.session_id.fetch_add(1, atomic::Ordering::Relaxed);
+               SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, id, 10, self.node_id]).unwrap()
+       }
+
+       fn get_channel_id(&self) -> [u8; 32] {
+               let id = self.channel_id.fetch_add(1, atomic::Ordering::Relaxed);
+               [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, id, 11, self.node_id]
+       }
+}
+
+#[inline]
+pub fn do_test(data: &[u8]) {
+       let fee_est = Arc::new(FuzzEstimator{});
+       let broadcast = Arc::new(TestBroadcaster{});
+
+       macro_rules! make_node {
+               ($node_id: expr) => { {
+                       let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new($node_id.to_string()));
+                       let watch = Arc::new(ChainWatchInterfaceUtil::new(Network::Bitcoin, Arc::clone(&logger)));
+                       let monitor = Arc::new(TestChannelMonitor::new(watch.clone(), broadcast.clone(), logger.clone(), fee_est.clone()));
+
+                       let keys_manager = Arc::new(KeyProvider { node_id: $node_id, session_id: atomic::AtomicU8::new(0), channel_id: atomic::AtomicU8::new(0) });
+                       let mut config = UserConfig::new();
+                       config.channel_options.fee_proportional_millionths = 0;
+                       config.channel_options.announced_channel = true;
+                       config.peer_channel_config_limits.min_dust_limit_satoshis = 0;
+                       (ChannelManager::new(Network::Bitcoin, fee_est.clone(), monitor.clone(), watch.clone(), broadcast.clone(), Arc::clone(&logger), keys_manager.clone(), config).unwrap(),
+                       monitor)
+               } }
+       }
+
+       macro_rules! reload_node {
+               ($ser: expr, $node_id: expr, $old_monitors: expr) => { {
+                       let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new($node_id.to_string()));
+                       let watch = Arc::new(ChainWatchInterfaceUtil::new(Network::Bitcoin, Arc::clone(&logger)));
+                       let monitor = Arc::new(TestChannelMonitor::new(watch.clone(), broadcast.clone(), logger.clone(), fee_est.clone()));
+
+                       let keys_manager = Arc::new(KeyProvider { node_id: $node_id, session_id: atomic::AtomicU8::new(0), channel_id: atomic::AtomicU8::new(0) });
+                       let mut config = UserConfig::new();
+                       config.channel_options.fee_proportional_millionths = 0;
+                       config.channel_options.announced_channel = true;
+                       config.peer_channel_config_limits.min_dust_limit_satoshis = 0;
+
+                       let mut monitors = HashMap::new();
+                       let mut old_monitors = $old_monitors.latest_good_update.lock().unwrap();
+                       for (outpoint, monitor_ser) in old_monitors.drain() {
+                               monitors.insert(outpoint, <(Sha256d, ChannelMonitor)>::read(&mut Cursor::new(&monitor_ser), Arc::clone(&logger)).expect("Failed to read monitor").1);
+                               monitor.latest_good_update.lock().unwrap().insert(outpoint, monitor_ser);
+                       }
+                       let mut monitor_refs = HashMap::new();
+                       for (outpoint, monitor) in monitors.iter() {
+                               monitor_refs.insert(*outpoint, monitor);
+                       }
+
+                       let read_args = ChannelManagerReadArgs {
+                               keys_manager,
+                               fee_estimator: fee_est.clone(),
+                               monitor: monitor.clone(),
+                               chain_monitor: watch,
+                               tx_broadcaster: broadcast.clone(),
+                               logger,
+                               default_config: config,
+                               channel_monitors: &monitor_refs,
+                       };
+
+                       let res = (<(Sha256d, ChannelManager)>::read(&mut Cursor::new(&$ser.0), read_args).expect("Failed to read manager").1, monitor);
+                       for (_, was_good) in $old_monitors.latest_updates_good_at_last_ser.lock().unwrap().iter() {
+                               if !was_good {
+                                       // If the last time we updated a monitor we didn't successfully update (and we
+                                       // have sense updated our serialized copy of the ChannelManager) we may
+                                       // force-close the channel on our counterparty cause we know we're missing
+                                       // something. Thus, we just return here since we can't continue to test.
+                                       return;
+                               }
+                       }
+                       res
+               } }
+       }
+
+
+       let mut channel_txn = Vec::new();
+       macro_rules! make_channel {
+               ($source: expr, $dest: expr, $chan_id: expr) => { {
+                       $source.create_channel($dest.get_our_node_id(), 10000000, 42, 0).unwrap();
+                       let open_channel = {
+                               let events = $source.get_and_clear_pending_msg_events();
+                               assert_eq!(events.len(), 1);
+                               if let events::MessageSendEvent::SendOpenChannel { ref msg, .. } = events[0] {
+                                       msg.clone()
+                               } else { panic!("Wrong event type"); }
+                       };
+
+                       $dest.handle_open_channel(&$source.get_our_node_id(), LocalFeatures::new(), &open_channel).unwrap();
+                       let accept_channel = {
+                               let events = $dest.get_and_clear_pending_msg_events();
+                               assert_eq!(events.len(), 1);
+                               if let events::MessageSendEvent::SendAcceptChannel { ref msg, .. } = events[0] {
+                                       msg.clone()
+                               } else { panic!("Wrong event type"); }
+                       };
+
+                       $source.handle_accept_channel(&$dest.get_our_node_id(), LocalFeatures::new(), &accept_channel).unwrap();
+                       {
+                               let events = $source.get_and_clear_pending_events();
+                               assert_eq!(events.len(), 1);
+                               if let events::Event::FundingGenerationReady { ref temporary_channel_id, ref channel_value_satoshis, ref output_script, .. } = events[0] {
+                                       let tx = Transaction { version: $chan_id, lock_time: 0, input: Vec::new(), output: vec![TxOut {
+                                               value: *channel_value_satoshis, script_pubkey: output_script.clone(),
+                                       }]};
+                                       let funding_output = OutPoint::new(tx.txid(), 0);
+                                       $source.funding_transaction_generated(&temporary_channel_id, funding_output);
+                                       channel_txn.push(tx);
+                               } else { panic!("Wrong event type"); }
+                       }
+
+                       let funding_created = {
+                               let events = $source.get_and_clear_pending_msg_events();
+                               assert_eq!(events.len(), 1);
+                               if let events::MessageSendEvent::SendFundingCreated { ref msg, .. } = events[0] {
+                                       msg.clone()
+                               } else { panic!("Wrong event type"); }
+                       };
+                       $dest.handle_funding_created(&$source.get_our_node_id(), &funding_created).unwrap();
+
+                       let funding_signed = {
+                               let events = $dest.get_and_clear_pending_msg_events();
+                               assert_eq!(events.len(), 1);
+                               if let events::MessageSendEvent::SendFundingSigned { ref msg, .. } = events[0] {
+                                       msg.clone()
+                               } else { panic!("Wrong event type"); }
+                       };
+                       $source.handle_funding_signed(&$dest.get_our_node_id(), &funding_signed).unwrap();
+
+                       {
+                               let events = $source.get_and_clear_pending_events();
+                               assert_eq!(events.len(), 1);
+                               if let events::Event::FundingBroadcastSafe { .. } = events[0] {
+                               } else { panic!("Wrong event type"); }
+                       }
+               } }
+       }
+
+       macro_rules! confirm_txn {
+               ($node: expr) => { {
+                       let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+                       let mut txn = Vec::with_capacity(channel_txn.len());
+                       let mut posn = Vec::with_capacity(channel_txn.len());
+                       for i in 0..channel_txn.len() {
+                               txn.push(&channel_txn[i]);
+                               posn.push(i as u32 + 1);
+                       }
+                       $node.block_connected(&header, 1, &txn, &posn);
+                       for i in 2..100 {
+                               header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+                               $node.block_connected(&header, i, &Vec::new(), &[0; 0]);
+                       }
+               } }
+       }
+
+       macro_rules! lock_fundings {
+               ($nodes: expr) => { {
+                       let mut node_events = Vec::new();
+                       for node in $nodes.iter() {
+                               node_events.push(node.get_and_clear_pending_msg_events());
+                       }
+                       for (idx, node_event) in node_events.iter().enumerate() {
+                               for event in node_event {
+                                       if let events::MessageSendEvent::SendFundingLocked { ref node_id, ref msg } = event {
+                                               for node in $nodes.iter() {
+                                                       if node.get_our_node_id() == *node_id {
+                                                               node.handle_funding_locked(&$nodes[idx].get_our_node_id(), msg).unwrap();
+                                                       }
+                                               }
+                                       } else { panic!("Wrong event type"); }
+                               }
+                       }
+
+                       for node in $nodes.iter() {
+                               let events = node.get_and_clear_pending_msg_events();
+                               for event in events {
+                                       if let events::MessageSendEvent::SendAnnouncementSignatures { .. } = event {
+                                       } else { panic!("Wrong event type"); }
+                               }
+                       }
+               } }
+       }
+
+       // 3 nodes is enough to hit all the possible cases, notably unknown-source-unknown-dest
+       // forwarding.
+       let (mut node_a, mut monitor_a) = make_node!(0);
+       let (mut node_b, mut monitor_b) = make_node!(1);
+       let (mut node_c, mut monitor_c) = make_node!(2);
+
+       let mut nodes = [node_a, node_b, node_c];
+
+       make_channel!(nodes[0], nodes[1], 0);
+       make_channel!(nodes[1], nodes[2], 1);
+
+       for node in nodes.iter() {
+               confirm_txn!(node);
+       }
+
+       lock_fundings!(nodes);
+
+       let chan_a = nodes[0].list_usable_channels()[0].short_channel_id.unwrap();
+       let chan_b = nodes[2].list_usable_channels()[0].short_channel_id.unwrap();
+
+       let mut payment_id = 0;
+
+       let mut chan_a_disconnected = false;
+       let mut chan_b_disconnected = false;
+       let mut ba_events = Vec::new();
+       let mut bc_events = Vec::new();
+
+       let mut node_a_ser = VecWriter(Vec::new());
+       nodes[0].write(&mut node_a_ser).unwrap();
+       let mut node_b_ser = VecWriter(Vec::new());
+       nodes[1].write(&mut node_b_ser).unwrap();
+       let mut node_c_ser = VecWriter(Vec::new());
+       nodes[2].write(&mut node_c_ser).unwrap();
+
+       macro_rules! test_err {
+               ($res: expr) => {
+                       match $res {
+                               Ok(()) => {},
+                               Err(HandleError { action: Some(ErrorAction::IgnoreError), .. }) => { },
+                               _ => { $res.unwrap() },
+                       }
+               }
+       }
+
+       macro_rules! test_return {
+               () => { {
+                       assert_eq!(nodes[0].list_channels().len(), 1);
+                       assert_eq!(nodes[1].list_channels().len(), 2);
+                       assert_eq!(nodes[2].list_channels().len(), 1);
+                       return;
+               } }
+       }
+
+       let mut read_pos = 0;
+       macro_rules! get_slice {
+               ($len: expr) => {
+                       {
+                               let slice_len = $len as usize;
+                               if data.len() < read_pos + slice_len {
+                                       test_return!();
+                               }
+                               read_pos += slice_len;
+                               &data[read_pos - slice_len..read_pos]
+                       }
+               }
+       }
+
+       loop {
+               macro_rules! send_payment {
+                       ($source: expr, $dest: expr) => { {
+                               let payment_hash = Sha256::hash(&[payment_id; 1]);
+                               payment_id = payment_id.wrapping_add(1);
+                               if let Err(_) = $source.send_payment(Route {
+                                       hops: vec![RouteHop {
+                                               pubkey: $dest.0.get_our_node_id(),
+                                               short_channel_id: $dest.1,
+                                               fee_msat: 5000000,
+                                               cltv_expiry_delta: 200,
+                                       }],
+                               }, PaymentHash(payment_hash.into_inner())) {
+                                       // Probably ran out of funds
+                                       test_return!();
+                               }
+                       } };
+                       ($source: expr, $middle: expr, $dest: expr) => { {
+                               let payment_hash = Sha256::hash(&[payment_id; 1]);
+                               payment_id = payment_id.wrapping_add(1);
+                               if let Err(_) = $source.send_payment(Route {
+                                       hops: vec![RouteHop {
+                                               pubkey: $middle.0.get_our_node_id(),
+                                               short_channel_id: $middle.1,
+                                               fee_msat: 50000,
+                                               cltv_expiry_delta: 100,
+                                       },RouteHop {
+                                               pubkey: $dest.0.get_our_node_id(),
+                                               short_channel_id: $dest.1,
+                                               fee_msat: 5000000,
+                                               cltv_expiry_delta: 200,
+                                       }],
+                               }, PaymentHash(payment_hash.into_inner())) {
+                                       // Probably ran out of funds
+                                       test_return!();
+                               }
+                       } }
+               }
+
+               macro_rules! process_msg_events {
+                       ($node: expr, $corrupt_forward: expr) => { {
+                               let events = if $node == 1 {
+                                       let mut new_events = Vec::new();
+                                       mem::swap(&mut new_events, &mut ba_events);
+                                       new_events.extend_from_slice(&bc_events[..]);
+                                       bc_events.clear();
+                                       new_events
+                               } else { Vec::new() };
+                               for event in events.iter().chain(nodes[$node].get_and_clear_pending_msg_events().iter()) {
+                                       match event {
+                                               events::MessageSendEvent::UpdateHTLCs { ref node_id, updates: CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, ref update_fulfill_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => {
+                                                       for dest in nodes.iter() {
+                                                               if dest.get_our_node_id() == *node_id {
+                                                                       assert!(update_fee.is_none());
+                                                                       for update_add in update_add_htlcs {
+                                                                               if !$corrupt_forward {
+                                                                                       test_err!(dest.handle_update_add_htlc(&nodes[$node].get_our_node_id(), &update_add));
+                                                                               } else {
+                                                                                       // Corrupt the update_add_htlc message so that its HMAC
+                                                                                       // check will fail and we generate a
+                                                                                       // update_fail_malformed_htlc instead of an
+                                                                                       // update_fail_htlc as we do when we reject a payment.
+                                                                                       let mut msg_ser = update_add.encode();
+                                                                                       msg_ser[1000] ^= 0xff;
+                                                                                       let new_msg = UpdateAddHTLC::read(&mut Cursor::new(&msg_ser)).unwrap();
+                                                                                       test_err!(dest.handle_update_add_htlc(&nodes[$node].get_our_node_id(), &new_msg));
+                                                                               }
+                                                                       }
+                                                                       for update_fulfill in update_fulfill_htlcs {
+                                                                               test_err!(dest.handle_update_fulfill_htlc(&nodes[$node].get_our_node_id(), &update_fulfill));
+                                                                       }
+                                                                       for update_fail in update_fail_htlcs {
+                                                                               test_err!(dest.handle_update_fail_htlc(&nodes[$node].get_our_node_id(), &update_fail));
+                                                                       }
+                                                                       for update_fail_malformed in update_fail_malformed_htlcs {
+                                                                               test_err!(dest.handle_update_fail_malformed_htlc(&nodes[$node].get_our_node_id(), &update_fail_malformed));
+                                                                       }
+                                                                       test_err!(dest.handle_commitment_signed(&nodes[$node].get_our_node_id(), &commitment_signed));
+                                                               }
+                                                       }
+                                               },
+                                               events::MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
+                                                       for dest in nodes.iter() {
+                                                               if dest.get_our_node_id() == *node_id {
+                                                                       test_err!(dest.handle_revoke_and_ack(&nodes[$node].get_our_node_id(), msg));
+                                                               }
+                                                       }
+                                               },
+                                               events::MessageSendEvent::SendChannelReestablish { ref node_id, ref msg } => {
+                                                       for dest in nodes.iter() {
+                                                               if dest.get_our_node_id() == *node_id {
+                                                                       test_err!(dest.handle_channel_reestablish(&nodes[$node].get_our_node_id(), msg));
+                                                               }
+                                                       }
+                                               },
+                                               events::MessageSendEvent::SendFundingLocked { .. } => {
+                                                       // Can be generated as a reestablish response
+                                               },
+                                               events::MessageSendEvent::PaymentFailureNetworkUpdate { .. } => {
+                                                       // Can be generated due to a payment forward being rejected due to a
+                                                       // channel having previously failed a monitor update
+                                               },
+                                               _ => panic!("Unhandled message event"),
+                                       }
+                               }
+                       } }
+               }
+
+               macro_rules! drain_msg_events_on_disconnect {
+                       ($counterparty_id: expr) => { {
+                               if $counterparty_id == 0 {
+                                       for event in nodes[0].get_and_clear_pending_msg_events() {
+                                               match event {
+                                                       events::MessageSendEvent::UpdateHTLCs { .. } => {},
+                                                       events::MessageSendEvent::SendRevokeAndACK { .. } => {},
+                                                       events::MessageSendEvent::SendChannelReestablish { .. } => {},
+                                                       events::MessageSendEvent::SendFundingLocked { .. } => {},
+                                                       events::MessageSendEvent::PaymentFailureNetworkUpdate { .. } => {},
+                                                       _ => panic!("Unhandled message event"),
+                                               }
+                                       }
+                                       ba_events.clear();
+                               } else {
+                                       for event in nodes[2].get_and_clear_pending_msg_events() {
+                                               match event {
+                                                       events::MessageSendEvent::UpdateHTLCs { .. } => {},
+                                                       events::MessageSendEvent::SendRevokeAndACK { .. } => {},
+                                                       events::MessageSendEvent::SendChannelReestablish { .. } => {},
+                                                       events::MessageSendEvent::SendFundingLocked { .. } => {},
+                                                       events::MessageSendEvent::PaymentFailureNetworkUpdate { .. } => {},
+                                                       _ => panic!("Unhandled message event"),
+                                               }
+                                       }
+                                       bc_events.clear();
+                               }
+                               let mut events = nodes[1].get_and_clear_pending_msg_events();
+                               let drop_node_id = if $counterparty_id == 0 { nodes[0].get_our_node_id() } else { nodes[2].get_our_node_id() };
+                               let msg_sink = if $counterparty_id == 0 { &mut bc_events } else { &mut ba_events };
+                               for event in events.drain(..) {
+                                       let push = match event {
+                                               events::MessageSendEvent::UpdateHTLCs { ref node_id, .. } => {
+                                                       if *node_id != drop_node_id { true } else { false }
+                                               },
+                                               events::MessageSendEvent::SendRevokeAndACK { ref node_id, .. } => {
+                                                       if *node_id != drop_node_id { true } else { false }
+                                               },
+                                               events::MessageSendEvent::SendChannelReestablish { ref node_id, .. } => {
+                                                       if *node_id != drop_node_id { true } else { false }
+                                               },
+                                               events::MessageSendEvent::SendFundingLocked { .. } => false,
+                                               events::MessageSendEvent::PaymentFailureNetworkUpdate { .. } => false,
+                                               _ => panic!("Unhandled message event"),
+                                       };
+                                       if push { msg_sink.push(event); }
+                               }
+                       } }
+               }
+
+               macro_rules! process_events {
+                       ($node: expr, $fail: expr) => { {
+                               // In case we get 256 payments we may have a hash collision, resulting in the
+                               // second claim/fail call not finding the duplicate-hash HTLC, so we have to
+                               // deduplicate the calls here.
+                               let mut claim_set = HashSet::new();
+                               let mut events = nodes[$node].get_and_clear_pending_events();
+                               // Sort events so that PendingHTLCsForwardable get processed last. This avoids a
+                               // case where we first process a PendingHTLCsForwardable, then claim/fail on a
+                               // PaymentReceived, claiming/failing two HTLCs, but leaving a just-generated
+                               // PaymentReceived event for the second HTLC in our pending_events (and breaking
+                               // our claim_set deduplication).
+                               events.sort_by(|a, b| {
+                                       if let events::Event::PaymentReceived { .. } = a {
+                                               if let events::Event::PendingHTLCsForwardable { .. } = b {
+                                                       Ordering::Less
+                                               } else { Ordering::Equal }
+                                       } else if let events::Event::PendingHTLCsForwardable { .. } = a {
+                                               if let events::Event::PaymentReceived { .. } = b {
+                                                       Ordering::Greater
+                                               } else { Ordering::Equal }
+                                       } else { Ordering::Equal }
+                               });
+                               for event in events.drain(..) {
+                                       match event {
+                                               events::Event::PaymentReceived { payment_hash, .. } => {
+                                                       if claim_set.insert(payment_hash.0) {
+                                                               if $fail {
+                                                                       assert!(nodes[$node].fail_htlc_backwards(&payment_hash));
+                                                               } else {
+                                                                       assert!(nodes[$node].claim_funds(PaymentPreimage(payment_hash.0)));
+                                                               }
+                                                       }
+                                               },
+                                               events::Event::PaymentSent { .. } => {},
+                                               events::Event::PaymentFailed { .. } => {},
+                                               events::Event::PendingHTLCsForwardable { .. } => {
+                                                       nodes[$node].process_pending_htlc_forwards();
+                                               },
+                                               _ => panic!("Unhandled event"),
+                                       }
+                               }
+                       } }
+               }
+
+               match get_slice!(1)[0] {
+                       0x00 => *monitor_a.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure),
+                       0x01 => *monitor_b.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure),
+                       0x02 => *monitor_c.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure),
+                       0x03 => *monitor_a.update_ret.lock().unwrap() = Ok(()),
+                       0x04 => *monitor_b.update_ret.lock().unwrap() = Ok(()),
+                       0x05 => *monitor_c.update_ret.lock().unwrap() = Ok(()),
+                       0x06 => { unsafe { IN_RESTORE = true }; nodes[0].test_restore_channel_monitor(); unsafe { IN_RESTORE = false }; },
+                       0x07 => { unsafe { IN_RESTORE = true }; nodes[1].test_restore_channel_monitor(); unsafe { IN_RESTORE = false }; },
+                       0x08 => { unsafe { IN_RESTORE = true }; nodes[2].test_restore_channel_monitor(); unsafe { IN_RESTORE = false }; },
+                       0x09 => send_payment!(nodes[0], (&nodes[1], chan_a)),
+                       0x0a => send_payment!(nodes[1], (&nodes[0], chan_a)),
+                       0x0b => send_payment!(nodes[1], (&nodes[2], chan_b)),
+                       0x0c => send_payment!(nodes[2], (&nodes[1], chan_b)),
+                       0x0d => send_payment!(nodes[0], (&nodes[1], chan_a), (&nodes[2], chan_b)),
+                       0x0e => send_payment!(nodes[2], (&nodes[1], chan_b), (&nodes[0], chan_a)),
+                       0x0f => {
+                               if !chan_a_disconnected {
+                                       nodes[0].peer_disconnected(&nodes[1].get_our_node_id(), false);
+                                       nodes[1].peer_disconnected(&nodes[0].get_our_node_id(), false);
+                                       chan_a_disconnected = true;
+                                       drain_msg_events_on_disconnect!(0);
+                               }
+                       },
+                       0x10 => {
+                               if !chan_b_disconnected {
+                                       nodes[1].peer_disconnected(&nodes[2].get_our_node_id(), false);
+                                       nodes[2].peer_disconnected(&nodes[1].get_our_node_id(), false);
+                                       chan_b_disconnected = true;
+                                       drain_msg_events_on_disconnect!(2);
+                               }
+                       },
+                       0x11 => {
+                               if chan_a_disconnected {
+                                       nodes[0].peer_connected(&nodes[1].get_our_node_id());
+                                       nodes[1].peer_connected(&nodes[0].get_our_node_id());
+                                       chan_a_disconnected = false;
+                               }
+                       },
+                       0x12 => {
+                               if chan_b_disconnected {
+                                       nodes[1].peer_connected(&nodes[2].get_our_node_id());
+                                       nodes[2].peer_connected(&nodes[1].get_our_node_id());
+                                       chan_b_disconnected = false;
+                               }
+                       },
+                       0x13 => process_msg_events!(0, true),
+                       0x14 => process_msg_events!(0, false),
+                       0x15 => process_events!(0, true),
+                       0x16 => process_events!(0, false),
+                       0x17 => process_msg_events!(1, true),
+                       0x18 => process_msg_events!(1, false),
+                       0x19 => process_events!(1, true),
+                       0x1a => process_events!(1, false),
+                       0x1b => process_msg_events!(2, true),
+                       0x1c => process_msg_events!(2, false),
+                       0x1d => process_events!(2, true),
+                       0x1e => process_events!(2, false),
+                       0x1f => {
+                               if !chan_a_disconnected {
+                                       nodes[1].peer_disconnected(&nodes[0].get_our_node_id(), false);
+                                       chan_a_disconnected = true;
+                                       drain_msg_events_on_disconnect!(0);
+                               }
+                               let (new_node_a, new_monitor_a) = reload_node!(node_a_ser, 0, monitor_a);
+                               node_a = Arc::new(new_node_a);
+                               nodes[0] = node_a.clone();
+                               monitor_a = new_monitor_a;
+                       },
+                       0x20 => {
+                               if !chan_a_disconnected {
+                                       nodes[0].peer_disconnected(&nodes[1].get_our_node_id(), false);
+                                       chan_a_disconnected = true;
+                                       nodes[0].get_and_clear_pending_msg_events();
+                                       ba_events.clear();
+                               }
+                               if !chan_b_disconnected {
+                                       nodes[2].peer_disconnected(&nodes[1].get_our_node_id(), false);
+                                       chan_b_disconnected = true;
+                                       nodes[2].get_and_clear_pending_msg_events();
+                                       bc_events.clear();
+                               }
+                               let (new_node_b, new_monitor_b) = reload_node!(node_b_ser, 1, monitor_b);
+                               node_b = Arc::new(new_node_b);
+                               nodes[1] = node_b.clone();
+                               monitor_b = new_monitor_b;
+                       },
+                       0x21 => {
+                               if !chan_b_disconnected {
+                                       nodes[1].peer_disconnected(&nodes[2].get_our_node_id(), false);
+                                       chan_b_disconnected = true;
+                                       drain_msg_events_on_disconnect!(2);
+                               }
+                               let (new_node_c, new_monitor_c) = reload_node!(node_c_ser, 2, monitor_c);
+                               node_c = Arc::new(new_node_c);
+                               nodes[2] = node_c.clone();
+                               monitor_c = new_monitor_c;
+                       },
+                       _ => test_return!(),
+               }
+
+               if monitor_a.should_update_manager.load(atomic::Ordering::Relaxed) {
+                       node_a_ser.0.clear();
+                       nodes[0].write(&mut node_a_ser).unwrap();
+                       monitor_a.should_update_manager.store(false, atomic::Ordering::Relaxed);
+                       *monitor_a.latest_updates_good_at_last_ser.lock().unwrap() = monitor_a.latest_update_good.lock().unwrap().clone();
+               }
+               if monitor_b.should_update_manager.load(atomic::Ordering::Relaxed) {
+                       node_b_ser.0.clear();
+                       nodes[1].write(&mut node_b_ser).unwrap();
+                       monitor_b.should_update_manager.store(false, atomic::Ordering::Relaxed);
+                       *monitor_b.latest_updates_good_at_last_ser.lock().unwrap() = monitor_b.latest_update_good.lock().unwrap().clone();
+               }
+               if monitor_c.should_update_manager.load(atomic::Ordering::Relaxed) {
+                       node_c_ser.0.clear();
+                       nodes[2].write(&mut node_c_ser).unwrap();
+                       monitor_c.should_update_manager.store(false, atomic::Ordering::Relaxed);
+                       *monitor_c.latest_updates_good_at_last_ser.lock().unwrap() = monitor_c.latest_update_good.lock().unwrap().clone();
+               }
+       }
+}
+
+#[cfg(feature = "afl")]
+#[macro_use] extern crate afl;
+#[cfg(feature = "afl")]
+fn main() {
+       fuzz!(|data| {
+               do_test(data);
+       });
+}
+
+#[cfg(feature = "honggfuzz")]
+#[macro_use] extern crate honggfuzz;
+#[cfg(feature = "honggfuzz")]
+fn main() {
+       loop {
+               fuzz!(|data| {
+                       do_test(data);
+               });
+       }
+}
+
+#[cfg(feature = "libfuzzer_fuzz")]
+#[macro_use] extern crate libfuzzer_sys;
+#[cfg(feature = "libfuzzer_fuzz")]
+fuzz_target!(|data: &[u8]| {
+       do_test(data);
+});
+
+extern crate hex;
+#[cfg(test)]
+mod tests {
+       #[test]
+       fn duplicate_crash() {
+               super::do_test(&::hex::decode("00").unwrap());
+       }
+}
diff --git a/lightning/fuzz/fuzz_targets/full_stack_target.rs b/lightning/fuzz/fuzz_targets/full_stack_target.rs
new file mode 100644 (file)
index 0000000..6145d00
--- /dev/null
@@ -0,0 +1,890 @@
+//! Test that no series of bytes received over the wire/connections created/payments sent can
+//! result in a crash. We do this by standing up a node and then reading bytes from input to denote
+//! actions such as creating new inbound/outbound connections, bytes to be read from a connection,
+//! or payments to send/ways to handle events generated.
+//! This test has been very useful, though due to its complexity good starting inputs are critical.
+
+//Uncomment this for libfuzzer builds:
+//#![no_main]
+
+extern crate bitcoin;
+extern crate bitcoin_hashes;
+extern crate lightning;
+extern crate secp256k1;
+
+use bitcoin::blockdata::block::BlockHeader;
+use bitcoin::blockdata::transaction::{Transaction, TxOut};
+use bitcoin::blockdata::script::{Builder, Script};
+use bitcoin::blockdata::opcodes;
+use bitcoin::consensus::encode::deserialize;
+use bitcoin::network::constants::Network;
+use bitcoin::util::hash::BitcoinHash;
+
+use bitcoin_hashes::Hash as TraitImport;
+use bitcoin_hashes::HashEngine as TraitImportEngine;
+use bitcoin_hashes::sha256::Hash as Sha256;
+use bitcoin_hashes::hash160::Hash as Hash160;
+use bitcoin_hashes::sha256d::Hash as Sha256dHash;
+
+use lightning::chain::chaininterface::{BroadcasterInterface,ConfirmationTarget,ChainListener,FeeEstimator,ChainWatchInterfaceUtil};
+use lightning::chain::transaction::OutPoint;
+use lightning::chain::keysinterface::{ChannelKeys, KeysInterface};
+use lightning::ln::channelmonitor;
+use lightning::ln::channelmanager::{ChannelManager, PaymentHash, PaymentPreimage};
+use lightning::ln::peer_handler::{MessageHandler,PeerManager,SocketDescriptor};
+use lightning::ln::router::Router;
+use lightning::util::events::{EventsProvider,Event};
+use lightning::util::logger::Logger;
+use lightning::util::config::UserConfig;
+
+mod utils;
+
+use utils::test_logger;
+
+use secp256k1::key::{PublicKey,SecretKey};
+use secp256k1::Secp256k1;
+
+use std::cell::RefCell;
+use std::collections::{HashMap, hash_map};
+use std::cmp;
+use std::hash::Hash;
+use std::sync::Arc;
+use std::sync::atomic::{AtomicU64,AtomicUsize,Ordering};
+
+#[inline]
+pub fn slice_to_be16(v: &[u8]) -> u16 {
+       ((v[0] as u16) << 8*1) |
+       ((v[1] as u16) << 8*0)
+}
+
+#[inline]
+pub fn slice_to_be24(v: &[u8]) -> u32 {
+       ((v[0] as u32) << 8*2) |
+       ((v[1] as u32) << 8*1) |
+       ((v[2] as u32) << 8*0)
+}
+
+#[inline]
+pub fn slice_to_be32(v: &[u8]) -> u32 {
+       ((v[0] as u32) << 8*3) |
+       ((v[1] as u32) << 8*2) |
+       ((v[2] as u32) << 8*1) |
+       ((v[3] as u32) << 8*0)
+}
+
+#[inline]
+pub fn be64_to_array(u: u64) -> [u8; 8] {
+       let mut v = [0; 8];
+       v[0] = ((u >> 8*7) & 0xff) as u8;
+       v[1] = ((u >> 8*6) & 0xff) as u8;
+       v[2] = ((u >> 8*5) & 0xff) as u8;
+       v[3] = ((u >> 8*4) & 0xff) as u8;
+       v[4] = ((u >> 8*3) & 0xff) as u8;
+       v[5] = ((u >> 8*2) & 0xff) as u8;
+       v[6] = ((u >> 8*1) & 0xff) as u8;
+       v[7] = ((u >> 8*0) & 0xff) as u8;
+       v
+}
+
+struct InputData {
+       data: Vec<u8>,
+       read_pos: AtomicUsize,
+}
+impl InputData {
+       fn get_slice(&self, len: usize) -> Option<&[u8]> {
+               let old_pos = self.read_pos.fetch_add(len, Ordering::AcqRel);
+               if self.data.len() < old_pos + len {
+                       return None;
+               }
+               Some(&self.data[old_pos..old_pos + len])
+       }
+}
+
+struct FuzzEstimator {
+       input: Arc<InputData>,
+}
+impl FeeEstimator for FuzzEstimator {
+       fn get_est_sat_per_1000_weight(&self, _: ConfirmationTarget) -> u64 {
+               //TODO: We should actually be testing at least much more than 64k...
+               match self.input.get_slice(2) {
+                       Some(slice) => cmp::max(slice_to_be16(slice) as u64, 253),
+                       None => 0
+               }
+       }
+}
+
+struct TestBroadcaster {}
+impl BroadcasterInterface for TestBroadcaster {
+       fn broadcast_transaction(&self, _tx: &Transaction) {}
+}
+
+#[derive(Clone)]
+struct Peer<'a> {
+       id: u8,
+       peers_connected: &'a RefCell<[bool; 256]>,
+}
+impl<'a> SocketDescriptor for Peer<'a> {
+       fn send_data(&mut self, data: &[u8], _resume_read: bool) -> usize {
+               data.len()
+       }
+       fn disconnect_socket(&mut self) {
+               assert!(self.peers_connected.borrow()[self.id as usize]);
+               self.peers_connected.borrow_mut()[self.id as usize] = false;
+       }
+}
+impl<'a> PartialEq for Peer<'a> {
+       fn eq(&self, other: &Self) -> bool {
+               self.id == other.id
+       }
+}
+impl<'a> Eq for Peer<'a> {}
+impl<'a> Hash for Peer<'a> {
+       fn hash<H : std::hash::Hasher>(&self, h: &mut H) {
+               self.id.hash(h)
+       }
+}
+
+struct MoneyLossDetector<'a> {
+       manager: Arc<ChannelManager>,
+       monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint>>,
+       handler: PeerManager<Peer<'a>>,
+
+       peers: &'a RefCell<[bool; 256]>,
+       funding_txn: Vec<Transaction>,
+       txids_confirmed: HashMap<Sha256dHash, usize>,
+       header_hashes: Vec<Sha256dHash>,
+       height: usize,
+       max_height: usize,
+       blocks_connected: u32,
+}
+impl<'a> MoneyLossDetector<'a> {
+       pub fn new(peers: &'a RefCell<[bool; 256]>, manager: Arc<ChannelManager>, monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint>>, handler: PeerManager<Peer<'a>>) -> Self {
+               MoneyLossDetector {
+                       manager,
+                       monitor,
+                       handler,
+
+                       peers,
+                       funding_txn: Vec::new(),
+                       txids_confirmed: HashMap::new(),
+                       header_hashes: vec![Default::default()],
+                       height: 0,
+                       max_height: 0,
+                       blocks_connected: 0,
+               }
+       }
+
+       fn connect_block(&mut self, all_txn: &[Transaction]) {
+               let mut txn = Vec::with_capacity(all_txn.len());
+               let mut txn_idxs = Vec::with_capacity(all_txn.len());
+               for (idx, tx) in all_txn.iter().enumerate() {
+                       let txid = tx.txid();
+                       match self.txids_confirmed.entry(txid) {
+                               hash_map::Entry::Vacant(e) => {
+                                       e.insert(self.height);
+                                       txn.push(tx);
+                                       txn_idxs.push(idx as u32 + 1);
+                               },
+                               _ => {},
+                       }
+               }
+
+               let header = BlockHeader { version: 0x20000000, prev_blockhash: self.header_hashes[self.height], merkle_root: Default::default(), time: self.blocks_connected, bits: 42, nonce: 42 };
+               self.height += 1;
+               self.blocks_connected += 1;
+               self.manager.block_connected(&header, self.height as u32, &txn[..], &txn_idxs[..]);
+               (*self.monitor).block_connected(&header, self.height as u32, &txn[..], &txn_idxs[..]);
+               if self.header_hashes.len() > self.height {
+                       self.header_hashes[self.height] = header.bitcoin_hash();
+               } else {
+                       assert_eq!(self.header_hashes.len(), self.height);
+                       self.header_hashes.push(header.bitcoin_hash());
+               }
+               self.max_height = cmp::max(self.height, self.max_height);
+       }
+
+       fn disconnect_block(&mut self) {
+               if self.height > 0 && (self.max_height < 6 || self.height >= self.max_height - 6) {
+                       self.height -= 1;
+                       let header = BlockHeader { version: 0x20000000, prev_blockhash: self.header_hashes[self.height], merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+                       self.manager.block_disconnected(&header, self.height as u32);
+                       self.monitor.block_disconnected(&header, self.height as u32);
+                       let removal_height = self.height;
+                       self.txids_confirmed.retain(|_, height| {
+                               removal_height != *height
+                       });
+               }
+       }
+}
+
+impl<'a> Drop for MoneyLossDetector<'a> {
+       fn drop(&mut self) {
+               if !::std::thread::panicking() {
+                       // Disconnect all peers
+                       for (idx, peer) in self.peers.borrow().iter().enumerate() {
+                               if *peer {
+                                       self.handler.disconnect_event(&Peer{id: idx as u8, peers_connected: &self.peers});
+                               }
+                       }
+
+                       // Force all channels onto the chain (and time out claim txn)
+                       self.manager.force_close_all_channels();
+               }
+       }
+}
+
+struct KeyProvider {
+       node_secret: SecretKey,
+       counter: AtomicU64,
+}
+impl KeysInterface for KeyProvider {
+       fn get_node_secret(&self) -> SecretKey {
+               self.node_secret.clone()
+       }
+
+       fn get_destination_script(&self) -> Script {
+               let secp_ctx = Secp256k1::signing_only();
+               let channel_monitor_claim_key = SecretKey::from_slice(&hex::decode("0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap()[..]).unwrap();
+               let our_channel_monitor_claim_key_hash = <Hash160 as bitcoin_hashes::Hash>::hash(&PublicKey::from_secret_key(&secp_ctx, &channel_monitor_claim_key).serialize());
+               Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(&our_channel_monitor_claim_key_hash[..]).into_script()
+       }
+
+       fn get_shutdown_pubkey(&self) -> PublicKey {
+               let secp_ctx = Secp256k1::signing_only();
+               PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]).unwrap())
+       }
+
+       fn get_channel_keys(&self, inbound: bool) -> ChannelKeys {
+               let ctr = self.counter.fetch_add(1, Ordering::Relaxed) as u8;
+               if inbound {
+                       ChannelKeys {
+                               funding_key:               SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, ctr]).unwrap(),
+                               revocation_base_key:       SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, ctr]).unwrap(),
+                               payment_base_key:          SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, ctr]).unwrap(),
+                               delayed_payment_base_key:  SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, ctr]).unwrap(),
+                               htlc_base_key:             SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, ctr]).unwrap(),
+                               commitment_seed: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, ctr],
+                       }
+               } else {
+                       ChannelKeys {
+                               funding_key:               SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, ctr]).unwrap(),
+                               revocation_base_key:       SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, ctr]).unwrap(),
+                               payment_base_key:          SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, ctr]).unwrap(),
+                               delayed_payment_base_key:  SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, ctr]).unwrap(),
+                               htlc_base_key:             SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, ctr]).unwrap(),
+                               commitment_seed: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, ctr],
+                       }
+               }
+       }
+
+       fn get_session_key(&self) -> SecretKey {
+               let ctr = self.counter.fetch_add(1, Ordering::Relaxed) as u8;
+               SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, ctr]).unwrap()
+       }
+
+       fn get_channel_id(&self) -> [u8; 32] {
+               let ctr = self.counter.fetch_add(1, Ordering::Relaxed);
+               [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+               (ctr >> 8*7) as u8, (ctr >> 8*6) as u8, (ctr >> 8*5) as u8, (ctr >> 8*4) as u8, (ctr >> 8*3) as u8, (ctr >> 8*2) as u8, (ctr >> 8*1) as u8, 14, (ctr >> 8*0) as u8]
+       }
+}
+
+#[inline]
+pub fn do_test(data: &[u8], logger: &Arc<Logger>) {
+       let input = Arc::new(InputData {
+               data: data.to_vec(),
+               read_pos: AtomicUsize::new(0),
+       });
+       let fee_est = Arc::new(FuzzEstimator {
+               input: input.clone(),
+       });
+
+       macro_rules! get_slice {
+               ($len: expr) => {
+                       match input.get_slice($len as usize) {
+                               Some(slice) => slice,
+                               None => return,
+                       }
+               }
+       }
+
+       macro_rules! get_pubkey {
+               () => {
+                       match PublicKey::from_slice(get_slice!(33)) {
+                               Ok(key) => key,
+                               Err(_) => return,
+                       }
+               }
+       }
+
+       let our_network_key = match SecretKey::from_slice(get_slice!(32)) {
+               Ok(key) => key,
+               Err(_) => return,
+       };
+
+       let watch = Arc::new(ChainWatchInterfaceUtil::new(Network::Bitcoin, Arc::clone(&logger)));
+       let broadcast = Arc::new(TestBroadcaster{});
+       let monitor = channelmonitor::SimpleManyChannelMonitor::new(watch.clone(), broadcast.clone(), Arc::clone(&logger), fee_est.clone());
+
+       let keys_manager = Arc::new(KeyProvider { node_secret: our_network_key.clone(), counter: AtomicU64::new(0) });
+       let mut config = UserConfig::new();
+       config.channel_options.fee_proportional_millionths =  slice_to_be32(get_slice!(4));
+       config.channel_options.announced_channel = get_slice!(1)[0] != 0;
+       config.peer_channel_config_limits.min_dust_limit_satoshis = 0;
+       let channelmanager = ChannelManager::new(Network::Bitcoin, fee_est.clone(), monitor.clone(), watch.clone(), broadcast.clone(), Arc::clone(&logger), keys_manager.clone(), config).unwrap();
+       let router = Arc::new(Router::new(PublicKey::from_secret_key(&Secp256k1::signing_only(), &keys_manager.get_node_secret()), watch.clone(), Arc::clone(&logger)));
+
+       let peers = RefCell::new([false; 256]);
+       let mut loss_detector = MoneyLossDetector::new(&peers, channelmanager.clone(), monitor.clone(), PeerManager::new(MessageHandler {
+               chan_handler: channelmanager.clone(),
+               route_handler: router.clone(),
+       }, our_network_key, &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0], Arc::clone(&logger)));
+
+       let mut should_forward = false;
+       let mut payments_received: Vec<PaymentHash> = Vec::new();
+       let mut payments_sent = 0;
+       let mut pending_funding_generation: Vec<([u8; 32], u64, Script)> = Vec::new();
+       let mut pending_funding_signatures = HashMap::new();
+       let mut pending_funding_relay = Vec::new();
+
+       loop {
+               match get_slice!(1)[0] {
+                       0 => {
+                               let mut new_id = 0;
+                               for i in 1..256 {
+                                       if !peers.borrow()[i-1] {
+                                               new_id = i;
+                                               break;
+                                       }
+                               }
+                               if new_id == 0 { return; }
+                               loss_detector.handler.new_outbound_connection(get_pubkey!(), Peer{id: (new_id - 1) as u8, peers_connected: &peers}).unwrap();
+                               peers.borrow_mut()[new_id - 1] = true;
+                       },
+                       1 => {
+                               let mut new_id = 0;
+                               for i in 1..256 {
+                                       if !peers.borrow()[i-1] {
+                                               new_id = i;
+                                               break;
+                                       }
+                               }
+                               if new_id == 0 { return; }
+                               loss_detector.handler.new_inbound_connection(Peer{id: (new_id - 1) as u8, peers_connected: &peers}).unwrap();
+                               peers.borrow_mut()[new_id - 1] = true;
+                       },
+                       2 => {
+                               let peer_id = get_slice!(1)[0];
+                               if !peers.borrow()[peer_id as usize] { return; }
+                               loss_detector.handler.disconnect_event(&Peer{id: peer_id, peers_connected: &peers});
+                               peers.borrow_mut()[peer_id as usize] = false;
+                       },
+                       3 => {
+                               let peer_id = get_slice!(1)[0];
+                               if !peers.borrow()[peer_id as usize] { return; }
+                               match loss_detector.handler.read_event(&mut Peer{id: peer_id, peers_connected: &peers}, get_slice!(get_slice!(1)[0]).to_vec()) {
+                                       Ok(res) => assert!(!res),
+                                       Err(_) => { peers.borrow_mut()[peer_id as usize] = false; }
+                               }
+                       },
+                       4 => {
+                               let value = slice_to_be24(get_slice!(3)) as u64;
+                               let route = match router.get_route(&get_pubkey!(), None, &Vec::new(), value, 42) {
+                                       Ok(route) => route,
+                                       Err(_) => return,
+                               };
+                               let mut payment_hash = PaymentHash([0; 32]);
+                               payment_hash.0[0..8].copy_from_slice(&be64_to_array(payments_sent));
+                               let mut sha = Sha256::engine();
+                               sha.input(&payment_hash.0[..]);
+                               payment_hash.0 = Sha256::from_engine(sha).into_inner();
+                               payments_sent += 1;
+                               match channelmanager.send_payment(route, payment_hash) {
+                                       Ok(_) => {},
+                                       Err(_) => return,
+                               }
+                       },
+                       5 => {
+                               let peer_id = get_slice!(1)[0];
+                               if !peers.borrow()[peer_id as usize] { return; }
+                               let their_key = get_pubkey!();
+                               let chan_value = slice_to_be24(get_slice!(3)) as u64;
+                               let push_msat_value = slice_to_be24(get_slice!(3)) as u64;
+                               if channelmanager.create_channel(their_key, chan_value, push_msat_value, 0).is_err() { return; }
+                       },
+                       6 => {
+                               let mut channels = channelmanager.list_channels();
+                               let channel_id = get_slice!(1)[0] as usize;
+                               if channel_id >= channels.len() { return; }
+                               channels.sort_by(|a, b| { a.channel_id.cmp(&b.channel_id) });
+                               if channelmanager.close_channel(&channels[channel_id].channel_id).is_err() { return; }
+                       },
+                       7 => {
+                               if should_forward {
+                                       channelmanager.process_pending_htlc_forwards();
+                                       should_forward = false;
+                               }
+                       },
+                       8 => {
+                               for payment in payments_received.drain(..) {
+                                       // SHA256 is defined as XOR of all input bytes placed in the first byte, and 0s
+                                       // for the remaining bytes. Thus, if not all remaining bytes are 0s we cannot
+                                       // fulfill this HTLC, but if they are, we can just take the first byte and
+                                       // place that anywhere in our preimage.
+                                       if &payment.0[1..] != &[0; 31] {
+                                               channelmanager.fail_htlc_backwards(&payment);
+                                       } else {
+                                               let mut payment_preimage = PaymentPreimage([0; 32]);
+                                               payment_preimage.0[0] = payment.0[0];
+                                               channelmanager.claim_funds(payment_preimage);
+                                       }
+                               }
+                       },
+                       9 => {
+                               for payment in payments_received.drain(..) {
+                                       channelmanager.fail_htlc_backwards(&payment);
+                               }
+                       },
+                       10 => {
+                               'outer_loop: for funding_generation in pending_funding_generation.drain(..) {
+                                       let mut tx = Transaction { version: 0, lock_time: 0, input: Vec::new(), output: vec![TxOut {
+                                                       value: funding_generation.1, script_pubkey: funding_generation.2,
+                                               }] };
+                                       let funding_output = 'search_loop: loop {
+                                               let funding_txid = tx.txid();
+                                               if let None = loss_detector.txids_confirmed.get(&funding_txid) {
+                                                       let outpoint = OutPoint::new(funding_txid, 0);
+                                                       for chan in channelmanager.list_channels() {
+                                                               if chan.channel_id == outpoint.to_channel_id() {
+                                                                       tx.version += 1;
+                                                                       continue 'search_loop;
+                                                               }
+                                                       }
+                                                       break outpoint;
+                                               }
+                                               tx.version += 1;
+                                               if tx.version > 0xff {
+                                                       continue 'outer_loop;
+                                               }
+                                       };
+                                       channelmanager.funding_transaction_generated(&funding_generation.0, funding_output.clone());
+                                       pending_funding_signatures.insert(funding_output, tx);
+                               }
+                       },
+                       11 => {
+                               if !pending_funding_relay.is_empty() {
+                                       loss_detector.connect_block(&pending_funding_relay[..]);
+                                       for _ in 2..100 {
+                                               loss_detector.connect_block(&[]);
+                                       }
+                               }
+                               for tx in pending_funding_relay.drain(..) {
+                                       loss_detector.funding_txn.push(tx);
+                               }
+                       },
+                       12 => {
+                               let txlen = slice_to_be16(get_slice!(2));
+                               if txlen == 0 {
+                                       loss_detector.connect_block(&[]);
+                               } else {
+                                       let txres: Result<Transaction, _> = deserialize(get_slice!(txlen));
+                                       if let Ok(tx) = txres {
+                                               loss_detector.connect_block(&[tx]);
+                                       } else {
+                                               return;
+                                       }
+                               }
+                       },
+                       13 => {
+                               loss_detector.disconnect_block();
+                       },
+                       14 => {
+                               let mut channels = channelmanager.list_channels();
+                               let channel_id = get_slice!(1)[0] as usize;
+                               if channel_id >= channels.len() { return; }
+                               channels.sort_by(|a, b| { a.channel_id.cmp(&b.channel_id) });
+                               channelmanager.force_close_channel(&channels[channel_id].channel_id);
+                       },
+                       _ => return,
+               }
+               loss_detector.handler.process_events();
+               for event in loss_detector.manager.get_and_clear_pending_events() {
+                       match event {
+                               Event::FundingGenerationReady { temporary_channel_id, channel_value_satoshis, output_script, .. } => {
+                                       pending_funding_generation.push((temporary_channel_id, channel_value_satoshis, output_script));
+                               },
+                               Event::FundingBroadcastSafe { funding_txo, .. } => {
+                                       pending_funding_relay.push(pending_funding_signatures.remove(&funding_txo).unwrap());
+                               },
+                               Event::PaymentReceived { payment_hash, .. } => {
+                                       payments_received.push(payment_hash);
+                               },
+                               Event::PaymentSent {..} => {},
+                               Event::PaymentFailed {..} => {},
+                               Event::PendingHTLCsForwardable {..} => {
+                                       should_forward = true;
+                               },
+                               Event::SpendableOutputs {..} => {},
+                       }
+               }
+       }
+}
+
+#[cfg(feature = "afl")]
+#[macro_use] extern crate afl;
+#[cfg(feature = "afl")]
+fn main() {
+       fuzz!(|data| {
+               let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new("".to_owned()));
+               do_test(data, &logger);
+       });
+}
+
+#[cfg(feature = "honggfuzz")]
+#[macro_use] extern crate honggfuzz;
+#[cfg(feature = "honggfuzz")]
+fn main() {
+       loop {
+               fuzz!(|data| {
+                       let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new("".to_owned()));
+                       do_test(data, &logger);
+               });
+       }
+}
+
+#[cfg(feature = "libfuzzer_fuzz")]
+#[macro_use] extern crate libfuzzer_sys;
+#[cfg(feature = "libfuzzer_fuzz")]
+fuzz_target!(|data: &[u8]| {
+       let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new("".to_owned()));
+       do_test(data, &logger);
+});
+
+extern crate hex;
+#[cfg(test)]
+mod tests {
+       use utils::test_logger;
+       use lightning::util::logger::{Logger, Record};
+       use std::collections::HashMap;
+       use std::sync::{Arc, Mutex};
+
+       #[test]
+       fn duplicate_crash() {
+               let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new("".to_owned()));
+               super::do_test(&::hex::decode("00").unwrap(), &logger);
+       }
+
+       struct TrackingLogger {
+               /// (module, message) -> count
+               pub lines: Mutex<HashMap<(String, String), usize>>,
+       }
+       impl Logger for TrackingLogger {
+               fn log(&self, record: &Record) {
+                       *self.lines.lock().unwrap().entry((record.module_path.to_string(), format!("{}", record.args))).or_insert(0) += 1;
+                       println!("{:<5} [{} : {}, {}] {}", record.level.to_string(), record.module_path, record.file, record.line, record.args);
+               }
+       }
+
+       #[test]
+       fn test_no_existing_test_breakage() {
+               // To avoid accidentally causing all existing fuzz test cases to be useless by making minor
+               // changes (such as requesting feerate info in a new place), we run a pretty full
+               // step-through with two peers and HTLC forwarding here. Obviously this is pretty finicky,
+               // so this should be updated pretty liberally, but at least we'll know when changes occur.
+               // If nothing else, this test serves as a pretty great initial full_stack_target seed.
+
+               // What each byte represents is broken down below, and then everything is concatenated into
+               // one large test at the end (you want %s/ -.*//g %s/\n\| \|\t\|\///g).
+
+               // Following BOLT 8, lightning message on the wire are: 2-byte encrypted message length + 
+               // 16-byte MAC of the encrypted message length + encrypted Lightning message + 16-byte MAC
+               // of the Lightning message
+               // I.e 2nd inbound read, len 18 : 0006 (encrypted message length) + 03000000000000000000000000000000 (MAC of the encrypted message length)
+               // Len 22 : 0010 00000000 (encrypted lightning message) + 03000000000000000000000000000000 (MAC of the Lightning message)
+
+               // 0000000000000000000000000000000000000000000000000000000000000000 - our network key
+               // 00000000 - fee_proportional_millionths
+               // 01 - announce_channels_publicly
+               //
+               // 00 - new outbound connection with id 0
+               // 030000000000000000000000000000000000000000000000000000000000000000 - peer's pubkey
+               // 030032 - inbound read from peer id 0 of len 50
+               // 00 030000000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - noise act two (0||pubkey||mac)
+               //
+               // 030012 - inbound read from peer id 0 of len 18
+               // 0006 03000000000000000000000000000000 - message header indicating message length 6
+               // 030016 - inbound read from peer id 0 of len 22
+               // 0010 00000000 03000000000000000000000000000000 - init message with no features (type 16) and mac
+               //
+               // 030012 - inbound read from peer id 0 of len 18
+               // 0141 03000000000000000000000000000000 - message header indicating message length 321
+               // 0300fe - inbound read from peer id 0 of len 254
+               // 0020 7500000000000000000000000000000000000000000000000000000000000000 ff4f00f805273c1b203bb5ebf8436bfde57b3be8c2f5e95d9491dbb181909679 000000000000c350 0000000000000000 0000000000000222 ffffffffffffffff 0000000000000222 0000000000000000 000000fd 0006 01e3 030000000000000000000000000000000000000000000000000000000000000001 030000000000000000000000000000000000000000000000000000000000000002 030000000000000000000000000000000000000000000000000000000000000003 030000000000000000000000000000000000000000000000000000000000000004 - beginning of open_channel message
+               // 030053 - inbound read from peer id 0 of len 83
+               // 030000000000000000000000000000000000000000000000000000000000000005 030000000000000000000000000000000000000000000000000000000000000000 01 03000000000000000000000000000000 - rest of open_channel and mac
+               //
+               // 00fd00fd00fd - Three feerate requests (all returning min feerate, which our open_channel also uses) (gonna be ingested by FuzzEstimator)
+               // - client should now respond with accept_channel (CHECK 1: type 33 to peer 03000000)
+               //
+               // 030012 - inbound read from peer id 0 of len 18
+               // 0084 03000000000000000000000000000000 - message header indicating message length 132
+               // 030094 - inbound read from peer id 0 of len 148
+               // 0022 ff4f00f805273c1b203bb5ebf8436bfde57b3be8c2f5e95d9491dbb181909679 3d00000000000000000000000000000000000000000000000000000000000000 0000 5c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 03000000000000000000000000000000 - funding_created and mac
+               // - client should now respond with funding_signed (CHECK 2: type 35 to peer 03000000)
+               //
+               // 0c005e - connect a block with one transaction of len 94
+               // 020000000100000000000000000000000000000000000000000000000000000000000000000000000000ffffffff0150c3000000000000220020ae0000000000000000000000000000000000000000000000000000000000000000000000 - the funding transaction
+               // 0c0000 - connect a block with no transactions
+               // 0c0000 - connect a block with no transactions
+               // 0c0000 - connect a block with no transactions
+               // 0c0000 - connect a block with no transactions
+               // 0c0000 - connect a block with no transactions
+               // 0c0000 - connect a block with no transactions
+               // 0c0000 - connect a block with no transactions
+               // 0c0000 - connect a block with no transactions
+               // 0c0000 - connect a block with no transactions
+               // 0c0000 - connect a block with no transactions
+               // 0c0000 - connect a block with no transactions
+               // 0c0000 - connect a block with no transactions
+               // - by now client should have sent a funding_locked (CHECK 3: SendFundingLocked to 03000000 for chan 3d000000)
+               //
+               // 030012 - inbound read from peer id 0 of len 18
+               // 0043 03000000000000000000000000000000 - message header indicating message length 67
+               // 030053 - inbound read from peer id 0 of len 83
+               // 0024 3d00000000000000000000000000000000000000000000000000000000000000 030100000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - funding_locked and mac
+               //
+               // 01 - new inbound connection with id 1
+               // 030132 - inbound read from peer id 1 of len 50
+               // 0003000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000 - inbound noise act 1
+               // 030142 - inbound read from peer id 1 of len 66
+               // 000302000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003000000000000000000000000000000 - inbound noise act 3
+               //
+               // 030112 - inbound read from peer id 1 of len 18
+               // 0006 01000000000000000000000000000000 - message header indicating message length 6
+               // 030116 - inbound read from peer id 1 of len 22
+               // 0010 00000000 01000000000000000000000000000000 - init message with no features (type 16)
+               //
+               // 05 01 030200000000000000000000000000000000000000000000000000000000000000 00c350 0003e8 - create outbound channel to peer 1 for 50k sat
+               // 00fd00fd00fd - Three feerate requests (all returning min feerate) (gonna be ingested by FuzzEstimator)
+               //
+               // 030112 - inbound read from peer id 1 of len 18
+               // 0110 01000000000000000000000000000000 - message header indicating message length 272
+               // 0301ff - inbound read from peer id 1 of len 255
+               // 0021 0000000000000000000000000000000000000000000000000000000000000e02 000000000000001a 00000000004c4b40 00000000000003e8 00000000000003e8 00000002 03f0 0005 030000000000000000000000000000000000000000000000000000000000000100 030000000000000000000000000000000000000000000000000000000000000200 030000000000000000000000000000000000000000000000000000000000000300 030000000000000000000000000000000000000000000000000000000000000400 030000000000000000000000000000000000000000000000000000000000000500 03000000000000000000000000000000 - beginning of accept_channel
+               // 030121 - inbound read from peer id 1 of len 33
+               // 0000000000000000000000000000000000 01000000000000000000000000000000 - rest of accept_channel and mac
+               //
+               // 0a - create the funding transaction (client should send funding_created now)
+               //
+               // 030112 - inbound read from peer id 1 of len 18
+               // 0062 01000000000000000000000000000000 - message header indicating message length 98
+               // 030172 - inbound read from peer id 1 of len 114
+               // 0023 3900000000000000000000000000000000000000000000000000000000000000 f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100 01000000000000000000000000000000 - funding_signed message and mac
+               //
+               // 0b - broadcast funding transaction
+               // - by now client should have sent a funding_locked (CHECK 4: SendFundingLocked to 03020000 for chan 3f000000)
+               //
+               // 030112 - inbound read from peer id 1 of len 18
+               // 0043 01000000000000000000000000000000 - message header indicating message length 67
+               // 030153 - inbound read from peer id 1 of len 83
+               // 0024 3900000000000000000000000000000000000000000000000000000000000000 030100000000000000000000000000000000000000000000000000000000000000 01000000000000000000000000000000 - funding_locked and mac
+               //
+               // 030012 - inbound read from peer id 0 of len 18
+               // 05ac 03000000000000000000000000000000 - message header indicating message length 1452
+               // 0300ff - inbound read from peer id 0 of len 255
+               // 0080 3d00000000000000000000000000000000000000000000000000000000000000 0000000000000000 0000000000003e80 ff00000000000000000000000000000000000000000000000000000000000000 00000121 00 030000000000000000000000000000000000000000000000000000000000000555 0000000e000001000000000000000003e8000000010000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - beginning of update_add_htlc from 0 to 1 via client
+               // 0300ff - inbound read from peer id 0 of len 255
+               // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
+               // 0300ff - inbound read from peer id 0 of len 255
+               // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
+               // 0300ff - inbound read from peer id 0 of len 255
+               // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
+               // 0300ff - inbound read from peer id 0 of len 255
+               // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
+               // 0300c1 - inbound read from peer id 0 of len 193
+               // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ef00000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - end of update_add_htlc from 0 to 1 via client and mac
+               //
+               // 00fd - A feerate request (returning min feerate, which our open_channel also uses) (gonna be ingested by FuzzEstimator)
+               //
+               // 030012 - inbound read from peer id 0 of len 18
+               // 0064 03000000000000000000000000000000 - message header indicating message length 100
+               // 030074 - inbound read from peer id 0 of len 116
+               // 0084 3d00000000000000000000000000000000000000000000000000000000000000 4d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 0000 03000000000000000000000000000000 - commitment_signed and mac
+               // - client should now respond with revoke_and_ack and commitment_signed (CHECK 5/6: types 133 and 132 to peer 03000000)
+               //
+               // 030012 - inbound read from peer id 0 of len 18
+               // 0063 03000000000000000000000000000000 - message header indicating message length 99
+               // 030073 - inbound read from peer id 0 of len 115
+               // 0085 3d00000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 030200000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - revoke_and_ack and mac
+               //
+               // 07 - process the now-pending HTLC forward
+               // - client now sends id 1 update_add_htlc and commitment_signed (CHECK 7: SendHTLCs event for node 03020000 with 1 HTLCs for channel 3f000000)
+               //
+               // - we respond with commitment_signed then revoke_and_ack (a weird, but valid, order)
+               // 030112 - inbound read from peer id 1 of len 18
+               // 0064 01000000000000000000000000000000 - message header indicating message length 100
+               // 030174 - inbound read from peer id 1 of len 116
+               // 0084 3900000000000000000000000000000000000000000000000000000000000000 f1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100 0000 01000000000000000000000000000000 - commitment_signed and mac
+               //
+               // 030112 - inbound read from peer id 1 of len 18
+               // 0063 01000000000000000000000000000000 - message header indicating message length 99
+               // 030173 - inbound read from peer id 1 of len 115
+               // 0085 3900000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 030200000000000000000000000000000000000000000000000000000000000000 01000000000000000000000000000000 - revoke_and_ack and mac
+               //
+               // 030112 - inbound read from peer id 1 of len 18
+               // 004a 01000000000000000000000000000000 - message header indicating message length 74
+               // 03015a - inbound read from peer id 1 of len 90
+               // 0082 3900000000000000000000000000000000000000000000000000000000000000 0000000000000000 ff00888888888888888888888888888888888888888888888888888888888888 01000000000000000000000000000000 - update_fulfill_htlc and mac
+               // - client should immediately claim the pending HTLC from peer 0 (CHECK 8: SendFulfillHTLCs for node 03000000 with preimage ff00888888 for channel 3d000000)
+               //
+               // 030112 - inbound read from peer id 1 of len 18
+               // 0064 01000000000000000000000000000000 - message header indicating message length 100
+               // 030174 - inbound read from peer id 1 of len 116
+               // 0084 3900000000000000000000000000000000000000000000000000000000000000 fd000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100 0000 01000000000000000000000000000000 - commitment_signed and mac
+               //
+               // 030112 - inbound read from peer id 1 of len 18
+               // 0063 01000000000000000000000000000000 - message header indicating message length 99
+               // 030173 - inbound read from peer id 1 of len 115
+               // 0085 3900000000000000000000000000000000000000000000000000000000000000 0100000000000000000000000000000000000000000000000000000000000000 030300000000000000000000000000000000000000000000000000000000000000 01000000000000000000000000000000 - revoke_and_ack and mac
+               //
+               // - before responding to the commitment_signed generated above, send a new HTLC
+               // 030012 - inbound read from peer id 0 of len 18
+               // 05ac 03000000000000000000000000000000 - message header indicating message length 1452
+               // 0300ff - inbound read from peer id 0 of len 255
+               // 0080 3d00000000000000000000000000000000000000000000000000000000000000 0000000000000001 0000000000003e80 ff00000000000000000000000000000000000000000000000000000000000000 00000121 00 030000000000000000000000000000000000000000000000000000000000000555 0000000e000001000000000000000003e8000000010000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - beginning of update_add_htlc from 0 to 1 via client
+               // 0300ff - inbound read from peer id 0 of len 255
+               // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
+               // 0300ff - inbound read from peer id 0 of len 255
+               // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
+               // 0300ff - inbound read from peer id 0 of len 255
+               // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
+               // 0300ff - inbound read from peer id 0 of len 255
+               // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
+               // 0300c1 - inbound read from peer id 0 of len 193
+               // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ef00000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - end of update_add_htlc from 0 to 1 via client and mac
+               //
+               // 00fd - A feerate request (returning min feerate, which our open_channel also uses) (gonna be ingested by FuzzEstimator)
+               //
+               // - now respond to the update_fulfill_htlc+commitment_signed messages the client sent to peer 0
+               // 030012 - inbound read from peer id 0 of len 18
+               // 0063 03000000000000000000000000000000 - message header indicating message length 99
+               // 030073 - inbound read from peer id 0 of len 115
+               // 0085 3d00000000000000000000000000000000000000000000000000000000000000 0100000000000000000000000000000000000000000000000000000000000000 030300000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - revoke_and_ack and mac
+               // - client should now respond with revoke_and_ack and commitment_signed (CHECK 5/6 duplicates)
+               //
+               // 030012 - inbound read from peer id 0 of len 18
+               // 0064 03000000000000000000000000000000 - message header indicating message length 100
+               // 030074 - inbound read from peer id 0 of len 116
+               // 0084 3d00000000000000000000000000000000000000000000000000000000000000 be000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 0000 03000000000000000000000000000000 - commitment_signed and mac
+               //
+               // 030012 - inbound read from peer id 0 of len 18
+               // 0063 03000000000000000000000000000000 - message header indicating message length 99
+               // 030073 - inbound read from peer id 0 of len 115
+               // 0085 3d00000000000000000000000000000000000000000000000000000000000000 0200000000000000000000000000000000000000000000000000000000000000 030400000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - revoke_and_ack and mac
+               //
+               // 07 - process the now-pending HTLC forward
+               // - client now sends id 1 update_add_htlc and commitment_signed (CHECK 7 duplicate)
+               // - we respond with revoke_and_ack, then commitment_signed, then update_fail_htlc
+               //
+               // 030112 - inbound read from peer id 1 of len 18
+               // 0064 01000000000000000000000000000000 - message header indicating message length 100
+               // 030174 - inbound read from peer id 1 of len 116
+               // 0084 3900000000000000000000000000000000000000000000000000000000000000 fc000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100 0000 01000000000000000000000000000000 - commitment_signed and mac
+               //
+               // 030112 - inbound read from peer id 1 of len 18
+               // 0063 01000000000000000000000000000000 - message header indicating message length 99
+               // 030173 - inbound read from peer id 1 of len 115
+               // 0085 3900000000000000000000000000000000000000000000000000000000000000 0200000000000000000000000000000000000000000000000000000000000000 030400000000000000000000000000000000000000000000000000000000000000 01000000000000000000000000000000 - revoke_and_ack and mac
+               //
+               // 030112 - inbound read from peer id 1 of len 18
+               // 002c 01000000000000000000000000000000 - message header indicating message length 44
+               // 03013c - inbound read from peer id 1 of len 60
+               // 0083 3900000000000000000000000000000000000000000000000000000000000000 0000000000000001 0000 01000000000000000000000000000000 - update_fail_htlc and mac
+               //
+               // 030112 - inbound read from peer id 1 of len 18
+               // 0064 01000000000000000000000000000000 - message header indicating message length 100
+               // 030174 - inbound read from peer id 1 of len 116
+               // 0084 3900000000000000000000000000000000000000000000000000000000000000 fb000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100 0000 01000000000000000000000000000000 - commitment_signed and mac
+               //
+               // 030112 - inbound read from peer id 1 of len 18
+               // 0063 01000000000000000000000000000000 - message header indicating message length 99
+               // 030173 - inbound read from peer id 1 of len 115
+               // 0085 3900000000000000000000000000000000000000000000000000000000000000 0300000000000000000000000000000000000000000000000000000000000000 030500000000000000000000000000000000000000000000000000000000000000 01000000000000000000000000000000 - revoke_and_ack and mac
+               //
+               // 07 - process the now-pending HTLC forward
+               // - client now sends id 0 update_fail_htlc and commitment_signed (CHECK 9)
+               // - now respond to the update_fail_htlc+commitment_signed messages the client sent to peer 0
+               //
+               // 030012 - inbound read from peer id 0 of len 18
+               // 0063 03000000000000000000000000000000 - message header indicating message length 99
+               // 030073 - inbound read from peer id 0 of len 115
+               // 0085 3d00000000000000000000000000000000000000000000000000000000000000 0300000000000000000000000000000000000000000000000000000000000000 030500000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - revoke_and_ack and mac
+               //
+               // 030012 - inbound read from peer id 0 of len 18
+               // 0064 03000000000000000000000000000000 - message header indicating message length 100
+               // 030074 - inbound read from peer id 0 of len 116
+               // 0084 3d00000000000000000000000000000000000000000000000000000000000000 4f000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 0000 03000000000000000000000000000000 - commitment_signed and mac
+               // - client should now respond with revoke_and_ack (CHECK 5 duplicate)
+               //
+               // 030012 - inbound read from peer id 0 of len 18
+               // 05ac 03000000000000000000000000000000 - message header indicating message length 1452
+               // 0300ff - inbound read from peer id 0 of len 255
+               // 0080 3d00000000000000000000000000000000000000000000000000000000000000 0000000000000002 00000000000b0838 ff00000000000000000000000000000000000000000000000000000000000000 00000121 00 030000000000000000000000000000000000000000000000000000000000000555 0000000e0000010000000000000003e800000000010000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - beginning of update_add_htlc from 0 to 1 via client
+               // 0300ff - inbound read from peer id 0 of len 255
+               // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
+               // 0300ff - inbound read from peer id 0 of len 255
+               // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
+               // 0300ff - inbound read from peer id 0 of len 255
+               // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
+               // 0300ff - inbound read from peer id 0 of len 255
+               // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
+               // 0300c1 - inbound read from peer id 0 of len 193
+               // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ef00000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - end of update_add_htlc from 0 to 1 via client and mac
+               //
+               // 00fd - A feerate request (returning min feerate, which our open_channel also uses) (gonna be ingested by FuzzEstimator)
+               //
+               // 030012 - inbound read from peer id 0 of len 18
+               // 00a4 03000000000000000000000000000000 - message header indicating message length 164
+               // 0300b4 - inbound read from peer id 0 of len 180
+               // 0084 3d00000000000000000000000000000000000000000000000000000000000000 07000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 0001 c8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007f00000000000000 03000000000000000000000000000000 - commitment_signed and mac
+               // - client should now respond with revoke_and_ack and commitment_signed (CHECK 5/6 duplicates)
+               //
+               // 030012 - inbound read from peer id 0 of len 18
+               // 0063 03000000000000000000000000000000 - message header indicating message length 99
+               // 030073 - inbound read from peer id 0 of len 115
+               // 0085 3d00000000000000000000000000000000000000000000000000000000000000 0400000000000000000000000000000000000000000000000000000000000000 030600000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - revoke_and_ack and mac
+               //
+               // 07 - process the now-pending HTLC forward
+               // - client now sends id 1 update_add_htlc and commitment_signed (CHECK 7 duplicate)
+               //
+               // 0c007d - connect a block with one transaction of len 125
+               // 0200000001390000000000000000000000000000000000000000000000000000000000000000000000000000008002000100000000000022002090000000000000000000000000000000000000000000000000000000000000006cc10000000000001600145c0000000000000000000000000000000000000005000020 - the commitment transaction for channel 3f00000000000000000000000000000000000000000000000000000000000000
+               // 00fd - A feerate request (returning min feerate, which our open_channel also uses) (gonna be ingested by FuzzEstimator)
+               // 00fd - A feerate request (returning min feerate, which our open_channel also uses) (gonna be ingested by FuzzEstimator)
+               // 0c005e - connect a block with one transaction of len 94
+               // 0200000001fd00000000000000000000000000000000000000000000000000000000000000000000000000000000014f00000000000000220020f60000000000000000000000000000000000000000000000000000000000000000000000 - the funding transaction
+               // 0c0000 - connect a block with no transactions
+               // 0c0000 - connect a block with no transactions
+               // 0c0000 - connect a block with no transactions
+               // 0c0000 - connect a block with no transactions
+               // 0c0000 - connect a block with no transactions
+               //
+               // 07 - process the now-pending HTLC forward
+               // - client now fails the HTLC backwards as it was unable to extract the payment preimage (CHECK 9 duplicate and CHECK 10)
+
+               let logger = Arc::new(TrackingLogger { lines: Mutex::new(HashMap::new()) });
+               super::do_test(&::hex::decode("00000000000000000000000000000000000000000000000000000000000000000000000001000300000000000000000000000000000000000000000000000000000000000000000300320003000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000030012000603000000000000000000000000000000030016001000000000030000000000000000000000000000000300120141030000000000000000000000000000000300fe00207500000000000000000000000000000000000000000000000000000000000000ff4f00f805273c1b203bb5ebf8436bfde57b3be8c2f5e95d9491dbb181909679000000000000c35000000000000000000000000000000222ffffffffffffffff00000000000002220000000000000000000000fd000601e3030000000000000000000000000000000000000000000000000000000000000001030000000000000000000000000000000000000000000000000000000000000002030000000000000000000000000000000000000000000000000000000000000003030000000000000000000000000000000000000000000000000000000000000004030053030000000000000000000000000000000000000000000000000000000000000005030000000000000000000000000000000000000000000000000000000000000000010300000000000000000000000000000000fd00fd00fd0300120084030000000000000000000000000000000300940022ff4f00f805273c1b203bb5ebf8436bfde57b3be8c2f5e95d9491dbb1819096793d0000000000000000000000000000000000000000000000000000000000000000005c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001030000000000000000000000000000000c005e020000000100000000000000000000000000000000000000000000000000000000000000000000000000ffffffff0150c3000000000000220020ae00000000000000000000000000000000000000000000000000000000000000000000000c00000c00000c00000c00000c00000c00000c00000c00000c00000c00000c00000c000003001200430300000000000000000000000000000003005300243d000000000000000000000000000000000000000000000000000000000000000301000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001030132000300000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003014200030200000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000300000000000000000000000000000003011200060100000000000000000000000000000003011600100000000001000000000000000000000000000000050103020000000000000000000000000000000000000000000000000000000000000000c3500003e800fd00fd00fd0301120110010000000000000000000000000000000301ff00210000000000000000000000000000000000000000000000000000000000000e02000000000000001a00000000004c4b4000000000000003e800000000000003e80000000203f00005030000000000000000000000000000000000000000000000000000000000000100030000000000000000000000000000000000000000000000000000000000000200030000000000000000000000000000000000000000000000000000000000000300030000000000000000000000000000000000000000000000000000000000000400030000000000000000000000000000000000000000000000000000000000000500030000000000000000000000000000000301210000000000000000000000000000000000010000000000000000000000000000000a03011200620100000000000000000000000000000003017200233900000000000000000000000000000000000000000000000000000000000000f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100010000000000000000000000000000000b030112004301000000000000000000000000000000030153002439000000000000000000000000000000000000000000000000000000000000000301000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000003001205ac030000000000000000000000000000000300ff00803d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003e80ff0000000000000000000000000000000000000000000000000000000000000000000121000300000000000000000000000000000000000000000000000000000000000005550000000e000001000000000000000003e8000000010000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300c1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffef000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000fd03001200640300000000000000000000000000000003007400843d000000000000000000000000000000000000000000000000000000000000004d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000300000000000000000000000000000003001200630300000000000000000000000000000003007300853d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030200000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000703011200640100000000000000000000000000000003017400843900000000000000000000000000000000000000000000000000000000000000f100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000100000000000000000000000000000003011200630100000000000000000000000000000003017300853900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003020000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000030112004a0100000000000000000000000000000003015a008239000000000000000000000000000000000000000000000000000000000000000000000000000000ff008888888888888888888888888888888888888888888888888888888888880100000000000000000000000000000003011200640100000000000000000000000000000003017400843900000000000000000000000000000000000000000000000000000000000000fd0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000010000000000000000000000000000000301120063010000000000000000000000000000000301730085390000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000303000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000003001205ac030000000000000000000000000000000300ff00803d0000000000000000000000000000000000000000000000000000000000000000000000000000010000000000003e80ff0000000000000000000000000000000000000000000000000000000000000000000121000300000000000000000000000000000000000000000000000000000000000005550000000e000001000000000000000003e8000000010000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300c1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffef000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000fd03001200630300000000000000000000000000000003007300853d0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000303000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003001200640300000000000000000000000000000003007400843d00000000000000000000000000000000000000000000000000000000000000be00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000300000000000000000000000000000003001200630300000000000000000000000000000003007300853d000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000030400000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000703011200640100000000000000000000000000000003017400843900000000000000000000000000000000000000000000000000000000000000fc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000100000000000000000000000000000003011200630100000000000000000000000000000003017300853900000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003040000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000030112002c0100000000000000000000000000000003013c00833900000000000000000000000000000000000000000000000000000000000000000000000000000100000100000000000000000000000000000003011200640100000000000000000000000000000003017400843900000000000000000000000000000000000000000000000000000000000000fb000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000001000000000000000000000000000000030112006301000000000000000000000000000000030173008539000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000030500000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000703001200630300000000000000000000000000000003007300853d0000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000305000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003001200640300000000000000000000000000000003007400843d000000000000000000000000000000000000000000000000000000000000004f00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000300000000000000000000000000000003001205ac030000000000000000000000000000000300ff00803d00000000000000000000000000000000000000000000000000000000000000000000000000000200000000000b0838ff0000000000000000000000000000000000000000000000000000000000000000000121000300000000000000000000000000000000000000000000000000000000000005550000000e0000010000000000000003e800000000010000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300c1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffef000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000fd03001200a4030000000000000000000000000000000300b400843d00000000000000000000000000000000000000000000000000000000000000070000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010001c8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007f000000000000000300000000000000000000000000000003001200630300000000000000000000000000000003007300853d00000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000003060000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000070c007d0200000001390000000000000000000000000000000000000000000000000000000000000000000000000000008002000100000000000022002090000000000000000000000000000000000000000000000000000000000000006cc10000000000001600145c000000000000000000000000000000000000000500002000fd00fd0c005e0200000001fd00000000000000000000000000000000000000000000000000000000000000000000000000000000014f00000000000000220020f600000000000000000000000000000000000000000000000000000000000000000000000c00000c00000c00000c00000c000007").unwrap(), &(Arc::clone(&logger) as Arc<Logger>));
+
+               let log_entries = logger.lines.lock().unwrap();
+               assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendAcceptChannel event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000000 for channel ff4f00f805273c1b203bb5ebf8436bfde57b3be8c2f5e95d9491dbb181909679".to_string())), Some(&1)); // 1
+               assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendFundingSigned event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000000 for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1)); // 2
+               assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendFundingLocked event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000000 for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1)); // 3
+               assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendFundingLocked event in peer_handler for node 030200000000000000000000000000000000000000000000000000000000000000 for channel 3900000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1)); // 4
+               assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendRevokeAndACK event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000000 for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&4)); // 5
+               assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling UpdateHTLCs event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000000 with 0 adds, 0 fulfills, 0 fails for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&3)); // 6
+               assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling UpdateHTLCs event in peer_handler for node 030200000000000000000000000000000000000000000000000000000000000000 with 1 adds, 0 fulfills, 0 fails for channel 3900000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&3)); // 7
+               assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling UpdateHTLCs event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000000 with 0 adds, 1 fulfills, 0 fails for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1)); // 8
+               assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling UpdateHTLCs event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000000 with 0 adds, 0 fulfills, 1 fails for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&2)); // 9
+               assert_eq!(log_entries.get(&("lightning::ln::channelmonitor".to_string(), "Input spending remote commitment tx (00000000000000000000000000000000000000000000000000000000000000fd:0) in 0000000000000000000000000000000000000000000000000000000000000044 resolves outbound HTLC with payment hash ff00000000000000000000000000000000000000000000000000000000000000 with timeout".to_string())), Some(&1)); // 10
+       }
+}
diff --git a/lightning/fuzz/fuzz_targets/msg_targets/gen_target.sh b/lightning/fuzz/fuzz_targets/msg_targets/gen_target.sh
new file mode 100755 (executable)
index 0000000..cfd100f
--- /dev/null
@@ -0,0 +1,36 @@
+#!/bin/sh
+
+GEN_TEST() {
+       tn=$(echo $1 | sed 's/\([a-z0-9]\)\([A-Z]\)/\1_\2/g')
+       fn=msg_$(echo $tn | tr '[:upper:]' '[:lower:]')_target.rs
+       cat msg_target_template.txt | sed s/MSG_TARGET/$1/ | sed "s/TEST_MSG/$2/" | sed "s/EXTRA_ARGS/$3/" > $fn
+}
+
+GEN_TEST AcceptChannel test_msg ""
+GEN_TEST AnnouncementSignatures test_msg ""
+GEN_TEST ChannelReestablish test_msg ""
+GEN_TEST ClosingSigned test_msg ""
+GEN_TEST CommitmentSigned test_msg ""
+GEN_TEST DecodedOnionErrorPacket test_msg ""
+GEN_TEST FundingCreated test_msg ""
+GEN_TEST FundingLocked test_msg ""
+GEN_TEST FundingSigned test_msg ""
+GEN_TEST Init test_msg ""
+GEN_TEST OpenChannel test_msg ""
+GEN_TEST RevokeAndACK test_msg ""
+GEN_TEST Shutdown test_msg ""
+GEN_TEST UpdateFailHTLC test_msg ""
+GEN_TEST UpdateFailMalformedHTLC test_msg ""
+GEN_TEST UpdateFee test_msg ""
+GEN_TEST UpdateFulfillHTLC test_msg ""
+
+GEN_TEST ChannelAnnouncement test_msg_exact ""
+GEN_TEST ChannelUpdate test_msg_exact ""
+GEN_TEST NodeAnnouncement test_msg_exact ""
+
+GEN_TEST UpdateAddHTLC test_msg_hole ", 85, 33"
+GEN_TEST ErrorMessage test_msg_hole ", 32, 2"
+GEN_TEST OnionHopData test_msg_hole ", 1+8+8+4, 12"
+
+GEN_TEST Ping test_msg_simple ""
+GEN_TEST Pong test_msg_simple ""
diff --git a/lightning/fuzz/fuzz_targets/msg_targets/msg_accept_channel_target.rs b/lightning/fuzz/fuzz_targets/msg_targets/msg_accept_channel_target.rs
new file mode 100644 (file)
index 0000000..0f18d02
--- /dev/null
@@ -0,0 +1,43 @@
+// This file is auto-generated by gen_target.sh based on msg_target_template.txt
+// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
+
+extern crate lightning;
+
+use lightning::ln::msgs;
+
+mod utils;
+use utils::VecWriter;
+
+#[inline]
+pub fn do_test(data: &[u8]) {
+       test_msg!(msgs::AcceptChannel, data);
+}
+
+#[cfg(feature = "afl")]
+#[macro_use] extern crate afl;
+#[cfg(feature = "afl")]
+fn main() {
+       fuzz!(|data| {
+               do_test(data);
+       });
+}
+
+#[cfg(feature = "honggfuzz")]
+#[macro_use] extern crate honggfuzz;
+#[cfg(feature = "honggfuzz")]
+fn main() {
+       loop {
+               fuzz!(|data| {
+                       do_test(data);
+               });
+       }
+}
+
+extern crate hex;
+#[cfg(test)]
+mod tests {
+       #[test]
+       fn duplicate_crash() {
+               super::do_test(&::hex::decode("00").unwrap());
+       }
+}
diff --git a/lightning/fuzz/fuzz_targets/msg_targets/msg_announcement_signatures_target.rs b/lightning/fuzz/fuzz_targets/msg_targets/msg_announcement_signatures_target.rs
new file mode 100644 (file)
index 0000000..226028e
--- /dev/null
@@ -0,0 +1,43 @@
+// This file is auto-generated by gen_target.sh based on msg_target_template.txt
+// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
+
+extern crate lightning;
+
+use lightning::ln::msgs;
+
+mod utils;
+use utils::VecWriter;
+
+#[inline]
+pub fn do_test(data: &[u8]) {
+       test_msg!(msgs::AnnouncementSignatures, data);
+}
+
+#[cfg(feature = "afl")]
+#[macro_use] extern crate afl;
+#[cfg(feature = "afl")]
+fn main() {
+       fuzz!(|data| {
+               do_test(data);
+       });
+}
+
+#[cfg(feature = "honggfuzz")]
+#[macro_use] extern crate honggfuzz;
+#[cfg(feature = "honggfuzz")]
+fn main() {
+       loop {
+               fuzz!(|data| {
+                       do_test(data);
+               });
+       }
+}
+
+extern crate hex;
+#[cfg(test)]
+mod tests {
+       #[test]
+       fn duplicate_crash() {
+               super::do_test(&::hex::decode("00").unwrap());
+       }
+}
diff --git a/lightning/fuzz/fuzz_targets/msg_targets/msg_channel_announcement_target.rs b/lightning/fuzz/fuzz_targets/msg_targets/msg_channel_announcement_target.rs
new file mode 100644 (file)
index 0000000..0bdc10e
--- /dev/null
@@ -0,0 +1,43 @@
+// This file is auto-generated by gen_target.sh based on msg_target_template.txt
+// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
+
+extern crate lightning;
+
+use lightning::ln::msgs;
+
+mod utils;
+use utils::VecWriter;
+
+#[inline]
+pub fn do_test(data: &[u8]) {
+       test_msg_exact!(msgs::ChannelAnnouncement, data);
+}
+
+#[cfg(feature = "afl")]
+#[macro_use] extern crate afl;
+#[cfg(feature = "afl")]
+fn main() {
+       fuzz!(|data| {
+               do_test(data);
+       });
+}
+
+#[cfg(feature = "honggfuzz")]
+#[macro_use] extern crate honggfuzz;
+#[cfg(feature = "honggfuzz")]
+fn main() {
+       loop {
+               fuzz!(|data| {
+                       do_test(data);
+               });
+       }
+}
+
+extern crate hex;
+#[cfg(test)]
+mod tests {
+       #[test]
+       fn duplicate_crash() {
+               super::do_test(&::hex::decode("00").unwrap());
+       }
+}
diff --git a/lightning/fuzz/fuzz_targets/msg_targets/msg_channel_reestablish_target.rs b/lightning/fuzz/fuzz_targets/msg_targets/msg_channel_reestablish_target.rs
new file mode 100644 (file)
index 0000000..4af5937
--- /dev/null
@@ -0,0 +1,43 @@
+// This file is auto-generated by gen_target.sh based on msg_target_template.txt
+// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
+
+extern crate lightning;
+
+use lightning::ln::msgs;
+
+mod utils;
+use utils::VecWriter;
+
+#[inline]
+pub fn do_test(data: &[u8]) {
+       test_msg!(msgs::ChannelReestablish, data);
+}
+
+#[cfg(feature = "afl")]
+#[macro_use] extern crate afl;
+#[cfg(feature = "afl")]
+fn main() {
+       fuzz!(|data| {
+               do_test(data);
+       });
+}
+
+#[cfg(feature = "honggfuzz")]
+#[macro_use] extern crate honggfuzz;
+#[cfg(feature = "honggfuzz")]
+fn main() {
+       loop {
+               fuzz!(|data| {
+                       do_test(data);
+               });
+       }
+}
+
+extern crate hex;
+#[cfg(test)]
+mod tests {
+       #[test]
+       fn duplicate_crash() {
+               super::do_test(&::hex::decode("00").unwrap());
+       }
+}
diff --git a/lightning/fuzz/fuzz_targets/msg_targets/msg_channel_update_target.rs b/lightning/fuzz/fuzz_targets/msg_targets/msg_channel_update_target.rs
new file mode 100644 (file)
index 0000000..724dca4
--- /dev/null
@@ -0,0 +1,43 @@
+// This file is auto-generated by gen_target.sh based on msg_target_template.txt
+// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
+
+extern crate lightning;
+
+use lightning::ln::msgs;
+
+mod utils;
+use utils::VecWriter;
+
+#[inline]
+pub fn do_test(data: &[u8]) {
+       test_msg_exact!(msgs::ChannelUpdate, data);
+}
+
+#[cfg(feature = "afl")]
+#[macro_use] extern crate afl;
+#[cfg(feature = "afl")]
+fn main() {
+       fuzz!(|data| {
+               do_test(data);
+       });
+}
+
+#[cfg(feature = "honggfuzz")]
+#[macro_use] extern crate honggfuzz;
+#[cfg(feature = "honggfuzz")]
+fn main() {
+       loop {
+               fuzz!(|data| {
+                       do_test(data);
+               });
+       }
+}
+
+extern crate hex;
+#[cfg(test)]
+mod tests {
+       #[test]
+       fn duplicate_crash() {
+               super::do_test(&::hex::decode("00").unwrap());
+       }
+}
diff --git a/lightning/fuzz/fuzz_targets/msg_targets/msg_closing_signed_target.rs b/lightning/fuzz/fuzz_targets/msg_targets/msg_closing_signed_target.rs
new file mode 100644 (file)
index 0000000..faeeae3
--- /dev/null
@@ -0,0 +1,43 @@
+// This file is auto-generated by gen_target.sh based on msg_target_template.txt
+// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
+
+extern crate lightning;
+
+use lightning::ln::msgs;
+
+mod utils;
+use utils::VecWriter;
+
+#[inline]
+pub fn do_test(data: &[u8]) {
+       test_msg!(msgs::ClosingSigned, data);
+}
+
+#[cfg(feature = "afl")]
+#[macro_use] extern crate afl;
+#[cfg(feature = "afl")]
+fn main() {
+       fuzz!(|data| {
+               do_test(data);
+       });
+}
+
+#[cfg(feature = "honggfuzz")]
+#[macro_use] extern crate honggfuzz;
+#[cfg(feature = "honggfuzz")]
+fn main() {
+       loop {
+               fuzz!(|data| {
+                       do_test(data);
+               });
+       }
+}
+
+extern crate hex;
+#[cfg(test)]
+mod tests {
+       #[test]
+       fn duplicate_crash() {
+               super::do_test(&::hex::decode("00").unwrap());
+       }
+}
diff --git a/lightning/fuzz/fuzz_targets/msg_targets/msg_commitment_signed_target.rs b/lightning/fuzz/fuzz_targets/msg_targets/msg_commitment_signed_target.rs
new file mode 100644 (file)
index 0000000..97c4b30
--- /dev/null
@@ -0,0 +1,43 @@
+// This file is auto-generated by gen_target.sh based on msg_target_template.txt
+// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
+
+extern crate lightning;
+
+use lightning::ln::msgs;
+
+mod utils;
+use utils::VecWriter;
+
+#[inline]
+pub fn do_test(data: &[u8]) {
+       test_msg!(msgs::CommitmentSigned, data);
+}
+
+#[cfg(feature = "afl")]
+#[macro_use] extern crate afl;
+#[cfg(feature = "afl")]
+fn main() {
+       fuzz!(|data| {
+               do_test(data);
+       });
+}
+
+#[cfg(feature = "honggfuzz")]
+#[macro_use] extern crate honggfuzz;
+#[cfg(feature = "honggfuzz")]
+fn main() {
+       loop {
+               fuzz!(|data| {
+                       do_test(data);
+               });
+       }
+}
+
+extern crate hex;
+#[cfg(test)]
+mod tests {
+       #[test]
+       fn duplicate_crash() {
+               super::do_test(&::hex::decode("00").unwrap());
+       }
+}
diff --git a/lightning/fuzz/fuzz_targets/msg_targets/msg_decoded_onion_error_packet_target.rs b/lightning/fuzz/fuzz_targets/msg_targets/msg_decoded_onion_error_packet_target.rs
new file mode 100644 (file)
index 0000000..9b190b0
--- /dev/null
@@ -0,0 +1,43 @@
+// This file is auto-generated by gen_target.sh based on msg_target_template.txt
+// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
+
+extern crate lightning;
+
+use lightning::ln::msgs;
+
+mod utils;
+use utils::VecWriter;
+
+#[inline]
+pub fn do_test(data: &[u8]) {
+       test_msg!(msgs::DecodedOnionErrorPacket, data);
+}
+
+#[cfg(feature = "afl")]
+#[macro_use] extern crate afl;
+#[cfg(feature = "afl")]
+fn main() {
+       fuzz!(|data| {
+               do_test(data);
+       });
+}
+
+#[cfg(feature = "honggfuzz")]
+#[macro_use] extern crate honggfuzz;
+#[cfg(feature = "honggfuzz")]
+fn main() {
+       loop {
+               fuzz!(|data| {
+                       do_test(data);
+               });
+       }
+}
+
+extern crate hex;
+#[cfg(test)]
+mod tests {
+       #[test]
+       fn duplicate_crash() {
+               super::do_test(&::hex::decode("00").unwrap());
+       }
+}
diff --git a/lightning/fuzz/fuzz_targets/msg_targets/msg_error_message_target.rs b/lightning/fuzz/fuzz_targets/msg_targets/msg_error_message_target.rs
new file mode 100644 (file)
index 0000000..d749dc9
--- /dev/null
@@ -0,0 +1,43 @@
+// This file is auto-generated by gen_target.sh based on msg_target_template.txt
+// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
+
+extern crate lightning;
+
+use lightning::ln::msgs;
+
+mod utils;
+use utils::VecWriter;
+
+#[inline]
+pub fn do_test(data: &[u8]) {
+       test_msg_hole!(msgs::ErrorMessage, data, 32, 2);
+}
+
+#[cfg(feature = "afl")]
+#[macro_use] extern crate afl;
+#[cfg(feature = "afl")]
+fn main() {
+       fuzz!(|data| {
+               do_test(data);
+       });
+}
+
+#[cfg(feature = "honggfuzz")]
+#[macro_use] extern crate honggfuzz;
+#[cfg(feature = "honggfuzz")]
+fn main() {
+       loop {
+               fuzz!(|data| {
+                       do_test(data);
+               });
+       }
+}
+
+extern crate hex;
+#[cfg(test)]
+mod tests {
+       #[test]
+       fn duplicate_crash() {
+               super::do_test(&::hex::decode("00").unwrap());
+       }
+}
diff --git a/lightning/fuzz/fuzz_targets/msg_targets/msg_funding_created_target.rs b/lightning/fuzz/fuzz_targets/msg_targets/msg_funding_created_target.rs
new file mode 100644 (file)
index 0000000..45b257b
--- /dev/null
@@ -0,0 +1,43 @@
+// This file is auto-generated by gen_target.sh based on msg_target_template.txt
+// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
+
+extern crate lightning;
+
+use lightning::ln::msgs;
+
+mod utils;
+use utils::VecWriter;
+
+#[inline]
+pub fn do_test(data: &[u8]) {
+       test_msg!(msgs::FundingCreated, data);
+}
+
+#[cfg(feature = "afl")]
+#[macro_use] extern crate afl;
+#[cfg(feature = "afl")]
+fn main() {
+       fuzz!(|data| {
+               do_test(data);
+       });
+}
+
+#[cfg(feature = "honggfuzz")]
+#[macro_use] extern crate honggfuzz;
+#[cfg(feature = "honggfuzz")]
+fn main() {
+       loop {
+               fuzz!(|data| {
+                       do_test(data);
+               });
+       }
+}
+
+extern crate hex;
+#[cfg(test)]
+mod tests {
+       #[test]
+       fn duplicate_crash() {
+               super::do_test(&::hex::decode("00").unwrap());
+       }
+}
diff --git a/lightning/fuzz/fuzz_targets/msg_targets/msg_funding_locked_target.rs b/lightning/fuzz/fuzz_targets/msg_targets/msg_funding_locked_target.rs
new file mode 100644 (file)
index 0000000..cd1e897
--- /dev/null
@@ -0,0 +1,43 @@
+// This file is auto-generated by gen_target.sh based on msg_target_template.txt
+// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
+
+extern crate lightning;
+
+use lightning::ln::msgs;
+
+mod utils;
+use utils::VecWriter;
+
+#[inline]
+pub fn do_test(data: &[u8]) {
+       test_msg!(msgs::FundingLocked, data);
+}
+
+#[cfg(feature = "afl")]
+#[macro_use] extern crate afl;
+#[cfg(feature = "afl")]
+fn main() {
+       fuzz!(|data| {
+               do_test(data);
+       });
+}
+
+#[cfg(feature = "honggfuzz")]
+#[macro_use] extern crate honggfuzz;
+#[cfg(feature = "honggfuzz")]
+fn main() {
+       loop {
+               fuzz!(|data| {
+                       do_test(data);
+               });
+       }
+}
+
+extern crate hex;
+#[cfg(test)]
+mod tests {
+       #[test]
+       fn duplicate_crash() {
+               super::do_test(&::hex::decode("00").unwrap());
+       }
+}
diff --git a/lightning/fuzz/fuzz_targets/msg_targets/msg_funding_signed_target.rs b/lightning/fuzz/fuzz_targets/msg_targets/msg_funding_signed_target.rs
new file mode 100644 (file)
index 0000000..5992d69
--- /dev/null
@@ -0,0 +1,43 @@
+// This file is auto-generated by gen_target.sh based on msg_target_template.txt
+// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
+
+extern crate lightning;
+
+use lightning::ln::msgs;
+
+mod utils;
+use utils::VecWriter;
+
+#[inline]
+pub fn do_test(data: &[u8]) {
+       test_msg!(msgs::FundingSigned, data);
+}
+
+#[cfg(feature = "afl")]
+#[macro_use] extern crate afl;
+#[cfg(feature = "afl")]
+fn main() {
+       fuzz!(|data| {
+               do_test(data);
+       });
+}
+
+#[cfg(feature = "honggfuzz")]
+#[macro_use] extern crate honggfuzz;
+#[cfg(feature = "honggfuzz")]
+fn main() {
+       loop {
+               fuzz!(|data| {
+                       do_test(data);
+               });
+       }
+}
+
+extern crate hex;
+#[cfg(test)]
+mod tests {
+       #[test]
+       fn duplicate_crash() {
+               super::do_test(&::hex::decode("00").unwrap());
+       }
+}
diff --git a/lightning/fuzz/fuzz_targets/msg_targets/msg_init_target.rs b/lightning/fuzz/fuzz_targets/msg_targets/msg_init_target.rs
new file mode 100644 (file)
index 0000000..cdca848
--- /dev/null
@@ -0,0 +1,43 @@
+// This file is auto-generated by gen_target.sh based on msg_target_template.txt
+// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
+
+extern crate lightning;
+
+use lightning::ln::msgs;
+
+mod utils;
+use utils::VecWriter;
+
+#[inline]
+pub fn do_test(data: &[u8]) {
+       test_msg!(msgs::Init, data);
+}
+
+#[cfg(feature = "afl")]
+#[macro_use] extern crate afl;
+#[cfg(feature = "afl")]
+fn main() {
+       fuzz!(|data| {
+               do_test(data);
+       });
+}
+
+#[cfg(feature = "honggfuzz")]
+#[macro_use] extern crate honggfuzz;
+#[cfg(feature = "honggfuzz")]
+fn main() {
+       loop {
+               fuzz!(|data| {
+                       do_test(data);
+               });
+       }
+}
+
+extern crate hex;
+#[cfg(test)]
+mod tests {
+       #[test]
+       fn duplicate_crash() {
+               super::do_test(&::hex::decode("00").unwrap());
+       }
+}
diff --git a/lightning/fuzz/fuzz_targets/msg_targets/msg_node_announcement_target.rs b/lightning/fuzz/fuzz_targets/msg_targets/msg_node_announcement_target.rs
new file mode 100644 (file)
index 0000000..f0a7a4c
--- /dev/null
@@ -0,0 +1,43 @@
+// This file is auto-generated by gen_target.sh based on msg_target_template.txt
+// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
+
+extern crate lightning;
+
+use lightning::ln::msgs;
+
+mod utils;
+use utils::VecWriter;
+
+#[inline]
+pub fn do_test(data: &[u8]) {
+       test_msg_exact!(msgs::NodeAnnouncement, data);
+}
+
+#[cfg(feature = "afl")]
+#[macro_use] extern crate afl;
+#[cfg(feature = "afl")]
+fn main() {
+       fuzz!(|data| {
+               do_test(data);
+       });
+}
+
+#[cfg(feature = "honggfuzz")]
+#[macro_use] extern crate honggfuzz;
+#[cfg(feature = "honggfuzz")]
+fn main() {
+       loop {
+               fuzz!(|data| {
+                       do_test(data);
+               });
+       }
+}
+
+extern crate hex;
+#[cfg(test)]
+mod tests {
+       #[test]
+       fn duplicate_crash() {
+               super::do_test(&::hex::decode("00").unwrap());
+       }
+}
diff --git a/lightning/fuzz/fuzz_targets/msg_targets/msg_onion_hop_data_target.rs b/lightning/fuzz/fuzz_targets/msg_targets/msg_onion_hop_data_target.rs
new file mode 100644 (file)
index 0000000..058c050
--- /dev/null
@@ -0,0 +1,43 @@
+// This file is auto-generated by gen_target.sh based on msg_target_template.txt
+// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
+
+extern crate lightning;
+
+use lightning::ln::msgs;
+
+mod utils;
+use utils::VecWriter;
+
+#[inline]
+pub fn do_test(data: &[u8]) {
+       test_msg_hole!(msgs::OnionHopData, data, 1+8+8+4, 12);
+}
+
+#[cfg(feature = "afl")]
+#[macro_use] extern crate afl;
+#[cfg(feature = "afl")]
+fn main() {
+       fuzz!(|data| {
+               do_test(data);
+       });
+}
+
+#[cfg(feature = "honggfuzz")]
+#[macro_use] extern crate honggfuzz;
+#[cfg(feature = "honggfuzz")]
+fn main() {
+       loop {
+               fuzz!(|data| {
+                       do_test(data);
+               });
+       }
+}
+
+extern crate hex;
+#[cfg(test)]
+mod tests {
+       #[test]
+       fn duplicate_crash() {
+               super::do_test(&::hex::decode("00").unwrap());
+       }
+}
diff --git a/lightning/fuzz/fuzz_targets/msg_targets/msg_open_channel_target.rs b/lightning/fuzz/fuzz_targets/msg_targets/msg_open_channel_target.rs
new file mode 100644 (file)
index 0000000..aa13e96
--- /dev/null
@@ -0,0 +1,43 @@
+// This file is auto-generated by gen_target.sh based on msg_target_template.txt
+// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
+
+extern crate lightning;
+
+use lightning::ln::msgs;
+
+mod utils;
+use utils::VecWriter;
+
+#[inline]
+pub fn do_test(data: &[u8]) {
+       test_msg!(msgs::OpenChannel, data);
+}
+
+#[cfg(feature = "afl")]
+#[macro_use] extern crate afl;
+#[cfg(feature = "afl")]
+fn main() {
+       fuzz!(|data| {
+               do_test(data);
+       });
+}
+
+#[cfg(feature = "honggfuzz")]
+#[macro_use] extern crate honggfuzz;
+#[cfg(feature = "honggfuzz")]
+fn main() {
+       loop {
+               fuzz!(|data| {
+                       do_test(data);
+               });
+       }
+}
+
+extern crate hex;
+#[cfg(test)]
+mod tests {
+       #[test]
+       fn duplicate_crash() {
+               super::do_test(&::hex::decode("00").unwrap());
+       }
+}
diff --git a/lightning/fuzz/fuzz_targets/msg_targets/msg_ping_target.rs b/lightning/fuzz/fuzz_targets/msg_targets/msg_ping_target.rs
new file mode 100644 (file)
index 0000000..d2ea913
--- /dev/null
@@ -0,0 +1,43 @@
+// This file is auto-generated by gen_target.sh based on msg_target_template.txt
+// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
+
+extern crate lightning;
+
+use lightning::ln::msgs;
+
+mod utils;
+use utils::VecWriter;
+
+#[inline]
+pub fn do_test(data: &[u8]) {
+       test_msg_simple!(msgs::Ping, data);
+}
+
+#[cfg(feature = "afl")]
+#[macro_use] extern crate afl;
+#[cfg(feature = "afl")]
+fn main() {
+       fuzz!(|data| {
+               do_test(data);
+       });
+}
+
+#[cfg(feature = "honggfuzz")]
+#[macro_use] extern crate honggfuzz;
+#[cfg(feature = "honggfuzz")]
+fn main() {
+       loop {
+               fuzz!(|data| {
+                       do_test(data);
+               });
+       }
+}
+
+extern crate hex;
+#[cfg(test)]
+mod tests {
+       #[test]
+       fn duplicate_crash() {
+               super::do_test(&::hex::decode("00").unwrap());
+       }
+}
diff --git a/lightning/fuzz/fuzz_targets/msg_targets/msg_pong_target.rs b/lightning/fuzz/fuzz_targets/msg_targets/msg_pong_target.rs
new file mode 100644 (file)
index 0000000..18120e2
--- /dev/null
@@ -0,0 +1,43 @@
+// This file is auto-generated by gen_target.sh based on msg_target_template.txt
+// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
+
+extern crate lightning;
+
+use lightning::ln::msgs;
+
+mod utils;
+use utils::VecWriter;
+
+#[inline]
+pub fn do_test(data: &[u8]) {
+       test_msg_simple!(msgs::Pong, data);
+}
+
+#[cfg(feature = "afl")]
+#[macro_use] extern crate afl;
+#[cfg(feature = "afl")]
+fn main() {
+       fuzz!(|data| {
+               do_test(data);
+       });
+}
+
+#[cfg(feature = "honggfuzz")]
+#[macro_use] extern crate honggfuzz;
+#[cfg(feature = "honggfuzz")]
+fn main() {
+       loop {
+               fuzz!(|data| {
+                       do_test(data);
+               });
+       }
+}
+
+extern crate hex;
+#[cfg(test)]
+mod tests {
+       #[test]
+       fn duplicate_crash() {
+               super::do_test(&::hex::decode("00").unwrap());
+       }
+}
diff --git a/lightning/fuzz/fuzz_targets/msg_targets/msg_revoke_and_ack_target.rs b/lightning/fuzz/fuzz_targets/msg_targets/msg_revoke_and_ack_target.rs
new file mode 100644 (file)
index 0000000..d82268d
--- /dev/null
@@ -0,0 +1,43 @@
+// This file is auto-generated by gen_target.sh based on msg_target_template.txt
+// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
+
+extern crate lightning;
+
+use lightning::ln::msgs;
+
+mod utils;
+use utils::VecWriter;
+
+#[inline]
+pub fn do_test(data: &[u8]) {
+       test_msg!(msgs::RevokeAndACK, data);
+}
+
+#[cfg(feature = "afl")]
+#[macro_use] extern crate afl;
+#[cfg(feature = "afl")]
+fn main() {
+       fuzz!(|data| {
+               do_test(data);
+       });
+}
+
+#[cfg(feature = "honggfuzz")]
+#[macro_use] extern crate honggfuzz;
+#[cfg(feature = "honggfuzz")]
+fn main() {
+       loop {
+               fuzz!(|data| {
+                       do_test(data);
+               });
+       }
+}
+
+extern crate hex;
+#[cfg(test)]
+mod tests {
+       #[test]
+       fn duplicate_crash() {
+               super::do_test(&::hex::decode("00").unwrap());
+       }
+}
diff --git a/lightning/fuzz/fuzz_targets/msg_targets/msg_shutdown_target.rs b/lightning/fuzz/fuzz_targets/msg_targets/msg_shutdown_target.rs
new file mode 100644 (file)
index 0000000..34d4d20
--- /dev/null
@@ -0,0 +1,43 @@
+// This file is auto-generated by gen_target.sh based on msg_target_template.txt
+// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
+
+extern crate lightning;
+
+use lightning::ln::msgs;
+
+mod utils;
+use utils::VecWriter;
+
+#[inline]
+pub fn do_test(data: &[u8]) {
+       test_msg!(msgs::Shutdown, data);
+}
+
+#[cfg(feature = "afl")]
+#[macro_use] extern crate afl;
+#[cfg(feature = "afl")]
+fn main() {
+       fuzz!(|data| {
+               do_test(data);
+       });
+}
+
+#[cfg(feature = "honggfuzz")]
+#[macro_use] extern crate honggfuzz;
+#[cfg(feature = "honggfuzz")]
+fn main() {
+       loop {
+               fuzz!(|data| {
+                       do_test(data);
+               });
+       }
+}
+
+extern crate hex;
+#[cfg(test)]
+mod tests {
+       #[test]
+       fn duplicate_crash() {
+               super::do_test(&::hex::decode("00").unwrap());
+       }
+}
diff --git a/lightning/fuzz/fuzz_targets/msg_targets/msg_target_template.txt b/lightning/fuzz/fuzz_targets/msg_targets/msg_target_template.txt
new file mode 100644 (file)
index 0000000..2704bcd
--- /dev/null
@@ -0,0 +1,43 @@
+// This file is auto-generated by gen_target.sh based on msg_target_template.txt
+// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
+
+extern crate lightning;
+
+use lightning::ln::msgs;
+
+mod utils;
+use utils::VecWriter;
+
+#[inline]
+pub fn do_test(data: &[u8]) {
+       TEST_MSG!(msgs::MSG_TARGET, dataEXTRA_ARGS);
+}
+
+#[cfg(feature = "afl")]
+#[macro_use] extern crate afl;
+#[cfg(feature = "afl")]
+fn main() {
+       fuzz!(|data| {
+               do_test(data);
+       });
+}
+
+#[cfg(feature = "honggfuzz")]
+#[macro_use] extern crate honggfuzz;
+#[cfg(feature = "honggfuzz")]
+fn main() {
+       loop {
+               fuzz!(|data| {
+                       do_test(data);
+               });
+       }
+}
+
+extern crate hex;
+#[cfg(test)]
+mod tests {
+       #[test]
+       fn duplicate_crash() {
+               super::do_test(&::hex::decode("00").unwrap());
+       }
+}
diff --git a/lightning/fuzz/fuzz_targets/msg_targets/msg_update_add_htlc_target.rs b/lightning/fuzz/fuzz_targets/msg_targets/msg_update_add_htlc_target.rs
new file mode 100644 (file)
index 0000000..e64a5c2
--- /dev/null
@@ -0,0 +1,43 @@
+// This file is auto-generated by gen_target.sh based on msg_target_template.txt
+// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
+
+extern crate lightning;
+
+use lightning::ln::msgs;
+
+mod utils;
+use utils::VecWriter;
+
+#[inline]
+pub fn do_test(data: &[u8]) {
+       test_msg_hole!(msgs::UpdateAddHTLC, data, 85, 33);
+}
+
+#[cfg(feature = "afl")]
+#[macro_use] extern crate afl;
+#[cfg(feature = "afl")]
+fn main() {
+       fuzz!(|data| {
+               do_test(data);
+       });
+}
+
+#[cfg(feature = "honggfuzz")]
+#[macro_use] extern crate honggfuzz;
+#[cfg(feature = "honggfuzz")]
+fn main() {
+       loop {
+               fuzz!(|data| {
+                       do_test(data);
+               });
+       }
+}
+
+extern crate hex;
+#[cfg(test)]
+mod tests {
+       #[test]
+       fn duplicate_crash() {
+               super::do_test(&::hex::decode("00").unwrap());
+       }
+}
diff --git a/lightning/fuzz/fuzz_targets/msg_targets/msg_update_fail_htlc_target.rs b/lightning/fuzz/fuzz_targets/msg_targets/msg_update_fail_htlc_target.rs
new file mode 100644 (file)
index 0000000..fedce56
--- /dev/null
@@ -0,0 +1,43 @@
+// This file is auto-generated by gen_target.sh based on msg_target_template.txt
+// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
+
+extern crate lightning;
+
+use lightning::ln::msgs;
+
+mod utils;
+use utils::VecWriter;
+
+#[inline]
+pub fn do_test(data: &[u8]) {
+       test_msg!(msgs::UpdateFailHTLC, data);
+}
+
+#[cfg(feature = "afl")]
+#[macro_use] extern crate afl;
+#[cfg(feature = "afl")]
+fn main() {
+       fuzz!(|data| {
+               do_test(data);
+       });
+}
+
+#[cfg(feature = "honggfuzz")]
+#[macro_use] extern crate honggfuzz;
+#[cfg(feature = "honggfuzz")]
+fn main() {
+       loop {
+               fuzz!(|data| {
+                       do_test(data);
+               });
+       }
+}
+
+extern crate hex;
+#[cfg(test)]
+mod tests {
+       #[test]
+       fn duplicate_crash() {
+               super::do_test(&::hex::decode("00").unwrap());
+       }
+}
diff --git a/lightning/fuzz/fuzz_targets/msg_targets/msg_update_fail_malformed_htlc_target.rs b/lightning/fuzz/fuzz_targets/msg_targets/msg_update_fail_malformed_htlc_target.rs
new file mode 100644 (file)
index 0000000..377378f
--- /dev/null
@@ -0,0 +1,43 @@
+// This file is auto-generated by gen_target.sh based on msg_target_template.txt
+// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
+
+extern crate lightning;
+
+use lightning::ln::msgs;
+
+mod utils;
+use utils::VecWriter;
+
+#[inline]
+pub fn do_test(data: &[u8]) {
+       test_msg!(msgs::UpdateFailMalformedHTLC, data);
+}
+
+#[cfg(feature = "afl")]
+#[macro_use] extern crate afl;
+#[cfg(feature = "afl")]
+fn main() {
+       fuzz!(|data| {
+               do_test(data);
+       });
+}
+
+#[cfg(feature = "honggfuzz")]
+#[macro_use] extern crate honggfuzz;
+#[cfg(feature = "honggfuzz")]
+fn main() {
+       loop {
+               fuzz!(|data| {
+                       do_test(data);
+               });
+       }
+}
+
+extern crate hex;
+#[cfg(test)]
+mod tests {
+       #[test]
+       fn duplicate_crash() {
+               super::do_test(&::hex::decode("00").unwrap());
+       }
+}
diff --git a/lightning/fuzz/fuzz_targets/msg_targets/msg_update_fee_target.rs b/lightning/fuzz/fuzz_targets/msg_targets/msg_update_fee_target.rs
new file mode 100644 (file)
index 0000000..56b9ac4
--- /dev/null
@@ -0,0 +1,43 @@
+// This file is auto-generated by gen_target.sh based on msg_target_template.txt
+// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
+
+extern crate lightning;
+
+use lightning::ln::msgs;
+
+mod utils;
+use utils::VecWriter;
+
+#[inline]
+pub fn do_test(data: &[u8]) {
+       test_msg!(msgs::UpdateFee, data);
+}
+
+#[cfg(feature = "afl")]
+#[macro_use] extern crate afl;
+#[cfg(feature = "afl")]
+fn main() {
+       fuzz!(|data| {
+               do_test(data);
+       });
+}
+
+#[cfg(feature = "honggfuzz")]
+#[macro_use] extern crate honggfuzz;
+#[cfg(feature = "honggfuzz")]
+fn main() {
+       loop {
+               fuzz!(|data| {
+                       do_test(data);
+               });
+       }
+}
+
+extern crate hex;
+#[cfg(test)]
+mod tests {
+       #[test]
+       fn duplicate_crash() {
+               super::do_test(&::hex::decode("00").unwrap());
+       }
+}
diff --git a/lightning/fuzz/fuzz_targets/msg_targets/msg_update_fulfill_htlc_target.rs b/lightning/fuzz/fuzz_targets/msg_targets/msg_update_fulfill_htlc_target.rs
new file mode 100644 (file)
index 0000000..f0c936d
--- /dev/null
@@ -0,0 +1,43 @@
+// This file is auto-generated by gen_target.sh based on msg_target_template.txt
+// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
+
+extern crate lightning;
+
+use lightning::ln::msgs;
+
+mod utils;
+use utils::VecWriter;
+
+#[inline]
+pub fn do_test(data: &[u8]) {
+       test_msg!(msgs::UpdateFulfillHTLC, data);
+}
+
+#[cfg(feature = "afl")]
+#[macro_use] extern crate afl;
+#[cfg(feature = "afl")]
+fn main() {
+       fuzz!(|data| {
+               do_test(data);
+       });
+}
+
+#[cfg(feature = "honggfuzz")]
+#[macro_use] extern crate honggfuzz;
+#[cfg(feature = "honggfuzz")]
+fn main() {
+       loop {
+               fuzz!(|data| {
+                       do_test(data);
+               });
+       }
+}
+
+extern crate hex;
+#[cfg(test)]
+mod tests {
+       #[test]
+       fn duplicate_crash() {
+               super::do_test(&::hex::decode("00").unwrap());
+       }
+}
diff --git a/lightning/fuzz/fuzz_targets/msg_targets/utils.rs b/lightning/fuzz/fuzz_targets/msg_targets/utils.rs
new file mode 100644 (file)
index 0000000..a5257ba
--- /dev/null
@@ -0,0 +1,81 @@
+#![macro_use]
+
+use lightning::util::ser::Writer;
+pub struct VecWriter(pub Vec<u8>);
+impl Writer for VecWriter {
+       fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
+               assert!(self.0.capacity() >= self.0.len() + buf.len());
+               self.0.extend_from_slice(buf);
+               Ok(())
+       }
+       fn size_hint(&mut self, size: usize) {
+               self.0.reserve_exact(size);
+       }
+}
+
+#[macro_export]
+macro_rules! test_msg {
+       ($MsgType: path, $data: ident) => {
+               {
+                       use lightning::util::ser::{Writeable, Readable};
+                       let mut r = ::std::io::Cursor::new($data);
+                       if let Ok(msg) = <$MsgType as Readable<::std::io::Cursor<&[u8]>>>::read(&mut r) {
+                               let p = r.position() as usize;
+                               let mut w = VecWriter(Vec::new());
+                               msg.write(&mut w).unwrap();
+
+                               assert_eq!(w.0.len(), p);
+                               assert_eq!(&r.into_inner()[..p], &w.0[..p]);
+                       }
+               }
+       }
+}
+
+#[macro_export]
+macro_rules! test_msg_simple {
+       ($MsgType: path, $data: ident) => {
+               {
+                       use lightning::util::ser::{Writeable, Readable};
+                       let mut r = ::std::io::Cursor::new($data);
+                       if let Ok(msg) = <$MsgType as Readable<::std::io::Cursor<&[u8]>>>::read(&mut r) {
+                               let mut w = VecWriter(Vec::new());
+                               msg.write(&mut w).unwrap();
+                       }
+               }
+       }
+}
+
+#[macro_export]
+macro_rules! test_msg_exact {
+       ($MsgType: path, $data: ident) => {
+               {
+                       use lightning::util::ser::{Writeable, Readable};
+                       let mut r = ::std::io::Cursor::new($data);
+                       if let Ok(msg) = <$MsgType as Readable<::std::io::Cursor<&[u8]>>>::read(&mut r) {
+                               let mut w = VecWriter(Vec::new());
+                               msg.write(&mut w).unwrap();
+
+                               assert_eq!(&r.into_inner()[..], &w.0[..]);
+                       }
+               }
+       }
+}
+
+#[macro_export]
+macro_rules! test_msg_hole {
+       ($MsgType: path, $data: ident, $hole: expr, $hole_len: expr) => {
+               {
+                       use lightning::util::ser::{Writeable, Readable};
+                       let mut r = ::std::io::Cursor::new($data);
+                       if let Ok(msg) = <$MsgType as Readable<::std::io::Cursor<&[u8]>>>::read(&mut r) {
+                               let mut w = VecWriter(Vec::new());
+                               msg.write(&mut w).unwrap();
+                               let p = w.0.len() as usize;
+
+                               assert_eq!(w.0.len(), p);
+                               assert_eq!(&r.get_ref()[..$hole], &w.0[..$hole]);
+                               assert_eq!(&r.get_ref()[$hole+$hole_len..p], &w.0[$hole+$hole_len..]);
+                       }
+               }
+       }
+}
diff --git a/lightning/fuzz/fuzz_targets/peer_crypt_target.rs b/lightning/fuzz/fuzz_targets/peer_crypt_target.rs
new file mode 100644 (file)
index 0000000..0b82303
--- /dev/null
@@ -0,0 +1,109 @@
+extern crate lightning;
+extern crate secp256k1;
+
+use lightning::ln::peer_channel_encryptor::PeerChannelEncryptor;
+
+use secp256k1::key::{PublicKey,SecretKey};
+
+#[inline]
+fn slice_to_be16(v: &[u8]) -> u16 {
+       ((v[0] as u16) << 8*1) |
+       ((v[1] as u16) << 8*0)
+}
+
+#[inline]
+pub fn do_test(data: &[u8]) {
+       let mut read_pos = 0;
+       macro_rules! get_slice {
+               ($len: expr) => {
+                       {
+                               let slice_len = $len as usize;
+                               if data.len() < read_pos + slice_len {
+                                       return;
+                               }
+                               read_pos += slice_len;
+                               &data[read_pos - slice_len..read_pos]
+                       }
+               }
+       }
+
+       let our_network_key = match SecretKey::from_slice(get_slice!(32)) {
+               Ok(key) => key,
+               Err(_) => return,
+       };
+       let ephemeral_key = match SecretKey::from_slice(get_slice!(32)) {
+               Ok(key) => key,
+               Err(_) => return,
+       };
+
+       let mut crypter = if get_slice!(1)[0] != 0 {
+               let their_pubkey = match PublicKey::from_slice(get_slice!(33)) {
+                       Ok(key) => key,
+                       Err(_) => return,
+               };
+               let mut crypter = PeerChannelEncryptor::new_outbound(their_pubkey, ephemeral_key);
+               crypter.get_act_one();
+               match crypter.process_act_two(get_slice!(50), &our_network_key) {
+                       Ok(_) => {},
+                       Err(_) => return,
+               }
+               assert!(crypter.is_ready_for_encryption());
+               crypter
+       } else {
+               let mut crypter = PeerChannelEncryptor::new_inbound(&our_network_key);
+               match crypter.process_act_one_with_keys(get_slice!(50), &our_network_key, ephemeral_key) {
+                       Ok(_) => {},
+                       Err(_) => return,
+               }
+               match crypter.process_act_three(get_slice!(66)) {
+                       Ok(_) => {},
+                       Err(_) => return,
+               }
+               assert!(crypter.is_ready_for_encryption());
+               crypter
+       };
+       loop {
+               if get_slice!(1)[0] == 0 {
+                       crypter.encrypt_message(get_slice!(slice_to_be16(get_slice!(2))));
+               } else {
+                       let len = match crypter.decrypt_length_header(get_slice!(16+2)) {
+                               Ok(len) => len,
+                               Err(_) => return,
+                       };
+                       match crypter.decrypt_message(get_slice!(len as usize + 16)) {
+                               Ok(_) => {},
+                               Err(_) => return,
+                       }
+               }
+       }
+}
+
+#[cfg(feature = "afl")]
+#[macro_use] extern crate afl;
+#[cfg(feature = "afl")]
+fn main() {
+       fuzz!(|data| {
+               do_test(data);
+       });
+}
+
+#[cfg(feature = "honggfuzz")]
+#[macro_use] extern crate honggfuzz;
+#[cfg(feature = "honggfuzz")]
+fn main() {
+       loop {
+               fuzz!(|data| {
+                       do_test(data);
+               });
+       }
+}
+
+extern crate hex;
+#[cfg(test)]
+mod tests {
+
+       #[test]
+       fn duplicate_crash() {
+               super::do_test(&::hex::decode("01").unwrap());
+       }
+}
diff --git a/lightning/fuzz/fuzz_targets/router_target.rs b/lightning/fuzz/fuzz_targets/router_target.rs
new file mode 100644 (file)
index 0000000..25bf382
--- /dev/null
@@ -0,0 +1,264 @@
+extern crate bitcoin;
+extern crate bitcoin_hashes;
+extern crate lightning;
+extern crate secp256k1;
+
+use bitcoin_hashes::sha256d::Hash as Sha256dHash;
+use bitcoin::blockdata::script::{Script, Builder};
+
+use lightning::chain::chaininterface::{ChainError,ChainWatchInterface, ChainListener};
+use lightning::ln::channelmanager::ChannelDetails;
+use lightning::ln::msgs;
+use lightning::ln::msgs::{RoutingMessageHandler};
+use lightning::ln::router::{Router, RouteHint};
+use lightning::util::logger::Logger;
+use lightning::util::ser::Readable;
+
+use secp256k1::key::PublicKey;
+
+mod utils;
+
+use utils::test_logger;
+
+use std::sync::{Weak, Arc};
+use std::sync::atomic::{AtomicUsize, Ordering};
+
+#[inline]
+pub fn slice_to_be16(v: &[u8]) -> u16 {
+       ((v[0] as u16) << 8*1) |
+       ((v[1] as u16) << 8*0)
+}
+
+#[inline]
+pub fn slice_to_be32(v: &[u8]) -> u32 {
+       ((v[0] as u32) << 8*3) |
+       ((v[1] as u32) << 8*2) |
+       ((v[2] as u32) << 8*1) |
+       ((v[3] as u32) << 8*0)
+}
+
+#[inline]
+pub fn slice_to_be64(v: &[u8]) -> u64 {
+       ((v[0] as u64) << 8*7) |
+       ((v[1] as u64) << 8*6) |
+       ((v[2] as u64) << 8*5) |
+       ((v[3] as u64) << 8*4) |
+       ((v[4] as u64) << 8*3) |
+       ((v[5] as u64) << 8*2) |
+       ((v[6] as u64) << 8*1) |
+       ((v[7] as u64) << 8*0)
+}
+
+
+struct InputData {
+       data: Vec<u8>,
+       read_pos: AtomicUsize,
+}
+impl InputData {
+       fn get_slice(&self, len: usize) -> Option<&[u8]> {
+               let old_pos = self.read_pos.fetch_add(len, Ordering::AcqRel);
+               if self.data.len() < old_pos + len {
+                       return None;
+               }
+               Some(&self.data[old_pos..old_pos + len])
+       }
+       fn get_slice_nonadvancing(&self, len: usize) -> Option<&[u8]> {
+               let old_pos = self.read_pos.load(Ordering::Acquire);
+               if self.data.len() < old_pos + len {
+                       return None;
+               }
+               Some(&self.data[old_pos..old_pos + len])
+       }
+}
+
+struct DummyChainWatcher {
+       input: Arc<InputData>,
+}
+
+impl ChainWatchInterface for DummyChainWatcher {
+       fn install_watch_tx(&self, _txid: &Sha256dHash, _script_pub_key: &Script) { }
+       fn install_watch_outpoint(&self, _outpoint: (Sha256dHash, u32), _out_script: &Script) { }
+       fn watch_all_txn(&self) { }
+       fn register_listener(&self, _listener: Weak<ChainListener>) { }
+
+       fn get_chain_utxo(&self, _genesis_hash: Sha256dHash, _unspent_tx_output_identifier: u64) -> Result<(Script, u64), ChainError> {
+               match self.input.get_slice(2) {
+                       Some(&[0, _]) => Err(ChainError::NotSupported),
+                       Some(&[1, _]) => Err(ChainError::NotWatched),
+                       Some(&[2, _]) => Err(ChainError::UnknownTx),
+                       Some(&[_, x]) => Ok((Builder::new().push_int(x as i64).into_script().to_v0_p2wsh(), 0)),
+                       None => Err(ChainError::UnknownTx),
+                       _ => unreachable!(),
+               }
+       }
+}
+
+#[inline]
+pub fn do_test(data: &[u8]) {
+       let input = Arc::new(InputData {
+               data: data.to_vec(),
+               read_pos: AtomicUsize::new(0),
+       });
+       macro_rules! get_slice_nonadvancing {
+               ($len: expr) => {
+                       match input.get_slice_nonadvancing($len as usize) {
+                               Some(slice) => slice,
+                               None => return,
+                       }
+               }
+       }
+       macro_rules! get_slice {
+               ($len: expr) => {
+                       match input.get_slice($len as usize) {
+                               Some(slice) => slice,
+                               None => return,
+                       }
+               }
+       }
+
+       macro_rules! decode_msg {
+               ($MsgType: path, $len: expr) => {{
+                       let mut reader = ::std::io::Cursor::new(get_slice!($len));
+                       match <($MsgType)>::read(&mut reader) {
+                               Ok(msg) => msg,
+                               Err(e) => match e {
+                                       msgs::DecodeError::UnknownVersion => return,
+                                       msgs::DecodeError::UnknownRequiredFeature => return,
+                                       msgs::DecodeError::InvalidValue => return,
+                                       msgs::DecodeError::ExtraAddressesPerType => return,
+                                       msgs::DecodeError::BadLengthDescriptor => return,
+                                       msgs::DecodeError::ShortRead => panic!("We picked the length..."),
+                                       msgs::DecodeError::Io(e) => panic!(format!("{}", e)),
+                               }
+                       }
+               }}
+       }
+
+       macro_rules! decode_msg_with_len16 {
+               ($MsgType: path, $begin_len: expr, $excess: expr) => {
+                       {
+                               let extra_len = slice_to_be16(&get_slice_nonadvancing!($begin_len as usize + 2)[$begin_len..$begin_len + 2]);
+                               decode_msg!($MsgType, $begin_len as usize + 2 + (extra_len as usize) + $excess)
+                       }
+               }
+       }
+
+       macro_rules! get_pubkey {
+               () => {
+                       match PublicKey::from_slice(get_slice!(33)) {
+                               Ok(key) => key,
+                               Err(_) => return,
+                       }
+               }
+       }
+
+       let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new("".to_owned()));
+       let chain_monitor = Arc::new(DummyChainWatcher {
+               input: Arc::clone(&input),
+       });
+
+       let our_pubkey = get_pubkey!();
+       let router = Router::new(our_pubkey.clone(), chain_monitor, Arc::clone(&logger));
+
+       loop {
+               match get_slice!(1)[0] {
+                       0 => {
+                               let start_len = slice_to_be16(&get_slice_nonadvancing!(64 + 2)[64..64 + 2]) as usize;
+                               let addr_len = slice_to_be16(&get_slice_nonadvancing!(64+start_len+2 + 74)[64+start_len+2 + 72..64+start_len+2 + 74]);
+                               if addr_len > (37+1)*4 {
+                                       return;
+                               }
+                               let _ = router.handle_node_announcement(&decode_msg_with_len16!(msgs::NodeAnnouncement, 64, 288));
+                       },
+                       1 => {
+                               let _ = router.handle_channel_announcement(&decode_msg_with_len16!(msgs::ChannelAnnouncement, 64*4, 32+8+33*4));
+                       },
+                       2 => {
+                               let _ = router.handle_channel_update(&decode_msg!(msgs::ChannelUpdate, 128));
+                       },
+                       3 => {
+                               match get_slice!(1)[0] {
+                                       0 => {
+                                               router.handle_htlc_fail_channel_update(&msgs::HTLCFailChannelUpdate::ChannelUpdateMessage {msg: decode_msg!(msgs::ChannelUpdate, 128)});
+                                       },
+                                       1 => {
+                                               let short_channel_id = slice_to_be64(get_slice!(8));
+                                               router.handle_htlc_fail_channel_update(&msgs::HTLCFailChannelUpdate::ChannelClosed {short_channel_id, is_permanent: false});
+                                       },
+                                       _ => return,
+                               }
+                       },
+                       4 => {
+                               let target = get_pubkey!();
+                               let mut first_hops_vec = Vec::new();
+                               let first_hops = match get_slice!(1)[0] {
+                                       0 => None,
+                                       1 => {
+                                               let count = slice_to_be16(get_slice!(2));
+                                               for _ in 0..count {
+                                                       first_hops_vec.push(ChannelDetails {
+                                                               channel_id: [0; 32],
+                                                               short_channel_id: Some(slice_to_be64(get_slice!(8))),
+                                                               remote_network_id: get_pubkey!(),
+                                                               channel_value_satoshis: slice_to_be64(get_slice!(8)),
+                                                               user_id: 0,
+                                                               inbound_capacity_msat: 0,
+                                                               is_live: true,
+                                                               outbound_capacity_msat: 0,
+                                                       });
+                                               }
+                                               Some(&first_hops_vec[..])
+                                       },
+                                       _ => return,
+                               };
+                               let mut last_hops_vec = Vec::new();
+                               let last_hops = {
+                                       let count = slice_to_be16(get_slice!(2));
+                                       for _ in 0..count {
+                                               last_hops_vec.push(RouteHint {
+                                                       src_node_id: get_pubkey!(),
+                                                       short_channel_id: slice_to_be64(get_slice!(8)),
+                                                       fee_base_msat: slice_to_be32(get_slice!(4)),
+                                                       fee_proportional_millionths: slice_to_be32(get_slice!(4)),
+                                                       cltv_expiry_delta: slice_to_be16(get_slice!(2)),
+                                                       htlc_minimum_msat: slice_to_be64(get_slice!(8)),
+                                               });
+                                       }
+                                       &last_hops_vec[..]
+                               };
+                               let _ = router.get_route(&target, first_hops, last_hops, slice_to_be64(get_slice!(8)), slice_to_be32(get_slice!(4)));
+                       },
+                       _ => return,
+               }
+       }
+}
+
+#[cfg(feature = "afl")]
+#[macro_use] extern crate afl;
+#[cfg(feature = "afl")]
+fn main() {
+       fuzz!(|data| {
+               do_test(data);
+       });
+}
+
+#[cfg(feature = "honggfuzz")]
+#[macro_use] extern crate honggfuzz;
+#[cfg(feature = "honggfuzz")]
+fn main() {
+       loop {
+               fuzz!(|data| {
+                       do_test(data);
+               });
+       }
+}
+
+extern crate hex;
+#[cfg(test)]
+mod tests {
+
+       #[test]
+       fn duplicate_crash() {
+               super::do_test(&::hex::decode("00").unwrap());
+       }
+}
diff --git a/lightning/fuzz/fuzz_targets/utils/mod.rs b/lightning/fuzz/fuzz_targets/utils/mod.rs
new file mode 100644 (file)
index 0000000..a7d7c32
--- /dev/null
@@ -0,0 +1 @@
+pub(crate) mod test_logger;
diff --git a/lightning/fuzz/fuzz_targets/utils/test_logger.rs b/lightning/fuzz/fuzz_targets/utils/test_logger.rs
new file mode 100644 (file)
index 0000000..097d001
--- /dev/null
@@ -0,0 +1,23 @@
+use lightning::util::logger::{Logger, Record};
+pub struct TestLogger {
+       #[cfg(test)]
+       id: String,
+}
+
+impl TestLogger {
+       pub fn new(_id: String) -> TestLogger {
+               TestLogger {
+                       #[cfg(test)]
+                       id: _id
+               }
+       }
+}
+
+impl Logger for TestLogger {
+       fn log(&self, record: &Record) {
+               #[cfg(test)]
+               println!("{:<5} {} [{} : {}, {}] {}", record.level.to_string(), self.id, record.module_path, record.file, record.line, record.args);
+               #[cfg(not(test))]
+               let _ = format!("{}", record.args);
+       }
+}
diff --git a/lightning/fuzz/travis-fuzz.sh b/lightning/fuzz/travis-fuzz.sh
new file mode 100755 (executable)
index 0000000..e602e95
--- /dev/null
@@ -0,0 +1,29 @@
+#!/bin/bash
+set -e
+
+pushd fuzz_targets/msg_targets
+rm *_target.rs
+./gen_target.sh
+[ "$(git diff)" != "" ] && exit 1
+popd
+
+cargo install --force honggfuzz
+for TARGET in fuzz_targets/*.rs fuzz_targets/msg_targets/*_target.rs; do
+       FILENAME=$(basename $TARGET)
+       FILE="${FILENAME%.*}"
+       HFUZZ_RUN_ARGS="--exit_upon_crash -v -n2"
+       if [ "$FILE" = "chanmon_fail_consistency" ]; then
+               HFUZZ_RUN_ARGS="$HFUZZ_RUN_ARGS -F 64 -N100000"
+       else
+               HFUZZ_RUN_ARGS="$HFUZZ_RUN_ARGS -N1000000"
+       fi
+       export HFUZZ_RUN_ARGS
+       HFUZZ_BUILD_ARGS="--features honggfuzz_fuzz" cargo hfuzz run $FILE
+       if [ -f hfuzz_workspace/$FILE/HONGGFUZZ.REPORT.TXT ]; then
+               cat hfuzz_workspace/$FILE/HONGGFUZZ.REPORT.TXT
+               for CASE in hfuzz_workspace/$FILE/SIG*; do
+                       cat $CASE | xxd -p
+               done
+               exit 1
+       fi
+done
diff --git a/lightning/src/chain/chaininterface.rs b/lightning/src/chain/chaininterface.rs
new file mode 100644 (file)
index 0000000..c0330fb
--- /dev/null
@@ -0,0 +1,321 @@
+//! Traits and utility impls which allow other parts of rust-lightning to interact with the
+//! blockchain.
+//!
+//! Includes traits for monitoring and receiving notifications of new blocks and block
+//! disconnections, transaction broadcasting, and feerate information requests.
+
+use bitcoin::blockdata::block::{Block, BlockHeader};
+use bitcoin::blockdata::transaction::Transaction;
+use bitcoin::blockdata::script::Script;
+use bitcoin::blockdata::constants::genesis_block;
+use bitcoin::util::hash::BitcoinHash;
+use bitcoin_hashes::sha256d::Hash as Sha256dHash;
+use bitcoin::network::constants::Network;
+
+use util::logger::Logger;
+
+use std::sync::{Mutex,Weak,MutexGuard,Arc};
+use std::sync::atomic::{AtomicUsize, Ordering};
+use std::collections::HashSet;
+
+/// Used to give chain error details upstream
+pub enum ChainError {
+       /// Client doesn't support UTXO lookup (but the chain hash matches our genesis block hash)
+       NotSupported,
+       /// Chain isn't the one watched
+       NotWatched,
+       /// Tx doesn't exist or is unconfirmed
+       UnknownTx,
+}
+
+/// An interface to request notification of certain scripts as they appear the
+/// chain.
+///
+/// Note that all of the functions implemented here *must* be reentrant-safe (obviously - they're
+/// called from inside the library in response to ChainListener events, P2P events, or timer
+/// events).
+pub trait ChainWatchInterface: Sync + Send {
+       /// Provides a txid/random-scriptPubKey-in-the-tx which much be watched for.
+       fn install_watch_tx(&self, txid: &Sha256dHash, script_pub_key: &Script);
+
+       /// Provides an outpoint which must be watched for, providing any transactions which spend the
+       /// given outpoint.
+       fn install_watch_outpoint(&self, outpoint: (Sha256dHash, u32), out_script: &Script);
+
+       /// Indicates that a listener needs to see all transactions.
+       fn watch_all_txn(&self);
+
+       /// Register the given listener to receive events. Only a weak pointer is provided and the
+       /// registration should be freed once that pointer expires.
+       fn register_listener(&self, listener: Weak<ChainListener>);
+       //TODO: unregister
+
+       /// Gets the script and value in satoshis for a given unspent transaction output given a
+       /// short_channel_id (aka unspent_tx_output_identier). For BTC/tBTC channels the top three
+       /// bytes are the block height, the next 3 the transaction index within the block, and the
+       /// final two the output within the transaction.
+       fn get_chain_utxo(&self, genesis_hash: Sha256dHash, unspent_tx_output_identifier: u64) -> Result<(Script, u64), ChainError>;
+}
+
+/// An interface to send a transaction to the Bitcoin network.
+pub trait BroadcasterInterface: Sync + Send {
+       /// Sends a transaction out to (hopefully) be mined.
+       fn broadcast_transaction(&self, tx: &Transaction);
+}
+
+/// A trait indicating a desire to listen for events from the chain
+pub trait ChainListener: Sync + Send {
+       /// Notifies a listener that a block was connected.
+       /// Note that if a new transaction/outpoint is watched during a block_connected call, the block
+       /// *must* be re-scanned with the new transaction/outpoints and block_connected should be
+       /// called again with the same header and (at least) the new transactions.
+       ///
+       /// Note that if non-new transaction/outpoints may be registered during a call, a second call
+       /// *must not* happen.
+       ///
+       /// This also means those counting confirmations using block_connected callbacks should watch
+       /// for duplicate headers and not count them towards confirmations!
+       fn block_connected(&self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], indexes_of_txn_matched: &[u32]);
+       /// Notifies a listener that a block was disconnected.
+       /// Unlike block_connected, this *must* never be called twice for the same disconnect event.
+       /// Height must be the one of the block which was disconnected (not new height of the best chain)
+       fn block_disconnected(&self, header: &BlockHeader, disconnected_height: u32);
+}
+
+/// An enum that represents the speed at which we want a transaction to confirm used for feerate
+/// estimation.
+pub enum ConfirmationTarget {
+       /// We are happy with this transaction confirming slowly when feerate drops some.
+       Background,
+       /// We'd like this transaction to confirm without major delay, but 12-18 blocks is fine.
+       Normal,
+       /// We'd like this transaction to confirm in the next few blocks.
+       HighPriority,
+}
+
+/// A trait which should be implemented to provide feerate information on a number of time
+/// horizons.
+///
+/// Note that all of the functions implemented here *must* be reentrant-safe (obviously - they're
+/// called from inside the library in response to ChainListener events, P2P events, or timer
+/// events).
+pub trait FeeEstimator: Sync + Send {
+       /// Gets estimated satoshis of fee required per 1000 Weight-Units.
+       ///
+       /// Must be no smaller than 253 (ie 1 satoshi-per-byte rounded up to ensure later round-downs
+       /// don't put us below 1 satoshi-per-byte).
+       ///
+       /// This translates to:
+       ///  * satoshis-per-byte * 250
+       ///  * ceil(satoshis-per-kbyte / 4)
+       fn get_est_sat_per_1000_weight(&self, confirmation_target: ConfirmationTarget) -> u64;
+}
+
+/// Utility for tracking registered txn/outpoints and checking for matches
+pub struct ChainWatchedUtil {
+       watch_all: bool,
+
+       // We are more conservative in matching during testing to ensure everything matches *exactly*,
+       // even though during normal runtime we take more optimized match approaches...
+       #[cfg(test)]
+       watched_txn: HashSet<(Sha256dHash, Script)>,
+       #[cfg(not(test))]
+       watched_txn: HashSet<Script>,
+
+       watched_outpoints: HashSet<(Sha256dHash, u32)>,
+}
+
+impl ChainWatchedUtil {
+       /// Constructs an empty (watches nothing) ChainWatchedUtil
+       pub fn new() -> Self {
+               Self {
+                       watch_all: false,
+                       watched_txn: HashSet::new(),
+                       watched_outpoints: HashSet::new(),
+               }
+       }
+
+       /// Registers a tx for monitoring, returning true if it was a new tx and false if we'd already
+       /// been watching for it.
+       pub fn register_tx(&mut self, txid: &Sha256dHash, script_pub_key: &Script) -> bool {
+               if self.watch_all { return false; }
+               #[cfg(test)]
+               {
+                       self.watched_txn.insert((txid.clone(), script_pub_key.clone()))
+               }
+               #[cfg(not(test))]
+               {
+                       let _tx_unused = txid; // It's used in cfg(test), though
+                       self.watched_txn.insert(script_pub_key.clone())
+               }
+       }
+
+       /// Registers an outpoint for monitoring, returning true if it was a new outpoint and false if
+       /// we'd already been watching for it
+       pub fn register_outpoint(&mut self, outpoint: (Sha256dHash, u32), _script_pub_key: &Script) -> bool {
+               if self.watch_all { return false; }
+               self.watched_outpoints.insert(outpoint)
+       }
+
+       /// Sets us to match all transactions, returning true if this is a new setting and false if
+       /// we'd already been set to match everything.
+       pub fn watch_all(&mut self) -> bool {
+               if self.watch_all { return false; }
+               self.watch_all = true;
+               true
+       }
+
+       /// Checks if a given transaction matches the current filter.
+       pub fn does_match_tx(&self, tx: &Transaction) -> bool {
+               if self.watch_all {
+                       return true;
+               }
+               for out in tx.output.iter() {
+                       #[cfg(test)]
+                       for &(ref txid, ref script) in self.watched_txn.iter() {
+                               if *script == out.script_pubkey {
+                                       if tx.txid() == *txid {
+                                               return true;
+                                       }
+                               }
+                       }
+                       #[cfg(not(test))]
+                       for script in self.watched_txn.iter() {
+                               if *script == out.script_pubkey {
+                                       return true;
+                               }
+                       }
+               }
+               for input in tx.input.iter() {
+                       for outpoint in self.watched_outpoints.iter() {
+                               let &(outpoint_hash, outpoint_index) = outpoint;
+                               if outpoint_hash == input.previous_output.txid && outpoint_index == input.previous_output.vout {
+                                       return true;
+                               }
+                       }
+               }
+               false
+       }
+}
+
+/// Utility to capture some common parts of ChainWatchInterface implementors.
+///
+/// Keeping a local copy of this in a ChainWatchInterface implementor is likely useful.
+pub struct ChainWatchInterfaceUtil {
+       network: Network,
+       watched: Mutex<ChainWatchedUtil>,
+       listeners: Mutex<Vec<Weak<ChainListener>>>,
+       reentered: AtomicUsize,
+       logger: Arc<Logger>,
+}
+
+/// Register listener
+impl ChainWatchInterface for ChainWatchInterfaceUtil {
+       fn install_watch_tx(&self, txid: &Sha256dHash, script_pub_key: &Script) {
+               let mut watched = self.watched.lock().unwrap();
+               if watched.register_tx(txid, script_pub_key) {
+                       self.reentered.fetch_add(1, Ordering::Relaxed);
+               }
+       }
+
+       fn install_watch_outpoint(&self, outpoint: (Sha256dHash, u32), out_script: &Script) {
+               let mut watched = self.watched.lock().unwrap();
+               if watched.register_outpoint(outpoint, out_script) {
+                       self.reentered.fetch_add(1, Ordering::Relaxed);
+               }
+       }
+
+       fn watch_all_txn(&self) {
+               let mut watched = self.watched.lock().unwrap();
+               if watched.watch_all() {
+                       self.reentered.fetch_add(1, Ordering::Relaxed);
+               }
+       }
+
+       fn register_listener(&self, listener: Weak<ChainListener>) {
+               let mut vec = self.listeners.lock().unwrap();
+               vec.push(listener);
+       }
+
+       fn get_chain_utxo(&self, genesis_hash: Sha256dHash, _unspent_tx_output_identifier: u64) -> Result<(Script, u64), ChainError> {
+               if genesis_hash != genesis_block(self.network).header.bitcoin_hash() {
+                       return Err(ChainError::NotWatched);
+               }
+               Err(ChainError::NotSupported)
+       }
+}
+
+impl ChainWatchInterfaceUtil {
+       /// Creates a new ChainWatchInterfaceUtil for the given network
+       pub fn new(network: Network, logger: Arc<Logger>) -> ChainWatchInterfaceUtil {
+               ChainWatchInterfaceUtil {
+                       network: network,
+                       watched: Mutex::new(ChainWatchedUtil::new()),
+                       listeners: Mutex::new(Vec::new()),
+                       reentered: AtomicUsize::new(1),
+                       logger: logger,
+               }
+       }
+
+       /// Notify listeners that a block was connected given a full, unfiltered block.
+       ///
+       /// Handles re-scanning the block and calling block_connected again if listeners register new
+       /// watch data during the callbacks for you (see ChainListener::block_connected for more info).
+       pub fn block_connected_with_filtering(&self, block: &Block, height: u32) {
+               let mut reentered = true;
+               while reentered {
+                       let mut matched = Vec::new();
+                       let mut matched_index = Vec::new();
+                       {
+                               let watched = self.watched.lock().unwrap();
+                               for (index, transaction) in block.txdata.iter().enumerate() {
+                                       if self.does_match_tx_unguarded(transaction, &watched) {
+                                               matched.push(transaction);
+                                               matched_index.push(index as u32);
+                                       }
+                               }
+                       }
+                       reentered = self.block_connected_checked(&block.header, height, matched.as_slice(), matched_index.as_slice());
+               }
+       }
+
+       /// Notify listeners that a block was disconnected.
+       pub fn block_disconnected(&self, header: &BlockHeader, disconnected_height: u32) {
+               let listeners = self.listeners.lock().unwrap().clone();
+               for listener in listeners.iter() {
+                       match listener.upgrade() {
+                               Some(arc) => arc.block_disconnected(&header, disconnected_height),
+                               None => ()
+                       }
+               }
+       }
+
+       /// Notify listeners that a block was connected, given pre-filtered list of transactions in the
+       /// block which matched the filter (probably using does_match_tx).
+       ///
+       /// Returns true if notified listeners registered additional watch data (implying that the
+       /// block must be re-scanned and this function called again prior to further block_connected
+       /// calls, see ChainListener::block_connected for more info).
+       pub fn block_connected_checked(&self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], indexes_of_txn_matched: &[u32]) -> bool {
+               let last_seen = self.reentered.load(Ordering::Relaxed);
+
+               let listeners = self.listeners.lock().unwrap().clone();
+               for listener in listeners.iter() {
+                       match listener.upgrade() {
+                               Some(arc) => arc.block_connected(header, height, txn_matched, indexes_of_txn_matched),
+                               None => ()
+                       }
+               }
+               return last_seen != self.reentered.load(Ordering::Relaxed);
+       }
+
+       /// Checks if a given transaction matches the current filter.
+       pub fn does_match_tx(&self, tx: &Transaction) -> bool {
+               let watched = self.watched.lock().unwrap();
+               self.does_match_tx_unguarded (tx, &watched)
+       }
+
+       fn does_match_tx_unguarded(&self, tx: &Transaction, watched: &MutexGuard<ChainWatchedUtil>) -> bool {
+               watched.does_match_tx(tx)
+       }
+}
diff --git a/lightning/src/chain/keysinterface.rs b/lightning/src/chain/keysinterface.rs
new file mode 100644 (file)
index 0000000..73d320b
--- /dev/null
@@ -0,0 +1,279 @@
+//! keysinterface provides keys into rust-lightning and defines some useful enums which describe
+//! spendable on-chain outputs which the user owns and is responsible for using just as any other
+//! on-chain output which is theirs.
+
+use bitcoin::blockdata::transaction::{OutPoint, TxOut};
+use bitcoin::blockdata::script::{Script, Builder};
+use bitcoin::blockdata::opcodes;
+use bitcoin::network::constants::Network;
+use bitcoin::util::bip32::{ExtendedPrivKey, ExtendedPubKey, ChildNumber};
+
+use bitcoin_hashes::{Hash, HashEngine};
+use bitcoin_hashes::sha256::HashEngine as Sha256State;
+use bitcoin_hashes::sha256::Hash as Sha256;
+use bitcoin_hashes::hash160::Hash as Hash160;
+
+use secp256k1::key::{SecretKey, PublicKey};
+use secp256k1::Secp256k1;
+use secp256k1;
+
+use util::byte_utils;
+use util::logger::Logger;
+
+use std::sync::Arc;
+use std::sync::atomic::{AtomicUsize, Ordering};
+
+/// When on-chain outputs are created by rust-lightning an event is generated which informs the
+/// user thereof. This enum describes the format of the output and provides the OutPoint.
+pub enum SpendableOutputDescriptor {
+       /// Outpoint with an output to a script which was provided via KeysInterface, thus you should
+       /// have stored somewhere how to spend script_pubkey!
+       /// Outputs from a justice tx, claim tx or preimage tx
+       StaticOutput {
+               /// The outpoint spendable by user wallet
+               outpoint: OutPoint,
+               /// The output which is referenced by the given outpoint
+               output: TxOut,
+       },
+       /// Outpoint commits to a P2WSH
+       /// P2WSH should be spend by the following witness :
+       /// <local_delayedsig> 0 <witnessScript>
+       /// With input nSequence set to_self_delay.
+       /// Outputs from a HTLC-Success/Timeout tx/commitment tx
+       DynamicOutputP2WSH {
+               /// Outpoint spendable by user wallet
+               outpoint: OutPoint,
+               /// local_delayedkey = delayed_payment_basepoint_secret + SHA256(per_commitment_point || delayed_payment_basepoint) OR
+               key: SecretKey,
+               /// witness redeemScript encumbering output.
+               witness_script: Script,
+               /// nSequence input must commit to self_delay to satisfy script's OP_CSV
+               to_self_delay: u16,
+               /// The output which is referenced by the given outpoint
+               output: TxOut,
+       },
+       /// Outpoint commits to a P2WPKH
+       /// P2WPKH should be spend by the following witness :
+       /// <local_sig> <local_pubkey>
+       /// Outputs to_remote from a commitment tx
+       DynamicOutputP2WPKH {
+               /// Outpoint spendable by user wallet
+               outpoint: OutPoint,
+               /// localkey = payment_basepoint_secret + SHA256(per_commitment_point || payment_basepoint
+               key: SecretKey,
+               /// The output which is reference by the given outpoint
+               output: TxOut,
+       }
+}
+
+/// A trait to describe an object which can get user secrets and key material.
+pub trait KeysInterface: Send + Sync {
+       /// Get node secret key (aka node_id or network_key)
+       fn get_node_secret(&self) -> SecretKey;
+       /// Get destination redeemScript to encumber static protocol exit points.
+       fn get_destination_script(&self) -> Script;
+       /// Get shutdown_pubkey to use as PublicKey at channel closure
+       fn get_shutdown_pubkey(&self) -> PublicKey;
+       /// Get a new set of ChannelKeys for per-channel secrets. These MUST be unique even if you
+       /// restarted with some stale data!
+       fn get_channel_keys(&self, inbound: bool) -> ChannelKeys;
+       /// Get a secret for construting an onion packet
+       fn get_session_key(&self) -> SecretKey;
+       /// Get a unique temporary channel id. Channels will be referred to by this until the funding
+       /// transaction is created, at which point they will use the outpoint in the funding
+       /// transaction.
+       fn get_channel_id(&self) -> [u8; 32];
+}
+
+/// Set of lightning keys needed to operate a channel as described in BOLT 3
+#[derive(Clone)]
+pub struct ChannelKeys {
+       /// Private key of anchor tx
+       pub funding_key: SecretKey,
+       /// Local secret key for blinded revocation pubkey
+       pub revocation_base_key: SecretKey,
+       /// Local secret key used in commitment tx htlc outputs
+       pub payment_base_key: SecretKey,
+       /// Local secret key used in HTLC tx
+       pub delayed_payment_base_key: SecretKey,
+       /// Local htlc secret key used in commitment tx htlc outputs
+       pub htlc_base_key: SecretKey,
+       /// Commitment seed
+       pub commitment_seed: [u8; 32],
+}
+
+impl_writeable!(ChannelKeys, 0, {
+       funding_key,
+       revocation_base_key,
+       payment_base_key,
+       delayed_payment_base_key,
+       htlc_base_key,
+       commitment_seed
+});
+
+/// Simple KeysInterface implementor that takes a 32-byte seed for use as a BIP 32 extended key
+/// and derives keys from that.
+///
+/// Your node_id is seed/0'
+/// ChannelMonitor closes may use seed/1'
+/// Cooperative closes may use seed/2'
+/// The two close keys may be needed to claim on-chain funds!
+pub struct KeysManager {
+       secp_ctx: Secp256k1<secp256k1::SignOnly>,
+       node_secret: SecretKey,
+       destination_script: Script,
+       shutdown_pubkey: PublicKey,
+       channel_master_key: ExtendedPrivKey,
+       channel_child_index: AtomicUsize,
+       session_master_key: ExtendedPrivKey,
+       session_child_index: AtomicUsize,
+       channel_id_master_key: ExtendedPrivKey,
+       channel_id_child_index: AtomicUsize,
+
+       unique_start: Sha256State,
+       logger: Arc<Logger>,
+}
+
+impl KeysManager {
+       /// Constructs a KeysManager from a 32-byte seed. If the seed is in some way biased (eg your
+       /// RNG is busted) this may panic (but more importantly, you will possibly lose funds).
+       /// starting_time isn't strictly required to actually be a time, but it must absolutely,
+       /// without a doubt, be unique to this instance. ie if you start multiple times with the same
+       /// seed, starting_time must be unique to each run. Thus, the easiest way to achieve this is to
+       /// simply use the current time (with very high precision).
+       ///
+       /// The seed MUST be backed up safely prior to use so that the keys can be re-created, however,
+       /// obviously, starting_time should be unique every time you reload the library - it is only
+       /// used to generate new ephemeral key data (which will be stored by the individual channel if
+       /// necessary).
+       ///
+       /// Note that the seed is required to recover certain on-chain funds independent of
+       /// ChannelMonitor data, though a current copy of ChannelMonitor data is also required for any
+       /// channel, and some on-chain during-closing funds.
+       ///
+       /// Note that until the 0.1 release there is no guarantee of backward compatibility between
+       /// versions. Once the library is more fully supported, the docs will be updated to include a
+       /// detailed description of the guarantee.
+       pub fn new(seed: &[u8; 32], network: Network, logger: Arc<Logger>, starting_time_secs: u64, starting_time_nanos: u32) -> KeysManager {
+               let secp_ctx = Secp256k1::signing_only();
+               match ExtendedPrivKey::new_master(network.clone(), seed) {
+                       Ok(master_key) => {
+                               let node_secret = master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(0).unwrap()).expect("Your RNG is busted").private_key.key;
+                               let destination_script = match master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(1).unwrap()) {
+                                       Ok(destination_key) => {
+                                               let pubkey_hash160 = Hash160::hash(&ExtendedPubKey::from_private(&secp_ctx, &destination_key).public_key.key.serialize()[..]);
+                                               Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0)
+                                                             .push_slice(&pubkey_hash160.into_inner())
+                                                             .into_script()
+                                       },
+                                       Err(_) => panic!("Your RNG is busted"),
+                               };
+                               let shutdown_pubkey = match master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(2).unwrap()) {
+                                       Ok(shutdown_key) => ExtendedPubKey::from_private(&secp_ctx, &shutdown_key).public_key.key,
+                                       Err(_) => panic!("Your RNG is busted"),
+                               };
+                               let channel_master_key = master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(3).unwrap()).expect("Your RNG is busted");
+                               let session_master_key = master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(4).unwrap()).expect("Your RNG is busted");
+                               let channel_id_master_key = master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(5).unwrap()).expect("Your RNG is busted");
+
+                               let mut unique_start = Sha256::engine();
+                               unique_start.input(&byte_utils::be64_to_array(starting_time_secs));
+                               unique_start.input(&byte_utils::be32_to_array(starting_time_nanos));
+                               unique_start.input(seed);
+
+                               KeysManager {
+                                       secp_ctx,
+                                       node_secret,
+                                       destination_script,
+                                       shutdown_pubkey,
+                                       channel_master_key,
+                                       channel_child_index: AtomicUsize::new(0),
+                                       session_master_key,
+                                       session_child_index: AtomicUsize::new(0),
+                                       channel_id_master_key,
+                                       channel_id_child_index: AtomicUsize::new(0),
+
+                                       unique_start,
+                                       logger,
+                               }
+                       },
+                       Err(_) => panic!("Your rng is busted"),
+               }
+       }
+}
+
+impl KeysInterface for KeysManager {
+       fn get_node_secret(&self) -> SecretKey {
+               self.node_secret.clone()
+       }
+
+       fn get_destination_script(&self) -> Script {
+               self.destination_script.clone()
+       }
+
+       fn get_shutdown_pubkey(&self) -> PublicKey {
+               self.shutdown_pubkey.clone()
+       }
+
+       fn get_channel_keys(&self, _inbound: bool) -> ChannelKeys {
+               // We only seriously intend to rely on the channel_master_key for true secure
+               // entropy, everything else just ensures uniqueness. We rely on the unique_start (ie
+               // starting_time provided in the constructor) to be unique.
+               let mut sha = self.unique_start.clone();
+
+               let child_ix = self.channel_child_index.fetch_add(1, Ordering::AcqRel);
+               let child_privkey = self.channel_master_key.ckd_priv(&self.secp_ctx, ChildNumber::from_hardened_idx(child_ix as u32).expect("key space exhausted")).expect("Your RNG is busted");
+               sha.input(&child_privkey.private_key.key[..]);
+
+               let seed = Sha256::from_engine(sha).into_inner();
+
+               let commitment_seed = {
+                       let mut sha = Sha256::engine();
+                       sha.input(&seed);
+                       sha.input(&b"commitment seed"[..]);
+                       Sha256::from_engine(sha).into_inner()
+               };
+               macro_rules! key_step {
+                       ($info: expr, $prev_key: expr) => {{
+                               let mut sha = Sha256::engine();
+                               sha.input(&seed);
+                               sha.input(&$prev_key[..]);
+                               sha.input(&$info[..]);
+                               SecretKey::from_slice(&Sha256::from_engine(sha).into_inner()).expect("SHA-256 is busted")
+                       }}
+               }
+               let funding_key = key_step!(b"funding key", commitment_seed);
+               let revocation_base_key = key_step!(b"revocation base key", funding_key);
+               let payment_base_key = key_step!(b"payment base key", revocation_base_key);
+               let delayed_payment_base_key = key_step!(b"delayed payment base key", payment_base_key);
+               let htlc_base_key = key_step!(b"HTLC base key", delayed_payment_base_key);
+
+               ChannelKeys {
+                       funding_key,
+                       revocation_base_key,
+                       payment_base_key,
+                       delayed_payment_base_key,
+                       htlc_base_key,
+                       commitment_seed,
+               }
+       }
+
+       fn get_session_key(&self) -> SecretKey {
+               let mut sha = self.unique_start.clone();
+
+               let child_ix = self.session_child_index.fetch_add(1, Ordering::AcqRel);
+               let child_privkey = self.session_master_key.ckd_priv(&self.secp_ctx, ChildNumber::from_hardened_idx(child_ix as u32).expect("key space exhausted")).expect("Your RNG is busted");
+               sha.input(&child_privkey.private_key.key[..]);
+               SecretKey::from_slice(&Sha256::from_engine(sha).into_inner()).expect("Your RNG is busted")
+       }
+
+       fn get_channel_id(&self) -> [u8; 32] {
+               let mut sha = self.unique_start.clone();
+
+               let child_ix = self.channel_id_child_index.fetch_add(1, Ordering::AcqRel);
+               let child_privkey = self.channel_id_master_key.ckd_priv(&self.secp_ctx, ChildNumber::from_hardened_idx(child_ix as u32).expect("key space exhausted")).expect("Your RNG is busted");
+               sha.input(&child_privkey.private_key.key[..]);
+
+               (Sha256::from_engine(sha).into_inner())
+       }
+}
diff --git a/lightning/src/chain/mod.rs b/lightning/src/chain/mod.rs
new file mode 100644 (file)
index 0000000..ffa5ed9
--- /dev/null
@@ -0,0 +1,5 @@
+//! Structs and traits which allow other parts of rust-lightning to interact with the blockchain.
+
+pub mod chaininterface;
+pub mod transaction;
+pub mod keysinterface;
diff --git a/lightning/src/chain/transaction.rs b/lightning/src/chain/transaction.rs
new file mode 100644 (file)
index 0000000..ce43984
--- /dev/null
@@ -0,0 +1,63 @@
+//! Contains simple structs describing parts of transactions on the chain.
+
+use bitcoin_hashes::sha256d::Hash as Sha256dHash;
+use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
+
+/// A reference to a transaction output.
+///
+/// Differs from bitcoin::blockdata::transaction::OutPoint as the index is a u16 instead of u32
+/// due to LN's restrictions on index values. Should reduce (possibly) unsafe conversions this way.
+#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
+pub struct OutPoint {
+       /// The referenced transaction's txid.
+       pub txid: Sha256dHash,
+       /// The index of the referenced output in its transaction's vout.
+       pub index: u16,
+}
+
+impl OutPoint {
+       /// Creates a new `OutPoint` from the txid and the index.
+       pub fn new(txid: Sha256dHash, index: u16) -> OutPoint {
+               OutPoint { txid, index }
+       }
+
+       /// Convert an `OutPoint` to a lightning channel id.
+       pub fn to_channel_id(&self) -> [u8; 32] {
+               let mut res = [0; 32];
+               res[..].copy_from_slice(&self.txid[..]);
+               res[30] ^= ((self.index >> 8) & 0xff) as u8;
+               res[31] ^= ((self.index >> 0) & 0xff) as u8;
+               res
+       }
+
+       /// Converts this OutPoint into the OutPoint field as used by rust-bitcoin
+       pub fn into_bitcoin_outpoint(self) -> BitcoinOutPoint {
+               BitcoinOutPoint {
+                       txid: self.txid,
+                       vout: self.index as u32,
+               }
+       }
+}
+
+#[cfg(test)]
+mod tests {
+       use chain::transaction::OutPoint;
+
+       use bitcoin::blockdata::transaction::Transaction;
+       use bitcoin::consensus::encode;
+
+       use hex;
+
+       #[test]
+       fn test_channel_id_calculation() {
+               let tx: Transaction = encode::deserialize(&hex::decode("020000000001010e0adef48412e4361325ac1c6e36411299ab09d4f083b9d8ddb55fbc06e1b0c00000000000feffffff0220a1070000000000220020f81d95e040bd0a493e38bae27bff52fe2bb58b93b293eb579c01c31b05c5af1dc072cfee54a3000016001434b1d6211af5551905dc2642d05f5b04d25a8fe80247304402207f570e3f0de50546aad25a872e3df059d277e776dda4269fa0d2cc8c2ee6ec9a022054e7fae5ca94d47534c86705857c24ceea3ad51c69dd6051c5850304880fc43a012103cb11a1bacc223d98d91f1946c6752e358a5eb1a1c983b3e6fb15378f453b76bd00000000").unwrap()[..]).unwrap();
+               assert_eq!(&OutPoint {
+                       txid: tx.txid(),
+                       index: 0
+               }.to_channel_id(), &hex::decode("3e88dd7165faf7be58b3c5bb2c9c452aebef682807ea57080f62e6f6e113c25e").unwrap()[..]);
+               assert_eq!(&OutPoint {
+                       txid: tx.txid(),
+                       index: 1
+               }.to_channel_id(), &hex::decode("3e88dd7165faf7be58b3c5bb2c9c452aebef682807ea57080f62e6f6e113c25f").unwrap()[..]);
+       }
+}
diff --git a/lightning/src/lib.rs b/lightning/src/lib.rs
new file mode 100644 (file)
index 0000000..68924b5
--- /dev/null
@@ -0,0 +1,30 @@
+#![crate_name = "lightning"]
+
+//! Rust-Lightning, not Rusty's Lightning!
+//!
+//! A full-featured but also flexible lightning implementation, in library form. This allows the
+//! user (you) to decide how they wish to use it instead of being a fully self-contained daemon.
+//! This means there is no built-in threading/execution environment and it's up to the user to
+//! figure out how best to make networking happen/timers fire/things get written to disk/keys get
+//! generated/etc. This makes it a good candidate for tight integration into an existing wallet
+//! instead of having a rather-separate lightning appendage to a wallet.
+
+#![cfg_attr(not(feature = "fuzztarget"), deny(missing_docs))]
+#![forbid(unsafe_code)]
+
+// In general, rust is absolutely horrid at supporting users doing things like,
+// for example, compiling Rust code for real environments. Disable useless lints
+// that don't do anything but annoy us and cant actually ever be resolved.
+#![allow(bare_trait_objects)]
+#![allow(ellipsis_inclusive_range_patterns)]
+
+extern crate bitcoin;
+extern crate bitcoin_hashes;
+extern crate secp256k1;
+#[cfg(test)] extern crate rand;
+#[cfg(test)] extern crate hex;
+
+#[macro_use]
+pub mod util;
+pub mod chain;
+pub mod ln;
diff --git a/lightning/src/ln/chan_utils.rs b/lightning/src/ln/chan_utils.rs
new file mode 100644 (file)
index 0000000..1d8dd30
--- /dev/null
@@ -0,0 +1,256 @@
+use bitcoin::blockdata::script::{Script,Builder};
+use bitcoin::blockdata::opcodes;
+use bitcoin::blockdata::transaction::{TxIn,TxOut,OutPoint,Transaction};
+
+use bitcoin_hashes::{Hash, HashEngine};
+use bitcoin_hashes::sha256::Hash as Sha256;
+use bitcoin_hashes::ripemd160::Hash as Ripemd160;
+use bitcoin_hashes::hash160::Hash as Hash160;
+use bitcoin_hashes::sha256d::Hash as Sha256dHash;
+
+use ln::channelmanager::PaymentHash;
+
+use secp256k1::key::{PublicKey,SecretKey};
+use secp256k1::Secp256k1;
+use secp256k1;
+
+pub const HTLC_SUCCESS_TX_WEIGHT: u64 = 703;
+pub const HTLC_TIMEOUT_TX_WEIGHT: u64 = 663;
+
+// Various functions for key derivation and transaction creation for use within channels. Primarily
+// used in Channel and ChannelMonitor.
+
+pub fn build_commitment_secret(commitment_seed: [u8; 32], idx: u64) -> [u8; 32] {
+       let mut res: [u8; 32] = commitment_seed;
+       for i in 0..48 {
+               let bitpos = 47 - i;
+               if idx & (1 << bitpos) == (1 << bitpos) {
+                       res[bitpos / 8] ^= 1 << (bitpos & 7);
+                       res = Sha256::hash(&res).into_inner();
+               }
+       }
+       res
+}
+
+pub fn derive_private_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, per_commitment_point: &PublicKey, base_secret: &SecretKey) -> Result<SecretKey, secp256k1::Error> {
+       let mut sha = Sha256::engine();
+       sha.input(&per_commitment_point.serialize());
+       sha.input(&PublicKey::from_secret_key(&secp_ctx, &base_secret).serialize());
+       let res = Sha256::from_engine(sha).into_inner();
+
+       let mut key = base_secret.clone();
+       key.add_assign(&res)?;
+       Ok(key)
+}
+
+pub fn derive_public_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, per_commitment_point: &PublicKey, base_point: &PublicKey) -> Result<PublicKey, secp256k1::Error> {
+       let mut sha = Sha256::engine();
+       sha.input(&per_commitment_point.serialize());
+       sha.input(&base_point.serialize());
+       let res = Sha256::from_engine(sha).into_inner();
+
+       let hashkey = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&res)?);
+       base_point.combine(&hashkey)
+}
+
+/// Derives a revocation key from its constituent parts
+pub fn derive_private_revocation_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, per_commitment_secret: &SecretKey, revocation_base_secret: &SecretKey) -> Result<SecretKey, secp256k1::Error> {
+       let revocation_base_point = PublicKey::from_secret_key(&secp_ctx, &revocation_base_secret);
+       let per_commitment_point = PublicKey::from_secret_key(&secp_ctx, &per_commitment_secret);
+
+       let rev_append_commit_hash_key = {
+               let mut sha = Sha256::engine();
+               sha.input(&revocation_base_point.serialize());
+               sha.input(&per_commitment_point.serialize());
+
+               Sha256::from_engine(sha).into_inner()
+       };
+       let commit_append_rev_hash_key = {
+               let mut sha = Sha256::engine();
+               sha.input(&per_commitment_point.serialize());
+               sha.input(&revocation_base_point.serialize());
+
+               Sha256::from_engine(sha).into_inner()
+       };
+
+       let mut part_a = revocation_base_secret.clone();
+       part_a.mul_assign(&rev_append_commit_hash_key)?;
+       let mut part_b = per_commitment_secret.clone();
+       part_b.mul_assign(&commit_append_rev_hash_key)?;
+       part_a.add_assign(&part_b[..])?;
+       Ok(part_a)
+}
+
+pub fn derive_public_revocation_key<T: secp256k1::Verification>(secp_ctx: &Secp256k1<T>, per_commitment_point: &PublicKey, revocation_base_point: &PublicKey) -> Result<PublicKey, secp256k1::Error> {
+       let rev_append_commit_hash_key = {
+               let mut sha = Sha256::engine();
+               sha.input(&revocation_base_point.serialize());
+               sha.input(&per_commitment_point.serialize());
+
+               Sha256::from_engine(sha).into_inner()
+       };
+       let commit_append_rev_hash_key = {
+               let mut sha = Sha256::engine();
+               sha.input(&per_commitment_point.serialize());
+               sha.input(&revocation_base_point.serialize());
+
+               Sha256::from_engine(sha).into_inner()
+       };
+
+       let mut part_a = revocation_base_point.clone();
+       part_a.mul_assign(&secp_ctx, &rev_append_commit_hash_key)?;
+       let mut part_b = per_commitment_point.clone();
+       part_b.mul_assign(&secp_ctx, &commit_append_rev_hash_key)?;
+       part_a.combine(&part_b)
+}
+
+pub struct TxCreationKeys {
+       pub per_commitment_point: PublicKey,
+       pub revocation_key: PublicKey,
+       pub a_htlc_key: PublicKey,
+       pub b_htlc_key: PublicKey,
+       pub a_delayed_payment_key: PublicKey,
+       pub b_payment_key: PublicKey,
+}
+
+impl TxCreationKeys {
+       pub fn new<T: secp256k1::Signing + secp256k1::Verification>(secp_ctx: &Secp256k1<T>, per_commitment_point: &PublicKey, a_delayed_payment_base: &PublicKey, a_htlc_base: &PublicKey, b_revocation_base: &PublicKey, b_payment_base: &PublicKey, b_htlc_base: &PublicKey) -> Result<TxCreationKeys, secp256k1::Error> {
+               Ok(TxCreationKeys {
+                       per_commitment_point: per_commitment_point.clone(),
+                       revocation_key: derive_public_revocation_key(&secp_ctx, &per_commitment_point, &b_revocation_base)?,
+                       a_htlc_key: derive_public_key(&secp_ctx, &per_commitment_point, &a_htlc_base)?,
+                       b_htlc_key: derive_public_key(&secp_ctx, &per_commitment_point, &b_htlc_base)?,
+                       a_delayed_payment_key: derive_public_key(&secp_ctx, &per_commitment_point, &a_delayed_payment_base)?,
+                       b_payment_key: derive_public_key(&secp_ctx, &per_commitment_point, &b_payment_base)?,
+               })
+       }
+}
+
+/// Gets the "to_local" output redeemscript, ie the script which is time-locked or spendable by
+/// the revocation key
+pub fn get_revokeable_redeemscript(revocation_key: &PublicKey, to_self_delay: u16, delayed_payment_key: &PublicKey) -> Script {
+       Builder::new().push_opcode(opcodes::all::OP_IF)
+                     .push_slice(&revocation_key.serialize())
+                     .push_opcode(opcodes::all::OP_ELSE)
+                     .push_int(to_self_delay as i64)
+                     .push_opcode(opcodes::all::OP_CSV)
+                     .push_opcode(opcodes::all::OP_DROP)
+                     .push_slice(&delayed_payment_key.serialize())
+                     .push_opcode(opcodes::all::OP_ENDIF)
+                     .push_opcode(opcodes::all::OP_CHECKSIG)
+                     .into_script()
+}
+
+#[derive(Clone, PartialEq)]
+pub struct HTLCOutputInCommitment {
+       pub offered: bool,
+       pub amount_msat: u64,
+       pub cltv_expiry: u32,
+       pub payment_hash: PaymentHash,
+       pub transaction_output_index: Option<u32>,
+}
+
+#[inline]
+pub fn get_htlc_redeemscript_with_explicit_keys(htlc: &HTLCOutputInCommitment, a_htlc_key: &PublicKey, b_htlc_key: &PublicKey, revocation_key: &PublicKey) -> Script {
+       let payment_hash160 = Ripemd160::hash(&htlc.payment_hash.0[..]).into_inner();
+       if htlc.offered {
+               Builder::new().push_opcode(opcodes::all::OP_DUP)
+                             .push_opcode(opcodes::all::OP_HASH160)
+                             .push_slice(&Hash160::hash(&revocation_key.serialize())[..])
+                             .push_opcode(opcodes::all::OP_EQUAL)
+                             .push_opcode(opcodes::all::OP_IF)
+                             .push_opcode(opcodes::all::OP_CHECKSIG)
+                             .push_opcode(opcodes::all::OP_ELSE)
+                             .push_slice(&b_htlc_key.serialize()[..])
+                             .push_opcode(opcodes::all::OP_SWAP)
+                             .push_opcode(opcodes::all::OP_SIZE)
+                             .push_int(32)
+                             .push_opcode(opcodes::all::OP_EQUAL)
+                             .push_opcode(opcodes::all::OP_NOTIF)
+                             .push_opcode(opcodes::all::OP_DROP)
+                             .push_int(2)
+                             .push_opcode(opcodes::all::OP_SWAP)
+                             .push_slice(&a_htlc_key.serialize()[..])
+                             .push_int(2)
+                             .push_opcode(opcodes::all::OP_CHECKMULTISIG)
+                             .push_opcode(opcodes::all::OP_ELSE)
+                             .push_opcode(opcodes::all::OP_HASH160)
+                             .push_slice(&payment_hash160)
+                             .push_opcode(opcodes::all::OP_EQUALVERIFY)
+                             .push_opcode(opcodes::all::OP_CHECKSIG)
+                             .push_opcode(opcodes::all::OP_ENDIF)
+                             .push_opcode(opcodes::all::OP_ENDIF)
+                             .into_script()
+       } else {
+               Builder::new().push_opcode(opcodes::all::OP_DUP)
+                             .push_opcode(opcodes::all::OP_HASH160)
+                             .push_slice(&Hash160::hash(&revocation_key.serialize())[..])
+                             .push_opcode(opcodes::all::OP_EQUAL)
+                             .push_opcode(opcodes::all::OP_IF)
+                             .push_opcode(opcodes::all::OP_CHECKSIG)
+                             .push_opcode(opcodes::all::OP_ELSE)
+                             .push_slice(&b_htlc_key.serialize()[..])
+                             .push_opcode(opcodes::all::OP_SWAP)
+                             .push_opcode(opcodes::all::OP_SIZE)
+                             .push_int(32)
+                             .push_opcode(opcodes::all::OP_EQUAL)
+                             .push_opcode(opcodes::all::OP_IF)
+                             .push_opcode(opcodes::all::OP_HASH160)
+                             .push_slice(&payment_hash160)
+                             .push_opcode(opcodes::all::OP_EQUALVERIFY)
+                             .push_int(2)
+                             .push_opcode(opcodes::all::OP_SWAP)
+                             .push_slice(&a_htlc_key.serialize()[..])
+                             .push_int(2)
+                             .push_opcode(opcodes::all::OP_CHECKMULTISIG)
+                             .push_opcode(opcodes::all::OP_ELSE)
+                             .push_opcode(opcodes::all::OP_DROP)
+                             .push_int(htlc.cltv_expiry as i64)
+                             .push_opcode(opcodes::all::OP_CLTV)
+                             .push_opcode(opcodes::all::OP_DROP)
+                             .push_opcode(opcodes::all::OP_CHECKSIG)
+                             .push_opcode(opcodes::all::OP_ENDIF)
+                             .push_opcode(opcodes::all::OP_ENDIF)
+                             .into_script()
+       }
+}
+
+/// note here that 'a_revocation_key' is generated using b_revocation_basepoint and a's
+/// commitment secret. 'htlc' does *not* need to have its previous_output_index filled.
+#[inline]
+pub fn get_htlc_redeemscript(htlc: &HTLCOutputInCommitment, keys: &TxCreationKeys) -> Script {
+       get_htlc_redeemscript_with_explicit_keys(htlc, &keys.a_htlc_key, &keys.b_htlc_key, &keys.revocation_key)
+}
+
+/// panics if htlc.transaction_output_index.is_none()!
+pub fn build_htlc_transaction(prev_hash: &Sha256dHash, feerate_per_kw: u64, to_self_delay: u16, htlc: &HTLCOutputInCommitment, a_delayed_payment_key: &PublicKey, revocation_key: &PublicKey) -> Transaction {
+       let mut txins: Vec<TxIn> = Vec::new();
+       txins.push(TxIn {
+               previous_output: OutPoint {
+                       txid: prev_hash.clone(),
+                       vout: htlc.transaction_output_index.expect("Can't build an HTLC transaction for a dust output"),
+               },
+               script_sig: Script::new(),
+               sequence: 0,
+               witness: Vec::new(),
+       });
+
+       let total_fee = if htlc.offered {
+                       feerate_per_kw * HTLC_TIMEOUT_TX_WEIGHT / 1000
+               } else {
+                       feerate_per_kw * HTLC_SUCCESS_TX_WEIGHT / 1000
+               };
+
+       let mut txouts: Vec<TxOut> = Vec::new();
+       txouts.push(TxOut {
+               script_pubkey: get_revokeable_redeemscript(revocation_key, to_self_delay, a_delayed_payment_key).to_v0_p2wsh(),
+               value: htlc.amount_msat / 1000 - total_fee //TODO: BOLT 3 does not specify if we should add amount_msat before dividing or if we should divide by 1000 before subtracting (as we do here)
+       });
+
+       Transaction {
+               version: 2,
+               lock_time: if htlc.offered { htlc.cltv_expiry } else { 0 },
+               input: txins,
+               output: txouts,
+       }
+}
diff --git a/lightning/src/ln/chanmon_update_fail_tests.rs b/lightning/src/ln/chanmon_update_fail_tests.rs
new file mode 100644 (file)
index 0000000..137d983
--- /dev/null
@@ -0,0 +1,1684 @@
+//! Functional tests which test the correct handling of ChannelMonitorUpdateErr returns from
+//! monitor updates.
+//! There are a bunch of these as their handling is relatively error-prone so they are split out
+//! here. See also the chanmon_fail_consistency fuzz test.
+
+use ln::channelmanager::{RAACommitmentOrder, PaymentPreimage, PaymentHash};
+use ln::channelmonitor::ChannelMonitorUpdateErr;
+use ln::msgs;
+use ln::msgs::{ChannelMessageHandler, LocalFeatures, RoutingMessageHandler};
+use util::events::{Event, EventsProvider, MessageSendEvent, MessageSendEventsProvider};
+use util::errors::APIError;
+
+use bitcoin_hashes::sha256::Hash as Sha256;
+use bitcoin_hashes::Hash;
+
+use ln::functional_test_utils::*;
+
+#[test]
+fn test_simple_monitor_permanent_update_fail() {
+       // Test that we handle a simple permanent monitor update failure
+       let mut nodes = create_network(2, &[None, None]);
+       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
+       let (_, payment_hash_1) = get_payment_preimage_hash!(nodes[0]);
+
+       *nodes[0].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::PermanentFailure);
+       if let Err(APIError::ChannelUnavailable {..}) = nodes[0].node.send_payment(route, payment_hash_1) {} else { panic!(); }
+       check_added_monitors!(nodes[0], 1);
+
+       let events_1 = nodes[0].node.get_and_clear_pending_msg_events();
+       assert_eq!(events_1.len(), 2);
+       match events_1[0] {
+               MessageSendEvent::BroadcastChannelUpdate { .. } => {},
+               _ => panic!("Unexpected event"),
+       };
+       match events_1[1] {
+               MessageSendEvent::HandleError { node_id, .. } => assert_eq!(node_id, nodes[1].node.get_our_node_id()),
+               _ => panic!("Unexpected event"),
+       };
+
+       // TODO: Once we hit the chain with the failure transaction we should check that we get a
+       // PaymentFailed event
+
+       assert_eq!(nodes[0].node.list_channels().len(), 0);
+}
+
+fn do_test_simple_monitor_temporary_update_fail(disconnect: bool) {
+       // Test that we can recover from a simple temporary monitor update failure optionally with
+       // a disconnect in between
+       let mut nodes = create_network(2, &[None, None]);
+       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
+       let (payment_preimage_1, payment_hash_1) = get_payment_preimage_hash!(nodes[0]);
+
+       *nodes[0].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
+       if let Err(APIError::MonitorUpdateFailed) = nodes[0].node.send_payment(route.clone(), payment_hash_1) {} else { panic!(); }
+       check_added_monitors!(nodes[0], 1);
+
+       assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
+       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
+       assert_eq!(nodes[0].node.list_channels().len(), 1);
+
+       if disconnect {
+               nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
+               nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
+               reconnect_nodes(&nodes[0], &nodes[1], (true, true), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
+       }
+
+       *nodes[0].chan_monitor.update_ret.lock().unwrap() = Ok(());
+       nodes[0].node.test_restore_channel_monitor();
+       check_added_monitors!(nodes[0], 1);
+
+       let mut events_2 = nodes[0].node.get_and_clear_pending_msg_events();
+       assert_eq!(events_2.len(), 1);
+       let payment_event = SendEvent::from_event(events_2.pop().unwrap());
+       assert_eq!(payment_event.node_id, nodes[1].node.get_our_node_id());
+       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
+       commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
+
+       expect_pending_htlcs_forwardable!(nodes[1]);
+
+       let events_3 = nodes[1].node.get_and_clear_pending_events();
+       assert_eq!(events_3.len(), 1);
+       match events_3[0] {
+               Event::PaymentReceived { ref payment_hash, amt } => {
+                       assert_eq!(payment_hash_1, *payment_hash);
+                       assert_eq!(amt, 1000000);
+               },
+               _ => panic!("Unexpected event"),
+       }
+
+       claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1);
+
+       // Now set it to failed again...
+       let (_, payment_hash_2) = get_payment_preimage_hash!(nodes[0]);
+       *nodes[0].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
+       if let Err(APIError::MonitorUpdateFailed) = nodes[0].node.send_payment(route, payment_hash_2) {} else { panic!(); }
+       check_added_monitors!(nodes[0], 1);
+
+       assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
+       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
+       assert_eq!(nodes[0].node.list_channels().len(), 1);
+
+       if disconnect {
+               nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
+               nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
+               reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
+       }
+
+       // ...and make sure we can force-close a TemporaryFailure channel with a PermanentFailure
+       *nodes[0].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::PermanentFailure);
+       nodes[0].node.test_restore_channel_monitor();
+       check_added_monitors!(nodes[0], 1);
+       check_closed_broadcast!(nodes[0]);
+
+       // TODO: Once we hit the chain with the failure transaction we should check that we get a
+       // PaymentFailed event
+
+       assert_eq!(nodes[0].node.list_channels().len(), 0);
+}
+
+#[test]
+fn test_simple_monitor_temporary_update_fail() {
+       do_test_simple_monitor_temporary_update_fail(false);
+       do_test_simple_monitor_temporary_update_fail(true);
+}
+
+fn do_test_monitor_temporary_update_fail(disconnect_count: usize) {
+       let disconnect_flags = 8 | 16;
+
+       // Test that we can recover from a temporary monitor update failure with some in-flight
+       // HTLCs going on at the same time potentially with some disconnection thrown in.
+       // * First we route a payment, then get a temporary monitor update failure when trying to
+       //   route a second payment. We then claim the first payment.
+       // * If disconnect_count is set, we will disconnect at this point (which is likely as
+       //   TemporaryFailure likely indicates net disconnect which resulted in failing to update
+       //   the ChannelMonitor on a watchtower).
+       // * If !(disconnect_count & 16) we deliver a update_fulfill_htlc/CS for the first payment
+       //   immediately, otherwise we wait disconnect and deliver them via the reconnect
+       //   channel_reestablish processing (ie disconnect_count & 16 makes no sense if
+       //   disconnect_count & !disconnect_flags is 0).
+       // * We then update the channel monitor, reconnecting if disconnect_count is set and walk
+       //   through message sending, potentially disconnect/reconnecting multiple times based on
+       //   disconnect_count, to get the update_fulfill_htlc through.
+       // * We then walk through more message exchanges to get the original update_add_htlc
+       //   through, swapping message ordering based on disconnect_count & 8 and optionally
+       //   disconnect/reconnecting based on disconnect_count.
+       let mut nodes = create_network(2, &[None, None]);
+       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       let (payment_preimage_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
+
+       // Now try to send a second payment which will fail to send
+       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
+       let (payment_preimage_2, payment_hash_2) = get_payment_preimage_hash!(nodes[0]);
+
+       *nodes[0].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
+       if let Err(APIError::MonitorUpdateFailed) = nodes[0].node.send_payment(route.clone(), payment_hash_2) {} else { panic!(); }
+       check_added_monitors!(nodes[0], 1);
+
+       assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
+       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
+       assert_eq!(nodes[0].node.list_channels().len(), 1);
+
+       // Claim the previous payment, which will result in a update_fulfill_htlc/CS from nodes[1]
+       // but nodes[0] won't respond since it is frozen.
+       assert!(nodes[1].node.claim_funds(payment_preimage_1));
+       check_added_monitors!(nodes[1], 1);
+       let events_2 = nodes[1].node.get_and_clear_pending_msg_events();
+       assert_eq!(events_2.len(), 1);
+       let (bs_initial_fulfill, bs_initial_commitment_signed) = match events_2[0] {
+               MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => {
+                       assert_eq!(*node_id, nodes[0].node.get_our_node_id());
+                       assert!(update_add_htlcs.is_empty());
+                       assert_eq!(update_fulfill_htlcs.len(), 1);
+                       assert!(update_fail_htlcs.is_empty());
+                       assert!(update_fail_malformed_htlcs.is_empty());
+                       assert!(update_fee.is_none());
+
+                       if (disconnect_count & 16) == 0 {
+                               nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_htlcs[0]).unwrap();
+                               let events_3 = nodes[0].node.get_and_clear_pending_events();
+                               assert_eq!(events_3.len(), 1);
+                               match events_3[0] {
+                                       Event::PaymentSent { ref payment_preimage } => {
+                                               assert_eq!(*payment_preimage, payment_preimage_1);
+                                       },
+                                       _ => panic!("Unexpected event"),
+                               }
+
+                               if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::IgnoreError) }) = nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), commitment_signed) {
+                                       assert_eq!(err, "Previous monitor update failure prevented generation of RAA");
+                               } else { panic!(); }
+                       }
+
+                       (update_fulfill_htlcs[0].clone(), commitment_signed.clone())
+               },
+               _ => panic!("Unexpected event"),
+       };
+
+       if disconnect_count & !disconnect_flags > 0 {
+               nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
+               nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
+       }
+
+       // Now fix monitor updating...
+       *nodes[0].chan_monitor.update_ret.lock().unwrap() = Ok(());
+       nodes[0].node.test_restore_channel_monitor();
+       check_added_monitors!(nodes[0], 1);
+
+       macro_rules! disconnect_reconnect_peers { () => { {
+               nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
+               nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
+
+               nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id());
+               let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
+               assert_eq!(reestablish_1.len(), 1);
+               nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id());
+               let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
+               assert_eq!(reestablish_2.len(), 1);
+
+               nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]).unwrap();
+               let as_resp = handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
+               nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]).unwrap();
+               let bs_resp = handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
+
+               assert!(as_resp.0.is_none());
+               assert!(bs_resp.0.is_none());
+
+               (reestablish_1, reestablish_2, as_resp, bs_resp)
+       } } }
+
+       let (payment_event, initial_revoke_and_ack) = if disconnect_count & !disconnect_flags > 0 {
+               assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
+               assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
+
+               nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id());
+               let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
+               assert_eq!(reestablish_1.len(), 1);
+               nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id());
+               let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
+               assert_eq!(reestablish_2.len(), 1);
+
+               nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]).unwrap();
+               check_added_monitors!(nodes[0], 0);
+               let mut as_resp = handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
+               nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]).unwrap();
+               check_added_monitors!(nodes[1], 0);
+               let mut bs_resp = handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
+
+               assert!(as_resp.0.is_none());
+               assert!(bs_resp.0.is_none());
+
+               assert!(bs_resp.1.is_none());
+               if (disconnect_count & 16) == 0 {
+                       assert!(bs_resp.2.is_none());
+
+                       assert!(as_resp.1.is_some());
+                       assert!(as_resp.2.is_some());
+                       assert!(as_resp.3 == RAACommitmentOrder::CommitmentFirst);
+               } else {
+                       assert!(bs_resp.2.as_ref().unwrap().update_add_htlcs.is_empty());
+                       assert!(bs_resp.2.as_ref().unwrap().update_fail_htlcs.is_empty());
+                       assert!(bs_resp.2.as_ref().unwrap().update_fail_malformed_htlcs.is_empty());
+                       assert!(bs_resp.2.as_ref().unwrap().update_fee.is_none());
+                       assert!(bs_resp.2.as_ref().unwrap().update_fulfill_htlcs == vec![bs_initial_fulfill]);
+                       assert!(bs_resp.2.as_ref().unwrap().commitment_signed == bs_initial_commitment_signed);
+
+                       assert!(as_resp.1.is_none());
+
+                       nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_resp.2.as_ref().unwrap().update_fulfill_htlcs[0]).unwrap();
+                       let events_3 = nodes[0].node.get_and_clear_pending_events();
+                       assert_eq!(events_3.len(), 1);
+                       match events_3[0] {
+                               Event::PaymentSent { ref payment_preimage } => {
+                                       assert_eq!(*payment_preimage, payment_preimage_1);
+                               },
+                               _ => panic!("Unexpected event"),
+                       }
+
+                       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_resp.2.as_ref().unwrap().commitment_signed).unwrap();
+                       let as_resp_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
+                       // No commitment_signed so get_event_msg's assert(len == 1) passes
+                       check_added_monitors!(nodes[0], 1);
+
+                       as_resp.1 = Some(as_resp_raa);
+                       bs_resp.2 = None;
+               }
+
+               if disconnect_count & !disconnect_flags > 1 {
+                       let (second_reestablish_1, second_reestablish_2, second_as_resp, second_bs_resp) = disconnect_reconnect_peers!();
+
+                       if (disconnect_count & 16) == 0 {
+                               assert!(reestablish_1 == second_reestablish_1);
+                               assert!(reestablish_2 == second_reestablish_2);
+                       }
+                       assert!(as_resp == second_as_resp);
+                       assert!(bs_resp == second_bs_resp);
+               }
+
+               (SendEvent::from_commitment_update(nodes[1].node.get_our_node_id(), as_resp.2.unwrap()), as_resp.1.unwrap())
+       } else {
+               let mut events_4 = nodes[0].node.get_and_clear_pending_msg_events();
+               assert_eq!(events_4.len(), 2);
+               (SendEvent::from_event(events_4.remove(0)), match events_4[0] {
+                       MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
+                               assert_eq!(*node_id, nodes[1].node.get_our_node_id());
+                               msg.clone()
+                       },
+                       _ => panic!("Unexpected event"),
+               })
+       };
+
+       assert_eq!(payment_event.node_id, nodes[1].node.get_our_node_id());
+
+       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
+       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg).unwrap();
+       let bs_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
+       // nodes[1] is awaiting an RAA from nodes[0] still so get_event_msg's assert(len == 1) passes
+       check_added_monitors!(nodes[1], 1);
+
+       if disconnect_count & !disconnect_flags > 2 {
+               let (_, _, as_resp, bs_resp) = disconnect_reconnect_peers!();
+
+               assert!(as_resp.1.unwrap() == initial_revoke_and_ack);
+               assert!(bs_resp.1.unwrap() == bs_revoke_and_ack);
+
+               assert!(as_resp.2.is_none());
+               assert!(bs_resp.2.is_none());
+       }
+
+       let as_commitment_update;
+       let bs_second_commitment_update;
+
+       macro_rules! handle_bs_raa { () => {
+               nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
+               as_commitment_update = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
+               assert!(as_commitment_update.update_add_htlcs.is_empty());
+               assert!(as_commitment_update.update_fulfill_htlcs.is_empty());
+               assert!(as_commitment_update.update_fail_htlcs.is_empty());
+               assert!(as_commitment_update.update_fail_malformed_htlcs.is_empty());
+               assert!(as_commitment_update.update_fee.is_none());
+               check_added_monitors!(nodes[0], 1);
+       } }
+
+       macro_rules! handle_initial_raa { () => {
+               nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &initial_revoke_and_ack).unwrap();
+               bs_second_commitment_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+               assert!(bs_second_commitment_update.update_add_htlcs.is_empty());
+               assert!(bs_second_commitment_update.update_fulfill_htlcs.is_empty());
+               assert!(bs_second_commitment_update.update_fail_htlcs.is_empty());
+               assert!(bs_second_commitment_update.update_fail_malformed_htlcs.is_empty());
+               assert!(bs_second_commitment_update.update_fee.is_none());
+               check_added_monitors!(nodes[1], 1);
+       } }
+
+       if (disconnect_count & 8) == 0 {
+               handle_bs_raa!();
+
+               if disconnect_count & !disconnect_flags > 3 {
+                       let (_, _, as_resp, bs_resp) = disconnect_reconnect_peers!();
+
+                       assert!(as_resp.1.unwrap() == initial_revoke_and_ack);
+                       assert!(bs_resp.1.is_none());
+
+                       assert!(as_resp.2.unwrap() == as_commitment_update);
+                       assert!(bs_resp.2.is_none());
+
+                       assert!(as_resp.3 == RAACommitmentOrder::RevokeAndACKFirst);
+               }
+
+               handle_initial_raa!();
+
+               if disconnect_count & !disconnect_flags > 4 {
+                       let (_, _, as_resp, bs_resp) = disconnect_reconnect_peers!();
+
+                       assert!(as_resp.1.is_none());
+                       assert!(bs_resp.1.is_none());
+
+                       assert!(as_resp.2.unwrap() == as_commitment_update);
+                       assert!(bs_resp.2.unwrap() == bs_second_commitment_update);
+               }
+       } else {
+               handle_initial_raa!();
+
+               if disconnect_count & !disconnect_flags > 3 {
+                       let (_, _, as_resp, bs_resp) = disconnect_reconnect_peers!();
+
+                       assert!(as_resp.1.is_none());
+                       assert!(bs_resp.1.unwrap() == bs_revoke_and_ack);
+
+                       assert!(as_resp.2.is_none());
+                       assert!(bs_resp.2.unwrap() == bs_second_commitment_update);
+
+                       assert!(bs_resp.3 == RAACommitmentOrder::RevokeAndACKFirst);
+               }
+
+               handle_bs_raa!();
+
+               if disconnect_count & !disconnect_flags > 4 {
+                       let (_, _, as_resp, bs_resp) = disconnect_reconnect_peers!();
+
+                       assert!(as_resp.1.is_none());
+                       assert!(bs_resp.1.is_none());
+
+                       assert!(as_resp.2.unwrap() == as_commitment_update);
+                       assert!(bs_resp.2.unwrap() == bs_second_commitment_update);
+               }
+       }
+
+       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_second_commitment_update.commitment_signed).unwrap();
+       let as_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
+       // No commitment_signed so get_event_msg's assert(len == 1) passes
+       check_added_monitors!(nodes[0], 1);
+
+       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_commitment_update.commitment_signed).unwrap();
+       let bs_second_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
+       // No commitment_signed so get_event_msg's assert(len == 1) passes
+       check_added_monitors!(nodes[1], 1);
+
+       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_and_ack).unwrap();
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+       check_added_monitors!(nodes[1], 1);
+
+       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_revoke_and_ack).unwrap();
+       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
+       check_added_monitors!(nodes[0], 1);
+
+       expect_pending_htlcs_forwardable!(nodes[1]);
+
+       let events_5 = nodes[1].node.get_and_clear_pending_events();
+       assert_eq!(events_5.len(), 1);
+       match events_5[0] {
+               Event::PaymentReceived { ref payment_hash, amt } => {
+                       assert_eq!(payment_hash_2, *payment_hash);
+                       assert_eq!(amt, 1000000);
+               },
+               _ => panic!("Unexpected event"),
+       }
+
+       claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
+}
+
+#[test]
+fn test_monitor_temporary_update_fail_a() {
+       do_test_monitor_temporary_update_fail(0);
+       do_test_monitor_temporary_update_fail(1);
+       do_test_monitor_temporary_update_fail(2);
+       do_test_monitor_temporary_update_fail(3);
+       do_test_monitor_temporary_update_fail(4);
+       do_test_monitor_temporary_update_fail(5);
+}
+
+#[test]
+fn test_monitor_temporary_update_fail_b() {
+       do_test_monitor_temporary_update_fail(2 | 8);
+       do_test_monitor_temporary_update_fail(3 | 8);
+       do_test_monitor_temporary_update_fail(4 | 8);
+       do_test_monitor_temporary_update_fail(5 | 8);
+}
+
+#[test]
+fn test_monitor_temporary_update_fail_c() {
+       do_test_monitor_temporary_update_fail(1 | 16);
+       do_test_monitor_temporary_update_fail(2 | 16);
+       do_test_monitor_temporary_update_fail(3 | 16);
+       do_test_monitor_temporary_update_fail(2 | 8 | 16);
+       do_test_monitor_temporary_update_fail(3 | 8 | 16);
+}
+
+#[test]
+fn test_monitor_update_fail_cs() {
+       // Tests handling of a monitor update failure when processing an incoming commitment_signed
+       let mut nodes = create_network(2, &[None, None]);
+       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
+       let (payment_preimage, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
+       nodes[0].node.send_payment(route, our_payment_hash).unwrap();
+       check_added_monitors!(nodes[0], 1);
+
+       let send_event = SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0));
+       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &send_event.msgs[0]).unwrap();
+
+       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
+       if let msgs::HandleError { err, action: Some(msgs::ErrorAction::IgnoreError) } = nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &send_event.commitment_msg).unwrap_err() {
+               assert_eq!(err, "Failed to update ChannelMonitor");
+       } else { panic!(); }
+       check_added_monitors!(nodes[1], 1);
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+
+       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Ok(());
+       nodes[1].node.test_restore_channel_monitor();
+       check_added_monitors!(nodes[1], 1);
+       let responses = nodes[1].node.get_and_clear_pending_msg_events();
+       assert_eq!(responses.len(), 2);
+
+       match responses[0] {
+               MessageSendEvent::SendRevokeAndACK { ref msg, ref node_id } => {
+                       assert_eq!(*node_id, nodes[0].node.get_our_node_id());
+                       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &msg).unwrap();
+                       check_added_monitors!(nodes[0], 1);
+               },
+               _ => panic!("Unexpected event"),
+       }
+       match responses[1] {
+               MessageSendEvent::UpdateHTLCs { ref updates, ref node_id } => {
+                       assert!(updates.update_add_htlcs.is_empty());
+                       assert!(updates.update_fulfill_htlcs.is_empty());
+                       assert!(updates.update_fail_htlcs.is_empty());
+                       assert!(updates.update_fail_malformed_htlcs.is_empty());
+                       assert!(updates.update_fee.is_none());
+                       assert_eq!(*node_id, nodes[0].node.get_our_node_id());
+
+                       *nodes[0].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
+                       if let msgs::HandleError { err, action: Some(msgs::ErrorAction::IgnoreError) } = nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &updates.commitment_signed).unwrap_err() {
+                               assert_eq!(err, "Failed to update ChannelMonitor");
+                       } else { panic!(); }
+                       check_added_monitors!(nodes[0], 1);
+                       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
+               },
+               _ => panic!("Unexpected event"),
+       }
+
+       *nodes[0].chan_monitor.update_ret.lock().unwrap() = Ok(());
+       nodes[0].node.test_restore_channel_monitor();
+       check_added_monitors!(nodes[0], 1);
+
+       let final_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
+       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &final_raa).unwrap();
+       check_added_monitors!(nodes[1], 1);
+
+       expect_pending_htlcs_forwardable!(nodes[1]);
+
+       let events = nodes[1].node.get_and_clear_pending_events();
+       assert_eq!(events.len(), 1);
+       match events[0] {
+               Event::PaymentReceived { payment_hash, amt } => {
+                       assert_eq!(payment_hash, our_payment_hash);
+                       assert_eq!(amt, 1000000);
+               },
+               _ => panic!("Unexpected event"),
+       };
+
+       claim_payment(&nodes[0], &[&nodes[1]], payment_preimage);
+}
+
+#[test]
+fn test_monitor_update_fail_no_rebroadcast() {
+       // Tests handling of a monitor update failure when no message rebroadcasting on
+       // test_restore_channel_monitor() is required. Backported from
+       // chanmon_fail_consistency fuzz tests.
+       let mut nodes = create_network(2, &[None, None]);
+       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
+       let (payment_preimage_1, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
+       nodes[0].node.send_payment(route, our_payment_hash).unwrap();
+       check_added_monitors!(nodes[0], 1);
+
+       let send_event = SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0));
+       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &send_event.msgs[0]).unwrap();
+       let bs_raa = commitment_signed_dance!(nodes[1], nodes[0], send_event.commitment_msg, false, true, false, true);
+
+       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
+       if let msgs::HandleError { err, action: Some(msgs::ErrorAction::IgnoreError) } = nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &bs_raa).unwrap_err() {
+               assert_eq!(err, "Failed to update ChannelMonitor");
+       } else { panic!(); }
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+       assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
+       check_added_monitors!(nodes[1], 1);
+
+       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Ok(());
+       nodes[1].node.test_restore_channel_monitor();
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+       check_added_monitors!(nodes[1], 1);
+       expect_pending_htlcs_forwardable!(nodes[1]);
+
+       let events = nodes[1].node.get_and_clear_pending_events();
+       assert_eq!(events.len(), 1);
+       match events[0] {
+               Event::PaymentReceived { payment_hash, .. } => {
+                       assert_eq!(payment_hash, our_payment_hash);
+               },
+               _ => panic!("Unexpected event"),
+       }
+
+       claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1);
+}
+
+#[test]
+fn test_monitor_update_raa_while_paused() {
+       // Tests handling of an RAA while monitor updating has already been marked failed.
+       // Backported from chanmon_fail_consistency fuzz tests as this used to be broken.
+       let mut nodes = create_network(2, &[None, None]);
+       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       send_payment(&nodes[0], &[&nodes[1]], 5000000);
+
+       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
+       let (payment_preimage_1, our_payment_hash_1) = get_payment_preimage_hash!(nodes[0]);
+       nodes[0].node.send_payment(route, our_payment_hash_1).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       let send_event_1 = SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0));
+
+       let route = nodes[1].router.get_route(&nodes[0].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
+       let (payment_preimage_2, our_payment_hash_2) = get_payment_preimage_hash!(nodes[0]);
+       nodes[1].node.send_payment(route, our_payment_hash_2).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       let send_event_2 = SendEvent::from_event(nodes[1].node.get_and_clear_pending_msg_events().remove(0));
+
+       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &send_event_1.msgs[0]).unwrap();
+       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &send_event_1.commitment_msg).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
+
+       *nodes[0].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
+       nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &send_event_2.msgs[0]).unwrap();
+       if let msgs::HandleError { err, action: Some(msgs::ErrorAction::IgnoreError) } = nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &send_event_2.commitment_msg).unwrap_err() {
+               assert_eq!(err, "Failed to update ChannelMonitor");
+       } else { panic!(); }
+       check_added_monitors!(nodes[0], 1);
+
+       if let msgs::HandleError { err, action: Some(msgs::ErrorAction::IgnoreError) } = nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa).unwrap_err() {
+               assert_eq!(err, "Previous monitor update failure prevented responses to RAA");
+       } else { panic!(); }
+       check_added_monitors!(nodes[0], 1);
+
+       *nodes[0].chan_monitor.update_ret.lock().unwrap() = Ok(());
+       nodes[0].node.test_restore_channel_monitor();
+       check_added_monitors!(nodes[0], 1);
+
+       let as_update_raa = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
+       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_update_raa.0).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       let bs_cs = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+
+       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_update_raa.1).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       let bs_second_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
+
+       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_cs.commitment_signed).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       let as_second_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
+
+       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_raa).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       expect_pending_htlcs_forwardable!(nodes[0]);
+       expect_payment_received!(nodes[0], our_payment_hash_2, 1000000);
+
+       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_second_raa).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       expect_pending_htlcs_forwardable!(nodes[1]);
+       expect_payment_received!(nodes[1], our_payment_hash_1, 1000000);
+
+       claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1);
+       claim_payment(&nodes[1], &[&nodes[0]], payment_preimage_2);
+}
+
+fn do_test_monitor_update_fail_raa(test_ignore_second_cs: bool) {
+       // Tests handling of a monitor update failure when processing an incoming RAA
+       let mut nodes = create_network(3, &[None, None, None]);
+       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+       let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
+
+       // Rebalance a bit so that we can send backwards from 2 to 1.
+       send_payment(&nodes[0], &[&nodes[1], &nodes[2]], 5000000);
+
+       // Route a first payment that we'll fail backwards
+       let (_, payment_hash_1) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1000000);
+
+       // Fail the payment backwards, failing the monitor update on nodes[1]'s receipt of the RAA
+       assert!(nodes[2].node.fail_htlc_backwards(&payment_hash_1));
+       expect_pending_htlcs_forwardable!(nodes[2]);
+       check_added_monitors!(nodes[2], 1);
+
+       let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
+       assert!(updates.update_add_htlcs.is_empty());
+       assert!(updates.update_fulfill_htlcs.is_empty());
+       assert_eq!(updates.update_fail_htlcs.len(), 1);
+       assert!(updates.update_fail_malformed_htlcs.is_empty());
+       assert!(updates.update_fee.is_none());
+       nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]).unwrap();
+
+       let bs_revoke_and_ack = commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false, true, false, true);
+       check_added_monitors!(nodes[0], 0);
+
+       // While the second channel is AwaitingRAA, forward a second payment to get it into the
+       // holding cell.
+       let (payment_preimage_2, payment_hash_2) = get_payment_preimage_hash!(nodes[0]);
+       let route = nodes[0].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
+       nodes[0].node.send_payment(route, payment_hash_2).unwrap();
+       check_added_monitors!(nodes[0], 1);
+
+       let mut send_event = SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0));
+       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &send_event.msgs[0]).unwrap();
+       commitment_signed_dance!(nodes[1], nodes[0], send_event.commitment_msg, false);
+
+       expect_pending_htlcs_forwardable!(nodes[1]);
+       check_added_monitors!(nodes[1], 0);
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+
+       // Now fail monitor updating.
+       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
+       if let msgs::HandleError { err, action: Some(msgs::ErrorAction::IgnoreError) } = nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_revoke_and_ack).unwrap_err() {
+               assert_eq!(err, "Failed to update ChannelMonitor");
+       } else { panic!(); }
+       assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+       check_added_monitors!(nodes[1], 1);
+
+       // Attempt to forward a third payment but fail due to the second channel being unavailable
+       // for forwarding.
+
+       let (_, payment_hash_3) = get_payment_preimage_hash!(nodes[0]);
+       let route = nodes[0].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
+       nodes[0].node.send_payment(route, payment_hash_3).unwrap();
+       check_added_monitors!(nodes[0], 1);
+
+       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Ok(()); // We succeed in updating the monitor for the first channel
+       send_event = SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0));
+       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &send_event.msgs[0]).unwrap();
+       commitment_signed_dance!(nodes[1], nodes[0], send_event.commitment_msg, false, true);
+       check_added_monitors!(nodes[1], 0);
+
+       let mut events_2 = nodes[1].node.get_and_clear_pending_msg_events();
+       assert_eq!(events_2.len(), 1);
+       match events_2.remove(0) {
+               MessageSendEvent::UpdateHTLCs { node_id, updates } => {
+                       assert_eq!(node_id, nodes[0].node.get_our_node_id());
+                       assert!(updates.update_fulfill_htlcs.is_empty());
+                       assert_eq!(updates.update_fail_htlcs.len(), 1);
+                       assert!(updates.update_fail_malformed_htlcs.is_empty());
+                       assert!(updates.update_add_htlcs.is_empty());
+                       assert!(updates.update_fee.is_none());
+
+                       nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]).unwrap();
+                       commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, false, true);
+
+                       let msg_events = nodes[0].node.get_and_clear_pending_msg_events();
+                       assert_eq!(msg_events.len(), 1);
+                       match msg_events[0] {
+                               MessageSendEvent::PaymentFailureNetworkUpdate { update: msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg }} => {
+                                       assert_eq!(msg.contents.short_channel_id, chan_2.0.contents.short_channel_id);
+                                       assert_eq!(msg.contents.flags & 2, 2); // temp disabled
+                               },
+                               _ => panic!("Unexpected event"),
+                       }
+
+                       let events = nodes[0].node.get_and_clear_pending_events();
+                       assert_eq!(events.len(), 1);
+                       if let Event::PaymentFailed { payment_hash, rejected_by_dest, .. } = events[0] {
+                               assert_eq!(payment_hash, payment_hash_3);
+                               assert!(!rejected_by_dest);
+                       } else { panic!("Unexpected event!"); }
+               },
+               _ => panic!("Unexpected event type!"),
+       };
+
+       let (payment_preimage_4, payment_hash_4) = if test_ignore_second_cs {
+               // Try to route another payment backwards from 2 to make sure 1 holds off on responding
+               let (payment_preimage_4, payment_hash_4) = get_payment_preimage_hash!(nodes[0]);
+               let route = nodes[2].router.get_route(&nodes[0].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
+               nodes[2].node.send_payment(route, payment_hash_4).unwrap();
+               check_added_monitors!(nodes[2], 1);
+
+               send_event = SendEvent::from_event(nodes[2].node.get_and_clear_pending_msg_events().remove(0));
+               nodes[1].node.handle_update_add_htlc(&nodes[2].node.get_our_node_id(), &send_event.msgs[0]).unwrap();
+               if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::IgnoreError) }) = nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &send_event.commitment_msg) {
+                       assert_eq!(err, "Previous monitor update failure prevented generation of RAA");
+               } else { panic!(); }
+               assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+               assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
+               (Some(payment_preimage_4), Some(payment_hash_4))
+       } else { (None, None) };
+
+       // Restore monitor updating, ensuring we immediately get a fail-back update and a
+       // update_add update.
+       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Ok(());
+       nodes[1].node.test_restore_channel_monitor();
+       check_added_monitors!(nodes[1], 1);
+       expect_pending_htlcs_forwardable!(nodes[1]);
+       check_added_monitors!(nodes[1], 1);
+
+       let mut events_3 = nodes[1].node.get_and_clear_pending_msg_events();
+       if test_ignore_second_cs {
+               assert_eq!(events_3.len(), 3);
+       } else {
+               assert_eq!(events_3.len(), 2);
+       }
+
+       // Note that the ordering of the events for different nodes is non-prescriptive, though the
+       // ordering of the two events that both go to nodes[2] have to stay in the same order.
+       let messages_a = match events_3.pop().unwrap() {
+               MessageSendEvent::UpdateHTLCs { node_id, mut updates } => {
+                       assert_eq!(node_id, nodes[0].node.get_our_node_id());
+                       assert!(updates.update_fulfill_htlcs.is_empty());
+                       assert_eq!(updates.update_fail_htlcs.len(), 1);
+                       assert!(updates.update_fail_malformed_htlcs.is_empty());
+                       assert!(updates.update_add_htlcs.is_empty());
+                       assert!(updates.update_fee.is_none());
+                       (updates.update_fail_htlcs.remove(0), updates.commitment_signed)
+               },
+               _ => panic!("Unexpected event type!"),
+       };
+       let raa = if test_ignore_second_cs {
+               match events_3.remove(1) {
+                       MessageSendEvent::SendRevokeAndACK { node_id, msg } => {
+                               assert_eq!(node_id, nodes[2].node.get_our_node_id());
+                               Some(msg.clone())
+                       },
+                       _ => panic!("Unexpected event"),
+               }
+       } else { None };
+       let send_event_b = SendEvent::from_event(events_3.remove(0));
+       assert_eq!(send_event_b.node_id, nodes[2].node.get_our_node_id());
+
+       // Now deliver the new messages...
+
+       nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &messages_a.0).unwrap();
+       commitment_signed_dance!(nodes[0], nodes[1], messages_a.1, false);
+       let events_4 = nodes[0].node.get_and_clear_pending_events();
+       assert_eq!(events_4.len(), 1);
+       if let Event::PaymentFailed { payment_hash, rejected_by_dest, .. } = events_4[0] {
+               assert_eq!(payment_hash, payment_hash_1);
+               assert!(rejected_by_dest);
+       } else { panic!("Unexpected event!"); }
+
+       nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &send_event_b.msgs[0]).unwrap();
+       if test_ignore_second_cs {
+               nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &send_event_b.commitment_msg).unwrap();
+               check_added_monitors!(nodes[2], 1);
+               let bs_revoke_and_ack = get_event_msg!(nodes[2], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
+               nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &raa.unwrap()).unwrap();
+               check_added_monitors!(nodes[2], 1);
+               let bs_cs = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
+               assert!(bs_cs.update_add_htlcs.is_empty());
+               assert!(bs_cs.update_fail_htlcs.is_empty());
+               assert!(bs_cs.update_fail_malformed_htlcs.is_empty());
+               assert!(bs_cs.update_fulfill_htlcs.is_empty());
+               assert!(bs_cs.update_fee.is_none());
+
+               nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
+               check_added_monitors!(nodes[1], 1);
+               let as_cs = get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
+               assert!(as_cs.update_add_htlcs.is_empty());
+               assert!(as_cs.update_fail_htlcs.is_empty());
+               assert!(as_cs.update_fail_malformed_htlcs.is_empty());
+               assert!(as_cs.update_fulfill_htlcs.is_empty());
+               assert!(as_cs.update_fee.is_none());
+
+               nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &bs_cs.commitment_signed).unwrap();
+               check_added_monitors!(nodes[1], 1);
+               let as_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
+
+               nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &as_cs.commitment_signed).unwrap();
+               check_added_monitors!(nodes[2], 1);
+               let bs_second_raa = get_event_msg!(nodes[2], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
+
+               nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_raa).unwrap();
+               check_added_monitors!(nodes[2], 1);
+               assert!(nodes[2].node.get_and_clear_pending_msg_events().is_empty());
+
+               nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_second_raa).unwrap();
+               check_added_monitors!(nodes[1], 1);
+               assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+       } else {
+               commitment_signed_dance!(nodes[2], nodes[1], send_event_b.commitment_msg, false);
+       }
+
+       expect_pending_htlcs_forwardable!(nodes[2]);
+
+       let events_6 = nodes[2].node.get_and_clear_pending_events();
+       assert_eq!(events_6.len(), 1);
+       match events_6[0] {
+               Event::PaymentReceived { payment_hash, .. } => { assert_eq!(payment_hash, payment_hash_2); },
+               _ => panic!("Unexpected event"),
+       };
+
+       if test_ignore_second_cs {
+               expect_pending_htlcs_forwardable!(nodes[1]);
+               check_added_monitors!(nodes[1], 1);
+
+               send_event = SendEvent::from_node(&nodes[1]);
+               assert_eq!(send_event.node_id, nodes[0].node.get_our_node_id());
+               assert_eq!(send_event.msgs.len(), 1);
+               nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &send_event.msgs[0]).unwrap();
+               commitment_signed_dance!(nodes[0], nodes[1], send_event.commitment_msg, false);
+
+               expect_pending_htlcs_forwardable!(nodes[0]);
+
+               let events_9 = nodes[0].node.get_and_clear_pending_events();
+               assert_eq!(events_9.len(), 1);
+               match events_9[0] {
+                       Event::PaymentReceived { payment_hash, .. } => assert_eq!(payment_hash, payment_hash_4.unwrap()),
+                       _ => panic!("Unexpected event"),
+               };
+               claim_payment(&nodes[2], &[&nodes[1], &nodes[0]], payment_preimage_4.unwrap());
+       }
+
+       claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage_2);
+}
+
+#[test]
+fn test_monitor_update_fail_raa() {
+       do_test_monitor_update_fail_raa(false);
+       do_test_monitor_update_fail_raa(true);
+}
+
+#[test]
+fn test_monitor_update_fail_reestablish() {
+       // Simple test for message retransmission after monitor update failure on
+       // channel_reestablish generating a monitor update (which comes from freeing holding cell
+       // HTLCs).
+       let mut nodes = create_network(3, &[None, None, None]);
+       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+       create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
+
+       let (our_payment_preimage, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1000000);
+
+       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
+       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
+
+       assert!(nodes[2].node.claim_funds(our_payment_preimage));
+       check_added_monitors!(nodes[2], 1);
+       let mut updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
+       assert!(updates.update_add_htlcs.is_empty());
+       assert!(updates.update_fail_htlcs.is_empty());
+       assert!(updates.update_fail_malformed_htlcs.is_empty());
+       assert!(updates.update_fee.is_none());
+       assert_eq!(updates.update_fulfill_htlcs.len(), 1);
+       nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+       commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false);
+
+       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
+       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id());
+       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id());
+
+       let as_reestablish = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id());
+       let bs_reestablish = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id());
+
+       nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bs_reestablish).unwrap();
+
+       if let msgs::HandleError { err, action: Some(msgs::ErrorAction::IgnoreError) } = nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &as_reestablish).unwrap_err() {
+               assert_eq!(err, "Failed to update ChannelMonitor");
+       } else { panic!(); }
+       check_added_monitors!(nodes[1], 1);
+
+       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
+       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
+
+       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id());
+       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id());
+
+       assert!(as_reestablish == get_event_msg!(nodes[0], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id()));
+       assert!(bs_reestablish == get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id()));
+
+       nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bs_reestablish).unwrap();
+
+       nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &as_reestablish).unwrap();
+       check_added_monitors!(nodes[1], 0);
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+
+       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Ok(());
+       nodes[1].node.test_restore_channel_monitor();
+       check_added_monitors!(nodes[1], 1);
+
+       updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+       assert!(updates.update_add_htlcs.is_empty());
+       assert!(updates.update_fail_htlcs.is_empty());
+       assert!(updates.update_fail_malformed_htlcs.is_empty());
+       assert!(updates.update_fee.is_none());
+       assert_eq!(updates.update_fulfill_htlcs.len(), 1);
+       nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]).unwrap();
+       commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, false);
+
+       let events = nodes[0].node.get_and_clear_pending_events();
+       assert_eq!(events.len(), 1);
+       match events[0] {
+               Event::PaymentSent { payment_preimage, .. } => assert_eq!(payment_preimage, our_payment_preimage),
+               _ => panic!("Unexpected event"),
+       }
+}
+
+#[test]
+fn raa_no_response_awaiting_raa_state() {
+       // This is a rather convoluted test which ensures that if handling of an RAA does not happen
+       // due to a previous monitor update failure, we still set AwaitingRemoteRevoke on the channel
+       // in question (assuming it intends to respond with a CS after monitor updating is restored).
+       // Backported from chanmon_fail_consistency fuzz tests as this used to be broken.
+       let mut nodes = create_network(2, &[None, None]);
+       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
+       let (payment_preimage_1, payment_hash_1) = get_payment_preimage_hash!(nodes[0]);
+       let (payment_preimage_2, payment_hash_2) = get_payment_preimage_hash!(nodes[0]);
+       let (payment_preimage_3, payment_hash_3) = get_payment_preimage_hash!(nodes[0]);
+
+       // Queue up two payments - one will be delivered right away, one immediately goes into the
+       // holding cell as nodes[0] is AwaitingRAA. Ultimately this allows us to deliver an RAA
+       // immediately after a CS. By setting failing the monitor update failure from the CS (which
+       // requires only an RAA response due to AwaitingRAA) we can deliver the RAA and require the CS
+       // generation during RAA while in monitor-update-failed state.
+       nodes[0].node.send_payment(route.clone(), payment_hash_1).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       nodes[0].node.send_payment(route.clone(), payment_hash_2).unwrap();
+       check_added_monitors!(nodes[0], 0);
+
+       let mut events = nodes[0].node.get_and_clear_pending_msg_events();
+       assert_eq!(events.len(), 1);
+       let payment_event = SendEvent::from_event(events.pop().unwrap());
+       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
+       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg).unwrap();
+       check_added_monitors!(nodes[1], 1);
+
+       let bs_responses = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_responses.0).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       let mut events = nodes[0].node.get_and_clear_pending_msg_events();
+       assert_eq!(events.len(), 1);
+       let payment_event = SendEvent::from_event(events.pop().unwrap());
+
+       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_responses.1).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
+
+       // Now we have a CS queued up which adds a new HTLC (which will need a RAA/CS response from
+       // nodes[1]) followed by an RAA. Fail the monitor updating prior to the CS, deliver the RAA,
+       // then restore channel monitor updates.
+       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
+       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
+       if let msgs::HandleError { err, action: Some(msgs::ErrorAction::IgnoreError) } = nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg).unwrap_err() {
+               assert_eq!(err, "Failed to update ChannelMonitor");
+       } else { panic!(); }
+       check_added_monitors!(nodes[1], 1);
+
+       if let msgs::HandleError { err, action: Some(msgs::ErrorAction::IgnoreError) } = nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa).unwrap_err() {
+               assert_eq!(err, "Previous monitor update failure prevented responses to RAA");
+       } else { panic!(); }
+       check_added_monitors!(nodes[1], 1);
+
+       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Ok(());
+       nodes[1].node.test_restore_channel_monitor();
+       // nodes[1] should be AwaitingRAA here!
+       check_added_monitors!(nodes[1], 1);
+       let bs_responses = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+       expect_pending_htlcs_forwardable!(nodes[1]);
+       expect_payment_received!(nodes[1], payment_hash_1, 1000000);
+
+       // We send a third payment here, which is somewhat of a redundant test, but the
+       // chanmon_fail_consistency test required it to actually find the bug (by seeing out-of-sync
+       // commitment transaction states) whereas here we can explicitly check for it.
+       nodes[0].node.send_payment(route.clone(), payment_hash_3).unwrap();
+       check_added_monitors!(nodes[0], 0);
+       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
+
+       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_responses.0).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       let mut events = nodes[0].node.get_and_clear_pending_msg_events();
+       assert_eq!(events.len(), 1);
+       let payment_event = SendEvent::from_event(events.pop().unwrap());
+
+       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_responses.1).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
+
+       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
+       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
+
+       // Finally deliver the RAA to nodes[1] which results in a CS response to the last update
+       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       expect_pending_htlcs_forwardable!(nodes[1]);
+       expect_payment_received!(nodes[1], payment_hash_2, 1000000);
+       let bs_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+
+       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa).unwrap();
+       check_added_monitors!(nodes[0], 1);
+
+       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_update.commitment_signed).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
+
+       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       expect_pending_htlcs_forwardable!(nodes[1]);
+       expect_payment_received!(nodes[1], payment_hash_3, 1000000);
+
+       claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1);
+       claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
+       claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_3);
+}
+
+#[test]
+fn claim_while_disconnected_monitor_update_fail() {
+       // Test for claiming a payment while disconnected and then having the resulting
+       // channel-update-generated monitor update fail. This kind of thing isn't a particularly
+       // contrived case for nodes with network instability.
+       // Backported from chanmon_fail_consistency fuzz tests as an unmerged version of the handling
+       // code introduced a regression in this test (specifically, this caught a removal of the
+       // channel_reestablish handling ensuring the order was sensical given the messages used).
+       let mut nodes = create_network(2, &[None, None]);
+       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       // Forward a payment for B to claim
+       let (payment_preimage_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
+
+       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
+       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
+
+       assert!(nodes[1].node.claim_funds(payment_preimage_1));
+       check_added_monitors!(nodes[1], 1);
+
+       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id());
+       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id());
+
+       let as_reconnect = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id());
+       let bs_reconnect = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id());
+
+       nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bs_reconnect).unwrap();
+       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
+
+       // Now deliver a's reestablish, freeing the claim from the holding cell, but fail the monitor
+       // update.
+       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
+
+       if let msgs::HandleError { err, action: Some(msgs::ErrorAction::IgnoreError) } = nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &as_reconnect).unwrap_err() {
+               assert_eq!(err, "Failed to update ChannelMonitor");
+       } else { panic!(); }
+       check_added_monitors!(nodes[1], 1);
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+
+       // Send a second payment from A to B, resulting in a commitment update that gets swallowed with
+       // the monitor still failed
+       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
+       let (payment_preimage_2, payment_hash_2) = get_payment_preimage_hash!(nodes[0]);
+       nodes[0].node.send_payment(route, payment_hash_2).unwrap();
+       check_added_monitors!(nodes[0], 1);
+
+       let as_updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
+       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &as_updates.update_add_htlcs[0]).unwrap();
+       if let msgs::HandleError { err, action: Some(msgs::ErrorAction::IgnoreError) } = nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_updates.commitment_signed).unwrap_err() {
+               assert_eq!(err, "Previous monitor update failure prevented generation of RAA");
+       } else { panic!(); }
+       // Note that nodes[1] not updating monitor here is OK - it wont take action on the new HTLC
+       // until we've test_restore_channel_monitor'd and updated for the new commitment transaction.
+
+       // Now un-fail the monitor, which will result in B sending its original commitment update,
+       // receiving the commitment update from A, and the resulting commitment dances.
+       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Ok(());
+       nodes[1].node.test_restore_channel_monitor();
+       check_added_monitors!(nodes[1], 1);
+
+       let bs_msgs = nodes[1].node.get_and_clear_pending_msg_events();
+       assert_eq!(bs_msgs.len(), 2);
+
+       match bs_msgs[0] {
+               MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
+                       assert_eq!(*node_id, nodes[0].node.get_our_node_id());
+                       nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]).unwrap();
+                       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &updates.commitment_signed).unwrap();
+                       check_added_monitors!(nodes[0], 1);
+
+                       let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
+                       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa).unwrap();
+                       check_added_monitors!(nodes[1], 1);
+               },
+               _ => panic!("Unexpected event"),
+       }
+
+       match bs_msgs[1] {
+               MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
+                       assert_eq!(*node_id, nodes[0].node.get_our_node_id());
+                       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), msg).unwrap();
+                       check_added_monitors!(nodes[0], 1);
+               },
+               _ => panic!("Unexpected event"),
+       }
+
+       let as_commitment = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
+
+       let bs_commitment = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_commitment.commitment_signed).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
+
+       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_commitment.commitment_signed).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
+       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa).unwrap();
+       check_added_monitors!(nodes[1], 1);
+
+       expect_pending_htlcs_forwardable!(nodes[1]);
+       expect_payment_received!(nodes[1], payment_hash_2, 1000000);
+
+       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa).unwrap();
+       check_added_monitors!(nodes[0], 1);
+
+       let events = nodes[0].node.get_and_clear_pending_events();
+       assert_eq!(events.len(), 1);
+       match events[0] {
+               Event::PaymentSent { ref payment_preimage } => {
+                       assert_eq!(*payment_preimage, payment_preimage_1);
+               },
+               _ => panic!("Unexpected event"),
+       }
+
+       claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
+}
+
+#[test]
+fn monitor_failed_no_reestablish_response() {
+       // Test for receiving a channel_reestablish after a monitor update failure resulted in no
+       // response to a commitment_signed.
+       // Backported from chanmon_fail_consistency fuzz tests as it caught a long-standing
+       // debug_assert!() failure in channel_reestablish handling.
+       let mut nodes = create_network(2, &[None, None]);
+       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       // Route the payment and deliver the initial commitment_signed (with a monitor update failure
+       // on receipt).
+       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
+       let (payment_preimage_1, payment_hash_1) = get_payment_preimage_hash!(nodes[0]);
+       nodes[0].node.send_payment(route, payment_hash_1).unwrap();
+       check_added_monitors!(nodes[0], 1);
+
+       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
+       let mut events = nodes[0].node.get_and_clear_pending_msg_events();
+       assert_eq!(events.len(), 1);
+       let payment_event = SendEvent::from_event(events.pop().unwrap());
+       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
+       if let msgs::HandleError { err, action: Some(msgs::ErrorAction::IgnoreError) } = nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg).unwrap_err() {
+               assert_eq!(err, "Failed to update ChannelMonitor");
+       } else { panic!(); }
+       check_added_monitors!(nodes[1], 1);
+
+       // Now disconnect and immediately reconnect, delivering the channel_reestablish while nodes[1]
+       // is still failing to update monitors.
+       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
+       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
+
+       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id());
+       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id());
+
+       let as_reconnect = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id());
+       let bs_reconnect = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id());
+
+       nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &as_reconnect).unwrap();
+       nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bs_reconnect).unwrap();
+
+       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Ok(());
+       nodes[1].node.test_restore_channel_monitor();
+       check_added_monitors!(nodes[1], 1);
+       let bs_responses = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+
+       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_responses.0).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_responses.1).unwrap();
+       check_added_monitors!(nodes[0], 1);
+
+       let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
+       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa).unwrap();
+       check_added_monitors!(nodes[1], 1);
+
+       expect_pending_htlcs_forwardable!(nodes[1]);
+       expect_payment_received!(nodes[1], payment_hash_1, 1000000);
+
+       claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1);
+}
+
+#[test]
+fn first_message_on_recv_ordering() {
+       // Test that if the initial generator of a monitor-update-frozen state doesn't generate
+       // messages, we're willing to flip the order of response messages if neccessary in resposne to
+       // a commitment_signed which needs to send an RAA first.
+       // At a high level, our goal is to fail monitor updating in response to an RAA which needs no
+       // response and then handle a CS while in the failed state, requiring an RAA followed by a CS
+       // response. To do this, we start routing two payments, with the final RAA for the first being
+       // delivered while B is in AwaitingRAA, hence when we deliver the CS for the second B will
+       // have no pending response but will want to send a RAA/CS (with the updates for the second
+       // payment applied).
+       // Backported from chanmon_fail_consistency fuzz tests as it caught a bug here.
+       let mut nodes = create_network(2, &[None, None]);
+       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       // Route the first payment outbound, holding the last RAA for B until we are set up so that we
+       // can deliver it and fail the monitor update.
+       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
+       let (payment_preimage_1, payment_hash_1) = get_payment_preimage_hash!(nodes[0]);
+       nodes[0].node.send_payment(route, payment_hash_1).unwrap();
+       check_added_monitors!(nodes[0], 1);
+
+       let mut events = nodes[0].node.get_and_clear_pending_msg_events();
+       assert_eq!(events.len(), 1);
+       let payment_event = SendEvent::from_event(events.pop().unwrap());
+       assert_eq!(payment_event.node_id, nodes[1].node.get_our_node_id());
+       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
+       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       let bs_responses = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+
+       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_responses.0).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_responses.1).unwrap();
+       check_added_monitors!(nodes[0], 1);
+
+       let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
+
+       // Route the second payment, generating an update_add_htlc/commitment_signed
+       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
+       let (payment_preimage_2, payment_hash_2) = get_payment_preimage_hash!(nodes[0]);
+       nodes[0].node.send_payment(route, payment_hash_2).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       let mut events = nodes[0].node.get_and_clear_pending_msg_events();
+       assert_eq!(events.len(), 1);
+       let payment_event = SendEvent::from_event(events.pop().unwrap());
+       assert_eq!(payment_event.node_id, nodes[1].node.get_our_node_id());
+
+       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
+
+       // Deliver the final RAA for the first payment, which does not require a response. RAAs
+       // generally require a commitment_signed, so the fact that we're expecting an opposite response
+       // to the next message also tests resetting the delivery order.
+       if let msgs::HandleError { err, action: Some(msgs::ErrorAction::IgnoreError) } = nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa).unwrap_err() {
+               assert_eq!(err, "Failed to update ChannelMonitor");
+       } else { panic!(); }
+       check_added_monitors!(nodes[1], 1);
+
+       // Now deliver the update_add_htlc/commitment_signed for the second payment, which does need an
+       // RAA/CS response, which should be generated when we call test_restore_channel_monitor (with
+       // the appropriate HTLC acceptance).
+       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
+       if let msgs::HandleError { err, action: Some(msgs::ErrorAction::IgnoreError) } = nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg).unwrap_err() {
+               assert_eq!(err, "Previous monitor update failure prevented generation of RAA");
+       } else { panic!(); }
+
+       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Ok(());
+       nodes[1].node.test_restore_channel_monitor();
+       check_added_monitors!(nodes[1], 1);
+
+       expect_pending_htlcs_forwardable!(nodes[1]);
+       expect_payment_received!(nodes[1], payment_hash_1, 1000000);
+
+       let bs_responses = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_responses.0).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_responses.1).unwrap();
+       check_added_monitors!(nodes[0], 1);
+
+       let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
+       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa).unwrap();
+       check_added_monitors!(nodes[1], 1);
+
+       expect_pending_htlcs_forwardable!(nodes[1]);
+       expect_payment_received!(nodes[1], payment_hash_2, 1000000);
+
+       claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1);
+       claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
+}
+
+#[test]
+fn test_monitor_update_fail_claim() {
+       // Basic test for monitor update failures when processing claim_funds calls.
+       // We set up a simple 3-node network, sending a payment from A to B and failing B's monitor
+       // update to claim the payment. We then send a payment C->B->A, making the forward of this
+       // payment from B to A fail due to the paused channel. Finally, we restore the channel monitor
+       // updating and claim the payment on B.
+       let mut nodes = create_network(3, &[None, None, None]);
+       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+       create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
+
+       // Rebalance a bit so that we can send backwards from 3 to 2.
+       send_payment(&nodes[0], &[&nodes[1], &nodes[2]], 5000000);
+
+       let (payment_preimage_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
+
+       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
+       assert!(nodes[1].node.claim_funds(payment_preimage_1));
+       check_added_monitors!(nodes[1], 1);
+
+       let route = nodes[2].router.get_route(&nodes[0].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
+       let (_, payment_hash_2) = get_payment_preimage_hash!(nodes[0]);
+       nodes[2].node.send_payment(route, payment_hash_2).unwrap();
+       check_added_monitors!(nodes[2], 1);
+
+       // Successfully update the monitor on the 1<->2 channel, but the 0<->1 channel should still be
+       // paused, so forward shouldn't succeed until we call test_restore_channel_monitor().
+       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Ok(());
+
+       let mut events = nodes[2].node.get_and_clear_pending_msg_events();
+       assert_eq!(events.len(), 1);
+       let payment_event = SendEvent::from_event(events.pop().unwrap());
+       nodes[1].node.handle_update_add_htlc(&nodes[2].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
+       commitment_signed_dance!(nodes[1], nodes[2], payment_event.commitment_msg, false, true);
+
+       let bs_fail_update = get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
+       nodes[2].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_fail_update.update_fail_htlcs[0]).unwrap();
+       commitment_signed_dance!(nodes[2], nodes[1], bs_fail_update.commitment_signed, false, true);
+
+       let msg_events = nodes[2].node.get_and_clear_pending_msg_events();
+       assert_eq!(msg_events.len(), 1);
+       match msg_events[0] {
+               MessageSendEvent::PaymentFailureNetworkUpdate { update: msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg }} => {
+                       assert_eq!(msg.contents.short_channel_id, chan_1.0.contents.short_channel_id);
+                       assert_eq!(msg.contents.flags & 2, 2); // temp disabled
+               },
+               _ => panic!("Unexpected event"),
+       }
+
+       let events = nodes[2].node.get_and_clear_pending_events();
+       assert_eq!(events.len(), 1);
+       if let Event::PaymentFailed { payment_hash, rejected_by_dest, .. } = events[0] {
+               assert_eq!(payment_hash, payment_hash_2);
+               assert!(!rejected_by_dest);
+       } else { panic!("Unexpected event!"); }
+
+       // Now restore monitor updating on the 0<->1 channel and claim the funds on B.
+       nodes[1].node.test_restore_channel_monitor();
+       check_added_monitors!(nodes[1], 1);
+
+       let bs_fulfill_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+       nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_fulfill_update.update_fulfill_htlcs[0]).unwrap();
+       commitment_signed_dance!(nodes[0], nodes[1], bs_fulfill_update.commitment_signed, false);
+
+       let events = nodes[0].node.get_and_clear_pending_events();
+       assert_eq!(events.len(), 1);
+       if let Event::PaymentSent { payment_preimage, .. } = events[0] {
+               assert_eq!(payment_preimage, payment_preimage_1);
+       } else { panic!("Unexpected event!"); }
+}
+
+#[test]
+fn test_monitor_update_on_pending_forwards() {
+       // Basic test for monitor update failures when processing pending HTLC fail/add forwards.
+       // We do this with a simple 3-node network, sending a payment from A to C and one from C to A.
+       // The payment from A to C will be failed by C and pending a back-fail to A, while the payment
+       // from C to A will be pending a forward to A.
+       let mut nodes = create_network(3, &[None, None, None]);
+       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+       create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
+
+       // Rebalance a bit so that we can send backwards from 3 to 1.
+       send_payment(&nodes[0], &[&nodes[1], &nodes[2]], 5000000);
+
+       let (_, payment_hash_1) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1000000);
+       assert!(nodes[2].node.fail_htlc_backwards(&payment_hash_1));
+       expect_pending_htlcs_forwardable!(nodes[2]);
+       check_added_monitors!(nodes[2], 1);
+
+       let cs_fail_update = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
+       nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &cs_fail_update.update_fail_htlcs[0]).unwrap();
+       commitment_signed_dance!(nodes[1], nodes[2], cs_fail_update.commitment_signed, true, true);
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+
+       let route = nodes[2].router.get_route(&nodes[0].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
+       let (payment_preimage_2, payment_hash_2) = get_payment_preimage_hash!(nodes[0]);
+       nodes[2].node.send_payment(route, payment_hash_2).unwrap();
+       check_added_monitors!(nodes[2], 1);
+
+       let mut events = nodes[2].node.get_and_clear_pending_msg_events();
+       assert_eq!(events.len(), 1);
+       let payment_event = SendEvent::from_event(events.pop().unwrap());
+       nodes[1].node.handle_update_add_htlc(&nodes[2].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
+       commitment_signed_dance!(nodes[1], nodes[2], payment_event.commitment_msg, false);
+
+       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
+       expect_pending_htlcs_forwardable!(nodes[1]);
+       check_added_monitors!(nodes[1], 1);
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+
+       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Ok(());
+       nodes[1].node.test_restore_channel_monitor();
+       check_added_monitors!(nodes[1], 1);
+
+       let bs_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+       nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fail_htlcs[0]).unwrap();
+       nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_add_htlcs[0]).unwrap();
+       commitment_signed_dance!(nodes[0], nodes[1], bs_updates.commitment_signed, false, true);
+
+       let events = nodes[0].node.get_and_clear_pending_events();
+       assert_eq!(events.len(), 2);
+       if let Event::PaymentFailed { payment_hash, rejected_by_dest, .. } = events[0] {
+               assert_eq!(payment_hash, payment_hash_1);
+               assert!(rejected_by_dest);
+       } else { panic!("Unexpected event!"); }
+       match events[1] {
+               Event::PendingHTLCsForwardable { .. } => { },
+               _ => panic!("Unexpected event"),
+       };
+       nodes[0].node.process_pending_htlc_forwards();
+       expect_payment_received!(nodes[0], payment_hash_2, 1000000);
+
+       claim_payment(&nodes[2], &[&nodes[1], &nodes[0]], payment_preimage_2);
+}
+
+#[test]
+fn monitor_update_claim_fail_no_response() {
+       // Test for claim_funds resulting in both a monitor update failure and no message response (due
+       // to channel being AwaitingRAA).
+       // Backported from chanmon_fail_consistency fuzz tests as an unmerged version of the handling
+       // code was broken.
+       let mut nodes = create_network(2, &[None, None]);
+       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       // Forward a payment for B to claim
+       let (payment_preimage_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
+
+       // Now start forwarding a second payment, skipping the last RAA so B is in AwaitingRAA
+       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
+       let (payment_preimage_2, payment_hash_2) = get_payment_preimage_hash!(nodes[0]);
+       nodes[0].node.send_payment(route, payment_hash_2).unwrap();
+       check_added_monitors!(nodes[0], 1);
+
+       let mut events = nodes[0].node.get_and_clear_pending_msg_events();
+       assert_eq!(events.len(), 1);
+       let payment_event = SendEvent::from_event(events.pop().unwrap());
+       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
+       let as_raa = commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false, true, false, true);
+
+       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
+       assert!(nodes[1].node.claim_funds(payment_preimage_1));
+       check_added_monitors!(nodes[1], 1);
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+
+       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Ok(());
+       nodes[1].node.test_restore_channel_monitor();
+       check_added_monitors!(nodes[1], 1);
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+
+       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       expect_pending_htlcs_forwardable!(nodes[1]);
+       expect_payment_received!(nodes[1], payment_hash_2, 1000000);
+
+       let bs_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+       nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fulfill_htlcs[0]).unwrap();
+       commitment_signed_dance!(nodes[0], nodes[1], bs_updates.commitment_signed, false);
+
+       let events = nodes[0].node.get_and_clear_pending_events();
+       assert_eq!(events.len(), 1);
+       match events[0] {
+               Event::PaymentSent { ref payment_preimage } => {
+                       assert_eq!(*payment_preimage, payment_preimage_1);
+               },
+               _ => panic!("Unexpected event"),
+       }
+
+       claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
+}
+
+// Note that restore_between_fails with !fail_on_generate is useless
+// Also note that !fail_on_generate && !fail_on_signed is useless
+// Finally, note that !fail_on_signed is not possible with fail_on_generate && !restore_between_fails
+// confirm_a_first and restore_b_before_conf are wholly unrelated to earlier bools and
+// restore_b_before_conf has no meaning if !confirm_a_first
+fn do_during_funding_monitor_fail(fail_on_generate: bool, restore_between_fails: bool, fail_on_signed: bool, confirm_a_first: bool, restore_b_before_conf: bool) {
+       // Test that if the monitor update generated by funding_transaction_generated fails we continue
+       // the channel setup happily after the update is restored.
+       let mut nodes = create_network(2, &[None, None]);
+
+       nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 43).unwrap();
+       nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), LocalFeatures::new(), &get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id())).unwrap();
+       nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), LocalFeatures::new(), &get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id())).unwrap();
+
+       let (temporary_channel_id, funding_tx, funding_output) = create_funding_transaction(&nodes[0], 100000, 43);
+
+       if fail_on_generate {
+               *nodes[0].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
+       }
+       nodes[0].node.funding_transaction_generated(&temporary_channel_id, funding_output);
+       check_added_monitors!(nodes[0], 1);
+
+       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
+       nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id())).unwrap();
+       check_added_monitors!(nodes[1], 1);
+
+       if restore_between_fails {
+               assert!(fail_on_generate);
+               *nodes[0].chan_monitor.update_ret.lock().unwrap() = Ok(());
+               nodes[0].node.test_restore_channel_monitor();
+               check_added_monitors!(nodes[0], 1);
+               assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
+               assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
+       }
+
+       if fail_on_signed {
+               *nodes[0].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
+       } else {
+               assert!(restore_between_fails || !fail_on_generate); // We can't switch to good now (there's no monitor update)
+               assert!(fail_on_generate); // Somebody has to fail
+       }
+       let funding_signed_res = nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id()));
+       if fail_on_signed || !restore_between_fails {
+               if let msgs::HandleError { err, action: Some(msgs::ErrorAction::IgnoreError) } = funding_signed_res.unwrap_err() {
+                       if fail_on_generate && !restore_between_fails {
+                               assert_eq!(err, "Previous monitor update failure prevented funding_signed from allowing funding broadcast");
+                               check_added_monitors!(nodes[0], 0);
+                       } else {
+                               assert_eq!(err, "Failed to update ChannelMonitor");
+                               check_added_monitors!(nodes[0], 1);
+                       }
+               } else { panic!(); }
+
+               assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
+               *nodes[0].chan_monitor.update_ret.lock().unwrap() = Ok(());
+               nodes[0].node.test_restore_channel_monitor();
+       } else {
+               funding_signed_res.unwrap();
+       }
+
+       check_added_monitors!(nodes[0], 1);
+
+       let events = nodes[0].node.get_and_clear_pending_events();
+       assert_eq!(events.len(), 1);
+       match events[0] {
+               Event::FundingBroadcastSafe { ref funding_txo, user_channel_id } => {
+                       assert_eq!(user_channel_id, 43);
+                       assert_eq!(*funding_txo, funding_output);
+               },
+               _ => panic!("Unexpected event"),
+       };
+
+       if confirm_a_first {
+               confirm_transaction(&nodes[0].chain_monitor, &funding_tx, funding_tx.version);
+               nodes[1].node.handle_funding_locked(&nodes[0].node.get_our_node_id(), &get_event_msg!(nodes[0], MessageSendEvent::SendFundingLocked, nodes[1].node.get_our_node_id())).unwrap();
+       } else {
+               assert!(!restore_b_before_conf);
+               confirm_transaction(&nodes[1].chain_monitor, &funding_tx, funding_tx.version);
+               assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+       }
+
+       // Make sure nodes[1] isn't stupid enough to re-send the FundingLocked on reconnect
+       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
+       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
+       reconnect_nodes(&nodes[0], &nodes[1], (false, confirm_a_first), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
+       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+
+       if !restore_b_before_conf {
+               confirm_transaction(&nodes[1].chain_monitor, &funding_tx, funding_tx.version);
+               assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+               assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
+       }
+
+       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Ok(());
+       nodes[1].node.test_restore_channel_monitor();
+       check_added_monitors!(nodes[1], 1);
+
+       let (channel_id, (announcement, as_update, bs_update)) = if !confirm_a_first {
+               nodes[0].node.handle_funding_locked(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendFundingLocked, nodes[0].node.get_our_node_id())).unwrap();
+
+               confirm_transaction(&nodes[0].chain_monitor, &funding_tx, funding_tx.version);
+               let (funding_locked, channel_id) = create_chan_between_nodes_with_value_confirm_second(&nodes[1], &nodes[0]);
+               (channel_id, create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &funding_locked))
+       } else {
+               if restore_b_before_conf {
+                       confirm_transaction(&nodes[1].chain_monitor, &funding_tx, funding_tx.version);
+               }
+               let (funding_locked, channel_id) = create_chan_between_nodes_with_value_confirm_second(&nodes[0], &nodes[1]);
+               (channel_id, create_chan_between_nodes_with_value_b(&nodes[1], &nodes[0], &funding_locked))
+       };
+       for node in nodes.iter() {
+               assert!(node.router.handle_channel_announcement(&announcement).unwrap());
+               node.router.handle_channel_update(&as_update).unwrap();
+               node.router.handle_channel_update(&bs_update).unwrap();
+       }
+
+       send_payment(&nodes[0], &[&nodes[1]], 8000000);
+       close_channel(&nodes[0], &nodes[1], &channel_id, funding_tx, true);
+}
+
+#[test]
+fn during_funding_monitor_fail() {
+       do_during_funding_monitor_fail(false, false, true, true, true);
+       do_during_funding_monitor_fail(true, false, true, false, false);
+       do_during_funding_monitor_fail(true, true, true, true, false);
+       do_during_funding_monitor_fail(true, true, false, false, false);
+}
diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs
new file mode 100644 (file)
index 0000000..c1eaad0
--- /dev/null
@@ -0,0 +1,4704 @@
+use bitcoin::blockdata::block::BlockHeader;
+use bitcoin::blockdata::script::{Script,Builder};
+use bitcoin::blockdata::transaction::{TxIn, TxOut, Transaction, SigHashType};
+use bitcoin::blockdata::opcodes;
+use bitcoin::util::hash::BitcoinHash;
+use bitcoin::util::bip143;
+use bitcoin::consensus::encode::{self, Encodable, Decodable};
+
+use bitcoin_hashes::{Hash, HashEngine};
+use bitcoin_hashes::sha256::Hash as Sha256;
+use bitcoin_hashes::hash160::Hash as Hash160;
+use bitcoin_hashes::sha256d::Hash as Sha256dHash;
+
+use secp256k1::key::{PublicKey,SecretKey};
+use secp256k1::{Secp256k1,Signature};
+use secp256k1;
+
+use ln::msgs;
+use ln::msgs::{DecodeError, OptionalField, LocalFeatures, DataLossProtect};
+use ln::channelmonitor::ChannelMonitor;
+use ln::channelmanager::{PendingHTLCStatus, HTLCSource, HTLCFailReason, HTLCFailureMsg, PendingForwardHTLCInfo, RAACommitmentOrder, PaymentPreimage, PaymentHash, BREAKDOWN_TIMEOUT, MAX_LOCAL_BREAKDOWN_TIMEOUT};
+use ln::chan_utils::{TxCreationKeys,HTLCOutputInCommitment,HTLC_SUCCESS_TX_WEIGHT,HTLC_TIMEOUT_TX_WEIGHT};
+use ln::chan_utils;
+use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
+use chain::transaction::OutPoint;
+use chain::keysinterface::{ChannelKeys, KeysInterface};
+use util::transaction_utils;
+use util::ser::{Readable, ReadableArgs, Writeable, Writer, WriterWriteAdaptor};
+use util::logger::{Logger, LogHolder};
+use util::errors::APIError;
+use util::config::{UserConfig,ChannelConfig};
+
+use std;
+use std::default::Default;
+use std::{cmp,mem,fmt};
+use std::sync::{Arc};
+
+#[cfg(test)]
+pub struct ChannelValueStat {
+       pub value_to_self_msat: u64,
+       pub channel_value_msat: u64,
+       pub channel_reserve_msat: u64,
+       pub pending_outbound_htlcs_amount_msat: u64,
+       pub pending_inbound_htlcs_amount_msat: u64,
+       pub holding_cell_outbound_amount_msat: u64,
+       pub their_max_htlc_value_in_flight_msat: u64, // outgoing
+}
+
+enum InboundHTLCRemovalReason {
+       FailRelay(msgs::OnionErrorPacket),
+       FailMalformed(([u8; 32], u16)),
+       Fulfill(PaymentPreimage),
+}
+
+enum InboundHTLCState {
+       /// Added by remote, to be included in next local commitment tx.
+       RemoteAnnounced(PendingHTLCStatus),
+       /// Included in a received commitment_signed message (implying we've revoke_and_ack'ed it), but
+       /// the remote side hasn't yet revoked their previous state, which we need them to do before we
+       /// accept this HTLC. Implies AwaitingRemoteRevoke.
+       /// We also have not yet included this HTLC in a commitment_signed message, and are waiting on
+       /// a remote revoke_and_ack on a previous state before we can do so.
+       AwaitingRemoteRevokeToAnnounce(PendingHTLCStatus),
+       /// Included in a received commitment_signed message (implying we've revoke_and_ack'ed it), but
+       /// the remote side hasn't yet revoked their previous state, which we need them to do before we
+       /// accept this HTLC. Implies AwaitingRemoteRevoke.
+       /// We have included this HTLC in our latest commitment_signed and are now just waiting on a
+       /// revoke_and_ack.
+       AwaitingAnnouncedRemoteRevoke(PendingHTLCStatus),
+       Committed,
+       /// Removed by us and a new commitment_signed was sent (if we were AwaitingRemoteRevoke when we
+       /// created it we would have put it in the holding cell instead). When they next revoke_and_ack
+       /// we'll drop it.
+       /// Note that we have to keep an eye on the HTLC until we've received a broadcastable
+       /// commitment transaction without it as otherwise we'll have to force-close the channel to
+       /// claim it before the timeout (obviously doesn't apply to revoked HTLCs that we can't claim
+       /// anyway). That said, ChannelMonitor does this for us (see
+       /// ChannelMonitor::would_broadcast_at_height) so we actually remove the HTLC from our own
+       /// local state before then, once we're sure that the next commitment_signed and
+       /// ChannelMonitor::provide_latest_local_commitment_tx_info will not include this HTLC.
+       LocalRemoved(InboundHTLCRemovalReason),
+}
+
+struct InboundHTLCOutput {
+       htlc_id: u64,
+       amount_msat: u64,
+       cltv_expiry: u32,
+       payment_hash: PaymentHash,
+       state: InboundHTLCState,
+}
+
+enum OutboundHTLCState {
+       /// Added by us and included in a commitment_signed (if we were AwaitingRemoteRevoke when we
+       /// created it we would have put it in the holding cell instead). When they next revoke_and_ack
+       /// we will promote to Committed (note that they may not accept it until the next time we
+       /// revoke, but we don't really care about that:
+       ///  * they've revoked, so worst case we can announce an old state and get our (option on)
+       ///    money back (though we won't), and,
+       ///  * we'll send them a revoke when they send a commitment_signed, and since only they're
+       ///    allowed to remove it, the "can only be removed once committed on both sides" requirement
+       ///    doesn't matter to us and it's up to them to enforce it, worst-case they jump ahead but
+       ///    we'll never get out of sync).
+       /// Note that we Box the OnionPacket as it's rather large and we don't want to blow up
+       /// OutboundHTLCOutput's size just for a temporary bit
+       LocalAnnounced(Box<msgs::OnionPacket>),
+       Committed,
+       /// Remote removed this (outbound) HTLC. We're waiting on their commitment_signed to finalize
+       /// the change (though they'll need to revoke before we fail the payment).
+       RemoteRemoved(Option<HTLCFailReason>),
+       /// Remote removed this and sent a commitment_signed (implying we've revoke_and_ack'ed it), but
+       /// the remote side hasn't yet revoked their previous state, which we need them to do before we
+       /// can do any backwards failing. Implies AwaitingRemoteRevoke.
+       /// We also have not yet removed this HTLC in a commitment_signed message, and are waiting on a
+       /// remote revoke_and_ack on a previous state before we can do so.
+       AwaitingRemoteRevokeToRemove(Option<HTLCFailReason>),
+       /// Remote removed this and sent a commitment_signed (implying we've revoke_and_ack'ed it), but
+       /// the remote side hasn't yet revoked their previous state, which we need them to do before we
+       /// can do any backwards failing. Implies AwaitingRemoteRevoke.
+       /// We have removed this HTLC in our latest commitment_signed and are now just waiting on a
+       /// revoke_and_ack to drop completely.
+       AwaitingRemovedRemoteRevoke(Option<HTLCFailReason>),
+}
+
+struct OutboundHTLCOutput {
+       htlc_id: u64,
+       amount_msat: u64,
+       cltv_expiry: u32,
+       payment_hash: PaymentHash,
+       state: OutboundHTLCState,
+       source: HTLCSource,
+}
+
+/// See AwaitingRemoteRevoke ChannelState for more info
+enum HTLCUpdateAwaitingACK {
+       AddHTLC { // TODO: Time out if we're getting close to cltv_expiry
+               // always outbound
+               amount_msat: u64,
+               cltv_expiry: u32,
+               payment_hash: PaymentHash,
+               source: HTLCSource,
+               onion_routing_packet: msgs::OnionPacket,
+       },
+       ClaimHTLC {
+               payment_preimage: PaymentPreimage,
+               htlc_id: u64,
+       },
+       FailHTLC {
+               htlc_id: u64,
+               err_packet: msgs::OnionErrorPacket,
+       },
+}
+
+/// There are a few "states" and then a number of flags which can be applied:
+/// We first move through init with OurInitSent -> TheirInitSent -> FundingCreated -> FundingSent.
+/// TheirFundingLocked and OurFundingLocked then get set on FundingSent, and when both are set we
+/// move on to ChannelFunded.
+/// Note that PeerDisconnected can be set on both ChannelFunded and FundingSent.
+/// ChannelFunded can then get all remaining flags set on it, until we finish shutdown, then we
+/// move on to ShutdownComplete, at which point most calls into this channel are disallowed.
+enum ChannelState {
+       /// Implies we have (or are prepared to) send our open_channel/accept_channel message
+       OurInitSent = (1 << 0),
+       /// Implies we have received their open_channel/accept_channel message
+       TheirInitSent = (1 << 1),
+       /// We have sent funding_created and are awaiting a funding_signed to advance to FundingSent.
+       /// Note that this is nonsense for an inbound channel as we immediately generate funding_signed
+       /// upon receipt of funding_created, so simply skip this state.
+       FundingCreated = 4,
+       /// Set when we have received/sent funding_created and funding_signed and are thus now waiting
+       /// on the funding transaction to confirm. The FundingLocked flags are set to indicate when we
+       /// and our counterparty consider the funding transaction confirmed.
+       FundingSent = 8,
+       /// Flag which can be set on FundingSent to indicate they sent us a funding_locked message.
+       /// Once both TheirFundingLocked and OurFundingLocked are set, state moves on to ChannelFunded.
+       TheirFundingLocked = (1 << 4),
+       /// Flag which can be set on FundingSent to indicate we sent them a funding_locked message.
+       /// Once both TheirFundingLocked and OurFundingLocked are set, state moves on to ChannelFunded.
+       OurFundingLocked = (1 << 5),
+       ChannelFunded = 64,
+       /// Flag which is set on ChannelFunded and FundingSent indicating remote side is considered
+       /// "disconnected" and no updates are allowed until after we've done a channel_reestablish
+       /// dance.
+       PeerDisconnected = (1 << 7),
+       /// Flag which is set on ChannelFunded, FundingCreated, and FundingSent indicating the user has
+       /// told us they failed to update our ChannelMonitor somewhere and we should pause sending any
+       /// outbound messages until they've managed to do so.
+       MonitorUpdateFailed = (1 << 8),
+       /// Flag which implies that we have sent a commitment_signed but are awaiting the responding
+       /// revoke_and_ack message. During this time period, we can't generate new commitment_signed
+       /// messages as then we will be unable to determine which HTLCs they included in their
+       /// revoke_and_ack implicit ACK, so instead we have to hold them away temporarily to be sent
+       /// later.
+       /// Flag is set on ChannelFunded.
+       AwaitingRemoteRevoke = (1 << 9),
+       /// Flag which is set on ChannelFunded or FundingSent after receiving a shutdown message from
+       /// the remote end. If set, they may not add any new HTLCs to the channel, and we are expected
+       /// to respond with our own shutdown message when possible.
+       RemoteShutdownSent = (1 << 10),
+       /// Flag which is set on ChannelFunded or FundingSent after sending a shutdown message. At this
+       /// point, we may not add any new HTLCs to the channel.
+       /// TODO: Investigate some kind of timeout mechanism by which point the remote end must provide
+       /// us their shutdown.
+       LocalShutdownSent = (1 << 11),
+       /// We've successfully negotiated a closing_signed dance. At this point ChannelManager is about
+       /// to drop us, but we store this anyway.
+       ShutdownComplete = 4096,
+}
+const BOTH_SIDES_SHUTDOWN_MASK: u32 = (ChannelState::LocalShutdownSent as u32 | ChannelState::RemoteShutdownSent as u32);
+const MULTI_STATE_FLAGS: u32 = (BOTH_SIDES_SHUTDOWN_MASK | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateFailed as u32);
+
+const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
+
+// TODO: We should refactor this to be an Inbound/OutboundChannel until initial setup handshaking
+// has been completed, and then turn into a Channel to get compiler-time enforcement of things like
+// calling channel_id() before we're set up or things like get_outbound_funding_signed on an
+// inbound channel.
+pub(super) struct Channel {
+       config: ChannelConfig,
+
+       user_id: u64,
+
+       channel_id: [u8; 32],
+       channel_state: u32,
+       channel_outbound: bool,
+       secp_ctx: Secp256k1<secp256k1::All>,
+       channel_value_satoshis: u64,
+
+       local_keys: ChannelKeys,
+       shutdown_pubkey: PublicKey,
+
+       // Our commitment numbers start at 2^48-1 and count down, whereas the ones used in transaction
+       // generation start at 0 and count up...this simplifies some parts of implementation at the
+       // cost of others, but should really just be changed.
+
+       cur_local_commitment_transaction_number: u64,
+       cur_remote_commitment_transaction_number: u64,
+       value_to_self_msat: u64, // Excluding all pending_htlcs, excluding fees
+       pending_inbound_htlcs: Vec<InboundHTLCOutput>,
+       pending_outbound_htlcs: Vec<OutboundHTLCOutput>,
+       holding_cell_htlc_updates: Vec<HTLCUpdateAwaitingACK>,
+
+       /// When resending CS/RAA messages on channel monitor restoration or on reconnect, we always
+       /// need to ensure we resend them in the order we originally generated them. Note that because
+       /// there can only ever be one in-flight CS and/or one in-flight RAA at any time, it is
+       /// sufficient to simply set this to the opposite of any message we are generating as we
+       /// generate it. ie when we generate a CS, we set this to RAAFirst as, if there is a pending
+       /// in-flight RAA to resend, it will have been the first thing we generated, and thus we should
+       /// send it first.
+       resend_order: RAACommitmentOrder,
+
+       monitor_pending_funding_locked: bool,
+       monitor_pending_revoke_and_ack: bool,
+       monitor_pending_commitment_signed: bool,
+       monitor_pending_forwards: Vec<(PendingForwardHTLCInfo, u64)>,
+       monitor_pending_failures: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>,
+
+       // pending_update_fee is filled when sending and receiving update_fee
+       // For outbound channel, feerate_per_kw is updated with the value from
+       // pending_update_fee when revoke_and_ack is received
+       //
+       // For inbound channel, feerate_per_kw is updated when it receives
+       // commitment_signed and revoke_and_ack is generated
+       // The pending value is kept when another pair of update_fee and commitment_signed
+       // is received during AwaitingRemoteRevoke and relieved when the expected
+       // revoke_and_ack is received and new commitment_signed is generated to be
+       // sent to the funder. Otherwise, the pending value is removed when receiving
+       // commitment_signed.
+       pending_update_fee: Option<u64>,
+       // update_fee() during ChannelState::AwaitingRemoteRevoke is hold in
+       // holdina_cell_update_fee then moved to pending_udpate_fee when revoke_and_ack
+       // is received. holding_cell_update_fee is updated when there are additional
+       // update_fee() during ChannelState::AwaitingRemoteRevoke.
+       holding_cell_update_fee: Option<u64>,
+       next_local_htlc_id: u64,
+       next_remote_htlc_id: u64,
+       channel_update_count: u32,
+       feerate_per_kw: u64,
+
+       #[cfg(debug_assertions)]
+       /// Max to_local and to_remote outputs in a locally-generated commitment transaction
+       max_commitment_tx_output_local: ::std::sync::Mutex<(u64, u64)>,
+       #[cfg(debug_assertions)]
+       /// Max to_local and to_remote outputs in a remote-generated commitment transaction
+       max_commitment_tx_output_remote: ::std::sync::Mutex<(u64, u64)>,
+
+       #[cfg(test)]
+       // Used in ChannelManager's tests to send a revoked transaction
+       pub last_local_commitment_txn: Vec<Transaction>,
+       #[cfg(not(test))]
+       last_local_commitment_txn: Vec<Transaction>,
+
+       last_sent_closing_fee: Option<(u64, u64)>, // (feerate, fee)
+
+       /// The hash of the block in which the funding transaction reached our CONF_TARGET. We use this
+       /// to detect unconfirmation after a serialize-unserialize roundtrip where we may not see a full
+       /// series of block_connected/block_disconnected calls. Obviously this is not a guarantee as we
+       /// could miss the funding_tx_confirmed_in block as well, but it serves as a useful fallback.
+       funding_tx_confirmed_in: Option<Sha256dHash>,
+       short_channel_id: Option<u64>,
+       /// Used to deduplicate block_connected callbacks, also used to verify consistency during
+       /// ChannelManager deserialization (hence pub(super))
+       pub(super) last_block_connected: Sha256dHash,
+       funding_tx_confirmations: u64,
+
+       their_dust_limit_satoshis: u64,
+       #[cfg(test)]
+       pub(super) our_dust_limit_satoshis: u64,
+       #[cfg(not(test))]
+       our_dust_limit_satoshis: u64,
+       #[cfg(test)]
+       pub(super) their_max_htlc_value_in_flight_msat: u64,
+       #[cfg(not(test))]
+       their_max_htlc_value_in_flight_msat: u64,
+       //get_our_max_htlc_value_in_flight_msat(): u64,
+       /// minimum channel reserve for **self** to maintain - set by them.
+       their_channel_reserve_satoshis: u64,
+       //get_our_channel_reserve_satoshis(): u64,
+       their_htlc_minimum_msat: u64,
+       our_htlc_minimum_msat: u64,
+       their_to_self_delay: u16,
+       our_to_self_delay: u16,
+       #[cfg(test)]
+       pub their_max_accepted_htlcs: u16,
+       #[cfg(not(test))]
+       their_max_accepted_htlcs: u16,
+       //implied by OUR_MAX_HTLCS: our_max_accepted_htlcs: u16,
+       minimum_depth: u32,
+
+       their_funding_pubkey: Option<PublicKey>,
+       their_revocation_basepoint: Option<PublicKey>,
+       their_payment_basepoint: Option<PublicKey>,
+       their_delayed_payment_basepoint: Option<PublicKey>,
+       their_htlc_basepoint: Option<PublicKey>,
+       their_cur_commitment_point: Option<PublicKey>,
+
+       their_prev_commitment_point: Option<PublicKey>,
+       their_node_id: PublicKey,
+
+       their_shutdown_scriptpubkey: Option<Script>,
+
+       channel_monitor: ChannelMonitor,
+
+       logger: Arc<Logger>,
+}
+
+pub const OUR_MAX_HTLCS: u16 = 50; //TODO
+/// Confirmation count threshold at which we close a channel. Ideally we'd keep the channel around
+/// on ice until the funding transaction gets more confirmations, but the LN protocol doesn't
+/// really allow for this, so instead we're stuck closing it out at that point.
+const UNCONF_THRESHOLD: u32 = 6;
+/// Exposing these two constants for use in test in ChannelMonitor
+pub const COMMITMENT_TX_BASE_WEIGHT: u64 = 724;
+pub const COMMITMENT_TX_WEIGHT_PER_HTLC: u64 = 172;
+const SPENDING_INPUT_FOR_A_OUTPUT_WEIGHT: u64 = 79; // prevout: 36, nSequence: 4, script len: 1, witness lengths: (3+1)/4, sig: 73/4, if-selector: 1, redeemScript: (6 ops + 2*33 pubkeys + 1*2 delay)/4
+const B_OUTPUT_PLUS_SPENDING_INPUT_WEIGHT: u64 = 104; // prevout: 40, nSequence: 4, script len: 1, witness lengths: 3/4, sig: 73/4, pubkey: 33/4, output: 31 (TODO: Wrong? Useless?)
+/// Maximmum `funding_satoshis` value, according to the BOLT #2 specification
+/// it's 2^24.
+pub const MAX_FUNDING_SATOSHIS: u64 = (1 << 24);
+
+#[cfg(test)]
+pub const ACCEPTED_HTLC_SCRIPT_WEIGHT: usize = 138; //Here we have a diff due to HTLC CLTV expiry being < 2^15 in test
+#[cfg(not(test))]
+pub const ACCEPTED_HTLC_SCRIPT_WEIGHT: usize = 139;
+pub const OFFERED_HTLC_SCRIPT_WEIGHT: usize = 133;
+
+/// Used to return a simple Error back to ChannelManager. Will get converted to a
+/// msgs::ErrorAction::SendErrorMessage or msgs::ErrorAction::IgnoreError as appropriate with our
+/// channel_id in ChannelManager.
+pub(super) enum ChannelError {
+       Ignore(&'static str),
+       Close(&'static str),
+       CloseDelayBroadcast {
+               msg: &'static str,
+               update: Option<ChannelMonitor>
+       },
+}
+
+impl fmt::Debug for ChannelError {
+       fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+               match self {
+                       &ChannelError::Ignore(e) => write!(f, "Ignore : {}", e),
+                       &ChannelError::Close(e) => write!(f, "Close : {}", e),
+                       &ChannelError::CloseDelayBroadcast { msg, .. } => write!(f, "CloseDelayBroadcast : {}", msg)
+               }
+       }
+}
+
+macro_rules! secp_check {
+       ($res: expr, $err: expr) => {
+               match $res {
+                       Ok(thing) => thing,
+                       Err(_) => return Err(ChannelError::Close($err)),
+               }
+       };
+}
+
+impl Channel {
+       // Convert constants + channel value to limits:
+       fn get_our_max_htlc_value_in_flight_msat(channel_value_satoshis: u64) -> u64 {
+               channel_value_satoshis * 1000 / 10 //TODO
+       }
+
+       /// Returns a minimum channel reserve value **they** need to maintain
+       ///
+       /// Guaranteed to return a value no larger than channel_value_satoshis
+       pub(crate) fn get_our_channel_reserve_satoshis(channel_value_satoshis: u64) -> u64 {
+               let (q, _) = channel_value_satoshis.overflowing_div(100);
+               cmp::min(channel_value_satoshis, cmp::max(q, 1000)) //TODO
+       }
+
+       fn derive_our_dust_limit_satoshis(at_open_background_feerate: u64) -> u64 {
+               cmp::max(at_open_background_feerate * B_OUTPUT_PLUS_SPENDING_INPUT_WEIGHT / 1000, 546) //TODO
+       }
+
+       fn derive_our_htlc_minimum_msat(_at_open_channel_feerate_per_kw: u64) -> u64 {
+               1000 // TODO
+       }
+
+       // Constructors:
+       pub fn new_outbound(fee_estimator: &FeeEstimator, keys_provider: &Arc<KeysInterface>, their_node_id: PublicKey, channel_value_satoshis: u64, push_msat: u64, user_id: u64, logger: Arc<Logger>, config: &UserConfig) -> Result<Channel, APIError> {
+               let chan_keys = keys_provider.get_channel_keys(false);
+
+               if channel_value_satoshis >= MAX_FUNDING_SATOSHIS {
+                       return Err(APIError::APIMisuseError{err: "funding value > 2^24"});
+               }
+
+               if push_msat > channel_value_satoshis * 1000 {
+                       return Err(APIError::APIMisuseError{err: "push value > channel value"});
+               }
+               if config.own_channel_config.our_to_self_delay < BREAKDOWN_TIMEOUT {
+                       return Err(APIError::APIMisuseError{err: "Configured with an unreasonable our_to_self_delay putting user funds at risks"});
+               }
+
+
+               let background_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background);
+               if Channel::get_our_channel_reserve_satoshis(channel_value_satoshis) < Channel::derive_our_dust_limit_satoshis(background_feerate) {
+                       return Err(APIError::FeeRateTooHigh{err: format!("Not enough reserve above dust limit can be found at current fee rate({})", background_feerate), feerate: background_feerate});
+               }
+
+               let feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
+
+               let secp_ctx = Secp256k1::new();
+               let channel_monitor = ChannelMonitor::new(&chan_keys.revocation_base_key, &chan_keys.delayed_payment_base_key,
+                                                         &chan_keys.htlc_base_key, &chan_keys.payment_base_key, &keys_provider.get_shutdown_pubkey(), config.own_channel_config.our_to_self_delay,
+                                                         keys_provider.get_destination_script(), logger.clone());
+
+               Ok(Channel {
+                       user_id: user_id,
+                       config: config.channel_options.clone(),
+
+                       channel_id: keys_provider.get_channel_id(),
+                       channel_state: ChannelState::OurInitSent as u32,
+                       channel_outbound: true,
+                       secp_ctx: secp_ctx,
+                       channel_value_satoshis: channel_value_satoshis,
+
+                       local_keys: chan_keys,
+                       shutdown_pubkey: keys_provider.get_shutdown_pubkey(),
+                       cur_local_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
+                       cur_remote_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
+                       value_to_self_msat: channel_value_satoshis * 1000 - push_msat,
+
+                       pending_inbound_htlcs: Vec::new(),
+                       pending_outbound_htlcs: Vec::new(),
+                       holding_cell_htlc_updates: Vec::new(),
+                       pending_update_fee: None,
+                       holding_cell_update_fee: None,
+                       next_local_htlc_id: 0,
+                       next_remote_htlc_id: 0,
+                       channel_update_count: 1,
+
+                       resend_order: RAACommitmentOrder::CommitmentFirst,
+
+                       monitor_pending_funding_locked: false,
+                       monitor_pending_revoke_and_ack: false,
+                       monitor_pending_commitment_signed: false,
+                       monitor_pending_forwards: Vec::new(),
+                       monitor_pending_failures: Vec::new(),
+
+                       #[cfg(debug_assertions)]
+                       max_commitment_tx_output_local: ::std::sync::Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
+                       #[cfg(debug_assertions)]
+                       max_commitment_tx_output_remote: ::std::sync::Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
+
+                       last_local_commitment_txn: Vec::new(),
+
+                       last_sent_closing_fee: None,
+
+                       funding_tx_confirmed_in: None,
+                       short_channel_id: None,
+                       last_block_connected: Default::default(),
+                       funding_tx_confirmations: 0,
+
+                       feerate_per_kw: feerate,
+                       their_dust_limit_satoshis: 0,
+                       our_dust_limit_satoshis: Channel::derive_our_dust_limit_satoshis(background_feerate),
+                       their_max_htlc_value_in_flight_msat: 0,
+                       their_channel_reserve_satoshis: 0,
+                       their_htlc_minimum_msat: 0,
+                       our_htlc_minimum_msat: Channel::derive_our_htlc_minimum_msat(feerate),
+                       their_to_self_delay: 0,
+                       our_to_self_delay: config.own_channel_config.our_to_self_delay,
+                       their_max_accepted_htlcs: 0,
+                       minimum_depth: 0, // Filled in in accept_channel
+
+                       their_funding_pubkey: None,
+                       their_revocation_basepoint: None,
+                       their_payment_basepoint: None,
+                       their_delayed_payment_basepoint: None,
+                       their_htlc_basepoint: None,
+                       their_cur_commitment_point: None,
+
+                       their_prev_commitment_point: None,
+                       their_node_id: their_node_id,
+
+                       their_shutdown_scriptpubkey: None,
+
+                       channel_monitor: channel_monitor,
+
+                       logger,
+               })
+       }
+
+       fn check_remote_fee(fee_estimator: &FeeEstimator, feerate_per_kw: u32) -> Result<(), ChannelError> {
+               if (feerate_per_kw as u64) < fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background) {
+                       return Err(ChannelError::Close("Peer's feerate much too low"));
+               }
+               if (feerate_per_kw as u64) > fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::HighPriority) * 2 {
+                       return Err(ChannelError::Close("Peer's feerate much too high"));
+               }
+               Ok(())
+       }
+
+       /// Creates a new channel from a remote sides' request for one.
+       /// Assumes chain_hash has already been checked and corresponds with what we expect!
+       pub fn new_from_req(fee_estimator: &FeeEstimator, keys_provider: &Arc<KeysInterface>, their_node_id: PublicKey, their_local_features: LocalFeatures, msg: &msgs::OpenChannel, user_id: u64, logger: Arc<Logger>, config: &UserConfig) -> Result<Channel, ChannelError> {
+               let chan_keys = keys_provider.get_channel_keys(true);
+               let mut local_config = (*config).channel_options.clone();
+
+               if config.own_channel_config.our_to_self_delay < BREAKDOWN_TIMEOUT {
+                       return Err(ChannelError::Close("Configured with an unreasonable our_to_self_delay putting user funds at risks"));
+               }
+
+               // Check sanity of message fields:
+               if msg.funding_satoshis >= MAX_FUNDING_SATOSHIS {
+                       return Err(ChannelError::Close("funding value > 2^24"));
+               }
+               if msg.channel_reserve_satoshis > msg.funding_satoshis {
+                       return Err(ChannelError::Close("Bogus channel_reserve_satoshis"));
+               }
+               if msg.push_msat > (msg.funding_satoshis - msg.channel_reserve_satoshis) * 1000 {
+                       return Err(ChannelError::Close("push_msat larger than funding value"));
+               }
+               if msg.dust_limit_satoshis > msg.funding_satoshis {
+                       return Err(ChannelError::Close("Peer never wants payout outputs?"));
+               }
+               if msg.dust_limit_satoshis > msg.channel_reserve_satoshis {
+                       return Err(ChannelError::Close("Bogus; channel reserve is less than dust limit"));
+               }
+               if msg.htlc_minimum_msat >= (msg.funding_satoshis - msg.channel_reserve_satoshis) * 1000 {
+                       return Err(ChannelError::Close("Minimum htlc value is full channel value"));
+               }
+               Channel::check_remote_fee(fee_estimator, msg.feerate_per_kw)?;
+
+               if msg.to_self_delay > config.peer_channel_config_limits.their_to_self_delay || msg.to_self_delay > MAX_LOCAL_BREAKDOWN_TIMEOUT {
+                       return Err(ChannelError::Close("They wanted our payments to be delayed by a needlessly long period"));
+               }
+               if msg.max_accepted_htlcs < 1 {
+                       return Err(ChannelError::Close("0 max_accpted_htlcs makes for a useless channel"));
+               }
+               if msg.max_accepted_htlcs > 483 {
+                       return Err(ChannelError::Close("max_accpted_htlcs > 483"));
+               }
+
+               // Now check against optional parameters as set by config...
+               if msg.funding_satoshis < config.peer_channel_config_limits.min_funding_satoshis {
+                       return Err(ChannelError::Close("funding satoshis is less than the user specified limit"));
+               }
+               if msg.htlc_minimum_msat > config.peer_channel_config_limits.max_htlc_minimum_msat {
+                       return Err(ChannelError::Close("htlc minimum msat is higher than the user specified limit"));
+               }
+               if msg.max_htlc_value_in_flight_msat < config.peer_channel_config_limits.min_max_htlc_value_in_flight_msat {
+                       return Err(ChannelError::Close("max htlc value in flight msat is less than the user specified limit"));
+               }
+               if msg.channel_reserve_satoshis > config.peer_channel_config_limits.max_channel_reserve_satoshis {
+                       return Err(ChannelError::Close("channel reserve satoshis is higher than the user specified limit"));
+               }
+               if msg.max_accepted_htlcs < config.peer_channel_config_limits.min_max_accepted_htlcs {
+                       return Err(ChannelError::Close("max accepted htlcs is less than the user specified limit"));
+               }
+               if msg.dust_limit_satoshis < config.peer_channel_config_limits.min_dust_limit_satoshis {
+                       return Err(ChannelError::Close("dust limit satoshis is less than the user specified limit"));
+               }
+               if msg.dust_limit_satoshis > config.peer_channel_config_limits.max_dust_limit_satoshis {
+                       return Err(ChannelError::Close("dust limit satoshis is greater than the user specified limit"));
+               }
+
+               // Convert things into internal flags and prep our state:
+
+               let their_announce = if (msg.channel_flags & 1) == 1 { true } else { false };
+               if config.peer_channel_config_limits.force_announced_channel_preference {
+                       if local_config.announced_channel != their_announce {
+                               return Err(ChannelError::Close("Peer tried to open channel but their announcement preference is different from ours"));
+                       }
+               }
+               // we either accept their preference or the preferences match
+               local_config.announced_channel = their_announce;
+
+               let background_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background);
+
+               let our_dust_limit_satoshis = Channel::derive_our_dust_limit_satoshis(background_feerate);
+               let our_channel_reserve_satoshis = Channel::get_our_channel_reserve_satoshis(msg.funding_satoshis);
+               if our_channel_reserve_satoshis < our_dust_limit_satoshis {
+                       return Err(ChannelError::Close("Suitable channel reserve not found. aborting"));
+               }
+               if msg.channel_reserve_satoshis < our_dust_limit_satoshis {
+                       return Err(ChannelError::Close("channel_reserve_satoshis too small"));
+               }
+               if our_channel_reserve_satoshis < msg.dust_limit_satoshis {
+                       return Err(ChannelError::Close("Dust limit too high for our channel reserve"));
+               }
+
+               // check if the funder's amount for the initial commitment tx is sufficient
+               // for full fee payment
+               let funders_amount_msat = msg.funding_satoshis * 1000 - msg.push_msat;
+               if funders_amount_msat < background_feerate * COMMITMENT_TX_BASE_WEIGHT {
+                       return Err(ChannelError::Close("Insufficient funding amount for initial commitment"));
+               }
+
+               let to_local_msat = msg.push_msat;
+               let to_remote_msat = funders_amount_msat - background_feerate * COMMITMENT_TX_BASE_WEIGHT;
+               if to_local_msat <= msg.channel_reserve_satoshis * 1000 && to_remote_msat <= our_channel_reserve_satoshis * 1000 {
+                       return Err(ChannelError::Close("Insufficient funding amount for initial commitment"));
+               }
+
+               let secp_ctx = Secp256k1::new();
+               let mut channel_monitor = ChannelMonitor::new(&chan_keys.revocation_base_key, &chan_keys.delayed_payment_base_key,
+                                                             &chan_keys.htlc_base_key, &chan_keys.payment_base_key, &keys_provider.get_shutdown_pubkey(), config.own_channel_config.our_to_self_delay,
+                                                             keys_provider.get_destination_script(), logger.clone());
+               channel_monitor.set_their_base_keys(&msg.htlc_basepoint, &msg.delayed_payment_basepoint);
+               channel_monitor.set_their_to_self_delay(msg.to_self_delay);
+
+               let their_shutdown_scriptpubkey = if their_local_features.supports_upfront_shutdown_script() {
+                       match &msg.shutdown_scriptpubkey {
+                               &OptionalField::Present(ref script) => {
+                                       // Peer is signaling upfront_shutdown and has provided a non-accepted scriptpubkey format. We enforce it while receiving shutdown msg
+                                       if script.is_p2pkh() || script.is_p2sh() || script.is_v0_p2wsh() || script.is_v0_p2wpkh() {
+                                               Some(script.clone())
+                                       // Peer is signaling upfront_shutdown and has opt-out with a 0-length script. We don't enforce anything
+                                       } else if script.len() == 0 {
+                                               None
+                                       // Peer is signaling upfront_shutdown and has provided a non-accepted scriptpubkey format. Fail the channel
+                                       } else {
+                                               return Err(ChannelError::Close("Peer is signaling upfront_shutdown but has provided a non-accepted scriptpubkey format"));
+                                       }
+                               },
+                               // Peer is signaling upfront shutdown but don't opt-out with correct mechanism (a.k.a 0-length script). Peer looks buggy, we fail the channel
+                               &OptionalField::Absent => {
+                                       return Err(ChannelError::Close("Peer is signaling upfront_shutdown but we don't get any script. Use 0-length script to opt-out"));
+                               }
+                       }
+               } else { None };
+
+               let mut chan = Channel {
+                       user_id: user_id,
+                       config: local_config,
+
+                       channel_id: msg.temporary_channel_id,
+                       channel_state: (ChannelState::OurInitSent as u32) | (ChannelState::TheirInitSent as u32),
+                       channel_outbound: false,
+                       secp_ctx: secp_ctx,
+
+                       local_keys: chan_keys,
+                       shutdown_pubkey: keys_provider.get_shutdown_pubkey(),
+                       cur_local_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
+                       cur_remote_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
+                       value_to_self_msat: msg.push_msat,
+
+                       pending_inbound_htlcs: Vec::new(),
+                       pending_outbound_htlcs: Vec::new(),
+                       holding_cell_htlc_updates: Vec::new(),
+                       pending_update_fee: None,
+                       holding_cell_update_fee: None,
+                       next_local_htlc_id: 0,
+                       next_remote_htlc_id: 0,
+                       channel_update_count: 1,
+
+                       resend_order: RAACommitmentOrder::CommitmentFirst,
+
+                       monitor_pending_funding_locked: false,
+                       monitor_pending_revoke_and_ack: false,
+                       monitor_pending_commitment_signed: false,
+                       monitor_pending_forwards: Vec::new(),
+                       monitor_pending_failures: Vec::new(),
+
+                       #[cfg(debug_assertions)]
+                       max_commitment_tx_output_local: ::std::sync::Mutex::new((msg.push_msat, msg.funding_satoshis * 1000 - msg.push_msat)),
+                       #[cfg(debug_assertions)]
+                       max_commitment_tx_output_remote: ::std::sync::Mutex::new((msg.push_msat, msg.funding_satoshis * 1000 - msg.push_msat)),
+
+                       last_local_commitment_txn: Vec::new(),
+
+                       last_sent_closing_fee: None,
+
+                       funding_tx_confirmed_in: None,
+                       short_channel_id: None,
+                       last_block_connected: Default::default(),
+                       funding_tx_confirmations: 0,
+
+                       feerate_per_kw: msg.feerate_per_kw as u64,
+                       channel_value_satoshis: msg.funding_satoshis,
+                       their_dust_limit_satoshis: msg.dust_limit_satoshis,
+                       our_dust_limit_satoshis: our_dust_limit_satoshis,
+                       their_max_htlc_value_in_flight_msat: cmp::min(msg.max_htlc_value_in_flight_msat, msg.funding_satoshis * 1000),
+                       their_channel_reserve_satoshis: msg.channel_reserve_satoshis,
+                       their_htlc_minimum_msat: msg.htlc_minimum_msat,
+                       our_htlc_minimum_msat: Channel::derive_our_htlc_minimum_msat(msg.feerate_per_kw as u64),
+                       their_to_self_delay: msg.to_self_delay,
+                       our_to_self_delay: config.own_channel_config.our_to_self_delay,
+                       their_max_accepted_htlcs: msg.max_accepted_htlcs,
+                       minimum_depth: config.own_channel_config.minimum_depth,
+
+                       their_funding_pubkey: Some(msg.funding_pubkey),
+                       their_revocation_basepoint: Some(msg.revocation_basepoint),
+                       their_payment_basepoint: Some(msg.payment_basepoint),
+                       their_delayed_payment_basepoint: Some(msg.delayed_payment_basepoint),
+                       their_htlc_basepoint: Some(msg.htlc_basepoint),
+                       their_cur_commitment_point: Some(msg.first_per_commitment_point),
+
+                       their_prev_commitment_point: None,
+                       their_node_id: their_node_id,
+
+                       their_shutdown_scriptpubkey,
+
+                       channel_monitor: channel_monitor,
+
+                       logger,
+               };
+
+               let obscure_factor = chan.get_commitment_transaction_number_obscure_factor();
+               chan.channel_monitor.set_commitment_obscure_factor(obscure_factor);
+
+               Ok(chan)
+       }
+
+       // Utilities to derive keys:
+
+       fn build_local_commitment_secret(&self, idx: u64) -> SecretKey {
+               let res = chan_utils::build_commitment_secret(self.local_keys.commitment_seed, idx);
+               SecretKey::from_slice(&res).unwrap()
+       }
+
+       // Utilities to build transactions:
+
+       fn get_commitment_transaction_number_obscure_factor(&self) -> u64 {
+               let mut sha = Sha256::engine();
+               let our_payment_basepoint = PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.payment_base_key);
+
+               if self.channel_outbound {
+                       sha.input(&our_payment_basepoint.serialize());
+                       sha.input(&self.their_payment_basepoint.unwrap().serialize());
+               } else {
+                       sha.input(&self.their_payment_basepoint.unwrap().serialize());
+                       sha.input(&our_payment_basepoint.serialize());
+               }
+               let res = Sha256::from_engine(sha).into_inner();
+
+               ((res[26] as u64) << 5*8) |
+               ((res[27] as u64) << 4*8) |
+               ((res[28] as u64) << 3*8) |
+               ((res[29] as u64) << 2*8) |
+               ((res[30] as u64) << 1*8) |
+               ((res[31] as u64) << 0*8)
+       }
+
+       /// Transaction nomenclature is somewhat confusing here as there are many different cases - a
+       /// transaction is referred to as "a's transaction" implying that a will be able to broadcast
+       /// the transaction. Thus, b will generally be sending a signature over such a transaction to
+       /// a, and a can revoke the transaction by providing b the relevant per_commitment_secret. As
+       /// such, a transaction is generally the result of b increasing the amount paid to a (or adding
+       /// an HTLC to a).
+       /// @local is used only to convert relevant internal structures which refer to remote vs local
+       /// to decide value of outputs and direction of HTLCs.
+       /// @generated_by_local is used to determine *which* HTLCs to include - noting that the HTLC
+       /// state may indicate that one peer has informed the other that they'd like to add an HTLC but
+       /// have not yet committed it. Such HTLCs will only be included in transactions which are being
+       /// generated by the peer which proposed adding the HTLCs, and thus we need to understand both
+       /// which peer generated this transaction and "to whom" this transaction flows.
+       /// Returns (the transaction built, the number of HTLC outputs which were present in the
+       /// transaction, the list of HTLCs which were not ignored when building the transaction).
+       /// Note that below-dust HTLCs are included in the third return value, but not the second, and
+       /// sources are provided only for outbound HTLCs in the third return value.
+       #[inline]
+       fn build_commitment_transaction(&self, commitment_number: u64, keys: &TxCreationKeys, local: bool, generated_by_local: bool, feerate_per_kw: u64) -> (Transaction, usize, Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)>) {
+               let obscured_commitment_transaction_number = self.get_commitment_transaction_number_obscure_factor() ^ (INITIAL_COMMITMENT_NUMBER - commitment_number);
+
+               let txins = {
+                       let mut ins: Vec<TxIn> = Vec::new();
+                       ins.push(TxIn {
+                               previous_output: self.channel_monitor.get_funding_txo().unwrap().into_bitcoin_outpoint(),
+                               script_sig: Script::new(),
+                               sequence: ((0x80 as u32) << 8*3) | ((obscured_commitment_transaction_number >> 3*8) as u32),
+                               witness: Vec::new(),
+                       });
+                       ins
+               };
+
+               let mut txouts: Vec<(TxOut, Option<(HTLCOutputInCommitment, Option<&HTLCSource>)>)> = Vec::with_capacity(self.pending_inbound_htlcs.len() + self.pending_outbound_htlcs.len() + 2);
+               let mut included_dust_htlcs: Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)> = Vec::new();
+
+               let dust_limit_satoshis = if local { self.our_dust_limit_satoshis } else { self.their_dust_limit_satoshis };
+               let mut remote_htlc_total_msat = 0;
+               let mut local_htlc_total_msat = 0;
+               let mut value_to_self_msat_offset = 0;
+
+               log_trace!(self, "Building commitment transaction number {} for {}, generated by {} with fee {}...", commitment_number, if local { "us" } else { "remote" }, if generated_by_local { "us" } else { "remote" }, feerate_per_kw);
+
+               macro_rules! get_htlc_in_commitment {
+                       ($htlc: expr, $offered: expr) => {
+                               HTLCOutputInCommitment {
+                                       offered: $offered,
+                                       amount_msat: $htlc.amount_msat,
+                                       cltv_expiry: $htlc.cltv_expiry,
+                                       payment_hash: $htlc.payment_hash,
+                                       transaction_output_index: None
+                               }
+                       }
+               }
+
+               macro_rules! add_htlc_output {
+                       ($htlc: expr, $outbound: expr, $source: expr, $state_name: expr) => {
+                               if $outbound == local { // "offered HTLC output"
+                                       let htlc_in_tx = get_htlc_in_commitment!($htlc, true);
+                                       if $htlc.amount_msat / 1000 >= dust_limit_satoshis + (feerate_per_kw * HTLC_TIMEOUT_TX_WEIGHT / 1000) {
+                                               log_trace!(self, "   ...including {} {} HTLC {} (hash {}) with value {}", if $outbound { "outbound" } else { "inbound" }, $state_name, $htlc.htlc_id, log_bytes!($htlc.payment_hash.0), $htlc.amount_msat);
+                                               txouts.push((TxOut {
+                                                       script_pubkey: chan_utils::get_htlc_redeemscript(&htlc_in_tx, &keys).to_v0_p2wsh(),
+                                                       value: $htlc.amount_msat / 1000
+                                               }, Some((htlc_in_tx, $source))));
+                                       } else {
+                                               log_trace!(self, "   ...including {} {} dust HTLC {} (hash {}) with value {} due to dust limit", if $outbound { "outbound" } else { "inbound" }, $state_name, $htlc.htlc_id, log_bytes!($htlc.payment_hash.0), $htlc.amount_msat);
+                                               included_dust_htlcs.push((htlc_in_tx, $source));
+                                       }
+                               } else {
+                                       let htlc_in_tx = get_htlc_in_commitment!($htlc, false);
+                                       if $htlc.amount_msat / 1000 >= dust_limit_satoshis + (feerate_per_kw * HTLC_SUCCESS_TX_WEIGHT / 1000) {
+                                               log_trace!(self, "   ...including {} {} HTLC {} (hash {}) with value {}", if $outbound { "outbound" } else { "inbound" }, $state_name, $htlc.htlc_id, log_bytes!($htlc.payment_hash.0), $htlc.amount_msat);
+                                               txouts.push((TxOut { // "received HTLC output"
+                                                       script_pubkey: chan_utils::get_htlc_redeemscript(&htlc_in_tx, &keys).to_v0_p2wsh(),
+                                                       value: $htlc.amount_msat / 1000
+                                               }, Some((htlc_in_tx, $source))));
+                                       } else {
+                                               log_trace!(self, "   ...including {} {} dust HTLC {} (hash {}) with value {}", if $outbound { "outbound" } else { "inbound" }, $state_name, $htlc.htlc_id, log_bytes!($htlc.payment_hash.0), $htlc.amount_msat);
+                                               included_dust_htlcs.push((htlc_in_tx, $source));
+                                       }
+                               }
+                       }
+               }
+
+               for ref htlc in self.pending_inbound_htlcs.iter() {
+                       let (include, state_name) = match htlc.state {
+                               InboundHTLCState::RemoteAnnounced(_) => (!generated_by_local, "RemoteAnnounced"),
+                               InboundHTLCState::AwaitingRemoteRevokeToAnnounce(_) => (!generated_by_local, "AwaitingRemoteRevokeToAnnounce"),
+                               InboundHTLCState::AwaitingAnnouncedRemoteRevoke(_) => (true, "AwaitingAnnouncedRemoteRevoke"),
+                               InboundHTLCState::Committed => (true, "Committed"),
+                               InboundHTLCState::LocalRemoved(_) => (!generated_by_local, "LocalRemoved"),
+                       };
+
+                       if include {
+                               add_htlc_output!(htlc, false, None, state_name);
+                               remote_htlc_total_msat += htlc.amount_msat;
+                       } else {
+                               log_trace!(self, "   ...not including inbound HTLC {} (hash {}) with value {} due to state ({})", htlc.htlc_id, log_bytes!(htlc.payment_hash.0), htlc.amount_msat, state_name);
+                               match &htlc.state {
+                                       &InboundHTLCState::LocalRemoved(ref reason) => {
+                                               if generated_by_local {
+                                                       if let &InboundHTLCRemovalReason::Fulfill(_) = reason {
+                                                               value_to_self_msat_offset += htlc.amount_msat as i64;
+                                                       }
+                                               }
+                                       },
+                                       _ => {},
+                               }
+                       }
+               }
+
+               for ref htlc in self.pending_outbound_htlcs.iter() {
+                       let (include, state_name) = match htlc.state {
+                               OutboundHTLCState::LocalAnnounced(_) => (generated_by_local, "LocalAnnounced"),
+                               OutboundHTLCState::Committed => (true, "Committed"),
+                               OutboundHTLCState::RemoteRemoved(_) => (generated_by_local, "RemoteRemoved"),
+                               OutboundHTLCState::AwaitingRemoteRevokeToRemove(_) => (generated_by_local, "AwaitingRemoteRevokeToRemove"),
+                               OutboundHTLCState::AwaitingRemovedRemoteRevoke(_) => (false, "AwaitingRemovedRemoteRevoke"),
+                       };
+
+                       if include {
+                               add_htlc_output!(htlc, true, Some(&htlc.source), state_name);
+                               local_htlc_total_msat += htlc.amount_msat;
+                       } else {
+                               log_trace!(self, "   ...not including outbound HTLC {} (hash {}) with value {} due to state ({})", htlc.htlc_id, log_bytes!(htlc.payment_hash.0), htlc.amount_msat, state_name);
+                               match htlc.state {
+                                       OutboundHTLCState::AwaitingRemoteRevokeToRemove(None)|OutboundHTLCState::AwaitingRemovedRemoteRevoke(None) => {
+                                               value_to_self_msat_offset -= htlc.amount_msat as i64;
+                                       },
+                                       OutboundHTLCState::RemoteRemoved(None) => {
+                                               if !generated_by_local {
+                                                       value_to_self_msat_offset -= htlc.amount_msat as i64;
+                                               }
+                                       },
+                                       _ => {},
+                               }
+                       }
+               }
+
+               let value_to_self_msat: i64 = (self.value_to_self_msat - local_htlc_total_msat) as i64 + value_to_self_msat_offset;
+               assert!(value_to_self_msat >= 0);
+               // Note that in case they have several just-awaiting-last-RAA fulfills in-progress (ie
+               // AwaitingRemoteRevokeToRemove or AwaitingRemovedRemoteRevoke) we may have allowed them to
+               // "violate" their reserve value by couting those against it. Thus, we have to convert
+               // everything to i64 before subtracting as otherwise we can overflow.
+               let value_to_remote_msat: i64 = (self.channel_value_satoshis * 1000) as i64 - (self.value_to_self_msat as i64) - (remote_htlc_total_msat as i64) - value_to_self_msat_offset;
+               assert!(value_to_remote_msat >= 0);
+
+               #[cfg(debug_assertions)]
+               {
+                       // Make sure that the to_self/to_remote is always either past the appropriate
+                       // channel_reserve *or* it is making progress towards it.
+                       let mut max_commitment_tx_output = if generated_by_local {
+                               self.max_commitment_tx_output_local.lock().unwrap()
+                       } else {
+                               self.max_commitment_tx_output_remote.lock().unwrap()
+                       };
+                       debug_assert!(max_commitment_tx_output.0 <= value_to_self_msat as u64 || value_to_self_msat / 1000 >= self.their_channel_reserve_satoshis as i64);
+                       max_commitment_tx_output.0 = cmp::max(max_commitment_tx_output.0, value_to_self_msat as u64);
+                       debug_assert!(max_commitment_tx_output.1 <= value_to_remote_msat as u64 || value_to_remote_msat / 1000 >= Channel::get_our_channel_reserve_satoshis(self.channel_value_satoshis) as i64);
+                       max_commitment_tx_output.1 = cmp::max(max_commitment_tx_output.1, value_to_remote_msat as u64);
+               }
+
+               let total_fee: u64 = feerate_per_kw * (COMMITMENT_TX_BASE_WEIGHT + (txouts.len() as u64) * COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000;
+               let (value_to_self, value_to_remote) = if self.channel_outbound {
+                       (value_to_self_msat / 1000 - total_fee as i64, value_to_remote_msat / 1000)
+               } else {
+                       (value_to_self_msat / 1000, value_to_remote_msat / 1000 - total_fee as i64)
+               };
+
+               let value_to_a = if local { value_to_self } else { value_to_remote };
+               let value_to_b = if local { value_to_remote } else { value_to_self };
+
+               if value_to_a >= (dust_limit_satoshis as i64) {
+                       log_trace!(self, "   ...including {} output with value {}", if local { "to_local" } else { "to_remote" }, value_to_a);
+                       txouts.push((TxOut {
+                               script_pubkey: chan_utils::get_revokeable_redeemscript(&keys.revocation_key,
+                                                                                      if local { self.their_to_self_delay } else { self.our_to_self_delay },
+                                                                                      &keys.a_delayed_payment_key).to_v0_p2wsh(),
+                               value: value_to_a as u64
+                       }, None));
+               }
+
+               if value_to_b >= (dust_limit_satoshis as i64) {
+                       log_trace!(self, "   ...including {} output with value {}", if local { "to_remote" } else { "to_local" }, value_to_b);
+                       txouts.push((TxOut {
+                               script_pubkey: Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0)
+                                                            .push_slice(&Hash160::hash(&keys.b_payment_key.serialize())[..])
+                                                            .into_script(),
+                               value: value_to_b as u64
+                       }, None));
+               }
+
+               transaction_utils::sort_outputs(&mut txouts, |a, b| {
+                       if let &Some(ref a_htlc) = a {
+                               if let &Some(ref b_htlc) = b {
+                                       a_htlc.0.cltv_expiry.cmp(&b_htlc.0.cltv_expiry)
+                                               // Note that due to hash collisions, we have to have a fallback comparison
+                                               // here for fuzztarget mode (otherwise at least chanmon_fail_consistency
+                                               // may fail)!
+                                               .then(a_htlc.0.payment_hash.0.cmp(&b_htlc.0.payment_hash.0))
+                               // For non-HTLC outputs, if they're copying our SPK we don't really care if we
+                               // close the channel due to mismatches - they're doing something dumb:
+                               } else { cmp::Ordering::Equal }
+                       } else { cmp::Ordering::Equal }
+               });
+
+               let mut outputs: Vec<TxOut> = Vec::with_capacity(txouts.len());
+               let mut htlcs_included: Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)> = Vec::with_capacity(txouts.len() + included_dust_htlcs.len());
+               for (idx, mut out) in txouts.drain(..).enumerate() {
+                       outputs.push(out.0);
+                       if let Some((mut htlc, source_option)) = out.1.take() {
+                               htlc.transaction_output_index = Some(idx as u32);
+                               htlcs_included.push((htlc, source_option));
+                       }
+               }
+               let non_dust_htlc_count = htlcs_included.len();
+               htlcs_included.append(&mut included_dust_htlcs);
+
+               (Transaction {
+                       version: 2,
+                       lock_time: ((0x20 as u32) << 8*3) | ((obscured_commitment_transaction_number & 0xffffffu64) as u32),
+                       input: txins,
+                       output: outputs,
+               }, non_dust_htlc_count, htlcs_included)
+       }
+
+       #[inline]
+       fn get_closing_scriptpubkey(&self) -> Script {
+               let our_channel_close_key_hash = Hash160::hash(&self.shutdown_pubkey.serialize());
+               Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(&our_channel_close_key_hash[..]).into_script()
+       }
+
+       #[inline]
+       fn get_closing_transaction_weight(a_scriptpubkey: &Script, b_scriptpubkey: &Script) -> u64 {
+               (4 + 1 + 36 + 4 + 1 + 1 + 2*(8+1) + 4 + a_scriptpubkey.len() as u64 + b_scriptpubkey.len() as u64)*4 + 2 + 1 + 1 + 2*(1 + 72)
+       }
+
+       #[inline]
+       fn build_closing_transaction(&self, proposed_total_fee_satoshis: u64, skip_remote_output: bool) -> (Transaction, u64) {
+               let txins = {
+                       let mut ins: Vec<TxIn> = Vec::new();
+                       ins.push(TxIn {
+                               previous_output: self.channel_monitor.get_funding_txo().unwrap().into_bitcoin_outpoint(),
+                               script_sig: Script::new(),
+                               sequence: 0xffffffff,
+                               witness: Vec::new(),
+                       });
+                       ins
+               };
+
+               assert!(self.pending_inbound_htlcs.is_empty());
+               assert!(self.pending_outbound_htlcs.is_empty());
+               let mut txouts: Vec<(TxOut, ())> = Vec::new();
+
+               let mut total_fee_satoshis = proposed_total_fee_satoshis;
+               let value_to_self: i64 = (self.value_to_self_msat as i64) / 1000 - if self.channel_outbound { total_fee_satoshis as i64 } else { 0 };
+               let value_to_remote: i64 = ((self.channel_value_satoshis * 1000 - self.value_to_self_msat) as i64 / 1000) - if self.channel_outbound { 0 } else { total_fee_satoshis as i64 };
+
+               if value_to_self < 0 {
+                       assert!(self.channel_outbound);
+                       total_fee_satoshis += (-value_to_self) as u64;
+               } else if value_to_remote < 0 {
+                       assert!(!self.channel_outbound);
+                       total_fee_satoshis += (-value_to_remote) as u64;
+               }
+
+               if !skip_remote_output && value_to_remote as u64 > self.our_dust_limit_satoshis {
+                       txouts.push((TxOut {
+                               script_pubkey: self.their_shutdown_scriptpubkey.clone().unwrap(),
+                               value: value_to_remote as u64
+                       }, ()));
+               }
+
+               if value_to_self as u64 > self.our_dust_limit_satoshis {
+                       txouts.push((TxOut {
+                               script_pubkey: self.get_closing_scriptpubkey(),
+                               value: value_to_self as u64
+                       }, ()));
+               }
+
+               transaction_utils::sort_outputs(&mut txouts, |_, _| { cmp::Ordering::Equal }); // Ordering doesnt matter if they used our pubkey...
+
+               let mut outputs: Vec<TxOut> = Vec::new();
+               for out in txouts.drain(..) {
+                       outputs.push(out.0);
+               }
+
+               (Transaction {
+                       version: 2,
+                       lock_time: 0,
+                       input: txins,
+                       output: outputs,
+               }, total_fee_satoshis)
+       }
+
+       #[inline]
+       /// Creates a set of keys for build_commitment_transaction to generate a transaction which our
+       /// counterparty will sign (ie DO NOT send signatures over a transaction created by this to
+       /// our counterparty!)
+       /// The result is a transaction which we can revoke ownership of (ie a "local" transaction)
+       /// TODO Some magic rust shit to compile-time check this?
+       fn build_local_transaction_keys(&self, commitment_number: u64) -> Result<TxCreationKeys, ChannelError> {
+               let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &self.build_local_commitment_secret(commitment_number));
+               let delayed_payment_base = PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.delayed_payment_base_key);
+               let htlc_basepoint = PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.htlc_base_key);
+
+               Ok(secp_check!(TxCreationKeys::new(&self.secp_ctx, &per_commitment_point, &delayed_payment_base, &htlc_basepoint, &self.their_revocation_basepoint.unwrap(), &self.their_payment_basepoint.unwrap(), &self.their_htlc_basepoint.unwrap()), "Local tx keys generation got bogus keys"))
+       }
+
+       #[inline]
+       /// Creates a set of keys for build_commitment_transaction to generate a transaction which we
+       /// will sign and send to our counterparty.
+       /// If an Err is returned, it is a ChannelError::Close (for get_outbound_funding_created)
+       fn build_remote_transaction_keys(&self) -> Result<TxCreationKeys, ChannelError> {
+               //TODO: Ensure that the payment_key derived here ends up in the library users' wallet as we
+               //may see payments to it!
+               let payment_basepoint = PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.payment_base_key);
+               let revocation_basepoint = PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.revocation_base_key);
+               let htlc_basepoint = PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.htlc_base_key);
+
+               Ok(secp_check!(TxCreationKeys::new(&self.secp_ctx, &self.their_cur_commitment_point.unwrap(), &self.their_delayed_payment_basepoint.unwrap(), &self.their_htlc_basepoint.unwrap(), &revocation_basepoint, &payment_basepoint, &htlc_basepoint), "Remote tx keys generation got bogus keys"))
+       }
+
+       /// Gets the redeemscript for the funding transaction output (ie the funding transaction output
+       /// pays to get_funding_redeemscript().to_v0_p2wsh()).
+       /// Panics if called before accept_channel/new_from_req
+       pub fn get_funding_redeemscript(&self) -> Script {
+               let builder = Builder::new().push_opcode(opcodes::all::OP_PUSHNUM_2);
+               let our_funding_key = PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.funding_key).serialize();
+               let their_funding_key = self.their_funding_pubkey.expect("get_funding_redeemscript only allowed after accept_channel").serialize();
+               if our_funding_key[..] < their_funding_key[..] {
+                       builder.push_slice(&our_funding_key)
+                               .push_slice(&their_funding_key)
+               } else {
+                       builder.push_slice(&their_funding_key)
+                               .push_slice(&our_funding_key)
+               }.push_opcode(opcodes::all::OP_PUSHNUM_2).push_opcode(opcodes::all::OP_CHECKMULTISIG).into_script()
+       }
+
+       fn sign_commitment_transaction(&self, tx: &mut Transaction, their_sig: &Signature) -> Signature {
+               if tx.input.len() != 1 {
+                       panic!("Tried to sign commitment transaction that had input count != 1!");
+               }
+               if tx.input[0].witness.len() != 0 {
+                       panic!("Tried to re-sign commitment transaction");
+               }
+
+               let funding_redeemscript = self.get_funding_redeemscript();
+
+               let sighash = hash_to_message!(&bip143::SighashComponents::new(&tx).sighash_all(&tx.input[0], &funding_redeemscript, self.channel_value_satoshis)[..]);
+               let our_sig = self.secp_ctx.sign(&sighash, &self.local_keys.funding_key);
+
+               tx.input[0].witness.push(Vec::new()); // First is the multisig dummy
+
+               let our_funding_key = PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.funding_key).serialize();
+               let their_funding_key = self.their_funding_pubkey.unwrap().serialize();
+               if our_funding_key[..] < their_funding_key[..] {
+                       tx.input[0].witness.push(our_sig.serialize_der().to_vec());
+                       tx.input[0].witness.push(their_sig.serialize_der().to_vec());
+               } else {
+                       tx.input[0].witness.push(their_sig.serialize_der().to_vec());
+                       tx.input[0].witness.push(our_sig.serialize_der().to_vec());
+               }
+               tx.input[0].witness[1].push(SigHashType::All as u8);
+               tx.input[0].witness[2].push(SigHashType::All as u8);
+
+               tx.input[0].witness.push(funding_redeemscript.into_bytes());
+
+               our_sig
+       }
+
+       /// Builds the htlc-success or htlc-timeout transaction which spends a given HTLC output
+       /// @local is used only to convert relevant internal structures which refer to remote vs local
+       /// to decide value of outputs and direction of HTLCs.
+       fn build_htlc_transaction(&self, prev_hash: &Sha256dHash, htlc: &HTLCOutputInCommitment, local: bool, keys: &TxCreationKeys, feerate_per_kw: u64) -> Transaction {
+               chan_utils::build_htlc_transaction(prev_hash, feerate_per_kw, if local { self.their_to_self_delay } else { self.our_to_self_delay }, htlc, &keys.a_delayed_payment_key, &keys.revocation_key)
+       }
+
+       fn create_htlc_tx_signature(&self, tx: &Transaction, htlc: &HTLCOutputInCommitment, keys: &TxCreationKeys) -> Result<(Script, Signature, bool), ChannelError> {
+               if tx.input.len() != 1 {
+                       panic!("Tried to sign HTLC transaction that had input count != 1!");
+               }
+
+               let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &keys);
+
+               let our_htlc_key = secp_check!(chan_utils::derive_private_key(&self.secp_ctx, &keys.per_commitment_point, &self.local_keys.htlc_base_key), "Derived invalid key, peer is maliciously selecting parameters");
+               let sighash = hash_to_message!(&bip143::SighashComponents::new(&tx).sighash_all(&tx.input[0], &htlc_redeemscript, htlc.amount_msat / 1000)[..]);
+               let is_local_tx = PublicKey::from_secret_key(&self.secp_ctx, &our_htlc_key) == keys.a_htlc_key;
+               Ok((htlc_redeemscript, self.secp_ctx.sign(&sighash, &our_htlc_key), is_local_tx))
+       }
+
+       /// Signs a transaction created by build_htlc_transaction. If the transaction is an
+       /// HTLC-Success transaction (ie htlc.offered is false), preimage must be set!
+       fn sign_htlc_transaction(&self, tx: &mut Transaction, their_sig: &Signature, preimage: &Option<PaymentPreimage>, htlc: &HTLCOutputInCommitment, keys: &TxCreationKeys) -> Result<Signature, ChannelError> {
+               if tx.input.len() != 1 {
+                       panic!("Tried to sign HTLC transaction that had input count != 1!");
+               }
+               if tx.input[0].witness.len() != 0 {
+                       panic!("Tried to re-sign HTLC transaction");
+               }
+
+               let (htlc_redeemscript, our_sig, local_tx) = self.create_htlc_tx_signature(tx, htlc, keys)?;
+
+               tx.input[0].witness.push(Vec::new()); // First is the multisig dummy
+
+               if local_tx { // b, then a
+                       tx.input[0].witness.push(their_sig.serialize_der().to_vec());
+                       tx.input[0].witness.push(our_sig.serialize_der().to_vec());
+               } else {
+                       tx.input[0].witness.push(our_sig.serialize_der().to_vec());
+                       tx.input[0].witness.push(their_sig.serialize_der().to_vec());
+               }
+               tx.input[0].witness[1].push(SigHashType::All as u8);
+               tx.input[0].witness[2].push(SigHashType::All as u8);
+
+               if htlc.offered {
+                       tx.input[0].witness.push(Vec::new());
+               } else {
+                       tx.input[0].witness.push(preimage.unwrap().0.to_vec());
+               }
+
+               tx.input[0].witness.push(htlc_redeemscript.into_bytes());
+
+               Ok(our_sig)
+       }
+
+       /// Per HTLC, only one get_update_fail_htlc or get_update_fulfill_htlc call may be made.
+       /// In such cases we debug_assert!(false) and return an IgnoreError. Thus, will always return
+       /// Ok(_) if debug assertions are turned on and preconditions are met.
+       fn get_update_fulfill_htlc(&mut self, htlc_id_arg: u64, payment_preimage_arg: PaymentPreimage) -> Result<(Option<msgs::UpdateFulfillHTLC>, Option<ChannelMonitor>), ChannelError> {
+               // Either ChannelFunded got set (which means it won't be unset) or there is no way any
+               // caller thought we could have something claimed (cause we wouldn't have accepted in an
+               // incoming HTLC anyway). If we got to ShutdownComplete, callers aren't allowed to call us,
+               // either.
+               if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
+                       panic!("Was asked to fulfill an HTLC when channel was not in an operational state");
+               }
+               assert_eq!(self.channel_state & ChannelState::ShutdownComplete as u32, 0);
+
+               let payment_hash_calc = PaymentHash(Sha256::hash(&payment_preimage_arg.0[..]).into_inner());
+
+               // ChannelManager may generate duplicate claims/fails due to HTLC update events from
+               // on-chain ChannelsMonitors during block rescan. Ideally we'd figure out a way to drop
+               // these, but for now we just have to treat them as normal.
+
+               let mut pending_idx = std::usize::MAX;
+               for (idx, htlc) in self.pending_inbound_htlcs.iter().enumerate() {
+                       if htlc.htlc_id == htlc_id_arg {
+                               assert_eq!(htlc.payment_hash, payment_hash_calc);
+                               match htlc.state {
+                                       InboundHTLCState::Committed => {},
+                                       InboundHTLCState::LocalRemoved(ref reason) => {
+                                               if let &InboundHTLCRemovalReason::Fulfill(_) = reason {
+                                               } else {
+                                                       log_warn!(self, "Have preimage and want to fulfill HTLC with payment hash {} we already failed against channel {}", log_bytes!(htlc.payment_hash.0), log_bytes!(self.channel_id()));
+                                               }
+                                               return Ok((None, None));
+                                       },
+                                       _ => {
+                                               debug_assert!(false, "Have an inbound HTLC we tried to claim before it was fully committed to");
+                                               // Don't return in release mode here so that we can update channel_monitor
+                                       }
+                               }
+                               pending_idx = idx;
+                               break;
+                       }
+               }
+               if pending_idx == std::usize::MAX {
+                       return Err(ChannelError::Ignore("Unable to find a pending HTLC which matched the given HTLC ID"));
+               }
+
+               // Now update local state:
+               //
+               // We have to put the payment_preimage in the channel_monitor right away here to ensure we
+               // can claim it even if the channel hits the chain before we see their next commitment.
+               self.channel_monitor.provide_payment_preimage(&payment_hash_calc, &payment_preimage_arg);
+
+               if (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32 | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateFailed as u32)) != 0 {
+                       for pending_update in self.holding_cell_htlc_updates.iter() {
+                               match pending_update {
+                                       &HTLCUpdateAwaitingACK::ClaimHTLC { htlc_id, .. } => {
+                                               if htlc_id_arg == htlc_id {
+                                                       return Ok((None, None));
+                                               }
+                                       },
+                                       &HTLCUpdateAwaitingACK::FailHTLC { htlc_id, .. } => {
+                                               if htlc_id_arg == htlc_id {
+                                                       log_warn!(self, "Have preimage and want to fulfill HTLC with pending failure against channel {}", log_bytes!(self.channel_id()));
+                                                       // TODO: We may actually be able to switch to a fulfill here, though its
+                                                       // rare enough it may not be worth the complexity burden.
+                                                       return Ok((None, Some(self.channel_monitor.clone())));
+                                               }
+                                       },
+                                       _ => {}
+                               }
+                       }
+                       log_trace!(self, "Adding HTLC claim to holding_cell! Current state: {}", self.channel_state);
+                       self.holding_cell_htlc_updates.push(HTLCUpdateAwaitingACK::ClaimHTLC {
+                               payment_preimage: payment_preimage_arg, htlc_id: htlc_id_arg,
+                       });
+                       return Ok((None, Some(self.channel_monitor.clone())));
+               }
+
+               {
+                       let htlc = &mut self.pending_inbound_htlcs[pending_idx];
+                       if let InboundHTLCState::Committed = htlc.state {
+                       } else {
+                               debug_assert!(false, "Have an inbound HTLC we tried to claim before it was fully committed to");
+                               return Ok((None, Some(self.channel_monitor.clone())));
+                       }
+                       log_trace!(self, "Upgrading HTLC {} to LocalRemoved with a Fulfill!", log_bytes!(htlc.payment_hash.0));
+                       htlc.state = InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::Fulfill(payment_preimage_arg.clone()));
+               }
+
+               Ok((Some(msgs::UpdateFulfillHTLC {
+                       channel_id: self.channel_id(),
+                       htlc_id: htlc_id_arg,
+                       payment_preimage: payment_preimage_arg,
+               }), Some(self.channel_monitor.clone())))
+       }
+
+       pub fn get_update_fulfill_htlc_and_commit(&mut self, htlc_id: u64, payment_preimage: PaymentPreimage) -> Result<(Option<(msgs::UpdateFulfillHTLC, msgs::CommitmentSigned)>, Option<ChannelMonitor>), ChannelError> {
+               match self.get_update_fulfill_htlc(htlc_id, payment_preimage)? {
+                       (Some(update_fulfill_htlc), _) => {
+                               let (commitment, monitor_update) = self.send_commitment_no_status_check()?;
+                               Ok((Some((update_fulfill_htlc, commitment)), Some(monitor_update)))
+                       },
+                       (None, Some(channel_monitor)) => Ok((None, Some(channel_monitor))),
+                       (None, None) => Ok((None, None))
+               }
+       }
+
+       /// Per HTLC, only one get_update_fail_htlc or get_update_fulfill_htlc call may be made.
+       /// In such cases we debug_assert!(false) and return an IgnoreError. Thus, will always return
+       /// Ok(_) if debug assertions are turned on and preconditions are met.
+       pub fn get_update_fail_htlc(&mut self, htlc_id_arg: u64, err_packet: msgs::OnionErrorPacket) -> Result<Option<msgs::UpdateFailHTLC>, ChannelError> {
+               if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
+                       panic!("Was asked to fail an HTLC when channel was not in an operational state");
+               }
+               assert_eq!(self.channel_state & ChannelState::ShutdownComplete as u32, 0);
+
+               // ChannelManager may generate duplicate claims/fails due to HTLC update events from
+               // on-chain ChannelsMonitors during block rescan. Ideally we'd figure out a way to drop
+               // these, but for now we just have to treat them as normal.
+
+               let mut pending_idx = std::usize::MAX;
+               for (idx, htlc) in self.pending_inbound_htlcs.iter().enumerate() {
+                       if htlc.htlc_id == htlc_id_arg {
+                               match htlc.state {
+                                       InboundHTLCState::Committed => {},
+                                       InboundHTLCState::LocalRemoved(_) => {
+                                               return Ok(None);
+                                       },
+                                       _ => {
+                                               debug_assert!(false, "Have an inbound HTLC we tried to claim before it was fully committed to");
+                                               return Err(ChannelError::Ignore("Unable to find a pending HTLC which matched the given HTLC ID"));
+                                       }
+                               }
+                               pending_idx = idx;
+                       }
+               }
+               if pending_idx == std::usize::MAX {
+                       return Err(ChannelError::Ignore("Unable to find a pending HTLC which matched the given HTLC ID"));
+               }
+
+               // Now update local state:
+               if (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32 | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateFailed as u32)) != 0 {
+                       for pending_update in self.holding_cell_htlc_updates.iter() {
+                               match pending_update {
+                                       &HTLCUpdateAwaitingACK::ClaimHTLC { htlc_id, .. } => {
+                                               if htlc_id_arg == htlc_id {
+                                                       return Err(ChannelError::Ignore("Unable to find a pending HTLC which matched the given HTLC ID"));
+                                               }
+                                       },
+                                       &HTLCUpdateAwaitingACK::FailHTLC { htlc_id, .. } => {
+                                               if htlc_id_arg == htlc_id {
+                                                       return Err(ChannelError::Ignore("Unable to find a pending HTLC which matched the given HTLC ID"));
+                                               }
+                                       },
+                                       _ => {}
+                               }
+                       }
+                       self.holding_cell_htlc_updates.push(HTLCUpdateAwaitingACK::FailHTLC {
+                               htlc_id: htlc_id_arg,
+                               err_packet,
+                       });
+                       return Ok(None);
+               }
+
+               {
+                       let htlc = &mut self.pending_inbound_htlcs[pending_idx];
+                       htlc.state = InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::FailRelay(err_packet.clone()));
+               }
+
+               Ok(Some(msgs::UpdateFailHTLC {
+                       channel_id: self.channel_id(),
+                       htlc_id: htlc_id_arg,
+                       reason: err_packet
+               }))
+       }
+
+       // Message handlers:
+
+       pub fn accept_channel(&mut self, msg: &msgs::AcceptChannel, config: &UserConfig, their_local_features: LocalFeatures) -> Result<(), ChannelError> {
+               // Check sanity of message fields:
+               if !self.channel_outbound {
+                       return Err(ChannelError::Close("Got an accept_channel message from an inbound peer"));
+               }
+               if self.channel_state != ChannelState::OurInitSent as u32 {
+                       return Err(ChannelError::Close("Got an accept_channel message at a strange time"));
+               }
+               if msg.dust_limit_satoshis > 21000000 * 100000000 {
+                       return Err(ChannelError::Close("Peer never wants payout outputs?"));
+               }
+               if msg.channel_reserve_satoshis > self.channel_value_satoshis {
+                       return Err(ChannelError::Close("Bogus channel_reserve_satoshis"));
+               }
+               if msg.dust_limit_satoshis > msg.channel_reserve_satoshis {
+                       return Err(ChannelError::Close("Bogus channel_reserve and dust_limit"));
+               }
+               if msg.channel_reserve_satoshis < self.our_dust_limit_satoshis {
+                       return Err(ChannelError::Close("Peer never wants payout outputs?"));
+               }
+               if msg.dust_limit_satoshis > Channel::get_our_channel_reserve_satoshis(self.channel_value_satoshis) {
+                       return Err(ChannelError::Close("Dust limit is bigger than our channel reverse"));
+               }
+               if msg.htlc_minimum_msat >= (self.channel_value_satoshis - msg.channel_reserve_satoshis) * 1000 {
+                       return Err(ChannelError::Close("Minimum htlc value is full channel value"));
+               }
+               if msg.to_self_delay > config.peer_channel_config_limits.their_to_self_delay || msg.to_self_delay > MAX_LOCAL_BREAKDOWN_TIMEOUT {
+                       return Err(ChannelError::Close("They wanted our payments to be delayed by a needlessly long period"));
+               }
+               if msg.max_accepted_htlcs < 1 {
+                       return Err(ChannelError::Close("0 max_accepted_htlcs makes for a useless channel"));
+               }
+               if msg.max_accepted_htlcs > 483 {
+                       return Err(ChannelError::Close("max_accepted_htlcs > 483"));
+               }
+
+               // Now check against optional parameters as set by config...
+               if msg.htlc_minimum_msat > config.peer_channel_config_limits.max_htlc_minimum_msat {
+                       return Err(ChannelError::Close("htlc minimum msat is higher than the user specified limit"));
+               }
+               if msg.max_htlc_value_in_flight_msat < config.peer_channel_config_limits.min_max_htlc_value_in_flight_msat {
+                       return Err(ChannelError::Close("max htlc value in flight msat is less than the user specified limit"));
+               }
+               if msg.channel_reserve_satoshis > config.peer_channel_config_limits.max_channel_reserve_satoshis {
+                       return Err(ChannelError::Close("channel reserve satoshis is higher than the user specified limit"));
+               }
+               if msg.max_accepted_htlcs < config.peer_channel_config_limits.min_max_accepted_htlcs {
+                       return Err(ChannelError::Close("max accepted htlcs is less than the user specified limit"));
+               }
+               if msg.dust_limit_satoshis < config.peer_channel_config_limits.min_dust_limit_satoshis {
+                       return Err(ChannelError::Close("dust limit satoshis is less than the user specified limit"));
+               }
+               if msg.dust_limit_satoshis > config.peer_channel_config_limits.max_dust_limit_satoshis {
+                       return Err(ChannelError::Close("dust limit satoshis is greater than the user specified limit"));
+               }
+               if msg.minimum_depth > config.peer_channel_config_limits.max_minimum_depth {
+                       return Err(ChannelError::Close("We consider the minimum depth to be unreasonably large"));
+               }
+
+               let their_shutdown_scriptpubkey = if their_local_features.supports_upfront_shutdown_script() {
+                       match &msg.shutdown_scriptpubkey {
+                               &OptionalField::Present(ref script) => {
+                                       // Peer is signaling upfront_shutdown and has provided a non-accepted scriptpubkey format. We enforce it while receiving shutdown msg
+                                       if script.is_p2pkh() || script.is_p2sh() || script.is_v0_p2wsh() || script.is_v0_p2wpkh() {
+                                               Some(script.clone())
+                                       // Peer is signaling upfront_shutdown and has opt-out with a 0-length script. We don't enforce anything
+                                       } else if script.len() == 0 {
+                                               None
+                                       // Peer is signaling upfront_shutdown and has provided a non-accepted scriptpubkey format. Fail the channel
+                                       } else {
+                                               return Err(ChannelError::Close("Peer is signaling upfront_shutdown but has provided a non-accepted scriptpubkey format"));
+                                       }
+                               },
+                               // Peer is signaling upfront shutdown but don't opt-out with correct mechanism (a.k.a 0-length script). Peer looks buggy, we fail the channel
+                               &OptionalField::Absent => {
+                                       return Err(ChannelError::Close("Peer is signaling upfront_shutdown but we don't get any script. Use 0-length script to opt-out"));
+                               }
+                       }
+               } else { None };
+
+               self.channel_monitor.set_their_base_keys(&msg.htlc_basepoint, &msg.delayed_payment_basepoint);
+
+               self.their_dust_limit_satoshis = msg.dust_limit_satoshis;
+               self.their_max_htlc_value_in_flight_msat = cmp::min(msg.max_htlc_value_in_flight_msat, self.channel_value_satoshis * 1000);
+               self.their_channel_reserve_satoshis = msg.channel_reserve_satoshis;
+               self.their_htlc_minimum_msat = msg.htlc_minimum_msat;
+               self.their_to_self_delay = msg.to_self_delay;
+               self.their_max_accepted_htlcs = msg.max_accepted_htlcs;
+               self.minimum_depth = msg.minimum_depth;
+               self.their_funding_pubkey = Some(msg.funding_pubkey);
+               self.their_revocation_basepoint = Some(msg.revocation_basepoint);
+               self.their_payment_basepoint = Some(msg.payment_basepoint);
+               self.their_delayed_payment_basepoint = Some(msg.delayed_payment_basepoint);
+               self.their_htlc_basepoint = Some(msg.htlc_basepoint);
+               self.their_cur_commitment_point = Some(msg.first_per_commitment_point);
+               self.their_shutdown_scriptpubkey = their_shutdown_scriptpubkey;
+
+               let obscure_factor = self.get_commitment_transaction_number_obscure_factor();
+               self.channel_monitor.set_commitment_obscure_factor(obscure_factor);
+               self.channel_monitor.set_their_to_self_delay(msg.to_self_delay);
+
+               self.channel_state = ChannelState::OurInitSent as u32 | ChannelState::TheirInitSent as u32;
+
+               Ok(())
+       }
+
+       fn funding_created_signature(&mut self, sig: &Signature) -> Result<(Transaction, Transaction, Signature, TxCreationKeys), ChannelError> {
+               let funding_script = self.get_funding_redeemscript();
+
+               let local_keys = self.build_local_transaction_keys(self.cur_local_commitment_transaction_number)?;
+               let mut local_initial_commitment_tx = self.build_commitment_transaction(self.cur_local_commitment_transaction_number, &local_keys, true, false, self.feerate_per_kw).0;
+               let local_sighash = hash_to_message!(&bip143::SighashComponents::new(&local_initial_commitment_tx).sighash_all(&local_initial_commitment_tx.input[0], &funding_script, self.channel_value_satoshis)[..]);
+
+               // They sign the "local" commitment transaction...
+               secp_check!(self.secp_ctx.verify(&local_sighash, &sig, &self.their_funding_pubkey.unwrap()), "Invalid funding_created signature from peer");
+
+               // ...and we sign it, allowing us to broadcast the tx if we wish
+               self.sign_commitment_transaction(&mut local_initial_commitment_tx, sig);
+
+               let remote_keys = self.build_remote_transaction_keys()?;
+               let remote_initial_commitment_tx = self.build_commitment_transaction(self.cur_remote_commitment_transaction_number, &remote_keys, false, false, self.feerate_per_kw).0;
+               let remote_sighash = hash_to_message!(&bip143::SighashComponents::new(&remote_initial_commitment_tx).sighash_all(&remote_initial_commitment_tx.input[0], &funding_script, self.channel_value_satoshis)[..]);
+
+               // We sign the "remote" commitment transaction, allowing them to broadcast the tx if they wish.
+               Ok((remote_initial_commitment_tx, local_initial_commitment_tx, self.secp_ctx.sign(&remote_sighash, &self.local_keys.funding_key), local_keys))
+       }
+
+       pub fn funding_created(&mut self, msg: &msgs::FundingCreated) -> Result<(msgs::FundingSigned, ChannelMonitor), ChannelError> {
+               if self.channel_outbound {
+                       return Err(ChannelError::Close("Received funding_created for an outbound channel?"));
+               }
+               if self.channel_state != (ChannelState::OurInitSent as u32 | ChannelState::TheirInitSent as u32) {
+                       // BOLT 2 says that if we disconnect before we send funding_signed we SHOULD NOT
+                       // remember the channel, so it's safe to just send an error_message here and drop the
+                       // channel.
+                       return Err(ChannelError::Close("Received funding_created after we got the channel!"));
+               }
+               if self.channel_monitor.get_min_seen_secret() != (1 << 48) ||
+                               self.cur_remote_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER ||
+                               self.cur_local_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
+                       panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
+               }
+
+               let funding_txo = OutPoint::new(msg.funding_txid, msg.funding_output_index);
+               let funding_txo_script = self.get_funding_redeemscript().to_v0_p2wsh();
+               self.channel_monitor.set_funding_info((funding_txo, funding_txo_script));
+
+               let (remote_initial_commitment_tx, local_initial_commitment_tx, our_signature, local_keys) = match self.funding_created_signature(&msg.signature) {
+                       Ok(res) => res,
+                       Err(e) => {
+                               self.channel_monitor.unset_funding_info();
+                               return Err(e);
+                       }
+               };
+
+               // Now that we're past error-generating stuff, update our local state:
+
+               self.channel_monitor.provide_latest_remote_commitment_tx_info(&remote_initial_commitment_tx, Vec::new(), self.cur_remote_commitment_transaction_number, self.their_cur_commitment_point.unwrap());
+               self.last_local_commitment_txn = vec![local_initial_commitment_tx.clone()];
+               self.channel_monitor.provide_latest_local_commitment_tx_info(local_initial_commitment_tx, local_keys, self.feerate_per_kw, Vec::new());
+               self.channel_state = ChannelState::FundingSent as u32;
+               self.channel_id = funding_txo.to_channel_id();
+               self.cur_remote_commitment_transaction_number -= 1;
+               self.cur_local_commitment_transaction_number -= 1;
+
+               Ok((msgs::FundingSigned {
+                       channel_id: self.channel_id,
+                       signature: our_signature
+               }, self.channel_monitor.clone()))
+       }
+
+       /// Handles a funding_signed message from the remote end.
+       /// If this call is successful, broadcast the funding transaction (and not before!)
+       pub fn funding_signed(&mut self, msg: &msgs::FundingSigned) -> Result<ChannelMonitor, ChannelError> {
+               if !self.channel_outbound {
+                       return Err(ChannelError::Close("Received funding_signed for an inbound channel?"));
+               }
+               if self.channel_state & !(ChannelState::MonitorUpdateFailed as u32) != ChannelState::FundingCreated as u32 {
+                       return Err(ChannelError::Close("Received funding_signed in strange state!"));
+               }
+               if self.channel_monitor.get_min_seen_secret() != (1 << 48) ||
+                               self.cur_remote_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER - 1 ||
+                               self.cur_local_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
+                       panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
+               }
+
+               let funding_script = self.get_funding_redeemscript();
+
+               let local_keys = self.build_local_transaction_keys(self.cur_local_commitment_transaction_number)?;
+               let mut local_initial_commitment_tx = self.build_commitment_transaction(self.cur_local_commitment_transaction_number, &local_keys, true, false, self.feerate_per_kw).0;
+               let local_sighash = hash_to_message!(&bip143::SighashComponents::new(&local_initial_commitment_tx).sighash_all(&local_initial_commitment_tx.input[0], &funding_script, self.channel_value_satoshis)[..]);
+
+               // They sign the "local" commitment transaction, allowing us to broadcast the tx if we wish.
+               secp_check!(self.secp_ctx.verify(&local_sighash, &msg.signature, &self.their_funding_pubkey.unwrap()), "Invalid funding_signed signature from peer");
+
+               self.sign_commitment_transaction(&mut local_initial_commitment_tx, &msg.signature);
+               self.channel_monitor.provide_latest_local_commitment_tx_info(local_initial_commitment_tx.clone(), local_keys, self.feerate_per_kw, Vec::new());
+               self.last_local_commitment_txn = vec![local_initial_commitment_tx];
+               self.channel_state = ChannelState::FundingSent as u32 | (self.channel_state & (ChannelState::MonitorUpdateFailed as u32));
+               self.cur_local_commitment_transaction_number -= 1;
+
+               if self.channel_state & (ChannelState::MonitorUpdateFailed as u32) == 0 {
+                       Ok(self.channel_monitor.clone())
+               } else {
+                       Err(ChannelError::Ignore("Previous monitor update failure prevented funding_signed from allowing funding broadcast"))
+               }
+       }
+
+       pub fn funding_locked(&mut self, msg: &msgs::FundingLocked) -> Result<(), ChannelError> {
+               if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
+                       return Err(ChannelError::Close("Peer sent funding_locked when we needed a channel_reestablish"));
+               }
+
+               let non_shutdown_state = self.channel_state & (!MULTI_STATE_FLAGS);
+
+               if non_shutdown_state == ChannelState::FundingSent as u32 {
+                       self.channel_state |= ChannelState::TheirFundingLocked as u32;
+               } else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::OurFundingLocked as u32) {
+                       self.channel_state = ChannelState::ChannelFunded as u32 | (self.channel_state & MULTI_STATE_FLAGS);
+                       self.channel_update_count += 1;
+               } else if (self.channel_state & (ChannelState::ChannelFunded as u32) != 0 &&
+                                // Note that funding_signed/funding_created will have decremented both by 1!
+                                self.cur_local_commitment_transaction_number == INITIAL_COMMITMENT_NUMBER - 1 &&
+                                self.cur_remote_commitment_transaction_number == INITIAL_COMMITMENT_NUMBER - 1) ||
+                               // If we reconnected before sending our funding locked they may still resend theirs:
+                               (self.channel_state & (ChannelState::FundingSent as u32 | ChannelState::TheirFundingLocked as u32) ==
+                                                     (ChannelState::FundingSent as u32 | ChannelState::TheirFundingLocked as u32)) {
+                       if self.their_cur_commitment_point != Some(msg.next_per_commitment_point) {
+                               return Err(ChannelError::Close("Peer sent a reconnect funding_locked with a different point"));
+                       }
+                       // They probably disconnected/reconnected and re-sent the funding_locked, which is required
+                       return Ok(());
+               } else {
+                       return Err(ChannelError::Close("Peer sent a funding_locked at a strange time"));
+               }
+
+               self.their_prev_commitment_point = self.their_cur_commitment_point;
+               self.their_cur_commitment_point = Some(msg.next_per_commitment_point);
+               Ok(())
+       }
+
+       /// Returns (inbound_htlc_count, htlc_inbound_value_msat)
+       fn get_inbound_pending_htlc_stats(&self) -> (u32, u64) {
+               let mut htlc_inbound_value_msat = 0;
+               for ref htlc in self.pending_inbound_htlcs.iter() {
+                       htlc_inbound_value_msat += htlc.amount_msat;
+               }
+               (self.pending_inbound_htlcs.len() as u32, htlc_inbound_value_msat)
+       }
+
+       /// Returns (outbound_htlc_count, htlc_outbound_value_msat) *including* pending adds in our
+       /// holding cell.
+       fn get_outbound_pending_htlc_stats(&self) -> (u32, u64) {
+               let mut htlc_outbound_value_msat = 0;
+               for ref htlc in self.pending_outbound_htlcs.iter() {
+                       htlc_outbound_value_msat += htlc.amount_msat;
+               }
+
+               let mut htlc_outbound_count = self.pending_outbound_htlcs.len();
+               for update in self.holding_cell_htlc_updates.iter() {
+                       if let &HTLCUpdateAwaitingACK::AddHTLC { ref amount_msat, .. } = update {
+                               htlc_outbound_count += 1;
+                               htlc_outbound_value_msat += amount_msat;
+                       }
+               }
+
+               (htlc_outbound_count as u32, htlc_outbound_value_msat)
+       }
+
+       /// Get the available (ie not including pending HTLCs) inbound and outbound balance in msat.
+       /// Doesn't bother handling the
+       /// if-we-removed-it-already-but-haven't-fully-resolved-they-can-still-send-an-inbound-HTLC
+       /// corner case properly.
+       pub fn get_inbound_outbound_available_balance_msat(&self) -> (u64, u64) {
+               // Note that we have to handle overflow due to the above case.
+               (cmp::min(self.channel_value_satoshis as i64 * 1000 - self.value_to_self_msat as i64 - self.get_inbound_pending_htlc_stats().1 as i64, 0) as u64,
+               cmp::min(self.value_to_self_msat as i64 - self.get_outbound_pending_htlc_stats().1 as i64, 0) as u64)
+       }
+
+       pub fn update_add_htlc(&mut self, msg: &msgs::UpdateAddHTLC, pending_forward_state: PendingHTLCStatus) -> Result<(), ChannelError> {
+               if (self.channel_state & (ChannelState::ChannelFunded as u32 | ChannelState::RemoteShutdownSent as u32)) != (ChannelState::ChannelFunded as u32) {
+                       return Err(ChannelError::Close("Got add HTLC message when channel was not in an operational state"));
+               }
+               if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
+                       return Err(ChannelError::Close("Peer sent update_add_htlc when we needed a channel_reestablish"));
+               }
+               if msg.amount_msat > self.channel_value_satoshis * 1000 {
+                       return Err(ChannelError::Close("Remote side tried to send more than the total value of the channel"));
+               }
+               if msg.amount_msat < self.our_htlc_minimum_msat {
+                       return Err(ChannelError::Close("Remote side tried to send less than our minimum HTLC value"));
+               }
+
+               let (inbound_htlc_count, htlc_inbound_value_msat) = self.get_inbound_pending_htlc_stats();
+               if inbound_htlc_count + 1 > OUR_MAX_HTLCS as u32 {
+                       return Err(ChannelError::Close("Remote tried to push more than our max accepted HTLCs"));
+               }
+               // Check our_max_htlc_value_in_flight_msat
+               if htlc_inbound_value_msat + msg.amount_msat > Channel::get_our_max_htlc_value_in_flight_msat(self.channel_value_satoshis) {
+                       return Err(ChannelError::Close("Remote HTLC add would put them over our max HTLC value"));
+               }
+               // Check our_channel_reserve_satoshis (we're getting paid, so they have to at least meet
+               // the reserve_satoshis we told them to always have as direct payment so that they lose
+               // something if we punish them for broadcasting an old state).
+               // Note that we don't really care about having a small/no to_remote output in our local
+               // commitment transactions, as the purpose of the channel reserve is to ensure we can
+               // punish *them* if they misbehave, so we discount any outbound HTLCs which will not be
+               // present in the next commitment transaction we send them (at least for fulfilled ones,
+               // failed ones won't modify value_to_self).
+               // Note that we will send HTLCs which another instance of rust-lightning would think
+               // violate the reserve value if we do not do this (as we forget inbound HTLCs from the
+               // Channel state once they will not be present in the next received commitment
+               // transaction).
+               let mut removed_outbound_total_msat = 0;
+               for ref htlc in self.pending_outbound_htlcs.iter() {
+                       if let OutboundHTLCState::AwaitingRemoteRevokeToRemove(None) = htlc.state {
+                               removed_outbound_total_msat += htlc.amount_msat;
+                       } else if let OutboundHTLCState::AwaitingRemovedRemoteRevoke(None) = htlc.state {
+                               removed_outbound_total_msat += htlc.amount_msat;
+                       }
+               }
+               if htlc_inbound_value_msat + msg.amount_msat + self.value_to_self_msat > (self.channel_value_satoshis - Channel::get_our_channel_reserve_satoshis(self.channel_value_satoshis)) * 1000 + removed_outbound_total_msat {
+                       return Err(ChannelError::Close("Remote HTLC add would put them over their reserve value"));
+               }
+               if self.next_remote_htlc_id != msg.htlc_id {
+                       return Err(ChannelError::Close("Remote skipped HTLC ID"));
+               }
+               if msg.cltv_expiry >= 500000000 {
+                       return Err(ChannelError::Close("Remote provided CLTV expiry in seconds instead of block height"));
+               }
+
+               //TODO: Check msg.cltv_expiry further? Do this in channel manager?
+
+               if self.channel_state & ChannelState::LocalShutdownSent as u32 != 0 {
+                       if let PendingHTLCStatus::Forward(_) = pending_forward_state {
+                               panic!("ChannelManager shouldn't be trying to add a forwardable HTLC after we've started closing");
+                       }
+               }
+
+               // Now update local state:
+               self.next_remote_htlc_id += 1;
+               self.pending_inbound_htlcs.push(InboundHTLCOutput {
+                       htlc_id: msg.htlc_id,
+                       amount_msat: msg.amount_msat,
+                       payment_hash: msg.payment_hash,
+                       cltv_expiry: msg.cltv_expiry,
+                       state: InboundHTLCState::RemoteAnnounced(pending_forward_state),
+               });
+               Ok(())
+       }
+
+       /// Marks an outbound HTLC which we have received update_fail/fulfill/malformed
+       #[inline]
+       fn mark_outbound_htlc_removed(&mut self, htlc_id: u64, check_preimage: Option<PaymentHash>, fail_reason: Option<HTLCFailReason>) -> Result<&HTLCSource, ChannelError> {
+               for htlc in self.pending_outbound_htlcs.iter_mut() {
+                       if htlc.htlc_id == htlc_id {
+                               match check_preimage {
+                                       None => {},
+                                       Some(payment_hash) =>
+                                               if payment_hash != htlc.payment_hash {
+                                                       return Err(ChannelError::Close("Remote tried to fulfill HTLC with an incorrect preimage"));
+                                               }
+                               };
+                               match htlc.state {
+                                       OutboundHTLCState::LocalAnnounced(_) =>
+                                               return Err(ChannelError::Close("Remote tried to fulfill/fail HTLC before it had been committed")),
+                                       OutboundHTLCState::Committed => {
+                                               htlc.state = OutboundHTLCState::RemoteRemoved(fail_reason);
+                                       },
+                                       OutboundHTLCState::AwaitingRemoteRevokeToRemove(_) | OutboundHTLCState::AwaitingRemovedRemoteRevoke(_) | OutboundHTLCState::RemoteRemoved(_) =>
+                                               return Err(ChannelError::Close("Remote tried to fulfill/fail HTLC that they'd already fulfilled/failed")),
+                               }
+                               return Ok(&htlc.source);
+                       }
+               }
+               Err(ChannelError::Close("Remote tried to fulfill/fail an HTLC we couldn't find"))
+       }
+
+       pub fn update_fulfill_htlc(&mut self, msg: &msgs::UpdateFulfillHTLC) -> Result<HTLCSource, ChannelError> {
+               if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
+                       return Err(ChannelError::Close("Got fulfill HTLC message when channel was not in an operational state"));
+               }
+               if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
+                       return Err(ChannelError::Close("Peer sent update_fulfill_htlc when we needed a channel_reestablish"));
+               }
+
+               let payment_hash = PaymentHash(Sha256::hash(&msg.payment_preimage.0[..]).into_inner());
+               self.mark_outbound_htlc_removed(msg.htlc_id, Some(payment_hash), None).map(|source| source.clone())
+       }
+
+       pub fn update_fail_htlc(&mut self, msg: &msgs::UpdateFailHTLC, fail_reason: HTLCFailReason) -> Result<(), ChannelError> {
+               if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
+                       return Err(ChannelError::Close("Got fail HTLC message when channel was not in an operational state"));
+               }
+               if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
+                       return Err(ChannelError::Close("Peer sent update_fail_htlc when we needed a channel_reestablish"));
+               }
+
+               self.mark_outbound_htlc_removed(msg.htlc_id, None, Some(fail_reason))?;
+               Ok(())
+       }
+
+       pub fn update_fail_malformed_htlc<'a>(&mut self, msg: &msgs::UpdateFailMalformedHTLC, fail_reason: HTLCFailReason) -> Result<(), ChannelError> {
+               if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
+                       return Err(ChannelError::Close("Got fail malformed HTLC message when channel was not in an operational state"));
+               }
+               if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
+                       return Err(ChannelError::Close("Peer sent update_fail_malformed_htlc when we needed a channel_reestablish"));
+               }
+
+               self.mark_outbound_htlc_removed(msg.htlc_id, None, Some(fail_reason))?;
+               Ok(())
+       }
+
+       pub fn commitment_signed(&mut self, msg: &msgs::CommitmentSigned, fee_estimator: &FeeEstimator) -> Result<(msgs::RevokeAndACK, Option<msgs::CommitmentSigned>, Option<msgs::ClosingSigned>, ChannelMonitor), ChannelError> {
+               if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
+                       return Err(ChannelError::Close("Got commitment signed message when channel was not in an operational state"));
+               }
+               if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
+                       return Err(ChannelError::Close("Peer sent commitment_signed when we needed a channel_reestablish"));
+               }
+               if self.channel_state & BOTH_SIDES_SHUTDOWN_MASK == BOTH_SIDES_SHUTDOWN_MASK && self.last_sent_closing_fee.is_some() {
+                       return Err(ChannelError::Close("Peer sent commitment_signed after we'd started exchanging closing_signeds"));
+               }
+
+               let funding_script = self.get_funding_redeemscript();
+
+               let local_keys = self.build_local_transaction_keys(self.cur_local_commitment_transaction_number)?;
+
+               let mut update_fee = false;
+               let feerate_per_kw = if !self.channel_outbound && self.pending_update_fee.is_some() {
+                       update_fee = true;
+                       self.pending_update_fee.unwrap()
+               } else {
+                       self.feerate_per_kw
+               };
+
+               let mut local_commitment_tx = {
+                       let mut commitment_tx = self.build_commitment_transaction(self.cur_local_commitment_transaction_number, &local_keys, true, false, feerate_per_kw);
+                       let htlcs_cloned: Vec<_> = commitment_tx.2.drain(..).map(|htlc| (htlc.0, htlc.1.map(|h| h.clone()))).collect();
+                       (commitment_tx.0, commitment_tx.1, htlcs_cloned)
+               };
+               let local_commitment_txid = local_commitment_tx.0.txid();
+               let local_sighash = hash_to_message!(&bip143::SighashComponents::new(&local_commitment_tx.0).sighash_all(&local_commitment_tx.0.input[0], &funding_script, self.channel_value_satoshis)[..]);
+               log_trace!(self, "Checking commitment tx signature {} by key {} against tx {} with redeemscript {}", log_bytes!(msg.signature.serialize_compact()[..]), log_bytes!(self.their_funding_pubkey.unwrap().serialize()), encode::serialize_hex(&local_commitment_tx.0), encode::serialize_hex(&funding_script));
+               secp_check!(self.secp_ctx.verify(&local_sighash, &msg.signature, &self.their_funding_pubkey.unwrap()), "Invalid commitment tx signature from peer");
+
+               //If channel fee was updated by funder confirm funder can afford the new fee rate when applied to the current local commitment transaction
+               if update_fee {
+                       let num_htlcs = local_commitment_tx.1;
+                       let total_fee: u64 = feerate_per_kw as u64 * (COMMITMENT_TX_BASE_WEIGHT + (num_htlcs as u64) * COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000;
+
+                       if self.channel_value_satoshis - self.value_to_self_msat / 1000 < total_fee + self.their_channel_reserve_satoshis {
+                               return Err(ChannelError::Close("Funding remote cannot afford proposed new fee"));
+                       }
+               }
+
+               if msg.htlc_signatures.len() != local_commitment_tx.1 {
+                       return Err(ChannelError::Close("Got wrong number of HTLC signatures from remote"));
+               }
+
+               let mut new_local_commitment_txn = Vec::with_capacity(local_commitment_tx.1 + 1);
+               self.sign_commitment_transaction(&mut local_commitment_tx.0, &msg.signature);
+               new_local_commitment_txn.push(local_commitment_tx.0.clone());
+
+               let mut htlcs_and_sigs = Vec::with_capacity(local_commitment_tx.2.len());
+               for (idx, (htlc, source)) in local_commitment_tx.2.drain(..).enumerate() {
+                       if let Some(_) = htlc.transaction_output_index {
+                               let mut htlc_tx = self.build_htlc_transaction(&local_commitment_txid, &htlc, true, &local_keys, feerate_per_kw);
+                               let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &local_keys);
+                               log_trace!(self, "Checking HTLC tx signature {} by key {} against tx {} with redeemscript {}", log_bytes!(msg.htlc_signatures[idx].serialize_compact()[..]), log_bytes!(local_keys.b_htlc_key.serialize()), encode::serialize_hex(&htlc_tx), encode::serialize_hex(&htlc_redeemscript));
+                               let htlc_sighash = hash_to_message!(&bip143::SighashComponents::new(&htlc_tx).sighash_all(&htlc_tx.input[0], &htlc_redeemscript, htlc.amount_msat / 1000)[..]);
+                               secp_check!(self.secp_ctx.verify(&htlc_sighash, &msg.htlc_signatures[idx], &local_keys.b_htlc_key), "Invalid HTLC tx signature from peer");
+                               let htlc_sig = if htlc.offered {
+                                       let htlc_sig = self.sign_htlc_transaction(&mut htlc_tx, &msg.htlc_signatures[idx], &None, &htlc, &local_keys)?;
+                                       new_local_commitment_txn.push(htlc_tx);
+                                       htlc_sig
+                               } else {
+                                       self.create_htlc_tx_signature(&htlc_tx, &htlc, &local_keys)?.1
+                               };
+                               htlcs_and_sigs.push((htlc, Some((msg.htlc_signatures[idx], htlc_sig)), source));
+                       } else {
+                               htlcs_and_sigs.push((htlc, None, source));
+                       }
+               }
+
+               let next_per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &self.build_local_commitment_secret(self.cur_local_commitment_transaction_number - 1));
+               let per_commitment_secret = chan_utils::build_commitment_secret(self.local_keys.commitment_seed, self.cur_local_commitment_transaction_number + 1);
+
+               // Update state now that we've passed all the can-fail calls...
+               let mut need_our_commitment = false;
+               if !self.channel_outbound {
+                       if let Some(fee_update) = self.pending_update_fee {
+                               self.feerate_per_kw = fee_update;
+                               // We later use the presence of pending_update_fee to indicate we should generate a
+                               // commitment_signed upon receipt of revoke_and_ack, so we can only set it to None
+                               // if we're not awaiting a revoke (ie will send a commitment_signed now).
+                               if (self.channel_state & ChannelState::AwaitingRemoteRevoke as u32) == 0 {
+                                       need_our_commitment = true;
+                                       self.pending_update_fee = None;
+                               }
+                       }
+               }
+
+               self.channel_monitor.provide_latest_local_commitment_tx_info(local_commitment_tx.0, local_keys, self.feerate_per_kw, htlcs_and_sigs);
+
+               for htlc in self.pending_inbound_htlcs.iter_mut() {
+                       let new_forward = if let &InboundHTLCState::RemoteAnnounced(ref forward_info) = &htlc.state {
+                               Some(forward_info.clone())
+                       } else { None };
+                       if let Some(forward_info) = new_forward {
+                               htlc.state = InboundHTLCState::AwaitingRemoteRevokeToAnnounce(forward_info);
+                               need_our_commitment = true;
+                       }
+               }
+               for htlc in self.pending_outbound_htlcs.iter_mut() {
+                       if let Some(fail_reason) = if let &mut OutboundHTLCState::RemoteRemoved(ref mut fail_reason) = &mut htlc.state {
+                               Some(fail_reason.take())
+                       } else { None } {
+                               htlc.state = OutboundHTLCState::AwaitingRemoteRevokeToRemove(fail_reason);
+                               need_our_commitment = true;
+                       }
+               }
+
+               self.cur_local_commitment_transaction_number -= 1;
+               self.last_local_commitment_txn = new_local_commitment_txn;
+               // Note that if we need_our_commitment & !AwaitingRemoteRevoke we'll call
+               // send_commitment_no_status_check() next which will reset this to RAAFirst.
+               self.resend_order = RAACommitmentOrder::CommitmentFirst;
+
+               if (self.channel_state & ChannelState::MonitorUpdateFailed as u32) != 0 {
+                       // In case we initially failed monitor updating without requiring a response, we need
+                       // to make sure the RAA gets sent first.
+                       self.monitor_pending_revoke_and_ack = true;
+                       if need_our_commitment && (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32)) == 0 {
+                               // If we were going to send a commitment_signed after the RAA, go ahead and do all
+                               // the corresponding HTLC status updates so that get_last_commitment_update
+                               // includes the right HTLCs.
+                               // Note that this generates a monitor update that we ignore! This is OK since we
+                               // won't actually send the commitment_signed that generated the update to the other
+                               // side until the latest monitor has been pulled from us and stored.
+                               self.monitor_pending_commitment_signed = true;
+                               self.send_commitment_no_status_check()?;
+                       }
+                       // TODO: Call maybe_propose_first_closing_signed on restoration (or call it here and
+                       // re-send the message on restoration)
+                       return Err(ChannelError::Ignore("Previous monitor update failure prevented generation of RAA"));
+               }
+
+               let (our_commitment_signed, monitor_update, closing_signed) = if need_our_commitment && (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32)) == 0 {
+                       // If we're AwaitingRemoteRevoke we can't send a new commitment here, but that's ok -
+                       // we'll send one right away when we get the revoke_and_ack when we
+                       // free_holding_cell_htlcs().
+                       let (msg, monitor) = self.send_commitment_no_status_check()?;
+                       (Some(msg), monitor, None)
+               } else if !need_our_commitment {
+                       (None, self.channel_monitor.clone(), self.maybe_propose_first_closing_signed(fee_estimator))
+               } else { (None, self.channel_monitor.clone(), None) };
+
+               Ok((msgs::RevokeAndACK {
+                       channel_id: self.channel_id,
+                       per_commitment_secret: per_commitment_secret,
+                       next_per_commitment_point: next_per_commitment_point,
+               }, our_commitment_signed, closing_signed, monitor_update))
+       }
+
+       /// Used to fulfill holding_cell_htlcs when we get a remote ack (or implicitly get it by them
+       /// fulfilling or failing the last pending HTLC)
+       fn free_holding_cell_htlcs(&mut self) -> Result<Option<(msgs::CommitmentUpdate, ChannelMonitor)>, ChannelError> {
+               assert_eq!(self.channel_state & ChannelState::MonitorUpdateFailed as u32, 0);
+               if self.holding_cell_htlc_updates.len() != 0 || self.holding_cell_update_fee.is_some() {
+                       log_trace!(self, "Freeing holding cell with {} HTLC updates{}", self.holding_cell_htlc_updates.len(), if self.holding_cell_update_fee.is_some() { " and a fee update" } else { "" });
+
+                       let mut htlc_updates = Vec::new();
+                       mem::swap(&mut htlc_updates, &mut self.holding_cell_htlc_updates);
+                       let mut update_add_htlcs = Vec::with_capacity(htlc_updates.len());
+                       let mut update_fulfill_htlcs = Vec::with_capacity(htlc_updates.len());
+                       let mut update_fail_htlcs = Vec::with_capacity(htlc_updates.len());
+                       let mut err = None;
+                       for htlc_update in htlc_updates.drain(..) {
+                               // Note that this *can* fail, though it should be due to rather-rare conditions on
+                               // fee races with adding too many outputs which push our total payments just over
+                               // the limit. In case it's less rare than I anticipate, we may want to revisit
+                               // handling this case better and maybe fulfilling some of the HTLCs while attempting
+                               // to rebalance channels.
+                               if err.is_some() { // We're back to AwaitingRemoteRevoke (or are about to fail the channel)
+                                       self.holding_cell_htlc_updates.push(htlc_update);
+                               } else {
+                                       match &htlc_update {
+                                               &HTLCUpdateAwaitingACK::AddHTLC {amount_msat, cltv_expiry, ref payment_hash, ref source, ref onion_routing_packet, ..} => {
+                                                       match self.send_htlc(amount_msat, *payment_hash, cltv_expiry, source.clone(), onion_routing_packet.clone()) {
+                                                               Ok(update_add_msg_option) => update_add_htlcs.push(update_add_msg_option.unwrap()),
+                                                               Err(e) => {
+                                                                       match e {
+                                                                               ChannelError::Ignore(ref msg) => {
+                                                                                       log_info!(self, "Failed to send HTLC with payment_hash {} due to {}", log_bytes!(payment_hash.0), msg);
+                                                                               },
+                                                                               _ => {
+                                                                                       log_info!(self, "Failed to send HTLC with payment_hash {} resulting in a channel closure during holding_cell freeing", log_bytes!(payment_hash.0));
+                                                                               },
+                                                                       }
+                                                                       err = Some(e);
+                                                               }
+                                                       }
+                                               },
+                                               &HTLCUpdateAwaitingACK::ClaimHTLC { ref payment_preimage, htlc_id, .. } => {
+                                                       match self.get_update_fulfill_htlc(htlc_id, *payment_preimage) {
+                                                               Ok(update_fulfill_msg_option) => update_fulfill_htlcs.push(update_fulfill_msg_option.0.unwrap()),
+                                                               Err(e) => {
+                                                                       if let ChannelError::Ignore(_) = e {}
+                                                                       else {
+                                                                               panic!("Got a non-IgnoreError action trying to fulfill holding cell HTLC");
+                                                                       }
+                                                               }
+                                                       }
+                                               },
+                                               &HTLCUpdateAwaitingACK::FailHTLC { htlc_id, ref err_packet } => {
+                                                       match self.get_update_fail_htlc(htlc_id, err_packet.clone()) {
+                                                               Ok(update_fail_msg_option) => update_fail_htlcs.push(update_fail_msg_option.unwrap()),
+                                                               Err(e) => {
+                                                                       if let ChannelError::Ignore(_) = e {}
+                                                                       else {
+                                                                               panic!("Got a non-IgnoreError action trying to fail holding cell HTLC");
+                                                                       }
+                                                               }
+                                                       }
+                                               },
+                                       }
+                                       if err.is_some() {
+                                               self.holding_cell_htlc_updates.push(htlc_update);
+                                               if let Some(ChannelError::Ignore(_)) = err {
+                                                       // If we failed to add the HTLC, but got an Ignore error, we should
+                                                       // still send the new commitment_signed, so reset the err to None.
+                                                       err = None;
+                                               }
+                                       }
+                               }
+                       }
+                       //TODO: Need to examine the type of err - if it's a fee issue or similar we may want to
+                       //fail it back the route, if it's a temporary issue we can ignore it...
+                       match err {
+                               None => {
+                                       if update_add_htlcs.is_empty() && update_fulfill_htlcs.is_empty() && update_fail_htlcs.is_empty() && self.holding_cell_update_fee.is_none() {
+                                               // This should never actually happen and indicates we got some Errs back
+                                               // from update_fulfill_htlc/update_fail_htlc, but we handle it anyway in
+                                               // case there is some strange way to hit duplicate HTLC removes.
+                                               return Ok(None);
+                                       }
+                                       let update_fee = if let Some(feerate) = self.holding_cell_update_fee {
+                                                       self.pending_update_fee = self.holding_cell_update_fee.take();
+                                                       Some(msgs::UpdateFee {
+                                                               channel_id: self.channel_id,
+                                                               feerate_per_kw: feerate as u32,
+                                                       })
+                                               } else {
+                                                       None
+                                               };
+                                       let (commitment_signed, monitor_update) = self.send_commitment_no_status_check()?;
+                                       Ok(Some((msgs::CommitmentUpdate {
+                                               update_add_htlcs,
+                                               update_fulfill_htlcs,
+                                               update_fail_htlcs,
+                                               update_fail_malformed_htlcs: Vec::new(),
+                                               update_fee: update_fee,
+                                               commitment_signed,
+                                       }, monitor_update)))
+                               },
+                               Some(e) => Err(e)
+                       }
+               } else {
+                       Ok(None)
+               }
+       }
+
+       /// Handles receiving a remote's revoke_and_ack. Note that we may return a new
+       /// commitment_signed message here in case we had pending outbound HTLCs to add which were
+       /// waiting on this revoke_and_ack. The generation of this new commitment_signed may also fail,
+       /// generating an appropriate error *after* the channel state has been updated based on the
+       /// revoke_and_ack message.
+       pub fn revoke_and_ack(&mut self, msg: &msgs::RevokeAndACK, fee_estimator: &FeeEstimator) -> Result<(Option<msgs::CommitmentUpdate>, Vec<(PendingForwardHTLCInfo, u64)>, Vec<(HTLCSource, PaymentHash, HTLCFailReason)>, Option<msgs::ClosingSigned>, ChannelMonitor), ChannelError> {
+               if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
+                       return Err(ChannelError::Close("Got revoke/ACK message when channel was not in an operational state"));
+               }
+               if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
+                       return Err(ChannelError::Close("Peer sent revoke_and_ack when we needed a channel_reestablish"));
+               }
+               if self.channel_state & BOTH_SIDES_SHUTDOWN_MASK == BOTH_SIDES_SHUTDOWN_MASK && self.last_sent_closing_fee.is_some() {
+                       return Err(ChannelError::Close("Peer sent revoke_and_ack after we'd started exchanging closing_signeds"));
+               }
+
+               if let Some(their_prev_commitment_point) = self.their_prev_commitment_point {
+                       if PublicKey::from_secret_key(&self.secp_ctx, &secp_check!(SecretKey::from_slice(&msg.per_commitment_secret), "Peer provided an invalid per_commitment_secret")) != their_prev_commitment_point {
+                               return Err(ChannelError::Close("Got a revoke commitment secret which didn't correspond to their current pubkey"));
+                       }
+               }
+               self.channel_monitor.provide_secret(self.cur_remote_commitment_transaction_number + 1, msg.per_commitment_secret)
+                       .map_err(|e| ChannelError::Close(e.0))?;
+
+               // Update state now that we've passed all the can-fail calls...
+               // (note that we may still fail to generate the new commitment_signed message, but that's
+               // OK, we step the channel here and *then* if the new generation fails we can fail the
+               // channel based on that, but stepping stuff here should be safe either way.
+               self.channel_state &= !(ChannelState::AwaitingRemoteRevoke as u32);
+               self.their_prev_commitment_point = self.their_cur_commitment_point;
+               self.their_cur_commitment_point = Some(msg.next_per_commitment_point);
+               self.cur_remote_commitment_transaction_number -= 1;
+
+               log_trace!(self, "Updating HTLCs on receipt of RAA...");
+               let mut to_forward_infos = Vec::new();
+               let mut revoked_htlcs = Vec::new();
+               let mut update_fail_htlcs = Vec::new();
+               let mut update_fail_malformed_htlcs = Vec::new();
+               let mut require_commitment = false;
+               let mut value_to_self_msat_diff: i64 = 0;
+
+               {
+                       // Take references explicitly so that we can hold multiple references to self.
+                       let pending_inbound_htlcs: &mut Vec<_> = &mut self.pending_inbound_htlcs;
+                       let pending_outbound_htlcs: &mut Vec<_> = &mut self.pending_outbound_htlcs;
+                       let logger = LogHolder { logger: &self.logger };
+
+                       // We really shouldnt have two passes here, but retain gives a non-mutable ref (Rust bug)
+                       pending_inbound_htlcs.retain(|htlc| {
+                               if let &InboundHTLCState::LocalRemoved(ref reason) = &htlc.state {
+                                       log_trace!(logger, " ...removing inbound LocalRemoved {}", log_bytes!(htlc.payment_hash.0));
+                                       if let &InboundHTLCRemovalReason::Fulfill(_) = reason {
+                                               value_to_self_msat_diff += htlc.amount_msat as i64;
+                                       }
+                                       false
+                               } else { true }
+                       });
+                       pending_outbound_htlcs.retain(|htlc| {
+                               if let &OutboundHTLCState::AwaitingRemovedRemoteRevoke(ref fail_reason) = &htlc.state {
+                                       log_trace!(logger, " ...removing outbound AwaitingRemovedRemoteRevoke {}", log_bytes!(htlc.payment_hash.0));
+                                       if let Some(reason) = fail_reason.clone() { // We really want take() here, but, again, non-mut ref :(
+                                               revoked_htlcs.push((htlc.source.clone(), htlc.payment_hash, reason));
+                                       } else {
+                                               // They fulfilled, so we sent them money
+                                               value_to_self_msat_diff -= htlc.amount_msat as i64;
+                                       }
+                                       false
+                               } else { true }
+                       });
+                       for htlc in pending_inbound_htlcs.iter_mut() {
+                               let swap = if let &InboundHTLCState::AwaitingRemoteRevokeToAnnounce(_) = &htlc.state {
+                                       log_trace!(logger, " ...promoting inbound AwaitingRemoteRevokeToAnnounce {} to Committed", log_bytes!(htlc.payment_hash.0));
+                                       true
+                               } else if let &InboundHTLCState::AwaitingAnnouncedRemoteRevoke(_) = &htlc.state {
+                                       log_trace!(logger, " ...promoting inbound AwaitingAnnouncedRemoteRevoke {} to Committed", log_bytes!(htlc.payment_hash.0));
+                                       true
+                               } else { false };
+                               if swap {
+                                       let mut state = InboundHTLCState::Committed;
+                                       mem::swap(&mut state, &mut htlc.state);
+
+                                       if let InboundHTLCState::AwaitingRemoteRevokeToAnnounce(forward_info) = state {
+                                               htlc.state = InboundHTLCState::AwaitingAnnouncedRemoteRevoke(forward_info);
+                                               require_commitment = true;
+                                       } else if let InboundHTLCState::AwaitingAnnouncedRemoteRevoke(forward_info) = state {
+                                               match forward_info {
+                                                       PendingHTLCStatus::Fail(fail_msg) => {
+                                                               require_commitment = true;
+                                                               match fail_msg {
+                                                                       HTLCFailureMsg::Relay(msg) => {
+                                                                               htlc.state = InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::FailRelay(msg.reason.clone()));
+                                                                               update_fail_htlcs.push(msg)
+                                                                       },
+                                                                       HTLCFailureMsg::Malformed(msg) => {
+                                                                               htlc.state = InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::FailMalformed((msg.sha256_of_onion, msg.failure_code)));
+                                                                               update_fail_malformed_htlcs.push(msg)
+                                                                       },
+                                                               }
+                                                       },
+                                                       PendingHTLCStatus::Forward(forward_info) => {
+                                                               to_forward_infos.push((forward_info, htlc.htlc_id));
+                                                               htlc.state = InboundHTLCState::Committed;
+                                                       }
+                                               }
+                                       }
+                               }
+                       }
+                       for htlc in pending_outbound_htlcs.iter_mut() {
+                               if let OutboundHTLCState::LocalAnnounced(_) = htlc.state {
+                                       log_trace!(logger, " ...promoting outbound LocalAnnounced {} to Committed", log_bytes!(htlc.payment_hash.0));
+                                       htlc.state = OutboundHTLCState::Committed;
+                               }
+                               if let Some(fail_reason) = if let &mut OutboundHTLCState::AwaitingRemoteRevokeToRemove(ref mut fail_reason) = &mut htlc.state {
+                                       Some(fail_reason.take())
+                               } else { None } {
+                                       log_trace!(logger, " ...promoting outbound AwaitingRemoteRevokeToRemove {} to AwaitingRemovedRemoteRevoke", log_bytes!(htlc.payment_hash.0));
+                                       htlc.state = OutboundHTLCState::AwaitingRemovedRemoteRevoke(fail_reason);
+                                       require_commitment = true;
+                               }
+                       }
+               }
+               self.value_to_self_msat = (self.value_to_self_msat as i64 + value_to_self_msat_diff) as u64;
+
+               if self.channel_outbound {
+                       if let Some(feerate) = self.pending_update_fee.take() {
+                               self.feerate_per_kw = feerate;
+                       }
+               } else {
+                       if let Some(feerate) = self.pending_update_fee {
+                               // Because a node cannot send two commitment_signeds in a row without getting a
+                               // revoke_and_ack from us (as it would otherwise not know the per_commitment_point
+                               // it should use to create keys with) and because a node can't send a
+                               // commitment_signed without changes, checking if the feerate is equal to the
+                               // pending feerate update is sufficient to detect require_commitment.
+                               if feerate == self.feerate_per_kw {
+                                       require_commitment = true;
+                                       self.pending_update_fee = None;
+                               }
+                       }
+               }
+
+               if (self.channel_state & ChannelState::MonitorUpdateFailed as u32) == ChannelState::MonitorUpdateFailed as u32 {
+                       // We can't actually generate a new commitment transaction (incl by freeing holding
+                       // cells) while we can't update the monitor, so we just return what we have.
+                       if require_commitment {
+                               self.monitor_pending_commitment_signed = true;
+                               // When the monitor updating is restored we'll call get_last_commitment_update(),
+                               // which does not update state, but we're definitely now awaiting a remote revoke
+                               // before we can step forward any more, so set it here.
+                               self.send_commitment_no_status_check()?;
+                       }
+                       self.monitor_pending_forwards.append(&mut to_forward_infos);
+                       self.monitor_pending_failures.append(&mut revoked_htlcs);
+                       return Ok((None, Vec::new(), Vec::new(), None, self.channel_monitor.clone()));
+               }
+
+               match self.free_holding_cell_htlcs()? {
+                       Some(mut commitment_update) => {
+                               commitment_update.0.update_fail_htlcs.reserve(update_fail_htlcs.len());
+                               for fail_msg in update_fail_htlcs.drain(..) {
+                                       commitment_update.0.update_fail_htlcs.push(fail_msg);
+                               }
+                               commitment_update.0.update_fail_malformed_htlcs.reserve(update_fail_malformed_htlcs.len());
+                               for fail_msg in update_fail_malformed_htlcs.drain(..) {
+                                       commitment_update.0.update_fail_malformed_htlcs.push(fail_msg);
+                               }
+                               Ok((Some(commitment_update.0), to_forward_infos, revoked_htlcs, None, commitment_update.1))
+                       },
+                       None => {
+                               if require_commitment {
+                                       let (commitment_signed, monitor_update) = self.send_commitment_no_status_check()?;
+                                       Ok((Some(msgs::CommitmentUpdate {
+                                               update_add_htlcs: Vec::new(),
+                                               update_fulfill_htlcs: Vec::new(),
+                                               update_fail_htlcs,
+                                               update_fail_malformed_htlcs,
+                                               update_fee: None,
+                                               commitment_signed
+                                       }), to_forward_infos, revoked_htlcs, None, monitor_update))
+                               } else {
+                                       Ok((None, to_forward_infos, revoked_htlcs, self.maybe_propose_first_closing_signed(fee_estimator), self.channel_monitor.clone()))
+                               }
+                       }
+               }
+
+       }
+
+       /// Adds a pending update to this channel. See the doc for send_htlc for
+       /// further details on the optionness of the return value.
+       /// You MUST call send_commitment prior to any other calls on this Channel
+       fn send_update_fee(&mut self, feerate_per_kw: u64) -> Option<msgs::UpdateFee> {
+               if !self.channel_outbound {
+                       panic!("Cannot send fee from inbound channel");
+               }
+               if !self.is_usable() {
+                       panic!("Cannot update fee until channel is fully established and we haven't started shutting down");
+               }
+               if !self.is_live() {
+                       panic!("Cannot update fee while peer is disconnected/we're awaiting a monitor update (ChannelManager should have caught this)");
+               }
+
+               if (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32)) == (ChannelState::AwaitingRemoteRevoke as u32) {
+                       self.holding_cell_update_fee = Some(feerate_per_kw);
+                       return None;
+               }
+
+               debug_assert!(self.pending_update_fee.is_none());
+               self.pending_update_fee = Some(feerate_per_kw);
+
+               Some(msgs::UpdateFee {
+                       channel_id: self.channel_id,
+                       feerate_per_kw: feerate_per_kw as u32,
+               })
+       }
+
+       pub fn send_update_fee_and_commit(&mut self, feerate_per_kw: u64) -> Result<Option<(msgs::UpdateFee, msgs::CommitmentSigned, ChannelMonitor)>, ChannelError> {
+               match self.send_update_fee(feerate_per_kw) {
+                       Some(update_fee) => {
+                               let (commitment_signed, monitor_update) = self.send_commitment_no_status_check()?;
+                               Ok(Some((update_fee, commitment_signed, monitor_update)))
+                       },
+                       None => Ok(None)
+               }
+       }
+
+       /// Removes any uncommitted HTLCs, to be used on peer disconnection, including any pending
+       /// HTLCs that we intended to add but haven't as we were waiting on a remote revoke.
+       /// Returns the set of PendingHTLCStatuses from remote uncommitted HTLCs (which we're
+       /// implicitly dropping) and the payment_hashes of HTLCs we tried to add but are dropping.
+       /// No further message handling calls may be made until a channel_reestablish dance has
+       /// completed.
+       pub fn remove_uncommitted_htlcs_and_mark_paused(&mut self) -> Vec<(HTLCSource, PaymentHash)> {
+               let mut outbound_drops = Vec::new();
+
+               assert_eq!(self.channel_state & ChannelState::ShutdownComplete as u32, 0);
+               if self.channel_state < ChannelState::FundingSent as u32 {
+                       self.channel_state = ChannelState::ShutdownComplete as u32;
+                       return outbound_drops;
+               }
+               // Upon reconnect we have to start the closing_signed dance over, but shutdown messages
+               // will be retransmitted.
+               self.last_sent_closing_fee = None;
+
+               let mut inbound_drop_count = 0;
+               self.pending_inbound_htlcs.retain(|htlc| {
+                       match htlc.state {
+                               InboundHTLCState::RemoteAnnounced(_) => {
+                                       // They sent us an update_add_htlc but we never got the commitment_signed.
+                                       // We'll tell them what commitment_signed we're expecting next and they'll drop
+                                       // this HTLC accordingly
+                                       inbound_drop_count += 1;
+                                       false
+                               },
+                               InboundHTLCState::AwaitingRemoteRevokeToAnnounce(_)|InboundHTLCState::AwaitingAnnouncedRemoteRevoke(_) => {
+                                       // We received a commitment_signed updating this HTLC and (at least hopefully)
+                                       // sent a revoke_and_ack (which we can re-transmit) and have heard nothing
+                                       // in response to it yet, so don't touch it.
+                                       true
+                               },
+                               InboundHTLCState::Committed => true,
+                               InboundHTLCState::LocalRemoved(_) => {
+                                       // We (hopefully) sent a commitment_signed updating this HTLC (which we can
+                                       // re-transmit if needed) and they may have even sent a revoke_and_ack back
+                                       // (that we missed). Keep this around for now and if they tell us they missed
+                                       // the commitment_signed we can re-transmit the update then.
+                                       true
+                               },
+                       }
+               });
+               self.next_remote_htlc_id -= inbound_drop_count;
+
+               for htlc in self.pending_outbound_htlcs.iter_mut() {
+                       if let OutboundHTLCState::RemoteRemoved(_) = htlc.state {
+                               // They sent us an update to remove this but haven't yet sent the corresponding
+                               // commitment_signed, we need to move it back to Committed and they can re-send
+                               // the update upon reconnection.
+                               htlc.state = OutboundHTLCState::Committed;
+                       }
+               }
+
+               self.holding_cell_htlc_updates.retain(|htlc_update| {
+                       match htlc_update {
+                               &HTLCUpdateAwaitingACK::AddHTLC { ref payment_hash, ref source, .. } => {
+                                       outbound_drops.push((source.clone(), payment_hash.clone()));
+                                       false
+                               },
+                               &HTLCUpdateAwaitingACK::ClaimHTLC {..} | &HTLCUpdateAwaitingACK::FailHTLC {..} => true,
+                       }
+               });
+               self.channel_state |= ChannelState::PeerDisconnected as u32;
+               log_debug!(self, "Peer disconnection resulted in {} remote-announced HTLC drops and {} waiting-to-locally-announced HTLC drops on channel {}", outbound_drops.len(), inbound_drop_count, log_bytes!(self.channel_id()));
+               outbound_drops
+       }
+
+       /// Indicates that a ChannelMonitor update failed to be stored by the client and further
+       /// updates are partially paused.
+       /// This must be called immediately after the call which generated the ChannelMonitor update
+       /// which failed. The messages which were generated from that call which generated the
+       /// monitor update failure must *not* have been sent to the remote end, and must instead
+       /// have been dropped. They will be regenerated when monitor_updating_restored is called.
+       pub fn monitor_update_failed(&mut self, resend_raa: bool, resend_commitment: bool, mut pending_forwards: Vec<(PendingForwardHTLCInfo, u64)>, mut pending_fails: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>) {
+               assert_eq!(self.channel_state & ChannelState::MonitorUpdateFailed as u32, 0);
+               self.monitor_pending_revoke_and_ack = resend_raa;
+               self.monitor_pending_commitment_signed = resend_commitment;
+               assert!(self.monitor_pending_forwards.is_empty());
+               mem::swap(&mut pending_forwards, &mut self.monitor_pending_forwards);
+               assert!(self.monitor_pending_failures.is_empty());
+               mem::swap(&mut pending_fails, &mut self.monitor_pending_failures);
+               self.channel_state |= ChannelState::MonitorUpdateFailed as u32;
+       }
+
+       /// Indicates that the latest ChannelMonitor update has been committed by the client
+       /// successfully and we should restore normal operation. Returns messages which should be sent
+       /// to the remote side.
+       pub fn monitor_updating_restored(&mut self) -> (Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>, RAACommitmentOrder, Vec<(PendingForwardHTLCInfo, u64)>, Vec<(HTLCSource, PaymentHash, HTLCFailReason)>, bool, Option<msgs::FundingLocked>) {
+               assert_eq!(self.channel_state & ChannelState::MonitorUpdateFailed as u32, ChannelState::MonitorUpdateFailed as u32);
+               self.channel_state &= !(ChannelState::MonitorUpdateFailed as u32);
+
+               let needs_broadcast_safe = self.channel_state & (ChannelState::FundingSent as u32) != 0 && self.channel_outbound;
+
+               // Because we will never generate a FundingBroadcastSafe event when we're in
+               // MonitorUpdateFailed, if we assume the user only broadcast the funding transaction when
+               // they received the FundingBroadcastSafe event, we can only ever hit
+               // monitor_pending_funding_locked when we're an inbound channel which failed to persist the
+               // monitor on funding_created, and we even got the funding transaction confirmed before the
+               // monitor was persisted.
+               let funding_locked = if self.monitor_pending_funding_locked {
+                       assert!(!self.channel_outbound, "Funding transaction broadcast without FundingBroadcastSafe!");
+                       self.monitor_pending_funding_locked = false;
+                       let next_per_commitment_secret = self.build_local_commitment_secret(self.cur_local_commitment_transaction_number);
+                       let next_per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &next_per_commitment_secret);
+                       Some(msgs::FundingLocked {
+                               channel_id: self.channel_id(),
+                               next_per_commitment_point: next_per_commitment_point,
+                       })
+               } else { None };
+
+               let mut forwards = Vec::new();
+               mem::swap(&mut forwards, &mut self.monitor_pending_forwards);
+               let mut failures = Vec::new();
+               mem::swap(&mut failures, &mut self.monitor_pending_failures);
+
+               if self.channel_state & (ChannelState::PeerDisconnected as u32) != 0 {
+                       self.monitor_pending_revoke_and_ack = false;
+                       self.monitor_pending_commitment_signed = false;
+                       return (None, None, RAACommitmentOrder::RevokeAndACKFirst, forwards, failures, needs_broadcast_safe, funding_locked);
+               }
+
+               let raa = if self.monitor_pending_revoke_and_ack {
+                       Some(self.get_last_revoke_and_ack())
+               } else { None };
+               let commitment_update = if self.monitor_pending_commitment_signed {
+                       Some(self.get_last_commitment_update())
+               } else { None };
+
+               self.monitor_pending_revoke_and_ack = false;
+               self.monitor_pending_commitment_signed = false;
+               let order = self.resend_order.clone();
+               log_trace!(self, "Restored monitor updating resulting in {}{} commitment update and {} RAA, with {} first",
+                       if needs_broadcast_safe { "a funding broadcast safe, " } else { "" },
+                       if commitment_update.is_some() { "a" } else { "no" },
+                       if raa.is_some() { "an" } else { "no" },
+                       match order { RAACommitmentOrder::CommitmentFirst => "commitment", RAACommitmentOrder::RevokeAndACKFirst => "RAA"});
+               (raa, commitment_update, order, forwards, failures, needs_broadcast_safe, funding_locked)
+       }
+
+       pub fn update_fee(&mut self, fee_estimator: &FeeEstimator, msg: &msgs::UpdateFee) -> Result<(), ChannelError> {
+               if self.channel_outbound {
+                       return Err(ChannelError::Close("Non-funding remote tried to update channel fee"));
+               }
+               if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
+                       return Err(ChannelError::Close("Peer sent update_fee when we needed a channel_reestablish"));
+               }
+               Channel::check_remote_fee(fee_estimator, msg.feerate_per_kw)?;
+               self.pending_update_fee = Some(msg.feerate_per_kw as u64);
+               self.channel_update_count += 1;
+               Ok(())
+       }
+
+       fn get_last_revoke_and_ack(&self) -> msgs::RevokeAndACK {
+               let next_per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &self.build_local_commitment_secret(self.cur_local_commitment_transaction_number));
+               let per_commitment_secret = chan_utils::build_commitment_secret(self.local_keys.commitment_seed, self.cur_local_commitment_transaction_number + 2);
+               msgs::RevokeAndACK {
+                       channel_id: self.channel_id,
+                       per_commitment_secret,
+                       next_per_commitment_point,
+               }
+       }
+
+       fn get_last_commitment_update(&self) -> msgs::CommitmentUpdate {
+               let mut update_add_htlcs = Vec::new();
+               let mut update_fulfill_htlcs = Vec::new();
+               let mut update_fail_htlcs = Vec::new();
+               let mut update_fail_malformed_htlcs = Vec::new();
+
+               for htlc in self.pending_outbound_htlcs.iter() {
+                       if let &OutboundHTLCState::LocalAnnounced(ref onion_packet) = &htlc.state {
+                               update_add_htlcs.push(msgs::UpdateAddHTLC {
+                                       channel_id: self.channel_id(),
+                                       htlc_id: htlc.htlc_id,
+                                       amount_msat: htlc.amount_msat,
+                                       payment_hash: htlc.payment_hash,
+                                       cltv_expiry: htlc.cltv_expiry,
+                                       onion_routing_packet: (**onion_packet).clone(),
+                               });
+                       }
+               }
+
+               for htlc in self.pending_inbound_htlcs.iter() {
+                       if let &InboundHTLCState::LocalRemoved(ref reason) = &htlc.state {
+                               match reason {
+                                       &InboundHTLCRemovalReason::FailRelay(ref err_packet) => {
+                                               update_fail_htlcs.push(msgs::UpdateFailHTLC {
+                                                       channel_id: self.channel_id(),
+                                                       htlc_id: htlc.htlc_id,
+                                                       reason: err_packet.clone()
+                                               });
+                                       },
+                                       &InboundHTLCRemovalReason::FailMalformed((ref sha256_of_onion, ref failure_code)) => {
+                                               update_fail_malformed_htlcs.push(msgs::UpdateFailMalformedHTLC {
+                                                       channel_id: self.channel_id(),
+                                                       htlc_id: htlc.htlc_id,
+                                                       sha256_of_onion: sha256_of_onion.clone(),
+                                                       failure_code: failure_code.clone(),
+                                               });
+                                       },
+                                       &InboundHTLCRemovalReason::Fulfill(ref payment_preimage) => {
+                                               update_fulfill_htlcs.push(msgs::UpdateFulfillHTLC {
+                                                       channel_id: self.channel_id(),
+                                                       htlc_id: htlc.htlc_id,
+                                                       payment_preimage: payment_preimage.clone(),
+                                               });
+                                       },
+                               }
+                       }
+               }
+
+               log_trace!(self, "Regenerated latest commitment update with {} update_adds, {} update_fulfills, {} update_fails, and {} update_fail_malformeds",
+                               update_add_htlcs.len(), update_fulfill_htlcs.len(), update_fail_htlcs.len(), update_fail_malformed_htlcs.len());
+               msgs::CommitmentUpdate {
+                       update_add_htlcs, update_fulfill_htlcs, update_fail_htlcs, update_fail_malformed_htlcs,
+                       update_fee: None,
+                       commitment_signed: self.send_commitment_no_state_update().expect("It looks like we failed to re-generate a commitment_signed we had previously sent?").0,
+               }
+       }
+
+       /// May panic if some calls other than message-handling calls (which will all Err immediately)
+       /// have been called between remove_uncommitted_htlcs_and_mark_paused and this call.
+       pub fn channel_reestablish(&mut self, msg: &msgs::ChannelReestablish) -> Result<(Option<msgs::FundingLocked>, Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>, Option<ChannelMonitor>, RAACommitmentOrder, Option<msgs::Shutdown>), ChannelError> {
+               if self.channel_state & (ChannelState::PeerDisconnected as u32) == 0 {
+                       // While BOLT 2 doesn't indicate explicitly we should error this channel here, it
+                       // almost certainly indicates we are going to end up out-of-sync in some way, so we
+                       // just close here instead of trying to recover.
+                       return Err(ChannelError::Close("Peer sent a loose channel_reestablish not after reconnect"));
+               }
+
+               if msg.next_local_commitment_number >= INITIAL_COMMITMENT_NUMBER || msg.next_remote_commitment_number >= INITIAL_COMMITMENT_NUMBER ||
+                       msg.next_local_commitment_number == 0 {
+                       return Err(ChannelError::Close("Peer sent a garbage channel_reestablish"));
+               }
+
+               if msg.next_remote_commitment_number > 0 {
+                       match msg.data_loss_protect {
+                               OptionalField::Present(ref data_loss) => {
+                                       if chan_utils::build_commitment_secret(self.local_keys.commitment_seed, INITIAL_COMMITMENT_NUMBER - msg.next_remote_commitment_number + 1) != data_loss.your_last_per_commitment_secret {
+                                               return Err(ChannelError::Close("Peer sent a garbage channel_reestablish with secret key not matching the commitment height provided"));
+                                       }
+                                       if msg.next_remote_commitment_number > INITIAL_COMMITMENT_NUMBER - self.cur_local_commitment_transaction_number {
+                                               self.channel_monitor.provide_rescue_remote_commitment_tx_info(data_loss.my_current_per_commitment_point);
+                                               return Err(ChannelError::CloseDelayBroadcast { msg: "We have fallen behind - we have received proof that if we broadcast remote is going to claim our funds - we can't do any automated broadcasting", update: Some(self.channel_monitor.clone())
+                                       });
+                                       }
+                               },
+                               OptionalField::Absent => {}
+                       }
+               }
+
+               // Go ahead and unmark PeerDisconnected as various calls we may make check for it (and all
+               // remaining cases either succeed or ErrorMessage-fail).
+               self.channel_state &= !(ChannelState::PeerDisconnected as u32);
+
+               let shutdown_msg = if self.channel_state & (ChannelState::LocalShutdownSent as u32) != 0 {
+                       Some(msgs::Shutdown {
+                               channel_id: self.channel_id,
+                               scriptpubkey: self.get_closing_scriptpubkey(),
+                       })
+               } else { None };
+
+               if self.channel_state & (ChannelState::FundingSent as u32) == ChannelState::FundingSent as u32 {
+                       // If we're waiting on a monitor update, we shouldn't re-send any funding_locked's.
+                       if self.channel_state & (ChannelState::OurFundingLocked as u32) == 0 ||
+                                       self.channel_state & (ChannelState::MonitorUpdateFailed as u32) != 0 {
+                               if msg.next_remote_commitment_number != 0 {
+                                       return Err(ChannelError::Close("Peer claimed they saw a revoke_and_ack but we haven't sent funding_locked yet"));
+                               }
+                               // Short circuit the whole handler as there is nothing we can resend them
+                               return Ok((None, None, None, None, RAACommitmentOrder::CommitmentFirst, shutdown_msg));
+                       }
+
+                       // We have OurFundingLocked set!
+                       let next_per_commitment_secret = self.build_local_commitment_secret(self.cur_local_commitment_transaction_number);
+                       let next_per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &next_per_commitment_secret);
+                       return Ok((Some(msgs::FundingLocked {
+                               channel_id: self.channel_id(),
+                               next_per_commitment_point: next_per_commitment_point,
+                       }), None, None, None, RAACommitmentOrder::CommitmentFirst, shutdown_msg));
+               }
+
+               let required_revoke = if msg.next_remote_commitment_number + 1 == INITIAL_COMMITMENT_NUMBER - self.cur_local_commitment_transaction_number {
+                       // Remote isn't waiting on any RevokeAndACK from us!
+                       // Note that if we need to repeat our FundingLocked we'll do that in the next if block.
+                       None
+               } else if msg.next_remote_commitment_number + 1 == (INITIAL_COMMITMENT_NUMBER - 1) - self.cur_local_commitment_transaction_number {
+                       if self.channel_state & (ChannelState::MonitorUpdateFailed as u32) != 0 {
+                               self.monitor_pending_revoke_and_ack = true;
+                               None
+                       } else {
+                               Some(self.get_last_revoke_and_ack())
+                       }
+               } else {
+                       return Err(ChannelError::Close("Peer attempted to reestablish channel with a very old local commitment transaction"));
+               };
+
+               // We increment cur_remote_commitment_transaction_number only upon receipt of
+               // revoke_and_ack, not on sending commitment_signed, so we add one if have
+               // AwaitingRemoteRevoke set, which indicates we sent a commitment_signed but haven't gotten
+               // the corresponding revoke_and_ack back yet.
+               let our_next_remote_commitment_number = INITIAL_COMMITMENT_NUMBER - self.cur_remote_commitment_transaction_number + if (self.channel_state & ChannelState::AwaitingRemoteRevoke as u32) != 0 { 1 } else { 0 };
+
+               let resend_funding_locked = if msg.next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self.cur_local_commitment_transaction_number == 1 {
+                       // We should never have to worry about MonitorUpdateFailed resending FundingLocked
+                       let next_per_commitment_secret = self.build_local_commitment_secret(self.cur_local_commitment_transaction_number);
+                       let next_per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &next_per_commitment_secret);
+                       Some(msgs::FundingLocked {
+                               channel_id: self.channel_id(),
+                               next_per_commitment_point: next_per_commitment_point,
+                       })
+               } else { None };
+
+               if msg.next_local_commitment_number == our_next_remote_commitment_number {
+                       if required_revoke.is_some() {
+                               log_debug!(self, "Reconnected channel {} with only lost outbound RAA", log_bytes!(self.channel_id()));
+                       } else {
+                               log_debug!(self, "Reconnected channel {} with no loss", log_bytes!(self.channel_id()));
+                       }
+
+                       if (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32 | ChannelState::MonitorUpdateFailed as u32)) == 0 {
+                               // We're up-to-date and not waiting on a remote revoke (if we are our
+                               // channel_reestablish should result in them sending a revoke_and_ack), but we may
+                               // have received some updates while we were disconnected. Free the holding cell
+                               // now!
+                               match self.free_holding_cell_htlcs() {
+                                       Err(ChannelError::Close(msg)) => return Err(ChannelError::Close(msg)),
+                                       Err(ChannelError::Ignore(_)) | Err(ChannelError::CloseDelayBroadcast { .. }) => panic!("Got non-channel-failing result from free_holding_cell_htlcs"),
+                                       Ok(Some((commitment_update, channel_monitor))) => return Ok((resend_funding_locked, required_revoke, Some(commitment_update), Some(channel_monitor), self.resend_order.clone(), shutdown_msg)),
+                                       Ok(None) => return Ok((resend_funding_locked, required_revoke, None, None, self.resend_order.clone(), shutdown_msg)),
+                               }
+                       } else {
+                               return Ok((resend_funding_locked, required_revoke, None, None, self.resend_order.clone(), shutdown_msg));
+                       }
+               } else if msg.next_local_commitment_number == our_next_remote_commitment_number - 1 {
+                       if required_revoke.is_some() {
+                               log_debug!(self, "Reconnected channel {} with lost outbound RAA and lost remote commitment tx", log_bytes!(self.channel_id()));
+                       } else {
+                               log_debug!(self, "Reconnected channel {} with only lost remote commitment tx", log_bytes!(self.channel_id()));
+                       }
+
+                       if self.channel_state & (ChannelState::MonitorUpdateFailed as u32) != 0 {
+                               self.monitor_pending_commitment_signed = true;
+                               return Ok((resend_funding_locked, None, None, None, self.resend_order.clone(), shutdown_msg));
+                       }
+
+                       return Ok((resend_funding_locked, required_revoke, Some(self.get_last_commitment_update()), None, self.resend_order.clone(), shutdown_msg));
+               } else {
+                       return Err(ChannelError::Close("Peer attempted to reestablish channel with a very old remote commitment transaction"));
+               }
+       }
+
+       fn maybe_propose_first_closing_signed(&mut self, fee_estimator: &FeeEstimator) -> Option<msgs::ClosingSigned> {
+               if !self.channel_outbound || !self.pending_inbound_htlcs.is_empty() || !self.pending_outbound_htlcs.is_empty() ||
+                               self.channel_state & (BOTH_SIDES_SHUTDOWN_MASK | ChannelState::AwaitingRemoteRevoke as u32) != BOTH_SIDES_SHUTDOWN_MASK ||
+                               self.last_sent_closing_fee.is_some() || self.pending_update_fee.is_some() {
+                       return None;
+               }
+
+               let mut proposed_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background);
+               if self.feerate_per_kw > proposed_feerate {
+                       proposed_feerate = self.feerate_per_kw;
+               }
+               let tx_weight = Self::get_closing_transaction_weight(&self.get_closing_scriptpubkey(), self.their_shutdown_scriptpubkey.as_ref().unwrap());
+               let proposed_total_fee_satoshis = proposed_feerate * tx_weight / 1000;
+
+               let (closing_tx, total_fee_satoshis) = self.build_closing_transaction(proposed_total_fee_satoshis, false);
+               let funding_redeemscript = self.get_funding_redeemscript();
+               let sighash = hash_to_message!(&bip143::SighashComponents::new(&closing_tx).sighash_all(&closing_tx.input[0], &funding_redeemscript, self.channel_value_satoshis)[..]);
+
+               self.last_sent_closing_fee = Some((proposed_feerate, total_fee_satoshis));
+               Some(msgs::ClosingSigned {
+                       channel_id: self.channel_id,
+                       fee_satoshis: total_fee_satoshis,
+                       signature: self.secp_ctx.sign(&sighash, &self.local_keys.funding_key),
+               })
+       }
+
+       pub fn shutdown(&mut self, fee_estimator: &FeeEstimator, msg: &msgs::Shutdown) -> Result<(Option<msgs::Shutdown>, Option<msgs::ClosingSigned>, Vec<(HTLCSource, PaymentHash)>), ChannelError> {
+               if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
+                       return Err(ChannelError::Close("Peer sent shutdown when we needed a channel_reestablish"));
+               }
+               if self.channel_state < ChannelState::FundingSent as u32 {
+                       // Spec says we should fail the connection, not the channel, but that's nonsense, there
+                       // are plenty of reasons you may want to fail a channel pre-funding, and spec says you
+                       // can do that via error message without getting a connection fail anyway...
+                       return Err(ChannelError::Close("Peer sent shutdown pre-funding generation"));
+               }
+               for htlc in self.pending_inbound_htlcs.iter() {
+                       if let InboundHTLCState::RemoteAnnounced(_) = htlc.state {
+                               return Err(ChannelError::Close("Got shutdown with remote pending HTLCs"));
+                       }
+               }
+               assert_eq!(self.channel_state & ChannelState::ShutdownComplete as u32, 0);
+
+               // BOLT 2 says we must only send a scriptpubkey of certain standard forms, which are up to
+               // 34 bytes in length, so don't let the remote peer feed us some super fee-heavy script.
+               if self.channel_outbound && msg.scriptpubkey.len() > 34 {
+                       return Err(ChannelError::Close("Got shutdown_scriptpubkey of absurd length from remote peer"));
+               }
+
+               //Check shutdown_scriptpubkey form as BOLT says we must
+               if !msg.scriptpubkey.is_p2pkh() && !msg.scriptpubkey.is_p2sh() && !msg.scriptpubkey.is_v0_p2wpkh() && !msg.scriptpubkey.is_v0_p2wsh() {
+                       return Err(ChannelError::Close("Got a nonstandard scriptpubkey from remote peer"));
+               }
+
+               if self.their_shutdown_scriptpubkey.is_some() {
+                       if Some(&msg.scriptpubkey) != self.their_shutdown_scriptpubkey.as_ref() {
+                               return Err(ChannelError::Close("Got shutdown request with a scriptpubkey which did not match their previous scriptpubkey"));
+                       }
+               } else {
+                       self.their_shutdown_scriptpubkey = Some(msg.scriptpubkey.clone());
+               }
+
+               // From here on out, we may not fail!
+
+               self.channel_state |= ChannelState::RemoteShutdownSent as u32;
+               self.channel_update_count += 1;
+
+               // We can't send our shutdown until we've committed all of our pending HTLCs, but the
+               // remote side is unlikely to accept any new HTLCs, so we go ahead and "free" any holding
+               // cell HTLCs and return them to fail the payment.
+               self.holding_cell_update_fee = None;
+               let mut dropped_outbound_htlcs = Vec::with_capacity(self.holding_cell_htlc_updates.len());
+               self.holding_cell_htlc_updates.retain(|htlc_update| {
+                       match htlc_update {
+                               &HTLCUpdateAwaitingACK::AddHTLC { ref payment_hash, ref source, .. } => {
+                                       dropped_outbound_htlcs.push((source.clone(), payment_hash.clone()));
+                                       false
+                               },
+                               _ => true
+                       }
+               });
+               // If we have any LocalAnnounced updates we'll probably just get back a update_fail_htlc
+               // immediately after the commitment dance, but we can send a Shutdown cause we won't send
+               // any further commitment updates after we set LocalShutdownSent.
+
+               let our_shutdown = if (self.channel_state & ChannelState::LocalShutdownSent as u32) == ChannelState::LocalShutdownSent as u32 {
+                       None
+               } else {
+                       Some(msgs::Shutdown {
+                               channel_id: self.channel_id,
+                               scriptpubkey: self.get_closing_scriptpubkey(),
+                       })
+               };
+
+               self.channel_state |= ChannelState::LocalShutdownSent as u32;
+               self.channel_update_count += 1;
+               Ok((our_shutdown, self.maybe_propose_first_closing_signed(fee_estimator), dropped_outbound_htlcs))
+       }
+
+       pub fn closing_signed(&mut self, fee_estimator: &FeeEstimator, msg: &msgs::ClosingSigned) -> Result<(Option<msgs::ClosingSigned>, Option<Transaction>), ChannelError> {
+               if self.channel_state & BOTH_SIDES_SHUTDOWN_MASK != BOTH_SIDES_SHUTDOWN_MASK {
+                       return Err(ChannelError::Close("Remote end sent us a closing_signed before both sides provided a shutdown"));
+               }
+               if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
+                       return Err(ChannelError::Close("Peer sent closing_signed when we needed a channel_reestablish"));
+               }
+               if !self.pending_inbound_htlcs.is_empty() || !self.pending_outbound_htlcs.is_empty() {
+                       return Err(ChannelError::Close("Remote end sent us a closing_signed while there were still pending HTLCs"));
+               }
+               if msg.fee_satoshis > 21000000 * 10000000 { //this is required to stop potential overflow in build_closing_transaction
+                       return Err(ChannelError::Close("Remote tried to send us a closing tx with > 21 million BTC fee"));
+               }
+
+               let funding_redeemscript = self.get_funding_redeemscript();
+               let (mut closing_tx, used_total_fee) = self.build_closing_transaction(msg.fee_satoshis, false);
+               if used_total_fee != msg.fee_satoshis {
+                       return Err(ChannelError::Close("Remote sent us a closing_signed with a fee greater than the value they can claim"));
+               }
+               let mut sighash = hash_to_message!(&bip143::SighashComponents::new(&closing_tx).sighash_all(&closing_tx.input[0], &funding_redeemscript, self.channel_value_satoshis)[..]);
+
+               match self.secp_ctx.verify(&sighash, &msg.signature, &self.their_funding_pubkey.unwrap()) {
+                       Ok(_) => {},
+                       Err(_e) => {
+                               // The remote end may have decided to revoke their output due to inconsistent dust
+                               // limits, so check for that case by re-checking the signature here.
+                               closing_tx = self.build_closing_transaction(msg.fee_satoshis, true).0;
+                               sighash = hash_to_message!(&bip143::SighashComponents::new(&closing_tx).sighash_all(&closing_tx.input[0], &funding_redeemscript, self.channel_value_satoshis)[..]);
+                               secp_check!(self.secp_ctx.verify(&sighash, &msg.signature, &self.their_funding_pubkey.unwrap()), "Invalid closing tx signature from peer");
+                       },
+               };
+
+               if let Some((_, last_fee)) = self.last_sent_closing_fee {
+                       if last_fee == msg.fee_satoshis {
+                               self.sign_commitment_transaction(&mut closing_tx, &msg.signature);
+                               self.channel_state = ChannelState::ShutdownComplete as u32;
+                               self.channel_update_count += 1;
+                               return Ok((None, Some(closing_tx)));
+                       }
+               }
+
+               macro_rules! propose_new_feerate {
+                       ($new_feerate: expr) => {
+                               let closing_tx_max_weight = Self::get_closing_transaction_weight(&self.get_closing_scriptpubkey(), self.their_shutdown_scriptpubkey.as_ref().unwrap());
+                               let (closing_tx, used_total_fee) = self.build_closing_transaction($new_feerate * closing_tx_max_weight / 1000, false);
+                               sighash = hash_to_message!(&bip143::SighashComponents::new(&closing_tx).sighash_all(&closing_tx.input[0], &funding_redeemscript, self.channel_value_satoshis)[..]);
+                               let our_sig = self.secp_ctx.sign(&sighash, &self.local_keys.funding_key);
+                               self.last_sent_closing_fee = Some(($new_feerate, used_total_fee));
+                               return Ok((Some(msgs::ClosingSigned {
+                                       channel_id: self.channel_id,
+                                       fee_satoshis: used_total_fee,
+                                       signature: our_sig,
+                               }), None))
+                       }
+               }
+
+               let proposed_sat_per_kw = msg.fee_satoshis * 1000 / closing_tx.get_weight() as u64;
+               if self.channel_outbound {
+                       let our_max_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
+                       if proposed_sat_per_kw > our_max_feerate {
+                               if let Some((last_feerate, _)) = self.last_sent_closing_fee {
+                                       if our_max_feerate <= last_feerate {
+                                               return Err(ChannelError::Close("Unable to come to consensus about closing feerate, remote wanted something higher than our Normal feerate"));
+                                       }
+                               }
+                               propose_new_feerate!(our_max_feerate);
+                       }
+               } else {
+                       let our_min_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background);
+                       if proposed_sat_per_kw < our_min_feerate {
+                               if let Some((last_feerate, _)) = self.last_sent_closing_fee {
+                                       if our_min_feerate >= last_feerate {
+                                               return Err(ChannelError::Close("Unable to come to consensus about closing feerate, remote wanted something lower than our Background feerate"));
+                                       }
+                               }
+                               propose_new_feerate!(our_min_feerate);
+                       }
+               }
+
+               let our_sig = self.sign_commitment_transaction(&mut closing_tx, &msg.signature);
+               self.channel_state = ChannelState::ShutdownComplete as u32;
+               self.channel_update_count += 1;
+
+               Ok((Some(msgs::ClosingSigned {
+                       channel_id: self.channel_id,
+                       fee_satoshis: msg.fee_satoshis,
+                       signature: our_sig,
+               }), Some(closing_tx)))
+       }
+
+       // Public utilities:
+
+       pub fn channel_id(&self) -> [u8; 32] {
+               self.channel_id
+       }
+
+       /// Gets the "user_id" value passed into the construction of this channel. It has no special
+       /// meaning and exists only to allow users to have a persistent identifier of a channel.
+       pub fn get_user_id(&self) -> u64 {
+               self.user_id
+       }
+
+       /// May only be called after funding has been initiated (ie is_funding_initiated() is true)
+       pub fn channel_monitor(&self) -> ChannelMonitor {
+               if self.channel_state < ChannelState::FundingCreated as u32 {
+                       panic!("Can't get a channel monitor until funding has been created");
+               }
+               self.channel_monitor.clone()
+       }
+
+       /// Guaranteed to be Some after both FundingLocked messages have been exchanged (and, thus,
+       /// is_usable() returns true).
+       /// Allowed in any state (including after shutdown)
+       pub fn get_short_channel_id(&self) -> Option<u64> {
+               self.short_channel_id
+       }
+
+       /// Returns the funding_txo we either got from our peer, or were given by
+       /// get_outbound_funding_created.
+       pub fn get_funding_txo(&self) -> Option<OutPoint> {
+               self.channel_monitor.get_funding_txo()
+       }
+
+       /// Allowed in any state (including after shutdown)
+       pub fn get_their_node_id(&self) -> PublicKey {
+               self.their_node_id
+       }
+
+       /// Allowed in any state (including after shutdown)
+       pub fn get_our_htlc_minimum_msat(&self) -> u64 {
+               self.our_htlc_minimum_msat
+       }
+
+       /// Allowed in any state (including after shutdown)
+       pub fn get_their_htlc_minimum_msat(&self) -> u64 {
+               self.our_htlc_minimum_msat
+       }
+
+       pub fn get_value_satoshis(&self) -> u64 {
+               self.channel_value_satoshis
+       }
+
+       pub fn get_fee_proportional_millionths(&self) -> u32 {
+               self.config.fee_proportional_millionths
+       }
+
+       #[cfg(test)]
+       pub fn get_feerate(&self) -> u64 {
+               self.feerate_per_kw
+       }
+
+       pub fn get_cur_local_commitment_transaction_number(&self) -> u64 {
+               self.cur_local_commitment_transaction_number + 1
+       }
+
+       pub fn get_cur_remote_commitment_transaction_number(&self) -> u64 {
+               self.cur_remote_commitment_transaction_number + 1 - if self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32) != 0 { 1 } else { 0 }
+       }
+
+       pub fn get_revoked_remote_commitment_transaction_number(&self) -> u64 {
+               self.cur_remote_commitment_transaction_number + 2
+       }
+
+       #[cfg(test)]
+       pub fn get_local_keys(&self) -> &ChannelKeys {
+               &self.local_keys
+       }
+
+       #[cfg(test)]
+       pub fn get_value_stat(&self) -> ChannelValueStat {
+               ChannelValueStat {
+                       value_to_self_msat: self.value_to_self_msat,
+                       channel_value_msat: self.channel_value_satoshis * 1000,
+                       channel_reserve_msat: self.their_channel_reserve_satoshis * 1000,
+                       pending_outbound_htlcs_amount_msat: self.pending_outbound_htlcs.iter().map(|ref h| h.amount_msat).sum::<u64>(),
+                       pending_inbound_htlcs_amount_msat: self.pending_inbound_htlcs.iter().map(|ref h| h.amount_msat).sum::<u64>(),
+                       holding_cell_outbound_amount_msat: {
+                               let mut res = 0;
+                               for h in self.holding_cell_htlc_updates.iter() {
+                                       match h {
+                                               &HTLCUpdateAwaitingACK::AddHTLC{amount_msat, .. } => {
+                                                       res += amount_msat;
+                                               }
+                                               _ => {}
+                                       }
+                               }
+                               res
+                       },
+                       their_max_htlc_value_in_flight_msat: self.their_max_htlc_value_in_flight_msat,
+               }
+       }
+
+       /// Allowed in any state (including after shutdown)
+       pub fn get_channel_update_count(&self) -> u32 {
+               self.channel_update_count
+       }
+
+       pub fn should_announce(&self) -> bool {
+               self.config.announced_channel
+       }
+
+       pub fn is_outbound(&self) -> bool {
+               self.channel_outbound
+       }
+
+       /// Gets the fee we'd want to charge for adding an HTLC output to this Channel
+       /// Allowed in any state (including after shutdown)
+       pub fn get_our_fee_base_msat(&self, fee_estimator: &FeeEstimator) -> u32 {
+               // For lack of a better metric, we calculate what it would cost to consolidate the new HTLC
+               // output value back into a transaction with the regular channel output:
+
+               // the fee cost of the HTLC-Success/HTLC-Timeout transaction:
+               let mut res = self.feerate_per_kw * cmp::max(HTLC_TIMEOUT_TX_WEIGHT, HTLC_SUCCESS_TX_WEIGHT) / 1000;
+
+               if self.channel_outbound {
+                       // + the marginal fee increase cost to us in the commitment transaction:
+                       res += self.feerate_per_kw * COMMITMENT_TX_WEIGHT_PER_HTLC / 1000;
+               }
+
+               // + the marginal cost of an input which spends the HTLC-Success/HTLC-Timeout output:
+               res += fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal) * SPENDING_INPUT_FOR_A_OUTPUT_WEIGHT / 1000;
+
+               res as u32
+       }
+
+       /// Returns true if we've ever received a message from the remote end for this Channel
+       pub fn have_received_message(&self) -> bool {
+               self.channel_state > (ChannelState::OurInitSent as u32)
+       }
+
+       /// Returns true if this channel is fully established and not known to be closing.
+       /// Allowed in any state (including after shutdown)
+       pub fn is_usable(&self) -> bool {
+               let mask = ChannelState::ChannelFunded as u32 | BOTH_SIDES_SHUTDOWN_MASK;
+               (self.channel_state & mask) == (ChannelState::ChannelFunded as u32)
+       }
+
+       /// Returns true if this channel is currently available for use. This is a superset of
+       /// is_usable() and considers things like the channel being temporarily disabled.
+       /// Allowed in any state (including after shutdown)
+       pub fn is_live(&self) -> bool {
+               self.is_usable() && (self.channel_state & (ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateFailed as u32) == 0)
+       }
+
+       /// Returns true if this channel has been marked as awaiting a monitor update to move forward.
+       /// Allowed in any state (including after shutdown)
+       pub fn is_awaiting_monitor_update(&self) -> bool {
+               (self.channel_state & ChannelState::MonitorUpdateFailed as u32) != 0
+       }
+
+       /// Returns true if funding_created was sent/received.
+       pub fn is_funding_initiated(&self) -> bool {
+               self.channel_state >= ChannelState::FundingCreated as u32
+       }
+
+       /// Returns true if this channel is fully shut down. True here implies that no further actions
+       /// may/will be taken on this channel, and thus this object should be freed. Any future changes
+       /// will be handled appropriately by the chain monitor.
+       pub fn is_shutdown(&self) -> bool {
+               if (self.channel_state & ChannelState::ShutdownComplete as u32) == ChannelState::ShutdownComplete as u32  {
+                       assert!(self.channel_state == ChannelState::ShutdownComplete as u32);
+                       true
+               } else { false }
+       }
+
+       /// Called by channelmanager based on chain blocks being connected.
+       /// Note that we only need to use this to detect funding_signed, anything else is handled by
+       /// the channel_monitor.
+       /// In case of Err, the channel may have been closed, at which point the standard requirements
+       /// apply - no calls may be made except those explicitly stated to be allowed post-shutdown.
+       /// Only returns an ErrorAction of DisconnectPeer, if Err.
+       pub fn block_connected(&mut self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], indexes_of_txn_matched: &[u32]) -> Result<Option<msgs::FundingLocked>, msgs::ErrorMessage> {
+               let non_shutdown_state = self.channel_state & (!MULTI_STATE_FLAGS);
+               if header.bitcoin_hash() != self.last_block_connected {
+                       self.last_block_connected = header.bitcoin_hash();
+                       self.channel_monitor.last_block_hash = self.last_block_connected;
+                       if self.funding_tx_confirmations > 0 {
+                               self.funding_tx_confirmations += 1;
+                               if self.funding_tx_confirmations == self.minimum_depth as u64 {
+                                       let need_commitment_update = if non_shutdown_state == ChannelState::FundingSent as u32 {
+                                               self.channel_state |= ChannelState::OurFundingLocked as u32;
+                                               true
+                                       } else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::TheirFundingLocked as u32) {
+                                               self.channel_state = ChannelState::ChannelFunded as u32 | (self.channel_state & MULTI_STATE_FLAGS);
+                                               self.channel_update_count += 1;
+                                               true
+                                       } else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::OurFundingLocked as u32) {
+                                               // We got a reorg but not enough to trigger a force close, just update
+                                               // funding_tx_confirmed_in and return.
+                                               false
+                                       } else if self.channel_state < ChannelState::ChannelFunded as u32 {
+                                               panic!("Started confirming a channel in a state pre-FundingSent?: {}", self.channel_state);
+                                       } else {
+                                               // We got a reorg but not enough to trigger a force close, just update
+                                               // funding_tx_confirmed_in and return.
+                                               false
+                                       };
+                                       self.funding_tx_confirmed_in = Some(header.bitcoin_hash());
+
+                                       //TODO: Note that this must be a duplicate of the previous commitment point they sent us,
+                                       //as otherwise we will have a commitment transaction that they can't revoke (well, kinda,
+                                       //they can by sending two revoke_and_acks back-to-back, but not really). This appears to be
+                                       //a protocol oversight, but I assume I'm just missing something.
+                                       if need_commitment_update {
+                                               if self.channel_state & (ChannelState::MonitorUpdateFailed as u32) == 0 {
+                                                       let next_per_commitment_secret = self.build_local_commitment_secret(self.cur_local_commitment_transaction_number);
+                                                       let next_per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &next_per_commitment_secret);
+                                                       return Ok(Some(msgs::FundingLocked {
+                                                               channel_id: self.channel_id,
+                                                               next_per_commitment_point: next_per_commitment_point,
+                                                       }));
+                                               } else {
+                                                       self.monitor_pending_funding_locked = true;
+                                                       return Ok(None);
+                                               }
+                                       }
+                               }
+                       }
+               }
+               if non_shutdown_state & !(ChannelState::TheirFundingLocked as u32) == ChannelState::FundingSent as u32 {
+                       for (ref tx, index_in_block) in txn_matched.iter().zip(indexes_of_txn_matched) {
+                               if tx.txid() == self.channel_monitor.get_funding_txo().unwrap().txid {
+                                       let txo_idx = self.channel_monitor.get_funding_txo().unwrap().index as usize;
+                                       if txo_idx >= tx.output.len() || tx.output[txo_idx].script_pubkey != self.get_funding_redeemscript().to_v0_p2wsh() ||
+                                                       tx.output[txo_idx].value != self.channel_value_satoshis {
+                                               if self.channel_outbound {
+                                                       // If we generated the funding transaction and it doesn't match what it
+                                                       // should, the client is really broken and we should just panic and
+                                                       // tell them off. That said, because hash collisions happen with high
+                                                       // probability in fuzztarget mode, if we're fuzzing we just close the
+                                                       // channel and move on.
+                                                       #[cfg(not(feature = "fuzztarget"))]
+                                                       panic!("Client called ChannelManager::funding_transaction_generated with bogus transaction!");
+                                               }
+                                               self.channel_state = ChannelState::ShutdownComplete as u32;
+                                               self.channel_update_count += 1;
+                                               return Err(msgs::ErrorMessage {
+                                                       channel_id: self.channel_id(),
+                                                       data: "funding tx had wrong script/value".to_owned()
+                                               });
+                                       } else {
+                                               if self.channel_outbound {
+                                                       for input in tx.input.iter() {
+                                                               if input.witness.is_empty() {
+                                                                       // We generated a malleable funding transaction, implying we've
+                                                                       // just exposed ourselves to funds loss to our counterparty.
+                                                                       #[cfg(not(feature = "fuzztarget"))]
+                                                                       panic!("Client called ChannelManager::funding_transaction_generated with bogus transaction!");
+                                                               }
+                                                       }
+                                               }
+                                               self.funding_tx_confirmations = 1;
+                                               self.short_channel_id = Some(((height as u64)          << (5*8)) |
+                                                                            ((*index_in_block as u64) << (2*8)) |
+                                                                            ((txo_idx as u64)         << (0*8)));
+                                       }
+                               }
+                       }
+               }
+               Ok(None)
+       }
+
+       /// Called by channelmanager based on chain blocks being disconnected.
+       /// Returns true if we need to close the channel now due to funding transaction
+       /// unconfirmation/reorg.
+       pub fn block_disconnected(&mut self, header: &BlockHeader) -> bool {
+               if self.funding_tx_confirmations > 0 {
+                       self.funding_tx_confirmations -= 1;
+                       if self.funding_tx_confirmations == UNCONF_THRESHOLD as u64 {
+                               return true;
+                       }
+               }
+               if Some(header.bitcoin_hash()) == self.funding_tx_confirmed_in {
+                       self.funding_tx_confirmations = self.minimum_depth as u64 - 1;
+               }
+               self.last_block_connected = header.bitcoin_hash();
+               self.channel_monitor.last_block_hash = self.last_block_connected;
+               false
+       }
+
+       // Methods to get unprompted messages to send to the remote end (or where we already returned
+       // something in the handler for the message that prompted this message):
+
+       pub fn get_open_channel(&self, chain_hash: Sha256dHash, fee_estimator: &FeeEstimator) -> msgs::OpenChannel {
+               if !self.channel_outbound {
+                       panic!("Tried to open a channel for an inbound channel?");
+               }
+               if self.channel_state != ChannelState::OurInitSent as u32 {
+                       panic!("Cannot generate an open_channel after we've moved forward");
+               }
+
+               if self.cur_local_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
+                       panic!("Tried to send an open_channel for a channel that has already advanced");
+               }
+
+               let local_commitment_secret = self.build_local_commitment_secret(self.cur_local_commitment_transaction_number);
+
+               msgs::OpenChannel {
+                       chain_hash: chain_hash,
+                       temporary_channel_id: self.channel_id,
+                       funding_satoshis: self.channel_value_satoshis,
+                       push_msat: self.channel_value_satoshis * 1000 - self.value_to_self_msat,
+                       dust_limit_satoshis: self.our_dust_limit_satoshis,
+                       max_htlc_value_in_flight_msat: Channel::get_our_max_htlc_value_in_flight_msat(self.channel_value_satoshis),
+                       channel_reserve_satoshis: Channel::get_our_channel_reserve_satoshis(self.channel_value_satoshis),
+                       htlc_minimum_msat: self.our_htlc_minimum_msat,
+                       feerate_per_kw: fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background) as u32,
+                       to_self_delay: self.our_to_self_delay,
+                       max_accepted_htlcs: OUR_MAX_HTLCS,
+                       funding_pubkey: PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.funding_key),
+                       revocation_basepoint: PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.revocation_base_key),
+                       payment_basepoint: PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.payment_base_key),
+                       delayed_payment_basepoint: PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.delayed_payment_base_key),
+                       htlc_basepoint: PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.htlc_base_key),
+                       first_per_commitment_point: PublicKey::from_secret_key(&self.secp_ctx, &local_commitment_secret),
+                       channel_flags: if self.config.announced_channel {1} else {0},
+                       shutdown_scriptpubkey: OptionalField::Present(if self.config.commit_upfront_shutdown_pubkey { self.get_closing_scriptpubkey() } else { Builder::new().into_script() })
+               }
+       }
+
+       pub fn get_accept_channel(&self) -> msgs::AcceptChannel {
+               if self.channel_outbound {
+                       panic!("Tried to send accept_channel for an outbound channel?");
+               }
+               if self.channel_state != (ChannelState::OurInitSent as u32) | (ChannelState::TheirInitSent as u32) {
+                       panic!("Tried to send accept_channel after channel had moved forward");
+               }
+               if self.cur_local_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
+                       panic!("Tried to send an accept_channel for a channel that has already advanced");
+               }
+
+               let local_commitment_secret = self.build_local_commitment_secret(self.cur_local_commitment_transaction_number);
+
+               msgs::AcceptChannel {
+                       temporary_channel_id: self.channel_id,
+                       dust_limit_satoshis: self.our_dust_limit_satoshis,
+                       max_htlc_value_in_flight_msat: Channel::get_our_max_htlc_value_in_flight_msat(self.channel_value_satoshis),
+                       channel_reserve_satoshis: Channel::get_our_channel_reserve_satoshis(self.channel_value_satoshis),
+                       htlc_minimum_msat: self.our_htlc_minimum_msat,
+                       minimum_depth: self.minimum_depth,
+                       to_self_delay: self.our_to_self_delay,
+                       max_accepted_htlcs: OUR_MAX_HTLCS,
+                       funding_pubkey: PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.funding_key),
+                       revocation_basepoint: PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.revocation_base_key),
+                       payment_basepoint: PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.payment_base_key),
+                       delayed_payment_basepoint: PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.delayed_payment_base_key),
+                       htlc_basepoint: PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.htlc_base_key),
+                       first_per_commitment_point: PublicKey::from_secret_key(&self.secp_ctx, &local_commitment_secret),
+                       shutdown_scriptpubkey: OptionalField::Present(if self.config.commit_upfront_shutdown_pubkey { self.get_closing_scriptpubkey() } else { Builder::new().into_script() })
+               }
+       }
+
+       /// If an Err is returned, it is a ChannelError::Close (for get_outbound_funding_created)
+       fn get_outbound_funding_created_signature(&mut self) -> Result<(Signature, Transaction), ChannelError> {
+               let funding_script = self.get_funding_redeemscript();
+
+               let remote_keys = self.build_remote_transaction_keys()?;
+               let remote_initial_commitment_tx = self.build_commitment_transaction(self.cur_remote_commitment_transaction_number, &remote_keys, false, false, self.feerate_per_kw).0;
+               let remote_sighash = hash_to_message!(&bip143::SighashComponents::new(&remote_initial_commitment_tx).sighash_all(&remote_initial_commitment_tx.input[0], &funding_script, self.channel_value_satoshis)[..]);
+
+               // We sign the "remote" commitment transaction, allowing them to broadcast the tx if they wish.
+               Ok((self.secp_ctx.sign(&remote_sighash, &self.local_keys.funding_key), remote_initial_commitment_tx))
+       }
+
+       /// Updates channel state with knowledge of the funding transaction's txid/index, and generates
+       /// a funding_created message for the remote peer.
+       /// Panics if called at some time other than immediately after initial handshake, if called twice,
+       /// or if called on an inbound channel.
+       /// Note that channel_id changes during this call!
+       /// Do NOT broadcast the funding transaction until after a successful funding_signed call!
+       /// If an Err is returned, it is a ChannelError::Close.
+       pub fn get_outbound_funding_created(&mut self, funding_txo: OutPoint) -> Result<(msgs::FundingCreated, ChannelMonitor), ChannelError> {
+               if !self.channel_outbound {
+                       panic!("Tried to create outbound funding_created message on an inbound channel!");
+               }
+               if self.channel_state != (ChannelState::OurInitSent as u32 | ChannelState::TheirInitSent as u32) {
+                       panic!("Tried to get a funding_created messsage at a time other than immediately after initial handshake completion (or tried to get funding_created twice)");
+               }
+               if self.channel_monitor.get_min_seen_secret() != (1 << 48) ||
+                               self.cur_remote_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER ||
+                               self.cur_local_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
+                       panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
+               }
+
+               let funding_txo_script = self.get_funding_redeemscript().to_v0_p2wsh();
+               self.channel_monitor.set_funding_info((funding_txo, funding_txo_script));
+
+               let (our_signature, commitment_tx) = match self.get_outbound_funding_created_signature() {
+                       Ok(res) => res,
+                       Err(e) => {
+                               log_error!(self, "Got bad signatures: {:?}!", e);
+                               self.channel_monitor.unset_funding_info();
+                               return Err(e);
+                       }
+               };
+
+               let temporary_channel_id = self.channel_id;
+
+               // Now that we're past error-generating stuff, update our local state:
+               self.channel_monitor.provide_latest_remote_commitment_tx_info(&commitment_tx, Vec::new(), self.cur_remote_commitment_transaction_number, self.their_cur_commitment_point.unwrap());
+               self.channel_state = ChannelState::FundingCreated as u32;
+               self.channel_id = funding_txo.to_channel_id();
+               self.cur_remote_commitment_transaction_number -= 1;
+
+               Ok((msgs::FundingCreated {
+                       temporary_channel_id: temporary_channel_id,
+                       funding_txid: funding_txo.txid,
+                       funding_output_index: funding_txo.index,
+                       signature: our_signature
+               }, self.channel_monitor.clone()))
+       }
+
+       /// Gets an UnsignedChannelAnnouncement, as well as a signature covering it using our
+       /// bitcoin_key, if available, for this channel. The channel must be publicly announceable and
+       /// available for use (have exchanged FundingLocked messages in both directions). Should be used
+       /// for both loose and in response to an AnnouncementSignatures message from the remote peer.
+       /// Will only fail if we're not in a state where channel_announcement may be sent (including
+       /// closing).
+       /// Note that the "channel must be funded" requirement is stricter than BOLT 7 requires - see
+       /// https://github.com/lightningnetwork/lightning-rfc/issues/468
+       pub fn get_channel_announcement(&self, our_node_id: PublicKey, chain_hash: Sha256dHash) -> Result<(msgs::UnsignedChannelAnnouncement, Signature), ChannelError> {
+               if !self.config.announced_channel {
+                       return Err(ChannelError::Ignore("Channel is not available for public announcements"));
+               }
+               if self.channel_state & (ChannelState::ChannelFunded as u32) == 0 {
+                       return Err(ChannelError::Ignore("Cannot get a ChannelAnnouncement until the channel funding has been locked"));
+               }
+               if (self.channel_state & (ChannelState::LocalShutdownSent as u32 | ChannelState::ShutdownComplete as u32)) != 0 {
+                       return Err(ChannelError::Ignore("Cannot get a ChannelAnnouncement once the channel is closing"));
+               }
+
+               let were_node_one = our_node_id.serialize()[..] < self.their_node_id.serialize()[..];
+               let our_bitcoin_key = PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.funding_key);
+
+               let msg = msgs::UnsignedChannelAnnouncement {
+                       features: msgs::GlobalFeatures::new(),
+                       chain_hash: chain_hash,
+                       short_channel_id: self.get_short_channel_id().unwrap(),
+                       node_id_1: if were_node_one { our_node_id } else { self.get_their_node_id() },
+                       node_id_2: if were_node_one { self.get_their_node_id() } else { our_node_id },
+                       bitcoin_key_1: if were_node_one { our_bitcoin_key } else { self.their_funding_pubkey.unwrap() },
+                       bitcoin_key_2: if were_node_one { self.their_funding_pubkey.unwrap() } else { our_bitcoin_key },
+                       excess_data: Vec::new(),
+               };
+
+               let msghash = hash_to_message!(&Sha256dHash::hash(&msg.encode()[..])[..]);
+               let sig = self.secp_ctx.sign(&msghash, &self.local_keys.funding_key);
+
+               Ok((msg, sig))
+       }
+
+       /// May panic if called on a channel that wasn't immediately-previously
+       /// self.remove_uncommitted_htlcs_and_mark_paused()'d
+       pub fn get_channel_reestablish(&self) -> msgs::ChannelReestablish {
+               assert_eq!(self.channel_state & ChannelState::PeerDisconnected as u32, ChannelState::PeerDisconnected as u32);
+               assert_ne!(self.cur_remote_commitment_transaction_number, INITIAL_COMMITMENT_NUMBER);
+               let data_loss_protect = if self.cur_remote_commitment_transaction_number + 1 < INITIAL_COMMITMENT_NUMBER {
+                       let remote_last_secret = self.channel_monitor.get_secret(self.cur_remote_commitment_transaction_number + 2).unwrap();
+                       log_trace!(self, "Enough info to generate a Data Loss Protect with per_commitment_secret {}", log_bytes!(remote_last_secret));
+                       OptionalField::Present(DataLossProtect {
+                               your_last_per_commitment_secret: remote_last_secret,
+                               my_current_per_commitment_point: PublicKey::from_secret_key(&self.secp_ctx, &self.build_local_commitment_secret(self.cur_local_commitment_transaction_number + 1))
+                       })
+               } else {
+                       log_debug!(self, "We don't seen yet any revoked secret, if this channnel has already been updated it means we are fallen-behind, you should wait for other peer closing");
+                       OptionalField::Present(DataLossProtect {
+                               your_last_per_commitment_secret: [0;32],
+                               my_current_per_commitment_point: PublicKey::from_secret_key(&self.secp_ctx, &self.build_local_commitment_secret(self.cur_local_commitment_transaction_number))
+                       })
+               };
+               msgs::ChannelReestablish {
+                       channel_id: self.channel_id(),
+                       // The protocol has two different commitment number concepts - the "commitment
+                       // transaction number", which starts from 0 and counts up, and the "revocation key
+                       // index" which starts at INITIAL_COMMITMENT_NUMBER and counts down. We track
+                       // commitment transaction numbers by the index which will be used to reveal the
+                       // revocation key for that commitment transaction, which means we have to convert them
+                       // to protocol-level commitment numbers here...
+
+                       // next_local_commitment_number is the next commitment_signed number we expect to
+                       // receive (indicating if they need to resend one that we missed).
+                       next_local_commitment_number: INITIAL_COMMITMENT_NUMBER - self.cur_local_commitment_transaction_number,
+                       // We have to set next_remote_commitment_number to the next revoke_and_ack we expect to
+                       // receive, however we track it by the next commitment number for a remote transaction
+                       // (which is one further, as they always revoke previous commitment transaction, not
+                       // the one we send) so we have to decrement by 1. Note that if
+                       // cur_remote_commitment_transaction_number is INITIAL_COMMITMENT_NUMBER we will have
+                       // dropped this channel on disconnect as it hasn't yet reached FundingSent so we can't
+                       // overflow here.
+                       next_remote_commitment_number: INITIAL_COMMITMENT_NUMBER - self.cur_remote_commitment_transaction_number - 1,
+                       data_loss_protect,
+               }
+       }
+
+
+       // Send stuff to our remote peers:
+
+       /// Adds a pending outbound HTLC to this channel, note that you probably want
+       /// send_htlc_and_commit instead cause you'll want both messages at once.
+       /// This returns an option instead of a pure UpdateAddHTLC as we may be in a state where we are
+       /// waiting on the remote peer to send us a revoke_and_ack during which time we cannot add new
+       /// HTLCs on the wire or we wouldn't be able to determine what they actually ACK'ed.
+       /// You MUST call send_commitment prior to any other calls on this Channel
+       /// If an Err is returned, it's a ChannelError::Ignore!
+       pub fn send_htlc(&mut self, amount_msat: u64, payment_hash: PaymentHash, cltv_expiry: u32, source: HTLCSource, onion_routing_packet: msgs::OnionPacket) -> Result<Option<msgs::UpdateAddHTLC>, ChannelError> {
+               if (self.channel_state & (ChannelState::ChannelFunded as u32 | BOTH_SIDES_SHUTDOWN_MASK)) != (ChannelState::ChannelFunded as u32) {
+                       return Err(ChannelError::Ignore("Cannot send HTLC until channel is fully established and we haven't started shutting down"));
+               }
+
+               if amount_msat > self.channel_value_satoshis * 1000 {
+                       return Err(ChannelError::Ignore("Cannot send more than the total value of the channel"));
+               }
+               if amount_msat < self.their_htlc_minimum_msat {
+                       return Err(ChannelError::Ignore("Cannot send less than their minimum HTLC value"));
+               }
+
+               if (self.channel_state & (ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateFailed as u32)) != 0 {
+                       // Note that this should never really happen, if we're !is_live() on receipt of an
+                       // incoming HTLC for relay will result in us rejecting the HTLC and we won't allow
+                       // the user to send directly into a !is_live() channel. However, if we
+                       // disconnected during the time the previous hop was doing the commitment dance we may
+                       // end up getting here after the forwarding delay. In any case, returning an
+                       // IgnoreError will get ChannelManager to do the right thing and fail backwards now.
+                       return Err(ChannelError::Ignore("Cannot send an HTLC while disconnected/frozen for channel monitor update"));
+               }
+
+               let (outbound_htlc_count, htlc_outbound_value_msat) = self.get_outbound_pending_htlc_stats();
+               if outbound_htlc_count + 1 > self.their_max_accepted_htlcs as u32 {
+                       return Err(ChannelError::Ignore("Cannot push more than their max accepted HTLCs"));
+               }
+               // Check their_max_htlc_value_in_flight_msat
+               if htlc_outbound_value_msat + amount_msat > self.their_max_htlc_value_in_flight_msat {
+                       return Err(ChannelError::Ignore("Cannot send value that would put us over the max HTLC value in flight our peer will accept"));
+               }
+
+               // Check self.their_channel_reserve_satoshis (the amount we must keep as
+               // reserve for them to have something to claim if we misbehave)
+               if self.value_to_self_msat < self.their_channel_reserve_satoshis * 1000 + amount_msat + htlc_outbound_value_msat {
+                       return Err(ChannelError::Ignore("Cannot send value that would put us over their reserve value"));
+               }
+
+               //TODO: Check cltv_expiry? Do this in channel manager?
+
+               // Now update local state:
+               if (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32)) == (ChannelState::AwaitingRemoteRevoke as u32) {
+                       self.holding_cell_htlc_updates.push(HTLCUpdateAwaitingACK::AddHTLC {
+                               amount_msat: amount_msat,
+                               payment_hash: payment_hash,
+                               cltv_expiry: cltv_expiry,
+                               source,
+                               onion_routing_packet: onion_routing_packet,
+                       });
+                       return Ok(None);
+               }
+
+               self.pending_outbound_htlcs.push(OutboundHTLCOutput {
+                       htlc_id: self.next_local_htlc_id,
+                       amount_msat: amount_msat,
+                       payment_hash: payment_hash.clone(),
+                       cltv_expiry: cltv_expiry,
+                       state: OutboundHTLCState::LocalAnnounced(Box::new(onion_routing_packet.clone())),
+                       source,
+               });
+
+               let res = msgs::UpdateAddHTLC {
+                       channel_id: self.channel_id,
+                       htlc_id: self.next_local_htlc_id,
+                       amount_msat: amount_msat,
+                       payment_hash: payment_hash,
+                       cltv_expiry: cltv_expiry,
+                       onion_routing_packet: onion_routing_packet,
+               };
+               self.next_local_htlc_id += 1;
+
+               Ok(Some(res))
+       }
+
+       /// Creates a signed commitment transaction to send to the remote peer.
+       /// Always returns a ChannelError::Close if an immediately-preceding (read: the
+       /// last call to this Channel) send_htlc returned Ok(Some(_)) and there is an Err.
+       /// May panic if called except immediately after a successful, Ok(Some(_))-returning send_htlc.
+       pub fn send_commitment(&mut self) -> Result<(msgs::CommitmentSigned, ChannelMonitor), ChannelError> {
+               if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
+                       panic!("Cannot create commitment tx until channel is fully established");
+               }
+               if (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32)) == (ChannelState::AwaitingRemoteRevoke as u32) {
+                       panic!("Cannot create commitment tx until remote revokes their previous commitment");
+               }
+               if (self.channel_state & (ChannelState::PeerDisconnected as u32)) == (ChannelState::PeerDisconnected as u32) {
+                       panic!("Cannot create commitment tx while disconnected, as send_htlc will have returned an Err so a send_commitment precondition has been violated");
+               }
+               if (self.channel_state & (ChannelState::MonitorUpdateFailed as u32)) == (ChannelState::MonitorUpdateFailed as u32) {
+                       panic!("Cannot create commitment tx while awaiting monitor update unfreeze, as send_htlc will have returned an Err so a send_commitment precondition has been violated");
+               }
+               let mut have_updates = self.pending_update_fee.is_some();
+               for htlc in self.pending_outbound_htlcs.iter() {
+                       if let OutboundHTLCState::LocalAnnounced(_) = htlc.state {
+                               have_updates = true;
+                       }
+                       if have_updates { break; }
+               }
+               for htlc in self.pending_inbound_htlcs.iter() {
+                       if let InboundHTLCState::LocalRemoved(_) = htlc.state {
+                               have_updates = true;
+                       }
+                       if have_updates { break; }
+               }
+               if !have_updates {
+                       panic!("Cannot create commitment tx until we have some updates to send");
+               }
+               self.send_commitment_no_status_check()
+       }
+       /// Only fails in case of bad keys
+       fn send_commitment_no_status_check(&mut self) -> Result<(msgs::CommitmentSigned, ChannelMonitor), ChannelError> {
+               // We can upgrade the status of some HTLCs that are waiting on a commitment, even if we
+               // fail to generate this, we still are at least at a position where upgrading their status
+               // is acceptable.
+               for htlc in self.pending_inbound_htlcs.iter_mut() {
+                       let new_state = if let &InboundHTLCState::AwaitingRemoteRevokeToAnnounce(ref forward_info) = &htlc.state {
+                               Some(InboundHTLCState::AwaitingAnnouncedRemoteRevoke(forward_info.clone()))
+                       } else { None };
+                       if let Some(state) = new_state {
+                               htlc.state = state;
+                       }
+               }
+               for htlc in self.pending_outbound_htlcs.iter_mut() {
+                       if let Some(fail_reason) = if let &mut OutboundHTLCState::AwaitingRemoteRevokeToRemove(ref mut fail_reason) = &mut htlc.state {
+                               Some(fail_reason.take())
+                       } else { None } {
+                               htlc.state = OutboundHTLCState::AwaitingRemovedRemoteRevoke(fail_reason);
+                       }
+               }
+               self.resend_order = RAACommitmentOrder::RevokeAndACKFirst;
+
+               let (res, remote_commitment_tx, htlcs) = match self.send_commitment_no_state_update() {
+                       Ok((res, (remote_commitment_tx, mut htlcs))) => {
+                               // Update state now that we've passed all the can-fail calls...
+                               let htlcs_no_ref = htlcs.drain(..).map(|(htlc, htlc_source)| (htlc, htlc_source.map(|source_ref| Box::new(source_ref.clone())))).collect();
+                               (res, remote_commitment_tx, htlcs_no_ref)
+                       },
+                       Err(e) => return Err(e),
+               };
+
+               self.channel_monitor.provide_latest_remote_commitment_tx_info(&remote_commitment_tx, htlcs, self.cur_remote_commitment_transaction_number, self.their_cur_commitment_point.unwrap());
+               self.channel_state |= ChannelState::AwaitingRemoteRevoke as u32;
+               Ok((res, self.channel_monitor.clone()))
+       }
+
+       /// Only fails in case of bad keys. Used for channel_reestablish commitment_signed generation
+       /// when we shouldn't change HTLC/channel state.
+       fn send_commitment_no_state_update(&self) -> Result<(msgs::CommitmentSigned, (Transaction, Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)>)), ChannelError> {
+               let funding_script = self.get_funding_redeemscript();
+
+               let mut feerate_per_kw = self.feerate_per_kw;
+               if let Some(feerate) = self.pending_update_fee {
+                       if self.channel_outbound {
+                               feerate_per_kw = feerate;
+                       }
+               }
+
+               let remote_keys = self.build_remote_transaction_keys()?;
+               let remote_commitment_tx = self.build_commitment_transaction(self.cur_remote_commitment_transaction_number, &remote_keys, false, true, feerate_per_kw);
+               let remote_commitment_txid = remote_commitment_tx.0.txid();
+               let remote_sighash = hash_to_message!(&bip143::SighashComponents::new(&remote_commitment_tx.0).sighash_all(&remote_commitment_tx.0.input[0], &funding_script, self.channel_value_satoshis)[..]);
+               let our_sig = self.secp_ctx.sign(&remote_sighash, &self.local_keys.funding_key);
+               log_trace!(self, "Signing remote commitment tx {} with redeemscript {} with pubkey {} -> {}", encode::serialize_hex(&remote_commitment_tx.0), encode::serialize_hex(&funding_script), log_bytes!(PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.funding_key).serialize()), log_bytes!(our_sig.serialize_compact()[..]));
+
+               let mut htlc_sigs = Vec::with_capacity(remote_commitment_tx.1);
+               for &(ref htlc, _) in remote_commitment_tx.2.iter() {
+                       if let Some(_) = htlc.transaction_output_index {
+                               let htlc_tx = self.build_htlc_transaction(&remote_commitment_txid, htlc, false, &remote_keys, feerate_per_kw);
+                               let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &remote_keys);
+                               let htlc_sighash = hash_to_message!(&bip143::SighashComponents::new(&htlc_tx).sighash_all(&htlc_tx.input[0], &htlc_redeemscript, htlc.amount_msat / 1000)[..]);
+                               let our_htlc_key = secp_check!(chan_utils::derive_private_key(&self.secp_ctx, &remote_keys.per_commitment_point, &self.local_keys.htlc_base_key), "Derived invalid key, peer is maliciously selecting parameters");
+                               htlc_sigs.push(self.secp_ctx.sign(&htlc_sighash, &our_htlc_key));
+                               log_trace!(self, "Signing remote HTLC tx {} with redeemscript {} with pubkey {} -> {}", encode::serialize_hex(&htlc_tx), encode::serialize_hex(&htlc_redeemscript), log_bytes!(PublicKey::from_secret_key(&self.secp_ctx, &our_htlc_key).serialize()), log_bytes!(htlc_sigs.last().unwrap().serialize_compact()[..]));
+                       }
+               }
+
+               Ok((msgs::CommitmentSigned {
+                       channel_id: self.channel_id,
+                       signature: our_sig,
+                       htlc_signatures: htlc_sigs,
+               }, (remote_commitment_tx.0, remote_commitment_tx.2)))
+       }
+
+       /// Adds a pending outbound HTLC to this channel, and creates a signed commitment transaction
+       /// to send to the remote peer in one go.
+       /// Shorthand for calling send_htlc() followed by send_commitment(), see docs on those for
+       /// more info.
+       pub fn send_htlc_and_commit(&mut self, amount_msat: u64, payment_hash: PaymentHash, cltv_expiry: u32, source: HTLCSource, onion_routing_packet: msgs::OnionPacket) -> Result<Option<(msgs::UpdateAddHTLC, msgs::CommitmentSigned, ChannelMonitor)>, ChannelError> {
+               match self.send_htlc(amount_msat, payment_hash, cltv_expiry, source, onion_routing_packet)? {
+                       Some(update_add_htlc) => {
+                               let (commitment_signed, monitor_update) = self.send_commitment_no_status_check()?;
+                               Ok(Some((update_add_htlc, commitment_signed, monitor_update)))
+                       },
+                       None => Ok(None)
+               }
+       }
+
+       /// Begins the shutdown process, getting a message for the remote peer and returning all
+       /// holding cell HTLCs for payment failure.
+       pub fn get_shutdown(&mut self) -> Result<(msgs::Shutdown, Vec<(HTLCSource, PaymentHash)>), APIError> {
+               for htlc in self.pending_outbound_htlcs.iter() {
+                       if let OutboundHTLCState::LocalAnnounced(_) = htlc.state {
+                               return Err(APIError::APIMisuseError{err: "Cannot begin shutdown with pending HTLCs. Process pending events first"});
+                       }
+               }
+               if self.channel_state & BOTH_SIDES_SHUTDOWN_MASK != 0 {
+                       if (self.channel_state & ChannelState::LocalShutdownSent as u32) == ChannelState::LocalShutdownSent as u32 {
+                               return Err(APIError::APIMisuseError{err: "Shutdown already in progress"});
+                       }
+                       else if (self.channel_state & ChannelState::RemoteShutdownSent as u32) == ChannelState::RemoteShutdownSent as u32 {
+                               return Err(APIError::ChannelUnavailable{err: "Shutdown already in progress by remote"});
+                       }
+               }
+               assert_eq!(self.channel_state & ChannelState::ShutdownComplete as u32, 0);
+               if self.channel_state & (ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateFailed as u32) != 0 {
+                       return Err(APIError::ChannelUnavailable{err: "Cannot begin shutdown while peer is disconnected or we're waiting on a monitor update, maybe force-close instead?"});
+               }
+
+               let our_closing_script = self.get_closing_scriptpubkey();
+
+               // From here on out, we may not fail!
+               if self.channel_state < ChannelState::FundingSent as u32 {
+                       self.channel_state = ChannelState::ShutdownComplete as u32;
+               } else {
+                       self.channel_state |= ChannelState::LocalShutdownSent as u32;
+               }
+               self.channel_update_count += 1;
+
+               // Go ahead and drop holding cell updates as we'd rather fail payments than wait to send
+               // our shutdown until we've committed all of the pending changes.
+               self.holding_cell_update_fee = None;
+               let mut dropped_outbound_htlcs = Vec::with_capacity(self.holding_cell_htlc_updates.len());
+               self.holding_cell_htlc_updates.retain(|htlc_update| {
+                       match htlc_update {
+                               &HTLCUpdateAwaitingACK::AddHTLC { ref payment_hash, ref source, .. } => {
+                                       dropped_outbound_htlcs.push((source.clone(), payment_hash.clone()));
+                                       false
+                               },
+                               _ => true
+                       }
+               });
+
+               Ok((msgs::Shutdown {
+                       channel_id: self.channel_id,
+                       scriptpubkey: our_closing_script,
+               }, dropped_outbound_htlcs))
+       }
+
+       /// Gets the latest commitment transaction and any dependent transactions for relay (forcing
+       /// shutdown of this channel - no more calls into this Channel may be made afterwards except
+       /// those explicitly stated to be allowed after shutdown completes, eg some simple getters).
+       /// Also returns the list of payment_hashes for channels which we can safely fail backwards
+       /// immediately (others we will have to allow to time out).
+       pub fn force_shutdown(&mut self) -> (Vec<Transaction>, Vec<(HTLCSource, PaymentHash)>) {
+               assert!(self.channel_state != ChannelState::ShutdownComplete as u32);
+
+               // We go ahead and "free" any holding cell HTLCs or HTLCs we haven't yet committed to and
+               // return them to fail the payment.
+               let mut dropped_outbound_htlcs = Vec::with_capacity(self.holding_cell_htlc_updates.len());
+               for htlc_update in self.holding_cell_htlc_updates.drain(..) {
+                       match htlc_update {
+                               HTLCUpdateAwaitingACK::AddHTLC { source, payment_hash, .. } => {
+                                       dropped_outbound_htlcs.push((source, payment_hash));
+                               },
+                               _ => {}
+                       }
+               }
+
+               for _htlc in self.pending_outbound_htlcs.drain(..) {
+                       //TODO: Do something with the remaining HTLCs
+                       //(we need to have the ChannelManager monitor them so we can claim the inbound HTLCs
+                       //which correspond)
+               }
+
+               self.channel_state = ChannelState::ShutdownComplete as u32;
+               self.channel_update_count += 1;
+               let mut res = Vec::new();
+               mem::swap(&mut res, &mut self.last_local_commitment_txn);
+               (res, dropped_outbound_htlcs)
+       }
+}
+
+const SERIALIZATION_VERSION: u8 = 1;
+const MIN_SERIALIZATION_VERSION: u8 = 1;
+
+impl Writeable for InboundHTLCRemovalReason {
+       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
+               match self {
+                       &InboundHTLCRemovalReason::FailRelay(ref error_packet) => {
+                               0u8.write(writer)?;
+                               error_packet.write(writer)?;
+                       },
+                       &InboundHTLCRemovalReason::FailMalformed((ref onion_hash, ref err_code)) => {
+                               1u8.write(writer)?;
+                               onion_hash.write(writer)?;
+                               err_code.write(writer)?;
+                       },
+                       &InboundHTLCRemovalReason::Fulfill(ref payment_preimage) => {
+                               2u8.write(writer)?;
+                               payment_preimage.write(writer)?;
+                       },
+               }
+               Ok(())
+       }
+}
+
+impl<R: ::std::io::Read> Readable<R> for InboundHTLCRemovalReason {
+       fn read(reader: &mut R) -> Result<Self, DecodeError> {
+               Ok(match <u8 as Readable<R>>::read(reader)? {
+                       0 => InboundHTLCRemovalReason::FailRelay(Readable::read(reader)?),
+                       1 => InboundHTLCRemovalReason::FailMalformed((Readable::read(reader)?, Readable::read(reader)?)),
+                       2 => InboundHTLCRemovalReason::Fulfill(Readable::read(reader)?),
+                       _ => return Err(DecodeError::InvalidValue),
+               })
+       }
+}
+
+impl Writeable for Channel {
+       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
+               // Note that we write out as if remove_uncommitted_htlcs_and_mark_paused had just been
+               // called but include holding cell updates (and obviously we don't modify self).
+
+               writer.write_all(&[SERIALIZATION_VERSION; 1])?;
+               writer.write_all(&[MIN_SERIALIZATION_VERSION; 1])?;
+
+               self.user_id.write(writer)?;
+               self.config.write(writer)?;
+
+               self.channel_id.write(writer)?;
+               (self.channel_state | ChannelState::PeerDisconnected as u32).write(writer)?;
+               self.channel_outbound.write(writer)?;
+               self.channel_value_satoshis.write(writer)?;
+
+               self.local_keys.write(writer)?;
+               self.shutdown_pubkey.write(writer)?;
+
+               self.cur_local_commitment_transaction_number.write(writer)?;
+               self.cur_remote_commitment_transaction_number.write(writer)?;
+               self.value_to_self_msat.write(writer)?;
+
+               let mut dropped_inbound_htlcs = 0;
+               for htlc in self.pending_inbound_htlcs.iter() {
+                       if let InboundHTLCState::RemoteAnnounced(_) = htlc.state {
+                               dropped_inbound_htlcs += 1;
+                       }
+               }
+               (self.pending_inbound_htlcs.len() as u64 - dropped_inbound_htlcs).write(writer)?;
+               for htlc in self.pending_inbound_htlcs.iter() {
+                       htlc.htlc_id.write(writer)?;
+                       htlc.amount_msat.write(writer)?;
+                       htlc.cltv_expiry.write(writer)?;
+                       htlc.payment_hash.write(writer)?;
+                       match &htlc.state {
+                               &InboundHTLCState::RemoteAnnounced(_) => {}, // Drop
+                               &InboundHTLCState::AwaitingRemoteRevokeToAnnounce(ref htlc_state) => {
+                                       1u8.write(writer)?;
+                                       htlc_state.write(writer)?;
+                               },
+                               &InboundHTLCState::AwaitingAnnouncedRemoteRevoke(ref htlc_state) => {
+                                       2u8.write(writer)?;
+                                       htlc_state.write(writer)?;
+                               },
+                               &InboundHTLCState::Committed => {
+                                       3u8.write(writer)?;
+                               },
+                               &InboundHTLCState::LocalRemoved(ref removal_reason) => {
+                                       4u8.write(writer)?;
+                                       removal_reason.write(writer)?;
+                               },
+                       }
+               }
+
+               macro_rules! write_option {
+                       ($thing: expr) => {
+                               match &$thing {
+                                       &None => 0u8.write(writer)?,
+                                       &Some(ref v) => {
+                                               1u8.write(writer)?;
+                                               v.write(writer)?;
+                                       },
+                               }
+                       }
+               }
+
+               (self.pending_outbound_htlcs.len() as u64).write(writer)?;
+               for htlc in self.pending_outbound_htlcs.iter() {
+                       htlc.htlc_id.write(writer)?;
+                       htlc.amount_msat.write(writer)?;
+                       htlc.cltv_expiry.write(writer)?;
+                       htlc.payment_hash.write(writer)?;
+                       htlc.source.write(writer)?;
+                       match &htlc.state {
+                               &OutboundHTLCState::LocalAnnounced(ref onion_packet) => {
+                                       0u8.write(writer)?;
+                                       onion_packet.write(writer)?;
+                               },
+                               &OutboundHTLCState::Committed => {
+                                       1u8.write(writer)?;
+                               },
+                               &OutboundHTLCState::RemoteRemoved(ref fail_reason) => {
+                                       2u8.write(writer)?;
+                                       write_option!(*fail_reason);
+                               },
+                               &OutboundHTLCState::AwaitingRemoteRevokeToRemove(ref fail_reason) => {
+                                       3u8.write(writer)?;
+                                       write_option!(*fail_reason);
+                               },
+                               &OutboundHTLCState::AwaitingRemovedRemoteRevoke(ref fail_reason) => {
+                                       4u8.write(writer)?;
+                                       write_option!(*fail_reason);
+                               },
+                       }
+               }
+
+               (self.holding_cell_htlc_updates.len() as u64).write(writer)?;
+               for update in self.holding_cell_htlc_updates.iter() {
+                       match update {
+                               &HTLCUpdateAwaitingACK::AddHTLC { ref amount_msat, ref cltv_expiry, ref payment_hash, ref source, ref onion_routing_packet } => {
+                                       0u8.write(writer)?;
+                                       amount_msat.write(writer)?;
+                                       cltv_expiry.write(writer)?;
+                                       payment_hash.write(writer)?;
+                                       source.write(writer)?;
+                                       onion_routing_packet.write(writer)?;
+                               },
+                               &HTLCUpdateAwaitingACK::ClaimHTLC { ref payment_preimage, ref htlc_id } => {
+                                       1u8.write(writer)?;
+                                       payment_preimage.write(writer)?;
+                                       htlc_id.write(writer)?;
+                               },
+                               &HTLCUpdateAwaitingACK::FailHTLC { ref htlc_id, ref err_packet } => {
+                                       2u8.write(writer)?;
+                                       htlc_id.write(writer)?;
+                                       err_packet.write(writer)?;
+                               }
+                       }
+               }
+
+               match self.resend_order {
+                       RAACommitmentOrder::CommitmentFirst => 0u8.write(writer)?,
+                       RAACommitmentOrder::RevokeAndACKFirst => 1u8.write(writer)?,
+               }
+
+               self.monitor_pending_funding_locked.write(writer)?;
+               self.monitor_pending_revoke_and_ack.write(writer)?;
+               self.monitor_pending_commitment_signed.write(writer)?;
+
+               (self.monitor_pending_forwards.len() as u64).write(writer)?;
+               for &(ref pending_forward, ref htlc_id) in self.monitor_pending_forwards.iter() {
+                       pending_forward.write(writer)?;
+                       htlc_id.write(writer)?;
+               }
+
+               (self.monitor_pending_failures.len() as u64).write(writer)?;
+               for &(ref htlc_source, ref payment_hash, ref fail_reason) in self.monitor_pending_failures.iter() {
+                       htlc_source.write(writer)?;
+                       payment_hash.write(writer)?;
+                       fail_reason.write(writer)?;
+               }
+
+               write_option!(self.pending_update_fee);
+               write_option!(self.holding_cell_update_fee);
+
+               self.next_local_htlc_id.write(writer)?;
+               (self.next_remote_htlc_id - dropped_inbound_htlcs).write(writer)?;
+               self.channel_update_count.write(writer)?;
+               self.feerate_per_kw.write(writer)?;
+
+               (self.last_local_commitment_txn.len() as u64).write(writer)?;
+               for tx in self.last_local_commitment_txn.iter() {
+                       if let Err(e) = tx.consensus_encode(&mut WriterWriteAdaptor(writer)) {
+                               match e {
+                                       encode::Error::Io(e) => return Err(e),
+                                       _ => panic!("last_local_commitment_txn must have been well-formed!"),
+                               }
+                       }
+               }
+
+               match self.last_sent_closing_fee {
+                       Some((feerate, fee)) => {
+                               1u8.write(writer)?;
+                               feerate.write(writer)?;
+                               fee.write(writer)?;
+                       },
+                       None => 0u8.write(writer)?,
+               }
+
+               write_option!(self.funding_tx_confirmed_in);
+               write_option!(self.short_channel_id);
+
+               self.last_block_connected.write(writer)?;
+               self.funding_tx_confirmations.write(writer)?;
+
+               self.their_dust_limit_satoshis.write(writer)?;
+               self.our_dust_limit_satoshis.write(writer)?;
+               self.their_max_htlc_value_in_flight_msat.write(writer)?;
+               self.their_channel_reserve_satoshis.write(writer)?;
+               self.their_htlc_minimum_msat.write(writer)?;
+               self.our_htlc_minimum_msat.write(writer)?;
+               self.their_to_self_delay.write(writer)?;
+               self.our_to_self_delay.write(writer)?;
+               self.their_max_accepted_htlcs.write(writer)?;
+               self.minimum_depth.write(writer)?;
+
+               write_option!(self.their_funding_pubkey);
+               write_option!(self.their_revocation_basepoint);
+               write_option!(self.their_payment_basepoint);
+               write_option!(self.their_delayed_payment_basepoint);
+               write_option!(self.their_htlc_basepoint);
+               write_option!(self.their_cur_commitment_point);
+
+               write_option!(self.their_prev_commitment_point);
+               self.their_node_id.write(writer)?;
+
+               write_option!(self.their_shutdown_scriptpubkey);
+
+               self.channel_monitor.write_for_disk(writer)?;
+               Ok(())
+       }
+}
+
+impl<R : ::std::io::Read> ReadableArgs<R, Arc<Logger>> for Channel {
+       fn read(reader: &mut R, logger: Arc<Logger>) -> Result<Self, DecodeError> {
+               let _ver: u8 = Readable::read(reader)?;
+               let min_ver: u8 = Readable::read(reader)?;
+               if min_ver > SERIALIZATION_VERSION {
+                       return Err(DecodeError::UnknownVersion);
+               }
+
+               let user_id = Readable::read(reader)?;
+               let config: ChannelConfig = Readable::read(reader)?;
+
+               let channel_id = Readable::read(reader)?;
+               let channel_state = Readable::read(reader)?;
+               let channel_outbound = Readable::read(reader)?;
+               let channel_value_satoshis = Readable::read(reader)?;
+
+               let local_keys = Readable::read(reader)?;
+               let shutdown_pubkey = Readable::read(reader)?;
+
+               let cur_local_commitment_transaction_number = Readable::read(reader)?;
+               let cur_remote_commitment_transaction_number = Readable::read(reader)?;
+               let value_to_self_msat = Readable::read(reader)?;
+
+               let pending_inbound_htlc_count: u64 = Readable::read(reader)?;
+               let mut pending_inbound_htlcs = Vec::with_capacity(cmp::min(pending_inbound_htlc_count as usize, OUR_MAX_HTLCS as usize));
+               for _ in 0..pending_inbound_htlc_count {
+                       pending_inbound_htlcs.push(InboundHTLCOutput {
+                               htlc_id: Readable::read(reader)?,
+                               amount_msat: Readable::read(reader)?,
+                               cltv_expiry: Readable::read(reader)?,
+                               payment_hash: Readable::read(reader)?,
+                               state: match <u8 as Readable<R>>::read(reader)? {
+                                       1 => InboundHTLCState::AwaitingRemoteRevokeToAnnounce(Readable::read(reader)?),
+                                       2 => InboundHTLCState::AwaitingAnnouncedRemoteRevoke(Readable::read(reader)?),
+                                       3 => InboundHTLCState::Committed,
+                                       4 => InboundHTLCState::LocalRemoved(Readable::read(reader)?),
+                                       _ => return Err(DecodeError::InvalidValue),
+                               },
+                       });
+               }
+
+               let pending_outbound_htlc_count: u64 = Readable::read(reader)?;
+               let mut pending_outbound_htlcs = Vec::with_capacity(cmp::min(pending_outbound_htlc_count as usize, OUR_MAX_HTLCS as usize));
+               for _ in 0..pending_outbound_htlc_count {
+                       pending_outbound_htlcs.push(OutboundHTLCOutput {
+                               htlc_id: Readable::read(reader)?,
+                               amount_msat: Readable::read(reader)?,
+                               cltv_expiry: Readable::read(reader)?,
+                               payment_hash: Readable::read(reader)?,
+                               source: Readable::read(reader)?,
+                               state: match <u8 as Readable<R>>::read(reader)? {
+                                       0 => OutboundHTLCState::LocalAnnounced(Box::new(Readable::read(reader)?)),
+                                       1 => OutboundHTLCState::Committed,
+                                       2 => OutboundHTLCState::RemoteRemoved(Readable::read(reader)?),
+                                       3 => OutboundHTLCState::AwaitingRemoteRevokeToRemove(Readable::read(reader)?),
+                                       4 => OutboundHTLCState::AwaitingRemovedRemoteRevoke(Readable::read(reader)?),
+                                       _ => return Err(DecodeError::InvalidValue),
+                               },
+                       });
+               }
+
+               let holding_cell_htlc_update_count: u64 = Readable::read(reader)?;
+               let mut holding_cell_htlc_updates = Vec::with_capacity(cmp::min(holding_cell_htlc_update_count as usize, OUR_MAX_HTLCS as usize*2));
+               for _ in 0..holding_cell_htlc_update_count {
+                       holding_cell_htlc_updates.push(match <u8 as Readable<R>>::read(reader)? {
+                               0 => HTLCUpdateAwaitingACK::AddHTLC {
+                                       amount_msat: Readable::read(reader)?,
+                                       cltv_expiry: Readable::read(reader)?,
+                                       payment_hash: Readable::read(reader)?,
+                                       source: Readable::read(reader)?,
+                                       onion_routing_packet: Readable::read(reader)?,
+                               },
+                               1 => HTLCUpdateAwaitingACK::ClaimHTLC {
+                                       payment_preimage: Readable::read(reader)?,
+                                       htlc_id: Readable::read(reader)?,
+                               },
+                               2 => HTLCUpdateAwaitingACK::FailHTLC {
+                                       htlc_id: Readable::read(reader)?,
+                                       err_packet: Readable::read(reader)?,
+                               },
+                               _ => return Err(DecodeError::InvalidValue),
+                       });
+               }
+
+               let resend_order = match <u8 as Readable<R>>::read(reader)? {
+                       0 => RAACommitmentOrder::CommitmentFirst,
+                       1 => RAACommitmentOrder::RevokeAndACKFirst,
+                       _ => return Err(DecodeError::InvalidValue),
+               };
+
+               let monitor_pending_funding_locked = Readable::read(reader)?;
+               let monitor_pending_revoke_and_ack = Readable::read(reader)?;
+               let monitor_pending_commitment_signed = Readable::read(reader)?;
+
+               let monitor_pending_forwards_count: u64 = Readable::read(reader)?;
+               let mut monitor_pending_forwards = Vec::with_capacity(cmp::min(monitor_pending_forwards_count as usize, OUR_MAX_HTLCS as usize));
+               for _ in 0..monitor_pending_forwards_count {
+                       monitor_pending_forwards.push((Readable::read(reader)?, Readable::read(reader)?));
+               }
+
+               let monitor_pending_failures_count: u64 = Readable::read(reader)?;
+               let mut monitor_pending_failures = Vec::with_capacity(cmp::min(monitor_pending_failures_count as usize, OUR_MAX_HTLCS as usize));
+               for _ in 0..monitor_pending_failures_count {
+                       monitor_pending_failures.push((Readable::read(reader)?, Readable::read(reader)?, Readable::read(reader)?));
+               }
+
+               let pending_update_fee = Readable::read(reader)?;
+               let holding_cell_update_fee = Readable::read(reader)?;
+
+               let next_local_htlc_id = Readable::read(reader)?;
+               let next_remote_htlc_id = Readable::read(reader)?;
+               let channel_update_count = Readable::read(reader)?;
+               let feerate_per_kw = Readable::read(reader)?;
+
+               let last_local_commitment_txn_count: u64 = Readable::read(reader)?;
+               let mut last_local_commitment_txn = Vec::with_capacity(cmp::min(last_local_commitment_txn_count as usize, OUR_MAX_HTLCS as usize*2 + 1));
+               for _ in 0..last_local_commitment_txn_count {
+                       last_local_commitment_txn.push(match Transaction::consensus_decode(reader.by_ref()) {
+                               Ok(tx) => tx,
+                               Err(_) => return Err(DecodeError::InvalidValue),
+                       });
+               }
+
+               let last_sent_closing_fee = match <u8 as Readable<R>>::read(reader)? {
+                       0 => None,
+                       1 => Some((Readable::read(reader)?, Readable::read(reader)?)),
+                       _ => return Err(DecodeError::InvalidValue),
+               };
+
+               let funding_tx_confirmed_in = Readable::read(reader)?;
+               let short_channel_id = Readable::read(reader)?;
+
+               let last_block_connected = Readable::read(reader)?;
+               let funding_tx_confirmations = Readable::read(reader)?;
+
+               let their_dust_limit_satoshis = Readable::read(reader)?;
+               let our_dust_limit_satoshis = Readable::read(reader)?;
+               let their_max_htlc_value_in_flight_msat = Readable::read(reader)?;
+               let their_channel_reserve_satoshis = Readable::read(reader)?;
+               let their_htlc_minimum_msat = Readable::read(reader)?;
+               let our_htlc_minimum_msat = Readable::read(reader)?;
+               let their_to_self_delay = Readable::read(reader)?;
+               let our_to_self_delay = Readable::read(reader)?;
+               let their_max_accepted_htlcs = Readable::read(reader)?;
+               let minimum_depth = Readable::read(reader)?;
+
+               let their_funding_pubkey = Readable::read(reader)?;
+               let their_revocation_basepoint = Readable::read(reader)?;
+               let their_payment_basepoint = Readable::read(reader)?;
+               let their_delayed_payment_basepoint = Readable::read(reader)?;
+               let their_htlc_basepoint = Readable::read(reader)?;
+               let their_cur_commitment_point = Readable::read(reader)?;
+
+               let their_prev_commitment_point = Readable::read(reader)?;
+               let their_node_id = Readable::read(reader)?;
+
+               let their_shutdown_scriptpubkey = Readable::read(reader)?;
+               let (monitor_last_block, channel_monitor) = ReadableArgs::read(reader, logger.clone())?;
+               // We drop the ChannelMonitor's last block connected hash cause we don't actually bother
+               // doing full block connection operations on the internal CHannelMonitor copies
+               if monitor_last_block != last_block_connected {
+                       return Err(DecodeError::InvalidValue);
+               }
+
+               Ok(Channel {
+                       user_id,
+
+                       config,
+                       channel_id,
+                       channel_state,
+                       channel_outbound,
+                       secp_ctx: Secp256k1::new(),
+                       channel_value_satoshis,
+
+                       local_keys,
+                       shutdown_pubkey,
+
+                       cur_local_commitment_transaction_number,
+                       cur_remote_commitment_transaction_number,
+                       value_to_self_msat,
+
+                       pending_inbound_htlcs,
+                       pending_outbound_htlcs,
+                       holding_cell_htlc_updates,
+
+                       resend_order,
+
+                       monitor_pending_funding_locked,
+                       monitor_pending_revoke_and_ack,
+                       monitor_pending_commitment_signed,
+                       monitor_pending_forwards,
+                       monitor_pending_failures,
+
+                       pending_update_fee,
+                       holding_cell_update_fee,
+                       next_local_htlc_id,
+                       next_remote_htlc_id,
+                       channel_update_count,
+                       feerate_per_kw,
+
+                       #[cfg(debug_assertions)]
+                       max_commitment_tx_output_local: ::std::sync::Mutex::new((0, 0)),
+                       #[cfg(debug_assertions)]
+                       max_commitment_tx_output_remote: ::std::sync::Mutex::new((0, 0)),
+
+                       last_local_commitment_txn,
+
+                       last_sent_closing_fee,
+
+                       funding_tx_confirmed_in,
+                       short_channel_id,
+                       last_block_connected,
+                       funding_tx_confirmations,
+
+                       their_dust_limit_satoshis,
+                       our_dust_limit_satoshis,
+                       their_max_htlc_value_in_flight_msat,
+                       their_channel_reserve_satoshis,
+                       their_htlc_minimum_msat,
+                       our_htlc_minimum_msat,
+                       their_to_self_delay,
+                       our_to_self_delay,
+                       their_max_accepted_htlcs,
+                       minimum_depth,
+
+                       their_funding_pubkey,
+                       their_revocation_basepoint,
+                       their_payment_basepoint,
+                       their_delayed_payment_basepoint,
+                       their_htlc_basepoint,
+                       their_cur_commitment_point,
+
+                       their_prev_commitment_point,
+                       their_node_id,
+
+                       their_shutdown_scriptpubkey,
+
+                       channel_monitor,
+
+                       logger,
+               })
+       }
+}
+
+#[cfg(test)]
+mod tests {
+       use bitcoin::util::bip143;
+       use bitcoin::consensus::encode::serialize;
+       use bitcoin::blockdata::script::{Script, Builder};
+       use bitcoin::blockdata::transaction::Transaction;
+       use bitcoin::blockdata::opcodes;
+       use bitcoin_hashes::hex::FromHex;
+       use hex;
+       use ln::channelmanager::{HTLCSource, PaymentPreimage, PaymentHash};
+       use ln::channel::{Channel,ChannelKeys,InboundHTLCOutput,OutboundHTLCOutput,InboundHTLCState,OutboundHTLCState,HTLCOutputInCommitment,TxCreationKeys};
+       use ln::channel::MAX_FUNDING_SATOSHIS;
+       use ln::chan_utils;
+       use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
+       use chain::keysinterface::KeysInterface;
+       use chain::transaction::OutPoint;
+       use util::config::UserConfig;
+       use util::test_utils;
+       use util::logger::Logger;
+       use secp256k1::{Secp256k1,Message,Signature};
+       use secp256k1::key::{SecretKey,PublicKey};
+       use bitcoin_hashes::sha256::Hash as Sha256;
+       use bitcoin_hashes::sha256d::Hash as Sha256dHash;
+       use bitcoin_hashes::hash160::Hash as Hash160;
+       use bitcoin_hashes::Hash;
+       use std::sync::Arc;
+
+       struct TestFeeEstimator {
+               fee_est: u64
+       }
+       impl FeeEstimator for TestFeeEstimator {
+               fn get_est_sat_per_1000_weight(&self, _: ConfirmationTarget) -> u64 {
+                       self.fee_est
+               }
+       }
+
+       #[test]
+       fn test_max_funding_satoshis() {
+               assert!(MAX_FUNDING_SATOSHIS <= 21_000_000 * 100_000_000,
+                       "MAX_FUNDING_SATOSHIS is greater than all satoshis in existence");
+       }
+
+       struct Keys {
+               chan_keys: ChannelKeys,
+       }
+       impl KeysInterface for Keys {
+               fn get_node_secret(&self) -> SecretKey { panic!(); }
+               fn get_destination_script(&self) -> Script {
+                       let secp_ctx = Secp256k1::signing_only();
+                       let channel_monitor_claim_key = SecretKey::from_slice(&hex::decode("0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap()[..]).unwrap();
+                       let our_channel_monitor_claim_key_hash = Hash160::hash(&PublicKey::from_secret_key(&secp_ctx, &channel_monitor_claim_key).serialize());
+                       Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(&our_channel_monitor_claim_key_hash[..]).into_script()
+               }
+
+               fn get_shutdown_pubkey(&self) -> PublicKey {
+                       let secp_ctx = Secp256k1::signing_only();
+                       let channel_close_key = SecretKey::from_slice(&hex::decode("0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap()[..]).unwrap();
+                       PublicKey::from_secret_key(&secp_ctx, &channel_close_key)
+               }
+
+               fn get_channel_keys(&self, _inbound: bool) -> ChannelKeys { self.chan_keys.clone() }
+               fn get_session_key(&self) -> SecretKey { panic!(); }
+               fn get_channel_id(&self) -> [u8; 32] { [0; 32] }
+       }
+
+       #[test]
+       fn outbound_commitment_test() {
+               // Test vectors from BOLT 3 Appendix C:
+               let feeest = TestFeeEstimator{fee_est: 15000};
+               let logger : Arc<Logger> = Arc::new(test_utils::TestLogger::new());
+               let secp_ctx = Secp256k1::new();
+
+               let chan_keys = ChannelKeys {
+                       funding_key: SecretKey::from_slice(&hex::decode("30ff4956bbdd3222d44cc5e8a1261dab1e07957bdac5ae88fe3261ef321f3749").unwrap()[..]).unwrap(),
+                       payment_base_key: SecretKey::from_slice(&hex::decode("1111111111111111111111111111111111111111111111111111111111111111").unwrap()[..]).unwrap(),
+                       delayed_payment_base_key: SecretKey::from_slice(&hex::decode("3333333333333333333333333333333333333333333333333333333333333333").unwrap()[..]).unwrap(),
+                       htlc_base_key: SecretKey::from_slice(&hex::decode("1111111111111111111111111111111111111111111111111111111111111111").unwrap()[..]).unwrap(),
+
+                       // These aren't set in the test vectors:
+                       revocation_base_key: SecretKey::from_slice(&hex::decode("0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap()[..]).unwrap(),
+                       commitment_seed: [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff],
+               };
+               assert_eq!(PublicKey::from_secret_key(&secp_ctx, &chan_keys.funding_key).serialize()[..],
+                               hex::decode("023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb").unwrap()[..]);
+               let keys_provider: Arc<KeysInterface> = Arc::new(Keys { chan_keys });
+
+               let their_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
+               let mut config = UserConfig::new();
+               config.channel_options.announced_channel = false;
+               let mut chan = Channel::new_outbound(&feeest, &keys_provider, their_node_id, 10000000, 100000, 42, Arc::clone(&logger), &config).unwrap(); // Nothing uses their network key in this test
+               chan.their_to_self_delay = 144;
+               chan.our_dust_limit_satoshis = 546;
+
+               let funding_info = OutPoint::new(Sha256dHash::from_hex("8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be").unwrap(), 0);
+               chan.channel_monitor.set_funding_info((funding_info, Script::new()));
+
+               chan.their_payment_basepoint = Some(PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("4444444444444444444444444444444444444444444444444444444444444444").unwrap()[..]).unwrap()));
+               assert_eq!(chan.their_payment_basepoint.unwrap().serialize()[..],
+                               hex::decode("032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991").unwrap()[..]);
+
+               chan.their_funding_pubkey = Some(PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("1552dfba4f6cf29a62a0af13c8d6981d36d0ef8d61ba10fb0fe90da7634d7e13").unwrap()[..]).unwrap()));
+               assert_eq!(chan.their_funding_pubkey.unwrap().serialize()[..],
+                               hex::decode("030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c1").unwrap()[..]);
+
+               chan.their_htlc_basepoint = Some(PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("4444444444444444444444444444444444444444444444444444444444444444").unwrap()[..]).unwrap()));
+               assert_eq!(chan.their_htlc_basepoint.unwrap().serialize()[..],
+                               hex::decode("032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991").unwrap()[..]);
+
+               chan.their_revocation_basepoint = Some(PublicKey::from_slice(&hex::decode("02466d7fcae563e5cb09a0d1870bb580344804617879a14949cf22285f1bae3f27").unwrap()[..]).unwrap());
+
+               // We can't just use build_local_transaction_keys here as the per_commitment_secret is not
+               // derived from a commitment_seed, so instead we copy it here and call
+               // build_commitment_transaction.
+               let delayed_payment_base = PublicKey::from_secret_key(&secp_ctx, &chan.local_keys.delayed_payment_base_key);
+               let per_commitment_secret = SecretKey::from_slice(&hex::decode("1f1e1d1c1b1a191817161514131211100f0e0d0c0b0a09080706050403020100").unwrap()[..]).unwrap();
+               let per_commitment_point = PublicKey::from_secret_key(&secp_ctx, &per_commitment_secret);
+               let htlc_basepoint = PublicKey::from_secret_key(&secp_ctx, &chan.local_keys.htlc_base_key);
+               let keys = TxCreationKeys::new(&secp_ctx, &per_commitment_point, &delayed_payment_base, &htlc_basepoint, &chan.their_revocation_basepoint.unwrap(), &chan.their_payment_basepoint.unwrap(), &chan.their_htlc_basepoint.unwrap()).unwrap();
+
+               let mut unsigned_tx: (Transaction, Vec<HTLCOutputInCommitment>);
+
+               macro_rules! test_commitment {
+                       ( $their_sig_hex: expr, $our_sig_hex: expr, $tx_hex: expr) => {
+                               unsigned_tx = {
+                                       let mut res = chan.build_commitment_transaction(0xffffffffffff - 42, &keys, true, false, chan.feerate_per_kw);
+                                       let htlcs = res.2.drain(..)
+                                               .filter_map(|(htlc, _)| if htlc.transaction_output_index.is_some() { Some(htlc) } else { None })
+                                               .collect();
+                                       (res.0, htlcs)
+                               };
+                               let their_signature = Signature::from_der(&hex::decode($their_sig_hex).unwrap()[..]).unwrap();
+                               let sighash = Message::from_slice(&bip143::SighashComponents::new(&unsigned_tx.0).sighash_all(&unsigned_tx.0.input[0], &chan.get_funding_redeemscript(), chan.channel_value_satoshis)[..]).unwrap();
+                               secp_ctx.verify(&sighash, &their_signature, &chan.their_funding_pubkey.unwrap()).unwrap();
+
+                               chan.sign_commitment_transaction(&mut unsigned_tx.0, &their_signature);
+
+                               assert_eq!(serialize(&unsigned_tx.0)[..],
+                                               hex::decode($tx_hex).unwrap()[..]);
+                       };
+               }
+
+               macro_rules! test_htlc_output {
+                       ( $htlc_idx: expr, $their_sig_hex: expr, $our_sig_hex: expr, $tx_hex: expr ) => {
+                               let remote_signature = Signature::from_der(&hex::decode($their_sig_hex).unwrap()[..]).unwrap();
+
+                               let ref htlc = unsigned_tx.1[$htlc_idx];
+                               let mut htlc_tx = chan.build_htlc_transaction(&unsigned_tx.0.txid(), &htlc, true, &keys, chan.feerate_per_kw);
+                               let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &keys);
+                               let htlc_sighash = Message::from_slice(&bip143::SighashComponents::new(&htlc_tx).sighash_all(&htlc_tx.input[0], &htlc_redeemscript, htlc.amount_msat / 1000)[..]).unwrap();
+                               secp_ctx.verify(&htlc_sighash, &remote_signature, &keys.b_htlc_key).unwrap();
+
+                               let mut preimage: Option<PaymentPreimage> = None;
+                               if !htlc.offered {
+                                       for i in 0..5 {
+                                               let out = PaymentHash(Sha256::hash(&[i; 32]).into_inner());
+                                               if out == htlc.payment_hash {
+                                                       preimage = Some(PaymentPreimage([i; 32]));
+                                               }
+                                       }
+
+                                       assert!(preimage.is_some());
+                               }
+
+                               chan.sign_htlc_transaction(&mut htlc_tx, &remote_signature, &preimage, &htlc, &keys).unwrap();
+                               assert_eq!(serialize(&htlc_tx)[..],
+                                               hex::decode($tx_hex).unwrap()[..]);
+                       };
+               }
+
+               {
+                       // simple commitment tx with no HTLCs
+                       chan.value_to_self_msat = 7000000000;
+
+                       test_commitment!("3045022100f51d2e566a70ba740fc5d8c0f07b9b93d2ed741c3c0860c613173de7d39e7968022041376d520e9c0e1ad52248ddf4b22e12be8763007df977253ef45a4ca3bdb7c0",
+                                        "3044022051b75c73198c6deee1a875871c3961832909acd297c6b908d59e3319e5185a46022055c419379c5051a78d00dbbce11b5b664a0c22815fbcc6fcef6b1937c3836939",
+                                        "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8002c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de84311054a56a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0400473044022051b75c73198c6deee1a875871c3961832909acd297c6b908d59e3319e5185a46022055c419379c5051a78d00dbbce11b5b664a0c22815fbcc6fcef6b1937c383693901483045022100f51d2e566a70ba740fc5d8c0f07b9b93d2ed741c3c0860c613173de7d39e7968022041376d520e9c0e1ad52248ddf4b22e12be8763007df977253ef45a4ca3bdb7c001475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220");
+               }
+
+               chan.pending_inbound_htlcs.push({
+                       let mut out = InboundHTLCOutput{
+                               htlc_id: 0,
+                               amount_msat: 1000000,
+                               cltv_expiry: 500,
+                               payment_hash: PaymentHash([0; 32]),
+                               state: InboundHTLCState::Committed,
+                       };
+                       out.payment_hash.0 = Sha256::hash(&hex::decode("0000000000000000000000000000000000000000000000000000000000000000").unwrap()).into_inner();
+                       out
+               });
+               chan.pending_inbound_htlcs.push({
+                       let mut out = InboundHTLCOutput{
+                               htlc_id: 1,
+                               amount_msat: 2000000,
+                               cltv_expiry: 501,
+                               payment_hash: PaymentHash([0; 32]),
+                               state: InboundHTLCState::Committed,
+                       };
+                       out.payment_hash.0 = Sha256::hash(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()).into_inner();
+                       out
+               });
+               chan.pending_outbound_htlcs.push({
+                       let mut out = OutboundHTLCOutput{
+                               htlc_id: 2,
+                               amount_msat: 2000000,
+                               cltv_expiry: 502,
+                               payment_hash: PaymentHash([0; 32]),
+                               state: OutboundHTLCState::Committed,
+                               source: HTLCSource::dummy(),
+                       };
+                       out.payment_hash.0 = Sha256::hash(&hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap()).into_inner();
+                       out
+               });
+               chan.pending_outbound_htlcs.push({
+                       let mut out = OutboundHTLCOutput{
+                               htlc_id: 3,
+                               amount_msat: 3000000,
+                               cltv_expiry: 503,
+                               payment_hash: PaymentHash([0; 32]),
+                               state: OutboundHTLCState::Committed,
+                               source: HTLCSource::dummy(),
+                       };
+                       out.payment_hash.0 = Sha256::hash(&hex::decode("0303030303030303030303030303030303030303030303030303030303030303").unwrap()).into_inner();
+                       out
+               });
+               chan.pending_inbound_htlcs.push({
+                       let mut out = InboundHTLCOutput{
+                               htlc_id: 4,
+                               amount_msat: 4000000,
+                               cltv_expiry: 504,
+                               payment_hash: PaymentHash([0; 32]),
+                               state: InboundHTLCState::Committed,
+                       };
+                       out.payment_hash.0 = Sha256::hash(&hex::decode("0404040404040404040404040404040404040404040404040404040404040404").unwrap()).into_inner();
+                       out
+               });
+
+               {
+                       // commitment tx with all five HTLCs untrimmed (minimum feerate)
+                       chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
+                       chan.feerate_per_kw = 0;
+
+                       test_commitment!("304402204fd4928835db1ccdfc40f5c78ce9bd65249b16348df81f0c44328dcdefc97d630220194d3869c38bc732dd87d13d2958015e2fc16829e74cd4377f84d215c0b70606",
+                                        "30440220275b0c325a5e9355650dc30c0eccfbc7efb23987c24b556b9dfdd40effca18d202206caceb2c067836c51f296740c7ae807ffcbfbf1dd3a0d56b6de9a5b247985f06",
+                                        "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8007e80300000000000022002052bfef0479d7b293c27e0f1eb294bea154c63a3294ef092c19af51409bce0e2ad007000000000000220020403d394747cae42e98ff01734ad5c08f82ba123d3d9a620abda88989651e2ab5d007000000000000220020748eba944fedc8827f6b06bc44678f93c0f9e6078b35c6331ed31e75f8ce0c2db80b000000000000220020c20b5d1f8584fd90443e7b7b720136174fa4b9333c261d04dbbd012635c0f419a00f0000000000002200208c48d15160397c9731df9bc3b236656efb6665fbfe92b4a6878e88a499f741c4c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de843110e0a06a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e04004730440220275b0c325a5e9355650dc30c0eccfbc7efb23987c24b556b9dfdd40effca18d202206caceb2c067836c51f296740c7ae807ffcbfbf1dd3a0d56b6de9a5b247985f060147304402204fd4928835db1ccdfc40f5c78ce9bd65249b16348df81f0c44328dcdefc97d630220194d3869c38bc732dd87d13d2958015e2fc16829e74cd4377f84d215c0b7060601475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220");
+
+                       assert_eq!(unsigned_tx.1.len(), 5);
+
+                       test_htlc_output!(0,
+                                         "304402206a6e59f18764a5bf8d4fa45eebc591566689441229c918b480fb2af8cc6a4aeb02205248f273be447684b33e3c8d1d85a8e0ca9fa0bae9ae33f0527ada9c162919a6",
+                                         "304402207cb324fa0de88f452ffa9389678127ebcf4cabe1dd848b8e076c1a1962bf34720220116ed922b12311bd602d67e60d2529917f21c5b82f25ff6506c0f87886b4dfd5",
+                                         "020000000001018154ecccf11a5fb56c39654c4deb4d2296f83c69268280b94d021370c94e219700000000000000000001e8030000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e050047304402206a6e59f18764a5bf8d4fa45eebc591566689441229c918b480fb2af8cc6a4aeb02205248f273be447684b33e3c8d1d85a8e0ca9fa0bae9ae33f0527ada9c162919a60147304402207cb324fa0de88f452ffa9389678127ebcf4cabe1dd848b8e076c1a1962bf34720220116ed922b12311bd602d67e60d2529917f21c5b82f25ff6506c0f87886b4dfd5012000000000000000000000000000000000000000000000000000000000000000008a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a914b8bcb07f6344b42ab04250c86a6e8b75d3fdbbc688527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f401b175ac686800000000");
+
+                       test_htlc_output!(1,
+                                         "3045022100d5275b3619953cb0c3b5aa577f04bc512380e60fa551762ce3d7a1bb7401cff9022037237ab0dac3fe100cde094e82e2bed9ba0ed1bb40154b48e56aa70f259e608b",
+                                         "3045022100c89172099507ff50f4c925e6c5150e871fb6e83dd73ff9fbb72f6ce829a9633f02203a63821d9162e99f9be712a68f9e589483994feae2661e4546cd5b6cec007be5",
+                                         "020000000001018154ecccf11a5fb56c39654c4deb4d2296f83c69268280b94d021370c94e219701000000000000000001d0070000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100d5275b3619953cb0c3b5aa577f04bc512380e60fa551762ce3d7a1bb7401cff9022037237ab0dac3fe100cde094e82e2bed9ba0ed1bb40154b48e56aa70f259e608b01483045022100c89172099507ff50f4c925e6c5150e871fb6e83dd73ff9fbb72f6ce829a9633f02203a63821d9162e99f9be712a68f9e589483994feae2661e4546cd5b6cec007be501008576a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c820120876475527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae67a914b43e1b38138a41b37f7cd9a1d274bc63e3a9b5d188ac6868f6010000");
+
+                       test_htlc_output!(2,
+                                         "304402201b63ec807771baf4fdff523c644080de17f1da478989308ad13a58b51db91d360220568939d38c9ce295adba15665fa68f51d967e8ed14a007b751540a80b325f202",
+                                         "3045022100def389deab09cee69eaa1ec14d9428770e45bcbe9feb46468ecf481371165c2f022015d2e3c46600b2ebba8dcc899768874cc6851fd1ecb3fffd15db1cc3de7e10da",
+                                         "020000000001018154ecccf11a5fb56c39654c4deb4d2296f83c69268280b94d021370c94e219702000000000000000001d0070000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e050047304402201b63ec807771baf4fdff523c644080de17f1da478989308ad13a58b51db91d360220568939d38c9ce295adba15665fa68f51d967e8ed14a007b751540a80b325f20201483045022100def389deab09cee69eaa1ec14d9428770e45bcbe9feb46468ecf481371165c2f022015d2e3c46600b2ebba8dcc899768874cc6851fd1ecb3fffd15db1cc3de7e10da012001010101010101010101010101010101010101010101010101010101010101018a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a9144b6b2e5444c2639cc0fb7bcea5afba3f3cdce23988527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f501b175ac686800000000");
+
+                       test_htlc_output!(3,
+                                         "3045022100daee1808f9861b6c3ecd14f7b707eca02dd6bdfc714ba2f33bc8cdba507bb182022026654bf8863af77d74f51f4e0b62d461a019561bb12acb120d3f7195d148a554",
+                                         "30440220643aacb19bbb72bd2b635bc3f7375481f5981bace78cdd8319b2988ffcc6704202203d27784ec8ad51ed3bd517a05525a5139bb0b755dd719e0054332d186ac08727",
+                                         "020000000001018154ecccf11a5fb56c39654c4deb4d2296f83c69268280b94d021370c94e219703000000000000000001b80b0000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100daee1808f9861b6c3ecd14f7b707eca02dd6bdfc714ba2f33bc8cdba507bb182022026654bf8863af77d74f51f4e0b62d461a019561bb12acb120d3f7195d148a554014730440220643aacb19bbb72bd2b635bc3f7375481f5981bace78cdd8319b2988ffcc6704202203d27784ec8ad51ed3bd517a05525a5139bb0b755dd719e0054332d186ac0872701008576a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c820120876475527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae67a9148a486ff2e31d6158bf39e2608864d63fefd09d5b88ac6868f7010000");
+
+                       test_htlc_output!(4,
+                                         "304402207e0410e45454b0978a623f36a10626ef17b27d9ad44e2760f98cfa3efb37924f0220220bd8acd43ecaa916a80bd4f919c495a2c58982ce7c8625153f8596692a801d",
+                                         "30440220549e80b4496803cbc4a1d09d46df50109f546d43fbbf86cd90b174b1484acd5402205f12a4f995cb9bded597eabfee195a285986aa6d93ae5bb72507ebc6a4e2349e",
+                                         "020000000001018154ecccf11a5fb56c39654c4deb4d2296f83c69268280b94d021370c94e219704000000000000000001a00f0000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e050047304402207e0410e45454b0978a623f36a10626ef17b27d9ad44e2760f98cfa3efb37924f0220220bd8acd43ecaa916a80bd4f919c495a2c58982ce7c8625153f8596692a801d014730440220549e80b4496803cbc4a1d09d46df50109f546d43fbbf86cd90b174b1484acd5402205f12a4f995cb9bded597eabfee195a285986aa6d93ae5bb72507ebc6a4e2349e012004040404040404040404040404040404040404040404040404040404040404048a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a91418bc1a114ccf9c052d3d23e28d3b0a9d1227434288527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f801b175ac686800000000");
+               }
+
+               {
+                       // commitment tx with seven outputs untrimmed (maximum feerate)
+                       chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
+                       chan.feerate_per_kw = 647;
+
+                       test_commitment!("3045022100a5c01383d3ec646d97e40f44318d49def817fcd61a0ef18008a665b3e151785502203e648efddd5838981ef55ec954be69c4a652d021e6081a100d034de366815e9b",
+                                        "304502210094bfd8f5572ac0157ec76a9551b6c5216a4538c07cd13a51af4a54cb26fa14320220768efce8ce6f4a5efac875142ff19237c011343670adf9c7ac69704a120d1163",
+                                        "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8007e80300000000000022002052bfef0479d7b293c27e0f1eb294bea154c63a3294ef092c19af51409bce0e2ad007000000000000220020403d394747cae42e98ff01734ad5c08f82ba123d3d9a620abda88989651e2ab5d007000000000000220020748eba944fedc8827f6b06bc44678f93c0f9e6078b35c6331ed31e75f8ce0c2db80b000000000000220020c20b5d1f8584fd90443e7b7b720136174fa4b9333c261d04dbbd012635c0f419a00f0000000000002200208c48d15160397c9731df9bc3b236656efb6665fbfe92b4a6878e88a499f741c4c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de843110e09c6a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e040048304502210094bfd8f5572ac0157ec76a9551b6c5216a4538c07cd13a51af4a54cb26fa14320220768efce8ce6f4a5efac875142ff19237c011343670adf9c7ac69704a120d116301483045022100a5c01383d3ec646d97e40f44318d49def817fcd61a0ef18008a665b3e151785502203e648efddd5838981ef55ec954be69c4a652d021e6081a100d034de366815e9b01475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220");
+
+                       assert_eq!(unsigned_tx.1.len(), 5);
+
+                       test_htlc_output!(0,
+                                         "30440220385a5afe75632f50128cbb029ee95c80156b5b4744beddc729ad339c9ca432c802202ba5f48550cad3379ac75b9b4fedb86a35baa6947f16ba5037fb8b11ab343740",
+                                         "304402205999590b8a79fa346e003a68fd40366397119b2b0cdf37b149968d6bc6fbcc4702202b1e1fb5ab7864931caed4e732c359e0fe3d86a548b557be2246efb1708d579a",
+                                         "020000000001018323148ce2419f21ca3d6780053747715832e18ac780931a514b187768882bb60000000000000000000122020000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e05004730440220385a5afe75632f50128cbb029ee95c80156b5b4744beddc729ad339c9ca432c802202ba5f48550cad3379ac75b9b4fedb86a35baa6947f16ba5037fb8b11ab3437400147304402205999590b8a79fa346e003a68fd40366397119b2b0cdf37b149968d6bc6fbcc4702202b1e1fb5ab7864931caed4e732c359e0fe3d86a548b557be2246efb1708d579a012000000000000000000000000000000000000000000000000000000000000000008a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a914b8bcb07f6344b42ab04250c86a6e8b75d3fdbbc688527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f401b175ac686800000000");
+
+                       test_htlc_output!(1,
+                                         "304402207ceb6678d4db33d2401fdc409959e57c16a6cb97a30261d9c61f29b8c58d34b90220084b4a17b4ca0e86f2d798b3698ca52de5621f2ce86f80bed79afa66874511b0",
+                                         "304402207ff03eb0127fc7c6cae49cc29e2a586b98d1e8969cf4a17dfa50b9c2647720b902205e2ecfda2252956c0ca32f175080e75e4e390e433feb1f8ce9f2ba55648a1dac",
+                                         "020000000001018323148ce2419f21ca3d6780053747715832e18ac780931a514b187768882bb60100000000000000000124060000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e050047304402207ceb6678d4db33d2401fdc409959e57c16a6cb97a30261d9c61f29b8c58d34b90220084b4a17b4ca0e86f2d798b3698ca52de5621f2ce86f80bed79afa66874511b00147304402207ff03eb0127fc7c6cae49cc29e2a586b98d1e8969cf4a17dfa50b9c2647720b902205e2ecfda2252956c0ca32f175080e75e4e390e433feb1f8ce9f2ba55648a1dac01008576a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c820120876475527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae67a914b43e1b38138a41b37f7cd9a1d274bc63e3a9b5d188ac6868f6010000");
+
+                       test_htlc_output!(2,
+                                         "304402206a401b29a0dff0d18ec903502c13d83e7ec019450113f4a7655a4ce40d1f65ba0220217723a084e727b6ca0cc8b6c69c014a7e4a01fcdcba3e3993f462a3c574d833",
+                                         "3045022100d50d067ca625d54e62df533a8f9291736678d0b86c28a61bb2a80cf42e702d6e02202373dde7e00218eacdafb9415fe0e1071beec1857d1af3c6a201a44cbc47c877",
+                                         "020000000001018323148ce2419f21ca3d6780053747715832e18ac780931a514b187768882bb6020000000000000000010a060000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e050047304402206a401b29a0dff0d18ec903502c13d83e7ec019450113f4a7655a4ce40d1f65ba0220217723a084e727b6ca0cc8b6c69c014a7e4a01fcdcba3e3993f462a3c574d83301483045022100d50d067ca625d54e62df533a8f9291736678d0b86c28a61bb2a80cf42e702d6e02202373dde7e00218eacdafb9415fe0e1071beec1857d1af3c6a201a44cbc47c877012001010101010101010101010101010101010101010101010101010101010101018a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a9144b6b2e5444c2639cc0fb7bcea5afba3f3cdce23988527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f501b175ac686800000000");
+
+                       test_htlc_output!(3,
+                                         "30450221009b1c987ba599ee3bde1dbca776b85481d70a78b681a8d84206723e2795c7cac002207aac84ad910f8598c4d1c0ea2e3399cf6627a4e3e90131315bc9f038451ce39d",
+                                         "3045022100db9dc65291077a52728c622987e9895b7241d4394d6dcb916d7600a3e8728c22022036ee3ee717ba0bb5c45ee84bc7bbf85c0f90f26ae4e4a25a6b4241afa8a3f1cb",
+                                         "020000000001018323148ce2419f21ca3d6780053747715832e18ac780931a514b187768882bb6030000000000000000010c0a0000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e05004830450221009b1c987ba599ee3bde1dbca776b85481d70a78b681a8d84206723e2795c7cac002207aac84ad910f8598c4d1c0ea2e3399cf6627a4e3e90131315bc9f038451ce39d01483045022100db9dc65291077a52728c622987e9895b7241d4394d6dcb916d7600a3e8728c22022036ee3ee717ba0bb5c45ee84bc7bbf85c0f90f26ae4e4a25a6b4241afa8a3f1cb01008576a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c820120876475527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae67a9148a486ff2e31d6158bf39e2608864d63fefd09d5b88ac6868f7010000");
+
+                       test_htlc_output!(4,
+                                         "3045022100cc28030b59f0914f45b84caa983b6f8effa900c952310708c2b5b00781117022022027ba2ccdf94d03c6d48b327f183f6e28c8a214d089b9227f94ac4f85315274f0",
+                                         "304402202d1a3c0d31200265d2a2def2753ead4959ae20b4083e19553acfffa5dfab60bf022020ede134149504e15b88ab261a066de49848411e15e70f9e6a5462aec2949f8f",
+                                         "020000000001018323148ce2419f21ca3d6780053747715832e18ac780931a514b187768882bb604000000000000000001da0d0000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100cc28030b59f0914f45b84caa983b6f8effa900c952310708c2b5b00781117022022027ba2ccdf94d03c6d48b327f183f6e28c8a214d089b9227f94ac4f85315274f00147304402202d1a3c0d31200265d2a2def2753ead4959ae20b4083e19553acfffa5dfab60bf022020ede134149504e15b88ab261a066de49848411e15e70f9e6a5462aec2949f8f012004040404040404040404040404040404040404040404040404040404040404048a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a91418bc1a114ccf9c052d3d23e28d3b0a9d1227434288527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f801b175ac686800000000");
+               }
+
+               {
+                       // commitment tx with six outputs untrimmed (minimum feerate)
+                       chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
+                       chan.feerate_per_kw = 648;
+
+                       test_commitment!("3044022072714e2fbb93cdd1c42eb0828b4f2eff143f717d8f26e79d6ada4f0dcb681bbe02200911be4e5161dd6ebe59ff1c58e1997c4aea804f81db6b698821db6093d7b057",
+                                        "3045022100a2270d5950c89ae0841233f6efea9c951898b301b2e89e0adbd2c687b9f32efa02207943d90f95b9610458e7c65a576e149750ff3accaacad004cd85e70b235e27de",
+                                        "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8006d007000000000000220020403d394747cae42e98ff01734ad5c08f82ba123d3d9a620abda88989651e2ab5d007000000000000220020748eba944fedc8827f6b06bc44678f93c0f9e6078b35c6331ed31e75f8ce0c2db80b000000000000220020c20b5d1f8584fd90443e7b7b720136174fa4b9333c261d04dbbd012635c0f419a00f0000000000002200208c48d15160397c9731df9bc3b236656efb6665fbfe92b4a6878e88a499f741c4c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de8431104e9d6a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0400483045022100a2270d5950c89ae0841233f6efea9c951898b301b2e89e0adbd2c687b9f32efa02207943d90f95b9610458e7c65a576e149750ff3accaacad004cd85e70b235e27de01473044022072714e2fbb93cdd1c42eb0828b4f2eff143f717d8f26e79d6ada4f0dcb681bbe02200911be4e5161dd6ebe59ff1c58e1997c4aea804f81db6b698821db6093d7b05701475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220");
+
+                       assert_eq!(unsigned_tx.1.len(), 4);
+
+                       test_htlc_output!(0,
+                                         "3044022062ef2e77591409d60d7817d9bb1e71d3c4a2931d1a6c7c8307422c84f001a251022022dad9726b0ae3fe92bda745a06f2c00f92342a186d84518588cf65f4dfaada8",
+                                         "3045022100a4c574f00411dd2f978ca5cdc1b848c311cd7849c087ad2f21a5bce5e8cc5ae90220090ae39a9bce2fb8bc879d7e9f9022df249f41e25e51f1a9bf6447a9eeffc098",
+                                         "02000000000101579c183eca9e8236a5d7f5dcd79cfec32c497fdc0ec61533cde99ecd436cadd10000000000000000000123060000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500473044022062ef2e77591409d60d7817d9bb1e71d3c4a2931d1a6c7c8307422c84f001a251022022dad9726b0ae3fe92bda745a06f2c00f92342a186d84518588cf65f4dfaada801483045022100a4c574f00411dd2f978ca5cdc1b848c311cd7849c087ad2f21a5bce5e8cc5ae90220090ae39a9bce2fb8bc879d7e9f9022df249f41e25e51f1a9bf6447a9eeffc09801008576a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c820120876475527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae67a914b43e1b38138a41b37f7cd9a1d274bc63e3a9b5d188ac6868f6010000");
+
+                       test_htlc_output!(1,
+                                         "3045022100e968cbbb5f402ed389fdc7f6cd2a80ed650bb42c79aeb2a5678444af94f6c78502204b47a1cb24ab5b0b6fe69fe9cfc7dba07b9dd0d8b95f372c1d9435146a88f8d4",
+                                         "304402207679cf19790bea76a733d2fa0672bd43ab455687a068f815a3d237581f57139a0220683a1a799e102071c206b207735ca80f627ab83d6616b4bcd017c5d79ef3e7d0",
+                                         "02000000000101579c183eca9e8236a5d7f5dcd79cfec32c497fdc0ec61533cde99ecd436cadd10100000000000000000109060000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100e968cbbb5f402ed389fdc7f6cd2a80ed650bb42c79aeb2a5678444af94f6c78502204b47a1cb24ab5b0b6fe69fe9cfc7dba07b9dd0d8b95f372c1d9435146a88f8d40147304402207679cf19790bea76a733d2fa0672bd43ab455687a068f815a3d237581f57139a0220683a1a799e102071c206b207735ca80f627ab83d6616b4bcd017c5d79ef3e7d0012001010101010101010101010101010101010101010101010101010101010101018a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a9144b6b2e5444c2639cc0fb7bcea5afba3f3cdce23988527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f501b175ac686800000000");
+
+                       test_htlc_output!(2,
+                                         "3045022100aa91932e305292cf9969cc23502bbf6cef83a5df39c95ad04a707c4f4fed5c7702207099fc0f3a9bfe1e7683c0e9aa5e76c5432eb20693bf4cb182f04d383dc9c8c2",
+                                         "304402200df76fea718745f3c529bac7fd37923e7309ce38b25c0781e4cf514dd9ef8dc802204172295739dbae9fe0474dcee3608e3433b4b2af3a2e6787108b02f894dcdda3",
+                                         "02000000000101579c183eca9e8236a5d7f5dcd79cfec32c497fdc0ec61533cde99ecd436cadd1020000000000000000010b0a0000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100aa91932e305292cf9969cc23502bbf6cef83a5df39c95ad04a707c4f4fed5c7702207099fc0f3a9bfe1e7683c0e9aa5e76c5432eb20693bf4cb182f04d383dc9c8c20147304402200df76fea718745f3c529bac7fd37923e7309ce38b25c0781e4cf514dd9ef8dc802204172295739dbae9fe0474dcee3608e3433b4b2af3a2e6787108b02f894dcdda301008576a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c820120876475527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae67a9148a486ff2e31d6158bf39e2608864d63fefd09d5b88ac6868f7010000");
+
+                       test_htlc_output!(3,
+                                         "3044022035cac88040a5bba420b1c4257235d5015309113460bc33f2853cd81ca36e632402202fc94fd3e81e9d34a9d01782a0284f3044370d03d60f3fc041e2da088d2de58f",
+                                         "304402200daf2eb7afd355b4caf6fb08387b5f031940ea29d1a9f35071288a839c9039e4022067201b562456e7948616c13acb876b386b511599b58ac1d94d127f91c50463a6",
+                                         "02000000000101579c183eca9e8236a5d7f5dcd79cfec32c497fdc0ec61533cde99ecd436cadd103000000000000000001d90d0000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500473044022035cac88040a5bba420b1c4257235d5015309113460bc33f2853cd81ca36e632402202fc94fd3e81e9d34a9d01782a0284f3044370d03d60f3fc041e2da088d2de58f0147304402200daf2eb7afd355b4caf6fb08387b5f031940ea29d1a9f35071288a839c9039e4022067201b562456e7948616c13acb876b386b511599b58ac1d94d127f91c50463a6012004040404040404040404040404040404040404040404040404040404040404048a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a91418bc1a114ccf9c052d3d23e28d3b0a9d1227434288527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f801b175ac686800000000");
+               }
+
+               {
+                       // commitment tx with six outputs untrimmed (maximum feerate)
+                       chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
+                       chan.feerate_per_kw = 2069;
+
+                       test_commitment!("3044022001d55e488b8b035b2dd29d50b65b530923a416d47f377284145bc8767b1b6a75022019bb53ddfe1cefaf156f924777eaaf8fdca1810695a7d0a247ad2afba8232eb4",
+                                        "304402203ca8f31c6a47519f83255dc69f1894d9a6d7476a19f498d31eaf0cd3a85eeb63022026fd92dc752b33905c4c838c528b692a8ad4ced959990b5d5ee2ff940fa90eea",
+                                        "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8006d007000000000000220020403d394747cae42e98ff01734ad5c08f82ba123d3d9a620abda88989651e2ab5d007000000000000220020748eba944fedc8827f6b06bc44678f93c0f9e6078b35c6331ed31e75f8ce0c2db80b000000000000220020c20b5d1f8584fd90443e7b7b720136174fa4b9333c261d04dbbd012635c0f419a00f0000000000002200208c48d15160397c9731df9bc3b236656efb6665fbfe92b4a6878e88a499f741c4c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de84311077956a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e040047304402203ca8f31c6a47519f83255dc69f1894d9a6d7476a19f498d31eaf0cd3a85eeb63022026fd92dc752b33905c4c838c528b692a8ad4ced959990b5d5ee2ff940fa90eea01473044022001d55e488b8b035b2dd29d50b65b530923a416d47f377284145bc8767b1b6a75022019bb53ddfe1cefaf156f924777eaaf8fdca1810695a7d0a247ad2afba8232eb401475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220");
+
+                       assert_eq!(unsigned_tx.1.len(), 4);
+
+                       test_htlc_output!(0,
+                                         "3045022100d1cf354de41c1369336cf85b225ed033f1f8982a01be503668df756a7e668b66022001254144fb4d0eecc61908fccc3388891ba17c5d7a1a8c62bdd307e5a513f992",
+                                         "3044022056eb1af429660e45a1b0b66568cb8c4a3aa7e4c9c292d5d6c47f86ebf2c8838f022065c3ac4ebe980ca7a41148569be4ad8751b0a724a41405697ec55035dae66402",
+                                         "02000000000101ca94a9ad516ebc0c4bdd7b6254871babfa978d5accafb554214137d398bfcf6a0000000000000000000175020000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100d1cf354de41c1369336cf85b225ed033f1f8982a01be503668df756a7e668b66022001254144fb4d0eecc61908fccc3388891ba17c5d7a1a8c62bdd307e5a513f99201473044022056eb1af429660e45a1b0b66568cb8c4a3aa7e4c9c292d5d6c47f86ebf2c8838f022065c3ac4ebe980ca7a41148569be4ad8751b0a724a41405697ec55035dae6640201008576a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c820120876475527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae67a914b43e1b38138a41b37f7cd9a1d274bc63e3a9b5d188ac6868f6010000");
+
+                       test_htlc_output!(1,
+                                         "3045022100d065569dcb94f090345402736385efeb8ea265131804beac06dd84d15dd2d6880220664feb0b4b2eb985fadb6ec7dc58c9334ea88ce599a9be760554a2d4b3b5d9f4",
+                                         "3045022100914bb232cd4b2690ee3d6cb8c3713c4ac9c4fb925323068d8b07f67c8541f8d9022057152f5f1615b793d2d45aac7518989ae4fe970f28b9b5c77504799d25433f7f",
+                                         "02000000000101ca94a9ad516ebc0c4bdd7b6254871babfa978d5accafb554214137d398bfcf6a0100000000000000000122020000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100d065569dcb94f090345402736385efeb8ea265131804beac06dd84d15dd2d6880220664feb0b4b2eb985fadb6ec7dc58c9334ea88ce599a9be760554a2d4b3b5d9f401483045022100914bb232cd4b2690ee3d6cb8c3713c4ac9c4fb925323068d8b07f67c8541f8d9022057152f5f1615b793d2d45aac7518989ae4fe970f28b9b5c77504799d25433f7f012001010101010101010101010101010101010101010101010101010101010101018a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a9144b6b2e5444c2639cc0fb7bcea5afba3f3cdce23988527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f501b175ac686800000000");
+
+                       test_htlc_output!(2,
+                                         "3045022100d4e69d363de993684eae7b37853c40722a4c1b4a7b588ad7b5d8a9b5006137a102207a069c628170ee34be5612747051bdcc087466dbaa68d5756ea81c10155aef18",
+                                         "304402200e362443f7af830b419771e8e1614fc391db3a4eb799989abfc5ab26d6fcd032022039ab0cad1c14dfbe9446bf847965e56fe016e0cbcf719fd18c1bfbf53ecbd9f9",
+                                         "02000000000101ca94a9ad516ebc0c4bdd7b6254871babfa978d5accafb554214137d398bfcf6a020000000000000000015d060000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100d4e69d363de993684eae7b37853c40722a4c1b4a7b588ad7b5d8a9b5006137a102207a069c628170ee34be5612747051bdcc087466dbaa68d5756ea81c10155aef180147304402200e362443f7af830b419771e8e1614fc391db3a4eb799989abfc5ab26d6fcd032022039ab0cad1c14dfbe9446bf847965e56fe016e0cbcf719fd18c1bfbf53ecbd9f901008576a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c820120876475527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae67a9148a486ff2e31d6158bf39e2608864d63fefd09d5b88ac6868f7010000");
+
+                       test_htlc_output!(3,
+                                         "30450221008ec888e36e4a4b3dc2ed6b823319855b2ae03006ca6ae0d9aa7e24bfc1d6f07102203b0f78885472a67ff4fe5916c0bb669487d659527509516fc3a08e87a2cc0a7c",
+                                         "304402202c3e14282b84b02705dfd00a6da396c9fe8a8bcb1d3fdb4b20a4feba09440e8b02202b058b39aa9b0c865b22095edcd9ff1f71bbfe20aa4993755e54d042755ed0d5",
+                                         "02000000000101ca94a9ad516ebc0c4bdd7b6254871babfa978d5accafb554214137d398bfcf6a03000000000000000001f2090000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e05004830450221008ec888e36e4a4b3dc2ed6b823319855b2ae03006ca6ae0d9aa7e24bfc1d6f07102203b0f78885472a67ff4fe5916c0bb669487d659527509516fc3a08e87a2cc0a7c0147304402202c3e14282b84b02705dfd00a6da396c9fe8a8bcb1d3fdb4b20a4feba09440e8b02202b058b39aa9b0c865b22095edcd9ff1f71bbfe20aa4993755e54d042755ed0d5012004040404040404040404040404040404040404040404040404040404040404048a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a91418bc1a114ccf9c052d3d23e28d3b0a9d1227434288527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f801b175ac686800000000");
+               }
+
+               {
+                       // commitment tx with five outputs untrimmed (minimum feerate)
+                       chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
+                       chan.feerate_per_kw = 2070;
+
+                       test_commitment!("3045022100f2377f7a67b7fc7f4e2c0c9e3a7de935c32417f5668eda31ea1db401b7dc53030220415fdbc8e91d0f735e70c21952342742e25249b0d062d43efbfc564499f37526",
+                                        "30440220443cb07f650aebbba14b8bc8d81e096712590f524c5991ac0ed3bbc8fd3bd0c7022028a635f548e3ca64b19b69b1ea00f05b22752f91daf0b6dab78e62ba52eb7fd0",
+                                        "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8005d007000000000000220020403d394747cae42e98ff01734ad5c08f82ba123d3d9a620abda88989651e2ab5b80b000000000000220020c20b5d1f8584fd90443e7b7b720136174fa4b9333c261d04dbbd012635c0f419a00f0000000000002200208c48d15160397c9731df9bc3b236656efb6665fbfe92b4a6878e88a499f741c4c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de843110da966a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e04004730440220443cb07f650aebbba14b8bc8d81e096712590f524c5991ac0ed3bbc8fd3bd0c7022028a635f548e3ca64b19b69b1ea00f05b22752f91daf0b6dab78e62ba52eb7fd001483045022100f2377f7a67b7fc7f4e2c0c9e3a7de935c32417f5668eda31ea1db401b7dc53030220415fdbc8e91d0f735e70c21952342742e25249b0d062d43efbfc564499f3752601475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220");
+
+                       assert_eq!(unsigned_tx.1.len(), 3);
+
+                       test_htlc_output!(0,
+                                         "3045022100eed143b1ee4bed5dc3cde40afa5db3e7354cbf9c44054b5f713f729356f08cf7022077161d171c2bbd9badf3c9934de65a4918de03bbac1450f715275f75b103f891",
+                                         "3045022100a0d043ed533e7fb1911e0553d31a8e2f3e6de19dbc035257f29d747c5e02f1f5022030cd38d8e84282175d49c1ebe0470db3ebd59768cf40780a784e248a43904fb8",
+                                         "0200000000010140a83ce364747ff277f4d7595d8d15f708418798922c40bc2b056aca5485a2180000000000000000000174020000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100eed143b1ee4bed5dc3cde40afa5db3e7354cbf9c44054b5f713f729356f08cf7022077161d171c2bbd9badf3c9934de65a4918de03bbac1450f715275f75b103f89101483045022100a0d043ed533e7fb1911e0553d31a8e2f3e6de19dbc035257f29d747c5e02f1f5022030cd38d8e84282175d49c1ebe0470db3ebd59768cf40780a784e248a43904fb801008576a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c820120876475527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae67a914b43e1b38138a41b37f7cd9a1d274bc63e3a9b5d188ac6868f6010000");
+
+                       test_htlc_output!(1,
+                                         "3044022071e9357619fd8d29a411dc053b326a5224c5d11268070e88ecb981b174747c7a02202b763ae29a9d0732fa8836dd8597439460b50472183f420021b768981b4f7cf6",
+                                         "3045022100adb1d679f65f96178b59f23ed37d3b70443118f345224a07ecb043eee2acc157022034d24524fe857144a3bcfff3065a9994d0a6ec5f11c681e49431d573e242612d",
+                                         "0200000000010140a83ce364747ff277f4d7595d8d15f708418798922c40bc2b056aca5485a218010000000000000000015c060000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500473044022071e9357619fd8d29a411dc053b326a5224c5d11268070e88ecb981b174747c7a02202b763ae29a9d0732fa8836dd8597439460b50472183f420021b768981b4f7cf601483045022100adb1d679f65f96178b59f23ed37d3b70443118f345224a07ecb043eee2acc157022034d24524fe857144a3bcfff3065a9994d0a6ec5f11c681e49431d573e242612d01008576a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c820120876475527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae67a9148a486ff2e31d6158bf39e2608864d63fefd09d5b88ac6868f7010000");
+
+                       test_htlc_output!(2,
+                                         "3045022100c9458a4d2cbb741705577deb0a890e5cb90ee141be0400d3162e533727c9cb2102206edcf765c5dc5e5f9b976ea8149bf8607b5a0efb30691138e1231302b640d2a4",
+                                         "304402200831422aa4e1ee6d55e0b894201770a8f8817a189356f2d70be76633ffa6a6f602200dd1b84a4855dc6727dd46c98daae43dfc70889d1ba7ef0087529a57c06e5e04",
+                                         "0200000000010140a83ce364747ff277f4d7595d8d15f708418798922c40bc2b056aca5485a21802000000000000000001f1090000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100c9458a4d2cbb741705577deb0a890e5cb90ee141be0400d3162e533727c9cb2102206edcf765c5dc5e5f9b976ea8149bf8607b5a0efb30691138e1231302b640d2a40147304402200831422aa4e1ee6d55e0b894201770a8f8817a189356f2d70be76633ffa6a6f602200dd1b84a4855dc6727dd46c98daae43dfc70889d1ba7ef0087529a57c06e5e04012004040404040404040404040404040404040404040404040404040404040404048a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a91418bc1a114ccf9c052d3d23e28d3b0a9d1227434288527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f801b175ac686800000000");
+               }
+
+               {
+                       // commitment tx with five outputs untrimmed (maximum feerate)
+                       chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
+                       chan.feerate_per_kw = 2194;
+
+                       test_commitment!("3045022100d33c4e541aa1d255d41ea9a3b443b3b822ad8f7f86862638aac1f69f8f760577022007e2a18e6931ce3d3a804b1c78eda1de17dbe1fb7a95488c9a4ec86203953348",
+                                        "304402203b1b010c109c2ecbe7feb2d259b9c4126bd5dc99ee693c422ec0a5781fe161ba0220571fe4e2c649dea9c7aaf7e49b382962f6a3494963c97d80fef9a430ca3f7061",
+                                        "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8005d007000000000000220020403d394747cae42e98ff01734ad5c08f82ba123d3d9a620abda88989651e2ab5b80b000000000000220020c20b5d1f8584fd90443e7b7b720136174fa4b9333c261d04dbbd012635c0f419a00f0000000000002200208c48d15160397c9731df9bc3b236656efb6665fbfe92b4a6878e88a499f741c4c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de84311040966a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e040047304402203b1b010c109c2ecbe7feb2d259b9c4126bd5dc99ee693c422ec0a5781fe161ba0220571fe4e2c649dea9c7aaf7e49b382962f6a3494963c97d80fef9a430ca3f706101483045022100d33c4e541aa1d255d41ea9a3b443b3b822ad8f7f86862638aac1f69f8f760577022007e2a18e6931ce3d3a804b1c78eda1de17dbe1fb7a95488c9a4ec8620395334801475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220");
+
+                       assert_eq!(unsigned_tx.1.len(), 3);
+
+                       test_htlc_output!(0,
+                                         "30450221009ed2f0a67f99e29c3c8cf45c08207b765980697781bb727fe0b1416de0e7622902206052684229bc171419ed290f4b615c943f819c0262414e43c5b91dcf72ddcf44",
+                                         "3044022004ad5f04ae69c71b3b141d4db9d0d4c38d84009fb3cfeeae6efdad414487a9a0022042d3fe1388c1ff517d1da7fb4025663d372c14728ed52dc88608363450ff6a2f",
+                                         "02000000000101fb824d4e4dafc0f567789dee3a6bce8d411fe80f5563d8cdfdcc7d7e4447d43a0000000000000000000122020000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e05004830450221009ed2f0a67f99e29c3c8cf45c08207b765980697781bb727fe0b1416de0e7622902206052684229bc171419ed290f4b615c943f819c0262414e43c5b91dcf72ddcf4401473044022004ad5f04ae69c71b3b141d4db9d0d4c38d84009fb3cfeeae6efdad414487a9a0022042d3fe1388c1ff517d1da7fb4025663d372c14728ed52dc88608363450ff6a2f01008576a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c820120876475527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae67a914b43e1b38138a41b37f7cd9a1d274bc63e3a9b5d188ac6868f6010000");
+
+                       test_htlc_output!(1,
+                                         "30440220155d3b90c67c33a8321996a9be5b82431b0c126613be751d400669da9d5c696702204318448bcd48824439d2c6a70be6e5747446be47ff45977cf41672bdc9b6b12d",
+                                         "304402201707050c870c1f77cc3ed58d6d71bf281de239e9eabd8ef0955bad0d7fe38dcc02204d36d80d0019b3a71e646a08fa4a5607761d341ae8be371946ebe437c289c915",
+                                         "02000000000101fb824d4e4dafc0f567789dee3a6bce8d411fe80f5563d8cdfdcc7d7e4447d43a010000000000000000010a060000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e05004730440220155d3b90c67c33a8321996a9be5b82431b0c126613be751d400669da9d5c696702204318448bcd48824439d2c6a70be6e5747446be47ff45977cf41672bdc9b6b12d0147304402201707050c870c1f77cc3ed58d6d71bf281de239e9eabd8ef0955bad0d7fe38dcc02204d36d80d0019b3a71e646a08fa4a5607761d341ae8be371946ebe437c289c91501008576a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c820120876475527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae67a9148a486ff2e31d6158bf39e2608864d63fefd09d5b88ac6868f7010000");
+
+                       test_htlc_output!(2,
+                                         "3045022100a12a9a473ece548584aabdd051779025a5ed4077c4b7aa376ec7a0b1645e5a48022039490b333f53b5b3e2ddde1d809e492cba2b3e5fc3a436cd3ffb4cd3d500fa5a",
+                                         "3045022100ff200bc934ab26ce9a559e998ceb0aee53bc40368e114ab9d3054d9960546e2802202496856ca163ac12c143110b6b3ac9d598df7254f2e17b3b94c3ab5301f4c3b0",
+                                         "02000000000101fb824d4e4dafc0f567789dee3a6bce8d411fe80f5563d8cdfdcc7d7e4447d43a020000000000000000019a090000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100a12a9a473ece548584aabdd051779025a5ed4077c4b7aa376ec7a0b1645e5a48022039490b333f53b5b3e2ddde1d809e492cba2b3e5fc3a436cd3ffb4cd3d500fa5a01483045022100ff200bc934ab26ce9a559e998ceb0aee53bc40368e114ab9d3054d9960546e2802202496856ca163ac12c143110b6b3ac9d598df7254f2e17b3b94c3ab5301f4c3b0012004040404040404040404040404040404040404040404040404040404040404048a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a91418bc1a114ccf9c052d3d23e28d3b0a9d1227434288527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f801b175ac686800000000");
+               }
+
+               {
+                       // commitment tx with four outputs untrimmed (minimum feerate)
+                       chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
+                       chan.feerate_per_kw = 2195;
+
+                       test_commitment!("304402205e2f76d4657fb732c0dfc820a18a7301e368f5799e06b7828007633741bda6df0220458009ae59d0c6246065c419359e05eb2a4b4ef4a1b310cc912db44eb7924298",
+                                        "304402203b12d44254244b8ff3bb4129b0920fd45120ab42f553d9976394b099d500c99e02205e95bb7a3164852ef0c48f9e0eaf145218f8e2c41251b231f03cbdc4f29a5429",
+                                        "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8004b80b000000000000220020c20b5d1f8584fd90443e7b7b720136174fa4b9333c261d04dbbd012635c0f419a00f0000000000002200208c48d15160397c9731df9bc3b236656efb6665fbfe92b4a6878e88a499f741c4c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de843110b8976a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e040047304402203b12d44254244b8ff3bb4129b0920fd45120ab42f553d9976394b099d500c99e02205e95bb7a3164852ef0c48f9e0eaf145218f8e2c41251b231f03cbdc4f29a54290147304402205e2f76d4657fb732c0dfc820a18a7301e368f5799e06b7828007633741bda6df0220458009ae59d0c6246065c419359e05eb2a4b4ef4a1b310cc912db44eb792429801475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220");
+
+                       assert_eq!(unsigned_tx.1.len(), 2);
+
+                       test_htlc_output!(0,
+                                         "3045022100a8a78fa1016a5c5c3704f2e8908715a3cef66723fb95f3132ec4d2d05cd84fb4022025ac49287b0861ec21932405f5600cbce94313dbde0e6c5d5af1b3366d8afbfc",
+                                         "3045022100be6ae1977fd7b630a53623f3f25c542317ccfc2b971782802a4f1ef538eb22b402207edc4d0408f8f38fd3c7365d1cfc26511b7cd2d4fecd8b005fba3cd5bc704390",
+                                         "020000000001014e16c488fa158431c1a82e8f661240ec0a71ba0ce92f2721a6538c510226ad5c0000000000000000000109060000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100a8a78fa1016a5c5c3704f2e8908715a3cef66723fb95f3132ec4d2d05cd84fb4022025ac49287b0861ec21932405f5600cbce94313dbde0e6c5d5af1b3366d8afbfc01483045022100be6ae1977fd7b630a53623f3f25c542317ccfc2b971782802a4f1ef538eb22b402207edc4d0408f8f38fd3c7365d1cfc26511b7cd2d4fecd8b005fba3cd5bc70439001008576a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c820120876475527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae67a9148a486ff2e31d6158bf39e2608864d63fefd09d5b88ac6868f7010000");
+
+                       test_htlc_output!(1,
+                                         "3045022100e769cb156aa2f7515d126cef7a69968629620ce82afcaa9e210969de6850df4602200b16b3f3486a229a48aadde520dbee31ae340dbadaffae74fbb56681fef27b92",
+                                         "30440220665b9cb4a978c09d1ca8977a534999bc8a49da624d0c5439451dd69cde1a003d022070eae0620f01f3c1bd029cc1488da13fb40fdab76f396ccd335479a11c5276d8",
+                                         "020000000001014e16c488fa158431c1a82e8f661240ec0a71ba0ce92f2721a6538c510226ad5c0100000000000000000199090000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100e769cb156aa2f7515d126cef7a69968629620ce82afcaa9e210969de6850df4602200b16b3f3486a229a48aadde520dbee31ae340dbadaffae74fbb56681fef27b92014730440220665b9cb4a978c09d1ca8977a534999bc8a49da624d0c5439451dd69cde1a003d022070eae0620f01f3c1bd029cc1488da13fb40fdab76f396ccd335479a11c5276d8012004040404040404040404040404040404040404040404040404040404040404048a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a91418bc1a114ccf9c052d3d23e28d3b0a9d1227434288527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f801b175ac686800000000");
+               }
+
+               {
+                       // commitment tx with four outputs untrimmed (maximum feerate)
+                       chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
+                       chan.feerate_per_kw = 3702;
+
+                       test_commitment!("3045022100c1a3b0b60ca092ed5080121f26a74a20cec6bdee3f8e47bae973fcdceb3eda5502207d467a9873c939bf3aa758014ae67295fedbca52412633f7e5b2670fc7c381c1",
+                                        "304402200e930a43c7951162dc15a2b7344f48091c74c70f7024e7116e900d8bcfba861c022066fa6cbda3929e21daa2e7e16a4b948db7e8919ef978402360d1095ffdaff7b0",
+                                        "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8004b80b000000000000220020c20b5d1f8584fd90443e7b7b720136174fa4b9333c261d04dbbd012635c0f419a00f0000000000002200208c48d15160397c9731df9bc3b236656efb6665fbfe92b4a6878e88a499f741c4c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de8431106f916a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e040047304402200e930a43c7951162dc15a2b7344f48091c74c70f7024e7116e900d8bcfba861c022066fa6cbda3929e21daa2e7e16a4b948db7e8919ef978402360d1095ffdaff7b001483045022100c1a3b0b60ca092ed5080121f26a74a20cec6bdee3f8e47bae973fcdceb3eda5502207d467a9873c939bf3aa758014ae67295fedbca52412633f7e5b2670fc7c381c101475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220");
+
+                       assert_eq!(unsigned_tx.1.len(), 2);
+
+                       test_htlc_output!(0,
+                                         "3045022100dfb73b4fe961b31a859b2bb1f4f15cabab9265016dd0272323dc6a9e85885c54022059a7b87c02861ee70662907f25ce11597d7b68d3399443a831ae40e777b76bdb",
+                                         "304402202765b9c9ece4f127fa5407faf66da4c5ce2719cdbe47cd3175fc7d48b482e43d02205605125925e07bad1e41c618a4b434d72c88a164981c4b8af5eaf4ee9142ec3a",
+                                         "02000000000101b8de11eb51c22498fe39722c7227b6e55ff1a94146cf638458cb9bc6a060d3a30000000000000000000122020000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100dfb73b4fe961b31a859b2bb1f4f15cabab9265016dd0272323dc6a9e85885c54022059a7b87c02861ee70662907f25ce11597d7b68d3399443a831ae40e777b76bdb0147304402202765b9c9ece4f127fa5407faf66da4c5ce2719cdbe47cd3175fc7d48b482e43d02205605125925e07bad1e41c618a4b434d72c88a164981c4b8af5eaf4ee9142ec3a01008576a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c820120876475527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae67a9148a486ff2e31d6158bf39e2608864d63fefd09d5b88ac6868f7010000");
+
+                       test_htlc_output!(1,
+                                         "3045022100ea9dc2a7c3c3640334dab733bb4e036e32a3106dc707b24227874fa4f7da746802204d672f7ac0fe765931a8df10b81e53a3242dd32bd9dc9331eb4a596da87954e9",
+                                         "30440220048a41c660c4841693de037d00a407810389f4574b3286afb7bc392a438fa3f802200401d71fa87c64fe621b49ac07e3bf85157ac680acb977124da28652cc7f1a5c",
+                                         "02000000000101b8de11eb51c22498fe39722c7227b6e55ff1a94146cf638458cb9bc6a060d3a30100000000000000000176050000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100ea9dc2a7c3c3640334dab733bb4e036e32a3106dc707b24227874fa4f7da746802204d672f7ac0fe765931a8df10b81e53a3242dd32bd9dc9331eb4a596da87954e9014730440220048a41c660c4841693de037d00a407810389f4574b3286afb7bc392a438fa3f802200401d71fa87c64fe621b49ac07e3bf85157ac680acb977124da28652cc7f1a5c012004040404040404040404040404040404040404040404040404040404040404048a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a91418bc1a114ccf9c052d3d23e28d3b0a9d1227434288527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f801b175ac686800000000");
+               }
+
+               {
+                       // commitment tx with three outputs untrimmed (minimum feerate)
+                       chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
+                       chan.feerate_per_kw = 3703;
+
+                       test_commitment!("30450221008b7c191dd46893b67b628e618d2dc8e81169d38bade310181ab77d7c94c6675e02203b4dd131fd7c9deb299560983dcdc485545c98f989f7ae8180c28289f9e6bdb0",
+                                        "3044022047305531dd44391dce03ae20f8735005c615eb077a974edb0059ea1a311857d602202e0ed6972fbdd1e8cb542b06e0929bc41b2ddf236e04cb75edd56151f4197506",
+                                        "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8003a00f0000000000002200208c48d15160397c9731df9bc3b236656efb6665fbfe92b4a6878e88a499f741c4c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de843110eb936a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0400473044022047305531dd44391dce03ae20f8735005c615eb077a974edb0059ea1a311857d602202e0ed6972fbdd1e8cb542b06e0929bc41b2ddf236e04cb75edd56151f4197506014830450221008b7c191dd46893b67b628e618d2dc8e81169d38bade310181ab77d7c94c6675e02203b4dd131fd7c9deb299560983dcdc485545c98f989f7ae8180c28289f9e6bdb001475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220");
+
+                       assert_eq!(unsigned_tx.1.len(), 1);
+
+                       test_htlc_output!(0,
+                                         "3044022044f65cf833afdcb9d18795ca93f7230005777662539815b8a601eeb3e57129a902206a4bf3e53392affbba52640627defa8dc8af61c958c9e827b2798ab45828abdd",
+                                         "3045022100b94d931a811b32eeb885c28ddcf999ae1981893b21dd1329929543fe87ce793002206370107fdd151c5f2384f9ceb71b3107c69c74c8ed5a28a94a4ab2d27d3b0724",
+                                         "020000000001011c076aa7fb3d7460d10df69432c904227ea84bbf3134d4ceee5fb0f135ef206d0000000000000000000175050000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500473044022044f65cf833afdcb9d18795ca93f7230005777662539815b8a601eeb3e57129a902206a4bf3e53392affbba52640627defa8dc8af61c958c9e827b2798ab45828abdd01483045022100b94d931a811b32eeb885c28ddcf999ae1981893b21dd1329929543fe87ce793002206370107fdd151c5f2384f9ceb71b3107c69c74c8ed5a28a94a4ab2d27d3b0724012004040404040404040404040404040404040404040404040404040404040404048a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a91418bc1a114ccf9c052d3d23e28d3b0a9d1227434288527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f801b175ac686800000000");
+               }
+
+               {
+                       // commitment tx with three outputs untrimmed (maximum feerate)
+                       chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
+                       chan.feerate_per_kw = 4914;
+
+                       test_commitment!("304402206d6cb93969d39177a09d5d45b583f34966195b77c7e585cf47ac5cce0c90cefb022031d71ae4e33a4e80df7f981d696fbdee517337806a3c7138b7491e2cbb077a0e",
+                                        "304402206a2679efa3c7aaffd2a447fd0df7aba8792858b589750f6a1203f9259173198a022008d52a0e77a99ab533c36206cb15ad7aeb2aa72b93d4b571e728cb5ec2f6fe26",
+                                        "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8003a00f0000000000002200208c48d15160397c9731df9bc3b236656efb6665fbfe92b4a6878e88a499f741c4c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de843110ae8f6a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e040047304402206a2679efa3c7aaffd2a447fd0df7aba8792858b589750f6a1203f9259173198a022008d52a0e77a99ab533c36206cb15ad7aeb2aa72b93d4b571e728cb5ec2f6fe260147304402206d6cb93969d39177a09d5d45b583f34966195b77c7e585cf47ac5cce0c90cefb022031d71ae4e33a4e80df7f981d696fbdee517337806a3c7138b7491e2cbb077a0e01475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220");
+
+                       assert_eq!(unsigned_tx.1.len(), 1);
+
+                       test_htlc_output!(0,
+                                         "3045022100fcb38506bfa11c02874092a843d0cc0a8613c23b639832564a5f69020cb0f6ba02206508b9e91eaa001425c190c68ee5f887e1ad5b1b314002e74db9dbd9e42dbecf",
+                                         "304502210086e76b460ddd3cea10525fba298405d3fe11383e56966a5091811368362f689a02200f72ee75657915e0ede89c28709acd113ede9e1b7be520e3bc5cda425ecd6e68",
+                                         "0200000000010110a3fdcbcd5db477cd3ad465e7f501ffa8c437e8301f00a6061138590add757f0000000000000000000122020000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100fcb38506bfa11c02874092a843d0cc0a8613c23b639832564a5f69020cb0f6ba02206508b9e91eaa001425c190c68ee5f887e1ad5b1b314002e74db9dbd9e42dbecf0148304502210086e76b460ddd3cea10525fba298405d3fe11383e56966a5091811368362f689a02200f72ee75657915e0ede89c28709acd113ede9e1b7be520e3bc5cda425ecd6e68012004040404040404040404040404040404040404040404040404040404040404048a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a91418bc1a114ccf9c052d3d23e28d3b0a9d1227434288527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f801b175ac686800000000");
+               }
+
+               {
+                       // commitment tx with two outputs untrimmed (minimum feerate)
+                       chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
+                       chan.feerate_per_kw = 4915;
+
+                       test_commitment!("304402200769ba89c7330dfa4feba447b6e322305f12ac7dac70ec6ba997ed7c1b598d0802204fe8d337e7fee781f9b7b1a06e580b22f4f79d740059560191d7db53f8765552",
+                                        "3045022100a012691ba6cea2f73fa8bac37750477e66363c6d28813b0bb6da77c8eb3fb0270220365e99c51304b0b1a6ab9ea1c8500db186693e39ec1ad5743ee231b0138384b9",
+                                        "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8002c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de843110fa926a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0400483045022100a012691ba6cea2f73fa8bac37750477e66363c6d28813b0bb6da77c8eb3fb0270220365e99c51304b0b1a6ab9ea1c8500db186693e39ec1ad5743ee231b0138384b90147304402200769ba89c7330dfa4feba447b6e322305f12ac7dac70ec6ba997ed7c1b598d0802204fe8d337e7fee781f9b7b1a06e580b22f4f79d740059560191d7db53f876555201475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220");
+
+                       assert_eq!(unsigned_tx.1.len(), 0);
+               }
+
+               {
+                       // commitment tx with two outputs untrimmed (maximum feerate)
+                       chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
+                       chan.feerate_per_kw = 9651180;
+
+                       test_commitment!("3044022037f83ff00c8e5fb18ae1f918ffc24e54581775a20ff1ae719297ef066c71caa9022039c529cccd89ff6c5ed1db799614533844bd6d101da503761c45c713996e3bbd",
+                                        "30440220514f977bf7edc442de8ce43ace9686e5ebdc0f893033f13e40fb46c8b8c6e1f90220188006227d175f5c35da0b092c57bea82537aed89f7778204dc5bacf4f29f2b9",
+                                        "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b800222020000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80ec0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de84311004004730440220514f977bf7edc442de8ce43ace9686e5ebdc0f893033f13e40fb46c8b8c6e1f90220188006227d175f5c35da0b092c57bea82537aed89f7778204dc5bacf4f29f2b901473044022037f83ff00c8e5fb18ae1f918ffc24e54581775a20ff1ae719297ef066c71caa9022039c529cccd89ff6c5ed1db799614533844bd6d101da503761c45c713996e3bbd01475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220");
+
+                       assert_eq!(unsigned_tx.1.len(), 0);
+               }
+
+               {
+                       // commitment tx with one output untrimmed (minimum feerate)
+                       chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
+                       chan.feerate_per_kw = 9651181;
+
+                       test_commitment!("3044022064901950be922e62cbe3f2ab93de2b99f37cff9fc473e73e394b27f88ef0731d02206d1dfa227527b4df44a07599289e207d6fd9cca60c0365682dcd3deaf739567e",
+                                        "3044022031a82b51bd014915fe68928d1abf4b9885353fb896cac10c3fdd88d7f9c7f2e00220716bda819641d2c63e65d3549b6120112e1aeaf1742eed94a471488e79e206b1",
+                                        "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8001c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de8431100400473044022031a82b51bd014915fe68928d1abf4b9885353fb896cac10c3fdd88d7f9c7f2e00220716bda819641d2c63e65d3549b6120112e1aeaf1742eed94a471488e79e206b101473044022064901950be922e62cbe3f2ab93de2b99f37cff9fc473e73e394b27f88ef0731d02206d1dfa227527b4df44a07599289e207d6fd9cca60c0365682dcd3deaf739567e01475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220");
+
+                       assert_eq!(unsigned_tx.1.len(), 0);
+               }
+
+               {
+                       // commitment tx with fee greater than funder amount
+                       chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
+                       chan.feerate_per_kw = 9651936;
+
+                       test_commitment!("3044022064901950be922e62cbe3f2ab93de2b99f37cff9fc473e73e394b27f88ef0731d02206d1dfa227527b4df44a07599289e207d6fd9cca60c0365682dcd3deaf739567e",
+                                        "3044022031a82b51bd014915fe68928d1abf4b9885353fb896cac10c3fdd88d7f9c7f2e00220716bda819641d2c63e65d3549b6120112e1aeaf1742eed94a471488e79e206b1",
+                                        "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8001c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de8431100400473044022031a82b51bd014915fe68928d1abf4b9885353fb896cac10c3fdd88d7f9c7f2e00220716bda819641d2c63e65d3549b6120112e1aeaf1742eed94a471488e79e206b101473044022064901950be922e62cbe3f2ab93de2b99f37cff9fc473e73e394b27f88ef0731d02206d1dfa227527b4df44a07599289e207d6fd9cca60c0365682dcd3deaf739567e01475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220");
+
+                       assert_eq!(unsigned_tx.1.len(), 0);
+               }
+       }
+
+       #[test]
+       fn test_per_commitment_secret_gen() {
+               // Test vectors from BOLT 3 Appendix D:
+
+               let mut seed = [0; 32];
+               seed[0..32].clone_from_slice(&hex::decode("0000000000000000000000000000000000000000000000000000000000000000").unwrap());
+               assert_eq!(chan_utils::build_commitment_secret(seed, 281474976710655),
+                          hex::decode("02a40c85b6f28da08dfdbe0926c53fab2de6d28c10301f8f7c4073d5e42e3148").unwrap()[..]);
+
+               seed[0..32].clone_from_slice(&hex::decode("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF").unwrap());
+               assert_eq!(chan_utils::build_commitment_secret(seed, 281474976710655),
+                          hex::decode("7cc854b54e3e0dcdb010d7a3fee464a9687be6e8db3be6854c475621e007a5dc").unwrap()[..]);
+
+               assert_eq!(chan_utils::build_commitment_secret(seed, 0xaaaaaaaaaaa),
+                          hex::decode("56f4008fb007ca9acf0e15b054d5c9fd12ee06cea347914ddbaed70d1c13a528").unwrap()[..]);
+
+               assert_eq!(chan_utils::build_commitment_secret(seed, 0x555555555555),
+                          hex::decode("9015daaeb06dba4ccc05b91b2f73bd54405f2be9f217fbacd3c5ac2e62327d31").unwrap()[..]);
+
+               seed[0..32].clone_from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap());
+               assert_eq!(chan_utils::build_commitment_secret(seed, 1),
+                          hex::decode("915c75942a26bb3a433a8ce2cb0427c29ec6c1775cfc78328b57f6ba7bfeaa9c").unwrap()[..]);
+       }
+
+       #[test]
+       fn test_key_derivation() {
+               // Test vectors from BOLT 3 Appendix E:
+               let secp_ctx = Secp256k1::new();
+
+               let base_secret = SecretKey::from_slice(&hex::decode("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f").unwrap()[..]).unwrap();
+               let per_commitment_secret = SecretKey::from_slice(&hex::decode("1f1e1d1c1b1a191817161514131211100f0e0d0c0b0a09080706050403020100").unwrap()[..]).unwrap();
+
+               let base_point = PublicKey::from_secret_key(&secp_ctx, &base_secret);
+               assert_eq!(base_point.serialize()[..], hex::decode("036d6caac248af96f6afa7f904f550253a0f3ef3f5aa2fe6838a95b216691468e2").unwrap()[..]);
+
+               let per_commitment_point = PublicKey::from_secret_key(&secp_ctx, &per_commitment_secret);
+               assert_eq!(per_commitment_point.serialize()[..], hex::decode("025f7117a78150fe2ef97db7cfc83bd57b2e2c0d0dd25eaf467a4a1c2a45ce1486").unwrap()[..]);
+
+               assert_eq!(chan_utils::derive_public_key(&secp_ctx, &per_commitment_point, &base_point).unwrap().serialize()[..],
+                               hex::decode("0235f2dbfaa89b57ec7b055afe29849ef7ddfeb1cefdb9ebdc43f5494984db29e5").unwrap()[..]);
+
+               assert_eq!(chan_utils::derive_private_key(&secp_ctx, &per_commitment_point, &base_secret).unwrap(),
+                               SecretKey::from_slice(&hex::decode("cbced912d3b21bf196a766651e436aff192362621ce317704ea2f75d87e7be0f").unwrap()[..]).unwrap());
+
+               assert_eq!(chan_utils::derive_public_revocation_key(&secp_ctx, &per_commitment_point, &base_point).unwrap().serialize()[..],
+                               hex::decode("02916e326636d19c33f13e8c0c3a03dd157f332f3e99c317c141dd865eb01f8ff0").unwrap()[..]);
+
+               assert_eq!(chan_utils::derive_private_revocation_key(&secp_ctx, &per_commitment_secret, &base_secret).unwrap(),
+                               SecretKey::from_slice(&hex::decode("d09ffff62ddb2297ab000cc85bcb4283fdeb6aa052affbc9dddcf33b61078110").unwrap()[..]).unwrap());
+       }
+}
diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs
new file mode 100644 (file)
index 0000000..a957a96
--- /dev/null
@@ -0,0 +1,3228 @@
+//! The top-level channel management and payment tracking stuff lives here.
+//!
+//! The ChannelManager is the main chunk of logic implementing the lightning protocol and is
+//! responsible for tracking which channels are open, HTLCs are in flight and reestablishing those
+//! upon reconnect to the relevant peer(s).
+//!
+//! It does not manage routing logic (see ln::router for that) nor does it manage constructing
+//! on-chain transactions (it only monitors the chain to watch for any force-closes that might
+//! imply it needs to fail HTLCs/payments/channels it manages).
+
+use bitcoin::blockdata::block::BlockHeader;
+use bitcoin::blockdata::transaction::Transaction;
+use bitcoin::blockdata::constants::genesis_block;
+use bitcoin::network::constants::Network;
+use bitcoin::util::hash::BitcoinHash;
+
+use bitcoin_hashes::{Hash, HashEngine};
+use bitcoin_hashes::hmac::{Hmac, HmacEngine};
+use bitcoin_hashes::sha256::Hash as Sha256;
+use bitcoin_hashes::sha256d::Hash as Sha256dHash;
+use bitcoin_hashes::cmp::fixed_time_eq;
+
+use secp256k1::key::{SecretKey,PublicKey};
+use secp256k1::Secp256k1;
+use secp256k1::ecdh::SharedSecret;
+use secp256k1;
+
+use chain::chaininterface::{BroadcasterInterface,ChainListener,ChainWatchInterface,FeeEstimator};
+use chain::transaction::OutPoint;
+use ln::channel::{Channel, ChannelError};
+use ln::channelmonitor::{ChannelMonitor, ChannelMonitorUpdateErr, ManyChannelMonitor, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ANTI_REORG_DELAY};
+use ln::router::Route;
+use ln::msgs;
+use ln::msgs::LocalFeatures;
+use ln::onion_utils;
+use ln::msgs::{ChannelMessageHandler, DecodeError, HandleError};
+use chain::keysinterface::KeysInterface;
+use util::config::UserConfig;
+use util::{byte_utils, events};
+use util::ser::{Readable, ReadableArgs, Writeable, Writer};
+use util::chacha20::ChaCha20;
+use util::logger::Logger;
+use util::errors::APIError;
+
+use std::{cmp, mem};
+use std::collections::{HashMap, hash_map, HashSet};
+use std::io::Cursor;
+use std::sync::{Arc, Mutex, MutexGuard, RwLock};
+use std::sync::atomic::{AtomicUsize, Ordering};
+use std::time::Duration;
+
+// We hold various information about HTLC relay in the HTLC objects in Channel itself:
+//
+// Upon receipt of an HTLC from a peer, we'll give it a PendingHTLCStatus indicating if it should
+// forward the HTLC with information it will give back to us when it does so, or if it should Fail
+// the HTLC with the relevant message for the Channel to handle giving to the remote peer.
+//
+// When a Channel forwards an HTLC to its peer, it will give us back the PendingForwardHTLCInfo
+// which we will use to construct an outbound HTLC, with a relevant HTLCSource::PreviousHopData
+// filled in to indicate where it came from (which we can use to either fail-backwards or fulfill
+// the HTLC backwards along the relevant path).
+// Alternatively, we can fill an outbound HTLC with a HTLCSource::OutboundRoute indicating this is
+// our payment, which we can use to decode errors or inform the user that the payment was sent.
+/// Stores the info we will need to send when we want to forward an HTLC onwards
+#[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
+pub(super) struct PendingForwardHTLCInfo {
+       onion_packet: Option<msgs::OnionPacket>,
+       incoming_shared_secret: [u8; 32],
+       payment_hash: PaymentHash,
+       short_channel_id: u64,
+       pub(super) amt_to_forward: u64,
+       pub(super) outgoing_cltv_value: u32,
+}
+
+#[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
+pub(super) enum HTLCFailureMsg {
+       Relay(msgs::UpdateFailHTLC),
+       Malformed(msgs::UpdateFailMalformedHTLC),
+}
+
+/// Stores whether we can't forward an HTLC or relevant forwarding info
+#[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
+pub(super) enum PendingHTLCStatus {
+       Forward(PendingForwardHTLCInfo),
+       Fail(HTLCFailureMsg),
+}
+
+/// Tracks the inbound corresponding to an outbound HTLC
+#[derive(Clone, PartialEq)]
+pub(super) struct HTLCPreviousHopData {
+       short_channel_id: u64,
+       htlc_id: u64,
+       incoming_packet_shared_secret: [u8; 32],
+}
+
+/// Tracks the inbound corresponding to an outbound HTLC
+#[derive(Clone, PartialEq)]
+pub(super) enum HTLCSource {
+       PreviousHopData(HTLCPreviousHopData),
+       OutboundRoute {
+               route: Route,
+               session_priv: SecretKey,
+               /// Technically we can recalculate this from the route, but we cache it here to avoid
+               /// doing a double-pass on route when we get a failure back
+               first_hop_htlc_msat: u64,
+       },
+}
+#[cfg(test)]
+impl HTLCSource {
+       pub fn dummy() -> Self {
+               HTLCSource::OutboundRoute {
+                       route: Route { hops: Vec::new() },
+                       session_priv: SecretKey::from_slice(&[1; 32]).unwrap(),
+                       first_hop_htlc_msat: 0,
+               }
+       }
+}
+
+#[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
+pub(super) enum HTLCFailReason {
+       ErrorPacket {
+               err: msgs::OnionErrorPacket,
+       },
+       Reason {
+               failure_code: u16,
+               data: Vec<u8>,
+       }
+}
+
+/// payment_hash type, use to cross-lock hop
+#[derive(Hash, Copy, Clone, PartialEq, Eq, Debug)]
+pub struct PaymentHash(pub [u8;32]);
+/// payment_preimage type, use to route payment between hop
+#[derive(Hash, Copy, Clone, PartialEq, Eq, Debug)]
+pub struct PaymentPreimage(pub [u8;32]);
+
+type ShutdownResult = (Vec<Transaction>, Vec<(HTLCSource, PaymentHash)>);
+
+/// Error type returned across the channel_state mutex boundary. When an Err is generated for a
+/// Channel, we generally end up with a ChannelError::Close for which we have to close the channel
+/// immediately (ie with no further calls on it made). Thus, this step happens inside a
+/// channel_state lock. We then return the set of things that need to be done outside the lock in
+/// this struct and call handle_error!() on it.
+
+struct MsgHandleErrInternal {
+       err: msgs::HandleError,
+       shutdown_finish: Option<(ShutdownResult, Option<msgs::ChannelUpdate>)>,
+}
+impl MsgHandleErrInternal {
+       #[inline]
+       fn send_err_msg_no_close(err: &'static str, channel_id: [u8; 32]) -> Self {
+               Self {
+                       err: HandleError {
+                               err,
+                               action: Some(msgs::ErrorAction::SendErrorMessage {
+                                       msg: msgs::ErrorMessage {
+                                               channel_id,
+                                               data: err.to_string()
+                                       },
+                               }),
+                       },
+                       shutdown_finish: None,
+               }
+       }
+       #[inline]
+       fn ignore_no_close(err: &'static str) -> Self {
+               Self {
+                       err: HandleError {
+                               err,
+                               action: Some(msgs::ErrorAction::IgnoreError),
+                       },
+                       shutdown_finish: None,
+               }
+       }
+       #[inline]
+       fn from_no_close(err: msgs::HandleError) -> Self {
+               Self { err, shutdown_finish: None }
+       }
+       #[inline]
+       fn from_finish_shutdown(err: &'static str, channel_id: [u8; 32], shutdown_res: ShutdownResult, channel_update: Option<msgs::ChannelUpdate>) -> Self {
+               Self {
+                       err: HandleError {
+                               err,
+                               action: Some(msgs::ErrorAction::SendErrorMessage {
+                                       msg: msgs::ErrorMessage {
+                                               channel_id,
+                                               data: err.to_string()
+                                       },
+                               }),
+                       },
+                       shutdown_finish: Some((shutdown_res, channel_update)),
+               }
+       }
+       #[inline]
+       fn from_chan_no_close(err: ChannelError, channel_id: [u8; 32]) -> Self {
+               Self {
+                       err: match err {
+                               ChannelError::Ignore(msg) => HandleError {
+                                       err: msg,
+                                       action: Some(msgs::ErrorAction::IgnoreError),
+                               },
+                               ChannelError::Close(msg) => HandleError {
+                                       err: msg,
+                                       action: Some(msgs::ErrorAction::SendErrorMessage {
+                                               msg: msgs::ErrorMessage {
+                                                       channel_id,
+                                                       data: msg.to_string()
+                                               },
+                                       }),
+                               },
+                               ChannelError::CloseDelayBroadcast { msg, .. } => HandleError {
+                                       err: msg,
+                                       action: Some(msgs::ErrorAction::SendErrorMessage {
+                                               msg: msgs::ErrorMessage {
+                                                       channel_id,
+                                                       data: msg.to_string()
+                                               },
+                                       }),
+                               },
+                       },
+                       shutdown_finish: None,
+               }
+       }
+}
+
+/// We hold back HTLCs we intend to relay for a random interval greater than this (see
+/// Event::PendingHTLCsForwardable for the API guidelines indicating how long should be waited).
+/// This provides some limited amount of privacy. Ideally this would range from somewhere like one
+/// second to 30 seconds, but people expect lightning to be, you know, kinda fast, sadly.
+const MIN_HTLC_RELAY_HOLDING_CELL_MILLIS: u64 = 100;
+
+pub(super) enum HTLCForwardInfo {
+       AddHTLC {
+               prev_short_channel_id: u64,
+               prev_htlc_id: u64,
+               forward_info: PendingForwardHTLCInfo,
+       },
+       FailHTLC {
+               htlc_id: u64,
+               err_packet: msgs::OnionErrorPacket,
+       },
+}
+
+/// For events which result in both a RevokeAndACK and a CommitmentUpdate, by default they should
+/// be sent in the order they appear in the return value, however sometimes the order needs to be
+/// variable at runtime (eg Channel::channel_reestablish needs to re-send messages in the order
+/// they were originally sent). In those cases, this enum is also returned.
+#[derive(Clone, PartialEq)]
+pub(super) enum RAACommitmentOrder {
+       /// Send the CommitmentUpdate messages first
+       CommitmentFirst,
+       /// Send the RevokeAndACK message first
+       RevokeAndACKFirst,
+}
+
+// Note this is only exposed in cfg(test):
+pub(super) struct ChannelHolder {
+       pub(super) by_id: HashMap<[u8; 32], Channel>,
+       pub(super) short_to_id: HashMap<u64, [u8; 32]>,
+       /// short channel id -> forward infos. Key of 0 means payments received
+       /// Note that while this is held in the same mutex as the channels themselves, no consistency
+       /// guarantees are made about the existence of a channel with the short id here, nor the short
+       /// ids in the PendingForwardHTLCInfo!
+       pub(super) forward_htlcs: HashMap<u64, Vec<HTLCForwardInfo>>,
+       /// payment_hash -> Vec<(amount_received, htlc_source)> for tracking things that were to us and
+       /// can be failed/claimed by the user
+       /// Note that while this is held in the same mutex as the channels themselves, no consistency
+       /// guarantees are made about the channels given here actually existing anymore by the time you
+       /// go to read them!
+       pub(super) claimable_htlcs: HashMap<PaymentHash, Vec<(u64, HTLCPreviousHopData)>>,
+       /// Messages to send to peers - pushed to in the same lock that they are generated in (except
+       /// for broadcast messages, where ordering isn't as strict).
+       pub(super) pending_msg_events: Vec<events::MessageSendEvent>,
+}
+pub(super) struct MutChannelHolder<'a> {
+       pub(super) by_id: &'a mut HashMap<[u8; 32], Channel>,
+       pub(super) short_to_id: &'a mut HashMap<u64, [u8; 32]>,
+       pub(super) forward_htlcs: &'a mut HashMap<u64, Vec<HTLCForwardInfo>>,
+       pub(super) claimable_htlcs: &'a mut HashMap<PaymentHash, Vec<(u64, HTLCPreviousHopData)>>,
+       pub(super) pending_msg_events: &'a mut Vec<events::MessageSendEvent>,
+}
+impl ChannelHolder {
+       pub(super) fn borrow_parts(&mut self) -> MutChannelHolder {
+               MutChannelHolder {
+                       by_id: &mut self.by_id,
+                       short_to_id: &mut self.short_to_id,
+                       forward_htlcs: &mut self.forward_htlcs,
+                       claimable_htlcs: &mut self.claimable_htlcs,
+                       pending_msg_events: &mut self.pending_msg_events,
+               }
+       }
+}
+
+#[cfg(not(any(target_pointer_width = "32", target_pointer_width = "64")))]
+const ERR: () = "You need at least 32 bit pointers (well, usize, but we'll assume they're the same) for ChannelManager::latest_block_height";
+
+/// Manager which keeps track of a number of channels and sends messages to the appropriate
+/// channel, also tracking HTLC preimages and forwarding onion packets appropriately.
+///
+/// Implements ChannelMessageHandler, handling the multi-channel parts and passing things through
+/// to individual Channels.
+///
+/// Implements Writeable to write out all channel state to disk. Implies peer_disconnected() for
+/// all peers during write/read (though does not modify this instance, only the instance being
+/// serialized). This will result in any channels which have not yet exchanged funding_created (ie
+/// called funding_transaction_generated for outbound channels).
+///
+/// Note that you can be a bit lazier about writing out ChannelManager than you can be with
+/// ChannelMonitors. With ChannelMonitors you MUST write each monitor update out to disk before
+/// returning from ManyChannelMonitor::add_update_monitor, with ChannelManagers, writing updates
+/// happens out-of-band (and will prevent any other ChannelManager operations from occurring during
+/// the serialization process). If the deserialized version is out-of-date compared to the
+/// ChannelMonitors passed by reference to read(), those channels will be force-closed based on the
+/// ChannelMonitor state and no funds will be lost (mod on-chain transaction fees).
+///
+/// Note that the deserializer is only implemented for (Sha256dHash, ChannelManager), which
+/// tells you the last block hash which was block_connect()ed. You MUST rescan any blocks along
+/// the "reorg path" (ie call block_disconnected() until you get to a common block and then call
+/// block_connected() to step towards your best block) upon deserialization before using the
+/// object!
+pub struct ChannelManager {
+       default_configuration: UserConfig,
+       genesis_hash: Sha256dHash,
+       fee_estimator: Arc<FeeEstimator>,
+       monitor: Arc<ManyChannelMonitor>,
+       chain_monitor: Arc<ChainWatchInterface>,
+       tx_broadcaster: Arc<BroadcasterInterface>,
+
+       #[cfg(test)]
+       pub(super) latest_block_height: AtomicUsize,
+       #[cfg(not(test))]
+       latest_block_height: AtomicUsize,
+       last_block_hash: Mutex<Sha256dHash>,
+       secp_ctx: Secp256k1<secp256k1::All>,
+
+       #[cfg(test)]
+       pub(super) channel_state: Mutex<ChannelHolder>,
+       #[cfg(not(test))]
+       channel_state: Mutex<ChannelHolder>,
+       our_network_key: SecretKey,
+
+       pending_events: Mutex<Vec<events::Event>>,
+       /// Used when we have to take a BIG lock to make sure everything is self-consistent.
+       /// Essentially just when we're serializing ourselves out.
+       /// Taken first everywhere where we are making changes before any other locks.
+       total_consistency_lock: RwLock<()>,
+
+       keys_manager: Arc<KeysInterface>,
+
+       logger: Arc<Logger>,
+}
+
+/// The amount of time we require our counterparty wait to claim their money (ie time between when
+/// we, or our watchtower, must check for them having broadcast a theft transaction).
+pub(crate) const BREAKDOWN_TIMEOUT: u16 = 6 * 24;
+/// The amount of time we're willing to wait to claim money back to us
+pub(crate) const MAX_LOCAL_BREAKDOWN_TIMEOUT: u16 = 6 * 24 * 7;
+
+/// The minimum number of blocks between an inbound HTLC's CLTV and the corresponding outbound
+/// HTLC's CLTV. This should always be a few blocks greater than channelmonitor::CLTV_CLAIM_BUFFER,
+/// ie the node we forwarded the payment on to should always have enough room to reliably time out
+/// the HTLC via a full update_fail_htlc/commitment_signed dance before we hit the
+/// CLTV_CLAIM_BUFFER point (we static assert that it's at least 3 blocks more).
+const CLTV_EXPIRY_DELTA: u16 = 6 * 12; //TODO?
+pub(super) const CLTV_FAR_FAR_AWAY: u32 = 6 * 24 * 7; //TODO?
+
+// Check that our CLTV_EXPIRY is at least CLTV_CLAIM_BUFFER + ANTI_REORG_DELAY + LATENCY_GRACE_PERIOD_BLOCKS,
+// ie that if the next-hop peer fails the HTLC within
+// LATENCY_GRACE_PERIOD_BLOCKS then we'll still have CLTV_CLAIM_BUFFER left to timeout it onchain,
+// then waiting ANTI_REORG_DELAY to be reorg-safe on the outbound HLTC and
+// failing the corresponding htlc backward, and us now seeing the last block of ANTI_REORG_DELAY before
+// LATENCY_GRACE_PERIOD_BLOCKS.
+#[deny(const_err)]
+#[allow(dead_code)]
+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;
+
+// Check for ability of an attacker to make us fail on-chain by delaying inbound claim. See
+// ChannelMontior::would_broadcast_at_height for a description of why this is needed.
+#[deny(const_err)]
+#[allow(dead_code)]
+const CHECK_CLTV_EXPIRY_SANITY_2: u32 = CLTV_EXPIRY_DELTA as u32 - LATENCY_GRACE_PERIOD_BLOCKS - 2*CLTV_CLAIM_BUFFER;
+
+macro_rules! secp_call {
+       ( $res: expr, $err: expr ) => {
+               match $res {
+                       Ok(key) => key,
+                       Err(_) => return Err($err),
+               }
+       };
+}
+
+/// Details of a channel, as returned by ChannelManager::list_channels and ChannelManager::list_usable_channels
+pub struct ChannelDetails {
+       /// The channel's ID (prior to funding transaction generation, this is a random 32 bytes,
+       /// thereafter this is the txid of the funding transaction xor the funding transaction output).
+       /// Note that this means this value is *not* persistent - it can change once during the
+       /// lifetime of the channel.
+       pub channel_id: [u8; 32],
+       /// The position of the funding transaction in the chain. None if the funding transaction has
+       /// not yet been confirmed and the channel fully opened.
+       pub short_channel_id: Option<u64>,
+       /// The node_id of our counterparty
+       pub remote_network_id: PublicKey,
+       /// The value, in satoshis, of this channel as appears in the funding output
+       pub channel_value_satoshis: u64,
+       /// The user_id passed in to create_channel, or 0 if the channel was inbound.
+       pub user_id: u64,
+       /// The available outbound capacity for sending HTLCs to the remote peer. This does not include
+       /// any pending HTLCs which are not yet fully resolved (and, thus, who's balance is not
+       /// available for inclusion in new outbound HTLCs). This further does not include any pending
+       /// outgoing HTLCs which are awaiting some other resolution to be sent.
+       pub outbound_capacity_msat: u64,
+       /// The available inbound capacity for the remote peer to send HTLCs to us. This does not
+       /// include any pending HTLCs which are not yet fully resolved (and, thus, who's balance is not
+       /// available for inclusion in new inbound HTLCs).
+       /// Note that there are some corner cases not fully handled here, so the actual available
+       /// inbound capacity may be slightly higher than this.
+       pub inbound_capacity_msat: u64,
+       /// True if the channel is (a) confirmed and funding_locked messages have been exchanged, (b)
+       /// the peer is connected, and (c) no monitor update failure is pending resolution.
+       pub is_live: bool,
+}
+
+macro_rules! handle_error {
+       ($self: ident, $internal: expr) => {
+               match $internal {
+                       Ok(msg) => Ok(msg),
+                       Err(MsgHandleErrInternal { err, shutdown_finish }) => {
+                               if let Some((shutdown_res, update_option)) = shutdown_finish {
+                                       $self.finish_force_close_channel(shutdown_res);
+                                       if let Some(update) = update_option {
+                                               let mut channel_state = $self.channel_state.lock().unwrap();
+                                               channel_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
+                                                       msg: update
+                                               });
+                                       }
+                               }
+                               Err(err)
+                       },
+               }
+       }
+}
+
+macro_rules! break_chan_entry {
+       ($self: ident, $res: expr, $channel_state: expr, $entry: expr) => {
+               match $res {
+                       Ok(res) => res,
+                       Err(ChannelError::Ignore(msg)) => {
+                               break Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), $entry.key().clone()))
+                       },
+                       Err(ChannelError::Close(msg)) => {
+                               log_trace!($self, "Closing channel {} due to Close-required error: {}", log_bytes!($entry.key()[..]), msg);
+                               let (channel_id, mut chan) = $entry.remove_entry();
+                               if let Some(short_id) = chan.get_short_channel_id() {
+                                       $channel_state.short_to_id.remove(&short_id);
+                               }
+                               break Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, chan.force_shutdown(), $self.get_channel_update(&chan).ok()))
+                       },
+                       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"); }
+               }
+       }
+}
+
+macro_rules! try_chan_entry {
+       ($self: ident, $res: expr, $channel_state: expr, $entry: expr) => {
+               match $res {
+                       Ok(res) => res,
+                       Err(ChannelError::Ignore(msg)) => {
+                               return Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), $entry.key().clone()))
+                       },
+                       Err(ChannelError::Close(msg)) => {
+                               log_trace!($self, "Closing channel {} due to Close-required error: {}", log_bytes!($entry.key()[..]), msg);
+                               let (channel_id, mut chan) = $entry.remove_entry();
+                               if let Some(short_id) = chan.get_short_channel_id() {
+                                       $channel_state.short_to_id.remove(&short_id);
+                               }
+                               return Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, chan.force_shutdown(), $self.get_channel_update(&chan).ok()))
+                       },
+                       Err(ChannelError::CloseDelayBroadcast { msg, update }) => {
+                               log_error!($self, "Channel {} need to be shutdown but closing transactions not broadcast due to {}", log_bytes!($entry.key()[..]), msg);
+                               let (channel_id, mut chan) = $entry.remove_entry();
+                               if let Some(short_id) = chan.get_short_channel_id() {
+                                       $channel_state.short_to_id.remove(&short_id);
+                               }
+                               if let Some(update) = update {
+                                       if let Err(e) = $self.monitor.add_update_monitor(update.get_funding_txo().unwrap(), update) {
+                                               match e {
+                                                       // Upstream channel is dead, but we want at least to fail backward HTLCs to save
+                                                       // downstream channels. In case of PermanentFailure, we are not going to be able
+                                                       // to claim back to_remote output on remote commitment transaction. Doesn't
+                                                       // make a difference here, we are concern about HTLCs circuit, not onchain funds.
+                                                       ChannelMonitorUpdateErr::PermanentFailure => {},
+                                                       ChannelMonitorUpdateErr::TemporaryFailure => {},
+                                               }
+                                       }
+                               }
+                               let mut shutdown_res = chan.force_shutdown();
+                               if shutdown_res.0.len() >= 1 {
+                                       log_error!($self, "You have a toxic local commitment transaction {} avaible in channel monitor, read comment in ChannelMonitor::get_latest_local_commitment_txn to be informed of manual action to take", shutdown_res.0[0].txid());
+                               }
+                               shutdown_res.0.clear();
+                               return Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, shutdown_res, $self.get_channel_update(&chan).ok()))
+                       }
+               }
+       }
+}
+
+macro_rules! handle_monitor_err {
+       ($self: ident, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr) => {
+               handle_monitor_err!($self, $err, $channel_state, $entry, $action_type, $resend_raa, $resend_commitment, Vec::new(), Vec::new())
+       };
+       ($self: ident, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr, $failed_forwards: expr, $failed_fails: expr) => {
+               match $err {
+                       ChannelMonitorUpdateErr::PermanentFailure => {
+                               log_error!($self, "Closing channel {} due to monitor update PermanentFailure", log_bytes!($entry.key()[..]));
+                               let (channel_id, mut chan) = $entry.remove_entry();
+                               if let Some(short_id) = chan.get_short_channel_id() {
+                                       $channel_state.short_to_id.remove(&short_id);
+                               }
+                               // TODO: $failed_fails is dropped here, which will cause other channels to hit the
+                               // chain in a confused state! We need to move them into the ChannelMonitor which
+                               // will be responsible for failing backwards once things confirm on-chain.
+                               // It's ok that we drop $failed_forwards here - at this point we'd rather they
+                               // broadcast HTLC-Timeout and pay the associated fees to get their funds back than
+                               // us bother trying to claim it just to forward on to another peer. If we're
+                               // splitting hairs we'd prefer to claim payments that were to us, but we haven't
+                               // given up the preimage yet, so might as well just wait until the payment is
+                               // retried, avoiding the on-chain fees.
+                               let res: Result<(), _> = Err(MsgHandleErrInternal::from_finish_shutdown("ChannelMonitor storage failure", channel_id, chan.force_shutdown(), $self.get_channel_update(&chan).ok()));
+                               res
+                       },
+                       ChannelMonitorUpdateErr::TemporaryFailure => {
+                               log_info!($self, "Disabling channel {} due to monitor update TemporaryFailure. On restore will send {} and process {} forwards and {} fails",
+                                               log_bytes!($entry.key()[..]),
+                                               if $resend_commitment && $resend_raa {
+                                                               match $action_type {
+                                                                       RAACommitmentOrder::CommitmentFirst => { "commitment then RAA" },
+                                                                       RAACommitmentOrder::RevokeAndACKFirst => { "RAA then commitment" },
+                                                               }
+                                                       } else if $resend_commitment { "commitment" }
+                                                       else if $resend_raa { "RAA" }
+                                                       else { "nothing" },
+                                               (&$failed_forwards as &Vec<(PendingForwardHTLCInfo, u64)>).len(),
+                                               (&$failed_fails as &Vec<(HTLCSource, PaymentHash, HTLCFailReason)>).len());
+                               if !$resend_commitment {
+                                       debug_assert!($action_type == RAACommitmentOrder::RevokeAndACKFirst || !$resend_raa);
+                               }
+                               if !$resend_raa {
+                                       debug_assert!($action_type == RAACommitmentOrder::CommitmentFirst || !$resend_commitment);
+                               }
+                               $entry.get_mut().monitor_update_failed($resend_raa, $resend_commitment, $failed_forwards, $failed_fails);
+                               Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore("Failed to update ChannelMonitor"), *$entry.key()))
+                       },
+               }
+       }
+}
+
+macro_rules! return_monitor_err {
+       ($self: ident, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr) => {
+               return handle_monitor_err!($self, $err, $channel_state, $entry, $action_type, $resend_raa, $resend_commitment);
+       };
+       ($self: ident, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr, $failed_forwards: expr, $failed_fails: expr) => {
+               return handle_monitor_err!($self, $err, $channel_state, $entry, $action_type, $resend_raa, $resend_commitment, $failed_forwards, $failed_fails);
+       }
+}
+
+// Does not break in case of TemporaryFailure!
+macro_rules! maybe_break_monitor_err {
+       ($self: ident, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr) => {
+               match (handle_monitor_err!($self, $err, $channel_state, $entry, $action_type, $resend_raa, $resend_commitment), $err) {
+                       (e, ChannelMonitorUpdateErr::PermanentFailure) => {
+                               break e;
+                       },
+                       (_, ChannelMonitorUpdateErr::TemporaryFailure) => { },
+               }
+       }
+}
+
+impl ChannelManager {
+       /// Constructs a new ChannelManager to hold several channels and route between them.
+       ///
+       /// This is the main "logic hub" for all channel-related actions, and implements
+       /// ChannelMessageHandler.
+       ///
+       /// Non-proportional fees are fixed according to our risk using the provided fee estimator.
+       ///
+       /// panics if channel_value_satoshis is >= `MAX_FUNDING_SATOSHIS`!
+       pub fn new(network: Network, feeest: Arc<FeeEstimator>, monitor: Arc<ManyChannelMonitor>, chain_monitor: Arc<ChainWatchInterface>, tx_broadcaster: Arc<BroadcasterInterface>, logger: Arc<Logger>,keys_manager: Arc<KeysInterface>, config: UserConfig) -> Result<Arc<ChannelManager>, secp256k1::Error> {
+               let secp_ctx = Secp256k1::new();
+
+               let res = Arc::new(ChannelManager {
+                       default_configuration: config.clone(),
+                       genesis_hash: genesis_block(network).header.bitcoin_hash(),
+                       fee_estimator: feeest.clone(),
+                       monitor: monitor.clone(),
+                       chain_monitor,
+                       tx_broadcaster,
+
+                       latest_block_height: AtomicUsize::new(0), //TODO: Get an init value
+                       last_block_hash: Mutex::new(Default::default()),
+                       secp_ctx,
+
+                       channel_state: Mutex::new(ChannelHolder{
+                               by_id: HashMap::new(),
+                               short_to_id: HashMap::new(),
+                               forward_htlcs: HashMap::new(),
+                               claimable_htlcs: HashMap::new(),
+                               pending_msg_events: Vec::new(),
+                       }),
+                       our_network_key: keys_manager.get_node_secret(),
+
+                       pending_events: Mutex::new(Vec::new()),
+                       total_consistency_lock: RwLock::new(()),
+
+                       keys_manager,
+
+                       logger,
+               });
+               let weak_res = Arc::downgrade(&res);
+               res.chain_monitor.register_listener(weak_res);
+               Ok(res)
+       }
+
+       /// Creates a new outbound channel to the given remote node and with the given value.
+       ///
+       /// user_id will be provided back as user_channel_id in FundingGenerationReady and
+       /// FundingBroadcastSafe events to allow tracking of which events correspond with which
+       /// create_channel call. Note that user_channel_id defaults to 0 for inbound channels, so you
+       /// may wish to avoid using 0 for user_id here.
+       ///
+       /// If successful, will generate a SendOpenChannel message event, so you should probably poll
+       /// PeerManager::process_events afterwards.
+       ///
+       /// Raises APIError::APIMisuseError when channel_value_satoshis > 2**24 or push_msat is
+       /// greater than channel_value_satoshis * 1k or channel_value_satoshis is < 1000.
+       pub fn create_channel(&self, their_network_key: PublicKey, channel_value_satoshis: u64, push_msat: u64, user_id: u64) -> Result<(), APIError> {
+               if channel_value_satoshis < 1000 {
+                       return Err(APIError::APIMisuseError { err: "channel_value must be at least 1000 satoshis" });
+               }
+
+               let channel = Channel::new_outbound(&*self.fee_estimator, &self.keys_manager, their_network_key, channel_value_satoshis, push_msat, user_id, Arc::clone(&self.logger), &self.default_configuration)?;
+               let res = channel.get_open_channel(self.genesis_hash.clone(), &*self.fee_estimator);
+
+               let _ = self.total_consistency_lock.read().unwrap();
+               let mut channel_state = self.channel_state.lock().unwrap();
+               match channel_state.by_id.entry(channel.channel_id()) {
+                       hash_map::Entry::Occupied(_) => {
+                               if cfg!(feature = "fuzztarget") {
+                                       return Err(APIError::APIMisuseError { err: "Fuzzy bad RNG" });
+                               } else {
+                                       panic!("RNG is bad???");
+                               }
+                       },
+                       hash_map::Entry::Vacant(entry) => { entry.insert(channel); }
+               }
+               channel_state.pending_msg_events.push(events::MessageSendEvent::SendOpenChannel {
+                       node_id: their_network_key,
+                       msg: res,
+               });
+               Ok(())
+       }
+
+       /// Gets the list of open channels, in random order. See ChannelDetail field documentation for
+       /// more information.
+       pub fn list_channels(&self) -> Vec<ChannelDetails> {
+               let channel_state = self.channel_state.lock().unwrap();
+               let mut res = Vec::with_capacity(channel_state.by_id.len());
+               for (channel_id, channel) in channel_state.by_id.iter() {
+                       let (inbound_capacity_msat, outbound_capacity_msat) = channel.get_inbound_outbound_available_balance_msat();
+                       res.push(ChannelDetails {
+                               channel_id: (*channel_id).clone(),
+                               short_channel_id: channel.get_short_channel_id(),
+                               remote_network_id: channel.get_their_node_id(),
+                               channel_value_satoshis: channel.get_value_satoshis(),
+                               inbound_capacity_msat,
+                               outbound_capacity_msat,
+                               user_id: channel.get_user_id(),
+                               is_live: channel.is_live(),
+                       });
+               }
+               res
+       }
+
+       /// Gets the list of usable channels, in random order. Useful as an argument to
+       /// Router::get_route to ensure non-announced channels are used.
+       ///
+       /// These are guaranteed to have their is_live value set to true, see the documentation for
+       /// ChannelDetails::is_live for more info on exactly what the criteria are.
+       pub fn list_usable_channels(&self) -> Vec<ChannelDetails> {
+               let channel_state = self.channel_state.lock().unwrap();
+               let mut res = Vec::with_capacity(channel_state.by_id.len());
+               for (channel_id, channel) in channel_state.by_id.iter() {
+                       // Note we use is_live here instead of usable which leads to somewhat confused
+                       // internal/external nomenclature, but that's ok cause that's probably what the user
+                       // really wanted anyway.
+                       if channel.is_live() {
+                               let (inbound_capacity_msat, outbound_capacity_msat) = channel.get_inbound_outbound_available_balance_msat();
+                               res.push(ChannelDetails {
+                                       channel_id: (*channel_id).clone(),
+                                       short_channel_id: channel.get_short_channel_id(),
+                                       remote_network_id: channel.get_their_node_id(),
+                                       channel_value_satoshis: channel.get_value_satoshis(),
+                                       inbound_capacity_msat,
+                                       outbound_capacity_msat,
+                                       user_id: channel.get_user_id(),
+                                       is_live: true,
+                               });
+                       }
+               }
+               res
+       }
+
+       /// Begins the process of closing a channel. After this call (plus some timeout), no new HTLCs
+       /// will be accepted on the given channel, and after additional timeout/the closing of all
+       /// pending HTLCs, the channel will be closed on chain.
+       ///
+       /// May generate a SendShutdown message event on success, which should be relayed.
+       pub fn close_channel(&self, channel_id: &[u8; 32]) -> Result<(), APIError> {
+               let _ = self.total_consistency_lock.read().unwrap();
+
+               let (mut failed_htlcs, chan_option) = {
+                       let mut channel_state_lock = self.channel_state.lock().unwrap();
+                       let channel_state = channel_state_lock.borrow_parts();
+                       match channel_state.by_id.entry(channel_id.clone()) {
+                               hash_map::Entry::Occupied(mut chan_entry) => {
+                                       let (shutdown_msg, failed_htlcs) = chan_entry.get_mut().get_shutdown()?;
+                                       channel_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
+                                               node_id: chan_entry.get().get_their_node_id(),
+                                               msg: shutdown_msg
+                                       });
+                                       if chan_entry.get().is_shutdown() {
+                                               if let Some(short_id) = chan_entry.get().get_short_channel_id() {
+                                                       channel_state.short_to_id.remove(&short_id);
+                                               }
+                                               (failed_htlcs, Some(chan_entry.remove_entry().1))
+                                       } else { (failed_htlcs, None) }
+                               },
+                               hash_map::Entry::Vacant(_) => return Err(APIError::ChannelUnavailable{err: "No such channel"})
+                       }
+               };
+               for htlc_source in failed_htlcs.drain(..) {
+                       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() });
+               }
+               let chan_update = if let Some(chan) = chan_option {
+                       if let Ok(update) = self.get_channel_update(&chan) {
+                               Some(update)
+                       } else { None }
+               } else { None };
+
+               if let Some(update) = chan_update {
+                       let mut channel_state = self.channel_state.lock().unwrap();
+                       channel_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
+                               msg: update
+                       });
+               }
+
+               Ok(())
+       }
+
+       #[inline]
+       fn finish_force_close_channel(&self, shutdown_res: ShutdownResult) {
+               let (local_txn, mut failed_htlcs) = shutdown_res;
+               log_trace!(self, "Finishing force-closure of channel with {} transactions to broadcast and {} HTLCs to fail", local_txn.len(), failed_htlcs.len());
+               for htlc_source in failed_htlcs.drain(..) {
+                       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() });
+               }
+               for tx in local_txn {
+                       self.tx_broadcaster.broadcast_transaction(&tx);
+               }
+       }
+
+       /// Force closes a channel, immediately broadcasting the latest local commitment transaction to
+       /// the chain and rejecting new HTLCs on the given channel.
+       pub fn force_close_channel(&self, channel_id: &[u8; 32]) {
+               let _ = self.total_consistency_lock.read().unwrap();
+
+               let mut chan = {
+                       let mut channel_state_lock = self.channel_state.lock().unwrap();
+                       let channel_state = channel_state_lock.borrow_parts();
+                       if let Some(chan) = channel_state.by_id.remove(channel_id) {
+                               if let Some(short_id) = chan.get_short_channel_id() {
+                                       channel_state.short_to_id.remove(&short_id);
+                               }
+                               chan
+                       } else {
+                               return;
+                       }
+               };
+               log_trace!(self, "Force-closing channel {}", log_bytes!(channel_id[..]));
+               self.finish_force_close_channel(chan.force_shutdown());
+               if let Ok(update) = self.get_channel_update(&chan) {
+                       let mut channel_state = self.channel_state.lock().unwrap();
+                       channel_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
+                               msg: update
+                       });
+               }
+       }
+
+       /// Force close all channels, immediately broadcasting the latest local commitment transaction
+       /// for each to the chain and rejecting new HTLCs on each.
+       pub fn force_close_all_channels(&self) {
+               for chan in self.list_channels() {
+                       self.force_close_channel(&chan.channel_id);
+               }
+       }
+
+       const ZERO:[u8; 65] = [0; 65];
+       fn decode_update_add_htlc_onion(&self, msg: &msgs::UpdateAddHTLC) -> (PendingHTLCStatus, MutexGuard<ChannelHolder>) {
+               macro_rules! return_malformed_err {
+                       ($msg: expr, $err_code: expr) => {
+                               {
+                                       log_info!(self, "Failed to accept/forward incoming HTLC: {}", $msg);
+                                       return (PendingHTLCStatus::Fail(HTLCFailureMsg::Malformed(msgs::UpdateFailMalformedHTLC {
+                                               channel_id: msg.channel_id,
+                                               htlc_id: msg.htlc_id,
+                                               sha256_of_onion: Sha256::hash(&msg.onion_routing_packet.hop_data).into_inner(),
+                                               failure_code: $err_code,
+                                       })), self.channel_state.lock().unwrap());
+                               }
+                       }
+               }
+
+               if let Err(_) = msg.onion_routing_packet.public_key {
+                       return_malformed_err!("invalid ephemeral pubkey", 0x8000 | 0x4000 | 6);
+               }
+
+               let shared_secret = {
+                       let mut arr = [0; 32];
+                       arr.copy_from_slice(&SharedSecret::new(&msg.onion_routing_packet.public_key.unwrap(), &self.our_network_key)[..]);
+                       arr
+               };
+               let (rho, mu) = onion_utils::gen_rho_mu_from_shared_secret(&shared_secret);
+
+               if msg.onion_routing_packet.version != 0 {
+                       //TODO: Spec doesn't indicate if we should only hash hop_data here (and in other
+                       //sha256_of_onion error data packets), or the entire onion_routing_packet. Either way,
+                       //the hash doesn't really serve any purpose - in the case of hashing all data, the
+                       //receiving node would have to brute force to figure out which version was put in the
+                       //packet by the node that send us the message, in the case of hashing the hop_data, the
+                       //node knows the HMAC matched, so they already know what is there...
+                       return_malformed_err!("Unknown onion packet version", 0x8000 | 0x4000 | 4);
+               }
+
+               let mut hmac = HmacEngine::<Sha256>::new(&mu);
+               hmac.input(&msg.onion_routing_packet.hop_data);
+               hmac.input(&msg.payment_hash.0[..]);
+               if !fixed_time_eq(&Hmac::from_engine(hmac).into_inner(), &msg.onion_routing_packet.hmac) {
+                       return_malformed_err!("HMAC Check failed", 0x8000 | 0x4000 | 5);
+               }
+
+               let mut channel_state = None;
+               macro_rules! return_err {
+                       ($msg: expr, $err_code: expr, $data: expr) => {
+                               {
+                                       log_info!(self, "Failed to accept/forward incoming HTLC: {}", $msg);
+                                       if channel_state.is_none() {
+                                               channel_state = Some(self.channel_state.lock().unwrap());
+                                       }
+                                       return (PendingHTLCStatus::Fail(HTLCFailureMsg::Relay(msgs::UpdateFailHTLC {
+                                               channel_id: msg.channel_id,
+                                               htlc_id: msg.htlc_id,
+                                               reason: onion_utils::build_first_hop_failure_packet(&shared_secret, $err_code, $data),
+                                       })), channel_state.unwrap());
+                               }
+                       }
+               }
+
+               let mut chacha = ChaCha20::new(&rho, &[0u8; 8]);
+               let next_hop_data = {
+                       let mut decoded = [0; 65];
+                       chacha.process(&msg.onion_routing_packet.hop_data[0..65], &mut decoded);
+                       match msgs::OnionHopData::read(&mut Cursor::new(&decoded[..])) {
+                               Err(err) => {
+                                       let error_code = match err {
+                                               msgs::DecodeError::UnknownVersion => 0x4000 | 1, // unknown realm byte
+                                               _ => 0x2000 | 2, // Should never happen
+                                       };
+                                       return_err!("Unable to decode our hop data", error_code, &[0;0]);
+                               },
+                               Ok(msg) => msg
+                       }
+               };
+
+               let pending_forward_info = if next_hop_data.hmac == [0; 32] {
+                               // OUR PAYMENT!
+                               // final_expiry_too_soon
+                               if (msg.cltv_expiry as u64) < self.latest_block_height.load(Ordering::Acquire) as u64 + (CLTV_CLAIM_BUFFER + LATENCY_GRACE_PERIOD_BLOCKS) as u64 {
+                                       return_err!("The final CLTV expiry is too soon to handle", 17, &[0;0]);
+                               }
+                               // final_incorrect_htlc_amount
+                               if next_hop_data.data.amt_to_forward > msg.amount_msat {
+                                       return_err!("Upstream node sent less than we were supposed to receive in payment", 19, &byte_utils::be64_to_array(msg.amount_msat));
+                               }
+                               // final_incorrect_cltv_expiry
+                               if next_hop_data.data.outgoing_cltv_value != msg.cltv_expiry {
+                                       return_err!("Upstream node set CLTV to the wrong value", 18, &byte_utils::be32_to_array(msg.cltv_expiry));
+                               }
+
+                               // Note that we could obviously respond immediately with an update_fulfill_htlc
+                               // message, however that would leak that we are the recipient of this payment, so
+                               // instead we stay symmetric with the forwarding case, only responding (after a
+                               // delay) once they've send us a commitment_signed!
+
+                               PendingHTLCStatus::Forward(PendingForwardHTLCInfo {
+                                       onion_packet: None,
+                                       payment_hash: msg.payment_hash.clone(),
+                                       short_channel_id: 0,
+                                       incoming_shared_secret: shared_secret,
+                                       amt_to_forward: next_hop_data.data.amt_to_forward,
+                                       outgoing_cltv_value: next_hop_data.data.outgoing_cltv_value,
+                               })
+                       } else {
+                               let mut new_packet_data = [0; 20*65];
+                               chacha.process(&msg.onion_routing_packet.hop_data[65..], &mut new_packet_data[0..19*65]);
+                               chacha.process(&ChannelManager::ZERO[..], &mut new_packet_data[19*65..]);
+
+                               let mut new_pubkey = msg.onion_routing_packet.public_key.unwrap();
+
+                               let blinding_factor = {
+                                       let mut sha = Sha256::engine();
+                                       sha.input(&new_pubkey.serialize()[..]);
+                                       sha.input(&shared_secret);
+                                       Sha256::from_engine(sha).into_inner()
+                               };
+
+                               let public_key = if let Err(e) = new_pubkey.mul_assign(&self.secp_ctx, &blinding_factor[..]) {
+                                       Err(e)
+                               } else { Ok(new_pubkey) };
+
+                               let outgoing_packet = msgs::OnionPacket {
+                                       version: 0,
+                                       public_key,
+                                       hop_data: new_packet_data,
+                                       hmac: next_hop_data.hmac.clone(),
+                               };
+
+                               PendingHTLCStatus::Forward(PendingForwardHTLCInfo {
+                                       onion_packet: Some(outgoing_packet),
+                                       payment_hash: msg.payment_hash.clone(),
+                                       short_channel_id: next_hop_data.data.short_channel_id,
+                                       incoming_shared_secret: shared_secret,
+                                       amt_to_forward: next_hop_data.data.amt_to_forward,
+                                       outgoing_cltv_value: next_hop_data.data.outgoing_cltv_value,
+                               })
+                       };
+
+               channel_state = Some(self.channel_state.lock().unwrap());
+               if let &PendingHTLCStatus::Forward(PendingForwardHTLCInfo { ref onion_packet, ref short_channel_id, ref amt_to_forward, ref outgoing_cltv_value, .. }) = &pending_forward_info {
+                       if onion_packet.is_some() { // If short_channel_id is 0 here, we'll reject them in the body here
+                               let id_option = channel_state.as_ref().unwrap().short_to_id.get(&short_channel_id).cloned();
+                               let forwarding_id = match id_option {
+                                       None => { // unknown_next_peer
+                                               return_err!("Don't have available channel for forwarding as requested.", 0x4000 | 10, &[0;0]);
+                                       },
+                                       Some(id) => id.clone(),
+                               };
+                               if let Some((err, code, chan_update)) = loop {
+                                       let chan = channel_state.as_mut().unwrap().by_id.get_mut(&forwarding_id).unwrap();
+
+                                       // Note that we could technically not return an error yet here and just hope
+                                       // that the connection is reestablished or monitor updated by the time we get
+                                       // around to doing the actual forward, but better to fail early if we can and
+                                       // hopefully an attacker trying to path-trace payments cannot make this occur
+                                       // on a small/per-node/per-channel scale.
+                                       if !chan.is_live() { // channel_disabled
+                                               break Some(("Forwarding channel is not in a ready state.", 0x1000 | 20, Some(self.get_channel_update(chan).unwrap())));
+                                       }
+                                       if *amt_to_forward < chan.get_their_htlc_minimum_msat() { // amount_below_minimum
+                                               break Some(("HTLC amount was below the htlc_minimum_msat", 0x1000 | 11, Some(self.get_channel_update(chan).unwrap())));
+                                       }
+                                       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_our_fee_base_msat(&*self.fee_estimator) as u64) });
+                                       if fee.is_none() || msg.amount_msat < fee.unwrap() || (msg.amount_msat - fee.unwrap()) < *amt_to_forward { // fee_insufficient
+                                               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())));
+                                       }
+                                       if (msg.cltv_expiry as u64) < (*outgoing_cltv_value) as u64 + CLTV_EXPIRY_DELTA as u64 { // incorrect_cltv_expiry
+                                               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())));
+                                       }
+                                       let cur_height = self.latest_block_height.load(Ordering::Acquire) as u32 + 1;
+                                       // We want to have at least LATENCY_GRACE_PERIOD_BLOCKS to fail prior to going on chain CLAIM_BUFFER blocks before expiration
+                                       if msg.cltv_expiry <= cur_height + CLTV_CLAIM_BUFFER + LATENCY_GRACE_PERIOD_BLOCKS as u32 { // expiry_too_soon
+                                               break Some(("CLTV expiry is too close", 0x1000 | 14, Some(self.get_channel_update(chan).unwrap())));
+                                       }
+                                       if msg.cltv_expiry > cur_height + CLTV_FAR_FAR_AWAY as u32 { // expiry_too_far
+                                               break Some(("CLTV expiry is too far in the future", 21, None));
+                                       }
+                                       break None;
+                               }
+                               {
+                                       let mut res = Vec::with_capacity(8 + 128);
+                                       if let Some(chan_update) = chan_update {
+                                               if code == 0x1000 | 11 || code == 0x1000 | 12 {
+                                                       res.extend_from_slice(&byte_utils::be64_to_array(msg.amount_msat));
+                                               }
+                                               else if code == 0x1000 | 13 {
+                                                       res.extend_from_slice(&byte_utils::be32_to_array(msg.cltv_expiry));
+                                               }
+                                               else if code == 0x1000 | 20 {
+                                                       res.extend_from_slice(&byte_utils::be16_to_array(chan_update.contents.flags));
+                                               }
+                                               res.extend_from_slice(&chan_update.encode_with_len()[..]);
+                                       }
+                                       return_err!(err, code, &res[..]);
+                               }
+                       }
+               }
+
+               (pending_forward_info, channel_state.unwrap())
+       }
+
+       /// only fails if the channel does not yet have an assigned short_id
+       /// May be called with channel_state already locked!
+       fn get_channel_update(&self, chan: &Channel) -> Result<msgs::ChannelUpdate, HandleError> {
+               let short_channel_id = match chan.get_short_channel_id() {
+                       None => return Err(HandleError{err: "Channel not yet established", action: None}),
+                       Some(id) => id,
+               };
+
+               let were_node_one = PublicKey::from_secret_key(&self.secp_ctx, &self.our_network_key).serialize()[..] < chan.get_their_node_id().serialize()[..];
+
+               let unsigned = msgs::UnsignedChannelUpdate {
+                       chain_hash: self.genesis_hash,
+                       short_channel_id: short_channel_id,
+                       timestamp: chan.get_channel_update_count(),
+                       flags: (!were_node_one) as u16 | ((!chan.is_live() as u16) << 1),
+                       cltv_expiry_delta: CLTV_EXPIRY_DELTA,
+                       htlc_minimum_msat: chan.get_our_htlc_minimum_msat(),
+                       fee_base_msat: chan.get_our_fee_base_msat(&*self.fee_estimator),
+                       fee_proportional_millionths: chan.get_fee_proportional_millionths(),
+                       excess_data: Vec::new(),
+               };
+
+               let msg_hash = Sha256dHash::hash(&unsigned.encode()[..]);
+               let sig = self.secp_ctx.sign(&hash_to_message!(&msg_hash[..]), &self.our_network_key);
+
+               Ok(msgs::ChannelUpdate {
+                       signature: sig,
+                       contents: unsigned
+               })
+       }
+
+       /// Sends a payment along a given route.
+       ///
+       /// Value parameters are provided via the last hop in route, see documentation for RouteHop
+       /// fields for more info.
+       ///
+       /// Note that if the payment_hash already exists elsewhere (eg you're sending a duplicative
+       /// payment), we don't do anything to stop you! We always try to ensure that if the provided
+       /// next hop knows the preimage to payment_hash they can claim an additional amount as
+       /// specified in the last hop in the route! Thus, you should probably do your own
+       /// payment_preimage tracking (which you should already be doing as they represent "proof of
+       /// payment") and prevent double-sends yourself.
+       ///
+       /// May generate a SendHTLCs message event on success, which should be relayed.
+       ///
+       /// Raises APIError::RoutError when invalid route or forward parameter
+       /// (cltv_delta, fee, node public key) is specified.
+       /// Raises APIError::ChannelUnavailable if the next-hop channel is not available for updates
+       /// (including due to previous monitor update failure or new permanent monitor update failure).
+       /// Raised APIError::MonitorUpdateFailed if a new monitor update failure prevented sending the
+       /// relevant updates.
+       ///
+       /// In case of APIError::RouteError/APIError::ChannelUnavailable, the payment send has failed
+       /// and you may wish to retry via a different route immediately.
+       /// In case of APIError::MonitorUpdateFailed, the commitment update has been irrevocably
+       /// committed on our end and we're just waiting for a monitor update to send it. Do NOT retry
+       /// the payment via a different route unless you intend to pay twice!
+       pub fn send_payment(&self, route: Route, payment_hash: PaymentHash) -> Result<(), APIError> {
+               if route.hops.len() < 1 || route.hops.len() > 20 {
+                       return Err(APIError::RouteError{err: "Route didn't go anywhere/had bogus size"});
+               }
+               let our_node_id = self.get_our_node_id();
+               for (idx, hop) in route.hops.iter().enumerate() {
+                       if idx != route.hops.len() - 1 && hop.pubkey == our_node_id {
+                               return Err(APIError::RouteError{err: "Route went through us but wasn't a simple rebalance loop to us"});
+                       }
+               }
+
+               let session_priv = self.keys_manager.get_session_key();
+
+               let cur_height = self.latest_block_height.load(Ordering::Acquire) as u32 + 1;
+
+               let onion_keys = secp_call!(onion_utils::construct_onion_keys(&self.secp_ctx, &route, &session_priv),
+                               APIError::RouteError{err: "Pubkey along hop was maliciously selected"});
+               let (onion_payloads, htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(&route, cur_height)?;
+               let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, &payment_hash);
+
+               let _ = self.total_consistency_lock.read().unwrap();
+
+               let err: Result<(), _> = loop {
+                       let mut channel_lock = self.channel_state.lock().unwrap();
+
+                       let id = match channel_lock.short_to_id.get(&route.hops.first().unwrap().short_channel_id) {
+                               None => return Err(APIError::ChannelUnavailable{err: "No channel available with first hop!"}),
+                               Some(id) => id.clone(),
+                       };
+
+                       let channel_state = channel_lock.borrow_parts();
+                       if let hash_map::Entry::Occupied(mut chan) = channel_state.by_id.entry(id) {
+                               match {
+                                       if chan.get().get_their_node_id() != route.hops.first().unwrap().pubkey {
+                                               return Err(APIError::RouteError{err: "Node ID mismatch on first hop!"});
+                                       }
+                                       if !chan.get().is_live() {
+                                               return Err(APIError::ChannelUnavailable{err: "Peer for first hop currently disconnected/pending monitor update!"});
+                                       }
+                                       break_chan_entry!(self, chan.get_mut().send_htlc_and_commit(htlc_msat, payment_hash.clone(), htlc_cltv, HTLCSource::OutboundRoute {
+                                               route: route.clone(),
+                                               session_priv: session_priv.clone(),
+                                               first_hop_htlc_msat: htlc_msat,
+                                       }, onion_packet), channel_state, chan)
+                               } {
+                                       Some((update_add, commitment_signed, chan_monitor)) => {
+                                               if let Err(e) = self.monitor.add_update_monitor(chan_monitor.get_funding_txo().unwrap(), chan_monitor) {
+                                                       maybe_break_monitor_err!(self, e, channel_state, chan, RAACommitmentOrder::CommitmentFirst, false, true);
+                                                       // Note that MonitorUpdateFailed here indicates (per function docs)
+                                                       // that we will resent the commitment update once we unfree monitor
+                                                       // updating, so we have to take special care that we don't return
+                                                       // something else in case we will resend later!
+                                                       return Err(APIError::MonitorUpdateFailed);
+                                               }
+
+                                               channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
+                                                       node_id: route.hops.first().unwrap().pubkey,
+                                                       updates: msgs::CommitmentUpdate {
+                                                               update_add_htlcs: vec![update_add],
+                                                               update_fulfill_htlcs: Vec::new(),
+                                                               update_fail_htlcs: Vec::new(),
+                                                               update_fail_malformed_htlcs: Vec::new(),
+                                                               update_fee: None,
+                                                               commitment_signed,
+                                                       },
+                                               });
+                                       },
+                                       None => {},
+                               }
+                       } else { unreachable!(); }
+                       return Ok(());
+               };
+
+               match handle_error!(self, err) {
+                       Ok(_) => unreachable!(),
+                       Err(e) => {
+                               if let Some(msgs::ErrorAction::IgnoreError) = e.action {
+                               } else {
+                                       log_error!(self, "Got bad keys: {}!", e.err);
+                                       let mut channel_state = self.channel_state.lock().unwrap();
+                                       channel_state.pending_msg_events.push(events::MessageSendEvent::HandleError {
+                                               node_id: route.hops.first().unwrap().pubkey,
+                                               action: e.action,
+                                       });
+                               }
+                               Err(APIError::ChannelUnavailable { err: e.err })
+                       },
+               }
+       }
+
+       /// Call this upon creation of a funding transaction for the given channel.
+       ///
+       /// Note that ALL inputs in the transaction pointed to by funding_txo MUST spend SegWit outputs
+       /// or your counterparty can steal your funds!
+       ///
+       /// Panics if a funding transaction has already been provided for this channel.
+       ///
+       /// May panic if the funding_txo is duplicative with some other channel (note that this should
+       /// be trivially prevented by using unique funding transaction keys per-channel).
+       pub fn funding_transaction_generated(&self, temporary_channel_id: &[u8; 32], funding_txo: OutPoint) {
+               let _ = self.total_consistency_lock.read().unwrap();
+
+               let (mut chan, msg, chan_monitor) = {
+                       let (res, chan) = {
+                               let mut channel_state = self.channel_state.lock().unwrap();
+                               match channel_state.by_id.remove(temporary_channel_id) {
+                                       Some(mut chan) => {
+                                               (chan.get_outbound_funding_created(funding_txo)
+                                                       .map_err(|e| if let ChannelError::Close(msg) = e {
+                                                               MsgHandleErrInternal::from_finish_shutdown(msg, chan.channel_id(), chan.force_shutdown(), None)
+                                                       } else { unreachable!(); })
+                                               , chan)
+                                       },
+                                       None => return
+                               }
+                       };
+                       match handle_error!(self, res) {
+                               Ok(funding_msg) => {
+                                       (chan, funding_msg.0, funding_msg.1)
+                               },
+                               Err(e) => {
+                                       log_error!(self, "Got bad signatures: {}!", e.err);
+                                       let mut channel_state = self.channel_state.lock().unwrap();
+                                       channel_state.pending_msg_events.push(events::MessageSendEvent::HandleError {
+                                               node_id: chan.get_their_node_id(),
+                                               action: e.action,
+                                       });
+                                       return;
+                               },
+                       }
+               };
+               // Because we have exclusive ownership of the channel here we can release the channel_state
+               // lock before add_update_monitor
+               if let Err(e) = self.monitor.add_update_monitor(chan_monitor.get_funding_txo().unwrap(), chan_monitor) {
+                       match e {
+                               ChannelMonitorUpdateErr::PermanentFailure => {
+                                       match handle_error!(self, Err(MsgHandleErrInternal::from_finish_shutdown("ChannelMonitor storage failure", *temporary_channel_id, chan.force_shutdown(), None))) {
+                                               Err(e) => {
+                                                       log_error!(self, "Failed to store ChannelMonitor update for funding tx generation");
+                                                       let mut channel_state = self.channel_state.lock().unwrap();
+                                                       channel_state.pending_msg_events.push(events::MessageSendEvent::HandleError {
+                                                               node_id: chan.get_their_node_id(),
+                                                               action: e.action,
+                                                       });
+                                                       return;
+                                               },
+                                               Ok(()) => unreachable!(),
+                                       }
+                               },
+                               ChannelMonitorUpdateErr::TemporaryFailure => {
+                                       // Its completely fine to continue with a FundingCreated until the monitor
+                                       // update is persisted, as long as we don't generate the FundingBroadcastSafe
+                                       // until the monitor has been safely persisted (as funding broadcast is not,
+                                       // in fact, safe).
+                                       chan.monitor_update_failed(false, false, Vec::new(), Vec::new());
+                               },
+                       }
+               }
+
+               let mut channel_state = self.channel_state.lock().unwrap();
+               channel_state.pending_msg_events.push(events::MessageSendEvent::SendFundingCreated {
+                       node_id: chan.get_their_node_id(),
+                       msg: msg,
+               });
+               match channel_state.by_id.entry(chan.channel_id()) {
+                       hash_map::Entry::Occupied(_) => {
+                               panic!("Generated duplicate funding txid?");
+                       },
+                       hash_map::Entry::Vacant(e) => {
+                               e.insert(chan);
+                       }
+               }
+       }
+
+       fn get_announcement_sigs(&self, chan: &Channel) -> Option<msgs::AnnouncementSignatures> {
+               if !chan.should_announce() { return None }
+
+               let (announcement, our_bitcoin_sig) = match chan.get_channel_announcement(self.get_our_node_id(), self.genesis_hash.clone()) {
+                       Ok(res) => res,
+                       Err(_) => return None, // Only in case of state precondition violations eg channel is closing
+               };
+               let msghash = hash_to_message!(&Sha256dHash::hash(&announcement.encode()[..])[..]);
+               let our_node_sig = self.secp_ctx.sign(&msghash, &self.our_network_key);
+
+               Some(msgs::AnnouncementSignatures {
+                       channel_id: chan.channel_id(),
+                       short_channel_id: chan.get_short_channel_id().unwrap(),
+                       node_signature: our_node_sig,
+                       bitcoin_signature: our_bitcoin_sig,
+               })
+       }
+
+       /// Processes HTLCs which are pending waiting on random forward delay.
+       ///
+       /// Should only really ever be called in response to a PendingHTLCsForwardable event.
+       /// Will likely generate further events.
+       pub fn process_pending_htlc_forwards(&self) {
+               let _ = self.total_consistency_lock.read().unwrap();
+
+               let mut new_events = Vec::new();
+               let mut failed_forwards = Vec::new();
+               let mut handle_errors = Vec::new();
+               {
+                       let mut channel_state_lock = self.channel_state.lock().unwrap();
+                       let channel_state = channel_state_lock.borrow_parts();
+
+                       for (short_chan_id, mut pending_forwards) in channel_state.forward_htlcs.drain() {
+                               if short_chan_id != 0 {
+                                       let forward_chan_id = match channel_state.short_to_id.get(&short_chan_id) {
+                                               Some(chan_id) => chan_id.clone(),
+                                               None => {
+                                                       failed_forwards.reserve(pending_forwards.len());
+                                                       for forward_info in pending_forwards.drain(..) {
+                                                               match forward_info {
+                                                                       HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info } => {
+                                                                               let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
+                                                                                       short_channel_id: prev_short_channel_id,
+                                                                                       htlc_id: prev_htlc_id,
+                                                                                       incoming_packet_shared_secret: forward_info.incoming_shared_secret,
+                                                                               });
+                                                                               failed_forwards.push((htlc_source, forward_info.payment_hash, 0x4000 | 10, None));
+                                                                       },
+                                                                       HTLCForwardInfo::FailHTLC { .. } => {
+                                                                               // Channel went away before we could fail it. This implies
+                                                                               // the channel is now on chain and our counterparty is
+                                                                               // trying to broadcast the HTLC-Timeout, but that's their
+                                                                               // problem, not ours.
+                                                                       }
+                                                               }
+                                                       }
+                                                       continue;
+                                               }
+                                       };
+                                       if let hash_map::Entry::Occupied(mut chan) = channel_state.by_id.entry(forward_chan_id) {
+                                               let mut add_htlc_msgs = Vec::new();
+                                               let mut fail_htlc_msgs = Vec::new();
+                                               for forward_info in pending_forwards.drain(..) {
+                                                       match forward_info {
+                                                               HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info } => {
+                                                                       log_trace!(self, "Adding HTLC from short id {} with payment_hash {} to channel with short id {} after delay", log_bytes!(forward_info.payment_hash.0), prev_short_channel_id, short_chan_id);
+                                                                       let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
+                                                                               short_channel_id: prev_short_channel_id,
+                                                                               htlc_id: prev_htlc_id,
+                                                                               incoming_packet_shared_secret: forward_info.incoming_shared_secret,
+                                                                       });
+                                                                       match chan.get_mut().send_htlc(forward_info.amt_to_forward, forward_info.payment_hash, forward_info.outgoing_cltv_value, htlc_source.clone(), forward_info.onion_packet.unwrap()) {
+                                                                               Err(e) => {
+                                                                                       if let ChannelError::Ignore(msg) = e {
+                                                                                               log_trace!(self, "Failed to forward HTLC with payment_hash {}: {}", log_bytes!(forward_info.payment_hash.0), msg);
+                                                                                       } else {
+                                                                                               panic!("Stated return value requirements in send_htlc() were not met");
+                                                                                       }
+                                                                                       let chan_update = self.get_channel_update(chan.get()).unwrap();
+                                                                                       failed_forwards.push((htlc_source, forward_info.payment_hash, 0x1000 | 7, Some(chan_update)));
+                                                                                       continue;
+                                                                               },
+                                                                               Ok(update_add) => {
+                                                                                       match update_add {
+                                                                                               Some(msg) => { add_htlc_msgs.push(msg); },
+                                                                                               None => {
+                                                                                                       // Nothing to do here...we're waiting on a remote
+                                                                                                       // revoke_and_ack before we can add anymore HTLCs. The Channel
+                                                                                                       // will automatically handle building the update_add_htlc and
+                                                                                                       // commitment_signed messages when we can.
+                                                                                                       // TODO: Do some kind of timer to set the channel as !is_live()
+                                                                                                       // as we don't really want others relying on us relaying through
+                                                                                                       // this channel currently :/.
+                                                                                               }
+                                                                                       }
+                                                                               }
+                                                                       }
+                                                               },
+                                                               HTLCForwardInfo::FailHTLC { htlc_id, err_packet } => {
+                                                                       log_trace!(self, "Failing HTLC back to channel with short id {} after delay", short_chan_id);
+                                                                       match chan.get_mut().get_update_fail_htlc(htlc_id, err_packet) {
+                                                                               Err(e) => {
+                                                                                       if let ChannelError::Ignore(msg) = e {
+                                                                                               log_trace!(self, "Failed to fail backwards to short_id {}: {}", short_chan_id, msg);
+                                                                                       } else {
+                                                                                               panic!("Stated return value requirements in get_update_fail_htlc() were not met");
+                                                                                       }
+                                                                                       // fail-backs are best-effort, we probably already have one
+                                                                                       // pending, and if not that's OK, if not, the channel is on
+                                                                                       // the chain and sending the HTLC-Timeout is their problem.
+                                                                                       continue;
+                                                                               },
+                                                                               Ok(Some(msg)) => { fail_htlc_msgs.push(msg); },
+                                                                               Ok(None) => {
+                                                                                       // Nothing to do here...we're waiting on a remote
+                                                                                       // revoke_and_ack before we can update the commitment
+                                                                                       // transaction. The Channel will automatically handle
+                                                                                       // building the update_fail_htlc and commitment_signed
+                                                                                       // messages when we can.
+                                                                                       // We don't need any kind of timer here as they should fail
+                                                                                       // the channel onto the chain if they can't get our
+                                                                                       // update_fail_htlc in time, it's not our problem.
+                                                                               }
+                                                                       }
+                                                               },
+                                                       }
+                                               }
+
+                                               if !add_htlc_msgs.is_empty() || !fail_htlc_msgs.is_empty() {
+                                                       let (commitment_msg, monitor) = match chan.get_mut().send_commitment() {
+                                                               Ok(res) => res,
+                                                               Err(e) => {
+                                                                       if let ChannelError::Ignore(_) = e {
+                                                                               panic!("Stated return value requirements in send_commitment() were not met");
+                                                                       }
+                                                                       //TODO: Handle...this is bad!
+                                                                       continue;
+                                                               },
+                                                       };
+                                                       if let Err(e) = self.monitor.add_update_monitor(monitor.get_funding_txo().unwrap(), monitor) {
+                                                               handle_errors.push((chan.get().get_their_node_id(), handle_monitor_err!(self, e, channel_state, chan, RAACommitmentOrder::CommitmentFirst, false, true)));
+                                                               continue;
+                                                       }
+                                                       channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
+                                                               node_id: chan.get().get_their_node_id(),
+                                                               updates: msgs::CommitmentUpdate {
+                                                                       update_add_htlcs: add_htlc_msgs,
+                                                                       update_fulfill_htlcs: Vec::new(),
+                                                                       update_fail_htlcs: fail_htlc_msgs,
+                                                                       update_fail_malformed_htlcs: Vec::new(),
+                                                                       update_fee: None,
+                                                                       commitment_signed: commitment_msg,
+                                                               },
+                                                       });
+                                               }
+                                       } else {
+                                               unreachable!();
+                                       }
+                               } else {
+                                       for forward_info in pending_forwards.drain(..) {
+                                               match forward_info {
+                                                       HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info } => {
+                                                               let prev_hop_data = HTLCPreviousHopData {
+                                                                       short_channel_id: prev_short_channel_id,
+                                                                       htlc_id: prev_htlc_id,
+                                                                       incoming_packet_shared_secret: forward_info.incoming_shared_secret,
+                                                               };
+                                                               match channel_state.claimable_htlcs.entry(forward_info.payment_hash) {
+                                                                       hash_map::Entry::Occupied(mut entry) => entry.get_mut().push((forward_info.amt_to_forward, prev_hop_data)),
+                                                                       hash_map::Entry::Vacant(entry) => { entry.insert(vec![(forward_info.amt_to_forward, prev_hop_data)]); },
+                                                               };
+                                                               new_events.push(events::Event::PaymentReceived {
+                                                                       payment_hash: forward_info.payment_hash,
+                                                                       amt: forward_info.amt_to_forward,
+                                                               });
+                                                       },
+                                                       HTLCForwardInfo::FailHTLC { .. } => {
+                                                               panic!("Got pending fail of our own HTLC");
+                                                       }
+                                               }
+                                       }
+                               }
+                       }
+               }
+
+               for (htlc_source, payment_hash, failure_code, update) in failed_forwards.drain(..) {
+                       match update {
+                               None => self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), htlc_source, &payment_hash, HTLCFailReason::Reason { failure_code, data: Vec::new() }),
+                               Some(chan_update) => self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), htlc_source, &payment_hash, HTLCFailReason::Reason { failure_code, data: chan_update.encode_with_len() }),
+                       };
+               }
+
+               for (their_node_id, err) in handle_errors.drain(..) {
+                       match handle_error!(self, err) {
+                               Ok(_) => {},
+                               Err(e) => {
+                                       if let Some(msgs::ErrorAction::IgnoreError) = e.action {
+                                       } else {
+                                               let mut channel_state = self.channel_state.lock().unwrap();
+                                               channel_state.pending_msg_events.push(events::MessageSendEvent::HandleError {
+                                                       node_id: their_node_id,
+                                                       action: e.action,
+                                               });
+                                       }
+                               },
+                       }
+               }
+
+               if new_events.is_empty() { return }
+               let mut events = self.pending_events.lock().unwrap();
+               events.append(&mut new_events);
+       }
+
+       /// Indicates that the preimage for payment_hash is unknown or the received amount is incorrect
+       /// after a PaymentReceived event, failing the HTLC back to its origin and freeing resources
+       /// along the path (including in our own channel on which we received it).
+       /// Returns false if no payment was found to fail backwards, true if the process of failing the
+       /// HTLC backwards has been started.
+       pub fn fail_htlc_backwards(&self, payment_hash: &PaymentHash) -> bool {
+               let _ = self.total_consistency_lock.read().unwrap();
+
+               let mut channel_state = Some(self.channel_state.lock().unwrap());
+               let removed_source = channel_state.as_mut().unwrap().claimable_htlcs.remove(payment_hash);
+               if let Some(mut sources) = removed_source {
+                       for (recvd_value, htlc_with_hash) in sources.drain(..) {
+                               if channel_state.is_none() { channel_state = Some(self.channel_state.lock().unwrap()); }
+                               self.fail_htlc_backwards_internal(channel_state.take().unwrap(),
+                                               HTLCSource::PreviousHopData(htlc_with_hash), payment_hash,
+                                               HTLCFailReason::Reason { failure_code: 0x4000 | 15, data: byte_utils::be64_to_array(recvd_value).to_vec() });
+                       }
+                       true
+               } else { false }
+       }
+
+       /// Fails an HTLC backwards to the sender of it to us.
+       /// Note that while we take a channel_state lock as input, we do *not* assume consistency here.
+       /// There are several callsites that do stupid things like loop over a list of payment_hashes
+       /// to fail and take the channel_state lock for each iteration (as we take ownership and may
+       /// drop it). In other words, no assumptions are made that entries in claimable_htlcs point to
+       /// still-available channels.
+       fn fail_htlc_backwards_internal(&self, mut channel_state_lock: MutexGuard<ChannelHolder>, source: HTLCSource, payment_hash: &PaymentHash, onion_error: HTLCFailReason) {
+               //TODO: There is a timing attack here where if a node fails an HTLC back to us they can
+               //identify whether we sent it or not based on the (I presume) very different runtime
+               //between the branches here. We should make this async and move it into the forward HTLCs
+               //timer handling.
+               match source {
+                       HTLCSource::OutboundRoute { ref route, .. } => {
+                               log_trace!(self, "Failing outbound payment HTLC with payment_hash {}", log_bytes!(payment_hash.0));
+                               mem::drop(channel_state_lock);
+                               match &onion_error {
+                                       &HTLCFailReason::ErrorPacket { ref err } => {
+#[cfg(test)]
+                                               let (channel_update, payment_retryable, onion_error_code) = onion_utils::process_onion_failure(&self.secp_ctx, &self.logger, &source, err.data.clone());
+#[cfg(not(test))]
+                                               let (channel_update, payment_retryable, _) = onion_utils::process_onion_failure(&self.secp_ctx, &self.logger, &source, err.data.clone());
+                                               // TODO: If we decided to blame ourselves (or one of our channels) in
+                                               // process_onion_failure we should close that channel as it implies our
+                                               // next-hop is needlessly blaming us!
+                                               if let Some(update) = channel_update {
+                                                       self.channel_state.lock().unwrap().pending_msg_events.push(
+                                                               events::MessageSendEvent::PaymentFailureNetworkUpdate {
+                                                                       update,
+                                                               }
+                                                       );
+                                               }
+                                               self.pending_events.lock().unwrap().push(
+                                                       events::Event::PaymentFailed {
+                                                               payment_hash: payment_hash.clone(),
+                                                               rejected_by_dest: !payment_retryable,
+#[cfg(test)]
+                                                               error_code: onion_error_code
+                                                       }
+                                               );
+                                       },
+                                       &HTLCFailReason::Reason {
+#[cfg(test)]
+                                                       ref failure_code,
+                                                       .. } => {
+                                               // we get a fail_malformed_htlc from the first hop
+                                               // TODO: We'd like to generate a PaymentFailureNetworkUpdate for temporary
+                                               // failures here, but that would be insufficient as Router::get_route
+                                               // generally ignores its view of our own channels as we provide them via
+                                               // ChannelDetails.
+                                               // TODO: For non-temporary failures, we really should be closing the
+                                               // channel here as we apparently can't relay through them anyway.
+                                               self.pending_events.lock().unwrap().push(
+                                                       events::Event::PaymentFailed {
+                                                               payment_hash: payment_hash.clone(),
+                                                               rejected_by_dest: route.hops.len() == 1,
+#[cfg(test)]
+                                                               error_code: Some(*failure_code),
+                                                       }
+                                               );
+                                       }
+                               }
+                       },
+                       HTLCSource::PreviousHopData(HTLCPreviousHopData { short_channel_id, htlc_id, incoming_packet_shared_secret }) => {
+                               let err_packet = match onion_error {
+                                       HTLCFailReason::Reason { failure_code, data } => {
+                                               log_trace!(self, "Failing HTLC with payment_hash {} backwards from us with code {}", log_bytes!(payment_hash.0), failure_code);
+                                               let packet = onion_utils::build_failure_packet(&incoming_packet_shared_secret, failure_code, &data[..]).encode();
+                                               onion_utils::encrypt_failure_packet(&incoming_packet_shared_secret, &packet)
+                                       },
+                                       HTLCFailReason::ErrorPacket { err } => {
+                                               log_trace!(self, "Failing HTLC with payment_hash {} backwards with pre-built ErrorPacket", log_bytes!(payment_hash.0));
+                                               onion_utils::encrypt_failure_packet(&incoming_packet_shared_secret, &err.data)
+                                       }
+                               };
+
+                               let mut forward_event = None;
+                               if channel_state_lock.forward_htlcs.is_empty() {
+                                       forward_event = Some(Duration::from_millis(MIN_HTLC_RELAY_HOLDING_CELL_MILLIS));
+                               }
+                               match channel_state_lock.forward_htlcs.entry(short_channel_id) {
+                                       hash_map::Entry::Occupied(mut entry) => {
+                                               entry.get_mut().push(HTLCForwardInfo::FailHTLC { htlc_id, err_packet });
+                                       },
+                                       hash_map::Entry::Vacant(entry) => {
+                                               entry.insert(vec!(HTLCForwardInfo::FailHTLC { htlc_id, err_packet }));
+                                       }
+                               }
+                               mem::drop(channel_state_lock);
+                               if let Some(time) = forward_event {
+                                       let mut pending_events = self.pending_events.lock().unwrap();
+                                       pending_events.push(events::Event::PendingHTLCsForwardable {
+                                               time_forwardable: time
+                                       });
+                               }
+                       },
+               }
+       }
+
+       /// Provides a payment preimage in response to a PaymentReceived event, returning true and
+       /// generating message events for the net layer to claim the payment, if possible. Thus, you
+       /// should probably kick the net layer to go send messages if this returns true!
+       ///
+       /// May panic if called except in response to a PaymentReceived event.
+       pub fn claim_funds(&self, payment_preimage: PaymentPreimage) -> bool {
+               let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0).into_inner());
+
+               let _ = self.total_consistency_lock.read().unwrap();
+
+               let mut channel_state = Some(self.channel_state.lock().unwrap());
+               let removed_source = channel_state.as_mut().unwrap().claimable_htlcs.remove(&payment_hash);
+               if let Some(mut sources) = removed_source {
+                       // TODO: We should require the user specify the expected amount so that we can claim
+                       // only payments for the correct amount, and reject payments for incorrect amounts
+                       // (which are probably middle nodes probing to break our privacy).
+                       for (_, htlc_with_hash) in sources.drain(..) {
+                               if channel_state.is_none() { channel_state = Some(self.channel_state.lock().unwrap()); }
+                               self.claim_funds_internal(channel_state.take().unwrap(), HTLCSource::PreviousHopData(htlc_with_hash), payment_preimage);
+                       }
+                       true
+               } else { false }
+       }
+       fn claim_funds_internal(&self, mut channel_state_lock: MutexGuard<ChannelHolder>, source: HTLCSource, payment_preimage: PaymentPreimage) {
+               let (their_node_id, err) = loop {
+                       match source {
+                               HTLCSource::OutboundRoute { .. } => {
+                                       mem::drop(channel_state_lock);
+                                       let mut pending_events = self.pending_events.lock().unwrap();
+                                       pending_events.push(events::Event::PaymentSent {
+                                               payment_preimage
+                                       });
+                               },
+                               HTLCSource::PreviousHopData(HTLCPreviousHopData { short_channel_id, htlc_id, .. }) => {
+                                       //TODO: Delay the claimed_funds relaying just like we do outbound relay!
+                                       let channel_state = channel_state_lock.borrow_parts();
+
+                                       let chan_id = match channel_state.short_to_id.get(&short_channel_id) {
+                                               Some(chan_id) => chan_id.clone(),
+                                               None => {
+                                                       // TODO: There is probably a channel manager somewhere that needs to
+                                                       // learn the preimage as the channel already hit the chain and that's
+                                                       // why it's missing.
+                                                       return
+                                               }
+                                       };
+
+                                       if let hash_map::Entry::Occupied(mut chan) = channel_state.by_id.entry(chan_id) {
+                                               let was_frozen_for_monitor = chan.get().is_awaiting_monitor_update();
+                                               match chan.get_mut().get_update_fulfill_htlc_and_commit(htlc_id, payment_preimage) {
+                                                       Ok((msgs, monitor_option)) => {
+                                                               if let Some(chan_monitor) = monitor_option {
+                                                                       if let Err(e) = self.monitor.add_update_monitor(chan_monitor.get_funding_txo().unwrap(), chan_monitor) {
+                                                                               if was_frozen_for_monitor {
+                                                                                       assert!(msgs.is_none());
+                                                                               } else {
+                                                                                       break (chan.get().get_their_node_id(), handle_monitor_err!(self, e, channel_state, chan, RAACommitmentOrder::CommitmentFirst, false, msgs.is_some()));
+                                                                               }
+                                                                       }
+                                                               }
+                                                               if let Some((msg, commitment_signed)) = msgs {
+                                                                       channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
+                                                                               node_id: chan.get().get_their_node_id(),
+                                                                               updates: msgs::CommitmentUpdate {
+                                                                                       update_add_htlcs: Vec::new(),
+                                                                                       update_fulfill_htlcs: vec![msg],
+                                                                                       update_fail_htlcs: Vec::new(),
+                                                                                       update_fail_malformed_htlcs: Vec::new(),
+                                                                                       update_fee: None,
+                                                                                       commitment_signed,
+                                                                               }
+                                                                       });
+                                                               }
+                                                       },
+                                                       Err(_e) => {
+                                                               // TODO: There is probably a channel manager somewhere that needs to
+                                                               // learn the preimage as the channel may be about to hit the chain.
+                                                               //TODO: Do something with e?
+                                                               return
+                                                       },
+                                               }
+                                       } else { unreachable!(); }
+                               },
+                       }
+                       return;
+               };
+
+               match handle_error!(self, err) {
+                       Ok(_) => {},
+                       Err(e) => {
+                               if let Some(msgs::ErrorAction::IgnoreError) = e.action {
+                               } else {
+                                       let mut channel_state = self.channel_state.lock().unwrap();
+                                       channel_state.pending_msg_events.push(events::MessageSendEvent::HandleError {
+                                               node_id: their_node_id,
+                                               action: e.action,
+                                       });
+                               }
+                       },
+               }
+       }
+
+       /// Gets the node_id held by this ChannelManager
+       pub fn get_our_node_id(&self) -> PublicKey {
+               PublicKey::from_secret_key(&self.secp_ctx, &self.our_network_key)
+       }
+
+       /// Used to restore channels to normal operation after a
+       /// ChannelMonitorUpdateErr::TemporaryFailure was returned from a channel monitor update
+       /// operation.
+       pub fn test_restore_channel_monitor(&self) {
+               let mut close_results = Vec::new();
+               let mut htlc_forwards = Vec::new();
+               let mut htlc_failures = Vec::new();
+               let mut pending_events = Vec::new();
+               let _ = self.total_consistency_lock.read().unwrap();
+
+               {
+                       let mut channel_lock = self.channel_state.lock().unwrap();
+                       let channel_state = channel_lock.borrow_parts();
+                       let short_to_id = channel_state.short_to_id;
+                       let pending_msg_events = channel_state.pending_msg_events;
+                       channel_state.by_id.retain(|_, channel| {
+                               if channel.is_awaiting_monitor_update() {
+                                       let chan_monitor = channel.channel_monitor();
+                                       if let Err(e) = self.monitor.add_update_monitor(chan_monitor.get_funding_txo().unwrap(), chan_monitor) {
+                                               match e {
+                                                       ChannelMonitorUpdateErr::PermanentFailure => {
+                                                               // TODO: There may be some pending HTLCs that we intended to fail
+                                                               // backwards when a monitor update failed. We should make sure
+                                                               // knowledge of those gets moved into the appropriate in-memory
+                                                               // ChannelMonitor and they get failed backwards once we get
+                                                               // on-chain confirmations.
+                                                               // Note I think #198 addresses this, so once it's merged a test
+                                                               // should be written.
+                                                               if let Some(short_id) = channel.get_short_channel_id() {
+                                                                       short_to_id.remove(&short_id);
+                                                               }
+                                                               close_results.push(channel.force_shutdown());
+                                                               if let Ok(update) = self.get_channel_update(&channel) {
+                                                                       pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
+                                                                               msg: update
+                                                                       });
+                                                               }
+                                                               false
+                                                       },
+                                                       ChannelMonitorUpdateErr::TemporaryFailure => true,
+                                               }
+                                       } else {
+                                               let (raa, commitment_update, order, pending_forwards, mut pending_failures, needs_broadcast_safe, funding_locked) = channel.monitor_updating_restored();
+                                               if !pending_forwards.is_empty() {
+                                                       htlc_forwards.push((channel.get_short_channel_id().expect("We can't have pending forwards before funding confirmation"), pending_forwards));
+                                               }
+                                               htlc_failures.append(&mut pending_failures);
+
+                                               macro_rules! handle_cs { () => {
+                                                       if let Some(update) = commitment_update {
+                                                               pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
+                                                                       node_id: channel.get_their_node_id(),
+                                                                       updates: update,
+                                                               });
+                                                       }
+                                               } }
+                                               macro_rules! handle_raa { () => {
+                                                       if let Some(revoke_and_ack) = raa {
+                                                               pending_msg_events.push(events::MessageSendEvent::SendRevokeAndACK {
+                                                                       node_id: channel.get_their_node_id(),
+                                                                       msg: revoke_and_ack,
+                                                               });
+                                                       }
+                                               } }
+                                               match order {
+                                                       RAACommitmentOrder::CommitmentFirst => {
+                                                               handle_cs!();
+                                                               handle_raa!();
+                                                       },
+                                                       RAACommitmentOrder::RevokeAndACKFirst => {
+                                                               handle_raa!();
+                                                               handle_cs!();
+                                                       },
+                                               }
+                                               if needs_broadcast_safe {
+                                                       pending_events.push(events::Event::FundingBroadcastSafe {
+                                                               funding_txo: channel.get_funding_txo().unwrap(),
+                                                               user_channel_id: channel.get_user_id(),
+                                                       });
+                                               }
+                                               if let Some(msg) = funding_locked {
+                                                       pending_msg_events.push(events::MessageSendEvent::SendFundingLocked {
+                                                               node_id: channel.get_their_node_id(),
+                                                               msg,
+                                                       });
+                                                       if let Some(announcement_sigs) = self.get_announcement_sigs(channel) {
+                                                               pending_msg_events.push(events::MessageSendEvent::SendAnnouncementSignatures {
+                                                                       node_id: channel.get_their_node_id(),
+                                                                       msg: announcement_sigs,
+                                                               });
+                                                       }
+                                                       short_to_id.insert(channel.get_short_channel_id().unwrap(), channel.channel_id());
+                                               }
+                                               true
+                                       }
+                               } else { true }
+                       });
+               }
+
+               self.pending_events.lock().unwrap().append(&mut pending_events);
+
+               for failure in htlc_failures.drain(..) {
+                       self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), failure.0, &failure.1, failure.2);
+               }
+               self.forward_htlcs(&mut htlc_forwards[..]);
+
+               for res in close_results.drain(..) {
+                       self.finish_force_close_channel(res);
+               }
+       }
+
+       fn internal_open_channel(&self, their_node_id: &PublicKey, their_local_features: LocalFeatures, msg: &msgs::OpenChannel) -> Result<(), MsgHandleErrInternal> {
+               if msg.chain_hash != self.genesis_hash {
+                       return Err(MsgHandleErrInternal::send_err_msg_no_close("Unknown genesis block hash", msg.temporary_channel_id.clone()));
+               }
+
+               let channel = Channel::new_from_req(&*self.fee_estimator, &self.keys_manager, their_node_id.clone(), their_local_features, msg, 0, Arc::clone(&self.logger), &self.default_configuration)
+                       .map_err(|e| MsgHandleErrInternal::from_chan_no_close(e, msg.temporary_channel_id))?;
+               let mut channel_state_lock = self.channel_state.lock().unwrap();
+               let channel_state = channel_state_lock.borrow_parts();
+               match channel_state.by_id.entry(channel.channel_id()) {
+                       hash_map::Entry::Occupied(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("temporary_channel_id collision!", msg.temporary_channel_id.clone())),
+                       hash_map::Entry::Vacant(entry) => {
+                               channel_state.pending_msg_events.push(events::MessageSendEvent::SendAcceptChannel {
+                                       node_id: their_node_id.clone(),
+                                       msg: channel.get_accept_channel(),
+                               });
+                               entry.insert(channel);
+                       }
+               }
+               Ok(())
+       }
+
+       fn internal_accept_channel(&self, their_node_id: &PublicKey, their_local_features: LocalFeatures, msg: &msgs::AcceptChannel) -> Result<(), MsgHandleErrInternal> {
+               let (value, output_script, user_id) = {
+                       let mut channel_lock = self.channel_state.lock().unwrap();
+                       let channel_state = channel_lock.borrow_parts();
+                       match channel_state.by_id.entry(msg.temporary_channel_id) {
+                               hash_map::Entry::Occupied(mut chan) => {
+                                       if chan.get().get_their_node_id() != *their_node_id {
+                                               //TODO: see issue #153, need a consistent behavior on obnoxious behavior from random node
+                                               return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.temporary_channel_id));
+                                       }
+                                       try_chan_entry!(self, chan.get_mut().accept_channel(&msg, &self.default_configuration, their_local_features), channel_state, chan);
+                                       (chan.get().get_value_satoshis(), chan.get().get_funding_redeemscript().to_v0_p2wsh(), chan.get().get_user_id())
+                               },
+                               //TODO: same as above
+                               hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.temporary_channel_id))
+                       }
+               };
+               let mut pending_events = self.pending_events.lock().unwrap();
+               pending_events.push(events::Event::FundingGenerationReady {
+                       temporary_channel_id: msg.temporary_channel_id,
+                       channel_value_satoshis: value,
+                       output_script: output_script,
+                       user_channel_id: user_id,
+               });
+               Ok(())
+       }
+
+       fn internal_funding_created(&self, their_node_id: &PublicKey, msg: &msgs::FundingCreated) -> Result<(), MsgHandleErrInternal> {
+               let ((funding_msg, monitor_update), mut chan) = {
+                       let mut channel_lock = self.channel_state.lock().unwrap();
+                       let channel_state = channel_lock.borrow_parts();
+                       match channel_state.by_id.entry(msg.temporary_channel_id.clone()) {
+                               hash_map::Entry::Occupied(mut chan) => {
+                                       if chan.get().get_their_node_id() != *their_node_id {
+                                               //TODO: here and below MsgHandleErrInternal, #153 case
+                                               return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.temporary_channel_id));
+                                       }
+                                       (try_chan_entry!(self, chan.get_mut().funding_created(msg), channel_state, chan), chan.remove())
+                               },
+                               hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.temporary_channel_id))
+                       }
+               };
+               // Because we have exclusive ownership of the channel here we can release the channel_state
+               // lock before add_update_monitor
+               if let Err(e) = self.monitor.add_update_monitor(monitor_update.get_funding_txo().unwrap(), monitor_update) {
+                       match e {
+                               ChannelMonitorUpdateErr::PermanentFailure => {
+                                       // Note that we reply with the new channel_id in error messages if we gave up on the
+                                       // channel, not the temporary_channel_id. This is compatible with ourselves, but the
+                                       // spec is somewhat ambiguous here. Not a huge deal since we'll send error messages for
+                                       // any messages referencing a previously-closed channel anyway.
+                                       return Err(MsgHandleErrInternal::from_finish_shutdown("ChannelMonitor storage failure", funding_msg.channel_id, chan.force_shutdown(), None));
+                               },
+                               ChannelMonitorUpdateErr::TemporaryFailure => {
+                                       // There's no problem signing a counterparty's funding transaction if our monitor
+                                       // hasn't persisted to disk yet - we can't lose money on a transaction that we haven't
+                                       // accepted payment from yet. We do, however, need to wait to send our funding_locked
+                                       // until we have persisted our monitor.
+                                       chan.monitor_update_failed(false, false, Vec::new(), Vec::new());
+                               },
+                       }
+               }
+               let mut channel_state_lock = self.channel_state.lock().unwrap();
+               let channel_state = channel_state_lock.borrow_parts();
+               match channel_state.by_id.entry(funding_msg.channel_id) {
+                       hash_map::Entry::Occupied(_) => {
+                               return Err(MsgHandleErrInternal::send_err_msg_no_close("Already had channel with the new channel_id", funding_msg.channel_id))
+                       },
+                       hash_map::Entry::Vacant(e) => {
+                               channel_state.pending_msg_events.push(events::MessageSendEvent::SendFundingSigned {
+                                       node_id: their_node_id.clone(),
+                                       msg: funding_msg,
+                               });
+                               e.insert(chan);
+                       }
+               }
+               Ok(())
+       }
+
+       fn internal_funding_signed(&self, their_node_id: &PublicKey, msg: &msgs::FundingSigned) -> Result<(), MsgHandleErrInternal> {
+               let (funding_txo, user_id) = {
+                       let mut channel_lock = self.channel_state.lock().unwrap();
+                       let channel_state = channel_lock.borrow_parts();
+                       match channel_state.by_id.entry(msg.channel_id) {
+                               hash_map::Entry::Occupied(mut chan) => {
+                                       if chan.get().get_their_node_id() != *their_node_id {
+                                               //TODO: here and below MsgHandleErrInternal, #153 case
+                                               return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
+                                       }
+                                       let chan_monitor = try_chan_entry!(self, chan.get_mut().funding_signed(&msg), channel_state, chan);
+                                       if let Err(e) = self.monitor.add_update_monitor(chan_monitor.get_funding_txo().unwrap(), chan_monitor) {
+                                               return_monitor_err!(self, e, channel_state, chan, RAACommitmentOrder::RevokeAndACKFirst, false, false);
+                                       }
+                                       (chan.get().get_funding_txo().unwrap(), chan.get().get_user_id())
+                               },
+                               hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
+                       }
+               };
+               let mut pending_events = self.pending_events.lock().unwrap();
+               pending_events.push(events::Event::FundingBroadcastSafe {
+                       funding_txo: funding_txo,
+                       user_channel_id: user_id,
+               });
+               Ok(())
+       }
+
+       fn internal_funding_locked(&self, their_node_id: &PublicKey, msg: &msgs::FundingLocked) -> Result<(), MsgHandleErrInternal> {
+               let mut channel_state_lock = self.channel_state.lock().unwrap();
+               let channel_state = channel_state_lock.borrow_parts();
+               match channel_state.by_id.entry(msg.channel_id) {
+                       hash_map::Entry::Occupied(mut chan) => {
+                               if chan.get().get_their_node_id() != *their_node_id {
+                                       //TODO: here and below MsgHandleErrInternal, #153 case
+                                       return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
+                               }
+                               try_chan_entry!(self, chan.get_mut().funding_locked(&msg), channel_state, chan);
+                               if let Some(announcement_sigs) = self.get_announcement_sigs(chan.get()) {
+                                       channel_state.pending_msg_events.push(events::MessageSendEvent::SendAnnouncementSignatures {
+                                               node_id: their_node_id.clone(),
+                                               msg: announcement_sigs,
+                                       });
+                               }
+                               Ok(())
+                       },
+                       hash_map::Entry::Vacant(_) => Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
+               }
+       }
+
+       fn internal_shutdown(&self, their_node_id: &PublicKey, msg: &msgs::Shutdown) -> Result<(), MsgHandleErrInternal> {
+               let (mut dropped_htlcs, chan_option) = {
+                       let mut channel_state_lock = self.channel_state.lock().unwrap();
+                       let channel_state = channel_state_lock.borrow_parts();
+
+                       match channel_state.by_id.entry(msg.channel_id.clone()) {
+                               hash_map::Entry::Occupied(mut chan_entry) => {
+                                       if chan_entry.get().get_their_node_id() != *their_node_id {
+                                               //TODO: here and below MsgHandleErrInternal, #153 case
+                                               return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
+                                       }
+                                       let (shutdown, closing_signed, dropped_htlcs) = try_chan_entry!(self, chan_entry.get_mut().shutdown(&*self.fee_estimator, &msg), channel_state, chan_entry);
+                                       if let Some(msg) = shutdown {
+                                               channel_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
+                                                       node_id: their_node_id.clone(),
+                                                       msg,
+                                               });
+                                       }
+                                       if let Some(msg) = closing_signed {
+                                               channel_state.pending_msg_events.push(events::MessageSendEvent::SendClosingSigned {
+                                                       node_id: their_node_id.clone(),
+                                                       msg,
+                                               });
+                                       }
+                                       if chan_entry.get().is_shutdown() {
+                                               if let Some(short_id) = chan_entry.get().get_short_channel_id() {
+                                                       channel_state.short_to_id.remove(&short_id);
+                                               }
+                                               (dropped_htlcs, Some(chan_entry.remove_entry().1))
+                                       } else { (dropped_htlcs, None) }
+                               },
+                               hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
+                       }
+               };
+               for htlc_source in dropped_htlcs.drain(..) {
+                       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() });
+               }
+               if let Some(chan) = chan_option {
+                       if let Ok(update) = self.get_channel_update(&chan) {
+                               let mut channel_state = self.channel_state.lock().unwrap();
+                               channel_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
+                                       msg: update
+                               });
+                       }
+               }
+               Ok(())
+       }
+
+       fn internal_closing_signed(&self, their_node_id: &PublicKey, msg: &msgs::ClosingSigned) -> Result<(), MsgHandleErrInternal> {
+               let (tx, chan_option) = {
+                       let mut channel_state_lock = self.channel_state.lock().unwrap();
+                       let channel_state = channel_state_lock.borrow_parts();
+                       match channel_state.by_id.entry(msg.channel_id.clone()) {
+                               hash_map::Entry::Occupied(mut chan_entry) => {
+                                       if chan_entry.get().get_their_node_id() != *their_node_id {
+                                               //TODO: here and below MsgHandleErrInternal, #153 case
+                                               return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
+                                       }
+                                       let (closing_signed, tx) = try_chan_entry!(self, chan_entry.get_mut().closing_signed(&*self.fee_estimator, &msg), channel_state, chan_entry);
+                                       if let Some(msg) = closing_signed {
+                                               channel_state.pending_msg_events.push(events::MessageSendEvent::SendClosingSigned {
+                                                       node_id: their_node_id.clone(),
+                                                       msg,
+                                               });
+                                       }
+                                       if tx.is_some() {
+                                               // We're done with this channel, we've got a signed closing transaction and
+                                               // will send the closing_signed back to the remote peer upon return. This
+                                               // also implies there are no pending HTLCs left on the channel, so we can
+                                               // fully delete it from tracking (the channel monitor is still around to
+                                               // watch for old state broadcasts)!
+                                               if let Some(short_id) = chan_entry.get().get_short_channel_id() {
+                                                       channel_state.short_to_id.remove(&short_id);
+                                               }
+                                               (tx, Some(chan_entry.remove_entry().1))
+                                       } else { (tx, None) }
+                               },
+                               hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
+                       }
+               };
+               if let Some(broadcast_tx) = tx {
+                       self.tx_broadcaster.broadcast_transaction(&broadcast_tx);
+               }
+               if let Some(chan) = chan_option {
+                       if let Ok(update) = self.get_channel_update(&chan) {
+                               let mut channel_state = self.channel_state.lock().unwrap();
+                               channel_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
+                                       msg: update
+                               });
+                       }
+               }
+               Ok(())
+       }
+
+       fn internal_update_add_htlc(&self, their_node_id: &PublicKey, msg: &msgs::UpdateAddHTLC) -> Result<(), MsgHandleErrInternal> {
+               //TODO: BOLT 4 points out a specific attack where a peer may re-send an onion packet and
+               //determine the state of the payment based on our response/if we forward anything/the time
+               //we take to respond. We should take care to avoid allowing such an attack.
+               //
+               //TODO: There exists a further attack where a node may garble the onion data, forward it to
+               //us repeatedly garbled in different ways, and compare our error messages, which are
+               //encrypted with the same key. It's not immediately obvious how to usefully exploit that,
+               //but we should prevent it anyway.
+
+               let (mut pending_forward_info, mut channel_state_lock) = self.decode_update_add_htlc_onion(msg);
+               let channel_state = channel_state_lock.borrow_parts();
+
+               match channel_state.by_id.entry(msg.channel_id) {
+                       hash_map::Entry::Occupied(mut chan) => {
+                               if chan.get().get_their_node_id() != *their_node_id {
+                                       //TODO: here MsgHandleErrInternal, #153 case
+                                       return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
+                               }
+                               if !chan.get().is_usable() {
+                                       // If the update_add is completely bogus, the call will Err and we will close,
+                                       // but if we've sent a shutdown and they haven't acknowledged it yet, we just
+                                       // want to reject the new HTLC and fail it backwards instead of forwarding.
+                                       if let PendingHTLCStatus::Forward(PendingForwardHTLCInfo { incoming_shared_secret, .. }) = pending_forward_info {
+                                               let chan_update = self.get_channel_update(chan.get());
+                                               pending_forward_info = PendingHTLCStatus::Fail(HTLCFailureMsg::Relay(msgs::UpdateFailHTLC {
+                                                       channel_id: msg.channel_id,
+                                                       htlc_id: msg.htlc_id,
+                                                       reason: if let Ok(update) = chan_update {
+                                                               // TODO: Note that |20 is defined as "channel FROM the processing
+                                                               // node has been disabled" (emphasis mine), which seems to imply
+                                                               // that we can't return |20 for an inbound channel being disabled.
+                                                               // This probably needs a spec update but should definitely be
+                                                               // allowed.
+                                                               onion_utils::build_first_hop_failure_packet(&incoming_shared_secret, 0x1000|20, &{
+                                                                       let mut res = Vec::with_capacity(8 + 128);
+                                                                       res.extend_from_slice(&byte_utils::be16_to_array(update.contents.flags));
+                                                                       res.extend_from_slice(&update.encode_with_len()[..]);
+                                                                       res
+                                                               }[..])
+                                                       } else {
+                                                               // This can only happen if the channel isn't in the fully-funded
+                                                               // state yet, implying our counterparty is trying to route payments
+                                                               // over the channel back to themselves (cause no one else should
+                                                               // know the short_id is a lightning channel yet). We should have no
+                                                               // problem just calling this unknown_next_peer
+                                                               onion_utils::build_first_hop_failure_packet(&incoming_shared_secret, 0x4000|10, &[])
+                                                       },
+                                               }));
+                                       }
+                               }
+                               try_chan_entry!(self, chan.get_mut().update_add_htlc(&msg, pending_forward_info), channel_state, chan);
+                       },
+                       hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
+               }
+               Ok(())
+       }
+
+       fn internal_update_fulfill_htlc(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFulfillHTLC) -> Result<(), MsgHandleErrInternal> {
+               let mut channel_lock = self.channel_state.lock().unwrap();
+               let htlc_source = {
+                       let channel_state = channel_lock.borrow_parts();
+                       match channel_state.by_id.entry(msg.channel_id) {
+                               hash_map::Entry::Occupied(mut chan) => {
+                                       if chan.get().get_their_node_id() != *their_node_id {
+                                               //TODO: here and below MsgHandleErrInternal, #153 case
+                                               return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
+                                       }
+                                       try_chan_entry!(self, chan.get_mut().update_fulfill_htlc(&msg), channel_state, chan)
+                               },
+                               hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
+                       }
+               };
+               self.claim_funds_internal(channel_lock, htlc_source, msg.payment_preimage.clone());
+               Ok(())
+       }
+
+       fn internal_update_fail_htlc(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFailHTLC) -> Result<(), MsgHandleErrInternal> {
+               let mut channel_lock = self.channel_state.lock().unwrap();
+               let channel_state = channel_lock.borrow_parts();
+               match channel_state.by_id.entry(msg.channel_id) {
+                       hash_map::Entry::Occupied(mut chan) => {
+                               if chan.get().get_their_node_id() != *their_node_id {
+                                       //TODO: here and below MsgHandleErrInternal, #153 case
+                                       return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
+                               }
+                               try_chan_entry!(self, chan.get_mut().update_fail_htlc(&msg, HTLCFailReason::ErrorPacket { err: msg.reason.clone() }), channel_state, chan);
+                       },
+                       hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
+               }
+               Ok(())
+       }
+
+       fn internal_update_fail_malformed_htlc(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFailMalformedHTLC) -> Result<(), MsgHandleErrInternal> {
+               let mut channel_lock = self.channel_state.lock().unwrap();
+               let channel_state = channel_lock.borrow_parts();
+               match channel_state.by_id.entry(msg.channel_id) {
+                       hash_map::Entry::Occupied(mut chan) => {
+                               if chan.get().get_their_node_id() != *their_node_id {
+                                       //TODO: here and below MsgHandleErrInternal, #153 case
+                                       return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
+                               }
+                               if (msg.failure_code & 0x8000) == 0 {
+                                       try_chan_entry!(self, Err(ChannelError::Close("Got update_fail_malformed_htlc with BADONION not set")), channel_state, chan);
+                               }
+                               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);
+                               Ok(())
+                       },
+                       hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
+               }
+       }
+
+       fn internal_commitment_signed(&self, their_node_id: &PublicKey, msg: &msgs::CommitmentSigned) -> Result<(), MsgHandleErrInternal> {
+               let mut channel_state_lock = self.channel_state.lock().unwrap();
+               let channel_state = channel_state_lock.borrow_parts();
+               match channel_state.by_id.entry(msg.channel_id) {
+                       hash_map::Entry::Occupied(mut chan) => {
+                               if chan.get().get_their_node_id() != *their_node_id {
+                                       //TODO: here and below MsgHandleErrInternal, #153 case
+                                       return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
+                               }
+                               let (revoke_and_ack, commitment_signed, closing_signed, chan_monitor) =
+                                       try_chan_entry!(self, chan.get_mut().commitment_signed(&msg, &*self.fee_estimator), channel_state, chan);
+                               if let Err(e) = self.monitor.add_update_monitor(chan_monitor.get_funding_txo().unwrap(), chan_monitor) {
+                                       return_monitor_err!(self, e, channel_state, chan, RAACommitmentOrder::RevokeAndACKFirst, true, commitment_signed.is_some());
+                                       //TODO: Rebroadcast closing_signed if present on monitor update restoration
+                               }
+                               channel_state.pending_msg_events.push(events::MessageSendEvent::SendRevokeAndACK {
+                                       node_id: their_node_id.clone(),
+                                       msg: revoke_and_ack,
+                               });
+                               if let Some(msg) = commitment_signed {
+                                       channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
+                                               node_id: their_node_id.clone(),
+                                               updates: msgs::CommitmentUpdate {
+                                                       update_add_htlcs: Vec::new(),
+                                                       update_fulfill_htlcs: Vec::new(),
+                                                       update_fail_htlcs: Vec::new(),
+                                                       update_fail_malformed_htlcs: Vec::new(),
+                                                       update_fee: None,
+                                                       commitment_signed: msg,
+                                               },
+                                       });
+                               }
+                               if let Some(msg) = closing_signed {
+                                       channel_state.pending_msg_events.push(events::MessageSendEvent::SendClosingSigned {
+                                               node_id: their_node_id.clone(),
+                                               msg,
+                                       });
+                               }
+                               Ok(())
+                       },
+                       hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
+               }
+       }
+
+       #[inline]
+       fn forward_htlcs(&self, per_source_pending_forwards: &mut [(u64, Vec<(PendingForwardHTLCInfo, u64)>)]) {
+               for &mut (prev_short_channel_id, ref mut pending_forwards) in per_source_pending_forwards {
+                       let mut forward_event = None;
+                       if !pending_forwards.is_empty() {
+                               let mut channel_state = self.channel_state.lock().unwrap();
+                               if channel_state.forward_htlcs.is_empty() {
+                                       forward_event = Some(Duration::from_millis(MIN_HTLC_RELAY_HOLDING_CELL_MILLIS))
+                               }
+                               for (forward_info, prev_htlc_id) in pending_forwards.drain(..) {
+                                       match channel_state.forward_htlcs.entry(forward_info.short_channel_id) {
+                                               hash_map::Entry::Occupied(mut entry) => {
+                                                       entry.get_mut().push(HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info });
+                                               },
+                                               hash_map::Entry::Vacant(entry) => {
+                                                       entry.insert(vec!(HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info }));
+                                               }
+                                       }
+                               }
+                       }
+                       match forward_event {
+                               Some(time) => {
+                                       let mut pending_events = self.pending_events.lock().unwrap();
+                                       pending_events.push(events::Event::PendingHTLCsForwardable {
+                                               time_forwardable: time
+                                       });
+                               }
+                               None => {},
+                       }
+               }
+       }
+
+       fn internal_revoke_and_ack(&self, their_node_id: &PublicKey, msg: &msgs::RevokeAndACK) -> Result<(), MsgHandleErrInternal> {
+               let (pending_forwards, mut pending_failures, short_channel_id) = {
+                       let mut channel_state_lock = self.channel_state.lock().unwrap();
+                       let channel_state = channel_state_lock.borrow_parts();
+                       match channel_state.by_id.entry(msg.channel_id) {
+                               hash_map::Entry::Occupied(mut chan) => {
+                                       if chan.get().get_their_node_id() != *their_node_id {
+                                               //TODO: here and below MsgHandleErrInternal, #153 case
+                                               return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
+                                       }
+                                       let was_frozen_for_monitor = chan.get().is_awaiting_monitor_update();
+                                       let (commitment_update, pending_forwards, pending_failures, closing_signed, chan_monitor) =
+                                               try_chan_entry!(self, chan.get_mut().revoke_and_ack(&msg, &*self.fee_estimator), channel_state, chan);
+                                       if let Err(e) = self.monitor.add_update_monitor(chan_monitor.get_funding_txo().unwrap(), chan_monitor) {
+                                               if was_frozen_for_monitor {
+                                                       assert!(commitment_update.is_none() && closing_signed.is_none() && pending_forwards.is_empty() && pending_failures.is_empty());
+                                                       return Err(MsgHandleErrInternal::ignore_no_close("Previous monitor update failure prevented responses to RAA"));
+                                               } else {
+                                                       return_monitor_err!(self, e, channel_state, chan, RAACommitmentOrder::CommitmentFirst, false, commitment_update.is_some(), pending_forwards, pending_failures);
+                                               }
+                                       }
+                                       if let Some(updates) = commitment_update {
+                                               channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
+                                                       node_id: their_node_id.clone(),
+                                                       updates,
+                                               });
+                                       }
+                                       if let Some(msg) = closing_signed {
+                                               channel_state.pending_msg_events.push(events::MessageSendEvent::SendClosingSigned {
+                                                       node_id: their_node_id.clone(),
+                                                       msg,
+                                               });
+                                       }
+                                       (pending_forwards, pending_failures, chan.get().get_short_channel_id().expect("RAA should only work on a short-id-available channel"))
+                               },
+                               hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
+                       }
+               };
+               for failure in pending_failures.drain(..) {
+                       self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), failure.0, &failure.1, failure.2);
+               }
+               self.forward_htlcs(&mut [(short_channel_id, pending_forwards)]);
+
+               Ok(())
+       }
+
+       fn internal_update_fee(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFee) -> Result<(), MsgHandleErrInternal> {
+               let mut channel_lock = self.channel_state.lock().unwrap();
+               let channel_state = channel_lock.borrow_parts();
+               match channel_state.by_id.entry(msg.channel_id) {
+                       hash_map::Entry::Occupied(mut chan) => {
+                               if chan.get().get_their_node_id() != *their_node_id {
+                                       //TODO: here and below MsgHandleErrInternal, #153 case
+                                       return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
+                               }
+                               try_chan_entry!(self, chan.get_mut().update_fee(&*self.fee_estimator, &msg), channel_state, chan);
+                       },
+                       hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
+               }
+               Ok(())
+       }
+
+       fn internal_announcement_signatures(&self, their_node_id: &PublicKey, msg: &msgs::AnnouncementSignatures) -> Result<(), MsgHandleErrInternal> {
+               let mut channel_state_lock = self.channel_state.lock().unwrap();
+               let channel_state = channel_state_lock.borrow_parts();
+
+               match channel_state.by_id.entry(msg.channel_id) {
+                       hash_map::Entry::Occupied(mut chan) => {
+                               if chan.get().get_their_node_id() != *their_node_id {
+                                       return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
+                               }
+                               if !chan.get().is_usable() {
+                                       return Err(MsgHandleErrInternal::from_no_close(HandleError{err: "Got an announcement_signatures before we were ready for it", action: Some(msgs::ErrorAction::IgnoreError)}));
+                               }
+
+                               let our_node_id = self.get_our_node_id();
+                               let (announcement, our_bitcoin_sig) =
+                                       try_chan_entry!(self, chan.get_mut().get_channel_announcement(our_node_id.clone(), self.genesis_hash.clone()), channel_state, chan);
+
+                               let were_node_one = announcement.node_id_1 == our_node_id;
+                               let msghash = hash_to_message!(&Sha256dHash::hash(&announcement.encode()[..])[..]);
+                               if self.secp_ctx.verify(&msghash, &msg.node_signature, if were_node_one { &announcement.node_id_2 } else { &announcement.node_id_1 }).is_err() ||
+                                               self.secp_ctx.verify(&msghash, &msg.bitcoin_signature, if were_node_one { &announcement.bitcoin_key_2 } else { &announcement.bitcoin_key_1 }).is_err() {
+                                       try_chan_entry!(self, Err(ChannelError::Close("Bad announcement_signatures node_signature")), channel_state, chan);
+                               }
+
+                               let our_node_sig = self.secp_ctx.sign(&msghash, &self.our_network_key);
+
+                               channel_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelAnnouncement {
+                                       msg: msgs::ChannelAnnouncement {
+                                               node_signature_1: if were_node_one { our_node_sig } else { msg.node_signature },
+                                               node_signature_2: if were_node_one { msg.node_signature } else { our_node_sig },
+                                               bitcoin_signature_1: if were_node_one { our_bitcoin_sig } else { msg.bitcoin_signature },
+                                               bitcoin_signature_2: if were_node_one { msg.bitcoin_signature } else { our_bitcoin_sig },
+                                               contents: announcement,
+                                       },
+                                       update_msg: self.get_channel_update(chan.get()).unwrap(), // can only fail if we're not in a ready state
+                               });
+                       },
+                       hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
+               }
+               Ok(())
+       }
+
+       fn internal_channel_reestablish(&self, their_node_id: &PublicKey, msg: &msgs::ChannelReestablish) -> Result<(), MsgHandleErrInternal> {
+               let mut channel_state_lock = self.channel_state.lock().unwrap();
+               let channel_state = channel_state_lock.borrow_parts();
+
+               match channel_state.by_id.entry(msg.channel_id) {
+                       hash_map::Entry::Occupied(mut chan) => {
+                               if chan.get().get_their_node_id() != *their_node_id {
+                                       return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
+                               }
+                               let (funding_locked, revoke_and_ack, commitment_update, channel_monitor, mut order, shutdown) =
+                                       try_chan_entry!(self, chan.get_mut().channel_reestablish(msg), channel_state, chan);
+                               if let Some(monitor) = channel_monitor {
+                                       if let Err(e) = self.monitor.add_update_monitor(monitor.get_funding_txo().unwrap(), monitor) {
+                                               // channel_reestablish doesn't guarantee the order it returns is sensical
+                                               // for the messages it returns, but if we're setting what messages to
+                                               // re-transmit on monitor update success, we need to make sure it is sane.
+                                               if revoke_and_ack.is_none() {
+                                                       order = RAACommitmentOrder::CommitmentFirst;
+                                               }
+                                               if commitment_update.is_none() {
+                                                       order = RAACommitmentOrder::RevokeAndACKFirst;
+                                               }
+                                               return_monitor_err!(self, e, channel_state, chan, order, revoke_and_ack.is_some(), commitment_update.is_some());
+                                               //TODO: Resend the funding_locked if needed once we get the monitor running again
+                                       }
+                               }
+                               if let Some(msg) = funding_locked {
+                                       channel_state.pending_msg_events.push(events::MessageSendEvent::SendFundingLocked {
+                                               node_id: their_node_id.clone(),
+                                               msg
+                                       });
+                               }
+                               macro_rules! send_raa { () => {
+                                       if let Some(msg) = revoke_and_ack {
+                                               channel_state.pending_msg_events.push(events::MessageSendEvent::SendRevokeAndACK {
+                                                       node_id: their_node_id.clone(),
+                                                       msg
+                                               });
+                                       }
+                               } }
+                               macro_rules! send_cu { () => {
+                                       if let Some(updates) = commitment_update {
+                                               channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
+                                                       node_id: their_node_id.clone(),
+                                                       updates
+                                               });
+                                       }
+                               } }
+                               match order {
+                                       RAACommitmentOrder::RevokeAndACKFirst => {
+                                               send_raa!();
+                                               send_cu!();
+                                       },
+                                       RAACommitmentOrder::CommitmentFirst => {
+                                               send_cu!();
+                                               send_raa!();
+                                       },
+                               }
+                               if let Some(msg) = shutdown {
+                                       channel_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
+                                               node_id: their_node_id.clone(),
+                                               msg,
+                                       });
+                               }
+                               Ok(())
+                       },
+                       hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
+               }
+       }
+
+       /// Begin Update fee process. Allowed only on an outbound channel.
+       /// If successful, will generate a UpdateHTLCs event, so you should probably poll
+       /// PeerManager::process_events afterwards.
+       /// Note: This API is likely to change!
+       #[doc(hidden)]
+       pub fn update_fee(&self, channel_id: [u8;32], feerate_per_kw: u64) -> Result<(), APIError> {
+               let _ = self.total_consistency_lock.read().unwrap();
+               let their_node_id;
+               let err: Result<(), _> = loop {
+                       let mut channel_state_lock = self.channel_state.lock().unwrap();
+                       let channel_state = channel_state_lock.borrow_parts();
+
+                       match channel_state.by_id.entry(channel_id) {
+                               hash_map::Entry::Vacant(_) => return Err(APIError::APIMisuseError{err: "Failed to find corresponding channel"}),
+                               hash_map::Entry::Occupied(mut chan) => {
+                                       if !chan.get().is_outbound() {
+                                               return Err(APIError::APIMisuseError{err: "update_fee cannot be sent for an inbound channel"});
+                                       }
+                                       if chan.get().is_awaiting_monitor_update() {
+                                               return Err(APIError::MonitorUpdateFailed);
+                                       }
+                                       if !chan.get().is_live() {
+                                               return Err(APIError::ChannelUnavailable{err: "Channel is either not yet fully established or peer is currently disconnected"});
+                                       }
+                                       their_node_id = chan.get().get_their_node_id();
+                                       if let Some((update_fee, commitment_signed, chan_monitor)) =
+                                                       break_chan_entry!(self, chan.get_mut().send_update_fee_and_commit(feerate_per_kw), channel_state, chan)
+                                       {
+                                               if let Err(_e) = self.monitor.add_update_monitor(chan_monitor.get_funding_txo().unwrap(), chan_monitor) {
+                                                       unimplemented!();
+                                               }
+                                               channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
+                                                       node_id: chan.get().get_their_node_id(),
+                                                       updates: msgs::CommitmentUpdate {
+                                                               update_add_htlcs: Vec::new(),
+                                                               update_fulfill_htlcs: Vec::new(),
+                                                               update_fail_htlcs: Vec::new(),
+                                                               update_fail_malformed_htlcs: Vec::new(),
+                                                               update_fee: Some(update_fee),
+                                                               commitment_signed,
+                                                       },
+                                               });
+                                       }
+                               },
+                       }
+                       return Ok(())
+               };
+
+               match handle_error!(self, err) {
+                       Ok(_) => unreachable!(),
+                       Err(e) => {
+                               if let Some(msgs::ErrorAction::IgnoreError) = e.action {
+                               } else {
+                                       log_error!(self, "Got bad keys: {}!", e.err);
+                                       let mut channel_state = self.channel_state.lock().unwrap();
+                                       channel_state.pending_msg_events.push(events::MessageSendEvent::HandleError {
+                                               node_id: their_node_id,
+                                               action: e.action,
+                                       });
+                               }
+                               Err(APIError::APIMisuseError { err: e.err })
+                       },
+               }
+       }
+}
+
+impl events::MessageSendEventsProvider for ChannelManager {
+       fn get_and_clear_pending_msg_events(&self) -> Vec<events::MessageSendEvent> {
+               // TODO: Event release to users and serialization is currently race-y: it's very easy for a
+               // user to serialize a ChannelManager with pending events in it and lose those events on
+               // restart. This is doubly true for the fail/fulfill-backs from monitor events!
+               {
+                       //TODO: This behavior should be documented.
+                       for htlc_update in self.monitor.fetch_pending_htlc_updated() {
+                               if let Some(preimage) = htlc_update.payment_preimage {
+                                       log_trace!(self, "Claiming HTLC with preimage {} from our monitor", log_bytes!(preimage.0));
+                                       self.claim_funds_internal(self.channel_state.lock().unwrap(), htlc_update.source, preimage);
+                               } else {
+                                       log_trace!(self, "Failing HTLC with hash {} from our monitor", log_bytes!(htlc_update.payment_hash.0));
+                                       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() });
+                               }
+                       }
+               }
+
+               let mut ret = Vec::new();
+               let mut channel_state = self.channel_state.lock().unwrap();
+               mem::swap(&mut ret, &mut channel_state.pending_msg_events);
+               ret
+       }
+}
+
+impl events::EventsProvider for ChannelManager {
+       fn get_and_clear_pending_events(&self) -> Vec<events::Event> {
+               // TODO: Event release to users and serialization is currently race-y: it's very easy for a
+               // user to serialize a ChannelManager with pending events in it and lose those events on
+               // restart. This is doubly true for the fail/fulfill-backs from monitor events!
+               {
+                       //TODO: This behavior should be documented.
+                       for htlc_update in self.monitor.fetch_pending_htlc_updated() {
+                               if let Some(preimage) = htlc_update.payment_preimage {
+                                       log_trace!(self, "Claiming HTLC with preimage {} from our monitor", log_bytes!(preimage.0));
+                                       self.claim_funds_internal(self.channel_state.lock().unwrap(), htlc_update.source, preimage);
+                               } else {
+                                       log_trace!(self, "Failing HTLC with hash {} from our monitor", log_bytes!(htlc_update.payment_hash.0));
+                                       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() });
+                               }
+                       }
+               }
+
+               let mut ret = Vec::new();
+               let mut pending_events = self.pending_events.lock().unwrap();
+               mem::swap(&mut ret, &mut *pending_events);
+               ret
+       }
+}
+
+impl ChainListener for ChannelManager {
+       fn block_connected(&self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], indexes_of_txn_matched: &[u32]) {
+               let header_hash = header.bitcoin_hash();
+               log_trace!(self, "Block {} at height {} connected with {} txn matched", header_hash, height, txn_matched.len());
+               let _ = self.total_consistency_lock.read().unwrap();
+               let mut failed_channels = Vec::new();
+               {
+                       let mut channel_lock = self.channel_state.lock().unwrap();
+                       let channel_state = channel_lock.borrow_parts();
+                       let short_to_id = channel_state.short_to_id;
+                       let pending_msg_events = channel_state.pending_msg_events;
+                       channel_state.by_id.retain(|_, channel| {
+                               let chan_res = channel.block_connected(header, height, txn_matched, indexes_of_txn_matched);
+                               if let Ok(Some(funding_locked)) = chan_res {
+                                       pending_msg_events.push(events::MessageSendEvent::SendFundingLocked {
+                                               node_id: channel.get_their_node_id(),
+                                               msg: funding_locked,
+                                       });
+                                       if let Some(announcement_sigs) = self.get_announcement_sigs(channel) {
+                                               pending_msg_events.push(events::MessageSendEvent::SendAnnouncementSignatures {
+                                                       node_id: channel.get_their_node_id(),
+                                                       msg: announcement_sigs,
+                                               });
+                                       }
+                                       short_to_id.insert(channel.get_short_channel_id().unwrap(), channel.channel_id());
+                               } else if let Err(e) = chan_res {
+                                       pending_msg_events.push(events::MessageSendEvent::HandleError {
+                                               node_id: channel.get_their_node_id(),
+                                               action: Some(msgs::ErrorAction::SendErrorMessage { msg: e }),
+                                       });
+                                       return false;
+                               }
+                               if let Some(funding_txo) = channel.get_funding_txo() {
+                                       for tx in txn_matched {
+                                               for inp in tx.input.iter() {
+                                                       if inp.previous_output == funding_txo.into_bitcoin_outpoint() {
+                                                               log_trace!(self, "Detected channel-closing tx {} spending {}:{}, closing channel {}", tx.txid(), inp.previous_output.txid, inp.previous_output.vout, log_bytes!(channel.channel_id()));
+                                                               if let Some(short_id) = channel.get_short_channel_id() {
+                                                                       short_to_id.remove(&short_id);
+                                                               }
+                                                               // It looks like our counterparty went on-chain. We go ahead and
+                                                               // broadcast our latest local state as well here, just in case its
+                                                               // some kind of SPV attack, though we expect these to be dropped.
+                                                               failed_channels.push(channel.force_shutdown());
+                                                               if let Ok(update) = self.get_channel_update(&channel) {
+                                                                       pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
+                                                                               msg: update
+                                                                       });
+                                                               }
+                                                               return false;
+                                                       }
+                                               }
+                                       }
+                               }
+                               if channel.is_funding_initiated() && channel.channel_monitor().would_broadcast_at_height(height) {
+                                       if let Some(short_id) = channel.get_short_channel_id() {
+                                               short_to_id.remove(&short_id);
+                                       }
+                                       failed_channels.push(channel.force_shutdown());
+                                       // If would_broadcast_at_height() is true, the channel_monitor will broadcast
+                                       // the latest local tx for us, so we should skip that here (it doesn't really
+                                       // hurt anything, but does make tests a bit simpler).
+                                       failed_channels.last_mut().unwrap().0 = Vec::new();
+                                       if let Ok(update) = self.get_channel_update(&channel) {
+                                               pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
+                                                       msg: update
+                                               });
+                                       }
+                                       return false;
+                               }
+                               true
+                       });
+               }
+               for failure in failed_channels.drain(..) {
+                       self.finish_force_close_channel(failure);
+               }
+               self.latest_block_height.store(height as usize, Ordering::Release);
+               *self.last_block_hash.try_lock().expect("block_(dis)connected must not be called in parallel") = header_hash;
+       }
+
+       /// We force-close the channel without letting our counterparty participate in the shutdown
+       fn block_disconnected(&self, header: &BlockHeader, _: u32) {
+               let _ = self.total_consistency_lock.read().unwrap();
+               let mut failed_channels = Vec::new();
+               {
+                       let mut channel_lock = self.channel_state.lock().unwrap();
+                       let channel_state = channel_lock.borrow_parts();
+                       let short_to_id = channel_state.short_to_id;
+                       let pending_msg_events = channel_state.pending_msg_events;
+                       channel_state.by_id.retain(|_,  v| {
+                               if v.block_disconnected(header) {
+                                       if let Some(short_id) = v.get_short_channel_id() {
+                                               short_to_id.remove(&short_id);
+                                       }
+                                       failed_channels.push(v.force_shutdown());
+                                       if let Ok(update) = self.get_channel_update(&v) {
+                                               pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
+                                                       msg: update
+                                               });
+                                       }
+                                       false
+                               } else {
+                                       true
+                               }
+                       });
+               }
+               for failure in failed_channels.drain(..) {
+                       self.finish_force_close_channel(failure);
+               }
+               self.latest_block_height.fetch_sub(1, Ordering::AcqRel);
+               *self.last_block_hash.try_lock().expect("block_(dis)connected must not be called in parallel") = header.bitcoin_hash();
+       }
+}
+
+impl ChannelMessageHandler for ChannelManager {
+       //TODO: Handle errors and close channel (or so)
+       fn handle_open_channel(&self, their_node_id: &PublicKey, their_local_features: LocalFeatures, msg: &msgs::OpenChannel) -> Result<(), HandleError> {
+               let _ = self.total_consistency_lock.read().unwrap();
+               handle_error!(self, self.internal_open_channel(their_node_id, their_local_features, msg))
+       }
+
+       fn handle_accept_channel(&self, their_node_id: &PublicKey, their_local_features: LocalFeatures, msg: &msgs::AcceptChannel) -> Result<(), HandleError> {
+               let _ = self.total_consistency_lock.read().unwrap();
+               handle_error!(self, self.internal_accept_channel(their_node_id, their_local_features, msg))
+       }
+
+       fn handle_funding_created(&self, their_node_id: &PublicKey, msg: &msgs::FundingCreated) -> Result<(), HandleError> {
+               let _ = self.total_consistency_lock.read().unwrap();
+               handle_error!(self, self.internal_funding_created(their_node_id, msg))
+       }
+
+       fn handle_funding_signed(&self, their_node_id: &PublicKey, msg: &msgs::FundingSigned) -> Result<(), HandleError> {
+               let _ = self.total_consistency_lock.read().unwrap();
+               handle_error!(self, self.internal_funding_signed(their_node_id, msg))
+       }
+
+       fn handle_funding_locked(&self, their_node_id: &PublicKey, msg: &msgs::FundingLocked) -> Result<(), HandleError> {
+               let _ = self.total_consistency_lock.read().unwrap();
+               handle_error!(self, self.internal_funding_locked(their_node_id, msg))
+       }
+
+       fn handle_shutdown(&self, their_node_id: &PublicKey, msg: &msgs::Shutdown) -> Result<(), HandleError> {
+               let _ = self.total_consistency_lock.read().unwrap();
+               handle_error!(self, self.internal_shutdown(their_node_id, msg))
+       }
+
+       fn handle_closing_signed(&self, their_node_id: &PublicKey, msg: &msgs::ClosingSigned) -> Result<(), HandleError> {
+               let _ = self.total_consistency_lock.read().unwrap();
+               handle_error!(self, self.internal_closing_signed(their_node_id, msg))
+       }
+
+       fn handle_update_add_htlc(&self, their_node_id: &PublicKey, msg: &msgs::UpdateAddHTLC) -> Result<(), msgs::HandleError> {
+               let _ = self.total_consistency_lock.read().unwrap();
+               handle_error!(self, self.internal_update_add_htlc(their_node_id, msg))
+       }
+
+       fn handle_update_fulfill_htlc(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFulfillHTLC) -> Result<(), HandleError> {
+               let _ = self.total_consistency_lock.read().unwrap();
+               handle_error!(self, self.internal_update_fulfill_htlc(their_node_id, msg))
+       }
+
+       fn handle_update_fail_htlc(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFailHTLC) -> Result<(), HandleError> {
+               let _ = self.total_consistency_lock.read().unwrap();
+               handle_error!(self, self.internal_update_fail_htlc(their_node_id, msg))
+       }
+
+       fn handle_update_fail_malformed_htlc(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFailMalformedHTLC) -> Result<(), HandleError> {
+               let _ = self.total_consistency_lock.read().unwrap();
+               handle_error!(self, self.internal_update_fail_malformed_htlc(their_node_id, msg))
+       }
+
+       fn handle_commitment_signed(&self, their_node_id: &PublicKey, msg: &msgs::CommitmentSigned) -> Result<(), HandleError> {
+               let _ = self.total_consistency_lock.read().unwrap();
+               handle_error!(self, self.internal_commitment_signed(their_node_id, msg))
+       }
+
+       fn handle_revoke_and_ack(&self, their_node_id: &PublicKey, msg: &msgs::RevokeAndACK) -> Result<(), HandleError> {
+               let _ = self.total_consistency_lock.read().unwrap();
+               handle_error!(self, self.internal_revoke_and_ack(their_node_id, msg))
+       }
+
+       fn handle_update_fee(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFee) -> Result<(), HandleError> {
+               let _ = self.total_consistency_lock.read().unwrap();
+               handle_error!(self, self.internal_update_fee(their_node_id, msg))
+       }
+
+       fn handle_announcement_signatures(&self, their_node_id: &PublicKey, msg: &msgs::AnnouncementSignatures) -> Result<(), HandleError> {
+               let _ = self.total_consistency_lock.read().unwrap();
+               handle_error!(self, self.internal_announcement_signatures(their_node_id, msg))
+       }
+
+       fn handle_channel_reestablish(&self, their_node_id: &PublicKey, msg: &msgs::ChannelReestablish) -> Result<(), HandleError> {
+               let _ = self.total_consistency_lock.read().unwrap();
+               handle_error!(self, self.internal_channel_reestablish(their_node_id, msg))
+       }
+
+       fn peer_disconnected(&self, their_node_id: &PublicKey, no_connection_possible: bool) {
+               let _ = self.total_consistency_lock.read().unwrap();
+               let mut failed_channels = Vec::new();
+               let mut failed_payments = Vec::new();
+               {
+                       let mut channel_state_lock = self.channel_state.lock().unwrap();
+                       let channel_state = channel_state_lock.borrow_parts();
+                       let short_to_id = channel_state.short_to_id;
+                       let pending_msg_events = channel_state.pending_msg_events;
+                       if no_connection_possible {
+                               log_debug!(self, "Failing all channels with {} due to no_connection_possible", log_pubkey!(their_node_id));
+                               channel_state.by_id.retain(|_, chan| {
+                                       if chan.get_their_node_id() == *their_node_id {
+                                               if let Some(short_id) = chan.get_short_channel_id() {
+                                                       short_to_id.remove(&short_id);
+                                               }
+                                               failed_channels.push(chan.force_shutdown());
+                                               if let Ok(update) = self.get_channel_update(&chan) {
+                                                       pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
+                                                               msg: update
+                                                       });
+                                               }
+                                               false
+                                       } else {
+                                               true
+                                       }
+                               });
+                       } else {
+                               log_debug!(self, "Marking channels with {} disconnected and generating channel_updates", log_pubkey!(their_node_id));
+                               channel_state.by_id.retain(|_, chan| {
+                                       if chan.get_their_node_id() == *their_node_id {
+                                               //TODO: mark channel disabled (and maybe announce such after a timeout).
+                                               let failed_adds = chan.remove_uncommitted_htlcs_and_mark_paused();
+                                               if !failed_adds.is_empty() {
+                                                       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
+                                                       failed_payments.push((chan_update, failed_adds));
+                                               }
+                                               if chan.is_shutdown() {
+                                                       if let Some(short_id) = chan.get_short_channel_id() {
+                                                               short_to_id.remove(&short_id);
+                                                       }
+                                                       return false;
+                                               }
+                                       }
+                                       true
+                               })
+                       }
+                       pending_msg_events.retain(|msg| {
+                               match msg {
+                                       &events::MessageSendEvent::SendAcceptChannel { ref node_id, .. } => node_id != their_node_id,
+                                       &events::MessageSendEvent::SendOpenChannel { ref node_id, .. } => node_id != their_node_id,
+                                       &events::MessageSendEvent::SendFundingCreated { ref node_id, .. } => node_id != their_node_id,
+                                       &events::MessageSendEvent::SendFundingSigned { ref node_id, .. } => node_id != their_node_id,
+                                       &events::MessageSendEvent::SendFundingLocked { ref node_id, .. } => node_id != their_node_id,
+                                       &events::MessageSendEvent::SendAnnouncementSignatures { ref node_id, .. } => node_id != their_node_id,
+                                       &events::MessageSendEvent::UpdateHTLCs { ref node_id, .. } => node_id != their_node_id,
+                                       &events::MessageSendEvent::SendRevokeAndACK { ref node_id, .. } => node_id != their_node_id,
+                                       &events::MessageSendEvent::SendClosingSigned { ref node_id, .. } => node_id != their_node_id,
+                                       &events::MessageSendEvent::SendShutdown { ref node_id, .. } => node_id != their_node_id,
+                                       &events::MessageSendEvent::SendChannelReestablish { ref node_id, .. } => node_id != their_node_id,
+                                       &events::MessageSendEvent::BroadcastChannelAnnouncement { .. } => true,
+                                       &events::MessageSendEvent::BroadcastChannelUpdate { .. } => true,
+                                       &events::MessageSendEvent::HandleError { ref node_id, .. } => node_id != their_node_id,
+                                       &events::MessageSendEvent::PaymentFailureNetworkUpdate { .. } => true,
+                               }
+                       });
+               }
+               for failure in failed_channels.drain(..) {
+                       self.finish_force_close_channel(failure);
+               }
+               for (chan_update, mut htlc_sources) in failed_payments {
+                       for (htlc_source, payment_hash) in htlc_sources.drain(..) {
+                               self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), htlc_source, &payment_hash, HTLCFailReason::Reason { failure_code: 0x1000 | 7, data: chan_update.clone() });
+                       }
+               }
+       }
+
+       fn peer_connected(&self, their_node_id: &PublicKey) {
+               log_debug!(self, "Generating channel_reestablish events for {}", log_pubkey!(their_node_id));
+
+               let _ = self.total_consistency_lock.read().unwrap();
+               let mut channel_state_lock = self.channel_state.lock().unwrap();
+               let channel_state = channel_state_lock.borrow_parts();
+               let pending_msg_events = channel_state.pending_msg_events;
+               channel_state.by_id.retain(|_, chan| {
+                       if chan.get_their_node_id() == *their_node_id {
+                               if !chan.have_received_message() {
+                                       // If we created this (outbound) channel while we were disconnected from the
+                                       // peer we probably failed to send the open_channel message, which is now
+                                       // lost. We can't have had anything pending related to this channel, so we just
+                                       // drop it.
+                                       false
+                               } else {
+                                       pending_msg_events.push(events::MessageSendEvent::SendChannelReestablish {
+                                               node_id: chan.get_their_node_id(),
+                                               msg: chan.get_channel_reestablish(),
+                                       });
+                                       true
+                               }
+                       } else { true }
+               });
+               //TODO: Also re-broadcast announcement_signatures
+       }
+
+       fn handle_error(&self, their_node_id: &PublicKey, msg: &msgs::ErrorMessage) {
+               let _ = self.total_consistency_lock.read().unwrap();
+
+               if msg.channel_id == [0; 32] {
+                       for chan in self.list_channels() {
+                               if chan.remote_network_id == *their_node_id {
+                                       self.force_close_channel(&chan.channel_id);
+                               }
+                       }
+               } else {
+                       self.force_close_channel(&msg.channel_id);
+               }
+       }
+}
+
+const SERIALIZATION_VERSION: u8 = 1;
+const MIN_SERIALIZATION_VERSION: u8 = 1;
+
+impl Writeable for PendingForwardHTLCInfo {
+       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
+               self.onion_packet.write(writer)?;
+               self.incoming_shared_secret.write(writer)?;
+               self.payment_hash.write(writer)?;
+               self.short_channel_id.write(writer)?;
+               self.amt_to_forward.write(writer)?;
+               self.outgoing_cltv_value.write(writer)?;
+               Ok(())
+       }
+}
+
+impl<R: ::std::io::Read> Readable<R> for PendingForwardHTLCInfo {
+       fn read(reader: &mut R) -> Result<PendingForwardHTLCInfo, DecodeError> {
+               Ok(PendingForwardHTLCInfo {
+                       onion_packet: Readable::read(reader)?,
+                       incoming_shared_secret: Readable::read(reader)?,
+                       payment_hash: Readable::read(reader)?,
+                       short_channel_id: Readable::read(reader)?,
+                       amt_to_forward: Readable::read(reader)?,
+                       outgoing_cltv_value: Readable::read(reader)?,
+               })
+       }
+}
+
+impl Writeable for HTLCFailureMsg {
+       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
+               match self {
+                       &HTLCFailureMsg::Relay(ref fail_msg) => {
+                               0u8.write(writer)?;
+                               fail_msg.write(writer)?;
+                       },
+                       &HTLCFailureMsg::Malformed(ref fail_msg) => {
+                               1u8.write(writer)?;
+                               fail_msg.write(writer)?;
+                       }
+               }
+               Ok(())
+       }
+}
+
+impl<R: ::std::io::Read> Readable<R> for HTLCFailureMsg {
+       fn read(reader: &mut R) -> Result<HTLCFailureMsg, DecodeError> {
+               match <u8 as Readable<R>>::read(reader)? {
+                       0 => Ok(HTLCFailureMsg::Relay(Readable::read(reader)?)),
+                       1 => Ok(HTLCFailureMsg::Malformed(Readable::read(reader)?)),
+                       _ => Err(DecodeError::InvalidValue),
+               }
+       }
+}
+
+impl Writeable for PendingHTLCStatus {
+       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
+               match self {
+                       &PendingHTLCStatus::Forward(ref forward_info) => {
+                               0u8.write(writer)?;
+                               forward_info.write(writer)?;
+                       },
+                       &PendingHTLCStatus::Fail(ref fail_msg) => {
+                               1u8.write(writer)?;
+                               fail_msg.write(writer)?;
+                       }
+               }
+               Ok(())
+       }
+}
+
+impl<R: ::std::io::Read> Readable<R> for PendingHTLCStatus {
+       fn read(reader: &mut R) -> Result<PendingHTLCStatus, DecodeError> {
+               match <u8 as Readable<R>>::read(reader)? {
+                       0 => Ok(PendingHTLCStatus::Forward(Readable::read(reader)?)),
+                       1 => Ok(PendingHTLCStatus::Fail(Readable::read(reader)?)),
+                       _ => Err(DecodeError::InvalidValue),
+               }
+       }
+}
+
+impl_writeable!(HTLCPreviousHopData, 0, {
+       short_channel_id,
+       htlc_id,
+       incoming_packet_shared_secret
+});
+
+impl Writeable for HTLCSource {
+       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
+               match self {
+                       &HTLCSource::PreviousHopData(ref hop_data) => {
+                               0u8.write(writer)?;
+                               hop_data.write(writer)?;
+                       },
+                       &HTLCSource::OutboundRoute { ref route, ref session_priv, ref first_hop_htlc_msat } => {
+                               1u8.write(writer)?;
+                               route.write(writer)?;
+                               session_priv.write(writer)?;
+                               first_hop_htlc_msat.write(writer)?;
+                       }
+               }
+               Ok(())
+       }
+}
+
+impl<R: ::std::io::Read> Readable<R> for HTLCSource {
+       fn read(reader: &mut R) -> Result<HTLCSource, DecodeError> {
+               match <u8 as Readable<R>>::read(reader)? {
+                       0 => Ok(HTLCSource::PreviousHopData(Readable::read(reader)?)),
+                       1 => Ok(HTLCSource::OutboundRoute {
+                               route: Readable::read(reader)?,
+                               session_priv: Readable::read(reader)?,
+                               first_hop_htlc_msat: Readable::read(reader)?,
+                       }),
+                       _ => Err(DecodeError::InvalidValue),
+               }
+       }
+}
+
+impl Writeable for HTLCFailReason {
+       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
+               match self {
+                       &HTLCFailReason::ErrorPacket { ref err } => {
+                               0u8.write(writer)?;
+                               err.write(writer)?;
+                       },
+                       &HTLCFailReason::Reason { ref failure_code, ref data } => {
+                               1u8.write(writer)?;
+                               failure_code.write(writer)?;
+                               data.write(writer)?;
+                       }
+               }
+               Ok(())
+       }
+}
+
+impl<R: ::std::io::Read> Readable<R> for HTLCFailReason {
+       fn read(reader: &mut R) -> Result<HTLCFailReason, DecodeError> {
+               match <u8 as Readable<R>>::read(reader)? {
+                       0 => Ok(HTLCFailReason::ErrorPacket { err: Readable::read(reader)? }),
+                       1 => Ok(HTLCFailReason::Reason {
+                               failure_code: Readable::read(reader)?,
+                               data: Readable::read(reader)?,
+                       }),
+                       _ => Err(DecodeError::InvalidValue),
+               }
+       }
+}
+
+impl Writeable for HTLCForwardInfo {
+       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
+               match self {
+                       &HTLCForwardInfo::AddHTLC { ref prev_short_channel_id, ref prev_htlc_id, ref forward_info } => {
+                               0u8.write(writer)?;
+                               prev_short_channel_id.write(writer)?;
+                               prev_htlc_id.write(writer)?;
+                               forward_info.write(writer)?;
+                       },
+                       &HTLCForwardInfo::FailHTLC { ref htlc_id, ref err_packet } => {
+                               1u8.write(writer)?;
+                               htlc_id.write(writer)?;
+                               err_packet.write(writer)?;
+                       },
+               }
+               Ok(())
+       }
+}
+
+impl<R: ::std::io::Read> Readable<R> for HTLCForwardInfo {
+       fn read(reader: &mut R) -> Result<HTLCForwardInfo, DecodeError> {
+               match <u8 as Readable<R>>::read(reader)? {
+                       0 => Ok(HTLCForwardInfo::AddHTLC {
+                               prev_short_channel_id: Readable::read(reader)?,
+                               prev_htlc_id: Readable::read(reader)?,
+                               forward_info: Readable::read(reader)?,
+                       }),
+                       1 => Ok(HTLCForwardInfo::FailHTLC {
+                               htlc_id: Readable::read(reader)?,
+                               err_packet: Readable::read(reader)?,
+                       }),
+                       _ => Err(DecodeError::InvalidValue),
+               }
+       }
+}
+
+impl Writeable for ChannelManager {
+       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
+               let _ = self.total_consistency_lock.write().unwrap();
+
+               writer.write_all(&[SERIALIZATION_VERSION; 1])?;
+               writer.write_all(&[MIN_SERIALIZATION_VERSION; 1])?;
+
+               self.genesis_hash.write(writer)?;
+               (self.latest_block_height.load(Ordering::Acquire) as u32).write(writer)?;
+               self.last_block_hash.lock().unwrap().write(writer)?;
+
+               let channel_state = self.channel_state.lock().unwrap();
+               let mut unfunded_channels = 0;
+               for (_, channel) in channel_state.by_id.iter() {
+                       if !channel.is_funding_initiated() {
+                               unfunded_channels += 1;
+                       }
+               }
+               ((channel_state.by_id.len() - unfunded_channels) as u64).write(writer)?;
+               for (_, channel) in channel_state.by_id.iter() {
+                       if channel.is_funding_initiated() {
+                               channel.write(writer)?;
+                       }
+               }
+
+               (channel_state.forward_htlcs.len() as u64).write(writer)?;
+               for (short_channel_id, pending_forwards) in channel_state.forward_htlcs.iter() {
+                       short_channel_id.write(writer)?;
+                       (pending_forwards.len() as u64).write(writer)?;
+                       for forward in pending_forwards {
+                               forward.write(writer)?;
+                       }
+               }
+
+               (channel_state.claimable_htlcs.len() as u64).write(writer)?;
+               for (payment_hash, previous_hops) in channel_state.claimable_htlcs.iter() {
+                       payment_hash.write(writer)?;
+                       (previous_hops.len() as u64).write(writer)?;
+                       for &(recvd_amt, ref previous_hop) in previous_hops.iter() {
+                               recvd_amt.write(writer)?;
+                               previous_hop.write(writer)?;
+                       }
+               }
+
+               Ok(())
+       }
+}
+
+/// Arguments for the creation of a ChannelManager that are not deserialized.
+///
+/// At a high-level, the process for deserializing a ChannelManager and resuming normal operation
+/// is:
+/// 1) Deserialize all stored ChannelMonitors.
+/// 2) Deserialize the ChannelManager by filling in this struct and calling <(Sha256dHash,
+///    ChannelManager)>::read(reader, args).
+///    This may result in closing some Channels if the ChannelMonitor is newer than the stored
+///    ChannelManager state to ensure no loss of funds. Thus, transactions may be broadcasted.
+/// 3) Register all relevant ChannelMonitor outpoints with your chain watch mechanism using
+///    ChannelMonitor::get_monitored_outpoints and ChannelMonitor::get_funding_txo().
+/// 4) Reconnect blocks on your ChannelMonitors.
+/// 5) Move the ChannelMonitors into your local ManyChannelMonitor.
+/// 6) Disconnect/connect blocks on the ChannelManager.
+/// 7) Register the new ChannelManager with your ChainWatchInterface (this does not happen
+///    automatically as it does in ChannelManager::new()).
+pub struct ChannelManagerReadArgs<'a> {
+       /// The keys provider which will give us relevant keys. Some keys will be loaded during
+       /// deserialization.
+       pub keys_manager: Arc<KeysInterface>,
+
+       /// The fee_estimator for use in the ChannelManager in the future.
+       ///
+       /// No calls to the FeeEstimator will be made during deserialization.
+       pub fee_estimator: Arc<FeeEstimator>,
+       /// The ManyChannelMonitor for use in the ChannelManager in the future.
+       ///
+       /// No calls to the ManyChannelMonitor will be made during deserialization. It is assumed that
+       /// you have deserialized ChannelMonitors separately and will add them to your
+       /// ManyChannelMonitor after deserializing this ChannelManager.
+       pub monitor: Arc<ManyChannelMonitor>,
+       /// The ChainWatchInterface for use in the ChannelManager in the future.
+       ///
+       /// No calls to the ChainWatchInterface will be made during deserialization.
+       pub chain_monitor: Arc<ChainWatchInterface>,
+       /// The BroadcasterInterface which will be used in the ChannelManager in the future and may be
+       /// used to broadcast the latest local commitment transactions of channels which must be
+       /// force-closed during deserialization.
+       pub tx_broadcaster: Arc<BroadcasterInterface>,
+       /// The Logger for use in the ChannelManager and which may be used to log information during
+       /// deserialization.
+       pub logger: Arc<Logger>,
+       /// Default settings used for new channels. Any existing channels will continue to use the
+       /// runtime settings which were stored when the ChannelManager was serialized.
+       pub default_config: UserConfig,
+
+       /// A map from channel funding outpoints to ChannelMonitors for those channels (ie
+       /// value.get_funding_txo() should be the key).
+       ///
+       /// If a monitor is inconsistent with the channel state during deserialization the channel will
+       /// be force-closed using the data in the ChannelMonitor and the channel will be dropped. This
+       /// is true for missing channels as well. If there is a monitor missing for which we find
+       /// channel data Err(DecodeError::InvalidValue) will be returned.
+       ///
+       /// In such cases the latest local transactions will be sent to the tx_broadcaster included in
+       /// this struct.
+       pub channel_monitors: &'a HashMap<OutPoint, &'a ChannelMonitor>,
+}
+
+impl<'a, R : ::std::io::Read> ReadableArgs<R, ChannelManagerReadArgs<'a>> for (Sha256dHash, ChannelManager) {
+       fn read(reader: &mut R, args: ChannelManagerReadArgs<'a>) -> Result<Self, DecodeError> {
+               let _ver: u8 = Readable::read(reader)?;
+               let min_ver: u8 = Readable::read(reader)?;
+               if min_ver > SERIALIZATION_VERSION {
+                       return Err(DecodeError::UnknownVersion);
+               }
+
+               let genesis_hash: Sha256dHash = Readable::read(reader)?;
+               let latest_block_height: u32 = Readable::read(reader)?;
+               let last_block_hash: Sha256dHash = Readable::read(reader)?;
+
+               let mut closed_channels = Vec::new();
+
+               let channel_count: u64 = Readable::read(reader)?;
+               let mut funding_txo_set = HashSet::with_capacity(cmp::min(channel_count as usize, 128));
+               let mut by_id = HashMap::with_capacity(cmp::min(channel_count as usize, 128));
+               let mut short_to_id = HashMap::with_capacity(cmp::min(channel_count as usize, 128));
+               for _ in 0..channel_count {
+                       let mut channel: Channel = ReadableArgs::read(reader, args.logger.clone())?;
+                       if channel.last_block_connected != last_block_hash {
+                               return Err(DecodeError::InvalidValue);
+                       }
+
+                       let funding_txo = channel.channel_monitor().get_funding_txo().ok_or(DecodeError::InvalidValue)?;
+                       funding_txo_set.insert(funding_txo.clone());
+                       if let Some(monitor) = args.channel_monitors.get(&funding_txo) {
+                               if channel.get_cur_local_commitment_transaction_number() != monitor.get_cur_local_commitment_number() ||
+                                               channel.get_revoked_remote_commitment_transaction_number() != monitor.get_min_seen_secret() ||
+                                               channel.get_cur_remote_commitment_transaction_number() != monitor.get_cur_remote_commitment_number() {
+                                       let mut force_close_res = channel.force_shutdown();
+                                       force_close_res.0 = monitor.get_latest_local_commitment_txn();
+                                       closed_channels.push(force_close_res);
+                               } else {
+                                       if let Some(short_channel_id) = channel.get_short_channel_id() {
+                                               short_to_id.insert(short_channel_id, channel.channel_id());
+                                       }
+                                       by_id.insert(channel.channel_id(), channel);
+                               }
+                       } else {
+                               return Err(DecodeError::InvalidValue);
+                       }
+               }
+
+               for (ref funding_txo, ref monitor) in args.channel_monitors.iter() {
+                       if !funding_txo_set.contains(funding_txo) {
+                               closed_channels.push((monitor.get_latest_local_commitment_txn(), Vec::new()));
+                       }
+               }
+
+               let forward_htlcs_count: u64 = Readable::read(reader)?;
+               let mut forward_htlcs = HashMap::with_capacity(cmp::min(forward_htlcs_count as usize, 128));
+               for _ in 0..forward_htlcs_count {
+                       let short_channel_id = Readable::read(reader)?;
+                       let pending_forwards_count: u64 = Readable::read(reader)?;
+                       let mut pending_forwards = Vec::with_capacity(cmp::min(pending_forwards_count as usize, 128));
+                       for _ in 0..pending_forwards_count {
+                               pending_forwards.push(Readable::read(reader)?);
+                       }
+                       forward_htlcs.insert(short_channel_id, pending_forwards);
+               }
+
+               let claimable_htlcs_count: u64 = Readable::read(reader)?;
+               let mut claimable_htlcs = HashMap::with_capacity(cmp::min(claimable_htlcs_count as usize, 128));
+               for _ in 0..claimable_htlcs_count {
+                       let payment_hash = Readable::read(reader)?;
+                       let previous_hops_len: u64 = Readable::read(reader)?;
+                       let mut previous_hops = Vec::with_capacity(cmp::min(previous_hops_len as usize, 2));
+                       for _ in 0..previous_hops_len {
+                               previous_hops.push((Readable::read(reader)?, Readable::read(reader)?));
+                       }
+                       claimable_htlcs.insert(payment_hash, previous_hops);
+               }
+
+               let channel_manager = ChannelManager {
+                       genesis_hash,
+                       fee_estimator: args.fee_estimator,
+                       monitor: args.monitor,
+                       chain_monitor: args.chain_monitor,
+                       tx_broadcaster: args.tx_broadcaster,
+
+                       latest_block_height: AtomicUsize::new(latest_block_height as usize),
+                       last_block_hash: Mutex::new(last_block_hash),
+                       secp_ctx: Secp256k1::new(),
+
+                       channel_state: Mutex::new(ChannelHolder {
+                               by_id,
+                               short_to_id,
+                               forward_htlcs,
+                               claimable_htlcs,
+                               pending_msg_events: Vec::new(),
+                       }),
+                       our_network_key: args.keys_manager.get_node_secret(),
+
+                       pending_events: Mutex::new(Vec::new()),
+                       total_consistency_lock: RwLock::new(()),
+                       keys_manager: args.keys_manager,
+                       logger: args.logger,
+                       default_configuration: args.default_config,
+               };
+
+               for close_res in closed_channels.drain(..) {
+                       channel_manager.finish_force_close_channel(close_res);
+                       //TODO: Broadcast channel update for closed channels, but only after we've made a
+                       //connection or two.
+               }
+
+               Ok((last_block_hash.clone(), channel_manager))
+       }
+}
diff --git a/lightning/src/ln/channelmonitor.rs b/lightning/src/ln/channelmonitor.rs
new file mode 100644 (file)
index 0000000..5bcd74f
--- /dev/null
@@ -0,0 +1,3415 @@
+//! The logic to monitor for on-chain transactions and create the relevant claim responses lives
+//! here.
+//!
+//! ChannelMonitor objects are generated by ChannelManager in response to relevant
+//! messages/actions, and MUST be persisted to disk (and, preferably, remotely) before progress can
+//! be made in responding to certain messages, see ManyChannelMonitor for more.
+//!
+//! Note that ChannelMonitors are an important part of the lightning trust model and a copy of the
+//! latest ChannelMonitor must always be actively monitoring for chain updates (and no out-of-date
+//! ChannelMonitors should do so). Thus, if you're building rust-lightning into an HSM or other
+//! security-domain-separated system design, you should consider having multiple paths for
+//! ChannelMonitors to get out of the HSM and onto monitoring devices.
+
+use bitcoin::blockdata::block::BlockHeader;
+use bitcoin::blockdata::transaction::{TxIn,TxOut,SigHashType,Transaction};
+use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
+use bitcoin::blockdata::script::{Script, Builder};
+use bitcoin::blockdata::opcodes;
+use bitcoin::consensus::encode::{self, Decodable, Encodable};
+use bitcoin::util::hash::BitcoinHash;
+use bitcoin::util::bip143;
+
+use bitcoin_hashes::Hash;
+use bitcoin_hashes::sha256::Hash as Sha256;
+use bitcoin_hashes::hash160::Hash as Hash160;
+use bitcoin_hashes::sha256d::Hash as Sha256dHash;
+
+use secp256k1::{Secp256k1,Signature};
+use secp256k1::key::{SecretKey,PublicKey};
+use secp256k1;
+
+use ln::msgs::DecodeError;
+use ln::chan_utils;
+use ln::chan_utils::HTLCOutputInCommitment;
+use ln::channelmanager::{HTLCSource, PaymentPreimage, PaymentHash};
+use ln::channel::{ACCEPTED_HTLC_SCRIPT_WEIGHT, OFFERED_HTLC_SCRIPT_WEIGHT};
+use chain::chaininterface::{ChainListener, ChainWatchInterface, BroadcasterInterface, FeeEstimator, ConfirmationTarget};
+use chain::transaction::OutPoint;
+use chain::keysinterface::SpendableOutputDescriptor;
+use util::logger::Logger;
+use util::ser::{ReadableArgs, Readable, Writer, Writeable, WriterWriteAdaptor, U48};
+use util::{byte_utils, events};
+
+use std::collections::{HashMap, hash_map};
+use std::sync::{Arc,Mutex};
+use std::{hash,cmp, mem};
+
+/// An error enum representing a failure to persist a channel monitor update.
+#[derive(Clone)]
+pub enum ChannelMonitorUpdateErr {
+       /// Used to indicate a temporary failure (eg connection to a watchtower or remote backup of
+       /// our state failed, but is expected to succeed at some point in the future).
+       ///
+       /// Such a failure will "freeze" a channel, preventing us from revoking old states or
+       /// submitting new commitment transactions to the remote party.
+       /// ChannelManager::test_restore_channel_monitor can be used to retry the update(s) and restore
+       /// the channel to an operational state.
+       ///
+       /// Note that continuing to operate when no copy of the updated ChannelMonitor could be
+       /// persisted is unsafe - if you failed to store the update on your own local disk you should
+       /// instead return PermanentFailure to force closure of the channel ASAP.
+       ///
+       /// Even when a channel has been "frozen" updates to the ChannelMonitor can continue to occur
+       /// (eg if an inbound HTLC which we forwarded was claimed upstream resulting in us attempting
+       /// to claim it on this channel) and those updates must be applied wherever they can be. At
+       /// least one such updated ChannelMonitor must be persisted otherwise PermanentFailure should
+       /// be returned to get things on-chain ASAP using only the in-memory copy. Obviously updates to
+       /// the channel which would invalidate previous ChannelMonitors are not made when a channel has
+       /// been "frozen".
+       ///
+       /// Note that even if updates made after TemporaryFailure succeed you must still call
+       /// test_restore_channel_monitor to ensure you have the latest monitor and re-enable normal
+       /// channel operation.
+       ///
+       /// For deployments where a copy of ChannelMonitors and other local state are backed up in a
+       /// remote location (with local copies persisted immediately), it is anticipated that all
+       /// updates will return TemporaryFailure until the remote copies could be updated.
+       TemporaryFailure,
+       /// Used to indicate no further channel monitor updates will be allowed (eg we've moved on to a
+       /// different watchtower and cannot update with all watchtowers that were previously informed
+       /// of this channel). This will force-close the channel in question.
+       ///
+       /// Should also be used to indicate a failure to update the local copy of the channel monitor.
+       PermanentFailure,
+}
+
+/// General Err type for ChannelMonitor actions. Generally, this implies that the data provided is
+/// inconsistent with the ChannelMonitor being called. eg for ChannelMonitor::insert_combine this
+/// means you tried to merge two monitors for different channels or for a channel which was
+/// restored from a backup and then generated new commitment updates.
+/// Contains a human-readable error message.
+#[derive(Debug)]
+pub struct MonitorUpdateError(pub &'static str);
+
+/// Simple structure send back by ManyChannelMonitor in case of HTLC detected onchain from a
+/// forward channel and from which info are needed to update HTLC in a backward channel.
+pub struct HTLCUpdate {
+       pub(super) payment_hash: PaymentHash,
+       pub(super) payment_preimage: Option<PaymentPreimage>,
+       pub(super) source: HTLCSource
+}
+
+/// Simple trait indicating ability to track a set of ChannelMonitors and multiplex events between
+/// them. Generally should be implemented by keeping a local SimpleManyChannelMonitor and passing
+/// events to it, while also taking any add_update_monitor events and passing them to some remote
+/// server(s).
+///
+/// Note that any updates to a channel's monitor *must* be applied to each instance of the
+/// channel's monitor everywhere (including remote watchtowers) *before* this function returns. If
+/// an update occurs and a remote watchtower is left with old state, it may broadcast transactions
+/// which we have revoked, allowing our counterparty to claim all funds in the channel!
+pub trait ManyChannelMonitor: Send + Sync {
+       /// Adds or updates a monitor for the given `funding_txo`.
+       ///
+       /// Implementor must also ensure that the funding_txo outpoint is registered with any relevant
+       /// ChainWatchInterfaces such that the provided monitor receives block_connected callbacks with
+       /// any spends of it.
+       fn add_update_monitor(&self, funding_txo: OutPoint, monitor: ChannelMonitor) -> Result<(), ChannelMonitorUpdateErr>;
+
+       /// Used by ChannelManager to get list of HTLC resolved onchain and which needed to be updated
+       /// with success or failure backward
+       fn fetch_pending_htlc_updated(&self) -> Vec<HTLCUpdate>;
+}
+
+/// A simple implementation of a ManyChannelMonitor and ChainListener. Can be used to create a
+/// watchtower or watch our own channels.
+///
+/// Note that you must provide your own key by which to refer to channels.
+///
+/// If you're accepting remote monitors (ie are implementing a watchtower), you must verify that
+/// users cannot overwrite a given channel by providing a duplicate key. ie you should probably
+/// index by a PublicKey which is required to sign any updates.
+///
+/// If you're using this for local monitoring of your own channels, you probably want to use
+/// `OutPoint` as the key, which will give you a ManyChannelMonitor implementation.
+pub struct SimpleManyChannelMonitor<Key> {
+       #[cfg(test)] // Used in ChannelManager tests to manipulate channels directly
+       pub monitors: Mutex<HashMap<Key, ChannelMonitor>>,
+       #[cfg(not(test))]
+       monitors: Mutex<HashMap<Key, ChannelMonitor>>,
+       chain_monitor: Arc<ChainWatchInterface>,
+       broadcaster: Arc<BroadcasterInterface>,
+       pending_events: Mutex<Vec<events::Event>>,
+       pending_htlc_updated: Mutex<HashMap<PaymentHash, Vec<(HTLCSource, Option<PaymentPreimage>)>>>,
+       logger: Arc<Logger>,
+       fee_estimator: Arc<FeeEstimator>
+}
+
+impl<Key : Send + cmp::Eq + hash::Hash> ChainListener for SimpleManyChannelMonitor<Key> {
+       fn block_connected(&self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], _indexes_of_txn_matched: &[u32]) {
+               let block_hash = header.bitcoin_hash();
+               let mut new_events: Vec<events::Event> = Vec::with_capacity(0);
+               let mut htlc_updated_infos = Vec::new();
+               {
+                       let mut monitors = self.monitors.lock().unwrap();
+                       for monitor in monitors.values_mut() {
+                               let (txn_outputs, spendable_outputs, mut htlc_updated) = monitor.block_connected(txn_matched, height, &block_hash, &*self.broadcaster, &*self.fee_estimator);
+                               if spendable_outputs.len() > 0 {
+                                       new_events.push(events::Event::SpendableOutputs {
+                                               outputs: spendable_outputs,
+                                       });
+                               }
+
+                               for (ref txid, ref outputs) in txn_outputs {
+                                       for (idx, output) in outputs.iter().enumerate() {
+                                               self.chain_monitor.install_watch_outpoint((txid.clone(), idx as u32), &output.script_pubkey);
+                                       }
+                               }
+                               htlc_updated_infos.append(&mut htlc_updated);
+                       }
+               }
+               {
+                       // ChannelManager will just need to fetch pending_htlc_updated and pass state backward
+                       let mut pending_htlc_updated = self.pending_htlc_updated.lock().unwrap();
+                       for htlc in htlc_updated_infos.drain(..) {
+                               match pending_htlc_updated.entry(htlc.2) {
+                                       hash_map::Entry::Occupied(mut e) => {
+                                               // In case of reorg we may have htlc outputs solved in a different way so
+                                               // we prefer to keep claims but don't store duplicate updates for a given
+                                               // (payment_hash, HTLCSource) pair.
+                                               let mut existing_claim = false;
+                                               e.get_mut().retain(|htlc_data| {
+                                                       if htlc.0 == htlc_data.0 {
+                                                               if htlc_data.1.is_some() {
+                                                                       existing_claim = true;
+                                                                       true
+                                                               } else { false }
+                                                       } else { true }
+                                               });
+                                               if !existing_claim {
+                                                       e.get_mut().push((htlc.0, htlc.1));
+                                               }
+                                       }
+                                       hash_map::Entry::Vacant(e) => {
+                                               e.insert(vec![(htlc.0, htlc.1)]);
+                                       }
+                               }
+                       }
+               }
+               let mut pending_events = self.pending_events.lock().unwrap();
+               pending_events.append(&mut new_events);
+       }
+
+       fn block_disconnected(&self, header: &BlockHeader, disconnected_height: u32) {
+               let block_hash = header.bitcoin_hash();
+               let mut monitors = self.monitors.lock().unwrap();
+               for monitor in monitors.values_mut() {
+                       monitor.block_disconnected(disconnected_height, &block_hash);
+               }
+       }
+}
+
+impl<Key : Send + cmp::Eq + hash::Hash + 'static> SimpleManyChannelMonitor<Key> {
+       /// Creates a new object which can be used to monitor several channels given the chain
+       /// interface with which to register to receive notifications.
+       pub fn new(chain_monitor: Arc<ChainWatchInterface>, broadcaster: Arc<BroadcasterInterface>, logger: Arc<Logger>, feeest: Arc<FeeEstimator>) -> Arc<SimpleManyChannelMonitor<Key>> {
+               let res = Arc::new(SimpleManyChannelMonitor {
+                       monitors: Mutex::new(HashMap::new()),
+                       chain_monitor,
+                       broadcaster,
+                       pending_events: Mutex::new(Vec::new()),
+                       pending_htlc_updated: Mutex::new(HashMap::new()),
+                       logger,
+                       fee_estimator: feeest,
+               });
+               let weak_res = Arc::downgrade(&res);
+               res.chain_monitor.register_listener(weak_res);
+               res
+       }
+
+       /// Adds or updates the monitor which monitors the channel referred to by the given key.
+       pub fn add_update_monitor_by_key(&self, key: Key, monitor: ChannelMonitor) -> Result<(), MonitorUpdateError> {
+               let mut monitors = self.monitors.lock().unwrap();
+               match monitors.get_mut(&key) {
+                       Some(orig_monitor) => {
+                               log_trace!(self, "Updating Channel Monitor for channel {}", log_funding_info!(monitor.key_storage));
+                               return orig_monitor.insert_combine(monitor);
+                       },
+                       None => {}
+               };
+               match monitor.key_storage {
+                       Storage::Local { ref funding_info, .. } => {
+                               match funding_info {
+                                       &None => {
+                                               return Err(MonitorUpdateError("Try to update a useless monitor without funding_txo !"));
+                                       },
+                                       &Some((ref outpoint, ref script)) => {
+                                               log_trace!(self, "Got new Channel Monitor for channel {}", log_bytes!(outpoint.to_channel_id()[..]));
+                                               self.chain_monitor.install_watch_tx(&outpoint.txid, script);
+                                               self.chain_monitor.install_watch_outpoint((outpoint.txid, outpoint.index as u32), script);
+                                       },
+                               }
+                       },
+                       Storage::Watchtower { .. } => {
+                               self.chain_monitor.watch_all_txn();
+                       }
+               }
+               monitors.insert(key, monitor);
+               Ok(())
+       }
+}
+
+impl ManyChannelMonitor for SimpleManyChannelMonitor<OutPoint> {
+       fn add_update_monitor(&self, funding_txo: OutPoint, monitor: ChannelMonitor) -> Result<(), ChannelMonitorUpdateErr> {
+               match self.add_update_monitor_by_key(funding_txo, monitor) {
+                       Ok(_) => Ok(()),
+                       Err(_) => Err(ChannelMonitorUpdateErr::PermanentFailure),
+               }
+       }
+
+       fn fetch_pending_htlc_updated(&self) -> Vec<HTLCUpdate> {
+               let mut updated = self.pending_htlc_updated.lock().unwrap();
+               let mut pending_htlcs_updated = Vec::with_capacity(updated.len());
+               for (k, v) in updated.drain() {
+                       for htlc_data in v {
+                               pending_htlcs_updated.push(HTLCUpdate {
+                                       payment_hash: k,
+                                       payment_preimage: htlc_data.1,
+                                       source: htlc_data.0,
+                               });
+                       }
+               }
+               pending_htlcs_updated
+       }
+}
+
+impl<Key : Send + cmp::Eq + hash::Hash> events::EventsProvider for SimpleManyChannelMonitor<Key> {
+       fn get_and_clear_pending_events(&self) -> Vec<events::Event> {
+               let mut pending_events = self.pending_events.lock().unwrap();
+               let mut ret = Vec::new();
+               mem::swap(&mut ret, &mut *pending_events);
+               ret
+       }
+}
+
+/// If an HTLC expires within this many blocks, don't try to claim it in a shared transaction,
+/// instead claiming it in its own individual transaction.
+const CLTV_SHARED_CLAIM_BUFFER: u32 = 12;
+/// If an HTLC expires within this many blocks, force-close the channel to broadcast the
+/// HTLC-Success transaction.
+/// In other words, this is an upper bound on how many blocks we think it can take us to get a
+/// transaction confirmed (and we use it in a few more, equivalent, places).
+pub(crate) const CLTV_CLAIM_BUFFER: u32 = 6;
+/// Number of blocks by which point we expect our counterparty to have seen new blocks on the
+/// network and done a full update_fail_htlc/commitment_signed dance (+ we've updated all our
+/// copies of ChannelMonitors, including watchtowers). We could enforce the contract by failing
+/// at CLTV expiration height but giving a grace period to our peer may be profitable for us if he
+/// can provide an over-late preimage. Nevertheless, grace period has to be accounted in our
+/// CLTV_EXPIRY_DELTA to be secure. Following this policy we may decrease the rate of channel failures
+/// due to expiration but increase the cost of funds being locked longuer in case of failure.
+/// This delay also cover a low-power peer being slow to process blocks and so being behind us on
+/// accurate block height.
+/// In case of onchain failure to be pass backward we may see the last block of ANTI_REORG_DELAY
+/// with at worst this delay, so we are not only using this value as a mercy for them but also
+/// us as a safeguard to delay with enough time.
+pub(crate) const LATENCY_GRACE_PERIOD_BLOCKS: u32 = 3;
+/// Number of blocks we wait on seeing a HTLC output being solved before we fail corresponding inbound
+/// HTLCs. This prevents us from failing backwards and then getting a reorg resulting in us losing money.
+/// We use also this delay to be sure we can remove our in-flight claim txn from bump candidates buffer.
+/// It may cause spurrious generation of bumped claim txn but that's allright given the outpoint is already
+/// solved by a previous claim tx. What we want to avoid is reorg evicting our claim tx and us not
+/// keeping bumping another claim tx to solve the outpoint.
+pub(crate) const ANTI_REORG_DELAY: u32 = 6;
+
+#[derive(Clone, PartialEq)]
+enum Storage {
+       Local {
+               revocation_base_key: SecretKey,
+               htlc_base_key: SecretKey,
+               delayed_payment_base_key: SecretKey,
+               payment_base_key: SecretKey,
+               shutdown_pubkey: PublicKey,
+               prev_latest_per_commitment_point: Option<PublicKey>,
+               latest_per_commitment_point: Option<PublicKey>,
+               funding_info: Option<(OutPoint, Script)>,
+               current_remote_commitment_txid: Option<Sha256dHash>,
+               prev_remote_commitment_txid: Option<Sha256dHash>,
+       },
+       Watchtower {
+               revocation_base_key: PublicKey,
+               htlc_base_key: PublicKey,
+       }
+}
+
+#[derive(Clone, PartialEq)]
+struct LocalSignedTx {
+       /// txid of the transaction in tx, just used to make comparison faster
+       txid: Sha256dHash,
+       tx: Transaction,
+       revocation_key: PublicKey,
+       a_htlc_key: PublicKey,
+       b_htlc_key: PublicKey,
+       delayed_payment_key: PublicKey,
+       feerate_per_kw: u64,
+       htlc_outputs: Vec<(HTLCOutputInCommitment, Option<(Signature, Signature)>, Option<HTLCSource>)>,
+}
+
+#[derive(PartialEq)]
+enum InputDescriptors {
+       RevokedOfferedHTLC,
+       RevokedReceivedHTLC,
+       OfferedHTLC,
+       ReceivedHTLC,
+       RevokedOutput, // either a revoked to_local output on commitment tx, a revoked HTLC-Timeout output or a revoked HTLC-Success output
+}
+
+/// When ChannelMonitor discovers an onchain outpoint being a step of a channel and that it needs
+/// to generate a tx to push channel state forward, we cache outpoint-solving tx material to build
+/// a new bumped one in case of lenghty confirmation delay
+#[derive(Clone, PartialEq)]
+enum TxMaterial {
+       Revoked {
+               script: Script,
+               pubkey: Option<PublicKey>,
+               key: SecretKey,
+               is_htlc: bool,
+               amount: u64,
+       },
+       RemoteHTLC {
+               script: Script,
+               key: SecretKey,
+               preimage: Option<PaymentPreimage>,
+               amount: u64,
+       },
+       LocalHTLC {
+               script: Script,
+               sigs: (Signature, Signature),
+               preimage: Option<PaymentPreimage>,
+               amount: u64,
+       }
+}
+
+/// Upon discovering of some classes of onchain tx by ChannelMonitor, we may have to take actions on it
+/// once they mature to enough confirmations (ANTI_REORG_DELAY)
+#[derive(Clone, PartialEq)]
+enum OnchainEvent {
+       /// Outpoint under claim process by our own tx, once this one get enough confirmations, we remove it from
+       /// bump-txn candidate buffer.
+       Claim {
+               outpoint: BitcoinOutPoint,
+       },
+       /// HTLC output getting solved by a timeout, at maturation we pass upstream payment source information to solve
+       /// inbound HTLC in backward channel. Note, in case of preimage, we pass info to upstream without delay as we can
+       /// only win from it, so it's never an OnchainEvent
+       HTLCUpdate {
+               htlc_update: (HTLCSource, PaymentHash),
+       },
+}
+
+const SERIALIZATION_VERSION: u8 = 1;
+const MIN_SERIALIZATION_VERSION: u8 = 1;
+
+/// A ChannelMonitor handles chain events (blocks connected and disconnected) and generates
+/// on-chain transactions to ensure no loss of funds occurs.
+///
+/// You MUST ensure that no ChannelMonitors for a given channel anywhere contain out-of-date
+/// information and are actively monitoring the chain.
+#[derive(Clone)]
+pub struct ChannelMonitor {
+       commitment_transaction_number_obscure_factor: u64,
+
+       key_storage: Storage,
+       their_htlc_base_key: Option<PublicKey>,
+       their_delayed_payment_base_key: Option<PublicKey>,
+       // first is the idx of the first of the two revocation points
+       their_cur_revocation_points: Option<(u64, PublicKey, Option<PublicKey>)>,
+
+       our_to_self_delay: u16,
+       their_to_self_delay: Option<u16>,
+
+       old_secrets: [([u8; 32], u64); 49],
+       remote_claimable_outpoints: HashMap<Sha256dHash, Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)>>,
+       /// We cannot identify HTLC-Success or HTLC-Timeout transactions by themselves on the chain.
+       /// Nor can we figure out their commitment numbers without the commitment transaction they are
+       /// spending. Thus, in order to claim them via revocation key, we track all the remote
+       /// commitment transactions which we find on-chain, mapping them to the commitment number which
+       /// can be used to derive the revocation key and claim the transactions.
+       remote_commitment_txn_on_chain: HashMap<Sha256dHash, (u64, Vec<Script>)>,
+       /// Cache used to make pruning of payment_preimages faster.
+       /// Maps payment_hash values to commitment numbers for remote transactions for non-revoked
+       /// remote transactions (ie should remain pretty small).
+       /// Serialized to disk but should generally not be sent to Watchtowers.
+       remote_hash_commitment_number: HashMap<PaymentHash, u64>,
+
+       // We store two local commitment transactions to avoid any race conditions where we may update
+       // some monitors (potentially on watchtowers) but then fail to update others, resulting in the
+       // various monitors for one channel being out of sync, and us broadcasting a local
+       // transaction for which we have deleted claim information on some watchtowers.
+       prev_local_signed_commitment_tx: Option<LocalSignedTx>,
+       current_local_signed_commitment_tx: Option<LocalSignedTx>,
+
+       // Used just for ChannelManager to make sure it has the latest channel data during
+       // deserialization
+       current_remote_commitment_number: u64,
+
+       payment_preimages: HashMap<PaymentHash, PaymentPreimage>,
+
+       destination_script: Script,
+       // Thanks to data loss protection, we may be able to claim our non-htlc funds
+       // back, this is the script we have to spend from but we need to
+       // scan every commitment transaction for that
+       to_remote_rescue: Option<(Script, SecretKey)>,
+
+       // Used to track outpoint in the process of being claimed by our transactions. We need to scan all transactions
+       // for inputs spending this. If height timer (u32) is expired and claim tx hasn't reached enough confirmations
+       // before, use TxMaterial to regenerate a new claim tx with a satoshis-per-1000-weight-units higher than last
+       // one (u64), if timelock expiration (u32) is near, decrease height timer, the in-between bumps delay.
+       // Last field cached (u32) is height of outpoint confirmation, which is needed to flush this tracker
+       // in case of reorgs, given block timer are scaled on timer expiration we can't deduce from it original height.
+       our_claim_txn_waiting_first_conf: HashMap<BitcoinOutPoint, (u32, TxMaterial, u64, u32, u32)>,
+
+       // Used to track onchain events, i.e transactions parts of channels confirmed on chain, on which
+       // we have to take actions once they reach enough confs. Key is a block height timer, i.e we enforce
+       // actions when we receive a block with given height. Actions depend on OnchainEvent type.
+       onchain_events_waiting_threshold_conf: HashMap<u32, Vec<OnchainEvent>>,
+
+       // We simply modify last_block_hash in Channel's block_connected so that serialization is
+       // consistent but hopefully the users' copy handles block_connected in a consistent way.
+       // (we do *not*, however, update them in insert_combine to ensure any local user copies keep
+       // their last_block_hash from its state and not based on updated copies that didn't run through
+       // the full block_connected).
+       pub(crate) last_block_hash: Sha256dHash,
+       secp_ctx: Secp256k1<secp256k1::All>, //TODO: dedup this a bit...
+       logger: Arc<Logger>,
+}
+
+macro_rules! subtract_high_prio_fee {
+       ($self: ident, $fee_estimator: expr, $value: expr, $predicted_weight: expr, $spent_txid: expr, $used_feerate: expr) => {
+               {
+                       $used_feerate = $fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::HighPriority);
+                       let mut fee = $used_feerate * ($predicted_weight as u64) / 1000;
+                       if $value <= fee {
+                               $used_feerate = $fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
+                               fee = $used_feerate * ($predicted_weight as u64) / 1000;
+                               if $value <= fee {
+                                       $used_feerate = $fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background);
+                                       fee = $used_feerate * ($predicted_weight as u64) / 1000;
+                                       if $value <= fee {
+                                               log_error!($self, "Failed to generate an on-chain punishment tx spending {} as even low priority fee ({} sat) was more than the entire claim balance ({} sat)",
+                                                       $spent_txid, fee, $value);
+                                               false
+                                       } else {
+                                               log_warn!($self, "Used low priority fee for on-chain punishment tx spending {} as high priority fee was more than the entire claim balance ({} sat)",
+                                                       $spent_txid, $value);
+                                               $value -= fee;
+                                               true
+                                       }
+                               } else {
+                                       log_warn!($self, "Used medium priority fee for on-chain punishment tx spending {} as high priority fee was more than the entire claim balance ({} sat)",
+                                               $spent_txid, $value);
+                                       $value -= fee;
+                                       true
+                               }
+                       } else {
+                               $value -= fee;
+                               true
+                       }
+               }
+       }
+}
+
+#[cfg(any(test, feature = "fuzztarget"))]
+/// Used only in testing and fuzztarget to check serialization roundtrips don't change the
+/// underlying object
+impl PartialEq for ChannelMonitor {
+       fn eq(&self, other: &Self) -> bool {
+               if self.commitment_transaction_number_obscure_factor != other.commitment_transaction_number_obscure_factor ||
+                       self.key_storage != other.key_storage ||
+                       self.their_htlc_base_key != other.their_htlc_base_key ||
+                       self.their_delayed_payment_base_key != other.their_delayed_payment_base_key ||
+                       self.their_cur_revocation_points != other.their_cur_revocation_points ||
+                       self.our_to_self_delay != other.our_to_self_delay ||
+                       self.their_to_self_delay != other.their_to_self_delay ||
+                       self.remote_claimable_outpoints != other.remote_claimable_outpoints ||
+                       self.remote_commitment_txn_on_chain != other.remote_commitment_txn_on_chain ||
+                       self.remote_hash_commitment_number != other.remote_hash_commitment_number ||
+                       self.prev_local_signed_commitment_tx != other.prev_local_signed_commitment_tx ||
+                       self.current_remote_commitment_number != other.current_remote_commitment_number ||
+                       self.current_local_signed_commitment_tx != other.current_local_signed_commitment_tx ||
+                       self.payment_preimages != other.payment_preimages ||
+                       self.destination_script != other.destination_script ||
+                       self.to_remote_rescue != other.to_remote_rescue ||
+                       self.our_claim_txn_waiting_first_conf != other.our_claim_txn_waiting_first_conf ||
+                       self.onchain_events_waiting_threshold_conf != other.onchain_events_waiting_threshold_conf
+               {
+                       false
+               } else {
+                       for (&(ref secret, ref idx), &(ref o_secret, ref o_idx)) in self.old_secrets.iter().zip(other.old_secrets.iter()) {
+                               if secret != o_secret || idx != o_idx {
+                                       return false
+                               }
+                       }
+                       true
+               }
+       }
+}
+
+impl ChannelMonitor {
+       pub(super) fn new(revocation_base_key: &SecretKey, delayed_payment_base_key: &SecretKey, htlc_base_key: &SecretKey, payment_base_key: &SecretKey, shutdown_pubkey: &PublicKey, our_to_self_delay: u16, destination_script: Script, logger: Arc<Logger>) -> ChannelMonitor {
+               ChannelMonitor {
+                       commitment_transaction_number_obscure_factor: 0,
+
+                       key_storage: Storage::Local {
+                               revocation_base_key: revocation_base_key.clone(),
+                               htlc_base_key: htlc_base_key.clone(),
+                               delayed_payment_base_key: delayed_payment_base_key.clone(),
+                               payment_base_key: payment_base_key.clone(),
+                               shutdown_pubkey: shutdown_pubkey.clone(),
+                               prev_latest_per_commitment_point: None,
+                               latest_per_commitment_point: None,
+                               funding_info: None,
+                               current_remote_commitment_txid: None,
+                               prev_remote_commitment_txid: None,
+                       },
+                       their_htlc_base_key: None,
+                       their_delayed_payment_base_key: None,
+                       their_cur_revocation_points: None,
+
+                       our_to_self_delay: our_to_self_delay,
+                       their_to_self_delay: None,
+
+                       old_secrets: [([0; 32], 1 << 48); 49],
+                       remote_claimable_outpoints: HashMap::new(),
+                       remote_commitment_txn_on_chain: HashMap::new(),
+                       remote_hash_commitment_number: HashMap::new(),
+
+                       prev_local_signed_commitment_tx: None,
+                       current_local_signed_commitment_tx: None,
+                       current_remote_commitment_number: 1 << 48,
+
+                       payment_preimages: HashMap::new(),
+                       destination_script: destination_script,
+                       to_remote_rescue: None,
+
+                       our_claim_txn_waiting_first_conf: HashMap::new(),
+
+                       onchain_events_waiting_threshold_conf: HashMap::new(),
+
+                       last_block_hash: Default::default(),
+                       secp_ctx: Secp256k1::new(),
+                       logger,
+               }
+       }
+
+       fn get_witnesses_weight(inputs: &[InputDescriptors]) -> usize {
+               let mut tx_weight = 2; // count segwit flags
+               for inp in inputs {
+                       // We use expected weight (and not actual) as signatures and time lock delays may vary
+                       tx_weight +=  match inp {
+                               // number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
+                               &InputDescriptors::RevokedOfferedHTLC => {
+                                       1 + 1 + 73 + 1 + 33 + 1 + 133
+                               },
+                               // number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
+                               &InputDescriptors::RevokedReceivedHTLC => {
+                                       1 + 1 + 73 + 1 + 33 + 1 + 139
+                               },
+                               // number_of_witness_elements + sig_length + remotehtlc_sig  + preimage_length + preimage + witness_script_length + witness_script
+                               &InputDescriptors::OfferedHTLC => {
+                                       1 + 1 + 73 + 1 + 32 + 1 + 133
+                               },
+                               // number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
+                               &InputDescriptors::ReceivedHTLC => {
+                                       1 + 1 + 73 + 1 + 1 + 1 + 139
+                               },
+                               // number_of_witness_elements + sig_length + revocation_sig + true_length + op_true + witness_script_length + witness_script
+                               &InputDescriptors::RevokedOutput => {
+                                       1 + 1 + 73 + 1 + 1 + 1 + 77
+                               },
+                       };
+               }
+               tx_weight
+       }
+
+       fn get_height_timer(current_height: u32, timelock_expiration: u32) -> u32 {
+               if timelock_expiration <= current_height || timelock_expiration - current_height <= 3 {
+                       return current_height + 1
+               } else if timelock_expiration - current_height <= 15 {
+                       return current_height + 3
+               }
+               current_height + 15
+       }
+
+       #[inline]
+       fn place_secret(idx: u64) -> u8 {
+               for i in 0..48 {
+                       if idx & (1 << i) == (1 << i) {
+                               return i
+                       }
+               }
+               48
+       }
+
+       #[inline]
+       fn derive_secret(secret: [u8; 32], bits: u8, idx: u64) -> [u8; 32] {
+               let mut res: [u8; 32] = secret;
+               for i in 0..bits {
+                       let bitpos = bits - 1 - i;
+                       if idx & (1 << bitpos) == (1 << bitpos) {
+                               res[(bitpos / 8) as usize] ^= 1 << (bitpos & 7);
+                               res = Sha256::hash(&res).into_inner();
+                       }
+               }
+               res
+       }
+
+       /// Inserts a revocation secret into this channel monitor. Prunes old preimages if neither
+       /// needed by local commitment transactions HTCLs nor by remote ones. Unless we haven't already seen remote
+       /// commitment transaction's secret, they are de facto pruned (we can use revocation key).
+       pub(super) fn provide_secret(&mut self, idx: u64, secret: [u8; 32]) -> Result<(), MonitorUpdateError> {
+               let pos = ChannelMonitor::place_secret(idx);
+               for i in 0..pos {
+                       let (old_secret, old_idx) = self.old_secrets[i as usize];
+                       if ChannelMonitor::derive_secret(secret, pos, old_idx) != old_secret {
+                               return Err(MonitorUpdateError("Previous secret did not match new one"));
+                       }
+               }
+               if self.get_min_seen_secret() <= idx {
+                       return Ok(());
+               }
+               self.old_secrets[pos as usize] = (secret, idx);
+
+               // Prune HTLCs from the previous remote commitment tx so we don't generate failure/fulfill
+               // events for now-revoked/fulfilled HTLCs.
+               // TODO: We should probably consider whether we're really getting the next secret here.
+               if let Storage::Local { ref mut prev_remote_commitment_txid, .. } = self.key_storage {
+                       if let Some(txid) = prev_remote_commitment_txid.take() {
+                               for &mut (_, ref mut source) in self.remote_claimable_outpoints.get_mut(&txid).unwrap() {
+                                       *source = None;
+                               }
+                       }
+               }
+
+               if !self.payment_preimages.is_empty() {
+                       let local_signed_commitment_tx = self.current_local_signed_commitment_tx.as_ref().expect("Channel needs at least an initial commitment tx !");
+                       let prev_local_signed_commitment_tx = self.prev_local_signed_commitment_tx.as_ref();
+                       let min_idx = self.get_min_seen_secret();
+                       let remote_hash_commitment_number = &mut self.remote_hash_commitment_number;
+
+                       self.payment_preimages.retain(|&k, _| {
+                               for &(ref htlc, _, _) in &local_signed_commitment_tx.htlc_outputs {
+                                       if k == htlc.payment_hash {
+                                               return true
+                                       }
+                               }
+                               if let Some(prev_local_commitment_tx) = prev_local_signed_commitment_tx {
+                                       for &(ref htlc, _, _) in prev_local_commitment_tx.htlc_outputs.iter() {
+                                               if k == htlc.payment_hash {
+                                                       return true
+                                               }
+                                       }
+                               }
+                               let contains = if let Some(cn) = remote_hash_commitment_number.get(&k) {
+                                       if *cn < min_idx {
+                                               return true
+                                       }
+                                       true
+                               } else { false };
+                               if contains {
+                                       remote_hash_commitment_number.remove(&k);
+                               }
+                               false
+                       });
+               }
+
+               Ok(())
+       }
+
+       /// Informs this monitor of the latest remote (ie non-broadcastable) commitment transaction.
+       /// The monitor watches for it to be broadcasted and then uses the HTLC information (and
+       /// possibly future revocation/preimage information) to claim outputs where possible.
+       /// We cache also the mapping hash:commitment number to lighten pruning of old preimages by watchtowers.
+       pub(super) fn provide_latest_remote_commitment_tx_info(&mut self, unsigned_commitment_tx: &Transaction, htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)>, commitment_number: u64, their_revocation_point: PublicKey) {
+               // TODO: Encrypt the htlc_outputs data with the single-hash of the commitment transaction
+               // so that a remote monitor doesn't learn anything unless there is a malicious close.
+               // (only maybe, sadly we cant do the same for local info, as we need to be aware of
+               // timeouts)
+               for &(ref htlc, _) in &htlc_outputs {
+                       self.remote_hash_commitment_number.insert(htlc.payment_hash, commitment_number);
+               }
+
+               let new_txid = unsigned_commitment_tx.txid();
+               log_trace!(self, "Tracking new remote commitment transaction with txid {} at commitment number {} with {} HTLC outputs", new_txid, commitment_number, htlc_outputs.len());
+               log_trace!(self, "New potential remote commitment transaction: {}", encode::serialize_hex(unsigned_commitment_tx));
+               if let Storage::Local { ref mut current_remote_commitment_txid, ref mut prev_remote_commitment_txid, .. } = self.key_storage {
+                       *prev_remote_commitment_txid = current_remote_commitment_txid.take();
+                       *current_remote_commitment_txid = Some(new_txid);
+               }
+               self.remote_claimable_outpoints.insert(new_txid, htlc_outputs);
+               self.current_remote_commitment_number = commitment_number;
+               //TODO: Merge this into the other per-remote-transaction output storage stuff
+               match self.their_cur_revocation_points {
+                       Some(old_points) => {
+                               if old_points.0 == commitment_number + 1 {
+                                       self.their_cur_revocation_points = Some((old_points.0, old_points.1, Some(their_revocation_point)));
+                               } else if old_points.0 == commitment_number + 2 {
+                                       if let Some(old_second_point) = old_points.2 {
+                                               self.their_cur_revocation_points = Some((old_points.0 - 1, old_second_point, Some(their_revocation_point)));
+                                       } else {
+                                               self.their_cur_revocation_points = Some((commitment_number, their_revocation_point, None));
+                                       }
+                               } else {
+                                       self.their_cur_revocation_points = Some((commitment_number, their_revocation_point, None));
+                               }
+                       },
+                       None => {
+                               self.their_cur_revocation_points = Some((commitment_number, their_revocation_point, None));
+                       }
+               }
+       }
+
+       pub(super) fn provide_rescue_remote_commitment_tx_info(&mut self, their_revocation_point: PublicKey) {
+               match self.key_storage {
+                       Storage::Local { ref payment_base_key, .. } => {
+                               if let Ok(payment_key) = chan_utils::derive_public_key(&self.secp_ctx, &their_revocation_point, &PublicKey::from_secret_key(&self.secp_ctx, &payment_base_key)) {
+                                       let to_remote_script =  Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0)
+                                               .push_slice(&Hash160::hash(&payment_key.serialize())[..])
+                                               .into_script();
+                                       if let Ok(to_remote_key) = chan_utils::derive_private_key(&self.secp_ctx, &their_revocation_point, &payment_base_key) {
+                                               self.to_remote_rescue = Some((to_remote_script, to_remote_key));
+                                       }
+                               }
+                       },
+                       Storage::Watchtower { .. } => {}
+               }
+       }
+
+       /// Informs this monitor of the latest local (ie broadcastable) commitment transaction. The
+       /// monitor watches for timeouts and may broadcast it if we approach such a timeout. Thus, it
+       /// is important that any clones of this channel monitor (including remote clones) by kept
+       /// up-to-date as our local commitment transaction is updated.
+       /// Panics if set_their_to_self_delay has never been called.
+       /// Also update Storage with latest local per_commitment_point to derive local_delayedkey in
+       /// case of onchain HTLC tx
+       pub(super) fn provide_latest_local_commitment_tx_info(&mut self, signed_commitment_tx: Transaction, local_keys: chan_utils::TxCreationKeys, feerate_per_kw: u64, htlc_outputs: Vec<(HTLCOutputInCommitment, Option<(Signature, Signature)>, Option<HTLCSource>)>) {
+               assert!(self.their_to_self_delay.is_some());
+               self.prev_local_signed_commitment_tx = self.current_local_signed_commitment_tx.take();
+               self.current_local_signed_commitment_tx = Some(LocalSignedTx {
+                       txid: signed_commitment_tx.txid(),
+                       tx: signed_commitment_tx,
+                       revocation_key: local_keys.revocation_key,
+                       a_htlc_key: local_keys.a_htlc_key,
+                       b_htlc_key: local_keys.b_htlc_key,
+                       delayed_payment_key: local_keys.a_delayed_payment_key,
+                       feerate_per_kw,
+                       htlc_outputs,
+               });
+
+               if let Storage::Local { ref mut latest_per_commitment_point, .. } = self.key_storage {
+                       *latest_per_commitment_point = Some(local_keys.per_commitment_point);
+               } else {
+                       panic!("Channel somehow ended up with its internal ChannelMonitor being in Watchtower mode?");
+               }
+       }
+
+       /// Provides a payment_hash->payment_preimage mapping. Will be automatically pruned when all
+       /// commitment_tx_infos which contain the payment hash have been revoked.
+       pub(super) fn provide_payment_preimage(&mut self, payment_hash: &PaymentHash, payment_preimage: &PaymentPreimage) {
+               self.payment_preimages.insert(payment_hash.clone(), payment_preimage.clone());
+       }
+
+       /// Combines this ChannelMonitor with the information contained in the other ChannelMonitor.
+       /// After a successful call this ChannelMonitor is up-to-date and is safe to use to monitor the
+       /// chain for new blocks/transactions.
+       pub fn insert_combine(&mut self, mut other: ChannelMonitor) -> Result<(), MonitorUpdateError> {
+               match self.key_storage {
+                       Storage::Local { ref funding_info, .. } => {
+                               if funding_info.is_none() { return Err(MonitorUpdateError("Try to combine a Local monitor without funding_info")); }
+                               let our_funding_info = funding_info;
+                               if let Storage::Local { ref funding_info, .. } = other.key_storage {
+                                       if funding_info.is_none() { return Err(MonitorUpdateError("Try to combine a Local monitor without funding_info")); }
+                                       // We should be able to compare the entire funding_txo, but in fuzztarget it's trivially
+                                       // easy to collide the funding_txo hash and have a different scriptPubKey.
+                                       if funding_info.as_ref().unwrap().0 != our_funding_info.as_ref().unwrap().0 {
+                                               return Err(MonitorUpdateError("Funding transaction outputs are not identical!"));
+                                       }
+                               } else {
+                                       return Err(MonitorUpdateError("Try to combine a Local monitor with a Watchtower one !"));
+                               }
+                       },
+                       Storage::Watchtower { .. } => {
+                               if let Storage::Watchtower { .. } = other.key_storage {
+                                       unimplemented!();
+                               } else {
+                                       return Err(MonitorUpdateError("Try to combine a Watchtower monitor with a Local one !"));
+                               }
+                       },
+               }
+               let other_min_secret = other.get_min_seen_secret();
+               let our_min_secret = self.get_min_seen_secret();
+               if our_min_secret > other_min_secret {
+                       self.provide_secret(other_min_secret, other.get_secret(other_min_secret).unwrap())?;
+               }
+               if let Some(ref local_tx) = self.current_local_signed_commitment_tx {
+                       if let Some(ref other_local_tx) = other.current_local_signed_commitment_tx {
+                               let our_commitment_number = 0xffffffffffff - ((((local_tx.tx.input[0].sequence as u64 & 0xffffff) << 3*8) | (local_tx.tx.lock_time as u64 & 0xffffff)) ^ self.commitment_transaction_number_obscure_factor);
+                               let other_commitment_number = 0xffffffffffff - ((((other_local_tx.tx.input[0].sequence as u64 & 0xffffff) << 3*8) | (other_local_tx.tx.lock_time as u64 & 0xffffff)) ^ other.commitment_transaction_number_obscure_factor);
+                               if our_commitment_number >= other_commitment_number {
+                                       self.key_storage = other.key_storage;
+                               }
+                       }
+               }
+               // TODO: We should use current_remote_commitment_number and the commitment number out of
+               // local transactions to decide how to merge
+               if our_min_secret >= other_min_secret {
+                       self.their_cur_revocation_points = other.their_cur_revocation_points;
+                       for (txid, htlcs) in other.remote_claimable_outpoints.drain() {
+                               self.remote_claimable_outpoints.insert(txid, htlcs);
+                       }
+                       if let Some(local_tx) = other.prev_local_signed_commitment_tx {
+                               self.prev_local_signed_commitment_tx = Some(local_tx);
+                       }
+                       if let Some(local_tx) = other.current_local_signed_commitment_tx {
+                               self.current_local_signed_commitment_tx = Some(local_tx);
+                       }
+                       self.payment_preimages = other.payment_preimages;
+                       self.to_remote_rescue = other.to_remote_rescue;
+               }
+
+               self.current_remote_commitment_number = cmp::min(self.current_remote_commitment_number, other.current_remote_commitment_number);
+               Ok(())
+       }
+
+       /// Panics if commitment_transaction_number_obscure_factor doesn't fit in 48 bits
+       pub(super) fn set_commitment_obscure_factor(&mut self, commitment_transaction_number_obscure_factor: u64) {
+               assert!(commitment_transaction_number_obscure_factor < (1 << 48));
+               self.commitment_transaction_number_obscure_factor = commitment_transaction_number_obscure_factor;
+       }
+
+       /// Allows this monitor to scan only for transactions which are applicable. Note that this is
+       /// optional, without it this monitor cannot be used in an SPV client, but you may wish to
+       /// avoid this (or call unset_funding_info) on a monitor you wish to send to a watchtower as it
+       /// provides slightly better privacy.
+       /// It's the responsibility of the caller to register outpoint and script with passing the former
+       /// value as key to add_update_monitor.
+       pub(super) fn set_funding_info(&mut self, new_funding_info: (OutPoint, Script)) {
+               match self.key_storage {
+                       Storage::Local { ref mut funding_info, .. } => {
+                               *funding_info = Some(new_funding_info);
+                       },
+                       Storage::Watchtower { .. } => {
+                               panic!("Channel somehow ended up with its internal ChannelMonitor being in Watchtower mode?");
+                       }
+               }
+       }
+
+       /// We log these base keys at channel opening to being able to rebuild redeemscript in case of leaked revoked commit tx
+       pub(super) fn set_their_base_keys(&mut self, their_htlc_base_key: &PublicKey, their_delayed_payment_base_key: &PublicKey) {
+               self.their_htlc_base_key = Some(their_htlc_base_key.clone());
+               self.their_delayed_payment_base_key = Some(their_delayed_payment_base_key.clone());
+       }
+
+       pub(super) fn set_their_to_self_delay(&mut self, their_to_self_delay: u16) {
+               self.their_to_self_delay = Some(their_to_self_delay);
+       }
+
+       pub(super) fn unset_funding_info(&mut self) {
+               match self.key_storage {
+                       Storage::Local { ref mut funding_info, .. } => {
+                               *funding_info = None;
+                       },
+                       Storage::Watchtower { .. } => {
+                               panic!("Channel somehow ended up with its internal ChannelMonitor being in Watchtower mode?");
+                       },
+               }
+       }
+
+       /// Gets the funding transaction outpoint of the channel this ChannelMonitor is monitoring for.
+       pub fn get_funding_txo(&self) -> Option<OutPoint> {
+               match self.key_storage {
+                       Storage::Local { ref funding_info, .. } => {
+                               match funding_info {
+                                       &Some((outpoint, _)) => Some(outpoint),
+                                       &None => None
+                               }
+                       },
+                       Storage::Watchtower { .. } => {
+                               return None;
+                       }
+               }
+       }
+
+       /// Gets the sets of all outpoints which this ChannelMonitor expects to hear about spends of.
+       /// Generally useful when deserializing as during normal operation the return values of
+       /// block_connected are sufficient to ensure all relevant outpoints are being monitored (note
+       /// that the get_funding_txo outpoint and transaction must also be monitored for!).
+       pub fn get_monitored_outpoints(&self) -> Vec<(Sha256dHash, u32, &Script)> {
+               let mut res = Vec::with_capacity(self.remote_commitment_txn_on_chain.len() * 2);
+               for (ref txid, &(_, ref outputs)) in self.remote_commitment_txn_on_chain.iter() {
+                       for (idx, output) in outputs.iter().enumerate() {
+                               res.push(((*txid).clone(), idx as u32, output));
+                       }
+               }
+               res
+       }
+
+       /// Serializes into a vec, with various modes for the exposed pub fns
+       fn write<W: Writer>(&self, writer: &mut W, for_local_storage: bool) -> Result<(), ::std::io::Error> {
+               //TODO: We still write out all the serialization here manually instead of using the fancy
+               //serialization framework we have, we should migrate things over to it.
+               writer.write_all(&[SERIALIZATION_VERSION; 1])?;
+               writer.write_all(&[MIN_SERIALIZATION_VERSION; 1])?;
+
+               // Set in initial Channel-object creation, so should always be set by now:
+               U48(self.commitment_transaction_number_obscure_factor).write(writer)?;
+
+               macro_rules! write_option {
+                       ($thing: expr) => {
+                               match $thing {
+                                       &Some(ref t) => {
+                                               1u8.write(writer)?;
+                                               t.write(writer)?;
+                                       },
+                                       &None => 0u8.write(writer)?,
+                               }
+                       }
+               }
+
+               match self.key_storage {
+                       Storage::Local { ref revocation_base_key, ref htlc_base_key, ref delayed_payment_base_key, ref payment_base_key, ref shutdown_pubkey, ref prev_latest_per_commitment_point, ref latest_per_commitment_point, ref funding_info, ref current_remote_commitment_txid, ref prev_remote_commitment_txid } => {
+                               writer.write_all(&[0; 1])?;
+                               writer.write_all(&revocation_base_key[..])?;
+                               writer.write_all(&htlc_base_key[..])?;
+                               writer.write_all(&delayed_payment_base_key[..])?;
+                               writer.write_all(&payment_base_key[..])?;
+                               writer.write_all(&shutdown_pubkey.serialize())?;
+                               prev_latest_per_commitment_point.write(writer)?;
+                               latest_per_commitment_point.write(writer)?;
+                               match funding_info  {
+                                       &Some((ref outpoint, ref script)) => {
+                                               writer.write_all(&outpoint.txid[..])?;
+                                               writer.write_all(&byte_utils::be16_to_array(outpoint.index))?;
+                                               script.write(writer)?;
+                                       },
+                                       &None => {
+                                               debug_assert!(false, "Try to serialize a useless Local monitor !");
+                                       },
+                               }
+                               current_remote_commitment_txid.write(writer)?;
+                               prev_remote_commitment_txid.write(writer)?;
+                       },
+                       Storage::Watchtower { .. } => unimplemented!(),
+               }
+
+               writer.write_all(&self.their_htlc_base_key.as_ref().unwrap().serialize())?;
+               writer.write_all(&self.their_delayed_payment_base_key.as_ref().unwrap().serialize())?;
+
+               match self.their_cur_revocation_points {
+                       Some((idx, pubkey, second_option)) => {
+                               writer.write_all(&byte_utils::be48_to_array(idx))?;
+                               writer.write_all(&pubkey.serialize())?;
+                               match second_option {
+                                       Some(second_pubkey) => {
+                                               writer.write_all(&second_pubkey.serialize())?;
+                                       },
+                                       None => {
+                                               writer.write_all(&[0; 33])?;
+                                       },
+                               }
+                       },
+                       None => {
+                               writer.write_all(&byte_utils::be48_to_array(0))?;
+                       },
+               }
+
+               writer.write_all(&byte_utils::be16_to_array(self.our_to_self_delay))?;
+               writer.write_all(&byte_utils::be16_to_array(self.their_to_self_delay.unwrap()))?;
+
+               for &(ref secret, ref idx) in self.old_secrets.iter() {
+                       writer.write_all(secret)?;
+                       writer.write_all(&byte_utils::be64_to_array(*idx))?;
+               }
+
+               macro_rules! serialize_htlc_in_commitment {
+                       ($htlc_output: expr) => {
+                               writer.write_all(&[$htlc_output.offered as u8; 1])?;
+                               writer.write_all(&byte_utils::be64_to_array($htlc_output.amount_msat))?;
+                               writer.write_all(&byte_utils::be32_to_array($htlc_output.cltv_expiry))?;
+                               writer.write_all(&$htlc_output.payment_hash.0[..])?;
+                               $htlc_output.transaction_output_index.write(writer)?;
+                       }
+               }
+
+               writer.write_all(&byte_utils::be64_to_array(self.remote_claimable_outpoints.len() as u64))?;
+               for (ref txid, ref htlc_infos) in self.remote_claimable_outpoints.iter() {
+                       writer.write_all(&txid[..])?;
+                       writer.write_all(&byte_utils::be64_to_array(htlc_infos.len() as u64))?;
+                       for &(ref htlc_output, ref htlc_source) in htlc_infos.iter() {
+                               serialize_htlc_in_commitment!(htlc_output);
+                               write_option!(htlc_source);
+                       }
+               }
+
+               writer.write_all(&byte_utils::be64_to_array(self.remote_commitment_txn_on_chain.len() as u64))?;
+               for (ref txid, &(commitment_number, ref txouts)) in self.remote_commitment_txn_on_chain.iter() {
+                       writer.write_all(&txid[..])?;
+                       writer.write_all(&byte_utils::be48_to_array(commitment_number))?;
+                       (txouts.len() as u64).write(writer)?;
+                       for script in txouts.iter() {
+                               script.write(writer)?;
+                       }
+               }
+
+               if for_local_storage {
+                       writer.write_all(&byte_utils::be64_to_array(self.remote_hash_commitment_number.len() as u64))?;
+                       for (ref payment_hash, commitment_number) in self.remote_hash_commitment_number.iter() {
+                               writer.write_all(&payment_hash.0[..])?;
+                               writer.write_all(&byte_utils::be48_to_array(*commitment_number))?;
+                       }
+               } else {
+                       writer.write_all(&byte_utils::be64_to_array(0))?;
+               }
+
+               macro_rules! serialize_local_tx {
+                       ($local_tx: expr) => {
+                               if let Err(e) = $local_tx.tx.consensus_encode(&mut WriterWriteAdaptor(writer)) {
+                                       match e {
+                                               encode::Error::Io(e) => return Err(e),
+                                               _ => panic!("local tx must have been well-formed!"),
+                                       }
+                               }
+
+                               writer.write_all(&$local_tx.revocation_key.serialize())?;
+                               writer.write_all(&$local_tx.a_htlc_key.serialize())?;
+                               writer.write_all(&$local_tx.b_htlc_key.serialize())?;
+                               writer.write_all(&$local_tx.delayed_payment_key.serialize())?;
+
+                               writer.write_all(&byte_utils::be64_to_array($local_tx.feerate_per_kw))?;
+                               writer.write_all(&byte_utils::be64_to_array($local_tx.htlc_outputs.len() as u64))?;
+                               for &(ref htlc_output, ref sigs, ref htlc_source) in $local_tx.htlc_outputs.iter() {
+                                       serialize_htlc_in_commitment!(htlc_output);
+                                       if let &Some((ref their_sig, ref our_sig)) = sigs {
+                                               1u8.write(writer)?;
+                                               writer.write_all(&their_sig.serialize_compact())?;
+                                               writer.write_all(&our_sig.serialize_compact())?;
+                                       } else {
+                                               0u8.write(writer)?;
+                                       }
+                                       write_option!(htlc_source);
+                               }
+                       }
+               }
+
+               if let Some(ref prev_local_tx) = self.prev_local_signed_commitment_tx {
+                       writer.write_all(&[1; 1])?;
+                       serialize_local_tx!(prev_local_tx);
+               } else {
+                       writer.write_all(&[0; 1])?;
+               }
+
+               if let Some(ref cur_local_tx) = self.current_local_signed_commitment_tx {
+                       writer.write_all(&[1; 1])?;
+                       serialize_local_tx!(cur_local_tx);
+               } else {
+                       writer.write_all(&[0; 1])?;
+               }
+
+               if for_local_storage {
+                       writer.write_all(&byte_utils::be48_to_array(self.current_remote_commitment_number))?;
+               } else {
+                       writer.write_all(&byte_utils::be48_to_array(0))?;
+               }
+
+               writer.write_all(&byte_utils::be64_to_array(self.payment_preimages.len() as u64))?;
+               for payment_preimage in self.payment_preimages.values() {
+                       writer.write_all(&payment_preimage.0[..])?;
+               }
+
+               self.last_block_hash.write(writer)?;
+               self.destination_script.write(writer)?;
+               if let Some((ref to_remote_script, ref local_key)) = self.to_remote_rescue {
+                       writer.write_all(&[1; 1])?;
+                       to_remote_script.write(writer)?;
+                       local_key.write(writer)?;
+               } else {
+                       writer.write_all(&[0; 1])?;
+               }
+
+               writer.write_all(&byte_utils::be64_to_array(self.our_claim_txn_waiting_first_conf.len() as u64))?;
+               for (ref outpoint, claim_tx_data) in self.our_claim_txn_waiting_first_conf.iter() {
+                       outpoint.write(writer)?;
+                       writer.write_all(&byte_utils::be32_to_array(claim_tx_data.0))?;
+                       match claim_tx_data.1 {
+                               TxMaterial::Revoked { ref script, ref pubkey, ref key, ref is_htlc, ref amount} => {
+                                       writer.write_all(&[0; 1])?;
+                                       script.write(writer)?;
+                                       pubkey.write(writer)?;
+                                       writer.write_all(&key[..])?;
+                                       if *is_htlc {
+                                               writer.write_all(&[0; 1])?;
+                                       } else {
+                                               writer.write_all(&[1; 1])?;
+                                       }
+                                       writer.write_all(&byte_utils::be64_to_array(*amount))?;
+                               },
+                               TxMaterial::RemoteHTLC { ref script, ref key, ref preimage, ref amount } => {
+                                       writer.write_all(&[1; 1])?;
+                                       script.write(writer)?;
+                                       key.write(writer)?;
+                                       preimage.write(writer)?;
+                                       writer.write_all(&byte_utils::be64_to_array(*amount))?;
+                               },
+                               TxMaterial::LocalHTLC { ref script, ref sigs, ref preimage, ref amount } => {
+                                       writer.write_all(&[2; 1])?;
+                                       script.write(writer)?;
+                                       sigs.0.write(writer)?;
+                                       sigs.1.write(writer)?;
+                                       preimage.write(writer)?;
+                                       writer.write_all(&byte_utils::be64_to_array(*amount))?;
+                               }
+                       }
+                       writer.write_all(&byte_utils::be64_to_array(claim_tx_data.2))?;
+                       writer.write_all(&byte_utils::be32_to_array(claim_tx_data.3))?;
+                       writer.write_all(&byte_utils::be32_to_array(claim_tx_data.4))?;
+               }
+
+               writer.write_all(&byte_utils::be64_to_array(self.onchain_events_waiting_threshold_conf.len() as u64))?;
+               for (ref target, ref events) in self.onchain_events_waiting_threshold_conf.iter() {
+                       writer.write_all(&byte_utils::be32_to_array(**target))?;
+                       writer.write_all(&byte_utils::be64_to_array(events.len() as u64))?;
+                       for ev in events.iter() {
+                               match *ev {
+                                       OnchainEvent::Claim { ref outpoint } => {
+                                               writer.write_all(&[0; 1])?;
+                                               outpoint.write(writer)?;
+                                       },
+                                       OnchainEvent::HTLCUpdate { ref htlc_update } => {
+                                               writer.write_all(&[1; 1])?;
+                                               htlc_update.0.write(writer)?;
+                                               htlc_update.1.write(writer)?;
+                                       }
+                               }
+                       }
+               }
+
+               Ok(())
+       }
+
+       /// Writes this monitor into the given writer, suitable for writing to disk.
+       ///
+       /// Note that the deserializer is only implemented for (Sha256dHash, ChannelMonitor), which
+       /// tells you the last block hash which was block_connect()ed. You MUST rescan any blocks along
+       /// the "reorg path" (ie not just starting at the same height but starting at the highest
+       /// common block that appears on your best chain as well as on the chain which contains the
+       /// last block hash returned) upon deserializing the object!
+       pub fn write_for_disk<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
+               self.write(writer, true)
+       }
+
+       /// Encodes this monitor into the given writer, suitable for sending to a remote watchtower
+       ///
+       /// Note that the deserializer is only implemented for (Sha256dHash, ChannelMonitor), which
+       /// tells you the last block hash which was block_connect()ed. You MUST rescan any blocks along
+       /// the "reorg path" (ie not just starting at the same height but starting at the highest
+       /// common block that appears on your best chain as well as on the chain which contains the
+       /// last block hash returned) upon deserializing the object!
+       pub fn write_for_watchtower<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
+               self.write(writer, false)
+       }
+
+       /// Can only fail if idx is < get_min_seen_secret
+       pub(super) fn get_secret(&self, idx: u64) -> Option<[u8; 32]> {
+               for i in 0..self.old_secrets.len() {
+                       if (idx & (!((1 << i) - 1))) == self.old_secrets[i].1 {
+                               return Some(ChannelMonitor::derive_secret(self.old_secrets[i].0, i as u8, idx))
+                       }
+               }
+               assert!(idx < self.get_min_seen_secret());
+               None
+       }
+
+       pub(super) fn get_min_seen_secret(&self) -> u64 {
+               //TODO This can be optimized?
+               let mut min = 1 << 48;
+               for &(_, idx) in self.old_secrets.iter() {
+                       if idx < min {
+                               min = idx;
+                       }
+               }
+               min
+       }
+
+       pub(super) fn get_cur_remote_commitment_number(&self) -> u64 {
+               self.current_remote_commitment_number
+       }
+
+       pub(super) fn get_cur_local_commitment_number(&self) -> u64 {
+               if let &Some(ref local_tx) = &self.current_local_signed_commitment_tx {
+                       0xffff_ffff_ffff - ((((local_tx.tx.input[0].sequence as u64 & 0xffffff) << 3*8) | (local_tx.tx.lock_time as u64 & 0xffffff)) ^ self.commitment_transaction_number_obscure_factor)
+               } else { 0xffff_ffff_ffff }
+       }
+
+       /// Attempts to claim a remote commitment transaction's outputs using the revocation key and
+       /// data in remote_claimable_outpoints. Will directly claim any HTLC outputs which expire at a
+       /// height > height + CLTV_SHARED_CLAIM_BUFFER. In any case, will install monitoring for
+       /// HTLC-Success/HTLC-Timeout transactions.
+       /// Return updates for HTLC pending in the channel and failed automatically by the broadcast of
+       /// revoked remote commitment tx
+       fn check_spend_remote_transaction(&mut self, tx: &Transaction, height: u32, fee_estimator: &FeeEstimator) -> (Vec<Transaction>, (Sha256dHash, Vec<TxOut>), Vec<SpendableOutputDescriptor>) {
+               // Most secp and related errors trying to create keys means we have no hope of constructing
+               // a spend transaction...so we return no transactions to broadcast
+               let mut txn_to_broadcast = Vec::new();
+               let mut watch_outputs = Vec::new();
+               let mut spendable_outputs = Vec::new();
+
+               let commitment_txid = tx.txid(); //TODO: This is gonna be a performance bottleneck for watchtowers!
+               let per_commitment_option = self.remote_claimable_outpoints.get(&commitment_txid);
+
+               macro_rules! ignore_error {
+                       ( $thing : expr ) => {
+                               match $thing {
+                                       Ok(a) => a,
+                                       Err(_) => return (txn_to_broadcast, (commitment_txid, watch_outputs), spendable_outputs)
+                               }
+                       };
+               }
+
+               let commitment_number = 0xffffffffffff - ((((tx.input[0].sequence as u64 & 0xffffff) << 3*8) | (tx.lock_time as u64 & 0xffffff)) ^ self.commitment_transaction_number_obscure_factor);
+               if commitment_number >= self.get_min_seen_secret() {
+                       let secret = self.get_secret(commitment_number).unwrap();
+                       let per_commitment_key = ignore_error!(SecretKey::from_slice(&secret));
+                       let (revocation_pubkey, b_htlc_key, local_payment_key) = match self.key_storage {
+                               Storage::Local { ref revocation_base_key, ref htlc_base_key, ref payment_base_key, .. } => {
+                                       let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key);
+                                       (ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &PublicKey::from_secret_key(&self.secp_ctx, &revocation_base_key))),
+                                       ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &per_commitment_point, &PublicKey::from_secret_key(&self.secp_ctx, &htlc_base_key))),
+                                       Some(ignore_error!(chan_utils::derive_private_key(&self.secp_ctx, &per_commitment_point, &payment_base_key))))
+                               },
+                               Storage::Watchtower { ref revocation_base_key, ref htlc_base_key, .. } => {
+                                       let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key);
+                                       (ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &revocation_base_key)),
+                                       ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &per_commitment_point, &htlc_base_key)),
+                                       None)
+                               },
+                       };
+                       let delayed_key = ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key), &self.their_delayed_payment_base_key.unwrap()));
+                       let a_htlc_key = match self.their_htlc_base_key {
+                               None => return (txn_to_broadcast, (commitment_txid, watch_outputs), spendable_outputs),
+                               Some(their_htlc_base_key) => ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key), &their_htlc_base_key)),
+                       };
+
+                       let revokeable_redeemscript = chan_utils::get_revokeable_redeemscript(&revocation_pubkey, self.our_to_self_delay, &delayed_key);
+                       let revokeable_p2wsh = revokeable_redeemscript.to_v0_p2wsh();
+
+                       let local_payment_p2wpkh = if let Some(payment_key) = local_payment_key {
+                               // Note that the Network here is ignored as we immediately drop the address for the
+                               // script_pubkey version.
+                               let payment_hash160 = Hash160::hash(&PublicKey::from_secret_key(&self.secp_ctx, &payment_key).serialize());
+                               Some(Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(&payment_hash160[..]).into_script())
+                       } else { None };
+
+                       let mut total_value = 0;
+                       let mut inputs = Vec::new();
+                       let mut inputs_info = Vec::new();
+                       let mut inputs_desc = Vec::new();
+
+                       for (idx, outp) in tx.output.iter().enumerate() {
+                               if outp.script_pubkey == revokeable_p2wsh {
+                                       inputs.push(TxIn {
+                                               previous_output: BitcoinOutPoint {
+                                                       txid: commitment_txid,
+                                                       vout: idx as u32,
+                                               },
+                                               script_sig: Script::new(),
+                                               sequence: 0xfffffffd,
+                                               witness: Vec::new(),
+                                       });
+                                       inputs_desc.push(InputDescriptors::RevokedOutput);
+                                       inputs_info.push((None, outp.value, self.our_to_self_delay as u32));
+                                       total_value += outp.value;
+                               } else if Some(&outp.script_pubkey) == local_payment_p2wpkh.as_ref() {
+                                       spendable_outputs.push(SpendableOutputDescriptor::DynamicOutputP2WPKH {
+                                               outpoint: BitcoinOutPoint { txid: commitment_txid, vout: idx as u32 },
+                                               key: local_payment_key.unwrap(),
+                                               output: outp.clone(),
+                                       });
+                               }
+                       }
+
+                       macro_rules! sign_input {
+                               ($sighash_parts: expr, $input: expr, $htlc_idx: expr, $amount: expr) => {
+                                       {
+                                               let (sig, redeemscript, revocation_key) = match self.key_storage {
+                                                       Storage::Local { ref revocation_base_key, .. } => {
+                                                               let redeemscript = if $htlc_idx.is_none() { revokeable_redeemscript.clone() } else {
+                                                                       let htlc = &per_commitment_option.unwrap()[$htlc_idx.unwrap()].0;
+                                                                       chan_utils::get_htlc_redeemscript_with_explicit_keys(htlc, &a_htlc_key, &b_htlc_key, &revocation_pubkey)
+                                                               };
+                                                               let sighash = hash_to_message!(&$sighash_parts.sighash_all(&$input, &redeemscript, $amount)[..]);
+                                                               let revocation_key = ignore_error!(chan_utils::derive_private_revocation_key(&self.secp_ctx, &per_commitment_key, &revocation_base_key));
+                                                               (self.secp_ctx.sign(&sighash, &revocation_key), redeemscript, revocation_key)
+                                                       },
+                                                       Storage::Watchtower { .. } => {
+                                                               unimplemented!();
+                                                       }
+                                               };
+                                               $input.witness.push(sig.serialize_der().to_vec());
+                                               $input.witness[0].push(SigHashType::All as u8);
+                                               if $htlc_idx.is_none() {
+                                                       $input.witness.push(vec!(1));
+                                               } else {
+                                                       $input.witness.push(revocation_pubkey.serialize().to_vec());
+                                               }
+                                               $input.witness.push(redeemscript.clone().into_bytes());
+                                               (redeemscript, revocation_key)
+                                       }
+                               }
+                       }
+
+                       if let Some(ref per_commitment_data) = per_commitment_option {
+                               inputs.reserve_exact(per_commitment_data.len());
+
+                               for (idx, &(ref htlc, _)) in per_commitment_data.iter().enumerate() {
+                                       if let Some(transaction_output_index) = htlc.transaction_output_index {
+                                               let expected_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, &a_htlc_key, &b_htlc_key, &revocation_pubkey);
+                                               if transaction_output_index as usize >= tx.output.len() ||
+                                                               tx.output[transaction_output_index as usize].value != htlc.amount_msat / 1000 ||
+                                                               tx.output[transaction_output_index as usize].script_pubkey != expected_script.to_v0_p2wsh() {
+                                                       return (txn_to_broadcast, (commitment_txid, watch_outputs), spendable_outputs); // Corrupted per_commitment_data, fuck this user
+                                               }
+                                               let input = TxIn {
+                                                       previous_output: BitcoinOutPoint {
+                                                               txid: commitment_txid,
+                                                               vout: transaction_output_index,
+                                                       },
+                                                       script_sig: Script::new(),
+                                                       sequence: 0xfffffffd,
+                                                       witness: Vec::new(),
+                                               };
+                                               if htlc.cltv_expiry > height + CLTV_SHARED_CLAIM_BUFFER {
+                                                       inputs.push(input);
+                                                       inputs_desc.push(if htlc.offered { InputDescriptors::RevokedOfferedHTLC } else { InputDescriptors::RevokedReceivedHTLC });
+                                                       inputs_info.push((Some(idx), tx.output[transaction_output_index as usize].value, htlc.cltv_expiry));
+                                                       total_value += tx.output[transaction_output_index as usize].value;
+                                               } else {
+                                                       let mut single_htlc_tx = Transaction {
+                                                               version: 2,
+                                                               lock_time: 0,
+                                                               input: vec![input],
+                                                               output: vec!(TxOut {
+                                                                       script_pubkey: self.destination_script.clone(),
+                                                                       value: htlc.amount_msat / 1000,
+                                                               }),
+                                                       };
+                                                       let predicted_weight = single_htlc_tx.get_weight() + Self::get_witnesses_weight(&[if htlc.offered { InputDescriptors::RevokedOfferedHTLC } else { InputDescriptors::RevokedReceivedHTLC }]);
+                                                       let height_timer = Self::get_height_timer(height, htlc.cltv_expiry);
+                                                       let mut used_feerate;
+                                                       if subtract_high_prio_fee!(self, fee_estimator, single_htlc_tx.output[0].value, predicted_weight, tx.txid(), used_feerate) {
+                                                               let sighash_parts = bip143::SighashComponents::new(&single_htlc_tx);
+                                                               let (redeemscript, revocation_key) = sign_input!(sighash_parts, single_htlc_tx.input[0], Some(idx), htlc.amount_msat / 1000);
+                                                               assert!(predicted_weight >= single_htlc_tx.get_weight());
+                                                               match self.our_claim_txn_waiting_first_conf.entry(single_htlc_tx.input[0].previous_output.clone()) {
+                                                                       hash_map::Entry::Occupied(_) => {},
+                                                                       hash_map::Entry::Vacant(entry) => { entry.insert((height_timer, TxMaterial::Revoked { script: redeemscript, pubkey: Some(revocation_pubkey), key: revocation_key, is_htlc: true, amount: htlc.amount_msat / 1000 }, used_feerate, htlc.cltv_expiry, height)); }
+                                                               }
+                                                               txn_to_broadcast.push(single_htlc_tx);
+                                                       }
+                                               }
+                                       }
+                               }
+                       }
+
+                       if !inputs.is_empty() || !txn_to_broadcast.is_empty() || per_commitment_option.is_some() { // ie we're confident this is actually ours
+                               // We're definitely a remote commitment transaction!
+                               log_trace!(self, "Got broadcast of revoked remote commitment transaction, generating general spend tx with {} inputs and {} other txn to broadcast", inputs.len(), txn_to_broadcast.len());
+                               watch_outputs.append(&mut tx.output.clone());
+                               self.remote_commitment_txn_on_chain.insert(commitment_txid, (commitment_number, tx.output.iter().map(|output| { output.script_pubkey.clone() }).collect()));
+
+                               macro_rules! check_htlc_fails {
+                                       ($txid: expr, $commitment_tx: expr) => {
+                                               if let Some(ref outpoints) = self.remote_claimable_outpoints.get($txid) {
+                                                       for &(ref htlc, ref source_option) in outpoints.iter() {
+                                                               if let &Some(ref source) = source_option {
+                                                                       log_info!(self, "Failing HTLC with payment_hash {} from {} remote commitment tx due to broadcast of revoked remote commitment transaction, waiting for confirmation (at height {})", log_bytes!(htlc.payment_hash.0), $commitment_tx, height + ANTI_REORG_DELAY - 1);
+                                                                       match self.onchain_events_waiting_threshold_conf.entry(height + ANTI_REORG_DELAY - 1) {
+                                                                               hash_map::Entry::Occupied(mut entry) => {
+                                                                                       let e = entry.get_mut();
+                                                                                       e.retain(|ref event| {
+                                                                                               match **event {
+                                                                                                       OnchainEvent::HTLCUpdate { ref htlc_update } => {
+                                                                                                               return htlc_update.0 != **source
+                                                                                                       },
+                                                                                                       _ => return true
+                                                                                               }
+                                                                                       });
+                                                                                       e.push(OnchainEvent::HTLCUpdate { htlc_update: ((**source).clone(), htlc.payment_hash.clone())});
+                                                                               }
+                                                                               hash_map::Entry::Vacant(entry) => {
+                                                                                       entry.insert(vec![OnchainEvent::HTLCUpdate { htlc_update: ((**source).clone(), htlc.payment_hash.clone())}]);
+                                                                               }
+                                                                       }
+                                                               }
+                                                       }
+                                               }
+                                       }
+                               }
+                               if let Storage::Local { ref current_remote_commitment_txid, ref prev_remote_commitment_txid, .. } = self.key_storage {
+                                       if let &Some(ref txid) = current_remote_commitment_txid {
+                                               check_htlc_fails!(txid, "current");
+                                       }
+                                       if let &Some(ref txid) = prev_remote_commitment_txid {
+                                               check_htlc_fails!(txid, "remote");
+                                       }
+                               }
+                               // No need to check local commitment txn, symmetric HTLCSource must be present as per-htlc data on remote commitment tx
+                       }
+                       if inputs.is_empty() { return (txn_to_broadcast, (commitment_txid, watch_outputs), spendable_outputs); } // Nothing to be done...probably a false positive/local tx
+
+                       let outputs = vec!(TxOut {
+                               script_pubkey: self.destination_script.clone(),
+                               value: total_value,
+                       });
+                       let mut spend_tx = Transaction {
+                               version: 2,
+                               lock_time: 0,
+                               input: inputs,
+                               output: outputs,
+                       };
+
+                       let predicted_weight = spend_tx.get_weight() + Self::get_witnesses_weight(&inputs_desc[..]);
+
+                       let mut used_feerate;
+                       if !subtract_high_prio_fee!(self, fee_estimator, spend_tx.output[0].value, predicted_weight, tx.txid(), used_feerate) {
+                               return (txn_to_broadcast, (commitment_txid, watch_outputs), spendable_outputs);
+                       }
+
+                       let sighash_parts = bip143::SighashComponents::new(&spend_tx);
+
+                       for (input, info) in spend_tx.input.iter_mut().zip(inputs_info.iter()) {
+                               let (redeemscript, revocation_key) = sign_input!(sighash_parts, input, info.0, info.1);
+                               let height_timer = Self::get_height_timer(height, info.2);
+                               match self.our_claim_txn_waiting_first_conf.entry(input.previous_output.clone()) {
+                                       hash_map::Entry::Occupied(_) => {},
+                                       hash_map::Entry::Vacant(entry) => { entry.insert((height_timer, TxMaterial::Revoked { script: redeemscript, pubkey: if info.0.is_some() { Some(revocation_pubkey) } else { None }, key: revocation_key, is_htlc: if info.0.is_some() { true } else { false }, amount: info.1 }, used_feerate, if !info.0.is_some() { height + info.2 } else { info.2 }, height)); }
+                               }
+                       }
+                       assert!(predicted_weight >= spend_tx.get_weight());
+
+                       spendable_outputs.push(SpendableOutputDescriptor::StaticOutput {
+                               outpoint: BitcoinOutPoint { txid: spend_tx.txid(), vout: 0 },
+                               output: spend_tx.output[0].clone(),
+                       });
+                       txn_to_broadcast.push(spend_tx);
+               } else if let Some(per_commitment_data) = per_commitment_option {
+                       // While this isn't useful yet, there is a potential race where if a counterparty
+                       // revokes a state at the same time as the commitment transaction for that state is
+                       // confirmed, and the watchtower receives the block before the user, the user could
+                       // upload a new ChannelMonitor with the revocation secret but the watchtower has
+                       // already processed the block, resulting in the remote_commitment_txn_on_chain entry
+                       // not being generated by the above conditional. Thus, to be safe, we go ahead and
+                       // insert it here.
+                       watch_outputs.append(&mut tx.output.clone());
+                       self.remote_commitment_txn_on_chain.insert(commitment_txid, (commitment_number, tx.output.iter().map(|output| { output.script_pubkey.clone() }).collect()));
+
+                       log_trace!(self, "Got broadcast of non-revoked remote commitment transaction {}", commitment_txid);
+
+                       macro_rules! check_htlc_fails {
+                               ($txid: expr, $commitment_tx: expr, $id: tt) => {
+                                       if let Some(ref latest_outpoints) = self.remote_claimable_outpoints.get($txid) {
+                                               $id: for &(ref htlc, ref source_option) in latest_outpoints.iter() {
+                                                       if let &Some(ref source) = source_option {
+                                                               // Check if the HTLC is present in the commitment transaction that was
+                                                               // broadcast, but not if it was below the dust limit, which we should
+                                                               // fail backwards immediately as there is no way for us to learn the
+                                                               // payment_preimage.
+                                                               // Note that if the dust limit were allowed to change between
+                                                               // commitment transactions we'd want to be check whether *any*
+                                                               // broadcastable commitment transaction has the HTLC in it, but it
+                                                               // cannot currently change after channel initialization, so we don't
+                                                               // need to here.
+                                                               for &(ref broadcast_htlc, ref broadcast_source) in per_commitment_data.iter() {
+                                                                       if broadcast_htlc.transaction_output_index.is_some() && Some(source) == broadcast_source.as_ref() {
+                                                                               continue $id;
+                                                                       }
+                                                               }
+                                                               log_trace!(self, "Failing HTLC with payment_hash {} from {} remote commitment tx due to broadcast of remote commitment transaction", log_bytes!(htlc.payment_hash.0), $commitment_tx);
+                                                               match self.onchain_events_waiting_threshold_conf.entry(height + ANTI_REORG_DELAY - 1) {
+                                                                       hash_map::Entry::Occupied(mut entry) => {
+                                                                               let e = entry.get_mut();
+                                                                               e.retain(|ref event| {
+                                                                                       match **event {
+                                                                                               OnchainEvent::HTLCUpdate { ref htlc_update } => {
+                                                                                                       return htlc_update.0 != **source
+                                                                                               },
+                                                                                               _ => return true
+                                                                                       }
+                                                                               });
+                                                                               e.push(OnchainEvent::HTLCUpdate { htlc_update: ((**source).clone(), htlc.payment_hash.clone())});
+                                                                       }
+                                                                       hash_map::Entry::Vacant(entry) => {
+                                                                               entry.insert(vec![OnchainEvent::HTLCUpdate { htlc_update: ((**source).clone(), htlc.payment_hash.clone())}]);
+                                                                       }
+                                                               }
+                                                       }
+                                               }
+                                       }
+                               }
+                       }
+                       if let Storage::Local { ref current_remote_commitment_txid, ref prev_remote_commitment_txid, .. } = self.key_storage {
+                               if let &Some(ref txid) = current_remote_commitment_txid {
+                                       check_htlc_fails!(txid, "current", 'current_loop);
+                               }
+                               if let &Some(ref txid) = prev_remote_commitment_txid {
+                                       check_htlc_fails!(txid, "previous", 'prev_loop);
+                               }
+                       }
+
+                       if let Some(revocation_points) = self.their_cur_revocation_points {
+                               let revocation_point_option =
+                                       if revocation_points.0 == commitment_number { Some(&revocation_points.1) }
+                                       else if let Some(point) = revocation_points.2.as_ref() {
+                                               if revocation_points.0 == commitment_number + 1 { Some(point) } else { None }
+                                       } else { None };
+                               if let Some(revocation_point) = revocation_point_option {
+                                       let (revocation_pubkey, b_htlc_key) = match self.key_storage {
+                                               Storage::Local { ref revocation_base_key, ref htlc_base_key, .. } => {
+                                                       (ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, revocation_point, &PublicKey::from_secret_key(&self.secp_ctx, &revocation_base_key))),
+                                                       ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, revocation_point, &PublicKey::from_secret_key(&self.secp_ctx, &htlc_base_key))))
+                                               },
+                                               Storage::Watchtower { ref revocation_base_key, ref htlc_base_key, .. } => {
+                                                       (ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, revocation_point, &revocation_base_key)),
+                                                       ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, revocation_point, &htlc_base_key)))
+                                               },
+                                       };
+                                       let a_htlc_key = match self.their_htlc_base_key {
+                                               None => return (txn_to_broadcast, (commitment_txid, watch_outputs), spendable_outputs),
+                                               Some(their_htlc_base_key) => ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, revocation_point, &their_htlc_base_key)),
+                                       };
+
+                                       for (idx, outp) in tx.output.iter().enumerate() {
+                                               if outp.script_pubkey.is_v0_p2wpkh() {
+                                                       match self.key_storage {
+                                                               Storage::Local { ref payment_base_key, .. } => {
+                                                                       if let Ok(local_key) = chan_utils::derive_private_key(&self.secp_ctx, &revocation_point, &payment_base_key) {
+                                                                               spendable_outputs.push(SpendableOutputDescriptor::DynamicOutputP2WPKH {
+                                                                                       outpoint: BitcoinOutPoint { txid: commitment_txid, vout: idx as u32 },
+                                                                                       key: local_key,
+                                                                                       output: outp.clone(),
+                                                                               });
+                                                                       }
+                                                               },
+                                                               Storage::Watchtower { .. } => {}
+                                                       }
+                                                       break; // Only to_remote ouput is claimable
+                                               }
+                                       }
+
+                                       let mut total_value = 0;
+                                       let mut inputs = Vec::new();
+                                       let mut inputs_desc = Vec::new();
+                                       let mut inputs_info = Vec::new();
+
+                                       macro_rules! sign_input {
+                                               ($sighash_parts: expr, $input: expr, $amount: expr, $preimage: expr) => {
+                                                       {
+                                                               let (sig, redeemscript, htlc_key) = match self.key_storage {
+                                                                       Storage::Local { ref htlc_base_key, .. } => {
+                                                                               let htlc = &per_commitment_option.unwrap()[$input.sequence as usize].0;
+                                                                               let redeemscript = chan_utils::get_htlc_redeemscript_with_explicit_keys(htlc, &a_htlc_key, &b_htlc_key, &revocation_pubkey);
+                                                                               let sighash = hash_to_message!(&$sighash_parts.sighash_all(&$input, &redeemscript, $amount)[..]);
+                                                                               let htlc_key = ignore_error!(chan_utils::derive_private_key(&self.secp_ctx, revocation_point, &htlc_base_key));
+                                                                               (self.secp_ctx.sign(&sighash, &htlc_key), redeemscript, htlc_key)
+                                                                       },
+                                                                       Storage::Watchtower { .. } => {
+                                                                               unimplemented!();
+                                                                       }
+                                                               };
+                                                               $input.witness.push(sig.serialize_der().to_vec());
+                                                               $input.witness[0].push(SigHashType::All as u8);
+                                                               $input.witness.push($preimage);
+                                                               $input.witness.push(redeemscript.clone().into_bytes());
+                                                               (redeemscript, htlc_key)
+                                                       }
+                                               }
+                                       }
+
+                                       for (idx, &(ref htlc, _)) in per_commitment_data.iter().enumerate() {
+                                               if let Some(transaction_output_index) = htlc.transaction_output_index {
+                                                       let expected_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, &a_htlc_key, &b_htlc_key, &revocation_pubkey);
+                                                       if transaction_output_index as usize >= tx.output.len() ||
+                                                                       tx.output[transaction_output_index as usize].value != htlc.amount_msat / 1000 ||
+                                                                       tx.output[transaction_output_index as usize].script_pubkey != expected_script.to_v0_p2wsh() {
+                                                               return (txn_to_broadcast, (commitment_txid, watch_outputs), spendable_outputs); // Corrupted per_commitment_data, fuck this user
+                                                       }
+                                                       if let Some(payment_preimage) = self.payment_preimages.get(&htlc.payment_hash) {
+                                                               let input = TxIn {
+                                                                       previous_output: BitcoinOutPoint {
+                                                                               txid: commitment_txid,
+                                                                               vout: transaction_output_index,
+                                                                       },
+                                                                       script_sig: Script::new(),
+                                                                       sequence: idx as u32, // reset to 0xfffffffd in sign_input
+                                                                       witness: Vec::new(),
+                                                               };
+                                                               if htlc.cltv_expiry > height + CLTV_SHARED_CLAIM_BUFFER {
+                                                                       inputs.push(input);
+                                                                       inputs_desc.push(if htlc.offered { InputDescriptors::OfferedHTLC } else { InputDescriptors::ReceivedHTLC });
+                                                                       inputs_info.push((payment_preimage, tx.output[transaction_output_index as usize].value, htlc.cltv_expiry));
+                                                                       total_value += tx.output[transaction_output_index as usize].value;
+                                                               } else {
+                                                                       let mut single_htlc_tx = Transaction {
+                                                                               version: 2,
+                                                                               lock_time: 0,
+                                                                               input: vec![input],
+                                                                               output: vec!(TxOut {
+                                                                                       script_pubkey: self.destination_script.clone(),
+                                                                                       value: htlc.amount_msat / 1000,
+                                                                               }),
+                                                                       };
+                                                                       let predicted_weight = single_htlc_tx.get_weight() + Self::get_witnesses_weight(&[if htlc.offered { InputDescriptors::OfferedHTLC } else { InputDescriptors::ReceivedHTLC }]);
+                                                                       let height_timer = Self::get_height_timer(height, htlc.cltv_expiry);
+                                                                       let mut used_feerate;
+                                                                       if subtract_high_prio_fee!(self, fee_estimator, single_htlc_tx.output[0].value, predicted_weight, tx.txid(), used_feerate) {
+                                                                               let sighash_parts = bip143::SighashComponents::new(&single_htlc_tx);
+                                                                               let (redeemscript, htlc_key) = sign_input!(sighash_parts, single_htlc_tx.input[0], htlc.amount_msat / 1000, payment_preimage.0.to_vec());
+                                                                               assert!(predicted_weight >= single_htlc_tx.get_weight());
+                                                                               spendable_outputs.push(SpendableOutputDescriptor::StaticOutput {
+                                                                                       outpoint: BitcoinOutPoint { txid: single_htlc_tx.txid(), vout: 0 },
+                                                                                       output: single_htlc_tx.output[0].clone(),
+                                                                               });
+                                                                               match self.our_claim_txn_waiting_first_conf.entry(single_htlc_tx.input[0].previous_output.clone()) {
+                                                                                       hash_map::Entry::Occupied(_) => {},
+                                                                                       hash_map::Entry::Vacant(entry) => { entry.insert((height_timer, TxMaterial::RemoteHTLC { script: redeemscript, key: htlc_key, preimage: Some(*payment_preimage), amount: htlc.amount_msat / 1000 }, used_feerate, htlc.cltv_expiry, height)); }
+                                                                               }
+                                                                               txn_to_broadcast.push(single_htlc_tx);
+                                                                       }
+                                                               }
+                                                       }
+                                                       if !htlc.offered {
+                                                               // TODO: If the HTLC has already expired, potentially merge it with the
+                                                               // rest of the claim transaction, as above.
+                                                               let input = TxIn {
+                                                                       previous_output: BitcoinOutPoint {
+                                                                               txid: commitment_txid,
+                                                                               vout: transaction_output_index,
+                                                                       },
+                                                                       script_sig: Script::new(),
+                                                                       sequence: idx as u32,
+                                                                       witness: Vec::new(),
+                                                               };
+                                                               let mut timeout_tx = Transaction {
+                                                                       version: 2,
+                                                                       lock_time: htlc.cltv_expiry,
+                                                                       input: vec![input],
+                                                                       output: vec!(TxOut {
+                                                                               script_pubkey: self.destination_script.clone(),
+                                                                               value: htlc.amount_msat / 1000,
+                                                                       }),
+                                                               };
+                                                               let predicted_weight = timeout_tx.get_weight() + Self::get_witnesses_weight(&[InputDescriptors::ReceivedHTLC]);
+                                                               let height_timer = Self::get_height_timer(height, htlc.cltv_expiry);
+                                                               let mut used_feerate;
+                                                               if subtract_high_prio_fee!(self, fee_estimator, timeout_tx.output[0].value, predicted_weight, tx.txid(), used_feerate) {
+                                                                       let sighash_parts = bip143::SighashComponents::new(&timeout_tx);
+                                                                       let (redeemscript, htlc_key) = sign_input!(sighash_parts, timeout_tx.input[0], htlc.amount_msat / 1000, vec![0]);
+                                                                       assert!(predicted_weight >= timeout_tx.get_weight());
+                                                                       //TODO: track SpendableOutputDescriptor
+                                                                       match self.our_claim_txn_waiting_first_conf.entry(timeout_tx.input[0].previous_output.clone()) {
+                                                                               hash_map::Entry::Occupied(_) => {},
+                                                                               hash_map::Entry::Vacant(entry) => { entry.insert((height_timer, TxMaterial::RemoteHTLC { script : redeemscript, key: htlc_key, preimage: None, amount: htlc.amount_msat / 1000 }, used_feerate, htlc.cltv_expiry, height)); }
+                                                                       }
+                                                               }
+                                                               txn_to_broadcast.push(timeout_tx);
+                                                       }
+                                               }
+                                       }
+
+                                       if inputs.is_empty() { return (txn_to_broadcast, (commitment_txid, watch_outputs), spendable_outputs); } // Nothing to be done...probably a false positive/local tx
+
+                                       let outputs = vec!(TxOut {
+                                               script_pubkey: self.destination_script.clone(),
+                                               value: total_value
+                                       });
+                                       let mut spend_tx = Transaction {
+                                               version: 2,
+                                               lock_time: 0,
+                                               input: inputs,
+                                               output: outputs,
+                                       };
+
+                                       let mut predicted_weight = spend_tx.get_weight() + Self::get_witnesses_weight(&inputs_desc[..]);
+
+                                       let mut used_feerate;
+                                       if !subtract_high_prio_fee!(self, fee_estimator, spend_tx.output[0].value, predicted_weight, tx.txid(), used_feerate) {
+                                               return (txn_to_broadcast, (commitment_txid, watch_outputs), spendable_outputs);
+                                       }
+
+                                       let sighash_parts = bip143::SighashComponents::new(&spend_tx);
+
+                                       for (input, info) in spend_tx.input.iter_mut().zip(inputs_info.iter()) {
+                                               let (redeemscript, htlc_key) = sign_input!(sighash_parts, input, info.1, (info.0).0.to_vec());
+                                               let height_timer = Self::get_height_timer(height, info.2);
+                                               match self.our_claim_txn_waiting_first_conf.entry(input.previous_output.clone()) {
+                                                       hash_map::Entry::Occupied(_) => {},
+                                                       hash_map::Entry::Vacant(entry) => { entry.insert((height_timer, TxMaterial::RemoteHTLC { script: redeemscript, key: htlc_key, preimage: Some(*(info.0)), amount: info.1}, used_feerate, info.2, height)); }
+                                               }
+                                       }
+                                       assert!(predicted_weight >= spend_tx.get_weight());
+                                       spendable_outputs.push(SpendableOutputDescriptor::StaticOutput {
+                                               outpoint: BitcoinOutPoint { txid: spend_tx.txid(), vout: 0 },
+                                               output: spend_tx.output[0].clone(),
+                                       });
+                                       txn_to_broadcast.push(spend_tx);
+                               }
+                       }
+               } else if let Some((ref to_remote_rescue, ref local_key)) = self.to_remote_rescue {
+                       for (idx, outp) in tx.output.iter().enumerate() {
+                               if to_remote_rescue == &outp.script_pubkey {
+                                       spendable_outputs.push(SpendableOutputDescriptor::DynamicOutputP2WPKH {
+                                               outpoint: BitcoinOutPoint { txid: commitment_txid, vout: idx as u32 },
+                                               key: local_key.clone(),
+                                               output: outp.clone(),
+                                       });
+                               }
+                       }
+               }
+
+               (txn_to_broadcast, (commitment_txid, watch_outputs), spendable_outputs)
+       }
+
+       /// Attempts to claim a remote HTLC-Success/HTLC-Timeout's outputs using the revocation key
+       fn check_spend_remote_htlc(&mut self, tx: &Transaction, commitment_number: u64, height: u32, fee_estimator: &FeeEstimator) -> (Option<Transaction>, Option<SpendableOutputDescriptor>) {
+               if tx.input.len() != 1 || tx.output.len() != 1 {
+                       return (None, None)
+               }
+
+               macro_rules! ignore_error {
+                       ( $thing : expr ) => {
+                               match $thing {
+                                       Ok(a) => a,
+                                       Err(_) => return (None, None)
+                               }
+                       };
+               }
+
+               let secret = if let Some(secret) = self.get_secret(commitment_number) { secret } else { return (None, None); };
+               let per_commitment_key = ignore_error!(SecretKey::from_slice(&secret));
+               let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key);
+               let revocation_pubkey = match self.key_storage {
+                       Storage::Local { ref revocation_base_key, .. } => {
+                               ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &PublicKey::from_secret_key(&self.secp_ctx, &revocation_base_key)))
+                       },
+                       Storage::Watchtower { ref revocation_base_key, .. } => {
+                               ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &revocation_base_key))
+                       },
+               };
+               let delayed_key = match self.their_delayed_payment_base_key {
+                       None => return (None, None),
+                       Some(their_delayed_payment_base_key) => ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &per_commitment_point, &their_delayed_payment_base_key)),
+               };
+               let redeemscript = chan_utils::get_revokeable_redeemscript(&revocation_pubkey, self.our_to_self_delay, &delayed_key);
+               let revokeable_p2wsh = redeemscript.to_v0_p2wsh();
+               let htlc_txid = tx.txid(); //TODO: This is gonna be a performance bottleneck for watchtowers!
+
+               let mut inputs = Vec::new();
+               let mut amount = 0;
+
+               if tx.output[0].script_pubkey == revokeable_p2wsh { //HTLC transactions have one txin, one txout
+                       inputs.push(TxIn {
+                               previous_output: BitcoinOutPoint {
+                                       txid: htlc_txid,
+                                       vout: 0,
+                               },
+                               script_sig: Script::new(),
+                               sequence: 0xfffffffd,
+                               witness: Vec::new(),
+                       });
+                       amount = tx.output[0].value;
+               }
+
+               if !inputs.is_empty() {
+                       let outputs = vec!(TxOut {
+                               script_pubkey: self.destination_script.clone(),
+                               value: amount
+                       });
+
+                       let mut spend_tx = Transaction {
+                               version: 2,
+                               lock_time: 0,
+                               input: inputs,
+                               output: outputs,
+                       };
+                       let predicted_weight = spend_tx.get_weight() + Self::get_witnesses_weight(&[InputDescriptors::RevokedOutput]);
+                       let mut used_feerate;
+                       if !subtract_high_prio_fee!(self, fee_estimator, spend_tx.output[0].value, predicted_weight, tx.txid(), used_feerate) {
+                               return (None, None);
+                       }
+
+                       let sighash_parts = bip143::SighashComponents::new(&spend_tx);
+
+                       let (sig, revocation_key) = match self.key_storage {
+                               Storage::Local { ref revocation_base_key, .. } => {
+                                       let sighash = hash_to_message!(&sighash_parts.sighash_all(&spend_tx.input[0], &redeemscript, amount)[..]);
+                                       let revocation_key = ignore_error!(chan_utils::derive_private_revocation_key(&self.secp_ctx, &per_commitment_key, &revocation_base_key));
+                                       (self.secp_ctx.sign(&sighash, &revocation_key), revocation_key)
+                               }
+                               Storage::Watchtower { .. } => {
+                                       unimplemented!();
+                               }
+                       };
+                       spend_tx.input[0].witness.push(sig.serialize_der().to_vec());
+                       spend_tx.input[0].witness[0].push(SigHashType::All as u8);
+                       spend_tx.input[0].witness.push(vec!(1));
+                       spend_tx.input[0].witness.push(redeemscript.clone().into_bytes());
+
+                       assert!(predicted_weight >= spend_tx.get_weight());
+                       let outpoint = BitcoinOutPoint { txid: spend_tx.txid(), vout: 0 };
+                       let output = spend_tx.output[0].clone();
+                       let height_timer = Self::get_height_timer(height, self.their_to_self_delay.unwrap() as u32); // We can safely unwrap given we are past channel opening
+                       match self.our_claim_txn_waiting_first_conf.entry(spend_tx.input[0].previous_output.clone()) {
+                               hash_map::Entry::Occupied(_) => {},
+                               hash_map::Entry::Vacant(entry) => { entry.insert((height_timer, TxMaterial::Revoked { script: redeemscript, pubkey: None, key: revocation_key, is_htlc: false, amount: tx.output[0].value }, used_feerate, height + self.our_to_self_delay as u32, height)); }
+                       }
+                       (Some(spend_tx), Some(SpendableOutputDescriptor::StaticOutput { outpoint, output }))
+               } else { (None, None) }
+       }
+
+       fn broadcast_by_local_state(&self, local_tx: &LocalSignedTx, per_commitment_point: &Option<PublicKey>, delayed_payment_base_key: &Option<SecretKey>, height: u32) -> (Vec<Transaction>, Vec<SpendableOutputDescriptor>, Vec<TxOut>, Vec<(BitcoinOutPoint, (u32, TxMaterial, u64, u32, u32))>) {
+               let mut res = Vec::with_capacity(local_tx.htlc_outputs.len());
+               let mut spendable_outputs = Vec::with_capacity(local_tx.htlc_outputs.len());
+               let mut watch_outputs = Vec::with_capacity(local_tx.htlc_outputs.len());
+               let mut pending_claims = Vec::with_capacity(local_tx.htlc_outputs.len());
+
+               macro_rules! add_dynamic_output {
+                       ($father_tx: expr, $vout: expr) => {
+                               if let Some(ref per_commitment_point) = *per_commitment_point {
+                                       if let Some(ref delayed_payment_base_key) = *delayed_payment_base_key {
+                                               if let Ok(local_delayedkey) = chan_utils::derive_private_key(&self.secp_ctx, per_commitment_point, delayed_payment_base_key) {
+                                                       spendable_outputs.push(SpendableOutputDescriptor::DynamicOutputP2WSH {
+                                                               outpoint: BitcoinOutPoint { txid: $father_tx.txid(), vout: $vout },
+                                                               key: local_delayedkey,
+                                                               witness_script: chan_utils::get_revokeable_redeemscript(&local_tx.revocation_key, self.our_to_self_delay, &local_tx.delayed_payment_key),
+                                                               to_self_delay: self.our_to_self_delay,
+                                                               output: $father_tx.output[$vout as usize].clone(),
+                                                       });
+                                               }
+                                       }
+                               }
+                       }
+               }
+
+
+               let redeemscript = chan_utils::get_revokeable_redeemscript(&local_tx.revocation_key, self.their_to_self_delay.unwrap(), &local_tx.delayed_payment_key);
+               let revokeable_p2wsh = redeemscript.to_v0_p2wsh();
+               for (idx, output) in local_tx.tx.output.iter().enumerate() {
+                       if output.script_pubkey == revokeable_p2wsh {
+                               add_dynamic_output!(local_tx.tx, idx as u32);
+                               break;
+                       }
+               }
+
+               for &(ref htlc, ref sigs, _) in local_tx.htlc_outputs.iter() {
+                       if let Some(transaction_output_index) = htlc.transaction_output_index {
+                               if let &Some((ref their_sig, ref our_sig)) = sigs {
+                                       if htlc.offered {
+                                               log_trace!(self, "Broadcasting HTLC-Timeout transaction against local commitment transactions");
+                                               let mut htlc_timeout_tx = chan_utils::build_htlc_transaction(&local_tx.txid, local_tx.feerate_per_kw, self.their_to_self_delay.unwrap(), htlc, &local_tx.delayed_payment_key, &local_tx.revocation_key);
+
+                                               htlc_timeout_tx.input[0].witness.push(Vec::new()); // First is the multisig dummy
+
+                                               htlc_timeout_tx.input[0].witness.push(their_sig.serialize_der().to_vec());
+                                               htlc_timeout_tx.input[0].witness[1].push(SigHashType::All as u8);
+                                               htlc_timeout_tx.input[0].witness.push(our_sig.serialize_der().to_vec());
+                                               htlc_timeout_tx.input[0].witness[2].push(SigHashType::All as u8);
+
+                                               htlc_timeout_tx.input[0].witness.push(Vec::new());
+                                               let htlc_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(htlc, &local_tx.a_htlc_key, &local_tx.b_htlc_key, &local_tx.revocation_key);
+                                               htlc_timeout_tx.input[0].witness.push(htlc_script.clone().into_bytes());
+
+                                               add_dynamic_output!(htlc_timeout_tx, 0);
+                                               let height_timer = Self::get_height_timer(height, htlc.cltv_expiry);
+                                               pending_claims.push((htlc_timeout_tx.input[0].previous_output.clone(), (height_timer, TxMaterial::LocalHTLC { script: htlc_script, sigs: (*their_sig, *our_sig), preimage: None, amount: htlc.amount_msat / 1000}, 0, htlc.cltv_expiry, height)));
+                                               res.push(htlc_timeout_tx);
+                                       } else {
+                                               if let Some(payment_preimage) = self.payment_preimages.get(&htlc.payment_hash) {
+                                                       log_trace!(self, "Broadcasting HTLC-Success transaction against local commitment transactions");
+                                                       let mut htlc_success_tx = chan_utils::build_htlc_transaction(&local_tx.txid, local_tx.feerate_per_kw, self.their_to_self_delay.unwrap(), htlc, &local_tx.delayed_payment_key, &local_tx.revocation_key);
+
+                                                       htlc_success_tx.input[0].witness.push(Vec::new()); // First is the multisig dummy
+
+                                                       htlc_success_tx.input[0].witness.push(their_sig.serialize_der().to_vec());
+                                                       htlc_success_tx.input[0].witness[1].push(SigHashType::All as u8);
+                                                       htlc_success_tx.input[0].witness.push(our_sig.serialize_der().to_vec());
+                                                       htlc_success_tx.input[0].witness[2].push(SigHashType::All as u8);
+
+                                                       htlc_success_tx.input[0].witness.push(payment_preimage.0.to_vec());
+                                                       let htlc_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(htlc, &local_tx.a_htlc_key, &local_tx.b_htlc_key, &local_tx.revocation_key);
+                                                       htlc_success_tx.input[0].witness.push(htlc_script.clone().into_bytes());
+
+                                                       add_dynamic_output!(htlc_success_tx, 0);
+                                                       let height_timer = Self::get_height_timer(height, htlc.cltv_expiry);
+                                                       pending_claims.push((htlc_success_tx.input[0].previous_output.clone(), (height_timer, TxMaterial::LocalHTLC { script: htlc_script, sigs: (*their_sig, *our_sig), preimage: Some(*payment_preimage), amount: htlc.amount_msat / 1000}, 0, htlc.cltv_expiry, height)));
+                                                       res.push(htlc_success_tx);
+                                               }
+                                       }
+                                       watch_outputs.push(local_tx.tx.output[transaction_output_index as usize].clone());
+                               } else { panic!("Should have sigs for non-dust local tx outputs!") }
+                       }
+               }
+
+               (res, spendable_outputs, watch_outputs, pending_claims)
+       }
+
+       /// Attempts to claim any claimable HTLCs in a commitment transaction which was not (yet)
+       /// revoked using data in local_claimable_outpoints.
+       /// Should not be used if check_spend_revoked_transaction succeeds.
+       fn check_spend_local_transaction(&mut self, tx: &Transaction, height: u32) -> (Vec<Transaction>, Vec<SpendableOutputDescriptor>, (Sha256dHash, Vec<TxOut>)) {
+               let commitment_txid = tx.txid();
+               let mut local_txn = Vec::new();
+               let mut spendable_outputs = Vec::new();
+               let mut watch_outputs = Vec::new();
+
+               macro_rules! wait_threshold_conf {
+                       ($height: expr, $source: expr, $commitment_tx: expr, $payment_hash: expr) => {
+                               log_trace!(self, "Failing HTLC with payment_hash {} from {} local commitment tx due to broadcast of transaction, waiting confirmation (at height{})", log_bytes!($payment_hash.0), $commitment_tx, height + ANTI_REORG_DELAY - 1);
+                               match self.onchain_events_waiting_threshold_conf.entry($height + ANTI_REORG_DELAY - 1) {
+                                       hash_map::Entry::Occupied(mut entry) => {
+                                               let e = entry.get_mut();
+                                               e.retain(|ref event| {
+                                                       match **event {
+                                                               OnchainEvent::HTLCUpdate { ref htlc_update } => {
+                                                                       return htlc_update.0 != $source
+                                                               },
+                                                               _ => return true
+                                                       }
+                                               });
+                                               e.push(OnchainEvent::HTLCUpdate { htlc_update: ($source, $payment_hash)});
+                                       }
+                                       hash_map::Entry::Vacant(entry) => {
+                                               entry.insert(vec![OnchainEvent::HTLCUpdate { htlc_update: ($source, $payment_hash)}]);
+                                       }
+                               }
+                       }
+               }
+
+               macro_rules! append_onchain_update {
+                       ($updates: expr) => {
+                               local_txn.append(&mut $updates.0);
+                               spendable_outputs.append(&mut $updates.1);
+                               watch_outputs.append(&mut $updates.2);
+                               for claim in $updates.3 {
+                                       match self.our_claim_txn_waiting_first_conf.entry(claim.0) {
+                                               hash_map::Entry::Occupied(_) => {},
+                                               hash_map::Entry::Vacant(entry) => { entry.insert(claim.1); }
+                                       }
+                               }
+                       }
+               }
+
+               // HTLCs set may differ between last and previous local commitment txn, in case of one them hitting chain, ensure we cancel all HTLCs backward
+               let mut is_local_tx = false;
+
+               if let &Some(ref local_tx) = &self.current_local_signed_commitment_tx {
+                       if local_tx.txid == commitment_txid {
+                               is_local_tx = true;
+                               log_trace!(self, "Got latest local commitment tx broadcast, searching for available HTLCs to claim");
+                               match self.key_storage {
+                                       Storage::Local { ref delayed_payment_base_key, ref latest_per_commitment_point, .. } => {
+                                               append_onchain_update!(self.broadcast_by_local_state(local_tx, latest_per_commitment_point, &Some(*delayed_payment_base_key), height));
+                                       },
+                                       Storage::Watchtower { .. } => {
+                                               append_onchain_update!(self.broadcast_by_local_state(local_tx, &None, &None, height));
+                                       }
+                               }
+                       }
+               }
+               if let &Some(ref local_tx) = &self.prev_local_signed_commitment_tx {
+                       if local_tx.txid == commitment_txid {
+                               is_local_tx = true;
+                               log_trace!(self, "Got previous local commitment tx broadcast, searching for available HTLCs to claim");
+                               match self.key_storage {
+                                       Storage::Local { ref delayed_payment_base_key, ref prev_latest_per_commitment_point, .. } => {
+                                               append_onchain_update!(self.broadcast_by_local_state(local_tx, prev_latest_per_commitment_point, &Some(*delayed_payment_base_key), height));
+                                       },
+                                       Storage::Watchtower { .. } => {
+                                               append_onchain_update!(self.broadcast_by_local_state(local_tx, &None, &None, height));
+                                       }
+                               }
+                       }
+               }
+
+               macro_rules! fail_dust_htlcs_after_threshold_conf {
+                       ($local_tx: expr) => {
+                               for &(ref htlc, _, ref source) in &$local_tx.htlc_outputs {
+                                       if htlc.transaction_output_index.is_none() {
+                                               if let &Some(ref source) = source {
+                                                       wait_threshold_conf!(height, source.clone(), "lastest", htlc.payment_hash.clone());
+                                               }
+                                       }
+                               }
+                       }
+               }
+
+               if is_local_tx {
+                       if let &Some(ref local_tx) = &self.current_local_signed_commitment_tx {
+                               fail_dust_htlcs_after_threshold_conf!(local_tx);
+                       }
+                       if let &Some(ref local_tx) = &self.prev_local_signed_commitment_tx {
+                               fail_dust_htlcs_after_threshold_conf!(local_tx);
+                       }
+               }
+
+               (local_txn, spendable_outputs, (commitment_txid, watch_outputs))
+       }
+
+       /// Generate a spendable output event when closing_transaction get registered onchain.
+       fn check_spend_closing_transaction(&self, tx: &Transaction) -> Option<SpendableOutputDescriptor> {
+               if tx.input[0].sequence == 0xFFFFFFFF && !tx.input[0].witness.is_empty() && tx.input[0].witness.last().unwrap().len() == 71 {
+                       match self.key_storage {
+                               Storage::Local { ref shutdown_pubkey, .. } =>  {
+                                       let our_channel_close_key_hash = Hash160::hash(&shutdown_pubkey.serialize());
+                                       let shutdown_script = Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(&our_channel_close_key_hash[..]).into_script();
+                                       for (idx, output) in tx.output.iter().enumerate() {
+                                               if shutdown_script == output.script_pubkey {
+                                                       return Some(SpendableOutputDescriptor::StaticOutput {
+                                                               outpoint: BitcoinOutPoint { txid: tx.txid(), vout: idx as u32 },
+                                                               output: output.clone(),
+                                                       });
+                                               }
+                                       }
+                               }
+                               Storage::Watchtower { .. } => {
+                                       //TODO: we need to ensure an offline client will generate the event when it
+                                       // comes back online after only the watchtower saw the transaction
+                               }
+                       }
+               }
+               None
+       }
+
+       /// Used by ChannelManager deserialization to broadcast the latest local state if its copy of
+       /// the Channel was out-of-date. You may use it to get a broadcastable local toxic tx in case of
+       /// fallen-behind, i.e when receiving a channel_reestablish with a proof that our remote side knows
+       /// a higher revocation secret than the local commitment number we are aware of. Broadcasting these
+       /// transactions are UNSAFE, as they allow remote side to punish you. Nevertheless you may want to
+       /// broadcast them if remote don't close channel with his higher commitment transaction after a
+       /// substantial amount of time (a month or even a year) to get back funds. Best may be to contact
+       /// out-of-band the other node operator to coordinate with him if option is available to you.
+       /// In any-case, choice is up to the user.
+       pub fn get_latest_local_commitment_txn(&self) -> Vec<Transaction> {
+               if let &Some(ref local_tx) = &self.current_local_signed_commitment_tx {
+                       let mut res = vec![local_tx.tx.clone()];
+                       match self.key_storage {
+                               Storage::Local { ref delayed_payment_base_key, ref prev_latest_per_commitment_point, .. } => {
+                                       res.append(&mut self.broadcast_by_local_state(local_tx, prev_latest_per_commitment_point, &Some(*delayed_payment_base_key), 0).0);
+                                       // We throw away the generated waiting_first_conf data as we aren't (yet) confirmed and we don't actually know what the caller wants to do.
+                                       // The data will be re-generated and tracked in check_spend_local_transaction if we get a confirmation.
+                               },
+                               _ => panic!("Can only broadcast by local channelmonitor"),
+                       };
+                       res
+               } else {
+                       Vec::new()
+               }
+       }
+
+       fn block_connected(&mut self, txn_matched: &[&Transaction], height: u32, block_hash: &Sha256dHash, broadcaster: &BroadcasterInterface, fee_estimator: &FeeEstimator)-> (Vec<(Sha256dHash, Vec<TxOut>)>, Vec<SpendableOutputDescriptor>, Vec<(HTLCSource, Option<PaymentPreimage>, PaymentHash)>) {
+               let mut watch_outputs = Vec::new();
+               let mut spendable_outputs = Vec::new();
+               let mut htlc_updated = Vec::new();
+               for tx in txn_matched {
+                       if tx.input.len() == 1 {
+                               // Assuming our keys were not leaked (in which case we're screwed no matter what),
+                               // commitment transactions and HTLC transactions will all only ever have one input,
+                               // which is an easy way to filter out any potential non-matching txn for lazy
+                               // filters.
+                               let prevout = &tx.input[0].previous_output;
+                               let mut txn: Vec<Transaction> = Vec::new();
+                               let funding_txo = match self.key_storage {
+                                       Storage::Local { ref funding_info, .. } => {
+                                               funding_info.clone()
+                                       }
+                                       Storage::Watchtower { .. } => {
+                                               unimplemented!();
+                                       }
+                               };
+                               if funding_txo.is_none() || (prevout.txid == funding_txo.as_ref().unwrap().0.txid && prevout.vout == funding_txo.as_ref().unwrap().0.index as u32) {
+                                       if (tx.input[0].sequence >> 8*3) as u8 == 0x80 && (tx.lock_time >> 8*3) as u8 == 0x20 {
+                                               let (remote_txn, new_outputs, mut spendable_output) = self.check_spend_remote_transaction(tx, height, fee_estimator);
+                                               txn = remote_txn;
+                                               spendable_outputs.append(&mut spendable_output);
+                                               if !new_outputs.1.is_empty() {
+                                                       watch_outputs.push(new_outputs);
+                                               }
+                                               if txn.is_empty() {
+                                                       let (local_txn, mut spendable_output, new_outputs) = self.check_spend_local_transaction(tx, height);
+                                                       spendable_outputs.append(&mut spendable_output);
+                                                       txn = local_txn;
+                                                       if !new_outputs.1.is_empty() {
+                                                               watch_outputs.push(new_outputs);
+                                                       }
+                                               }
+                                       }
+                                       if !funding_txo.is_none() && txn.is_empty() {
+                                               if let Some(spendable_output) = self.check_spend_closing_transaction(tx) {
+                                                       spendable_outputs.push(spendable_output);
+                                               }
+                                       }
+                               } else {
+                                       if let Some(&(commitment_number, _)) = self.remote_commitment_txn_on_chain.get(&prevout.txid) {
+                                               let (tx, spendable_output) = self.check_spend_remote_htlc(tx, commitment_number, height, fee_estimator);
+                                               if let Some(tx) = tx {
+                                                       txn.push(tx);
+                                               }
+                                               if let Some(spendable_output) = spendable_output {
+                                                       spendable_outputs.push(spendable_output);
+                                               }
+                                       }
+                               }
+                               for tx in txn.iter() {
+                                       broadcaster.broadcast_transaction(tx);
+                               }
+                       }
+                       // While all commitment/HTLC-Success/HTLC-Timeout transactions have one input, HTLCs
+                       // can also be resolved in a few other ways which can have more than one output. Thus,
+                       // we call is_resolving_htlc_output here outside of the tx.input.len() == 1 check.
+                       let mut updated = self.is_resolving_htlc_output(tx, height);
+                       if updated.len() > 0 {
+                               htlc_updated.append(&mut updated);
+                       }
+                       for inp in &tx.input {
+                               if self.our_claim_txn_waiting_first_conf.contains_key(&inp.previous_output) {
+                                       match self.onchain_events_waiting_threshold_conf.entry(height + ANTI_REORG_DELAY - 1) {
+                                               hash_map::Entry::Occupied(mut entry) => {
+                                                       let e = entry.get_mut();
+                                                       e.retain(|ref event| {
+                                                               match **event {
+                                                                       OnchainEvent::Claim { outpoint } => {
+                                                                               return outpoint != inp.previous_output
+                                                                       },
+                                                                       _ => return true
+                                                               }
+                                                       });
+                                                       e.push(OnchainEvent::Claim { outpoint: inp.previous_output.clone()});
+                                               }
+                                               hash_map::Entry::Vacant(entry) => {
+                                                       entry.insert(vec![OnchainEvent::Claim { outpoint: inp.previous_output.clone()}]);
+                                               }
+                                       }
+                               }
+                       }
+               }
+               let mut pending_claims = Vec::new();
+               if let Some(ref cur_local_tx) = self.current_local_signed_commitment_tx {
+                       if self.would_broadcast_at_height(height) {
+                               broadcaster.broadcast_transaction(&cur_local_tx.tx);
+                               match self.key_storage {
+                                       Storage::Local { ref delayed_payment_base_key, ref latest_per_commitment_point, .. } => {
+                                               let (txs, mut spendable_output, new_outputs, mut pending_txn) = self.broadcast_by_local_state(&cur_local_tx, latest_per_commitment_point, &Some(*delayed_payment_base_key), height);
+                                               spendable_outputs.append(&mut spendable_output);
+                                               pending_claims.append(&mut pending_txn);
+                                               if !new_outputs.is_empty() {
+                                                       watch_outputs.push((cur_local_tx.txid.clone(), new_outputs));
+                                               }
+                                               for tx in txs {
+                                                       broadcaster.broadcast_transaction(&tx);
+                                               }
+                                       },
+                                       Storage::Watchtower { .. } => {
+                                               let (txs, mut spendable_output, new_outputs, mut pending_txn) = self.broadcast_by_local_state(&cur_local_tx, &None, &None, height);
+                                               spendable_outputs.append(&mut spendable_output);
+                                               pending_claims.append(&mut pending_txn);
+                                               if !new_outputs.is_empty() {
+                                                       watch_outputs.push((cur_local_tx.txid.clone(), new_outputs));
+                                               }
+                                               for tx in txs {
+                                                       broadcaster.broadcast_transaction(&tx);
+                                               }
+                                       }
+                               }
+                       }
+               }
+               for claim in pending_claims {
+                       match self.our_claim_txn_waiting_first_conf.entry(claim.0) {
+                               hash_map::Entry::Occupied(_) => {},
+                               hash_map::Entry::Vacant(entry) => { entry.insert(claim.1); }
+                       }
+               }
+               if let Some(events) = self.onchain_events_waiting_threshold_conf.remove(&height) {
+                       for ev in events {
+                               match ev {
+                                       OnchainEvent::Claim { outpoint } => {
+                                               self.our_claim_txn_waiting_first_conf.remove(&outpoint);
+                                       },
+                                       OnchainEvent::HTLCUpdate { htlc_update } => {
+                                               log_trace!(self, "HTLC {} failure update has got enough confirmations to be passed upstream", log_bytes!((htlc_update.1).0));
+                                               htlc_updated.push((htlc_update.0, None, htlc_update.1));
+                                       },
+                               }
+                       }
+               }
+               //TODO: iter on buffered TxMaterial in our_claim_txn_waiting_first_conf, if block timer is expired generate a bumped claim tx (RBF or CPFP accordingly)
+               self.last_block_hash = block_hash.clone();
+               (watch_outputs, spendable_outputs, htlc_updated)
+       }
+
+       fn block_disconnected(&mut self, height: u32, block_hash: &Sha256dHash) {
+               if let Some(_) = self.onchain_events_waiting_threshold_conf.remove(&(height + ANTI_REORG_DELAY - 1)) {
+                       //We may discard:
+                       //- htlc update there as failure-trigger tx (revoked commitment tx, non-revoked commitment tx, HTLC-timeout tx) has been disconnected
+                       //- our claim tx on a commitment tx output
+               }
+               self.our_claim_txn_waiting_first_conf.retain(|_, ref mut v| if v.3 == height { false } else { true });
+               self.last_block_hash = block_hash.clone();
+       }
+
+       pub(super) fn would_broadcast_at_height(&self, height: u32) -> bool {
+               // We need to consider all HTLCs which are:
+               //  * in any unrevoked remote commitment transaction, as they could broadcast said
+               //    transactions and we'd end up in a race, or
+               //  * are in our latest local commitment transaction, as this is the thing we will
+               //    broadcast if we go on-chain.
+               // Note that we consider HTLCs which were below dust threshold here - while they don't
+               // strictly imply that we need to fail the channel, we need to go ahead and fail them back
+               // to the source, and if we don't fail the channel we will have to ensure that the next
+               // updates that peer sends us are update_fails, failing the channel if not. It's probably
+               // easier to just fail the channel as this case should be rare enough anyway.
+               macro_rules! scan_commitment {
+                       ($htlcs: expr, $local_tx: expr) => {
+                               for ref htlc in $htlcs {
+                                       // For inbound HTLCs which we know the preimage for, we have to ensure we hit the
+                                       // chain with enough room to claim the HTLC without our counterparty being able to
+                                       // time out the HTLC first.
+                                       // For outbound HTLCs which our counterparty hasn't failed/claimed, our primary
+                                       // concern is being able to claim the corresponding inbound HTLC (on another
+                                       // channel) before it expires. In fact, we don't even really care if our
+                                       // counterparty here claims such an outbound HTLC after it expired as long as we
+                                       // can still claim the corresponding HTLC. Thus, to avoid needlessly hitting the
+                                       // chain when our counterparty is waiting for expiration to off-chain fail an HTLC
+                                       // we give ourselves a few blocks of headroom after expiration before going
+                                       // on-chain for an expired HTLC.
+                                       // Note that, to avoid a potential attack whereby a node delays claiming an HTLC
+                                       // from us until we've reached the point where we go on-chain with the
+                                       // corresponding inbound HTLC, we must ensure that outbound HTLCs go on chain at
+                                       // least CLTV_CLAIM_BUFFER blocks prior to the inbound HTLC.
+                                       //  aka outbound_cltv + LATENCY_GRACE_PERIOD_BLOCKS == height - CLTV_CLAIM_BUFFER
+                                       //      inbound_cltv == height + CLTV_CLAIM_BUFFER
+                                       //      outbound_cltv + LATENCY_GRACE_PERIOD_BLOCKS + CLTV_CLAIM_BUFFER <= inbound_cltv - CLTV_CLAIM_BUFFER
+                                       //      LATENCY_GRACE_PERIOD_BLOCKS + 2*CLTV_CLAIM_BUFFER <= inbound_cltv - outbound_cltv
+                                       //      CLTV_EXPIRY_DELTA <= inbound_cltv - outbound_cltv (by check in ChannelManager::decode_update_add_htlc_onion)
+                                       //      LATENCY_GRACE_PERIOD_BLOCKS + 2*CLTV_CLAIM_BUFFER <= CLTV_EXPIRY_DELTA
+                                       //  The final, above, condition is checked for statically in channelmanager
+                                       //  with CHECK_CLTV_EXPIRY_SANITY_2.
+                                       let htlc_outbound = $local_tx == htlc.offered;
+                                       if ( htlc_outbound && htlc.cltv_expiry + LATENCY_GRACE_PERIOD_BLOCKS <= height) ||
+                                          (!htlc_outbound && htlc.cltv_expiry <= height + CLTV_CLAIM_BUFFER && self.payment_preimages.contains_key(&htlc.payment_hash)) {
+                                               log_info!(self, "Force-closing channel due to {} HTLC timeout, HTLC expiry is {}", if htlc_outbound { "outbound" } else { "inbound "}, htlc.cltv_expiry);
+                                               return true;
+                                       }
+                               }
+                       }
+               }
+
+               if let Some(ref cur_local_tx) = self.current_local_signed_commitment_tx {
+                       scan_commitment!(cur_local_tx.htlc_outputs.iter().map(|&(ref a, _, _)| a), true);
+               }
+
+               if let Storage::Local { ref current_remote_commitment_txid, ref prev_remote_commitment_txid, .. } = self.key_storage {
+                       if let &Some(ref txid) = current_remote_commitment_txid {
+                               if let Some(ref htlc_outputs) = self.remote_claimable_outpoints.get(txid) {
+                                       scan_commitment!(htlc_outputs.iter().map(|&(ref a, _)| a), false);
+                               }
+                       }
+                       if let &Some(ref txid) = prev_remote_commitment_txid {
+                               if let Some(ref htlc_outputs) = self.remote_claimable_outpoints.get(txid) {
+                                       scan_commitment!(htlc_outputs.iter().map(|&(ref a, _)| a), false);
+                               }
+                       }
+               }
+
+               false
+       }
+
+       /// Check if any transaction broadcasted is resolving HTLC output by a success or timeout on a local
+       /// or remote commitment tx, if so send back the source, preimage if found and payment_hash of resolved HTLC
+       fn is_resolving_htlc_output(&mut self, tx: &Transaction, height: u32) -> Vec<(HTLCSource, Option<PaymentPreimage>, PaymentHash)> {
+               let mut htlc_updated = Vec::new();
+
+               'outer_loop: for input in &tx.input {
+                       let mut payment_data = None;
+                       let revocation_sig_claim = (input.witness.len() == 3 && input.witness[2].len() == OFFERED_HTLC_SCRIPT_WEIGHT && input.witness[1].len() == 33)
+                               || (input.witness.len() == 3 && input.witness[2].len() == ACCEPTED_HTLC_SCRIPT_WEIGHT && input.witness[1].len() == 33);
+                       let accepted_preimage_claim = input.witness.len() == 5 && input.witness[4].len() == ACCEPTED_HTLC_SCRIPT_WEIGHT;
+                       let offered_preimage_claim = input.witness.len() == 3 && input.witness[2].len() == OFFERED_HTLC_SCRIPT_WEIGHT;
+
+                       macro_rules! log_claim {
+                               ($tx_info: expr, $local_tx: expr, $htlc: expr, $source_avail: expr) => {
+                                       // We found the output in question, but aren't failing it backwards
+                                       // as we have no corresponding source and no valid remote commitment txid
+                                       // to try a weak source binding with same-hash, same-value still-valid offered HTLC.
+                                       // This implies either it is an inbound HTLC or an outbound HTLC on a revoked transaction.
+                                       let outbound_htlc = $local_tx == $htlc.offered;
+                                       if ($local_tx && revocation_sig_claim) ||
+                                                       (outbound_htlc && !$source_avail && (accepted_preimage_claim || offered_preimage_claim)) {
+                                               log_error!(self, "Input spending {} ({}:{}) in {} resolves {} HTLC with payment hash {} with {}!",
+                                                       $tx_info, input.previous_output.txid, input.previous_output.vout, tx.txid(),
+                                                       if outbound_htlc { "outbound" } else { "inbound" }, log_bytes!($htlc.payment_hash.0),
+                                                       if revocation_sig_claim { "revocation sig" } else { "preimage claim after we'd passed the HTLC resolution back" });
+                                       } else {
+                                               log_info!(self, "Input spending {} ({}:{}) in {} resolves {} HTLC with payment hash {} with {}",
+                                                       $tx_info, input.previous_output.txid, input.previous_output.vout, tx.txid(),
+                                                       if outbound_htlc { "outbound" } else { "inbound" }, log_bytes!($htlc.payment_hash.0),
+                                                       if revocation_sig_claim { "revocation sig" } else if accepted_preimage_claim || offered_preimage_claim { "preimage" } else { "timeout" });
+                                       }
+                               }
+                       }
+
+                       macro_rules! check_htlc_valid_remote {
+                               ($remote_txid: expr, $htlc_output: expr) => {
+                                       if let &Some(txid) = $remote_txid {
+                                               for &(ref pending_htlc, ref pending_source) in self.remote_claimable_outpoints.get(&txid).unwrap() {
+                                                       if pending_htlc.payment_hash == $htlc_output.payment_hash && pending_htlc.amount_msat == $htlc_output.amount_msat {
+                                                               if let &Some(ref source) = pending_source {
+                                                                       log_claim!("revoked remote commitment tx", false, pending_htlc, true);
+                                                                       payment_data = Some(((**source).clone(), $htlc_output.payment_hash));
+                                                                       break;
+                                                               }
+                                                       }
+                                               }
+                                       }
+                               }
+                       }
+
+                       macro_rules! scan_commitment {
+                               ($htlcs: expr, $tx_info: expr, $local_tx: expr) => {
+                                       for (ref htlc_output, source_option) in $htlcs {
+                                               if Some(input.previous_output.vout) == htlc_output.transaction_output_index {
+                                                       if let Some(ref source) = source_option {
+                                                               log_claim!($tx_info, $local_tx, htlc_output, true);
+                                                               // We have a resolution of an HTLC either from one of our latest
+                                                               // local commitment transactions or an unrevoked remote commitment
+                                                               // transaction. This implies we either learned a preimage, the HTLC
+                                                               // has timed out, or we screwed up. In any case, we should now
+                                                               // resolve the source HTLC with the original sender.
+                                                               payment_data = Some(((*source).clone(), htlc_output.payment_hash));
+                                                       } else if !$local_tx {
+                                                               if let Storage::Local { ref current_remote_commitment_txid, .. } = self.key_storage {
+                                                                       check_htlc_valid_remote!(current_remote_commitment_txid, htlc_output);
+                                                               }
+                                                               if payment_data.is_none() {
+                                                                       if let Storage::Local { ref prev_remote_commitment_txid, .. } = self.key_storage {
+                                                                               check_htlc_valid_remote!(prev_remote_commitment_txid, htlc_output);
+                                                                       }
+                                                               }
+                                                       }
+                                                       if payment_data.is_none() {
+                                                               log_claim!($tx_info, $local_tx, htlc_output, false);
+                                                               continue 'outer_loop;
+                                                       }
+                                               }
+                                       }
+                               }
+                       }
+
+                       if let Some(ref current_local_signed_commitment_tx) = self.current_local_signed_commitment_tx {
+                               if input.previous_output.txid == current_local_signed_commitment_tx.txid {
+                                       scan_commitment!(current_local_signed_commitment_tx.htlc_outputs.iter().map(|&(ref a, _, ref b)| (a, b.as_ref())),
+                                               "our latest local commitment tx", true);
+                               }
+                       }
+                       if let Some(ref prev_local_signed_commitment_tx) = self.prev_local_signed_commitment_tx {
+                               if input.previous_output.txid == prev_local_signed_commitment_tx.txid {
+                                       scan_commitment!(prev_local_signed_commitment_tx.htlc_outputs.iter().map(|&(ref a, _, ref b)| (a, b.as_ref())),
+                                               "our previous local commitment tx", true);
+                               }
+                       }
+                       if let Some(ref htlc_outputs) = self.remote_claimable_outpoints.get(&input.previous_output.txid) {
+                               scan_commitment!(htlc_outputs.iter().map(|&(ref a, ref b)| (a, (b.as_ref().clone()).map(|boxed| &**boxed))),
+                                       "remote commitment tx", false);
+                       }
+
+                       // Check that scan_commitment, above, decided there is some source worth relaying an
+                       // HTLC resolution backwards to and figure out whether we learned a preimage from it.
+                       if let Some((source, payment_hash)) = payment_data {
+                               let mut payment_preimage = PaymentPreimage([0; 32]);
+                               if accepted_preimage_claim {
+                                       payment_preimage.0.copy_from_slice(&input.witness[3]);
+                                       htlc_updated.push((source, Some(payment_preimage), payment_hash));
+                               } else if offered_preimage_claim {
+                                       payment_preimage.0.copy_from_slice(&input.witness[1]);
+                                       htlc_updated.push((source, Some(payment_preimage), payment_hash));
+                               } else {
+                                       log_info!(self, "Failing HTLC with payment_hash {} timeout by a spend tx, waiting for confirmation (at height{})", log_bytes!(payment_hash.0), height + ANTI_REORG_DELAY - 1);
+                                       match self.onchain_events_waiting_threshold_conf.entry(height + ANTI_REORG_DELAY - 1) {
+                                               hash_map::Entry::Occupied(mut entry) => {
+                                                       let e = entry.get_mut();
+                                                       e.retain(|ref event| {
+                                                               match **event {
+                                                                       OnchainEvent::HTLCUpdate { ref htlc_update } => {
+                                                                               return htlc_update.0 != source
+                                                                       },
+                                                                       _ => return true
+                                                               }
+                                                       });
+                                                       e.push(OnchainEvent::HTLCUpdate { htlc_update: (source, payment_hash)});
+                                               }
+                                               hash_map::Entry::Vacant(entry) => {
+                                                       entry.insert(vec![OnchainEvent::HTLCUpdate { htlc_update: (source, payment_hash)}]);
+                                               }
+                                       }
+                               }
+                       }
+               }
+               htlc_updated
+       }
+}
+
+const MAX_ALLOC_SIZE: usize = 64*1024;
+
+impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelMonitor) {
+       fn read(reader: &mut R, logger: Arc<Logger>) -> Result<Self, DecodeError> {
+               let secp_ctx = Secp256k1::new();
+               macro_rules! unwrap_obj {
+                       ($key: expr) => {
+                               match $key {
+                                       Ok(res) => res,
+                                       Err(_) => return Err(DecodeError::InvalidValue),
+                               }
+                       }
+               }
+
+               let _ver: u8 = Readable::read(reader)?;
+               let min_ver: u8 = Readable::read(reader)?;
+               if min_ver > SERIALIZATION_VERSION {
+                       return Err(DecodeError::UnknownVersion);
+               }
+
+               let commitment_transaction_number_obscure_factor = <U48 as Readable<R>>::read(reader)?.0;
+
+               let key_storage = match <u8 as Readable<R>>::read(reader)? {
+                       0 => {
+                               let revocation_base_key = Readable::read(reader)?;
+                               let htlc_base_key = Readable::read(reader)?;
+                               let delayed_payment_base_key = Readable::read(reader)?;
+                               let payment_base_key = Readable::read(reader)?;
+                               let shutdown_pubkey = Readable::read(reader)?;
+                               let prev_latest_per_commitment_point = Readable::read(reader)?;
+                               let latest_per_commitment_point = Readable::read(reader)?;
+                               // Technically this can fail and serialize fail a round-trip, but only for serialization of
+                               // barely-init'd ChannelMonitors that we can't do anything with.
+                               let outpoint = OutPoint {
+                                       txid: Readable::read(reader)?,
+                                       index: Readable::read(reader)?,
+                               };
+                               let funding_info = Some((outpoint, Readable::read(reader)?));
+                               let current_remote_commitment_txid = Readable::read(reader)?;
+                               let prev_remote_commitment_txid = Readable::read(reader)?;
+                               Storage::Local {
+                                       revocation_base_key,
+                                       htlc_base_key,
+                                       delayed_payment_base_key,
+                                       payment_base_key,
+                                       shutdown_pubkey,
+                                       prev_latest_per_commitment_point,
+                                       latest_per_commitment_point,
+                                       funding_info,
+                                       current_remote_commitment_txid,
+                                       prev_remote_commitment_txid,
+                               }
+                       },
+                       _ => return Err(DecodeError::InvalidValue),
+               };
+
+               let their_htlc_base_key = Some(Readable::read(reader)?);
+               let their_delayed_payment_base_key = Some(Readable::read(reader)?);
+
+               let their_cur_revocation_points = {
+                       let first_idx = <U48 as Readable<R>>::read(reader)?.0;
+                       if first_idx == 0 {
+                               None
+                       } else {
+                               let first_point = Readable::read(reader)?;
+                               let second_point_slice: [u8; 33] = Readable::read(reader)?;
+                               if second_point_slice[0..32] == [0; 32] && second_point_slice[32] == 0 {
+                                       Some((first_idx, first_point, None))
+                               } else {
+                                       Some((first_idx, first_point, Some(unwrap_obj!(PublicKey::from_slice(&second_point_slice)))))
+                               }
+                       }
+               };
+
+               let our_to_self_delay: u16 = Readable::read(reader)?;
+               let their_to_self_delay: Option<u16> = Some(Readable::read(reader)?);
+
+               let mut old_secrets = [([0; 32], 1 << 48); 49];
+               for &mut (ref mut secret, ref mut idx) in old_secrets.iter_mut() {
+                       *secret = Readable::read(reader)?;
+                       *idx = Readable::read(reader)?;
+               }
+
+               macro_rules! read_htlc_in_commitment {
+                       () => {
+                               {
+                                       let offered: bool = Readable::read(reader)?;
+                                       let amount_msat: u64 = Readable::read(reader)?;
+                                       let cltv_expiry: u32 = Readable::read(reader)?;
+                                       let payment_hash: PaymentHash = Readable::read(reader)?;
+                                       let transaction_output_index: Option<u32> = Readable::read(reader)?;
+
+                                       HTLCOutputInCommitment {
+                                               offered, amount_msat, cltv_expiry, payment_hash, transaction_output_index
+                                       }
+                               }
+                       }
+               }
+
+               let remote_claimable_outpoints_len: u64 = Readable::read(reader)?;
+               let mut remote_claimable_outpoints = HashMap::with_capacity(cmp::min(remote_claimable_outpoints_len as usize, MAX_ALLOC_SIZE / 64));
+               for _ in 0..remote_claimable_outpoints_len {
+                       let txid: Sha256dHash = Readable::read(reader)?;
+                       let htlcs_count: u64 = Readable::read(reader)?;
+                       let mut htlcs = Vec::with_capacity(cmp::min(htlcs_count as usize, MAX_ALLOC_SIZE / 32));
+                       for _ in 0..htlcs_count {
+                               htlcs.push((read_htlc_in_commitment!(), <Option<HTLCSource> as Readable<R>>::read(reader)?.map(|o: HTLCSource| Box::new(o))));
+                       }
+                       if let Some(_) = remote_claimable_outpoints.insert(txid, htlcs) {
+                               return Err(DecodeError::InvalidValue);
+                       }
+               }
+
+               let remote_commitment_txn_on_chain_len: u64 = Readable::read(reader)?;
+               let mut remote_commitment_txn_on_chain = HashMap::with_capacity(cmp::min(remote_commitment_txn_on_chain_len as usize, MAX_ALLOC_SIZE / 32));
+               for _ in 0..remote_commitment_txn_on_chain_len {
+                       let txid: Sha256dHash = Readable::read(reader)?;
+                       let commitment_number = <U48 as Readable<R>>::read(reader)?.0;
+                       let outputs_count = <u64 as Readable<R>>::read(reader)?;
+                       let mut outputs = Vec::with_capacity(cmp::min(outputs_count as usize, MAX_ALLOC_SIZE / 8));
+                       for _ in 0..outputs_count {
+                               outputs.push(Readable::read(reader)?);
+                       }
+                       if let Some(_) = remote_commitment_txn_on_chain.insert(txid, (commitment_number, outputs)) {
+                               return Err(DecodeError::InvalidValue);
+                       }
+               }
+
+               let remote_hash_commitment_number_len: u64 = Readable::read(reader)?;
+               let mut remote_hash_commitment_number = HashMap::with_capacity(cmp::min(remote_hash_commitment_number_len as usize, MAX_ALLOC_SIZE / 32));
+               for _ in 0..remote_hash_commitment_number_len {
+                       let payment_hash: PaymentHash = Readable::read(reader)?;
+                       let commitment_number = <U48 as Readable<R>>::read(reader)?.0;
+                       if let Some(_) = remote_hash_commitment_number.insert(payment_hash, commitment_number) {
+                               return Err(DecodeError::InvalidValue);
+                       }
+               }
+
+               macro_rules! read_local_tx {
+                       () => {
+                               {
+                                       let tx = match Transaction::consensus_decode(reader.by_ref()) {
+                                               Ok(tx) => tx,
+                                               Err(e) => match e {
+                                                       encode::Error::Io(ioe) => return Err(DecodeError::Io(ioe)),
+                                                       _ => return Err(DecodeError::InvalidValue),
+                                               },
+                                       };
+
+                                       if tx.input.is_empty() {
+                                               // Ensure tx didn't hit the 0-input ambiguity case.
+                                               return Err(DecodeError::InvalidValue);
+                                       }
+
+                                       let revocation_key = Readable::read(reader)?;
+                                       let a_htlc_key = Readable::read(reader)?;
+                                       let b_htlc_key = Readable::read(reader)?;
+                                       let delayed_payment_key = Readable::read(reader)?;
+                                       let feerate_per_kw: u64 = Readable::read(reader)?;
+
+                                       let htlcs_len: u64 = Readable::read(reader)?;
+                                       let mut htlcs = Vec::with_capacity(cmp::min(htlcs_len as usize, MAX_ALLOC_SIZE / 128));
+                                       for _ in 0..htlcs_len {
+                                               let htlc = read_htlc_in_commitment!();
+                                               let sigs = match <u8 as Readable<R>>::read(reader)? {
+                                                       0 => None,
+                                                       1 => Some((Readable::read(reader)?, Readable::read(reader)?)),
+                                                       _ => return Err(DecodeError::InvalidValue),
+                                               };
+                                               htlcs.push((htlc, sigs, Readable::read(reader)?));
+                                       }
+
+                                       LocalSignedTx {
+                                               txid: tx.txid(),
+                                               tx, revocation_key, a_htlc_key, b_htlc_key, delayed_payment_key, feerate_per_kw,
+                                               htlc_outputs: htlcs
+                                       }
+                               }
+                       }
+               }
+
+               let prev_local_signed_commitment_tx = match <u8 as Readable<R>>::read(reader)? {
+                       0 => None,
+                       1 => {
+                               Some(read_local_tx!())
+                       },
+                       _ => return Err(DecodeError::InvalidValue),
+               };
+
+               let current_local_signed_commitment_tx = match <u8 as Readable<R>>::read(reader)? {
+                       0 => None,
+                       1 => {
+                               Some(read_local_tx!())
+                       },
+                       _ => return Err(DecodeError::InvalidValue),
+               };
+
+               let current_remote_commitment_number = <U48 as Readable<R>>::read(reader)?.0;
+
+               let payment_preimages_len: u64 = Readable::read(reader)?;
+               let mut payment_preimages = HashMap::with_capacity(cmp::min(payment_preimages_len as usize, MAX_ALLOC_SIZE / 32));
+               for _ in 0..payment_preimages_len {
+                       let preimage: PaymentPreimage = Readable::read(reader)?;
+                       let hash = PaymentHash(Sha256::hash(&preimage.0[..]).into_inner());
+                       if let Some(_) = payment_preimages.insert(hash, preimage) {
+                               return Err(DecodeError::InvalidValue);
+                       }
+               }
+
+               let last_block_hash: Sha256dHash = Readable::read(reader)?;
+               let destination_script = Readable::read(reader)?;
+               let to_remote_rescue = match <u8 as Readable<R>>::read(reader)? {
+                       0 => None,
+                       1 => {
+                               let to_remote_script = Readable::read(reader)?;
+                               let local_key = Readable::read(reader)?;
+                               Some((to_remote_script, local_key))
+                       }
+                       _ => return Err(DecodeError::InvalidValue),
+               };
+
+               let our_claim_txn_waiting_first_conf_len: u64 = Readable::read(reader)?;
+               let mut our_claim_txn_waiting_first_conf = HashMap::with_capacity(cmp::min(our_claim_txn_waiting_first_conf_len as usize, MAX_ALLOC_SIZE / 128));
+               for _ in 0..our_claim_txn_waiting_first_conf_len {
+                       let outpoint = Readable::read(reader)?;
+                       let height_target = Readable::read(reader)?;
+                       let tx_material = match <u8 as Readable<R>>::read(reader)? {
+                               0 => {
+                                       let script = Readable::read(reader)?;
+                                       let pubkey = Readable::read(reader)?;
+                                       let key = Readable::read(reader)?;
+                                       let is_htlc = match <u8 as Readable<R>>::read(reader)? {
+                                               0 => true,
+                                               1 => false,
+                                               _ => return Err(DecodeError::InvalidValue),
+                                       };
+                                       let amount = Readable::read(reader)?;
+                                       TxMaterial::Revoked {
+                                               script,
+                                               pubkey,
+                                               key,
+                                               is_htlc,
+                                               amount
+                                       }
+                               },
+                               1 => {
+                                       let script = Readable::read(reader)?;
+                                       let key = Readable::read(reader)?;
+                                       let preimage = Readable::read(reader)?;
+                                       let amount = Readable::read(reader)?;
+                                       TxMaterial::RemoteHTLC {
+                                               script,
+                                               key,
+                                               preimage,
+                                               amount
+                                       }
+                               },
+                               2 => {
+                                       let script = Readable::read(reader)?;
+                                       let their_sig = Readable::read(reader)?;
+                                       let our_sig = Readable::read(reader)?;
+                                       let preimage = Readable::read(reader)?;
+                                       let amount = Readable::read(reader)?;
+                                       TxMaterial::LocalHTLC {
+                                               script,
+                                               sigs: (their_sig, our_sig),
+                                               preimage,
+                                               amount
+                                       }
+                               }
+                               _ => return Err(DecodeError::InvalidValue),
+                       };
+                       let last_fee = Readable::read(reader)?;
+                       let timelock_expiration = Readable::read(reader)?;
+                       let height = Readable::read(reader)?;
+                       our_claim_txn_waiting_first_conf.insert(outpoint, (height_target, tx_material, last_fee, timelock_expiration, height));
+               }
+
+               let waiting_threshold_conf_len: u64 = Readable::read(reader)?;
+               let mut onchain_events_waiting_threshold_conf = HashMap::with_capacity(cmp::min(waiting_threshold_conf_len as usize, MAX_ALLOC_SIZE / 128));
+               for _ in 0..waiting_threshold_conf_len {
+                       let height_target = Readable::read(reader)?;
+                       let events_len: u64 = Readable::read(reader)?;
+                       let mut events = Vec::with_capacity(cmp::min(events_len as usize, MAX_ALLOC_SIZE / 128));
+                       for _ in 0..events_len {
+                               let ev = match <u8 as Readable<R>>::read(reader)? {
+                                       0 => {
+                                               let outpoint = Readable::read(reader)?;
+                                               OnchainEvent::Claim {
+                                                       outpoint
+                                               }
+                                       },
+                                       1 => {
+                                               let htlc_source = Readable::read(reader)?;
+                                               let hash = Readable::read(reader)?;
+                                               OnchainEvent::HTLCUpdate {
+                                                       htlc_update: (htlc_source, hash)
+                                               }
+                                       },
+                                       _ => return Err(DecodeError::InvalidValue),
+                               };
+                               events.push(ev);
+                       }
+                       onchain_events_waiting_threshold_conf.insert(height_target, events);
+               }
+
+               Ok((last_block_hash.clone(), ChannelMonitor {
+                       commitment_transaction_number_obscure_factor,
+
+                       key_storage,
+                       their_htlc_base_key,
+                       their_delayed_payment_base_key,
+                       their_cur_revocation_points,
+
+                       our_to_self_delay,
+                       their_to_self_delay,
+
+                       old_secrets,
+                       remote_claimable_outpoints,
+                       remote_commitment_txn_on_chain,
+                       remote_hash_commitment_number,
+
+                       prev_local_signed_commitment_tx,
+                       current_local_signed_commitment_tx,
+                       current_remote_commitment_number,
+
+                       payment_preimages,
+
+                       destination_script,
+                       to_remote_rescue,
+
+                       our_claim_txn_waiting_first_conf,
+
+                       onchain_events_waiting_threshold_conf,
+
+                       last_block_hash,
+                       secp_ctx,
+                       logger,
+               }))
+       }
+
+}
+
+#[cfg(test)]
+mod tests {
+       use bitcoin::blockdata::script::{Script, Builder};
+       use bitcoin::blockdata::opcodes;
+       use bitcoin::blockdata::transaction::{Transaction, TxIn, TxOut, SigHashType};
+       use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
+       use bitcoin::util::bip143;
+       use bitcoin_hashes::Hash;
+       use bitcoin_hashes::sha256::Hash as Sha256;
+       use bitcoin_hashes::sha256d::Hash as Sha256dHash;
+       use bitcoin_hashes::hex::FromHex;
+       use hex;
+       use ln::channelmanager::{PaymentPreimage, PaymentHash};
+       use ln::channelmonitor::{ChannelMonitor, InputDescriptors};
+       use ln::chan_utils;
+       use ln::chan_utils::{HTLCOutputInCommitment, TxCreationKeys};
+       use util::test_utils::TestLogger;
+       use secp256k1::key::{SecretKey,PublicKey};
+       use secp256k1::Secp256k1;
+       use rand::{thread_rng,Rng};
+       use std::sync::Arc;
+
+       #[test]
+       fn test_per_commitment_storage() {
+               // Test vectors from BOLT 3:
+               let mut secrets: Vec<[u8; 32]> = Vec::new();
+               let mut monitor: ChannelMonitor;
+               let secp_ctx = Secp256k1::new();
+               let logger = Arc::new(TestLogger::new());
+
+               macro_rules! test_secrets {
+                       () => {
+                               let mut idx = 281474976710655;
+                               for secret in secrets.iter() {
+                                       assert_eq!(monitor.get_secret(idx).unwrap(), *secret);
+                                       idx -= 1;
+                               }
+                               assert_eq!(monitor.get_min_seen_secret(), idx + 1);
+                               assert!(monitor.get_secret(idx).is_none());
+                       };
+               }
+
+               {
+                       // insert_secret correct sequence
+                       monitor = ChannelMonitor::new(&SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
+                       secrets.clear();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("7cc854b54e3e0dcdb010d7a3fee464a9687be6e8db3be6854c475621e007a5dc").unwrap());
+                       monitor.provide_secret(281474976710655, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c7518c8ae4660ed02894df8976fa1a3659c1a8b4b5bec0c4b872abeba4cb8964").unwrap());
+                       monitor.provide_secret(281474976710654, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("2273e227a5b7449b6e70f1fb4652864038b1cbf9cd7c043a7d6456b7fc275ad8").unwrap());
+                       monitor.provide_secret(281474976710653, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116").unwrap());
+                       monitor.provide_secret(281474976710652, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c65716add7aa98ba7acb236352d665cab17345fe45b55fb879ff80e6bd0c41dd").unwrap());
+                       monitor.provide_secret(281474976710651, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("969660042a28f32d9be17344e09374b379962d03db1574df5a8a5a47e19ce3f2").unwrap());
+                       monitor.provide_secret(281474976710650, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("a5a64476122ca0925fb344bdc1854c1c0a59fc614298e50a33e331980a220f32").unwrap());
+                       monitor.provide_secret(281474976710649, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("05cde6323d949933f7f7b78776bcc1ea6d9b31447732e3802e1f7ac44b650e17").unwrap());
+                       monitor.provide_secret(281474976710648, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+               }
+
+               {
+                       // insert_secret #1 incorrect
+                       monitor = ChannelMonitor::new(&SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
+                       secrets.clear();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("02a40c85b6f28da08dfdbe0926c53fab2de6d28c10301f8f7c4073d5e42e3148").unwrap());
+                       monitor.provide_secret(281474976710655, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c7518c8ae4660ed02894df8976fa1a3659c1a8b4b5bec0c4b872abeba4cb8964").unwrap());
+                       assert_eq!(monitor.provide_secret(281474976710654, secrets.last().unwrap().clone()).unwrap_err().0,
+                                       "Previous secret did not match new one");
+               }
+
+               {
+                       // insert_secret #2 incorrect (#1 derived from incorrect)
+                       monitor = ChannelMonitor::new(&SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
+                       secrets.clear();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("02a40c85b6f28da08dfdbe0926c53fab2de6d28c10301f8f7c4073d5e42e3148").unwrap());
+                       monitor.provide_secret(281474976710655, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("dddc3a8d14fddf2b68fa8c7fbad2748274937479dd0f8930d5ebb4ab6bd866a3").unwrap());
+                       monitor.provide_secret(281474976710654, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("2273e227a5b7449b6e70f1fb4652864038b1cbf9cd7c043a7d6456b7fc275ad8").unwrap());
+                       monitor.provide_secret(281474976710653, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116").unwrap());
+                       assert_eq!(monitor.provide_secret(281474976710652, secrets.last().unwrap().clone()).unwrap_err().0,
+                                       "Previous secret did not match new one");
+               }
+
+               {
+                       // insert_secret #3 incorrect
+                       monitor = ChannelMonitor::new(&SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
+                       secrets.clear();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("7cc854b54e3e0dcdb010d7a3fee464a9687be6e8db3be6854c475621e007a5dc").unwrap());
+                       monitor.provide_secret(281474976710655, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c7518c8ae4660ed02894df8976fa1a3659c1a8b4b5bec0c4b872abeba4cb8964").unwrap());
+                       monitor.provide_secret(281474976710654, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c51a18b13e8527e579ec56365482c62f180b7d5760b46e9477dae59e87ed423a").unwrap());
+                       monitor.provide_secret(281474976710653, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116").unwrap());
+                       assert_eq!(monitor.provide_secret(281474976710652, secrets.last().unwrap().clone()).unwrap_err().0,
+                                       "Previous secret did not match new one");
+               }
+
+               {
+                       // insert_secret #4 incorrect (1,2,3 derived from incorrect)
+                       monitor = ChannelMonitor::new(&SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
+                       secrets.clear();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("02a40c85b6f28da08dfdbe0926c53fab2de6d28c10301f8f7c4073d5e42e3148").unwrap());
+                       monitor.provide_secret(281474976710655, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("dddc3a8d14fddf2b68fa8c7fbad2748274937479dd0f8930d5ebb4ab6bd866a3").unwrap());
+                       monitor.provide_secret(281474976710654, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c51a18b13e8527e579ec56365482c62f180b7d5760b46e9477dae59e87ed423a").unwrap());
+                       monitor.provide_secret(281474976710653, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("ba65d7b0ef55a3ba300d4e87af29868f394f8f138d78a7011669c79b37b936f4").unwrap());
+                       monitor.provide_secret(281474976710652, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c65716add7aa98ba7acb236352d665cab17345fe45b55fb879ff80e6bd0c41dd").unwrap());
+                       monitor.provide_secret(281474976710651, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("969660042a28f32d9be17344e09374b379962d03db1574df5a8a5a47e19ce3f2").unwrap());
+                       monitor.provide_secret(281474976710650, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("a5a64476122ca0925fb344bdc1854c1c0a59fc614298e50a33e331980a220f32").unwrap());
+                       monitor.provide_secret(281474976710649, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("05cde6323d949933f7f7b78776bcc1ea6d9b31447732e3802e1f7ac44b650e17").unwrap());
+                       assert_eq!(monitor.provide_secret(281474976710648, secrets.last().unwrap().clone()).unwrap_err().0,
+                                       "Previous secret did not match new one");
+               }
+
+               {
+                       // insert_secret #5 incorrect
+                       monitor = ChannelMonitor::new(&SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
+                       secrets.clear();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("7cc854b54e3e0dcdb010d7a3fee464a9687be6e8db3be6854c475621e007a5dc").unwrap());
+                       monitor.provide_secret(281474976710655, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c7518c8ae4660ed02894df8976fa1a3659c1a8b4b5bec0c4b872abeba4cb8964").unwrap());
+                       monitor.provide_secret(281474976710654, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("2273e227a5b7449b6e70f1fb4652864038b1cbf9cd7c043a7d6456b7fc275ad8").unwrap());
+                       monitor.provide_secret(281474976710653, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116").unwrap());
+                       monitor.provide_secret(281474976710652, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("631373ad5f9ef654bb3dade742d09504c567edd24320d2fcd68e3cc47e2ff6a6").unwrap());
+                       monitor.provide_secret(281474976710651, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("969660042a28f32d9be17344e09374b379962d03db1574df5a8a5a47e19ce3f2").unwrap());
+                       assert_eq!(monitor.provide_secret(281474976710650, secrets.last().unwrap().clone()).unwrap_err().0,
+                                       "Previous secret did not match new one");
+               }
+
+               {
+                       // insert_secret #6 incorrect (5 derived from incorrect)
+                       monitor = ChannelMonitor::new(&SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
+                       secrets.clear();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("7cc854b54e3e0dcdb010d7a3fee464a9687be6e8db3be6854c475621e007a5dc").unwrap());
+                       monitor.provide_secret(281474976710655, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c7518c8ae4660ed02894df8976fa1a3659c1a8b4b5bec0c4b872abeba4cb8964").unwrap());
+                       monitor.provide_secret(281474976710654, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("2273e227a5b7449b6e70f1fb4652864038b1cbf9cd7c043a7d6456b7fc275ad8").unwrap());
+                       monitor.provide_secret(281474976710653, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116").unwrap());
+                       monitor.provide_secret(281474976710652, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("631373ad5f9ef654bb3dade742d09504c567edd24320d2fcd68e3cc47e2ff6a6").unwrap());
+                       monitor.provide_secret(281474976710651, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("b7e76a83668bde38b373970155c868a653304308f9896692f904a23731224bb1").unwrap());
+                       monitor.provide_secret(281474976710650, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("a5a64476122ca0925fb344bdc1854c1c0a59fc614298e50a33e331980a220f32").unwrap());
+                       monitor.provide_secret(281474976710649, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("05cde6323d949933f7f7b78776bcc1ea6d9b31447732e3802e1f7ac44b650e17").unwrap());
+                       assert_eq!(monitor.provide_secret(281474976710648, secrets.last().unwrap().clone()).unwrap_err().0,
+                                       "Previous secret did not match new one");
+               }
+
+               {
+                       // insert_secret #7 incorrect
+                       monitor = ChannelMonitor::new(&SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
+                       secrets.clear();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("7cc854b54e3e0dcdb010d7a3fee464a9687be6e8db3be6854c475621e007a5dc").unwrap());
+                       monitor.provide_secret(281474976710655, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c7518c8ae4660ed02894df8976fa1a3659c1a8b4b5bec0c4b872abeba4cb8964").unwrap());
+                       monitor.provide_secret(281474976710654, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("2273e227a5b7449b6e70f1fb4652864038b1cbf9cd7c043a7d6456b7fc275ad8").unwrap());
+                       monitor.provide_secret(281474976710653, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116").unwrap());
+                       monitor.provide_secret(281474976710652, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c65716add7aa98ba7acb236352d665cab17345fe45b55fb879ff80e6bd0c41dd").unwrap());
+                       monitor.provide_secret(281474976710651, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("969660042a28f32d9be17344e09374b379962d03db1574df5a8a5a47e19ce3f2").unwrap());
+                       monitor.provide_secret(281474976710650, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("e7971de736e01da8ed58b94c2fc216cb1dca9e326f3a96e7194fe8ea8af6c0a3").unwrap());
+                       monitor.provide_secret(281474976710649, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("05cde6323d949933f7f7b78776bcc1ea6d9b31447732e3802e1f7ac44b650e17").unwrap());
+                       assert_eq!(monitor.provide_secret(281474976710648, secrets.last().unwrap().clone()).unwrap_err().0,
+                                       "Previous secret did not match new one");
+               }
+
+               {
+                       // insert_secret #8 incorrect
+                       monitor = ChannelMonitor::new(&SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
+                       secrets.clear();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("7cc854b54e3e0dcdb010d7a3fee464a9687be6e8db3be6854c475621e007a5dc").unwrap());
+                       monitor.provide_secret(281474976710655, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c7518c8ae4660ed02894df8976fa1a3659c1a8b4b5bec0c4b872abeba4cb8964").unwrap());
+                       monitor.provide_secret(281474976710654, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("2273e227a5b7449b6e70f1fb4652864038b1cbf9cd7c043a7d6456b7fc275ad8").unwrap());
+                       monitor.provide_secret(281474976710653, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116").unwrap());
+                       monitor.provide_secret(281474976710652, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c65716add7aa98ba7acb236352d665cab17345fe45b55fb879ff80e6bd0c41dd").unwrap());
+                       monitor.provide_secret(281474976710651, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("969660042a28f32d9be17344e09374b379962d03db1574df5a8a5a47e19ce3f2").unwrap());
+                       monitor.provide_secret(281474976710650, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("a5a64476122ca0925fb344bdc1854c1c0a59fc614298e50a33e331980a220f32").unwrap());
+                       monitor.provide_secret(281474976710649, secrets.last().unwrap().clone()).unwrap();
+                       test_secrets!();
+
+                       secrets.push([0; 32]);
+                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("a7efbc61aac46d34f77778bac22c8a20c6a46ca460addc49009bda875ec88fa4").unwrap());
+                       assert_eq!(monitor.provide_secret(281474976710648, secrets.last().unwrap().clone()).unwrap_err().0,
+                                       "Previous secret did not match new one");
+               }
+       }
+
+       #[test]
+       fn test_prune_preimages() {
+               let secp_ctx = Secp256k1::new();
+               let logger = Arc::new(TestLogger::new());
+
+               let dummy_key = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
+               macro_rules! dummy_keys {
+                       () => {
+                               {
+                                       TxCreationKeys {
+                                               per_commitment_point: dummy_key.clone(),
+                                               revocation_key: dummy_key.clone(),
+                                               a_htlc_key: dummy_key.clone(),
+                                               b_htlc_key: dummy_key.clone(),
+                                               a_delayed_payment_key: dummy_key.clone(),
+                                               b_payment_key: dummy_key.clone(),
+                                       }
+                               }
+                       }
+               }
+               let dummy_tx = Transaction { version: 0, lock_time: 0, input: Vec::new(), output: Vec::new() };
+
+               let mut preimages = Vec::new();
+               {
+                       let mut rng  = thread_rng();
+                       for _ in 0..20 {
+                               let mut preimage = PaymentPreimage([0; 32]);
+                               rng.fill_bytes(&mut preimage.0[..]);
+                               let hash = PaymentHash(Sha256::hash(&preimage.0[..]).into_inner());
+                               preimages.push((preimage, hash));
+                       }
+               }
+
+               macro_rules! preimages_slice_to_htlc_outputs {
+                       ($preimages_slice: expr) => {
+                               {
+                                       let mut res = Vec::new();
+                                       for (idx, preimage) in $preimages_slice.iter().enumerate() {
+                                               res.push((HTLCOutputInCommitment {
+                                                       offered: true,
+                                                       amount_msat: 0,
+                                                       cltv_expiry: 0,
+                                                       payment_hash: preimage.1.clone(),
+                                                       transaction_output_index: Some(idx as u32),
+                                               }, None));
+                                       }
+                                       res
+                               }
+                       }
+               }
+               macro_rules! preimages_to_local_htlcs {
+                       ($preimages_slice: expr) => {
+                               {
+                                       let mut inp = preimages_slice_to_htlc_outputs!($preimages_slice);
+                                       let res: Vec<_> = inp.drain(..).map(|e| { (e.0, None, e.1) }).collect();
+                                       res
+                               }
+                       }
+               }
+
+               macro_rules! test_preimages_exist {
+                       ($preimages_slice: expr, $monitor: expr) => {
+                               for preimage in $preimages_slice {
+                                       assert!($monitor.payment_preimages.contains_key(&preimage.1));
+                               }
+                       }
+               }
+
+               // Prune with one old state and a local commitment tx holding a few overlaps with the
+               // old state.
+               let mut monitor = ChannelMonitor::new(&SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
+               monitor.set_their_to_self_delay(10);
+
+               monitor.provide_latest_local_commitment_tx_info(dummy_tx.clone(), dummy_keys!(), 0, preimages_to_local_htlcs!(preimages[0..10]));
+               monitor.provide_latest_remote_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[5..15]), 281474976710655, dummy_key);
+               monitor.provide_latest_remote_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[15..20]), 281474976710654, dummy_key);
+               monitor.provide_latest_remote_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[17..20]), 281474976710653, dummy_key);
+               monitor.provide_latest_remote_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[18..20]), 281474976710652, dummy_key);
+               for &(ref preimage, ref hash) in preimages.iter() {
+                       monitor.provide_payment_preimage(hash, preimage);
+               }
+
+               // Now provide a secret, pruning preimages 10-15
+               let mut secret = [0; 32];
+               secret[0..32].clone_from_slice(&hex::decode("7cc854b54e3e0dcdb010d7a3fee464a9687be6e8db3be6854c475621e007a5dc").unwrap());
+               monitor.provide_secret(281474976710655, secret.clone()).unwrap();
+               assert_eq!(monitor.payment_preimages.len(), 15);
+               test_preimages_exist!(&preimages[0..10], monitor);
+               test_preimages_exist!(&preimages[15..20], monitor);
+
+               // Now provide a further secret, pruning preimages 15-17
+               secret[0..32].clone_from_slice(&hex::decode("c7518c8ae4660ed02894df8976fa1a3659c1a8b4b5bec0c4b872abeba4cb8964").unwrap());
+               monitor.provide_secret(281474976710654, secret.clone()).unwrap();
+               assert_eq!(monitor.payment_preimages.len(), 13);
+               test_preimages_exist!(&preimages[0..10], monitor);
+               test_preimages_exist!(&preimages[17..20], monitor);
+
+               // Now update local commitment tx info, pruning only element 18 as we still care about the
+               // previous commitment tx's preimages too
+               monitor.provide_latest_local_commitment_tx_info(dummy_tx.clone(), dummy_keys!(), 0, preimages_to_local_htlcs!(preimages[0..5]));
+               secret[0..32].clone_from_slice(&hex::decode("2273e227a5b7449b6e70f1fb4652864038b1cbf9cd7c043a7d6456b7fc275ad8").unwrap());
+               monitor.provide_secret(281474976710653, secret.clone()).unwrap();
+               assert_eq!(monitor.payment_preimages.len(), 12);
+               test_preimages_exist!(&preimages[0..10], monitor);
+               test_preimages_exist!(&preimages[18..20], monitor);
+
+               // But if we do it again, we'll prune 5-10
+               monitor.provide_latest_local_commitment_tx_info(dummy_tx.clone(), dummy_keys!(), 0, preimages_to_local_htlcs!(preimages[0..3]));
+               secret[0..32].clone_from_slice(&hex::decode("27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116").unwrap());
+               monitor.provide_secret(281474976710652, secret.clone()).unwrap();
+               assert_eq!(monitor.payment_preimages.len(), 5);
+               test_preimages_exist!(&preimages[0..5], monitor);
+       }
+
+       #[test]
+       fn test_claim_txn_weight_computation() {
+               // We test Claim txn weight, knowing that we want expected weigth and
+               // not actual case to avoid sigs and time-lock delays hell variances.
+
+               let secp_ctx = Secp256k1::new();
+               let privkey = SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap();
+               let pubkey = PublicKey::from_secret_key(&secp_ctx, &privkey);
+               let mut sum_actual_sigs = 0;
+
+               macro_rules! sign_input {
+                       ($sighash_parts: expr, $input: expr, $idx: expr, $amount: expr, $input_type: expr, $sum_actual_sigs: expr) => {
+                               let htlc = HTLCOutputInCommitment {
+                                       offered: if *$input_type == InputDescriptors::RevokedOfferedHTLC || *$input_type == InputDescriptors::OfferedHTLC { true } else { false },
+                                       amount_msat: 0,
+                                       cltv_expiry: 2 << 16,
+                                       payment_hash: PaymentHash([1; 32]),
+                                       transaction_output_index: Some($idx),
+                               };
+                               let redeem_script = if *$input_type == InputDescriptors::RevokedOutput { chan_utils::get_revokeable_redeemscript(&pubkey, 256, &pubkey) } else { chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, &pubkey, &pubkey, &pubkey) };
+                               let sighash = hash_to_message!(&$sighash_parts.sighash_all(&$input, &redeem_script, $amount)[..]);
+                               let sig = secp_ctx.sign(&sighash, &privkey);
+                               $input.witness.push(sig.serialize_der().to_vec());
+                               $input.witness[0].push(SigHashType::All as u8);
+                               sum_actual_sigs += $input.witness[0].len();
+                               if *$input_type == InputDescriptors::RevokedOutput {
+                                       $input.witness.push(vec!(1));
+                               } else if *$input_type == InputDescriptors::RevokedOfferedHTLC || *$input_type == InputDescriptors::RevokedReceivedHTLC {
+                                       $input.witness.push(pubkey.clone().serialize().to_vec());
+                               } else if *$input_type == InputDescriptors::ReceivedHTLC {
+                                       $input.witness.push(vec![0]);
+                               } else {
+                                       $input.witness.push(PaymentPreimage([1; 32]).0.to_vec());
+                               }
+                               $input.witness.push(redeem_script.into_bytes());
+                               println!("witness[0] {}", $input.witness[0].len());
+                               println!("witness[1] {}", $input.witness[1].len());
+                               println!("witness[2] {}", $input.witness[2].len());
+                       }
+               }
+
+               let script_pubkey = Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script();
+               let txid = Sha256dHash::from_hex("56944c5d3f98413ef45cf54545538103cc9f298e0575820ad3591376e2e0f65d").unwrap();
+
+               // Justice tx with 1 to_local, 2 revoked offered HTLCs, 1 revoked received HTLCs
+               let mut claim_tx = Transaction { version: 0, lock_time: 0, input: Vec::new(), output: Vec::new() };
+               for i in 0..4 {
+                       claim_tx.input.push(TxIn {
+                               previous_output: BitcoinOutPoint {
+                                       txid,
+                                       vout: i,
+                               },
+                               script_sig: Script::new(),
+                               sequence: 0xfffffffd,
+                               witness: Vec::new(),
+                       });
+               }
+               claim_tx.output.push(TxOut {
+                       script_pubkey: script_pubkey.clone(),
+                       value: 0,
+               });
+               let base_weight = claim_tx.get_weight();
+               let sighash_parts = bip143::SighashComponents::new(&claim_tx);
+               let inputs_des = vec![InputDescriptors::RevokedOutput, InputDescriptors::RevokedOfferedHTLC, InputDescriptors::RevokedOfferedHTLC, InputDescriptors::RevokedReceivedHTLC];
+               for (idx, inp) in claim_tx.input.iter_mut().zip(inputs_des.iter()).enumerate() {
+                       sign_input!(sighash_parts, inp.0, idx as u32, 0, inp.1, sum_actual_sigs);
+               }
+               assert_eq!(base_weight + ChannelMonitor::get_witnesses_weight(&inputs_des[..]),  claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() - sum_actual_sigs));
+
+               // Claim tx with 1 offered HTLCs, 3 received HTLCs
+               claim_tx.input.clear();
+               sum_actual_sigs = 0;
+               for i in 0..4 {
+                       claim_tx.input.push(TxIn {
+                               previous_output: BitcoinOutPoint {
+                                       txid,
+                                       vout: i,
+                               },
+                               script_sig: Script::new(),
+                               sequence: 0xfffffffd,
+                               witness: Vec::new(),
+                       });
+               }
+               let base_weight = claim_tx.get_weight();
+               let sighash_parts = bip143::SighashComponents::new(&claim_tx);
+               let inputs_des = vec![InputDescriptors::OfferedHTLC, InputDescriptors::ReceivedHTLC, InputDescriptors::ReceivedHTLC, InputDescriptors::ReceivedHTLC];
+               for (idx, inp) in claim_tx.input.iter_mut().zip(inputs_des.iter()).enumerate() {
+                       sign_input!(sighash_parts, inp.0, idx as u32, 0, inp.1, sum_actual_sigs);
+               }
+               assert_eq!(base_weight + ChannelMonitor::get_witnesses_weight(&inputs_des[..]),  claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() - sum_actual_sigs));
+
+               // Justice tx with 1 revoked HTLC-Success tx output
+               claim_tx.input.clear();
+               sum_actual_sigs = 0;
+               claim_tx.input.push(TxIn {
+                       previous_output: BitcoinOutPoint {
+                               txid,
+                               vout: 0,
+                       },
+                       script_sig: Script::new(),
+                       sequence: 0xfffffffd,
+                       witness: Vec::new(),
+               });
+               let base_weight = claim_tx.get_weight();
+               let sighash_parts = bip143::SighashComponents::new(&claim_tx);
+               let inputs_des = vec![InputDescriptors::RevokedOutput];
+               for (idx, inp) in claim_tx.input.iter_mut().zip(inputs_des.iter()).enumerate() {
+                       sign_input!(sighash_parts, inp.0, idx as u32, 0, inp.1, sum_actual_sigs);
+               }
+               assert_eq!(base_weight + ChannelMonitor::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_isg */ (73 * inputs_des.len() - sum_actual_sigs));
+       }
+
+       // Further testing is done in the ChannelManager integration tests.
+}
diff --git a/lightning/src/ln/functional_test_utils.rs b/lightning/src/ln/functional_test_utils.rs
new file mode 100644 (file)
index 0000000..3c84cff
--- /dev/null
@@ -0,0 +1,1227 @@
+//! A bunch of useful utilities for building networks of nodes and exchanging messages between
+//! nodes for functional tests.
+
+use chain::chaininterface;
+use chain::transaction::OutPoint;
+use chain::keysinterface::KeysInterface;
+use ln::channelmanager::{ChannelManager,RAACommitmentOrder, PaymentPreimage, PaymentHash};
+use ln::router::{Route, Router};
+use ln::msgs;
+use ln::msgs::{ChannelMessageHandler,RoutingMessageHandler, LocalFeatures};
+use util::test_utils;
+use util::events::{Event, EventsProvider, MessageSendEvent, MessageSendEventsProvider};
+use util::errors::APIError;
+use util::logger::Logger;
+use util::config::UserConfig;
+
+use bitcoin::util::hash::BitcoinHash;
+use bitcoin::blockdata::block::BlockHeader;
+use bitcoin::blockdata::transaction::{Transaction, TxOut};
+use bitcoin::network::constants::Network;
+
+use bitcoin_hashes::sha256::Hash as Sha256;
+use bitcoin_hashes::sha256d::Hash as Sha256d;
+use bitcoin_hashes::Hash;
+
+use secp256k1::Secp256k1;
+use secp256k1::key::PublicKey;
+
+use rand::{thread_rng,Rng};
+
+use std::cell::RefCell;
+use std::rc::Rc;
+use std::sync::{Arc, Mutex};
+use std::mem;
+
+pub const CHAN_CONFIRM_DEPTH: u32 = 100;
+pub fn confirm_transaction(chain: &chaininterface::ChainWatchInterfaceUtil, tx: &Transaction, chan_id: u32) {
+       assert!(chain.does_match_tx(tx));
+       let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+       chain.block_connected_checked(&header, 1, &[tx; 1], &[chan_id; 1]);
+       for i in 2..CHAN_CONFIRM_DEPTH {
+               header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+               chain.block_connected_checked(&header, i, &[tx; 0], &[0; 0]);
+       }
+}
+
+pub fn connect_blocks(chain: &chaininterface::ChainWatchInterfaceUtil, depth: u32, height: u32, parent: bool, prev_blockhash: Sha256d) -> Sha256d {
+       let mut header = BlockHeader { version: 0x2000000, prev_blockhash: if parent { prev_blockhash } else { Default::default() }, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+       chain.block_connected_checked(&header, height + 1, &Vec::new(), &Vec::new());
+       for i in 2..depth + 1 {
+               header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+               chain.block_connected_checked(&header, height + i, &Vec::new(), &Vec::new());
+       }
+       header.bitcoin_hash()
+}
+
+pub struct Node {
+       pub chain_monitor: Arc<chaininterface::ChainWatchInterfaceUtil>,
+       pub tx_broadcaster: Arc<test_utils::TestBroadcaster>,
+       pub chan_monitor: Arc<test_utils::TestChannelMonitor>,
+       pub keys_manager: Arc<test_utils::TestKeysInterface>,
+       pub node: Arc<ChannelManager>,
+       pub router: Router,
+       pub node_seed: [u8; 32],
+       pub network_payment_count: Rc<RefCell<u8>>,
+       pub network_chan_count: Rc<RefCell<u32>>,
+}
+impl Drop for Node {
+       fn drop(&mut self) {
+               if !::std::thread::panicking() {
+                       // Check that we processed all pending events
+                       assert!(self.node.get_and_clear_pending_msg_events().is_empty());
+                       assert!(self.node.get_and_clear_pending_events().is_empty());
+                       assert!(self.chan_monitor.added_monitors.lock().unwrap().is_empty());
+               }
+       }
+}
+
+pub fn create_chan_between_nodes(node_a: &Node, node_b: &Node, a_flags: LocalFeatures, b_flags: LocalFeatures) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
+       create_chan_between_nodes_with_value(node_a, node_b, 100000, 10001, a_flags, b_flags)
+}
+
+pub fn create_chan_between_nodes_with_value(node_a: &Node, node_b: &Node, channel_value: u64, push_msat: u64, a_flags: LocalFeatures, b_flags: LocalFeatures) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
+       let (funding_locked, channel_id, tx) = create_chan_between_nodes_with_value_a(node_a, node_b, channel_value, push_msat, a_flags, b_flags);
+       let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(node_a, node_b, &funding_locked);
+       (announcement, as_update, bs_update, channel_id, tx)
+}
+
+macro_rules! get_revoke_commit_msgs {
+       ($node: expr, $node_id: expr) => {
+               {
+                       let events = $node.node.get_and_clear_pending_msg_events();
+                       assert_eq!(events.len(), 2);
+                       (match events[0] {
+                               MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
+                                       assert_eq!(*node_id, $node_id);
+                                       (*msg).clone()
+                               },
+                               _ => panic!("Unexpected event"),
+                       }, match events[1] {
+                               MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
+                                       assert_eq!(*node_id, $node_id);
+                                       assert!(updates.update_add_htlcs.is_empty());
+                                       assert!(updates.update_fulfill_htlcs.is_empty());
+                                       assert!(updates.update_fail_htlcs.is_empty());
+                                       assert!(updates.update_fail_malformed_htlcs.is_empty());
+                                       assert!(updates.update_fee.is_none());
+                                       updates.commitment_signed.clone()
+                               },
+                               _ => panic!("Unexpected event"),
+                       })
+               }
+       }
+}
+
+macro_rules! get_event_msg {
+       ($node: expr, $event_type: path, $node_id: expr) => {
+               {
+                       let events = $node.node.get_and_clear_pending_msg_events();
+                       assert_eq!(events.len(), 1);
+                       match events[0] {
+                               $event_type { ref node_id, ref msg } => {
+                                       assert_eq!(*node_id, $node_id);
+                                       (*msg).clone()
+                               },
+                               _ => panic!("Unexpected event"),
+                       }
+               }
+       }
+}
+
+macro_rules! get_htlc_update_msgs {
+       ($node: expr, $node_id: expr) => {
+               {
+                       let events = $node.node.get_and_clear_pending_msg_events();
+                       assert_eq!(events.len(), 1);
+                       match events[0] {
+                               MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
+                                       assert_eq!(*node_id, $node_id);
+                                       (*updates).clone()
+                               },
+                               _ => panic!("Unexpected event"),
+                       }
+               }
+       }
+}
+
+macro_rules! get_feerate {
+       ($node: expr, $channel_id: expr) => {
+               {
+                       let chan_lock = $node.node.channel_state.lock().unwrap();
+                       let chan = chan_lock.by_id.get(&$channel_id).unwrap();
+                       chan.get_feerate()
+               }
+       }
+}
+
+pub fn create_funding_transaction(node: &Node, expected_chan_value: u64, expected_user_chan_id: u64) -> ([u8; 32], Transaction, OutPoint) {
+       let chan_id = *node.network_chan_count.borrow();
+
+       let events = node.node.get_and_clear_pending_events();
+       assert_eq!(events.len(), 1);
+       match events[0] {
+               Event::FundingGenerationReady { ref temporary_channel_id, ref channel_value_satoshis, ref output_script, user_channel_id } => {
+                       assert_eq!(*channel_value_satoshis, expected_chan_value);
+                       assert_eq!(user_channel_id, expected_user_chan_id);
+
+                       let tx = Transaction { version: chan_id as u32, lock_time: 0, input: Vec::new(), output: vec![TxOut {
+                               value: *channel_value_satoshis, script_pubkey: output_script.clone(),
+                       }]};
+                       let funding_outpoint = OutPoint::new(tx.txid(), 0);
+                       (*temporary_channel_id, tx, funding_outpoint)
+               },
+               _ => panic!("Unexpected event"),
+       }
+}
+
+pub fn create_chan_between_nodes_with_value_init(node_a: &Node, node_b: &Node, channel_value: u64, push_msat: u64, a_flags: LocalFeatures, b_flags: LocalFeatures) -> Transaction {
+       node_a.node.create_channel(node_b.node.get_our_node_id(), channel_value, push_msat, 42).unwrap();
+       node_b.node.handle_open_channel(&node_a.node.get_our_node_id(), a_flags, &get_event_msg!(node_a, MessageSendEvent::SendOpenChannel, node_b.node.get_our_node_id())).unwrap();
+       node_a.node.handle_accept_channel(&node_b.node.get_our_node_id(), b_flags, &get_event_msg!(node_b, MessageSendEvent::SendAcceptChannel, node_a.node.get_our_node_id())).unwrap();
+
+       let (temporary_channel_id, tx, funding_output) = create_funding_transaction(node_a, channel_value, 42);
+
+       {
+               node_a.node.funding_transaction_generated(&temporary_channel_id, funding_output);
+               let mut added_monitors = node_a.chan_monitor.added_monitors.lock().unwrap();
+               assert_eq!(added_monitors.len(), 1);
+               assert_eq!(added_monitors[0].0, funding_output);
+               added_monitors.clear();
+       }
+
+       node_b.node.handle_funding_created(&node_a.node.get_our_node_id(), &get_event_msg!(node_a, MessageSendEvent::SendFundingCreated, node_b.node.get_our_node_id())).unwrap();
+       {
+               let mut added_monitors = node_b.chan_monitor.added_monitors.lock().unwrap();
+               assert_eq!(added_monitors.len(), 1);
+               assert_eq!(added_monitors[0].0, funding_output);
+               added_monitors.clear();
+       }
+
+       node_a.node.handle_funding_signed(&node_b.node.get_our_node_id(), &get_event_msg!(node_b, MessageSendEvent::SendFundingSigned, node_a.node.get_our_node_id())).unwrap();
+       {
+               let mut added_monitors = node_a.chan_monitor.added_monitors.lock().unwrap();
+               assert_eq!(added_monitors.len(), 1);
+               assert_eq!(added_monitors[0].0, funding_output);
+               added_monitors.clear();
+       }
+
+       let events_4 = node_a.node.get_and_clear_pending_events();
+       assert_eq!(events_4.len(), 1);
+       match events_4[0] {
+               Event::FundingBroadcastSafe { ref funding_txo, user_channel_id } => {
+                       assert_eq!(user_channel_id, 42);
+                       assert_eq!(*funding_txo, funding_output);
+               },
+               _ => panic!("Unexpected event"),
+       };
+
+       tx
+}
+
+pub fn create_chan_between_nodes_with_value_confirm_first(node_recv: &Node, node_conf: &Node, tx: &Transaction) {
+       confirm_transaction(&node_conf.chain_monitor, &tx, tx.version);
+       node_recv.node.handle_funding_locked(&node_conf.node.get_our_node_id(), &get_event_msg!(node_conf, MessageSendEvent::SendFundingLocked, node_recv.node.get_our_node_id())).unwrap();
+}
+
+pub fn create_chan_between_nodes_with_value_confirm_second(node_recv: &Node, node_conf: &Node) -> ((msgs::FundingLocked, msgs::AnnouncementSignatures), [u8; 32]) {
+       let channel_id;
+       let events_6 = node_conf.node.get_and_clear_pending_msg_events();
+       assert_eq!(events_6.len(), 2);
+       ((match events_6[0] {
+               MessageSendEvent::SendFundingLocked { ref node_id, ref msg } => {
+                       channel_id = msg.channel_id.clone();
+                       assert_eq!(*node_id, node_recv.node.get_our_node_id());
+                       msg.clone()
+               },
+               _ => panic!("Unexpected event"),
+       }, match events_6[1] {
+               MessageSendEvent::SendAnnouncementSignatures { ref node_id, ref msg } => {
+                       assert_eq!(*node_id, node_recv.node.get_our_node_id());
+                       msg.clone()
+               },
+               _ => panic!("Unexpected event"),
+       }), channel_id)
+}
+
+pub fn create_chan_between_nodes_with_value_confirm(node_a: &Node, node_b: &Node, tx: &Transaction) -> ((msgs::FundingLocked, msgs::AnnouncementSignatures), [u8; 32]) {
+       create_chan_between_nodes_with_value_confirm_first(node_a, node_b, tx);
+       confirm_transaction(&node_a.chain_monitor, &tx, tx.version);
+       create_chan_between_nodes_with_value_confirm_second(node_b, node_a)
+}
+
+pub fn create_chan_between_nodes_with_value_a(node_a: &Node, node_b: &Node, channel_value: u64, push_msat: u64, a_flags: LocalFeatures, b_flags: LocalFeatures) -> ((msgs::FundingLocked, msgs::AnnouncementSignatures), [u8; 32], Transaction) {
+       let tx = create_chan_between_nodes_with_value_init(node_a, node_b, channel_value, push_msat, a_flags, b_flags);
+       let (msgs, chan_id) = create_chan_between_nodes_with_value_confirm(node_a, node_b, &tx);
+       (msgs, chan_id, tx)
+}
+
+pub fn create_chan_between_nodes_with_value_b(node_a: &Node, node_b: &Node, as_funding_msgs: &(msgs::FundingLocked, msgs::AnnouncementSignatures)) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate) {
+       node_b.node.handle_funding_locked(&node_a.node.get_our_node_id(), &as_funding_msgs.0).unwrap();
+       let bs_announcement_sigs = get_event_msg!(node_b, MessageSendEvent::SendAnnouncementSignatures, node_a.node.get_our_node_id());
+       node_b.node.handle_announcement_signatures(&node_a.node.get_our_node_id(), &as_funding_msgs.1).unwrap();
+
+       let events_7 = node_b.node.get_and_clear_pending_msg_events();
+       assert_eq!(events_7.len(), 1);
+       let (announcement, bs_update) = match events_7[0] {
+               MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
+                       (msg, update_msg)
+               },
+               _ => panic!("Unexpected event"),
+       };
+
+       node_a.node.handle_announcement_signatures(&node_b.node.get_our_node_id(), &bs_announcement_sigs).unwrap();
+       let events_8 = node_a.node.get_and_clear_pending_msg_events();
+       assert_eq!(events_8.len(), 1);
+       let as_update = match events_8[0] {
+               MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
+                       assert!(*announcement == *msg);
+                       assert_eq!(update_msg.contents.short_channel_id, announcement.contents.short_channel_id);
+                       assert_eq!(update_msg.contents.short_channel_id, bs_update.contents.short_channel_id);
+                       update_msg
+               },
+               _ => panic!("Unexpected event"),
+       };
+
+       *node_a.network_chan_count.borrow_mut() += 1;
+
+       ((*announcement).clone(), (*as_update).clone(), (*bs_update).clone())
+}
+
+pub fn create_announced_chan_between_nodes(nodes: &Vec<Node>, a: usize, b: usize, a_flags: LocalFeatures, b_flags: LocalFeatures) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
+       create_announced_chan_between_nodes_with_value(nodes, a, b, 100000, 10001, a_flags, b_flags)
+}
+
+pub fn create_announced_chan_between_nodes_with_value(nodes: &Vec<Node>, a: usize, b: usize, channel_value: u64, push_msat: u64, a_flags: LocalFeatures, b_flags: LocalFeatures) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
+       let chan_announcement = create_chan_between_nodes_with_value(&nodes[a], &nodes[b], channel_value, push_msat, a_flags, b_flags);
+       for node in nodes {
+               assert!(node.router.handle_channel_announcement(&chan_announcement.0).unwrap());
+               node.router.handle_channel_update(&chan_announcement.1).unwrap();
+               node.router.handle_channel_update(&chan_announcement.2).unwrap();
+       }
+       (chan_announcement.1, chan_announcement.2, chan_announcement.3, chan_announcement.4)
+}
+
+macro_rules! check_spends {
+       ($tx: expr, $spends_tx: expr) => {
+               {
+                       $tx.verify(|out_point| {
+                               if out_point.txid == $spends_tx.txid() {
+                                       $spends_tx.output.get(out_point.vout as usize).cloned()
+                               } else {
+                                       None
+                               }
+                       }).unwrap();
+               }
+       }
+}
+
+macro_rules! get_closing_signed_broadcast {
+       ($node: expr, $dest_pubkey: expr) => {
+               {
+                       let events = $node.get_and_clear_pending_msg_events();
+                       assert!(events.len() == 1 || events.len() == 2);
+                       (match events[events.len() - 1] {
+                               MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
+                                       assert_eq!(msg.contents.flags & 2, 2);
+                                       msg.clone()
+                               },
+                               _ => panic!("Unexpected event"),
+                       }, if events.len() == 2 {
+                               match events[0] {
+                                       MessageSendEvent::SendClosingSigned { ref node_id, ref msg } => {
+                                               assert_eq!(*node_id, $dest_pubkey);
+                                               Some(msg.clone())
+                                       },
+                                       _ => panic!("Unexpected event"),
+                               }
+                       } else { None })
+               }
+       }
+}
+
+macro_rules! check_closed_broadcast {
+       ($node: expr) => {{
+               let events = $node.node.get_and_clear_pending_msg_events();
+               assert_eq!(events.len(), 1);
+               match events[0] {
+                       MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
+                               assert_eq!(msg.contents.flags & 2, 2);
+                       },
+                       _ => panic!("Unexpected event"),
+               }
+       }}
+}
+
+pub fn close_channel(outbound_node: &Node, inbound_node: &Node, channel_id: &[u8; 32], funding_tx: Transaction, close_inbound_first: bool) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, Transaction) {
+       let (node_a, broadcaster_a, struct_a) = if close_inbound_first { (&inbound_node.node, &inbound_node.tx_broadcaster, inbound_node) } else { (&outbound_node.node, &outbound_node.tx_broadcaster, outbound_node) };
+       let (node_b, broadcaster_b) = if close_inbound_first { (&outbound_node.node, &outbound_node.tx_broadcaster) } else { (&inbound_node.node, &inbound_node.tx_broadcaster) };
+       let (tx_a, tx_b);
+
+       node_a.close_channel(channel_id).unwrap();
+       node_b.handle_shutdown(&node_a.get_our_node_id(), &get_event_msg!(struct_a, MessageSendEvent::SendShutdown, node_b.get_our_node_id())).unwrap();
+
+       let events_1 = node_b.get_and_clear_pending_msg_events();
+       assert!(events_1.len() >= 1);
+       let shutdown_b = match events_1[0] {
+               MessageSendEvent::SendShutdown { ref node_id, ref msg } => {
+                       assert_eq!(node_id, &node_a.get_our_node_id());
+                       msg.clone()
+               },
+               _ => panic!("Unexpected event"),
+       };
+
+       let closing_signed_b = if !close_inbound_first {
+               assert_eq!(events_1.len(), 1);
+               None
+       } else {
+               Some(match events_1[1] {
+                       MessageSendEvent::SendClosingSigned { ref node_id, ref msg } => {
+                               assert_eq!(node_id, &node_a.get_our_node_id());
+                               msg.clone()
+                       },
+                       _ => panic!("Unexpected event"),
+               })
+       };
+
+       node_a.handle_shutdown(&node_b.get_our_node_id(), &shutdown_b).unwrap();
+       let (as_update, bs_update) = if close_inbound_first {
+               assert!(node_a.get_and_clear_pending_msg_events().is_empty());
+               node_a.handle_closing_signed(&node_b.get_our_node_id(), &closing_signed_b.unwrap()).unwrap();
+               assert_eq!(broadcaster_a.txn_broadcasted.lock().unwrap().len(), 1);
+               tx_a = broadcaster_a.txn_broadcasted.lock().unwrap().remove(0);
+               let (as_update, closing_signed_a) = get_closing_signed_broadcast!(node_a, node_b.get_our_node_id());
+
+               node_b.handle_closing_signed(&node_a.get_our_node_id(), &closing_signed_a.unwrap()).unwrap();
+               let (bs_update, none_b) = get_closing_signed_broadcast!(node_b, node_a.get_our_node_id());
+               assert!(none_b.is_none());
+               assert_eq!(broadcaster_b.txn_broadcasted.lock().unwrap().len(), 1);
+               tx_b = broadcaster_b.txn_broadcasted.lock().unwrap().remove(0);
+               (as_update, bs_update)
+       } else {
+               let closing_signed_a = get_event_msg!(struct_a, MessageSendEvent::SendClosingSigned, node_b.get_our_node_id());
+
+               node_b.handle_closing_signed(&node_a.get_our_node_id(), &closing_signed_a).unwrap();
+               assert_eq!(broadcaster_b.txn_broadcasted.lock().unwrap().len(), 1);
+               tx_b = broadcaster_b.txn_broadcasted.lock().unwrap().remove(0);
+               let (bs_update, closing_signed_b) = get_closing_signed_broadcast!(node_b, node_a.get_our_node_id());
+
+               node_a.handle_closing_signed(&node_b.get_our_node_id(), &closing_signed_b.unwrap()).unwrap();
+               let (as_update, none_a) = get_closing_signed_broadcast!(node_a, node_b.get_our_node_id());
+               assert!(none_a.is_none());
+               assert_eq!(broadcaster_a.txn_broadcasted.lock().unwrap().len(), 1);
+               tx_a = broadcaster_a.txn_broadcasted.lock().unwrap().remove(0);
+               (as_update, bs_update)
+       };
+       assert_eq!(tx_a, tx_b);
+       check_spends!(tx_a, funding_tx);
+
+       (as_update, bs_update, tx_a)
+}
+
+pub struct SendEvent {
+       pub node_id: PublicKey,
+       pub msgs: Vec<msgs::UpdateAddHTLC>,
+       pub commitment_msg: msgs::CommitmentSigned,
+}
+impl SendEvent {
+       pub fn from_commitment_update(node_id: PublicKey, updates: msgs::CommitmentUpdate) -> SendEvent {
+               assert!(updates.update_fulfill_htlcs.is_empty());
+               assert!(updates.update_fail_htlcs.is_empty());
+               assert!(updates.update_fail_malformed_htlcs.is_empty());
+               assert!(updates.update_fee.is_none());
+               SendEvent { node_id: node_id, msgs: updates.update_add_htlcs, commitment_msg: updates.commitment_signed }
+       }
+
+       pub fn from_event(event: MessageSendEvent) -> SendEvent {
+               match event {
+                       MessageSendEvent::UpdateHTLCs { node_id, updates } => SendEvent::from_commitment_update(node_id, updates),
+                       _ => panic!("Unexpected event type!"),
+               }
+       }
+
+       pub fn from_node(node: &Node) -> SendEvent {
+               let mut events = node.node.get_and_clear_pending_msg_events();
+               assert_eq!(events.len(), 1);
+               SendEvent::from_event(events.pop().unwrap())
+       }
+}
+
+macro_rules! check_added_monitors {
+       ($node: expr, $count: expr) => {
+               {
+                       let mut added_monitors = $node.chan_monitor.added_monitors.lock().unwrap();
+                       assert_eq!(added_monitors.len(), $count);
+                       added_monitors.clear();
+               }
+       }
+}
+
+macro_rules! commitment_signed_dance {
+       ($node_a: expr, $node_b: expr, $commitment_signed: expr, $fail_backwards: expr, true /* skip last step */) => {
+               {
+                       check_added_monitors!($node_a, 0);
+                       assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
+                       $node_a.node.handle_commitment_signed(&$node_b.node.get_our_node_id(), &$commitment_signed).unwrap();
+                       check_added_monitors!($node_a, 1);
+                       commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, false);
+               }
+       };
+       ($node_a: expr, $node_b: expr, (), $fail_backwards: expr, true /* skip last step */, true /* return extra message */, true /* return last RAA */) => {
+               {
+                       let (as_revoke_and_ack, as_commitment_signed) = get_revoke_commit_msgs!($node_a, $node_b.node.get_our_node_id());
+                       check_added_monitors!($node_b, 0);
+                       assert!($node_b.node.get_and_clear_pending_msg_events().is_empty());
+                       $node_b.node.handle_revoke_and_ack(&$node_a.node.get_our_node_id(), &as_revoke_and_ack).unwrap();
+                       assert!($node_b.node.get_and_clear_pending_msg_events().is_empty());
+                       check_added_monitors!($node_b, 1);
+                       $node_b.node.handle_commitment_signed(&$node_a.node.get_our_node_id(), &as_commitment_signed).unwrap();
+                       let (bs_revoke_and_ack, extra_msg_option) = {
+                               let events = $node_b.node.get_and_clear_pending_msg_events();
+                               assert!(events.len() <= 2);
+                               (match events[0] {
+                                       MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
+                                               assert_eq!(*node_id, $node_a.node.get_our_node_id());
+                                               (*msg).clone()
+                                       },
+                                       _ => panic!("Unexpected event"),
+                               }, events.get(1).map(|e| e.clone()))
+                       };
+                       check_added_monitors!($node_b, 1);
+                       if $fail_backwards {
+                               assert!($node_a.node.get_and_clear_pending_events().is_empty());
+                               assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
+                       }
+                       (extra_msg_option, bs_revoke_and_ack)
+               }
+       };
+       ($node_a: expr, $node_b: expr, $commitment_signed: expr, $fail_backwards: expr, true /* skip last step */, false /* return extra message */, true /* return last RAA */) => {
+               {
+                       check_added_monitors!($node_a, 0);
+                       assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
+                       $node_a.node.handle_commitment_signed(&$node_b.node.get_our_node_id(), &$commitment_signed).unwrap();
+                       check_added_monitors!($node_a, 1);
+                       let (extra_msg_option, bs_revoke_and_ack) = commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, true, true);
+                       assert!(extra_msg_option.is_none());
+                       bs_revoke_and_ack
+               }
+       };
+       ($node_a: expr, $node_b: expr, (), $fail_backwards: expr, true /* skip last step */, true /* return extra message */) => {
+               {
+                       let (extra_msg_option, bs_revoke_and_ack) = commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, true, true);
+                       $node_a.node.handle_revoke_and_ack(&$node_b.node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
+                       check_added_monitors!($node_a, 1);
+                       extra_msg_option
+               }
+       };
+       ($node_a: expr, $node_b: expr, (), $fail_backwards: expr, true /* skip last step */, false /* no extra message */) => {
+               {
+                       assert!(commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, true).is_none());
+               }
+       };
+       ($node_a: expr, $node_b: expr, $commitment_signed: expr, $fail_backwards: expr) => {
+               {
+                       commitment_signed_dance!($node_a, $node_b, $commitment_signed, $fail_backwards, true);
+                       if $fail_backwards {
+                               expect_pending_htlcs_forwardable!($node_a);
+                               check_added_monitors!($node_a, 1);
+
+                               let channel_state = $node_a.node.channel_state.lock().unwrap();
+                               assert_eq!(channel_state.pending_msg_events.len(), 1);
+                               if let MessageSendEvent::UpdateHTLCs { ref node_id, .. } = channel_state.pending_msg_events[0] {
+                                       assert_ne!(*node_id, $node_b.node.get_our_node_id());
+                               } else { panic!("Unexpected event"); }
+                       } else {
+                               assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
+                       }
+               }
+       }
+}
+
+macro_rules! get_payment_preimage_hash {
+       ($node: expr) => {
+               {
+                       let payment_preimage = PaymentPreimage([*$node.network_payment_count.borrow(); 32]);
+                       *$node.network_payment_count.borrow_mut() += 1;
+                       let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0[..]).into_inner());
+                       (payment_preimage, payment_hash)
+               }
+       }
+}
+
+macro_rules! expect_pending_htlcs_forwardable {
+       ($node: expr) => {{
+               let events = $node.node.get_and_clear_pending_events();
+               assert_eq!(events.len(), 1);
+               match events[0] {
+                       Event::PendingHTLCsForwardable { .. } => { },
+                       _ => panic!("Unexpected event"),
+               };
+               $node.node.process_pending_htlc_forwards();
+       }}
+}
+
+macro_rules! expect_payment_received {
+       ($node: expr, $expected_payment_hash: expr, $expected_recv_value: expr) => {
+               let events = $node.node.get_and_clear_pending_events();
+               assert_eq!(events.len(), 1);
+               match events[0] {
+                       Event::PaymentReceived { ref payment_hash, amt } => {
+                               assert_eq!($expected_payment_hash, *payment_hash);
+                               assert_eq!($expected_recv_value, amt);
+                       },
+                       _ => panic!("Unexpected event"),
+               }
+       }
+}
+
+macro_rules! expect_payment_sent {
+       ($node: expr, $expected_payment_preimage: expr) => {
+               let events = $node.node.get_and_clear_pending_events();
+               assert_eq!(events.len(), 1);
+               match events[0] {
+                       Event::PaymentSent { ref payment_preimage } => {
+                               assert_eq!($expected_payment_preimage, *payment_preimage);
+                       },
+                       _ => panic!("Unexpected event"),
+               }
+       }
+}
+
+pub fn send_along_route_with_hash(origin_node: &Node, route: Route, expected_route: &[&Node], recv_value: u64, our_payment_hash: PaymentHash) {
+       let mut payment_event = {
+               origin_node.node.send_payment(route, our_payment_hash).unwrap();
+               check_added_monitors!(origin_node, 1);
+
+               let mut events = origin_node.node.get_and_clear_pending_msg_events();
+               assert_eq!(events.len(), 1);
+               SendEvent::from_event(events.remove(0))
+       };
+       let mut prev_node = origin_node;
+
+       for (idx, &node) in expected_route.iter().enumerate() {
+               assert_eq!(node.node.get_our_node_id(), payment_event.node_id);
+
+               node.node.handle_update_add_htlc(&prev_node.node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
+               check_added_monitors!(node, 0);
+               commitment_signed_dance!(node, prev_node, payment_event.commitment_msg, false);
+
+               expect_pending_htlcs_forwardable!(node);
+
+               if idx == expected_route.len() - 1 {
+                       let events_2 = node.node.get_and_clear_pending_events();
+                       assert_eq!(events_2.len(), 1);
+                       match events_2[0] {
+                               Event::PaymentReceived { ref payment_hash, amt } => {
+                                       assert_eq!(our_payment_hash, *payment_hash);
+                                       assert_eq!(amt, recv_value);
+                               },
+                               _ => panic!("Unexpected event"),
+                       }
+               } else {
+                       let mut events_2 = node.node.get_and_clear_pending_msg_events();
+                       assert_eq!(events_2.len(), 1);
+                       check_added_monitors!(node, 1);
+                       payment_event = SendEvent::from_event(events_2.remove(0));
+                       assert_eq!(payment_event.msgs.len(), 1);
+               }
+
+               prev_node = node;
+       }
+}
+
+pub fn send_along_route(origin_node: &Node, route: Route, expected_route: &[&Node], recv_value: u64) -> (PaymentPreimage, PaymentHash) {
+       let (our_payment_preimage, our_payment_hash) = get_payment_preimage_hash!(origin_node);
+       send_along_route_with_hash(origin_node, route, expected_route, recv_value, our_payment_hash);
+       (our_payment_preimage, our_payment_hash)
+}
+
+pub fn claim_payment_along_route(origin_node: &Node, expected_route: &[&Node], skip_last: bool, our_payment_preimage: PaymentPreimage) {
+       assert!(expected_route.last().unwrap().node.claim_funds(our_payment_preimage));
+       check_added_monitors!(expected_route.last().unwrap(), 1);
+
+       let mut next_msgs: Option<(msgs::UpdateFulfillHTLC, msgs::CommitmentSigned)> = None;
+       let mut expected_next_node = expected_route.last().unwrap().node.get_our_node_id();
+       macro_rules! get_next_msgs {
+               ($node: expr) => {
+                       {
+                               let events = $node.node.get_and_clear_pending_msg_events();
+                               assert_eq!(events.len(), 1);
+                               match events[0] {
+                                       MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => {
+                                               assert!(update_add_htlcs.is_empty());
+                                               assert_eq!(update_fulfill_htlcs.len(), 1);
+                                               assert!(update_fail_htlcs.is_empty());
+                                               assert!(update_fail_malformed_htlcs.is_empty());
+                                               assert!(update_fee.is_none());
+                                               expected_next_node = node_id.clone();
+                                               Some((update_fulfill_htlcs[0].clone(), commitment_signed.clone()))
+                                       },
+                                       _ => panic!("Unexpected event"),
+                               }
+                       }
+               }
+       }
+
+       macro_rules! last_update_fulfill_dance {
+               ($node: expr, $prev_node: expr) => {
+                       {
+                               $node.node.handle_update_fulfill_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0).unwrap();
+                               check_added_monitors!($node, 0);
+                               assert!($node.node.get_and_clear_pending_msg_events().is_empty());
+                               commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, false);
+                       }
+               }
+       }
+       macro_rules! mid_update_fulfill_dance {
+               ($node: expr, $prev_node: expr, $new_msgs: expr) => {
+                       {
+                               $node.node.handle_update_fulfill_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0).unwrap();
+                               check_added_monitors!($node, 1);
+                               let new_next_msgs = if $new_msgs {
+                                       get_next_msgs!($node)
+                               } else {
+                                       assert!($node.node.get_and_clear_pending_msg_events().is_empty());
+                                       None
+                               };
+                               commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, false);
+                               next_msgs = new_next_msgs;
+                       }
+               }
+       }
+
+       let mut prev_node = expected_route.last().unwrap();
+       for (idx, node) in expected_route.iter().rev().enumerate() {
+               assert_eq!(expected_next_node, node.node.get_our_node_id());
+               let update_next_msgs = !skip_last || idx != expected_route.len() - 1;
+               if next_msgs.is_some() {
+                       mid_update_fulfill_dance!(node, prev_node, update_next_msgs);
+               } else if update_next_msgs {
+                       next_msgs = get_next_msgs!(node);
+               } else {
+                       assert!(node.node.get_and_clear_pending_msg_events().is_empty());
+               }
+               if !skip_last && idx == expected_route.len() - 1 {
+                       assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
+               }
+
+               prev_node = node;
+       }
+
+       if !skip_last {
+               last_update_fulfill_dance!(origin_node, expected_route.first().unwrap());
+               expect_payment_sent!(origin_node, our_payment_preimage);
+       }
+}
+
+pub fn claim_payment(origin_node: &Node, expected_route: &[&Node], our_payment_preimage: PaymentPreimage) {
+       claim_payment_along_route(origin_node, expected_route, false, our_payment_preimage);
+}
+
+pub const TEST_FINAL_CLTV: u32 = 32;
+
+pub fn route_payment(origin_node: &Node, expected_route: &[&Node], recv_value: u64) -> (PaymentPreimage, PaymentHash) {
+       let route = origin_node.router.get_route(&expected_route.last().unwrap().node.get_our_node_id(), None, &Vec::new(), recv_value, TEST_FINAL_CLTV).unwrap();
+       assert_eq!(route.hops.len(), expected_route.len());
+       for (node, hop) in expected_route.iter().zip(route.hops.iter()) {
+               assert_eq!(hop.pubkey, node.node.get_our_node_id());
+       }
+
+       send_along_route(origin_node, route, expected_route, recv_value)
+}
+
+pub fn route_over_limit(origin_node: &Node, expected_route: &[&Node], recv_value: u64) {
+       let route = origin_node.router.get_route(&expected_route.last().unwrap().node.get_our_node_id(), None, &Vec::new(), recv_value, TEST_FINAL_CLTV).unwrap();
+       assert_eq!(route.hops.len(), expected_route.len());
+       for (node, hop) in expected_route.iter().zip(route.hops.iter()) {
+               assert_eq!(hop.pubkey, node.node.get_our_node_id());
+       }
+
+       let (_, our_payment_hash) = get_payment_preimage_hash!(origin_node);
+
+       let err = origin_node.node.send_payment(route, our_payment_hash).err().unwrap();
+       match err {
+               APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over the max HTLC value in flight our peer will accept"),
+               _ => panic!("Unknown error variants"),
+       };
+}
+
+pub fn send_payment(origin: &Node, expected_route: &[&Node], recv_value: u64) {
+       let our_payment_preimage = route_payment(&origin, expected_route, recv_value).0;
+       claim_payment(&origin, expected_route, our_payment_preimage);
+}
+
+pub fn fail_payment_along_route(origin_node: &Node, expected_route: &[&Node], skip_last: bool, our_payment_hash: PaymentHash) {
+       assert!(expected_route.last().unwrap().node.fail_htlc_backwards(&our_payment_hash));
+       expect_pending_htlcs_forwardable!(expected_route.last().unwrap());
+       check_added_monitors!(expected_route.last().unwrap(), 1);
+
+       let mut next_msgs: Option<(msgs::UpdateFailHTLC, msgs::CommitmentSigned)> = None;
+       macro_rules! update_fail_dance {
+               ($node: expr, $prev_node: expr, $last_node: expr) => {
+                       {
+                               $node.node.handle_update_fail_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0).unwrap();
+                               commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, !$last_node);
+                               if skip_last && $last_node {
+                                       expect_pending_htlcs_forwardable!($node);
+                               }
+                       }
+               }
+       }
+
+       let mut expected_next_node = expected_route.last().unwrap().node.get_our_node_id();
+       let mut prev_node = expected_route.last().unwrap();
+       for (idx, node) in expected_route.iter().rev().enumerate() {
+               assert_eq!(expected_next_node, node.node.get_our_node_id());
+               if next_msgs.is_some() {
+                       // We may be the "last node" for the purpose of the commitment dance if we're
+                       // skipping the last node (implying it is disconnected) and we're the
+                       // second-to-last node!
+                       update_fail_dance!(node, prev_node, skip_last && idx == expected_route.len() - 1);
+               }
+
+               let events = node.node.get_and_clear_pending_msg_events();
+               if !skip_last || idx != expected_route.len() - 1 {
+                       assert_eq!(events.len(), 1);
+                       match events[0] {
+                               MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => {
+                                       assert!(update_add_htlcs.is_empty());
+                                       assert!(update_fulfill_htlcs.is_empty());
+                                       assert_eq!(update_fail_htlcs.len(), 1);
+                                       assert!(update_fail_malformed_htlcs.is_empty());
+                                       assert!(update_fee.is_none());
+                                       expected_next_node = node_id.clone();
+                                       next_msgs = Some((update_fail_htlcs[0].clone(), commitment_signed.clone()));
+                               },
+                               _ => panic!("Unexpected event"),
+                       }
+               } else {
+                       assert!(events.is_empty());
+               }
+               if !skip_last && idx == expected_route.len() - 1 {
+                       assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
+               }
+
+               prev_node = node;
+       }
+
+       if !skip_last {
+               update_fail_dance!(origin_node, expected_route.first().unwrap(), true);
+
+               let events = origin_node.node.get_and_clear_pending_events();
+               assert_eq!(events.len(), 1);
+               match events[0] {
+                       Event::PaymentFailed { payment_hash, rejected_by_dest, .. } => {
+                               assert_eq!(payment_hash, our_payment_hash);
+                               assert!(rejected_by_dest);
+                       },
+                       _ => panic!("Unexpected event"),
+               }
+       }
+}
+
+pub fn fail_payment(origin_node: &Node, expected_route: &[&Node], our_payment_hash: PaymentHash) {
+       fail_payment_along_route(origin_node, expected_route, false, our_payment_hash);
+}
+
+pub fn create_network(node_count: usize, node_config: &[Option<UserConfig>]) -> Vec<Node> {
+       let mut nodes = Vec::new();
+       let mut rng = thread_rng();
+       let secp_ctx = Secp256k1::new();
+
+       let chan_count = Rc::new(RefCell::new(0));
+       let payment_count = Rc::new(RefCell::new(0));
+
+       for i in 0..node_count {
+               let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::with_id(format!("node {}", i)));
+               let feeest = Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 });
+               let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, Arc::clone(&logger)));
+               let tx_broadcaster = Arc::new(test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new())});
+               let mut seed = [0; 32];
+               rng.fill_bytes(&mut seed);
+               let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&seed, Network::Testnet, Arc::clone(&logger)));
+               let chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(chain_monitor.clone(), tx_broadcaster.clone(), logger.clone(), feeest.clone()));
+               let mut default_config = UserConfig::new();
+               default_config.channel_options.announced_channel = true;
+               default_config.peer_channel_config_limits.force_announced_channel_preference = false;
+               let node = ChannelManager::new(Network::Testnet, feeest.clone(), chan_monitor.clone(), chain_monitor.clone(), tx_broadcaster.clone(), Arc::clone(&logger), keys_manager.clone(), if node_config[i].is_some() { node_config[i].clone().unwrap() } else { default_config }).unwrap();
+               let router = Router::new(PublicKey::from_secret_key(&secp_ctx, &keys_manager.get_node_secret()), chain_monitor.clone(), Arc::clone(&logger));
+               nodes.push(Node { chain_monitor, tx_broadcaster, chan_monitor, node, router, keys_manager, node_seed: seed,
+                       network_payment_count: payment_count.clone(),
+                       network_chan_count: chan_count.clone(),
+               });
+       }
+
+       nodes
+}
+
+#[derive(PartialEq)]
+pub enum HTLCType { NONE, TIMEOUT, SUCCESS }
+/// Tests that the given node has broadcast transactions for the given Channel
+///
+/// First checks that the latest local commitment tx has been broadcast, unless an explicit
+/// commitment_tx is provided, which may be used to test that a remote commitment tx was
+/// broadcast and the revoked outputs were claimed.
+///
+/// Next tests that there is (or is not) a transaction that spends the commitment transaction
+/// that appears to be the type of HTLC transaction specified in has_htlc_tx.
+///
+/// All broadcast transactions must be accounted for in one of the above three types of we'll
+/// also fail.
+pub fn test_txn_broadcast(node: &Node, chan: &(msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction), commitment_tx: Option<Transaction>, has_htlc_tx: HTLCType) -> Vec<Transaction> {
+       let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
+       assert!(node_txn.len() >= if commitment_tx.is_some() { 0 } else { 1 } + if has_htlc_tx == HTLCType::NONE { 0 } else { 1 });
+
+       let mut res = Vec::with_capacity(2);
+       node_txn.retain(|tx| {
+               if tx.input.len() == 1 && tx.input[0].previous_output.txid == chan.3.txid() {
+                       check_spends!(tx, chan.3.clone());
+                       if commitment_tx.is_none() {
+                               res.push(tx.clone());
+                       }
+                       false
+               } else { true }
+       });
+       if let Some(explicit_tx) = commitment_tx {
+               res.push(explicit_tx.clone());
+       }
+
+       assert_eq!(res.len(), 1);
+
+       if has_htlc_tx != HTLCType::NONE {
+               node_txn.retain(|tx| {
+                       if tx.input.len() == 1 && tx.input[0].previous_output.txid == res[0].txid() {
+                               check_spends!(tx, res[0].clone());
+                               if has_htlc_tx == HTLCType::TIMEOUT {
+                                       assert!(tx.lock_time != 0);
+                               } else {
+                                       assert!(tx.lock_time == 0);
+                               }
+                               res.push(tx.clone());
+                               false
+                       } else { true }
+               });
+               assert!(res.len() == 2 || res.len() == 3);
+               if res.len() == 3 {
+                       assert_eq!(res[1], res[2]);
+               }
+       }
+
+       assert!(node_txn.is_empty());
+       res
+}
+
+/// Tests that the given node has broadcast a claim transaction against the provided revoked
+/// HTLC transaction.
+pub fn test_revoked_htlc_claim_txn_broadcast(node: &Node, revoked_tx: Transaction) {
+       let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
+       assert_eq!(node_txn.len(), 1);
+       node_txn.retain(|tx| {
+               if tx.input.len() == 1 && tx.input[0].previous_output.txid == revoked_tx.txid() {
+                       check_spends!(tx, revoked_tx.clone());
+                       false
+               } else { true }
+       });
+       assert!(node_txn.is_empty());
+}
+
+pub fn check_preimage_claim(node: &Node, prev_txn: &Vec<Transaction>) -> Vec<Transaction> {
+       let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
+
+       assert!(node_txn.len() >= 1);
+       assert_eq!(node_txn[0].input.len(), 1);
+       let mut found_prev = false;
+
+       for tx in prev_txn {
+               if node_txn[0].input[0].previous_output.txid == tx.txid() {
+                       check_spends!(node_txn[0], tx.clone());
+                       assert!(node_txn[0].input[0].witness[2].len() > 106); // must spend an htlc output
+                       assert_eq!(tx.input.len(), 1); // must spend a commitment tx
+
+                       found_prev = true;
+                       break;
+               }
+       }
+       assert!(found_prev);
+
+       let mut res = Vec::new();
+       mem::swap(&mut *node_txn, &mut res);
+       res
+}
+
+pub fn get_announce_close_broadcast_events(nodes: &Vec<Node>, a: usize, b: usize) {
+       let events_1 = nodes[a].node.get_and_clear_pending_msg_events();
+       assert_eq!(events_1.len(), 1);
+       let as_update = match events_1[0] {
+               MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
+                       msg.clone()
+               },
+               _ => panic!("Unexpected event"),
+       };
+
+       let events_2 = nodes[b].node.get_and_clear_pending_msg_events();
+       assert_eq!(events_2.len(), 1);
+       let bs_update = match events_2[0] {
+               MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
+                       msg.clone()
+               },
+               _ => panic!("Unexpected event"),
+       };
+
+       for node in nodes {
+               node.router.handle_channel_update(&as_update).unwrap();
+               node.router.handle_channel_update(&bs_update).unwrap();
+       }
+}
+
+macro_rules! get_channel_value_stat {
+       ($node: expr, $channel_id: expr) => {{
+               let chan_lock = $node.node.channel_state.lock().unwrap();
+               let chan = chan_lock.by_id.get(&$channel_id).unwrap();
+               chan.get_value_stat()
+       }}
+}
+
+macro_rules! get_chan_reestablish_msgs {
+       ($src_node: expr, $dst_node: expr) => {
+               {
+                       let mut res = Vec::with_capacity(1);
+                       for msg in $src_node.node.get_and_clear_pending_msg_events() {
+                               if let MessageSendEvent::SendChannelReestablish { ref node_id, ref msg } = msg {
+                                       assert_eq!(*node_id, $dst_node.node.get_our_node_id());
+                                       res.push(msg.clone());
+                               } else {
+                                       panic!("Unexpected event")
+                               }
+                       }
+                       res
+               }
+       }
+}
+
+macro_rules! handle_chan_reestablish_msgs {
+       ($src_node: expr, $dst_node: expr) => {
+               {
+                       let msg_events = $src_node.node.get_and_clear_pending_msg_events();
+                       let mut idx = 0;
+                       let funding_locked = if let Some(&MessageSendEvent::SendFundingLocked { ref node_id, ref msg }) = msg_events.get(0) {
+                               idx += 1;
+                               assert_eq!(*node_id, $dst_node.node.get_our_node_id());
+                               Some(msg.clone())
+                       } else {
+                               None
+                       };
+
+                       let mut revoke_and_ack = None;
+                       let mut commitment_update = None;
+                       let order = if let Some(ev) = msg_events.get(idx) {
+                               idx += 1;
+                               match ev {
+                                       &MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
+                                               assert_eq!(*node_id, $dst_node.node.get_our_node_id());
+                                               revoke_and_ack = Some(msg.clone());
+                                               RAACommitmentOrder::RevokeAndACKFirst
+                                       },
+                                       &MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
+                                               assert_eq!(*node_id, $dst_node.node.get_our_node_id());
+                                               commitment_update = Some(updates.clone());
+                                               RAACommitmentOrder::CommitmentFirst
+                                       },
+                                       _ => panic!("Unexpected event"),
+                               }
+                       } else {
+                               RAACommitmentOrder::CommitmentFirst
+                       };
+
+                       if let Some(ev) = msg_events.get(idx) {
+                               match ev {
+                                       &MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
+                                               assert_eq!(*node_id, $dst_node.node.get_our_node_id());
+                                               assert!(revoke_and_ack.is_none());
+                                               revoke_and_ack = Some(msg.clone());
+                                       },
+                                       &MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
+                                               assert_eq!(*node_id, $dst_node.node.get_our_node_id());
+                                               assert!(commitment_update.is_none());
+                                               commitment_update = Some(updates.clone());
+                                       },
+                                       _ => panic!("Unexpected event"),
+                               }
+                       }
+
+                       (funding_locked, revoke_and_ack, commitment_update, order)
+               }
+       }
+}
+
+/// pending_htlc_adds includes both the holding cell and in-flight update_add_htlcs, whereas
+/// for claims/fails they are separated out.
+pub fn reconnect_nodes(node_a: &Node, node_b: &Node, send_funding_locked: (bool, bool), pending_htlc_adds: (i64, i64), pending_htlc_claims: (usize, usize), pending_cell_htlc_claims: (usize, usize), pending_cell_htlc_fails: (usize, usize), pending_raa: (bool, bool)) {
+       node_a.node.peer_connected(&node_b.node.get_our_node_id());
+       let reestablish_1 = get_chan_reestablish_msgs!(node_a, node_b);
+       node_b.node.peer_connected(&node_a.node.get_our_node_id());
+       let reestablish_2 = get_chan_reestablish_msgs!(node_b, node_a);
+
+       if send_funding_locked.0 {
+               // If a expects a funding_locked, it better not think it has received a revoke_and_ack
+               // from b
+               for reestablish in reestablish_1.iter() {
+                       assert_eq!(reestablish.next_remote_commitment_number, 0);
+               }
+       }
+       if send_funding_locked.1 {
+               // If b expects a funding_locked, it better not think it has received a revoke_and_ack
+               // from a
+               for reestablish in reestablish_2.iter() {
+                       assert_eq!(reestablish.next_remote_commitment_number, 0);
+               }
+       }
+       if send_funding_locked.0 || send_funding_locked.1 {
+               // If we expect any funding_locked's, both sides better have set
+               // next_local_commitment_number to 1
+               for reestablish in reestablish_1.iter() {
+                       assert_eq!(reestablish.next_local_commitment_number, 1);
+               }
+               for reestablish in reestablish_2.iter() {
+                       assert_eq!(reestablish.next_local_commitment_number, 1);
+               }
+       }
+
+       let mut resp_1 = Vec::new();
+       for msg in reestablish_1 {
+               node_b.node.handle_channel_reestablish(&node_a.node.get_our_node_id(), &msg).unwrap();
+               resp_1.push(handle_chan_reestablish_msgs!(node_b, node_a));
+       }
+       if pending_cell_htlc_claims.0 != 0 || pending_cell_htlc_fails.0 != 0 {
+               check_added_monitors!(node_b, 1);
+       } else {
+               check_added_monitors!(node_b, 0);
+       }
+
+       let mut resp_2 = Vec::new();
+       for msg in reestablish_2 {
+               node_a.node.handle_channel_reestablish(&node_b.node.get_our_node_id(), &msg).unwrap();
+               resp_2.push(handle_chan_reestablish_msgs!(node_a, node_b));
+       }
+       if pending_cell_htlc_claims.1 != 0 || pending_cell_htlc_fails.1 != 0 {
+               check_added_monitors!(node_a, 1);
+       } else {
+               check_added_monitors!(node_a, 0);
+       }
+
+       // We don't yet support both needing updates, as that would require a different commitment dance:
+       assert!((pending_htlc_adds.0 == 0 && pending_htlc_claims.0 == 0 && pending_cell_htlc_claims.0 == 0 && pending_cell_htlc_fails.0 == 0) ||
+                       (pending_htlc_adds.1 == 0 && pending_htlc_claims.1 == 0 && pending_cell_htlc_claims.1 == 0 && pending_cell_htlc_fails.1 == 0));
+
+       for chan_msgs in resp_1.drain(..) {
+               if send_funding_locked.0 {
+                       node_a.node.handle_funding_locked(&node_b.node.get_our_node_id(), &chan_msgs.0.unwrap()).unwrap();
+                       let announcement_event = node_a.node.get_and_clear_pending_msg_events();
+                       if !announcement_event.is_empty() {
+                               assert_eq!(announcement_event.len(), 1);
+                               if let MessageSendEvent::SendAnnouncementSignatures { .. } = announcement_event[0] {
+                                       //TODO: Test announcement_sigs re-sending
+                               } else { panic!("Unexpected event!"); }
+                       }
+               } else {
+                       assert!(chan_msgs.0.is_none());
+               }
+               if pending_raa.0 {
+                       assert!(chan_msgs.3 == RAACommitmentOrder::RevokeAndACKFirst);
+                       node_a.node.handle_revoke_and_ack(&node_b.node.get_our_node_id(), &chan_msgs.1.unwrap()).unwrap();
+                       assert!(node_a.node.get_and_clear_pending_msg_events().is_empty());
+                       check_added_monitors!(node_a, 1);
+               } else {
+                       assert!(chan_msgs.1.is_none());
+               }
+               if pending_htlc_adds.0 != 0 || pending_htlc_claims.0 != 0 || pending_cell_htlc_claims.0 != 0 || pending_cell_htlc_fails.0 != 0 {
+                       let commitment_update = chan_msgs.2.unwrap();
+                       if pending_htlc_adds.0 != -1 { // We use -1 to denote a response commitment_signed
+                               assert_eq!(commitment_update.update_add_htlcs.len(), pending_htlc_adds.0 as usize);
+                       } else {
+                               assert!(commitment_update.update_add_htlcs.is_empty());
+                       }
+                       assert_eq!(commitment_update.update_fulfill_htlcs.len(), pending_htlc_claims.0 + pending_cell_htlc_claims.0);
+                       assert_eq!(commitment_update.update_fail_htlcs.len(), pending_cell_htlc_fails.0);
+                       assert!(commitment_update.update_fail_malformed_htlcs.is_empty());
+                       for update_add in commitment_update.update_add_htlcs {
+                               node_a.node.handle_update_add_htlc(&node_b.node.get_our_node_id(), &update_add).unwrap();
+                       }
+                       for update_fulfill in commitment_update.update_fulfill_htlcs {
+                               node_a.node.handle_update_fulfill_htlc(&node_b.node.get_our_node_id(), &update_fulfill).unwrap();
+                       }
+                       for update_fail in commitment_update.update_fail_htlcs {
+                               node_a.node.handle_update_fail_htlc(&node_b.node.get_our_node_id(), &update_fail).unwrap();
+                       }
+
+                       if pending_htlc_adds.0 != -1 { // We use -1 to denote a response commitment_signed
+                               commitment_signed_dance!(node_a, node_b, commitment_update.commitment_signed, false);
+                       } else {
+                               node_a.node.handle_commitment_signed(&node_b.node.get_our_node_id(), &commitment_update.commitment_signed).unwrap();
+                               check_added_monitors!(node_a, 1);
+                               let as_revoke_and_ack = get_event_msg!(node_a, MessageSendEvent::SendRevokeAndACK, node_b.node.get_our_node_id());
+                               // No commitment_signed so get_event_msg's assert(len == 1) passes
+                               node_b.node.handle_revoke_and_ack(&node_a.node.get_our_node_id(), &as_revoke_and_ack).unwrap();
+                               assert!(node_b.node.get_and_clear_pending_msg_events().is_empty());
+                               check_added_monitors!(node_b, 1);
+                       }
+               } else {
+                       assert!(chan_msgs.2.is_none());
+               }
+       }
+
+       for chan_msgs in resp_2.drain(..) {
+               if send_funding_locked.1 {
+                       node_b.node.handle_funding_locked(&node_a.node.get_our_node_id(), &chan_msgs.0.unwrap()).unwrap();
+                       let announcement_event = node_b.node.get_and_clear_pending_msg_events();
+                       if !announcement_event.is_empty() {
+                               assert_eq!(announcement_event.len(), 1);
+                               if let MessageSendEvent::SendAnnouncementSignatures { .. } = announcement_event[0] {
+                                       //TODO: Test announcement_sigs re-sending
+                               } else { panic!("Unexpected event!"); }
+                       }
+               } else {
+                       assert!(chan_msgs.0.is_none());
+               }
+               if pending_raa.1 {
+                       assert!(chan_msgs.3 == RAACommitmentOrder::RevokeAndACKFirst);
+                       node_b.node.handle_revoke_and_ack(&node_a.node.get_our_node_id(), &chan_msgs.1.unwrap()).unwrap();
+                       assert!(node_b.node.get_and_clear_pending_msg_events().is_empty());
+                       check_added_monitors!(node_b, 1);
+               } else {
+                       assert!(chan_msgs.1.is_none());
+               }
+               if pending_htlc_adds.1 != 0 || pending_htlc_claims.1 != 0 || pending_cell_htlc_claims.1 != 0 || pending_cell_htlc_fails.1 != 0 {
+                       let commitment_update = chan_msgs.2.unwrap();
+                       if pending_htlc_adds.1 != -1 { // We use -1 to denote a response commitment_signed
+                               assert_eq!(commitment_update.update_add_htlcs.len(), pending_htlc_adds.1 as usize);
+                       }
+                       assert_eq!(commitment_update.update_fulfill_htlcs.len(), pending_htlc_claims.0 + pending_cell_htlc_claims.0);
+                       assert_eq!(commitment_update.update_fail_htlcs.len(), pending_cell_htlc_fails.0);
+                       assert!(commitment_update.update_fail_malformed_htlcs.is_empty());
+                       for update_add in commitment_update.update_add_htlcs {
+                               node_b.node.handle_update_add_htlc(&node_a.node.get_our_node_id(), &update_add).unwrap();
+                       }
+                       for update_fulfill in commitment_update.update_fulfill_htlcs {
+                               node_b.node.handle_update_fulfill_htlc(&node_a.node.get_our_node_id(), &update_fulfill).unwrap();
+                       }
+                       for update_fail in commitment_update.update_fail_htlcs {
+                               node_b.node.handle_update_fail_htlc(&node_a.node.get_our_node_id(), &update_fail).unwrap();
+                       }
+
+                       if pending_htlc_adds.1 != -1 { // We use -1 to denote a response commitment_signed
+                               commitment_signed_dance!(node_b, node_a, commitment_update.commitment_signed, false);
+                       } else {
+                               node_b.node.handle_commitment_signed(&node_a.node.get_our_node_id(), &commitment_update.commitment_signed).unwrap();
+                               check_added_monitors!(node_b, 1);
+                               let bs_revoke_and_ack = get_event_msg!(node_b, MessageSendEvent::SendRevokeAndACK, node_a.node.get_our_node_id());
+                               // No commitment_signed so get_event_msg's assert(len == 1) passes
+                               node_a.node.handle_revoke_and_ack(&node_b.node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
+                               assert!(node_a.node.get_and_clear_pending_msg_events().is_empty());
+                               check_added_monitors!(node_a, 1);
+                       }
+               } else {
+                       assert!(chan_msgs.2.is_none());
+               }
+       }
+}
diff --git a/lightning/src/ln/functional_tests.rs b/lightning/src/ln/functional_tests.rs
new file mode 100644 (file)
index 0000000..044c167
--- /dev/null
@@ -0,0 +1,6114 @@
+//! Tests that test standing up a network of ChannelManagers, creating channels, sending
+//! payments/messages between them, and often checking the resulting ChannelMonitors are able to
+//! claim outputs on-chain.
+
+use chain::transaction::OutPoint;
+use chain::chaininterface::{ChainListener, ChainWatchInterface, ChainWatchInterfaceUtil};
+use chain::keysinterface::{KeysInterface, SpendableOutputDescriptor, KeysManager};
+use chain::keysinterface;
+use ln::channel::{COMMITMENT_TX_BASE_WEIGHT, COMMITMENT_TX_WEIGHT_PER_HTLC};
+use ln::channelmanager::{ChannelManager,ChannelManagerReadArgs,HTLCForwardInfo,RAACommitmentOrder, PaymentPreimage, PaymentHash, BREAKDOWN_TIMEOUT};
+use ln::channelmonitor::{ChannelMonitor, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ManyChannelMonitor, ANTI_REORG_DELAY};
+use ln::channel::{ACCEPTED_HTLC_SCRIPT_WEIGHT, OFFERED_HTLC_SCRIPT_WEIGHT, Channel, ChannelError};
+use ln::onion_utils;
+use ln::router::{Route, RouteHop};
+use ln::msgs;
+use ln::msgs::{ChannelMessageHandler,RoutingMessageHandler,HTLCFailChannelUpdate, LocalFeatures, ErrorAction};
+use util::test_utils;
+use util::events::{Event, EventsProvider, MessageSendEvent, MessageSendEventsProvider};
+use util::errors::APIError;
+use util::ser::{Writeable, ReadableArgs};
+use util::config::UserConfig;
+use util::logger::Logger;
+
+use bitcoin::util::hash::BitcoinHash;
+use bitcoin_hashes::sha256d::Hash as Sha256dHash;
+use bitcoin::util::bip143;
+use bitcoin::util::address::Address;
+use bitcoin::util::bip32::{ChildNumber, ExtendedPubKey, ExtendedPrivKey};
+use bitcoin::blockdata::block::{Block, BlockHeader};
+use bitcoin::blockdata::transaction::{Transaction, TxOut, TxIn, SigHashType, OutPoint as BitcoinOutPoint};
+use bitcoin::blockdata::script::{Builder, Script};
+use bitcoin::blockdata::opcodes;
+use bitcoin::blockdata::constants::genesis_block;
+use bitcoin::network::constants::Network;
+
+use bitcoin_hashes::sha256::Hash as Sha256;
+use bitcoin_hashes::Hash;
+
+use secp256k1::{Secp256k1, Message};
+use secp256k1::key::{PublicKey,SecretKey};
+
+use std::collections::{BTreeSet, HashMap, HashSet};
+use std::default::Default;
+use std::sync::{Arc, Mutex};
+use std::sync::atomic::Ordering;
+use std::mem;
+
+use rand::{thread_rng, Rng};
+
+use ln::functional_test_utils::*;
+
+#[test]
+fn test_insane_channel_opens() {
+       // Stand up a network of 2 nodes
+       let nodes = create_network(2, &[None, None]);
+
+       // Instantiate channel parameters where we push the maximum msats given our
+       // funding satoshis
+       let channel_value_sat = 31337; // same as funding satoshis
+       let channel_reserve_satoshis = Channel::get_our_channel_reserve_satoshis(channel_value_sat);
+       let push_msat = (channel_value_sat - channel_reserve_satoshis) * 1000;
+
+       // Have node0 initiate a channel to node1 with aforementioned parameters
+       nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_sat, push_msat, 42).unwrap();
+
+       // Extract the channel open message from node0 to node1
+       let open_channel_message = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
+
+       // Test helper that asserts we get the correct error string given a mutator
+       // that supposedly makes the channel open message insane
+       let insane_open_helper = |expected_error_str, message_mutator: fn(msgs::OpenChannel) -> msgs::OpenChannel| {
+               match nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), LocalFeatures::new(), &message_mutator(open_channel_message.clone())) {
+                       Err(msgs::HandleError{ err: error_str, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) => {
+                               assert_eq!(error_str, expected_error_str, "unexpected HandleError string (expected `{}`, actual `{}`)", expected_error_str, error_str)
+                       },
+                       Err(msgs::HandleError{..}) => {panic!("unexpected HandleError action")},
+                       _ => panic!("insane OpenChannel message was somehow Ok"),
+               }
+       };
+
+       use ln::channel::MAX_FUNDING_SATOSHIS;
+       use ln::channelmanager::MAX_LOCAL_BREAKDOWN_TIMEOUT;
+
+       // Test all mutations that would make the channel open message insane
+       insane_open_helper("funding value > 2^24", |mut msg| { msg.funding_satoshis = MAX_FUNDING_SATOSHIS; msg });
+
+       insane_open_helper("Bogus channel_reserve_satoshis", |mut msg| { msg.channel_reserve_satoshis = msg.funding_satoshis + 1; msg });
+
+       insane_open_helper("push_msat larger than funding value", |mut msg| { msg.push_msat = (msg.funding_satoshis - msg.channel_reserve_satoshis) * 1000 + 1; msg });
+
+       insane_open_helper("Peer never wants payout outputs?", |mut msg| { msg.dust_limit_satoshis = msg.funding_satoshis + 1 ; msg });
+
+       insane_open_helper("Bogus; channel reserve is less than dust limit", |mut msg| { msg.dust_limit_satoshis = msg.channel_reserve_satoshis + 1; msg });
+
+       insane_open_helper("Minimum htlc value is full channel value", |mut msg| { msg.htlc_minimum_msat = (msg.funding_satoshis - msg.channel_reserve_satoshis) * 1000; msg });
+
+       insane_open_helper("They wanted our payments to be delayed by a needlessly long period", |mut msg| { msg.to_self_delay = MAX_LOCAL_BREAKDOWN_TIMEOUT + 1; msg });
+
+       insane_open_helper("0 max_accpted_htlcs makes for a useless channel", |mut msg| { msg.max_accepted_htlcs = 0; msg });
+
+       insane_open_helper("max_accpted_htlcs > 483", |mut msg| { msg.max_accepted_htlcs = 484; msg });
+}
+
+#[test]
+fn test_async_inbound_update_fee() {
+       let mut nodes = create_network(2, &[None, None]);
+       let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+       let channel_id = chan.2;
+
+       // balancing
+       send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
+
+       // A                                        B
+       // update_fee                            ->
+       // send (1) commitment_signed            -.
+       //                                       <- update_add_htlc/commitment_signed
+       // send (2) RAA (awaiting remote revoke) -.
+       // (1) commitment_signed is delivered    ->
+       //                                       .- send (3) RAA (awaiting remote revoke)
+       // (2) RAA is delivered                  ->
+       //                                       .- send (4) commitment_signed
+       //                                       <- (3) RAA is delivered
+       // send (5) commitment_signed            -.
+       //                                       <- (4) commitment_signed is delivered
+       // send (6) RAA                          -.
+       // (5) commitment_signed is delivered    ->
+       //                                       <- RAA
+       // (6) RAA is delivered                  ->
+
+       // First nodes[0] generates an update_fee
+       nodes[0].node.update_fee(channel_id, get_feerate!(nodes[0], channel_id) + 20).unwrap();
+       check_added_monitors!(nodes[0], 1);
+
+       let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
+       assert_eq!(events_0.len(), 1);
+       let (update_msg, commitment_signed) = match events_0[0] { // (1)
+               MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, ref commitment_signed, .. }, .. } => {
+                       (update_fee.as_ref(), commitment_signed)
+               },
+               _ => panic!("Unexpected event"),
+       };
+
+       nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap()).unwrap();
+
+       // ...but before it's delivered, nodes[1] starts to send a payment back to nodes[0]...
+       let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
+       nodes[1].node.send_payment(nodes[1].router.get_route(&nodes[0].node.get_our_node_id(), None, &Vec::new(), 40000, TEST_FINAL_CLTV).unwrap(), our_payment_hash).unwrap();
+       check_added_monitors!(nodes[1], 1);
+
+       let payment_event = {
+               let mut events_1 = nodes[1].node.get_and_clear_pending_msg_events();
+               assert_eq!(events_1.len(), 1);
+               SendEvent::from_event(events_1.remove(0))
+       };
+       assert_eq!(payment_event.node_id, nodes[0].node.get_our_node_id());
+       assert_eq!(payment_event.msgs.len(), 1);
+
+       // ...now when the messages get delivered everyone should be happy
+       nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
+       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &payment_event.commitment_msg).unwrap(); // (2)
+       let as_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
+       // nodes[0] is awaiting nodes[1] revoke_and_ack so get_event_msg's assert(len == 1) passes
+       check_added_monitors!(nodes[0], 1);
+
+       // deliver(1), generate (3):
+       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed).unwrap();
+       let bs_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
+       // nodes[1] is awaiting nodes[0] revoke_and_ack so get_event_msg's assert(len == 1) passes
+       check_added_monitors!(nodes[1], 1);
+
+       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_and_ack).unwrap(); // deliver (2)
+       let bs_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+       assert!(bs_update.update_add_htlcs.is_empty()); // (4)
+       assert!(bs_update.update_fulfill_htlcs.is_empty()); // (4)
+       assert!(bs_update.update_fail_htlcs.is_empty()); // (4)
+       assert!(bs_update.update_fail_malformed_htlcs.is_empty()); // (4)
+       assert!(bs_update.update_fee.is_none()); // (4)
+       check_added_monitors!(nodes[1], 1);
+
+       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack).unwrap(); // deliver (3)
+       let as_update = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
+       assert!(as_update.update_add_htlcs.is_empty()); // (5)
+       assert!(as_update.update_fulfill_htlcs.is_empty()); // (5)
+       assert!(as_update.update_fail_htlcs.is_empty()); // (5)
+       assert!(as_update.update_fail_malformed_htlcs.is_empty()); // (5)
+       assert!(as_update.update_fee.is_none()); // (5)
+       check_added_monitors!(nodes[0], 1);
+
+       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_update.commitment_signed).unwrap(); // deliver (4)
+       let as_second_revoke = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
+       // only (6) so get_event_msg's assert(len == 1) passes
+       check_added_monitors!(nodes[0], 1);
+
+       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_update.commitment_signed).unwrap(); // deliver (5)
+       let bs_second_revoke = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
+       check_added_monitors!(nodes[1], 1);
+
+       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_revoke).unwrap();
+       check_added_monitors!(nodes[0], 1);
+
+       let events_2 = nodes[0].node.get_and_clear_pending_events();
+       assert_eq!(events_2.len(), 1);
+       match events_2[0] {
+               Event::PendingHTLCsForwardable {..} => {}, // If we actually processed we'd receive the payment
+               _ => panic!("Unexpected event"),
+       }
+
+       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_second_revoke).unwrap(); // deliver (6)
+       check_added_monitors!(nodes[1], 1);
+}
+
+#[test]
+fn test_update_fee_unordered_raa() {
+       // Just the intro to the previous test followed by an out-of-order RAA (which caused a
+       // crash in an earlier version of the update_fee patch)
+       let mut nodes = create_network(2, &[None, None]);
+       let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+       let channel_id = chan.2;
+
+       // balancing
+       send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
+
+       // First nodes[0] generates an update_fee
+       nodes[0].node.update_fee(channel_id, get_feerate!(nodes[0], channel_id) + 20).unwrap();
+       check_added_monitors!(nodes[0], 1);
+
+       let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
+       assert_eq!(events_0.len(), 1);
+       let update_msg = match events_0[0] { // (1)
+               MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, .. }, .. } => {
+                       update_fee.as_ref()
+               },
+               _ => panic!("Unexpected event"),
+       };
+
+       nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap()).unwrap();
+
+       // ...but before it's delivered, nodes[1] starts to send a payment back to nodes[0]...
+       let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
+       nodes[1].node.send_payment(nodes[1].router.get_route(&nodes[0].node.get_our_node_id(), None, &Vec::new(), 40000, TEST_FINAL_CLTV).unwrap(), our_payment_hash).unwrap();
+       check_added_monitors!(nodes[1], 1);
+
+       let payment_event = {
+               let mut events_1 = nodes[1].node.get_and_clear_pending_msg_events();
+               assert_eq!(events_1.len(), 1);
+               SendEvent::from_event(events_1.remove(0))
+       };
+       assert_eq!(payment_event.node_id, nodes[0].node.get_our_node_id());
+       assert_eq!(payment_event.msgs.len(), 1);
+
+       // ...now when the messages get delivered everyone should be happy
+       nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
+       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &payment_event.commitment_msg).unwrap(); // (2)
+       let as_revoke_msg = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
+       // nodes[0] is awaiting nodes[1] revoke_and_ack so get_event_msg's assert(len == 1) passes
+       check_added_monitors!(nodes[0], 1);
+
+       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_msg).unwrap(); // deliver (2)
+       check_added_monitors!(nodes[1], 1);
+
+       // We can't continue, sadly, because our (1) now has a bogus signature
+}
+
+#[test]
+fn test_multi_flight_update_fee() {
+       let nodes = create_network(2, &[None, None]);
+       let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+       let channel_id = chan.2;
+
+       // A                                        B
+       // update_fee/commitment_signed          ->
+       //                                       .- send (1) RAA and (2) commitment_signed
+       // update_fee (never committed)          ->
+       // (3) update_fee                        ->
+       // We have to manually generate the above update_fee, it is allowed by the protocol but we
+       // don't track which updates correspond to which revoke_and_ack responses so we're in
+       // AwaitingRAA mode and will not generate the update_fee yet.
+       //                                       <- (1) RAA delivered
+       // (3) is generated and send (4) CS      -.
+       // Note that A cannot generate (4) prior to (1) being delivered as it otherwise doesn't
+       // know the per_commitment_point to use for it.
+       //                                       <- (2) commitment_signed delivered
+       // revoke_and_ack                        ->
+       //                                          B should send no response here
+       // (4) commitment_signed delivered       ->
+       //                                       <- RAA/commitment_signed delivered
+       // revoke_and_ack                        ->
+
+       // First nodes[0] generates an update_fee
+       let initial_feerate = get_feerate!(nodes[0], channel_id);
+       nodes[0].node.update_fee(channel_id, initial_feerate + 20).unwrap();
+       check_added_monitors!(nodes[0], 1);
+
+       let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
+       assert_eq!(events_0.len(), 1);
+       let (update_msg_1, commitment_signed_1) = match events_0[0] { // (1)
+               MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, ref commitment_signed, .. }, .. } => {
+                       (update_fee.as_ref().unwrap(), commitment_signed)
+               },
+               _ => panic!("Unexpected event"),
+       };
+
+       // Deliver first update_fee/commitment_signed pair, generating (1) and (2):
+       nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg_1).unwrap();
+       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed_1).unwrap();
+       let (bs_revoke_msg, bs_commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+       check_added_monitors!(nodes[1], 1);
+
+       // nodes[0] is awaiting a revoke from nodes[1] before it will create a new commitment
+       // transaction:
+       nodes[0].node.update_fee(channel_id, initial_feerate + 40).unwrap();
+       assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
+       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
+
+       // Create the (3) update_fee message that nodes[0] will generate before it does...
+       let mut update_msg_2 = msgs::UpdateFee {
+               channel_id: update_msg_1.channel_id.clone(),
+               feerate_per_kw: (initial_feerate + 30) as u32,
+       };
+
+       nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &update_msg_2).unwrap();
+
+       update_msg_2.feerate_per_kw = (initial_feerate + 40) as u32;
+       // Deliver (3)
+       nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &update_msg_2).unwrap();
+
+       // Deliver (1), generating (3) and (4)
+       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_msg).unwrap();
+       let as_second_update = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
+       check_added_monitors!(nodes[0], 1);
+       assert!(as_second_update.update_add_htlcs.is_empty());
+       assert!(as_second_update.update_fulfill_htlcs.is_empty());
+       assert!(as_second_update.update_fail_htlcs.is_empty());
+       assert!(as_second_update.update_fail_malformed_htlcs.is_empty());
+       // Check that the update_fee newly generated matches what we delivered:
+       assert_eq!(as_second_update.update_fee.as_ref().unwrap().channel_id, update_msg_2.channel_id);
+       assert_eq!(as_second_update.update_fee.as_ref().unwrap().feerate_per_kw, update_msg_2.feerate_per_kw);
+
+       // Deliver (2) commitment_signed
+       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_commitment_signed).unwrap();
+       let as_revoke_msg = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
+       check_added_monitors!(nodes[0], 1);
+       // No commitment_signed so get_event_msg's assert(len == 1) passes
+
+       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_msg).unwrap();
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+       check_added_monitors!(nodes[1], 1);
+
+       // Delever (4)
+       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_second_update.commitment_signed).unwrap();
+       let (bs_second_revoke, bs_second_commitment) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+       check_added_monitors!(nodes[1], 1);
+
+       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_revoke).unwrap();
+       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
+       check_added_monitors!(nodes[0], 1);
+
+       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_second_commitment).unwrap();
+       let as_second_revoke = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
+       // No commitment_signed so get_event_msg's assert(len == 1) passes
+       check_added_monitors!(nodes[0], 1);
+
+       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_second_revoke).unwrap();
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+       check_added_monitors!(nodes[1], 1);
+}
+
+#[test]
+fn test_update_fee_vanilla() {
+       let nodes = create_network(2, &[None, None]);
+       let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+       let channel_id = chan.2;
+
+       let feerate = get_feerate!(nodes[0], channel_id);
+       nodes[0].node.update_fee(channel_id, feerate+25).unwrap();
+       check_added_monitors!(nodes[0], 1);
+
+       let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
+       assert_eq!(events_0.len(), 1);
+       let (update_msg, commitment_signed) = match events_0[0] {
+                       MessageSendEvent::UpdateHTLCs { node_id:_, updates: msgs::CommitmentUpdate { update_add_htlcs:_, update_fulfill_htlcs:_, update_fail_htlcs:_, update_fail_malformed_htlcs:_, ref update_fee, ref commitment_signed } } => {
+                       (update_fee.as_ref(), commitment_signed)
+               },
+               _ => panic!("Unexpected event"),
+       };
+       nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap()).unwrap();
+
+       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed).unwrap();
+       let (revoke_msg, commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+       check_added_monitors!(nodes[1], 1);
+
+       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke_msg).unwrap();
+       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
+       check_added_monitors!(nodes[0], 1);
+
+       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed).unwrap();
+       let revoke_msg = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
+       // No commitment_signed so get_event_msg's assert(len == 1) passes
+       check_added_monitors!(nodes[0], 1);
+
+       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke_msg).unwrap();
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+       check_added_monitors!(nodes[1], 1);
+}
+
+#[test]
+fn test_update_fee_that_funder_cannot_afford() {
+       let nodes = create_network(2, &[None, None]);
+       let channel_value = 1888;
+       let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, channel_value, 700000, LocalFeatures::new(), LocalFeatures::new());
+       let channel_id = chan.2;
+
+       let feerate = 260;
+       nodes[0].node.update_fee(channel_id, feerate).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       let update_msg = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
+
+       nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &update_msg.update_fee.unwrap()).unwrap();
+
+       commitment_signed_dance!(nodes[1], nodes[0], update_msg.commitment_signed, false);
+
+       //Confirm that the new fee based on the last local commitment txn is what we expected based on the feerate of 260 set above.
+       //This value results in a fee that is exactly what the funder can afford (277 sat + 1000 sat channel reserve)
+       {
+               let chan_lock = nodes[1].node.channel_state.lock().unwrap();
+               let chan = chan_lock.by_id.get(&channel_id).unwrap();
+
+               //We made sure neither party's funds are below the dust limit so -2 non-HTLC txns from number of outputs
+               let num_htlcs = chan.last_local_commitment_txn[0].output.len() - 2;
+               let total_fee: u64 = feerate * (COMMITMENT_TX_BASE_WEIGHT + (num_htlcs as u64) * COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000;
+               let mut actual_fee = chan.last_local_commitment_txn[0].output.iter().fold(0, |acc, output| acc + output.value);
+               actual_fee = channel_value - actual_fee;
+               assert_eq!(total_fee, actual_fee);
+       } //drop the mutex
+
+       //Add 2 to the previous fee rate to the final fee increases by 1 (with no HTLCs the fee is essentially
+       //fee_rate*(724/1000) so the increment of 1*0.724 is rounded back down)
+       nodes[0].node.update_fee(channel_id, feerate+2).unwrap();
+       check_added_monitors!(nodes[0], 1);
+
+       let update2_msg = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
+
+       nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &update2_msg.update_fee.unwrap()).unwrap();
+
+       //While producing the commitment_signed response after handling a received update_fee request the
+       //check to see if the funder, who sent the update_fee request, can afford the new fee (funder_balance >= fee+channel_reserve)
+       //Should produce and error.
+       let err = nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &update2_msg.commitment_signed).unwrap_err();
+
+       assert!(match err.err {
+               "Funding remote cannot afford proposed new fee" => true,
+               _ => false,
+       });
+
+       //clear the message we could not handle
+       nodes[1].node.get_and_clear_pending_msg_events();
+}
+
+#[test]
+fn test_update_fee_with_fundee_update_add_htlc() {
+       let mut nodes = create_network(2, &[None, None]);
+       let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+       let channel_id = chan.2;
+
+       // balancing
+       send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
+
+       let feerate = get_feerate!(nodes[0], channel_id);
+       nodes[0].node.update_fee(channel_id, feerate+20).unwrap();
+       check_added_monitors!(nodes[0], 1);
+
+       let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
+       assert_eq!(events_0.len(), 1);
+       let (update_msg, commitment_signed) = match events_0[0] {
+                       MessageSendEvent::UpdateHTLCs { node_id:_, updates: msgs::CommitmentUpdate { update_add_htlcs:_, update_fulfill_htlcs:_, update_fail_htlcs:_, update_fail_malformed_htlcs:_, ref update_fee, ref commitment_signed } } => {
+                       (update_fee.as_ref(), commitment_signed)
+               },
+               _ => panic!("Unexpected event"),
+       };
+       nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap()).unwrap();
+       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed).unwrap();
+       let (revoke_msg, commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+       check_added_monitors!(nodes[1], 1);
+
+       let route = nodes[1].router.get_route(&nodes[0].node.get_our_node_id(), None, &Vec::new(), 800000, TEST_FINAL_CLTV).unwrap();
+
+       let (our_payment_preimage, our_payment_hash) = get_payment_preimage_hash!(nodes[1]);
+
+       // nothing happens since node[1] is in AwaitingRemoteRevoke
+       nodes[1].node.send_payment(route, our_payment_hash).unwrap();
+       {
+               let mut added_monitors = nodes[0].chan_monitor.added_monitors.lock().unwrap();
+               assert_eq!(added_monitors.len(), 0);
+               added_monitors.clear();
+       }
+       assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
+       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
+       // node[1] has nothing to do
+
+       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke_msg).unwrap();
+       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
+       check_added_monitors!(nodes[0], 1);
+
+       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed).unwrap();
+       let revoke_msg = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
+       // No commitment_signed so get_event_msg's assert(len == 1) passes
+       check_added_monitors!(nodes[0], 1);
+       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke_msg).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       // AwaitingRemoteRevoke ends here
+
+       let commitment_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+       assert_eq!(commitment_update.update_add_htlcs.len(), 1);
+       assert_eq!(commitment_update.update_fulfill_htlcs.len(), 0);
+       assert_eq!(commitment_update.update_fail_htlcs.len(), 0);
+       assert_eq!(commitment_update.update_fail_malformed_htlcs.len(), 0);
+       assert_eq!(commitment_update.update_fee.is_none(), true);
+
+       nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &commitment_update.update_add_htlcs[0]).unwrap();
+       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_update.commitment_signed).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       let (revoke, commitment_signed) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
+
+       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+
+       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &commitment_signed).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       let revoke = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
+       // No commitment_signed so get_event_msg's assert(len == 1) passes
+
+       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
+
+       expect_pending_htlcs_forwardable!(nodes[0]);
+
+       let events = nodes[0].node.get_and_clear_pending_events();
+       assert_eq!(events.len(), 1);
+       match events[0] {
+               Event::PaymentReceived { .. } => { },
+               _ => panic!("Unexpected event"),
+       };
+
+       claim_payment(&nodes[1], &vec!(&nodes[0])[..], our_payment_preimage);
+
+       send_payment(&nodes[1], &vec!(&nodes[0])[..], 800000);
+       send_payment(&nodes[0], &vec!(&nodes[1])[..], 800000);
+       close_channel(&nodes[0], &nodes[1], &chan.2, chan.3, true);
+}
+
+#[test]
+fn test_update_fee() {
+       let nodes = create_network(2, &[None, None]);
+       let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+       let channel_id = chan.2;
+
+       // A                                        B
+       // (1) update_fee/commitment_signed      ->
+       //                                       <- (2) revoke_and_ack
+       //                                       .- send (3) commitment_signed
+       // (4) update_fee/commitment_signed      ->
+       //                                       .- send (5) revoke_and_ack (no CS as we're awaiting a revoke)
+       //                                       <- (3) commitment_signed delivered
+       // send (6) revoke_and_ack               -.
+       //                                       <- (5) deliver revoke_and_ack
+       // (6) deliver revoke_and_ack            ->
+       //                                       .- send (7) commitment_signed in response to (4)
+       //                                       <- (7) deliver commitment_signed
+       // revoke_and_ack                        ->
+
+       // Create and deliver (1)...
+       let feerate = get_feerate!(nodes[0], channel_id);
+       nodes[0].node.update_fee(channel_id, feerate+20).unwrap();
+       check_added_monitors!(nodes[0], 1);
+
+       let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
+       assert_eq!(events_0.len(), 1);
+       let (update_msg, commitment_signed) = match events_0[0] {
+                       MessageSendEvent::UpdateHTLCs { node_id:_, updates: msgs::CommitmentUpdate { update_add_htlcs:_, update_fulfill_htlcs:_, update_fail_htlcs:_, update_fail_malformed_htlcs:_, ref update_fee, ref commitment_signed } } => {
+                       (update_fee.as_ref(), commitment_signed)
+               },
+               _ => panic!("Unexpected event"),
+       };
+       nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap()).unwrap();
+
+       // Generate (2) and (3):
+       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed).unwrap();
+       let (revoke_msg, commitment_signed_0) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+       check_added_monitors!(nodes[1], 1);
+
+       // Deliver (2):
+       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke_msg).unwrap();
+       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
+       check_added_monitors!(nodes[0], 1);
+
+       // Create and deliver (4)...
+       nodes[0].node.update_fee(channel_id, feerate+30).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
+       assert_eq!(events_0.len(), 1);
+       let (update_msg, commitment_signed) = match events_0[0] {
+                       MessageSendEvent::UpdateHTLCs { node_id:_, updates: msgs::CommitmentUpdate { update_add_htlcs:_, update_fulfill_htlcs:_, update_fail_htlcs:_, update_fail_malformed_htlcs:_, ref update_fee, ref commitment_signed } } => {
+                       (update_fee.as_ref(), commitment_signed)
+               },
+               _ => panic!("Unexpected event"),
+       };
+
+       nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap()).unwrap();
+       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       // ... creating (5)
+       let revoke_msg = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
+       // No commitment_signed so get_event_msg's assert(len == 1) passes
+
+       // Handle (3), creating (6):
+       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed_0).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       let revoke_msg_0 = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
+       // No commitment_signed so get_event_msg's assert(len == 1) passes
+
+       // Deliver (5):
+       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke_msg).unwrap();
+       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
+       check_added_monitors!(nodes[0], 1);
+
+       // Deliver (6), creating (7):
+       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke_msg_0).unwrap();
+       let commitment_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+       assert!(commitment_update.update_add_htlcs.is_empty());
+       assert!(commitment_update.update_fulfill_htlcs.is_empty());
+       assert!(commitment_update.update_fail_htlcs.is_empty());
+       assert!(commitment_update.update_fail_malformed_htlcs.is_empty());
+       assert!(commitment_update.update_fee.is_none());
+       check_added_monitors!(nodes[1], 1);
+
+       // Deliver (7)
+       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_update.commitment_signed).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       let revoke_msg = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
+       // No commitment_signed so get_event_msg's assert(len == 1) passes
+
+       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke_msg).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+
+       assert_eq!(get_feerate!(nodes[0], channel_id), feerate + 30);
+       assert_eq!(get_feerate!(nodes[1], channel_id), feerate + 30);
+       close_channel(&nodes[0], &nodes[1], &chan.2, chan.3, true);
+}
+
+#[test]
+fn pre_funding_lock_shutdown_test() {
+       // Test sending a shutdown prior to funding_locked after funding generation
+       let nodes = create_network(2, &[None, None]);
+       let tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 8000000, 0, LocalFeatures::new(), LocalFeatures::new());
+       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+       nodes[0].chain_monitor.block_connected_checked(&header, 1, &[&tx; 1], &[1; 1]);
+       nodes[1].chain_monitor.block_connected_checked(&header, 1, &[&tx; 1], &[1; 1]);
+
+       nodes[0].node.close_channel(&OutPoint::new(tx.txid(), 0).to_channel_id()).unwrap();
+       let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
+       nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown).unwrap();
+       let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
+       nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown).unwrap();
+
+       let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
+       nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed).unwrap();
+       let (_, node_1_closing_signed) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
+       nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed.unwrap()).unwrap();
+       let (_, node_0_none) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
+       assert!(node_0_none.is_none());
+
+       assert!(nodes[0].node.list_channels().is_empty());
+       assert!(nodes[1].node.list_channels().is_empty());
+}
+
+#[test]
+fn updates_shutdown_wait() {
+       // Test sending a shutdown with outstanding updates pending
+       let mut nodes = create_network(3, &[None, None, None]);
+       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+       let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
+       let route_1 = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap();
+       let route_2 = nodes[1].router.get_route(&nodes[0].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap();
+
+       let (our_payment_preimage, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 100000);
+
+       nodes[0].node.close_channel(&chan_1.2).unwrap();
+       let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
+       nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown).unwrap();
+       let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
+       nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown).unwrap();
+
+       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+
+       let (_, payment_hash) = get_payment_preimage_hash!(nodes[0]);
+       if let Err(APIError::ChannelUnavailable {..}) = nodes[0].node.send_payment(route_1, payment_hash) {}
+       else { panic!("New sends should fail!") };
+       if let Err(APIError::ChannelUnavailable {..}) = nodes[1].node.send_payment(route_2, payment_hash) {}
+       else { panic!("New sends should fail!") };
+
+       assert!(nodes[2].node.claim_funds(our_payment_preimage));
+       check_added_monitors!(nodes[2], 1);
+       let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
+       assert!(updates.update_add_htlcs.is_empty());
+       assert!(updates.update_fail_htlcs.is_empty());
+       assert!(updates.update_fail_malformed_htlcs.is_empty());
+       assert!(updates.update_fee.is_none());
+       assert_eq!(updates.update_fulfill_htlcs.len(), 1);
+       nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       let updates_2 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+       commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false);
+
+       assert!(updates_2.update_add_htlcs.is_empty());
+       assert!(updates_2.update_fail_htlcs.is_empty());
+       assert!(updates_2.update_fail_malformed_htlcs.is_empty());
+       assert!(updates_2.update_fee.is_none());
+       assert_eq!(updates_2.update_fulfill_htlcs.len(), 1);
+       nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates_2.update_fulfill_htlcs[0]).unwrap();
+       commitment_signed_dance!(nodes[0], nodes[1], updates_2.commitment_signed, false, true);
+
+       let events = nodes[0].node.get_and_clear_pending_events();
+       assert_eq!(events.len(), 1);
+       match events[0] {
+               Event::PaymentSent { ref payment_preimage } => {
+                       assert_eq!(our_payment_preimage, *payment_preimage);
+               },
+               _ => panic!("Unexpected event"),
+       }
+
+       let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
+       nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed).unwrap();
+       let (_, node_1_closing_signed) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
+       nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed.unwrap()).unwrap();
+       let (_, node_0_none) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
+       assert!(node_0_none.is_none());
+
+       assert!(nodes[0].node.list_channels().is_empty());
+
+       assert_eq!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
+       nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
+       close_channel(&nodes[1], &nodes[2], &chan_2.2, chan_2.3, true);
+       assert!(nodes[1].node.list_channels().is_empty());
+       assert!(nodes[2].node.list_channels().is_empty());
+}
+
+#[test]
+fn htlc_fail_async_shutdown() {
+       // Test HTLCs fail if shutdown starts even if messages are delivered out-of-order
+       let mut nodes = create_network(3, &[None, None, None]);
+       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+       let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
+
+       let route = nodes[0].router.get_route(&nodes[2].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap();
+       let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
+       nodes[0].node.send_payment(route, our_payment_hash).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
+       assert_eq!(updates.update_add_htlcs.len(), 1);
+       assert!(updates.update_fulfill_htlcs.is_empty());
+       assert!(updates.update_fail_htlcs.is_empty());
+       assert!(updates.update_fail_malformed_htlcs.is_empty());
+       assert!(updates.update_fee.is_none());
+
+       nodes[1].node.close_channel(&chan_1.2).unwrap();
+       let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
+       nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown).unwrap();
+       let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
+
+       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]).unwrap();
+       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &updates.commitment_signed).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown).unwrap();
+       commitment_signed_dance!(nodes[1], nodes[0], (), false, true, false);
+
+       let updates_2 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+       assert!(updates_2.update_add_htlcs.is_empty());
+       assert!(updates_2.update_fulfill_htlcs.is_empty());
+       assert_eq!(updates_2.update_fail_htlcs.len(), 1);
+       assert!(updates_2.update_fail_malformed_htlcs.is_empty());
+       assert!(updates_2.update_fee.is_none());
+
+       nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates_2.update_fail_htlcs[0]).unwrap();
+       commitment_signed_dance!(nodes[0], nodes[1], updates_2.commitment_signed, false, true);
+
+       let events = nodes[0].node.get_and_clear_pending_events();
+       assert_eq!(events.len(), 1);
+       match events[0] {
+               Event::PaymentFailed { ref payment_hash, ref rejected_by_dest, .. } => {
+                       assert_eq!(our_payment_hash, *payment_hash);
+                       assert!(!rejected_by_dest);
+               },
+               _ => panic!("Unexpected event"),
+       }
+
+       let msg_events = nodes[0].node.get_and_clear_pending_msg_events();
+       assert_eq!(msg_events.len(), 2);
+       let node_0_closing_signed = match msg_events[0] {
+               MessageSendEvent::SendClosingSigned { ref node_id, ref msg } => {
+                       assert_eq!(*node_id, nodes[1].node.get_our_node_id());
+                       (*msg).clone()
+               },
+               _ => panic!("Unexpected event"),
+       };
+       match msg_events[1] {
+               MessageSendEvent::PaymentFailureNetworkUpdate { update: msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg }} => {
+                       assert_eq!(msg.contents.short_channel_id, chan_1.0.contents.short_channel_id);
+               },
+               _ => panic!("Unexpected event"),
+       }
+
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+       nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed).unwrap();
+       let (_, node_1_closing_signed) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
+       nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed.unwrap()).unwrap();
+       let (_, node_0_none) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
+       assert!(node_0_none.is_none());
+
+       assert!(nodes[0].node.list_channels().is_empty());
+
+       assert_eq!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
+       nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
+       close_channel(&nodes[1], &nodes[2], &chan_2.2, chan_2.3, true);
+       assert!(nodes[1].node.list_channels().is_empty());
+       assert!(nodes[2].node.list_channels().is_empty());
+}
+
+fn do_test_shutdown_rebroadcast(recv_count: u8) {
+       // Test that shutdown/closing_signed is re-sent on reconnect with a variable number of
+       // messages delivered prior to disconnect
+       let nodes = create_network(3, &[None, None, None]);
+       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+       let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
+
+       let (our_payment_preimage, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 100000);
+
+       nodes[1].node.close_channel(&chan_1.2).unwrap();
+       let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
+       if recv_count > 0 {
+               nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown).unwrap();
+               let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
+               if recv_count > 1 {
+                       nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown).unwrap();
+               }
+       }
+
+       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
+       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
+
+       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id());
+       let node_0_reestablish = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id());
+       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id());
+       let node_1_reestablish = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id());
+
+       nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &node_0_reestablish).unwrap();
+       let node_1_2nd_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
+       assert!(node_1_shutdown == node_1_2nd_shutdown);
+
+       nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &node_1_reestablish).unwrap();
+       let node_0_2nd_shutdown = if recv_count > 0 {
+               let node_0_2nd_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
+               nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_2nd_shutdown).unwrap();
+               node_0_2nd_shutdown
+       } else {
+               assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
+               nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_2nd_shutdown).unwrap();
+               get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id())
+       };
+       nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_2nd_shutdown).unwrap();
+
+       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+
+       assert!(nodes[2].node.claim_funds(our_payment_preimage));
+       check_added_monitors!(nodes[2], 1);
+       let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
+       assert!(updates.update_add_htlcs.is_empty());
+       assert!(updates.update_fail_htlcs.is_empty());
+       assert!(updates.update_fail_malformed_htlcs.is_empty());
+       assert!(updates.update_fee.is_none());
+       assert_eq!(updates.update_fulfill_htlcs.len(), 1);
+       nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       let updates_2 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+       commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false);
+
+       assert!(updates_2.update_add_htlcs.is_empty());
+       assert!(updates_2.update_fail_htlcs.is_empty());
+       assert!(updates_2.update_fail_malformed_htlcs.is_empty());
+       assert!(updates_2.update_fee.is_none());
+       assert_eq!(updates_2.update_fulfill_htlcs.len(), 1);
+       nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates_2.update_fulfill_htlcs[0]).unwrap();
+       commitment_signed_dance!(nodes[0], nodes[1], updates_2.commitment_signed, false, true);
+
+       let events = nodes[0].node.get_and_clear_pending_events();
+       assert_eq!(events.len(), 1);
+       match events[0] {
+               Event::PaymentSent { ref payment_preimage } => {
+                       assert_eq!(our_payment_preimage, *payment_preimage);
+               },
+               _ => panic!("Unexpected event"),
+       }
+
+       let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
+       if recv_count > 0 {
+               nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed).unwrap();
+               let (_, node_1_closing_signed) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
+               assert!(node_1_closing_signed.is_some());
+       }
+
+       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
+       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
+
+       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id());
+       let node_0_2nd_reestablish = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id());
+       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id());
+       if recv_count == 0 {
+               // If all closing_signeds weren't delivered we can just resume where we left off...
+               let node_1_2nd_reestablish = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id());
+
+               nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &node_1_2nd_reestablish).unwrap();
+               let node_0_3rd_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
+               assert!(node_0_2nd_shutdown == node_0_3rd_shutdown);
+
+               nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &node_0_2nd_reestablish).unwrap();
+               let node_1_3rd_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
+               assert!(node_1_3rd_shutdown == node_1_2nd_shutdown);
+
+               nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_3rd_shutdown).unwrap();
+               assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+
+               nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_3rd_shutdown).unwrap();
+               let node_0_2nd_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
+               assert!(node_0_closing_signed == node_0_2nd_closing_signed);
+
+               nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed).unwrap();
+               let (_, node_1_closing_signed) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
+               nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed.unwrap()).unwrap();
+               let (_, node_0_none) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
+               assert!(node_0_none.is_none());
+       } else {
+               // If one node, however, received + responded with an identical closing_signed we end
+               // up erroring and node[0] will try to broadcast its own latest commitment transaction.
+               // There isn't really anything better we can do simply, but in the future we might
+               // explore storing a set of recently-closed channels that got disconnected during
+               // closing_signed and avoiding broadcasting local commitment txn for some timeout to
+               // give our counterparty enough time to (potentially) broadcast a cooperative closing
+               // transaction.
+               assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+
+               if let Err(msgs::HandleError{action: Some(msgs::ErrorAction::SendErrorMessage{msg}), ..}) =
+                               nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &node_0_2nd_reestablish) {
+                       nodes[0].node.handle_error(&nodes[1].node.get_our_node_id(), &msg);
+                       let msgs::ErrorMessage {ref channel_id, ..} = msg;
+                       assert_eq!(*channel_id, chan_1.2);
+               } else { panic!("Needed SendErrorMessage close"); }
+
+               // get_closing_signed_broadcast usually eats the BroadcastChannelUpdate for us and
+               // checks it, but in this case nodes[0] didn't ever get a chance to receive a
+               // closing_signed so we do it ourselves
+               check_closed_broadcast!(nodes[0]);
+       }
+
+       assert!(nodes[0].node.list_channels().is_empty());
+
+       assert_eq!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
+       nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
+       close_channel(&nodes[1], &nodes[2], &chan_2.2, chan_2.3, true);
+       assert!(nodes[1].node.list_channels().is_empty());
+       assert!(nodes[2].node.list_channels().is_empty());
+}
+
+#[test]
+fn test_shutdown_rebroadcast() {
+       do_test_shutdown_rebroadcast(0);
+       do_test_shutdown_rebroadcast(1);
+       do_test_shutdown_rebroadcast(2);
+}
+
+#[test]
+fn fake_network_test() {
+       // Simple test which builds a network of ChannelManagers, connects them to each other, and
+       // tests that payments get routed and transactions broadcast in semi-reasonable ways.
+       let nodes = create_network(4, &[None, None, None, None]);
+
+       // Create some initial channels
+       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+       let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
+       let chan_3 = create_announced_chan_between_nodes(&nodes, 2, 3, LocalFeatures::new(), LocalFeatures::new());
+
+       // Rebalance the network a bit by relaying one payment through all the channels...
+       send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 8000000);
+       send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 8000000);
+       send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 8000000);
+       send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 8000000);
+
+       // Send some more payments
+       send_payment(&nodes[1], &vec!(&nodes[2], &nodes[3])[..], 1000000);
+       send_payment(&nodes[3], &vec!(&nodes[2], &nodes[1], &nodes[0])[..], 1000000);
+       send_payment(&nodes[3], &vec!(&nodes[2], &nodes[1])[..], 1000000);
+
+       // Test failure packets
+       let payment_hash_1 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 1000000).1;
+       fail_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], payment_hash_1);
+
+       // Add a new channel that skips 3
+       let chan_4 = create_announced_chan_between_nodes(&nodes, 1, 3, LocalFeatures::new(), LocalFeatures::new());
+
+       send_payment(&nodes[0], &vec!(&nodes[1], &nodes[3])[..], 1000000);
+       send_payment(&nodes[2], &vec!(&nodes[3])[..], 1000000);
+       send_payment(&nodes[1], &vec!(&nodes[3])[..], 8000000);
+       send_payment(&nodes[1], &vec!(&nodes[3])[..], 8000000);
+       send_payment(&nodes[1], &vec!(&nodes[3])[..], 8000000);
+       send_payment(&nodes[1], &vec!(&nodes[3])[..], 8000000);
+       send_payment(&nodes[1], &vec!(&nodes[3])[..], 8000000);
+
+       // Do some rebalance loop payments, simultaneously
+       let mut hops = Vec::with_capacity(3);
+       hops.push(RouteHop {
+               pubkey: nodes[2].node.get_our_node_id(),
+               short_channel_id: chan_2.0.contents.short_channel_id,
+               fee_msat: 0,
+               cltv_expiry_delta: chan_3.0.contents.cltv_expiry_delta as u32
+       });
+       hops.push(RouteHop {
+               pubkey: nodes[3].node.get_our_node_id(),
+               short_channel_id: chan_3.0.contents.short_channel_id,
+               fee_msat: 0,
+               cltv_expiry_delta: chan_4.1.contents.cltv_expiry_delta as u32
+       });
+       hops.push(RouteHop {
+               pubkey: nodes[1].node.get_our_node_id(),
+               short_channel_id: chan_4.0.contents.short_channel_id,
+               fee_msat: 1000000,
+               cltv_expiry_delta: TEST_FINAL_CLTV,
+       });
+       hops[1].fee_msat = chan_4.1.contents.fee_base_msat as u64 + chan_4.1.contents.fee_proportional_millionths as u64 * hops[2].fee_msat as u64 / 1000000;
+       hops[0].fee_msat = chan_3.0.contents.fee_base_msat as u64 + chan_3.0.contents.fee_proportional_millionths as u64 * hops[1].fee_msat as u64 / 1000000;
+       let payment_preimage_1 = send_along_route(&nodes[1], Route { hops }, &vec!(&nodes[2], &nodes[3], &nodes[1])[..], 1000000).0;
+
+       let mut hops = Vec::with_capacity(3);
+       hops.push(RouteHop {
+               pubkey: nodes[3].node.get_our_node_id(),
+               short_channel_id: chan_4.0.contents.short_channel_id,
+               fee_msat: 0,
+               cltv_expiry_delta: chan_3.1.contents.cltv_expiry_delta as u32
+       });
+       hops.push(RouteHop {
+               pubkey: nodes[2].node.get_our_node_id(),
+               short_channel_id: chan_3.0.contents.short_channel_id,
+               fee_msat: 0,
+               cltv_expiry_delta: chan_2.1.contents.cltv_expiry_delta as u32
+       });
+       hops.push(RouteHop {
+               pubkey: nodes[1].node.get_our_node_id(),
+               short_channel_id: chan_2.0.contents.short_channel_id,
+               fee_msat: 1000000,
+               cltv_expiry_delta: TEST_FINAL_CLTV,
+       });
+       hops[1].fee_msat = chan_2.1.contents.fee_base_msat as u64 + chan_2.1.contents.fee_proportional_millionths as u64 * hops[2].fee_msat as u64 / 1000000;
+       hops[0].fee_msat = chan_3.1.contents.fee_base_msat as u64 + chan_3.1.contents.fee_proportional_millionths as u64 * hops[1].fee_msat as u64 / 1000000;
+       let payment_hash_2 = send_along_route(&nodes[1], Route { hops }, &vec!(&nodes[3], &nodes[2], &nodes[1])[..], 1000000).1;
+
+       // Claim the rebalances...
+       fail_payment(&nodes[1], &vec!(&nodes[3], &nodes[2], &nodes[1])[..], payment_hash_2);
+       claim_payment(&nodes[1], &vec!(&nodes[2], &nodes[3], &nodes[1])[..], payment_preimage_1);
+
+       // Add a duplicate new channel from 2 to 4
+       let chan_5 = create_announced_chan_between_nodes(&nodes, 1, 3, LocalFeatures::new(), LocalFeatures::new());
+
+       // Send some payments across both channels
+       let payment_preimage_3 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[3])[..], 3000000).0;
+       let payment_preimage_4 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[3])[..], 3000000).0;
+       let payment_preimage_5 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[3])[..], 3000000).0;
+
+       route_over_limit(&nodes[0], &vec!(&nodes[1], &nodes[3])[..], 3000000);
+
+       //TODO: Test that routes work again here as we've been notified that the channel is full
+
+       claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[3])[..], payment_preimage_3);
+       claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[3])[..], payment_preimage_4);
+       claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[3])[..], payment_preimage_5);
+
+       // Close down the channels...
+       close_channel(&nodes[0], &nodes[1], &chan_1.2, chan_1.3, true);
+       close_channel(&nodes[1], &nodes[2], &chan_2.2, chan_2.3, false);
+       close_channel(&nodes[2], &nodes[3], &chan_3.2, chan_3.3, true);
+       close_channel(&nodes[1], &nodes[3], &chan_4.2, chan_4.3, false);
+       close_channel(&nodes[1], &nodes[3], &chan_5.2, chan_5.3, false);
+}
+
+#[test]
+fn holding_cell_htlc_counting() {
+       // Tests that HTLCs in the holding cell count towards the pending HTLC limits on outbound HTLCs
+       // to ensure we don't end up with HTLCs sitting around in our holding cell for several
+       // commitment dance rounds.
+       let mut nodes = create_network(3, &[None, None, None]);
+       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+       let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
+
+       let mut payments = Vec::new();
+       for _ in 0..::ln::channel::OUR_MAX_HTLCS {
+               let route = nodes[1].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 100000, TEST_FINAL_CLTV).unwrap();
+               let (payment_preimage, payment_hash) = get_payment_preimage_hash!(nodes[0]);
+               nodes[1].node.send_payment(route, payment_hash).unwrap();
+               payments.push((payment_preimage, payment_hash));
+       }
+       check_added_monitors!(nodes[1], 1);
+
+       let mut events = nodes[1].node.get_and_clear_pending_msg_events();
+       assert_eq!(events.len(), 1);
+       let initial_payment_event = SendEvent::from_event(events.pop().unwrap());
+       assert_eq!(initial_payment_event.node_id, nodes[2].node.get_our_node_id());
+
+       // There is now one HTLC in an outbound commitment transaction and (OUR_MAX_HTLCS - 1) HTLCs in
+       // the holding cell waiting on B's RAA to send. At this point we should not be able to add
+       // another HTLC.
+       let route = nodes[1].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 100000, TEST_FINAL_CLTV).unwrap();
+       let (_, payment_hash_1) = get_payment_preimage_hash!(nodes[0]);
+       if let APIError::ChannelUnavailable { err } = nodes[1].node.send_payment(route, payment_hash_1).unwrap_err() {
+               assert_eq!(err, "Cannot push more than their max accepted HTLCs");
+       } else { panic!("Unexpected event"); }
+
+       // This should also be true if we try to forward a payment.
+       let route = nodes[0].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 100000, TEST_FINAL_CLTV).unwrap();
+       let (_, payment_hash_2) = get_payment_preimage_hash!(nodes[0]);
+       nodes[0].node.send_payment(route, payment_hash_2).unwrap();
+       check_added_monitors!(nodes[0], 1);
+
+       let mut events = nodes[0].node.get_and_clear_pending_msg_events();
+       assert_eq!(events.len(), 1);
+       let payment_event = SendEvent::from_event(events.pop().unwrap());
+       assert_eq!(payment_event.node_id, nodes[1].node.get_our_node_id());
+
+       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
+       commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
+       // We have to forward pending HTLCs twice - once tries to forward the payment forward (and
+       // fails), the second will process the resulting failure and fail the HTLC backward.
+       expect_pending_htlcs_forwardable!(nodes[1]);
+       expect_pending_htlcs_forwardable!(nodes[1]);
+       check_added_monitors!(nodes[1], 1);
+
+       let bs_fail_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+       nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_fail_updates.update_fail_htlcs[0]).unwrap();
+       commitment_signed_dance!(nodes[0], nodes[1], bs_fail_updates.commitment_signed, false, true);
+
+       let events = nodes[0].node.get_and_clear_pending_msg_events();
+       assert_eq!(events.len(), 1);
+       match events[0] {
+               MessageSendEvent::PaymentFailureNetworkUpdate { update: msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg }} => {
+                       assert_eq!(msg.contents.short_channel_id, chan_2.0.contents.short_channel_id);
+               },
+               _ => panic!("Unexpected event"),
+       }
+
+       let events = nodes[0].node.get_and_clear_pending_events();
+       assert_eq!(events.len(), 1);
+       match events[0] {
+               Event::PaymentFailed { payment_hash, rejected_by_dest, .. } => {
+                       assert_eq!(payment_hash, payment_hash_2);
+                       assert!(!rejected_by_dest);
+               },
+               _ => panic!("Unexpected event"),
+       }
+
+       // Now forward all the pending HTLCs and claim them back
+       nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &initial_payment_event.msgs[0]).unwrap();
+       nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &initial_payment_event.commitment_msg).unwrap();
+       check_added_monitors!(nodes[2], 1);
+
+       let (bs_revoke_and_ack, bs_commitment_signed) = get_revoke_commit_msgs!(nodes[2], nodes[1].node.get_our_node_id());
+       nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       let as_updates = get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
+
+       nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &bs_commitment_signed).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       let as_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
+
+       for ref update in as_updates.update_add_htlcs.iter() {
+               nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), update).unwrap();
+       }
+       nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &as_updates.commitment_signed).unwrap();
+       check_added_monitors!(nodes[2], 1);
+       nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_raa).unwrap();
+       check_added_monitors!(nodes[2], 1);
+       let (bs_revoke_and_ack, bs_commitment_signed) = get_revoke_commit_msgs!(nodes[2], nodes[1].node.get_our_node_id());
+
+       nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &bs_commitment_signed).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       let as_final_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
+
+       nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_final_raa).unwrap();
+       check_added_monitors!(nodes[2], 1);
+
+       expect_pending_htlcs_forwardable!(nodes[2]);
+
+       let events = nodes[2].node.get_and_clear_pending_events();
+       assert_eq!(events.len(), payments.len());
+       for (event, &(_, ref hash)) in events.iter().zip(payments.iter()) {
+               match event {
+                       &Event::PaymentReceived { ref payment_hash, .. } => {
+                               assert_eq!(*payment_hash, *hash);
+                       },
+                       _ => panic!("Unexpected event"),
+               };
+       }
+
+       for (preimage, _) in payments.drain(..) {
+               claim_payment(&nodes[1], &[&nodes[2]], preimage);
+       }
+
+       send_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1000000);
+}
+
+#[test]
+fn duplicate_htlc_test() {
+       // Test that we accept duplicate payment_hash HTLCs across the network and that
+       // claiming/failing them are all separate and don't affect each other
+       let mut nodes = create_network(6, &[None, None, None, None, None, None]);
+
+       // Create some initial channels to route via 3 to 4/5 from 0/1/2
+       create_announced_chan_between_nodes(&nodes, 0, 3, LocalFeatures::new(), LocalFeatures::new());
+       create_announced_chan_between_nodes(&nodes, 1, 3, LocalFeatures::new(), LocalFeatures::new());
+       create_announced_chan_between_nodes(&nodes, 2, 3, LocalFeatures::new(), LocalFeatures::new());
+       create_announced_chan_between_nodes(&nodes, 3, 4, LocalFeatures::new(), LocalFeatures::new());
+       create_announced_chan_between_nodes(&nodes, 3, 5, LocalFeatures::new(), LocalFeatures::new());
+
+       let (payment_preimage, payment_hash) = route_payment(&nodes[0], &vec!(&nodes[3], &nodes[4])[..], 1000000);
+
+       *nodes[0].network_payment_count.borrow_mut() -= 1;
+       assert_eq!(route_payment(&nodes[1], &vec!(&nodes[3])[..], 1000000).0, payment_preimage);
+
+       *nodes[0].network_payment_count.borrow_mut() -= 1;
+       assert_eq!(route_payment(&nodes[2], &vec!(&nodes[3], &nodes[5])[..], 1000000).0, payment_preimage);
+
+       claim_payment(&nodes[0], &vec!(&nodes[3], &nodes[4])[..], payment_preimage);
+       fail_payment(&nodes[2], &vec!(&nodes[3], &nodes[5])[..], payment_hash);
+       claim_payment(&nodes[1], &vec!(&nodes[3])[..], payment_preimage);
+}
+
+fn do_channel_reserve_test(test_recv: bool) {
+       use ln::msgs::HandleError;
+
+       let mut nodes = create_network(3, &[None, None, None]);
+       let chan_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1900, 1001, LocalFeatures::new(), LocalFeatures::new());
+       let chan_2 = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1900, 1001, LocalFeatures::new(), LocalFeatures::new());
+
+       let mut stat01 = get_channel_value_stat!(nodes[0], chan_1.2);
+       let mut stat11 = get_channel_value_stat!(nodes[1], chan_1.2);
+
+       let mut stat12 = get_channel_value_stat!(nodes[1], chan_2.2);
+       let mut stat22 = get_channel_value_stat!(nodes[2], chan_2.2);
+
+       macro_rules! get_route_and_payment_hash {
+               ($recv_value: expr) => {{
+                       let route = nodes[0].router.get_route(&nodes.last().unwrap().node.get_our_node_id(), None, &Vec::new(), $recv_value, TEST_FINAL_CLTV).unwrap();
+                       let (payment_preimage, payment_hash) = get_payment_preimage_hash!(nodes[0]);
+                       (route, payment_hash, payment_preimage)
+               }}
+       };
+
+       macro_rules! expect_forward {
+               ($node: expr) => {{
+                       let mut events = $node.node.get_and_clear_pending_msg_events();
+                       assert_eq!(events.len(), 1);
+                       check_added_monitors!($node, 1);
+                       let payment_event = SendEvent::from_event(events.remove(0));
+                       payment_event
+               }}
+       }
+
+       let feemsat = 239; // somehow we know?
+       let total_fee_msat = (nodes.len() - 2) as u64 * 239;
+
+       let recv_value_0 = stat01.their_max_htlc_value_in_flight_msat - total_fee_msat;
+
+       // attempt to send amt_msat > their_max_htlc_value_in_flight_msat
+       {
+               let (route, our_payment_hash, _) = get_route_and_payment_hash!(recv_value_0 + 1);
+               assert!(route.hops.iter().rev().skip(1).all(|h| h.fee_msat == feemsat));
+               let err = nodes[0].node.send_payment(route, our_payment_hash).err().unwrap();
+               match err {
+                       APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over the max HTLC value in flight our peer will accept"),
+                       _ => panic!("Unknown error variants"),
+               }
+       }
+
+       let mut htlc_id = 0;
+       // channel reserve is bigger than their_max_htlc_value_in_flight_msat so loop to deplete
+       // nodes[0]'s wealth
+       loop {
+               let amt_msat = recv_value_0 + total_fee_msat;
+               if stat01.value_to_self_msat - amt_msat < stat01.channel_reserve_msat {
+                       break;
+               }
+               send_payment(&nodes[0], &vec![&nodes[1], &nodes[2]][..], recv_value_0);
+               htlc_id += 1;
+
+               let (stat01_, stat11_, stat12_, stat22_) = (
+                       get_channel_value_stat!(nodes[0], chan_1.2),
+                       get_channel_value_stat!(nodes[1], chan_1.2),
+                       get_channel_value_stat!(nodes[1], chan_2.2),
+                       get_channel_value_stat!(nodes[2], chan_2.2),
+               );
+
+               assert_eq!(stat01_.value_to_self_msat, stat01.value_to_self_msat - amt_msat);
+               assert_eq!(stat11_.value_to_self_msat, stat11.value_to_self_msat + amt_msat);
+               assert_eq!(stat12_.value_to_self_msat, stat12.value_to_self_msat - (amt_msat - feemsat));
+               assert_eq!(stat22_.value_to_self_msat, stat22.value_to_self_msat + (amt_msat - feemsat));
+               stat01 = stat01_; stat11 = stat11_; stat12 = stat12_; stat22 = stat22_;
+       }
+
+       {
+               let recv_value = stat01.value_to_self_msat - stat01.channel_reserve_msat - total_fee_msat;
+               // attempt to get channel_reserve violation
+               let (route, our_payment_hash, _) = get_route_and_payment_hash!(recv_value + 1);
+               let err = nodes[0].node.send_payment(route.clone(), our_payment_hash).err().unwrap();
+               match err {
+                       APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over their reserve value"),
+                       _ => panic!("Unknown error variants"),
+               }
+       }
+
+       // adding pending output
+       let recv_value_1 = (stat01.value_to_self_msat - stat01.channel_reserve_msat - total_fee_msat)/2;
+       let amt_msat_1 = recv_value_1 + total_fee_msat;
+
+       let (route_1, our_payment_hash_1, our_payment_preimage_1) = get_route_and_payment_hash!(recv_value_1);
+       let payment_event_1 = {
+               nodes[0].node.send_payment(route_1, our_payment_hash_1).unwrap();
+               check_added_monitors!(nodes[0], 1);
+
+               let mut events = nodes[0].node.get_and_clear_pending_msg_events();
+               assert_eq!(events.len(), 1);
+               SendEvent::from_event(events.remove(0))
+       };
+       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event_1.msgs[0]).unwrap();
+
+       // channel reserve test with htlc pending output > 0
+       let recv_value_2 = stat01.value_to_self_msat - amt_msat_1 - stat01.channel_reserve_msat - total_fee_msat;
+       {
+               let (route, our_payment_hash, _) = get_route_and_payment_hash!(recv_value_2 + 1);
+               match nodes[0].node.send_payment(route, our_payment_hash).err().unwrap() {
+                       APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over their reserve value"),
+                       _ => panic!("Unknown error variants"),
+               }
+       }
+
+       {
+               // test channel_reserve test on nodes[1] side
+               let (route, our_payment_hash, _) = get_route_and_payment_hash!(recv_value_2 + 1);
+
+               // Need to manually create update_add_htlc message to go around the channel reserve check in send_htlc()
+               let secp_ctx = Secp256k1::new();
+               let session_priv = SecretKey::from_slice(&{
+                       let mut session_key = [0; 32];
+                       let mut rng = thread_rng();
+                       rng.fill_bytes(&mut session_key);
+                       session_key
+               }).expect("RNG is bad!");
+
+               let cur_height = nodes[0].node.latest_block_height.load(Ordering::Acquire) as u32 + 1;
+               let onion_keys = onion_utils::construct_onion_keys(&secp_ctx, &route, &session_priv).unwrap();
+               let (onion_payloads, htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(&route, cur_height).unwrap();
+               let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, &our_payment_hash);
+               let msg = msgs::UpdateAddHTLC {
+                       channel_id: chan_1.2,
+                       htlc_id,
+                       amount_msat: htlc_msat,
+                       payment_hash: our_payment_hash,
+                       cltv_expiry: htlc_cltv,
+                       onion_routing_packet: onion_packet,
+               };
+
+               if test_recv {
+                       let err = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &msg).err().unwrap();
+                       match err {
+                               HandleError{err, .. } => assert_eq!(err, "Remote HTLC add would put them over their reserve value"),
+                       }
+                       // If we send a garbage message, the channel should get closed, making the rest of this test case fail.
+                       assert_eq!(nodes[1].node.list_channels().len(), 1);
+                       assert_eq!(nodes[1].node.list_channels().len(), 1);
+                       check_closed_broadcast!(nodes[1]);
+                       return;
+               }
+       }
+
+       // split the rest to test holding cell
+       let recv_value_21 = recv_value_2/2;
+       let recv_value_22 = recv_value_2 - recv_value_21 - total_fee_msat;
+       {
+               let stat = get_channel_value_stat!(nodes[0], chan_1.2);
+               assert_eq!(stat.value_to_self_msat - (stat.pending_outbound_htlcs_amount_msat + recv_value_21 + recv_value_22 + total_fee_msat + total_fee_msat), stat.channel_reserve_msat);
+       }
+
+       // now see if they go through on both sides
+       let (route_21, our_payment_hash_21, our_payment_preimage_21) = get_route_and_payment_hash!(recv_value_21);
+       // but this will stuck in the holding cell
+       nodes[0].node.send_payment(route_21, our_payment_hash_21).unwrap();
+       check_added_monitors!(nodes[0], 0);
+       let events = nodes[0].node.get_and_clear_pending_events();
+       assert_eq!(events.len(), 0);
+
+       // test with outbound holding cell amount > 0
+       {
+               let (route, our_payment_hash, _) = get_route_and_payment_hash!(recv_value_22+1);
+               match nodes[0].node.send_payment(route, our_payment_hash).err().unwrap() {
+                       APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over their reserve value"),
+                       _ => panic!("Unknown error variants"),
+               }
+       }
+
+       let (route_22, our_payment_hash_22, our_payment_preimage_22) = get_route_and_payment_hash!(recv_value_22);
+       // this will also stuck in the holding cell
+       nodes[0].node.send_payment(route_22, our_payment_hash_22).unwrap();
+       check_added_monitors!(nodes[0], 0);
+       assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
+       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
+
+       // flush the pending htlc
+       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event_1.commitment_msg).unwrap();
+       let (as_revoke_and_ack, as_commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+       check_added_monitors!(nodes[1], 1);
+
+       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_revoke_and_ack).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       let commitment_update_2 = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
+
+       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &as_commitment_signed).unwrap();
+       let bs_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
+       // No commitment_signed so get_event_msg's assert(len == 1) passes
+       check_added_monitors!(nodes[0], 1);
+
+       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+       check_added_monitors!(nodes[1], 1);
+
+       expect_pending_htlcs_forwardable!(nodes[1]);
+
+       let ref payment_event_11 = expect_forward!(nodes[1]);
+       nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event_11.msgs[0]).unwrap();
+       commitment_signed_dance!(nodes[2], nodes[1], payment_event_11.commitment_msg, false);
+
+       expect_pending_htlcs_forwardable!(nodes[2]);
+       expect_payment_received!(nodes[2], our_payment_hash_1, recv_value_1);
+
+       // flush the htlcs in the holding cell
+       assert_eq!(commitment_update_2.update_add_htlcs.len(), 2);
+       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &commitment_update_2.update_add_htlcs[0]).unwrap();
+       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &commitment_update_2.update_add_htlcs[1]).unwrap();
+       commitment_signed_dance!(nodes[1], nodes[0], &commitment_update_2.commitment_signed, false);
+       expect_pending_htlcs_forwardable!(nodes[1]);
+
+       let ref payment_event_3 = expect_forward!(nodes[1]);
+       assert_eq!(payment_event_3.msgs.len(), 2);
+       nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event_3.msgs[0]).unwrap();
+       nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event_3.msgs[1]).unwrap();
+
+       commitment_signed_dance!(nodes[2], nodes[1], &payment_event_3.commitment_msg, false);
+       expect_pending_htlcs_forwardable!(nodes[2]);
+
+       let events = nodes[2].node.get_and_clear_pending_events();
+       assert_eq!(events.len(), 2);
+       match events[0] {
+               Event::PaymentReceived { ref payment_hash, amt } => {
+                       assert_eq!(our_payment_hash_21, *payment_hash);
+                       assert_eq!(recv_value_21, amt);
+               },
+               _ => panic!("Unexpected event"),
+       }
+       match events[1] {
+               Event::PaymentReceived { ref payment_hash, amt } => {
+                       assert_eq!(our_payment_hash_22, *payment_hash);
+                       assert_eq!(recv_value_22, amt);
+               },
+               _ => panic!("Unexpected event"),
+       }
+
+       claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), our_payment_preimage_1);
+       claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), our_payment_preimage_21);
+       claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), our_payment_preimage_22);
+
+       let expected_value_to_self = stat01.value_to_self_msat - (recv_value_1 + total_fee_msat) - (recv_value_21 + total_fee_msat) - (recv_value_22 + total_fee_msat);
+       let stat0 = get_channel_value_stat!(nodes[0], chan_1.2);
+       assert_eq!(stat0.value_to_self_msat, expected_value_to_self);
+       assert_eq!(stat0.value_to_self_msat, stat0.channel_reserve_msat);
+
+       let stat2 = get_channel_value_stat!(nodes[2], chan_2.2);
+       assert_eq!(stat2.value_to_self_msat, stat22.value_to_self_msat + recv_value_1 + recv_value_21 + recv_value_22);
+}
+
+#[test]
+fn channel_reserve_test() {
+       do_channel_reserve_test(false);
+       do_channel_reserve_test(true);
+}
+
+#[test]
+fn channel_reserve_in_flight_removes() {
+       // In cases where one side claims an HTLC, it thinks it has additional available funds that it
+       // can send to its counterparty, but due to update ordering, the other side may not yet have
+       // considered those HTLCs fully removed.
+       // This tests that we don't count HTLCs which will not be included in the next remote
+       // commitment transaction towards the reserve value (as it implies no commitment transaction
+       // will be generated which violates the remote reserve value).
+       // This was broken previously, and discovered by the chanmon_fail_consistency fuzz test.
+       // To test this we:
+       //  * route two HTLCs from A to B (note that, at a high level, this test is checking that, when
+       //    you consider the values of both of these HTLCs, B may not send an HTLC back to A, but if
+       //    you only consider the value of the first HTLC, it may not),
+       //  * start routing a third HTLC from A to B,
+       //  * claim the first two HTLCs (though B will generate an update_fulfill for one, and put
+       //    the other claim in its holding cell, as it immediately goes into AwaitingRAA),
+       //  * deliver the first fulfill from B
+       //  * deliver the update_add and an RAA from A, resulting in B freeing the second holding cell
+       //    claim,
+       //  * deliver A's response CS and RAA.
+       //    This results in A having the second HTLC in AwaitingRemovedRemoteRevoke, but B having
+       //    removed it fully. B now has the push_msat plus the first two HTLCs in value.
+       //  * Now B happily sends another HTLC, potentially violating its reserve value from A's point
+       //    of view (if A counts the AwaitingRemovedRemoteRevoke HTLC).
+       let mut nodes = create_network(2, &[None, None]);
+       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       let b_chan_values = get_channel_value_stat!(nodes[1], chan_1.2);
+       // Route the first two HTLCs.
+       let (payment_preimage_1, _) = route_payment(&nodes[0], &[&nodes[1]], b_chan_values.channel_reserve_msat - b_chan_values.value_to_self_msat - 10000);
+       let (payment_preimage_2, _) = route_payment(&nodes[0], &[&nodes[1]], 20000);
+
+       // Start routing the third HTLC (this is just used to get everyone in the right state).
+       let (payment_preimage_3, payment_hash_3) = get_payment_preimage_hash!(nodes[0]);
+       let send_1 = {
+               let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap();
+               nodes[0].node.send_payment(route, payment_hash_3).unwrap();
+               check_added_monitors!(nodes[0], 1);
+               let mut events = nodes[0].node.get_and_clear_pending_msg_events();
+               assert_eq!(events.len(), 1);
+               SendEvent::from_event(events.remove(0))
+       };
+
+       // Now claim both of the first two HTLCs on B's end, putting B in AwaitingRAA and generating an
+       // initial fulfill/CS.
+       assert!(nodes[1].node.claim_funds(payment_preimage_1));
+       check_added_monitors!(nodes[1], 1);
+       let bs_removes = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+
+       // This claim goes in B's holding cell, allowing us to have a pending B->A RAA which does not
+       // remove the second HTLC when we send the HTLC back from B to A.
+       assert!(nodes[1].node.claim_funds(payment_preimage_2));
+       check_added_monitors!(nodes[1], 1);
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+
+       nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_removes.update_fulfill_htlcs[0]).unwrap();
+       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_removes.commitment_signed).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
+       expect_payment_sent!(nodes[0], payment_preimage_1);
+
+       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &send_1.msgs[0]).unwrap();
+       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &send_1.commitment_msg).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       // B is already AwaitingRAA, so cant generate a CS here
+       let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
+
+       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       let bs_cs = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+
+       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       let as_cs = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
+
+       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_cs.commitment_signed).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
+
+       // The second HTLCis removed, but as A is in AwaitingRAA it can't generate a CS here, so the
+       // RAA that B generated above doesn't fully resolve the second HTLC from A's point of view.
+       // However, the RAA A generates here *does* fully resolve the HTLC from B's point of view (as A
+       // can no longer broadcast a commitment transaction with it and B has the preimage so can go
+       // on-chain as necessary).
+       nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_cs.update_fulfill_htlcs[0]).unwrap();
+       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_cs.commitment_signed).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
+       expect_payment_sent!(nodes[0], payment_preimage_2);
+
+       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+
+       expect_pending_htlcs_forwardable!(nodes[1]);
+       expect_payment_received!(nodes[1], payment_hash_3, 100000);
+
+       // Note that as this RAA was generated before the delivery of the update_fulfill it shouldn't
+       // resolve the second HTLC from A's point of view.
+       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       let as_cs = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
+
+       // Now that B doesn't have the second RAA anymore, but A still does, send a payment from B back
+       // to A to ensure that A doesn't count the almost-removed HTLC in update_add processing.
+       let (payment_preimage_4, payment_hash_4) = get_payment_preimage_hash!(nodes[1]);
+       let send_2 = {
+               let route = nodes[1].router.get_route(&nodes[0].node.get_our_node_id(), None, &[], 10000, TEST_FINAL_CLTV).unwrap();
+               nodes[1].node.send_payment(route, payment_hash_4).unwrap();
+               check_added_monitors!(nodes[1], 1);
+               let mut events = nodes[1].node.get_and_clear_pending_msg_events();
+               assert_eq!(events.len(), 1);
+               SendEvent::from_event(events.remove(0))
+       };
+
+       nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &send_2.msgs[0]).unwrap();
+       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &send_2.commitment_msg).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
+
+       // Now just resolve all the outstanding messages/HTLCs for completeness...
+
+       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_cs.commitment_signed).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
+
+       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa).unwrap();
+       check_added_monitors!(nodes[1], 1);
+
+       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       let as_cs = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
+
+       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_cs.commitment_signed).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
+
+       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa).unwrap();
+       check_added_monitors!(nodes[0], 1);
+
+       expect_pending_htlcs_forwardable!(nodes[0]);
+       expect_payment_received!(nodes[0], payment_hash_4, 10000);
+
+       claim_payment(&nodes[1], &[&nodes[0]], payment_preimage_4);
+       claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_3);
+}
+
+#[test]
+fn channel_monitor_network_test() {
+       // Simple test which builds a network of ChannelManagers, connects them to each other, and
+       // tests that ChannelMonitor is able to recover from various states.
+       let nodes = create_network(5, &[None, None, None, None, None]);
+
+       // Create some initial channels
+       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+       let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
+       let chan_3 = create_announced_chan_between_nodes(&nodes, 2, 3, LocalFeatures::new(), LocalFeatures::new());
+       let chan_4 = create_announced_chan_between_nodes(&nodes, 3, 4, LocalFeatures::new(), LocalFeatures::new());
+
+       // Rebalance the network a bit by relaying one payment through all the channels...
+       send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3], &nodes[4])[..], 8000000);
+       send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3], &nodes[4])[..], 8000000);
+       send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3], &nodes[4])[..], 8000000);
+       send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3], &nodes[4])[..], 8000000);
+
+       // Simple case with no pending HTLCs:
+       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), true);
+       {
+               let mut node_txn = test_txn_broadcast(&nodes[1], &chan_1, None, HTLCType::NONE);
+               let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+               nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn.drain(..).next().unwrap()] }, 1);
+               test_txn_broadcast(&nodes[0], &chan_1, None, HTLCType::NONE);
+       }
+       get_announce_close_broadcast_events(&nodes, 0, 1);
+       assert_eq!(nodes[0].node.list_channels().len(), 0);
+       assert_eq!(nodes[1].node.list_channels().len(), 1);
+
+       // One pending HTLC is discarded by the force-close:
+       let payment_preimage_1 = route_payment(&nodes[1], &vec!(&nodes[2], &nodes[3])[..], 3000000).0;
+
+       // Simple case of one pending HTLC to HTLC-Timeout
+       nodes[1].node.peer_disconnected(&nodes[2].node.get_our_node_id(), true);
+       {
+               let mut node_txn = test_txn_broadcast(&nodes[1], &chan_2, None, HTLCType::TIMEOUT);
+               let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+               nodes[2].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn.drain(..).next().unwrap()] }, 1);
+               test_txn_broadcast(&nodes[2], &chan_2, None, HTLCType::NONE);
+       }
+       get_announce_close_broadcast_events(&nodes, 1, 2);
+       assert_eq!(nodes[1].node.list_channels().len(), 0);
+       assert_eq!(nodes[2].node.list_channels().len(), 1);
+
+       macro_rules! claim_funds {
+               ($node: expr, $prev_node: expr, $preimage: expr) => {
+                       {
+                               assert!($node.node.claim_funds($preimage));
+                               check_added_monitors!($node, 1);
+
+                               let events = $node.node.get_and_clear_pending_msg_events();
+                               assert_eq!(events.len(), 1);
+                               match events[0] {
+                                       MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, .. } } => {
+                                               assert!(update_add_htlcs.is_empty());
+                                               assert!(update_fail_htlcs.is_empty());
+                                               assert_eq!(*node_id, $prev_node.node.get_our_node_id());
+                                       },
+                                       _ => panic!("Unexpected event"),
+                               };
+                       }
+               }
+       }
+
+       // nodes[3] gets the preimage, but nodes[2] already disconnected, resulting in a nodes[2]
+       // HTLC-Timeout and a nodes[3] claim against it (+ its own announces)
+       nodes[2].node.peer_disconnected(&nodes[3].node.get_our_node_id(), true);
+       {
+               let node_txn = test_txn_broadcast(&nodes[2], &chan_3, None, HTLCType::TIMEOUT);
+
+               // Claim the payment on nodes[3], giving it knowledge of the preimage
+               claim_funds!(nodes[3], nodes[2], payment_preimage_1);
+
+               let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+               nodes[3].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn[0].clone()] }, 1);
+
+               check_preimage_claim(&nodes[3], &node_txn);
+       }
+       get_announce_close_broadcast_events(&nodes, 2, 3);
+       assert_eq!(nodes[2].node.list_channels().len(), 0);
+       assert_eq!(nodes[3].node.list_channels().len(), 1);
+
+       { // Cheat and reset nodes[4]'s height to 1
+               let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+               nodes[4].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![] }, 1);
+       }
+
+       assert_eq!(nodes[3].node.latest_block_height.load(Ordering::Acquire), 1);
+       assert_eq!(nodes[4].node.latest_block_height.load(Ordering::Acquire), 1);
+       // One pending HTLC to time out:
+       let payment_preimage_2 = route_payment(&nodes[3], &vec!(&nodes[4])[..], 3000000).0;
+       // CLTV expires at TEST_FINAL_CLTV + 1 (current height) + 1 (added in send_payment for
+       // buffer space).
+
+       {
+               let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+               nodes[3].chain_monitor.block_connected_checked(&header, 2, &Vec::new()[..], &[0; 0]);
+               for i in 3..TEST_FINAL_CLTV + 2 + LATENCY_GRACE_PERIOD_BLOCKS + 1 {
+                       header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+                       nodes[3].chain_monitor.block_connected_checked(&header, i, &Vec::new()[..], &[0; 0]);
+               }
+
+               let node_txn = test_txn_broadcast(&nodes[3], &chan_4, None, HTLCType::TIMEOUT);
+
+               // Claim the payment on nodes[4], giving it knowledge of the preimage
+               claim_funds!(nodes[4], nodes[3], payment_preimage_2);
+
+               header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+               nodes[4].chain_monitor.block_connected_checked(&header, 2, &Vec::new()[..], &[0; 0]);
+               for i in 3..TEST_FINAL_CLTV + 2 - CLTV_CLAIM_BUFFER + 1 {
+                       header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+                       nodes[4].chain_monitor.block_connected_checked(&header, i, &Vec::new()[..], &[0; 0]);
+               }
+
+               test_txn_broadcast(&nodes[4], &chan_4, None, HTLCType::SUCCESS);
+
+               header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+               nodes[4].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn[0].clone()] }, TEST_FINAL_CLTV - 5);
+
+               check_preimage_claim(&nodes[4], &node_txn);
+       }
+       get_announce_close_broadcast_events(&nodes, 3, 4);
+       assert_eq!(nodes[3].node.list_channels().len(), 0);
+       assert_eq!(nodes[4].node.list_channels().len(), 0);
+}
+
+#[test]
+fn test_justice_tx() {
+       // Test justice txn built on revoked HTLC-Success tx, against both sides
+
+       let mut alice_config = UserConfig::new();
+       alice_config.channel_options.announced_channel = true;
+       alice_config.peer_channel_config_limits.force_announced_channel_preference = false;
+       alice_config.own_channel_config.our_to_self_delay = 6 * 24 * 5;
+       let mut bob_config = UserConfig::new();
+       bob_config.channel_options.announced_channel = true;
+       bob_config.peer_channel_config_limits.force_announced_channel_preference = false;
+       bob_config.own_channel_config.our_to_self_delay = 6 * 24 * 3;
+       let nodes = create_network(2, &[Some(alice_config), Some(bob_config)]);
+       // Create some new channels:
+       let chan_5 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       // A pending HTLC which will be revoked:
+       let payment_preimage_3 = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
+       // Get the will-be-revoked local txn from nodes[0]
+       let revoked_local_txn = nodes[0].node.channel_state.lock().unwrap().by_id.iter().next().unwrap().1.last_local_commitment_txn.clone();
+       assert_eq!(revoked_local_txn.len(), 2); // First commitment tx, then HTLC tx
+       assert_eq!(revoked_local_txn[0].input.len(), 1);
+       assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_5.3.txid());
+       assert_eq!(revoked_local_txn[0].output.len(), 2); // Only HTLC and output back to 0 are present
+       assert_eq!(revoked_local_txn[1].input.len(), 1);
+       assert_eq!(revoked_local_txn[1].input[0].previous_output.txid, revoked_local_txn[0].txid());
+       assert_eq!(revoked_local_txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); // HTLC-Timeout
+       // Revoke the old state
+       claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_3);
+
+       {
+               let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+               nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
+               {
+                       let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
+                       assert_eq!(node_txn.len(), 3);
+                       assert_eq!(node_txn.pop().unwrap(), node_txn[0]); // An outpoint registration will result in a 2nd block_connected
+                       assert_eq!(node_txn[0].input.len(), 2); // We should claim the revoked output and the HTLC output
+
+                       check_spends!(node_txn[0], revoked_local_txn[0].clone());
+                       node_txn.swap_remove(0);
+               }
+               test_txn_broadcast(&nodes[1], &chan_5, None, HTLCType::NONE);
+
+               nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
+               let node_txn = test_txn_broadcast(&nodes[0], &chan_5, Some(revoked_local_txn[0].clone()), HTLCType::TIMEOUT);
+               header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+               nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn[1].clone()] }, 1);
+               test_revoked_htlc_claim_txn_broadcast(&nodes[1], node_txn[1].clone());
+       }
+       get_announce_close_broadcast_events(&nodes, 0, 1);
+
+       assert_eq!(nodes[0].node.list_channels().len(), 0);
+       assert_eq!(nodes[1].node.list_channels().len(), 0);
+
+       // We test justice_tx build by A on B's revoked HTLC-Success tx
+       // Create some new channels:
+       let chan_6 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       // A pending HTLC which will be revoked:
+       let payment_preimage_4 = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
+       // Get the will-be-revoked local txn from B
+       let revoked_local_txn = nodes[1].node.channel_state.lock().unwrap().by_id.iter().next().unwrap().1.last_local_commitment_txn.clone();
+       assert_eq!(revoked_local_txn.len(), 1); // Only commitment tx
+       assert_eq!(revoked_local_txn[0].input.len(), 1);
+       assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_6.3.txid());
+       assert_eq!(revoked_local_txn[0].output.len(), 2); // Only HTLC and output back to A are present
+       // Revoke the old state
+       claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_4);
+       {
+               let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+               nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
+               {
+                       let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
+                       assert_eq!(node_txn.len(), 3);
+                       assert_eq!(node_txn.pop().unwrap(), node_txn[0]); // An outpoint registration will result in a 2nd block_connected
+                       assert_eq!(node_txn[0].input.len(), 1); // We claim the received HTLC output
+
+                       check_spends!(node_txn[0], revoked_local_txn[0].clone());
+                       node_txn.swap_remove(0);
+               }
+               test_txn_broadcast(&nodes[0], &chan_6, None, HTLCType::NONE);
+
+               nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
+               let node_txn = test_txn_broadcast(&nodes[1], &chan_6, Some(revoked_local_txn[0].clone()), HTLCType::SUCCESS);
+               header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+               nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn[1].clone()] }, 1);
+               test_revoked_htlc_claim_txn_broadcast(&nodes[0], node_txn[1].clone());
+       }
+       get_announce_close_broadcast_events(&nodes, 0, 1);
+       assert_eq!(nodes[0].node.list_channels().len(), 0);
+       assert_eq!(nodes[1].node.list_channels().len(), 0);
+}
+
+#[test]
+fn revoked_output_claim() {
+       // Simple test to ensure a node will claim a revoked output when a stale remote commitment
+       // transaction is broadcast by its counterparty
+       let nodes = create_network(2, &[None, None]);
+       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+       // node[0] is gonna to revoke an old state thus node[1] should be able to claim the revoked output
+       let revoked_local_txn = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
+       assert_eq!(revoked_local_txn.len(), 1);
+       // Only output is the full channel value back to nodes[0]:
+       assert_eq!(revoked_local_txn[0].output.len(), 1);
+       // Send a payment through, updating everyone's latest commitment txn
+       send_payment(&nodes[0], &vec!(&nodes[1])[..], 5000000);
+
+       // Inform nodes[1] that nodes[0] broadcast a stale tx
+       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
+       let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
+       assert_eq!(node_txn.len(), 3); // nodes[1] will broadcast justice tx twice, and its own local state once
+
+       assert_eq!(node_txn[0], node_txn[2]);
+
+       check_spends!(node_txn[0], revoked_local_txn[0].clone());
+       check_spends!(node_txn[1], chan_1.3.clone());
+
+       // Inform nodes[0] that a watchtower cheated on its behalf, so it will force-close the chan
+       nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
+       get_announce_close_broadcast_events(&nodes, 0, 1);
+}
+
+#[test]
+fn claim_htlc_outputs_shared_tx() {
+       // Node revoked old state, htlcs haven't time out yet, claim them in shared justice tx
+       let nodes = create_network(2, &[None, None]);
+
+       // Create some new channel:
+       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       // Rebalance the network to generate htlc in the two directions
+       send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
+       // node[0] is gonna to revoke an old state thus node[1] should be able to claim both offered/received HTLC outputs on top of commitment tx
+       let payment_preimage_1 = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
+       let (_payment_preimage_2, payment_hash_2) = route_payment(&nodes[1], &vec!(&nodes[0])[..], 3000000);
+
+       // Get the will-be-revoked local txn from node[0]
+       let revoked_local_txn = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
+       assert_eq!(revoked_local_txn.len(), 2); // commitment tx + 1 HTLC-Timeout tx
+       assert_eq!(revoked_local_txn[0].input.len(), 1);
+       assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_1.3.txid());
+       assert_eq!(revoked_local_txn[1].input.len(), 1);
+       assert_eq!(revoked_local_txn[1].input[0].previous_output.txid, revoked_local_txn[0].txid());
+       assert_eq!(revoked_local_txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); // HTLC-Timeout
+       check_spends!(revoked_local_txn[1], revoked_local_txn[0].clone());
+
+       //Revoke the old state
+       claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_1);
+
+       {
+               let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+               nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
+               nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
+               connect_blocks(&nodes[1].chain_monitor, ANTI_REORG_DELAY - 1, 1, true, header.bitcoin_hash());
+
+               let events = nodes[1].node.get_and_clear_pending_events();
+               assert_eq!(events.len(), 1);
+               match events[0] {
+                       Event::PaymentFailed { payment_hash, .. } => {
+                               assert_eq!(payment_hash, payment_hash_2);
+                       },
+                       _ => panic!("Unexpected event"),
+               }
+
+               let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
+               assert_eq!(node_txn.len(), 4);
+
+               assert_eq!(node_txn[0].input.len(), 3); // Claim the revoked output + both revoked HTLC outputs
+               check_spends!(node_txn[0], revoked_local_txn[0].clone());
+
+               assert_eq!(node_txn[0], node_txn[3]); // justice tx is duplicated due to block re-scanning
+
+               let mut witness_lens = BTreeSet::new();
+               witness_lens.insert(node_txn[0].input[0].witness.last().unwrap().len());
+               witness_lens.insert(node_txn[0].input[1].witness.last().unwrap().len());
+               witness_lens.insert(node_txn[0].input[2].witness.last().unwrap().len());
+               assert_eq!(witness_lens.len(), 3);
+               assert_eq!(*witness_lens.iter().skip(0).next().unwrap(), 77); // revoked to_local
+               assert_eq!(*witness_lens.iter().skip(1).next().unwrap(), OFFERED_HTLC_SCRIPT_WEIGHT); // revoked offered HTLC
+               assert_eq!(*witness_lens.iter().skip(2).next().unwrap(), ACCEPTED_HTLC_SCRIPT_WEIGHT); // revoked received HTLC
+
+               // Next nodes[1] broadcasts its current local tx state:
+               assert_eq!(node_txn[1].input.len(), 1);
+               assert_eq!(node_txn[1].input[0].previous_output.txid, chan_1.3.txid()); //Spending funding tx unique txouput, tx broadcasted by ChannelManager
+
+               assert_eq!(node_txn[2].input.len(), 1);
+               let witness_script = node_txn[2].clone().input[0].witness.pop().unwrap();
+               assert_eq!(witness_script.len(), OFFERED_HTLC_SCRIPT_WEIGHT); //Spending an offered htlc output
+               assert_eq!(node_txn[2].input[0].previous_output.txid, node_txn[1].txid());
+               assert_ne!(node_txn[2].input[0].previous_output.txid, node_txn[0].input[0].previous_output.txid);
+               assert_ne!(node_txn[2].input[0].previous_output.txid, node_txn[0].input[1].previous_output.txid);
+       }
+       get_announce_close_broadcast_events(&nodes, 0, 1);
+       assert_eq!(nodes[0].node.list_channels().len(), 0);
+       assert_eq!(nodes[1].node.list_channels().len(), 0);
+}
+
+#[test]
+fn claim_htlc_outputs_single_tx() {
+       // Node revoked old state, htlcs have timed out, claim each of them in separated justice tx
+       let nodes = create_network(2, &[None, None]);
+
+       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       // Rebalance the network to generate htlc in the two directions
+       send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
+       // node[0] is gonna to revoke an old state thus node[1] should be able to claim both offered/received HTLC outputs on top of commitment tx, but this
+       // time as two different claim transactions as we're gonna to timeout htlc with given a high current height
+       let payment_preimage_1 = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
+       let (_payment_preimage_2, payment_hash_2) = route_payment(&nodes[1], &vec!(&nodes[0])[..], 3000000);
+
+       // Get the will-be-revoked local txn from node[0]
+       let revoked_local_txn = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
+
+       //Revoke the old state
+       claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_1);
+
+       {
+               let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+               nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 200);
+               nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 200);
+               connect_blocks(&nodes[1].chain_monitor, ANTI_REORG_DELAY - 1, 200, true, header.bitcoin_hash());
+
+               let events = nodes[1].node.get_and_clear_pending_events();
+               assert_eq!(events.len(), 1);
+               match events[0] {
+                       Event::PaymentFailed { payment_hash, .. } => {
+                               assert_eq!(payment_hash, payment_hash_2);
+                       },
+                       _ => panic!("Unexpected event"),
+               }
+
+               let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
+               assert_eq!(node_txn.len(), 22); // ChannelManager : 2, ChannelMontitor: 8 (1 standard revoked output, 2 revocation htlc tx, 1 local commitment tx + 1 htlc timeout tx) * 2 (block-rescan) + 5 * (1 local commitment tx + 1 htlc timeout tx)
+
+               assert_eq!(node_txn[0], node_txn[7]);
+               assert_eq!(node_txn[1], node_txn[8]);
+               assert_eq!(node_txn[2], node_txn[9]);
+               assert_eq!(node_txn[3], node_txn[10]);
+               assert_eq!(node_txn[4], node_txn[11]);
+               assert_eq!(node_txn[3], node_txn[5]); //local commitment tx + htlc timeout tx broadcasted by ChannelManger
+               assert_eq!(node_txn[4], node_txn[6]);
+
+               for i in 12..22 {
+                       if i % 2 == 0 { assert_eq!(node_txn[3], node_txn[i]); } else { assert_eq!(node_txn[4], node_txn[i]); }
+               }
+
+               assert_eq!(node_txn[0].input.len(), 1);
+               assert_eq!(node_txn[1].input.len(), 1);
+               assert_eq!(node_txn[2].input.len(), 1);
+
+               fn get_txout(out_point: &BitcoinOutPoint, tx: &Transaction) -> Option<TxOut> {
+                       if out_point.txid == tx.txid() {
+                               tx.output.get(out_point.vout as usize).cloned()
+                       } else {
+                               None
+                       }
+               }
+               node_txn[0].verify(|out|get_txout(out, &revoked_local_txn[0])).unwrap();
+               node_txn[1].verify(|out|get_txout(out, &revoked_local_txn[0])).unwrap();
+               node_txn[2].verify(|out|get_txout(out, &revoked_local_txn[0])).unwrap();
+
+               let mut witness_lens = BTreeSet::new();
+               witness_lens.insert(node_txn[0].input[0].witness.last().unwrap().len());
+               witness_lens.insert(node_txn[1].input[0].witness.last().unwrap().len());
+               witness_lens.insert(node_txn[2].input[0].witness.last().unwrap().len());
+               assert_eq!(witness_lens.len(), 3);
+               assert_eq!(*witness_lens.iter().skip(0).next().unwrap(), 77); // revoked to_local
+               assert_eq!(*witness_lens.iter().skip(1).next().unwrap(), OFFERED_HTLC_SCRIPT_WEIGHT); // revoked offered HTLC
+               assert_eq!(*witness_lens.iter().skip(2).next().unwrap(), ACCEPTED_HTLC_SCRIPT_WEIGHT); // revoked received HTLC
+
+               assert_eq!(node_txn[3].input.len(), 1);
+               check_spends!(node_txn[3], chan_1.3.clone());
+
+               assert_eq!(node_txn[4].input.len(), 1);
+               let witness_script = node_txn[4].input[0].witness.last().unwrap();
+               assert_eq!(witness_script.len(), OFFERED_HTLC_SCRIPT_WEIGHT); //Spending an offered htlc output
+               assert_eq!(node_txn[4].input[0].previous_output.txid, node_txn[3].txid());
+               assert_ne!(node_txn[4].input[0].previous_output.txid, node_txn[0].input[0].previous_output.txid);
+               assert_ne!(node_txn[4].input[0].previous_output.txid, node_txn[1].input[0].previous_output.txid);
+       }
+       get_announce_close_broadcast_events(&nodes, 0, 1);
+       assert_eq!(nodes[0].node.list_channels().len(), 0);
+       assert_eq!(nodes[1].node.list_channels().len(), 0);
+}
+
+#[test]
+fn test_htlc_on_chain_success() {
+       // Test that in case of a unilateral close onchain, we detect the state of output thanks to
+       // ChainWatchInterface and pass the preimage backward accordingly. So here we test that ChannelManager is
+       // broadcasting the right event to other nodes in payment path.
+       // We test with two HTLCs simultaneously as that was not handled correctly in the past.
+       // A --------------------> B ----------------------> C (preimage)
+       // First, C should claim the HTLC outputs via HTLC-Success when its own latest local
+       // commitment transaction was broadcast.
+       // Then, B should learn the preimage from said transactions, attempting to claim backwards
+       // towards B.
+       // B should be able to claim via preimage if A then broadcasts its local tx.
+       // Finally, when A sees B's latest local commitment transaction it should be able to claim
+       // the HTLC outputs via the preimage it learned (which, once confirmed should generate a
+       // PaymentSent event).
+
+       let nodes = create_network(3, &[None, None, None]);
+
+       // Create some initial channels
+       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+       let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
+
+       // Rebalance the network a bit by relaying one payment through all the channels...
+       send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
+       send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
+
+       let (our_payment_preimage, _payment_hash) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), 3000000);
+       let (our_payment_preimage_2, _payment_hash_2) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), 3000000);
+       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42};
+
+       // Broadcast legit commitment tx from C on B's chain
+       // Broadcast HTLC Success transaction by C on received output from C's commitment tx on B's chain
+       let commitment_tx = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone();
+       assert_eq!(commitment_tx.len(), 1);
+       check_spends!(commitment_tx[0], chan_2.3.clone());
+       nodes[2].node.claim_funds(our_payment_preimage);
+       nodes[2].node.claim_funds(our_payment_preimage_2);
+       check_added_monitors!(nodes[2], 2);
+       let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
+       assert!(updates.update_add_htlcs.is_empty());
+       assert!(updates.update_fail_htlcs.is_empty());
+       assert!(updates.update_fail_malformed_htlcs.is_empty());
+       assert_eq!(updates.update_fulfill_htlcs.len(), 1);
+
+       nodes[2].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 1);
+       check_closed_broadcast!(nodes[2]);
+       let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 1 (commitment tx), ChannelMonitor : 4 (2*2 * HTLC-Success tx)
+       assert_eq!(node_txn.len(), 5);
+       assert_eq!(node_txn[0], node_txn[3]);
+       assert_eq!(node_txn[1], node_txn[4]);
+       assert_eq!(node_txn[2], commitment_tx[0]);
+       check_spends!(node_txn[0], commitment_tx[0].clone());
+       check_spends!(node_txn[1], commitment_tx[0].clone());
+       assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
+       assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
+       assert!(node_txn[0].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
+       assert!(node_txn[1].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
+       assert_eq!(node_txn[0].lock_time, 0);
+       assert_eq!(node_txn[1].lock_time, 0);
+
+       // Verify that B's ChannelManager is able to extract preimage from HTLC Success tx and pass it backward
+       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: node_txn}, 1);
+       let events = nodes[1].node.get_and_clear_pending_msg_events();
+       {
+               let mut added_monitors = nodes[1].chan_monitor.added_monitors.lock().unwrap();
+               assert_eq!(added_monitors.len(), 2);
+               assert_eq!(added_monitors[0].0.txid, chan_1.3.txid());
+               assert_eq!(added_monitors[1].0.txid, chan_1.3.txid());
+               added_monitors.clear();
+       }
+       assert_eq!(events.len(), 2);
+       match events[0] {
+               MessageSendEvent::BroadcastChannelUpdate { .. } => {},
+               _ => panic!("Unexpected event"),
+       }
+       match events[1] {
+               MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, ref update_fulfill_htlcs, ref update_fail_malformed_htlcs, .. } } => {
+                       assert!(update_add_htlcs.is_empty());
+                       assert!(update_fail_htlcs.is_empty());
+                       assert_eq!(update_fulfill_htlcs.len(), 1);
+                       assert!(update_fail_malformed_htlcs.is_empty());
+                       assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
+               },
+               _ => panic!("Unexpected event"),
+       };
+       macro_rules! check_tx_local_broadcast {
+               ($node: expr, $htlc_offered: expr, $commitment_tx: expr, $chan_tx: expr) => { {
+                       // ChannelManager : 3 (commitment tx, 2*HTLC-Timeout tx), ChannelMonitor : 2 (timeout tx) * 2 (block-rescan)
+                       let mut node_txn = $node.tx_broadcaster.txn_broadcasted.lock().unwrap();
+                       assert_eq!(node_txn.len(), 7);
+                       assert_eq!(node_txn[0], node_txn[5]);
+                       assert_eq!(node_txn[1], node_txn[6]);
+                       check_spends!(node_txn[0], $commitment_tx.clone());
+                       check_spends!(node_txn[1], $commitment_tx.clone());
+                       assert_ne!(node_txn[0].lock_time, 0);
+                       assert_ne!(node_txn[1].lock_time, 0);
+                       if $htlc_offered {
+                               assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
+                               assert_eq!(node_txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
+                               assert!(node_txn[0].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
+                               assert!(node_txn[1].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
+                       } else {
+                               assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
+                               assert_eq!(node_txn[1].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
+                               assert!(node_txn[0].output[0].script_pubkey.is_v0_p2wpkh()); // direct payment
+                               assert!(node_txn[1].output[0].script_pubkey.is_v0_p2wpkh()); // direct payment
+                       }
+                       check_spends!(node_txn[2], $chan_tx.clone());
+                       check_spends!(node_txn[3], node_txn[2].clone());
+                       check_spends!(node_txn[4], node_txn[2].clone());
+                       assert_eq!(node_txn[2].input[0].witness.last().unwrap().len(), 71);
+                       assert_eq!(node_txn[3].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
+                       assert_eq!(node_txn[4].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
+                       assert!(node_txn[3].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
+                       assert!(node_txn[4].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
+                       assert_ne!(node_txn[3].lock_time, 0);
+                       assert_ne!(node_txn[4].lock_time, 0);
+                       node_txn.clear();
+               } }
+       }
+       // nodes[1] now broadcasts its own local state as a fallback, suggesting an alternate
+       // commitment transaction with a corresponding HTLC-Timeout transactions, as well as a
+       // timeout-claim of the output that nodes[2] just claimed via success.
+       check_tx_local_broadcast!(nodes[1], false, commitment_tx[0], chan_2.3);
+
+       // Broadcast legit commitment tx from A on B's chain
+       // Broadcast preimage tx by B on offered output from A commitment tx  on A's chain
+       let commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
+       check_spends!(commitment_tx[0], chan_1.3.clone());
+       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 1);
+       check_closed_broadcast!(nodes[1]);
+       let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 1 (commitment tx), ChannelMonitor : 1 (HTLC-Success) * 2 (block-rescan)
+       assert_eq!(node_txn.len(), 3);
+       assert_eq!(node_txn[0], node_txn[2]);
+       check_spends!(node_txn[0], commitment_tx[0].clone());
+       assert_eq!(node_txn[0].input.len(), 2);
+       assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
+       assert_eq!(node_txn[0].input[1].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
+       assert_eq!(node_txn[0].lock_time, 0);
+       assert!(node_txn[0].output[0].script_pubkey.is_v0_p2wpkh()); // direct payment
+       check_spends!(node_txn[1], chan_1.3.clone());
+       assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), 71);
+       // We don't bother to check that B can claim the HTLC output on its commitment tx here as
+       // we already checked the same situation with A.
+
+       // Verify that A's ChannelManager is able to extract preimage from preimage tx and generate PaymentSent
+       nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone(), node_txn[0].clone()] }, 1);
+       check_closed_broadcast!(nodes[0]);
+       let events = nodes[0].node.get_and_clear_pending_events();
+       assert_eq!(events.len(), 2);
+       let mut first_claimed = false;
+       for event in events {
+               match event {
+                       Event::PaymentSent { payment_preimage } => {
+                               if payment_preimage == our_payment_preimage {
+                                       assert!(!first_claimed);
+                                       first_claimed = true;
+                               } else {
+                                       assert_eq!(payment_preimage, our_payment_preimage_2);
+                               }
+                       },
+                       _ => panic!("Unexpected event"),
+               }
+       }
+       check_tx_local_broadcast!(nodes[0], true, commitment_tx[0], chan_1.3);
+}
+
+#[test]
+fn test_htlc_on_chain_timeout() {
+       // Test that in case of a unilateral close onchain, we detect the state of output thanks to
+       // ChainWatchInterface and timeout the HTLC backward accordingly. So here we test that ChannelManager is
+       // broadcasting the right event to other nodes in payment path.
+       // A ------------------> B ----------------------> C (timeout)
+       //    B's commitment tx                 C's commitment tx
+       //            \                                  \
+       //         B's HTLC timeout tx               B's timeout tx
+
+       let nodes = create_network(3, &[None, None, None]);
+
+       // Create some intial channels
+       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+       let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
+
+       // Rebalance the network a bit by relaying one payment thorugh all the channels...
+       send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
+       send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
+
+       let (_payment_preimage, payment_hash) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), 3000000);
+       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42};
+
+       // Broadcast legit commitment tx from C on B's chain
+       let commitment_tx = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone();
+       check_spends!(commitment_tx[0], chan_2.3.clone());
+       nodes[2].node.fail_htlc_backwards(&payment_hash);
+       check_added_monitors!(nodes[2], 0);
+       expect_pending_htlcs_forwardable!(nodes[2]);
+       check_added_monitors!(nodes[2], 1);
+
+       let events = nodes[2].node.get_and_clear_pending_msg_events();
+       assert_eq!(events.len(), 1);
+       match events[0] {
+               MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, .. } } => {
+                       assert!(update_add_htlcs.is_empty());
+                       assert!(!update_fail_htlcs.is_empty());
+                       assert!(update_fulfill_htlcs.is_empty());
+                       assert!(update_fail_malformed_htlcs.is_empty());
+                       assert_eq!(nodes[1].node.get_our_node_id(), *node_id);
+               },
+               _ => panic!("Unexpected event"),
+       };
+       nodes[2].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 1);
+       check_closed_broadcast!(nodes[2]);
+       let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 1 (commitment tx)
+       assert_eq!(node_txn.len(), 1);
+       check_spends!(node_txn[0], chan_2.3.clone());
+       assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), 71);
+
+       // Broadcast timeout transaction by B on received output from C's commitment tx on B's chain
+       // Verify that B's ChannelManager is able to detect that HTLC is timeout by its own tx and react backward in consequence
+       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 200);
+       let timeout_tx;
+       {
+               let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
+               assert_eq!(node_txn.len(), 8); // ChannelManager : 2 (commitment tx, HTLC-Timeout tx), ChannelMonitor : 6 (HTLC-Timeout tx, commitment tx, timeout tx) * 2 (block-rescan)
+               assert_eq!(node_txn[0], node_txn[5]);
+               assert_eq!(node_txn[1], node_txn[6]);
+               assert_eq!(node_txn[2], node_txn[7]);
+               check_spends!(node_txn[0], commitment_tx[0].clone());
+               assert_eq!(node_txn[0].clone().input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
+               check_spends!(node_txn[1], chan_2.3.clone());
+               check_spends!(node_txn[2], node_txn[1].clone());
+               assert_eq!(node_txn[1].clone().input[0].witness.last().unwrap().len(), 71);
+               assert_eq!(node_txn[2].clone().input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
+               check_spends!(node_txn[3], chan_2.3.clone());
+               check_spends!(node_txn[4], node_txn[3].clone());
+               assert_eq!(node_txn[3].input[0].witness.clone().last().unwrap().len(), 71);
+               assert_eq!(node_txn[4].input[0].witness.clone().last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
+               timeout_tx = node_txn[0].clone();
+               node_txn.clear();
+       }
+
+       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![timeout_tx]}, 1);
+       connect_blocks(&nodes[1].chain_monitor, ANTI_REORG_DELAY - 1, 1, true, header.bitcoin_hash());
+       check_added_monitors!(nodes[1], 0);
+       check_closed_broadcast!(nodes[1]);
+
+       expect_pending_htlcs_forwardable!(nodes[1]);
+       check_added_monitors!(nodes[1], 1);
+       let events = nodes[1].node.get_and_clear_pending_msg_events();
+       assert_eq!(events.len(), 1);
+       match events[0] {
+               MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, ref update_fulfill_htlcs, ref update_fail_malformed_htlcs, .. } } => {
+                       assert!(update_add_htlcs.is_empty());
+                       assert!(!update_fail_htlcs.is_empty());
+                       assert!(update_fulfill_htlcs.is_empty());
+                       assert!(update_fail_malformed_htlcs.is_empty());
+                       assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
+               },
+               _ => panic!("Unexpected event"),
+       };
+       let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // Well... here we detect our own htlc_timeout_tx so no tx to be generated
+       assert_eq!(node_txn.len(), 0);
+
+       // Broadcast legit commitment tx from B on A's chain
+       let commitment_tx = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
+       check_spends!(commitment_tx[0], chan_1.3.clone());
+
+       nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 200);
+       check_closed_broadcast!(nodes[0]);
+       let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 2 (commitment tx, HTLC-Timeout tx), ChannelMonitor : 2 (timeout tx) * 2 block-rescan
+       assert_eq!(node_txn.len(), 4);
+       assert_eq!(node_txn[0], node_txn[3]);
+       check_spends!(node_txn[0], commitment_tx[0].clone());
+       assert_eq!(node_txn[0].clone().input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
+       check_spends!(node_txn[1], chan_1.3.clone());
+       check_spends!(node_txn[2], node_txn[1].clone());
+       assert_eq!(node_txn[1].clone().input[0].witness.last().unwrap().len(), 71);
+       assert_eq!(node_txn[2].clone().input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
+}
+
+#[test]
+fn test_simple_commitment_revoked_fail_backward() {
+       // Test that in case of a revoked commitment tx, we detect the resolution of output by justice tx
+       // and fail backward accordingly.
+
+       let nodes = create_network(3, &[None, None, None]);
+
+       // Create some initial channels
+       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+       let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
+
+       let (payment_preimage, _payment_hash) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3000000);
+       // Get the will-be-revoked local txn from nodes[2]
+       let revoked_local_txn = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone();
+       // Revoke the old state
+       claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage);
+
+       route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3000000);
+
+       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42};
+       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
+       connect_blocks(&nodes[1].chain_monitor, ANTI_REORG_DELAY - 1, 1, true, header.bitcoin_hash());
+       check_added_monitors!(nodes[1], 0);
+       check_closed_broadcast!(nodes[1]);
+
+       expect_pending_htlcs_forwardable!(nodes[1]);
+       check_added_monitors!(nodes[1], 1);
+       let events = nodes[1].node.get_and_clear_pending_msg_events();
+       assert_eq!(events.len(), 1);
+       match events[0] {
+               MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, ref update_fulfill_htlcs, ref update_fail_malformed_htlcs, ref commitment_signed, .. } } => {
+                       assert!(update_add_htlcs.is_empty());
+                       assert_eq!(update_fail_htlcs.len(), 1);
+                       assert!(update_fulfill_htlcs.is_empty());
+                       assert!(update_fail_malformed_htlcs.is_empty());
+                       assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
+
+                       nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[0]).unwrap();
+                       commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, false, true);
+
+                       let events = nodes[0].node.get_and_clear_pending_msg_events();
+                       assert_eq!(events.len(), 1);
+                       match events[0] {
+                               MessageSendEvent::PaymentFailureNetworkUpdate { .. } => {},
+                               _ => panic!("Unexpected event"),
+                       }
+                       let events = nodes[0].node.get_and_clear_pending_events();
+                       assert_eq!(events.len(), 1);
+                       match events[0] {
+                               Event::PaymentFailed { .. } => {},
+                               _ => panic!("Unexpected event"),
+                       }
+               },
+               _ => panic!("Unexpected event"),
+       }
+}
+
+fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool, use_dust: bool, no_to_remote: bool) {
+       // Test that if our counterparty broadcasts a revoked commitment transaction we fail all
+       // pending HTLCs on that channel backwards even if the HTLCs aren't present in our latest
+       // commitment transaction anymore.
+       // To do this, we have the peer which will broadcast a revoked commitment transaction send
+       // a number of update_fail/commitment_signed updates without ever sending the RAA in
+       // response to our commitment_signed. This is somewhat misbehavior-y, though not
+       // technically disallowed and we should probably handle it reasonably.
+       // Note that this is pretty exhaustive as an outbound HTLC which we haven't yet
+       // failed/fulfilled backwards must be in at least one of the latest two remote commitment
+       // transactions:
+       // * Once we move it out of our holding cell/add it, we will immediately include it in a
+       //   commitment_signed (implying it will be in the latest remote commitment transaction).
+       // * Once they remove it, we will send a (the first) commitment_signed without the HTLC,
+       //   and once they revoke the previous commitment transaction (allowing us to send a new
+       //   commitment_signed) we will be free to fail/fulfill the HTLC backwards.
+       let mut nodes = create_network(3, &[None, None, None]);
+
+       // Create some initial channels
+       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+       let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
+
+       let (payment_preimage, _payment_hash) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], if no_to_remote { 10_000 } else { 3_000_000 });
+       // Get the will-be-revoked local txn from nodes[2]
+       let revoked_local_txn = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone();
+       assert_eq!(revoked_local_txn[0].output.len(), if no_to_remote { 1 } else { 2 });
+       // Revoke the old state
+       claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage);
+
+       let value = if use_dust {
+               // The dust limit applied to HTLC outputs considers the fee of the HTLC transaction as
+               // well, so HTLCs at exactly the dust limit will not be included in commitment txn.
+               nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().our_dust_limit_satoshis * 1000
+       } else { 3000000 };
+
+       let (_, first_payment_hash) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], value);
+       let (_, second_payment_hash) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], value);
+       let (_, third_payment_hash) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], value);
+
+       assert!(nodes[2].node.fail_htlc_backwards(&first_payment_hash));
+       expect_pending_htlcs_forwardable!(nodes[2]);
+       check_added_monitors!(nodes[2], 1);
+       let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
+       assert!(updates.update_add_htlcs.is_empty());
+       assert!(updates.update_fulfill_htlcs.is_empty());
+       assert!(updates.update_fail_malformed_htlcs.is_empty());
+       assert_eq!(updates.update_fail_htlcs.len(), 1);
+       assert!(updates.update_fee.is_none());
+       nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]).unwrap();
+       let bs_raa = commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false, true, false, true);
+       // Drop the last RAA from 3 -> 2
+
+       assert!(nodes[2].node.fail_htlc_backwards(&second_payment_hash));
+       expect_pending_htlcs_forwardable!(nodes[2]);
+       check_added_monitors!(nodes[2], 1);
+       let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
+       assert!(updates.update_add_htlcs.is_empty());
+       assert!(updates.update_fulfill_htlcs.is_empty());
+       assert!(updates.update_fail_malformed_htlcs.is_empty());
+       assert_eq!(updates.update_fail_htlcs.len(), 1);
+       assert!(updates.update_fee.is_none());
+       nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]).unwrap();
+       nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &updates.commitment_signed).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       // Note that nodes[1] is in AwaitingRAA, so won't send a CS
+       let as_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
+       nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_raa).unwrap();
+       check_added_monitors!(nodes[2], 1);
+
+       assert!(nodes[2].node.fail_htlc_backwards(&third_payment_hash));
+       expect_pending_htlcs_forwardable!(nodes[2]);
+       check_added_monitors!(nodes[2], 1);
+       let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
+       assert!(updates.update_add_htlcs.is_empty());
+       assert!(updates.update_fulfill_htlcs.is_empty());
+       assert!(updates.update_fail_malformed_htlcs.is_empty());
+       assert_eq!(updates.update_fail_htlcs.len(), 1);
+       assert!(updates.update_fee.is_none());
+       nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]).unwrap();
+       // At this point first_payment_hash has dropped out of the latest two commitment
+       // transactions that nodes[1] is tracking...
+       nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &updates.commitment_signed).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       // Note that nodes[1] is (still) in AwaitingRAA, so won't send a CS
+       let as_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
+       nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_raa).unwrap();
+       check_added_monitors!(nodes[2], 1);
+
+       // Add a fourth HTLC, this one will get sequestered away in nodes[1]'s holding cell waiting
+       // on nodes[2]'s RAA.
+       let route = nodes[1].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
+       let (_, fourth_payment_hash) = get_payment_preimage_hash!(nodes[0]);
+       nodes[1].node.send_payment(route, fourth_payment_hash).unwrap();
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+       assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
+       check_added_monitors!(nodes[1], 0);
+
+       if deliver_bs_raa {
+               nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_raa).unwrap();
+               // One monitor for the new revocation preimage, no second on as we won't generate a new
+               // commitment transaction for nodes[0] until process_pending_htlc_forwards().
+               check_added_monitors!(nodes[1], 1);
+               let events = nodes[1].node.get_and_clear_pending_events();
+               assert_eq!(events.len(), 1);
+               match events[0] {
+                       Event::PendingHTLCsForwardable { .. } => { },
+                       _ => panic!("Unexpected event"),
+               };
+               // Deliberately don't process the pending fail-back so they all fail back at once after
+               // block connection just like the !deliver_bs_raa case
+       }
+
+       let mut failed_htlcs = HashSet::new();
+       assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
+
+       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42};
+       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
+       connect_blocks(&nodes[1].chain_monitor, ANTI_REORG_DELAY - 1, 1, true, header.bitcoin_hash());
+
+       let events = nodes[1].node.get_and_clear_pending_events();
+       assert_eq!(events.len(), if deliver_bs_raa { 1 } else { 2 });
+       match events[0] {
+               Event::PaymentFailed { ref payment_hash, .. } => {
+                       assert_eq!(*payment_hash, fourth_payment_hash);
+               },
+               _ => panic!("Unexpected event"),
+       }
+       if !deliver_bs_raa {
+               match events[1] {
+                       Event::PendingHTLCsForwardable { .. } => { },
+                       _ => panic!("Unexpected event"),
+               };
+       }
+       nodes[1].node.process_pending_htlc_forwards();
+       check_added_monitors!(nodes[1], 1);
+
+       let events = nodes[1].node.get_and_clear_pending_msg_events();
+       assert_eq!(events.len(), if deliver_bs_raa { 3 } else { 2 });
+       match events[if deliver_bs_raa { 1 } else { 0 }] {
+               MessageSendEvent::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
+               _ => panic!("Unexpected event"),
+       }
+       if deliver_bs_raa {
+               match events[0] {
+                       MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, ref update_fulfill_htlcs, ref update_fail_malformed_htlcs, .. } } => {
+                               assert_eq!(nodes[2].node.get_our_node_id(), *node_id);
+                               assert_eq!(update_add_htlcs.len(), 1);
+                               assert!(update_fulfill_htlcs.is_empty());
+                               assert!(update_fail_htlcs.is_empty());
+                               assert!(update_fail_malformed_htlcs.is_empty());
+                       },
+                       _ => panic!("Unexpected event"),
+               }
+       }
+       match events[if deliver_bs_raa { 2 } else { 1 }] {
+               MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, ref update_fulfill_htlcs, ref update_fail_malformed_htlcs, ref commitment_signed, .. } } => {
+                       assert!(update_add_htlcs.is_empty());
+                       assert_eq!(update_fail_htlcs.len(), 3);
+                       assert!(update_fulfill_htlcs.is_empty());
+                       assert!(update_fail_malformed_htlcs.is_empty());
+                       assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
+
+                       nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[0]).unwrap();
+                       nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[1]).unwrap();
+                       nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[2]).unwrap();
+
+                       commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, false, true);
+
+                       let events = nodes[0].node.get_and_clear_pending_msg_events();
+                       // If we delivered B's RAA we got an unknown preimage error, not something
+                       // that we should update our routing table for.
+                       assert_eq!(events.len(), if deliver_bs_raa { 2 } else { 3 });
+                       for event in events {
+                               match event {
+                                       MessageSendEvent::PaymentFailureNetworkUpdate { .. } => {},
+                                       _ => panic!("Unexpected event"),
+                               }
+                       }
+                       let events = nodes[0].node.get_and_clear_pending_events();
+                       assert_eq!(events.len(), 3);
+                       match events[0] {
+                               Event::PaymentFailed { ref payment_hash, .. } => {
+                                       assert!(failed_htlcs.insert(payment_hash.0));
+                               },
+                               _ => panic!("Unexpected event"),
+                       }
+                       match events[1] {
+                               Event::PaymentFailed { ref payment_hash, .. } => {
+                                       assert!(failed_htlcs.insert(payment_hash.0));
+                               },
+                               _ => panic!("Unexpected event"),
+                       }
+                       match events[2] {
+                               Event::PaymentFailed { ref payment_hash, .. } => {
+                                       assert!(failed_htlcs.insert(payment_hash.0));
+                               },
+                               _ => panic!("Unexpected event"),
+                       }
+               },
+               _ => panic!("Unexpected event"),
+       }
+
+       assert!(failed_htlcs.contains(&first_payment_hash.0));
+       assert!(failed_htlcs.contains(&second_payment_hash.0));
+       assert!(failed_htlcs.contains(&third_payment_hash.0));
+}
+
+#[test]
+fn test_commitment_revoked_fail_backward_exhaustive_a() {
+       do_test_commitment_revoked_fail_backward_exhaustive(false, true, false);
+       do_test_commitment_revoked_fail_backward_exhaustive(true, true, false);
+       do_test_commitment_revoked_fail_backward_exhaustive(false, false, false);
+       do_test_commitment_revoked_fail_backward_exhaustive(true, false, false);
+}
+
+#[test]
+fn test_commitment_revoked_fail_backward_exhaustive_b() {
+       do_test_commitment_revoked_fail_backward_exhaustive(false, true, true);
+       do_test_commitment_revoked_fail_backward_exhaustive(true, true, true);
+       do_test_commitment_revoked_fail_backward_exhaustive(false, false, true);
+       do_test_commitment_revoked_fail_backward_exhaustive(true, false, true);
+}
+
+#[test]
+fn test_htlc_ignore_latest_remote_commitment() {
+       // Test that HTLC transactions spending the latest remote commitment transaction are simply
+       // ignored if we cannot claim them. This originally tickled an invalid unwrap().
+       let nodes = create_network(2, &[None, None]);
+       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       route_payment(&nodes[0], &[&nodes[1]], 10000000);
+       nodes[0].node.force_close_channel(&nodes[0].node.list_channels()[0].channel_id);
+       check_closed_broadcast!(nodes[0]);
+
+       let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
+       assert_eq!(node_txn.len(), 2);
+
+       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+       nodes[1].chain_monitor.block_connected_checked(&header, 1, &[&node_txn[0], &node_txn[1]], &[1; 2]);
+       check_closed_broadcast!(nodes[1]);
+
+       // Duplicate the block_connected call since this may happen due to other listeners
+       // registering new transactions
+       nodes[1].chain_monitor.block_connected_checked(&header, 1, &[&node_txn[0], &node_txn[1]], &[1; 2]);
+}
+
+#[test]
+fn test_force_close_fail_back() {
+       // Check which HTLCs are failed-backwards on channel force-closure
+       let mut nodes = create_network(3, &[None, None, None]);
+       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+       create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
+
+       let route = nodes[0].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 1000000, 42).unwrap();
+
+       let (our_payment_preimage, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
+
+       let mut payment_event = {
+               nodes[0].node.send_payment(route, our_payment_hash).unwrap();
+               check_added_monitors!(nodes[0], 1);
+
+               let mut events = nodes[0].node.get_and_clear_pending_msg_events();
+               assert_eq!(events.len(), 1);
+               SendEvent::from_event(events.remove(0))
+       };
+
+       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
+       commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
+
+       expect_pending_htlcs_forwardable!(nodes[1]);
+
+       let mut events_2 = nodes[1].node.get_and_clear_pending_msg_events();
+       assert_eq!(events_2.len(), 1);
+       payment_event = SendEvent::from_event(events_2.remove(0));
+       assert_eq!(payment_event.msgs.len(), 1);
+
+       check_added_monitors!(nodes[1], 1);
+       nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
+       nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &payment_event.commitment_msg).unwrap();
+       check_added_monitors!(nodes[2], 1);
+       let (_, _) = get_revoke_commit_msgs!(nodes[2], nodes[1].node.get_our_node_id());
+
+       // nodes[2] now has the latest commitment transaction, but hasn't revoked its previous
+       // state or updated nodes[1]' state. Now force-close and broadcast that commitment/HTLC
+       // transaction and ensure nodes[1] doesn't fail-backwards (this was originally a bug!).
+
+       nodes[2].node.force_close_channel(&payment_event.commitment_msg.channel_id);
+       check_closed_broadcast!(nodes[2]);
+       let tx = {
+               let mut node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap();
+               // Note that we don't bother broadcasting the HTLC-Success transaction here as we don't
+               // have a use for it unless nodes[2] learns the preimage somehow, the funds will go
+               // back to nodes[1] upon timeout otherwise.
+               assert_eq!(node_txn.len(), 1);
+               node_txn.remove(0)
+       };
+
+       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+       nodes[1].chain_monitor.block_connected_checked(&header, 1, &[&tx], &[1]);
+
+       // Note no UpdateHTLCs event here from nodes[1] to nodes[0]!
+       check_closed_broadcast!(nodes[1]);
+
+       // Now check that if we add the preimage to ChannelMonitor it broadcasts our HTLC-Success..
+       {
+               let mut monitors = nodes[2].chan_monitor.simple_monitor.monitors.lock().unwrap();
+               monitors.get_mut(&OutPoint::new(Sha256dHash::from_slice(&payment_event.commitment_msg.channel_id[..]).unwrap(), 0)).unwrap()
+                       .provide_payment_preimage(&our_payment_hash, &our_payment_preimage);
+       }
+       nodes[2].chain_monitor.block_connected_checked(&header, 1, &[&tx], &[1]);
+       let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap();
+       assert_eq!(node_txn.len(), 1);
+       assert_eq!(node_txn[0].input.len(), 1);
+       assert_eq!(node_txn[0].input[0].previous_output.txid, tx.txid());
+       assert_eq!(node_txn[0].lock_time, 0); // Must be an HTLC-Success
+       assert_eq!(node_txn[0].input[0].witness.len(), 5); // Must be an HTLC-Success
+
+       check_spends!(node_txn[0], tx);
+}
+
+#[test]
+fn test_unconf_chan() {
+       // After creating a chan between nodes, we disconnect all blocks previously seen to force a channel close on nodes[0] side
+       let nodes = create_network(2, &[None, None]);
+       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       let channel_state = nodes[0].node.channel_state.lock().unwrap();
+       assert_eq!(channel_state.by_id.len(), 1);
+       assert_eq!(channel_state.short_to_id.len(), 1);
+       mem::drop(channel_state);
+
+       let mut headers = Vec::new();
+       let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+       headers.push(header.clone());
+       for _i in 2..100 {
+               header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+               headers.push(header.clone());
+       }
+       let mut height = 99;
+       while !headers.is_empty() {
+               nodes[0].node.block_disconnected(&headers.pop().unwrap(), height);
+               height -= 1;
+       }
+       check_closed_broadcast!(nodes[0]);
+       let channel_state = nodes[0].node.channel_state.lock().unwrap();
+       assert_eq!(channel_state.by_id.len(), 0);
+       assert_eq!(channel_state.short_to_id.len(), 0);
+}
+
+#[test]
+fn test_simple_peer_disconnect() {
+       // Test that we can reconnect when there are no lost messages
+       let nodes = create_network(3, &[None, None, None]);
+       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+       create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
+
+       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
+       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
+       reconnect_nodes(&nodes[0], &nodes[1], (true, true), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
+
+       let payment_preimage_1 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).0;
+       let payment_hash_2 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).1;
+       fail_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), payment_hash_2);
+       claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), payment_preimage_1);
+
+       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
+       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
+       reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
+
+       let payment_preimage_3 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).0;
+       let payment_preimage_4 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).0;
+       let payment_hash_5 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).1;
+       let payment_hash_6 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).1;
+
+       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
+       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
+
+       claim_payment_along_route(&nodes[0], &vec!(&nodes[1], &nodes[2]), true, payment_preimage_3);
+       fail_payment_along_route(&nodes[0], &[&nodes[1], &nodes[2]], true, payment_hash_5);
+
+       reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (1, 0), (1, 0), (false, false));
+       {
+               let events = nodes[0].node.get_and_clear_pending_events();
+               assert_eq!(events.len(), 2);
+               match events[0] {
+                       Event::PaymentSent { payment_preimage } => {
+                               assert_eq!(payment_preimage, payment_preimage_3);
+                       },
+                       _ => panic!("Unexpected event"),
+               }
+               match events[1] {
+                       Event::PaymentFailed { payment_hash, rejected_by_dest, .. } => {
+                               assert_eq!(payment_hash, payment_hash_5);
+                               assert!(rejected_by_dest);
+                       },
+                       _ => panic!("Unexpected event"),
+               }
+       }
+
+       claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), payment_preimage_4);
+       fail_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), payment_hash_6);
+}
+
+fn do_test_drop_messages_peer_disconnect(messages_delivered: u8) {
+       // Test that we can reconnect when in-flight HTLC updates get dropped
+       let mut nodes = create_network(2, &[None, None]);
+       if messages_delivered == 0 {
+               create_chan_between_nodes_with_value_a(&nodes[0], &nodes[1], 100000, 10001, LocalFeatures::new(), LocalFeatures::new());
+               // nodes[1] doesn't receive the funding_locked message (it'll be re-sent on reconnect)
+       } else {
+               create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+       }
+
+       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), Some(&nodes[0].node.list_usable_channels()), &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
+       let (payment_preimage_1, payment_hash_1) = get_payment_preimage_hash!(nodes[0]);
+
+       let payment_event = {
+               nodes[0].node.send_payment(route.clone(), payment_hash_1).unwrap();
+               check_added_monitors!(nodes[0], 1);
+
+               let mut events = nodes[0].node.get_and_clear_pending_msg_events();
+               assert_eq!(events.len(), 1);
+               SendEvent::from_event(events.remove(0))
+       };
+       assert_eq!(nodes[1].node.get_our_node_id(), payment_event.node_id);
+
+       if messages_delivered < 2 {
+               // Drop the payment_event messages, and let them get re-generated in reconnect_nodes!
+       } else {
+               nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
+               if messages_delivered >= 3 {
+                       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg).unwrap();
+                       check_added_monitors!(nodes[1], 1);
+                       let (bs_revoke_and_ack, bs_commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+
+                       if messages_delivered >= 4 {
+                               nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
+                               assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
+                               check_added_monitors!(nodes[0], 1);
+
+                               if messages_delivered >= 5 {
+                                       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_commitment_signed).unwrap();
+                                       let as_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
+                                       // No commitment_signed so get_event_msg's assert(len == 1) passes
+                                       check_added_monitors!(nodes[0], 1);
+
+                                       if messages_delivered >= 6 {
+                                               nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_and_ack).unwrap();
+                                               assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+                                               check_added_monitors!(nodes[1], 1);
+                                       }
+                               }
+                       }
+               }
+       }
+
+       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
+       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
+       if messages_delivered < 3 {
+               // Even if the funding_locked messages get exchanged, as long as nothing further was
+               // received on either side, both sides will need to resend them.
+               reconnect_nodes(&nodes[0], &nodes[1], (true, true), (0, 1), (0, 0), (0, 0), (0, 0), (false, false));
+       } else if messages_delivered == 3 {
+               // nodes[0] still wants its RAA + commitment_signed
+               reconnect_nodes(&nodes[0], &nodes[1], (false, false), (-1, 0), (0, 0), (0, 0), (0, 0), (true, false));
+       } else if messages_delivered == 4 {
+               // nodes[0] still wants its commitment_signed
+               reconnect_nodes(&nodes[0], &nodes[1], (false, false), (-1, 0), (0, 0), (0, 0), (0, 0), (false, false));
+       } else if messages_delivered == 5 {
+               // nodes[1] still wants its final RAA
+               reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, true));
+       } else if messages_delivered == 6 {
+               // Everything was delivered...
+               reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
+       }
+
+       let events_1 = nodes[1].node.get_and_clear_pending_events();
+       assert_eq!(events_1.len(), 1);
+       match events_1[0] {
+               Event::PendingHTLCsForwardable { .. } => { },
+               _ => panic!("Unexpected event"),
+       };
+
+       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
+       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
+       reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
+
+       nodes[1].node.process_pending_htlc_forwards();
+
+       let events_2 = nodes[1].node.get_and_clear_pending_events();
+       assert_eq!(events_2.len(), 1);
+       match events_2[0] {
+               Event::PaymentReceived { ref payment_hash, amt } => {
+                       assert_eq!(payment_hash_1, *payment_hash);
+                       assert_eq!(amt, 1000000);
+               },
+               _ => panic!("Unexpected event"),
+       }
+
+       nodes[1].node.claim_funds(payment_preimage_1);
+       check_added_monitors!(nodes[1], 1);
+
+       let events_3 = nodes[1].node.get_and_clear_pending_msg_events();
+       assert_eq!(events_3.len(), 1);
+       let (update_fulfill_htlc, commitment_signed) = match events_3[0] {
+               MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
+                       assert_eq!(*node_id, nodes[0].node.get_our_node_id());
+                       assert!(updates.update_add_htlcs.is_empty());
+                       assert!(updates.update_fail_htlcs.is_empty());
+                       assert_eq!(updates.update_fulfill_htlcs.len(), 1);
+                       assert!(updates.update_fail_malformed_htlcs.is_empty());
+                       assert!(updates.update_fee.is_none());
+                       (updates.update_fulfill_htlcs[0].clone(), updates.commitment_signed.clone())
+               },
+               _ => panic!("Unexpected event"),
+       };
+
+       if messages_delivered >= 1 {
+               nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_htlc).unwrap();
+
+               let events_4 = nodes[0].node.get_and_clear_pending_events();
+               assert_eq!(events_4.len(), 1);
+               match events_4[0] {
+                       Event::PaymentSent { ref payment_preimage } => {
+                               assert_eq!(payment_preimage_1, *payment_preimage);
+                       },
+                       _ => panic!("Unexpected event"),
+               }
+
+               if messages_delivered >= 2 {
+                       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed).unwrap();
+                       check_added_monitors!(nodes[0], 1);
+                       let (as_revoke_and_ack, as_commitment_signed) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
+
+                       if messages_delivered >= 3 {
+                               nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_and_ack).unwrap();
+                               assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+                               check_added_monitors!(nodes[1], 1);
+
+                               if messages_delivered >= 4 {
+                                       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_commitment_signed).unwrap();
+                                       let bs_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
+                                       // No commitment_signed so get_event_msg's assert(len == 1) passes
+                                       check_added_monitors!(nodes[1], 1);
+
+                                       if messages_delivered >= 5 {
+                                               nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
+                                               assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
+                                               check_added_monitors!(nodes[0], 1);
+                                       }
+                               }
+                       }
+               }
+       }
+
+       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
+       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
+       if messages_delivered < 2 {
+               reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (1, 0), (0, 0), (0, 0), (false, false));
+               //TODO: Deduplicate PaymentSent events, then enable this if:
+               //if messages_delivered < 1 {
+                       let events_4 = nodes[0].node.get_and_clear_pending_events();
+                       assert_eq!(events_4.len(), 1);
+                       match events_4[0] {
+                               Event::PaymentSent { ref payment_preimage } => {
+                                       assert_eq!(payment_preimage_1, *payment_preimage);
+                               },
+                               _ => panic!("Unexpected event"),
+                       }
+               //}
+       } else if messages_delivered == 2 {
+               // nodes[0] still wants its RAA + commitment_signed
+               reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, -1), (0, 0), (0, 0), (0, 0), (false, true));
+       } else if messages_delivered == 3 {
+               // nodes[0] still wants its commitment_signed
+               reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, -1), (0, 0), (0, 0), (0, 0), (false, false));
+       } else if messages_delivered == 4 {
+               // nodes[1] still wants its final RAA
+               reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (true, false));
+       } else if messages_delivered == 5 {
+               // Everything was delivered...
+               reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
+       }
+
+       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
+       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
+       reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
+
+       // Channel should still work fine...
+       let payment_preimage_2 = send_along_route(&nodes[0], route, &[&nodes[1]], 1000000).0;
+       claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
+}
+
+#[test]
+fn test_drop_messages_peer_disconnect_a() {
+       do_test_drop_messages_peer_disconnect(0);
+       do_test_drop_messages_peer_disconnect(1);
+       do_test_drop_messages_peer_disconnect(2);
+       do_test_drop_messages_peer_disconnect(3);
+}
+
+#[test]
+fn test_drop_messages_peer_disconnect_b() {
+       do_test_drop_messages_peer_disconnect(4);
+       do_test_drop_messages_peer_disconnect(5);
+       do_test_drop_messages_peer_disconnect(6);
+}
+
+#[test]
+fn test_funding_peer_disconnect() {
+       // Test that we can lock in our funding tx while disconnected
+       let nodes = create_network(2, &[None, None]);
+       let tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 100000, 10001, LocalFeatures::new(), LocalFeatures::new());
+
+       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
+       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
+
+       confirm_transaction(&nodes[0].chain_monitor, &tx, tx.version);
+       let events_1 = nodes[0].node.get_and_clear_pending_msg_events();
+       assert_eq!(events_1.len(), 1);
+       match events_1[0] {
+               MessageSendEvent::SendFundingLocked { ref node_id, msg: _ } => {
+                       assert_eq!(*node_id, nodes[1].node.get_our_node_id());
+               },
+               _ => panic!("Unexpected event"),
+       }
+
+       reconnect_nodes(&nodes[0], &nodes[1], (false, true), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
+
+       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
+       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
+
+       confirm_transaction(&nodes[1].chain_monitor, &tx, tx.version);
+       let events_2 = nodes[1].node.get_and_clear_pending_msg_events();
+       assert_eq!(events_2.len(), 2);
+       match events_2[0] {
+               MessageSendEvent::SendFundingLocked { ref node_id, msg: _ } => {
+                       assert_eq!(*node_id, nodes[0].node.get_our_node_id());
+               },
+               _ => panic!("Unexpected event"),
+       }
+       match events_2[1] {
+               MessageSendEvent::SendAnnouncementSignatures { ref node_id, msg: _ } => {
+                       assert_eq!(*node_id, nodes[0].node.get_our_node_id());
+               },
+               _ => panic!("Unexpected event"),
+       }
+
+       reconnect_nodes(&nodes[0], &nodes[1], (true, true), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
+
+       // TODO: We shouldn't need to manually pass list_usable_chanels here once we support
+       // rebroadcasting announcement_signatures upon reconnect.
+
+       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), Some(&nodes[0].node.list_usable_channels()), &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
+       let (payment_preimage, _) = send_along_route(&nodes[0], route, &[&nodes[1]], 1000000);
+       claim_payment(&nodes[0], &[&nodes[1]], payment_preimage);
+}
+
+#[test]
+fn test_drop_messages_peer_disconnect_dual_htlc() {
+       // Test that we can handle reconnecting when both sides of a channel have pending
+       // commitment_updates when we disconnect.
+       let mut nodes = create_network(2, &[None, None]);
+       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       let (payment_preimage_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
+
+       // Now try to send a second payment which will fail to send
+       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
+       let (payment_preimage_2, payment_hash_2) = get_payment_preimage_hash!(nodes[0]);
+
+       nodes[0].node.send_payment(route.clone(), payment_hash_2).unwrap();
+       check_added_monitors!(nodes[0], 1);
+
+       let events_1 = nodes[0].node.get_and_clear_pending_msg_events();
+       assert_eq!(events_1.len(), 1);
+       match events_1[0] {
+               MessageSendEvent::UpdateHTLCs { .. } => {},
+               _ => panic!("Unexpected event"),
+       }
+
+       assert!(nodes[1].node.claim_funds(payment_preimage_1));
+       check_added_monitors!(nodes[1], 1);
+
+       let events_2 = nodes[1].node.get_and_clear_pending_msg_events();
+       assert_eq!(events_2.len(), 1);
+       match events_2[0] {
+               MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => {
+                       assert_eq!(*node_id, nodes[0].node.get_our_node_id());
+                       assert!(update_add_htlcs.is_empty());
+                       assert_eq!(update_fulfill_htlcs.len(), 1);
+                       assert!(update_fail_htlcs.is_empty());
+                       assert!(update_fail_malformed_htlcs.is_empty());
+                       assert!(update_fee.is_none());
+
+                       nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_htlcs[0]).unwrap();
+                       let events_3 = nodes[0].node.get_and_clear_pending_events();
+                       assert_eq!(events_3.len(), 1);
+                       match events_3[0] {
+                               Event::PaymentSent { ref payment_preimage } => {
+                                       assert_eq!(*payment_preimage, payment_preimage_1);
+                               },
+                               _ => panic!("Unexpected event"),
+                       }
+
+                       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), commitment_signed).unwrap();
+                       let _ = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
+                       // No commitment_signed so get_event_msg's assert(len == 1) passes
+                       check_added_monitors!(nodes[0], 1);
+               },
+               _ => panic!("Unexpected event"),
+       }
+
+       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
+       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
+
+       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id());
+       let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
+       assert_eq!(reestablish_1.len(), 1);
+       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id());
+       let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
+       assert_eq!(reestablish_2.len(), 1);
+
+       nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]).unwrap();
+       let as_resp = handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
+       nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]).unwrap();
+       let bs_resp = handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
+
+       assert!(as_resp.0.is_none());
+       assert!(bs_resp.0.is_none());
+
+       assert!(bs_resp.1.is_none());
+       assert!(bs_resp.2.is_none());
+
+       assert!(as_resp.3 == RAACommitmentOrder::CommitmentFirst);
+
+       assert_eq!(as_resp.2.as_ref().unwrap().update_add_htlcs.len(), 1);
+       assert!(as_resp.2.as_ref().unwrap().update_fulfill_htlcs.is_empty());
+       assert!(as_resp.2.as_ref().unwrap().update_fail_htlcs.is_empty());
+       assert!(as_resp.2.as_ref().unwrap().update_fail_malformed_htlcs.is_empty());
+       assert!(as_resp.2.as_ref().unwrap().update_fee.is_none());
+       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &as_resp.2.as_ref().unwrap().update_add_htlcs[0]).unwrap();
+       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_resp.2.as_ref().unwrap().commitment_signed).unwrap();
+       let bs_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
+       // No commitment_signed so get_event_msg's assert(len == 1) passes
+       check_added_monitors!(nodes[1], 1);
+
+       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), as_resp.1.as_ref().unwrap()).unwrap();
+       let bs_second_commitment_signed = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+       assert!(bs_second_commitment_signed.update_add_htlcs.is_empty());
+       assert!(bs_second_commitment_signed.update_fulfill_htlcs.is_empty());
+       assert!(bs_second_commitment_signed.update_fail_htlcs.is_empty());
+       assert!(bs_second_commitment_signed.update_fail_malformed_htlcs.is_empty());
+       assert!(bs_second_commitment_signed.update_fee.is_none());
+       check_added_monitors!(nodes[1], 1);
+
+       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
+       let as_commitment_signed = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
+       assert!(as_commitment_signed.update_add_htlcs.is_empty());
+       assert!(as_commitment_signed.update_fulfill_htlcs.is_empty());
+       assert!(as_commitment_signed.update_fail_htlcs.is_empty());
+       assert!(as_commitment_signed.update_fail_malformed_htlcs.is_empty());
+       assert!(as_commitment_signed.update_fee.is_none());
+       check_added_monitors!(nodes[0], 1);
+
+       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_second_commitment_signed.commitment_signed).unwrap();
+       let as_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
+       // No commitment_signed so get_event_msg's assert(len == 1) passes
+       check_added_monitors!(nodes[0], 1);
+
+       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_commitment_signed.commitment_signed).unwrap();
+       let bs_second_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
+       // No commitment_signed so get_event_msg's assert(len == 1) passes
+       check_added_monitors!(nodes[1], 1);
+
+       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_and_ack).unwrap();
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+       check_added_monitors!(nodes[1], 1);
+
+       expect_pending_htlcs_forwardable!(nodes[1]);
+
+       let events_5 = nodes[1].node.get_and_clear_pending_events();
+       assert_eq!(events_5.len(), 1);
+       match events_5[0] {
+               Event::PaymentReceived { ref payment_hash, amt: _ } => {
+                       assert_eq!(payment_hash_2, *payment_hash);
+               },
+               _ => panic!("Unexpected event"),
+       }
+
+       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_revoke_and_ack).unwrap();
+       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
+       check_added_monitors!(nodes[0], 1);
+
+       claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
+}
+
+#[test]
+fn test_invalid_channel_announcement() {
+       //Test BOLT 7 channel_announcement msg requirement for final node, gather data to build customed channel_announcement msgs
+       let secp_ctx = Secp256k1::new();
+       let nodes = create_network(2, &[None, None]);
+
+       let chan_announcement = create_chan_between_nodes(&nodes[0], &nodes[1], LocalFeatures::new(), LocalFeatures::new());
+
+       let a_channel_lock = nodes[0].node.channel_state.lock().unwrap();
+       let b_channel_lock = nodes[1].node.channel_state.lock().unwrap();
+       let as_chan = a_channel_lock.by_id.get(&chan_announcement.3).unwrap();
+       let bs_chan = b_channel_lock.by_id.get(&chan_announcement.3).unwrap();
+
+       let _ = nodes[0].router.handle_htlc_fail_channel_update(&msgs::HTLCFailChannelUpdate::ChannelClosed { short_channel_id : as_chan.get_short_channel_id().unwrap(), is_permanent: false } );
+
+       let as_bitcoin_key = PublicKey::from_secret_key(&secp_ctx, &as_chan.get_local_keys().funding_key);
+       let bs_bitcoin_key = PublicKey::from_secret_key(&secp_ctx, &bs_chan.get_local_keys().funding_key);
+
+       let as_network_key = nodes[0].node.get_our_node_id();
+       let bs_network_key = nodes[1].node.get_our_node_id();
+
+       let were_node_one = as_bitcoin_key.serialize()[..] < bs_bitcoin_key.serialize()[..];
+
+       let mut chan_announcement;
+
+       macro_rules! dummy_unsigned_msg {
+               () => {
+                       msgs::UnsignedChannelAnnouncement {
+                               features: msgs::GlobalFeatures::new(),
+                               chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
+                               short_channel_id: as_chan.get_short_channel_id().unwrap(),
+                               node_id_1: if were_node_one { as_network_key } else { bs_network_key },
+                               node_id_2: if were_node_one { bs_network_key } else { as_network_key },
+                               bitcoin_key_1: if were_node_one { as_bitcoin_key } else { bs_bitcoin_key },
+                               bitcoin_key_2: if were_node_one { bs_bitcoin_key } else { as_bitcoin_key },
+                               excess_data: Vec::new(),
+                       };
+               }
+       }
+
+       macro_rules! sign_msg {
+               ($unsigned_msg: expr) => {
+                       let msghash = Message::from_slice(&Sha256dHash::hash(&$unsigned_msg.encode()[..])[..]).unwrap();
+                       let as_bitcoin_sig = secp_ctx.sign(&msghash, &as_chan.get_local_keys().funding_key);
+                       let bs_bitcoin_sig = secp_ctx.sign(&msghash, &bs_chan.get_local_keys().funding_key);
+                       let as_node_sig = secp_ctx.sign(&msghash, &nodes[0].keys_manager.get_node_secret());
+                       let bs_node_sig = secp_ctx.sign(&msghash, &nodes[1].keys_manager.get_node_secret());
+                       chan_announcement = msgs::ChannelAnnouncement {
+                               node_signature_1 : if were_node_one { as_node_sig } else { bs_node_sig},
+                               node_signature_2 : if were_node_one { bs_node_sig } else { as_node_sig},
+                               bitcoin_signature_1: if were_node_one { as_bitcoin_sig } else { bs_bitcoin_sig },
+                               bitcoin_signature_2 : if were_node_one { bs_bitcoin_sig } else { as_bitcoin_sig },
+                               contents: $unsigned_msg
+                       }
+               }
+       }
+
+       let unsigned_msg = dummy_unsigned_msg!();
+       sign_msg!(unsigned_msg);
+       assert_eq!(nodes[0].router.handle_channel_announcement(&chan_announcement).unwrap(), true);
+       let _ = nodes[0].router.handle_htlc_fail_channel_update(&msgs::HTLCFailChannelUpdate::ChannelClosed { short_channel_id : as_chan.get_short_channel_id().unwrap(), is_permanent: false } );
+
+       // Configured with Network::Testnet
+       let mut unsigned_msg = dummy_unsigned_msg!();
+       unsigned_msg.chain_hash = genesis_block(Network::Bitcoin).header.bitcoin_hash();
+       sign_msg!(unsigned_msg);
+       assert!(nodes[0].router.handle_channel_announcement(&chan_announcement).is_err());
+
+       let mut unsigned_msg = dummy_unsigned_msg!();
+       unsigned_msg.chain_hash = Sha256dHash::hash(&[1,2,3,4,5,6,7,8,9]);
+       sign_msg!(unsigned_msg);
+       assert!(nodes[0].router.handle_channel_announcement(&chan_announcement).is_err());
+}
+
+#[test]
+fn test_no_txn_manager_serialize_deserialize() {
+       let mut nodes = create_network(2, &[None, None]);
+
+       let tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 100000, 10001, LocalFeatures::new(), LocalFeatures::new());
+
+       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
+
+       let nodes_0_serialized = nodes[0].node.encode();
+       let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new());
+       nodes[0].chan_monitor.simple_monitor.monitors.lock().unwrap().iter().next().unwrap().1.write_for_disk(&mut chan_0_monitor_serialized).unwrap();
+
+       nodes[0].chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(nodes[0].chain_monitor.clone(), nodes[0].tx_broadcaster.clone(), Arc::new(test_utils::TestLogger::new()), Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 })));
+       let mut chan_0_monitor_read = &chan_0_monitor_serialized.0[..];
+       let (_, chan_0_monitor) = <(Sha256dHash, ChannelMonitor)>::read(&mut chan_0_monitor_read, Arc::new(test_utils::TestLogger::new())).unwrap();
+       assert!(chan_0_monitor_read.is_empty());
+
+       let mut nodes_0_read = &nodes_0_serialized[..];
+       let config = UserConfig::new();
+       let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&nodes[0].node_seed, Network::Testnet, Arc::new(test_utils::TestLogger::new())));
+       let (_, nodes_0_deserialized) = {
+               let mut channel_monitors = HashMap::new();
+               channel_monitors.insert(chan_0_monitor.get_funding_txo().unwrap(), &chan_0_monitor);
+               <(Sha256dHash, ChannelManager)>::read(&mut nodes_0_read, ChannelManagerReadArgs {
+                       default_config: config,
+                       keys_manager,
+                       fee_estimator: Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 }),
+                       monitor: nodes[0].chan_monitor.clone(),
+                       chain_monitor: nodes[0].chain_monitor.clone(),
+                       tx_broadcaster: nodes[0].tx_broadcaster.clone(),
+                       logger: Arc::new(test_utils::TestLogger::new()),
+                       channel_monitors: &channel_monitors,
+               }).unwrap()
+       };
+       assert!(nodes_0_read.is_empty());
+
+       assert!(nodes[0].chan_monitor.add_update_monitor(chan_0_monitor.get_funding_txo().unwrap(), chan_0_monitor).is_ok());
+       nodes[0].node = Arc::new(nodes_0_deserialized);
+       let nodes_0_as_listener: Arc<ChainListener> = nodes[0].node.clone();
+       nodes[0].chain_monitor.register_listener(Arc::downgrade(&nodes_0_as_listener));
+       assert_eq!(nodes[0].node.list_channels().len(), 1);
+       check_added_monitors!(nodes[0], 1);
+
+       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id());
+       let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
+       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id());
+       let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
+
+       nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]).unwrap();
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+       nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]).unwrap();
+       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
+
+       let (funding_locked, _) = create_chan_between_nodes_with_value_confirm(&nodes[0], &nodes[1], &tx);
+       let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &funding_locked);
+       for node in nodes.iter() {
+               assert!(node.router.handle_channel_announcement(&announcement).unwrap());
+               node.router.handle_channel_update(&as_update).unwrap();
+               node.router.handle_channel_update(&bs_update).unwrap();
+       }
+
+       send_payment(&nodes[0], &[&nodes[1]], 1000000);
+}
+
+#[test]
+fn test_simple_manager_serialize_deserialize() {
+       let mut nodes = create_network(2, &[None, None]);
+       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       let (our_payment_preimage, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
+       let (_, our_payment_hash) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
+
+       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
+
+       let nodes_0_serialized = nodes[0].node.encode();
+       let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new());
+       nodes[0].chan_monitor.simple_monitor.monitors.lock().unwrap().iter().next().unwrap().1.write_for_disk(&mut chan_0_monitor_serialized).unwrap();
+
+       nodes[0].chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(nodes[0].chain_monitor.clone(), nodes[0].tx_broadcaster.clone(), Arc::new(test_utils::TestLogger::new()), Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 })));
+       let mut chan_0_monitor_read = &chan_0_monitor_serialized.0[..];
+       let (_, chan_0_monitor) = <(Sha256dHash, ChannelMonitor)>::read(&mut chan_0_monitor_read, Arc::new(test_utils::TestLogger::new())).unwrap();
+       assert!(chan_0_monitor_read.is_empty());
+
+       let mut nodes_0_read = &nodes_0_serialized[..];
+       let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&nodes[0].node_seed, Network::Testnet, Arc::new(test_utils::TestLogger::new())));
+       let (_, nodes_0_deserialized) = {
+               let mut channel_monitors = HashMap::new();
+               channel_monitors.insert(chan_0_monitor.get_funding_txo().unwrap(), &chan_0_monitor);
+               <(Sha256dHash, ChannelManager)>::read(&mut nodes_0_read, ChannelManagerReadArgs {
+                       default_config: UserConfig::new(),
+                       keys_manager,
+                       fee_estimator: Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 }),
+                       monitor: nodes[0].chan_monitor.clone(),
+                       chain_monitor: nodes[0].chain_monitor.clone(),
+                       tx_broadcaster: nodes[0].tx_broadcaster.clone(),
+                       logger: Arc::new(test_utils::TestLogger::new()),
+                       channel_monitors: &channel_monitors,
+               }).unwrap()
+       };
+       assert!(nodes_0_read.is_empty());
+
+       assert!(nodes[0].chan_monitor.add_update_monitor(chan_0_monitor.get_funding_txo().unwrap(), chan_0_monitor).is_ok());
+       nodes[0].node = Arc::new(nodes_0_deserialized);
+       check_added_monitors!(nodes[0], 1);
+
+       reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
+
+       fail_payment(&nodes[0], &[&nodes[1]], our_payment_hash);
+       claim_payment(&nodes[0], &[&nodes[1]], our_payment_preimage);
+}
+
+#[test]
+fn test_manager_serialize_deserialize_inconsistent_monitor() {
+       // Test deserializing a ChannelManager with an out-of-date ChannelMonitor
+       let mut nodes = create_network(4, &[None, None, None, None]);
+       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+       create_announced_chan_between_nodes(&nodes, 2, 0, LocalFeatures::new(), LocalFeatures::new());
+       let (_, _, channel_id, funding_tx) = create_announced_chan_between_nodes(&nodes, 0, 3, LocalFeatures::new(), LocalFeatures::new());
+
+       let (our_payment_preimage, _) = route_payment(&nodes[2], &[&nodes[0], &nodes[1]], 1000000);
+
+       // Serialize the ChannelManager here, but the monitor we keep up-to-date
+       let nodes_0_serialized = nodes[0].node.encode();
+
+       route_payment(&nodes[0], &[&nodes[3]], 1000000);
+       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
+       nodes[2].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
+       nodes[3].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
+
+       // Now the ChannelMonitor (which is now out-of-sync with ChannelManager for channel w/
+       // nodes[3])
+       let mut node_0_monitors_serialized = Vec::new();
+       for monitor in nodes[0].chan_monitor.simple_monitor.monitors.lock().unwrap().iter() {
+               let mut writer = test_utils::TestVecWriter(Vec::new());
+               monitor.1.write_for_disk(&mut writer).unwrap();
+               node_0_monitors_serialized.push(writer.0);
+       }
+
+       nodes[0].chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(nodes[0].chain_monitor.clone(), nodes[0].tx_broadcaster.clone(), Arc::new(test_utils::TestLogger::new()), Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 })));
+       let mut node_0_monitors = Vec::new();
+       for serialized in node_0_monitors_serialized.iter() {
+               let mut read = &serialized[..];
+               let (_, monitor) = <(Sha256dHash, ChannelMonitor)>::read(&mut read, Arc::new(test_utils::TestLogger::new())).unwrap();
+               assert!(read.is_empty());
+               node_0_monitors.push(monitor);
+       }
+
+       let mut nodes_0_read = &nodes_0_serialized[..];
+       let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&nodes[0].node_seed, Network::Testnet, Arc::new(test_utils::TestLogger::new())));
+       let (_, nodes_0_deserialized) = <(Sha256dHash, ChannelManager)>::read(&mut nodes_0_read, ChannelManagerReadArgs {
+               default_config: UserConfig::new(),
+               keys_manager,
+               fee_estimator: Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 }),
+               monitor: nodes[0].chan_monitor.clone(),
+               chain_monitor: nodes[0].chain_monitor.clone(),
+               tx_broadcaster: nodes[0].tx_broadcaster.clone(),
+               logger: Arc::new(test_utils::TestLogger::new()),
+               channel_monitors: &node_0_monitors.iter().map(|monitor| { (monitor.get_funding_txo().unwrap(), monitor) }).collect(),
+       }).unwrap();
+       assert!(nodes_0_read.is_empty());
+
+       { // Channel close should result in a commitment tx and an HTLC tx
+               let txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
+               assert_eq!(txn.len(), 2);
+               assert_eq!(txn[0].input[0].previous_output.txid, funding_tx.txid());
+               assert_eq!(txn[1].input[0].previous_output.txid, txn[0].txid());
+       }
+
+       for monitor in node_0_monitors.drain(..) {
+               assert!(nodes[0].chan_monitor.add_update_monitor(monitor.get_funding_txo().unwrap(), monitor).is_ok());
+               check_added_monitors!(nodes[0], 1);
+       }
+       nodes[0].node = Arc::new(nodes_0_deserialized);
+
+       // nodes[1] and nodes[2] have no lost state with nodes[0]...
+       reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
+       reconnect_nodes(&nodes[0], &nodes[2], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
+       //... and we can even still claim the payment!
+       claim_payment(&nodes[2], &[&nodes[0], &nodes[1]], our_payment_preimage);
+
+       nodes[3].node.peer_connected(&nodes[0].node.get_our_node_id());
+       let reestablish = get_event_msg!(nodes[3], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id());
+       nodes[0].node.peer_connected(&nodes[3].node.get_our_node_id());
+       if let Err(msgs::HandleError { action: Some(msgs::ErrorAction::SendErrorMessage { msg }), .. }) = nodes[0].node.handle_channel_reestablish(&nodes[3].node.get_our_node_id(), &reestablish) {
+               assert_eq!(msg.channel_id, channel_id);
+       } else { panic!("Unexpected result"); }
+}
+
+macro_rules! check_spendable_outputs {
+       ($node: expr, $der_idx: expr) => {
+               {
+                       let events = $node.chan_monitor.simple_monitor.get_and_clear_pending_events();
+                       let mut txn = Vec::new();
+                       for event in events {
+                               match event {
+                                       Event::SpendableOutputs { ref outputs } => {
+                                               for outp in outputs {
+                                                       match *outp {
+                                                               SpendableOutputDescriptor::DynamicOutputP2WPKH { ref outpoint, ref key, ref output } => {
+                                                                       let input = TxIn {
+                                                                               previous_output: outpoint.clone(),
+                                                                               script_sig: Script::new(),
+                                                                               sequence: 0,
+                                                                               witness: Vec::new(),
+                                                                       };
+                                                                       let outp = TxOut {
+                                                                               script_pubkey: Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(),
+                                                                               value: output.value,
+                                                                       };
+                                                                       let mut spend_tx = Transaction {
+                                                                               version: 2,
+                                                                               lock_time: 0,
+                                                                               input: vec![input],
+                                                                               output: vec![outp],
+                                                                       };
+                                                                       let secp_ctx = Secp256k1::new();
+                                                                       let remotepubkey = PublicKey::from_secret_key(&secp_ctx, &key);
+                                                                       let witness_script = Address::p2pkh(&::bitcoin::PublicKey{compressed: true, key: remotepubkey}, Network::Testnet).script_pubkey();
+                                                                       let sighash = Message::from_slice(&bip143::SighashComponents::new(&spend_tx).sighash_all(&spend_tx.input[0], &witness_script, output.value)[..]).unwrap();
+                                                                       let remotesig = secp_ctx.sign(&sighash, key);
+                                                                       spend_tx.input[0].witness.push(remotesig.serialize_der().to_vec());
+                                                                       spend_tx.input[0].witness[0].push(SigHashType::All as u8);
+                                                                       spend_tx.input[0].witness.push(remotepubkey.serialize().to_vec());
+                                                                       txn.push(spend_tx);
+                                                               },
+                                                               SpendableOutputDescriptor::DynamicOutputP2WSH { ref outpoint, ref key, ref witness_script, ref to_self_delay, ref output } => {
+                                                                       let input = TxIn {
+                                                                               previous_output: outpoint.clone(),
+                                                                               script_sig: Script::new(),
+                                                                               sequence: *to_self_delay as u32,
+                                                                               witness: Vec::new(),
+                                                                       };
+                                                                       let outp = TxOut {
+                                                                               script_pubkey: Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(),
+                                                                               value: output.value,
+                                                                       };
+                                                                       let mut spend_tx = Transaction {
+                                                                               version: 2,
+                                                                               lock_time: 0,
+                                                                               input: vec![input],
+                                                                               output: vec![outp],
+                                                                       };
+                                                                       let secp_ctx = Secp256k1::new();
+                                                                       let sighash = Message::from_slice(&bip143::SighashComponents::new(&spend_tx).sighash_all(&spend_tx.input[0], witness_script, output.value)[..]).unwrap();
+                                                                       let local_delaysig = secp_ctx.sign(&sighash, key);
+                                                                       spend_tx.input[0].witness.push(local_delaysig.serialize_der().to_vec());
+                                                                       spend_tx.input[0].witness[0].push(SigHashType::All as u8);
+                                                                       spend_tx.input[0].witness.push(vec!(0));
+                                                                       spend_tx.input[0].witness.push(witness_script.clone().into_bytes());
+                                                                       txn.push(spend_tx);
+                                                               },
+                                                               SpendableOutputDescriptor::StaticOutput { ref outpoint, ref output } => {
+                                                                       let secp_ctx = Secp256k1::new();
+                                                                       let input = TxIn {
+                                                                               previous_output: outpoint.clone(),
+                                                                               script_sig: Script::new(),
+                                                                               sequence: 0,
+                                                                               witness: Vec::new(),
+                                                                       };
+                                                                       let outp = TxOut {
+                                                                               script_pubkey: Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(),
+                                                                               value: output.value,
+                                                                       };
+                                                                       let mut spend_tx = Transaction {
+                                                                               version: 2,
+                                                                               lock_time: 0,
+                                                                               input: vec![input],
+                                                                               output: vec![outp.clone()],
+                                                                       };
+                                                                       let secret = {
+                                                                               match ExtendedPrivKey::new_master(Network::Testnet, &$node.node_seed) {
+                                                                                       Ok(master_key) => {
+                                                                                               match master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx($der_idx).expect("key space exhausted")) {
+                                                                                                       Ok(key) => key,
+                                                                                                       Err(_) => panic!("Your RNG is busted"),
+                                                                                               }
+                                                                                       }
+                                                                                       Err(_) => panic!("Your rng is busted"),
+                                                                               }
+                                                                       };
+                                                                       let pubkey = ExtendedPubKey::from_private(&secp_ctx, &secret).public_key;
+                                                                       let witness_script = Address::p2pkh(&pubkey, Network::Testnet).script_pubkey();
+                                                                       let sighash = Message::from_slice(&bip143::SighashComponents::new(&spend_tx).sighash_all(&spend_tx.input[0], &witness_script, output.value)[..]).unwrap();
+                                                                       let sig = secp_ctx.sign(&sighash, &secret.private_key.key);
+                                                                       spend_tx.input[0].witness.push(sig.serialize_der().to_vec());
+                                                                       spend_tx.input[0].witness[0].push(SigHashType::All as u8);
+                                                                       spend_tx.input[0].witness.push(pubkey.key.serialize().to_vec());
+                                                                       txn.push(spend_tx);
+                                                               },
+                                                       }
+                                               }
+                                       },
+                                       _ => panic!("Unexpected event"),
+                               };
+                       }
+                       txn
+               }
+       }
+}
+
+#[test]
+fn test_claim_sizeable_push_msat() {
+       // Incidentally test SpendableOutput event generation due to detection of to_local output on commitment tx
+       let nodes = create_network(2, &[None, None]);
+
+       let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 99000000, LocalFeatures::new(), LocalFeatures::new());
+       nodes[1].node.force_close_channel(&chan.2);
+       check_closed_broadcast!(nodes[1]);
+       let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
+       assert_eq!(node_txn.len(), 1);
+       check_spends!(node_txn[0], chan.3.clone());
+       assert_eq!(node_txn[0].output.len(), 2); // We can't force trimming of to_remote output as channel_reserve_satoshis block us to do so at channel opening
+
+       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn[0].clone()] }, 0);
+       let spend_txn = check_spendable_outputs!(nodes[1], 1);
+       assert_eq!(spend_txn.len(), 1);
+       check_spends!(spend_txn[0], node_txn[0].clone());
+}
+
+#[test]
+fn test_claim_on_remote_sizeable_push_msat() {
+       // Same test as previous, just test on remote commitment tx, as per_commitment_point registration changes following you're funder/fundee and
+       // to_remote output is encumbered by a P2WPKH
+
+       let nodes = create_network(2, &[None, None]);
+
+       let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 99000000, LocalFeatures::new(), LocalFeatures::new());
+       nodes[0].node.force_close_channel(&chan.2);
+       check_closed_broadcast!(nodes[0]);
+
+       let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
+       assert_eq!(node_txn.len(), 1);
+       check_spends!(node_txn[0], chan.3.clone());
+       assert_eq!(node_txn[0].output.len(), 2); // We can't force trimming of to_remote output as channel_reserve_satoshis block us to do so at channel opening
+
+       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn[0].clone()] }, 0);
+       check_closed_broadcast!(nodes[1]);
+       let spend_txn = check_spendable_outputs!(nodes[1], 1);
+       assert_eq!(spend_txn.len(), 2);
+       assert_eq!(spend_txn[0], spend_txn[1]);
+       check_spends!(spend_txn[0], node_txn[0].clone());
+}
+
+#[test]
+fn test_claim_on_remote_revoked_sizeable_push_msat() {
+       // Same test as previous, just test on remote revoked commitment tx, as per_commitment_point registration changes following you're funder/fundee and
+       // to_remote output is encumbered by a P2WPKH
+
+       let nodes = create_network(2, &[None, None]);
+
+       let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 59000000, LocalFeatures::new(), LocalFeatures::new());
+       let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
+       let revoked_local_txn = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone();
+       assert_eq!(revoked_local_txn[0].input.len(), 1);
+       assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan.3.txid());
+
+       claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
+       let  header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
+       check_closed_broadcast!(nodes[1]);
+
+       let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
+       let spend_txn = check_spendable_outputs!(nodes[1], 1);
+       assert_eq!(spend_txn.len(), 4);
+       assert_eq!(spend_txn[0], spend_txn[2]); // to_remote output on revoked remote commitment_tx
+       check_spends!(spend_txn[0], revoked_local_txn[0].clone());
+       assert_eq!(spend_txn[1], spend_txn[3]); // to_local output on local commitment tx
+       check_spends!(spend_txn[1], node_txn[0].clone());
+}
+
+#[test]
+fn test_static_spendable_outputs_preimage_tx() {
+       let nodes = create_network(2, &[None, None]);
+
+       // Create some initial channels
+       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
+
+       let commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
+       assert_eq!(commitment_tx[0].input.len(), 1);
+       assert_eq!(commitment_tx[0].input[0].previous_output.txid, chan_1.3.txid());
+
+       // Settle A's commitment tx on B's chain
+       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+       assert!(nodes[1].node.claim_funds(payment_preimage));
+       check_added_monitors!(nodes[1], 1);
+       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()] }, 1);
+       let events = nodes[1].node.get_and_clear_pending_msg_events();
+       match events[0] {
+               MessageSendEvent::UpdateHTLCs { .. } => {},
+               _ => panic!("Unexpected event"),
+       }
+       match events[1] {
+               MessageSendEvent::BroadcastChannelUpdate { .. } => {},
+               _ => panic!("Unexepected event"),
+       }
+
+       // Check B's monitor was able to send back output descriptor event for preimage tx on A's commitment tx
+       let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap(); // ChannelManager : 1 (local commitment tx), ChannelMonitor: 2 (1 preimage tx) * 2 (block-rescan)
+       check_spends!(node_txn[0], commitment_tx[0].clone());
+       assert_eq!(node_txn[0], node_txn[2]);
+       assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
+       check_spends!(node_txn[1], chan_1.3.clone());
+
+       let spend_txn = check_spendable_outputs!(nodes[1], 1); // , 0, 0, 1, 1);
+       assert_eq!(spend_txn.len(), 2);
+       assert_eq!(spend_txn[0], spend_txn[1]);
+       check_spends!(spend_txn[0], node_txn[0].clone());
+}
+
+#[test]
+fn test_static_spendable_outputs_justice_tx_revoked_commitment_tx() {
+       let nodes = create_network(2, &[None, None]);
+
+       // Create some initial channels
+       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
+       let revoked_local_txn = nodes[0].node.channel_state.lock().unwrap().by_id.iter().next().unwrap().1.last_local_commitment_txn.clone();
+       assert_eq!(revoked_local_txn[0].input.len(), 1);
+       assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_1.3.txid());
+
+       claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
+
+       let  header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
+       check_closed_broadcast!(nodes[1]);
+
+       let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
+       assert_eq!(node_txn.len(), 3);
+       assert_eq!(node_txn.pop().unwrap(), node_txn[0]);
+       assert_eq!(node_txn[0].input.len(), 2);
+       check_spends!(node_txn[0], revoked_local_txn[0].clone());
+
+       let spend_txn = check_spendable_outputs!(nodes[1], 1);
+       assert_eq!(spend_txn.len(), 2);
+       assert_eq!(spend_txn[0], spend_txn[1]);
+       check_spends!(spend_txn[0], node_txn[0].clone());
+}
+
+#[test]
+fn test_static_spendable_outputs_justice_tx_revoked_htlc_timeout_tx() {
+       let nodes = create_network(2, &[None, None]);
+
+       // Create some initial channels
+       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
+       let revoked_local_txn = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
+       assert_eq!(revoked_local_txn[0].input.len(), 1);
+       assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_1.3.txid());
+
+       claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
+
+       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+       // A will generate HTLC-Timeout from revoked commitment tx
+       nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
+       check_closed_broadcast!(nodes[0]);
+
+       let revoked_htlc_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
+       assert_eq!(revoked_htlc_txn.len(), 3);
+       assert_eq!(revoked_htlc_txn[0], revoked_htlc_txn[2]);
+       assert_eq!(revoked_htlc_txn[0].input.len(), 1);
+       assert_eq!(revoked_htlc_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
+       check_spends!(revoked_htlc_txn[0], revoked_local_txn[0].clone());
+       check_spends!(revoked_htlc_txn[1], chan_1.3.clone());
+
+       // B will generate justice tx from A's revoked commitment/HTLC tx
+       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone(), revoked_htlc_txn[0].clone()] }, 1);
+       check_closed_broadcast!(nodes[1]);
+
+       let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
+       assert_eq!(node_txn.len(), 4);
+       assert_eq!(node_txn[3].input.len(), 1);
+       check_spends!(node_txn[3], revoked_htlc_txn[0].clone());
+
+       // Check B's ChannelMonitor was able to generate the right spendable output descriptor
+       let spend_txn = check_spendable_outputs!(nodes[1], 1);
+       assert_eq!(spend_txn.len(), 3);
+       assert_eq!(spend_txn[0], spend_txn[1]);
+       check_spends!(spend_txn[0], node_txn[0].clone());
+       check_spends!(spend_txn[2], node_txn[3].clone());
+}
+
+#[test]
+fn test_static_spendable_outputs_justice_tx_revoked_htlc_success_tx() {
+       let nodes = create_network(2, &[None, None]);
+
+       // Create some initial channels
+       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
+       let revoked_local_txn = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
+       assert_eq!(revoked_local_txn[0].input.len(), 1);
+       assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_1.3.txid());
+
+       claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
+
+       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+       // B will generate HTLC-Success from revoked commitment tx
+       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
+       check_closed_broadcast!(nodes[1]);
+       let revoked_htlc_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
+
+       assert_eq!(revoked_htlc_txn.len(), 3);
+       assert_eq!(revoked_htlc_txn[0], revoked_htlc_txn[2]);
+       assert_eq!(revoked_htlc_txn[0].input.len(), 1);
+       assert_eq!(revoked_htlc_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
+       check_spends!(revoked_htlc_txn[0], revoked_local_txn[0].clone());
+
+       // A will generate justice tx from B's revoked commitment/HTLC tx
+       nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone(), revoked_htlc_txn[0].clone()] }, 1);
+       check_closed_broadcast!(nodes[0]);
+
+       let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
+       assert_eq!(node_txn.len(), 4);
+       assert_eq!(node_txn[3].input.len(), 1);
+       check_spends!(node_txn[3], revoked_htlc_txn[0].clone());
+
+       // Check A's ChannelMonitor was able to generate the right spendable output descriptor
+       let spend_txn = check_spendable_outputs!(nodes[0], 1);
+       assert_eq!(spend_txn.len(), 5);
+       assert_eq!(spend_txn[0], spend_txn[2]);
+       assert_eq!(spend_txn[1], spend_txn[3]);
+       check_spends!(spend_txn[0], revoked_local_txn[0].clone()); // spending to_remote output from revoked local tx
+       check_spends!(spend_txn[1], node_txn[2].clone()); // spending justice tx output from revoked local tx htlc received output
+       check_spends!(spend_txn[4], node_txn[3].clone()); // spending justice tx output on htlc success tx
+}
+
+#[test]
+fn test_onchain_to_onchain_claim() {
+       // Test that in case of channel closure, we detect the state of output thanks to
+       // ChainWatchInterface and claim HTLC on downstream peer's remote commitment tx.
+       // First, have C claim an HTLC against its own latest commitment transaction.
+       // Then, broadcast these to B, which should update the monitor downstream on the A<->B
+       // channel.
+       // Finally, check that B will claim the HTLC output if A's latest commitment transaction
+       // gets broadcast.
+
+       let nodes = create_network(3, &[None, None, None]);
+
+       // Create some initial channels
+       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+       let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
+
+       // Rebalance the network a bit by relaying one payment through all the channels ...
+       send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
+       send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
+
+       let (payment_preimage, _payment_hash) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), 3000000);
+       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42};
+       let commitment_tx = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone();
+       check_spends!(commitment_tx[0], chan_2.3.clone());
+       nodes[2].node.claim_funds(payment_preimage);
+       check_added_monitors!(nodes[2], 1);
+       let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
+       assert!(updates.update_add_htlcs.is_empty());
+       assert!(updates.update_fail_htlcs.is_empty());
+       assert_eq!(updates.update_fulfill_htlcs.len(), 1);
+       assert!(updates.update_fail_malformed_htlcs.is_empty());
+
+       nodes[2].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 1);
+       check_closed_broadcast!(nodes[2]);
+
+       let c_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 2 (commitment tx, HTLC-Success tx), ChannelMonitor : 1 (HTLC-Success tx)
+       assert_eq!(c_txn.len(), 3);
+       assert_eq!(c_txn[0], c_txn[2]);
+       assert_eq!(commitment_tx[0], c_txn[1]);
+       check_spends!(c_txn[1], chan_2.3.clone());
+       check_spends!(c_txn[2], c_txn[1].clone());
+       assert_eq!(c_txn[1].input[0].witness.clone().last().unwrap().len(), 71);
+       assert_eq!(c_txn[2].input[0].witness.clone().last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
+       assert!(c_txn[0].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
+       assert_eq!(c_txn[0].lock_time, 0); // Success tx
+
+       // So we broadcast C's commitment tx and HTLC-Success on B's chain, we should successfully be able to extract preimage and update downstream monitor
+       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![c_txn[1].clone(), c_txn[2].clone()]}, 1);
+       {
+               let mut b_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
+               assert_eq!(b_txn.len(), 4);
+               assert_eq!(b_txn[0], b_txn[3]);
+               check_spends!(b_txn[1], chan_2.3); // B local commitment tx, issued by ChannelManager
+               check_spends!(b_txn[2], b_txn[1].clone()); // HTLC-Timeout on B local commitment tx, issued by ChannelManager
+               assert_eq!(b_txn[2].input[0].witness.clone().last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
+               assert!(b_txn[2].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
+               assert_ne!(b_txn[2].lock_time, 0); // Timeout tx
+               check_spends!(b_txn[0], c_txn[1].clone()); // timeout tx on C remote commitment tx, issued by ChannelMonitor, * 2 due to block rescan
+               assert_eq!(b_txn[0].input[0].witness.clone().last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
+               assert!(b_txn[0].output[0].script_pubkey.is_v0_p2wpkh()); // direct payment
+               assert_ne!(b_txn[2].lock_time, 0); // Timeout tx
+               b_txn.clear();
+       }
+       let msg_events = nodes[1].node.get_and_clear_pending_msg_events();
+       check_added_monitors!(nodes[1], 1);
+       match msg_events[0] {
+               MessageSendEvent::BroadcastChannelUpdate {  .. } => {},
+               _ => panic!("Unexpected event"),
+       }
+       match msg_events[1] {
+               MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, .. } } => {
+                       assert!(update_add_htlcs.is_empty());
+                       assert!(update_fail_htlcs.is_empty());
+                       assert_eq!(update_fulfill_htlcs.len(), 1);
+                       assert!(update_fail_malformed_htlcs.is_empty());
+                       assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
+               },
+               _ => panic!("Unexpected event"),
+       };
+       // Broadcast A's commitment tx on B's chain to see if we are able to claim inbound HTLC with our HTLC-Success tx
+       let commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
+       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 1);
+       let b_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
+       assert_eq!(b_txn.len(), 3);
+       check_spends!(b_txn[1], chan_1.3); // Local commitment tx, issued by ChannelManager
+       assert_eq!(b_txn[0], b_txn[2]); // HTLC-Success tx, issued by ChannelMonitor, * 2 due to block rescan
+       check_spends!(b_txn[0], commitment_tx[0].clone());
+       assert_eq!(b_txn[0].input[0].witness.clone().last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
+       assert!(b_txn[0].output[0].script_pubkey.is_v0_p2wpkh()); // direct payment
+       assert_eq!(b_txn[2].lock_time, 0); // Success tx
+
+       check_closed_broadcast!(nodes[1]);
+}
+
+#[test]
+fn test_duplicate_payment_hash_one_failure_one_success() {
+       // Topology : A --> B --> C
+       // We route 2 payments with same hash between B and C, one will be timeout, the other successfully claim
+       let mut nodes = create_network(3, &[None, None, None]);
+
+       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+       let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
+
+       let (our_payment_preimage, duplicate_payment_hash) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 900000);
+       *nodes[0].network_payment_count.borrow_mut() -= 1;
+       assert_eq!(route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 900000).1, duplicate_payment_hash);
+
+       let commitment_txn = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone();
+       assert_eq!(commitment_txn[0].input.len(), 1);
+       check_spends!(commitment_txn[0], chan_2.3.clone());
+
+       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_txn[0].clone()] }, 1);
+       check_closed_broadcast!(nodes[1]);
+
+       let htlc_timeout_tx;
+       { // Extract one of the two HTLC-Timeout transaction
+               let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
+               assert_eq!(node_txn.len(), 7);
+               assert_eq!(node_txn[0], node_txn[5]);
+               assert_eq!(node_txn[1], node_txn[6]);
+               check_spends!(node_txn[0], commitment_txn[0].clone());
+               assert_eq!(node_txn[0].input.len(), 1);
+               check_spends!(node_txn[1], commitment_txn[0].clone());
+               assert_eq!(node_txn[1].input.len(), 1);
+               assert_ne!(node_txn[0].input[0], node_txn[1].input[0]);
+               check_spends!(node_txn[2], chan_2.3.clone());
+               check_spends!(node_txn[3], node_txn[2].clone());
+               check_spends!(node_txn[4], node_txn[2].clone());
+               htlc_timeout_tx = node_txn[1].clone();
+       }
+
+       nodes[2].node.claim_funds(our_payment_preimage);
+       nodes[2].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_txn[0].clone()] }, 1);
+       check_added_monitors!(nodes[2], 2);
+       let events = nodes[2].node.get_and_clear_pending_msg_events();
+       match events[0] {
+               MessageSendEvent::UpdateHTLCs { .. } => {},
+               _ => panic!("Unexpected event"),
+       }
+       match events[1] {
+               MessageSendEvent::BroadcastChannelUpdate { .. } => {},
+               _ => panic!("Unexepected event"),
+       }
+       let htlc_success_txn: Vec<_> = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
+       assert_eq!(htlc_success_txn.len(), 5);
+       check_spends!(htlc_success_txn[2], chan_2.3.clone());
+       assert_eq!(htlc_success_txn[0], htlc_success_txn[3]);
+       assert_eq!(htlc_success_txn[0].input.len(), 1);
+       assert_eq!(htlc_success_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
+       assert_eq!(htlc_success_txn[1], htlc_success_txn[4]);
+       assert_eq!(htlc_success_txn[1].input.len(), 1);
+       assert_eq!(htlc_success_txn[1].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
+       assert_ne!(htlc_success_txn[0].input[0], htlc_success_txn[1].input[0]);
+       check_spends!(htlc_success_txn[0], commitment_txn[0].clone());
+       check_spends!(htlc_success_txn[1], commitment_txn[0].clone());
+
+       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![htlc_timeout_tx] }, 200);
+       connect_blocks(&nodes[1].chain_monitor, ANTI_REORG_DELAY - 1, 200, true, header.bitcoin_hash());
+       expect_pending_htlcs_forwardable!(nodes[1]);
+       let htlc_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+       assert!(htlc_updates.update_add_htlcs.is_empty());
+       assert_eq!(htlc_updates.update_fail_htlcs.len(), 1);
+       assert_eq!(htlc_updates.update_fail_htlcs[0].htlc_id, 1);
+       assert!(htlc_updates.update_fulfill_htlcs.is_empty());
+       assert!(htlc_updates.update_fail_malformed_htlcs.is_empty());
+       check_added_monitors!(nodes[1], 1);
+
+       nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &htlc_updates.update_fail_htlcs[0]).unwrap();
+       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
+       {
+               commitment_signed_dance!(nodes[0], nodes[1], &htlc_updates.commitment_signed, false, true);
+               let events = nodes[0].node.get_and_clear_pending_msg_events();
+               assert_eq!(events.len(), 1);
+               match events[0] {
+                       MessageSendEvent::PaymentFailureNetworkUpdate { update: msgs::HTLCFailChannelUpdate::ChannelClosed { .. }  } => {
+                       },
+                       _ => { panic!("Unexpected event"); }
+               }
+       }
+       let events = nodes[0].node.get_and_clear_pending_events();
+       match events[0] {
+               Event::PaymentFailed { ref payment_hash, .. } => {
+                       assert_eq!(*payment_hash, duplicate_payment_hash);
+               }
+               _ => panic!("Unexpected event"),
+       }
+
+       // Solve 2nd HTLC by broadcasting on B's chain HTLC-Success Tx from C
+       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![htlc_success_txn[0].clone()] }, 200);
+       let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+       assert!(updates.update_add_htlcs.is_empty());
+       assert!(updates.update_fail_htlcs.is_empty());
+       assert_eq!(updates.update_fulfill_htlcs.len(), 1);
+       assert_eq!(updates.update_fulfill_htlcs[0].htlc_id, 0);
+       assert!(updates.update_fail_malformed_htlcs.is_empty());
+       check_added_monitors!(nodes[1], 1);
+
+       nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]).unwrap();
+       commitment_signed_dance!(nodes[0], nodes[1], &updates.commitment_signed, false);
+
+       let events = nodes[0].node.get_and_clear_pending_events();
+       match events[0] {
+               Event::PaymentSent { ref payment_preimage } => {
+                       assert_eq!(*payment_preimage, our_payment_preimage);
+               }
+               _ => panic!("Unexpected event"),
+       }
+}
+
+#[test]
+fn test_dynamic_spendable_outputs_local_htlc_success_tx() {
+       let nodes = create_network(2, &[None, None]);
+
+       // Create some initial channels
+       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 9000000).0;
+       let local_txn = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
+       assert_eq!(local_txn[0].input.len(), 1);
+       check_spends!(local_txn[0], chan_1.3.clone());
+
+       // Give B knowledge of preimage to be able to generate a local HTLC-Success Tx
+       nodes[1].node.claim_funds(payment_preimage);
+       check_added_monitors!(nodes[1], 1);
+       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![local_txn[0].clone()] }, 1);
+       let events = nodes[1].node.get_and_clear_pending_msg_events();
+       match events[0] {
+               MessageSendEvent::UpdateHTLCs { .. } => {},
+               _ => panic!("Unexpected event"),
+       }
+       match events[1] {
+               MessageSendEvent::BroadcastChannelUpdate { .. } => {},
+               _ => panic!("Unexepected event"),
+       }
+       let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
+       assert_eq!(node_txn[0].input.len(), 1);
+       assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
+       check_spends!(node_txn[0], local_txn[0].clone());
+
+       // Verify that B is able to spend its own HTLC-Success tx thanks to spendable output event given back by its ChannelMonitor
+       let spend_txn = check_spendable_outputs!(nodes[1], 1);
+       assert_eq!(spend_txn.len(), 2);
+       check_spends!(spend_txn[0], node_txn[0].clone());
+       check_spends!(spend_txn[1], node_txn[2].clone());
+}
+
+fn do_test_fail_backwards_unrevoked_remote_announce(deliver_last_raa: bool, announce_latest: bool) {
+       // Test that we fail backwards the full set of HTLCs we need to when remote broadcasts an
+       // unrevoked commitment transaction.
+       // This includes HTLCs which were below the dust threshold as well as HTLCs which were awaiting
+       // a remote RAA before they could be failed backwards (and combinations thereof).
+       // We also test duplicate-hash HTLCs by adding two nodes on each side of the target nodes which
+       // use the same payment hashes.
+       // Thus, we use a six-node network:
+       //
+       // A \         / E
+       //    - C - D -
+       // B /         \ F
+       // And test where C fails back to A/B when D announces its latest commitment transaction
+       let nodes = create_network(6, &[None, None, None, None, None, None]);
+
+       create_announced_chan_between_nodes(&nodes, 0, 2, LocalFeatures::new(), LocalFeatures::new());
+       create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
+       let chan = create_announced_chan_between_nodes(&nodes, 2, 3, LocalFeatures::new(), LocalFeatures::new());
+       create_announced_chan_between_nodes(&nodes, 3, 4, LocalFeatures::new(), LocalFeatures::new());
+       create_announced_chan_between_nodes(&nodes, 3, 5, LocalFeatures::new(), LocalFeatures::new());
+
+       // Rebalance and check output sanity...
+       send_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 500000);
+       send_payment(&nodes[1], &[&nodes[2], &nodes[3], &nodes[5]], 500000);
+       assert_eq!(nodes[3].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn[0].output.len(), 2);
+
+       let ds_dust_limit = nodes[3].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().our_dust_limit_satoshis;
+       // 0th HTLC:
+       let (_, payment_hash_1) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], ds_dust_limit*1000); // not added < dust limit + HTLC tx fee
+       // 1st HTLC:
+       let (_, payment_hash_2) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], ds_dust_limit*1000); // not added < dust limit + HTLC tx fee
+       let route = nodes[1].router.get_route(&nodes[5].node.get_our_node_id(), None, &Vec::new(), ds_dust_limit*1000, TEST_FINAL_CLTV).unwrap();
+       // 2nd HTLC:
+       send_along_route_with_hash(&nodes[1], route.clone(), &[&nodes[2], &nodes[3], &nodes[5]], ds_dust_limit*1000, payment_hash_1); // not added < dust limit + HTLC tx fee
+       // 3rd HTLC:
+       send_along_route_with_hash(&nodes[1], route, &[&nodes[2], &nodes[3], &nodes[5]], ds_dust_limit*1000, payment_hash_2); // not added < dust limit + HTLC tx fee
+       // 4th HTLC:
+       let (_, payment_hash_3) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 1000000);
+       // 5th HTLC:
+       let (_, payment_hash_4) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 1000000);
+       let route = nodes[1].router.get_route(&nodes[5].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
+       // 6th HTLC:
+       send_along_route_with_hash(&nodes[1], route.clone(), &[&nodes[2], &nodes[3], &nodes[5]], 1000000, payment_hash_3);
+       // 7th HTLC:
+       send_along_route_with_hash(&nodes[1], route, &[&nodes[2], &nodes[3], &nodes[5]], 1000000, payment_hash_4);
+
+       // 8th HTLC:
+       let (_, payment_hash_5) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 1000000);
+       // 9th HTLC:
+       let route = nodes[1].router.get_route(&nodes[5].node.get_our_node_id(), None, &Vec::new(), ds_dust_limit*1000, TEST_FINAL_CLTV).unwrap();
+       send_along_route_with_hash(&nodes[1], route, &[&nodes[2], &nodes[3], &nodes[5]], ds_dust_limit*1000, payment_hash_5); // not added < dust limit + HTLC tx fee
+
+       // 10th HTLC:
+       let (_, payment_hash_6) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], ds_dust_limit*1000); // not added < dust limit + HTLC tx fee
+       // 11th HTLC:
+       let route = nodes[1].router.get_route(&nodes[5].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
+       send_along_route_with_hash(&nodes[1], route, &[&nodes[2], &nodes[3], &nodes[5]], 1000000, payment_hash_6);
+
+       // Double-check that six of the new HTLC were added
+       // We now have six HTLCs pending over the dust limit and six HTLCs under the dust limit (ie,
+       // with to_local and to_remote outputs, 8 outputs and 6 HTLCs not included).
+       assert_eq!(nodes[3].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.len(), 1);
+       assert_eq!(nodes[3].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn[0].output.len(), 8);
+
+       // Now fail back three of the over-dust-limit and three of the under-dust-limit payments in one go.
+       // Fail 0th below-dust, 4th above-dust, 8th above-dust, 10th below-dust HTLCs
+       assert!(nodes[4].node.fail_htlc_backwards(&payment_hash_1));
+       assert!(nodes[4].node.fail_htlc_backwards(&payment_hash_3));
+       assert!(nodes[4].node.fail_htlc_backwards(&payment_hash_5));
+       assert!(nodes[4].node.fail_htlc_backwards(&payment_hash_6));
+       check_added_monitors!(nodes[4], 0);
+       expect_pending_htlcs_forwardable!(nodes[4]);
+       check_added_monitors!(nodes[4], 1);
+
+       let four_removes = get_htlc_update_msgs!(nodes[4], nodes[3].node.get_our_node_id());
+       nodes[3].node.handle_update_fail_htlc(&nodes[4].node.get_our_node_id(), &four_removes.update_fail_htlcs[0]).unwrap();
+       nodes[3].node.handle_update_fail_htlc(&nodes[4].node.get_our_node_id(), &four_removes.update_fail_htlcs[1]).unwrap();
+       nodes[3].node.handle_update_fail_htlc(&nodes[4].node.get_our_node_id(), &four_removes.update_fail_htlcs[2]).unwrap();
+       nodes[3].node.handle_update_fail_htlc(&nodes[4].node.get_our_node_id(), &four_removes.update_fail_htlcs[3]).unwrap();
+       commitment_signed_dance!(nodes[3], nodes[4], four_removes.commitment_signed, false);
+
+       // Fail 3rd below-dust and 7th above-dust HTLCs
+       assert!(nodes[5].node.fail_htlc_backwards(&payment_hash_2));
+       assert!(nodes[5].node.fail_htlc_backwards(&payment_hash_4));
+       check_added_monitors!(nodes[5], 0);
+       expect_pending_htlcs_forwardable!(nodes[5]);
+       check_added_monitors!(nodes[5], 1);
+
+       let two_removes = get_htlc_update_msgs!(nodes[5], nodes[3].node.get_our_node_id());
+       nodes[3].node.handle_update_fail_htlc(&nodes[5].node.get_our_node_id(), &two_removes.update_fail_htlcs[0]).unwrap();
+       nodes[3].node.handle_update_fail_htlc(&nodes[5].node.get_our_node_id(), &two_removes.update_fail_htlcs[1]).unwrap();
+       commitment_signed_dance!(nodes[3], nodes[5], two_removes.commitment_signed, false);
+
+       let ds_prev_commitment_tx = nodes[3].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone();
+
+       expect_pending_htlcs_forwardable!(nodes[3]);
+       check_added_monitors!(nodes[3], 1);
+       let six_removes = get_htlc_update_msgs!(nodes[3], nodes[2].node.get_our_node_id());
+       nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[0]).unwrap();
+       nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[1]).unwrap();
+       nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[2]).unwrap();
+       nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[3]).unwrap();
+       nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[4]).unwrap();
+       nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[5]).unwrap();
+       if deliver_last_raa {
+               commitment_signed_dance!(nodes[2], nodes[3], six_removes.commitment_signed, false);
+       } else {
+               let _cs_last_raa = commitment_signed_dance!(nodes[2], nodes[3], six_removes.commitment_signed, false, true, false, true);
+       }
+
+       // D's latest commitment transaction now contains 1st + 2nd + 9th HTLCs (implicitly, they're
+       // below the dust limit) and the 5th + 6th + 11th HTLCs. It has failed back the 0th, 3rd, 4th,
+       // 7th, 8th, and 10th, but as we haven't yet delivered the final RAA to C, the fails haven't
+       // propagated back to A/B yet (and D has two unrevoked commitment transactions).
+       //
+       // We now broadcast the latest commitment transaction, which *should* result in failures for
+       // the 0th, 1st, 2nd, 3rd, 4th, 7th, 8th, 9th, and 10th HTLCs, ie all the below-dust HTLCs and
+       // the non-broadcast above-dust HTLCs.
+       //
+       // Alternatively, we may broadcast the previous commitment transaction, which should only
+       // result in failures for the below-dust HTLCs, ie the 0th, 1st, 2nd, 3rd, 9th, and 10th HTLCs.
+       let ds_last_commitment_tx = nodes[3].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone();
+
+       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+       if announce_latest {
+               nodes[2].chain_monitor.block_connected_checked(&header, 1, &[&ds_last_commitment_tx[0]], &[1; 1]);
+       } else {
+               nodes[2].chain_monitor.block_connected_checked(&header, 1, &[&ds_prev_commitment_tx[0]], &[1; 1]);
+       }
+       connect_blocks(&nodes[2].chain_monitor, ANTI_REORG_DELAY - 1, 1, true,  header.bitcoin_hash());
+       check_closed_broadcast!(nodes[2]);
+       expect_pending_htlcs_forwardable!(nodes[2]);
+       check_added_monitors!(nodes[2], 2);
+
+       let cs_msgs = nodes[2].node.get_and_clear_pending_msg_events();
+       assert_eq!(cs_msgs.len(), 2);
+       let mut a_done = false;
+       for msg in cs_msgs {
+               match msg {
+                       MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
+                               // Both under-dust HTLCs and the one above-dust HTLC that we had already failed
+                               // should be failed-backwards here.
+                               let target = if *node_id == nodes[0].node.get_our_node_id() {
+                                       // If announce_latest, expect 0th, 1st, 4th, 8th, 10th HTLCs, else only 0th, 1st, 10th below-dust HTLCs
+                                       for htlc in &updates.update_fail_htlcs {
+                                               assert!(htlc.htlc_id == 1 || htlc.htlc_id == 2 || htlc.htlc_id == 6 || if announce_latest { htlc.htlc_id == 3 || htlc.htlc_id == 5 } else { false });
+                                       }
+                                       assert_eq!(updates.update_fail_htlcs.len(), if announce_latest { 5 } else { 3 });
+                                       assert!(!a_done);
+                                       a_done = true;
+                                       &nodes[0]
+                               } else {
+                                       // If announce_latest, expect 2nd, 3rd, 7th, 9th HTLCs, else only 2nd, 3rd, 9th below-dust HTLCs
+                                       for htlc in &updates.update_fail_htlcs {
+                                               assert!(htlc.htlc_id == 1 || htlc.htlc_id == 2 || htlc.htlc_id == 5 || if announce_latest { htlc.htlc_id == 4 } else { false });
+                                       }
+                                       assert_eq!(*node_id, nodes[1].node.get_our_node_id());
+                                       assert_eq!(updates.update_fail_htlcs.len(), if announce_latest { 4 } else { 3 });
+                                       &nodes[1]
+                               };
+                               target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]).unwrap();
+                               target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[1]).unwrap();
+                               target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[2]).unwrap();
+                               if announce_latest {
+                                       target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[3]).unwrap();
+                                       if *node_id == nodes[0].node.get_our_node_id() {
+                                               target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[4]).unwrap();
+                                       }
+                               }
+                               commitment_signed_dance!(target, nodes[2], updates.commitment_signed, false, true);
+                       },
+                       _ => panic!("Unexpected event"),
+               }
+       }
+
+       let as_events = nodes[0].node.get_and_clear_pending_events();
+       assert_eq!(as_events.len(), if announce_latest { 5 } else { 3 });
+       let mut as_failds = HashSet::new();
+       for event in as_events.iter() {
+               if let &Event::PaymentFailed { ref payment_hash, ref rejected_by_dest, .. } = event {
+                       assert!(as_failds.insert(*payment_hash));
+                       if *payment_hash != payment_hash_2 {
+                               assert_eq!(*rejected_by_dest, deliver_last_raa);
+                       } else {
+                               assert!(!rejected_by_dest);
+                       }
+               } else { panic!("Unexpected event"); }
+       }
+       assert!(as_failds.contains(&payment_hash_1));
+       assert!(as_failds.contains(&payment_hash_2));
+       if announce_latest {
+               assert!(as_failds.contains(&payment_hash_3));
+               assert!(as_failds.contains(&payment_hash_5));
+       }
+       assert!(as_failds.contains(&payment_hash_6));
+
+       let bs_events = nodes[1].node.get_and_clear_pending_events();
+       assert_eq!(bs_events.len(), if announce_latest { 4 } else { 3 });
+       let mut bs_failds = HashSet::new();
+       for event in bs_events.iter() {
+               if let &Event::PaymentFailed { ref payment_hash, ref rejected_by_dest, .. } = event {
+                       assert!(bs_failds.insert(*payment_hash));
+                       if *payment_hash != payment_hash_1 && *payment_hash != payment_hash_5 {
+                               assert_eq!(*rejected_by_dest, deliver_last_raa);
+                       } else {
+                               assert!(!rejected_by_dest);
+                       }
+               } else { panic!("Unexpected event"); }
+       }
+       assert!(bs_failds.contains(&payment_hash_1));
+       assert!(bs_failds.contains(&payment_hash_2));
+       if announce_latest {
+               assert!(bs_failds.contains(&payment_hash_4));
+       }
+       assert!(bs_failds.contains(&payment_hash_5));
+
+       // For each HTLC which was not failed-back by normal process (ie deliver_last_raa), we should
+       // get a PaymentFailureNetworkUpdate. A should have gotten 4 HTLCs which were failed-back due
+       // to unknown-preimage-etc, B should have gotten 2. Thus, in the
+       // announce_latest && deliver_last_raa case, we should have 5-4=1 and 4-2=2
+       // PaymentFailureNetworkUpdates.
+       let as_msg_events = nodes[0].node.get_and_clear_pending_msg_events();
+       assert_eq!(as_msg_events.len(), if deliver_last_raa { 1 } else if !announce_latest { 3 } else { 5 });
+       let bs_msg_events = nodes[1].node.get_and_clear_pending_msg_events();
+       assert_eq!(bs_msg_events.len(), if deliver_last_raa { 2 } else if !announce_latest { 3 } else { 4 });
+       for event in as_msg_events.iter().chain(bs_msg_events.iter()) {
+               match event {
+                       &MessageSendEvent::PaymentFailureNetworkUpdate { .. } => {},
+                       _ => panic!("Unexpected event"),
+               }
+       }
+}
+
+#[test]
+fn test_fail_backwards_latest_remote_announce_a() {
+       do_test_fail_backwards_unrevoked_remote_announce(false, true);
+}
+
+#[test]
+fn test_fail_backwards_latest_remote_announce_b() {
+       do_test_fail_backwards_unrevoked_remote_announce(true, true);
+}
+
+#[test]
+fn test_fail_backwards_previous_remote_announce() {
+       do_test_fail_backwards_unrevoked_remote_announce(false, false);
+       // Note that true, true doesn't make sense as it implies we announce a revoked state, which is
+       // tested for in test_commitment_revoked_fail_backward_exhaustive()
+}
+
+#[test]
+fn test_dynamic_spendable_outputs_local_htlc_timeout_tx() {
+       let nodes = create_network(2, &[None, None]);
+
+       // Create some initial channels
+       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       route_payment(&nodes[0], &vec!(&nodes[1])[..], 9000000).0;
+       let local_txn = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
+       assert_eq!(local_txn[0].input.len(), 1);
+       check_spends!(local_txn[0], chan_1.3.clone());
+
+       // Timeout HTLC on A's chain and so it can generate a HTLC-Timeout tx
+       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+       nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![local_txn[0].clone()] }, 200);
+       check_closed_broadcast!(nodes[0]);
+
+       let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
+       assert_eq!(node_txn[0].input.len(), 1);
+       assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
+       check_spends!(node_txn[0], local_txn[0].clone());
+
+       // Verify that A is able to spend its own HTLC-Timeout tx thanks to spendable output event given back by its ChannelMonitor
+       let spend_txn = check_spendable_outputs!(nodes[0], 1);
+       assert_eq!(spend_txn.len(), 8);
+       assert_eq!(spend_txn[0], spend_txn[2]);
+       assert_eq!(spend_txn[0], spend_txn[4]);
+       assert_eq!(spend_txn[0], spend_txn[6]);
+       assert_eq!(spend_txn[1], spend_txn[3]);
+       assert_eq!(spend_txn[1], spend_txn[5]);
+       assert_eq!(spend_txn[1], spend_txn[7]);
+       check_spends!(spend_txn[0], local_txn[0].clone());
+       check_spends!(spend_txn[1], node_txn[0].clone());
+}
+
+#[test]
+fn test_static_output_closing_tx() {
+       let nodes = create_network(2, &[None, None]);
+
+       let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
+       let closing_tx = close_channel(&nodes[0], &nodes[1], &chan.2, chan.3, true).2;
+
+       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+       nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![closing_tx.clone()] }, 1);
+       let spend_txn = check_spendable_outputs!(nodes[0], 2);
+       assert_eq!(spend_txn.len(), 1);
+       check_spends!(spend_txn[0], closing_tx.clone());
+
+       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![closing_tx.clone()] }, 1);
+       let spend_txn = check_spendable_outputs!(nodes[1], 2);
+       assert_eq!(spend_txn.len(), 1);
+       check_spends!(spend_txn[0], closing_tx);
+}
+
+fn do_htlc_claim_local_commitment_only(use_dust: bool) {
+       let nodes = create_network(2, &[None, None]);
+       let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       let (our_payment_preimage, _) = route_payment(&nodes[0], &[&nodes[1]], if use_dust { 50000 } else { 3000000 });
+
+       // Claim the payment, but don't deliver A's commitment_signed, resulting in the HTLC only being
+       // present in B's local commitment transaction, but none of A's commitment transactions.
+       assert!(nodes[1].node.claim_funds(our_payment_preimage));
+       check_added_monitors!(nodes[1], 1);
+
+       let bs_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+       nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fulfill_htlcs[0]).unwrap();
+       let events = nodes[0].node.get_and_clear_pending_events();
+       assert_eq!(events.len(), 1);
+       match events[0] {
+               Event::PaymentSent { payment_preimage } => {
+                       assert_eq!(payment_preimage, our_payment_preimage);
+               },
+               _ => panic!("Unexpected event"),
+       }
+
+       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_updates.commitment_signed).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       let as_updates = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
+       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_updates.0).unwrap();
+       check_added_monitors!(nodes[1], 1);
+
+       let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+       for i in 1..TEST_FINAL_CLTV - CLTV_CLAIM_BUFFER + CHAN_CONFIRM_DEPTH + 1 {
+               nodes[1].chain_monitor.block_connected_checked(&header, i, &Vec::new(), &Vec::new());
+               header.prev_blockhash = header.bitcoin_hash();
+       }
+       test_txn_broadcast(&nodes[1], &chan, None, if use_dust { HTLCType::NONE } else { HTLCType::SUCCESS });
+       check_closed_broadcast!(nodes[1]);
+}
+
+fn do_htlc_claim_current_remote_commitment_only(use_dust: bool) {
+       let mut nodes = create_network(2, &[None, None]);
+       let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), if use_dust { 50000 } else { 3000000 }, TEST_FINAL_CLTV).unwrap();
+       let (_, payment_hash) = get_payment_preimage_hash!(nodes[0]);
+       nodes[0].node.send_payment(route, payment_hash).unwrap();
+       check_added_monitors!(nodes[0], 1);
+
+       let _as_update = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
+
+       // As far as A is concerned, the HTLC is now present only in the latest remote commitment
+       // transaction, however it is not in A's latest local commitment, so we can just broadcast that
+       // to "time out" the HTLC.
+
+       let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+       for i in 1..TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + CHAN_CONFIRM_DEPTH + 1 {
+               nodes[0].chain_monitor.block_connected_checked(&header, i, &Vec::new(), &Vec::new());
+               header.prev_blockhash = header.bitcoin_hash();
+       }
+       test_txn_broadcast(&nodes[0], &chan, None, HTLCType::NONE);
+       check_closed_broadcast!(nodes[0]);
+}
+
+fn do_htlc_claim_previous_remote_commitment_only(use_dust: bool, check_revoke_no_close: bool) {
+       let nodes = create_network(3, &[None, None, None]);
+       let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       // Fail the payment, but don't deliver A's final RAA, resulting in the HTLC only being present
+       // in B's previous (unrevoked) commitment transaction, but none of A's commitment transactions.
+       // Also optionally test that we *don't* fail the channel in case the commitment transaction was
+       // actually revoked.
+       let htlc_value = if use_dust { 50000 } else { 3000000 };
+       let (_, our_payment_hash) = route_payment(&nodes[0], &[&nodes[1]], htlc_value);
+       assert!(nodes[1].node.fail_htlc_backwards(&our_payment_hash));
+       expect_pending_htlcs_forwardable!(nodes[1]);
+       check_added_monitors!(nodes[1], 1);
+
+       let bs_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+       nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fail_htlcs[0]).unwrap();
+       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_updates.commitment_signed).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       let as_updates = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
+       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_updates.0).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_updates.1).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       let bs_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
+
+       if check_revoke_no_close {
+               nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
+               check_added_monitors!(nodes[0], 1);
+       }
+
+       let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+       for i in 1..TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + CHAN_CONFIRM_DEPTH + 1 {
+               nodes[0].chain_monitor.block_connected_checked(&header, i, &Vec::new(), &Vec::new());
+               header.prev_blockhash = header.bitcoin_hash();
+       }
+       if !check_revoke_no_close {
+               test_txn_broadcast(&nodes[0], &chan, None, HTLCType::NONE);
+               check_closed_broadcast!(nodes[0]);
+       } else {
+               let events = nodes[0].node.get_and_clear_pending_events();
+               assert_eq!(events.len(), 1);
+               match events[0] {
+                       Event::PaymentFailed { payment_hash, rejected_by_dest, .. } => {
+                               assert_eq!(payment_hash, our_payment_hash);
+                               assert!(rejected_by_dest);
+                       },
+                       _ => panic!("Unexpected event"),
+               }
+       }
+}
+
+// Test that we close channels on-chain when broadcastable HTLCs reach their timeout window.
+// There are only a few cases to test here:
+//  * its not really normative behavior, but we test that below-dust HTLCs "included" in
+//    broadcastable commitment transactions result in channel closure,
+//  * its included in an unrevoked-but-previous remote commitment transaction,
+//  * its included in the latest remote or local commitment transactions.
+// We test each of the three possible commitment transactions individually and use both dust and
+// non-dust HTLCs.
+// Note that we don't bother testing both outbound and inbound HTLC failures for each case, and we
+// assume they are handled the same across all six cases, as both outbound and inbound failures are
+// tested for at least one of the cases in other tests.
+#[test]
+fn htlc_claim_single_commitment_only_a() {
+       do_htlc_claim_local_commitment_only(true);
+       do_htlc_claim_local_commitment_only(false);
+
+       do_htlc_claim_current_remote_commitment_only(true);
+       do_htlc_claim_current_remote_commitment_only(false);
+}
+
+#[test]
+fn htlc_claim_single_commitment_only_b() {
+       do_htlc_claim_previous_remote_commitment_only(true, false);
+       do_htlc_claim_previous_remote_commitment_only(false, false);
+       do_htlc_claim_previous_remote_commitment_only(true, true);
+       do_htlc_claim_previous_remote_commitment_only(false, true);
+}
+
+fn run_onion_failure_test<F1,F2>(_name: &str, test_case: u8, nodes: &Vec<Node>, route: &Route, payment_hash: &PaymentHash, callback_msg: F1, callback_node: F2, expected_retryable: bool, expected_error_code: Option<u16>, expected_channel_update: Option<HTLCFailChannelUpdate>)
+       where F1: for <'a> FnMut(&'a mut msgs::UpdateAddHTLC),
+                               F2: FnMut(),
+{
+       run_onion_failure_test_with_fail_intercept(_name, test_case, nodes, route, payment_hash, callback_msg, |_|{}, callback_node, expected_retryable, expected_error_code, expected_channel_update);
+}
+
+// test_case
+// 0: node1 fails backward
+// 1: final node fails backward
+// 2: payment completed but the user rejects the payment
+// 3: final node fails backward (but tamper onion payloads from node0)
+// 100: trigger error in the intermediate node and tamper returning fail_htlc
+// 200: trigger error in the final node and tamper returning fail_htlc
+fn run_onion_failure_test_with_fail_intercept<F1,F2,F3>(_name: &str, test_case: u8, nodes: &Vec<Node>, route: &Route, payment_hash: &PaymentHash, mut callback_msg: F1, mut callback_fail: F2, mut callback_node: F3, expected_retryable: bool, expected_error_code: Option<u16>, expected_channel_update: Option<HTLCFailChannelUpdate>)
+       where F1: for <'a> FnMut(&'a mut msgs::UpdateAddHTLC),
+                               F2: for <'a> FnMut(&'a mut msgs::UpdateFailHTLC),
+                               F3: FnMut(),
+{
+
+       // reset block height
+       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+       for ix in 0..nodes.len() {
+               nodes[ix].chain_monitor.block_connected_checked(&header, 1, &Vec::new()[..], &[0; 0]);
+       }
+
+       macro_rules! expect_event {
+               ($node: expr, $event_type: path) => {{
+                       let events = $node.node.get_and_clear_pending_events();
+                       assert_eq!(events.len(), 1);
+                       match events[0] {
+                               $event_type { .. } => {},
+                               _ => panic!("Unexpected event"),
+                       }
+               }}
+       }
+
+       macro_rules! expect_htlc_forward {
+               ($node: expr) => {{
+                       expect_event!($node, Event::PendingHTLCsForwardable);
+                       $node.node.process_pending_htlc_forwards();
+               }}
+       }
+
+       // 0 ~~> 2 send payment
+       nodes[0].node.send_payment(route.clone(), payment_hash.clone()).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       let update_0 = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
+       // temper update_add (0 => 1)
+       let mut update_add_0 = update_0.update_add_htlcs[0].clone();
+       if test_case == 0 || test_case == 3 || test_case == 100 {
+               callback_msg(&mut update_add_0);
+               callback_node();
+       }
+       // 0 => 1 update_add & CS
+       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &update_add_0).unwrap();
+       commitment_signed_dance!(nodes[1], nodes[0], &update_0.commitment_signed, false, true);
+
+       let update_1_0 = match test_case {
+               0|100 => { // intermediate node failure; fail backward to 0
+                       let update_1_0 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+                       assert!(update_1_0.update_fail_htlcs.len()+update_1_0.update_fail_malformed_htlcs.len()==1 && (update_1_0.update_fail_htlcs.len()==1 || update_1_0.update_fail_malformed_htlcs.len()==1));
+                       update_1_0
+               },
+               1|2|3|200 => { // final node failure; forwarding to 2
+                       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+                       // forwarding on 1
+                       if test_case != 200 {
+                               callback_node();
+                       }
+                       expect_htlc_forward!(&nodes[1]);
+
+                       let update_1 = get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
+                       check_added_monitors!(&nodes[1], 1);
+                       assert_eq!(update_1.update_add_htlcs.len(), 1);
+                       // tamper update_add (1 => 2)
+                       let mut update_add_1 = update_1.update_add_htlcs[0].clone();
+                       if test_case != 3 && test_case != 200 {
+                               callback_msg(&mut update_add_1);
+                       }
+
+                       // 1 => 2
+                       nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &update_add_1).unwrap();
+                       commitment_signed_dance!(nodes[2], nodes[1], update_1.commitment_signed, false, true);
+
+                       if test_case == 2 || test_case == 200 {
+                               expect_htlc_forward!(&nodes[2]);
+                               expect_event!(&nodes[2], Event::PaymentReceived);
+                               callback_node();
+                               expect_pending_htlcs_forwardable!(nodes[2]);
+                       }
+
+                       let update_2_1 = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
+                       if test_case == 2 || test_case == 200 {
+                               check_added_monitors!(&nodes[2], 1);
+                       }
+                       assert!(update_2_1.update_fail_htlcs.len() == 1);
+
+                       let mut fail_msg = update_2_1.update_fail_htlcs[0].clone();
+                       if test_case == 200 {
+                               callback_fail(&mut fail_msg);
+                       }
+
+                       // 2 => 1
+                       nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &fail_msg).unwrap();
+                       commitment_signed_dance!(nodes[1], nodes[2], update_2_1.commitment_signed, true);
+
+                       // backward fail on 1
+                       let update_1_0 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+                       assert!(update_1_0.update_fail_htlcs.len() == 1);
+                       update_1_0
+               },
+               _ => unreachable!(),
+       };
+
+       // 1 => 0 commitment_signed_dance
+       if update_1_0.update_fail_htlcs.len() > 0 {
+               let mut fail_msg = update_1_0.update_fail_htlcs[0].clone();
+               if test_case == 100 {
+                       callback_fail(&mut fail_msg);
+               }
+               nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_msg).unwrap();
+       } else {
+               nodes[0].node.handle_update_fail_malformed_htlc(&nodes[1].node.get_our_node_id(), &update_1_0.update_fail_malformed_htlcs[0]).unwrap();
+       };
+
+       commitment_signed_dance!(nodes[0], nodes[1], update_1_0.commitment_signed, false, true);
+
+       let events = nodes[0].node.get_and_clear_pending_events();
+       assert_eq!(events.len(), 1);
+       if let &Event::PaymentFailed { payment_hash:_, ref rejected_by_dest, ref error_code } = &events[0] {
+               assert_eq!(*rejected_by_dest, !expected_retryable);
+               assert_eq!(*error_code, expected_error_code);
+       } else {
+               panic!("Uexpected event");
+       }
+
+       let events = nodes[0].node.get_and_clear_pending_msg_events();
+       if expected_channel_update.is_some() {
+               assert_eq!(events.len(), 1);
+               match events[0] {
+                       MessageSendEvent::PaymentFailureNetworkUpdate { ref update } => {
+                               match update {
+                                       &HTLCFailChannelUpdate::ChannelUpdateMessage { .. } => {
+                                               if let HTLCFailChannelUpdate::ChannelUpdateMessage { .. } = expected_channel_update.unwrap() {} else {
+                                                       panic!("channel_update not found!");
+                                               }
+                                       },
+                                       &HTLCFailChannelUpdate::ChannelClosed { ref short_channel_id, ref is_permanent } => {
+                                               if let HTLCFailChannelUpdate::ChannelClosed { short_channel_id: ref expected_short_channel_id, is_permanent: ref expected_is_permanent } = expected_channel_update.unwrap() {
+                                                       assert!(*short_channel_id == *expected_short_channel_id);
+                                                       assert!(*is_permanent == *expected_is_permanent);
+                                               } else {
+                                                       panic!("Unexpected message event");
+                                               }
+                                       },
+                                       &HTLCFailChannelUpdate::NodeFailure { ref node_id, ref is_permanent } => {
+                                               if let HTLCFailChannelUpdate::NodeFailure { node_id: ref expected_node_id, is_permanent: ref expected_is_permanent } = expected_channel_update.unwrap() {
+                                                       assert!(*node_id == *expected_node_id);
+                                                       assert!(*is_permanent == *expected_is_permanent);
+                                               } else {
+                                                       panic!("Unexpected message event");
+                                               }
+                                       },
+                               }
+                       },
+                       _ => panic!("Unexpected message event"),
+               }
+       } else {
+               assert_eq!(events.len(), 0);
+       }
+}
+
+impl msgs::ChannelUpdate {
+       fn dummy() -> msgs::ChannelUpdate {
+               use secp256k1::ffi::Signature as FFISignature;
+               use secp256k1::Signature;
+               msgs::ChannelUpdate {
+                       signature: Signature::from(FFISignature::new()),
+                       contents: msgs::UnsignedChannelUpdate {
+                               chain_hash: Sha256dHash::hash(&vec![0u8][..]),
+                               short_channel_id: 0,
+                               timestamp: 0,
+                               flags: 0,
+                               cltv_expiry_delta: 0,
+                               htlc_minimum_msat: 0,
+                               fee_base_msat: 0,
+                               fee_proportional_millionths: 0,
+                               excess_data: vec![],
+                       }
+               }
+       }
+}
+
+#[test]
+fn test_onion_failure() {
+       use ln::msgs::ChannelUpdate;
+       use ln::channelmanager::CLTV_FAR_FAR_AWAY;
+       use secp256k1;
+
+       const BADONION: u16 = 0x8000;
+       const PERM: u16 = 0x4000;
+       const NODE: u16 = 0x2000;
+       const UPDATE: u16 = 0x1000;
+
+       let mut nodes = create_network(3, &[None, None, None]);
+       for node in nodes.iter() {
+               *node.keys_manager.override_session_priv.lock().unwrap() = Some(SecretKey::from_slice(&[3; 32]).unwrap());
+       }
+       let channels = [create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new()), create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new())];
+       let (_, payment_hash) = get_payment_preimage_hash!(nodes[0]);
+       let route = nodes[0].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 40000, TEST_FINAL_CLTV).unwrap();
+       // positve case
+       send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 40000);
+
+       // intermediate node failure
+       run_onion_failure_test("invalid_realm", 0, &nodes, &route, &payment_hash, |msg| {
+               let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
+               let cur_height = nodes[0].node.latest_block_height.load(Ordering::Acquire) as u32 + 1;
+               let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
+               let (mut onion_payloads, _htlc_msat, _htlc_cltv) = onion_utils::build_onion_payloads(&route, cur_height).unwrap();
+               onion_payloads[0].realm = 3;
+               msg.onion_routing_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, &payment_hash);
+       }, ||{}, true, Some(PERM|1), Some(msgs::HTLCFailChannelUpdate::ChannelClosed{short_channel_id: channels[1].0.contents.short_channel_id, is_permanent: true}));//XXX incremented channels idx here
+
+       // final node failure
+       run_onion_failure_test("invalid_realm", 3, &nodes, &route, &payment_hash, |msg| {
+               let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
+               let cur_height = nodes[0].node.latest_block_height.load(Ordering::Acquire) as u32 + 1;
+               let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
+               let (mut onion_payloads, _htlc_msat, _htlc_cltv) = onion_utils::build_onion_payloads(&route, cur_height).unwrap();
+               onion_payloads[1].realm = 3;
+               msg.onion_routing_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, &payment_hash);
+       }, ||{}, false, Some(PERM|1), Some(msgs::HTLCFailChannelUpdate::ChannelClosed{short_channel_id: channels[1].0.contents.short_channel_id, is_permanent: true}));
+
+       // the following three with run_onion_failure_test_with_fail_intercept() test only the origin node
+       // receiving simulated fail messages
+       // intermediate node failure
+       run_onion_failure_test_with_fail_intercept("temporary_node_failure", 100, &nodes, &route, &payment_hash, |msg| {
+               // trigger error
+               msg.amount_msat -= 1;
+       }, |msg| {
+               // and tamper returning error message
+               let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
+               let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
+               msg.reason = onion_utils::build_first_hop_failure_packet(&onion_keys[0].shared_secret[..], NODE|2, &[0;0]);
+       }, ||{}, true, Some(NODE|2), Some(msgs::HTLCFailChannelUpdate::NodeFailure{node_id: route.hops[0].pubkey, is_permanent: false}));
+
+       // final node failure
+       run_onion_failure_test_with_fail_intercept("temporary_node_failure", 200, &nodes, &route, &payment_hash, |_msg| {}, |msg| {
+               // and tamper returning error message
+               let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
+               let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
+               msg.reason = onion_utils::build_first_hop_failure_packet(&onion_keys[1].shared_secret[..], NODE|2, &[0;0]);
+       }, ||{
+               nodes[2].node.fail_htlc_backwards(&payment_hash);
+       }, true, Some(NODE|2), Some(msgs::HTLCFailChannelUpdate::NodeFailure{node_id: route.hops[1].pubkey, is_permanent: false}));
+
+       // intermediate node failure
+       run_onion_failure_test_with_fail_intercept("permanent_node_failure", 100, &nodes, &route, &payment_hash, |msg| {
+               msg.amount_msat -= 1;
+       }, |msg| {
+               let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
+               let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
+               msg.reason = onion_utils::build_first_hop_failure_packet(&onion_keys[0].shared_secret[..], PERM|NODE|2, &[0;0]);
+       }, ||{}, true, Some(PERM|NODE|2), Some(msgs::HTLCFailChannelUpdate::NodeFailure{node_id: route.hops[0].pubkey, is_permanent: true}));
+
+       // final node failure
+       run_onion_failure_test_with_fail_intercept("permanent_node_failure", 200, &nodes, &route, &payment_hash, |_msg| {}, |msg| {
+               let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
+               let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
+               msg.reason = onion_utils::build_first_hop_failure_packet(&onion_keys[1].shared_secret[..], PERM|NODE|2, &[0;0]);
+       }, ||{
+               nodes[2].node.fail_htlc_backwards(&payment_hash);
+       }, false, Some(PERM|NODE|2), Some(msgs::HTLCFailChannelUpdate::NodeFailure{node_id: route.hops[1].pubkey, is_permanent: true}));
+
+       // intermediate node failure
+       run_onion_failure_test_with_fail_intercept("required_node_feature_missing", 100, &nodes, &route, &payment_hash, |msg| {
+               msg.amount_msat -= 1;
+       }, |msg| {
+               let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
+               let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
+               msg.reason = onion_utils::build_first_hop_failure_packet(&onion_keys[0].shared_secret[..], PERM|NODE|3, &[0;0]);
+       }, ||{
+               nodes[2].node.fail_htlc_backwards(&payment_hash);
+       }, true, Some(PERM|NODE|3), Some(msgs::HTLCFailChannelUpdate::NodeFailure{node_id: route.hops[0].pubkey, is_permanent: true}));
+
+       // final node failure
+       run_onion_failure_test_with_fail_intercept("required_node_feature_missing", 200, &nodes, &route, &payment_hash, |_msg| {}, |msg| {
+               let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
+               let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
+               msg.reason = onion_utils::build_first_hop_failure_packet(&onion_keys[1].shared_secret[..], PERM|NODE|3, &[0;0]);
+       }, ||{
+               nodes[2].node.fail_htlc_backwards(&payment_hash);
+       }, false, Some(PERM|NODE|3), Some(msgs::HTLCFailChannelUpdate::NodeFailure{node_id: route.hops[1].pubkey, is_permanent: true}));
+
+       run_onion_failure_test("invalid_onion_version", 0, &nodes, &route, &payment_hash, |msg| { msg.onion_routing_packet.version = 1; }, ||{}, true,
+               Some(BADONION|PERM|4), None);
+
+       run_onion_failure_test("invalid_onion_hmac", 0, &nodes, &route, &payment_hash, |msg| { msg.onion_routing_packet.hmac = [3; 32]; }, ||{}, true,
+               Some(BADONION|PERM|5), None);
+
+       run_onion_failure_test("invalid_onion_key", 0, &nodes, &route, &payment_hash, |msg| { msg.onion_routing_packet.public_key = Err(secp256k1::Error::InvalidPublicKey);}, ||{}, true,
+               Some(BADONION|PERM|6), None);
+
+       run_onion_failure_test_with_fail_intercept("temporary_channel_failure", 100, &nodes, &route, &payment_hash, |msg| {
+               msg.amount_msat -= 1;
+       }, |msg| {
+               let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
+               let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
+               msg.reason = onion_utils::build_first_hop_failure_packet(&onion_keys[0].shared_secret[..], UPDATE|7, &ChannelUpdate::dummy().encode_with_len()[..]);
+       }, ||{}, true, Some(UPDATE|7), Some(msgs::HTLCFailChannelUpdate::ChannelUpdateMessage{msg: ChannelUpdate::dummy()}));
+
+       run_onion_failure_test_with_fail_intercept("permanent_channel_failure", 100, &nodes, &route, &payment_hash, |msg| {
+               msg.amount_msat -= 1;
+       }, |msg| {
+               let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
+               let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
+               msg.reason = onion_utils::build_first_hop_failure_packet(&onion_keys[0].shared_secret[..], PERM|8, &[0;0]);
+               // short_channel_id from the processing node
+       }, ||{}, true, Some(PERM|8), Some(msgs::HTLCFailChannelUpdate::ChannelClosed{short_channel_id: channels[1].0.contents.short_channel_id, is_permanent: true}));
+
+       run_onion_failure_test_with_fail_intercept("required_channel_feature_missing", 100, &nodes, &route, &payment_hash, |msg| {
+               msg.amount_msat -= 1;
+       }, |msg| {
+               let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
+               let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
+               msg.reason = onion_utils::build_first_hop_failure_packet(&onion_keys[0].shared_secret[..], PERM|9, &[0;0]);
+               // short_channel_id from the processing node
+       }, ||{}, true, Some(PERM|9), Some(msgs::HTLCFailChannelUpdate::ChannelClosed{short_channel_id: channels[1].0.contents.short_channel_id, is_permanent: true}));
+
+       let mut bogus_route = route.clone();
+       bogus_route.hops[1].short_channel_id -= 1;
+       run_onion_failure_test("unknown_next_peer", 0, &nodes, &bogus_route, &payment_hash, |_| {}, ||{}, true, Some(PERM|10),
+         Some(msgs::HTLCFailChannelUpdate::ChannelClosed{short_channel_id: bogus_route.hops[1].short_channel_id, is_permanent:true}));
+
+       let amt_to_forward = nodes[1].node.channel_state.lock().unwrap().by_id.get(&channels[1].2).unwrap().get_their_htlc_minimum_msat() - 1;
+       let mut bogus_route = route.clone();
+       let route_len = bogus_route.hops.len();
+       bogus_route.hops[route_len-1].fee_msat = amt_to_forward;
+       run_onion_failure_test("amount_below_minimum", 0, &nodes, &bogus_route, &payment_hash, |_| {}, ||{}, true, Some(UPDATE|11), Some(msgs::HTLCFailChannelUpdate::ChannelUpdateMessage{msg: ChannelUpdate::dummy()}));
+
+       //TODO: with new config API, we will be able to generate both valid and
+       //invalid channel_update cases.
+       run_onion_failure_test("fee_insufficient", 0, &nodes, &route, &payment_hash, |msg| {
+               msg.amount_msat -= 1;
+       }, || {}, true, Some(UPDATE|12), Some(msgs::HTLCFailChannelUpdate::ChannelClosed { short_channel_id: channels[0].0.contents.short_channel_id, is_permanent: true}));
+
+       run_onion_failure_test("incorrect_cltv_expiry", 0, &nodes, &route, &payment_hash, |msg| {
+               // need to violate: cltv_expiry - cltv_expiry_delta >= outgoing_cltv_value
+               msg.cltv_expiry -= 1;
+       }, || {}, true, Some(UPDATE|13), Some(msgs::HTLCFailChannelUpdate::ChannelClosed { short_channel_id: channels[0].0.contents.short_channel_id, is_permanent: true}));
+
+       run_onion_failure_test("expiry_too_soon", 0, &nodes, &route, &payment_hash, |msg| {
+               let height = msg.cltv_expiry - CLTV_CLAIM_BUFFER - LATENCY_GRACE_PERIOD_BLOCKS + 1;
+               let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+               nodes[1].chain_monitor.block_connected_checked(&header, height, &Vec::new()[..], &[0; 0]);
+       }, ||{}, true, Some(UPDATE|14), Some(msgs::HTLCFailChannelUpdate::ChannelUpdateMessage{msg: ChannelUpdate::dummy()}));
+
+       run_onion_failure_test("unknown_payment_hash", 2, &nodes, &route, &payment_hash, |_| {}, || {
+               nodes[2].node.fail_htlc_backwards(&payment_hash);
+       }, false, Some(PERM|15), None);
+
+       run_onion_failure_test("final_expiry_too_soon", 1, &nodes, &route, &payment_hash, |msg| {
+               let height = msg.cltv_expiry - CLTV_CLAIM_BUFFER - LATENCY_GRACE_PERIOD_BLOCKS + 1;
+               let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+               nodes[2].chain_monitor.block_connected_checked(&header, height, &Vec::new()[..], &[0; 0]);
+       }, || {}, true, Some(17), None);
+
+       run_onion_failure_test("final_incorrect_cltv_expiry", 1, &nodes, &route, &payment_hash, |_| {}, || {
+               for (_, pending_forwards) in nodes[1].node.channel_state.lock().unwrap().borrow_parts().forward_htlcs.iter_mut() {
+                       for f in pending_forwards.iter_mut() {
+                               match f {
+                                       &mut HTLCForwardInfo::AddHTLC { ref mut forward_info, .. } =>
+                                               forward_info.outgoing_cltv_value += 1,
+                                       _ => {},
+                               }
+                       }
+               }
+       }, true, Some(18), None);
+
+       run_onion_failure_test("final_incorrect_htlc_amount", 1, &nodes, &route, &payment_hash, |_| {}, || {
+               // violate amt_to_forward > msg.amount_msat
+               for (_, pending_forwards) in nodes[1].node.channel_state.lock().unwrap().borrow_parts().forward_htlcs.iter_mut() {
+                       for f in pending_forwards.iter_mut() {
+                               match f {
+                                       &mut HTLCForwardInfo::AddHTLC { ref mut forward_info, .. } =>
+                                               forward_info.amt_to_forward -= 1,
+                                       _ => {},
+                               }
+                       }
+               }
+       }, true, Some(19), None);
+
+       run_onion_failure_test("channel_disabled", 0, &nodes, &route, &payment_hash, |_| {}, || {
+               // disconnect event to the channel between nodes[1] ~ nodes[2]
+               nodes[1].node.peer_disconnected(&nodes[2].node.get_our_node_id(), false);
+               nodes[2].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
+       }, true, Some(UPDATE|20), Some(msgs::HTLCFailChannelUpdate::ChannelUpdateMessage{msg: ChannelUpdate::dummy()}));
+       reconnect_nodes(&nodes[1], &nodes[2], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
+
+       run_onion_failure_test("expiry_too_far", 0, &nodes, &route, &payment_hash, |msg| {
+               let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
+               let mut route = route.clone();
+               let height = 1;
+               route.hops[1].cltv_expiry_delta += CLTV_FAR_FAR_AWAY + route.hops[0].cltv_expiry_delta + 1;
+               let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
+               let (onion_payloads, _, htlc_cltv) = onion_utils::build_onion_payloads(&route, height).unwrap();
+               let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, &payment_hash);
+               msg.cltv_expiry = htlc_cltv;
+               msg.onion_routing_packet = onion_packet;
+       }, ||{}, true, Some(21), None);
+}
+
+#[test]
+#[should_panic]
+fn bolt2_open_channel_sending_node_checks_part1() { //This test needs to be on its own as we are catching a panic
+       let nodes = create_network(2, &[None, None]);
+       //Force duplicate channel ids
+       for node in nodes.iter() {
+               *node.keys_manager.override_channel_id_priv.lock().unwrap() = Some([0; 32]);
+       }
+
+       // BOLT #2 spec: Sending node must ensure temporary_channel_id is unique from any other channel ID with the same peer.
+       let channel_value_satoshis=10000;
+       let push_msat=10001;
+       nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42).unwrap();
+       let node0_to_1_send_open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
+       nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), LocalFeatures::new(), &node0_to_1_send_open_channel).unwrap();
+
+       //Create a second channel with a channel_id collision
+       assert!(nodes[0].node.create_channel(nodes[0].node.get_our_node_id(), channel_value_satoshis, push_msat, 42).is_err());
+}
+
+#[test]
+fn bolt2_open_channel_sending_node_checks_part2() {
+       let nodes = create_network(2, &[None, None]);
+
+       // BOLT #2 spec: Sending node must set funding_satoshis to less than 2^24 satoshis
+       let channel_value_satoshis=2^24;
+       let push_msat=10001;
+       assert!(nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42).is_err());
+
+       // BOLT #2 spec: Sending node must set push_msat to equal or less than 1000 * funding_satoshis
+       let channel_value_satoshis=10000;
+       // Test when push_msat is equal to 1000 * funding_satoshis.
+       let push_msat=1000*channel_value_satoshis+1;
+       assert!(nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42).is_err());
+
+       // BOLT #2 spec: Sending node must set set channel_reserve_satoshis greater than or equal to dust_limit_satoshis
+       let channel_value_satoshis=10000;
+       let push_msat=10001;
+       assert!(nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42).is_ok()); //Create a valid channel
+       let node0_to_1_send_open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
+       assert!(node0_to_1_send_open_channel.channel_reserve_satoshis>=node0_to_1_send_open_channel.dust_limit_satoshis);
+
+       // BOLT #2 spec: Sending node must set undefined bits in channel_flags to 0
+       // Only the least-significant bit of channel_flags is currently defined resulting in channel_flags only having one of two possible states 0 or 1
+       assert!(node0_to_1_send_open_channel.channel_flags<=1);
+
+       // BOLT #2 spec: Sending node should set to_self_delay sufficient to ensure the sender can irreversibly spend a commitment transaction output, in case of misbehaviour by the receiver.
+       assert!(BREAKDOWN_TIMEOUT>0);
+       assert!(node0_to_1_send_open_channel.to_self_delay==BREAKDOWN_TIMEOUT);
+
+       // BOLT #2 spec: Sending node must ensure the chain_hash value identifies the chain it wishes to open the channel within.
+       let chain_hash=genesis_block(Network::Testnet).header.bitcoin_hash();
+       assert_eq!(node0_to_1_send_open_channel.chain_hash,chain_hash);
+
+       // BOLT #2 spec: Sending node must set funding_pubkey, revocation_basepoint, htlc_basepoint, payment_basepoint, and delayed_payment_basepoint to valid DER-encoded, compressed, secp256k1 pubkeys.
+       assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.funding_pubkey.serialize()).is_ok());
+       assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.revocation_basepoint.serialize()).is_ok());
+       assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.htlc_basepoint.serialize()).is_ok());
+       assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.payment_basepoint.serialize()).is_ok());
+       assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.delayed_payment_basepoint.serialize()).is_ok());
+}
+
+// BOLT 2 Requirements for the Sender when constructing and sending an update_add_htlc message.
+// BOLT 2 Requirement: MUST NOT offer amount_msat it cannot pay for in the remote commitment transaction at the current feerate_per_kw (see "Updating Fees") while maintaining its channel reserve.
+//TODO: I don't believe this is explicitly enforced when sending an HTLC but as the Fee aspect of the BOLT specs is in flux leaving this as a TODO.
+
+#[test]
+fn test_update_add_htlc_bolt2_sender_value_below_minimum_msat() {
+       //BOLT2 Requirement: MUST offer amount_msat greater than 0.
+       //BOLT2 Requirement: MUST NOT offer amount_msat below the receiving node's htlc_minimum_msat (same validation check catches both of these)
+       let mut nodes = create_network(2, &[None, None]);
+       let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, LocalFeatures::new(), LocalFeatures::new());
+       let mut route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap();
+       let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
+
+       route.hops[0].fee_msat = 0;
+
+       let err = nodes[0].node.send_payment(route, our_payment_hash);
+
+       if let Err(APIError::ChannelUnavailable{err}) = err {
+               assert_eq!(err, "Cannot send less than their minimum HTLC value");
+       } else {
+               assert!(false);
+       }
+}
+
+#[test]
+fn test_update_add_htlc_bolt2_sender_cltv_expiry_too_high() {
+       //BOLT 2 Requirement: MUST set cltv_expiry less than 500000000.
+       //It is enforced when constructing a route.
+       let mut nodes = create_network(2, &[None, None]);
+       let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 0, LocalFeatures::new(), LocalFeatures::new());
+       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 100000000, 500000001).unwrap();
+       let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
+
+       let err = nodes[0].node.send_payment(route, our_payment_hash);
+
+       if let Err(APIError::RouteError{err}) = err {
+               assert_eq!(err, "Channel CLTV overflowed?!");
+       } else {
+               assert!(false);
+       }
+}
+
+#[test]
+fn test_update_add_htlc_bolt2_sender_exceed_max_htlc_num_and_htlc_id_increment() {
+       //BOLT 2 Requirement: if result would be offering more than the remote's max_accepted_htlcs HTLCs, in the remote commitment transaction: MUST NOT add an HTLC.
+       //BOLT 2 Requirement: for the first HTLC it offers MUST set id to 0.
+       //BOLT 2 Requirement: MUST increase the value of id by 1 for each successive offer.
+       let mut nodes = create_network(2, &[None, None]);
+       let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 0, LocalFeatures::new(), LocalFeatures::new());
+       let max_accepted_htlcs = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().their_max_accepted_htlcs as u64;
+
+       for i in 0..max_accepted_htlcs {
+               let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap();
+               let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
+               let payment_event = {
+                       nodes[0].node.send_payment(route, our_payment_hash).unwrap();
+                       check_added_monitors!(nodes[0], 1);
+
+                       let mut events = nodes[0].node.get_and_clear_pending_msg_events();
+                       assert_eq!(events.len(), 1);
+                       if let MessageSendEvent::UpdateHTLCs { node_id: _, updates: msgs::CommitmentUpdate{ update_add_htlcs: ref htlcs, .. }, } = events[0] {
+                               assert_eq!(htlcs[0].htlc_id, i);
+                       } else {
+                               assert!(false);
+                       }
+                       SendEvent::from_event(events.remove(0))
+               };
+               nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
+               check_added_monitors!(nodes[1], 0);
+               commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
+
+               expect_pending_htlcs_forwardable!(nodes[1]);
+               expect_payment_received!(nodes[1], our_payment_hash, 100000);
+       }
+       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap();
+       let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
+       let err = nodes[0].node.send_payment(route, our_payment_hash);
+
+       if let Err(APIError::ChannelUnavailable{err}) = err {
+               assert_eq!(err, "Cannot push more than their max accepted HTLCs");
+       } else {
+               assert!(false);
+       }
+}
+
+#[test]
+fn test_update_add_htlc_bolt2_sender_exceed_max_htlc_value_in_flight() {
+       //BOLT 2 Requirement: if the sum of total offered HTLCs would exceed the remote's max_htlc_value_in_flight_msat: MUST NOT add an HTLC.
+       let mut nodes = create_network(2, &[None, None]);
+       let channel_value = 100000;
+       let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, channel_value, 0, LocalFeatures::new(), LocalFeatures::new());
+       let max_in_flight = get_channel_value_stat!(nodes[0], chan.2).their_max_htlc_value_in_flight_msat;
+
+       send_payment(&nodes[0], &vec!(&nodes[1])[..], max_in_flight);
+
+       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], max_in_flight+1, TEST_FINAL_CLTV).unwrap();
+       let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
+       let err = nodes[0].node.send_payment(route, our_payment_hash);
+
+       if let Err(APIError::ChannelUnavailable{err}) = err {
+               assert_eq!(err, "Cannot send value that would put us over the max HTLC value in flight our peer will accept");
+       } else {
+               assert!(false);
+       }
+
+       send_payment(&nodes[0], &[&nodes[1]], max_in_flight);
+}
+
+// BOLT 2 Requirements for the Receiver when handling an update_add_htlc message.
+#[test]
+fn test_update_add_htlc_bolt2_receiver_check_amount_received_more_than_min() {
+       //BOLT2 Requirement: receiving an amount_msat equal to 0, OR less than its own htlc_minimum_msat -> SHOULD fail the channel.
+       let mut nodes = create_network(2, &[None, None]);
+       let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, LocalFeatures::new(), LocalFeatures::new());
+       let htlc_minimum_msat: u64;
+       {
+               let chan_lock = nodes[0].node.channel_state.lock().unwrap();
+               let channel = chan_lock.by_id.get(&chan.2).unwrap();
+               htlc_minimum_msat = channel.get_our_htlc_minimum_msat();
+       }
+       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], htlc_minimum_msat, TEST_FINAL_CLTV).unwrap();
+       let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
+       nodes[0].node.send_payment(route, our_payment_hash).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
+       updates.update_add_htlcs[0].amount_msat = htlc_minimum_msat-1;
+       let err = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
+       if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
+               assert_eq!(err, "Remote side tried to send less than our minimum HTLC value");
+       } else {
+               assert!(false);
+       }
+       assert!(nodes[1].node.list_channels().is_empty());
+       check_closed_broadcast!(nodes[1]);
+}
+
+#[test]
+fn test_update_add_htlc_bolt2_receiver_sender_can_afford_amount_sent() {
+       //BOLT2 Requirement: receiving an amount_msat that the sending node cannot afford at the current feerate_per_kw (while maintaining its channel reserve): SHOULD fail the channel
+       let mut nodes = create_network(2, &[None, None]);
+       let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, LocalFeatures::new(), LocalFeatures::new());
+
+       let their_channel_reserve = get_channel_value_stat!(nodes[0], chan.2).channel_reserve_msat;
+
+       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 5000000-their_channel_reserve, TEST_FINAL_CLTV).unwrap();
+       let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
+       nodes[0].node.send_payment(route, our_payment_hash).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
+
+       updates.update_add_htlcs[0].amount_msat = 5000000-their_channel_reserve+1;
+       let err = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
+
+       if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
+               assert_eq!(err, "Remote HTLC add would put them over their reserve value");
+       } else {
+               assert!(false);
+       }
+
+       assert!(nodes[1].node.list_channels().is_empty());
+       check_closed_broadcast!(nodes[1]);
+}
+
+#[test]
+fn test_update_add_htlc_bolt2_receiver_check_max_htlc_limit() {
+       //BOLT 2 Requirement: if a sending node adds more than its max_accepted_htlcs HTLCs to its local commitment transaction: SHOULD fail the channel
+       //BOLT 2 Requirement: MUST allow multiple HTLCs with the same payment_hash.
+       let mut nodes = create_network(2, &[None, None]);
+       let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, LocalFeatures::new(), LocalFeatures::new());
+       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 3999999, TEST_FINAL_CLTV).unwrap();
+       let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
+
+       let session_priv = SecretKey::from_slice(&{
+               let mut session_key = [0; 32];
+               let mut rng = thread_rng();
+               rng.fill_bytes(&mut session_key);
+               session_key
+       }).expect("RNG is bad!");
+
+       let cur_height = nodes[0].node.latest_block_height.load(Ordering::Acquire) as u32 + 1;
+       let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::signing_only(), &route, &session_priv).unwrap();
+       let (onion_payloads, _htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(&route, cur_height).unwrap();
+       let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, &our_payment_hash);
+
+       let mut msg = msgs::UpdateAddHTLC {
+               channel_id: chan.2,
+               htlc_id: 0,
+               amount_msat: 1000,
+               payment_hash: our_payment_hash,
+               cltv_expiry: htlc_cltv,
+               onion_routing_packet: onion_packet.clone(),
+       };
+
+       for i in 0..super::channel::OUR_MAX_HTLCS {
+               msg.htlc_id = i as u64;
+               nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &msg).unwrap();
+       }
+       msg.htlc_id = (super::channel::OUR_MAX_HTLCS) as u64;
+       let err = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &msg);
+
+       if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
+               assert_eq!(err, "Remote tried to push more than our max accepted HTLCs");
+       } else {
+               assert!(false);
+       }
+
+       assert!(nodes[1].node.list_channels().is_empty());
+       check_closed_broadcast!(nodes[1]);
+}
+
+#[test]
+fn test_update_add_htlc_bolt2_receiver_check_max_in_flight_msat() {
+       //OR adds more than its max_htlc_value_in_flight_msat worth of offered HTLCs to its local commitment transaction: SHOULD fail the channel
+       let mut nodes = create_network(2, &[None, None]);
+       let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000, LocalFeatures::new(), LocalFeatures::new());
+       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV).unwrap();
+       let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
+       nodes[0].node.send_payment(route, our_payment_hash).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
+       updates.update_add_htlcs[0].amount_msat = get_channel_value_stat!(nodes[1], chan.2).their_max_htlc_value_in_flight_msat + 1;
+       let err = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
+
+       if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
+               assert_eq!(err,"Remote HTLC add would put them over our max HTLC value");
+       } else {
+               assert!(false);
+       }
+
+       assert!(nodes[1].node.list_channels().is_empty());
+       check_closed_broadcast!(nodes[1]);
+}
+
+#[test]
+fn test_update_add_htlc_bolt2_receiver_check_cltv_expiry() {
+       //BOLT2 Requirement: if sending node sets cltv_expiry to greater or equal to 500000000: SHOULD fail the channel.
+       let mut nodes = create_network(2, &[None, None]);
+       create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, LocalFeatures::new(), LocalFeatures::new());
+       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 3999999, TEST_FINAL_CLTV).unwrap();
+       let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
+       nodes[0].node.send_payment(route, our_payment_hash).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
+       updates.update_add_htlcs[0].cltv_expiry = 500000000;
+       let err = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
+
+       if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
+               assert_eq!(err,"Remote provided CLTV expiry in seconds instead of block height");
+       } else {
+               assert!(false);
+       }
+
+       assert!(nodes[1].node.list_channels().is_empty());
+       check_closed_broadcast!(nodes[1]);
+}
+
+#[test]
+fn test_update_add_htlc_bolt2_receiver_check_repeated_id_ignore() {
+       //BOLT 2 requirement: if the sender did not previously acknowledge the commitment of that HTLC: MUST ignore a repeated id value after a reconnection.
+       // We test this by first testing that that repeated HTLCs pass commitment signature checks
+       // after disconnect and that non-sequential htlc_ids result in a channel failure.
+       let mut nodes = create_network(2, &[None, None]);
+       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV).unwrap();
+       let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
+       nodes[0].node.send_payment(route, our_payment_hash).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
+       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]).unwrap();
+
+       //Disconnect and Reconnect
+       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
+       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
+       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id());
+       let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
+       assert_eq!(reestablish_1.len(), 1);
+       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id());
+       let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
+       assert_eq!(reestablish_2.len(), 1);
+       nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]).unwrap();
+       handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
+       nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]).unwrap();
+       handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
+
+       //Resend HTLC
+       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]).unwrap();
+       assert_eq!(updates.commitment_signed.htlc_signatures.len(), 1);
+       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &updates.commitment_signed).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       let _bs_responses = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+
+       let err = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
+       if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
+               assert_eq!(err, "Remote skipped HTLC ID");
+       } else {
+               assert!(false);
+       }
+
+       assert!(nodes[1].node.list_channels().is_empty());
+       check_closed_broadcast!(nodes[1]);
+}
+
+#[test]
+fn test_update_fulfill_htlc_bolt2_update_fulfill_htlc_before_commitment() {
+       //BOLT 2 Requirement: until the corresponding HTLC is irrevocably committed in both sides' commitment transactions:     MUST NOT send an update_fulfill_htlc, update_fail_htlc, or update_fail_malformed_htlc.
+
+       let mut nodes = create_network(2, &[None, None]);
+       let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV).unwrap();
+       let (our_payment_preimage, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
+       nodes[0].node.send_payment(route, our_payment_hash).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
+       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]).unwrap();
+
+       let update_msg = msgs::UpdateFulfillHTLC{
+               channel_id: chan.2,
+               htlc_id: 0,
+               payment_preimage: our_payment_preimage,
+       };
+
+       let err = nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_msg);
+
+       if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
+               assert_eq!(err, "Remote tried to fulfill/fail HTLC before it had been committed");
+       } else {
+               assert!(false);
+       }
+
+       assert!(nodes[0].node.list_channels().is_empty());
+       check_closed_broadcast!(nodes[0]);
+}
+
+#[test]
+fn test_update_fulfill_htlc_bolt2_update_fail_htlc_before_commitment() {
+       //BOLT 2 Requirement: until the corresponding HTLC is irrevocably committed in both sides' commitment transactions:     MUST NOT send an update_fulfill_htlc, update_fail_htlc, or update_fail_malformed_htlc.
+
+       let mut nodes = create_network(2, &[None, None]);
+       let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV).unwrap();
+       let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
+       nodes[0].node.send_payment(route, our_payment_hash).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
+       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]).unwrap();
+
+       let update_msg = msgs::UpdateFailHTLC{
+               channel_id: chan.2,
+               htlc_id: 0,
+               reason: msgs::OnionErrorPacket { data: Vec::new()},
+       };
+
+       let err = nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_msg);
+
+       if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
+               assert_eq!(err, "Remote tried to fulfill/fail HTLC before it had been committed");
+       } else {
+               assert!(false);
+       }
+
+       assert!(nodes[0].node.list_channels().is_empty());
+       check_closed_broadcast!(nodes[0]);
+}
+
+#[test]
+fn test_update_fulfill_htlc_bolt2_update_fail_malformed_htlc_before_commitment() {
+       //BOLT 2 Requirement: until the corresponding HTLC is irrevocably committed in both sides' commitment transactions:     MUST NOT send an update_fulfill_htlc, update_fail_htlc, or update_fail_malformed_htlc.
+
+       let mut nodes = create_network(2, &[None, None]);
+       let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV).unwrap();
+       let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
+       nodes[0].node.send_payment(route, our_payment_hash).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
+       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]).unwrap();
+
+       let update_msg = msgs::UpdateFailMalformedHTLC{
+               channel_id: chan.2,
+               htlc_id: 0,
+               sha256_of_onion: [1; 32],
+               failure_code: 0x8000,
+       };
+
+       let err = nodes[0].node.handle_update_fail_malformed_htlc(&nodes[1].node.get_our_node_id(), &update_msg);
+
+       if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
+               assert_eq!(err, "Remote tried to fulfill/fail HTLC before it had been committed");
+       } else {
+               assert!(false);
+       }
+
+       assert!(nodes[0].node.list_channels().is_empty());
+       check_closed_broadcast!(nodes[0]);
+}
+
+#[test]
+fn test_update_fulfill_htlc_bolt2_incorrect_htlc_id() {
+       //BOLT 2 Requirement: A receiving node: if the id does not correspond to an HTLC in its current commitment transaction MUST fail the channel.
+
+       let nodes = create_network(2, &[None, None]);
+       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       let our_payment_preimage = route_payment(&nodes[0], &[&nodes[1]], 100000).0;
+
+       nodes[1].node.claim_funds(our_payment_preimage);
+       check_added_monitors!(nodes[1], 1);
+
+       let events = nodes[1].node.get_and_clear_pending_msg_events();
+       assert_eq!(events.len(), 1);
+       let mut update_fulfill_msg: msgs::UpdateFulfillHTLC = {
+               match events[0] {
+                       MessageSendEvent::UpdateHTLCs { node_id: _ , updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, .. } } => {
+                               assert!(update_add_htlcs.is_empty());
+                               assert_eq!(update_fulfill_htlcs.len(), 1);
+                               assert!(update_fail_htlcs.is_empty());
+                               assert!(update_fail_malformed_htlcs.is_empty());
+                               assert!(update_fee.is_none());
+                               update_fulfill_htlcs[0].clone()
+                       },
+                       _ => panic!("Unexpected event"),
+               }
+       };
+
+       update_fulfill_msg.htlc_id = 1;
+
+       let err = nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_msg);
+       if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
+               assert_eq!(err, "Remote tried to fulfill/fail an HTLC we couldn't find");
+       } else {
+               assert!(false);
+       }
+
+       assert!(nodes[0].node.list_channels().is_empty());
+       check_closed_broadcast!(nodes[0]);
+}
+
+#[test]
+fn test_update_fulfill_htlc_bolt2_wrong_preimage() {
+       //BOLT 2 Requirement: A receiving node: if the payment_preimage value in update_fulfill_htlc doesn't SHA256 hash to the corresponding HTLC payment_hash MUST fail the channel.
+
+       let nodes = create_network(2, &[None, None]);
+       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       let our_payment_preimage = route_payment(&nodes[0], &[&nodes[1]], 100000).0;
+
+       nodes[1].node.claim_funds(our_payment_preimage);
+       check_added_monitors!(nodes[1], 1);
+
+       let events = nodes[1].node.get_and_clear_pending_msg_events();
+       assert_eq!(events.len(), 1);
+       let mut update_fulfill_msg: msgs::UpdateFulfillHTLC = {
+               match events[0] {
+                       MessageSendEvent::UpdateHTLCs { node_id: _ , updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, .. } } => {
+                               assert!(update_add_htlcs.is_empty());
+                               assert_eq!(update_fulfill_htlcs.len(), 1);
+                               assert!(update_fail_htlcs.is_empty());
+                               assert!(update_fail_malformed_htlcs.is_empty());
+                               assert!(update_fee.is_none());
+                               update_fulfill_htlcs[0].clone()
+                       },
+                       _ => panic!("Unexpected event"),
+               }
+       };
+
+       update_fulfill_msg.payment_preimage = PaymentPreimage([1; 32]);
+
+       let err = nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_msg);
+       if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
+               assert_eq!(err, "Remote tried to fulfill HTLC with an incorrect preimage");
+       } else {
+               assert!(false);
+       }
+
+       assert!(nodes[0].node.list_channels().is_empty());
+       check_closed_broadcast!(nodes[0]);
+}
+
+
+#[test]
+fn test_update_fulfill_htlc_bolt2_missing_badonion_bit_for_malformed_htlc_message() {
+       //BOLT 2 Requirement: A receiving node: if the BADONION bit in failure_code is not set for update_fail_malformed_htlc MUST fail the channel.
+
+       let mut nodes = create_network(2, &[None, None]);
+       create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000, LocalFeatures::new(), LocalFeatures::new());
+       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV).unwrap();
+       let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
+       nodes[0].node.send_payment(route, our_payment_hash).unwrap();
+       check_added_monitors!(nodes[0], 1);
+
+       let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
+       updates.update_add_htlcs[0].onion_routing_packet.version = 1; //Produce a malformed HTLC message
+
+       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]).unwrap();
+       check_added_monitors!(nodes[1], 0);
+       commitment_signed_dance!(nodes[1], nodes[0], updates.commitment_signed, false, true);
+
+       let events = nodes[1].node.get_and_clear_pending_msg_events();
+
+       let mut update_msg: msgs::UpdateFailMalformedHTLC = {
+               match events[0] {
+                       MessageSendEvent::UpdateHTLCs { node_id: _ , updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, .. } } => {
+                               assert!(update_add_htlcs.is_empty());
+                               assert!(update_fulfill_htlcs.is_empty());
+                               assert!(update_fail_htlcs.is_empty());
+                               assert_eq!(update_fail_malformed_htlcs.len(), 1);
+                               assert!(update_fee.is_none());
+                               update_fail_malformed_htlcs[0].clone()
+                       },
+                       _ => panic!("Unexpected event"),
+               }
+       };
+       update_msg.failure_code &= !0x8000;
+       let err = nodes[0].node.handle_update_fail_malformed_htlc(&nodes[1].node.get_our_node_id(), &update_msg);
+       if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
+               assert_eq!(err, "Got update_fail_malformed_htlc with BADONION not set");
+       } else {
+               assert!(false);
+       }
+
+       assert!(nodes[0].node.list_channels().is_empty());
+       check_closed_broadcast!(nodes[0]);
+}
+
+#[test]
+fn test_update_fulfill_htlc_bolt2_after_malformed_htlc_message_must_forward_update_fail_htlc() {
+       //BOLT 2 Requirement: a receiving node which has an outgoing HTLC canceled by update_fail_malformed_htlc:
+       //    * MUST return an error in the update_fail_htlc sent to the link which originally sent the HTLC, using the failure_code given and setting the data to sha256_of_onion.
+
+       let mut nodes = create_network(3, &[None, None, None]);
+       create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000, LocalFeatures::new(), LocalFeatures::new());
+       create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1000000, 1000000, LocalFeatures::new(), LocalFeatures::new());
+
+       let route = nodes[0].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 100000, TEST_FINAL_CLTV).unwrap();
+       let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
+
+       //First hop
+       let mut payment_event = {
+               nodes[0].node.send_payment(route, our_payment_hash).unwrap();
+               check_added_monitors!(nodes[0], 1);
+               let mut events = nodes[0].node.get_and_clear_pending_msg_events();
+               assert_eq!(events.len(), 1);
+               SendEvent::from_event(events.remove(0))
+       };
+       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
+       check_added_monitors!(nodes[1], 0);
+       commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
+       expect_pending_htlcs_forwardable!(nodes[1]);
+       let mut events_2 = nodes[1].node.get_and_clear_pending_msg_events();
+       assert_eq!(events_2.len(), 1);
+       check_added_monitors!(nodes[1], 1);
+       payment_event = SendEvent::from_event(events_2.remove(0));
+       assert_eq!(payment_event.msgs.len(), 1);
+
+       //Second Hop
+       payment_event.msgs[0].onion_routing_packet.version = 1; //Produce a malformed HTLC message
+       nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
+       check_added_monitors!(nodes[2], 0);
+       commitment_signed_dance!(nodes[2], nodes[1], payment_event.commitment_msg, false, true);
+
+       let events_3 = nodes[2].node.get_and_clear_pending_msg_events();
+       assert_eq!(events_3.len(), 1);
+       let update_msg : (msgs::UpdateFailMalformedHTLC, msgs::CommitmentSigned) = {
+               match events_3[0] {
+                       MessageSendEvent::UpdateHTLCs { node_id: _ , updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => {
+                               assert!(update_add_htlcs.is_empty());
+                               assert!(update_fulfill_htlcs.is_empty());
+                               assert!(update_fail_htlcs.is_empty());
+                               assert_eq!(update_fail_malformed_htlcs.len(), 1);
+                               assert!(update_fee.is_none());
+                               (update_fail_malformed_htlcs[0].clone(), commitment_signed.clone())
+                       },
+                       _ => panic!("Unexpected event"),
+               }
+       };
+
+       nodes[1].node.handle_update_fail_malformed_htlc(&nodes[2].node.get_our_node_id(), &update_msg.0).unwrap();
+
+       check_added_monitors!(nodes[1], 0);
+       commitment_signed_dance!(nodes[1], nodes[2], update_msg.1, false, true);
+       expect_pending_htlcs_forwardable!(nodes[1]);
+       let events_4 = nodes[1].node.get_and_clear_pending_msg_events();
+       assert_eq!(events_4.len(), 1);
+
+       //Confirm that handlinge the update_malformed_htlc message produces an update_fail_htlc message to be forwarded back along the route
+       match events_4[0] {
+               MessageSendEvent::UpdateHTLCs { node_id: _ , updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, .. } } => {
+                       assert!(update_add_htlcs.is_empty());
+                       assert!(update_fulfill_htlcs.is_empty());
+                       assert_eq!(update_fail_htlcs.len(), 1);
+                       assert!(update_fail_malformed_htlcs.is_empty());
+                       assert!(update_fee.is_none());
+               },
+               _ => panic!("Unexpected event"),
+       };
+
+       check_added_monitors!(nodes[1], 1);
+}
+
+fn do_test_failure_delay_dust_htlc_local_commitment(announce_latest: bool) {
+       // Dust-HTLC failure updates must be delayed until failure-trigger tx (in this case local commitment) reach ANTI_REORG_DELAY
+       // We can have at most two valid local commitment tx, so both cases must be covered, and both txs must be checked to get them all as
+       // HTLC could have been removed from lastest local commitment tx but still valid until we get remote RAA
+
+       let nodes = create_network(2, &[None, None]);
+       let chan =create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       let bs_dust_limit = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().our_dust_limit_satoshis;
+
+       // We route 2 dust-HTLCs between A and B
+       let (_, payment_hash_1) = route_payment(&nodes[0], &[&nodes[1]], bs_dust_limit*1000);
+       let (_, payment_hash_2) = route_payment(&nodes[0], &[&nodes[1]], bs_dust_limit*1000);
+       route_payment(&nodes[0], &[&nodes[1]], 1000000);
+
+       // Cache one local commitment tx as previous
+       let as_prev_commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone();
+
+       // Fail one HTLC to prune it in the will-be-latest-local commitment tx
+       assert!(nodes[1].node.fail_htlc_backwards(&payment_hash_2));
+       check_added_monitors!(nodes[1], 0);
+       expect_pending_htlcs_forwardable!(nodes[1]);
+       check_added_monitors!(nodes[1], 1);
+
+       let remove = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+       nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &remove.update_fail_htlcs[0]).unwrap();
+       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &remove.commitment_signed).unwrap();
+       check_added_monitors!(nodes[0], 1);
+
+       // Cache one local commitment tx as lastest
+       let as_last_commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone();
+
+       let events = nodes[0].node.get_and_clear_pending_msg_events();
+       match events[0] {
+               MessageSendEvent::SendRevokeAndACK { node_id, .. } => {
+                       assert_eq!(node_id, nodes[1].node.get_our_node_id());
+               },
+               _ => panic!("Unexpected event"),
+       }
+       match events[1] {
+               MessageSendEvent::UpdateHTLCs { node_id, .. } => {
+                       assert_eq!(node_id, nodes[1].node.get_our_node_id());
+               },
+               _ => panic!("Unexpected event"),
+       }
+
+       assert_ne!(as_prev_commitment_tx, as_last_commitment_tx);
+       // Fail the 2 dust-HTLCs, move their failure in maturation buffer (htlc_updated_waiting_threshold_conf)
+       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+       if announce_latest {
+               nodes[0].chain_monitor.block_connected_checked(&header, 1, &[&as_last_commitment_tx[0]], &[1; 1]);
+       } else {
+               nodes[0].chain_monitor.block_connected_checked(&header, 1, &[&as_prev_commitment_tx[0]], &[1; 1]);
+       }
+
+       let events = nodes[0].node.get_and_clear_pending_msg_events();
+       assert_eq!(events.len(), 1);
+       match events[0] {
+               MessageSendEvent::BroadcastChannelUpdate { .. } => {},
+               _ => panic!("Unexpected event"),
+       }
+
+       assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
+       connect_blocks(&nodes[0].chain_monitor, ANTI_REORG_DELAY - 1, 1, true,  header.bitcoin_hash());
+       let events = nodes[0].node.get_and_clear_pending_events();
+       // Only 2 PaymentFailed events should show up, over-dust HTLC has to be failed by timeout tx
+       assert_eq!(events.len(), 2);
+       let mut first_failed = false;
+       for event in events {
+               match event {
+                       Event::PaymentFailed { payment_hash, .. } => {
+                               if payment_hash == payment_hash_1 {
+                                       assert!(!first_failed);
+                                       first_failed = true;
+                               } else {
+                                       assert_eq!(payment_hash, payment_hash_2);
+                               }
+                       }
+                       _ => panic!("Unexpected event"),
+               }
+       }
+}
+
+#[test]
+fn test_failure_delay_dust_htlc_local_commitment() {
+       do_test_failure_delay_dust_htlc_local_commitment(true);
+       do_test_failure_delay_dust_htlc_local_commitment(false);
+}
+
+#[test]
+fn test_no_failure_dust_htlc_local_commitment() {
+       // Transaction filters for failing back dust htlc based on local commitment txn infos has been
+       // prone to error, we test here that a dummy transaction don't fail them.
+
+       let nodes = create_network(2, &[None, None]);
+       let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       // Rebalance a bit
+       send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
+
+       let as_dust_limit = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().our_dust_limit_satoshis;
+       let bs_dust_limit = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().our_dust_limit_satoshis;
+
+       // We route 2 dust-HTLCs between A and B
+       let (preimage_1, _) = route_payment(&nodes[0], &[&nodes[1]], bs_dust_limit*1000);
+       let (preimage_2, _) = route_payment(&nodes[1], &[&nodes[0]], as_dust_limit*1000);
+
+       // Build a dummy invalid transaction trying to spend a commitment tx
+       let input = TxIn {
+               previous_output: BitcoinOutPoint { txid: chan.3.txid(), vout: 0 },
+               script_sig: Script::new(),
+               sequence: 0,
+               witness: Vec::new(),
+       };
+
+       let outp = TxOut {
+               script_pubkey: Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(),
+               value: 10000,
+       };
+
+       let dummy_tx = Transaction {
+               version: 2,
+               lock_time: 0,
+               input: vec![input],
+               output: vec![outp]
+       };
+
+       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+       nodes[0].chan_monitor.simple_monitor.block_connected(&header, 1, &[&dummy_tx], &[1;1]);
+       assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
+       assert_eq!(nodes[0].node.get_and_clear_pending_msg_events().len(), 0);
+       // We broadcast a few more block to check everything is all right
+       connect_blocks(&nodes[0].chain_monitor, 20, 1, true,  header.bitcoin_hash());
+       assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
+       assert_eq!(nodes[0].node.get_and_clear_pending_msg_events().len(), 0);
+
+       claim_payment(&nodes[0], &vec!(&nodes[1])[..], preimage_1);
+       claim_payment(&nodes[1], &vec!(&nodes[0])[..], preimage_2);
+}
+
+fn do_test_sweep_outbound_htlc_failure_update(revoked: bool, local: bool) {
+       // Outbound HTLC-failure updates must be cancelled if we get a reorg before we reach ANTI_REORG_DELAY.
+       // Broadcast of revoked remote commitment tx, trigger failure-update of dust/non-dust HTLCs
+       // Broadcast of remote commitment tx, trigger failure-update of dust-HTLCs
+       // Broadcast of timeout tx on remote commitment tx, trigger failure-udate of non-dust HTLCs
+       // Broadcast of local commitment tx, trigger failure-update of dust-HTLCs
+       // Broadcast of HTLC-timeout tx on local commitment tx, trigger failure-update of non-dust HTLCs
+
+       let nodes = create_network(3, &[None, None, None]);
+       let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+       let bs_dust_limit = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().our_dust_limit_satoshis;
+
+       let (_payment_preimage_1, dust_hash) = route_payment(&nodes[0], &[&nodes[1]], bs_dust_limit*1000);
+       let (_payment_preimage_2, non_dust_hash) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
+
+       let as_commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone();
+       let bs_commitment_tx = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone();
+
+       // We revoked bs_commitment_tx
+       if revoked {
+               let (payment_preimage_3, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
+               claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_3);
+       }
+
+       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+       let mut timeout_tx = Vec::new();
+       if local {
+               // We fail dust-HTLC 1 by broadcast of local commitment tx
+               nodes[0].chain_monitor.block_connected_checked(&header, 1, &[&as_commitment_tx[0]], &[1; 1]);
+               let events = nodes[0].node.get_and_clear_pending_msg_events();
+               assert_eq!(events.len(), 1);
+               match events[0] {
+                       MessageSendEvent::BroadcastChannelUpdate { .. } => {},
+                       _ => panic!("Unexpected event"),
+               }
+               assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
+               timeout_tx.push(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap()[0].clone());
+               let parent_hash  = connect_blocks(&nodes[0].chain_monitor, ANTI_REORG_DELAY - 1, 2, true, header.bitcoin_hash());
+               let events = nodes[0].node.get_and_clear_pending_events();
+               assert_eq!(events.len(), 1);
+               match events[0] {
+                       Event::PaymentFailed { payment_hash, .. } => {
+                               assert_eq!(payment_hash, dust_hash);
+                       },
+                       _ => panic!("Unexpected event"),
+               }
+               assert_eq!(timeout_tx[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
+               // We fail non-dust-HTLC 2 by broadcast of local HTLC-timeout tx on local commitment tx
+               let header_2 = BlockHeader { version: 0x20000000, prev_blockhash: parent_hash, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+               assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
+               nodes[0].chain_monitor.block_connected_checked(&header_2, 7, &[&timeout_tx[0]], &[1; 1]);
+               let header_3 = BlockHeader { version: 0x20000000, prev_blockhash: header_2.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+               connect_blocks(&nodes[0].chain_monitor, ANTI_REORG_DELAY - 1, 8, true, header_3.bitcoin_hash());
+               let events = nodes[0].node.get_and_clear_pending_events();
+               assert_eq!(events.len(), 1);
+               match events[0] {
+                       Event::PaymentFailed { payment_hash, .. } => {
+                               assert_eq!(payment_hash, non_dust_hash);
+                       },
+                       _ => panic!("Unexpected event"),
+               }
+       } else {
+               // We fail dust-HTLC 1 by broadcast of remote commitment tx. If revoked, fail also non-dust HTLC
+               nodes[0].chain_monitor.block_connected_checked(&header, 1, &[&bs_commitment_tx[0]], &[1; 1]);
+               assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
+               let events = nodes[0].node.get_and_clear_pending_msg_events();
+               assert_eq!(events.len(), 1);
+               match events[0] {
+                       MessageSendEvent::BroadcastChannelUpdate { .. } => {},
+                       _ => panic!("Unexpected event"),
+               }
+               timeout_tx.push(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap()[0].clone());
+               let parent_hash  = connect_blocks(&nodes[0].chain_monitor, ANTI_REORG_DELAY - 1, 2, true, header.bitcoin_hash());
+               let header_2 = BlockHeader { version: 0x20000000, prev_blockhash: parent_hash, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+               if !revoked {
+                       let events = nodes[0].node.get_and_clear_pending_events();
+                       assert_eq!(events.len(), 1);
+                       match events[0] {
+                               Event::PaymentFailed { payment_hash, .. } => {
+                                       assert_eq!(payment_hash, dust_hash);
+                               },
+                               _ => panic!("Unexpected event"),
+                       }
+                       assert_eq!(timeout_tx[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
+                       // We fail non-dust-HTLC 2 by broadcast of local timeout tx on remote commitment tx
+                       nodes[0].chain_monitor.block_connected_checked(&header_2, 7, &[&timeout_tx[0]], &[1; 1]);
+                       assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
+                       let header_3 = BlockHeader { version: 0x20000000, prev_blockhash: header_2.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+                       connect_blocks(&nodes[0].chain_monitor, ANTI_REORG_DELAY - 1, 8, true, header_3.bitcoin_hash());
+                       let events = nodes[0].node.get_and_clear_pending_events();
+                       assert_eq!(events.len(), 1);
+                       match events[0] {
+                               Event::PaymentFailed { payment_hash, .. } => {
+                                       assert_eq!(payment_hash, non_dust_hash);
+                               },
+                               _ => panic!("Unexpected event"),
+                       }
+               } else {
+                       // If revoked, both dust & non-dust HTLCs should have been failed after ANTI_REORG_DELAY confs of revoked
+                       // commitment tx
+                       let events = nodes[0].node.get_and_clear_pending_events();
+                       assert_eq!(events.len(), 2);
+                       let first;
+                       match events[0] {
+                               Event::PaymentFailed { payment_hash, .. } => {
+                                       if payment_hash == dust_hash { first = true; }
+                                       else { first = false; }
+                               },
+                               _ => panic!("Unexpected event"),
+                       }
+                       match events[1] {
+                               Event::PaymentFailed { payment_hash, .. } => {
+                                       if first { assert_eq!(payment_hash, non_dust_hash); }
+                                       else { assert_eq!(payment_hash, dust_hash); }
+                               },
+                               _ => panic!("Unexpected event"),
+                       }
+               }
+       }
+}
+
+#[test]
+fn test_sweep_outbound_htlc_failure_update() {
+       do_test_sweep_outbound_htlc_failure_update(false, true);
+       do_test_sweep_outbound_htlc_failure_update(false, false);
+       do_test_sweep_outbound_htlc_failure_update(true, false);
+}
+
+#[test]
+fn test_upfront_shutdown_script() {
+       // BOLT 2 : Option upfront shutdown script, if peer commit its closing_script at channel opening
+       // enforce it at shutdown message
+
+       let mut config = UserConfig::new();
+       config.channel_options.announced_channel = true;
+       config.peer_channel_config_limits.force_announced_channel_preference = false;
+       config.channel_options.commit_upfront_shutdown_pubkey = false;
+       let nodes = create_network(3, &[None, Some(config), None]);
+
+       // We test that in case of peer committing upfront to a script, if it changes at closing, we refuse to sign
+       let flags = LocalFeatures::new();
+       let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 1000000, 1000000, flags.clone(), flags.clone());
+       nodes[0].node.close_channel(&OutPoint::new(chan.3.txid(), 0).to_channel_id()).unwrap();
+       let mut node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[2].node.get_our_node_id());
+       node_0_shutdown.scriptpubkey = Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script().to_p2sh();
+       // Test we enforce upfront_scriptpbukey if by providing a diffrent one at closing that  we disconnect peer
+       if let Err(error) = nodes[2].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown) {
+               if let Some(error) = error.action {
+                       match error {
+                               ErrorAction::SendErrorMessage { msg } => {
+                                       assert_eq!(msg.data,"Got shutdown request with a scriptpubkey which did not match their previous scriptpubkey");
+                               },
+                               _ => { assert!(false); }
+                       }
+               } else { assert!(false); }
+       } else { assert!(false); }
+       let events = nodes[2].node.get_and_clear_pending_msg_events();
+       assert_eq!(events.len(), 1);
+       match events[0] {
+               MessageSendEvent::BroadcastChannelUpdate { .. } => {},
+               _ => panic!("Unexpected event"),
+       }
+
+       // We test that in case of peer committing upfront to a script, if it doesn't change at closing, we sign
+       let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 1000000, 1000000, flags.clone(), flags.clone());
+       nodes[0].node.close_channel(&OutPoint::new(chan.3.txid(), 0).to_channel_id()).unwrap();
+       let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[2].node.get_our_node_id());
+       // We test that in case of peer committing upfront to a script, if it oesn't change at closing, we sign
+       if let Ok(_) = nodes[2].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown) {}
+       else { assert!(false) }
+       let events = nodes[2].node.get_and_clear_pending_msg_events();
+       assert_eq!(events.len(), 1);
+       match events[0] {
+               MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[0].node.get_our_node_id()) }
+               _ => panic!("Unexpected event"),
+       }
+
+       // We test that if case of peer non-signaling we don't enforce committed script at channel opening
+       let mut flags_no = LocalFeatures::new();
+       flags_no.unset_upfront_shutdown_script();
+       let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000, flags_no, flags.clone());
+       nodes[0].node.close_channel(&OutPoint::new(chan.3.txid(), 0).to_channel_id()).unwrap();
+       let mut node_1_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
+       node_1_shutdown.scriptpubkey = Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script().to_p2sh();
+       if let Ok(_) = nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_1_shutdown) {}
+       else { assert!(false) }
+       let events = nodes[1].node.get_and_clear_pending_msg_events();
+       assert_eq!(events.len(), 1);
+       match events[0] {
+               MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[0].node.get_our_node_id()) }
+               _ => panic!("Unexpected event"),
+       }
+
+       // We test that if user opt-out, we provide a zero-length script at channel opening and we are able to close
+       // channel smoothly, opt-out is from channel initiator here
+       let chan = create_announced_chan_between_nodes_with_value(&nodes, 1, 0, 1000000, 1000000, flags.clone(), flags.clone());
+       nodes[1].node.close_channel(&OutPoint::new(chan.3.txid(), 0).to_channel_id()).unwrap();
+       let mut node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
+       node_0_shutdown.scriptpubkey = Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script().to_p2sh();
+       if let Ok(_) = nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_0_shutdown) {}
+       else { assert!(false) }
+       let events = nodes[0].node.get_and_clear_pending_msg_events();
+       assert_eq!(events.len(), 1);
+       match events[0] {
+               MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
+               _ => panic!("Unexpected event"),
+       }
+
+       //// We test that if user opt-out, we provide a zero-length script at channel opening and we are able to close
+       //// channel smoothly
+       let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000, flags.clone(), flags.clone());
+       nodes[1].node.close_channel(&OutPoint::new(chan.3.txid(), 0).to_channel_id()).unwrap();
+       let mut node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
+       node_0_shutdown.scriptpubkey = Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script().to_p2sh();
+       if let Ok(_) = nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_0_shutdown) {}
+       else { assert!(false) }
+       let events = nodes[0].node.get_and_clear_pending_msg_events();
+       assert_eq!(events.len(), 2);
+       match events[0] {
+               MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
+               _ => panic!("Unexpected event"),
+       }
+       match events[1] {
+               MessageSendEvent::SendClosingSigned { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
+               _ => panic!("Unexpected event"),
+       }
+}
+
+#[test]
+fn test_user_configurable_csv_delay() {
+       // We test our channel constructors yield errors when we pass them absurd csv delay
+
+       let mut low_our_to_self_config = UserConfig::new();
+       low_our_to_self_config.own_channel_config.our_to_self_delay = 6;
+       let mut high_their_to_self_config = UserConfig::new();
+       high_their_to_self_config.peer_channel_config_limits.their_to_self_delay = 100;
+       let nodes = create_network(2, &[Some(high_their_to_self_config.clone()), None]);
+
+       // We test config.our_to_self > BREAKDOWN_TIMEOUT is enforced in Channel::new_outbound()
+       let keys_manager: Arc<KeysInterface> = Arc::new(KeysManager::new(&nodes[0].node_seed, Network::Testnet, Arc::new(test_utils::TestLogger::new()), 10, 20));
+       if let Err(error) = Channel::new_outbound(&test_utils::TestFeeEstimator { sat_per_kw: 253 }, &keys_manager, nodes[1].node.get_our_node_id(), 1000000, 1000000, 0, Arc::new(test_utils::TestLogger::new()), &low_our_to_self_config) {
+               match error {
+                       APIError::APIMisuseError { err } => { assert_eq!(err, "Configured with an unreasonable our_to_self_delay putting user funds at risks"); },
+                       _ => panic!("Unexpected event"),
+               }
+       } else { assert!(false) }
+
+       // We test config.our_to_self > BREAKDOWN_TIMEOUT is enforced in Channel::new_from_req()
+       nodes[1].node.create_channel(nodes[0].node.get_our_node_id(), 1000000, 1000000, 42).unwrap();
+       let mut open_channel = get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[0].node.get_our_node_id());
+       open_channel.to_self_delay = 200;
+       if let Err(error) = Channel::new_from_req(&test_utils::TestFeeEstimator { sat_per_kw: 253 }, &keys_manager, nodes[1].node.get_our_node_id(), LocalFeatures::new(), &open_channel, 0, Arc::new(test_utils::TestLogger::new()), &low_our_to_self_config) {
+               match error {
+                       ChannelError::Close(err) => { assert_eq!(err, "Configured with an unreasonable our_to_self_delay putting user funds at risks"); },
+                       _ => panic!("Unexpected event"),
+               }
+       } else { assert!(false); }
+
+       // We test msg.to_self_delay <= config.their_to_self_delay is enforced in Chanel::accept_channel()
+       nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 1000000, 1000000, 42).unwrap();
+       nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), LocalFeatures::new(), &get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id())).unwrap();
+       let mut accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
+       accept_channel.to_self_delay = 200;
+       if let Err(error) = nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), LocalFeatures::new(), &accept_channel) {
+               if let Some(error) = error.action {
+                       match error {
+                               ErrorAction::SendErrorMessage { msg } => {
+                                       assert_eq!(msg.data,"They wanted our payments to be delayed by a needlessly long period");
+                               },
+                               _ => { assert!(false); }
+                       }
+               } else { assert!(false); }
+       } else { assert!(false); }
+
+       // We test msg.to_self_delay <= config.their_to_self_delay is enforced in Channel::new_from_req()
+       nodes[1].node.create_channel(nodes[0].node.get_our_node_id(), 1000000, 1000000, 42).unwrap();
+       let mut open_channel = get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[0].node.get_our_node_id());
+       open_channel.to_self_delay = 200;
+       if let Err(error) = Channel::new_from_req(&test_utils::TestFeeEstimator { sat_per_kw: 253 }, &keys_manager, nodes[1].node.get_our_node_id(), LocalFeatures::new(), &open_channel, 0, Arc::new(test_utils::TestLogger::new()), &high_their_to_self_config) {
+               match error {
+                       ChannelError::Close(err) => { assert_eq!(err, "They wanted our payments to be delayed by a needlessly long period"); },
+                       _ => panic!("Unexpected event"),
+               }
+       } else { assert!(false); }
+}
+
+#[test]
+fn test_data_loss_protect() {
+       // We want to be sure that :
+       // * we don't broadcast our Local Commitment Tx in case of fallen behind
+       // * we close channel in case of detecting other being fallen behind
+       // * we are able to claim our own outputs thanks to remote my_current_per_commitment_point
+       let mut nodes = create_network(2, &[None, None]);
+
+       let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000, LocalFeatures::new(), LocalFeatures::new());
+
+       // Cache node A state before any channel update
+       let previous_node_state = nodes[0].node.encode();
+       let mut previous_chan_monitor_state = test_utils::TestVecWriter(Vec::new());
+       nodes[0].chan_monitor.simple_monitor.monitors.lock().unwrap().iter().next().unwrap().1.write_for_disk(&mut previous_chan_monitor_state).unwrap();
+
+       send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
+       send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
+
+       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
+       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
+
+       // Restore node A from previous state
+       let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::with_id(format!("node {}", 0)));
+       let chan_monitor = <(Sha256dHash, ChannelMonitor)>::read(&mut ::std::io::Cursor::new(previous_chan_monitor_state.0), Arc::clone(&logger)).unwrap().1;
+       let chain_monitor = Arc::new(ChainWatchInterfaceUtil::new(Network::Testnet, Arc::clone(&logger)));
+       let tx_broadcaster = Arc::new(test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new())});
+       let feeest = Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 });
+       let monitor = Arc::new(test_utils::TestChannelMonitor::new(chain_monitor.clone(), tx_broadcaster.clone(), logger.clone(), feeest.clone()));
+       let mut channel_monitors = HashMap::new();
+       channel_monitors.insert(OutPoint { txid: chan.3.txid(), index: 0 }, &chan_monitor);
+       let node_state_0 = <(Sha256dHash, ChannelManager)>::read(&mut ::std::io::Cursor::new(previous_node_state), ChannelManagerReadArgs {
+               keys_manager: Arc::new(keysinterface::KeysManager::new(&nodes[0].node_seed, Network::Testnet, Arc::clone(&logger), 42, 21)),
+               fee_estimator: feeest.clone(),
+               monitor: monitor.clone(),
+               chain_monitor: chain_monitor.clone(),
+               logger: Arc::clone(&logger),
+               tx_broadcaster,
+               default_config: UserConfig::new(),
+               channel_monitors: &channel_monitors
+       }).unwrap().1;
+       nodes[0].node = Arc::new(node_state_0);
+       monitor.add_update_monitor(OutPoint { txid: chan.3.txid(), index: 0 }, chan_monitor.clone()).is_ok();
+       nodes[0].chan_monitor = monitor;
+       nodes[0].chain_monitor = chain_monitor;
+       check_added_monitors!(nodes[0], 1);
+
+       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id());
+       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id());
+
+       let reestablish_0 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
+
+       // Check we update monitor following learning of per_commitment_point from B
+       if let Err(err) = nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_0[0])  {
+               if let Some(error) = err.action {
+                       match error {
+                               ErrorAction::SendErrorMessage { msg } => {
+                                       assert_eq!(msg.data, "We have fallen behind - we have received proof that if we broadcast remote is going to claim our funds - we can't do any automated broadcasting");
+                               },
+                               _ => panic!("Unexpected event!"),
+                       }
+               } else { assert!(false); }
+       } else { assert!(false); }
+       check_added_monitors!(nodes[0], 1);
+
+       {
+               let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
+               assert_eq!(node_txn.len(), 0);
+       }
+
+       let mut reestablish_1 = Vec::with_capacity(1);
+       for msg in nodes[0].node.get_and_clear_pending_msg_events() {
+               if let MessageSendEvent::SendChannelReestablish { ref node_id, ref msg } = msg {
+                       assert_eq!(*node_id, nodes[1].node.get_our_node_id());
+                       reestablish_1.push(msg.clone());
+               } else if let MessageSendEvent::BroadcastChannelUpdate { .. } = msg {
+               } else {
+                       panic!("Unexpected event")
+               }
+       }
+
+       // Check we close channel detecting A is fallen-behind
+       if let Err(err) = nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]) {
+               if let Some(error) = err.action {
+                       match error {
+                               ErrorAction::SendErrorMessage { msg } => {
+                                       assert_eq!(msg.data, "Peer attempted to reestablish channel with a very old local commitment transaction"); },
+                               _ => panic!("Unexpected event!"),
+                       }
+               } else { assert!(false); }
+       } else { assert!(false); }
+
+       let events = nodes[1].node.get_and_clear_pending_msg_events();
+       assert_eq!(events.len(), 1);
+       match events[0] {
+               MessageSendEvent::BroadcastChannelUpdate { .. } => {},
+               _ => panic!("Unexpected event"),
+       }
+
+       // Check A is able to claim to_remote output
+       let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
+       assert_eq!(node_txn.len(), 1);
+       check_spends!(node_txn[0], chan.3.clone());
+       assert_eq!(node_txn[0].output.len(), 2);
+       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42};
+       nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn[0].clone()]}, 1);
+       let spend_txn = check_spendable_outputs!(nodes[0], 1);
+       assert_eq!(spend_txn.len(), 1);
+       check_spends!(spend_txn[0], node_txn[0].clone());
+}
diff --git a/lightning/src/ln/mod.rs b/lightning/src/ln/mod.rs
new file mode 100644 (file)
index 0000000..9b1b442
--- /dev/null
@@ -0,0 +1,32 @@
+//! High level lightning structs and impls live here.
+//!
+//! You probably want to create a channelmanager::ChannelManager, and a router::Router first.
+//! Then, you probably want to pass them both on to a peer_handler::PeerManager and use that to
+//! create/manage connections and call get_and_clear_pending_events after each action, handling
+//! them appropriately.
+//!
+//! When you want to open/close a channel or send a payment, call into your ChannelManager and when
+//! you want to learn things about the network topology (eg get a route for sending a payment),
+//! call into your Router.
+
+pub mod channelmanager;
+pub mod channelmonitor;
+pub mod msgs;
+pub mod router;
+pub mod peer_handler;
+
+#[cfg(feature = "fuzztarget")]
+pub mod peer_channel_encryptor;
+#[cfg(not(feature = "fuzztarget"))]
+pub(crate) mod peer_channel_encryptor;
+
+mod channel;
+mod chan_utils;
+mod onion_utils;
+
+#[cfg(test)]
+#[macro_use] mod functional_test_utils;
+#[cfg(test)]
+mod functional_tests;
+#[cfg(test)]
+mod chanmon_update_fail_tests;
diff --git a/lightning/src/ln/msgs.rs b/lightning/src/ln/msgs.rs
new file mode 100644 (file)
index 0000000..0e6c4b9
--- /dev/null
@@ -0,0 +1,2063 @@
+//! Wire messages, traits representing wire message handlers, and a few error types live here.
+//!
+//! For a normal node you probably don't need to use anything here, however, if you wish to split a
+//! node into an internet-facing route/message socket handling daemon and a separate daemon (or
+//! server entirely) which handles only channel-related messages you may wish to implement
+//! ChannelMessageHandler yourself and use it to re-serialize messages and pass them across
+//! daemons/servers.
+//!
+//! Note that if you go with such an architecture (instead of passing raw socket events to a
+//! non-internet-facing system) you trust the frontend internet-facing system to not lie about the
+//! source node_id of the message, however this does allow you to significantly reduce bandwidth
+//! between the systems as routing messages can represent a significant chunk of bandwidth usage
+//! (especially for non-channel-publicly-announcing nodes). As an alternate design which avoids
+//! this issue, if you have sufficient bidirectional bandwidth between your systems, you may send
+//! raw socket events into your non-internet-facing system and then send routing events back to
+//! track the network on the less-secure system.
+
+use secp256k1::key::PublicKey;
+use secp256k1::Signature;
+use secp256k1;
+use bitcoin_hashes::sha256d::Hash as Sha256dHash;
+use bitcoin::blockdata::script::Script;
+
+use std::error::Error;
+use std::{cmp, fmt};
+use std::io::Read;
+use std::result::Result;
+
+use util::events;
+use util::ser::{Readable, Writeable, Writer};
+
+use ln::channelmanager::{PaymentPreimage, PaymentHash};
+
+/// An error in decoding a message or struct.
+#[derive(Debug)]
+pub enum DecodeError {
+       /// A version byte specified something we don't know how to handle.
+       /// Includes unknown realm byte in an OnionHopData packet
+       UnknownVersion,
+       /// Unknown feature mandating we fail to parse message
+       UnknownRequiredFeature,
+       /// Value was invalid, eg a byte which was supposed to be a bool was something other than a 0
+       /// or 1, a public key/private key/signature was invalid, text wasn't UTF-8, etc
+       InvalidValue,
+       /// Buffer too short
+       ShortRead,
+       /// node_announcement included more than one address of a given type!
+       ExtraAddressesPerType,
+       /// A length descriptor in the packet didn't describe the later data correctly
+       BadLengthDescriptor,
+       /// Error from std::io
+       Io(::std::io::Error),
+}
+
+/// Tracks localfeatures which are only in init messages
+#[derive(Clone, PartialEq)]
+pub struct LocalFeatures {
+       flags: Vec<u8>,
+}
+
+impl LocalFeatures {
+       /// Create a blank LocalFeatures flags (visibility extended for fuzz tests)
+       #[cfg(not(feature = "fuzztarget"))]
+       pub(crate) fn new() -> LocalFeatures {
+               LocalFeatures {
+                       flags: vec![2 | 1 << 5],
+               }
+       }
+       #[cfg(feature = "fuzztarget")]
+       pub fn new() -> LocalFeatures {
+               LocalFeatures {
+                       flags: vec![2 | 1 << 5],
+               }
+       }
+
+       pub(crate) fn supports_data_loss_protect(&self) -> bool {
+               self.flags.len() > 0 && (self.flags[0] & 3) != 0
+       }
+       pub(crate) fn initial_routing_sync(&self) -> bool {
+               self.flags.len() > 0 && (self.flags[0] & (1 << 3)) != 0
+       }
+       pub(crate) fn set_initial_routing_sync(&mut self) {
+               if self.flags.len() == 0 {
+                       self.flags.resize(1, 1 << 3);
+               } else {
+                       self.flags[0] |= 1 << 3;
+               }
+       }
+
+       pub(crate) fn supports_upfront_shutdown_script(&self) -> bool {
+               self.flags.len() > 0 && (self.flags[0] & (3 << 4)) != 0
+       }
+       #[cfg(test)]
+       pub(crate) fn unset_upfront_shutdown_script(&mut self) {
+               self.flags[0] ^= 1 << 5;
+       }
+
+       pub(crate) fn requires_unknown_bits(&self) -> bool {
+               self.flags.iter().enumerate().any(|(idx, &byte)| {
+                       ( idx != 0 && (byte & 0x55) != 0 ) || ( idx == 0 && (byte & 0x14) != 0 )
+               })
+       }
+
+       pub(crate) fn supports_unknown_bits(&self) -> bool {
+               self.flags.iter().enumerate().any(|(idx, &byte)| {
+                       ( idx != 0 && byte != 0 ) || ( idx == 0 && (byte & 0xc4) != 0 )
+               })
+       }
+}
+
+/// Tracks globalfeatures which are in init messages and routing announcements
+#[derive(Clone, PartialEq, Debug)]
+pub struct GlobalFeatures {
+       #[cfg(not(test))]
+       flags: Vec<u8>,
+       // Used to test encoding of diverse msgs
+       #[cfg(test)]
+       pub flags: Vec<u8>
+}
+
+impl GlobalFeatures {
+       pub(crate) fn new() -> GlobalFeatures {
+               GlobalFeatures {
+                       flags: Vec::new(),
+               }
+       }
+
+       pub(crate) fn requires_unknown_bits(&self) -> bool {
+               for &byte in self.flags.iter() {
+                       if (byte & 0x55) != 0 {
+                               return true;
+                       }
+               }
+               return false;
+       }
+
+       pub(crate) fn supports_unknown_bits(&self) -> bool {
+               for &byte in self.flags.iter() {
+                       if byte != 0 {
+                               return true;
+                       }
+               }
+               return false;
+       }
+}
+
+/// An init message to be sent or received from a peer
+pub struct Init {
+       pub(crate) global_features: GlobalFeatures,
+       pub(crate) local_features: LocalFeatures,
+}
+
+/// An error message to be sent or received from a peer
+#[derive(Clone)]
+pub struct ErrorMessage {
+       pub(crate) channel_id: [u8; 32],
+       pub(crate) data: String,
+}
+
+/// A ping message to be sent or received from a peer
+pub struct Ping {
+       pub(crate) ponglen: u16,
+       pub(crate) byteslen: u16,
+}
+
+/// A pong message to be sent or received from a peer
+pub struct Pong {
+       pub(crate) byteslen: u16,
+}
+
+/// An open_channel message to be sent or received from a peer
+#[derive(Clone)]
+pub struct OpenChannel {
+       pub(crate) chain_hash: Sha256dHash,
+       pub(crate) temporary_channel_id: [u8; 32],
+       pub(crate) funding_satoshis: u64,
+       pub(crate) push_msat: u64,
+       pub(crate) dust_limit_satoshis: u64,
+       pub(crate) max_htlc_value_in_flight_msat: u64,
+       pub(crate) channel_reserve_satoshis: u64,
+       pub(crate) htlc_minimum_msat: u64,
+       pub(crate) feerate_per_kw: u32,
+       pub(crate) to_self_delay: u16,
+       pub(crate) max_accepted_htlcs: u16,
+       pub(crate) funding_pubkey: PublicKey,
+       pub(crate) revocation_basepoint: PublicKey,
+       pub(crate) payment_basepoint: PublicKey,
+       pub(crate) delayed_payment_basepoint: PublicKey,
+       pub(crate) htlc_basepoint: PublicKey,
+       pub(crate) first_per_commitment_point: PublicKey,
+       pub(crate) channel_flags: u8,
+       pub(crate) shutdown_scriptpubkey: OptionalField<Script>,
+}
+
+/// An accept_channel message to be sent or received from a peer
+#[derive(Clone)]
+pub struct AcceptChannel {
+       pub(crate) temporary_channel_id: [u8; 32],
+       pub(crate) dust_limit_satoshis: u64,
+       pub(crate) max_htlc_value_in_flight_msat: u64,
+       pub(crate) channel_reserve_satoshis: u64,
+       pub(crate) htlc_minimum_msat: u64,
+       pub(crate) minimum_depth: u32,
+       pub(crate) to_self_delay: u16,
+       pub(crate) max_accepted_htlcs: u16,
+       pub(crate) funding_pubkey: PublicKey,
+       pub(crate) revocation_basepoint: PublicKey,
+       pub(crate) payment_basepoint: PublicKey,
+       pub(crate) delayed_payment_basepoint: PublicKey,
+       pub(crate) htlc_basepoint: PublicKey,
+       pub(crate) first_per_commitment_point: PublicKey,
+       pub(crate) shutdown_scriptpubkey: OptionalField<Script>
+}
+
+/// A funding_created message to be sent or received from a peer
+#[derive(Clone)]
+pub struct FundingCreated {
+       pub(crate) temporary_channel_id: [u8; 32],
+       pub(crate) funding_txid: Sha256dHash,
+       pub(crate) funding_output_index: u16,
+       pub(crate) signature: Signature,
+}
+
+/// A funding_signed message to be sent or received from a peer
+#[derive(Clone)]
+pub struct FundingSigned {
+       pub(crate) channel_id: [u8; 32],
+       pub(crate) signature: Signature,
+}
+
+/// A funding_locked message to be sent or received from a peer
+#[derive(Clone, PartialEq)]
+pub struct FundingLocked {
+       pub(crate) channel_id: [u8; 32],
+       pub(crate) next_per_commitment_point: PublicKey,
+}
+
+/// A shutdown message to be sent or received from a peer
+#[derive(Clone, PartialEq)]
+pub struct Shutdown {
+       pub(crate) channel_id: [u8; 32],
+       pub(crate) scriptpubkey: Script,
+}
+
+/// A closing_signed message to be sent or received from a peer
+#[derive(Clone, PartialEq)]
+pub struct ClosingSigned {
+       pub(crate) channel_id: [u8; 32],
+       pub(crate) fee_satoshis: u64,
+       pub(crate) signature: Signature,
+}
+
+/// An update_add_htlc message to be sent or received from a peer
+#[derive(Clone, PartialEq)]
+pub struct UpdateAddHTLC {
+       pub(crate) channel_id: [u8; 32],
+       pub(crate) htlc_id: u64,
+       pub(crate) amount_msat: u64,
+       pub(crate) payment_hash: PaymentHash,
+       pub(crate) cltv_expiry: u32,
+       pub(crate) onion_routing_packet: OnionPacket,
+}
+
+/// An update_fulfill_htlc message to be sent or received from a peer
+#[derive(Clone, PartialEq)]
+pub struct UpdateFulfillHTLC {
+       pub(crate) channel_id: [u8; 32],
+       pub(crate) htlc_id: u64,
+       pub(crate) payment_preimage: PaymentPreimage,
+}
+
+/// An update_fail_htlc message to be sent or received from a peer
+#[derive(Clone, PartialEq)]
+pub struct UpdateFailHTLC {
+       pub(crate) channel_id: [u8; 32],
+       pub(crate) htlc_id: u64,
+       pub(crate) reason: OnionErrorPacket,
+}
+
+/// An update_fail_malformed_htlc message to be sent or received from a peer
+#[derive(Clone, PartialEq)]
+pub struct UpdateFailMalformedHTLC {
+       pub(crate) channel_id: [u8; 32],
+       pub(crate) htlc_id: u64,
+       pub(crate) sha256_of_onion: [u8; 32],
+       pub(crate) failure_code: u16,
+}
+
+/// A commitment_signed message to be sent or received from a peer
+#[derive(Clone, PartialEq)]
+pub struct CommitmentSigned {
+       pub(crate) channel_id: [u8; 32],
+       pub(crate) signature: Signature,
+       pub(crate) htlc_signatures: Vec<Signature>,
+}
+
+/// A revoke_and_ack message to be sent or received from a peer
+#[derive(Clone, PartialEq)]
+pub struct RevokeAndACK {
+       pub(crate) channel_id: [u8; 32],
+       pub(crate) per_commitment_secret: [u8; 32],
+       pub(crate) next_per_commitment_point: PublicKey,
+}
+
+/// An update_fee message to be sent or received from a peer
+#[derive(PartialEq, Clone)]
+pub struct UpdateFee {
+       pub(crate) channel_id: [u8; 32],
+       pub(crate) feerate_per_kw: u32,
+}
+
+#[derive(PartialEq, Clone)]
+pub(crate) struct DataLossProtect {
+       pub(crate) your_last_per_commitment_secret: [u8; 32],
+       pub(crate) my_current_per_commitment_point: PublicKey,
+}
+
+/// A channel_reestablish message to be sent or received from a peer
+#[derive(PartialEq, Clone)]
+pub struct ChannelReestablish {
+       pub(crate) channel_id: [u8; 32],
+       pub(crate) next_local_commitment_number: u64,
+       pub(crate) next_remote_commitment_number: u64,
+       pub(crate) data_loss_protect: OptionalField<DataLossProtect>,
+}
+
+/// An announcement_signatures message to be sent or received from a peer
+#[derive(PartialEq, Clone, Debug)]
+pub struct AnnouncementSignatures {
+       pub(crate) channel_id: [u8; 32],
+       pub(crate) short_channel_id: u64,
+       pub(crate) node_signature: Signature,
+       pub(crate) bitcoin_signature: Signature,
+}
+
+/// An address which can be used to connect to a remote peer
+#[derive(Clone, PartialEq, Debug)]
+pub enum NetAddress {
+       /// An IPv4 address/port on which the peer is listening.
+       IPv4 {
+               /// The 4-byte IPv4 address
+               addr: [u8; 4],
+               /// The port on which the node is listening
+               port: u16,
+       },
+       /// An IPv6 address/port on which the peer is listening.
+       IPv6 {
+               /// The 16-byte IPv6 address
+               addr: [u8; 16],
+               /// The port on which the node is listening
+               port: u16,
+       },
+       /// An old-style Tor onion address/port on which the peer is listening.
+       OnionV2 {
+               /// The bytes (usually encoded in base32 with ".onion" appended)
+               addr: [u8; 10],
+               /// The port on which the node is listening
+               port: u16,
+       },
+       /// A new-style Tor onion address/port on which the peer is listening.
+       /// To create the human-readable "hostname", concatenate ed25519_pubkey, checksum, and version,
+       /// wrap as base32 and append ".onion".
+       OnionV3 {
+               /// The ed25519 long-term public key of the peer
+               ed25519_pubkey: [u8; 32],
+               /// The checksum of the pubkey and version, as included in the onion address
+               checksum: u16,
+               /// The version byte, as defined by the Tor Onion v3 spec.
+               version: u8,
+               /// The port on which the node is listening
+               port: u16,
+       },
+}
+impl NetAddress {
+       fn get_id(&self) -> u8 {
+               match self {
+                       &NetAddress::IPv4 {..} => { 1 },
+                       &NetAddress::IPv6 {..} => { 2 },
+                       &NetAddress::OnionV2 {..} => { 3 },
+                       &NetAddress::OnionV3 {..} => { 4 },
+               }
+       }
+
+       /// Strict byte-length of address descriptor, 1-byte type not recorded
+       fn len(&self) -> u16 {
+               match self {
+                       &NetAddress::IPv4 { .. } => { 6 },
+                       &NetAddress::IPv6 { .. } => { 18 },
+                       &NetAddress::OnionV2 { .. } => { 12 },
+                       &NetAddress::OnionV3 { .. } => { 37 },
+               }
+       }
+}
+
+impl Writeable for NetAddress {
+       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
+               match self {
+                       &NetAddress::IPv4 { ref addr, ref port } => {
+                               1u8.write(writer)?;
+                               addr.write(writer)?;
+                               port.write(writer)?;
+                       },
+                       &NetAddress::IPv6 { ref addr, ref port } => {
+                               2u8.write(writer)?;
+                               addr.write(writer)?;
+                               port.write(writer)?;
+                       },
+                       &NetAddress::OnionV2 { ref addr, ref port } => {
+                               3u8.write(writer)?;
+                               addr.write(writer)?;
+                               port.write(writer)?;
+                       },
+                       &NetAddress::OnionV3 { ref ed25519_pubkey, ref checksum, ref version, ref port } => {
+                               4u8.write(writer)?;
+                               ed25519_pubkey.write(writer)?;
+                               checksum.write(writer)?;
+                               version.write(writer)?;
+                               port.write(writer)?;
+                       }
+               }
+               Ok(())
+       }
+}
+
+impl<R: ::std::io::Read>  Readable<R> for Result<NetAddress, u8> {
+       fn read(reader: &mut R) -> Result<Result<NetAddress, u8>, DecodeError> {
+               let byte = <u8 as Readable<R>>::read(reader)?;
+               match byte {
+                       1 => {
+                               Ok(Ok(NetAddress::IPv4 {
+                                       addr: Readable::read(reader)?,
+                                       port: Readable::read(reader)?,
+                               }))
+                       },
+                       2 => {
+                               Ok(Ok(NetAddress::IPv6 {
+                                       addr: Readable::read(reader)?,
+                                       port: Readable::read(reader)?,
+                               }))
+                       },
+                       3 => {
+                               Ok(Ok(NetAddress::OnionV2 {
+                                       addr: Readable::read(reader)?,
+                                       port: Readable::read(reader)?,
+                               }))
+                       },
+                       4 => {
+                               Ok(Ok(NetAddress::OnionV3 {
+                                       ed25519_pubkey: Readable::read(reader)?,
+                                       checksum: Readable::read(reader)?,
+                                       version: Readable::read(reader)?,
+                                       port: Readable::read(reader)?,
+                               }))
+                       },
+                       _ => return Ok(Err(byte)),
+               }
+       }
+}
+
+// Only exposed as broadcast of node_announcement should be filtered by node_id
+/// The unsigned part of a node_announcement
+#[derive(PartialEq, Clone, Debug)]
+pub struct UnsignedNodeAnnouncement {
+       pub(crate) features: GlobalFeatures,
+       pub(crate) timestamp: u32,
+       /// The node_id this announcement originated from (don't rebroadcast the node_announcement back
+       /// to this node).
+       pub        node_id: PublicKey,
+       pub(crate) rgb: [u8; 3],
+       pub(crate) alias: [u8; 32],
+       /// List of addresses on which this node is reachable. Note that you may only have up to one
+       /// address of each type, if you have more, they may be silently discarded or we may panic!
+       pub(crate) addresses: Vec<NetAddress>,
+       pub(crate) excess_address_data: Vec<u8>,
+       pub(crate) excess_data: Vec<u8>,
+}
+#[derive(PartialEq, Clone)]
+/// A node_announcement message to be sent or received from a peer
+pub struct NodeAnnouncement {
+       pub(crate) signature: Signature,
+       pub(crate) contents: UnsignedNodeAnnouncement,
+}
+
+// Only exposed as broadcast of channel_announcement should be filtered by node_id
+/// The unsigned part of a channel_announcement
+#[derive(PartialEq, Clone, Debug)]
+pub struct UnsignedChannelAnnouncement {
+       pub(crate) features: GlobalFeatures,
+       pub(crate) chain_hash: Sha256dHash,
+       pub(crate) short_channel_id: u64,
+       /// One of the two node_ids which are endpoints of this channel
+       pub        node_id_1: PublicKey,
+       /// The other of the two node_ids which are endpoints of this channel
+       pub        node_id_2: PublicKey,
+       pub(crate) bitcoin_key_1: PublicKey,
+       pub(crate) bitcoin_key_2: PublicKey,
+       pub(crate) excess_data: Vec<u8>,
+}
+/// A channel_announcement message to be sent or received from a peer
+#[derive(PartialEq, Clone, Debug)]
+pub struct ChannelAnnouncement {
+       pub(crate) node_signature_1: Signature,
+       pub(crate) node_signature_2: Signature,
+       pub(crate) bitcoin_signature_1: Signature,
+       pub(crate) bitcoin_signature_2: Signature,
+       pub(crate) contents: UnsignedChannelAnnouncement,
+}
+
+#[derive(PartialEq, Clone, Debug)]
+pub(crate) struct UnsignedChannelUpdate {
+       pub(crate) chain_hash: Sha256dHash,
+       pub(crate) short_channel_id: u64,
+       pub(crate) timestamp: u32,
+       pub(crate) flags: u16,
+       pub(crate) cltv_expiry_delta: u16,
+       pub(crate) htlc_minimum_msat: u64,
+       pub(crate) fee_base_msat: u32,
+       pub(crate) fee_proportional_millionths: u32,
+       pub(crate) excess_data: Vec<u8>,
+}
+/// A channel_update message to be sent or received from a peer
+#[derive(PartialEq, Clone, Debug)]
+pub struct ChannelUpdate {
+       pub(crate) signature: Signature,
+       pub(crate) contents: UnsignedChannelUpdate,
+}
+
+/// Used to put an error message in a HandleError
+#[derive(Clone)]
+pub enum ErrorAction {
+       /// The peer took some action which made us think they were useless. Disconnect them.
+       DisconnectPeer {
+               /// An error message which we should make an effort to send before we disconnect.
+               msg: Option<ErrorMessage>
+       },
+       /// The peer did something harmless that we weren't able to process, just log and ignore
+       IgnoreError,
+       /// The peer did something incorrect. Tell them.
+       SendErrorMessage {
+               /// The message to send.
+               msg: ErrorMessage
+       },
+}
+
+/// An Err type for failure to process messages.
+pub struct HandleError { //TODO: rename me
+       /// A human-readable message describing the error
+       pub err: &'static str,
+       /// The action which should be taken against the offending peer.
+       pub action: Option<ErrorAction>, //TODO: Make this required
+}
+
+/// Struct used to return values from revoke_and_ack messages, containing a bunch of commitment
+/// transaction updates if they were pending.
+#[derive(PartialEq, Clone)]
+pub struct CommitmentUpdate {
+       /// update_add_htlc messages which should be sent
+       pub update_add_htlcs: Vec<UpdateAddHTLC>,
+       /// update_fulfill_htlc messages which should be sent
+       pub update_fulfill_htlcs: Vec<UpdateFulfillHTLC>,
+       /// update_fail_htlc messages which should be sent
+       pub update_fail_htlcs: Vec<UpdateFailHTLC>,
+       /// update_fail_malformed_htlc messages which should be sent
+       pub update_fail_malformed_htlcs: Vec<UpdateFailMalformedHTLC>,
+       /// An update_fee message which should be sent
+       pub update_fee: Option<UpdateFee>,
+       /// Finally, the commitment_signed message which should be sent
+       pub commitment_signed: CommitmentSigned,
+}
+
+/// The information we received from a peer along the route of a payment we originated. This is
+/// returned by ChannelMessageHandler::handle_update_fail_htlc to be passed into
+/// RoutingMessageHandler::handle_htlc_fail_channel_update to update our network map.
+#[derive(Clone)]
+pub enum HTLCFailChannelUpdate {
+       /// We received an error which included a full ChannelUpdate message.
+       ChannelUpdateMessage {
+               /// The unwrapped message we received
+               msg: ChannelUpdate,
+       },
+       /// We received an error which indicated only that a channel has been closed
+       ChannelClosed {
+               /// The short_channel_id which has now closed.
+               short_channel_id: u64,
+               /// when this true, this channel should be permanently removed from the
+               /// consideration. Otherwise, this channel can be restored as new channel_update is received
+               is_permanent: bool,
+       },
+       /// We received an error which indicated only that a node has failed
+       NodeFailure {
+               /// The node_id that has failed.
+               node_id: PublicKey,
+               /// when this true, node should be permanently removed from the
+               /// consideration. Otherwise, the channels connected to this node can be
+               /// restored as new channel_update is received
+               is_permanent: bool,
+       }
+}
+
+/// Messages could have optional fields to use with extended features
+/// As we wish to serialize these differently from Option<T>s (Options get a tag byte, but
+/// OptionalFeild simply gets Present if there are enough bytes to read into it), we have a
+/// separate enum type for them.
+#[derive(Clone, PartialEq)]
+pub enum OptionalField<T> {
+       /// Optional field is included in message
+       Present(T),
+       /// Optional field is absent in message
+       Absent
+}
+
+/// A trait to describe an object which can receive channel messages.
+///
+/// Messages MAY be called in parallel when they originate from different their_node_ids, however
+/// they MUST NOT be called in parallel when the two calls have the same their_node_id.
+pub trait ChannelMessageHandler : events::MessageSendEventsProvider + Send + Sync {
+       //Channel init:
+       /// Handle an incoming open_channel message from the given peer.
+       fn handle_open_channel(&self, their_node_id: &PublicKey, their_local_features: LocalFeatures, msg: &OpenChannel) -> Result<(), HandleError>;
+       /// Handle an incoming accept_channel message from the given peer.
+       fn handle_accept_channel(&self, their_node_id: &PublicKey, their_local_features: LocalFeatures, msg: &AcceptChannel) -> Result<(), HandleError>;
+       /// Handle an incoming funding_created message from the given peer.
+       fn handle_funding_created(&self, their_node_id: &PublicKey, msg: &FundingCreated) -> Result<(), HandleError>;
+       /// Handle an incoming funding_signed message from the given peer.
+       fn handle_funding_signed(&self, their_node_id: &PublicKey, msg: &FundingSigned) -> Result<(), HandleError>;
+       /// Handle an incoming funding_locked message from the given peer.
+       fn handle_funding_locked(&self, their_node_id: &PublicKey, msg: &FundingLocked) -> Result<(), HandleError>;
+
+       // Channl close:
+       /// Handle an incoming shutdown message from the given peer.
+       fn handle_shutdown(&self, their_node_id: &PublicKey, msg: &Shutdown) -> Result<(), HandleError>;
+       /// Handle an incoming closing_signed message from the given peer.
+       fn handle_closing_signed(&self, their_node_id: &PublicKey, msg: &ClosingSigned) -> Result<(), HandleError>;
+
+       // HTLC handling:
+       /// Handle an incoming update_add_htlc message from the given peer.
+       fn handle_update_add_htlc(&self, their_node_id: &PublicKey, msg: &UpdateAddHTLC) -> Result<(), HandleError>;
+       /// Handle an incoming update_fulfill_htlc message from the given peer.
+       fn handle_update_fulfill_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFulfillHTLC) -> Result<(), HandleError>;
+       /// Handle an incoming update_fail_htlc message from the given peer.
+       fn handle_update_fail_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFailHTLC) -> Result<(), HandleError>;
+       /// Handle an incoming update_fail_malformed_htlc message from the given peer.
+       fn handle_update_fail_malformed_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFailMalformedHTLC) -> Result<(), HandleError>;
+       /// Handle an incoming commitment_signed message from the given peer.
+       fn handle_commitment_signed(&self, their_node_id: &PublicKey, msg: &CommitmentSigned) -> Result<(), HandleError>;
+       /// Handle an incoming revoke_and_ack message from the given peer.
+       fn handle_revoke_and_ack(&self, their_node_id: &PublicKey, msg: &RevokeAndACK) -> Result<(), HandleError>;
+
+       /// Handle an incoming update_fee message from the given peer.
+       fn handle_update_fee(&self, their_node_id: &PublicKey, msg: &UpdateFee) -> Result<(), HandleError>;
+
+       // Channel-to-announce:
+       /// Handle an incoming announcement_signatures message from the given peer.
+       fn handle_announcement_signatures(&self, their_node_id: &PublicKey, msg: &AnnouncementSignatures) -> Result<(), HandleError>;
+
+       // Connection loss/reestablish:
+       /// Indicates a connection to the peer failed/an existing connection was lost. If no connection
+       /// is believed to be possible in the future (eg they're sending us messages we don't
+       /// understand or indicate they require unknown feature bits), no_connection_possible is set
+       /// and any outstanding channels should be failed.
+       fn peer_disconnected(&self, their_node_id: &PublicKey, no_connection_possible: bool);
+
+       /// Handle a peer reconnecting, possibly generating channel_reestablish message(s).
+       fn peer_connected(&self, their_node_id: &PublicKey);
+       /// Handle an incoming channel_reestablish message from the given peer.
+       fn handle_channel_reestablish(&self, their_node_id: &PublicKey, msg: &ChannelReestablish) -> Result<(), HandleError>;
+
+       // Error:
+       /// Handle an incoming error message from the given peer.
+       fn handle_error(&self, their_node_id: &PublicKey, msg: &ErrorMessage);
+}
+
+/// A trait to describe an object which can receive routing messages.
+pub trait RoutingMessageHandler : Send + Sync {
+       /// Handle an incoming node_announcement message, returning true if it should be forwarded on,
+       /// false or returning an Err otherwise.
+       fn handle_node_announcement(&self, msg: &NodeAnnouncement) -> Result<bool, HandleError>;
+       /// Handle a channel_announcement message, returning true if it should be forwarded on, false
+       /// or returning an Err otherwise.
+       fn handle_channel_announcement(&self, msg: &ChannelAnnouncement) -> Result<bool, HandleError>;
+       /// Handle an incoming channel_update message, returning true if it should be forwarded on,
+       /// false or returning an Err otherwise.
+       fn handle_channel_update(&self, msg: &ChannelUpdate) -> Result<bool, HandleError>;
+       /// Handle some updates to the route graph that we learned due to an outbound failed payment.
+       fn handle_htlc_fail_channel_update(&self, update: &HTLCFailChannelUpdate);
+       /// Gets a subset of the channel announcements and updates required to dump our routing table
+       /// to a remote node, starting at the short_channel_id indicated by starting_point and
+       /// including batch_amount entries.
+       fn get_next_channel_announcements(&self, starting_point: u64, batch_amount: u8) -> Vec<(ChannelAnnouncement, ChannelUpdate, ChannelUpdate)>;
+       /// Gets a subset of the node announcements required to dump our routing table to a remote node,
+       /// starting at the node *after* the provided publickey and including batch_amount entries.
+       /// If None is provided for starting_point, we start at the first node.
+       fn get_next_node_announcements(&self, starting_point: Option<&PublicKey>, batch_amount: u8) -> Vec<NodeAnnouncement>;
+}
+
+pub(crate) struct OnionRealm0HopData {
+       pub(crate) short_channel_id: u64,
+       pub(crate) amt_to_forward: u64,
+       pub(crate) outgoing_cltv_value: u32,
+       // 12 bytes of 0-padding
+}
+
+mod fuzzy_internal_msgs {
+       // These types aren't intended to be pub, but are exposed for direct fuzzing (as we deserialize
+       // them from untrusted input):
+
+       use super::OnionRealm0HopData;
+       pub struct OnionHopData {
+               pub(crate) realm: u8,
+               pub(crate) data: OnionRealm0HopData,
+               pub(crate) hmac: [u8; 32],
+       }
+
+       pub struct DecodedOnionErrorPacket {
+               pub(crate) hmac: [u8; 32],
+               pub(crate) failuremsg: Vec<u8>,
+               pub(crate) pad: Vec<u8>,
+       }
+}
+#[cfg(feature = "fuzztarget")]
+pub use self::fuzzy_internal_msgs::*;
+#[cfg(not(feature = "fuzztarget"))]
+pub(crate) use self::fuzzy_internal_msgs::*;
+
+#[derive(Clone)]
+pub(crate) struct OnionPacket {
+       pub(crate) version: u8,
+       /// In order to ensure we always return an error on Onion decode in compliance with BOLT 4, we
+       /// have to deserialize OnionPackets contained in UpdateAddHTLCs even if the ephemeral public
+       /// key (here) is bogus, so we hold a Result instead of a PublicKey as we'd like.
+       pub(crate) public_key: Result<PublicKey, secp256k1::Error>,
+       pub(crate) hop_data: [u8; 20*65],
+       pub(crate) hmac: [u8; 32],
+}
+
+impl PartialEq for OnionPacket {
+       fn eq(&self, other: &OnionPacket) -> bool {
+               for (i, j) in self.hop_data.iter().zip(other.hop_data.iter()) {
+                       if i != j { return false; }
+               }
+               self.version == other.version &&
+                       self.public_key == other.public_key &&
+                       self.hmac == other.hmac
+       }
+}
+
+#[derive(Clone, PartialEq)]
+pub(crate) struct OnionErrorPacket {
+       // This really should be a constant size slice, but the spec lets these things be up to 128KB?
+       // (TODO) We limit it in decode to much lower...
+       pub(crate) data: Vec<u8>,
+}
+
+impl Error for DecodeError {
+       fn description(&self) -> &str {
+               match *self {
+                       DecodeError::UnknownVersion => "Unknown realm byte in Onion packet",
+                       DecodeError::UnknownRequiredFeature => "Unknown required feature preventing decode",
+                       DecodeError::InvalidValue => "Nonsense bytes didn't map to the type they were interpreted as",
+                       DecodeError::ShortRead => "Packet extended beyond the provided bytes",
+                       DecodeError::ExtraAddressesPerType => "More than one address of a single type",
+                       DecodeError::BadLengthDescriptor => "A length descriptor in the packet didn't describe the later data correctly",
+                       DecodeError::Io(ref e) => e.description(),
+               }
+       }
+}
+impl fmt::Display for DecodeError {
+       fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+               f.write_str(self.description())
+       }
+}
+
+impl fmt::Debug for HandleError {
+       fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+               f.write_str(self.err)
+       }
+}
+
+impl From<::std::io::Error> for DecodeError {
+       fn from(e: ::std::io::Error) -> Self {
+               if e.kind() == ::std::io::ErrorKind::UnexpectedEof {
+                       DecodeError::ShortRead
+               } else {
+                       DecodeError::Io(e)
+               }
+       }
+}
+
+impl Writeable for OptionalField<Script> {
+       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
+               match *self {
+                       OptionalField::Present(ref script) => {
+                               // Note that Writeable for script includes the 16-bit length tag for us
+                               script.write(w)?;
+                       },
+                       OptionalField::Absent => {}
+               }
+               Ok(())
+       }
+}
+
+impl<R: Read> Readable<R> for OptionalField<Script> {
+       fn read(r: &mut R) -> Result<Self, DecodeError> {
+               match <u16 as Readable<R>>::read(r) {
+                       Ok(len) => {
+                               let mut buf = vec![0; len as usize];
+                               r.read_exact(&mut buf)?;
+                               Ok(OptionalField::Present(Script::from(buf)))
+                       },
+                       Err(DecodeError::ShortRead) => Ok(OptionalField::Absent),
+                       Err(e) => Err(e)
+               }
+       }
+}
+
+impl_writeable_len_match!(AcceptChannel, {
+               {AcceptChannel{ shutdown_scriptpubkey: OptionalField::Present(ref script), .. }, 270 + 2 + script.len()},
+               {_, 270}
+       }, {
+       temporary_channel_id,
+       dust_limit_satoshis,
+       max_htlc_value_in_flight_msat,
+       channel_reserve_satoshis,
+       htlc_minimum_msat,
+       minimum_depth,
+       to_self_delay,
+       max_accepted_htlcs,
+       funding_pubkey,
+       revocation_basepoint,
+       payment_basepoint,
+       delayed_payment_basepoint,
+       htlc_basepoint,
+       first_per_commitment_point,
+       shutdown_scriptpubkey
+});
+
+impl_writeable!(AnnouncementSignatures, 32+8+64*2, {
+       channel_id,
+       short_channel_id,
+       node_signature,
+       bitcoin_signature
+});
+
+impl Writeable for ChannelReestablish {
+       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
+               w.size_hint(if let OptionalField::Present(..) = self.data_loss_protect { 32+2*8+33+32 } else { 32+2*8 });
+               self.channel_id.write(w)?;
+               self.next_local_commitment_number.write(w)?;
+               self.next_remote_commitment_number.write(w)?;
+               match self.data_loss_protect {
+                       OptionalField::Present(ref data_loss_protect) => {
+                               (*data_loss_protect).your_last_per_commitment_secret.write(w)?;
+                               (*data_loss_protect).my_current_per_commitment_point.write(w)?;
+                       },
+                       OptionalField::Absent => {}
+               }
+               Ok(())
+       }
+}
+
+impl<R: Read> Readable<R> for ChannelReestablish{
+       fn read(r: &mut R) -> Result<Self, DecodeError> {
+               Ok(Self {
+                       channel_id: Readable::read(r)?,
+                       next_local_commitment_number: Readable::read(r)?,
+                       next_remote_commitment_number: Readable::read(r)?,
+                       data_loss_protect: {
+                               match <[u8; 32] as Readable<R>>::read(r) {
+                                       Ok(your_last_per_commitment_secret) =>
+                                               OptionalField::Present(DataLossProtect {
+                                                       your_last_per_commitment_secret,
+                                                       my_current_per_commitment_point: Readable::read(r)?,
+                                               }),
+                                       Err(DecodeError::ShortRead) => OptionalField::Absent,
+                                       Err(e) => return Err(e)
+                               }
+                       }
+               })
+       }
+}
+
+impl_writeable!(ClosingSigned, 32+8+64, {
+       channel_id,
+       fee_satoshis,
+       signature
+});
+
+impl_writeable_len_match!(CommitmentSigned, {
+               { CommitmentSigned { ref htlc_signatures, .. }, 32+64+2+htlc_signatures.len()*64 }
+       }, {
+       channel_id,
+       signature,
+       htlc_signatures
+});
+
+impl_writeable_len_match!(DecodedOnionErrorPacket, {
+               { DecodedOnionErrorPacket { ref failuremsg, ref pad, .. }, 32 + 4 + failuremsg.len() + pad.len() }
+       }, {
+       hmac,
+       failuremsg,
+       pad
+});
+
+impl_writeable!(FundingCreated, 32+32+2+64, {
+       temporary_channel_id,
+       funding_txid,
+       funding_output_index,
+       signature
+});
+
+impl_writeable!(FundingSigned, 32+64, {
+       channel_id,
+       signature
+});
+
+impl_writeable!(FundingLocked, 32+33, {
+       channel_id,
+       next_per_commitment_point
+});
+
+impl_writeable_len_match!(GlobalFeatures, {
+               { GlobalFeatures { ref flags }, flags.len() + 2 }
+       }, {
+       flags
+});
+
+impl_writeable_len_match!(LocalFeatures, {
+               { LocalFeatures { ref flags }, flags.len() + 2 }
+       }, {
+       flags
+});
+
+impl_writeable_len_match!(Init, {
+               { Init { ref global_features, ref local_features }, global_features.flags.len() + local_features.flags.len() + 4 }
+       }, {
+       global_features,
+       local_features
+});
+
+impl_writeable_len_match!(OpenChannel, {
+               { OpenChannel { shutdown_scriptpubkey: OptionalField::Present(ref script), .. }, 319 + 2 + script.len() },
+               { _, 319 }
+       }, {
+       chain_hash,
+       temporary_channel_id,
+       funding_satoshis,
+       push_msat,
+       dust_limit_satoshis,
+       max_htlc_value_in_flight_msat,
+       channel_reserve_satoshis,
+       htlc_minimum_msat,
+       feerate_per_kw,
+       to_self_delay,
+       max_accepted_htlcs,
+       funding_pubkey,
+       revocation_basepoint,
+       payment_basepoint,
+       delayed_payment_basepoint,
+       htlc_basepoint,
+       first_per_commitment_point,
+       channel_flags,
+       shutdown_scriptpubkey
+});
+
+impl_writeable!(RevokeAndACK, 32+32+33, {
+       channel_id,
+       per_commitment_secret,
+       next_per_commitment_point
+});
+
+impl_writeable_len_match!(Shutdown, {
+               { Shutdown { ref scriptpubkey, .. }, 32 + 2 + scriptpubkey.len() }
+       }, {
+       channel_id,
+       scriptpubkey
+});
+
+impl_writeable_len_match!(UpdateFailHTLC, {
+               { UpdateFailHTLC { ref reason, .. }, 32 + 10 + reason.data.len() }
+       }, {
+       channel_id,
+       htlc_id,
+       reason
+});
+
+impl_writeable!(UpdateFailMalformedHTLC, 32+8+32+2, {
+       channel_id,
+       htlc_id,
+       sha256_of_onion,
+       failure_code
+});
+
+impl_writeable!(UpdateFee, 32+4, {
+       channel_id,
+       feerate_per_kw
+});
+
+impl_writeable!(UpdateFulfillHTLC, 32+8+32, {
+       channel_id,
+       htlc_id,
+       payment_preimage
+});
+
+impl_writeable_len_match!(OnionErrorPacket, {
+               { OnionErrorPacket { ref data, .. }, 2 + data.len() }
+       }, {
+       data
+});
+
+impl Writeable for OnionPacket {
+       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
+               w.size_hint(1 + 33 + 20*65 + 32);
+               self.version.write(w)?;
+               match self.public_key {
+                       Ok(pubkey) => pubkey.write(w)?,
+                       Err(_) => [0u8;33].write(w)?,
+               }
+               w.write_all(&self.hop_data)?;
+               self.hmac.write(w)?;
+               Ok(())
+       }
+}
+
+impl<R: Read> Readable<R> for OnionPacket {
+       fn read(r: &mut R) -> Result<Self, DecodeError> {
+               Ok(OnionPacket {
+                       version: Readable::read(r)?,
+                       public_key: {
+                               let mut buf = [0u8;33];
+                               r.read_exact(&mut buf)?;
+                               PublicKey::from_slice(&buf)
+                       },
+                       hop_data: Readable::read(r)?,
+                       hmac: Readable::read(r)?,
+               })
+       }
+}
+
+impl_writeable!(UpdateAddHTLC, 32+8+8+32+4+1366, {
+       channel_id,
+       htlc_id,
+       amount_msat,
+       payment_hash,
+       cltv_expiry,
+       onion_routing_packet
+});
+
+impl Writeable for OnionRealm0HopData {
+       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
+               w.size_hint(32);
+               self.short_channel_id.write(w)?;
+               self.amt_to_forward.write(w)?;
+               self.outgoing_cltv_value.write(w)?;
+               w.write_all(&[0;12])?;
+               Ok(())
+       }
+}
+
+impl<R: Read> Readable<R> for OnionRealm0HopData {
+       fn read(r: &mut R) -> Result<Self, DecodeError> {
+               Ok(OnionRealm0HopData {
+                       short_channel_id: Readable::read(r)?,
+                       amt_to_forward: Readable::read(r)?,
+                       outgoing_cltv_value: {
+                               let v: u32 = Readable::read(r)?;
+                               r.read_exact(&mut [0; 12])?;
+                               v
+                       }
+               })
+       }
+}
+
+impl Writeable for OnionHopData {
+       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
+               w.size_hint(65);
+               self.realm.write(w)?;
+               self.data.write(w)?;
+               self.hmac.write(w)?;
+               Ok(())
+       }
+}
+
+impl<R: Read> Readable<R> for OnionHopData {
+       fn read(r: &mut R) -> Result<Self, DecodeError> {
+               Ok(OnionHopData {
+                       realm: {
+                               let r: u8 = Readable::read(r)?;
+                               if r != 0 {
+                                       return Err(DecodeError::UnknownVersion);
+                               }
+                               r
+                       },
+                       data: Readable::read(r)?,
+                       hmac: Readable::read(r)?,
+               })
+       }
+}
+
+impl Writeable for Ping {
+       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
+               w.size_hint(self.byteslen as usize + 4);
+               self.ponglen.write(w)?;
+               vec![0u8; self.byteslen as usize].write(w)?; // size-unchecked write
+               Ok(())
+       }
+}
+
+impl<R: Read> Readable<R> for Ping {
+       fn read(r: &mut R) -> Result<Self, DecodeError> {
+               Ok(Ping {
+                       ponglen: Readable::read(r)?,
+                       byteslen: {
+                               let byteslen = Readable::read(r)?;
+                               r.read_exact(&mut vec![0u8; byteslen as usize][..])?;
+                               byteslen
+                       }
+               })
+       }
+}
+
+impl Writeable for Pong {
+       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
+               w.size_hint(self.byteslen as usize + 2);
+               vec![0u8; self.byteslen as usize].write(w)?; // size-unchecked write
+               Ok(())
+       }
+}
+
+impl<R: Read> Readable<R> for Pong {
+       fn read(r: &mut R) -> Result<Self, DecodeError> {
+               Ok(Pong {
+                       byteslen: {
+                               let byteslen = Readable::read(r)?;
+                               r.read_exact(&mut vec![0u8; byteslen as usize][..])?;
+                               byteslen
+                       }
+               })
+       }
+}
+
+impl Writeable for UnsignedChannelAnnouncement {
+       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
+               w.size_hint(2 + 2*32 + 4*33 + self.features.flags.len() + self.excess_data.len());
+               self.features.write(w)?;
+               self.chain_hash.write(w)?;
+               self.short_channel_id.write(w)?;
+               self.node_id_1.write(w)?;
+               self.node_id_2.write(w)?;
+               self.bitcoin_key_1.write(w)?;
+               self.bitcoin_key_2.write(w)?;
+               w.write_all(&self.excess_data[..])?;
+               Ok(())
+       }
+}
+
+impl<R: Read> Readable<R> for UnsignedChannelAnnouncement {
+       fn read(r: &mut R) -> Result<Self, DecodeError> {
+               Ok(Self {
+                       features: {
+                               let f: GlobalFeatures = Readable::read(r)?;
+                               if f.requires_unknown_bits() {
+                                       return Err(DecodeError::UnknownRequiredFeature);
+                               }
+                               f
+                       },
+                       chain_hash: Readable::read(r)?,
+                       short_channel_id: Readable::read(r)?,
+                       node_id_1: Readable::read(r)?,
+                       node_id_2: Readable::read(r)?,
+                       bitcoin_key_1: Readable::read(r)?,
+                       bitcoin_key_2: Readable::read(r)?,
+                       excess_data: {
+                               let mut excess_data = vec![];
+                               r.read_to_end(&mut excess_data)?;
+                               excess_data
+                       },
+               })
+       }
+}
+
+impl_writeable_len_match!(ChannelAnnouncement, {
+               { ChannelAnnouncement { contents: UnsignedChannelAnnouncement {ref features, ref excess_data, ..}, .. },
+                       2 + 2*32 + 4*33 + features.flags.len() + excess_data.len() + 4*64 }
+       }, {
+       node_signature_1,
+       node_signature_2,
+       bitcoin_signature_1,
+       bitcoin_signature_2,
+       contents
+});
+
+impl Writeable for UnsignedChannelUpdate {
+       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
+               w.size_hint(64 + self.excess_data.len());
+               self.chain_hash.write(w)?;
+               self.short_channel_id.write(w)?;
+               self.timestamp.write(w)?;
+               self.flags.write(w)?;
+               self.cltv_expiry_delta.write(w)?;
+               self.htlc_minimum_msat.write(w)?;
+               self.fee_base_msat.write(w)?;
+               self.fee_proportional_millionths.write(w)?;
+               w.write_all(&self.excess_data[..])?;
+               Ok(())
+       }
+}
+
+impl<R: Read> Readable<R> for UnsignedChannelUpdate {
+       fn read(r: &mut R) -> Result<Self, DecodeError> {
+               Ok(Self {
+                       chain_hash: Readable::read(r)?,
+                       short_channel_id: Readable::read(r)?,
+                       timestamp: Readable::read(r)?,
+                       flags: Readable::read(r)?,
+                       cltv_expiry_delta: Readable::read(r)?,
+                       htlc_minimum_msat: Readable::read(r)?,
+                       fee_base_msat: Readable::read(r)?,
+                       fee_proportional_millionths: Readable::read(r)?,
+                       excess_data: {
+                               let mut excess_data = vec![];
+                               r.read_to_end(&mut excess_data)?;
+                               excess_data
+                       },
+               })
+       }
+}
+
+impl_writeable_len_match!(ChannelUpdate, {
+               { ChannelUpdate { contents: UnsignedChannelUpdate {ref excess_data, ..}, .. },
+                       64 + excess_data.len() + 64 }
+       }, {
+       signature,
+       contents
+});
+
+impl Writeable for ErrorMessage {
+       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
+               w.size_hint(32 + 2 + self.data.len());
+               self.channel_id.write(w)?;
+               (self.data.len() as u16).write(w)?;
+               w.write_all(self.data.as_bytes())?;
+               Ok(())
+       }
+}
+
+impl<R: Read> Readable<R> for ErrorMessage {
+       fn read(r: &mut R) -> Result<Self, DecodeError> {
+               Ok(Self {
+                       channel_id: Readable::read(r)?,
+                       data: {
+                               let mut sz: usize = <u16 as Readable<R>>::read(r)? as usize;
+                               let mut data = vec![];
+                               let data_len = r.read_to_end(&mut data)?;
+                               sz = cmp::min(data_len, sz);
+                               match String::from_utf8(data[..sz as usize].to_vec()) {
+                                       Ok(s) => s,
+                                       Err(_) => return Err(DecodeError::InvalidValue),
+                               }
+                       }
+               })
+       }
+}
+
+impl Writeable for UnsignedNodeAnnouncement {
+       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
+               w.size_hint(64 + 76 + self.features.flags.len() + self.addresses.len()*38 + self.excess_address_data.len() + self.excess_data.len());
+               self.features.write(w)?;
+               self.timestamp.write(w)?;
+               self.node_id.write(w)?;
+               w.write_all(&self.rgb)?;
+               self.alias.write(w)?;
+
+               let mut addrs_to_encode = self.addresses.clone();
+               addrs_to_encode.sort_unstable_by(|a, b| { a.get_id().cmp(&b.get_id()) });
+               addrs_to_encode.dedup_by(|a, b| { a.get_id() == b.get_id() });
+               let mut addr_len = 0;
+               for addr in &addrs_to_encode {
+                       addr_len += 1 + addr.len();
+               }
+               (addr_len + self.excess_address_data.len() as u16).write(w)?;
+               for addr in addrs_to_encode {
+                       addr.write(w)?;
+               }
+               w.write_all(&self.excess_address_data[..])?;
+               w.write_all(&self.excess_data[..])?;
+               Ok(())
+       }
+}
+
+impl<R: Read> Readable<R> for UnsignedNodeAnnouncement {
+       fn read(r: &mut R) -> Result<Self, DecodeError> {
+               let features: GlobalFeatures = Readable::read(r)?;
+               if features.requires_unknown_bits() {
+                       return Err(DecodeError::UnknownRequiredFeature);
+               }
+               let timestamp: u32 = Readable::read(r)?;
+               let node_id: PublicKey = Readable::read(r)?;
+               let mut rgb = [0; 3];
+               r.read_exact(&mut rgb)?;
+               let alias: [u8; 32] = Readable::read(r)?;
+
+               let addr_len: u16 = Readable::read(r)?;
+               let mut addresses: Vec<NetAddress> = Vec::with_capacity(4);
+               let mut addr_readpos = 0;
+               let mut excess = false;
+               let mut excess_byte = 0;
+               loop {
+                       if addr_len <= addr_readpos { break; }
+                       match Readable::read(r) {
+                               Ok(Ok(addr)) => {
+                                       match addr {
+                                               NetAddress::IPv4 { .. } => {
+                                                       if addresses.len() > 0 {
+                                                               return Err(DecodeError::ExtraAddressesPerType);
+                                                       }
+                                               },
+                                               NetAddress::IPv6 { .. } => {
+                                                       if addresses.len() > 1 || (addresses.len() == 1 && addresses[0].get_id() != 1) {
+                                                               return Err(DecodeError::ExtraAddressesPerType);
+                                                       }
+                                               },
+                                               NetAddress::OnionV2 { .. } => {
+                                                       if addresses.len() > 2 || (addresses.len() > 0 && addresses.last().unwrap().get_id() > 2) {
+                                                               return Err(DecodeError::ExtraAddressesPerType);
+                                                       }
+                                               },
+                                               NetAddress::OnionV3 { .. } => {
+                                                       if addresses.len() > 3 || (addresses.len() > 0 && addresses.last().unwrap().get_id() > 3) {
+                                                               return Err(DecodeError::ExtraAddressesPerType);
+                                                       }
+                                               },
+                                       }
+                                       if addr_len < addr_readpos + 1 + addr.len() {
+                                               return Err(DecodeError::BadLengthDescriptor);
+                                       }
+                                       addr_readpos += (1 + addr.len()) as u16;
+                                       addresses.push(addr);
+                               },
+                               Ok(Err(unknown_descriptor)) => {
+                                       excess = true;
+                                       excess_byte = unknown_descriptor;
+                                       break;
+                               },
+                               Err(DecodeError::ShortRead) => return Err(DecodeError::BadLengthDescriptor),
+                               Err(e) => return Err(e),
+                       }
+               }
+
+               let mut excess_data = vec![];
+               let excess_address_data = if addr_readpos < addr_len {
+                       let mut excess_address_data = vec![0; (addr_len - addr_readpos) as usize];
+                       r.read_exact(&mut excess_address_data[if excess { 1 } else { 0 }..])?;
+                       if excess {
+                               excess_address_data[0] = excess_byte;
+                       }
+                       excess_address_data
+               } else {
+                       if excess {
+                               excess_data.push(excess_byte);
+                       }
+                       Vec::new()
+               };
+               r.read_to_end(&mut excess_data)?;
+               Ok(UnsignedNodeAnnouncement {
+                       features,
+                       timestamp,
+                       node_id,
+                       rgb,
+                       alias,
+                       addresses,
+                       excess_address_data,
+                       excess_data,
+               })
+       }
+}
+
+impl_writeable_len_match!(NodeAnnouncement, {
+               { NodeAnnouncement { contents: UnsignedNodeAnnouncement { ref features, ref addresses, ref excess_address_data, ref excess_data, ..}, .. },
+                       64 + 76 + features.flags.len() + addresses.len()*38 + excess_address_data.len() + excess_data.len() }
+       }, {
+       signature,
+       contents
+});
+
+#[cfg(test)]
+mod tests {
+       use hex;
+       use ln::msgs;
+       use ln::msgs::{GlobalFeatures, LocalFeatures, OptionalField, OnionErrorPacket};
+       use ln::channelmanager::{PaymentPreimage, PaymentHash};
+       use util::ser::Writeable;
+
+       use bitcoin_hashes::sha256d::Hash as Sha256dHash;
+       use bitcoin_hashes::hex::FromHex;
+       use bitcoin::util::address::Address;
+       use bitcoin::network::constants::Network;
+       use bitcoin::blockdata::script::Builder;
+       use bitcoin::blockdata::opcodes;
+
+       use secp256k1::key::{PublicKey,SecretKey};
+       use secp256k1::{Secp256k1, Message};
+
+       #[test]
+       fn encoding_channel_reestablish_no_secret() {
+               let cr = msgs::ChannelReestablish {
+                       channel_id: [4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0],
+                       next_local_commitment_number: 3,
+                       next_remote_commitment_number: 4,
+                       data_loss_protect: OptionalField::Absent,
+               };
+
+               let encoded_value = cr.encode();
+               assert_eq!(
+                       encoded_value,
+                       vec![4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4]
+               );
+       }
+
+       #[test]
+       fn encoding_channel_reestablish_with_secret() {
+               let public_key = {
+                       let secp_ctx = Secp256k1::new();
+                       PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap())
+               };
+
+               let cr = msgs::ChannelReestablish {
+                       channel_id: [4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0],
+                       next_local_commitment_number: 3,
+                       next_remote_commitment_number: 4,
+                       data_loss_protect: OptionalField::Present(msgs::DataLossProtect { your_last_per_commitment_secret: [9;32], my_current_per_commitment_point: public_key}),
+               };
+
+               let encoded_value = cr.encode();
+               assert_eq!(
+                       encoded_value,
+                       vec![4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 3, 27, 132, 197, 86, 123, 18, 100, 64, 153, 93, 62, 213, 170, 186, 5, 101, 215, 30, 24, 52, 96, 72, 25, 255, 156, 23, 245, 233, 213, 221, 7, 143]
+               );
+       }
+
+       macro_rules! get_keys_from {
+               ($slice: expr, $secp_ctx: expr) => {
+                       {
+                               let privkey = SecretKey::from_slice(&hex::decode($slice).unwrap()[..]).unwrap();
+                               let pubkey = PublicKey::from_secret_key(&$secp_ctx, &privkey);
+                               (privkey, pubkey)
+                       }
+               }
+       }
+
+       macro_rules! get_sig_on {
+               ($privkey: expr, $ctx: expr, $string: expr) => {
+                       {
+                               let sighash = Message::from_slice(&$string.into_bytes()[..]).unwrap();
+                               $ctx.sign(&sighash, &$privkey)
+                       }
+               }
+       }
+
+       #[test]
+       fn encoding_announcement_signatures() {
+               let secp_ctx = Secp256k1::new();
+               let (privkey, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
+               let sig_1 = get_sig_on!(privkey, secp_ctx, String::from("01010101010101010101010101010101"));
+               let sig_2 = get_sig_on!(privkey, secp_ctx, String::from("02020202020202020202020202020202"));
+               let announcement_signatures = msgs::AnnouncementSignatures {
+                       channel_id: [4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0],
+                       short_channel_id: 2316138423780173,
+                       node_signature: sig_1,
+                       bitcoin_signature: sig_2,
+               };
+
+               let encoded_value = announcement_signatures.encode();
+               assert_eq!(encoded_value, hex::decode("040000000000000005000000000000000600000000000000070000000000000000083a840000034dd977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073acf9953cef4700860f5967838eba2bae89288ad188ebf8b20bf995c3ea53a26df1876d0a3a0e13172ba286a673140190c02ba9da60a2e43a745188c8a83c7f3ef").unwrap());
+       }
+
+       fn do_encoding_channel_announcement(unknown_features_bits: bool, non_bitcoin_chain_hash: bool, excess_data: bool) {
+               let secp_ctx = Secp256k1::new();
+               let (privkey_1, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
+               let (privkey_2, pubkey_2) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202", secp_ctx);
+               let (privkey_3, pubkey_3) = get_keys_from!("0303030303030303030303030303030303030303030303030303030303030303", secp_ctx);
+               let (privkey_4, pubkey_4) = get_keys_from!("0404040404040404040404040404040404040404040404040404040404040404", secp_ctx);
+               let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
+               let sig_2 = get_sig_on!(privkey_2, secp_ctx, String::from("01010101010101010101010101010101"));
+               let sig_3 = get_sig_on!(privkey_3, secp_ctx, String::from("01010101010101010101010101010101"));
+               let sig_4 = get_sig_on!(privkey_4, secp_ctx, String::from("01010101010101010101010101010101"));
+               let mut features = GlobalFeatures::new();
+               if unknown_features_bits {
+                       features.flags = vec![0xFF, 0xFF];
+               }
+               let unsigned_channel_announcement = msgs::UnsignedChannelAnnouncement {
+                       features,
+                       chain_hash: if !non_bitcoin_chain_hash { Sha256dHash::from_hex("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap() } else { Sha256dHash::from_hex("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943").unwrap() },
+                       short_channel_id: 2316138423780173,
+                       node_id_1: pubkey_1,
+                       node_id_2: pubkey_2,
+                       bitcoin_key_1: pubkey_3,
+                       bitcoin_key_2: pubkey_4,
+                       excess_data: if excess_data { vec![10, 0, 0, 20, 0, 0, 30, 0, 0, 40] } else { Vec::new() },
+               };
+               let channel_announcement = msgs::ChannelAnnouncement {
+                       node_signature_1: sig_1,
+                       node_signature_2: sig_2,
+                       bitcoin_signature_1: sig_3,
+                       bitcoin_signature_2: sig_4,
+                       contents: unsigned_channel_announcement,
+               };
+               let encoded_value = channel_announcement.encode();
+               let mut target_value = hex::decode("d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a1735b6a427e80d5fe7cd90a2f4ee08dc9c27cda7c35a4172e5d85b12c49d4232537e98f9b1f3c5e6989a8b9644e90e8918127680dbd0d4043510840fc0f1e11a216c280b5395a2546e7e4b2663e04f811622f15a4f91e83aa2e92ba2a573c139142c54ae63072a1ec1ee7dc0c04bde5c847806172aa05c92c22ae8e308d1d2692b12cc195ce0a2d1bda6a88befa19fa07f51caa75ce83837f28965600b8aacab0855ffb0e741ec5f7c41421e9829a9d48611c8c831f71be5ea73e66594977ffd").unwrap();
+               if unknown_features_bits {
+                       target_value.append(&mut hex::decode("0002ffff").unwrap());
+               } else {
+                       target_value.append(&mut hex::decode("0000").unwrap());
+               }
+               if non_bitcoin_chain_hash {
+                       target_value.append(&mut hex::decode("43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea330900000000").unwrap());
+               } else {
+                       target_value.append(&mut hex::decode("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f").unwrap());
+               }
+               target_value.append(&mut hex::decode("00083a840000034d031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f024d4b6cd1361032ca9bd2aeb9d900aa4d45d9ead80ac9423374c451a7254d076602531fe6068134503d2723133227c867ac8fa6c83c537e9a44c3c5bdbdcb1fe33703462779ad4aad39514614751a71085f2f10e1c7a593e4e030efb5b8721ce55b0b").unwrap());
+               if excess_data {
+                       target_value.append(&mut hex::decode("0a00001400001e000028").unwrap());
+               }
+               assert_eq!(encoded_value, target_value);
+       }
+
+       #[test]
+       fn encoding_channel_announcement() {
+               do_encoding_channel_announcement(false, false, false);
+               do_encoding_channel_announcement(true, false, false);
+               do_encoding_channel_announcement(true, true, false);
+               do_encoding_channel_announcement(true, true, true);
+               do_encoding_channel_announcement(false, true, true);
+               do_encoding_channel_announcement(false, false, true);
+               do_encoding_channel_announcement(false, true, false);
+               do_encoding_channel_announcement(true, false, true);
+       }
+
+       fn do_encoding_node_announcement(unknown_features_bits: bool, ipv4: bool, ipv6: bool, onionv2: bool, onionv3: bool, excess_address_data: bool, excess_data: bool) {
+               let secp_ctx = Secp256k1::new();
+               let (privkey_1, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
+               let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
+               let mut features = GlobalFeatures::new();
+               if unknown_features_bits {
+                       features.flags = vec![0xFF, 0xFF];
+               }
+               let mut addresses = Vec::new();
+               if ipv4 {
+                       addresses.push(msgs::NetAddress::IPv4 {
+                               addr: [255, 254, 253, 252],
+                               port: 9735
+                       });
+               }
+               if ipv6 {
+                       addresses.push(msgs::NetAddress::IPv6 {
+                               addr: [255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 245, 244, 243, 242, 241, 240],
+                               port: 9735
+                       });
+               }
+               if onionv2 {
+                       addresses.push(msgs::NetAddress::OnionV2 {
+                               addr: [255, 254, 253, 252, 251, 250, 249, 248, 247, 246],
+                               port: 9735
+                       });
+               }
+               if onionv3 {
+                       addresses.push(msgs::NetAddress::OnionV3 {
+                               ed25519_pubkey: [255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 245, 244, 243, 242, 241, 240, 239, 238, 237, 236, 235, 234, 233, 232, 231, 230, 229, 228, 227, 226, 225, 224],
+                               checksum: 32,
+                               version: 16,
+                               port: 9735
+                       });
+               }
+               let mut addr_len = 0;
+               for addr in &addresses {
+                       addr_len += addr.len() + 1;
+               }
+               let unsigned_node_announcement = msgs::UnsignedNodeAnnouncement {
+                       features,
+                       timestamp: 20190119,
+                       node_id: pubkey_1,
+                       rgb: [32; 3],
+                       alias: [16;32],
+                       addresses,
+                       excess_address_data: if excess_address_data { vec![33, 108, 40, 11, 83, 149, 162, 84, 110, 126, 75, 38, 99, 224, 79, 129, 22, 34, 241, 90, 79, 146, 232, 58, 162, 233, 43, 162, 165, 115, 193, 57, 20, 44, 84, 174, 99, 7, 42, 30, 193, 238, 125, 192, 192, 75, 222, 92, 132, 120, 6, 23, 42, 160, 92, 146, 194, 42, 232, 227, 8, 209, 210, 105] } else { Vec::new() },
+                       excess_data: if excess_data { vec![59, 18, 204, 25, 92, 224, 162, 209, 189, 166, 168, 139, 239, 161, 159, 160, 127, 81, 202, 167, 92, 232, 56, 55, 242, 137, 101, 96, 11, 138, 172, 171, 8, 85, 255, 176, 231, 65, 236, 95, 124, 65, 66, 30, 152, 41, 169, 212, 134, 17, 200, 200, 49, 247, 27, 229, 234, 115, 230, 101, 148, 151, 127, 253] } else { Vec::new() },
+               };
+               addr_len += unsigned_node_announcement.excess_address_data.len() as u16;
+               let node_announcement = msgs::NodeAnnouncement {
+                       signature: sig_1,
+                       contents: unsigned_node_announcement,
+               };
+               let encoded_value = node_announcement.encode();
+               let mut target_value = hex::decode("d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
+               if unknown_features_bits {
+                       target_value.append(&mut hex::decode("0002ffff").unwrap());
+               } else {
+                       target_value.append(&mut hex::decode("0000").unwrap());
+               }
+               target_value.append(&mut hex::decode("013413a7031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f2020201010101010101010101010101010101010101010101010101010101010101010").unwrap());
+               target_value.append(&mut vec![(addr_len >> 8) as u8, addr_len as u8]);
+               if ipv4 {
+                       target_value.append(&mut hex::decode("01fffefdfc2607").unwrap());
+               }
+               if ipv6 {
+                       target_value.append(&mut hex::decode("02fffefdfcfbfaf9f8f7f6f5f4f3f2f1f02607").unwrap());
+               }
+               if onionv2 {
+                       target_value.append(&mut hex::decode("03fffefdfcfbfaf9f8f7f62607").unwrap());
+               }
+               if onionv3 {
+                       target_value.append(&mut hex::decode("04fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0efeeedecebeae9e8e7e6e5e4e3e2e1e00020102607").unwrap());
+               }
+               if excess_address_data {
+                       target_value.append(&mut hex::decode("216c280b5395a2546e7e4b2663e04f811622f15a4f92e83aa2e92ba2a573c139142c54ae63072a1ec1ee7dc0c04bde5c847806172aa05c92c22ae8e308d1d269").unwrap());
+               }
+               if excess_data {
+                       target_value.append(&mut hex::decode("3b12cc195ce0a2d1bda6a88befa19fa07f51caa75ce83837f28965600b8aacab0855ffb0e741ec5f7c41421e9829a9d48611c8c831f71be5ea73e66594977ffd").unwrap());
+               }
+               assert_eq!(encoded_value, target_value);
+       }
+
+       #[test]
+       fn encoding_node_announcement() {
+               do_encoding_node_announcement(true, true, true, true, true, true, true);
+               do_encoding_node_announcement(false, false, false, false, false, false, false);
+               do_encoding_node_announcement(false, true, false, false, false, false, false);
+               do_encoding_node_announcement(false, false, true, false, false, false, false);
+               do_encoding_node_announcement(false, false, false, true, false, false, false);
+               do_encoding_node_announcement(false, false, false, false, true, false, false);
+               do_encoding_node_announcement(false, false, false, false, false, true, false);
+               do_encoding_node_announcement(false, true, false, true, false, true, false);
+               do_encoding_node_announcement(false, false, true, false, true, false, false);
+       }
+
+       fn do_encoding_channel_update(non_bitcoin_chain_hash: bool, direction: bool, disable: bool, htlc_maximum_msat: bool) {
+               let secp_ctx = Secp256k1::new();
+               let (privkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
+               let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
+               let unsigned_channel_update = msgs::UnsignedChannelUpdate {
+                       chain_hash: if !non_bitcoin_chain_hash { Sha256dHash::from_hex("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap() } else { Sha256dHash::from_hex("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943").unwrap() },
+                       short_channel_id: 2316138423780173,
+                       timestamp: 20190119,
+                       flags: if direction { 1 } else { 0 } | if disable { 1 << 1 } else { 0 } | if htlc_maximum_msat { 1 << 8 } else { 0 },
+                       cltv_expiry_delta: 144,
+                       htlc_minimum_msat: 1000000,
+                       fee_base_msat: 10000,
+                       fee_proportional_millionths: 20,
+                       excess_data: if htlc_maximum_msat { vec![0, 0, 0, 0, 59, 154, 202, 0] } else { Vec::new() }
+               };
+               let channel_update = msgs::ChannelUpdate {
+                       signature: sig_1,
+                       contents: unsigned_channel_update
+               };
+               let encoded_value = channel_update.encode();
+               let mut target_value = hex::decode("d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
+               if non_bitcoin_chain_hash {
+                       target_value.append(&mut hex::decode("43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea330900000000").unwrap());
+               } else {
+                       target_value.append(&mut hex::decode("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f").unwrap());
+               }
+               target_value.append(&mut hex::decode("00083a840000034d013413a7").unwrap());
+               if htlc_maximum_msat {
+                       target_value.append(&mut hex::decode("01").unwrap());
+               } else {
+                       target_value.append(&mut hex::decode("00").unwrap());
+               }
+               target_value.append(&mut hex::decode("00").unwrap());
+               if direction {
+                       let flag = target_value.last_mut().unwrap();
+                       *flag = 1;
+               }
+               if disable {
+                       let flag = target_value.last_mut().unwrap();
+                       *flag = *flag | 1 << 1;
+               }
+               target_value.append(&mut hex::decode("009000000000000f42400000271000000014").unwrap());
+               if htlc_maximum_msat {
+                       target_value.append(&mut hex::decode("000000003b9aca00").unwrap());
+               }
+               assert_eq!(encoded_value, target_value);
+       }
+
+       #[test]
+       fn encoding_channel_update() {
+               do_encoding_channel_update(false, false, false, false);
+               do_encoding_channel_update(true, false, false, false);
+               do_encoding_channel_update(false, true, false, false);
+               do_encoding_channel_update(false, false, true, false);
+               do_encoding_channel_update(false, false, false, true);
+               do_encoding_channel_update(true, true, true, true);
+       }
+
+       fn do_encoding_open_channel(non_bitcoin_chain_hash: bool, random_bit: bool, shutdown: bool) {
+               let secp_ctx = Secp256k1::new();
+               let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
+               let (_, pubkey_2) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202", secp_ctx);
+               let (_, pubkey_3) = get_keys_from!("0303030303030303030303030303030303030303030303030303030303030303", secp_ctx);
+               let (_, pubkey_4) = get_keys_from!("0404040404040404040404040404040404040404040404040404040404040404", secp_ctx);
+               let (_, pubkey_5) = get_keys_from!("0505050505050505050505050505050505050505050505050505050505050505", secp_ctx);
+               let (_, pubkey_6) = get_keys_from!("0606060606060606060606060606060606060606060606060606060606060606", secp_ctx);
+               let open_channel = msgs::OpenChannel {
+                       chain_hash: if !non_bitcoin_chain_hash { Sha256dHash::from_hex("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap() } else { Sha256dHash::from_hex("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943").unwrap() },
+                       temporary_channel_id: [2; 32],
+                       funding_satoshis: 1311768467284833366,
+                       push_msat: 2536655962884945560,
+                       dust_limit_satoshis: 3608586615801332854,
+                       max_htlc_value_in_flight_msat: 8517154655701053848,
+                       channel_reserve_satoshis: 8665828695742877976,
+                       htlc_minimum_msat: 2316138423780173,
+                       feerate_per_kw: 821716,
+                       to_self_delay: 49340,
+                       max_accepted_htlcs: 49340,
+                       funding_pubkey: pubkey_1,
+                       revocation_basepoint: pubkey_2,
+                       payment_basepoint: pubkey_3,
+                       delayed_payment_basepoint: pubkey_4,
+                       htlc_basepoint: pubkey_5,
+                       first_per_commitment_point: pubkey_6,
+                       channel_flags: if random_bit { 1 << 5 } else { 0 },
+                       shutdown_scriptpubkey: if shutdown { OptionalField::Present(Address::p2pkh(&::bitcoin::PublicKey{compressed: true, key: pubkey_1}, Network::Testnet).script_pubkey()) } else { OptionalField::Absent }
+               };
+               let encoded_value = open_channel.encode();
+               let mut target_value = Vec::new();
+               if non_bitcoin_chain_hash {
+                       target_value.append(&mut hex::decode("43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea330900000000").unwrap());
+               } else {
+                       target_value.append(&mut hex::decode("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f").unwrap());
+               }
+               target_value.append(&mut hex::decode("02020202020202020202020202020202020202020202020202020202020202021234567890123456233403289122369832144668701144767633030896203198784335490624111800083a840000034d000c89d4c0bcc0bc031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f024d4b6cd1361032ca9bd2aeb9d900aa4d45d9ead80ac9423374c451a7254d076602531fe6068134503d2723133227c867ac8fa6c83c537e9a44c3c5bdbdcb1fe33703462779ad4aad39514614751a71085f2f10e1c7a593e4e030efb5b8721ce55b0b0362c0a046dacce86ddd0343c6d3c7c79c2208ba0d9c9cf24a6d046d21d21f90f703f006a18d5653c4edf5391ff23a61f03ff83d237e880ee61187fa9f379a028e0a").unwrap());
+               if random_bit {
+                       target_value.append(&mut hex::decode("20").unwrap());
+               } else {
+                       target_value.append(&mut hex::decode("00").unwrap());
+               }
+               if shutdown {
+                       target_value.append(&mut hex::decode("001976a91479b000887626b294a914501a4cd226b58b23598388ac").unwrap());
+               }
+               assert_eq!(encoded_value, target_value);
+       }
+
+       #[test]
+       fn encoding_open_channel() {
+               do_encoding_open_channel(false, false, false);
+               do_encoding_open_channel(true, false, false);
+               do_encoding_open_channel(false, true, false);
+               do_encoding_open_channel(false, false, true);
+               do_encoding_open_channel(true, true, true);
+       }
+
+       fn do_encoding_accept_channel(shutdown: bool) {
+               let secp_ctx = Secp256k1::new();
+               let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
+               let (_, pubkey_2) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202", secp_ctx);
+               let (_, pubkey_3) = get_keys_from!("0303030303030303030303030303030303030303030303030303030303030303", secp_ctx);
+               let (_, pubkey_4) = get_keys_from!("0404040404040404040404040404040404040404040404040404040404040404", secp_ctx);
+               let (_, pubkey_5) = get_keys_from!("0505050505050505050505050505050505050505050505050505050505050505", secp_ctx);
+               let (_, pubkey_6) = get_keys_from!("0606060606060606060606060606060606060606060606060606060606060606", secp_ctx);
+               let accept_channel = msgs::AcceptChannel {
+                       temporary_channel_id: [2; 32],
+                       dust_limit_satoshis: 1311768467284833366,
+                       max_htlc_value_in_flight_msat: 2536655962884945560,
+                       channel_reserve_satoshis: 3608586615801332854,
+                       htlc_minimum_msat: 2316138423780173,
+                       minimum_depth: 821716,
+                       to_self_delay: 49340,
+                       max_accepted_htlcs: 49340,
+                       funding_pubkey: pubkey_1,
+                       revocation_basepoint: pubkey_2,
+                       payment_basepoint: pubkey_3,
+                       delayed_payment_basepoint: pubkey_4,
+                       htlc_basepoint: pubkey_5,
+                       first_per_commitment_point: pubkey_6,
+                       shutdown_scriptpubkey: if shutdown { OptionalField::Present(Address::p2pkh(&::bitcoin::PublicKey{compressed: true, key: pubkey_1}, Network::Testnet).script_pubkey()) } else { OptionalField::Absent }
+               };
+               let encoded_value = accept_channel.encode();
+               let mut target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020212345678901234562334032891223698321446687011447600083a840000034d000c89d4c0bcc0bc031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f024d4b6cd1361032ca9bd2aeb9d900aa4d45d9ead80ac9423374c451a7254d076602531fe6068134503d2723133227c867ac8fa6c83c537e9a44c3c5bdbdcb1fe33703462779ad4aad39514614751a71085f2f10e1c7a593e4e030efb5b8721ce55b0b0362c0a046dacce86ddd0343c6d3c7c79c2208ba0d9c9cf24a6d046d21d21f90f703f006a18d5653c4edf5391ff23a61f03ff83d237e880ee61187fa9f379a028e0a").unwrap();
+               if shutdown {
+                       target_value.append(&mut hex::decode("001976a91479b000887626b294a914501a4cd226b58b23598388ac").unwrap());
+               }
+               assert_eq!(encoded_value, target_value);
+       }
+
+       #[test]
+       fn encoding_accept_channel() {
+               do_encoding_accept_channel(false);
+               do_encoding_accept_channel(true);
+       }
+
+       #[test]
+       fn encoding_funding_created() {
+               let secp_ctx = Secp256k1::new();
+               let (privkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
+               let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
+               let funding_created = msgs::FundingCreated {
+                       temporary_channel_id: [2; 32],
+                       funding_txid: Sha256dHash::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap(),
+                       funding_output_index: 255,
+                       signature: sig_1,
+               };
+               let encoded_value = funding_created.encode();
+               let target_value = hex::decode("02020202020202020202020202020202020202020202020202020202020202026e96fe9f8b0ddcd729ba03cfafa5a27b050b39d354dd980814268dfa9a44d4c200ffd977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
+               assert_eq!(encoded_value, target_value);
+       }
+
+       #[test]
+       fn encoding_funding_signed() {
+               let secp_ctx = Secp256k1::new();
+               let (privkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
+               let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
+               let funding_signed = msgs::FundingSigned {
+                       channel_id: [2; 32],
+                       signature: sig_1,
+               };
+               let encoded_value = funding_signed.encode();
+               let target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
+               assert_eq!(encoded_value, target_value);
+       }
+
+       #[test]
+       fn encoding_funding_locked() {
+               let secp_ctx = Secp256k1::new();
+               let (_, pubkey_1,) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
+               let funding_locked = msgs::FundingLocked {
+                       channel_id: [2; 32],
+                       next_per_commitment_point: pubkey_1,
+               };
+               let encoded_value = funding_locked.encode();
+               let target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f").unwrap();
+               assert_eq!(encoded_value, target_value);
+       }
+
+       fn do_encoding_shutdown(script_type: u8) {
+               let secp_ctx = Secp256k1::new();
+               let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
+               let script = Builder::new().push_opcode(opcodes::OP_TRUE).into_script();
+               let shutdown = msgs::Shutdown {
+                       channel_id: [2; 32],
+                       scriptpubkey: if script_type == 1 { Address::p2pkh(&::bitcoin::PublicKey{compressed: true, key: pubkey_1}, Network::Testnet).script_pubkey() } else if script_type == 2 { Address::p2sh(&script, Network::Testnet).script_pubkey() } else if script_type == 3 { Address::p2wpkh(&::bitcoin::PublicKey{compressed: true, key: pubkey_1}, Network::Testnet).script_pubkey() } else { Address::p2wsh(&script, Network::Testnet).script_pubkey() },
+               };
+               let encoded_value = shutdown.encode();
+               let mut target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap();
+               if script_type == 1 {
+                       target_value.append(&mut hex::decode("001976a91479b000887626b294a914501a4cd226b58b23598388ac").unwrap());
+               } else if script_type == 2 {
+                       target_value.append(&mut hex::decode("0017a914da1745e9b549bd0bfa1a569971c77eba30cd5a4b87").unwrap());
+               } else if script_type == 3 {
+                       target_value.append(&mut hex::decode("0016001479b000887626b294a914501a4cd226b58b235983").unwrap());
+               } else if script_type == 4 {
+                       target_value.append(&mut hex::decode("002200204ae81572f06e1b88fd5ced7a1a000945432e83e1551e6f721ee9c00b8cc33260").unwrap());
+               }
+               assert_eq!(encoded_value, target_value);
+       }
+
+       #[test]
+       fn encoding_shutdown() {
+               do_encoding_shutdown(1);
+               do_encoding_shutdown(2);
+               do_encoding_shutdown(3);
+               do_encoding_shutdown(4);
+       }
+
+       #[test]
+       fn encoding_closing_signed() {
+               let secp_ctx = Secp256k1::new();
+               let (privkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
+               let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
+               let closing_signed = msgs::ClosingSigned {
+                       channel_id: [2; 32],
+                       fee_satoshis: 2316138423780173,
+                       signature: sig_1,
+               };
+               let encoded_value = closing_signed.encode();
+               let target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020200083a840000034dd977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
+               assert_eq!(encoded_value, target_value);
+       }
+
+       #[test]
+       fn encoding_update_add_htlc() {
+               let secp_ctx = Secp256k1::new();
+               let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
+               let onion_routing_packet = msgs::OnionPacket {
+                       version: 255,
+                       public_key: Ok(pubkey_1),
+                       hop_data: [1; 20*65],
+                       hmac: [2; 32]
+               };
+               let update_add_htlc = msgs::UpdateAddHTLC {
+                       channel_id: [2; 32],
+                       htlc_id: 2316138423780173,
+                       amount_msat: 3608586615801332854,
+                       payment_hash: PaymentHash([1; 32]),
+                       cltv_expiry: 821716,
+                       onion_routing_packet
+               };
+               let encoded_value = update_add_htlc.encode();
+               let target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020200083a840000034d32144668701144760101010101010101010101010101010101010101010101010101010101010101000c89d4ff031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010202020202020202020202020202020202020202020202020202020202020202").unwrap();
+               assert_eq!(encoded_value, target_value);
+       }
+
+       #[test]
+       fn encoding_update_fulfill_htlc() {
+               let update_fulfill_htlc = msgs::UpdateFulfillHTLC {
+                       channel_id: [2; 32],
+                       htlc_id: 2316138423780173,
+                       payment_preimage: PaymentPreimage([1; 32]),
+               };
+               let encoded_value = update_fulfill_htlc.encode();
+               let target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020200083a840000034d0101010101010101010101010101010101010101010101010101010101010101").unwrap();
+               assert_eq!(encoded_value, target_value);
+       }
+
+       #[test]
+       fn encoding_update_fail_htlc() {
+               let reason = OnionErrorPacket {
+                       data: [1; 32].to_vec(),
+               };
+               let update_fail_htlc = msgs::UpdateFailHTLC {
+                       channel_id: [2; 32],
+                       htlc_id: 2316138423780173,
+                       reason
+               };
+               let encoded_value = update_fail_htlc.encode();
+               let target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020200083a840000034d00200101010101010101010101010101010101010101010101010101010101010101").unwrap();
+               assert_eq!(encoded_value, target_value);
+       }
+
+       #[test]
+       fn encoding_update_fail_malformed_htlc() {
+               let update_fail_malformed_htlc = msgs::UpdateFailMalformedHTLC {
+                       channel_id: [2; 32],
+                       htlc_id: 2316138423780173,
+                       sha256_of_onion: [1; 32],
+                       failure_code: 255
+               };
+               let encoded_value = update_fail_malformed_htlc.encode();
+               let target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020200083a840000034d010101010101010101010101010101010101010101010101010101010101010100ff").unwrap();
+               assert_eq!(encoded_value, target_value);
+       }
+
+       fn do_encoding_commitment_signed(htlcs: bool) {
+               let secp_ctx = Secp256k1::new();
+               let (privkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
+               let (privkey_2, _) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202", secp_ctx);
+               let (privkey_3, _) = get_keys_from!("0303030303030303030303030303030303030303030303030303030303030303", secp_ctx);
+               let (privkey_4, _) = get_keys_from!("0404040404040404040404040404040404040404040404040404040404040404", secp_ctx);
+               let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
+               let sig_2 = get_sig_on!(privkey_2, secp_ctx, String::from("01010101010101010101010101010101"));
+               let sig_3 = get_sig_on!(privkey_3, secp_ctx, String::from("01010101010101010101010101010101"));
+               let sig_4 = get_sig_on!(privkey_4, secp_ctx, String::from("01010101010101010101010101010101"));
+               let commitment_signed = msgs::CommitmentSigned {
+                       channel_id: [2; 32],
+                       signature: sig_1,
+                       htlc_signatures: if htlcs { vec![sig_2, sig_3, sig_4] } else { Vec::new() },
+               };
+               let encoded_value = commitment_signed.encode();
+               let mut target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
+               if htlcs {
+                       target_value.append(&mut hex::decode("00031735b6a427e80d5fe7cd90a2f4ee08dc9c27cda7c35a4172e5d85b12c49d4232537e98f9b1f3c5e6989a8b9644e90e8918127680dbd0d4043510840fc0f1e11a216c280b5395a2546e7e4b2663e04f811622f15a4f91e83aa2e92ba2a573c139142c54ae63072a1ec1ee7dc0c04bde5c847806172aa05c92c22ae8e308d1d2692b12cc195ce0a2d1bda6a88befa19fa07f51caa75ce83837f28965600b8aacab0855ffb0e741ec5f7c41421e9829a9d48611c8c831f71be5ea73e66594977ffd").unwrap());
+               } else {
+                       target_value.append(&mut hex::decode("0000").unwrap());
+               }
+               assert_eq!(encoded_value, target_value);
+       }
+
+       #[test]
+       fn encoding_commitment_signed() {
+               do_encoding_commitment_signed(true);
+               do_encoding_commitment_signed(false);
+       }
+
+       #[test]
+       fn encoding_revoke_and_ack() {
+               let secp_ctx = Secp256k1::new();
+               let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
+               let raa = msgs::RevokeAndACK {
+                       channel_id: [2; 32],
+                       per_commitment_secret: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
+                       next_per_commitment_point: pubkey_1,
+               };
+               let encoded_value = raa.encode();
+               let target_value = hex::decode("02020202020202020202020202020202020202020202020202020202020202020101010101010101010101010101010101010101010101010101010101010101031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f").unwrap();
+               assert_eq!(encoded_value, target_value);
+       }
+
+       #[test]
+       fn encoding_update_fee() {
+               let update_fee = msgs::UpdateFee {
+                       channel_id: [2; 32],
+                       feerate_per_kw: 20190119,
+               };
+               let encoded_value = update_fee.encode();
+               let target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202013413a7").unwrap();
+               assert_eq!(encoded_value, target_value);
+       }
+
+       fn do_encoding_init(unknown_global_bits: bool, initial_routing_sync: bool) {
+               let mut global = GlobalFeatures::new();
+               if unknown_global_bits {
+                       global.flags = vec![0xFF, 0xFF];
+               }
+               let mut local = LocalFeatures::new();
+               if initial_routing_sync {
+                       local.set_initial_routing_sync();
+               }
+               let init = msgs::Init {
+                       global_features: global,
+                       local_features: local,
+               };
+               let encoded_value = init.encode();
+               let mut target_value = Vec::new();
+               if unknown_global_bits {
+                       target_value.append(&mut hex::decode("0002ffff").unwrap());
+               } else {
+                       target_value.append(&mut hex::decode("0000").unwrap());
+               }
+               if initial_routing_sync {
+                       target_value.append(&mut hex::decode("00012a").unwrap());
+               } else {
+                       target_value.append(&mut hex::decode("000122").unwrap());
+               }
+               assert_eq!(encoded_value, target_value);
+       }
+
+       #[test]
+       fn encoding_init() {
+               do_encoding_init(false, false);
+               do_encoding_init(true, false);
+               do_encoding_init(false, true);
+               do_encoding_init(true, true);
+       }
+
+       #[test]
+       fn encoding_error() {
+               let error = msgs::ErrorMessage {
+                       channel_id: [2; 32],
+                       data: String::from("rust-lightning"),
+               };
+               let encoded_value = error.encode();
+               let target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202000e727573742d6c696768746e696e67").unwrap();
+               assert_eq!(encoded_value, target_value);
+       }
+
+       #[test]
+       fn encoding_ping() {
+               let ping = msgs::Ping {
+                       ponglen: 64,
+                       byteslen: 64
+               };
+               let encoded_value = ping.encode();
+               let target_value = hex::decode("0040004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").unwrap();
+               assert_eq!(encoded_value, target_value);
+       }
+
+       #[test]
+       fn encoding_pong() {
+               let pong = msgs::Pong {
+                       byteslen: 64
+               };
+               let encoded_value = pong.encode();
+               let target_value = hex::decode("004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").unwrap();
+               assert_eq!(encoded_value, target_value);
+       }
+}
diff --git a/lightning/src/ln/onion_utils.rs b/lightning/src/ln/onion_utils.rs
new file mode 100644 (file)
index 0000000..6ca8811
--- /dev/null
@@ -0,0 +1,579 @@
+use ln::channelmanager::{PaymentHash, HTLCSource};
+use ln::msgs;
+use ln::router::{Route,RouteHop};
+use util::byte_utils;
+use util::chacha20::ChaCha20;
+use util::errors::{self, APIError};
+use util::ser::{Readable, Writeable};
+use util::logger::{Logger, LogHolder};
+
+use bitcoin_hashes::{Hash, HashEngine};
+use bitcoin_hashes::cmp::fixed_time_eq;
+use bitcoin_hashes::hmac::{Hmac, HmacEngine};
+use bitcoin_hashes::sha256::Hash as Sha256;
+
+use secp256k1::key::{SecretKey,PublicKey};
+use secp256k1::Secp256k1;
+use secp256k1::ecdh::SharedSecret;
+use secp256k1;
+
+use std::io::Cursor;
+use std::sync::Arc;
+
+pub(super) struct OnionKeys {
+       #[cfg(test)]
+       pub(super) shared_secret: SharedSecret,
+       #[cfg(test)]
+       pub(super) blinding_factor: [u8; 32],
+       pub(super) ephemeral_pubkey: PublicKey,
+       pub(super) rho: [u8; 32],
+       pub(super) mu: [u8; 32],
+}
+
+#[inline]
+pub(super) fn gen_rho_mu_from_shared_secret(shared_secret: &[u8]) -> ([u8; 32], [u8; 32]) {
+       assert_eq!(shared_secret.len(), 32);
+       ({
+               let mut hmac = HmacEngine::<Sha256>::new(&[0x72, 0x68, 0x6f]); // rho
+               hmac.input(&shared_secret[..]);
+               Hmac::from_engine(hmac).into_inner()
+       },
+       {
+               let mut hmac = HmacEngine::<Sha256>::new(&[0x6d, 0x75]); // mu
+               hmac.input(&shared_secret[..]);
+               Hmac::from_engine(hmac).into_inner()
+       })
+}
+
+#[inline]
+pub(super) fn gen_um_from_shared_secret(shared_secret: &[u8]) -> [u8; 32] {
+       assert_eq!(shared_secret.len(), 32);
+       let mut hmac = HmacEngine::<Sha256>::new(&[0x75, 0x6d]); // um
+       hmac.input(&shared_secret[..]);
+       Hmac::from_engine(hmac).into_inner()
+}
+
+#[inline]
+pub(super) fn gen_ammag_from_shared_secret(shared_secret: &[u8]) -> [u8; 32] {
+       assert_eq!(shared_secret.len(), 32);
+       let mut hmac = HmacEngine::<Sha256>::new(&[0x61, 0x6d, 0x6d, 0x61, 0x67]); // ammag
+       hmac.input(&shared_secret[..]);
+       Hmac::from_engine(hmac).into_inner()
+}
+
+// can only fail if an intermediary hop has an invalid public key or session_priv is invalid
+#[inline]
+pub(super) fn construct_onion_keys_callback<T: secp256k1::Signing, FType: FnMut(SharedSecret, [u8; 32], PublicKey, &RouteHop)> (secp_ctx: &Secp256k1<T>, route: &Route, session_priv: &SecretKey, mut callback: FType) -> Result<(), secp256k1::Error> {
+       let mut blinded_priv = session_priv.clone();
+       let mut blinded_pub = PublicKey::from_secret_key(secp_ctx, &blinded_priv);
+
+       for hop in route.hops.iter() {
+               let shared_secret = SharedSecret::new(&hop.pubkey, &blinded_priv);
+
+               let mut sha = Sha256::engine();
+               sha.input(&blinded_pub.serialize()[..]);
+               sha.input(&shared_secret[..]);
+               let blinding_factor = Sha256::from_engine(sha).into_inner();
+
+               let ephemeral_pubkey = blinded_pub;
+
+               blinded_priv.mul_assign(&blinding_factor)?;
+               blinded_pub = PublicKey::from_secret_key(secp_ctx, &blinded_priv);
+
+               callback(shared_secret, blinding_factor, ephemeral_pubkey, hop);
+       }
+
+       Ok(())
+}
+
+// can only fail if an intermediary hop has an invalid public key or session_priv is invalid
+pub(super) fn construct_onion_keys<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, route: &Route, session_priv: &SecretKey) -> Result<Vec<OnionKeys>, secp256k1::Error> {
+       let mut res = Vec::with_capacity(route.hops.len());
+
+       construct_onion_keys_callback(secp_ctx, route, session_priv, |shared_secret, _blinding_factor, ephemeral_pubkey, _| {
+               let (rho, mu) = gen_rho_mu_from_shared_secret(&shared_secret[..]);
+
+               res.push(OnionKeys {
+                       #[cfg(test)]
+                       shared_secret,
+                       #[cfg(test)]
+                       blinding_factor: _blinding_factor,
+                       ephemeral_pubkey,
+                       rho,
+                       mu,
+               });
+       })?;
+
+       Ok(res)
+}
+
+/// returns the hop data, as well as the first-hop value_msat and CLTV value we should send.
+pub(super) fn build_onion_payloads(route: &Route, starting_htlc_offset: u32) -> Result<(Vec<msgs::OnionHopData>, u64, u32), APIError> {
+       let mut cur_value_msat = 0u64;
+       let mut cur_cltv = starting_htlc_offset;
+       let mut last_short_channel_id = 0;
+       let mut res: Vec<msgs::OnionHopData> = Vec::with_capacity(route.hops.len());
+
+       for hop in route.hops.iter().rev() {
+               // First hop gets special values so that it can check, on receipt, that everything is
+               // exactly as it should be (and the next hop isn't trying to probe to find out if we're
+               // the intended recipient).
+               let value_msat = if cur_value_msat == 0 { hop.fee_msat } else { cur_value_msat };
+               let cltv = if cur_cltv == starting_htlc_offset { hop.cltv_expiry_delta + starting_htlc_offset } else { cur_cltv };
+               res.insert(0, msgs::OnionHopData {
+                       realm: 0,
+                       data: msgs::OnionRealm0HopData {
+                               short_channel_id: last_short_channel_id,
+                               amt_to_forward: value_msat,
+                               outgoing_cltv_value: cltv,
+                       },
+                       hmac: [0; 32],
+               });
+               cur_value_msat += hop.fee_msat;
+               if cur_value_msat >= 21000000 * 100000000 * 1000 {
+                       return Err(APIError::RouteError{err: "Channel fees overflowed?!"});
+               }
+               cur_cltv += hop.cltv_expiry_delta as u32;
+               if cur_cltv >= 500000000 {
+                       return Err(APIError::RouteError{err: "Channel CLTV overflowed?!"});
+               }
+               last_short_channel_id = hop.short_channel_id;
+       }
+       Ok((res, cur_value_msat, cur_cltv))
+}
+
+#[inline]
+fn shift_arr_right(arr: &mut [u8; 20*65]) {
+       for i in (65..20*65).rev() {
+               arr[i] = arr[i-65];
+       }
+       for i in 0..65 {
+               arr[i] = 0;
+       }
+}
+
+#[inline]
+fn xor_bufs(dst: &mut[u8], src: &[u8]) {
+       assert_eq!(dst.len(), src.len());
+
+       for i in 0..dst.len() {
+               dst[i] ^= src[i];
+       }
+}
+
+const ZERO:[u8; 21*65] = [0; 21*65];
+pub(super) fn construct_onion_packet(mut payloads: Vec<msgs::OnionHopData>, onion_keys: Vec<OnionKeys>, associated_data: &PaymentHash) -> msgs::OnionPacket {
+       let mut buf = Vec::with_capacity(21*65);
+       buf.resize(21*65, 0);
+
+       let filler = {
+               let iters = payloads.len() - 1;
+               let end_len = iters * 65;
+               let mut res = Vec::with_capacity(end_len);
+               res.resize(end_len, 0);
+
+               for (i, keys) in onion_keys.iter().enumerate() {
+                       if i == payloads.len() - 1 { continue; }
+                       let mut chacha = ChaCha20::new(&keys.rho, &[0u8; 8]);
+                       chacha.process(&ZERO, &mut buf); // We don't have a seek function :(
+                       xor_bufs(&mut res[0..(i + 1)*65], &buf[(20 - i)*65..21*65]);
+               }
+               res
+       };
+
+       let mut packet_data = [0; 20*65];
+       let mut hmac_res = [0; 32];
+
+       for (i, (payload, keys)) in payloads.iter_mut().zip(onion_keys.iter()).rev().enumerate() {
+               shift_arr_right(&mut packet_data);
+               payload.hmac = hmac_res;
+               packet_data[0..65].copy_from_slice(&payload.encode()[..]);
+
+               let mut chacha = ChaCha20::new(&keys.rho, &[0u8; 8]);
+               chacha.process(&packet_data, &mut buf[0..20*65]);
+               packet_data[..].copy_from_slice(&buf[0..20*65]);
+
+               if i == 0 {
+                       packet_data[20*65 - filler.len()..20*65].copy_from_slice(&filler[..]);
+               }
+
+               let mut hmac = HmacEngine::<Sha256>::new(&keys.mu);
+               hmac.input(&packet_data);
+               hmac.input(&associated_data.0[..]);
+               hmac_res = Hmac::from_engine(hmac).into_inner();
+       }
+
+       msgs::OnionPacket{
+               version: 0,
+               public_key: Ok(onion_keys.first().unwrap().ephemeral_pubkey),
+               hop_data: packet_data,
+               hmac: hmac_res,
+       }
+}
+
+/// Encrypts a failure packet. raw_packet can either be a
+/// msgs::DecodedOnionErrorPacket.encode() result or a msgs::OnionErrorPacket.data element.
+pub(super) fn encrypt_failure_packet(shared_secret: &[u8], raw_packet: &[u8]) -> msgs::OnionErrorPacket {
+       let ammag = gen_ammag_from_shared_secret(&shared_secret);
+
+       let mut packet_crypted = Vec::with_capacity(raw_packet.len());
+       packet_crypted.resize(raw_packet.len(), 0);
+       let mut chacha = ChaCha20::new(&ammag, &[0u8; 8]);
+       chacha.process(&raw_packet, &mut packet_crypted[..]);
+       msgs::OnionErrorPacket {
+               data: packet_crypted,
+       }
+}
+
+pub(super) fn build_failure_packet(shared_secret: &[u8], failure_type: u16, failure_data: &[u8]) -> msgs::DecodedOnionErrorPacket {
+       assert_eq!(shared_secret.len(), 32);
+       assert!(failure_data.len() <= 256 - 2);
+
+       let um = gen_um_from_shared_secret(&shared_secret);
+
+       let failuremsg = {
+               let mut res = Vec::with_capacity(2 + failure_data.len());
+               res.push(((failure_type >> 8) & 0xff) as u8);
+               res.push(((failure_type >> 0) & 0xff) as u8);
+               res.extend_from_slice(&failure_data[..]);
+               res
+       };
+       let pad = {
+               let mut res = Vec::with_capacity(256 - 2 - failure_data.len());
+               res.resize(256 - 2 - failure_data.len(), 0);
+               res
+       };
+       let mut packet = msgs::DecodedOnionErrorPacket {
+               hmac: [0; 32],
+               failuremsg: failuremsg,
+               pad: pad,
+       };
+
+       let mut hmac = HmacEngine::<Sha256>::new(&um);
+       hmac.input(&packet.encode()[32..]);
+       packet.hmac = Hmac::from_engine(hmac).into_inner();
+
+       packet
+}
+
+#[inline]
+pub(super) fn build_first_hop_failure_packet(shared_secret: &[u8], failure_type: u16, failure_data: &[u8]) -> msgs::OnionErrorPacket {
+       let failure_packet = build_failure_packet(shared_secret, failure_type, failure_data);
+       encrypt_failure_packet(shared_secret, &failure_packet.encode()[..])
+}
+
+/// Process failure we got back from upstream on a payment we sent (implying htlc_source is an
+/// OutboundRoute).
+/// Returns update, a boolean indicating that the payment itself failed, and the error code.
+pub(super) fn process_onion_failure<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, logger: &Arc<Logger>, htlc_source: &HTLCSource, mut packet_decrypted: Vec<u8>) -> (Option<msgs::HTLCFailChannelUpdate>, bool, Option<u16>) {
+       if let &HTLCSource::OutboundRoute { ref route, ref session_priv, ref first_hop_htlc_msat } = htlc_source {
+               let mut res = None;
+               let mut htlc_msat = *first_hop_htlc_msat;
+               let mut error_code_ret = None;
+               let mut next_route_hop_ix = 0;
+               let mut is_from_final_node = false;
+
+               // Handle packed channel/node updates for passing back for the route handler
+               construct_onion_keys_callback(secp_ctx, route, session_priv, |shared_secret, _, _, route_hop| {
+                       next_route_hop_ix += 1;
+                       if res.is_some() { return; }
+
+                       let amt_to_forward = htlc_msat - route_hop.fee_msat;
+                       htlc_msat = amt_to_forward;
+
+                       let ammag = gen_ammag_from_shared_secret(&shared_secret[..]);
+
+                       let mut decryption_tmp = Vec::with_capacity(packet_decrypted.len());
+                       decryption_tmp.resize(packet_decrypted.len(), 0);
+                       let mut chacha = ChaCha20::new(&ammag, &[0u8; 8]);
+                       chacha.process(&packet_decrypted, &mut decryption_tmp[..]);
+                       packet_decrypted = decryption_tmp;
+
+                       is_from_final_node = route.hops.last().unwrap().pubkey == route_hop.pubkey;
+
+                       if let Ok(err_packet) = msgs::DecodedOnionErrorPacket::read(&mut Cursor::new(&packet_decrypted)) {
+                               let um = gen_um_from_shared_secret(&shared_secret[..]);
+                               let mut hmac = HmacEngine::<Sha256>::new(&um);
+                               hmac.input(&err_packet.encode()[32..]);
+
+                               if fixed_time_eq(&Hmac::from_engine(hmac).into_inner(), &err_packet.hmac) {
+                                       if let Some(error_code_slice) = err_packet.failuremsg.get(0..2) {
+                                               const PERM: u16 = 0x4000;
+                                               const NODE: u16 = 0x2000;
+                                               const UPDATE: u16 = 0x1000;
+
+                                               let error_code = byte_utils::slice_to_be16(&error_code_slice);
+                                               error_code_ret = Some(error_code);
+
+                                               let (debug_field, debug_field_size) = errors::get_onion_debug_field(error_code);
+
+                                               // indicate that payment parameter has failed and no need to
+                                               // update Route object
+                                               let payment_failed = (match error_code & 0xff {
+                                                       15|16|17|18|19 => true,
+                                                       _ => false,
+                                               } && is_from_final_node) // PERM bit observed below even this error is from the intermediate nodes
+                                               || error_code == 21; // Special case error 21 as the Route object is bogus, TODO: Maybe fail the node if the CLTV was reasonable?
+
+                                               let mut fail_channel_update = None;
+
+                                               if error_code & NODE == NODE {
+                                                       fail_channel_update = Some(msgs::HTLCFailChannelUpdate::NodeFailure { node_id: route_hop.pubkey, is_permanent: error_code & PERM == PERM });
+                                               }
+                                               else if error_code & PERM == PERM {
+                                                       fail_channel_update = if payment_failed {None} else {Some(msgs::HTLCFailChannelUpdate::ChannelClosed {
+                                                               short_channel_id: route.hops[next_route_hop_ix - if next_route_hop_ix == route.hops.len() { 1 } else { 0 }].short_channel_id,
+                                                               is_permanent: true,
+                                                       })};
+                                               }
+                                               else if error_code & UPDATE == UPDATE {
+                                                       if let Some(update_len_slice) = err_packet.failuremsg.get(debug_field_size+2..debug_field_size+4) {
+                                                               let update_len = byte_utils::slice_to_be16(&update_len_slice) as usize;
+                                                               if let Some(update_slice) = err_packet.failuremsg.get(debug_field_size + 4..debug_field_size + 4 + update_len) {
+                                                                       if let Ok(chan_update) = msgs::ChannelUpdate::read(&mut Cursor::new(&update_slice)) {
+                                                                               // if channel_update should NOT have caused the failure:
+                                                                               // MAY treat the channel_update as invalid.
+                                                                               let is_chan_update_invalid = match error_code & 0xff {
+                                                                                       7 => false,
+                                                                                       11 => amt_to_forward > chan_update.contents.htlc_minimum_msat,
+                                                                                       12 => {
+                                                                                               let new_fee = amt_to_forward.checked_mul(chan_update.contents.fee_proportional_millionths as u64).and_then(|prop_fee| { (prop_fee / 1000000).checked_add(chan_update.contents.fee_base_msat as u64) });
+                                                                                               new_fee.is_some() && route_hop.fee_msat >= new_fee.unwrap()
+                                                                                       }
+                                                                                       13 => route_hop.cltv_expiry_delta as u16 >= chan_update.contents.cltv_expiry_delta,
+                                                                                       14 => false, // expiry_too_soon; always valid?
+                                                                                       20 => chan_update.contents.flags & 2 == 0,
+                                                                                       _ => false, // unknown error code; take channel_update as valid
+                                                                               };
+                                                                               fail_channel_update = if is_chan_update_invalid {
+                                                                                       // This probably indicates the node which forwarded
+                                                                                       // to the node in question corrupted something.
+                                                                                       Some(msgs::HTLCFailChannelUpdate::ChannelClosed {
+                                                                                               short_channel_id: route_hop.short_channel_id,
+                                                                                               is_permanent: true,
+                                                                                       })
+                                                                               } else {
+                                                                                       Some(msgs::HTLCFailChannelUpdate::ChannelUpdateMessage {
+                                                                                               msg: chan_update,
+                                                                                       })
+                                                                               };
+                                                                       }
+                                                               }
+                                                       }
+                                                       if fail_channel_update.is_none() {
+                                                               // They provided an UPDATE which was obviously bogus, not worth
+                                                               // trying to relay through them anymore.
+                                                               fail_channel_update = Some(msgs::HTLCFailChannelUpdate::NodeFailure {
+                                                                       node_id: route_hop.pubkey,
+                                                                       is_permanent: true,
+                                                               });
+                                                       }
+                                               } else if !payment_failed {
+                                                       // We can't understand their error messages and they failed to
+                                                       // forward...they probably can't understand our forwards so its
+                                                       // really not worth trying any further.
+                                                       fail_channel_update = Some(msgs::HTLCFailChannelUpdate::NodeFailure {
+                                                               node_id: route_hop.pubkey,
+                                                               is_permanent: true,
+                                                       });
+                                               }
+
+                                               // TODO: Here (and a few other places) we assume that BADONION errors
+                                               // are always "sourced" from the node previous to the one which failed
+                                               // to decode the onion.
+                                               res = Some((fail_channel_update, !(error_code & PERM == PERM && is_from_final_node)));
+
+                                               let (description, title) = errors::get_onion_error_description(error_code);
+                                               if debug_field_size > 0 && err_packet.failuremsg.len() >= 4 + debug_field_size {
+                                                       let log_holder = LogHolder { logger };
+                                                       log_warn!(log_holder, "Onion Error[{}({:#x}) {}({})] {}", title, error_code, debug_field, log_bytes!(&err_packet.failuremsg[4..4+debug_field_size]), description);
+                                               }
+                                               else {
+                                                       let log_holder = LogHolder { logger };
+                                                       log_warn!(log_holder, "Onion Error[{}({:#x})] {}", title, error_code, description);
+                                               }
+                                       } else {
+                                               // Useless packet that we can't use but it passed HMAC, so it
+                                               // definitely came from the peer in question
+                                               res = Some((Some(msgs::HTLCFailChannelUpdate::NodeFailure {
+                                                       node_id: route_hop.pubkey,
+                                                       is_permanent: true,
+                                               }), !is_from_final_node));
+                                       }
+                               }
+                       }
+               }).expect("Route that we sent via spontaneously grew invalid keys in the middle of it?");
+               if let Some((channel_update, payment_retryable)) = res {
+                       (channel_update, payment_retryable, error_code_ret)
+               } else {
+                       // only not set either packet unparseable or hmac does not match with any
+                       // payment not retryable only when garbage is from the final node
+                       (None, !is_from_final_node, None)
+               }
+       } else { unreachable!(); }
+}
+
+#[cfg(test)]
+mod tests {
+       use ln::channelmanager::PaymentHash;
+       use ln::router::{Route, RouteHop};
+       use ln::msgs;
+       use util::ser::Writeable;
+
+       use hex;
+
+       use secp256k1::Secp256k1;
+       use secp256k1::key::{PublicKey,SecretKey};
+
+       use super::OnionKeys;
+
+       fn build_test_onion_keys() -> Vec<OnionKeys> {
+               // Keys from BOLT 4, used in both test vector tests
+               let secp_ctx = Secp256k1::new();
+
+               let route = Route {
+                       hops: vec!(
+                                       RouteHop {
+                                               pubkey: PublicKey::from_slice(&hex::decode("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap(),
+                                               short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0 // Test vectors are garbage and not generateble from a RouteHop, we fill in payloads manually
+                                       },
+                                       RouteHop {
+                                               pubkey: PublicKey::from_slice(&hex::decode("0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c").unwrap()[..]).unwrap(),
+                                               short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0 // Test vectors are garbage and not generateble from a RouteHop, we fill in payloads manually
+                                       },
+                                       RouteHop {
+                                               pubkey: PublicKey::from_slice(&hex::decode("027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007").unwrap()[..]).unwrap(),
+                                               short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0 // Test vectors are garbage and not generateble from a RouteHop, we fill in payloads manually
+                                       },
+                                       RouteHop {
+                                               pubkey: PublicKey::from_slice(&hex::decode("032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991").unwrap()[..]).unwrap(),
+                                               short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0 // Test vectors are garbage and not generateble from a RouteHop, we fill in payloads manually
+                                       },
+                                       RouteHop {
+                                               pubkey: PublicKey::from_slice(&hex::decode("02edabbd16b41c8371b92ef2f04c1185b4f03b6dcd52ba9b78d9d7c89c8f221145").unwrap()[..]).unwrap(),
+                                               short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0 // Test vectors are garbage and not generateble from a RouteHop, we fill in payloads manually
+                                       },
+                       ),
+               };
+
+               let session_priv = SecretKey::from_slice(&hex::decode("4141414141414141414141414141414141414141414141414141414141414141").unwrap()[..]).unwrap();
+
+               let onion_keys = super::construct_onion_keys(&secp_ctx, &route, &session_priv).unwrap();
+               assert_eq!(onion_keys.len(), route.hops.len());
+               onion_keys
+       }
+
+       #[test]
+       fn onion_vectors() {
+               // Packet creation test vectors from BOLT 4
+               let onion_keys = build_test_onion_keys();
+
+               assert_eq!(onion_keys[0].shared_secret[..], hex::decode("53eb63ea8a3fec3b3cd433b85cd62a4b145e1dda09391b348c4e1cd36a03ea66").unwrap()[..]);
+               assert_eq!(onion_keys[0].blinding_factor[..], hex::decode("2ec2e5da605776054187180343287683aa6a51b4b1c04d6dd49c45d8cffb3c36").unwrap()[..]);
+               assert_eq!(onion_keys[0].ephemeral_pubkey.serialize()[..], hex::decode("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]);
+               assert_eq!(onion_keys[0].rho, hex::decode("ce496ec94def95aadd4bec15cdb41a740c9f2b62347c4917325fcc6fb0453986").unwrap()[..]);
+               assert_eq!(onion_keys[0].mu, hex::decode("b57061dc6d0a2b9f261ac410c8b26d64ac5506cbba30267a649c28c179400eba").unwrap()[..]);
+
+               assert_eq!(onion_keys[1].shared_secret[..], hex::decode("a6519e98832a0b179f62123b3567c106db99ee37bef036e783263602f3488fae").unwrap()[..]);
+               assert_eq!(onion_keys[1].blinding_factor[..], hex::decode("bf66c28bc22e598cfd574a1931a2bafbca09163df2261e6d0056b2610dab938f").unwrap()[..]);
+               assert_eq!(onion_keys[1].ephemeral_pubkey.serialize()[..], hex::decode("028f9438bfbf7feac2e108d677e3a82da596be706cc1cf342b75c7b7e22bf4e6e2").unwrap()[..]);
+               assert_eq!(onion_keys[1].rho, hex::decode("450ffcabc6449094918ebe13d4f03e433d20a3d28a768203337bc40b6e4b2c59").unwrap()[..]);
+               assert_eq!(onion_keys[1].mu, hex::decode("05ed2b4a3fb023c2ff5dd6ed4b9b6ea7383f5cfe9d59c11d121ec2c81ca2eea9").unwrap()[..]);
+
+               assert_eq!(onion_keys[2].shared_secret[..], hex::decode("3a6b412548762f0dbccce5c7ae7bb8147d1caf9b5471c34120b30bc9c04891cc").unwrap()[..]);
+               assert_eq!(onion_keys[2].blinding_factor[..], hex::decode("a1f2dadd184eb1627049673f18c6325814384facdee5bfd935d9cb031a1698a5").unwrap()[..]);
+               assert_eq!(onion_keys[2].ephemeral_pubkey.serialize()[..], hex::decode("03bfd8225241ea71cd0843db7709f4c222f62ff2d4516fd38b39914ab6b83e0da0").unwrap()[..]);
+               assert_eq!(onion_keys[2].rho, hex::decode("11bf5c4f960239cb37833936aa3d02cea82c0f39fd35f566109c41f9eac8deea").unwrap()[..]);
+               assert_eq!(onion_keys[2].mu, hex::decode("caafe2820fa00eb2eeb78695ae452eba38f5a53ed6d53518c5c6edf76f3f5b78").unwrap()[..]);
+
+               assert_eq!(onion_keys[3].shared_secret[..], hex::decode("21e13c2d7cfe7e18836df50872466117a295783ab8aab0e7ecc8c725503ad02d").unwrap()[..]);
+               assert_eq!(onion_keys[3].blinding_factor[..], hex::decode("7cfe0b699f35525029ae0fa437c69d0f20f7ed4e3916133f9cacbb13c82ff262").unwrap()[..]);
+               assert_eq!(onion_keys[3].ephemeral_pubkey.serialize()[..], hex::decode("031dde6926381289671300239ea8e57ffaf9bebd05b9a5b95beaf07af05cd43595").unwrap()[..]);
+               assert_eq!(onion_keys[3].rho, hex::decode("cbe784ab745c13ff5cffc2fbe3e84424aa0fd669b8ead4ee562901a4a4e89e9e").unwrap()[..]);
+               assert_eq!(onion_keys[3].mu, hex::decode("5052aa1b3d9f0655a0932e50d42f0c9ba0705142c25d225515c45f47c0036ee9").unwrap()[..]);
+
+               assert_eq!(onion_keys[4].shared_secret[..], hex::decode("b5756b9b542727dbafc6765a49488b023a725d631af688fc031217e90770c328").unwrap()[..]);
+               assert_eq!(onion_keys[4].blinding_factor[..], hex::decode("c96e00dddaf57e7edcd4fb5954be5b65b09f17cb6d20651b4e90315be5779205").unwrap()[..]);
+               assert_eq!(onion_keys[4].ephemeral_pubkey.serialize()[..], hex::decode("03a214ebd875aab6ddfd77f22c5e7311d7f77f17a169e599f157bbcdae8bf071f4").unwrap()[..]);
+               assert_eq!(onion_keys[4].rho, hex::decode("034e18b8cc718e8af6339106e706c52d8df89e2b1f7e9142d996acf88df8799b").unwrap()[..]);
+               assert_eq!(onion_keys[4].mu, hex::decode("8e45e5c61c2b24cb6382444db6698727afb063adecd72aada233d4bf273d975a").unwrap()[..]);
+
+               // Test vectors below are flat-out wrong: they claim to set outgoing_cltv_value to non-0 :/
+               let payloads = vec!(
+                       msgs::OnionHopData {
+                               realm: 0,
+                               data: msgs::OnionRealm0HopData {
+                                       short_channel_id: 0,
+                                       amt_to_forward: 0,
+                                       outgoing_cltv_value: 0,
+                               },
+                               hmac: [0; 32],
+                       },
+                       msgs::OnionHopData {
+                               realm: 0,
+                               data: msgs::OnionRealm0HopData {
+                                       short_channel_id: 0x0101010101010101,
+                                       amt_to_forward: 0x0100000001,
+                                       outgoing_cltv_value: 0,
+                               },
+                               hmac: [0; 32],
+                       },
+                       msgs::OnionHopData {
+                               realm: 0,
+                               data: msgs::OnionRealm0HopData {
+                                       short_channel_id: 0x0202020202020202,
+                                       amt_to_forward: 0x0200000002,
+                                       outgoing_cltv_value: 0,
+                               },
+                               hmac: [0; 32],
+                       },
+                       msgs::OnionHopData {
+                               realm: 0,
+                               data: msgs::OnionRealm0HopData {
+                                       short_channel_id: 0x0303030303030303,
+                                       amt_to_forward: 0x0300000003,
+                                       outgoing_cltv_value: 0,
+                               },
+                               hmac: [0; 32],
+                       },
+                       msgs::OnionHopData {
+                               realm: 0,
+                               data: msgs::OnionRealm0HopData {
+                                       short_channel_id: 0x0404040404040404,
+                                       amt_to_forward: 0x0400000004,
+                                       outgoing_cltv_value: 0,
+                               },
+                               hmac: [0; 32],
+                       },
+               );
+
+               let packet = super::construct_onion_packet(payloads, onion_keys, &PaymentHash([0x42; 32]));
+               // Just check the final packet encoding, as it includes all the per-hop vectors in it
+               // anyway...
+               assert_eq!(packet.encode(), hex::decode("0002eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619e5f14350c2a76fc232b5e46d421e9615471ab9e0bc887beff8c95fdb878f7b3a716a996c7845c93d90e4ecbb9bde4ece2f69425c99e4bc820e44485455f135edc0d10f7d61ab590531cf08000179a333a347f8b4072f216400406bdf3bf038659793d4a1fd7b246979e3150a0a4cb052c9ec69acf0f48c3d39cd55675fe717cb7d80ce721caad69320c3a469a202f1e468c67eaf7a7cd8226d0fd32f7b48084dca885d56047694762b67021713ca673929c163ec36e04e40ca8e1c6d17569419d3039d9a1ec866abe044a9ad635778b961fc0776dc832b3a451bd5d35072d2269cf9b040f6b7a7dad84fb114ed413b1426cb96ceaf83825665ed5a1d002c1687f92465b49ed4c7f0218ff8c6c7dd7221d589c65b3b9aaa71a41484b122846c7c7b57e02e679ea8469b70e14fe4f70fee4d87b910cf144be6fe48eef24da475c0b0bcc6565ae82cd3f4e3b24c76eaa5616c6111343306ab35c1fe5ca4a77c0e314ed7dba39d6f1e0de791719c241a939cc493bea2bae1c1e932679ea94d29084278513c77b899cc98059d06a27d171b0dbdf6bee13ddc4fc17a0c4d2827d488436b57baa167544138ca2e64a11b43ac8a06cd0c2fba2d4d900ed2d9205305e2d7383cc98dacb078133de5f6fb6bed2ef26ba92cea28aafc3b9948dd9ae5559e8bd6920b8cea462aa445ca6a95e0e7ba52961b181c79e73bd581821df2b10173727a810c92b83b5ba4a0403eb710d2ca10689a35bec6c3a708e9e92f7d78ff3c5d9989574b00c6736f84c199256e76e19e78f0c98a9d580b4a658c84fc8f2096c2fbea8f5f8c59d0fdacb3be2802ef802abbecb3aba4acaac69a0e965abd8981e9896b1f6ef9d60f7a164b371af869fd0e48073742825e9434fc54da837e120266d53302954843538ea7c6c3dbfb4ff3b2fdbe244437f2a153ccf7bdb4c92aa08102d4f3cff2ae5ef86fab4653595e6a5837fa2f3e29f27a9cde5966843fb847a4a61f1e76c281fe8bb2b0a181d096100db5a1a5ce7a910238251a43ca556712eaadea167fb4d7d75825e440f3ecd782036d7574df8bceacb397abefc5f5254d2722215c53ff54af8299aaaad642c6d72a14d27882d9bbd539e1cc7a527526ba89b8c037ad09120e98ab042d3e8652b31ae0e478516bfaf88efca9f3676ffe99d2819dcaeb7610a626695f53117665d267d3f7abebd6bbd6733f645c72c389f03855bdf1e4b8075b516569b118233a0f0971d24b83113c0b096f5216a207ca99a7cddc81c130923fe3d91e7508c9ac5f2e914ff5dccab9e558566fa14efb34ac98d878580814b94b73acbfde9072f30b881f7f0fff42d4045d1ace6322d86a97d164aa84d93a60498065cc7c20e636f5862dc81531a88c60305a2e59a985be327a6902e4bed986dbf4a0b50c217af0ea7fdf9ab37f9ea1a1aaa72f54cf40154ea9b269f1a7c09f9f43245109431a175d50e2db0132337baa0ef97eed0fcf20489da36b79a1172faccc2f7ded7c60e00694282d93359c4682135642bc81f433574aa8ef0c97b4ade7ca372c5ffc23c7eddd839bab4e0f14d6df15c9dbeab176bec8b5701cf054eb3072f6dadc98f88819042bf10c407516ee58bce33fbe3b3d86a54255e577db4598e30a135361528c101683a5fcde7e8ba53f3456254be8f45fe3a56120ae96ea3773631fcb3873aa3abd91bcff00bd38bd43697a2e789e00da6077482e7b1b1a677b5afae4c54e6cbdf7377b694eb7d7a5b913476a5be923322d3de06060fd5e819635232a2cf4f0731da13b8546d1d6d4f8d75b9fce6c2341a71b0ea6f780df54bfdb0dd5cd9855179f602f9172307c7268724c3618e6817abd793adc214a0dc0bc616816632f27ea336fb56dfd").unwrap());
+       }
+
+       #[test]
+       fn test_failure_packet_onion() {
+               // Returning Errors test vectors from BOLT 4
+
+               let onion_keys = build_test_onion_keys();
+               let onion_error = super::build_failure_packet(&onion_keys[4].shared_secret[..], 0x2002, &[0; 0]);
+               assert_eq!(onion_error.encode(), hex::decode("4c2fc8bc08510334b6833ad9c3e79cd1b52ae59dfe5c2a4b23ead50f09f7ee0b0002200200fe0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").unwrap());
+
+               let onion_packet_1 = super::encrypt_failure_packet(&onion_keys[4].shared_secret[..], &onion_error.encode()[..]);
+               assert_eq!(onion_packet_1.data, hex::decode("a5e6bd0c74cb347f10cce367f949098f2457d14c046fd8a22cb96efb30b0fdcda8cb9168b50f2fd45edd73c1b0c8b33002df376801ff58aaa94000bf8a86f92620f343baef38a580102395ae3abf9128d1047a0736ff9b83d456740ebbb4aeb3aa9737f18fb4afb4aa074fb26c4d702f42968888550a3bded8c05247e045b866baef0499f079fdaeef6538f31d44deafffdfd3afa2fb4ca9082b8f1c465371a9894dd8c243fb4847e004f5256b3e90e2edde4c9fb3082ddfe4d1e734cacd96ef0706bf63c9984e22dc98851bcccd1c3494351feb458c9c6af41c0044bea3c47552b1d992ae542b17a2d0bba1a096c78d169034ecb55b6e3a7263c26017f033031228833c1daefc0dedb8cf7c3e37c9c37ebfe42f3225c326e8bcfd338804c145b16e34e4").unwrap());
+
+               let onion_packet_2 = super::encrypt_failure_packet(&onion_keys[3].shared_secret[..], &onion_packet_1.data[..]);
+               assert_eq!(onion_packet_2.data, hex::decode("c49a1ce81680f78f5f2000cda36268de34a3f0a0662f55b4e837c83a8773c22aa081bab1616a0011585323930fa5b9fae0c85770a2279ff59ec427ad1bbff9001c0cd1497004bd2a0f68b50704cf6d6a4bf3c8b6a0833399a24b3456961ba00736785112594f65b6b2d44d9f5ea4e49b5e1ec2af978cbe31c67114440ac51a62081df0ed46d4a3df295da0b0fe25c0115019f03f15ec86fabb4c852f83449e812f141a9395b3f70b766ebbd4ec2fae2b6955bd8f32684c15abfe8fd3a6261e52650e8807a92158d9f1463261a925e4bfba44bd20b166d532f0017185c3a6ac7957adefe45559e3072c8dc35abeba835a8cb01a71a15c736911126f27d46a36168ca5ef7dccd4e2886212602b181463e0dd30185c96348f9743a02aca8ec27c0b90dca270").unwrap());
+
+               let onion_packet_3 = super::encrypt_failure_packet(&onion_keys[2].shared_secret[..], &onion_packet_2.data[..]);
+               assert_eq!(onion_packet_3.data, hex::decode("a5d3e8634cfe78b2307d87c6d90be6fe7855b4f2cc9b1dfb19e92e4b79103f61ff9ac25f412ddfb7466e74f81b3e545563cdd8f5524dae873de61d7bdfccd496af2584930d2b566b4f8d3881f8c043df92224f38cf094cfc09d92655989531524593ec6d6caec1863bdfaa79229b5020acc034cd6deeea1021c50586947b9b8e6faa83b81fbfa6133c0af5d6b07c017f7158fa94f0d206baf12dda6b68f785b773b360fd0497e16cc402d779c8d48d0fa6315536ef0660f3f4e1865f5b38ea49c7da4fd959de4e83ff3ab686f059a45c65ba2af4a6a79166aa0f496bf04d06987b6d2ea205bdb0d347718b9aeff5b61dfff344993a275b79717cd815b6ad4c0beb568c4ac9c36ff1c315ec1119a1993c4b61e6eaa0375e0aaf738ac691abd3263bf937e3").unwrap());
+
+               let onion_packet_4 = super::encrypt_failure_packet(&onion_keys[1].shared_secret[..], &onion_packet_3.data[..]);
+               assert_eq!(onion_packet_4.data, hex::decode("aac3200c4968f56b21f53e5e374e3a2383ad2b1b6501bbcc45abc31e59b26881b7dfadbb56ec8dae8857add94e6702fb4c3a4de22e2e669e1ed926b04447fc73034bb730f4932acd62727b75348a648a1128744657ca6a4e713b9b646c3ca66cac02cdab44dd3439890ef3aaf61708714f7375349b8da541b2548d452d84de7084bb95b3ac2345201d624d31f4d52078aa0fa05a88b4e20202bd2b86ac5b52919ea305a8949de95e935eed0319cf3cf19ebea61d76ba92532497fcdc9411d06bcd4275094d0a4a3c5d3a945e43305a5a9256e333e1f64dbca5fcd4e03a39b9012d197506e06f29339dfee3331995b21615337ae060233d39befea925cc262873e0530408e6990f1cbd233a150ef7b004ff6166c70c68d9f8c853c1abca640b8660db2921").unwrap());
+
+               let onion_packet_5 = super::encrypt_failure_packet(&onion_keys[0].shared_secret[..], &onion_packet_4.data[..]);
+               assert_eq!(onion_packet_5.data, hex::decode("9c5add3963fc7f6ed7f148623c84134b5647e1306419dbe2174e523fa9e2fbed3a06a19f899145610741c83ad40b7712aefaddec8c6baf7325d92ea4ca4d1df8bce517f7e54554608bf2bd8071a4f52a7a2f7ffbb1413edad81eeea5785aa9d990f2865dc23b4bc3c301a94eec4eabebca66be5cf638f693ec256aec514620cc28ee4a94bd9565bc4d4962b9d3641d4278fb319ed2b84de5b665f307a2db0f7fbb757366067d88c50f7e829138fde4f78d39b5b5802f1b92a8a820865af5cc79f9f30bc3f461c66af95d13e5e1f0381c184572a91dee1c849048a647a1158cf884064deddbf1b0b88dfe2f791428d0ba0f6fb2f04e14081f69165ae66d9297c118f0907705c9c4954a199bae0bb96fad763d690e7daa6cfda59ba7f2c8d11448b604d12d").unwrap());
+       }
+}
diff --git a/lightning/src/ln/peer_channel_encryptor.rs b/lightning/src/ln/peer_channel_encryptor.rs
new file mode 100644 (file)
index 0000000..9d716a2
--- /dev/null
@@ -0,0 +1,716 @@
+use ln::msgs::HandleError;
+use ln::msgs;
+
+use bitcoin_hashes::{Hash, HashEngine, Hmac, HmacEngine};
+use bitcoin_hashes::sha256::Hash as Sha256;
+
+use secp256k1::Secp256k1;
+use secp256k1::key::{PublicKey,SecretKey};
+use secp256k1::ecdh::SharedSecret;
+use secp256k1;
+
+use util::chacha20poly1305rfc::ChaCha20Poly1305RFC;
+use util::byte_utils;
+
+// Sha256("Noise_XK_secp256k1_ChaChaPoly_SHA256")
+const NOISE_CK: [u8; 32] = [0x26, 0x40, 0xf5, 0x2e, 0xeb, 0xcd, 0x9e, 0x88, 0x29, 0x58, 0x95, 0x1c, 0x79, 0x42, 0x50, 0xee, 0xdb, 0x28, 0x00, 0x2c, 0x05, 0xd7, 0xdc, 0x2e, 0xa0, 0xf1, 0x95, 0x40, 0x60, 0x42, 0xca, 0xf1];
+// Sha256(NOISE_CK || "lightning")
+const NOISE_H: [u8; 32] = [0xd1, 0xfb, 0xf6, 0xde, 0xe4, 0xf6, 0x86, 0xf1, 0x32, 0xfd, 0x70, 0x2c, 0x4a, 0xbf, 0x8f, 0xba, 0x4b, 0xb4, 0x20, 0xd8, 0x9d, 0x2a, 0x04, 0x8a, 0x3c, 0x4f, 0x4c, 0x09, 0x2e, 0x37, 0xb6, 0x76];
+
+pub enum NextNoiseStep {
+       ActOne,
+       ActTwo,
+       ActThree,
+       NoiseComplete,
+}
+
+#[derive(PartialEq)]
+enum NoiseStep {
+       PreActOne,
+       PostActOne,
+       PostActTwo,
+       // When done swap noise_state for NoiseState::Finished
+}
+
+struct BidirectionalNoiseState {
+       h: [u8; 32],
+       ck: [u8; 32],
+}
+enum DirectionalNoiseState {
+       Outbound {
+               ie: SecretKey,
+       },
+       Inbound {
+               ie: Option<PublicKey>, // filled in if state >= PostActOne
+               re: Option<SecretKey>, // filled in if state >= PostActTwo
+               temp_k2: Option<[u8; 32]>, // filled in if state >= PostActTwo
+       }
+}
+enum NoiseState {
+       InProgress {
+               state: NoiseStep,
+               directional_state: DirectionalNoiseState,
+               bidirectional_state: BidirectionalNoiseState,
+       },
+       Finished {
+               sk: [u8; 32],
+               sn: u64,
+               sck: [u8; 32],
+               rk: [u8; 32],
+               rn: u64,
+               rck: [u8; 32],
+       }
+}
+
+pub struct PeerChannelEncryptor {
+       secp_ctx: Secp256k1<secp256k1::SignOnly>,
+       their_node_id: Option<PublicKey>, // filled in for outbound, or inbound after noise_state is Finished
+
+       noise_state: NoiseState,
+}
+
+impl PeerChannelEncryptor {
+       pub fn new_outbound(their_node_id: PublicKey, ephemeral_key: SecretKey) -> PeerChannelEncryptor {
+               let secp_ctx = Secp256k1::signing_only();
+
+               let mut sha = Sha256::engine();
+               sha.input(&NOISE_H);
+               sha.input(&their_node_id.serialize()[..]);
+               let h = Sha256::from_engine(sha).into_inner();
+
+               PeerChannelEncryptor {
+                       their_node_id: Some(their_node_id),
+                       secp_ctx: secp_ctx,
+                       noise_state: NoiseState::InProgress {
+                               state: NoiseStep::PreActOne,
+                               directional_state: DirectionalNoiseState::Outbound {
+                                       ie: ephemeral_key,
+                               },
+                               bidirectional_state: BidirectionalNoiseState {
+                                       h: h,
+                                       ck: NOISE_CK,
+                               },
+                       }
+               }
+       }
+
+       pub fn new_inbound(our_node_secret: &SecretKey) -> PeerChannelEncryptor {
+               let secp_ctx = Secp256k1::signing_only();
+
+               let mut sha = Sha256::engine();
+               sha.input(&NOISE_H);
+               let our_node_id = PublicKey::from_secret_key(&secp_ctx, our_node_secret);
+               sha.input(&our_node_id.serialize()[..]);
+               let h = Sha256::from_engine(sha).into_inner();
+
+               PeerChannelEncryptor {
+                       their_node_id: None,
+                       secp_ctx: secp_ctx,
+                       noise_state: NoiseState::InProgress {
+                               state: NoiseStep::PreActOne,
+                               directional_state: DirectionalNoiseState::Inbound {
+                                       ie: None,
+                                       re: None,
+                                       temp_k2: None,
+                               },
+                               bidirectional_state: BidirectionalNoiseState {
+                                       h: h,
+                                       ck: NOISE_CK,
+                               },
+                       }
+               }
+       }
+
+       #[inline]
+       fn encrypt_with_ad(res: &mut[u8], n: u64, key: &[u8; 32], h: &[u8], plaintext: &[u8]) {
+               let mut nonce = [0; 12];
+               nonce[4..].copy_from_slice(&byte_utils::le64_to_array(n));
+
+               let mut chacha = ChaCha20Poly1305RFC::new(key, &nonce, h);
+               let mut tag = [0; 16];
+               chacha.encrypt(plaintext, &mut res[0..plaintext.len()], &mut tag);
+               res[plaintext.len()..].copy_from_slice(&tag);
+       }
+
+       #[inline]
+       fn decrypt_with_ad(res: &mut[u8], n: u64, key: &[u8; 32], h: &[u8], cyphertext: &[u8]) -> Result<(), HandleError> {
+               let mut nonce = [0; 12];
+               nonce[4..].copy_from_slice(&byte_utils::le64_to_array(n));
+
+               let mut chacha = ChaCha20Poly1305RFC::new(key, &nonce, h);
+               if !chacha.decrypt(&cyphertext[0..cyphertext.len() - 16], res, &cyphertext[cyphertext.len() - 16..]) {
+                       return Err(HandleError{err: "Bad MAC", action: Some(msgs::ErrorAction::DisconnectPeer{ msg: None })});
+               }
+               Ok(())
+       }
+
+       fn hkdf_extract_expand(salt: &[u8], ikm: &[u8]) -> ([u8; 32], [u8; 32]) {
+               let mut hmac = HmacEngine::<Sha256>::new(salt);
+               hmac.input(ikm);
+               let prk = Hmac::from_engine(hmac).into_inner();
+               let mut hmac = HmacEngine::<Sha256>::new(&prk[..]);
+               hmac.input(&[1; 1]);
+               let t1 = Hmac::from_engine(hmac).into_inner();
+               let mut hmac = HmacEngine::<Sha256>::new(&prk[..]);
+               hmac.input(&t1);
+               hmac.input(&[2; 1]);
+               (t1, Hmac::from_engine(hmac).into_inner())
+       }
+
+       #[inline]
+       fn hkdf(state: &mut BidirectionalNoiseState, ss: SharedSecret) -> [u8; 32] {
+               let (t1, t2) = Self::hkdf_extract_expand(&state.ck, &ss[..]);
+               state.ck = t1;
+               t2
+       }
+
+       #[inline]
+       fn outbound_noise_act<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, state: &mut BidirectionalNoiseState, our_key: &SecretKey, their_key: &PublicKey) -> ([u8; 50], [u8; 32]) {
+               let our_pub = PublicKey::from_secret_key(secp_ctx, &our_key);
+
+               let mut sha = Sha256::engine();
+               sha.input(&state.h);
+               sha.input(&our_pub.serialize()[..]);
+               state.h = Sha256::from_engine(sha).into_inner();
+
+               let ss = SharedSecret::new(&their_key, &our_key);
+               let temp_k = PeerChannelEncryptor::hkdf(state, ss);
+
+               let mut res = [0; 50];
+               res[1..34].copy_from_slice(&our_pub.serialize()[..]);
+               PeerChannelEncryptor::encrypt_with_ad(&mut res[34..], 0, &temp_k, &state.h, &[0; 0]);
+
+               let mut sha = Sha256::engine();
+               sha.input(&state.h);
+               sha.input(&res[34..]);
+               state.h = Sha256::from_engine(sha).into_inner();
+
+               (res, temp_k)
+       }
+
+       #[inline]
+       fn inbound_noise_act(state: &mut BidirectionalNoiseState, act: &[u8], our_key: &SecretKey) -> Result<(PublicKey, [u8; 32]), HandleError> {
+               assert_eq!(act.len(), 50);
+
+               if act[0] != 0 {
+                       return Err(HandleError{err: "Unknown handshake version number", action: Some(msgs::ErrorAction::DisconnectPeer{ msg: None })});
+               }
+
+               let their_pub = match PublicKey::from_slice(&act[1..34]) {
+                       Err(_) => return Err(HandleError{err: "Invalid public key", action: Some(msgs::ErrorAction::DisconnectPeer{ msg: None })}),
+                       Ok(key) => key,
+               };
+
+               let mut sha = Sha256::engine();
+               sha.input(&state.h);
+               sha.input(&their_pub.serialize()[..]);
+               state.h = Sha256::from_engine(sha).into_inner();
+
+               let ss = SharedSecret::new(&their_pub, &our_key);
+               let temp_k = PeerChannelEncryptor::hkdf(state, ss);
+
+               let mut dec = [0; 0];
+               PeerChannelEncryptor::decrypt_with_ad(&mut dec, 0, &temp_k, &state.h, &act[34..])?;
+
+               let mut sha = Sha256::engine();
+               sha.input(&state.h);
+               sha.input(&act[34..]);
+               state.h = Sha256::from_engine(sha).into_inner();
+
+               Ok((their_pub, temp_k))
+       }
+
+       pub fn get_act_one(&mut self) -> [u8; 50] {
+               match self.noise_state {
+                       NoiseState::InProgress { ref mut state, ref directional_state, ref mut bidirectional_state } =>
+                               match directional_state {
+                                       &DirectionalNoiseState::Outbound { ref ie } => {
+                                               if *state != NoiseStep::PreActOne {
+                                                       panic!("Requested act at wrong step");
+                                               }
+
+                                               let (res, _) = PeerChannelEncryptor::outbound_noise_act(&self.secp_ctx, bidirectional_state, &ie, &self.their_node_id.unwrap());
+                                               *state = NoiseStep::PostActOne;
+                                               res
+                                       },
+                                       _ => panic!("Wrong direction for act"),
+                               },
+                       _ => panic!("Cannot get act one after noise handshake completes"),
+               }
+       }
+
+       pub fn process_act_one_with_keys(&mut self, act_one: &[u8], our_node_secret: &SecretKey, our_ephemeral: SecretKey) -> Result<[u8; 50], HandleError> {
+               assert_eq!(act_one.len(), 50);
+
+               match self.noise_state {
+                       NoiseState::InProgress { ref mut state, ref mut directional_state, ref mut bidirectional_state } =>
+                               match directional_state {
+                                       &mut DirectionalNoiseState::Inbound { ref mut ie, ref mut re, ref mut temp_k2 } => {
+                                               if *state != NoiseStep::PreActOne {
+                                                       panic!("Requested act at wrong step");
+                                               }
+
+                                               let (their_pub, _) = PeerChannelEncryptor::inbound_noise_act(bidirectional_state, act_one, &our_node_secret)?;
+                                               ie.get_or_insert(their_pub);
+
+                                               re.get_or_insert(our_ephemeral);
+
+                                               let (res, temp_k) = PeerChannelEncryptor::outbound_noise_act(&self.secp_ctx, bidirectional_state, &re.unwrap(), &ie.unwrap());
+                                               *temp_k2 = Some(temp_k);
+                                               *state = NoiseStep::PostActTwo;
+                                               Ok(res)
+                                       },
+                                       _ => panic!("Wrong direction for act"),
+                               },
+                       _ => panic!("Cannot get act one after noise handshake completes"),
+               }
+       }
+
+       pub fn process_act_two(&mut self, act_two: &[u8], our_node_secret: &SecretKey) -> Result<([u8; 66], PublicKey), HandleError> {
+               assert_eq!(act_two.len(), 50);
+
+               let final_hkdf;
+               let ck;
+               let res: [u8; 66] = match self.noise_state {
+                       NoiseState::InProgress { ref state, ref directional_state, ref mut bidirectional_state } =>
+                               match directional_state {
+                                       &DirectionalNoiseState::Outbound { ref ie } => {
+                                               if *state != NoiseStep::PostActOne {
+                                                       panic!("Requested act at wrong step");
+                                               }
+
+                                               let (re, temp_k2) = PeerChannelEncryptor::inbound_noise_act(bidirectional_state, act_two, &ie)?;
+
+                                               let mut res = [0; 66];
+                                               let our_node_id = PublicKey::from_secret_key(&self.secp_ctx, &our_node_secret);
+
+                                               PeerChannelEncryptor::encrypt_with_ad(&mut res[1..50], 1, &temp_k2, &bidirectional_state.h, &our_node_id.serialize()[..]);
+
+                                               let mut sha = Sha256::engine();
+                                               sha.input(&bidirectional_state.h);
+                                               sha.input(&res[1..50]);
+                                               bidirectional_state.h = Sha256::from_engine(sha).into_inner();
+
+                                               let ss = SharedSecret::new(&re, our_node_secret);
+                                               let temp_k = PeerChannelEncryptor::hkdf(bidirectional_state, ss);
+
+                                               PeerChannelEncryptor::encrypt_with_ad(&mut res[50..], 0, &temp_k, &bidirectional_state.h, &[0; 0]);
+                                               final_hkdf = Self::hkdf_extract_expand(&bidirectional_state.ck, &[0; 0]);
+                                               ck = bidirectional_state.ck.clone();
+                                               res
+                                       },
+                                       _ => panic!("Wrong direction for act"),
+                               },
+                       _ => panic!("Cannot get act one after noise handshake completes"),
+               };
+
+               let (sk, rk) = final_hkdf;
+               self.noise_state = NoiseState::Finished {
+                       sk: sk,
+                       sn: 0,
+                       sck: ck.clone(),
+                       rk: rk,
+                       rn: 0,
+                       rck: ck,
+               };
+
+               Ok((res, self.their_node_id.unwrap().clone()))
+       }
+
+       pub fn process_act_three(&mut self, act_three: &[u8]) -> Result<PublicKey, HandleError> {
+               assert_eq!(act_three.len(), 66);
+
+               let final_hkdf;
+               let ck;
+               match self.noise_state {
+                       NoiseState::InProgress { ref state, ref directional_state, ref mut bidirectional_state } =>
+                               match directional_state {
+                                       &DirectionalNoiseState::Inbound { ie: _, ref re, ref temp_k2 } => {
+                                               if *state != NoiseStep::PostActTwo {
+                                                       panic!("Requested act at wrong step");
+                                               }
+                                               if act_three[0] != 0 {
+                                                       return Err(HandleError{err: "Unknown handshake version number", action: Some(msgs::ErrorAction::DisconnectPeer{ msg: None })});
+                                               }
+
+                                               let mut their_node_id = [0; 33];
+                                               PeerChannelEncryptor::decrypt_with_ad(&mut their_node_id, 1, &temp_k2.unwrap(), &bidirectional_state.h, &act_three[1..50])?;
+                                               self.their_node_id = Some(match PublicKey::from_slice(&their_node_id) {
+                                                       Ok(key) => key,
+                                                       Err(_) => return Err(HandleError{err: "Bad node_id from peer", action: Some(msgs::ErrorAction::DisconnectPeer{ msg: None })}),
+                                               });
+
+                                               let mut sha = Sha256::engine();
+                                               sha.input(&bidirectional_state.h);
+                                               sha.input(&act_three[1..50]);
+                                               bidirectional_state.h = Sha256::from_engine(sha).into_inner();
+
+                                               let ss = SharedSecret::new(&self.their_node_id.unwrap(), &re.unwrap());
+                                               let temp_k = PeerChannelEncryptor::hkdf(bidirectional_state, ss);
+
+                                               PeerChannelEncryptor::decrypt_with_ad(&mut [0; 0], 0, &temp_k, &bidirectional_state.h, &act_three[50..])?;
+                                               final_hkdf = Self::hkdf_extract_expand(&bidirectional_state.ck, &[0; 0]);
+                                               ck = bidirectional_state.ck.clone();
+                                       },
+                                       _ => panic!("Wrong direction for act"),
+                               },
+                       _ => panic!("Cannot get act one after noise handshake completes"),
+               }
+
+               let (rk, sk) = final_hkdf;
+               self.noise_state = NoiseState::Finished {
+                       sk: sk,
+                       sn: 0,
+                       sck: ck.clone(),
+                       rk: rk,
+                       rn: 0,
+                       rck: ck,
+               };
+
+               Ok(self.their_node_id.unwrap().clone())
+       }
+
+       /// Encrypts the given message, returning the encrypted version
+       /// panics if msg.len() > 65535 or Noise handshake has not finished.
+       pub fn encrypt_message(&mut self, msg: &[u8]) -> Vec<u8> {
+               if msg.len() > 65535 {
+                       panic!("Attempted to encrypt message longer than 65535 bytes!");
+               }
+
+               let mut res = Vec::with_capacity(msg.len() + 16*2 + 2);
+               res.resize(msg.len() + 16*2 + 2, 0);
+
+               match self.noise_state {
+                       NoiseState::Finished { ref mut sk, ref mut sn, ref mut sck, rk: _, rn: _, rck: _ } => {
+                               if *sn >= 1000 {
+                                       let (new_sck, new_sk) = Self::hkdf_extract_expand(sck, sk);
+                                       *sck = new_sck;
+                                       *sk = new_sk;
+                                       *sn = 0;
+                               }
+
+                               Self::encrypt_with_ad(&mut res[0..16+2], *sn, sk, &[0; 0], &byte_utils::be16_to_array(msg.len() as u16));
+                               *sn += 1;
+
+                               Self::encrypt_with_ad(&mut res[16+2..], *sn, sk, &[0; 0], msg);
+                               *sn += 1;
+                       },
+                       _ => panic!("Tried to encrypt a message prior to noise handshake completion"),
+               }
+
+               res
+       }
+
+       /// Decrypts a message length header from the remote peer.
+       /// panics if noise handshake has not yet finished or msg.len() != 18
+       pub fn decrypt_length_header(&mut self, msg: &[u8]) -> Result<u16, HandleError> {
+               assert_eq!(msg.len(), 16+2);
+
+               match self.noise_state {
+                       NoiseState::Finished { sk: _, sn: _, sck: _, ref mut rk, ref mut rn, ref mut rck } => {
+                               if *rn >= 1000 {
+                                       let (new_rck, new_rk) = Self::hkdf_extract_expand(rck, rk);
+                                       *rck = new_rck;
+                                       *rk = new_rk;
+                                       *rn = 0;
+                               }
+
+                               let mut res = [0; 2];
+                               Self::decrypt_with_ad(&mut res, *rn, rk, &[0; 0], msg)?;
+                               *rn += 1;
+                               Ok(byte_utils::slice_to_be16(&res))
+                       },
+                       _ => panic!("Tried to encrypt a message prior to noise handshake completion"),
+               }
+       }
+
+       /// Decrypts the given message.
+       /// panics if msg.len() > 65535 + 16
+       pub fn decrypt_message(&mut self, msg: &[u8]) -> Result<Vec<u8>, HandleError> {
+               if msg.len() > 65535 + 16 {
+                       panic!("Attempted to encrypt message longer than 65535 bytes!");
+               }
+
+               match self.noise_state {
+                       NoiseState::Finished { sk: _, sn: _, sck: _, ref rk, ref mut rn, rck: _ } => {
+                               let mut res = Vec::with_capacity(msg.len() - 16);
+                               res.resize(msg.len() - 16, 0);
+                               Self::decrypt_with_ad(&mut res[..], *rn, rk, &[0; 0], msg)?;
+                               *rn += 1;
+
+                               Ok(res)
+                       },
+                       _ => panic!("Tried to encrypt a message prior to noise handshake completion"),
+               }
+       }
+
+       pub fn get_noise_step(&self) -> NextNoiseStep {
+               match self.noise_state {
+                       NoiseState::InProgress {ref state, ..} => {
+                               match state {
+                                       &NoiseStep::PreActOne => NextNoiseStep::ActOne,
+                                       &NoiseStep::PostActOne => NextNoiseStep::ActTwo,
+                                       &NoiseStep::PostActTwo => NextNoiseStep::ActThree,
+                               }
+                       },
+                       NoiseState::Finished {..} => NextNoiseStep::NoiseComplete,
+               }
+       }
+
+       pub fn is_ready_for_encryption(&self) -> bool {
+               match self.noise_state {
+                       NoiseState::InProgress {..} => { false },
+                       NoiseState::Finished {..} => { true }
+               }
+       }
+}
+
+#[cfg(test)]
+mod tests {
+       use secp256k1::key::{PublicKey,SecretKey};
+
+       use hex;
+
+       use ln::peer_channel_encryptor::{PeerChannelEncryptor,NoiseState};
+
+       fn get_outbound_peer_for_initiator_test_vectors() -> PeerChannelEncryptor {
+               let their_node_id = PublicKey::from_slice(&hex::decode("028d7500dd4c12685d1f568b4c2b5048e8534b873319f3a8daa612b469132ec7f7").unwrap()[..]).unwrap();
+
+               let mut outbound_peer = PeerChannelEncryptor::new_outbound(their_node_id, SecretKey::from_slice(&hex::decode("1212121212121212121212121212121212121212121212121212121212121212").unwrap()[..]).unwrap());
+               assert_eq!(outbound_peer.get_act_one()[..], hex::decode("00036360e856310ce5d294e8be33fc807077dc56ac80d95d9cd4ddbd21325eff73f70df6086551151f58b8afe6c195782c6a").unwrap()[..]);
+               outbound_peer
+       }
+
+       #[test]
+       fn noise_initiator_test_vectors() {
+               let our_node_id = SecretKey::from_slice(&hex::decode("1111111111111111111111111111111111111111111111111111111111111111").unwrap()[..]).unwrap();
+
+               {
+                       // transport-initiator successful handshake
+                       let mut outbound_peer = get_outbound_peer_for_initiator_test_vectors();
+
+                       let act_two = hex::decode("0002466d7fcae563e5cb09a0d1870bb580344804617879a14949cf22285f1bae3f276e2470b93aac583c9ef6eafca3f730ae").unwrap().to_vec();
+                       assert_eq!(outbound_peer.process_act_two(&act_two[..], &our_node_id).unwrap().0[..], hex::decode("00b9e3a702e93e3a9948c2ed6e5fd7590a6e1c3a0344cfc9d5b57357049aa22355361aa02e55a8fc28fef5bd6d71ad0c38228dc68b1c466263b47fdf31e560e139ba").unwrap()[..]);
+
+                       match outbound_peer.noise_state {
+                               NoiseState::Finished { sk, sn, sck, rk, rn, rck } => {
+                                       assert_eq!(sk, hex::decode("969ab31b4d288cedf6218839b27a3e2140827047f2c0f01bf5c04435d43511a9").unwrap()[..]);
+                                       assert_eq!(sn, 0);
+                                       assert_eq!(sck, hex::decode("919219dbb2920afa8db80f9a51787a840bcf111ed8d588caf9ab4be716e42b01").unwrap()[..]);
+                                       assert_eq!(rk, hex::decode("bb9020b8965f4df047e07f955f3c4b88418984aadc5cdb35096b9ea8fa5c3442").unwrap()[..]);
+                                       assert_eq!(rn, 0);
+                                       assert_eq!(rck, hex::decode("919219dbb2920afa8db80f9a51787a840bcf111ed8d588caf9ab4be716e42b01").unwrap()[..]);
+                               },
+                               _ => panic!()
+                       }
+               }
+               {
+                       // transport-initiator act2 short read test
+                       // Can't actually test this cause process_act_two requires you pass the right length!
+               }
+               {
+                       // transport-initiator act2 bad version test
+                       let mut outbound_peer = get_outbound_peer_for_initiator_test_vectors();
+
+                       let act_two = hex::decode("0102466d7fcae563e5cb09a0d1870bb580344804617879a14949cf22285f1bae3f276e2470b93aac583c9ef6eafca3f730ae").unwrap().to_vec();
+                       assert!(outbound_peer.process_act_two(&act_two[..], &our_node_id).is_err());
+               }
+
+               {
+                       // transport-initiator act2 bad key serialization test
+                       let mut outbound_peer = get_outbound_peer_for_initiator_test_vectors();
+
+                       let act_two = hex::decode("0004466d7fcae563e5cb09a0d1870bb580344804617879a14949cf22285f1bae3f276e2470b93aac583c9ef6eafca3f730ae").unwrap().to_vec();
+                       assert!(outbound_peer.process_act_two(&act_two[..], &our_node_id).is_err());
+               }
+
+               {
+                       // transport-initiator act2 bad MAC test
+                       let mut outbound_peer = get_outbound_peer_for_initiator_test_vectors();
+
+                       let act_two = hex::decode("0002466d7fcae563e5cb09a0d1870bb580344804617879a14949cf22285f1bae3f276e2470b93aac583c9ef6eafca3f730af").unwrap().to_vec();
+                       assert!(outbound_peer.process_act_two(&act_two[..], &our_node_id).is_err());
+               }
+       }
+
+       #[test]
+       fn noise_responder_test_vectors() {
+               let our_node_id = SecretKey::from_slice(&hex::decode("2121212121212121212121212121212121212121212121212121212121212121").unwrap()[..]).unwrap();
+               let our_ephemeral = SecretKey::from_slice(&hex::decode("2222222222222222222222222222222222222222222222222222222222222222").unwrap()[..]).unwrap();
+
+               {
+                       // transport-responder successful handshake
+                       let mut inbound_peer = PeerChannelEncryptor::new_inbound(&our_node_id);
+
+                       let act_one = hex::decode("00036360e856310ce5d294e8be33fc807077dc56ac80d95d9cd4ddbd21325eff73f70df6086551151f58b8afe6c195782c6a").unwrap().to_vec();
+                       assert_eq!(inbound_peer.process_act_one_with_keys(&act_one[..], &our_node_id, our_ephemeral.clone()).unwrap()[..], hex::decode("0002466d7fcae563e5cb09a0d1870bb580344804617879a14949cf22285f1bae3f276e2470b93aac583c9ef6eafca3f730ae").unwrap()[..]);
+
+                       let act_three = hex::decode("00b9e3a702e93e3a9948c2ed6e5fd7590a6e1c3a0344cfc9d5b57357049aa22355361aa02e55a8fc28fef5bd6d71ad0c38228dc68b1c466263b47fdf31e560e139ba").unwrap().to_vec();
+                       // test vector doesn't specify the initiator static key, but it's the same as the one
+                       // from transport-initiator successful handshake
+                       assert_eq!(inbound_peer.process_act_three(&act_three[..]).unwrap().serialize()[..], hex::decode("034f355bdcb7cc0af728ef3cceb9615d90684bb5b2ca5f859ab0f0b704075871aa").unwrap()[..]);
+
+                       match inbound_peer.noise_state {
+                               NoiseState::Finished { sk, sn, sck, rk, rn, rck } => {
+                                       assert_eq!(sk, hex::decode("bb9020b8965f4df047e07f955f3c4b88418984aadc5cdb35096b9ea8fa5c3442").unwrap()[..]);
+                                       assert_eq!(sn, 0);
+                                       assert_eq!(sck, hex::decode("919219dbb2920afa8db80f9a51787a840bcf111ed8d588caf9ab4be716e42b01").unwrap()[..]);
+                                       assert_eq!(rk, hex::decode("969ab31b4d288cedf6218839b27a3e2140827047f2c0f01bf5c04435d43511a9").unwrap()[..]);
+                                       assert_eq!(rn, 0);
+                                       assert_eq!(rck, hex::decode("919219dbb2920afa8db80f9a51787a840bcf111ed8d588caf9ab4be716e42b01").unwrap()[..]);
+                               },
+                               _ => panic!()
+                       }
+               }
+               {
+                       // transport-responder act1 short read test
+                       // Can't actually test this cause process_act_one requires you pass the right length!
+               }
+               {
+                       // transport-responder act1 bad version test
+                       let mut inbound_peer = PeerChannelEncryptor::new_inbound(&our_node_id);
+
+                       let act_one = hex::decode("01036360e856310ce5d294e8be33fc807077dc56ac80d95d9cd4ddbd21325eff73f70df6086551151f58b8afe6c195782c6a").unwrap().to_vec();
+                       assert!(inbound_peer.process_act_one_with_keys(&act_one[..], &our_node_id, our_ephemeral.clone()).is_err());
+               }
+               {
+                       // transport-responder act1 bad key serialization test
+                       let mut inbound_peer = PeerChannelEncryptor::new_inbound(&our_node_id);
+
+                       let act_one =hex::decode("00046360e856310ce5d294e8be33fc807077dc56ac80d95d9cd4ddbd21325eff73f70df6086551151f58b8afe6c195782c6a").unwrap().to_vec();
+                       assert!(inbound_peer.process_act_one_with_keys(&act_one[..], &our_node_id, our_ephemeral.clone()).is_err());
+               }
+               {
+                       // transport-responder act1 bad MAC test
+                       let mut inbound_peer = PeerChannelEncryptor::new_inbound(&our_node_id);
+
+                       let act_one = hex::decode("00036360e856310ce5d294e8be33fc807077dc56ac80d95d9cd4ddbd21325eff73f70df6086551151f58b8afe6c195782c6b").unwrap().to_vec();
+                       assert!(inbound_peer.process_act_one_with_keys(&act_one[..], &our_node_id, our_ephemeral.clone()).is_err());
+               }
+               {
+                       // transport-responder act3 bad version test
+                       let mut inbound_peer = PeerChannelEncryptor::new_inbound(&our_node_id);
+
+                       let act_one = hex::decode("00036360e856310ce5d294e8be33fc807077dc56ac80d95d9cd4ddbd21325eff73f70df6086551151f58b8afe6c195782c6a").unwrap().to_vec();
+                       assert_eq!(inbound_peer.process_act_one_with_keys(&act_one[..], &our_node_id, our_ephemeral.clone()).unwrap()[..], hex::decode("0002466d7fcae563e5cb09a0d1870bb580344804617879a14949cf22285f1bae3f276e2470b93aac583c9ef6eafca3f730ae").unwrap()[..]);
+
+                       let act_three = hex::decode("01b9e3a702e93e3a9948c2ed6e5fd7590a6e1c3a0344cfc9d5b57357049aa22355361aa02e55a8fc28fef5bd6d71ad0c38228dc68b1c466263b47fdf31e560e139ba").unwrap().to_vec();
+                       assert!(inbound_peer.process_act_three(&act_three[..]).is_err());
+               }
+               {
+                       // transport-responder act3 short read test
+                       // Can't actually test this cause process_act_three requires you pass the right length!
+               }
+               {
+                       // transport-responder act3 bad MAC for ciphertext test
+                       let mut inbound_peer = PeerChannelEncryptor::new_inbound(&our_node_id);
+
+                       let act_one = hex::decode("00036360e856310ce5d294e8be33fc807077dc56ac80d95d9cd4ddbd21325eff73f70df6086551151f58b8afe6c195782c6a").unwrap().to_vec();
+                       assert_eq!(inbound_peer.process_act_one_with_keys(&act_one[..], &our_node_id, our_ephemeral.clone()).unwrap()[..], hex::decode("0002466d7fcae563e5cb09a0d1870bb580344804617879a14949cf22285f1bae3f276e2470b93aac583c9ef6eafca3f730ae").unwrap()[..]);
+
+                       let act_three = hex::decode("00c9e3a702e93e3a9948c2ed6e5fd7590a6e1c3a0344cfc9d5b57357049aa22355361aa02e55a8fc28fef5bd6d71ad0c38228dc68b1c466263b47fdf31e560e139ba").unwrap().to_vec();
+                       assert!(inbound_peer.process_act_three(&act_three[..]).is_err());
+               }
+               {
+                       // transport-responder act3 bad rs test
+                       let mut inbound_peer = PeerChannelEncryptor::new_inbound(&our_node_id);
+
+                       let act_one = hex::decode("00036360e856310ce5d294e8be33fc807077dc56ac80d95d9cd4ddbd21325eff73f70df6086551151f58b8afe6c195782c6a").unwrap().to_vec();
+                       assert_eq!(inbound_peer.process_act_one_with_keys(&act_one[..], &our_node_id, our_ephemeral.clone()).unwrap()[..], hex::decode("0002466d7fcae563e5cb09a0d1870bb580344804617879a14949cf22285f1bae3f276e2470b93aac583c9ef6eafca3f730ae").unwrap()[..]);
+
+                       let act_three = hex::decode("00bfe3a702e93e3a9948c2ed6e5fd7590a6e1c3a0344cfc9d5b57357049aa2235536ad09a8ee351870c2bb7f78b754a26c6cef79a98d25139c856d7efd252c2ae73c").unwrap().to_vec();
+                       assert!(inbound_peer.process_act_three(&act_three[..]).is_err());
+               }
+               {
+                       // transport-responder act3 bad MAC test
+                       let mut inbound_peer = PeerChannelEncryptor::new_inbound(&our_node_id);
+
+                       let act_one = hex::decode("00036360e856310ce5d294e8be33fc807077dc56ac80d95d9cd4ddbd21325eff73f70df6086551151f58b8afe6c195782c6a").unwrap().to_vec();
+                       assert_eq!(inbound_peer.process_act_one_with_keys(&act_one[..], &our_node_id, our_ephemeral.clone()).unwrap()[..], hex::decode("0002466d7fcae563e5cb09a0d1870bb580344804617879a14949cf22285f1bae3f276e2470b93aac583c9ef6eafca3f730ae").unwrap()[..]);
+
+                       let act_three = hex::decode("00b9e3a702e93e3a9948c2ed6e5fd7590a6e1c3a0344cfc9d5b57357049aa22355361aa02e55a8fc28fef5bd6d71ad0c38228dc68b1c466263b47fdf31e560e139bb").unwrap().to_vec();
+                       assert!(inbound_peer.process_act_three(&act_three[..]).is_err());
+               }
+       }
+
+
+       #[test]
+       fn message_encryption_decryption_test_vectors() {
+               // We use the same keys as the initiator and responder test vectors, so we copy those tests
+               // here and use them to encrypt.
+               let mut outbound_peer = get_outbound_peer_for_initiator_test_vectors();
+
+               {
+                       let our_node_id = SecretKey::from_slice(&hex::decode("1111111111111111111111111111111111111111111111111111111111111111").unwrap()[..]).unwrap();
+
+                       let act_two = hex::decode("0002466d7fcae563e5cb09a0d1870bb580344804617879a14949cf22285f1bae3f276e2470b93aac583c9ef6eafca3f730ae").unwrap().to_vec();
+                       assert_eq!(outbound_peer.process_act_two(&act_two[..], &our_node_id).unwrap().0[..], hex::decode("00b9e3a702e93e3a9948c2ed6e5fd7590a6e1c3a0344cfc9d5b57357049aa22355361aa02e55a8fc28fef5bd6d71ad0c38228dc68b1c466263b47fdf31e560e139ba").unwrap()[..]);
+
+                       match outbound_peer.noise_state {
+                               NoiseState::Finished { sk, sn, sck, rk, rn, rck } => {
+                                       assert_eq!(sk, hex::decode("969ab31b4d288cedf6218839b27a3e2140827047f2c0f01bf5c04435d43511a9").unwrap()[..]);
+                                       assert_eq!(sn, 0);
+                                       assert_eq!(sck, hex::decode("919219dbb2920afa8db80f9a51787a840bcf111ed8d588caf9ab4be716e42b01").unwrap()[..]);
+                                       assert_eq!(rk, hex::decode("bb9020b8965f4df047e07f955f3c4b88418984aadc5cdb35096b9ea8fa5c3442").unwrap()[..]);
+                                       assert_eq!(rn, 0);
+                                       assert_eq!(rck, hex::decode("919219dbb2920afa8db80f9a51787a840bcf111ed8d588caf9ab4be716e42b01").unwrap()[..]);
+                               },
+                               _ => panic!()
+                       }
+               }
+
+               let mut inbound_peer;
+
+               {
+                       // transport-responder successful handshake
+                       let our_node_id = SecretKey::from_slice(&hex::decode("2121212121212121212121212121212121212121212121212121212121212121").unwrap()[..]).unwrap();
+                       let our_ephemeral = SecretKey::from_slice(&hex::decode("2222222222222222222222222222222222222222222222222222222222222222").unwrap()[..]).unwrap();
+
+                       inbound_peer = PeerChannelEncryptor::new_inbound(&our_node_id);
+
+                       let act_one = hex::decode("00036360e856310ce5d294e8be33fc807077dc56ac80d95d9cd4ddbd21325eff73f70df6086551151f58b8afe6c195782c6a").unwrap().to_vec();
+                       assert_eq!(inbound_peer.process_act_one_with_keys(&act_one[..], &our_node_id, our_ephemeral.clone()).unwrap()[..], hex::decode("0002466d7fcae563e5cb09a0d1870bb580344804617879a14949cf22285f1bae3f276e2470b93aac583c9ef6eafca3f730ae").unwrap()[..]);
+
+                       let act_three = hex::decode("00b9e3a702e93e3a9948c2ed6e5fd7590a6e1c3a0344cfc9d5b57357049aa22355361aa02e55a8fc28fef5bd6d71ad0c38228dc68b1c466263b47fdf31e560e139ba").unwrap().to_vec();
+                       // test vector doesn't specify the initiator static key, but it's the same as the one
+                       // from transport-initiator successful handshake
+                       assert_eq!(inbound_peer.process_act_three(&act_three[..]).unwrap().serialize()[..], hex::decode("034f355bdcb7cc0af728ef3cceb9615d90684bb5b2ca5f859ab0f0b704075871aa").unwrap()[..]);
+
+                       match inbound_peer.noise_state {
+                               NoiseState::Finished { sk, sn, sck, rk, rn, rck } => {
+                                       assert_eq!(sk, hex::decode("bb9020b8965f4df047e07f955f3c4b88418984aadc5cdb35096b9ea8fa5c3442").unwrap()[..]);
+                                       assert_eq!(sn, 0);
+                                       assert_eq!(sck, hex::decode("919219dbb2920afa8db80f9a51787a840bcf111ed8d588caf9ab4be716e42b01").unwrap()[..]);
+                                       assert_eq!(rk, hex::decode("969ab31b4d288cedf6218839b27a3e2140827047f2c0f01bf5c04435d43511a9").unwrap()[..]);
+                                       assert_eq!(rn, 0);
+                                       assert_eq!(rck, hex::decode("919219dbb2920afa8db80f9a51787a840bcf111ed8d588caf9ab4be716e42b01").unwrap()[..]);
+                               },
+                               _ => panic!()
+                       }
+               }
+
+               for i in 0..1005 {
+                       let msg = [0x68, 0x65, 0x6c, 0x6c, 0x6f];
+                       let res = outbound_peer.encrypt_message(&msg);
+                       assert_eq!(res.len(), 5 + 2*16 + 2);
+
+                       let len_header = res[0..2+16].to_vec();
+                       assert_eq!(inbound_peer.decrypt_length_header(&len_header[..]).unwrap() as usize, msg.len());
+                       assert_eq!(inbound_peer.decrypt_message(&res[2+16..]).unwrap()[..], msg[..]);
+
+                       if i == 0 {
+                               assert_eq!(res, hex::decode("cf2b30ddf0cf3f80e7c35a6e6730b59fe802473180f396d88a8fb0db8cbcf25d2f214cf9ea1d95").unwrap());
+                       } else if i == 1 {
+                               assert_eq!(res, hex::decode("72887022101f0b6753e0c7de21657d35a4cb2a1f5cde2650528bbc8f837d0f0d7ad833b1a256a1").unwrap());
+                       } else if i == 500 {
+                               assert_eq!(res, hex::decode("178cb9d7387190fa34db9c2d50027d21793c9bc2d40b1e14dcf30ebeeeb220f48364f7a4c68bf8").unwrap());
+                       } else if i == 501 {
+                               assert_eq!(res, hex::decode("1b186c57d44eb6de4c057c49940d79bb838a145cb528d6e8fd26dbe50a60ca2c104b56b60e45bd").unwrap());
+                       } else if i == 1000 {
+                               assert_eq!(res, hex::decode("4a2f3cc3b5e78ddb83dcb426d9863d9d9a723b0337c89dd0b005d89f8d3c05c52b76b29b740f09").unwrap());
+                       } else if i == 1001 {
+                               assert_eq!(res, hex::decode("2ecd8c8a5629d0d02ab457a0fdd0f7b90a192cd46be5ecb6ca570bfc5e268338b1a16cf4ef2d36").unwrap());
+                       }
+               }
+       }
+}
diff --git a/lightning/src/ln/peer_handler.rs b/lightning/src/ln/peer_handler.rs
new file mode 100644 (file)
index 0000000..8094f25
--- /dev/null
@@ -0,0 +1,1183 @@
+//! Top level peer message handling and socket handling logic lives here.
+//!
+//! Instead of actually servicing sockets ourselves we require that you implement the
+//! SocketDescriptor interface and use that to receive actions which you should perform on the
+//! socket, and call into PeerManager with bytes read from the socket. The PeerManager will then
+//! call into the provided message handlers (probably a ChannelManager and Router) with messages
+//! they should handle, and encoding/sending response messages.
+
+use secp256k1::key::{SecretKey,PublicKey};
+
+use ln::msgs;
+use util::ser::{Writeable, Writer, Readable};
+use ln::peer_channel_encryptor::{PeerChannelEncryptor,NextNoiseStep};
+use util::byte_utils;
+use util::events::{MessageSendEvent};
+use util::logger::Logger;
+
+use std::collections::{HashMap,hash_map,HashSet,LinkedList};
+use std::sync::{Arc, Mutex};
+use std::sync::atomic::{AtomicUsize, Ordering};
+use std::{cmp,error,hash,fmt};
+
+use bitcoin_hashes::sha256::Hash as Sha256;
+use bitcoin_hashes::sha256::HashEngine as Sha256Engine;
+use bitcoin_hashes::{HashEngine, Hash};
+
+/// Provides references to trait impls which handle different types of messages.
+pub struct MessageHandler {
+       /// A message handler which handles messages specific to channels. Usually this is just a
+       /// ChannelManager object.
+       pub chan_handler: Arc<msgs::ChannelMessageHandler>,
+       /// A message handler which handles messages updating our knowledge of the network channel
+       /// graph. Usually this is just a Router object.
+       pub route_handler: Arc<msgs::RoutingMessageHandler>,
+}
+
+/// Provides an object which can be used to send data to and which uniquely identifies a connection
+/// to a remote host. You will need to be able to generate multiple of these which meet Eq and
+/// implement Hash to meet the PeerManager API.
+///
+/// For efficiency, Clone should be relatively cheap for this type.
+///
+/// You probably want to just extend an int and put a file descriptor in a struct and implement
+/// send_data. Note that if you are using a higher-level net library that may close() itself, be
+/// careful to ensure you don't have races whereby you might register a new connection with an fd
+/// the same as a yet-to-be-disconnect_event()-ed.
+pub trait SocketDescriptor : cmp::Eq + hash::Hash + Clone {
+       /// Attempts to send some data from the given slice to the peer.
+       ///
+       /// Returns the amount of data which was sent, possibly 0 if the socket has since disconnected.
+       /// Note that in the disconnected case, a disconnect_event must still fire and further write
+       /// attempts may occur until that time.
+       ///
+       /// If the returned size is smaller than data.len(), a write_available event must
+       /// trigger the next time more data can be written. Additionally, until the a send_data event
+       /// completes fully, no further read_events should trigger on the same peer!
+       ///
+       /// If a read_event on this descriptor had previously returned true (indicating that read
+       /// events should be paused to prevent DoS in the send buffer), resume_read may be set
+       /// indicating that read events on this descriptor should resume. A resume_read of false does
+       /// *not* imply that further read events should be paused.
+       fn send_data(&mut self, data: &[u8], resume_read: bool) -> usize;
+       /// Disconnect the socket pointed to by this SocketDescriptor. Once this function returns, no
+       /// more calls to write_event, read_event or disconnect_event may be made with this descriptor.
+       /// No disconnect_event should be generated as a result of this call, though obviously races
+       /// may occur whereby disconnect_socket is called after a call to disconnect_event but prior to
+       /// that event completing.
+       fn disconnect_socket(&mut self);
+}
+
+/// Error for PeerManager errors. If you get one of these, you must disconnect the socket and
+/// generate no further read/write_events for the descriptor, only triggering a single
+/// disconnect_event (unless it was provided in response to a new_*_connection event, in which case
+/// no such disconnect_event must be generated and the socket be silently disconencted).
+pub struct PeerHandleError {
+       /// Used to indicate that we probably can't make any future connections to this peer, implying
+       /// we should go ahead and force-close any channels we have with it.
+       no_connection_possible: bool,
+}
+impl fmt::Debug for PeerHandleError {
+       fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
+               formatter.write_str("Peer Sent Invalid Data")
+       }
+}
+impl fmt::Display for PeerHandleError {
+       fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
+               formatter.write_str("Peer Sent Invalid Data")
+       }
+}
+impl error::Error for PeerHandleError {
+       fn description(&self) -> &str {
+               "Peer Sent Invalid Data"
+       }
+}
+
+enum InitSyncTracker{
+       NoSyncRequested,
+       ChannelsSyncing(u64),
+       NodesSyncing(PublicKey),
+}
+
+struct Peer {
+       channel_encryptor: PeerChannelEncryptor,
+       outbound: bool,
+       their_node_id: Option<PublicKey>,
+       their_global_features: Option<msgs::GlobalFeatures>,
+       their_local_features: Option<msgs::LocalFeatures>,
+
+       pending_outbound_buffer: LinkedList<Vec<u8>>,
+       pending_outbound_buffer_first_msg_offset: usize,
+       awaiting_write_event: bool,
+
+       pending_read_buffer: Vec<u8>,
+       pending_read_buffer_pos: usize,
+       pending_read_is_header: bool,
+
+       sync_status: InitSyncTracker,
+}
+
+impl Peer {
+       /// Returns true if the channel announcements/updates for the given channel should be
+       /// forwarded to this peer.
+       /// If we are sending our routing table to this peer and we have not yet sent channel
+       /// announcements/updates for the given channel_id then we will send it when we get to that
+       /// point and we shouldn't send it yet to avoid sending duplicate updates. If we've already
+       /// sent the old versions, we should send the update, and so return true here.
+       fn should_forward_channel(&self, channel_id: u64)->bool{
+               match self.sync_status {
+                       InitSyncTracker::NoSyncRequested => true,
+                       InitSyncTracker::ChannelsSyncing(i) => i < channel_id,
+                       InitSyncTracker::NodesSyncing(_) => true,
+               }
+       }
+}
+
+struct PeerHolder<Descriptor: SocketDescriptor> {
+       peers: HashMap<Descriptor, Peer>,
+       /// Added to by do_read_event for cases where we pushed a message onto the send buffer but
+       /// didn't call do_attempt_write_data to avoid reentrancy. Cleared in process_events()
+       peers_needing_send: HashSet<Descriptor>,
+       /// Only add to this set when noise completes:
+       node_id_to_descriptor: HashMap<PublicKey, Descriptor>,
+}
+struct MutPeerHolder<'a, Descriptor: SocketDescriptor + 'a> {
+       peers: &'a mut HashMap<Descriptor, Peer>,
+       peers_needing_send: &'a mut HashSet<Descriptor>,
+       node_id_to_descriptor: &'a mut HashMap<PublicKey, Descriptor>,
+}
+impl<Descriptor: SocketDescriptor> PeerHolder<Descriptor> {
+       fn borrow_parts(&mut self) -> MutPeerHolder<Descriptor> {
+               MutPeerHolder {
+                       peers: &mut self.peers,
+                       peers_needing_send: &mut self.peers_needing_send,
+                       node_id_to_descriptor: &mut self.node_id_to_descriptor,
+               }
+       }
+}
+
+#[cfg(not(any(target_pointer_width = "32", target_pointer_width = "64")))]
+fn _check_usize_is_32_or_64() {
+       // See below, less than 32 bit pointers may be unsafe here!
+       unsafe { mem::transmute::<*const usize, [u8; 4]>(panic!()); }
+}
+
+/// A PeerManager manages a set of peers, described by their SocketDescriptor and marshalls socket
+/// events into messages which it passes on to its MessageHandlers.
+pub struct PeerManager<Descriptor: SocketDescriptor> {
+       message_handler: MessageHandler,
+       peers: Mutex<PeerHolder<Descriptor>>,
+       our_node_secret: SecretKey,
+       ephemeral_key_midstate: Sha256Engine,
+
+       // Usize needs to be at least 32 bits to avoid overflowing both low and high. If usize is 64
+       // bits we will never realistically count into high:
+       peer_counter_low: AtomicUsize,
+       peer_counter_high: AtomicUsize,
+
+       initial_syncs_sent: AtomicUsize,
+       logger: Arc<Logger>,
+}
+
+struct VecWriter(Vec<u8>);
+impl Writer for VecWriter {
+       fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
+               self.0.extend_from_slice(buf);
+               Ok(())
+       }
+       fn size_hint(&mut self, size: usize) {
+               self.0.reserve_exact(size);
+       }
+}
+
+macro_rules! encode_msg {
+       ($msg: expr, $msg_code: expr) => {{
+               let mut msg = VecWriter(Vec::new());
+               ($msg_code as u16).write(&mut msg).unwrap();
+               $msg.write(&mut msg).unwrap();
+               msg.0
+       }}
+}
+
+//TODO: Really should do something smarter for this
+const INITIAL_SYNCS_TO_SEND: usize = 5;
+
+/// Manages and reacts to connection events. You probably want to use file descriptors as PeerIds.
+/// PeerIds may repeat, but only after disconnect_event() has been called.
+impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
+       /// Constructs a new PeerManager with the given message handlers and node_id secret key
+       /// ephemeral_random_data is used to derive per-connection ephemeral keys and must be
+       /// cryptographically secure random bytes.
+       pub fn new(message_handler: MessageHandler, our_node_secret: SecretKey, ephemeral_random_data: &[u8; 32], logger: Arc<Logger>) -> PeerManager<Descriptor> {
+               let mut ephemeral_key_midstate = Sha256::engine();
+               ephemeral_key_midstate.input(ephemeral_random_data);
+
+               PeerManager {
+                       message_handler: message_handler,
+                       peers: Mutex::new(PeerHolder {
+                               peers: HashMap::new(),
+                               peers_needing_send: HashSet::new(),
+                               node_id_to_descriptor: HashMap::new()
+                       }),
+                       our_node_secret: our_node_secret,
+                       ephemeral_key_midstate,
+                       peer_counter_low: AtomicUsize::new(0),
+                       peer_counter_high: AtomicUsize::new(0),
+                       initial_syncs_sent: AtomicUsize::new(0),
+                       logger,
+               }
+       }
+
+       /// Get the list of node ids for peers which have completed the initial handshake.
+       ///
+       /// For outbound connections, this will be the same as the their_node_id parameter passed in to
+       /// new_outbound_connection, however entries will only appear once the initial handshake has
+       /// completed and we are sure the remote peer has the private key for the given node_id.
+       pub fn get_peer_node_ids(&self) -> Vec<PublicKey> {
+               let peers = self.peers.lock().unwrap();
+               peers.peers.values().filter_map(|p| {
+                       if !p.channel_encryptor.is_ready_for_encryption() || p.their_global_features.is_none() {
+                               return None;
+                       }
+                       p.their_node_id
+               }).collect()
+       }
+
+       fn get_ephemeral_key(&self) -> SecretKey {
+               let mut ephemeral_hash = self.ephemeral_key_midstate.clone();
+               let low = self.peer_counter_low.fetch_add(1, Ordering::AcqRel);
+               let high = if low == 0 {
+                       self.peer_counter_high.fetch_add(1, Ordering::AcqRel)
+               } else {
+                       self.peer_counter_high.load(Ordering::Acquire)
+               };
+               ephemeral_hash.input(&byte_utils::le64_to_array(low as u64));
+               ephemeral_hash.input(&byte_utils::le64_to_array(high as u64));
+               SecretKey::from_slice(&Sha256::from_engine(ephemeral_hash).into_inner()).expect("You broke SHA-256!")
+       }
+
+       /// Indicates a new outbound connection has been established to a node with the given node_id.
+       /// Note that if an Err is returned here you MUST NOT call disconnect_event for the new
+       /// descriptor but must disconnect the connection immediately.
+       ///
+       /// Returns a small number of bytes to send to the remote node (currently always 50).
+       ///
+       /// Panics if descriptor is duplicative with some other descriptor which has not yet has a
+       /// disconnect_event.
+       pub fn new_outbound_connection(&self, their_node_id: PublicKey, descriptor: Descriptor) -> Result<Vec<u8>, PeerHandleError> {
+               let mut peer_encryptor = PeerChannelEncryptor::new_outbound(their_node_id.clone(), self.get_ephemeral_key());
+               let res = peer_encryptor.get_act_one().to_vec();
+               let pending_read_buffer = [0; 50].to_vec(); // Noise act two is 50 bytes
+
+               let mut peers = self.peers.lock().unwrap();
+               if peers.peers.insert(descriptor, Peer {
+                       channel_encryptor: peer_encryptor,
+                       outbound: true,
+                       their_node_id: None,
+                       their_global_features: None,
+                       their_local_features: None,
+
+                       pending_outbound_buffer: LinkedList::new(),
+                       pending_outbound_buffer_first_msg_offset: 0,
+                       awaiting_write_event: false,
+
+                       pending_read_buffer: pending_read_buffer,
+                       pending_read_buffer_pos: 0,
+                       pending_read_is_header: false,
+
+                       sync_status: InitSyncTracker::NoSyncRequested,
+               }).is_some() {
+                       panic!("PeerManager driver duplicated descriptors!");
+               };
+               Ok(res)
+       }
+
+       /// Indicates a new inbound connection has been established.
+       ///
+       /// May refuse the connection by returning an Err, but will never write bytes to the remote end
+       /// (outbound connector always speaks first). Note that if an Err is returned here you MUST NOT
+       /// call disconnect_event for the new descriptor but must disconnect the connection
+       /// immediately.
+       ///
+       /// Panics if descriptor is duplicative with some other descriptor which has not yet has a
+       /// disconnect_event.
+       pub fn new_inbound_connection(&self, descriptor: Descriptor) -> Result<(), PeerHandleError> {
+               let peer_encryptor = PeerChannelEncryptor::new_inbound(&self.our_node_secret);
+               let pending_read_buffer = [0; 50].to_vec(); // Noise act one is 50 bytes
+
+               let mut peers = self.peers.lock().unwrap();
+               if peers.peers.insert(descriptor, Peer {
+                       channel_encryptor: peer_encryptor,
+                       outbound: false,
+                       their_node_id: None,
+                       their_global_features: None,
+                       their_local_features: None,
+
+                       pending_outbound_buffer: LinkedList::new(),
+                       pending_outbound_buffer_first_msg_offset: 0,
+                       awaiting_write_event: false,
+
+                       pending_read_buffer: pending_read_buffer,
+                       pending_read_buffer_pos: 0,
+                       pending_read_is_header: false,
+
+                       sync_status: InitSyncTracker::NoSyncRequested,
+               }).is_some() {
+                       panic!("PeerManager driver duplicated descriptors!");
+               };
+               Ok(())
+       }
+
+       fn do_attempt_write_data(&self, descriptor: &mut Descriptor, peer: &mut Peer) {
+               macro_rules! encode_and_send_msg {
+                       ($msg: expr, $msg_code: expr) => {
+                               {
+                                       log_trace!(self, "Encoding and sending sync update message of type {} to {}", $msg_code, log_pubkey!(peer.their_node_id.unwrap()));
+                                       peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!($msg, $msg_code)[..]));
+                               }
+                       }
+               }
+               const MSG_BUFF_SIZE: usize = 10;
+               while !peer.awaiting_write_event {
+                       if peer.pending_outbound_buffer.len() < MSG_BUFF_SIZE {
+                               match peer.sync_status {
+                                       InitSyncTracker::NoSyncRequested => {},
+                                       InitSyncTracker::ChannelsSyncing(c) if c < 0xffff_ffff_ffff_ffff => {
+                                               let steps = ((MSG_BUFF_SIZE - peer.pending_outbound_buffer.len() + 2) / 3) as u8;
+                                               let all_messages = self.message_handler.route_handler.get_next_channel_announcements(0, steps);
+                                               for &(ref announce, ref update_a, ref update_b) in all_messages.iter() {
+                                                       encode_and_send_msg!(announce, 256);
+                                                       encode_and_send_msg!(update_a, 258);
+                                                       encode_and_send_msg!(update_b, 258);
+                                                       peer.sync_status = InitSyncTracker::ChannelsSyncing(announce.contents.short_channel_id + 1);
+                                               }
+                                               if all_messages.is_empty() || all_messages.len() != steps as usize {
+                                                       peer.sync_status = InitSyncTracker::ChannelsSyncing(0xffff_ffff_ffff_ffff);
+                                               }
+                                       },
+                                       InitSyncTracker::ChannelsSyncing(c) if c == 0xffff_ffff_ffff_ffff => {
+                                               let steps = (MSG_BUFF_SIZE - peer.pending_outbound_buffer.len()) as u8;
+                                               let all_messages = self.message_handler.route_handler.get_next_node_announcements(None, steps);
+                                               for msg in all_messages.iter() {
+                                                       encode_and_send_msg!(msg, 256);
+                                                       peer.sync_status = InitSyncTracker::NodesSyncing(msg.contents.node_id);
+                                               }
+                                               if all_messages.is_empty() || all_messages.len() != steps as usize {
+                                                       peer.sync_status = InitSyncTracker::NoSyncRequested;
+                                               }
+                                       },
+                                       InitSyncTracker::ChannelsSyncing(_) => unreachable!(),
+                                       InitSyncTracker::NodesSyncing(key) => {
+                                               let steps = (MSG_BUFF_SIZE - peer.pending_outbound_buffer.len()) as u8;
+                                               let all_messages = self.message_handler.route_handler.get_next_node_announcements(Some(&key), steps);
+                                               for msg in all_messages.iter() {
+                                                       encode_and_send_msg!(msg, 256);
+                                                       peer.sync_status = InitSyncTracker::NodesSyncing(msg.contents.node_id);
+                                               }
+                                               if all_messages.is_empty() || all_messages.len() != steps as usize {
+                                                       peer.sync_status = InitSyncTracker::NoSyncRequested;
+                                               }
+                                       },
+                               }
+                       }
+
+                       if {
+                               let next_buff = match peer.pending_outbound_buffer.front() {
+                                       None => return,
+                                       Some(buff) => buff,
+                               };
+
+                               let should_be_reading = peer.pending_outbound_buffer.len() < MSG_BUFF_SIZE;
+                               let pending = &next_buff[peer.pending_outbound_buffer_first_msg_offset..];
+                               let data_sent = descriptor.send_data(pending, should_be_reading);
+                               peer.pending_outbound_buffer_first_msg_offset += data_sent;
+                               if peer.pending_outbound_buffer_first_msg_offset == next_buff.len() { true } else { false }
+                       } {
+                               peer.pending_outbound_buffer_first_msg_offset = 0;
+                               peer.pending_outbound_buffer.pop_front();
+                       } else {
+                               peer.awaiting_write_event = true;
+                       }
+               }
+       }
+
+       /// Indicates that there is room to write data to the given socket descriptor.
+       ///
+       /// May return an Err to indicate that the connection should be closed.
+       ///
+       /// Will most likely call send_data on the descriptor passed in (or the descriptor handed into
+       /// new_*\_connection) before returning. Thus, be very careful with reentrancy issues! The
+       /// invariants around calling write_event in case a write did not fully complete must still
+       /// hold - be ready to call write_event again if a write call generated here isn't sufficient!
+       /// Panics if the descriptor was not previously registered in a new_\*_connection event.
+       pub fn write_event(&self, descriptor: &mut Descriptor) -> Result<(), PeerHandleError> {
+               let mut peers = self.peers.lock().unwrap();
+               match peers.peers.get_mut(descriptor) {
+                       None => panic!("Descriptor for write_event is not already known to PeerManager"),
+                       Some(peer) => {
+                               peer.awaiting_write_event = false;
+                               self.do_attempt_write_data(descriptor, peer);
+                       }
+               };
+               Ok(())
+       }
+
+       /// Indicates that data was read from the given socket descriptor.
+       ///
+       /// May return an Err to indicate that the connection should be closed.
+       ///
+       /// Will *not* call back into send_data on any descriptors to avoid reentrancy complexity.
+       /// Thus, however, you almost certainly want to call process_events() after any read_event to
+       /// generate send_data calls to handle responses.
+       ///
+       /// If Ok(true) is returned, further read_events should not be triggered until a write_event on
+       /// this file descriptor has resume_read set (preventing DoS issues in the send buffer).
+       ///
+       /// Panics if the descriptor was not previously registered in a new_*_connection event.
+       pub fn read_event(&self, peer_descriptor: &mut Descriptor, data: Vec<u8>) -> Result<bool, PeerHandleError> {
+               match self.do_read_event(peer_descriptor, data) {
+                       Ok(res) => Ok(res),
+                       Err(e) => {
+                               self.disconnect_event_internal(peer_descriptor, e.no_connection_possible);
+                               Err(e)
+                       }
+               }
+       }
+
+       fn do_read_event(&self, peer_descriptor: &mut Descriptor, data: Vec<u8>) -> Result<bool, PeerHandleError> {
+               let pause_read = {
+                       let mut peers_lock = self.peers.lock().unwrap();
+                       let peers = peers_lock.borrow_parts();
+                       let pause_read = match peers.peers.get_mut(peer_descriptor) {
+                               None => panic!("Descriptor for read_event is not already known to PeerManager"),
+                               Some(peer) => {
+                                       assert!(peer.pending_read_buffer.len() > 0);
+                                       assert!(peer.pending_read_buffer.len() > peer.pending_read_buffer_pos);
+
+                                       let mut read_pos = 0;
+                                       while read_pos < data.len() {
+                                               {
+                                                       let data_to_copy = cmp::min(peer.pending_read_buffer.len() - peer.pending_read_buffer_pos, data.len() - read_pos);
+                                                       peer.pending_read_buffer[peer.pending_read_buffer_pos..peer.pending_read_buffer_pos + data_to_copy].copy_from_slice(&data[read_pos..read_pos + data_to_copy]);
+                                                       read_pos += data_to_copy;
+                                                       peer.pending_read_buffer_pos += data_to_copy;
+                                               }
+
+                                               if peer.pending_read_buffer_pos == peer.pending_read_buffer.len() {
+                                                       peer.pending_read_buffer_pos = 0;
+
+                                                       macro_rules! encode_and_send_msg {
+                                                               ($msg: expr, $msg_code: expr) => {
+                                                                       {
+                                                                               log_trace!(self, "Encoding and sending message of type {} to {}", $msg_code, log_pubkey!(peer.their_node_id.unwrap()));
+                                                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!($msg, $msg_code)[..]));
+                                                                               peers.peers_needing_send.insert(peer_descriptor.clone());
+                                                                       }
+                                                               }
+                                                       }
+
+                                                       macro_rules! try_potential_handleerror {
+                                                               ($thing: expr) => {
+                                                                       match $thing {
+                                                                               Ok(x) => x,
+                                                                               Err(e) => {
+                                                                                       if let Some(action) = e.action {
+                                                                                               match action {
+                                                                                                       msgs::ErrorAction::DisconnectPeer { msg: _ } => {
+                                                                                                               //TODO: Try to push msg
+                                                                                                               log_trace!(self, "Got Err handling message, disconnecting peer because {}", e.err);
+                                                                                                               return Err(PeerHandleError{ no_connection_possible: false });
+                                                                                                       },
+                                                                                                       msgs::ErrorAction::IgnoreError => {
+                                                                                                               log_trace!(self, "Got Err handling message, ignoring because {}", e.err);
+                                                                                                               continue;
+                                                                                                       },
+                                                                                                       msgs::ErrorAction::SendErrorMessage { msg } => {
+                                                                                                               log_trace!(self, "Got Err handling message, sending Error message because {}", e.err);
+                                                                                                               encode_and_send_msg!(msg, 17);
+                                                                                                               continue;
+                                                                                                       },
+                                                                                               }
+                                                                                       } else {
+                                                                                               log_debug!(self, "Got Err handling message, action not yet filled in: {}", e.err);
+                                                                                               return Err(PeerHandleError{ no_connection_possible: false });
+                                                                                       }
+                                                                               }
+                                                                       };
+                                                               }
+                                                       }
+
+                                                       macro_rules! try_potential_decodeerror {
+                                                               ($thing: expr) => {
+                                                                       match $thing {
+                                                                               Ok(x) => x,
+                                                                               Err(e) => {
+                                                                                       match e {
+                                                                                               msgs::DecodeError::UnknownVersion => return Err(PeerHandleError{ no_connection_possible: false }),
+                                                                                               msgs::DecodeError::UnknownRequiredFeature => {
+                                                                                                       log_debug!(self, "Got a channel/node announcement with an known required feature flag, you may want to update!");
+                                                                                                       continue;
+                                                                                               },
+                                                                                               msgs::DecodeError::InvalidValue => {
+                                                                                                       log_debug!(self, "Got an invalid value while deserializing message");
+                                                                                                       return Err(PeerHandleError{ no_connection_possible: false });
+                                                                                               },
+                                                                                               msgs::DecodeError::ShortRead => {
+                                                                                                       log_debug!(self, "Deserialization failed due to shortness of message");
+                                                                                                       return Err(PeerHandleError{ no_connection_possible: false });
+                                                                                               },
+                                                                                               msgs::DecodeError::ExtraAddressesPerType => {
+                                                                                                       log_debug!(self, "Error decoding message, ignoring due to lnd spec incompatibility. See https://github.com/lightningnetwork/lnd/issues/1407");
+                                                                                                       continue;
+                                                                                               },
+                                                                                               msgs::DecodeError::BadLengthDescriptor => return Err(PeerHandleError{ no_connection_possible: false }),
+                                                                                               msgs::DecodeError::Io(_) => return Err(PeerHandleError{ no_connection_possible: false }),
+                                                                                       }
+                                                                               }
+                                                                       };
+                                                               }
+                                                       }
+
+                                                       macro_rules! insert_node_id {
+                                                               () => {
+                                                                       match peers.node_id_to_descriptor.entry(peer.their_node_id.unwrap()) {
+                                                                               hash_map::Entry::Occupied(_) => {
+                                                                                       log_trace!(self, "Got second connection with {}, closing", log_pubkey!(peer.their_node_id.unwrap()));
+                                                                                       peer.their_node_id = None; // Unset so that we don't generate a peer_disconnected event
+                                                                                       return Err(PeerHandleError{ no_connection_possible: false })
+                                                                               },
+                                                                               hash_map::Entry::Vacant(entry) => {
+                                                                                       log_trace!(self, "Finished noise handshake for connection with {}", log_pubkey!(peer.their_node_id.unwrap()));
+                                                                                       entry.insert(peer_descriptor.clone())
+                                                                               },
+                                                                       };
+                                                               }
+                                                       }
+
+                                                       let next_step = peer.channel_encryptor.get_noise_step();
+                                                       match next_step {
+                                                               NextNoiseStep::ActOne => {
+                                                                       let act_two = try_potential_handleerror!(peer.channel_encryptor.process_act_one_with_keys(&peer.pending_read_buffer[..], &self.our_node_secret, self.get_ephemeral_key())).to_vec();
+                                                                       peer.pending_outbound_buffer.push_back(act_two);
+                                                                       peer.pending_read_buffer = [0; 66].to_vec(); // act three is 66 bytes long
+                                                               },
+                                                               NextNoiseStep::ActTwo => {
+                                                                       let (act_three, their_node_id) = try_potential_handleerror!(peer.channel_encryptor.process_act_two(&peer.pending_read_buffer[..], &self.our_node_secret));
+                                                                       peer.pending_outbound_buffer.push_back(act_three.to_vec());
+                                                                       peer.pending_read_buffer = [0; 18].to_vec(); // Message length header is 18 bytes
+                                                                       peer.pending_read_is_header = true;
+
+                                                                       peer.their_node_id = Some(their_node_id);
+                                                                       insert_node_id!();
+                                                                       let mut local_features = msgs::LocalFeatures::new();
+                                                                       if self.initial_syncs_sent.load(Ordering::Acquire) < INITIAL_SYNCS_TO_SEND {
+                                                                               self.initial_syncs_sent.fetch_add(1, Ordering::AcqRel);
+                                                                               local_features.set_initial_routing_sync();
+                                                                       }
+                                                                       encode_and_send_msg!(msgs::Init {
+                                                                               global_features: msgs::GlobalFeatures::new(),
+                                                                               local_features,
+                                                                       }, 16);
+                                                               },
+                                                               NextNoiseStep::ActThree => {
+                                                                       let their_node_id = try_potential_handleerror!(peer.channel_encryptor.process_act_three(&peer.pending_read_buffer[..]));
+                                                                       peer.pending_read_buffer = [0; 18].to_vec(); // Message length header is 18 bytes
+                                                                       peer.pending_read_is_header = true;
+                                                                       peer.their_node_id = Some(their_node_id);
+                                                                       insert_node_id!();
+                                                               },
+                                                               NextNoiseStep::NoiseComplete => {
+                                                                       if peer.pending_read_is_header {
+                                                                               let msg_len = try_potential_handleerror!(peer.channel_encryptor.decrypt_length_header(&peer.pending_read_buffer[..]));
+                                                                               peer.pending_read_buffer = Vec::with_capacity(msg_len as usize + 16);
+                                                                               peer.pending_read_buffer.resize(msg_len as usize + 16, 0);
+                                                                               if msg_len < 2 { // Need at least the message type tag
+                                                                                       return Err(PeerHandleError{ no_connection_possible: false });
+                                                                               }
+                                                                               peer.pending_read_is_header = false;
+                                                                       } else {
+                                                                               let msg_data = try_potential_handleerror!(peer.channel_encryptor.decrypt_message(&peer.pending_read_buffer[..]));
+                                                                               assert!(msg_data.len() >= 2);
+
+                                                                               // Reset read buffer
+                                                                               peer.pending_read_buffer = [0; 18].to_vec();
+                                                                               peer.pending_read_is_header = true;
+
+                                                                               let msg_type = byte_utils::slice_to_be16(&msg_data[0..2]);
+                                                                               log_trace!(self, "Received message of type {} from {}", msg_type, log_pubkey!(peer.their_node_id.unwrap()));
+                                                                               if msg_type != 16 && peer.their_global_features.is_none() {
+                                                                                       // Need an init message as first message
+                                                                                       log_trace!(self, "Peer {} sent non-Init first message", log_pubkey!(peer.their_node_id.unwrap()));
+                                                                                       return Err(PeerHandleError{ no_connection_possible: false });
+                                                                               }
+                                                                               let mut reader = ::std::io::Cursor::new(&msg_data[2..]);
+                                                                               match msg_type {
+                                                                                       // Connection control:
+                                                                                       16 => {
+                                                                                               let msg = try_potential_decodeerror!(msgs::Init::read(&mut reader));
+                                                                                               if msg.global_features.requires_unknown_bits() {
+                                                                                                       log_info!(self, "Peer global features required unknown version bits");
+                                                                                                       return Err(PeerHandleError{ no_connection_possible: true });
+                                                                                               }
+                                                                                               if msg.local_features.requires_unknown_bits() {
+                                                                                                       log_info!(self, "Peer local features required unknown version bits");
+                                                                                                       return Err(PeerHandleError{ no_connection_possible: true });
+                                                                                               }
+                                                                                               if peer.their_global_features.is_some() {
+                                                                                                       return Err(PeerHandleError{ no_connection_possible: false });
+                                                                                               }
+
+                                                                                               log_info!(self, "Received peer Init message: data_loss_protect: {}, initial_routing_sync: {}, upfront_shutdown_script: {}, unkown local flags: {}, unknown global flags: {}",
+                                                                                                       if msg.local_features.supports_data_loss_protect() { "supported" } else { "not supported"},
+                                                                                                       if msg.local_features.initial_routing_sync() { "requested" } else { "not requested" },
+                                                                                                       if msg.local_features.supports_upfront_shutdown_script() { "supported" } else { "not supported"},
+                                                                                                       if msg.local_features.supports_unknown_bits() { "present" } else { "none" },
+                                                                                                       if msg.global_features.supports_unknown_bits() { "present" } else { "none" });
+
+                                                                                               if msg.local_features.initial_routing_sync() {
+                                                                                                       peer.sync_status = InitSyncTracker::ChannelsSyncing(0);
+                                                                                                       peers.peers_needing_send.insert(peer_descriptor.clone());
+                                                                                               }
+                                                                                               peer.their_global_features = Some(msg.global_features);
+                                                                                               peer.their_local_features = Some(msg.local_features);
+
+                                                                                               if !peer.outbound {
+                                                                                                       let mut local_features = msgs::LocalFeatures::new();
+                                                                                                       if self.initial_syncs_sent.load(Ordering::Acquire) < INITIAL_SYNCS_TO_SEND {
+                                                                                                               self.initial_syncs_sent.fetch_add(1, Ordering::AcqRel);
+                                                                                                               local_features.set_initial_routing_sync();
+                                                                                                       }
+
+                                                                                                       encode_and_send_msg!(msgs::Init {
+                                                                                                               global_features: msgs::GlobalFeatures::new(),
+                                                                                                               local_features,
+                                                                                                       }, 16);
+                                                                                               }
+
+                                                                                               self.message_handler.chan_handler.peer_connected(&peer.their_node_id.unwrap());
+                                                                                       },
+                                                                                       17 => {
+                                                                                               let msg = try_potential_decodeerror!(msgs::ErrorMessage::read(&mut reader));
+                                                                                               let mut data_is_printable = true;
+                                                                                               for b in msg.data.bytes() {
+                                                                                                       if b < 32 || b > 126 {
+                                                                                                               data_is_printable = false;
+                                                                                                               break;
+                                                                                                       }
+                                                                                               }
+
+                                                                                               if data_is_printable {
+                                                                                                       log_debug!(self, "Got Err message from {}: {}", log_pubkey!(peer.their_node_id.unwrap()), msg.data);
+                                                                                               } else {
+                                                                                                       log_debug!(self, "Got Err message from {} with non-ASCII error message", log_pubkey!(peer.their_node_id.unwrap()));
+                                                                                               }
+                                                                                               self.message_handler.chan_handler.handle_error(&peer.their_node_id.unwrap(), &msg);
+                                                                                               if msg.channel_id == [0; 32] {
+                                                                                                       return Err(PeerHandleError{ no_connection_possible: true });
+                                                                                               }
+                                                                                       },
+
+                                                                                       18 => {
+                                                                                               let msg = try_potential_decodeerror!(msgs::Ping::read(&mut reader));
+                                                                                               if msg.ponglen < 65532 {
+                                                                                                       let resp = msgs::Pong { byteslen: msg.ponglen };
+                                                                                                       encode_and_send_msg!(resp, 19);
+                                                                                               }
+                                                                                       },
+                                                                                       19 => {
+                                                                                               try_potential_decodeerror!(msgs::Pong::read(&mut reader));
+                                                                                       },
+
+                                                                                       // Channel control:
+                                                                                       32 => {
+                                                                                               let msg = try_potential_decodeerror!(msgs::OpenChannel::read(&mut reader));
+                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_open_channel(&peer.their_node_id.unwrap(), peer.their_local_features.clone().unwrap(), &msg));
+                                                                                       },
+                                                                                       33 => {
+                                                                                               let msg = try_potential_decodeerror!(msgs::AcceptChannel::read(&mut reader));
+                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_accept_channel(&peer.their_node_id.unwrap(), peer.their_local_features.clone().unwrap(), &msg));
+                                                                                       },
+
+                                                                                       34 => {
+                                                                                               let msg = try_potential_decodeerror!(msgs::FundingCreated::read(&mut reader));
+                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_funding_created(&peer.their_node_id.unwrap(), &msg));
+                                                                                       },
+                                                                                       35 => {
+                                                                                               let msg = try_potential_decodeerror!(msgs::FundingSigned::read(&mut reader));
+                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_funding_signed(&peer.their_node_id.unwrap(), &msg));
+                                                                                       },
+                                                                                       36 => {
+                                                                                               let msg = try_potential_decodeerror!(msgs::FundingLocked::read(&mut reader));
+                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_funding_locked(&peer.their_node_id.unwrap(), &msg));
+                                                                                       },
+
+                                                                                       38 => {
+                                                                                               let msg = try_potential_decodeerror!(msgs::Shutdown::read(&mut reader));
+                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_shutdown(&peer.their_node_id.unwrap(), &msg));
+                                                                                       },
+                                                                                       39 => {
+                                                                                               let msg = try_potential_decodeerror!(msgs::ClosingSigned::read(&mut reader));
+                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_closing_signed(&peer.their_node_id.unwrap(), &msg));
+                                                                                       },
+
+                                                                                       128 => {
+                                                                                               let msg = try_potential_decodeerror!(msgs::UpdateAddHTLC::read(&mut reader));
+                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_update_add_htlc(&peer.their_node_id.unwrap(), &msg));
+                                                                                       },
+                                                                                       130 => {
+                                                                                               let msg = try_potential_decodeerror!(msgs::UpdateFulfillHTLC::read(&mut reader));
+                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_update_fulfill_htlc(&peer.their_node_id.unwrap(), &msg));
+                                                                                       },
+                                                                                       131 => {
+                                                                                               let msg = try_potential_decodeerror!(msgs::UpdateFailHTLC::read(&mut reader));
+                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_update_fail_htlc(&peer.their_node_id.unwrap(), &msg));
+                                                                                       },
+                                                                                       135 => {
+                                                                                               let msg = try_potential_decodeerror!(msgs::UpdateFailMalformedHTLC::read(&mut reader));
+                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_update_fail_malformed_htlc(&peer.their_node_id.unwrap(), &msg));
+                                                                                       },
+
+                                                                                       132 => {
+                                                                                               let msg = try_potential_decodeerror!(msgs::CommitmentSigned::read(&mut reader));
+                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_commitment_signed(&peer.their_node_id.unwrap(), &msg));
+                                                                                       },
+                                                                                       133 => {
+                                                                                               let msg = try_potential_decodeerror!(msgs::RevokeAndACK::read(&mut reader));
+                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_revoke_and_ack(&peer.their_node_id.unwrap(), &msg));
+                                                                                       },
+                                                                                       134 => {
+                                                                                               let msg = try_potential_decodeerror!(msgs::UpdateFee::read(&mut reader));
+                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_update_fee(&peer.their_node_id.unwrap(), &msg));
+                                                                                       },
+                                                                                       136 => {
+                                                                                               let msg = try_potential_decodeerror!(msgs::ChannelReestablish::read(&mut reader));
+                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_channel_reestablish(&peer.their_node_id.unwrap(), &msg));
+                                                                                       },
+
+                                                                                       // Routing control:
+                                                                                       259 => {
+                                                                                               let msg = try_potential_decodeerror!(msgs::AnnouncementSignatures::read(&mut reader));
+                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_announcement_signatures(&peer.their_node_id.unwrap(), &msg));
+                                                                                       },
+                                                                                       256 => {
+                                                                                               let msg = try_potential_decodeerror!(msgs::ChannelAnnouncement::read(&mut reader));
+                                                                                               let should_forward = try_potential_handleerror!(self.message_handler.route_handler.handle_channel_announcement(&msg));
+
+                                                                                               if should_forward {
+                                                                                                       // TODO: forward msg along to all our other peers!
+                                                                                               }
+                                                                                       },
+                                                                                       257 => {
+                                                                                               let msg = try_potential_decodeerror!(msgs::NodeAnnouncement::read(&mut reader));
+                                                                                               let should_forward = try_potential_handleerror!(self.message_handler.route_handler.handle_node_announcement(&msg));
+
+                                                                                               if should_forward {
+                                                                                                       // TODO: forward msg along to all our other peers!
+                                                                                               }
+                                                                                       },
+                                                                                       258 => {
+                                                                                               let msg = try_potential_decodeerror!(msgs::ChannelUpdate::read(&mut reader));
+                                                                                               let should_forward = try_potential_handleerror!(self.message_handler.route_handler.handle_channel_update(&msg));
+
+                                                                                               if should_forward {
+                                                                                                       // TODO: forward msg along to all our other peers!
+                                                                                               }
+                                                                                       },
+                                                                                       _ => {
+                                                                                               if (msg_type & 1) == 0 {
+                                                                                                       return Err(PeerHandleError{ no_connection_possible: true });
+                                                                                               }
+                                                                                       },
+                                                                               }
+                                                                       }
+                                                               }
+                                                       }
+                                               }
+                                       }
+
+                                       self.do_attempt_write_data(peer_descriptor, peer);
+
+                                       peer.pending_outbound_buffer.len() > 10 // pause_read
+                               }
+                       };
+
+                       pause_read
+               };
+
+               Ok(pause_read)
+       }
+
+       /// Checks for any events generated by our handlers and processes them. Includes sending most
+       /// response messages as well as messages generated by calls to handler functions directly (eg
+       /// functions like ChannelManager::process_pending_htlc_forward or send_payment).
+       pub fn process_events(&self) {
+               {
+                       // TODO: There are some DoS attacks here where you can flood someone's outbound send
+                       // buffer by doing things like announcing channels on another node. We should be willing to
+                       // drop optional-ish messages when send buffers get full!
+
+                       let mut events_generated = self.message_handler.chan_handler.get_and_clear_pending_msg_events();
+                       let mut peers_lock = self.peers.lock().unwrap();
+                       let peers = peers_lock.borrow_parts();
+                       for event in events_generated.drain(..) {
+                               macro_rules! get_peer_for_forwarding {
+                                       ($node_id: expr, $handle_no_such_peer: block) => {
+                                               {
+                                                       let descriptor = match peers.node_id_to_descriptor.get($node_id) {
+                                                               Some(descriptor) => descriptor.clone(),
+                                                               None => {
+                                                                       $handle_no_such_peer;
+                                                                       continue;
+                                                               },
+                                                       };
+                                                       match peers.peers.get_mut(&descriptor) {
+                                                               Some(peer) => {
+                                                                       if peer.their_global_features.is_none() {
+                                                                               $handle_no_such_peer;
+                                                                               continue;
+                                                                       }
+                                                                       (descriptor, peer)
+                                                               },
+                                                               None => panic!("Inconsistent peers set state!"),
+                                                       }
+                                               }
+                                       }
+                               }
+                               match event {
+                                       MessageSendEvent::SendAcceptChannel { ref node_id, ref msg } => {
+                                               log_trace!(self, "Handling SendAcceptChannel event in peer_handler for node {} for channel {}",
+                                                               log_pubkey!(node_id),
+                                                               log_bytes!(msg.temporary_channel_id));
+                                               let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
+                                                               //TODO: Drop the pending channel? (or just let it timeout, but that sucks)
+                                                       });
+                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 33)));
+                                               self.do_attempt_write_data(&mut descriptor, peer);
+                                       },
+                                       MessageSendEvent::SendOpenChannel { ref node_id, ref msg } => {
+                                               log_trace!(self, "Handling SendOpenChannel event in peer_handler for node {} for channel {}",
+                                                               log_pubkey!(node_id),
+                                                               log_bytes!(msg.temporary_channel_id));
+                                               let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
+                                                               //TODO: Drop the pending channel? (or just let it timeout, but that sucks)
+                                                       });
+                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 32)));
+                                               self.do_attempt_write_data(&mut descriptor, peer);
+                                       },
+                                       MessageSendEvent::SendFundingCreated { ref node_id, ref msg } => {
+                                               log_trace!(self, "Handling SendFundingCreated event in peer_handler for node {} for channel {} (which becomes {})",
+                                                               log_pubkey!(node_id),
+                                                               log_bytes!(msg.temporary_channel_id),
+                                                               log_funding_channel_id!(msg.funding_txid, msg.funding_output_index));
+                                               let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
+                                                               //TODO: generate a DiscardFunding event indicating to the wallet that
+                                                               //they should just throw away this funding transaction
+                                                       });
+                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 34)));
+                                               self.do_attempt_write_data(&mut descriptor, peer);
+                                       },
+                                       MessageSendEvent::SendFundingSigned { ref node_id, ref msg } => {
+                                               log_trace!(self, "Handling SendFundingSigned event in peer_handler for node {} for channel {}",
+                                                               log_pubkey!(node_id),
+                                                               log_bytes!(msg.channel_id));
+                                               let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
+                                                               //TODO: generate a DiscardFunding event indicating to the wallet that
+                                                               //they should just throw away this funding transaction
+                                                       });
+                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 35)));
+                                               self.do_attempt_write_data(&mut descriptor, peer);
+                                       },
+                                       MessageSendEvent::SendFundingLocked { ref node_id, ref msg } => {
+                                               log_trace!(self, "Handling SendFundingLocked event in peer_handler for node {} for channel {}",
+                                                               log_pubkey!(node_id),
+                                                               log_bytes!(msg.channel_id));
+                                               let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
+                                                               //TODO: Do whatever we're gonna do for handling dropped messages
+                                                       });
+                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 36)));
+                                               self.do_attempt_write_data(&mut descriptor, peer);
+                                       },
+                                       MessageSendEvent::SendAnnouncementSignatures { ref node_id, ref msg } => {
+                                               log_trace!(self, "Handling SendAnnouncementSignatures event in peer_handler for node {} for channel {})",
+                                                               log_pubkey!(node_id),
+                                                               log_bytes!(msg.channel_id));
+                                               let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
+                                                               //TODO: generate a DiscardFunding event indicating to the wallet that
+                                                               //they should just throw away this funding transaction
+                                                       });
+                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 259)));
+                                               self.do_attempt_write_data(&mut descriptor, peer);
+                                       },
+                                       MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => {
+                                               log_trace!(self, "Handling UpdateHTLCs event in peer_handler for node {} with {} adds, {} fulfills, {} fails for channel {}",
+                                                               log_pubkey!(node_id),
+                                                               update_add_htlcs.len(),
+                                                               update_fulfill_htlcs.len(),
+                                                               update_fail_htlcs.len(),
+                                                               log_bytes!(commitment_signed.channel_id));
+                                               let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
+                                                               //TODO: Do whatever we're gonna do for handling dropped messages
+                                                       });
+                                               for msg in update_add_htlcs {
+                                                       peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 128)));
+                                               }
+                                               for msg in update_fulfill_htlcs {
+                                                       peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 130)));
+                                               }
+                                               for msg in update_fail_htlcs {
+                                                       peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 131)));
+                                               }
+                                               for msg in update_fail_malformed_htlcs {
+                                                       peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 135)));
+                                               }
+                                               if let &Some(ref msg) = update_fee {
+                                                       peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 134)));
+                                               }
+                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(commitment_signed, 132)));
+                                               self.do_attempt_write_data(&mut descriptor, peer);
+                                       },
+                                       MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
+                                               log_trace!(self, "Handling SendRevokeAndACK event in peer_handler for node {} for channel {}",
+                                                               log_pubkey!(node_id),
+                                                               log_bytes!(msg.channel_id));
+                                               let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
+                                                               //TODO: Do whatever we're gonna do for handling dropped messages
+                                                       });
+                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 133)));
+                                               self.do_attempt_write_data(&mut descriptor, peer);
+                                       },
+                                       MessageSendEvent::SendClosingSigned { ref node_id, ref msg } => {
+                                               log_trace!(self, "Handling SendClosingSigned event in peer_handler for node {} for channel {}",
+                                                               log_pubkey!(node_id),
+                                                               log_bytes!(msg.channel_id));
+                                               let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
+                                                               //TODO: Do whatever we're gonna do for handling dropped messages
+                                                       });
+                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 39)));
+                                               self.do_attempt_write_data(&mut descriptor, peer);
+                                       },
+                                       MessageSendEvent::SendShutdown { ref node_id, ref msg } => {
+                                               log_trace!(self, "Handling Shutdown event in peer_handler for node {} for channel {}",
+                                                               log_pubkey!(node_id),
+                                                               log_bytes!(msg.channel_id));
+                                               let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
+                                                               //TODO: Do whatever we're gonna do for handling dropped messages
+                                                       });
+                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 38)));
+                                               self.do_attempt_write_data(&mut descriptor, peer);
+                                       },
+                                       MessageSendEvent::SendChannelReestablish { ref node_id, ref msg } => {
+                                               log_trace!(self, "Handling SendChannelReestablish event in peer_handler for node {} for channel {}",
+                                                               log_pubkey!(node_id),
+                                                               log_bytes!(msg.channel_id));
+                                               let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
+                                                               //TODO: Do whatever we're gonna do for handling dropped messages
+                                                       });
+                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 136)));
+                                               self.do_attempt_write_data(&mut descriptor, peer);
+                                       },
+                                       MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
+                                               log_trace!(self, "Handling BroadcastChannelAnnouncement event in peer_handler for short channel id {}", msg.contents.short_channel_id);
+                                               if self.message_handler.route_handler.handle_channel_announcement(msg).is_ok() && self.message_handler.route_handler.handle_channel_update(update_msg).is_ok() {
+                                                       let encoded_msg = encode_msg!(msg, 256);
+                                                       let encoded_update_msg = encode_msg!(update_msg, 258);
+
+                                                       for (ref descriptor, ref mut peer) in peers.peers.iter_mut() {
+                                                               if !peer.channel_encryptor.is_ready_for_encryption() || peer.their_global_features.is_none() ||
+                                                                               !peer.should_forward_channel(msg.contents.short_channel_id) {
+                                                                       continue
+                                                               }
+                                                               match peer.their_node_id {
+                                                                       None => continue,
+                                                                       Some(their_node_id) => {
+                                                                               if their_node_id == msg.contents.node_id_1 || their_node_id == msg.contents.node_id_2 {
+                                                                                       continue
+                                                                               }
+                                                                       }
+                                                               }
+                                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encoded_msg[..]));
+                                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encoded_update_msg[..]));
+                                                               self.do_attempt_write_data(&mut (*descriptor).clone(), peer);
+                                                       }
+                                               }
+                                       },
+                                       MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
+                                               log_trace!(self, "Handling BroadcastChannelUpdate event in peer_handler for short channel id {}", msg.contents.short_channel_id);
+                                               if self.message_handler.route_handler.handle_channel_update(msg).is_ok() {
+                                                       let encoded_msg = encode_msg!(msg, 258);
+
+                                                       for (ref descriptor, ref mut peer) in peers.peers.iter_mut() {
+                                                               if !peer.channel_encryptor.is_ready_for_encryption() || peer.their_global_features.is_none() ||
+                                                                               !peer.should_forward_channel(msg.contents.short_channel_id)  {
+                                                                       continue
+                                                               }
+                                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encoded_msg[..]));
+                                                               self.do_attempt_write_data(&mut (*descriptor).clone(), peer);
+                                                       }
+                                               }
+                                       },
+                                       MessageSendEvent::PaymentFailureNetworkUpdate { ref update } => {
+                                               self.message_handler.route_handler.handle_htlc_fail_channel_update(update);
+                                       },
+                                       MessageSendEvent::HandleError { ref node_id, ref action } => {
+                                               if let Some(ref action) = *action {
+                                                       match *action {
+                                                               msgs::ErrorAction::DisconnectPeer { ref msg } => {
+                                                                       if let Some(mut descriptor) = peers.node_id_to_descriptor.remove(node_id) {
+                                                                               peers.peers_needing_send.remove(&descriptor);
+                                                                               if let Some(mut peer) = peers.peers.remove(&descriptor) {
+                                                                                       if let Some(ref msg) = *msg {
+                                                                                               log_trace!(self, "Handling DisconnectPeer HandleError event in peer_handler for node {} with message {}",
+                                                                                                               log_pubkey!(node_id),
+                                                                                                               msg.data);
+                                                                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 17)));
+                                                                                               // This isn't guaranteed to work, but if there is enough free
+                                                                                               // room in the send buffer, put the error message there...
+                                                                                               self.do_attempt_write_data(&mut descriptor, &mut peer);
+                                                                                       } else {
+                                                                                               log_trace!(self, "Handling DisconnectPeer HandleError event in peer_handler for node {} with no message", log_pubkey!(node_id));
+                                                                                       }
+                                                                               }
+                                                                               descriptor.disconnect_socket();
+                                                                               self.message_handler.chan_handler.peer_disconnected(&node_id, false);
+                                                                       }
+                                                               },
+                                                               msgs::ErrorAction::IgnoreError => {},
+                                                               msgs::ErrorAction::SendErrorMessage { ref msg } => {
+                                                                       log_trace!(self, "Handling SendErrorMessage HandleError event in peer_handler for node {} with message {}",
+                                                                                       log_pubkey!(node_id),
+                                                                                       msg.data);
+                                                                       let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
+                                                                               //TODO: Do whatever we're gonna do for handling dropped messages
+                                                                       });
+                                                                       peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 17)));
+                                                                       self.do_attempt_write_data(&mut descriptor, peer);
+                                                               },
+                                                       }
+                                               } else {
+                                                       log_error!(self, "Got no-action HandleError Event in peer_handler for node {}, no such events should ever be generated!", log_pubkey!(node_id));
+                                               }
+                                       }
+                               }
+                       }
+
+                       for mut descriptor in peers.peers_needing_send.drain() {
+                               match peers.peers.get_mut(&descriptor) {
+                                       Some(peer) => self.do_attempt_write_data(&mut descriptor, peer),
+                                       None => panic!("Inconsistent peers set state!"),
+                               }
+                       }
+               }
+       }
+
+       /// Indicates that the given socket descriptor's connection is now closed.
+       ///
+       /// This must be called even if a PeerHandleError was given for a read_event or write_event,
+       /// but must NOT be called if a PeerHandleError was provided out of a new_\*\_connection event!
+       ///
+       /// Panics if the descriptor was not previously registered in a successful new_*_connection event.
+       pub fn disconnect_event(&self, descriptor: &Descriptor) {
+               self.disconnect_event_internal(descriptor, false);
+       }
+
+       fn disconnect_event_internal(&self, descriptor: &Descriptor, no_connection_possible: bool) {
+               let mut peers = self.peers.lock().unwrap();
+               peers.peers_needing_send.remove(descriptor);
+               let peer_option = peers.peers.remove(descriptor);
+               match peer_option {
+                       None => panic!("Descriptor for disconnect_event is not already known to PeerManager"),
+                       Some(peer) => {
+                               match peer.their_node_id {
+                                       Some(node_id) => {
+                                               peers.node_id_to_descriptor.remove(&node_id);
+                                               self.message_handler.chan_handler.peer_disconnected(&node_id, no_connection_possible);
+                                       },
+                                       None => {}
+                               }
+                       }
+               };
+       }
+}
+
+#[cfg(test)]
+mod tests {
+       use ln::peer_handler::{PeerManager, MessageHandler, SocketDescriptor};
+       use ln::msgs;
+       use util::events;
+       use util::test_utils;
+       use util::logger::Logger;
+
+       use secp256k1::Secp256k1;
+       use secp256k1::key::{SecretKey, PublicKey};
+
+       use rand::{thread_rng, Rng};
+
+       use std::sync::{Arc};
+
+       #[derive(PartialEq, Eq, Clone, Hash)]
+       struct FileDescriptor {
+               fd: u16,
+       }
+
+       impl SocketDescriptor for FileDescriptor {
+               fn send_data(&mut self, data: &[u8], _resume_read: bool) -> usize {
+                       data.len()
+               }
+
+               fn disconnect_socket(&mut self) {}
+       }
+
+       fn create_network(peer_count: usize) -> Vec<PeerManager<FileDescriptor>> {
+               let mut peers = Vec::new();
+               let mut rng = thread_rng();
+               let logger : Arc<Logger> = Arc::new(test_utils::TestLogger::new());
+               let mut ephemeral_bytes = [0; 32];
+               rng.fill_bytes(&mut ephemeral_bytes);
+
+               for _ in 0..peer_count {
+                       let chan_handler = test_utils::TestChannelMessageHandler::new();
+                       let router = test_utils::TestRoutingMessageHandler::new();
+                       let node_id = {
+                               let mut key_slice = [0;32];
+                               rng.fill_bytes(&mut key_slice);
+                               SecretKey::from_slice(&key_slice).unwrap()
+                       };
+                       let msg_handler = MessageHandler { chan_handler: Arc::new(chan_handler), route_handler: Arc::new(router) };
+                       let peer = PeerManager::new(msg_handler, node_id, &ephemeral_bytes, Arc::clone(&logger));
+                       peers.push(peer);
+               }
+
+               peers
+       }
+
+       fn establish_connection(peer_a: &PeerManager<FileDescriptor>, peer_b: &PeerManager<FileDescriptor>) {
+               let secp_ctx = Secp256k1::new();
+               let their_id = PublicKey::from_secret_key(&secp_ctx, &peer_b.our_node_secret);
+               let fd = FileDescriptor { fd: 1};
+               peer_a.new_inbound_connection(fd.clone()).unwrap();
+               peer_a.peers.lock().unwrap().node_id_to_descriptor.insert(their_id, fd.clone());
+       }
+
+       #[test]
+       fn test_disconnect_peer() {
+               // Simple test which builds a network of PeerManager, connects and brings them to NoiseState::Finished and
+               // push a DisconnectPeer event to remove the node flagged by id
+               let mut peers = create_network(2);
+               establish_connection(&peers[0], &peers[1]);
+               assert_eq!(peers[0].peers.lock().unwrap().peers.len(), 1);
+
+               let secp_ctx = Secp256k1::new();
+               let their_id = PublicKey::from_secret_key(&secp_ctx, &peers[1].our_node_secret);
+
+               let chan_handler = test_utils::TestChannelMessageHandler::new();
+               chan_handler.pending_events.lock().unwrap().push(events::MessageSendEvent::HandleError {
+                       node_id: their_id,
+                       action: Some(msgs::ErrorAction::DisconnectPeer { msg: None }),
+               });
+               assert_eq!(chan_handler.pending_events.lock().unwrap().len(), 1);
+               peers[0].message_handler.chan_handler = Arc::new(chan_handler);
+
+               peers[0].process_events();
+               assert_eq!(peers[0].peers.lock().unwrap().peers.len(), 0);
+       }
+}
diff --git a/lightning/src/ln/router.rs b/lightning/src/ln/router.rs
new file mode 100644 (file)
index 0000000..bb20c31
--- /dev/null
@@ -0,0 +1,1633 @@
+//! The top-level routing/network map tracking logic lives here.
+//!
+//! You probably want to create a Router and use that as your RoutingMessageHandler and then
+//! interrogate it to get routes for your own payments.
+
+use secp256k1::key::PublicKey;
+use secp256k1::Secp256k1;
+use secp256k1;
+
+use bitcoin_hashes::sha256d::Hash as Sha256dHash;
+use bitcoin_hashes::Hash;
+use bitcoin::blockdata::script::Builder;
+use bitcoin::blockdata::opcodes;
+
+use chain::chaininterface::{ChainError, ChainWatchInterface};
+use ln::channelmanager;
+use ln::msgs::{DecodeError,ErrorAction,HandleError,RoutingMessageHandler,NetAddress,GlobalFeatures};
+use ln::msgs;
+use util::ser::{Writeable, Readable, Writer, ReadableArgs};
+use util::logger::Logger;
+
+use std::cmp;
+use std::sync::{RwLock,Arc};
+use std::collections::{HashMap,BinaryHeap,BTreeMap};
+use std::collections::btree_map::Entry as BtreeEntry;
+use std;
+
+/// A hop in a route
+#[derive(Clone, PartialEq)]
+pub struct RouteHop {
+       /// The node_id of the node at this hop.
+       pub pubkey: PublicKey,
+       /// The channel that should be used from the previous hop to reach this node.
+       pub short_channel_id: u64,
+       /// The fee taken on this hop. For the last hop, this should be the full value of the payment.
+       pub fee_msat: u64,
+       /// The CLTV delta added for this hop. For the last hop, this should be the full CLTV value
+       /// expected at the destination, in excess of the current block height.
+       pub cltv_expiry_delta: u32,
+}
+
+/// A route from us through the network to a destination
+#[derive(Clone, PartialEq)]
+pub struct Route {
+       /// The list of hops, NOT INCLUDING our own, where the last hop is the destination. Thus, this
+       /// must always be at least length one. By protocol rules, this may not currently exceed 20 in
+       /// length.
+       pub hops: Vec<RouteHop>,
+}
+
+impl Writeable for Route {
+       fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
+               (self.hops.len() as u8).write(writer)?;
+               for hop in self.hops.iter() {
+                       hop.pubkey.write(writer)?;
+                       hop.short_channel_id.write(writer)?;
+                       hop.fee_msat.write(writer)?;
+                       hop.cltv_expiry_delta.write(writer)?;
+               }
+               Ok(())
+       }
+}
+
+impl<R: ::std::io::Read> Readable<R> for Route {
+       fn read(reader: &mut R) -> Result<Route, DecodeError> {
+               let hops_count: u8 = Readable::read(reader)?;
+               let mut hops = Vec::with_capacity(hops_count as usize);
+               for _ in 0..hops_count {
+                       hops.push(RouteHop {
+                               pubkey: Readable::read(reader)?,
+                               short_channel_id: Readable::read(reader)?,
+                               fee_msat: Readable::read(reader)?,
+                               cltv_expiry_delta: Readable::read(reader)?,
+                       });
+               }
+               Ok(Route {
+                       hops
+               })
+       }
+}
+
+#[derive(PartialEq)]
+struct DirectionalChannelInfo {
+       src_node_id: PublicKey,
+       last_update: u32,
+       enabled: bool,
+       cltv_expiry_delta: u16,
+       htlc_minimum_msat: u64,
+       fee_base_msat: u32,
+       fee_proportional_millionths: u32,
+       last_update_message: Option<msgs::ChannelUpdate>,
+}
+
+impl std::fmt::Display for DirectionalChannelInfo {
+       fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
+               write!(f, "src_node_id {}, last_update {}, enabled {}, cltv_expiry_delta {}, htlc_minimum_msat {}, fee_base_msat {}, fee_proportional_millionths {}", log_pubkey!(self.src_node_id), self.last_update, self.enabled, self.cltv_expiry_delta, self.htlc_minimum_msat, self.fee_base_msat, self.fee_proportional_millionths)?;
+               Ok(())
+       }
+}
+
+impl_writeable!(DirectionalChannelInfo, 0, {
+       src_node_id,
+       last_update,
+       enabled,
+       cltv_expiry_delta,
+       htlc_minimum_msat,
+       fee_base_msat,
+       fee_proportional_millionths,
+       last_update_message
+});
+
+#[derive(PartialEq)]
+struct ChannelInfo {
+       features: GlobalFeatures,
+       one_to_two: DirectionalChannelInfo,
+       two_to_one: DirectionalChannelInfo,
+       //this is cached here so we can send out it later if required by route_init_sync
+       //keep an eye on this to see if the extra memory is a problem
+       announcement_message: Option<msgs::ChannelAnnouncement>,
+}
+
+impl std::fmt::Display for ChannelInfo {
+       fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
+               write!(f, "features: {}, one_to_two: {}, two_to_one: {}", log_bytes!(self.features.encode()), self.one_to_two, self.two_to_one)?;
+               Ok(())
+       }
+}
+
+impl_writeable!(ChannelInfo, 0, {
+       features,
+       one_to_two,
+       two_to_one,
+       announcement_message
+});
+
+#[derive(PartialEq)]
+struct NodeInfo {
+       #[cfg(feature = "non_bitcoin_chain_hash_routing")]
+       channels: Vec<(u64, Sha256dHash)>,
+       #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
+       channels: Vec<u64>,
+
+       lowest_inbound_channel_fee_base_msat: u32,
+       lowest_inbound_channel_fee_proportional_millionths: u32,
+
+       features: GlobalFeatures,
+       last_update: u32,
+       rgb: [u8; 3],
+       alias: [u8; 32],
+       addresses: Vec<NetAddress>,
+       //this is cached here so we can send out it later if required by route_init_sync
+       //keep an eye on this to see if the extra memory is a problem
+       announcement_message: Option<msgs::NodeAnnouncement>,
+}
+
+impl std::fmt::Display for NodeInfo {
+       fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
+               write!(f, "features: {}, last_update: {}, lowest_inbound_channel_fee_base_msat: {}, lowest_inbound_channel_fee_proportional_millionths: {}, channels: {:?}", log_bytes!(self.features.encode()), self.last_update, self.lowest_inbound_channel_fee_base_msat, self.lowest_inbound_channel_fee_proportional_millionths, &self.channels[..])?;
+               Ok(())
+       }
+}
+
+impl Writeable for NodeInfo {
+       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
+               (self.channels.len() as u64).write(writer)?;
+               for ref chan in self.channels.iter() {
+                       chan.write(writer)?;
+               }
+               self.lowest_inbound_channel_fee_base_msat.write(writer)?;
+               self.lowest_inbound_channel_fee_proportional_millionths.write(writer)?;
+               self.features.write(writer)?;
+               self.last_update.write(writer)?;
+               self.rgb.write(writer)?;
+               self.alias.write(writer)?;
+               (self.addresses.len() as u64).write(writer)?;
+               for ref addr in &self.addresses {
+                       addr.write(writer)?;
+               }
+               self.announcement_message.write(writer)?;
+               Ok(())
+       }
+}
+
+const MAX_ALLOC_SIZE: u64 = 64*1024;
+
+impl<R: ::std::io::Read> Readable<R> for NodeInfo {
+       fn read(reader: &mut R) -> Result<NodeInfo, DecodeError> {
+               let channels_count: u64 = Readable::read(reader)?;
+               let mut channels = Vec::with_capacity(cmp::min(channels_count, MAX_ALLOC_SIZE / 8) as usize);
+               for _ in 0..channels_count {
+                       channels.push(Readable::read(reader)?);
+               }
+               let lowest_inbound_channel_fee_base_msat = Readable::read(reader)?;
+               let lowest_inbound_channel_fee_proportional_millionths = Readable::read(reader)?;
+               let features = Readable::read(reader)?;
+               let last_update = Readable::read(reader)?;
+               let rgb = Readable::read(reader)?;
+               let alias = Readable::read(reader)?;
+               let addresses_count: u64 = Readable::read(reader)?;
+               let mut addresses = Vec::with_capacity(cmp::min(addresses_count, MAX_ALLOC_SIZE / 40) as usize);
+               for _ in 0..addresses_count {
+                       match Readable::read(reader) {
+                               Ok(Ok(addr)) => { addresses.push(addr); },
+                               Ok(Err(_)) => return Err(DecodeError::InvalidValue),
+                               Err(DecodeError::ShortRead) => return Err(DecodeError::BadLengthDescriptor),
+                               _ => unreachable!(),
+                       }
+               }
+               let announcement_message = Readable::read(reader)?;
+               Ok(NodeInfo {
+                       channels,
+                       lowest_inbound_channel_fee_base_msat,
+                       lowest_inbound_channel_fee_proportional_millionths,
+                       features,
+                       last_update,
+                       rgb,
+                       alias,
+                       addresses,
+                       announcement_message
+               })
+       }
+}
+
+#[derive(PartialEq)]
+struct NetworkMap {
+       #[cfg(feature = "non_bitcoin_chain_hash_routing")]
+       channels: BTreeMap<(u64, Sha256dHash), ChannelInfo>,
+       #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
+       channels: BTreeMap<u64, ChannelInfo>,
+
+       our_node_id: PublicKey,
+       nodes: BTreeMap<PublicKey, NodeInfo>,
+}
+
+impl Writeable for NetworkMap {
+       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
+               (self.channels.len() as u64).write(writer)?;
+               for (ref chan_id, ref chan_info) in self.channels.iter() {
+                       (*chan_id).write(writer)?;
+                       chan_info.write(writer)?;
+               }
+               self.our_node_id.write(writer)?;
+               (self.nodes.len() as u64).write(writer)?;
+               for (ref node_id, ref node_info) in self.nodes.iter() {
+                       node_id.write(writer)?;
+                       node_info.write(writer)?;
+               }
+               Ok(())
+       }
+}
+
+impl<R: ::std::io::Read> Readable<R> for NetworkMap {
+       fn read(reader: &mut R) -> Result<NetworkMap, DecodeError> {
+               let channels_count: u64 = Readable::read(reader)?;
+               let mut channels = BTreeMap::new();
+               for _ in 0..channels_count {
+                       let chan_id: u64 = Readable::read(reader)?;
+                       let chan_info = Readable::read(reader)?;
+                       channels.insert(chan_id, chan_info);
+               }
+               let our_node_id = Readable::read(reader)?;
+               let nodes_count: u64 = Readable::read(reader)?;
+               let mut nodes = BTreeMap::new();
+               for _ in 0..nodes_count {
+                       let node_id = Readable::read(reader)?;
+                       let node_info = Readable::read(reader)?;
+                       nodes.insert(node_id, node_info);
+               }
+               Ok(NetworkMap {
+                       channels,
+                       our_node_id,
+                       nodes,
+               })
+       }
+}
+
+struct MutNetworkMap<'a> {
+       #[cfg(feature = "non_bitcoin_chain_hash_routing")]
+       channels: &'a mut BTreeMap<(u64, Sha256dHash), ChannelInfo>,
+       #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
+       channels: &'a mut BTreeMap<u64, ChannelInfo>,
+       nodes: &'a mut BTreeMap<PublicKey, NodeInfo>,
+}
+impl NetworkMap {
+       fn borrow_parts(&mut self) -> MutNetworkMap {
+               MutNetworkMap {
+                       channels: &mut self.channels,
+                       nodes: &mut self.nodes,
+               }
+       }
+}
+impl std::fmt::Display for NetworkMap {
+       fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
+               write!(f, "Node id {} network map\n[Channels]\n", log_pubkey!(self.our_node_id))?;
+               for (key, val) in self.channels.iter() {
+                       write!(f, " {}: {}\n", key, val)?;
+               }
+               write!(f, "[Nodes]\n")?;
+               for (key, val) in self.nodes.iter() {
+                       write!(f, " {}: {}\n", log_pubkey!(key), val)?;
+               }
+               Ok(())
+       }
+}
+
+impl NetworkMap {
+       #[cfg(feature = "non_bitcoin_chain_hash_routing")]
+       #[inline]
+       fn get_key(short_channel_id: u64, chain_hash: Sha256dHash) -> (u64, Sha256dHash) {
+               (short_channel_id, chain_hash)
+       }
+
+       #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
+       #[inline]
+       fn get_key(short_channel_id: u64, _: Sha256dHash) -> u64 {
+               short_channel_id
+       }
+
+       #[cfg(feature = "non_bitcoin_chain_hash_routing")]
+       #[inline]
+       fn get_short_id(id: &(u64, Sha256dHash)) -> &u64 {
+               &id.0
+       }
+
+       #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
+       #[inline]
+       fn get_short_id(id: &u64) -> &u64 {
+               id
+       }
+}
+
+/// A channel descriptor which provides a last-hop route to get_route
+pub struct RouteHint {
+       /// The node_id of the non-target end of the route
+       pub src_node_id: PublicKey,
+       /// The short_channel_id of this channel
+       pub short_channel_id: u64,
+       /// The static msat-denominated fee which must be paid to use this channel
+       pub fee_base_msat: u32,
+       /// The dynamic proportional fee which must be paid to use this channel, denominated in
+       /// millionths of the value being forwarded to the next hop.
+       pub fee_proportional_millionths: u32,
+       /// The difference in CLTV values between this node and the next node.
+       pub cltv_expiry_delta: u16,
+       /// The minimum value, in msat, which must be relayed to the next hop.
+       pub htlc_minimum_msat: u64,
+}
+
+/// Tracks a view of the network, receiving updates from peers and generating Routes to
+/// payment destinations.
+pub struct Router {
+       secp_ctx: Secp256k1<secp256k1::VerifyOnly>,
+       network_map: RwLock<NetworkMap>,
+       chain_monitor: Arc<ChainWatchInterface>,
+       logger: Arc<Logger>,
+}
+
+const SERIALIZATION_VERSION: u8 = 1;
+const MIN_SERIALIZATION_VERSION: u8 = 1;
+
+impl Writeable for Router {
+       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
+               writer.write_all(&[SERIALIZATION_VERSION; 1])?;
+               writer.write_all(&[MIN_SERIALIZATION_VERSION; 1])?;
+
+               let network = self.network_map.read().unwrap();
+               network.write(writer)?;
+               Ok(())
+       }
+}
+
+/// Arguments for the creation of a Router that are not deserialized.
+/// At a high-level, the process for deserializing a Router and resuming normal operation is:
+/// 1) Deserialize the Router by filling in this struct and calling <Router>::read(reaser, args).
+/// 2) Register the new Router with your ChainWatchInterface
+pub struct RouterReadArgs {
+       /// The ChainWatchInterface for use in the Router in the future.
+       ///
+       /// No calls to the ChainWatchInterface will be made during deserialization.
+       pub chain_monitor: Arc<ChainWatchInterface>,
+       /// The Logger for use in the ChannelManager and which may be used to log information during
+       /// deserialization.
+       pub logger: Arc<Logger>,
+}
+
+impl<R: ::std::io::Read> ReadableArgs<R, RouterReadArgs> for Router {
+       fn read(reader: &mut R, args: RouterReadArgs) -> Result<Router, DecodeError> {
+               let _ver: u8 = Readable::read(reader)?;
+               let min_ver: u8 = Readable::read(reader)?;
+               if min_ver > SERIALIZATION_VERSION {
+                       return Err(DecodeError::UnknownVersion);
+               }
+               let network_map = Readable::read(reader)?;
+               Ok(Router {
+                       secp_ctx: Secp256k1::verification_only(),
+                       network_map: RwLock::new(network_map),
+                       chain_monitor: args.chain_monitor,
+                       logger: args.logger,
+               })
+       }
+}
+
+macro_rules! secp_verify_sig {
+       ( $secp_ctx: expr, $msg: expr, $sig: expr, $pubkey: expr ) => {
+               match $secp_ctx.verify($msg, $sig, $pubkey) {
+                       Ok(_) => {},
+                       Err(_) => return Err(HandleError{err: "Invalid signature from remote node", action: None}),
+               }
+       };
+}
+
+impl RoutingMessageHandler for Router {
+       fn handle_node_announcement(&self, msg: &msgs::NodeAnnouncement) -> Result<bool, HandleError> {
+               let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
+               secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &msg.contents.node_id);
+
+               if msg.contents.features.requires_unknown_bits() {
+                       panic!("Unknown-required-features NodeAnnouncements should never deserialize!");
+               }
+
+               let mut network = self.network_map.write().unwrap();
+               match network.nodes.get_mut(&msg.contents.node_id) {
+                       None => Err(HandleError{err: "No existing channels for node_announcement", action: Some(ErrorAction::IgnoreError)}),
+                       Some(node) => {
+                               if node.last_update >= msg.contents.timestamp {
+                                       return Err(HandleError{err: "Update older than last processed update", action: Some(ErrorAction::IgnoreError)});
+                               }
+
+                               node.features = msg.contents.features.clone();
+                               node.last_update = msg.contents.timestamp;
+                               node.rgb = msg.contents.rgb;
+                               node.alias = msg.contents.alias;
+                               node.addresses = msg.contents.addresses.clone();
+
+                               let should_relay = msg.contents.excess_data.is_empty() && msg.contents.excess_address_data.is_empty() && !msg.contents.features.supports_unknown_bits();
+                               node.announcement_message = if should_relay { Some(msg.clone()) } else { None };
+                               Ok(should_relay)
+                       }
+               }
+       }
+
+       fn handle_channel_announcement(&self, msg: &msgs::ChannelAnnouncement) -> Result<bool, HandleError> {
+               if msg.contents.node_id_1 == msg.contents.node_id_2 || msg.contents.bitcoin_key_1 == msg.contents.bitcoin_key_2 {
+                       return Err(HandleError{err: "Channel announcement node had a channel with itself", action: Some(ErrorAction::IgnoreError)});
+               }
+
+               let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
+               secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_1, &msg.contents.node_id_1);
+               secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_2, &msg.contents.node_id_2);
+               secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_1, &msg.contents.bitcoin_key_1);
+               secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_2, &msg.contents.bitcoin_key_2);
+
+               if msg.contents.features.requires_unknown_bits() {
+                       panic!("Unknown-required-features ChannelAnnouncements should never deserialize!");
+               }
+
+               let checked_utxo = match self.chain_monitor.get_chain_utxo(msg.contents.chain_hash, msg.contents.short_channel_id) {
+                       Ok((script_pubkey, _value)) => {
+                               let expected_script = Builder::new().push_opcode(opcodes::all::OP_PUSHNUM_2)
+                                                                   .push_slice(&msg.contents.bitcoin_key_1.serialize())
+                                                                   .push_slice(&msg.contents.bitcoin_key_2.serialize())
+                                                                   .push_opcode(opcodes::all::OP_PUSHNUM_2)
+                                                                   .push_opcode(opcodes::all::OP_CHECKMULTISIG).into_script().to_v0_p2wsh();
+                               if script_pubkey != expected_script {
+                                       return Err(HandleError{err: "Channel announcement keys didn't match on-chain script", action: Some(ErrorAction::IgnoreError)});
+                               }
+                               //TODO: Check if value is worth storing, use it to inform routing, and compare it
+                               //to the new HTLC max field in channel_update
+                               true
+                       },
+                       Err(ChainError::NotSupported) => {
+                               // Tentatively accept, potentially exposing us to DoS attacks
+                               false
+                       },
+                       Err(ChainError::NotWatched) => {
+                               return Err(HandleError{err: "Channel announced on an unknown chain", action: Some(ErrorAction::IgnoreError)});
+                       },
+                       Err(ChainError::UnknownTx) => {
+                               return Err(HandleError{err: "Channel announced without corresponding UTXO entry", action: Some(ErrorAction::IgnoreError)});
+                       },
+               };
+
+               let mut network_lock = self.network_map.write().unwrap();
+               let network = network_lock.borrow_parts();
+
+               let should_relay = msg.contents.excess_data.is_empty() && !msg.contents.features.supports_unknown_bits();
+
+               let chan_info = ChannelInfo {
+                               features: msg.contents.features.clone(),
+                               one_to_two: DirectionalChannelInfo {
+                                       src_node_id: msg.contents.node_id_1.clone(),
+                                       last_update: 0,
+                                       enabled: false,
+                                       cltv_expiry_delta: u16::max_value(),
+                                       htlc_minimum_msat: u64::max_value(),
+                                       fee_base_msat: u32::max_value(),
+                                       fee_proportional_millionths: u32::max_value(),
+                                       last_update_message: None,
+                               },
+                               two_to_one: DirectionalChannelInfo {
+                                       src_node_id: msg.contents.node_id_2.clone(),
+                                       last_update: 0,
+                                       enabled: false,
+                                       cltv_expiry_delta: u16::max_value(),
+                                       htlc_minimum_msat: u64::max_value(),
+                                       fee_base_msat: u32::max_value(),
+                                       fee_proportional_millionths: u32::max_value(),
+                                       last_update_message: None,
+                               },
+                               announcement_message: if should_relay { Some(msg.clone()) } else { None },
+                       };
+
+               match network.channels.entry(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)) {
+                       BtreeEntry::Occupied(mut entry) => {
+                               //TODO: because asking the blockchain if short_channel_id is valid is only optional
+                               //in the blockchain API, we need to handle it smartly here, though it's unclear
+                               //exactly how...
+                               if checked_utxo {
+                                       // Either our UTXO provider is busted, there was a reorg, or the UTXO provider
+                                       // only sometimes returns results. In any case remove the previous entry. Note
+                                       // that the spec expects us to "blacklist" the node_ids involved, but we can't
+                                       // do that because
+                                       // a) we don't *require* a UTXO provider that always returns results.
+                                       // b) we don't track UTXOs of channels we know about and remove them if they
+                                       //    get reorg'd out.
+                                       // c) it's unclear how to do so without exposing ourselves to massive DoS risk.
+                                       Self::remove_channel_in_nodes(network.nodes, &entry.get(), msg.contents.short_channel_id);
+                                       *entry.get_mut() = chan_info;
+                               } else {
+                                       return Err(HandleError{err: "Already have knowledge of channel", action: Some(ErrorAction::IgnoreError)})
+                               }
+                       },
+                       BtreeEntry::Vacant(entry) => {
+                               entry.insert(chan_info);
+                       }
+               };
+
+               macro_rules! add_channel_to_node {
+                       ( $node_id: expr ) => {
+                               match network.nodes.entry($node_id) {
+                                       BtreeEntry::Occupied(node_entry) => {
+                                               node_entry.into_mut().channels.push(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash));
+                                       },
+                                       BtreeEntry::Vacant(node_entry) => {
+                                               node_entry.insert(NodeInfo {
+                                                       channels: vec!(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)),
+                                                       lowest_inbound_channel_fee_base_msat: u32::max_value(),
+                                                       lowest_inbound_channel_fee_proportional_millionths: u32::max_value(),
+                                                       features: GlobalFeatures::new(),
+                                                       last_update: 0,
+                                                       rgb: [0; 3],
+                                                       alias: [0; 32],
+                                                       addresses: Vec::new(),
+                                                       announcement_message: None,
+                                               });
+                                       }
+                               }
+                       };
+               }
+
+               add_channel_to_node!(msg.contents.node_id_1);
+               add_channel_to_node!(msg.contents.node_id_2);
+
+               Ok(should_relay)
+       }
+
+       fn handle_htlc_fail_channel_update(&self, update: &msgs::HTLCFailChannelUpdate) {
+               match update {
+                       &msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg } => {
+                               let _ = self.handle_channel_update(msg);
+                       },
+                       &msgs::HTLCFailChannelUpdate::ChannelClosed { ref short_channel_id, ref is_permanent } => {
+                               let mut network = self.network_map.write().unwrap();
+                               if *is_permanent {
+                                       if let Some(chan) = network.channels.remove(short_channel_id) {
+                                               Self::remove_channel_in_nodes(&mut network.nodes, &chan, *short_channel_id);
+                                       }
+                               } else {
+                                       if let Some(chan) = network.channels.get_mut(short_channel_id) {
+                                               chan.one_to_two.enabled = false;
+                                               chan.two_to_one.enabled = false;
+                                       }
+                               }
+                       },
+                       &msgs::HTLCFailChannelUpdate::NodeFailure { ref node_id, ref is_permanent } => {
+                               if *is_permanent {
+                                       //TODO: Wholly remove the node
+                               } else {
+                                       self.mark_node_bad(node_id, false);
+                               }
+                       },
+               }
+       }
+
+       fn handle_channel_update(&self, msg: &msgs::ChannelUpdate) -> Result<bool, HandleError> {
+               let mut network = self.network_map.write().unwrap();
+               let dest_node_id;
+               let chan_enabled = msg.contents.flags & (1 << 1) != (1 << 1);
+               let chan_was_enabled;
+
+               match network.channels.get_mut(&NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)) {
+                       None => return Err(HandleError{err: "Couldn't find channel for update", action: Some(ErrorAction::IgnoreError)}),
+                       Some(channel) => {
+                               macro_rules! maybe_update_channel_info {
+                                       ( $target: expr) => {
+                                               if $target.last_update >= msg.contents.timestamp {
+                                                       return Err(HandleError{err: "Update older than last processed update", action: Some(ErrorAction::IgnoreError)});
+                                               }
+                                               chan_was_enabled = $target.enabled;
+                                               $target.last_update = msg.contents.timestamp;
+                                               $target.enabled = chan_enabled;
+                                               $target.cltv_expiry_delta = msg.contents.cltv_expiry_delta;
+                                               $target.htlc_minimum_msat = msg.contents.htlc_minimum_msat;
+                                               $target.fee_base_msat = msg.contents.fee_base_msat;
+                                               $target.fee_proportional_millionths = msg.contents.fee_proportional_millionths;
+                                               $target.last_update_message = if msg.contents.excess_data.is_empty() {
+                                                       Some(msg.clone())
+                                               } else {
+                                                       None
+                                               };
+                                       }
+                               }
+                               let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
+                               if msg.contents.flags & 1 == 1 {
+                                       dest_node_id = channel.one_to_two.src_node_id.clone();
+                                       secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &channel.two_to_one.src_node_id);
+                                       maybe_update_channel_info!(channel.two_to_one);
+                               } else {
+                                       dest_node_id = channel.two_to_one.src_node_id.clone();
+                                       secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &channel.one_to_two.src_node_id);
+                                       maybe_update_channel_info!(channel.one_to_two);
+                               }
+                       }
+               }
+
+               if chan_enabled {
+                       let node = network.nodes.get_mut(&dest_node_id).unwrap();
+                       node.lowest_inbound_channel_fee_base_msat = cmp::min(node.lowest_inbound_channel_fee_base_msat, msg.contents.fee_base_msat);
+                       node.lowest_inbound_channel_fee_proportional_millionths = cmp::min(node.lowest_inbound_channel_fee_proportional_millionths, msg.contents.fee_proportional_millionths);
+               } else if chan_was_enabled {
+                       let mut lowest_inbound_channel_fee_base_msat = u32::max_value();
+                       let mut lowest_inbound_channel_fee_proportional_millionths = u32::max_value();
+
+                       {
+                               let node = network.nodes.get(&dest_node_id).unwrap();
+
+                               for chan_id in node.channels.iter() {
+                                       let chan = network.channels.get(chan_id).unwrap();
+                                       if chan.one_to_two.src_node_id == dest_node_id {
+                                               lowest_inbound_channel_fee_base_msat = cmp::min(lowest_inbound_channel_fee_base_msat, chan.two_to_one.fee_base_msat);
+                                               lowest_inbound_channel_fee_proportional_millionths = cmp::min(lowest_inbound_channel_fee_proportional_millionths, chan.two_to_one.fee_proportional_millionths);
+                                       } else {
+                                               lowest_inbound_channel_fee_base_msat = cmp::min(lowest_inbound_channel_fee_base_msat, chan.one_to_two.fee_base_msat);
+                                               lowest_inbound_channel_fee_proportional_millionths = cmp::min(lowest_inbound_channel_fee_proportional_millionths, chan.one_to_two.fee_proportional_millionths);
+                                       }
+                               }
+                       }
+
+                       //TODO: satisfy the borrow-checker without a double-map-lookup :(
+                       let mut_node = network.nodes.get_mut(&dest_node_id).unwrap();
+                       mut_node.lowest_inbound_channel_fee_base_msat = lowest_inbound_channel_fee_base_msat;
+                       mut_node.lowest_inbound_channel_fee_proportional_millionths = lowest_inbound_channel_fee_proportional_millionths;
+               }
+
+               Ok(msg.contents.excess_data.is_empty())
+       }
+
+
+       fn get_next_channel_announcements(&self, starting_point: u64, batch_amount: u8) -> Vec<(msgs::ChannelAnnouncement, msgs::ChannelUpdate,msgs::ChannelUpdate)> {
+               let mut result = Vec::with_capacity(batch_amount as usize);
+               let network = self.network_map.read().unwrap();
+               let mut iter = network.channels.range(starting_point..);
+               while result.len() < batch_amount as usize {
+                       if let Some((_, ref chan)) = iter.next() {
+                               if chan.announcement_message.is_some() &&
+                                               chan.one_to_two.last_update_message.is_some() &&
+                                               chan.two_to_one.last_update_message.is_some() {
+                                       result.push((chan.announcement_message.clone().unwrap(),
+                                               chan.one_to_two.last_update_message.clone().unwrap(),
+                                               chan.two_to_one.last_update_message.clone().unwrap()));
+                               } else {
+                                       // TODO: We may end up sending un-announced channel_updates if we are sending
+                                       // initial sync data while receiving announce/updates for this channel.
+                               }
+                       } else {
+                               return result;
+                       }
+               }
+               result
+       }
+
+       fn get_next_node_announcements(&self, starting_point: Option<&PublicKey>, batch_amount: u8) -> Vec<msgs::NodeAnnouncement> {
+               let mut result = Vec::with_capacity(batch_amount as usize);
+               let network = self.network_map.read().unwrap();
+               let mut iter = if let Some(pubkey) = starting_point {
+                               let mut iter = network.nodes.range((*pubkey)..);
+                               iter.next();
+                               iter
+                       } else {
+                               network.nodes.range(..)
+                       };
+               while result.len() < batch_amount as usize {
+                       if let Some((_, ref node)) = iter.next() {
+                               if node.announcement_message.is_some() {
+                                       result.push(node.announcement_message.clone().unwrap());
+                               }
+                       } else {
+                               return result;
+                       }
+               }
+               result
+       }
+}
+
+#[derive(Eq, PartialEq)]
+struct RouteGraphNode {
+       pubkey: PublicKey,
+       lowest_fee_to_peer_through_node: u64,
+       lowest_fee_to_node: u64,
+}
+
+impl cmp::Ord for RouteGraphNode {
+       fn cmp(&self, other: &RouteGraphNode) -> cmp::Ordering {
+               other.lowest_fee_to_peer_through_node.cmp(&self.lowest_fee_to_peer_through_node)
+                       .then_with(|| other.pubkey.serialize().cmp(&self.pubkey.serialize()))
+       }
+}
+
+impl cmp::PartialOrd for RouteGraphNode {
+       fn partial_cmp(&self, other: &RouteGraphNode) -> Option<cmp::Ordering> {
+               Some(self.cmp(other))
+       }
+}
+
+struct DummyDirectionalChannelInfo {
+       src_node_id: PublicKey,
+       cltv_expiry_delta: u32,
+       htlc_minimum_msat: u64,
+       fee_base_msat: u32,
+       fee_proportional_millionths: u32,
+}
+
+impl Router {
+       /// Creates a new router with the given node_id to be used as the source for get_route()
+       pub fn new(our_pubkey: PublicKey, chain_monitor: Arc<ChainWatchInterface>, logger: Arc<Logger>) -> Router {
+               let mut nodes = BTreeMap::new();
+               nodes.insert(our_pubkey.clone(), NodeInfo {
+                       channels: Vec::new(),
+                       lowest_inbound_channel_fee_base_msat: u32::max_value(),
+                       lowest_inbound_channel_fee_proportional_millionths: u32::max_value(),
+                       features: GlobalFeatures::new(),
+                       last_update: 0,
+                       rgb: [0; 3],
+                       alias: [0; 32],
+                       addresses: Vec::new(),
+                       announcement_message: None,
+               });
+               Router {
+                       secp_ctx: Secp256k1::verification_only(),
+                       network_map: RwLock::new(NetworkMap {
+                               channels: BTreeMap::new(),
+                               our_node_id: our_pubkey,
+                               nodes: nodes,
+                       }),
+                       chain_monitor,
+                       logger,
+               }
+       }
+
+       /// Dumps the entire network view of this Router to the logger provided in the constructor at
+       /// level Trace
+       pub fn trace_state(&self) {
+               log_trace!(self, "{}", self.network_map.read().unwrap());
+       }
+
+       /// Get network addresses by node id
+       pub fn get_addresses(&self, pubkey: &PublicKey) -> Option<Vec<NetAddress>> {
+               let network = self.network_map.read().unwrap();
+               network.nodes.get(pubkey).map(|n| n.addresses.clone())
+       }
+
+       /// Marks a node as having failed a route. This will avoid re-using the node in routes for now,
+       /// with an exponential decay in node "badness". Note that there is deliberately no
+       /// mark_channel_bad as a node may simply lie and suggest that an upstream channel from it is
+       /// what failed the route and not the node itself. Instead, setting the blamed_upstream_node
+       /// boolean will reduce the penalty, returning the node to usability faster. If the node is
+       /// behaving correctly, it will disable the failing channel and we will use it again next time.
+       pub fn mark_node_bad(&self, _node_id: &PublicKey, _blamed_upstream_node: bool) {
+               unimplemented!();
+       }
+
+       fn remove_channel_in_nodes(nodes: &mut BTreeMap<PublicKey, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
+               macro_rules! remove_from_node {
+                       ($node_id: expr) => {
+                               if let BtreeEntry::Occupied(mut entry) = nodes.entry($node_id) {
+                                       entry.get_mut().channels.retain(|chan_id| {
+                                               short_channel_id != *NetworkMap::get_short_id(chan_id)
+                                       });
+                                       if entry.get().channels.is_empty() {
+                                               entry.remove_entry();
+                                       }
+                               } else {
+                                       panic!("Had channel that pointed to unknown node (ie inconsistent network map)!");
+                               }
+                       }
+               }
+               remove_from_node!(chan.one_to_two.src_node_id);
+               remove_from_node!(chan.two_to_one.src_node_id);
+       }
+
+       /// Gets a route from us to the given target node.
+       ///
+       /// Extra routing hops between known nodes and the target will be used if they are included in
+       /// last_hops.
+       ///
+       /// If some channels aren't announced, it may be useful to fill in a first_hops with the
+       /// results from a local ChannelManager::list_usable_channels() call. If it is filled in, our
+       /// (this Router's) view of our local channels will be ignored, and only those in first_hops
+       /// will be used.
+       ///
+       /// Panics if first_hops contains channels without short_channel_ids
+       /// (ChannelManager::list_usable_channels will never include such channels).
+       ///
+       /// The fees on channels from us to next-hops are ignored (as they are assumed to all be
+       /// equal), however the enabled/disabled bit on such channels as well as the htlc_minimum_msat
+       /// *is* checked as they may change based on the receiving node.
+       pub fn get_route(&self, target: &PublicKey, first_hops: Option<&[channelmanager::ChannelDetails]>, last_hops: &[RouteHint], final_value_msat: u64, final_cltv: u32) -> Result<Route, HandleError> {
+               // TODO: Obviously *only* using total fee cost sucks. We should consider weighting by
+               // uptime/success in using a node in the past.
+               let network = self.network_map.read().unwrap();
+
+               if *target == network.our_node_id {
+                       return Err(HandleError{err: "Cannot generate a route to ourselves", action: None});
+               }
+
+               if final_value_msat > 21_000_000 * 1_0000_0000 * 1000 {
+                       return Err(HandleError{err: "Cannot generate a route of more value than all existing satoshis", action: None});
+               }
+
+               // We do a dest-to-source Dijkstra's sorting by each node's distance from the destination
+               // plus the minimum per-HTLC fee to get from it to another node (aka "shitty A*").
+               // TODO: There are a few tweaks we could do, including possibly pre-calculating more stuff
+               // to use as the A* heuristic beyond just the cost to get one node further than the current
+               // one.
+
+               let dummy_directional_info = DummyDirectionalChannelInfo { // used for first_hops routes
+                       src_node_id: network.our_node_id.clone(),
+                       cltv_expiry_delta: 0,
+                       htlc_minimum_msat: 0,
+                       fee_base_msat: 0,
+                       fee_proportional_millionths: 0,
+               };
+
+               let mut targets = BinaryHeap::new(); //TODO: Do we care about switching to eg Fibbonaci heap?
+               let mut dist = HashMap::with_capacity(network.nodes.len());
+
+               let mut first_hop_targets = HashMap::with_capacity(if first_hops.is_some() { first_hops.as_ref().unwrap().len() } else { 0 });
+               if let Some(hops) = first_hops {
+                       for chan in hops {
+                               let short_channel_id = chan.short_channel_id.expect("first_hops should be filled in with usable channels, not pending ones");
+                               if chan.remote_network_id == *target {
+                                       return Ok(Route {
+                                               hops: vec![RouteHop {
+                                                       pubkey: chan.remote_network_id,
+                                                       short_channel_id,
+                                                       fee_msat: final_value_msat,
+                                                       cltv_expiry_delta: final_cltv,
+                                               }],
+                                       });
+                               }
+                               first_hop_targets.insert(chan.remote_network_id, short_channel_id);
+                       }
+                       if first_hop_targets.is_empty() {
+                               return Err(HandleError{err: "Cannot route when there are no outbound routes away from us", action: None});
+                       }
+               }
+
+               macro_rules! add_entry {
+                       // Adds entry which goes from the node pointed to by $directional_info to
+                       // $dest_node_id over the channel with id $chan_id with fees described in
+                       // $directional_info.
+                       ( $chan_id: expr, $dest_node_id: expr, $directional_info: expr, $starting_fee_msat: expr ) => {
+                               //TODO: Explore simply adding fee to hit htlc_minimum_msat
+                               if $starting_fee_msat as u64 + final_value_msat >= $directional_info.htlc_minimum_msat {
+                                       let proportional_fee_millions = ($starting_fee_msat + final_value_msat).checked_mul($directional_info.fee_proportional_millionths as u64);
+                                       if let Some(new_fee) = proportional_fee_millions.and_then(|part| {
+                                                       ($directional_info.fee_base_msat as u64).checked_add(part / 1000000) })
+                                       {
+                                               let mut total_fee = $starting_fee_msat as u64;
+                                               let hm_entry = dist.entry(&$directional_info.src_node_id);
+                                               let old_entry = hm_entry.or_insert_with(|| {
+                                                       let node = network.nodes.get(&$directional_info.src_node_id).unwrap();
+                                                       (u64::max_value(),
+                                                               node.lowest_inbound_channel_fee_base_msat,
+                                                               node.lowest_inbound_channel_fee_proportional_millionths,
+                                                               RouteHop {
+                                                                       pubkey: $dest_node_id.clone(),
+                                                                       short_channel_id: 0,
+                                                                       fee_msat: 0,
+                                                                       cltv_expiry_delta: 0,
+                                                       })
+                                               });
+                                               if $directional_info.src_node_id != network.our_node_id {
+                                                       // Ignore new_fee for channel-from-us as we assume all channels-from-us
+                                                       // will have the same effective-fee
+                                                       total_fee += new_fee;
+                                                       if let Some(fee_inc) = final_value_msat.checked_add(total_fee).and_then(|inc| { (old_entry.2 as u64).checked_mul(inc) }) {
+                                                               total_fee += fee_inc / 1000000 + (old_entry.1 as u64);
+                                                       } else {
+                                                               // max_value means we'll always fail the old_entry.0 > total_fee check
+                                                               total_fee = u64::max_value();
+                                                       }
+                                               }
+                                               let new_graph_node = RouteGraphNode {
+                                                       pubkey: $directional_info.src_node_id,
+                                                       lowest_fee_to_peer_through_node: total_fee,
+                                                       lowest_fee_to_node: $starting_fee_msat as u64 + new_fee,
+                                               };
+                                               if old_entry.0 > total_fee {
+                                                       targets.push(new_graph_node);
+                                                       old_entry.0 = total_fee;
+                                                       old_entry.3 = RouteHop {
+                                                               pubkey: $dest_node_id.clone(),
+                                                               short_channel_id: $chan_id.clone(),
+                                                               fee_msat: new_fee, // This field is ignored on the last-hop anyway
+                                                               cltv_expiry_delta: $directional_info.cltv_expiry_delta as u32,
+                                                       }
+                                               }
+                                       }
+                               }
+                       };
+               }
+
+               macro_rules! add_entries_to_cheapest_to_target_node {
+                       ( $node: expr, $node_id: expr, $fee_to_target_msat: expr ) => {
+                               if first_hops.is_some() {
+                                       if let Some(first_hop) = first_hop_targets.get(&$node_id) {
+                                               add_entry!(first_hop, $node_id, dummy_directional_info, $fee_to_target_msat);
+                                       }
+                               }
+
+                               for chan_id in $node.channels.iter() {
+                                       let chan = network.channels.get(chan_id).unwrap();
+                                       if chan.one_to_two.src_node_id == *$node_id {
+                                               // ie $node is one, ie next hop in A* is two, via the two_to_one channel
+                                               if first_hops.is_none() || chan.two_to_one.src_node_id != network.our_node_id {
+                                                       if chan.two_to_one.enabled {
+                                                               add_entry!(chan_id, chan.one_to_two.src_node_id, chan.two_to_one, $fee_to_target_msat);
+                                                       }
+                                               }
+                                       } else {
+                                               if first_hops.is_none() || chan.one_to_two.src_node_id != network.our_node_id {
+                                                       if chan.one_to_two.enabled {
+                                                               add_entry!(chan_id, chan.two_to_one.src_node_id, chan.one_to_two, $fee_to_target_msat);
+                                                       }
+                                               }
+                                       }
+                               }
+                       };
+               }
+
+               match network.nodes.get(target) {
+                       None => {},
+                       Some(node) => {
+                               add_entries_to_cheapest_to_target_node!(node, target, 0);
+                       },
+               }
+
+               for hop in last_hops.iter() {
+                       if first_hops.is_none() || hop.src_node_id != network.our_node_id { // first_hop overrules last_hops
+                               if network.nodes.get(&hop.src_node_id).is_some() {
+                                       if first_hops.is_some() {
+                                               if let Some(first_hop) = first_hop_targets.get(&hop.src_node_id) {
+                                                       add_entry!(first_hop, hop.src_node_id, dummy_directional_info, 0);
+                                               }
+                                       }
+                                       add_entry!(hop.short_channel_id, target, hop, 0);
+                               }
+                       }
+               }
+
+               while let Some(RouteGraphNode { pubkey, lowest_fee_to_node, .. }) = targets.pop() {
+                       if pubkey == network.our_node_id {
+                               let mut res = vec!(dist.remove(&network.our_node_id).unwrap().3);
+                               while res.last().unwrap().pubkey != *target {
+                                       let new_entry = match dist.remove(&res.last().unwrap().pubkey) {
+                                               Some(hop) => hop.3,
+                                               None => return Err(HandleError{err: "Failed to find a non-fee-overflowing path to the given destination", action: None}),
+                                       };
+                                       res.last_mut().unwrap().fee_msat = new_entry.fee_msat;
+                                       res.last_mut().unwrap().cltv_expiry_delta = new_entry.cltv_expiry_delta;
+                                       res.push(new_entry);
+                               }
+                               res.last_mut().unwrap().fee_msat = final_value_msat;
+                               res.last_mut().unwrap().cltv_expiry_delta = final_cltv;
+                               let route = Route { hops: res };
+                               log_trace!(self, "Got route: {}", log_route!(route));
+                               return Ok(route);
+                       }
+
+                       match network.nodes.get(&pubkey) {
+                               None => {},
+                               Some(node) => {
+                                       add_entries_to_cheapest_to_target_node!(node, &pubkey, lowest_fee_to_node);
+                               },
+                       }
+               }
+
+               Err(HandleError{err: "Failed to find a path to the given destination", action: None})
+       }
+}
+
+#[cfg(test)]
+mod tests {
+       use chain::chaininterface;
+       use ln::channelmanager;
+       use ln::router::{Router,NodeInfo,NetworkMap,ChannelInfo,DirectionalChannelInfo,RouteHint};
+       use ln::msgs::GlobalFeatures;
+       use util::test_utils;
+       use util::test_utils::TestVecWriter;
+       use util::logger::Logger;
+       use util::ser::{Writeable, Readable};
+
+       use bitcoin_hashes::sha256d::Hash as Sha256dHash;
+       use bitcoin_hashes::Hash;
+       use bitcoin::network::constants::Network;
+
+       use hex;
+
+       use secp256k1::key::{PublicKey,SecretKey};
+       use secp256k1::Secp256k1;
+
+       use std::sync::Arc;
+
+       #[test]
+       fn route_test() {
+               let secp_ctx = Secp256k1::new();
+               let our_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap());
+               let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::new());
+               let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, Arc::clone(&logger)));
+               let router = Router::new(our_id, chain_monitor, Arc::clone(&logger));
+
+               // Build network from our_id to node8:
+               //
+               //        -1(1)2-  node1  -1(3)2-
+               //       /                       \
+               // our_id -1(12)2- node8 -1(13)2--- node3
+               //       \                       /
+               //        -1(2)2-  node2  -1(4)2-
+               //
+               //
+               // chan1  1-to-2: disabled
+               // chan1  2-to-1: enabled, 0 fee
+               //
+               // chan2  1-to-2: enabled, ignored fee
+               // chan2  2-to-1: enabled, 0 fee
+               //
+               // chan3  1-to-2: enabled, 0 fee
+               // chan3  2-to-1: enabled, 100 msat fee
+               //
+               // chan4  1-to-2: enabled, 100% fee
+               // chan4  2-to-1: enabled, 0 fee
+               //
+               // chan12 1-to-2: enabled, ignored fee
+               // chan12 2-to-1: enabled, 0 fee
+               //
+               // chan13 1-to-2: enabled, 200% fee
+               // chan13 2-to-1: enabled, 0 fee
+               //
+               //
+               //       -1(5)2- node4 -1(8)2--
+               //       |         2          |
+               //       |       (11)         |
+               //      /          1           \
+               // node3--1(6)2- node5 -1(9)2--- node7 (not in global route map)
+               //      \                      /
+               //       -1(7)2- node6 -1(10)2-
+               //
+               // chan5  1-to-2: enabled, 100 msat fee
+               // chan5  2-to-1: enabled, 0 fee
+               //
+               // chan6  1-to-2: enabled, 0 fee
+               // chan6  2-to-1: enabled, 0 fee
+               //
+               // chan7  1-to-2: enabled, 100% fee
+               // chan7  2-to-1: enabled, 0 fee
+               //
+               // chan8  1-to-2: enabled, variable fee (0 then 1000 msat)
+               // chan8  2-to-1: enabled, 0 fee
+               //
+               // chan9  1-to-2: enabled, 1001 msat fee
+               // chan9  2-to-1: enabled, 0 fee
+               //
+               // chan10 1-to-2: enabled, 0 fee
+               // chan10 2-to-1: enabled, 0 fee
+               //
+               // chan11 1-to-2: enabled, 0 fee
+               // chan11 2-to-1: enabled, 0 fee
+
+               let node1 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap()[..]).unwrap());
+               let node2 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0303030303030303030303030303030303030303030303030303030303030303").unwrap()[..]).unwrap());
+               let node3 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0404040404040404040404040404040404040404040404040404040404040404").unwrap()[..]).unwrap());
+               let node4 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0505050505050505050505050505050505050505050505050505050505050505").unwrap()[..]).unwrap());
+               let node5 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0606060606060606060606060606060606060606060606060606060606060606").unwrap()[..]).unwrap());
+               let node6 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0707070707070707070707070707070707070707070707070707070707070707").unwrap()[..]).unwrap());
+               let node7 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0808080808080808080808080808080808080808080808080808080808080808").unwrap()[..]).unwrap());
+               let node8 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0909090909090909090909090909090909090909090909090909090909090909").unwrap()[..]).unwrap());
+
+               let zero_hash = Sha256dHash::hash(&[0; 32]);
+
+               {
+                       let mut network = router.network_map.write().unwrap();
+
+                       network.nodes.insert(node1.clone(), NodeInfo {
+                               channels: vec!(NetworkMap::get_key(1, zero_hash.clone()), NetworkMap::get_key(3, zero_hash.clone())),
+                               lowest_inbound_channel_fee_base_msat: 100,
+                               lowest_inbound_channel_fee_proportional_millionths: 0,
+                               features: GlobalFeatures::new(),
+                               last_update: 1,
+                               rgb: [0; 3],
+                               alias: [0; 32],
+                               addresses: Vec::new(),
+                               announcement_message: None,
+                       });
+                       network.channels.insert(NetworkMap::get_key(1, zero_hash.clone()), ChannelInfo {
+                               features: GlobalFeatures::new(),
+                               one_to_two: DirectionalChannelInfo {
+                                       src_node_id: our_id.clone(),
+                                       last_update: 0,
+                                       enabled: false,
+                                       cltv_expiry_delta: u16::max_value(), // This value should be ignored
+                                       htlc_minimum_msat: 0,
+                                       fee_base_msat: u32::max_value(), // This value should be ignored
+                                       fee_proportional_millionths: u32::max_value(), // This value should be ignored
+                                       last_update_message: None,
+                               }, two_to_one: DirectionalChannelInfo {
+                                       src_node_id: node1.clone(),
+                                       last_update: 0,
+                                       enabled: true,
+                                       cltv_expiry_delta: 0,
+                                       htlc_minimum_msat: 0,
+                                       fee_base_msat: 0,
+                                       fee_proportional_millionths: 0,
+                                       last_update_message: None,
+                               },
+                               announcement_message: None,
+                       });
+                       network.nodes.insert(node2.clone(), NodeInfo {
+                               channels: vec!(NetworkMap::get_key(2, zero_hash.clone()), NetworkMap::get_key(4, zero_hash.clone())),
+                               lowest_inbound_channel_fee_base_msat: 0,
+                               lowest_inbound_channel_fee_proportional_millionths: 0,
+                               features: GlobalFeatures::new(),
+                               last_update: 1,
+                               rgb: [0; 3],
+                               alias: [0; 32],
+                               addresses: Vec::new(),
+                               announcement_message: None,
+                       });
+                       network.channels.insert(NetworkMap::get_key(2, zero_hash.clone()), ChannelInfo {
+                               features: GlobalFeatures::new(),
+                               one_to_two: DirectionalChannelInfo {
+                                       src_node_id: our_id.clone(),
+                                       last_update: 0,
+                                       enabled: true,
+                                       cltv_expiry_delta: u16::max_value(), // This value should be ignored
+                                       htlc_minimum_msat: 0,
+                                       fee_base_msat: u32::max_value(), // This value should be ignored
+                                       fee_proportional_millionths: u32::max_value(), // This value should be ignored
+                                       last_update_message: None,
+                               }, two_to_one: DirectionalChannelInfo {
+                                       src_node_id: node2.clone(),
+                                       last_update: 0,
+                                       enabled: true,
+                                       cltv_expiry_delta: 0,
+                                       htlc_minimum_msat: 0,
+                                       fee_base_msat: 0,
+                                       fee_proportional_millionths: 0,
+                                       last_update_message: None,
+                               },
+                               announcement_message: None,
+                       });
+                       network.nodes.insert(node8.clone(), NodeInfo {
+                               channels: vec!(NetworkMap::get_key(12, zero_hash.clone()), NetworkMap::get_key(13, zero_hash.clone())),
+                               lowest_inbound_channel_fee_base_msat: 0,
+                               lowest_inbound_channel_fee_proportional_millionths: 0,
+                               features: GlobalFeatures::new(),
+                               last_update: 1,
+                               rgb: [0; 3],
+                               alias: [0; 32],
+                               addresses: Vec::new(),
+                               announcement_message: None,
+                       });
+                       network.channels.insert(NetworkMap::get_key(12, zero_hash.clone()), ChannelInfo {
+                               features: GlobalFeatures::new(),
+                               one_to_two: DirectionalChannelInfo {
+                                       src_node_id: our_id.clone(),
+                                       last_update: 0,
+                                       enabled: true,
+                                       cltv_expiry_delta: u16::max_value(), // This value should be ignored
+                                       htlc_minimum_msat: 0,
+                                       fee_base_msat: u32::max_value(), // This value should be ignored
+                                       fee_proportional_millionths: u32::max_value(), // This value should be ignored
+                                       last_update_message: None,
+                               }, two_to_one: DirectionalChannelInfo {
+                                       src_node_id: node8.clone(),
+                                       last_update: 0,
+                                       enabled: true,
+                                       cltv_expiry_delta: 0,
+                                       htlc_minimum_msat: 0,
+                                       fee_base_msat: 0,
+                                       fee_proportional_millionths: 0,
+                                       last_update_message: None,
+                               },
+                               announcement_message: None,
+                       });
+                       network.nodes.insert(node3.clone(), NodeInfo {
+                               channels: vec!(
+                                       NetworkMap::get_key(3, zero_hash.clone()),
+                                       NetworkMap::get_key(4, zero_hash.clone()),
+                                       NetworkMap::get_key(13, zero_hash.clone()),
+                                       NetworkMap::get_key(5, zero_hash.clone()),
+                                       NetworkMap::get_key(6, zero_hash.clone()),
+                                       NetworkMap::get_key(7, zero_hash.clone())),
+                               lowest_inbound_channel_fee_base_msat: 0,
+                               lowest_inbound_channel_fee_proportional_millionths: 0,
+                               features: GlobalFeatures::new(),
+                               last_update: 1,
+                               rgb: [0; 3],
+                               alias: [0; 32],
+                               addresses: Vec::new(),
+                               announcement_message: None,
+                       });
+                       network.channels.insert(NetworkMap::get_key(3, zero_hash.clone()), ChannelInfo {
+                               features: GlobalFeatures::new(),
+                               one_to_two: DirectionalChannelInfo {
+                                       src_node_id: node1.clone(),
+                                       last_update: 0,
+                                       enabled: true,
+                                       cltv_expiry_delta: (3 << 8) | 1,
+                                       htlc_minimum_msat: 0,
+                                       fee_base_msat: 0,
+                                       fee_proportional_millionths: 0,
+                                       last_update_message: None,
+                               }, two_to_one: DirectionalChannelInfo {
+                                       src_node_id: node3.clone(),
+                                       last_update: 0,
+                                       enabled: true,
+                                       cltv_expiry_delta: (3 << 8) | 2,
+                                       htlc_minimum_msat: 0,
+                                       fee_base_msat: 100,
+                                       fee_proportional_millionths: 0,
+                                       last_update_message: None,
+                               },
+                               announcement_message: None,
+                       });
+                       network.channels.insert(NetworkMap::get_key(4, zero_hash.clone()), ChannelInfo {
+                               features: GlobalFeatures::new(),
+                               one_to_two: DirectionalChannelInfo {
+                                       src_node_id: node2.clone(),
+                                       last_update: 0,
+                                       enabled: true,
+                                       cltv_expiry_delta: (4 << 8) | 1,
+                                       htlc_minimum_msat: 0,
+                                       fee_base_msat: 0,
+                                       fee_proportional_millionths: 1000000,
+                                       last_update_message: None,
+                               }, two_to_one: DirectionalChannelInfo {
+                                       src_node_id: node3.clone(),
+                                       last_update: 0,
+                                       enabled: true,
+                                       cltv_expiry_delta: (4 << 8) | 2,
+                                       htlc_minimum_msat: 0,
+                                       fee_base_msat: 0,
+                                       fee_proportional_millionths: 0,
+                                       last_update_message: None,
+                               },
+                               announcement_message: None,
+                       });
+                       network.channels.insert(NetworkMap::get_key(13, zero_hash.clone()), ChannelInfo {
+                               features: GlobalFeatures::new(),
+                               one_to_two: DirectionalChannelInfo {
+                                       src_node_id: node8.clone(),
+                                       last_update: 0,
+                                       enabled: true,
+                                       cltv_expiry_delta: (13 << 8) | 1,
+                                       htlc_minimum_msat: 0,
+                                       fee_base_msat: 0,
+                                       fee_proportional_millionths: 2000000,
+                                       last_update_message: None,
+                               }, two_to_one: DirectionalChannelInfo {
+                                       src_node_id: node3.clone(),
+                                       last_update: 0,
+                                       enabled: true,
+                                       cltv_expiry_delta: (13 << 8) | 2,
+                                       htlc_minimum_msat: 0,
+                                       fee_base_msat: 0,
+                                       fee_proportional_millionths: 0,
+                                       last_update_message: None,
+                               },
+                               announcement_message: None,
+                       });
+                       network.nodes.insert(node4.clone(), NodeInfo {
+                               channels: vec!(NetworkMap::get_key(5, zero_hash.clone()), NetworkMap::get_key(11, zero_hash.clone())),
+                               lowest_inbound_channel_fee_base_msat: 0,
+                               lowest_inbound_channel_fee_proportional_millionths: 0,
+                               features: GlobalFeatures::new(),
+                               last_update: 1,
+                               rgb: [0; 3],
+                               alias: [0; 32],
+                               addresses: Vec::new(),
+                               announcement_message: None,
+                       });
+                       network.channels.insert(NetworkMap::get_key(5, zero_hash.clone()), ChannelInfo {
+                               features: GlobalFeatures::new(),
+                               one_to_two: DirectionalChannelInfo {
+                                       src_node_id: node3.clone(),
+                                       last_update: 0,
+                                       enabled: true,
+                                       cltv_expiry_delta: (5 << 8) | 1,
+                                       htlc_minimum_msat: 0,
+                                       fee_base_msat: 100,
+                                       fee_proportional_millionths: 0,
+                                       last_update_message: None,
+                               }, two_to_one: DirectionalChannelInfo {
+                                       src_node_id: node4.clone(),
+                                       last_update: 0,
+                                       enabled: true,
+                                       cltv_expiry_delta: (5 << 8) | 2,
+                                       htlc_minimum_msat: 0,
+                                       fee_base_msat: 0,
+                                       fee_proportional_millionths: 0,
+                                       last_update_message: None,
+                               },
+                               announcement_message: None,
+                       });
+                       network.nodes.insert(node5.clone(), NodeInfo {
+                               channels: vec!(NetworkMap::get_key(6, zero_hash.clone()), NetworkMap::get_key(11, zero_hash.clone())),
+                               lowest_inbound_channel_fee_base_msat: 0,
+                               lowest_inbound_channel_fee_proportional_millionths: 0,
+                               features: GlobalFeatures::new(),
+                               last_update: 1,
+                               rgb: [0; 3],
+                               alias: [0; 32],
+                               addresses: Vec::new(),
+                               announcement_message: None,
+                       });
+                       network.channels.insert(NetworkMap::get_key(6, zero_hash.clone()), ChannelInfo {
+                               features: GlobalFeatures::new(),
+                               one_to_two: DirectionalChannelInfo {
+                                       src_node_id: node3.clone(),
+                                       last_update: 0,
+                                       enabled: true,
+                                       cltv_expiry_delta: (6 << 8) | 1,
+                                       htlc_minimum_msat: 0,
+                                       fee_base_msat: 0,
+                                       fee_proportional_millionths: 0,
+                                       last_update_message: None,
+                               }, two_to_one: DirectionalChannelInfo {
+                                       src_node_id: node5.clone(),
+                                       last_update: 0,
+                                       enabled: true,
+                                       cltv_expiry_delta: (6 << 8) | 2,
+                                       htlc_minimum_msat: 0,
+                                       fee_base_msat: 0,
+                                       fee_proportional_millionths: 0,
+                                       last_update_message: None,
+                               },
+                               announcement_message: None,
+                       });
+                       network.channels.insert(NetworkMap::get_key(11, zero_hash.clone()), ChannelInfo {
+                               features: GlobalFeatures::new(),
+                               one_to_two: DirectionalChannelInfo {
+                                       src_node_id: node5.clone(),
+                                       last_update: 0,
+                                       enabled: true,
+                                       cltv_expiry_delta: (11 << 8) | 1,
+                                       htlc_minimum_msat: 0,
+                                       fee_base_msat: 0,
+                                       fee_proportional_millionths: 0,
+                                       last_update_message: None,
+                               }, two_to_one: DirectionalChannelInfo {
+                                       src_node_id: node4.clone(),
+                                       last_update: 0,
+                                       enabled: true,
+                                       cltv_expiry_delta: (11 << 8) | 2,
+                                       htlc_minimum_msat: 0,
+                                       fee_base_msat: 0,
+                                       fee_proportional_millionths: 0,
+                                       last_update_message: None,
+                               },
+                               announcement_message: None,
+                       });
+                       network.nodes.insert(node6.clone(), NodeInfo {
+                               channels: vec!(NetworkMap::get_key(7, zero_hash.clone())),
+                               lowest_inbound_channel_fee_base_msat: 0,
+                               lowest_inbound_channel_fee_proportional_millionths: 0,
+                               features: GlobalFeatures::new(),
+                               last_update: 1,
+                               rgb: [0; 3],
+                               alias: [0; 32],
+                               addresses: Vec::new(),
+                               announcement_message: None,
+                       });
+                       network.channels.insert(NetworkMap::get_key(7, zero_hash.clone()), ChannelInfo {
+                               features: GlobalFeatures::new(),
+                               one_to_two: DirectionalChannelInfo {
+                                       src_node_id: node3.clone(),
+                                       last_update: 0,
+                                       enabled: true,
+                                       cltv_expiry_delta: (7 << 8) | 1,
+                                       htlc_minimum_msat: 0,
+                                       fee_base_msat: 0,
+                                       fee_proportional_millionths: 1000000,
+                                       last_update_message: None,
+                               }, two_to_one: DirectionalChannelInfo {
+                                       src_node_id: node6.clone(),
+                                       last_update: 0,
+                                       enabled: true,
+                                       cltv_expiry_delta: (7 << 8) | 2,
+                                       htlc_minimum_msat: 0,
+                                       fee_base_msat: 0,
+                                       fee_proportional_millionths: 0,
+                                       last_update_message: None,
+                               },
+                               announcement_message: None,
+                       });
+               }
+
+               { // Simple route to 3 via 2
+                       let route = router.get_route(&node3, None, &Vec::new(), 100, 42).unwrap();
+                       assert_eq!(route.hops.len(), 2);
+
+                       assert_eq!(route.hops[0].pubkey, node2);
+                       assert_eq!(route.hops[0].short_channel_id, 2);
+                       assert_eq!(route.hops[0].fee_msat, 100);
+                       assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
+
+                       assert_eq!(route.hops[1].pubkey, node3);
+                       assert_eq!(route.hops[1].short_channel_id, 4);
+                       assert_eq!(route.hops[1].fee_msat, 100);
+                       assert_eq!(route.hops[1].cltv_expiry_delta, 42);
+               }
+
+               { // Route to 1 via 2 and 3 because our channel to 1 is disabled
+                       let route = router.get_route(&node1, None, &Vec::new(), 100, 42).unwrap();
+                       assert_eq!(route.hops.len(), 3);
+
+                       assert_eq!(route.hops[0].pubkey, node2);
+                       assert_eq!(route.hops[0].short_channel_id, 2);
+                       assert_eq!(route.hops[0].fee_msat, 200);
+                       assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
+
+                       assert_eq!(route.hops[1].pubkey, node3);
+                       assert_eq!(route.hops[1].short_channel_id, 4);
+                       assert_eq!(route.hops[1].fee_msat, 100);
+                       assert_eq!(route.hops[1].cltv_expiry_delta, (3 << 8) | 2);
+
+                       assert_eq!(route.hops[2].pubkey, node1);
+                       assert_eq!(route.hops[2].short_channel_id, 3);
+                       assert_eq!(route.hops[2].fee_msat, 100);
+                       assert_eq!(route.hops[2].cltv_expiry_delta, 42);
+               }
+
+               { // If we specify a channel to node8, that overrides our local channel view and that gets used
+                       let our_chans = vec![channelmanager::ChannelDetails {
+                               channel_id: [0; 32],
+                               short_channel_id: Some(42),
+                               remote_network_id: node8.clone(),
+                               channel_value_satoshis: 0,
+                               user_id: 0,
+                               outbound_capacity_msat: 0,
+                               inbound_capacity_msat: 0,
+                               is_live: true,
+                       }];
+                       let route = router.get_route(&node3, Some(&our_chans), &Vec::new(), 100, 42).unwrap();
+                       assert_eq!(route.hops.len(), 2);
+
+                       assert_eq!(route.hops[0].pubkey, node8);
+                       assert_eq!(route.hops[0].short_channel_id, 42);
+                       assert_eq!(route.hops[0].fee_msat, 200);
+                       assert_eq!(route.hops[0].cltv_expiry_delta, (13 << 8) | 1);
+
+                       assert_eq!(route.hops[1].pubkey, node3);
+                       assert_eq!(route.hops[1].short_channel_id, 13);
+                       assert_eq!(route.hops[1].fee_msat, 100);
+                       assert_eq!(route.hops[1].cltv_expiry_delta, 42);
+               }
+
+               let mut last_hops = vec!(RouteHint {
+                               src_node_id: node4.clone(),
+                               short_channel_id: 8,
+                               fee_base_msat: 0,
+                               fee_proportional_millionths: 0,
+                               cltv_expiry_delta: (8 << 8) | 1,
+                               htlc_minimum_msat: 0,
+                       }, RouteHint {
+                               src_node_id: node5.clone(),
+                               short_channel_id: 9,
+                               fee_base_msat: 1001,
+                               fee_proportional_millionths: 0,
+                               cltv_expiry_delta: (9 << 8) | 1,
+                               htlc_minimum_msat: 0,
+                       }, RouteHint {
+                               src_node_id: node6.clone(),
+                               short_channel_id: 10,
+                               fee_base_msat: 0,
+                               fee_proportional_millionths: 0,
+                               cltv_expiry_delta: (10 << 8) | 1,
+                               htlc_minimum_msat: 0,
+                       });
+
+               { // Simple test across 2, 3, 5, and 4 via a last_hop channel
+                       let route = router.get_route(&node7, None, &last_hops, 100, 42).unwrap();
+                       assert_eq!(route.hops.len(), 5);
+
+                       assert_eq!(route.hops[0].pubkey, node2);
+                       assert_eq!(route.hops[0].short_channel_id, 2);
+                       assert_eq!(route.hops[0].fee_msat, 100);
+                       assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
+
+                       assert_eq!(route.hops[1].pubkey, node3);
+                       assert_eq!(route.hops[1].short_channel_id, 4);
+                       assert_eq!(route.hops[1].fee_msat, 0);
+                       assert_eq!(route.hops[1].cltv_expiry_delta, (6 << 8) | 1);
+
+                       assert_eq!(route.hops[2].pubkey, node5);
+                       assert_eq!(route.hops[2].short_channel_id, 6);
+                       assert_eq!(route.hops[2].fee_msat, 0);
+                       assert_eq!(route.hops[2].cltv_expiry_delta, (11 << 8) | 1);
+
+                       assert_eq!(route.hops[3].pubkey, node4);
+                       assert_eq!(route.hops[3].short_channel_id, 11);
+                       assert_eq!(route.hops[3].fee_msat, 0);
+                       assert_eq!(route.hops[3].cltv_expiry_delta, (8 << 8) | 1);
+
+                       assert_eq!(route.hops[4].pubkey, node7);
+                       assert_eq!(route.hops[4].short_channel_id, 8);
+                       assert_eq!(route.hops[4].fee_msat, 100);
+                       assert_eq!(route.hops[4].cltv_expiry_delta, 42);
+               }
+
+               { // Simple test with outbound channel to 4 to test that last_hops and first_hops connect
+                       let our_chans = vec![channelmanager::ChannelDetails {
+                               channel_id: [0; 32],
+                               short_channel_id: Some(42),
+                               remote_network_id: node4.clone(),
+                               channel_value_satoshis: 0,
+                               user_id: 0,
+                               outbound_capacity_msat: 0,
+                               inbound_capacity_msat: 0,
+                               is_live: true,
+                       }];
+                       let route = router.get_route(&node7, Some(&our_chans), &last_hops, 100, 42).unwrap();
+                       assert_eq!(route.hops.len(), 2);
+
+                       assert_eq!(route.hops[0].pubkey, node4);
+                       assert_eq!(route.hops[0].short_channel_id, 42);
+                       assert_eq!(route.hops[0].fee_msat, 0);
+                       assert_eq!(route.hops[0].cltv_expiry_delta, (8 << 8) | 1);
+
+                       assert_eq!(route.hops[1].pubkey, node7);
+                       assert_eq!(route.hops[1].short_channel_id, 8);
+                       assert_eq!(route.hops[1].fee_msat, 100);
+                       assert_eq!(route.hops[1].cltv_expiry_delta, 42);
+               }
+
+               last_hops[0].fee_base_msat = 1000;
+
+               { // Revert to via 6 as the fee on 8 goes up
+                       let route = router.get_route(&node7, None, &last_hops, 100, 42).unwrap();
+                       assert_eq!(route.hops.len(), 4);
+
+                       assert_eq!(route.hops[0].pubkey, node2);
+                       assert_eq!(route.hops[0].short_channel_id, 2);
+                       assert_eq!(route.hops[0].fee_msat, 200); // fee increased as its % of value transferred across node
+                       assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
+
+                       assert_eq!(route.hops[1].pubkey, node3);
+                       assert_eq!(route.hops[1].short_channel_id, 4);
+                       assert_eq!(route.hops[1].fee_msat, 100);
+                       assert_eq!(route.hops[1].cltv_expiry_delta, (7 << 8) | 1);
+
+                       assert_eq!(route.hops[2].pubkey, node6);
+                       assert_eq!(route.hops[2].short_channel_id, 7);
+                       assert_eq!(route.hops[2].fee_msat, 0);
+                       assert_eq!(route.hops[2].cltv_expiry_delta, (10 << 8) | 1);
+
+                       assert_eq!(route.hops[3].pubkey, node7);
+                       assert_eq!(route.hops[3].short_channel_id, 10);
+                       assert_eq!(route.hops[3].fee_msat, 100);
+                       assert_eq!(route.hops[3].cltv_expiry_delta, 42);
+               }
+
+               { // ...but still use 8 for larger payments as 6 has a variable feerate
+                       let route = router.get_route(&node7, None, &last_hops, 2000, 42).unwrap();
+                       assert_eq!(route.hops.len(), 5);
+
+                       assert_eq!(route.hops[0].pubkey, node2);
+                       assert_eq!(route.hops[0].short_channel_id, 2);
+                       assert_eq!(route.hops[0].fee_msat, 3000);
+                       assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
+
+                       assert_eq!(route.hops[1].pubkey, node3);
+                       assert_eq!(route.hops[1].short_channel_id, 4);
+                       assert_eq!(route.hops[1].fee_msat, 0);
+                       assert_eq!(route.hops[1].cltv_expiry_delta, (6 << 8) | 1);
+
+                       assert_eq!(route.hops[2].pubkey, node5);
+                       assert_eq!(route.hops[2].short_channel_id, 6);
+                       assert_eq!(route.hops[2].fee_msat, 0);
+                       assert_eq!(route.hops[2].cltv_expiry_delta, (11 << 8) | 1);
+
+                       assert_eq!(route.hops[3].pubkey, node4);
+                       assert_eq!(route.hops[3].short_channel_id, 11);
+                       assert_eq!(route.hops[3].fee_msat, 1000);
+                       assert_eq!(route.hops[3].cltv_expiry_delta, (8 << 8) | 1);
+
+                       assert_eq!(route.hops[4].pubkey, node7);
+                       assert_eq!(route.hops[4].short_channel_id, 8);
+                       assert_eq!(route.hops[4].fee_msat, 2000);
+                       assert_eq!(route.hops[4].cltv_expiry_delta, 42);
+               }
+
+               { // Test Router serialization/deserialization
+                       let mut w = TestVecWriter(Vec::new());
+                       let network = router.network_map.read().unwrap();
+                       assert!(!network.channels.is_empty());
+                       assert!(!network.nodes.is_empty());
+                       network.write(&mut w).unwrap();
+                       assert!(<NetworkMap>::read(&mut ::std::io::Cursor::new(&w.0)).unwrap() == *network);
+               }
+       }
+}
diff --git a/lightning/src/util/byte_utils.rs b/lightning/src/util/byte_utils.rs
new file mode 100644 (file)
index 0000000..4444566
--- /dev/null
@@ -0,0 +1,106 @@
+#[inline]
+pub fn slice_to_be16(v: &[u8]) -> u16 {
+       ((v[0] as u16) << 8*1) |
+       ((v[1] as u16) << 8*0)
+}
+#[inline]
+pub fn slice_to_be32(v: &[u8]) -> u32 {
+       ((v[0] as u32) << 8*3) |
+       ((v[1] as u32) << 8*2) |
+       ((v[2] as u32) << 8*1) |
+       ((v[3] as u32) << 8*0)
+}
+#[cfg(not(feature = "fuzztarget"))] // Used only by poly1305
+#[inline]
+pub fn slice_to_le32(v: &[u8]) -> u32 {
+       ((v[0] as u32) << 8*0) |
+       ((v[1] as u32) << 8*1) |
+       ((v[2] as u32) << 8*2) |
+       ((v[3] as u32) << 8*3)
+}
+#[inline]
+pub fn slice_to_be48(v: &[u8]) -> u64 {
+       ((v[0] as u64) << 8*5) |
+       ((v[1] as u64) << 8*4) |
+       ((v[2] as u64) << 8*3) |
+       ((v[3] as u64) << 8*2) |
+       ((v[4] as u64) << 8*1) |
+       ((v[5] as u64) << 8*0)
+}
+#[inline]
+pub fn slice_to_be64(v: &[u8]) -> u64 {
+       ((v[0] as u64) << 8*7) |
+       ((v[1] as u64) << 8*6) |
+       ((v[2] as u64) << 8*5) |
+       ((v[3] as u64) << 8*4) |
+       ((v[4] as u64) << 8*3) |
+       ((v[5] as u64) << 8*2) |
+       ((v[6] as u64) << 8*1) |
+       ((v[7] as u64) << 8*0)
+}
+
+#[inline]
+pub fn be16_to_array(u: u16) -> [u8; 2] {
+       let mut v = [0; 2];
+       v[0] = ((u >> 8*1) & 0xff) as u8;
+       v[1] = ((u >> 8*0) & 0xff) as u8;
+       v
+}
+#[inline]
+pub fn be32_to_array(u: u32) -> [u8; 4] {
+       let mut v = [0; 4];
+       v[0] = ((u >> 8*3) & 0xff) as u8;
+       v[1] = ((u >> 8*2) & 0xff) as u8;
+       v[2] = ((u >> 8*1) & 0xff) as u8;
+       v[3] = ((u >> 8*0) & 0xff) as u8;
+       v
+}
+#[cfg(not(feature = "fuzztarget"))] // Used only by poly1305
+#[inline]
+pub fn le32_to_array(u: u32) -> [u8; 4] {
+       let mut v = [0; 4];
+       v[0] = ((u >> 8*0) & 0xff) as u8;
+       v[1] = ((u >> 8*1) & 0xff) as u8;
+       v[2] = ((u >> 8*2) & 0xff) as u8;
+       v[3] = ((u >> 8*3) & 0xff) as u8;
+       v
+}
+#[inline]
+pub fn be48_to_array(u: u64) -> [u8; 6] {
+       assert!(u & 0xffff_0000_0000_0000 == 0);
+       let mut v = [0; 6];
+       v[0] = ((u >> 8*5) & 0xff) as u8;
+       v[1] = ((u >> 8*4) & 0xff) as u8;
+       v[2] = ((u >> 8*3) & 0xff) as u8;
+       v[3] = ((u >> 8*2) & 0xff) as u8;
+       v[4] = ((u >> 8*1) & 0xff) as u8;
+       v[5] = ((u >> 8*0) & 0xff) as u8;
+       v
+}
+#[inline]
+pub fn be64_to_array(u: u64) -> [u8; 8] {
+       let mut v = [0; 8];
+       v[0] = ((u >> 8*7) & 0xff) as u8;
+       v[1] = ((u >> 8*6) & 0xff) as u8;
+       v[2] = ((u >> 8*5) & 0xff) as u8;
+       v[3] = ((u >> 8*4) & 0xff) as u8;
+       v[4] = ((u >> 8*3) & 0xff) as u8;
+       v[5] = ((u >> 8*2) & 0xff) as u8;
+       v[6] = ((u >> 8*1) & 0xff) as u8;
+       v[7] = ((u >> 8*0) & 0xff) as u8;
+       v
+}
+
+#[inline]
+pub fn le64_to_array(u: u64) -> [u8; 8] {
+       let mut v = [0; 8];
+       v[0] = ((u >> 8*0) & 0xff) as u8;
+       v[1] = ((u >> 8*1) & 0xff) as u8;
+       v[2] = ((u >> 8*2) & 0xff) as u8;
+       v[3] = ((u >> 8*3) & 0xff) as u8;
+       v[4] = ((u >> 8*4) & 0xff) as u8;
+       v[5] = ((u >> 8*5) & 0xff) as u8;
+       v[6] = ((u >> 8*6) & 0xff) as u8;
+       v[7] = ((u >> 8*7) & 0xff) as u8;
+       v
+}
diff --git a/lightning/src/util/chacha20.rs b/lightning/src/util/chacha20.rs
new file mode 100644 (file)
index 0000000..dd90b20
--- /dev/null
@@ -0,0 +1,550 @@
+// This file was stolen from rust-crypto.
+// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#[cfg(not(feature = "fuzztarget"))]
+mod real_chacha {
+       use std::cmp;
+       use util::byte_utils::{slice_to_le32, le32_to_array};
+
+       #[derive(Clone, Copy, PartialEq, Eq)]
+       #[allow(non_camel_case_types)]
+       struct u32x4(pub u32, pub u32, pub u32, pub u32);
+       impl ::std::ops::Add for u32x4 {
+               type Output = u32x4;
+               fn add(self, rhs: u32x4) -> u32x4 {
+                       u32x4(self.0.wrapping_add(rhs.0),
+                             self.1.wrapping_add(rhs.1),
+                             self.2.wrapping_add(rhs.2),
+                             self.3.wrapping_add(rhs.3))
+               }
+       }
+       impl ::std::ops::Sub for u32x4 {
+               type Output = u32x4;
+               fn sub(self, rhs: u32x4) -> u32x4 {
+                       u32x4(self.0.wrapping_sub(rhs.0),
+                             self.1.wrapping_sub(rhs.1),
+                             self.2.wrapping_sub(rhs.2),
+                             self.3.wrapping_sub(rhs.3))
+               }
+       }
+       impl ::std::ops::BitXor for u32x4 {
+               type Output = u32x4;
+               fn bitxor(self, rhs: u32x4) -> u32x4 {
+                       u32x4(self.0 ^ rhs.0, self.1 ^ rhs.1, self.2 ^ rhs.2, self.3 ^ rhs.3)
+               }
+       }
+       impl ::std::ops::Shr<u32x4> for u32x4 {
+               type Output = u32x4;
+               fn shr(self, rhs: u32x4) -> u32x4 {
+                       u32x4(self.0 >> rhs.0, self.1 >> rhs.1, self.2 >> rhs.2, self.3 >> rhs.3)
+               }
+       }
+       impl ::std::ops::Shl<u32x4> for u32x4 {
+               type Output = u32x4;
+               fn shl(self, rhs: u32x4) -> u32x4 {
+                       u32x4(self.0 << rhs.0, self.1 << rhs.1, self.2 << rhs.2, self.3 << rhs.3)
+               }
+       }
+
+       #[derive(Clone,Copy)]
+       struct ChaChaState {
+               a: u32x4,
+               b: u32x4,
+               c: u32x4,
+               d: u32x4
+       }
+
+       #[derive(Copy)]
+       pub struct ChaCha20 {
+               state  : ChaChaState,
+               output : [u8; 64],
+               offset : usize,
+       }
+
+       impl Clone for ChaCha20 { fn clone(&self) -> ChaCha20 { *self } }
+
+       macro_rules! swizzle {
+               ($b: expr, $c: expr, $d: expr) => {{
+                       let u32x4(b10, b11, b12, b13) = $b;
+                       $b = u32x4(b11, b12, b13, b10);
+                       let u32x4(c10, c11, c12, c13) = $c;
+                       $c = u32x4(c12, c13,c10, c11);
+                       let u32x4(d10, d11, d12, d13) = $d;
+                       $d = u32x4(d13, d10, d11, d12);
+               }}
+       }
+
+       macro_rules! state_to_buffer {
+               ($state: expr, $output: expr) => {{
+                       let u32x4(a1, a2, a3, a4) = $state.a;
+                       let u32x4(b1, b2, b3, b4) = $state.b;
+                       let u32x4(c1, c2, c3, c4) = $state.c;
+                       let u32x4(d1, d2, d3, d4) = $state.d;
+                       let lens = [
+                               a1,a2,a3,a4,
+                               b1,b2,b3,b4,
+                               c1,c2,c3,c4,
+                               d1,d2,d3,d4
+                       ];
+                       for i in 0..lens.len() {
+                               $output[i*4..(i+1)*4].copy_from_slice(&le32_to_array(lens[i]));
+                       }
+               }}
+       }
+
+       macro_rules! round{
+               ($state: expr) => {{
+                       $state.a = $state.a + $state.b;
+                       rotate!($state.d, $state.a, S16);
+                       $state.c = $state.c + $state.d;
+                       rotate!($state.b, $state.c, S12);
+                       $state.a = $state.a + $state.b;
+                       rotate!($state.d, $state.a, S8);
+                       $state.c = $state.c + $state.d;
+                       rotate!($state.b, $state.c, S7);
+               }}
+       }
+
+       macro_rules! rotate {
+               ($a: expr, $b: expr, $c:expr) => {{
+                       let v = $a ^ $b;
+                       let r = S32 - $c;
+                       let right = v >> r;
+                       $a = (v << $c) ^ right
+               }}
+       }
+
+       const S32:u32x4 = u32x4(32, 32, 32, 32);
+       const S16:u32x4 = u32x4(16, 16, 16, 16);
+       const S12:u32x4 = u32x4(12, 12, 12, 12);
+       const S8:u32x4 = u32x4(8, 8, 8, 8);
+       const S7:u32x4 = u32x4(7, 7, 7, 7);
+
+       impl ChaCha20 {
+               pub fn new(key: &[u8], nonce: &[u8]) -> ChaCha20 {
+                       assert!(key.len() == 16 || key.len() == 32);
+                       assert!(nonce.len() == 8 || nonce.len() == 12);
+
+                       ChaCha20{ state: ChaCha20::expand(key, nonce), output: [0u8; 64], offset: 64 }
+               }
+
+               fn expand(key: &[u8], nonce: &[u8]) -> ChaChaState {
+                       let constant = match key.len() {
+                               16 => b"expand 16-byte k",
+                               32 => b"expand 32-byte k",
+                               _  => unreachable!(),
+                       };
+                       ChaChaState {
+                               a: u32x4(
+                                       slice_to_le32(&constant[0..4]),
+                                       slice_to_le32(&constant[4..8]),
+                                       slice_to_le32(&constant[8..12]),
+                                       slice_to_le32(&constant[12..16])
+                               ),
+                               b: u32x4(
+                                       slice_to_le32(&key[0..4]),
+                                       slice_to_le32(&key[4..8]),
+                                       slice_to_le32(&key[8..12]),
+                                       slice_to_le32(&key[12..16])
+                               ),
+                               c: if key.len() == 16 {
+                                       u32x4(
+                                               slice_to_le32(&key[0..4]),
+                                               slice_to_le32(&key[4..8]),
+                                               slice_to_le32(&key[8..12]),
+                                               slice_to_le32(&key[12..16])
+                                       )
+                               } else {
+                                       u32x4(
+                                               slice_to_le32(&key[16..20]),
+                                               slice_to_le32(&key[20..24]),
+                                               slice_to_le32(&key[24..28]),
+                                               slice_to_le32(&key[28..32])
+                                       )
+                               },
+                               d: if nonce.len() == 16 {
+                                       u32x4(
+                                               slice_to_le32(&nonce[0..4]),
+                                               slice_to_le32(&nonce[4..8]),
+                                               slice_to_le32(&nonce[8..12]),
+                                               slice_to_le32(&nonce[12..16])
+                                       )
+                               } else if nonce.len() == 12 {
+                                       u32x4(
+                                               0,
+                                               slice_to_le32(&nonce[0..4]),
+                                               slice_to_le32(&nonce[4..8]),
+                                               slice_to_le32(&nonce[8..12])
+                                       )
+                               } else {
+                                       u32x4(
+                                               0,
+                                               0,
+                                               slice_to_le32(&nonce[0..4]),
+                                               slice_to_le32(&nonce[4..8])
+                                       )
+                               }
+                       }
+               }
+
+               // put the the next 64 keystream bytes into self.output
+               fn update(&mut self) {
+                       let mut state = self.state;
+
+                       for _ in 0..10 {
+                               round!(state);
+                               swizzle!(state.b, state.c, state.d);
+                               round!(state);
+                               swizzle!(state.d, state.c, state.b);
+                       }
+                       state.a = state.a + self.state.a;
+                       state.b = state.b + self.state.b;
+                       state.c = state.c + self.state.c;
+                       state.d = state.d + self.state.d;
+
+                       state_to_buffer!(state, self.output);
+
+                       self.state.d = self.state.d + u32x4(1, 0, 0, 0);
+                       let u32x4(c12, _, _, _) = self.state.d;
+                       if c12 == 0 {
+                               // we could increment the other counter word with an 8 byte nonce
+                               // but other implementations like boringssl have this same
+                               // limitation
+                               panic!("counter is exhausted");
+                       }
+
+                       self.offset = 0;
+               }
+
+               pub fn process(&mut self, input: &[u8], output: &mut [u8]) {
+                       assert!(input.len() == output.len());
+                       let len = input.len();
+                       let mut i = 0;
+                       while i < len {
+                               // If there is no keystream available in the output buffer,
+                               // generate the next block.
+                               if self.offset == 64 {
+                                       self.update();
+                               }
+
+                               // Process the min(available keystream, remaining input length).
+                               let count = cmp::min(64 - self.offset, len - i);
+                               // explicitly assert lengths to avoid bounds checks:
+                               assert!(output.len() >= i + count);
+                               assert!(input.len() >= i + count);
+                               assert!(self.output.len() >= self.offset + count);
+                               for j in 0..count {
+                                       output[i + j] = input[i + j] ^ self.output[self.offset + j];
+                               }
+                               i += count;
+                               self.offset += count;
+                       }
+               }
+       }
+}
+#[cfg(not(feature = "fuzztarget"))]
+pub use self::real_chacha::ChaCha20;
+
+#[cfg(feature = "fuzztarget")]
+mod fuzzy_chacha {
+       pub struct ChaCha20 {}
+
+       impl ChaCha20 {
+               pub fn new(key: &[u8], nonce: &[u8]) -> ChaCha20 {
+                       assert!(key.len() == 16 || key.len() == 32);
+                       assert!(nonce.len() == 8 || nonce.len() == 12);
+                       Self {}
+               }
+
+               pub fn process(&mut self, input: &[u8], output: &mut [u8]) {
+                       output.copy_from_slice(input);
+               }
+       }
+}
+#[cfg(feature = "fuzztarget")]
+pub use self::fuzzy_chacha::ChaCha20;
+
+#[cfg(test)]
+mod test {
+       use std::iter::repeat;
+
+       use super::ChaCha20;
+
+       #[test]
+       fn test_chacha20_256_tls_vectors() {
+               struct TestVector {
+                       key:   [u8; 32],
+                       nonce: [u8; 8],
+                       keystream: Vec<u8>,
+               };
+               // taken from http://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-04
+               let test_vectors = vec!(
+                       TestVector{
+                               key: [
+                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                               ],
+                               nonce: [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ],
+                               keystream: vec!(
+                                       0x76, 0xb8, 0xe0, 0xad, 0xa0, 0xf1, 0x3d, 0x90,
+                                       0x40, 0x5d, 0x6a, 0xe5, 0x53, 0x86, 0xbd, 0x28,
+                                       0xbd, 0xd2, 0x19, 0xb8, 0xa0, 0x8d, 0xed, 0x1a,
+                                       0xa8, 0x36, 0xef, 0xcc, 0x8b, 0x77, 0x0d, 0xc7,
+                                       0xda, 0x41, 0x59, 0x7c, 0x51, 0x57, 0x48, 0x8d,
+                                       0x77, 0x24, 0xe0, 0x3f, 0xb8, 0xd8, 0x4a, 0x37,
+                                       0x6a, 0x43, 0xb8, 0xf4, 0x15, 0x18, 0xa1, 0x1c,
+                                       0xc3, 0x87, 0xb6, 0x69, 0xb2, 0xee, 0x65, 0x86,
+                               ),
+                       }, TestVector{
+                               key: [
+                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
+                               ],
+                               nonce: [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ],
+                               keystream: vec!(
+                                       0x45, 0x40, 0xf0, 0x5a, 0x9f, 0x1f, 0xb2, 0x96,
+                                       0xd7, 0x73, 0x6e, 0x7b, 0x20, 0x8e, 0x3c, 0x96,
+                                       0xeb, 0x4f, 0xe1, 0x83, 0x46, 0x88, 0xd2, 0x60,
+                                       0x4f, 0x45, 0x09, 0x52, 0xed, 0x43, 0x2d, 0x41,
+                                       0xbb, 0xe2, 0xa0, 0xb6, 0xea, 0x75, 0x66, 0xd2,
+                                       0xa5, 0xd1, 0xe7, 0xe2, 0x0d, 0x42, 0xaf, 0x2c,
+                                       0x53, 0xd7, 0x92, 0xb1, 0xc4, 0x3f, 0xea, 0x81,
+                                       0x7e, 0x9a, 0xd2, 0x75, 0xae, 0x54, 0x69, 0x63,
+                               ),
+                       }, TestVector{
+                               key: [
+                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                               ],
+                               nonce: [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 ],
+                               keystream: vec!(
+                                       0xde, 0x9c, 0xba, 0x7b, 0xf3, 0xd6, 0x9e, 0xf5,
+                                       0xe7, 0x86, 0xdc, 0x63, 0x97, 0x3f, 0x65, 0x3a,
+                                       0x0b, 0x49, 0xe0, 0x15, 0xad, 0xbf, 0xf7, 0x13,
+                                       0x4f, 0xcb, 0x7d, 0xf1, 0x37, 0x82, 0x10, 0x31,
+                                       0xe8, 0x5a, 0x05, 0x02, 0x78, 0xa7, 0x08, 0x45,
+                                       0x27, 0x21, 0x4f, 0x73, 0xef, 0xc7, 0xfa, 0x5b,
+                                       0x52, 0x77, 0x06, 0x2e, 0xb7, 0xa0, 0x43, 0x3e,
+                                       0x44, 0x5f, 0x41, 0xe3,
+                               ),
+                       }, TestVector{
+                               key: [
+                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                               ],
+                               nonce: [ 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ],
+                               keystream: vec!(
+                                       0xef, 0x3f, 0xdf, 0xd6, 0xc6, 0x15, 0x78, 0xfb,
+                                       0xf5, 0xcf, 0x35, 0xbd, 0x3d, 0xd3, 0x3b, 0x80,
+                                       0x09, 0x63, 0x16, 0x34, 0xd2, 0x1e, 0x42, 0xac,
+                                       0x33, 0x96, 0x0b, 0xd1, 0x38, 0xe5, 0x0d, 0x32,
+                                       0x11, 0x1e, 0x4c, 0xaf, 0x23, 0x7e, 0xe5, 0x3c,
+                                       0xa8, 0xad, 0x64, 0x26, 0x19, 0x4a, 0x88, 0x54,
+                                       0x5d, 0xdc, 0x49, 0x7a, 0x0b, 0x46, 0x6e, 0x7d,
+                                       0x6b, 0xbd, 0xb0, 0x04, 0x1b, 0x2f, 0x58, 0x6b,
+                               ),
+                       }, TestVector{
+                               key: [
+                                       0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+                                       0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+                                       0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
+                                       0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
+                               ],
+                               nonce: [ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 ],
+                               keystream: vec!(
+                                       0xf7, 0x98, 0xa1, 0x89, 0xf1, 0x95, 0xe6, 0x69,
+                                       0x82, 0x10, 0x5f, 0xfb, 0x64, 0x0b, 0xb7, 0x75,
+                                       0x7f, 0x57, 0x9d, 0xa3, 0x16, 0x02, 0xfc, 0x93,
+                                       0xec, 0x01, 0xac, 0x56, 0xf8, 0x5a, 0xc3, 0xc1,
+                                       0x34, 0xa4, 0x54, 0x7b, 0x73, 0x3b, 0x46, 0x41,
+                                       0x30, 0x42, 0xc9, 0x44, 0x00, 0x49, 0x17, 0x69,
+                                       0x05, 0xd3, 0xbe, 0x59, 0xea, 0x1c, 0x53, 0xf1,
+                                       0x59, 0x16, 0x15, 0x5c, 0x2b, 0xe8, 0x24, 0x1a,
+                                       0x38, 0x00, 0x8b, 0x9a, 0x26, 0xbc, 0x35, 0x94,
+                                       0x1e, 0x24, 0x44, 0x17, 0x7c, 0x8a, 0xde, 0x66,
+                                       0x89, 0xde, 0x95, 0x26, 0x49, 0x86, 0xd9, 0x58,
+                                       0x89, 0xfb, 0x60, 0xe8, 0x46, 0x29, 0xc9, 0xbd,
+                                       0x9a, 0x5a, 0xcb, 0x1c, 0xc1, 0x18, 0xbe, 0x56,
+                                       0x3e, 0xb9, 0xb3, 0xa4, 0xa4, 0x72, 0xf8, 0x2e,
+                                       0x09, 0xa7, 0xe7, 0x78, 0x49, 0x2b, 0x56, 0x2e,
+                                       0xf7, 0x13, 0x0e, 0x88, 0xdf, 0xe0, 0x31, 0xc7,
+                                       0x9d, 0xb9, 0xd4, 0xf7, 0xc7, 0xa8, 0x99, 0x15,
+                                       0x1b, 0x9a, 0x47, 0x50, 0x32, 0xb6, 0x3f, 0xc3,
+                                       0x85, 0x24, 0x5f, 0xe0, 0x54, 0xe3, 0xdd, 0x5a,
+                                       0x97, 0xa5, 0xf5, 0x76, 0xfe, 0x06, 0x40, 0x25,
+                                       0xd3, 0xce, 0x04, 0x2c, 0x56, 0x6a, 0xb2, 0xc5,
+                                       0x07, 0xb1, 0x38, 0xdb, 0x85, 0x3e, 0x3d, 0x69,
+                                       0x59, 0x66, 0x09, 0x96, 0x54, 0x6c, 0xc9, 0xc4,
+                                       0xa6, 0xea, 0xfd, 0xc7, 0x77, 0xc0, 0x40, 0xd7,
+                                       0x0e, 0xaf, 0x46, 0xf7, 0x6d, 0xad, 0x39, 0x79,
+                                       0xe5, 0xc5, 0x36, 0x0c, 0x33, 0x17, 0x16, 0x6a,
+                                       0x1c, 0x89, 0x4c, 0x94, 0xa3, 0x71, 0x87, 0x6a,
+                                       0x94, 0xdf, 0x76, 0x28, 0xfe, 0x4e, 0xaa, 0xf2,
+                                       0xcc, 0xb2, 0x7d, 0x5a, 0xaa, 0xe0, 0xad, 0x7a,
+                                       0xd0, 0xf9, 0xd4, 0xb6, 0xad, 0x3b, 0x54, 0x09,
+                                       0x87, 0x46, 0xd4, 0x52, 0x4d, 0x38, 0x40, 0x7a,
+                                       0x6d, 0xeb, 0x3a, 0xb7, 0x8f, 0xab, 0x78, 0xc9,
+                               ),
+                       },
+               );
+
+               for tv in test_vectors.iter() {
+                       let mut c = ChaCha20::new(&tv.key, &tv.nonce);
+                       let input: Vec<u8> = repeat(0).take(tv.keystream.len()).collect();
+                       let mut output: Vec<u8> = repeat(0).take(input.len()).collect();
+                       c.process(&input[..], &mut output[..]);
+                       assert_eq!(output, tv.keystream);
+               }
+       }
+
+       #[test]
+       fn test_chacha20_256_tls_vectors_96_nonce() {
+               struct TestVector {
+                       key:   [u8; 32],
+                       nonce: [u8; 12],
+                       keystream: Vec<u8>,
+               };
+               // taken from http://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-04
+               let test_vectors = vec!(
+                       TestVector{
+                               key: [
+                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                               ],
+                               nonce: [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ],
+                               keystream: vec!(
+                                       0x76, 0xb8, 0xe0, 0xad, 0xa0, 0xf1, 0x3d, 0x90,
+                                       0x40, 0x5d, 0x6a, 0xe5, 0x53, 0x86, 0xbd, 0x28,
+                                       0xbd, 0xd2, 0x19, 0xb8, 0xa0, 0x8d, 0xed, 0x1a,
+                                       0xa8, 0x36, 0xef, 0xcc, 0x8b, 0x77, 0x0d, 0xc7,
+                                       0xda, 0x41, 0x59, 0x7c, 0x51, 0x57, 0x48, 0x8d,
+                                       0x77, 0x24, 0xe0, 0x3f, 0xb8, 0xd8, 0x4a, 0x37,
+                                       0x6a, 0x43, 0xb8, 0xf4, 0x15, 0x18, 0xa1, 0x1c,
+                                       0xc3, 0x87, 0xb6, 0x69, 0xb2, 0xee, 0x65, 0x86,
+                               ),
+                       }, TestVector{
+                               key: [
+                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
+                               ],
+                               nonce: [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ],
+                               keystream: vec!(
+                                       0x45, 0x40, 0xf0, 0x5a, 0x9f, 0x1f, 0xb2, 0x96,
+                                       0xd7, 0x73, 0x6e, 0x7b, 0x20, 0x8e, 0x3c, 0x96,
+                                       0xeb, 0x4f, 0xe1, 0x83, 0x46, 0x88, 0xd2, 0x60,
+                                       0x4f, 0x45, 0x09, 0x52, 0xed, 0x43, 0x2d, 0x41,
+                                       0xbb, 0xe2, 0xa0, 0xb6, 0xea, 0x75, 0x66, 0xd2,
+                                       0xa5, 0xd1, 0xe7, 0xe2, 0x0d, 0x42, 0xaf, 0x2c,
+                                       0x53, 0xd7, 0x92, 0xb1, 0xc4, 0x3f, 0xea, 0x81,
+                                       0x7e, 0x9a, 0xd2, 0x75, 0xae, 0x54, 0x69, 0x63,
+                               ),
+                       }, TestVector{
+                               key: [
+                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                               ],
+                               nonce: [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 ],
+                               keystream: vec!(
+                                       0xde, 0x9c, 0xba, 0x7b, 0xf3, 0xd6, 0x9e, 0xf5,
+                                       0xe7, 0x86, 0xdc, 0x63, 0x97, 0x3f, 0x65, 0x3a,
+                                       0x0b, 0x49, 0xe0, 0x15, 0xad, 0xbf, 0xf7, 0x13,
+                                       0x4f, 0xcb, 0x7d, 0xf1, 0x37, 0x82, 0x10, 0x31,
+                                       0xe8, 0x5a, 0x05, 0x02, 0x78, 0xa7, 0x08, 0x45,
+                                       0x27, 0x21, 0x4f, 0x73, 0xef, 0xc7, 0xfa, 0x5b,
+                                       0x52, 0x77, 0x06, 0x2e, 0xb7, 0xa0, 0x43, 0x3e,
+                                       0x44, 0x5f, 0x41, 0xe3,
+                               ),
+                       }, TestVector{
+                               key: [
+                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                               ],
+                               nonce: [ 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ],
+                               keystream: vec!(
+                                       0xef, 0x3f, 0xdf, 0xd6, 0xc6, 0x15, 0x78, 0xfb,
+                                       0xf5, 0xcf, 0x35, 0xbd, 0x3d, 0xd3, 0x3b, 0x80,
+                                       0x09, 0x63, 0x16, 0x34, 0xd2, 0x1e, 0x42, 0xac,
+                                       0x33, 0x96, 0x0b, 0xd1, 0x38, 0xe5, 0x0d, 0x32,
+                                       0x11, 0x1e, 0x4c, 0xaf, 0x23, 0x7e, 0xe5, 0x3c,
+                                       0xa8, 0xad, 0x64, 0x26, 0x19, 0x4a, 0x88, 0x54,
+                                       0x5d, 0xdc, 0x49, 0x7a, 0x0b, 0x46, 0x6e, 0x7d,
+                                       0x6b, 0xbd, 0xb0, 0x04, 0x1b, 0x2f, 0x58, 0x6b,
+                               ),
+                       }, TestVector{
+                               key: [
+                                       0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+                                       0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+                                       0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
+                                       0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
+                               ],
+                               nonce: [0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 ],
+                               keystream: vec!(
+                                       0xf7, 0x98, 0xa1, 0x89, 0xf1, 0x95, 0xe6, 0x69,
+                                       0x82, 0x10, 0x5f, 0xfb, 0x64, 0x0b, 0xb7, 0x75,
+                                       0x7f, 0x57, 0x9d, 0xa3, 0x16, 0x02, 0xfc, 0x93,
+                                       0xec, 0x01, 0xac, 0x56, 0xf8, 0x5a, 0xc3, 0xc1,
+                                       0x34, 0xa4, 0x54, 0x7b, 0x73, 0x3b, 0x46, 0x41,
+                                       0x30, 0x42, 0xc9, 0x44, 0x00, 0x49, 0x17, 0x69,
+                                       0x05, 0xd3, 0xbe, 0x59, 0xea, 0x1c, 0x53, 0xf1,
+                                       0x59, 0x16, 0x15, 0x5c, 0x2b, 0xe8, 0x24, 0x1a,
+                                       0x38, 0x00, 0x8b, 0x9a, 0x26, 0xbc, 0x35, 0x94,
+                                       0x1e, 0x24, 0x44, 0x17, 0x7c, 0x8a, 0xde, 0x66,
+                                       0x89, 0xde, 0x95, 0x26, 0x49, 0x86, 0xd9, 0x58,
+                                       0x89, 0xfb, 0x60, 0xe8, 0x46, 0x29, 0xc9, 0xbd,
+                                       0x9a, 0x5a, 0xcb, 0x1c, 0xc1, 0x18, 0xbe, 0x56,
+                                       0x3e, 0xb9, 0xb3, 0xa4, 0xa4, 0x72, 0xf8, 0x2e,
+                                       0x09, 0xa7, 0xe7, 0x78, 0x49, 0x2b, 0x56, 0x2e,
+                                       0xf7, 0x13, 0x0e, 0x88, 0xdf, 0xe0, 0x31, 0xc7,
+                                       0x9d, 0xb9, 0xd4, 0xf7, 0xc7, 0xa8, 0x99, 0x15,
+                                       0x1b, 0x9a, 0x47, 0x50, 0x32, 0xb6, 0x3f, 0xc3,
+                                       0x85, 0x24, 0x5f, 0xe0, 0x54, 0xe3, 0xdd, 0x5a,
+                                       0x97, 0xa5, 0xf5, 0x76, 0xfe, 0x06, 0x40, 0x25,
+                                       0xd3, 0xce, 0x04, 0x2c, 0x56, 0x6a, 0xb2, 0xc5,
+                                       0x07, 0xb1, 0x38, 0xdb, 0x85, 0x3e, 0x3d, 0x69,
+                                       0x59, 0x66, 0x09, 0x96, 0x54, 0x6c, 0xc9, 0xc4,
+                                       0xa6, 0xea, 0xfd, 0xc7, 0x77, 0xc0, 0x40, 0xd7,
+                                       0x0e, 0xaf, 0x46, 0xf7, 0x6d, 0xad, 0x39, 0x79,
+                                       0xe5, 0xc5, 0x36, 0x0c, 0x33, 0x17, 0x16, 0x6a,
+                                       0x1c, 0x89, 0x4c, 0x94, 0xa3, 0x71, 0x87, 0x6a,
+                                       0x94, 0xdf, 0x76, 0x28, 0xfe, 0x4e, 0xaa, 0xf2,
+                                       0xcc, 0xb2, 0x7d, 0x5a, 0xaa, 0xe0, 0xad, 0x7a,
+                                       0xd0, 0xf9, 0xd4, 0xb6, 0xad, 0x3b, 0x54, 0x09,
+                                       0x87, 0x46, 0xd4, 0x52, 0x4d, 0x38, 0x40, 0x7a,
+                                       0x6d, 0xeb, 0x3a, 0xb7, 0x8f, 0xab, 0x78, 0xc9,
+                               ),
+                       },
+               );
+
+               for tv in test_vectors.iter() {
+                       let mut c = ChaCha20::new(&tv.key, &tv.nonce);
+                       let input: Vec<u8> = repeat(0).take(tv.keystream.len()).collect();
+                       let mut output: Vec<u8> = repeat(0).take(input.len()).collect();
+                       c.process(&input[..], &mut output[..]);
+                       assert_eq!(output, tv.keystream);
+               }
+       }
+}
diff --git a/lightning/src/util/chacha20poly1305rfc.rs b/lightning/src/util/chacha20poly1305rfc.rs
new file mode 100644 (file)
index 0000000..1d3af1e
--- /dev/null
@@ -0,0 +1,147 @@
+// ring has a garbage API so its use is avoided, but rust-crypto doesn't have RFC-variant poly1305
+// Instead, we steal rust-crypto's implementation and tweak it to match the RFC.
+
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// This is a port of Andrew Moons poly1305-donna
+// https://github.com/floodyberry/poly1305-donna
+
+#[cfg(not(feature = "fuzztarget"))]
+mod real_chachapoly {
+       use util::chacha20::ChaCha20;
+       use util::poly1305::Poly1305;
+       use bitcoin_hashes::cmp::fixed_time_eq;
+
+       use util::byte_utils;
+
+       #[derive(Clone, Copy)]
+       pub struct ChaCha20Poly1305RFC {
+               cipher: ChaCha20,
+               mac: Poly1305,
+               finished: bool,
+               data_len: usize,
+               aad_len: u64,
+       }
+
+       impl ChaCha20Poly1305RFC {
+               #[inline]
+               fn pad_mac_16(mac: &mut Poly1305, len: usize) {
+                       if len % 16 != 0 {
+                               mac.input(&[0; 16][0..16 - (len % 16)]);
+                       }
+               }
+               pub fn new(key: &[u8], nonce: &[u8], aad: &[u8]) -> ChaCha20Poly1305RFC {
+                       assert!(key.len() == 16 || key.len() == 32);
+                       assert!(nonce.len() == 12);
+
+                       // Ehh, I'm too lazy to *also* tweak ChaCha20 to make it RFC-compliant
+                       assert!(nonce[0] == 0 && nonce[1] == 0 && nonce[2] == 0 && nonce[3] == 0);
+
+                       let mut cipher = ChaCha20::new(key, &nonce[4..]);
+                       let mut mac_key = [0u8; 64];
+                       let zero_key = [0u8; 64];
+                       cipher.process(&zero_key, &mut mac_key);
+
+                       let mut mac = Poly1305::new(&mac_key[..32]);
+                       mac.input(aad);
+                       ChaCha20Poly1305RFC::pad_mac_16(&mut mac, aad.len());
+
+                       ChaCha20Poly1305RFC {
+                               cipher: cipher,
+                               mac: mac,
+                               finished: false,
+                               data_len: 0,
+                               aad_len: aad.len() as u64,
+                       }
+               }
+
+               pub fn encrypt(&mut self, input: &[u8], output: &mut [u8], out_tag: &mut [u8]) {
+                       assert!(input.len() == output.len());
+                       assert!(self.finished == false);
+                       self.cipher.process(input, output);
+                       self.data_len += input.len();
+                       self.mac.input(output);
+                       ChaCha20Poly1305RFC::pad_mac_16(&mut self.mac, self.data_len);
+                       self.finished = true;
+                       self.mac.input(&byte_utils::le64_to_array(self.aad_len));
+                       self.mac.input(&byte_utils::le64_to_array(self.data_len as u64));
+                       self.mac.raw_result(out_tag);
+               }
+
+               pub fn decrypt(&mut self, input: &[u8], output: &mut [u8], tag: &[u8]) -> bool {
+                       assert!(input.len() == output.len());
+                       assert!(self.finished == false);
+
+                       self.finished = true;
+
+                       self.mac.input(input);
+
+                       self.data_len += input.len();
+                       ChaCha20Poly1305RFC::pad_mac_16(&mut self.mac, self.data_len);
+                       self.mac.input(&byte_utils::le64_to_array(self.aad_len));
+                       self.mac.input(&byte_utils::le64_to_array(self.data_len as u64));
+
+                       let mut calc_tag =  [0u8; 16];
+                       self.mac.raw_result(&mut calc_tag);
+                       if fixed_time_eq(&calc_tag, tag) {
+                               self.cipher.process(input, output);
+                               true
+                       } else {
+                               false
+                       }
+               }
+       }
+}
+#[cfg(not(feature = "fuzztarget"))]
+pub use self::real_chachapoly::ChaCha20Poly1305RFC;
+
+#[cfg(feature = "fuzztarget")]
+mod fuzzy_chachapoly {
+       #[derive(Clone, Copy)]
+       pub struct ChaCha20Poly1305RFC {
+               tag: [u8; 16],
+               finished: bool,
+       }
+       impl ChaCha20Poly1305RFC {
+               pub fn new(key: &[u8], nonce: &[u8], _aad: &[u8]) -> ChaCha20Poly1305RFC {
+                       assert!(key.len() == 16 || key.len() == 32);
+                       assert!(nonce.len() == 12);
+
+                       // Ehh, I'm too lazy to *also* tweak ChaCha20 to make it RFC-compliant
+                       assert!(nonce[0] == 0 && nonce[1] == 0 && nonce[2] == 0 && nonce[3] == 0);
+
+                       let mut tag = [0; 16];
+                       tag.copy_from_slice(&key[0..16]);
+
+                       ChaCha20Poly1305RFC {
+                               tag,
+                               finished: false,
+                       }
+               }
+
+               pub fn encrypt(&mut self, input: &[u8], output: &mut [u8], out_tag: &mut [u8]) {
+                       assert!(input.len() == output.len());
+                       assert!(self.finished == false);
+
+                       output.copy_from_slice(&input);
+                       out_tag.copy_from_slice(&self.tag);
+                       self.finished = true;
+               }
+
+               pub fn decrypt(&mut self, input: &[u8], output: &mut [u8], tag: &[u8]) -> bool {
+                       assert!(input.len() == output.len());
+                       assert!(self.finished == false);
+
+                       if tag[..] != self.tag[..] { return false; }
+                       output.copy_from_slice(input);
+                       self.finished = true;
+                       true
+               }
+       }
+}
+#[cfg(feature = "fuzztarget")]
+pub use self::fuzzy_chachapoly::ChaCha20Poly1305RFC;
diff --git a/lightning/src/util/config.rs b/lightning/src/util/config.rs
new file mode 100644 (file)
index 0000000..0b61a56
--- /dev/null
@@ -0,0 +1,183 @@
+//! Various user-configurable channel limits and settings which ChannelManager
+//! applies for you.
+
+use ln::channelmanager::{BREAKDOWN_TIMEOUT, MAX_LOCAL_BREAKDOWN_TIMEOUT};
+
+/// Top-level config which holds ChannelHandshakeLimits and ChannelConfig.
+#[derive(Clone, Debug)]
+pub struct UserConfig {
+       /// Channel config that we propose to our counterparty.
+       pub own_channel_config: ChannelHandshakeConfig,
+       /// Limits applied to our counterparty's proposed channel config settings.
+       pub peer_channel_config_limits: ChannelHandshakeLimits,
+       /// Channel config which affects behavior during channel lifetime.
+       pub channel_options: ChannelConfig,
+}
+
+impl UserConfig {
+       /// Provides sane defaults for most configurations (but with 0 relay fees!)
+       pub fn new() -> Self{
+               UserConfig {
+                       own_channel_config: ChannelHandshakeConfig::new(),
+                       peer_channel_config_limits: ChannelHandshakeLimits::new(),
+                       channel_options: ChannelConfig::new(),
+               }
+       }
+}
+
+/// Configuration we set when applicable.
+#[derive(Clone, Debug)]
+pub struct ChannelHandshakeConfig {
+       /// Confirmations we will wait for before considering the channel locked in.
+       /// Applied only for inbound channels (see ChannelHandshakeLimits::max_minimum_depth for the
+       /// equivalent limit applied to outbound channels).
+       pub minimum_depth: u32,
+       /// Set to the amount of time we require our counterparty to wait to claim their money.
+       ///
+       /// It's one of the main parameter of our security model. We (or one of our watchtowers) MUST
+       /// be online to check for peer having broadcast a revoked transaction to steal our funds
+       /// at least once every our_to_self_delay blocks.
+       /// Default is BREAKDOWN_TIMEOUT, we enforce it as a minimum at channel opening so you can
+       /// tweak config to ask for more security, not less.
+       ///
+       /// Meanwhile, asking for a too high delay, we bother peer to freeze funds for nothing in
+       /// case of an honest unilateral channel close, which implicitly decrease the economic value of
+       /// our channel.
+       pub our_to_self_delay: u16,
+}
+
+impl ChannelHandshakeConfig {
+       /// Provides sane defaults for `ChannelHandshakeConfig`
+       pub fn new() -> ChannelHandshakeConfig {
+               ChannelHandshakeConfig {
+                       minimum_depth: 6,
+                       our_to_self_delay: BREAKDOWN_TIMEOUT,
+               }
+       }
+}
+
+/// Optional channel limits which are applied during channel creation.
+///
+/// These limits are only applied to our counterparty's limits, not our own.
+///
+/// Use 0/<type>::max_value() as appropriate to skip checking.
+#[derive(Copy, Clone, Debug)]
+pub struct ChannelHandshakeLimits {
+       /// Minimum allowed satoshis when a channel is funded, this is supplied by the sender and so
+       /// only applies to inbound channels.
+       pub min_funding_satoshis: u64,
+       /// The remote node sets a limit on the minimum size of HTLCs we can send to them. This allows
+       /// you to limit the maximum minimum-size they can require.
+       pub max_htlc_minimum_msat: u64,
+       /// The remote node sets a limit on the maximum value of pending HTLCs to them at any given
+       /// time to limit their funds exposure to HTLCs. This allows you to set a minimum such value.
+       pub min_max_htlc_value_in_flight_msat: u64,
+       /// The remote node will require we keep a certain amount in direct payment to ourselves at all
+       /// time, ensuring that we are able to be punished if we broadcast an old state. This allows to
+       /// you limit the amount which we will have to keep to ourselves (and cannot use for HTLCs).
+       pub max_channel_reserve_satoshis: u64,
+       /// The remote node sets a limit on the maximum number of pending HTLCs to them at any given
+       /// time. This allows you to set a minimum such value.
+       pub min_max_accepted_htlcs: u16,
+       /// Outputs below a certain value will not be added to on-chain transactions. The dust value is
+       /// required to always be higher than this value so this only applies to HTLC outputs (and
+       /// potentially to-self outputs before any payments have been made).
+       /// Thus, HTLCs below this amount plus HTLC transaction fees are not enforceable on-chain.
+       /// This setting allows you to set a minimum dust limit for their commitment transactions,
+       /// reflecting the reality that tiny outputs are not considered standard transactions and will
+       /// not propagate through the Bitcoin network.
+       /// Defaults to 546, or the current dust limit on the Bitcoin network.
+       pub min_dust_limit_satoshis: u64,
+       /// Maximum allowed threshold above which outputs will not be generated in their commitment
+       /// transactions.
+       /// HTLCs below this amount plus HTLC transaction fees are not enforceable on-chain.
+       pub max_dust_limit_satoshis: u64,
+       /// Before a channel is usable the funding transaction will need to be confirmed by at least a
+       /// certain number of blocks, specified by the node which is not the funder (as the funder can
+       /// assume they aren't going to double-spend themselves).
+       /// This config allows you to set a limit on the maximum amount of time to wait. Defaults to
+       /// 144 blocks or roughly one day and only applies to outbound channels.
+       pub max_minimum_depth: u32,
+       /// Set to force the incoming channel to match our announced channel preference in
+       /// ChannelConfig.
+       /// Defaults to true to make the default that no announced channels are possible (which is
+       /// appropriate for any nodes which are not online very reliably).
+       pub force_announced_channel_preference: bool,
+       /// Set to the amount of time we're willing to wait to claim money back to us.
+       ///
+       /// Not checking this value would be a security issue, as our peer would be able to set it to
+       /// max relative lock-time (a year) and we would "lose" money as it would be locked for a long time.
+       /// Default is MAX_LOCAL_BREAKDOWN_TIMEOUT, which we also enforce as a maximum value
+       /// so you can tweak config to reduce the loss of having useless locked funds (if your peer accepts)
+       pub their_to_self_delay: u16
+}
+
+impl ChannelHandshakeLimits {
+       /// Provides sane defaults for most configurations.
+       ///
+       /// Most additional limits are disabled except those with which specify a default in individual
+       /// field documentation. Note that this may result in barely-usable channels, but since they
+       /// are applied mostly only to incoming channels that's not much of a problem.
+       pub fn new() -> Self {
+               ChannelHandshakeLimits {
+                       min_funding_satoshis: 0,
+                       max_htlc_minimum_msat: <u64>::max_value(),
+                       min_max_htlc_value_in_flight_msat: 0,
+                       max_channel_reserve_satoshis: <u64>::max_value(),
+                       min_max_accepted_htlcs: 0,
+                       min_dust_limit_satoshis: 546,
+                       max_dust_limit_satoshis: <u64>::max_value(),
+                       max_minimum_depth: 144,
+                       force_announced_channel_preference: true,
+                       their_to_self_delay: MAX_LOCAL_BREAKDOWN_TIMEOUT,
+               }
+       }
+}
+
+/// Options which apply on a per-channel basis and may change at runtime or based on negotiation
+/// with our counterparty.
+#[derive(Copy, Clone, Debug)]
+pub struct ChannelConfig {
+       /// Amount (in millionths of a satoshi) the channel will charge per transferred satoshi.
+       /// This may be allowed to change at runtime in a later update, however doing so must result in
+       /// update messages sent to notify all nodes of our updated relay fee.
+       pub fee_proportional_millionths: u32,
+       /// Set to announce the channel publicly and notify all nodes that they can route via this
+       /// channel.
+       ///
+       /// This should only be set to true for nodes which expect to be online reliably.
+       ///
+       /// As the node which funds a channel picks this value this will only apply for new outbound
+       /// channels unless ChannelHandshakeLimits::force_announced_channel_preferences is set.
+       ///
+       /// This cannot be changed after the initial channel handshake.
+       pub announced_channel: bool,
+       /// When set, we commit to an upfront shutdown_pubkey at channel open. If our counterparty
+       /// supports it, they will then enforce the mutual-close output to us matches what we provided
+       /// at intialization, preventing us from closing to an alternate pubkey.
+       ///
+       /// This is set to true by default to provide a slight increase in security, though ultimately
+       /// any attacker who is able to take control of a channel can just as easily send the funds via
+       /// lightning payments, so we never require that our counterparties support this option.
+       ///
+       /// This cannot be changed after a channel has been initialized.
+       pub commit_upfront_shutdown_pubkey: bool
+}
+
+impl ChannelConfig {
+       /// Provides sane defaults for most configurations (but with zero relay fees!).
+       pub fn new() -> Self {
+               ChannelConfig {
+                       fee_proportional_millionths: 0,
+                       announced_channel: false,
+                       commit_upfront_shutdown_pubkey: true,
+               }
+       }
+}
+
+//Add write and readable traits to channelconfig
+impl_writeable!(ChannelConfig, 8+1+1, {
+       fee_proportional_millionths,
+       announced_channel,
+       commit_upfront_shutdown_pubkey
+});
diff --git a/lightning/src/util/errors.rs b/lightning/src/util/errors.rs
new file mode 100644 (file)
index 0000000..27f8377
--- /dev/null
@@ -0,0 +1,96 @@
+//! Error types live here.
+
+use std::fmt;
+
+/// Indicates an error on the client's part (usually some variant of attempting to use too-low or
+/// too-high values)
+pub enum APIError {
+       /// Indicates the API was wholly misused (see err for more). Cases where these can be returned
+       /// are documented, but generally indicates some precondition of a function was violated.
+       APIMisuseError {
+               /// A human-readable error message
+               err: &'static str
+       },
+       /// Due to a high feerate, we were unable to complete the request.
+       /// For example, this may be returned if the feerate implies we cannot open a channel at the
+       /// requested value, but opening a larger channel would succeed.
+       FeeRateTooHigh {
+               /// A human-readable error message
+               err: String,
+               /// The feerate which was too high.
+               feerate: u64
+       },
+       /// A malformed Route was provided (eg overflowed value, node id mismatch, overly-looped route,
+       /// too-many-hops, etc).
+       RouteError {
+               /// A human-readable error message
+               err: &'static str
+       },
+       /// We were unable to complete the request as the Channel required to do so is unable to
+       /// complete the request (or was not found). This can take many forms, including disconnected
+       /// peer, channel at capacity, channel shutting down, etc.
+       ChannelUnavailable {
+               /// A human-readable error message
+               err: &'static str
+       },
+       /// An attempt to call add_update_monitor returned an Err (ie you did this!), causing the
+       /// attempted action to fail.
+       MonitorUpdateFailed,
+}
+
+impl fmt::Debug for APIError {
+       fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+               match *self {
+                       APIError::APIMisuseError {ref err} => f.write_str(err),
+                       APIError::FeeRateTooHigh {ref err, ref feerate} => write!(f, "{} feerate: {}", err, feerate),
+                       APIError::RouteError {ref err} => f.write_str(err),
+                       APIError::ChannelUnavailable {ref err} => f.write_str(err),
+                       APIError::MonitorUpdateFailed => f.write_str("Client indicated a channel monitor update failed"),
+               }
+       }
+}
+
+#[inline]
+pub(crate) fn get_onion_debug_field(error_code: u16) -> (&'static str, usize) {
+       match error_code & 0xff {
+               4|5|6 => ("sha256_of_onion", 32),
+               11|12 => ("htlc_msat", 8),
+               13|18 => ("cltv_expiry", 4),
+               19 => ("incoming_htlc_msat", 8),
+               20 => ("flags", 2),
+               _ => ("", 0),
+       }
+}
+
+#[inline]
+pub(crate) fn get_onion_error_description(error_code: u16) -> (&'static str, &'static str) {
+       const BADONION: u16 = 0x8000;
+       const PERM: u16 = 0x4000;
+       const NODE: u16 = 0x2000;
+       const UPDATE: u16 = 0x1000;
+       match error_code {
+               _c if _c == PERM|1 => ("The realm byte was not understood by the processing node", "invalid_realm"),
+               _c if _c == NODE|2 => ("Node indicated temporary node failure", "temporary_node_failure"),
+               _c if _c == PERM|NODE|2 => ("Node indicated permanent node failure", "permanent_node_failure"),
+               _c if _c == PERM|NODE|3 => ("Node indicated the required node feature is missing in the onion", "required_node_feature_missing"),
+               _c if _c == BADONION|PERM|4 => ("Node indicated the version by is not understood", "invalid_onion_version"),
+               _c if _c == BADONION|PERM|5  => ("Node indicated the HMAC of the onion is incorrect", "invalid_onion_hmac"),
+               _c if _c == BADONION|PERM|6 => ("Node indicated the ephemeral public keys is not parseable", "invalid_onion_key"),
+               _c if _c == UPDATE|7 => ("Node indicated the outgoing channel is unable to handle the HTLC temporarily", "temporary_channel_failure"),
+               _c if _c == PERM|8 => ("Node indicated the outgoing channel is unable to handle the HTLC peramanently", "permanent_channel_failure"),
+               _c if _c == PERM|9 => ("Node indicated the required feature for the outgoing channel is not satisfied", "required_channel_feature_missing"),
+               _c if _c == PERM|10 => ("Node indicated the outbound channel is not found for the specified short_channel_id in the onion packet", "unknown_next_peer"),
+               _c if _c == UPDATE|11 => ("Node indicated the HTLC amount was below the required minmum for the outbound channel", "amount_below_minimum"),
+               _c if _c == UPDATE|12 => ("Node indicated the fee amount does not meet the required level", "fee_insufficient"),
+               _c if _c == UPDATE|13 => ("Node indicated the cltv_expiry does not comply with the cltv_expiry_delta required by the outgoing channel", "incorrect_cltv_expiry"),
+               _c if _c == UPDATE|14 => ("Node indicated the CLTV expiry too close to the current block height for safe handling", "expiry_too_soon"),
+               _c if _c == PERM|15 => ("The final node indicated the payment hash is unknown or amount is incorrect", "incorrect_or_unknown_payment_details"),
+               _c if _c == PERM|16 => ("The final node indicated the payment amount is incorrect", "incorrect_payment_amount"),
+               _c if _c == 17 => ("The final node indicated the CLTV expiry is too close to the current block height for safe handling", "final_expiry_too_soon"),
+               _c if _c == 18 => ("The final node indicated the CLTV expiry in the HTLC does not match the value in the onion", "final_incorrect_cltv_expiry"),
+               _c if _c == 19 => ("The final node indicated the amount in the HTLC does not match the value in the onion", "final_incorrect_htlc_amount"),
+               _c if _c == UPDATE|20 => ("Node indicated the outbound channel has been disabled", "channel_disabled"),
+               _c if _c == 21 => ("Node indicated the CLTV expiry in the HTLC is too far in the future", "expiry_too_far"),
+               _ => ("Unknown", ""),
+       }
+}
diff --git a/lightning/src/util/events.rs b/lightning/src/util/events.rs
new file mode 100644 (file)
index 0000000..96a5b48
--- /dev/null
@@ -0,0 +1,235 @@
+//! Events are returned from various bits in the library which indicate some action must be taken
+//! by the client.
+//!
+//! Because we don't have a built-in runtime, it's up to the client to call events at a time in the
+//! future, as well as generate and broadcast funding transactions handle payment preimages and a
+//! few other things.
+//!
+//! Note that many events are handled for you by PeerHandler, so in the common design of having a
+//! PeerManager which marshalls messages to ChannelManager and Router you only need to call
+//! process_events on the PeerHandler and then get_and_clear_pending_events and handle the events
+//! that bubble up to the surface. If, however, you do not have a PeerHandler managing a
+//! ChannelManager you need to handle all of the events which may be generated.
+//TODO: We need better separation of event types ^
+
+use ln::msgs;
+use ln::channelmanager::{PaymentPreimage, PaymentHash};
+use chain::transaction::OutPoint;
+use chain::keysinterface::SpendableOutputDescriptor;
+
+use bitcoin::blockdata::script::Script;
+
+use secp256k1::key::PublicKey;
+
+use std::time::Duration;
+
+/// An Event which you should probably take some action in response to.
+pub enum Event {
+       /// Used to indicate that the client should generate a funding transaction with the given
+       /// parameters and then call ChannelManager::funding_transaction_generated.
+       /// Generated in ChannelManager message handling.
+       /// Note that *all inputs* in the funding transaction must spend SegWit outputs or your
+       /// counterparty can steal your funds!
+       FundingGenerationReady {
+               /// The random channel_id we picked which you'll need to pass into
+               /// ChannelManager::funding_transaction_generated.
+               temporary_channel_id: [u8; 32],
+               /// The value, in satoshis, that the output should have.
+               channel_value_satoshis: u64,
+               /// The script which should be used in the transaction output.
+               output_script: Script,
+               /// The value passed in to ChannelManager::create_channel
+               user_channel_id: u64,
+       },
+       /// Used to indicate that the client may now broadcast the funding transaction it created for a
+       /// channel. Broadcasting such a transaction prior to this event may lead to our counterparty
+       /// trivially stealing all funds in the funding transaction!
+       FundingBroadcastSafe {
+               /// The output, which was passed to ChannelManager::funding_transaction_generated, which is
+               /// now safe to broadcast.
+               funding_txo: OutPoint,
+               /// The value passed in to ChannelManager::create_channel
+               user_channel_id: u64,
+       },
+       /// Indicates we've received money! Just gotta dig out that payment preimage and feed it to
+       /// ChannelManager::claim_funds to get it....
+       /// Note that if the preimage is not known or the amount paid is incorrect, you must call
+       /// ChannelManager::fail_htlc_backwards to free up resources for this HTLC.
+       /// The amount paid should be considered 'incorrect' when it is less than or more than twice
+       /// the amount expected.
+       PaymentReceived {
+               /// The hash for which the preimage should be handed to the ChannelManager.
+               payment_hash: PaymentHash,
+               /// The value, in thousandths of a satoshi, that this payment is for. Note that you must
+               /// compare this to the expected value before accepting the payment (as otherwise you are
+               /// providing proof-of-payment for less than the value you expected!).
+               amt: u64,
+       },
+       /// Indicates an outbound payment we made succeeded (ie it made it all the way to its target
+       /// and we got back the payment preimage for it).
+       /// Note that duplicative PaymentSent Events may be generated - it is your responsibility to
+       /// deduplicate them by payment_preimage (which MUST be unique)!
+       PaymentSent {
+               /// The preimage to the hash given to ChannelManager::send_payment.
+               /// Note that this serves as a payment receipt, if you wish to have such a thing, you must
+               /// store it somehow!
+               payment_preimage: PaymentPreimage,
+       },
+       /// Indicates an outbound payment we made failed. Probably some intermediary node dropped
+       /// something. You may wish to retry with a different route.
+       /// Note that duplicative PaymentFailed Events may be generated - it is your responsibility to
+       /// deduplicate them by payment_hash (which MUST be unique)!
+       PaymentFailed {
+               /// The hash which was given to ChannelManager::send_payment.
+               payment_hash: PaymentHash,
+               /// Indicates the payment was rejected for some reason by the recipient. This implies that
+               /// the payment has failed, not just the route in question. If this is not set, you may
+               /// retry the payment via a different route.
+               rejected_by_dest: bool,
+#[cfg(test)]
+               error_code: Option<u16>,
+       },
+       /// Used to indicate that ChannelManager::process_pending_htlc_forwards should be called at a
+       /// time in the future.
+       PendingHTLCsForwardable {
+               /// The minimum amount of time that should be waited prior to calling
+               /// process_pending_htlc_forwards. To increase the effort required to correlate payments,
+               /// you should wait a random amount of time in roughly the range (now + time_forwardable,
+               /// now + 5*time_forwardable).
+               time_forwardable: Duration,
+       },
+       /// Used to indicate that an output was generated on-chain which you should know how to spend.
+       /// Such an output will *not* ever be spent by rust-lightning, so you need to store them
+       /// somewhere and spend them when you create on-chain spends.
+       SpendableOutputs {
+               /// The outputs which you should store as spendable by you.
+               outputs: Vec<SpendableOutputDescriptor>,
+       },
+}
+
+/// An event generated by ChannelManager which indicates a message should be sent to a peer (or
+/// broadcast to most peers).
+/// These events are handled by PeerManager::process_events if you are using a PeerManager.
+#[derive(Clone)]
+pub enum MessageSendEvent {
+       /// Used to indicate that we've accepted a channel open and should send the accept_channel
+       /// message provided to the given peer.
+       SendAcceptChannel {
+               /// The node_id of the node which should receive this message
+               node_id: PublicKey,
+               /// The message which should be sent.
+               msg: msgs::AcceptChannel,
+       },
+       /// Used to indicate that we've initiated a channel open and should send the open_channel
+       /// message provided to the given peer.
+       SendOpenChannel {
+               /// The node_id of the node which should receive this message
+               node_id: PublicKey,
+               /// The message which should be sent.
+               msg: msgs::OpenChannel,
+       },
+       /// Used to indicate that a funding_created message should be sent to the peer with the given node_id.
+       SendFundingCreated {
+               /// The node_id of the node which should receive this message
+               node_id: PublicKey,
+               /// The message which should be sent.
+               msg: msgs::FundingCreated,
+       },
+       /// Used to indicate that a funding_signed message should be sent to the peer with the given node_id.
+       SendFundingSigned {
+               /// The node_id of the node which should receive this message
+               node_id: PublicKey,
+               /// The message which should be sent.
+               msg: msgs::FundingSigned,
+       },
+       /// Used to indicate that a funding_locked message should be sent to the peer with the given node_id.
+       SendFundingLocked {
+               /// The node_id of the node which should receive these message(s)
+               node_id: PublicKey,
+               /// The funding_locked message which should be sent.
+               msg: msgs::FundingLocked,
+       },
+       /// Used to indicate that an announcement_signatures message should be sent to the peer with the given node_id.
+       SendAnnouncementSignatures {
+               /// The node_id of the node which should receive these message(s)
+               node_id: PublicKey,
+               /// The announcement_signatures message which should be sent.
+               msg: msgs::AnnouncementSignatures,
+       },
+       /// Used to indicate that a series of HTLC update messages, as well as a commitment_signed
+       /// message should be sent to the peer with the given node_id.
+       UpdateHTLCs {
+               /// The node_id of the node which should receive these message(s)
+               node_id: PublicKey,
+               /// The update messages which should be sent. ALL messages in the struct should be sent!
+               updates: msgs::CommitmentUpdate,
+       },
+       /// Used to indicate that a revoke_and_ack message should be sent to the peer with the given node_id.
+       SendRevokeAndACK {
+               /// The node_id of the node which should receive this message
+               node_id: PublicKey,
+               /// The message which should be sent.
+               msg: msgs::RevokeAndACK,
+       },
+       /// Used to indicate that a closing_signed message should be sent to the peer with the given node_id.
+       SendClosingSigned {
+               /// The node_id of the node which should receive this message
+               node_id: PublicKey,
+               /// The message which should be sent.
+               msg: msgs::ClosingSigned,
+       },
+       /// Used to indicate that a shutdown message should be sent to the peer with the given node_id.
+       SendShutdown {
+               /// The node_id of the node which should receive this message
+               node_id: PublicKey,
+               /// The message which should be sent.
+               msg: msgs::Shutdown,
+       },
+       /// Used to indicate that a channel_reestablish message should be sent to the peer with the given node_id.
+       SendChannelReestablish {
+               /// The node_id of the node which should receive this message
+               node_id: PublicKey,
+               /// The message which should be sent.
+               msg: msgs::ChannelReestablish,
+       },
+       /// Used to indicate that a channel_announcement and channel_update should be broadcast to all
+       /// peers (except the peer with node_id either msg.contents.node_id_1 or msg.contents.node_id_2).
+       BroadcastChannelAnnouncement {
+               /// The channel_announcement which should be sent.
+               msg: msgs::ChannelAnnouncement,
+               /// The followup channel_update which should be sent.
+               update_msg: msgs::ChannelUpdate,
+       },
+       /// Used to indicate that a channel_update should be broadcast to all peers.
+       BroadcastChannelUpdate {
+               /// The channel_update which should be sent.
+               msg: msgs::ChannelUpdate,
+       },
+       /// Broadcast an error downstream to be handled
+       HandleError {
+               /// The node_id of the node which should receive this message
+               node_id: PublicKey,
+               /// The action which should be taken.
+               action: Option<msgs::ErrorAction>
+       },
+       /// When a payment fails we may receive updates back from the hop where it failed. In such
+       /// cases this event is generated so that we can inform the router of this information.
+       PaymentFailureNetworkUpdate {
+               /// The channel/node update which should be sent to router
+               update: msgs::HTLCFailChannelUpdate,
+       }
+}
+
+/// A trait indicating an object may generate message send events
+pub trait MessageSendEventsProvider {
+       /// Gets the list of pending events which were generated by previous actions, clearing the list
+       /// in the process.
+       fn get_and_clear_pending_msg_events(&self) -> Vec<MessageSendEvent>;
+}
+
+/// A trait indicating an object may generate events
+pub trait EventsProvider {
+       /// Gets the list of pending events which were generated by previous actions, clearing the list
+       /// in the process.
+       fn get_and_clear_pending_events(&self) -> Vec<Event>;
+}
diff --git a/lightning/src/util/fuzz_wrappers.rs b/lightning/src/util/fuzz_wrappers.rs
new file mode 100644 (file)
index 0000000..508acf0
--- /dev/null
@@ -0,0 +1,17 @@
+macro_rules! hash_to_message {
+       ($slice: expr) => {
+               {
+                       #[cfg(not(feature = "fuzztarget"))]
+                       {
+                               ::secp256k1::Message::from_slice($slice).unwrap()
+                       }
+                       #[cfg(feature = "fuzztarget")]
+                       {
+                               match ::secp256k1::Message::from_slice($slice) {
+                                       Ok(msg) => msg,
+                                       Err(_) => ::secp256k1::Message::from_slice(&[1; 32]).unwrap()
+                               }
+                       }
+               }
+       }
+}
diff --git a/lightning/src/util/logger.rs b/lightning/src/util/logger.rs
new file mode 100644 (file)
index 0000000..e727723
--- /dev/null
@@ -0,0 +1,167 @@
+// Pruned copy of crate rust log, without global logger
+// https://github.com/rust-lang-nursery/log #7a60286
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+//! Log traits live here, which are called throughout the library to provide useful information for
+//! debugging purposes.
+//!
+//! There is currently 2 ways to filter log messages. First one, by using compilation features, e.g "max_level_off".
+//! The second one, client-side by implementing check against Record Level field.
+//! Each module may have its own Logger or share one.
+
+use std::cmp;
+use std::fmt;
+use std::sync::Arc;
+
+static LOG_LEVEL_NAMES: [&'static str; 6] = ["OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE"];
+
+/// An enum representing the available verbosity levels of the logger.
+#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
+pub enum Level {
+       ///Designates logger being silent
+       Off,
+       /// Designates very serious errors
+       Error,
+       /// Designates hazardous situations
+       Warn,
+       /// Designates useful information
+       Info,
+       /// Designates lower priority information
+       Debug,
+       /// Designates very low priority, often extremely verbose, information
+       Trace,
+}
+
+impl PartialOrd for Level {
+       #[inline]
+       fn partial_cmp(&self, other: &Level) -> Option<cmp::Ordering> {
+               Some(self.cmp(other))
+       }
+
+       #[inline]
+       fn lt(&self, other: &Level) -> bool {
+               (*self as usize) < *other as usize
+       }
+
+       #[inline]
+       fn le(&self, other: &Level) -> bool {
+               *self as usize <= *other as usize
+       }
+
+       #[inline]
+       fn gt(&self, other: &Level) -> bool {
+               *self as usize > *other as usize
+       }
+
+       #[inline]
+       fn ge(&self, other: &Level) -> bool {
+               *self as usize >= *other as usize
+       }
+}
+
+impl Ord for Level {
+       #[inline]
+       fn cmp(&self, other: &Level) -> cmp::Ordering {
+               (*self as usize).cmp(&(*other as usize))
+       }
+}
+
+impl fmt::Display for Level {
+       fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+               fmt.pad(LOG_LEVEL_NAMES[*self as usize])
+       }
+}
+
+impl Level {
+       /// Returns the most verbose logging level.
+       #[inline]
+       pub fn max() -> Level {
+               Level::Trace
+       }
+}
+
+/// A Record, unit of logging output with Metadata to enable filtering
+/// Module_path, file, line to inform on log's source
+#[derive(Clone,Debug)]
+pub struct Record<'a> {
+       /// The verbosity level of the message.
+       pub level: Level,
+       /// The message body.
+       pub args: fmt::Arguments<'a>,
+       /// The module path of the message.
+       pub module_path: &'a str,
+       /// The source file containing the message.
+       pub file: &'a str,
+       /// The line containing the message.
+       pub line: u32,
+}
+
+impl<'a> Record<'a> {
+       /// Returns a new Record.
+       #[inline]
+       pub fn new(level: Level, args: fmt::Arguments<'a>, module_path: &'a str, file: &'a str, line: u32) -> Record<'a> {
+               Record {
+                       level,
+                       args,
+                       module_path,
+                       file,
+                       line
+               }
+       }
+}
+
+/// A trait encapsulating the operations required of a logger
+pub trait Logger: Sync + Send {
+       /// Logs the `Record`
+       fn log(&self, record: &Record);
+}
+
+pub(crate) struct LogHolder<'a> { pub(crate) logger: &'a Arc<Logger> }
+
+#[cfg(test)]
+mod tests {
+       use util::logger::{Logger, Level};
+       use util::test_utils::TestLogger;
+       use std::sync::{Arc};
+
+       #[test]
+       fn test_level_show() {
+               assert_eq!("INFO", Level::Info.to_string());
+               assert_eq!("ERROR", Level::Error.to_string());
+               assert_ne!("WARN", Level::Error.to_string());
+       }
+
+       struct WrapperLog {
+               logger: Arc<Logger>
+       }
+
+       impl WrapperLog {
+               fn new(logger: Arc<Logger>) -> WrapperLog {
+                       WrapperLog {
+                               logger,
+                       }
+               }
+
+               fn call_macros(&self) {
+                       log_error!(self, "This is an error");
+                       log_warn!(self, "This is a warning");
+                       log_info!(self, "This is an info");
+                       log_debug!(self, "This is a debug");
+                       log_trace!(self, "This is a trace");
+               }
+       }
+
+       #[test]
+       fn test_logging_macros() {
+               let mut logger = TestLogger::new();
+               logger.enable(Level::Trace);
+               let logger : Arc<Logger> = Arc::new(logger);
+               let wrapper = WrapperLog::new(Arc::clone(&logger));
+               wrapper.call_macros();
+       }
+}
diff --git a/lightning/src/util/macro_logger.rs b/lightning/src/util/macro_logger.rs
new file mode 100644 (file)
index 0000000..1f68542
--- /dev/null
@@ -0,0 +1,131 @@
+use chain::transaction::OutPoint;
+
+use bitcoin_hashes::sha256d::Hash as Sha256dHash;
+use secp256k1::key::PublicKey;
+
+use ln::router::Route;
+
+use std;
+
+pub(crate) struct DebugPubKey<'a>(pub &'a PublicKey);
+impl<'a> std::fmt::Display for DebugPubKey<'a> {
+       fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
+               for i in self.0.serialize().iter() {
+                       write!(f, "{:02x}", i)?;
+               }
+               Ok(())
+       }
+}
+macro_rules! log_pubkey {
+       ($obj: expr) => {
+               ::util::macro_logger::DebugPubKey(&$obj)
+       }
+}
+
+pub(crate) struct DebugBytes<'a>(pub &'a [u8]);
+impl<'a> std::fmt::Display for DebugBytes<'a> {
+       fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
+               for i in self.0 {
+                       write!(f, "{:02x}", i)?;
+               }
+               Ok(())
+       }
+}
+macro_rules! log_bytes {
+       ($obj: expr) => {
+               ::util::macro_logger::DebugBytes(&$obj)
+       }
+}
+
+pub(crate) struct DebugFundingChannelId<'a>(pub &'a Sha256dHash, pub u16);
+impl<'a> std::fmt::Display for DebugFundingChannelId<'a> {
+       fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
+               for i in OutPoint::new(self.0.clone(), self.1).to_channel_id().iter() {
+                       write!(f, "{:02x}", i)?;
+               }
+               Ok(())
+       }
+}
+macro_rules! log_funding_channel_id {
+       ($funding_txid: expr, $funding_txo: expr) => {
+               ::util::macro_logger::DebugFundingChannelId(&$funding_txid, $funding_txo)
+       }
+}
+
+pub(crate) struct DebugFundingInfo<'a, T: 'a>(pub &'a Option<(OutPoint, T)>);
+impl<'a, T> std::fmt::Display for DebugFundingInfo<'a, T> {
+       fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
+               match self.0.as_ref() {
+                       Some(&(ref funding_output, _)) => DebugBytes(&funding_output.to_channel_id()[..]).fmt(f),
+                       None => write!(f, "without funding output set"),
+               }
+       }
+}
+macro_rules! log_funding_info {
+       ($key_storage: expr) => {
+               match $key_storage {
+                       Storage::Local { ref funding_info, .. } => {
+                               ::util::macro_logger::DebugFundingInfo(&funding_info)
+                       },
+                       Storage::Watchtower { .. } => {
+                               ::util::macro_logger::DebugFundingInfo(&None)
+                       }
+               }
+       }
+}
+
+pub(crate) struct DebugRoute<'a>(pub &'a Route);
+impl<'a> std::fmt::Display for DebugRoute<'a> {
+       fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
+               for h in self.0.hops.iter() {
+                       write!(f, "node_id: {}, short_channel_id: {}, fee_msat: {}, cltv_expiry_delta: {}\n", log_pubkey!(h.pubkey), h.short_channel_id, h.fee_msat, h.cltv_expiry_delta)?;
+               }
+               Ok(())
+       }
+}
+macro_rules! log_route {
+       ($obj: expr) => {
+               ::util::macro_logger::DebugRoute(&$obj)
+       }
+}
+
+macro_rules! log_internal {
+       ($self: ident, $lvl:expr, $($arg:tt)+) => (
+               &$self.logger.log(&::util::logger::Record::new($lvl, format_args!($($arg)+), module_path!(), file!(), line!()));
+       );
+}
+
+macro_rules! log_error {
+       ($self: ident, $($arg:tt)*) => (
+               #[cfg(not(any(feature = "max_level_off")))]
+               log_internal!($self, $crate::util::logger::Level::Error, $($arg)*);
+       )
+}
+
+macro_rules! log_warn {
+       ($self: ident, $($arg:tt)*) => (
+               #[cfg(not(any(feature = "max_level_off", feature = "max_level_error")))]
+               log_internal!($self, $crate::util::logger::Level::Warn, $($arg)*);
+       )
+}
+
+macro_rules! log_info {
+       ($self: ident, $($arg:tt)*) => (
+               #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn")))]
+               log_internal!($self, $crate::util::logger::Level::Info, $($arg)*);
+       )
+}
+
+macro_rules! log_debug {
+       ($self: ident, $($arg:tt)*) => (
+               #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info")))]
+               log_internal!($self, $crate::util::logger::Level::Debug, $($arg)*);
+       )
+}
+
+macro_rules! log_trace {
+       ($self: ident, $($arg:tt)*) => (
+               #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info", feature = "max_level_debug")))]
+               log_internal!($self, $crate::util::logger::Level::Trace, $($arg)*);
+       )
+}
diff --git a/lightning/src/util/mod.rs b/lightning/src/util/mod.rs
new file mode 100644 (file)
index 0000000..aab7703
--- /dev/null
@@ -0,0 +1,27 @@
+//! Some utility modules live here. See individual sub-modules for more info.
+
+pub mod events;
+pub mod errors;
+pub mod ser;
+
+pub(crate) mod byte_utils;
+pub(crate) mod chacha20;
+#[cfg(not(feature = "fuzztarget"))]
+pub(crate) mod poly1305;
+pub(crate) mod chacha20poly1305rfc;
+pub(crate) mod transaction_utils;
+
+#[macro_use]
+pub(crate) mod ser_macros;
+#[macro_use]
+pub(crate) mod macro_logger;
+
+// These have to come after macro_logger to build
+pub mod logger;
+pub mod config;
+
+#[cfg(test)]
+pub(crate) mod test_utils;
+
+#[macro_use]
+pub(crate) mod fuzz_wrappers;
diff --git a/lightning/src/util/poly1305.rs b/lightning/src/util/poly1305.rs
new file mode 100644 (file)
index 0000000..541c398
--- /dev/null
@@ -0,0 +1,340 @@
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// This is a port of Andrew Moons poly1305-donna
+// https://github.com/floodyberry/poly1305-donna
+
+use std::cmp::min;
+use util::byte_utils::{slice_to_le32, le32_to_array};
+
+#[derive(Clone, Copy)]
+pub struct Poly1305 {
+       r         : [u32; 5],
+       h         : [u32; 5],
+       pad       : [u32; 4],
+       leftover  : usize,
+       buffer    : [u8; 16],
+       finalized : bool,
+}
+
+impl Poly1305 {
+       pub fn new(key: &[u8]) -> Poly1305 {
+               assert!(key.len() == 32);
+               let mut poly = Poly1305{ r: [0u32; 5], h: [0u32; 5], pad: [0u32; 4], leftover: 0, buffer: [0u8; 16], finalized: false };
+
+               // r &= 0xffffffc0ffffffc0ffffffc0fffffff
+               poly.r[0] = (slice_to_le32(&key[0..4])       ) & 0x3ffffff;
+               poly.r[1] = (slice_to_le32(&key[3..7])   >> 2) & 0x3ffff03;
+               poly.r[2] = (slice_to_le32(&key[6..10])  >> 4) & 0x3ffc0ff;
+               poly.r[3] = (slice_to_le32(&key[9..13])  >> 6) & 0x3f03fff;
+               poly.r[4] = (slice_to_le32(&key[12..16]) >> 8) & 0x00fffff;
+
+               poly.pad[0] = slice_to_le32(&key[16..20]);
+               poly.pad[1] = slice_to_le32(&key[20..24]);
+               poly.pad[2] = slice_to_le32(&key[24..28]);
+               poly.pad[3] = slice_to_le32(&key[28..32]);
+
+               poly
+       }
+
+       fn block(&mut self, m: &[u8]) {
+               let hibit : u32 = if self.finalized { 0 } else { 1 << 24 };
+
+               let r0 = self.r[0];
+               let r1 = self.r[1];
+               let r2 = self.r[2];
+               let r3 = self.r[3];
+               let r4 = self.r[4];
+
+               let s1 = r1 * 5;
+               let s2 = r2 * 5;
+               let s3 = r3 * 5;
+               let s4 = r4 * 5;
+
+               let mut h0 = self.h[0];
+               let mut h1 = self.h[1];
+               let mut h2 = self.h[2];
+               let mut h3 = self.h[3];
+               let mut h4 = self.h[4];
+
+               // h += m
+               h0 += (slice_to_le32(&m[0..4])       ) & 0x3ffffff;
+               h1 += (slice_to_le32(&m[3..7])   >> 2) & 0x3ffffff;
+               h2 += (slice_to_le32(&m[6..10])  >> 4) & 0x3ffffff;
+               h3 += (slice_to_le32(&m[9..13])  >> 6) & 0x3ffffff;
+               h4 += (slice_to_le32(&m[12..16]) >> 8) | hibit;
+
+               // h *= r
+               let     d0 = (h0 as u64 * r0 as u64) + (h1 as u64 * s4 as u64) + (h2 as u64 * s3 as u64) + (h3 as u64 * s2 as u64) + (h4 as u64 * s1 as u64);
+               let mut d1 = (h0 as u64 * r1 as u64) + (h1 as u64 * r0 as u64) + (h2 as u64 * s4 as u64) + (h3 as u64 * s3 as u64) + (h4 as u64 * s2 as u64);
+               let mut d2 = (h0 as u64 * r2 as u64) + (h1 as u64 * r1 as u64) + (h2 as u64 * r0 as u64) + (h3 as u64 * s4 as u64) + (h4 as u64 * s3 as u64);
+               let mut d3 = (h0 as u64 * r3 as u64) + (h1 as u64 * r2 as u64) + (h2 as u64 * r1 as u64) + (h3 as u64 * r0 as u64) + (h4 as u64 * s4 as u64);
+               let mut d4 = (h0 as u64 * r4 as u64) + (h1 as u64 * r3 as u64) + (h2 as u64 * r2 as u64) + (h3 as u64 * r1 as u64) + (h4 as u64 * r0 as u64);
+
+               // (partial) h %= p
+               let mut c : u32;
+                               c = (d0 >> 26) as u32; h0 = d0 as u32 & 0x3ffffff;
+               d1 += c as u64; c = (d1 >> 26) as u32; h1 = d1 as u32 & 0x3ffffff;
+               d2 += c as u64; c = (d2 >> 26) as u32; h2 = d2 as u32 & 0x3ffffff;
+               d3 += c as u64; c = (d3 >> 26) as u32; h3 = d3 as u32 & 0x3ffffff;
+               d4 += c as u64; c = (d4 >> 26) as u32; h4 = d4 as u32 & 0x3ffffff;
+               h0 += c * 5;    c = h0 >> 26; h0 = h0 & 0x3ffffff;
+               h1 += c;
+
+               self.h[0] = h0;
+               self.h[1] = h1;
+               self.h[2] = h2;
+               self.h[3] = h3;
+               self.h[4] = h4;
+       }
+
+       pub fn finish(&mut self) {
+               if self.leftover > 0 {
+                       self.buffer[self.leftover] = 1;
+                       for i in self.leftover+1..16 {
+                               self.buffer[i] = 0;
+                       }
+                       self.finalized = true;
+                       let tmp = self.buffer;
+                       self.block(&tmp);
+               }
+
+               // fully carry h
+               let mut h0 = self.h[0];
+               let mut h1 = self.h[1];
+               let mut h2 = self.h[2];
+               let mut h3 = self.h[3];
+               let mut h4 = self.h[4];
+
+               let mut c : u32;
+                            c = h1 >> 26; h1 = h1 & 0x3ffffff;
+               h2 +=     c; c = h2 >> 26; h2 = h2 & 0x3ffffff;
+               h3 +=     c; c = h3 >> 26; h3 = h3 & 0x3ffffff;
+               h4 +=     c; c = h4 >> 26; h4 = h4 & 0x3ffffff;
+               h0 += c * 5; c = h0 >> 26; h0 = h0 & 0x3ffffff;
+               h1 +=     c;
+
+               // compute h + -p
+               let mut g0 = h0.wrapping_add(5); c = g0 >> 26; g0 &= 0x3ffffff;
+               let mut g1 = h1.wrapping_add(c); c = g1 >> 26; g1 &= 0x3ffffff;
+               let mut g2 = h2.wrapping_add(c); c = g2 >> 26; g2 &= 0x3ffffff;
+               let mut g3 = h3.wrapping_add(c); c = g3 >> 26; g3 &= 0x3ffffff;
+               let mut g4 = h4.wrapping_add(c).wrapping_sub(1 << 26);
+
+               // select h if h < p, or h + -p if h >= p
+               let mut mask = (g4 >> (32 - 1)).wrapping_sub(1);
+               g0 &= mask;
+               g1 &= mask;
+               g2 &= mask;
+               g3 &= mask;
+               g4 &= mask;
+               mask = !mask;
+               h0 = (h0 & mask) | g0;
+               h1 = (h1 & mask) | g1;
+               h2 = (h2 & mask) | g2;
+               h3 = (h3 & mask) | g3;
+               h4 = (h4 & mask) | g4;
+
+               // h = h % (2^128)
+               h0 = ((h0      ) | (h1 << 26)) & 0xffffffff;
+               h1 = ((h1 >>  6) | (h2 << 20)) & 0xffffffff;
+               h2 = ((h2 >> 12) | (h3 << 14)) & 0xffffffff;
+               h3 = ((h3 >> 18) | (h4 <<  8)) & 0xffffffff;
+
+               // h = mac = (h + pad) % (2^128)
+               let mut f : u64;
+               f = h0 as u64 + self.pad[0] as u64            ; h0 = f as u32;
+               f = h1 as u64 + self.pad[1] as u64 + (f >> 32); h1 = f as u32;
+               f = h2 as u64 + self.pad[2] as u64 + (f >> 32); h2 = f as u32;
+               f = h3 as u64 + self.pad[3] as u64 + (f >> 32); h3 = f as u32;
+
+               self.h[0] = h0;
+               self.h[1] = h1;
+               self.h[2] = h2;
+               self.h[3] = h3;
+       }
+
+       pub fn input(&mut self, data: &[u8]) {
+               assert!(!self.finalized);
+               let mut m = data;
+
+               if self.leftover > 0 {
+                       let want = min(16 - self.leftover, m.len());
+                       for i in 0..want {
+                               self.buffer[self.leftover+i] = m[i];
+                       }
+                       m = &m[want..];
+                       self.leftover += want;
+
+                       if self.leftover < 16 {
+                               return;
+                       }
+
+                       // self.block(self.buffer[..]);
+                       let tmp = self.buffer;
+                       self.block(&tmp);
+
+                       self.leftover = 0;
+               }
+
+               while m.len() >= 16 {
+                       self.block(&m[0..16]);
+                       m = &m[16..];
+               }
+
+               for i in 0..m.len() {
+                       self.buffer[i] = m[i];
+               }
+               self.leftover = m.len();
+       }
+
+       pub fn raw_result(&mut self, output: &mut [u8]) {
+               assert!(output.len() >= 16);
+               if !self.finalized{
+                       self.finish();
+               }
+               output[0..4].copy_from_slice(&le32_to_array(self.h[0]));
+               output[4..8].copy_from_slice(&le32_to_array(self.h[1]));
+               output[8..12].copy_from_slice(&le32_to_array(self.h[2]));
+               output[12..16].copy_from_slice(&le32_to_array(self.h[3]));
+       }
+}
+
+#[cfg(test)]
+mod test {
+       use std::iter::repeat;
+
+       use util::poly1305::Poly1305;
+
+       fn poly1305(key: &[u8], msg: &[u8], mac: &mut [u8]) {
+               let mut poly = Poly1305::new(key);
+               poly.input(msg);
+               poly.raw_result(mac);
+       }
+
+       #[test]
+       fn test_nacl_vector() {
+               let key = [
+                       0xee,0xa6,0xa7,0x25,0x1c,0x1e,0x72,0x91,
+                       0x6d,0x11,0xc2,0xcb,0x21,0x4d,0x3c,0x25,
+                       0x25,0x39,0x12,0x1d,0x8e,0x23,0x4e,0x65,
+                       0x2d,0x65,0x1f,0xa4,0xc8,0xcf,0xf8,0x80,
+               ];
+
+               let msg = [
+                       0x8e,0x99,0x3b,0x9f,0x48,0x68,0x12,0x73,
+                       0xc2,0x96,0x50,0xba,0x32,0xfc,0x76,0xce,
+                       0x48,0x33,0x2e,0xa7,0x16,0x4d,0x96,0xa4,
+                       0x47,0x6f,0xb8,0xc5,0x31,0xa1,0x18,0x6a,
+                       0xc0,0xdf,0xc1,0x7c,0x98,0xdc,0xe8,0x7b,
+                       0x4d,0xa7,0xf0,0x11,0xec,0x48,0xc9,0x72,
+                       0x71,0xd2,0xc2,0x0f,0x9b,0x92,0x8f,0xe2,
+                       0x27,0x0d,0x6f,0xb8,0x63,0xd5,0x17,0x38,
+                       0xb4,0x8e,0xee,0xe3,0x14,0xa7,0xcc,0x8a,
+                       0xb9,0x32,0x16,0x45,0x48,0xe5,0x26,0xae,
+                       0x90,0x22,0x43,0x68,0x51,0x7a,0xcf,0xea,
+                       0xbd,0x6b,0xb3,0x73,0x2b,0xc0,0xe9,0xda,
+                       0x99,0x83,0x2b,0x61,0xca,0x01,0xb6,0xde,
+                       0x56,0x24,0x4a,0x9e,0x88,0xd5,0xf9,0xb3,
+                       0x79,0x73,0xf6,0x22,0xa4,0x3d,0x14,0xa6,
+                       0x59,0x9b,0x1f,0x65,0x4c,0xb4,0x5a,0x74,
+                       0xe3,0x55,0xa5,
+               ];
+
+               let expected = [
+                       0xf3,0xff,0xc7,0x70,0x3f,0x94,0x00,0xe5,
+                       0x2a,0x7d,0xfb,0x4b,0x3d,0x33,0x05,0xd9,
+               ];
+
+               let mut mac = [0u8; 16];
+               poly1305(&key, &msg, &mut mac);
+               assert_eq!(&mac[..], &expected[..]);
+
+               let mut poly = Poly1305::new(&key);
+               poly.input(&msg[0..32]);
+               poly.input(&msg[32..96]);
+               poly.input(&msg[96..112]);
+               poly.input(&msg[112..120]);
+               poly.input(&msg[120..124]);
+               poly.input(&msg[124..126]);
+               poly.input(&msg[126..127]);
+               poly.input(&msg[127..128]);
+               poly.input(&msg[128..129]);
+               poly.input(&msg[129..130]);
+               poly.input(&msg[130..131]);
+               poly.raw_result(&mut mac);
+               assert_eq!(&mac[..], &expected[..]);
+       }
+
+       #[test]
+       fn donna_self_test() {
+               let wrap_key = [
+                       0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+               ];
+
+               let wrap_msg = [
+                       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+                       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+               ];
+
+               let wrap_mac = [
+                       0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+               ];
+
+               let mut mac = [0u8; 16];
+               poly1305(&wrap_key, &wrap_msg, &mut mac);
+               assert_eq!(&mac[..], &wrap_mac[..]);
+
+               let total_key = [
+                       0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0xff,
+                       0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xff, 0xff,
+                       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+                       0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
+               ];
+
+               let total_mac = [
+                       0x64, 0xaf, 0xe2, 0xe8, 0xd6, 0xad, 0x7b, 0xbd,
+                       0xd2, 0x87, 0xf9, 0x7c, 0x44, 0x62, 0x3d, 0x39,
+               ];
+
+               let mut tpoly = Poly1305::new(&total_key);
+               for i in 0..256 {
+                       let key: Vec<u8> = repeat(i as u8).take(32).collect();
+                       let msg: Vec<u8> = repeat(i as u8).take(256).collect();
+                       let mut mac = [0u8; 16];
+                       poly1305(&key[..], &msg[0..i], &mut mac);
+                       tpoly.input(&mac);
+               }
+               tpoly.raw_result(&mut mac);
+               assert_eq!(&mac[..], &total_mac[..]);
+       }
+
+       #[test]
+       fn test_tls_vectors() {
+               // from http://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-04
+               let key = b"this is 32-byte key for Poly1305";
+               let msg = [0u8; 32];
+               let expected = [
+                       0x49, 0xec, 0x78, 0x09, 0x0e, 0x48, 0x1e, 0xc6,
+                       0xc2, 0x6b, 0x33, 0xb9, 0x1c, 0xcc, 0x03, 0x07,
+               ];
+               let mut mac = [0u8; 16];
+               poly1305(key, &msg, &mut mac);
+               assert_eq!(&mac[..], &expected[..]);
+
+               let msg = b"Hello world!";
+               let expected= [
+                       0xa6, 0xf7, 0x45, 0x00, 0x8f, 0x81, 0xc9, 0x16,
+                       0xa2, 0x0d, 0xcc, 0x74, 0xee, 0xf2, 0xb2, 0xf0,
+               ];
+               poly1305(key, msg, &mut mac);
+               assert_eq!(&mac[..], &expected[..]);
+       }
+}
diff --git a/lightning/src/util/ser.rs b/lightning/src/util/ser.rs
new file mode 100644 (file)
index 0000000..a2ef16b
--- /dev/null
@@ -0,0 +1,444 @@
+//! A very simple serialization framework which is used to serialize/deserialize messages as well
+//! as ChannelsManagers and ChannelMonitors.
+
+use std::result::Result;
+use std::io::{Read, Write};
+use std::collections::HashMap;
+use std::hash::Hash;
+
+use secp256k1::Signature;
+use secp256k1::key::{PublicKey, SecretKey};
+use bitcoin::blockdata::script::Script;
+use bitcoin::blockdata::transaction::OutPoint;
+use bitcoin_hashes::sha256d::Hash as Sha256dHash;
+use std::marker::Sized;
+use ln::msgs::DecodeError;
+use ln::channelmanager::{PaymentPreimage, PaymentHash};
+use util::byte_utils;
+
+use util::byte_utils::{be64_to_array, be48_to_array, be32_to_array, be16_to_array, slice_to_be16, slice_to_be32, slice_to_be48, slice_to_be64};
+
+const MAX_BUF_SIZE: usize = 64 * 1024;
+
+/// A trait that is similar to std::io::Write but has one extra function which can be used to size
+/// buffers being written into.
+/// An impl is provided for any type that also impls std::io::Write which simply ignores size
+/// hints.
+pub trait Writer {
+       /// Writes the given buf out. See std::io::Write::write_all for more
+       fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error>;
+       /// Hints that data of the given size is about the be written. This may not always be called
+       /// prior to data being written and may be safely ignored.
+       fn size_hint(&mut self, size: usize);
+}
+
+impl<W: Write> Writer for W {
+       #[inline]
+       fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
+               <Self as ::std::io::Write>::write_all(self, buf)
+       }
+       #[inline]
+       fn size_hint(&mut self, _size: usize) { }
+}
+
+pub(crate) struct WriterWriteAdaptor<'a, W: Writer + 'a>(pub &'a mut W);
+impl<'a, W: Writer + 'a> Write for WriterWriteAdaptor<'a, W> {
+       fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
+               self.0.write_all(buf)
+       }
+       fn write(&mut self, buf: &[u8]) -> Result<usize, ::std::io::Error> {
+               self.0.write_all(buf)?;
+               Ok(buf.len())
+       }
+       fn flush(&mut self) -> Result<(), ::std::io::Error> {
+               Ok(())
+       }
+}
+
+struct VecWriter(Vec<u8>);
+impl Writer for VecWriter {
+       fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
+               self.0.extend_from_slice(buf);
+               Ok(())
+       }
+       fn size_hint(&mut self, size: usize) {
+               self.0.reserve_exact(size);
+       }
+}
+
+/// A trait that various rust-lightning types implement allowing them to be written out to a Writer
+pub trait Writeable {
+       /// Writes self out to the given Writer
+       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error>;
+
+       /// Writes self out to a Vec<u8>
+       fn encode(&self) -> Vec<u8> {
+               let mut msg = VecWriter(Vec::new());
+               self.write(&mut msg).unwrap();
+               msg.0
+       }
+
+       /// Writes self out to a Vec<u8>
+       fn encode_with_len(&self) -> Vec<u8> {
+               let mut msg = VecWriter(Vec::new());
+               0u16.write(&mut msg).unwrap();
+               self.write(&mut msg).unwrap();
+               let len = msg.0.len();
+               msg.0[..2].copy_from_slice(&byte_utils::be16_to_array(len as u16 - 2));
+               msg.0
+       }
+}
+
+/// A trait that various rust-lightning types implement allowing them to be read in from a Read
+pub trait Readable<R>
+       where Self: Sized,
+             R: Read
+{
+       /// Reads a Self in from the given Read
+       fn read(reader: &mut R) -> Result<Self, DecodeError>;
+}
+
+/// A trait that various higher-level rust-lightning types implement allowing them to be read in
+/// from a Read given some additional set of arguments which is required to deserialize.
+pub trait ReadableArgs<R, P>
+       where Self: Sized,
+             R: Read
+{
+       /// Reads a Self in from the given Read
+       fn read(reader: &mut R, params: P) -> Result<Self, DecodeError>;
+}
+
+pub(crate) struct U48(pub u64);
+impl Writeable for U48 {
+       #[inline]
+       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
+               writer.write_all(&be48_to_array(self.0))
+       }
+}
+impl<R: Read> Readable<R> for U48 {
+       #[inline]
+       fn read(reader: &mut R) -> Result<U48, DecodeError> {
+               let mut buf = [0; 6];
+               reader.read_exact(&mut buf)?;
+               Ok(U48(slice_to_be48(&buf)))
+       }
+}
+
+macro_rules! impl_writeable_primitive {
+       ($val_type:ty, $meth_write:ident, $len: expr, $meth_read:ident) => {
+               impl Writeable for $val_type {
+                       #[inline]
+                       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
+                               writer.write_all(&$meth_write(*self))
+                       }
+               }
+               impl<R: Read> Readable<R> for $val_type {
+                       #[inline]
+                       fn read(reader: &mut R) -> Result<$val_type, DecodeError> {
+                               let mut buf = [0; $len];
+                               reader.read_exact(&mut buf)?;
+                               Ok($meth_read(&buf))
+                       }
+               }
+       }
+}
+
+impl_writeable_primitive!(u64, be64_to_array, 8, slice_to_be64);
+impl_writeable_primitive!(u32, be32_to_array, 4, slice_to_be32);
+impl_writeable_primitive!(u16, be16_to_array, 2, slice_to_be16);
+
+impl Writeable for u8 {
+       #[inline]
+       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
+               writer.write_all(&[*self])
+       }
+}
+impl<R: Read> Readable<R> for u8 {
+       #[inline]
+       fn read(reader: &mut R) -> Result<u8, DecodeError> {
+               let mut buf = [0; 1];
+               reader.read_exact(&mut buf)?;
+               Ok(buf[0])
+       }
+}
+
+impl Writeable for bool {
+       #[inline]
+       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
+               writer.write_all(&[if *self {1} else {0}])
+       }
+}
+impl<R: Read> Readable<R> for bool {
+       #[inline]
+       fn read(reader: &mut R) -> Result<bool, DecodeError> {
+               let mut buf = [0; 1];
+               reader.read_exact(&mut buf)?;
+               if buf[0] != 0 && buf[0] != 1 {
+                       return Err(DecodeError::InvalidValue);
+               }
+               Ok(buf[0] == 1)
+       }
+}
+
+// u8 arrays
+macro_rules! impl_array {
+       ( $size:expr ) => (
+               impl Writeable for [u8; $size]
+               {
+                       #[inline]
+                       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
+                               w.write_all(self)
+                       }
+               }
+
+               impl<R: Read> Readable<R> for [u8; $size]
+               {
+                       #[inline]
+                       fn read(r: &mut R) -> Result<Self, DecodeError> {
+                               let mut buf = [0u8; $size];
+                               r.read_exact(&mut buf)?;
+                               Ok(buf)
+                       }
+               }
+       );
+}
+
+//TODO: performance issue with [u8; size] with impl_array!()
+impl_array!(3); // for rgb
+impl_array!(4); // for IPv4
+impl_array!(10); // for OnionV2
+impl_array!(16); // for IPv6
+impl_array!(32); // for channel id & hmac
+impl_array!(33); // for PublicKey
+impl_array!(64); // for Signature
+impl_array!(1300); // for OnionPacket.hop_data
+
+// HashMap
+impl<K, V> Writeable for HashMap<K, V>
+       where K: Writeable + Eq + Hash,
+             V: Writeable
+{
+       #[inline]
+       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
+       (self.len() as u16).write(w)?;
+               for (key, value) in self.iter() {
+                       key.write(w)?;
+                       value.write(w)?;
+               }
+               Ok(())
+       }
+}
+
+impl<R, K, V> Readable<R> for HashMap<K, V>
+       where R: Read,
+             K: Readable<R> + Eq + Hash,
+             V: Readable<R>
+{
+       #[inline]
+       fn read(r: &mut R) -> Result<Self, DecodeError> {
+               let len: u16 = Readable::read(r)?;
+               let mut ret = HashMap::with_capacity(len as usize);
+               for _ in 0..len {
+                       ret.insert(K::read(r)?, V::read(r)?);
+               }
+               Ok(ret)
+       }
+}
+
+// Vectors
+impl Writeable for Vec<u8> {
+       #[inline]
+       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
+               (self.len() as u16).write(w)?;
+               w.write_all(&self)
+       }
+}
+
+impl<R: Read> Readable<R> for Vec<u8> {
+       #[inline]
+       fn read(r: &mut R) -> Result<Self, DecodeError> {
+               let len: u16 = Readable::read(r)?;
+               let mut ret = Vec::with_capacity(len as usize);
+               ret.resize(len as usize, 0);
+               r.read_exact(&mut ret)?;
+               Ok(ret)
+       }
+}
+impl Writeable for Vec<Signature> {
+       #[inline]
+       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
+               (self.len() as u16).write(w)?;
+               for e in self.iter() {
+                       e.write(w)?;
+               }
+               Ok(())
+       }
+}
+
+impl<R: Read> Readable<R> for Vec<Signature> {
+       #[inline]
+       fn read(r: &mut R) -> Result<Self, DecodeError> {
+               let len: u16 = Readable::read(r)?;
+               let byte_size = (len as usize)
+                               .checked_mul(33)
+                               .ok_or(DecodeError::BadLengthDescriptor)?;
+               if byte_size > MAX_BUF_SIZE {
+                       return Err(DecodeError::BadLengthDescriptor);
+               }
+               let mut ret = Vec::with_capacity(len as usize);
+               for _ in 0..len { ret.push(Signature::read(r)?); }
+               Ok(ret)
+       }
+}
+
+impl Writeable for Script {
+       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
+               (self.len() as u16).write(w)?;
+               w.write_all(self.as_bytes())
+       }
+}
+
+impl<R: Read> Readable<R> for Script {
+       fn read(r: &mut R) -> Result<Self, DecodeError> {
+               let len = <u16 as Readable<R>>::read(r)? as usize;
+               let mut buf = vec![0; len];
+               r.read_exact(&mut buf)?;
+               Ok(Script::from(buf))
+       }
+}
+
+impl Writeable for PublicKey {
+       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
+               self.serialize().write(w)
+       }
+}
+
+impl<R: Read> Readable<R> for PublicKey {
+       fn read(r: &mut R) -> Result<Self, DecodeError> {
+               let buf: [u8; 33] = Readable::read(r)?;
+               match PublicKey::from_slice(&buf) {
+                       Ok(key) => Ok(key),
+                       Err(_) => return Err(DecodeError::InvalidValue),
+               }
+       }
+}
+
+impl Writeable for SecretKey {
+       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
+               let mut ser = [0; 32];
+               ser.copy_from_slice(&self[..]);
+               ser.write(w)
+       }
+}
+
+impl<R: Read> Readable<R> for SecretKey {
+       fn read(r: &mut R) -> Result<Self, DecodeError> {
+               let buf: [u8; 32] = Readable::read(r)?;
+               match SecretKey::from_slice(&buf) {
+                       Ok(key) => Ok(key),
+                       Err(_) => return Err(DecodeError::InvalidValue),
+               }
+       }
+}
+
+impl Writeable for Sha256dHash {
+       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
+               w.write_all(&self[..])
+       }
+}
+
+impl<R: Read> Readable<R> for Sha256dHash {
+       fn read(r: &mut R) -> Result<Self, DecodeError> {
+               use bitcoin_hashes::Hash;
+
+               let buf: [u8; 32] = Readable::read(r)?;
+               Ok(Sha256dHash::from_slice(&buf[..]).unwrap())
+       }
+}
+
+impl Writeable for Signature {
+       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
+               self.serialize_compact().write(w)
+       }
+}
+
+impl<R: Read> Readable<R> for Signature {
+       fn read(r: &mut R) -> Result<Self, DecodeError> {
+               let buf: [u8; 64] = Readable::read(r)?;
+               match Signature::from_compact(&buf) {
+                       Ok(sig) => Ok(sig),
+                       Err(_) => return Err(DecodeError::InvalidValue),
+               }
+       }
+}
+
+impl Writeable for PaymentPreimage {
+       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
+               self.0.write(w)
+       }
+}
+
+impl<R: Read> Readable<R> for PaymentPreimage {
+       fn read(r: &mut R) -> Result<Self, DecodeError> {
+               let buf: [u8; 32] = Readable::read(r)?;
+               Ok(PaymentPreimage(buf))
+       }
+}
+
+impl Writeable for PaymentHash {
+       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
+               self.0.write(w)
+       }
+}
+
+impl<R: Read> Readable<R> for PaymentHash {
+       fn read(r: &mut R) -> Result<Self, DecodeError> {
+               let buf: [u8; 32] = Readable::read(r)?;
+               Ok(PaymentHash(buf))
+       }
+}
+
+impl<T: Writeable> Writeable for Option<T> {
+       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
+               match *self {
+                       None => 0u8.write(w)?,
+                       Some(ref data) => {
+                               1u8.write(w)?;
+                               data.write(w)?;
+                       }
+               }
+               Ok(())
+       }
+}
+
+impl<R, T> Readable<R> for Option<T>
+       where R: Read,
+             T: Readable<R>
+{
+       fn read(r: &mut R) -> Result<Self, DecodeError> {
+               match <u8 as Readable<R>>::read(r)? {
+                       0 => Ok(None),
+                       1 => Ok(Some(Readable::read(r)?)),
+                       _ => return Err(DecodeError::InvalidValue),
+               }
+       }
+}
+
+impl Writeable for OutPoint {
+       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
+               self.txid.write(w)?;
+               self.vout.write(w)?;
+               Ok(())
+       }
+}
+
+impl<R: Read> Readable<R> for OutPoint {
+       fn read(r: &mut R) -> Result<Self, DecodeError> {
+               let txid = Readable::read(r)?;
+               let vout = Readable::read(r)?;
+               Ok(OutPoint {
+                       txid,
+                       vout,
+               })
+       }
+}
diff --git a/lightning/src/util/ser_macros.rs b/lightning/src/util/ser_macros.rs
new file mode 100644 (file)
index 0000000..48e87b3
--- /dev/null
@@ -0,0 +1,42 @@
+macro_rules! impl_writeable {
+       ($st:ident, $len: expr, {$($field:ident),*}) => {
+               impl ::util::ser::Writeable for $st {
+                       fn write<W: ::util::ser::Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
+                               if $len != 0 {
+                                       w.size_hint($len);
+                               }
+                               $( self.$field.write(w)?; )*
+                               Ok(())
+                       }
+               }
+
+               impl<R: ::std::io::Read> ::util::ser::Readable<R> for $st {
+                       fn read(r: &mut R) -> Result<Self, ::ln::msgs::DecodeError> {
+                               Ok(Self {
+                                       $($field: ::util::ser::Readable::read(r)?),*
+                               })
+                       }
+               }
+       }
+}
+macro_rules! impl_writeable_len_match {
+       ($st:ident, {$({$m: pat, $l: expr}),*}, {$($field:ident),*}) => {
+               impl Writeable for $st {
+                       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
+                               w.size_hint(match *self {
+                                       $($m => $l,)*
+                               });
+                               $( self.$field.write(w)?; )*
+                               Ok(())
+                       }
+               }
+
+               impl<R: ::std::io::Read> Readable<R> for $st {
+                       fn read(r: &mut R) -> Result<Self, DecodeError> {
+                               Ok(Self {
+                                       $($field: Readable::read(r)?),*
+                               })
+                       }
+               }
+       }
+}
diff --git a/lightning/src/util/test_utils.rs b/lightning/src/util/test_utils.rs
new file mode 100644 (file)
index 0000000..cc6fe96
--- /dev/null
@@ -0,0 +1,253 @@
+use chain::chaininterface;
+use chain::chaininterface::ConfirmationTarget;
+use chain::transaction::OutPoint;
+use chain::keysinterface;
+use ln::channelmonitor;
+use ln::msgs;
+use ln::msgs::LocalFeatures;
+use ln::msgs::{HandleError};
+use ln::channelmonitor::HTLCUpdate;
+use util::events;
+use util::logger::{Logger, Level, Record};
+use util::ser::{ReadableArgs, Writer};
+
+use bitcoin::blockdata::transaction::Transaction;
+use bitcoin::blockdata::script::Script;
+use bitcoin_hashes::sha256d::Hash as Sha256dHash;
+use bitcoin::network::constants::Network;
+
+use secp256k1::{SecretKey, PublicKey};
+
+use std::time::{SystemTime, UNIX_EPOCH};
+use std::sync::{Arc,Mutex};
+use std::{mem};
+
+pub struct TestVecWriter(pub Vec<u8>);
+impl Writer for TestVecWriter {
+       fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
+               self.0.extend_from_slice(buf);
+               Ok(())
+       }
+       fn size_hint(&mut self, size: usize) {
+               self.0.reserve_exact(size);
+       }
+}
+
+pub struct TestFeeEstimator {
+       pub sat_per_kw: u64,
+}
+impl chaininterface::FeeEstimator for TestFeeEstimator {
+       fn get_est_sat_per_1000_weight(&self, _confirmation_target: ConfirmationTarget) -> u64 {
+               self.sat_per_kw
+       }
+}
+
+pub struct TestChannelMonitor {
+       pub added_monitors: Mutex<Vec<(OutPoint, channelmonitor::ChannelMonitor)>>,
+       pub simple_monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint>>,
+       pub update_ret: Mutex<Result<(), channelmonitor::ChannelMonitorUpdateErr>>,
+}
+impl TestChannelMonitor {
+       pub fn new(chain_monitor: Arc<chaininterface::ChainWatchInterface>, broadcaster: Arc<chaininterface::BroadcasterInterface>, logger: Arc<Logger>, fee_estimator: Arc<chaininterface::FeeEstimator>) -> Self {
+               Self {
+                       added_monitors: Mutex::new(Vec::new()),
+                       simple_monitor: channelmonitor::SimpleManyChannelMonitor::new(chain_monitor, broadcaster, logger, fee_estimator),
+                       update_ret: Mutex::new(Ok(())),
+               }
+       }
+}
+impl channelmonitor::ManyChannelMonitor for TestChannelMonitor {
+       fn add_update_monitor(&self, funding_txo: OutPoint, monitor: channelmonitor::ChannelMonitor) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> {
+               // At every point where we get a monitor update, we should be able to send a useful monitor
+               // to a watchtower and disk...
+               let mut w = TestVecWriter(Vec::new());
+               monitor.write_for_disk(&mut w).unwrap();
+               assert!(<(Sha256dHash, channelmonitor::ChannelMonitor)>::read(
+                               &mut ::std::io::Cursor::new(&w.0), Arc::new(TestLogger::new())).unwrap().1 == monitor);
+               w.0.clear();
+               monitor.write_for_watchtower(&mut w).unwrap(); // This at least shouldn't crash...
+               self.added_monitors.lock().unwrap().push((funding_txo, monitor.clone()));
+               assert!(self.simple_monitor.add_update_monitor(funding_txo, monitor).is_ok());
+               self.update_ret.lock().unwrap().clone()
+       }
+
+       fn fetch_pending_htlc_updated(&self) -> Vec<HTLCUpdate> {
+               return self.simple_monitor.fetch_pending_htlc_updated();
+       }
+}
+
+pub struct TestBroadcaster {
+       pub txn_broadcasted: Mutex<Vec<Transaction>>,
+}
+impl chaininterface::BroadcasterInterface for TestBroadcaster {
+       fn broadcast_transaction(&self, tx: &Transaction) {
+               self.txn_broadcasted.lock().unwrap().push(tx.clone());
+       }
+}
+
+pub struct TestChannelMessageHandler {
+       pub pending_events: Mutex<Vec<events::MessageSendEvent>>,
+}
+
+impl TestChannelMessageHandler {
+       pub fn new() -> Self {
+               TestChannelMessageHandler {
+                       pending_events: Mutex::new(Vec::new()),
+               }
+       }
+}
+
+impl msgs::ChannelMessageHandler for TestChannelMessageHandler {
+       fn handle_open_channel(&self, _their_node_id: &PublicKey, _their_local_features: LocalFeatures, _msg: &msgs::OpenChannel) -> Result<(), HandleError> {
+               Err(HandleError { err: "", action: None })
+       }
+       fn handle_accept_channel(&self, _their_node_id: &PublicKey, _their_local_features: LocalFeatures, _msg: &msgs::AcceptChannel) -> Result<(), HandleError> {
+               Err(HandleError { err: "", action: None })
+       }
+       fn handle_funding_created(&self, _their_node_id: &PublicKey, _msg: &msgs::FundingCreated) -> Result<(), HandleError> {
+               Err(HandleError { err: "", action: None })
+       }
+       fn handle_funding_signed(&self, _their_node_id: &PublicKey, _msg: &msgs::FundingSigned) -> Result<(), HandleError> {
+               Err(HandleError { err: "", action: None })
+       }
+       fn handle_funding_locked(&self, _their_node_id: &PublicKey, _msg: &msgs::FundingLocked) -> Result<(), HandleError> {
+               Err(HandleError { err: "", action: None })
+       }
+       fn handle_shutdown(&self, _their_node_id: &PublicKey, _msg: &msgs::Shutdown) -> Result<(), HandleError> {
+               Err(HandleError { err: "", action: None })
+       }
+       fn handle_closing_signed(&self, _their_node_id: &PublicKey, _msg: &msgs::ClosingSigned) -> Result<(), HandleError> {
+               Err(HandleError { err: "", action: None })
+       }
+       fn handle_update_add_htlc(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateAddHTLC) -> Result<(), HandleError> {
+               Err(HandleError { err: "", action: None })
+       }
+       fn handle_update_fulfill_htlc(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateFulfillHTLC) -> Result<(), HandleError> {
+               Err(HandleError { err: "", action: None })
+       }
+       fn handle_update_fail_htlc(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateFailHTLC) -> Result<(), HandleError> {
+               Err(HandleError { err: "", action: None })
+       }
+       fn handle_update_fail_malformed_htlc(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateFailMalformedHTLC) -> Result<(), HandleError> {
+               Err(HandleError { err: "", action: None })
+       }
+       fn handle_commitment_signed(&self, _their_node_id: &PublicKey, _msg: &msgs::CommitmentSigned) -> Result<(), HandleError> {
+               Err(HandleError { err: "", action: None })
+       }
+       fn handle_revoke_and_ack(&self, _their_node_id: &PublicKey, _msg: &msgs::RevokeAndACK) -> Result<(), HandleError> {
+               Err(HandleError { err: "", action: None })
+       }
+       fn handle_update_fee(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateFee) -> Result<(), HandleError> {
+               Err(HandleError { err: "", action: None })
+       }
+       fn handle_announcement_signatures(&self, _their_node_id: &PublicKey, _msg: &msgs::AnnouncementSignatures) -> Result<(), HandleError> {
+               Err(HandleError { err: "", action: None })
+       }
+       fn handle_channel_reestablish(&self, _their_node_id: &PublicKey, _msg: &msgs::ChannelReestablish) -> Result<(), HandleError> {
+               Err(HandleError { err: "", action: None })
+       }
+       fn peer_disconnected(&self, _their_node_id: &PublicKey, _no_connection_possible: bool) {}
+       fn peer_connected(&self, _their_node_id: &PublicKey) {}
+       fn handle_error(&self, _their_node_id: &PublicKey, _msg: &msgs::ErrorMessage) {}
+}
+
+impl events::MessageSendEventsProvider for TestChannelMessageHandler {
+       fn get_and_clear_pending_msg_events(&self) -> Vec<events::MessageSendEvent> {
+               let mut pending_events = self.pending_events.lock().unwrap();
+               let mut ret = Vec::new();
+               mem::swap(&mut ret, &mut *pending_events);
+               ret
+       }
+}
+
+pub struct TestRoutingMessageHandler {}
+
+impl TestRoutingMessageHandler {
+       pub fn new() -> Self {
+               TestRoutingMessageHandler {}
+       }
+}
+impl msgs::RoutingMessageHandler for TestRoutingMessageHandler {
+       fn handle_node_announcement(&self, _msg: &msgs::NodeAnnouncement) -> Result<bool, HandleError> {
+               Err(HandleError { err: "", action: None })
+       }
+       fn handle_channel_announcement(&self, _msg: &msgs::ChannelAnnouncement) -> Result<bool, HandleError> {
+               Err(HandleError { err: "", action: None })
+       }
+       fn handle_channel_update(&self, _msg: &msgs::ChannelUpdate) -> Result<bool, HandleError> {
+               Err(HandleError { err: "", action: None })
+       }
+       fn handle_htlc_fail_channel_update(&self, _update: &msgs::HTLCFailChannelUpdate) {}
+       fn get_next_channel_announcements(&self, _starting_point: u64, _batch_amount: u8) -> Vec<(msgs::ChannelAnnouncement, msgs::ChannelUpdate,msgs::ChannelUpdate)> {
+               Vec::new()
+       }
+       fn get_next_node_announcements(&self, _starting_point: Option<&PublicKey>, _batch_amount: u8) -> Vec<msgs::NodeAnnouncement> {
+               Vec::new()
+       }
+}
+
+pub struct TestLogger {
+       level: Level,
+       id: String,
+}
+
+impl TestLogger {
+       pub fn new() -> TestLogger {
+               Self::with_id("".to_owned())
+       }
+       pub fn with_id(id: String) -> TestLogger {
+               TestLogger {
+                       level: Level::Trace,
+                       id,
+               }
+       }
+       pub fn enable(&mut self, level: Level) {
+               self.level = level;
+       }
+}
+
+impl Logger for TestLogger {
+       fn log(&self, record: &Record) {
+               if self.level >= record.level {
+                       println!("{:<5} {} [{} : {}, {}] {}", record.level.to_string(), self.id, record.module_path, record.file, record.line, record.args);
+               }
+       }
+}
+
+pub struct TestKeysInterface {
+       backing: keysinterface::KeysManager,
+       pub override_session_priv: Mutex<Option<SecretKey>>,
+       pub override_channel_id_priv: Mutex<Option<[u8; 32]>>,
+}
+
+impl keysinterface::KeysInterface for TestKeysInterface {
+       fn get_node_secret(&self) -> SecretKey { self.backing.get_node_secret() }
+       fn get_destination_script(&self) -> Script { self.backing.get_destination_script() }
+       fn get_shutdown_pubkey(&self) -> PublicKey { self.backing.get_shutdown_pubkey() }
+       fn get_channel_keys(&self, inbound: bool) -> keysinterface::ChannelKeys { self.backing.get_channel_keys(inbound) }
+
+       fn get_session_key(&self) -> SecretKey {
+               match *self.override_session_priv.lock().unwrap() {
+                       Some(key) => key.clone(),
+                       None => self.backing.get_session_key()
+               }
+       }
+
+       fn get_channel_id(&self) -> [u8; 32] {
+               match *self.override_channel_id_priv.lock().unwrap() {
+                       Some(key) => key.clone(),
+                       None => self.backing.get_channel_id()
+               }
+       }
+}
+
+impl TestKeysInterface {
+       pub fn new(seed: &[u8; 32], network: Network, logger: Arc<Logger>) -> Self {
+               let now = SystemTime::now().duration_since(UNIX_EPOCH).expect("Time went backwards");
+               Self {
+                       backing: keysinterface::KeysManager::new(seed, network, logger, now.as_secs(), now.subsec_nanos()),
+                       override_session_priv: Mutex::new(None),
+                       override_channel_id_priv: Mutex::new(None),
+               }
+       }
+}
diff --git a/lightning/src/util/transaction_utils.rs b/lightning/src/util/transaction_utils.rs
new file mode 100644 (file)
index 0000000..a7c1e6b
--- /dev/null
@@ -0,0 +1,152 @@
+use bitcoin::blockdata::transaction::TxOut;
+
+use std::cmp::Ordering;
+
+pub fn sort_outputs<T, C : Fn(&T, &T) -> Ordering>(outputs: &mut Vec<(TxOut, T)>, tie_breaker: C) {
+       outputs.sort_unstable_by(|a, b| {
+               a.0.value.cmp(&b.0.value).then_with(|| {
+                       a.0.script_pubkey[..].cmp(&b.0.script_pubkey[..]).then_with(|| {
+                               tie_breaker(&a.1, &b.1)
+                       })
+               })
+       });
+}
+
+#[cfg(test)]
+mod tests {
+       use super::*;
+
+       use bitcoin::blockdata::script::{Script, Builder};
+       use bitcoin::blockdata::transaction::TxOut;
+
+       use hex::decode;
+
+       #[test]
+       fn sort_output_by_value() {
+               let txout1 = TxOut {
+                       value:  100,
+                       script_pubkey: Builder::new().push_int(0).into_script()
+               };
+               let txout1_ = txout1.clone();
+
+               let txout2 = TxOut {
+                       value: 99,
+                       script_pubkey: Builder::new().push_int(0).into_script()
+               };
+               let txout2_ = txout2.clone();
+
+               let mut outputs = vec![(txout1, "ignore"), (txout2, "ignore")];
+               sort_outputs(&mut outputs, |_, _| { unreachable!(); });
+
+               assert_eq!(
+                       &outputs,
+                       &vec![(txout2_, "ignore"), (txout1_, "ignore")]
+                       );
+       }
+
+       #[test]
+       fn sort_output_by_script_pubkey() {
+               let txout1 = TxOut {
+                       value:  100,
+                       script_pubkey: Builder::new().push_int(3).into_script(),
+               };
+               let txout1_ = txout1.clone();
+
+               let txout2 = TxOut {
+                       value: 100,
+                       script_pubkey: Builder::new().push_int(1).push_int(2).into_script()
+               };
+               let txout2_ = txout2.clone();
+
+               let mut outputs = vec![(txout1, "ignore"), (txout2, "ignore")];
+               sort_outputs(&mut outputs, |_, _| { unreachable!(); });
+
+               assert_eq!(
+                       &outputs,
+                       &vec![(txout2_, "ignore"), (txout1_, "ignore")]
+                       );
+       }
+
+       #[test]
+       fn sort_output_by_bip_test() {
+               let txout1 = TxOut {
+                       value: 100000000,
+                       script_pubkey: script_from_hex("41046a0765b5865641ce08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a68aee3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac")
+               };
+               let txout1_ = txout1.clone();
+
+               // doesn't deserialize cleanly:
+               let txout2 = TxOut {
+                       value: 2400000000,
+                       script_pubkey: script_from_hex("41044a656f065871a353f216ca26cef8dde2f03e8c16202d2e8ad769f02032cb86a5eb5e56842e92e19141d60a01928f8dd2c875a390f67c1f6c94cfc617c0ea45afac")
+               };
+               let txout2_ = txout2.clone();
+
+               let mut outputs = vec![(txout1, "ignore"), (txout2, "ignore")];
+               sort_outputs(&mut outputs, |_, _| { unreachable!(); });
+
+               assert_eq!(&outputs, &vec![(txout1_, "ignore"), (txout2_, "ignore")]);
+       }
+
+       #[test]
+       fn sort_output_tie_breaker_test() {
+               let txout1 = TxOut {
+                       value:  100,
+                       script_pubkey: Builder::new().push_int(1).push_int(2).into_script()
+               };
+               let txout1_ = txout1.clone();
+
+               let txout2 = txout1.clone();
+               let txout2_ = txout1.clone();
+
+               let mut outputs = vec![(txout1, 420), (txout2, 69)];
+               sort_outputs(&mut outputs, |a, b| { a.cmp(b) });
+
+               assert_eq!(
+                       &outputs,
+                       &vec![(txout2_, 69), (txout1_, 420)]
+               );
+       }
+
+       fn script_from_hex(hex_str: &str) -> Script {
+               Script::from(decode(hex_str).unwrap())
+       }
+
+       macro_rules! bip_txout_tests {
+               ($($name:ident: $value:expr,)*) => {
+                       $(
+                               #[test]
+                               fn $name() {
+                                       let expected_raw: Vec<(u64, &str)> = $value;
+                                       let expected: Vec<(TxOut, &str)> = expected_raw.iter()
+                                               .map(|txout_raw| TxOut {
+                                                       value: txout_raw.0,
+                                                       script_pubkey: script_from_hex(txout_raw.1)
+                                               }).map(|txout| (txout, "ignore"))
+                                       .collect();
+
+                                       let mut outputs = expected.clone();
+                                       outputs.reverse(); // prep it
+
+                                       // actually do the work!
+                                       sort_outputs(&mut outputs, |_, _| { unreachable!(); });
+
+                                       assert_eq!(outputs, expected);
+                               }
+                       )*
+               }
+       }
+
+       const TXOUT1: [(u64, &str); 2] = [
+               (400057456, "76a9144a5fba237213a062f6f57978f796390bdcf8d01588ac"),
+               (40000000000, "76a9145be32612930b8323add2212a4ec03c1562084f8488ac"),
+       ];
+       const TXOUT2: [(u64, &str); 2] = [
+               (100000000, "41046a0765b5865641ce08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a68aee3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac"),
+               (2400000000, "41044a656f065871a353f216ca26cef8dde2f03e8c16202d2e8ad769f02032cb86a5eb5e56842e92e19141d60a01928f8dd2c875a390f67c1f6c94cfc617c0ea45afac"),
+       ];
+       bip_txout_tests! {
+               bip69_txout_test_1: TXOUT1.to_vec(),
+               bip69_txout_test_2: TXOUT2.to_vec(),
+       }
+}
diff --git a/net-tokio/Cargo.toml b/net-tokio/Cargo.toml
deleted file mode 100644 (file)
index 6b8d42c..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-[package]
-name = "lightning-net-tokio"
-version = "0.0.1"
-authors = ["Matt Corallo"]
-license = "Apache-2.0"
-description = """
-Implementation of the rust-lightning network stack using Tokio.
-For Rust-Lightning clients which wish to make direct connections to Lightning P2P nodes, this is a simple alternative to implementing the nerequired network stack, especially for those already using Tokio.
-"""
-
-[dependencies]
-bitcoin = "0.20"
-bitcoin_hashes = "0.7"
-lightning = { version = "0.0.9", path = "../" }
-secp256k1 = "0.15"
-tokio-codec = "0.1"
-futures = "0.1"
-tokio = "0.1"
-bytes = "0.4"
diff --git a/net-tokio/src/lib.rs b/net-tokio/src/lib.rs
deleted file mode 100644 (file)
index 0bc36b2..0000000
+++ /dev/null
@@ -1,270 +0,0 @@
-extern crate bytes;
-extern crate tokio;
-extern crate tokio_codec;
-extern crate futures;
-extern crate lightning;
-extern crate secp256k1;
-
-use bytes::BufMut;
-
-use futures::future;
-use futures::future::Future;
-use futures::{AsyncSink, Stream, Sink};
-use futures::sync::mpsc;
-
-use secp256k1::key::PublicKey;
-
-use tokio::timer::Delay;
-use tokio::net::TcpStream;
-
-use lightning::ln::peer_handler;
-use lightning::ln::peer_handler::SocketDescriptor as LnSocketTrait;
-
-use std::mem;
-use std::net::SocketAddr;
-use std::sync::{Arc, Mutex};
-use std::sync::atomic::{AtomicU64, Ordering};
-use std::time::{Duration, Instant};
-use std::vec::Vec;
-use std::hash::Hash;
-
-static ID_COUNTER: AtomicU64 = AtomicU64::new(0);
-
-/// A connection to a remote peer. Can be constructed either as a remote connection using
-/// Connection::setup_outbound o
-pub struct Connection {
-       writer: Option<mpsc::Sender<bytes::Bytes>>,
-       event_notify: mpsc::Sender<()>,
-       pending_read: Vec<u8>,
-       read_blocker: Option<futures::sync::oneshot::Sender<Result<(), ()>>>,
-       read_paused: bool,
-       need_disconnect: bool,
-       id: u64,
-}
-impl Connection {
-       fn schedule_read(peer_manager: Arc<peer_handler::PeerManager<SocketDescriptor>>, us: Arc<Mutex<Self>>, reader: futures::stream::SplitStream<tokio_codec::Framed<TcpStream, tokio_codec::BytesCodec>>) {
-               let us_ref = us.clone();
-               let us_close_ref = us.clone();
-               let peer_manager_ref = peer_manager.clone();
-               tokio::spawn(reader.for_each(move |b| {
-                       let pending_read = b.to_vec();
-                       {
-                               let mut lock = us_ref.lock().unwrap();
-                               assert!(lock.pending_read.is_empty());
-                               if lock.read_paused {
-                                       lock.pending_read = pending_read;
-                                       let (sender, blocker) = futures::sync::oneshot::channel();
-                                       lock.read_blocker = Some(sender);
-                                       return future::Either::A(blocker.then(|_| { Ok(()) }));
-                               }
-                       }
-                       //TODO: There's a race where we don't meet the requirements of disconnect_socket if its
-                       //called right here, after we release the us_ref lock in the scope above, but before we
-                       //call read_event!
-                       match peer_manager.read_event(&mut SocketDescriptor::new(us_ref.clone(), peer_manager.clone()), pending_read) {
-                               Ok(pause_read) => {
-                                       if pause_read {
-                                               let mut lock = us_ref.lock().unwrap();
-                                               lock.read_paused = true;
-                                       }
-                               },
-                               Err(e) => {
-                                       us_ref.lock().unwrap().need_disconnect = false;
-                                       return future::Either::B(future::result(Err(std::io::Error::new(std::io::ErrorKind::InvalidData, e))));
-                               }
-                       }
-
-                       if let Err(e) = us_ref.lock().unwrap().event_notify.try_send(()) {
-                               // Ignore full errors as we just need them to poll after this point, so if the user
-                               // hasn't received the last send yet, it doesn't matter.
-                               assert!(e.is_full());
-                       }
-
-                       future::Either::B(future::result(Ok(())))
-               }).then(move |_| {
-                       if us_close_ref.lock().unwrap().need_disconnect {
-                               peer_manager_ref.disconnect_event(&SocketDescriptor::new(us_close_ref, peer_manager_ref.clone()));
-                               println!("Peer disconnected!");
-                       } else {
-                               println!("We disconnected peer!");
-                       }
-                       Ok(())
-               }));
-       }
-
-       fn new(event_notify: mpsc::Sender<()>, stream: TcpStream) -> (futures::stream::SplitStream<tokio_codec::Framed<TcpStream, tokio_codec::BytesCodec>>, Arc<Mutex<Self>>) {
-               let (writer, reader) = tokio_codec::Framed::new(stream, tokio_codec::BytesCodec::new()).split();
-               let (send_sink, send_stream) = mpsc::channel(3);
-               tokio::spawn(writer.send_all(send_stream.map_err(|_| -> std::io::Error {
-                       unreachable!();
-               })).then(|_| {
-                       future::result(Ok(()))
-               }));
-               let us = Arc::new(Mutex::new(Self { writer: Some(send_sink), event_notify, pending_read: Vec::new(), read_blocker: None, read_paused: false, need_disconnect: true, id: ID_COUNTER.fetch_add(1, Ordering::AcqRel) }));
-
-               (reader, us)
-       }
-
-       /// Process incoming messages and feed outgoing messages on the provided socket generated by
-       /// accepting an incoming connection (by scheduling futures with tokio::spawn).
-       ///
-       /// You should poll the Receive end of event_notify and call get_and_clear_pending_events() on
-       /// ChannelManager and ChannelMonitor objects.
-       pub fn setup_inbound(peer_manager: Arc<peer_handler::PeerManager<SocketDescriptor>>, event_notify: mpsc::Sender<()>, stream: TcpStream) {
-               let (reader, us) = Self::new(event_notify, stream);
-
-               if let Ok(_) = peer_manager.new_inbound_connection(SocketDescriptor::new(us.clone(), peer_manager.clone())) {
-                       Self::schedule_read(peer_manager, us, reader);
-               }
-       }
-
-       /// Process incoming messages and feed outgoing messages on the provided socket generated by
-       /// making an outbound connection which is expected to be accepted by a peer with the given
-       /// public key (by scheduling futures with tokio::spawn).
-       ///
-       /// You should poll the Receive end of event_notify and call get_and_clear_pending_events() on
-       /// ChannelManager and ChannelMonitor objects.
-       pub fn setup_outbound(peer_manager: Arc<peer_handler::PeerManager<SocketDescriptor>>, event_notify: mpsc::Sender<()>, their_node_id: PublicKey, stream: TcpStream) {
-               let (reader, us) = Self::new(event_notify, stream);
-
-               if let Ok(initial_send) = peer_manager.new_outbound_connection(their_node_id, SocketDescriptor::new(us.clone(), peer_manager.clone())) {
-                       if SocketDescriptor::new(us.clone(), peer_manager.clone()).send_data(&initial_send, true) == initial_send.len() {
-                               Self::schedule_read(peer_manager, us, reader);
-                       } else {
-                               println!("Failed to write first full message to socket!");
-                       }
-               }
-       }
-
-       /// Process incoming messages and feed outgoing messages on a new connection made to the given
-       /// socket address which is expected to be accepted by a peer with the given public key (by
-       /// scheduling futures with tokio::spawn).
-       ///
-       /// You should poll the Receive end of event_notify and call get_and_clear_pending_events() on
-       /// ChannelManager and ChannelMonitor objects.
-       pub fn connect_outbound(peer_manager: Arc<peer_handler::PeerManager<SocketDescriptor>>, event_notify: mpsc::Sender<()>, their_node_id: PublicKey, addr: SocketAddr) {
-               let connect_timeout = Delay::new(Instant::now() + Duration::from_secs(10)).then(|_| {
-                       future::err(std::io::Error::new(std::io::ErrorKind::TimedOut, "timeout reached"))
-               });
-               tokio::spawn(TcpStream::connect(&addr).select(connect_timeout)
-                       .and_then(move |stream| {
-                               Connection::setup_outbound(peer_manager, event_notify, their_node_id, stream.0);
-                               future::ok(())
-                       }).or_else(|_| {
-                               //TODO: return errors somehow
-                               future::ok(())
-                       }));
-       }
-}
-
-#[derive(Clone)]
-pub struct SocketDescriptor {
-       conn: Arc<Mutex<Connection>>,
-       id: u64,
-       peer_manager: Arc<peer_handler::PeerManager<SocketDescriptor>>,
-}
-impl SocketDescriptor {
-       fn new(conn: Arc<Mutex<Connection>>, peer_manager: Arc<peer_handler::PeerManager<SocketDescriptor>>) -> Self {
-               let id = conn.lock().unwrap().id;
-               Self { conn, id, peer_manager }
-       }
-}
-impl peer_handler::SocketDescriptor for SocketDescriptor {
-       fn send_data(&mut self, data: &[u8], resume_read: bool) -> usize {
-               macro_rules! schedule_read {
-                       ($us_ref: expr) => {
-                               tokio::spawn(future::lazy(move || -> Result<(), ()> {
-                                       let mut read_data = Vec::new();
-                                       {
-                                               let mut us = $us_ref.conn.lock().unwrap();
-                                               mem::swap(&mut read_data, &mut us.pending_read);
-                                       }
-                                       if !read_data.is_empty() {
-                                               let mut us_clone = $us_ref.clone();
-                                               match $us_ref.peer_manager.read_event(&mut us_clone, read_data) {
-                                                       Ok(pause_read) => {
-                                                               if pause_read { return Ok(()); }
-                                                       },
-                                                       Err(_) => {
-                                                               //TODO: Not actually sure how to do this
-                                                               return Ok(());
-                                                       }
-                                               }
-                                       }
-                                       let mut us = $us_ref.conn.lock().unwrap();
-                                       if let Some(sender) = us.read_blocker.take() {
-                                               sender.send(Ok(())).unwrap();
-                                       }
-                                       us.read_paused = false;
-                                       if let Err(e) = us.event_notify.try_send(()) {
-                                               // Ignore full errors as we just need them to poll after this point, so if the user
-                                               // hasn't received the last send yet, it doesn't matter.
-                                               assert!(e.is_full());
-                                       }
-                                       Ok(())
-                               }));
-                       }
-               }
-
-               let mut us = self.conn.lock().unwrap();
-               if resume_read {
-                       let us_ref = self.clone();
-                       schedule_read!(us_ref);
-               }
-               if data.is_empty() { return 0; }
-               if us.writer.is_none() {
-                       us.read_paused = true;
-                       return 0;
-               }
-
-               let mut bytes = bytes::BytesMut::with_capacity(data.len());
-               bytes.put(data);
-               let write_res = us.writer.as_mut().unwrap().start_send(bytes.freeze());
-               match write_res {
-                       Ok(res) => {
-                               match res {
-                                       AsyncSink::Ready => {
-                                               data.len()
-                                       },
-                                       AsyncSink::NotReady(_) => {
-                                               us.read_paused = true;
-                                               let us_ref = self.clone();
-                                               tokio::spawn(us.writer.take().unwrap().flush().then(move |writer_res| -> Result<(), ()> {
-                                                       if let Ok(writer) = writer_res {
-                                                               {
-                                                                       let mut us = us_ref.conn.lock().unwrap();
-                                                                       us.writer = Some(writer);
-                                                               }
-                                                               schedule_read!(us_ref);
-                                                       } // we'll fire the disconnect event on the socket reader end
-                                                       Ok(())
-                                               }));
-                                               0
-                                       }
-                               }
-                       },
-                       Err(_) => {
-                               // We'll fire the disconnected event on the socket reader end
-                               0
-                       },
-               }
-       }
-
-       fn disconnect_socket(&mut self) {
-               let mut us = self.conn.lock().unwrap();
-               us.need_disconnect = true;
-               us.read_paused = true;
-       }
-}
-impl Eq for SocketDescriptor {}
-impl PartialEq for SocketDescriptor {
-       fn eq(&self, o: &Self) -> bool {
-               self.id == o.id
-       }
-}
-impl Hash for SocketDescriptor {
-       fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
-               self.id.hash(state);
-       }
-}
-
diff --git a/src/chain/chaininterface.rs b/src/chain/chaininterface.rs
deleted file mode 100644 (file)
index c0330fb..0000000
+++ /dev/null
@@ -1,321 +0,0 @@
-//! Traits and utility impls which allow other parts of rust-lightning to interact with the
-//! blockchain.
-//!
-//! Includes traits for monitoring and receiving notifications of new blocks and block
-//! disconnections, transaction broadcasting, and feerate information requests.
-
-use bitcoin::blockdata::block::{Block, BlockHeader};
-use bitcoin::blockdata::transaction::Transaction;
-use bitcoin::blockdata::script::Script;
-use bitcoin::blockdata::constants::genesis_block;
-use bitcoin::util::hash::BitcoinHash;
-use bitcoin_hashes::sha256d::Hash as Sha256dHash;
-use bitcoin::network::constants::Network;
-
-use util::logger::Logger;
-
-use std::sync::{Mutex,Weak,MutexGuard,Arc};
-use std::sync::atomic::{AtomicUsize, Ordering};
-use std::collections::HashSet;
-
-/// Used to give chain error details upstream
-pub enum ChainError {
-       /// Client doesn't support UTXO lookup (but the chain hash matches our genesis block hash)
-       NotSupported,
-       /// Chain isn't the one watched
-       NotWatched,
-       /// Tx doesn't exist or is unconfirmed
-       UnknownTx,
-}
-
-/// An interface to request notification of certain scripts as they appear the
-/// chain.
-///
-/// Note that all of the functions implemented here *must* be reentrant-safe (obviously - they're
-/// called from inside the library in response to ChainListener events, P2P events, or timer
-/// events).
-pub trait ChainWatchInterface: Sync + Send {
-       /// Provides a txid/random-scriptPubKey-in-the-tx which much be watched for.
-       fn install_watch_tx(&self, txid: &Sha256dHash, script_pub_key: &Script);
-
-       /// Provides an outpoint which must be watched for, providing any transactions which spend the
-       /// given outpoint.
-       fn install_watch_outpoint(&self, outpoint: (Sha256dHash, u32), out_script: &Script);
-
-       /// Indicates that a listener needs to see all transactions.
-       fn watch_all_txn(&self);
-
-       /// Register the given listener to receive events. Only a weak pointer is provided and the
-       /// registration should be freed once that pointer expires.
-       fn register_listener(&self, listener: Weak<ChainListener>);
-       //TODO: unregister
-
-       /// Gets the script and value in satoshis for a given unspent transaction output given a
-       /// short_channel_id (aka unspent_tx_output_identier). For BTC/tBTC channels the top three
-       /// bytes are the block height, the next 3 the transaction index within the block, and the
-       /// final two the output within the transaction.
-       fn get_chain_utxo(&self, genesis_hash: Sha256dHash, unspent_tx_output_identifier: u64) -> Result<(Script, u64), ChainError>;
-}
-
-/// An interface to send a transaction to the Bitcoin network.
-pub trait BroadcasterInterface: Sync + Send {
-       /// Sends a transaction out to (hopefully) be mined.
-       fn broadcast_transaction(&self, tx: &Transaction);
-}
-
-/// A trait indicating a desire to listen for events from the chain
-pub trait ChainListener: Sync + Send {
-       /// Notifies a listener that a block was connected.
-       /// Note that if a new transaction/outpoint is watched during a block_connected call, the block
-       /// *must* be re-scanned with the new transaction/outpoints and block_connected should be
-       /// called again with the same header and (at least) the new transactions.
-       ///
-       /// Note that if non-new transaction/outpoints may be registered during a call, a second call
-       /// *must not* happen.
-       ///
-       /// This also means those counting confirmations using block_connected callbacks should watch
-       /// for duplicate headers and not count them towards confirmations!
-       fn block_connected(&self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], indexes_of_txn_matched: &[u32]);
-       /// Notifies a listener that a block was disconnected.
-       /// Unlike block_connected, this *must* never be called twice for the same disconnect event.
-       /// Height must be the one of the block which was disconnected (not new height of the best chain)
-       fn block_disconnected(&self, header: &BlockHeader, disconnected_height: u32);
-}
-
-/// An enum that represents the speed at which we want a transaction to confirm used for feerate
-/// estimation.
-pub enum ConfirmationTarget {
-       /// We are happy with this transaction confirming slowly when feerate drops some.
-       Background,
-       /// We'd like this transaction to confirm without major delay, but 12-18 blocks is fine.
-       Normal,
-       /// We'd like this transaction to confirm in the next few blocks.
-       HighPriority,
-}
-
-/// A trait which should be implemented to provide feerate information on a number of time
-/// horizons.
-///
-/// Note that all of the functions implemented here *must* be reentrant-safe (obviously - they're
-/// called from inside the library in response to ChainListener events, P2P events, or timer
-/// events).
-pub trait FeeEstimator: Sync + Send {
-       /// Gets estimated satoshis of fee required per 1000 Weight-Units.
-       ///
-       /// Must be no smaller than 253 (ie 1 satoshi-per-byte rounded up to ensure later round-downs
-       /// don't put us below 1 satoshi-per-byte).
-       ///
-       /// This translates to:
-       ///  * satoshis-per-byte * 250
-       ///  * ceil(satoshis-per-kbyte / 4)
-       fn get_est_sat_per_1000_weight(&self, confirmation_target: ConfirmationTarget) -> u64;
-}
-
-/// Utility for tracking registered txn/outpoints and checking for matches
-pub struct ChainWatchedUtil {
-       watch_all: bool,
-
-       // We are more conservative in matching during testing to ensure everything matches *exactly*,
-       // even though during normal runtime we take more optimized match approaches...
-       #[cfg(test)]
-       watched_txn: HashSet<(Sha256dHash, Script)>,
-       #[cfg(not(test))]
-       watched_txn: HashSet<Script>,
-
-       watched_outpoints: HashSet<(Sha256dHash, u32)>,
-}
-
-impl ChainWatchedUtil {
-       /// Constructs an empty (watches nothing) ChainWatchedUtil
-       pub fn new() -> Self {
-               Self {
-                       watch_all: false,
-                       watched_txn: HashSet::new(),
-                       watched_outpoints: HashSet::new(),
-               }
-       }
-
-       /// Registers a tx for monitoring, returning true if it was a new tx and false if we'd already
-       /// been watching for it.
-       pub fn register_tx(&mut self, txid: &Sha256dHash, script_pub_key: &Script) -> bool {
-               if self.watch_all { return false; }
-               #[cfg(test)]
-               {
-                       self.watched_txn.insert((txid.clone(), script_pub_key.clone()))
-               }
-               #[cfg(not(test))]
-               {
-                       let _tx_unused = txid; // It's used in cfg(test), though
-                       self.watched_txn.insert(script_pub_key.clone())
-               }
-       }
-
-       /// Registers an outpoint for monitoring, returning true if it was a new outpoint and false if
-       /// we'd already been watching for it
-       pub fn register_outpoint(&mut self, outpoint: (Sha256dHash, u32), _script_pub_key: &Script) -> bool {
-               if self.watch_all { return false; }
-               self.watched_outpoints.insert(outpoint)
-       }
-
-       /// Sets us to match all transactions, returning true if this is a new setting and false if
-       /// we'd already been set to match everything.
-       pub fn watch_all(&mut self) -> bool {
-               if self.watch_all { return false; }
-               self.watch_all = true;
-               true
-       }
-
-       /// Checks if a given transaction matches the current filter.
-       pub fn does_match_tx(&self, tx: &Transaction) -> bool {
-               if self.watch_all {
-                       return true;
-               }
-               for out in tx.output.iter() {
-                       #[cfg(test)]
-                       for &(ref txid, ref script) in self.watched_txn.iter() {
-                               if *script == out.script_pubkey {
-                                       if tx.txid() == *txid {
-                                               return true;
-                                       }
-                               }
-                       }
-                       #[cfg(not(test))]
-                       for script in self.watched_txn.iter() {
-                               if *script == out.script_pubkey {
-                                       return true;
-                               }
-                       }
-               }
-               for input in tx.input.iter() {
-                       for outpoint in self.watched_outpoints.iter() {
-                               let &(outpoint_hash, outpoint_index) = outpoint;
-                               if outpoint_hash == input.previous_output.txid && outpoint_index == input.previous_output.vout {
-                                       return true;
-                               }
-                       }
-               }
-               false
-       }
-}
-
-/// Utility to capture some common parts of ChainWatchInterface implementors.
-///
-/// Keeping a local copy of this in a ChainWatchInterface implementor is likely useful.
-pub struct ChainWatchInterfaceUtil {
-       network: Network,
-       watched: Mutex<ChainWatchedUtil>,
-       listeners: Mutex<Vec<Weak<ChainListener>>>,
-       reentered: AtomicUsize,
-       logger: Arc<Logger>,
-}
-
-/// Register listener
-impl ChainWatchInterface for ChainWatchInterfaceUtil {
-       fn install_watch_tx(&self, txid: &Sha256dHash, script_pub_key: &Script) {
-               let mut watched = self.watched.lock().unwrap();
-               if watched.register_tx(txid, script_pub_key) {
-                       self.reentered.fetch_add(1, Ordering::Relaxed);
-               }
-       }
-
-       fn install_watch_outpoint(&self, outpoint: (Sha256dHash, u32), out_script: &Script) {
-               let mut watched = self.watched.lock().unwrap();
-               if watched.register_outpoint(outpoint, out_script) {
-                       self.reentered.fetch_add(1, Ordering::Relaxed);
-               }
-       }
-
-       fn watch_all_txn(&self) {
-               let mut watched = self.watched.lock().unwrap();
-               if watched.watch_all() {
-                       self.reentered.fetch_add(1, Ordering::Relaxed);
-               }
-       }
-
-       fn register_listener(&self, listener: Weak<ChainListener>) {
-               let mut vec = self.listeners.lock().unwrap();
-               vec.push(listener);
-       }
-
-       fn get_chain_utxo(&self, genesis_hash: Sha256dHash, _unspent_tx_output_identifier: u64) -> Result<(Script, u64), ChainError> {
-               if genesis_hash != genesis_block(self.network).header.bitcoin_hash() {
-                       return Err(ChainError::NotWatched);
-               }
-               Err(ChainError::NotSupported)
-       }
-}
-
-impl ChainWatchInterfaceUtil {
-       /// Creates a new ChainWatchInterfaceUtil for the given network
-       pub fn new(network: Network, logger: Arc<Logger>) -> ChainWatchInterfaceUtil {
-               ChainWatchInterfaceUtil {
-                       network: network,
-                       watched: Mutex::new(ChainWatchedUtil::new()),
-                       listeners: Mutex::new(Vec::new()),
-                       reentered: AtomicUsize::new(1),
-                       logger: logger,
-               }
-       }
-
-       /// Notify listeners that a block was connected given a full, unfiltered block.
-       ///
-       /// Handles re-scanning the block and calling block_connected again if listeners register new
-       /// watch data during the callbacks for you (see ChainListener::block_connected for more info).
-       pub fn block_connected_with_filtering(&self, block: &Block, height: u32) {
-               let mut reentered = true;
-               while reentered {
-                       let mut matched = Vec::new();
-                       let mut matched_index = Vec::new();
-                       {
-                               let watched = self.watched.lock().unwrap();
-                               for (index, transaction) in block.txdata.iter().enumerate() {
-                                       if self.does_match_tx_unguarded(transaction, &watched) {
-                                               matched.push(transaction);
-                                               matched_index.push(index as u32);
-                                       }
-                               }
-                       }
-                       reentered = self.block_connected_checked(&block.header, height, matched.as_slice(), matched_index.as_slice());
-               }
-       }
-
-       /// Notify listeners that a block was disconnected.
-       pub fn block_disconnected(&self, header: &BlockHeader, disconnected_height: u32) {
-               let listeners = self.listeners.lock().unwrap().clone();
-               for listener in listeners.iter() {
-                       match listener.upgrade() {
-                               Some(arc) => arc.block_disconnected(&header, disconnected_height),
-                               None => ()
-                       }
-               }
-       }
-
-       /// Notify listeners that a block was connected, given pre-filtered list of transactions in the
-       /// block which matched the filter (probably using does_match_tx).
-       ///
-       /// Returns true if notified listeners registered additional watch data (implying that the
-       /// block must be re-scanned and this function called again prior to further block_connected
-       /// calls, see ChainListener::block_connected for more info).
-       pub fn block_connected_checked(&self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], indexes_of_txn_matched: &[u32]) -> bool {
-               let last_seen = self.reentered.load(Ordering::Relaxed);
-
-               let listeners = self.listeners.lock().unwrap().clone();
-               for listener in listeners.iter() {
-                       match listener.upgrade() {
-                               Some(arc) => arc.block_connected(header, height, txn_matched, indexes_of_txn_matched),
-                               None => ()
-                       }
-               }
-               return last_seen != self.reentered.load(Ordering::Relaxed);
-       }
-
-       /// Checks if a given transaction matches the current filter.
-       pub fn does_match_tx(&self, tx: &Transaction) -> bool {
-               let watched = self.watched.lock().unwrap();
-               self.does_match_tx_unguarded (tx, &watched)
-       }
-
-       fn does_match_tx_unguarded(&self, tx: &Transaction, watched: &MutexGuard<ChainWatchedUtil>) -> bool {
-               watched.does_match_tx(tx)
-       }
-}
diff --git a/src/chain/keysinterface.rs b/src/chain/keysinterface.rs
deleted file mode 100644 (file)
index 73d320b..0000000
+++ /dev/null
@@ -1,279 +0,0 @@
-//! keysinterface provides keys into rust-lightning and defines some useful enums which describe
-//! spendable on-chain outputs which the user owns and is responsible for using just as any other
-//! on-chain output which is theirs.
-
-use bitcoin::blockdata::transaction::{OutPoint, TxOut};
-use bitcoin::blockdata::script::{Script, Builder};
-use bitcoin::blockdata::opcodes;
-use bitcoin::network::constants::Network;
-use bitcoin::util::bip32::{ExtendedPrivKey, ExtendedPubKey, ChildNumber};
-
-use bitcoin_hashes::{Hash, HashEngine};
-use bitcoin_hashes::sha256::HashEngine as Sha256State;
-use bitcoin_hashes::sha256::Hash as Sha256;
-use bitcoin_hashes::hash160::Hash as Hash160;
-
-use secp256k1::key::{SecretKey, PublicKey};
-use secp256k1::Secp256k1;
-use secp256k1;
-
-use util::byte_utils;
-use util::logger::Logger;
-
-use std::sync::Arc;
-use std::sync::atomic::{AtomicUsize, Ordering};
-
-/// When on-chain outputs are created by rust-lightning an event is generated which informs the
-/// user thereof. This enum describes the format of the output and provides the OutPoint.
-pub enum SpendableOutputDescriptor {
-       /// Outpoint with an output to a script which was provided via KeysInterface, thus you should
-       /// have stored somewhere how to spend script_pubkey!
-       /// Outputs from a justice tx, claim tx or preimage tx
-       StaticOutput {
-               /// The outpoint spendable by user wallet
-               outpoint: OutPoint,
-               /// The output which is referenced by the given outpoint
-               output: TxOut,
-       },
-       /// Outpoint commits to a P2WSH
-       /// P2WSH should be spend by the following witness :
-       /// <local_delayedsig> 0 <witnessScript>
-       /// With input nSequence set to_self_delay.
-       /// Outputs from a HTLC-Success/Timeout tx/commitment tx
-       DynamicOutputP2WSH {
-               /// Outpoint spendable by user wallet
-               outpoint: OutPoint,
-               /// local_delayedkey = delayed_payment_basepoint_secret + SHA256(per_commitment_point || delayed_payment_basepoint) OR
-               key: SecretKey,
-               /// witness redeemScript encumbering output.
-               witness_script: Script,
-               /// nSequence input must commit to self_delay to satisfy script's OP_CSV
-               to_self_delay: u16,
-               /// The output which is referenced by the given outpoint
-               output: TxOut,
-       },
-       /// Outpoint commits to a P2WPKH
-       /// P2WPKH should be spend by the following witness :
-       /// <local_sig> <local_pubkey>
-       /// Outputs to_remote from a commitment tx
-       DynamicOutputP2WPKH {
-               /// Outpoint spendable by user wallet
-               outpoint: OutPoint,
-               /// localkey = payment_basepoint_secret + SHA256(per_commitment_point || payment_basepoint
-               key: SecretKey,
-               /// The output which is reference by the given outpoint
-               output: TxOut,
-       }
-}
-
-/// A trait to describe an object which can get user secrets and key material.
-pub trait KeysInterface: Send + Sync {
-       /// Get node secret key (aka node_id or network_key)
-       fn get_node_secret(&self) -> SecretKey;
-       /// Get destination redeemScript to encumber static protocol exit points.
-       fn get_destination_script(&self) -> Script;
-       /// Get shutdown_pubkey to use as PublicKey at channel closure
-       fn get_shutdown_pubkey(&self) -> PublicKey;
-       /// Get a new set of ChannelKeys for per-channel secrets. These MUST be unique even if you
-       /// restarted with some stale data!
-       fn get_channel_keys(&self, inbound: bool) -> ChannelKeys;
-       /// Get a secret for construting an onion packet
-       fn get_session_key(&self) -> SecretKey;
-       /// Get a unique temporary channel id. Channels will be referred to by this until the funding
-       /// transaction is created, at which point they will use the outpoint in the funding
-       /// transaction.
-       fn get_channel_id(&self) -> [u8; 32];
-}
-
-/// Set of lightning keys needed to operate a channel as described in BOLT 3
-#[derive(Clone)]
-pub struct ChannelKeys {
-       /// Private key of anchor tx
-       pub funding_key: SecretKey,
-       /// Local secret key for blinded revocation pubkey
-       pub revocation_base_key: SecretKey,
-       /// Local secret key used in commitment tx htlc outputs
-       pub payment_base_key: SecretKey,
-       /// Local secret key used in HTLC tx
-       pub delayed_payment_base_key: SecretKey,
-       /// Local htlc secret key used in commitment tx htlc outputs
-       pub htlc_base_key: SecretKey,
-       /// Commitment seed
-       pub commitment_seed: [u8; 32],
-}
-
-impl_writeable!(ChannelKeys, 0, {
-       funding_key,
-       revocation_base_key,
-       payment_base_key,
-       delayed_payment_base_key,
-       htlc_base_key,
-       commitment_seed
-});
-
-/// Simple KeysInterface implementor that takes a 32-byte seed for use as a BIP 32 extended key
-/// and derives keys from that.
-///
-/// Your node_id is seed/0'
-/// ChannelMonitor closes may use seed/1'
-/// Cooperative closes may use seed/2'
-/// The two close keys may be needed to claim on-chain funds!
-pub struct KeysManager {
-       secp_ctx: Secp256k1<secp256k1::SignOnly>,
-       node_secret: SecretKey,
-       destination_script: Script,
-       shutdown_pubkey: PublicKey,
-       channel_master_key: ExtendedPrivKey,
-       channel_child_index: AtomicUsize,
-       session_master_key: ExtendedPrivKey,
-       session_child_index: AtomicUsize,
-       channel_id_master_key: ExtendedPrivKey,
-       channel_id_child_index: AtomicUsize,
-
-       unique_start: Sha256State,
-       logger: Arc<Logger>,
-}
-
-impl KeysManager {
-       /// Constructs a KeysManager from a 32-byte seed. If the seed is in some way biased (eg your
-       /// RNG is busted) this may panic (but more importantly, you will possibly lose funds).
-       /// starting_time isn't strictly required to actually be a time, but it must absolutely,
-       /// without a doubt, be unique to this instance. ie if you start multiple times with the same
-       /// seed, starting_time must be unique to each run. Thus, the easiest way to achieve this is to
-       /// simply use the current time (with very high precision).
-       ///
-       /// The seed MUST be backed up safely prior to use so that the keys can be re-created, however,
-       /// obviously, starting_time should be unique every time you reload the library - it is only
-       /// used to generate new ephemeral key data (which will be stored by the individual channel if
-       /// necessary).
-       ///
-       /// Note that the seed is required to recover certain on-chain funds independent of
-       /// ChannelMonitor data, though a current copy of ChannelMonitor data is also required for any
-       /// channel, and some on-chain during-closing funds.
-       ///
-       /// Note that until the 0.1 release there is no guarantee of backward compatibility between
-       /// versions. Once the library is more fully supported, the docs will be updated to include a
-       /// detailed description of the guarantee.
-       pub fn new(seed: &[u8; 32], network: Network, logger: Arc<Logger>, starting_time_secs: u64, starting_time_nanos: u32) -> KeysManager {
-               let secp_ctx = Secp256k1::signing_only();
-               match ExtendedPrivKey::new_master(network.clone(), seed) {
-                       Ok(master_key) => {
-                               let node_secret = master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(0).unwrap()).expect("Your RNG is busted").private_key.key;
-                               let destination_script = match master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(1).unwrap()) {
-                                       Ok(destination_key) => {
-                                               let pubkey_hash160 = Hash160::hash(&ExtendedPubKey::from_private(&secp_ctx, &destination_key).public_key.key.serialize()[..]);
-                                               Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0)
-                                                             .push_slice(&pubkey_hash160.into_inner())
-                                                             .into_script()
-                                       },
-                                       Err(_) => panic!("Your RNG is busted"),
-                               };
-                               let shutdown_pubkey = match master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(2).unwrap()) {
-                                       Ok(shutdown_key) => ExtendedPubKey::from_private(&secp_ctx, &shutdown_key).public_key.key,
-                                       Err(_) => panic!("Your RNG is busted"),
-                               };
-                               let channel_master_key = master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(3).unwrap()).expect("Your RNG is busted");
-                               let session_master_key = master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(4).unwrap()).expect("Your RNG is busted");
-                               let channel_id_master_key = master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(5).unwrap()).expect("Your RNG is busted");
-
-                               let mut unique_start = Sha256::engine();
-                               unique_start.input(&byte_utils::be64_to_array(starting_time_secs));
-                               unique_start.input(&byte_utils::be32_to_array(starting_time_nanos));
-                               unique_start.input(seed);
-
-                               KeysManager {
-                                       secp_ctx,
-                                       node_secret,
-                                       destination_script,
-                                       shutdown_pubkey,
-                                       channel_master_key,
-                                       channel_child_index: AtomicUsize::new(0),
-                                       session_master_key,
-                                       session_child_index: AtomicUsize::new(0),
-                                       channel_id_master_key,
-                                       channel_id_child_index: AtomicUsize::new(0),
-
-                                       unique_start,
-                                       logger,
-                               }
-                       },
-                       Err(_) => panic!("Your rng is busted"),
-               }
-       }
-}
-
-impl KeysInterface for KeysManager {
-       fn get_node_secret(&self) -> SecretKey {
-               self.node_secret.clone()
-       }
-
-       fn get_destination_script(&self) -> Script {
-               self.destination_script.clone()
-       }
-
-       fn get_shutdown_pubkey(&self) -> PublicKey {
-               self.shutdown_pubkey.clone()
-       }
-
-       fn get_channel_keys(&self, _inbound: bool) -> ChannelKeys {
-               // We only seriously intend to rely on the channel_master_key for true secure
-               // entropy, everything else just ensures uniqueness. We rely on the unique_start (ie
-               // starting_time provided in the constructor) to be unique.
-               let mut sha = self.unique_start.clone();
-
-               let child_ix = self.channel_child_index.fetch_add(1, Ordering::AcqRel);
-               let child_privkey = self.channel_master_key.ckd_priv(&self.secp_ctx, ChildNumber::from_hardened_idx(child_ix as u32).expect("key space exhausted")).expect("Your RNG is busted");
-               sha.input(&child_privkey.private_key.key[..]);
-
-               let seed = Sha256::from_engine(sha).into_inner();
-
-               let commitment_seed = {
-                       let mut sha = Sha256::engine();
-                       sha.input(&seed);
-                       sha.input(&b"commitment seed"[..]);
-                       Sha256::from_engine(sha).into_inner()
-               };
-               macro_rules! key_step {
-                       ($info: expr, $prev_key: expr) => {{
-                               let mut sha = Sha256::engine();
-                               sha.input(&seed);
-                               sha.input(&$prev_key[..]);
-                               sha.input(&$info[..]);
-                               SecretKey::from_slice(&Sha256::from_engine(sha).into_inner()).expect("SHA-256 is busted")
-                       }}
-               }
-               let funding_key = key_step!(b"funding key", commitment_seed);
-               let revocation_base_key = key_step!(b"revocation base key", funding_key);
-               let payment_base_key = key_step!(b"payment base key", revocation_base_key);
-               let delayed_payment_base_key = key_step!(b"delayed payment base key", payment_base_key);
-               let htlc_base_key = key_step!(b"HTLC base key", delayed_payment_base_key);
-
-               ChannelKeys {
-                       funding_key,
-                       revocation_base_key,
-                       payment_base_key,
-                       delayed_payment_base_key,
-                       htlc_base_key,
-                       commitment_seed,
-               }
-       }
-
-       fn get_session_key(&self) -> SecretKey {
-               let mut sha = self.unique_start.clone();
-
-               let child_ix = self.session_child_index.fetch_add(1, Ordering::AcqRel);
-               let child_privkey = self.session_master_key.ckd_priv(&self.secp_ctx, ChildNumber::from_hardened_idx(child_ix as u32).expect("key space exhausted")).expect("Your RNG is busted");
-               sha.input(&child_privkey.private_key.key[..]);
-               SecretKey::from_slice(&Sha256::from_engine(sha).into_inner()).expect("Your RNG is busted")
-       }
-
-       fn get_channel_id(&self) -> [u8; 32] {
-               let mut sha = self.unique_start.clone();
-
-               let child_ix = self.channel_id_child_index.fetch_add(1, Ordering::AcqRel);
-               let child_privkey = self.channel_id_master_key.ckd_priv(&self.secp_ctx, ChildNumber::from_hardened_idx(child_ix as u32).expect("key space exhausted")).expect("Your RNG is busted");
-               sha.input(&child_privkey.private_key.key[..]);
-
-               (Sha256::from_engine(sha).into_inner())
-       }
-}
diff --git a/src/chain/mod.rs b/src/chain/mod.rs
deleted file mode 100644 (file)
index ffa5ed9..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-//! Structs and traits which allow other parts of rust-lightning to interact with the blockchain.
-
-pub mod chaininterface;
-pub mod transaction;
-pub mod keysinterface;
diff --git a/src/chain/transaction.rs b/src/chain/transaction.rs
deleted file mode 100644 (file)
index ce43984..0000000
+++ /dev/null
@@ -1,63 +0,0 @@
-//! Contains simple structs describing parts of transactions on the chain.
-
-use bitcoin_hashes::sha256d::Hash as Sha256dHash;
-use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
-
-/// A reference to a transaction output.
-///
-/// Differs from bitcoin::blockdata::transaction::OutPoint as the index is a u16 instead of u32
-/// due to LN's restrictions on index values. Should reduce (possibly) unsafe conversions this way.
-#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
-pub struct OutPoint {
-       /// The referenced transaction's txid.
-       pub txid: Sha256dHash,
-       /// The index of the referenced output in its transaction's vout.
-       pub index: u16,
-}
-
-impl OutPoint {
-       /// Creates a new `OutPoint` from the txid and the index.
-       pub fn new(txid: Sha256dHash, index: u16) -> OutPoint {
-               OutPoint { txid, index }
-       }
-
-       /// Convert an `OutPoint` to a lightning channel id.
-       pub fn to_channel_id(&self) -> [u8; 32] {
-               let mut res = [0; 32];
-               res[..].copy_from_slice(&self.txid[..]);
-               res[30] ^= ((self.index >> 8) & 0xff) as u8;
-               res[31] ^= ((self.index >> 0) & 0xff) as u8;
-               res
-       }
-
-       /// Converts this OutPoint into the OutPoint field as used by rust-bitcoin
-       pub fn into_bitcoin_outpoint(self) -> BitcoinOutPoint {
-               BitcoinOutPoint {
-                       txid: self.txid,
-                       vout: self.index as u32,
-               }
-       }
-}
-
-#[cfg(test)]
-mod tests {
-       use chain::transaction::OutPoint;
-
-       use bitcoin::blockdata::transaction::Transaction;
-       use bitcoin::consensus::encode;
-
-       use hex;
-
-       #[test]
-       fn test_channel_id_calculation() {
-               let tx: Transaction = encode::deserialize(&hex::decode("020000000001010e0adef48412e4361325ac1c6e36411299ab09d4f083b9d8ddb55fbc06e1b0c00000000000feffffff0220a1070000000000220020f81d95e040bd0a493e38bae27bff52fe2bb58b93b293eb579c01c31b05c5af1dc072cfee54a3000016001434b1d6211af5551905dc2642d05f5b04d25a8fe80247304402207f570e3f0de50546aad25a872e3df059d277e776dda4269fa0d2cc8c2ee6ec9a022054e7fae5ca94d47534c86705857c24ceea3ad51c69dd6051c5850304880fc43a012103cb11a1bacc223d98d91f1946c6752e358a5eb1a1c983b3e6fb15378f453b76bd00000000").unwrap()[..]).unwrap();
-               assert_eq!(&OutPoint {
-                       txid: tx.txid(),
-                       index: 0
-               }.to_channel_id(), &hex::decode("3e88dd7165faf7be58b3c5bb2c9c452aebef682807ea57080f62e6f6e113c25e").unwrap()[..]);
-               assert_eq!(&OutPoint {
-                       txid: tx.txid(),
-                       index: 1
-               }.to_channel_id(), &hex::decode("3e88dd7165faf7be58b3c5bb2c9c452aebef682807ea57080f62e6f6e113c25f").unwrap()[..]);
-       }
-}
diff --git a/src/lib.rs b/src/lib.rs
deleted file mode 100644 (file)
index 68924b5..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-#![crate_name = "lightning"]
-
-//! Rust-Lightning, not Rusty's Lightning!
-//!
-//! A full-featured but also flexible lightning implementation, in library form. This allows the
-//! user (you) to decide how they wish to use it instead of being a fully self-contained daemon.
-//! This means there is no built-in threading/execution environment and it's up to the user to
-//! figure out how best to make networking happen/timers fire/things get written to disk/keys get
-//! generated/etc. This makes it a good candidate for tight integration into an existing wallet
-//! instead of having a rather-separate lightning appendage to a wallet.
-
-#![cfg_attr(not(feature = "fuzztarget"), deny(missing_docs))]
-#![forbid(unsafe_code)]
-
-// In general, rust is absolutely horrid at supporting users doing things like,
-// for example, compiling Rust code for real environments. Disable useless lints
-// that don't do anything but annoy us and cant actually ever be resolved.
-#![allow(bare_trait_objects)]
-#![allow(ellipsis_inclusive_range_patterns)]
-
-extern crate bitcoin;
-extern crate bitcoin_hashes;
-extern crate secp256k1;
-#[cfg(test)] extern crate rand;
-#[cfg(test)] extern crate hex;
-
-#[macro_use]
-pub mod util;
-pub mod chain;
-pub mod ln;
diff --git a/src/ln/chan_utils.rs b/src/ln/chan_utils.rs
deleted file mode 100644 (file)
index 1d8dd30..0000000
+++ /dev/null
@@ -1,256 +0,0 @@
-use bitcoin::blockdata::script::{Script,Builder};
-use bitcoin::blockdata::opcodes;
-use bitcoin::blockdata::transaction::{TxIn,TxOut,OutPoint,Transaction};
-
-use bitcoin_hashes::{Hash, HashEngine};
-use bitcoin_hashes::sha256::Hash as Sha256;
-use bitcoin_hashes::ripemd160::Hash as Ripemd160;
-use bitcoin_hashes::hash160::Hash as Hash160;
-use bitcoin_hashes::sha256d::Hash as Sha256dHash;
-
-use ln::channelmanager::PaymentHash;
-
-use secp256k1::key::{PublicKey,SecretKey};
-use secp256k1::Secp256k1;
-use secp256k1;
-
-pub const HTLC_SUCCESS_TX_WEIGHT: u64 = 703;
-pub const HTLC_TIMEOUT_TX_WEIGHT: u64 = 663;
-
-// Various functions for key derivation and transaction creation for use within channels. Primarily
-// used in Channel and ChannelMonitor.
-
-pub fn build_commitment_secret(commitment_seed: [u8; 32], idx: u64) -> [u8; 32] {
-       let mut res: [u8; 32] = commitment_seed;
-       for i in 0..48 {
-               let bitpos = 47 - i;
-               if idx & (1 << bitpos) == (1 << bitpos) {
-                       res[bitpos / 8] ^= 1 << (bitpos & 7);
-                       res = Sha256::hash(&res).into_inner();
-               }
-       }
-       res
-}
-
-pub fn derive_private_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, per_commitment_point: &PublicKey, base_secret: &SecretKey) -> Result<SecretKey, secp256k1::Error> {
-       let mut sha = Sha256::engine();
-       sha.input(&per_commitment_point.serialize());
-       sha.input(&PublicKey::from_secret_key(&secp_ctx, &base_secret).serialize());
-       let res = Sha256::from_engine(sha).into_inner();
-
-       let mut key = base_secret.clone();
-       key.add_assign(&res)?;
-       Ok(key)
-}
-
-pub fn derive_public_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, per_commitment_point: &PublicKey, base_point: &PublicKey) -> Result<PublicKey, secp256k1::Error> {
-       let mut sha = Sha256::engine();
-       sha.input(&per_commitment_point.serialize());
-       sha.input(&base_point.serialize());
-       let res = Sha256::from_engine(sha).into_inner();
-
-       let hashkey = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&res)?);
-       base_point.combine(&hashkey)
-}
-
-/// Derives a revocation key from its constituent parts
-pub fn derive_private_revocation_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, per_commitment_secret: &SecretKey, revocation_base_secret: &SecretKey) -> Result<SecretKey, secp256k1::Error> {
-       let revocation_base_point = PublicKey::from_secret_key(&secp_ctx, &revocation_base_secret);
-       let per_commitment_point = PublicKey::from_secret_key(&secp_ctx, &per_commitment_secret);
-
-       let rev_append_commit_hash_key = {
-               let mut sha = Sha256::engine();
-               sha.input(&revocation_base_point.serialize());
-               sha.input(&per_commitment_point.serialize());
-
-               Sha256::from_engine(sha).into_inner()
-       };
-       let commit_append_rev_hash_key = {
-               let mut sha = Sha256::engine();
-               sha.input(&per_commitment_point.serialize());
-               sha.input(&revocation_base_point.serialize());
-
-               Sha256::from_engine(sha).into_inner()
-       };
-
-       let mut part_a = revocation_base_secret.clone();
-       part_a.mul_assign(&rev_append_commit_hash_key)?;
-       let mut part_b = per_commitment_secret.clone();
-       part_b.mul_assign(&commit_append_rev_hash_key)?;
-       part_a.add_assign(&part_b[..])?;
-       Ok(part_a)
-}
-
-pub fn derive_public_revocation_key<T: secp256k1::Verification>(secp_ctx: &Secp256k1<T>, per_commitment_point: &PublicKey, revocation_base_point: &PublicKey) -> Result<PublicKey, secp256k1::Error> {
-       let rev_append_commit_hash_key = {
-               let mut sha = Sha256::engine();
-               sha.input(&revocation_base_point.serialize());
-               sha.input(&per_commitment_point.serialize());
-
-               Sha256::from_engine(sha).into_inner()
-       };
-       let commit_append_rev_hash_key = {
-               let mut sha = Sha256::engine();
-               sha.input(&per_commitment_point.serialize());
-               sha.input(&revocation_base_point.serialize());
-
-               Sha256::from_engine(sha).into_inner()
-       };
-
-       let mut part_a = revocation_base_point.clone();
-       part_a.mul_assign(&secp_ctx, &rev_append_commit_hash_key)?;
-       let mut part_b = per_commitment_point.clone();
-       part_b.mul_assign(&secp_ctx, &commit_append_rev_hash_key)?;
-       part_a.combine(&part_b)
-}
-
-pub struct TxCreationKeys {
-       pub per_commitment_point: PublicKey,
-       pub revocation_key: PublicKey,
-       pub a_htlc_key: PublicKey,
-       pub b_htlc_key: PublicKey,
-       pub a_delayed_payment_key: PublicKey,
-       pub b_payment_key: PublicKey,
-}
-
-impl TxCreationKeys {
-       pub fn new<T: secp256k1::Signing + secp256k1::Verification>(secp_ctx: &Secp256k1<T>, per_commitment_point: &PublicKey, a_delayed_payment_base: &PublicKey, a_htlc_base: &PublicKey, b_revocation_base: &PublicKey, b_payment_base: &PublicKey, b_htlc_base: &PublicKey) -> Result<TxCreationKeys, secp256k1::Error> {
-               Ok(TxCreationKeys {
-                       per_commitment_point: per_commitment_point.clone(),
-                       revocation_key: derive_public_revocation_key(&secp_ctx, &per_commitment_point, &b_revocation_base)?,
-                       a_htlc_key: derive_public_key(&secp_ctx, &per_commitment_point, &a_htlc_base)?,
-                       b_htlc_key: derive_public_key(&secp_ctx, &per_commitment_point, &b_htlc_base)?,
-                       a_delayed_payment_key: derive_public_key(&secp_ctx, &per_commitment_point, &a_delayed_payment_base)?,
-                       b_payment_key: derive_public_key(&secp_ctx, &per_commitment_point, &b_payment_base)?,
-               })
-       }
-}
-
-/// Gets the "to_local" output redeemscript, ie the script which is time-locked or spendable by
-/// the revocation key
-pub fn get_revokeable_redeemscript(revocation_key: &PublicKey, to_self_delay: u16, delayed_payment_key: &PublicKey) -> Script {
-       Builder::new().push_opcode(opcodes::all::OP_IF)
-                     .push_slice(&revocation_key.serialize())
-                     .push_opcode(opcodes::all::OP_ELSE)
-                     .push_int(to_self_delay as i64)
-                     .push_opcode(opcodes::all::OP_CSV)
-                     .push_opcode(opcodes::all::OP_DROP)
-                     .push_slice(&delayed_payment_key.serialize())
-                     .push_opcode(opcodes::all::OP_ENDIF)
-                     .push_opcode(opcodes::all::OP_CHECKSIG)
-                     .into_script()
-}
-
-#[derive(Clone, PartialEq)]
-pub struct HTLCOutputInCommitment {
-       pub offered: bool,
-       pub amount_msat: u64,
-       pub cltv_expiry: u32,
-       pub payment_hash: PaymentHash,
-       pub transaction_output_index: Option<u32>,
-}
-
-#[inline]
-pub fn get_htlc_redeemscript_with_explicit_keys(htlc: &HTLCOutputInCommitment, a_htlc_key: &PublicKey, b_htlc_key: &PublicKey, revocation_key: &PublicKey) -> Script {
-       let payment_hash160 = Ripemd160::hash(&htlc.payment_hash.0[..]).into_inner();
-       if htlc.offered {
-               Builder::new().push_opcode(opcodes::all::OP_DUP)
-                             .push_opcode(opcodes::all::OP_HASH160)
-                             .push_slice(&Hash160::hash(&revocation_key.serialize())[..])
-                             .push_opcode(opcodes::all::OP_EQUAL)
-                             .push_opcode(opcodes::all::OP_IF)
-                             .push_opcode(opcodes::all::OP_CHECKSIG)
-                             .push_opcode(opcodes::all::OP_ELSE)
-                             .push_slice(&b_htlc_key.serialize()[..])
-                             .push_opcode(opcodes::all::OP_SWAP)
-                             .push_opcode(opcodes::all::OP_SIZE)
-                             .push_int(32)
-                             .push_opcode(opcodes::all::OP_EQUAL)
-                             .push_opcode(opcodes::all::OP_NOTIF)
-                             .push_opcode(opcodes::all::OP_DROP)
-                             .push_int(2)
-                             .push_opcode(opcodes::all::OP_SWAP)
-                             .push_slice(&a_htlc_key.serialize()[..])
-                             .push_int(2)
-                             .push_opcode(opcodes::all::OP_CHECKMULTISIG)
-                             .push_opcode(opcodes::all::OP_ELSE)
-                             .push_opcode(opcodes::all::OP_HASH160)
-                             .push_slice(&payment_hash160)
-                             .push_opcode(opcodes::all::OP_EQUALVERIFY)
-                             .push_opcode(opcodes::all::OP_CHECKSIG)
-                             .push_opcode(opcodes::all::OP_ENDIF)
-                             .push_opcode(opcodes::all::OP_ENDIF)
-                             .into_script()
-       } else {
-               Builder::new().push_opcode(opcodes::all::OP_DUP)
-                             .push_opcode(opcodes::all::OP_HASH160)
-                             .push_slice(&Hash160::hash(&revocation_key.serialize())[..])
-                             .push_opcode(opcodes::all::OP_EQUAL)
-                             .push_opcode(opcodes::all::OP_IF)
-                             .push_opcode(opcodes::all::OP_CHECKSIG)
-                             .push_opcode(opcodes::all::OP_ELSE)
-                             .push_slice(&b_htlc_key.serialize()[..])
-                             .push_opcode(opcodes::all::OP_SWAP)
-                             .push_opcode(opcodes::all::OP_SIZE)
-                             .push_int(32)
-                             .push_opcode(opcodes::all::OP_EQUAL)
-                             .push_opcode(opcodes::all::OP_IF)
-                             .push_opcode(opcodes::all::OP_HASH160)
-                             .push_slice(&payment_hash160)
-                             .push_opcode(opcodes::all::OP_EQUALVERIFY)
-                             .push_int(2)
-                             .push_opcode(opcodes::all::OP_SWAP)
-                             .push_slice(&a_htlc_key.serialize()[..])
-                             .push_int(2)
-                             .push_opcode(opcodes::all::OP_CHECKMULTISIG)
-                             .push_opcode(opcodes::all::OP_ELSE)
-                             .push_opcode(opcodes::all::OP_DROP)
-                             .push_int(htlc.cltv_expiry as i64)
-                             .push_opcode(opcodes::all::OP_CLTV)
-                             .push_opcode(opcodes::all::OP_DROP)
-                             .push_opcode(opcodes::all::OP_CHECKSIG)
-                             .push_opcode(opcodes::all::OP_ENDIF)
-                             .push_opcode(opcodes::all::OP_ENDIF)
-                             .into_script()
-       }
-}
-
-/// note here that 'a_revocation_key' is generated using b_revocation_basepoint and a's
-/// commitment secret. 'htlc' does *not* need to have its previous_output_index filled.
-#[inline]
-pub fn get_htlc_redeemscript(htlc: &HTLCOutputInCommitment, keys: &TxCreationKeys) -> Script {
-       get_htlc_redeemscript_with_explicit_keys(htlc, &keys.a_htlc_key, &keys.b_htlc_key, &keys.revocation_key)
-}
-
-/// panics if htlc.transaction_output_index.is_none()!
-pub fn build_htlc_transaction(prev_hash: &Sha256dHash, feerate_per_kw: u64, to_self_delay: u16, htlc: &HTLCOutputInCommitment, a_delayed_payment_key: &PublicKey, revocation_key: &PublicKey) -> Transaction {
-       let mut txins: Vec<TxIn> = Vec::new();
-       txins.push(TxIn {
-               previous_output: OutPoint {
-                       txid: prev_hash.clone(),
-                       vout: htlc.transaction_output_index.expect("Can't build an HTLC transaction for a dust output"),
-               },
-               script_sig: Script::new(),
-               sequence: 0,
-               witness: Vec::new(),
-       });
-
-       let total_fee = if htlc.offered {
-                       feerate_per_kw * HTLC_TIMEOUT_TX_WEIGHT / 1000
-               } else {
-                       feerate_per_kw * HTLC_SUCCESS_TX_WEIGHT / 1000
-               };
-
-       let mut txouts: Vec<TxOut> = Vec::new();
-       txouts.push(TxOut {
-               script_pubkey: get_revokeable_redeemscript(revocation_key, to_self_delay, a_delayed_payment_key).to_v0_p2wsh(),
-               value: htlc.amount_msat / 1000 - total_fee //TODO: BOLT 3 does not specify if we should add amount_msat before dividing or if we should divide by 1000 before subtracting (as we do here)
-       });
-
-       Transaction {
-               version: 2,
-               lock_time: if htlc.offered { htlc.cltv_expiry } else { 0 },
-               input: txins,
-               output: txouts,
-       }
-}
diff --git a/src/ln/chanmon_update_fail_tests.rs b/src/ln/chanmon_update_fail_tests.rs
deleted file mode 100644 (file)
index 137d983..0000000
+++ /dev/null
@@ -1,1684 +0,0 @@
-//! Functional tests which test the correct handling of ChannelMonitorUpdateErr returns from
-//! monitor updates.
-//! There are a bunch of these as their handling is relatively error-prone so they are split out
-//! here. See also the chanmon_fail_consistency fuzz test.
-
-use ln::channelmanager::{RAACommitmentOrder, PaymentPreimage, PaymentHash};
-use ln::channelmonitor::ChannelMonitorUpdateErr;
-use ln::msgs;
-use ln::msgs::{ChannelMessageHandler, LocalFeatures, RoutingMessageHandler};
-use util::events::{Event, EventsProvider, MessageSendEvent, MessageSendEventsProvider};
-use util::errors::APIError;
-
-use bitcoin_hashes::sha256::Hash as Sha256;
-use bitcoin_hashes::Hash;
-
-use ln::functional_test_utils::*;
-
-#[test]
-fn test_simple_monitor_permanent_update_fail() {
-       // Test that we handle a simple permanent monitor update failure
-       let mut nodes = create_network(2, &[None, None]);
-       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
-       let (_, payment_hash_1) = get_payment_preimage_hash!(nodes[0]);
-
-       *nodes[0].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::PermanentFailure);
-       if let Err(APIError::ChannelUnavailable {..}) = nodes[0].node.send_payment(route, payment_hash_1) {} else { panic!(); }
-       check_added_monitors!(nodes[0], 1);
-
-       let events_1 = nodes[0].node.get_and_clear_pending_msg_events();
-       assert_eq!(events_1.len(), 2);
-       match events_1[0] {
-               MessageSendEvent::BroadcastChannelUpdate { .. } => {},
-               _ => panic!("Unexpected event"),
-       };
-       match events_1[1] {
-               MessageSendEvent::HandleError { node_id, .. } => assert_eq!(node_id, nodes[1].node.get_our_node_id()),
-               _ => panic!("Unexpected event"),
-       };
-
-       // TODO: Once we hit the chain with the failure transaction we should check that we get a
-       // PaymentFailed event
-
-       assert_eq!(nodes[0].node.list_channels().len(), 0);
-}
-
-fn do_test_simple_monitor_temporary_update_fail(disconnect: bool) {
-       // Test that we can recover from a simple temporary monitor update failure optionally with
-       // a disconnect in between
-       let mut nodes = create_network(2, &[None, None]);
-       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
-       let (payment_preimage_1, payment_hash_1) = get_payment_preimage_hash!(nodes[0]);
-
-       *nodes[0].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
-       if let Err(APIError::MonitorUpdateFailed) = nodes[0].node.send_payment(route.clone(), payment_hash_1) {} else { panic!(); }
-       check_added_monitors!(nodes[0], 1);
-
-       assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
-       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
-       assert_eq!(nodes[0].node.list_channels().len(), 1);
-
-       if disconnect {
-               nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
-               nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
-               reconnect_nodes(&nodes[0], &nodes[1], (true, true), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
-       }
-
-       *nodes[0].chan_monitor.update_ret.lock().unwrap() = Ok(());
-       nodes[0].node.test_restore_channel_monitor();
-       check_added_monitors!(nodes[0], 1);
-
-       let mut events_2 = nodes[0].node.get_and_clear_pending_msg_events();
-       assert_eq!(events_2.len(), 1);
-       let payment_event = SendEvent::from_event(events_2.pop().unwrap());
-       assert_eq!(payment_event.node_id, nodes[1].node.get_our_node_id());
-       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
-       commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
-
-       expect_pending_htlcs_forwardable!(nodes[1]);
-
-       let events_3 = nodes[1].node.get_and_clear_pending_events();
-       assert_eq!(events_3.len(), 1);
-       match events_3[0] {
-               Event::PaymentReceived { ref payment_hash, amt } => {
-                       assert_eq!(payment_hash_1, *payment_hash);
-                       assert_eq!(amt, 1000000);
-               },
-               _ => panic!("Unexpected event"),
-       }
-
-       claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1);
-
-       // Now set it to failed again...
-       let (_, payment_hash_2) = get_payment_preimage_hash!(nodes[0]);
-       *nodes[0].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
-       if let Err(APIError::MonitorUpdateFailed) = nodes[0].node.send_payment(route, payment_hash_2) {} else { panic!(); }
-       check_added_monitors!(nodes[0], 1);
-
-       assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
-       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
-       assert_eq!(nodes[0].node.list_channels().len(), 1);
-
-       if disconnect {
-               nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
-               nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
-               reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
-       }
-
-       // ...and make sure we can force-close a TemporaryFailure channel with a PermanentFailure
-       *nodes[0].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::PermanentFailure);
-       nodes[0].node.test_restore_channel_monitor();
-       check_added_monitors!(nodes[0], 1);
-       check_closed_broadcast!(nodes[0]);
-
-       // TODO: Once we hit the chain with the failure transaction we should check that we get a
-       // PaymentFailed event
-
-       assert_eq!(nodes[0].node.list_channels().len(), 0);
-}
-
-#[test]
-fn test_simple_monitor_temporary_update_fail() {
-       do_test_simple_monitor_temporary_update_fail(false);
-       do_test_simple_monitor_temporary_update_fail(true);
-}
-
-fn do_test_monitor_temporary_update_fail(disconnect_count: usize) {
-       let disconnect_flags = 8 | 16;
-
-       // Test that we can recover from a temporary monitor update failure with some in-flight
-       // HTLCs going on at the same time potentially with some disconnection thrown in.
-       // * First we route a payment, then get a temporary monitor update failure when trying to
-       //   route a second payment. We then claim the first payment.
-       // * If disconnect_count is set, we will disconnect at this point (which is likely as
-       //   TemporaryFailure likely indicates net disconnect which resulted in failing to update
-       //   the ChannelMonitor on a watchtower).
-       // * If !(disconnect_count & 16) we deliver a update_fulfill_htlc/CS for the first payment
-       //   immediately, otherwise we wait disconnect and deliver them via the reconnect
-       //   channel_reestablish processing (ie disconnect_count & 16 makes no sense if
-       //   disconnect_count & !disconnect_flags is 0).
-       // * We then update the channel monitor, reconnecting if disconnect_count is set and walk
-       //   through message sending, potentially disconnect/reconnecting multiple times based on
-       //   disconnect_count, to get the update_fulfill_htlc through.
-       // * We then walk through more message exchanges to get the original update_add_htlc
-       //   through, swapping message ordering based on disconnect_count & 8 and optionally
-       //   disconnect/reconnecting based on disconnect_count.
-       let mut nodes = create_network(2, &[None, None]);
-       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       let (payment_preimage_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
-
-       // Now try to send a second payment which will fail to send
-       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
-       let (payment_preimage_2, payment_hash_2) = get_payment_preimage_hash!(nodes[0]);
-
-       *nodes[0].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
-       if let Err(APIError::MonitorUpdateFailed) = nodes[0].node.send_payment(route.clone(), payment_hash_2) {} else { panic!(); }
-       check_added_monitors!(nodes[0], 1);
-
-       assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
-       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
-       assert_eq!(nodes[0].node.list_channels().len(), 1);
-
-       // Claim the previous payment, which will result in a update_fulfill_htlc/CS from nodes[1]
-       // but nodes[0] won't respond since it is frozen.
-       assert!(nodes[1].node.claim_funds(payment_preimage_1));
-       check_added_monitors!(nodes[1], 1);
-       let events_2 = nodes[1].node.get_and_clear_pending_msg_events();
-       assert_eq!(events_2.len(), 1);
-       let (bs_initial_fulfill, bs_initial_commitment_signed) = match events_2[0] {
-               MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => {
-                       assert_eq!(*node_id, nodes[0].node.get_our_node_id());
-                       assert!(update_add_htlcs.is_empty());
-                       assert_eq!(update_fulfill_htlcs.len(), 1);
-                       assert!(update_fail_htlcs.is_empty());
-                       assert!(update_fail_malformed_htlcs.is_empty());
-                       assert!(update_fee.is_none());
-
-                       if (disconnect_count & 16) == 0 {
-                               nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_htlcs[0]).unwrap();
-                               let events_3 = nodes[0].node.get_and_clear_pending_events();
-                               assert_eq!(events_3.len(), 1);
-                               match events_3[0] {
-                                       Event::PaymentSent { ref payment_preimage } => {
-                                               assert_eq!(*payment_preimage, payment_preimage_1);
-                                       },
-                                       _ => panic!("Unexpected event"),
-                               }
-
-                               if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::IgnoreError) }) = nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), commitment_signed) {
-                                       assert_eq!(err, "Previous monitor update failure prevented generation of RAA");
-                               } else { panic!(); }
-                       }
-
-                       (update_fulfill_htlcs[0].clone(), commitment_signed.clone())
-               },
-               _ => panic!("Unexpected event"),
-       };
-
-       if disconnect_count & !disconnect_flags > 0 {
-               nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
-               nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
-       }
-
-       // Now fix monitor updating...
-       *nodes[0].chan_monitor.update_ret.lock().unwrap() = Ok(());
-       nodes[0].node.test_restore_channel_monitor();
-       check_added_monitors!(nodes[0], 1);
-
-       macro_rules! disconnect_reconnect_peers { () => { {
-               nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
-               nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
-
-               nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id());
-               let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
-               assert_eq!(reestablish_1.len(), 1);
-               nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id());
-               let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
-               assert_eq!(reestablish_2.len(), 1);
-
-               nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]).unwrap();
-               let as_resp = handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
-               nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]).unwrap();
-               let bs_resp = handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
-
-               assert!(as_resp.0.is_none());
-               assert!(bs_resp.0.is_none());
-
-               (reestablish_1, reestablish_2, as_resp, bs_resp)
-       } } }
-
-       let (payment_event, initial_revoke_and_ack) = if disconnect_count & !disconnect_flags > 0 {
-               assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
-               assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
-
-               nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id());
-               let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
-               assert_eq!(reestablish_1.len(), 1);
-               nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id());
-               let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
-               assert_eq!(reestablish_2.len(), 1);
-
-               nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]).unwrap();
-               check_added_monitors!(nodes[0], 0);
-               let mut as_resp = handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
-               nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]).unwrap();
-               check_added_monitors!(nodes[1], 0);
-               let mut bs_resp = handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
-
-               assert!(as_resp.0.is_none());
-               assert!(bs_resp.0.is_none());
-
-               assert!(bs_resp.1.is_none());
-               if (disconnect_count & 16) == 0 {
-                       assert!(bs_resp.2.is_none());
-
-                       assert!(as_resp.1.is_some());
-                       assert!(as_resp.2.is_some());
-                       assert!(as_resp.3 == RAACommitmentOrder::CommitmentFirst);
-               } else {
-                       assert!(bs_resp.2.as_ref().unwrap().update_add_htlcs.is_empty());
-                       assert!(bs_resp.2.as_ref().unwrap().update_fail_htlcs.is_empty());
-                       assert!(bs_resp.2.as_ref().unwrap().update_fail_malformed_htlcs.is_empty());
-                       assert!(bs_resp.2.as_ref().unwrap().update_fee.is_none());
-                       assert!(bs_resp.2.as_ref().unwrap().update_fulfill_htlcs == vec![bs_initial_fulfill]);
-                       assert!(bs_resp.2.as_ref().unwrap().commitment_signed == bs_initial_commitment_signed);
-
-                       assert!(as_resp.1.is_none());
-
-                       nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_resp.2.as_ref().unwrap().update_fulfill_htlcs[0]).unwrap();
-                       let events_3 = nodes[0].node.get_and_clear_pending_events();
-                       assert_eq!(events_3.len(), 1);
-                       match events_3[0] {
-                               Event::PaymentSent { ref payment_preimage } => {
-                                       assert_eq!(*payment_preimage, payment_preimage_1);
-                               },
-                               _ => panic!("Unexpected event"),
-                       }
-
-                       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_resp.2.as_ref().unwrap().commitment_signed).unwrap();
-                       let as_resp_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
-                       // No commitment_signed so get_event_msg's assert(len == 1) passes
-                       check_added_monitors!(nodes[0], 1);
-
-                       as_resp.1 = Some(as_resp_raa);
-                       bs_resp.2 = None;
-               }
-
-               if disconnect_count & !disconnect_flags > 1 {
-                       let (second_reestablish_1, second_reestablish_2, second_as_resp, second_bs_resp) = disconnect_reconnect_peers!();
-
-                       if (disconnect_count & 16) == 0 {
-                               assert!(reestablish_1 == second_reestablish_1);
-                               assert!(reestablish_2 == second_reestablish_2);
-                       }
-                       assert!(as_resp == second_as_resp);
-                       assert!(bs_resp == second_bs_resp);
-               }
-
-               (SendEvent::from_commitment_update(nodes[1].node.get_our_node_id(), as_resp.2.unwrap()), as_resp.1.unwrap())
-       } else {
-               let mut events_4 = nodes[0].node.get_and_clear_pending_msg_events();
-               assert_eq!(events_4.len(), 2);
-               (SendEvent::from_event(events_4.remove(0)), match events_4[0] {
-                       MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
-                               assert_eq!(*node_id, nodes[1].node.get_our_node_id());
-                               msg.clone()
-                       },
-                       _ => panic!("Unexpected event"),
-               })
-       };
-
-       assert_eq!(payment_event.node_id, nodes[1].node.get_our_node_id());
-
-       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
-       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg).unwrap();
-       let bs_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
-       // nodes[1] is awaiting an RAA from nodes[0] still so get_event_msg's assert(len == 1) passes
-       check_added_monitors!(nodes[1], 1);
-
-       if disconnect_count & !disconnect_flags > 2 {
-               let (_, _, as_resp, bs_resp) = disconnect_reconnect_peers!();
-
-               assert!(as_resp.1.unwrap() == initial_revoke_and_ack);
-               assert!(bs_resp.1.unwrap() == bs_revoke_and_ack);
-
-               assert!(as_resp.2.is_none());
-               assert!(bs_resp.2.is_none());
-       }
-
-       let as_commitment_update;
-       let bs_second_commitment_update;
-
-       macro_rules! handle_bs_raa { () => {
-               nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
-               as_commitment_update = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
-               assert!(as_commitment_update.update_add_htlcs.is_empty());
-               assert!(as_commitment_update.update_fulfill_htlcs.is_empty());
-               assert!(as_commitment_update.update_fail_htlcs.is_empty());
-               assert!(as_commitment_update.update_fail_malformed_htlcs.is_empty());
-               assert!(as_commitment_update.update_fee.is_none());
-               check_added_monitors!(nodes[0], 1);
-       } }
-
-       macro_rules! handle_initial_raa { () => {
-               nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &initial_revoke_and_ack).unwrap();
-               bs_second_commitment_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-               assert!(bs_second_commitment_update.update_add_htlcs.is_empty());
-               assert!(bs_second_commitment_update.update_fulfill_htlcs.is_empty());
-               assert!(bs_second_commitment_update.update_fail_htlcs.is_empty());
-               assert!(bs_second_commitment_update.update_fail_malformed_htlcs.is_empty());
-               assert!(bs_second_commitment_update.update_fee.is_none());
-               check_added_monitors!(nodes[1], 1);
-       } }
-
-       if (disconnect_count & 8) == 0 {
-               handle_bs_raa!();
-
-               if disconnect_count & !disconnect_flags > 3 {
-                       let (_, _, as_resp, bs_resp) = disconnect_reconnect_peers!();
-
-                       assert!(as_resp.1.unwrap() == initial_revoke_and_ack);
-                       assert!(bs_resp.1.is_none());
-
-                       assert!(as_resp.2.unwrap() == as_commitment_update);
-                       assert!(bs_resp.2.is_none());
-
-                       assert!(as_resp.3 == RAACommitmentOrder::RevokeAndACKFirst);
-               }
-
-               handle_initial_raa!();
-
-               if disconnect_count & !disconnect_flags > 4 {
-                       let (_, _, as_resp, bs_resp) = disconnect_reconnect_peers!();
-
-                       assert!(as_resp.1.is_none());
-                       assert!(bs_resp.1.is_none());
-
-                       assert!(as_resp.2.unwrap() == as_commitment_update);
-                       assert!(bs_resp.2.unwrap() == bs_second_commitment_update);
-               }
-       } else {
-               handle_initial_raa!();
-
-               if disconnect_count & !disconnect_flags > 3 {
-                       let (_, _, as_resp, bs_resp) = disconnect_reconnect_peers!();
-
-                       assert!(as_resp.1.is_none());
-                       assert!(bs_resp.1.unwrap() == bs_revoke_and_ack);
-
-                       assert!(as_resp.2.is_none());
-                       assert!(bs_resp.2.unwrap() == bs_second_commitment_update);
-
-                       assert!(bs_resp.3 == RAACommitmentOrder::RevokeAndACKFirst);
-               }
-
-               handle_bs_raa!();
-
-               if disconnect_count & !disconnect_flags > 4 {
-                       let (_, _, as_resp, bs_resp) = disconnect_reconnect_peers!();
-
-                       assert!(as_resp.1.is_none());
-                       assert!(bs_resp.1.is_none());
-
-                       assert!(as_resp.2.unwrap() == as_commitment_update);
-                       assert!(bs_resp.2.unwrap() == bs_second_commitment_update);
-               }
-       }
-
-       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_second_commitment_update.commitment_signed).unwrap();
-       let as_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
-       // No commitment_signed so get_event_msg's assert(len == 1) passes
-       check_added_monitors!(nodes[0], 1);
-
-       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_commitment_update.commitment_signed).unwrap();
-       let bs_second_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
-       // No commitment_signed so get_event_msg's assert(len == 1) passes
-       check_added_monitors!(nodes[1], 1);
-
-       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_and_ack).unwrap();
-       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-       check_added_monitors!(nodes[1], 1);
-
-       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_revoke_and_ack).unwrap();
-       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
-       check_added_monitors!(nodes[0], 1);
-
-       expect_pending_htlcs_forwardable!(nodes[1]);
-
-       let events_5 = nodes[1].node.get_and_clear_pending_events();
-       assert_eq!(events_5.len(), 1);
-       match events_5[0] {
-               Event::PaymentReceived { ref payment_hash, amt } => {
-                       assert_eq!(payment_hash_2, *payment_hash);
-                       assert_eq!(amt, 1000000);
-               },
-               _ => panic!("Unexpected event"),
-       }
-
-       claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
-}
-
-#[test]
-fn test_monitor_temporary_update_fail_a() {
-       do_test_monitor_temporary_update_fail(0);
-       do_test_monitor_temporary_update_fail(1);
-       do_test_monitor_temporary_update_fail(2);
-       do_test_monitor_temporary_update_fail(3);
-       do_test_monitor_temporary_update_fail(4);
-       do_test_monitor_temporary_update_fail(5);
-}
-
-#[test]
-fn test_monitor_temporary_update_fail_b() {
-       do_test_monitor_temporary_update_fail(2 | 8);
-       do_test_monitor_temporary_update_fail(3 | 8);
-       do_test_monitor_temporary_update_fail(4 | 8);
-       do_test_monitor_temporary_update_fail(5 | 8);
-}
-
-#[test]
-fn test_monitor_temporary_update_fail_c() {
-       do_test_monitor_temporary_update_fail(1 | 16);
-       do_test_monitor_temporary_update_fail(2 | 16);
-       do_test_monitor_temporary_update_fail(3 | 16);
-       do_test_monitor_temporary_update_fail(2 | 8 | 16);
-       do_test_monitor_temporary_update_fail(3 | 8 | 16);
-}
-
-#[test]
-fn test_monitor_update_fail_cs() {
-       // Tests handling of a monitor update failure when processing an incoming commitment_signed
-       let mut nodes = create_network(2, &[None, None]);
-       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
-       let (payment_preimage, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
-       nodes[0].node.send_payment(route, our_payment_hash).unwrap();
-       check_added_monitors!(nodes[0], 1);
-
-       let send_event = SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0));
-       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &send_event.msgs[0]).unwrap();
-
-       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
-       if let msgs::HandleError { err, action: Some(msgs::ErrorAction::IgnoreError) } = nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &send_event.commitment_msg).unwrap_err() {
-               assert_eq!(err, "Failed to update ChannelMonitor");
-       } else { panic!(); }
-       check_added_monitors!(nodes[1], 1);
-       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-
-       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Ok(());
-       nodes[1].node.test_restore_channel_monitor();
-       check_added_monitors!(nodes[1], 1);
-       let responses = nodes[1].node.get_and_clear_pending_msg_events();
-       assert_eq!(responses.len(), 2);
-
-       match responses[0] {
-               MessageSendEvent::SendRevokeAndACK { ref msg, ref node_id } => {
-                       assert_eq!(*node_id, nodes[0].node.get_our_node_id());
-                       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &msg).unwrap();
-                       check_added_monitors!(nodes[0], 1);
-               },
-               _ => panic!("Unexpected event"),
-       }
-       match responses[1] {
-               MessageSendEvent::UpdateHTLCs { ref updates, ref node_id } => {
-                       assert!(updates.update_add_htlcs.is_empty());
-                       assert!(updates.update_fulfill_htlcs.is_empty());
-                       assert!(updates.update_fail_htlcs.is_empty());
-                       assert!(updates.update_fail_malformed_htlcs.is_empty());
-                       assert!(updates.update_fee.is_none());
-                       assert_eq!(*node_id, nodes[0].node.get_our_node_id());
-
-                       *nodes[0].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
-                       if let msgs::HandleError { err, action: Some(msgs::ErrorAction::IgnoreError) } = nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &updates.commitment_signed).unwrap_err() {
-                               assert_eq!(err, "Failed to update ChannelMonitor");
-                       } else { panic!(); }
-                       check_added_monitors!(nodes[0], 1);
-                       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
-               },
-               _ => panic!("Unexpected event"),
-       }
-
-       *nodes[0].chan_monitor.update_ret.lock().unwrap() = Ok(());
-       nodes[0].node.test_restore_channel_monitor();
-       check_added_monitors!(nodes[0], 1);
-
-       let final_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
-       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &final_raa).unwrap();
-       check_added_monitors!(nodes[1], 1);
-
-       expect_pending_htlcs_forwardable!(nodes[1]);
-
-       let events = nodes[1].node.get_and_clear_pending_events();
-       assert_eq!(events.len(), 1);
-       match events[0] {
-               Event::PaymentReceived { payment_hash, amt } => {
-                       assert_eq!(payment_hash, our_payment_hash);
-                       assert_eq!(amt, 1000000);
-               },
-               _ => panic!("Unexpected event"),
-       };
-
-       claim_payment(&nodes[0], &[&nodes[1]], payment_preimage);
-}
-
-#[test]
-fn test_monitor_update_fail_no_rebroadcast() {
-       // Tests handling of a monitor update failure when no message rebroadcasting on
-       // test_restore_channel_monitor() is required. Backported from
-       // chanmon_fail_consistency fuzz tests.
-       let mut nodes = create_network(2, &[None, None]);
-       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
-       let (payment_preimage_1, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
-       nodes[0].node.send_payment(route, our_payment_hash).unwrap();
-       check_added_monitors!(nodes[0], 1);
-
-       let send_event = SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0));
-       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &send_event.msgs[0]).unwrap();
-       let bs_raa = commitment_signed_dance!(nodes[1], nodes[0], send_event.commitment_msg, false, true, false, true);
-
-       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
-       if let msgs::HandleError { err, action: Some(msgs::ErrorAction::IgnoreError) } = nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &bs_raa).unwrap_err() {
-               assert_eq!(err, "Failed to update ChannelMonitor");
-       } else { panic!(); }
-       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-       assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
-       check_added_monitors!(nodes[1], 1);
-
-       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Ok(());
-       nodes[1].node.test_restore_channel_monitor();
-       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-       check_added_monitors!(nodes[1], 1);
-       expect_pending_htlcs_forwardable!(nodes[1]);
-
-       let events = nodes[1].node.get_and_clear_pending_events();
-       assert_eq!(events.len(), 1);
-       match events[0] {
-               Event::PaymentReceived { payment_hash, .. } => {
-                       assert_eq!(payment_hash, our_payment_hash);
-               },
-               _ => panic!("Unexpected event"),
-       }
-
-       claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1);
-}
-
-#[test]
-fn test_monitor_update_raa_while_paused() {
-       // Tests handling of an RAA while monitor updating has already been marked failed.
-       // Backported from chanmon_fail_consistency fuzz tests as this used to be broken.
-       let mut nodes = create_network(2, &[None, None]);
-       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       send_payment(&nodes[0], &[&nodes[1]], 5000000);
-
-       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
-       let (payment_preimage_1, our_payment_hash_1) = get_payment_preimage_hash!(nodes[0]);
-       nodes[0].node.send_payment(route, our_payment_hash_1).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       let send_event_1 = SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0));
-
-       let route = nodes[1].router.get_route(&nodes[0].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
-       let (payment_preimage_2, our_payment_hash_2) = get_payment_preimage_hash!(nodes[0]);
-       nodes[1].node.send_payment(route, our_payment_hash_2).unwrap();
-       check_added_monitors!(nodes[1], 1);
-       let send_event_2 = SendEvent::from_event(nodes[1].node.get_and_clear_pending_msg_events().remove(0));
-
-       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &send_event_1.msgs[0]).unwrap();
-       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &send_event_1.commitment_msg).unwrap();
-       check_added_monitors!(nodes[1], 1);
-       let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
-
-       *nodes[0].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
-       nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &send_event_2.msgs[0]).unwrap();
-       if let msgs::HandleError { err, action: Some(msgs::ErrorAction::IgnoreError) } = nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &send_event_2.commitment_msg).unwrap_err() {
-               assert_eq!(err, "Failed to update ChannelMonitor");
-       } else { panic!(); }
-       check_added_monitors!(nodes[0], 1);
-
-       if let msgs::HandleError { err, action: Some(msgs::ErrorAction::IgnoreError) } = nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa).unwrap_err() {
-               assert_eq!(err, "Previous monitor update failure prevented responses to RAA");
-       } else { panic!(); }
-       check_added_monitors!(nodes[0], 1);
-
-       *nodes[0].chan_monitor.update_ret.lock().unwrap() = Ok(());
-       nodes[0].node.test_restore_channel_monitor();
-       check_added_monitors!(nodes[0], 1);
-
-       let as_update_raa = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
-       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_update_raa.0).unwrap();
-       check_added_monitors!(nodes[1], 1);
-       let bs_cs = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-
-       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_update_raa.1).unwrap();
-       check_added_monitors!(nodes[1], 1);
-       let bs_second_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
-
-       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_cs.commitment_signed).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       let as_second_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
-
-       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_raa).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       expect_pending_htlcs_forwardable!(nodes[0]);
-       expect_payment_received!(nodes[0], our_payment_hash_2, 1000000);
-
-       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_second_raa).unwrap();
-       check_added_monitors!(nodes[1], 1);
-       expect_pending_htlcs_forwardable!(nodes[1]);
-       expect_payment_received!(nodes[1], our_payment_hash_1, 1000000);
-
-       claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1);
-       claim_payment(&nodes[1], &[&nodes[0]], payment_preimage_2);
-}
-
-fn do_test_monitor_update_fail_raa(test_ignore_second_cs: bool) {
-       // Tests handling of a monitor update failure when processing an incoming RAA
-       let mut nodes = create_network(3, &[None, None, None]);
-       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-       let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
-
-       // Rebalance a bit so that we can send backwards from 2 to 1.
-       send_payment(&nodes[0], &[&nodes[1], &nodes[2]], 5000000);
-
-       // Route a first payment that we'll fail backwards
-       let (_, payment_hash_1) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1000000);
-
-       // Fail the payment backwards, failing the monitor update on nodes[1]'s receipt of the RAA
-       assert!(nodes[2].node.fail_htlc_backwards(&payment_hash_1));
-       expect_pending_htlcs_forwardable!(nodes[2]);
-       check_added_monitors!(nodes[2], 1);
-
-       let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
-       assert!(updates.update_add_htlcs.is_empty());
-       assert!(updates.update_fulfill_htlcs.is_empty());
-       assert_eq!(updates.update_fail_htlcs.len(), 1);
-       assert!(updates.update_fail_malformed_htlcs.is_empty());
-       assert!(updates.update_fee.is_none());
-       nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]).unwrap();
-
-       let bs_revoke_and_ack = commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false, true, false, true);
-       check_added_monitors!(nodes[0], 0);
-
-       // While the second channel is AwaitingRAA, forward a second payment to get it into the
-       // holding cell.
-       let (payment_preimage_2, payment_hash_2) = get_payment_preimage_hash!(nodes[0]);
-       let route = nodes[0].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
-       nodes[0].node.send_payment(route, payment_hash_2).unwrap();
-       check_added_monitors!(nodes[0], 1);
-
-       let mut send_event = SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0));
-       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &send_event.msgs[0]).unwrap();
-       commitment_signed_dance!(nodes[1], nodes[0], send_event.commitment_msg, false);
-
-       expect_pending_htlcs_forwardable!(nodes[1]);
-       check_added_monitors!(nodes[1], 0);
-       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-
-       // Now fail monitor updating.
-       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
-       if let msgs::HandleError { err, action: Some(msgs::ErrorAction::IgnoreError) } = nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_revoke_and_ack).unwrap_err() {
-               assert_eq!(err, "Failed to update ChannelMonitor");
-       } else { panic!(); }
-       assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
-       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-       check_added_monitors!(nodes[1], 1);
-
-       // Attempt to forward a third payment but fail due to the second channel being unavailable
-       // for forwarding.
-
-       let (_, payment_hash_3) = get_payment_preimage_hash!(nodes[0]);
-       let route = nodes[0].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
-       nodes[0].node.send_payment(route, payment_hash_3).unwrap();
-       check_added_monitors!(nodes[0], 1);
-
-       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Ok(()); // We succeed in updating the monitor for the first channel
-       send_event = SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0));
-       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &send_event.msgs[0]).unwrap();
-       commitment_signed_dance!(nodes[1], nodes[0], send_event.commitment_msg, false, true);
-       check_added_monitors!(nodes[1], 0);
-
-       let mut events_2 = nodes[1].node.get_and_clear_pending_msg_events();
-       assert_eq!(events_2.len(), 1);
-       match events_2.remove(0) {
-               MessageSendEvent::UpdateHTLCs { node_id, updates } => {
-                       assert_eq!(node_id, nodes[0].node.get_our_node_id());
-                       assert!(updates.update_fulfill_htlcs.is_empty());
-                       assert_eq!(updates.update_fail_htlcs.len(), 1);
-                       assert!(updates.update_fail_malformed_htlcs.is_empty());
-                       assert!(updates.update_add_htlcs.is_empty());
-                       assert!(updates.update_fee.is_none());
-
-                       nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]).unwrap();
-                       commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, false, true);
-
-                       let msg_events = nodes[0].node.get_and_clear_pending_msg_events();
-                       assert_eq!(msg_events.len(), 1);
-                       match msg_events[0] {
-                               MessageSendEvent::PaymentFailureNetworkUpdate { update: msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg }} => {
-                                       assert_eq!(msg.contents.short_channel_id, chan_2.0.contents.short_channel_id);
-                                       assert_eq!(msg.contents.flags & 2, 2); // temp disabled
-                               },
-                               _ => panic!("Unexpected event"),
-                       }
-
-                       let events = nodes[0].node.get_and_clear_pending_events();
-                       assert_eq!(events.len(), 1);
-                       if let Event::PaymentFailed { payment_hash, rejected_by_dest, .. } = events[0] {
-                               assert_eq!(payment_hash, payment_hash_3);
-                               assert!(!rejected_by_dest);
-                       } else { panic!("Unexpected event!"); }
-               },
-               _ => panic!("Unexpected event type!"),
-       };
-
-       let (payment_preimage_4, payment_hash_4) = if test_ignore_second_cs {
-               // Try to route another payment backwards from 2 to make sure 1 holds off on responding
-               let (payment_preimage_4, payment_hash_4) = get_payment_preimage_hash!(nodes[0]);
-               let route = nodes[2].router.get_route(&nodes[0].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
-               nodes[2].node.send_payment(route, payment_hash_4).unwrap();
-               check_added_monitors!(nodes[2], 1);
-
-               send_event = SendEvent::from_event(nodes[2].node.get_and_clear_pending_msg_events().remove(0));
-               nodes[1].node.handle_update_add_htlc(&nodes[2].node.get_our_node_id(), &send_event.msgs[0]).unwrap();
-               if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::IgnoreError) }) = nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &send_event.commitment_msg) {
-                       assert_eq!(err, "Previous monitor update failure prevented generation of RAA");
-               } else { panic!(); }
-               assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-               assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
-               (Some(payment_preimage_4), Some(payment_hash_4))
-       } else { (None, None) };
-
-       // Restore monitor updating, ensuring we immediately get a fail-back update and a
-       // update_add update.
-       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Ok(());
-       nodes[1].node.test_restore_channel_monitor();
-       check_added_monitors!(nodes[1], 1);
-       expect_pending_htlcs_forwardable!(nodes[1]);
-       check_added_monitors!(nodes[1], 1);
-
-       let mut events_3 = nodes[1].node.get_and_clear_pending_msg_events();
-       if test_ignore_second_cs {
-               assert_eq!(events_3.len(), 3);
-       } else {
-               assert_eq!(events_3.len(), 2);
-       }
-
-       // Note that the ordering of the events for different nodes is non-prescriptive, though the
-       // ordering of the two events that both go to nodes[2] have to stay in the same order.
-       let messages_a = match events_3.pop().unwrap() {
-               MessageSendEvent::UpdateHTLCs { node_id, mut updates } => {
-                       assert_eq!(node_id, nodes[0].node.get_our_node_id());
-                       assert!(updates.update_fulfill_htlcs.is_empty());
-                       assert_eq!(updates.update_fail_htlcs.len(), 1);
-                       assert!(updates.update_fail_malformed_htlcs.is_empty());
-                       assert!(updates.update_add_htlcs.is_empty());
-                       assert!(updates.update_fee.is_none());
-                       (updates.update_fail_htlcs.remove(0), updates.commitment_signed)
-               },
-               _ => panic!("Unexpected event type!"),
-       };
-       let raa = if test_ignore_second_cs {
-               match events_3.remove(1) {
-                       MessageSendEvent::SendRevokeAndACK { node_id, msg } => {
-                               assert_eq!(node_id, nodes[2].node.get_our_node_id());
-                               Some(msg.clone())
-                       },
-                       _ => panic!("Unexpected event"),
-               }
-       } else { None };
-       let send_event_b = SendEvent::from_event(events_3.remove(0));
-       assert_eq!(send_event_b.node_id, nodes[2].node.get_our_node_id());
-
-       // Now deliver the new messages...
-
-       nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &messages_a.0).unwrap();
-       commitment_signed_dance!(nodes[0], nodes[1], messages_a.1, false);
-       let events_4 = nodes[0].node.get_and_clear_pending_events();
-       assert_eq!(events_4.len(), 1);
-       if let Event::PaymentFailed { payment_hash, rejected_by_dest, .. } = events_4[0] {
-               assert_eq!(payment_hash, payment_hash_1);
-               assert!(rejected_by_dest);
-       } else { panic!("Unexpected event!"); }
-
-       nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &send_event_b.msgs[0]).unwrap();
-       if test_ignore_second_cs {
-               nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &send_event_b.commitment_msg).unwrap();
-               check_added_monitors!(nodes[2], 1);
-               let bs_revoke_and_ack = get_event_msg!(nodes[2], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
-               nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &raa.unwrap()).unwrap();
-               check_added_monitors!(nodes[2], 1);
-               let bs_cs = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
-               assert!(bs_cs.update_add_htlcs.is_empty());
-               assert!(bs_cs.update_fail_htlcs.is_empty());
-               assert!(bs_cs.update_fail_malformed_htlcs.is_empty());
-               assert!(bs_cs.update_fulfill_htlcs.is_empty());
-               assert!(bs_cs.update_fee.is_none());
-
-               nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
-               check_added_monitors!(nodes[1], 1);
-               let as_cs = get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
-               assert!(as_cs.update_add_htlcs.is_empty());
-               assert!(as_cs.update_fail_htlcs.is_empty());
-               assert!(as_cs.update_fail_malformed_htlcs.is_empty());
-               assert!(as_cs.update_fulfill_htlcs.is_empty());
-               assert!(as_cs.update_fee.is_none());
-
-               nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &bs_cs.commitment_signed).unwrap();
-               check_added_monitors!(nodes[1], 1);
-               let as_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
-
-               nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &as_cs.commitment_signed).unwrap();
-               check_added_monitors!(nodes[2], 1);
-               let bs_second_raa = get_event_msg!(nodes[2], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
-
-               nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_raa).unwrap();
-               check_added_monitors!(nodes[2], 1);
-               assert!(nodes[2].node.get_and_clear_pending_msg_events().is_empty());
-
-               nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_second_raa).unwrap();
-               check_added_monitors!(nodes[1], 1);
-               assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-       } else {
-               commitment_signed_dance!(nodes[2], nodes[1], send_event_b.commitment_msg, false);
-       }
-
-       expect_pending_htlcs_forwardable!(nodes[2]);
-
-       let events_6 = nodes[2].node.get_and_clear_pending_events();
-       assert_eq!(events_6.len(), 1);
-       match events_6[0] {
-               Event::PaymentReceived { payment_hash, .. } => { assert_eq!(payment_hash, payment_hash_2); },
-               _ => panic!("Unexpected event"),
-       };
-
-       if test_ignore_second_cs {
-               expect_pending_htlcs_forwardable!(nodes[1]);
-               check_added_monitors!(nodes[1], 1);
-
-               send_event = SendEvent::from_node(&nodes[1]);
-               assert_eq!(send_event.node_id, nodes[0].node.get_our_node_id());
-               assert_eq!(send_event.msgs.len(), 1);
-               nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &send_event.msgs[0]).unwrap();
-               commitment_signed_dance!(nodes[0], nodes[1], send_event.commitment_msg, false);
-
-               expect_pending_htlcs_forwardable!(nodes[0]);
-
-               let events_9 = nodes[0].node.get_and_clear_pending_events();
-               assert_eq!(events_9.len(), 1);
-               match events_9[0] {
-                       Event::PaymentReceived { payment_hash, .. } => assert_eq!(payment_hash, payment_hash_4.unwrap()),
-                       _ => panic!("Unexpected event"),
-               };
-               claim_payment(&nodes[2], &[&nodes[1], &nodes[0]], payment_preimage_4.unwrap());
-       }
-
-       claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage_2);
-}
-
-#[test]
-fn test_monitor_update_fail_raa() {
-       do_test_monitor_update_fail_raa(false);
-       do_test_monitor_update_fail_raa(true);
-}
-
-#[test]
-fn test_monitor_update_fail_reestablish() {
-       // Simple test for message retransmission after monitor update failure on
-       // channel_reestablish generating a monitor update (which comes from freeing holding cell
-       // HTLCs).
-       let mut nodes = create_network(3, &[None, None, None]);
-       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-       create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
-
-       let (our_payment_preimage, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1000000);
-
-       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
-       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
-
-       assert!(nodes[2].node.claim_funds(our_payment_preimage));
-       check_added_monitors!(nodes[2], 1);
-       let mut updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
-       assert!(updates.update_add_htlcs.is_empty());
-       assert!(updates.update_fail_htlcs.is_empty());
-       assert!(updates.update_fail_malformed_htlcs.is_empty());
-       assert!(updates.update_fee.is_none());
-       assert_eq!(updates.update_fulfill_htlcs.len(), 1);
-       nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]).unwrap();
-       check_added_monitors!(nodes[1], 1);
-       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-       commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false);
-
-       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
-       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id());
-       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id());
-
-       let as_reestablish = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id());
-       let bs_reestablish = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id());
-
-       nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bs_reestablish).unwrap();
-
-       if let msgs::HandleError { err, action: Some(msgs::ErrorAction::IgnoreError) } = nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &as_reestablish).unwrap_err() {
-               assert_eq!(err, "Failed to update ChannelMonitor");
-       } else { panic!(); }
-       check_added_monitors!(nodes[1], 1);
-
-       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
-       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
-
-       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id());
-       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id());
-
-       assert!(as_reestablish == get_event_msg!(nodes[0], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id()));
-       assert!(bs_reestablish == get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id()));
-
-       nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bs_reestablish).unwrap();
-
-       nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &as_reestablish).unwrap();
-       check_added_monitors!(nodes[1], 0);
-       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-
-       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Ok(());
-       nodes[1].node.test_restore_channel_monitor();
-       check_added_monitors!(nodes[1], 1);
-
-       updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-       assert!(updates.update_add_htlcs.is_empty());
-       assert!(updates.update_fail_htlcs.is_empty());
-       assert!(updates.update_fail_malformed_htlcs.is_empty());
-       assert!(updates.update_fee.is_none());
-       assert_eq!(updates.update_fulfill_htlcs.len(), 1);
-       nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]).unwrap();
-       commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, false);
-
-       let events = nodes[0].node.get_and_clear_pending_events();
-       assert_eq!(events.len(), 1);
-       match events[0] {
-               Event::PaymentSent { payment_preimage, .. } => assert_eq!(payment_preimage, our_payment_preimage),
-               _ => panic!("Unexpected event"),
-       }
-}
-
-#[test]
-fn raa_no_response_awaiting_raa_state() {
-       // This is a rather convoluted test which ensures that if handling of an RAA does not happen
-       // due to a previous monitor update failure, we still set AwaitingRemoteRevoke on the channel
-       // in question (assuming it intends to respond with a CS after monitor updating is restored).
-       // Backported from chanmon_fail_consistency fuzz tests as this used to be broken.
-       let mut nodes = create_network(2, &[None, None]);
-       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
-       let (payment_preimage_1, payment_hash_1) = get_payment_preimage_hash!(nodes[0]);
-       let (payment_preimage_2, payment_hash_2) = get_payment_preimage_hash!(nodes[0]);
-       let (payment_preimage_3, payment_hash_3) = get_payment_preimage_hash!(nodes[0]);
-
-       // Queue up two payments - one will be delivered right away, one immediately goes into the
-       // holding cell as nodes[0] is AwaitingRAA. Ultimately this allows us to deliver an RAA
-       // immediately after a CS. By setting failing the monitor update failure from the CS (which
-       // requires only an RAA response due to AwaitingRAA) we can deliver the RAA and require the CS
-       // generation during RAA while in monitor-update-failed state.
-       nodes[0].node.send_payment(route.clone(), payment_hash_1).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       nodes[0].node.send_payment(route.clone(), payment_hash_2).unwrap();
-       check_added_monitors!(nodes[0], 0);
-
-       let mut events = nodes[0].node.get_and_clear_pending_msg_events();
-       assert_eq!(events.len(), 1);
-       let payment_event = SendEvent::from_event(events.pop().unwrap());
-       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
-       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg).unwrap();
-       check_added_monitors!(nodes[1], 1);
-
-       let bs_responses = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_responses.0).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       let mut events = nodes[0].node.get_and_clear_pending_msg_events();
-       assert_eq!(events.len(), 1);
-       let payment_event = SendEvent::from_event(events.pop().unwrap());
-
-       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_responses.1).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
-
-       // Now we have a CS queued up which adds a new HTLC (which will need a RAA/CS response from
-       // nodes[1]) followed by an RAA. Fail the monitor updating prior to the CS, deliver the RAA,
-       // then restore channel monitor updates.
-       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
-       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
-       if let msgs::HandleError { err, action: Some(msgs::ErrorAction::IgnoreError) } = nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg).unwrap_err() {
-               assert_eq!(err, "Failed to update ChannelMonitor");
-       } else { panic!(); }
-       check_added_monitors!(nodes[1], 1);
-
-       if let msgs::HandleError { err, action: Some(msgs::ErrorAction::IgnoreError) } = nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa).unwrap_err() {
-               assert_eq!(err, "Previous monitor update failure prevented responses to RAA");
-       } else { panic!(); }
-       check_added_monitors!(nodes[1], 1);
-
-       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Ok(());
-       nodes[1].node.test_restore_channel_monitor();
-       // nodes[1] should be AwaitingRAA here!
-       check_added_monitors!(nodes[1], 1);
-       let bs_responses = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-       expect_pending_htlcs_forwardable!(nodes[1]);
-       expect_payment_received!(nodes[1], payment_hash_1, 1000000);
-
-       // We send a third payment here, which is somewhat of a redundant test, but the
-       // chanmon_fail_consistency test required it to actually find the bug (by seeing out-of-sync
-       // commitment transaction states) whereas here we can explicitly check for it.
-       nodes[0].node.send_payment(route.clone(), payment_hash_3).unwrap();
-       check_added_monitors!(nodes[0], 0);
-       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
-
-       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_responses.0).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       let mut events = nodes[0].node.get_and_clear_pending_msg_events();
-       assert_eq!(events.len(), 1);
-       let payment_event = SendEvent::from_event(events.pop().unwrap());
-
-       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_responses.1).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
-
-       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
-       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg).unwrap();
-       check_added_monitors!(nodes[1], 1);
-       let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
-
-       // Finally deliver the RAA to nodes[1] which results in a CS response to the last update
-       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa).unwrap();
-       check_added_monitors!(nodes[1], 1);
-       expect_pending_htlcs_forwardable!(nodes[1]);
-       expect_payment_received!(nodes[1], payment_hash_2, 1000000);
-       let bs_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-
-       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa).unwrap();
-       check_added_monitors!(nodes[0], 1);
-
-       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_update.commitment_signed).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
-
-       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa).unwrap();
-       check_added_monitors!(nodes[1], 1);
-       expect_pending_htlcs_forwardable!(nodes[1]);
-       expect_payment_received!(nodes[1], payment_hash_3, 1000000);
-
-       claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1);
-       claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
-       claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_3);
-}
-
-#[test]
-fn claim_while_disconnected_monitor_update_fail() {
-       // Test for claiming a payment while disconnected and then having the resulting
-       // channel-update-generated monitor update fail. This kind of thing isn't a particularly
-       // contrived case for nodes with network instability.
-       // Backported from chanmon_fail_consistency fuzz tests as an unmerged version of the handling
-       // code introduced a regression in this test (specifically, this caught a removal of the
-       // channel_reestablish handling ensuring the order was sensical given the messages used).
-       let mut nodes = create_network(2, &[None, None]);
-       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       // Forward a payment for B to claim
-       let (payment_preimage_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
-
-       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
-       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
-
-       assert!(nodes[1].node.claim_funds(payment_preimage_1));
-       check_added_monitors!(nodes[1], 1);
-
-       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id());
-       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id());
-
-       let as_reconnect = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id());
-       let bs_reconnect = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id());
-
-       nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bs_reconnect).unwrap();
-       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
-
-       // Now deliver a's reestablish, freeing the claim from the holding cell, but fail the monitor
-       // update.
-       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
-
-       if let msgs::HandleError { err, action: Some(msgs::ErrorAction::IgnoreError) } = nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &as_reconnect).unwrap_err() {
-               assert_eq!(err, "Failed to update ChannelMonitor");
-       } else { panic!(); }
-       check_added_monitors!(nodes[1], 1);
-       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-
-       // Send a second payment from A to B, resulting in a commitment update that gets swallowed with
-       // the monitor still failed
-       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
-       let (payment_preimage_2, payment_hash_2) = get_payment_preimage_hash!(nodes[0]);
-       nodes[0].node.send_payment(route, payment_hash_2).unwrap();
-       check_added_monitors!(nodes[0], 1);
-
-       let as_updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
-       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &as_updates.update_add_htlcs[0]).unwrap();
-       if let msgs::HandleError { err, action: Some(msgs::ErrorAction::IgnoreError) } = nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_updates.commitment_signed).unwrap_err() {
-               assert_eq!(err, "Previous monitor update failure prevented generation of RAA");
-       } else { panic!(); }
-       // Note that nodes[1] not updating monitor here is OK - it wont take action on the new HTLC
-       // until we've test_restore_channel_monitor'd and updated for the new commitment transaction.
-
-       // Now un-fail the monitor, which will result in B sending its original commitment update,
-       // receiving the commitment update from A, and the resulting commitment dances.
-       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Ok(());
-       nodes[1].node.test_restore_channel_monitor();
-       check_added_monitors!(nodes[1], 1);
-
-       let bs_msgs = nodes[1].node.get_and_clear_pending_msg_events();
-       assert_eq!(bs_msgs.len(), 2);
-
-       match bs_msgs[0] {
-               MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
-                       assert_eq!(*node_id, nodes[0].node.get_our_node_id());
-                       nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]).unwrap();
-                       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &updates.commitment_signed).unwrap();
-                       check_added_monitors!(nodes[0], 1);
-
-                       let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
-                       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa).unwrap();
-                       check_added_monitors!(nodes[1], 1);
-               },
-               _ => panic!("Unexpected event"),
-       }
-
-       match bs_msgs[1] {
-               MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
-                       assert_eq!(*node_id, nodes[0].node.get_our_node_id());
-                       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), msg).unwrap();
-                       check_added_monitors!(nodes[0], 1);
-               },
-               _ => panic!("Unexpected event"),
-       }
-
-       let as_commitment = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
-
-       let bs_commitment = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_commitment.commitment_signed).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
-
-       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_commitment.commitment_signed).unwrap();
-       check_added_monitors!(nodes[1], 1);
-       let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
-       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa).unwrap();
-       check_added_monitors!(nodes[1], 1);
-
-       expect_pending_htlcs_forwardable!(nodes[1]);
-       expect_payment_received!(nodes[1], payment_hash_2, 1000000);
-
-       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa).unwrap();
-       check_added_monitors!(nodes[0], 1);
-
-       let events = nodes[0].node.get_and_clear_pending_events();
-       assert_eq!(events.len(), 1);
-       match events[0] {
-               Event::PaymentSent { ref payment_preimage } => {
-                       assert_eq!(*payment_preimage, payment_preimage_1);
-               },
-               _ => panic!("Unexpected event"),
-       }
-
-       claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
-}
-
-#[test]
-fn monitor_failed_no_reestablish_response() {
-       // Test for receiving a channel_reestablish after a monitor update failure resulted in no
-       // response to a commitment_signed.
-       // Backported from chanmon_fail_consistency fuzz tests as it caught a long-standing
-       // debug_assert!() failure in channel_reestablish handling.
-       let mut nodes = create_network(2, &[None, None]);
-       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       // Route the payment and deliver the initial commitment_signed (with a monitor update failure
-       // on receipt).
-       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
-       let (payment_preimage_1, payment_hash_1) = get_payment_preimage_hash!(nodes[0]);
-       nodes[0].node.send_payment(route, payment_hash_1).unwrap();
-       check_added_monitors!(nodes[0], 1);
-
-       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
-       let mut events = nodes[0].node.get_and_clear_pending_msg_events();
-       assert_eq!(events.len(), 1);
-       let payment_event = SendEvent::from_event(events.pop().unwrap());
-       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
-       if let msgs::HandleError { err, action: Some(msgs::ErrorAction::IgnoreError) } = nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg).unwrap_err() {
-               assert_eq!(err, "Failed to update ChannelMonitor");
-       } else { panic!(); }
-       check_added_monitors!(nodes[1], 1);
-
-       // Now disconnect and immediately reconnect, delivering the channel_reestablish while nodes[1]
-       // is still failing to update monitors.
-       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
-       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
-
-       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id());
-       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id());
-
-       let as_reconnect = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id());
-       let bs_reconnect = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id());
-
-       nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &as_reconnect).unwrap();
-       nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bs_reconnect).unwrap();
-
-       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Ok(());
-       nodes[1].node.test_restore_channel_monitor();
-       check_added_monitors!(nodes[1], 1);
-       let bs_responses = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-
-       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_responses.0).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_responses.1).unwrap();
-       check_added_monitors!(nodes[0], 1);
-
-       let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
-       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa).unwrap();
-       check_added_monitors!(nodes[1], 1);
-
-       expect_pending_htlcs_forwardable!(nodes[1]);
-       expect_payment_received!(nodes[1], payment_hash_1, 1000000);
-
-       claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1);
-}
-
-#[test]
-fn first_message_on_recv_ordering() {
-       // Test that if the initial generator of a monitor-update-frozen state doesn't generate
-       // messages, we're willing to flip the order of response messages if neccessary in resposne to
-       // a commitment_signed which needs to send an RAA first.
-       // At a high level, our goal is to fail monitor updating in response to an RAA which needs no
-       // response and then handle a CS while in the failed state, requiring an RAA followed by a CS
-       // response. To do this, we start routing two payments, with the final RAA for the first being
-       // delivered while B is in AwaitingRAA, hence when we deliver the CS for the second B will
-       // have no pending response but will want to send a RAA/CS (with the updates for the second
-       // payment applied).
-       // Backported from chanmon_fail_consistency fuzz tests as it caught a bug here.
-       let mut nodes = create_network(2, &[None, None]);
-       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       // Route the first payment outbound, holding the last RAA for B until we are set up so that we
-       // can deliver it and fail the monitor update.
-       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
-       let (payment_preimage_1, payment_hash_1) = get_payment_preimage_hash!(nodes[0]);
-       nodes[0].node.send_payment(route, payment_hash_1).unwrap();
-       check_added_monitors!(nodes[0], 1);
-
-       let mut events = nodes[0].node.get_and_clear_pending_msg_events();
-       assert_eq!(events.len(), 1);
-       let payment_event = SendEvent::from_event(events.pop().unwrap());
-       assert_eq!(payment_event.node_id, nodes[1].node.get_our_node_id());
-       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
-       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg).unwrap();
-       check_added_monitors!(nodes[1], 1);
-       let bs_responses = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-
-       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_responses.0).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_responses.1).unwrap();
-       check_added_monitors!(nodes[0], 1);
-
-       let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
-
-       // Route the second payment, generating an update_add_htlc/commitment_signed
-       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
-       let (payment_preimage_2, payment_hash_2) = get_payment_preimage_hash!(nodes[0]);
-       nodes[0].node.send_payment(route, payment_hash_2).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       let mut events = nodes[0].node.get_and_clear_pending_msg_events();
-       assert_eq!(events.len(), 1);
-       let payment_event = SendEvent::from_event(events.pop().unwrap());
-       assert_eq!(payment_event.node_id, nodes[1].node.get_our_node_id());
-
-       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
-
-       // Deliver the final RAA for the first payment, which does not require a response. RAAs
-       // generally require a commitment_signed, so the fact that we're expecting an opposite response
-       // to the next message also tests resetting the delivery order.
-       if let msgs::HandleError { err, action: Some(msgs::ErrorAction::IgnoreError) } = nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa).unwrap_err() {
-               assert_eq!(err, "Failed to update ChannelMonitor");
-       } else { panic!(); }
-       check_added_monitors!(nodes[1], 1);
-
-       // Now deliver the update_add_htlc/commitment_signed for the second payment, which does need an
-       // RAA/CS response, which should be generated when we call test_restore_channel_monitor (with
-       // the appropriate HTLC acceptance).
-       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
-       if let msgs::HandleError { err, action: Some(msgs::ErrorAction::IgnoreError) } = nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg).unwrap_err() {
-               assert_eq!(err, "Previous monitor update failure prevented generation of RAA");
-       } else { panic!(); }
-
-       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Ok(());
-       nodes[1].node.test_restore_channel_monitor();
-       check_added_monitors!(nodes[1], 1);
-
-       expect_pending_htlcs_forwardable!(nodes[1]);
-       expect_payment_received!(nodes[1], payment_hash_1, 1000000);
-
-       let bs_responses = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_responses.0).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_responses.1).unwrap();
-       check_added_monitors!(nodes[0], 1);
-
-       let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
-       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa).unwrap();
-       check_added_monitors!(nodes[1], 1);
-
-       expect_pending_htlcs_forwardable!(nodes[1]);
-       expect_payment_received!(nodes[1], payment_hash_2, 1000000);
-
-       claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1);
-       claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
-}
-
-#[test]
-fn test_monitor_update_fail_claim() {
-       // Basic test for monitor update failures when processing claim_funds calls.
-       // We set up a simple 3-node network, sending a payment from A to B and failing B's monitor
-       // update to claim the payment. We then send a payment C->B->A, making the forward of this
-       // payment from B to A fail due to the paused channel. Finally, we restore the channel monitor
-       // updating and claim the payment on B.
-       let mut nodes = create_network(3, &[None, None, None]);
-       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-       create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
-
-       // Rebalance a bit so that we can send backwards from 3 to 2.
-       send_payment(&nodes[0], &[&nodes[1], &nodes[2]], 5000000);
-
-       let (payment_preimage_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
-
-       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
-       assert!(nodes[1].node.claim_funds(payment_preimage_1));
-       check_added_monitors!(nodes[1], 1);
-
-       let route = nodes[2].router.get_route(&nodes[0].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
-       let (_, payment_hash_2) = get_payment_preimage_hash!(nodes[0]);
-       nodes[2].node.send_payment(route, payment_hash_2).unwrap();
-       check_added_monitors!(nodes[2], 1);
-
-       // Successfully update the monitor on the 1<->2 channel, but the 0<->1 channel should still be
-       // paused, so forward shouldn't succeed until we call test_restore_channel_monitor().
-       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Ok(());
-
-       let mut events = nodes[2].node.get_and_clear_pending_msg_events();
-       assert_eq!(events.len(), 1);
-       let payment_event = SendEvent::from_event(events.pop().unwrap());
-       nodes[1].node.handle_update_add_htlc(&nodes[2].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
-       commitment_signed_dance!(nodes[1], nodes[2], payment_event.commitment_msg, false, true);
-
-       let bs_fail_update = get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
-       nodes[2].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_fail_update.update_fail_htlcs[0]).unwrap();
-       commitment_signed_dance!(nodes[2], nodes[1], bs_fail_update.commitment_signed, false, true);
-
-       let msg_events = nodes[2].node.get_and_clear_pending_msg_events();
-       assert_eq!(msg_events.len(), 1);
-       match msg_events[0] {
-               MessageSendEvent::PaymentFailureNetworkUpdate { update: msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg }} => {
-                       assert_eq!(msg.contents.short_channel_id, chan_1.0.contents.short_channel_id);
-                       assert_eq!(msg.contents.flags & 2, 2); // temp disabled
-               },
-               _ => panic!("Unexpected event"),
-       }
-
-       let events = nodes[2].node.get_and_clear_pending_events();
-       assert_eq!(events.len(), 1);
-       if let Event::PaymentFailed { payment_hash, rejected_by_dest, .. } = events[0] {
-               assert_eq!(payment_hash, payment_hash_2);
-               assert!(!rejected_by_dest);
-       } else { panic!("Unexpected event!"); }
-
-       // Now restore monitor updating on the 0<->1 channel and claim the funds on B.
-       nodes[1].node.test_restore_channel_monitor();
-       check_added_monitors!(nodes[1], 1);
-
-       let bs_fulfill_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-       nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_fulfill_update.update_fulfill_htlcs[0]).unwrap();
-       commitment_signed_dance!(nodes[0], nodes[1], bs_fulfill_update.commitment_signed, false);
-
-       let events = nodes[0].node.get_and_clear_pending_events();
-       assert_eq!(events.len(), 1);
-       if let Event::PaymentSent { payment_preimage, .. } = events[0] {
-               assert_eq!(payment_preimage, payment_preimage_1);
-       } else { panic!("Unexpected event!"); }
-}
-
-#[test]
-fn test_monitor_update_on_pending_forwards() {
-       // Basic test for monitor update failures when processing pending HTLC fail/add forwards.
-       // We do this with a simple 3-node network, sending a payment from A to C and one from C to A.
-       // The payment from A to C will be failed by C and pending a back-fail to A, while the payment
-       // from C to A will be pending a forward to A.
-       let mut nodes = create_network(3, &[None, None, None]);
-       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-       create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
-
-       // Rebalance a bit so that we can send backwards from 3 to 1.
-       send_payment(&nodes[0], &[&nodes[1], &nodes[2]], 5000000);
-
-       let (_, payment_hash_1) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1000000);
-       assert!(nodes[2].node.fail_htlc_backwards(&payment_hash_1));
-       expect_pending_htlcs_forwardable!(nodes[2]);
-       check_added_monitors!(nodes[2], 1);
-
-       let cs_fail_update = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
-       nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &cs_fail_update.update_fail_htlcs[0]).unwrap();
-       commitment_signed_dance!(nodes[1], nodes[2], cs_fail_update.commitment_signed, true, true);
-       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-
-       let route = nodes[2].router.get_route(&nodes[0].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
-       let (payment_preimage_2, payment_hash_2) = get_payment_preimage_hash!(nodes[0]);
-       nodes[2].node.send_payment(route, payment_hash_2).unwrap();
-       check_added_monitors!(nodes[2], 1);
-
-       let mut events = nodes[2].node.get_and_clear_pending_msg_events();
-       assert_eq!(events.len(), 1);
-       let payment_event = SendEvent::from_event(events.pop().unwrap());
-       nodes[1].node.handle_update_add_htlc(&nodes[2].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
-       commitment_signed_dance!(nodes[1], nodes[2], payment_event.commitment_msg, false);
-
-       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
-       expect_pending_htlcs_forwardable!(nodes[1]);
-       check_added_monitors!(nodes[1], 1);
-       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-
-       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Ok(());
-       nodes[1].node.test_restore_channel_monitor();
-       check_added_monitors!(nodes[1], 1);
-
-       let bs_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-       nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fail_htlcs[0]).unwrap();
-       nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_add_htlcs[0]).unwrap();
-       commitment_signed_dance!(nodes[0], nodes[1], bs_updates.commitment_signed, false, true);
-
-       let events = nodes[0].node.get_and_clear_pending_events();
-       assert_eq!(events.len(), 2);
-       if let Event::PaymentFailed { payment_hash, rejected_by_dest, .. } = events[0] {
-               assert_eq!(payment_hash, payment_hash_1);
-               assert!(rejected_by_dest);
-       } else { panic!("Unexpected event!"); }
-       match events[1] {
-               Event::PendingHTLCsForwardable { .. } => { },
-               _ => panic!("Unexpected event"),
-       };
-       nodes[0].node.process_pending_htlc_forwards();
-       expect_payment_received!(nodes[0], payment_hash_2, 1000000);
-
-       claim_payment(&nodes[2], &[&nodes[1], &nodes[0]], payment_preimage_2);
-}
-
-#[test]
-fn monitor_update_claim_fail_no_response() {
-       // Test for claim_funds resulting in both a monitor update failure and no message response (due
-       // to channel being AwaitingRAA).
-       // Backported from chanmon_fail_consistency fuzz tests as an unmerged version of the handling
-       // code was broken.
-       let mut nodes = create_network(2, &[None, None]);
-       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       // Forward a payment for B to claim
-       let (payment_preimage_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
-
-       // Now start forwarding a second payment, skipping the last RAA so B is in AwaitingRAA
-       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
-       let (payment_preimage_2, payment_hash_2) = get_payment_preimage_hash!(nodes[0]);
-       nodes[0].node.send_payment(route, payment_hash_2).unwrap();
-       check_added_monitors!(nodes[0], 1);
-
-       let mut events = nodes[0].node.get_and_clear_pending_msg_events();
-       assert_eq!(events.len(), 1);
-       let payment_event = SendEvent::from_event(events.pop().unwrap());
-       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
-       let as_raa = commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false, true, false, true);
-
-       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
-       assert!(nodes[1].node.claim_funds(payment_preimage_1));
-       check_added_monitors!(nodes[1], 1);
-       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-
-       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Ok(());
-       nodes[1].node.test_restore_channel_monitor();
-       check_added_monitors!(nodes[1], 1);
-       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-
-       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa).unwrap();
-       check_added_monitors!(nodes[1], 1);
-       expect_pending_htlcs_forwardable!(nodes[1]);
-       expect_payment_received!(nodes[1], payment_hash_2, 1000000);
-
-       let bs_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-       nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fulfill_htlcs[0]).unwrap();
-       commitment_signed_dance!(nodes[0], nodes[1], bs_updates.commitment_signed, false);
-
-       let events = nodes[0].node.get_and_clear_pending_events();
-       assert_eq!(events.len(), 1);
-       match events[0] {
-               Event::PaymentSent { ref payment_preimage } => {
-                       assert_eq!(*payment_preimage, payment_preimage_1);
-               },
-               _ => panic!("Unexpected event"),
-       }
-
-       claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
-}
-
-// Note that restore_between_fails with !fail_on_generate is useless
-// Also note that !fail_on_generate && !fail_on_signed is useless
-// Finally, note that !fail_on_signed is not possible with fail_on_generate && !restore_between_fails
-// confirm_a_first and restore_b_before_conf are wholly unrelated to earlier bools and
-// restore_b_before_conf has no meaning if !confirm_a_first
-fn do_during_funding_monitor_fail(fail_on_generate: bool, restore_between_fails: bool, fail_on_signed: bool, confirm_a_first: bool, restore_b_before_conf: bool) {
-       // Test that if the monitor update generated by funding_transaction_generated fails we continue
-       // the channel setup happily after the update is restored.
-       let mut nodes = create_network(2, &[None, None]);
-
-       nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 43).unwrap();
-       nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), LocalFeatures::new(), &get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id())).unwrap();
-       nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), LocalFeatures::new(), &get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id())).unwrap();
-
-       let (temporary_channel_id, funding_tx, funding_output) = create_funding_transaction(&nodes[0], 100000, 43);
-
-       if fail_on_generate {
-               *nodes[0].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
-       }
-       nodes[0].node.funding_transaction_generated(&temporary_channel_id, funding_output);
-       check_added_monitors!(nodes[0], 1);
-
-       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
-       nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id())).unwrap();
-       check_added_monitors!(nodes[1], 1);
-
-       if restore_between_fails {
-               assert!(fail_on_generate);
-               *nodes[0].chan_monitor.update_ret.lock().unwrap() = Ok(());
-               nodes[0].node.test_restore_channel_monitor();
-               check_added_monitors!(nodes[0], 1);
-               assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
-               assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
-       }
-
-       if fail_on_signed {
-               *nodes[0].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
-       } else {
-               assert!(restore_between_fails || !fail_on_generate); // We can't switch to good now (there's no monitor update)
-               assert!(fail_on_generate); // Somebody has to fail
-       }
-       let funding_signed_res = nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id()));
-       if fail_on_signed || !restore_between_fails {
-               if let msgs::HandleError { err, action: Some(msgs::ErrorAction::IgnoreError) } = funding_signed_res.unwrap_err() {
-                       if fail_on_generate && !restore_between_fails {
-                               assert_eq!(err, "Previous monitor update failure prevented funding_signed from allowing funding broadcast");
-                               check_added_monitors!(nodes[0], 0);
-                       } else {
-                               assert_eq!(err, "Failed to update ChannelMonitor");
-                               check_added_monitors!(nodes[0], 1);
-                       }
-               } else { panic!(); }
-
-               assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
-               *nodes[0].chan_monitor.update_ret.lock().unwrap() = Ok(());
-               nodes[0].node.test_restore_channel_monitor();
-       } else {
-               funding_signed_res.unwrap();
-       }
-
-       check_added_monitors!(nodes[0], 1);
-
-       let events = nodes[0].node.get_and_clear_pending_events();
-       assert_eq!(events.len(), 1);
-       match events[0] {
-               Event::FundingBroadcastSafe { ref funding_txo, user_channel_id } => {
-                       assert_eq!(user_channel_id, 43);
-                       assert_eq!(*funding_txo, funding_output);
-               },
-               _ => panic!("Unexpected event"),
-       };
-
-       if confirm_a_first {
-               confirm_transaction(&nodes[0].chain_monitor, &funding_tx, funding_tx.version);
-               nodes[1].node.handle_funding_locked(&nodes[0].node.get_our_node_id(), &get_event_msg!(nodes[0], MessageSendEvent::SendFundingLocked, nodes[1].node.get_our_node_id())).unwrap();
-       } else {
-               assert!(!restore_b_before_conf);
-               confirm_transaction(&nodes[1].chain_monitor, &funding_tx, funding_tx.version);
-               assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-       }
-
-       // Make sure nodes[1] isn't stupid enough to re-send the FundingLocked on reconnect
-       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
-       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
-       reconnect_nodes(&nodes[0], &nodes[1], (false, confirm_a_first), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
-       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
-       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-
-       if !restore_b_before_conf {
-               confirm_transaction(&nodes[1].chain_monitor, &funding_tx, funding_tx.version);
-               assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-               assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
-       }
-
-       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Ok(());
-       nodes[1].node.test_restore_channel_monitor();
-       check_added_monitors!(nodes[1], 1);
-
-       let (channel_id, (announcement, as_update, bs_update)) = if !confirm_a_first {
-               nodes[0].node.handle_funding_locked(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendFundingLocked, nodes[0].node.get_our_node_id())).unwrap();
-
-               confirm_transaction(&nodes[0].chain_monitor, &funding_tx, funding_tx.version);
-               let (funding_locked, channel_id) = create_chan_between_nodes_with_value_confirm_second(&nodes[1], &nodes[0]);
-               (channel_id, create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &funding_locked))
-       } else {
-               if restore_b_before_conf {
-                       confirm_transaction(&nodes[1].chain_monitor, &funding_tx, funding_tx.version);
-               }
-               let (funding_locked, channel_id) = create_chan_between_nodes_with_value_confirm_second(&nodes[0], &nodes[1]);
-               (channel_id, create_chan_between_nodes_with_value_b(&nodes[1], &nodes[0], &funding_locked))
-       };
-       for node in nodes.iter() {
-               assert!(node.router.handle_channel_announcement(&announcement).unwrap());
-               node.router.handle_channel_update(&as_update).unwrap();
-               node.router.handle_channel_update(&bs_update).unwrap();
-       }
-
-       send_payment(&nodes[0], &[&nodes[1]], 8000000);
-       close_channel(&nodes[0], &nodes[1], &channel_id, funding_tx, true);
-}
-
-#[test]
-fn during_funding_monitor_fail() {
-       do_during_funding_monitor_fail(false, false, true, true, true);
-       do_during_funding_monitor_fail(true, false, true, false, false);
-       do_during_funding_monitor_fail(true, true, true, true, false);
-       do_during_funding_monitor_fail(true, true, false, false, false);
-}
diff --git a/src/ln/channel.rs b/src/ln/channel.rs
deleted file mode 100644 (file)
index c1eaad0..0000000
+++ /dev/null
@@ -1,4704 +0,0 @@
-use bitcoin::blockdata::block::BlockHeader;
-use bitcoin::blockdata::script::{Script,Builder};
-use bitcoin::blockdata::transaction::{TxIn, TxOut, Transaction, SigHashType};
-use bitcoin::blockdata::opcodes;
-use bitcoin::util::hash::BitcoinHash;
-use bitcoin::util::bip143;
-use bitcoin::consensus::encode::{self, Encodable, Decodable};
-
-use bitcoin_hashes::{Hash, HashEngine};
-use bitcoin_hashes::sha256::Hash as Sha256;
-use bitcoin_hashes::hash160::Hash as Hash160;
-use bitcoin_hashes::sha256d::Hash as Sha256dHash;
-
-use secp256k1::key::{PublicKey,SecretKey};
-use secp256k1::{Secp256k1,Signature};
-use secp256k1;
-
-use ln::msgs;
-use ln::msgs::{DecodeError, OptionalField, LocalFeatures, DataLossProtect};
-use ln::channelmonitor::ChannelMonitor;
-use ln::channelmanager::{PendingHTLCStatus, HTLCSource, HTLCFailReason, HTLCFailureMsg, PendingForwardHTLCInfo, RAACommitmentOrder, PaymentPreimage, PaymentHash, BREAKDOWN_TIMEOUT, MAX_LOCAL_BREAKDOWN_TIMEOUT};
-use ln::chan_utils::{TxCreationKeys,HTLCOutputInCommitment,HTLC_SUCCESS_TX_WEIGHT,HTLC_TIMEOUT_TX_WEIGHT};
-use ln::chan_utils;
-use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
-use chain::transaction::OutPoint;
-use chain::keysinterface::{ChannelKeys, KeysInterface};
-use util::transaction_utils;
-use util::ser::{Readable, ReadableArgs, Writeable, Writer, WriterWriteAdaptor};
-use util::logger::{Logger, LogHolder};
-use util::errors::APIError;
-use util::config::{UserConfig,ChannelConfig};
-
-use std;
-use std::default::Default;
-use std::{cmp,mem,fmt};
-use std::sync::{Arc};
-
-#[cfg(test)]
-pub struct ChannelValueStat {
-       pub value_to_self_msat: u64,
-       pub channel_value_msat: u64,
-       pub channel_reserve_msat: u64,
-       pub pending_outbound_htlcs_amount_msat: u64,
-       pub pending_inbound_htlcs_amount_msat: u64,
-       pub holding_cell_outbound_amount_msat: u64,
-       pub their_max_htlc_value_in_flight_msat: u64, // outgoing
-}
-
-enum InboundHTLCRemovalReason {
-       FailRelay(msgs::OnionErrorPacket),
-       FailMalformed(([u8; 32], u16)),
-       Fulfill(PaymentPreimage),
-}
-
-enum InboundHTLCState {
-       /// Added by remote, to be included in next local commitment tx.
-       RemoteAnnounced(PendingHTLCStatus),
-       /// Included in a received commitment_signed message (implying we've revoke_and_ack'ed it), but
-       /// the remote side hasn't yet revoked their previous state, which we need them to do before we
-       /// accept this HTLC. Implies AwaitingRemoteRevoke.
-       /// We also have not yet included this HTLC in a commitment_signed message, and are waiting on
-       /// a remote revoke_and_ack on a previous state before we can do so.
-       AwaitingRemoteRevokeToAnnounce(PendingHTLCStatus),
-       /// Included in a received commitment_signed message (implying we've revoke_and_ack'ed it), but
-       /// the remote side hasn't yet revoked their previous state, which we need them to do before we
-       /// accept this HTLC. Implies AwaitingRemoteRevoke.
-       /// We have included this HTLC in our latest commitment_signed and are now just waiting on a
-       /// revoke_and_ack.
-       AwaitingAnnouncedRemoteRevoke(PendingHTLCStatus),
-       Committed,
-       /// Removed by us and a new commitment_signed was sent (if we were AwaitingRemoteRevoke when we
-       /// created it we would have put it in the holding cell instead). When they next revoke_and_ack
-       /// we'll drop it.
-       /// Note that we have to keep an eye on the HTLC until we've received a broadcastable
-       /// commitment transaction without it as otherwise we'll have to force-close the channel to
-       /// claim it before the timeout (obviously doesn't apply to revoked HTLCs that we can't claim
-       /// anyway). That said, ChannelMonitor does this for us (see
-       /// ChannelMonitor::would_broadcast_at_height) so we actually remove the HTLC from our own
-       /// local state before then, once we're sure that the next commitment_signed and
-       /// ChannelMonitor::provide_latest_local_commitment_tx_info will not include this HTLC.
-       LocalRemoved(InboundHTLCRemovalReason),
-}
-
-struct InboundHTLCOutput {
-       htlc_id: u64,
-       amount_msat: u64,
-       cltv_expiry: u32,
-       payment_hash: PaymentHash,
-       state: InboundHTLCState,
-}
-
-enum OutboundHTLCState {
-       /// Added by us and included in a commitment_signed (if we were AwaitingRemoteRevoke when we
-       /// created it we would have put it in the holding cell instead). When they next revoke_and_ack
-       /// we will promote to Committed (note that they may not accept it until the next time we
-       /// revoke, but we don't really care about that:
-       ///  * they've revoked, so worst case we can announce an old state and get our (option on)
-       ///    money back (though we won't), and,
-       ///  * we'll send them a revoke when they send a commitment_signed, and since only they're
-       ///    allowed to remove it, the "can only be removed once committed on both sides" requirement
-       ///    doesn't matter to us and it's up to them to enforce it, worst-case they jump ahead but
-       ///    we'll never get out of sync).
-       /// Note that we Box the OnionPacket as it's rather large and we don't want to blow up
-       /// OutboundHTLCOutput's size just for a temporary bit
-       LocalAnnounced(Box<msgs::OnionPacket>),
-       Committed,
-       /// Remote removed this (outbound) HTLC. We're waiting on their commitment_signed to finalize
-       /// the change (though they'll need to revoke before we fail the payment).
-       RemoteRemoved(Option<HTLCFailReason>),
-       /// Remote removed this and sent a commitment_signed (implying we've revoke_and_ack'ed it), but
-       /// the remote side hasn't yet revoked their previous state, which we need them to do before we
-       /// can do any backwards failing. Implies AwaitingRemoteRevoke.
-       /// We also have not yet removed this HTLC in a commitment_signed message, and are waiting on a
-       /// remote revoke_and_ack on a previous state before we can do so.
-       AwaitingRemoteRevokeToRemove(Option<HTLCFailReason>),
-       /// Remote removed this and sent a commitment_signed (implying we've revoke_and_ack'ed it), but
-       /// the remote side hasn't yet revoked their previous state, which we need them to do before we
-       /// can do any backwards failing. Implies AwaitingRemoteRevoke.
-       /// We have removed this HTLC in our latest commitment_signed and are now just waiting on a
-       /// revoke_and_ack to drop completely.
-       AwaitingRemovedRemoteRevoke(Option<HTLCFailReason>),
-}
-
-struct OutboundHTLCOutput {
-       htlc_id: u64,
-       amount_msat: u64,
-       cltv_expiry: u32,
-       payment_hash: PaymentHash,
-       state: OutboundHTLCState,
-       source: HTLCSource,
-}
-
-/// See AwaitingRemoteRevoke ChannelState for more info
-enum HTLCUpdateAwaitingACK {
-       AddHTLC { // TODO: Time out if we're getting close to cltv_expiry
-               // always outbound
-               amount_msat: u64,
-               cltv_expiry: u32,
-               payment_hash: PaymentHash,
-               source: HTLCSource,
-               onion_routing_packet: msgs::OnionPacket,
-       },
-       ClaimHTLC {
-               payment_preimage: PaymentPreimage,
-               htlc_id: u64,
-       },
-       FailHTLC {
-               htlc_id: u64,
-               err_packet: msgs::OnionErrorPacket,
-       },
-}
-
-/// There are a few "states" and then a number of flags which can be applied:
-/// We first move through init with OurInitSent -> TheirInitSent -> FundingCreated -> FundingSent.
-/// TheirFundingLocked and OurFundingLocked then get set on FundingSent, and when both are set we
-/// move on to ChannelFunded.
-/// Note that PeerDisconnected can be set on both ChannelFunded and FundingSent.
-/// ChannelFunded can then get all remaining flags set on it, until we finish shutdown, then we
-/// move on to ShutdownComplete, at which point most calls into this channel are disallowed.
-enum ChannelState {
-       /// Implies we have (or are prepared to) send our open_channel/accept_channel message
-       OurInitSent = (1 << 0),
-       /// Implies we have received their open_channel/accept_channel message
-       TheirInitSent = (1 << 1),
-       /// We have sent funding_created and are awaiting a funding_signed to advance to FundingSent.
-       /// Note that this is nonsense for an inbound channel as we immediately generate funding_signed
-       /// upon receipt of funding_created, so simply skip this state.
-       FundingCreated = 4,
-       /// Set when we have received/sent funding_created and funding_signed and are thus now waiting
-       /// on the funding transaction to confirm. The FundingLocked flags are set to indicate when we
-       /// and our counterparty consider the funding transaction confirmed.
-       FundingSent = 8,
-       /// Flag which can be set on FundingSent to indicate they sent us a funding_locked message.
-       /// Once both TheirFundingLocked and OurFundingLocked are set, state moves on to ChannelFunded.
-       TheirFundingLocked = (1 << 4),
-       /// Flag which can be set on FundingSent to indicate we sent them a funding_locked message.
-       /// Once both TheirFundingLocked and OurFundingLocked are set, state moves on to ChannelFunded.
-       OurFundingLocked = (1 << 5),
-       ChannelFunded = 64,
-       /// Flag which is set on ChannelFunded and FundingSent indicating remote side is considered
-       /// "disconnected" and no updates are allowed until after we've done a channel_reestablish
-       /// dance.
-       PeerDisconnected = (1 << 7),
-       /// Flag which is set on ChannelFunded, FundingCreated, and FundingSent indicating the user has
-       /// told us they failed to update our ChannelMonitor somewhere and we should pause sending any
-       /// outbound messages until they've managed to do so.
-       MonitorUpdateFailed = (1 << 8),
-       /// Flag which implies that we have sent a commitment_signed but are awaiting the responding
-       /// revoke_and_ack message. During this time period, we can't generate new commitment_signed
-       /// messages as then we will be unable to determine which HTLCs they included in their
-       /// revoke_and_ack implicit ACK, so instead we have to hold them away temporarily to be sent
-       /// later.
-       /// Flag is set on ChannelFunded.
-       AwaitingRemoteRevoke = (1 << 9),
-       /// Flag which is set on ChannelFunded or FundingSent after receiving a shutdown message from
-       /// the remote end. If set, they may not add any new HTLCs to the channel, and we are expected
-       /// to respond with our own shutdown message when possible.
-       RemoteShutdownSent = (1 << 10),
-       /// Flag which is set on ChannelFunded or FundingSent after sending a shutdown message. At this
-       /// point, we may not add any new HTLCs to the channel.
-       /// TODO: Investigate some kind of timeout mechanism by which point the remote end must provide
-       /// us their shutdown.
-       LocalShutdownSent = (1 << 11),
-       /// We've successfully negotiated a closing_signed dance. At this point ChannelManager is about
-       /// to drop us, but we store this anyway.
-       ShutdownComplete = 4096,
-}
-const BOTH_SIDES_SHUTDOWN_MASK: u32 = (ChannelState::LocalShutdownSent as u32 | ChannelState::RemoteShutdownSent as u32);
-const MULTI_STATE_FLAGS: u32 = (BOTH_SIDES_SHUTDOWN_MASK | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateFailed as u32);
-
-const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
-
-// TODO: We should refactor this to be an Inbound/OutboundChannel until initial setup handshaking
-// has been completed, and then turn into a Channel to get compiler-time enforcement of things like
-// calling channel_id() before we're set up or things like get_outbound_funding_signed on an
-// inbound channel.
-pub(super) struct Channel {
-       config: ChannelConfig,
-
-       user_id: u64,
-
-       channel_id: [u8; 32],
-       channel_state: u32,
-       channel_outbound: bool,
-       secp_ctx: Secp256k1<secp256k1::All>,
-       channel_value_satoshis: u64,
-
-       local_keys: ChannelKeys,
-       shutdown_pubkey: PublicKey,
-
-       // Our commitment numbers start at 2^48-1 and count down, whereas the ones used in transaction
-       // generation start at 0 and count up...this simplifies some parts of implementation at the
-       // cost of others, but should really just be changed.
-
-       cur_local_commitment_transaction_number: u64,
-       cur_remote_commitment_transaction_number: u64,
-       value_to_self_msat: u64, // Excluding all pending_htlcs, excluding fees
-       pending_inbound_htlcs: Vec<InboundHTLCOutput>,
-       pending_outbound_htlcs: Vec<OutboundHTLCOutput>,
-       holding_cell_htlc_updates: Vec<HTLCUpdateAwaitingACK>,
-
-       /// When resending CS/RAA messages on channel monitor restoration or on reconnect, we always
-       /// need to ensure we resend them in the order we originally generated them. Note that because
-       /// there can only ever be one in-flight CS and/or one in-flight RAA at any time, it is
-       /// sufficient to simply set this to the opposite of any message we are generating as we
-       /// generate it. ie when we generate a CS, we set this to RAAFirst as, if there is a pending
-       /// in-flight RAA to resend, it will have been the first thing we generated, and thus we should
-       /// send it first.
-       resend_order: RAACommitmentOrder,
-
-       monitor_pending_funding_locked: bool,
-       monitor_pending_revoke_and_ack: bool,
-       monitor_pending_commitment_signed: bool,
-       monitor_pending_forwards: Vec<(PendingForwardHTLCInfo, u64)>,
-       monitor_pending_failures: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>,
-
-       // pending_update_fee is filled when sending and receiving update_fee
-       // For outbound channel, feerate_per_kw is updated with the value from
-       // pending_update_fee when revoke_and_ack is received
-       //
-       // For inbound channel, feerate_per_kw is updated when it receives
-       // commitment_signed and revoke_and_ack is generated
-       // The pending value is kept when another pair of update_fee and commitment_signed
-       // is received during AwaitingRemoteRevoke and relieved when the expected
-       // revoke_and_ack is received and new commitment_signed is generated to be
-       // sent to the funder. Otherwise, the pending value is removed when receiving
-       // commitment_signed.
-       pending_update_fee: Option<u64>,
-       // update_fee() during ChannelState::AwaitingRemoteRevoke is hold in
-       // holdina_cell_update_fee then moved to pending_udpate_fee when revoke_and_ack
-       // is received. holding_cell_update_fee is updated when there are additional
-       // update_fee() during ChannelState::AwaitingRemoteRevoke.
-       holding_cell_update_fee: Option<u64>,
-       next_local_htlc_id: u64,
-       next_remote_htlc_id: u64,
-       channel_update_count: u32,
-       feerate_per_kw: u64,
-
-       #[cfg(debug_assertions)]
-       /// Max to_local and to_remote outputs in a locally-generated commitment transaction
-       max_commitment_tx_output_local: ::std::sync::Mutex<(u64, u64)>,
-       #[cfg(debug_assertions)]
-       /// Max to_local and to_remote outputs in a remote-generated commitment transaction
-       max_commitment_tx_output_remote: ::std::sync::Mutex<(u64, u64)>,
-
-       #[cfg(test)]
-       // Used in ChannelManager's tests to send a revoked transaction
-       pub last_local_commitment_txn: Vec<Transaction>,
-       #[cfg(not(test))]
-       last_local_commitment_txn: Vec<Transaction>,
-
-       last_sent_closing_fee: Option<(u64, u64)>, // (feerate, fee)
-
-       /// The hash of the block in which the funding transaction reached our CONF_TARGET. We use this
-       /// to detect unconfirmation after a serialize-unserialize roundtrip where we may not see a full
-       /// series of block_connected/block_disconnected calls. Obviously this is not a guarantee as we
-       /// could miss the funding_tx_confirmed_in block as well, but it serves as a useful fallback.
-       funding_tx_confirmed_in: Option<Sha256dHash>,
-       short_channel_id: Option<u64>,
-       /// Used to deduplicate block_connected callbacks, also used to verify consistency during
-       /// ChannelManager deserialization (hence pub(super))
-       pub(super) last_block_connected: Sha256dHash,
-       funding_tx_confirmations: u64,
-
-       their_dust_limit_satoshis: u64,
-       #[cfg(test)]
-       pub(super) our_dust_limit_satoshis: u64,
-       #[cfg(not(test))]
-       our_dust_limit_satoshis: u64,
-       #[cfg(test)]
-       pub(super) their_max_htlc_value_in_flight_msat: u64,
-       #[cfg(not(test))]
-       their_max_htlc_value_in_flight_msat: u64,
-       //get_our_max_htlc_value_in_flight_msat(): u64,
-       /// minimum channel reserve for **self** to maintain - set by them.
-       their_channel_reserve_satoshis: u64,
-       //get_our_channel_reserve_satoshis(): u64,
-       their_htlc_minimum_msat: u64,
-       our_htlc_minimum_msat: u64,
-       their_to_self_delay: u16,
-       our_to_self_delay: u16,
-       #[cfg(test)]
-       pub their_max_accepted_htlcs: u16,
-       #[cfg(not(test))]
-       their_max_accepted_htlcs: u16,
-       //implied by OUR_MAX_HTLCS: our_max_accepted_htlcs: u16,
-       minimum_depth: u32,
-
-       their_funding_pubkey: Option<PublicKey>,
-       their_revocation_basepoint: Option<PublicKey>,
-       their_payment_basepoint: Option<PublicKey>,
-       their_delayed_payment_basepoint: Option<PublicKey>,
-       their_htlc_basepoint: Option<PublicKey>,
-       their_cur_commitment_point: Option<PublicKey>,
-
-       their_prev_commitment_point: Option<PublicKey>,
-       their_node_id: PublicKey,
-
-       their_shutdown_scriptpubkey: Option<Script>,
-
-       channel_monitor: ChannelMonitor,
-
-       logger: Arc<Logger>,
-}
-
-pub const OUR_MAX_HTLCS: u16 = 50; //TODO
-/// Confirmation count threshold at which we close a channel. Ideally we'd keep the channel around
-/// on ice until the funding transaction gets more confirmations, but the LN protocol doesn't
-/// really allow for this, so instead we're stuck closing it out at that point.
-const UNCONF_THRESHOLD: u32 = 6;
-/// Exposing these two constants for use in test in ChannelMonitor
-pub const COMMITMENT_TX_BASE_WEIGHT: u64 = 724;
-pub const COMMITMENT_TX_WEIGHT_PER_HTLC: u64 = 172;
-const SPENDING_INPUT_FOR_A_OUTPUT_WEIGHT: u64 = 79; // prevout: 36, nSequence: 4, script len: 1, witness lengths: (3+1)/4, sig: 73/4, if-selector: 1, redeemScript: (6 ops + 2*33 pubkeys + 1*2 delay)/4
-const B_OUTPUT_PLUS_SPENDING_INPUT_WEIGHT: u64 = 104; // prevout: 40, nSequence: 4, script len: 1, witness lengths: 3/4, sig: 73/4, pubkey: 33/4, output: 31 (TODO: Wrong? Useless?)
-/// Maximmum `funding_satoshis` value, according to the BOLT #2 specification
-/// it's 2^24.
-pub const MAX_FUNDING_SATOSHIS: u64 = (1 << 24);
-
-#[cfg(test)]
-pub const ACCEPTED_HTLC_SCRIPT_WEIGHT: usize = 138; //Here we have a diff due to HTLC CLTV expiry being < 2^15 in test
-#[cfg(not(test))]
-pub const ACCEPTED_HTLC_SCRIPT_WEIGHT: usize = 139;
-pub const OFFERED_HTLC_SCRIPT_WEIGHT: usize = 133;
-
-/// Used to return a simple Error back to ChannelManager. Will get converted to a
-/// msgs::ErrorAction::SendErrorMessage or msgs::ErrorAction::IgnoreError as appropriate with our
-/// channel_id in ChannelManager.
-pub(super) enum ChannelError {
-       Ignore(&'static str),
-       Close(&'static str),
-       CloseDelayBroadcast {
-               msg: &'static str,
-               update: Option<ChannelMonitor>
-       },
-}
-
-impl fmt::Debug for ChannelError {
-       fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-               match self {
-                       &ChannelError::Ignore(e) => write!(f, "Ignore : {}", e),
-                       &ChannelError::Close(e) => write!(f, "Close : {}", e),
-                       &ChannelError::CloseDelayBroadcast { msg, .. } => write!(f, "CloseDelayBroadcast : {}", msg)
-               }
-       }
-}
-
-macro_rules! secp_check {
-       ($res: expr, $err: expr) => {
-               match $res {
-                       Ok(thing) => thing,
-                       Err(_) => return Err(ChannelError::Close($err)),
-               }
-       };
-}
-
-impl Channel {
-       // Convert constants + channel value to limits:
-       fn get_our_max_htlc_value_in_flight_msat(channel_value_satoshis: u64) -> u64 {
-               channel_value_satoshis * 1000 / 10 //TODO
-       }
-
-       /// Returns a minimum channel reserve value **they** need to maintain
-       ///
-       /// Guaranteed to return a value no larger than channel_value_satoshis
-       pub(crate) fn get_our_channel_reserve_satoshis(channel_value_satoshis: u64) -> u64 {
-               let (q, _) = channel_value_satoshis.overflowing_div(100);
-               cmp::min(channel_value_satoshis, cmp::max(q, 1000)) //TODO
-       }
-
-       fn derive_our_dust_limit_satoshis(at_open_background_feerate: u64) -> u64 {
-               cmp::max(at_open_background_feerate * B_OUTPUT_PLUS_SPENDING_INPUT_WEIGHT / 1000, 546) //TODO
-       }
-
-       fn derive_our_htlc_minimum_msat(_at_open_channel_feerate_per_kw: u64) -> u64 {
-               1000 // TODO
-       }
-
-       // Constructors:
-       pub fn new_outbound(fee_estimator: &FeeEstimator, keys_provider: &Arc<KeysInterface>, their_node_id: PublicKey, channel_value_satoshis: u64, push_msat: u64, user_id: u64, logger: Arc<Logger>, config: &UserConfig) -> Result<Channel, APIError> {
-               let chan_keys = keys_provider.get_channel_keys(false);
-
-               if channel_value_satoshis >= MAX_FUNDING_SATOSHIS {
-                       return Err(APIError::APIMisuseError{err: "funding value > 2^24"});
-               }
-
-               if push_msat > channel_value_satoshis * 1000 {
-                       return Err(APIError::APIMisuseError{err: "push value > channel value"});
-               }
-               if config.own_channel_config.our_to_self_delay < BREAKDOWN_TIMEOUT {
-                       return Err(APIError::APIMisuseError{err: "Configured with an unreasonable our_to_self_delay putting user funds at risks"});
-               }
-
-
-               let background_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background);
-               if Channel::get_our_channel_reserve_satoshis(channel_value_satoshis) < Channel::derive_our_dust_limit_satoshis(background_feerate) {
-                       return Err(APIError::FeeRateTooHigh{err: format!("Not enough reserve above dust limit can be found at current fee rate({})", background_feerate), feerate: background_feerate});
-               }
-
-               let feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
-
-               let secp_ctx = Secp256k1::new();
-               let channel_monitor = ChannelMonitor::new(&chan_keys.revocation_base_key, &chan_keys.delayed_payment_base_key,
-                                                         &chan_keys.htlc_base_key, &chan_keys.payment_base_key, &keys_provider.get_shutdown_pubkey(), config.own_channel_config.our_to_self_delay,
-                                                         keys_provider.get_destination_script(), logger.clone());
-
-               Ok(Channel {
-                       user_id: user_id,
-                       config: config.channel_options.clone(),
-
-                       channel_id: keys_provider.get_channel_id(),
-                       channel_state: ChannelState::OurInitSent as u32,
-                       channel_outbound: true,
-                       secp_ctx: secp_ctx,
-                       channel_value_satoshis: channel_value_satoshis,
-
-                       local_keys: chan_keys,
-                       shutdown_pubkey: keys_provider.get_shutdown_pubkey(),
-                       cur_local_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
-                       cur_remote_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
-                       value_to_self_msat: channel_value_satoshis * 1000 - push_msat,
-
-                       pending_inbound_htlcs: Vec::new(),
-                       pending_outbound_htlcs: Vec::new(),
-                       holding_cell_htlc_updates: Vec::new(),
-                       pending_update_fee: None,
-                       holding_cell_update_fee: None,
-                       next_local_htlc_id: 0,
-                       next_remote_htlc_id: 0,
-                       channel_update_count: 1,
-
-                       resend_order: RAACommitmentOrder::CommitmentFirst,
-
-                       monitor_pending_funding_locked: false,
-                       monitor_pending_revoke_and_ack: false,
-                       monitor_pending_commitment_signed: false,
-                       monitor_pending_forwards: Vec::new(),
-                       monitor_pending_failures: Vec::new(),
-
-                       #[cfg(debug_assertions)]
-                       max_commitment_tx_output_local: ::std::sync::Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
-                       #[cfg(debug_assertions)]
-                       max_commitment_tx_output_remote: ::std::sync::Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
-
-                       last_local_commitment_txn: Vec::new(),
-
-                       last_sent_closing_fee: None,
-
-                       funding_tx_confirmed_in: None,
-                       short_channel_id: None,
-                       last_block_connected: Default::default(),
-                       funding_tx_confirmations: 0,
-
-                       feerate_per_kw: feerate,
-                       their_dust_limit_satoshis: 0,
-                       our_dust_limit_satoshis: Channel::derive_our_dust_limit_satoshis(background_feerate),
-                       their_max_htlc_value_in_flight_msat: 0,
-                       their_channel_reserve_satoshis: 0,
-                       their_htlc_minimum_msat: 0,
-                       our_htlc_minimum_msat: Channel::derive_our_htlc_minimum_msat(feerate),
-                       their_to_self_delay: 0,
-                       our_to_self_delay: config.own_channel_config.our_to_self_delay,
-                       their_max_accepted_htlcs: 0,
-                       minimum_depth: 0, // Filled in in accept_channel
-
-                       their_funding_pubkey: None,
-                       their_revocation_basepoint: None,
-                       their_payment_basepoint: None,
-                       their_delayed_payment_basepoint: None,
-                       their_htlc_basepoint: None,
-                       their_cur_commitment_point: None,
-
-                       their_prev_commitment_point: None,
-                       their_node_id: their_node_id,
-
-                       their_shutdown_scriptpubkey: None,
-
-                       channel_monitor: channel_monitor,
-
-                       logger,
-               })
-       }
-
-       fn check_remote_fee(fee_estimator: &FeeEstimator, feerate_per_kw: u32) -> Result<(), ChannelError> {
-               if (feerate_per_kw as u64) < fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background) {
-                       return Err(ChannelError::Close("Peer's feerate much too low"));
-               }
-               if (feerate_per_kw as u64) > fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::HighPriority) * 2 {
-                       return Err(ChannelError::Close("Peer's feerate much too high"));
-               }
-               Ok(())
-       }
-
-       /// Creates a new channel from a remote sides' request for one.
-       /// Assumes chain_hash has already been checked and corresponds with what we expect!
-       pub fn new_from_req(fee_estimator: &FeeEstimator, keys_provider: &Arc<KeysInterface>, their_node_id: PublicKey, their_local_features: LocalFeatures, msg: &msgs::OpenChannel, user_id: u64, logger: Arc<Logger>, config: &UserConfig) -> Result<Channel, ChannelError> {
-               let chan_keys = keys_provider.get_channel_keys(true);
-               let mut local_config = (*config).channel_options.clone();
-
-               if config.own_channel_config.our_to_self_delay < BREAKDOWN_TIMEOUT {
-                       return Err(ChannelError::Close("Configured with an unreasonable our_to_self_delay putting user funds at risks"));
-               }
-
-               // Check sanity of message fields:
-               if msg.funding_satoshis >= MAX_FUNDING_SATOSHIS {
-                       return Err(ChannelError::Close("funding value > 2^24"));
-               }
-               if msg.channel_reserve_satoshis > msg.funding_satoshis {
-                       return Err(ChannelError::Close("Bogus channel_reserve_satoshis"));
-               }
-               if msg.push_msat > (msg.funding_satoshis - msg.channel_reserve_satoshis) * 1000 {
-                       return Err(ChannelError::Close("push_msat larger than funding value"));
-               }
-               if msg.dust_limit_satoshis > msg.funding_satoshis {
-                       return Err(ChannelError::Close("Peer never wants payout outputs?"));
-               }
-               if msg.dust_limit_satoshis > msg.channel_reserve_satoshis {
-                       return Err(ChannelError::Close("Bogus; channel reserve is less than dust limit"));
-               }
-               if msg.htlc_minimum_msat >= (msg.funding_satoshis - msg.channel_reserve_satoshis) * 1000 {
-                       return Err(ChannelError::Close("Minimum htlc value is full channel value"));
-               }
-               Channel::check_remote_fee(fee_estimator, msg.feerate_per_kw)?;
-
-               if msg.to_self_delay > config.peer_channel_config_limits.their_to_self_delay || msg.to_self_delay > MAX_LOCAL_BREAKDOWN_TIMEOUT {
-                       return Err(ChannelError::Close("They wanted our payments to be delayed by a needlessly long period"));
-               }
-               if msg.max_accepted_htlcs < 1 {
-                       return Err(ChannelError::Close("0 max_accpted_htlcs makes for a useless channel"));
-               }
-               if msg.max_accepted_htlcs > 483 {
-                       return Err(ChannelError::Close("max_accpted_htlcs > 483"));
-               }
-
-               // Now check against optional parameters as set by config...
-               if msg.funding_satoshis < config.peer_channel_config_limits.min_funding_satoshis {
-                       return Err(ChannelError::Close("funding satoshis is less than the user specified limit"));
-               }
-               if msg.htlc_minimum_msat > config.peer_channel_config_limits.max_htlc_minimum_msat {
-                       return Err(ChannelError::Close("htlc minimum msat is higher than the user specified limit"));
-               }
-               if msg.max_htlc_value_in_flight_msat < config.peer_channel_config_limits.min_max_htlc_value_in_flight_msat {
-                       return Err(ChannelError::Close("max htlc value in flight msat is less than the user specified limit"));
-               }
-               if msg.channel_reserve_satoshis > config.peer_channel_config_limits.max_channel_reserve_satoshis {
-                       return Err(ChannelError::Close("channel reserve satoshis is higher than the user specified limit"));
-               }
-               if msg.max_accepted_htlcs < config.peer_channel_config_limits.min_max_accepted_htlcs {
-                       return Err(ChannelError::Close("max accepted htlcs is less than the user specified limit"));
-               }
-               if msg.dust_limit_satoshis < config.peer_channel_config_limits.min_dust_limit_satoshis {
-                       return Err(ChannelError::Close("dust limit satoshis is less than the user specified limit"));
-               }
-               if msg.dust_limit_satoshis > config.peer_channel_config_limits.max_dust_limit_satoshis {
-                       return Err(ChannelError::Close("dust limit satoshis is greater than the user specified limit"));
-               }
-
-               // Convert things into internal flags and prep our state:
-
-               let their_announce = if (msg.channel_flags & 1) == 1 { true } else { false };
-               if config.peer_channel_config_limits.force_announced_channel_preference {
-                       if local_config.announced_channel != their_announce {
-                               return Err(ChannelError::Close("Peer tried to open channel but their announcement preference is different from ours"));
-                       }
-               }
-               // we either accept their preference or the preferences match
-               local_config.announced_channel = their_announce;
-
-               let background_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background);
-
-               let our_dust_limit_satoshis = Channel::derive_our_dust_limit_satoshis(background_feerate);
-               let our_channel_reserve_satoshis = Channel::get_our_channel_reserve_satoshis(msg.funding_satoshis);
-               if our_channel_reserve_satoshis < our_dust_limit_satoshis {
-                       return Err(ChannelError::Close("Suitable channel reserve not found. aborting"));
-               }
-               if msg.channel_reserve_satoshis < our_dust_limit_satoshis {
-                       return Err(ChannelError::Close("channel_reserve_satoshis too small"));
-               }
-               if our_channel_reserve_satoshis < msg.dust_limit_satoshis {
-                       return Err(ChannelError::Close("Dust limit too high for our channel reserve"));
-               }
-
-               // check if the funder's amount for the initial commitment tx is sufficient
-               // for full fee payment
-               let funders_amount_msat = msg.funding_satoshis * 1000 - msg.push_msat;
-               if funders_amount_msat < background_feerate * COMMITMENT_TX_BASE_WEIGHT {
-                       return Err(ChannelError::Close("Insufficient funding amount for initial commitment"));
-               }
-
-               let to_local_msat = msg.push_msat;
-               let to_remote_msat = funders_amount_msat - background_feerate * COMMITMENT_TX_BASE_WEIGHT;
-               if to_local_msat <= msg.channel_reserve_satoshis * 1000 && to_remote_msat <= our_channel_reserve_satoshis * 1000 {
-                       return Err(ChannelError::Close("Insufficient funding amount for initial commitment"));
-               }
-
-               let secp_ctx = Secp256k1::new();
-               let mut channel_monitor = ChannelMonitor::new(&chan_keys.revocation_base_key, &chan_keys.delayed_payment_base_key,
-                                                             &chan_keys.htlc_base_key, &chan_keys.payment_base_key, &keys_provider.get_shutdown_pubkey(), config.own_channel_config.our_to_self_delay,
-                                                             keys_provider.get_destination_script(), logger.clone());
-               channel_monitor.set_their_base_keys(&msg.htlc_basepoint, &msg.delayed_payment_basepoint);
-               channel_monitor.set_their_to_self_delay(msg.to_self_delay);
-
-               let their_shutdown_scriptpubkey = if their_local_features.supports_upfront_shutdown_script() {
-                       match &msg.shutdown_scriptpubkey {
-                               &OptionalField::Present(ref script) => {
-                                       // Peer is signaling upfront_shutdown and has provided a non-accepted scriptpubkey format. We enforce it while receiving shutdown msg
-                                       if script.is_p2pkh() || script.is_p2sh() || script.is_v0_p2wsh() || script.is_v0_p2wpkh() {
-                                               Some(script.clone())
-                                       // Peer is signaling upfront_shutdown and has opt-out with a 0-length script. We don't enforce anything
-                                       } else if script.len() == 0 {
-                                               None
-                                       // Peer is signaling upfront_shutdown and has provided a non-accepted scriptpubkey format. Fail the channel
-                                       } else {
-                                               return Err(ChannelError::Close("Peer is signaling upfront_shutdown but has provided a non-accepted scriptpubkey format"));
-                                       }
-                               },
-                               // Peer is signaling upfront shutdown but don't opt-out with correct mechanism (a.k.a 0-length script). Peer looks buggy, we fail the channel
-                               &OptionalField::Absent => {
-                                       return Err(ChannelError::Close("Peer is signaling upfront_shutdown but we don't get any script. Use 0-length script to opt-out"));
-                               }
-                       }
-               } else { None };
-
-               let mut chan = Channel {
-                       user_id: user_id,
-                       config: local_config,
-
-                       channel_id: msg.temporary_channel_id,
-                       channel_state: (ChannelState::OurInitSent as u32) | (ChannelState::TheirInitSent as u32),
-                       channel_outbound: false,
-                       secp_ctx: secp_ctx,
-
-                       local_keys: chan_keys,
-                       shutdown_pubkey: keys_provider.get_shutdown_pubkey(),
-                       cur_local_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
-                       cur_remote_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
-                       value_to_self_msat: msg.push_msat,
-
-                       pending_inbound_htlcs: Vec::new(),
-                       pending_outbound_htlcs: Vec::new(),
-                       holding_cell_htlc_updates: Vec::new(),
-                       pending_update_fee: None,
-                       holding_cell_update_fee: None,
-                       next_local_htlc_id: 0,
-                       next_remote_htlc_id: 0,
-                       channel_update_count: 1,
-
-                       resend_order: RAACommitmentOrder::CommitmentFirst,
-
-                       monitor_pending_funding_locked: false,
-                       monitor_pending_revoke_and_ack: false,
-                       monitor_pending_commitment_signed: false,
-                       monitor_pending_forwards: Vec::new(),
-                       monitor_pending_failures: Vec::new(),
-
-                       #[cfg(debug_assertions)]
-                       max_commitment_tx_output_local: ::std::sync::Mutex::new((msg.push_msat, msg.funding_satoshis * 1000 - msg.push_msat)),
-                       #[cfg(debug_assertions)]
-                       max_commitment_tx_output_remote: ::std::sync::Mutex::new((msg.push_msat, msg.funding_satoshis * 1000 - msg.push_msat)),
-
-                       last_local_commitment_txn: Vec::new(),
-
-                       last_sent_closing_fee: None,
-
-                       funding_tx_confirmed_in: None,
-                       short_channel_id: None,
-                       last_block_connected: Default::default(),
-                       funding_tx_confirmations: 0,
-
-                       feerate_per_kw: msg.feerate_per_kw as u64,
-                       channel_value_satoshis: msg.funding_satoshis,
-                       their_dust_limit_satoshis: msg.dust_limit_satoshis,
-                       our_dust_limit_satoshis: our_dust_limit_satoshis,
-                       their_max_htlc_value_in_flight_msat: cmp::min(msg.max_htlc_value_in_flight_msat, msg.funding_satoshis * 1000),
-                       their_channel_reserve_satoshis: msg.channel_reserve_satoshis,
-                       their_htlc_minimum_msat: msg.htlc_minimum_msat,
-                       our_htlc_minimum_msat: Channel::derive_our_htlc_minimum_msat(msg.feerate_per_kw as u64),
-                       their_to_self_delay: msg.to_self_delay,
-                       our_to_self_delay: config.own_channel_config.our_to_self_delay,
-                       their_max_accepted_htlcs: msg.max_accepted_htlcs,
-                       minimum_depth: config.own_channel_config.minimum_depth,
-
-                       their_funding_pubkey: Some(msg.funding_pubkey),
-                       their_revocation_basepoint: Some(msg.revocation_basepoint),
-                       their_payment_basepoint: Some(msg.payment_basepoint),
-                       their_delayed_payment_basepoint: Some(msg.delayed_payment_basepoint),
-                       their_htlc_basepoint: Some(msg.htlc_basepoint),
-                       their_cur_commitment_point: Some(msg.first_per_commitment_point),
-
-                       their_prev_commitment_point: None,
-                       their_node_id: their_node_id,
-
-                       their_shutdown_scriptpubkey,
-
-                       channel_monitor: channel_monitor,
-
-                       logger,
-               };
-
-               let obscure_factor = chan.get_commitment_transaction_number_obscure_factor();
-               chan.channel_monitor.set_commitment_obscure_factor(obscure_factor);
-
-               Ok(chan)
-       }
-
-       // Utilities to derive keys:
-
-       fn build_local_commitment_secret(&self, idx: u64) -> SecretKey {
-               let res = chan_utils::build_commitment_secret(self.local_keys.commitment_seed, idx);
-               SecretKey::from_slice(&res).unwrap()
-       }
-
-       // Utilities to build transactions:
-
-       fn get_commitment_transaction_number_obscure_factor(&self) -> u64 {
-               let mut sha = Sha256::engine();
-               let our_payment_basepoint = PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.payment_base_key);
-
-               if self.channel_outbound {
-                       sha.input(&our_payment_basepoint.serialize());
-                       sha.input(&self.their_payment_basepoint.unwrap().serialize());
-               } else {
-                       sha.input(&self.their_payment_basepoint.unwrap().serialize());
-                       sha.input(&our_payment_basepoint.serialize());
-               }
-               let res = Sha256::from_engine(sha).into_inner();
-
-               ((res[26] as u64) << 5*8) |
-               ((res[27] as u64) << 4*8) |
-               ((res[28] as u64) << 3*8) |
-               ((res[29] as u64) << 2*8) |
-               ((res[30] as u64) << 1*8) |
-               ((res[31] as u64) << 0*8)
-       }
-
-       /// Transaction nomenclature is somewhat confusing here as there are many different cases - a
-       /// transaction is referred to as "a's transaction" implying that a will be able to broadcast
-       /// the transaction. Thus, b will generally be sending a signature over such a transaction to
-       /// a, and a can revoke the transaction by providing b the relevant per_commitment_secret. As
-       /// such, a transaction is generally the result of b increasing the amount paid to a (or adding
-       /// an HTLC to a).
-       /// @local is used only to convert relevant internal structures which refer to remote vs local
-       /// to decide value of outputs and direction of HTLCs.
-       /// @generated_by_local is used to determine *which* HTLCs to include - noting that the HTLC
-       /// state may indicate that one peer has informed the other that they'd like to add an HTLC but
-       /// have not yet committed it. Such HTLCs will only be included in transactions which are being
-       /// generated by the peer which proposed adding the HTLCs, and thus we need to understand both
-       /// which peer generated this transaction and "to whom" this transaction flows.
-       /// Returns (the transaction built, the number of HTLC outputs which were present in the
-       /// transaction, the list of HTLCs which were not ignored when building the transaction).
-       /// Note that below-dust HTLCs are included in the third return value, but not the second, and
-       /// sources are provided only for outbound HTLCs in the third return value.
-       #[inline]
-       fn build_commitment_transaction(&self, commitment_number: u64, keys: &TxCreationKeys, local: bool, generated_by_local: bool, feerate_per_kw: u64) -> (Transaction, usize, Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)>) {
-               let obscured_commitment_transaction_number = self.get_commitment_transaction_number_obscure_factor() ^ (INITIAL_COMMITMENT_NUMBER - commitment_number);
-
-               let txins = {
-                       let mut ins: Vec<TxIn> = Vec::new();
-                       ins.push(TxIn {
-                               previous_output: self.channel_monitor.get_funding_txo().unwrap().into_bitcoin_outpoint(),
-                               script_sig: Script::new(),
-                               sequence: ((0x80 as u32) << 8*3) | ((obscured_commitment_transaction_number >> 3*8) as u32),
-                               witness: Vec::new(),
-                       });
-                       ins
-               };
-
-               let mut txouts: Vec<(TxOut, Option<(HTLCOutputInCommitment, Option<&HTLCSource>)>)> = Vec::with_capacity(self.pending_inbound_htlcs.len() + self.pending_outbound_htlcs.len() + 2);
-               let mut included_dust_htlcs: Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)> = Vec::new();
-
-               let dust_limit_satoshis = if local { self.our_dust_limit_satoshis } else { self.their_dust_limit_satoshis };
-               let mut remote_htlc_total_msat = 0;
-               let mut local_htlc_total_msat = 0;
-               let mut value_to_self_msat_offset = 0;
-
-               log_trace!(self, "Building commitment transaction number {} for {}, generated by {} with fee {}...", commitment_number, if local { "us" } else { "remote" }, if generated_by_local { "us" } else { "remote" }, feerate_per_kw);
-
-               macro_rules! get_htlc_in_commitment {
-                       ($htlc: expr, $offered: expr) => {
-                               HTLCOutputInCommitment {
-                                       offered: $offered,
-                                       amount_msat: $htlc.amount_msat,
-                                       cltv_expiry: $htlc.cltv_expiry,
-                                       payment_hash: $htlc.payment_hash,
-                                       transaction_output_index: None
-                               }
-                       }
-               }
-
-               macro_rules! add_htlc_output {
-                       ($htlc: expr, $outbound: expr, $source: expr, $state_name: expr) => {
-                               if $outbound == local { // "offered HTLC output"
-                                       let htlc_in_tx = get_htlc_in_commitment!($htlc, true);
-                                       if $htlc.amount_msat / 1000 >= dust_limit_satoshis + (feerate_per_kw * HTLC_TIMEOUT_TX_WEIGHT / 1000) {
-                                               log_trace!(self, "   ...including {} {} HTLC {} (hash {}) with value {}", if $outbound { "outbound" } else { "inbound" }, $state_name, $htlc.htlc_id, log_bytes!($htlc.payment_hash.0), $htlc.amount_msat);
-                                               txouts.push((TxOut {
-                                                       script_pubkey: chan_utils::get_htlc_redeemscript(&htlc_in_tx, &keys).to_v0_p2wsh(),
-                                                       value: $htlc.amount_msat / 1000
-                                               }, Some((htlc_in_tx, $source))));
-                                       } else {
-                                               log_trace!(self, "   ...including {} {} dust HTLC {} (hash {}) with value {} due to dust limit", if $outbound { "outbound" } else { "inbound" }, $state_name, $htlc.htlc_id, log_bytes!($htlc.payment_hash.0), $htlc.amount_msat);
-                                               included_dust_htlcs.push((htlc_in_tx, $source));
-                                       }
-                               } else {
-                                       let htlc_in_tx = get_htlc_in_commitment!($htlc, false);
-                                       if $htlc.amount_msat / 1000 >= dust_limit_satoshis + (feerate_per_kw * HTLC_SUCCESS_TX_WEIGHT / 1000) {
-                                               log_trace!(self, "   ...including {} {} HTLC {} (hash {}) with value {}", if $outbound { "outbound" } else { "inbound" }, $state_name, $htlc.htlc_id, log_bytes!($htlc.payment_hash.0), $htlc.amount_msat);
-                                               txouts.push((TxOut { // "received HTLC output"
-                                                       script_pubkey: chan_utils::get_htlc_redeemscript(&htlc_in_tx, &keys).to_v0_p2wsh(),
-                                                       value: $htlc.amount_msat / 1000
-                                               }, Some((htlc_in_tx, $source))));
-                                       } else {
-                                               log_trace!(self, "   ...including {} {} dust HTLC {} (hash {}) with value {}", if $outbound { "outbound" } else { "inbound" }, $state_name, $htlc.htlc_id, log_bytes!($htlc.payment_hash.0), $htlc.amount_msat);
-                                               included_dust_htlcs.push((htlc_in_tx, $source));
-                                       }
-                               }
-                       }
-               }
-
-               for ref htlc in self.pending_inbound_htlcs.iter() {
-                       let (include, state_name) = match htlc.state {
-                               InboundHTLCState::RemoteAnnounced(_) => (!generated_by_local, "RemoteAnnounced"),
-                               InboundHTLCState::AwaitingRemoteRevokeToAnnounce(_) => (!generated_by_local, "AwaitingRemoteRevokeToAnnounce"),
-                               InboundHTLCState::AwaitingAnnouncedRemoteRevoke(_) => (true, "AwaitingAnnouncedRemoteRevoke"),
-                               InboundHTLCState::Committed => (true, "Committed"),
-                               InboundHTLCState::LocalRemoved(_) => (!generated_by_local, "LocalRemoved"),
-                       };
-
-                       if include {
-                               add_htlc_output!(htlc, false, None, state_name);
-                               remote_htlc_total_msat += htlc.amount_msat;
-                       } else {
-                               log_trace!(self, "   ...not including inbound HTLC {} (hash {}) with value {} due to state ({})", htlc.htlc_id, log_bytes!(htlc.payment_hash.0), htlc.amount_msat, state_name);
-                               match &htlc.state {
-                                       &InboundHTLCState::LocalRemoved(ref reason) => {
-                                               if generated_by_local {
-                                                       if let &InboundHTLCRemovalReason::Fulfill(_) = reason {
-                                                               value_to_self_msat_offset += htlc.amount_msat as i64;
-                                                       }
-                                               }
-                                       },
-                                       _ => {},
-                               }
-                       }
-               }
-
-               for ref htlc in self.pending_outbound_htlcs.iter() {
-                       let (include, state_name) = match htlc.state {
-                               OutboundHTLCState::LocalAnnounced(_) => (generated_by_local, "LocalAnnounced"),
-                               OutboundHTLCState::Committed => (true, "Committed"),
-                               OutboundHTLCState::RemoteRemoved(_) => (generated_by_local, "RemoteRemoved"),
-                               OutboundHTLCState::AwaitingRemoteRevokeToRemove(_) => (generated_by_local, "AwaitingRemoteRevokeToRemove"),
-                               OutboundHTLCState::AwaitingRemovedRemoteRevoke(_) => (false, "AwaitingRemovedRemoteRevoke"),
-                       };
-
-                       if include {
-                               add_htlc_output!(htlc, true, Some(&htlc.source), state_name);
-                               local_htlc_total_msat += htlc.amount_msat;
-                       } else {
-                               log_trace!(self, "   ...not including outbound HTLC {} (hash {}) with value {} due to state ({})", htlc.htlc_id, log_bytes!(htlc.payment_hash.0), htlc.amount_msat, state_name);
-                               match htlc.state {
-                                       OutboundHTLCState::AwaitingRemoteRevokeToRemove(None)|OutboundHTLCState::AwaitingRemovedRemoteRevoke(None) => {
-                                               value_to_self_msat_offset -= htlc.amount_msat as i64;
-                                       },
-                                       OutboundHTLCState::RemoteRemoved(None) => {
-                                               if !generated_by_local {
-                                                       value_to_self_msat_offset -= htlc.amount_msat as i64;
-                                               }
-                                       },
-                                       _ => {},
-                               }
-                       }
-               }
-
-               let value_to_self_msat: i64 = (self.value_to_self_msat - local_htlc_total_msat) as i64 + value_to_self_msat_offset;
-               assert!(value_to_self_msat >= 0);
-               // Note that in case they have several just-awaiting-last-RAA fulfills in-progress (ie
-               // AwaitingRemoteRevokeToRemove or AwaitingRemovedRemoteRevoke) we may have allowed them to
-               // "violate" their reserve value by couting those against it. Thus, we have to convert
-               // everything to i64 before subtracting as otherwise we can overflow.
-               let value_to_remote_msat: i64 = (self.channel_value_satoshis * 1000) as i64 - (self.value_to_self_msat as i64) - (remote_htlc_total_msat as i64) - value_to_self_msat_offset;
-               assert!(value_to_remote_msat >= 0);
-
-               #[cfg(debug_assertions)]
-               {
-                       // Make sure that the to_self/to_remote is always either past the appropriate
-                       // channel_reserve *or* it is making progress towards it.
-                       let mut max_commitment_tx_output = if generated_by_local {
-                               self.max_commitment_tx_output_local.lock().unwrap()
-                       } else {
-                               self.max_commitment_tx_output_remote.lock().unwrap()
-                       };
-                       debug_assert!(max_commitment_tx_output.0 <= value_to_self_msat as u64 || value_to_self_msat / 1000 >= self.their_channel_reserve_satoshis as i64);
-                       max_commitment_tx_output.0 = cmp::max(max_commitment_tx_output.0, value_to_self_msat as u64);
-                       debug_assert!(max_commitment_tx_output.1 <= value_to_remote_msat as u64 || value_to_remote_msat / 1000 >= Channel::get_our_channel_reserve_satoshis(self.channel_value_satoshis) as i64);
-                       max_commitment_tx_output.1 = cmp::max(max_commitment_tx_output.1, value_to_remote_msat as u64);
-               }
-
-               let total_fee: u64 = feerate_per_kw * (COMMITMENT_TX_BASE_WEIGHT + (txouts.len() as u64) * COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000;
-               let (value_to_self, value_to_remote) = if self.channel_outbound {
-                       (value_to_self_msat / 1000 - total_fee as i64, value_to_remote_msat / 1000)
-               } else {
-                       (value_to_self_msat / 1000, value_to_remote_msat / 1000 - total_fee as i64)
-               };
-
-               let value_to_a = if local { value_to_self } else { value_to_remote };
-               let value_to_b = if local { value_to_remote } else { value_to_self };
-
-               if value_to_a >= (dust_limit_satoshis as i64) {
-                       log_trace!(self, "   ...including {} output with value {}", if local { "to_local" } else { "to_remote" }, value_to_a);
-                       txouts.push((TxOut {
-                               script_pubkey: chan_utils::get_revokeable_redeemscript(&keys.revocation_key,
-                                                                                      if local { self.their_to_self_delay } else { self.our_to_self_delay },
-                                                                                      &keys.a_delayed_payment_key).to_v0_p2wsh(),
-                               value: value_to_a as u64
-                       }, None));
-               }
-
-               if value_to_b >= (dust_limit_satoshis as i64) {
-                       log_trace!(self, "   ...including {} output with value {}", if local { "to_remote" } else { "to_local" }, value_to_b);
-                       txouts.push((TxOut {
-                               script_pubkey: Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0)
-                                                            .push_slice(&Hash160::hash(&keys.b_payment_key.serialize())[..])
-                                                            .into_script(),
-                               value: value_to_b as u64
-                       }, None));
-               }
-
-               transaction_utils::sort_outputs(&mut txouts, |a, b| {
-                       if let &Some(ref a_htlc) = a {
-                               if let &Some(ref b_htlc) = b {
-                                       a_htlc.0.cltv_expiry.cmp(&b_htlc.0.cltv_expiry)
-                                               // Note that due to hash collisions, we have to have a fallback comparison
-                                               // here for fuzztarget mode (otherwise at least chanmon_fail_consistency
-                                               // may fail)!
-                                               .then(a_htlc.0.payment_hash.0.cmp(&b_htlc.0.payment_hash.0))
-                               // For non-HTLC outputs, if they're copying our SPK we don't really care if we
-                               // close the channel due to mismatches - they're doing something dumb:
-                               } else { cmp::Ordering::Equal }
-                       } else { cmp::Ordering::Equal }
-               });
-
-               let mut outputs: Vec<TxOut> = Vec::with_capacity(txouts.len());
-               let mut htlcs_included: Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)> = Vec::with_capacity(txouts.len() + included_dust_htlcs.len());
-               for (idx, mut out) in txouts.drain(..).enumerate() {
-                       outputs.push(out.0);
-                       if let Some((mut htlc, source_option)) = out.1.take() {
-                               htlc.transaction_output_index = Some(idx as u32);
-                               htlcs_included.push((htlc, source_option));
-                       }
-               }
-               let non_dust_htlc_count = htlcs_included.len();
-               htlcs_included.append(&mut included_dust_htlcs);
-
-               (Transaction {
-                       version: 2,
-                       lock_time: ((0x20 as u32) << 8*3) | ((obscured_commitment_transaction_number & 0xffffffu64) as u32),
-                       input: txins,
-                       output: outputs,
-               }, non_dust_htlc_count, htlcs_included)
-       }
-
-       #[inline]
-       fn get_closing_scriptpubkey(&self) -> Script {
-               let our_channel_close_key_hash = Hash160::hash(&self.shutdown_pubkey.serialize());
-               Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(&our_channel_close_key_hash[..]).into_script()
-       }
-
-       #[inline]
-       fn get_closing_transaction_weight(a_scriptpubkey: &Script, b_scriptpubkey: &Script) -> u64 {
-               (4 + 1 + 36 + 4 + 1 + 1 + 2*(8+1) + 4 + a_scriptpubkey.len() as u64 + b_scriptpubkey.len() as u64)*4 + 2 + 1 + 1 + 2*(1 + 72)
-       }
-
-       #[inline]
-       fn build_closing_transaction(&self, proposed_total_fee_satoshis: u64, skip_remote_output: bool) -> (Transaction, u64) {
-               let txins = {
-                       let mut ins: Vec<TxIn> = Vec::new();
-                       ins.push(TxIn {
-                               previous_output: self.channel_monitor.get_funding_txo().unwrap().into_bitcoin_outpoint(),
-                               script_sig: Script::new(),
-                               sequence: 0xffffffff,
-                               witness: Vec::new(),
-                       });
-                       ins
-               };
-
-               assert!(self.pending_inbound_htlcs.is_empty());
-               assert!(self.pending_outbound_htlcs.is_empty());
-               let mut txouts: Vec<(TxOut, ())> = Vec::new();
-
-               let mut total_fee_satoshis = proposed_total_fee_satoshis;
-               let value_to_self: i64 = (self.value_to_self_msat as i64) / 1000 - if self.channel_outbound { total_fee_satoshis as i64 } else { 0 };
-               let value_to_remote: i64 = ((self.channel_value_satoshis * 1000 - self.value_to_self_msat) as i64 / 1000) - if self.channel_outbound { 0 } else { total_fee_satoshis as i64 };
-
-               if value_to_self < 0 {
-                       assert!(self.channel_outbound);
-                       total_fee_satoshis += (-value_to_self) as u64;
-               } else if value_to_remote < 0 {
-                       assert!(!self.channel_outbound);
-                       total_fee_satoshis += (-value_to_remote) as u64;
-               }
-
-               if !skip_remote_output && value_to_remote as u64 > self.our_dust_limit_satoshis {
-                       txouts.push((TxOut {
-                               script_pubkey: self.their_shutdown_scriptpubkey.clone().unwrap(),
-                               value: value_to_remote as u64
-                       }, ()));
-               }
-
-               if value_to_self as u64 > self.our_dust_limit_satoshis {
-                       txouts.push((TxOut {
-                               script_pubkey: self.get_closing_scriptpubkey(),
-                               value: value_to_self as u64
-                       }, ()));
-               }
-
-               transaction_utils::sort_outputs(&mut txouts, |_, _| { cmp::Ordering::Equal }); // Ordering doesnt matter if they used our pubkey...
-
-               let mut outputs: Vec<TxOut> = Vec::new();
-               for out in txouts.drain(..) {
-                       outputs.push(out.0);
-               }
-
-               (Transaction {
-                       version: 2,
-                       lock_time: 0,
-                       input: txins,
-                       output: outputs,
-               }, total_fee_satoshis)
-       }
-
-       #[inline]
-       /// Creates a set of keys for build_commitment_transaction to generate a transaction which our
-       /// counterparty will sign (ie DO NOT send signatures over a transaction created by this to
-       /// our counterparty!)
-       /// The result is a transaction which we can revoke ownership of (ie a "local" transaction)
-       /// TODO Some magic rust shit to compile-time check this?
-       fn build_local_transaction_keys(&self, commitment_number: u64) -> Result<TxCreationKeys, ChannelError> {
-               let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &self.build_local_commitment_secret(commitment_number));
-               let delayed_payment_base = PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.delayed_payment_base_key);
-               let htlc_basepoint = PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.htlc_base_key);
-
-               Ok(secp_check!(TxCreationKeys::new(&self.secp_ctx, &per_commitment_point, &delayed_payment_base, &htlc_basepoint, &self.their_revocation_basepoint.unwrap(), &self.their_payment_basepoint.unwrap(), &self.their_htlc_basepoint.unwrap()), "Local tx keys generation got bogus keys"))
-       }
-
-       #[inline]
-       /// Creates a set of keys for build_commitment_transaction to generate a transaction which we
-       /// will sign and send to our counterparty.
-       /// If an Err is returned, it is a ChannelError::Close (for get_outbound_funding_created)
-       fn build_remote_transaction_keys(&self) -> Result<TxCreationKeys, ChannelError> {
-               //TODO: Ensure that the payment_key derived here ends up in the library users' wallet as we
-               //may see payments to it!
-               let payment_basepoint = PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.payment_base_key);
-               let revocation_basepoint = PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.revocation_base_key);
-               let htlc_basepoint = PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.htlc_base_key);
-
-               Ok(secp_check!(TxCreationKeys::new(&self.secp_ctx, &self.their_cur_commitment_point.unwrap(), &self.their_delayed_payment_basepoint.unwrap(), &self.their_htlc_basepoint.unwrap(), &revocation_basepoint, &payment_basepoint, &htlc_basepoint), "Remote tx keys generation got bogus keys"))
-       }
-
-       /// Gets the redeemscript for the funding transaction output (ie the funding transaction output
-       /// pays to get_funding_redeemscript().to_v0_p2wsh()).
-       /// Panics if called before accept_channel/new_from_req
-       pub fn get_funding_redeemscript(&self) -> Script {
-               let builder = Builder::new().push_opcode(opcodes::all::OP_PUSHNUM_2);
-               let our_funding_key = PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.funding_key).serialize();
-               let their_funding_key = self.their_funding_pubkey.expect("get_funding_redeemscript only allowed after accept_channel").serialize();
-               if our_funding_key[..] < their_funding_key[..] {
-                       builder.push_slice(&our_funding_key)
-                               .push_slice(&their_funding_key)
-               } else {
-                       builder.push_slice(&their_funding_key)
-                               .push_slice(&our_funding_key)
-               }.push_opcode(opcodes::all::OP_PUSHNUM_2).push_opcode(opcodes::all::OP_CHECKMULTISIG).into_script()
-       }
-
-       fn sign_commitment_transaction(&self, tx: &mut Transaction, their_sig: &Signature) -> Signature {
-               if tx.input.len() != 1 {
-                       panic!("Tried to sign commitment transaction that had input count != 1!");
-               }
-               if tx.input[0].witness.len() != 0 {
-                       panic!("Tried to re-sign commitment transaction");
-               }
-
-               let funding_redeemscript = self.get_funding_redeemscript();
-
-               let sighash = hash_to_message!(&bip143::SighashComponents::new(&tx).sighash_all(&tx.input[0], &funding_redeemscript, self.channel_value_satoshis)[..]);
-               let our_sig = self.secp_ctx.sign(&sighash, &self.local_keys.funding_key);
-
-               tx.input[0].witness.push(Vec::new()); // First is the multisig dummy
-
-               let our_funding_key = PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.funding_key).serialize();
-               let their_funding_key = self.their_funding_pubkey.unwrap().serialize();
-               if our_funding_key[..] < their_funding_key[..] {
-                       tx.input[0].witness.push(our_sig.serialize_der().to_vec());
-                       tx.input[0].witness.push(their_sig.serialize_der().to_vec());
-               } else {
-                       tx.input[0].witness.push(their_sig.serialize_der().to_vec());
-                       tx.input[0].witness.push(our_sig.serialize_der().to_vec());
-               }
-               tx.input[0].witness[1].push(SigHashType::All as u8);
-               tx.input[0].witness[2].push(SigHashType::All as u8);
-
-               tx.input[0].witness.push(funding_redeemscript.into_bytes());
-
-               our_sig
-       }
-
-       /// Builds the htlc-success or htlc-timeout transaction which spends a given HTLC output
-       /// @local is used only to convert relevant internal structures which refer to remote vs local
-       /// to decide value of outputs and direction of HTLCs.
-       fn build_htlc_transaction(&self, prev_hash: &Sha256dHash, htlc: &HTLCOutputInCommitment, local: bool, keys: &TxCreationKeys, feerate_per_kw: u64) -> Transaction {
-               chan_utils::build_htlc_transaction(prev_hash, feerate_per_kw, if local { self.their_to_self_delay } else { self.our_to_self_delay }, htlc, &keys.a_delayed_payment_key, &keys.revocation_key)
-       }
-
-       fn create_htlc_tx_signature(&self, tx: &Transaction, htlc: &HTLCOutputInCommitment, keys: &TxCreationKeys) -> Result<(Script, Signature, bool), ChannelError> {
-               if tx.input.len() != 1 {
-                       panic!("Tried to sign HTLC transaction that had input count != 1!");
-               }
-
-               let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &keys);
-
-               let our_htlc_key = secp_check!(chan_utils::derive_private_key(&self.secp_ctx, &keys.per_commitment_point, &self.local_keys.htlc_base_key), "Derived invalid key, peer is maliciously selecting parameters");
-               let sighash = hash_to_message!(&bip143::SighashComponents::new(&tx).sighash_all(&tx.input[0], &htlc_redeemscript, htlc.amount_msat / 1000)[..]);
-               let is_local_tx = PublicKey::from_secret_key(&self.secp_ctx, &our_htlc_key) == keys.a_htlc_key;
-               Ok((htlc_redeemscript, self.secp_ctx.sign(&sighash, &our_htlc_key), is_local_tx))
-       }
-
-       /// Signs a transaction created by build_htlc_transaction. If the transaction is an
-       /// HTLC-Success transaction (ie htlc.offered is false), preimage must be set!
-       fn sign_htlc_transaction(&self, tx: &mut Transaction, their_sig: &Signature, preimage: &Option<PaymentPreimage>, htlc: &HTLCOutputInCommitment, keys: &TxCreationKeys) -> Result<Signature, ChannelError> {
-               if tx.input.len() != 1 {
-                       panic!("Tried to sign HTLC transaction that had input count != 1!");
-               }
-               if tx.input[0].witness.len() != 0 {
-                       panic!("Tried to re-sign HTLC transaction");
-               }
-
-               let (htlc_redeemscript, our_sig, local_tx) = self.create_htlc_tx_signature(tx, htlc, keys)?;
-
-               tx.input[0].witness.push(Vec::new()); // First is the multisig dummy
-
-               if local_tx { // b, then a
-                       tx.input[0].witness.push(their_sig.serialize_der().to_vec());
-                       tx.input[0].witness.push(our_sig.serialize_der().to_vec());
-               } else {
-                       tx.input[0].witness.push(our_sig.serialize_der().to_vec());
-                       tx.input[0].witness.push(their_sig.serialize_der().to_vec());
-               }
-               tx.input[0].witness[1].push(SigHashType::All as u8);
-               tx.input[0].witness[2].push(SigHashType::All as u8);
-
-               if htlc.offered {
-                       tx.input[0].witness.push(Vec::new());
-               } else {
-                       tx.input[0].witness.push(preimage.unwrap().0.to_vec());
-               }
-
-               tx.input[0].witness.push(htlc_redeemscript.into_bytes());
-
-               Ok(our_sig)
-       }
-
-       /// Per HTLC, only one get_update_fail_htlc or get_update_fulfill_htlc call may be made.
-       /// In such cases we debug_assert!(false) and return an IgnoreError. Thus, will always return
-       /// Ok(_) if debug assertions are turned on and preconditions are met.
-       fn get_update_fulfill_htlc(&mut self, htlc_id_arg: u64, payment_preimage_arg: PaymentPreimage) -> Result<(Option<msgs::UpdateFulfillHTLC>, Option<ChannelMonitor>), ChannelError> {
-               // Either ChannelFunded got set (which means it won't be unset) or there is no way any
-               // caller thought we could have something claimed (cause we wouldn't have accepted in an
-               // incoming HTLC anyway). If we got to ShutdownComplete, callers aren't allowed to call us,
-               // either.
-               if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
-                       panic!("Was asked to fulfill an HTLC when channel was not in an operational state");
-               }
-               assert_eq!(self.channel_state & ChannelState::ShutdownComplete as u32, 0);
-
-               let payment_hash_calc = PaymentHash(Sha256::hash(&payment_preimage_arg.0[..]).into_inner());
-
-               // ChannelManager may generate duplicate claims/fails due to HTLC update events from
-               // on-chain ChannelsMonitors during block rescan. Ideally we'd figure out a way to drop
-               // these, but for now we just have to treat them as normal.
-
-               let mut pending_idx = std::usize::MAX;
-               for (idx, htlc) in self.pending_inbound_htlcs.iter().enumerate() {
-                       if htlc.htlc_id == htlc_id_arg {
-                               assert_eq!(htlc.payment_hash, payment_hash_calc);
-                               match htlc.state {
-                                       InboundHTLCState::Committed => {},
-                                       InboundHTLCState::LocalRemoved(ref reason) => {
-                                               if let &InboundHTLCRemovalReason::Fulfill(_) = reason {
-                                               } else {
-                                                       log_warn!(self, "Have preimage and want to fulfill HTLC with payment hash {} we already failed against channel {}", log_bytes!(htlc.payment_hash.0), log_bytes!(self.channel_id()));
-                                               }
-                                               return Ok((None, None));
-                                       },
-                                       _ => {
-                                               debug_assert!(false, "Have an inbound HTLC we tried to claim before it was fully committed to");
-                                               // Don't return in release mode here so that we can update channel_monitor
-                                       }
-                               }
-                               pending_idx = idx;
-                               break;
-                       }
-               }
-               if pending_idx == std::usize::MAX {
-                       return Err(ChannelError::Ignore("Unable to find a pending HTLC which matched the given HTLC ID"));
-               }
-
-               // Now update local state:
-               //
-               // We have to put the payment_preimage in the channel_monitor right away here to ensure we
-               // can claim it even if the channel hits the chain before we see their next commitment.
-               self.channel_monitor.provide_payment_preimage(&payment_hash_calc, &payment_preimage_arg);
-
-               if (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32 | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateFailed as u32)) != 0 {
-                       for pending_update in self.holding_cell_htlc_updates.iter() {
-                               match pending_update {
-                                       &HTLCUpdateAwaitingACK::ClaimHTLC { htlc_id, .. } => {
-                                               if htlc_id_arg == htlc_id {
-                                                       return Ok((None, None));
-                                               }
-                                       },
-                                       &HTLCUpdateAwaitingACK::FailHTLC { htlc_id, .. } => {
-                                               if htlc_id_arg == htlc_id {
-                                                       log_warn!(self, "Have preimage and want to fulfill HTLC with pending failure against channel {}", log_bytes!(self.channel_id()));
-                                                       // TODO: We may actually be able to switch to a fulfill here, though its
-                                                       // rare enough it may not be worth the complexity burden.
-                                                       return Ok((None, Some(self.channel_monitor.clone())));
-                                               }
-                                       },
-                                       _ => {}
-                               }
-                       }
-                       log_trace!(self, "Adding HTLC claim to holding_cell! Current state: {}", self.channel_state);
-                       self.holding_cell_htlc_updates.push(HTLCUpdateAwaitingACK::ClaimHTLC {
-                               payment_preimage: payment_preimage_arg, htlc_id: htlc_id_arg,
-                       });
-                       return Ok((None, Some(self.channel_monitor.clone())));
-               }
-
-               {
-                       let htlc = &mut self.pending_inbound_htlcs[pending_idx];
-                       if let InboundHTLCState::Committed = htlc.state {
-                       } else {
-                               debug_assert!(false, "Have an inbound HTLC we tried to claim before it was fully committed to");
-                               return Ok((None, Some(self.channel_monitor.clone())));
-                       }
-                       log_trace!(self, "Upgrading HTLC {} to LocalRemoved with a Fulfill!", log_bytes!(htlc.payment_hash.0));
-                       htlc.state = InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::Fulfill(payment_preimage_arg.clone()));
-               }
-
-               Ok((Some(msgs::UpdateFulfillHTLC {
-                       channel_id: self.channel_id(),
-                       htlc_id: htlc_id_arg,
-                       payment_preimage: payment_preimage_arg,
-               }), Some(self.channel_monitor.clone())))
-       }
-
-       pub fn get_update_fulfill_htlc_and_commit(&mut self, htlc_id: u64, payment_preimage: PaymentPreimage) -> Result<(Option<(msgs::UpdateFulfillHTLC, msgs::CommitmentSigned)>, Option<ChannelMonitor>), ChannelError> {
-               match self.get_update_fulfill_htlc(htlc_id, payment_preimage)? {
-                       (Some(update_fulfill_htlc), _) => {
-                               let (commitment, monitor_update) = self.send_commitment_no_status_check()?;
-                               Ok((Some((update_fulfill_htlc, commitment)), Some(monitor_update)))
-                       },
-                       (None, Some(channel_monitor)) => Ok((None, Some(channel_monitor))),
-                       (None, None) => Ok((None, None))
-               }
-       }
-
-       /// Per HTLC, only one get_update_fail_htlc or get_update_fulfill_htlc call may be made.
-       /// In such cases we debug_assert!(false) and return an IgnoreError. Thus, will always return
-       /// Ok(_) if debug assertions are turned on and preconditions are met.
-       pub fn get_update_fail_htlc(&mut self, htlc_id_arg: u64, err_packet: msgs::OnionErrorPacket) -> Result<Option<msgs::UpdateFailHTLC>, ChannelError> {
-               if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
-                       panic!("Was asked to fail an HTLC when channel was not in an operational state");
-               }
-               assert_eq!(self.channel_state & ChannelState::ShutdownComplete as u32, 0);
-
-               // ChannelManager may generate duplicate claims/fails due to HTLC update events from
-               // on-chain ChannelsMonitors during block rescan. Ideally we'd figure out a way to drop
-               // these, but for now we just have to treat them as normal.
-
-               let mut pending_idx = std::usize::MAX;
-               for (idx, htlc) in self.pending_inbound_htlcs.iter().enumerate() {
-                       if htlc.htlc_id == htlc_id_arg {
-                               match htlc.state {
-                                       InboundHTLCState::Committed => {},
-                                       InboundHTLCState::LocalRemoved(_) => {
-                                               return Ok(None);
-                                       },
-                                       _ => {
-                                               debug_assert!(false, "Have an inbound HTLC we tried to claim before it was fully committed to");
-                                               return Err(ChannelError::Ignore("Unable to find a pending HTLC which matched the given HTLC ID"));
-                                       }
-                               }
-                               pending_idx = idx;
-                       }
-               }
-               if pending_idx == std::usize::MAX {
-                       return Err(ChannelError::Ignore("Unable to find a pending HTLC which matched the given HTLC ID"));
-               }
-
-               // Now update local state:
-               if (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32 | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateFailed as u32)) != 0 {
-                       for pending_update in self.holding_cell_htlc_updates.iter() {
-                               match pending_update {
-                                       &HTLCUpdateAwaitingACK::ClaimHTLC { htlc_id, .. } => {
-                                               if htlc_id_arg == htlc_id {
-                                                       return Err(ChannelError::Ignore("Unable to find a pending HTLC which matched the given HTLC ID"));
-                                               }
-                                       },
-                                       &HTLCUpdateAwaitingACK::FailHTLC { htlc_id, .. } => {
-                                               if htlc_id_arg == htlc_id {
-                                                       return Err(ChannelError::Ignore("Unable to find a pending HTLC which matched the given HTLC ID"));
-                                               }
-                                       },
-                                       _ => {}
-                               }
-                       }
-                       self.holding_cell_htlc_updates.push(HTLCUpdateAwaitingACK::FailHTLC {
-                               htlc_id: htlc_id_arg,
-                               err_packet,
-                       });
-                       return Ok(None);
-               }
-
-               {
-                       let htlc = &mut self.pending_inbound_htlcs[pending_idx];
-                       htlc.state = InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::FailRelay(err_packet.clone()));
-               }
-
-               Ok(Some(msgs::UpdateFailHTLC {
-                       channel_id: self.channel_id(),
-                       htlc_id: htlc_id_arg,
-                       reason: err_packet
-               }))
-       }
-
-       // Message handlers:
-
-       pub fn accept_channel(&mut self, msg: &msgs::AcceptChannel, config: &UserConfig, their_local_features: LocalFeatures) -> Result<(), ChannelError> {
-               // Check sanity of message fields:
-               if !self.channel_outbound {
-                       return Err(ChannelError::Close("Got an accept_channel message from an inbound peer"));
-               }
-               if self.channel_state != ChannelState::OurInitSent as u32 {
-                       return Err(ChannelError::Close("Got an accept_channel message at a strange time"));
-               }
-               if msg.dust_limit_satoshis > 21000000 * 100000000 {
-                       return Err(ChannelError::Close("Peer never wants payout outputs?"));
-               }
-               if msg.channel_reserve_satoshis > self.channel_value_satoshis {
-                       return Err(ChannelError::Close("Bogus channel_reserve_satoshis"));
-               }
-               if msg.dust_limit_satoshis > msg.channel_reserve_satoshis {
-                       return Err(ChannelError::Close("Bogus channel_reserve and dust_limit"));
-               }
-               if msg.channel_reserve_satoshis < self.our_dust_limit_satoshis {
-                       return Err(ChannelError::Close("Peer never wants payout outputs?"));
-               }
-               if msg.dust_limit_satoshis > Channel::get_our_channel_reserve_satoshis(self.channel_value_satoshis) {
-                       return Err(ChannelError::Close("Dust limit is bigger than our channel reverse"));
-               }
-               if msg.htlc_minimum_msat >= (self.channel_value_satoshis - msg.channel_reserve_satoshis) * 1000 {
-                       return Err(ChannelError::Close("Minimum htlc value is full channel value"));
-               }
-               if msg.to_self_delay > config.peer_channel_config_limits.their_to_self_delay || msg.to_self_delay > MAX_LOCAL_BREAKDOWN_TIMEOUT {
-                       return Err(ChannelError::Close("They wanted our payments to be delayed by a needlessly long period"));
-               }
-               if msg.max_accepted_htlcs < 1 {
-                       return Err(ChannelError::Close("0 max_accepted_htlcs makes for a useless channel"));
-               }
-               if msg.max_accepted_htlcs > 483 {
-                       return Err(ChannelError::Close("max_accepted_htlcs > 483"));
-               }
-
-               // Now check against optional parameters as set by config...
-               if msg.htlc_minimum_msat > config.peer_channel_config_limits.max_htlc_minimum_msat {
-                       return Err(ChannelError::Close("htlc minimum msat is higher than the user specified limit"));
-               }
-               if msg.max_htlc_value_in_flight_msat < config.peer_channel_config_limits.min_max_htlc_value_in_flight_msat {
-                       return Err(ChannelError::Close("max htlc value in flight msat is less than the user specified limit"));
-               }
-               if msg.channel_reserve_satoshis > config.peer_channel_config_limits.max_channel_reserve_satoshis {
-                       return Err(ChannelError::Close("channel reserve satoshis is higher than the user specified limit"));
-               }
-               if msg.max_accepted_htlcs < config.peer_channel_config_limits.min_max_accepted_htlcs {
-                       return Err(ChannelError::Close("max accepted htlcs is less than the user specified limit"));
-               }
-               if msg.dust_limit_satoshis < config.peer_channel_config_limits.min_dust_limit_satoshis {
-                       return Err(ChannelError::Close("dust limit satoshis is less than the user specified limit"));
-               }
-               if msg.dust_limit_satoshis > config.peer_channel_config_limits.max_dust_limit_satoshis {
-                       return Err(ChannelError::Close("dust limit satoshis is greater than the user specified limit"));
-               }
-               if msg.minimum_depth > config.peer_channel_config_limits.max_minimum_depth {
-                       return Err(ChannelError::Close("We consider the minimum depth to be unreasonably large"));
-               }
-
-               let their_shutdown_scriptpubkey = if their_local_features.supports_upfront_shutdown_script() {
-                       match &msg.shutdown_scriptpubkey {
-                               &OptionalField::Present(ref script) => {
-                                       // Peer is signaling upfront_shutdown and has provided a non-accepted scriptpubkey format. We enforce it while receiving shutdown msg
-                                       if script.is_p2pkh() || script.is_p2sh() || script.is_v0_p2wsh() || script.is_v0_p2wpkh() {
-                                               Some(script.clone())
-                                       // Peer is signaling upfront_shutdown and has opt-out with a 0-length script. We don't enforce anything
-                                       } else if script.len() == 0 {
-                                               None
-                                       // Peer is signaling upfront_shutdown and has provided a non-accepted scriptpubkey format. Fail the channel
-                                       } else {
-                                               return Err(ChannelError::Close("Peer is signaling upfront_shutdown but has provided a non-accepted scriptpubkey format"));
-                                       }
-                               },
-                               // Peer is signaling upfront shutdown but don't opt-out with correct mechanism (a.k.a 0-length script). Peer looks buggy, we fail the channel
-                               &OptionalField::Absent => {
-                                       return Err(ChannelError::Close("Peer is signaling upfront_shutdown but we don't get any script. Use 0-length script to opt-out"));
-                               }
-                       }
-               } else { None };
-
-               self.channel_monitor.set_their_base_keys(&msg.htlc_basepoint, &msg.delayed_payment_basepoint);
-
-               self.their_dust_limit_satoshis = msg.dust_limit_satoshis;
-               self.their_max_htlc_value_in_flight_msat = cmp::min(msg.max_htlc_value_in_flight_msat, self.channel_value_satoshis * 1000);
-               self.their_channel_reserve_satoshis = msg.channel_reserve_satoshis;
-               self.their_htlc_minimum_msat = msg.htlc_minimum_msat;
-               self.their_to_self_delay = msg.to_self_delay;
-               self.their_max_accepted_htlcs = msg.max_accepted_htlcs;
-               self.minimum_depth = msg.minimum_depth;
-               self.their_funding_pubkey = Some(msg.funding_pubkey);
-               self.their_revocation_basepoint = Some(msg.revocation_basepoint);
-               self.their_payment_basepoint = Some(msg.payment_basepoint);
-               self.their_delayed_payment_basepoint = Some(msg.delayed_payment_basepoint);
-               self.their_htlc_basepoint = Some(msg.htlc_basepoint);
-               self.their_cur_commitment_point = Some(msg.first_per_commitment_point);
-               self.their_shutdown_scriptpubkey = their_shutdown_scriptpubkey;
-
-               let obscure_factor = self.get_commitment_transaction_number_obscure_factor();
-               self.channel_monitor.set_commitment_obscure_factor(obscure_factor);
-               self.channel_monitor.set_their_to_self_delay(msg.to_self_delay);
-
-               self.channel_state = ChannelState::OurInitSent as u32 | ChannelState::TheirInitSent as u32;
-
-               Ok(())
-       }
-
-       fn funding_created_signature(&mut self, sig: &Signature) -> Result<(Transaction, Transaction, Signature, TxCreationKeys), ChannelError> {
-               let funding_script = self.get_funding_redeemscript();
-
-               let local_keys = self.build_local_transaction_keys(self.cur_local_commitment_transaction_number)?;
-               let mut local_initial_commitment_tx = self.build_commitment_transaction(self.cur_local_commitment_transaction_number, &local_keys, true, false, self.feerate_per_kw).0;
-               let local_sighash = hash_to_message!(&bip143::SighashComponents::new(&local_initial_commitment_tx).sighash_all(&local_initial_commitment_tx.input[0], &funding_script, self.channel_value_satoshis)[..]);
-
-               // They sign the "local" commitment transaction...
-               secp_check!(self.secp_ctx.verify(&local_sighash, &sig, &self.their_funding_pubkey.unwrap()), "Invalid funding_created signature from peer");
-
-               // ...and we sign it, allowing us to broadcast the tx if we wish
-               self.sign_commitment_transaction(&mut local_initial_commitment_tx, sig);
-
-               let remote_keys = self.build_remote_transaction_keys()?;
-               let remote_initial_commitment_tx = self.build_commitment_transaction(self.cur_remote_commitment_transaction_number, &remote_keys, false, false, self.feerate_per_kw).0;
-               let remote_sighash = hash_to_message!(&bip143::SighashComponents::new(&remote_initial_commitment_tx).sighash_all(&remote_initial_commitment_tx.input[0], &funding_script, self.channel_value_satoshis)[..]);
-
-               // We sign the "remote" commitment transaction, allowing them to broadcast the tx if they wish.
-               Ok((remote_initial_commitment_tx, local_initial_commitment_tx, self.secp_ctx.sign(&remote_sighash, &self.local_keys.funding_key), local_keys))
-       }
-
-       pub fn funding_created(&mut self, msg: &msgs::FundingCreated) -> Result<(msgs::FundingSigned, ChannelMonitor), ChannelError> {
-               if self.channel_outbound {
-                       return Err(ChannelError::Close("Received funding_created for an outbound channel?"));
-               }
-               if self.channel_state != (ChannelState::OurInitSent as u32 | ChannelState::TheirInitSent as u32) {
-                       // BOLT 2 says that if we disconnect before we send funding_signed we SHOULD NOT
-                       // remember the channel, so it's safe to just send an error_message here and drop the
-                       // channel.
-                       return Err(ChannelError::Close("Received funding_created after we got the channel!"));
-               }
-               if self.channel_monitor.get_min_seen_secret() != (1 << 48) ||
-                               self.cur_remote_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER ||
-                               self.cur_local_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
-                       panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
-               }
-
-               let funding_txo = OutPoint::new(msg.funding_txid, msg.funding_output_index);
-               let funding_txo_script = self.get_funding_redeemscript().to_v0_p2wsh();
-               self.channel_monitor.set_funding_info((funding_txo, funding_txo_script));
-
-               let (remote_initial_commitment_tx, local_initial_commitment_tx, our_signature, local_keys) = match self.funding_created_signature(&msg.signature) {
-                       Ok(res) => res,
-                       Err(e) => {
-                               self.channel_monitor.unset_funding_info();
-                               return Err(e);
-                       }
-               };
-
-               // Now that we're past error-generating stuff, update our local state:
-
-               self.channel_monitor.provide_latest_remote_commitment_tx_info(&remote_initial_commitment_tx, Vec::new(), self.cur_remote_commitment_transaction_number, self.their_cur_commitment_point.unwrap());
-               self.last_local_commitment_txn = vec![local_initial_commitment_tx.clone()];
-               self.channel_monitor.provide_latest_local_commitment_tx_info(local_initial_commitment_tx, local_keys, self.feerate_per_kw, Vec::new());
-               self.channel_state = ChannelState::FundingSent as u32;
-               self.channel_id = funding_txo.to_channel_id();
-               self.cur_remote_commitment_transaction_number -= 1;
-               self.cur_local_commitment_transaction_number -= 1;
-
-               Ok((msgs::FundingSigned {
-                       channel_id: self.channel_id,
-                       signature: our_signature
-               }, self.channel_monitor.clone()))
-       }
-
-       /// Handles a funding_signed message from the remote end.
-       /// If this call is successful, broadcast the funding transaction (and not before!)
-       pub fn funding_signed(&mut self, msg: &msgs::FundingSigned) -> Result<ChannelMonitor, ChannelError> {
-               if !self.channel_outbound {
-                       return Err(ChannelError::Close("Received funding_signed for an inbound channel?"));
-               }
-               if self.channel_state & !(ChannelState::MonitorUpdateFailed as u32) != ChannelState::FundingCreated as u32 {
-                       return Err(ChannelError::Close("Received funding_signed in strange state!"));
-               }
-               if self.channel_monitor.get_min_seen_secret() != (1 << 48) ||
-                               self.cur_remote_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER - 1 ||
-                               self.cur_local_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
-                       panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
-               }
-
-               let funding_script = self.get_funding_redeemscript();
-
-               let local_keys = self.build_local_transaction_keys(self.cur_local_commitment_transaction_number)?;
-               let mut local_initial_commitment_tx = self.build_commitment_transaction(self.cur_local_commitment_transaction_number, &local_keys, true, false, self.feerate_per_kw).0;
-               let local_sighash = hash_to_message!(&bip143::SighashComponents::new(&local_initial_commitment_tx).sighash_all(&local_initial_commitment_tx.input[0], &funding_script, self.channel_value_satoshis)[..]);
-
-               // They sign the "local" commitment transaction, allowing us to broadcast the tx if we wish.
-               secp_check!(self.secp_ctx.verify(&local_sighash, &msg.signature, &self.their_funding_pubkey.unwrap()), "Invalid funding_signed signature from peer");
-
-               self.sign_commitment_transaction(&mut local_initial_commitment_tx, &msg.signature);
-               self.channel_monitor.provide_latest_local_commitment_tx_info(local_initial_commitment_tx.clone(), local_keys, self.feerate_per_kw, Vec::new());
-               self.last_local_commitment_txn = vec![local_initial_commitment_tx];
-               self.channel_state = ChannelState::FundingSent as u32 | (self.channel_state & (ChannelState::MonitorUpdateFailed as u32));
-               self.cur_local_commitment_transaction_number -= 1;
-
-               if self.channel_state & (ChannelState::MonitorUpdateFailed as u32) == 0 {
-                       Ok(self.channel_monitor.clone())
-               } else {
-                       Err(ChannelError::Ignore("Previous monitor update failure prevented funding_signed from allowing funding broadcast"))
-               }
-       }
-
-       pub fn funding_locked(&mut self, msg: &msgs::FundingLocked) -> Result<(), ChannelError> {
-               if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
-                       return Err(ChannelError::Close("Peer sent funding_locked when we needed a channel_reestablish"));
-               }
-
-               let non_shutdown_state = self.channel_state & (!MULTI_STATE_FLAGS);
-
-               if non_shutdown_state == ChannelState::FundingSent as u32 {
-                       self.channel_state |= ChannelState::TheirFundingLocked as u32;
-               } else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::OurFundingLocked as u32) {
-                       self.channel_state = ChannelState::ChannelFunded as u32 | (self.channel_state & MULTI_STATE_FLAGS);
-                       self.channel_update_count += 1;
-               } else if (self.channel_state & (ChannelState::ChannelFunded as u32) != 0 &&
-                                // Note that funding_signed/funding_created will have decremented both by 1!
-                                self.cur_local_commitment_transaction_number == INITIAL_COMMITMENT_NUMBER - 1 &&
-                                self.cur_remote_commitment_transaction_number == INITIAL_COMMITMENT_NUMBER - 1) ||
-                               // If we reconnected before sending our funding locked they may still resend theirs:
-                               (self.channel_state & (ChannelState::FundingSent as u32 | ChannelState::TheirFundingLocked as u32) ==
-                                                     (ChannelState::FundingSent as u32 | ChannelState::TheirFundingLocked as u32)) {
-                       if self.their_cur_commitment_point != Some(msg.next_per_commitment_point) {
-                               return Err(ChannelError::Close("Peer sent a reconnect funding_locked with a different point"));
-                       }
-                       // They probably disconnected/reconnected and re-sent the funding_locked, which is required
-                       return Ok(());
-               } else {
-                       return Err(ChannelError::Close("Peer sent a funding_locked at a strange time"));
-               }
-
-               self.their_prev_commitment_point = self.their_cur_commitment_point;
-               self.their_cur_commitment_point = Some(msg.next_per_commitment_point);
-               Ok(())
-       }
-
-       /// Returns (inbound_htlc_count, htlc_inbound_value_msat)
-       fn get_inbound_pending_htlc_stats(&self) -> (u32, u64) {
-               let mut htlc_inbound_value_msat = 0;
-               for ref htlc in self.pending_inbound_htlcs.iter() {
-                       htlc_inbound_value_msat += htlc.amount_msat;
-               }
-               (self.pending_inbound_htlcs.len() as u32, htlc_inbound_value_msat)
-       }
-
-       /// Returns (outbound_htlc_count, htlc_outbound_value_msat) *including* pending adds in our
-       /// holding cell.
-       fn get_outbound_pending_htlc_stats(&self) -> (u32, u64) {
-               let mut htlc_outbound_value_msat = 0;
-               for ref htlc in self.pending_outbound_htlcs.iter() {
-                       htlc_outbound_value_msat += htlc.amount_msat;
-               }
-
-               let mut htlc_outbound_count = self.pending_outbound_htlcs.len();
-               for update in self.holding_cell_htlc_updates.iter() {
-                       if let &HTLCUpdateAwaitingACK::AddHTLC { ref amount_msat, .. } = update {
-                               htlc_outbound_count += 1;
-                               htlc_outbound_value_msat += amount_msat;
-                       }
-               }
-
-               (htlc_outbound_count as u32, htlc_outbound_value_msat)
-       }
-
-       /// Get the available (ie not including pending HTLCs) inbound and outbound balance in msat.
-       /// Doesn't bother handling the
-       /// if-we-removed-it-already-but-haven't-fully-resolved-they-can-still-send-an-inbound-HTLC
-       /// corner case properly.
-       pub fn get_inbound_outbound_available_balance_msat(&self) -> (u64, u64) {
-               // Note that we have to handle overflow due to the above case.
-               (cmp::min(self.channel_value_satoshis as i64 * 1000 - self.value_to_self_msat as i64 - self.get_inbound_pending_htlc_stats().1 as i64, 0) as u64,
-               cmp::min(self.value_to_self_msat as i64 - self.get_outbound_pending_htlc_stats().1 as i64, 0) as u64)
-       }
-
-       pub fn update_add_htlc(&mut self, msg: &msgs::UpdateAddHTLC, pending_forward_state: PendingHTLCStatus) -> Result<(), ChannelError> {
-               if (self.channel_state & (ChannelState::ChannelFunded as u32 | ChannelState::RemoteShutdownSent as u32)) != (ChannelState::ChannelFunded as u32) {
-                       return Err(ChannelError::Close("Got add HTLC message when channel was not in an operational state"));
-               }
-               if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
-                       return Err(ChannelError::Close("Peer sent update_add_htlc when we needed a channel_reestablish"));
-               }
-               if msg.amount_msat > self.channel_value_satoshis * 1000 {
-                       return Err(ChannelError::Close("Remote side tried to send more than the total value of the channel"));
-               }
-               if msg.amount_msat < self.our_htlc_minimum_msat {
-                       return Err(ChannelError::Close("Remote side tried to send less than our minimum HTLC value"));
-               }
-
-               let (inbound_htlc_count, htlc_inbound_value_msat) = self.get_inbound_pending_htlc_stats();
-               if inbound_htlc_count + 1 > OUR_MAX_HTLCS as u32 {
-                       return Err(ChannelError::Close("Remote tried to push more than our max accepted HTLCs"));
-               }
-               // Check our_max_htlc_value_in_flight_msat
-               if htlc_inbound_value_msat + msg.amount_msat > Channel::get_our_max_htlc_value_in_flight_msat(self.channel_value_satoshis) {
-                       return Err(ChannelError::Close("Remote HTLC add would put them over our max HTLC value"));
-               }
-               // Check our_channel_reserve_satoshis (we're getting paid, so they have to at least meet
-               // the reserve_satoshis we told them to always have as direct payment so that they lose
-               // something if we punish them for broadcasting an old state).
-               // Note that we don't really care about having a small/no to_remote output in our local
-               // commitment transactions, as the purpose of the channel reserve is to ensure we can
-               // punish *them* if they misbehave, so we discount any outbound HTLCs which will not be
-               // present in the next commitment transaction we send them (at least for fulfilled ones,
-               // failed ones won't modify value_to_self).
-               // Note that we will send HTLCs which another instance of rust-lightning would think
-               // violate the reserve value if we do not do this (as we forget inbound HTLCs from the
-               // Channel state once they will not be present in the next received commitment
-               // transaction).
-               let mut removed_outbound_total_msat = 0;
-               for ref htlc in self.pending_outbound_htlcs.iter() {
-                       if let OutboundHTLCState::AwaitingRemoteRevokeToRemove(None) = htlc.state {
-                               removed_outbound_total_msat += htlc.amount_msat;
-                       } else if let OutboundHTLCState::AwaitingRemovedRemoteRevoke(None) = htlc.state {
-                               removed_outbound_total_msat += htlc.amount_msat;
-                       }
-               }
-               if htlc_inbound_value_msat + msg.amount_msat + self.value_to_self_msat > (self.channel_value_satoshis - Channel::get_our_channel_reserve_satoshis(self.channel_value_satoshis)) * 1000 + removed_outbound_total_msat {
-                       return Err(ChannelError::Close("Remote HTLC add would put them over their reserve value"));
-               }
-               if self.next_remote_htlc_id != msg.htlc_id {
-                       return Err(ChannelError::Close("Remote skipped HTLC ID"));
-               }
-               if msg.cltv_expiry >= 500000000 {
-                       return Err(ChannelError::Close("Remote provided CLTV expiry in seconds instead of block height"));
-               }
-
-               //TODO: Check msg.cltv_expiry further? Do this in channel manager?
-
-               if self.channel_state & ChannelState::LocalShutdownSent as u32 != 0 {
-                       if let PendingHTLCStatus::Forward(_) = pending_forward_state {
-                               panic!("ChannelManager shouldn't be trying to add a forwardable HTLC after we've started closing");
-                       }
-               }
-
-               // Now update local state:
-               self.next_remote_htlc_id += 1;
-               self.pending_inbound_htlcs.push(InboundHTLCOutput {
-                       htlc_id: msg.htlc_id,
-                       amount_msat: msg.amount_msat,
-                       payment_hash: msg.payment_hash,
-                       cltv_expiry: msg.cltv_expiry,
-                       state: InboundHTLCState::RemoteAnnounced(pending_forward_state),
-               });
-               Ok(())
-       }
-
-       /// Marks an outbound HTLC which we have received update_fail/fulfill/malformed
-       #[inline]
-       fn mark_outbound_htlc_removed(&mut self, htlc_id: u64, check_preimage: Option<PaymentHash>, fail_reason: Option<HTLCFailReason>) -> Result<&HTLCSource, ChannelError> {
-               for htlc in self.pending_outbound_htlcs.iter_mut() {
-                       if htlc.htlc_id == htlc_id {
-                               match check_preimage {
-                                       None => {},
-                                       Some(payment_hash) =>
-                                               if payment_hash != htlc.payment_hash {
-                                                       return Err(ChannelError::Close("Remote tried to fulfill HTLC with an incorrect preimage"));
-                                               }
-                               };
-                               match htlc.state {
-                                       OutboundHTLCState::LocalAnnounced(_) =>
-                                               return Err(ChannelError::Close("Remote tried to fulfill/fail HTLC before it had been committed")),
-                                       OutboundHTLCState::Committed => {
-                                               htlc.state = OutboundHTLCState::RemoteRemoved(fail_reason);
-                                       },
-                                       OutboundHTLCState::AwaitingRemoteRevokeToRemove(_) | OutboundHTLCState::AwaitingRemovedRemoteRevoke(_) | OutboundHTLCState::RemoteRemoved(_) =>
-                                               return Err(ChannelError::Close("Remote tried to fulfill/fail HTLC that they'd already fulfilled/failed")),
-                               }
-                               return Ok(&htlc.source);
-                       }
-               }
-               Err(ChannelError::Close("Remote tried to fulfill/fail an HTLC we couldn't find"))
-       }
-
-       pub fn update_fulfill_htlc(&mut self, msg: &msgs::UpdateFulfillHTLC) -> Result<HTLCSource, ChannelError> {
-               if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
-                       return Err(ChannelError::Close("Got fulfill HTLC message when channel was not in an operational state"));
-               }
-               if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
-                       return Err(ChannelError::Close("Peer sent update_fulfill_htlc when we needed a channel_reestablish"));
-               }
-
-               let payment_hash = PaymentHash(Sha256::hash(&msg.payment_preimage.0[..]).into_inner());
-               self.mark_outbound_htlc_removed(msg.htlc_id, Some(payment_hash), None).map(|source| source.clone())
-       }
-
-       pub fn update_fail_htlc(&mut self, msg: &msgs::UpdateFailHTLC, fail_reason: HTLCFailReason) -> Result<(), ChannelError> {
-               if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
-                       return Err(ChannelError::Close("Got fail HTLC message when channel was not in an operational state"));
-               }
-               if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
-                       return Err(ChannelError::Close("Peer sent update_fail_htlc when we needed a channel_reestablish"));
-               }
-
-               self.mark_outbound_htlc_removed(msg.htlc_id, None, Some(fail_reason))?;
-               Ok(())
-       }
-
-       pub fn update_fail_malformed_htlc<'a>(&mut self, msg: &msgs::UpdateFailMalformedHTLC, fail_reason: HTLCFailReason) -> Result<(), ChannelError> {
-               if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
-                       return Err(ChannelError::Close("Got fail malformed HTLC message when channel was not in an operational state"));
-               }
-               if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
-                       return Err(ChannelError::Close("Peer sent update_fail_malformed_htlc when we needed a channel_reestablish"));
-               }
-
-               self.mark_outbound_htlc_removed(msg.htlc_id, None, Some(fail_reason))?;
-               Ok(())
-       }
-
-       pub fn commitment_signed(&mut self, msg: &msgs::CommitmentSigned, fee_estimator: &FeeEstimator) -> Result<(msgs::RevokeAndACK, Option<msgs::CommitmentSigned>, Option<msgs::ClosingSigned>, ChannelMonitor), ChannelError> {
-               if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
-                       return Err(ChannelError::Close("Got commitment signed message when channel was not in an operational state"));
-               }
-               if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
-                       return Err(ChannelError::Close("Peer sent commitment_signed when we needed a channel_reestablish"));
-               }
-               if self.channel_state & BOTH_SIDES_SHUTDOWN_MASK == BOTH_SIDES_SHUTDOWN_MASK && self.last_sent_closing_fee.is_some() {
-                       return Err(ChannelError::Close("Peer sent commitment_signed after we'd started exchanging closing_signeds"));
-               }
-
-               let funding_script = self.get_funding_redeemscript();
-
-               let local_keys = self.build_local_transaction_keys(self.cur_local_commitment_transaction_number)?;
-
-               let mut update_fee = false;
-               let feerate_per_kw = if !self.channel_outbound && self.pending_update_fee.is_some() {
-                       update_fee = true;
-                       self.pending_update_fee.unwrap()
-               } else {
-                       self.feerate_per_kw
-               };
-
-               let mut local_commitment_tx = {
-                       let mut commitment_tx = self.build_commitment_transaction(self.cur_local_commitment_transaction_number, &local_keys, true, false, feerate_per_kw);
-                       let htlcs_cloned: Vec<_> = commitment_tx.2.drain(..).map(|htlc| (htlc.0, htlc.1.map(|h| h.clone()))).collect();
-                       (commitment_tx.0, commitment_tx.1, htlcs_cloned)
-               };
-               let local_commitment_txid = local_commitment_tx.0.txid();
-               let local_sighash = hash_to_message!(&bip143::SighashComponents::new(&local_commitment_tx.0).sighash_all(&local_commitment_tx.0.input[0], &funding_script, self.channel_value_satoshis)[..]);
-               log_trace!(self, "Checking commitment tx signature {} by key {} against tx {} with redeemscript {}", log_bytes!(msg.signature.serialize_compact()[..]), log_bytes!(self.their_funding_pubkey.unwrap().serialize()), encode::serialize_hex(&local_commitment_tx.0), encode::serialize_hex(&funding_script));
-               secp_check!(self.secp_ctx.verify(&local_sighash, &msg.signature, &self.their_funding_pubkey.unwrap()), "Invalid commitment tx signature from peer");
-
-               //If channel fee was updated by funder confirm funder can afford the new fee rate when applied to the current local commitment transaction
-               if update_fee {
-                       let num_htlcs = local_commitment_tx.1;
-                       let total_fee: u64 = feerate_per_kw as u64 * (COMMITMENT_TX_BASE_WEIGHT + (num_htlcs as u64) * COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000;
-
-                       if self.channel_value_satoshis - self.value_to_self_msat / 1000 < total_fee + self.their_channel_reserve_satoshis {
-                               return Err(ChannelError::Close("Funding remote cannot afford proposed new fee"));
-                       }
-               }
-
-               if msg.htlc_signatures.len() != local_commitment_tx.1 {
-                       return Err(ChannelError::Close("Got wrong number of HTLC signatures from remote"));
-               }
-
-               let mut new_local_commitment_txn = Vec::with_capacity(local_commitment_tx.1 + 1);
-               self.sign_commitment_transaction(&mut local_commitment_tx.0, &msg.signature);
-               new_local_commitment_txn.push(local_commitment_tx.0.clone());
-
-               let mut htlcs_and_sigs = Vec::with_capacity(local_commitment_tx.2.len());
-               for (idx, (htlc, source)) in local_commitment_tx.2.drain(..).enumerate() {
-                       if let Some(_) = htlc.transaction_output_index {
-                               let mut htlc_tx = self.build_htlc_transaction(&local_commitment_txid, &htlc, true, &local_keys, feerate_per_kw);
-                               let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &local_keys);
-                               log_trace!(self, "Checking HTLC tx signature {} by key {} against tx {} with redeemscript {}", log_bytes!(msg.htlc_signatures[idx].serialize_compact()[..]), log_bytes!(local_keys.b_htlc_key.serialize()), encode::serialize_hex(&htlc_tx), encode::serialize_hex(&htlc_redeemscript));
-                               let htlc_sighash = hash_to_message!(&bip143::SighashComponents::new(&htlc_tx).sighash_all(&htlc_tx.input[0], &htlc_redeemscript, htlc.amount_msat / 1000)[..]);
-                               secp_check!(self.secp_ctx.verify(&htlc_sighash, &msg.htlc_signatures[idx], &local_keys.b_htlc_key), "Invalid HTLC tx signature from peer");
-                               let htlc_sig = if htlc.offered {
-                                       let htlc_sig = self.sign_htlc_transaction(&mut htlc_tx, &msg.htlc_signatures[idx], &None, &htlc, &local_keys)?;
-                                       new_local_commitment_txn.push(htlc_tx);
-                                       htlc_sig
-                               } else {
-                                       self.create_htlc_tx_signature(&htlc_tx, &htlc, &local_keys)?.1
-                               };
-                               htlcs_and_sigs.push((htlc, Some((msg.htlc_signatures[idx], htlc_sig)), source));
-                       } else {
-                               htlcs_and_sigs.push((htlc, None, source));
-                       }
-               }
-
-               let next_per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &self.build_local_commitment_secret(self.cur_local_commitment_transaction_number - 1));
-               let per_commitment_secret = chan_utils::build_commitment_secret(self.local_keys.commitment_seed, self.cur_local_commitment_transaction_number + 1);
-
-               // Update state now that we've passed all the can-fail calls...
-               let mut need_our_commitment = false;
-               if !self.channel_outbound {
-                       if let Some(fee_update) = self.pending_update_fee {
-                               self.feerate_per_kw = fee_update;
-                               // We later use the presence of pending_update_fee to indicate we should generate a
-                               // commitment_signed upon receipt of revoke_and_ack, so we can only set it to None
-                               // if we're not awaiting a revoke (ie will send a commitment_signed now).
-                               if (self.channel_state & ChannelState::AwaitingRemoteRevoke as u32) == 0 {
-                                       need_our_commitment = true;
-                                       self.pending_update_fee = None;
-                               }
-                       }
-               }
-
-               self.channel_monitor.provide_latest_local_commitment_tx_info(local_commitment_tx.0, local_keys, self.feerate_per_kw, htlcs_and_sigs);
-
-               for htlc in self.pending_inbound_htlcs.iter_mut() {
-                       let new_forward = if let &InboundHTLCState::RemoteAnnounced(ref forward_info) = &htlc.state {
-                               Some(forward_info.clone())
-                       } else { None };
-                       if let Some(forward_info) = new_forward {
-                               htlc.state = InboundHTLCState::AwaitingRemoteRevokeToAnnounce(forward_info);
-                               need_our_commitment = true;
-                       }
-               }
-               for htlc in self.pending_outbound_htlcs.iter_mut() {
-                       if let Some(fail_reason) = if let &mut OutboundHTLCState::RemoteRemoved(ref mut fail_reason) = &mut htlc.state {
-                               Some(fail_reason.take())
-                       } else { None } {
-                               htlc.state = OutboundHTLCState::AwaitingRemoteRevokeToRemove(fail_reason);
-                               need_our_commitment = true;
-                       }
-               }
-
-               self.cur_local_commitment_transaction_number -= 1;
-               self.last_local_commitment_txn = new_local_commitment_txn;
-               // Note that if we need_our_commitment & !AwaitingRemoteRevoke we'll call
-               // send_commitment_no_status_check() next which will reset this to RAAFirst.
-               self.resend_order = RAACommitmentOrder::CommitmentFirst;
-
-               if (self.channel_state & ChannelState::MonitorUpdateFailed as u32) != 0 {
-                       // In case we initially failed monitor updating without requiring a response, we need
-                       // to make sure the RAA gets sent first.
-                       self.monitor_pending_revoke_and_ack = true;
-                       if need_our_commitment && (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32)) == 0 {
-                               // If we were going to send a commitment_signed after the RAA, go ahead and do all
-                               // the corresponding HTLC status updates so that get_last_commitment_update
-                               // includes the right HTLCs.
-                               // Note that this generates a monitor update that we ignore! This is OK since we
-                               // won't actually send the commitment_signed that generated the update to the other
-                               // side until the latest monitor has been pulled from us and stored.
-                               self.monitor_pending_commitment_signed = true;
-                               self.send_commitment_no_status_check()?;
-                       }
-                       // TODO: Call maybe_propose_first_closing_signed on restoration (or call it here and
-                       // re-send the message on restoration)
-                       return Err(ChannelError::Ignore("Previous monitor update failure prevented generation of RAA"));
-               }
-
-               let (our_commitment_signed, monitor_update, closing_signed) = if need_our_commitment && (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32)) == 0 {
-                       // If we're AwaitingRemoteRevoke we can't send a new commitment here, but that's ok -
-                       // we'll send one right away when we get the revoke_and_ack when we
-                       // free_holding_cell_htlcs().
-                       let (msg, monitor) = self.send_commitment_no_status_check()?;
-                       (Some(msg), monitor, None)
-               } else if !need_our_commitment {
-                       (None, self.channel_monitor.clone(), self.maybe_propose_first_closing_signed(fee_estimator))
-               } else { (None, self.channel_monitor.clone(), None) };
-
-               Ok((msgs::RevokeAndACK {
-                       channel_id: self.channel_id,
-                       per_commitment_secret: per_commitment_secret,
-                       next_per_commitment_point: next_per_commitment_point,
-               }, our_commitment_signed, closing_signed, monitor_update))
-       }
-
-       /// Used to fulfill holding_cell_htlcs when we get a remote ack (or implicitly get it by them
-       /// fulfilling or failing the last pending HTLC)
-       fn free_holding_cell_htlcs(&mut self) -> Result<Option<(msgs::CommitmentUpdate, ChannelMonitor)>, ChannelError> {
-               assert_eq!(self.channel_state & ChannelState::MonitorUpdateFailed as u32, 0);
-               if self.holding_cell_htlc_updates.len() != 0 || self.holding_cell_update_fee.is_some() {
-                       log_trace!(self, "Freeing holding cell with {} HTLC updates{}", self.holding_cell_htlc_updates.len(), if self.holding_cell_update_fee.is_some() { " and a fee update" } else { "" });
-
-                       let mut htlc_updates = Vec::new();
-                       mem::swap(&mut htlc_updates, &mut self.holding_cell_htlc_updates);
-                       let mut update_add_htlcs = Vec::with_capacity(htlc_updates.len());
-                       let mut update_fulfill_htlcs = Vec::with_capacity(htlc_updates.len());
-                       let mut update_fail_htlcs = Vec::with_capacity(htlc_updates.len());
-                       let mut err = None;
-                       for htlc_update in htlc_updates.drain(..) {
-                               // Note that this *can* fail, though it should be due to rather-rare conditions on
-                               // fee races with adding too many outputs which push our total payments just over
-                               // the limit. In case it's less rare than I anticipate, we may want to revisit
-                               // handling this case better and maybe fulfilling some of the HTLCs while attempting
-                               // to rebalance channels.
-                               if err.is_some() { // We're back to AwaitingRemoteRevoke (or are about to fail the channel)
-                                       self.holding_cell_htlc_updates.push(htlc_update);
-                               } else {
-                                       match &htlc_update {
-                                               &HTLCUpdateAwaitingACK::AddHTLC {amount_msat, cltv_expiry, ref payment_hash, ref source, ref onion_routing_packet, ..} => {
-                                                       match self.send_htlc(amount_msat, *payment_hash, cltv_expiry, source.clone(), onion_routing_packet.clone()) {
-                                                               Ok(update_add_msg_option) => update_add_htlcs.push(update_add_msg_option.unwrap()),
-                                                               Err(e) => {
-                                                                       match e {
-                                                                               ChannelError::Ignore(ref msg) => {
-                                                                                       log_info!(self, "Failed to send HTLC with payment_hash {} due to {}", log_bytes!(payment_hash.0), msg);
-                                                                               },
-                                                                               _ => {
-                                                                                       log_info!(self, "Failed to send HTLC with payment_hash {} resulting in a channel closure during holding_cell freeing", log_bytes!(payment_hash.0));
-                                                                               },
-                                                                       }
-                                                                       err = Some(e);
-                                                               }
-                                                       }
-                                               },
-                                               &HTLCUpdateAwaitingACK::ClaimHTLC { ref payment_preimage, htlc_id, .. } => {
-                                                       match self.get_update_fulfill_htlc(htlc_id, *payment_preimage) {
-                                                               Ok(update_fulfill_msg_option) => update_fulfill_htlcs.push(update_fulfill_msg_option.0.unwrap()),
-                                                               Err(e) => {
-                                                                       if let ChannelError::Ignore(_) = e {}
-                                                                       else {
-                                                                               panic!("Got a non-IgnoreError action trying to fulfill holding cell HTLC");
-                                                                       }
-                                                               }
-                                                       }
-                                               },
-                                               &HTLCUpdateAwaitingACK::FailHTLC { htlc_id, ref err_packet } => {
-                                                       match self.get_update_fail_htlc(htlc_id, err_packet.clone()) {
-                                                               Ok(update_fail_msg_option) => update_fail_htlcs.push(update_fail_msg_option.unwrap()),
-                                                               Err(e) => {
-                                                                       if let ChannelError::Ignore(_) = e {}
-                                                                       else {
-                                                                               panic!("Got a non-IgnoreError action trying to fail holding cell HTLC");
-                                                                       }
-                                                               }
-                                                       }
-                                               },
-                                       }
-                                       if err.is_some() {
-                                               self.holding_cell_htlc_updates.push(htlc_update);
-                                               if let Some(ChannelError::Ignore(_)) = err {
-                                                       // If we failed to add the HTLC, but got an Ignore error, we should
-                                                       // still send the new commitment_signed, so reset the err to None.
-                                                       err = None;
-                                               }
-                                       }
-                               }
-                       }
-                       //TODO: Need to examine the type of err - if it's a fee issue or similar we may want to
-                       //fail it back the route, if it's a temporary issue we can ignore it...
-                       match err {
-                               None => {
-                                       if update_add_htlcs.is_empty() && update_fulfill_htlcs.is_empty() && update_fail_htlcs.is_empty() && self.holding_cell_update_fee.is_none() {
-                                               // This should never actually happen and indicates we got some Errs back
-                                               // from update_fulfill_htlc/update_fail_htlc, but we handle it anyway in
-                                               // case there is some strange way to hit duplicate HTLC removes.
-                                               return Ok(None);
-                                       }
-                                       let update_fee = if let Some(feerate) = self.holding_cell_update_fee {
-                                                       self.pending_update_fee = self.holding_cell_update_fee.take();
-                                                       Some(msgs::UpdateFee {
-                                                               channel_id: self.channel_id,
-                                                               feerate_per_kw: feerate as u32,
-                                                       })
-                                               } else {
-                                                       None
-                                               };
-                                       let (commitment_signed, monitor_update) = self.send_commitment_no_status_check()?;
-                                       Ok(Some((msgs::CommitmentUpdate {
-                                               update_add_htlcs,
-                                               update_fulfill_htlcs,
-                                               update_fail_htlcs,
-                                               update_fail_malformed_htlcs: Vec::new(),
-                                               update_fee: update_fee,
-                                               commitment_signed,
-                                       }, monitor_update)))
-                               },
-                               Some(e) => Err(e)
-                       }
-               } else {
-                       Ok(None)
-               }
-       }
-
-       /// Handles receiving a remote's revoke_and_ack. Note that we may return a new
-       /// commitment_signed message here in case we had pending outbound HTLCs to add which were
-       /// waiting on this revoke_and_ack. The generation of this new commitment_signed may also fail,
-       /// generating an appropriate error *after* the channel state has been updated based on the
-       /// revoke_and_ack message.
-       pub fn revoke_and_ack(&mut self, msg: &msgs::RevokeAndACK, fee_estimator: &FeeEstimator) -> Result<(Option<msgs::CommitmentUpdate>, Vec<(PendingForwardHTLCInfo, u64)>, Vec<(HTLCSource, PaymentHash, HTLCFailReason)>, Option<msgs::ClosingSigned>, ChannelMonitor), ChannelError> {
-               if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
-                       return Err(ChannelError::Close("Got revoke/ACK message when channel was not in an operational state"));
-               }
-               if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
-                       return Err(ChannelError::Close("Peer sent revoke_and_ack when we needed a channel_reestablish"));
-               }
-               if self.channel_state & BOTH_SIDES_SHUTDOWN_MASK == BOTH_SIDES_SHUTDOWN_MASK && self.last_sent_closing_fee.is_some() {
-                       return Err(ChannelError::Close("Peer sent revoke_and_ack after we'd started exchanging closing_signeds"));
-               }
-
-               if let Some(their_prev_commitment_point) = self.their_prev_commitment_point {
-                       if PublicKey::from_secret_key(&self.secp_ctx, &secp_check!(SecretKey::from_slice(&msg.per_commitment_secret), "Peer provided an invalid per_commitment_secret")) != their_prev_commitment_point {
-                               return Err(ChannelError::Close("Got a revoke commitment secret which didn't correspond to their current pubkey"));
-                       }
-               }
-               self.channel_monitor.provide_secret(self.cur_remote_commitment_transaction_number + 1, msg.per_commitment_secret)
-                       .map_err(|e| ChannelError::Close(e.0))?;
-
-               // Update state now that we've passed all the can-fail calls...
-               // (note that we may still fail to generate the new commitment_signed message, but that's
-               // OK, we step the channel here and *then* if the new generation fails we can fail the
-               // channel based on that, but stepping stuff here should be safe either way.
-               self.channel_state &= !(ChannelState::AwaitingRemoteRevoke as u32);
-               self.their_prev_commitment_point = self.their_cur_commitment_point;
-               self.their_cur_commitment_point = Some(msg.next_per_commitment_point);
-               self.cur_remote_commitment_transaction_number -= 1;
-
-               log_trace!(self, "Updating HTLCs on receipt of RAA...");
-               let mut to_forward_infos = Vec::new();
-               let mut revoked_htlcs = Vec::new();
-               let mut update_fail_htlcs = Vec::new();
-               let mut update_fail_malformed_htlcs = Vec::new();
-               let mut require_commitment = false;
-               let mut value_to_self_msat_diff: i64 = 0;
-
-               {
-                       // Take references explicitly so that we can hold multiple references to self.
-                       let pending_inbound_htlcs: &mut Vec<_> = &mut self.pending_inbound_htlcs;
-                       let pending_outbound_htlcs: &mut Vec<_> = &mut self.pending_outbound_htlcs;
-                       let logger = LogHolder { logger: &self.logger };
-
-                       // We really shouldnt have two passes here, but retain gives a non-mutable ref (Rust bug)
-                       pending_inbound_htlcs.retain(|htlc| {
-                               if let &InboundHTLCState::LocalRemoved(ref reason) = &htlc.state {
-                                       log_trace!(logger, " ...removing inbound LocalRemoved {}", log_bytes!(htlc.payment_hash.0));
-                                       if let &InboundHTLCRemovalReason::Fulfill(_) = reason {
-                                               value_to_self_msat_diff += htlc.amount_msat as i64;
-                                       }
-                                       false
-                               } else { true }
-                       });
-                       pending_outbound_htlcs.retain(|htlc| {
-                               if let &OutboundHTLCState::AwaitingRemovedRemoteRevoke(ref fail_reason) = &htlc.state {
-                                       log_trace!(logger, " ...removing outbound AwaitingRemovedRemoteRevoke {}", log_bytes!(htlc.payment_hash.0));
-                                       if let Some(reason) = fail_reason.clone() { // We really want take() here, but, again, non-mut ref :(
-                                               revoked_htlcs.push((htlc.source.clone(), htlc.payment_hash, reason));
-                                       } else {
-                                               // They fulfilled, so we sent them money
-                                               value_to_self_msat_diff -= htlc.amount_msat as i64;
-                                       }
-                                       false
-                               } else { true }
-                       });
-                       for htlc in pending_inbound_htlcs.iter_mut() {
-                               let swap = if let &InboundHTLCState::AwaitingRemoteRevokeToAnnounce(_) = &htlc.state {
-                                       log_trace!(logger, " ...promoting inbound AwaitingRemoteRevokeToAnnounce {} to Committed", log_bytes!(htlc.payment_hash.0));
-                                       true
-                               } else if let &InboundHTLCState::AwaitingAnnouncedRemoteRevoke(_) = &htlc.state {
-                                       log_trace!(logger, " ...promoting inbound AwaitingAnnouncedRemoteRevoke {} to Committed", log_bytes!(htlc.payment_hash.0));
-                                       true
-                               } else { false };
-                               if swap {
-                                       let mut state = InboundHTLCState::Committed;
-                                       mem::swap(&mut state, &mut htlc.state);
-
-                                       if let InboundHTLCState::AwaitingRemoteRevokeToAnnounce(forward_info) = state {
-                                               htlc.state = InboundHTLCState::AwaitingAnnouncedRemoteRevoke(forward_info);
-                                               require_commitment = true;
-                                       } else if let InboundHTLCState::AwaitingAnnouncedRemoteRevoke(forward_info) = state {
-                                               match forward_info {
-                                                       PendingHTLCStatus::Fail(fail_msg) => {
-                                                               require_commitment = true;
-                                                               match fail_msg {
-                                                                       HTLCFailureMsg::Relay(msg) => {
-                                                                               htlc.state = InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::FailRelay(msg.reason.clone()));
-                                                                               update_fail_htlcs.push(msg)
-                                                                       },
-                                                                       HTLCFailureMsg::Malformed(msg) => {
-                                                                               htlc.state = InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::FailMalformed((msg.sha256_of_onion, msg.failure_code)));
-                                                                               update_fail_malformed_htlcs.push(msg)
-                                                                       },
-                                                               }
-                                                       },
-                                                       PendingHTLCStatus::Forward(forward_info) => {
-                                                               to_forward_infos.push((forward_info, htlc.htlc_id));
-                                                               htlc.state = InboundHTLCState::Committed;
-                                                       }
-                                               }
-                                       }
-                               }
-                       }
-                       for htlc in pending_outbound_htlcs.iter_mut() {
-                               if let OutboundHTLCState::LocalAnnounced(_) = htlc.state {
-                                       log_trace!(logger, " ...promoting outbound LocalAnnounced {} to Committed", log_bytes!(htlc.payment_hash.0));
-                                       htlc.state = OutboundHTLCState::Committed;
-                               }
-                               if let Some(fail_reason) = if let &mut OutboundHTLCState::AwaitingRemoteRevokeToRemove(ref mut fail_reason) = &mut htlc.state {
-                                       Some(fail_reason.take())
-                               } else { None } {
-                                       log_trace!(logger, " ...promoting outbound AwaitingRemoteRevokeToRemove {} to AwaitingRemovedRemoteRevoke", log_bytes!(htlc.payment_hash.0));
-                                       htlc.state = OutboundHTLCState::AwaitingRemovedRemoteRevoke(fail_reason);
-                                       require_commitment = true;
-                               }
-                       }
-               }
-               self.value_to_self_msat = (self.value_to_self_msat as i64 + value_to_self_msat_diff) as u64;
-
-               if self.channel_outbound {
-                       if let Some(feerate) = self.pending_update_fee.take() {
-                               self.feerate_per_kw = feerate;
-                       }
-               } else {
-                       if let Some(feerate) = self.pending_update_fee {
-                               // Because a node cannot send two commitment_signeds in a row without getting a
-                               // revoke_and_ack from us (as it would otherwise not know the per_commitment_point
-                               // it should use to create keys with) and because a node can't send a
-                               // commitment_signed without changes, checking if the feerate is equal to the
-                               // pending feerate update is sufficient to detect require_commitment.
-                               if feerate == self.feerate_per_kw {
-                                       require_commitment = true;
-                                       self.pending_update_fee = None;
-                               }
-                       }
-               }
-
-               if (self.channel_state & ChannelState::MonitorUpdateFailed as u32) == ChannelState::MonitorUpdateFailed as u32 {
-                       // We can't actually generate a new commitment transaction (incl by freeing holding
-                       // cells) while we can't update the monitor, so we just return what we have.
-                       if require_commitment {
-                               self.monitor_pending_commitment_signed = true;
-                               // When the monitor updating is restored we'll call get_last_commitment_update(),
-                               // which does not update state, but we're definitely now awaiting a remote revoke
-                               // before we can step forward any more, so set it here.
-                               self.send_commitment_no_status_check()?;
-                       }
-                       self.monitor_pending_forwards.append(&mut to_forward_infos);
-                       self.monitor_pending_failures.append(&mut revoked_htlcs);
-                       return Ok((None, Vec::new(), Vec::new(), None, self.channel_monitor.clone()));
-               }
-
-               match self.free_holding_cell_htlcs()? {
-                       Some(mut commitment_update) => {
-                               commitment_update.0.update_fail_htlcs.reserve(update_fail_htlcs.len());
-                               for fail_msg in update_fail_htlcs.drain(..) {
-                                       commitment_update.0.update_fail_htlcs.push(fail_msg);
-                               }
-                               commitment_update.0.update_fail_malformed_htlcs.reserve(update_fail_malformed_htlcs.len());
-                               for fail_msg in update_fail_malformed_htlcs.drain(..) {
-                                       commitment_update.0.update_fail_malformed_htlcs.push(fail_msg);
-                               }
-                               Ok((Some(commitment_update.0), to_forward_infos, revoked_htlcs, None, commitment_update.1))
-                       },
-                       None => {
-                               if require_commitment {
-                                       let (commitment_signed, monitor_update) = self.send_commitment_no_status_check()?;
-                                       Ok((Some(msgs::CommitmentUpdate {
-                                               update_add_htlcs: Vec::new(),
-                                               update_fulfill_htlcs: Vec::new(),
-                                               update_fail_htlcs,
-                                               update_fail_malformed_htlcs,
-                                               update_fee: None,
-                                               commitment_signed
-                                       }), to_forward_infos, revoked_htlcs, None, monitor_update))
-                               } else {
-                                       Ok((None, to_forward_infos, revoked_htlcs, self.maybe_propose_first_closing_signed(fee_estimator), self.channel_monitor.clone()))
-                               }
-                       }
-               }
-
-       }
-
-       /// Adds a pending update to this channel. See the doc for send_htlc for
-       /// further details on the optionness of the return value.
-       /// You MUST call send_commitment prior to any other calls on this Channel
-       fn send_update_fee(&mut self, feerate_per_kw: u64) -> Option<msgs::UpdateFee> {
-               if !self.channel_outbound {
-                       panic!("Cannot send fee from inbound channel");
-               }
-               if !self.is_usable() {
-                       panic!("Cannot update fee until channel is fully established and we haven't started shutting down");
-               }
-               if !self.is_live() {
-                       panic!("Cannot update fee while peer is disconnected/we're awaiting a monitor update (ChannelManager should have caught this)");
-               }
-
-               if (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32)) == (ChannelState::AwaitingRemoteRevoke as u32) {
-                       self.holding_cell_update_fee = Some(feerate_per_kw);
-                       return None;
-               }
-
-               debug_assert!(self.pending_update_fee.is_none());
-               self.pending_update_fee = Some(feerate_per_kw);
-
-               Some(msgs::UpdateFee {
-                       channel_id: self.channel_id,
-                       feerate_per_kw: feerate_per_kw as u32,
-               })
-       }
-
-       pub fn send_update_fee_and_commit(&mut self, feerate_per_kw: u64) -> Result<Option<(msgs::UpdateFee, msgs::CommitmentSigned, ChannelMonitor)>, ChannelError> {
-               match self.send_update_fee(feerate_per_kw) {
-                       Some(update_fee) => {
-                               let (commitment_signed, monitor_update) = self.send_commitment_no_status_check()?;
-                               Ok(Some((update_fee, commitment_signed, monitor_update)))
-                       },
-                       None => Ok(None)
-               }
-       }
-
-       /// Removes any uncommitted HTLCs, to be used on peer disconnection, including any pending
-       /// HTLCs that we intended to add but haven't as we were waiting on a remote revoke.
-       /// Returns the set of PendingHTLCStatuses from remote uncommitted HTLCs (which we're
-       /// implicitly dropping) and the payment_hashes of HTLCs we tried to add but are dropping.
-       /// No further message handling calls may be made until a channel_reestablish dance has
-       /// completed.
-       pub fn remove_uncommitted_htlcs_and_mark_paused(&mut self) -> Vec<(HTLCSource, PaymentHash)> {
-               let mut outbound_drops = Vec::new();
-
-               assert_eq!(self.channel_state & ChannelState::ShutdownComplete as u32, 0);
-               if self.channel_state < ChannelState::FundingSent as u32 {
-                       self.channel_state = ChannelState::ShutdownComplete as u32;
-                       return outbound_drops;
-               }
-               // Upon reconnect we have to start the closing_signed dance over, but shutdown messages
-               // will be retransmitted.
-               self.last_sent_closing_fee = None;
-
-               let mut inbound_drop_count = 0;
-               self.pending_inbound_htlcs.retain(|htlc| {
-                       match htlc.state {
-                               InboundHTLCState::RemoteAnnounced(_) => {
-                                       // They sent us an update_add_htlc but we never got the commitment_signed.
-                                       // We'll tell them what commitment_signed we're expecting next and they'll drop
-                                       // this HTLC accordingly
-                                       inbound_drop_count += 1;
-                                       false
-                               },
-                               InboundHTLCState::AwaitingRemoteRevokeToAnnounce(_)|InboundHTLCState::AwaitingAnnouncedRemoteRevoke(_) => {
-                                       // We received a commitment_signed updating this HTLC and (at least hopefully)
-                                       // sent a revoke_and_ack (which we can re-transmit) and have heard nothing
-                                       // in response to it yet, so don't touch it.
-                                       true
-                               },
-                               InboundHTLCState::Committed => true,
-                               InboundHTLCState::LocalRemoved(_) => {
-                                       // We (hopefully) sent a commitment_signed updating this HTLC (which we can
-                                       // re-transmit if needed) and they may have even sent a revoke_and_ack back
-                                       // (that we missed). Keep this around for now and if they tell us they missed
-                                       // the commitment_signed we can re-transmit the update then.
-                                       true
-                               },
-                       }
-               });
-               self.next_remote_htlc_id -= inbound_drop_count;
-
-               for htlc in self.pending_outbound_htlcs.iter_mut() {
-                       if let OutboundHTLCState::RemoteRemoved(_) = htlc.state {
-                               // They sent us an update to remove this but haven't yet sent the corresponding
-                               // commitment_signed, we need to move it back to Committed and they can re-send
-                               // the update upon reconnection.
-                               htlc.state = OutboundHTLCState::Committed;
-                       }
-               }
-
-               self.holding_cell_htlc_updates.retain(|htlc_update| {
-                       match htlc_update {
-                               &HTLCUpdateAwaitingACK::AddHTLC { ref payment_hash, ref source, .. } => {
-                                       outbound_drops.push((source.clone(), payment_hash.clone()));
-                                       false
-                               },
-                               &HTLCUpdateAwaitingACK::ClaimHTLC {..} | &HTLCUpdateAwaitingACK::FailHTLC {..} => true,
-                       }
-               });
-               self.channel_state |= ChannelState::PeerDisconnected as u32;
-               log_debug!(self, "Peer disconnection resulted in {} remote-announced HTLC drops and {} waiting-to-locally-announced HTLC drops on channel {}", outbound_drops.len(), inbound_drop_count, log_bytes!(self.channel_id()));
-               outbound_drops
-       }
-
-       /// Indicates that a ChannelMonitor update failed to be stored by the client and further
-       /// updates are partially paused.
-       /// This must be called immediately after the call which generated the ChannelMonitor update
-       /// which failed. The messages which were generated from that call which generated the
-       /// monitor update failure must *not* have been sent to the remote end, and must instead
-       /// have been dropped. They will be regenerated when monitor_updating_restored is called.
-       pub fn monitor_update_failed(&mut self, resend_raa: bool, resend_commitment: bool, mut pending_forwards: Vec<(PendingForwardHTLCInfo, u64)>, mut pending_fails: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>) {
-               assert_eq!(self.channel_state & ChannelState::MonitorUpdateFailed as u32, 0);
-               self.monitor_pending_revoke_and_ack = resend_raa;
-               self.monitor_pending_commitment_signed = resend_commitment;
-               assert!(self.monitor_pending_forwards.is_empty());
-               mem::swap(&mut pending_forwards, &mut self.monitor_pending_forwards);
-               assert!(self.monitor_pending_failures.is_empty());
-               mem::swap(&mut pending_fails, &mut self.monitor_pending_failures);
-               self.channel_state |= ChannelState::MonitorUpdateFailed as u32;
-       }
-
-       /// Indicates that the latest ChannelMonitor update has been committed by the client
-       /// successfully and we should restore normal operation. Returns messages which should be sent
-       /// to the remote side.
-       pub fn monitor_updating_restored(&mut self) -> (Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>, RAACommitmentOrder, Vec<(PendingForwardHTLCInfo, u64)>, Vec<(HTLCSource, PaymentHash, HTLCFailReason)>, bool, Option<msgs::FundingLocked>) {
-               assert_eq!(self.channel_state & ChannelState::MonitorUpdateFailed as u32, ChannelState::MonitorUpdateFailed as u32);
-               self.channel_state &= !(ChannelState::MonitorUpdateFailed as u32);
-
-               let needs_broadcast_safe = self.channel_state & (ChannelState::FundingSent as u32) != 0 && self.channel_outbound;
-
-               // Because we will never generate a FundingBroadcastSafe event when we're in
-               // MonitorUpdateFailed, if we assume the user only broadcast the funding transaction when
-               // they received the FundingBroadcastSafe event, we can only ever hit
-               // monitor_pending_funding_locked when we're an inbound channel which failed to persist the
-               // monitor on funding_created, and we even got the funding transaction confirmed before the
-               // monitor was persisted.
-               let funding_locked = if self.monitor_pending_funding_locked {
-                       assert!(!self.channel_outbound, "Funding transaction broadcast without FundingBroadcastSafe!");
-                       self.monitor_pending_funding_locked = false;
-                       let next_per_commitment_secret = self.build_local_commitment_secret(self.cur_local_commitment_transaction_number);
-                       let next_per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &next_per_commitment_secret);
-                       Some(msgs::FundingLocked {
-                               channel_id: self.channel_id(),
-                               next_per_commitment_point: next_per_commitment_point,
-                       })
-               } else { None };
-
-               let mut forwards = Vec::new();
-               mem::swap(&mut forwards, &mut self.monitor_pending_forwards);
-               let mut failures = Vec::new();
-               mem::swap(&mut failures, &mut self.monitor_pending_failures);
-
-               if self.channel_state & (ChannelState::PeerDisconnected as u32) != 0 {
-                       self.monitor_pending_revoke_and_ack = false;
-                       self.monitor_pending_commitment_signed = false;
-                       return (None, None, RAACommitmentOrder::RevokeAndACKFirst, forwards, failures, needs_broadcast_safe, funding_locked);
-               }
-
-               let raa = if self.monitor_pending_revoke_and_ack {
-                       Some(self.get_last_revoke_and_ack())
-               } else { None };
-               let commitment_update = if self.monitor_pending_commitment_signed {
-                       Some(self.get_last_commitment_update())
-               } else { None };
-
-               self.monitor_pending_revoke_and_ack = false;
-               self.monitor_pending_commitment_signed = false;
-               let order = self.resend_order.clone();
-               log_trace!(self, "Restored monitor updating resulting in {}{} commitment update and {} RAA, with {} first",
-                       if needs_broadcast_safe { "a funding broadcast safe, " } else { "" },
-                       if commitment_update.is_some() { "a" } else { "no" },
-                       if raa.is_some() { "an" } else { "no" },
-                       match order { RAACommitmentOrder::CommitmentFirst => "commitment", RAACommitmentOrder::RevokeAndACKFirst => "RAA"});
-               (raa, commitment_update, order, forwards, failures, needs_broadcast_safe, funding_locked)
-       }
-
-       pub fn update_fee(&mut self, fee_estimator: &FeeEstimator, msg: &msgs::UpdateFee) -> Result<(), ChannelError> {
-               if self.channel_outbound {
-                       return Err(ChannelError::Close("Non-funding remote tried to update channel fee"));
-               }
-               if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
-                       return Err(ChannelError::Close("Peer sent update_fee when we needed a channel_reestablish"));
-               }
-               Channel::check_remote_fee(fee_estimator, msg.feerate_per_kw)?;
-               self.pending_update_fee = Some(msg.feerate_per_kw as u64);
-               self.channel_update_count += 1;
-               Ok(())
-       }
-
-       fn get_last_revoke_and_ack(&self) -> msgs::RevokeAndACK {
-               let next_per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &self.build_local_commitment_secret(self.cur_local_commitment_transaction_number));
-               let per_commitment_secret = chan_utils::build_commitment_secret(self.local_keys.commitment_seed, self.cur_local_commitment_transaction_number + 2);
-               msgs::RevokeAndACK {
-                       channel_id: self.channel_id,
-                       per_commitment_secret,
-                       next_per_commitment_point,
-               }
-       }
-
-       fn get_last_commitment_update(&self) -> msgs::CommitmentUpdate {
-               let mut update_add_htlcs = Vec::new();
-               let mut update_fulfill_htlcs = Vec::new();
-               let mut update_fail_htlcs = Vec::new();
-               let mut update_fail_malformed_htlcs = Vec::new();
-
-               for htlc in self.pending_outbound_htlcs.iter() {
-                       if let &OutboundHTLCState::LocalAnnounced(ref onion_packet) = &htlc.state {
-                               update_add_htlcs.push(msgs::UpdateAddHTLC {
-                                       channel_id: self.channel_id(),
-                                       htlc_id: htlc.htlc_id,
-                                       amount_msat: htlc.amount_msat,
-                                       payment_hash: htlc.payment_hash,
-                                       cltv_expiry: htlc.cltv_expiry,
-                                       onion_routing_packet: (**onion_packet).clone(),
-                               });
-                       }
-               }
-
-               for htlc in self.pending_inbound_htlcs.iter() {
-                       if let &InboundHTLCState::LocalRemoved(ref reason) = &htlc.state {
-                               match reason {
-                                       &InboundHTLCRemovalReason::FailRelay(ref err_packet) => {
-                                               update_fail_htlcs.push(msgs::UpdateFailHTLC {
-                                                       channel_id: self.channel_id(),
-                                                       htlc_id: htlc.htlc_id,
-                                                       reason: err_packet.clone()
-                                               });
-                                       },
-                                       &InboundHTLCRemovalReason::FailMalformed((ref sha256_of_onion, ref failure_code)) => {
-                                               update_fail_malformed_htlcs.push(msgs::UpdateFailMalformedHTLC {
-                                                       channel_id: self.channel_id(),
-                                                       htlc_id: htlc.htlc_id,
-                                                       sha256_of_onion: sha256_of_onion.clone(),
-                                                       failure_code: failure_code.clone(),
-                                               });
-                                       },
-                                       &InboundHTLCRemovalReason::Fulfill(ref payment_preimage) => {
-                                               update_fulfill_htlcs.push(msgs::UpdateFulfillHTLC {
-                                                       channel_id: self.channel_id(),
-                                                       htlc_id: htlc.htlc_id,
-                                                       payment_preimage: payment_preimage.clone(),
-                                               });
-                                       },
-                               }
-                       }
-               }
-
-               log_trace!(self, "Regenerated latest commitment update with {} update_adds, {} update_fulfills, {} update_fails, and {} update_fail_malformeds",
-                               update_add_htlcs.len(), update_fulfill_htlcs.len(), update_fail_htlcs.len(), update_fail_malformed_htlcs.len());
-               msgs::CommitmentUpdate {
-                       update_add_htlcs, update_fulfill_htlcs, update_fail_htlcs, update_fail_malformed_htlcs,
-                       update_fee: None,
-                       commitment_signed: self.send_commitment_no_state_update().expect("It looks like we failed to re-generate a commitment_signed we had previously sent?").0,
-               }
-       }
-
-       /// May panic if some calls other than message-handling calls (which will all Err immediately)
-       /// have been called between remove_uncommitted_htlcs_and_mark_paused and this call.
-       pub fn channel_reestablish(&mut self, msg: &msgs::ChannelReestablish) -> Result<(Option<msgs::FundingLocked>, Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>, Option<ChannelMonitor>, RAACommitmentOrder, Option<msgs::Shutdown>), ChannelError> {
-               if self.channel_state & (ChannelState::PeerDisconnected as u32) == 0 {
-                       // While BOLT 2 doesn't indicate explicitly we should error this channel here, it
-                       // almost certainly indicates we are going to end up out-of-sync in some way, so we
-                       // just close here instead of trying to recover.
-                       return Err(ChannelError::Close("Peer sent a loose channel_reestablish not after reconnect"));
-               }
-
-               if msg.next_local_commitment_number >= INITIAL_COMMITMENT_NUMBER || msg.next_remote_commitment_number >= INITIAL_COMMITMENT_NUMBER ||
-                       msg.next_local_commitment_number == 0 {
-                       return Err(ChannelError::Close("Peer sent a garbage channel_reestablish"));
-               }
-
-               if msg.next_remote_commitment_number > 0 {
-                       match msg.data_loss_protect {
-                               OptionalField::Present(ref data_loss) => {
-                                       if chan_utils::build_commitment_secret(self.local_keys.commitment_seed, INITIAL_COMMITMENT_NUMBER - msg.next_remote_commitment_number + 1) != data_loss.your_last_per_commitment_secret {
-                                               return Err(ChannelError::Close("Peer sent a garbage channel_reestablish with secret key not matching the commitment height provided"));
-                                       }
-                                       if msg.next_remote_commitment_number > INITIAL_COMMITMENT_NUMBER - self.cur_local_commitment_transaction_number {
-                                               self.channel_monitor.provide_rescue_remote_commitment_tx_info(data_loss.my_current_per_commitment_point);
-                                               return Err(ChannelError::CloseDelayBroadcast { msg: "We have fallen behind - we have received proof that if we broadcast remote is going to claim our funds - we can't do any automated broadcasting", update: Some(self.channel_monitor.clone())
-                                       });
-                                       }
-                               },
-                               OptionalField::Absent => {}
-                       }
-               }
-
-               // Go ahead and unmark PeerDisconnected as various calls we may make check for it (and all
-               // remaining cases either succeed or ErrorMessage-fail).
-               self.channel_state &= !(ChannelState::PeerDisconnected as u32);
-
-               let shutdown_msg = if self.channel_state & (ChannelState::LocalShutdownSent as u32) != 0 {
-                       Some(msgs::Shutdown {
-                               channel_id: self.channel_id,
-                               scriptpubkey: self.get_closing_scriptpubkey(),
-                       })
-               } else { None };
-
-               if self.channel_state & (ChannelState::FundingSent as u32) == ChannelState::FundingSent as u32 {
-                       // If we're waiting on a monitor update, we shouldn't re-send any funding_locked's.
-                       if self.channel_state & (ChannelState::OurFundingLocked as u32) == 0 ||
-                                       self.channel_state & (ChannelState::MonitorUpdateFailed as u32) != 0 {
-                               if msg.next_remote_commitment_number != 0 {
-                                       return Err(ChannelError::Close("Peer claimed they saw a revoke_and_ack but we haven't sent funding_locked yet"));
-                               }
-                               // Short circuit the whole handler as there is nothing we can resend them
-                               return Ok((None, None, None, None, RAACommitmentOrder::CommitmentFirst, shutdown_msg));
-                       }
-
-                       // We have OurFundingLocked set!
-                       let next_per_commitment_secret = self.build_local_commitment_secret(self.cur_local_commitment_transaction_number);
-                       let next_per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &next_per_commitment_secret);
-                       return Ok((Some(msgs::FundingLocked {
-                               channel_id: self.channel_id(),
-                               next_per_commitment_point: next_per_commitment_point,
-                       }), None, None, None, RAACommitmentOrder::CommitmentFirst, shutdown_msg));
-               }
-
-               let required_revoke = if msg.next_remote_commitment_number + 1 == INITIAL_COMMITMENT_NUMBER - self.cur_local_commitment_transaction_number {
-                       // Remote isn't waiting on any RevokeAndACK from us!
-                       // Note that if we need to repeat our FundingLocked we'll do that in the next if block.
-                       None
-               } else if msg.next_remote_commitment_number + 1 == (INITIAL_COMMITMENT_NUMBER - 1) - self.cur_local_commitment_transaction_number {
-                       if self.channel_state & (ChannelState::MonitorUpdateFailed as u32) != 0 {
-                               self.monitor_pending_revoke_and_ack = true;
-                               None
-                       } else {
-                               Some(self.get_last_revoke_and_ack())
-                       }
-               } else {
-                       return Err(ChannelError::Close("Peer attempted to reestablish channel with a very old local commitment transaction"));
-               };
-
-               // We increment cur_remote_commitment_transaction_number only upon receipt of
-               // revoke_and_ack, not on sending commitment_signed, so we add one if have
-               // AwaitingRemoteRevoke set, which indicates we sent a commitment_signed but haven't gotten
-               // the corresponding revoke_and_ack back yet.
-               let our_next_remote_commitment_number = INITIAL_COMMITMENT_NUMBER - self.cur_remote_commitment_transaction_number + if (self.channel_state & ChannelState::AwaitingRemoteRevoke as u32) != 0 { 1 } else { 0 };
-
-               let resend_funding_locked = if msg.next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self.cur_local_commitment_transaction_number == 1 {
-                       // We should never have to worry about MonitorUpdateFailed resending FundingLocked
-                       let next_per_commitment_secret = self.build_local_commitment_secret(self.cur_local_commitment_transaction_number);
-                       let next_per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &next_per_commitment_secret);
-                       Some(msgs::FundingLocked {
-                               channel_id: self.channel_id(),
-                               next_per_commitment_point: next_per_commitment_point,
-                       })
-               } else { None };
-
-               if msg.next_local_commitment_number == our_next_remote_commitment_number {
-                       if required_revoke.is_some() {
-                               log_debug!(self, "Reconnected channel {} with only lost outbound RAA", log_bytes!(self.channel_id()));
-                       } else {
-                               log_debug!(self, "Reconnected channel {} with no loss", log_bytes!(self.channel_id()));
-                       }
-
-                       if (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32 | ChannelState::MonitorUpdateFailed as u32)) == 0 {
-                               // We're up-to-date and not waiting on a remote revoke (if we are our
-                               // channel_reestablish should result in them sending a revoke_and_ack), but we may
-                               // have received some updates while we were disconnected. Free the holding cell
-                               // now!
-                               match self.free_holding_cell_htlcs() {
-                                       Err(ChannelError::Close(msg)) => return Err(ChannelError::Close(msg)),
-                                       Err(ChannelError::Ignore(_)) | Err(ChannelError::CloseDelayBroadcast { .. }) => panic!("Got non-channel-failing result from free_holding_cell_htlcs"),
-                                       Ok(Some((commitment_update, channel_monitor))) => return Ok((resend_funding_locked, required_revoke, Some(commitment_update), Some(channel_monitor), self.resend_order.clone(), shutdown_msg)),
-                                       Ok(None) => return Ok((resend_funding_locked, required_revoke, None, None, self.resend_order.clone(), shutdown_msg)),
-                               }
-                       } else {
-                               return Ok((resend_funding_locked, required_revoke, None, None, self.resend_order.clone(), shutdown_msg));
-                       }
-               } else if msg.next_local_commitment_number == our_next_remote_commitment_number - 1 {
-                       if required_revoke.is_some() {
-                               log_debug!(self, "Reconnected channel {} with lost outbound RAA and lost remote commitment tx", log_bytes!(self.channel_id()));
-                       } else {
-                               log_debug!(self, "Reconnected channel {} with only lost remote commitment tx", log_bytes!(self.channel_id()));
-                       }
-
-                       if self.channel_state & (ChannelState::MonitorUpdateFailed as u32) != 0 {
-                               self.monitor_pending_commitment_signed = true;
-                               return Ok((resend_funding_locked, None, None, None, self.resend_order.clone(), shutdown_msg));
-                       }
-
-                       return Ok((resend_funding_locked, required_revoke, Some(self.get_last_commitment_update()), None, self.resend_order.clone(), shutdown_msg));
-               } else {
-                       return Err(ChannelError::Close("Peer attempted to reestablish channel with a very old remote commitment transaction"));
-               }
-       }
-
-       fn maybe_propose_first_closing_signed(&mut self, fee_estimator: &FeeEstimator) -> Option<msgs::ClosingSigned> {
-               if !self.channel_outbound || !self.pending_inbound_htlcs.is_empty() || !self.pending_outbound_htlcs.is_empty() ||
-                               self.channel_state & (BOTH_SIDES_SHUTDOWN_MASK | ChannelState::AwaitingRemoteRevoke as u32) != BOTH_SIDES_SHUTDOWN_MASK ||
-                               self.last_sent_closing_fee.is_some() || self.pending_update_fee.is_some() {
-                       return None;
-               }
-
-               let mut proposed_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background);
-               if self.feerate_per_kw > proposed_feerate {
-                       proposed_feerate = self.feerate_per_kw;
-               }
-               let tx_weight = Self::get_closing_transaction_weight(&self.get_closing_scriptpubkey(), self.their_shutdown_scriptpubkey.as_ref().unwrap());
-               let proposed_total_fee_satoshis = proposed_feerate * tx_weight / 1000;
-
-               let (closing_tx, total_fee_satoshis) = self.build_closing_transaction(proposed_total_fee_satoshis, false);
-               let funding_redeemscript = self.get_funding_redeemscript();
-               let sighash = hash_to_message!(&bip143::SighashComponents::new(&closing_tx).sighash_all(&closing_tx.input[0], &funding_redeemscript, self.channel_value_satoshis)[..]);
-
-               self.last_sent_closing_fee = Some((proposed_feerate, total_fee_satoshis));
-               Some(msgs::ClosingSigned {
-                       channel_id: self.channel_id,
-                       fee_satoshis: total_fee_satoshis,
-                       signature: self.secp_ctx.sign(&sighash, &self.local_keys.funding_key),
-               })
-       }
-
-       pub fn shutdown(&mut self, fee_estimator: &FeeEstimator, msg: &msgs::Shutdown) -> Result<(Option<msgs::Shutdown>, Option<msgs::ClosingSigned>, Vec<(HTLCSource, PaymentHash)>), ChannelError> {
-               if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
-                       return Err(ChannelError::Close("Peer sent shutdown when we needed a channel_reestablish"));
-               }
-               if self.channel_state < ChannelState::FundingSent as u32 {
-                       // Spec says we should fail the connection, not the channel, but that's nonsense, there
-                       // are plenty of reasons you may want to fail a channel pre-funding, and spec says you
-                       // can do that via error message without getting a connection fail anyway...
-                       return Err(ChannelError::Close("Peer sent shutdown pre-funding generation"));
-               }
-               for htlc in self.pending_inbound_htlcs.iter() {
-                       if let InboundHTLCState::RemoteAnnounced(_) = htlc.state {
-                               return Err(ChannelError::Close("Got shutdown with remote pending HTLCs"));
-                       }
-               }
-               assert_eq!(self.channel_state & ChannelState::ShutdownComplete as u32, 0);
-
-               // BOLT 2 says we must only send a scriptpubkey of certain standard forms, which are up to
-               // 34 bytes in length, so don't let the remote peer feed us some super fee-heavy script.
-               if self.channel_outbound && msg.scriptpubkey.len() > 34 {
-                       return Err(ChannelError::Close("Got shutdown_scriptpubkey of absurd length from remote peer"));
-               }
-
-               //Check shutdown_scriptpubkey form as BOLT says we must
-               if !msg.scriptpubkey.is_p2pkh() && !msg.scriptpubkey.is_p2sh() && !msg.scriptpubkey.is_v0_p2wpkh() && !msg.scriptpubkey.is_v0_p2wsh() {
-                       return Err(ChannelError::Close("Got a nonstandard scriptpubkey from remote peer"));
-               }
-
-               if self.their_shutdown_scriptpubkey.is_some() {
-                       if Some(&msg.scriptpubkey) != self.their_shutdown_scriptpubkey.as_ref() {
-                               return Err(ChannelError::Close("Got shutdown request with a scriptpubkey which did not match their previous scriptpubkey"));
-                       }
-               } else {
-                       self.their_shutdown_scriptpubkey = Some(msg.scriptpubkey.clone());
-               }
-
-               // From here on out, we may not fail!
-
-               self.channel_state |= ChannelState::RemoteShutdownSent as u32;
-               self.channel_update_count += 1;
-
-               // We can't send our shutdown until we've committed all of our pending HTLCs, but the
-               // remote side is unlikely to accept any new HTLCs, so we go ahead and "free" any holding
-               // cell HTLCs and return them to fail the payment.
-               self.holding_cell_update_fee = None;
-               let mut dropped_outbound_htlcs = Vec::with_capacity(self.holding_cell_htlc_updates.len());
-               self.holding_cell_htlc_updates.retain(|htlc_update| {
-                       match htlc_update {
-                               &HTLCUpdateAwaitingACK::AddHTLC { ref payment_hash, ref source, .. } => {
-                                       dropped_outbound_htlcs.push((source.clone(), payment_hash.clone()));
-                                       false
-                               },
-                               _ => true
-                       }
-               });
-               // If we have any LocalAnnounced updates we'll probably just get back a update_fail_htlc
-               // immediately after the commitment dance, but we can send a Shutdown cause we won't send
-               // any further commitment updates after we set LocalShutdownSent.
-
-               let our_shutdown = if (self.channel_state & ChannelState::LocalShutdownSent as u32) == ChannelState::LocalShutdownSent as u32 {
-                       None
-               } else {
-                       Some(msgs::Shutdown {
-                               channel_id: self.channel_id,
-                               scriptpubkey: self.get_closing_scriptpubkey(),
-                       })
-               };
-
-               self.channel_state |= ChannelState::LocalShutdownSent as u32;
-               self.channel_update_count += 1;
-               Ok((our_shutdown, self.maybe_propose_first_closing_signed(fee_estimator), dropped_outbound_htlcs))
-       }
-
-       pub fn closing_signed(&mut self, fee_estimator: &FeeEstimator, msg: &msgs::ClosingSigned) -> Result<(Option<msgs::ClosingSigned>, Option<Transaction>), ChannelError> {
-               if self.channel_state & BOTH_SIDES_SHUTDOWN_MASK != BOTH_SIDES_SHUTDOWN_MASK {
-                       return Err(ChannelError::Close("Remote end sent us a closing_signed before both sides provided a shutdown"));
-               }
-               if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
-                       return Err(ChannelError::Close("Peer sent closing_signed when we needed a channel_reestablish"));
-               }
-               if !self.pending_inbound_htlcs.is_empty() || !self.pending_outbound_htlcs.is_empty() {
-                       return Err(ChannelError::Close("Remote end sent us a closing_signed while there were still pending HTLCs"));
-               }
-               if msg.fee_satoshis > 21000000 * 10000000 { //this is required to stop potential overflow in build_closing_transaction
-                       return Err(ChannelError::Close("Remote tried to send us a closing tx with > 21 million BTC fee"));
-               }
-
-               let funding_redeemscript = self.get_funding_redeemscript();
-               let (mut closing_tx, used_total_fee) = self.build_closing_transaction(msg.fee_satoshis, false);
-               if used_total_fee != msg.fee_satoshis {
-                       return Err(ChannelError::Close("Remote sent us a closing_signed with a fee greater than the value they can claim"));
-               }
-               let mut sighash = hash_to_message!(&bip143::SighashComponents::new(&closing_tx).sighash_all(&closing_tx.input[0], &funding_redeemscript, self.channel_value_satoshis)[..]);
-
-               match self.secp_ctx.verify(&sighash, &msg.signature, &self.their_funding_pubkey.unwrap()) {
-                       Ok(_) => {},
-                       Err(_e) => {
-                               // The remote end may have decided to revoke their output due to inconsistent dust
-                               // limits, so check for that case by re-checking the signature here.
-                               closing_tx = self.build_closing_transaction(msg.fee_satoshis, true).0;
-                               sighash = hash_to_message!(&bip143::SighashComponents::new(&closing_tx).sighash_all(&closing_tx.input[0], &funding_redeemscript, self.channel_value_satoshis)[..]);
-                               secp_check!(self.secp_ctx.verify(&sighash, &msg.signature, &self.their_funding_pubkey.unwrap()), "Invalid closing tx signature from peer");
-                       },
-               };
-
-               if let Some((_, last_fee)) = self.last_sent_closing_fee {
-                       if last_fee == msg.fee_satoshis {
-                               self.sign_commitment_transaction(&mut closing_tx, &msg.signature);
-                               self.channel_state = ChannelState::ShutdownComplete as u32;
-                               self.channel_update_count += 1;
-                               return Ok((None, Some(closing_tx)));
-                       }
-               }
-
-               macro_rules! propose_new_feerate {
-                       ($new_feerate: expr) => {
-                               let closing_tx_max_weight = Self::get_closing_transaction_weight(&self.get_closing_scriptpubkey(), self.their_shutdown_scriptpubkey.as_ref().unwrap());
-                               let (closing_tx, used_total_fee) = self.build_closing_transaction($new_feerate * closing_tx_max_weight / 1000, false);
-                               sighash = hash_to_message!(&bip143::SighashComponents::new(&closing_tx).sighash_all(&closing_tx.input[0], &funding_redeemscript, self.channel_value_satoshis)[..]);
-                               let our_sig = self.secp_ctx.sign(&sighash, &self.local_keys.funding_key);
-                               self.last_sent_closing_fee = Some(($new_feerate, used_total_fee));
-                               return Ok((Some(msgs::ClosingSigned {
-                                       channel_id: self.channel_id,
-                                       fee_satoshis: used_total_fee,
-                                       signature: our_sig,
-                               }), None))
-                       }
-               }
-
-               let proposed_sat_per_kw = msg.fee_satoshis * 1000 / closing_tx.get_weight() as u64;
-               if self.channel_outbound {
-                       let our_max_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
-                       if proposed_sat_per_kw > our_max_feerate {
-                               if let Some((last_feerate, _)) = self.last_sent_closing_fee {
-                                       if our_max_feerate <= last_feerate {
-                                               return Err(ChannelError::Close("Unable to come to consensus about closing feerate, remote wanted something higher than our Normal feerate"));
-                                       }
-                               }
-                               propose_new_feerate!(our_max_feerate);
-                       }
-               } else {
-                       let our_min_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background);
-                       if proposed_sat_per_kw < our_min_feerate {
-                               if let Some((last_feerate, _)) = self.last_sent_closing_fee {
-                                       if our_min_feerate >= last_feerate {
-                                               return Err(ChannelError::Close("Unable to come to consensus about closing feerate, remote wanted something lower than our Background feerate"));
-                                       }
-                               }
-                               propose_new_feerate!(our_min_feerate);
-                       }
-               }
-
-               let our_sig = self.sign_commitment_transaction(&mut closing_tx, &msg.signature);
-               self.channel_state = ChannelState::ShutdownComplete as u32;
-               self.channel_update_count += 1;
-
-               Ok((Some(msgs::ClosingSigned {
-                       channel_id: self.channel_id,
-                       fee_satoshis: msg.fee_satoshis,
-                       signature: our_sig,
-               }), Some(closing_tx)))
-       }
-
-       // Public utilities:
-
-       pub fn channel_id(&self) -> [u8; 32] {
-               self.channel_id
-       }
-
-       /// Gets the "user_id" value passed into the construction of this channel. It has no special
-       /// meaning and exists only to allow users to have a persistent identifier of a channel.
-       pub fn get_user_id(&self) -> u64 {
-               self.user_id
-       }
-
-       /// May only be called after funding has been initiated (ie is_funding_initiated() is true)
-       pub fn channel_monitor(&self) -> ChannelMonitor {
-               if self.channel_state < ChannelState::FundingCreated as u32 {
-                       panic!("Can't get a channel monitor until funding has been created");
-               }
-               self.channel_monitor.clone()
-       }
-
-       /// Guaranteed to be Some after both FundingLocked messages have been exchanged (and, thus,
-       /// is_usable() returns true).
-       /// Allowed in any state (including after shutdown)
-       pub fn get_short_channel_id(&self) -> Option<u64> {
-               self.short_channel_id
-       }
-
-       /// Returns the funding_txo we either got from our peer, or were given by
-       /// get_outbound_funding_created.
-       pub fn get_funding_txo(&self) -> Option<OutPoint> {
-               self.channel_monitor.get_funding_txo()
-       }
-
-       /// Allowed in any state (including after shutdown)
-       pub fn get_their_node_id(&self) -> PublicKey {
-               self.their_node_id
-       }
-
-       /// Allowed in any state (including after shutdown)
-       pub fn get_our_htlc_minimum_msat(&self) -> u64 {
-               self.our_htlc_minimum_msat
-       }
-
-       /// Allowed in any state (including after shutdown)
-       pub fn get_their_htlc_minimum_msat(&self) -> u64 {
-               self.our_htlc_minimum_msat
-       }
-
-       pub fn get_value_satoshis(&self) -> u64 {
-               self.channel_value_satoshis
-       }
-
-       pub fn get_fee_proportional_millionths(&self) -> u32 {
-               self.config.fee_proportional_millionths
-       }
-
-       #[cfg(test)]
-       pub fn get_feerate(&self) -> u64 {
-               self.feerate_per_kw
-       }
-
-       pub fn get_cur_local_commitment_transaction_number(&self) -> u64 {
-               self.cur_local_commitment_transaction_number + 1
-       }
-
-       pub fn get_cur_remote_commitment_transaction_number(&self) -> u64 {
-               self.cur_remote_commitment_transaction_number + 1 - if self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32) != 0 { 1 } else { 0 }
-       }
-
-       pub fn get_revoked_remote_commitment_transaction_number(&self) -> u64 {
-               self.cur_remote_commitment_transaction_number + 2
-       }
-
-       #[cfg(test)]
-       pub fn get_local_keys(&self) -> &ChannelKeys {
-               &self.local_keys
-       }
-
-       #[cfg(test)]
-       pub fn get_value_stat(&self) -> ChannelValueStat {
-               ChannelValueStat {
-                       value_to_self_msat: self.value_to_self_msat,
-                       channel_value_msat: self.channel_value_satoshis * 1000,
-                       channel_reserve_msat: self.their_channel_reserve_satoshis * 1000,
-                       pending_outbound_htlcs_amount_msat: self.pending_outbound_htlcs.iter().map(|ref h| h.amount_msat).sum::<u64>(),
-                       pending_inbound_htlcs_amount_msat: self.pending_inbound_htlcs.iter().map(|ref h| h.amount_msat).sum::<u64>(),
-                       holding_cell_outbound_amount_msat: {
-                               let mut res = 0;
-                               for h in self.holding_cell_htlc_updates.iter() {
-                                       match h {
-                                               &HTLCUpdateAwaitingACK::AddHTLC{amount_msat, .. } => {
-                                                       res += amount_msat;
-                                               }
-                                               _ => {}
-                                       }
-                               }
-                               res
-                       },
-                       their_max_htlc_value_in_flight_msat: self.their_max_htlc_value_in_flight_msat,
-               }
-       }
-
-       /// Allowed in any state (including after shutdown)
-       pub fn get_channel_update_count(&self) -> u32 {
-               self.channel_update_count
-       }
-
-       pub fn should_announce(&self) -> bool {
-               self.config.announced_channel
-       }
-
-       pub fn is_outbound(&self) -> bool {
-               self.channel_outbound
-       }
-
-       /// Gets the fee we'd want to charge for adding an HTLC output to this Channel
-       /// Allowed in any state (including after shutdown)
-       pub fn get_our_fee_base_msat(&self, fee_estimator: &FeeEstimator) -> u32 {
-               // For lack of a better metric, we calculate what it would cost to consolidate the new HTLC
-               // output value back into a transaction with the regular channel output:
-
-               // the fee cost of the HTLC-Success/HTLC-Timeout transaction:
-               let mut res = self.feerate_per_kw * cmp::max(HTLC_TIMEOUT_TX_WEIGHT, HTLC_SUCCESS_TX_WEIGHT) / 1000;
-
-               if self.channel_outbound {
-                       // + the marginal fee increase cost to us in the commitment transaction:
-                       res += self.feerate_per_kw * COMMITMENT_TX_WEIGHT_PER_HTLC / 1000;
-               }
-
-               // + the marginal cost of an input which spends the HTLC-Success/HTLC-Timeout output:
-               res += fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal) * SPENDING_INPUT_FOR_A_OUTPUT_WEIGHT / 1000;
-
-               res as u32
-       }
-
-       /// Returns true if we've ever received a message from the remote end for this Channel
-       pub fn have_received_message(&self) -> bool {
-               self.channel_state > (ChannelState::OurInitSent as u32)
-       }
-
-       /// Returns true if this channel is fully established and not known to be closing.
-       /// Allowed in any state (including after shutdown)
-       pub fn is_usable(&self) -> bool {
-               let mask = ChannelState::ChannelFunded as u32 | BOTH_SIDES_SHUTDOWN_MASK;
-               (self.channel_state & mask) == (ChannelState::ChannelFunded as u32)
-       }
-
-       /// Returns true if this channel is currently available for use. This is a superset of
-       /// is_usable() and considers things like the channel being temporarily disabled.
-       /// Allowed in any state (including after shutdown)
-       pub fn is_live(&self) -> bool {
-               self.is_usable() && (self.channel_state & (ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateFailed as u32) == 0)
-       }
-
-       /// Returns true if this channel has been marked as awaiting a monitor update to move forward.
-       /// Allowed in any state (including after shutdown)
-       pub fn is_awaiting_monitor_update(&self) -> bool {
-               (self.channel_state & ChannelState::MonitorUpdateFailed as u32) != 0
-       }
-
-       /// Returns true if funding_created was sent/received.
-       pub fn is_funding_initiated(&self) -> bool {
-               self.channel_state >= ChannelState::FundingCreated as u32
-       }
-
-       /// Returns true if this channel is fully shut down. True here implies that no further actions
-       /// may/will be taken on this channel, and thus this object should be freed. Any future changes
-       /// will be handled appropriately by the chain monitor.
-       pub fn is_shutdown(&self) -> bool {
-               if (self.channel_state & ChannelState::ShutdownComplete as u32) == ChannelState::ShutdownComplete as u32  {
-                       assert!(self.channel_state == ChannelState::ShutdownComplete as u32);
-                       true
-               } else { false }
-       }
-
-       /// Called by channelmanager based on chain blocks being connected.
-       /// Note that we only need to use this to detect funding_signed, anything else is handled by
-       /// the channel_monitor.
-       /// In case of Err, the channel may have been closed, at which point the standard requirements
-       /// apply - no calls may be made except those explicitly stated to be allowed post-shutdown.
-       /// Only returns an ErrorAction of DisconnectPeer, if Err.
-       pub fn block_connected(&mut self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], indexes_of_txn_matched: &[u32]) -> Result<Option<msgs::FundingLocked>, msgs::ErrorMessage> {
-               let non_shutdown_state = self.channel_state & (!MULTI_STATE_FLAGS);
-               if header.bitcoin_hash() != self.last_block_connected {
-                       self.last_block_connected = header.bitcoin_hash();
-                       self.channel_monitor.last_block_hash = self.last_block_connected;
-                       if self.funding_tx_confirmations > 0 {
-                               self.funding_tx_confirmations += 1;
-                               if self.funding_tx_confirmations == self.minimum_depth as u64 {
-                                       let need_commitment_update = if non_shutdown_state == ChannelState::FundingSent as u32 {
-                                               self.channel_state |= ChannelState::OurFundingLocked as u32;
-                                               true
-                                       } else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::TheirFundingLocked as u32) {
-                                               self.channel_state = ChannelState::ChannelFunded as u32 | (self.channel_state & MULTI_STATE_FLAGS);
-                                               self.channel_update_count += 1;
-                                               true
-                                       } else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::OurFundingLocked as u32) {
-                                               // We got a reorg but not enough to trigger a force close, just update
-                                               // funding_tx_confirmed_in and return.
-                                               false
-                                       } else if self.channel_state < ChannelState::ChannelFunded as u32 {
-                                               panic!("Started confirming a channel in a state pre-FundingSent?: {}", self.channel_state);
-                                       } else {
-                                               // We got a reorg but not enough to trigger a force close, just update
-                                               // funding_tx_confirmed_in and return.
-                                               false
-                                       };
-                                       self.funding_tx_confirmed_in = Some(header.bitcoin_hash());
-
-                                       //TODO: Note that this must be a duplicate of the previous commitment point they sent us,
-                                       //as otherwise we will have a commitment transaction that they can't revoke (well, kinda,
-                                       //they can by sending two revoke_and_acks back-to-back, but not really). This appears to be
-                                       //a protocol oversight, but I assume I'm just missing something.
-                                       if need_commitment_update {
-                                               if self.channel_state & (ChannelState::MonitorUpdateFailed as u32) == 0 {
-                                                       let next_per_commitment_secret = self.build_local_commitment_secret(self.cur_local_commitment_transaction_number);
-                                                       let next_per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &next_per_commitment_secret);
-                                                       return Ok(Some(msgs::FundingLocked {
-                                                               channel_id: self.channel_id,
-                                                               next_per_commitment_point: next_per_commitment_point,
-                                                       }));
-                                               } else {
-                                                       self.monitor_pending_funding_locked = true;
-                                                       return Ok(None);
-                                               }
-                                       }
-                               }
-                       }
-               }
-               if non_shutdown_state & !(ChannelState::TheirFundingLocked as u32) == ChannelState::FundingSent as u32 {
-                       for (ref tx, index_in_block) in txn_matched.iter().zip(indexes_of_txn_matched) {
-                               if tx.txid() == self.channel_monitor.get_funding_txo().unwrap().txid {
-                                       let txo_idx = self.channel_monitor.get_funding_txo().unwrap().index as usize;
-                                       if txo_idx >= tx.output.len() || tx.output[txo_idx].script_pubkey != self.get_funding_redeemscript().to_v0_p2wsh() ||
-                                                       tx.output[txo_idx].value != self.channel_value_satoshis {
-                                               if self.channel_outbound {
-                                                       // If we generated the funding transaction and it doesn't match what it
-                                                       // should, the client is really broken and we should just panic and
-                                                       // tell them off. That said, because hash collisions happen with high
-                                                       // probability in fuzztarget mode, if we're fuzzing we just close the
-                                                       // channel and move on.
-                                                       #[cfg(not(feature = "fuzztarget"))]
-                                                       panic!("Client called ChannelManager::funding_transaction_generated with bogus transaction!");
-                                               }
-                                               self.channel_state = ChannelState::ShutdownComplete as u32;
-                                               self.channel_update_count += 1;
-                                               return Err(msgs::ErrorMessage {
-                                                       channel_id: self.channel_id(),
-                                                       data: "funding tx had wrong script/value".to_owned()
-                                               });
-                                       } else {
-                                               if self.channel_outbound {
-                                                       for input in tx.input.iter() {
-                                                               if input.witness.is_empty() {
-                                                                       // We generated a malleable funding transaction, implying we've
-                                                                       // just exposed ourselves to funds loss to our counterparty.
-                                                                       #[cfg(not(feature = "fuzztarget"))]
-                                                                       panic!("Client called ChannelManager::funding_transaction_generated with bogus transaction!");
-                                                               }
-                                                       }
-                                               }
-                                               self.funding_tx_confirmations = 1;
-                                               self.short_channel_id = Some(((height as u64)          << (5*8)) |
-                                                                            ((*index_in_block as u64) << (2*8)) |
-                                                                            ((txo_idx as u64)         << (0*8)));
-                                       }
-                               }
-                       }
-               }
-               Ok(None)
-       }
-
-       /// Called by channelmanager based on chain blocks being disconnected.
-       /// Returns true if we need to close the channel now due to funding transaction
-       /// unconfirmation/reorg.
-       pub fn block_disconnected(&mut self, header: &BlockHeader) -> bool {
-               if self.funding_tx_confirmations > 0 {
-                       self.funding_tx_confirmations -= 1;
-                       if self.funding_tx_confirmations == UNCONF_THRESHOLD as u64 {
-                               return true;
-                       }
-               }
-               if Some(header.bitcoin_hash()) == self.funding_tx_confirmed_in {
-                       self.funding_tx_confirmations = self.minimum_depth as u64 - 1;
-               }
-               self.last_block_connected = header.bitcoin_hash();
-               self.channel_monitor.last_block_hash = self.last_block_connected;
-               false
-       }
-
-       // Methods to get unprompted messages to send to the remote end (or where we already returned
-       // something in the handler for the message that prompted this message):
-
-       pub fn get_open_channel(&self, chain_hash: Sha256dHash, fee_estimator: &FeeEstimator) -> msgs::OpenChannel {
-               if !self.channel_outbound {
-                       panic!("Tried to open a channel for an inbound channel?");
-               }
-               if self.channel_state != ChannelState::OurInitSent as u32 {
-                       panic!("Cannot generate an open_channel after we've moved forward");
-               }
-
-               if self.cur_local_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
-                       panic!("Tried to send an open_channel for a channel that has already advanced");
-               }
-
-               let local_commitment_secret = self.build_local_commitment_secret(self.cur_local_commitment_transaction_number);
-
-               msgs::OpenChannel {
-                       chain_hash: chain_hash,
-                       temporary_channel_id: self.channel_id,
-                       funding_satoshis: self.channel_value_satoshis,
-                       push_msat: self.channel_value_satoshis * 1000 - self.value_to_self_msat,
-                       dust_limit_satoshis: self.our_dust_limit_satoshis,
-                       max_htlc_value_in_flight_msat: Channel::get_our_max_htlc_value_in_flight_msat(self.channel_value_satoshis),
-                       channel_reserve_satoshis: Channel::get_our_channel_reserve_satoshis(self.channel_value_satoshis),
-                       htlc_minimum_msat: self.our_htlc_minimum_msat,
-                       feerate_per_kw: fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background) as u32,
-                       to_self_delay: self.our_to_self_delay,
-                       max_accepted_htlcs: OUR_MAX_HTLCS,
-                       funding_pubkey: PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.funding_key),
-                       revocation_basepoint: PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.revocation_base_key),
-                       payment_basepoint: PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.payment_base_key),
-                       delayed_payment_basepoint: PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.delayed_payment_base_key),
-                       htlc_basepoint: PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.htlc_base_key),
-                       first_per_commitment_point: PublicKey::from_secret_key(&self.secp_ctx, &local_commitment_secret),
-                       channel_flags: if self.config.announced_channel {1} else {0},
-                       shutdown_scriptpubkey: OptionalField::Present(if self.config.commit_upfront_shutdown_pubkey { self.get_closing_scriptpubkey() } else { Builder::new().into_script() })
-               }
-       }
-
-       pub fn get_accept_channel(&self) -> msgs::AcceptChannel {
-               if self.channel_outbound {
-                       panic!("Tried to send accept_channel for an outbound channel?");
-               }
-               if self.channel_state != (ChannelState::OurInitSent as u32) | (ChannelState::TheirInitSent as u32) {
-                       panic!("Tried to send accept_channel after channel had moved forward");
-               }
-               if self.cur_local_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
-                       panic!("Tried to send an accept_channel for a channel that has already advanced");
-               }
-
-               let local_commitment_secret = self.build_local_commitment_secret(self.cur_local_commitment_transaction_number);
-
-               msgs::AcceptChannel {
-                       temporary_channel_id: self.channel_id,
-                       dust_limit_satoshis: self.our_dust_limit_satoshis,
-                       max_htlc_value_in_flight_msat: Channel::get_our_max_htlc_value_in_flight_msat(self.channel_value_satoshis),
-                       channel_reserve_satoshis: Channel::get_our_channel_reserve_satoshis(self.channel_value_satoshis),
-                       htlc_minimum_msat: self.our_htlc_minimum_msat,
-                       minimum_depth: self.minimum_depth,
-                       to_self_delay: self.our_to_self_delay,
-                       max_accepted_htlcs: OUR_MAX_HTLCS,
-                       funding_pubkey: PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.funding_key),
-                       revocation_basepoint: PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.revocation_base_key),
-                       payment_basepoint: PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.payment_base_key),
-                       delayed_payment_basepoint: PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.delayed_payment_base_key),
-                       htlc_basepoint: PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.htlc_base_key),
-                       first_per_commitment_point: PublicKey::from_secret_key(&self.secp_ctx, &local_commitment_secret),
-                       shutdown_scriptpubkey: OptionalField::Present(if self.config.commit_upfront_shutdown_pubkey { self.get_closing_scriptpubkey() } else { Builder::new().into_script() })
-               }
-       }
-
-       /// If an Err is returned, it is a ChannelError::Close (for get_outbound_funding_created)
-       fn get_outbound_funding_created_signature(&mut self) -> Result<(Signature, Transaction), ChannelError> {
-               let funding_script = self.get_funding_redeemscript();
-
-               let remote_keys = self.build_remote_transaction_keys()?;
-               let remote_initial_commitment_tx = self.build_commitment_transaction(self.cur_remote_commitment_transaction_number, &remote_keys, false, false, self.feerate_per_kw).0;
-               let remote_sighash = hash_to_message!(&bip143::SighashComponents::new(&remote_initial_commitment_tx).sighash_all(&remote_initial_commitment_tx.input[0], &funding_script, self.channel_value_satoshis)[..]);
-
-               // We sign the "remote" commitment transaction, allowing them to broadcast the tx if they wish.
-               Ok((self.secp_ctx.sign(&remote_sighash, &self.local_keys.funding_key), remote_initial_commitment_tx))
-       }
-
-       /// Updates channel state with knowledge of the funding transaction's txid/index, and generates
-       /// a funding_created message for the remote peer.
-       /// Panics if called at some time other than immediately after initial handshake, if called twice,
-       /// or if called on an inbound channel.
-       /// Note that channel_id changes during this call!
-       /// Do NOT broadcast the funding transaction until after a successful funding_signed call!
-       /// If an Err is returned, it is a ChannelError::Close.
-       pub fn get_outbound_funding_created(&mut self, funding_txo: OutPoint) -> Result<(msgs::FundingCreated, ChannelMonitor), ChannelError> {
-               if !self.channel_outbound {
-                       panic!("Tried to create outbound funding_created message on an inbound channel!");
-               }
-               if self.channel_state != (ChannelState::OurInitSent as u32 | ChannelState::TheirInitSent as u32) {
-                       panic!("Tried to get a funding_created messsage at a time other than immediately after initial handshake completion (or tried to get funding_created twice)");
-               }
-               if self.channel_monitor.get_min_seen_secret() != (1 << 48) ||
-                               self.cur_remote_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER ||
-                               self.cur_local_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
-                       panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
-               }
-
-               let funding_txo_script = self.get_funding_redeemscript().to_v0_p2wsh();
-               self.channel_monitor.set_funding_info((funding_txo, funding_txo_script));
-
-               let (our_signature, commitment_tx) = match self.get_outbound_funding_created_signature() {
-                       Ok(res) => res,
-                       Err(e) => {
-                               log_error!(self, "Got bad signatures: {:?}!", e);
-                               self.channel_monitor.unset_funding_info();
-                               return Err(e);
-                       }
-               };
-
-               let temporary_channel_id = self.channel_id;
-
-               // Now that we're past error-generating stuff, update our local state:
-               self.channel_monitor.provide_latest_remote_commitment_tx_info(&commitment_tx, Vec::new(), self.cur_remote_commitment_transaction_number, self.their_cur_commitment_point.unwrap());
-               self.channel_state = ChannelState::FundingCreated as u32;
-               self.channel_id = funding_txo.to_channel_id();
-               self.cur_remote_commitment_transaction_number -= 1;
-
-               Ok((msgs::FundingCreated {
-                       temporary_channel_id: temporary_channel_id,
-                       funding_txid: funding_txo.txid,
-                       funding_output_index: funding_txo.index,
-                       signature: our_signature
-               }, self.channel_monitor.clone()))
-       }
-
-       /// Gets an UnsignedChannelAnnouncement, as well as a signature covering it using our
-       /// bitcoin_key, if available, for this channel. The channel must be publicly announceable and
-       /// available for use (have exchanged FundingLocked messages in both directions). Should be used
-       /// for both loose and in response to an AnnouncementSignatures message from the remote peer.
-       /// Will only fail if we're not in a state where channel_announcement may be sent (including
-       /// closing).
-       /// Note that the "channel must be funded" requirement is stricter than BOLT 7 requires - see
-       /// https://github.com/lightningnetwork/lightning-rfc/issues/468
-       pub fn get_channel_announcement(&self, our_node_id: PublicKey, chain_hash: Sha256dHash) -> Result<(msgs::UnsignedChannelAnnouncement, Signature), ChannelError> {
-               if !self.config.announced_channel {
-                       return Err(ChannelError::Ignore("Channel is not available for public announcements"));
-               }
-               if self.channel_state & (ChannelState::ChannelFunded as u32) == 0 {
-                       return Err(ChannelError::Ignore("Cannot get a ChannelAnnouncement until the channel funding has been locked"));
-               }
-               if (self.channel_state & (ChannelState::LocalShutdownSent as u32 | ChannelState::ShutdownComplete as u32)) != 0 {
-                       return Err(ChannelError::Ignore("Cannot get a ChannelAnnouncement once the channel is closing"));
-               }
-
-               let were_node_one = our_node_id.serialize()[..] < self.their_node_id.serialize()[..];
-               let our_bitcoin_key = PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.funding_key);
-
-               let msg = msgs::UnsignedChannelAnnouncement {
-                       features: msgs::GlobalFeatures::new(),
-                       chain_hash: chain_hash,
-                       short_channel_id: self.get_short_channel_id().unwrap(),
-                       node_id_1: if were_node_one { our_node_id } else { self.get_their_node_id() },
-                       node_id_2: if were_node_one { self.get_their_node_id() } else { our_node_id },
-                       bitcoin_key_1: if were_node_one { our_bitcoin_key } else { self.their_funding_pubkey.unwrap() },
-                       bitcoin_key_2: if were_node_one { self.their_funding_pubkey.unwrap() } else { our_bitcoin_key },
-                       excess_data: Vec::new(),
-               };
-
-               let msghash = hash_to_message!(&Sha256dHash::hash(&msg.encode()[..])[..]);
-               let sig = self.secp_ctx.sign(&msghash, &self.local_keys.funding_key);
-
-               Ok((msg, sig))
-       }
-
-       /// May panic if called on a channel that wasn't immediately-previously
-       /// self.remove_uncommitted_htlcs_and_mark_paused()'d
-       pub fn get_channel_reestablish(&self) -> msgs::ChannelReestablish {
-               assert_eq!(self.channel_state & ChannelState::PeerDisconnected as u32, ChannelState::PeerDisconnected as u32);
-               assert_ne!(self.cur_remote_commitment_transaction_number, INITIAL_COMMITMENT_NUMBER);
-               let data_loss_protect = if self.cur_remote_commitment_transaction_number + 1 < INITIAL_COMMITMENT_NUMBER {
-                       let remote_last_secret = self.channel_monitor.get_secret(self.cur_remote_commitment_transaction_number + 2).unwrap();
-                       log_trace!(self, "Enough info to generate a Data Loss Protect with per_commitment_secret {}", log_bytes!(remote_last_secret));
-                       OptionalField::Present(DataLossProtect {
-                               your_last_per_commitment_secret: remote_last_secret,
-                               my_current_per_commitment_point: PublicKey::from_secret_key(&self.secp_ctx, &self.build_local_commitment_secret(self.cur_local_commitment_transaction_number + 1))
-                       })
-               } else {
-                       log_debug!(self, "We don't seen yet any revoked secret, if this channnel has already been updated it means we are fallen-behind, you should wait for other peer closing");
-                       OptionalField::Present(DataLossProtect {
-                               your_last_per_commitment_secret: [0;32],
-                               my_current_per_commitment_point: PublicKey::from_secret_key(&self.secp_ctx, &self.build_local_commitment_secret(self.cur_local_commitment_transaction_number))
-                       })
-               };
-               msgs::ChannelReestablish {
-                       channel_id: self.channel_id(),
-                       // The protocol has two different commitment number concepts - the "commitment
-                       // transaction number", which starts from 0 and counts up, and the "revocation key
-                       // index" which starts at INITIAL_COMMITMENT_NUMBER and counts down. We track
-                       // commitment transaction numbers by the index which will be used to reveal the
-                       // revocation key for that commitment transaction, which means we have to convert them
-                       // to protocol-level commitment numbers here...
-
-                       // next_local_commitment_number is the next commitment_signed number we expect to
-                       // receive (indicating if they need to resend one that we missed).
-                       next_local_commitment_number: INITIAL_COMMITMENT_NUMBER - self.cur_local_commitment_transaction_number,
-                       // We have to set next_remote_commitment_number to the next revoke_and_ack we expect to
-                       // receive, however we track it by the next commitment number for a remote transaction
-                       // (which is one further, as they always revoke previous commitment transaction, not
-                       // the one we send) so we have to decrement by 1. Note that if
-                       // cur_remote_commitment_transaction_number is INITIAL_COMMITMENT_NUMBER we will have
-                       // dropped this channel on disconnect as it hasn't yet reached FundingSent so we can't
-                       // overflow here.
-                       next_remote_commitment_number: INITIAL_COMMITMENT_NUMBER - self.cur_remote_commitment_transaction_number - 1,
-                       data_loss_protect,
-               }
-       }
-
-
-       // Send stuff to our remote peers:
-
-       /// Adds a pending outbound HTLC to this channel, note that you probably want
-       /// send_htlc_and_commit instead cause you'll want both messages at once.
-       /// This returns an option instead of a pure UpdateAddHTLC as we may be in a state where we are
-       /// waiting on the remote peer to send us a revoke_and_ack during which time we cannot add new
-       /// HTLCs on the wire or we wouldn't be able to determine what they actually ACK'ed.
-       /// You MUST call send_commitment prior to any other calls on this Channel
-       /// If an Err is returned, it's a ChannelError::Ignore!
-       pub fn send_htlc(&mut self, amount_msat: u64, payment_hash: PaymentHash, cltv_expiry: u32, source: HTLCSource, onion_routing_packet: msgs::OnionPacket) -> Result<Option<msgs::UpdateAddHTLC>, ChannelError> {
-               if (self.channel_state & (ChannelState::ChannelFunded as u32 | BOTH_SIDES_SHUTDOWN_MASK)) != (ChannelState::ChannelFunded as u32) {
-                       return Err(ChannelError::Ignore("Cannot send HTLC until channel is fully established and we haven't started shutting down"));
-               }
-
-               if amount_msat > self.channel_value_satoshis * 1000 {
-                       return Err(ChannelError::Ignore("Cannot send more than the total value of the channel"));
-               }
-               if amount_msat < self.their_htlc_minimum_msat {
-                       return Err(ChannelError::Ignore("Cannot send less than their minimum HTLC value"));
-               }
-
-               if (self.channel_state & (ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateFailed as u32)) != 0 {
-                       // Note that this should never really happen, if we're !is_live() on receipt of an
-                       // incoming HTLC for relay will result in us rejecting the HTLC and we won't allow
-                       // the user to send directly into a !is_live() channel. However, if we
-                       // disconnected during the time the previous hop was doing the commitment dance we may
-                       // end up getting here after the forwarding delay. In any case, returning an
-                       // IgnoreError will get ChannelManager to do the right thing and fail backwards now.
-                       return Err(ChannelError::Ignore("Cannot send an HTLC while disconnected/frozen for channel monitor update"));
-               }
-
-               let (outbound_htlc_count, htlc_outbound_value_msat) = self.get_outbound_pending_htlc_stats();
-               if outbound_htlc_count + 1 > self.their_max_accepted_htlcs as u32 {
-                       return Err(ChannelError::Ignore("Cannot push more than their max accepted HTLCs"));
-               }
-               // Check their_max_htlc_value_in_flight_msat
-               if htlc_outbound_value_msat + amount_msat > self.their_max_htlc_value_in_flight_msat {
-                       return Err(ChannelError::Ignore("Cannot send value that would put us over the max HTLC value in flight our peer will accept"));
-               }
-
-               // Check self.their_channel_reserve_satoshis (the amount we must keep as
-               // reserve for them to have something to claim if we misbehave)
-               if self.value_to_self_msat < self.their_channel_reserve_satoshis * 1000 + amount_msat + htlc_outbound_value_msat {
-                       return Err(ChannelError::Ignore("Cannot send value that would put us over their reserve value"));
-               }
-
-               //TODO: Check cltv_expiry? Do this in channel manager?
-
-               // Now update local state:
-               if (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32)) == (ChannelState::AwaitingRemoteRevoke as u32) {
-                       self.holding_cell_htlc_updates.push(HTLCUpdateAwaitingACK::AddHTLC {
-                               amount_msat: amount_msat,
-                               payment_hash: payment_hash,
-                               cltv_expiry: cltv_expiry,
-                               source,
-                               onion_routing_packet: onion_routing_packet,
-                       });
-                       return Ok(None);
-               }
-
-               self.pending_outbound_htlcs.push(OutboundHTLCOutput {
-                       htlc_id: self.next_local_htlc_id,
-                       amount_msat: amount_msat,
-                       payment_hash: payment_hash.clone(),
-                       cltv_expiry: cltv_expiry,
-                       state: OutboundHTLCState::LocalAnnounced(Box::new(onion_routing_packet.clone())),
-                       source,
-               });
-
-               let res = msgs::UpdateAddHTLC {
-                       channel_id: self.channel_id,
-                       htlc_id: self.next_local_htlc_id,
-                       amount_msat: amount_msat,
-                       payment_hash: payment_hash,
-                       cltv_expiry: cltv_expiry,
-                       onion_routing_packet: onion_routing_packet,
-               };
-               self.next_local_htlc_id += 1;
-
-               Ok(Some(res))
-       }
-
-       /// Creates a signed commitment transaction to send to the remote peer.
-       /// Always returns a ChannelError::Close if an immediately-preceding (read: the
-       /// last call to this Channel) send_htlc returned Ok(Some(_)) and there is an Err.
-       /// May panic if called except immediately after a successful, Ok(Some(_))-returning send_htlc.
-       pub fn send_commitment(&mut self) -> Result<(msgs::CommitmentSigned, ChannelMonitor), ChannelError> {
-               if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
-                       panic!("Cannot create commitment tx until channel is fully established");
-               }
-               if (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32)) == (ChannelState::AwaitingRemoteRevoke as u32) {
-                       panic!("Cannot create commitment tx until remote revokes their previous commitment");
-               }
-               if (self.channel_state & (ChannelState::PeerDisconnected as u32)) == (ChannelState::PeerDisconnected as u32) {
-                       panic!("Cannot create commitment tx while disconnected, as send_htlc will have returned an Err so a send_commitment precondition has been violated");
-               }
-               if (self.channel_state & (ChannelState::MonitorUpdateFailed as u32)) == (ChannelState::MonitorUpdateFailed as u32) {
-                       panic!("Cannot create commitment tx while awaiting monitor update unfreeze, as send_htlc will have returned an Err so a send_commitment precondition has been violated");
-               }
-               let mut have_updates = self.pending_update_fee.is_some();
-               for htlc in self.pending_outbound_htlcs.iter() {
-                       if let OutboundHTLCState::LocalAnnounced(_) = htlc.state {
-                               have_updates = true;
-                       }
-                       if have_updates { break; }
-               }
-               for htlc in self.pending_inbound_htlcs.iter() {
-                       if let InboundHTLCState::LocalRemoved(_) = htlc.state {
-                               have_updates = true;
-                       }
-                       if have_updates { break; }
-               }
-               if !have_updates {
-                       panic!("Cannot create commitment tx until we have some updates to send");
-               }
-               self.send_commitment_no_status_check()
-       }
-       /// Only fails in case of bad keys
-       fn send_commitment_no_status_check(&mut self) -> Result<(msgs::CommitmentSigned, ChannelMonitor), ChannelError> {
-               // We can upgrade the status of some HTLCs that are waiting on a commitment, even if we
-               // fail to generate this, we still are at least at a position where upgrading their status
-               // is acceptable.
-               for htlc in self.pending_inbound_htlcs.iter_mut() {
-                       let new_state = if let &InboundHTLCState::AwaitingRemoteRevokeToAnnounce(ref forward_info) = &htlc.state {
-                               Some(InboundHTLCState::AwaitingAnnouncedRemoteRevoke(forward_info.clone()))
-                       } else { None };
-                       if let Some(state) = new_state {
-                               htlc.state = state;
-                       }
-               }
-               for htlc in self.pending_outbound_htlcs.iter_mut() {
-                       if let Some(fail_reason) = if let &mut OutboundHTLCState::AwaitingRemoteRevokeToRemove(ref mut fail_reason) = &mut htlc.state {
-                               Some(fail_reason.take())
-                       } else { None } {
-                               htlc.state = OutboundHTLCState::AwaitingRemovedRemoteRevoke(fail_reason);
-                       }
-               }
-               self.resend_order = RAACommitmentOrder::RevokeAndACKFirst;
-
-               let (res, remote_commitment_tx, htlcs) = match self.send_commitment_no_state_update() {
-                       Ok((res, (remote_commitment_tx, mut htlcs))) => {
-                               // Update state now that we've passed all the can-fail calls...
-                               let htlcs_no_ref = htlcs.drain(..).map(|(htlc, htlc_source)| (htlc, htlc_source.map(|source_ref| Box::new(source_ref.clone())))).collect();
-                               (res, remote_commitment_tx, htlcs_no_ref)
-                       },
-                       Err(e) => return Err(e),
-               };
-
-               self.channel_monitor.provide_latest_remote_commitment_tx_info(&remote_commitment_tx, htlcs, self.cur_remote_commitment_transaction_number, self.their_cur_commitment_point.unwrap());
-               self.channel_state |= ChannelState::AwaitingRemoteRevoke as u32;
-               Ok((res, self.channel_monitor.clone()))
-       }
-
-       /// Only fails in case of bad keys. Used for channel_reestablish commitment_signed generation
-       /// when we shouldn't change HTLC/channel state.
-       fn send_commitment_no_state_update(&self) -> Result<(msgs::CommitmentSigned, (Transaction, Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)>)), ChannelError> {
-               let funding_script = self.get_funding_redeemscript();
-
-               let mut feerate_per_kw = self.feerate_per_kw;
-               if let Some(feerate) = self.pending_update_fee {
-                       if self.channel_outbound {
-                               feerate_per_kw = feerate;
-                       }
-               }
-
-               let remote_keys = self.build_remote_transaction_keys()?;
-               let remote_commitment_tx = self.build_commitment_transaction(self.cur_remote_commitment_transaction_number, &remote_keys, false, true, feerate_per_kw);
-               let remote_commitment_txid = remote_commitment_tx.0.txid();
-               let remote_sighash = hash_to_message!(&bip143::SighashComponents::new(&remote_commitment_tx.0).sighash_all(&remote_commitment_tx.0.input[0], &funding_script, self.channel_value_satoshis)[..]);
-               let our_sig = self.secp_ctx.sign(&remote_sighash, &self.local_keys.funding_key);
-               log_trace!(self, "Signing remote commitment tx {} with redeemscript {} with pubkey {} -> {}", encode::serialize_hex(&remote_commitment_tx.0), encode::serialize_hex(&funding_script), log_bytes!(PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.funding_key).serialize()), log_bytes!(our_sig.serialize_compact()[..]));
-
-               let mut htlc_sigs = Vec::with_capacity(remote_commitment_tx.1);
-               for &(ref htlc, _) in remote_commitment_tx.2.iter() {
-                       if let Some(_) = htlc.transaction_output_index {
-                               let htlc_tx = self.build_htlc_transaction(&remote_commitment_txid, htlc, false, &remote_keys, feerate_per_kw);
-                               let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &remote_keys);
-                               let htlc_sighash = hash_to_message!(&bip143::SighashComponents::new(&htlc_tx).sighash_all(&htlc_tx.input[0], &htlc_redeemscript, htlc.amount_msat / 1000)[..]);
-                               let our_htlc_key = secp_check!(chan_utils::derive_private_key(&self.secp_ctx, &remote_keys.per_commitment_point, &self.local_keys.htlc_base_key), "Derived invalid key, peer is maliciously selecting parameters");
-                               htlc_sigs.push(self.secp_ctx.sign(&htlc_sighash, &our_htlc_key));
-                               log_trace!(self, "Signing remote HTLC tx {} with redeemscript {} with pubkey {} -> {}", encode::serialize_hex(&htlc_tx), encode::serialize_hex(&htlc_redeemscript), log_bytes!(PublicKey::from_secret_key(&self.secp_ctx, &our_htlc_key).serialize()), log_bytes!(htlc_sigs.last().unwrap().serialize_compact()[..]));
-                       }
-               }
-
-               Ok((msgs::CommitmentSigned {
-                       channel_id: self.channel_id,
-                       signature: our_sig,
-                       htlc_signatures: htlc_sigs,
-               }, (remote_commitment_tx.0, remote_commitment_tx.2)))
-       }
-
-       /// Adds a pending outbound HTLC to this channel, and creates a signed commitment transaction
-       /// to send to the remote peer in one go.
-       /// Shorthand for calling send_htlc() followed by send_commitment(), see docs on those for
-       /// more info.
-       pub fn send_htlc_and_commit(&mut self, amount_msat: u64, payment_hash: PaymentHash, cltv_expiry: u32, source: HTLCSource, onion_routing_packet: msgs::OnionPacket) -> Result<Option<(msgs::UpdateAddHTLC, msgs::CommitmentSigned, ChannelMonitor)>, ChannelError> {
-               match self.send_htlc(amount_msat, payment_hash, cltv_expiry, source, onion_routing_packet)? {
-                       Some(update_add_htlc) => {
-                               let (commitment_signed, monitor_update) = self.send_commitment_no_status_check()?;
-                               Ok(Some((update_add_htlc, commitment_signed, monitor_update)))
-                       },
-                       None => Ok(None)
-               }
-       }
-
-       /// Begins the shutdown process, getting a message for the remote peer and returning all
-       /// holding cell HTLCs for payment failure.
-       pub fn get_shutdown(&mut self) -> Result<(msgs::Shutdown, Vec<(HTLCSource, PaymentHash)>), APIError> {
-               for htlc in self.pending_outbound_htlcs.iter() {
-                       if let OutboundHTLCState::LocalAnnounced(_) = htlc.state {
-                               return Err(APIError::APIMisuseError{err: "Cannot begin shutdown with pending HTLCs. Process pending events first"});
-                       }
-               }
-               if self.channel_state & BOTH_SIDES_SHUTDOWN_MASK != 0 {
-                       if (self.channel_state & ChannelState::LocalShutdownSent as u32) == ChannelState::LocalShutdownSent as u32 {
-                               return Err(APIError::APIMisuseError{err: "Shutdown already in progress"});
-                       }
-                       else if (self.channel_state & ChannelState::RemoteShutdownSent as u32) == ChannelState::RemoteShutdownSent as u32 {
-                               return Err(APIError::ChannelUnavailable{err: "Shutdown already in progress by remote"});
-                       }
-               }
-               assert_eq!(self.channel_state & ChannelState::ShutdownComplete as u32, 0);
-               if self.channel_state & (ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateFailed as u32) != 0 {
-                       return Err(APIError::ChannelUnavailable{err: "Cannot begin shutdown while peer is disconnected or we're waiting on a monitor update, maybe force-close instead?"});
-               }
-
-               let our_closing_script = self.get_closing_scriptpubkey();
-
-               // From here on out, we may not fail!
-               if self.channel_state < ChannelState::FundingSent as u32 {
-                       self.channel_state = ChannelState::ShutdownComplete as u32;
-               } else {
-                       self.channel_state |= ChannelState::LocalShutdownSent as u32;
-               }
-               self.channel_update_count += 1;
-
-               // Go ahead and drop holding cell updates as we'd rather fail payments than wait to send
-               // our shutdown until we've committed all of the pending changes.
-               self.holding_cell_update_fee = None;
-               let mut dropped_outbound_htlcs = Vec::with_capacity(self.holding_cell_htlc_updates.len());
-               self.holding_cell_htlc_updates.retain(|htlc_update| {
-                       match htlc_update {
-                               &HTLCUpdateAwaitingACK::AddHTLC { ref payment_hash, ref source, .. } => {
-                                       dropped_outbound_htlcs.push((source.clone(), payment_hash.clone()));
-                                       false
-                               },
-                               _ => true
-                       }
-               });
-
-               Ok((msgs::Shutdown {
-                       channel_id: self.channel_id,
-                       scriptpubkey: our_closing_script,
-               }, dropped_outbound_htlcs))
-       }
-
-       /// Gets the latest commitment transaction and any dependent transactions for relay (forcing
-       /// shutdown of this channel - no more calls into this Channel may be made afterwards except
-       /// those explicitly stated to be allowed after shutdown completes, eg some simple getters).
-       /// Also returns the list of payment_hashes for channels which we can safely fail backwards
-       /// immediately (others we will have to allow to time out).
-       pub fn force_shutdown(&mut self) -> (Vec<Transaction>, Vec<(HTLCSource, PaymentHash)>) {
-               assert!(self.channel_state != ChannelState::ShutdownComplete as u32);
-
-               // We go ahead and "free" any holding cell HTLCs or HTLCs we haven't yet committed to and
-               // return them to fail the payment.
-               let mut dropped_outbound_htlcs = Vec::with_capacity(self.holding_cell_htlc_updates.len());
-               for htlc_update in self.holding_cell_htlc_updates.drain(..) {
-                       match htlc_update {
-                               HTLCUpdateAwaitingACK::AddHTLC { source, payment_hash, .. } => {
-                                       dropped_outbound_htlcs.push((source, payment_hash));
-                               },
-                               _ => {}
-                       }
-               }
-
-               for _htlc in self.pending_outbound_htlcs.drain(..) {
-                       //TODO: Do something with the remaining HTLCs
-                       //(we need to have the ChannelManager monitor them so we can claim the inbound HTLCs
-                       //which correspond)
-               }
-
-               self.channel_state = ChannelState::ShutdownComplete as u32;
-               self.channel_update_count += 1;
-               let mut res = Vec::new();
-               mem::swap(&mut res, &mut self.last_local_commitment_txn);
-               (res, dropped_outbound_htlcs)
-       }
-}
-
-const SERIALIZATION_VERSION: u8 = 1;
-const MIN_SERIALIZATION_VERSION: u8 = 1;
-
-impl Writeable for InboundHTLCRemovalReason {
-       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
-               match self {
-                       &InboundHTLCRemovalReason::FailRelay(ref error_packet) => {
-                               0u8.write(writer)?;
-                               error_packet.write(writer)?;
-                       },
-                       &InboundHTLCRemovalReason::FailMalformed((ref onion_hash, ref err_code)) => {
-                               1u8.write(writer)?;
-                               onion_hash.write(writer)?;
-                               err_code.write(writer)?;
-                       },
-                       &InboundHTLCRemovalReason::Fulfill(ref payment_preimage) => {
-                               2u8.write(writer)?;
-                               payment_preimage.write(writer)?;
-                       },
-               }
-               Ok(())
-       }
-}
-
-impl<R: ::std::io::Read> Readable<R> for InboundHTLCRemovalReason {
-       fn read(reader: &mut R) -> Result<Self, DecodeError> {
-               Ok(match <u8 as Readable<R>>::read(reader)? {
-                       0 => InboundHTLCRemovalReason::FailRelay(Readable::read(reader)?),
-                       1 => InboundHTLCRemovalReason::FailMalformed((Readable::read(reader)?, Readable::read(reader)?)),
-                       2 => InboundHTLCRemovalReason::Fulfill(Readable::read(reader)?),
-                       _ => return Err(DecodeError::InvalidValue),
-               })
-       }
-}
-
-impl Writeable for Channel {
-       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
-               // Note that we write out as if remove_uncommitted_htlcs_and_mark_paused had just been
-               // called but include holding cell updates (and obviously we don't modify self).
-
-               writer.write_all(&[SERIALIZATION_VERSION; 1])?;
-               writer.write_all(&[MIN_SERIALIZATION_VERSION; 1])?;
-
-               self.user_id.write(writer)?;
-               self.config.write(writer)?;
-
-               self.channel_id.write(writer)?;
-               (self.channel_state | ChannelState::PeerDisconnected as u32).write(writer)?;
-               self.channel_outbound.write(writer)?;
-               self.channel_value_satoshis.write(writer)?;
-
-               self.local_keys.write(writer)?;
-               self.shutdown_pubkey.write(writer)?;
-
-               self.cur_local_commitment_transaction_number.write(writer)?;
-               self.cur_remote_commitment_transaction_number.write(writer)?;
-               self.value_to_self_msat.write(writer)?;
-
-               let mut dropped_inbound_htlcs = 0;
-               for htlc in self.pending_inbound_htlcs.iter() {
-                       if let InboundHTLCState::RemoteAnnounced(_) = htlc.state {
-                               dropped_inbound_htlcs += 1;
-                       }
-               }
-               (self.pending_inbound_htlcs.len() as u64 - dropped_inbound_htlcs).write(writer)?;
-               for htlc in self.pending_inbound_htlcs.iter() {
-                       htlc.htlc_id.write(writer)?;
-                       htlc.amount_msat.write(writer)?;
-                       htlc.cltv_expiry.write(writer)?;
-                       htlc.payment_hash.write(writer)?;
-                       match &htlc.state {
-                               &InboundHTLCState::RemoteAnnounced(_) => {}, // Drop
-                               &InboundHTLCState::AwaitingRemoteRevokeToAnnounce(ref htlc_state) => {
-                                       1u8.write(writer)?;
-                                       htlc_state.write(writer)?;
-                               },
-                               &InboundHTLCState::AwaitingAnnouncedRemoteRevoke(ref htlc_state) => {
-                                       2u8.write(writer)?;
-                                       htlc_state.write(writer)?;
-                               },
-                               &InboundHTLCState::Committed => {
-                                       3u8.write(writer)?;
-                               },
-                               &InboundHTLCState::LocalRemoved(ref removal_reason) => {
-                                       4u8.write(writer)?;
-                                       removal_reason.write(writer)?;
-                               },
-                       }
-               }
-
-               macro_rules! write_option {
-                       ($thing: expr) => {
-                               match &$thing {
-                                       &None => 0u8.write(writer)?,
-                                       &Some(ref v) => {
-                                               1u8.write(writer)?;
-                                               v.write(writer)?;
-                                       },
-                               }
-                       }
-               }
-
-               (self.pending_outbound_htlcs.len() as u64).write(writer)?;
-               for htlc in self.pending_outbound_htlcs.iter() {
-                       htlc.htlc_id.write(writer)?;
-                       htlc.amount_msat.write(writer)?;
-                       htlc.cltv_expiry.write(writer)?;
-                       htlc.payment_hash.write(writer)?;
-                       htlc.source.write(writer)?;
-                       match &htlc.state {
-                               &OutboundHTLCState::LocalAnnounced(ref onion_packet) => {
-                                       0u8.write(writer)?;
-                                       onion_packet.write(writer)?;
-                               },
-                               &OutboundHTLCState::Committed => {
-                                       1u8.write(writer)?;
-                               },
-                               &OutboundHTLCState::RemoteRemoved(ref fail_reason) => {
-                                       2u8.write(writer)?;
-                                       write_option!(*fail_reason);
-                               },
-                               &OutboundHTLCState::AwaitingRemoteRevokeToRemove(ref fail_reason) => {
-                                       3u8.write(writer)?;
-                                       write_option!(*fail_reason);
-                               },
-                               &OutboundHTLCState::AwaitingRemovedRemoteRevoke(ref fail_reason) => {
-                                       4u8.write(writer)?;
-                                       write_option!(*fail_reason);
-                               },
-                       }
-               }
-
-               (self.holding_cell_htlc_updates.len() as u64).write(writer)?;
-               for update in self.holding_cell_htlc_updates.iter() {
-                       match update {
-                               &HTLCUpdateAwaitingACK::AddHTLC { ref amount_msat, ref cltv_expiry, ref payment_hash, ref source, ref onion_routing_packet } => {
-                                       0u8.write(writer)?;
-                                       amount_msat.write(writer)?;
-                                       cltv_expiry.write(writer)?;
-                                       payment_hash.write(writer)?;
-                                       source.write(writer)?;
-                                       onion_routing_packet.write(writer)?;
-                               },
-                               &HTLCUpdateAwaitingACK::ClaimHTLC { ref payment_preimage, ref htlc_id } => {
-                                       1u8.write(writer)?;
-                                       payment_preimage.write(writer)?;
-                                       htlc_id.write(writer)?;
-                               },
-                               &HTLCUpdateAwaitingACK::FailHTLC { ref htlc_id, ref err_packet } => {
-                                       2u8.write(writer)?;
-                                       htlc_id.write(writer)?;
-                                       err_packet.write(writer)?;
-                               }
-                       }
-               }
-
-               match self.resend_order {
-                       RAACommitmentOrder::CommitmentFirst => 0u8.write(writer)?,
-                       RAACommitmentOrder::RevokeAndACKFirst => 1u8.write(writer)?,
-               }
-
-               self.monitor_pending_funding_locked.write(writer)?;
-               self.monitor_pending_revoke_and_ack.write(writer)?;
-               self.monitor_pending_commitment_signed.write(writer)?;
-
-               (self.monitor_pending_forwards.len() as u64).write(writer)?;
-               for &(ref pending_forward, ref htlc_id) in self.monitor_pending_forwards.iter() {
-                       pending_forward.write(writer)?;
-                       htlc_id.write(writer)?;
-               }
-
-               (self.monitor_pending_failures.len() as u64).write(writer)?;
-               for &(ref htlc_source, ref payment_hash, ref fail_reason) in self.monitor_pending_failures.iter() {
-                       htlc_source.write(writer)?;
-                       payment_hash.write(writer)?;
-                       fail_reason.write(writer)?;
-               }
-
-               write_option!(self.pending_update_fee);
-               write_option!(self.holding_cell_update_fee);
-
-               self.next_local_htlc_id.write(writer)?;
-               (self.next_remote_htlc_id - dropped_inbound_htlcs).write(writer)?;
-               self.channel_update_count.write(writer)?;
-               self.feerate_per_kw.write(writer)?;
-
-               (self.last_local_commitment_txn.len() as u64).write(writer)?;
-               for tx in self.last_local_commitment_txn.iter() {
-                       if let Err(e) = tx.consensus_encode(&mut WriterWriteAdaptor(writer)) {
-                               match e {
-                                       encode::Error::Io(e) => return Err(e),
-                                       _ => panic!("last_local_commitment_txn must have been well-formed!"),
-                               }
-                       }
-               }
-
-               match self.last_sent_closing_fee {
-                       Some((feerate, fee)) => {
-                               1u8.write(writer)?;
-                               feerate.write(writer)?;
-                               fee.write(writer)?;
-                       },
-                       None => 0u8.write(writer)?,
-               }
-
-               write_option!(self.funding_tx_confirmed_in);
-               write_option!(self.short_channel_id);
-
-               self.last_block_connected.write(writer)?;
-               self.funding_tx_confirmations.write(writer)?;
-
-               self.their_dust_limit_satoshis.write(writer)?;
-               self.our_dust_limit_satoshis.write(writer)?;
-               self.their_max_htlc_value_in_flight_msat.write(writer)?;
-               self.their_channel_reserve_satoshis.write(writer)?;
-               self.their_htlc_minimum_msat.write(writer)?;
-               self.our_htlc_minimum_msat.write(writer)?;
-               self.their_to_self_delay.write(writer)?;
-               self.our_to_self_delay.write(writer)?;
-               self.their_max_accepted_htlcs.write(writer)?;
-               self.minimum_depth.write(writer)?;
-
-               write_option!(self.their_funding_pubkey);
-               write_option!(self.their_revocation_basepoint);
-               write_option!(self.their_payment_basepoint);
-               write_option!(self.their_delayed_payment_basepoint);
-               write_option!(self.their_htlc_basepoint);
-               write_option!(self.their_cur_commitment_point);
-
-               write_option!(self.their_prev_commitment_point);
-               self.their_node_id.write(writer)?;
-
-               write_option!(self.their_shutdown_scriptpubkey);
-
-               self.channel_monitor.write_for_disk(writer)?;
-               Ok(())
-       }
-}
-
-impl<R : ::std::io::Read> ReadableArgs<R, Arc<Logger>> for Channel {
-       fn read(reader: &mut R, logger: Arc<Logger>) -> Result<Self, DecodeError> {
-               let _ver: u8 = Readable::read(reader)?;
-               let min_ver: u8 = Readable::read(reader)?;
-               if min_ver > SERIALIZATION_VERSION {
-                       return Err(DecodeError::UnknownVersion);
-               }
-
-               let user_id = Readable::read(reader)?;
-               let config: ChannelConfig = Readable::read(reader)?;
-
-               let channel_id = Readable::read(reader)?;
-               let channel_state = Readable::read(reader)?;
-               let channel_outbound = Readable::read(reader)?;
-               let channel_value_satoshis = Readable::read(reader)?;
-
-               let local_keys = Readable::read(reader)?;
-               let shutdown_pubkey = Readable::read(reader)?;
-
-               let cur_local_commitment_transaction_number = Readable::read(reader)?;
-               let cur_remote_commitment_transaction_number = Readable::read(reader)?;
-               let value_to_self_msat = Readable::read(reader)?;
-
-               let pending_inbound_htlc_count: u64 = Readable::read(reader)?;
-               let mut pending_inbound_htlcs = Vec::with_capacity(cmp::min(pending_inbound_htlc_count as usize, OUR_MAX_HTLCS as usize));
-               for _ in 0..pending_inbound_htlc_count {
-                       pending_inbound_htlcs.push(InboundHTLCOutput {
-                               htlc_id: Readable::read(reader)?,
-                               amount_msat: Readable::read(reader)?,
-                               cltv_expiry: Readable::read(reader)?,
-                               payment_hash: Readable::read(reader)?,
-                               state: match <u8 as Readable<R>>::read(reader)? {
-                                       1 => InboundHTLCState::AwaitingRemoteRevokeToAnnounce(Readable::read(reader)?),
-                                       2 => InboundHTLCState::AwaitingAnnouncedRemoteRevoke(Readable::read(reader)?),
-                                       3 => InboundHTLCState::Committed,
-                                       4 => InboundHTLCState::LocalRemoved(Readable::read(reader)?),
-                                       _ => return Err(DecodeError::InvalidValue),
-                               },
-                       });
-               }
-
-               let pending_outbound_htlc_count: u64 = Readable::read(reader)?;
-               let mut pending_outbound_htlcs = Vec::with_capacity(cmp::min(pending_outbound_htlc_count as usize, OUR_MAX_HTLCS as usize));
-               for _ in 0..pending_outbound_htlc_count {
-                       pending_outbound_htlcs.push(OutboundHTLCOutput {
-                               htlc_id: Readable::read(reader)?,
-                               amount_msat: Readable::read(reader)?,
-                               cltv_expiry: Readable::read(reader)?,
-                               payment_hash: Readable::read(reader)?,
-                               source: Readable::read(reader)?,
-                               state: match <u8 as Readable<R>>::read(reader)? {
-                                       0 => OutboundHTLCState::LocalAnnounced(Box::new(Readable::read(reader)?)),
-                                       1 => OutboundHTLCState::Committed,
-                                       2 => OutboundHTLCState::RemoteRemoved(Readable::read(reader)?),
-                                       3 => OutboundHTLCState::AwaitingRemoteRevokeToRemove(Readable::read(reader)?),
-                                       4 => OutboundHTLCState::AwaitingRemovedRemoteRevoke(Readable::read(reader)?),
-                                       _ => return Err(DecodeError::InvalidValue),
-                               },
-                       });
-               }
-
-               let holding_cell_htlc_update_count: u64 = Readable::read(reader)?;
-               let mut holding_cell_htlc_updates = Vec::with_capacity(cmp::min(holding_cell_htlc_update_count as usize, OUR_MAX_HTLCS as usize*2));
-               for _ in 0..holding_cell_htlc_update_count {
-                       holding_cell_htlc_updates.push(match <u8 as Readable<R>>::read(reader)? {
-                               0 => HTLCUpdateAwaitingACK::AddHTLC {
-                                       amount_msat: Readable::read(reader)?,
-                                       cltv_expiry: Readable::read(reader)?,
-                                       payment_hash: Readable::read(reader)?,
-                                       source: Readable::read(reader)?,
-                                       onion_routing_packet: Readable::read(reader)?,
-                               },
-                               1 => HTLCUpdateAwaitingACK::ClaimHTLC {
-                                       payment_preimage: Readable::read(reader)?,
-                                       htlc_id: Readable::read(reader)?,
-                               },
-                               2 => HTLCUpdateAwaitingACK::FailHTLC {
-                                       htlc_id: Readable::read(reader)?,
-                                       err_packet: Readable::read(reader)?,
-                               },
-                               _ => return Err(DecodeError::InvalidValue),
-                       });
-               }
-
-               let resend_order = match <u8 as Readable<R>>::read(reader)? {
-                       0 => RAACommitmentOrder::CommitmentFirst,
-                       1 => RAACommitmentOrder::RevokeAndACKFirst,
-                       _ => return Err(DecodeError::InvalidValue),
-               };
-
-               let monitor_pending_funding_locked = Readable::read(reader)?;
-               let monitor_pending_revoke_and_ack = Readable::read(reader)?;
-               let monitor_pending_commitment_signed = Readable::read(reader)?;
-
-               let monitor_pending_forwards_count: u64 = Readable::read(reader)?;
-               let mut monitor_pending_forwards = Vec::with_capacity(cmp::min(monitor_pending_forwards_count as usize, OUR_MAX_HTLCS as usize));
-               for _ in 0..monitor_pending_forwards_count {
-                       monitor_pending_forwards.push((Readable::read(reader)?, Readable::read(reader)?));
-               }
-
-               let monitor_pending_failures_count: u64 = Readable::read(reader)?;
-               let mut monitor_pending_failures = Vec::with_capacity(cmp::min(monitor_pending_failures_count as usize, OUR_MAX_HTLCS as usize));
-               for _ in 0..monitor_pending_failures_count {
-                       monitor_pending_failures.push((Readable::read(reader)?, Readable::read(reader)?, Readable::read(reader)?));
-               }
-
-               let pending_update_fee = Readable::read(reader)?;
-               let holding_cell_update_fee = Readable::read(reader)?;
-
-               let next_local_htlc_id = Readable::read(reader)?;
-               let next_remote_htlc_id = Readable::read(reader)?;
-               let channel_update_count = Readable::read(reader)?;
-               let feerate_per_kw = Readable::read(reader)?;
-
-               let last_local_commitment_txn_count: u64 = Readable::read(reader)?;
-               let mut last_local_commitment_txn = Vec::with_capacity(cmp::min(last_local_commitment_txn_count as usize, OUR_MAX_HTLCS as usize*2 + 1));
-               for _ in 0..last_local_commitment_txn_count {
-                       last_local_commitment_txn.push(match Transaction::consensus_decode(reader.by_ref()) {
-                               Ok(tx) => tx,
-                               Err(_) => return Err(DecodeError::InvalidValue),
-                       });
-               }
-
-               let last_sent_closing_fee = match <u8 as Readable<R>>::read(reader)? {
-                       0 => None,
-                       1 => Some((Readable::read(reader)?, Readable::read(reader)?)),
-                       _ => return Err(DecodeError::InvalidValue),
-               };
-
-               let funding_tx_confirmed_in = Readable::read(reader)?;
-               let short_channel_id = Readable::read(reader)?;
-
-               let last_block_connected = Readable::read(reader)?;
-               let funding_tx_confirmations = Readable::read(reader)?;
-
-               let their_dust_limit_satoshis = Readable::read(reader)?;
-               let our_dust_limit_satoshis = Readable::read(reader)?;
-               let their_max_htlc_value_in_flight_msat = Readable::read(reader)?;
-               let their_channel_reserve_satoshis = Readable::read(reader)?;
-               let their_htlc_minimum_msat = Readable::read(reader)?;
-               let our_htlc_minimum_msat = Readable::read(reader)?;
-               let their_to_self_delay = Readable::read(reader)?;
-               let our_to_self_delay = Readable::read(reader)?;
-               let their_max_accepted_htlcs = Readable::read(reader)?;
-               let minimum_depth = Readable::read(reader)?;
-
-               let their_funding_pubkey = Readable::read(reader)?;
-               let their_revocation_basepoint = Readable::read(reader)?;
-               let their_payment_basepoint = Readable::read(reader)?;
-               let their_delayed_payment_basepoint = Readable::read(reader)?;
-               let their_htlc_basepoint = Readable::read(reader)?;
-               let their_cur_commitment_point = Readable::read(reader)?;
-
-               let their_prev_commitment_point = Readable::read(reader)?;
-               let their_node_id = Readable::read(reader)?;
-
-               let their_shutdown_scriptpubkey = Readable::read(reader)?;
-               let (monitor_last_block, channel_monitor) = ReadableArgs::read(reader, logger.clone())?;
-               // We drop the ChannelMonitor's last block connected hash cause we don't actually bother
-               // doing full block connection operations on the internal CHannelMonitor copies
-               if monitor_last_block != last_block_connected {
-                       return Err(DecodeError::InvalidValue);
-               }
-
-               Ok(Channel {
-                       user_id,
-
-                       config,
-                       channel_id,
-                       channel_state,
-                       channel_outbound,
-                       secp_ctx: Secp256k1::new(),
-                       channel_value_satoshis,
-
-                       local_keys,
-                       shutdown_pubkey,
-
-                       cur_local_commitment_transaction_number,
-                       cur_remote_commitment_transaction_number,
-                       value_to_self_msat,
-
-                       pending_inbound_htlcs,
-                       pending_outbound_htlcs,
-                       holding_cell_htlc_updates,
-
-                       resend_order,
-
-                       monitor_pending_funding_locked,
-                       monitor_pending_revoke_and_ack,
-                       monitor_pending_commitment_signed,
-                       monitor_pending_forwards,
-                       monitor_pending_failures,
-
-                       pending_update_fee,
-                       holding_cell_update_fee,
-                       next_local_htlc_id,
-                       next_remote_htlc_id,
-                       channel_update_count,
-                       feerate_per_kw,
-
-                       #[cfg(debug_assertions)]
-                       max_commitment_tx_output_local: ::std::sync::Mutex::new((0, 0)),
-                       #[cfg(debug_assertions)]
-                       max_commitment_tx_output_remote: ::std::sync::Mutex::new((0, 0)),
-
-                       last_local_commitment_txn,
-
-                       last_sent_closing_fee,
-
-                       funding_tx_confirmed_in,
-                       short_channel_id,
-                       last_block_connected,
-                       funding_tx_confirmations,
-
-                       their_dust_limit_satoshis,
-                       our_dust_limit_satoshis,
-                       their_max_htlc_value_in_flight_msat,
-                       their_channel_reserve_satoshis,
-                       their_htlc_minimum_msat,
-                       our_htlc_minimum_msat,
-                       their_to_self_delay,
-                       our_to_self_delay,
-                       their_max_accepted_htlcs,
-                       minimum_depth,
-
-                       their_funding_pubkey,
-                       their_revocation_basepoint,
-                       their_payment_basepoint,
-                       their_delayed_payment_basepoint,
-                       their_htlc_basepoint,
-                       their_cur_commitment_point,
-
-                       their_prev_commitment_point,
-                       their_node_id,
-
-                       their_shutdown_scriptpubkey,
-
-                       channel_monitor,
-
-                       logger,
-               })
-       }
-}
-
-#[cfg(test)]
-mod tests {
-       use bitcoin::util::bip143;
-       use bitcoin::consensus::encode::serialize;
-       use bitcoin::blockdata::script::{Script, Builder};
-       use bitcoin::blockdata::transaction::Transaction;
-       use bitcoin::blockdata::opcodes;
-       use bitcoin_hashes::hex::FromHex;
-       use hex;
-       use ln::channelmanager::{HTLCSource, PaymentPreimage, PaymentHash};
-       use ln::channel::{Channel,ChannelKeys,InboundHTLCOutput,OutboundHTLCOutput,InboundHTLCState,OutboundHTLCState,HTLCOutputInCommitment,TxCreationKeys};
-       use ln::channel::MAX_FUNDING_SATOSHIS;
-       use ln::chan_utils;
-       use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
-       use chain::keysinterface::KeysInterface;
-       use chain::transaction::OutPoint;
-       use util::config::UserConfig;
-       use util::test_utils;
-       use util::logger::Logger;
-       use secp256k1::{Secp256k1,Message,Signature};
-       use secp256k1::key::{SecretKey,PublicKey};
-       use bitcoin_hashes::sha256::Hash as Sha256;
-       use bitcoin_hashes::sha256d::Hash as Sha256dHash;
-       use bitcoin_hashes::hash160::Hash as Hash160;
-       use bitcoin_hashes::Hash;
-       use std::sync::Arc;
-
-       struct TestFeeEstimator {
-               fee_est: u64
-       }
-       impl FeeEstimator for TestFeeEstimator {
-               fn get_est_sat_per_1000_weight(&self, _: ConfirmationTarget) -> u64 {
-                       self.fee_est
-               }
-       }
-
-       #[test]
-       fn test_max_funding_satoshis() {
-               assert!(MAX_FUNDING_SATOSHIS <= 21_000_000 * 100_000_000,
-                       "MAX_FUNDING_SATOSHIS is greater than all satoshis in existence");
-       }
-
-       struct Keys {
-               chan_keys: ChannelKeys,
-       }
-       impl KeysInterface for Keys {
-               fn get_node_secret(&self) -> SecretKey { panic!(); }
-               fn get_destination_script(&self) -> Script {
-                       let secp_ctx = Secp256k1::signing_only();
-                       let channel_monitor_claim_key = SecretKey::from_slice(&hex::decode("0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap()[..]).unwrap();
-                       let our_channel_monitor_claim_key_hash = Hash160::hash(&PublicKey::from_secret_key(&secp_ctx, &channel_monitor_claim_key).serialize());
-                       Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(&our_channel_monitor_claim_key_hash[..]).into_script()
-               }
-
-               fn get_shutdown_pubkey(&self) -> PublicKey {
-                       let secp_ctx = Secp256k1::signing_only();
-                       let channel_close_key = SecretKey::from_slice(&hex::decode("0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap()[..]).unwrap();
-                       PublicKey::from_secret_key(&secp_ctx, &channel_close_key)
-               }
-
-               fn get_channel_keys(&self, _inbound: bool) -> ChannelKeys { self.chan_keys.clone() }
-               fn get_session_key(&self) -> SecretKey { panic!(); }
-               fn get_channel_id(&self) -> [u8; 32] { [0; 32] }
-       }
-
-       #[test]
-       fn outbound_commitment_test() {
-               // Test vectors from BOLT 3 Appendix C:
-               let feeest = TestFeeEstimator{fee_est: 15000};
-               let logger : Arc<Logger> = Arc::new(test_utils::TestLogger::new());
-               let secp_ctx = Secp256k1::new();
-
-               let chan_keys = ChannelKeys {
-                       funding_key: SecretKey::from_slice(&hex::decode("30ff4956bbdd3222d44cc5e8a1261dab1e07957bdac5ae88fe3261ef321f3749").unwrap()[..]).unwrap(),
-                       payment_base_key: SecretKey::from_slice(&hex::decode("1111111111111111111111111111111111111111111111111111111111111111").unwrap()[..]).unwrap(),
-                       delayed_payment_base_key: SecretKey::from_slice(&hex::decode("3333333333333333333333333333333333333333333333333333333333333333").unwrap()[..]).unwrap(),
-                       htlc_base_key: SecretKey::from_slice(&hex::decode("1111111111111111111111111111111111111111111111111111111111111111").unwrap()[..]).unwrap(),
-
-                       // These aren't set in the test vectors:
-                       revocation_base_key: SecretKey::from_slice(&hex::decode("0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap()[..]).unwrap(),
-                       commitment_seed: [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff],
-               };
-               assert_eq!(PublicKey::from_secret_key(&secp_ctx, &chan_keys.funding_key).serialize()[..],
-                               hex::decode("023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb").unwrap()[..]);
-               let keys_provider: Arc<KeysInterface> = Arc::new(Keys { chan_keys });
-
-               let their_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
-               let mut config = UserConfig::new();
-               config.channel_options.announced_channel = false;
-               let mut chan = Channel::new_outbound(&feeest, &keys_provider, their_node_id, 10000000, 100000, 42, Arc::clone(&logger), &config).unwrap(); // Nothing uses their network key in this test
-               chan.their_to_self_delay = 144;
-               chan.our_dust_limit_satoshis = 546;
-
-               let funding_info = OutPoint::new(Sha256dHash::from_hex("8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be").unwrap(), 0);
-               chan.channel_monitor.set_funding_info((funding_info, Script::new()));
-
-               chan.their_payment_basepoint = Some(PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("4444444444444444444444444444444444444444444444444444444444444444").unwrap()[..]).unwrap()));
-               assert_eq!(chan.their_payment_basepoint.unwrap().serialize()[..],
-                               hex::decode("032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991").unwrap()[..]);
-
-               chan.their_funding_pubkey = Some(PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("1552dfba4f6cf29a62a0af13c8d6981d36d0ef8d61ba10fb0fe90da7634d7e13").unwrap()[..]).unwrap()));
-               assert_eq!(chan.their_funding_pubkey.unwrap().serialize()[..],
-                               hex::decode("030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c1").unwrap()[..]);
-
-               chan.their_htlc_basepoint = Some(PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("4444444444444444444444444444444444444444444444444444444444444444").unwrap()[..]).unwrap()));
-               assert_eq!(chan.their_htlc_basepoint.unwrap().serialize()[..],
-                               hex::decode("032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991").unwrap()[..]);
-
-               chan.their_revocation_basepoint = Some(PublicKey::from_slice(&hex::decode("02466d7fcae563e5cb09a0d1870bb580344804617879a14949cf22285f1bae3f27").unwrap()[..]).unwrap());
-
-               // We can't just use build_local_transaction_keys here as the per_commitment_secret is not
-               // derived from a commitment_seed, so instead we copy it here and call
-               // build_commitment_transaction.
-               let delayed_payment_base = PublicKey::from_secret_key(&secp_ctx, &chan.local_keys.delayed_payment_base_key);
-               let per_commitment_secret = SecretKey::from_slice(&hex::decode("1f1e1d1c1b1a191817161514131211100f0e0d0c0b0a09080706050403020100").unwrap()[..]).unwrap();
-               let per_commitment_point = PublicKey::from_secret_key(&secp_ctx, &per_commitment_secret);
-               let htlc_basepoint = PublicKey::from_secret_key(&secp_ctx, &chan.local_keys.htlc_base_key);
-               let keys = TxCreationKeys::new(&secp_ctx, &per_commitment_point, &delayed_payment_base, &htlc_basepoint, &chan.their_revocation_basepoint.unwrap(), &chan.their_payment_basepoint.unwrap(), &chan.their_htlc_basepoint.unwrap()).unwrap();
-
-               let mut unsigned_tx: (Transaction, Vec<HTLCOutputInCommitment>);
-
-               macro_rules! test_commitment {
-                       ( $their_sig_hex: expr, $our_sig_hex: expr, $tx_hex: expr) => {
-                               unsigned_tx = {
-                                       let mut res = chan.build_commitment_transaction(0xffffffffffff - 42, &keys, true, false, chan.feerate_per_kw);
-                                       let htlcs = res.2.drain(..)
-                                               .filter_map(|(htlc, _)| if htlc.transaction_output_index.is_some() { Some(htlc) } else { None })
-                                               .collect();
-                                       (res.0, htlcs)
-                               };
-                               let their_signature = Signature::from_der(&hex::decode($their_sig_hex).unwrap()[..]).unwrap();
-                               let sighash = Message::from_slice(&bip143::SighashComponents::new(&unsigned_tx.0).sighash_all(&unsigned_tx.0.input[0], &chan.get_funding_redeemscript(), chan.channel_value_satoshis)[..]).unwrap();
-                               secp_ctx.verify(&sighash, &their_signature, &chan.their_funding_pubkey.unwrap()).unwrap();
-
-                               chan.sign_commitment_transaction(&mut unsigned_tx.0, &their_signature);
-
-                               assert_eq!(serialize(&unsigned_tx.0)[..],
-                                               hex::decode($tx_hex).unwrap()[..]);
-                       };
-               }
-
-               macro_rules! test_htlc_output {
-                       ( $htlc_idx: expr, $their_sig_hex: expr, $our_sig_hex: expr, $tx_hex: expr ) => {
-                               let remote_signature = Signature::from_der(&hex::decode($their_sig_hex).unwrap()[..]).unwrap();
-
-                               let ref htlc = unsigned_tx.1[$htlc_idx];
-                               let mut htlc_tx = chan.build_htlc_transaction(&unsigned_tx.0.txid(), &htlc, true, &keys, chan.feerate_per_kw);
-                               let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &keys);
-                               let htlc_sighash = Message::from_slice(&bip143::SighashComponents::new(&htlc_tx).sighash_all(&htlc_tx.input[0], &htlc_redeemscript, htlc.amount_msat / 1000)[..]).unwrap();
-                               secp_ctx.verify(&htlc_sighash, &remote_signature, &keys.b_htlc_key).unwrap();
-
-                               let mut preimage: Option<PaymentPreimage> = None;
-                               if !htlc.offered {
-                                       for i in 0..5 {
-                                               let out = PaymentHash(Sha256::hash(&[i; 32]).into_inner());
-                                               if out == htlc.payment_hash {
-                                                       preimage = Some(PaymentPreimage([i; 32]));
-                                               }
-                                       }
-
-                                       assert!(preimage.is_some());
-                               }
-
-                               chan.sign_htlc_transaction(&mut htlc_tx, &remote_signature, &preimage, &htlc, &keys).unwrap();
-                               assert_eq!(serialize(&htlc_tx)[..],
-                                               hex::decode($tx_hex).unwrap()[..]);
-                       };
-               }
-
-               {
-                       // simple commitment tx with no HTLCs
-                       chan.value_to_self_msat = 7000000000;
-
-                       test_commitment!("3045022100f51d2e566a70ba740fc5d8c0f07b9b93d2ed741c3c0860c613173de7d39e7968022041376d520e9c0e1ad52248ddf4b22e12be8763007df977253ef45a4ca3bdb7c0",
-                                        "3044022051b75c73198c6deee1a875871c3961832909acd297c6b908d59e3319e5185a46022055c419379c5051a78d00dbbce11b5b664a0c22815fbcc6fcef6b1937c3836939",
-                                        "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8002c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de84311054a56a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0400473044022051b75c73198c6deee1a875871c3961832909acd297c6b908d59e3319e5185a46022055c419379c5051a78d00dbbce11b5b664a0c22815fbcc6fcef6b1937c383693901483045022100f51d2e566a70ba740fc5d8c0f07b9b93d2ed741c3c0860c613173de7d39e7968022041376d520e9c0e1ad52248ddf4b22e12be8763007df977253ef45a4ca3bdb7c001475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220");
-               }
-
-               chan.pending_inbound_htlcs.push({
-                       let mut out = InboundHTLCOutput{
-                               htlc_id: 0,
-                               amount_msat: 1000000,
-                               cltv_expiry: 500,
-                               payment_hash: PaymentHash([0; 32]),
-                               state: InboundHTLCState::Committed,
-                       };
-                       out.payment_hash.0 = Sha256::hash(&hex::decode("0000000000000000000000000000000000000000000000000000000000000000").unwrap()).into_inner();
-                       out
-               });
-               chan.pending_inbound_htlcs.push({
-                       let mut out = InboundHTLCOutput{
-                               htlc_id: 1,
-                               amount_msat: 2000000,
-                               cltv_expiry: 501,
-                               payment_hash: PaymentHash([0; 32]),
-                               state: InboundHTLCState::Committed,
-                       };
-                       out.payment_hash.0 = Sha256::hash(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()).into_inner();
-                       out
-               });
-               chan.pending_outbound_htlcs.push({
-                       let mut out = OutboundHTLCOutput{
-                               htlc_id: 2,
-                               amount_msat: 2000000,
-                               cltv_expiry: 502,
-                               payment_hash: PaymentHash([0; 32]),
-                               state: OutboundHTLCState::Committed,
-                               source: HTLCSource::dummy(),
-                       };
-                       out.payment_hash.0 = Sha256::hash(&hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap()).into_inner();
-                       out
-               });
-               chan.pending_outbound_htlcs.push({
-                       let mut out = OutboundHTLCOutput{
-                               htlc_id: 3,
-                               amount_msat: 3000000,
-                               cltv_expiry: 503,
-                               payment_hash: PaymentHash([0; 32]),
-                               state: OutboundHTLCState::Committed,
-                               source: HTLCSource::dummy(),
-                       };
-                       out.payment_hash.0 = Sha256::hash(&hex::decode("0303030303030303030303030303030303030303030303030303030303030303").unwrap()).into_inner();
-                       out
-               });
-               chan.pending_inbound_htlcs.push({
-                       let mut out = InboundHTLCOutput{
-                               htlc_id: 4,
-                               amount_msat: 4000000,
-                               cltv_expiry: 504,
-                               payment_hash: PaymentHash([0; 32]),
-                               state: InboundHTLCState::Committed,
-                       };
-                       out.payment_hash.0 = Sha256::hash(&hex::decode("0404040404040404040404040404040404040404040404040404040404040404").unwrap()).into_inner();
-                       out
-               });
-
-               {
-                       // commitment tx with all five HTLCs untrimmed (minimum feerate)
-                       chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
-                       chan.feerate_per_kw = 0;
-
-                       test_commitment!("304402204fd4928835db1ccdfc40f5c78ce9bd65249b16348df81f0c44328dcdefc97d630220194d3869c38bc732dd87d13d2958015e2fc16829e74cd4377f84d215c0b70606",
-                                        "30440220275b0c325a5e9355650dc30c0eccfbc7efb23987c24b556b9dfdd40effca18d202206caceb2c067836c51f296740c7ae807ffcbfbf1dd3a0d56b6de9a5b247985f06",
-                                        "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8007e80300000000000022002052bfef0479d7b293c27e0f1eb294bea154c63a3294ef092c19af51409bce0e2ad007000000000000220020403d394747cae42e98ff01734ad5c08f82ba123d3d9a620abda88989651e2ab5d007000000000000220020748eba944fedc8827f6b06bc44678f93c0f9e6078b35c6331ed31e75f8ce0c2db80b000000000000220020c20b5d1f8584fd90443e7b7b720136174fa4b9333c261d04dbbd012635c0f419a00f0000000000002200208c48d15160397c9731df9bc3b236656efb6665fbfe92b4a6878e88a499f741c4c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de843110e0a06a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e04004730440220275b0c325a5e9355650dc30c0eccfbc7efb23987c24b556b9dfdd40effca18d202206caceb2c067836c51f296740c7ae807ffcbfbf1dd3a0d56b6de9a5b247985f060147304402204fd4928835db1ccdfc40f5c78ce9bd65249b16348df81f0c44328dcdefc97d630220194d3869c38bc732dd87d13d2958015e2fc16829e74cd4377f84d215c0b7060601475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220");
-
-                       assert_eq!(unsigned_tx.1.len(), 5);
-
-                       test_htlc_output!(0,
-                                         "304402206a6e59f18764a5bf8d4fa45eebc591566689441229c918b480fb2af8cc6a4aeb02205248f273be447684b33e3c8d1d85a8e0ca9fa0bae9ae33f0527ada9c162919a6",
-                                         "304402207cb324fa0de88f452ffa9389678127ebcf4cabe1dd848b8e076c1a1962bf34720220116ed922b12311bd602d67e60d2529917f21c5b82f25ff6506c0f87886b4dfd5",
-                                         "020000000001018154ecccf11a5fb56c39654c4deb4d2296f83c69268280b94d021370c94e219700000000000000000001e8030000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e050047304402206a6e59f18764a5bf8d4fa45eebc591566689441229c918b480fb2af8cc6a4aeb02205248f273be447684b33e3c8d1d85a8e0ca9fa0bae9ae33f0527ada9c162919a60147304402207cb324fa0de88f452ffa9389678127ebcf4cabe1dd848b8e076c1a1962bf34720220116ed922b12311bd602d67e60d2529917f21c5b82f25ff6506c0f87886b4dfd5012000000000000000000000000000000000000000000000000000000000000000008a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a914b8bcb07f6344b42ab04250c86a6e8b75d3fdbbc688527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f401b175ac686800000000");
-
-                       test_htlc_output!(1,
-                                         "3045022100d5275b3619953cb0c3b5aa577f04bc512380e60fa551762ce3d7a1bb7401cff9022037237ab0dac3fe100cde094e82e2bed9ba0ed1bb40154b48e56aa70f259e608b",
-                                         "3045022100c89172099507ff50f4c925e6c5150e871fb6e83dd73ff9fbb72f6ce829a9633f02203a63821d9162e99f9be712a68f9e589483994feae2661e4546cd5b6cec007be5",
-                                         "020000000001018154ecccf11a5fb56c39654c4deb4d2296f83c69268280b94d021370c94e219701000000000000000001d0070000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100d5275b3619953cb0c3b5aa577f04bc512380e60fa551762ce3d7a1bb7401cff9022037237ab0dac3fe100cde094e82e2bed9ba0ed1bb40154b48e56aa70f259e608b01483045022100c89172099507ff50f4c925e6c5150e871fb6e83dd73ff9fbb72f6ce829a9633f02203a63821d9162e99f9be712a68f9e589483994feae2661e4546cd5b6cec007be501008576a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c820120876475527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae67a914b43e1b38138a41b37f7cd9a1d274bc63e3a9b5d188ac6868f6010000");
-
-                       test_htlc_output!(2,
-                                         "304402201b63ec807771baf4fdff523c644080de17f1da478989308ad13a58b51db91d360220568939d38c9ce295adba15665fa68f51d967e8ed14a007b751540a80b325f202",
-                                         "3045022100def389deab09cee69eaa1ec14d9428770e45bcbe9feb46468ecf481371165c2f022015d2e3c46600b2ebba8dcc899768874cc6851fd1ecb3fffd15db1cc3de7e10da",
-                                         "020000000001018154ecccf11a5fb56c39654c4deb4d2296f83c69268280b94d021370c94e219702000000000000000001d0070000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e050047304402201b63ec807771baf4fdff523c644080de17f1da478989308ad13a58b51db91d360220568939d38c9ce295adba15665fa68f51d967e8ed14a007b751540a80b325f20201483045022100def389deab09cee69eaa1ec14d9428770e45bcbe9feb46468ecf481371165c2f022015d2e3c46600b2ebba8dcc899768874cc6851fd1ecb3fffd15db1cc3de7e10da012001010101010101010101010101010101010101010101010101010101010101018a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a9144b6b2e5444c2639cc0fb7bcea5afba3f3cdce23988527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f501b175ac686800000000");
-
-                       test_htlc_output!(3,
-                                         "3045022100daee1808f9861b6c3ecd14f7b707eca02dd6bdfc714ba2f33bc8cdba507bb182022026654bf8863af77d74f51f4e0b62d461a019561bb12acb120d3f7195d148a554",
-                                         "30440220643aacb19bbb72bd2b635bc3f7375481f5981bace78cdd8319b2988ffcc6704202203d27784ec8ad51ed3bd517a05525a5139bb0b755dd719e0054332d186ac08727",
-                                         "020000000001018154ecccf11a5fb56c39654c4deb4d2296f83c69268280b94d021370c94e219703000000000000000001b80b0000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100daee1808f9861b6c3ecd14f7b707eca02dd6bdfc714ba2f33bc8cdba507bb182022026654bf8863af77d74f51f4e0b62d461a019561bb12acb120d3f7195d148a554014730440220643aacb19bbb72bd2b635bc3f7375481f5981bace78cdd8319b2988ffcc6704202203d27784ec8ad51ed3bd517a05525a5139bb0b755dd719e0054332d186ac0872701008576a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c820120876475527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae67a9148a486ff2e31d6158bf39e2608864d63fefd09d5b88ac6868f7010000");
-
-                       test_htlc_output!(4,
-                                         "304402207e0410e45454b0978a623f36a10626ef17b27d9ad44e2760f98cfa3efb37924f0220220bd8acd43ecaa916a80bd4f919c495a2c58982ce7c8625153f8596692a801d",
-                                         "30440220549e80b4496803cbc4a1d09d46df50109f546d43fbbf86cd90b174b1484acd5402205f12a4f995cb9bded597eabfee195a285986aa6d93ae5bb72507ebc6a4e2349e",
-                                         "020000000001018154ecccf11a5fb56c39654c4deb4d2296f83c69268280b94d021370c94e219704000000000000000001a00f0000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e050047304402207e0410e45454b0978a623f36a10626ef17b27d9ad44e2760f98cfa3efb37924f0220220bd8acd43ecaa916a80bd4f919c495a2c58982ce7c8625153f8596692a801d014730440220549e80b4496803cbc4a1d09d46df50109f546d43fbbf86cd90b174b1484acd5402205f12a4f995cb9bded597eabfee195a285986aa6d93ae5bb72507ebc6a4e2349e012004040404040404040404040404040404040404040404040404040404040404048a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a91418bc1a114ccf9c052d3d23e28d3b0a9d1227434288527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f801b175ac686800000000");
-               }
-
-               {
-                       // commitment tx with seven outputs untrimmed (maximum feerate)
-                       chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
-                       chan.feerate_per_kw = 647;
-
-                       test_commitment!("3045022100a5c01383d3ec646d97e40f44318d49def817fcd61a0ef18008a665b3e151785502203e648efddd5838981ef55ec954be69c4a652d021e6081a100d034de366815e9b",
-                                        "304502210094bfd8f5572ac0157ec76a9551b6c5216a4538c07cd13a51af4a54cb26fa14320220768efce8ce6f4a5efac875142ff19237c011343670adf9c7ac69704a120d1163",
-                                        "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8007e80300000000000022002052bfef0479d7b293c27e0f1eb294bea154c63a3294ef092c19af51409bce0e2ad007000000000000220020403d394747cae42e98ff01734ad5c08f82ba123d3d9a620abda88989651e2ab5d007000000000000220020748eba944fedc8827f6b06bc44678f93c0f9e6078b35c6331ed31e75f8ce0c2db80b000000000000220020c20b5d1f8584fd90443e7b7b720136174fa4b9333c261d04dbbd012635c0f419a00f0000000000002200208c48d15160397c9731df9bc3b236656efb6665fbfe92b4a6878e88a499f741c4c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de843110e09c6a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e040048304502210094bfd8f5572ac0157ec76a9551b6c5216a4538c07cd13a51af4a54cb26fa14320220768efce8ce6f4a5efac875142ff19237c011343670adf9c7ac69704a120d116301483045022100a5c01383d3ec646d97e40f44318d49def817fcd61a0ef18008a665b3e151785502203e648efddd5838981ef55ec954be69c4a652d021e6081a100d034de366815e9b01475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220");
-
-                       assert_eq!(unsigned_tx.1.len(), 5);
-
-                       test_htlc_output!(0,
-                                         "30440220385a5afe75632f50128cbb029ee95c80156b5b4744beddc729ad339c9ca432c802202ba5f48550cad3379ac75b9b4fedb86a35baa6947f16ba5037fb8b11ab343740",
-                                         "304402205999590b8a79fa346e003a68fd40366397119b2b0cdf37b149968d6bc6fbcc4702202b1e1fb5ab7864931caed4e732c359e0fe3d86a548b557be2246efb1708d579a",
-                                         "020000000001018323148ce2419f21ca3d6780053747715832e18ac780931a514b187768882bb60000000000000000000122020000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e05004730440220385a5afe75632f50128cbb029ee95c80156b5b4744beddc729ad339c9ca432c802202ba5f48550cad3379ac75b9b4fedb86a35baa6947f16ba5037fb8b11ab3437400147304402205999590b8a79fa346e003a68fd40366397119b2b0cdf37b149968d6bc6fbcc4702202b1e1fb5ab7864931caed4e732c359e0fe3d86a548b557be2246efb1708d579a012000000000000000000000000000000000000000000000000000000000000000008a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a914b8bcb07f6344b42ab04250c86a6e8b75d3fdbbc688527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f401b175ac686800000000");
-
-                       test_htlc_output!(1,
-                                         "304402207ceb6678d4db33d2401fdc409959e57c16a6cb97a30261d9c61f29b8c58d34b90220084b4a17b4ca0e86f2d798b3698ca52de5621f2ce86f80bed79afa66874511b0",
-                                         "304402207ff03eb0127fc7c6cae49cc29e2a586b98d1e8969cf4a17dfa50b9c2647720b902205e2ecfda2252956c0ca32f175080e75e4e390e433feb1f8ce9f2ba55648a1dac",
-                                         "020000000001018323148ce2419f21ca3d6780053747715832e18ac780931a514b187768882bb60100000000000000000124060000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e050047304402207ceb6678d4db33d2401fdc409959e57c16a6cb97a30261d9c61f29b8c58d34b90220084b4a17b4ca0e86f2d798b3698ca52de5621f2ce86f80bed79afa66874511b00147304402207ff03eb0127fc7c6cae49cc29e2a586b98d1e8969cf4a17dfa50b9c2647720b902205e2ecfda2252956c0ca32f175080e75e4e390e433feb1f8ce9f2ba55648a1dac01008576a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c820120876475527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae67a914b43e1b38138a41b37f7cd9a1d274bc63e3a9b5d188ac6868f6010000");
-
-                       test_htlc_output!(2,
-                                         "304402206a401b29a0dff0d18ec903502c13d83e7ec019450113f4a7655a4ce40d1f65ba0220217723a084e727b6ca0cc8b6c69c014a7e4a01fcdcba3e3993f462a3c574d833",
-                                         "3045022100d50d067ca625d54e62df533a8f9291736678d0b86c28a61bb2a80cf42e702d6e02202373dde7e00218eacdafb9415fe0e1071beec1857d1af3c6a201a44cbc47c877",
-                                         "020000000001018323148ce2419f21ca3d6780053747715832e18ac780931a514b187768882bb6020000000000000000010a060000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e050047304402206a401b29a0dff0d18ec903502c13d83e7ec019450113f4a7655a4ce40d1f65ba0220217723a084e727b6ca0cc8b6c69c014a7e4a01fcdcba3e3993f462a3c574d83301483045022100d50d067ca625d54e62df533a8f9291736678d0b86c28a61bb2a80cf42e702d6e02202373dde7e00218eacdafb9415fe0e1071beec1857d1af3c6a201a44cbc47c877012001010101010101010101010101010101010101010101010101010101010101018a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a9144b6b2e5444c2639cc0fb7bcea5afba3f3cdce23988527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f501b175ac686800000000");
-
-                       test_htlc_output!(3,
-                                         "30450221009b1c987ba599ee3bde1dbca776b85481d70a78b681a8d84206723e2795c7cac002207aac84ad910f8598c4d1c0ea2e3399cf6627a4e3e90131315bc9f038451ce39d",
-                                         "3045022100db9dc65291077a52728c622987e9895b7241d4394d6dcb916d7600a3e8728c22022036ee3ee717ba0bb5c45ee84bc7bbf85c0f90f26ae4e4a25a6b4241afa8a3f1cb",
-                                         "020000000001018323148ce2419f21ca3d6780053747715832e18ac780931a514b187768882bb6030000000000000000010c0a0000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e05004830450221009b1c987ba599ee3bde1dbca776b85481d70a78b681a8d84206723e2795c7cac002207aac84ad910f8598c4d1c0ea2e3399cf6627a4e3e90131315bc9f038451ce39d01483045022100db9dc65291077a52728c622987e9895b7241d4394d6dcb916d7600a3e8728c22022036ee3ee717ba0bb5c45ee84bc7bbf85c0f90f26ae4e4a25a6b4241afa8a3f1cb01008576a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c820120876475527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae67a9148a486ff2e31d6158bf39e2608864d63fefd09d5b88ac6868f7010000");
-
-                       test_htlc_output!(4,
-                                         "3045022100cc28030b59f0914f45b84caa983b6f8effa900c952310708c2b5b00781117022022027ba2ccdf94d03c6d48b327f183f6e28c8a214d089b9227f94ac4f85315274f0",
-                                         "304402202d1a3c0d31200265d2a2def2753ead4959ae20b4083e19553acfffa5dfab60bf022020ede134149504e15b88ab261a066de49848411e15e70f9e6a5462aec2949f8f",
-                                         "020000000001018323148ce2419f21ca3d6780053747715832e18ac780931a514b187768882bb604000000000000000001da0d0000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100cc28030b59f0914f45b84caa983b6f8effa900c952310708c2b5b00781117022022027ba2ccdf94d03c6d48b327f183f6e28c8a214d089b9227f94ac4f85315274f00147304402202d1a3c0d31200265d2a2def2753ead4959ae20b4083e19553acfffa5dfab60bf022020ede134149504e15b88ab261a066de49848411e15e70f9e6a5462aec2949f8f012004040404040404040404040404040404040404040404040404040404040404048a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a91418bc1a114ccf9c052d3d23e28d3b0a9d1227434288527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f801b175ac686800000000");
-               }
-
-               {
-                       // commitment tx with six outputs untrimmed (minimum feerate)
-                       chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
-                       chan.feerate_per_kw = 648;
-
-                       test_commitment!("3044022072714e2fbb93cdd1c42eb0828b4f2eff143f717d8f26e79d6ada4f0dcb681bbe02200911be4e5161dd6ebe59ff1c58e1997c4aea804f81db6b698821db6093d7b057",
-                                        "3045022100a2270d5950c89ae0841233f6efea9c951898b301b2e89e0adbd2c687b9f32efa02207943d90f95b9610458e7c65a576e149750ff3accaacad004cd85e70b235e27de",
-                                        "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8006d007000000000000220020403d394747cae42e98ff01734ad5c08f82ba123d3d9a620abda88989651e2ab5d007000000000000220020748eba944fedc8827f6b06bc44678f93c0f9e6078b35c6331ed31e75f8ce0c2db80b000000000000220020c20b5d1f8584fd90443e7b7b720136174fa4b9333c261d04dbbd012635c0f419a00f0000000000002200208c48d15160397c9731df9bc3b236656efb6665fbfe92b4a6878e88a499f741c4c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de8431104e9d6a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0400483045022100a2270d5950c89ae0841233f6efea9c951898b301b2e89e0adbd2c687b9f32efa02207943d90f95b9610458e7c65a576e149750ff3accaacad004cd85e70b235e27de01473044022072714e2fbb93cdd1c42eb0828b4f2eff143f717d8f26e79d6ada4f0dcb681bbe02200911be4e5161dd6ebe59ff1c58e1997c4aea804f81db6b698821db6093d7b05701475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220");
-
-                       assert_eq!(unsigned_tx.1.len(), 4);
-
-                       test_htlc_output!(0,
-                                         "3044022062ef2e77591409d60d7817d9bb1e71d3c4a2931d1a6c7c8307422c84f001a251022022dad9726b0ae3fe92bda745a06f2c00f92342a186d84518588cf65f4dfaada8",
-                                         "3045022100a4c574f00411dd2f978ca5cdc1b848c311cd7849c087ad2f21a5bce5e8cc5ae90220090ae39a9bce2fb8bc879d7e9f9022df249f41e25e51f1a9bf6447a9eeffc098",
-                                         "02000000000101579c183eca9e8236a5d7f5dcd79cfec32c497fdc0ec61533cde99ecd436cadd10000000000000000000123060000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500473044022062ef2e77591409d60d7817d9bb1e71d3c4a2931d1a6c7c8307422c84f001a251022022dad9726b0ae3fe92bda745a06f2c00f92342a186d84518588cf65f4dfaada801483045022100a4c574f00411dd2f978ca5cdc1b848c311cd7849c087ad2f21a5bce5e8cc5ae90220090ae39a9bce2fb8bc879d7e9f9022df249f41e25e51f1a9bf6447a9eeffc09801008576a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c820120876475527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae67a914b43e1b38138a41b37f7cd9a1d274bc63e3a9b5d188ac6868f6010000");
-
-                       test_htlc_output!(1,
-                                         "3045022100e968cbbb5f402ed389fdc7f6cd2a80ed650bb42c79aeb2a5678444af94f6c78502204b47a1cb24ab5b0b6fe69fe9cfc7dba07b9dd0d8b95f372c1d9435146a88f8d4",
-                                         "304402207679cf19790bea76a733d2fa0672bd43ab455687a068f815a3d237581f57139a0220683a1a799e102071c206b207735ca80f627ab83d6616b4bcd017c5d79ef3e7d0",
-                                         "02000000000101579c183eca9e8236a5d7f5dcd79cfec32c497fdc0ec61533cde99ecd436cadd10100000000000000000109060000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100e968cbbb5f402ed389fdc7f6cd2a80ed650bb42c79aeb2a5678444af94f6c78502204b47a1cb24ab5b0b6fe69fe9cfc7dba07b9dd0d8b95f372c1d9435146a88f8d40147304402207679cf19790bea76a733d2fa0672bd43ab455687a068f815a3d237581f57139a0220683a1a799e102071c206b207735ca80f627ab83d6616b4bcd017c5d79ef3e7d0012001010101010101010101010101010101010101010101010101010101010101018a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a9144b6b2e5444c2639cc0fb7bcea5afba3f3cdce23988527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f501b175ac686800000000");
-
-                       test_htlc_output!(2,
-                                         "3045022100aa91932e305292cf9969cc23502bbf6cef83a5df39c95ad04a707c4f4fed5c7702207099fc0f3a9bfe1e7683c0e9aa5e76c5432eb20693bf4cb182f04d383dc9c8c2",
-                                         "304402200df76fea718745f3c529bac7fd37923e7309ce38b25c0781e4cf514dd9ef8dc802204172295739dbae9fe0474dcee3608e3433b4b2af3a2e6787108b02f894dcdda3",
-                                         "02000000000101579c183eca9e8236a5d7f5dcd79cfec32c497fdc0ec61533cde99ecd436cadd1020000000000000000010b0a0000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100aa91932e305292cf9969cc23502bbf6cef83a5df39c95ad04a707c4f4fed5c7702207099fc0f3a9bfe1e7683c0e9aa5e76c5432eb20693bf4cb182f04d383dc9c8c20147304402200df76fea718745f3c529bac7fd37923e7309ce38b25c0781e4cf514dd9ef8dc802204172295739dbae9fe0474dcee3608e3433b4b2af3a2e6787108b02f894dcdda301008576a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c820120876475527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae67a9148a486ff2e31d6158bf39e2608864d63fefd09d5b88ac6868f7010000");
-
-                       test_htlc_output!(3,
-                                         "3044022035cac88040a5bba420b1c4257235d5015309113460bc33f2853cd81ca36e632402202fc94fd3e81e9d34a9d01782a0284f3044370d03d60f3fc041e2da088d2de58f",
-                                         "304402200daf2eb7afd355b4caf6fb08387b5f031940ea29d1a9f35071288a839c9039e4022067201b562456e7948616c13acb876b386b511599b58ac1d94d127f91c50463a6",
-                                         "02000000000101579c183eca9e8236a5d7f5dcd79cfec32c497fdc0ec61533cde99ecd436cadd103000000000000000001d90d0000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500473044022035cac88040a5bba420b1c4257235d5015309113460bc33f2853cd81ca36e632402202fc94fd3e81e9d34a9d01782a0284f3044370d03d60f3fc041e2da088d2de58f0147304402200daf2eb7afd355b4caf6fb08387b5f031940ea29d1a9f35071288a839c9039e4022067201b562456e7948616c13acb876b386b511599b58ac1d94d127f91c50463a6012004040404040404040404040404040404040404040404040404040404040404048a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a91418bc1a114ccf9c052d3d23e28d3b0a9d1227434288527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f801b175ac686800000000");
-               }
-
-               {
-                       // commitment tx with six outputs untrimmed (maximum feerate)
-                       chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
-                       chan.feerate_per_kw = 2069;
-
-                       test_commitment!("3044022001d55e488b8b035b2dd29d50b65b530923a416d47f377284145bc8767b1b6a75022019bb53ddfe1cefaf156f924777eaaf8fdca1810695a7d0a247ad2afba8232eb4",
-                                        "304402203ca8f31c6a47519f83255dc69f1894d9a6d7476a19f498d31eaf0cd3a85eeb63022026fd92dc752b33905c4c838c528b692a8ad4ced959990b5d5ee2ff940fa90eea",
-                                        "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8006d007000000000000220020403d394747cae42e98ff01734ad5c08f82ba123d3d9a620abda88989651e2ab5d007000000000000220020748eba944fedc8827f6b06bc44678f93c0f9e6078b35c6331ed31e75f8ce0c2db80b000000000000220020c20b5d1f8584fd90443e7b7b720136174fa4b9333c261d04dbbd012635c0f419a00f0000000000002200208c48d15160397c9731df9bc3b236656efb6665fbfe92b4a6878e88a499f741c4c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de84311077956a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e040047304402203ca8f31c6a47519f83255dc69f1894d9a6d7476a19f498d31eaf0cd3a85eeb63022026fd92dc752b33905c4c838c528b692a8ad4ced959990b5d5ee2ff940fa90eea01473044022001d55e488b8b035b2dd29d50b65b530923a416d47f377284145bc8767b1b6a75022019bb53ddfe1cefaf156f924777eaaf8fdca1810695a7d0a247ad2afba8232eb401475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220");
-
-                       assert_eq!(unsigned_tx.1.len(), 4);
-
-                       test_htlc_output!(0,
-                                         "3045022100d1cf354de41c1369336cf85b225ed033f1f8982a01be503668df756a7e668b66022001254144fb4d0eecc61908fccc3388891ba17c5d7a1a8c62bdd307e5a513f992",
-                                         "3044022056eb1af429660e45a1b0b66568cb8c4a3aa7e4c9c292d5d6c47f86ebf2c8838f022065c3ac4ebe980ca7a41148569be4ad8751b0a724a41405697ec55035dae66402",
-                                         "02000000000101ca94a9ad516ebc0c4bdd7b6254871babfa978d5accafb554214137d398bfcf6a0000000000000000000175020000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100d1cf354de41c1369336cf85b225ed033f1f8982a01be503668df756a7e668b66022001254144fb4d0eecc61908fccc3388891ba17c5d7a1a8c62bdd307e5a513f99201473044022056eb1af429660e45a1b0b66568cb8c4a3aa7e4c9c292d5d6c47f86ebf2c8838f022065c3ac4ebe980ca7a41148569be4ad8751b0a724a41405697ec55035dae6640201008576a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c820120876475527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae67a914b43e1b38138a41b37f7cd9a1d274bc63e3a9b5d188ac6868f6010000");
-
-                       test_htlc_output!(1,
-                                         "3045022100d065569dcb94f090345402736385efeb8ea265131804beac06dd84d15dd2d6880220664feb0b4b2eb985fadb6ec7dc58c9334ea88ce599a9be760554a2d4b3b5d9f4",
-                                         "3045022100914bb232cd4b2690ee3d6cb8c3713c4ac9c4fb925323068d8b07f67c8541f8d9022057152f5f1615b793d2d45aac7518989ae4fe970f28b9b5c77504799d25433f7f",
-                                         "02000000000101ca94a9ad516ebc0c4bdd7b6254871babfa978d5accafb554214137d398bfcf6a0100000000000000000122020000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100d065569dcb94f090345402736385efeb8ea265131804beac06dd84d15dd2d6880220664feb0b4b2eb985fadb6ec7dc58c9334ea88ce599a9be760554a2d4b3b5d9f401483045022100914bb232cd4b2690ee3d6cb8c3713c4ac9c4fb925323068d8b07f67c8541f8d9022057152f5f1615b793d2d45aac7518989ae4fe970f28b9b5c77504799d25433f7f012001010101010101010101010101010101010101010101010101010101010101018a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a9144b6b2e5444c2639cc0fb7bcea5afba3f3cdce23988527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f501b175ac686800000000");
-
-                       test_htlc_output!(2,
-                                         "3045022100d4e69d363de993684eae7b37853c40722a4c1b4a7b588ad7b5d8a9b5006137a102207a069c628170ee34be5612747051bdcc087466dbaa68d5756ea81c10155aef18",
-                                         "304402200e362443f7af830b419771e8e1614fc391db3a4eb799989abfc5ab26d6fcd032022039ab0cad1c14dfbe9446bf847965e56fe016e0cbcf719fd18c1bfbf53ecbd9f9",
-                                         "02000000000101ca94a9ad516ebc0c4bdd7b6254871babfa978d5accafb554214137d398bfcf6a020000000000000000015d060000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100d4e69d363de993684eae7b37853c40722a4c1b4a7b588ad7b5d8a9b5006137a102207a069c628170ee34be5612747051bdcc087466dbaa68d5756ea81c10155aef180147304402200e362443f7af830b419771e8e1614fc391db3a4eb799989abfc5ab26d6fcd032022039ab0cad1c14dfbe9446bf847965e56fe016e0cbcf719fd18c1bfbf53ecbd9f901008576a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c820120876475527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae67a9148a486ff2e31d6158bf39e2608864d63fefd09d5b88ac6868f7010000");
-
-                       test_htlc_output!(3,
-                                         "30450221008ec888e36e4a4b3dc2ed6b823319855b2ae03006ca6ae0d9aa7e24bfc1d6f07102203b0f78885472a67ff4fe5916c0bb669487d659527509516fc3a08e87a2cc0a7c",
-                                         "304402202c3e14282b84b02705dfd00a6da396c9fe8a8bcb1d3fdb4b20a4feba09440e8b02202b058b39aa9b0c865b22095edcd9ff1f71bbfe20aa4993755e54d042755ed0d5",
-                                         "02000000000101ca94a9ad516ebc0c4bdd7b6254871babfa978d5accafb554214137d398bfcf6a03000000000000000001f2090000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e05004830450221008ec888e36e4a4b3dc2ed6b823319855b2ae03006ca6ae0d9aa7e24bfc1d6f07102203b0f78885472a67ff4fe5916c0bb669487d659527509516fc3a08e87a2cc0a7c0147304402202c3e14282b84b02705dfd00a6da396c9fe8a8bcb1d3fdb4b20a4feba09440e8b02202b058b39aa9b0c865b22095edcd9ff1f71bbfe20aa4993755e54d042755ed0d5012004040404040404040404040404040404040404040404040404040404040404048a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a91418bc1a114ccf9c052d3d23e28d3b0a9d1227434288527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f801b175ac686800000000");
-               }
-
-               {
-                       // commitment tx with five outputs untrimmed (minimum feerate)
-                       chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
-                       chan.feerate_per_kw = 2070;
-
-                       test_commitment!("3045022100f2377f7a67b7fc7f4e2c0c9e3a7de935c32417f5668eda31ea1db401b7dc53030220415fdbc8e91d0f735e70c21952342742e25249b0d062d43efbfc564499f37526",
-                                        "30440220443cb07f650aebbba14b8bc8d81e096712590f524c5991ac0ed3bbc8fd3bd0c7022028a635f548e3ca64b19b69b1ea00f05b22752f91daf0b6dab78e62ba52eb7fd0",
-                                        "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8005d007000000000000220020403d394747cae42e98ff01734ad5c08f82ba123d3d9a620abda88989651e2ab5b80b000000000000220020c20b5d1f8584fd90443e7b7b720136174fa4b9333c261d04dbbd012635c0f419a00f0000000000002200208c48d15160397c9731df9bc3b236656efb6665fbfe92b4a6878e88a499f741c4c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de843110da966a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e04004730440220443cb07f650aebbba14b8bc8d81e096712590f524c5991ac0ed3bbc8fd3bd0c7022028a635f548e3ca64b19b69b1ea00f05b22752f91daf0b6dab78e62ba52eb7fd001483045022100f2377f7a67b7fc7f4e2c0c9e3a7de935c32417f5668eda31ea1db401b7dc53030220415fdbc8e91d0f735e70c21952342742e25249b0d062d43efbfc564499f3752601475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220");
-
-                       assert_eq!(unsigned_tx.1.len(), 3);
-
-                       test_htlc_output!(0,
-                                         "3045022100eed143b1ee4bed5dc3cde40afa5db3e7354cbf9c44054b5f713f729356f08cf7022077161d171c2bbd9badf3c9934de65a4918de03bbac1450f715275f75b103f891",
-                                         "3045022100a0d043ed533e7fb1911e0553d31a8e2f3e6de19dbc035257f29d747c5e02f1f5022030cd38d8e84282175d49c1ebe0470db3ebd59768cf40780a784e248a43904fb8",
-                                         "0200000000010140a83ce364747ff277f4d7595d8d15f708418798922c40bc2b056aca5485a2180000000000000000000174020000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100eed143b1ee4bed5dc3cde40afa5db3e7354cbf9c44054b5f713f729356f08cf7022077161d171c2bbd9badf3c9934de65a4918de03bbac1450f715275f75b103f89101483045022100a0d043ed533e7fb1911e0553d31a8e2f3e6de19dbc035257f29d747c5e02f1f5022030cd38d8e84282175d49c1ebe0470db3ebd59768cf40780a784e248a43904fb801008576a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c820120876475527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae67a914b43e1b38138a41b37f7cd9a1d274bc63e3a9b5d188ac6868f6010000");
-
-                       test_htlc_output!(1,
-                                         "3044022071e9357619fd8d29a411dc053b326a5224c5d11268070e88ecb981b174747c7a02202b763ae29a9d0732fa8836dd8597439460b50472183f420021b768981b4f7cf6",
-                                         "3045022100adb1d679f65f96178b59f23ed37d3b70443118f345224a07ecb043eee2acc157022034d24524fe857144a3bcfff3065a9994d0a6ec5f11c681e49431d573e242612d",
-                                         "0200000000010140a83ce364747ff277f4d7595d8d15f708418798922c40bc2b056aca5485a218010000000000000000015c060000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500473044022071e9357619fd8d29a411dc053b326a5224c5d11268070e88ecb981b174747c7a02202b763ae29a9d0732fa8836dd8597439460b50472183f420021b768981b4f7cf601483045022100adb1d679f65f96178b59f23ed37d3b70443118f345224a07ecb043eee2acc157022034d24524fe857144a3bcfff3065a9994d0a6ec5f11c681e49431d573e242612d01008576a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c820120876475527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae67a9148a486ff2e31d6158bf39e2608864d63fefd09d5b88ac6868f7010000");
-
-                       test_htlc_output!(2,
-                                         "3045022100c9458a4d2cbb741705577deb0a890e5cb90ee141be0400d3162e533727c9cb2102206edcf765c5dc5e5f9b976ea8149bf8607b5a0efb30691138e1231302b640d2a4",
-                                         "304402200831422aa4e1ee6d55e0b894201770a8f8817a189356f2d70be76633ffa6a6f602200dd1b84a4855dc6727dd46c98daae43dfc70889d1ba7ef0087529a57c06e5e04",
-                                         "0200000000010140a83ce364747ff277f4d7595d8d15f708418798922c40bc2b056aca5485a21802000000000000000001f1090000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100c9458a4d2cbb741705577deb0a890e5cb90ee141be0400d3162e533727c9cb2102206edcf765c5dc5e5f9b976ea8149bf8607b5a0efb30691138e1231302b640d2a40147304402200831422aa4e1ee6d55e0b894201770a8f8817a189356f2d70be76633ffa6a6f602200dd1b84a4855dc6727dd46c98daae43dfc70889d1ba7ef0087529a57c06e5e04012004040404040404040404040404040404040404040404040404040404040404048a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a91418bc1a114ccf9c052d3d23e28d3b0a9d1227434288527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f801b175ac686800000000");
-               }
-
-               {
-                       // commitment tx with five outputs untrimmed (maximum feerate)
-                       chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
-                       chan.feerate_per_kw = 2194;
-
-                       test_commitment!("3045022100d33c4e541aa1d255d41ea9a3b443b3b822ad8f7f86862638aac1f69f8f760577022007e2a18e6931ce3d3a804b1c78eda1de17dbe1fb7a95488c9a4ec86203953348",
-                                        "304402203b1b010c109c2ecbe7feb2d259b9c4126bd5dc99ee693c422ec0a5781fe161ba0220571fe4e2c649dea9c7aaf7e49b382962f6a3494963c97d80fef9a430ca3f7061",
-                                        "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8005d007000000000000220020403d394747cae42e98ff01734ad5c08f82ba123d3d9a620abda88989651e2ab5b80b000000000000220020c20b5d1f8584fd90443e7b7b720136174fa4b9333c261d04dbbd012635c0f419a00f0000000000002200208c48d15160397c9731df9bc3b236656efb6665fbfe92b4a6878e88a499f741c4c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de84311040966a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e040047304402203b1b010c109c2ecbe7feb2d259b9c4126bd5dc99ee693c422ec0a5781fe161ba0220571fe4e2c649dea9c7aaf7e49b382962f6a3494963c97d80fef9a430ca3f706101483045022100d33c4e541aa1d255d41ea9a3b443b3b822ad8f7f86862638aac1f69f8f760577022007e2a18e6931ce3d3a804b1c78eda1de17dbe1fb7a95488c9a4ec8620395334801475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220");
-
-                       assert_eq!(unsigned_tx.1.len(), 3);
-
-                       test_htlc_output!(0,
-                                         "30450221009ed2f0a67f99e29c3c8cf45c08207b765980697781bb727fe0b1416de0e7622902206052684229bc171419ed290f4b615c943f819c0262414e43c5b91dcf72ddcf44",
-                                         "3044022004ad5f04ae69c71b3b141d4db9d0d4c38d84009fb3cfeeae6efdad414487a9a0022042d3fe1388c1ff517d1da7fb4025663d372c14728ed52dc88608363450ff6a2f",
-                                         "02000000000101fb824d4e4dafc0f567789dee3a6bce8d411fe80f5563d8cdfdcc7d7e4447d43a0000000000000000000122020000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e05004830450221009ed2f0a67f99e29c3c8cf45c08207b765980697781bb727fe0b1416de0e7622902206052684229bc171419ed290f4b615c943f819c0262414e43c5b91dcf72ddcf4401473044022004ad5f04ae69c71b3b141d4db9d0d4c38d84009fb3cfeeae6efdad414487a9a0022042d3fe1388c1ff517d1da7fb4025663d372c14728ed52dc88608363450ff6a2f01008576a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c820120876475527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae67a914b43e1b38138a41b37f7cd9a1d274bc63e3a9b5d188ac6868f6010000");
-
-                       test_htlc_output!(1,
-                                         "30440220155d3b90c67c33a8321996a9be5b82431b0c126613be751d400669da9d5c696702204318448bcd48824439d2c6a70be6e5747446be47ff45977cf41672bdc9b6b12d",
-                                         "304402201707050c870c1f77cc3ed58d6d71bf281de239e9eabd8ef0955bad0d7fe38dcc02204d36d80d0019b3a71e646a08fa4a5607761d341ae8be371946ebe437c289c915",
-                                         "02000000000101fb824d4e4dafc0f567789dee3a6bce8d411fe80f5563d8cdfdcc7d7e4447d43a010000000000000000010a060000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e05004730440220155d3b90c67c33a8321996a9be5b82431b0c126613be751d400669da9d5c696702204318448bcd48824439d2c6a70be6e5747446be47ff45977cf41672bdc9b6b12d0147304402201707050c870c1f77cc3ed58d6d71bf281de239e9eabd8ef0955bad0d7fe38dcc02204d36d80d0019b3a71e646a08fa4a5607761d341ae8be371946ebe437c289c91501008576a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c820120876475527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae67a9148a486ff2e31d6158bf39e2608864d63fefd09d5b88ac6868f7010000");
-
-                       test_htlc_output!(2,
-                                         "3045022100a12a9a473ece548584aabdd051779025a5ed4077c4b7aa376ec7a0b1645e5a48022039490b333f53b5b3e2ddde1d809e492cba2b3e5fc3a436cd3ffb4cd3d500fa5a",
-                                         "3045022100ff200bc934ab26ce9a559e998ceb0aee53bc40368e114ab9d3054d9960546e2802202496856ca163ac12c143110b6b3ac9d598df7254f2e17b3b94c3ab5301f4c3b0",
-                                         "02000000000101fb824d4e4dafc0f567789dee3a6bce8d411fe80f5563d8cdfdcc7d7e4447d43a020000000000000000019a090000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100a12a9a473ece548584aabdd051779025a5ed4077c4b7aa376ec7a0b1645e5a48022039490b333f53b5b3e2ddde1d809e492cba2b3e5fc3a436cd3ffb4cd3d500fa5a01483045022100ff200bc934ab26ce9a559e998ceb0aee53bc40368e114ab9d3054d9960546e2802202496856ca163ac12c143110b6b3ac9d598df7254f2e17b3b94c3ab5301f4c3b0012004040404040404040404040404040404040404040404040404040404040404048a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a91418bc1a114ccf9c052d3d23e28d3b0a9d1227434288527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f801b175ac686800000000");
-               }
-
-               {
-                       // commitment tx with four outputs untrimmed (minimum feerate)
-                       chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
-                       chan.feerate_per_kw = 2195;
-
-                       test_commitment!("304402205e2f76d4657fb732c0dfc820a18a7301e368f5799e06b7828007633741bda6df0220458009ae59d0c6246065c419359e05eb2a4b4ef4a1b310cc912db44eb7924298",
-                                        "304402203b12d44254244b8ff3bb4129b0920fd45120ab42f553d9976394b099d500c99e02205e95bb7a3164852ef0c48f9e0eaf145218f8e2c41251b231f03cbdc4f29a5429",
-                                        "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8004b80b000000000000220020c20b5d1f8584fd90443e7b7b720136174fa4b9333c261d04dbbd012635c0f419a00f0000000000002200208c48d15160397c9731df9bc3b236656efb6665fbfe92b4a6878e88a499f741c4c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de843110b8976a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e040047304402203b12d44254244b8ff3bb4129b0920fd45120ab42f553d9976394b099d500c99e02205e95bb7a3164852ef0c48f9e0eaf145218f8e2c41251b231f03cbdc4f29a54290147304402205e2f76d4657fb732c0dfc820a18a7301e368f5799e06b7828007633741bda6df0220458009ae59d0c6246065c419359e05eb2a4b4ef4a1b310cc912db44eb792429801475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220");
-
-                       assert_eq!(unsigned_tx.1.len(), 2);
-
-                       test_htlc_output!(0,
-                                         "3045022100a8a78fa1016a5c5c3704f2e8908715a3cef66723fb95f3132ec4d2d05cd84fb4022025ac49287b0861ec21932405f5600cbce94313dbde0e6c5d5af1b3366d8afbfc",
-                                         "3045022100be6ae1977fd7b630a53623f3f25c542317ccfc2b971782802a4f1ef538eb22b402207edc4d0408f8f38fd3c7365d1cfc26511b7cd2d4fecd8b005fba3cd5bc704390",
-                                         "020000000001014e16c488fa158431c1a82e8f661240ec0a71ba0ce92f2721a6538c510226ad5c0000000000000000000109060000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100a8a78fa1016a5c5c3704f2e8908715a3cef66723fb95f3132ec4d2d05cd84fb4022025ac49287b0861ec21932405f5600cbce94313dbde0e6c5d5af1b3366d8afbfc01483045022100be6ae1977fd7b630a53623f3f25c542317ccfc2b971782802a4f1ef538eb22b402207edc4d0408f8f38fd3c7365d1cfc26511b7cd2d4fecd8b005fba3cd5bc70439001008576a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c820120876475527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae67a9148a486ff2e31d6158bf39e2608864d63fefd09d5b88ac6868f7010000");
-
-                       test_htlc_output!(1,
-                                         "3045022100e769cb156aa2f7515d126cef7a69968629620ce82afcaa9e210969de6850df4602200b16b3f3486a229a48aadde520dbee31ae340dbadaffae74fbb56681fef27b92",
-                                         "30440220665b9cb4a978c09d1ca8977a534999bc8a49da624d0c5439451dd69cde1a003d022070eae0620f01f3c1bd029cc1488da13fb40fdab76f396ccd335479a11c5276d8",
-                                         "020000000001014e16c488fa158431c1a82e8f661240ec0a71ba0ce92f2721a6538c510226ad5c0100000000000000000199090000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100e769cb156aa2f7515d126cef7a69968629620ce82afcaa9e210969de6850df4602200b16b3f3486a229a48aadde520dbee31ae340dbadaffae74fbb56681fef27b92014730440220665b9cb4a978c09d1ca8977a534999bc8a49da624d0c5439451dd69cde1a003d022070eae0620f01f3c1bd029cc1488da13fb40fdab76f396ccd335479a11c5276d8012004040404040404040404040404040404040404040404040404040404040404048a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a91418bc1a114ccf9c052d3d23e28d3b0a9d1227434288527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f801b175ac686800000000");
-               }
-
-               {
-                       // commitment tx with four outputs untrimmed (maximum feerate)
-                       chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
-                       chan.feerate_per_kw = 3702;
-
-                       test_commitment!("3045022100c1a3b0b60ca092ed5080121f26a74a20cec6bdee3f8e47bae973fcdceb3eda5502207d467a9873c939bf3aa758014ae67295fedbca52412633f7e5b2670fc7c381c1",
-                                        "304402200e930a43c7951162dc15a2b7344f48091c74c70f7024e7116e900d8bcfba861c022066fa6cbda3929e21daa2e7e16a4b948db7e8919ef978402360d1095ffdaff7b0",
-                                        "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8004b80b000000000000220020c20b5d1f8584fd90443e7b7b720136174fa4b9333c261d04dbbd012635c0f419a00f0000000000002200208c48d15160397c9731df9bc3b236656efb6665fbfe92b4a6878e88a499f741c4c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de8431106f916a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e040047304402200e930a43c7951162dc15a2b7344f48091c74c70f7024e7116e900d8bcfba861c022066fa6cbda3929e21daa2e7e16a4b948db7e8919ef978402360d1095ffdaff7b001483045022100c1a3b0b60ca092ed5080121f26a74a20cec6bdee3f8e47bae973fcdceb3eda5502207d467a9873c939bf3aa758014ae67295fedbca52412633f7e5b2670fc7c381c101475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220");
-
-                       assert_eq!(unsigned_tx.1.len(), 2);
-
-                       test_htlc_output!(0,
-                                         "3045022100dfb73b4fe961b31a859b2bb1f4f15cabab9265016dd0272323dc6a9e85885c54022059a7b87c02861ee70662907f25ce11597d7b68d3399443a831ae40e777b76bdb",
-                                         "304402202765b9c9ece4f127fa5407faf66da4c5ce2719cdbe47cd3175fc7d48b482e43d02205605125925e07bad1e41c618a4b434d72c88a164981c4b8af5eaf4ee9142ec3a",
-                                         "02000000000101b8de11eb51c22498fe39722c7227b6e55ff1a94146cf638458cb9bc6a060d3a30000000000000000000122020000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100dfb73b4fe961b31a859b2bb1f4f15cabab9265016dd0272323dc6a9e85885c54022059a7b87c02861ee70662907f25ce11597d7b68d3399443a831ae40e777b76bdb0147304402202765b9c9ece4f127fa5407faf66da4c5ce2719cdbe47cd3175fc7d48b482e43d02205605125925e07bad1e41c618a4b434d72c88a164981c4b8af5eaf4ee9142ec3a01008576a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c820120876475527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae67a9148a486ff2e31d6158bf39e2608864d63fefd09d5b88ac6868f7010000");
-
-                       test_htlc_output!(1,
-                                         "3045022100ea9dc2a7c3c3640334dab733bb4e036e32a3106dc707b24227874fa4f7da746802204d672f7ac0fe765931a8df10b81e53a3242dd32bd9dc9331eb4a596da87954e9",
-                                         "30440220048a41c660c4841693de037d00a407810389f4574b3286afb7bc392a438fa3f802200401d71fa87c64fe621b49ac07e3bf85157ac680acb977124da28652cc7f1a5c",
-                                         "02000000000101b8de11eb51c22498fe39722c7227b6e55ff1a94146cf638458cb9bc6a060d3a30100000000000000000176050000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100ea9dc2a7c3c3640334dab733bb4e036e32a3106dc707b24227874fa4f7da746802204d672f7ac0fe765931a8df10b81e53a3242dd32bd9dc9331eb4a596da87954e9014730440220048a41c660c4841693de037d00a407810389f4574b3286afb7bc392a438fa3f802200401d71fa87c64fe621b49ac07e3bf85157ac680acb977124da28652cc7f1a5c012004040404040404040404040404040404040404040404040404040404040404048a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a91418bc1a114ccf9c052d3d23e28d3b0a9d1227434288527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f801b175ac686800000000");
-               }
-
-               {
-                       // commitment tx with three outputs untrimmed (minimum feerate)
-                       chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
-                       chan.feerate_per_kw = 3703;
-
-                       test_commitment!("30450221008b7c191dd46893b67b628e618d2dc8e81169d38bade310181ab77d7c94c6675e02203b4dd131fd7c9deb299560983dcdc485545c98f989f7ae8180c28289f9e6bdb0",
-                                        "3044022047305531dd44391dce03ae20f8735005c615eb077a974edb0059ea1a311857d602202e0ed6972fbdd1e8cb542b06e0929bc41b2ddf236e04cb75edd56151f4197506",
-                                        "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8003a00f0000000000002200208c48d15160397c9731df9bc3b236656efb6665fbfe92b4a6878e88a499f741c4c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de843110eb936a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0400473044022047305531dd44391dce03ae20f8735005c615eb077a974edb0059ea1a311857d602202e0ed6972fbdd1e8cb542b06e0929bc41b2ddf236e04cb75edd56151f4197506014830450221008b7c191dd46893b67b628e618d2dc8e81169d38bade310181ab77d7c94c6675e02203b4dd131fd7c9deb299560983dcdc485545c98f989f7ae8180c28289f9e6bdb001475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220");
-
-                       assert_eq!(unsigned_tx.1.len(), 1);
-
-                       test_htlc_output!(0,
-                                         "3044022044f65cf833afdcb9d18795ca93f7230005777662539815b8a601eeb3e57129a902206a4bf3e53392affbba52640627defa8dc8af61c958c9e827b2798ab45828abdd",
-                                         "3045022100b94d931a811b32eeb885c28ddcf999ae1981893b21dd1329929543fe87ce793002206370107fdd151c5f2384f9ceb71b3107c69c74c8ed5a28a94a4ab2d27d3b0724",
-                                         "020000000001011c076aa7fb3d7460d10df69432c904227ea84bbf3134d4ceee5fb0f135ef206d0000000000000000000175050000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500473044022044f65cf833afdcb9d18795ca93f7230005777662539815b8a601eeb3e57129a902206a4bf3e53392affbba52640627defa8dc8af61c958c9e827b2798ab45828abdd01483045022100b94d931a811b32eeb885c28ddcf999ae1981893b21dd1329929543fe87ce793002206370107fdd151c5f2384f9ceb71b3107c69c74c8ed5a28a94a4ab2d27d3b0724012004040404040404040404040404040404040404040404040404040404040404048a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a91418bc1a114ccf9c052d3d23e28d3b0a9d1227434288527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f801b175ac686800000000");
-               }
-
-               {
-                       // commitment tx with three outputs untrimmed (maximum feerate)
-                       chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
-                       chan.feerate_per_kw = 4914;
-
-                       test_commitment!("304402206d6cb93969d39177a09d5d45b583f34966195b77c7e585cf47ac5cce0c90cefb022031d71ae4e33a4e80df7f981d696fbdee517337806a3c7138b7491e2cbb077a0e",
-                                        "304402206a2679efa3c7aaffd2a447fd0df7aba8792858b589750f6a1203f9259173198a022008d52a0e77a99ab533c36206cb15ad7aeb2aa72b93d4b571e728cb5ec2f6fe26",
-                                        "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8003a00f0000000000002200208c48d15160397c9731df9bc3b236656efb6665fbfe92b4a6878e88a499f741c4c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de843110ae8f6a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e040047304402206a2679efa3c7aaffd2a447fd0df7aba8792858b589750f6a1203f9259173198a022008d52a0e77a99ab533c36206cb15ad7aeb2aa72b93d4b571e728cb5ec2f6fe260147304402206d6cb93969d39177a09d5d45b583f34966195b77c7e585cf47ac5cce0c90cefb022031d71ae4e33a4e80df7f981d696fbdee517337806a3c7138b7491e2cbb077a0e01475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220");
-
-                       assert_eq!(unsigned_tx.1.len(), 1);
-
-                       test_htlc_output!(0,
-                                         "3045022100fcb38506bfa11c02874092a843d0cc0a8613c23b639832564a5f69020cb0f6ba02206508b9e91eaa001425c190c68ee5f887e1ad5b1b314002e74db9dbd9e42dbecf",
-                                         "304502210086e76b460ddd3cea10525fba298405d3fe11383e56966a5091811368362f689a02200f72ee75657915e0ede89c28709acd113ede9e1b7be520e3bc5cda425ecd6e68",
-                                         "0200000000010110a3fdcbcd5db477cd3ad465e7f501ffa8c437e8301f00a6061138590add757f0000000000000000000122020000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100fcb38506bfa11c02874092a843d0cc0a8613c23b639832564a5f69020cb0f6ba02206508b9e91eaa001425c190c68ee5f887e1ad5b1b314002e74db9dbd9e42dbecf0148304502210086e76b460ddd3cea10525fba298405d3fe11383e56966a5091811368362f689a02200f72ee75657915e0ede89c28709acd113ede9e1b7be520e3bc5cda425ecd6e68012004040404040404040404040404040404040404040404040404040404040404048a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a91418bc1a114ccf9c052d3d23e28d3b0a9d1227434288527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f801b175ac686800000000");
-               }
-
-               {
-                       // commitment tx with two outputs untrimmed (minimum feerate)
-                       chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
-                       chan.feerate_per_kw = 4915;
-
-                       test_commitment!("304402200769ba89c7330dfa4feba447b6e322305f12ac7dac70ec6ba997ed7c1b598d0802204fe8d337e7fee781f9b7b1a06e580b22f4f79d740059560191d7db53f8765552",
-                                        "3045022100a012691ba6cea2f73fa8bac37750477e66363c6d28813b0bb6da77c8eb3fb0270220365e99c51304b0b1a6ab9ea1c8500db186693e39ec1ad5743ee231b0138384b9",
-                                        "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8002c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de843110fa926a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0400483045022100a012691ba6cea2f73fa8bac37750477e66363c6d28813b0bb6da77c8eb3fb0270220365e99c51304b0b1a6ab9ea1c8500db186693e39ec1ad5743ee231b0138384b90147304402200769ba89c7330dfa4feba447b6e322305f12ac7dac70ec6ba997ed7c1b598d0802204fe8d337e7fee781f9b7b1a06e580b22f4f79d740059560191d7db53f876555201475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220");
-
-                       assert_eq!(unsigned_tx.1.len(), 0);
-               }
-
-               {
-                       // commitment tx with two outputs untrimmed (maximum feerate)
-                       chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
-                       chan.feerate_per_kw = 9651180;
-
-                       test_commitment!("3044022037f83ff00c8e5fb18ae1f918ffc24e54581775a20ff1ae719297ef066c71caa9022039c529cccd89ff6c5ed1db799614533844bd6d101da503761c45c713996e3bbd",
-                                        "30440220514f977bf7edc442de8ce43ace9686e5ebdc0f893033f13e40fb46c8b8c6e1f90220188006227d175f5c35da0b092c57bea82537aed89f7778204dc5bacf4f29f2b9",
-                                        "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b800222020000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80ec0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de84311004004730440220514f977bf7edc442de8ce43ace9686e5ebdc0f893033f13e40fb46c8b8c6e1f90220188006227d175f5c35da0b092c57bea82537aed89f7778204dc5bacf4f29f2b901473044022037f83ff00c8e5fb18ae1f918ffc24e54581775a20ff1ae719297ef066c71caa9022039c529cccd89ff6c5ed1db799614533844bd6d101da503761c45c713996e3bbd01475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220");
-
-                       assert_eq!(unsigned_tx.1.len(), 0);
-               }
-
-               {
-                       // commitment tx with one output untrimmed (minimum feerate)
-                       chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
-                       chan.feerate_per_kw = 9651181;
-
-                       test_commitment!("3044022064901950be922e62cbe3f2ab93de2b99f37cff9fc473e73e394b27f88ef0731d02206d1dfa227527b4df44a07599289e207d6fd9cca60c0365682dcd3deaf739567e",
-                                        "3044022031a82b51bd014915fe68928d1abf4b9885353fb896cac10c3fdd88d7f9c7f2e00220716bda819641d2c63e65d3549b6120112e1aeaf1742eed94a471488e79e206b1",
-                                        "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8001c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de8431100400473044022031a82b51bd014915fe68928d1abf4b9885353fb896cac10c3fdd88d7f9c7f2e00220716bda819641d2c63e65d3549b6120112e1aeaf1742eed94a471488e79e206b101473044022064901950be922e62cbe3f2ab93de2b99f37cff9fc473e73e394b27f88ef0731d02206d1dfa227527b4df44a07599289e207d6fd9cca60c0365682dcd3deaf739567e01475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220");
-
-                       assert_eq!(unsigned_tx.1.len(), 0);
-               }
-
-               {
-                       // commitment tx with fee greater than funder amount
-                       chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
-                       chan.feerate_per_kw = 9651936;
-
-                       test_commitment!("3044022064901950be922e62cbe3f2ab93de2b99f37cff9fc473e73e394b27f88ef0731d02206d1dfa227527b4df44a07599289e207d6fd9cca60c0365682dcd3deaf739567e",
-                                        "3044022031a82b51bd014915fe68928d1abf4b9885353fb896cac10c3fdd88d7f9c7f2e00220716bda819641d2c63e65d3549b6120112e1aeaf1742eed94a471488e79e206b1",
-                                        "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8001c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de8431100400473044022031a82b51bd014915fe68928d1abf4b9885353fb896cac10c3fdd88d7f9c7f2e00220716bda819641d2c63e65d3549b6120112e1aeaf1742eed94a471488e79e206b101473044022064901950be922e62cbe3f2ab93de2b99f37cff9fc473e73e394b27f88ef0731d02206d1dfa227527b4df44a07599289e207d6fd9cca60c0365682dcd3deaf739567e01475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220");
-
-                       assert_eq!(unsigned_tx.1.len(), 0);
-               }
-       }
-
-       #[test]
-       fn test_per_commitment_secret_gen() {
-               // Test vectors from BOLT 3 Appendix D:
-
-               let mut seed = [0; 32];
-               seed[0..32].clone_from_slice(&hex::decode("0000000000000000000000000000000000000000000000000000000000000000").unwrap());
-               assert_eq!(chan_utils::build_commitment_secret(seed, 281474976710655),
-                          hex::decode("02a40c85b6f28da08dfdbe0926c53fab2de6d28c10301f8f7c4073d5e42e3148").unwrap()[..]);
-
-               seed[0..32].clone_from_slice(&hex::decode("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF").unwrap());
-               assert_eq!(chan_utils::build_commitment_secret(seed, 281474976710655),
-                          hex::decode("7cc854b54e3e0dcdb010d7a3fee464a9687be6e8db3be6854c475621e007a5dc").unwrap()[..]);
-
-               assert_eq!(chan_utils::build_commitment_secret(seed, 0xaaaaaaaaaaa),
-                          hex::decode("56f4008fb007ca9acf0e15b054d5c9fd12ee06cea347914ddbaed70d1c13a528").unwrap()[..]);
-
-               assert_eq!(chan_utils::build_commitment_secret(seed, 0x555555555555),
-                          hex::decode("9015daaeb06dba4ccc05b91b2f73bd54405f2be9f217fbacd3c5ac2e62327d31").unwrap()[..]);
-
-               seed[0..32].clone_from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap());
-               assert_eq!(chan_utils::build_commitment_secret(seed, 1),
-                          hex::decode("915c75942a26bb3a433a8ce2cb0427c29ec6c1775cfc78328b57f6ba7bfeaa9c").unwrap()[..]);
-       }
-
-       #[test]
-       fn test_key_derivation() {
-               // Test vectors from BOLT 3 Appendix E:
-               let secp_ctx = Secp256k1::new();
-
-               let base_secret = SecretKey::from_slice(&hex::decode("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f").unwrap()[..]).unwrap();
-               let per_commitment_secret = SecretKey::from_slice(&hex::decode("1f1e1d1c1b1a191817161514131211100f0e0d0c0b0a09080706050403020100").unwrap()[..]).unwrap();
-
-               let base_point = PublicKey::from_secret_key(&secp_ctx, &base_secret);
-               assert_eq!(base_point.serialize()[..], hex::decode("036d6caac248af96f6afa7f904f550253a0f3ef3f5aa2fe6838a95b216691468e2").unwrap()[..]);
-
-               let per_commitment_point = PublicKey::from_secret_key(&secp_ctx, &per_commitment_secret);
-               assert_eq!(per_commitment_point.serialize()[..], hex::decode("025f7117a78150fe2ef97db7cfc83bd57b2e2c0d0dd25eaf467a4a1c2a45ce1486").unwrap()[..]);
-
-               assert_eq!(chan_utils::derive_public_key(&secp_ctx, &per_commitment_point, &base_point).unwrap().serialize()[..],
-                               hex::decode("0235f2dbfaa89b57ec7b055afe29849ef7ddfeb1cefdb9ebdc43f5494984db29e5").unwrap()[..]);
-
-               assert_eq!(chan_utils::derive_private_key(&secp_ctx, &per_commitment_point, &base_secret).unwrap(),
-                               SecretKey::from_slice(&hex::decode("cbced912d3b21bf196a766651e436aff192362621ce317704ea2f75d87e7be0f").unwrap()[..]).unwrap());
-
-               assert_eq!(chan_utils::derive_public_revocation_key(&secp_ctx, &per_commitment_point, &base_point).unwrap().serialize()[..],
-                               hex::decode("02916e326636d19c33f13e8c0c3a03dd157f332f3e99c317c141dd865eb01f8ff0").unwrap()[..]);
-
-               assert_eq!(chan_utils::derive_private_revocation_key(&secp_ctx, &per_commitment_secret, &base_secret).unwrap(),
-                               SecretKey::from_slice(&hex::decode("d09ffff62ddb2297ab000cc85bcb4283fdeb6aa052affbc9dddcf33b61078110").unwrap()[..]).unwrap());
-       }
-}
diff --git a/src/ln/channelmanager.rs b/src/ln/channelmanager.rs
deleted file mode 100644 (file)
index a957a96..0000000
+++ /dev/null
@@ -1,3228 +0,0 @@
-//! The top-level channel management and payment tracking stuff lives here.
-//!
-//! The ChannelManager is the main chunk of logic implementing the lightning protocol and is
-//! responsible for tracking which channels are open, HTLCs are in flight and reestablishing those
-//! upon reconnect to the relevant peer(s).
-//!
-//! It does not manage routing logic (see ln::router for that) nor does it manage constructing
-//! on-chain transactions (it only monitors the chain to watch for any force-closes that might
-//! imply it needs to fail HTLCs/payments/channels it manages).
-
-use bitcoin::blockdata::block::BlockHeader;
-use bitcoin::blockdata::transaction::Transaction;
-use bitcoin::blockdata::constants::genesis_block;
-use bitcoin::network::constants::Network;
-use bitcoin::util::hash::BitcoinHash;
-
-use bitcoin_hashes::{Hash, HashEngine};
-use bitcoin_hashes::hmac::{Hmac, HmacEngine};
-use bitcoin_hashes::sha256::Hash as Sha256;
-use bitcoin_hashes::sha256d::Hash as Sha256dHash;
-use bitcoin_hashes::cmp::fixed_time_eq;
-
-use secp256k1::key::{SecretKey,PublicKey};
-use secp256k1::Secp256k1;
-use secp256k1::ecdh::SharedSecret;
-use secp256k1;
-
-use chain::chaininterface::{BroadcasterInterface,ChainListener,ChainWatchInterface,FeeEstimator};
-use chain::transaction::OutPoint;
-use ln::channel::{Channel, ChannelError};
-use ln::channelmonitor::{ChannelMonitor, ChannelMonitorUpdateErr, ManyChannelMonitor, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ANTI_REORG_DELAY};
-use ln::router::Route;
-use ln::msgs;
-use ln::msgs::LocalFeatures;
-use ln::onion_utils;
-use ln::msgs::{ChannelMessageHandler, DecodeError, HandleError};
-use chain::keysinterface::KeysInterface;
-use util::config::UserConfig;
-use util::{byte_utils, events};
-use util::ser::{Readable, ReadableArgs, Writeable, Writer};
-use util::chacha20::ChaCha20;
-use util::logger::Logger;
-use util::errors::APIError;
-
-use std::{cmp, mem};
-use std::collections::{HashMap, hash_map, HashSet};
-use std::io::Cursor;
-use std::sync::{Arc, Mutex, MutexGuard, RwLock};
-use std::sync::atomic::{AtomicUsize, Ordering};
-use std::time::Duration;
-
-// We hold various information about HTLC relay in the HTLC objects in Channel itself:
-//
-// Upon receipt of an HTLC from a peer, we'll give it a PendingHTLCStatus indicating if it should
-// forward the HTLC with information it will give back to us when it does so, or if it should Fail
-// the HTLC with the relevant message for the Channel to handle giving to the remote peer.
-//
-// When a Channel forwards an HTLC to its peer, it will give us back the PendingForwardHTLCInfo
-// which we will use to construct an outbound HTLC, with a relevant HTLCSource::PreviousHopData
-// filled in to indicate where it came from (which we can use to either fail-backwards or fulfill
-// the HTLC backwards along the relevant path).
-// Alternatively, we can fill an outbound HTLC with a HTLCSource::OutboundRoute indicating this is
-// our payment, which we can use to decode errors or inform the user that the payment was sent.
-/// Stores the info we will need to send when we want to forward an HTLC onwards
-#[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
-pub(super) struct PendingForwardHTLCInfo {
-       onion_packet: Option<msgs::OnionPacket>,
-       incoming_shared_secret: [u8; 32],
-       payment_hash: PaymentHash,
-       short_channel_id: u64,
-       pub(super) amt_to_forward: u64,
-       pub(super) outgoing_cltv_value: u32,
-}
-
-#[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
-pub(super) enum HTLCFailureMsg {
-       Relay(msgs::UpdateFailHTLC),
-       Malformed(msgs::UpdateFailMalformedHTLC),
-}
-
-/// Stores whether we can't forward an HTLC or relevant forwarding info
-#[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
-pub(super) enum PendingHTLCStatus {
-       Forward(PendingForwardHTLCInfo),
-       Fail(HTLCFailureMsg),
-}
-
-/// Tracks the inbound corresponding to an outbound HTLC
-#[derive(Clone, PartialEq)]
-pub(super) struct HTLCPreviousHopData {
-       short_channel_id: u64,
-       htlc_id: u64,
-       incoming_packet_shared_secret: [u8; 32],
-}
-
-/// Tracks the inbound corresponding to an outbound HTLC
-#[derive(Clone, PartialEq)]
-pub(super) enum HTLCSource {
-       PreviousHopData(HTLCPreviousHopData),
-       OutboundRoute {
-               route: Route,
-               session_priv: SecretKey,
-               /// Technically we can recalculate this from the route, but we cache it here to avoid
-               /// doing a double-pass on route when we get a failure back
-               first_hop_htlc_msat: u64,
-       },
-}
-#[cfg(test)]
-impl HTLCSource {
-       pub fn dummy() -> Self {
-               HTLCSource::OutboundRoute {
-                       route: Route { hops: Vec::new() },
-                       session_priv: SecretKey::from_slice(&[1; 32]).unwrap(),
-                       first_hop_htlc_msat: 0,
-               }
-       }
-}
-
-#[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
-pub(super) enum HTLCFailReason {
-       ErrorPacket {
-               err: msgs::OnionErrorPacket,
-       },
-       Reason {
-               failure_code: u16,
-               data: Vec<u8>,
-       }
-}
-
-/// payment_hash type, use to cross-lock hop
-#[derive(Hash, Copy, Clone, PartialEq, Eq, Debug)]
-pub struct PaymentHash(pub [u8;32]);
-/// payment_preimage type, use to route payment between hop
-#[derive(Hash, Copy, Clone, PartialEq, Eq, Debug)]
-pub struct PaymentPreimage(pub [u8;32]);
-
-type ShutdownResult = (Vec<Transaction>, Vec<(HTLCSource, PaymentHash)>);
-
-/// Error type returned across the channel_state mutex boundary. When an Err is generated for a
-/// Channel, we generally end up with a ChannelError::Close for which we have to close the channel
-/// immediately (ie with no further calls on it made). Thus, this step happens inside a
-/// channel_state lock. We then return the set of things that need to be done outside the lock in
-/// this struct and call handle_error!() on it.
-
-struct MsgHandleErrInternal {
-       err: msgs::HandleError,
-       shutdown_finish: Option<(ShutdownResult, Option<msgs::ChannelUpdate>)>,
-}
-impl MsgHandleErrInternal {
-       #[inline]
-       fn send_err_msg_no_close(err: &'static str, channel_id: [u8; 32]) -> Self {
-               Self {
-                       err: HandleError {
-                               err,
-                               action: Some(msgs::ErrorAction::SendErrorMessage {
-                                       msg: msgs::ErrorMessage {
-                                               channel_id,
-                                               data: err.to_string()
-                                       },
-                               }),
-                       },
-                       shutdown_finish: None,
-               }
-       }
-       #[inline]
-       fn ignore_no_close(err: &'static str) -> Self {
-               Self {
-                       err: HandleError {
-                               err,
-                               action: Some(msgs::ErrorAction::IgnoreError),
-                       },
-                       shutdown_finish: None,
-               }
-       }
-       #[inline]
-       fn from_no_close(err: msgs::HandleError) -> Self {
-               Self { err, shutdown_finish: None }
-       }
-       #[inline]
-       fn from_finish_shutdown(err: &'static str, channel_id: [u8; 32], shutdown_res: ShutdownResult, channel_update: Option<msgs::ChannelUpdate>) -> Self {
-               Self {
-                       err: HandleError {
-                               err,
-                               action: Some(msgs::ErrorAction::SendErrorMessage {
-                                       msg: msgs::ErrorMessage {
-                                               channel_id,
-                                               data: err.to_string()
-                                       },
-                               }),
-                       },
-                       shutdown_finish: Some((shutdown_res, channel_update)),
-               }
-       }
-       #[inline]
-       fn from_chan_no_close(err: ChannelError, channel_id: [u8; 32]) -> Self {
-               Self {
-                       err: match err {
-                               ChannelError::Ignore(msg) => HandleError {
-                                       err: msg,
-                                       action: Some(msgs::ErrorAction::IgnoreError),
-                               },
-                               ChannelError::Close(msg) => HandleError {
-                                       err: msg,
-                                       action: Some(msgs::ErrorAction::SendErrorMessage {
-                                               msg: msgs::ErrorMessage {
-                                                       channel_id,
-                                                       data: msg.to_string()
-                                               },
-                                       }),
-                               },
-                               ChannelError::CloseDelayBroadcast { msg, .. } => HandleError {
-                                       err: msg,
-                                       action: Some(msgs::ErrorAction::SendErrorMessage {
-                                               msg: msgs::ErrorMessage {
-                                                       channel_id,
-                                                       data: msg.to_string()
-                                               },
-                                       }),
-                               },
-                       },
-                       shutdown_finish: None,
-               }
-       }
-}
-
-/// We hold back HTLCs we intend to relay for a random interval greater than this (see
-/// Event::PendingHTLCsForwardable for the API guidelines indicating how long should be waited).
-/// This provides some limited amount of privacy. Ideally this would range from somewhere like one
-/// second to 30 seconds, but people expect lightning to be, you know, kinda fast, sadly.
-const MIN_HTLC_RELAY_HOLDING_CELL_MILLIS: u64 = 100;
-
-pub(super) enum HTLCForwardInfo {
-       AddHTLC {
-               prev_short_channel_id: u64,
-               prev_htlc_id: u64,
-               forward_info: PendingForwardHTLCInfo,
-       },
-       FailHTLC {
-               htlc_id: u64,
-               err_packet: msgs::OnionErrorPacket,
-       },
-}
-
-/// For events which result in both a RevokeAndACK and a CommitmentUpdate, by default they should
-/// be sent in the order they appear in the return value, however sometimes the order needs to be
-/// variable at runtime (eg Channel::channel_reestablish needs to re-send messages in the order
-/// they were originally sent). In those cases, this enum is also returned.
-#[derive(Clone, PartialEq)]
-pub(super) enum RAACommitmentOrder {
-       /// Send the CommitmentUpdate messages first
-       CommitmentFirst,
-       /// Send the RevokeAndACK message first
-       RevokeAndACKFirst,
-}
-
-// Note this is only exposed in cfg(test):
-pub(super) struct ChannelHolder {
-       pub(super) by_id: HashMap<[u8; 32], Channel>,
-       pub(super) short_to_id: HashMap<u64, [u8; 32]>,
-       /// short channel id -> forward infos. Key of 0 means payments received
-       /// Note that while this is held in the same mutex as the channels themselves, no consistency
-       /// guarantees are made about the existence of a channel with the short id here, nor the short
-       /// ids in the PendingForwardHTLCInfo!
-       pub(super) forward_htlcs: HashMap<u64, Vec<HTLCForwardInfo>>,
-       /// payment_hash -> Vec<(amount_received, htlc_source)> for tracking things that were to us and
-       /// can be failed/claimed by the user
-       /// Note that while this is held in the same mutex as the channels themselves, no consistency
-       /// guarantees are made about the channels given here actually existing anymore by the time you
-       /// go to read them!
-       pub(super) claimable_htlcs: HashMap<PaymentHash, Vec<(u64, HTLCPreviousHopData)>>,
-       /// Messages to send to peers - pushed to in the same lock that they are generated in (except
-       /// for broadcast messages, where ordering isn't as strict).
-       pub(super) pending_msg_events: Vec<events::MessageSendEvent>,
-}
-pub(super) struct MutChannelHolder<'a> {
-       pub(super) by_id: &'a mut HashMap<[u8; 32], Channel>,
-       pub(super) short_to_id: &'a mut HashMap<u64, [u8; 32]>,
-       pub(super) forward_htlcs: &'a mut HashMap<u64, Vec<HTLCForwardInfo>>,
-       pub(super) claimable_htlcs: &'a mut HashMap<PaymentHash, Vec<(u64, HTLCPreviousHopData)>>,
-       pub(super) pending_msg_events: &'a mut Vec<events::MessageSendEvent>,
-}
-impl ChannelHolder {
-       pub(super) fn borrow_parts(&mut self) -> MutChannelHolder {
-               MutChannelHolder {
-                       by_id: &mut self.by_id,
-                       short_to_id: &mut self.short_to_id,
-                       forward_htlcs: &mut self.forward_htlcs,
-                       claimable_htlcs: &mut self.claimable_htlcs,
-                       pending_msg_events: &mut self.pending_msg_events,
-               }
-       }
-}
-
-#[cfg(not(any(target_pointer_width = "32", target_pointer_width = "64")))]
-const ERR: () = "You need at least 32 bit pointers (well, usize, but we'll assume they're the same) for ChannelManager::latest_block_height";
-
-/// Manager which keeps track of a number of channels and sends messages to the appropriate
-/// channel, also tracking HTLC preimages and forwarding onion packets appropriately.
-///
-/// Implements ChannelMessageHandler, handling the multi-channel parts and passing things through
-/// to individual Channels.
-///
-/// Implements Writeable to write out all channel state to disk. Implies peer_disconnected() for
-/// all peers during write/read (though does not modify this instance, only the instance being
-/// serialized). This will result in any channels which have not yet exchanged funding_created (ie
-/// called funding_transaction_generated for outbound channels).
-///
-/// Note that you can be a bit lazier about writing out ChannelManager than you can be with
-/// ChannelMonitors. With ChannelMonitors you MUST write each monitor update out to disk before
-/// returning from ManyChannelMonitor::add_update_monitor, with ChannelManagers, writing updates
-/// happens out-of-band (and will prevent any other ChannelManager operations from occurring during
-/// the serialization process). If the deserialized version is out-of-date compared to the
-/// ChannelMonitors passed by reference to read(), those channels will be force-closed based on the
-/// ChannelMonitor state and no funds will be lost (mod on-chain transaction fees).
-///
-/// Note that the deserializer is only implemented for (Sha256dHash, ChannelManager), which
-/// tells you the last block hash which was block_connect()ed. You MUST rescan any blocks along
-/// the "reorg path" (ie call block_disconnected() until you get to a common block and then call
-/// block_connected() to step towards your best block) upon deserialization before using the
-/// object!
-pub struct ChannelManager {
-       default_configuration: UserConfig,
-       genesis_hash: Sha256dHash,
-       fee_estimator: Arc<FeeEstimator>,
-       monitor: Arc<ManyChannelMonitor>,
-       chain_monitor: Arc<ChainWatchInterface>,
-       tx_broadcaster: Arc<BroadcasterInterface>,
-
-       #[cfg(test)]
-       pub(super) latest_block_height: AtomicUsize,
-       #[cfg(not(test))]
-       latest_block_height: AtomicUsize,
-       last_block_hash: Mutex<Sha256dHash>,
-       secp_ctx: Secp256k1<secp256k1::All>,
-
-       #[cfg(test)]
-       pub(super) channel_state: Mutex<ChannelHolder>,
-       #[cfg(not(test))]
-       channel_state: Mutex<ChannelHolder>,
-       our_network_key: SecretKey,
-
-       pending_events: Mutex<Vec<events::Event>>,
-       /// Used when we have to take a BIG lock to make sure everything is self-consistent.
-       /// Essentially just when we're serializing ourselves out.
-       /// Taken first everywhere where we are making changes before any other locks.
-       total_consistency_lock: RwLock<()>,
-
-       keys_manager: Arc<KeysInterface>,
-
-       logger: Arc<Logger>,
-}
-
-/// The amount of time we require our counterparty wait to claim their money (ie time between when
-/// we, or our watchtower, must check for them having broadcast a theft transaction).
-pub(crate) const BREAKDOWN_TIMEOUT: u16 = 6 * 24;
-/// The amount of time we're willing to wait to claim money back to us
-pub(crate) const MAX_LOCAL_BREAKDOWN_TIMEOUT: u16 = 6 * 24 * 7;
-
-/// The minimum number of blocks between an inbound HTLC's CLTV and the corresponding outbound
-/// HTLC's CLTV. This should always be a few blocks greater than channelmonitor::CLTV_CLAIM_BUFFER,
-/// ie the node we forwarded the payment on to should always have enough room to reliably time out
-/// the HTLC via a full update_fail_htlc/commitment_signed dance before we hit the
-/// CLTV_CLAIM_BUFFER point (we static assert that it's at least 3 blocks more).
-const CLTV_EXPIRY_DELTA: u16 = 6 * 12; //TODO?
-pub(super) const CLTV_FAR_FAR_AWAY: u32 = 6 * 24 * 7; //TODO?
-
-// Check that our CLTV_EXPIRY is at least CLTV_CLAIM_BUFFER + ANTI_REORG_DELAY + LATENCY_GRACE_PERIOD_BLOCKS,
-// ie that if the next-hop peer fails the HTLC within
-// LATENCY_GRACE_PERIOD_BLOCKS then we'll still have CLTV_CLAIM_BUFFER left to timeout it onchain,
-// then waiting ANTI_REORG_DELAY to be reorg-safe on the outbound HLTC and
-// failing the corresponding htlc backward, and us now seeing the last block of ANTI_REORG_DELAY before
-// LATENCY_GRACE_PERIOD_BLOCKS.
-#[deny(const_err)]
-#[allow(dead_code)]
-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;
-
-// Check for ability of an attacker to make us fail on-chain by delaying inbound claim. See
-// ChannelMontior::would_broadcast_at_height for a description of why this is needed.
-#[deny(const_err)]
-#[allow(dead_code)]
-const CHECK_CLTV_EXPIRY_SANITY_2: u32 = CLTV_EXPIRY_DELTA as u32 - LATENCY_GRACE_PERIOD_BLOCKS - 2*CLTV_CLAIM_BUFFER;
-
-macro_rules! secp_call {
-       ( $res: expr, $err: expr ) => {
-               match $res {
-                       Ok(key) => key,
-                       Err(_) => return Err($err),
-               }
-       };
-}
-
-/// Details of a channel, as returned by ChannelManager::list_channels and ChannelManager::list_usable_channels
-pub struct ChannelDetails {
-       /// The channel's ID (prior to funding transaction generation, this is a random 32 bytes,
-       /// thereafter this is the txid of the funding transaction xor the funding transaction output).
-       /// Note that this means this value is *not* persistent - it can change once during the
-       /// lifetime of the channel.
-       pub channel_id: [u8; 32],
-       /// The position of the funding transaction in the chain. None if the funding transaction has
-       /// not yet been confirmed and the channel fully opened.
-       pub short_channel_id: Option<u64>,
-       /// The node_id of our counterparty
-       pub remote_network_id: PublicKey,
-       /// The value, in satoshis, of this channel as appears in the funding output
-       pub channel_value_satoshis: u64,
-       /// The user_id passed in to create_channel, or 0 if the channel was inbound.
-       pub user_id: u64,
-       /// The available outbound capacity for sending HTLCs to the remote peer. This does not include
-       /// any pending HTLCs which are not yet fully resolved (and, thus, who's balance is not
-       /// available for inclusion in new outbound HTLCs). This further does not include any pending
-       /// outgoing HTLCs which are awaiting some other resolution to be sent.
-       pub outbound_capacity_msat: u64,
-       /// The available inbound capacity for the remote peer to send HTLCs to us. This does not
-       /// include any pending HTLCs which are not yet fully resolved (and, thus, who's balance is not
-       /// available for inclusion in new inbound HTLCs).
-       /// Note that there are some corner cases not fully handled here, so the actual available
-       /// inbound capacity may be slightly higher than this.
-       pub inbound_capacity_msat: u64,
-       /// True if the channel is (a) confirmed and funding_locked messages have been exchanged, (b)
-       /// the peer is connected, and (c) no monitor update failure is pending resolution.
-       pub is_live: bool,
-}
-
-macro_rules! handle_error {
-       ($self: ident, $internal: expr) => {
-               match $internal {
-                       Ok(msg) => Ok(msg),
-                       Err(MsgHandleErrInternal { err, shutdown_finish }) => {
-                               if let Some((shutdown_res, update_option)) = shutdown_finish {
-                                       $self.finish_force_close_channel(shutdown_res);
-                                       if let Some(update) = update_option {
-                                               let mut channel_state = $self.channel_state.lock().unwrap();
-                                               channel_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
-                                                       msg: update
-                                               });
-                                       }
-                               }
-                               Err(err)
-                       },
-               }
-       }
-}
-
-macro_rules! break_chan_entry {
-       ($self: ident, $res: expr, $channel_state: expr, $entry: expr) => {
-               match $res {
-                       Ok(res) => res,
-                       Err(ChannelError::Ignore(msg)) => {
-                               break Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), $entry.key().clone()))
-                       },
-                       Err(ChannelError::Close(msg)) => {
-                               log_trace!($self, "Closing channel {} due to Close-required error: {}", log_bytes!($entry.key()[..]), msg);
-                               let (channel_id, mut chan) = $entry.remove_entry();
-                               if let Some(short_id) = chan.get_short_channel_id() {
-                                       $channel_state.short_to_id.remove(&short_id);
-                               }
-                               break Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, chan.force_shutdown(), $self.get_channel_update(&chan).ok()))
-                       },
-                       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"); }
-               }
-       }
-}
-
-macro_rules! try_chan_entry {
-       ($self: ident, $res: expr, $channel_state: expr, $entry: expr) => {
-               match $res {
-                       Ok(res) => res,
-                       Err(ChannelError::Ignore(msg)) => {
-                               return Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), $entry.key().clone()))
-                       },
-                       Err(ChannelError::Close(msg)) => {
-                               log_trace!($self, "Closing channel {} due to Close-required error: {}", log_bytes!($entry.key()[..]), msg);
-                               let (channel_id, mut chan) = $entry.remove_entry();
-                               if let Some(short_id) = chan.get_short_channel_id() {
-                                       $channel_state.short_to_id.remove(&short_id);
-                               }
-                               return Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, chan.force_shutdown(), $self.get_channel_update(&chan).ok()))
-                       },
-                       Err(ChannelError::CloseDelayBroadcast { msg, update }) => {
-                               log_error!($self, "Channel {} need to be shutdown but closing transactions not broadcast due to {}", log_bytes!($entry.key()[..]), msg);
-                               let (channel_id, mut chan) = $entry.remove_entry();
-                               if let Some(short_id) = chan.get_short_channel_id() {
-                                       $channel_state.short_to_id.remove(&short_id);
-                               }
-                               if let Some(update) = update {
-                                       if let Err(e) = $self.monitor.add_update_monitor(update.get_funding_txo().unwrap(), update) {
-                                               match e {
-                                                       // Upstream channel is dead, but we want at least to fail backward HTLCs to save
-                                                       // downstream channels. In case of PermanentFailure, we are not going to be able
-                                                       // to claim back to_remote output on remote commitment transaction. Doesn't
-                                                       // make a difference here, we are concern about HTLCs circuit, not onchain funds.
-                                                       ChannelMonitorUpdateErr::PermanentFailure => {},
-                                                       ChannelMonitorUpdateErr::TemporaryFailure => {},
-                                               }
-                                       }
-                               }
-                               let mut shutdown_res = chan.force_shutdown();
-                               if shutdown_res.0.len() >= 1 {
-                                       log_error!($self, "You have a toxic local commitment transaction {} avaible in channel monitor, read comment in ChannelMonitor::get_latest_local_commitment_txn to be informed of manual action to take", shutdown_res.0[0].txid());
-                               }
-                               shutdown_res.0.clear();
-                               return Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, shutdown_res, $self.get_channel_update(&chan).ok()))
-                       }
-               }
-       }
-}
-
-macro_rules! handle_monitor_err {
-       ($self: ident, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr) => {
-               handle_monitor_err!($self, $err, $channel_state, $entry, $action_type, $resend_raa, $resend_commitment, Vec::new(), Vec::new())
-       };
-       ($self: ident, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr, $failed_forwards: expr, $failed_fails: expr) => {
-               match $err {
-                       ChannelMonitorUpdateErr::PermanentFailure => {
-                               log_error!($self, "Closing channel {} due to monitor update PermanentFailure", log_bytes!($entry.key()[..]));
-                               let (channel_id, mut chan) = $entry.remove_entry();
-                               if let Some(short_id) = chan.get_short_channel_id() {
-                                       $channel_state.short_to_id.remove(&short_id);
-                               }
-                               // TODO: $failed_fails is dropped here, which will cause other channels to hit the
-                               // chain in a confused state! We need to move them into the ChannelMonitor which
-                               // will be responsible for failing backwards once things confirm on-chain.
-                               // It's ok that we drop $failed_forwards here - at this point we'd rather they
-                               // broadcast HTLC-Timeout and pay the associated fees to get their funds back than
-                               // us bother trying to claim it just to forward on to another peer. If we're
-                               // splitting hairs we'd prefer to claim payments that were to us, but we haven't
-                               // given up the preimage yet, so might as well just wait until the payment is
-                               // retried, avoiding the on-chain fees.
-                               let res: Result<(), _> = Err(MsgHandleErrInternal::from_finish_shutdown("ChannelMonitor storage failure", channel_id, chan.force_shutdown(), $self.get_channel_update(&chan).ok()));
-                               res
-                       },
-                       ChannelMonitorUpdateErr::TemporaryFailure => {
-                               log_info!($self, "Disabling channel {} due to monitor update TemporaryFailure. On restore will send {} and process {} forwards and {} fails",
-                                               log_bytes!($entry.key()[..]),
-                                               if $resend_commitment && $resend_raa {
-                                                               match $action_type {
-                                                                       RAACommitmentOrder::CommitmentFirst => { "commitment then RAA" },
-                                                                       RAACommitmentOrder::RevokeAndACKFirst => { "RAA then commitment" },
-                                                               }
-                                                       } else if $resend_commitment { "commitment" }
-                                                       else if $resend_raa { "RAA" }
-                                                       else { "nothing" },
-                                               (&$failed_forwards as &Vec<(PendingForwardHTLCInfo, u64)>).len(),
-                                               (&$failed_fails as &Vec<(HTLCSource, PaymentHash, HTLCFailReason)>).len());
-                               if !$resend_commitment {
-                                       debug_assert!($action_type == RAACommitmentOrder::RevokeAndACKFirst || !$resend_raa);
-                               }
-                               if !$resend_raa {
-                                       debug_assert!($action_type == RAACommitmentOrder::CommitmentFirst || !$resend_commitment);
-                               }
-                               $entry.get_mut().monitor_update_failed($resend_raa, $resend_commitment, $failed_forwards, $failed_fails);
-                               Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore("Failed to update ChannelMonitor"), *$entry.key()))
-                       },
-               }
-       }
-}
-
-macro_rules! return_monitor_err {
-       ($self: ident, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr) => {
-               return handle_monitor_err!($self, $err, $channel_state, $entry, $action_type, $resend_raa, $resend_commitment);
-       };
-       ($self: ident, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr, $failed_forwards: expr, $failed_fails: expr) => {
-               return handle_monitor_err!($self, $err, $channel_state, $entry, $action_type, $resend_raa, $resend_commitment, $failed_forwards, $failed_fails);
-       }
-}
-
-// Does not break in case of TemporaryFailure!
-macro_rules! maybe_break_monitor_err {
-       ($self: ident, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr) => {
-               match (handle_monitor_err!($self, $err, $channel_state, $entry, $action_type, $resend_raa, $resend_commitment), $err) {
-                       (e, ChannelMonitorUpdateErr::PermanentFailure) => {
-                               break e;
-                       },
-                       (_, ChannelMonitorUpdateErr::TemporaryFailure) => { },
-               }
-       }
-}
-
-impl ChannelManager {
-       /// Constructs a new ChannelManager to hold several channels and route between them.
-       ///
-       /// This is the main "logic hub" for all channel-related actions, and implements
-       /// ChannelMessageHandler.
-       ///
-       /// Non-proportional fees are fixed according to our risk using the provided fee estimator.
-       ///
-       /// panics if channel_value_satoshis is >= `MAX_FUNDING_SATOSHIS`!
-       pub fn new(network: Network, feeest: Arc<FeeEstimator>, monitor: Arc<ManyChannelMonitor>, chain_monitor: Arc<ChainWatchInterface>, tx_broadcaster: Arc<BroadcasterInterface>, logger: Arc<Logger>,keys_manager: Arc<KeysInterface>, config: UserConfig) -> Result<Arc<ChannelManager>, secp256k1::Error> {
-               let secp_ctx = Secp256k1::new();
-
-               let res = Arc::new(ChannelManager {
-                       default_configuration: config.clone(),
-                       genesis_hash: genesis_block(network).header.bitcoin_hash(),
-                       fee_estimator: feeest.clone(),
-                       monitor: monitor.clone(),
-                       chain_monitor,
-                       tx_broadcaster,
-
-                       latest_block_height: AtomicUsize::new(0), //TODO: Get an init value
-                       last_block_hash: Mutex::new(Default::default()),
-                       secp_ctx,
-
-                       channel_state: Mutex::new(ChannelHolder{
-                               by_id: HashMap::new(),
-                               short_to_id: HashMap::new(),
-                               forward_htlcs: HashMap::new(),
-                               claimable_htlcs: HashMap::new(),
-                               pending_msg_events: Vec::new(),
-                       }),
-                       our_network_key: keys_manager.get_node_secret(),
-
-                       pending_events: Mutex::new(Vec::new()),
-                       total_consistency_lock: RwLock::new(()),
-
-                       keys_manager,
-
-                       logger,
-               });
-               let weak_res = Arc::downgrade(&res);
-               res.chain_monitor.register_listener(weak_res);
-               Ok(res)
-       }
-
-       /// Creates a new outbound channel to the given remote node and with the given value.
-       ///
-       /// user_id will be provided back as user_channel_id in FundingGenerationReady and
-       /// FundingBroadcastSafe events to allow tracking of which events correspond with which
-       /// create_channel call. Note that user_channel_id defaults to 0 for inbound channels, so you
-       /// may wish to avoid using 0 for user_id here.
-       ///
-       /// If successful, will generate a SendOpenChannel message event, so you should probably poll
-       /// PeerManager::process_events afterwards.
-       ///
-       /// Raises APIError::APIMisuseError when channel_value_satoshis > 2**24 or push_msat is
-       /// greater than channel_value_satoshis * 1k or channel_value_satoshis is < 1000.
-       pub fn create_channel(&self, their_network_key: PublicKey, channel_value_satoshis: u64, push_msat: u64, user_id: u64) -> Result<(), APIError> {
-               if channel_value_satoshis < 1000 {
-                       return Err(APIError::APIMisuseError { err: "channel_value must be at least 1000 satoshis" });
-               }
-
-               let channel = Channel::new_outbound(&*self.fee_estimator, &self.keys_manager, their_network_key, channel_value_satoshis, push_msat, user_id, Arc::clone(&self.logger), &self.default_configuration)?;
-               let res = channel.get_open_channel(self.genesis_hash.clone(), &*self.fee_estimator);
-
-               let _ = self.total_consistency_lock.read().unwrap();
-               let mut channel_state = self.channel_state.lock().unwrap();
-               match channel_state.by_id.entry(channel.channel_id()) {
-                       hash_map::Entry::Occupied(_) => {
-                               if cfg!(feature = "fuzztarget") {
-                                       return Err(APIError::APIMisuseError { err: "Fuzzy bad RNG" });
-                               } else {
-                                       panic!("RNG is bad???");
-                               }
-                       },
-                       hash_map::Entry::Vacant(entry) => { entry.insert(channel); }
-               }
-               channel_state.pending_msg_events.push(events::MessageSendEvent::SendOpenChannel {
-                       node_id: their_network_key,
-                       msg: res,
-               });
-               Ok(())
-       }
-
-       /// Gets the list of open channels, in random order. See ChannelDetail field documentation for
-       /// more information.
-       pub fn list_channels(&self) -> Vec<ChannelDetails> {
-               let channel_state = self.channel_state.lock().unwrap();
-               let mut res = Vec::with_capacity(channel_state.by_id.len());
-               for (channel_id, channel) in channel_state.by_id.iter() {
-                       let (inbound_capacity_msat, outbound_capacity_msat) = channel.get_inbound_outbound_available_balance_msat();
-                       res.push(ChannelDetails {
-                               channel_id: (*channel_id).clone(),
-                               short_channel_id: channel.get_short_channel_id(),
-                               remote_network_id: channel.get_their_node_id(),
-                               channel_value_satoshis: channel.get_value_satoshis(),
-                               inbound_capacity_msat,
-                               outbound_capacity_msat,
-                               user_id: channel.get_user_id(),
-                               is_live: channel.is_live(),
-                       });
-               }
-               res
-       }
-
-       /// Gets the list of usable channels, in random order. Useful as an argument to
-       /// Router::get_route to ensure non-announced channels are used.
-       ///
-       /// These are guaranteed to have their is_live value set to true, see the documentation for
-       /// ChannelDetails::is_live for more info on exactly what the criteria are.
-       pub fn list_usable_channels(&self) -> Vec<ChannelDetails> {
-               let channel_state = self.channel_state.lock().unwrap();
-               let mut res = Vec::with_capacity(channel_state.by_id.len());
-               for (channel_id, channel) in channel_state.by_id.iter() {
-                       // Note we use is_live here instead of usable which leads to somewhat confused
-                       // internal/external nomenclature, but that's ok cause that's probably what the user
-                       // really wanted anyway.
-                       if channel.is_live() {
-                               let (inbound_capacity_msat, outbound_capacity_msat) = channel.get_inbound_outbound_available_balance_msat();
-                               res.push(ChannelDetails {
-                                       channel_id: (*channel_id).clone(),
-                                       short_channel_id: channel.get_short_channel_id(),
-                                       remote_network_id: channel.get_their_node_id(),
-                                       channel_value_satoshis: channel.get_value_satoshis(),
-                                       inbound_capacity_msat,
-                                       outbound_capacity_msat,
-                                       user_id: channel.get_user_id(),
-                                       is_live: true,
-                               });
-                       }
-               }
-               res
-       }
-
-       /// Begins the process of closing a channel. After this call (plus some timeout), no new HTLCs
-       /// will be accepted on the given channel, and after additional timeout/the closing of all
-       /// pending HTLCs, the channel will be closed on chain.
-       ///
-       /// May generate a SendShutdown message event on success, which should be relayed.
-       pub fn close_channel(&self, channel_id: &[u8; 32]) -> Result<(), APIError> {
-               let _ = self.total_consistency_lock.read().unwrap();
-
-               let (mut failed_htlcs, chan_option) = {
-                       let mut channel_state_lock = self.channel_state.lock().unwrap();
-                       let channel_state = channel_state_lock.borrow_parts();
-                       match channel_state.by_id.entry(channel_id.clone()) {
-                               hash_map::Entry::Occupied(mut chan_entry) => {
-                                       let (shutdown_msg, failed_htlcs) = chan_entry.get_mut().get_shutdown()?;
-                                       channel_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
-                                               node_id: chan_entry.get().get_their_node_id(),
-                                               msg: shutdown_msg
-                                       });
-                                       if chan_entry.get().is_shutdown() {
-                                               if let Some(short_id) = chan_entry.get().get_short_channel_id() {
-                                                       channel_state.short_to_id.remove(&short_id);
-                                               }
-                                               (failed_htlcs, Some(chan_entry.remove_entry().1))
-                                       } else { (failed_htlcs, None) }
-                               },
-                               hash_map::Entry::Vacant(_) => return Err(APIError::ChannelUnavailable{err: "No such channel"})
-                       }
-               };
-               for htlc_source in failed_htlcs.drain(..) {
-                       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() });
-               }
-               let chan_update = if let Some(chan) = chan_option {
-                       if let Ok(update) = self.get_channel_update(&chan) {
-                               Some(update)
-                       } else { None }
-               } else { None };
-
-               if let Some(update) = chan_update {
-                       let mut channel_state = self.channel_state.lock().unwrap();
-                       channel_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
-                               msg: update
-                       });
-               }
-
-               Ok(())
-       }
-
-       #[inline]
-       fn finish_force_close_channel(&self, shutdown_res: ShutdownResult) {
-               let (local_txn, mut failed_htlcs) = shutdown_res;
-               log_trace!(self, "Finishing force-closure of channel with {} transactions to broadcast and {} HTLCs to fail", local_txn.len(), failed_htlcs.len());
-               for htlc_source in failed_htlcs.drain(..) {
-                       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() });
-               }
-               for tx in local_txn {
-                       self.tx_broadcaster.broadcast_transaction(&tx);
-               }
-       }
-
-       /// Force closes a channel, immediately broadcasting the latest local commitment transaction to
-       /// the chain and rejecting new HTLCs on the given channel.
-       pub fn force_close_channel(&self, channel_id: &[u8; 32]) {
-               let _ = self.total_consistency_lock.read().unwrap();
-
-               let mut chan = {
-                       let mut channel_state_lock = self.channel_state.lock().unwrap();
-                       let channel_state = channel_state_lock.borrow_parts();
-                       if let Some(chan) = channel_state.by_id.remove(channel_id) {
-                               if let Some(short_id) = chan.get_short_channel_id() {
-                                       channel_state.short_to_id.remove(&short_id);
-                               }
-                               chan
-                       } else {
-                               return;
-                       }
-               };
-               log_trace!(self, "Force-closing channel {}", log_bytes!(channel_id[..]));
-               self.finish_force_close_channel(chan.force_shutdown());
-               if let Ok(update) = self.get_channel_update(&chan) {
-                       let mut channel_state = self.channel_state.lock().unwrap();
-                       channel_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
-                               msg: update
-                       });
-               }
-       }
-
-       /// Force close all channels, immediately broadcasting the latest local commitment transaction
-       /// for each to the chain and rejecting new HTLCs on each.
-       pub fn force_close_all_channels(&self) {
-               for chan in self.list_channels() {
-                       self.force_close_channel(&chan.channel_id);
-               }
-       }
-
-       const ZERO:[u8; 65] = [0; 65];
-       fn decode_update_add_htlc_onion(&self, msg: &msgs::UpdateAddHTLC) -> (PendingHTLCStatus, MutexGuard<ChannelHolder>) {
-               macro_rules! return_malformed_err {
-                       ($msg: expr, $err_code: expr) => {
-                               {
-                                       log_info!(self, "Failed to accept/forward incoming HTLC: {}", $msg);
-                                       return (PendingHTLCStatus::Fail(HTLCFailureMsg::Malformed(msgs::UpdateFailMalformedHTLC {
-                                               channel_id: msg.channel_id,
-                                               htlc_id: msg.htlc_id,
-                                               sha256_of_onion: Sha256::hash(&msg.onion_routing_packet.hop_data).into_inner(),
-                                               failure_code: $err_code,
-                                       })), self.channel_state.lock().unwrap());
-                               }
-                       }
-               }
-
-               if let Err(_) = msg.onion_routing_packet.public_key {
-                       return_malformed_err!("invalid ephemeral pubkey", 0x8000 | 0x4000 | 6);
-               }
-
-               let shared_secret = {
-                       let mut arr = [0; 32];
-                       arr.copy_from_slice(&SharedSecret::new(&msg.onion_routing_packet.public_key.unwrap(), &self.our_network_key)[..]);
-                       arr
-               };
-               let (rho, mu) = onion_utils::gen_rho_mu_from_shared_secret(&shared_secret);
-
-               if msg.onion_routing_packet.version != 0 {
-                       //TODO: Spec doesn't indicate if we should only hash hop_data here (and in other
-                       //sha256_of_onion error data packets), or the entire onion_routing_packet. Either way,
-                       //the hash doesn't really serve any purpose - in the case of hashing all data, the
-                       //receiving node would have to brute force to figure out which version was put in the
-                       //packet by the node that send us the message, in the case of hashing the hop_data, the
-                       //node knows the HMAC matched, so they already know what is there...
-                       return_malformed_err!("Unknown onion packet version", 0x8000 | 0x4000 | 4);
-               }
-
-               let mut hmac = HmacEngine::<Sha256>::new(&mu);
-               hmac.input(&msg.onion_routing_packet.hop_data);
-               hmac.input(&msg.payment_hash.0[..]);
-               if !fixed_time_eq(&Hmac::from_engine(hmac).into_inner(), &msg.onion_routing_packet.hmac) {
-                       return_malformed_err!("HMAC Check failed", 0x8000 | 0x4000 | 5);
-               }
-
-               let mut channel_state = None;
-               macro_rules! return_err {
-                       ($msg: expr, $err_code: expr, $data: expr) => {
-                               {
-                                       log_info!(self, "Failed to accept/forward incoming HTLC: {}", $msg);
-                                       if channel_state.is_none() {
-                                               channel_state = Some(self.channel_state.lock().unwrap());
-                                       }
-                                       return (PendingHTLCStatus::Fail(HTLCFailureMsg::Relay(msgs::UpdateFailHTLC {
-                                               channel_id: msg.channel_id,
-                                               htlc_id: msg.htlc_id,
-                                               reason: onion_utils::build_first_hop_failure_packet(&shared_secret, $err_code, $data),
-                                       })), channel_state.unwrap());
-                               }
-                       }
-               }
-
-               let mut chacha = ChaCha20::new(&rho, &[0u8; 8]);
-               let next_hop_data = {
-                       let mut decoded = [0; 65];
-                       chacha.process(&msg.onion_routing_packet.hop_data[0..65], &mut decoded);
-                       match msgs::OnionHopData::read(&mut Cursor::new(&decoded[..])) {
-                               Err(err) => {
-                                       let error_code = match err {
-                                               msgs::DecodeError::UnknownVersion => 0x4000 | 1, // unknown realm byte
-                                               _ => 0x2000 | 2, // Should never happen
-                                       };
-                                       return_err!("Unable to decode our hop data", error_code, &[0;0]);
-                               },
-                               Ok(msg) => msg
-                       }
-               };
-
-               let pending_forward_info = if next_hop_data.hmac == [0; 32] {
-                               // OUR PAYMENT!
-                               // final_expiry_too_soon
-                               if (msg.cltv_expiry as u64) < self.latest_block_height.load(Ordering::Acquire) as u64 + (CLTV_CLAIM_BUFFER + LATENCY_GRACE_PERIOD_BLOCKS) as u64 {
-                                       return_err!("The final CLTV expiry is too soon to handle", 17, &[0;0]);
-                               }
-                               // final_incorrect_htlc_amount
-                               if next_hop_data.data.amt_to_forward > msg.amount_msat {
-                                       return_err!("Upstream node sent less than we were supposed to receive in payment", 19, &byte_utils::be64_to_array(msg.amount_msat));
-                               }
-                               // final_incorrect_cltv_expiry
-                               if next_hop_data.data.outgoing_cltv_value != msg.cltv_expiry {
-                                       return_err!("Upstream node set CLTV to the wrong value", 18, &byte_utils::be32_to_array(msg.cltv_expiry));
-                               }
-
-                               // Note that we could obviously respond immediately with an update_fulfill_htlc
-                               // message, however that would leak that we are the recipient of this payment, so
-                               // instead we stay symmetric with the forwarding case, only responding (after a
-                               // delay) once they've send us a commitment_signed!
-
-                               PendingHTLCStatus::Forward(PendingForwardHTLCInfo {
-                                       onion_packet: None,
-                                       payment_hash: msg.payment_hash.clone(),
-                                       short_channel_id: 0,
-                                       incoming_shared_secret: shared_secret,
-                                       amt_to_forward: next_hop_data.data.amt_to_forward,
-                                       outgoing_cltv_value: next_hop_data.data.outgoing_cltv_value,
-                               })
-                       } else {
-                               let mut new_packet_data = [0; 20*65];
-                               chacha.process(&msg.onion_routing_packet.hop_data[65..], &mut new_packet_data[0..19*65]);
-                               chacha.process(&ChannelManager::ZERO[..], &mut new_packet_data[19*65..]);
-
-                               let mut new_pubkey = msg.onion_routing_packet.public_key.unwrap();
-
-                               let blinding_factor = {
-                                       let mut sha = Sha256::engine();
-                                       sha.input(&new_pubkey.serialize()[..]);
-                                       sha.input(&shared_secret);
-                                       Sha256::from_engine(sha).into_inner()
-                               };
-
-                               let public_key = if let Err(e) = new_pubkey.mul_assign(&self.secp_ctx, &blinding_factor[..]) {
-                                       Err(e)
-                               } else { Ok(new_pubkey) };
-
-                               let outgoing_packet = msgs::OnionPacket {
-                                       version: 0,
-                                       public_key,
-                                       hop_data: new_packet_data,
-                                       hmac: next_hop_data.hmac.clone(),
-                               };
-
-                               PendingHTLCStatus::Forward(PendingForwardHTLCInfo {
-                                       onion_packet: Some(outgoing_packet),
-                                       payment_hash: msg.payment_hash.clone(),
-                                       short_channel_id: next_hop_data.data.short_channel_id,
-                                       incoming_shared_secret: shared_secret,
-                                       amt_to_forward: next_hop_data.data.amt_to_forward,
-                                       outgoing_cltv_value: next_hop_data.data.outgoing_cltv_value,
-                               })
-                       };
-
-               channel_state = Some(self.channel_state.lock().unwrap());
-               if let &PendingHTLCStatus::Forward(PendingForwardHTLCInfo { ref onion_packet, ref short_channel_id, ref amt_to_forward, ref outgoing_cltv_value, .. }) = &pending_forward_info {
-                       if onion_packet.is_some() { // If short_channel_id is 0 here, we'll reject them in the body here
-                               let id_option = channel_state.as_ref().unwrap().short_to_id.get(&short_channel_id).cloned();
-                               let forwarding_id = match id_option {
-                                       None => { // unknown_next_peer
-                                               return_err!("Don't have available channel for forwarding as requested.", 0x4000 | 10, &[0;0]);
-                                       },
-                                       Some(id) => id.clone(),
-                               };
-                               if let Some((err, code, chan_update)) = loop {
-                                       let chan = channel_state.as_mut().unwrap().by_id.get_mut(&forwarding_id).unwrap();
-
-                                       // Note that we could technically not return an error yet here and just hope
-                                       // that the connection is reestablished or monitor updated by the time we get
-                                       // around to doing the actual forward, but better to fail early if we can and
-                                       // hopefully an attacker trying to path-trace payments cannot make this occur
-                                       // on a small/per-node/per-channel scale.
-                                       if !chan.is_live() { // channel_disabled
-                                               break Some(("Forwarding channel is not in a ready state.", 0x1000 | 20, Some(self.get_channel_update(chan).unwrap())));
-                                       }
-                                       if *amt_to_forward < chan.get_their_htlc_minimum_msat() { // amount_below_minimum
-                                               break Some(("HTLC amount was below the htlc_minimum_msat", 0x1000 | 11, Some(self.get_channel_update(chan).unwrap())));
-                                       }
-                                       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_our_fee_base_msat(&*self.fee_estimator) as u64) });
-                                       if fee.is_none() || msg.amount_msat < fee.unwrap() || (msg.amount_msat - fee.unwrap()) < *amt_to_forward { // fee_insufficient
-                                               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())));
-                                       }
-                                       if (msg.cltv_expiry as u64) < (*outgoing_cltv_value) as u64 + CLTV_EXPIRY_DELTA as u64 { // incorrect_cltv_expiry
-                                               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())));
-                                       }
-                                       let cur_height = self.latest_block_height.load(Ordering::Acquire) as u32 + 1;
-                                       // We want to have at least LATENCY_GRACE_PERIOD_BLOCKS to fail prior to going on chain CLAIM_BUFFER blocks before expiration
-                                       if msg.cltv_expiry <= cur_height + CLTV_CLAIM_BUFFER + LATENCY_GRACE_PERIOD_BLOCKS as u32 { // expiry_too_soon
-                                               break Some(("CLTV expiry is too close", 0x1000 | 14, Some(self.get_channel_update(chan).unwrap())));
-                                       }
-                                       if msg.cltv_expiry > cur_height + CLTV_FAR_FAR_AWAY as u32 { // expiry_too_far
-                                               break Some(("CLTV expiry is too far in the future", 21, None));
-                                       }
-                                       break None;
-                               }
-                               {
-                                       let mut res = Vec::with_capacity(8 + 128);
-                                       if let Some(chan_update) = chan_update {
-                                               if code == 0x1000 | 11 || code == 0x1000 | 12 {
-                                                       res.extend_from_slice(&byte_utils::be64_to_array(msg.amount_msat));
-                                               }
-                                               else if code == 0x1000 | 13 {
-                                                       res.extend_from_slice(&byte_utils::be32_to_array(msg.cltv_expiry));
-                                               }
-                                               else if code == 0x1000 | 20 {
-                                                       res.extend_from_slice(&byte_utils::be16_to_array(chan_update.contents.flags));
-                                               }
-                                               res.extend_from_slice(&chan_update.encode_with_len()[..]);
-                                       }
-                                       return_err!(err, code, &res[..]);
-                               }
-                       }
-               }
-
-               (pending_forward_info, channel_state.unwrap())
-       }
-
-       /// only fails if the channel does not yet have an assigned short_id
-       /// May be called with channel_state already locked!
-       fn get_channel_update(&self, chan: &Channel) -> Result<msgs::ChannelUpdate, HandleError> {
-               let short_channel_id = match chan.get_short_channel_id() {
-                       None => return Err(HandleError{err: "Channel not yet established", action: None}),
-                       Some(id) => id,
-               };
-
-               let were_node_one = PublicKey::from_secret_key(&self.secp_ctx, &self.our_network_key).serialize()[..] < chan.get_their_node_id().serialize()[..];
-
-               let unsigned = msgs::UnsignedChannelUpdate {
-                       chain_hash: self.genesis_hash,
-                       short_channel_id: short_channel_id,
-                       timestamp: chan.get_channel_update_count(),
-                       flags: (!were_node_one) as u16 | ((!chan.is_live() as u16) << 1),
-                       cltv_expiry_delta: CLTV_EXPIRY_DELTA,
-                       htlc_minimum_msat: chan.get_our_htlc_minimum_msat(),
-                       fee_base_msat: chan.get_our_fee_base_msat(&*self.fee_estimator),
-                       fee_proportional_millionths: chan.get_fee_proportional_millionths(),
-                       excess_data: Vec::new(),
-               };
-
-               let msg_hash = Sha256dHash::hash(&unsigned.encode()[..]);
-               let sig = self.secp_ctx.sign(&hash_to_message!(&msg_hash[..]), &self.our_network_key);
-
-               Ok(msgs::ChannelUpdate {
-                       signature: sig,
-                       contents: unsigned
-               })
-       }
-
-       /// Sends a payment along a given route.
-       ///
-       /// Value parameters are provided via the last hop in route, see documentation for RouteHop
-       /// fields for more info.
-       ///
-       /// Note that if the payment_hash already exists elsewhere (eg you're sending a duplicative
-       /// payment), we don't do anything to stop you! We always try to ensure that if the provided
-       /// next hop knows the preimage to payment_hash they can claim an additional amount as
-       /// specified in the last hop in the route! Thus, you should probably do your own
-       /// payment_preimage tracking (which you should already be doing as they represent "proof of
-       /// payment") and prevent double-sends yourself.
-       ///
-       /// May generate a SendHTLCs message event on success, which should be relayed.
-       ///
-       /// Raises APIError::RoutError when invalid route or forward parameter
-       /// (cltv_delta, fee, node public key) is specified.
-       /// Raises APIError::ChannelUnavailable if the next-hop channel is not available for updates
-       /// (including due to previous monitor update failure or new permanent monitor update failure).
-       /// Raised APIError::MonitorUpdateFailed if a new monitor update failure prevented sending the
-       /// relevant updates.
-       ///
-       /// In case of APIError::RouteError/APIError::ChannelUnavailable, the payment send has failed
-       /// and you may wish to retry via a different route immediately.
-       /// In case of APIError::MonitorUpdateFailed, the commitment update has been irrevocably
-       /// committed on our end and we're just waiting for a monitor update to send it. Do NOT retry
-       /// the payment via a different route unless you intend to pay twice!
-       pub fn send_payment(&self, route: Route, payment_hash: PaymentHash) -> Result<(), APIError> {
-               if route.hops.len() < 1 || route.hops.len() > 20 {
-                       return Err(APIError::RouteError{err: "Route didn't go anywhere/had bogus size"});
-               }
-               let our_node_id = self.get_our_node_id();
-               for (idx, hop) in route.hops.iter().enumerate() {
-                       if idx != route.hops.len() - 1 && hop.pubkey == our_node_id {
-                               return Err(APIError::RouteError{err: "Route went through us but wasn't a simple rebalance loop to us"});
-                       }
-               }
-
-               let session_priv = self.keys_manager.get_session_key();
-
-               let cur_height = self.latest_block_height.load(Ordering::Acquire) as u32 + 1;
-
-               let onion_keys = secp_call!(onion_utils::construct_onion_keys(&self.secp_ctx, &route, &session_priv),
-                               APIError::RouteError{err: "Pubkey along hop was maliciously selected"});
-               let (onion_payloads, htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(&route, cur_height)?;
-               let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, &payment_hash);
-
-               let _ = self.total_consistency_lock.read().unwrap();
-
-               let err: Result<(), _> = loop {
-                       let mut channel_lock = self.channel_state.lock().unwrap();
-
-                       let id = match channel_lock.short_to_id.get(&route.hops.first().unwrap().short_channel_id) {
-                               None => return Err(APIError::ChannelUnavailable{err: "No channel available with first hop!"}),
-                               Some(id) => id.clone(),
-                       };
-
-                       let channel_state = channel_lock.borrow_parts();
-                       if let hash_map::Entry::Occupied(mut chan) = channel_state.by_id.entry(id) {
-                               match {
-                                       if chan.get().get_their_node_id() != route.hops.first().unwrap().pubkey {
-                                               return Err(APIError::RouteError{err: "Node ID mismatch on first hop!"});
-                                       }
-                                       if !chan.get().is_live() {
-                                               return Err(APIError::ChannelUnavailable{err: "Peer for first hop currently disconnected/pending monitor update!"});
-                                       }
-                                       break_chan_entry!(self, chan.get_mut().send_htlc_and_commit(htlc_msat, payment_hash.clone(), htlc_cltv, HTLCSource::OutboundRoute {
-                                               route: route.clone(),
-                                               session_priv: session_priv.clone(),
-                                               first_hop_htlc_msat: htlc_msat,
-                                       }, onion_packet), channel_state, chan)
-                               } {
-                                       Some((update_add, commitment_signed, chan_monitor)) => {
-                                               if let Err(e) = self.monitor.add_update_monitor(chan_monitor.get_funding_txo().unwrap(), chan_monitor) {
-                                                       maybe_break_monitor_err!(self, e, channel_state, chan, RAACommitmentOrder::CommitmentFirst, false, true);
-                                                       // Note that MonitorUpdateFailed here indicates (per function docs)
-                                                       // that we will resent the commitment update once we unfree monitor
-                                                       // updating, so we have to take special care that we don't return
-                                                       // something else in case we will resend later!
-                                                       return Err(APIError::MonitorUpdateFailed);
-                                               }
-
-                                               channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
-                                                       node_id: route.hops.first().unwrap().pubkey,
-                                                       updates: msgs::CommitmentUpdate {
-                                                               update_add_htlcs: vec![update_add],
-                                                               update_fulfill_htlcs: Vec::new(),
-                                                               update_fail_htlcs: Vec::new(),
-                                                               update_fail_malformed_htlcs: Vec::new(),
-                                                               update_fee: None,
-                                                               commitment_signed,
-                                                       },
-                                               });
-                                       },
-                                       None => {},
-                               }
-                       } else { unreachable!(); }
-                       return Ok(());
-               };
-
-               match handle_error!(self, err) {
-                       Ok(_) => unreachable!(),
-                       Err(e) => {
-                               if let Some(msgs::ErrorAction::IgnoreError) = e.action {
-                               } else {
-                                       log_error!(self, "Got bad keys: {}!", e.err);
-                                       let mut channel_state = self.channel_state.lock().unwrap();
-                                       channel_state.pending_msg_events.push(events::MessageSendEvent::HandleError {
-                                               node_id: route.hops.first().unwrap().pubkey,
-                                               action: e.action,
-                                       });
-                               }
-                               Err(APIError::ChannelUnavailable { err: e.err })
-                       },
-               }
-       }
-
-       /// Call this upon creation of a funding transaction for the given channel.
-       ///
-       /// Note that ALL inputs in the transaction pointed to by funding_txo MUST spend SegWit outputs
-       /// or your counterparty can steal your funds!
-       ///
-       /// Panics if a funding transaction has already been provided for this channel.
-       ///
-       /// May panic if the funding_txo is duplicative with some other channel (note that this should
-       /// be trivially prevented by using unique funding transaction keys per-channel).
-       pub fn funding_transaction_generated(&self, temporary_channel_id: &[u8; 32], funding_txo: OutPoint) {
-               let _ = self.total_consistency_lock.read().unwrap();
-
-               let (mut chan, msg, chan_monitor) = {
-                       let (res, chan) = {
-                               let mut channel_state = self.channel_state.lock().unwrap();
-                               match channel_state.by_id.remove(temporary_channel_id) {
-                                       Some(mut chan) => {
-                                               (chan.get_outbound_funding_created(funding_txo)
-                                                       .map_err(|e| if let ChannelError::Close(msg) = e {
-                                                               MsgHandleErrInternal::from_finish_shutdown(msg, chan.channel_id(), chan.force_shutdown(), None)
-                                                       } else { unreachable!(); })
-                                               , chan)
-                                       },
-                                       None => return
-                               }
-                       };
-                       match handle_error!(self, res) {
-                               Ok(funding_msg) => {
-                                       (chan, funding_msg.0, funding_msg.1)
-                               },
-                               Err(e) => {
-                                       log_error!(self, "Got bad signatures: {}!", e.err);
-                                       let mut channel_state = self.channel_state.lock().unwrap();
-                                       channel_state.pending_msg_events.push(events::MessageSendEvent::HandleError {
-                                               node_id: chan.get_their_node_id(),
-                                               action: e.action,
-                                       });
-                                       return;
-                               },
-                       }
-               };
-               // Because we have exclusive ownership of the channel here we can release the channel_state
-               // lock before add_update_monitor
-               if let Err(e) = self.monitor.add_update_monitor(chan_monitor.get_funding_txo().unwrap(), chan_monitor) {
-                       match e {
-                               ChannelMonitorUpdateErr::PermanentFailure => {
-                                       match handle_error!(self, Err(MsgHandleErrInternal::from_finish_shutdown("ChannelMonitor storage failure", *temporary_channel_id, chan.force_shutdown(), None))) {
-                                               Err(e) => {
-                                                       log_error!(self, "Failed to store ChannelMonitor update for funding tx generation");
-                                                       let mut channel_state = self.channel_state.lock().unwrap();
-                                                       channel_state.pending_msg_events.push(events::MessageSendEvent::HandleError {
-                                                               node_id: chan.get_their_node_id(),
-                                                               action: e.action,
-                                                       });
-                                                       return;
-                                               },
-                                               Ok(()) => unreachable!(),
-                                       }
-                               },
-                               ChannelMonitorUpdateErr::TemporaryFailure => {
-                                       // Its completely fine to continue with a FundingCreated until the monitor
-                                       // update is persisted, as long as we don't generate the FundingBroadcastSafe
-                                       // until the monitor has been safely persisted (as funding broadcast is not,
-                                       // in fact, safe).
-                                       chan.monitor_update_failed(false, false, Vec::new(), Vec::new());
-                               },
-                       }
-               }
-
-               let mut channel_state = self.channel_state.lock().unwrap();
-               channel_state.pending_msg_events.push(events::MessageSendEvent::SendFundingCreated {
-                       node_id: chan.get_their_node_id(),
-                       msg: msg,
-               });
-               match channel_state.by_id.entry(chan.channel_id()) {
-                       hash_map::Entry::Occupied(_) => {
-                               panic!("Generated duplicate funding txid?");
-                       },
-                       hash_map::Entry::Vacant(e) => {
-                               e.insert(chan);
-                       }
-               }
-       }
-
-       fn get_announcement_sigs(&self, chan: &Channel) -> Option<msgs::AnnouncementSignatures> {
-               if !chan.should_announce() { return None }
-
-               let (announcement, our_bitcoin_sig) = match chan.get_channel_announcement(self.get_our_node_id(), self.genesis_hash.clone()) {
-                       Ok(res) => res,
-                       Err(_) => return None, // Only in case of state precondition violations eg channel is closing
-               };
-               let msghash = hash_to_message!(&Sha256dHash::hash(&announcement.encode()[..])[..]);
-               let our_node_sig = self.secp_ctx.sign(&msghash, &self.our_network_key);
-
-               Some(msgs::AnnouncementSignatures {
-                       channel_id: chan.channel_id(),
-                       short_channel_id: chan.get_short_channel_id().unwrap(),
-                       node_signature: our_node_sig,
-                       bitcoin_signature: our_bitcoin_sig,
-               })
-       }
-
-       /// Processes HTLCs which are pending waiting on random forward delay.
-       ///
-       /// Should only really ever be called in response to a PendingHTLCsForwardable event.
-       /// Will likely generate further events.
-       pub fn process_pending_htlc_forwards(&self) {
-               let _ = self.total_consistency_lock.read().unwrap();
-
-               let mut new_events = Vec::new();
-               let mut failed_forwards = Vec::new();
-               let mut handle_errors = Vec::new();
-               {
-                       let mut channel_state_lock = self.channel_state.lock().unwrap();
-                       let channel_state = channel_state_lock.borrow_parts();
-
-                       for (short_chan_id, mut pending_forwards) in channel_state.forward_htlcs.drain() {
-                               if short_chan_id != 0 {
-                                       let forward_chan_id = match channel_state.short_to_id.get(&short_chan_id) {
-                                               Some(chan_id) => chan_id.clone(),
-                                               None => {
-                                                       failed_forwards.reserve(pending_forwards.len());
-                                                       for forward_info in pending_forwards.drain(..) {
-                                                               match forward_info {
-                                                                       HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info } => {
-                                                                               let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
-                                                                                       short_channel_id: prev_short_channel_id,
-                                                                                       htlc_id: prev_htlc_id,
-                                                                                       incoming_packet_shared_secret: forward_info.incoming_shared_secret,
-                                                                               });
-                                                                               failed_forwards.push((htlc_source, forward_info.payment_hash, 0x4000 | 10, None));
-                                                                       },
-                                                                       HTLCForwardInfo::FailHTLC { .. } => {
-                                                                               // Channel went away before we could fail it. This implies
-                                                                               // the channel is now on chain and our counterparty is
-                                                                               // trying to broadcast the HTLC-Timeout, but that's their
-                                                                               // problem, not ours.
-                                                                       }
-                                                               }
-                                                       }
-                                                       continue;
-                                               }
-                                       };
-                                       if let hash_map::Entry::Occupied(mut chan) = channel_state.by_id.entry(forward_chan_id) {
-                                               let mut add_htlc_msgs = Vec::new();
-                                               let mut fail_htlc_msgs = Vec::new();
-                                               for forward_info in pending_forwards.drain(..) {
-                                                       match forward_info {
-                                                               HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info } => {
-                                                                       log_trace!(self, "Adding HTLC from short id {} with payment_hash {} to channel with short id {} after delay", log_bytes!(forward_info.payment_hash.0), prev_short_channel_id, short_chan_id);
-                                                                       let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
-                                                                               short_channel_id: prev_short_channel_id,
-                                                                               htlc_id: prev_htlc_id,
-                                                                               incoming_packet_shared_secret: forward_info.incoming_shared_secret,
-                                                                       });
-                                                                       match chan.get_mut().send_htlc(forward_info.amt_to_forward, forward_info.payment_hash, forward_info.outgoing_cltv_value, htlc_source.clone(), forward_info.onion_packet.unwrap()) {
-                                                                               Err(e) => {
-                                                                                       if let ChannelError::Ignore(msg) = e {
-                                                                                               log_trace!(self, "Failed to forward HTLC with payment_hash {}: {}", log_bytes!(forward_info.payment_hash.0), msg);
-                                                                                       } else {
-                                                                                               panic!("Stated return value requirements in send_htlc() were not met");
-                                                                                       }
-                                                                                       let chan_update = self.get_channel_update(chan.get()).unwrap();
-                                                                                       failed_forwards.push((htlc_source, forward_info.payment_hash, 0x1000 | 7, Some(chan_update)));
-                                                                                       continue;
-                                                                               },
-                                                                               Ok(update_add) => {
-                                                                                       match update_add {
-                                                                                               Some(msg) => { add_htlc_msgs.push(msg); },
-                                                                                               None => {
-                                                                                                       // Nothing to do here...we're waiting on a remote
-                                                                                                       // revoke_and_ack before we can add anymore HTLCs. The Channel
-                                                                                                       // will automatically handle building the update_add_htlc and
-                                                                                                       // commitment_signed messages when we can.
-                                                                                                       // TODO: Do some kind of timer to set the channel as !is_live()
-                                                                                                       // as we don't really want others relying on us relaying through
-                                                                                                       // this channel currently :/.
-                                                                                               }
-                                                                                       }
-                                                                               }
-                                                                       }
-                                                               },
-                                                               HTLCForwardInfo::FailHTLC { htlc_id, err_packet } => {
-                                                                       log_trace!(self, "Failing HTLC back to channel with short id {} after delay", short_chan_id);
-                                                                       match chan.get_mut().get_update_fail_htlc(htlc_id, err_packet) {
-                                                                               Err(e) => {
-                                                                                       if let ChannelError::Ignore(msg) = e {
-                                                                                               log_trace!(self, "Failed to fail backwards to short_id {}: {}", short_chan_id, msg);
-                                                                                       } else {
-                                                                                               panic!("Stated return value requirements in get_update_fail_htlc() were not met");
-                                                                                       }
-                                                                                       // fail-backs are best-effort, we probably already have one
-                                                                                       // pending, and if not that's OK, if not, the channel is on
-                                                                                       // the chain and sending the HTLC-Timeout is their problem.
-                                                                                       continue;
-                                                                               },
-                                                                               Ok(Some(msg)) => { fail_htlc_msgs.push(msg); },
-                                                                               Ok(None) => {
-                                                                                       // Nothing to do here...we're waiting on a remote
-                                                                                       // revoke_and_ack before we can update the commitment
-                                                                                       // transaction. The Channel will automatically handle
-                                                                                       // building the update_fail_htlc and commitment_signed
-                                                                                       // messages when we can.
-                                                                                       // We don't need any kind of timer here as they should fail
-                                                                                       // the channel onto the chain if they can't get our
-                                                                                       // update_fail_htlc in time, it's not our problem.
-                                                                               }
-                                                                       }
-                                                               },
-                                                       }
-                                               }
-
-                                               if !add_htlc_msgs.is_empty() || !fail_htlc_msgs.is_empty() {
-                                                       let (commitment_msg, monitor) = match chan.get_mut().send_commitment() {
-                                                               Ok(res) => res,
-                                                               Err(e) => {
-                                                                       if let ChannelError::Ignore(_) = e {
-                                                                               panic!("Stated return value requirements in send_commitment() were not met");
-                                                                       }
-                                                                       //TODO: Handle...this is bad!
-                                                                       continue;
-                                                               },
-                                                       };
-                                                       if let Err(e) = self.monitor.add_update_monitor(monitor.get_funding_txo().unwrap(), monitor) {
-                                                               handle_errors.push((chan.get().get_their_node_id(), handle_monitor_err!(self, e, channel_state, chan, RAACommitmentOrder::CommitmentFirst, false, true)));
-                                                               continue;
-                                                       }
-                                                       channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
-                                                               node_id: chan.get().get_their_node_id(),
-                                                               updates: msgs::CommitmentUpdate {
-                                                                       update_add_htlcs: add_htlc_msgs,
-                                                                       update_fulfill_htlcs: Vec::new(),
-                                                                       update_fail_htlcs: fail_htlc_msgs,
-                                                                       update_fail_malformed_htlcs: Vec::new(),
-                                                                       update_fee: None,
-                                                                       commitment_signed: commitment_msg,
-                                                               },
-                                                       });
-                                               }
-                                       } else {
-                                               unreachable!();
-                                       }
-                               } else {
-                                       for forward_info in pending_forwards.drain(..) {
-                                               match forward_info {
-                                                       HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info } => {
-                                                               let prev_hop_data = HTLCPreviousHopData {
-                                                                       short_channel_id: prev_short_channel_id,
-                                                                       htlc_id: prev_htlc_id,
-                                                                       incoming_packet_shared_secret: forward_info.incoming_shared_secret,
-                                                               };
-                                                               match channel_state.claimable_htlcs.entry(forward_info.payment_hash) {
-                                                                       hash_map::Entry::Occupied(mut entry) => entry.get_mut().push((forward_info.amt_to_forward, prev_hop_data)),
-                                                                       hash_map::Entry::Vacant(entry) => { entry.insert(vec![(forward_info.amt_to_forward, prev_hop_data)]); },
-                                                               };
-                                                               new_events.push(events::Event::PaymentReceived {
-                                                                       payment_hash: forward_info.payment_hash,
-                                                                       amt: forward_info.amt_to_forward,
-                                                               });
-                                                       },
-                                                       HTLCForwardInfo::FailHTLC { .. } => {
-                                                               panic!("Got pending fail of our own HTLC");
-                                                       }
-                                               }
-                                       }
-                               }
-                       }
-               }
-
-               for (htlc_source, payment_hash, failure_code, update) in failed_forwards.drain(..) {
-                       match update {
-                               None => self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), htlc_source, &payment_hash, HTLCFailReason::Reason { failure_code, data: Vec::new() }),
-                               Some(chan_update) => self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), htlc_source, &payment_hash, HTLCFailReason::Reason { failure_code, data: chan_update.encode_with_len() }),
-                       };
-               }
-
-               for (their_node_id, err) in handle_errors.drain(..) {
-                       match handle_error!(self, err) {
-                               Ok(_) => {},
-                               Err(e) => {
-                                       if let Some(msgs::ErrorAction::IgnoreError) = e.action {
-                                       } else {
-                                               let mut channel_state = self.channel_state.lock().unwrap();
-                                               channel_state.pending_msg_events.push(events::MessageSendEvent::HandleError {
-                                                       node_id: their_node_id,
-                                                       action: e.action,
-                                               });
-                                       }
-                               },
-                       }
-               }
-
-               if new_events.is_empty() { return }
-               let mut events = self.pending_events.lock().unwrap();
-               events.append(&mut new_events);
-       }
-
-       /// Indicates that the preimage for payment_hash is unknown or the received amount is incorrect
-       /// after a PaymentReceived event, failing the HTLC back to its origin and freeing resources
-       /// along the path (including in our own channel on which we received it).
-       /// Returns false if no payment was found to fail backwards, true if the process of failing the
-       /// HTLC backwards has been started.
-       pub fn fail_htlc_backwards(&self, payment_hash: &PaymentHash) -> bool {
-               let _ = self.total_consistency_lock.read().unwrap();
-
-               let mut channel_state = Some(self.channel_state.lock().unwrap());
-               let removed_source = channel_state.as_mut().unwrap().claimable_htlcs.remove(payment_hash);
-               if let Some(mut sources) = removed_source {
-                       for (recvd_value, htlc_with_hash) in sources.drain(..) {
-                               if channel_state.is_none() { channel_state = Some(self.channel_state.lock().unwrap()); }
-                               self.fail_htlc_backwards_internal(channel_state.take().unwrap(),
-                                               HTLCSource::PreviousHopData(htlc_with_hash), payment_hash,
-                                               HTLCFailReason::Reason { failure_code: 0x4000 | 15, data: byte_utils::be64_to_array(recvd_value).to_vec() });
-                       }
-                       true
-               } else { false }
-       }
-
-       /// Fails an HTLC backwards to the sender of it to us.
-       /// Note that while we take a channel_state lock as input, we do *not* assume consistency here.
-       /// There are several callsites that do stupid things like loop over a list of payment_hashes
-       /// to fail and take the channel_state lock for each iteration (as we take ownership and may
-       /// drop it). In other words, no assumptions are made that entries in claimable_htlcs point to
-       /// still-available channels.
-       fn fail_htlc_backwards_internal(&self, mut channel_state_lock: MutexGuard<ChannelHolder>, source: HTLCSource, payment_hash: &PaymentHash, onion_error: HTLCFailReason) {
-               //TODO: There is a timing attack here where if a node fails an HTLC back to us they can
-               //identify whether we sent it or not based on the (I presume) very different runtime
-               //between the branches here. We should make this async and move it into the forward HTLCs
-               //timer handling.
-               match source {
-                       HTLCSource::OutboundRoute { ref route, .. } => {
-                               log_trace!(self, "Failing outbound payment HTLC with payment_hash {}", log_bytes!(payment_hash.0));
-                               mem::drop(channel_state_lock);
-                               match &onion_error {
-                                       &HTLCFailReason::ErrorPacket { ref err } => {
-#[cfg(test)]
-                                               let (channel_update, payment_retryable, onion_error_code) = onion_utils::process_onion_failure(&self.secp_ctx, &self.logger, &source, err.data.clone());
-#[cfg(not(test))]
-                                               let (channel_update, payment_retryable, _) = onion_utils::process_onion_failure(&self.secp_ctx, &self.logger, &source, err.data.clone());
-                                               // TODO: If we decided to blame ourselves (or one of our channels) in
-                                               // process_onion_failure we should close that channel as it implies our
-                                               // next-hop is needlessly blaming us!
-                                               if let Some(update) = channel_update {
-                                                       self.channel_state.lock().unwrap().pending_msg_events.push(
-                                                               events::MessageSendEvent::PaymentFailureNetworkUpdate {
-                                                                       update,
-                                                               }
-                                                       );
-                                               }
-                                               self.pending_events.lock().unwrap().push(
-                                                       events::Event::PaymentFailed {
-                                                               payment_hash: payment_hash.clone(),
-                                                               rejected_by_dest: !payment_retryable,
-#[cfg(test)]
-                                                               error_code: onion_error_code
-                                                       }
-                                               );
-                                       },
-                                       &HTLCFailReason::Reason {
-#[cfg(test)]
-                                                       ref failure_code,
-                                                       .. } => {
-                                               // we get a fail_malformed_htlc from the first hop
-                                               // TODO: We'd like to generate a PaymentFailureNetworkUpdate for temporary
-                                               // failures here, but that would be insufficient as Router::get_route
-                                               // generally ignores its view of our own channels as we provide them via
-                                               // ChannelDetails.
-                                               // TODO: For non-temporary failures, we really should be closing the
-                                               // channel here as we apparently can't relay through them anyway.
-                                               self.pending_events.lock().unwrap().push(
-                                                       events::Event::PaymentFailed {
-                                                               payment_hash: payment_hash.clone(),
-                                                               rejected_by_dest: route.hops.len() == 1,
-#[cfg(test)]
-                                                               error_code: Some(*failure_code),
-                                                       }
-                                               );
-                                       }
-                               }
-                       },
-                       HTLCSource::PreviousHopData(HTLCPreviousHopData { short_channel_id, htlc_id, incoming_packet_shared_secret }) => {
-                               let err_packet = match onion_error {
-                                       HTLCFailReason::Reason { failure_code, data } => {
-                                               log_trace!(self, "Failing HTLC with payment_hash {} backwards from us with code {}", log_bytes!(payment_hash.0), failure_code);
-                                               let packet = onion_utils::build_failure_packet(&incoming_packet_shared_secret, failure_code, &data[..]).encode();
-                                               onion_utils::encrypt_failure_packet(&incoming_packet_shared_secret, &packet)
-                                       },
-                                       HTLCFailReason::ErrorPacket { err } => {
-                                               log_trace!(self, "Failing HTLC with payment_hash {} backwards with pre-built ErrorPacket", log_bytes!(payment_hash.0));
-                                               onion_utils::encrypt_failure_packet(&incoming_packet_shared_secret, &err.data)
-                                       }
-                               };
-
-                               let mut forward_event = None;
-                               if channel_state_lock.forward_htlcs.is_empty() {
-                                       forward_event = Some(Duration::from_millis(MIN_HTLC_RELAY_HOLDING_CELL_MILLIS));
-                               }
-                               match channel_state_lock.forward_htlcs.entry(short_channel_id) {
-                                       hash_map::Entry::Occupied(mut entry) => {
-                                               entry.get_mut().push(HTLCForwardInfo::FailHTLC { htlc_id, err_packet });
-                                       },
-                                       hash_map::Entry::Vacant(entry) => {
-                                               entry.insert(vec!(HTLCForwardInfo::FailHTLC { htlc_id, err_packet }));
-                                       }
-                               }
-                               mem::drop(channel_state_lock);
-                               if let Some(time) = forward_event {
-                                       let mut pending_events = self.pending_events.lock().unwrap();
-                                       pending_events.push(events::Event::PendingHTLCsForwardable {
-                                               time_forwardable: time
-                                       });
-                               }
-                       },
-               }
-       }
-
-       /// Provides a payment preimage in response to a PaymentReceived event, returning true and
-       /// generating message events for the net layer to claim the payment, if possible. Thus, you
-       /// should probably kick the net layer to go send messages if this returns true!
-       ///
-       /// May panic if called except in response to a PaymentReceived event.
-       pub fn claim_funds(&self, payment_preimage: PaymentPreimage) -> bool {
-               let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0).into_inner());
-
-               let _ = self.total_consistency_lock.read().unwrap();
-
-               let mut channel_state = Some(self.channel_state.lock().unwrap());
-               let removed_source = channel_state.as_mut().unwrap().claimable_htlcs.remove(&payment_hash);
-               if let Some(mut sources) = removed_source {
-                       // TODO: We should require the user specify the expected amount so that we can claim
-                       // only payments for the correct amount, and reject payments for incorrect amounts
-                       // (which are probably middle nodes probing to break our privacy).
-                       for (_, htlc_with_hash) in sources.drain(..) {
-                               if channel_state.is_none() { channel_state = Some(self.channel_state.lock().unwrap()); }
-                               self.claim_funds_internal(channel_state.take().unwrap(), HTLCSource::PreviousHopData(htlc_with_hash), payment_preimage);
-                       }
-                       true
-               } else { false }
-       }
-       fn claim_funds_internal(&self, mut channel_state_lock: MutexGuard<ChannelHolder>, source: HTLCSource, payment_preimage: PaymentPreimage) {
-               let (their_node_id, err) = loop {
-                       match source {
-                               HTLCSource::OutboundRoute { .. } => {
-                                       mem::drop(channel_state_lock);
-                                       let mut pending_events = self.pending_events.lock().unwrap();
-                                       pending_events.push(events::Event::PaymentSent {
-                                               payment_preimage
-                                       });
-                               },
-                               HTLCSource::PreviousHopData(HTLCPreviousHopData { short_channel_id, htlc_id, .. }) => {
-                                       //TODO: Delay the claimed_funds relaying just like we do outbound relay!
-                                       let channel_state = channel_state_lock.borrow_parts();
-
-                                       let chan_id = match channel_state.short_to_id.get(&short_channel_id) {
-                                               Some(chan_id) => chan_id.clone(),
-                                               None => {
-                                                       // TODO: There is probably a channel manager somewhere that needs to
-                                                       // learn the preimage as the channel already hit the chain and that's
-                                                       // why it's missing.
-                                                       return
-                                               }
-                                       };
-
-                                       if let hash_map::Entry::Occupied(mut chan) = channel_state.by_id.entry(chan_id) {
-                                               let was_frozen_for_monitor = chan.get().is_awaiting_monitor_update();
-                                               match chan.get_mut().get_update_fulfill_htlc_and_commit(htlc_id, payment_preimage) {
-                                                       Ok((msgs, monitor_option)) => {
-                                                               if let Some(chan_monitor) = monitor_option {
-                                                                       if let Err(e) = self.monitor.add_update_monitor(chan_monitor.get_funding_txo().unwrap(), chan_monitor) {
-                                                                               if was_frozen_for_monitor {
-                                                                                       assert!(msgs.is_none());
-                                                                               } else {
-                                                                                       break (chan.get().get_their_node_id(), handle_monitor_err!(self, e, channel_state, chan, RAACommitmentOrder::CommitmentFirst, false, msgs.is_some()));
-                                                                               }
-                                                                       }
-                                                               }
-                                                               if let Some((msg, commitment_signed)) = msgs {
-                                                                       channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
-                                                                               node_id: chan.get().get_their_node_id(),
-                                                                               updates: msgs::CommitmentUpdate {
-                                                                                       update_add_htlcs: Vec::new(),
-                                                                                       update_fulfill_htlcs: vec![msg],
-                                                                                       update_fail_htlcs: Vec::new(),
-                                                                                       update_fail_malformed_htlcs: Vec::new(),
-                                                                                       update_fee: None,
-                                                                                       commitment_signed,
-                                                                               }
-                                                                       });
-                                                               }
-                                                       },
-                                                       Err(_e) => {
-                                                               // TODO: There is probably a channel manager somewhere that needs to
-                                                               // learn the preimage as the channel may be about to hit the chain.
-                                                               //TODO: Do something with e?
-                                                               return
-                                                       },
-                                               }
-                                       } else { unreachable!(); }
-                               },
-                       }
-                       return;
-               };
-
-               match handle_error!(self, err) {
-                       Ok(_) => {},
-                       Err(e) => {
-                               if let Some(msgs::ErrorAction::IgnoreError) = e.action {
-                               } else {
-                                       let mut channel_state = self.channel_state.lock().unwrap();
-                                       channel_state.pending_msg_events.push(events::MessageSendEvent::HandleError {
-                                               node_id: their_node_id,
-                                               action: e.action,
-                                       });
-                               }
-                       },
-               }
-       }
-
-       /// Gets the node_id held by this ChannelManager
-       pub fn get_our_node_id(&self) -> PublicKey {
-               PublicKey::from_secret_key(&self.secp_ctx, &self.our_network_key)
-       }
-
-       /// Used to restore channels to normal operation after a
-       /// ChannelMonitorUpdateErr::TemporaryFailure was returned from a channel monitor update
-       /// operation.
-       pub fn test_restore_channel_monitor(&self) {
-               let mut close_results = Vec::new();
-               let mut htlc_forwards = Vec::new();
-               let mut htlc_failures = Vec::new();
-               let mut pending_events = Vec::new();
-               let _ = self.total_consistency_lock.read().unwrap();
-
-               {
-                       let mut channel_lock = self.channel_state.lock().unwrap();
-                       let channel_state = channel_lock.borrow_parts();
-                       let short_to_id = channel_state.short_to_id;
-                       let pending_msg_events = channel_state.pending_msg_events;
-                       channel_state.by_id.retain(|_, channel| {
-                               if channel.is_awaiting_monitor_update() {
-                                       let chan_monitor = channel.channel_monitor();
-                                       if let Err(e) = self.monitor.add_update_monitor(chan_monitor.get_funding_txo().unwrap(), chan_monitor) {
-                                               match e {
-                                                       ChannelMonitorUpdateErr::PermanentFailure => {
-                                                               // TODO: There may be some pending HTLCs that we intended to fail
-                                                               // backwards when a monitor update failed. We should make sure
-                                                               // knowledge of those gets moved into the appropriate in-memory
-                                                               // ChannelMonitor and they get failed backwards once we get
-                                                               // on-chain confirmations.
-                                                               // Note I think #198 addresses this, so once it's merged a test
-                                                               // should be written.
-                                                               if let Some(short_id) = channel.get_short_channel_id() {
-                                                                       short_to_id.remove(&short_id);
-                                                               }
-                                                               close_results.push(channel.force_shutdown());
-                                                               if let Ok(update) = self.get_channel_update(&channel) {
-                                                                       pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
-                                                                               msg: update
-                                                                       });
-                                                               }
-                                                               false
-                                                       },
-                                                       ChannelMonitorUpdateErr::TemporaryFailure => true,
-                                               }
-                                       } else {
-                                               let (raa, commitment_update, order, pending_forwards, mut pending_failures, needs_broadcast_safe, funding_locked) = channel.monitor_updating_restored();
-                                               if !pending_forwards.is_empty() {
-                                                       htlc_forwards.push((channel.get_short_channel_id().expect("We can't have pending forwards before funding confirmation"), pending_forwards));
-                                               }
-                                               htlc_failures.append(&mut pending_failures);
-
-                                               macro_rules! handle_cs { () => {
-                                                       if let Some(update) = commitment_update {
-                                                               pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
-                                                                       node_id: channel.get_their_node_id(),
-                                                                       updates: update,
-                                                               });
-                                                       }
-                                               } }
-                                               macro_rules! handle_raa { () => {
-                                                       if let Some(revoke_and_ack) = raa {
-                                                               pending_msg_events.push(events::MessageSendEvent::SendRevokeAndACK {
-                                                                       node_id: channel.get_their_node_id(),
-                                                                       msg: revoke_and_ack,
-                                                               });
-                                                       }
-                                               } }
-                                               match order {
-                                                       RAACommitmentOrder::CommitmentFirst => {
-                                                               handle_cs!();
-                                                               handle_raa!();
-                                                       },
-                                                       RAACommitmentOrder::RevokeAndACKFirst => {
-                                                               handle_raa!();
-                                                               handle_cs!();
-                                                       },
-                                               }
-                                               if needs_broadcast_safe {
-                                                       pending_events.push(events::Event::FundingBroadcastSafe {
-                                                               funding_txo: channel.get_funding_txo().unwrap(),
-                                                               user_channel_id: channel.get_user_id(),
-                                                       });
-                                               }
-                                               if let Some(msg) = funding_locked {
-                                                       pending_msg_events.push(events::MessageSendEvent::SendFundingLocked {
-                                                               node_id: channel.get_their_node_id(),
-                                                               msg,
-                                                       });
-                                                       if let Some(announcement_sigs) = self.get_announcement_sigs(channel) {
-                                                               pending_msg_events.push(events::MessageSendEvent::SendAnnouncementSignatures {
-                                                                       node_id: channel.get_their_node_id(),
-                                                                       msg: announcement_sigs,
-                                                               });
-                                                       }
-                                                       short_to_id.insert(channel.get_short_channel_id().unwrap(), channel.channel_id());
-                                               }
-                                               true
-                                       }
-                               } else { true }
-                       });
-               }
-
-               self.pending_events.lock().unwrap().append(&mut pending_events);
-
-               for failure in htlc_failures.drain(..) {
-                       self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), failure.0, &failure.1, failure.2);
-               }
-               self.forward_htlcs(&mut htlc_forwards[..]);
-
-               for res in close_results.drain(..) {
-                       self.finish_force_close_channel(res);
-               }
-       }
-
-       fn internal_open_channel(&self, their_node_id: &PublicKey, their_local_features: LocalFeatures, msg: &msgs::OpenChannel) -> Result<(), MsgHandleErrInternal> {
-               if msg.chain_hash != self.genesis_hash {
-                       return Err(MsgHandleErrInternal::send_err_msg_no_close("Unknown genesis block hash", msg.temporary_channel_id.clone()));
-               }
-
-               let channel = Channel::new_from_req(&*self.fee_estimator, &self.keys_manager, their_node_id.clone(), their_local_features, msg, 0, Arc::clone(&self.logger), &self.default_configuration)
-                       .map_err(|e| MsgHandleErrInternal::from_chan_no_close(e, msg.temporary_channel_id))?;
-               let mut channel_state_lock = self.channel_state.lock().unwrap();
-               let channel_state = channel_state_lock.borrow_parts();
-               match channel_state.by_id.entry(channel.channel_id()) {
-                       hash_map::Entry::Occupied(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("temporary_channel_id collision!", msg.temporary_channel_id.clone())),
-                       hash_map::Entry::Vacant(entry) => {
-                               channel_state.pending_msg_events.push(events::MessageSendEvent::SendAcceptChannel {
-                                       node_id: their_node_id.clone(),
-                                       msg: channel.get_accept_channel(),
-                               });
-                               entry.insert(channel);
-                       }
-               }
-               Ok(())
-       }
-
-       fn internal_accept_channel(&self, their_node_id: &PublicKey, their_local_features: LocalFeatures, msg: &msgs::AcceptChannel) -> Result<(), MsgHandleErrInternal> {
-               let (value, output_script, user_id) = {
-                       let mut channel_lock = self.channel_state.lock().unwrap();
-                       let channel_state = channel_lock.borrow_parts();
-                       match channel_state.by_id.entry(msg.temporary_channel_id) {
-                               hash_map::Entry::Occupied(mut chan) => {
-                                       if chan.get().get_their_node_id() != *their_node_id {
-                                               //TODO: see issue #153, need a consistent behavior on obnoxious behavior from random node
-                                               return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.temporary_channel_id));
-                                       }
-                                       try_chan_entry!(self, chan.get_mut().accept_channel(&msg, &self.default_configuration, their_local_features), channel_state, chan);
-                                       (chan.get().get_value_satoshis(), chan.get().get_funding_redeemscript().to_v0_p2wsh(), chan.get().get_user_id())
-                               },
-                               //TODO: same as above
-                               hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.temporary_channel_id))
-                       }
-               };
-               let mut pending_events = self.pending_events.lock().unwrap();
-               pending_events.push(events::Event::FundingGenerationReady {
-                       temporary_channel_id: msg.temporary_channel_id,
-                       channel_value_satoshis: value,
-                       output_script: output_script,
-                       user_channel_id: user_id,
-               });
-               Ok(())
-       }
-
-       fn internal_funding_created(&self, their_node_id: &PublicKey, msg: &msgs::FundingCreated) -> Result<(), MsgHandleErrInternal> {
-               let ((funding_msg, monitor_update), mut chan) = {
-                       let mut channel_lock = self.channel_state.lock().unwrap();
-                       let channel_state = channel_lock.borrow_parts();
-                       match channel_state.by_id.entry(msg.temporary_channel_id.clone()) {
-                               hash_map::Entry::Occupied(mut chan) => {
-                                       if chan.get().get_their_node_id() != *their_node_id {
-                                               //TODO: here and below MsgHandleErrInternal, #153 case
-                                               return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.temporary_channel_id));
-                                       }
-                                       (try_chan_entry!(self, chan.get_mut().funding_created(msg), channel_state, chan), chan.remove())
-                               },
-                               hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.temporary_channel_id))
-                       }
-               };
-               // Because we have exclusive ownership of the channel here we can release the channel_state
-               // lock before add_update_monitor
-               if let Err(e) = self.monitor.add_update_monitor(monitor_update.get_funding_txo().unwrap(), monitor_update) {
-                       match e {
-                               ChannelMonitorUpdateErr::PermanentFailure => {
-                                       // Note that we reply with the new channel_id in error messages if we gave up on the
-                                       // channel, not the temporary_channel_id. This is compatible with ourselves, but the
-                                       // spec is somewhat ambiguous here. Not a huge deal since we'll send error messages for
-                                       // any messages referencing a previously-closed channel anyway.
-                                       return Err(MsgHandleErrInternal::from_finish_shutdown("ChannelMonitor storage failure", funding_msg.channel_id, chan.force_shutdown(), None));
-                               },
-                               ChannelMonitorUpdateErr::TemporaryFailure => {
-                                       // There's no problem signing a counterparty's funding transaction if our monitor
-                                       // hasn't persisted to disk yet - we can't lose money on a transaction that we haven't
-                                       // accepted payment from yet. We do, however, need to wait to send our funding_locked
-                                       // until we have persisted our monitor.
-                                       chan.monitor_update_failed(false, false, Vec::new(), Vec::new());
-                               },
-                       }
-               }
-               let mut channel_state_lock = self.channel_state.lock().unwrap();
-               let channel_state = channel_state_lock.borrow_parts();
-               match channel_state.by_id.entry(funding_msg.channel_id) {
-                       hash_map::Entry::Occupied(_) => {
-                               return Err(MsgHandleErrInternal::send_err_msg_no_close("Already had channel with the new channel_id", funding_msg.channel_id))
-                       },
-                       hash_map::Entry::Vacant(e) => {
-                               channel_state.pending_msg_events.push(events::MessageSendEvent::SendFundingSigned {
-                                       node_id: their_node_id.clone(),
-                                       msg: funding_msg,
-                               });
-                               e.insert(chan);
-                       }
-               }
-               Ok(())
-       }
-
-       fn internal_funding_signed(&self, their_node_id: &PublicKey, msg: &msgs::FundingSigned) -> Result<(), MsgHandleErrInternal> {
-               let (funding_txo, user_id) = {
-                       let mut channel_lock = self.channel_state.lock().unwrap();
-                       let channel_state = channel_lock.borrow_parts();
-                       match channel_state.by_id.entry(msg.channel_id) {
-                               hash_map::Entry::Occupied(mut chan) => {
-                                       if chan.get().get_their_node_id() != *their_node_id {
-                                               //TODO: here and below MsgHandleErrInternal, #153 case
-                                               return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
-                                       }
-                                       let chan_monitor = try_chan_entry!(self, chan.get_mut().funding_signed(&msg), channel_state, chan);
-                                       if let Err(e) = self.monitor.add_update_monitor(chan_monitor.get_funding_txo().unwrap(), chan_monitor) {
-                                               return_monitor_err!(self, e, channel_state, chan, RAACommitmentOrder::RevokeAndACKFirst, false, false);
-                                       }
-                                       (chan.get().get_funding_txo().unwrap(), chan.get().get_user_id())
-                               },
-                               hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
-                       }
-               };
-               let mut pending_events = self.pending_events.lock().unwrap();
-               pending_events.push(events::Event::FundingBroadcastSafe {
-                       funding_txo: funding_txo,
-                       user_channel_id: user_id,
-               });
-               Ok(())
-       }
-
-       fn internal_funding_locked(&self, their_node_id: &PublicKey, msg: &msgs::FundingLocked) -> Result<(), MsgHandleErrInternal> {
-               let mut channel_state_lock = self.channel_state.lock().unwrap();
-               let channel_state = channel_state_lock.borrow_parts();
-               match channel_state.by_id.entry(msg.channel_id) {
-                       hash_map::Entry::Occupied(mut chan) => {
-                               if chan.get().get_their_node_id() != *their_node_id {
-                                       //TODO: here and below MsgHandleErrInternal, #153 case
-                                       return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
-                               }
-                               try_chan_entry!(self, chan.get_mut().funding_locked(&msg), channel_state, chan);
-                               if let Some(announcement_sigs) = self.get_announcement_sigs(chan.get()) {
-                                       channel_state.pending_msg_events.push(events::MessageSendEvent::SendAnnouncementSignatures {
-                                               node_id: their_node_id.clone(),
-                                               msg: announcement_sigs,
-                                       });
-                               }
-                               Ok(())
-                       },
-                       hash_map::Entry::Vacant(_) => Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
-               }
-       }
-
-       fn internal_shutdown(&self, their_node_id: &PublicKey, msg: &msgs::Shutdown) -> Result<(), MsgHandleErrInternal> {
-               let (mut dropped_htlcs, chan_option) = {
-                       let mut channel_state_lock = self.channel_state.lock().unwrap();
-                       let channel_state = channel_state_lock.borrow_parts();
-
-                       match channel_state.by_id.entry(msg.channel_id.clone()) {
-                               hash_map::Entry::Occupied(mut chan_entry) => {
-                                       if chan_entry.get().get_their_node_id() != *their_node_id {
-                                               //TODO: here and below MsgHandleErrInternal, #153 case
-                                               return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
-                                       }
-                                       let (shutdown, closing_signed, dropped_htlcs) = try_chan_entry!(self, chan_entry.get_mut().shutdown(&*self.fee_estimator, &msg), channel_state, chan_entry);
-                                       if let Some(msg) = shutdown {
-                                               channel_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
-                                                       node_id: their_node_id.clone(),
-                                                       msg,
-                                               });
-                                       }
-                                       if let Some(msg) = closing_signed {
-                                               channel_state.pending_msg_events.push(events::MessageSendEvent::SendClosingSigned {
-                                                       node_id: their_node_id.clone(),
-                                                       msg,
-                                               });
-                                       }
-                                       if chan_entry.get().is_shutdown() {
-                                               if let Some(short_id) = chan_entry.get().get_short_channel_id() {
-                                                       channel_state.short_to_id.remove(&short_id);
-                                               }
-                                               (dropped_htlcs, Some(chan_entry.remove_entry().1))
-                                       } else { (dropped_htlcs, None) }
-                               },
-                               hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
-                       }
-               };
-               for htlc_source in dropped_htlcs.drain(..) {
-                       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() });
-               }
-               if let Some(chan) = chan_option {
-                       if let Ok(update) = self.get_channel_update(&chan) {
-                               let mut channel_state = self.channel_state.lock().unwrap();
-                               channel_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
-                                       msg: update
-                               });
-                       }
-               }
-               Ok(())
-       }
-
-       fn internal_closing_signed(&self, their_node_id: &PublicKey, msg: &msgs::ClosingSigned) -> Result<(), MsgHandleErrInternal> {
-               let (tx, chan_option) = {
-                       let mut channel_state_lock = self.channel_state.lock().unwrap();
-                       let channel_state = channel_state_lock.borrow_parts();
-                       match channel_state.by_id.entry(msg.channel_id.clone()) {
-                               hash_map::Entry::Occupied(mut chan_entry) => {
-                                       if chan_entry.get().get_their_node_id() != *their_node_id {
-                                               //TODO: here and below MsgHandleErrInternal, #153 case
-                                               return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
-                                       }
-                                       let (closing_signed, tx) = try_chan_entry!(self, chan_entry.get_mut().closing_signed(&*self.fee_estimator, &msg), channel_state, chan_entry);
-                                       if let Some(msg) = closing_signed {
-                                               channel_state.pending_msg_events.push(events::MessageSendEvent::SendClosingSigned {
-                                                       node_id: their_node_id.clone(),
-                                                       msg,
-                                               });
-                                       }
-                                       if tx.is_some() {
-                                               // We're done with this channel, we've got a signed closing transaction and
-                                               // will send the closing_signed back to the remote peer upon return. This
-                                               // also implies there are no pending HTLCs left on the channel, so we can
-                                               // fully delete it from tracking (the channel monitor is still around to
-                                               // watch for old state broadcasts)!
-                                               if let Some(short_id) = chan_entry.get().get_short_channel_id() {
-                                                       channel_state.short_to_id.remove(&short_id);
-                                               }
-                                               (tx, Some(chan_entry.remove_entry().1))
-                                       } else { (tx, None) }
-                               },
-                               hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
-                       }
-               };
-               if let Some(broadcast_tx) = tx {
-                       self.tx_broadcaster.broadcast_transaction(&broadcast_tx);
-               }
-               if let Some(chan) = chan_option {
-                       if let Ok(update) = self.get_channel_update(&chan) {
-                               let mut channel_state = self.channel_state.lock().unwrap();
-                               channel_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
-                                       msg: update
-                               });
-                       }
-               }
-               Ok(())
-       }
-
-       fn internal_update_add_htlc(&self, their_node_id: &PublicKey, msg: &msgs::UpdateAddHTLC) -> Result<(), MsgHandleErrInternal> {
-               //TODO: BOLT 4 points out a specific attack where a peer may re-send an onion packet and
-               //determine the state of the payment based on our response/if we forward anything/the time
-               //we take to respond. We should take care to avoid allowing such an attack.
-               //
-               //TODO: There exists a further attack where a node may garble the onion data, forward it to
-               //us repeatedly garbled in different ways, and compare our error messages, which are
-               //encrypted with the same key. It's not immediately obvious how to usefully exploit that,
-               //but we should prevent it anyway.
-
-               let (mut pending_forward_info, mut channel_state_lock) = self.decode_update_add_htlc_onion(msg);
-               let channel_state = channel_state_lock.borrow_parts();
-
-               match channel_state.by_id.entry(msg.channel_id) {
-                       hash_map::Entry::Occupied(mut chan) => {
-                               if chan.get().get_their_node_id() != *their_node_id {
-                                       //TODO: here MsgHandleErrInternal, #153 case
-                                       return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
-                               }
-                               if !chan.get().is_usable() {
-                                       // If the update_add is completely bogus, the call will Err and we will close,
-                                       // but if we've sent a shutdown and they haven't acknowledged it yet, we just
-                                       // want to reject the new HTLC and fail it backwards instead of forwarding.
-                                       if let PendingHTLCStatus::Forward(PendingForwardHTLCInfo { incoming_shared_secret, .. }) = pending_forward_info {
-                                               let chan_update = self.get_channel_update(chan.get());
-                                               pending_forward_info = PendingHTLCStatus::Fail(HTLCFailureMsg::Relay(msgs::UpdateFailHTLC {
-                                                       channel_id: msg.channel_id,
-                                                       htlc_id: msg.htlc_id,
-                                                       reason: if let Ok(update) = chan_update {
-                                                               // TODO: Note that |20 is defined as "channel FROM the processing
-                                                               // node has been disabled" (emphasis mine), which seems to imply
-                                                               // that we can't return |20 for an inbound channel being disabled.
-                                                               // This probably needs a spec update but should definitely be
-                                                               // allowed.
-                                                               onion_utils::build_first_hop_failure_packet(&incoming_shared_secret, 0x1000|20, &{
-                                                                       let mut res = Vec::with_capacity(8 + 128);
-                                                                       res.extend_from_slice(&byte_utils::be16_to_array(update.contents.flags));
-                                                                       res.extend_from_slice(&update.encode_with_len()[..]);
-                                                                       res
-                                                               }[..])
-                                                       } else {
-                                                               // This can only happen if the channel isn't in the fully-funded
-                                                               // state yet, implying our counterparty is trying to route payments
-                                                               // over the channel back to themselves (cause no one else should
-                                                               // know the short_id is a lightning channel yet). We should have no
-                                                               // problem just calling this unknown_next_peer
-                                                               onion_utils::build_first_hop_failure_packet(&incoming_shared_secret, 0x4000|10, &[])
-                                                       },
-                                               }));
-                                       }
-                               }
-                               try_chan_entry!(self, chan.get_mut().update_add_htlc(&msg, pending_forward_info), channel_state, chan);
-                       },
-                       hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
-               }
-               Ok(())
-       }
-
-       fn internal_update_fulfill_htlc(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFulfillHTLC) -> Result<(), MsgHandleErrInternal> {
-               let mut channel_lock = self.channel_state.lock().unwrap();
-               let htlc_source = {
-                       let channel_state = channel_lock.borrow_parts();
-                       match channel_state.by_id.entry(msg.channel_id) {
-                               hash_map::Entry::Occupied(mut chan) => {
-                                       if chan.get().get_their_node_id() != *their_node_id {
-                                               //TODO: here and below MsgHandleErrInternal, #153 case
-                                               return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
-                                       }
-                                       try_chan_entry!(self, chan.get_mut().update_fulfill_htlc(&msg), channel_state, chan)
-                               },
-                               hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
-                       }
-               };
-               self.claim_funds_internal(channel_lock, htlc_source, msg.payment_preimage.clone());
-               Ok(())
-       }
-
-       fn internal_update_fail_htlc(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFailHTLC) -> Result<(), MsgHandleErrInternal> {
-               let mut channel_lock = self.channel_state.lock().unwrap();
-               let channel_state = channel_lock.borrow_parts();
-               match channel_state.by_id.entry(msg.channel_id) {
-                       hash_map::Entry::Occupied(mut chan) => {
-                               if chan.get().get_their_node_id() != *their_node_id {
-                                       //TODO: here and below MsgHandleErrInternal, #153 case
-                                       return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
-                               }
-                               try_chan_entry!(self, chan.get_mut().update_fail_htlc(&msg, HTLCFailReason::ErrorPacket { err: msg.reason.clone() }), channel_state, chan);
-                       },
-                       hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
-               }
-               Ok(())
-       }
-
-       fn internal_update_fail_malformed_htlc(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFailMalformedHTLC) -> Result<(), MsgHandleErrInternal> {
-               let mut channel_lock = self.channel_state.lock().unwrap();
-               let channel_state = channel_lock.borrow_parts();
-               match channel_state.by_id.entry(msg.channel_id) {
-                       hash_map::Entry::Occupied(mut chan) => {
-                               if chan.get().get_their_node_id() != *their_node_id {
-                                       //TODO: here and below MsgHandleErrInternal, #153 case
-                                       return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
-                               }
-                               if (msg.failure_code & 0x8000) == 0 {
-                                       try_chan_entry!(self, Err(ChannelError::Close("Got update_fail_malformed_htlc with BADONION not set")), channel_state, chan);
-                               }
-                               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);
-                               Ok(())
-                       },
-                       hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
-               }
-       }
-
-       fn internal_commitment_signed(&self, their_node_id: &PublicKey, msg: &msgs::CommitmentSigned) -> Result<(), MsgHandleErrInternal> {
-               let mut channel_state_lock = self.channel_state.lock().unwrap();
-               let channel_state = channel_state_lock.borrow_parts();
-               match channel_state.by_id.entry(msg.channel_id) {
-                       hash_map::Entry::Occupied(mut chan) => {
-                               if chan.get().get_their_node_id() != *their_node_id {
-                                       //TODO: here and below MsgHandleErrInternal, #153 case
-                                       return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
-                               }
-                               let (revoke_and_ack, commitment_signed, closing_signed, chan_monitor) =
-                                       try_chan_entry!(self, chan.get_mut().commitment_signed(&msg, &*self.fee_estimator), channel_state, chan);
-                               if let Err(e) = self.monitor.add_update_monitor(chan_monitor.get_funding_txo().unwrap(), chan_monitor) {
-                                       return_monitor_err!(self, e, channel_state, chan, RAACommitmentOrder::RevokeAndACKFirst, true, commitment_signed.is_some());
-                                       //TODO: Rebroadcast closing_signed if present on monitor update restoration
-                               }
-                               channel_state.pending_msg_events.push(events::MessageSendEvent::SendRevokeAndACK {
-                                       node_id: their_node_id.clone(),
-                                       msg: revoke_and_ack,
-                               });
-                               if let Some(msg) = commitment_signed {
-                                       channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
-                                               node_id: their_node_id.clone(),
-                                               updates: msgs::CommitmentUpdate {
-                                                       update_add_htlcs: Vec::new(),
-                                                       update_fulfill_htlcs: Vec::new(),
-                                                       update_fail_htlcs: Vec::new(),
-                                                       update_fail_malformed_htlcs: Vec::new(),
-                                                       update_fee: None,
-                                                       commitment_signed: msg,
-                                               },
-                                       });
-                               }
-                               if let Some(msg) = closing_signed {
-                                       channel_state.pending_msg_events.push(events::MessageSendEvent::SendClosingSigned {
-                                               node_id: their_node_id.clone(),
-                                               msg,
-                                       });
-                               }
-                               Ok(())
-                       },
-                       hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
-               }
-       }
-
-       #[inline]
-       fn forward_htlcs(&self, per_source_pending_forwards: &mut [(u64, Vec<(PendingForwardHTLCInfo, u64)>)]) {
-               for &mut (prev_short_channel_id, ref mut pending_forwards) in per_source_pending_forwards {
-                       let mut forward_event = None;
-                       if !pending_forwards.is_empty() {
-                               let mut channel_state = self.channel_state.lock().unwrap();
-                               if channel_state.forward_htlcs.is_empty() {
-                                       forward_event = Some(Duration::from_millis(MIN_HTLC_RELAY_HOLDING_CELL_MILLIS))
-                               }
-                               for (forward_info, prev_htlc_id) in pending_forwards.drain(..) {
-                                       match channel_state.forward_htlcs.entry(forward_info.short_channel_id) {
-                                               hash_map::Entry::Occupied(mut entry) => {
-                                                       entry.get_mut().push(HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info });
-                                               },
-                                               hash_map::Entry::Vacant(entry) => {
-                                                       entry.insert(vec!(HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info }));
-                                               }
-                                       }
-                               }
-                       }
-                       match forward_event {
-                               Some(time) => {
-                                       let mut pending_events = self.pending_events.lock().unwrap();
-                                       pending_events.push(events::Event::PendingHTLCsForwardable {
-                                               time_forwardable: time
-                                       });
-                               }
-                               None => {},
-                       }
-               }
-       }
-
-       fn internal_revoke_and_ack(&self, their_node_id: &PublicKey, msg: &msgs::RevokeAndACK) -> Result<(), MsgHandleErrInternal> {
-               let (pending_forwards, mut pending_failures, short_channel_id) = {
-                       let mut channel_state_lock = self.channel_state.lock().unwrap();
-                       let channel_state = channel_state_lock.borrow_parts();
-                       match channel_state.by_id.entry(msg.channel_id) {
-                               hash_map::Entry::Occupied(mut chan) => {
-                                       if chan.get().get_their_node_id() != *their_node_id {
-                                               //TODO: here and below MsgHandleErrInternal, #153 case
-                                               return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
-                                       }
-                                       let was_frozen_for_monitor = chan.get().is_awaiting_monitor_update();
-                                       let (commitment_update, pending_forwards, pending_failures, closing_signed, chan_monitor) =
-                                               try_chan_entry!(self, chan.get_mut().revoke_and_ack(&msg, &*self.fee_estimator), channel_state, chan);
-                                       if let Err(e) = self.monitor.add_update_monitor(chan_monitor.get_funding_txo().unwrap(), chan_monitor) {
-                                               if was_frozen_for_monitor {
-                                                       assert!(commitment_update.is_none() && closing_signed.is_none() && pending_forwards.is_empty() && pending_failures.is_empty());
-                                                       return Err(MsgHandleErrInternal::ignore_no_close("Previous monitor update failure prevented responses to RAA"));
-                                               } else {
-                                                       return_monitor_err!(self, e, channel_state, chan, RAACommitmentOrder::CommitmentFirst, false, commitment_update.is_some(), pending_forwards, pending_failures);
-                                               }
-                                       }
-                                       if let Some(updates) = commitment_update {
-                                               channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
-                                                       node_id: their_node_id.clone(),
-                                                       updates,
-                                               });
-                                       }
-                                       if let Some(msg) = closing_signed {
-                                               channel_state.pending_msg_events.push(events::MessageSendEvent::SendClosingSigned {
-                                                       node_id: their_node_id.clone(),
-                                                       msg,
-                                               });
-                                       }
-                                       (pending_forwards, pending_failures, chan.get().get_short_channel_id().expect("RAA should only work on a short-id-available channel"))
-                               },
-                               hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
-                       }
-               };
-               for failure in pending_failures.drain(..) {
-                       self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), failure.0, &failure.1, failure.2);
-               }
-               self.forward_htlcs(&mut [(short_channel_id, pending_forwards)]);
-
-               Ok(())
-       }
-
-       fn internal_update_fee(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFee) -> Result<(), MsgHandleErrInternal> {
-               let mut channel_lock = self.channel_state.lock().unwrap();
-               let channel_state = channel_lock.borrow_parts();
-               match channel_state.by_id.entry(msg.channel_id) {
-                       hash_map::Entry::Occupied(mut chan) => {
-                               if chan.get().get_their_node_id() != *their_node_id {
-                                       //TODO: here and below MsgHandleErrInternal, #153 case
-                                       return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
-                               }
-                               try_chan_entry!(self, chan.get_mut().update_fee(&*self.fee_estimator, &msg), channel_state, chan);
-                       },
-                       hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
-               }
-               Ok(())
-       }
-
-       fn internal_announcement_signatures(&self, their_node_id: &PublicKey, msg: &msgs::AnnouncementSignatures) -> Result<(), MsgHandleErrInternal> {
-               let mut channel_state_lock = self.channel_state.lock().unwrap();
-               let channel_state = channel_state_lock.borrow_parts();
-
-               match channel_state.by_id.entry(msg.channel_id) {
-                       hash_map::Entry::Occupied(mut chan) => {
-                               if chan.get().get_their_node_id() != *their_node_id {
-                                       return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
-                               }
-                               if !chan.get().is_usable() {
-                                       return Err(MsgHandleErrInternal::from_no_close(HandleError{err: "Got an announcement_signatures before we were ready for it", action: Some(msgs::ErrorAction::IgnoreError)}));
-                               }
-
-                               let our_node_id = self.get_our_node_id();
-                               let (announcement, our_bitcoin_sig) =
-                                       try_chan_entry!(self, chan.get_mut().get_channel_announcement(our_node_id.clone(), self.genesis_hash.clone()), channel_state, chan);
-
-                               let were_node_one = announcement.node_id_1 == our_node_id;
-                               let msghash = hash_to_message!(&Sha256dHash::hash(&announcement.encode()[..])[..]);
-                               if self.secp_ctx.verify(&msghash, &msg.node_signature, if were_node_one { &announcement.node_id_2 } else { &announcement.node_id_1 }).is_err() ||
-                                               self.secp_ctx.verify(&msghash, &msg.bitcoin_signature, if were_node_one { &announcement.bitcoin_key_2 } else { &announcement.bitcoin_key_1 }).is_err() {
-                                       try_chan_entry!(self, Err(ChannelError::Close("Bad announcement_signatures node_signature")), channel_state, chan);
-                               }
-
-                               let our_node_sig = self.secp_ctx.sign(&msghash, &self.our_network_key);
-
-                               channel_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelAnnouncement {
-                                       msg: msgs::ChannelAnnouncement {
-                                               node_signature_1: if were_node_one { our_node_sig } else { msg.node_signature },
-                                               node_signature_2: if were_node_one { msg.node_signature } else { our_node_sig },
-                                               bitcoin_signature_1: if were_node_one { our_bitcoin_sig } else { msg.bitcoin_signature },
-                                               bitcoin_signature_2: if were_node_one { msg.bitcoin_signature } else { our_bitcoin_sig },
-                                               contents: announcement,
-                                       },
-                                       update_msg: self.get_channel_update(chan.get()).unwrap(), // can only fail if we're not in a ready state
-                               });
-                       },
-                       hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
-               }
-               Ok(())
-       }
-
-       fn internal_channel_reestablish(&self, their_node_id: &PublicKey, msg: &msgs::ChannelReestablish) -> Result<(), MsgHandleErrInternal> {
-               let mut channel_state_lock = self.channel_state.lock().unwrap();
-               let channel_state = channel_state_lock.borrow_parts();
-
-               match channel_state.by_id.entry(msg.channel_id) {
-                       hash_map::Entry::Occupied(mut chan) => {
-                               if chan.get().get_their_node_id() != *their_node_id {
-                                       return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
-                               }
-                               let (funding_locked, revoke_and_ack, commitment_update, channel_monitor, mut order, shutdown) =
-                                       try_chan_entry!(self, chan.get_mut().channel_reestablish(msg), channel_state, chan);
-                               if let Some(monitor) = channel_monitor {
-                                       if let Err(e) = self.monitor.add_update_monitor(monitor.get_funding_txo().unwrap(), monitor) {
-                                               // channel_reestablish doesn't guarantee the order it returns is sensical
-                                               // for the messages it returns, but if we're setting what messages to
-                                               // re-transmit on monitor update success, we need to make sure it is sane.
-                                               if revoke_and_ack.is_none() {
-                                                       order = RAACommitmentOrder::CommitmentFirst;
-                                               }
-                                               if commitment_update.is_none() {
-                                                       order = RAACommitmentOrder::RevokeAndACKFirst;
-                                               }
-                                               return_monitor_err!(self, e, channel_state, chan, order, revoke_and_ack.is_some(), commitment_update.is_some());
-                                               //TODO: Resend the funding_locked if needed once we get the monitor running again
-                                       }
-                               }
-                               if let Some(msg) = funding_locked {
-                                       channel_state.pending_msg_events.push(events::MessageSendEvent::SendFundingLocked {
-                                               node_id: their_node_id.clone(),
-                                               msg
-                                       });
-                               }
-                               macro_rules! send_raa { () => {
-                                       if let Some(msg) = revoke_and_ack {
-                                               channel_state.pending_msg_events.push(events::MessageSendEvent::SendRevokeAndACK {
-                                                       node_id: their_node_id.clone(),
-                                                       msg
-                                               });
-                                       }
-                               } }
-                               macro_rules! send_cu { () => {
-                                       if let Some(updates) = commitment_update {
-                                               channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
-                                                       node_id: their_node_id.clone(),
-                                                       updates
-                                               });
-                                       }
-                               } }
-                               match order {
-                                       RAACommitmentOrder::RevokeAndACKFirst => {
-                                               send_raa!();
-                                               send_cu!();
-                                       },
-                                       RAACommitmentOrder::CommitmentFirst => {
-                                               send_cu!();
-                                               send_raa!();
-                                       },
-                               }
-                               if let Some(msg) = shutdown {
-                                       channel_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
-                                               node_id: their_node_id.clone(),
-                                               msg,
-                                       });
-                               }
-                               Ok(())
-                       },
-                       hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
-               }
-       }
-
-       /// Begin Update fee process. Allowed only on an outbound channel.
-       /// If successful, will generate a UpdateHTLCs event, so you should probably poll
-       /// PeerManager::process_events afterwards.
-       /// Note: This API is likely to change!
-       #[doc(hidden)]
-       pub fn update_fee(&self, channel_id: [u8;32], feerate_per_kw: u64) -> Result<(), APIError> {
-               let _ = self.total_consistency_lock.read().unwrap();
-               let their_node_id;
-               let err: Result<(), _> = loop {
-                       let mut channel_state_lock = self.channel_state.lock().unwrap();
-                       let channel_state = channel_state_lock.borrow_parts();
-
-                       match channel_state.by_id.entry(channel_id) {
-                               hash_map::Entry::Vacant(_) => return Err(APIError::APIMisuseError{err: "Failed to find corresponding channel"}),
-                               hash_map::Entry::Occupied(mut chan) => {
-                                       if !chan.get().is_outbound() {
-                                               return Err(APIError::APIMisuseError{err: "update_fee cannot be sent for an inbound channel"});
-                                       }
-                                       if chan.get().is_awaiting_monitor_update() {
-                                               return Err(APIError::MonitorUpdateFailed);
-                                       }
-                                       if !chan.get().is_live() {
-                                               return Err(APIError::ChannelUnavailable{err: "Channel is either not yet fully established or peer is currently disconnected"});
-                                       }
-                                       their_node_id = chan.get().get_their_node_id();
-                                       if let Some((update_fee, commitment_signed, chan_monitor)) =
-                                                       break_chan_entry!(self, chan.get_mut().send_update_fee_and_commit(feerate_per_kw), channel_state, chan)
-                                       {
-                                               if let Err(_e) = self.monitor.add_update_monitor(chan_monitor.get_funding_txo().unwrap(), chan_monitor) {
-                                                       unimplemented!();
-                                               }
-                                               channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
-                                                       node_id: chan.get().get_their_node_id(),
-                                                       updates: msgs::CommitmentUpdate {
-                                                               update_add_htlcs: Vec::new(),
-                                                               update_fulfill_htlcs: Vec::new(),
-                                                               update_fail_htlcs: Vec::new(),
-                                                               update_fail_malformed_htlcs: Vec::new(),
-                                                               update_fee: Some(update_fee),
-                                                               commitment_signed,
-                                                       },
-                                               });
-                                       }
-                               },
-                       }
-                       return Ok(())
-               };
-
-               match handle_error!(self, err) {
-                       Ok(_) => unreachable!(),
-                       Err(e) => {
-                               if let Some(msgs::ErrorAction::IgnoreError) = e.action {
-                               } else {
-                                       log_error!(self, "Got bad keys: {}!", e.err);
-                                       let mut channel_state = self.channel_state.lock().unwrap();
-                                       channel_state.pending_msg_events.push(events::MessageSendEvent::HandleError {
-                                               node_id: their_node_id,
-                                               action: e.action,
-                                       });
-                               }
-                               Err(APIError::APIMisuseError { err: e.err })
-                       },
-               }
-       }
-}
-
-impl events::MessageSendEventsProvider for ChannelManager {
-       fn get_and_clear_pending_msg_events(&self) -> Vec<events::MessageSendEvent> {
-               // TODO: Event release to users and serialization is currently race-y: it's very easy for a
-               // user to serialize a ChannelManager with pending events in it and lose those events on
-               // restart. This is doubly true for the fail/fulfill-backs from monitor events!
-               {
-                       //TODO: This behavior should be documented.
-                       for htlc_update in self.monitor.fetch_pending_htlc_updated() {
-                               if let Some(preimage) = htlc_update.payment_preimage {
-                                       log_trace!(self, "Claiming HTLC with preimage {} from our monitor", log_bytes!(preimage.0));
-                                       self.claim_funds_internal(self.channel_state.lock().unwrap(), htlc_update.source, preimage);
-                               } else {
-                                       log_trace!(self, "Failing HTLC with hash {} from our monitor", log_bytes!(htlc_update.payment_hash.0));
-                                       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() });
-                               }
-                       }
-               }
-
-               let mut ret = Vec::new();
-               let mut channel_state = self.channel_state.lock().unwrap();
-               mem::swap(&mut ret, &mut channel_state.pending_msg_events);
-               ret
-       }
-}
-
-impl events::EventsProvider for ChannelManager {
-       fn get_and_clear_pending_events(&self) -> Vec<events::Event> {
-               // TODO: Event release to users and serialization is currently race-y: it's very easy for a
-               // user to serialize a ChannelManager with pending events in it and lose those events on
-               // restart. This is doubly true for the fail/fulfill-backs from monitor events!
-               {
-                       //TODO: This behavior should be documented.
-                       for htlc_update in self.monitor.fetch_pending_htlc_updated() {
-                               if let Some(preimage) = htlc_update.payment_preimage {
-                                       log_trace!(self, "Claiming HTLC with preimage {} from our monitor", log_bytes!(preimage.0));
-                                       self.claim_funds_internal(self.channel_state.lock().unwrap(), htlc_update.source, preimage);
-                               } else {
-                                       log_trace!(self, "Failing HTLC with hash {} from our monitor", log_bytes!(htlc_update.payment_hash.0));
-                                       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() });
-                               }
-                       }
-               }
-
-               let mut ret = Vec::new();
-               let mut pending_events = self.pending_events.lock().unwrap();
-               mem::swap(&mut ret, &mut *pending_events);
-               ret
-       }
-}
-
-impl ChainListener for ChannelManager {
-       fn block_connected(&self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], indexes_of_txn_matched: &[u32]) {
-               let header_hash = header.bitcoin_hash();
-               log_trace!(self, "Block {} at height {} connected with {} txn matched", header_hash, height, txn_matched.len());
-               let _ = self.total_consistency_lock.read().unwrap();
-               let mut failed_channels = Vec::new();
-               {
-                       let mut channel_lock = self.channel_state.lock().unwrap();
-                       let channel_state = channel_lock.borrow_parts();
-                       let short_to_id = channel_state.short_to_id;
-                       let pending_msg_events = channel_state.pending_msg_events;
-                       channel_state.by_id.retain(|_, channel| {
-                               let chan_res = channel.block_connected(header, height, txn_matched, indexes_of_txn_matched);
-                               if let Ok(Some(funding_locked)) = chan_res {
-                                       pending_msg_events.push(events::MessageSendEvent::SendFundingLocked {
-                                               node_id: channel.get_their_node_id(),
-                                               msg: funding_locked,
-                                       });
-                                       if let Some(announcement_sigs) = self.get_announcement_sigs(channel) {
-                                               pending_msg_events.push(events::MessageSendEvent::SendAnnouncementSignatures {
-                                                       node_id: channel.get_their_node_id(),
-                                                       msg: announcement_sigs,
-                                               });
-                                       }
-                                       short_to_id.insert(channel.get_short_channel_id().unwrap(), channel.channel_id());
-                               } else if let Err(e) = chan_res {
-                                       pending_msg_events.push(events::MessageSendEvent::HandleError {
-                                               node_id: channel.get_their_node_id(),
-                                               action: Some(msgs::ErrorAction::SendErrorMessage { msg: e }),
-                                       });
-                                       return false;
-                               }
-                               if let Some(funding_txo) = channel.get_funding_txo() {
-                                       for tx in txn_matched {
-                                               for inp in tx.input.iter() {
-                                                       if inp.previous_output == funding_txo.into_bitcoin_outpoint() {
-                                                               log_trace!(self, "Detected channel-closing tx {} spending {}:{}, closing channel {}", tx.txid(), inp.previous_output.txid, inp.previous_output.vout, log_bytes!(channel.channel_id()));
-                                                               if let Some(short_id) = channel.get_short_channel_id() {
-                                                                       short_to_id.remove(&short_id);
-                                                               }
-                                                               // It looks like our counterparty went on-chain. We go ahead and
-                                                               // broadcast our latest local state as well here, just in case its
-                                                               // some kind of SPV attack, though we expect these to be dropped.
-                                                               failed_channels.push(channel.force_shutdown());
-                                                               if let Ok(update) = self.get_channel_update(&channel) {
-                                                                       pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
-                                                                               msg: update
-                                                                       });
-                                                               }
-                                                               return false;
-                                                       }
-                                               }
-                                       }
-                               }
-                               if channel.is_funding_initiated() && channel.channel_monitor().would_broadcast_at_height(height) {
-                                       if let Some(short_id) = channel.get_short_channel_id() {
-                                               short_to_id.remove(&short_id);
-                                       }
-                                       failed_channels.push(channel.force_shutdown());
-                                       // If would_broadcast_at_height() is true, the channel_monitor will broadcast
-                                       // the latest local tx for us, so we should skip that here (it doesn't really
-                                       // hurt anything, but does make tests a bit simpler).
-                                       failed_channels.last_mut().unwrap().0 = Vec::new();
-                                       if let Ok(update) = self.get_channel_update(&channel) {
-                                               pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
-                                                       msg: update
-                                               });
-                                       }
-                                       return false;
-                               }
-                               true
-                       });
-               }
-               for failure in failed_channels.drain(..) {
-                       self.finish_force_close_channel(failure);
-               }
-               self.latest_block_height.store(height as usize, Ordering::Release);
-               *self.last_block_hash.try_lock().expect("block_(dis)connected must not be called in parallel") = header_hash;
-       }
-
-       /// We force-close the channel without letting our counterparty participate in the shutdown
-       fn block_disconnected(&self, header: &BlockHeader, _: u32) {
-               let _ = self.total_consistency_lock.read().unwrap();
-               let mut failed_channels = Vec::new();
-               {
-                       let mut channel_lock = self.channel_state.lock().unwrap();
-                       let channel_state = channel_lock.borrow_parts();
-                       let short_to_id = channel_state.short_to_id;
-                       let pending_msg_events = channel_state.pending_msg_events;
-                       channel_state.by_id.retain(|_,  v| {
-                               if v.block_disconnected(header) {
-                                       if let Some(short_id) = v.get_short_channel_id() {
-                                               short_to_id.remove(&short_id);
-                                       }
-                                       failed_channels.push(v.force_shutdown());
-                                       if let Ok(update) = self.get_channel_update(&v) {
-                                               pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
-                                                       msg: update
-                                               });
-                                       }
-                                       false
-                               } else {
-                                       true
-                               }
-                       });
-               }
-               for failure in failed_channels.drain(..) {
-                       self.finish_force_close_channel(failure);
-               }
-               self.latest_block_height.fetch_sub(1, Ordering::AcqRel);
-               *self.last_block_hash.try_lock().expect("block_(dis)connected must not be called in parallel") = header.bitcoin_hash();
-       }
-}
-
-impl ChannelMessageHandler for ChannelManager {
-       //TODO: Handle errors and close channel (or so)
-       fn handle_open_channel(&self, their_node_id: &PublicKey, their_local_features: LocalFeatures, msg: &msgs::OpenChannel) -> Result<(), HandleError> {
-               let _ = self.total_consistency_lock.read().unwrap();
-               handle_error!(self, self.internal_open_channel(their_node_id, their_local_features, msg))
-       }
-
-       fn handle_accept_channel(&self, their_node_id: &PublicKey, their_local_features: LocalFeatures, msg: &msgs::AcceptChannel) -> Result<(), HandleError> {
-               let _ = self.total_consistency_lock.read().unwrap();
-               handle_error!(self, self.internal_accept_channel(their_node_id, their_local_features, msg))
-       }
-
-       fn handle_funding_created(&self, their_node_id: &PublicKey, msg: &msgs::FundingCreated) -> Result<(), HandleError> {
-               let _ = self.total_consistency_lock.read().unwrap();
-               handle_error!(self, self.internal_funding_created(their_node_id, msg))
-       }
-
-       fn handle_funding_signed(&self, their_node_id: &PublicKey, msg: &msgs::FundingSigned) -> Result<(), HandleError> {
-               let _ = self.total_consistency_lock.read().unwrap();
-               handle_error!(self, self.internal_funding_signed(their_node_id, msg))
-       }
-
-       fn handle_funding_locked(&self, their_node_id: &PublicKey, msg: &msgs::FundingLocked) -> Result<(), HandleError> {
-               let _ = self.total_consistency_lock.read().unwrap();
-               handle_error!(self, self.internal_funding_locked(their_node_id, msg))
-       }
-
-       fn handle_shutdown(&self, their_node_id: &PublicKey, msg: &msgs::Shutdown) -> Result<(), HandleError> {
-               let _ = self.total_consistency_lock.read().unwrap();
-               handle_error!(self, self.internal_shutdown(their_node_id, msg))
-       }
-
-       fn handle_closing_signed(&self, their_node_id: &PublicKey, msg: &msgs::ClosingSigned) -> Result<(), HandleError> {
-               let _ = self.total_consistency_lock.read().unwrap();
-               handle_error!(self, self.internal_closing_signed(their_node_id, msg))
-       }
-
-       fn handle_update_add_htlc(&self, their_node_id: &PublicKey, msg: &msgs::UpdateAddHTLC) -> Result<(), msgs::HandleError> {
-               let _ = self.total_consistency_lock.read().unwrap();
-               handle_error!(self, self.internal_update_add_htlc(their_node_id, msg))
-       }
-
-       fn handle_update_fulfill_htlc(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFulfillHTLC) -> Result<(), HandleError> {
-               let _ = self.total_consistency_lock.read().unwrap();
-               handle_error!(self, self.internal_update_fulfill_htlc(their_node_id, msg))
-       }
-
-       fn handle_update_fail_htlc(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFailHTLC) -> Result<(), HandleError> {
-               let _ = self.total_consistency_lock.read().unwrap();
-               handle_error!(self, self.internal_update_fail_htlc(their_node_id, msg))
-       }
-
-       fn handle_update_fail_malformed_htlc(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFailMalformedHTLC) -> Result<(), HandleError> {
-               let _ = self.total_consistency_lock.read().unwrap();
-               handle_error!(self, self.internal_update_fail_malformed_htlc(their_node_id, msg))
-       }
-
-       fn handle_commitment_signed(&self, their_node_id: &PublicKey, msg: &msgs::CommitmentSigned) -> Result<(), HandleError> {
-               let _ = self.total_consistency_lock.read().unwrap();
-               handle_error!(self, self.internal_commitment_signed(their_node_id, msg))
-       }
-
-       fn handle_revoke_and_ack(&self, their_node_id: &PublicKey, msg: &msgs::RevokeAndACK) -> Result<(), HandleError> {
-               let _ = self.total_consistency_lock.read().unwrap();
-               handle_error!(self, self.internal_revoke_and_ack(their_node_id, msg))
-       }
-
-       fn handle_update_fee(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFee) -> Result<(), HandleError> {
-               let _ = self.total_consistency_lock.read().unwrap();
-               handle_error!(self, self.internal_update_fee(their_node_id, msg))
-       }
-
-       fn handle_announcement_signatures(&self, their_node_id: &PublicKey, msg: &msgs::AnnouncementSignatures) -> Result<(), HandleError> {
-               let _ = self.total_consistency_lock.read().unwrap();
-               handle_error!(self, self.internal_announcement_signatures(their_node_id, msg))
-       }
-
-       fn handle_channel_reestablish(&self, their_node_id: &PublicKey, msg: &msgs::ChannelReestablish) -> Result<(), HandleError> {
-               let _ = self.total_consistency_lock.read().unwrap();
-               handle_error!(self, self.internal_channel_reestablish(their_node_id, msg))
-       }
-
-       fn peer_disconnected(&self, their_node_id: &PublicKey, no_connection_possible: bool) {
-               let _ = self.total_consistency_lock.read().unwrap();
-               let mut failed_channels = Vec::new();
-               let mut failed_payments = Vec::new();
-               {
-                       let mut channel_state_lock = self.channel_state.lock().unwrap();
-                       let channel_state = channel_state_lock.borrow_parts();
-                       let short_to_id = channel_state.short_to_id;
-                       let pending_msg_events = channel_state.pending_msg_events;
-                       if no_connection_possible {
-                               log_debug!(self, "Failing all channels with {} due to no_connection_possible", log_pubkey!(their_node_id));
-                               channel_state.by_id.retain(|_, chan| {
-                                       if chan.get_their_node_id() == *their_node_id {
-                                               if let Some(short_id) = chan.get_short_channel_id() {
-                                                       short_to_id.remove(&short_id);
-                                               }
-                                               failed_channels.push(chan.force_shutdown());
-                                               if let Ok(update) = self.get_channel_update(&chan) {
-                                                       pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
-                                                               msg: update
-                                                       });
-                                               }
-                                               false
-                                       } else {
-                                               true
-                                       }
-                               });
-                       } else {
-                               log_debug!(self, "Marking channels with {} disconnected and generating channel_updates", log_pubkey!(their_node_id));
-                               channel_state.by_id.retain(|_, chan| {
-                                       if chan.get_their_node_id() == *their_node_id {
-                                               //TODO: mark channel disabled (and maybe announce such after a timeout).
-                                               let failed_adds = chan.remove_uncommitted_htlcs_and_mark_paused();
-                                               if !failed_adds.is_empty() {
-                                                       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
-                                                       failed_payments.push((chan_update, failed_adds));
-                                               }
-                                               if chan.is_shutdown() {
-                                                       if let Some(short_id) = chan.get_short_channel_id() {
-                                                               short_to_id.remove(&short_id);
-                                                       }
-                                                       return false;
-                                               }
-                                       }
-                                       true
-                               })
-                       }
-                       pending_msg_events.retain(|msg| {
-                               match msg {
-                                       &events::MessageSendEvent::SendAcceptChannel { ref node_id, .. } => node_id != their_node_id,
-                                       &events::MessageSendEvent::SendOpenChannel { ref node_id, .. } => node_id != their_node_id,
-                                       &events::MessageSendEvent::SendFundingCreated { ref node_id, .. } => node_id != their_node_id,
-                                       &events::MessageSendEvent::SendFundingSigned { ref node_id, .. } => node_id != their_node_id,
-                                       &events::MessageSendEvent::SendFundingLocked { ref node_id, .. } => node_id != their_node_id,
-                                       &events::MessageSendEvent::SendAnnouncementSignatures { ref node_id, .. } => node_id != their_node_id,
-                                       &events::MessageSendEvent::UpdateHTLCs { ref node_id, .. } => node_id != their_node_id,
-                                       &events::MessageSendEvent::SendRevokeAndACK { ref node_id, .. } => node_id != their_node_id,
-                                       &events::MessageSendEvent::SendClosingSigned { ref node_id, .. } => node_id != their_node_id,
-                                       &events::MessageSendEvent::SendShutdown { ref node_id, .. } => node_id != their_node_id,
-                                       &events::MessageSendEvent::SendChannelReestablish { ref node_id, .. } => node_id != their_node_id,
-                                       &events::MessageSendEvent::BroadcastChannelAnnouncement { .. } => true,
-                                       &events::MessageSendEvent::BroadcastChannelUpdate { .. } => true,
-                                       &events::MessageSendEvent::HandleError { ref node_id, .. } => node_id != their_node_id,
-                                       &events::MessageSendEvent::PaymentFailureNetworkUpdate { .. } => true,
-                               }
-                       });
-               }
-               for failure in failed_channels.drain(..) {
-                       self.finish_force_close_channel(failure);
-               }
-               for (chan_update, mut htlc_sources) in failed_payments {
-                       for (htlc_source, payment_hash) in htlc_sources.drain(..) {
-                               self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), htlc_source, &payment_hash, HTLCFailReason::Reason { failure_code: 0x1000 | 7, data: chan_update.clone() });
-                       }
-               }
-       }
-
-       fn peer_connected(&self, their_node_id: &PublicKey) {
-               log_debug!(self, "Generating channel_reestablish events for {}", log_pubkey!(their_node_id));
-
-               let _ = self.total_consistency_lock.read().unwrap();
-               let mut channel_state_lock = self.channel_state.lock().unwrap();
-               let channel_state = channel_state_lock.borrow_parts();
-               let pending_msg_events = channel_state.pending_msg_events;
-               channel_state.by_id.retain(|_, chan| {
-                       if chan.get_their_node_id() == *their_node_id {
-                               if !chan.have_received_message() {
-                                       // If we created this (outbound) channel while we were disconnected from the
-                                       // peer we probably failed to send the open_channel message, which is now
-                                       // lost. We can't have had anything pending related to this channel, so we just
-                                       // drop it.
-                                       false
-                               } else {
-                                       pending_msg_events.push(events::MessageSendEvent::SendChannelReestablish {
-                                               node_id: chan.get_their_node_id(),
-                                               msg: chan.get_channel_reestablish(),
-                                       });
-                                       true
-                               }
-                       } else { true }
-               });
-               //TODO: Also re-broadcast announcement_signatures
-       }
-
-       fn handle_error(&self, their_node_id: &PublicKey, msg: &msgs::ErrorMessage) {
-               let _ = self.total_consistency_lock.read().unwrap();
-
-               if msg.channel_id == [0; 32] {
-                       for chan in self.list_channels() {
-                               if chan.remote_network_id == *their_node_id {
-                                       self.force_close_channel(&chan.channel_id);
-                               }
-                       }
-               } else {
-                       self.force_close_channel(&msg.channel_id);
-               }
-       }
-}
-
-const SERIALIZATION_VERSION: u8 = 1;
-const MIN_SERIALIZATION_VERSION: u8 = 1;
-
-impl Writeable for PendingForwardHTLCInfo {
-       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
-               self.onion_packet.write(writer)?;
-               self.incoming_shared_secret.write(writer)?;
-               self.payment_hash.write(writer)?;
-               self.short_channel_id.write(writer)?;
-               self.amt_to_forward.write(writer)?;
-               self.outgoing_cltv_value.write(writer)?;
-               Ok(())
-       }
-}
-
-impl<R: ::std::io::Read> Readable<R> for PendingForwardHTLCInfo {
-       fn read(reader: &mut R) -> Result<PendingForwardHTLCInfo, DecodeError> {
-               Ok(PendingForwardHTLCInfo {
-                       onion_packet: Readable::read(reader)?,
-                       incoming_shared_secret: Readable::read(reader)?,
-                       payment_hash: Readable::read(reader)?,
-                       short_channel_id: Readable::read(reader)?,
-                       amt_to_forward: Readable::read(reader)?,
-                       outgoing_cltv_value: Readable::read(reader)?,
-               })
-       }
-}
-
-impl Writeable for HTLCFailureMsg {
-       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
-               match self {
-                       &HTLCFailureMsg::Relay(ref fail_msg) => {
-                               0u8.write(writer)?;
-                               fail_msg.write(writer)?;
-                       },
-                       &HTLCFailureMsg::Malformed(ref fail_msg) => {
-                               1u8.write(writer)?;
-                               fail_msg.write(writer)?;
-                       }
-               }
-               Ok(())
-       }
-}
-
-impl<R: ::std::io::Read> Readable<R> for HTLCFailureMsg {
-       fn read(reader: &mut R) -> Result<HTLCFailureMsg, DecodeError> {
-               match <u8 as Readable<R>>::read(reader)? {
-                       0 => Ok(HTLCFailureMsg::Relay(Readable::read(reader)?)),
-                       1 => Ok(HTLCFailureMsg::Malformed(Readable::read(reader)?)),
-                       _ => Err(DecodeError::InvalidValue),
-               }
-       }
-}
-
-impl Writeable for PendingHTLCStatus {
-       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
-               match self {
-                       &PendingHTLCStatus::Forward(ref forward_info) => {
-                               0u8.write(writer)?;
-                               forward_info.write(writer)?;
-                       },
-                       &PendingHTLCStatus::Fail(ref fail_msg) => {
-                               1u8.write(writer)?;
-                               fail_msg.write(writer)?;
-                       }
-               }
-               Ok(())
-       }
-}
-
-impl<R: ::std::io::Read> Readable<R> for PendingHTLCStatus {
-       fn read(reader: &mut R) -> Result<PendingHTLCStatus, DecodeError> {
-               match <u8 as Readable<R>>::read(reader)? {
-                       0 => Ok(PendingHTLCStatus::Forward(Readable::read(reader)?)),
-                       1 => Ok(PendingHTLCStatus::Fail(Readable::read(reader)?)),
-                       _ => Err(DecodeError::InvalidValue),
-               }
-       }
-}
-
-impl_writeable!(HTLCPreviousHopData, 0, {
-       short_channel_id,
-       htlc_id,
-       incoming_packet_shared_secret
-});
-
-impl Writeable for HTLCSource {
-       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
-               match self {
-                       &HTLCSource::PreviousHopData(ref hop_data) => {
-                               0u8.write(writer)?;
-                               hop_data.write(writer)?;
-                       },
-                       &HTLCSource::OutboundRoute { ref route, ref session_priv, ref first_hop_htlc_msat } => {
-                               1u8.write(writer)?;
-                               route.write(writer)?;
-                               session_priv.write(writer)?;
-                               first_hop_htlc_msat.write(writer)?;
-                       }
-               }
-               Ok(())
-       }
-}
-
-impl<R: ::std::io::Read> Readable<R> for HTLCSource {
-       fn read(reader: &mut R) -> Result<HTLCSource, DecodeError> {
-               match <u8 as Readable<R>>::read(reader)? {
-                       0 => Ok(HTLCSource::PreviousHopData(Readable::read(reader)?)),
-                       1 => Ok(HTLCSource::OutboundRoute {
-                               route: Readable::read(reader)?,
-                               session_priv: Readable::read(reader)?,
-                               first_hop_htlc_msat: Readable::read(reader)?,
-                       }),
-                       _ => Err(DecodeError::InvalidValue),
-               }
-       }
-}
-
-impl Writeable for HTLCFailReason {
-       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
-               match self {
-                       &HTLCFailReason::ErrorPacket { ref err } => {
-                               0u8.write(writer)?;
-                               err.write(writer)?;
-                       },
-                       &HTLCFailReason::Reason { ref failure_code, ref data } => {
-                               1u8.write(writer)?;
-                               failure_code.write(writer)?;
-                               data.write(writer)?;
-                       }
-               }
-               Ok(())
-       }
-}
-
-impl<R: ::std::io::Read> Readable<R> for HTLCFailReason {
-       fn read(reader: &mut R) -> Result<HTLCFailReason, DecodeError> {
-               match <u8 as Readable<R>>::read(reader)? {
-                       0 => Ok(HTLCFailReason::ErrorPacket { err: Readable::read(reader)? }),
-                       1 => Ok(HTLCFailReason::Reason {
-                               failure_code: Readable::read(reader)?,
-                               data: Readable::read(reader)?,
-                       }),
-                       _ => Err(DecodeError::InvalidValue),
-               }
-       }
-}
-
-impl Writeable for HTLCForwardInfo {
-       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
-               match self {
-                       &HTLCForwardInfo::AddHTLC { ref prev_short_channel_id, ref prev_htlc_id, ref forward_info } => {
-                               0u8.write(writer)?;
-                               prev_short_channel_id.write(writer)?;
-                               prev_htlc_id.write(writer)?;
-                               forward_info.write(writer)?;
-                       },
-                       &HTLCForwardInfo::FailHTLC { ref htlc_id, ref err_packet } => {
-                               1u8.write(writer)?;
-                               htlc_id.write(writer)?;
-                               err_packet.write(writer)?;
-                       },
-               }
-               Ok(())
-       }
-}
-
-impl<R: ::std::io::Read> Readable<R> for HTLCForwardInfo {
-       fn read(reader: &mut R) -> Result<HTLCForwardInfo, DecodeError> {
-               match <u8 as Readable<R>>::read(reader)? {
-                       0 => Ok(HTLCForwardInfo::AddHTLC {
-                               prev_short_channel_id: Readable::read(reader)?,
-                               prev_htlc_id: Readable::read(reader)?,
-                               forward_info: Readable::read(reader)?,
-                       }),
-                       1 => Ok(HTLCForwardInfo::FailHTLC {
-                               htlc_id: Readable::read(reader)?,
-                               err_packet: Readable::read(reader)?,
-                       }),
-                       _ => Err(DecodeError::InvalidValue),
-               }
-       }
-}
-
-impl Writeable for ChannelManager {
-       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
-               let _ = self.total_consistency_lock.write().unwrap();
-
-               writer.write_all(&[SERIALIZATION_VERSION; 1])?;
-               writer.write_all(&[MIN_SERIALIZATION_VERSION; 1])?;
-
-               self.genesis_hash.write(writer)?;
-               (self.latest_block_height.load(Ordering::Acquire) as u32).write(writer)?;
-               self.last_block_hash.lock().unwrap().write(writer)?;
-
-               let channel_state = self.channel_state.lock().unwrap();
-               let mut unfunded_channels = 0;
-               for (_, channel) in channel_state.by_id.iter() {
-                       if !channel.is_funding_initiated() {
-                               unfunded_channels += 1;
-                       }
-               }
-               ((channel_state.by_id.len() - unfunded_channels) as u64).write(writer)?;
-               for (_, channel) in channel_state.by_id.iter() {
-                       if channel.is_funding_initiated() {
-                               channel.write(writer)?;
-                       }
-               }
-
-               (channel_state.forward_htlcs.len() as u64).write(writer)?;
-               for (short_channel_id, pending_forwards) in channel_state.forward_htlcs.iter() {
-                       short_channel_id.write(writer)?;
-                       (pending_forwards.len() as u64).write(writer)?;
-                       for forward in pending_forwards {
-                               forward.write(writer)?;
-                       }
-               }
-
-               (channel_state.claimable_htlcs.len() as u64).write(writer)?;
-               for (payment_hash, previous_hops) in channel_state.claimable_htlcs.iter() {
-                       payment_hash.write(writer)?;
-                       (previous_hops.len() as u64).write(writer)?;
-                       for &(recvd_amt, ref previous_hop) in previous_hops.iter() {
-                               recvd_amt.write(writer)?;
-                               previous_hop.write(writer)?;
-                       }
-               }
-
-               Ok(())
-       }
-}
-
-/// Arguments for the creation of a ChannelManager that are not deserialized.
-///
-/// At a high-level, the process for deserializing a ChannelManager and resuming normal operation
-/// is:
-/// 1) Deserialize all stored ChannelMonitors.
-/// 2) Deserialize the ChannelManager by filling in this struct and calling <(Sha256dHash,
-///    ChannelManager)>::read(reader, args).
-///    This may result in closing some Channels if the ChannelMonitor is newer than the stored
-///    ChannelManager state to ensure no loss of funds. Thus, transactions may be broadcasted.
-/// 3) Register all relevant ChannelMonitor outpoints with your chain watch mechanism using
-///    ChannelMonitor::get_monitored_outpoints and ChannelMonitor::get_funding_txo().
-/// 4) Reconnect blocks on your ChannelMonitors.
-/// 5) Move the ChannelMonitors into your local ManyChannelMonitor.
-/// 6) Disconnect/connect blocks on the ChannelManager.
-/// 7) Register the new ChannelManager with your ChainWatchInterface (this does not happen
-///    automatically as it does in ChannelManager::new()).
-pub struct ChannelManagerReadArgs<'a> {
-       /// The keys provider which will give us relevant keys. Some keys will be loaded during
-       /// deserialization.
-       pub keys_manager: Arc<KeysInterface>,
-
-       /// The fee_estimator for use in the ChannelManager in the future.
-       ///
-       /// No calls to the FeeEstimator will be made during deserialization.
-       pub fee_estimator: Arc<FeeEstimator>,
-       /// The ManyChannelMonitor for use in the ChannelManager in the future.
-       ///
-       /// No calls to the ManyChannelMonitor will be made during deserialization. It is assumed that
-       /// you have deserialized ChannelMonitors separately and will add them to your
-       /// ManyChannelMonitor after deserializing this ChannelManager.
-       pub monitor: Arc<ManyChannelMonitor>,
-       /// The ChainWatchInterface for use in the ChannelManager in the future.
-       ///
-       /// No calls to the ChainWatchInterface will be made during deserialization.
-       pub chain_monitor: Arc<ChainWatchInterface>,
-       /// The BroadcasterInterface which will be used in the ChannelManager in the future and may be
-       /// used to broadcast the latest local commitment transactions of channels which must be
-       /// force-closed during deserialization.
-       pub tx_broadcaster: Arc<BroadcasterInterface>,
-       /// The Logger for use in the ChannelManager and which may be used to log information during
-       /// deserialization.
-       pub logger: Arc<Logger>,
-       /// Default settings used for new channels. Any existing channels will continue to use the
-       /// runtime settings which were stored when the ChannelManager was serialized.
-       pub default_config: UserConfig,
-
-       /// A map from channel funding outpoints to ChannelMonitors for those channels (ie
-       /// value.get_funding_txo() should be the key).
-       ///
-       /// If a monitor is inconsistent with the channel state during deserialization the channel will
-       /// be force-closed using the data in the ChannelMonitor and the channel will be dropped. This
-       /// is true for missing channels as well. If there is a monitor missing for which we find
-       /// channel data Err(DecodeError::InvalidValue) will be returned.
-       ///
-       /// In such cases the latest local transactions will be sent to the tx_broadcaster included in
-       /// this struct.
-       pub channel_monitors: &'a HashMap<OutPoint, &'a ChannelMonitor>,
-}
-
-impl<'a, R : ::std::io::Read> ReadableArgs<R, ChannelManagerReadArgs<'a>> for (Sha256dHash, ChannelManager) {
-       fn read(reader: &mut R, args: ChannelManagerReadArgs<'a>) -> Result<Self, DecodeError> {
-               let _ver: u8 = Readable::read(reader)?;
-               let min_ver: u8 = Readable::read(reader)?;
-               if min_ver > SERIALIZATION_VERSION {
-                       return Err(DecodeError::UnknownVersion);
-               }
-
-               let genesis_hash: Sha256dHash = Readable::read(reader)?;
-               let latest_block_height: u32 = Readable::read(reader)?;
-               let last_block_hash: Sha256dHash = Readable::read(reader)?;
-
-               let mut closed_channels = Vec::new();
-
-               let channel_count: u64 = Readable::read(reader)?;
-               let mut funding_txo_set = HashSet::with_capacity(cmp::min(channel_count as usize, 128));
-               let mut by_id = HashMap::with_capacity(cmp::min(channel_count as usize, 128));
-               let mut short_to_id = HashMap::with_capacity(cmp::min(channel_count as usize, 128));
-               for _ in 0..channel_count {
-                       let mut channel: Channel = ReadableArgs::read(reader, args.logger.clone())?;
-                       if channel.last_block_connected != last_block_hash {
-                               return Err(DecodeError::InvalidValue);
-                       }
-
-                       let funding_txo = channel.channel_monitor().get_funding_txo().ok_or(DecodeError::InvalidValue)?;
-                       funding_txo_set.insert(funding_txo.clone());
-                       if let Some(monitor) = args.channel_monitors.get(&funding_txo) {
-                               if channel.get_cur_local_commitment_transaction_number() != monitor.get_cur_local_commitment_number() ||
-                                               channel.get_revoked_remote_commitment_transaction_number() != monitor.get_min_seen_secret() ||
-                                               channel.get_cur_remote_commitment_transaction_number() != monitor.get_cur_remote_commitment_number() {
-                                       let mut force_close_res = channel.force_shutdown();
-                                       force_close_res.0 = monitor.get_latest_local_commitment_txn();
-                                       closed_channels.push(force_close_res);
-                               } else {
-                                       if let Some(short_channel_id) = channel.get_short_channel_id() {
-                                               short_to_id.insert(short_channel_id, channel.channel_id());
-                                       }
-                                       by_id.insert(channel.channel_id(), channel);
-                               }
-                       } else {
-                               return Err(DecodeError::InvalidValue);
-                       }
-               }
-
-               for (ref funding_txo, ref monitor) in args.channel_monitors.iter() {
-                       if !funding_txo_set.contains(funding_txo) {
-                               closed_channels.push((monitor.get_latest_local_commitment_txn(), Vec::new()));
-                       }
-               }
-
-               let forward_htlcs_count: u64 = Readable::read(reader)?;
-               let mut forward_htlcs = HashMap::with_capacity(cmp::min(forward_htlcs_count as usize, 128));
-               for _ in 0..forward_htlcs_count {
-                       let short_channel_id = Readable::read(reader)?;
-                       let pending_forwards_count: u64 = Readable::read(reader)?;
-                       let mut pending_forwards = Vec::with_capacity(cmp::min(pending_forwards_count as usize, 128));
-                       for _ in 0..pending_forwards_count {
-                               pending_forwards.push(Readable::read(reader)?);
-                       }
-                       forward_htlcs.insert(short_channel_id, pending_forwards);
-               }
-
-               let claimable_htlcs_count: u64 = Readable::read(reader)?;
-               let mut claimable_htlcs = HashMap::with_capacity(cmp::min(claimable_htlcs_count as usize, 128));
-               for _ in 0..claimable_htlcs_count {
-                       let payment_hash = Readable::read(reader)?;
-                       let previous_hops_len: u64 = Readable::read(reader)?;
-                       let mut previous_hops = Vec::with_capacity(cmp::min(previous_hops_len as usize, 2));
-                       for _ in 0..previous_hops_len {
-                               previous_hops.push((Readable::read(reader)?, Readable::read(reader)?));
-                       }
-                       claimable_htlcs.insert(payment_hash, previous_hops);
-               }
-
-               let channel_manager = ChannelManager {
-                       genesis_hash,
-                       fee_estimator: args.fee_estimator,
-                       monitor: args.monitor,
-                       chain_monitor: args.chain_monitor,
-                       tx_broadcaster: args.tx_broadcaster,
-
-                       latest_block_height: AtomicUsize::new(latest_block_height as usize),
-                       last_block_hash: Mutex::new(last_block_hash),
-                       secp_ctx: Secp256k1::new(),
-
-                       channel_state: Mutex::new(ChannelHolder {
-                               by_id,
-                               short_to_id,
-                               forward_htlcs,
-                               claimable_htlcs,
-                               pending_msg_events: Vec::new(),
-                       }),
-                       our_network_key: args.keys_manager.get_node_secret(),
-
-                       pending_events: Mutex::new(Vec::new()),
-                       total_consistency_lock: RwLock::new(()),
-                       keys_manager: args.keys_manager,
-                       logger: args.logger,
-                       default_configuration: args.default_config,
-               };
-
-               for close_res in closed_channels.drain(..) {
-                       channel_manager.finish_force_close_channel(close_res);
-                       //TODO: Broadcast channel update for closed channels, but only after we've made a
-                       //connection or two.
-               }
-
-               Ok((last_block_hash.clone(), channel_manager))
-       }
-}
diff --git a/src/ln/channelmonitor.rs b/src/ln/channelmonitor.rs
deleted file mode 100644 (file)
index 5bcd74f..0000000
+++ /dev/null
@@ -1,3415 +0,0 @@
-//! The logic to monitor for on-chain transactions and create the relevant claim responses lives
-//! here.
-//!
-//! ChannelMonitor objects are generated by ChannelManager in response to relevant
-//! messages/actions, and MUST be persisted to disk (and, preferably, remotely) before progress can
-//! be made in responding to certain messages, see ManyChannelMonitor for more.
-//!
-//! Note that ChannelMonitors are an important part of the lightning trust model and a copy of the
-//! latest ChannelMonitor must always be actively monitoring for chain updates (and no out-of-date
-//! ChannelMonitors should do so). Thus, if you're building rust-lightning into an HSM or other
-//! security-domain-separated system design, you should consider having multiple paths for
-//! ChannelMonitors to get out of the HSM and onto monitoring devices.
-
-use bitcoin::blockdata::block::BlockHeader;
-use bitcoin::blockdata::transaction::{TxIn,TxOut,SigHashType,Transaction};
-use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
-use bitcoin::blockdata::script::{Script, Builder};
-use bitcoin::blockdata::opcodes;
-use bitcoin::consensus::encode::{self, Decodable, Encodable};
-use bitcoin::util::hash::BitcoinHash;
-use bitcoin::util::bip143;
-
-use bitcoin_hashes::Hash;
-use bitcoin_hashes::sha256::Hash as Sha256;
-use bitcoin_hashes::hash160::Hash as Hash160;
-use bitcoin_hashes::sha256d::Hash as Sha256dHash;
-
-use secp256k1::{Secp256k1,Signature};
-use secp256k1::key::{SecretKey,PublicKey};
-use secp256k1;
-
-use ln::msgs::DecodeError;
-use ln::chan_utils;
-use ln::chan_utils::HTLCOutputInCommitment;
-use ln::channelmanager::{HTLCSource, PaymentPreimage, PaymentHash};
-use ln::channel::{ACCEPTED_HTLC_SCRIPT_WEIGHT, OFFERED_HTLC_SCRIPT_WEIGHT};
-use chain::chaininterface::{ChainListener, ChainWatchInterface, BroadcasterInterface, FeeEstimator, ConfirmationTarget};
-use chain::transaction::OutPoint;
-use chain::keysinterface::SpendableOutputDescriptor;
-use util::logger::Logger;
-use util::ser::{ReadableArgs, Readable, Writer, Writeable, WriterWriteAdaptor, U48};
-use util::{byte_utils, events};
-
-use std::collections::{HashMap, hash_map};
-use std::sync::{Arc,Mutex};
-use std::{hash,cmp, mem};
-
-/// An error enum representing a failure to persist a channel monitor update.
-#[derive(Clone)]
-pub enum ChannelMonitorUpdateErr {
-       /// Used to indicate a temporary failure (eg connection to a watchtower or remote backup of
-       /// our state failed, but is expected to succeed at some point in the future).
-       ///
-       /// Such a failure will "freeze" a channel, preventing us from revoking old states or
-       /// submitting new commitment transactions to the remote party.
-       /// ChannelManager::test_restore_channel_monitor can be used to retry the update(s) and restore
-       /// the channel to an operational state.
-       ///
-       /// Note that continuing to operate when no copy of the updated ChannelMonitor could be
-       /// persisted is unsafe - if you failed to store the update on your own local disk you should
-       /// instead return PermanentFailure to force closure of the channel ASAP.
-       ///
-       /// Even when a channel has been "frozen" updates to the ChannelMonitor can continue to occur
-       /// (eg if an inbound HTLC which we forwarded was claimed upstream resulting in us attempting
-       /// to claim it on this channel) and those updates must be applied wherever they can be. At
-       /// least one such updated ChannelMonitor must be persisted otherwise PermanentFailure should
-       /// be returned to get things on-chain ASAP using only the in-memory copy. Obviously updates to
-       /// the channel which would invalidate previous ChannelMonitors are not made when a channel has
-       /// been "frozen".
-       ///
-       /// Note that even if updates made after TemporaryFailure succeed you must still call
-       /// test_restore_channel_monitor to ensure you have the latest monitor and re-enable normal
-       /// channel operation.
-       ///
-       /// For deployments where a copy of ChannelMonitors and other local state are backed up in a
-       /// remote location (with local copies persisted immediately), it is anticipated that all
-       /// updates will return TemporaryFailure until the remote copies could be updated.
-       TemporaryFailure,
-       /// Used to indicate no further channel monitor updates will be allowed (eg we've moved on to a
-       /// different watchtower and cannot update with all watchtowers that were previously informed
-       /// of this channel). This will force-close the channel in question.
-       ///
-       /// Should also be used to indicate a failure to update the local copy of the channel monitor.
-       PermanentFailure,
-}
-
-/// General Err type for ChannelMonitor actions. Generally, this implies that the data provided is
-/// inconsistent with the ChannelMonitor being called. eg for ChannelMonitor::insert_combine this
-/// means you tried to merge two monitors for different channels or for a channel which was
-/// restored from a backup and then generated new commitment updates.
-/// Contains a human-readable error message.
-#[derive(Debug)]
-pub struct MonitorUpdateError(pub &'static str);
-
-/// Simple structure send back by ManyChannelMonitor in case of HTLC detected onchain from a
-/// forward channel and from which info are needed to update HTLC in a backward channel.
-pub struct HTLCUpdate {
-       pub(super) payment_hash: PaymentHash,
-       pub(super) payment_preimage: Option<PaymentPreimage>,
-       pub(super) source: HTLCSource
-}
-
-/// Simple trait indicating ability to track a set of ChannelMonitors and multiplex events between
-/// them. Generally should be implemented by keeping a local SimpleManyChannelMonitor and passing
-/// events to it, while also taking any add_update_monitor events and passing them to some remote
-/// server(s).
-///
-/// Note that any updates to a channel's monitor *must* be applied to each instance of the
-/// channel's monitor everywhere (including remote watchtowers) *before* this function returns. If
-/// an update occurs and a remote watchtower is left with old state, it may broadcast transactions
-/// which we have revoked, allowing our counterparty to claim all funds in the channel!
-pub trait ManyChannelMonitor: Send + Sync {
-       /// Adds or updates a monitor for the given `funding_txo`.
-       ///
-       /// Implementor must also ensure that the funding_txo outpoint is registered with any relevant
-       /// ChainWatchInterfaces such that the provided monitor receives block_connected callbacks with
-       /// any spends of it.
-       fn add_update_monitor(&self, funding_txo: OutPoint, monitor: ChannelMonitor) -> Result<(), ChannelMonitorUpdateErr>;
-
-       /// Used by ChannelManager to get list of HTLC resolved onchain and which needed to be updated
-       /// with success or failure backward
-       fn fetch_pending_htlc_updated(&self) -> Vec<HTLCUpdate>;
-}
-
-/// A simple implementation of a ManyChannelMonitor and ChainListener. Can be used to create a
-/// watchtower or watch our own channels.
-///
-/// Note that you must provide your own key by which to refer to channels.
-///
-/// If you're accepting remote monitors (ie are implementing a watchtower), you must verify that
-/// users cannot overwrite a given channel by providing a duplicate key. ie you should probably
-/// index by a PublicKey which is required to sign any updates.
-///
-/// If you're using this for local monitoring of your own channels, you probably want to use
-/// `OutPoint` as the key, which will give you a ManyChannelMonitor implementation.
-pub struct SimpleManyChannelMonitor<Key> {
-       #[cfg(test)] // Used in ChannelManager tests to manipulate channels directly
-       pub monitors: Mutex<HashMap<Key, ChannelMonitor>>,
-       #[cfg(not(test))]
-       monitors: Mutex<HashMap<Key, ChannelMonitor>>,
-       chain_monitor: Arc<ChainWatchInterface>,
-       broadcaster: Arc<BroadcasterInterface>,
-       pending_events: Mutex<Vec<events::Event>>,
-       pending_htlc_updated: Mutex<HashMap<PaymentHash, Vec<(HTLCSource, Option<PaymentPreimage>)>>>,
-       logger: Arc<Logger>,
-       fee_estimator: Arc<FeeEstimator>
-}
-
-impl<Key : Send + cmp::Eq + hash::Hash> ChainListener for SimpleManyChannelMonitor<Key> {
-       fn block_connected(&self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], _indexes_of_txn_matched: &[u32]) {
-               let block_hash = header.bitcoin_hash();
-               let mut new_events: Vec<events::Event> = Vec::with_capacity(0);
-               let mut htlc_updated_infos = Vec::new();
-               {
-                       let mut monitors = self.monitors.lock().unwrap();
-                       for monitor in monitors.values_mut() {
-                               let (txn_outputs, spendable_outputs, mut htlc_updated) = monitor.block_connected(txn_matched, height, &block_hash, &*self.broadcaster, &*self.fee_estimator);
-                               if spendable_outputs.len() > 0 {
-                                       new_events.push(events::Event::SpendableOutputs {
-                                               outputs: spendable_outputs,
-                                       });
-                               }
-
-                               for (ref txid, ref outputs) in txn_outputs {
-                                       for (idx, output) in outputs.iter().enumerate() {
-                                               self.chain_monitor.install_watch_outpoint((txid.clone(), idx as u32), &output.script_pubkey);
-                                       }
-                               }
-                               htlc_updated_infos.append(&mut htlc_updated);
-                       }
-               }
-               {
-                       // ChannelManager will just need to fetch pending_htlc_updated and pass state backward
-                       let mut pending_htlc_updated = self.pending_htlc_updated.lock().unwrap();
-                       for htlc in htlc_updated_infos.drain(..) {
-                               match pending_htlc_updated.entry(htlc.2) {
-                                       hash_map::Entry::Occupied(mut e) => {
-                                               // In case of reorg we may have htlc outputs solved in a different way so
-                                               // we prefer to keep claims but don't store duplicate updates for a given
-                                               // (payment_hash, HTLCSource) pair.
-                                               let mut existing_claim = false;
-                                               e.get_mut().retain(|htlc_data| {
-                                                       if htlc.0 == htlc_data.0 {
-                                                               if htlc_data.1.is_some() {
-                                                                       existing_claim = true;
-                                                                       true
-                                                               } else { false }
-                                                       } else { true }
-                                               });
-                                               if !existing_claim {
-                                                       e.get_mut().push((htlc.0, htlc.1));
-                                               }
-                                       }
-                                       hash_map::Entry::Vacant(e) => {
-                                               e.insert(vec![(htlc.0, htlc.1)]);
-                                       }
-                               }
-                       }
-               }
-               let mut pending_events = self.pending_events.lock().unwrap();
-               pending_events.append(&mut new_events);
-       }
-
-       fn block_disconnected(&self, header: &BlockHeader, disconnected_height: u32) {
-               let block_hash = header.bitcoin_hash();
-               let mut monitors = self.monitors.lock().unwrap();
-               for monitor in monitors.values_mut() {
-                       monitor.block_disconnected(disconnected_height, &block_hash);
-               }
-       }
-}
-
-impl<Key : Send + cmp::Eq + hash::Hash + 'static> SimpleManyChannelMonitor<Key> {
-       /// Creates a new object which can be used to monitor several channels given the chain
-       /// interface with which to register to receive notifications.
-       pub fn new(chain_monitor: Arc<ChainWatchInterface>, broadcaster: Arc<BroadcasterInterface>, logger: Arc<Logger>, feeest: Arc<FeeEstimator>) -> Arc<SimpleManyChannelMonitor<Key>> {
-               let res = Arc::new(SimpleManyChannelMonitor {
-                       monitors: Mutex::new(HashMap::new()),
-                       chain_monitor,
-                       broadcaster,
-                       pending_events: Mutex::new(Vec::new()),
-                       pending_htlc_updated: Mutex::new(HashMap::new()),
-                       logger,
-                       fee_estimator: feeest,
-               });
-               let weak_res = Arc::downgrade(&res);
-               res.chain_monitor.register_listener(weak_res);
-               res
-       }
-
-       /// Adds or updates the monitor which monitors the channel referred to by the given key.
-       pub fn add_update_monitor_by_key(&self, key: Key, monitor: ChannelMonitor) -> Result<(), MonitorUpdateError> {
-               let mut monitors = self.monitors.lock().unwrap();
-               match monitors.get_mut(&key) {
-                       Some(orig_monitor) => {
-                               log_trace!(self, "Updating Channel Monitor for channel {}", log_funding_info!(monitor.key_storage));
-                               return orig_monitor.insert_combine(monitor);
-                       },
-                       None => {}
-               };
-               match monitor.key_storage {
-                       Storage::Local { ref funding_info, .. } => {
-                               match funding_info {
-                                       &None => {
-                                               return Err(MonitorUpdateError("Try to update a useless monitor without funding_txo !"));
-                                       },
-                                       &Some((ref outpoint, ref script)) => {
-                                               log_trace!(self, "Got new Channel Monitor for channel {}", log_bytes!(outpoint.to_channel_id()[..]));
-                                               self.chain_monitor.install_watch_tx(&outpoint.txid, script);
-                                               self.chain_monitor.install_watch_outpoint((outpoint.txid, outpoint.index as u32), script);
-                                       },
-                               }
-                       },
-                       Storage::Watchtower { .. } => {
-                               self.chain_monitor.watch_all_txn();
-                       }
-               }
-               monitors.insert(key, monitor);
-               Ok(())
-       }
-}
-
-impl ManyChannelMonitor for SimpleManyChannelMonitor<OutPoint> {
-       fn add_update_monitor(&self, funding_txo: OutPoint, monitor: ChannelMonitor) -> Result<(), ChannelMonitorUpdateErr> {
-               match self.add_update_monitor_by_key(funding_txo, monitor) {
-                       Ok(_) => Ok(()),
-                       Err(_) => Err(ChannelMonitorUpdateErr::PermanentFailure),
-               }
-       }
-
-       fn fetch_pending_htlc_updated(&self) -> Vec<HTLCUpdate> {
-               let mut updated = self.pending_htlc_updated.lock().unwrap();
-               let mut pending_htlcs_updated = Vec::with_capacity(updated.len());
-               for (k, v) in updated.drain() {
-                       for htlc_data in v {
-                               pending_htlcs_updated.push(HTLCUpdate {
-                                       payment_hash: k,
-                                       payment_preimage: htlc_data.1,
-                                       source: htlc_data.0,
-                               });
-                       }
-               }
-               pending_htlcs_updated
-       }
-}
-
-impl<Key : Send + cmp::Eq + hash::Hash> events::EventsProvider for SimpleManyChannelMonitor<Key> {
-       fn get_and_clear_pending_events(&self) -> Vec<events::Event> {
-               let mut pending_events = self.pending_events.lock().unwrap();
-               let mut ret = Vec::new();
-               mem::swap(&mut ret, &mut *pending_events);
-               ret
-       }
-}
-
-/// If an HTLC expires within this many blocks, don't try to claim it in a shared transaction,
-/// instead claiming it in its own individual transaction.
-const CLTV_SHARED_CLAIM_BUFFER: u32 = 12;
-/// If an HTLC expires within this many blocks, force-close the channel to broadcast the
-/// HTLC-Success transaction.
-/// In other words, this is an upper bound on how many blocks we think it can take us to get a
-/// transaction confirmed (and we use it in a few more, equivalent, places).
-pub(crate) const CLTV_CLAIM_BUFFER: u32 = 6;
-/// Number of blocks by which point we expect our counterparty to have seen new blocks on the
-/// network and done a full update_fail_htlc/commitment_signed dance (+ we've updated all our
-/// copies of ChannelMonitors, including watchtowers). We could enforce the contract by failing
-/// at CLTV expiration height but giving a grace period to our peer may be profitable for us if he
-/// can provide an over-late preimage. Nevertheless, grace period has to be accounted in our
-/// CLTV_EXPIRY_DELTA to be secure. Following this policy we may decrease the rate of channel failures
-/// due to expiration but increase the cost of funds being locked longuer in case of failure.
-/// This delay also cover a low-power peer being slow to process blocks and so being behind us on
-/// accurate block height.
-/// In case of onchain failure to be pass backward we may see the last block of ANTI_REORG_DELAY
-/// with at worst this delay, so we are not only using this value as a mercy for them but also
-/// us as a safeguard to delay with enough time.
-pub(crate) const LATENCY_GRACE_PERIOD_BLOCKS: u32 = 3;
-/// Number of blocks we wait on seeing a HTLC output being solved before we fail corresponding inbound
-/// HTLCs. This prevents us from failing backwards and then getting a reorg resulting in us losing money.
-/// We use also this delay to be sure we can remove our in-flight claim txn from bump candidates buffer.
-/// It may cause spurrious generation of bumped claim txn but that's allright given the outpoint is already
-/// solved by a previous claim tx. What we want to avoid is reorg evicting our claim tx and us not
-/// keeping bumping another claim tx to solve the outpoint.
-pub(crate) const ANTI_REORG_DELAY: u32 = 6;
-
-#[derive(Clone, PartialEq)]
-enum Storage {
-       Local {
-               revocation_base_key: SecretKey,
-               htlc_base_key: SecretKey,
-               delayed_payment_base_key: SecretKey,
-               payment_base_key: SecretKey,
-               shutdown_pubkey: PublicKey,
-               prev_latest_per_commitment_point: Option<PublicKey>,
-               latest_per_commitment_point: Option<PublicKey>,
-               funding_info: Option<(OutPoint, Script)>,
-               current_remote_commitment_txid: Option<Sha256dHash>,
-               prev_remote_commitment_txid: Option<Sha256dHash>,
-       },
-       Watchtower {
-               revocation_base_key: PublicKey,
-               htlc_base_key: PublicKey,
-       }
-}
-
-#[derive(Clone, PartialEq)]
-struct LocalSignedTx {
-       /// txid of the transaction in tx, just used to make comparison faster
-       txid: Sha256dHash,
-       tx: Transaction,
-       revocation_key: PublicKey,
-       a_htlc_key: PublicKey,
-       b_htlc_key: PublicKey,
-       delayed_payment_key: PublicKey,
-       feerate_per_kw: u64,
-       htlc_outputs: Vec<(HTLCOutputInCommitment, Option<(Signature, Signature)>, Option<HTLCSource>)>,
-}
-
-#[derive(PartialEq)]
-enum InputDescriptors {
-       RevokedOfferedHTLC,
-       RevokedReceivedHTLC,
-       OfferedHTLC,
-       ReceivedHTLC,
-       RevokedOutput, // either a revoked to_local output on commitment tx, a revoked HTLC-Timeout output or a revoked HTLC-Success output
-}
-
-/// When ChannelMonitor discovers an onchain outpoint being a step of a channel and that it needs
-/// to generate a tx to push channel state forward, we cache outpoint-solving tx material to build
-/// a new bumped one in case of lenghty confirmation delay
-#[derive(Clone, PartialEq)]
-enum TxMaterial {
-       Revoked {
-               script: Script,
-               pubkey: Option<PublicKey>,
-               key: SecretKey,
-               is_htlc: bool,
-               amount: u64,
-       },
-       RemoteHTLC {
-               script: Script,
-               key: SecretKey,
-               preimage: Option<PaymentPreimage>,
-               amount: u64,
-       },
-       LocalHTLC {
-               script: Script,
-               sigs: (Signature, Signature),
-               preimage: Option<PaymentPreimage>,
-               amount: u64,
-       }
-}
-
-/// Upon discovering of some classes of onchain tx by ChannelMonitor, we may have to take actions on it
-/// once they mature to enough confirmations (ANTI_REORG_DELAY)
-#[derive(Clone, PartialEq)]
-enum OnchainEvent {
-       /// Outpoint under claim process by our own tx, once this one get enough confirmations, we remove it from
-       /// bump-txn candidate buffer.
-       Claim {
-               outpoint: BitcoinOutPoint,
-       },
-       /// HTLC output getting solved by a timeout, at maturation we pass upstream payment source information to solve
-       /// inbound HTLC in backward channel. Note, in case of preimage, we pass info to upstream without delay as we can
-       /// only win from it, so it's never an OnchainEvent
-       HTLCUpdate {
-               htlc_update: (HTLCSource, PaymentHash),
-       },
-}
-
-const SERIALIZATION_VERSION: u8 = 1;
-const MIN_SERIALIZATION_VERSION: u8 = 1;
-
-/// A ChannelMonitor handles chain events (blocks connected and disconnected) and generates
-/// on-chain transactions to ensure no loss of funds occurs.
-///
-/// You MUST ensure that no ChannelMonitors for a given channel anywhere contain out-of-date
-/// information and are actively monitoring the chain.
-#[derive(Clone)]
-pub struct ChannelMonitor {
-       commitment_transaction_number_obscure_factor: u64,
-
-       key_storage: Storage,
-       their_htlc_base_key: Option<PublicKey>,
-       their_delayed_payment_base_key: Option<PublicKey>,
-       // first is the idx of the first of the two revocation points
-       their_cur_revocation_points: Option<(u64, PublicKey, Option<PublicKey>)>,
-
-       our_to_self_delay: u16,
-       their_to_self_delay: Option<u16>,
-
-       old_secrets: [([u8; 32], u64); 49],
-       remote_claimable_outpoints: HashMap<Sha256dHash, Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)>>,
-       /// We cannot identify HTLC-Success or HTLC-Timeout transactions by themselves on the chain.
-       /// Nor can we figure out their commitment numbers without the commitment transaction they are
-       /// spending. Thus, in order to claim them via revocation key, we track all the remote
-       /// commitment transactions which we find on-chain, mapping them to the commitment number which
-       /// can be used to derive the revocation key and claim the transactions.
-       remote_commitment_txn_on_chain: HashMap<Sha256dHash, (u64, Vec<Script>)>,
-       /// Cache used to make pruning of payment_preimages faster.
-       /// Maps payment_hash values to commitment numbers for remote transactions for non-revoked
-       /// remote transactions (ie should remain pretty small).
-       /// Serialized to disk but should generally not be sent to Watchtowers.
-       remote_hash_commitment_number: HashMap<PaymentHash, u64>,
-
-       // We store two local commitment transactions to avoid any race conditions where we may update
-       // some monitors (potentially on watchtowers) but then fail to update others, resulting in the
-       // various monitors for one channel being out of sync, and us broadcasting a local
-       // transaction for which we have deleted claim information on some watchtowers.
-       prev_local_signed_commitment_tx: Option<LocalSignedTx>,
-       current_local_signed_commitment_tx: Option<LocalSignedTx>,
-
-       // Used just for ChannelManager to make sure it has the latest channel data during
-       // deserialization
-       current_remote_commitment_number: u64,
-
-       payment_preimages: HashMap<PaymentHash, PaymentPreimage>,
-
-       destination_script: Script,
-       // Thanks to data loss protection, we may be able to claim our non-htlc funds
-       // back, this is the script we have to spend from but we need to
-       // scan every commitment transaction for that
-       to_remote_rescue: Option<(Script, SecretKey)>,
-
-       // Used to track outpoint in the process of being claimed by our transactions. We need to scan all transactions
-       // for inputs spending this. If height timer (u32) is expired and claim tx hasn't reached enough confirmations
-       // before, use TxMaterial to regenerate a new claim tx with a satoshis-per-1000-weight-units higher than last
-       // one (u64), if timelock expiration (u32) is near, decrease height timer, the in-between bumps delay.
-       // Last field cached (u32) is height of outpoint confirmation, which is needed to flush this tracker
-       // in case of reorgs, given block timer are scaled on timer expiration we can't deduce from it original height.
-       our_claim_txn_waiting_first_conf: HashMap<BitcoinOutPoint, (u32, TxMaterial, u64, u32, u32)>,
-
-       // Used to track onchain events, i.e transactions parts of channels confirmed on chain, on which
-       // we have to take actions once they reach enough confs. Key is a block height timer, i.e we enforce
-       // actions when we receive a block with given height. Actions depend on OnchainEvent type.
-       onchain_events_waiting_threshold_conf: HashMap<u32, Vec<OnchainEvent>>,
-
-       // We simply modify last_block_hash in Channel's block_connected so that serialization is
-       // consistent but hopefully the users' copy handles block_connected in a consistent way.
-       // (we do *not*, however, update them in insert_combine to ensure any local user copies keep
-       // their last_block_hash from its state and not based on updated copies that didn't run through
-       // the full block_connected).
-       pub(crate) last_block_hash: Sha256dHash,
-       secp_ctx: Secp256k1<secp256k1::All>, //TODO: dedup this a bit...
-       logger: Arc<Logger>,
-}
-
-macro_rules! subtract_high_prio_fee {
-       ($self: ident, $fee_estimator: expr, $value: expr, $predicted_weight: expr, $spent_txid: expr, $used_feerate: expr) => {
-               {
-                       $used_feerate = $fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::HighPriority);
-                       let mut fee = $used_feerate * ($predicted_weight as u64) / 1000;
-                       if $value <= fee {
-                               $used_feerate = $fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
-                               fee = $used_feerate * ($predicted_weight as u64) / 1000;
-                               if $value <= fee {
-                                       $used_feerate = $fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background);
-                                       fee = $used_feerate * ($predicted_weight as u64) / 1000;
-                                       if $value <= fee {
-                                               log_error!($self, "Failed to generate an on-chain punishment tx spending {} as even low priority fee ({} sat) was more than the entire claim balance ({} sat)",
-                                                       $spent_txid, fee, $value);
-                                               false
-                                       } else {
-                                               log_warn!($self, "Used low priority fee for on-chain punishment tx spending {} as high priority fee was more than the entire claim balance ({} sat)",
-                                                       $spent_txid, $value);
-                                               $value -= fee;
-                                               true
-                                       }
-                               } else {
-                                       log_warn!($self, "Used medium priority fee for on-chain punishment tx spending {} as high priority fee was more than the entire claim balance ({} sat)",
-                                               $spent_txid, $value);
-                                       $value -= fee;
-                                       true
-                               }
-                       } else {
-                               $value -= fee;
-                               true
-                       }
-               }
-       }
-}
-
-#[cfg(any(test, feature = "fuzztarget"))]
-/// Used only in testing and fuzztarget to check serialization roundtrips don't change the
-/// underlying object
-impl PartialEq for ChannelMonitor {
-       fn eq(&self, other: &Self) -> bool {
-               if self.commitment_transaction_number_obscure_factor != other.commitment_transaction_number_obscure_factor ||
-                       self.key_storage != other.key_storage ||
-                       self.their_htlc_base_key != other.their_htlc_base_key ||
-                       self.their_delayed_payment_base_key != other.their_delayed_payment_base_key ||
-                       self.their_cur_revocation_points != other.their_cur_revocation_points ||
-                       self.our_to_self_delay != other.our_to_self_delay ||
-                       self.their_to_self_delay != other.their_to_self_delay ||
-                       self.remote_claimable_outpoints != other.remote_claimable_outpoints ||
-                       self.remote_commitment_txn_on_chain != other.remote_commitment_txn_on_chain ||
-                       self.remote_hash_commitment_number != other.remote_hash_commitment_number ||
-                       self.prev_local_signed_commitment_tx != other.prev_local_signed_commitment_tx ||
-                       self.current_remote_commitment_number != other.current_remote_commitment_number ||
-                       self.current_local_signed_commitment_tx != other.current_local_signed_commitment_tx ||
-                       self.payment_preimages != other.payment_preimages ||
-                       self.destination_script != other.destination_script ||
-                       self.to_remote_rescue != other.to_remote_rescue ||
-                       self.our_claim_txn_waiting_first_conf != other.our_claim_txn_waiting_first_conf ||
-                       self.onchain_events_waiting_threshold_conf != other.onchain_events_waiting_threshold_conf
-               {
-                       false
-               } else {
-                       for (&(ref secret, ref idx), &(ref o_secret, ref o_idx)) in self.old_secrets.iter().zip(other.old_secrets.iter()) {
-                               if secret != o_secret || idx != o_idx {
-                                       return false
-                               }
-                       }
-                       true
-               }
-       }
-}
-
-impl ChannelMonitor {
-       pub(super) fn new(revocation_base_key: &SecretKey, delayed_payment_base_key: &SecretKey, htlc_base_key: &SecretKey, payment_base_key: &SecretKey, shutdown_pubkey: &PublicKey, our_to_self_delay: u16, destination_script: Script, logger: Arc<Logger>) -> ChannelMonitor {
-               ChannelMonitor {
-                       commitment_transaction_number_obscure_factor: 0,
-
-                       key_storage: Storage::Local {
-                               revocation_base_key: revocation_base_key.clone(),
-                               htlc_base_key: htlc_base_key.clone(),
-                               delayed_payment_base_key: delayed_payment_base_key.clone(),
-                               payment_base_key: payment_base_key.clone(),
-                               shutdown_pubkey: shutdown_pubkey.clone(),
-                               prev_latest_per_commitment_point: None,
-                               latest_per_commitment_point: None,
-                               funding_info: None,
-                               current_remote_commitment_txid: None,
-                               prev_remote_commitment_txid: None,
-                       },
-                       their_htlc_base_key: None,
-                       their_delayed_payment_base_key: None,
-                       their_cur_revocation_points: None,
-
-                       our_to_self_delay: our_to_self_delay,
-                       their_to_self_delay: None,
-
-                       old_secrets: [([0; 32], 1 << 48); 49],
-                       remote_claimable_outpoints: HashMap::new(),
-                       remote_commitment_txn_on_chain: HashMap::new(),
-                       remote_hash_commitment_number: HashMap::new(),
-
-                       prev_local_signed_commitment_tx: None,
-                       current_local_signed_commitment_tx: None,
-                       current_remote_commitment_number: 1 << 48,
-
-                       payment_preimages: HashMap::new(),
-                       destination_script: destination_script,
-                       to_remote_rescue: None,
-
-                       our_claim_txn_waiting_first_conf: HashMap::new(),
-
-                       onchain_events_waiting_threshold_conf: HashMap::new(),
-
-                       last_block_hash: Default::default(),
-                       secp_ctx: Secp256k1::new(),
-                       logger,
-               }
-       }
-
-       fn get_witnesses_weight(inputs: &[InputDescriptors]) -> usize {
-               let mut tx_weight = 2; // count segwit flags
-               for inp in inputs {
-                       // We use expected weight (and not actual) as signatures and time lock delays may vary
-                       tx_weight +=  match inp {
-                               // number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
-                               &InputDescriptors::RevokedOfferedHTLC => {
-                                       1 + 1 + 73 + 1 + 33 + 1 + 133
-                               },
-                               // number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
-                               &InputDescriptors::RevokedReceivedHTLC => {
-                                       1 + 1 + 73 + 1 + 33 + 1 + 139
-                               },
-                               // number_of_witness_elements + sig_length + remotehtlc_sig  + preimage_length + preimage + witness_script_length + witness_script
-                               &InputDescriptors::OfferedHTLC => {
-                                       1 + 1 + 73 + 1 + 32 + 1 + 133
-                               },
-                               // number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
-                               &InputDescriptors::ReceivedHTLC => {
-                                       1 + 1 + 73 + 1 + 1 + 1 + 139
-                               },
-                               // number_of_witness_elements + sig_length + revocation_sig + true_length + op_true + witness_script_length + witness_script
-                               &InputDescriptors::RevokedOutput => {
-                                       1 + 1 + 73 + 1 + 1 + 1 + 77
-                               },
-                       };
-               }
-               tx_weight
-       }
-
-       fn get_height_timer(current_height: u32, timelock_expiration: u32) -> u32 {
-               if timelock_expiration <= current_height || timelock_expiration - current_height <= 3 {
-                       return current_height + 1
-               } else if timelock_expiration - current_height <= 15 {
-                       return current_height + 3
-               }
-               current_height + 15
-       }
-
-       #[inline]
-       fn place_secret(idx: u64) -> u8 {
-               for i in 0..48 {
-                       if idx & (1 << i) == (1 << i) {
-                               return i
-                       }
-               }
-               48
-       }
-
-       #[inline]
-       fn derive_secret(secret: [u8; 32], bits: u8, idx: u64) -> [u8; 32] {
-               let mut res: [u8; 32] = secret;
-               for i in 0..bits {
-                       let bitpos = bits - 1 - i;
-                       if idx & (1 << bitpos) == (1 << bitpos) {
-                               res[(bitpos / 8) as usize] ^= 1 << (bitpos & 7);
-                               res = Sha256::hash(&res).into_inner();
-                       }
-               }
-               res
-       }
-
-       /// Inserts a revocation secret into this channel monitor. Prunes old preimages if neither
-       /// needed by local commitment transactions HTCLs nor by remote ones. Unless we haven't already seen remote
-       /// commitment transaction's secret, they are de facto pruned (we can use revocation key).
-       pub(super) fn provide_secret(&mut self, idx: u64, secret: [u8; 32]) -> Result<(), MonitorUpdateError> {
-               let pos = ChannelMonitor::place_secret(idx);
-               for i in 0..pos {
-                       let (old_secret, old_idx) = self.old_secrets[i as usize];
-                       if ChannelMonitor::derive_secret(secret, pos, old_idx) != old_secret {
-                               return Err(MonitorUpdateError("Previous secret did not match new one"));
-                       }
-               }
-               if self.get_min_seen_secret() <= idx {
-                       return Ok(());
-               }
-               self.old_secrets[pos as usize] = (secret, idx);
-
-               // Prune HTLCs from the previous remote commitment tx so we don't generate failure/fulfill
-               // events for now-revoked/fulfilled HTLCs.
-               // TODO: We should probably consider whether we're really getting the next secret here.
-               if let Storage::Local { ref mut prev_remote_commitment_txid, .. } = self.key_storage {
-                       if let Some(txid) = prev_remote_commitment_txid.take() {
-                               for &mut (_, ref mut source) in self.remote_claimable_outpoints.get_mut(&txid).unwrap() {
-                                       *source = None;
-                               }
-                       }
-               }
-
-               if !self.payment_preimages.is_empty() {
-                       let local_signed_commitment_tx = self.current_local_signed_commitment_tx.as_ref().expect("Channel needs at least an initial commitment tx !");
-                       let prev_local_signed_commitment_tx = self.prev_local_signed_commitment_tx.as_ref();
-                       let min_idx = self.get_min_seen_secret();
-                       let remote_hash_commitment_number = &mut self.remote_hash_commitment_number;
-
-                       self.payment_preimages.retain(|&k, _| {
-                               for &(ref htlc, _, _) in &local_signed_commitment_tx.htlc_outputs {
-                                       if k == htlc.payment_hash {
-                                               return true
-                                       }
-                               }
-                               if let Some(prev_local_commitment_tx) = prev_local_signed_commitment_tx {
-                                       for &(ref htlc, _, _) in prev_local_commitment_tx.htlc_outputs.iter() {
-                                               if k == htlc.payment_hash {
-                                                       return true
-                                               }
-                                       }
-                               }
-                               let contains = if let Some(cn) = remote_hash_commitment_number.get(&k) {
-                                       if *cn < min_idx {
-                                               return true
-                                       }
-                                       true
-                               } else { false };
-                               if contains {
-                                       remote_hash_commitment_number.remove(&k);
-                               }
-                               false
-                       });
-               }
-
-               Ok(())
-       }
-
-       /// Informs this monitor of the latest remote (ie non-broadcastable) commitment transaction.
-       /// The monitor watches for it to be broadcasted and then uses the HTLC information (and
-       /// possibly future revocation/preimage information) to claim outputs where possible.
-       /// We cache also the mapping hash:commitment number to lighten pruning of old preimages by watchtowers.
-       pub(super) fn provide_latest_remote_commitment_tx_info(&mut self, unsigned_commitment_tx: &Transaction, htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)>, commitment_number: u64, their_revocation_point: PublicKey) {
-               // TODO: Encrypt the htlc_outputs data with the single-hash of the commitment transaction
-               // so that a remote monitor doesn't learn anything unless there is a malicious close.
-               // (only maybe, sadly we cant do the same for local info, as we need to be aware of
-               // timeouts)
-               for &(ref htlc, _) in &htlc_outputs {
-                       self.remote_hash_commitment_number.insert(htlc.payment_hash, commitment_number);
-               }
-
-               let new_txid = unsigned_commitment_tx.txid();
-               log_trace!(self, "Tracking new remote commitment transaction with txid {} at commitment number {} with {} HTLC outputs", new_txid, commitment_number, htlc_outputs.len());
-               log_trace!(self, "New potential remote commitment transaction: {}", encode::serialize_hex(unsigned_commitment_tx));
-               if let Storage::Local { ref mut current_remote_commitment_txid, ref mut prev_remote_commitment_txid, .. } = self.key_storage {
-                       *prev_remote_commitment_txid = current_remote_commitment_txid.take();
-                       *current_remote_commitment_txid = Some(new_txid);
-               }
-               self.remote_claimable_outpoints.insert(new_txid, htlc_outputs);
-               self.current_remote_commitment_number = commitment_number;
-               //TODO: Merge this into the other per-remote-transaction output storage stuff
-               match self.their_cur_revocation_points {
-                       Some(old_points) => {
-                               if old_points.0 == commitment_number + 1 {
-                                       self.their_cur_revocation_points = Some((old_points.0, old_points.1, Some(their_revocation_point)));
-                               } else if old_points.0 == commitment_number + 2 {
-                                       if let Some(old_second_point) = old_points.2 {
-                                               self.their_cur_revocation_points = Some((old_points.0 - 1, old_second_point, Some(their_revocation_point)));
-                                       } else {
-                                               self.their_cur_revocation_points = Some((commitment_number, their_revocation_point, None));
-                                       }
-                               } else {
-                                       self.their_cur_revocation_points = Some((commitment_number, their_revocation_point, None));
-                               }
-                       },
-                       None => {
-                               self.their_cur_revocation_points = Some((commitment_number, their_revocation_point, None));
-                       }
-               }
-       }
-
-       pub(super) fn provide_rescue_remote_commitment_tx_info(&mut self, their_revocation_point: PublicKey) {
-               match self.key_storage {
-                       Storage::Local { ref payment_base_key, .. } => {
-                               if let Ok(payment_key) = chan_utils::derive_public_key(&self.secp_ctx, &their_revocation_point, &PublicKey::from_secret_key(&self.secp_ctx, &payment_base_key)) {
-                                       let to_remote_script =  Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0)
-                                               .push_slice(&Hash160::hash(&payment_key.serialize())[..])
-                                               .into_script();
-                                       if let Ok(to_remote_key) = chan_utils::derive_private_key(&self.secp_ctx, &their_revocation_point, &payment_base_key) {
-                                               self.to_remote_rescue = Some((to_remote_script, to_remote_key));
-                                       }
-                               }
-                       },
-                       Storage::Watchtower { .. } => {}
-               }
-       }
-
-       /// Informs this monitor of the latest local (ie broadcastable) commitment transaction. The
-       /// monitor watches for timeouts and may broadcast it if we approach such a timeout. Thus, it
-       /// is important that any clones of this channel monitor (including remote clones) by kept
-       /// up-to-date as our local commitment transaction is updated.
-       /// Panics if set_their_to_self_delay has never been called.
-       /// Also update Storage with latest local per_commitment_point to derive local_delayedkey in
-       /// case of onchain HTLC tx
-       pub(super) fn provide_latest_local_commitment_tx_info(&mut self, signed_commitment_tx: Transaction, local_keys: chan_utils::TxCreationKeys, feerate_per_kw: u64, htlc_outputs: Vec<(HTLCOutputInCommitment, Option<(Signature, Signature)>, Option<HTLCSource>)>) {
-               assert!(self.their_to_self_delay.is_some());
-               self.prev_local_signed_commitment_tx = self.current_local_signed_commitment_tx.take();
-               self.current_local_signed_commitment_tx = Some(LocalSignedTx {
-                       txid: signed_commitment_tx.txid(),
-                       tx: signed_commitment_tx,
-                       revocation_key: local_keys.revocation_key,
-                       a_htlc_key: local_keys.a_htlc_key,
-                       b_htlc_key: local_keys.b_htlc_key,
-                       delayed_payment_key: local_keys.a_delayed_payment_key,
-                       feerate_per_kw,
-                       htlc_outputs,
-               });
-
-               if let Storage::Local { ref mut latest_per_commitment_point, .. } = self.key_storage {
-                       *latest_per_commitment_point = Some(local_keys.per_commitment_point);
-               } else {
-                       panic!("Channel somehow ended up with its internal ChannelMonitor being in Watchtower mode?");
-               }
-       }
-
-       /// Provides a payment_hash->payment_preimage mapping. Will be automatically pruned when all
-       /// commitment_tx_infos which contain the payment hash have been revoked.
-       pub(super) fn provide_payment_preimage(&mut self, payment_hash: &PaymentHash, payment_preimage: &PaymentPreimage) {
-               self.payment_preimages.insert(payment_hash.clone(), payment_preimage.clone());
-       }
-
-       /// Combines this ChannelMonitor with the information contained in the other ChannelMonitor.
-       /// After a successful call this ChannelMonitor is up-to-date and is safe to use to monitor the
-       /// chain for new blocks/transactions.
-       pub fn insert_combine(&mut self, mut other: ChannelMonitor) -> Result<(), MonitorUpdateError> {
-               match self.key_storage {
-                       Storage::Local { ref funding_info, .. } => {
-                               if funding_info.is_none() { return Err(MonitorUpdateError("Try to combine a Local monitor without funding_info")); }
-                               let our_funding_info = funding_info;
-                               if let Storage::Local { ref funding_info, .. } = other.key_storage {
-                                       if funding_info.is_none() { return Err(MonitorUpdateError("Try to combine a Local monitor without funding_info")); }
-                                       // We should be able to compare the entire funding_txo, but in fuzztarget it's trivially
-                                       // easy to collide the funding_txo hash and have a different scriptPubKey.
-                                       if funding_info.as_ref().unwrap().0 != our_funding_info.as_ref().unwrap().0 {
-                                               return Err(MonitorUpdateError("Funding transaction outputs are not identical!"));
-                                       }
-                               } else {
-                                       return Err(MonitorUpdateError("Try to combine a Local monitor with a Watchtower one !"));
-                               }
-                       },
-                       Storage::Watchtower { .. } => {
-                               if let Storage::Watchtower { .. } = other.key_storage {
-                                       unimplemented!();
-                               } else {
-                                       return Err(MonitorUpdateError("Try to combine a Watchtower monitor with a Local one !"));
-                               }
-                       },
-               }
-               let other_min_secret = other.get_min_seen_secret();
-               let our_min_secret = self.get_min_seen_secret();
-               if our_min_secret > other_min_secret {
-                       self.provide_secret(other_min_secret, other.get_secret(other_min_secret).unwrap())?;
-               }
-               if let Some(ref local_tx) = self.current_local_signed_commitment_tx {
-                       if let Some(ref other_local_tx) = other.current_local_signed_commitment_tx {
-                               let our_commitment_number = 0xffffffffffff - ((((local_tx.tx.input[0].sequence as u64 & 0xffffff) << 3*8) | (local_tx.tx.lock_time as u64 & 0xffffff)) ^ self.commitment_transaction_number_obscure_factor);
-                               let other_commitment_number = 0xffffffffffff - ((((other_local_tx.tx.input[0].sequence as u64 & 0xffffff) << 3*8) | (other_local_tx.tx.lock_time as u64 & 0xffffff)) ^ other.commitment_transaction_number_obscure_factor);
-                               if our_commitment_number >= other_commitment_number {
-                                       self.key_storage = other.key_storage;
-                               }
-                       }
-               }
-               // TODO: We should use current_remote_commitment_number and the commitment number out of
-               // local transactions to decide how to merge
-               if our_min_secret >= other_min_secret {
-                       self.their_cur_revocation_points = other.their_cur_revocation_points;
-                       for (txid, htlcs) in other.remote_claimable_outpoints.drain() {
-                               self.remote_claimable_outpoints.insert(txid, htlcs);
-                       }
-                       if let Some(local_tx) = other.prev_local_signed_commitment_tx {
-                               self.prev_local_signed_commitment_tx = Some(local_tx);
-                       }
-                       if let Some(local_tx) = other.current_local_signed_commitment_tx {
-                               self.current_local_signed_commitment_tx = Some(local_tx);
-                       }
-                       self.payment_preimages = other.payment_preimages;
-                       self.to_remote_rescue = other.to_remote_rescue;
-               }
-
-               self.current_remote_commitment_number = cmp::min(self.current_remote_commitment_number, other.current_remote_commitment_number);
-               Ok(())
-       }
-
-       /// Panics if commitment_transaction_number_obscure_factor doesn't fit in 48 bits
-       pub(super) fn set_commitment_obscure_factor(&mut self, commitment_transaction_number_obscure_factor: u64) {
-               assert!(commitment_transaction_number_obscure_factor < (1 << 48));
-               self.commitment_transaction_number_obscure_factor = commitment_transaction_number_obscure_factor;
-       }
-
-       /// Allows this monitor to scan only for transactions which are applicable. Note that this is
-       /// optional, without it this monitor cannot be used in an SPV client, but you may wish to
-       /// avoid this (or call unset_funding_info) on a monitor you wish to send to a watchtower as it
-       /// provides slightly better privacy.
-       /// It's the responsibility of the caller to register outpoint and script with passing the former
-       /// value as key to add_update_monitor.
-       pub(super) fn set_funding_info(&mut self, new_funding_info: (OutPoint, Script)) {
-               match self.key_storage {
-                       Storage::Local { ref mut funding_info, .. } => {
-                               *funding_info = Some(new_funding_info);
-                       },
-                       Storage::Watchtower { .. } => {
-                               panic!("Channel somehow ended up with its internal ChannelMonitor being in Watchtower mode?");
-                       }
-               }
-       }
-
-       /// We log these base keys at channel opening to being able to rebuild redeemscript in case of leaked revoked commit tx
-       pub(super) fn set_their_base_keys(&mut self, their_htlc_base_key: &PublicKey, their_delayed_payment_base_key: &PublicKey) {
-               self.their_htlc_base_key = Some(their_htlc_base_key.clone());
-               self.their_delayed_payment_base_key = Some(their_delayed_payment_base_key.clone());
-       }
-
-       pub(super) fn set_their_to_self_delay(&mut self, their_to_self_delay: u16) {
-               self.their_to_self_delay = Some(their_to_self_delay);
-       }
-
-       pub(super) fn unset_funding_info(&mut self) {
-               match self.key_storage {
-                       Storage::Local { ref mut funding_info, .. } => {
-                               *funding_info = None;
-                       },
-                       Storage::Watchtower { .. } => {
-                               panic!("Channel somehow ended up with its internal ChannelMonitor being in Watchtower mode?");
-                       },
-               }
-       }
-
-       /// Gets the funding transaction outpoint of the channel this ChannelMonitor is monitoring for.
-       pub fn get_funding_txo(&self) -> Option<OutPoint> {
-               match self.key_storage {
-                       Storage::Local { ref funding_info, .. } => {
-                               match funding_info {
-                                       &Some((outpoint, _)) => Some(outpoint),
-                                       &None => None
-                               }
-                       },
-                       Storage::Watchtower { .. } => {
-                               return None;
-                       }
-               }
-       }
-
-       /// Gets the sets of all outpoints which this ChannelMonitor expects to hear about spends of.
-       /// Generally useful when deserializing as during normal operation the return values of
-       /// block_connected are sufficient to ensure all relevant outpoints are being monitored (note
-       /// that the get_funding_txo outpoint and transaction must also be monitored for!).
-       pub fn get_monitored_outpoints(&self) -> Vec<(Sha256dHash, u32, &Script)> {
-               let mut res = Vec::with_capacity(self.remote_commitment_txn_on_chain.len() * 2);
-               for (ref txid, &(_, ref outputs)) in self.remote_commitment_txn_on_chain.iter() {
-                       for (idx, output) in outputs.iter().enumerate() {
-                               res.push(((*txid).clone(), idx as u32, output));
-                       }
-               }
-               res
-       }
-
-       /// Serializes into a vec, with various modes for the exposed pub fns
-       fn write<W: Writer>(&self, writer: &mut W, for_local_storage: bool) -> Result<(), ::std::io::Error> {
-               //TODO: We still write out all the serialization here manually instead of using the fancy
-               //serialization framework we have, we should migrate things over to it.
-               writer.write_all(&[SERIALIZATION_VERSION; 1])?;
-               writer.write_all(&[MIN_SERIALIZATION_VERSION; 1])?;
-
-               // Set in initial Channel-object creation, so should always be set by now:
-               U48(self.commitment_transaction_number_obscure_factor).write(writer)?;
-
-               macro_rules! write_option {
-                       ($thing: expr) => {
-                               match $thing {
-                                       &Some(ref t) => {
-                                               1u8.write(writer)?;
-                                               t.write(writer)?;
-                                       },
-                                       &None => 0u8.write(writer)?,
-                               }
-                       }
-               }
-
-               match self.key_storage {
-                       Storage::Local { ref revocation_base_key, ref htlc_base_key, ref delayed_payment_base_key, ref payment_base_key, ref shutdown_pubkey, ref prev_latest_per_commitment_point, ref latest_per_commitment_point, ref funding_info, ref current_remote_commitment_txid, ref prev_remote_commitment_txid } => {
-                               writer.write_all(&[0; 1])?;
-                               writer.write_all(&revocation_base_key[..])?;
-                               writer.write_all(&htlc_base_key[..])?;
-                               writer.write_all(&delayed_payment_base_key[..])?;
-                               writer.write_all(&payment_base_key[..])?;
-                               writer.write_all(&shutdown_pubkey.serialize())?;
-                               prev_latest_per_commitment_point.write(writer)?;
-                               latest_per_commitment_point.write(writer)?;
-                               match funding_info  {
-                                       &Some((ref outpoint, ref script)) => {
-                                               writer.write_all(&outpoint.txid[..])?;
-                                               writer.write_all(&byte_utils::be16_to_array(outpoint.index))?;
-                                               script.write(writer)?;
-                                       },
-                                       &None => {
-                                               debug_assert!(false, "Try to serialize a useless Local monitor !");
-                                       },
-                               }
-                               current_remote_commitment_txid.write(writer)?;
-                               prev_remote_commitment_txid.write(writer)?;
-                       },
-                       Storage::Watchtower { .. } => unimplemented!(),
-               }
-
-               writer.write_all(&self.their_htlc_base_key.as_ref().unwrap().serialize())?;
-               writer.write_all(&self.their_delayed_payment_base_key.as_ref().unwrap().serialize())?;
-
-               match self.their_cur_revocation_points {
-                       Some((idx, pubkey, second_option)) => {
-                               writer.write_all(&byte_utils::be48_to_array(idx))?;
-                               writer.write_all(&pubkey.serialize())?;
-                               match second_option {
-                                       Some(second_pubkey) => {
-                                               writer.write_all(&second_pubkey.serialize())?;
-                                       },
-                                       None => {
-                                               writer.write_all(&[0; 33])?;
-                                       },
-                               }
-                       },
-                       None => {
-                               writer.write_all(&byte_utils::be48_to_array(0))?;
-                       },
-               }
-
-               writer.write_all(&byte_utils::be16_to_array(self.our_to_self_delay))?;
-               writer.write_all(&byte_utils::be16_to_array(self.their_to_self_delay.unwrap()))?;
-
-               for &(ref secret, ref idx) in self.old_secrets.iter() {
-                       writer.write_all(secret)?;
-                       writer.write_all(&byte_utils::be64_to_array(*idx))?;
-               }
-
-               macro_rules! serialize_htlc_in_commitment {
-                       ($htlc_output: expr) => {
-                               writer.write_all(&[$htlc_output.offered as u8; 1])?;
-                               writer.write_all(&byte_utils::be64_to_array($htlc_output.amount_msat))?;
-                               writer.write_all(&byte_utils::be32_to_array($htlc_output.cltv_expiry))?;
-                               writer.write_all(&$htlc_output.payment_hash.0[..])?;
-                               $htlc_output.transaction_output_index.write(writer)?;
-                       }
-               }
-
-               writer.write_all(&byte_utils::be64_to_array(self.remote_claimable_outpoints.len() as u64))?;
-               for (ref txid, ref htlc_infos) in self.remote_claimable_outpoints.iter() {
-                       writer.write_all(&txid[..])?;
-                       writer.write_all(&byte_utils::be64_to_array(htlc_infos.len() as u64))?;
-                       for &(ref htlc_output, ref htlc_source) in htlc_infos.iter() {
-                               serialize_htlc_in_commitment!(htlc_output);
-                               write_option!(htlc_source);
-                       }
-               }
-
-               writer.write_all(&byte_utils::be64_to_array(self.remote_commitment_txn_on_chain.len() as u64))?;
-               for (ref txid, &(commitment_number, ref txouts)) in self.remote_commitment_txn_on_chain.iter() {
-                       writer.write_all(&txid[..])?;
-                       writer.write_all(&byte_utils::be48_to_array(commitment_number))?;
-                       (txouts.len() as u64).write(writer)?;
-                       for script in txouts.iter() {
-                               script.write(writer)?;
-                       }
-               }
-
-               if for_local_storage {
-                       writer.write_all(&byte_utils::be64_to_array(self.remote_hash_commitment_number.len() as u64))?;
-                       for (ref payment_hash, commitment_number) in self.remote_hash_commitment_number.iter() {
-                               writer.write_all(&payment_hash.0[..])?;
-                               writer.write_all(&byte_utils::be48_to_array(*commitment_number))?;
-                       }
-               } else {
-                       writer.write_all(&byte_utils::be64_to_array(0))?;
-               }
-
-               macro_rules! serialize_local_tx {
-                       ($local_tx: expr) => {
-                               if let Err(e) = $local_tx.tx.consensus_encode(&mut WriterWriteAdaptor(writer)) {
-                                       match e {
-                                               encode::Error::Io(e) => return Err(e),
-                                               _ => panic!("local tx must have been well-formed!"),
-                                       }
-                               }
-
-                               writer.write_all(&$local_tx.revocation_key.serialize())?;
-                               writer.write_all(&$local_tx.a_htlc_key.serialize())?;
-                               writer.write_all(&$local_tx.b_htlc_key.serialize())?;
-                               writer.write_all(&$local_tx.delayed_payment_key.serialize())?;
-
-                               writer.write_all(&byte_utils::be64_to_array($local_tx.feerate_per_kw))?;
-                               writer.write_all(&byte_utils::be64_to_array($local_tx.htlc_outputs.len() as u64))?;
-                               for &(ref htlc_output, ref sigs, ref htlc_source) in $local_tx.htlc_outputs.iter() {
-                                       serialize_htlc_in_commitment!(htlc_output);
-                                       if let &Some((ref their_sig, ref our_sig)) = sigs {
-                                               1u8.write(writer)?;
-                                               writer.write_all(&their_sig.serialize_compact())?;
-                                               writer.write_all(&our_sig.serialize_compact())?;
-                                       } else {
-                                               0u8.write(writer)?;
-                                       }
-                                       write_option!(htlc_source);
-                               }
-                       }
-               }
-
-               if let Some(ref prev_local_tx) = self.prev_local_signed_commitment_tx {
-                       writer.write_all(&[1; 1])?;
-                       serialize_local_tx!(prev_local_tx);
-               } else {
-                       writer.write_all(&[0; 1])?;
-               }
-
-               if let Some(ref cur_local_tx) = self.current_local_signed_commitment_tx {
-                       writer.write_all(&[1; 1])?;
-                       serialize_local_tx!(cur_local_tx);
-               } else {
-                       writer.write_all(&[0; 1])?;
-               }
-
-               if for_local_storage {
-                       writer.write_all(&byte_utils::be48_to_array(self.current_remote_commitment_number))?;
-               } else {
-                       writer.write_all(&byte_utils::be48_to_array(0))?;
-               }
-
-               writer.write_all(&byte_utils::be64_to_array(self.payment_preimages.len() as u64))?;
-               for payment_preimage in self.payment_preimages.values() {
-                       writer.write_all(&payment_preimage.0[..])?;
-               }
-
-               self.last_block_hash.write(writer)?;
-               self.destination_script.write(writer)?;
-               if let Some((ref to_remote_script, ref local_key)) = self.to_remote_rescue {
-                       writer.write_all(&[1; 1])?;
-                       to_remote_script.write(writer)?;
-                       local_key.write(writer)?;
-               } else {
-                       writer.write_all(&[0; 1])?;
-               }
-
-               writer.write_all(&byte_utils::be64_to_array(self.our_claim_txn_waiting_first_conf.len() as u64))?;
-               for (ref outpoint, claim_tx_data) in self.our_claim_txn_waiting_first_conf.iter() {
-                       outpoint.write(writer)?;
-                       writer.write_all(&byte_utils::be32_to_array(claim_tx_data.0))?;
-                       match claim_tx_data.1 {
-                               TxMaterial::Revoked { ref script, ref pubkey, ref key, ref is_htlc, ref amount} => {
-                                       writer.write_all(&[0; 1])?;
-                                       script.write(writer)?;
-                                       pubkey.write(writer)?;
-                                       writer.write_all(&key[..])?;
-                                       if *is_htlc {
-                                               writer.write_all(&[0; 1])?;
-                                       } else {
-                                               writer.write_all(&[1; 1])?;
-                                       }
-                                       writer.write_all(&byte_utils::be64_to_array(*amount))?;
-                               },
-                               TxMaterial::RemoteHTLC { ref script, ref key, ref preimage, ref amount } => {
-                                       writer.write_all(&[1; 1])?;
-                                       script.write(writer)?;
-                                       key.write(writer)?;
-                                       preimage.write(writer)?;
-                                       writer.write_all(&byte_utils::be64_to_array(*amount))?;
-                               },
-                               TxMaterial::LocalHTLC { ref script, ref sigs, ref preimage, ref amount } => {
-                                       writer.write_all(&[2; 1])?;
-                                       script.write(writer)?;
-                                       sigs.0.write(writer)?;
-                                       sigs.1.write(writer)?;
-                                       preimage.write(writer)?;
-                                       writer.write_all(&byte_utils::be64_to_array(*amount))?;
-                               }
-                       }
-                       writer.write_all(&byte_utils::be64_to_array(claim_tx_data.2))?;
-                       writer.write_all(&byte_utils::be32_to_array(claim_tx_data.3))?;
-                       writer.write_all(&byte_utils::be32_to_array(claim_tx_data.4))?;
-               }
-
-               writer.write_all(&byte_utils::be64_to_array(self.onchain_events_waiting_threshold_conf.len() as u64))?;
-               for (ref target, ref events) in self.onchain_events_waiting_threshold_conf.iter() {
-                       writer.write_all(&byte_utils::be32_to_array(**target))?;
-                       writer.write_all(&byte_utils::be64_to_array(events.len() as u64))?;
-                       for ev in events.iter() {
-                               match *ev {
-                                       OnchainEvent::Claim { ref outpoint } => {
-                                               writer.write_all(&[0; 1])?;
-                                               outpoint.write(writer)?;
-                                       },
-                                       OnchainEvent::HTLCUpdate { ref htlc_update } => {
-                                               writer.write_all(&[1; 1])?;
-                                               htlc_update.0.write(writer)?;
-                                               htlc_update.1.write(writer)?;
-                                       }
-                               }
-                       }
-               }
-
-               Ok(())
-       }
-
-       /// Writes this monitor into the given writer, suitable for writing to disk.
-       ///
-       /// Note that the deserializer is only implemented for (Sha256dHash, ChannelMonitor), which
-       /// tells you the last block hash which was block_connect()ed. You MUST rescan any blocks along
-       /// the "reorg path" (ie not just starting at the same height but starting at the highest
-       /// common block that appears on your best chain as well as on the chain which contains the
-       /// last block hash returned) upon deserializing the object!
-       pub fn write_for_disk<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
-               self.write(writer, true)
-       }
-
-       /// Encodes this monitor into the given writer, suitable for sending to a remote watchtower
-       ///
-       /// Note that the deserializer is only implemented for (Sha256dHash, ChannelMonitor), which
-       /// tells you the last block hash which was block_connect()ed. You MUST rescan any blocks along
-       /// the "reorg path" (ie not just starting at the same height but starting at the highest
-       /// common block that appears on your best chain as well as on the chain which contains the
-       /// last block hash returned) upon deserializing the object!
-       pub fn write_for_watchtower<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
-               self.write(writer, false)
-       }
-
-       /// Can only fail if idx is < get_min_seen_secret
-       pub(super) fn get_secret(&self, idx: u64) -> Option<[u8; 32]> {
-               for i in 0..self.old_secrets.len() {
-                       if (idx & (!((1 << i) - 1))) == self.old_secrets[i].1 {
-                               return Some(ChannelMonitor::derive_secret(self.old_secrets[i].0, i as u8, idx))
-                       }
-               }
-               assert!(idx < self.get_min_seen_secret());
-               None
-       }
-
-       pub(super) fn get_min_seen_secret(&self) -> u64 {
-               //TODO This can be optimized?
-               let mut min = 1 << 48;
-               for &(_, idx) in self.old_secrets.iter() {
-                       if idx < min {
-                               min = idx;
-                       }
-               }
-               min
-       }
-
-       pub(super) fn get_cur_remote_commitment_number(&self) -> u64 {
-               self.current_remote_commitment_number
-       }
-
-       pub(super) fn get_cur_local_commitment_number(&self) -> u64 {
-               if let &Some(ref local_tx) = &self.current_local_signed_commitment_tx {
-                       0xffff_ffff_ffff - ((((local_tx.tx.input[0].sequence as u64 & 0xffffff) << 3*8) | (local_tx.tx.lock_time as u64 & 0xffffff)) ^ self.commitment_transaction_number_obscure_factor)
-               } else { 0xffff_ffff_ffff }
-       }
-
-       /// Attempts to claim a remote commitment transaction's outputs using the revocation key and
-       /// data in remote_claimable_outpoints. Will directly claim any HTLC outputs which expire at a
-       /// height > height + CLTV_SHARED_CLAIM_BUFFER. In any case, will install monitoring for
-       /// HTLC-Success/HTLC-Timeout transactions.
-       /// Return updates for HTLC pending in the channel and failed automatically by the broadcast of
-       /// revoked remote commitment tx
-       fn check_spend_remote_transaction(&mut self, tx: &Transaction, height: u32, fee_estimator: &FeeEstimator) -> (Vec<Transaction>, (Sha256dHash, Vec<TxOut>), Vec<SpendableOutputDescriptor>) {
-               // Most secp and related errors trying to create keys means we have no hope of constructing
-               // a spend transaction...so we return no transactions to broadcast
-               let mut txn_to_broadcast = Vec::new();
-               let mut watch_outputs = Vec::new();
-               let mut spendable_outputs = Vec::new();
-
-               let commitment_txid = tx.txid(); //TODO: This is gonna be a performance bottleneck for watchtowers!
-               let per_commitment_option = self.remote_claimable_outpoints.get(&commitment_txid);
-
-               macro_rules! ignore_error {
-                       ( $thing : expr ) => {
-                               match $thing {
-                                       Ok(a) => a,
-                                       Err(_) => return (txn_to_broadcast, (commitment_txid, watch_outputs), spendable_outputs)
-                               }
-                       };
-               }
-
-               let commitment_number = 0xffffffffffff - ((((tx.input[0].sequence as u64 & 0xffffff) << 3*8) | (tx.lock_time as u64 & 0xffffff)) ^ self.commitment_transaction_number_obscure_factor);
-               if commitment_number >= self.get_min_seen_secret() {
-                       let secret = self.get_secret(commitment_number).unwrap();
-                       let per_commitment_key = ignore_error!(SecretKey::from_slice(&secret));
-                       let (revocation_pubkey, b_htlc_key, local_payment_key) = match self.key_storage {
-                               Storage::Local { ref revocation_base_key, ref htlc_base_key, ref payment_base_key, .. } => {
-                                       let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key);
-                                       (ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &PublicKey::from_secret_key(&self.secp_ctx, &revocation_base_key))),
-                                       ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &per_commitment_point, &PublicKey::from_secret_key(&self.secp_ctx, &htlc_base_key))),
-                                       Some(ignore_error!(chan_utils::derive_private_key(&self.secp_ctx, &per_commitment_point, &payment_base_key))))
-                               },
-                               Storage::Watchtower { ref revocation_base_key, ref htlc_base_key, .. } => {
-                                       let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key);
-                                       (ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &revocation_base_key)),
-                                       ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &per_commitment_point, &htlc_base_key)),
-                                       None)
-                               },
-                       };
-                       let delayed_key = ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key), &self.their_delayed_payment_base_key.unwrap()));
-                       let a_htlc_key = match self.their_htlc_base_key {
-                               None => return (txn_to_broadcast, (commitment_txid, watch_outputs), spendable_outputs),
-                               Some(their_htlc_base_key) => ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key), &their_htlc_base_key)),
-                       };
-
-                       let revokeable_redeemscript = chan_utils::get_revokeable_redeemscript(&revocation_pubkey, self.our_to_self_delay, &delayed_key);
-                       let revokeable_p2wsh = revokeable_redeemscript.to_v0_p2wsh();
-
-                       let local_payment_p2wpkh = if let Some(payment_key) = local_payment_key {
-                               // Note that the Network here is ignored as we immediately drop the address for the
-                               // script_pubkey version.
-                               let payment_hash160 = Hash160::hash(&PublicKey::from_secret_key(&self.secp_ctx, &payment_key).serialize());
-                               Some(Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(&payment_hash160[..]).into_script())
-                       } else { None };
-
-                       let mut total_value = 0;
-                       let mut inputs = Vec::new();
-                       let mut inputs_info = Vec::new();
-                       let mut inputs_desc = Vec::new();
-
-                       for (idx, outp) in tx.output.iter().enumerate() {
-                               if outp.script_pubkey == revokeable_p2wsh {
-                                       inputs.push(TxIn {
-                                               previous_output: BitcoinOutPoint {
-                                                       txid: commitment_txid,
-                                                       vout: idx as u32,
-                                               },
-                                               script_sig: Script::new(),
-                                               sequence: 0xfffffffd,
-                                               witness: Vec::new(),
-                                       });
-                                       inputs_desc.push(InputDescriptors::RevokedOutput);
-                                       inputs_info.push((None, outp.value, self.our_to_self_delay as u32));
-                                       total_value += outp.value;
-                               } else if Some(&outp.script_pubkey) == local_payment_p2wpkh.as_ref() {
-                                       spendable_outputs.push(SpendableOutputDescriptor::DynamicOutputP2WPKH {
-                                               outpoint: BitcoinOutPoint { txid: commitment_txid, vout: idx as u32 },
-                                               key: local_payment_key.unwrap(),
-                                               output: outp.clone(),
-                                       });
-                               }
-                       }
-
-                       macro_rules! sign_input {
-                               ($sighash_parts: expr, $input: expr, $htlc_idx: expr, $amount: expr) => {
-                                       {
-                                               let (sig, redeemscript, revocation_key) = match self.key_storage {
-                                                       Storage::Local { ref revocation_base_key, .. } => {
-                                                               let redeemscript = if $htlc_idx.is_none() { revokeable_redeemscript.clone() } else {
-                                                                       let htlc = &per_commitment_option.unwrap()[$htlc_idx.unwrap()].0;
-                                                                       chan_utils::get_htlc_redeemscript_with_explicit_keys(htlc, &a_htlc_key, &b_htlc_key, &revocation_pubkey)
-                                                               };
-                                                               let sighash = hash_to_message!(&$sighash_parts.sighash_all(&$input, &redeemscript, $amount)[..]);
-                                                               let revocation_key = ignore_error!(chan_utils::derive_private_revocation_key(&self.secp_ctx, &per_commitment_key, &revocation_base_key));
-                                                               (self.secp_ctx.sign(&sighash, &revocation_key), redeemscript, revocation_key)
-                                                       },
-                                                       Storage::Watchtower { .. } => {
-                                                               unimplemented!();
-                                                       }
-                                               };
-                                               $input.witness.push(sig.serialize_der().to_vec());
-                                               $input.witness[0].push(SigHashType::All as u8);
-                                               if $htlc_idx.is_none() {
-                                                       $input.witness.push(vec!(1));
-                                               } else {
-                                                       $input.witness.push(revocation_pubkey.serialize().to_vec());
-                                               }
-                                               $input.witness.push(redeemscript.clone().into_bytes());
-                                               (redeemscript, revocation_key)
-                                       }
-                               }
-                       }
-
-                       if let Some(ref per_commitment_data) = per_commitment_option {
-                               inputs.reserve_exact(per_commitment_data.len());
-
-                               for (idx, &(ref htlc, _)) in per_commitment_data.iter().enumerate() {
-                                       if let Some(transaction_output_index) = htlc.transaction_output_index {
-                                               let expected_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, &a_htlc_key, &b_htlc_key, &revocation_pubkey);
-                                               if transaction_output_index as usize >= tx.output.len() ||
-                                                               tx.output[transaction_output_index as usize].value != htlc.amount_msat / 1000 ||
-                                                               tx.output[transaction_output_index as usize].script_pubkey != expected_script.to_v0_p2wsh() {
-                                                       return (txn_to_broadcast, (commitment_txid, watch_outputs), spendable_outputs); // Corrupted per_commitment_data, fuck this user
-                                               }
-                                               let input = TxIn {
-                                                       previous_output: BitcoinOutPoint {
-                                                               txid: commitment_txid,
-                                                               vout: transaction_output_index,
-                                                       },
-                                                       script_sig: Script::new(),
-                                                       sequence: 0xfffffffd,
-                                                       witness: Vec::new(),
-                                               };
-                                               if htlc.cltv_expiry > height + CLTV_SHARED_CLAIM_BUFFER {
-                                                       inputs.push(input);
-                                                       inputs_desc.push(if htlc.offered { InputDescriptors::RevokedOfferedHTLC } else { InputDescriptors::RevokedReceivedHTLC });
-                                                       inputs_info.push((Some(idx), tx.output[transaction_output_index as usize].value, htlc.cltv_expiry));
-                                                       total_value += tx.output[transaction_output_index as usize].value;
-                                               } else {
-                                                       let mut single_htlc_tx = Transaction {
-                                                               version: 2,
-                                                               lock_time: 0,
-                                                               input: vec![input],
-                                                               output: vec!(TxOut {
-                                                                       script_pubkey: self.destination_script.clone(),
-                                                                       value: htlc.amount_msat / 1000,
-                                                               }),
-                                                       };
-                                                       let predicted_weight = single_htlc_tx.get_weight() + Self::get_witnesses_weight(&[if htlc.offered { InputDescriptors::RevokedOfferedHTLC } else { InputDescriptors::RevokedReceivedHTLC }]);
-                                                       let height_timer = Self::get_height_timer(height, htlc.cltv_expiry);
-                                                       let mut used_feerate;
-                                                       if subtract_high_prio_fee!(self, fee_estimator, single_htlc_tx.output[0].value, predicted_weight, tx.txid(), used_feerate) {
-                                                               let sighash_parts = bip143::SighashComponents::new(&single_htlc_tx);
-                                                               let (redeemscript, revocation_key) = sign_input!(sighash_parts, single_htlc_tx.input[0], Some(idx), htlc.amount_msat / 1000);
-                                                               assert!(predicted_weight >= single_htlc_tx.get_weight());
-                                                               match self.our_claim_txn_waiting_first_conf.entry(single_htlc_tx.input[0].previous_output.clone()) {
-                                                                       hash_map::Entry::Occupied(_) => {},
-                                                                       hash_map::Entry::Vacant(entry) => { entry.insert((height_timer, TxMaterial::Revoked { script: redeemscript, pubkey: Some(revocation_pubkey), key: revocation_key, is_htlc: true, amount: htlc.amount_msat / 1000 }, used_feerate, htlc.cltv_expiry, height)); }
-                                                               }
-                                                               txn_to_broadcast.push(single_htlc_tx);
-                                                       }
-                                               }
-                                       }
-                               }
-                       }
-
-                       if !inputs.is_empty() || !txn_to_broadcast.is_empty() || per_commitment_option.is_some() { // ie we're confident this is actually ours
-                               // We're definitely a remote commitment transaction!
-                               log_trace!(self, "Got broadcast of revoked remote commitment transaction, generating general spend tx with {} inputs and {} other txn to broadcast", inputs.len(), txn_to_broadcast.len());
-                               watch_outputs.append(&mut tx.output.clone());
-                               self.remote_commitment_txn_on_chain.insert(commitment_txid, (commitment_number, tx.output.iter().map(|output| { output.script_pubkey.clone() }).collect()));
-
-                               macro_rules! check_htlc_fails {
-                                       ($txid: expr, $commitment_tx: expr) => {
-                                               if let Some(ref outpoints) = self.remote_claimable_outpoints.get($txid) {
-                                                       for &(ref htlc, ref source_option) in outpoints.iter() {
-                                                               if let &Some(ref source) = source_option {
-                                                                       log_info!(self, "Failing HTLC with payment_hash {} from {} remote commitment tx due to broadcast of revoked remote commitment transaction, waiting for confirmation (at height {})", log_bytes!(htlc.payment_hash.0), $commitment_tx, height + ANTI_REORG_DELAY - 1);
-                                                                       match self.onchain_events_waiting_threshold_conf.entry(height + ANTI_REORG_DELAY - 1) {
-                                                                               hash_map::Entry::Occupied(mut entry) => {
-                                                                                       let e = entry.get_mut();
-                                                                                       e.retain(|ref event| {
-                                                                                               match **event {
-                                                                                                       OnchainEvent::HTLCUpdate { ref htlc_update } => {
-                                                                                                               return htlc_update.0 != **source
-                                                                                                       },
-                                                                                                       _ => return true
-                                                                                               }
-                                                                                       });
-                                                                                       e.push(OnchainEvent::HTLCUpdate { htlc_update: ((**source).clone(), htlc.payment_hash.clone())});
-                                                                               }
-                                                                               hash_map::Entry::Vacant(entry) => {
-                                                                                       entry.insert(vec![OnchainEvent::HTLCUpdate { htlc_update: ((**source).clone(), htlc.payment_hash.clone())}]);
-                                                                               }
-                                                                       }
-                                                               }
-                                                       }
-                                               }
-                                       }
-                               }
-                               if let Storage::Local { ref current_remote_commitment_txid, ref prev_remote_commitment_txid, .. } = self.key_storage {
-                                       if let &Some(ref txid) = current_remote_commitment_txid {
-                                               check_htlc_fails!(txid, "current");
-                                       }
-                                       if let &Some(ref txid) = prev_remote_commitment_txid {
-                                               check_htlc_fails!(txid, "remote");
-                                       }
-                               }
-                               // No need to check local commitment txn, symmetric HTLCSource must be present as per-htlc data on remote commitment tx
-                       }
-                       if inputs.is_empty() { return (txn_to_broadcast, (commitment_txid, watch_outputs), spendable_outputs); } // Nothing to be done...probably a false positive/local tx
-
-                       let outputs = vec!(TxOut {
-                               script_pubkey: self.destination_script.clone(),
-                               value: total_value,
-                       });
-                       let mut spend_tx = Transaction {
-                               version: 2,
-                               lock_time: 0,
-                               input: inputs,
-                               output: outputs,
-                       };
-
-                       let predicted_weight = spend_tx.get_weight() + Self::get_witnesses_weight(&inputs_desc[..]);
-
-                       let mut used_feerate;
-                       if !subtract_high_prio_fee!(self, fee_estimator, spend_tx.output[0].value, predicted_weight, tx.txid(), used_feerate) {
-                               return (txn_to_broadcast, (commitment_txid, watch_outputs), spendable_outputs);
-                       }
-
-                       let sighash_parts = bip143::SighashComponents::new(&spend_tx);
-
-                       for (input, info) in spend_tx.input.iter_mut().zip(inputs_info.iter()) {
-                               let (redeemscript, revocation_key) = sign_input!(sighash_parts, input, info.0, info.1);
-                               let height_timer = Self::get_height_timer(height, info.2);
-                               match self.our_claim_txn_waiting_first_conf.entry(input.previous_output.clone()) {
-                                       hash_map::Entry::Occupied(_) => {},
-                                       hash_map::Entry::Vacant(entry) => { entry.insert((height_timer, TxMaterial::Revoked { script: redeemscript, pubkey: if info.0.is_some() { Some(revocation_pubkey) } else { None }, key: revocation_key, is_htlc: if info.0.is_some() { true } else { false }, amount: info.1 }, used_feerate, if !info.0.is_some() { height + info.2 } else { info.2 }, height)); }
-                               }
-                       }
-                       assert!(predicted_weight >= spend_tx.get_weight());
-
-                       spendable_outputs.push(SpendableOutputDescriptor::StaticOutput {
-                               outpoint: BitcoinOutPoint { txid: spend_tx.txid(), vout: 0 },
-                               output: spend_tx.output[0].clone(),
-                       });
-                       txn_to_broadcast.push(spend_tx);
-               } else if let Some(per_commitment_data) = per_commitment_option {
-                       // While this isn't useful yet, there is a potential race where if a counterparty
-                       // revokes a state at the same time as the commitment transaction for that state is
-                       // confirmed, and the watchtower receives the block before the user, the user could
-                       // upload a new ChannelMonitor with the revocation secret but the watchtower has
-                       // already processed the block, resulting in the remote_commitment_txn_on_chain entry
-                       // not being generated by the above conditional. Thus, to be safe, we go ahead and
-                       // insert it here.
-                       watch_outputs.append(&mut tx.output.clone());
-                       self.remote_commitment_txn_on_chain.insert(commitment_txid, (commitment_number, tx.output.iter().map(|output| { output.script_pubkey.clone() }).collect()));
-
-                       log_trace!(self, "Got broadcast of non-revoked remote commitment transaction {}", commitment_txid);
-
-                       macro_rules! check_htlc_fails {
-                               ($txid: expr, $commitment_tx: expr, $id: tt) => {
-                                       if let Some(ref latest_outpoints) = self.remote_claimable_outpoints.get($txid) {
-                                               $id: for &(ref htlc, ref source_option) in latest_outpoints.iter() {
-                                                       if let &Some(ref source) = source_option {
-                                                               // Check if the HTLC is present in the commitment transaction that was
-                                                               // broadcast, but not if it was below the dust limit, which we should
-                                                               // fail backwards immediately as there is no way for us to learn the
-                                                               // payment_preimage.
-                                                               // Note that if the dust limit were allowed to change between
-                                                               // commitment transactions we'd want to be check whether *any*
-                                                               // broadcastable commitment transaction has the HTLC in it, but it
-                                                               // cannot currently change after channel initialization, so we don't
-                                                               // need to here.
-                                                               for &(ref broadcast_htlc, ref broadcast_source) in per_commitment_data.iter() {
-                                                                       if broadcast_htlc.transaction_output_index.is_some() && Some(source) == broadcast_source.as_ref() {
-                                                                               continue $id;
-                                                                       }
-                                                               }
-                                                               log_trace!(self, "Failing HTLC with payment_hash {} from {} remote commitment tx due to broadcast of remote commitment transaction", log_bytes!(htlc.payment_hash.0), $commitment_tx);
-                                                               match self.onchain_events_waiting_threshold_conf.entry(height + ANTI_REORG_DELAY - 1) {
-                                                                       hash_map::Entry::Occupied(mut entry) => {
-                                                                               let e = entry.get_mut();
-                                                                               e.retain(|ref event| {
-                                                                                       match **event {
-                                                                                               OnchainEvent::HTLCUpdate { ref htlc_update } => {
-                                                                                                       return htlc_update.0 != **source
-                                                                                               },
-                                                                                               _ => return true
-                                                                                       }
-                                                                               });
-                                                                               e.push(OnchainEvent::HTLCUpdate { htlc_update: ((**source).clone(), htlc.payment_hash.clone())});
-                                                                       }
-                                                                       hash_map::Entry::Vacant(entry) => {
-                                                                               entry.insert(vec![OnchainEvent::HTLCUpdate { htlc_update: ((**source).clone(), htlc.payment_hash.clone())}]);
-                                                                       }
-                                                               }
-                                                       }
-                                               }
-                                       }
-                               }
-                       }
-                       if let Storage::Local { ref current_remote_commitment_txid, ref prev_remote_commitment_txid, .. } = self.key_storage {
-                               if let &Some(ref txid) = current_remote_commitment_txid {
-                                       check_htlc_fails!(txid, "current", 'current_loop);
-                               }
-                               if let &Some(ref txid) = prev_remote_commitment_txid {
-                                       check_htlc_fails!(txid, "previous", 'prev_loop);
-                               }
-                       }
-
-                       if let Some(revocation_points) = self.their_cur_revocation_points {
-                               let revocation_point_option =
-                                       if revocation_points.0 == commitment_number { Some(&revocation_points.1) }
-                                       else if let Some(point) = revocation_points.2.as_ref() {
-                                               if revocation_points.0 == commitment_number + 1 { Some(point) } else { None }
-                                       } else { None };
-                               if let Some(revocation_point) = revocation_point_option {
-                                       let (revocation_pubkey, b_htlc_key) = match self.key_storage {
-                                               Storage::Local { ref revocation_base_key, ref htlc_base_key, .. } => {
-                                                       (ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, revocation_point, &PublicKey::from_secret_key(&self.secp_ctx, &revocation_base_key))),
-                                                       ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, revocation_point, &PublicKey::from_secret_key(&self.secp_ctx, &htlc_base_key))))
-                                               },
-                                               Storage::Watchtower { ref revocation_base_key, ref htlc_base_key, .. } => {
-                                                       (ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, revocation_point, &revocation_base_key)),
-                                                       ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, revocation_point, &htlc_base_key)))
-                                               },
-                                       };
-                                       let a_htlc_key = match self.their_htlc_base_key {
-                                               None => return (txn_to_broadcast, (commitment_txid, watch_outputs), spendable_outputs),
-                                               Some(their_htlc_base_key) => ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, revocation_point, &their_htlc_base_key)),
-                                       };
-
-                                       for (idx, outp) in tx.output.iter().enumerate() {
-                                               if outp.script_pubkey.is_v0_p2wpkh() {
-                                                       match self.key_storage {
-                                                               Storage::Local { ref payment_base_key, .. } => {
-                                                                       if let Ok(local_key) = chan_utils::derive_private_key(&self.secp_ctx, &revocation_point, &payment_base_key) {
-                                                                               spendable_outputs.push(SpendableOutputDescriptor::DynamicOutputP2WPKH {
-                                                                                       outpoint: BitcoinOutPoint { txid: commitment_txid, vout: idx as u32 },
-                                                                                       key: local_key,
-                                                                                       output: outp.clone(),
-                                                                               });
-                                                                       }
-                                                               },
-                                                               Storage::Watchtower { .. } => {}
-                                                       }
-                                                       break; // Only to_remote ouput is claimable
-                                               }
-                                       }
-
-                                       let mut total_value = 0;
-                                       let mut inputs = Vec::new();
-                                       let mut inputs_desc = Vec::new();
-                                       let mut inputs_info = Vec::new();
-
-                                       macro_rules! sign_input {
-                                               ($sighash_parts: expr, $input: expr, $amount: expr, $preimage: expr) => {
-                                                       {
-                                                               let (sig, redeemscript, htlc_key) = match self.key_storage {
-                                                                       Storage::Local { ref htlc_base_key, .. } => {
-                                                                               let htlc = &per_commitment_option.unwrap()[$input.sequence as usize].0;
-                                                                               let redeemscript = chan_utils::get_htlc_redeemscript_with_explicit_keys(htlc, &a_htlc_key, &b_htlc_key, &revocation_pubkey);
-                                                                               let sighash = hash_to_message!(&$sighash_parts.sighash_all(&$input, &redeemscript, $amount)[..]);
-                                                                               let htlc_key = ignore_error!(chan_utils::derive_private_key(&self.secp_ctx, revocation_point, &htlc_base_key));
-                                                                               (self.secp_ctx.sign(&sighash, &htlc_key), redeemscript, htlc_key)
-                                                                       },
-                                                                       Storage::Watchtower { .. } => {
-                                                                               unimplemented!();
-                                                                       }
-                                                               };
-                                                               $input.witness.push(sig.serialize_der().to_vec());
-                                                               $input.witness[0].push(SigHashType::All as u8);
-                                                               $input.witness.push($preimage);
-                                                               $input.witness.push(redeemscript.clone().into_bytes());
-                                                               (redeemscript, htlc_key)
-                                                       }
-                                               }
-                                       }
-
-                                       for (idx, &(ref htlc, _)) in per_commitment_data.iter().enumerate() {
-                                               if let Some(transaction_output_index) = htlc.transaction_output_index {
-                                                       let expected_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, &a_htlc_key, &b_htlc_key, &revocation_pubkey);
-                                                       if transaction_output_index as usize >= tx.output.len() ||
-                                                                       tx.output[transaction_output_index as usize].value != htlc.amount_msat / 1000 ||
-                                                                       tx.output[transaction_output_index as usize].script_pubkey != expected_script.to_v0_p2wsh() {
-                                                               return (txn_to_broadcast, (commitment_txid, watch_outputs), spendable_outputs); // Corrupted per_commitment_data, fuck this user
-                                                       }
-                                                       if let Some(payment_preimage) = self.payment_preimages.get(&htlc.payment_hash) {
-                                                               let input = TxIn {
-                                                                       previous_output: BitcoinOutPoint {
-                                                                               txid: commitment_txid,
-                                                                               vout: transaction_output_index,
-                                                                       },
-                                                                       script_sig: Script::new(),
-                                                                       sequence: idx as u32, // reset to 0xfffffffd in sign_input
-                                                                       witness: Vec::new(),
-                                                               };
-                                                               if htlc.cltv_expiry > height + CLTV_SHARED_CLAIM_BUFFER {
-                                                                       inputs.push(input);
-                                                                       inputs_desc.push(if htlc.offered { InputDescriptors::OfferedHTLC } else { InputDescriptors::ReceivedHTLC });
-                                                                       inputs_info.push((payment_preimage, tx.output[transaction_output_index as usize].value, htlc.cltv_expiry));
-                                                                       total_value += tx.output[transaction_output_index as usize].value;
-                                                               } else {
-                                                                       let mut single_htlc_tx = Transaction {
-                                                                               version: 2,
-                                                                               lock_time: 0,
-                                                                               input: vec![input],
-                                                                               output: vec!(TxOut {
-                                                                                       script_pubkey: self.destination_script.clone(),
-                                                                                       value: htlc.amount_msat / 1000,
-                                                                               }),
-                                                                       };
-                                                                       let predicted_weight = single_htlc_tx.get_weight() + Self::get_witnesses_weight(&[if htlc.offered { InputDescriptors::OfferedHTLC } else { InputDescriptors::ReceivedHTLC }]);
-                                                                       let height_timer = Self::get_height_timer(height, htlc.cltv_expiry);
-                                                                       let mut used_feerate;
-                                                                       if subtract_high_prio_fee!(self, fee_estimator, single_htlc_tx.output[0].value, predicted_weight, tx.txid(), used_feerate) {
-                                                                               let sighash_parts = bip143::SighashComponents::new(&single_htlc_tx);
-                                                                               let (redeemscript, htlc_key) = sign_input!(sighash_parts, single_htlc_tx.input[0], htlc.amount_msat / 1000, payment_preimage.0.to_vec());
-                                                                               assert!(predicted_weight >= single_htlc_tx.get_weight());
-                                                                               spendable_outputs.push(SpendableOutputDescriptor::StaticOutput {
-                                                                                       outpoint: BitcoinOutPoint { txid: single_htlc_tx.txid(), vout: 0 },
-                                                                                       output: single_htlc_tx.output[0].clone(),
-                                                                               });
-                                                                               match self.our_claim_txn_waiting_first_conf.entry(single_htlc_tx.input[0].previous_output.clone()) {
-                                                                                       hash_map::Entry::Occupied(_) => {},
-                                                                                       hash_map::Entry::Vacant(entry) => { entry.insert((height_timer, TxMaterial::RemoteHTLC { script: redeemscript, key: htlc_key, preimage: Some(*payment_preimage), amount: htlc.amount_msat / 1000 }, used_feerate, htlc.cltv_expiry, height)); }
-                                                                               }
-                                                                               txn_to_broadcast.push(single_htlc_tx);
-                                                                       }
-                                                               }
-                                                       }
-                                                       if !htlc.offered {
-                                                               // TODO: If the HTLC has already expired, potentially merge it with the
-                                                               // rest of the claim transaction, as above.
-                                                               let input = TxIn {
-                                                                       previous_output: BitcoinOutPoint {
-                                                                               txid: commitment_txid,
-                                                                               vout: transaction_output_index,
-                                                                       },
-                                                                       script_sig: Script::new(),
-                                                                       sequence: idx as u32,
-                                                                       witness: Vec::new(),
-                                                               };
-                                                               let mut timeout_tx = Transaction {
-                                                                       version: 2,
-                                                                       lock_time: htlc.cltv_expiry,
-                                                                       input: vec![input],
-                                                                       output: vec!(TxOut {
-                                                                               script_pubkey: self.destination_script.clone(),
-                                                                               value: htlc.amount_msat / 1000,
-                                                                       }),
-                                                               };
-                                                               let predicted_weight = timeout_tx.get_weight() + Self::get_witnesses_weight(&[InputDescriptors::ReceivedHTLC]);
-                                                               let height_timer = Self::get_height_timer(height, htlc.cltv_expiry);
-                                                               let mut used_feerate;
-                                                               if subtract_high_prio_fee!(self, fee_estimator, timeout_tx.output[0].value, predicted_weight, tx.txid(), used_feerate) {
-                                                                       let sighash_parts = bip143::SighashComponents::new(&timeout_tx);
-                                                                       let (redeemscript, htlc_key) = sign_input!(sighash_parts, timeout_tx.input[0], htlc.amount_msat / 1000, vec![0]);
-                                                                       assert!(predicted_weight >= timeout_tx.get_weight());
-                                                                       //TODO: track SpendableOutputDescriptor
-                                                                       match self.our_claim_txn_waiting_first_conf.entry(timeout_tx.input[0].previous_output.clone()) {
-                                                                               hash_map::Entry::Occupied(_) => {},
-                                                                               hash_map::Entry::Vacant(entry) => { entry.insert((height_timer, TxMaterial::RemoteHTLC { script : redeemscript, key: htlc_key, preimage: None, amount: htlc.amount_msat / 1000 }, used_feerate, htlc.cltv_expiry, height)); }
-                                                                       }
-                                                               }
-                                                               txn_to_broadcast.push(timeout_tx);
-                                                       }
-                                               }
-                                       }
-
-                                       if inputs.is_empty() { return (txn_to_broadcast, (commitment_txid, watch_outputs), spendable_outputs); } // Nothing to be done...probably a false positive/local tx
-
-                                       let outputs = vec!(TxOut {
-                                               script_pubkey: self.destination_script.clone(),
-                                               value: total_value
-                                       });
-                                       let mut spend_tx = Transaction {
-                                               version: 2,
-                                               lock_time: 0,
-                                               input: inputs,
-                                               output: outputs,
-                                       };
-
-                                       let mut predicted_weight = spend_tx.get_weight() + Self::get_witnesses_weight(&inputs_desc[..]);
-
-                                       let mut used_feerate;
-                                       if !subtract_high_prio_fee!(self, fee_estimator, spend_tx.output[0].value, predicted_weight, tx.txid(), used_feerate) {
-                                               return (txn_to_broadcast, (commitment_txid, watch_outputs), spendable_outputs);
-                                       }
-
-                                       let sighash_parts = bip143::SighashComponents::new(&spend_tx);
-
-                                       for (input, info) in spend_tx.input.iter_mut().zip(inputs_info.iter()) {
-                                               let (redeemscript, htlc_key) = sign_input!(sighash_parts, input, info.1, (info.0).0.to_vec());
-                                               let height_timer = Self::get_height_timer(height, info.2);
-                                               match self.our_claim_txn_waiting_first_conf.entry(input.previous_output.clone()) {
-                                                       hash_map::Entry::Occupied(_) => {},
-                                                       hash_map::Entry::Vacant(entry) => { entry.insert((height_timer, TxMaterial::RemoteHTLC { script: redeemscript, key: htlc_key, preimage: Some(*(info.0)), amount: info.1}, used_feerate, info.2, height)); }
-                                               }
-                                       }
-                                       assert!(predicted_weight >= spend_tx.get_weight());
-                                       spendable_outputs.push(SpendableOutputDescriptor::StaticOutput {
-                                               outpoint: BitcoinOutPoint { txid: spend_tx.txid(), vout: 0 },
-                                               output: spend_tx.output[0].clone(),
-                                       });
-                                       txn_to_broadcast.push(spend_tx);
-                               }
-                       }
-               } else if let Some((ref to_remote_rescue, ref local_key)) = self.to_remote_rescue {
-                       for (idx, outp) in tx.output.iter().enumerate() {
-                               if to_remote_rescue == &outp.script_pubkey {
-                                       spendable_outputs.push(SpendableOutputDescriptor::DynamicOutputP2WPKH {
-                                               outpoint: BitcoinOutPoint { txid: commitment_txid, vout: idx as u32 },
-                                               key: local_key.clone(),
-                                               output: outp.clone(),
-                                       });
-                               }
-                       }
-               }
-
-               (txn_to_broadcast, (commitment_txid, watch_outputs), spendable_outputs)
-       }
-
-       /// Attempts to claim a remote HTLC-Success/HTLC-Timeout's outputs using the revocation key
-       fn check_spend_remote_htlc(&mut self, tx: &Transaction, commitment_number: u64, height: u32, fee_estimator: &FeeEstimator) -> (Option<Transaction>, Option<SpendableOutputDescriptor>) {
-               if tx.input.len() != 1 || tx.output.len() != 1 {
-                       return (None, None)
-               }
-
-               macro_rules! ignore_error {
-                       ( $thing : expr ) => {
-                               match $thing {
-                                       Ok(a) => a,
-                                       Err(_) => return (None, None)
-                               }
-                       };
-               }
-
-               let secret = if let Some(secret) = self.get_secret(commitment_number) { secret } else { return (None, None); };
-               let per_commitment_key = ignore_error!(SecretKey::from_slice(&secret));
-               let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key);
-               let revocation_pubkey = match self.key_storage {
-                       Storage::Local { ref revocation_base_key, .. } => {
-                               ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &PublicKey::from_secret_key(&self.secp_ctx, &revocation_base_key)))
-                       },
-                       Storage::Watchtower { ref revocation_base_key, .. } => {
-                               ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &revocation_base_key))
-                       },
-               };
-               let delayed_key = match self.their_delayed_payment_base_key {
-                       None => return (None, None),
-                       Some(their_delayed_payment_base_key) => ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &per_commitment_point, &their_delayed_payment_base_key)),
-               };
-               let redeemscript = chan_utils::get_revokeable_redeemscript(&revocation_pubkey, self.our_to_self_delay, &delayed_key);
-               let revokeable_p2wsh = redeemscript.to_v0_p2wsh();
-               let htlc_txid = tx.txid(); //TODO: This is gonna be a performance bottleneck for watchtowers!
-
-               let mut inputs = Vec::new();
-               let mut amount = 0;
-
-               if tx.output[0].script_pubkey == revokeable_p2wsh { //HTLC transactions have one txin, one txout
-                       inputs.push(TxIn {
-                               previous_output: BitcoinOutPoint {
-                                       txid: htlc_txid,
-                                       vout: 0,
-                               },
-                               script_sig: Script::new(),
-                               sequence: 0xfffffffd,
-                               witness: Vec::new(),
-                       });
-                       amount = tx.output[0].value;
-               }
-
-               if !inputs.is_empty() {
-                       let outputs = vec!(TxOut {
-                               script_pubkey: self.destination_script.clone(),
-                               value: amount
-                       });
-
-                       let mut spend_tx = Transaction {
-                               version: 2,
-                               lock_time: 0,
-                               input: inputs,
-                               output: outputs,
-                       };
-                       let predicted_weight = spend_tx.get_weight() + Self::get_witnesses_weight(&[InputDescriptors::RevokedOutput]);
-                       let mut used_feerate;
-                       if !subtract_high_prio_fee!(self, fee_estimator, spend_tx.output[0].value, predicted_weight, tx.txid(), used_feerate) {
-                               return (None, None);
-                       }
-
-                       let sighash_parts = bip143::SighashComponents::new(&spend_tx);
-
-                       let (sig, revocation_key) = match self.key_storage {
-                               Storage::Local { ref revocation_base_key, .. } => {
-                                       let sighash = hash_to_message!(&sighash_parts.sighash_all(&spend_tx.input[0], &redeemscript, amount)[..]);
-                                       let revocation_key = ignore_error!(chan_utils::derive_private_revocation_key(&self.secp_ctx, &per_commitment_key, &revocation_base_key));
-                                       (self.secp_ctx.sign(&sighash, &revocation_key), revocation_key)
-                               }
-                               Storage::Watchtower { .. } => {
-                                       unimplemented!();
-                               }
-                       };
-                       spend_tx.input[0].witness.push(sig.serialize_der().to_vec());
-                       spend_tx.input[0].witness[0].push(SigHashType::All as u8);
-                       spend_tx.input[0].witness.push(vec!(1));
-                       spend_tx.input[0].witness.push(redeemscript.clone().into_bytes());
-
-                       assert!(predicted_weight >= spend_tx.get_weight());
-                       let outpoint = BitcoinOutPoint { txid: spend_tx.txid(), vout: 0 };
-                       let output = spend_tx.output[0].clone();
-                       let height_timer = Self::get_height_timer(height, self.their_to_self_delay.unwrap() as u32); // We can safely unwrap given we are past channel opening
-                       match self.our_claim_txn_waiting_first_conf.entry(spend_tx.input[0].previous_output.clone()) {
-                               hash_map::Entry::Occupied(_) => {},
-                               hash_map::Entry::Vacant(entry) => { entry.insert((height_timer, TxMaterial::Revoked { script: redeemscript, pubkey: None, key: revocation_key, is_htlc: false, amount: tx.output[0].value }, used_feerate, height + self.our_to_self_delay as u32, height)); }
-                       }
-                       (Some(spend_tx), Some(SpendableOutputDescriptor::StaticOutput { outpoint, output }))
-               } else { (None, None) }
-       }
-
-       fn broadcast_by_local_state(&self, local_tx: &LocalSignedTx, per_commitment_point: &Option<PublicKey>, delayed_payment_base_key: &Option<SecretKey>, height: u32) -> (Vec<Transaction>, Vec<SpendableOutputDescriptor>, Vec<TxOut>, Vec<(BitcoinOutPoint, (u32, TxMaterial, u64, u32, u32))>) {
-               let mut res = Vec::with_capacity(local_tx.htlc_outputs.len());
-               let mut spendable_outputs = Vec::with_capacity(local_tx.htlc_outputs.len());
-               let mut watch_outputs = Vec::with_capacity(local_tx.htlc_outputs.len());
-               let mut pending_claims = Vec::with_capacity(local_tx.htlc_outputs.len());
-
-               macro_rules! add_dynamic_output {
-                       ($father_tx: expr, $vout: expr) => {
-                               if let Some(ref per_commitment_point) = *per_commitment_point {
-                                       if let Some(ref delayed_payment_base_key) = *delayed_payment_base_key {
-                                               if let Ok(local_delayedkey) = chan_utils::derive_private_key(&self.secp_ctx, per_commitment_point, delayed_payment_base_key) {
-                                                       spendable_outputs.push(SpendableOutputDescriptor::DynamicOutputP2WSH {
-                                                               outpoint: BitcoinOutPoint { txid: $father_tx.txid(), vout: $vout },
-                                                               key: local_delayedkey,
-                                                               witness_script: chan_utils::get_revokeable_redeemscript(&local_tx.revocation_key, self.our_to_self_delay, &local_tx.delayed_payment_key),
-                                                               to_self_delay: self.our_to_self_delay,
-                                                               output: $father_tx.output[$vout as usize].clone(),
-                                                       });
-                                               }
-                                       }
-                               }
-                       }
-               }
-
-
-               let redeemscript = chan_utils::get_revokeable_redeemscript(&local_tx.revocation_key, self.their_to_self_delay.unwrap(), &local_tx.delayed_payment_key);
-               let revokeable_p2wsh = redeemscript.to_v0_p2wsh();
-               for (idx, output) in local_tx.tx.output.iter().enumerate() {
-                       if output.script_pubkey == revokeable_p2wsh {
-                               add_dynamic_output!(local_tx.tx, idx as u32);
-                               break;
-                       }
-               }
-
-               for &(ref htlc, ref sigs, _) in local_tx.htlc_outputs.iter() {
-                       if let Some(transaction_output_index) = htlc.transaction_output_index {
-                               if let &Some((ref their_sig, ref our_sig)) = sigs {
-                                       if htlc.offered {
-                                               log_trace!(self, "Broadcasting HTLC-Timeout transaction against local commitment transactions");
-                                               let mut htlc_timeout_tx = chan_utils::build_htlc_transaction(&local_tx.txid, local_tx.feerate_per_kw, self.their_to_self_delay.unwrap(), htlc, &local_tx.delayed_payment_key, &local_tx.revocation_key);
-
-                                               htlc_timeout_tx.input[0].witness.push(Vec::new()); // First is the multisig dummy
-
-                                               htlc_timeout_tx.input[0].witness.push(their_sig.serialize_der().to_vec());
-                                               htlc_timeout_tx.input[0].witness[1].push(SigHashType::All as u8);
-                                               htlc_timeout_tx.input[0].witness.push(our_sig.serialize_der().to_vec());
-                                               htlc_timeout_tx.input[0].witness[2].push(SigHashType::All as u8);
-
-                                               htlc_timeout_tx.input[0].witness.push(Vec::new());
-                                               let htlc_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(htlc, &local_tx.a_htlc_key, &local_tx.b_htlc_key, &local_tx.revocation_key);
-                                               htlc_timeout_tx.input[0].witness.push(htlc_script.clone().into_bytes());
-
-                                               add_dynamic_output!(htlc_timeout_tx, 0);
-                                               let height_timer = Self::get_height_timer(height, htlc.cltv_expiry);
-                                               pending_claims.push((htlc_timeout_tx.input[0].previous_output.clone(), (height_timer, TxMaterial::LocalHTLC { script: htlc_script, sigs: (*their_sig, *our_sig), preimage: None, amount: htlc.amount_msat / 1000}, 0, htlc.cltv_expiry, height)));
-                                               res.push(htlc_timeout_tx);
-                                       } else {
-                                               if let Some(payment_preimage) = self.payment_preimages.get(&htlc.payment_hash) {
-                                                       log_trace!(self, "Broadcasting HTLC-Success transaction against local commitment transactions");
-                                                       let mut htlc_success_tx = chan_utils::build_htlc_transaction(&local_tx.txid, local_tx.feerate_per_kw, self.their_to_self_delay.unwrap(), htlc, &local_tx.delayed_payment_key, &local_tx.revocation_key);
-
-                                                       htlc_success_tx.input[0].witness.push(Vec::new()); // First is the multisig dummy
-
-                                                       htlc_success_tx.input[0].witness.push(their_sig.serialize_der().to_vec());
-                                                       htlc_success_tx.input[0].witness[1].push(SigHashType::All as u8);
-                                                       htlc_success_tx.input[0].witness.push(our_sig.serialize_der().to_vec());
-                                                       htlc_success_tx.input[0].witness[2].push(SigHashType::All as u8);
-
-                                                       htlc_success_tx.input[0].witness.push(payment_preimage.0.to_vec());
-                                                       let htlc_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(htlc, &local_tx.a_htlc_key, &local_tx.b_htlc_key, &local_tx.revocation_key);
-                                                       htlc_success_tx.input[0].witness.push(htlc_script.clone().into_bytes());
-
-                                                       add_dynamic_output!(htlc_success_tx, 0);
-                                                       let height_timer = Self::get_height_timer(height, htlc.cltv_expiry);
-                                                       pending_claims.push((htlc_success_tx.input[0].previous_output.clone(), (height_timer, TxMaterial::LocalHTLC { script: htlc_script, sigs: (*their_sig, *our_sig), preimage: Some(*payment_preimage), amount: htlc.amount_msat / 1000}, 0, htlc.cltv_expiry, height)));
-                                                       res.push(htlc_success_tx);
-                                               }
-                                       }
-                                       watch_outputs.push(local_tx.tx.output[transaction_output_index as usize].clone());
-                               } else { panic!("Should have sigs for non-dust local tx outputs!") }
-                       }
-               }
-
-               (res, spendable_outputs, watch_outputs, pending_claims)
-       }
-
-       /// Attempts to claim any claimable HTLCs in a commitment transaction which was not (yet)
-       /// revoked using data in local_claimable_outpoints.
-       /// Should not be used if check_spend_revoked_transaction succeeds.
-       fn check_spend_local_transaction(&mut self, tx: &Transaction, height: u32) -> (Vec<Transaction>, Vec<SpendableOutputDescriptor>, (Sha256dHash, Vec<TxOut>)) {
-               let commitment_txid = tx.txid();
-               let mut local_txn = Vec::new();
-               let mut spendable_outputs = Vec::new();
-               let mut watch_outputs = Vec::new();
-
-               macro_rules! wait_threshold_conf {
-                       ($height: expr, $source: expr, $commitment_tx: expr, $payment_hash: expr) => {
-                               log_trace!(self, "Failing HTLC with payment_hash {} from {} local commitment tx due to broadcast of transaction, waiting confirmation (at height{})", log_bytes!($payment_hash.0), $commitment_tx, height + ANTI_REORG_DELAY - 1);
-                               match self.onchain_events_waiting_threshold_conf.entry($height + ANTI_REORG_DELAY - 1) {
-                                       hash_map::Entry::Occupied(mut entry) => {
-                                               let e = entry.get_mut();
-                                               e.retain(|ref event| {
-                                                       match **event {
-                                                               OnchainEvent::HTLCUpdate { ref htlc_update } => {
-                                                                       return htlc_update.0 != $source
-                                                               },
-                                                               _ => return true
-                                                       }
-                                               });
-                                               e.push(OnchainEvent::HTLCUpdate { htlc_update: ($source, $payment_hash)});
-                                       }
-                                       hash_map::Entry::Vacant(entry) => {
-                                               entry.insert(vec![OnchainEvent::HTLCUpdate { htlc_update: ($source, $payment_hash)}]);
-                                       }
-                               }
-                       }
-               }
-
-               macro_rules! append_onchain_update {
-                       ($updates: expr) => {
-                               local_txn.append(&mut $updates.0);
-                               spendable_outputs.append(&mut $updates.1);
-                               watch_outputs.append(&mut $updates.2);
-                               for claim in $updates.3 {
-                                       match self.our_claim_txn_waiting_first_conf.entry(claim.0) {
-                                               hash_map::Entry::Occupied(_) => {},
-                                               hash_map::Entry::Vacant(entry) => { entry.insert(claim.1); }
-                                       }
-                               }
-                       }
-               }
-
-               // HTLCs set may differ between last and previous local commitment txn, in case of one them hitting chain, ensure we cancel all HTLCs backward
-               let mut is_local_tx = false;
-
-               if let &Some(ref local_tx) = &self.current_local_signed_commitment_tx {
-                       if local_tx.txid == commitment_txid {
-                               is_local_tx = true;
-                               log_trace!(self, "Got latest local commitment tx broadcast, searching for available HTLCs to claim");
-                               match self.key_storage {
-                                       Storage::Local { ref delayed_payment_base_key, ref latest_per_commitment_point, .. } => {
-                                               append_onchain_update!(self.broadcast_by_local_state(local_tx, latest_per_commitment_point, &Some(*delayed_payment_base_key), height));
-                                       },
-                                       Storage::Watchtower { .. } => {
-                                               append_onchain_update!(self.broadcast_by_local_state(local_tx, &None, &None, height));
-                                       }
-                               }
-                       }
-               }
-               if let &Some(ref local_tx) = &self.prev_local_signed_commitment_tx {
-                       if local_tx.txid == commitment_txid {
-                               is_local_tx = true;
-                               log_trace!(self, "Got previous local commitment tx broadcast, searching for available HTLCs to claim");
-                               match self.key_storage {
-                                       Storage::Local { ref delayed_payment_base_key, ref prev_latest_per_commitment_point, .. } => {
-                                               append_onchain_update!(self.broadcast_by_local_state(local_tx, prev_latest_per_commitment_point, &Some(*delayed_payment_base_key), height));
-                                       },
-                                       Storage::Watchtower { .. } => {
-                                               append_onchain_update!(self.broadcast_by_local_state(local_tx, &None, &None, height));
-                                       }
-                               }
-                       }
-               }
-
-               macro_rules! fail_dust_htlcs_after_threshold_conf {
-                       ($local_tx: expr) => {
-                               for &(ref htlc, _, ref source) in &$local_tx.htlc_outputs {
-                                       if htlc.transaction_output_index.is_none() {
-                                               if let &Some(ref source) = source {
-                                                       wait_threshold_conf!(height, source.clone(), "lastest", htlc.payment_hash.clone());
-                                               }
-                                       }
-                               }
-                       }
-               }
-
-               if is_local_tx {
-                       if let &Some(ref local_tx) = &self.current_local_signed_commitment_tx {
-                               fail_dust_htlcs_after_threshold_conf!(local_tx);
-                       }
-                       if let &Some(ref local_tx) = &self.prev_local_signed_commitment_tx {
-                               fail_dust_htlcs_after_threshold_conf!(local_tx);
-                       }
-               }
-
-               (local_txn, spendable_outputs, (commitment_txid, watch_outputs))
-       }
-
-       /// Generate a spendable output event when closing_transaction get registered onchain.
-       fn check_spend_closing_transaction(&self, tx: &Transaction) -> Option<SpendableOutputDescriptor> {
-               if tx.input[0].sequence == 0xFFFFFFFF && !tx.input[0].witness.is_empty() && tx.input[0].witness.last().unwrap().len() == 71 {
-                       match self.key_storage {
-                               Storage::Local { ref shutdown_pubkey, .. } =>  {
-                                       let our_channel_close_key_hash = Hash160::hash(&shutdown_pubkey.serialize());
-                                       let shutdown_script = Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(&our_channel_close_key_hash[..]).into_script();
-                                       for (idx, output) in tx.output.iter().enumerate() {
-                                               if shutdown_script == output.script_pubkey {
-                                                       return Some(SpendableOutputDescriptor::StaticOutput {
-                                                               outpoint: BitcoinOutPoint { txid: tx.txid(), vout: idx as u32 },
-                                                               output: output.clone(),
-                                                       });
-                                               }
-                                       }
-                               }
-                               Storage::Watchtower { .. } => {
-                                       //TODO: we need to ensure an offline client will generate the event when it
-                                       // comes back online after only the watchtower saw the transaction
-                               }
-                       }
-               }
-               None
-       }
-
-       /// Used by ChannelManager deserialization to broadcast the latest local state if its copy of
-       /// the Channel was out-of-date. You may use it to get a broadcastable local toxic tx in case of
-       /// fallen-behind, i.e when receiving a channel_reestablish with a proof that our remote side knows
-       /// a higher revocation secret than the local commitment number we are aware of. Broadcasting these
-       /// transactions are UNSAFE, as they allow remote side to punish you. Nevertheless you may want to
-       /// broadcast them if remote don't close channel with his higher commitment transaction after a
-       /// substantial amount of time (a month or even a year) to get back funds. Best may be to contact
-       /// out-of-band the other node operator to coordinate with him if option is available to you.
-       /// In any-case, choice is up to the user.
-       pub fn get_latest_local_commitment_txn(&self) -> Vec<Transaction> {
-               if let &Some(ref local_tx) = &self.current_local_signed_commitment_tx {
-                       let mut res = vec![local_tx.tx.clone()];
-                       match self.key_storage {
-                               Storage::Local { ref delayed_payment_base_key, ref prev_latest_per_commitment_point, .. } => {
-                                       res.append(&mut self.broadcast_by_local_state(local_tx, prev_latest_per_commitment_point, &Some(*delayed_payment_base_key), 0).0);
-                                       // We throw away the generated waiting_first_conf data as we aren't (yet) confirmed and we don't actually know what the caller wants to do.
-                                       // The data will be re-generated and tracked in check_spend_local_transaction if we get a confirmation.
-                               },
-                               _ => panic!("Can only broadcast by local channelmonitor"),
-                       };
-                       res
-               } else {
-                       Vec::new()
-               }
-       }
-
-       fn block_connected(&mut self, txn_matched: &[&Transaction], height: u32, block_hash: &Sha256dHash, broadcaster: &BroadcasterInterface, fee_estimator: &FeeEstimator)-> (Vec<(Sha256dHash, Vec<TxOut>)>, Vec<SpendableOutputDescriptor>, Vec<(HTLCSource, Option<PaymentPreimage>, PaymentHash)>) {
-               let mut watch_outputs = Vec::new();
-               let mut spendable_outputs = Vec::new();
-               let mut htlc_updated = Vec::new();
-               for tx in txn_matched {
-                       if tx.input.len() == 1 {
-                               // Assuming our keys were not leaked (in which case we're screwed no matter what),
-                               // commitment transactions and HTLC transactions will all only ever have one input,
-                               // which is an easy way to filter out any potential non-matching txn for lazy
-                               // filters.
-                               let prevout = &tx.input[0].previous_output;
-                               let mut txn: Vec<Transaction> = Vec::new();
-                               let funding_txo = match self.key_storage {
-                                       Storage::Local { ref funding_info, .. } => {
-                                               funding_info.clone()
-                                       }
-                                       Storage::Watchtower { .. } => {
-                                               unimplemented!();
-                                       }
-                               };
-                               if funding_txo.is_none() || (prevout.txid == funding_txo.as_ref().unwrap().0.txid && prevout.vout == funding_txo.as_ref().unwrap().0.index as u32) {
-                                       if (tx.input[0].sequence >> 8*3) as u8 == 0x80 && (tx.lock_time >> 8*3) as u8 == 0x20 {
-                                               let (remote_txn, new_outputs, mut spendable_output) = self.check_spend_remote_transaction(tx, height, fee_estimator);
-                                               txn = remote_txn;
-                                               spendable_outputs.append(&mut spendable_output);
-                                               if !new_outputs.1.is_empty() {
-                                                       watch_outputs.push(new_outputs);
-                                               }
-                                               if txn.is_empty() {
-                                                       let (local_txn, mut spendable_output, new_outputs) = self.check_spend_local_transaction(tx, height);
-                                                       spendable_outputs.append(&mut spendable_output);
-                                                       txn = local_txn;
-                                                       if !new_outputs.1.is_empty() {
-                                                               watch_outputs.push(new_outputs);
-                                                       }
-                                               }
-                                       }
-                                       if !funding_txo.is_none() && txn.is_empty() {
-                                               if let Some(spendable_output) = self.check_spend_closing_transaction(tx) {
-                                                       spendable_outputs.push(spendable_output);
-                                               }
-                                       }
-                               } else {
-                                       if let Some(&(commitment_number, _)) = self.remote_commitment_txn_on_chain.get(&prevout.txid) {
-                                               let (tx, spendable_output) = self.check_spend_remote_htlc(tx, commitment_number, height, fee_estimator);
-                                               if let Some(tx) = tx {
-                                                       txn.push(tx);
-                                               }
-                                               if let Some(spendable_output) = spendable_output {
-                                                       spendable_outputs.push(spendable_output);
-                                               }
-                                       }
-                               }
-                               for tx in txn.iter() {
-                                       broadcaster.broadcast_transaction(tx);
-                               }
-                       }
-                       // While all commitment/HTLC-Success/HTLC-Timeout transactions have one input, HTLCs
-                       // can also be resolved in a few other ways which can have more than one output. Thus,
-                       // we call is_resolving_htlc_output here outside of the tx.input.len() == 1 check.
-                       let mut updated = self.is_resolving_htlc_output(tx, height);
-                       if updated.len() > 0 {
-                               htlc_updated.append(&mut updated);
-                       }
-                       for inp in &tx.input {
-                               if self.our_claim_txn_waiting_first_conf.contains_key(&inp.previous_output) {
-                                       match self.onchain_events_waiting_threshold_conf.entry(height + ANTI_REORG_DELAY - 1) {
-                                               hash_map::Entry::Occupied(mut entry) => {
-                                                       let e = entry.get_mut();
-                                                       e.retain(|ref event| {
-                                                               match **event {
-                                                                       OnchainEvent::Claim { outpoint } => {
-                                                                               return outpoint != inp.previous_output
-                                                                       },
-                                                                       _ => return true
-                                                               }
-                                                       });
-                                                       e.push(OnchainEvent::Claim { outpoint: inp.previous_output.clone()});
-                                               }
-                                               hash_map::Entry::Vacant(entry) => {
-                                                       entry.insert(vec![OnchainEvent::Claim { outpoint: inp.previous_output.clone()}]);
-                                               }
-                                       }
-                               }
-                       }
-               }
-               let mut pending_claims = Vec::new();
-               if let Some(ref cur_local_tx) = self.current_local_signed_commitment_tx {
-                       if self.would_broadcast_at_height(height) {
-                               broadcaster.broadcast_transaction(&cur_local_tx.tx);
-                               match self.key_storage {
-                                       Storage::Local { ref delayed_payment_base_key, ref latest_per_commitment_point, .. } => {
-                                               let (txs, mut spendable_output, new_outputs, mut pending_txn) = self.broadcast_by_local_state(&cur_local_tx, latest_per_commitment_point, &Some(*delayed_payment_base_key), height);
-                                               spendable_outputs.append(&mut spendable_output);
-                                               pending_claims.append(&mut pending_txn);
-                                               if !new_outputs.is_empty() {
-                                                       watch_outputs.push((cur_local_tx.txid.clone(), new_outputs));
-                                               }
-                                               for tx in txs {
-                                                       broadcaster.broadcast_transaction(&tx);
-                                               }
-                                       },
-                                       Storage::Watchtower { .. } => {
-                                               let (txs, mut spendable_output, new_outputs, mut pending_txn) = self.broadcast_by_local_state(&cur_local_tx, &None, &None, height);
-                                               spendable_outputs.append(&mut spendable_output);
-                                               pending_claims.append(&mut pending_txn);
-                                               if !new_outputs.is_empty() {
-                                                       watch_outputs.push((cur_local_tx.txid.clone(), new_outputs));
-                                               }
-                                               for tx in txs {
-                                                       broadcaster.broadcast_transaction(&tx);
-                                               }
-                                       }
-                               }
-                       }
-               }
-               for claim in pending_claims {
-                       match self.our_claim_txn_waiting_first_conf.entry(claim.0) {
-                               hash_map::Entry::Occupied(_) => {},
-                               hash_map::Entry::Vacant(entry) => { entry.insert(claim.1); }
-                       }
-               }
-               if let Some(events) = self.onchain_events_waiting_threshold_conf.remove(&height) {
-                       for ev in events {
-                               match ev {
-                                       OnchainEvent::Claim { outpoint } => {
-                                               self.our_claim_txn_waiting_first_conf.remove(&outpoint);
-                                       },
-                                       OnchainEvent::HTLCUpdate { htlc_update } => {
-                                               log_trace!(self, "HTLC {} failure update has got enough confirmations to be passed upstream", log_bytes!((htlc_update.1).0));
-                                               htlc_updated.push((htlc_update.0, None, htlc_update.1));
-                                       },
-                               }
-                       }
-               }
-               //TODO: iter on buffered TxMaterial in our_claim_txn_waiting_first_conf, if block timer is expired generate a bumped claim tx (RBF or CPFP accordingly)
-               self.last_block_hash = block_hash.clone();
-               (watch_outputs, spendable_outputs, htlc_updated)
-       }
-
-       fn block_disconnected(&mut self, height: u32, block_hash: &Sha256dHash) {
-               if let Some(_) = self.onchain_events_waiting_threshold_conf.remove(&(height + ANTI_REORG_DELAY - 1)) {
-                       //We may discard:
-                       //- htlc update there as failure-trigger tx (revoked commitment tx, non-revoked commitment tx, HTLC-timeout tx) has been disconnected
-                       //- our claim tx on a commitment tx output
-               }
-               self.our_claim_txn_waiting_first_conf.retain(|_, ref mut v| if v.3 == height { false } else { true });
-               self.last_block_hash = block_hash.clone();
-       }
-
-       pub(super) fn would_broadcast_at_height(&self, height: u32) -> bool {
-               // We need to consider all HTLCs which are:
-               //  * in any unrevoked remote commitment transaction, as they could broadcast said
-               //    transactions and we'd end up in a race, or
-               //  * are in our latest local commitment transaction, as this is the thing we will
-               //    broadcast if we go on-chain.
-               // Note that we consider HTLCs which were below dust threshold here - while they don't
-               // strictly imply that we need to fail the channel, we need to go ahead and fail them back
-               // to the source, and if we don't fail the channel we will have to ensure that the next
-               // updates that peer sends us are update_fails, failing the channel if not. It's probably
-               // easier to just fail the channel as this case should be rare enough anyway.
-               macro_rules! scan_commitment {
-                       ($htlcs: expr, $local_tx: expr) => {
-                               for ref htlc in $htlcs {
-                                       // For inbound HTLCs which we know the preimage for, we have to ensure we hit the
-                                       // chain with enough room to claim the HTLC without our counterparty being able to
-                                       // time out the HTLC first.
-                                       // For outbound HTLCs which our counterparty hasn't failed/claimed, our primary
-                                       // concern is being able to claim the corresponding inbound HTLC (on another
-                                       // channel) before it expires. In fact, we don't even really care if our
-                                       // counterparty here claims such an outbound HTLC after it expired as long as we
-                                       // can still claim the corresponding HTLC. Thus, to avoid needlessly hitting the
-                                       // chain when our counterparty is waiting for expiration to off-chain fail an HTLC
-                                       // we give ourselves a few blocks of headroom after expiration before going
-                                       // on-chain for an expired HTLC.
-                                       // Note that, to avoid a potential attack whereby a node delays claiming an HTLC
-                                       // from us until we've reached the point where we go on-chain with the
-                                       // corresponding inbound HTLC, we must ensure that outbound HTLCs go on chain at
-                                       // least CLTV_CLAIM_BUFFER blocks prior to the inbound HTLC.
-                                       //  aka outbound_cltv + LATENCY_GRACE_PERIOD_BLOCKS == height - CLTV_CLAIM_BUFFER
-                                       //      inbound_cltv == height + CLTV_CLAIM_BUFFER
-                                       //      outbound_cltv + LATENCY_GRACE_PERIOD_BLOCKS + CLTV_CLAIM_BUFFER <= inbound_cltv - CLTV_CLAIM_BUFFER
-                                       //      LATENCY_GRACE_PERIOD_BLOCKS + 2*CLTV_CLAIM_BUFFER <= inbound_cltv - outbound_cltv
-                                       //      CLTV_EXPIRY_DELTA <= inbound_cltv - outbound_cltv (by check in ChannelManager::decode_update_add_htlc_onion)
-                                       //      LATENCY_GRACE_PERIOD_BLOCKS + 2*CLTV_CLAIM_BUFFER <= CLTV_EXPIRY_DELTA
-                                       //  The final, above, condition is checked for statically in channelmanager
-                                       //  with CHECK_CLTV_EXPIRY_SANITY_2.
-                                       let htlc_outbound = $local_tx == htlc.offered;
-                                       if ( htlc_outbound && htlc.cltv_expiry + LATENCY_GRACE_PERIOD_BLOCKS <= height) ||
-                                          (!htlc_outbound && htlc.cltv_expiry <= height + CLTV_CLAIM_BUFFER && self.payment_preimages.contains_key(&htlc.payment_hash)) {
-                                               log_info!(self, "Force-closing channel due to {} HTLC timeout, HTLC expiry is {}", if htlc_outbound { "outbound" } else { "inbound "}, htlc.cltv_expiry);
-                                               return true;
-                                       }
-                               }
-                       }
-               }
-
-               if let Some(ref cur_local_tx) = self.current_local_signed_commitment_tx {
-                       scan_commitment!(cur_local_tx.htlc_outputs.iter().map(|&(ref a, _, _)| a), true);
-               }
-
-               if let Storage::Local { ref current_remote_commitment_txid, ref prev_remote_commitment_txid, .. } = self.key_storage {
-                       if let &Some(ref txid) = current_remote_commitment_txid {
-                               if let Some(ref htlc_outputs) = self.remote_claimable_outpoints.get(txid) {
-                                       scan_commitment!(htlc_outputs.iter().map(|&(ref a, _)| a), false);
-                               }
-                       }
-                       if let &Some(ref txid) = prev_remote_commitment_txid {
-                               if let Some(ref htlc_outputs) = self.remote_claimable_outpoints.get(txid) {
-                                       scan_commitment!(htlc_outputs.iter().map(|&(ref a, _)| a), false);
-                               }
-                       }
-               }
-
-               false
-       }
-
-       /// Check if any transaction broadcasted is resolving HTLC output by a success or timeout on a local
-       /// or remote commitment tx, if so send back the source, preimage if found and payment_hash of resolved HTLC
-       fn is_resolving_htlc_output(&mut self, tx: &Transaction, height: u32) -> Vec<(HTLCSource, Option<PaymentPreimage>, PaymentHash)> {
-               let mut htlc_updated = Vec::new();
-
-               'outer_loop: for input in &tx.input {
-                       let mut payment_data = None;
-                       let revocation_sig_claim = (input.witness.len() == 3 && input.witness[2].len() == OFFERED_HTLC_SCRIPT_WEIGHT && input.witness[1].len() == 33)
-                               || (input.witness.len() == 3 && input.witness[2].len() == ACCEPTED_HTLC_SCRIPT_WEIGHT && input.witness[1].len() == 33);
-                       let accepted_preimage_claim = input.witness.len() == 5 && input.witness[4].len() == ACCEPTED_HTLC_SCRIPT_WEIGHT;
-                       let offered_preimage_claim = input.witness.len() == 3 && input.witness[2].len() == OFFERED_HTLC_SCRIPT_WEIGHT;
-
-                       macro_rules! log_claim {
-                               ($tx_info: expr, $local_tx: expr, $htlc: expr, $source_avail: expr) => {
-                                       // We found the output in question, but aren't failing it backwards
-                                       // as we have no corresponding source and no valid remote commitment txid
-                                       // to try a weak source binding with same-hash, same-value still-valid offered HTLC.
-                                       // This implies either it is an inbound HTLC or an outbound HTLC on a revoked transaction.
-                                       let outbound_htlc = $local_tx == $htlc.offered;
-                                       if ($local_tx && revocation_sig_claim) ||
-                                                       (outbound_htlc && !$source_avail && (accepted_preimage_claim || offered_preimage_claim)) {
-                                               log_error!(self, "Input spending {} ({}:{}) in {} resolves {} HTLC with payment hash {} with {}!",
-                                                       $tx_info, input.previous_output.txid, input.previous_output.vout, tx.txid(),
-                                                       if outbound_htlc { "outbound" } else { "inbound" }, log_bytes!($htlc.payment_hash.0),
-                                                       if revocation_sig_claim { "revocation sig" } else { "preimage claim after we'd passed the HTLC resolution back" });
-                                       } else {
-                                               log_info!(self, "Input spending {} ({}:{}) in {} resolves {} HTLC with payment hash {} with {}",
-                                                       $tx_info, input.previous_output.txid, input.previous_output.vout, tx.txid(),
-                                                       if outbound_htlc { "outbound" } else { "inbound" }, log_bytes!($htlc.payment_hash.0),
-                                                       if revocation_sig_claim { "revocation sig" } else if accepted_preimage_claim || offered_preimage_claim { "preimage" } else { "timeout" });
-                                       }
-                               }
-                       }
-
-                       macro_rules! check_htlc_valid_remote {
-                               ($remote_txid: expr, $htlc_output: expr) => {
-                                       if let &Some(txid) = $remote_txid {
-                                               for &(ref pending_htlc, ref pending_source) in self.remote_claimable_outpoints.get(&txid).unwrap() {
-                                                       if pending_htlc.payment_hash == $htlc_output.payment_hash && pending_htlc.amount_msat == $htlc_output.amount_msat {
-                                                               if let &Some(ref source) = pending_source {
-                                                                       log_claim!("revoked remote commitment tx", false, pending_htlc, true);
-                                                                       payment_data = Some(((**source).clone(), $htlc_output.payment_hash));
-                                                                       break;
-                                                               }
-                                                       }
-                                               }
-                                       }
-                               }
-                       }
-
-                       macro_rules! scan_commitment {
-                               ($htlcs: expr, $tx_info: expr, $local_tx: expr) => {
-                                       for (ref htlc_output, source_option) in $htlcs {
-                                               if Some(input.previous_output.vout) == htlc_output.transaction_output_index {
-                                                       if let Some(ref source) = source_option {
-                                                               log_claim!($tx_info, $local_tx, htlc_output, true);
-                                                               // We have a resolution of an HTLC either from one of our latest
-                                                               // local commitment transactions or an unrevoked remote commitment
-                                                               // transaction. This implies we either learned a preimage, the HTLC
-                                                               // has timed out, or we screwed up. In any case, we should now
-                                                               // resolve the source HTLC with the original sender.
-                                                               payment_data = Some(((*source).clone(), htlc_output.payment_hash));
-                                                       } else if !$local_tx {
-                                                               if let Storage::Local { ref current_remote_commitment_txid, .. } = self.key_storage {
-                                                                       check_htlc_valid_remote!(current_remote_commitment_txid, htlc_output);
-                                                               }
-                                                               if payment_data.is_none() {
-                                                                       if let Storage::Local { ref prev_remote_commitment_txid, .. } = self.key_storage {
-                                                                               check_htlc_valid_remote!(prev_remote_commitment_txid, htlc_output);
-                                                                       }
-                                                               }
-                                                       }
-                                                       if payment_data.is_none() {
-                                                               log_claim!($tx_info, $local_tx, htlc_output, false);
-                                                               continue 'outer_loop;
-                                                       }
-                                               }
-                                       }
-                               }
-                       }
-
-                       if let Some(ref current_local_signed_commitment_tx) = self.current_local_signed_commitment_tx {
-                               if input.previous_output.txid == current_local_signed_commitment_tx.txid {
-                                       scan_commitment!(current_local_signed_commitment_tx.htlc_outputs.iter().map(|&(ref a, _, ref b)| (a, b.as_ref())),
-                                               "our latest local commitment tx", true);
-                               }
-                       }
-                       if let Some(ref prev_local_signed_commitment_tx) = self.prev_local_signed_commitment_tx {
-                               if input.previous_output.txid == prev_local_signed_commitment_tx.txid {
-                                       scan_commitment!(prev_local_signed_commitment_tx.htlc_outputs.iter().map(|&(ref a, _, ref b)| (a, b.as_ref())),
-                                               "our previous local commitment tx", true);
-                               }
-                       }
-                       if let Some(ref htlc_outputs) = self.remote_claimable_outpoints.get(&input.previous_output.txid) {
-                               scan_commitment!(htlc_outputs.iter().map(|&(ref a, ref b)| (a, (b.as_ref().clone()).map(|boxed| &**boxed))),
-                                       "remote commitment tx", false);
-                       }
-
-                       // Check that scan_commitment, above, decided there is some source worth relaying an
-                       // HTLC resolution backwards to and figure out whether we learned a preimage from it.
-                       if let Some((source, payment_hash)) = payment_data {
-                               let mut payment_preimage = PaymentPreimage([0; 32]);
-                               if accepted_preimage_claim {
-                                       payment_preimage.0.copy_from_slice(&input.witness[3]);
-                                       htlc_updated.push((source, Some(payment_preimage), payment_hash));
-                               } else if offered_preimage_claim {
-                                       payment_preimage.0.copy_from_slice(&input.witness[1]);
-                                       htlc_updated.push((source, Some(payment_preimage), payment_hash));
-                               } else {
-                                       log_info!(self, "Failing HTLC with payment_hash {} timeout by a spend tx, waiting for confirmation (at height{})", log_bytes!(payment_hash.0), height + ANTI_REORG_DELAY - 1);
-                                       match self.onchain_events_waiting_threshold_conf.entry(height + ANTI_REORG_DELAY - 1) {
-                                               hash_map::Entry::Occupied(mut entry) => {
-                                                       let e = entry.get_mut();
-                                                       e.retain(|ref event| {
-                                                               match **event {
-                                                                       OnchainEvent::HTLCUpdate { ref htlc_update } => {
-                                                                               return htlc_update.0 != source
-                                                                       },
-                                                                       _ => return true
-                                                               }
-                                                       });
-                                                       e.push(OnchainEvent::HTLCUpdate { htlc_update: (source, payment_hash)});
-                                               }
-                                               hash_map::Entry::Vacant(entry) => {
-                                                       entry.insert(vec![OnchainEvent::HTLCUpdate { htlc_update: (source, payment_hash)}]);
-                                               }
-                                       }
-                               }
-                       }
-               }
-               htlc_updated
-       }
-}
-
-const MAX_ALLOC_SIZE: usize = 64*1024;
-
-impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelMonitor) {
-       fn read(reader: &mut R, logger: Arc<Logger>) -> Result<Self, DecodeError> {
-               let secp_ctx = Secp256k1::new();
-               macro_rules! unwrap_obj {
-                       ($key: expr) => {
-                               match $key {
-                                       Ok(res) => res,
-                                       Err(_) => return Err(DecodeError::InvalidValue),
-                               }
-                       }
-               }
-
-               let _ver: u8 = Readable::read(reader)?;
-               let min_ver: u8 = Readable::read(reader)?;
-               if min_ver > SERIALIZATION_VERSION {
-                       return Err(DecodeError::UnknownVersion);
-               }
-
-               let commitment_transaction_number_obscure_factor = <U48 as Readable<R>>::read(reader)?.0;
-
-               let key_storage = match <u8 as Readable<R>>::read(reader)? {
-                       0 => {
-                               let revocation_base_key = Readable::read(reader)?;
-                               let htlc_base_key = Readable::read(reader)?;
-                               let delayed_payment_base_key = Readable::read(reader)?;
-                               let payment_base_key = Readable::read(reader)?;
-                               let shutdown_pubkey = Readable::read(reader)?;
-                               let prev_latest_per_commitment_point = Readable::read(reader)?;
-                               let latest_per_commitment_point = Readable::read(reader)?;
-                               // Technically this can fail and serialize fail a round-trip, but only for serialization of
-                               // barely-init'd ChannelMonitors that we can't do anything with.
-                               let outpoint = OutPoint {
-                                       txid: Readable::read(reader)?,
-                                       index: Readable::read(reader)?,
-                               };
-                               let funding_info = Some((outpoint, Readable::read(reader)?));
-                               let current_remote_commitment_txid = Readable::read(reader)?;
-                               let prev_remote_commitment_txid = Readable::read(reader)?;
-                               Storage::Local {
-                                       revocation_base_key,
-                                       htlc_base_key,
-                                       delayed_payment_base_key,
-                                       payment_base_key,
-                                       shutdown_pubkey,
-                                       prev_latest_per_commitment_point,
-                                       latest_per_commitment_point,
-                                       funding_info,
-                                       current_remote_commitment_txid,
-                                       prev_remote_commitment_txid,
-                               }
-                       },
-                       _ => return Err(DecodeError::InvalidValue),
-               };
-
-               let their_htlc_base_key = Some(Readable::read(reader)?);
-               let their_delayed_payment_base_key = Some(Readable::read(reader)?);
-
-               let their_cur_revocation_points = {
-                       let first_idx = <U48 as Readable<R>>::read(reader)?.0;
-                       if first_idx == 0 {
-                               None
-                       } else {
-                               let first_point = Readable::read(reader)?;
-                               let second_point_slice: [u8; 33] = Readable::read(reader)?;
-                               if second_point_slice[0..32] == [0; 32] && second_point_slice[32] == 0 {
-                                       Some((first_idx, first_point, None))
-                               } else {
-                                       Some((first_idx, first_point, Some(unwrap_obj!(PublicKey::from_slice(&second_point_slice)))))
-                               }
-                       }
-               };
-
-               let our_to_self_delay: u16 = Readable::read(reader)?;
-               let their_to_self_delay: Option<u16> = Some(Readable::read(reader)?);
-
-               let mut old_secrets = [([0; 32], 1 << 48); 49];
-               for &mut (ref mut secret, ref mut idx) in old_secrets.iter_mut() {
-                       *secret = Readable::read(reader)?;
-                       *idx = Readable::read(reader)?;
-               }
-
-               macro_rules! read_htlc_in_commitment {
-                       () => {
-                               {
-                                       let offered: bool = Readable::read(reader)?;
-                                       let amount_msat: u64 = Readable::read(reader)?;
-                                       let cltv_expiry: u32 = Readable::read(reader)?;
-                                       let payment_hash: PaymentHash = Readable::read(reader)?;
-                                       let transaction_output_index: Option<u32> = Readable::read(reader)?;
-
-                                       HTLCOutputInCommitment {
-                                               offered, amount_msat, cltv_expiry, payment_hash, transaction_output_index
-                                       }
-                               }
-                       }
-               }
-
-               let remote_claimable_outpoints_len: u64 = Readable::read(reader)?;
-               let mut remote_claimable_outpoints = HashMap::with_capacity(cmp::min(remote_claimable_outpoints_len as usize, MAX_ALLOC_SIZE / 64));
-               for _ in 0..remote_claimable_outpoints_len {
-                       let txid: Sha256dHash = Readable::read(reader)?;
-                       let htlcs_count: u64 = Readable::read(reader)?;
-                       let mut htlcs = Vec::with_capacity(cmp::min(htlcs_count as usize, MAX_ALLOC_SIZE / 32));
-                       for _ in 0..htlcs_count {
-                               htlcs.push((read_htlc_in_commitment!(), <Option<HTLCSource> as Readable<R>>::read(reader)?.map(|o: HTLCSource| Box::new(o))));
-                       }
-                       if let Some(_) = remote_claimable_outpoints.insert(txid, htlcs) {
-                               return Err(DecodeError::InvalidValue);
-                       }
-               }
-
-               let remote_commitment_txn_on_chain_len: u64 = Readable::read(reader)?;
-               let mut remote_commitment_txn_on_chain = HashMap::with_capacity(cmp::min(remote_commitment_txn_on_chain_len as usize, MAX_ALLOC_SIZE / 32));
-               for _ in 0..remote_commitment_txn_on_chain_len {
-                       let txid: Sha256dHash = Readable::read(reader)?;
-                       let commitment_number = <U48 as Readable<R>>::read(reader)?.0;
-                       let outputs_count = <u64 as Readable<R>>::read(reader)?;
-                       let mut outputs = Vec::with_capacity(cmp::min(outputs_count as usize, MAX_ALLOC_SIZE / 8));
-                       for _ in 0..outputs_count {
-                               outputs.push(Readable::read(reader)?);
-                       }
-                       if let Some(_) = remote_commitment_txn_on_chain.insert(txid, (commitment_number, outputs)) {
-                               return Err(DecodeError::InvalidValue);
-                       }
-               }
-
-               let remote_hash_commitment_number_len: u64 = Readable::read(reader)?;
-               let mut remote_hash_commitment_number = HashMap::with_capacity(cmp::min(remote_hash_commitment_number_len as usize, MAX_ALLOC_SIZE / 32));
-               for _ in 0..remote_hash_commitment_number_len {
-                       let payment_hash: PaymentHash = Readable::read(reader)?;
-                       let commitment_number = <U48 as Readable<R>>::read(reader)?.0;
-                       if let Some(_) = remote_hash_commitment_number.insert(payment_hash, commitment_number) {
-                               return Err(DecodeError::InvalidValue);
-                       }
-               }
-
-               macro_rules! read_local_tx {
-                       () => {
-                               {
-                                       let tx = match Transaction::consensus_decode(reader.by_ref()) {
-                                               Ok(tx) => tx,
-                                               Err(e) => match e {
-                                                       encode::Error::Io(ioe) => return Err(DecodeError::Io(ioe)),
-                                                       _ => return Err(DecodeError::InvalidValue),
-                                               },
-                                       };
-
-                                       if tx.input.is_empty() {
-                                               // Ensure tx didn't hit the 0-input ambiguity case.
-                                               return Err(DecodeError::InvalidValue);
-                                       }
-
-                                       let revocation_key = Readable::read(reader)?;
-                                       let a_htlc_key = Readable::read(reader)?;
-                                       let b_htlc_key = Readable::read(reader)?;
-                                       let delayed_payment_key = Readable::read(reader)?;
-                                       let feerate_per_kw: u64 = Readable::read(reader)?;
-
-                                       let htlcs_len: u64 = Readable::read(reader)?;
-                                       let mut htlcs = Vec::with_capacity(cmp::min(htlcs_len as usize, MAX_ALLOC_SIZE / 128));
-                                       for _ in 0..htlcs_len {
-                                               let htlc = read_htlc_in_commitment!();
-                                               let sigs = match <u8 as Readable<R>>::read(reader)? {
-                                                       0 => None,
-                                                       1 => Some((Readable::read(reader)?, Readable::read(reader)?)),
-                                                       _ => return Err(DecodeError::InvalidValue),
-                                               };
-                                               htlcs.push((htlc, sigs, Readable::read(reader)?));
-                                       }
-
-                                       LocalSignedTx {
-                                               txid: tx.txid(),
-                                               tx, revocation_key, a_htlc_key, b_htlc_key, delayed_payment_key, feerate_per_kw,
-                                               htlc_outputs: htlcs
-                                       }
-                               }
-                       }
-               }
-
-               let prev_local_signed_commitment_tx = match <u8 as Readable<R>>::read(reader)? {
-                       0 => None,
-                       1 => {
-                               Some(read_local_tx!())
-                       },
-                       _ => return Err(DecodeError::InvalidValue),
-               };
-
-               let current_local_signed_commitment_tx = match <u8 as Readable<R>>::read(reader)? {
-                       0 => None,
-                       1 => {
-                               Some(read_local_tx!())
-                       },
-                       _ => return Err(DecodeError::InvalidValue),
-               };
-
-               let current_remote_commitment_number = <U48 as Readable<R>>::read(reader)?.0;
-
-               let payment_preimages_len: u64 = Readable::read(reader)?;
-               let mut payment_preimages = HashMap::with_capacity(cmp::min(payment_preimages_len as usize, MAX_ALLOC_SIZE / 32));
-               for _ in 0..payment_preimages_len {
-                       let preimage: PaymentPreimage = Readable::read(reader)?;
-                       let hash = PaymentHash(Sha256::hash(&preimage.0[..]).into_inner());
-                       if let Some(_) = payment_preimages.insert(hash, preimage) {
-                               return Err(DecodeError::InvalidValue);
-                       }
-               }
-
-               let last_block_hash: Sha256dHash = Readable::read(reader)?;
-               let destination_script = Readable::read(reader)?;
-               let to_remote_rescue = match <u8 as Readable<R>>::read(reader)? {
-                       0 => None,
-                       1 => {
-                               let to_remote_script = Readable::read(reader)?;
-                               let local_key = Readable::read(reader)?;
-                               Some((to_remote_script, local_key))
-                       }
-                       _ => return Err(DecodeError::InvalidValue),
-               };
-
-               let our_claim_txn_waiting_first_conf_len: u64 = Readable::read(reader)?;
-               let mut our_claim_txn_waiting_first_conf = HashMap::with_capacity(cmp::min(our_claim_txn_waiting_first_conf_len as usize, MAX_ALLOC_SIZE / 128));
-               for _ in 0..our_claim_txn_waiting_first_conf_len {
-                       let outpoint = Readable::read(reader)?;
-                       let height_target = Readable::read(reader)?;
-                       let tx_material = match <u8 as Readable<R>>::read(reader)? {
-                               0 => {
-                                       let script = Readable::read(reader)?;
-                                       let pubkey = Readable::read(reader)?;
-                                       let key = Readable::read(reader)?;
-                                       let is_htlc = match <u8 as Readable<R>>::read(reader)? {
-                                               0 => true,
-                                               1 => false,
-                                               _ => return Err(DecodeError::InvalidValue),
-                                       };
-                                       let amount = Readable::read(reader)?;
-                                       TxMaterial::Revoked {
-                                               script,
-                                               pubkey,
-                                               key,
-                                               is_htlc,
-                                               amount
-                                       }
-                               },
-                               1 => {
-                                       let script = Readable::read(reader)?;
-                                       let key = Readable::read(reader)?;
-                                       let preimage = Readable::read(reader)?;
-                                       let amount = Readable::read(reader)?;
-                                       TxMaterial::RemoteHTLC {
-                                               script,
-                                               key,
-                                               preimage,
-                                               amount
-                                       }
-                               },
-                               2 => {
-                                       let script = Readable::read(reader)?;
-                                       let their_sig = Readable::read(reader)?;
-                                       let our_sig = Readable::read(reader)?;
-                                       let preimage = Readable::read(reader)?;
-                                       let amount = Readable::read(reader)?;
-                                       TxMaterial::LocalHTLC {
-                                               script,
-                                               sigs: (their_sig, our_sig),
-                                               preimage,
-                                               amount
-                                       }
-                               }
-                               _ => return Err(DecodeError::InvalidValue),
-                       };
-                       let last_fee = Readable::read(reader)?;
-                       let timelock_expiration = Readable::read(reader)?;
-                       let height = Readable::read(reader)?;
-                       our_claim_txn_waiting_first_conf.insert(outpoint, (height_target, tx_material, last_fee, timelock_expiration, height));
-               }
-
-               let waiting_threshold_conf_len: u64 = Readable::read(reader)?;
-               let mut onchain_events_waiting_threshold_conf = HashMap::with_capacity(cmp::min(waiting_threshold_conf_len as usize, MAX_ALLOC_SIZE / 128));
-               for _ in 0..waiting_threshold_conf_len {
-                       let height_target = Readable::read(reader)?;
-                       let events_len: u64 = Readable::read(reader)?;
-                       let mut events = Vec::with_capacity(cmp::min(events_len as usize, MAX_ALLOC_SIZE / 128));
-                       for _ in 0..events_len {
-                               let ev = match <u8 as Readable<R>>::read(reader)? {
-                                       0 => {
-                                               let outpoint = Readable::read(reader)?;
-                                               OnchainEvent::Claim {
-                                                       outpoint
-                                               }
-                                       },
-                                       1 => {
-                                               let htlc_source = Readable::read(reader)?;
-                                               let hash = Readable::read(reader)?;
-                                               OnchainEvent::HTLCUpdate {
-                                                       htlc_update: (htlc_source, hash)
-                                               }
-                                       },
-                                       _ => return Err(DecodeError::InvalidValue),
-                               };
-                               events.push(ev);
-                       }
-                       onchain_events_waiting_threshold_conf.insert(height_target, events);
-               }
-
-               Ok((last_block_hash.clone(), ChannelMonitor {
-                       commitment_transaction_number_obscure_factor,
-
-                       key_storage,
-                       their_htlc_base_key,
-                       their_delayed_payment_base_key,
-                       their_cur_revocation_points,
-
-                       our_to_self_delay,
-                       their_to_self_delay,
-
-                       old_secrets,
-                       remote_claimable_outpoints,
-                       remote_commitment_txn_on_chain,
-                       remote_hash_commitment_number,
-
-                       prev_local_signed_commitment_tx,
-                       current_local_signed_commitment_tx,
-                       current_remote_commitment_number,
-
-                       payment_preimages,
-
-                       destination_script,
-                       to_remote_rescue,
-
-                       our_claim_txn_waiting_first_conf,
-
-                       onchain_events_waiting_threshold_conf,
-
-                       last_block_hash,
-                       secp_ctx,
-                       logger,
-               }))
-       }
-
-}
-
-#[cfg(test)]
-mod tests {
-       use bitcoin::blockdata::script::{Script, Builder};
-       use bitcoin::blockdata::opcodes;
-       use bitcoin::blockdata::transaction::{Transaction, TxIn, TxOut, SigHashType};
-       use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
-       use bitcoin::util::bip143;
-       use bitcoin_hashes::Hash;
-       use bitcoin_hashes::sha256::Hash as Sha256;
-       use bitcoin_hashes::sha256d::Hash as Sha256dHash;
-       use bitcoin_hashes::hex::FromHex;
-       use hex;
-       use ln::channelmanager::{PaymentPreimage, PaymentHash};
-       use ln::channelmonitor::{ChannelMonitor, InputDescriptors};
-       use ln::chan_utils;
-       use ln::chan_utils::{HTLCOutputInCommitment, TxCreationKeys};
-       use util::test_utils::TestLogger;
-       use secp256k1::key::{SecretKey,PublicKey};
-       use secp256k1::Secp256k1;
-       use rand::{thread_rng,Rng};
-       use std::sync::Arc;
-
-       #[test]
-       fn test_per_commitment_storage() {
-               // Test vectors from BOLT 3:
-               let mut secrets: Vec<[u8; 32]> = Vec::new();
-               let mut monitor: ChannelMonitor;
-               let secp_ctx = Secp256k1::new();
-               let logger = Arc::new(TestLogger::new());
-
-               macro_rules! test_secrets {
-                       () => {
-                               let mut idx = 281474976710655;
-                               for secret in secrets.iter() {
-                                       assert_eq!(monitor.get_secret(idx).unwrap(), *secret);
-                                       idx -= 1;
-                               }
-                               assert_eq!(monitor.get_min_seen_secret(), idx + 1);
-                               assert!(monitor.get_secret(idx).is_none());
-                       };
-               }
-
-               {
-                       // insert_secret correct sequence
-                       monitor = ChannelMonitor::new(&SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
-                       secrets.clear();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("7cc854b54e3e0dcdb010d7a3fee464a9687be6e8db3be6854c475621e007a5dc").unwrap());
-                       monitor.provide_secret(281474976710655, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c7518c8ae4660ed02894df8976fa1a3659c1a8b4b5bec0c4b872abeba4cb8964").unwrap());
-                       monitor.provide_secret(281474976710654, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("2273e227a5b7449b6e70f1fb4652864038b1cbf9cd7c043a7d6456b7fc275ad8").unwrap());
-                       monitor.provide_secret(281474976710653, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116").unwrap());
-                       monitor.provide_secret(281474976710652, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c65716add7aa98ba7acb236352d665cab17345fe45b55fb879ff80e6bd0c41dd").unwrap());
-                       monitor.provide_secret(281474976710651, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("969660042a28f32d9be17344e09374b379962d03db1574df5a8a5a47e19ce3f2").unwrap());
-                       monitor.provide_secret(281474976710650, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("a5a64476122ca0925fb344bdc1854c1c0a59fc614298e50a33e331980a220f32").unwrap());
-                       monitor.provide_secret(281474976710649, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("05cde6323d949933f7f7b78776bcc1ea6d9b31447732e3802e1f7ac44b650e17").unwrap());
-                       monitor.provide_secret(281474976710648, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-               }
-
-               {
-                       // insert_secret #1 incorrect
-                       monitor = ChannelMonitor::new(&SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
-                       secrets.clear();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("02a40c85b6f28da08dfdbe0926c53fab2de6d28c10301f8f7c4073d5e42e3148").unwrap());
-                       monitor.provide_secret(281474976710655, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c7518c8ae4660ed02894df8976fa1a3659c1a8b4b5bec0c4b872abeba4cb8964").unwrap());
-                       assert_eq!(monitor.provide_secret(281474976710654, secrets.last().unwrap().clone()).unwrap_err().0,
-                                       "Previous secret did not match new one");
-               }
-
-               {
-                       // insert_secret #2 incorrect (#1 derived from incorrect)
-                       monitor = ChannelMonitor::new(&SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
-                       secrets.clear();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("02a40c85b6f28da08dfdbe0926c53fab2de6d28c10301f8f7c4073d5e42e3148").unwrap());
-                       monitor.provide_secret(281474976710655, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("dddc3a8d14fddf2b68fa8c7fbad2748274937479dd0f8930d5ebb4ab6bd866a3").unwrap());
-                       monitor.provide_secret(281474976710654, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("2273e227a5b7449b6e70f1fb4652864038b1cbf9cd7c043a7d6456b7fc275ad8").unwrap());
-                       monitor.provide_secret(281474976710653, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116").unwrap());
-                       assert_eq!(monitor.provide_secret(281474976710652, secrets.last().unwrap().clone()).unwrap_err().0,
-                                       "Previous secret did not match new one");
-               }
-
-               {
-                       // insert_secret #3 incorrect
-                       monitor = ChannelMonitor::new(&SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
-                       secrets.clear();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("7cc854b54e3e0dcdb010d7a3fee464a9687be6e8db3be6854c475621e007a5dc").unwrap());
-                       monitor.provide_secret(281474976710655, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c7518c8ae4660ed02894df8976fa1a3659c1a8b4b5bec0c4b872abeba4cb8964").unwrap());
-                       monitor.provide_secret(281474976710654, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c51a18b13e8527e579ec56365482c62f180b7d5760b46e9477dae59e87ed423a").unwrap());
-                       monitor.provide_secret(281474976710653, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116").unwrap());
-                       assert_eq!(monitor.provide_secret(281474976710652, secrets.last().unwrap().clone()).unwrap_err().0,
-                                       "Previous secret did not match new one");
-               }
-
-               {
-                       // insert_secret #4 incorrect (1,2,3 derived from incorrect)
-                       monitor = ChannelMonitor::new(&SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
-                       secrets.clear();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("02a40c85b6f28da08dfdbe0926c53fab2de6d28c10301f8f7c4073d5e42e3148").unwrap());
-                       monitor.provide_secret(281474976710655, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("dddc3a8d14fddf2b68fa8c7fbad2748274937479dd0f8930d5ebb4ab6bd866a3").unwrap());
-                       monitor.provide_secret(281474976710654, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c51a18b13e8527e579ec56365482c62f180b7d5760b46e9477dae59e87ed423a").unwrap());
-                       monitor.provide_secret(281474976710653, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("ba65d7b0ef55a3ba300d4e87af29868f394f8f138d78a7011669c79b37b936f4").unwrap());
-                       monitor.provide_secret(281474976710652, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c65716add7aa98ba7acb236352d665cab17345fe45b55fb879ff80e6bd0c41dd").unwrap());
-                       monitor.provide_secret(281474976710651, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("969660042a28f32d9be17344e09374b379962d03db1574df5a8a5a47e19ce3f2").unwrap());
-                       monitor.provide_secret(281474976710650, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("a5a64476122ca0925fb344bdc1854c1c0a59fc614298e50a33e331980a220f32").unwrap());
-                       monitor.provide_secret(281474976710649, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("05cde6323d949933f7f7b78776bcc1ea6d9b31447732e3802e1f7ac44b650e17").unwrap());
-                       assert_eq!(monitor.provide_secret(281474976710648, secrets.last().unwrap().clone()).unwrap_err().0,
-                                       "Previous secret did not match new one");
-               }
-
-               {
-                       // insert_secret #5 incorrect
-                       monitor = ChannelMonitor::new(&SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
-                       secrets.clear();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("7cc854b54e3e0dcdb010d7a3fee464a9687be6e8db3be6854c475621e007a5dc").unwrap());
-                       monitor.provide_secret(281474976710655, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c7518c8ae4660ed02894df8976fa1a3659c1a8b4b5bec0c4b872abeba4cb8964").unwrap());
-                       monitor.provide_secret(281474976710654, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("2273e227a5b7449b6e70f1fb4652864038b1cbf9cd7c043a7d6456b7fc275ad8").unwrap());
-                       monitor.provide_secret(281474976710653, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116").unwrap());
-                       monitor.provide_secret(281474976710652, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("631373ad5f9ef654bb3dade742d09504c567edd24320d2fcd68e3cc47e2ff6a6").unwrap());
-                       monitor.provide_secret(281474976710651, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("969660042a28f32d9be17344e09374b379962d03db1574df5a8a5a47e19ce3f2").unwrap());
-                       assert_eq!(monitor.provide_secret(281474976710650, secrets.last().unwrap().clone()).unwrap_err().0,
-                                       "Previous secret did not match new one");
-               }
-
-               {
-                       // insert_secret #6 incorrect (5 derived from incorrect)
-                       monitor = ChannelMonitor::new(&SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
-                       secrets.clear();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("7cc854b54e3e0dcdb010d7a3fee464a9687be6e8db3be6854c475621e007a5dc").unwrap());
-                       monitor.provide_secret(281474976710655, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c7518c8ae4660ed02894df8976fa1a3659c1a8b4b5bec0c4b872abeba4cb8964").unwrap());
-                       monitor.provide_secret(281474976710654, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("2273e227a5b7449b6e70f1fb4652864038b1cbf9cd7c043a7d6456b7fc275ad8").unwrap());
-                       monitor.provide_secret(281474976710653, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116").unwrap());
-                       monitor.provide_secret(281474976710652, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("631373ad5f9ef654bb3dade742d09504c567edd24320d2fcd68e3cc47e2ff6a6").unwrap());
-                       monitor.provide_secret(281474976710651, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("b7e76a83668bde38b373970155c868a653304308f9896692f904a23731224bb1").unwrap());
-                       monitor.provide_secret(281474976710650, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("a5a64476122ca0925fb344bdc1854c1c0a59fc614298e50a33e331980a220f32").unwrap());
-                       monitor.provide_secret(281474976710649, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("05cde6323d949933f7f7b78776bcc1ea6d9b31447732e3802e1f7ac44b650e17").unwrap());
-                       assert_eq!(monitor.provide_secret(281474976710648, secrets.last().unwrap().clone()).unwrap_err().0,
-                                       "Previous secret did not match new one");
-               }
-
-               {
-                       // insert_secret #7 incorrect
-                       monitor = ChannelMonitor::new(&SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
-                       secrets.clear();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("7cc854b54e3e0dcdb010d7a3fee464a9687be6e8db3be6854c475621e007a5dc").unwrap());
-                       monitor.provide_secret(281474976710655, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c7518c8ae4660ed02894df8976fa1a3659c1a8b4b5bec0c4b872abeba4cb8964").unwrap());
-                       monitor.provide_secret(281474976710654, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("2273e227a5b7449b6e70f1fb4652864038b1cbf9cd7c043a7d6456b7fc275ad8").unwrap());
-                       monitor.provide_secret(281474976710653, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116").unwrap());
-                       monitor.provide_secret(281474976710652, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c65716add7aa98ba7acb236352d665cab17345fe45b55fb879ff80e6bd0c41dd").unwrap());
-                       monitor.provide_secret(281474976710651, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("969660042a28f32d9be17344e09374b379962d03db1574df5a8a5a47e19ce3f2").unwrap());
-                       monitor.provide_secret(281474976710650, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("e7971de736e01da8ed58b94c2fc216cb1dca9e326f3a96e7194fe8ea8af6c0a3").unwrap());
-                       monitor.provide_secret(281474976710649, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("05cde6323d949933f7f7b78776bcc1ea6d9b31447732e3802e1f7ac44b650e17").unwrap());
-                       assert_eq!(monitor.provide_secret(281474976710648, secrets.last().unwrap().clone()).unwrap_err().0,
-                                       "Previous secret did not match new one");
-               }
-
-               {
-                       // insert_secret #8 incorrect
-                       monitor = ChannelMonitor::new(&SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
-                       secrets.clear();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("7cc854b54e3e0dcdb010d7a3fee464a9687be6e8db3be6854c475621e007a5dc").unwrap());
-                       monitor.provide_secret(281474976710655, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c7518c8ae4660ed02894df8976fa1a3659c1a8b4b5bec0c4b872abeba4cb8964").unwrap());
-                       monitor.provide_secret(281474976710654, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("2273e227a5b7449b6e70f1fb4652864038b1cbf9cd7c043a7d6456b7fc275ad8").unwrap());
-                       monitor.provide_secret(281474976710653, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116").unwrap());
-                       monitor.provide_secret(281474976710652, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c65716add7aa98ba7acb236352d665cab17345fe45b55fb879ff80e6bd0c41dd").unwrap());
-                       monitor.provide_secret(281474976710651, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("969660042a28f32d9be17344e09374b379962d03db1574df5a8a5a47e19ce3f2").unwrap());
-                       monitor.provide_secret(281474976710650, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("a5a64476122ca0925fb344bdc1854c1c0a59fc614298e50a33e331980a220f32").unwrap());
-                       monitor.provide_secret(281474976710649, secrets.last().unwrap().clone()).unwrap();
-                       test_secrets!();
-
-                       secrets.push([0; 32]);
-                       secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("a7efbc61aac46d34f77778bac22c8a20c6a46ca460addc49009bda875ec88fa4").unwrap());
-                       assert_eq!(monitor.provide_secret(281474976710648, secrets.last().unwrap().clone()).unwrap_err().0,
-                                       "Previous secret did not match new one");
-               }
-       }
-
-       #[test]
-       fn test_prune_preimages() {
-               let secp_ctx = Secp256k1::new();
-               let logger = Arc::new(TestLogger::new());
-
-               let dummy_key = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
-               macro_rules! dummy_keys {
-                       () => {
-                               {
-                                       TxCreationKeys {
-                                               per_commitment_point: dummy_key.clone(),
-                                               revocation_key: dummy_key.clone(),
-                                               a_htlc_key: dummy_key.clone(),
-                                               b_htlc_key: dummy_key.clone(),
-                                               a_delayed_payment_key: dummy_key.clone(),
-                                               b_payment_key: dummy_key.clone(),
-                                       }
-                               }
-                       }
-               }
-               let dummy_tx = Transaction { version: 0, lock_time: 0, input: Vec::new(), output: Vec::new() };
-
-               let mut preimages = Vec::new();
-               {
-                       let mut rng  = thread_rng();
-                       for _ in 0..20 {
-                               let mut preimage = PaymentPreimage([0; 32]);
-                               rng.fill_bytes(&mut preimage.0[..]);
-                               let hash = PaymentHash(Sha256::hash(&preimage.0[..]).into_inner());
-                               preimages.push((preimage, hash));
-                       }
-               }
-
-               macro_rules! preimages_slice_to_htlc_outputs {
-                       ($preimages_slice: expr) => {
-                               {
-                                       let mut res = Vec::new();
-                                       for (idx, preimage) in $preimages_slice.iter().enumerate() {
-                                               res.push((HTLCOutputInCommitment {
-                                                       offered: true,
-                                                       amount_msat: 0,
-                                                       cltv_expiry: 0,
-                                                       payment_hash: preimage.1.clone(),
-                                                       transaction_output_index: Some(idx as u32),
-                                               }, None));
-                                       }
-                                       res
-                               }
-                       }
-               }
-               macro_rules! preimages_to_local_htlcs {
-                       ($preimages_slice: expr) => {
-                               {
-                                       let mut inp = preimages_slice_to_htlc_outputs!($preimages_slice);
-                                       let res: Vec<_> = inp.drain(..).map(|e| { (e.0, None, e.1) }).collect();
-                                       res
-                               }
-                       }
-               }
-
-               macro_rules! test_preimages_exist {
-                       ($preimages_slice: expr, $monitor: expr) => {
-                               for preimage in $preimages_slice {
-                                       assert!($monitor.payment_preimages.contains_key(&preimage.1));
-                               }
-                       }
-               }
-
-               // Prune with one old state and a local commitment tx holding a few overlaps with the
-               // old state.
-               let mut monitor = ChannelMonitor::new(&SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
-               monitor.set_their_to_self_delay(10);
-
-               monitor.provide_latest_local_commitment_tx_info(dummy_tx.clone(), dummy_keys!(), 0, preimages_to_local_htlcs!(preimages[0..10]));
-               monitor.provide_latest_remote_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[5..15]), 281474976710655, dummy_key);
-               monitor.provide_latest_remote_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[15..20]), 281474976710654, dummy_key);
-               monitor.provide_latest_remote_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[17..20]), 281474976710653, dummy_key);
-               monitor.provide_latest_remote_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[18..20]), 281474976710652, dummy_key);
-               for &(ref preimage, ref hash) in preimages.iter() {
-                       monitor.provide_payment_preimage(hash, preimage);
-               }
-
-               // Now provide a secret, pruning preimages 10-15
-               let mut secret = [0; 32];
-               secret[0..32].clone_from_slice(&hex::decode("7cc854b54e3e0dcdb010d7a3fee464a9687be6e8db3be6854c475621e007a5dc").unwrap());
-               monitor.provide_secret(281474976710655, secret.clone()).unwrap();
-               assert_eq!(monitor.payment_preimages.len(), 15);
-               test_preimages_exist!(&preimages[0..10], monitor);
-               test_preimages_exist!(&preimages[15..20], monitor);
-
-               // Now provide a further secret, pruning preimages 15-17
-               secret[0..32].clone_from_slice(&hex::decode("c7518c8ae4660ed02894df8976fa1a3659c1a8b4b5bec0c4b872abeba4cb8964").unwrap());
-               monitor.provide_secret(281474976710654, secret.clone()).unwrap();
-               assert_eq!(monitor.payment_preimages.len(), 13);
-               test_preimages_exist!(&preimages[0..10], monitor);
-               test_preimages_exist!(&preimages[17..20], monitor);
-
-               // Now update local commitment tx info, pruning only element 18 as we still care about the
-               // previous commitment tx's preimages too
-               monitor.provide_latest_local_commitment_tx_info(dummy_tx.clone(), dummy_keys!(), 0, preimages_to_local_htlcs!(preimages[0..5]));
-               secret[0..32].clone_from_slice(&hex::decode("2273e227a5b7449b6e70f1fb4652864038b1cbf9cd7c043a7d6456b7fc275ad8").unwrap());
-               monitor.provide_secret(281474976710653, secret.clone()).unwrap();
-               assert_eq!(monitor.payment_preimages.len(), 12);
-               test_preimages_exist!(&preimages[0..10], monitor);
-               test_preimages_exist!(&preimages[18..20], monitor);
-
-               // But if we do it again, we'll prune 5-10
-               monitor.provide_latest_local_commitment_tx_info(dummy_tx.clone(), dummy_keys!(), 0, preimages_to_local_htlcs!(preimages[0..3]));
-               secret[0..32].clone_from_slice(&hex::decode("27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116").unwrap());
-               monitor.provide_secret(281474976710652, secret.clone()).unwrap();
-               assert_eq!(monitor.payment_preimages.len(), 5);
-               test_preimages_exist!(&preimages[0..5], monitor);
-       }
-
-       #[test]
-       fn test_claim_txn_weight_computation() {
-               // We test Claim txn weight, knowing that we want expected weigth and
-               // not actual case to avoid sigs and time-lock delays hell variances.
-
-               let secp_ctx = Secp256k1::new();
-               let privkey = SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap();
-               let pubkey = PublicKey::from_secret_key(&secp_ctx, &privkey);
-               let mut sum_actual_sigs = 0;
-
-               macro_rules! sign_input {
-                       ($sighash_parts: expr, $input: expr, $idx: expr, $amount: expr, $input_type: expr, $sum_actual_sigs: expr) => {
-                               let htlc = HTLCOutputInCommitment {
-                                       offered: if *$input_type == InputDescriptors::RevokedOfferedHTLC || *$input_type == InputDescriptors::OfferedHTLC { true } else { false },
-                                       amount_msat: 0,
-                                       cltv_expiry: 2 << 16,
-                                       payment_hash: PaymentHash([1; 32]),
-                                       transaction_output_index: Some($idx),
-                               };
-                               let redeem_script = if *$input_type == InputDescriptors::RevokedOutput { chan_utils::get_revokeable_redeemscript(&pubkey, 256, &pubkey) } else { chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, &pubkey, &pubkey, &pubkey) };
-                               let sighash = hash_to_message!(&$sighash_parts.sighash_all(&$input, &redeem_script, $amount)[..]);
-                               let sig = secp_ctx.sign(&sighash, &privkey);
-                               $input.witness.push(sig.serialize_der().to_vec());
-                               $input.witness[0].push(SigHashType::All as u8);
-                               sum_actual_sigs += $input.witness[0].len();
-                               if *$input_type == InputDescriptors::RevokedOutput {
-                                       $input.witness.push(vec!(1));
-                               } else if *$input_type == InputDescriptors::RevokedOfferedHTLC || *$input_type == InputDescriptors::RevokedReceivedHTLC {
-                                       $input.witness.push(pubkey.clone().serialize().to_vec());
-                               } else if *$input_type == InputDescriptors::ReceivedHTLC {
-                                       $input.witness.push(vec![0]);
-                               } else {
-                                       $input.witness.push(PaymentPreimage([1; 32]).0.to_vec());
-                               }
-                               $input.witness.push(redeem_script.into_bytes());
-                               println!("witness[0] {}", $input.witness[0].len());
-                               println!("witness[1] {}", $input.witness[1].len());
-                               println!("witness[2] {}", $input.witness[2].len());
-                       }
-               }
-
-               let script_pubkey = Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script();
-               let txid = Sha256dHash::from_hex("56944c5d3f98413ef45cf54545538103cc9f298e0575820ad3591376e2e0f65d").unwrap();
-
-               // Justice tx with 1 to_local, 2 revoked offered HTLCs, 1 revoked received HTLCs
-               let mut claim_tx = Transaction { version: 0, lock_time: 0, input: Vec::new(), output: Vec::new() };
-               for i in 0..4 {
-                       claim_tx.input.push(TxIn {
-                               previous_output: BitcoinOutPoint {
-                                       txid,
-                                       vout: i,
-                               },
-                               script_sig: Script::new(),
-                               sequence: 0xfffffffd,
-                               witness: Vec::new(),
-                       });
-               }
-               claim_tx.output.push(TxOut {
-                       script_pubkey: script_pubkey.clone(),
-                       value: 0,
-               });
-               let base_weight = claim_tx.get_weight();
-               let sighash_parts = bip143::SighashComponents::new(&claim_tx);
-               let inputs_des = vec![InputDescriptors::RevokedOutput, InputDescriptors::RevokedOfferedHTLC, InputDescriptors::RevokedOfferedHTLC, InputDescriptors::RevokedReceivedHTLC];
-               for (idx, inp) in claim_tx.input.iter_mut().zip(inputs_des.iter()).enumerate() {
-                       sign_input!(sighash_parts, inp.0, idx as u32, 0, inp.1, sum_actual_sigs);
-               }
-               assert_eq!(base_weight + ChannelMonitor::get_witnesses_weight(&inputs_des[..]),  claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() - sum_actual_sigs));
-
-               // Claim tx with 1 offered HTLCs, 3 received HTLCs
-               claim_tx.input.clear();
-               sum_actual_sigs = 0;
-               for i in 0..4 {
-                       claim_tx.input.push(TxIn {
-                               previous_output: BitcoinOutPoint {
-                                       txid,
-                                       vout: i,
-                               },
-                               script_sig: Script::new(),
-                               sequence: 0xfffffffd,
-                               witness: Vec::new(),
-                       });
-               }
-               let base_weight = claim_tx.get_weight();
-               let sighash_parts = bip143::SighashComponents::new(&claim_tx);
-               let inputs_des = vec![InputDescriptors::OfferedHTLC, InputDescriptors::ReceivedHTLC, InputDescriptors::ReceivedHTLC, InputDescriptors::ReceivedHTLC];
-               for (idx, inp) in claim_tx.input.iter_mut().zip(inputs_des.iter()).enumerate() {
-                       sign_input!(sighash_parts, inp.0, idx as u32, 0, inp.1, sum_actual_sigs);
-               }
-               assert_eq!(base_weight + ChannelMonitor::get_witnesses_weight(&inputs_des[..]),  claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() - sum_actual_sigs));
-
-               // Justice tx with 1 revoked HTLC-Success tx output
-               claim_tx.input.clear();
-               sum_actual_sigs = 0;
-               claim_tx.input.push(TxIn {
-                       previous_output: BitcoinOutPoint {
-                               txid,
-                               vout: 0,
-                       },
-                       script_sig: Script::new(),
-                       sequence: 0xfffffffd,
-                       witness: Vec::new(),
-               });
-               let base_weight = claim_tx.get_weight();
-               let sighash_parts = bip143::SighashComponents::new(&claim_tx);
-               let inputs_des = vec![InputDescriptors::RevokedOutput];
-               for (idx, inp) in claim_tx.input.iter_mut().zip(inputs_des.iter()).enumerate() {
-                       sign_input!(sighash_parts, inp.0, idx as u32, 0, inp.1, sum_actual_sigs);
-               }
-               assert_eq!(base_weight + ChannelMonitor::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_isg */ (73 * inputs_des.len() - sum_actual_sigs));
-       }
-
-       // Further testing is done in the ChannelManager integration tests.
-}
diff --git a/src/ln/functional_test_utils.rs b/src/ln/functional_test_utils.rs
deleted file mode 100644 (file)
index 3c84cff..0000000
+++ /dev/null
@@ -1,1227 +0,0 @@
-//! A bunch of useful utilities for building networks of nodes and exchanging messages between
-//! nodes for functional tests.
-
-use chain::chaininterface;
-use chain::transaction::OutPoint;
-use chain::keysinterface::KeysInterface;
-use ln::channelmanager::{ChannelManager,RAACommitmentOrder, PaymentPreimage, PaymentHash};
-use ln::router::{Route, Router};
-use ln::msgs;
-use ln::msgs::{ChannelMessageHandler,RoutingMessageHandler, LocalFeatures};
-use util::test_utils;
-use util::events::{Event, EventsProvider, MessageSendEvent, MessageSendEventsProvider};
-use util::errors::APIError;
-use util::logger::Logger;
-use util::config::UserConfig;
-
-use bitcoin::util::hash::BitcoinHash;
-use bitcoin::blockdata::block::BlockHeader;
-use bitcoin::blockdata::transaction::{Transaction, TxOut};
-use bitcoin::network::constants::Network;
-
-use bitcoin_hashes::sha256::Hash as Sha256;
-use bitcoin_hashes::sha256d::Hash as Sha256d;
-use bitcoin_hashes::Hash;
-
-use secp256k1::Secp256k1;
-use secp256k1::key::PublicKey;
-
-use rand::{thread_rng,Rng};
-
-use std::cell::RefCell;
-use std::rc::Rc;
-use std::sync::{Arc, Mutex};
-use std::mem;
-
-pub const CHAN_CONFIRM_DEPTH: u32 = 100;
-pub fn confirm_transaction(chain: &chaininterface::ChainWatchInterfaceUtil, tx: &Transaction, chan_id: u32) {
-       assert!(chain.does_match_tx(tx));
-       let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-       chain.block_connected_checked(&header, 1, &[tx; 1], &[chan_id; 1]);
-       for i in 2..CHAN_CONFIRM_DEPTH {
-               header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-               chain.block_connected_checked(&header, i, &[tx; 0], &[0; 0]);
-       }
-}
-
-pub fn connect_blocks(chain: &chaininterface::ChainWatchInterfaceUtil, depth: u32, height: u32, parent: bool, prev_blockhash: Sha256d) -> Sha256d {
-       let mut header = BlockHeader { version: 0x2000000, prev_blockhash: if parent { prev_blockhash } else { Default::default() }, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-       chain.block_connected_checked(&header, height + 1, &Vec::new(), &Vec::new());
-       for i in 2..depth + 1 {
-               header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-               chain.block_connected_checked(&header, height + i, &Vec::new(), &Vec::new());
-       }
-       header.bitcoin_hash()
-}
-
-pub struct Node {
-       pub chain_monitor: Arc<chaininterface::ChainWatchInterfaceUtil>,
-       pub tx_broadcaster: Arc<test_utils::TestBroadcaster>,
-       pub chan_monitor: Arc<test_utils::TestChannelMonitor>,
-       pub keys_manager: Arc<test_utils::TestKeysInterface>,
-       pub node: Arc<ChannelManager>,
-       pub router: Router,
-       pub node_seed: [u8; 32],
-       pub network_payment_count: Rc<RefCell<u8>>,
-       pub network_chan_count: Rc<RefCell<u32>>,
-}
-impl Drop for Node {
-       fn drop(&mut self) {
-               if !::std::thread::panicking() {
-                       // Check that we processed all pending events
-                       assert!(self.node.get_and_clear_pending_msg_events().is_empty());
-                       assert!(self.node.get_and_clear_pending_events().is_empty());
-                       assert!(self.chan_monitor.added_monitors.lock().unwrap().is_empty());
-               }
-       }
-}
-
-pub fn create_chan_between_nodes(node_a: &Node, node_b: &Node, a_flags: LocalFeatures, b_flags: LocalFeatures) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
-       create_chan_between_nodes_with_value(node_a, node_b, 100000, 10001, a_flags, b_flags)
-}
-
-pub fn create_chan_between_nodes_with_value(node_a: &Node, node_b: &Node, channel_value: u64, push_msat: u64, a_flags: LocalFeatures, b_flags: LocalFeatures) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
-       let (funding_locked, channel_id, tx) = create_chan_between_nodes_with_value_a(node_a, node_b, channel_value, push_msat, a_flags, b_flags);
-       let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(node_a, node_b, &funding_locked);
-       (announcement, as_update, bs_update, channel_id, tx)
-}
-
-macro_rules! get_revoke_commit_msgs {
-       ($node: expr, $node_id: expr) => {
-               {
-                       let events = $node.node.get_and_clear_pending_msg_events();
-                       assert_eq!(events.len(), 2);
-                       (match events[0] {
-                               MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
-                                       assert_eq!(*node_id, $node_id);
-                                       (*msg).clone()
-                               },
-                               _ => panic!("Unexpected event"),
-                       }, match events[1] {
-                               MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
-                                       assert_eq!(*node_id, $node_id);
-                                       assert!(updates.update_add_htlcs.is_empty());
-                                       assert!(updates.update_fulfill_htlcs.is_empty());
-                                       assert!(updates.update_fail_htlcs.is_empty());
-                                       assert!(updates.update_fail_malformed_htlcs.is_empty());
-                                       assert!(updates.update_fee.is_none());
-                                       updates.commitment_signed.clone()
-                               },
-                               _ => panic!("Unexpected event"),
-                       })
-               }
-       }
-}
-
-macro_rules! get_event_msg {
-       ($node: expr, $event_type: path, $node_id: expr) => {
-               {
-                       let events = $node.node.get_and_clear_pending_msg_events();
-                       assert_eq!(events.len(), 1);
-                       match events[0] {
-                               $event_type { ref node_id, ref msg } => {
-                                       assert_eq!(*node_id, $node_id);
-                                       (*msg).clone()
-                               },
-                               _ => panic!("Unexpected event"),
-                       }
-               }
-       }
-}
-
-macro_rules! get_htlc_update_msgs {
-       ($node: expr, $node_id: expr) => {
-               {
-                       let events = $node.node.get_and_clear_pending_msg_events();
-                       assert_eq!(events.len(), 1);
-                       match events[0] {
-                               MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
-                                       assert_eq!(*node_id, $node_id);
-                                       (*updates).clone()
-                               },
-                               _ => panic!("Unexpected event"),
-                       }
-               }
-       }
-}
-
-macro_rules! get_feerate {
-       ($node: expr, $channel_id: expr) => {
-               {
-                       let chan_lock = $node.node.channel_state.lock().unwrap();
-                       let chan = chan_lock.by_id.get(&$channel_id).unwrap();
-                       chan.get_feerate()
-               }
-       }
-}
-
-pub fn create_funding_transaction(node: &Node, expected_chan_value: u64, expected_user_chan_id: u64) -> ([u8; 32], Transaction, OutPoint) {
-       let chan_id = *node.network_chan_count.borrow();
-
-       let events = node.node.get_and_clear_pending_events();
-       assert_eq!(events.len(), 1);
-       match events[0] {
-               Event::FundingGenerationReady { ref temporary_channel_id, ref channel_value_satoshis, ref output_script, user_channel_id } => {
-                       assert_eq!(*channel_value_satoshis, expected_chan_value);
-                       assert_eq!(user_channel_id, expected_user_chan_id);
-
-                       let tx = Transaction { version: chan_id as u32, lock_time: 0, input: Vec::new(), output: vec![TxOut {
-                               value: *channel_value_satoshis, script_pubkey: output_script.clone(),
-                       }]};
-                       let funding_outpoint = OutPoint::new(tx.txid(), 0);
-                       (*temporary_channel_id, tx, funding_outpoint)
-               },
-               _ => panic!("Unexpected event"),
-       }
-}
-
-pub fn create_chan_between_nodes_with_value_init(node_a: &Node, node_b: &Node, channel_value: u64, push_msat: u64, a_flags: LocalFeatures, b_flags: LocalFeatures) -> Transaction {
-       node_a.node.create_channel(node_b.node.get_our_node_id(), channel_value, push_msat, 42).unwrap();
-       node_b.node.handle_open_channel(&node_a.node.get_our_node_id(), a_flags, &get_event_msg!(node_a, MessageSendEvent::SendOpenChannel, node_b.node.get_our_node_id())).unwrap();
-       node_a.node.handle_accept_channel(&node_b.node.get_our_node_id(), b_flags, &get_event_msg!(node_b, MessageSendEvent::SendAcceptChannel, node_a.node.get_our_node_id())).unwrap();
-
-       let (temporary_channel_id, tx, funding_output) = create_funding_transaction(node_a, channel_value, 42);
-
-       {
-               node_a.node.funding_transaction_generated(&temporary_channel_id, funding_output);
-               let mut added_monitors = node_a.chan_monitor.added_monitors.lock().unwrap();
-               assert_eq!(added_monitors.len(), 1);
-               assert_eq!(added_monitors[0].0, funding_output);
-               added_monitors.clear();
-       }
-
-       node_b.node.handle_funding_created(&node_a.node.get_our_node_id(), &get_event_msg!(node_a, MessageSendEvent::SendFundingCreated, node_b.node.get_our_node_id())).unwrap();
-       {
-               let mut added_monitors = node_b.chan_monitor.added_monitors.lock().unwrap();
-               assert_eq!(added_monitors.len(), 1);
-               assert_eq!(added_monitors[0].0, funding_output);
-               added_monitors.clear();
-       }
-
-       node_a.node.handle_funding_signed(&node_b.node.get_our_node_id(), &get_event_msg!(node_b, MessageSendEvent::SendFundingSigned, node_a.node.get_our_node_id())).unwrap();
-       {
-               let mut added_monitors = node_a.chan_monitor.added_monitors.lock().unwrap();
-               assert_eq!(added_monitors.len(), 1);
-               assert_eq!(added_monitors[0].0, funding_output);
-               added_monitors.clear();
-       }
-
-       let events_4 = node_a.node.get_and_clear_pending_events();
-       assert_eq!(events_4.len(), 1);
-       match events_4[0] {
-               Event::FundingBroadcastSafe { ref funding_txo, user_channel_id } => {
-                       assert_eq!(user_channel_id, 42);
-                       assert_eq!(*funding_txo, funding_output);
-               },
-               _ => panic!("Unexpected event"),
-       };
-
-       tx
-}
-
-pub fn create_chan_between_nodes_with_value_confirm_first(node_recv: &Node, node_conf: &Node, tx: &Transaction) {
-       confirm_transaction(&node_conf.chain_monitor, &tx, tx.version);
-       node_recv.node.handle_funding_locked(&node_conf.node.get_our_node_id(), &get_event_msg!(node_conf, MessageSendEvent::SendFundingLocked, node_recv.node.get_our_node_id())).unwrap();
-}
-
-pub fn create_chan_between_nodes_with_value_confirm_second(node_recv: &Node, node_conf: &Node) -> ((msgs::FundingLocked, msgs::AnnouncementSignatures), [u8; 32]) {
-       let channel_id;
-       let events_6 = node_conf.node.get_and_clear_pending_msg_events();
-       assert_eq!(events_6.len(), 2);
-       ((match events_6[0] {
-               MessageSendEvent::SendFundingLocked { ref node_id, ref msg } => {
-                       channel_id = msg.channel_id.clone();
-                       assert_eq!(*node_id, node_recv.node.get_our_node_id());
-                       msg.clone()
-               },
-               _ => panic!("Unexpected event"),
-       }, match events_6[1] {
-               MessageSendEvent::SendAnnouncementSignatures { ref node_id, ref msg } => {
-                       assert_eq!(*node_id, node_recv.node.get_our_node_id());
-                       msg.clone()
-               },
-               _ => panic!("Unexpected event"),
-       }), channel_id)
-}
-
-pub fn create_chan_between_nodes_with_value_confirm(node_a: &Node, node_b: &Node, tx: &Transaction) -> ((msgs::FundingLocked, msgs::AnnouncementSignatures), [u8; 32]) {
-       create_chan_between_nodes_with_value_confirm_first(node_a, node_b, tx);
-       confirm_transaction(&node_a.chain_monitor, &tx, tx.version);
-       create_chan_between_nodes_with_value_confirm_second(node_b, node_a)
-}
-
-pub fn create_chan_between_nodes_with_value_a(node_a: &Node, node_b: &Node, channel_value: u64, push_msat: u64, a_flags: LocalFeatures, b_flags: LocalFeatures) -> ((msgs::FundingLocked, msgs::AnnouncementSignatures), [u8; 32], Transaction) {
-       let tx = create_chan_between_nodes_with_value_init(node_a, node_b, channel_value, push_msat, a_flags, b_flags);
-       let (msgs, chan_id) = create_chan_between_nodes_with_value_confirm(node_a, node_b, &tx);
-       (msgs, chan_id, tx)
-}
-
-pub fn create_chan_between_nodes_with_value_b(node_a: &Node, node_b: &Node, as_funding_msgs: &(msgs::FundingLocked, msgs::AnnouncementSignatures)) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate) {
-       node_b.node.handle_funding_locked(&node_a.node.get_our_node_id(), &as_funding_msgs.0).unwrap();
-       let bs_announcement_sigs = get_event_msg!(node_b, MessageSendEvent::SendAnnouncementSignatures, node_a.node.get_our_node_id());
-       node_b.node.handle_announcement_signatures(&node_a.node.get_our_node_id(), &as_funding_msgs.1).unwrap();
-
-       let events_7 = node_b.node.get_and_clear_pending_msg_events();
-       assert_eq!(events_7.len(), 1);
-       let (announcement, bs_update) = match events_7[0] {
-               MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
-                       (msg, update_msg)
-               },
-               _ => panic!("Unexpected event"),
-       };
-
-       node_a.node.handle_announcement_signatures(&node_b.node.get_our_node_id(), &bs_announcement_sigs).unwrap();
-       let events_8 = node_a.node.get_and_clear_pending_msg_events();
-       assert_eq!(events_8.len(), 1);
-       let as_update = match events_8[0] {
-               MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
-                       assert!(*announcement == *msg);
-                       assert_eq!(update_msg.contents.short_channel_id, announcement.contents.short_channel_id);
-                       assert_eq!(update_msg.contents.short_channel_id, bs_update.contents.short_channel_id);
-                       update_msg
-               },
-               _ => panic!("Unexpected event"),
-       };
-
-       *node_a.network_chan_count.borrow_mut() += 1;
-
-       ((*announcement).clone(), (*as_update).clone(), (*bs_update).clone())
-}
-
-pub fn create_announced_chan_between_nodes(nodes: &Vec<Node>, a: usize, b: usize, a_flags: LocalFeatures, b_flags: LocalFeatures) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
-       create_announced_chan_between_nodes_with_value(nodes, a, b, 100000, 10001, a_flags, b_flags)
-}
-
-pub fn create_announced_chan_between_nodes_with_value(nodes: &Vec<Node>, a: usize, b: usize, channel_value: u64, push_msat: u64, a_flags: LocalFeatures, b_flags: LocalFeatures) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
-       let chan_announcement = create_chan_between_nodes_with_value(&nodes[a], &nodes[b], channel_value, push_msat, a_flags, b_flags);
-       for node in nodes {
-               assert!(node.router.handle_channel_announcement(&chan_announcement.0).unwrap());
-               node.router.handle_channel_update(&chan_announcement.1).unwrap();
-               node.router.handle_channel_update(&chan_announcement.2).unwrap();
-       }
-       (chan_announcement.1, chan_announcement.2, chan_announcement.3, chan_announcement.4)
-}
-
-macro_rules! check_spends {
-       ($tx: expr, $spends_tx: expr) => {
-               {
-                       $tx.verify(|out_point| {
-                               if out_point.txid == $spends_tx.txid() {
-                                       $spends_tx.output.get(out_point.vout as usize).cloned()
-                               } else {
-                                       None
-                               }
-                       }).unwrap();
-               }
-       }
-}
-
-macro_rules! get_closing_signed_broadcast {
-       ($node: expr, $dest_pubkey: expr) => {
-               {
-                       let events = $node.get_and_clear_pending_msg_events();
-                       assert!(events.len() == 1 || events.len() == 2);
-                       (match events[events.len() - 1] {
-                               MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
-                                       assert_eq!(msg.contents.flags & 2, 2);
-                                       msg.clone()
-                               },
-                               _ => panic!("Unexpected event"),
-                       }, if events.len() == 2 {
-                               match events[0] {
-                                       MessageSendEvent::SendClosingSigned { ref node_id, ref msg } => {
-                                               assert_eq!(*node_id, $dest_pubkey);
-                                               Some(msg.clone())
-                                       },
-                                       _ => panic!("Unexpected event"),
-                               }
-                       } else { None })
-               }
-       }
-}
-
-macro_rules! check_closed_broadcast {
-       ($node: expr) => {{
-               let events = $node.node.get_and_clear_pending_msg_events();
-               assert_eq!(events.len(), 1);
-               match events[0] {
-                       MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
-                               assert_eq!(msg.contents.flags & 2, 2);
-                       },
-                       _ => panic!("Unexpected event"),
-               }
-       }}
-}
-
-pub fn close_channel(outbound_node: &Node, inbound_node: &Node, channel_id: &[u8; 32], funding_tx: Transaction, close_inbound_first: bool) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, Transaction) {
-       let (node_a, broadcaster_a, struct_a) = if close_inbound_first { (&inbound_node.node, &inbound_node.tx_broadcaster, inbound_node) } else { (&outbound_node.node, &outbound_node.tx_broadcaster, outbound_node) };
-       let (node_b, broadcaster_b) = if close_inbound_first { (&outbound_node.node, &outbound_node.tx_broadcaster) } else { (&inbound_node.node, &inbound_node.tx_broadcaster) };
-       let (tx_a, tx_b);
-
-       node_a.close_channel(channel_id).unwrap();
-       node_b.handle_shutdown(&node_a.get_our_node_id(), &get_event_msg!(struct_a, MessageSendEvent::SendShutdown, node_b.get_our_node_id())).unwrap();
-
-       let events_1 = node_b.get_and_clear_pending_msg_events();
-       assert!(events_1.len() >= 1);
-       let shutdown_b = match events_1[0] {
-               MessageSendEvent::SendShutdown { ref node_id, ref msg } => {
-                       assert_eq!(node_id, &node_a.get_our_node_id());
-                       msg.clone()
-               },
-               _ => panic!("Unexpected event"),
-       };
-
-       let closing_signed_b = if !close_inbound_first {
-               assert_eq!(events_1.len(), 1);
-               None
-       } else {
-               Some(match events_1[1] {
-                       MessageSendEvent::SendClosingSigned { ref node_id, ref msg } => {
-                               assert_eq!(node_id, &node_a.get_our_node_id());
-                               msg.clone()
-                       },
-                       _ => panic!("Unexpected event"),
-               })
-       };
-
-       node_a.handle_shutdown(&node_b.get_our_node_id(), &shutdown_b).unwrap();
-       let (as_update, bs_update) = if close_inbound_first {
-               assert!(node_a.get_and_clear_pending_msg_events().is_empty());
-               node_a.handle_closing_signed(&node_b.get_our_node_id(), &closing_signed_b.unwrap()).unwrap();
-               assert_eq!(broadcaster_a.txn_broadcasted.lock().unwrap().len(), 1);
-               tx_a = broadcaster_a.txn_broadcasted.lock().unwrap().remove(0);
-               let (as_update, closing_signed_a) = get_closing_signed_broadcast!(node_a, node_b.get_our_node_id());
-
-               node_b.handle_closing_signed(&node_a.get_our_node_id(), &closing_signed_a.unwrap()).unwrap();
-               let (bs_update, none_b) = get_closing_signed_broadcast!(node_b, node_a.get_our_node_id());
-               assert!(none_b.is_none());
-               assert_eq!(broadcaster_b.txn_broadcasted.lock().unwrap().len(), 1);
-               tx_b = broadcaster_b.txn_broadcasted.lock().unwrap().remove(0);
-               (as_update, bs_update)
-       } else {
-               let closing_signed_a = get_event_msg!(struct_a, MessageSendEvent::SendClosingSigned, node_b.get_our_node_id());
-
-               node_b.handle_closing_signed(&node_a.get_our_node_id(), &closing_signed_a).unwrap();
-               assert_eq!(broadcaster_b.txn_broadcasted.lock().unwrap().len(), 1);
-               tx_b = broadcaster_b.txn_broadcasted.lock().unwrap().remove(0);
-               let (bs_update, closing_signed_b) = get_closing_signed_broadcast!(node_b, node_a.get_our_node_id());
-
-               node_a.handle_closing_signed(&node_b.get_our_node_id(), &closing_signed_b.unwrap()).unwrap();
-               let (as_update, none_a) = get_closing_signed_broadcast!(node_a, node_b.get_our_node_id());
-               assert!(none_a.is_none());
-               assert_eq!(broadcaster_a.txn_broadcasted.lock().unwrap().len(), 1);
-               tx_a = broadcaster_a.txn_broadcasted.lock().unwrap().remove(0);
-               (as_update, bs_update)
-       };
-       assert_eq!(tx_a, tx_b);
-       check_spends!(tx_a, funding_tx);
-
-       (as_update, bs_update, tx_a)
-}
-
-pub struct SendEvent {
-       pub node_id: PublicKey,
-       pub msgs: Vec<msgs::UpdateAddHTLC>,
-       pub commitment_msg: msgs::CommitmentSigned,
-}
-impl SendEvent {
-       pub fn from_commitment_update(node_id: PublicKey, updates: msgs::CommitmentUpdate) -> SendEvent {
-               assert!(updates.update_fulfill_htlcs.is_empty());
-               assert!(updates.update_fail_htlcs.is_empty());
-               assert!(updates.update_fail_malformed_htlcs.is_empty());
-               assert!(updates.update_fee.is_none());
-               SendEvent { node_id: node_id, msgs: updates.update_add_htlcs, commitment_msg: updates.commitment_signed }
-       }
-
-       pub fn from_event(event: MessageSendEvent) -> SendEvent {
-               match event {
-                       MessageSendEvent::UpdateHTLCs { node_id, updates } => SendEvent::from_commitment_update(node_id, updates),
-                       _ => panic!("Unexpected event type!"),
-               }
-       }
-
-       pub fn from_node(node: &Node) -> SendEvent {
-               let mut events = node.node.get_and_clear_pending_msg_events();
-               assert_eq!(events.len(), 1);
-               SendEvent::from_event(events.pop().unwrap())
-       }
-}
-
-macro_rules! check_added_monitors {
-       ($node: expr, $count: expr) => {
-               {
-                       let mut added_monitors = $node.chan_monitor.added_monitors.lock().unwrap();
-                       assert_eq!(added_monitors.len(), $count);
-                       added_monitors.clear();
-               }
-       }
-}
-
-macro_rules! commitment_signed_dance {
-       ($node_a: expr, $node_b: expr, $commitment_signed: expr, $fail_backwards: expr, true /* skip last step */) => {
-               {
-                       check_added_monitors!($node_a, 0);
-                       assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
-                       $node_a.node.handle_commitment_signed(&$node_b.node.get_our_node_id(), &$commitment_signed).unwrap();
-                       check_added_monitors!($node_a, 1);
-                       commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, false);
-               }
-       };
-       ($node_a: expr, $node_b: expr, (), $fail_backwards: expr, true /* skip last step */, true /* return extra message */, true /* return last RAA */) => {
-               {
-                       let (as_revoke_and_ack, as_commitment_signed) = get_revoke_commit_msgs!($node_a, $node_b.node.get_our_node_id());
-                       check_added_monitors!($node_b, 0);
-                       assert!($node_b.node.get_and_clear_pending_msg_events().is_empty());
-                       $node_b.node.handle_revoke_and_ack(&$node_a.node.get_our_node_id(), &as_revoke_and_ack).unwrap();
-                       assert!($node_b.node.get_and_clear_pending_msg_events().is_empty());
-                       check_added_monitors!($node_b, 1);
-                       $node_b.node.handle_commitment_signed(&$node_a.node.get_our_node_id(), &as_commitment_signed).unwrap();
-                       let (bs_revoke_and_ack, extra_msg_option) = {
-                               let events = $node_b.node.get_and_clear_pending_msg_events();
-                               assert!(events.len() <= 2);
-                               (match events[0] {
-                                       MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
-                                               assert_eq!(*node_id, $node_a.node.get_our_node_id());
-                                               (*msg).clone()
-                                       },
-                                       _ => panic!("Unexpected event"),
-                               }, events.get(1).map(|e| e.clone()))
-                       };
-                       check_added_monitors!($node_b, 1);
-                       if $fail_backwards {
-                               assert!($node_a.node.get_and_clear_pending_events().is_empty());
-                               assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
-                       }
-                       (extra_msg_option, bs_revoke_and_ack)
-               }
-       };
-       ($node_a: expr, $node_b: expr, $commitment_signed: expr, $fail_backwards: expr, true /* skip last step */, false /* return extra message */, true /* return last RAA */) => {
-               {
-                       check_added_monitors!($node_a, 0);
-                       assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
-                       $node_a.node.handle_commitment_signed(&$node_b.node.get_our_node_id(), &$commitment_signed).unwrap();
-                       check_added_monitors!($node_a, 1);
-                       let (extra_msg_option, bs_revoke_and_ack) = commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, true, true);
-                       assert!(extra_msg_option.is_none());
-                       bs_revoke_and_ack
-               }
-       };
-       ($node_a: expr, $node_b: expr, (), $fail_backwards: expr, true /* skip last step */, true /* return extra message */) => {
-               {
-                       let (extra_msg_option, bs_revoke_and_ack) = commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, true, true);
-                       $node_a.node.handle_revoke_and_ack(&$node_b.node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
-                       check_added_monitors!($node_a, 1);
-                       extra_msg_option
-               }
-       };
-       ($node_a: expr, $node_b: expr, (), $fail_backwards: expr, true /* skip last step */, false /* no extra message */) => {
-               {
-                       assert!(commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, true).is_none());
-               }
-       };
-       ($node_a: expr, $node_b: expr, $commitment_signed: expr, $fail_backwards: expr) => {
-               {
-                       commitment_signed_dance!($node_a, $node_b, $commitment_signed, $fail_backwards, true);
-                       if $fail_backwards {
-                               expect_pending_htlcs_forwardable!($node_a);
-                               check_added_monitors!($node_a, 1);
-
-                               let channel_state = $node_a.node.channel_state.lock().unwrap();
-                               assert_eq!(channel_state.pending_msg_events.len(), 1);
-                               if let MessageSendEvent::UpdateHTLCs { ref node_id, .. } = channel_state.pending_msg_events[0] {
-                                       assert_ne!(*node_id, $node_b.node.get_our_node_id());
-                               } else { panic!("Unexpected event"); }
-                       } else {
-                               assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
-                       }
-               }
-       }
-}
-
-macro_rules! get_payment_preimage_hash {
-       ($node: expr) => {
-               {
-                       let payment_preimage = PaymentPreimage([*$node.network_payment_count.borrow(); 32]);
-                       *$node.network_payment_count.borrow_mut() += 1;
-                       let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0[..]).into_inner());
-                       (payment_preimage, payment_hash)
-               }
-       }
-}
-
-macro_rules! expect_pending_htlcs_forwardable {
-       ($node: expr) => {{
-               let events = $node.node.get_and_clear_pending_events();
-               assert_eq!(events.len(), 1);
-               match events[0] {
-                       Event::PendingHTLCsForwardable { .. } => { },
-                       _ => panic!("Unexpected event"),
-               };
-               $node.node.process_pending_htlc_forwards();
-       }}
-}
-
-macro_rules! expect_payment_received {
-       ($node: expr, $expected_payment_hash: expr, $expected_recv_value: expr) => {
-               let events = $node.node.get_and_clear_pending_events();
-               assert_eq!(events.len(), 1);
-               match events[0] {
-                       Event::PaymentReceived { ref payment_hash, amt } => {
-                               assert_eq!($expected_payment_hash, *payment_hash);
-                               assert_eq!($expected_recv_value, amt);
-                       },
-                       _ => panic!("Unexpected event"),
-               }
-       }
-}
-
-macro_rules! expect_payment_sent {
-       ($node: expr, $expected_payment_preimage: expr) => {
-               let events = $node.node.get_and_clear_pending_events();
-               assert_eq!(events.len(), 1);
-               match events[0] {
-                       Event::PaymentSent { ref payment_preimage } => {
-                               assert_eq!($expected_payment_preimage, *payment_preimage);
-                       },
-                       _ => panic!("Unexpected event"),
-               }
-       }
-}
-
-pub fn send_along_route_with_hash(origin_node: &Node, route: Route, expected_route: &[&Node], recv_value: u64, our_payment_hash: PaymentHash) {
-       let mut payment_event = {
-               origin_node.node.send_payment(route, our_payment_hash).unwrap();
-               check_added_monitors!(origin_node, 1);
-
-               let mut events = origin_node.node.get_and_clear_pending_msg_events();
-               assert_eq!(events.len(), 1);
-               SendEvent::from_event(events.remove(0))
-       };
-       let mut prev_node = origin_node;
-
-       for (idx, &node) in expected_route.iter().enumerate() {
-               assert_eq!(node.node.get_our_node_id(), payment_event.node_id);
-
-               node.node.handle_update_add_htlc(&prev_node.node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
-               check_added_monitors!(node, 0);
-               commitment_signed_dance!(node, prev_node, payment_event.commitment_msg, false);
-
-               expect_pending_htlcs_forwardable!(node);
-
-               if idx == expected_route.len() - 1 {
-                       let events_2 = node.node.get_and_clear_pending_events();
-                       assert_eq!(events_2.len(), 1);
-                       match events_2[0] {
-                               Event::PaymentReceived { ref payment_hash, amt } => {
-                                       assert_eq!(our_payment_hash, *payment_hash);
-                                       assert_eq!(amt, recv_value);
-                               },
-                               _ => panic!("Unexpected event"),
-                       }
-               } else {
-                       let mut events_2 = node.node.get_and_clear_pending_msg_events();
-                       assert_eq!(events_2.len(), 1);
-                       check_added_monitors!(node, 1);
-                       payment_event = SendEvent::from_event(events_2.remove(0));
-                       assert_eq!(payment_event.msgs.len(), 1);
-               }
-
-               prev_node = node;
-       }
-}
-
-pub fn send_along_route(origin_node: &Node, route: Route, expected_route: &[&Node], recv_value: u64) -> (PaymentPreimage, PaymentHash) {
-       let (our_payment_preimage, our_payment_hash) = get_payment_preimage_hash!(origin_node);
-       send_along_route_with_hash(origin_node, route, expected_route, recv_value, our_payment_hash);
-       (our_payment_preimage, our_payment_hash)
-}
-
-pub fn claim_payment_along_route(origin_node: &Node, expected_route: &[&Node], skip_last: bool, our_payment_preimage: PaymentPreimage) {
-       assert!(expected_route.last().unwrap().node.claim_funds(our_payment_preimage));
-       check_added_monitors!(expected_route.last().unwrap(), 1);
-
-       let mut next_msgs: Option<(msgs::UpdateFulfillHTLC, msgs::CommitmentSigned)> = None;
-       let mut expected_next_node = expected_route.last().unwrap().node.get_our_node_id();
-       macro_rules! get_next_msgs {
-               ($node: expr) => {
-                       {
-                               let events = $node.node.get_and_clear_pending_msg_events();
-                               assert_eq!(events.len(), 1);
-                               match events[0] {
-                                       MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => {
-                                               assert!(update_add_htlcs.is_empty());
-                                               assert_eq!(update_fulfill_htlcs.len(), 1);
-                                               assert!(update_fail_htlcs.is_empty());
-                                               assert!(update_fail_malformed_htlcs.is_empty());
-                                               assert!(update_fee.is_none());
-                                               expected_next_node = node_id.clone();
-                                               Some((update_fulfill_htlcs[0].clone(), commitment_signed.clone()))
-                                       },
-                                       _ => panic!("Unexpected event"),
-                               }
-                       }
-               }
-       }
-
-       macro_rules! last_update_fulfill_dance {
-               ($node: expr, $prev_node: expr) => {
-                       {
-                               $node.node.handle_update_fulfill_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0).unwrap();
-                               check_added_monitors!($node, 0);
-                               assert!($node.node.get_and_clear_pending_msg_events().is_empty());
-                               commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, false);
-                       }
-               }
-       }
-       macro_rules! mid_update_fulfill_dance {
-               ($node: expr, $prev_node: expr, $new_msgs: expr) => {
-                       {
-                               $node.node.handle_update_fulfill_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0).unwrap();
-                               check_added_monitors!($node, 1);
-                               let new_next_msgs = if $new_msgs {
-                                       get_next_msgs!($node)
-                               } else {
-                                       assert!($node.node.get_and_clear_pending_msg_events().is_empty());
-                                       None
-                               };
-                               commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, false);
-                               next_msgs = new_next_msgs;
-                       }
-               }
-       }
-
-       let mut prev_node = expected_route.last().unwrap();
-       for (idx, node) in expected_route.iter().rev().enumerate() {
-               assert_eq!(expected_next_node, node.node.get_our_node_id());
-               let update_next_msgs = !skip_last || idx != expected_route.len() - 1;
-               if next_msgs.is_some() {
-                       mid_update_fulfill_dance!(node, prev_node, update_next_msgs);
-               } else if update_next_msgs {
-                       next_msgs = get_next_msgs!(node);
-               } else {
-                       assert!(node.node.get_and_clear_pending_msg_events().is_empty());
-               }
-               if !skip_last && idx == expected_route.len() - 1 {
-                       assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
-               }
-
-               prev_node = node;
-       }
-
-       if !skip_last {
-               last_update_fulfill_dance!(origin_node, expected_route.first().unwrap());
-               expect_payment_sent!(origin_node, our_payment_preimage);
-       }
-}
-
-pub fn claim_payment(origin_node: &Node, expected_route: &[&Node], our_payment_preimage: PaymentPreimage) {
-       claim_payment_along_route(origin_node, expected_route, false, our_payment_preimage);
-}
-
-pub const TEST_FINAL_CLTV: u32 = 32;
-
-pub fn route_payment(origin_node: &Node, expected_route: &[&Node], recv_value: u64) -> (PaymentPreimage, PaymentHash) {
-       let route = origin_node.router.get_route(&expected_route.last().unwrap().node.get_our_node_id(), None, &Vec::new(), recv_value, TEST_FINAL_CLTV).unwrap();
-       assert_eq!(route.hops.len(), expected_route.len());
-       for (node, hop) in expected_route.iter().zip(route.hops.iter()) {
-               assert_eq!(hop.pubkey, node.node.get_our_node_id());
-       }
-
-       send_along_route(origin_node, route, expected_route, recv_value)
-}
-
-pub fn route_over_limit(origin_node: &Node, expected_route: &[&Node], recv_value: u64) {
-       let route = origin_node.router.get_route(&expected_route.last().unwrap().node.get_our_node_id(), None, &Vec::new(), recv_value, TEST_FINAL_CLTV).unwrap();
-       assert_eq!(route.hops.len(), expected_route.len());
-       for (node, hop) in expected_route.iter().zip(route.hops.iter()) {
-               assert_eq!(hop.pubkey, node.node.get_our_node_id());
-       }
-
-       let (_, our_payment_hash) = get_payment_preimage_hash!(origin_node);
-
-       let err = origin_node.node.send_payment(route, our_payment_hash).err().unwrap();
-       match err {
-               APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over the max HTLC value in flight our peer will accept"),
-               _ => panic!("Unknown error variants"),
-       };
-}
-
-pub fn send_payment(origin: &Node, expected_route: &[&Node], recv_value: u64) {
-       let our_payment_preimage = route_payment(&origin, expected_route, recv_value).0;
-       claim_payment(&origin, expected_route, our_payment_preimage);
-}
-
-pub fn fail_payment_along_route(origin_node: &Node, expected_route: &[&Node], skip_last: bool, our_payment_hash: PaymentHash) {
-       assert!(expected_route.last().unwrap().node.fail_htlc_backwards(&our_payment_hash));
-       expect_pending_htlcs_forwardable!(expected_route.last().unwrap());
-       check_added_monitors!(expected_route.last().unwrap(), 1);
-
-       let mut next_msgs: Option<(msgs::UpdateFailHTLC, msgs::CommitmentSigned)> = None;
-       macro_rules! update_fail_dance {
-               ($node: expr, $prev_node: expr, $last_node: expr) => {
-                       {
-                               $node.node.handle_update_fail_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0).unwrap();
-                               commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, !$last_node);
-                               if skip_last && $last_node {
-                                       expect_pending_htlcs_forwardable!($node);
-                               }
-                       }
-               }
-       }
-
-       let mut expected_next_node = expected_route.last().unwrap().node.get_our_node_id();
-       let mut prev_node = expected_route.last().unwrap();
-       for (idx, node) in expected_route.iter().rev().enumerate() {
-               assert_eq!(expected_next_node, node.node.get_our_node_id());
-               if next_msgs.is_some() {
-                       // We may be the "last node" for the purpose of the commitment dance if we're
-                       // skipping the last node (implying it is disconnected) and we're the
-                       // second-to-last node!
-                       update_fail_dance!(node, prev_node, skip_last && idx == expected_route.len() - 1);
-               }
-
-               let events = node.node.get_and_clear_pending_msg_events();
-               if !skip_last || idx != expected_route.len() - 1 {
-                       assert_eq!(events.len(), 1);
-                       match events[0] {
-                               MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => {
-                                       assert!(update_add_htlcs.is_empty());
-                                       assert!(update_fulfill_htlcs.is_empty());
-                                       assert_eq!(update_fail_htlcs.len(), 1);
-                                       assert!(update_fail_malformed_htlcs.is_empty());
-                                       assert!(update_fee.is_none());
-                                       expected_next_node = node_id.clone();
-                                       next_msgs = Some((update_fail_htlcs[0].clone(), commitment_signed.clone()));
-                               },
-                               _ => panic!("Unexpected event"),
-                       }
-               } else {
-                       assert!(events.is_empty());
-               }
-               if !skip_last && idx == expected_route.len() - 1 {
-                       assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
-               }
-
-               prev_node = node;
-       }
-
-       if !skip_last {
-               update_fail_dance!(origin_node, expected_route.first().unwrap(), true);
-
-               let events = origin_node.node.get_and_clear_pending_events();
-               assert_eq!(events.len(), 1);
-               match events[0] {
-                       Event::PaymentFailed { payment_hash, rejected_by_dest, .. } => {
-                               assert_eq!(payment_hash, our_payment_hash);
-                               assert!(rejected_by_dest);
-                       },
-                       _ => panic!("Unexpected event"),
-               }
-       }
-}
-
-pub fn fail_payment(origin_node: &Node, expected_route: &[&Node], our_payment_hash: PaymentHash) {
-       fail_payment_along_route(origin_node, expected_route, false, our_payment_hash);
-}
-
-pub fn create_network(node_count: usize, node_config: &[Option<UserConfig>]) -> Vec<Node> {
-       let mut nodes = Vec::new();
-       let mut rng = thread_rng();
-       let secp_ctx = Secp256k1::new();
-
-       let chan_count = Rc::new(RefCell::new(0));
-       let payment_count = Rc::new(RefCell::new(0));
-
-       for i in 0..node_count {
-               let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::with_id(format!("node {}", i)));
-               let feeest = Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 });
-               let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, Arc::clone(&logger)));
-               let tx_broadcaster = Arc::new(test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new())});
-               let mut seed = [0; 32];
-               rng.fill_bytes(&mut seed);
-               let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&seed, Network::Testnet, Arc::clone(&logger)));
-               let chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(chain_monitor.clone(), tx_broadcaster.clone(), logger.clone(), feeest.clone()));
-               let mut default_config = UserConfig::new();
-               default_config.channel_options.announced_channel = true;
-               default_config.peer_channel_config_limits.force_announced_channel_preference = false;
-               let node = ChannelManager::new(Network::Testnet, feeest.clone(), chan_monitor.clone(), chain_monitor.clone(), tx_broadcaster.clone(), Arc::clone(&logger), keys_manager.clone(), if node_config[i].is_some() { node_config[i].clone().unwrap() } else { default_config }).unwrap();
-               let router = Router::new(PublicKey::from_secret_key(&secp_ctx, &keys_manager.get_node_secret()), chain_monitor.clone(), Arc::clone(&logger));
-               nodes.push(Node { chain_monitor, tx_broadcaster, chan_monitor, node, router, keys_manager, node_seed: seed,
-                       network_payment_count: payment_count.clone(),
-                       network_chan_count: chan_count.clone(),
-               });
-       }
-
-       nodes
-}
-
-#[derive(PartialEq)]
-pub enum HTLCType { NONE, TIMEOUT, SUCCESS }
-/// Tests that the given node has broadcast transactions for the given Channel
-///
-/// First checks that the latest local commitment tx has been broadcast, unless an explicit
-/// commitment_tx is provided, which may be used to test that a remote commitment tx was
-/// broadcast and the revoked outputs were claimed.
-///
-/// Next tests that there is (or is not) a transaction that spends the commitment transaction
-/// that appears to be the type of HTLC transaction specified in has_htlc_tx.
-///
-/// All broadcast transactions must be accounted for in one of the above three types of we'll
-/// also fail.
-pub fn test_txn_broadcast(node: &Node, chan: &(msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction), commitment_tx: Option<Transaction>, has_htlc_tx: HTLCType) -> Vec<Transaction> {
-       let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
-       assert!(node_txn.len() >= if commitment_tx.is_some() { 0 } else { 1 } + if has_htlc_tx == HTLCType::NONE { 0 } else { 1 });
-
-       let mut res = Vec::with_capacity(2);
-       node_txn.retain(|tx| {
-               if tx.input.len() == 1 && tx.input[0].previous_output.txid == chan.3.txid() {
-                       check_spends!(tx, chan.3.clone());
-                       if commitment_tx.is_none() {
-                               res.push(tx.clone());
-                       }
-                       false
-               } else { true }
-       });
-       if let Some(explicit_tx) = commitment_tx {
-               res.push(explicit_tx.clone());
-       }
-
-       assert_eq!(res.len(), 1);
-
-       if has_htlc_tx != HTLCType::NONE {
-               node_txn.retain(|tx| {
-                       if tx.input.len() == 1 && tx.input[0].previous_output.txid == res[0].txid() {
-                               check_spends!(tx, res[0].clone());
-                               if has_htlc_tx == HTLCType::TIMEOUT {
-                                       assert!(tx.lock_time != 0);
-                               } else {
-                                       assert!(tx.lock_time == 0);
-                               }
-                               res.push(tx.clone());
-                               false
-                       } else { true }
-               });
-               assert!(res.len() == 2 || res.len() == 3);
-               if res.len() == 3 {
-                       assert_eq!(res[1], res[2]);
-               }
-       }
-
-       assert!(node_txn.is_empty());
-       res
-}
-
-/// Tests that the given node has broadcast a claim transaction against the provided revoked
-/// HTLC transaction.
-pub fn test_revoked_htlc_claim_txn_broadcast(node: &Node, revoked_tx: Transaction) {
-       let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
-       assert_eq!(node_txn.len(), 1);
-       node_txn.retain(|tx| {
-               if tx.input.len() == 1 && tx.input[0].previous_output.txid == revoked_tx.txid() {
-                       check_spends!(tx, revoked_tx.clone());
-                       false
-               } else { true }
-       });
-       assert!(node_txn.is_empty());
-}
-
-pub fn check_preimage_claim(node: &Node, prev_txn: &Vec<Transaction>) -> Vec<Transaction> {
-       let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
-
-       assert!(node_txn.len() >= 1);
-       assert_eq!(node_txn[0].input.len(), 1);
-       let mut found_prev = false;
-
-       for tx in prev_txn {
-               if node_txn[0].input[0].previous_output.txid == tx.txid() {
-                       check_spends!(node_txn[0], tx.clone());
-                       assert!(node_txn[0].input[0].witness[2].len() > 106); // must spend an htlc output
-                       assert_eq!(tx.input.len(), 1); // must spend a commitment tx
-
-                       found_prev = true;
-                       break;
-               }
-       }
-       assert!(found_prev);
-
-       let mut res = Vec::new();
-       mem::swap(&mut *node_txn, &mut res);
-       res
-}
-
-pub fn get_announce_close_broadcast_events(nodes: &Vec<Node>, a: usize, b: usize) {
-       let events_1 = nodes[a].node.get_and_clear_pending_msg_events();
-       assert_eq!(events_1.len(), 1);
-       let as_update = match events_1[0] {
-               MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
-                       msg.clone()
-               },
-               _ => panic!("Unexpected event"),
-       };
-
-       let events_2 = nodes[b].node.get_and_clear_pending_msg_events();
-       assert_eq!(events_2.len(), 1);
-       let bs_update = match events_2[0] {
-               MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
-                       msg.clone()
-               },
-               _ => panic!("Unexpected event"),
-       };
-
-       for node in nodes {
-               node.router.handle_channel_update(&as_update).unwrap();
-               node.router.handle_channel_update(&bs_update).unwrap();
-       }
-}
-
-macro_rules! get_channel_value_stat {
-       ($node: expr, $channel_id: expr) => {{
-               let chan_lock = $node.node.channel_state.lock().unwrap();
-               let chan = chan_lock.by_id.get(&$channel_id).unwrap();
-               chan.get_value_stat()
-       }}
-}
-
-macro_rules! get_chan_reestablish_msgs {
-       ($src_node: expr, $dst_node: expr) => {
-               {
-                       let mut res = Vec::with_capacity(1);
-                       for msg in $src_node.node.get_and_clear_pending_msg_events() {
-                               if let MessageSendEvent::SendChannelReestablish { ref node_id, ref msg } = msg {
-                                       assert_eq!(*node_id, $dst_node.node.get_our_node_id());
-                                       res.push(msg.clone());
-                               } else {
-                                       panic!("Unexpected event")
-                               }
-                       }
-                       res
-               }
-       }
-}
-
-macro_rules! handle_chan_reestablish_msgs {
-       ($src_node: expr, $dst_node: expr) => {
-               {
-                       let msg_events = $src_node.node.get_and_clear_pending_msg_events();
-                       let mut idx = 0;
-                       let funding_locked = if let Some(&MessageSendEvent::SendFundingLocked { ref node_id, ref msg }) = msg_events.get(0) {
-                               idx += 1;
-                               assert_eq!(*node_id, $dst_node.node.get_our_node_id());
-                               Some(msg.clone())
-                       } else {
-                               None
-                       };
-
-                       let mut revoke_and_ack = None;
-                       let mut commitment_update = None;
-                       let order = if let Some(ev) = msg_events.get(idx) {
-                               idx += 1;
-                               match ev {
-                                       &MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
-                                               assert_eq!(*node_id, $dst_node.node.get_our_node_id());
-                                               revoke_and_ack = Some(msg.clone());
-                                               RAACommitmentOrder::RevokeAndACKFirst
-                                       },
-                                       &MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
-                                               assert_eq!(*node_id, $dst_node.node.get_our_node_id());
-                                               commitment_update = Some(updates.clone());
-                                               RAACommitmentOrder::CommitmentFirst
-                                       },
-                                       _ => panic!("Unexpected event"),
-                               }
-                       } else {
-                               RAACommitmentOrder::CommitmentFirst
-                       };
-
-                       if let Some(ev) = msg_events.get(idx) {
-                               match ev {
-                                       &MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
-                                               assert_eq!(*node_id, $dst_node.node.get_our_node_id());
-                                               assert!(revoke_and_ack.is_none());
-                                               revoke_and_ack = Some(msg.clone());
-                                       },
-                                       &MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
-                                               assert_eq!(*node_id, $dst_node.node.get_our_node_id());
-                                               assert!(commitment_update.is_none());
-                                               commitment_update = Some(updates.clone());
-                                       },
-                                       _ => panic!("Unexpected event"),
-                               }
-                       }
-
-                       (funding_locked, revoke_and_ack, commitment_update, order)
-               }
-       }
-}
-
-/// pending_htlc_adds includes both the holding cell and in-flight update_add_htlcs, whereas
-/// for claims/fails they are separated out.
-pub fn reconnect_nodes(node_a: &Node, node_b: &Node, send_funding_locked: (bool, bool), pending_htlc_adds: (i64, i64), pending_htlc_claims: (usize, usize), pending_cell_htlc_claims: (usize, usize), pending_cell_htlc_fails: (usize, usize), pending_raa: (bool, bool)) {
-       node_a.node.peer_connected(&node_b.node.get_our_node_id());
-       let reestablish_1 = get_chan_reestablish_msgs!(node_a, node_b);
-       node_b.node.peer_connected(&node_a.node.get_our_node_id());
-       let reestablish_2 = get_chan_reestablish_msgs!(node_b, node_a);
-
-       if send_funding_locked.0 {
-               // If a expects a funding_locked, it better not think it has received a revoke_and_ack
-               // from b
-               for reestablish in reestablish_1.iter() {
-                       assert_eq!(reestablish.next_remote_commitment_number, 0);
-               }
-       }
-       if send_funding_locked.1 {
-               // If b expects a funding_locked, it better not think it has received a revoke_and_ack
-               // from a
-               for reestablish in reestablish_2.iter() {
-                       assert_eq!(reestablish.next_remote_commitment_number, 0);
-               }
-       }
-       if send_funding_locked.0 || send_funding_locked.1 {
-               // If we expect any funding_locked's, both sides better have set
-               // next_local_commitment_number to 1
-               for reestablish in reestablish_1.iter() {
-                       assert_eq!(reestablish.next_local_commitment_number, 1);
-               }
-               for reestablish in reestablish_2.iter() {
-                       assert_eq!(reestablish.next_local_commitment_number, 1);
-               }
-       }
-
-       let mut resp_1 = Vec::new();
-       for msg in reestablish_1 {
-               node_b.node.handle_channel_reestablish(&node_a.node.get_our_node_id(), &msg).unwrap();
-               resp_1.push(handle_chan_reestablish_msgs!(node_b, node_a));
-       }
-       if pending_cell_htlc_claims.0 != 0 || pending_cell_htlc_fails.0 != 0 {
-               check_added_monitors!(node_b, 1);
-       } else {
-               check_added_monitors!(node_b, 0);
-       }
-
-       let mut resp_2 = Vec::new();
-       for msg in reestablish_2 {
-               node_a.node.handle_channel_reestablish(&node_b.node.get_our_node_id(), &msg).unwrap();
-               resp_2.push(handle_chan_reestablish_msgs!(node_a, node_b));
-       }
-       if pending_cell_htlc_claims.1 != 0 || pending_cell_htlc_fails.1 != 0 {
-               check_added_monitors!(node_a, 1);
-       } else {
-               check_added_monitors!(node_a, 0);
-       }
-
-       // We don't yet support both needing updates, as that would require a different commitment dance:
-       assert!((pending_htlc_adds.0 == 0 && pending_htlc_claims.0 == 0 && pending_cell_htlc_claims.0 == 0 && pending_cell_htlc_fails.0 == 0) ||
-                       (pending_htlc_adds.1 == 0 && pending_htlc_claims.1 == 0 && pending_cell_htlc_claims.1 == 0 && pending_cell_htlc_fails.1 == 0));
-
-       for chan_msgs in resp_1.drain(..) {
-               if send_funding_locked.0 {
-                       node_a.node.handle_funding_locked(&node_b.node.get_our_node_id(), &chan_msgs.0.unwrap()).unwrap();
-                       let announcement_event = node_a.node.get_and_clear_pending_msg_events();
-                       if !announcement_event.is_empty() {
-                               assert_eq!(announcement_event.len(), 1);
-                               if let MessageSendEvent::SendAnnouncementSignatures { .. } = announcement_event[0] {
-                                       //TODO: Test announcement_sigs re-sending
-                               } else { panic!("Unexpected event!"); }
-                       }
-               } else {
-                       assert!(chan_msgs.0.is_none());
-               }
-               if pending_raa.0 {
-                       assert!(chan_msgs.3 == RAACommitmentOrder::RevokeAndACKFirst);
-                       node_a.node.handle_revoke_and_ack(&node_b.node.get_our_node_id(), &chan_msgs.1.unwrap()).unwrap();
-                       assert!(node_a.node.get_and_clear_pending_msg_events().is_empty());
-                       check_added_monitors!(node_a, 1);
-               } else {
-                       assert!(chan_msgs.1.is_none());
-               }
-               if pending_htlc_adds.0 != 0 || pending_htlc_claims.0 != 0 || pending_cell_htlc_claims.0 != 0 || pending_cell_htlc_fails.0 != 0 {
-                       let commitment_update = chan_msgs.2.unwrap();
-                       if pending_htlc_adds.0 != -1 { // We use -1 to denote a response commitment_signed
-                               assert_eq!(commitment_update.update_add_htlcs.len(), pending_htlc_adds.0 as usize);
-                       } else {
-                               assert!(commitment_update.update_add_htlcs.is_empty());
-                       }
-                       assert_eq!(commitment_update.update_fulfill_htlcs.len(), pending_htlc_claims.0 + pending_cell_htlc_claims.0);
-                       assert_eq!(commitment_update.update_fail_htlcs.len(), pending_cell_htlc_fails.0);
-                       assert!(commitment_update.update_fail_malformed_htlcs.is_empty());
-                       for update_add in commitment_update.update_add_htlcs {
-                               node_a.node.handle_update_add_htlc(&node_b.node.get_our_node_id(), &update_add).unwrap();
-                       }
-                       for update_fulfill in commitment_update.update_fulfill_htlcs {
-                               node_a.node.handle_update_fulfill_htlc(&node_b.node.get_our_node_id(), &update_fulfill).unwrap();
-                       }
-                       for update_fail in commitment_update.update_fail_htlcs {
-                               node_a.node.handle_update_fail_htlc(&node_b.node.get_our_node_id(), &update_fail).unwrap();
-                       }
-
-                       if pending_htlc_adds.0 != -1 { // We use -1 to denote a response commitment_signed
-                               commitment_signed_dance!(node_a, node_b, commitment_update.commitment_signed, false);
-                       } else {
-                               node_a.node.handle_commitment_signed(&node_b.node.get_our_node_id(), &commitment_update.commitment_signed).unwrap();
-                               check_added_monitors!(node_a, 1);
-                               let as_revoke_and_ack = get_event_msg!(node_a, MessageSendEvent::SendRevokeAndACK, node_b.node.get_our_node_id());
-                               // No commitment_signed so get_event_msg's assert(len == 1) passes
-                               node_b.node.handle_revoke_and_ack(&node_a.node.get_our_node_id(), &as_revoke_and_ack).unwrap();
-                               assert!(node_b.node.get_and_clear_pending_msg_events().is_empty());
-                               check_added_monitors!(node_b, 1);
-                       }
-               } else {
-                       assert!(chan_msgs.2.is_none());
-               }
-       }
-
-       for chan_msgs in resp_2.drain(..) {
-               if send_funding_locked.1 {
-                       node_b.node.handle_funding_locked(&node_a.node.get_our_node_id(), &chan_msgs.0.unwrap()).unwrap();
-                       let announcement_event = node_b.node.get_and_clear_pending_msg_events();
-                       if !announcement_event.is_empty() {
-                               assert_eq!(announcement_event.len(), 1);
-                               if let MessageSendEvent::SendAnnouncementSignatures { .. } = announcement_event[0] {
-                                       //TODO: Test announcement_sigs re-sending
-                               } else { panic!("Unexpected event!"); }
-                       }
-               } else {
-                       assert!(chan_msgs.0.is_none());
-               }
-               if pending_raa.1 {
-                       assert!(chan_msgs.3 == RAACommitmentOrder::RevokeAndACKFirst);
-                       node_b.node.handle_revoke_and_ack(&node_a.node.get_our_node_id(), &chan_msgs.1.unwrap()).unwrap();
-                       assert!(node_b.node.get_and_clear_pending_msg_events().is_empty());
-                       check_added_monitors!(node_b, 1);
-               } else {
-                       assert!(chan_msgs.1.is_none());
-               }
-               if pending_htlc_adds.1 != 0 || pending_htlc_claims.1 != 0 || pending_cell_htlc_claims.1 != 0 || pending_cell_htlc_fails.1 != 0 {
-                       let commitment_update = chan_msgs.2.unwrap();
-                       if pending_htlc_adds.1 != -1 { // We use -1 to denote a response commitment_signed
-                               assert_eq!(commitment_update.update_add_htlcs.len(), pending_htlc_adds.1 as usize);
-                       }
-                       assert_eq!(commitment_update.update_fulfill_htlcs.len(), pending_htlc_claims.0 + pending_cell_htlc_claims.0);
-                       assert_eq!(commitment_update.update_fail_htlcs.len(), pending_cell_htlc_fails.0);
-                       assert!(commitment_update.update_fail_malformed_htlcs.is_empty());
-                       for update_add in commitment_update.update_add_htlcs {
-                               node_b.node.handle_update_add_htlc(&node_a.node.get_our_node_id(), &update_add).unwrap();
-                       }
-                       for update_fulfill in commitment_update.update_fulfill_htlcs {
-                               node_b.node.handle_update_fulfill_htlc(&node_a.node.get_our_node_id(), &update_fulfill).unwrap();
-                       }
-                       for update_fail in commitment_update.update_fail_htlcs {
-                               node_b.node.handle_update_fail_htlc(&node_a.node.get_our_node_id(), &update_fail).unwrap();
-                       }
-
-                       if pending_htlc_adds.1 != -1 { // We use -1 to denote a response commitment_signed
-                               commitment_signed_dance!(node_b, node_a, commitment_update.commitment_signed, false);
-                       } else {
-                               node_b.node.handle_commitment_signed(&node_a.node.get_our_node_id(), &commitment_update.commitment_signed).unwrap();
-                               check_added_monitors!(node_b, 1);
-                               let bs_revoke_and_ack = get_event_msg!(node_b, MessageSendEvent::SendRevokeAndACK, node_a.node.get_our_node_id());
-                               // No commitment_signed so get_event_msg's assert(len == 1) passes
-                               node_a.node.handle_revoke_and_ack(&node_b.node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
-                               assert!(node_a.node.get_and_clear_pending_msg_events().is_empty());
-                               check_added_monitors!(node_a, 1);
-                       }
-               } else {
-                       assert!(chan_msgs.2.is_none());
-               }
-       }
-}
diff --git a/src/ln/functional_tests.rs b/src/ln/functional_tests.rs
deleted file mode 100644 (file)
index 044c167..0000000
+++ /dev/null
@@ -1,6114 +0,0 @@
-//! Tests that test standing up a network of ChannelManagers, creating channels, sending
-//! payments/messages between them, and often checking the resulting ChannelMonitors are able to
-//! claim outputs on-chain.
-
-use chain::transaction::OutPoint;
-use chain::chaininterface::{ChainListener, ChainWatchInterface, ChainWatchInterfaceUtil};
-use chain::keysinterface::{KeysInterface, SpendableOutputDescriptor, KeysManager};
-use chain::keysinterface;
-use ln::channel::{COMMITMENT_TX_BASE_WEIGHT, COMMITMENT_TX_WEIGHT_PER_HTLC};
-use ln::channelmanager::{ChannelManager,ChannelManagerReadArgs,HTLCForwardInfo,RAACommitmentOrder, PaymentPreimage, PaymentHash, BREAKDOWN_TIMEOUT};
-use ln::channelmonitor::{ChannelMonitor, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ManyChannelMonitor, ANTI_REORG_DELAY};
-use ln::channel::{ACCEPTED_HTLC_SCRIPT_WEIGHT, OFFERED_HTLC_SCRIPT_WEIGHT, Channel, ChannelError};
-use ln::onion_utils;
-use ln::router::{Route, RouteHop};
-use ln::msgs;
-use ln::msgs::{ChannelMessageHandler,RoutingMessageHandler,HTLCFailChannelUpdate, LocalFeatures, ErrorAction};
-use util::test_utils;
-use util::events::{Event, EventsProvider, MessageSendEvent, MessageSendEventsProvider};
-use util::errors::APIError;
-use util::ser::{Writeable, ReadableArgs};
-use util::config::UserConfig;
-use util::logger::Logger;
-
-use bitcoin::util::hash::BitcoinHash;
-use bitcoin_hashes::sha256d::Hash as Sha256dHash;
-use bitcoin::util::bip143;
-use bitcoin::util::address::Address;
-use bitcoin::util::bip32::{ChildNumber, ExtendedPubKey, ExtendedPrivKey};
-use bitcoin::blockdata::block::{Block, BlockHeader};
-use bitcoin::blockdata::transaction::{Transaction, TxOut, TxIn, SigHashType, OutPoint as BitcoinOutPoint};
-use bitcoin::blockdata::script::{Builder, Script};
-use bitcoin::blockdata::opcodes;
-use bitcoin::blockdata::constants::genesis_block;
-use bitcoin::network::constants::Network;
-
-use bitcoin_hashes::sha256::Hash as Sha256;
-use bitcoin_hashes::Hash;
-
-use secp256k1::{Secp256k1, Message};
-use secp256k1::key::{PublicKey,SecretKey};
-
-use std::collections::{BTreeSet, HashMap, HashSet};
-use std::default::Default;
-use std::sync::{Arc, Mutex};
-use std::sync::atomic::Ordering;
-use std::mem;
-
-use rand::{thread_rng, Rng};
-
-use ln::functional_test_utils::*;
-
-#[test]
-fn test_insane_channel_opens() {
-       // Stand up a network of 2 nodes
-       let nodes = create_network(2, &[None, None]);
-
-       // Instantiate channel parameters where we push the maximum msats given our
-       // funding satoshis
-       let channel_value_sat = 31337; // same as funding satoshis
-       let channel_reserve_satoshis = Channel::get_our_channel_reserve_satoshis(channel_value_sat);
-       let push_msat = (channel_value_sat - channel_reserve_satoshis) * 1000;
-
-       // Have node0 initiate a channel to node1 with aforementioned parameters
-       nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_sat, push_msat, 42).unwrap();
-
-       // Extract the channel open message from node0 to node1
-       let open_channel_message = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
-
-       // Test helper that asserts we get the correct error string given a mutator
-       // that supposedly makes the channel open message insane
-       let insane_open_helper = |expected_error_str, message_mutator: fn(msgs::OpenChannel) -> msgs::OpenChannel| {
-               match nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), LocalFeatures::new(), &message_mutator(open_channel_message.clone())) {
-                       Err(msgs::HandleError{ err: error_str, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) => {
-                               assert_eq!(error_str, expected_error_str, "unexpected HandleError string (expected `{}`, actual `{}`)", expected_error_str, error_str)
-                       },
-                       Err(msgs::HandleError{..}) => {panic!("unexpected HandleError action")},
-                       _ => panic!("insane OpenChannel message was somehow Ok"),
-               }
-       };
-
-       use ln::channel::MAX_FUNDING_SATOSHIS;
-       use ln::channelmanager::MAX_LOCAL_BREAKDOWN_TIMEOUT;
-
-       // Test all mutations that would make the channel open message insane
-       insane_open_helper("funding value > 2^24", |mut msg| { msg.funding_satoshis = MAX_FUNDING_SATOSHIS; msg });
-
-       insane_open_helper("Bogus channel_reserve_satoshis", |mut msg| { msg.channel_reserve_satoshis = msg.funding_satoshis + 1; msg });
-
-       insane_open_helper("push_msat larger than funding value", |mut msg| { msg.push_msat = (msg.funding_satoshis - msg.channel_reserve_satoshis) * 1000 + 1; msg });
-
-       insane_open_helper("Peer never wants payout outputs?", |mut msg| { msg.dust_limit_satoshis = msg.funding_satoshis + 1 ; msg });
-
-       insane_open_helper("Bogus; channel reserve is less than dust limit", |mut msg| { msg.dust_limit_satoshis = msg.channel_reserve_satoshis + 1; msg });
-
-       insane_open_helper("Minimum htlc value is full channel value", |mut msg| { msg.htlc_minimum_msat = (msg.funding_satoshis - msg.channel_reserve_satoshis) * 1000; msg });
-
-       insane_open_helper("They wanted our payments to be delayed by a needlessly long period", |mut msg| { msg.to_self_delay = MAX_LOCAL_BREAKDOWN_TIMEOUT + 1; msg });
-
-       insane_open_helper("0 max_accpted_htlcs makes for a useless channel", |mut msg| { msg.max_accepted_htlcs = 0; msg });
-
-       insane_open_helper("max_accpted_htlcs > 483", |mut msg| { msg.max_accepted_htlcs = 484; msg });
-}
-
-#[test]
-fn test_async_inbound_update_fee() {
-       let mut nodes = create_network(2, &[None, None]);
-       let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-       let channel_id = chan.2;
-
-       // balancing
-       send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
-
-       // A                                        B
-       // update_fee                            ->
-       // send (1) commitment_signed            -.
-       //                                       <- update_add_htlc/commitment_signed
-       // send (2) RAA (awaiting remote revoke) -.
-       // (1) commitment_signed is delivered    ->
-       //                                       .- send (3) RAA (awaiting remote revoke)
-       // (2) RAA is delivered                  ->
-       //                                       .- send (4) commitment_signed
-       //                                       <- (3) RAA is delivered
-       // send (5) commitment_signed            -.
-       //                                       <- (4) commitment_signed is delivered
-       // send (6) RAA                          -.
-       // (5) commitment_signed is delivered    ->
-       //                                       <- RAA
-       // (6) RAA is delivered                  ->
-
-       // First nodes[0] generates an update_fee
-       nodes[0].node.update_fee(channel_id, get_feerate!(nodes[0], channel_id) + 20).unwrap();
-       check_added_monitors!(nodes[0], 1);
-
-       let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
-       assert_eq!(events_0.len(), 1);
-       let (update_msg, commitment_signed) = match events_0[0] { // (1)
-               MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, ref commitment_signed, .. }, .. } => {
-                       (update_fee.as_ref(), commitment_signed)
-               },
-               _ => panic!("Unexpected event"),
-       };
-
-       nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap()).unwrap();
-
-       // ...but before it's delivered, nodes[1] starts to send a payment back to nodes[0]...
-       let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
-       nodes[1].node.send_payment(nodes[1].router.get_route(&nodes[0].node.get_our_node_id(), None, &Vec::new(), 40000, TEST_FINAL_CLTV).unwrap(), our_payment_hash).unwrap();
-       check_added_monitors!(nodes[1], 1);
-
-       let payment_event = {
-               let mut events_1 = nodes[1].node.get_and_clear_pending_msg_events();
-               assert_eq!(events_1.len(), 1);
-               SendEvent::from_event(events_1.remove(0))
-       };
-       assert_eq!(payment_event.node_id, nodes[0].node.get_our_node_id());
-       assert_eq!(payment_event.msgs.len(), 1);
-
-       // ...now when the messages get delivered everyone should be happy
-       nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
-       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &payment_event.commitment_msg).unwrap(); // (2)
-       let as_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
-       // nodes[0] is awaiting nodes[1] revoke_and_ack so get_event_msg's assert(len == 1) passes
-       check_added_monitors!(nodes[0], 1);
-
-       // deliver(1), generate (3):
-       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed).unwrap();
-       let bs_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
-       // nodes[1] is awaiting nodes[0] revoke_and_ack so get_event_msg's assert(len == 1) passes
-       check_added_monitors!(nodes[1], 1);
-
-       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_and_ack).unwrap(); // deliver (2)
-       let bs_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-       assert!(bs_update.update_add_htlcs.is_empty()); // (4)
-       assert!(bs_update.update_fulfill_htlcs.is_empty()); // (4)
-       assert!(bs_update.update_fail_htlcs.is_empty()); // (4)
-       assert!(bs_update.update_fail_malformed_htlcs.is_empty()); // (4)
-       assert!(bs_update.update_fee.is_none()); // (4)
-       check_added_monitors!(nodes[1], 1);
-
-       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack).unwrap(); // deliver (3)
-       let as_update = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
-       assert!(as_update.update_add_htlcs.is_empty()); // (5)
-       assert!(as_update.update_fulfill_htlcs.is_empty()); // (5)
-       assert!(as_update.update_fail_htlcs.is_empty()); // (5)
-       assert!(as_update.update_fail_malformed_htlcs.is_empty()); // (5)
-       assert!(as_update.update_fee.is_none()); // (5)
-       check_added_monitors!(nodes[0], 1);
-
-       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_update.commitment_signed).unwrap(); // deliver (4)
-       let as_second_revoke = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
-       // only (6) so get_event_msg's assert(len == 1) passes
-       check_added_monitors!(nodes[0], 1);
-
-       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_update.commitment_signed).unwrap(); // deliver (5)
-       let bs_second_revoke = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
-       check_added_monitors!(nodes[1], 1);
-
-       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_revoke).unwrap();
-       check_added_monitors!(nodes[0], 1);
-
-       let events_2 = nodes[0].node.get_and_clear_pending_events();
-       assert_eq!(events_2.len(), 1);
-       match events_2[0] {
-               Event::PendingHTLCsForwardable {..} => {}, // If we actually processed we'd receive the payment
-               _ => panic!("Unexpected event"),
-       }
-
-       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_second_revoke).unwrap(); // deliver (6)
-       check_added_monitors!(nodes[1], 1);
-}
-
-#[test]
-fn test_update_fee_unordered_raa() {
-       // Just the intro to the previous test followed by an out-of-order RAA (which caused a
-       // crash in an earlier version of the update_fee patch)
-       let mut nodes = create_network(2, &[None, None]);
-       let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-       let channel_id = chan.2;
-
-       // balancing
-       send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
-
-       // First nodes[0] generates an update_fee
-       nodes[0].node.update_fee(channel_id, get_feerate!(nodes[0], channel_id) + 20).unwrap();
-       check_added_monitors!(nodes[0], 1);
-
-       let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
-       assert_eq!(events_0.len(), 1);
-       let update_msg = match events_0[0] { // (1)
-               MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, .. }, .. } => {
-                       update_fee.as_ref()
-               },
-               _ => panic!("Unexpected event"),
-       };
-
-       nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap()).unwrap();
-
-       // ...but before it's delivered, nodes[1] starts to send a payment back to nodes[0]...
-       let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
-       nodes[1].node.send_payment(nodes[1].router.get_route(&nodes[0].node.get_our_node_id(), None, &Vec::new(), 40000, TEST_FINAL_CLTV).unwrap(), our_payment_hash).unwrap();
-       check_added_monitors!(nodes[1], 1);
-
-       let payment_event = {
-               let mut events_1 = nodes[1].node.get_and_clear_pending_msg_events();
-               assert_eq!(events_1.len(), 1);
-               SendEvent::from_event(events_1.remove(0))
-       };
-       assert_eq!(payment_event.node_id, nodes[0].node.get_our_node_id());
-       assert_eq!(payment_event.msgs.len(), 1);
-
-       // ...now when the messages get delivered everyone should be happy
-       nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
-       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &payment_event.commitment_msg).unwrap(); // (2)
-       let as_revoke_msg = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
-       // nodes[0] is awaiting nodes[1] revoke_and_ack so get_event_msg's assert(len == 1) passes
-       check_added_monitors!(nodes[0], 1);
-
-       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_msg).unwrap(); // deliver (2)
-       check_added_monitors!(nodes[1], 1);
-
-       // We can't continue, sadly, because our (1) now has a bogus signature
-}
-
-#[test]
-fn test_multi_flight_update_fee() {
-       let nodes = create_network(2, &[None, None]);
-       let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-       let channel_id = chan.2;
-
-       // A                                        B
-       // update_fee/commitment_signed          ->
-       //                                       .- send (1) RAA and (2) commitment_signed
-       // update_fee (never committed)          ->
-       // (3) update_fee                        ->
-       // We have to manually generate the above update_fee, it is allowed by the protocol but we
-       // don't track which updates correspond to which revoke_and_ack responses so we're in
-       // AwaitingRAA mode and will not generate the update_fee yet.
-       //                                       <- (1) RAA delivered
-       // (3) is generated and send (4) CS      -.
-       // Note that A cannot generate (4) prior to (1) being delivered as it otherwise doesn't
-       // know the per_commitment_point to use for it.
-       //                                       <- (2) commitment_signed delivered
-       // revoke_and_ack                        ->
-       //                                          B should send no response here
-       // (4) commitment_signed delivered       ->
-       //                                       <- RAA/commitment_signed delivered
-       // revoke_and_ack                        ->
-
-       // First nodes[0] generates an update_fee
-       let initial_feerate = get_feerate!(nodes[0], channel_id);
-       nodes[0].node.update_fee(channel_id, initial_feerate + 20).unwrap();
-       check_added_monitors!(nodes[0], 1);
-
-       let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
-       assert_eq!(events_0.len(), 1);
-       let (update_msg_1, commitment_signed_1) = match events_0[0] { // (1)
-               MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, ref commitment_signed, .. }, .. } => {
-                       (update_fee.as_ref().unwrap(), commitment_signed)
-               },
-               _ => panic!("Unexpected event"),
-       };
-
-       // Deliver first update_fee/commitment_signed pair, generating (1) and (2):
-       nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg_1).unwrap();
-       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed_1).unwrap();
-       let (bs_revoke_msg, bs_commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-       check_added_monitors!(nodes[1], 1);
-
-       // nodes[0] is awaiting a revoke from nodes[1] before it will create a new commitment
-       // transaction:
-       nodes[0].node.update_fee(channel_id, initial_feerate + 40).unwrap();
-       assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
-       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
-
-       // Create the (3) update_fee message that nodes[0] will generate before it does...
-       let mut update_msg_2 = msgs::UpdateFee {
-               channel_id: update_msg_1.channel_id.clone(),
-               feerate_per_kw: (initial_feerate + 30) as u32,
-       };
-
-       nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &update_msg_2).unwrap();
-
-       update_msg_2.feerate_per_kw = (initial_feerate + 40) as u32;
-       // Deliver (3)
-       nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &update_msg_2).unwrap();
-
-       // Deliver (1), generating (3) and (4)
-       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_msg).unwrap();
-       let as_second_update = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
-       check_added_monitors!(nodes[0], 1);
-       assert!(as_second_update.update_add_htlcs.is_empty());
-       assert!(as_second_update.update_fulfill_htlcs.is_empty());
-       assert!(as_second_update.update_fail_htlcs.is_empty());
-       assert!(as_second_update.update_fail_malformed_htlcs.is_empty());
-       // Check that the update_fee newly generated matches what we delivered:
-       assert_eq!(as_second_update.update_fee.as_ref().unwrap().channel_id, update_msg_2.channel_id);
-       assert_eq!(as_second_update.update_fee.as_ref().unwrap().feerate_per_kw, update_msg_2.feerate_per_kw);
-
-       // Deliver (2) commitment_signed
-       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_commitment_signed).unwrap();
-       let as_revoke_msg = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
-       check_added_monitors!(nodes[0], 1);
-       // No commitment_signed so get_event_msg's assert(len == 1) passes
-
-       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_msg).unwrap();
-       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-       check_added_monitors!(nodes[1], 1);
-
-       // Delever (4)
-       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_second_update.commitment_signed).unwrap();
-       let (bs_second_revoke, bs_second_commitment) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-       check_added_monitors!(nodes[1], 1);
-
-       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_revoke).unwrap();
-       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
-       check_added_monitors!(nodes[0], 1);
-
-       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_second_commitment).unwrap();
-       let as_second_revoke = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
-       // No commitment_signed so get_event_msg's assert(len == 1) passes
-       check_added_monitors!(nodes[0], 1);
-
-       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_second_revoke).unwrap();
-       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-       check_added_monitors!(nodes[1], 1);
-}
-
-#[test]
-fn test_update_fee_vanilla() {
-       let nodes = create_network(2, &[None, None]);
-       let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-       let channel_id = chan.2;
-
-       let feerate = get_feerate!(nodes[0], channel_id);
-       nodes[0].node.update_fee(channel_id, feerate+25).unwrap();
-       check_added_monitors!(nodes[0], 1);
-
-       let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
-       assert_eq!(events_0.len(), 1);
-       let (update_msg, commitment_signed) = match events_0[0] {
-                       MessageSendEvent::UpdateHTLCs { node_id:_, updates: msgs::CommitmentUpdate { update_add_htlcs:_, update_fulfill_htlcs:_, update_fail_htlcs:_, update_fail_malformed_htlcs:_, ref update_fee, ref commitment_signed } } => {
-                       (update_fee.as_ref(), commitment_signed)
-               },
-               _ => panic!("Unexpected event"),
-       };
-       nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap()).unwrap();
-
-       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed).unwrap();
-       let (revoke_msg, commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-       check_added_monitors!(nodes[1], 1);
-
-       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke_msg).unwrap();
-       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
-       check_added_monitors!(nodes[0], 1);
-
-       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed).unwrap();
-       let revoke_msg = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
-       // No commitment_signed so get_event_msg's assert(len == 1) passes
-       check_added_monitors!(nodes[0], 1);
-
-       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke_msg).unwrap();
-       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-       check_added_monitors!(nodes[1], 1);
-}
-
-#[test]
-fn test_update_fee_that_funder_cannot_afford() {
-       let nodes = create_network(2, &[None, None]);
-       let channel_value = 1888;
-       let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, channel_value, 700000, LocalFeatures::new(), LocalFeatures::new());
-       let channel_id = chan.2;
-
-       let feerate = 260;
-       nodes[0].node.update_fee(channel_id, feerate).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       let update_msg = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
-
-       nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &update_msg.update_fee.unwrap()).unwrap();
-
-       commitment_signed_dance!(nodes[1], nodes[0], update_msg.commitment_signed, false);
-
-       //Confirm that the new fee based on the last local commitment txn is what we expected based on the feerate of 260 set above.
-       //This value results in a fee that is exactly what the funder can afford (277 sat + 1000 sat channel reserve)
-       {
-               let chan_lock = nodes[1].node.channel_state.lock().unwrap();
-               let chan = chan_lock.by_id.get(&channel_id).unwrap();
-
-               //We made sure neither party's funds are below the dust limit so -2 non-HTLC txns from number of outputs
-               let num_htlcs = chan.last_local_commitment_txn[0].output.len() - 2;
-               let total_fee: u64 = feerate * (COMMITMENT_TX_BASE_WEIGHT + (num_htlcs as u64) * COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000;
-               let mut actual_fee = chan.last_local_commitment_txn[0].output.iter().fold(0, |acc, output| acc + output.value);
-               actual_fee = channel_value - actual_fee;
-               assert_eq!(total_fee, actual_fee);
-       } //drop the mutex
-
-       //Add 2 to the previous fee rate to the final fee increases by 1 (with no HTLCs the fee is essentially
-       //fee_rate*(724/1000) so the increment of 1*0.724 is rounded back down)
-       nodes[0].node.update_fee(channel_id, feerate+2).unwrap();
-       check_added_monitors!(nodes[0], 1);
-
-       let update2_msg = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
-
-       nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &update2_msg.update_fee.unwrap()).unwrap();
-
-       //While producing the commitment_signed response after handling a received update_fee request the
-       //check to see if the funder, who sent the update_fee request, can afford the new fee (funder_balance >= fee+channel_reserve)
-       //Should produce and error.
-       let err = nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &update2_msg.commitment_signed).unwrap_err();
-
-       assert!(match err.err {
-               "Funding remote cannot afford proposed new fee" => true,
-               _ => false,
-       });
-
-       //clear the message we could not handle
-       nodes[1].node.get_and_clear_pending_msg_events();
-}
-
-#[test]
-fn test_update_fee_with_fundee_update_add_htlc() {
-       let mut nodes = create_network(2, &[None, None]);
-       let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-       let channel_id = chan.2;
-
-       // balancing
-       send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
-
-       let feerate = get_feerate!(nodes[0], channel_id);
-       nodes[0].node.update_fee(channel_id, feerate+20).unwrap();
-       check_added_monitors!(nodes[0], 1);
-
-       let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
-       assert_eq!(events_0.len(), 1);
-       let (update_msg, commitment_signed) = match events_0[0] {
-                       MessageSendEvent::UpdateHTLCs { node_id:_, updates: msgs::CommitmentUpdate { update_add_htlcs:_, update_fulfill_htlcs:_, update_fail_htlcs:_, update_fail_malformed_htlcs:_, ref update_fee, ref commitment_signed } } => {
-                       (update_fee.as_ref(), commitment_signed)
-               },
-               _ => panic!("Unexpected event"),
-       };
-       nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap()).unwrap();
-       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed).unwrap();
-       let (revoke_msg, commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-       check_added_monitors!(nodes[1], 1);
-
-       let route = nodes[1].router.get_route(&nodes[0].node.get_our_node_id(), None, &Vec::new(), 800000, TEST_FINAL_CLTV).unwrap();
-
-       let (our_payment_preimage, our_payment_hash) = get_payment_preimage_hash!(nodes[1]);
-
-       // nothing happens since node[1] is in AwaitingRemoteRevoke
-       nodes[1].node.send_payment(route, our_payment_hash).unwrap();
-       {
-               let mut added_monitors = nodes[0].chan_monitor.added_monitors.lock().unwrap();
-               assert_eq!(added_monitors.len(), 0);
-               added_monitors.clear();
-       }
-       assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
-       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
-       // node[1] has nothing to do
-
-       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke_msg).unwrap();
-       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
-       check_added_monitors!(nodes[0], 1);
-
-       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed).unwrap();
-       let revoke_msg = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
-       // No commitment_signed so get_event_msg's assert(len == 1) passes
-       check_added_monitors!(nodes[0], 1);
-       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke_msg).unwrap();
-       check_added_monitors!(nodes[1], 1);
-       // AwaitingRemoteRevoke ends here
-
-       let commitment_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-       assert_eq!(commitment_update.update_add_htlcs.len(), 1);
-       assert_eq!(commitment_update.update_fulfill_htlcs.len(), 0);
-       assert_eq!(commitment_update.update_fail_htlcs.len(), 0);
-       assert_eq!(commitment_update.update_fail_malformed_htlcs.len(), 0);
-       assert_eq!(commitment_update.update_fee.is_none(), true);
-
-       nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &commitment_update.update_add_htlcs[0]).unwrap();
-       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_update.commitment_signed).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       let (revoke, commitment_signed) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
-
-       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke).unwrap();
-       check_added_monitors!(nodes[1], 1);
-       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-
-       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &commitment_signed).unwrap();
-       check_added_monitors!(nodes[1], 1);
-       let revoke = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
-       // No commitment_signed so get_event_msg's assert(len == 1) passes
-
-       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
-
-       expect_pending_htlcs_forwardable!(nodes[0]);
-
-       let events = nodes[0].node.get_and_clear_pending_events();
-       assert_eq!(events.len(), 1);
-       match events[0] {
-               Event::PaymentReceived { .. } => { },
-               _ => panic!("Unexpected event"),
-       };
-
-       claim_payment(&nodes[1], &vec!(&nodes[0])[..], our_payment_preimage);
-
-       send_payment(&nodes[1], &vec!(&nodes[0])[..], 800000);
-       send_payment(&nodes[0], &vec!(&nodes[1])[..], 800000);
-       close_channel(&nodes[0], &nodes[1], &chan.2, chan.3, true);
-}
-
-#[test]
-fn test_update_fee() {
-       let nodes = create_network(2, &[None, None]);
-       let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-       let channel_id = chan.2;
-
-       // A                                        B
-       // (1) update_fee/commitment_signed      ->
-       //                                       <- (2) revoke_and_ack
-       //                                       .- send (3) commitment_signed
-       // (4) update_fee/commitment_signed      ->
-       //                                       .- send (5) revoke_and_ack (no CS as we're awaiting a revoke)
-       //                                       <- (3) commitment_signed delivered
-       // send (6) revoke_and_ack               -.
-       //                                       <- (5) deliver revoke_and_ack
-       // (6) deliver revoke_and_ack            ->
-       //                                       .- send (7) commitment_signed in response to (4)
-       //                                       <- (7) deliver commitment_signed
-       // revoke_and_ack                        ->
-
-       // Create and deliver (1)...
-       let feerate = get_feerate!(nodes[0], channel_id);
-       nodes[0].node.update_fee(channel_id, feerate+20).unwrap();
-       check_added_monitors!(nodes[0], 1);
-
-       let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
-       assert_eq!(events_0.len(), 1);
-       let (update_msg, commitment_signed) = match events_0[0] {
-                       MessageSendEvent::UpdateHTLCs { node_id:_, updates: msgs::CommitmentUpdate { update_add_htlcs:_, update_fulfill_htlcs:_, update_fail_htlcs:_, update_fail_malformed_htlcs:_, ref update_fee, ref commitment_signed } } => {
-                       (update_fee.as_ref(), commitment_signed)
-               },
-               _ => panic!("Unexpected event"),
-       };
-       nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap()).unwrap();
-
-       // Generate (2) and (3):
-       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed).unwrap();
-       let (revoke_msg, commitment_signed_0) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-       check_added_monitors!(nodes[1], 1);
-
-       // Deliver (2):
-       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke_msg).unwrap();
-       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
-       check_added_monitors!(nodes[0], 1);
-
-       // Create and deliver (4)...
-       nodes[0].node.update_fee(channel_id, feerate+30).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
-       assert_eq!(events_0.len(), 1);
-       let (update_msg, commitment_signed) = match events_0[0] {
-                       MessageSendEvent::UpdateHTLCs { node_id:_, updates: msgs::CommitmentUpdate { update_add_htlcs:_, update_fulfill_htlcs:_, update_fail_htlcs:_, update_fail_malformed_htlcs:_, ref update_fee, ref commitment_signed } } => {
-                       (update_fee.as_ref(), commitment_signed)
-               },
-               _ => panic!("Unexpected event"),
-       };
-
-       nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap()).unwrap();
-       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed).unwrap();
-       check_added_monitors!(nodes[1], 1);
-       // ... creating (5)
-       let revoke_msg = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
-       // No commitment_signed so get_event_msg's assert(len == 1) passes
-
-       // Handle (3), creating (6):
-       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed_0).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       let revoke_msg_0 = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
-       // No commitment_signed so get_event_msg's assert(len == 1) passes
-
-       // Deliver (5):
-       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke_msg).unwrap();
-       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
-       check_added_monitors!(nodes[0], 1);
-
-       // Deliver (6), creating (7):
-       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke_msg_0).unwrap();
-       let commitment_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-       assert!(commitment_update.update_add_htlcs.is_empty());
-       assert!(commitment_update.update_fulfill_htlcs.is_empty());
-       assert!(commitment_update.update_fail_htlcs.is_empty());
-       assert!(commitment_update.update_fail_malformed_htlcs.is_empty());
-       assert!(commitment_update.update_fee.is_none());
-       check_added_monitors!(nodes[1], 1);
-
-       // Deliver (7)
-       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_update.commitment_signed).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       let revoke_msg = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
-       // No commitment_signed so get_event_msg's assert(len == 1) passes
-
-       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke_msg).unwrap();
-       check_added_monitors!(nodes[1], 1);
-       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-
-       assert_eq!(get_feerate!(nodes[0], channel_id), feerate + 30);
-       assert_eq!(get_feerate!(nodes[1], channel_id), feerate + 30);
-       close_channel(&nodes[0], &nodes[1], &chan.2, chan.3, true);
-}
-
-#[test]
-fn pre_funding_lock_shutdown_test() {
-       // Test sending a shutdown prior to funding_locked after funding generation
-       let nodes = create_network(2, &[None, None]);
-       let tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 8000000, 0, LocalFeatures::new(), LocalFeatures::new());
-       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-       nodes[0].chain_monitor.block_connected_checked(&header, 1, &[&tx; 1], &[1; 1]);
-       nodes[1].chain_monitor.block_connected_checked(&header, 1, &[&tx; 1], &[1; 1]);
-
-       nodes[0].node.close_channel(&OutPoint::new(tx.txid(), 0).to_channel_id()).unwrap();
-       let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
-       nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown).unwrap();
-       let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
-       nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown).unwrap();
-
-       let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
-       nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed).unwrap();
-       let (_, node_1_closing_signed) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
-       nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed.unwrap()).unwrap();
-       let (_, node_0_none) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
-       assert!(node_0_none.is_none());
-
-       assert!(nodes[0].node.list_channels().is_empty());
-       assert!(nodes[1].node.list_channels().is_empty());
-}
-
-#[test]
-fn updates_shutdown_wait() {
-       // Test sending a shutdown with outstanding updates pending
-       let mut nodes = create_network(3, &[None, None, None]);
-       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-       let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
-       let route_1 = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap();
-       let route_2 = nodes[1].router.get_route(&nodes[0].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap();
-
-       let (our_payment_preimage, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 100000);
-
-       nodes[0].node.close_channel(&chan_1.2).unwrap();
-       let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
-       nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown).unwrap();
-       let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
-       nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown).unwrap();
-
-       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
-       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-
-       let (_, payment_hash) = get_payment_preimage_hash!(nodes[0]);
-       if let Err(APIError::ChannelUnavailable {..}) = nodes[0].node.send_payment(route_1, payment_hash) {}
-       else { panic!("New sends should fail!") };
-       if let Err(APIError::ChannelUnavailable {..}) = nodes[1].node.send_payment(route_2, payment_hash) {}
-       else { panic!("New sends should fail!") };
-
-       assert!(nodes[2].node.claim_funds(our_payment_preimage));
-       check_added_monitors!(nodes[2], 1);
-       let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
-       assert!(updates.update_add_htlcs.is_empty());
-       assert!(updates.update_fail_htlcs.is_empty());
-       assert!(updates.update_fail_malformed_htlcs.is_empty());
-       assert!(updates.update_fee.is_none());
-       assert_eq!(updates.update_fulfill_htlcs.len(), 1);
-       nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]).unwrap();
-       check_added_monitors!(nodes[1], 1);
-       let updates_2 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-       commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false);
-
-       assert!(updates_2.update_add_htlcs.is_empty());
-       assert!(updates_2.update_fail_htlcs.is_empty());
-       assert!(updates_2.update_fail_malformed_htlcs.is_empty());
-       assert!(updates_2.update_fee.is_none());
-       assert_eq!(updates_2.update_fulfill_htlcs.len(), 1);
-       nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates_2.update_fulfill_htlcs[0]).unwrap();
-       commitment_signed_dance!(nodes[0], nodes[1], updates_2.commitment_signed, false, true);
-
-       let events = nodes[0].node.get_and_clear_pending_events();
-       assert_eq!(events.len(), 1);
-       match events[0] {
-               Event::PaymentSent { ref payment_preimage } => {
-                       assert_eq!(our_payment_preimage, *payment_preimage);
-               },
-               _ => panic!("Unexpected event"),
-       }
-
-       let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
-       nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed).unwrap();
-       let (_, node_1_closing_signed) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
-       nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed.unwrap()).unwrap();
-       let (_, node_0_none) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
-       assert!(node_0_none.is_none());
-
-       assert!(nodes[0].node.list_channels().is_empty());
-
-       assert_eq!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
-       nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
-       close_channel(&nodes[1], &nodes[2], &chan_2.2, chan_2.3, true);
-       assert!(nodes[1].node.list_channels().is_empty());
-       assert!(nodes[2].node.list_channels().is_empty());
-}
-
-#[test]
-fn htlc_fail_async_shutdown() {
-       // Test HTLCs fail if shutdown starts even if messages are delivered out-of-order
-       let mut nodes = create_network(3, &[None, None, None]);
-       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-       let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
-
-       let route = nodes[0].router.get_route(&nodes[2].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap();
-       let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
-       nodes[0].node.send_payment(route, our_payment_hash).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
-       assert_eq!(updates.update_add_htlcs.len(), 1);
-       assert!(updates.update_fulfill_htlcs.is_empty());
-       assert!(updates.update_fail_htlcs.is_empty());
-       assert!(updates.update_fail_malformed_htlcs.is_empty());
-       assert!(updates.update_fee.is_none());
-
-       nodes[1].node.close_channel(&chan_1.2).unwrap();
-       let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
-       nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown).unwrap();
-       let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
-
-       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]).unwrap();
-       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &updates.commitment_signed).unwrap();
-       check_added_monitors!(nodes[1], 1);
-       nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown).unwrap();
-       commitment_signed_dance!(nodes[1], nodes[0], (), false, true, false);
-
-       let updates_2 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-       assert!(updates_2.update_add_htlcs.is_empty());
-       assert!(updates_2.update_fulfill_htlcs.is_empty());
-       assert_eq!(updates_2.update_fail_htlcs.len(), 1);
-       assert!(updates_2.update_fail_malformed_htlcs.is_empty());
-       assert!(updates_2.update_fee.is_none());
-
-       nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates_2.update_fail_htlcs[0]).unwrap();
-       commitment_signed_dance!(nodes[0], nodes[1], updates_2.commitment_signed, false, true);
-
-       let events = nodes[0].node.get_and_clear_pending_events();
-       assert_eq!(events.len(), 1);
-       match events[0] {
-               Event::PaymentFailed { ref payment_hash, ref rejected_by_dest, .. } => {
-                       assert_eq!(our_payment_hash, *payment_hash);
-                       assert!(!rejected_by_dest);
-               },
-               _ => panic!("Unexpected event"),
-       }
-
-       let msg_events = nodes[0].node.get_and_clear_pending_msg_events();
-       assert_eq!(msg_events.len(), 2);
-       let node_0_closing_signed = match msg_events[0] {
-               MessageSendEvent::SendClosingSigned { ref node_id, ref msg } => {
-                       assert_eq!(*node_id, nodes[1].node.get_our_node_id());
-                       (*msg).clone()
-               },
-               _ => panic!("Unexpected event"),
-       };
-       match msg_events[1] {
-               MessageSendEvent::PaymentFailureNetworkUpdate { update: msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg }} => {
-                       assert_eq!(msg.contents.short_channel_id, chan_1.0.contents.short_channel_id);
-               },
-               _ => panic!("Unexpected event"),
-       }
-
-       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-       nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed).unwrap();
-       let (_, node_1_closing_signed) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
-       nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed.unwrap()).unwrap();
-       let (_, node_0_none) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
-       assert!(node_0_none.is_none());
-
-       assert!(nodes[0].node.list_channels().is_empty());
-
-       assert_eq!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
-       nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
-       close_channel(&nodes[1], &nodes[2], &chan_2.2, chan_2.3, true);
-       assert!(nodes[1].node.list_channels().is_empty());
-       assert!(nodes[2].node.list_channels().is_empty());
-}
-
-fn do_test_shutdown_rebroadcast(recv_count: u8) {
-       // Test that shutdown/closing_signed is re-sent on reconnect with a variable number of
-       // messages delivered prior to disconnect
-       let nodes = create_network(3, &[None, None, None]);
-       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-       let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
-
-       let (our_payment_preimage, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 100000);
-
-       nodes[1].node.close_channel(&chan_1.2).unwrap();
-       let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
-       if recv_count > 0 {
-               nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown).unwrap();
-               let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
-               if recv_count > 1 {
-                       nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown).unwrap();
-               }
-       }
-
-       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
-       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
-
-       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id());
-       let node_0_reestablish = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id());
-       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id());
-       let node_1_reestablish = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id());
-
-       nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &node_0_reestablish).unwrap();
-       let node_1_2nd_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
-       assert!(node_1_shutdown == node_1_2nd_shutdown);
-
-       nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &node_1_reestablish).unwrap();
-       let node_0_2nd_shutdown = if recv_count > 0 {
-               let node_0_2nd_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
-               nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_2nd_shutdown).unwrap();
-               node_0_2nd_shutdown
-       } else {
-               assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
-               nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_2nd_shutdown).unwrap();
-               get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id())
-       };
-       nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_2nd_shutdown).unwrap();
-
-       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
-       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-
-       assert!(nodes[2].node.claim_funds(our_payment_preimage));
-       check_added_monitors!(nodes[2], 1);
-       let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
-       assert!(updates.update_add_htlcs.is_empty());
-       assert!(updates.update_fail_htlcs.is_empty());
-       assert!(updates.update_fail_malformed_htlcs.is_empty());
-       assert!(updates.update_fee.is_none());
-       assert_eq!(updates.update_fulfill_htlcs.len(), 1);
-       nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]).unwrap();
-       check_added_monitors!(nodes[1], 1);
-       let updates_2 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-       commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false);
-
-       assert!(updates_2.update_add_htlcs.is_empty());
-       assert!(updates_2.update_fail_htlcs.is_empty());
-       assert!(updates_2.update_fail_malformed_htlcs.is_empty());
-       assert!(updates_2.update_fee.is_none());
-       assert_eq!(updates_2.update_fulfill_htlcs.len(), 1);
-       nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates_2.update_fulfill_htlcs[0]).unwrap();
-       commitment_signed_dance!(nodes[0], nodes[1], updates_2.commitment_signed, false, true);
-
-       let events = nodes[0].node.get_and_clear_pending_events();
-       assert_eq!(events.len(), 1);
-       match events[0] {
-               Event::PaymentSent { ref payment_preimage } => {
-                       assert_eq!(our_payment_preimage, *payment_preimage);
-               },
-               _ => panic!("Unexpected event"),
-       }
-
-       let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
-       if recv_count > 0 {
-               nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed).unwrap();
-               let (_, node_1_closing_signed) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
-               assert!(node_1_closing_signed.is_some());
-       }
-
-       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
-       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
-
-       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id());
-       let node_0_2nd_reestablish = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id());
-       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id());
-       if recv_count == 0 {
-               // If all closing_signeds weren't delivered we can just resume where we left off...
-               let node_1_2nd_reestablish = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id());
-
-               nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &node_1_2nd_reestablish).unwrap();
-               let node_0_3rd_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
-               assert!(node_0_2nd_shutdown == node_0_3rd_shutdown);
-
-               nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &node_0_2nd_reestablish).unwrap();
-               let node_1_3rd_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
-               assert!(node_1_3rd_shutdown == node_1_2nd_shutdown);
-
-               nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_3rd_shutdown).unwrap();
-               assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-
-               nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_3rd_shutdown).unwrap();
-               let node_0_2nd_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
-               assert!(node_0_closing_signed == node_0_2nd_closing_signed);
-
-               nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed).unwrap();
-               let (_, node_1_closing_signed) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
-               nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed.unwrap()).unwrap();
-               let (_, node_0_none) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
-               assert!(node_0_none.is_none());
-       } else {
-               // If one node, however, received + responded with an identical closing_signed we end
-               // up erroring and node[0] will try to broadcast its own latest commitment transaction.
-               // There isn't really anything better we can do simply, but in the future we might
-               // explore storing a set of recently-closed channels that got disconnected during
-               // closing_signed and avoiding broadcasting local commitment txn for some timeout to
-               // give our counterparty enough time to (potentially) broadcast a cooperative closing
-               // transaction.
-               assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-
-               if let Err(msgs::HandleError{action: Some(msgs::ErrorAction::SendErrorMessage{msg}), ..}) =
-                               nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &node_0_2nd_reestablish) {
-                       nodes[0].node.handle_error(&nodes[1].node.get_our_node_id(), &msg);
-                       let msgs::ErrorMessage {ref channel_id, ..} = msg;
-                       assert_eq!(*channel_id, chan_1.2);
-               } else { panic!("Needed SendErrorMessage close"); }
-
-               // get_closing_signed_broadcast usually eats the BroadcastChannelUpdate for us and
-               // checks it, but in this case nodes[0] didn't ever get a chance to receive a
-               // closing_signed so we do it ourselves
-               check_closed_broadcast!(nodes[0]);
-       }
-
-       assert!(nodes[0].node.list_channels().is_empty());
-
-       assert_eq!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
-       nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
-       close_channel(&nodes[1], &nodes[2], &chan_2.2, chan_2.3, true);
-       assert!(nodes[1].node.list_channels().is_empty());
-       assert!(nodes[2].node.list_channels().is_empty());
-}
-
-#[test]
-fn test_shutdown_rebroadcast() {
-       do_test_shutdown_rebroadcast(0);
-       do_test_shutdown_rebroadcast(1);
-       do_test_shutdown_rebroadcast(2);
-}
-
-#[test]
-fn fake_network_test() {
-       // Simple test which builds a network of ChannelManagers, connects them to each other, and
-       // tests that payments get routed and transactions broadcast in semi-reasonable ways.
-       let nodes = create_network(4, &[None, None, None, None]);
-
-       // Create some initial channels
-       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-       let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
-       let chan_3 = create_announced_chan_between_nodes(&nodes, 2, 3, LocalFeatures::new(), LocalFeatures::new());
-
-       // Rebalance the network a bit by relaying one payment through all the channels...
-       send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 8000000);
-       send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 8000000);
-       send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 8000000);
-       send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 8000000);
-
-       // Send some more payments
-       send_payment(&nodes[1], &vec!(&nodes[2], &nodes[3])[..], 1000000);
-       send_payment(&nodes[3], &vec!(&nodes[2], &nodes[1], &nodes[0])[..], 1000000);
-       send_payment(&nodes[3], &vec!(&nodes[2], &nodes[1])[..], 1000000);
-
-       // Test failure packets
-       let payment_hash_1 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 1000000).1;
-       fail_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], payment_hash_1);
-
-       // Add a new channel that skips 3
-       let chan_4 = create_announced_chan_between_nodes(&nodes, 1, 3, LocalFeatures::new(), LocalFeatures::new());
-
-       send_payment(&nodes[0], &vec!(&nodes[1], &nodes[3])[..], 1000000);
-       send_payment(&nodes[2], &vec!(&nodes[3])[..], 1000000);
-       send_payment(&nodes[1], &vec!(&nodes[3])[..], 8000000);
-       send_payment(&nodes[1], &vec!(&nodes[3])[..], 8000000);
-       send_payment(&nodes[1], &vec!(&nodes[3])[..], 8000000);
-       send_payment(&nodes[1], &vec!(&nodes[3])[..], 8000000);
-       send_payment(&nodes[1], &vec!(&nodes[3])[..], 8000000);
-
-       // Do some rebalance loop payments, simultaneously
-       let mut hops = Vec::with_capacity(3);
-       hops.push(RouteHop {
-               pubkey: nodes[2].node.get_our_node_id(),
-               short_channel_id: chan_2.0.contents.short_channel_id,
-               fee_msat: 0,
-               cltv_expiry_delta: chan_3.0.contents.cltv_expiry_delta as u32
-       });
-       hops.push(RouteHop {
-               pubkey: nodes[3].node.get_our_node_id(),
-               short_channel_id: chan_3.0.contents.short_channel_id,
-               fee_msat: 0,
-               cltv_expiry_delta: chan_4.1.contents.cltv_expiry_delta as u32
-       });
-       hops.push(RouteHop {
-               pubkey: nodes[1].node.get_our_node_id(),
-               short_channel_id: chan_4.0.contents.short_channel_id,
-               fee_msat: 1000000,
-               cltv_expiry_delta: TEST_FINAL_CLTV,
-       });
-       hops[1].fee_msat = chan_4.1.contents.fee_base_msat as u64 + chan_4.1.contents.fee_proportional_millionths as u64 * hops[2].fee_msat as u64 / 1000000;
-       hops[0].fee_msat = chan_3.0.contents.fee_base_msat as u64 + chan_3.0.contents.fee_proportional_millionths as u64 * hops[1].fee_msat as u64 / 1000000;
-       let payment_preimage_1 = send_along_route(&nodes[1], Route { hops }, &vec!(&nodes[2], &nodes[3], &nodes[1])[..], 1000000).0;
-
-       let mut hops = Vec::with_capacity(3);
-       hops.push(RouteHop {
-               pubkey: nodes[3].node.get_our_node_id(),
-               short_channel_id: chan_4.0.contents.short_channel_id,
-               fee_msat: 0,
-               cltv_expiry_delta: chan_3.1.contents.cltv_expiry_delta as u32
-       });
-       hops.push(RouteHop {
-               pubkey: nodes[2].node.get_our_node_id(),
-               short_channel_id: chan_3.0.contents.short_channel_id,
-               fee_msat: 0,
-               cltv_expiry_delta: chan_2.1.contents.cltv_expiry_delta as u32
-       });
-       hops.push(RouteHop {
-               pubkey: nodes[1].node.get_our_node_id(),
-               short_channel_id: chan_2.0.contents.short_channel_id,
-               fee_msat: 1000000,
-               cltv_expiry_delta: TEST_FINAL_CLTV,
-       });
-       hops[1].fee_msat = chan_2.1.contents.fee_base_msat as u64 + chan_2.1.contents.fee_proportional_millionths as u64 * hops[2].fee_msat as u64 / 1000000;
-       hops[0].fee_msat = chan_3.1.contents.fee_base_msat as u64 + chan_3.1.contents.fee_proportional_millionths as u64 * hops[1].fee_msat as u64 / 1000000;
-       let payment_hash_2 = send_along_route(&nodes[1], Route { hops }, &vec!(&nodes[3], &nodes[2], &nodes[1])[..], 1000000).1;
-
-       // Claim the rebalances...
-       fail_payment(&nodes[1], &vec!(&nodes[3], &nodes[2], &nodes[1])[..], payment_hash_2);
-       claim_payment(&nodes[1], &vec!(&nodes[2], &nodes[3], &nodes[1])[..], payment_preimage_1);
-
-       // Add a duplicate new channel from 2 to 4
-       let chan_5 = create_announced_chan_between_nodes(&nodes, 1, 3, LocalFeatures::new(), LocalFeatures::new());
-
-       // Send some payments across both channels
-       let payment_preimage_3 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[3])[..], 3000000).0;
-       let payment_preimage_4 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[3])[..], 3000000).0;
-       let payment_preimage_5 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[3])[..], 3000000).0;
-
-       route_over_limit(&nodes[0], &vec!(&nodes[1], &nodes[3])[..], 3000000);
-
-       //TODO: Test that routes work again here as we've been notified that the channel is full
-
-       claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[3])[..], payment_preimage_3);
-       claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[3])[..], payment_preimage_4);
-       claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[3])[..], payment_preimage_5);
-
-       // Close down the channels...
-       close_channel(&nodes[0], &nodes[1], &chan_1.2, chan_1.3, true);
-       close_channel(&nodes[1], &nodes[2], &chan_2.2, chan_2.3, false);
-       close_channel(&nodes[2], &nodes[3], &chan_3.2, chan_3.3, true);
-       close_channel(&nodes[1], &nodes[3], &chan_4.2, chan_4.3, false);
-       close_channel(&nodes[1], &nodes[3], &chan_5.2, chan_5.3, false);
-}
-
-#[test]
-fn holding_cell_htlc_counting() {
-       // Tests that HTLCs in the holding cell count towards the pending HTLC limits on outbound HTLCs
-       // to ensure we don't end up with HTLCs sitting around in our holding cell for several
-       // commitment dance rounds.
-       let mut nodes = create_network(3, &[None, None, None]);
-       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-       let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
-
-       let mut payments = Vec::new();
-       for _ in 0..::ln::channel::OUR_MAX_HTLCS {
-               let route = nodes[1].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 100000, TEST_FINAL_CLTV).unwrap();
-               let (payment_preimage, payment_hash) = get_payment_preimage_hash!(nodes[0]);
-               nodes[1].node.send_payment(route, payment_hash).unwrap();
-               payments.push((payment_preimage, payment_hash));
-       }
-       check_added_monitors!(nodes[1], 1);
-
-       let mut events = nodes[1].node.get_and_clear_pending_msg_events();
-       assert_eq!(events.len(), 1);
-       let initial_payment_event = SendEvent::from_event(events.pop().unwrap());
-       assert_eq!(initial_payment_event.node_id, nodes[2].node.get_our_node_id());
-
-       // There is now one HTLC in an outbound commitment transaction and (OUR_MAX_HTLCS - 1) HTLCs in
-       // the holding cell waiting on B's RAA to send. At this point we should not be able to add
-       // another HTLC.
-       let route = nodes[1].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 100000, TEST_FINAL_CLTV).unwrap();
-       let (_, payment_hash_1) = get_payment_preimage_hash!(nodes[0]);
-       if let APIError::ChannelUnavailable { err } = nodes[1].node.send_payment(route, payment_hash_1).unwrap_err() {
-               assert_eq!(err, "Cannot push more than their max accepted HTLCs");
-       } else { panic!("Unexpected event"); }
-
-       // This should also be true if we try to forward a payment.
-       let route = nodes[0].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 100000, TEST_FINAL_CLTV).unwrap();
-       let (_, payment_hash_2) = get_payment_preimage_hash!(nodes[0]);
-       nodes[0].node.send_payment(route, payment_hash_2).unwrap();
-       check_added_monitors!(nodes[0], 1);
-
-       let mut events = nodes[0].node.get_and_clear_pending_msg_events();
-       assert_eq!(events.len(), 1);
-       let payment_event = SendEvent::from_event(events.pop().unwrap());
-       assert_eq!(payment_event.node_id, nodes[1].node.get_our_node_id());
-
-       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
-       commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
-       // We have to forward pending HTLCs twice - once tries to forward the payment forward (and
-       // fails), the second will process the resulting failure and fail the HTLC backward.
-       expect_pending_htlcs_forwardable!(nodes[1]);
-       expect_pending_htlcs_forwardable!(nodes[1]);
-       check_added_monitors!(nodes[1], 1);
-
-       let bs_fail_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-       nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_fail_updates.update_fail_htlcs[0]).unwrap();
-       commitment_signed_dance!(nodes[0], nodes[1], bs_fail_updates.commitment_signed, false, true);
-
-       let events = nodes[0].node.get_and_clear_pending_msg_events();
-       assert_eq!(events.len(), 1);
-       match events[0] {
-               MessageSendEvent::PaymentFailureNetworkUpdate { update: msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg }} => {
-                       assert_eq!(msg.contents.short_channel_id, chan_2.0.contents.short_channel_id);
-               },
-               _ => panic!("Unexpected event"),
-       }
-
-       let events = nodes[0].node.get_and_clear_pending_events();
-       assert_eq!(events.len(), 1);
-       match events[0] {
-               Event::PaymentFailed { payment_hash, rejected_by_dest, .. } => {
-                       assert_eq!(payment_hash, payment_hash_2);
-                       assert!(!rejected_by_dest);
-               },
-               _ => panic!("Unexpected event"),
-       }
-
-       // Now forward all the pending HTLCs and claim them back
-       nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &initial_payment_event.msgs[0]).unwrap();
-       nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &initial_payment_event.commitment_msg).unwrap();
-       check_added_monitors!(nodes[2], 1);
-
-       let (bs_revoke_and_ack, bs_commitment_signed) = get_revoke_commit_msgs!(nodes[2], nodes[1].node.get_our_node_id());
-       nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
-       check_added_monitors!(nodes[1], 1);
-       let as_updates = get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
-
-       nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &bs_commitment_signed).unwrap();
-       check_added_monitors!(nodes[1], 1);
-       let as_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
-
-       for ref update in as_updates.update_add_htlcs.iter() {
-               nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), update).unwrap();
-       }
-       nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &as_updates.commitment_signed).unwrap();
-       check_added_monitors!(nodes[2], 1);
-       nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_raa).unwrap();
-       check_added_monitors!(nodes[2], 1);
-       let (bs_revoke_and_ack, bs_commitment_signed) = get_revoke_commit_msgs!(nodes[2], nodes[1].node.get_our_node_id());
-
-       nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
-       check_added_monitors!(nodes[1], 1);
-       nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &bs_commitment_signed).unwrap();
-       check_added_monitors!(nodes[1], 1);
-       let as_final_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
-
-       nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_final_raa).unwrap();
-       check_added_monitors!(nodes[2], 1);
-
-       expect_pending_htlcs_forwardable!(nodes[2]);
-
-       let events = nodes[2].node.get_and_clear_pending_events();
-       assert_eq!(events.len(), payments.len());
-       for (event, &(_, ref hash)) in events.iter().zip(payments.iter()) {
-               match event {
-                       &Event::PaymentReceived { ref payment_hash, .. } => {
-                               assert_eq!(*payment_hash, *hash);
-                       },
-                       _ => panic!("Unexpected event"),
-               };
-       }
-
-       for (preimage, _) in payments.drain(..) {
-               claim_payment(&nodes[1], &[&nodes[2]], preimage);
-       }
-
-       send_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1000000);
-}
-
-#[test]
-fn duplicate_htlc_test() {
-       // Test that we accept duplicate payment_hash HTLCs across the network and that
-       // claiming/failing them are all separate and don't affect each other
-       let mut nodes = create_network(6, &[None, None, None, None, None, None]);
-
-       // Create some initial channels to route via 3 to 4/5 from 0/1/2
-       create_announced_chan_between_nodes(&nodes, 0, 3, LocalFeatures::new(), LocalFeatures::new());
-       create_announced_chan_between_nodes(&nodes, 1, 3, LocalFeatures::new(), LocalFeatures::new());
-       create_announced_chan_between_nodes(&nodes, 2, 3, LocalFeatures::new(), LocalFeatures::new());
-       create_announced_chan_between_nodes(&nodes, 3, 4, LocalFeatures::new(), LocalFeatures::new());
-       create_announced_chan_between_nodes(&nodes, 3, 5, LocalFeatures::new(), LocalFeatures::new());
-
-       let (payment_preimage, payment_hash) = route_payment(&nodes[0], &vec!(&nodes[3], &nodes[4])[..], 1000000);
-
-       *nodes[0].network_payment_count.borrow_mut() -= 1;
-       assert_eq!(route_payment(&nodes[1], &vec!(&nodes[3])[..], 1000000).0, payment_preimage);
-
-       *nodes[0].network_payment_count.borrow_mut() -= 1;
-       assert_eq!(route_payment(&nodes[2], &vec!(&nodes[3], &nodes[5])[..], 1000000).0, payment_preimage);
-
-       claim_payment(&nodes[0], &vec!(&nodes[3], &nodes[4])[..], payment_preimage);
-       fail_payment(&nodes[2], &vec!(&nodes[3], &nodes[5])[..], payment_hash);
-       claim_payment(&nodes[1], &vec!(&nodes[3])[..], payment_preimage);
-}
-
-fn do_channel_reserve_test(test_recv: bool) {
-       use ln::msgs::HandleError;
-
-       let mut nodes = create_network(3, &[None, None, None]);
-       let chan_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1900, 1001, LocalFeatures::new(), LocalFeatures::new());
-       let chan_2 = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1900, 1001, LocalFeatures::new(), LocalFeatures::new());
-
-       let mut stat01 = get_channel_value_stat!(nodes[0], chan_1.2);
-       let mut stat11 = get_channel_value_stat!(nodes[1], chan_1.2);
-
-       let mut stat12 = get_channel_value_stat!(nodes[1], chan_2.2);
-       let mut stat22 = get_channel_value_stat!(nodes[2], chan_2.2);
-
-       macro_rules! get_route_and_payment_hash {
-               ($recv_value: expr) => {{
-                       let route = nodes[0].router.get_route(&nodes.last().unwrap().node.get_our_node_id(), None, &Vec::new(), $recv_value, TEST_FINAL_CLTV).unwrap();
-                       let (payment_preimage, payment_hash) = get_payment_preimage_hash!(nodes[0]);
-                       (route, payment_hash, payment_preimage)
-               }}
-       };
-
-       macro_rules! expect_forward {
-               ($node: expr) => {{
-                       let mut events = $node.node.get_and_clear_pending_msg_events();
-                       assert_eq!(events.len(), 1);
-                       check_added_monitors!($node, 1);
-                       let payment_event = SendEvent::from_event(events.remove(0));
-                       payment_event
-               }}
-       }
-
-       let feemsat = 239; // somehow we know?
-       let total_fee_msat = (nodes.len() - 2) as u64 * 239;
-
-       let recv_value_0 = stat01.their_max_htlc_value_in_flight_msat - total_fee_msat;
-
-       // attempt to send amt_msat > their_max_htlc_value_in_flight_msat
-       {
-               let (route, our_payment_hash, _) = get_route_and_payment_hash!(recv_value_0 + 1);
-               assert!(route.hops.iter().rev().skip(1).all(|h| h.fee_msat == feemsat));
-               let err = nodes[0].node.send_payment(route, our_payment_hash).err().unwrap();
-               match err {
-                       APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over the max HTLC value in flight our peer will accept"),
-                       _ => panic!("Unknown error variants"),
-               }
-       }
-
-       let mut htlc_id = 0;
-       // channel reserve is bigger than their_max_htlc_value_in_flight_msat so loop to deplete
-       // nodes[0]'s wealth
-       loop {
-               let amt_msat = recv_value_0 + total_fee_msat;
-               if stat01.value_to_self_msat - amt_msat < stat01.channel_reserve_msat {
-                       break;
-               }
-               send_payment(&nodes[0], &vec![&nodes[1], &nodes[2]][..], recv_value_0);
-               htlc_id += 1;
-
-               let (stat01_, stat11_, stat12_, stat22_) = (
-                       get_channel_value_stat!(nodes[0], chan_1.2),
-                       get_channel_value_stat!(nodes[1], chan_1.2),
-                       get_channel_value_stat!(nodes[1], chan_2.2),
-                       get_channel_value_stat!(nodes[2], chan_2.2),
-               );
-
-               assert_eq!(stat01_.value_to_self_msat, stat01.value_to_self_msat - amt_msat);
-               assert_eq!(stat11_.value_to_self_msat, stat11.value_to_self_msat + amt_msat);
-               assert_eq!(stat12_.value_to_self_msat, stat12.value_to_self_msat - (amt_msat - feemsat));
-               assert_eq!(stat22_.value_to_self_msat, stat22.value_to_self_msat + (amt_msat - feemsat));
-               stat01 = stat01_; stat11 = stat11_; stat12 = stat12_; stat22 = stat22_;
-       }
-
-       {
-               let recv_value = stat01.value_to_self_msat - stat01.channel_reserve_msat - total_fee_msat;
-               // attempt to get channel_reserve violation
-               let (route, our_payment_hash, _) = get_route_and_payment_hash!(recv_value + 1);
-               let err = nodes[0].node.send_payment(route.clone(), our_payment_hash).err().unwrap();
-               match err {
-                       APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over their reserve value"),
-                       _ => panic!("Unknown error variants"),
-               }
-       }
-
-       // adding pending output
-       let recv_value_1 = (stat01.value_to_self_msat - stat01.channel_reserve_msat - total_fee_msat)/2;
-       let amt_msat_1 = recv_value_1 + total_fee_msat;
-
-       let (route_1, our_payment_hash_1, our_payment_preimage_1) = get_route_and_payment_hash!(recv_value_1);
-       let payment_event_1 = {
-               nodes[0].node.send_payment(route_1, our_payment_hash_1).unwrap();
-               check_added_monitors!(nodes[0], 1);
-
-               let mut events = nodes[0].node.get_and_clear_pending_msg_events();
-               assert_eq!(events.len(), 1);
-               SendEvent::from_event(events.remove(0))
-       };
-       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event_1.msgs[0]).unwrap();
-
-       // channel reserve test with htlc pending output > 0
-       let recv_value_2 = stat01.value_to_self_msat - amt_msat_1 - stat01.channel_reserve_msat - total_fee_msat;
-       {
-               let (route, our_payment_hash, _) = get_route_and_payment_hash!(recv_value_2 + 1);
-               match nodes[0].node.send_payment(route, our_payment_hash).err().unwrap() {
-                       APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over their reserve value"),
-                       _ => panic!("Unknown error variants"),
-               }
-       }
-
-       {
-               // test channel_reserve test on nodes[1] side
-               let (route, our_payment_hash, _) = get_route_and_payment_hash!(recv_value_2 + 1);
-
-               // Need to manually create update_add_htlc message to go around the channel reserve check in send_htlc()
-               let secp_ctx = Secp256k1::new();
-               let session_priv = SecretKey::from_slice(&{
-                       let mut session_key = [0; 32];
-                       let mut rng = thread_rng();
-                       rng.fill_bytes(&mut session_key);
-                       session_key
-               }).expect("RNG is bad!");
-
-               let cur_height = nodes[0].node.latest_block_height.load(Ordering::Acquire) as u32 + 1;
-               let onion_keys = onion_utils::construct_onion_keys(&secp_ctx, &route, &session_priv).unwrap();
-               let (onion_payloads, htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(&route, cur_height).unwrap();
-               let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, &our_payment_hash);
-               let msg = msgs::UpdateAddHTLC {
-                       channel_id: chan_1.2,
-                       htlc_id,
-                       amount_msat: htlc_msat,
-                       payment_hash: our_payment_hash,
-                       cltv_expiry: htlc_cltv,
-                       onion_routing_packet: onion_packet,
-               };
-
-               if test_recv {
-                       let err = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &msg).err().unwrap();
-                       match err {
-                               HandleError{err, .. } => assert_eq!(err, "Remote HTLC add would put them over their reserve value"),
-                       }
-                       // If we send a garbage message, the channel should get closed, making the rest of this test case fail.
-                       assert_eq!(nodes[1].node.list_channels().len(), 1);
-                       assert_eq!(nodes[1].node.list_channels().len(), 1);
-                       check_closed_broadcast!(nodes[1]);
-                       return;
-               }
-       }
-
-       // split the rest to test holding cell
-       let recv_value_21 = recv_value_2/2;
-       let recv_value_22 = recv_value_2 - recv_value_21 - total_fee_msat;
-       {
-               let stat = get_channel_value_stat!(nodes[0], chan_1.2);
-               assert_eq!(stat.value_to_self_msat - (stat.pending_outbound_htlcs_amount_msat + recv_value_21 + recv_value_22 + total_fee_msat + total_fee_msat), stat.channel_reserve_msat);
-       }
-
-       // now see if they go through on both sides
-       let (route_21, our_payment_hash_21, our_payment_preimage_21) = get_route_and_payment_hash!(recv_value_21);
-       // but this will stuck in the holding cell
-       nodes[0].node.send_payment(route_21, our_payment_hash_21).unwrap();
-       check_added_monitors!(nodes[0], 0);
-       let events = nodes[0].node.get_and_clear_pending_events();
-       assert_eq!(events.len(), 0);
-
-       // test with outbound holding cell amount > 0
-       {
-               let (route, our_payment_hash, _) = get_route_and_payment_hash!(recv_value_22+1);
-               match nodes[0].node.send_payment(route, our_payment_hash).err().unwrap() {
-                       APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over their reserve value"),
-                       _ => panic!("Unknown error variants"),
-               }
-       }
-
-       let (route_22, our_payment_hash_22, our_payment_preimage_22) = get_route_and_payment_hash!(recv_value_22);
-       // this will also stuck in the holding cell
-       nodes[0].node.send_payment(route_22, our_payment_hash_22).unwrap();
-       check_added_monitors!(nodes[0], 0);
-       assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
-       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
-
-       // flush the pending htlc
-       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event_1.commitment_msg).unwrap();
-       let (as_revoke_and_ack, as_commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-       check_added_monitors!(nodes[1], 1);
-
-       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_revoke_and_ack).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       let commitment_update_2 = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
-
-       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &as_commitment_signed).unwrap();
-       let bs_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
-       // No commitment_signed so get_event_msg's assert(len == 1) passes
-       check_added_monitors!(nodes[0], 1);
-
-       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
-       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-       check_added_monitors!(nodes[1], 1);
-
-       expect_pending_htlcs_forwardable!(nodes[1]);
-
-       let ref payment_event_11 = expect_forward!(nodes[1]);
-       nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event_11.msgs[0]).unwrap();
-       commitment_signed_dance!(nodes[2], nodes[1], payment_event_11.commitment_msg, false);
-
-       expect_pending_htlcs_forwardable!(nodes[2]);
-       expect_payment_received!(nodes[2], our_payment_hash_1, recv_value_1);
-
-       // flush the htlcs in the holding cell
-       assert_eq!(commitment_update_2.update_add_htlcs.len(), 2);
-       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &commitment_update_2.update_add_htlcs[0]).unwrap();
-       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &commitment_update_2.update_add_htlcs[1]).unwrap();
-       commitment_signed_dance!(nodes[1], nodes[0], &commitment_update_2.commitment_signed, false);
-       expect_pending_htlcs_forwardable!(nodes[1]);
-
-       let ref payment_event_3 = expect_forward!(nodes[1]);
-       assert_eq!(payment_event_3.msgs.len(), 2);
-       nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event_3.msgs[0]).unwrap();
-       nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event_3.msgs[1]).unwrap();
-
-       commitment_signed_dance!(nodes[2], nodes[1], &payment_event_3.commitment_msg, false);
-       expect_pending_htlcs_forwardable!(nodes[2]);
-
-       let events = nodes[2].node.get_and_clear_pending_events();
-       assert_eq!(events.len(), 2);
-       match events[0] {
-               Event::PaymentReceived { ref payment_hash, amt } => {
-                       assert_eq!(our_payment_hash_21, *payment_hash);
-                       assert_eq!(recv_value_21, amt);
-               },
-               _ => panic!("Unexpected event"),
-       }
-       match events[1] {
-               Event::PaymentReceived { ref payment_hash, amt } => {
-                       assert_eq!(our_payment_hash_22, *payment_hash);
-                       assert_eq!(recv_value_22, amt);
-               },
-               _ => panic!("Unexpected event"),
-       }
-
-       claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), our_payment_preimage_1);
-       claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), our_payment_preimage_21);
-       claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), our_payment_preimage_22);
-
-       let expected_value_to_self = stat01.value_to_self_msat - (recv_value_1 + total_fee_msat) - (recv_value_21 + total_fee_msat) - (recv_value_22 + total_fee_msat);
-       let stat0 = get_channel_value_stat!(nodes[0], chan_1.2);
-       assert_eq!(stat0.value_to_self_msat, expected_value_to_self);
-       assert_eq!(stat0.value_to_self_msat, stat0.channel_reserve_msat);
-
-       let stat2 = get_channel_value_stat!(nodes[2], chan_2.2);
-       assert_eq!(stat2.value_to_self_msat, stat22.value_to_self_msat + recv_value_1 + recv_value_21 + recv_value_22);
-}
-
-#[test]
-fn channel_reserve_test() {
-       do_channel_reserve_test(false);
-       do_channel_reserve_test(true);
-}
-
-#[test]
-fn channel_reserve_in_flight_removes() {
-       // In cases where one side claims an HTLC, it thinks it has additional available funds that it
-       // can send to its counterparty, but due to update ordering, the other side may not yet have
-       // considered those HTLCs fully removed.
-       // This tests that we don't count HTLCs which will not be included in the next remote
-       // commitment transaction towards the reserve value (as it implies no commitment transaction
-       // will be generated which violates the remote reserve value).
-       // This was broken previously, and discovered by the chanmon_fail_consistency fuzz test.
-       // To test this we:
-       //  * route two HTLCs from A to B (note that, at a high level, this test is checking that, when
-       //    you consider the values of both of these HTLCs, B may not send an HTLC back to A, but if
-       //    you only consider the value of the first HTLC, it may not),
-       //  * start routing a third HTLC from A to B,
-       //  * claim the first two HTLCs (though B will generate an update_fulfill for one, and put
-       //    the other claim in its holding cell, as it immediately goes into AwaitingRAA),
-       //  * deliver the first fulfill from B
-       //  * deliver the update_add and an RAA from A, resulting in B freeing the second holding cell
-       //    claim,
-       //  * deliver A's response CS and RAA.
-       //    This results in A having the second HTLC in AwaitingRemovedRemoteRevoke, but B having
-       //    removed it fully. B now has the push_msat plus the first two HTLCs in value.
-       //  * Now B happily sends another HTLC, potentially violating its reserve value from A's point
-       //    of view (if A counts the AwaitingRemovedRemoteRevoke HTLC).
-       let mut nodes = create_network(2, &[None, None]);
-       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       let b_chan_values = get_channel_value_stat!(nodes[1], chan_1.2);
-       // Route the first two HTLCs.
-       let (payment_preimage_1, _) = route_payment(&nodes[0], &[&nodes[1]], b_chan_values.channel_reserve_msat - b_chan_values.value_to_self_msat - 10000);
-       let (payment_preimage_2, _) = route_payment(&nodes[0], &[&nodes[1]], 20000);
-
-       // Start routing the third HTLC (this is just used to get everyone in the right state).
-       let (payment_preimage_3, payment_hash_3) = get_payment_preimage_hash!(nodes[0]);
-       let send_1 = {
-               let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap();
-               nodes[0].node.send_payment(route, payment_hash_3).unwrap();
-               check_added_monitors!(nodes[0], 1);
-               let mut events = nodes[0].node.get_and_clear_pending_msg_events();
-               assert_eq!(events.len(), 1);
-               SendEvent::from_event(events.remove(0))
-       };
-
-       // Now claim both of the first two HTLCs on B's end, putting B in AwaitingRAA and generating an
-       // initial fulfill/CS.
-       assert!(nodes[1].node.claim_funds(payment_preimage_1));
-       check_added_monitors!(nodes[1], 1);
-       let bs_removes = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-
-       // This claim goes in B's holding cell, allowing us to have a pending B->A RAA which does not
-       // remove the second HTLC when we send the HTLC back from B to A.
-       assert!(nodes[1].node.claim_funds(payment_preimage_2));
-       check_added_monitors!(nodes[1], 1);
-       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-
-       nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_removes.update_fulfill_htlcs[0]).unwrap();
-       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_removes.commitment_signed).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
-       expect_payment_sent!(nodes[0], payment_preimage_1);
-
-       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &send_1.msgs[0]).unwrap();
-       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &send_1.commitment_msg).unwrap();
-       check_added_monitors!(nodes[1], 1);
-       // B is already AwaitingRAA, so cant generate a CS here
-       let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
-
-       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa).unwrap();
-       check_added_monitors!(nodes[1], 1);
-       let bs_cs = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-
-       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       let as_cs = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
-
-       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_cs.commitment_signed).unwrap();
-       check_added_monitors!(nodes[1], 1);
-       let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
-
-       // The second HTLCis removed, but as A is in AwaitingRAA it can't generate a CS here, so the
-       // RAA that B generated above doesn't fully resolve the second HTLC from A's point of view.
-       // However, the RAA A generates here *does* fully resolve the HTLC from B's point of view (as A
-       // can no longer broadcast a commitment transaction with it and B has the preimage so can go
-       // on-chain as necessary).
-       nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_cs.update_fulfill_htlcs[0]).unwrap();
-       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_cs.commitment_signed).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
-       expect_payment_sent!(nodes[0], payment_preimage_2);
-
-       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa).unwrap();
-       check_added_monitors!(nodes[1], 1);
-       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-
-       expect_pending_htlcs_forwardable!(nodes[1]);
-       expect_payment_received!(nodes[1], payment_hash_3, 100000);
-
-       // Note that as this RAA was generated before the delivery of the update_fulfill it shouldn't
-       // resolve the second HTLC from A's point of view.
-       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       let as_cs = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
-
-       // Now that B doesn't have the second RAA anymore, but A still does, send a payment from B back
-       // to A to ensure that A doesn't count the almost-removed HTLC in update_add processing.
-       let (payment_preimage_4, payment_hash_4) = get_payment_preimage_hash!(nodes[1]);
-       let send_2 = {
-               let route = nodes[1].router.get_route(&nodes[0].node.get_our_node_id(), None, &[], 10000, TEST_FINAL_CLTV).unwrap();
-               nodes[1].node.send_payment(route, payment_hash_4).unwrap();
-               check_added_monitors!(nodes[1], 1);
-               let mut events = nodes[1].node.get_and_clear_pending_msg_events();
-               assert_eq!(events.len(), 1);
-               SendEvent::from_event(events.remove(0))
-       };
-
-       nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &send_2.msgs[0]).unwrap();
-       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &send_2.commitment_msg).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
-
-       // Now just resolve all the outstanding messages/HTLCs for completeness...
-
-       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_cs.commitment_signed).unwrap();
-       check_added_monitors!(nodes[1], 1);
-       let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
-
-       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa).unwrap();
-       check_added_monitors!(nodes[1], 1);
-
-       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       let as_cs = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
-
-       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_cs.commitment_signed).unwrap();
-       check_added_monitors!(nodes[1], 1);
-       let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
-
-       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa).unwrap();
-       check_added_monitors!(nodes[0], 1);
-
-       expect_pending_htlcs_forwardable!(nodes[0]);
-       expect_payment_received!(nodes[0], payment_hash_4, 10000);
-
-       claim_payment(&nodes[1], &[&nodes[0]], payment_preimage_4);
-       claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_3);
-}
-
-#[test]
-fn channel_monitor_network_test() {
-       // Simple test which builds a network of ChannelManagers, connects them to each other, and
-       // tests that ChannelMonitor is able to recover from various states.
-       let nodes = create_network(5, &[None, None, None, None, None]);
-
-       // Create some initial channels
-       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-       let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
-       let chan_3 = create_announced_chan_between_nodes(&nodes, 2, 3, LocalFeatures::new(), LocalFeatures::new());
-       let chan_4 = create_announced_chan_between_nodes(&nodes, 3, 4, LocalFeatures::new(), LocalFeatures::new());
-
-       // Rebalance the network a bit by relaying one payment through all the channels...
-       send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3], &nodes[4])[..], 8000000);
-       send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3], &nodes[4])[..], 8000000);
-       send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3], &nodes[4])[..], 8000000);
-       send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3], &nodes[4])[..], 8000000);
-
-       // Simple case with no pending HTLCs:
-       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), true);
-       {
-               let mut node_txn = test_txn_broadcast(&nodes[1], &chan_1, None, HTLCType::NONE);
-               let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-               nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn.drain(..).next().unwrap()] }, 1);
-               test_txn_broadcast(&nodes[0], &chan_1, None, HTLCType::NONE);
-       }
-       get_announce_close_broadcast_events(&nodes, 0, 1);
-       assert_eq!(nodes[0].node.list_channels().len(), 0);
-       assert_eq!(nodes[1].node.list_channels().len(), 1);
-
-       // One pending HTLC is discarded by the force-close:
-       let payment_preimage_1 = route_payment(&nodes[1], &vec!(&nodes[2], &nodes[3])[..], 3000000).0;
-
-       // Simple case of one pending HTLC to HTLC-Timeout
-       nodes[1].node.peer_disconnected(&nodes[2].node.get_our_node_id(), true);
-       {
-               let mut node_txn = test_txn_broadcast(&nodes[1], &chan_2, None, HTLCType::TIMEOUT);
-               let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-               nodes[2].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn.drain(..).next().unwrap()] }, 1);
-               test_txn_broadcast(&nodes[2], &chan_2, None, HTLCType::NONE);
-       }
-       get_announce_close_broadcast_events(&nodes, 1, 2);
-       assert_eq!(nodes[1].node.list_channels().len(), 0);
-       assert_eq!(nodes[2].node.list_channels().len(), 1);
-
-       macro_rules! claim_funds {
-               ($node: expr, $prev_node: expr, $preimage: expr) => {
-                       {
-                               assert!($node.node.claim_funds($preimage));
-                               check_added_monitors!($node, 1);
-
-                               let events = $node.node.get_and_clear_pending_msg_events();
-                               assert_eq!(events.len(), 1);
-                               match events[0] {
-                                       MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, .. } } => {
-                                               assert!(update_add_htlcs.is_empty());
-                                               assert!(update_fail_htlcs.is_empty());
-                                               assert_eq!(*node_id, $prev_node.node.get_our_node_id());
-                                       },
-                                       _ => panic!("Unexpected event"),
-                               };
-                       }
-               }
-       }
-
-       // nodes[3] gets the preimage, but nodes[2] already disconnected, resulting in a nodes[2]
-       // HTLC-Timeout and a nodes[3] claim against it (+ its own announces)
-       nodes[2].node.peer_disconnected(&nodes[3].node.get_our_node_id(), true);
-       {
-               let node_txn = test_txn_broadcast(&nodes[2], &chan_3, None, HTLCType::TIMEOUT);
-
-               // Claim the payment on nodes[3], giving it knowledge of the preimage
-               claim_funds!(nodes[3], nodes[2], payment_preimage_1);
-
-               let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-               nodes[3].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn[0].clone()] }, 1);
-
-               check_preimage_claim(&nodes[3], &node_txn);
-       }
-       get_announce_close_broadcast_events(&nodes, 2, 3);
-       assert_eq!(nodes[2].node.list_channels().len(), 0);
-       assert_eq!(nodes[3].node.list_channels().len(), 1);
-
-       { // Cheat and reset nodes[4]'s height to 1
-               let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-               nodes[4].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![] }, 1);
-       }
-
-       assert_eq!(nodes[3].node.latest_block_height.load(Ordering::Acquire), 1);
-       assert_eq!(nodes[4].node.latest_block_height.load(Ordering::Acquire), 1);
-       // One pending HTLC to time out:
-       let payment_preimage_2 = route_payment(&nodes[3], &vec!(&nodes[4])[..], 3000000).0;
-       // CLTV expires at TEST_FINAL_CLTV + 1 (current height) + 1 (added in send_payment for
-       // buffer space).
-
-       {
-               let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-               nodes[3].chain_monitor.block_connected_checked(&header, 2, &Vec::new()[..], &[0; 0]);
-               for i in 3..TEST_FINAL_CLTV + 2 + LATENCY_GRACE_PERIOD_BLOCKS + 1 {
-                       header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-                       nodes[3].chain_monitor.block_connected_checked(&header, i, &Vec::new()[..], &[0; 0]);
-               }
-
-               let node_txn = test_txn_broadcast(&nodes[3], &chan_4, None, HTLCType::TIMEOUT);
-
-               // Claim the payment on nodes[4], giving it knowledge of the preimage
-               claim_funds!(nodes[4], nodes[3], payment_preimage_2);
-
-               header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-               nodes[4].chain_monitor.block_connected_checked(&header, 2, &Vec::new()[..], &[0; 0]);
-               for i in 3..TEST_FINAL_CLTV + 2 - CLTV_CLAIM_BUFFER + 1 {
-                       header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-                       nodes[4].chain_monitor.block_connected_checked(&header, i, &Vec::new()[..], &[0; 0]);
-               }
-
-               test_txn_broadcast(&nodes[4], &chan_4, None, HTLCType::SUCCESS);
-
-               header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-               nodes[4].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn[0].clone()] }, TEST_FINAL_CLTV - 5);
-
-               check_preimage_claim(&nodes[4], &node_txn);
-       }
-       get_announce_close_broadcast_events(&nodes, 3, 4);
-       assert_eq!(nodes[3].node.list_channels().len(), 0);
-       assert_eq!(nodes[4].node.list_channels().len(), 0);
-}
-
-#[test]
-fn test_justice_tx() {
-       // Test justice txn built on revoked HTLC-Success tx, against both sides
-
-       let mut alice_config = UserConfig::new();
-       alice_config.channel_options.announced_channel = true;
-       alice_config.peer_channel_config_limits.force_announced_channel_preference = false;
-       alice_config.own_channel_config.our_to_self_delay = 6 * 24 * 5;
-       let mut bob_config = UserConfig::new();
-       bob_config.channel_options.announced_channel = true;
-       bob_config.peer_channel_config_limits.force_announced_channel_preference = false;
-       bob_config.own_channel_config.our_to_self_delay = 6 * 24 * 3;
-       let nodes = create_network(2, &[Some(alice_config), Some(bob_config)]);
-       // Create some new channels:
-       let chan_5 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       // A pending HTLC which will be revoked:
-       let payment_preimage_3 = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
-       // Get the will-be-revoked local txn from nodes[0]
-       let revoked_local_txn = nodes[0].node.channel_state.lock().unwrap().by_id.iter().next().unwrap().1.last_local_commitment_txn.clone();
-       assert_eq!(revoked_local_txn.len(), 2); // First commitment tx, then HTLC tx
-       assert_eq!(revoked_local_txn[0].input.len(), 1);
-       assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_5.3.txid());
-       assert_eq!(revoked_local_txn[0].output.len(), 2); // Only HTLC and output back to 0 are present
-       assert_eq!(revoked_local_txn[1].input.len(), 1);
-       assert_eq!(revoked_local_txn[1].input[0].previous_output.txid, revoked_local_txn[0].txid());
-       assert_eq!(revoked_local_txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); // HTLC-Timeout
-       // Revoke the old state
-       claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_3);
-
-       {
-               let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-               nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
-               {
-                       let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
-                       assert_eq!(node_txn.len(), 3);
-                       assert_eq!(node_txn.pop().unwrap(), node_txn[0]); // An outpoint registration will result in a 2nd block_connected
-                       assert_eq!(node_txn[0].input.len(), 2); // We should claim the revoked output and the HTLC output
-
-                       check_spends!(node_txn[0], revoked_local_txn[0].clone());
-                       node_txn.swap_remove(0);
-               }
-               test_txn_broadcast(&nodes[1], &chan_5, None, HTLCType::NONE);
-
-               nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
-               let node_txn = test_txn_broadcast(&nodes[0], &chan_5, Some(revoked_local_txn[0].clone()), HTLCType::TIMEOUT);
-               header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-               nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn[1].clone()] }, 1);
-               test_revoked_htlc_claim_txn_broadcast(&nodes[1], node_txn[1].clone());
-       }
-       get_announce_close_broadcast_events(&nodes, 0, 1);
-
-       assert_eq!(nodes[0].node.list_channels().len(), 0);
-       assert_eq!(nodes[1].node.list_channels().len(), 0);
-
-       // We test justice_tx build by A on B's revoked HTLC-Success tx
-       // Create some new channels:
-       let chan_6 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       // A pending HTLC which will be revoked:
-       let payment_preimage_4 = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
-       // Get the will-be-revoked local txn from B
-       let revoked_local_txn = nodes[1].node.channel_state.lock().unwrap().by_id.iter().next().unwrap().1.last_local_commitment_txn.clone();
-       assert_eq!(revoked_local_txn.len(), 1); // Only commitment tx
-       assert_eq!(revoked_local_txn[0].input.len(), 1);
-       assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_6.3.txid());
-       assert_eq!(revoked_local_txn[0].output.len(), 2); // Only HTLC and output back to A are present
-       // Revoke the old state
-       claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_4);
-       {
-               let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-               nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
-               {
-                       let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
-                       assert_eq!(node_txn.len(), 3);
-                       assert_eq!(node_txn.pop().unwrap(), node_txn[0]); // An outpoint registration will result in a 2nd block_connected
-                       assert_eq!(node_txn[0].input.len(), 1); // We claim the received HTLC output
-
-                       check_spends!(node_txn[0], revoked_local_txn[0].clone());
-                       node_txn.swap_remove(0);
-               }
-               test_txn_broadcast(&nodes[0], &chan_6, None, HTLCType::NONE);
-
-               nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
-               let node_txn = test_txn_broadcast(&nodes[1], &chan_6, Some(revoked_local_txn[0].clone()), HTLCType::SUCCESS);
-               header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-               nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn[1].clone()] }, 1);
-               test_revoked_htlc_claim_txn_broadcast(&nodes[0], node_txn[1].clone());
-       }
-       get_announce_close_broadcast_events(&nodes, 0, 1);
-       assert_eq!(nodes[0].node.list_channels().len(), 0);
-       assert_eq!(nodes[1].node.list_channels().len(), 0);
-}
-
-#[test]
-fn revoked_output_claim() {
-       // Simple test to ensure a node will claim a revoked output when a stale remote commitment
-       // transaction is broadcast by its counterparty
-       let nodes = create_network(2, &[None, None]);
-       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-       // node[0] is gonna to revoke an old state thus node[1] should be able to claim the revoked output
-       let revoked_local_txn = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
-       assert_eq!(revoked_local_txn.len(), 1);
-       // Only output is the full channel value back to nodes[0]:
-       assert_eq!(revoked_local_txn[0].output.len(), 1);
-       // Send a payment through, updating everyone's latest commitment txn
-       send_payment(&nodes[0], &vec!(&nodes[1])[..], 5000000);
-
-       // Inform nodes[1] that nodes[0] broadcast a stale tx
-       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
-       let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
-       assert_eq!(node_txn.len(), 3); // nodes[1] will broadcast justice tx twice, and its own local state once
-
-       assert_eq!(node_txn[0], node_txn[2]);
-
-       check_spends!(node_txn[0], revoked_local_txn[0].clone());
-       check_spends!(node_txn[1], chan_1.3.clone());
-
-       // Inform nodes[0] that a watchtower cheated on its behalf, so it will force-close the chan
-       nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
-       get_announce_close_broadcast_events(&nodes, 0, 1);
-}
-
-#[test]
-fn claim_htlc_outputs_shared_tx() {
-       // Node revoked old state, htlcs haven't time out yet, claim them in shared justice tx
-       let nodes = create_network(2, &[None, None]);
-
-       // Create some new channel:
-       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       // Rebalance the network to generate htlc in the two directions
-       send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
-       // node[0] is gonna to revoke an old state thus node[1] should be able to claim both offered/received HTLC outputs on top of commitment tx
-       let payment_preimage_1 = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
-       let (_payment_preimage_2, payment_hash_2) = route_payment(&nodes[1], &vec!(&nodes[0])[..], 3000000);
-
-       // Get the will-be-revoked local txn from node[0]
-       let revoked_local_txn = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
-       assert_eq!(revoked_local_txn.len(), 2); // commitment tx + 1 HTLC-Timeout tx
-       assert_eq!(revoked_local_txn[0].input.len(), 1);
-       assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_1.3.txid());
-       assert_eq!(revoked_local_txn[1].input.len(), 1);
-       assert_eq!(revoked_local_txn[1].input[0].previous_output.txid, revoked_local_txn[0].txid());
-       assert_eq!(revoked_local_txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); // HTLC-Timeout
-       check_spends!(revoked_local_txn[1], revoked_local_txn[0].clone());
-
-       //Revoke the old state
-       claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_1);
-
-       {
-               let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-               nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
-               nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
-               connect_blocks(&nodes[1].chain_monitor, ANTI_REORG_DELAY - 1, 1, true, header.bitcoin_hash());
-
-               let events = nodes[1].node.get_and_clear_pending_events();
-               assert_eq!(events.len(), 1);
-               match events[0] {
-                       Event::PaymentFailed { payment_hash, .. } => {
-                               assert_eq!(payment_hash, payment_hash_2);
-                       },
-                       _ => panic!("Unexpected event"),
-               }
-
-               let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
-               assert_eq!(node_txn.len(), 4);
-
-               assert_eq!(node_txn[0].input.len(), 3); // Claim the revoked output + both revoked HTLC outputs
-               check_spends!(node_txn[0], revoked_local_txn[0].clone());
-
-               assert_eq!(node_txn[0], node_txn[3]); // justice tx is duplicated due to block re-scanning
-
-               let mut witness_lens = BTreeSet::new();
-               witness_lens.insert(node_txn[0].input[0].witness.last().unwrap().len());
-               witness_lens.insert(node_txn[0].input[1].witness.last().unwrap().len());
-               witness_lens.insert(node_txn[0].input[2].witness.last().unwrap().len());
-               assert_eq!(witness_lens.len(), 3);
-               assert_eq!(*witness_lens.iter().skip(0).next().unwrap(), 77); // revoked to_local
-               assert_eq!(*witness_lens.iter().skip(1).next().unwrap(), OFFERED_HTLC_SCRIPT_WEIGHT); // revoked offered HTLC
-               assert_eq!(*witness_lens.iter().skip(2).next().unwrap(), ACCEPTED_HTLC_SCRIPT_WEIGHT); // revoked received HTLC
-
-               // Next nodes[1] broadcasts its current local tx state:
-               assert_eq!(node_txn[1].input.len(), 1);
-               assert_eq!(node_txn[1].input[0].previous_output.txid, chan_1.3.txid()); //Spending funding tx unique txouput, tx broadcasted by ChannelManager
-
-               assert_eq!(node_txn[2].input.len(), 1);
-               let witness_script = node_txn[2].clone().input[0].witness.pop().unwrap();
-               assert_eq!(witness_script.len(), OFFERED_HTLC_SCRIPT_WEIGHT); //Spending an offered htlc output
-               assert_eq!(node_txn[2].input[0].previous_output.txid, node_txn[1].txid());
-               assert_ne!(node_txn[2].input[0].previous_output.txid, node_txn[0].input[0].previous_output.txid);
-               assert_ne!(node_txn[2].input[0].previous_output.txid, node_txn[0].input[1].previous_output.txid);
-       }
-       get_announce_close_broadcast_events(&nodes, 0, 1);
-       assert_eq!(nodes[0].node.list_channels().len(), 0);
-       assert_eq!(nodes[1].node.list_channels().len(), 0);
-}
-
-#[test]
-fn claim_htlc_outputs_single_tx() {
-       // Node revoked old state, htlcs have timed out, claim each of them in separated justice tx
-       let nodes = create_network(2, &[None, None]);
-
-       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       // Rebalance the network to generate htlc in the two directions
-       send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
-       // node[0] is gonna to revoke an old state thus node[1] should be able to claim both offered/received HTLC outputs on top of commitment tx, but this
-       // time as two different claim transactions as we're gonna to timeout htlc with given a high current height
-       let payment_preimage_1 = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
-       let (_payment_preimage_2, payment_hash_2) = route_payment(&nodes[1], &vec!(&nodes[0])[..], 3000000);
-
-       // Get the will-be-revoked local txn from node[0]
-       let revoked_local_txn = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
-
-       //Revoke the old state
-       claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_1);
-
-       {
-               let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-               nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 200);
-               nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 200);
-               connect_blocks(&nodes[1].chain_monitor, ANTI_REORG_DELAY - 1, 200, true, header.bitcoin_hash());
-
-               let events = nodes[1].node.get_and_clear_pending_events();
-               assert_eq!(events.len(), 1);
-               match events[0] {
-                       Event::PaymentFailed { payment_hash, .. } => {
-                               assert_eq!(payment_hash, payment_hash_2);
-                       },
-                       _ => panic!("Unexpected event"),
-               }
-
-               let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
-               assert_eq!(node_txn.len(), 22); // ChannelManager : 2, ChannelMontitor: 8 (1 standard revoked output, 2 revocation htlc tx, 1 local commitment tx + 1 htlc timeout tx) * 2 (block-rescan) + 5 * (1 local commitment tx + 1 htlc timeout tx)
-
-               assert_eq!(node_txn[0], node_txn[7]);
-               assert_eq!(node_txn[1], node_txn[8]);
-               assert_eq!(node_txn[2], node_txn[9]);
-               assert_eq!(node_txn[3], node_txn[10]);
-               assert_eq!(node_txn[4], node_txn[11]);
-               assert_eq!(node_txn[3], node_txn[5]); //local commitment tx + htlc timeout tx broadcasted by ChannelManger
-               assert_eq!(node_txn[4], node_txn[6]);
-
-               for i in 12..22 {
-                       if i % 2 == 0 { assert_eq!(node_txn[3], node_txn[i]); } else { assert_eq!(node_txn[4], node_txn[i]); }
-               }
-
-               assert_eq!(node_txn[0].input.len(), 1);
-               assert_eq!(node_txn[1].input.len(), 1);
-               assert_eq!(node_txn[2].input.len(), 1);
-
-               fn get_txout(out_point: &BitcoinOutPoint, tx: &Transaction) -> Option<TxOut> {
-                       if out_point.txid == tx.txid() {
-                               tx.output.get(out_point.vout as usize).cloned()
-                       } else {
-                               None
-                       }
-               }
-               node_txn[0].verify(|out|get_txout(out, &revoked_local_txn[0])).unwrap();
-               node_txn[1].verify(|out|get_txout(out, &revoked_local_txn[0])).unwrap();
-               node_txn[2].verify(|out|get_txout(out, &revoked_local_txn[0])).unwrap();
-
-               let mut witness_lens = BTreeSet::new();
-               witness_lens.insert(node_txn[0].input[0].witness.last().unwrap().len());
-               witness_lens.insert(node_txn[1].input[0].witness.last().unwrap().len());
-               witness_lens.insert(node_txn[2].input[0].witness.last().unwrap().len());
-               assert_eq!(witness_lens.len(), 3);
-               assert_eq!(*witness_lens.iter().skip(0).next().unwrap(), 77); // revoked to_local
-               assert_eq!(*witness_lens.iter().skip(1).next().unwrap(), OFFERED_HTLC_SCRIPT_WEIGHT); // revoked offered HTLC
-               assert_eq!(*witness_lens.iter().skip(2).next().unwrap(), ACCEPTED_HTLC_SCRIPT_WEIGHT); // revoked received HTLC
-
-               assert_eq!(node_txn[3].input.len(), 1);
-               check_spends!(node_txn[3], chan_1.3.clone());
-
-               assert_eq!(node_txn[4].input.len(), 1);
-               let witness_script = node_txn[4].input[0].witness.last().unwrap();
-               assert_eq!(witness_script.len(), OFFERED_HTLC_SCRIPT_WEIGHT); //Spending an offered htlc output
-               assert_eq!(node_txn[4].input[0].previous_output.txid, node_txn[3].txid());
-               assert_ne!(node_txn[4].input[0].previous_output.txid, node_txn[0].input[0].previous_output.txid);
-               assert_ne!(node_txn[4].input[0].previous_output.txid, node_txn[1].input[0].previous_output.txid);
-       }
-       get_announce_close_broadcast_events(&nodes, 0, 1);
-       assert_eq!(nodes[0].node.list_channels().len(), 0);
-       assert_eq!(nodes[1].node.list_channels().len(), 0);
-}
-
-#[test]
-fn test_htlc_on_chain_success() {
-       // Test that in case of a unilateral close onchain, we detect the state of output thanks to
-       // ChainWatchInterface and pass the preimage backward accordingly. So here we test that ChannelManager is
-       // broadcasting the right event to other nodes in payment path.
-       // We test with two HTLCs simultaneously as that was not handled correctly in the past.
-       // A --------------------> B ----------------------> C (preimage)
-       // First, C should claim the HTLC outputs via HTLC-Success when its own latest local
-       // commitment transaction was broadcast.
-       // Then, B should learn the preimage from said transactions, attempting to claim backwards
-       // towards B.
-       // B should be able to claim via preimage if A then broadcasts its local tx.
-       // Finally, when A sees B's latest local commitment transaction it should be able to claim
-       // the HTLC outputs via the preimage it learned (which, once confirmed should generate a
-       // PaymentSent event).
-
-       let nodes = create_network(3, &[None, None, None]);
-
-       // Create some initial channels
-       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-       let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
-
-       // Rebalance the network a bit by relaying one payment through all the channels...
-       send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
-       send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
-
-       let (our_payment_preimage, _payment_hash) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), 3000000);
-       let (our_payment_preimage_2, _payment_hash_2) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), 3000000);
-       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42};
-
-       // Broadcast legit commitment tx from C on B's chain
-       // Broadcast HTLC Success transaction by C on received output from C's commitment tx on B's chain
-       let commitment_tx = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone();
-       assert_eq!(commitment_tx.len(), 1);
-       check_spends!(commitment_tx[0], chan_2.3.clone());
-       nodes[2].node.claim_funds(our_payment_preimage);
-       nodes[2].node.claim_funds(our_payment_preimage_2);
-       check_added_monitors!(nodes[2], 2);
-       let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
-       assert!(updates.update_add_htlcs.is_empty());
-       assert!(updates.update_fail_htlcs.is_empty());
-       assert!(updates.update_fail_malformed_htlcs.is_empty());
-       assert_eq!(updates.update_fulfill_htlcs.len(), 1);
-
-       nodes[2].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 1);
-       check_closed_broadcast!(nodes[2]);
-       let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 1 (commitment tx), ChannelMonitor : 4 (2*2 * HTLC-Success tx)
-       assert_eq!(node_txn.len(), 5);
-       assert_eq!(node_txn[0], node_txn[3]);
-       assert_eq!(node_txn[1], node_txn[4]);
-       assert_eq!(node_txn[2], commitment_tx[0]);
-       check_spends!(node_txn[0], commitment_tx[0].clone());
-       check_spends!(node_txn[1], commitment_tx[0].clone());
-       assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
-       assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
-       assert!(node_txn[0].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
-       assert!(node_txn[1].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
-       assert_eq!(node_txn[0].lock_time, 0);
-       assert_eq!(node_txn[1].lock_time, 0);
-
-       // Verify that B's ChannelManager is able to extract preimage from HTLC Success tx and pass it backward
-       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: node_txn}, 1);
-       let events = nodes[1].node.get_and_clear_pending_msg_events();
-       {
-               let mut added_monitors = nodes[1].chan_monitor.added_monitors.lock().unwrap();
-               assert_eq!(added_monitors.len(), 2);
-               assert_eq!(added_monitors[0].0.txid, chan_1.3.txid());
-               assert_eq!(added_monitors[1].0.txid, chan_1.3.txid());
-               added_monitors.clear();
-       }
-       assert_eq!(events.len(), 2);
-       match events[0] {
-               MessageSendEvent::BroadcastChannelUpdate { .. } => {},
-               _ => panic!("Unexpected event"),
-       }
-       match events[1] {
-               MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, ref update_fulfill_htlcs, ref update_fail_malformed_htlcs, .. } } => {
-                       assert!(update_add_htlcs.is_empty());
-                       assert!(update_fail_htlcs.is_empty());
-                       assert_eq!(update_fulfill_htlcs.len(), 1);
-                       assert!(update_fail_malformed_htlcs.is_empty());
-                       assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
-               },
-               _ => panic!("Unexpected event"),
-       };
-       macro_rules! check_tx_local_broadcast {
-               ($node: expr, $htlc_offered: expr, $commitment_tx: expr, $chan_tx: expr) => { {
-                       // ChannelManager : 3 (commitment tx, 2*HTLC-Timeout tx), ChannelMonitor : 2 (timeout tx) * 2 (block-rescan)
-                       let mut node_txn = $node.tx_broadcaster.txn_broadcasted.lock().unwrap();
-                       assert_eq!(node_txn.len(), 7);
-                       assert_eq!(node_txn[0], node_txn[5]);
-                       assert_eq!(node_txn[1], node_txn[6]);
-                       check_spends!(node_txn[0], $commitment_tx.clone());
-                       check_spends!(node_txn[1], $commitment_tx.clone());
-                       assert_ne!(node_txn[0].lock_time, 0);
-                       assert_ne!(node_txn[1].lock_time, 0);
-                       if $htlc_offered {
-                               assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
-                               assert_eq!(node_txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
-                               assert!(node_txn[0].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
-                               assert!(node_txn[1].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
-                       } else {
-                               assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
-                               assert_eq!(node_txn[1].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
-                               assert!(node_txn[0].output[0].script_pubkey.is_v0_p2wpkh()); // direct payment
-                               assert!(node_txn[1].output[0].script_pubkey.is_v0_p2wpkh()); // direct payment
-                       }
-                       check_spends!(node_txn[2], $chan_tx.clone());
-                       check_spends!(node_txn[3], node_txn[2].clone());
-                       check_spends!(node_txn[4], node_txn[2].clone());
-                       assert_eq!(node_txn[2].input[0].witness.last().unwrap().len(), 71);
-                       assert_eq!(node_txn[3].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
-                       assert_eq!(node_txn[4].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
-                       assert!(node_txn[3].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
-                       assert!(node_txn[4].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
-                       assert_ne!(node_txn[3].lock_time, 0);
-                       assert_ne!(node_txn[4].lock_time, 0);
-                       node_txn.clear();
-               } }
-       }
-       // nodes[1] now broadcasts its own local state as a fallback, suggesting an alternate
-       // commitment transaction with a corresponding HTLC-Timeout transactions, as well as a
-       // timeout-claim of the output that nodes[2] just claimed via success.
-       check_tx_local_broadcast!(nodes[1], false, commitment_tx[0], chan_2.3);
-
-       // Broadcast legit commitment tx from A on B's chain
-       // Broadcast preimage tx by B on offered output from A commitment tx  on A's chain
-       let commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
-       check_spends!(commitment_tx[0], chan_1.3.clone());
-       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 1);
-       check_closed_broadcast!(nodes[1]);
-       let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 1 (commitment tx), ChannelMonitor : 1 (HTLC-Success) * 2 (block-rescan)
-       assert_eq!(node_txn.len(), 3);
-       assert_eq!(node_txn[0], node_txn[2]);
-       check_spends!(node_txn[0], commitment_tx[0].clone());
-       assert_eq!(node_txn[0].input.len(), 2);
-       assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
-       assert_eq!(node_txn[0].input[1].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
-       assert_eq!(node_txn[0].lock_time, 0);
-       assert!(node_txn[0].output[0].script_pubkey.is_v0_p2wpkh()); // direct payment
-       check_spends!(node_txn[1], chan_1.3.clone());
-       assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), 71);
-       // We don't bother to check that B can claim the HTLC output on its commitment tx here as
-       // we already checked the same situation with A.
-
-       // Verify that A's ChannelManager is able to extract preimage from preimage tx and generate PaymentSent
-       nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone(), node_txn[0].clone()] }, 1);
-       check_closed_broadcast!(nodes[0]);
-       let events = nodes[0].node.get_and_clear_pending_events();
-       assert_eq!(events.len(), 2);
-       let mut first_claimed = false;
-       for event in events {
-               match event {
-                       Event::PaymentSent { payment_preimage } => {
-                               if payment_preimage == our_payment_preimage {
-                                       assert!(!first_claimed);
-                                       first_claimed = true;
-                               } else {
-                                       assert_eq!(payment_preimage, our_payment_preimage_2);
-                               }
-                       },
-                       _ => panic!("Unexpected event"),
-               }
-       }
-       check_tx_local_broadcast!(nodes[0], true, commitment_tx[0], chan_1.3);
-}
-
-#[test]
-fn test_htlc_on_chain_timeout() {
-       // Test that in case of a unilateral close onchain, we detect the state of output thanks to
-       // ChainWatchInterface and timeout the HTLC backward accordingly. So here we test that ChannelManager is
-       // broadcasting the right event to other nodes in payment path.
-       // A ------------------> B ----------------------> C (timeout)
-       //    B's commitment tx                 C's commitment tx
-       //            \                                  \
-       //         B's HTLC timeout tx               B's timeout tx
-
-       let nodes = create_network(3, &[None, None, None]);
-
-       // Create some intial channels
-       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-       let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
-
-       // Rebalance the network a bit by relaying one payment thorugh all the channels...
-       send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
-       send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
-
-       let (_payment_preimage, payment_hash) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), 3000000);
-       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42};
-
-       // Broadcast legit commitment tx from C on B's chain
-       let commitment_tx = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone();
-       check_spends!(commitment_tx[0], chan_2.3.clone());
-       nodes[2].node.fail_htlc_backwards(&payment_hash);
-       check_added_monitors!(nodes[2], 0);
-       expect_pending_htlcs_forwardable!(nodes[2]);
-       check_added_monitors!(nodes[2], 1);
-
-       let events = nodes[2].node.get_and_clear_pending_msg_events();
-       assert_eq!(events.len(), 1);
-       match events[0] {
-               MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, .. } } => {
-                       assert!(update_add_htlcs.is_empty());
-                       assert!(!update_fail_htlcs.is_empty());
-                       assert!(update_fulfill_htlcs.is_empty());
-                       assert!(update_fail_malformed_htlcs.is_empty());
-                       assert_eq!(nodes[1].node.get_our_node_id(), *node_id);
-               },
-               _ => panic!("Unexpected event"),
-       };
-       nodes[2].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 1);
-       check_closed_broadcast!(nodes[2]);
-       let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 1 (commitment tx)
-       assert_eq!(node_txn.len(), 1);
-       check_spends!(node_txn[0], chan_2.3.clone());
-       assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), 71);
-
-       // Broadcast timeout transaction by B on received output from C's commitment tx on B's chain
-       // Verify that B's ChannelManager is able to detect that HTLC is timeout by its own tx and react backward in consequence
-       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 200);
-       let timeout_tx;
-       {
-               let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
-               assert_eq!(node_txn.len(), 8); // ChannelManager : 2 (commitment tx, HTLC-Timeout tx), ChannelMonitor : 6 (HTLC-Timeout tx, commitment tx, timeout tx) * 2 (block-rescan)
-               assert_eq!(node_txn[0], node_txn[5]);
-               assert_eq!(node_txn[1], node_txn[6]);
-               assert_eq!(node_txn[2], node_txn[7]);
-               check_spends!(node_txn[0], commitment_tx[0].clone());
-               assert_eq!(node_txn[0].clone().input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
-               check_spends!(node_txn[1], chan_2.3.clone());
-               check_spends!(node_txn[2], node_txn[1].clone());
-               assert_eq!(node_txn[1].clone().input[0].witness.last().unwrap().len(), 71);
-               assert_eq!(node_txn[2].clone().input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
-               check_spends!(node_txn[3], chan_2.3.clone());
-               check_spends!(node_txn[4], node_txn[3].clone());
-               assert_eq!(node_txn[3].input[0].witness.clone().last().unwrap().len(), 71);
-               assert_eq!(node_txn[4].input[0].witness.clone().last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
-               timeout_tx = node_txn[0].clone();
-               node_txn.clear();
-       }
-
-       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![timeout_tx]}, 1);
-       connect_blocks(&nodes[1].chain_monitor, ANTI_REORG_DELAY - 1, 1, true, header.bitcoin_hash());
-       check_added_monitors!(nodes[1], 0);
-       check_closed_broadcast!(nodes[1]);
-
-       expect_pending_htlcs_forwardable!(nodes[1]);
-       check_added_monitors!(nodes[1], 1);
-       let events = nodes[1].node.get_and_clear_pending_msg_events();
-       assert_eq!(events.len(), 1);
-       match events[0] {
-               MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, ref update_fulfill_htlcs, ref update_fail_malformed_htlcs, .. } } => {
-                       assert!(update_add_htlcs.is_empty());
-                       assert!(!update_fail_htlcs.is_empty());
-                       assert!(update_fulfill_htlcs.is_empty());
-                       assert!(update_fail_malformed_htlcs.is_empty());
-                       assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
-               },
-               _ => panic!("Unexpected event"),
-       };
-       let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // Well... here we detect our own htlc_timeout_tx so no tx to be generated
-       assert_eq!(node_txn.len(), 0);
-
-       // Broadcast legit commitment tx from B on A's chain
-       let commitment_tx = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
-       check_spends!(commitment_tx[0], chan_1.3.clone());
-
-       nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 200);
-       check_closed_broadcast!(nodes[0]);
-       let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 2 (commitment tx, HTLC-Timeout tx), ChannelMonitor : 2 (timeout tx) * 2 block-rescan
-       assert_eq!(node_txn.len(), 4);
-       assert_eq!(node_txn[0], node_txn[3]);
-       check_spends!(node_txn[0], commitment_tx[0].clone());
-       assert_eq!(node_txn[0].clone().input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
-       check_spends!(node_txn[1], chan_1.3.clone());
-       check_spends!(node_txn[2], node_txn[1].clone());
-       assert_eq!(node_txn[1].clone().input[0].witness.last().unwrap().len(), 71);
-       assert_eq!(node_txn[2].clone().input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
-}
-
-#[test]
-fn test_simple_commitment_revoked_fail_backward() {
-       // Test that in case of a revoked commitment tx, we detect the resolution of output by justice tx
-       // and fail backward accordingly.
-
-       let nodes = create_network(3, &[None, None, None]);
-
-       // Create some initial channels
-       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-       let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
-
-       let (payment_preimage, _payment_hash) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3000000);
-       // Get the will-be-revoked local txn from nodes[2]
-       let revoked_local_txn = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone();
-       // Revoke the old state
-       claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage);
-
-       route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3000000);
-
-       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42};
-       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
-       connect_blocks(&nodes[1].chain_monitor, ANTI_REORG_DELAY - 1, 1, true, header.bitcoin_hash());
-       check_added_monitors!(nodes[1], 0);
-       check_closed_broadcast!(nodes[1]);
-
-       expect_pending_htlcs_forwardable!(nodes[1]);
-       check_added_monitors!(nodes[1], 1);
-       let events = nodes[1].node.get_and_clear_pending_msg_events();
-       assert_eq!(events.len(), 1);
-       match events[0] {
-               MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, ref update_fulfill_htlcs, ref update_fail_malformed_htlcs, ref commitment_signed, .. } } => {
-                       assert!(update_add_htlcs.is_empty());
-                       assert_eq!(update_fail_htlcs.len(), 1);
-                       assert!(update_fulfill_htlcs.is_empty());
-                       assert!(update_fail_malformed_htlcs.is_empty());
-                       assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
-
-                       nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[0]).unwrap();
-                       commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, false, true);
-
-                       let events = nodes[0].node.get_and_clear_pending_msg_events();
-                       assert_eq!(events.len(), 1);
-                       match events[0] {
-                               MessageSendEvent::PaymentFailureNetworkUpdate { .. } => {},
-                               _ => panic!("Unexpected event"),
-                       }
-                       let events = nodes[0].node.get_and_clear_pending_events();
-                       assert_eq!(events.len(), 1);
-                       match events[0] {
-                               Event::PaymentFailed { .. } => {},
-                               _ => panic!("Unexpected event"),
-                       }
-               },
-               _ => panic!("Unexpected event"),
-       }
-}
-
-fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool, use_dust: bool, no_to_remote: bool) {
-       // Test that if our counterparty broadcasts a revoked commitment transaction we fail all
-       // pending HTLCs on that channel backwards even if the HTLCs aren't present in our latest
-       // commitment transaction anymore.
-       // To do this, we have the peer which will broadcast a revoked commitment transaction send
-       // a number of update_fail/commitment_signed updates without ever sending the RAA in
-       // response to our commitment_signed. This is somewhat misbehavior-y, though not
-       // technically disallowed and we should probably handle it reasonably.
-       // Note that this is pretty exhaustive as an outbound HTLC which we haven't yet
-       // failed/fulfilled backwards must be in at least one of the latest two remote commitment
-       // transactions:
-       // * Once we move it out of our holding cell/add it, we will immediately include it in a
-       //   commitment_signed (implying it will be in the latest remote commitment transaction).
-       // * Once they remove it, we will send a (the first) commitment_signed without the HTLC,
-       //   and once they revoke the previous commitment transaction (allowing us to send a new
-       //   commitment_signed) we will be free to fail/fulfill the HTLC backwards.
-       let mut nodes = create_network(3, &[None, None, None]);
-
-       // Create some initial channels
-       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-       let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
-
-       let (payment_preimage, _payment_hash) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], if no_to_remote { 10_000 } else { 3_000_000 });
-       // Get the will-be-revoked local txn from nodes[2]
-       let revoked_local_txn = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone();
-       assert_eq!(revoked_local_txn[0].output.len(), if no_to_remote { 1 } else { 2 });
-       // Revoke the old state
-       claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage);
-
-       let value = if use_dust {
-               // The dust limit applied to HTLC outputs considers the fee of the HTLC transaction as
-               // well, so HTLCs at exactly the dust limit will not be included in commitment txn.
-               nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().our_dust_limit_satoshis * 1000
-       } else { 3000000 };
-
-       let (_, first_payment_hash) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], value);
-       let (_, second_payment_hash) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], value);
-       let (_, third_payment_hash) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], value);
-
-       assert!(nodes[2].node.fail_htlc_backwards(&first_payment_hash));
-       expect_pending_htlcs_forwardable!(nodes[2]);
-       check_added_monitors!(nodes[2], 1);
-       let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
-       assert!(updates.update_add_htlcs.is_empty());
-       assert!(updates.update_fulfill_htlcs.is_empty());
-       assert!(updates.update_fail_malformed_htlcs.is_empty());
-       assert_eq!(updates.update_fail_htlcs.len(), 1);
-       assert!(updates.update_fee.is_none());
-       nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]).unwrap();
-       let bs_raa = commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false, true, false, true);
-       // Drop the last RAA from 3 -> 2
-
-       assert!(nodes[2].node.fail_htlc_backwards(&second_payment_hash));
-       expect_pending_htlcs_forwardable!(nodes[2]);
-       check_added_monitors!(nodes[2], 1);
-       let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
-       assert!(updates.update_add_htlcs.is_empty());
-       assert!(updates.update_fulfill_htlcs.is_empty());
-       assert!(updates.update_fail_malformed_htlcs.is_empty());
-       assert_eq!(updates.update_fail_htlcs.len(), 1);
-       assert!(updates.update_fee.is_none());
-       nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]).unwrap();
-       nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &updates.commitment_signed).unwrap();
-       check_added_monitors!(nodes[1], 1);
-       // Note that nodes[1] is in AwaitingRAA, so won't send a CS
-       let as_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
-       nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_raa).unwrap();
-       check_added_monitors!(nodes[2], 1);
-
-       assert!(nodes[2].node.fail_htlc_backwards(&third_payment_hash));
-       expect_pending_htlcs_forwardable!(nodes[2]);
-       check_added_monitors!(nodes[2], 1);
-       let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
-       assert!(updates.update_add_htlcs.is_empty());
-       assert!(updates.update_fulfill_htlcs.is_empty());
-       assert!(updates.update_fail_malformed_htlcs.is_empty());
-       assert_eq!(updates.update_fail_htlcs.len(), 1);
-       assert!(updates.update_fee.is_none());
-       nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]).unwrap();
-       // At this point first_payment_hash has dropped out of the latest two commitment
-       // transactions that nodes[1] is tracking...
-       nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &updates.commitment_signed).unwrap();
-       check_added_monitors!(nodes[1], 1);
-       // Note that nodes[1] is (still) in AwaitingRAA, so won't send a CS
-       let as_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
-       nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_raa).unwrap();
-       check_added_monitors!(nodes[2], 1);
-
-       // Add a fourth HTLC, this one will get sequestered away in nodes[1]'s holding cell waiting
-       // on nodes[2]'s RAA.
-       let route = nodes[1].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
-       let (_, fourth_payment_hash) = get_payment_preimage_hash!(nodes[0]);
-       nodes[1].node.send_payment(route, fourth_payment_hash).unwrap();
-       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-       assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
-       check_added_monitors!(nodes[1], 0);
-
-       if deliver_bs_raa {
-               nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_raa).unwrap();
-               // One monitor for the new revocation preimage, no second on as we won't generate a new
-               // commitment transaction for nodes[0] until process_pending_htlc_forwards().
-               check_added_monitors!(nodes[1], 1);
-               let events = nodes[1].node.get_and_clear_pending_events();
-               assert_eq!(events.len(), 1);
-               match events[0] {
-                       Event::PendingHTLCsForwardable { .. } => { },
-                       _ => panic!("Unexpected event"),
-               };
-               // Deliberately don't process the pending fail-back so they all fail back at once after
-               // block connection just like the !deliver_bs_raa case
-       }
-
-       let mut failed_htlcs = HashSet::new();
-       assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
-
-       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42};
-       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
-       connect_blocks(&nodes[1].chain_monitor, ANTI_REORG_DELAY - 1, 1, true, header.bitcoin_hash());
-
-       let events = nodes[1].node.get_and_clear_pending_events();
-       assert_eq!(events.len(), if deliver_bs_raa { 1 } else { 2 });
-       match events[0] {
-               Event::PaymentFailed { ref payment_hash, .. } => {
-                       assert_eq!(*payment_hash, fourth_payment_hash);
-               },
-               _ => panic!("Unexpected event"),
-       }
-       if !deliver_bs_raa {
-               match events[1] {
-                       Event::PendingHTLCsForwardable { .. } => { },
-                       _ => panic!("Unexpected event"),
-               };
-       }
-       nodes[1].node.process_pending_htlc_forwards();
-       check_added_monitors!(nodes[1], 1);
-
-       let events = nodes[1].node.get_and_clear_pending_msg_events();
-       assert_eq!(events.len(), if deliver_bs_raa { 3 } else { 2 });
-       match events[if deliver_bs_raa { 1 } else { 0 }] {
-               MessageSendEvent::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
-               _ => panic!("Unexpected event"),
-       }
-       if deliver_bs_raa {
-               match events[0] {
-                       MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, ref update_fulfill_htlcs, ref update_fail_malformed_htlcs, .. } } => {
-                               assert_eq!(nodes[2].node.get_our_node_id(), *node_id);
-                               assert_eq!(update_add_htlcs.len(), 1);
-                               assert!(update_fulfill_htlcs.is_empty());
-                               assert!(update_fail_htlcs.is_empty());
-                               assert!(update_fail_malformed_htlcs.is_empty());
-                       },
-                       _ => panic!("Unexpected event"),
-               }
-       }
-       match events[if deliver_bs_raa { 2 } else { 1 }] {
-               MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, ref update_fulfill_htlcs, ref update_fail_malformed_htlcs, ref commitment_signed, .. } } => {
-                       assert!(update_add_htlcs.is_empty());
-                       assert_eq!(update_fail_htlcs.len(), 3);
-                       assert!(update_fulfill_htlcs.is_empty());
-                       assert!(update_fail_malformed_htlcs.is_empty());
-                       assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
-
-                       nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[0]).unwrap();
-                       nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[1]).unwrap();
-                       nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[2]).unwrap();
-
-                       commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, false, true);
-
-                       let events = nodes[0].node.get_and_clear_pending_msg_events();
-                       // If we delivered B's RAA we got an unknown preimage error, not something
-                       // that we should update our routing table for.
-                       assert_eq!(events.len(), if deliver_bs_raa { 2 } else { 3 });
-                       for event in events {
-                               match event {
-                                       MessageSendEvent::PaymentFailureNetworkUpdate { .. } => {},
-                                       _ => panic!("Unexpected event"),
-                               }
-                       }
-                       let events = nodes[0].node.get_and_clear_pending_events();
-                       assert_eq!(events.len(), 3);
-                       match events[0] {
-                               Event::PaymentFailed { ref payment_hash, .. } => {
-                                       assert!(failed_htlcs.insert(payment_hash.0));
-                               },
-                               _ => panic!("Unexpected event"),
-                       }
-                       match events[1] {
-                               Event::PaymentFailed { ref payment_hash, .. } => {
-                                       assert!(failed_htlcs.insert(payment_hash.0));
-                               },
-                               _ => panic!("Unexpected event"),
-                       }
-                       match events[2] {
-                               Event::PaymentFailed { ref payment_hash, .. } => {
-                                       assert!(failed_htlcs.insert(payment_hash.0));
-                               },
-                               _ => panic!("Unexpected event"),
-                       }
-               },
-               _ => panic!("Unexpected event"),
-       }
-
-       assert!(failed_htlcs.contains(&first_payment_hash.0));
-       assert!(failed_htlcs.contains(&second_payment_hash.0));
-       assert!(failed_htlcs.contains(&third_payment_hash.0));
-}
-
-#[test]
-fn test_commitment_revoked_fail_backward_exhaustive_a() {
-       do_test_commitment_revoked_fail_backward_exhaustive(false, true, false);
-       do_test_commitment_revoked_fail_backward_exhaustive(true, true, false);
-       do_test_commitment_revoked_fail_backward_exhaustive(false, false, false);
-       do_test_commitment_revoked_fail_backward_exhaustive(true, false, false);
-}
-
-#[test]
-fn test_commitment_revoked_fail_backward_exhaustive_b() {
-       do_test_commitment_revoked_fail_backward_exhaustive(false, true, true);
-       do_test_commitment_revoked_fail_backward_exhaustive(true, true, true);
-       do_test_commitment_revoked_fail_backward_exhaustive(false, false, true);
-       do_test_commitment_revoked_fail_backward_exhaustive(true, false, true);
-}
-
-#[test]
-fn test_htlc_ignore_latest_remote_commitment() {
-       // Test that HTLC transactions spending the latest remote commitment transaction are simply
-       // ignored if we cannot claim them. This originally tickled an invalid unwrap().
-       let nodes = create_network(2, &[None, None]);
-       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       route_payment(&nodes[0], &[&nodes[1]], 10000000);
-       nodes[0].node.force_close_channel(&nodes[0].node.list_channels()[0].channel_id);
-       check_closed_broadcast!(nodes[0]);
-
-       let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
-       assert_eq!(node_txn.len(), 2);
-
-       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-       nodes[1].chain_monitor.block_connected_checked(&header, 1, &[&node_txn[0], &node_txn[1]], &[1; 2]);
-       check_closed_broadcast!(nodes[1]);
-
-       // Duplicate the block_connected call since this may happen due to other listeners
-       // registering new transactions
-       nodes[1].chain_monitor.block_connected_checked(&header, 1, &[&node_txn[0], &node_txn[1]], &[1; 2]);
-}
-
-#[test]
-fn test_force_close_fail_back() {
-       // Check which HTLCs are failed-backwards on channel force-closure
-       let mut nodes = create_network(3, &[None, None, None]);
-       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-       create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
-
-       let route = nodes[0].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 1000000, 42).unwrap();
-
-       let (our_payment_preimage, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
-
-       let mut payment_event = {
-               nodes[0].node.send_payment(route, our_payment_hash).unwrap();
-               check_added_monitors!(nodes[0], 1);
-
-               let mut events = nodes[0].node.get_and_clear_pending_msg_events();
-               assert_eq!(events.len(), 1);
-               SendEvent::from_event(events.remove(0))
-       };
-
-       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
-       commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
-
-       expect_pending_htlcs_forwardable!(nodes[1]);
-
-       let mut events_2 = nodes[1].node.get_and_clear_pending_msg_events();
-       assert_eq!(events_2.len(), 1);
-       payment_event = SendEvent::from_event(events_2.remove(0));
-       assert_eq!(payment_event.msgs.len(), 1);
-
-       check_added_monitors!(nodes[1], 1);
-       nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
-       nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &payment_event.commitment_msg).unwrap();
-       check_added_monitors!(nodes[2], 1);
-       let (_, _) = get_revoke_commit_msgs!(nodes[2], nodes[1].node.get_our_node_id());
-
-       // nodes[2] now has the latest commitment transaction, but hasn't revoked its previous
-       // state or updated nodes[1]' state. Now force-close and broadcast that commitment/HTLC
-       // transaction and ensure nodes[1] doesn't fail-backwards (this was originally a bug!).
-
-       nodes[2].node.force_close_channel(&payment_event.commitment_msg.channel_id);
-       check_closed_broadcast!(nodes[2]);
-       let tx = {
-               let mut node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap();
-               // Note that we don't bother broadcasting the HTLC-Success transaction here as we don't
-               // have a use for it unless nodes[2] learns the preimage somehow, the funds will go
-               // back to nodes[1] upon timeout otherwise.
-               assert_eq!(node_txn.len(), 1);
-               node_txn.remove(0)
-       };
-
-       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-       nodes[1].chain_monitor.block_connected_checked(&header, 1, &[&tx], &[1]);
-
-       // Note no UpdateHTLCs event here from nodes[1] to nodes[0]!
-       check_closed_broadcast!(nodes[1]);
-
-       // Now check that if we add the preimage to ChannelMonitor it broadcasts our HTLC-Success..
-       {
-               let mut monitors = nodes[2].chan_monitor.simple_monitor.monitors.lock().unwrap();
-               monitors.get_mut(&OutPoint::new(Sha256dHash::from_slice(&payment_event.commitment_msg.channel_id[..]).unwrap(), 0)).unwrap()
-                       .provide_payment_preimage(&our_payment_hash, &our_payment_preimage);
-       }
-       nodes[2].chain_monitor.block_connected_checked(&header, 1, &[&tx], &[1]);
-       let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap();
-       assert_eq!(node_txn.len(), 1);
-       assert_eq!(node_txn[0].input.len(), 1);
-       assert_eq!(node_txn[0].input[0].previous_output.txid, tx.txid());
-       assert_eq!(node_txn[0].lock_time, 0); // Must be an HTLC-Success
-       assert_eq!(node_txn[0].input[0].witness.len(), 5); // Must be an HTLC-Success
-
-       check_spends!(node_txn[0], tx);
-}
-
-#[test]
-fn test_unconf_chan() {
-       // After creating a chan between nodes, we disconnect all blocks previously seen to force a channel close on nodes[0] side
-       let nodes = create_network(2, &[None, None]);
-       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       let channel_state = nodes[0].node.channel_state.lock().unwrap();
-       assert_eq!(channel_state.by_id.len(), 1);
-       assert_eq!(channel_state.short_to_id.len(), 1);
-       mem::drop(channel_state);
-
-       let mut headers = Vec::new();
-       let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-       headers.push(header.clone());
-       for _i in 2..100 {
-               header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-               headers.push(header.clone());
-       }
-       let mut height = 99;
-       while !headers.is_empty() {
-               nodes[0].node.block_disconnected(&headers.pop().unwrap(), height);
-               height -= 1;
-       }
-       check_closed_broadcast!(nodes[0]);
-       let channel_state = nodes[0].node.channel_state.lock().unwrap();
-       assert_eq!(channel_state.by_id.len(), 0);
-       assert_eq!(channel_state.short_to_id.len(), 0);
-}
-
-#[test]
-fn test_simple_peer_disconnect() {
-       // Test that we can reconnect when there are no lost messages
-       let nodes = create_network(3, &[None, None, None]);
-       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-       create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
-
-       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
-       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
-       reconnect_nodes(&nodes[0], &nodes[1], (true, true), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
-
-       let payment_preimage_1 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).0;
-       let payment_hash_2 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).1;
-       fail_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), payment_hash_2);
-       claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), payment_preimage_1);
-
-       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
-       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
-       reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
-
-       let payment_preimage_3 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).0;
-       let payment_preimage_4 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).0;
-       let payment_hash_5 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).1;
-       let payment_hash_6 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).1;
-
-       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
-       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
-
-       claim_payment_along_route(&nodes[0], &vec!(&nodes[1], &nodes[2]), true, payment_preimage_3);
-       fail_payment_along_route(&nodes[0], &[&nodes[1], &nodes[2]], true, payment_hash_5);
-
-       reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (1, 0), (1, 0), (false, false));
-       {
-               let events = nodes[0].node.get_and_clear_pending_events();
-               assert_eq!(events.len(), 2);
-               match events[0] {
-                       Event::PaymentSent { payment_preimage } => {
-                               assert_eq!(payment_preimage, payment_preimage_3);
-                       },
-                       _ => panic!("Unexpected event"),
-               }
-               match events[1] {
-                       Event::PaymentFailed { payment_hash, rejected_by_dest, .. } => {
-                               assert_eq!(payment_hash, payment_hash_5);
-                               assert!(rejected_by_dest);
-                       },
-                       _ => panic!("Unexpected event"),
-               }
-       }
-
-       claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), payment_preimage_4);
-       fail_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), payment_hash_6);
-}
-
-fn do_test_drop_messages_peer_disconnect(messages_delivered: u8) {
-       // Test that we can reconnect when in-flight HTLC updates get dropped
-       let mut nodes = create_network(2, &[None, None]);
-       if messages_delivered == 0 {
-               create_chan_between_nodes_with_value_a(&nodes[0], &nodes[1], 100000, 10001, LocalFeatures::new(), LocalFeatures::new());
-               // nodes[1] doesn't receive the funding_locked message (it'll be re-sent on reconnect)
-       } else {
-               create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-       }
-
-       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), Some(&nodes[0].node.list_usable_channels()), &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
-       let (payment_preimage_1, payment_hash_1) = get_payment_preimage_hash!(nodes[0]);
-
-       let payment_event = {
-               nodes[0].node.send_payment(route.clone(), payment_hash_1).unwrap();
-               check_added_monitors!(nodes[0], 1);
-
-               let mut events = nodes[0].node.get_and_clear_pending_msg_events();
-               assert_eq!(events.len(), 1);
-               SendEvent::from_event(events.remove(0))
-       };
-       assert_eq!(nodes[1].node.get_our_node_id(), payment_event.node_id);
-
-       if messages_delivered < 2 {
-               // Drop the payment_event messages, and let them get re-generated in reconnect_nodes!
-       } else {
-               nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
-               if messages_delivered >= 3 {
-                       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg).unwrap();
-                       check_added_monitors!(nodes[1], 1);
-                       let (bs_revoke_and_ack, bs_commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-
-                       if messages_delivered >= 4 {
-                               nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
-                               assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
-                               check_added_monitors!(nodes[0], 1);
-
-                               if messages_delivered >= 5 {
-                                       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_commitment_signed).unwrap();
-                                       let as_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
-                                       // No commitment_signed so get_event_msg's assert(len == 1) passes
-                                       check_added_monitors!(nodes[0], 1);
-
-                                       if messages_delivered >= 6 {
-                                               nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_and_ack).unwrap();
-                                               assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-                                               check_added_monitors!(nodes[1], 1);
-                                       }
-                               }
-                       }
-               }
-       }
-
-       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
-       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
-       if messages_delivered < 3 {
-               // Even if the funding_locked messages get exchanged, as long as nothing further was
-               // received on either side, both sides will need to resend them.
-               reconnect_nodes(&nodes[0], &nodes[1], (true, true), (0, 1), (0, 0), (0, 0), (0, 0), (false, false));
-       } else if messages_delivered == 3 {
-               // nodes[0] still wants its RAA + commitment_signed
-               reconnect_nodes(&nodes[0], &nodes[1], (false, false), (-1, 0), (0, 0), (0, 0), (0, 0), (true, false));
-       } else if messages_delivered == 4 {
-               // nodes[0] still wants its commitment_signed
-               reconnect_nodes(&nodes[0], &nodes[1], (false, false), (-1, 0), (0, 0), (0, 0), (0, 0), (false, false));
-       } else if messages_delivered == 5 {
-               // nodes[1] still wants its final RAA
-               reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, true));
-       } else if messages_delivered == 6 {
-               // Everything was delivered...
-               reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
-       }
-
-       let events_1 = nodes[1].node.get_and_clear_pending_events();
-       assert_eq!(events_1.len(), 1);
-       match events_1[0] {
-               Event::PendingHTLCsForwardable { .. } => { },
-               _ => panic!("Unexpected event"),
-       };
-
-       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
-       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
-       reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
-
-       nodes[1].node.process_pending_htlc_forwards();
-
-       let events_2 = nodes[1].node.get_and_clear_pending_events();
-       assert_eq!(events_2.len(), 1);
-       match events_2[0] {
-               Event::PaymentReceived { ref payment_hash, amt } => {
-                       assert_eq!(payment_hash_1, *payment_hash);
-                       assert_eq!(amt, 1000000);
-               },
-               _ => panic!("Unexpected event"),
-       }
-
-       nodes[1].node.claim_funds(payment_preimage_1);
-       check_added_monitors!(nodes[1], 1);
-
-       let events_3 = nodes[1].node.get_and_clear_pending_msg_events();
-       assert_eq!(events_3.len(), 1);
-       let (update_fulfill_htlc, commitment_signed) = match events_3[0] {
-               MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
-                       assert_eq!(*node_id, nodes[0].node.get_our_node_id());
-                       assert!(updates.update_add_htlcs.is_empty());
-                       assert!(updates.update_fail_htlcs.is_empty());
-                       assert_eq!(updates.update_fulfill_htlcs.len(), 1);
-                       assert!(updates.update_fail_malformed_htlcs.is_empty());
-                       assert!(updates.update_fee.is_none());
-                       (updates.update_fulfill_htlcs[0].clone(), updates.commitment_signed.clone())
-               },
-               _ => panic!("Unexpected event"),
-       };
-
-       if messages_delivered >= 1 {
-               nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_htlc).unwrap();
-
-               let events_4 = nodes[0].node.get_and_clear_pending_events();
-               assert_eq!(events_4.len(), 1);
-               match events_4[0] {
-                       Event::PaymentSent { ref payment_preimage } => {
-                               assert_eq!(payment_preimage_1, *payment_preimage);
-                       },
-                       _ => panic!("Unexpected event"),
-               }
-
-               if messages_delivered >= 2 {
-                       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed).unwrap();
-                       check_added_monitors!(nodes[0], 1);
-                       let (as_revoke_and_ack, as_commitment_signed) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
-
-                       if messages_delivered >= 3 {
-                               nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_and_ack).unwrap();
-                               assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-                               check_added_monitors!(nodes[1], 1);
-
-                               if messages_delivered >= 4 {
-                                       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_commitment_signed).unwrap();
-                                       let bs_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
-                                       // No commitment_signed so get_event_msg's assert(len == 1) passes
-                                       check_added_monitors!(nodes[1], 1);
-
-                                       if messages_delivered >= 5 {
-                                               nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
-                                               assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
-                                               check_added_monitors!(nodes[0], 1);
-                                       }
-                               }
-                       }
-               }
-       }
-
-       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
-       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
-       if messages_delivered < 2 {
-               reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (1, 0), (0, 0), (0, 0), (false, false));
-               //TODO: Deduplicate PaymentSent events, then enable this if:
-               //if messages_delivered < 1 {
-                       let events_4 = nodes[0].node.get_and_clear_pending_events();
-                       assert_eq!(events_4.len(), 1);
-                       match events_4[0] {
-                               Event::PaymentSent { ref payment_preimage } => {
-                                       assert_eq!(payment_preimage_1, *payment_preimage);
-                               },
-                               _ => panic!("Unexpected event"),
-                       }
-               //}
-       } else if messages_delivered == 2 {
-               // nodes[0] still wants its RAA + commitment_signed
-               reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, -1), (0, 0), (0, 0), (0, 0), (false, true));
-       } else if messages_delivered == 3 {
-               // nodes[0] still wants its commitment_signed
-               reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, -1), (0, 0), (0, 0), (0, 0), (false, false));
-       } else if messages_delivered == 4 {
-               // nodes[1] still wants its final RAA
-               reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (true, false));
-       } else if messages_delivered == 5 {
-               // Everything was delivered...
-               reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
-       }
-
-       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
-       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
-       reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
-
-       // Channel should still work fine...
-       let payment_preimage_2 = send_along_route(&nodes[0], route, &[&nodes[1]], 1000000).0;
-       claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
-}
-
-#[test]
-fn test_drop_messages_peer_disconnect_a() {
-       do_test_drop_messages_peer_disconnect(0);
-       do_test_drop_messages_peer_disconnect(1);
-       do_test_drop_messages_peer_disconnect(2);
-       do_test_drop_messages_peer_disconnect(3);
-}
-
-#[test]
-fn test_drop_messages_peer_disconnect_b() {
-       do_test_drop_messages_peer_disconnect(4);
-       do_test_drop_messages_peer_disconnect(5);
-       do_test_drop_messages_peer_disconnect(6);
-}
-
-#[test]
-fn test_funding_peer_disconnect() {
-       // Test that we can lock in our funding tx while disconnected
-       let nodes = create_network(2, &[None, None]);
-       let tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 100000, 10001, LocalFeatures::new(), LocalFeatures::new());
-
-       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
-       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
-
-       confirm_transaction(&nodes[0].chain_monitor, &tx, tx.version);
-       let events_1 = nodes[0].node.get_and_clear_pending_msg_events();
-       assert_eq!(events_1.len(), 1);
-       match events_1[0] {
-               MessageSendEvent::SendFundingLocked { ref node_id, msg: _ } => {
-                       assert_eq!(*node_id, nodes[1].node.get_our_node_id());
-               },
-               _ => panic!("Unexpected event"),
-       }
-
-       reconnect_nodes(&nodes[0], &nodes[1], (false, true), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
-
-       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
-       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
-
-       confirm_transaction(&nodes[1].chain_monitor, &tx, tx.version);
-       let events_2 = nodes[1].node.get_and_clear_pending_msg_events();
-       assert_eq!(events_2.len(), 2);
-       match events_2[0] {
-               MessageSendEvent::SendFundingLocked { ref node_id, msg: _ } => {
-                       assert_eq!(*node_id, nodes[0].node.get_our_node_id());
-               },
-               _ => panic!("Unexpected event"),
-       }
-       match events_2[1] {
-               MessageSendEvent::SendAnnouncementSignatures { ref node_id, msg: _ } => {
-                       assert_eq!(*node_id, nodes[0].node.get_our_node_id());
-               },
-               _ => panic!("Unexpected event"),
-       }
-
-       reconnect_nodes(&nodes[0], &nodes[1], (true, true), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
-
-       // TODO: We shouldn't need to manually pass list_usable_chanels here once we support
-       // rebroadcasting announcement_signatures upon reconnect.
-
-       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), Some(&nodes[0].node.list_usable_channels()), &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
-       let (payment_preimage, _) = send_along_route(&nodes[0], route, &[&nodes[1]], 1000000);
-       claim_payment(&nodes[0], &[&nodes[1]], payment_preimage);
-}
-
-#[test]
-fn test_drop_messages_peer_disconnect_dual_htlc() {
-       // Test that we can handle reconnecting when both sides of a channel have pending
-       // commitment_updates when we disconnect.
-       let mut nodes = create_network(2, &[None, None]);
-       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       let (payment_preimage_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
-
-       // Now try to send a second payment which will fail to send
-       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
-       let (payment_preimage_2, payment_hash_2) = get_payment_preimage_hash!(nodes[0]);
-
-       nodes[0].node.send_payment(route.clone(), payment_hash_2).unwrap();
-       check_added_monitors!(nodes[0], 1);
-
-       let events_1 = nodes[0].node.get_and_clear_pending_msg_events();
-       assert_eq!(events_1.len(), 1);
-       match events_1[0] {
-               MessageSendEvent::UpdateHTLCs { .. } => {},
-               _ => panic!("Unexpected event"),
-       }
-
-       assert!(nodes[1].node.claim_funds(payment_preimage_1));
-       check_added_monitors!(nodes[1], 1);
-
-       let events_2 = nodes[1].node.get_and_clear_pending_msg_events();
-       assert_eq!(events_2.len(), 1);
-       match events_2[0] {
-               MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => {
-                       assert_eq!(*node_id, nodes[0].node.get_our_node_id());
-                       assert!(update_add_htlcs.is_empty());
-                       assert_eq!(update_fulfill_htlcs.len(), 1);
-                       assert!(update_fail_htlcs.is_empty());
-                       assert!(update_fail_malformed_htlcs.is_empty());
-                       assert!(update_fee.is_none());
-
-                       nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_htlcs[0]).unwrap();
-                       let events_3 = nodes[0].node.get_and_clear_pending_events();
-                       assert_eq!(events_3.len(), 1);
-                       match events_3[0] {
-                               Event::PaymentSent { ref payment_preimage } => {
-                                       assert_eq!(*payment_preimage, payment_preimage_1);
-                               },
-                               _ => panic!("Unexpected event"),
-                       }
-
-                       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), commitment_signed).unwrap();
-                       let _ = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
-                       // No commitment_signed so get_event_msg's assert(len == 1) passes
-                       check_added_monitors!(nodes[0], 1);
-               },
-               _ => panic!("Unexpected event"),
-       }
-
-       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
-       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
-
-       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id());
-       let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
-       assert_eq!(reestablish_1.len(), 1);
-       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id());
-       let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
-       assert_eq!(reestablish_2.len(), 1);
-
-       nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]).unwrap();
-       let as_resp = handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
-       nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]).unwrap();
-       let bs_resp = handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
-
-       assert!(as_resp.0.is_none());
-       assert!(bs_resp.0.is_none());
-
-       assert!(bs_resp.1.is_none());
-       assert!(bs_resp.2.is_none());
-
-       assert!(as_resp.3 == RAACommitmentOrder::CommitmentFirst);
-
-       assert_eq!(as_resp.2.as_ref().unwrap().update_add_htlcs.len(), 1);
-       assert!(as_resp.2.as_ref().unwrap().update_fulfill_htlcs.is_empty());
-       assert!(as_resp.2.as_ref().unwrap().update_fail_htlcs.is_empty());
-       assert!(as_resp.2.as_ref().unwrap().update_fail_malformed_htlcs.is_empty());
-       assert!(as_resp.2.as_ref().unwrap().update_fee.is_none());
-       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &as_resp.2.as_ref().unwrap().update_add_htlcs[0]).unwrap();
-       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_resp.2.as_ref().unwrap().commitment_signed).unwrap();
-       let bs_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
-       // No commitment_signed so get_event_msg's assert(len == 1) passes
-       check_added_monitors!(nodes[1], 1);
-
-       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), as_resp.1.as_ref().unwrap()).unwrap();
-       let bs_second_commitment_signed = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-       assert!(bs_second_commitment_signed.update_add_htlcs.is_empty());
-       assert!(bs_second_commitment_signed.update_fulfill_htlcs.is_empty());
-       assert!(bs_second_commitment_signed.update_fail_htlcs.is_empty());
-       assert!(bs_second_commitment_signed.update_fail_malformed_htlcs.is_empty());
-       assert!(bs_second_commitment_signed.update_fee.is_none());
-       check_added_monitors!(nodes[1], 1);
-
-       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
-       let as_commitment_signed = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
-       assert!(as_commitment_signed.update_add_htlcs.is_empty());
-       assert!(as_commitment_signed.update_fulfill_htlcs.is_empty());
-       assert!(as_commitment_signed.update_fail_htlcs.is_empty());
-       assert!(as_commitment_signed.update_fail_malformed_htlcs.is_empty());
-       assert!(as_commitment_signed.update_fee.is_none());
-       check_added_monitors!(nodes[0], 1);
-
-       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_second_commitment_signed.commitment_signed).unwrap();
-       let as_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
-       // No commitment_signed so get_event_msg's assert(len == 1) passes
-       check_added_monitors!(nodes[0], 1);
-
-       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_commitment_signed.commitment_signed).unwrap();
-       let bs_second_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
-       // No commitment_signed so get_event_msg's assert(len == 1) passes
-       check_added_monitors!(nodes[1], 1);
-
-       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_and_ack).unwrap();
-       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-       check_added_monitors!(nodes[1], 1);
-
-       expect_pending_htlcs_forwardable!(nodes[1]);
-
-       let events_5 = nodes[1].node.get_and_clear_pending_events();
-       assert_eq!(events_5.len(), 1);
-       match events_5[0] {
-               Event::PaymentReceived { ref payment_hash, amt: _ } => {
-                       assert_eq!(payment_hash_2, *payment_hash);
-               },
-               _ => panic!("Unexpected event"),
-       }
-
-       nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_revoke_and_ack).unwrap();
-       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
-       check_added_monitors!(nodes[0], 1);
-
-       claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
-}
-
-#[test]
-fn test_invalid_channel_announcement() {
-       //Test BOLT 7 channel_announcement msg requirement for final node, gather data to build customed channel_announcement msgs
-       let secp_ctx = Secp256k1::new();
-       let nodes = create_network(2, &[None, None]);
-
-       let chan_announcement = create_chan_between_nodes(&nodes[0], &nodes[1], LocalFeatures::new(), LocalFeatures::new());
-
-       let a_channel_lock = nodes[0].node.channel_state.lock().unwrap();
-       let b_channel_lock = nodes[1].node.channel_state.lock().unwrap();
-       let as_chan = a_channel_lock.by_id.get(&chan_announcement.3).unwrap();
-       let bs_chan = b_channel_lock.by_id.get(&chan_announcement.3).unwrap();
-
-       let _ = nodes[0].router.handle_htlc_fail_channel_update(&msgs::HTLCFailChannelUpdate::ChannelClosed { short_channel_id : as_chan.get_short_channel_id().unwrap(), is_permanent: false } );
-
-       let as_bitcoin_key = PublicKey::from_secret_key(&secp_ctx, &as_chan.get_local_keys().funding_key);
-       let bs_bitcoin_key = PublicKey::from_secret_key(&secp_ctx, &bs_chan.get_local_keys().funding_key);
-
-       let as_network_key = nodes[0].node.get_our_node_id();
-       let bs_network_key = nodes[1].node.get_our_node_id();
-
-       let were_node_one = as_bitcoin_key.serialize()[..] < bs_bitcoin_key.serialize()[..];
-
-       let mut chan_announcement;
-
-       macro_rules! dummy_unsigned_msg {
-               () => {
-                       msgs::UnsignedChannelAnnouncement {
-                               features: msgs::GlobalFeatures::new(),
-                               chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
-                               short_channel_id: as_chan.get_short_channel_id().unwrap(),
-                               node_id_1: if were_node_one { as_network_key } else { bs_network_key },
-                               node_id_2: if were_node_one { bs_network_key } else { as_network_key },
-                               bitcoin_key_1: if were_node_one { as_bitcoin_key } else { bs_bitcoin_key },
-                               bitcoin_key_2: if were_node_one { bs_bitcoin_key } else { as_bitcoin_key },
-                               excess_data: Vec::new(),
-                       };
-               }
-       }
-
-       macro_rules! sign_msg {
-               ($unsigned_msg: expr) => {
-                       let msghash = Message::from_slice(&Sha256dHash::hash(&$unsigned_msg.encode()[..])[..]).unwrap();
-                       let as_bitcoin_sig = secp_ctx.sign(&msghash, &as_chan.get_local_keys().funding_key);
-                       let bs_bitcoin_sig = secp_ctx.sign(&msghash, &bs_chan.get_local_keys().funding_key);
-                       let as_node_sig = secp_ctx.sign(&msghash, &nodes[0].keys_manager.get_node_secret());
-                       let bs_node_sig = secp_ctx.sign(&msghash, &nodes[1].keys_manager.get_node_secret());
-                       chan_announcement = msgs::ChannelAnnouncement {
-                               node_signature_1 : if were_node_one { as_node_sig } else { bs_node_sig},
-                               node_signature_2 : if were_node_one { bs_node_sig } else { as_node_sig},
-                               bitcoin_signature_1: if were_node_one { as_bitcoin_sig } else { bs_bitcoin_sig },
-                               bitcoin_signature_2 : if were_node_one { bs_bitcoin_sig } else { as_bitcoin_sig },
-                               contents: $unsigned_msg
-                       }
-               }
-       }
-
-       let unsigned_msg = dummy_unsigned_msg!();
-       sign_msg!(unsigned_msg);
-       assert_eq!(nodes[0].router.handle_channel_announcement(&chan_announcement).unwrap(), true);
-       let _ = nodes[0].router.handle_htlc_fail_channel_update(&msgs::HTLCFailChannelUpdate::ChannelClosed { short_channel_id : as_chan.get_short_channel_id().unwrap(), is_permanent: false } );
-
-       // Configured with Network::Testnet
-       let mut unsigned_msg = dummy_unsigned_msg!();
-       unsigned_msg.chain_hash = genesis_block(Network::Bitcoin).header.bitcoin_hash();
-       sign_msg!(unsigned_msg);
-       assert!(nodes[0].router.handle_channel_announcement(&chan_announcement).is_err());
-
-       let mut unsigned_msg = dummy_unsigned_msg!();
-       unsigned_msg.chain_hash = Sha256dHash::hash(&[1,2,3,4,5,6,7,8,9]);
-       sign_msg!(unsigned_msg);
-       assert!(nodes[0].router.handle_channel_announcement(&chan_announcement).is_err());
-}
-
-#[test]
-fn test_no_txn_manager_serialize_deserialize() {
-       let mut nodes = create_network(2, &[None, None]);
-
-       let tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 100000, 10001, LocalFeatures::new(), LocalFeatures::new());
-
-       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
-
-       let nodes_0_serialized = nodes[0].node.encode();
-       let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new());
-       nodes[0].chan_monitor.simple_monitor.monitors.lock().unwrap().iter().next().unwrap().1.write_for_disk(&mut chan_0_monitor_serialized).unwrap();
-
-       nodes[0].chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(nodes[0].chain_monitor.clone(), nodes[0].tx_broadcaster.clone(), Arc::new(test_utils::TestLogger::new()), Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 })));
-       let mut chan_0_monitor_read = &chan_0_monitor_serialized.0[..];
-       let (_, chan_0_monitor) = <(Sha256dHash, ChannelMonitor)>::read(&mut chan_0_monitor_read, Arc::new(test_utils::TestLogger::new())).unwrap();
-       assert!(chan_0_monitor_read.is_empty());
-
-       let mut nodes_0_read = &nodes_0_serialized[..];
-       let config = UserConfig::new();
-       let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&nodes[0].node_seed, Network::Testnet, Arc::new(test_utils::TestLogger::new())));
-       let (_, nodes_0_deserialized) = {
-               let mut channel_monitors = HashMap::new();
-               channel_monitors.insert(chan_0_monitor.get_funding_txo().unwrap(), &chan_0_monitor);
-               <(Sha256dHash, ChannelManager)>::read(&mut nodes_0_read, ChannelManagerReadArgs {
-                       default_config: config,
-                       keys_manager,
-                       fee_estimator: Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 }),
-                       monitor: nodes[0].chan_monitor.clone(),
-                       chain_monitor: nodes[0].chain_monitor.clone(),
-                       tx_broadcaster: nodes[0].tx_broadcaster.clone(),
-                       logger: Arc::new(test_utils::TestLogger::new()),
-                       channel_monitors: &channel_monitors,
-               }).unwrap()
-       };
-       assert!(nodes_0_read.is_empty());
-
-       assert!(nodes[0].chan_monitor.add_update_monitor(chan_0_monitor.get_funding_txo().unwrap(), chan_0_monitor).is_ok());
-       nodes[0].node = Arc::new(nodes_0_deserialized);
-       let nodes_0_as_listener: Arc<ChainListener> = nodes[0].node.clone();
-       nodes[0].chain_monitor.register_listener(Arc::downgrade(&nodes_0_as_listener));
-       assert_eq!(nodes[0].node.list_channels().len(), 1);
-       check_added_monitors!(nodes[0], 1);
-
-       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id());
-       let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
-       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id());
-       let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
-
-       nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]).unwrap();
-       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-       nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]).unwrap();
-       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
-
-       let (funding_locked, _) = create_chan_between_nodes_with_value_confirm(&nodes[0], &nodes[1], &tx);
-       let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &funding_locked);
-       for node in nodes.iter() {
-               assert!(node.router.handle_channel_announcement(&announcement).unwrap());
-               node.router.handle_channel_update(&as_update).unwrap();
-               node.router.handle_channel_update(&bs_update).unwrap();
-       }
-
-       send_payment(&nodes[0], &[&nodes[1]], 1000000);
-}
-
-#[test]
-fn test_simple_manager_serialize_deserialize() {
-       let mut nodes = create_network(2, &[None, None]);
-       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       let (our_payment_preimage, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
-       let (_, our_payment_hash) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
-
-       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
-
-       let nodes_0_serialized = nodes[0].node.encode();
-       let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new());
-       nodes[0].chan_monitor.simple_monitor.monitors.lock().unwrap().iter().next().unwrap().1.write_for_disk(&mut chan_0_monitor_serialized).unwrap();
-
-       nodes[0].chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(nodes[0].chain_monitor.clone(), nodes[0].tx_broadcaster.clone(), Arc::new(test_utils::TestLogger::new()), Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 })));
-       let mut chan_0_monitor_read = &chan_0_monitor_serialized.0[..];
-       let (_, chan_0_monitor) = <(Sha256dHash, ChannelMonitor)>::read(&mut chan_0_monitor_read, Arc::new(test_utils::TestLogger::new())).unwrap();
-       assert!(chan_0_monitor_read.is_empty());
-
-       let mut nodes_0_read = &nodes_0_serialized[..];
-       let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&nodes[0].node_seed, Network::Testnet, Arc::new(test_utils::TestLogger::new())));
-       let (_, nodes_0_deserialized) = {
-               let mut channel_monitors = HashMap::new();
-               channel_monitors.insert(chan_0_monitor.get_funding_txo().unwrap(), &chan_0_monitor);
-               <(Sha256dHash, ChannelManager)>::read(&mut nodes_0_read, ChannelManagerReadArgs {
-                       default_config: UserConfig::new(),
-                       keys_manager,
-                       fee_estimator: Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 }),
-                       monitor: nodes[0].chan_monitor.clone(),
-                       chain_monitor: nodes[0].chain_monitor.clone(),
-                       tx_broadcaster: nodes[0].tx_broadcaster.clone(),
-                       logger: Arc::new(test_utils::TestLogger::new()),
-                       channel_monitors: &channel_monitors,
-               }).unwrap()
-       };
-       assert!(nodes_0_read.is_empty());
-
-       assert!(nodes[0].chan_monitor.add_update_monitor(chan_0_monitor.get_funding_txo().unwrap(), chan_0_monitor).is_ok());
-       nodes[0].node = Arc::new(nodes_0_deserialized);
-       check_added_monitors!(nodes[0], 1);
-
-       reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
-
-       fail_payment(&nodes[0], &[&nodes[1]], our_payment_hash);
-       claim_payment(&nodes[0], &[&nodes[1]], our_payment_preimage);
-}
-
-#[test]
-fn test_manager_serialize_deserialize_inconsistent_monitor() {
-       // Test deserializing a ChannelManager with an out-of-date ChannelMonitor
-       let mut nodes = create_network(4, &[None, None, None, None]);
-       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-       create_announced_chan_between_nodes(&nodes, 2, 0, LocalFeatures::new(), LocalFeatures::new());
-       let (_, _, channel_id, funding_tx) = create_announced_chan_between_nodes(&nodes, 0, 3, LocalFeatures::new(), LocalFeatures::new());
-
-       let (our_payment_preimage, _) = route_payment(&nodes[2], &[&nodes[0], &nodes[1]], 1000000);
-
-       // Serialize the ChannelManager here, but the monitor we keep up-to-date
-       let nodes_0_serialized = nodes[0].node.encode();
-
-       route_payment(&nodes[0], &[&nodes[3]], 1000000);
-       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
-       nodes[2].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
-       nodes[3].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
-
-       // Now the ChannelMonitor (which is now out-of-sync with ChannelManager for channel w/
-       // nodes[3])
-       let mut node_0_monitors_serialized = Vec::new();
-       for monitor in nodes[0].chan_monitor.simple_monitor.monitors.lock().unwrap().iter() {
-               let mut writer = test_utils::TestVecWriter(Vec::new());
-               monitor.1.write_for_disk(&mut writer).unwrap();
-               node_0_monitors_serialized.push(writer.0);
-       }
-
-       nodes[0].chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(nodes[0].chain_monitor.clone(), nodes[0].tx_broadcaster.clone(), Arc::new(test_utils::TestLogger::new()), Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 })));
-       let mut node_0_monitors = Vec::new();
-       for serialized in node_0_monitors_serialized.iter() {
-               let mut read = &serialized[..];
-               let (_, monitor) = <(Sha256dHash, ChannelMonitor)>::read(&mut read, Arc::new(test_utils::TestLogger::new())).unwrap();
-               assert!(read.is_empty());
-               node_0_monitors.push(monitor);
-       }
-
-       let mut nodes_0_read = &nodes_0_serialized[..];
-       let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&nodes[0].node_seed, Network::Testnet, Arc::new(test_utils::TestLogger::new())));
-       let (_, nodes_0_deserialized) = <(Sha256dHash, ChannelManager)>::read(&mut nodes_0_read, ChannelManagerReadArgs {
-               default_config: UserConfig::new(),
-               keys_manager,
-               fee_estimator: Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 }),
-               monitor: nodes[0].chan_monitor.clone(),
-               chain_monitor: nodes[0].chain_monitor.clone(),
-               tx_broadcaster: nodes[0].tx_broadcaster.clone(),
-               logger: Arc::new(test_utils::TestLogger::new()),
-               channel_monitors: &node_0_monitors.iter().map(|monitor| { (monitor.get_funding_txo().unwrap(), monitor) }).collect(),
-       }).unwrap();
-       assert!(nodes_0_read.is_empty());
-
-       { // Channel close should result in a commitment tx and an HTLC tx
-               let txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
-               assert_eq!(txn.len(), 2);
-               assert_eq!(txn[0].input[0].previous_output.txid, funding_tx.txid());
-               assert_eq!(txn[1].input[0].previous_output.txid, txn[0].txid());
-       }
-
-       for monitor in node_0_monitors.drain(..) {
-               assert!(nodes[0].chan_monitor.add_update_monitor(monitor.get_funding_txo().unwrap(), monitor).is_ok());
-               check_added_monitors!(nodes[0], 1);
-       }
-       nodes[0].node = Arc::new(nodes_0_deserialized);
-
-       // nodes[1] and nodes[2] have no lost state with nodes[0]...
-       reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
-       reconnect_nodes(&nodes[0], &nodes[2], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
-       //... and we can even still claim the payment!
-       claim_payment(&nodes[2], &[&nodes[0], &nodes[1]], our_payment_preimage);
-
-       nodes[3].node.peer_connected(&nodes[0].node.get_our_node_id());
-       let reestablish = get_event_msg!(nodes[3], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id());
-       nodes[0].node.peer_connected(&nodes[3].node.get_our_node_id());
-       if let Err(msgs::HandleError { action: Some(msgs::ErrorAction::SendErrorMessage { msg }), .. }) = nodes[0].node.handle_channel_reestablish(&nodes[3].node.get_our_node_id(), &reestablish) {
-               assert_eq!(msg.channel_id, channel_id);
-       } else { panic!("Unexpected result"); }
-}
-
-macro_rules! check_spendable_outputs {
-       ($node: expr, $der_idx: expr) => {
-               {
-                       let events = $node.chan_monitor.simple_monitor.get_and_clear_pending_events();
-                       let mut txn = Vec::new();
-                       for event in events {
-                               match event {
-                                       Event::SpendableOutputs { ref outputs } => {
-                                               for outp in outputs {
-                                                       match *outp {
-                                                               SpendableOutputDescriptor::DynamicOutputP2WPKH { ref outpoint, ref key, ref output } => {
-                                                                       let input = TxIn {
-                                                                               previous_output: outpoint.clone(),
-                                                                               script_sig: Script::new(),
-                                                                               sequence: 0,
-                                                                               witness: Vec::new(),
-                                                                       };
-                                                                       let outp = TxOut {
-                                                                               script_pubkey: Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(),
-                                                                               value: output.value,
-                                                                       };
-                                                                       let mut spend_tx = Transaction {
-                                                                               version: 2,
-                                                                               lock_time: 0,
-                                                                               input: vec![input],
-                                                                               output: vec![outp],
-                                                                       };
-                                                                       let secp_ctx = Secp256k1::new();
-                                                                       let remotepubkey = PublicKey::from_secret_key(&secp_ctx, &key);
-                                                                       let witness_script = Address::p2pkh(&::bitcoin::PublicKey{compressed: true, key: remotepubkey}, Network::Testnet).script_pubkey();
-                                                                       let sighash = Message::from_slice(&bip143::SighashComponents::new(&spend_tx).sighash_all(&spend_tx.input[0], &witness_script, output.value)[..]).unwrap();
-                                                                       let remotesig = secp_ctx.sign(&sighash, key);
-                                                                       spend_tx.input[0].witness.push(remotesig.serialize_der().to_vec());
-                                                                       spend_tx.input[0].witness[0].push(SigHashType::All as u8);
-                                                                       spend_tx.input[0].witness.push(remotepubkey.serialize().to_vec());
-                                                                       txn.push(spend_tx);
-                                                               },
-                                                               SpendableOutputDescriptor::DynamicOutputP2WSH { ref outpoint, ref key, ref witness_script, ref to_self_delay, ref output } => {
-                                                                       let input = TxIn {
-                                                                               previous_output: outpoint.clone(),
-                                                                               script_sig: Script::new(),
-                                                                               sequence: *to_self_delay as u32,
-                                                                               witness: Vec::new(),
-                                                                       };
-                                                                       let outp = TxOut {
-                                                                               script_pubkey: Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(),
-                                                                               value: output.value,
-                                                                       };
-                                                                       let mut spend_tx = Transaction {
-                                                                               version: 2,
-                                                                               lock_time: 0,
-                                                                               input: vec![input],
-                                                                               output: vec![outp],
-                                                                       };
-                                                                       let secp_ctx = Secp256k1::new();
-                                                                       let sighash = Message::from_slice(&bip143::SighashComponents::new(&spend_tx).sighash_all(&spend_tx.input[0], witness_script, output.value)[..]).unwrap();
-                                                                       let local_delaysig = secp_ctx.sign(&sighash, key);
-                                                                       spend_tx.input[0].witness.push(local_delaysig.serialize_der().to_vec());
-                                                                       spend_tx.input[0].witness[0].push(SigHashType::All as u8);
-                                                                       spend_tx.input[0].witness.push(vec!(0));
-                                                                       spend_tx.input[0].witness.push(witness_script.clone().into_bytes());
-                                                                       txn.push(spend_tx);
-                                                               },
-                                                               SpendableOutputDescriptor::StaticOutput { ref outpoint, ref output } => {
-                                                                       let secp_ctx = Secp256k1::new();
-                                                                       let input = TxIn {
-                                                                               previous_output: outpoint.clone(),
-                                                                               script_sig: Script::new(),
-                                                                               sequence: 0,
-                                                                               witness: Vec::new(),
-                                                                       };
-                                                                       let outp = TxOut {
-                                                                               script_pubkey: Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(),
-                                                                               value: output.value,
-                                                                       };
-                                                                       let mut spend_tx = Transaction {
-                                                                               version: 2,
-                                                                               lock_time: 0,
-                                                                               input: vec![input],
-                                                                               output: vec![outp.clone()],
-                                                                       };
-                                                                       let secret = {
-                                                                               match ExtendedPrivKey::new_master(Network::Testnet, &$node.node_seed) {
-                                                                                       Ok(master_key) => {
-                                                                                               match master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx($der_idx).expect("key space exhausted")) {
-                                                                                                       Ok(key) => key,
-                                                                                                       Err(_) => panic!("Your RNG is busted"),
-                                                                                               }
-                                                                                       }
-                                                                                       Err(_) => panic!("Your rng is busted"),
-                                                                               }
-                                                                       };
-                                                                       let pubkey = ExtendedPubKey::from_private(&secp_ctx, &secret).public_key;
-                                                                       let witness_script = Address::p2pkh(&pubkey, Network::Testnet).script_pubkey();
-                                                                       let sighash = Message::from_slice(&bip143::SighashComponents::new(&spend_tx).sighash_all(&spend_tx.input[0], &witness_script, output.value)[..]).unwrap();
-                                                                       let sig = secp_ctx.sign(&sighash, &secret.private_key.key);
-                                                                       spend_tx.input[0].witness.push(sig.serialize_der().to_vec());
-                                                                       spend_tx.input[0].witness[0].push(SigHashType::All as u8);
-                                                                       spend_tx.input[0].witness.push(pubkey.key.serialize().to_vec());
-                                                                       txn.push(spend_tx);
-                                                               },
-                                                       }
-                                               }
-                                       },
-                                       _ => panic!("Unexpected event"),
-                               };
-                       }
-                       txn
-               }
-       }
-}
-
-#[test]
-fn test_claim_sizeable_push_msat() {
-       // Incidentally test SpendableOutput event generation due to detection of to_local output on commitment tx
-       let nodes = create_network(2, &[None, None]);
-
-       let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 99000000, LocalFeatures::new(), LocalFeatures::new());
-       nodes[1].node.force_close_channel(&chan.2);
-       check_closed_broadcast!(nodes[1]);
-       let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
-       assert_eq!(node_txn.len(), 1);
-       check_spends!(node_txn[0], chan.3.clone());
-       assert_eq!(node_txn[0].output.len(), 2); // We can't force trimming of to_remote output as channel_reserve_satoshis block us to do so at channel opening
-
-       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn[0].clone()] }, 0);
-       let spend_txn = check_spendable_outputs!(nodes[1], 1);
-       assert_eq!(spend_txn.len(), 1);
-       check_spends!(spend_txn[0], node_txn[0].clone());
-}
-
-#[test]
-fn test_claim_on_remote_sizeable_push_msat() {
-       // Same test as previous, just test on remote commitment tx, as per_commitment_point registration changes following you're funder/fundee and
-       // to_remote output is encumbered by a P2WPKH
-
-       let nodes = create_network(2, &[None, None]);
-
-       let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 99000000, LocalFeatures::new(), LocalFeatures::new());
-       nodes[0].node.force_close_channel(&chan.2);
-       check_closed_broadcast!(nodes[0]);
-
-       let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
-       assert_eq!(node_txn.len(), 1);
-       check_spends!(node_txn[0], chan.3.clone());
-       assert_eq!(node_txn[0].output.len(), 2); // We can't force trimming of to_remote output as channel_reserve_satoshis block us to do so at channel opening
-
-       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn[0].clone()] }, 0);
-       check_closed_broadcast!(nodes[1]);
-       let spend_txn = check_spendable_outputs!(nodes[1], 1);
-       assert_eq!(spend_txn.len(), 2);
-       assert_eq!(spend_txn[0], spend_txn[1]);
-       check_spends!(spend_txn[0], node_txn[0].clone());
-}
-
-#[test]
-fn test_claim_on_remote_revoked_sizeable_push_msat() {
-       // Same test as previous, just test on remote revoked commitment tx, as per_commitment_point registration changes following you're funder/fundee and
-       // to_remote output is encumbered by a P2WPKH
-
-       let nodes = create_network(2, &[None, None]);
-
-       let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 59000000, LocalFeatures::new(), LocalFeatures::new());
-       let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
-       let revoked_local_txn = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone();
-       assert_eq!(revoked_local_txn[0].input.len(), 1);
-       assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan.3.txid());
-
-       claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
-       let  header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
-       check_closed_broadcast!(nodes[1]);
-
-       let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
-       let spend_txn = check_spendable_outputs!(nodes[1], 1);
-       assert_eq!(spend_txn.len(), 4);
-       assert_eq!(spend_txn[0], spend_txn[2]); // to_remote output on revoked remote commitment_tx
-       check_spends!(spend_txn[0], revoked_local_txn[0].clone());
-       assert_eq!(spend_txn[1], spend_txn[3]); // to_local output on local commitment tx
-       check_spends!(spend_txn[1], node_txn[0].clone());
-}
-
-#[test]
-fn test_static_spendable_outputs_preimage_tx() {
-       let nodes = create_network(2, &[None, None]);
-
-       // Create some initial channels
-       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
-
-       let commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
-       assert_eq!(commitment_tx[0].input.len(), 1);
-       assert_eq!(commitment_tx[0].input[0].previous_output.txid, chan_1.3.txid());
-
-       // Settle A's commitment tx on B's chain
-       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-       assert!(nodes[1].node.claim_funds(payment_preimage));
-       check_added_monitors!(nodes[1], 1);
-       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()] }, 1);
-       let events = nodes[1].node.get_and_clear_pending_msg_events();
-       match events[0] {
-               MessageSendEvent::UpdateHTLCs { .. } => {},
-               _ => panic!("Unexpected event"),
-       }
-       match events[1] {
-               MessageSendEvent::BroadcastChannelUpdate { .. } => {},
-               _ => panic!("Unexepected event"),
-       }
-
-       // Check B's monitor was able to send back output descriptor event for preimage tx on A's commitment tx
-       let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap(); // ChannelManager : 1 (local commitment tx), ChannelMonitor: 2 (1 preimage tx) * 2 (block-rescan)
-       check_spends!(node_txn[0], commitment_tx[0].clone());
-       assert_eq!(node_txn[0], node_txn[2]);
-       assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
-       check_spends!(node_txn[1], chan_1.3.clone());
-
-       let spend_txn = check_spendable_outputs!(nodes[1], 1); // , 0, 0, 1, 1);
-       assert_eq!(spend_txn.len(), 2);
-       assert_eq!(spend_txn[0], spend_txn[1]);
-       check_spends!(spend_txn[0], node_txn[0].clone());
-}
-
-#[test]
-fn test_static_spendable_outputs_justice_tx_revoked_commitment_tx() {
-       let nodes = create_network(2, &[None, None]);
-
-       // Create some initial channels
-       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
-       let revoked_local_txn = nodes[0].node.channel_state.lock().unwrap().by_id.iter().next().unwrap().1.last_local_commitment_txn.clone();
-       assert_eq!(revoked_local_txn[0].input.len(), 1);
-       assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_1.3.txid());
-
-       claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
-
-       let  header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
-       check_closed_broadcast!(nodes[1]);
-
-       let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
-       assert_eq!(node_txn.len(), 3);
-       assert_eq!(node_txn.pop().unwrap(), node_txn[0]);
-       assert_eq!(node_txn[0].input.len(), 2);
-       check_spends!(node_txn[0], revoked_local_txn[0].clone());
-
-       let spend_txn = check_spendable_outputs!(nodes[1], 1);
-       assert_eq!(spend_txn.len(), 2);
-       assert_eq!(spend_txn[0], spend_txn[1]);
-       check_spends!(spend_txn[0], node_txn[0].clone());
-}
-
-#[test]
-fn test_static_spendable_outputs_justice_tx_revoked_htlc_timeout_tx() {
-       let nodes = create_network(2, &[None, None]);
-
-       // Create some initial channels
-       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
-       let revoked_local_txn = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
-       assert_eq!(revoked_local_txn[0].input.len(), 1);
-       assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_1.3.txid());
-
-       claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
-
-       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-       // A will generate HTLC-Timeout from revoked commitment tx
-       nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
-       check_closed_broadcast!(nodes[0]);
-
-       let revoked_htlc_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
-       assert_eq!(revoked_htlc_txn.len(), 3);
-       assert_eq!(revoked_htlc_txn[0], revoked_htlc_txn[2]);
-       assert_eq!(revoked_htlc_txn[0].input.len(), 1);
-       assert_eq!(revoked_htlc_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
-       check_spends!(revoked_htlc_txn[0], revoked_local_txn[0].clone());
-       check_spends!(revoked_htlc_txn[1], chan_1.3.clone());
-
-       // B will generate justice tx from A's revoked commitment/HTLC tx
-       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone(), revoked_htlc_txn[0].clone()] }, 1);
-       check_closed_broadcast!(nodes[1]);
-
-       let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
-       assert_eq!(node_txn.len(), 4);
-       assert_eq!(node_txn[3].input.len(), 1);
-       check_spends!(node_txn[3], revoked_htlc_txn[0].clone());
-
-       // Check B's ChannelMonitor was able to generate the right spendable output descriptor
-       let spend_txn = check_spendable_outputs!(nodes[1], 1);
-       assert_eq!(spend_txn.len(), 3);
-       assert_eq!(spend_txn[0], spend_txn[1]);
-       check_spends!(spend_txn[0], node_txn[0].clone());
-       check_spends!(spend_txn[2], node_txn[3].clone());
-}
-
-#[test]
-fn test_static_spendable_outputs_justice_tx_revoked_htlc_success_tx() {
-       let nodes = create_network(2, &[None, None]);
-
-       // Create some initial channels
-       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
-       let revoked_local_txn = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
-       assert_eq!(revoked_local_txn[0].input.len(), 1);
-       assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_1.3.txid());
-
-       claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
-
-       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-       // B will generate HTLC-Success from revoked commitment tx
-       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
-       check_closed_broadcast!(nodes[1]);
-       let revoked_htlc_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
-
-       assert_eq!(revoked_htlc_txn.len(), 3);
-       assert_eq!(revoked_htlc_txn[0], revoked_htlc_txn[2]);
-       assert_eq!(revoked_htlc_txn[0].input.len(), 1);
-       assert_eq!(revoked_htlc_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
-       check_spends!(revoked_htlc_txn[0], revoked_local_txn[0].clone());
-
-       // A will generate justice tx from B's revoked commitment/HTLC tx
-       nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone(), revoked_htlc_txn[0].clone()] }, 1);
-       check_closed_broadcast!(nodes[0]);
-
-       let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
-       assert_eq!(node_txn.len(), 4);
-       assert_eq!(node_txn[3].input.len(), 1);
-       check_spends!(node_txn[3], revoked_htlc_txn[0].clone());
-
-       // Check A's ChannelMonitor was able to generate the right spendable output descriptor
-       let spend_txn = check_spendable_outputs!(nodes[0], 1);
-       assert_eq!(spend_txn.len(), 5);
-       assert_eq!(spend_txn[0], spend_txn[2]);
-       assert_eq!(spend_txn[1], spend_txn[3]);
-       check_spends!(spend_txn[0], revoked_local_txn[0].clone()); // spending to_remote output from revoked local tx
-       check_spends!(spend_txn[1], node_txn[2].clone()); // spending justice tx output from revoked local tx htlc received output
-       check_spends!(spend_txn[4], node_txn[3].clone()); // spending justice tx output on htlc success tx
-}
-
-#[test]
-fn test_onchain_to_onchain_claim() {
-       // Test that in case of channel closure, we detect the state of output thanks to
-       // ChainWatchInterface and claim HTLC on downstream peer's remote commitment tx.
-       // First, have C claim an HTLC against its own latest commitment transaction.
-       // Then, broadcast these to B, which should update the monitor downstream on the A<->B
-       // channel.
-       // Finally, check that B will claim the HTLC output if A's latest commitment transaction
-       // gets broadcast.
-
-       let nodes = create_network(3, &[None, None, None]);
-
-       // Create some initial channels
-       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-       let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
-
-       // Rebalance the network a bit by relaying one payment through all the channels ...
-       send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
-       send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
-
-       let (payment_preimage, _payment_hash) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), 3000000);
-       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42};
-       let commitment_tx = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone();
-       check_spends!(commitment_tx[0], chan_2.3.clone());
-       nodes[2].node.claim_funds(payment_preimage);
-       check_added_monitors!(nodes[2], 1);
-       let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
-       assert!(updates.update_add_htlcs.is_empty());
-       assert!(updates.update_fail_htlcs.is_empty());
-       assert_eq!(updates.update_fulfill_htlcs.len(), 1);
-       assert!(updates.update_fail_malformed_htlcs.is_empty());
-
-       nodes[2].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 1);
-       check_closed_broadcast!(nodes[2]);
-
-       let c_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 2 (commitment tx, HTLC-Success tx), ChannelMonitor : 1 (HTLC-Success tx)
-       assert_eq!(c_txn.len(), 3);
-       assert_eq!(c_txn[0], c_txn[2]);
-       assert_eq!(commitment_tx[0], c_txn[1]);
-       check_spends!(c_txn[1], chan_2.3.clone());
-       check_spends!(c_txn[2], c_txn[1].clone());
-       assert_eq!(c_txn[1].input[0].witness.clone().last().unwrap().len(), 71);
-       assert_eq!(c_txn[2].input[0].witness.clone().last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
-       assert!(c_txn[0].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
-       assert_eq!(c_txn[0].lock_time, 0); // Success tx
-
-       // So we broadcast C's commitment tx and HTLC-Success on B's chain, we should successfully be able to extract preimage and update downstream monitor
-       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![c_txn[1].clone(), c_txn[2].clone()]}, 1);
-       {
-               let mut b_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
-               assert_eq!(b_txn.len(), 4);
-               assert_eq!(b_txn[0], b_txn[3]);
-               check_spends!(b_txn[1], chan_2.3); // B local commitment tx, issued by ChannelManager
-               check_spends!(b_txn[2], b_txn[1].clone()); // HTLC-Timeout on B local commitment tx, issued by ChannelManager
-               assert_eq!(b_txn[2].input[0].witness.clone().last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
-               assert!(b_txn[2].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
-               assert_ne!(b_txn[2].lock_time, 0); // Timeout tx
-               check_spends!(b_txn[0], c_txn[1].clone()); // timeout tx on C remote commitment tx, issued by ChannelMonitor, * 2 due to block rescan
-               assert_eq!(b_txn[0].input[0].witness.clone().last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
-               assert!(b_txn[0].output[0].script_pubkey.is_v0_p2wpkh()); // direct payment
-               assert_ne!(b_txn[2].lock_time, 0); // Timeout tx
-               b_txn.clear();
-       }
-       let msg_events = nodes[1].node.get_and_clear_pending_msg_events();
-       check_added_monitors!(nodes[1], 1);
-       match msg_events[0] {
-               MessageSendEvent::BroadcastChannelUpdate {  .. } => {},
-               _ => panic!("Unexpected event"),
-       }
-       match msg_events[1] {
-               MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, .. } } => {
-                       assert!(update_add_htlcs.is_empty());
-                       assert!(update_fail_htlcs.is_empty());
-                       assert_eq!(update_fulfill_htlcs.len(), 1);
-                       assert!(update_fail_malformed_htlcs.is_empty());
-                       assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
-               },
-               _ => panic!("Unexpected event"),
-       };
-       // Broadcast A's commitment tx on B's chain to see if we are able to claim inbound HTLC with our HTLC-Success tx
-       let commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
-       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 1);
-       let b_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
-       assert_eq!(b_txn.len(), 3);
-       check_spends!(b_txn[1], chan_1.3); // Local commitment tx, issued by ChannelManager
-       assert_eq!(b_txn[0], b_txn[2]); // HTLC-Success tx, issued by ChannelMonitor, * 2 due to block rescan
-       check_spends!(b_txn[0], commitment_tx[0].clone());
-       assert_eq!(b_txn[0].input[0].witness.clone().last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
-       assert!(b_txn[0].output[0].script_pubkey.is_v0_p2wpkh()); // direct payment
-       assert_eq!(b_txn[2].lock_time, 0); // Success tx
-
-       check_closed_broadcast!(nodes[1]);
-}
-
-#[test]
-fn test_duplicate_payment_hash_one_failure_one_success() {
-       // Topology : A --> B --> C
-       // We route 2 payments with same hash between B and C, one will be timeout, the other successfully claim
-       let mut nodes = create_network(3, &[None, None, None]);
-
-       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-       let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
-
-       let (our_payment_preimage, duplicate_payment_hash) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 900000);
-       *nodes[0].network_payment_count.borrow_mut() -= 1;
-       assert_eq!(route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 900000).1, duplicate_payment_hash);
-
-       let commitment_txn = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone();
-       assert_eq!(commitment_txn[0].input.len(), 1);
-       check_spends!(commitment_txn[0], chan_2.3.clone());
-
-       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_txn[0].clone()] }, 1);
-       check_closed_broadcast!(nodes[1]);
-
-       let htlc_timeout_tx;
-       { // Extract one of the two HTLC-Timeout transaction
-               let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
-               assert_eq!(node_txn.len(), 7);
-               assert_eq!(node_txn[0], node_txn[5]);
-               assert_eq!(node_txn[1], node_txn[6]);
-               check_spends!(node_txn[0], commitment_txn[0].clone());
-               assert_eq!(node_txn[0].input.len(), 1);
-               check_spends!(node_txn[1], commitment_txn[0].clone());
-               assert_eq!(node_txn[1].input.len(), 1);
-               assert_ne!(node_txn[0].input[0], node_txn[1].input[0]);
-               check_spends!(node_txn[2], chan_2.3.clone());
-               check_spends!(node_txn[3], node_txn[2].clone());
-               check_spends!(node_txn[4], node_txn[2].clone());
-               htlc_timeout_tx = node_txn[1].clone();
-       }
-
-       nodes[2].node.claim_funds(our_payment_preimage);
-       nodes[2].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_txn[0].clone()] }, 1);
-       check_added_monitors!(nodes[2], 2);
-       let events = nodes[2].node.get_and_clear_pending_msg_events();
-       match events[0] {
-               MessageSendEvent::UpdateHTLCs { .. } => {},
-               _ => panic!("Unexpected event"),
-       }
-       match events[1] {
-               MessageSendEvent::BroadcastChannelUpdate { .. } => {},
-               _ => panic!("Unexepected event"),
-       }
-       let htlc_success_txn: Vec<_> = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
-       assert_eq!(htlc_success_txn.len(), 5);
-       check_spends!(htlc_success_txn[2], chan_2.3.clone());
-       assert_eq!(htlc_success_txn[0], htlc_success_txn[3]);
-       assert_eq!(htlc_success_txn[0].input.len(), 1);
-       assert_eq!(htlc_success_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
-       assert_eq!(htlc_success_txn[1], htlc_success_txn[4]);
-       assert_eq!(htlc_success_txn[1].input.len(), 1);
-       assert_eq!(htlc_success_txn[1].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
-       assert_ne!(htlc_success_txn[0].input[0], htlc_success_txn[1].input[0]);
-       check_spends!(htlc_success_txn[0], commitment_txn[0].clone());
-       check_spends!(htlc_success_txn[1], commitment_txn[0].clone());
-
-       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![htlc_timeout_tx] }, 200);
-       connect_blocks(&nodes[1].chain_monitor, ANTI_REORG_DELAY - 1, 200, true, header.bitcoin_hash());
-       expect_pending_htlcs_forwardable!(nodes[1]);
-       let htlc_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-       assert!(htlc_updates.update_add_htlcs.is_empty());
-       assert_eq!(htlc_updates.update_fail_htlcs.len(), 1);
-       assert_eq!(htlc_updates.update_fail_htlcs[0].htlc_id, 1);
-       assert!(htlc_updates.update_fulfill_htlcs.is_empty());
-       assert!(htlc_updates.update_fail_malformed_htlcs.is_empty());
-       check_added_monitors!(nodes[1], 1);
-
-       nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &htlc_updates.update_fail_htlcs[0]).unwrap();
-       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
-       {
-               commitment_signed_dance!(nodes[0], nodes[1], &htlc_updates.commitment_signed, false, true);
-               let events = nodes[0].node.get_and_clear_pending_msg_events();
-               assert_eq!(events.len(), 1);
-               match events[0] {
-                       MessageSendEvent::PaymentFailureNetworkUpdate { update: msgs::HTLCFailChannelUpdate::ChannelClosed { .. }  } => {
-                       },
-                       _ => { panic!("Unexpected event"); }
-               }
-       }
-       let events = nodes[0].node.get_and_clear_pending_events();
-       match events[0] {
-               Event::PaymentFailed { ref payment_hash, .. } => {
-                       assert_eq!(*payment_hash, duplicate_payment_hash);
-               }
-               _ => panic!("Unexpected event"),
-       }
-
-       // Solve 2nd HTLC by broadcasting on B's chain HTLC-Success Tx from C
-       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![htlc_success_txn[0].clone()] }, 200);
-       let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-       assert!(updates.update_add_htlcs.is_empty());
-       assert!(updates.update_fail_htlcs.is_empty());
-       assert_eq!(updates.update_fulfill_htlcs.len(), 1);
-       assert_eq!(updates.update_fulfill_htlcs[0].htlc_id, 0);
-       assert!(updates.update_fail_malformed_htlcs.is_empty());
-       check_added_monitors!(nodes[1], 1);
-
-       nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]).unwrap();
-       commitment_signed_dance!(nodes[0], nodes[1], &updates.commitment_signed, false);
-
-       let events = nodes[0].node.get_and_clear_pending_events();
-       match events[0] {
-               Event::PaymentSent { ref payment_preimage } => {
-                       assert_eq!(*payment_preimage, our_payment_preimage);
-               }
-               _ => panic!("Unexpected event"),
-       }
-}
-
-#[test]
-fn test_dynamic_spendable_outputs_local_htlc_success_tx() {
-       let nodes = create_network(2, &[None, None]);
-
-       // Create some initial channels
-       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 9000000).0;
-       let local_txn = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
-       assert_eq!(local_txn[0].input.len(), 1);
-       check_spends!(local_txn[0], chan_1.3.clone());
-
-       // Give B knowledge of preimage to be able to generate a local HTLC-Success Tx
-       nodes[1].node.claim_funds(payment_preimage);
-       check_added_monitors!(nodes[1], 1);
-       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![local_txn[0].clone()] }, 1);
-       let events = nodes[1].node.get_and_clear_pending_msg_events();
-       match events[0] {
-               MessageSendEvent::UpdateHTLCs { .. } => {},
-               _ => panic!("Unexpected event"),
-       }
-       match events[1] {
-               MessageSendEvent::BroadcastChannelUpdate { .. } => {},
-               _ => panic!("Unexepected event"),
-       }
-       let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
-       assert_eq!(node_txn[0].input.len(), 1);
-       assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
-       check_spends!(node_txn[0], local_txn[0].clone());
-
-       // Verify that B is able to spend its own HTLC-Success tx thanks to spendable output event given back by its ChannelMonitor
-       let spend_txn = check_spendable_outputs!(nodes[1], 1);
-       assert_eq!(spend_txn.len(), 2);
-       check_spends!(spend_txn[0], node_txn[0].clone());
-       check_spends!(spend_txn[1], node_txn[2].clone());
-}
-
-fn do_test_fail_backwards_unrevoked_remote_announce(deliver_last_raa: bool, announce_latest: bool) {
-       // Test that we fail backwards the full set of HTLCs we need to when remote broadcasts an
-       // unrevoked commitment transaction.
-       // This includes HTLCs which were below the dust threshold as well as HTLCs which were awaiting
-       // a remote RAA before they could be failed backwards (and combinations thereof).
-       // We also test duplicate-hash HTLCs by adding two nodes on each side of the target nodes which
-       // use the same payment hashes.
-       // Thus, we use a six-node network:
-       //
-       // A \         / E
-       //    - C - D -
-       // B /         \ F
-       // And test where C fails back to A/B when D announces its latest commitment transaction
-       let nodes = create_network(6, &[None, None, None, None, None, None]);
-
-       create_announced_chan_between_nodes(&nodes, 0, 2, LocalFeatures::new(), LocalFeatures::new());
-       create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
-       let chan = create_announced_chan_between_nodes(&nodes, 2, 3, LocalFeatures::new(), LocalFeatures::new());
-       create_announced_chan_between_nodes(&nodes, 3, 4, LocalFeatures::new(), LocalFeatures::new());
-       create_announced_chan_between_nodes(&nodes, 3, 5, LocalFeatures::new(), LocalFeatures::new());
-
-       // Rebalance and check output sanity...
-       send_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 500000);
-       send_payment(&nodes[1], &[&nodes[2], &nodes[3], &nodes[5]], 500000);
-       assert_eq!(nodes[3].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn[0].output.len(), 2);
-
-       let ds_dust_limit = nodes[3].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().our_dust_limit_satoshis;
-       // 0th HTLC:
-       let (_, payment_hash_1) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], ds_dust_limit*1000); // not added < dust limit + HTLC tx fee
-       // 1st HTLC:
-       let (_, payment_hash_2) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], ds_dust_limit*1000); // not added < dust limit + HTLC tx fee
-       let route = nodes[1].router.get_route(&nodes[5].node.get_our_node_id(), None, &Vec::new(), ds_dust_limit*1000, TEST_FINAL_CLTV).unwrap();
-       // 2nd HTLC:
-       send_along_route_with_hash(&nodes[1], route.clone(), &[&nodes[2], &nodes[3], &nodes[5]], ds_dust_limit*1000, payment_hash_1); // not added < dust limit + HTLC tx fee
-       // 3rd HTLC:
-       send_along_route_with_hash(&nodes[1], route, &[&nodes[2], &nodes[3], &nodes[5]], ds_dust_limit*1000, payment_hash_2); // not added < dust limit + HTLC tx fee
-       // 4th HTLC:
-       let (_, payment_hash_3) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 1000000);
-       // 5th HTLC:
-       let (_, payment_hash_4) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 1000000);
-       let route = nodes[1].router.get_route(&nodes[5].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
-       // 6th HTLC:
-       send_along_route_with_hash(&nodes[1], route.clone(), &[&nodes[2], &nodes[3], &nodes[5]], 1000000, payment_hash_3);
-       // 7th HTLC:
-       send_along_route_with_hash(&nodes[1], route, &[&nodes[2], &nodes[3], &nodes[5]], 1000000, payment_hash_4);
-
-       // 8th HTLC:
-       let (_, payment_hash_5) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 1000000);
-       // 9th HTLC:
-       let route = nodes[1].router.get_route(&nodes[5].node.get_our_node_id(), None, &Vec::new(), ds_dust_limit*1000, TEST_FINAL_CLTV).unwrap();
-       send_along_route_with_hash(&nodes[1], route, &[&nodes[2], &nodes[3], &nodes[5]], ds_dust_limit*1000, payment_hash_5); // not added < dust limit + HTLC tx fee
-
-       // 10th HTLC:
-       let (_, payment_hash_6) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], ds_dust_limit*1000); // not added < dust limit + HTLC tx fee
-       // 11th HTLC:
-       let route = nodes[1].router.get_route(&nodes[5].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
-       send_along_route_with_hash(&nodes[1], route, &[&nodes[2], &nodes[3], &nodes[5]], 1000000, payment_hash_6);
-
-       // Double-check that six of the new HTLC were added
-       // We now have six HTLCs pending over the dust limit and six HTLCs under the dust limit (ie,
-       // with to_local and to_remote outputs, 8 outputs and 6 HTLCs not included).
-       assert_eq!(nodes[3].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.len(), 1);
-       assert_eq!(nodes[3].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn[0].output.len(), 8);
-
-       // Now fail back three of the over-dust-limit and three of the under-dust-limit payments in one go.
-       // Fail 0th below-dust, 4th above-dust, 8th above-dust, 10th below-dust HTLCs
-       assert!(nodes[4].node.fail_htlc_backwards(&payment_hash_1));
-       assert!(nodes[4].node.fail_htlc_backwards(&payment_hash_3));
-       assert!(nodes[4].node.fail_htlc_backwards(&payment_hash_5));
-       assert!(nodes[4].node.fail_htlc_backwards(&payment_hash_6));
-       check_added_monitors!(nodes[4], 0);
-       expect_pending_htlcs_forwardable!(nodes[4]);
-       check_added_monitors!(nodes[4], 1);
-
-       let four_removes = get_htlc_update_msgs!(nodes[4], nodes[3].node.get_our_node_id());
-       nodes[3].node.handle_update_fail_htlc(&nodes[4].node.get_our_node_id(), &four_removes.update_fail_htlcs[0]).unwrap();
-       nodes[3].node.handle_update_fail_htlc(&nodes[4].node.get_our_node_id(), &four_removes.update_fail_htlcs[1]).unwrap();
-       nodes[3].node.handle_update_fail_htlc(&nodes[4].node.get_our_node_id(), &four_removes.update_fail_htlcs[2]).unwrap();
-       nodes[3].node.handle_update_fail_htlc(&nodes[4].node.get_our_node_id(), &four_removes.update_fail_htlcs[3]).unwrap();
-       commitment_signed_dance!(nodes[3], nodes[4], four_removes.commitment_signed, false);
-
-       // Fail 3rd below-dust and 7th above-dust HTLCs
-       assert!(nodes[5].node.fail_htlc_backwards(&payment_hash_2));
-       assert!(nodes[5].node.fail_htlc_backwards(&payment_hash_4));
-       check_added_monitors!(nodes[5], 0);
-       expect_pending_htlcs_forwardable!(nodes[5]);
-       check_added_monitors!(nodes[5], 1);
-
-       let two_removes = get_htlc_update_msgs!(nodes[5], nodes[3].node.get_our_node_id());
-       nodes[3].node.handle_update_fail_htlc(&nodes[5].node.get_our_node_id(), &two_removes.update_fail_htlcs[0]).unwrap();
-       nodes[3].node.handle_update_fail_htlc(&nodes[5].node.get_our_node_id(), &two_removes.update_fail_htlcs[1]).unwrap();
-       commitment_signed_dance!(nodes[3], nodes[5], two_removes.commitment_signed, false);
-
-       let ds_prev_commitment_tx = nodes[3].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone();
-
-       expect_pending_htlcs_forwardable!(nodes[3]);
-       check_added_monitors!(nodes[3], 1);
-       let six_removes = get_htlc_update_msgs!(nodes[3], nodes[2].node.get_our_node_id());
-       nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[0]).unwrap();
-       nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[1]).unwrap();
-       nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[2]).unwrap();
-       nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[3]).unwrap();
-       nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[4]).unwrap();
-       nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[5]).unwrap();
-       if deliver_last_raa {
-               commitment_signed_dance!(nodes[2], nodes[3], six_removes.commitment_signed, false);
-       } else {
-               let _cs_last_raa = commitment_signed_dance!(nodes[2], nodes[3], six_removes.commitment_signed, false, true, false, true);
-       }
-
-       // D's latest commitment transaction now contains 1st + 2nd + 9th HTLCs (implicitly, they're
-       // below the dust limit) and the 5th + 6th + 11th HTLCs. It has failed back the 0th, 3rd, 4th,
-       // 7th, 8th, and 10th, but as we haven't yet delivered the final RAA to C, the fails haven't
-       // propagated back to A/B yet (and D has two unrevoked commitment transactions).
-       //
-       // We now broadcast the latest commitment transaction, which *should* result in failures for
-       // the 0th, 1st, 2nd, 3rd, 4th, 7th, 8th, 9th, and 10th HTLCs, ie all the below-dust HTLCs and
-       // the non-broadcast above-dust HTLCs.
-       //
-       // Alternatively, we may broadcast the previous commitment transaction, which should only
-       // result in failures for the below-dust HTLCs, ie the 0th, 1st, 2nd, 3rd, 9th, and 10th HTLCs.
-       let ds_last_commitment_tx = nodes[3].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone();
-
-       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-       if announce_latest {
-               nodes[2].chain_monitor.block_connected_checked(&header, 1, &[&ds_last_commitment_tx[0]], &[1; 1]);
-       } else {
-               nodes[2].chain_monitor.block_connected_checked(&header, 1, &[&ds_prev_commitment_tx[0]], &[1; 1]);
-       }
-       connect_blocks(&nodes[2].chain_monitor, ANTI_REORG_DELAY - 1, 1, true,  header.bitcoin_hash());
-       check_closed_broadcast!(nodes[2]);
-       expect_pending_htlcs_forwardable!(nodes[2]);
-       check_added_monitors!(nodes[2], 2);
-
-       let cs_msgs = nodes[2].node.get_and_clear_pending_msg_events();
-       assert_eq!(cs_msgs.len(), 2);
-       let mut a_done = false;
-       for msg in cs_msgs {
-               match msg {
-                       MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
-                               // Both under-dust HTLCs and the one above-dust HTLC that we had already failed
-                               // should be failed-backwards here.
-                               let target = if *node_id == nodes[0].node.get_our_node_id() {
-                                       // If announce_latest, expect 0th, 1st, 4th, 8th, 10th HTLCs, else only 0th, 1st, 10th below-dust HTLCs
-                                       for htlc in &updates.update_fail_htlcs {
-                                               assert!(htlc.htlc_id == 1 || htlc.htlc_id == 2 || htlc.htlc_id == 6 || if announce_latest { htlc.htlc_id == 3 || htlc.htlc_id == 5 } else { false });
-                                       }
-                                       assert_eq!(updates.update_fail_htlcs.len(), if announce_latest { 5 } else { 3 });
-                                       assert!(!a_done);
-                                       a_done = true;
-                                       &nodes[0]
-                               } else {
-                                       // If announce_latest, expect 2nd, 3rd, 7th, 9th HTLCs, else only 2nd, 3rd, 9th below-dust HTLCs
-                                       for htlc in &updates.update_fail_htlcs {
-                                               assert!(htlc.htlc_id == 1 || htlc.htlc_id == 2 || htlc.htlc_id == 5 || if announce_latest { htlc.htlc_id == 4 } else { false });
-                                       }
-                                       assert_eq!(*node_id, nodes[1].node.get_our_node_id());
-                                       assert_eq!(updates.update_fail_htlcs.len(), if announce_latest { 4 } else { 3 });
-                                       &nodes[1]
-                               };
-                               target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]).unwrap();
-                               target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[1]).unwrap();
-                               target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[2]).unwrap();
-                               if announce_latest {
-                                       target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[3]).unwrap();
-                                       if *node_id == nodes[0].node.get_our_node_id() {
-                                               target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[4]).unwrap();
-                                       }
-                               }
-                               commitment_signed_dance!(target, nodes[2], updates.commitment_signed, false, true);
-                       },
-                       _ => panic!("Unexpected event"),
-               }
-       }
-
-       let as_events = nodes[0].node.get_and_clear_pending_events();
-       assert_eq!(as_events.len(), if announce_latest { 5 } else { 3 });
-       let mut as_failds = HashSet::new();
-       for event in as_events.iter() {
-               if let &Event::PaymentFailed { ref payment_hash, ref rejected_by_dest, .. } = event {
-                       assert!(as_failds.insert(*payment_hash));
-                       if *payment_hash != payment_hash_2 {
-                               assert_eq!(*rejected_by_dest, deliver_last_raa);
-                       } else {
-                               assert!(!rejected_by_dest);
-                       }
-               } else { panic!("Unexpected event"); }
-       }
-       assert!(as_failds.contains(&payment_hash_1));
-       assert!(as_failds.contains(&payment_hash_2));
-       if announce_latest {
-               assert!(as_failds.contains(&payment_hash_3));
-               assert!(as_failds.contains(&payment_hash_5));
-       }
-       assert!(as_failds.contains(&payment_hash_6));
-
-       let bs_events = nodes[1].node.get_and_clear_pending_events();
-       assert_eq!(bs_events.len(), if announce_latest { 4 } else { 3 });
-       let mut bs_failds = HashSet::new();
-       for event in bs_events.iter() {
-               if let &Event::PaymentFailed { ref payment_hash, ref rejected_by_dest, .. } = event {
-                       assert!(bs_failds.insert(*payment_hash));
-                       if *payment_hash != payment_hash_1 && *payment_hash != payment_hash_5 {
-                               assert_eq!(*rejected_by_dest, deliver_last_raa);
-                       } else {
-                               assert!(!rejected_by_dest);
-                       }
-               } else { panic!("Unexpected event"); }
-       }
-       assert!(bs_failds.contains(&payment_hash_1));
-       assert!(bs_failds.contains(&payment_hash_2));
-       if announce_latest {
-               assert!(bs_failds.contains(&payment_hash_4));
-       }
-       assert!(bs_failds.contains(&payment_hash_5));
-
-       // For each HTLC which was not failed-back by normal process (ie deliver_last_raa), we should
-       // get a PaymentFailureNetworkUpdate. A should have gotten 4 HTLCs which were failed-back due
-       // to unknown-preimage-etc, B should have gotten 2. Thus, in the
-       // announce_latest && deliver_last_raa case, we should have 5-4=1 and 4-2=2
-       // PaymentFailureNetworkUpdates.
-       let as_msg_events = nodes[0].node.get_and_clear_pending_msg_events();
-       assert_eq!(as_msg_events.len(), if deliver_last_raa { 1 } else if !announce_latest { 3 } else { 5 });
-       let bs_msg_events = nodes[1].node.get_and_clear_pending_msg_events();
-       assert_eq!(bs_msg_events.len(), if deliver_last_raa { 2 } else if !announce_latest { 3 } else { 4 });
-       for event in as_msg_events.iter().chain(bs_msg_events.iter()) {
-               match event {
-                       &MessageSendEvent::PaymentFailureNetworkUpdate { .. } => {},
-                       _ => panic!("Unexpected event"),
-               }
-       }
-}
-
-#[test]
-fn test_fail_backwards_latest_remote_announce_a() {
-       do_test_fail_backwards_unrevoked_remote_announce(false, true);
-}
-
-#[test]
-fn test_fail_backwards_latest_remote_announce_b() {
-       do_test_fail_backwards_unrevoked_remote_announce(true, true);
-}
-
-#[test]
-fn test_fail_backwards_previous_remote_announce() {
-       do_test_fail_backwards_unrevoked_remote_announce(false, false);
-       // Note that true, true doesn't make sense as it implies we announce a revoked state, which is
-       // tested for in test_commitment_revoked_fail_backward_exhaustive()
-}
-
-#[test]
-fn test_dynamic_spendable_outputs_local_htlc_timeout_tx() {
-       let nodes = create_network(2, &[None, None]);
-
-       // Create some initial channels
-       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       route_payment(&nodes[0], &vec!(&nodes[1])[..], 9000000).0;
-       let local_txn = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
-       assert_eq!(local_txn[0].input.len(), 1);
-       check_spends!(local_txn[0], chan_1.3.clone());
-
-       // Timeout HTLC on A's chain and so it can generate a HTLC-Timeout tx
-       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-       nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![local_txn[0].clone()] }, 200);
-       check_closed_broadcast!(nodes[0]);
-
-       let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
-       assert_eq!(node_txn[0].input.len(), 1);
-       assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
-       check_spends!(node_txn[0], local_txn[0].clone());
-
-       // Verify that A is able to spend its own HTLC-Timeout tx thanks to spendable output event given back by its ChannelMonitor
-       let spend_txn = check_spendable_outputs!(nodes[0], 1);
-       assert_eq!(spend_txn.len(), 8);
-       assert_eq!(spend_txn[0], spend_txn[2]);
-       assert_eq!(spend_txn[0], spend_txn[4]);
-       assert_eq!(spend_txn[0], spend_txn[6]);
-       assert_eq!(spend_txn[1], spend_txn[3]);
-       assert_eq!(spend_txn[1], spend_txn[5]);
-       assert_eq!(spend_txn[1], spend_txn[7]);
-       check_spends!(spend_txn[0], local_txn[0].clone());
-       check_spends!(spend_txn[1], node_txn[0].clone());
-}
-
-#[test]
-fn test_static_output_closing_tx() {
-       let nodes = create_network(2, &[None, None]);
-
-       let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
-       let closing_tx = close_channel(&nodes[0], &nodes[1], &chan.2, chan.3, true).2;
-
-       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-       nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![closing_tx.clone()] }, 1);
-       let spend_txn = check_spendable_outputs!(nodes[0], 2);
-       assert_eq!(spend_txn.len(), 1);
-       check_spends!(spend_txn[0], closing_tx.clone());
-
-       nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![closing_tx.clone()] }, 1);
-       let spend_txn = check_spendable_outputs!(nodes[1], 2);
-       assert_eq!(spend_txn.len(), 1);
-       check_spends!(spend_txn[0], closing_tx);
-}
-
-fn do_htlc_claim_local_commitment_only(use_dust: bool) {
-       let nodes = create_network(2, &[None, None]);
-       let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       let (our_payment_preimage, _) = route_payment(&nodes[0], &[&nodes[1]], if use_dust { 50000 } else { 3000000 });
-
-       // Claim the payment, but don't deliver A's commitment_signed, resulting in the HTLC only being
-       // present in B's local commitment transaction, but none of A's commitment transactions.
-       assert!(nodes[1].node.claim_funds(our_payment_preimage));
-       check_added_monitors!(nodes[1], 1);
-
-       let bs_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-       nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fulfill_htlcs[0]).unwrap();
-       let events = nodes[0].node.get_and_clear_pending_events();
-       assert_eq!(events.len(), 1);
-       match events[0] {
-               Event::PaymentSent { payment_preimage } => {
-                       assert_eq!(payment_preimage, our_payment_preimage);
-               },
-               _ => panic!("Unexpected event"),
-       }
-
-       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_updates.commitment_signed).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       let as_updates = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
-       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_updates.0).unwrap();
-       check_added_monitors!(nodes[1], 1);
-
-       let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-       for i in 1..TEST_FINAL_CLTV - CLTV_CLAIM_BUFFER + CHAN_CONFIRM_DEPTH + 1 {
-               nodes[1].chain_monitor.block_connected_checked(&header, i, &Vec::new(), &Vec::new());
-               header.prev_blockhash = header.bitcoin_hash();
-       }
-       test_txn_broadcast(&nodes[1], &chan, None, if use_dust { HTLCType::NONE } else { HTLCType::SUCCESS });
-       check_closed_broadcast!(nodes[1]);
-}
-
-fn do_htlc_claim_current_remote_commitment_only(use_dust: bool) {
-       let mut nodes = create_network(2, &[None, None]);
-       let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), if use_dust { 50000 } else { 3000000 }, TEST_FINAL_CLTV).unwrap();
-       let (_, payment_hash) = get_payment_preimage_hash!(nodes[0]);
-       nodes[0].node.send_payment(route, payment_hash).unwrap();
-       check_added_monitors!(nodes[0], 1);
-
-       let _as_update = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
-
-       // As far as A is concerned, the HTLC is now present only in the latest remote commitment
-       // transaction, however it is not in A's latest local commitment, so we can just broadcast that
-       // to "time out" the HTLC.
-
-       let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-       for i in 1..TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + CHAN_CONFIRM_DEPTH + 1 {
-               nodes[0].chain_monitor.block_connected_checked(&header, i, &Vec::new(), &Vec::new());
-               header.prev_blockhash = header.bitcoin_hash();
-       }
-       test_txn_broadcast(&nodes[0], &chan, None, HTLCType::NONE);
-       check_closed_broadcast!(nodes[0]);
-}
-
-fn do_htlc_claim_previous_remote_commitment_only(use_dust: bool, check_revoke_no_close: bool) {
-       let nodes = create_network(3, &[None, None, None]);
-       let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       // Fail the payment, but don't deliver A's final RAA, resulting in the HTLC only being present
-       // in B's previous (unrevoked) commitment transaction, but none of A's commitment transactions.
-       // Also optionally test that we *don't* fail the channel in case the commitment transaction was
-       // actually revoked.
-       let htlc_value = if use_dust { 50000 } else { 3000000 };
-       let (_, our_payment_hash) = route_payment(&nodes[0], &[&nodes[1]], htlc_value);
-       assert!(nodes[1].node.fail_htlc_backwards(&our_payment_hash));
-       expect_pending_htlcs_forwardable!(nodes[1]);
-       check_added_monitors!(nodes[1], 1);
-
-       let bs_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-       nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fail_htlcs[0]).unwrap();
-       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_updates.commitment_signed).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       let as_updates = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
-       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_updates.0).unwrap();
-       check_added_monitors!(nodes[1], 1);
-       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_updates.1).unwrap();
-       check_added_monitors!(nodes[1], 1);
-       let bs_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
-
-       if check_revoke_no_close {
-               nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
-               check_added_monitors!(nodes[0], 1);
-       }
-
-       let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-       for i in 1..TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + CHAN_CONFIRM_DEPTH + 1 {
-               nodes[0].chain_monitor.block_connected_checked(&header, i, &Vec::new(), &Vec::new());
-               header.prev_blockhash = header.bitcoin_hash();
-       }
-       if !check_revoke_no_close {
-               test_txn_broadcast(&nodes[0], &chan, None, HTLCType::NONE);
-               check_closed_broadcast!(nodes[0]);
-       } else {
-               let events = nodes[0].node.get_and_clear_pending_events();
-               assert_eq!(events.len(), 1);
-               match events[0] {
-                       Event::PaymentFailed { payment_hash, rejected_by_dest, .. } => {
-                               assert_eq!(payment_hash, our_payment_hash);
-                               assert!(rejected_by_dest);
-                       },
-                       _ => panic!("Unexpected event"),
-               }
-       }
-}
-
-// Test that we close channels on-chain when broadcastable HTLCs reach their timeout window.
-// There are only a few cases to test here:
-//  * its not really normative behavior, but we test that below-dust HTLCs "included" in
-//    broadcastable commitment transactions result in channel closure,
-//  * its included in an unrevoked-but-previous remote commitment transaction,
-//  * its included in the latest remote or local commitment transactions.
-// We test each of the three possible commitment transactions individually and use both dust and
-// non-dust HTLCs.
-// Note that we don't bother testing both outbound and inbound HTLC failures for each case, and we
-// assume they are handled the same across all six cases, as both outbound and inbound failures are
-// tested for at least one of the cases in other tests.
-#[test]
-fn htlc_claim_single_commitment_only_a() {
-       do_htlc_claim_local_commitment_only(true);
-       do_htlc_claim_local_commitment_only(false);
-
-       do_htlc_claim_current_remote_commitment_only(true);
-       do_htlc_claim_current_remote_commitment_only(false);
-}
-
-#[test]
-fn htlc_claim_single_commitment_only_b() {
-       do_htlc_claim_previous_remote_commitment_only(true, false);
-       do_htlc_claim_previous_remote_commitment_only(false, false);
-       do_htlc_claim_previous_remote_commitment_only(true, true);
-       do_htlc_claim_previous_remote_commitment_only(false, true);
-}
-
-fn run_onion_failure_test<F1,F2>(_name: &str, test_case: u8, nodes: &Vec<Node>, route: &Route, payment_hash: &PaymentHash, callback_msg: F1, callback_node: F2, expected_retryable: bool, expected_error_code: Option<u16>, expected_channel_update: Option<HTLCFailChannelUpdate>)
-       where F1: for <'a> FnMut(&'a mut msgs::UpdateAddHTLC),
-                               F2: FnMut(),
-{
-       run_onion_failure_test_with_fail_intercept(_name, test_case, nodes, route, payment_hash, callback_msg, |_|{}, callback_node, expected_retryable, expected_error_code, expected_channel_update);
-}
-
-// test_case
-// 0: node1 fails backward
-// 1: final node fails backward
-// 2: payment completed but the user rejects the payment
-// 3: final node fails backward (but tamper onion payloads from node0)
-// 100: trigger error in the intermediate node and tamper returning fail_htlc
-// 200: trigger error in the final node and tamper returning fail_htlc
-fn run_onion_failure_test_with_fail_intercept<F1,F2,F3>(_name: &str, test_case: u8, nodes: &Vec<Node>, route: &Route, payment_hash: &PaymentHash, mut callback_msg: F1, mut callback_fail: F2, mut callback_node: F3, expected_retryable: bool, expected_error_code: Option<u16>, expected_channel_update: Option<HTLCFailChannelUpdate>)
-       where F1: for <'a> FnMut(&'a mut msgs::UpdateAddHTLC),
-                               F2: for <'a> FnMut(&'a mut msgs::UpdateFailHTLC),
-                               F3: FnMut(),
-{
-
-       // reset block height
-       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-       for ix in 0..nodes.len() {
-               nodes[ix].chain_monitor.block_connected_checked(&header, 1, &Vec::new()[..], &[0; 0]);
-       }
-
-       macro_rules! expect_event {
-               ($node: expr, $event_type: path) => {{
-                       let events = $node.node.get_and_clear_pending_events();
-                       assert_eq!(events.len(), 1);
-                       match events[0] {
-                               $event_type { .. } => {},
-                               _ => panic!("Unexpected event"),
-                       }
-               }}
-       }
-
-       macro_rules! expect_htlc_forward {
-               ($node: expr) => {{
-                       expect_event!($node, Event::PendingHTLCsForwardable);
-                       $node.node.process_pending_htlc_forwards();
-               }}
-       }
-
-       // 0 ~~> 2 send payment
-       nodes[0].node.send_payment(route.clone(), payment_hash.clone()).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       let update_0 = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
-       // temper update_add (0 => 1)
-       let mut update_add_0 = update_0.update_add_htlcs[0].clone();
-       if test_case == 0 || test_case == 3 || test_case == 100 {
-               callback_msg(&mut update_add_0);
-               callback_node();
-       }
-       // 0 => 1 update_add & CS
-       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &update_add_0).unwrap();
-       commitment_signed_dance!(nodes[1], nodes[0], &update_0.commitment_signed, false, true);
-
-       let update_1_0 = match test_case {
-               0|100 => { // intermediate node failure; fail backward to 0
-                       let update_1_0 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-                       assert!(update_1_0.update_fail_htlcs.len()+update_1_0.update_fail_malformed_htlcs.len()==1 && (update_1_0.update_fail_htlcs.len()==1 || update_1_0.update_fail_malformed_htlcs.len()==1));
-                       update_1_0
-               },
-               1|2|3|200 => { // final node failure; forwarding to 2
-                       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-                       // forwarding on 1
-                       if test_case != 200 {
-                               callback_node();
-                       }
-                       expect_htlc_forward!(&nodes[1]);
-
-                       let update_1 = get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
-                       check_added_monitors!(&nodes[1], 1);
-                       assert_eq!(update_1.update_add_htlcs.len(), 1);
-                       // tamper update_add (1 => 2)
-                       let mut update_add_1 = update_1.update_add_htlcs[0].clone();
-                       if test_case != 3 && test_case != 200 {
-                               callback_msg(&mut update_add_1);
-                       }
-
-                       // 1 => 2
-                       nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &update_add_1).unwrap();
-                       commitment_signed_dance!(nodes[2], nodes[1], update_1.commitment_signed, false, true);
-
-                       if test_case == 2 || test_case == 200 {
-                               expect_htlc_forward!(&nodes[2]);
-                               expect_event!(&nodes[2], Event::PaymentReceived);
-                               callback_node();
-                               expect_pending_htlcs_forwardable!(nodes[2]);
-                       }
-
-                       let update_2_1 = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
-                       if test_case == 2 || test_case == 200 {
-                               check_added_monitors!(&nodes[2], 1);
-                       }
-                       assert!(update_2_1.update_fail_htlcs.len() == 1);
-
-                       let mut fail_msg = update_2_1.update_fail_htlcs[0].clone();
-                       if test_case == 200 {
-                               callback_fail(&mut fail_msg);
-                       }
-
-                       // 2 => 1
-                       nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &fail_msg).unwrap();
-                       commitment_signed_dance!(nodes[1], nodes[2], update_2_1.commitment_signed, true);
-
-                       // backward fail on 1
-                       let update_1_0 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-                       assert!(update_1_0.update_fail_htlcs.len() == 1);
-                       update_1_0
-               },
-               _ => unreachable!(),
-       };
-
-       // 1 => 0 commitment_signed_dance
-       if update_1_0.update_fail_htlcs.len() > 0 {
-               let mut fail_msg = update_1_0.update_fail_htlcs[0].clone();
-               if test_case == 100 {
-                       callback_fail(&mut fail_msg);
-               }
-               nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_msg).unwrap();
-       } else {
-               nodes[0].node.handle_update_fail_malformed_htlc(&nodes[1].node.get_our_node_id(), &update_1_0.update_fail_malformed_htlcs[0]).unwrap();
-       };
-
-       commitment_signed_dance!(nodes[0], nodes[1], update_1_0.commitment_signed, false, true);
-
-       let events = nodes[0].node.get_and_clear_pending_events();
-       assert_eq!(events.len(), 1);
-       if let &Event::PaymentFailed { payment_hash:_, ref rejected_by_dest, ref error_code } = &events[0] {
-               assert_eq!(*rejected_by_dest, !expected_retryable);
-               assert_eq!(*error_code, expected_error_code);
-       } else {
-               panic!("Uexpected event");
-       }
-
-       let events = nodes[0].node.get_and_clear_pending_msg_events();
-       if expected_channel_update.is_some() {
-               assert_eq!(events.len(), 1);
-               match events[0] {
-                       MessageSendEvent::PaymentFailureNetworkUpdate { ref update } => {
-                               match update {
-                                       &HTLCFailChannelUpdate::ChannelUpdateMessage { .. } => {
-                                               if let HTLCFailChannelUpdate::ChannelUpdateMessage { .. } = expected_channel_update.unwrap() {} else {
-                                                       panic!("channel_update not found!");
-                                               }
-                                       },
-                                       &HTLCFailChannelUpdate::ChannelClosed { ref short_channel_id, ref is_permanent } => {
-                                               if let HTLCFailChannelUpdate::ChannelClosed { short_channel_id: ref expected_short_channel_id, is_permanent: ref expected_is_permanent } = expected_channel_update.unwrap() {
-                                                       assert!(*short_channel_id == *expected_short_channel_id);
-                                                       assert!(*is_permanent == *expected_is_permanent);
-                                               } else {
-                                                       panic!("Unexpected message event");
-                                               }
-                                       },
-                                       &HTLCFailChannelUpdate::NodeFailure { ref node_id, ref is_permanent } => {
-                                               if let HTLCFailChannelUpdate::NodeFailure { node_id: ref expected_node_id, is_permanent: ref expected_is_permanent } = expected_channel_update.unwrap() {
-                                                       assert!(*node_id == *expected_node_id);
-                                                       assert!(*is_permanent == *expected_is_permanent);
-                                               } else {
-                                                       panic!("Unexpected message event");
-                                               }
-                                       },
-                               }
-                       },
-                       _ => panic!("Unexpected message event"),
-               }
-       } else {
-               assert_eq!(events.len(), 0);
-       }
-}
-
-impl msgs::ChannelUpdate {
-       fn dummy() -> msgs::ChannelUpdate {
-               use secp256k1::ffi::Signature as FFISignature;
-               use secp256k1::Signature;
-               msgs::ChannelUpdate {
-                       signature: Signature::from(FFISignature::new()),
-                       contents: msgs::UnsignedChannelUpdate {
-                               chain_hash: Sha256dHash::hash(&vec![0u8][..]),
-                               short_channel_id: 0,
-                               timestamp: 0,
-                               flags: 0,
-                               cltv_expiry_delta: 0,
-                               htlc_minimum_msat: 0,
-                               fee_base_msat: 0,
-                               fee_proportional_millionths: 0,
-                               excess_data: vec![],
-                       }
-               }
-       }
-}
-
-#[test]
-fn test_onion_failure() {
-       use ln::msgs::ChannelUpdate;
-       use ln::channelmanager::CLTV_FAR_FAR_AWAY;
-       use secp256k1;
-
-       const BADONION: u16 = 0x8000;
-       const PERM: u16 = 0x4000;
-       const NODE: u16 = 0x2000;
-       const UPDATE: u16 = 0x1000;
-
-       let mut nodes = create_network(3, &[None, None, None]);
-       for node in nodes.iter() {
-               *node.keys_manager.override_session_priv.lock().unwrap() = Some(SecretKey::from_slice(&[3; 32]).unwrap());
-       }
-       let channels = [create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new()), create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new())];
-       let (_, payment_hash) = get_payment_preimage_hash!(nodes[0]);
-       let route = nodes[0].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 40000, TEST_FINAL_CLTV).unwrap();
-       // positve case
-       send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 40000);
-
-       // intermediate node failure
-       run_onion_failure_test("invalid_realm", 0, &nodes, &route, &payment_hash, |msg| {
-               let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
-               let cur_height = nodes[0].node.latest_block_height.load(Ordering::Acquire) as u32 + 1;
-               let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
-               let (mut onion_payloads, _htlc_msat, _htlc_cltv) = onion_utils::build_onion_payloads(&route, cur_height).unwrap();
-               onion_payloads[0].realm = 3;
-               msg.onion_routing_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, &payment_hash);
-       }, ||{}, true, Some(PERM|1), Some(msgs::HTLCFailChannelUpdate::ChannelClosed{short_channel_id: channels[1].0.contents.short_channel_id, is_permanent: true}));//XXX incremented channels idx here
-
-       // final node failure
-       run_onion_failure_test("invalid_realm", 3, &nodes, &route, &payment_hash, |msg| {
-               let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
-               let cur_height = nodes[0].node.latest_block_height.load(Ordering::Acquire) as u32 + 1;
-               let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
-               let (mut onion_payloads, _htlc_msat, _htlc_cltv) = onion_utils::build_onion_payloads(&route, cur_height).unwrap();
-               onion_payloads[1].realm = 3;
-               msg.onion_routing_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, &payment_hash);
-       }, ||{}, false, Some(PERM|1), Some(msgs::HTLCFailChannelUpdate::ChannelClosed{short_channel_id: channels[1].0.contents.short_channel_id, is_permanent: true}));
-
-       // the following three with run_onion_failure_test_with_fail_intercept() test only the origin node
-       // receiving simulated fail messages
-       // intermediate node failure
-       run_onion_failure_test_with_fail_intercept("temporary_node_failure", 100, &nodes, &route, &payment_hash, |msg| {
-               // trigger error
-               msg.amount_msat -= 1;
-       }, |msg| {
-               // and tamper returning error message
-               let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
-               let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
-               msg.reason = onion_utils::build_first_hop_failure_packet(&onion_keys[0].shared_secret[..], NODE|2, &[0;0]);
-       }, ||{}, true, Some(NODE|2), Some(msgs::HTLCFailChannelUpdate::NodeFailure{node_id: route.hops[0].pubkey, is_permanent: false}));
-
-       // final node failure
-       run_onion_failure_test_with_fail_intercept("temporary_node_failure", 200, &nodes, &route, &payment_hash, |_msg| {}, |msg| {
-               // and tamper returning error message
-               let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
-               let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
-               msg.reason = onion_utils::build_first_hop_failure_packet(&onion_keys[1].shared_secret[..], NODE|2, &[0;0]);
-       }, ||{
-               nodes[2].node.fail_htlc_backwards(&payment_hash);
-       }, true, Some(NODE|2), Some(msgs::HTLCFailChannelUpdate::NodeFailure{node_id: route.hops[1].pubkey, is_permanent: false}));
-
-       // intermediate node failure
-       run_onion_failure_test_with_fail_intercept("permanent_node_failure", 100, &nodes, &route, &payment_hash, |msg| {
-               msg.amount_msat -= 1;
-       }, |msg| {
-               let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
-               let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
-               msg.reason = onion_utils::build_first_hop_failure_packet(&onion_keys[0].shared_secret[..], PERM|NODE|2, &[0;0]);
-       }, ||{}, true, Some(PERM|NODE|2), Some(msgs::HTLCFailChannelUpdate::NodeFailure{node_id: route.hops[0].pubkey, is_permanent: true}));
-
-       // final node failure
-       run_onion_failure_test_with_fail_intercept("permanent_node_failure", 200, &nodes, &route, &payment_hash, |_msg| {}, |msg| {
-               let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
-               let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
-               msg.reason = onion_utils::build_first_hop_failure_packet(&onion_keys[1].shared_secret[..], PERM|NODE|2, &[0;0]);
-       }, ||{
-               nodes[2].node.fail_htlc_backwards(&payment_hash);
-       }, false, Some(PERM|NODE|2), Some(msgs::HTLCFailChannelUpdate::NodeFailure{node_id: route.hops[1].pubkey, is_permanent: true}));
-
-       // intermediate node failure
-       run_onion_failure_test_with_fail_intercept("required_node_feature_missing", 100, &nodes, &route, &payment_hash, |msg| {
-               msg.amount_msat -= 1;
-       }, |msg| {
-               let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
-               let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
-               msg.reason = onion_utils::build_first_hop_failure_packet(&onion_keys[0].shared_secret[..], PERM|NODE|3, &[0;0]);
-       }, ||{
-               nodes[2].node.fail_htlc_backwards(&payment_hash);
-       }, true, Some(PERM|NODE|3), Some(msgs::HTLCFailChannelUpdate::NodeFailure{node_id: route.hops[0].pubkey, is_permanent: true}));
-
-       // final node failure
-       run_onion_failure_test_with_fail_intercept("required_node_feature_missing", 200, &nodes, &route, &payment_hash, |_msg| {}, |msg| {
-               let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
-               let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
-               msg.reason = onion_utils::build_first_hop_failure_packet(&onion_keys[1].shared_secret[..], PERM|NODE|3, &[0;0]);
-       }, ||{
-               nodes[2].node.fail_htlc_backwards(&payment_hash);
-       }, false, Some(PERM|NODE|3), Some(msgs::HTLCFailChannelUpdate::NodeFailure{node_id: route.hops[1].pubkey, is_permanent: true}));
-
-       run_onion_failure_test("invalid_onion_version", 0, &nodes, &route, &payment_hash, |msg| { msg.onion_routing_packet.version = 1; }, ||{}, true,
-               Some(BADONION|PERM|4), None);
-
-       run_onion_failure_test("invalid_onion_hmac", 0, &nodes, &route, &payment_hash, |msg| { msg.onion_routing_packet.hmac = [3; 32]; }, ||{}, true,
-               Some(BADONION|PERM|5), None);
-
-       run_onion_failure_test("invalid_onion_key", 0, &nodes, &route, &payment_hash, |msg| { msg.onion_routing_packet.public_key = Err(secp256k1::Error::InvalidPublicKey);}, ||{}, true,
-               Some(BADONION|PERM|6), None);
-
-       run_onion_failure_test_with_fail_intercept("temporary_channel_failure", 100, &nodes, &route, &payment_hash, |msg| {
-               msg.amount_msat -= 1;
-       }, |msg| {
-               let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
-               let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
-               msg.reason = onion_utils::build_first_hop_failure_packet(&onion_keys[0].shared_secret[..], UPDATE|7, &ChannelUpdate::dummy().encode_with_len()[..]);
-       }, ||{}, true, Some(UPDATE|7), Some(msgs::HTLCFailChannelUpdate::ChannelUpdateMessage{msg: ChannelUpdate::dummy()}));
-
-       run_onion_failure_test_with_fail_intercept("permanent_channel_failure", 100, &nodes, &route, &payment_hash, |msg| {
-               msg.amount_msat -= 1;
-       }, |msg| {
-               let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
-               let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
-               msg.reason = onion_utils::build_first_hop_failure_packet(&onion_keys[0].shared_secret[..], PERM|8, &[0;0]);
-               // short_channel_id from the processing node
-       }, ||{}, true, Some(PERM|8), Some(msgs::HTLCFailChannelUpdate::ChannelClosed{short_channel_id: channels[1].0.contents.short_channel_id, is_permanent: true}));
-
-       run_onion_failure_test_with_fail_intercept("required_channel_feature_missing", 100, &nodes, &route, &payment_hash, |msg| {
-               msg.amount_msat -= 1;
-       }, |msg| {
-               let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
-               let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
-               msg.reason = onion_utils::build_first_hop_failure_packet(&onion_keys[0].shared_secret[..], PERM|9, &[0;0]);
-               // short_channel_id from the processing node
-       }, ||{}, true, Some(PERM|9), Some(msgs::HTLCFailChannelUpdate::ChannelClosed{short_channel_id: channels[1].0.contents.short_channel_id, is_permanent: true}));
-
-       let mut bogus_route = route.clone();
-       bogus_route.hops[1].short_channel_id -= 1;
-       run_onion_failure_test("unknown_next_peer", 0, &nodes, &bogus_route, &payment_hash, |_| {}, ||{}, true, Some(PERM|10),
-         Some(msgs::HTLCFailChannelUpdate::ChannelClosed{short_channel_id: bogus_route.hops[1].short_channel_id, is_permanent:true}));
-
-       let amt_to_forward = nodes[1].node.channel_state.lock().unwrap().by_id.get(&channels[1].2).unwrap().get_their_htlc_minimum_msat() - 1;
-       let mut bogus_route = route.clone();
-       let route_len = bogus_route.hops.len();
-       bogus_route.hops[route_len-1].fee_msat = amt_to_forward;
-       run_onion_failure_test("amount_below_minimum", 0, &nodes, &bogus_route, &payment_hash, |_| {}, ||{}, true, Some(UPDATE|11), Some(msgs::HTLCFailChannelUpdate::ChannelUpdateMessage{msg: ChannelUpdate::dummy()}));
-
-       //TODO: with new config API, we will be able to generate both valid and
-       //invalid channel_update cases.
-       run_onion_failure_test("fee_insufficient", 0, &nodes, &route, &payment_hash, |msg| {
-               msg.amount_msat -= 1;
-       }, || {}, true, Some(UPDATE|12), Some(msgs::HTLCFailChannelUpdate::ChannelClosed { short_channel_id: channels[0].0.contents.short_channel_id, is_permanent: true}));
-
-       run_onion_failure_test("incorrect_cltv_expiry", 0, &nodes, &route, &payment_hash, |msg| {
-               // need to violate: cltv_expiry - cltv_expiry_delta >= outgoing_cltv_value
-               msg.cltv_expiry -= 1;
-       }, || {}, true, Some(UPDATE|13), Some(msgs::HTLCFailChannelUpdate::ChannelClosed { short_channel_id: channels[0].0.contents.short_channel_id, is_permanent: true}));
-
-       run_onion_failure_test("expiry_too_soon", 0, &nodes, &route, &payment_hash, |msg| {
-               let height = msg.cltv_expiry - CLTV_CLAIM_BUFFER - LATENCY_GRACE_PERIOD_BLOCKS + 1;
-               let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-               nodes[1].chain_monitor.block_connected_checked(&header, height, &Vec::new()[..], &[0; 0]);
-       }, ||{}, true, Some(UPDATE|14), Some(msgs::HTLCFailChannelUpdate::ChannelUpdateMessage{msg: ChannelUpdate::dummy()}));
-
-       run_onion_failure_test("unknown_payment_hash", 2, &nodes, &route, &payment_hash, |_| {}, || {
-               nodes[2].node.fail_htlc_backwards(&payment_hash);
-       }, false, Some(PERM|15), None);
-
-       run_onion_failure_test("final_expiry_too_soon", 1, &nodes, &route, &payment_hash, |msg| {
-               let height = msg.cltv_expiry - CLTV_CLAIM_BUFFER - LATENCY_GRACE_PERIOD_BLOCKS + 1;
-               let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-               nodes[2].chain_monitor.block_connected_checked(&header, height, &Vec::new()[..], &[0; 0]);
-       }, || {}, true, Some(17), None);
-
-       run_onion_failure_test("final_incorrect_cltv_expiry", 1, &nodes, &route, &payment_hash, |_| {}, || {
-               for (_, pending_forwards) in nodes[1].node.channel_state.lock().unwrap().borrow_parts().forward_htlcs.iter_mut() {
-                       for f in pending_forwards.iter_mut() {
-                               match f {
-                                       &mut HTLCForwardInfo::AddHTLC { ref mut forward_info, .. } =>
-                                               forward_info.outgoing_cltv_value += 1,
-                                       _ => {},
-                               }
-                       }
-               }
-       }, true, Some(18), None);
-
-       run_onion_failure_test("final_incorrect_htlc_amount", 1, &nodes, &route, &payment_hash, |_| {}, || {
-               // violate amt_to_forward > msg.amount_msat
-               for (_, pending_forwards) in nodes[1].node.channel_state.lock().unwrap().borrow_parts().forward_htlcs.iter_mut() {
-                       for f in pending_forwards.iter_mut() {
-                               match f {
-                                       &mut HTLCForwardInfo::AddHTLC { ref mut forward_info, .. } =>
-                                               forward_info.amt_to_forward -= 1,
-                                       _ => {},
-                               }
-                       }
-               }
-       }, true, Some(19), None);
-
-       run_onion_failure_test("channel_disabled", 0, &nodes, &route, &payment_hash, |_| {}, || {
-               // disconnect event to the channel between nodes[1] ~ nodes[2]
-               nodes[1].node.peer_disconnected(&nodes[2].node.get_our_node_id(), false);
-               nodes[2].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
-       }, true, Some(UPDATE|20), Some(msgs::HTLCFailChannelUpdate::ChannelUpdateMessage{msg: ChannelUpdate::dummy()}));
-       reconnect_nodes(&nodes[1], &nodes[2], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
-
-       run_onion_failure_test("expiry_too_far", 0, &nodes, &route, &payment_hash, |msg| {
-               let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
-               let mut route = route.clone();
-               let height = 1;
-               route.hops[1].cltv_expiry_delta += CLTV_FAR_FAR_AWAY + route.hops[0].cltv_expiry_delta + 1;
-               let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
-               let (onion_payloads, _, htlc_cltv) = onion_utils::build_onion_payloads(&route, height).unwrap();
-               let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, &payment_hash);
-               msg.cltv_expiry = htlc_cltv;
-               msg.onion_routing_packet = onion_packet;
-       }, ||{}, true, Some(21), None);
-}
-
-#[test]
-#[should_panic]
-fn bolt2_open_channel_sending_node_checks_part1() { //This test needs to be on its own as we are catching a panic
-       let nodes = create_network(2, &[None, None]);
-       //Force duplicate channel ids
-       for node in nodes.iter() {
-               *node.keys_manager.override_channel_id_priv.lock().unwrap() = Some([0; 32]);
-       }
-
-       // BOLT #2 spec: Sending node must ensure temporary_channel_id is unique from any other channel ID with the same peer.
-       let channel_value_satoshis=10000;
-       let push_msat=10001;
-       nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42).unwrap();
-       let node0_to_1_send_open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
-       nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), LocalFeatures::new(), &node0_to_1_send_open_channel).unwrap();
-
-       //Create a second channel with a channel_id collision
-       assert!(nodes[0].node.create_channel(nodes[0].node.get_our_node_id(), channel_value_satoshis, push_msat, 42).is_err());
-}
-
-#[test]
-fn bolt2_open_channel_sending_node_checks_part2() {
-       let nodes = create_network(2, &[None, None]);
-
-       // BOLT #2 spec: Sending node must set funding_satoshis to less than 2^24 satoshis
-       let channel_value_satoshis=2^24;
-       let push_msat=10001;
-       assert!(nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42).is_err());
-
-       // BOLT #2 spec: Sending node must set push_msat to equal or less than 1000 * funding_satoshis
-       let channel_value_satoshis=10000;
-       // Test when push_msat is equal to 1000 * funding_satoshis.
-       let push_msat=1000*channel_value_satoshis+1;
-       assert!(nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42).is_err());
-
-       // BOLT #2 spec: Sending node must set set channel_reserve_satoshis greater than or equal to dust_limit_satoshis
-       let channel_value_satoshis=10000;
-       let push_msat=10001;
-       assert!(nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42).is_ok()); //Create a valid channel
-       let node0_to_1_send_open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
-       assert!(node0_to_1_send_open_channel.channel_reserve_satoshis>=node0_to_1_send_open_channel.dust_limit_satoshis);
-
-       // BOLT #2 spec: Sending node must set undefined bits in channel_flags to 0
-       // Only the least-significant bit of channel_flags is currently defined resulting in channel_flags only having one of two possible states 0 or 1
-       assert!(node0_to_1_send_open_channel.channel_flags<=1);
-
-       // BOLT #2 spec: Sending node should set to_self_delay sufficient to ensure the sender can irreversibly spend a commitment transaction output, in case of misbehaviour by the receiver.
-       assert!(BREAKDOWN_TIMEOUT>0);
-       assert!(node0_to_1_send_open_channel.to_self_delay==BREAKDOWN_TIMEOUT);
-
-       // BOLT #2 spec: Sending node must ensure the chain_hash value identifies the chain it wishes to open the channel within.
-       let chain_hash=genesis_block(Network::Testnet).header.bitcoin_hash();
-       assert_eq!(node0_to_1_send_open_channel.chain_hash,chain_hash);
-
-       // BOLT #2 spec: Sending node must set funding_pubkey, revocation_basepoint, htlc_basepoint, payment_basepoint, and delayed_payment_basepoint to valid DER-encoded, compressed, secp256k1 pubkeys.
-       assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.funding_pubkey.serialize()).is_ok());
-       assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.revocation_basepoint.serialize()).is_ok());
-       assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.htlc_basepoint.serialize()).is_ok());
-       assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.payment_basepoint.serialize()).is_ok());
-       assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.delayed_payment_basepoint.serialize()).is_ok());
-}
-
-// BOLT 2 Requirements for the Sender when constructing and sending an update_add_htlc message.
-// BOLT 2 Requirement: MUST NOT offer amount_msat it cannot pay for in the remote commitment transaction at the current feerate_per_kw (see "Updating Fees") while maintaining its channel reserve.
-//TODO: I don't believe this is explicitly enforced when sending an HTLC but as the Fee aspect of the BOLT specs is in flux leaving this as a TODO.
-
-#[test]
-fn test_update_add_htlc_bolt2_sender_value_below_minimum_msat() {
-       //BOLT2 Requirement: MUST offer amount_msat greater than 0.
-       //BOLT2 Requirement: MUST NOT offer amount_msat below the receiving node's htlc_minimum_msat (same validation check catches both of these)
-       let mut nodes = create_network(2, &[None, None]);
-       let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, LocalFeatures::new(), LocalFeatures::new());
-       let mut route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap();
-       let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
-
-       route.hops[0].fee_msat = 0;
-
-       let err = nodes[0].node.send_payment(route, our_payment_hash);
-
-       if let Err(APIError::ChannelUnavailable{err}) = err {
-               assert_eq!(err, "Cannot send less than their minimum HTLC value");
-       } else {
-               assert!(false);
-       }
-}
-
-#[test]
-fn test_update_add_htlc_bolt2_sender_cltv_expiry_too_high() {
-       //BOLT 2 Requirement: MUST set cltv_expiry less than 500000000.
-       //It is enforced when constructing a route.
-       let mut nodes = create_network(2, &[None, None]);
-       let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 0, LocalFeatures::new(), LocalFeatures::new());
-       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 100000000, 500000001).unwrap();
-       let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
-
-       let err = nodes[0].node.send_payment(route, our_payment_hash);
-
-       if let Err(APIError::RouteError{err}) = err {
-               assert_eq!(err, "Channel CLTV overflowed?!");
-       } else {
-               assert!(false);
-       }
-}
-
-#[test]
-fn test_update_add_htlc_bolt2_sender_exceed_max_htlc_num_and_htlc_id_increment() {
-       //BOLT 2 Requirement: if result would be offering more than the remote's max_accepted_htlcs HTLCs, in the remote commitment transaction: MUST NOT add an HTLC.
-       //BOLT 2 Requirement: for the first HTLC it offers MUST set id to 0.
-       //BOLT 2 Requirement: MUST increase the value of id by 1 for each successive offer.
-       let mut nodes = create_network(2, &[None, None]);
-       let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 0, LocalFeatures::new(), LocalFeatures::new());
-       let max_accepted_htlcs = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().their_max_accepted_htlcs as u64;
-
-       for i in 0..max_accepted_htlcs {
-               let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap();
-               let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
-               let payment_event = {
-                       nodes[0].node.send_payment(route, our_payment_hash).unwrap();
-                       check_added_monitors!(nodes[0], 1);
-
-                       let mut events = nodes[0].node.get_and_clear_pending_msg_events();
-                       assert_eq!(events.len(), 1);
-                       if let MessageSendEvent::UpdateHTLCs { node_id: _, updates: msgs::CommitmentUpdate{ update_add_htlcs: ref htlcs, .. }, } = events[0] {
-                               assert_eq!(htlcs[0].htlc_id, i);
-                       } else {
-                               assert!(false);
-                       }
-                       SendEvent::from_event(events.remove(0))
-               };
-               nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
-               check_added_monitors!(nodes[1], 0);
-               commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
-
-               expect_pending_htlcs_forwardable!(nodes[1]);
-               expect_payment_received!(nodes[1], our_payment_hash, 100000);
-       }
-       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap();
-       let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
-       let err = nodes[0].node.send_payment(route, our_payment_hash);
-
-       if let Err(APIError::ChannelUnavailable{err}) = err {
-               assert_eq!(err, "Cannot push more than their max accepted HTLCs");
-       } else {
-               assert!(false);
-       }
-}
-
-#[test]
-fn test_update_add_htlc_bolt2_sender_exceed_max_htlc_value_in_flight() {
-       //BOLT 2 Requirement: if the sum of total offered HTLCs would exceed the remote's max_htlc_value_in_flight_msat: MUST NOT add an HTLC.
-       let mut nodes = create_network(2, &[None, None]);
-       let channel_value = 100000;
-       let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, channel_value, 0, LocalFeatures::new(), LocalFeatures::new());
-       let max_in_flight = get_channel_value_stat!(nodes[0], chan.2).their_max_htlc_value_in_flight_msat;
-
-       send_payment(&nodes[0], &vec!(&nodes[1])[..], max_in_flight);
-
-       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], max_in_flight+1, TEST_FINAL_CLTV).unwrap();
-       let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
-       let err = nodes[0].node.send_payment(route, our_payment_hash);
-
-       if let Err(APIError::ChannelUnavailable{err}) = err {
-               assert_eq!(err, "Cannot send value that would put us over the max HTLC value in flight our peer will accept");
-       } else {
-               assert!(false);
-       }
-
-       send_payment(&nodes[0], &[&nodes[1]], max_in_flight);
-}
-
-// BOLT 2 Requirements for the Receiver when handling an update_add_htlc message.
-#[test]
-fn test_update_add_htlc_bolt2_receiver_check_amount_received_more_than_min() {
-       //BOLT2 Requirement: receiving an amount_msat equal to 0, OR less than its own htlc_minimum_msat -> SHOULD fail the channel.
-       let mut nodes = create_network(2, &[None, None]);
-       let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, LocalFeatures::new(), LocalFeatures::new());
-       let htlc_minimum_msat: u64;
-       {
-               let chan_lock = nodes[0].node.channel_state.lock().unwrap();
-               let channel = chan_lock.by_id.get(&chan.2).unwrap();
-               htlc_minimum_msat = channel.get_our_htlc_minimum_msat();
-       }
-       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], htlc_minimum_msat, TEST_FINAL_CLTV).unwrap();
-       let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
-       nodes[0].node.send_payment(route, our_payment_hash).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
-       updates.update_add_htlcs[0].amount_msat = htlc_minimum_msat-1;
-       let err = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
-       if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
-               assert_eq!(err, "Remote side tried to send less than our minimum HTLC value");
-       } else {
-               assert!(false);
-       }
-       assert!(nodes[1].node.list_channels().is_empty());
-       check_closed_broadcast!(nodes[1]);
-}
-
-#[test]
-fn test_update_add_htlc_bolt2_receiver_sender_can_afford_amount_sent() {
-       //BOLT2 Requirement: receiving an amount_msat that the sending node cannot afford at the current feerate_per_kw (while maintaining its channel reserve): SHOULD fail the channel
-       let mut nodes = create_network(2, &[None, None]);
-       let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, LocalFeatures::new(), LocalFeatures::new());
-
-       let their_channel_reserve = get_channel_value_stat!(nodes[0], chan.2).channel_reserve_msat;
-
-       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 5000000-their_channel_reserve, TEST_FINAL_CLTV).unwrap();
-       let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
-       nodes[0].node.send_payment(route, our_payment_hash).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
-
-       updates.update_add_htlcs[0].amount_msat = 5000000-their_channel_reserve+1;
-       let err = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
-
-       if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
-               assert_eq!(err, "Remote HTLC add would put them over their reserve value");
-       } else {
-               assert!(false);
-       }
-
-       assert!(nodes[1].node.list_channels().is_empty());
-       check_closed_broadcast!(nodes[1]);
-}
-
-#[test]
-fn test_update_add_htlc_bolt2_receiver_check_max_htlc_limit() {
-       //BOLT 2 Requirement: if a sending node adds more than its max_accepted_htlcs HTLCs to its local commitment transaction: SHOULD fail the channel
-       //BOLT 2 Requirement: MUST allow multiple HTLCs with the same payment_hash.
-       let mut nodes = create_network(2, &[None, None]);
-       let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, LocalFeatures::new(), LocalFeatures::new());
-       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 3999999, TEST_FINAL_CLTV).unwrap();
-       let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
-
-       let session_priv = SecretKey::from_slice(&{
-               let mut session_key = [0; 32];
-               let mut rng = thread_rng();
-               rng.fill_bytes(&mut session_key);
-               session_key
-       }).expect("RNG is bad!");
-
-       let cur_height = nodes[0].node.latest_block_height.load(Ordering::Acquire) as u32 + 1;
-       let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::signing_only(), &route, &session_priv).unwrap();
-       let (onion_payloads, _htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(&route, cur_height).unwrap();
-       let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, &our_payment_hash);
-
-       let mut msg = msgs::UpdateAddHTLC {
-               channel_id: chan.2,
-               htlc_id: 0,
-               amount_msat: 1000,
-               payment_hash: our_payment_hash,
-               cltv_expiry: htlc_cltv,
-               onion_routing_packet: onion_packet.clone(),
-       };
-
-       for i in 0..super::channel::OUR_MAX_HTLCS {
-               msg.htlc_id = i as u64;
-               nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &msg).unwrap();
-       }
-       msg.htlc_id = (super::channel::OUR_MAX_HTLCS) as u64;
-       let err = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &msg);
-
-       if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
-               assert_eq!(err, "Remote tried to push more than our max accepted HTLCs");
-       } else {
-               assert!(false);
-       }
-
-       assert!(nodes[1].node.list_channels().is_empty());
-       check_closed_broadcast!(nodes[1]);
-}
-
-#[test]
-fn test_update_add_htlc_bolt2_receiver_check_max_in_flight_msat() {
-       //OR adds more than its max_htlc_value_in_flight_msat worth of offered HTLCs to its local commitment transaction: SHOULD fail the channel
-       let mut nodes = create_network(2, &[None, None]);
-       let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000, LocalFeatures::new(), LocalFeatures::new());
-       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV).unwrap();
-       let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
-       nodes[0].node.send_payment(route, our_payment_hash).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
-       updates.update_add_htlcs[0].amount_msat = get_channel_value_stat!(nodes[1], chan.2).their_max_htlc_value_in_flight_msat + 1;
-       let err = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
-
-       if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
-               assert_eq!(err,"Remote HTLC add would put them over our max HTLC value");
-       } else {
-               assert!(false);
-       }
-
-       assert!(nodes[1].node.list_channels().is_empty());
-       check_closed_broadcast!(nodes[1]);
-}
-
-#[test]
-fn test_update_add_htlc_bolt2_receiver_check_cltv_expiry() {
-       //BOLT2 Requirement: if sending node sets cltv_expiry to greater or equal to 500000000: SHOULD fail the channel.
-       let mut nodes = create_network(2, &[None, None]);
-       create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, LocalFeatures::new(), LocalFeatures::new());
-       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 3999999, TEST_FINAL_CLTV).unwrap();
-       let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
-       nodes[0].node.send_payment(route, our_payment_hash).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
-       updates.update_add_htlcs[0].cltv_expiry = 500000000;
-       let err = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
-
-       if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
-               assert_eq!(err,"Remote provided CLTV expiry in seconds instead of block height");
-       } else {
-               assert!(false);
-       }
-
-       assert!(nodes[1].node.list_channels().is_empty());
-       check_closed_broadcast!(nodes[1]);
-}
-
-#[test]
-fn test_update_add_htlc_bolt2_receiver_check_repeated_id_ignore() {
-       //BOLT 2 requirement: if the sender did not previously acknowledge the commitment of that HTLC: MUST ignore a repeated id value after a reconnection.
-       // We test this by first testing that that repeated HTLCs pass commitment signature checks
-       // after disconnect and that non-sequential htlc_ids result in a channel failure.
-       let mut nodes = create_network(2, &[None, None]);
-       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV).unwrap();
-       let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
-       nodes[0].node.send_payment(route, our_payment_hash).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
-       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]).unwrap();
-
-       //Disconnect and Reconnect
-       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
-       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
-       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id());
-       let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
-       assert_eq!(reestablish_1.len(), 1);
-       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id());
-       let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
-       assert_eq!(reestablish_2.len(), 1);
-       nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]).unwrap();
-       handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
-       nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]).unwrap();
-       handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
-
-       //Resend HTLC
-       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]).unwrap();
-       assert_eq!(updates.commitment_signed.htlc_signatures.len(), 1);
-       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &updates.commitment_signed).unwrap();
-       check_added_monitors!(nodes[1], 1);
-       let _bs_responses = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-
-       let err = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
-       if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
-               assert_eq!(err, "Remote skipped HTLC ID");
-       } else {
-               assert!(false);
-       }
-
-       assert!(nodes[1].node.list_channels().is_empty());
-       check_closed_broadcast!(nodes[1]);
-}
-
-#[test]
-fn test_update_fulfill_htlc_bolt2_update_fulfill_htlc_before_commitment() {
-       //BOLT 2 Requirement: until the corresponding HTLC is irrevocably committed in both sides' commitment transactions:     MUST NOT send an update_fulfill_htlc, update_fail_htlc, or update_fail_malformed_htlc.
-
-       let mut nodes = create_network(2, &[None, None]);
-       let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV).unwrap();
-       let (our_payment_preimage, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
-       nodes[0].node.send_payment(route, our_payment_hash).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
-       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]).unwrap();
-
-       let update_msg = msgs::UpdateFulfillHTLC{
-               channel_id: chan.2,
-               htlc_id: 0,
-               payment_preimage: our_payment_preimage,
-       };
-
-       let err = nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_msg);
-
-       if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
-               assert_eq!(err, "Remote tried to fulfill/fail HTLC before it had been committed");
-       } else {
-               assert!(false);
-       }
-
-       assert!(nodes[0].node.list_channels().is_empty());
-       check_closed_broadcast!(nodes[0]);
-}
-
-#[test]
-fn test_update_fulfill_htlc_bolt2_update_fail_htlc_before_commitment() {
-       //BOLT 2 Requirement: until the corresponding HTLC is irrevocably committed in both sides' commitment transactions:     MUST NOT send an update_fulfill_htlc, update_fail_htlc, or update_fail_malformed_htlc.
-
-       let mut nodes = create_network(2, &[None, None]);
-       let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV).unwrap();
-       let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
-       nodes[0].node.send_payment(route, our_payment_hash).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
-       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]).unwrap();
-
-       let update_msg = msgs::UpdateFailHTLC{
-               channel_id: chan.2,
-               htlc_id: 0,
-               reason: msgs::OnionErrorPacket { data: Vec::new()},
-       };
-
-       let err = nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_msg);
-
-       if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
-               assert_eq!(err, "Remote tried to fulfill/fail HTLC before it had been committed");
-       } else {
-               assert!(false);
-       }
-
-       assert!(nodes[0].node.list_channels().is_empty());
-       check_closed_broadcast!(nodes[0]);
-}
-
-#[test]
-fn test_update_fulfill_htlc_bolt2_update_fail_malformed_htlc_before_commitment() {
-       //BOLT 2 Requirement: until the corresponding HTLC is irrevocably committed in both sides' commitment transactions:     MUST NOT send an update_fulfill_htlc, update_fail_htlc, or update_fail_malformed_htlc.
-
-       let mut nodes = create_network(2, &[None, None]);
-       let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV).unwrap();
-       let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
-       nodes[0].node.send_payment(route, our_payment_hash).unwrap();
-       check_added_monitors!(nodes[0], 1);
-       let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
-       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]).unwrap();
-
-       let update_msg = msgs::UpdateFailMalformedHTLC{
-               channel_id: chan.2,
-               htlc_id: 0,
-               sha256_of_onion: [1; 32],
-               failure_code: 0x8000,
-       };
-
-       let err = nodes[0].node.handle_update_fail_malformed_htlc(&nodes[1].node.get_our_node_id(), &update_msg);
-
-       if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
-               assert_eq!(err, "Remote tried to fulfill/fail HTLC before it had been committed");
-       } else {
-               assert!(false);
-       }
-
-       assert!(nodes[0].node.list_channels().is_empty());
-       check_closed_broadcast!(nodes[0]);
-}
-
-#[test]
-fn test_update_fulfill_htlc_bolt2_incorrect_htlc_id() {
-       //BOLT 2 Requirement: A receiving node: if the id does not correspond to an HTLC in its current commitment transaction MUST fail the channel.
-
-       let nodes = create_network(2, &[None, None]);
-       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       let our_payment_preimage = route_payment(&nodes[0], &[&nodes[1]], 100000).0;
-
-       nodes[1].node.claim_funds(our_payment_preimage);
-       check_added_monitors!(nodes[1], 1);
-
-       let events = nodes[1].node.get_and_clear_pending_msg_events();
-       assert_eq!(events.len(), 1);
-       let mut update_fulfill_msg: msgs::UpdateFulfillHTLC = {
-               match events[0] {
-                       MessageSendEvent::UpdateHTLCs { node_id: _ , updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, .. } } => {
-                               assert!(update_add_htlcs.is_empty());
-                               assert_eq!(update_fulfill_htlcs.len(), 1);
-                               assert!(update_fail_htlcs.is_empty());
-                               assert!(update_fail_malformed_htlcs.is_empty());
-                               assert!(update_fee.is_none());
-                               update_fulfill_htlcs[0].clone()
-                       },
-                       _ => panic!("Unexpected event"),
-               }
-       };
-
-       update_fulfill_msg.htlc_id = 1;
-
-       let err = nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_msg);
-       if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
-               assert_eq!(err, "Remote tried to fulfill/fail an HTLC we couldn't find");
-       } else {
-               assert!(false);
-       }
-
-       assert!(nodes[0].node.list_channels().is_empty());
-       check_closed_broadcast!(nodes[0]);
-}
-
-#[test]
-fn test_update_fulfill_htlc_bolt2_wrong_preimage() {
-       //BOLT 2 Requirement: A receiving node: if the payment_preimage value in update_fulfill_htlc doesn't SHA256 hash to the corresponding HTLC payment_hash MUST fail the channel.
-
-       let nodes = create_network(2, &[None, None]);
-       create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       let our_payment_preimage = route_payment(&nodes[0], &[&nodes[1]], 100000).0;
-
-       nodes[1].node.claim_funds(our_payment_preimage);
-       check_added_monitors!(nodes[1], 1);
-
-       let events = nodes[1].node.get_and_clear_pending_msg_events();
-       assert_eq!(events.len(), 1);
-       let mut update_fulfill_msg: msgs::UpdateFulfillHTLC = {
-               match events[0] {
-                       MessageSendEvent::UpdateHTLCs { node_id: _ , updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, .. } } => {
-                               assert!(update_add_htlcs.is_empty());
-                               assert_eq!(update_fulfill_htlcs.len(), 1);
-                               assert!(update_fail_htlcs.is_empty());
-                               assert!(update_fail_malformed_htlcs.is_empty());
-                               assert!(update_fee.is_none());
-                               update_fulfill_htlcs[0].clone()
-                       },
-                       _ => panic!("Unexpected event"),
-               }
-       };
-
-       update_fulfill_msg.payment_preimage = PaymentPreimage([1; 32]);
-
-       let err = nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_msg);
-       if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
-               assert_eq!(err, "Remote tried to fulfill HTLC with an incorrect preimage");
-       } else {
-               assert!(false);
-       }
-
-       assert!(nodes[0].node.list_channels().is_empty());
-       check_closed_broadcast!(nodes[0]);
-}
-
-
-#[test]
-fn test_update_fulfill_htlc_bolt2_missing_badonion_bit_for_malformed_htlc_message() {
-       //BOLT 2 Requirement: A receiving node: if the BADONION bit in failure_code is not set for update_fail_malformed_htlc MUST fail the channel.
-
-       let mut nodes = create_network(2, &[None, None]);
-       create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000, LocalFeatures::new(), LocalFeatures::new());
-       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV).unwrap();
-       let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
-       nodes[0].node.send_payment(route, our_payment_hash).unwrap();
-       check_added_monitors!(nodes[0], 1);
-
-       let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
-       updates.update_add_htlcs[0].onion_routing_packet.version = 1; //Produce a malformed HTLC message
-
-       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]).unwrap();
-       check_added_monitors!(nodes[1], 0);
-       commitment_signed_dance!(nodes[1], nodes[0], updates.commitment_signed, false, true);
-
-       let events = nodes[1].node.get_and_clear_pending_msg_events();
-
-       let mut update_msg: msgs::UpdateFailMalformedHTLC = {
-               match events[0] {
-                       MessageSendEvent::UpdateHTLCs { node_id: _ , updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, .. } } => {
-                               assert!(update_add_htlcs.is_empty());
-                               assert!(update_fulfill_htlcs.is_empty());
-                               assert!(update_fail_htlcs.is_empty());
-                               assert_eq!(update_fail_malformed_htlcs.len(), 1);
-                               assert!(update_fee.is_none());
-                               update_fail_malformed_htlcs[0].clone()
-                       },
-                       _ => panic!("Unexpected event"),
-               }
-       };
-       update_msg.failure_code &= !0x8000;
-       let err = nodes[0].node.handle_update_fail_malformed_htlc(&nodes[1].node.get_our_node_id(), &update_msg);
-       if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
-               assert_eq!(err, "Got update_fail_malformed_htlc with BADONION not set");
-       } else {
-               assert!(false);
-       }
-
-       assert!(nodes[0].node.list_channels().is_empty());
-       check_closed_broadcast!(nodes[0]);
-}
-
-#[test]
-fn test_update_fulfill_htlc_bolt2_after_malformed_htlc_message_must_forward_update_fail_htlc() {
-       //BOLT 2 Requirement: a receiving node which has an outgoing HTLC canceled by update_fail_malformed_htlc:
-       //    * MUST return an error in the update_fail_htlc sent to the link which originally sent the HTLC, using the failure_code given and setting the data to sha256_of_onion.
-
-       let mut nodes = create_network(3, &[None, None, None]);
-       create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000, LocalFeatures::new(), LocalFeatures::new());
-       create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1000000, 1000000, LocalFeatures::new(), LocalFeatures::new());
-
-       let route = nodes[0].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 100000, TEST_FINAL_CLTV).unwrap();
-       let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
-
-       //First hop
-       let mut payment_event = {
-               nodes[0].node.send_payment(route, our_payment_hash).unwrap();
-               check_added_monitors!(nodes[0], 1);
-               let mut events = nodes[0].node.get_and_clear_pending_msg_events();
-               assert_eq!(events.len(), 1);
-               SendEvent::from_event(events.remove(0))
-       };
-       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
-       check_added_monitors!(nodes[1], 0);
-       commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
-       expect_pending_htlcs_forwardable!(nodes[1]);
-       let mut events_2 = nodes[1].node.get_and_clear_pending_msg_events();
-       assert_eq!(events_2.len(), 1);
-       check_added_monitors!(nodes[1], 1);
-       payment_event = SendEvent::from_event(events_2.remove(0));
-       assert_eq!(payment_event.msgs.len(), 1);
-
-       //Second Hop
-       payment_event.msgs[0].onion_routing_packet.version = 1; //Produce a malformed HTLC message
-       nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
-       check_added_monitors!(nodes[2], 0);
-       commitment_signed_dance!(nodes[2], nodes[1], payment_event.commitment_msg, false, true);
-
-       let events_3 = nodes[2].node.get_and_clear_pending_msg_events();
-       assert_eq!(events_3.len(), 1);
-       let update_msg : (msgs::UpdateFailMalformedHTLC, msgs::CommitmentSigned) = {
-               match events_3[0] {
-                       MessageSendEvent::UpdateHTLCs { node_id: _ , updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => {
-                               assert!(update_add_htlcs.is_empty());
-                               assert!(update_fulfill_htlcs.is_empty());
-                               assert!(update_fail_htlcs.is_empty());
-                               assert_eq!(update_fail_malformed_htlcs.len(), 1);
-                               assert!(update_fee.is_none());
-                               (update_fail_malformed_htlcs[0].clone(), commitment_signed.clone())
-                       },
-                       _ => panic!("Unexpected event"),
-               }
-       };
-
-       nodes[1].node.handle_update_fail_malformed_htlc(&nodes[2].node.get_our_node_id(), &update_msg.0).unwrap();
-
-       check_added_monitors!(nodes[1], 0);
-       commitment_signed_dance!(nodes[1], nodes[2], update_msg.1, false, true);
-       expect_pending_htlcs_forwardable!(nodes[1]);
-       let events_4 = nodes[1].node.get_and_clear_pending_msg_events();
-       assert_eq!(events_4.len(), 1);
-
-       //Confirm that handlinge the update_malformed_htlc message produces an update_fail_htlc message to be forwarded back along the route
-       match events_4[0] {
-               MessageSendEvent::UpdateHTLCs { node_id: _ , updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, .. } } => {
-                       assert!(update_add_htlcs.is_empty());
-                       assert!(update_fulfill_htlcs.is_empty());
-                       assert_eq!(update_fail_htlcs.len(), 1);
-                       assert!(update_fail_malformed_htlcs.is_empty());
-                       assert!(update_fee.is_none());
-               },
-               _ => panic!("Unexpected event"),
-       };
-
-       check_added_monitors!(nodes[1], 1);
-}
-
-fn do_test_failure_delay_dust_htlc_local_commitment(announce_latest: bool) {
-       // Dust-HTLC failure updates must be delayed until failure-trigger tx (in this case local commitment) reach ANTI_REORG_DELAY
-       // We can have at most two valid local commitment tx, so both cases must be covered, and both txs must be checked to get them all as
-       // HTLC could have been removed from lastest local commitment tx but still valid until we get remote RAA
-
-       let nodes = create_network(2, &[None, None]);
-       let chan =create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       let bs_dust_limit = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().our_dust_limit_satoshis;
-
-       // We route 2 dust-HTLCs between A and B
-       let (_, payment_hash_1) = route_payment(&nodes[0], &[&nodes[1]], bs_dust_limit*1000);
-       let (_, payment_hash_2) = route_payment(&nodes[0], &[&nodes[1]], bs_dust_limit*1000);
-       route_payment(&nodes[0], &[&nodes[1]], 1000000);
-
-       // Cache one local commitment tx as previous
-       let as_prev_commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone();
-
-       // Fail one HTLC to prune it in the will-be-latest-local commitment tx
-       assert!(nodes[1].node.fail_htlc_backwards(&payment_hash_2));
-       check_added_monitors!(nodes[1], 0);
-       expect_pending_htlcs_forwardable!(nodes[1]);
-       check_added_monitors!(nodes[1], 1);
-
-       let remove = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-       nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &remove.update_fail_htlcs[0]).unwrap();
-       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &remove.commitment_signed).unwrap();
-       check_added_monitors!(nodes[0], 1);
-
-       // Cache one local commitment tx as lastest
-       let as_last_commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone();
-
-       let events = nodes[0].node.get_and_clear_pending_msg_events();
-       match events[0] {
-               MessageSendEvent::SendRevokeAndACK { node_id, .. } => {
-                       assert_eq!(node_id, nodes[1].node.get_our_node_id());
-               },
-               _ => panic!("Unexpected event"),
-       }
-       match events[1] {
-               MessageSendEvent::UpdateHTLCs { node_id, .. } => {
-                       assert_eq!(node_id, nodes[1].node.get_our_node_id());
-               },
-               _ => panic!("Unexpected event"),
-       }
-
-       assert_ne!(as_prev_commitment_tx, as_last_commitment_tx);
-       // Fail the 2 dust-HTLCs, move their failure in maturation buffer (htlc_updated_waiting_threshold_conf)
-       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-       if announce_latest {
-               nodes[0].chain_monitor.block_connected_checked(&header, 1, &[&as_last_commitment_tx[0]], &[1; 1]);
-       } else {
-               nodes[0].chain_monitor.block_connected_checked(&header, 1, &[&as_prev_commitment_tx[0]], &[1; 1]);
-       }
-
-       let events = nodes[0].node.get_and_clear_pending_msg_events();
-       assert_eq!(events.len(), 1);
-       match events[0] {
-               MessageSendEvent::BroadcastChannelUpdate { .. } => {},
-               _ => panic!("Unexpected event"),
-       }
-
-       assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
-       connect_blocks(&nodes[0].chain_monitor, ANTI_REORG_DELAY - 1, 1, true,  header.bitcoin_hash());
-       let events = nodes[0].node.get_and_clear_pending_events();
-       // Only 2 PaymentFailed events should show up, over-dust HTLC has to be failed by timeout tx
-       assert_eq!(events.len(), 2);
-       let mut first_failed = false;
-       for event in events {
-               match event {
-                       Event::PaymentFailed { payment_hash, .. } => {
-                               if payment_hash == payment_hash_1 {
-                                       assert!(!first_failed);
-                                       first_failed = true;
-                               } else {
-                                       assert_eq!(payment_hash, payment_hash_2);
-                               }
-                       }
-                       _ => panic!("Unexpected event"),
-               }
-       }
-}
-
-#[test]
-fn test_failure_delay_dust_htlc_local_commitment() {
-       do_test_failure_delay_dust_htlc_local_commitment(true);
-       do_test_failure_delay_dust_htlc_local_commitment(false);
-}
-
-#[test]
-fn test_no_failure_dust_htlc_local_commitment() {
-       // Transaction filters for failing back dust htlc based on local commitment txn infos has been
-       // prone to error, we test here that a dummy transaction don't fail them.
-
-       let nodes = create_network(2, &[None, None]);
-       let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       // Rebalance a bit
-       send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
-
-       let as_dust_limit = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().our_dust_limit_satoshis;
-       let bs_dust_limit = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().our_dust_limit_satoshis;
-
-       // We route 2 dust-HTLCs between A and B
-       let (preimage_1, _) = route_payment(&nodes[0], &[&nodes[1]], bs_dust_limit*1000);
-       let (preimage_2, _) = route_payment(&nodes[1], &[&nodes[0]], as_dust_limit*1000);
-
-       // Build a dummy invalid transaction trying to spend a commitment tx
-       let input = TxIn {
-               previous_output: BitcoinOutPoint { txid: chan.3.txid(), vout: 0 },
-               script_sig: Script::new(),
-               sequence: 0,
-               witness: Vec::new(),
-       };
-
-       let outp = TxOut {
-               script_pubkey: Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(),
-               value: 10000,
-       };
-
-       let dummy_tx = Transaction {
-               version: 2,
-               lock_time: 0,
-               input: vec![input],
-               output: vec![outp]
-       };
-
-       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-       nodes[0].chan_monitor.simple_monitor.block_connected(&header, 1, &[&dummy_tx], &[1;1]);
-       assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
-       assert_eq!(nodes[0].node.get_and_clear_pending_msg_events().len(), 0);
-       // We broadcast a few more block to check everything is all right
-       connect_blocks(&nodes[0].chain_monitor, 20, 1, true,  header.bitcoin_hash());
-       assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
-       assert_eq!(nodes[0].node.get_and_clear_pending_msg_events().len(), 0);
-
-       claim_payment(&nodes[0], &vec!(&nodes[1])[..], preimage_1);
-       claim_payment(&nodes[1], &vec!(&nodes[0])[..], preimage_2);
-}
-
-fn do_test_sweep_outbound_htlc_failure_update(revoked: bool, local: bool) {
-       // Outbound HTLC-failure updates must be cancelled if we get a reorg before we reach ANTI_REORG_DELAY.
-       // Broadcast of revoked remote commitment tx, trigger failure-update of dust/non-dust HTLCs
-       // Broadcast of remote commitment tx, trigger failure-update of dust-HTLCs
-       // Broadcast of timeout tx on remote commitment tx, trigger failure-udate of non-dust HTLCs
-       // Broadcast of local commitment tx, trigger failure-update of dust-HTLCs
-       // Broadcast of HTLC-timeout tx on local commitment tx, trigger failure-update of non-dust HTLCs
-
-       let nodes = create_network(3, &[None, None, None]);
-       let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
-
-       let bs_dust_limit = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().our_dust_limit_satoshis;
-
-       let (_payment_preimage_1, dust_hash) = route_payment(&nodes[0], &[&nodes[1]], bs_dust_limit*1000);
-       let (_payment_preimage_2, non_dust_hash) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
-
-       let as_commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone();
-       let bs_commitment_tx = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone();
-
-       // We revoked bs_commitment_tx
-       if revoked {
-               let (payment_preimage_3, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
-               claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_3);
-       }
-
-       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-       let mut timeout_tx = Vec::new();
-       if local {
-               // We fail dust-HTLC 1 by broadcast of local commitment tx
-               nodes[0].chain_monitor.block_connected_checked(&header, 1, &[&as_commitment_tx[0]], &[1; 1]);
-               let events = nodes[0].node.get_and_clear_pending_msg_events();
-               assert_eq!(events.len(), 1);
-               match events[0] {
-                       MessageSendEvent::BroadcastChannelUpdate { .. } => {},
-                       _ => panic!("Unexpected event"),
-               }
-               assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
-               timeout_tx.push(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap()[0].clone());
-               let parent_hash  = connect_blocks(&nodes[0].chain_monitor, ANTI_REORG_DELAY - 1, 2, true, header.bitcoin_hash());
-               let events = nodes[0].node.get_and_clear_pending_events();
-               assert_eq!(events.len(), 1);
-               match events[0] {
-                       Event::PaymentFailed { payment_hash, .. } => {
-                               assert_eq!(payment_hash, dust_hash);
-                       },
-                       _ => panic!("Unexpected event"),
-               }
-               assert_eq!(timeout_tx[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
-               // We fail non-dust-HTLC 2 by broadcast of local HTLC-timeout tx on local commitment tx
-               let header_2 = BlockHeader { version: 0x20000000, prev_blockhash: parent_hash, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-               assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
-               nodes[0].chain_monitor.block_connected_checked(&header_2, 7, &[&timeout_tx[0]], &[1; 1]);
-               let header_3 = BlockHeader { version: 0x20000000, prev_blockhash: header_2.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-               connect_blocks(&nodes[0].chain_monitor, ANTI_REORG_DELAY - 1, 8, true, header_3.bitcoin_hash());
-               let events = nodes[0].node.get_and_clear_pending_events();
-               assert_eq!(events.len(), 1);
-               match events[0] {
-                       Event::PaymentFailed { payment_hash, .. } => {
-                               assert_eq!(payment_hash, non_dust_hash);
-                       },
-                       _ => panic!("Unexpected event"),
-               }
-       } else {
-               // We fail dust-HTLC 1 by broadcast of remote commitment tx. If revoked, fail also non-dust HTLC
-               nodes[0].chain_monitor.block_connected_checked(&header, 1, &[&bs_commitment_tx[0]], &[1; 1]);
-               assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
-               let events = nodes[0].node.get_and_clear_pending_msg_events();
-               assert_eq!(events.len(), 1);
-               match events[0] {
-                       MessageSendEvent::BroadcastChannelUpdate { .. } => {},
-                       _ => panic!("Unexpected event"),
-               }
-               timeout_tx.push(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap()[0].clone());
-               let parent_hash  = connect_blocks(&nodes[0].chain_monitor, ANTI_REORG_DELAY - 1, 2, true, header.bitcoin_hash());
-               let header_2 = BlockHeader { version: 0x20000000, prev_blockhash: parent_hash, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-               if !revoked {
-                       let events = nodes[0].node.get_and_clear_pending_events();
-                       assert_eq!(events.len(), 1);
-                       match events[0] {
-                               Event::PaymentFailed { payment_hash, .. } => {
-                                       assert_eq!(payment_hash, dust_hash);
-                               },
-                               _ => panic!("Unexpected event"),
-                       }
-                       assert_eq!(timeout_tx[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
-                       // We fail non-dust-HTLC 2 by broadcast of local timeout tx on remote commitment tx
-                       nodes[0].chain_monitor.block_connected_checked(&header_2, 7, &[&timeout_tx[0]], &[1; 1]);
-                       assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
-                       let header_3 = BlockHeader { version: 0x20000000, prev_blockhash: header_2.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-                       connect_blocks(&nodes[0].chain_monitor, ANTI_REORG_DELAY - 1, 8, true, header_3.bitcoin_hash());
-                       let events = nodes[0].node.get_and_clear_pending_events();
-                       assert_eq!(events.len(), 1);
-                       match events[0] {
-                               Event::PaymentFailed { payment_hash, .. } => {
-                                       assert_eq!(payment_hash, non_dust_hash);
-                               },
-                               _ => panic!("Unexpected event"),
-                       }
-               } else {
-                       // If revoked, both dust & non-dust HTLCs should have been failed after ANTI_REORG_DELAY confs of revoked
-                       // commitment tx
-                       let events = nodes[0].node.get_and_clear_pending_events();
-                       assert_eq!(events.len(), 2);
-                       let first;
-                       match events[0] {
-                               Event::PaymentFailed { payment_hash, .. } => {
-                                       if payment_hash == dust_hash { first = true; }
-                                       else { first = false; }
-                               },
-                               _ => panic!("Unexpected event"),
-                       }
-                       match events[1] {
-                               Event::PaymentFailed { payment_hash, .. } => {
-                                       if first { assert_eq!(payment_hash, non_dust_hash); }
-                                       else { assert_eq!(payment_hash, dust_hash); }
-                               },
-                               _ => panic!("Unexpected event"),
-                       }
-               }
-       }
-}
-
-#[test]
-fn test_sweep_outbound_htlc_failure_update() {
-       do_test_sweep_outbound_htlc_failure_update(false, true);
-       do_test_sweep_outbound_htlc_failure_update(false, false);
-       do_test_sweep_outbound_htlc_failure_update(true, false);
-}
-
-#[test]
-fn test_upfront_shutdown_script() {
-       // BOLT 2 : Option upfront shutdown script, if peer commit its closing_script at channel opening
-       // enforce it at shutdown message
-
-       let mut config = UserConfig::new();
-       config.channel_options.announced_channel = true;
-       config.peer_channel_config_limits.force_announced_channel_preference = false;
-       config.channel_options.commit_upfront_shutdown_pubkey = false;
-       let nodes = create_network(3, &[None, Some(config), None]);
-
-       // We test that in case of peer committing upfront to a script, if it changes at closing, we refuse to sign
-       let flags = LocalFeatures::new();
-       let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 1000000, 1000000, flags.clone(), flags.clone());
-       nodes[0].node.close_channel(&OutPoint::new(chan.3.txid(), 0).to_channel_id()).unwrap();
-       let mut node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[2].node.get_our_node_id());
-       node_0_shutdown.scriptpubkey = Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script().to_p2sh();
-       // Test we enforce upfront_scriptpbukey if by providing a diffrent one at closing that  we disconnect peer
-       if let Err(error) = nodes[2].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown) {
-               if let Some(error) = error.action {
-                       match error {
-                               ErrorAction::SendErrorMessage { msg } => {
-                                       assert_eq!(msg.data,"Got shutdown request with a scriptpubkey which did not match their previous scriptpubkey");
-                               },
-                               _ => { assert!(false); }
-                       }
-               } else { assert!(false); }
-       } else { assert!(false); }
-       let events = nodes[2].node.get_and_clear_pending_msg_events();
-       assert_eq!(events.len(), 1);
-       match events[0] {
-               MessageSendEvent::BroadcastChannelUpdate { .. } => {},
-               _ => panic!("Unexpected event"),
-       }
-
-       // We test that in case of peer committing upfront to a script, if it doesn't change at closing, we sign
-       let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 1000000, 1000000, flags.clone(), flags.clone());
-       nodes[0].node.close_channel(&OutPoint::new(chan.3.txid(), 0).to_channel_id()).unwrap();
-       let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[2].node.get_our_node_id());
-       // We test that in case of peer committing upfront to a script, if it oesn't change at closing, we sign
-       if let Ok(_) = nodes[2].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown) {}
-       else { assert!(false) }
-       let events = nodes[2].node.get_and_clear_pending_msg_events();
-       assert_eq!(events.len(), 1);
-       match events[0] {
-               MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[0].node.get_our_node_id()) }
-               _ => panic!("Unexpected event"),
-       }
-
-       // We test that if case of peer non-signaling we don't enforce committed script at channel opening
-       let mut flags_no = LocalFeatures::new();
-       flags_no.unset_upfront_shutdown_script();
-       let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000, flags_no, flags.clone());
-       nodes[0].node.close_channel(&OutPoint::new(chan.3.txid(), 0).to_channel_id()).unwrap();
-       let mut node_1_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
-       node_1_shutdown.scriptpubkey = Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script().to_p2sh();
-       if let Ok(_) = nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_1_shutdown) {}
-       else { assert!(false) }
-       let events = nodes[1].node.get_and_clear_pending_msg_events();
-       assert_eq!(events.len(), 1);
-       match events[0] {
-               MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[0].node.get_our_node_id()) }
-               _ => panic!("Unexpected event"),
-       }
-
-       // We test that if user opt-out, we provide a zero-length script at channel opening and we are able to close
-       // channel smoothly, opt-out is from channel initiator here
-       let chan = create_announced_chan_between_nodes_with_value(&nodes, 1, 0, 1000000, 1000000, flags.clone(), flags.clone());
-       nodes[1].node.close_channel(&OutPoint::new(chan.3.txid(), 0).to_channel_id()).unwrap();
-       let mut node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
-       node_0_shutdown.scriptpubkey = Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script().to_p2sh();
-       if let Ok(_) = nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_0_shutdown) {}
-       else { assert!(false) }
-       let events = nodes[0].node.get_and_clear_pending_msg_events();
-       assert_eq!(events.len(), 1);
-       match events[0] {
-               MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
-               _ => panic!("Unexpected event"),
-       }
-
-       //// We test that if user opt-out, we provide a zero-length script at channel opening and we are able to close
-       //// channel smoothly
-       let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000, flags.clone(), flags.clone());
-       nodes[1].node.close_channel(&OutPoint::new(chan.3.txid(), 0).to_channel_id()).unwrap();
-       let mut node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
-       node_0_shutdown.scriptpubkey = Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script().to_p2sh();
-       if let Ok(_) = nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_0_shutdown) {}
-       else { assert!(false) }
-       let events = nodes[0].node.get_and_clear_pending_msg_events();
-       assert_eq!(events.len(), 2);
-       match events[0] {
-               MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
-               _ => panic!("Unexpected event"),
-       }
-       match events[1] {
-               MessageSendEvent::SendClosingSigned { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
-               _ => panic!("Unexpected event"),
-       }
-}
-
-#[test]
-fn test_user_configurable_csv_delay() {
-       // We test our channel constructors yield errors when we pass them absurd csv delay
-
-       let mut low_our_to_self_config = UserConfig::new();
-       low_our_to_self_config.own_channel_config.our_to_self_delay = 6;
-       let mut high_their_to_self_config = UserConfig::new();
-       high_their_to_self_config.peer_channel_config_limits.their_to_self_delay = 100;
-       let nodes = create_network(2, &[Some(high_their_to_self_config.clone()), None]);
-
-       // We test config.our_to_self > BREAKDOWN_TIMEOUT is enforced in Channel::new_outbound()
-       let keys_manager: Arc<KeysInterface> = Arc::new(KeysManager::new(&nodes[0].node_seed, Network::Testnet, Arc::new(test_utils::TestLogger::new()), 10, 20));
-       if let Err(error) = Channel::new_outbound(&test_utils::TestFeeEstimator { sat_per_kw: 253 }, &keys_manager, nodes[1].node.get_our_node_id(), 1000000, 1000000, 0, Arc::new(test_utils::TestLogger::new()), &low_our_to_self_config) {
-               match error {
-                       APIError::APIMisuseError { err } => { assert_eq!(err, "Configured with an unreasonable our_to_self_delay putting user funds at risks"); },
-                       _ => panic!("Unexpected event"),
-               }
-       } else { assert!(false) }
-
-       // We test config.our_to_self > BREAKDOWN_TIMEOUT is enforced in Channel::new_from_req()
-       nodes[1].node.create_channel(nodes[0].node.get_our_node_id(), 1000000, 1000000, 42).unwrap();
-       let mut open_channel = get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[0].node.get_our_node_id());
-       open_channel.to_self_delay = 200;
-       if let Err(error) = Channel::new_from_req(&test_utils::TestFeeEstimator { sat_per_kw: 253 }, &keys_manager, nodes[1].node.get_our_node_id(), LocalFeatures::new(), &open_channel, 0, Arc::new(test_utils::TestLogger::new()), &low_our_to_self_config) {
-               match error {
-                       ChannelError::Close(err) => { assert_eq!(err, "Configured with an unreasonable our_to_self_delay putting user funds at risks"); },
-                       _ => panic!("Unexpected event"),
-               }
-       } else { assert!(false); }
-
-       // We test msg.to_self_delay <= config.their_to_self_delay is enforced in Chanel::accept_channel()
-       nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 1000000, 1000000, 42).unwrap();
-       nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), LocalFeatures::new(), &get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id())).unwrap();
-       let mut accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
-       accept_channel.to_self_delay = 200;
-       if let Err(error) = nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), LocalFeatures::new(), &accept_channel) {
-               if let Some(error) = error.action {
-                       match error {
-                               ErrorAction::SendErrorMessage { msg } => {
-                                       assert_eq!(msg.data,"They wanted our payments to be delayed by a needlessly long period");
-                               },
-                               _ => { assert!(false); }
-                       }
-               } else { assert!(false); }
-       } else { assert!(false); }
-
-       // We test msg.to_self_delay <= config.their_to_self_delay is enforced in Channel::new_from_req()
-       nodes[1].node.create_channel(nodes[0].node.get_our_node_id(), 1000000, 1000000, 42).unwrap();
-       let mut open_channel = get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[0].node.get_our_node_id());
-       open_channel.to_self_delay = 200;
-       if let Err(error) = Channel::new_from_req(&test_utils::TestFeeEstimator { sat_per_kw: 253 }, &keys_manager, nodes[1].node.get_our_node_id(), LocalFeatures::new(), &open_channel, 0, Arc::new(test_utils::TestLogger::new()), &high_their_to_self_config) {
-               match error {
-                       ChannelError::Close(err) => { assert_eq!(err, "They wanted our payments to be delayed by a needlessly long period"); },
-                       _ => panic!("Unexpected event"),
-               }
-       } else { assert!(false); }
-}
-
-#[test]
-fn test_data_loss_protect() {
-       // We want to be sure that :
-       // * we don't broadcast our Local Commitment Tx in case of fallen behind
-       // * we close channel in case of detecting other being fallen behind
-       // * we are able to claim our own outputs thanks to remote my_current_per_commitment_point
-       let mut nodes = create_network(2, &[None, None]);
-
-       let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000, LocalFeatures::new(), LocalFeatures::new());
-
-       // Cache node A state before any channel update
-       let previous_node_state = nodes[0].node.encode();
-       let mut previous_chan_monitor_state = test_utils::TestVecWriter(Vec::new());
-       nodes[0].chan_monitor.simple_monitor.monitors.lock().unwrap().iter().next().unwrap().1.write_for_disk(&mut previous_chan_monitor_state).unwrap();
-
-       send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
-       send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
-
-       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
-       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
-
-       // Restore node A from previous state
-       let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::with_id(format!("node {}", 0)));
-       let chan_monitor = <(Sha256dHash, ChannelMonitor)>::read(&mut ::std::io::Cursor::new(previous_chan_monitor_state.0), Arc::clone(&logger)).unwrap().1;
-       let chain_monitor = Arc::new(ChainWatchInterfaceUtil::new(Network::Testnet, Arc::clone(&logger)));
-       let tx_broadcaster = Arc::new(test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new())});
-       let feeest = Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 });
-       let monitor = Arc::new(test_utils::TestChannelMonitor::new(chain_monitor.clone(), tx_broadcaster.clone(), logger.clone(), feeest.clone()));
-       let mut channel_monitors = HashMap::new();
-       channel_monitors.insert(OutPoint { txid: chan.3.txid(), index: 0 }, &chan_monitor);
-       let node_state_0 = <(Sha256dHash, ChannelManager)>::read(&mut ::std::io::Cursor::new(previous_node_state), ChannelManagerReadArgs {
-               keys_manager: Arc::new(keysinterface::KeysManager::new(&nodes[0].node_seed, Network::Testnet, Arc::clone(&logger), 42, 21)),
-               fee_estimator: feeest.clone(),
-               monitor: monitor.clone(),
-               chain_monitor: chain_monitor.clone(),
-               logger: Arc::clone(&logger),
-               tx_broadcaster,
-               default_config: UserConfig::new(),
-               channel_monitors: &channel_monitors
-       }).unwrap().1;
-       nodes[0].node = Arc::new(node_state_0);
-       monitor.add_update_monitor(OutPoint { txid: chan.3.txid(), index: 0 }, chan_monitor.clone()).is_ok();
-       nodes[0].chan_monitor = monitor;
-       nodes[0].chain_monitor = chain_monitor;
-       check_added_monitors!(nodes[0], 1);
-
-       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id());
-       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id());
-
-       let reestablish_0 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
-
-       // Check we update monitor following learning of per_commitment_point from B
-       if let Err(err) = nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_0[0])  {
-               if let Some(error) = err.action {
-                       match error {
-                               ErrorAction::SendErrorMessage { msg } => {
-                                       assert_eq!(msg.data, "We have fallen behind - we have received proof that if we broadcast remote is going to claim our funds - we can't do any automated broadcasting");
-                               },
-                               _ => panic!("Unexpected event!"),
-                       }
-               } else { assert!(false); }
-       } else { assert!(false); }
-       check_added_monitors!(nodes[0], 1);
-
-       {
-               let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
-               assert_eq!(node_txn.len(), 0);
-       }
-
-       let mut reestablish_1 = Vec::with_capacity(1);
-       for msg in nodes[0].node.get_and_clear_pending_msg_events() {
-               if let MessageSendEvent::SendChannelReestablish { ref node_id, ref msg } = msg {
-                       assert_eq!(*node_id, nodes[1].node.get_our_node_id());
-                       reestablish_1.push(msg.clone());
-               } else if let MessageSendEvent::BroadcastChannelUpdate { .. } = msg {
-               } else {
-                       panic!("Unexpected event")
-               }
-       }
-
-       // Check we close channel detecting A is fallen-behind
-       if let Err(err) = nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]) {
-               if let Some(error) = err.action {
-                       match error {
-                               ErrorAction::SendErrorMessage { msg } => {
-                                       assert_eq!(msg.data, "Peer attempted to reestablish channel with a very old local commitment transaction"); },
-                               _ => panic!("Unexpected event!"),
-                       }
-               } else { assert!(false); }
-       } else { assert!(false); }
-
-       let events = nodes[1].node.get_and_clear_pending_msg_events();
-       assert_eq!(events.len(), 1);
-       match events[0] {
-               MessageSendEvent::BroadcastChannelUpdate { .. } => {},
-               _ => panic!("Unexpected event"),
-       }
-
-       // Check A is able to claim to_remote output
-       let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
-       assert_eq!(node_txn.len(), 1);
-       check_spends!(node_txn[0], chan.3.clone());
-       assert_eq!(node_txn[0].output.len(), 2);
-       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42};
-       nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn[0].clone()]}, 1);
-       let spend_txn = check_spendable_outputs!(nodes[0], 1);
-       assert_eq!(spend_txn.len(), 1);
-       check_spends!(spend_txn[0], node_txn[0].clone());
-}
diff --git a/src/ln/mod.rs b/src/ln/mod.rs
deleted file mode 100644 (file)
index 9b1b442..0000000
+++ /dev/null
@@ -1,32 +0,0 @@
-//! High level lightning structs and impls live here.
-//!
-//! You probably want to create a channelmanager::ChannelManager, and a router::Router first.
-//! Then, you probably want to pass them both on to a peer_handler::PeerManager and use that to
-//! create/manage connections and call get_and_clear_pending_events after each action, handling
-//! them appropriately.
-//!
-//! When you want to open/close a channel or send a payment, call into your ChannelManager and when
-//! you want to learn things about the network topology (eg get a route for sending a payment),
-//! call into your Router.
-
-pub mod channelmanager;
-pub mod channelmonitor;
-pub mod msgs;
-pub mod router;
-pub mod peer_handler;
-
-#[cfg(feature = "fuzztarget")]
-pub mod peer_channel_encryptor;
-#[cfg(not(feature = "fuzztarget"))]
-pub(crate) mod peer_channel_encryptor;
-
-mod channel;
-mod chan_utils;
-mod onion_utils;
-
-#[cfg(test)]
-#[macro_use] mod functional_test_utils;
-#[cfg(test)]
-mod functional_tests;
-#[cfg(test)]
-mod chanmon_update_fail_tests;
diff --git a/src/ln/msgs.rs b/src/ln/msgs.rs
deleted file mode 100644 (file)
index 0e6c4b9..0000000
+++ /dev/null
@@ -1,2063 +0,0 @@
-//! Wire messages, traits representing wire message handlers, and a few error types live here.
-//!
-//! For a normal node you probably don't need to use anything here, however, if you wish to split a
-//! node into an internet-facing route/message socket handling daemon and a separate daemon (or
-//! server entirely) which handles only channel-related messages you may wish to implement
-//! ChannelMessageHandler yourself and use it to re-serialize messages and pass them across
-//! daemons/servers.
-//!
-//! Note that if you go with such an architecture (instead of passing raw socket events to a
-//! non-internet-facing system) you trust the frontend internet-facing system to not lie about the
-//! source node_id of the message, however this does allow you to significantly reduce bandwidth
-//! between the systems as routing messages can represent a significant chunk of bandwidth usage
-//! (especially for non-channel-publicly-announcing nodes). As an alternate design which avoids
-//! this issue, if you have sufficient bidirectional bandwidth between your systems, you may send
-//! raw socket events into your non-internet-facing system and then send routing events back to
-//! track the network on the less-secure system.
-
-use secp256k1::key::PublicKey;
-use secp256k1::Signature;
-use secp256k1;
-use bitcoin_hashes::sha256d::Hash as Sha256dHash;
-use bitcoin::blockdata::script::Script;
-
-use std::error::Error;
-use std::{cmp, fmt};
-use std::io::Read;
-use std::result::Result;
-
-use util::events;
-use util::ser::{Readable, Writeable, Writer};
-
-use ln::channelmanager::{PaymentPreimage, PaymentHash};
-
-/// An error in decoding a message or struct.
-#[derive(Debug)]
-pub enum DecodeError {
-       /// A version byte specified something we don't know how to handle.
-       /// Includes unknown realm byte in an OnionHopData packet
-       UnknownVersion,
-       /// Unknown feature mandating we fail to parse message
-       UnknownRequiredFeature,
-       /// Value was invalid, eg a byte which was supposed to be a bool was something other than a 0
-       /// or 1, a public key/private key/signature was invalid, text wasn't UTF-8, etc
-       InvalidValue,
-       /// Buffer too short
-       ShortRead,
-       /// node_announcement included more than one address of a given type!
-       ExtraAddressesPerType,
-       /// A length descriptor in the packet didn't describe the later data correctly
-       BadLengthDescriptor,
-       /// Error from std::io
-       Io(::std::io::Error),
-}
-
-/// Tracks localfeatures which are only in init messages
-#[derive(Clone, PartialEq)]
-pub struct LocalFeatures {
-       flags: Vec<u8>,
-}
-
-impl LocalFeatures {
-       /// Create a blank LocalFeatures flags (visibility extended for fuzz tests)
-       #[cfg(not(feature = "fuzztarget"))]
-       pub(crate) fn new() -> LocalFeatures {
-               LocalFeatures {
-                       flags: vec![2 | 1 << 5],
-               }
-       }
-       #[cfg(feature = "fuzztarget")]
-       pub fn new() -> LocalFeatures {
-               LocalFeatures {
-                       flags: vec![2 | 1 << 5],
-               }
-       }
-
-       pub(crate) fn supports_data_loss_protect(&self) -> bool {
-               self.flags.len() > 0 && (self.flags[0] & 3) != 0
-       }
-       pub(crate) fn initial_routing_sync(&self) -> bool {
-               self.flags.len() > 0 && (self.flags[0] & (1 << 3)) != 0
-       }
-       pub(crate) fn set_initial_routing_sync(&mut self) {
-               if self.flags.len() == 0 {
-                       self.flags.resize(1, 1 << 3);
-               } else {
-                       self.flags[0] |= 1 << 3;
-               }
-       }
-
-       pub(crate) fn supports_upfront_shutdown_script(&self) -> bool {
-               self.flags.len() > 0 && (self.flags[0] & (3 << 4)) != 0
-       }
-       #[cfg(test)]
-       pub(crate) fn unset_upfront_shutdown_script(&mut self) {
-               self.flags[0] ^= 1 << 5;
-       }
-
-       pub(crate) fn requires_unknown_bits(&self) -> bool {
-               self.flags.iter().enumerate().any(|(idx, &byte)| {
-                       ( idx != 0 && (byte & 0x55) != 0 ) || ( idx == 0 && (byte & 0x14) != 0 )
-               })
-       }
-
-       pub(crate) fn supports_unknown_bits(&self) -> bool {
-               self.flags.iter().enumerate().any(|(idx, &byte)| {
-                       ( idx != 0 && byte != 0 ) || ( idx == 0 && (byte & 0xc4) != 0 )
-               })
-       }
-}
-
-/// Tracks globalfeatures which are in init messages and routing announcements
-#[derive(Clone, PartialEq, Debug)]
-pub struct GlobalFeatures {
-       #[cfg(not(test))]
-       flags: Vec<u8>,
-       // Used to test encoding of diverse msgs
-       #[cfg(test)]
-       pub flags: Vec<u8>
-}
-
-impl GlobalFeatures {
-       pub(crate) fn new() -> GlobalFeatures {
-               GlobalFeatures {
-                       flags: Vec::new(),
-               }
-       }
-
-       pub(crate) fn requires_unknown_bits(&self) -> bool {
-               for &byte in self.flags.iter() {
-                       if (byte & 0x55) != 0 {
-                               return true;
-                       }
-               }
-               return false;
-       }
-
-       pub(crate) fn supports_unknown_bits(&self) -> bool {
-               for &byte in self.flags.iter() {
-                       if byte != 0 {
-                               return true;
-                       }
-               }
-               return false;
-       }
-}
-
-/// An init message to be sent or received from a peer
-pub struct Init {
-       pub(crate) global_features: GlobalFeatures,
-       pub(crate) local_features: LocalFeatures,
-}
-
-/// An error message to be sent or received from a peer
-#[derive(Clone)]
-pub struct ErrorMessage {
-       pub(crate) channel_id: [u8; 32],
-       pub(crate) data: String,
-}
-
-/// A ping message to be sent or received from a peer
-pub struct Ping {
-       pub(crate) ponglen: u16,
-       pub(crate) byteslen: u16,
-}
-
-/// A pong message to be sent or received from a peer
-pub struct Pong {
-       pub(crate) byteslen: u16,
-}
-
-/// An open_channel message to be sent or received from a peer
-#[derive(Clone)]
-pub struct OpenChannel {
-       pub(crate) chain_hash: Sha256dHash,
-       pub(crate) temporary_channel_id: [u8; 32],
-       pub(crate) funding_satoshis: u64,
-       pub(crate) push_msat: u64,
-       pub(crate) dust_limit_satoshis: u64,
-       pub(crate) max_htlc_value_in_flight_msat: u64,
-       pub(crate) channel_reserve_satoshis: u64,
-       pub(crate) htlc_minimum_msat: u64,
-       pub(crate) feerate_per_kw: u32,
-       pub(crate) to_self_delay: u16,
-       pub(crate) max_accepted_htlcs: u16,
-       pub(crate) funding_pubkey: PublicKey,
-       pub(crate) revocation_basepoint: PublicKey,
-       pub(crate) payment_basepoint: PublicKey,
-       pub(crate) delayed_payment_basepoint: PublicKey,
-       pub(crate) htlc_basepoint: PublicKey,
-       pub(crate) first_per_commitment_point: PublicKey,
-       pub(crate) channel_flags: u8,
-       pub(crate) shutdown_scriptpubkey: OptionalField<Script>,
-}
-
-/// An accept_channel message to be sent or received from a peer
-#[derive(Clone)]
-pub struct AcceptChannel {
-       pub(crate) temporary_channel_id: [u8; 32],
-       pub(crate) dust_limit_satoshis: u64,
-       pub(crate) max_htlc_value_in_flight_msat: u64,
-       pub(crate) channel_reserve_satoshis: u64,
-       pub(crate) htlc_minimum_msat: u64,
-       pub(crate) minimum_depth: u32,
-       pub(crate) to_self_delay: u16,
-       pub(crate) max_accepted_htlcs: u16,
-       pub(crate) funding_pubkey: PublicKey,
-       pub(crate) revocation_basepoint: PublicKey,
-       pub(crate) payment_basepoint: PublicKey,
-       pub(crate) delayed_payment_basepoint: PublicKey,
-       pub(crate) htlc_basepoint: PublicKey,
-       pub(crate) first_per_commitment_point: PublicKey,
-       pub(crate) shutdown_scriptpubkey: OptionalField<Script>
-}
-
-/// A funding_created message to be sent or received from a peer
-#[derive(Clone)]
-pub struct FundingCreated {
-       pub(crate) temporary_channel_id: [u8; 32],
-       pub(crate) funding_txid: Sha256dHash,
-       pub(crate) funding_output_index: u16,
-       pub(crate) signature: Signature,
-}
-
-/// A funding_signed message to be sent or received from a peer
-#[derive(Clone)]
-pub struct FundingSigned {
-       pub(crate) channel_id: [u8; 32],
-       pub(crate) signature: Signature,
-}
-
-/// A funding_locked message to be sent or received from a peer
-#[derive(Clone, PartialEq)]
-pub struct FundingLocked {
-       pub(crate) channel_id: [u8; 32],
-       pub(crate) next_per_commitment_point: PublicKey,
-}
-
-/// A shutdown message to be sent or received from a peer
-#[derive(Clone, PartialEq)]
-pub struct Shutdown {
-       pub(crate) channel_id: [u8; 32],
-       pub(crate) scriptpubkey: Script,
-}
-
-/// A closing_signed message to be sent or received from a peer
-#[derive(Clone, PartialEq)]
-pub struct ClosingSigned {
-       pub(crate) channel_id: [u8; 32],
-       pub(crate) fee_satoshis: u64,
-       pub(crate) signature: Signature,
-}
-
-/// An update_add_htlc message to be sent or received from a peer
-#[derive(Clone, PartialEq)]
-pub struct UpdateAddHTLC {
-       pub(crate) channel_id: [u8; 32],
-       pub(crate) htlc_id: u64,
-       pub(crate) amount_msat: u64,
-       pub(crate) payment_hash: PaymentHash,
-       pub(crate) cltv_expiry: u32,
-       pub(crate) onion_routing_packet: OnionPacket,
-}
-
-/// An update_fulfill_htlc message to be sent or received from a peer
-#[derive(Clone, PartialEq)]
-pub struct UpdateFulfillHTLC {
-       pub(crate) channel_id: [u8; 32],
-       pub(crate) htlc_id: u64,
-       pub(crate) payment_preimage: PaymentPreimage,
-}
-
-/// An update_fail_htlc message to be sent or received from a peer
-#[derive(Clone, PartialEq)]
-pub struct UpdateFailHTLC {
-       pub(crate) channel_id: [u8; 32],
-       pub(crate) htlc_id: u64,
-       pub(crate) reason: OnionErrorPacket,
-}
-
-/// An update_fail_malformed_htlc message to be sent or received from a peer
-#[derive(Clone, PartialEq)]
-pub struct UpdateFailMalformedHTLC {
-       pub(crate) channel_id: [u8; 32],
-       pub(crate) htlc_id: u64,
-       pub(crate) sha256_of_onion: [u8; 32],
-       pub(crate) failure_code: u16,
-}
-
-/// A commitment_signed message to be sent or received from a peer
-#[derive(Clone, PartialEq)]
-pub struct CommitmentSigned {
-       pub(crate) channel_id: [u8; 32],
-       pub(crate) signature: Signature,
-       pub(crate) htlc_signatures: Vec<Signature>,
-}
-
-/// A revoke_and_ack message to be sent or received from a peer
-#[derive(Clone, PartialEq)]
-pub struct RevokeAndACK {
-       pub(crate) channel_id: [u8; 32],
-       pub(crate) per_commitment_secret: [u8; 32],
-       pub(crate) next_per_commitment_point: PublicKey,
-}
-
-/// An update_fee message to be sent or received from a peer
-#[derive(PartialEq, Clone)]
-pub struct UpdateFee {
-       pub(crate) channel_id: [u8; 32],
-       pub(crate) feerate_per_kw: u32,
-}
-
-#[derive(PartialEq, Clone)]
-pub(crate) struct DataLossProtect {
-       pub(crate) your_last_per_commitment_secret: [u8; 32],
-       pub(crate) my_current_per_commitment_point: PublicKey,
-}
-
-/// A channel_reestablish message to be sent or received from a peer
-#[derive(PartialEq, Clone)]
-pub struct ChannelReestablish {
-       pub(crate) channel_id: [u8; 32],
-       pub(crate) next_local_commitment_number: u64,
-       pub(crate) next_remote_commitment_number: u64,
-       pub(crate) data_loss_protect: OptionalField<DataLossProtect>,
-}
-
-/// An announcement_signatures message to be sent or received from a peer
-#[derive(PartialEq, Clone, Debug)]
-pub struct AnnouncementSignatures {
-       pub(crate) channel_id: [u8; 32],
-       pub(crate) short_channel_id: u64,
-       pub(crate) node_signature: Signature,
-       pub(crate) bitcoin_signature: Signature,
-}
-
-/// An address which can be used to connect to a remote peer
-#[derive(Clone, PartialEq, Debug)]
-pub enum NetAddress {
-       /// An IPv4 address/port on which the peer is listening.
-       IPv4 {
-               /// The 4-byte IPv4 address
-               addr: [u8; 4],
-               /// The port on which the node is listening
-               port: u16,
-       },
-       /// An IPv6 address/port on which the peer is listening.
-       IPv6 {
-               /// The 16-byte IPv6 address
-               addr: [u8; 16],
-               /// The port on which the node is listening
-               port: u16,
-       },
-       /// An old-style Tor onion address/port on which the peer is listening.
-       OnionV2 {
-               /// The bytes (usually encoded in base32 with ".onion" appended)
-               addr: [u8; 10],
-               /// The port on which the node is listening
-               port: u16,
-       },
-       /// A new-style Tor onion address/port on which the peer is listening.
-       /// To create the human-readable "hostname", concatenate ed25519_pubkey, checksum, and version,
-       /// wrap as base32 and append ".onion".
-       OnionV3 {
-               /// The ed25519 long-term public key of the peer
-               ed25519_pubkey: [u8; 32],
-               /// The checksum of the pubkey and version, as included in the onion address
-               checksum: u16,
-               /// The version byte, as defined by the Tor Onion v3 spec.
-               version: u8,
-               /// The port on which the node is listening
-               port: u16,
-       },
-}
-impl NetAddress {
-       fn get_id(&self) -> u8 {
-               match self {
-                       &NetAddress::IPv4 {..} => { 1 },
-                       &NetAddress::IPv6 {..} => { 2 },
-                       &NetAddress::OnionV2 {..} => { 3 },
-                       &NetAddress::OnionV3 {..} => { 4 },
-               }
-       }
-
-       /// Strict byte-length of address descriptor, 1-byte type not recorded
-       fn len(&self) -> u16 {
-               match self {
-                       &NetAddress::IPv4 { .. } => { 6 },
-                       &NetAddress::IPv6 { .. } => { 18 },
-                       &NetAddress::OnionV2 { .. } => { 12 },
-                       &NetAddress::OnionV3 { .. } => { 37 },
-               }
-       }
-}
-
-impl Writeable for NetAddress {
-       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
-               match self {
-                       &NetAddress::IPv4 { ref addr, ref port } => {
-                               1u8.write(writer)?;
-                               addr.write(writer)?;
-                               port.write(writer)?;
-                       },
-                       &NetAddress::IPv6 { ref addr, ref port } => {
-                               2u8.write(writer)?;
-                               addr.write(writer)?;
-                               port.write(writer)?;
-                       },
-                       &NetAddress::OnionV2 { ref addr, ref port } => {
-                               3u8.write(writer)?;
-                               addr.write(writer)?;
-                               port.write(writer)?;
-                       },
-                       &NetAddress::OnionV3 { ref ed25519_pubkey, ref checksum, ref version, ref port } => {
-                               4u8.write(writer)?;
-                               ed25519_pubkey.write(writer)?;
-                               checksum.write(writer)?;
-                               version.write(writer)?;
-                               port.write(writer)?;
-                       }
-               }
-               Ok(())
-       }
-}
-
-impl<R: ::std::io::Read>  Readable<R> for Result<NetAddress, u8> {
-       fn read(reader: &mut R) -> Result<Result<NetAddress, u8>, DecodeError> {
-               let byte = <u8 as Readable<R>>::read(reader)?;
-               match byte {
-                       1 => {
-                               Ok(Ok(NetAddress::IPv4 {
-                                       addr: Readable::read(reader)?,
-                                       port: Readable::read(reader)?,
-                               }))
-                       },
-                       2 => {
-                               Ok(Ok(NetAddress::IPv6 {
-                                       addr: Readable::read(reader)?,
-                                       port: Readable::read(reader)?,
-                               }))
-                       },
-                       3 => {
-                               Ok(Ok(NetAddress::OnionV2 {
-                                       addr: Readable::read(reader)?,
-                                       port: Readable::read(reader)?,
-                               }))
-                       },
-                       4 => {
-                               Ok(Ok(NetAddress::OnionV3 {
-                                       ed25519_pubkey: Readable::read(reader)?,
-                                       checksum: Readable::read(reader)?,
-                                       version: Readable::read(reader)?,
-                                       port: Readable::read(reader)?,
-                               }))
-                       },
-                       _ => return Ok(Err(byte)),
-               }
-       }
-}
-
-// Only exposed as broadcast of node_announcement should be filtered by node_id
-/// The unsigned part of a node_announcement
-#[derive(PartialEq, Clone, Debug)]
-pub struct UnsignedNodeAnnouncement {
-       pub(crate) features: GlobalFeatures,
-       pub(crate) timestamp: u32,
-       /// The node_id this announcement originated from (don't rebroadcast the node_announcement back
-       /// to this node).
-       pub        node_id: PublicKey,
-       pub(crate) rgb: [u8; 3],
-       pub(crate) alias: [u8; 32],
-       /// List of addresses on which this node is reachable. Note that you may only have up to one
-       /// address of each type, if you have more, they may be silently discarded or we may panic!
-       pub(crate) addresses: Vec<NetAddress>,
-       pub(crate) excess_address_data: Vec<u8>,
-       pub(crate) excess_data: Vec<u8>,
-}
-#[derive(PartialEq, Clone)]
-/// A node_announcement message to be sent or received from a peer
-pub struct NodeAnnouncement {
-       pub(crate) signature: Signature,
-       pub(crate) contents: UnsignedNodeAnnouncement,
-}
-
-// Only exposed as broadcast of channel_announcement should be filtered by node_id
-/// The unsigned part of a channel_announcement
-#[derive(PartialEq, Clone, Debug)]
-pub struct UnsignedChannelAnnouncement {
-       pub(crate) features: GlobalFeatures,
-       pub(crate) chain_hash: Sha256dHash,
-       pub(crate) short_channel_id: u64,
-       /// One of the two node_ids which are endpoints of this channel
-       pub        node_id_1: PublicKey,
-       /// The other of the two node_ids which are endpoints of this channel
-       pub        node_id_2: PublicKey,
-       pub(crate) bitcoin_key_1: PublicKey,
-       pub(crate) bitcoin_key_2: PublicKey,
-       pub(crate) excess_data: Vec<u8>,
-}
-/// A channel_announcement message to be sent or received from a peer
-#[derive(PartialEq, Clone, Debug)]
-pub struct ChannelAnnouncement {
-       pub(crate) node_signature_1: Signature,
-       pub(crate) node_signature_2: Signature,
-       pub(crate) bitcoin_signature_1: Signature,
-       pub(crate) bitcoin_signature_2: Signature,
-       pub(crate) contents: UnsignedChannelAnnouncement,
-}
-
-#[derive(PartialEq, Clone, Debug)]
-pub(crate) struct UnsignedChannelUpdate {
-       pub(crate) chain_hash: Sha256dHash,
-       pub(crate) short_channel_id: u64,
-       pub(crate) timestamp: u32,
-       pub(crate) flags: u16,
-       pub(crate) cltv_expiry_delta: u16,
-       pub(crate) htlc_minimum_msat: u64,
-       pub(crate) fee_base_msat: u32,
-       pub(crate) fee_proportional_millionths: u32,
-       pub(crate) excess_data: Vec<u8>,
-}
-/// A channel_update message to be sent or received from a peer
-#[derive(PartialEq, Clone, Debug)]
-pub struct ChannelUpdate {
-       pub(crate) signature: Signature,
-       pub(crate) contents: UnsignedChannelUpdate,
-}
-
-/// Used to put an error message in a HandleError
-#[derive(Clone)]
-pub enum ErrorAction {
-       /// The peer took some action which made us think they were useless. Disconnect them.
-       DisconnectPeer {
-               /// An error message which we should make an effort to send before we disconnect.
-               msg: Option<ErrorMessage>
-       },
-       /// The peer did something harmless that we weren't able to process, just log and ignore
-       IgnoreError,
-       /// The peer did something incorrect. Tell them.
-       SendErrorMessage {
-               /// The message to send.
-               msg: ErrorMessage
-       },
-}
-
-/// An Err type for failure to process messages.
-pub struct HandleError { //TODO: rename me
-       /// A human-readable message describing the error
-       pub err: &'static str,
-       /// The action which should be taken against the offending peer.
-       pub action: Option<ErrorAction>, //TODO: Make this required
-}
-
-/// Struct used to return values from revoke_and_ack messages, containing a bunch of commitment
-/// transaction updates if they were pending.
-#[derive(PartialEq, Clone)]
-pub struct CommitmentUpdate {
-       /// update_add_htlc messages which should be sent
-       pub update_add_htlcs: Vec<UpdateAddHTLC>,
-       /// update_fulfill_htlc messages which should be sent
-       pub update_fulfill_htlcs: Vec<UpdateFulfillHTLC>,
-       /// update_fail_htlc messages which should be sent
-       pub update_fail_htlcs: Vec<UpdateFailHTLC>,
-       /// update_fail_malformed_htlc messages which should be sent
-       pub update_fail_malformed_htlcs: Vec<UpdateFailMalformedHTLC>,
-       /// An update_fee message which should be sent
-       pub update_fee: Option<UpdateFee>,
-       /// Finally, the commitment_signed message which should be sent
-       pub commitment_signed: CommitmentSigned,
-}
-
-/// The information we received from a peer along the route of a payment we originated. This is
-/// returned by ChannelMessageHandler::handle_update_fail_htlc to be passed into
-/// RoutingMessageHandler::handle_htlc_fail_channel_update to update our network map.
-#[derive(Clone)]
-pub enum HTLCFailChannelUpdate {
-       /// We received an error which included a full ChannelUpdate message.
-       ChannelUpdateMessage {
-               /// The unwrapped message we received
-               msg: ChannelUpdate,
-       },
-       /// We received an error which indicated only that a channel has been closed
-       ChannelClosed {
-               /// The short_channel_id which has now closed.
-               short_channel_id: u64,
-               /// when this true, this channel should be permanently removed from the
-               /// consideration. Otherwise, this channel can be restored as new channel_update is received
-               is_permanent: bool,
-       },
-       /// We received an error which indicated only that a node has failed
-       NodeFailure {
-               /// The node_id that has failed.
-               node_id: PublicKey,
-               /// when this true, node should be permanently removed from the
-               /// consideration. Otherwise, the channels connected to this node can be
-               /// restored as new channel_update is received
-               is_permanent: bool,
-       }
-}
-
-/// Messages could have optional fields to use with extended features
-/// As we wish to serialize these differently from Option<T>s (Options get a tag byte, but
-/// OptionalFeild simply gets Present if there are enough bytes to read into it), we have a
-/// separate enum type for them.
-#[derive(Clone, PartialEq)]
-pub enum OptionalField<T> {
-       /// Optional field is included in message
-       Present(T),
-       /// Optional field is absent in message
-       Absent
-}
-
-/// A trait to describe an object which can receive channel messages.
-///
-/// Messages MAY be called in parallel when they originate from different their_node_ids, however
-/// they MUST NOT be called in parallel when the two calls have the same their_node_id.
-pub trait ChannelMessageHandler : events::MessageSendEventsProvider + Send + Sync {
-       //Channel init:
-       /// Handle an incoming open_channel message from the given peer.
-       fn handle_open_channel(&self, their_node_id: &PublicKey, their_local_features: LocalFeatures, msg: &OpenChannel) -> Result<(), HandleError>;
-       /// Handle an incoming accept_channel message from the given peer.
-       fn handle_accept_channel(&self, their_node_id: &PublicKey, their_local_features: LocalFeatures, msg: &AcceptChannel) -> Result<(), HandleError>;
-       /// Handle an incoming funding_created message from the given peer.
-       fn handle_funding_created(&self, their_node_id: &PublicKey, msg: &FundingCreated) -> Result<(), HandleError>;
-       /// Handle an incoming funding_signed message from the given peer.
-       fn handle_funding_signed(&self, their_node_id: &PublicKey, msg: &FundingSigned) -> Result<(), HandleError>;
-       /// Handle an incoming funding_locked message from the given peer.
-       fn handle_funding_locked(&self, their_node_id: &PublicKey, msg: &FundingLocked) -> Result<(), HandleError>;
-
-       // Channl close:
-       /// Handle an incoming shutdown message from the given peer.
-       fn handle_shutdown(&self, their_node_id: &PublicKey, msg: &Shutdown) -> Result<(), HandleError>;
-       /// Handle an incoming closing_signed message from the given peer.
-       fn handle_closing_signed(&self, their_node_id: &PublicKey, msg: &ClosingSigned) -> Result<(), HandleError>;
-
-       // HTLC handling:
-       /// Handle an incoming update_add_htlc message from the given peer.
-       fn handle_update_add_htlc(&self, their_node_id: &PublicKey, msg: &UpdateAddHTLC) -> Result<(), HandleError>;
-       /// Handle an incoming update_fulfill_htlc message from the given peer.
-       fn handle_update_fulfill_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFulfillHTLC) -> Result<(), HandleError>;
-       /// Handle an incoming update_fail_htlc message from the given peer.
-       fn handle_update_fail_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFailHTLC) -> Result<(), HandleError>;
-       /// Handle an incoming update_fail_malformed_htlc message from the given peer.
-       fn handle_update_fail_malformed_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFailMalformedHTLC) -> Result<(), HandleError>;
-       /// Handle an incoming commitment_signed message from the given peer.
-       fn handle_commitment_signed(&self, their_node_id: &PublicKey, msg: &CommitmentSigned) -> Result<(), HandleError>;
-       /// Handle an incoming revoke_and_ack message from the given peer.
-       fn handle_revoke_and_ack(&self, their_node_id: &PublicKey, msg: &RevokeAndACK) -> Result<(), HandleError>;
-
-       /// Handle an incoming update_fee message from the given peer.
-       fn handle_update_fee(&self, their_node_id: &PublicKey, msg: &UpdateFee) -> Result<(), HandleError>;
-
-       // Channel-to-announce:
-       /// Handle an incoming announcement_signatures message from the given peer.
-       fn handle_announcement_signatures(&self, their_node_id: &PublicKey, msg: &AnnouncementSignatures) -> Result<(), HandleError>;
-
-       // Connection loss/reestablish:
-       /// Indicates a connection to the peer failed/an existing connection was lost. If no connection
-       /// is believed to be possible in the future (eg they're sending us messages we don't
-       /// understand or indicate they require unknown feature bits), no_connection_possible is set
-       /// and any outstanding channels should be failed.
-       fn peer_disconnected(&self, their_node_id: &PublicKey, no_connection_possible: bool);
-
-       /// Handle a peer reconnecting, possibly generating channel_reestablish message(s).
-       fn peer_connected(&self, their_node_id: &PublicKey);
-       /// Handle an incoming channel_reestablish message from the given peer.
-       fn handle_channel_reestablish(&self, their_node_id: &PublicKey, msg: &ChannelReestablish) -> Result<(), HandleError>;
-
-       // Error:
-       /// Handle an incoming error message from the given peer.
-       fn handle_error(&self, their_node_id: &PublicKey, msg: &ErrorMessage);
-}
-
-/// A trait to describe an object which can receive routing messages.
-pub trait RoutingMessageHandler : Send + Sync {
-       /// Handle an incoming node_announcement message, returning true if it should be forwarded on,
-       /// false or returning an Err otherwise.
-       fn handle_node_announcement(&self, msg: &NodeAnnouncement) -> Result<bool, HandleError>;
-       /// Handle a channel_announcement message, returning true if it should be forwarded on, false
-       /// or returning an Err otherwise.
-       fn handle_channel_announcement(&self, msg: &ChannelAnnouncement) -> Result<bool, HandleError>;
-       /// Handle an incoming channel_update message, returning true if it should be forwarded on,
-       /// false or returning an Err otherwise.
-       fn handle_channel_update(&self, msg: &ChannelUpdate) -> Result<bool, HandleError>;
-       /// Handle some updates to the route graph that we learned due to an outbound failed payment.
-       fn handle_htlc_fail_channel_update(&self, update: &HTLCFailChannelUpdate);
-       /// Gets a subset of the channel announcements and updates required to dump our routing table
-       /// to a remote node, starting at the short_channel_id indicated by starting_point and
-       /// including batch_amount entries.
-       fn get_next_channel_announcements(&self, starting_point: u64, batch_amount: u8) -> Vec<(ChannelAnnouncement, ChannelUpdate, ChannelUpdate)>;
-       /// Gets a subset of the node announcements required to dump our routing table to a remote node,
-       /// starting at the node *after* the provided publickey and including batch_amount entries.
-       /// If None is provided for starting_point, we start at the first node.
-       fn get_next_node_announcements(&self, starting_point: Option<&PublicKey>, batch_amount: u8) -> Vec<NodeAnnouncement>;
-}
-
-pub(crate) struct OnionRealm0HopData {
-       pub(crate) short_channel_id: u64,
-       pub(crate) amt_to_forward: u64,
-       pub(crate) outgoing_cltv_value: u32,
-       // 12 bytes of 0-padding
-}
-
-mod fuzzy_internal_msgs {
-       // These types aren't intended to be pub, but are exposed for direct fuzzing (as we deserialize
-       // them from untrusted input):
-
-       use super::OnionRealm0HopData;
-       pub struct OnionHopData {
-               pub(crate) realm: u8,
-               pub(crate) data: OnionRealm0HopData,
-               pub(crate) hmac: [u8; 32],
-       }
-
-       pub struct DecodedOnionErrorPacket {
-               pub(crate) hmac: [u8; 32],
-               pub(crate) failuremsg: Vec<u8>,
-               pub(crate) pad: Vec<u8>,
-       }
-}
-#[cfg(feature = "fuzztarget")]
-pub use self::fuzzy_internal_msgs::*;
-#[cfg(not(feature = "fuzztarget"))]
-pub(crate) use self::fuzzy_internal_msgs::*;
-
-#[derive(Clone)]
-pub(crate) struct OnionPacket {
-       pub(crate) version: u8,
-       /// In order to ensure we always return an error on Onion decode in compliance with BOLT 4, we
-       /// have to deserialize OnionPackets contained in UpdateAddHTLCs even if the ephemeral public
-       /// key (here) is bogus, so we hold a Result instead of a PublicKey as we'd like.
-       pub(crate) public_key: Result<PublicKey, secp256k1::Error>,
-       pub(crate) hop_data: [u8; 20*65],
-       pub(crate) hmac: [u8; 32],
-}
-
-impl PartialEq for OnionPacket {
-       fn eq(&self, other: &OnionPacket) -> bool {
-               for (i, j) in self.hop_data.iter().zip(other.hop_data.iter()) {
-                       if i != j { return false; }
-               }
-               self.version == other.version &&
-                       self.public_key == other.public_key &&
-                       self.hmac == other.hmac
-       }
-}
-
-#[derive(Clone, PartialEq)]
-pub(crate) struct OnionErrorPacket {
-       // This really should be a constant size slice, but the spec lets these things be up to 128KB?
-       // (TODO) We limit it in decode to much lower...
-       pub(crate) data: Vec<u8>,
-}
-
-impl Error for DecodeError {
-       fn description(&self) -> &str {
-               match *self {
-                       DecodeError::UnknownVersion => "Unknown realm byte in Onion packet",
-                       DecodeError::UnknownRequiredFeature => "Unknown required feature preventing decode",
-                       DecodeError::InvalidValue => "Nonsense bytes didn't map to the type they were interpreted as",
-                       DecodeError::ShortRead => "Packet extended beyond the provided bytes",
-                       DecodeError::ExtraAddressesPerType => "More than one address of a single type",
-                       DecodeError::BadLengthDescriptor => "A length descriptor in the packet didn't describe the later data correctly",
-                       DecodeError::Io(ref e) => e.description(),
-               }
-       }
-}
-impl fmt::Display for DecodeError {
-       fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-               f.write_str(self.description())
-       }
-}
-
-impl fmt::Debug for HandleError {
-       fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-               f.write_str(self.err)
-       }
-}
-
-impl From<::std::io::Error> for DecodeError {
-       fn from(e: ::std::io::Error) -> Self {
-               if e.kind() == ::std::io::ErrorKind::UnexpectedEof {
-                       DecodeError::ShortRead
-               } else {
-                       DecodeError::Io(e)
-               }
-       }
-}
-
-impl Writeable for OptionalField<Script> {
-       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
-               match *self {
-                       OptionalField::Present(ref script) => {
-                               // Note that Writeable for script includes the 16-bit length tag for us
-                               script.write(w)?;
-                       },
-                       OptionalField::Absent => {}
-               }
-               Ok(())
-       }
-}
-
-impl<R: Read> Readable<R> for OptionalField<Script> {
-       fn read(r: &mut R) -> Result<Self, DecodeError> {
-               match <u16 as Readable<R>>::read(r) {
-                       Ok(len) => {
-                               let mut buf = vec![0; len as usize];
-                               r.read_exact(&mut buf)?;
-                               Ok(OptionalField::Present(Script::from(buf)))
-                       },
-                       Err(DecodeError::ShortRead) => Ok(OptionalField::Absent),
-                       Err(e) => Err(e)
-               }
-       }
-}
-
-impl_writeable_len_match!(AcceptChannel, {
-               {AcceptChannel{ shutdown_scriptpubkey: OptionalField::Present(ref script), .. }, 270 + 2 + script.len()},
-               {_, 270}
-       }, {
-       temporary_channel_id,
-       dust_limit_satoshis,
-       max_htlc_value_in_flight_msat,
-       channel_reserve_satoshis,
-       htlc_minimum_msat,
-       minimum_depth,
-       to_self_delay,
-       max_accepted_htlcs,
-       funding_pubkey,
-       revocation_basepoint,
-       payment_basepoint,
-       delayed_payment_basepoint,
-       htlc_basepoint,
-       first_per_commitment_point,
-       shutdown_scriptpubkey
-});
-
-impl_writeable!(AnnouncementSignatures, 32+8+64*2, {
-       channel_id,
-       short_channel_id,
-       node_signature,
-       bitcoin_signature
-});
-
-impl Writeable for ChannelReestablish {
-       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
-               w.size_hint(if let OptionalField::Present(..) = self.data_loss_protect { 32+2*8+33+32 } else { 32+2*8 });
-               self.channel_id.write(w)?;
-               self.next_local_commitment_number.write(w)?;
-               self.next_remote_commitment_number.write(w)?;
-               match self.data_loss_protect {
-                       OptionalField::Present(ref data_loss_protect) => {
-                               (*data_loss_protect).your_last_per_commitment_secret.write(w)?;
-                               (*data_loss_protect).my_current_per_commitment_point.write(w)?;
-                       },
-                       OptionalField::Absent => {}
-               }
-               Ok(())
-       }
-}
-
-impl<R: Read> Readable<R> for ChannelReestablish{
-       fn read(r: &mut R) -> Result<Self, DecodeError> {
-               Ok(Self {
-                       channel_id: Readable::read(r)?,
-                       next_local_commitment_number: Readable::read(r)?,
-                       next_remote_commitment_number: Readable::read(r)?,
-                       data_loss_protect: {
-                               match <[u8; 32] as Readable<R>>::read(r) {
-                                       Ok(your_last_per_commitment_secret) =>
-                                               OptionalField::Present(DataLossProtect {
-                                                       your_last_per_commitment_secret,
-                                                       my_current_per_commitment_point: Readable::read(r)?,
-                                               }),
-                                       Err(DecodeError::ShortRead) => OptionalField::Absent,
-                                       Err(e) => return Err(e)
-                               }
-                       }
-               })
-       }
-}
-
-impl_writeable!(ClosingSigned, 32+8+64, {
-       channel_id,
-       fee_satoshis,
-       signature
-});
-
-impl_writeable_len_match!(CommitmentSigned, {
-               { CommitmentSigned { ref htlc_signatures, .. }, 32+64+2+htlc_signatures.len()*64 }
-       }, {
-       channel_id,
-       signature,
-       htlc_signatures
-});
-
-impl_writeable_len_match!(DecodedOnionErrorPacket, {
-               { DecodedOnionErrorPacket { ref failuremsg, ref pad, .. }, 32 + 4 + failuremsg.len() + pad.len() }
-       }, {
-       hmac,
-       failuremsg,
-       pad
-});
-
-impl_writeable!(FundingCreated, 32+32+2+64, {
-       temporary_channel_id,
-       funding_txid,
-       funding_output_index,
-       signature
-});
-
-impl_writeable!(FundingSigned, 32+64, {
-       channel_id,
-       signature
-});
-
-impl_writeable!(FundingLocked, 32+33, {
-       channel_id,
-       next_per_commitment_point
-});
-
-impl_writeable_len_match!(GlobalFeatures, {
-               { GlobalFeatures { ref flags }, flags.len() + 2 }
-       }, {
-       flags
-});
-
-impl_writeable_len_match!(LocalFeatures, {
-               { LocalFeatures { ref flags }, flags.len() + 2 }
-       }, {
-       flags
-});
-
-impl_writeable_len_match!(Init, {
-               { Init { ref global_features, ref local_features }, global_features.flags.len() + local_features.flags.len() + 4 }
-       }, {
-       global_features,
-       local_features
-});
-
-impl_writeable_len_match!(OpenChannel, {
-               { OpenChannel { shutdown_scriptpubkey: OptionalField::Present(ref script), .. }, 319 + 2 + script.len() },
-               { _, 319 }
-       }, {
-       chain_hash,
-       temporary_channel_id,
-       funding_satoshis,
-       push_msat,
-       dust_limit_satoshis,
-       max_htlc_value_in_flight_msat,
-       channel_reserve_satoshis,
-       htlc_minimum_msat,
-       feerate_per_kw,
-       to_self_delay,
-       max_accepted_htlcs,
-       funding_pubkey,
-       revocation_basepoint,
-       payment_basepoint,
-       delayed_payment_basepoint,
-       htlc_basepoint,
-       first_per_commitment_point,
-       channel_flags,
-       shutdown_scriptpubkey
-});
-
-impl_writeable!(RevokeAndACK, 32+32+33, {
-       channel_id,
-       per_commitment_secret,
-       next_per_commitment_point
-});
-
-impl_writeable_len_match!(Shutdown, {
-               { Shutdown { ref scriptpubkey, .. }, 32 + 2 + scriptpubkey.len() }
-       }, {
-       channel_id,
-       scriptpubkey
-});
-
-impl_writeable_len_match!(UpdateFailHTLC, {
-               { UpdateFailHTLC { ref reason, .. }, 32 + 10 + reason.data.len() }
-       }, {
-       channel_id,
-       htlc_id,
-       reason
-});
-
-impl_writeable!(UpdateFailMalformedHTLC, 32+8+32+2, {
-       channel_id,
-       htlc_id,
-       sha256_of_onion,
-       failure_code
-});
-
-impl_writeable!(UpdateFee, 32+4, {
-       channel_id,
-       feerate_per_kw
-});
-
-impl_writeable!(UpdateFulfillHTLC, 32+8+32, {
-       channel_id,
-       htlc_id,
-       payment_preimage
-});
-
-impl_writeable_len_match!(OnionErrorPacket, {
-               { OnionErrorPacket { ref data, .. }, 2 + data.len() }
-       }, {
-       data
-});
-
-impl Writeable for OnionPacket {
-       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
-               w.size_hint(1 + 33 + 20*65 + 32);
-               self.version.write(w)?;
-               match self.public_key {
-                       Ok(pubkey) => pubkey.write(w)?,
-                       Err(_) => [0u8;33].write(w)?,
-               }
-               w.write_all(&self.hop_data)?;
-               self.hmac.write(w)?;
-               Ok(())
-       }
-}
-
-impl<R: Read> Readable<R> for OnionPacket {
-       fn read(r: &mut R) -> Result<Self, DecodeError> {
-               Ok(OnionPacket {
-                       version: Readable::read(r)?,
-                       public_key: {
-                               let mut buf = [0u8;33];
-                               r.read_exact(&mut buf)?;
-                               PublicKey::from_slice(&buf)
-                       },
-                       hop_data: Readable::read(r)?,
-                       hmac: Readable::read(r)?,
-               })
-       }
-}
-
-impl_writeable!(UpdateAddHTLC, 32+8+8+32+4+1366, {
-       channel_id,
-       htlc_id,
-       amount_msat,
-       payment_hash,
-       cltv_expiry,
-       onion_routing_packet
-});
-
-impl Writeable for OnionRealm0HopData {
-       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
-               w.size_hint(32);
-               self.short_channel_id.write(w)?;
-               self.amt_to_forward.write(w)?;
-               self.outgoing_cltv_value.write(w)?;
-               w.write_all(&[0;12])?;
-               Ok(())
-       }
-}
-
-impl<R: Read> Readable<R> for OnionRealm0HopData {
-       fn read(r: &mut R) -> Result<Self, DecodeError> {
-               Ok(OnionRealm0HopData {
-                       short_channel_id: Readable::read(r)?,
-                       amt_to_forward: Readable::read(r)?,
-                       outgoing_cltv_value: {
-                               let v: u32 = Readable::read(r)?;
-                               r.read_exact(&mut [0; 12])?;
-                               v
-                       }
-               })
-       }
-}
-
-impl Writeable for OnionHopData {
-       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
-               w.size_hint(65);
-               self.realm.write(w)?;
-               self.data.write(w)?;
-               self.hmac.write(w)?;
-               Ok(())
-       }
-}
-
-impl<R: Read> Readable<R> for OnionHopData {
-       fn read(r: &mut R) -> Result<Self, DecodeError> {
-               Ok(OnionHopData {
-                       realm: {
-                               let r: u8 = Readable::read(r)?;
-                               if r != 0 {
-                                       return Err(DecodeError::UnknownVersion);
-                               }
-                               r
-                       },
-                       data: Readable::read(r)?,
-                       hmac: Readable::read(r)?,
-               })
-       }
-}
-
-impl Writeable for Ping {
-       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
-               w.size_hint(self.byteslen as usize + 4);
-               self.ponglen.write(w)?;
-               vec![0u8; self.byteslen as usize].write(w)?; // size-unchecked write
-               Ok(())
-       }
-}
-
-impl<R: Read> Readable<R> for Ping {
-       fn read(r: &mut R) -> Result<Self, DecodeError> {
-               Ok(Ping {
-                       ponglen: Readable::read(r)?,
-                       byteslen: {
-                               let byteslen = Readable::read(r)?;
-                               r.read_exact(&mut vec![0u8; byteslen as usize][..])?;
-                               byteslen
-                       }
-               })
-       }
-}
-
-impl Writeable for Pong {
-       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
-               w.size_hint(self.byteslen as usize + 2);
-               vec![0u8; self.byteslen as usize].write(w)?; // size-unchecked write
-               Ok(())
-       }
-}
-
-impl<R: Read> Readable<R> for Pong {
-       fn read(r: &mut R) -> Result<Self, DecodeError> {
-               Ok(Pong {
-                       byteslen: {
-                               let byteslen = Readable::read(r)?;
-                               r.read_exact(&mut vec![0u8; byteslen as usize][..])?;
-                               byteslen
-                       }
-               })
-       }
-}
-
-impl Writeable for UnsignedChannelAnnouncement {
-       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
-               w.size_hint(2 + 2*32 + 4*33 + self.features.flags.len() + self.excess_data.len());
-               self.features.write(w)?;
-               self.chain_hash.write(w)?;
-               self.short_channel_id.write(w)?;
-               self.node_id_1.write(w)?;
-               self.node_id_2.write(w)?;
-               self.bitcoin_key_1.write(w)?;
-               self.bitcoin_key_2.write(w)?;
-               w.write_all(&self.excess_data[..])?;
-               Ok(())
-       }
-}
-
-impl<R: Read> Readable<R> for UnsignedChannelAnnouncement {
-       fn read(r: &mut R) -> Result<Self, DecodeError> {
-               Ok(Self {
-                       features: {
-                               let f: GlobalFeatures = Readable::read(r)?;
-                               if f.requires_unknown_bits() {
-                                       return Err(DecodeError::UnknownRequiredFeature);
-                               }
-                               f
-                       },
-                       chain_hash: Readable::read(r)?,
-                       short_channel_id: Readable::read(r)?,
-                       node_id_1: Readable::read(r)?,
-                       node_id_2: Readable::read(r)?,
-                       bitcoin_key_1: Readable::read(r)?,
-                       bitcoin_key_2: Readable::read(r)?,
-                       excess_data: {
-                               let mut excess_data = vec![];
-                               r.read_to_end(&mut excess_data)?;
-                               excess_data
-                       },
-               })
-       }
-}
-
-impl_writeable_len_match!(ChannelAnnouncement, {
-               { ChannelAnnouncement { contents: UnsignedChannelAnnouncement {ref features, ref excess_data, ..}, .. },
-                       2 + 2*32 + 4*33 + features.flags.len() + excess_data.len() + 4*64 }
-       }, {
-       node_signature_1,
-       node_signature_2,
-       bitcoin_signature_1,
-       bitcoin_signature_2,
-       contents
-});
-
-impl Writeable for UnsignedChannelUpdate {
-       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
-               w.size_hint(64 + self.excess_data.len());
-               self.chain_hash.write(w)?;
-               self.short_channel_id.write(w)?;
-               self.timestamp.write(w)?;
-               self.flags.write(w)?;
-               self.cltv_expiry_delta.write(w)?;
-               self.htlc_minimum_msat.write(w)?;
-               self.fee_base_msat.write(w)?;
-               self.fee_proportional_millionths.write(w)?;
-               w.write_all(&self.excess_data[..])?;
-               Ok(())
-       }
-}
-
-impl<R: Read> Readable<R> for UnsignedChannelUpdate {
-       fn read(r: &mut R) -> Result<Self, DecodeError> {
-               Ok(Self {
-                       chain_hash: Readable::read(r)?,
-                       short_channel_id: Readable::read(r)?,
-                       timestamp: Readable::read(r)?,
-                       flags: Readable::read(r)?,
-                       cltv_expiry_delta: Readable::read(r)?,
-                       htlc_minimum_msat: Readable::read(r)?,
-                       fee_base_msat: Readable::read(r)?,
-                       fee_proportional_millionths: Readable::read(r)?,
-                       excess_data: {
-                               let mut excess_data = vec![];
-                               r.read_to_end(&mut excess_data)?;
-                               excess_data
-                       },
-               })
-       }
-}
-
-impl_writeable_len_match!(ChannelUpdate, {
-               { ChannelUpdate { contents: UnsignedChannelUpdate {ref excess_data, ..}, .. },
-                       64 + excess_data.len() + 64 }
-       }, {
-       signature,
-       contents
-});
-
-impl Writeable for ErrorMessage {
-       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
-               w.size_hint(32 + 2 + self.data.len());
-               self.channel_id.write(w)?;
-               (self.data.len() as u16).write(w)?;
-               w.write_all(self.data.as_bytes())?;
-               Ok(())
-       }
-}
-
-impl<R: Read> Readable<R> for ErrorMessage {
-       fn read(r: &mut R) -> Result<Self, DecodeError> {
-               Ok(Self {
-                       channel_id: Readable::read(r)?,
-                       data: {
-                               let mut sz: usize = <u16 as Readable<R>>::read(r)? as usize;
-                               let mut data = vec![];
-                               let data_len = r.read_to_end(&mut data)?;
-                               sz = cmp::min(data_len, sz);
-                               match String::from_utf8(data[..sz as usize].to_vec()) {
-                                       Ok(s) => s,
-                                       Err(_) => return Err(DecodeError::InvalidValue),
-                               }
-                       }
-               })
-       }
-}
-
-impl Writeable for UnsignedNodeAnnouncement {
-       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
-               w.size_hint(64 + 76 + self.features.flags.len() + self.addresses.len()*38 + self.excess_address_data.len() + self.excess_data.len());
-               self.features.write(w)?;
-               self.timestamp.write(w)?;
-               self.node_id.write(w)?;
-               w.write_all(&self.rgb)?;
-               self.alias.write(w)?;
-
-               let mut addrs_to_encode = self.addresses.clone();
-               addrs_to_encode.sort_unstable_by(|a, b| { a.get_id().cmp(&b.get_id()) });
-               addrs_to_encode.dedup_by(|a, b| { a.get_id() == b.get_id() });
-               let mut addr_len = 0;
-               for addr in &addrs_to_encode {
-                       addr_len += 1 + addr.len();
-               }
-               (addr_len + self.excess_address_data.len() as u16).write(w)?;
-               for addr in addrs_to_encode {
-                       addr.write(w)?;
-               }
-               w.write_all(&self.excess_address_data[..])?;
-               w.write_all(&self.excess_data[..])?;
-               Ok(())
-       }
-}
-
-impl<R: Read> Readable<R> for UnsignedNodeAnnouncement {
-       fn read(r: &mut R) -> Result<Self, DecodeError> {
-               let features: GlobalFeatures = Readable::read(r)?;
-               if features.requires_unknown_bits() {
-                       return Err(DecodeError::UnknownRequiredFeature);
-               }
-               let timestamp: u32 = Readable::read(r)?;
-               let node_id: PublicKey = Readable::read(r)?;
-               let mut rgb = [0; 3];
-               r.read_exact(&mut rgb)?;
-               let alias: [u8; 32] = Readable::read(r)?;
-
-               let addr_len: u16 = Readable::read(r)?;
-               let mut addresses: Vec<NetAddress> = Vec::with_capacity(4);
-               let mut addr_readpos = 0;
-               let mut excess = false;
-               let mut excess_byte = 0;
-               loop {
-                       if addr_len <= addr_readpos { break; }
-                       match Readable::read(r) {
-                               Ok(Ok(addr)) => {
-                                       match addr {
-                                               NetAddress::IPv4 { .. } => {
-                                                       if addresses.len() > 0 {
-                                                               return Err(DecodeError::ExtraAddressesPerType);
-                                                       }
-                                               },
-                                               NetAddress::IPv6 { .. } => {
-                                                       if addresses.len() > 1 || (addresses.len() == 1 && addresses[0].get_id() != 1) {
-                                                               return Err(DecodeError::ExtraAddressesPerType);
-                                                       }
-                                               },
-                                               NetAddress::OnionV2 { .. } => {
-                                                       if addresses.len() > 2 || (addresses.len() > 0 && addresses.last().unwrap().get_id() > 2) {
-                                                               return Err(DecodeError::ExtraAddressesPerType);
-                                                       }
-                                               },
-                                               NetAddress::OnionV3 { .. } => {
-                                                       if addresses.len() > 3 || (addresses.len() > 0 && addresses.last().unwrap().get_id() > 3) {
-                                                               return Err(DecodeError::ExtraAddressesPerType);
-                                                       }
-                                               },
-                                       }
-                                       if addr_len < addr_readpos + 1 + addr.len() {
-                                               return Err(DecodeError::BadLengthDescriptor);
-                                       }
-                                       addr_readpos += (1 + addr.len()) as u16;
-                                       addresses.push(addr);
-                               },
-                               Ok(Err(unknown_descriptor)) => {
-                                       excess = true;
-                                       excess_byte = unknown_descriptor;
-                                       break;
-                               },
-                               Err(DecodeError::ShortRead) => return Err(DecodeError::BadLengthDescriptor),
-                               Err(e) => return Err(e),
-                       }
-               }
-
-               let mut excess_data = vec![];
-               let excess_address_data = if addr_readpos < addr_len {
-                       let mut excess_address_data = vec![0; (addr_len - addr_readpos) as usize];
-                       r.read_exact(&mut excess_address_data[if excess { 1 } else { 0 }..])?;
-                       if excess {
-                               excess_address_data[0] = excess_byte;
-                       }
-                       excess_address_data
-               } else {
-                       if excess {
-                               excess_data.push(excess_byte);
-                       }
-                       Vec::new()
-               };
-               r.read_to_end(&mut excess_data)?;
-               Ok(UnsignedNodeAnnouncement {
-                       features,
-                       timestamp,
-                       node_id,
-                       rgb,
-                       alias,
-                       addresses,
-                       excess_address_data,
-                       excess_data,
-               })
-       }
-}
-
-impl_writeable_len_match!(NodeAnnouncement, {
-               { NodeAnnouncement { contents: UnsignedNodeAnnouncement { ref features, ref addresses, ref excess_address_data, ref excess_data, ..}, .. },
-                       64 + 76 + features.flags.len() + addresses.len()*38 + excess_address_data.len() + excess_data.len() }
-       }, {
-       signature,
-       contents
-});
-
-#[cfg(test)]
-mod tests {
-       use hex;
-       use ln::msgs;
-       use ln::msgs::{GlobalFeatures, LocalFeatures, OptionalField, OnionErrorPacket};
-       use ln::channelmanager::{PaymentPreimage, PaymentHash};
-       use util::ser::Writeable;
-
-       use bitcoin_hashes::sha256d::Hash as Sha256dHash;
-       use bitcoin_hashes::hex::FromHex;
-       use bitcoin::util::address::Address;
-       use bitcoin::network::constants::Network;
-       use bitcoin::blockdata::script::Builder;
-       use bitcoin::blockdata::opcodes;
-
-       use secp256k1::key::{PublicKey,SecretKey};
-       use secp256k1::{Secp256k1, Message};
-
-       #[test]
-       fn encoding_channel_reestablish_no_secret() {
-               let cr = msgs::ChannelReestablish {
-                       channel_id: [4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0],
-                       next_local_commitment_number: 3,
-                       next_remote_commitment_number: 4,
-                       data_loss_protect: OptionalField::Absent,
-               };
-
-               let encoded_value = cr.encode();
-               assert_eq!(
-                       encoded_value,
-                       vec![4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4]
-               );
-       }
-
-       #[test]
-       fn encoding_channel_reestablish_with_secret() {
-               let public_key = {
-                       let secp_ctx = Secp256k1::new();
-                       PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap())
-               };
-
-               let cr = msgs::ChannelReestablish {
-                       channel_id: [4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0],
-                       next_local_commitment_number: 3,
-                       next_remote_commitment_number: 4,
-                       data_loss_protect: OptionalField::Present(msgs::DataLossProtect { your_last_per_commitment_secret: [9;32], my_current_per_commitment_point: public_key}),
-               };
-
-               let encoded_value = cr.encode();
-               assert_eq!(
-                       encoded_value,
-                       vec![4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 3, 27, 132, 197, 86, 123, 18, 100, 64, 153, 93, 62, 213, 170, 186, 5, 101, 215, 30, 24, 52, 96, 72, 25, 255, 156, 23, 245, 233, 213, 221, 7, 143]
-               );
-       }
-
-       macro_rules! get_keys_from {
-               ($slice: expr, $secp_ctx: expr) => {
-                       {
-                               let privkey = SecretKey::from_slice(&hex::decode($slice).unwrap()[..]).unwrap();
-                               let pubkey = PublicKey::from_secret_key(&$secp_ctx, &privkey);
-                               (privkey, pubkey)
-                       }
-               }
-       }
-
-       macro_rules! get_sig_on {
-               ($privkey: expr, $ctx: expr, $string: expr) => {
-                       {
-                               let sighash = Message::from_slice(&$string.into_bytes()[..]).unwrap();
-                               $ctx.sign(&sighash, &$privkey)
-                       }
-               }
-       }
-
-       #[test]
-       fn encoding_announcement_signatures() {
-               let secp_ctx = Secp256k1::new();
-               let (privkey, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
-               let sig_1 = get_sig_on!(privkey, secp_ctx, String::from("01010101010101010101010101010101"));
-               let sig_2 = get_sig_on!(privkey, secp_ctx, String::from("02020202020202020202020202020202"));
-               let announcement_signatures = msgs::AnnouncementSignatures {
-                       channel_id: [4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0],
-                       short_channel_id: 2316138423780173,
-                       node_signature: sig_1,
-                       bitcoin_signature: sig_2,
-               };
-
-               let encoded_value = announcement_signatures.encode();
-               assert_eq!(encoded_value, hex::decode("040000000000000005000000000000000600000000000000070000000000000000083a840000034dd977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073acf9953cef4700860f5967838eba2bae89288ad188ebf8b20bf995c3ea53a26df1876d0a3a0e13172ba286a673140190c02ba9da60a2e43a745188c8a83c7f3ef").unwrap());
-       }
-
-       fn do_encoding_channel_announcement(unknown_features_bits: bool, non_bitcoin_chain_hash: bool, excess_data: bool) {
-               let secp_ctx = Secp256k1::new();
-               let (privkey_1, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
-               let (privkey_2, pubkey_2) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202", secp_ctx);
-               let (privkey_3, pubkey_3) = get_keys_from!("0303030303030303030303030303030303030303030303030303030303030303", secp_ctx);
-               let (privkey_4, pubkey_4) = get_keys_from!("0404040404040404040404040404040404040404040404040404040404040404", secp_ctx);
-               let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
-               let sig_2 = get_sig_on!(privkey_2, secp_ctx, String::from("01010101010101010101010101010101"));
-               let sig_3 = get_sig_on!(privkey_3, secp_ctx, String::from("01010101010101010101010101010101"));
-               let sig_4 = get_sig_on!(privkey_4, secp_ctx, String::from("01010101010101010101010101010101"));
-               let mut features = GlobalFeatures::new();
-               if unknown_features_bits {
-                       features.flags = vec![0xFF, 0xFF];
-               }
-               let unsigned_channel_announcement = msgs::UnsignedChannelAnnouncement {
-                       features,
-                       chain_hash: if !non_bitcoin_chain_hash { Sha256dHash::from_hex("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap() } else { Sha256dHash::from_hex("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943").unwrap() },
-                       short_channel_id: 2316138423780173,
-                       node_id_1: pubkey_1,
-                       node_id_2: pubkey_2,
-                       bitcoin_key_1: pubkey_3,
-                       bitcoin_key_2: pubkey_4,
-                       excess_data: if excess_data { vec![10, 0, 0, 20, 0, 0, 30, 0, 0, 40] } else { Vec::new() },
-               };
-               let channel_announcement = msgs::ChannelAnnouncement {
-                       node_signature_1: sig_1,
-                       node_signature_2: sig_2,
-                       bitcoin_signature_1: sig_3,
-                       bitcoin_signature_2: sig_4,
-                       contents: unsigned_channel_announcement,
-               };
-               let encoded_value = channel_announcement.encode();
-               let mut target_value = hex::decode("d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a1735b6a427e80d5fe7cd90a2f4ee08dc9c27cda7c35a4172e5d85b12c49d4232537e98f9b1f3c5e6989a8b9644e90e8918127680dbd0d4043510840fc0f1e11a216c280b5395a2546e7e4b2663e04f811622f15a4f91e83aa2e92ba2a573c139142c54ae63072a1ec1ee7dc0c04bde5c847806172aa05c92c22ae8e308d1d2692b12cc195ce0a2d1bda6a88befa19fa07f51caa75ce83837f28965600b8aacab0855ffb0e741ec5f7c41421e9829a9d48611c8c831f71be5ea73e66594977ffd").unwrap();
-               if unknown_features_bits {
-                       target_value.append(&mut hex::decode("0002ffff").unwrap());
-               } else {
-                       target_value.append(&mut hex::decode("0000").unwrap());
-               }
-               if non_bitcoin_chain_hash {
-                       target_value.append(&mut hex::decode("43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea330900000000").unwrap());
-               } else {
-                       target_value.append(&mut hex::decode("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f").unwrap());
-               }
-               target_value.append(&mut hex::decode("00083a840000034d031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f024d4b6cd1361032ca9bd2aeb9d900aa4d45d9ead80ac9423374c451a7254d076602531fe6068134503d2723133227c867ac8fa6c83c537e9a44c3c5bdbdcb1fe33703462779ad4aad39514614751a71085f2f10e1c7a593e4e030efb5b8721ce55b0b").unwrap());
-               if excess_data {
-                       target_value.append(&mut hex::decode("0a00001400001e000028").unwrap());
-               }
-               assert_eq!(encoded_value, target_value);
-       }
-
-       #[test]
-       fn encoding_channel_announcement() {
-               do_encoding_channel_announcement(false, false, false);
-               do_encoding_channel_announcement(true, false, false);
-               do_encoding_channel_announcement(true, true, false);
-               do_encoding_channel_announcement(true, true, true);
-               do_encoding_channel_announcement(false, true, true);
-               do_encoding_channel_announcement(false, false, true);
-               do_encoding_channel_announcement(false, true, false);
-               do_encoding_channel_announcement(true, false, true);
-       }
-
-       fn do_encoding_node_announcement(unknown_features_bits: bool, ipv4: bool, ipv6: bool, onionv2: bool, onionv3: bool, excess_address_data: bool, excess_data: bool) {
-               let secp_ctx = Secp256k1::new();
-               let (privkey_1, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
-               let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
-               let mut features = GlobalFeatures::new();
-               if unknown_features_bits {
-                       features.flags = vec![0xFF, 0xFF];
-               }
-               let mut addresses = Vec::new();
-               if ipv4 {
-                       addresses.push(msgs::NetAddress::IPv4 {
-                               addr: [255, 254, 253, 252],
-                               port: 9735
-                       });
-               }
-               if ipv6 {
-                       addresses.push(msgs::NetAddress::IPv6 {
-                               addr: [255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 245, 244, 243, 242, 241, 240],
-                               port: 9735
-                       });
-               }
-               if onionv2 {
-                       addresses.push(msgs::NetAddress::OnionV2 {
-                               addr: [255, 254, 253, 252, 251, 250, 249, 248, 247, 246],
-                               port: 9735
-                       });
-               }
-               if onionv3 {
-                       addresses.push(msgs::NetAddress::OnionV3 {
-                               ed25519_pubkey: [255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 245, 244, 243, 242, 241, 240, 239, 238, 237, 236, 235, 234, 233, 232, 231, 230, 229, 228, 227, 226, 225, 224],
-                               checksum: 32,
-                               version: 16,
-                               port: 9735
-                       });
-               }
-               let mut addr_len = 0;
-               for addr in &addresses {
-                       addr_len += addr.len() + 1;
-               }
-               let unsigned_node_announcement = msgs::UnsignedNodeAnnouncement {
-                       features,
-                       timestamp: 20190119,
-                       node_id: pubkey_1,
-                       rgb: [32; 3],
-                       alias: [16;32],
-                       addresses,
-                       excess_address_data: if excess_address_data { vec![33, 108, 40, 11, 83, 149, 162, 84, 110, 126, 75, 38, 99, 224, 79, 129, 22, 34, 241, 90, 79, 146, 232, 58, 162, 233, 43, 162, 165, 115, 193, 57, 20, 44, 84, 174, 99, 7, 42, 30, 193, 238, 125, 192, 192, 75, 222, 92, 132, 120, 6, 23, 42, 160, 92, 146, 194, 42, 232, 227, 8, 209, 210, 105] } else { Vec::new() },
-                       excess_data: if excess_data { vec![59, 18, 204, 25, 92, 224, 162, 209, 189, 166, 168, 139, 239, 161, 159, 160, 127, 81, 202, 167, 92, 232, 56, 55, 242, 137, 101, 96, 11, 138, 172, 171, 8, 85, 255, 176, 231, 65, 236, 95, 124, 65, 66, 30, 152, 41, 169, 212, 134, 17, 200, 200, 49, 247, 27, 229, 234, 115, 230, 101, 148, 151, 127, 253] } else { Vec::new() },
-               };
-               addr_len += unsigned_node_announcement.excess_address_data.len() as u16;
-               let node_announcement = msgs::NodeAnnouncement {
-                       signature: sig_1,
-                       contents: unsigned_node_announcement,
-               };
-               let encoded_value = node_announcement.encode();
-               let mut target_value = hex::decode("d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
-               if unknown_features_bits {
-                       target_value.append(&mut hex::decode("0002ffff").unwrap());
-               } else {
-                       target_value.append(&mut hex::decode("0000").unwrap());
-               }
-               target_value.append(&mut hex::decode("013413a7031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f2020201010101010101010101010101010101010101010101010101010101010101010").unwrap());
-               target_value.append(&mut vec![(addr_len >> 8) as u8, addr_len as u8]);
-               if ipv4 {
-                       target_value.append(&mut hex::decode("01fffefdfc2607").unwrap());
-               }
-               if ipv6 {
-                       target_value.append(&mut hex::decode("02fffefdfcfbfaf9f8f7f6f5f4f3f2f1f02607").unwrap());
-               }
-               if onionv2 {
-                       target_value.append(&mut hex::decode("03fffefdfcfbfaf9f8f7f62607").unwrap());
-               }
-               if onionv3 {
-                       target_value.append(&mut hex::decode("04fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0efeeedecebeae9e8e7e6e5e4e3e2e1e00020102607").unwrap());
-               }
-               if excess_address_data {
-                       target_value.append(&mut hex::decode("216c280b5395a2546e7e4b2663e04f811622f15a4f92e83aa2e92ba2a573c139142c54ae63072a1ec1ee7dc0c04bde5c847806172aa05c92c22ae8e308d1d269").unwrap());
-               }
-               if excess_data {
-                       target_value.append(&mut hex::decode("3b12cc195ce0a2d1bda6a88befa19fa07f51caa75ce83837f28965600b8aacab0855ffb0e741ec5f7c41421e9829a9d48611c8c831f71be5ea73e66594977ffd").unwrap());
-               }
-               assert_eq!(encoded_value, target_value);
-       }
-
-       #[test]
-       fn encoding_node_announcement() {
-               do_encoding_node_announcement(true, true, true, true, true, true, true);
-               do_encoding_node_announcement(false, false, false, false, false, false, false);
-               do_encoding_node_announcement(false, true, false, false, false, false, false);
-               do_encoding_node_announcement(false, false, true, false, false, false, false);
-               do_encoding_node_announcement(false, false, false, true, false, false, false);
-               do_encoding_node_announcement(false, false, false, false, true, false, false);
-               do_encoding_node_announcement(false, false, false, false, false, true, false);
-               do_encoding_node_announcement(false, true, false, true, false, true, false);
-               do_encoding_node_announcement(false, false, true, false, true, false, false);
-       }
-
-       fn do_encoding_channel_update(non_bitcoin_chain_hash: bool, direction: bool, disable: bool, htlc_maximum_msat: bool) {
-               let secp_ctx = Secp256k1::new();
-               let (privkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
-               let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
-               let unsigned_channel_update = msgs::UnsignedChannelUpdate {
-                       chain_hash: if !non_bitcoin_chain_hash { Sha256dHash::from_hex("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap() } else { Sha256dHash::from_hex("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943").unwrap() },
-                       short_channel_id: 2316138423780173,
-                       timestamp: 20190119,
-                       flags: if direction { 1 } else { 0 } | if disable { 1 << 1 } else { 0 } | if htlc_maximum_msat { 1 << 8 } else { 0 },
-                       cltv_expiry_delta: 144,
-                       htlc_minimum_msat: 1000000,
-                       fee_base_msat: 10000,
-                       fee_proportional_millionths: 20,
-                       excess_data: if htlc_maximum_msat { vec![0, 0, 0, 0, 59, 154, 202, 0] } else { Vec::new() }
-               };
-               let channel_update = msgs::ChannelUpdate {
-                       signature: sig_1,
-                       contents: unsigned_channel_update
-               };
-               let encoded_value = channel_update.encode();
-               let mut target_value = hex::decode("d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
-               if non_bitcoin_chain_hash {
-                       target_value.append(&mut hex::decode("43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea330900000000").unwrap());
-               } else {
-                       target_value.append(&mut hex::decode("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f").unwrap());
-               }
-               target_value.append(&mut hex::decode("00083a840000034d013413a7").unwrap());
-               if htlc_maximum_msat {
-                       target_value.append(&mut hex::decode("01").unwrap());
-               } else {
-                       target_value.append(&mut hex::decode("00").unwrap());
-               }
-               target_value.append(&mut hex::decode("00").unwrap());
-               if direction {
-                       let flag = target_value.last_mut().unwrap();
-                       *flag = 1;
-               }
-               if disable {
-                       let flag = target_value.last_mut().unwrap();
-                       *flag = *flag | 1 << 1;
-               }
-               target_value.append(&mut hex::decode("009000000000000f42400000271000000014").unwrap());
-               if htlc_maximum_msat {
-                       target_value.append(&mut hex::decode("000000003b9aca00").unwrap());
-               }
-               assert_eq!(encoded_value, target_value);
-       }
-
-       #[test]
-       fn encoding_channel_update() {
-               do_encoding_channel_update(false, false, false, false);
-               do_encoding_channel_update(true, false, false, false);
-               do_encoding_channel_update(false, true, false, false);
-               do_encoding_channel_update(false, false, true, false);
-               do_encoding_channel_update(false, false, false, true);
-               do_encoding_channel_update(true, true, true, true);
-       }
-
-       fn do_encoding_open_channel(non_bitcoin_chain_hash: bool, random_bit: bool, shutdown: bool) {
-               let secp_ctx = Secp256k1::new();
-               let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
-               let (_, pubkey_2) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202", secp_ctx);
-               let (_, pubkey_3) = get_keys_from!("0303030303030303030303030303030303030303030303030303030303030303", secp_ctx);
-               let (_, pubkey_4) = get_keys_from!("0404040404040404040404040404040404040404040404040404040404040404", secp_ctx);
-               let (_, pubkey_5) = get_keys_from!("0505050505050505050505050505050505050505050505050505050505050505", secp_ctx);
-               let (_, pubkey_6) = get_keys_from!("0606060606060606060606060606060606060606060606060606060606060606", secp_ctx);
-               let open_channel = msgs::OpenChannel {
-                       chain_hash: if !non_bitcoin_chain_hash { Sha256dHash::from_hex("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap() } else { Sha256dHash::from_hex("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943").unwrap() },
-                       temporary_channel_id: [2; 32],
-                       funding_satoshis: 1311768467284833366,
-                       push_msat: 2536655962884945560,
-                       dust_limit_satoshis: 3608586615801332854,
-                       max_htlc_value_in_flight_msat: 8517154655701053848,
-                       channel_reserve_satoshis: 8665828695742877976,
-                       htlc_minimum_msat: 2316138423780173,
-                       feerate_per_kw: 821716,
-                       to_self_delay: 49340,
-                       max_accepted_htlcs: 49340,
-                       funding_pubkey: pubkey_1,
-                       revocation_basepoint: pubkey_2,
-                       payment_basepoint: pubkey_3,
-                       delayed_payment_basepoint: pubkey_4,
-                       htlc_basepoint: pubkey_5,
-                       first_per_commitment_point: pubkey_6,
-                       channel_flags: if random_bit { 1 << 5 } else { 0 },
-                       shutdown_scriptpubkey: if shutdown { OptionalField::Present(Address::p2pkh(&::bitcoin::PublicKey{compressed: true, key: pubkey_1}, Network::Testnet).script_pubkey()) } else { OptionalField::Absent }
-               };
-               let encoded_value = open_channel.encode();
-               let mut target_value = Vec::new();
-               if non_bitcoin_chain_hash {
-                       target_value.append(&mut hex::decode("43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea330900000000").unwrap());
-               } else {
-                       target_value.append(&mut hex::decode("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f").unwrap());
-               }
-               target_value.append(&mut hex::decode("02020202020202020202020202020202020202020202020202020202020202021234567890123456233403289122369832144668701144767633030896203198784335490624111800083a840000034d000c89d4c0bcc0bc031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f024d4b6cd1361032ca9bd2aeb9d900aa4d45d9ead80ac9423374c451a7254d076602531fe6068134503d2723133227c867ac8fa6c83c537e9a44c3c5bdbdcb1fe33703462779ad4aad39514614751a71085f2f10e1c7a593e4e030efb5b8721ce55b0b0362c0a046dacce86ddd0343c6d3c7c79c2208ba0d9c9cf24a6d046d21d21f90f703f006a18d5653c4edf5391ff23a61f03ff83d237e880ee61187fa9f379a028e0a").unwrap());
-               if random_bit {
-                       target_value.append(&mut hex::decode("20").unwrap());
-               } else {
-                       target_value.append(&mut hex::decode("00").unwrap());
-               }
-               if shutdown {
-                       target_value.append(&mut hex::decode("001976a91479b000887626b294a914501a4cd226b58b23598388ac").unwrap());
-               }
-               assert_eq!(encoded_value, target_value);
-       }
-
-       #[test]
-       fn encoding_open_channel() {
-               do_encoding_open_channel(false, false, false);
-               do_encoding_open_channel(true, false, false);
-               do_encoding_open_channel(false, true, false);
-               do_encoding_open_channel(false, false, true);
-               do_encoding_open_channel(true, true, true);
-       }
-
-       fn do_encoding_accept_channel(shutdown: bool) {
-               let secp_ctx = Secp256k1::new();
-               let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
-               let (_, pubkey_2) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202", secp_ctx);
-               let (_, pubkey_3) = get_keys_from!("0303030303030303030303030303030303030303030303030303030303030303", secp_ctx);
-               let (_, pubkey_4) = get_keys_from!("0404040404040404040404040404040404040404040404040404040404040404", secp_ctx);
-               let (_, pubkey_5) = get_keys_from!("0505050505050505050505050505050505050505050505050505050505050505", secp_ctx);
-               let (_, pubkey_6) = get_keys_from!("0606060606060606060606060606060606060606060606060606060606060606", secp_ctx);
-               let accept_channel = msgs::AcceptChannel {
-                       temporary_channel_id: [2; 32],
-                       dust_limit_satoshis: 1311768467284833366,
-                       max_htlc_value_in_flight_msat: 2536655962884945560,
-                       channel_reserve_satoshis: 3608586615801332854,
-                       htlc_minimum_msat: 2316138423780173,
-                       minimum_depth: 821716,
-                       to_self_delay: 49340,
-                       max_accepted_htlcs: 49340,
-                       funding_pubkey: pubkey_1,
-                       revocation_basepoint: pubkey_2,
-                       payment_basepoint: pubkey_3,
-                       delayed_payment_basepoint: pubkey_4,
-                       htlc_basepoint: pubkey_5,
-                       first_per_commitment_point: pubkey_6,
-                       shutdown_scriptpubkey: if shutdown { OptionalField::Present(Address::p2pkh(&::bitcoin::PublicKey{compressed: true, key: pubkey_1}, Network::Testnet).script_pubkey()) } else { OptionalField::Absent }
-               };
-               let encoded_value = accept_channel.encode();
-               let mut target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020212345678901234562334032891223698321446687011447600083a840000034d000c89d4c0bcc0bc031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f024d4b6cd1361032ca9bd2aeb9d900aa4d45d9ead80ac9423374c451a7254d076602531fe6068134503d2723133227c867ac8fa6c83c537e9a44c3c5bdbdcb1fe33703462779ad4aad39514614751a71085f2f10e1c7a593e4e030efb5b8721ce55b0b0362c0a046dacce86ddd0343c6d3c7c79c2208ba0d9c9cf24a6d046d21d21f90f703f006a18d5653c4edf5391ff23a61f03ff83d237e880ee61187fa9f379a028e0a").unwrap();
-               if shutdown {
-                       target_value.append(&mut hex::decode("001976a91479b000887626b294a914501a4cd226b58b23598388ac").unwrap());
-               }
-               assert_eq!(encoded_value, target_value);
-       }
-
-       #[test]
-       fn encoding_accept_channel() {
-               do_encoding_accept_channel(false);
-               do_encoding_accept_channel(true);
-       }
-
-       #[test]
-       fn encoding_funding_created() {
-               let secp_ctx = Secp256k1::new();
-               let (privkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
-               let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
-               let funding_created = msgs::FundingCreated {
-                       temporary_channel_id: [2; 32],
-                       funding_txid: Sha256dHash::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap(),
-                       funding_output_index: 255,
-                       signature: sig_1,
-               };
-               let encoded_value = funding_created.encode();
-               let target_value = hex::decode("02020202020202020202020202020202020202020202020202020202020202026e96fe9f8b0ddcd729ba03cfafa5a27b050b39d354dd980814268dfa9a44d4c200ffd977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
-               assert_eq!(encoded_value, target_value);
-       }
-
-       #[test]
-       fn encoding_funding_signed() {
-               let secp_ctx = Secp256k1::new();
-               let (privkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
-               let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
-               let funding_signed = msgs::FundingSigned {
-                       channel_id: [2; 32],
-                       signature: sig_1,
-               };
-               let encoded_value = funding_signed.encode();
-               let target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
-               assert_eq!(encoded_value, target_value);
-       }
-
-       #[test]
-       fn encoding_funding_locked() {
-               let secp_ctx = Secp256k1::new();
-               let (_, pubkey_1,) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
-               let funding_locked = msgs::FundingLocked {
-                       channel_id: [2; 32],
-                       next_per_commitment_point: pubkey_1,
-               };
-               let encoded_value = funding_locked.encode();
-               let target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f").unwrap();
-               assert_eq!(encoded_value, target_value);
-       }
-
-       fn do_encoding_shutdown(script_type: u8) {
-               let secp_ctx = Secp256k1::new();
-               let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
-               let script = Builder::new().push_opcode(opcodes::OP_TRUE).into_script();
-               let shutdown = msgs::Shutdown {
-                       channel_id: [2; 32],
-                       scriptpubkey: if script_type == 1 { Address::p2pkh(&::bitcoin::PublicKey{compressed: true, key: pubkey_1}, Network::Testnet).script_pubkey() } else if script_type == 2 { Address::p2sh(&script, Network::Testnet).script_pubkey() } else if script_type == 3 { Address::p2wpkh(&::bitcoin::PublicKey{compressed: true, key: pubkey_1}, Network::Testnet).script_pubkey() } else { Address::p2wsh(&script, Network::Testnet).script_pubkey() },
-               };
-               let encoded_value = shutdown.encode();
-               let mut target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap();
-               if script_type == 1 {
-                       target_value.append(&mut hex::decode("001976a91479b000887626b294a914501a4cd226b58b23598388ac").unwrap());
-               } else if script_type == 2 {
-                       target_value.append(&mut hex::decode("0017a914da1745e9b549bd0bfa1a569971c77eba30cd5a4b87").unwrap());
-               } else if script_type == 3 {
-                       target_value.append(&mut hex::decode("0016001479b000887626b294a914501a4cd226b58b235983").unwrap());
-               } else if script_type == 4 {
-                       target_value.append(&mut hex::decode("002200204ae81572f06e1b88fd5ced7a1a000945432e83e1551e6f721ee9c00b8cc33260").unwrap());
-               }
-               assert_eq!(encoded_value, target_value);
-       }
-
-       #[test]
-       fn encoding_shutdown() {
-               do_encoding_shutdown(1);
-               do_encoding_shutdown(2);
-               do_encoding_shutdown(3);
-               do_encoding_shutdown(4);
-       }
-
-       #[test]
-       fn encoding_closing_signed() {
-               let secp_ctx = Secp256k1::new();
-               let (privkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
-               let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
-               let closing_signed = msgs::ClosingSigned {
-                       channel_id: [2; 32],
-                       fee_satoshis: 2316138423780173,
-                       signature: sig_1,
-               };
-               let encoded_value = closing_signed.encode();
-               let target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020200083a840000034dd977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
-               assert_eq!(encoded_value, target_value);
-       }
-
-       #[test]
-       fn encoding_update_add_htlc() {
-               let secp_ctx = Secp256k1::new();
-               let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
-               let onion_routing_packet = msgs::OnionPacket {
-                       version: 255,
-                       public_key: Ok(pubkey_1),
-                       hop_data: [1; 20*65],
-                       hmac: [2; 32]
-               };
-               let update_add_htlc = msgs::UpdateAddHTLC {
-                       channel_id: [2; 32],
-                       htlc_id: 2316138423780173,
-                       amount_msat: 3608586615801332854,
-                       payment_hash: PaymentHash([1; 32]),
-                       cltv_expiry: 821716,
-                       onion_routing_packet
-               };
-               let encoded_value = update_add_htlc.encode();
-               let target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020200083a840000034d32144668701144760101010101010101010101010101010101010101010101010101010101010101000c89d4ff031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010202020202020202020202020202020202020202020202020202020202020202").unwrap();
-               assert_eq!(encoded_value, target_value);
-       }
-
-       #[test]
-       fn encoding_update_fulfill_htlc() {
-               let update_fulfill_htlc = msgs::UpdateFulfillHTLC {
-                       channel_id: [2; 32],
-                       htlc_id: 2316138423780173,
-                       payment_preimage: PaymentPreimage([1; 32]),
-               };
-               let encoded_value = update_fulfill_htlc.encode();
-               let target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020200083a840000034d0101010101010101010101010101010101010101010101010101010101010101").unwrap();
-               assert_eq!(encoded_value, target_value);
-       }
-
-       #[test]
-       fn encoding_update_fail_htlc() {
-               let reason = OnionErrorPacket {
-                       data: [1; 32].to_vec(),
-               };
-               let update_fail_htlc = msgs::UpdateFailHTLC {
-                       channel_id: [2; 32],
-                       htlc_id: 2316138423780173,
-                       reason
-               };
-               let encoded_value = update_fail_htlc.encode();
-               let target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020200083a840000034d00200101010101010101010101010101010101010101010101010101010101010101").unwrap();
-               assert_eq!(encoded_value, target_value);
-       }
-
-       #[test]
-       fn encoding_update_fail_malformed_htlc() {
-               let update_fail_malformed_htlc = msgs::UpdateFailMalformedHTLC {
-                       channel_id: [2; 32],
-                       htlc_id: 2316138423780173,
-                       sha256_of_onion: [1; 32],
-                       failure_code: 255
-               };
-               let encoded_value = update_fail_malformed_htlc.encode();
-               let target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020200083a840000034d010101010101010101010101010101010101010101010101010101010101010100ff").unwrap();
-               assert_eq!(encoded_value, target_value);
-       }
-
-       fn do_encoding_commitment_signed(htlcs: bool) {
-               let secp_ctx = Secp256k1::new();
-               let (privkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
-               let (privkey_2, _) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202", secp_ctx);
-               let (privkey_3, _) = get_keys_from!("0303030303030303030303030303030303030303030303030303030303030303", secp_ctx);
-               let (privkey_4, _) = get_keys_from!("0404040404040404040404040404040404040404040404040404040404040404", secp_ctx);
-               let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
-               let sig_2 = get_sig_on!(privkey_2, secp_ctx, String::from("01010101010101010101010101010101"));
-               let sig_3 = get_sig_on!(privkey_3, secp_ctx, String::from("01010101010101010101010101010101"));
-               let sig_4 = get_sig_on!(privkey_4, secp_ctx, String::from("01010101010101010101010101010101"));
-               let commitment_signed = msgs::CommitmentSigned {
-                       channel_id: [2; 32],
-                       signature: sig_1,
-                       htlc_signatures: if htlcs { vec![sig_2, sig_3, sig_4] } else { Vec::new() },
-               };
-               let encoded_value = commitment_signed.encode();
-               let mut target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
-               if htlcs {
-                       target_value.append(&mut hex::decode("00031735b6a427e80d5fe7cd90a2f4ee08dc9c27cda7c35a4172e5d85b12c49d4232537e98f9b1f3c5e6989a8b9644e90e8918127680dbd0d4043510840fc0f1e11a216c280b5395a2546e7e4b2663e04f811622f15a4f91e83aa2e92ba2a573c139142c54ae63072a1ec1ee7dc0c04bde5c847806172aa05c92c22ae8e308d1d2692b12cc195ce0a2d1bda6a88befa19fa07f51caa75ce83837f28965600b8aacab0855ffb0e741ec5f7c41421e9829a9d48611c8c831f71be5ea73e66594977ffd").unwrap());
-               } else {
-                       target_value.append(&mut hex::decode("0000").unwrap());
-               }
-               assert_eq!(encoded_value, target_value);
-       }
-
-       #[test]
-       fn encoding_commitment_signed() {
-               do_encoding_commitment_signed(true);
-               do_encoding_commitment_signed(false);
-       }
-
-       #[test]
-       fn encoding_revoke_and_ack() {
-               let secp_ctx = Secp256k1::new();
-               let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
-               let raa = msgs::RevokeAndACK {
-                       channel_id: [2; 32],
-                       per_commitment_secret: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
-                       next_per_commitment_point: pubkey_1,
-               };
-               let encoded_value = raa.encode();
-               let target_value = hex::decode("02020202020202020202020202020202020202020202020202020202020202020101010101010101010101010101010101010101010101010101010101010101031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f").unwrap();
-               assert_eq!(encoded_value, target_value);
-       }
-
-       #[test]
-       fn encoding_update_fee() {
-               let update_fee = msgs::UpdateFee {
-                       channel_id: [2; 32],
-                       feerate_per_kw: 20190119,
-               };
-               let encoded_value = update_fee.encode();
-               let target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202013413a7").unwrap();
-               assert_eq!(encoded_value, target_value);
-       }
-
-       fn do_encoding_init(unknown_global_bits: bool, initial_routing_sync: bool) {
-               let mut global = GlobalFeatures::new();
-               if unknown_global_bits {
-                       global.flags = vec![0xFF, 0xFF];
-               }
-               let mut local = LocalFeatures::new();
-               if initial_routing_sync {
-                       local.set_initial_routing_sync();
-               }
-               let init = msgs::Init {
-                       global_features: global,
-                       local_features: local,
-               };
-               let encoded_value = init.encode();
-               let mut target_value = Vec::new();
-               if unknown_global_bits {
-                       target_value.append(&mut hex::decode("0002ffff").unwrap());
-               } else {
-                       target_value.append(&mut hex::decode("0000").unwrap());
-               }
-               if initial_routing_sync {
-                       target_value.append(&mut hex::decode("00012a").unwrap());
-               } else {
-                       target_value.append(&mut hex::decode("000122").unwrap());
-               }
-               assert_eq!(encoded_value, target_value);
-       }
-
-       #[test]
-       fn encoding_init() {
-               do_encoding_init(false, false);
-               do_encoding_init(true, false);
-               do_encoding_init(false, true);
-               do_encoding_init(true, true);
-       }
-
-       #[test]
-       fn encoding_error() {
-               let error = msgs::ErrorMessage {
-                       channel_id: [2; 32],
-                       data: String::from("rust-lightning"),
-               };
-               let encoded_value = error.encode();
-               let target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202000e727573742d6c696768746e696e67").unwrap();
-               assert_eq!(encoded_value, target_value);
-       }
-
-       #[test]
-       fn encoding_ping() {
-               let ping = msgs::Ping {
-                       ponglen: 64,
-                       byteslen: 64
-               };
-               let encoded_value = ping.encode();
-               let target_value = hex::decode("0040004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").unwrap();
-               assert_eq!(encoded_value, target_value);
-       }
-
-       #[test]
-       fn encoding_pong() {
-               let pong = msgs::Pong {
-                       byteslen: 64
-               };
-               let encoded_value = pong.encode();
-               let target_value = hex::decode("004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").unwrap();
-               assert_eq!(encoded_value, target_value);
-       }
-}
diff --git a/src/ln/onion_utils.rs b/src/ln/onion_utils.rs
deleted file mode 100644 (file)
index 6ca8811..0000000
+++ /dev/null
@@ -1,579 +0,0 @@
-use ln::channelmanager::{PaymentHash, HTLCSource};
-use ln::msgs;
-use ln::router::{Route,RouteHop};
-use util::byte_utils;
-use util::chacha20::ChaCha20;
-use util::errors::{self, APIError};
-use util::ser::{Readable, Writeable};
-use util::logger::{Logger, LogHolder};
-
-use bitcoin_hashes::{Hash, HashEngine};
-use bitcoin_hashes::cmp::fixed_time_eq;
-use bitcoin_hashes::hmac::{Hmac, HmacEngine};
-use bitcoin_hashes::sha256::Hash as Sha256;
-
-use secp256k1::key::{SecretKey,PublicKey};
-use secp256k1::Secp256k1;
-use secp256k1::ecdh::SharedSecret;
-use secp256k1;
-
-use std::io::Cursor;
-use std::sync::Arc;
-
-pub(super) struct OnionKeys {
-       #[cfg(test)]
-       pub(super) shared_secret: SharedSecret,
-       #[cfg(test)]
-       pub(super) blinding_factor: [u8; 32],
-       pub(super) ephemeral_pubkey: PublicKey,
-       pub(super) rho: [u8; 32],
-       pub(super) mu: [u8; 32],
-}
-
-#[inline]
-pub(super) fn gen_rho_mu_from_shared_secret(shared_secret: &[u8]) -> ([u8; 32], [u8; 32]) {
-       assert_eq!(shared_secret.len(), 32);
-       ({
-               let mut hmac = HmacEngine::<Sha256>::new(&[0x72, 0x68, 0x6f]); // rho
-               hmac.input(&shared_secret[..]);
-               Hmac::from_engine(hmac).into_inner()
-       },
-       {
-               let mut hmac = HmacEngine::<Sha256>::new(&[0x6d, 0x75]); // mu
-               hmac.input(&shared_secret[..]);
-               Hmac::from_engine(hmac).into_inner()
-       })
-}
-
-#[inline]
-pub(super) fn gen_um_from_shared_secret(shared_secret: &[u8]) -> [u8; 32] {
-       assert_eq!(shared_secret.len(), 32);
-       let mut hmac = HmacEngine::<Sha256>::new(&[0x75, 0x6d]); // um
-       hmac.input(&shared_secret[..]);
-       Hmac::from_engine(hmac).into_inner()
-}
-
-#[inline]
-pub(super) fn gen_ammag_from_shared_secret(shared_secret: &[u8]) -> [u8; 32] {
-       assert_eq!(shared_secret.len(), 32);
-       let mut hmac = HmacEngine::<Sha256>::new(&[0x61, 0x6d, 0x6d, 0x61, 0x67]); // ammag
-       hmac.input(&shared_secret[..]);
-       Hmac::from_engine(hmac).into_inner()
-}
-
-// can only fail if an intermediary hop has an invalid public key or session_priv is invalid
-#[inline]
-pub(super) fn construct_onion_keys_callback<T: secp256k1::Signing, FType: FnMut(SharedSecret, [u8; 32], PublicKey, &RouteHop)> (secp_ctx: &Secp256k1<T>, route: &Route, session_priv: &SecretKey, mut callback: FType) -> Result<(), secp256k1::Error> {
-       let mut blinded_priv = session_priv.clone();
-       let mut blinded_pub = PublicKey::from_secret_key(secp_ctx, &blinded_priv);
-
-       for hop in route.hops.iter() {
-               let shared_secret = SharedSecret::new(&hop.pubkey, &blinded_priv);
-
-               let mut sha = Sha256::engine();
-               sha.input(&blinded_pub.serialize()[..]);
-               sha.input(&shared_secret[..]);
-               let blinding_factor = Sha256::from_engine(sha).into_inner();
-
-               let ephemeral_pubkey = blinded_pub;
-
-               blinded_priv.mul_assign(&blinding_factor)?;
-               blinded_pub = PublicKey::from_secret_key(secp_ctx, &blinded_priv);
-
-               callback(shared_secret, blinding_factor, ephemeral_pubkey, hop);
-       }
-
-       Ok(())
-}
-
-// can only fail if an intermediary hop has an invalid public key or session_priv is invalid
-pub(super) fn construct_onion_keys<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, route: &Route, session_priv: &SecretKey) -> Result<Vec<OnionKeys>, secp256k1::Error> {
-       let mut res = Vec::with_capacity(route.hops.len());
-
-       construct_onion_keys_callback(secp_ctx, route, session_priv, |shared_secret, _blinding_factor, ephemeral_pubkey, _| {
-               let (rho, mu) = gen_rho_mu_from_shared_secret(&shared_secret[..]);
-
-               res.push(OnionKeys {
-                       #[cfg(test)]
-                       shared_secret,
-                       #[cfg(test)]
-                       blinding_factor: _blinding_factor,
-                       ephemeral_pubkey,
-                       rho,
-                       mu,
-               });
-       })?;
-
-       Ok(res)
-}
-
-/// returns the hop data, as well as the first-hop value_msat and CLTV value we should send.
-pub(super) fn build_onion_payloads(route: &Route, starting_htlc_offset: u32) -> Result<(Vec<msgs::OnionHopData>, u64, u32), APIError> {
-       let mut cur_value_msat = 0u64;
-       let mut cur_cltv = starting_htlc_offset;
-       let mut last_short_channel_id = 0;
-       let mut res: Vec<msgs::OnionHopData> = Vec::with_capacity(route.hops.len());
-
-       for hop in route.hops.iter().rev() {
-               // First hop gets special values so that it can check, on receipt, that everything is
-               // exactly as it should be (and the next hop isn't trying to probe to find out if we're
-               // the intended recipient).
-               let value_msat = if cur_value_msat == 0 { hop.fee_msat } else { cur_value_msat };
-               let cltv = if cur_cltv == starting_htlc_offset { hop.cltv_expiry_delta + starting_htlc_offset } else { cur_cltv };
-               res.insert(0, msgs::OnionHopData {
-                       realm: 0,
-                       data: msgs::OnionRealm0HopData {
-                               short_channel_id: last_short_channel_id,
-                               amt_to_forward: value_msat,
-                               outgoing_cltv_value: cltv,
-                       },
-                       hmac: [0; 32],
-               });
-               cur_value_msat += hop.fee_msat;
-               if cur_value_msat >= 21000000 * 100000000 * 1000 {
-                       return Err(APIError::RouteError{err: "Channel fees overflowed?!"});
-               }
-               cur_cltv += hop.cltv_expiry_delta as u32;
-               if cur_cltv >= 500000000 {
-                       return Err(APIError::RouteError{err: "Channel CLTV overflowed?!"});
-               }
-               last_short_channel_id = hop.short_channel_id;
-       }
-       Ok((res, cur_value_msat, cur_cltv))
-}
-
-#[inline]
-fn shift_arr_right(arr: &mut [u8; 20*65]) {
-       for i in (65..20*65).rev() {
-               arr[i] = arr[i-65];
-       }
-       for i in 0..65 {
-               arr[i] = 0;
-       }
-}
-
-#[inline]
-fn xor_bufs(dst: &mut[u8], src: &[u8]) {
-       assert_eq!(dst.len(), src.len());
-
-       for i in 0..dst.len() {
-               dst[i] ^= src[i];
-       }
-}
-
-const ZERO:[u8; 21*65] = [0; 21*65];
-pub(super) fn construct_onion_packet(mut payloads: Vec<msgs::OnionHopData>, onion_keys: Vec<OnionKeys>, associated_data: &PaymentHash) -> msgs::OnionPacket {
-       let mut buf = Vec::with_capacity(21*65);
-       buf.resize(21*65, 0);
-
-       let filler = {
-               let iters = payloads.len() - 1;
-               let end_len = iters * 65;
-               let mut res = Vec::with_capacity(end_len);
-               res.resize(end_len, 0);
-
-               for (i, keys) in onion_keys.iter().enumerate() {
-                       if i == payloads.len() - 1 { continue; }
-                       let mut chacha = ChaCha20::new(&keys.rho, &[0u8; 8]);
-                       chacha.process(&ZERO, &mut buf); // We don't have a seek function :(
-                       xor_bufs(&mut res[0..(i + 1)*65], &buf[(20 - i)*65..21*65]);
-               }
-               res
-       };
-
-       let mut packet_data = [0; 20*65];
-       let mut hmac_res = [0; 32];
-
-       for (i, (payload, keys)) in payloads.iter_mut().zip(onion_keys.iter()).rev().enumerate() {
-               shift_arr_right(&mut packet_data);
-               payload.hmac = hmac_res;
-               packet_data[0..65].copy_from_slice(&payload.encode()[..]);
-
-               let mut chacha = ChaCha20::new(&keys.rho, &[0u8; 8]);
-               chacha.process(&packet_data, &mut buf[0..20*65]);
-               packet_data[..].copy_from_slice(&buf[0..20*65]);
-
-               if i == 0 {
-                       packet_data[20*65 - filler.len()..20*65].copy_from_slice(&filler[..]);
-               }
-
-               let mut hmac = HmacEngine::<Sha256>::new(&keys.mu);
-               hmac.input(&packet_data);
-               hmac.input(&associated_data.0[..]);
-               hmac_res = Hmac::from_engine(hmac).into_inner();
-       }
-
-       msgs::OnionPacket{
-               version: 0,
-               public_key: Ok(onion_keys.first().unwrap().ephemeral_pubkey),
-               hop_data: packet_data,
-               hmac: hmac_res,
-       }
-}
-
-/// Encrypts a failure packet. raw_packet can either be a
-/// msgs::DecodedOnionErrorPacket.encode() result or a msgs::OnionErrorPacket.data element.
-pub(super) fn encrypt_failure_packet(shared_secret: &[u8], raw_packet: &[u8]) -> msgs::OnionErrorPacket {
-       let ammag = gen_ammag_from_shared_secret(&shared_secret);
-
-       let mut packet_crypted = Vec::with_capacity(raw_packet.len());
-       packet_crypted.resize(raw_packet.len(), 0);
-       let mut chacha = ChaCha20::new(&ammag, &[0u8; 8]);
-       chacha.process(&raw_packet, &mut packet_crypted[..]);
-       msgs::OnionErrorPacket {
-               data: packet_crypted,
-       }
-}
-
-pub(super) fn build_failure_packet(shared_secret: &[u8], failure_type: u16, failure_data: &[u8]) -> msgs::DecodedOnionErrorPacket {
-       assert_eq!(shared_secret.len(), 32);
-       assert!(failure_data.len() <= 256 - 2);
-
-       let um = gen_um_from_shared_secret(&shared_secret);
-
-       let failuremsg = {
-               let mut res = Vec::with_capacity(2 + failure_data.len());
-               res.push(((failure_type >> 8) & 0xff) as u8);
-               res.push(((failure_type >> 0) & 0xff) as u8);
-               res.extend_from_slice(&failure_data[..]);
-               res
-       };
-       let pad = {
-               let mut res = Vec::with_capacity(256 - 2 - failure_data.len());
-               res.resize(256 - 2 - failure_data.len(), 0);
-               res
-       };
-       let mut packet = msgs::DecodedOnionErrorPacket {
-               hmac: [0; 32],
-               failuremsg: failuremsg,
-               pad: pad,
-       };
-
-       let mut hmac = HmacEngine::<Sha256>::new(&um);
-       hmac.input(&packet.encode()[32..]);
-       packet.hmac = Hmac::from_engine(hmac).into_inner();
-
-       packet
-}
-
-#[inline]
-pub(super) fn build_first_hop_failure_packet(shared_secret: &[u8], failure_type: u16, failure_data: &[u8]) -> msgs::OnionErrorPacket {
-       let failure_packet = build_failure_packet(shared_secret, failure_type, failure_data);
-       encrypt_failure_packet(shared_secret, &failure_packet.encode()[..])
-}
-
-/// Process failure we got back from upstream on a payment we sent (implying htlc_source is an
-/// OutboundRoute).
-/// Returns update, a boolean indicating that the payment itself failed, and the error code.
-pub(super) fn process_onion_failure<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, logger: &Arc<Logger>, htlc_source: &HTLCSource, mut packet_decrypted: Vec<u8>) -> (Option<msgs::HTLCFailChannelUpdate>, bool, Option<u16>) {
-       if let &HTLCSource::OutboundRoute { ref route, ref session_priv, ref first_hop_htlc_msat } = htlc_source {
-               let mut res = None;
-               let mut htlc_msat = *first_hop_htlc_msat;
-               let mut error_code_ret = None;
-               let mut next_route_hop_ix = 0;
-               let mut is_from_final_node = false;
-
-               // Handle packed channel/node updates for passing back for the route handler
-               construct_onion_keys_callback(secp_ctx, route, session_priv, |shared_secret, _, _, route_hop| {
-                       next_route_hop_ix += 1;
-                       if res.is_some() { return; }
-
-                       let amt_to_forward = htlc_msat - route_hop.fee_msat;
-                       htlc_msat = amt_to_forward;
-
-                       let ammag = gen_ammag_from_shared_secret(&shared_secret[..]);
-
-                       let mut decryption_tmp = Vec::with_capacity(packet_decrypted.len());
-                       decryption_tmp.resize(packet_decrypted.len(), 0);
-                       let mut chacha = ChaCha20::new(&ammag, &[0u8; 8]);
-                       chacha.process(&packet_decrypted, &mut decryption_tmp[..]);
-                       packet_decrypted = decryption_tmp;
-
-                       is_from_final_node = route.hops.last().unwrap().pubkey == route_hop.pubkey;
-
-                       if let Ok(err_packet) = msgs::DecodedOnionErrorPacket::read(&mut Cursor::new(&packet_decrypted)) {
-                               let um = gen_um_from_shared_secret(&shared_secret[..]);
-                               let mut hmac = HmacEngine::<Sha256>::new(&um);
-                               hmac.input(&err_packet.encode()[32..]);
-
-                               if fixed_time_eq(&Hmac::from_engine(hmac).into_inner(), &err_packet.hmac) {
-                                       if let Some(error_code_slice) = err_packet.failuremsg.get(0..2) {
-                                               const PERM: u16 = 0x4000;
-                                               const NODE: u16 = 0x2000;
-                                               const UPDATE: u16 = 0x1000;
-
-                                               let error_code = byte_utils::slice_to_be16(&error_code_slice);
-                                               error_code_ret = Some(error_code);
-
-                                               let (debug_field, debug_field_size) = errors::get_onion_debug_field(error_code);
-
-                                               // indicate that payment parameter has failed and no need to
-                                               // update Route object
-                                               let payment_failed = (match error_code & 0xff {
-                                                       15|16|17|18|19 => true,
-                                                       _ => false,
-                                               } && is_from_final_node) // PERM bit observed below even this error is from the intermediate nodes
-                                               || error_code == 21; // Special case error 21 as the Route object is bogus, TODO: Maybe fail the node if the CLTV was reasonable?
-
-                                               let mut fail_channel_update = None;
-
-                                               if error_code & NODE == NODE {
-                                                       fail_channel_update = Some(msgs::HTLCFailChannelUpdate::NodeFailure { node_id: route_hop.pubkey, is_permanent: error_code & PERM == PERM });
-                                               }
-                                               else if error_code & PERM == PERM {
-                                                       fail_channel_update = if payment_failed {None} else {Some(msgs::HTLCFailChannelUpdate::ChannelClosed {
-                                                               short_channel_id: route.hops[next_route_hop_ix - if next_route_hop_ix == route.hops.len() { 1 } else { 0 }].short_channel_id,
-                                                               is_permanent: true,
-                                                       })};
-                                               }
-                                               else if error_code & UPDATE == UPDATE {
-                                                       if let Some(update_len_slice) = err_packet.failuremsg.get(debug_field_size+2..debug_field_size+4) {
-                                                               let update_len = byte_utils::slice_to_be16(&update_len_slice) as usize;
-                                                               if let Some(update_slice) = err_packet.failuremsg.get(debug_field_size + 4..debug_field_size + 4 + update_len) {
-                                                                       if let Ok(chan_update) = msgs::ChannelUpdate::read(&mut Cursor::new(&update_slice)) {
-                                                                               // if channel_update should NOT have caused the failure:
-                                                                               // MAY treat the channel_update as invalid.
-                                                                               let is_chan_update_invalid = match error_code & 0xff {
-                                                                                       7 => false,
-                                                                                       11 => amt_to_forward > chan_update.contents.htlc_minimum_msat,
-                                                                                       12 => {
-                                                                                               let new_fee = amt_to_forward.checked_mul(chan_update.contents.fee_proportional_millionths as u64).and_then(|prop_fee| { (prop_fee / 1000000).checked_add(chan_update.contents.fee_base_msat as u64) });
-                                                                                               new_fee.is_some() && route_hop.fee_msat >= new_fee.unwrap()
-                                                                                       }
-                                                                                       13 => route_hop.cltv_expiry_delta as u16 >= chan_update.contents.cltv_expiry_delta,
-                                                                                       14 => false, // expiry_too_soon; always valid?
-                                                                                       20 => chan_update.contents.flags & 2 == 0,
-                                                                                       _ => false, // unknown error code; take channel_update as valid
-                                                                               };
-                                                                               fail_channel_update = if is_chan_update_invalid {
-                                                                                       // This probably indicates the node which forwarded
-                                                                                       // to the node in question corrupted something.
-                                                                                       Some(msgs::HTLCFailChannelUpdate::ChannelClosed {
-                                                                                               short_channel_id: route_hop.short_channel_id,
-                                                                                               is_permanent: true,
-                                                                                       })
-                                                                               } else {
-                                                                                       Some(msgs::HTLCFailChannelUpdate::ChannelUpdateMessage {
-                                                                                               msg: chan_update,
-                                                                                       })
-                                                                               };
-                                                                       }
-                                                               }
-                                                       }
-                                                       if fail_channel_update.is_none() {
-                                                               // They provided an UPDATE which was obviously bogus, not worth
-                                                               // trying to relay through them anymore.
-                                                               fail_channel_update = Some(msgs::HTLCFailChannelUpdate::NodeFailure {
-                                                                       node_id: route_hop.pubkey,
-                                                                       is_permanent: true,
-                                                               });
-                                                       }
-                                               } else if !payment_failed {
-                                                       // We can't understand their error messages and they failed to
-                                                       // forward...they probably can't understand our forwards so its
-                                                       // really not worth trying any further.
-                                                       fail_channel_update = Some(msgs::HTLCFailChannelUpdate::NodeFailure {
-                                                               node_id: route_hop.pubkey,
-                                                               is_permanent: true,
-                                                       });
-                                               }
-
-                                               // TODO: Here (and a few other places) we assume that BADONION errors
-                                               // are always "sourced" from the node previous to the one which failed
-                                               // to decode the onion.
-                                               res = Some((fail_channel_update, !(error_code & PERM == PERM && is_from_final_node)));
-
-                                               let (description, title) = errors::get_onion_error_description(error_code);
-                                               if debug_field_size > 0 && err_packet.failuremsg.len() >= 4 + debug_field_size {
-                                                       let log_holder = LogHolder { logger };
-                                                       log_warn!(log_holder, "Onion Error[{}({:#x}) {}({})] {}", title, error_code, debug_field, log_bytes!(&err_packet.failuremsg[4..4+debug_field_size]), description);
-                                               }
-                                               else {
-                                                       let log_holder = LogHolder { logger };
-                                                       log_warn!(log_holder, "Onion Error[{}({:#x})] {}", title, error_code, description);
-                                               }
-                                       } else {
-                                               // Useless packet that we can't use but it passed HMAC, so it
-                                               // definitely came from the peer in question
-                                               res = Some((Some(msgs::HTLCFailChannelUpdate::NodeFailure {
-                                                       node_id: route_hop.pubkey,
-                                                       is_permanent: true,
-                                               }), !is_from_final_node));
-                                       }
-                               }
-                       }
-               }).expect("Route that we sent via spontaneously grew invalid keys in the middle of it?");
-               if let Some((channel_update, payment_retryable)) = res {
-                       (channel_update, payment_retryable, error_code_ret)
-               } else {
-                       // only not set either packet unparseable or hmac does not match with any
-                       // payment not retryable only when garbage is from the final node
-                       (None, !is_from_final_node, None)
-               }
-       } else { unreachable!(); }
-}
-
-#[cfg(test)]
-mod tests {
-       use ln::channelmanager::PaymentHash;
-       use ln::router::{Route, RouteHop};
-       use ln::msgs;
-       use util::ser::Writeable;
-
-       use hex;
-
-       use secp256k1::Secp256k1;
-       use secp256k1::key::{PublicKey,SecretKey};
-
-       use super::OnionKeys;
-
-       fn build_test_onion_keys() -> Vec<OnionKeys> {
-               // Keys from BOLT 4, used in both test vector tests
-               let secp_ctx = Secp256k1::new();
-
-               let route = Route {
-                       hops: vec!(
-                                       RouteHop {
-                                               pubkey: PublicKey::from_slice(&hex::decode("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap(),
-                                               short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0 // Test vectors are garbage and not generateble from a RouteHop, we fill in payloads manually
-                                       },
-                                       RouteHop {
-                                               pubkey: PublicKey::from_slice(&hex::decode("0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c").unwrap()[..]).unwrap(),
-                                               short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0 // Test vectors are garbage and not generateble from a RouteHop, we fill in payloads manually
-                                       },
-                                       RouteHop {
-                                               pubkey: PublicKey::from_slice(&hex::decode("027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007").unwrap()[..]).unwrap(),
-                                               short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0 // Test vectors are garbage and not generateble from a RouteHop, we fill in payloads manually
-                                       },
-                                       RouteHop {
-                                               pubkey: PublicKey::from_slice(&hex::decode("032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991").unwrap()[..]).unwrap(),
-                                               short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0 // Test vectors are garbage and not generateble from a RouteHop, we fill in payloads manually
-                                       },
-                                       RouteHop {
-                                               pubkey: PublicKey::from_slice(&hex::decode("02edabbd16b41c8371b92ef2f04c1185b4f03b6dcd52ba9b78d9d7c89c8f221145").unwrap()[..]).unwrap(),
-                                               short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0 // Test vectors are garbage and not generateble from a RouteHop, we fill in payloads manually
-                                       },
-                       ),
-               };
-
-               let session_priv = SecretKey::from_slice(&hex::decode("4141414141414141414141414141414141414141414141414141414141414141").unwrap()[..]).unwrap();
-
-               let onion_keys = super::construct_onion_keys(&secp_ctx, &route, &session_priv).unwrap();
-               assert_eq!(onion_keys.len(), route.hops.len());
-               onion_keys
-       }
-
-       #[test]
-       fn onion_vectors() {
-               // Packet creation test vectors from BOLT 4
-               let onion_keys = build_test_onion_keys();
-
-               assert_eq!(onion_keys[0].shared_secret[..], hex::decode("53eb63ea8a3fec3b3cd433b85cd62a4b145e1dda09391b348c4e1cd36a03ea66").unwrap()[..]);
-               assert_eq!(onion_keys[0].blinding_factor[..], hex::decode("2ec2e5da605776054187180343287683aa6a51b4b1c04d6dd49c45d8cffb3c36").unwrap()[..]);
-               assert_eq!(onion_keys[0].ephemeral_pubkey.serialize()[..], hex::decode("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]);
-               assert_eq!(onion_keys[0].rho, hex::decode("ce496ec94def95aadd4bec15cdb41a740c9f2b62347c4917325fcc6fb0453986").unwrap()[..]);
-               assert_eq!(onion_keys[0].mu, hex::decode("b57061dc6d0a2b9f261ac410c8b26d64ac5506cbba30267a649c28c179400eba").unwrap()[..]);
-
-               assert_eq!(onion_keys[1].shared_secret[..], hex::decode("a6519e98832a0b179f62123b3567c106db99ee37bef036e783263602f3488fae").unwrap()[..]);
-               assert_eq!(onion_keys[1].blinding_factor[..], hex::decode("bf66c28bc22e598cfd574a1931a2bafbca09163df2261e6d0056b2610dab938f").unwrap()[..]);
-               assert_eq!(onion_keys[1].ephemeral_pubkey.serialize()[..], hex::decode("028f9438bfbf7feac2e108d677e3a82da596be706cc1cf342b75c7b7e22bf4e6e2").unwrap()[..]);
-               assert_eq!(onion_keys[1].rho, hex::decode("450ffcabc6449094918ebe13d4f03e433d20a3d28a768203337bc40b6e4b2c59").unwrap()[..]);
-               assert_eq!(onion_keys[1].mu, hex::decode("05ed2b4a3fb023c2ff5dd6ed4b9b6ea7383f5cfe9d59c11d121ec2c81ca2eea9").unwrap()[..]);
-
-               assert_eq!(onion_keys[2].shared_secret[..], hex::decode("3a6b412548762f0dbccce5c7ae7bb8147d1caf9b5471c34120b30bc9c04891cc").unwrap()[..]);
-               assert_eq!(onion_keys[2].blinding_factor[..], hex::decode("a1f2dadd184eb1627049673f18c6325814384facdee5bfd935d9cb031a1698a5").unwrap()[..]);
-               assert_eq!(onion_keys[2].ephemeral_pubkey.serialize()[..], hex::decode("03bfd8225241ea71cd0843db7709f4c222f62ff2d4516fd38b39914ab6b83e0da0").unwrap()[..]);
-               assert_eq!(onion_keys[2].rho, hex::decode("11bf5c4f960239cb37833936aa3d02cea82c0f39fd35f566109c41f9eac8deea").unwrap()[..]);
-               assert_eq!(onion_keys[2].mu, hex::decode("caafe2820fa00eb2eeb78695ae452eba38f5a53ed6d53518c5c6edf76f3f5b78").unwrap()[..]);
-
-               assert_eq!(onion_keys[3].shared_secret[..], hex::decode("21e13c2d7cfe7e18836df50872466117a295783ab8aab0e7ecc8c725503ad02d").unwrap()[..]);
-               assert_eq!(onion_keys[3].blinding_factor[..], hex::decode("7cfe0b699f35525029ae0fa437c69d0f20f7ed4e3916133f9cacbb13c82ff262").unwrap()[..]);
-               assert_eq!(onion_keys[3].ephemeral_pubkey.serialize()[..], hex::decode("031dde6926381289671300239ea8e57ffaf9bebd05b9a5b95beaf07af05cd43595").unwrap()[..]);
-               assert_eq!(onion_keys[3].rho, hex::decode("cbe784ab745c13ff5cffc2fbe3e84424aa0fd669b8ead4ee562901a4a4e89e9e").unwrap()[..]);
-               assert_eq!(onion_keys[3].mu, hex::decode("5052aa1b3d9f0655a0932e50d42f0c9ba0705142c25d225515c45f47c0036ee9").unwrap()[..]);
-
-               assert_eq!(onion_keys[4].shared_secret[..], hex::decode("b5756b9b542727dbafc6765a49488b023a725d631af688fc031217e90770c328").unwrap()[..]);
-               assert_eq!(onion_keys[4].blinding_factor[..], hex::decode("c96e00dddaf57e7edcd4fb5954be5b65b09f17cb6d20651b4e90315be5779205").unwrap()[..]);
-               assert_eq!(onion_keys[4].ephemeral_pubkey.serialize()[..], hex::decode("03a214ebd875aab6ddfd77f22c5e7311d7f77f17a169e599f157bbcdae8bf071f4").unwrap()[..]);
-               assert_eq!(onion_keys[4].rho, hex::decode("034e18b8cc718e8af6339106e706c52d8df89e2b1f7e9142d996acf88df8799b").unwrap()[..]);
-               assert_eq!(onion_keys[4].mu, hex::decode("8e45e5c61c2b24cb6382444db6698727afb063adecd72aada233d4bf273d975a").unwrap()[..]);
-
-               // Test vectors below are flat-out wrong: they claim to set outgoing_cltv_value to non-0 :/
-               let payloads = vec!(
-                       msgs::OnionHopData {
-                               realm: 0,
-                               data: msgs::OnionRealm0HopData {
-                                       short_channel_id: 0,
-                                       amt_to_forward: 0,
-                                       outgoing_cltv_value: 0,
-                               },
-                               hmac: [0; 32],
-                       },
-                       msgs::OnionHopData {
-                               realm: 0,
-                               data: msgs::OnionRealm0HopData {
-                                       short_channel_id: 0x0101010101010101,
-                                       amt_to_forward: 0x0100000001,
-                                       outgoing_cltv_value: 0,
-                               },
-                               hmac: [0; 32],
-                       },
-                       msgs::OnionHopData {
-                               realm: 0,
-                               data: msgs::OnionRealm0HopData {
-                                       short_channel_id: 0x0202020202020202,
-                                       amt_to_forward: 0x0200000002,
-                                       outgoing_cltv_value: 0,
-                               },
-                               hmac: [0; 32],
-                       },
-                       msgs::OnionHopData {
-                               realm: 0,
-                               data: msgs::OnionRealm0HopData {
-                                       short_channel_id: 0x0303030303030303,
-                                       amt_to_forward: 0x0300000003,
-                                       outgoing_cltv_value: 0,
-                               },
-                               hmac: [0; 32],
-                       },
-                       msgs::OnionHopData {
-                               realm: 0,
-                               data: msgs::OnionRealm0HopData {
-                                       short_channel_id: 0x0404040404040404,
-                                       amt_to_forward: 0x0400000004,
-                                       outgoing_cltv_value: 0,
-                               },
-                               hmac: [0; 32],
-                       },
-               );
-
-               let packet = super::construct_onion_packet(payloads, onion_keys, &PaymentHash([0x42; 32]));
-               // Just check the final packet encoding, as it includes all the per-hop vectors in it
-               // anyway...
-               assert_eq!(packet.encode(), hex::decode("0002eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619e5f14350c2a76fc232b5e46d421e9615471ab9e0bc887beff8c95fdb878f7b3a716a996c7845c93d90e4ecbb9bde4ece2f69425c99e4bc820e44485455f135edc0d10f7d61ab590531cf08000179a333a347f8b4072f216400406bdf3bf038659793d4a1fd7b246979e3150a0a4cb052c9ec69acf0f48c3d39cd55675fe717cb7d80ce721caad69320c3a469a202f1e468c67eaf7a7cd8226d0fd32f7b48084dca885d56047694762b67021713ca673929c163ec36e04e40ca8e1c6d17569419d3039d9a1ec866abe044a9ad635778b961fc0776dc832b3a451bd5d35072d2269cf9b040f6b7a7dad84fb114ed413b1426cb96ceaf83825665ed5a1d002c1687f92465b49ed4c7f0218ff8c6c7dd7221d589c65b3b9aaa71a41484b122846c7c7b57e02e679ea8469b70e14fe4f70fee4d87b910cf144be6fe48eef24da475c0b0bcc6565ae82cd3f4e3b24c76eaa5616c6111343306ab35c1fe5ca4a77c0e314ed7dba39d6f1e0de791719c241a939cc493bea2bae1c1e932679ea94d29084278513c77b899cc98059d06a27d171b0dbdf6bee13ddc4fc17a0c4d2827d488436b57baa167544138ca2e64a11b43ac8a06cd0c2fba2d4d900ed2d9205305e2d7383cc98dacb078133de5f6fb6bed2ef26ba92cea28aafc3b9948dd9ae5559e8bd6920b8cea462aa445ca6a95e0e7ba52961b181c79e73bd581821df2b10173727a810c92b83b5ba4a0403eb710d2ca10689a35bec6c3a708e9e92f7d78ff3c5d9989574b00c6736f84c199256e76e19e78f0c98a9d580b4a658c84fc8f2096c2fbea8f5f8c59d0fdacb3be2802ef802abbecb3aba4acaac69a0e965abd8981e9896b1f6ef9d60f7a164b371af869fd0e48073742825e9434fc54da837e120266d53302954843538ea7c6c3dbfb4ff3b2fdbe244437f2a153ccf7bdb4c92aa08102d4f3cff2ae5ef86fab4653595e6a5837fa2f3e29f27a9cde5966843fb847a4a61f1e76c281fe8bb2b0a181d096100db5a1a5ce7a910238251a43ca556712eaadea167fb4d7d75825e440f3ecd782036d7574df8bceacb397abefc5f5254d2722215c53ff54af8299aaaad642c6d72a14d27882d9bbd539e1cc7a527526ba89b8c037ad09120e98ab042d3e8652b31ae0e478516bfaf88efca9f3676ffe99d2819dcaeb7610a626695f53117665d267d3f7abebd6bbd6733f645c72c389f03855bdf1e4b8075b516569b118233a0f0971d24b83113c0b096f5216a207ca99a7cddc81c130923fe3d91e7508c9ac5f2e914ff5dccab9e558566fa14efb34ac98d878580814b94b73acbfde9072f30b881f7f0fff42d4045d1ace6322d86a97d164aa84d93a60498065cc7c20e636f5862dc81531a88c60305a2e59a985be327a6902e4bed986dbf4a0b50c217af0ea7fdf9ab37f9ea1a1aaa72f54cf40154ea9b269f1a7c09f9f43245109431a175d50e2db0132337baa0ef97eed0fcf20489da36b79a1172faccc2f7ded7c60e00694282d93359c4682135642bc81f433574aa8ef0c97b4ade7ca372c5ffc23c7eddd839bab4e0f14d6df15c9dbeab176bec8b5701cf054eb3072f6dadc98f88819042bf10c407516ee58bce33fbe3b3d86a54255e577db4598e30a135361528c101683a5fcde7e8ba53f3456254be8f45fe3a56120ae96ea3773631fcb3873aa3abd91bcff00bd38bd43697a2e789e00da6077482e7b1b1a677b5afae4c54e6cbdf7377b694eb7d7a5b913476a5be923322d3de06060fd5e819635232a2cf4f0731da13b8546d1d6d4f8d75b9fce6c2341a71b0ea6f780df54bfdb0dd5cd9855179f602f9172307c7268724c3618e6817abd793adc214a0dc0bc616816632f27ea336fb56dfd").unwrap());
-       }
-
-       #[test]
-       fn test_failure_packet_onion() {
-               // Returning Errors test vectors from BOLT 4
-
-               let onion_keys = build_test_onion_keys();
-               let onion_error = super::build_failure_packet(&onion_keys[4].shared_secret[..], 0x2002, &[0; 0]);
-               assert_eq!(onion_error.encode(), hex::decode("4c2fc8bc08510334b6833ad9c3e79cd1b52ae59dfe5c2a4b23ead50f09f7ee0b0002200200fe0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").unwrap());
-
-               let onion_packet_1 = super::encrypt_failure_packet(&onion_keys[4].shared_secret[..], &onion_error.encode()[..]);
-               assert_eq!(onion_packet_1.data, hex::decode("a5e6bd0c74cb347f10cce367f949098f2457d14c046fd8a22cb96efb30b0fdcda8cb9168b50f2fd45edd73c1b0c8b33002df376801ff58aaa94000bf8a86f92620f343baef38a580102395ae3abf9128d1047a0736ff9b83d456740ebbb4aeb3aa9737f18fb4afb4aa074fb26c4d702f42968888550a3bded8c05247e045b866baef0499f079fdaeef6538f31d44deafffdfd3afa2fb4ca9082b8f1c465371a9894dd8c243fb4847e004f5256b3e90e2edde4c9fb3082ddfe4d1e734cacd96ef0706bf63c9984e22dc98851bcccd1c3494351feb458c9c6af41c0044bea3c47552b1d992ae542b17a2d0bba1a096c78d169034ecb55b6e3a7263c26017f033031228833c1daefc0dedb8cf7c3e37c9c37ebfe42f3225c326e8bcfd338804c145b16e34e4").unwrap());
-
-               let onion_packet_2 = super::encrypt_failure_packet(&onion_keys[3].shared_secret[..], &onion_packet_1.data[..]);
-               assert_eq!(onion_packet_2.data, hex::decode("c49a1ce81680f78f5f2000cda36268de34a3f0a0662f55b4e837c83a8773c22aa081bab1616a0011585323930fa5b9fae0c85770a2279ff59ec427ad1bbff9001c0cd1497004bd2a0f68b50704cf6d6a4bf3c8b6a0833399a24b3456961ba00736785112594f65b6b2d44d9f5ea4e49b5e1ec2af978cbe31c67114440ac51a62081df0ed46d4a3df295da0b0fe25c0115019f03f15ec86fabb4c852f83449e812f141a9395b3f70b766ebbd4ec2fae2b6955bd8f32684c15abfe8fd3a6261e52650e8807a92158d9f1463261a925e4bfba44bd20b166d532f0017185c3a6ac7957adefe45559e3072c8dc35abeba835a8cb01a71a15c736911126f27d46a36168ca5ef7dccd4e2886212602b181463e0dd30185c96348f9743a02aca8ec27c0b90dca270").unwrap());
-
-               let onion_packet_3 = super::encrypt_failure_packet(&onion_keys[2].shared_secret[..], &onion_packet_2.data[..]);
-               assert_eq!(onion_packet_3.data, hex::decode("a5d3e8634cfe78b2307d87c6d90be6fe7855b4f2cc9b1dfb19e92e4b79103f61ff9ac25f412ddfb7466e74f81b3e545563cdd8f5524dae873de61d7bdfccd496af2584930d2b566b4f8d3881f8c043df92224f38cf094cfc09d92655989531524593ec6d6caec1863bdfaa79229b5020acc034cd6deeea1021c50586947b9b8e6faa83b81fbfa6133c0af5d6b07c017f7158fa94f0d206baf12dda6b68f785b773b360fd0497e16cc402d779c8d48d0fa6315536ef0660f3f4e1865f5b38ea49c7da4fd959de4e83ff3ab686f059a45c65ba2af4a6a79166aa0f496bf04d06987b6d2ea205bdb0d347718b9aeff5b61dfff344993a275b79717cd815b6ad4c0beb568c4ac9c36ff1c315ec1119a1993c4b61e6eaa0375e0aaf738ac691abd3263bf937e3").unwrap());
-
-               let onion_packet_4 = super::encrypt_failure_packet(&onion_keys[1].shared_secret[..], &onion_packet_3.data[..]);
-               assert_eq!(onion_packet_4.data, hex::decode("aac3200c4968f56b21f53e5e374e3a2383ad2b1b6501bbcc45abc31e59b26881b7dfadbb56ec8dae8857add94e6702fb4c3a4de22e2e669e1ed926b04447fc73034bb730f4932acd62727b75348a648a1128744657ca6a4e713b9b646c3ca66cac02cdab44dd3439890ef3aaf61708714f7375349b8da541b2548d452d84de7084bb95b3ac2345201d624d31f4d52078aa0fa05a88b4e20202bd2b86ac5b52919ea305a8949de95e935eed0319cf3cf19ebea61d76ba92532497fcdc9411d06bcd4275094d0a4a3c5d3a945e43305a5a9256e333e1f64dbca5fcd4e03a39b9012d197506e06f29339dfee3331995b21615337ae060233d39befea925cc262873e0530408e6990f1cbd233a150ef7b004ff6166c70c68d9f8c853c1abca640b8660db2921").unwrap());
-
-               let onion_packet_5 = super::encrypt_failure_packet(&onion_keys[0].shared_secret[..], &onion_packet_4.data[..]);
-               assert_eq!(onion_packet_5.data, hex::decode("9c5add3963fc7f6ed7f148623c84134b5647e1306419dbe2174e523fa9e2fbed3a06a19f899145610741c83ad40b7712aefaddec8c6baf7325d92ea4ca4d1df8bce517f7e54554608bf2bd8071a4f52a7a2f7ffbb1413edad81eeea5785aa9d990f2865dc23b4bc3c301a94eec4eabebca66be5cf638f693ec256aec514620cc28ee4a94bd9565bc4d4962b9d3641d4278fb319ed2b84de5b665f307a2db0f7fbb757366067d88c50f7e829138fde4f78d39b5b5802f1b92a8a820865af5cc79f9f30bc3f461c66af95d13e5e1f0381c184572a91dee1c849048a647a1158cf884064deddbf1b0b88dfe2f791428d0ba0f6fb2f04e14081f69165ae66d9297c118f0907705c9c4954a199bae0bb96fad763d690e7daa6cfda59ba7f2c8d11448b604d12d").unwrap());
-       }
-}
diff --git a/src/ln/peer_channel_encryptor.rs b/src/ln/peer_channel_encryptor.rs
deleted file mode 100644 (file)
index 9d716a2..0000000
+++ /dev/null
@@ -1,716 +0,0 @@
-use ln::msgs::HandleError;
-use ln::msgs;
-
-use bitcoin_hashes::{Hash, HashEngine, Hmac, HmacEngine};
-use bitcoin_hashes::sha256::Hash as Sha256;
-
-use secp256k1::Secp256k1;
-use secp256k1::key::{PublicKey,SecretKey};
-use secp256k1::ecdh::SharedSecret;
-use secp256k1;
-
-use util::chacha20poly1305rfc::ChaCha20Poly1305RFC;
-use util::byte_utils;
-
-// Sha256("Noise_XK_secp256k1_ChaChaPoly_SHA256")
-const NOISE_CK: [u8; 32] = [0x26, 0x40, 0xf5, 0x2e, 0xeb, 0xcd, 0x9e, 0x88, 0x29, 0x58, 0x95, 0x1c, 0x79, 0x42, 0x50, 0xee, 0xdb, 0x28, 0x00, 0x2c, 0x05, 0xd7, 0xdc, 0x2e, 0xa0, 0xf1, 0x95, 0x40, 0x60, 0x42, 0xca, 0xf1];
-// Sha256(NOISE_CK || "lightning")
-const NOISE_H: [u8; 32] = [0xd1, 0xfb, 0xf6, 0xde, 0xe4, 0xf6, 0x86, 0xf1, 0x32, 0xfd, 0x70, 0x2c, 0x4a, 0xbf, 0x8f, 0xba, 0x4b, 0xb4, 0x20, 0xd8, 0x9d, 0x2a, 0x04, 0x8a, 0x3c, 0x4f, 0x4c, 0x09, 0x2e, 0x37, 0xb6, 0x76];
-
-pub enum NextNoiseStep {
-       ActOne,
-       ActTwo,
-       ActThree,
-       NoiseComplete,
-}
-
-#[derive(PartialEq)]
-enum NoiseStep {
-       PreActOne,
-       PostActOne,
-       PostActTwo,
-       // When done swap noise_state for NoiseState::Finished
-}
-
-struct BidirectionalNoiseState {
-       h: [u8; 32],
-       ck: [u8; 32],
-}
-enum DirectionalNoiseState {
-       Outbound {
-               ie: SecretKey,
-       },
-       Inbound {
-               ie: Option<PublicKey>, // filled in if state >= PostActOne
-               re: Option<SecretKey>, // filled in if state >= PostActTwo
-               temp_k2: Option<[u8; 32]>, // filled in if state >= PostActTwo
-       }
-}
-enum NoiseState {
-       InProgress {
-               state: NoiseStep,
-               directional_state: DirectionalNoiseState,
-               bidirectional_state: BidirectionalNoiseState,
-       },
-       Finished {
-               sk: [u8; 32],
-               sn: u64,
-               sck: [u8; 32],
-               rk: [u8; 32],
-               rn: u64,
-               rck: [u8; 32],
-       }
-}
-
-pub struct PeerChannelEncryptor {
-       secp_ctx: Secp256k1<secp256k1::SignOnly>,
-       their_node_id: Option<PublicKey>, // filled in for outbound, or inbound after noise_state is Finished
-
-       noise_state: NoiseState,
-}
-
-impl PeerChannelEncryptor {
-       pub fn new_outbound(their_node_id: PublicKey, ephemeral_key: SecretKey) -> PeerChannelEncryptor {
-               let secp_ctx = Secp256k1::signing_only();
-
-               let mut sha = Sha256::engine();
-               sha.input(&NOISE_H);
-               sha.input(&their_node_id.serialize()[..]);
-               let h = Sha256::from_engine(sha).into_inner();
-
-               PeerChannelEncryptor {
-                       their_node_id: Some(their_node_id),
-                       secp_ctx: secp_ctx,
-                       noise_state: NoiseState::InProgress {
-                               state: NoiseStep::PreActOne,
-                               directional_state: DirectionalNoiseState::Outbound {
-                                       ie: ephemeral_key,
-                               },
-                               bidirectional_state: BidirectionalNoiseState {
-                                       h: h,
-                                       ck: NOISE_CK,
-                               },
-                       }
-               }
-       }
-
-       pub fn new_inbound(our_node_secret: &SecretKey) -> PeerChannelEncryptor {
-               let secp_ctx = Secp256k1::signing_only();
-
-               let mut sha = Sha256::engine();
-               sha.input(&NOISE_H);
-               let our_node_id = PublicKey::from_secret_key(&secp_ctx, our_node_secret);
-               sha.input(&our_node_id.serialize()[..]);
-               let h = Sha256::from_engine(sha).into_inner();
-
-               PeerChannelEncryptor {
-                       their_node_id: None,
-                       secp_ctx: secp_ctx,
-                       noise_state: NoiseState::InProgress {
-                               state: NoiseStep::PreActOne,
-                               directional_state: DirectionalNoiseState::Inbound {
-                                       ie: None,
-                                       re: None,
-                                       temp_k2: None,
-                               },
-                               bidirectional_state: BidirectionalNoiseState {
-                                       h: h,
-                                       ck: NOISE_CK,
-                               },
-                       }
-               }
-       }
-
-       #[inline]
-       fn encrypt_with_ad(res: &mut[u8], n: u64, key: &[u8; 32], h: &[u8], plaintext: &[u8]) {
-               let mut nonce = [0; 12];
-               nonce[4..].copy_from_slice(&byte_utils::le64_to_array(n));
-
-               let mut chacha = ChaCha20Poly1305RFC::new(key, &nonce, h);
-               let mut tag = [0; 16];
-               chacha.encrypt(plaintext, &mut res[0..plaintext.len()], &mut tag);
-               res[plaintext.len()..].copy_from_slice(&tag);
-       }
-
-       #[inline]
-       fn decrypt_with_ad(res: &mut[u8], n: u64, key: &[u8; 32], h: &[u8], cyphertext: &[u8]) -> Result<(), HandleError> {
-               let mut nonce = [0; 12];
-               nonce[4..].copy_from_slice(&byte_utils::le64_to_array(n));
-
-               let mut chacha = ChaCha20Poly1305RFC::new(key, &nonce, h);
-               if !chacha.decrypt(&cyphertext[0..cyphertext.len() - 16], res, &cyphertext[cyphertext.len() - 16..]) {
-                       return Err(HandleError{err: "Bad MAC", action: Some(msgs::ErrorAction::DisconnectPeer{ msg: None })});
-               }
-               Ok(())
-       }
-
-       fn hkdf_extract_expand(salt: &[u8], ikm: &[u8]) -> ([u8; 32], [u8; 32]) {
-               let mut hmac = HmacEngine::<Sha256>::new(salt);
-               hmac.input(ikm);
-               let prk = Hmac::from_engine(hmac).into_inner();
-               let mut hmac = HmacEngine::<Sha256>::new(&prk[..]);
-               hmac.input(&[1; 1]);
-               let t1 = Hmac::from_engine(hmac).into_inner();
-               let mut hmac = HmacEngine::<Sha256>::new(&prk[..]);
-               hmac.input(&t1);
-               hmac.input(&[2; 1]);
-               (t1, Hmac::from_engine(hmac).into_inner())
-       }
-
-       #[inline]
-       fn hkdf(state: &mut BidirectionalNoiseState, ss: SharedSecret) -> [u8; 32] {
-               let (t1, t2) = Self::hkdf_extract_expand(&state.ck, &ss[..]);
-               state.ck = t1;
-               t2
-       }
-
-       #[inline]
-       fn outbound_noise_act<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, state: &mut BidirectionalNoiseState, our_key: &SecretKey, their_key: &PublicKey) -> ([u8; 50], [u8; 32]) {
-               let our_pub = PublicKey::from_secret_key(secp_ctx, &our_key);
-
-               let mut sha = Sha256::engine();
-               sha.input(&state.h);
-               sha.input(&our_pub.serialize()[..]);
-               state.h = Sha256::from_engine(sha).into_inner();
-
-               let ss = SharedSecret::new(&their_key, &our_key);
-               let temp_k = PeerChannelEncryptor::hkdf(state, ss);
-
-               let mut res = [0; 50];
-               res[1..34].copy_from_slice(&our_pub.serialize()[..]);
-               PeerChannelEncryptor::encrypt_with_ad(&mut res[34..], 0, &temp_k, &state.h, &[0; 0]);
-
-               let mut sha = Sha256::engine();
-               sha.input(&state.h);
-               sha.input(&res[34..]);
-               state.h = Sha256::from_engine(sha).into_inner();
-
-               (res, temp_k)
-       }
-
-       #[inline]
-       fn inbound_noise_act(state: &mut BidirectionalNoiseState, act: &[u8], our_key: &SecretKey) -> Result<(PublicKey, [u8; 32]), HandleError> {
-               assert_eq!(act.len(), 50);
-
-               if act[0] != 0 {
-                       return Err(HandleError{err: "Unknown handshake version number", action: Some(msgs::ErrorAction::DisconnectPeer{ msg: None })});
-               }
-
-               let their_pub = match PublicKey::from_slice(&act[1..34]) {
-                       Err(_) => return Err(HandleError{err: "Invalid public key", action: Some(msgs::ErrorAction::DisconnectPeer{ msg: None })}),
-                       Ok(key) => key,
-               };
-
-               let mut sha = Sha256::engine();
-               sha.input(&state.h);
-               sha.input(&their_pub.serialize()[..]);
-               state.h = Sha256::from_engine(sha).into_inner();
-
-               let ss = SharedSecret::new(&their_pub, &our_key);
-               let temp_k = PeerChannelEncryptor::hkdf(state, ss);
-
-               let mut dec = [0; 0];
-               PeerChannelEncryptor::decrypt_with_ad(&mut dec, 0, &temp_k, &state.h, &act[34..])?;
-
-               let mut sha = Sha256::engine();
-               sha.input(&state.h);
-               sha.input(&act[34..]);
-               state.h = Sha256::from_engine(sha).into_inner();
-
-               Ok((their_pub, temp_k))
-       }
-
-       pub fn get_act_one(&mut self) -> [u8; 50] {
-               match self.noise_state {
-                       NoiseState::InProgress { ref mut state, ref directional_state, ref mut bidirectional_state } =>
-                               match directional_state {
-                                       &DirectionalNoiseState::Outbound { ref ie } => {
-                                               if *state != NoiseStep::PreActOne {
-                                                       panic!("Requested act at wrong step");
-                                               }
-
-                                               let (res, _) = PeerChannelEncryptor::outbound_noise_act(&self.secp_ctx, bidirectional_state, &ie, &self.their_node_id.unwrap());
-                                               *state = NoiseStep::PostActOne;
-                                               res
-                                       },
-                                       _ => panic!("Wrong direction for act"),
-                               },
-                       _ => panic!("Cannot get act one after noise handshake completes"),
-               }
-       }
-
-       pub fn process_act_one_with_keys(&mut self, act_one: &[u8], our_node_secret: &SecretKey, our_ephemeral: SecretKey) -> Result<[u8; 50], HandleError> {
-               assert_eq!(act_one.len(), 50);
-
-               match self.noise_state {
-                       NoiseState::InProgress { ref mut state, ref mut directional_state, ref mut bidirectional_state } =>
-                               match directional_state {
-                                       &mut DirectionalNoiseState::Inbound { ref mut ie, ref mut re, ref mut temp_k2 } => {
-                                               if *state != NoiseStep::PreActOne {
-                                                       panic!("Requested act at wrong step");
-                                               }
-
-                                               let (their_pub, _) = PeerChannelEncryptor::inbound_noise_act(bidirectional_state, act_one, &our_node_secret)?;
-                                               ie.get_or_insert(their_pub);
-
-                                               re.get_or_insert(our_ephemeral);
-
-                                               let (res, temp_k) = PeerChannelEncryptor::outbound_noise_act(&self.secp_ctx, bidirectional_state, &re.unwrap(), &ie.unwrap());
-                                               *temp_k2 = Some(temp_k);
-                                               *state = NoiseStep::PostActTwo;
-                                               Ok(res)
-                                       },
-                                       _ => panic!("Wrong direction for act"),
-                               },
-                       _ => panic!("Cannot get act one after noise handshake completes"),
-               }
-       }
-
-       pub fn process_act_two(&mut self, act_two: &[u8], our_node_secret: &SecretKey) -> Result<([u8; 66], PublicKey), HandleError> {
-               assert_eq!(act_two.len(), 50);
-
-               let final_hkdf;
-               let ck;
-               let res: [u8; 66] = match self.noise_state {
-                       NoiseState::InProgress { ref state, ref directional_state, ref mut bidirectional_state } =>
-                               match directional_state {
-                                       &DirectionalNoiseState::Outbound { ref ie } => {
-                                               if *state != NoiseStep::PostActOne {
-                                                       panic!("Requested act at wrong step");
-                                               }
-
-                                               let (re, temp_k2) = PeerChannelEncryptor::inbound_noise_act(bidirectional_state, act_two, &ie)?;
-
-                                               let mut res = [0; 66];
-                                               let our_node_id = PublicKey::from_secret_key(&self.secp_ctx, &our_node_secret);
-
-                                               PeerChannelEncryptor::encrypt_with_ad(&mut res[1..50], 1, &temp_k2, &bidirectional_state.h, &our_node_id.serialize()[..]);
-
-                                               let mut sha = Sha256::engine();
-                                               sha.input(&bidirectional_state.h);
-                                               sha.input(&res[1..50]);
-                                               bidirectional_state.h = Sha256::from_engine(sha).into_inner();
-
-                                               let ss = SharedSecret::new(&re, our_node_secret);
-                                               let temp_k = PeerChannelEncryptor::hkdf(bidirectional_state, ss);
-
-                                               PeerChannelEncryptor::encrypt_with_ad(&mut res[50..], 0, &temp_k, &bidirectional_state.h, &[0; 0]);
-                                               final_hkdf = Self::hkdf_extract_expand(&bidirectional_state.ck, &[0; 0]);
-                                               ck = bidirectional_state.ck.clone();
-                                               res
-                                       },
-                                       _ => panic!("Wrong direction for act"),
-                               },
-                       _ => panic!("Cannot get act one after noise handshake completes"),
-               };
-
-               let (sk, rk) = final_hkdf;
-               self.noise_state = NoiseState::Finished {
-                       sk: sk,
-                       sn: 0,
-                       sck: ck.clone(),
-                       rk: rk,
-                       rn: 0,
-                       rck: ck,
-               };
-
-               Ok((res, self.their_node_id.unwrap().clone()))
-       }
-
-       pub fn process_act_three(&mut self, act_three: &[u8]) -> Result<PublicKey, HandleError> {
-               assert_eq!(act_three.len(), 66);
-
-               let final_hkdf;
-               let ck;
-               match self.noise_state {
-                       NoiseState::InProgress { ref state, ref directional_state, ref mut bidirectional_state } =>
-                               match directional_state {
-                                       &DirectionalNoiseState::Inbound { ie: _, ref re, ref temp_k2 } => {
-                                               if *state != NoiseStep::PostActTwo {
-                                                       panic!("Requested act at wrong step");
-                                               }
-                                               if act_three[0] != 0 {
-                                                       return Err(HandleError{err: "Unknown handshake version number", action: Some(msgs::ErrorAction::DisconnectPeer{ msg: None })});
-                                               }
-
-                                               let mut their_node_id = [0; 33];
-                                               PeerChannelEncryptor::decrypt_with_ad(&mut their_node_id, 1, &temp_k2.unwrap(), &bidirectional_state.h, &act_three[1..50])?;
-                                               self.their_node_id = Some(match PublicKey::from_slice(&their_node_id) {
-                                                       Ok(key) => key,
-                                                       Err(_) => return Err(HandleError{err: "Bad node_id from peer", action: Some(msgs::ErrorAction::DisconnectPeer{ msg: None })}),
-                                               });
-
-                                               let mut sha = Sha256::engine();
-                                               sha.input(&bidirectional_state.h);
-                                               sha.input(&act_three[1..50]);
-                                               bidirectional_state.h = Sha256::from_engine(sha).into_inner();
-
-                                               let ss = SharedSecret::new(&self.their_node_id.unwrap(), &re.unwrap());
-                                               let temp_k = PeerChannelEncryptor::hkdf(bidirectional_state, ss);
-
-                                               PeerChannelEncryptor::decrypt_with_ad(&mut [0; 0], 0, &temp_k, &bidirectional_state.h, &act_three[50..])?;
-                                               final_hkdf = Self::hkdf_extract_expand(&bidirectional_state.ck, &[0; 0]);
-                                               ck = bidirectional_state.ck.clone();
-                                       },
-                                       _ => panic!("Wrong direction for act"),
-                               },
-                       _ => panic!("Cannot get act one after noise handshake completes"),
-               }
-
-               let (rk, sk) = final_hkdf;
-               self.noise_state = NoiseState::Finished {
-                       sk: sk,
-                       sn: 0,
-                       sck: ck.clone(),
-                       rk: rk,
-                       rn: 0,
-                       rck: ck,
-               };
-
-               Ok(self.their_node_id.unwrap().clone())
-       }
-
-       /// Encrypts the given message, returning the encrypted version
-       /// panics if msg.len() > 65535 or Noise handshake has not finished.
-       pub fn encrypt_message(&mut self, msg: &[u8]) -> Vec<u8> {
-               if msg.len() > 65535 {
-                       panic!("Attempted to encrypt message longer than 65535 bytes!");
-               }
-
-               let mut res = Vec::with_capacity(msg.len() + 16*2 + 2);
-               res.resize(msg.len() + 16*2 + 2, 0);
-
-               match self.noise_state {
-                       NoiseState::Finished { ref mut sk, ref mut sn, ref mut sck, rk: _, rn: _, rck: _ } => {
-                               if *sn >= 1000 {
-                                       let (new_sck, new_sk) = Self::hkdf_extract_expand(sck, sk);
-                                       *sck = new_sck;
-                                       *sk = new_sk;
-                                       *sn = 0;
-                               }
-
-                               Self::encrypt_with_ad(&mut res[0..16+2], *sn, sk, &[0; 0], &byte_utils::be16_to_array(msg.len() as u16));
-                               *sn += 1;
-
-                               Self::encrypt_with_ad(&mut res[16+2..], *sn, sk, &[0; 0], msg);
-                               *sn += 1;
-                       },
-                       _ => panic!("Tried to encrypt a message prior to noise handshake completion"),
-               }
-
-               res
-       }
-
-       /// Decrypts a message length header from the remote peer.
-       /// panics if noise handshake has not yet finished or msg.len() != 18
-       pub fn decrypt_length_header(&mut self, msg: &[u8]) -> Result<u16, HandleError> {
-               assert_eq!(msg.len(), 16+2);
-
-               match self.noise_state {
-                       NoiseState::Finished { sk: _, sn: _, sck: _, ref mut rk, ref mut rn, ref mut rck } => {
-                               if *rn >= 1000 {
-                                       let (new_rck, new_rk) = Self::hkdf_extract_expand(rck, rk);
-                                       *rck = new_rck;
-                                       *rk = new_rk;
-                                       *rn = 0;
-                               }
-
-                               let mut res = [0; 2];
-                               Self::decrypt_with_ad(&mut res, *rn, rk, &[0; 0], msg)?;
-                               *rn += 1;
-                               Ok(byte_utils::slice_to_be16(&res))
-                       },
-                       _ => panic!("Tried to encrypt a message prior to noise handshake completion"),
-               }
-       }
-
-       /// Decrypts the given message.
-       /// panics if msg.len() > 65535 + 16
-       pub fn decrypt_message(&mut self, msg: &[u8]) -> Result<Vec<u8>, HandleError> {
-               if msg.len() > 65535 + 16 {
-                       panic!("Attempted to encrypt message longer than 65535 bytes!");
-               }
-
-               match self.noise_state {
-                       NoiseState::Finished { sk: _, sn: _, sck: _, ref rk, ref mut rn, rck: _ } => {
-                               let mut res = Vec::with_capacity(msg.len() - 16);
-                               res.resize(msg.len() - 16, 0);
-                               Self::decrypt_with_ad(&mut res[..], *rn, rk, &[0; 0], msg)?;
-                               *rn += 1;
-
-                               Ok(res)
-                       },
-                       _ => panic!("Tried to encrypt a message prior to noise handshake completion"),
-               }
-       }
-
-       pub fn get_noise_step(&self) -> NextNoiseStep {
-               match self.noise_state {
-                       NoiseState::InProgress {ref state, ..} => {
-                               match state {
-                                       &NoiseStep::PreActOne => NextNoiseStep::ActOne,
-                                       &NoiseStep::PostActOne => NextNoiseStep::ActTwo,
-                                       &NoiseStep::PostActTwo => NextNoiseStep::ActThree,
-                               }
-                       },
-                       NoiseState::Finished {..} => NextNoiseStep::NoiseComplete,
-               }
-       }
-
-       pub fn is_ready_for_encryption(&self) -> bool {
-               match self.noise_state {
-                       NoiseState::InProgress {..} => { false },
-                       NoiseState::Finished {..} => { true }
-               }
-       }
-}
-
-#[cfg(test)]
-mod tests {
-       use secp256k1::key::{PublicKey,SecretKey};
-
-       use hex;
-
-       use ln::peer_channel_encryptor::{PeerChannelEncryptor,NoiseState};
-
-       fn get_outbound_peer_for_initiator_test_vectors() -> PeerChannelEncryptor {
-               let their_node_id = PublicKey::from_slice(&hex::decode("028d7500dd4c12685d1f568b4c2b5048e8534b873319f3a8daa612b469132ec7f7").unwrap()[..]).unwrap();
-
-               let mut outbound_peer = PeerChannelEncryptor::new_outbound(their_node_id, SecretKey::from_slice(&hex::decode("1212121212121212121212121212121212121212121212121212121212121212").unwrap()[..]).unwrap());
-               assert_eq!(outbound_peer.get_act_one()[..], hex::decode("00036360e856310ce5d294e8be33fc807077dc56ac80d95d9cd4ddbd21325eff73f70df6086551151f58b8afe6c195782c6a").unwrap()[..]);
-               outbound_peer
-       }
-
-       #[test]
-       fn noise_initiator_test_vectors() {
-               let our_node_id = SecretKey::from_slice(&hex::decode("1111111111111111111111111111111111111111111111111111111111111111").unwrap()[..]).unwrap();
-
-               {
-                       // transport-initiator successful handshake
-                       let mut outbound_peer = get_outbound_peer_for_initiator_test_vectors();
-
-                       let act_two = hex::decode("0002466d7fcae563e5cb09a0d1870bb580344804617879a14949cf22285f1bae3f276e2470b93aac583c9ef6eafca3f730ae").unwrap().to_vec();
-                       assert_eq!(outbound_peer.process_act_two(&act_two[..], &our_node_id).unwrap().0[..], hex::decode("00b9e3a702e93e3a9948c2ed6e5fd7590a6e1c3a0344cfc9d5b57357049aa22355361aa02e55a8fc28fef5bd6d71ad0c38228dc68b1c466263b47fdf31e560e139ba").unwrap()[..]);
-
-                       match outbound_peer.noise_state {
-                               NoiseState::Finished { sk, sn, sck, rk, rn, rck } => {
-                                       assert_eq!(sk, hex::decode("969ab31b4d288cedf6218839b27a3e2140827047f2c0f01bf5c04435d43511a9").unwrap()[..]);
-                                       assert_eq!(sn, 0);
-                                       assert_eq!(sck, hex::decode("919219dbb2920afa8db80f9a51787a840bcf111ed8d588caf9ab4be716e42b01").unwrap()[..]);
-                                       assert_eq!(rk, hex::decode("bb9020b8965f4df047e07f955f3c4b88418984aadc5cdb35096b9ea8fa5c3442").unwrap()[..]);
-                                       assert_eq!(rn, 0);
-                                       assert_eq!(rck, hex::decode("919219dbb2920afa8db80f9a51787a840bcf111ed8d588caf9ab4be716e42b01").unwrap()[..]);
-                               },
-                               _ => panic!()
-                       }
-               }
-               {
-                       // transport-initiator act2 short read test
-                       // Can't actually test this cause process_act_two requires you pass the right length!
-               }
-               {
-                       // transport-initiator act2 bad version test
-                       let mut outbound_peer = get_outbound_peer_for_initiator_test_vectors();
-
-                       let act_two = hex::decode("0102466d7fcae563e5cb09a0d1870bb580344804617879a14949cf22285f1bae3f276e2470b93aac583c9ef6eafca3f730ae").unwrap().to_vec();
-                       assert!(outbound_peer.process_act_two(&act_two[..], &our_node_id).is_err());
-               }
-
-               {
-                       // transport-initiator act2 bad key serialization test
-                       let mut outbound_peer = get_outbound_peer_for_initiator_test_vectors();
-
-                       let act_two = hex::decode("0004466d7fcae563e5cb09a0d1870bb580344804617879a14949cf22285f1bae3f276e2470b93aac583c9ef6eafca3f730ae").unwrap().to_vec();
-                       assert!(outbound_peer.process_act_two(&act_two[..], &our_node_id).is_err());
-               }
-
-               {
-                       // transport-initiator act2 bad MAC test
-                       let mut outbound_peer = get_outbound_peer_for_initiator_test_vectors();
-
-                       let act_two = hex::decode("0002466d7fcae563e5cb09a0d1870bb580344804617879a14949cf22285f1bae3f276e2470b93aac583c9ef6eafca3f730af").unwrap().to_vec();
-                       assert!(outbound_peer.process_act_two(&act_two[..], &our_node_id).is_err());
-               }
-       }
-
-       #[test]
-       fn noise_responder_test_vectors() {
-               let our_node_id = SecretKey::from_slice(&hex::decode("2121212121212121212121212121212121212121212121212121212121212121").unwrap()[..]).unwrap();
-               let our_ephemeral = SecretKey::from_slice(&hex::decode("2222222222222222222222222222222222222222222222222222222222222222").unwrap()[..]).unwrap();
-
-               {
-                       // transport-responder successful handshake
-                       let mut inbound_peer = PeerChannelEncryptor::new_inbound(&our_node_id);
-
-                       let act_one = hex::decode("00036360e856310ce5d294e8be33fc807077dc56ac80d95d9cd4ddbd21325eff73f70df6086551151f58b8afe6c195782c6a").unwrap().to_vec();
-                       assert_eq!(inbound_peer.process_act_one_with_keys(&act_one[..], &our_node_id, our_ephemeral.clone()).unwrap()[..], hex::decode("0002466d7fcae563e5cb09a0d1870bb580344804617879a14949cf22285f1bae3f276e2470b93aac583c9ef6eafca3f730ae").unwrap()[..]);
-
-                       let act_three = hex::decode("00b9e3a702e93e3a9948c2ed6e5fd7590a6e1c3a0344cfc9d5b57357049aa22355361aa02e55a8fc28fef5bd6d71ad0c38228dc68b1c466263b47fdf31e560e139ba").unwrap().to_vec();
-                       // test vector doesn't specify the initiator static key, but it's the same as the one
-                       // from transport-initiator successful handshake
-                       assert_eq!(inbound_peer.process_act_three(&act_three[..]).unwrap().serialize()[..], hex::decode("034f355bdcb7cc0af728ef3cceb9615d90684bb5b2ca5f859ab0f0b704075871aa").unwrap()[..]);
-
-                       match inbound_peer.noise_state {
-                               NoiseState::Finished { sk, sn, sck, rk, rn, rck } => {
-                                       assert_eq!(sk, hex::decode("bb9020b8965f4df047e07f955f3c4b88418984aadc5cdb35096b9ea8fa5c3442").unwrap()[..]);
-                                       assert_eq!(sn, 0);
-                                       assert_eq!(sck, hex::decode("919219dbb2920afa8db80f9a51787a840bcf111ed8d588caf9ab4be716e42b01").unwrap()[..]);
-                                       assert_eq!(rk, hex::decode("969ab31b4d288cedf6218839b27a3e2140827047f2c0f01bf5c04435d43511a9").unwrap()[..]);
-                                       assert_eq!(rn, 0);
-                                       assert_eq!(rck, hex::decode("919219dbb2920afa8db80f9a51787a840bcf111ed8d588caf9ab4be716e42b01").unwrap()[..]);
-                               },
-                               _ => panic!()
-                       }
-               }
-               {
-                       // transport-responder act1 short read test
-                       // Can't actually test this cause process_act_one requires you pass the right length!
-               }
-               {
-                       // transport-responder act1 bad version test
-                       let mut inbound_peer = PeerChannelEncryptor::new_inbound(&our_node_id);
-
-                       let act_one = hex::decode("01036360e856310ce5d294e8be33fc807077dc56ac80d95d9cd4ddbd21325eff73f70df6086551151f58b8afe6c195782c6a").unwrap().to_vec();
-                       assert!(inbound_peer.process_act_one_with_keys(&act_one[..], &our_node_id, our_ephemeral.clone()).is_err());
-               }
-               {
-                       // transport-responder act1 bad key serialization test
-                       let mut inbound_peer = PeerChannelEncryptor::new_inbound(&our_node_id);
-
-                       let act_one =hex::decode("00046360e856310ce5d294e8be33fc807077dc56ac80d95d9cd4ddbd21325eff73f70df6086551151f58b8afe6c195782c6a").unwrap().to_vec();
-                       assert!(inbound_peer.process_act_one_with_keys(&act_one[..], &our_node_id, our_ephemeral.clone()).is_err());
-               }
-               {
-                       // transport-responder act1 bad MAC test
-                       let mut inbound_peer = PeerChannelEncryptor::new_inbound(&our_node_id);
-
-                       let act_one = hex::decode("00036360e856310ce5d294e8be33fc807077dc56ac80d95d9cd4ddbd21325eff73f70df6086551151f58b8afe6c195782c6b").unwrap().to_vec();
-                       assert!(inbound_peer.process_act_one_with_keys(&act_one[..], &our_node_id, our_ephemeral.clone()).is_err());
-               }
-               {
-                       // transport-responder act3 bad version test
-                       let mut inbound_peer = PeerChannelEncryptor::new_inbound(&our_node_id);
-
-                       let act_one = hex::decode("00036360e856310ce5d294e8be33fc807077dc56ac80d95d9cd4ddbd21325eff73f70df6086551151f58b8afe6c195782c6a").unwrap().to_vec();
-                       assert_eq!(inbound_peer.process_act_one_with_keys(&act_one[..], &our_node_id, our_ephemeral.clone()).unwrap()[..], hex::decode("0002466d7fcae563e5cb09a0d1870bb580344804617879a14949cf22285f1bae3f276e2470b93aac583c9ef6eafca3f730ae").unwrap()[..]);
-
-                       let act_three = hex::decode("01b9e3a702e93e3a9948c2ed6e5fd7590a6e1c3a0344cfc9d5b57357049aa22355361aa02e55a8fc28fef5bd6d71ad0c38228dc68b1c466263b47fdf31e560e139ba").unwrap().to_vec();
-                       assert!(inbound_peer.process_act_three(&act_three[..]).is_err());
-               }
-               {
-                       // transport-responder act3 short read test
-                       // Can't actually test this cause process_act_three requires you pass the right length!
-               }
-               {
-                       // transport-responder act3 bad MAC for ciphertext test
-                       let mut inbound_peer = PeerChannelEncryptor::new_inbound(&our_node_id);
-
-                       let act_one = hex::decode("00036360e856310ce5d294e8be33fc807077dc56ac80d95d9cd4ddbd21325eff73f70df6086551151f58b8afe6c195782c6a").unwrap().to_vec();
-                       assert_eq!(inbound_peer.process_act_one_with_keys(&act_one[..], &our_node_id, our_ephemeral.clone()).unwrap()[..], hex::decode("0002466d7fcae563e5cb09a0d1870bb580344804617879a14949cf22285f1bae3f276e2470b93aac583c9ef6eafca3f730ae").unwrap()[..]);
-
-                       let act_three = hex::decode("00c9e3a702e93e3a9948c2ed6e5fd7590a6e1c3a0344cfc9d5b57357049aa22355361aa02e55a8fc28fef5bd6d71ad0c38228dc68b1c466263b47fdf31e560e139ba").unwrap().to_vec();
-                       assert!(inbound_peer.process_act_three(&act_three[..]).is_err());
-               }
-               {
-                       // transport-responder act3 bad rs test
-                       let mut inbound_peer = PeerChannelEncryptor::new_inbound(&our_node_id);
-
-                       let act_one = hex::decode("00036360e856310ce5d294e8be33fc807077dc56ac80d95d9cd4ddbd21325eff73f70df6086551151f58b8afe6c195782c6a").unwrap().to_vec();
-                       assert_eq!(inbound_peer.process_act_one_with_keys(&act_one[..], &our_node_id, our_ephemeral.clone()).unwrap()[..], hex::decode("0002466d7fcae563e5cb09a0d1870bb580344804617879a14949cf22285f1bae3f276e2470b93aac583c9ef6eafca3f730ae").unwrap()[..]);
-
-                       let act_three = hex::decode("00bfe3a702e93e3a9948c2ed6e5fd7590a6e1c3a0344cfc9d5b57357049aa2235536ad09a8ee351870c2bb7f78b754a26c6cef79a98d25139c856d7efd252c2ae73c").unwrap().to_vec();
-                       assert!(inbound_peer.process_act_three(&act_three[..]).is_err());
-               }
-               {
-                       // transport-responder act3 bad MAC test
-                       let mut inbound_peer = PeerChannelEncryptor::new_inbound(&our_node_id);
-
-                       let act_one = hex::decode("00036360e856310ce5d294e8be33fc807077dc56ac80d95d9cd4ddbd21325eff73f70df6086551151f58b8afe6c195782c6a").unwrap().to_vec();
-                       assert_eq!(inbound_peer.process_act_one_with_keys(&act_one[..], &our_node_id, our_ephemeral.clone()).unwrap()[..], hex::decode("0002466d7fcae563e5cb09a0d1870bb580344804617879a14949cf22285f1bae3f276e2470b93aac583c9ef6eafca3f730ae").unwrap()[..]);
-
-                       let act_three = hex::decode("00b9e3a702e93e3a9948c2ed6e5fd7590a6e1c3a0344cfc9d5b57357049aa22355361aa02e55a8fc28fef5bd6d71ad0c38228dc68b1c466263b47fdf31e560e139bb").unwrap().to_vec();
-                       assert!(inbound_peer.process_act_three(&act_three[..]).is_err());
-               }
-       }
-
-
-       #[test]
-       fn message_encryption_decryption_test_vectors() {
-               // We use the same keys as the initiator and responder test vectors, so we copy those tests
-               // here and use them to encrypt.
-               let mut outbound_peer = get_outbound_peer_for_initiator_test_vectors();
-
-               {
-                       let our_node_id = SecretKey::from_slice(&hex::decode("1111111111111111111111111111111111111111111111111111111111111111").unwrap()[..]).unwrap();
-
-                       let act_two = hex::decode("0002466d7fcae563e5cb09a0d1870bb580344804617879a14949cf22285f1bae3f276e2470b93aac583c9ef6eafca3f730ae").unwrap().to_vec();
-                       assert_eq!(outbound_peer.process_act_two(&act_two[..], &our_node_id).unwrap().0[..], hex::decode("00b9e3a702e93e3a9948c2ed6e5fd7590a6e1c3a0344cfc9d5b57357049aa22355361aa02e55a8fc28fef5bd6d71ad0c38228dc68b1c466263b47fdf31e560e139ba").unwrap()[..]);
-
-                       match outbound_peer.noise_state {
-                               NoiseState::Finished { sk, sn, sck, rk, rn, rck } => {
-                                       assert_eq!(sk, hex::decode("969ab31b4d288cedf6218839b27a3e2140827047f2c0f01bf5c04435d43511a9").unwrap()[..]);
-                                       assert_eq!(sn, 0);
-                                       assert_eq!(sck, hex::decode("919219dbb2920afa8db80f9a51787a840bcf111ed8d588caf9ab4be716e42b01").unwrap()[..]);
-                                       assert_eq!(rk, hex::decode("bb9020b8965f4df047e07f955f3c4b88418984aadc5cdb35096b9ea8fa5c3442").unwrap()[..]);
-                                       assert_eq!(rn, 0);
-                                       assert_eq!(rck, hex::decode("919219dbb2920afa8db80f9a51787a840bcf111ed8d588caf9ab4be716e42b01").unwrap()[..]);
-                               },
-                               _ => panic!()
-                       }
-               }
-
-               let mut inbound_peer;
-
-               {
-                       // transport-responder successful handshake
-                       let our_node_id = SecretKey::from_slice(&hex::decode("2121212121212121212121212121212121212121212121212121212121212121").unwrap()[..]).unwrap();
-                       let our_ephemeral = SecretKey::from_slice(&hex::decode("2222222222222222222222222222222222222222222222222222222222222222").unwrap()[..]).unwrap();
-
-                       inbound_peer = PeerChannelEncryptor::new_inbound(&our_node_id);
-
-                       let act_one = hex::decode("00036360e856310ce5d294e8be33fc807077dc56ac80d95d9cd4ddbd21325eff73f70df6086551151f58b8afe6c195782c6a").unwrap().to_vec();
-                       assert_eq!(inbound_peer.process_act_one_with_keys(&act_one[..], &our_node_id, our_ephemeral.clone()).unwrap()[..], hex::decode("0002466d7fcae563e5cb09a0d1870bb580344804617879a14949cf22285f1bae3f276e2470b93aac583c9ef6eafca3f730ae").unwrap()[..]);
-
-                       let act_three = hex::decode("00b9e3a702e93e3a9948c2ed6e5fd7590a6e1c3a0344cfc9d5b57357049aa22355361aa02e55a8fc28fef5bd6d71ad0c38228dc68b1c466263b47fdf31e560e139ba").unwrap().to_vec();
-                       // test vector doesn't specify the initiator static key, but it's the same as the one
-                       // from transport-initiator successful handshake
-                       assert_eq!(inbound_peer.process_act_three(&act_three[..]).unwrap().serialize()[..], hex::decode("034f355bdcb7cc0af728ef3cceb9615d90684bb5b2ca5f859ab0f0b704075871aa").unwrap()[..]);
-
-                       match inbound_peer.noise_state {
-                               NoiseState::Finished { sk, sn, sck, rk, rn, rck } => {
-                                       assert_eq!(sk, hex::decode("bb9020b8965f4df047e07f955f3c4b88418984aadc5cdb35096b9ea8fa5c3442").unwrap()[..]);
-                                       assert_eq!(sn, 0);
-                                       assert_eq!(sck, hex::decode("919219dbb2920afa8db80f9a51787a840bcf111ed8d588caf9ab4be716e42b01").unwrap()[..]);
-                                       assert_eq!(rk, hex::decode("969ab31b4d288cedf6218839b27a3e2140827047f2c0f01bf5c04435d43511a9").unwrap()[..]);
-                                       assert_eq!(rn, 0);
-                                       assert_eq!(rck, hex::decode("919219dbb2920afa8db80f9a51787a840bcf111ed8d588caf9ab4be716e42b01").unwrap()[..]);
-                               },
-                               _ => panic!()
-                       }
-               }
-
-               for i in 0..1005 {
-                       let msg = [0x68, 0x65, 0x6c, 0x6c, 0x6f];
-                       let res = outbound_peer.encrypt_message(&msg);
-                       assert_eq!(res.len(), 5 + 2*16 + 2);
-
-                       let len_header = res[0..2+16].to_vec();
-                       assert_eq!(inbound_peer.decrypt_length_header(&len_header[..]).unwrap() as usize, msg.len());
-                       assert_eq!(inbound_peer.decrypt_message(&res[2+16..]).unwrap()[..], msg[..]);
-
-                       if i == 0 {
-                               assert_eq!(res, hex::decode("cf2b30ddf0cf3f80e7c35a6e6730b59fe802473180f396d88a8fb0db8cbcf25d2f214cf9ea1d95").unwrap());
-                       } else if i == 1 {
-                               assert_eq!(res, hex::decode("72887022101f0b6753e0c7de21657d35a4cb2a1f5cde2650528bbc8f837d0f0d7ad833b1a256a1").unwrap());
-                       } else if i == 500 {
-                               assert_eq!(res, hex::decode("178cb9d7387190fa34db9c2d50027d21793c9bc2d40b1e14dcf30ebeeeb220f48364f7a4c68bf8").unwrap());
-                       } else if i == 501 {
-                               assert_eq!(res, hex::decode("1b186c57d44eb6de4c057c49940d79bb838a145cb528d6e8fd26dbe50a60ca2c104b56b60e45bd").unwrap());
-                       } else if i == 1000 {
-                               assert_eq!(res, hex::decode("4a2f3cc3b5e78ddb83dcb426d9863d9d9a723b0337c89dd0b005d89f8d3c05c52b76b29b740f09").unwrap());
-                       } else if i == 1001 {
-                               assert_eq!(res, hex::decode("2ecd8c8a5629d0d02ab457a0fdd0f7b90a192cd46be5ecb6ca570bfc5e268338b1a16cf4ef2d36").unwrap());
-                       }
-               }
-       }
-}
diff --git a/src/ln/peer_handler.rs b/src/ln/peer_handler.rs
deleted file mode 100644 (file)
index 8094f25..0000000
+++ /dev/null
@@ -1,1183 +0,0 @@
-//! Top level peer message handling and socket handling logic lives here.
-//!
-//! Instead of actually servicing sockets ourselves we require that you implement the
-//! SocketDescriptor interface and use that to receive actions which you should perform on the
-//! socket, and call into PeerManager with bytes read from the socket. The PeerManager will then
-//! call into the provided message handlers (probably a ChannelManager and Router) with messages
-//! they should handle, and encoding/sending response messages.
-
-use secp256k1::key::{SecretKey,PublicKey};
-
-use ln::msgs;
-use util::ser::{Writeable, Writer, Readable};
-use ln::peer_channel_encryptor::{PeerChannelEncryptor,NextNoiseStep};
-use util::byte_utils;
-use util::events::{MessageSendEvent};
-use util::logger::Logger;
-
-use std::collections::{HashMap,hash_map,HashSet,LinkedList};
-use std::sync::{Arc, Mutex};
-use std::sync::atomic::{AtomicUsize, Ordering};
-use std::{cmp,error,hash,fmt};
-
-use bitcoin_hashes::sha256::Hash as Sha256;
-use bitcoin_hashes::sha256::HashEngine as Sha256Engine;
-use bitcoin_hashes::{HashEngine, Hash};
-
-/// Provides references to trait impls which handle different types of messages.
-pub struct MessageHandler {
-       /// A message handler which handles messages specific to channels. Usually this is just a
-       /// ChannelManager object.
-       pub chan_handler: Arc<msgs::ChannelMessageHandler>,
-       /// A message handler which handles messages updating our knowledge of the network channel
-       /// graph. Usually this is just a Router object.
-       pub route_handler: Arc<msgs::RoutingMessageHandler>,
-}
-
-/// Provides an object which can be used to send data to and which uniquely identifies a connection
-/// to a remote host. You will need to be able to generate multiple of these which meet Eq and
-/// implement Hash to meet the PeerManager API.
-///
-/// For efficiency, Clone should be relatively cheap for this type.
-///
-/// You probably want to just extend an int and put a file descriptor in a struct and implement
-/// send_data. Note that if you are using a higher-level net library that may close() itself, be
-/// careful to ensure you don't have races whereby you might register a new connection with an fd
-/// the same as a yet-to-be-disconnect_event()-ed.
-pub trait SocketDescriptor : cmp::Eq + hash::Hash + Clone {
-       /// Attempts to send some data from the given slice to the peer.
-       ///
-       /// Returns the amount of data which was sent, possibly 0 if the socket has since disconnected.
-       /// Note that in the disconnected case, a disconnect_event must still fire and further write
-       /// attempts may occur until that time.
-       ///
-       /// If the returned size is smaller than data.len(), a write_available event must
-       /// trigger the next time more data can be written. Additionally, until the a send_data event
-       /// completes fully, no further read_events should trigger on the same peer!
-       ///
-       /// If a read_event on this descriptor had previously returned true (indicating that read
-       /// events should be paused to prevent DoS in the send buffer), resume_read may be set
-       /// indicating that read events on this descriptor should resume. A resume_read of false does
-       /// *not* imply that further read events should be paused.
-       fn send_data(&mut self, data: &[u8], resume_read: bool) -> usize;
-       /// Disconnect the socket pointed to by this SocketDescriptor. Once this function returns, no
-       /// more calls to write_event, read_event or disconnect_event may be made with this descriptor.
-       /// No disconnect_event should be generated as a result of this call, though obviously races
-       /// may occur whereby disconnect_socket is called after a call to disconnect_event but prior to
-       /// that event completing.
-       fn disconnect_socket(&mut self);
-}
-
-/// Error for PeerManager errors. If you get one of these, you must disconnect the socket and
-/// generate no further read/write_events for the descriptor, only triggering a single
-/// disconnect_event (unless it was provided in response to a new_*_connection event, in which case
-/// no such disconnect_event must be generated and the socket be silently disconencted).
-pub struct PeerHandleError {
-       /// Used to indicate that we probably can't make any future connections to this peer, implying
-       /// we should go ahead and force-close any channels we have with it.
-       no_connection_possible: bool,
-}
-impl fmt::Debug for PeerHandleError {
-       fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
-               formatter.write_str("Peer Sent Invalid Data")
-       }
-}
-impl fmt::Display for PeerHandleError {
-       fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
-               formatter.write_str("Peer Sent Invalid Data")
-       }
-}
-impl error::Error for PeerHandleError {
-       fn description(&self) -> &str {
-               "Peer Sent Invalid Data"
-       }
-}
-
-enum InitSyncTracker{
-       NoSyncRequested,
-       ChannelsSyncing(u64),
-       NodesSyncing(PublicKey),
-}
-
-struct Peer {
-       channel_encryptor: PeerChannelEncryptor,
-       outbound: bool,
-       their_node_id: Option<PublicKey>,
-       their_global_features: Option<msgs::GlobalFeatures>,
-       their_local_features: Option<msgs::LocalFeatures>,
-
-       pending_outbound_buffer: LinkedList<Vec<u8>>,
-       pending_outbound_buffer_first_msg_offset: usize,
-       awaiting_write_event: bool,
-
-       pending_read_buffer: Vec<u8>,
-       pending_read_buffer_pos: usize,
-       pending_read_is_header: bool,
-
-       sync_status: InitSyncTracker,
-}
-
-impl Peer {
-       /// Returns true if the channel announcements/updates for the given channel should be
-       /// forwarded to this peer.
-       /// If we are sending our routing table to this peer and we have not yet sent channel
-       /// announcements/updates for the given channel_id then we will send it when we get to that
-       /// point and we shouldn't send it yet to avoid sending duplicate updates. If we've already
-       /// sent the old versions, we should send the update, and so return true here.
-       fn should_forward_channel(&self, channel_id: u64)->bool{
-               match self.sync_status {
-                       InitSyncTracker::NoSyncRequested => true,
-                       InitSyncTracker::ChannelsSyncing(i) => i < channel_id,
-                       InitSyncTracker::NodesSyncing(_) => true,
-               }
-       }
-}
-
-struct PeerHolder<Descriptor: SocketDescriptor> {
-       peers: HashMap<Descriptor, Peer>,
-       /// Added to by do_read_event for cases where we pushed a message onto the send buffer but
-       /// didn't call do_attempt_write_data to avoid reentrancy. Cleared in process_events()
-       peers_needing_send: HashSet<Descriptor>,
-       /// Only add to this set when noise completes:
-       node_id_to_descriptor: HashMap<PublicKey, Descriptor>,
-}
-struct MutPeerHolder<'a, Descriptor: SocketDescriptor + 'a> {
-       peers: &'a mut HashMap<Descriptor, Peer>,
-       peers_needing_send: &'a mut HashSet<Descriptor>,
-       node_id_to_descriptor: &'a mut HashMap<PublicKey, Descriptor>,
-}
-impl<Descriptor: SocketDescriptor> PeerHolder<Descriptor> {
-       fn borrow_parts(&mut self) -> MutPeerHolder<Descriptor> {
-               MutPeerHolder {
-                       peers: &mut self.peers,
-                       peers_needing_send: &mut self.peers_needing_send,
-                       node_id_to_descriptor: &mut self.node_id_to_descriptor,
-               }
-       }
-}
-
-#[cfg(not(any(target_pointer_width = "32", target_pointer_width = "64")))]
-fn _check_usize_is_32_or_64() {
-       // See below, less than 32 bit pointers may be unsafe here!
-       unsafe { mem::transmute::<*const usize, [u8; 4]>(panic!()); }
-}
-
-/// A PeerManager manages a set of peers, described by their SocketDescriptor and marshalls socket
-/// events into messages which it passes on to its MessageHandlers.
-pub struct PeerManager<Descriptor: SocketDescriptor> {
-       message_handler: MessageHandler,
-       peers: Mutex<PeerHolder<Descriptor>>,
-       our_node_secret: SecretKey,
-       ephemeral_key_midstate: Sha256Engine,
-
-       // Usize needs to be at least 32 bits to avoid overflowing both low and high. If usize is 64
-       // bits we will never realistically count into high:
-       peer_counter_low: AtomicUsize,
-       peer_counter_high: AtomicUsize,
-
-       initial_syncs_sent: AtomicUsize,
-       logger: Arc<Logger>,
-}
-
-struct VecWriter(Vec<u8>);
-impl Writer for VecWriter {
-       fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
-               self.0.extend_from_slice(buf);
-               Ok(())
-       }
-       fn size_hint(&mut self, size: usize) {
-               self.0.reserve_exact(size);
-       }
-}
-
-macro_rules! encode_msg {
-       ($msg: expr, $msg_code: expr) => {{
-               let mut msg = VecWriter(Vec::new());
-               ($msg_code as u16).write(&mut msg).unwrap();
-               $msg.write(&mut msg).unwrap();
-               msg.0
-       }}
-}
-
-//TODO: Really should do something smarter for this
-const INITIAL_SYNCS_TO_SEND: usize = 5;
-
-/// Manages and reacts to connection events. You probably want to use file descriptors as PeerIds.
-/// PeerIds may repeat, but only after disconnect_event() has been called.
-impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
-       /// Constructs a new PeerManager with the given message handlers and node_id secret key
-       /// ephemeral_random_data is used to derive per-connection ephemeral keys and must be
-       /// cryptographically secure random bytes.
-       pub fn new(message_handler: MessageHandler, our_node_secret: SecretKey, ephemeral_random_data: &[u8; 32], logger: Arc<Logger>) -> PeerManager<Descriptor> {
-               let mut ephemeral_key_midstate = Sha256::engine();
-               ephemeral_key_midstate.input(ephemeral_random_data);
-
-               PeerManager {
-                       message_handler: message_handler,
-                       peers: Mutex::new(PeerHolder {
-                               peers: HashMap::new(),
-                               peers_needing_send: HashSet::new(),
-                               node_id_to_descriptor: HashMap::new()
-                       }),
-                       our_node_secret: our_node_secret,
-                       ephemeral_key_midstate,
-                       peer_counter_low: AtomicUsize::new(0),
-                       peer_counter_high: AtomicUsize::new(0),
-                       initial_syncs_sent: AtomicUsize::new(0),
-                       logger,
-               }
-       }
-
-       /// Get the list of node ids for peers which have completed the initial handshake.
-       ///
-       /// For outbound connections, this will be the same as the their_node_id parameter passed in to
-       /// new_outbound_connection, however entries will only appear once the initial handshake has
-       /// completed and we are sure the remote peer has the private key for the given node_id.
-       pub fn get_peer_node_ids(&self) -> Vec<PublicKey> {
-               let peers = self.peers.lock().unwrap();
-               peers.peers.values().filter_map(|p| {
-                       if !p.channel_encryptor.is_ready_for_encryption() || p.their_global_features.is_none() {
-                               return None;
-                       }
-                       p.their_node_id
-               }).collect()
-       }
-
-       fn get_ephemeral_key(&self) -> SecretKey {
-               let mut ephemeral_hash = self.ephemeral_key_midstate.clone();
-               let low = self.peer_counter_low.fetch_add(1, Ordering::AcqRel);
-               let high = if low == 0 {
-                       self.peer_counter_high.fetch_add(1, Ordering::AcqRel)
-               } else {
-                       self.peer_counter_high.load(Ordering::Acquire)
-               };
-               ephemeral_hash.input(&byte_utils::le64_to_array(low as u64));
-               ephemeral_hash.input(&byte_utils::le64_to_array(high as u64));
-               SecretKey::from_slice(&Sha256::from_engine(ephemeral_hash).into_inner()).expect("You broke SHA-256!")
-       }
-
-       /// Indicates a new outbound connection has been established to a node with the given node_id.
-       /// Note that if an Err is returned here you MUST NOT call disconnect_event for the new
-       /// descriptor but must disconnect the connection immediately.
-       ///
-       /// Returns a small number of bytes to send to the remote node (currently always 50).
-       ///
-       /// Panics if descriptor is duplicative with some other descriptor which has not yet has a
-       /// disconnect_event.
-       pub fn new_outbound_connection(&self, their_node_id: PublicKey, descriptor: Descriptor) -> Result<Vec<u8>, PeerHandleError> {
-               let mut peer_encryptor = PeerChannelEncryptor::new_outbound(their_node_id.clone(), self.get_ephemeral_key());
-               let res = peer_encryptor.get_act_one().to_vec();
-               let pending_read_buffer = [0; 50].to_vec(); // Noise act two is 50 bytes
-
-               let mut peers = self.peers.lock().unwrap();
-               if peers.peers.insert(descriptor, Peer {
-                       channel_encryptor: peer_encryptor,
-                       outbound: true,
-                       their_node_id: None,
-                       their_global_features: None,
-                       their_local_features: None,
-
-                       pending_outbound_buffer: LinkedList::new(),
-                       pending_outbound_buffer_first_msg_offset: 0,
-                       awaiting_write_event: false,
-
-                       pending_read_buffer: pending_read_buffer,
-                       pending_read_buffer_pos: 0,
-                       pending_read_is_header: false,
-
-                       sync_status: InitSyncTracker::NoSyncRequested,
-               }).is_some() {
-                       panic!("PeerManager driver duplicated descriptors!");
-               };
-               Ok(res)
-       }
-
-       /// Indicates a new inbound connection has been established.
-       ///
-       /// May refuse the connection by returning an Err, but will never write bytes to the remote end
-       /// (outbound connector always speaks first). Note that if an Err is returned here you MUST NOT
-       /// call disconnect_event for the new descriptor but must disconnect the connection
-       /// immediately.
-       ///
-       /// Panics if descriptor is duplicative with some other descriptor which has not yet has a
-       /// disconnect_event.
-       pub fn new_inbound_connection(&self, descriptor: Descriptor) -> Result<(), PeerHandleError> {
-               let peer_encryptor = PeerChannelEncryptor::new_inbound(&self.our_node_secret);
-               let pending_read_buffer = [0; 50].to_vec(); // Noise act one is 50 bytes
-
-               let mut peers = self.peers.lock().unwrap();
-               if peers.peers.insert(descriptor, Peer {
-                       channel_encryptor: peer_encryptor,
-                       outbound: false,
-                       their_node_id: None,
-                       their_global_features: None,
-                       their_local_features: None,
-
-                       pending_outbound_buffer: LinkedList::new(),
-                       pending_outbound_buffer_first_msg_offset: 0,
-                       awaiting_write_event: false,
-
-                       pending_read_buffer: pending_read_buffer,
-                       pending_read_buffer_pos: 0,
-                       pending_read_is_header: false,
-
-                       sync_status: InitSyncTracker::NoSyncRequested,
-               }).is_some() {
-                       panic!("PeerManager driver duplicated descriptors!");
-               };
-               Ok(())
-       }
-
-       fn do_attempt_write_data(&self, descriptor: &mut Descriptor, peer: &mut Peer) {
-               macro_rules! encode_and_send_msg {
-                       ($msg: expr, $msg_code: expr) => {
-                               {
-                                       log_trace!(self, "Encoding and sending sync update message of type {} to {}", $msg_code, log_pubkey!(peer.their_node_id.unwrap()));
-                                       peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!($msg, $msg_code)[..]));
-                               }
-                       }
-               }
-               const MSG_BUFF_SIZE: usize = 10;
-               while !peer.awaiting_write_event {
-                       if peer.pending_outbound_buffer.len() < MSG_BUFF_SIZE {
-                               match peer.sync_status {
-                                       InitSyncTracker::NoSyncRequested => {},
-                                       InitSyncTracker::ChannelsSyncing(c) if c < 0xffff_ffff_ffff_ffff => {
-                                               let steps = ((MSG_BUFF_SIZE - peer.pending_outbound_buffer.len() + 2) / 3) as u8;
-                                               let all_messages = self.message_handler.route_handler.get_next_channel_announcements(0, steps);
-                                               for &(ref announce, ref update_a, ref update_b) in all_messages.iter() {
-                                                       encode_and_send_msg!(announce, 256);
-                                                       encode_and_send_msg!(update_a, 258);
-                                                       encode_and_send_msg!(update_b, 258);
-                                                       peer.sync_status = InitSyncTracker::ChannelsSyncing(announce.contents.short_channel_id + 1);
-                                               }
-                                               if all_messages.is_empty() || all_messages.len() != steps as usize {
-                                                       peer.sync_status = InitSyncTracker::ChannelsSyncing(0xffff_ffff_ffff_ffff);
-                                               }
-                                       },
-                                       InitSyncTracker::ChannelsSyncing(c) if c == 0xffff_ffff_ffff_ffff => {
-                                               let steps = (MSG_BUFF_SIZE - peer.pending_outbound_buffer.len()) as u8;
-                                               let all_messages = self.message_handler.route_handler.get_next_node_announcements(None, steps);
-                                               for msg in all_messages.iter() {
-                                                       encode_and_send_msg!(msg, 256);
-                                                       peer.sync_status = InitSyncTracker::NodesSyncing(msg.contents.node_id);
-                                               }
-                                               if all_messages.is_empty() || all_messages.len() != steps as usize {
-                                                       peer.sync_status = InitSyncTracker::NoSyncRequested;
-                                               }
-                                       },
-                                       InitSyncTracker::ChannelsSyncing(_) => unreachable!(),
-                                       InitSyncTracker::NodesSyncing(key) => {
-                                               let steps = (MSG_BUFF_SIZE - peer.pending_outbound_buffer.len()) as u8;
-                                               let all_messages = self.message_handler.route_handler.get_next_node_announcements(Some(&key), steps);
-                                               for msg in all_messages.iter() {
-                                                       encode_and_send_msg!(msg, 256);
-                                                       peer.sync_status = InitSyncTracker::NodesSyncing(msg.contents.node_id);
-                                               }
-                                               if all_messages.is_empty() || all_messages.len() != steps as usize {
-                                                       peer.sync_status = InitSyncTracker::NoSyncRequested;
-                                               }
-                                       },
-                               }
-                       }
-
-                       if {
-                               let next_buff = match peer.pending_outbound_buffer.front() {
-                                       None => return,
-                                       Some(buff) => buff,
-                               };
-
-                               let should_be_reading = peer.pending_outbound_buffer.len() < MSG_BUFF_SIZE;
-                               let pending = &next_buff[peer.pending_outbound_buffer_first_msg_offset..];
-                               let data_sent = descriptor.send_data(pending, should_be_reading);
-                               peer.pending_outbound_buffer_first_msg_offset += data_sent;
-                               if peer.pending_outbound_buffer_first_msg_offset == next_buff.len() { true } else { false }
-                       } {
-                               peer.pending_outbound_buffer_first_msg_offset = 0;
-                               peer.pending_outbound_buffer.pop_front();
-                       } else {
-                               peer.awaiting_write_event = true;
-                       }
-               }
-       }
-
-       /// Indicates that there is room to write data to the given socket descriptor.
-       ///
-       /// May return an Err to indicate that the connection should be closed.
-       ///
-       /// Will most likely call send_data on the descriptor passed in (or the descriptor handed into
-       /// new_*\_connection) before returning. Thus, be very careful with reentrancy issues! The
-       /// invariants around calling write_event in case a write did not fully complete must still
-       /// hold - be ready to call write_event again if a write call generated here isn't sufficient!
-       /// Panics if the descriptor was not previously registered in a new_\*_connection event.
-       pub fn write_event(&self, descriptor: &mut Descriptor) -> Result<(), PeerHandleError> {
-               let mut peers = self.peers.lock().unwrap();
-               match peers.peers.get_mut(descriptor) {
-                       None => panic!("Descriptor for write_event is not already known to PeerManager"),
-                       Some(peer) => {
-                               peer.awaiting_write_event = false;
-                               self.do_attempt_write_data(descriptor, peer);
-                       }
-               };
-               Ok(())
-       }
-
-       /// Indicates that data was read from the given socket descriptor.
-       ///
-       /// May return an Err to indicate that the connection should be closed.
-       ///
-       /// Will *not* call back into send_data on any descriptors to avoid reentrancy complexity.
-       /// Thus, however, you almost certainly want to call process_events() after any read_event to
-       /// generate send_data calls to handle responses.
-       ///
-       /// If Ok(true) is returned, further read_events should not be triggered until a write_event on
-       /// this file descriptor has resume_read set (preventing DoS issues in the send buffer).
-       ///
-       /// Panics if the descriptor was not previously registered in a new_*_connection event.
-       pub fn read_event(&self, peer_descriptor: &mut Descriptor, data: Vec<u8>) -> Result<bool, PeerHandleError> {
-               match self.do_read_event(peer_descriptor, data) {
-                       Ok(res) => Ok(res),
-                       Err(e) => {
-                               self.disconnect_event_internal(peer_descriptor, e.no_connection_possible);
-                               Err(e)
-                       }
-               }
-       }
-
-       fn do_read_event(&self, peer_descriptor: &mut Descriptor, data: Vec<u8>) -> Result<bool, PeerHandleError> {
-               let pause_read = {
-                       let mut peers_lock = self.peers.lock().unwrap();
-                       let peers = peers_lock.borrow_parts();
-                       let pause_read = match peers.peers.get_mut(peer_descriptor) {
-                               None => panic!("Descriptor for read_event is not already known to PeerManager"),
-                               Some(peer) => {
-                                       assert!(peer.pending_read_buffer.len() > 0);
-                                       assert!(peer.pending_read_buffer.len() > peer.pending_read_buffer_pos);
-
-                                       let mut read_pos = 0;
-                                       while read_pos < data.len() {
-                                               {
-                                                       let data_to_copy = cmp::min(peer.pending_read_buffer.len() - peer.pending_read_buffer_pos, data.len() - read_pos);
-                                                       peer.pending_read_buffer[peer.pending_read_buffer_pos..peer.pending_read_buffer_pos + data_to_copy].copy_from_slice(&data[read_pos..read_pos + data_to_copy]);
-                                                       read_pos += data_to_copy;
-                                                       peer.pending_read_buffer_pos += data_to_copy;
-                                               }
-
-                                               if peer.pending_read_buffer_pos == peer.pending_read_buffer.len() {
-                                                       peer.pending_read_buffer_pos = 0;
-
-                                                       macro_rules! encode_and_send_msg {
-                                                               ($msg: expr, $msg_code: expr) => {
-                                                                       {
-                                                                               log_trace!(self, "Encoding and sending message of type {} to {}", $msg_code, log_pubkey!(peer.their_node_id.unwrap()));
-                                                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!($msg, $msg_code)[..]));
-                                                                               peers.peers_needing_send.insert(peer_descriptor.clone());
-                                                                       }
-                                                               }
-                                                       }
-
-                                                       macro_rules! try_potential_handleerror {
-                                                               ($thing: expr) => {
-                                                                       match $thing {
-                                                                               Ok(x) => x,
-                                                                               Err(e) => {
-                                                                                       if let Some(action) = e.action {
-                                                                                               match action {
-                                                                                                       msgs::ErrorAction::DisconnectPeer { msg: _ } => {
-                                                                                                               //TODO: Try to push msg
-                                                                                                               log_trace!(self, "Got Err handling message, disconnecting peer because {}", e.err);
-                                                                                                               return Err(PeerHandleError{ no_connection_possible: false });
-                                                                                                       },
-                                                                                                       msgs::ErrorAction::IgnoreError => {
-                                                                                                               log_trace!(self, "Got Err handling message, ignoring because {}", e.err);
-                                                                                                               continue;
-                                                                                                       },
-                                                                                                       msgs::ErrorAction::SendErrorMessage { msg } => {
-                                                                                                               log_trace!(self, "Got Err handling message, sending Error message because {}", e.err);
-                                                                                                               encode_and_send_msg!(msg, 17);
-                                                                                                               continue;
-                                                                                                       },
-                                                                                               }
-                                                                                       } else {
-                                                                                               log_debug!(self, "Got Err handling message, action not yet filled in: {}", e.err);
-                                                                                               return Err(PeerHandleError{ no_connection_possible: false });
-                                                                                       }
-                                                                               }
-                                                                       };
-                                                               }
-                                                       }
-
-                                                       macro_rules! try_potential_decodeerror {
-                                                               ($thing: expr) => {
-                                                                       match $thing {
-                                                                               Ok(x) => x,
-                                                                               Err(e) => {
-                                                                                       match e {
-                                                                                               msgs::DecodeError::UnknownVersion => return Err(PeerHandleError{ no_connection_possible: false }),
-                                                                                               msgs::DecodeError::UnknownRequiredFeature => {
-                                                                                                       log_debug!(self, "Got a channel/node announcement with an known required feature flag, you may want to update!");
-                                                                                                       continue;
-                                                                                               },
-                                                                                               msgs::DecodeError::InvalidValue => {
-                                                                                                       log_debug!(self, "Got an invalid value while deserializing message");
-                                                                                                       return Err(PeerHandleError{ no_connection_possible: false });
-                                                                                               },
-                                                                                               msgs::DecodeError::ShortRead => {
-                                                                                                       log_debug!(self, "Deserialization failed due to shortness of message");
-                                                                                                       return Err(PeerHandleError{ no_connection_possible: false });
-                                                                                               },
-                                                                                               msgs::DecodeError::ExtraAddressesPerType => {
-                                                                                                       log_debug!(self, "Error decoding message, ignoring due to lnd spec incompatibility. See https://github.com/lightningnetwork/lnd/issues/1407");
-                                                                                                       continue;
-                                                                                               },
-                                                                                               msgs::DecodeError::BadLengthDescriptor => return Err(PeerHandleError{ no_connection_possible: false }),
-                                                                                               msgs::DecodeError::Io(_) => return Err(PeerHandleError{ no_connection_possible: false }),
-                                                                                       }
-                                                                               }
-                                                                       };
-                                                               }
-                                                       }
-
-                                                       macro_rules! insert_node_id {
-                                                               () => {
-                                                                       match peers.node_id_to_descriptor.entry(peer.their_node_id.unwrap()) {
-                                                                               hash_map::Entry::Occupied(_) => {
-                                                                                       log_trace!(self, "Got second connection with {}, closing", log_pubkey!(peer.their_node_id.unwrap()));
-                                                                                       peer.their_node_id = None; // Unset so that we don't generate a peer_disconnected event
-                                                                                       return Err(PeerHandleError{ no_connection_possible: false })
-                                                                               },
-                                                                               hash_map::Entry::Vacant(entry) => {
-                                                                                       log_trace!(self, "Finished noise handshake for connection with {}", log_pubkey!(peer.their_node_id.unwrap()));
-                                                                                       entry.insert(peer_descriptor.clone())
-                                                                               },
-                                                                       };
-                                                               }
-                                                       }
-
-                                                       let next_step = peer.channel_encryptor.get_noise_step();
-                                                       match next_step {
-                                                               NextNoiseStep::ActOne => {
-                                                                       let act_two = try_potential_handleerror!(peer.channel_encryptor.process_act_one_with_keys(&peer.pending_read_buffer[..], &self.our_node_secret, self.get_ephemeral_key())).to_vec();
-                                                                       peer.pending_outbound_buffer.push_back(act_two);
-                                                                       peer.pending_read_buffer = [0; 66].to_vec(); // act three is 66 bytes long
-                                                               },
-                                                               NextNoiseStep::ActTwo => {
-                                                                       let (act_three, their_node_id) = try_potential_handleerror!(peer.channel_encryptor.process_act_two(&peer.pending_read_buffer[..], &self.our_node_secret));
-                                                                       peer.pending_outbound_buffer.push_back(act_three.to_vec());
-                                                                       peer.pending_read_buffer = [0; 18].to_vec(); // Message length header is 18 bytes
-                                                                       peer.pending_read_is_header = true;
-
-                                                                       peer.their_node_id = Some(their_node_id);
-                                                                       insert_node_id!();
-                                                                       let mut local_features = msgs::LocalFeatures::new();
-                                                                       if self.initial_syncs_sent.load(Ordering::Acquire) < INITIAL_SYNCS_TO_SEND {
-                                                                               self.initial_syncs_sent.fetch_add(1, Ordering::AcqRel);
-                                                                               local_features.set_initial_routing_sync();
-                                                                       }
-                                                                       encode_and_send_msg!(msgs::Init {
-                                                                               global_features: msgs::GlobalFeatures::new(),
-                                                                               local_features,
-                                                                       }, 16);
-                                                               },
-                                                               NextNoiseStep::ActThree => {
-                                                                       let their_node_id = try_potential_handleerror!(peer.channel_encryptor.process_act_three(&peer.pending_read_buffer[..]));
-                                                                       peer.pending_read_buffer = [0; 18].to_vec(); // Message length header is 18 bytes
-                                                                       peer.pending_read_is_header = true;
-                                                                       peer.their_node_id = Some(their_node_id);
-                                                                       insert_node_id!();
-                                                               },
-                                                               NextNoiseStep::NoiseComplete => {
-                                                                       if peer.pending_read_is_header {
-                                                                               let msg_len = try_potential_handleerror!(peer.channel_encryptor.decrypt_length_header(&peer.pending_read_buffer[..]));
-                                                                               peer.pending_read_buffer = Vec::with_capacity(msg_len as usize + 16);
-                                                                               peer.pending_read_buffer.resize(msg_len as usize + 16, 0);
-                                                                               if msg_len < 2 { // Need at least the message type tag
-                                                                                       return Err(PeerHandleError{ no_connection_possible: false });
-                                                                               }
-                                                                               peer.pending_read_is_header = false;
-                                                                       } else {
-                                                                               let msg_data = try_potential_handleerror!(peer.channel_encryptor.decrypt_message(&peer.pending_read_buffer[..]));
-                                                                               assert!(msg_data.len() >= 2);
-
-                                                                               // Reset read buffer
-                                                                               peer.pending_read_buffer = [0; 18].to_vec();
-                                                                               peer.pending_read_is_header = true;
-
-                                                                               let msg_type = byte_utils::slice_to_be16(&msg_data[0..2]);
-                                                                               log_trace!(self, "Received message of type {} from {}", msg_type, log_pubkey!(peer.their_node_id.unwrap()));
-                                                                               if msg_type != 16 && peer.their_global_features.is_none() {
-                                                                                       // Need an init message as first message
-                                                                                       log_trace!(self, "Peer {} sent non-Init first message", log_pubkey!(peer.their_node_id.unwrap()));
-                                                                                       return Err(PeerHandleError{ no_connection_possible: false });
-                                                                               }
-                                                                               let mut reader = ::std::io::Cursor::new(&msg_data[2..]);
-                                                                               match msg_type {
-                                                                                       // Connection control:
-                                                                                       16 => {
-                                                                                               let msg = try_potential_decodeerror!(msgs::Init::read(&mut reader));
-                                                                                               if msg.global_features.requires_unknown_bits() {
-                                                                                                       log_info!(self, "Peer global features required unknown version bits");
-                                                                                                       return Err(PeerHandleError{ no_connection_possible: true });
-                                                                                               }
-                                                                                               if msg.local_features.requires_unknown_bits() {
-                                                                                                       log_info!(self, "Peer local features required unknown version bits");
-                                                                                                       return Err(PeerHandleError{ no_connection_possible: true });
-                                                                                               }
-                                                                                               if peer.their_global_features.is_some() {
-                                                                                                       return Err(PeerHandleError{ no_connection_possible: false });
-                                                                                               }
-
-                                                                                               log_info!(self, "Received peer Init message: data_loss_protect: {}, initial_routing_sync: {}, upfront_shutdown_script: {}, unkown local flags: {}, unknown global flags: {}",
-                                                                                                       if msg.local_features.supports_data_loss_protect() { "supported" } else { "not supported"},
-                                                                                                       if msg.local_features.initial_routing_sync() { "requested" } else { "not requested" },
-                                                                                                       if msg.local_features.supports_upfront_shutdown_script() { "supported" } else { "not supported"},
-                                                                                                       if msg.local_features.supports_unknown_bits() { "present" } else { "none" },
-                                                                                                       if msg.global_features.supports_unknown_bits() { "present" } else { "none" });
-
-                                                                                               if msg.local_features.initial_routing_sync() {
-                                                                                                       peer.sync_status = InitSyncTracker::ChannelsSyncing(0);
-                                                                                                       peers.peers_needing_send.insert(peer_descriptor.clone());
-                                                                                               }
-                                                                                               peer.their_global_features = Some(msg.global_features);
-                                                                                               peer.their_local_features = Some(msg.local_features);
-
-                                                                                               if !peer.outbound {
-                                                                                                       let mut local_features = msgs::LocalFeatures::new();
-                                                                                                       if self.initial_syncs_sent.load(Ordering::Acquire) < INITIAL_SYNCS_TO_SEND {
-                                                                                                               self.initial_syncs_sent.fetch_add(1, Ordering::AcqRel);
-                                                                                                               local_features.set_initial_routing_sync();
-                                                                                                       }
-
-                                                                                                       encode_and_send_msg!(msgs::Init {
-                                                                                                               global_features: msgs::GlobalFeatures::new(),
-                                                                                                               local_features,
-                                                                                                       }, 16);
-                                                                                               }
-
-                                                                                               self.message_handler.chan_handler.peer_connected(&peer.their_node_id.unwrap());
-                                                                                       },
-                                                                                       17 => {
-                                                                                               let msg = try_potential_decodeerror!(msgs::ErrorMessage::read(&mut reader));
-                                                                                               let mut data_is_printable = true;
-                                                                                               for b in msg.data.bytes() {
-                                                                                                       if b < 32 || b > 126 {
-                                                                                                               data_is_printable = false;
-                                                                                                               break;
-                                                                                                       }
-                                                                                               }
-
-                                                                                               if data_is_printable {
-                                                                                                       log_debug!(self, "Got Err message from {}: {}", log_pubkey!(peer.their_node_id.unwrap()), msg.data);
-                                                                                               } else {
-                                                                                                       log_debug!(self, "Got Err message from {} with non-ASCII error message", log_pubkey!(peer.their_node_id.unwrap()));
-                                                                                               }
-                                                                                               self.message_handler.chan_handler.handle_error(&peer.their_node_id.unwrap(), &msg);
-                                                                                               if msg.channel_id == [0; 32] {
-                                                                                                       return Err(PeerHandleError{ no_connection_possible: true });
-                                                                                               }
-                                                                                       },
-
-                                                                                       18 => {
-                                                                                               let msg = try_potential_decodeerror!(msgs::Ping::read(&mut reader));
-                                                                                               if msg.ponglen < 65532 {
-                                                                                                       let resp = msgs::Pong { byteslen: msg.ponglen };
-                                                                                                       encode_and_send_msg!(resp, 19);
-                                                                                               }
-                                                                                       },
-                                                                                       19 => {
-                                                                                               try_potential_decodeerror!(msgs::Pong::read(&mut reader));
-                                                                                       },
-
-                                                                                       // Channel control:
-                                                                                       32 => {
-                                                                                               let msg = try_potential_decodeerror!(msgs::OpenChannel::read(&mut reader));
-                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_open_channel(&peer.their_node_id.unwrap(), peer.their_local_features.clone().unwrap(), &msg));
-                                                                                       },
-                                                                                       33 => {
-                                                                                               let msg = try_potential_decodeerror!(msgs::AcceptChannel::read(&mut reader));
-                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_accept_channel(&peer.their_node_id.unwrap(), peer.their_local_features.clone().unwrap(), &msg));
-                                                                                       },
-
-                                                                                       34 => {
-                                                                                               let msg = try_potential_decodeerror!(msgs::FundingCreated::read(&mut reader));
-                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_funding_created(&peer.their_node_id.unwrap(), &msg));
-                                                                                       },
-                                                                                       35 => {
-                                                                                               let msg = try_potential_decodeerror!(msgs::FundingSigned::read(&mut reader));
-                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_funding_signed(&peer.their_node_id.unwrap(), &msg));
-                                                                                       },
-                                                                                       36 => {
-                                                                                               let msg = try_potential_decodeerror!(msgs::FundingLocked::read(&mut reader));
-                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_funding_locked(&peer.their_node_id.unwrap(), &msg));
-                                                                                       },
-
-                                                                                       38 => {
-                                                                                               let msg = try_potential_decodeerror!(msgs::Shutdown::read(&mut reader));
-                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_shutdown(&peer.their_node_id.unwrap(), &msg));
-                                                                                       },
-                                                                                       39 => {
-                                                                                               let msg = try_potential_decodeerror!(msgs::ClosingSigned::read(&mut reader));
-                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_closing_signed(&peer.their_node_id.unwrap(), &msg));
-                                                                                       },
-
-                                                                                       128 => {
-                                                                                               let msg = try_potential_decodeerror!(msgs::UpdateAddHTLC::read(&mut reader));
-                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_update_add_htlc(&peer.their_node_id.unwrap(), &msg));
-                                                                                       },
-                                                                                       130 => {
-                                                                                               let msg = try_potential_decodeerror!(msgs::UpdateFulfillHTLC::read(&mut reader));
-                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_update_fulfill_htlc(&peer.their_node_id.unwrap(), &msg));
-                                                                                       },
-                                                                                       131 => {
-                                                                                               let msg = try_potential_decodeerror!(msgs::UpdateFailHTLC::read(&mut reader));
-                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_update_fail_htlc(&peer.their_node_id.unwrap(), &msg));
-                                                                                       },
-                                                                                       135 => {
-                                                                                               let msg = try_potential_decodeerror!(msgs::UpdateFailMalformedHTLC::read(&mut reader));
-                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_update_fail_malformed_htlc(&peer.their_node_id.unwrap(), &msg));
-                                                                                       },
-
-                                                                                       132 => {
-                                                                                               let msg = try_potential_decodeerror!(msgs::CommitmentSigned::read(&mut reader));
-                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_commitment_signed(&peer.their_node_id.unwrap(), &msg));
-                                                                                       },
-                                                                                       133 => {
-                                                                                               let msg = try_potential_decodeerror!(msgs::RevokeAndACK::read(&mut reader));
-                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_revoke_and_ack(&peer.their_node_id.unwrap(), &msg));
-                                                                                       },
-                                                                                       134 => {
-                                                                                               let msg = try_potential_decodeerror!(msgs::UpdateFee::read(&mut reader));
-                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_update_fee(&peer.their_node_id.unwrap(), &msg));
-                                                                                       },
-                                                                                       136 => {
-                                                                                               let msg = try_potential_decodeerror!(msgs::ChannelReestablish::read(&mut reader));
-                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_channel_reestablish(&peer.their_node_id.unwrap(), &msg));
-                                                                                       },
-
-                                                                                       // Routing control:
-                                                                                       259 => {
-                                                                                               let msg = try_potential_decodeerror!(msgs::AnnouncementSignatures::read(&mut reader));
-                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_announcement_signatures(&peer.their_node_id.unwrap(), &msg));
-                                                                                       },
-                                                                                       256 => {
-                                                                                               let msg = try_potential_decodeerror!(msgs::ChannelAnnouncement::read(&mut reader));
-                                                                                               let should_forward = try_potential_handleerror!(self.message_handler.route_handler.handle_channel_announcement(&msg));
-
-                                                                                               if should_forward {
-                                                                                                       // TODO: forward msg along to all our other peers!
-                                                                                               }
-                                                                                       },
-                                                                                       257 => {
-                                                                                               let msg = try_potential_decodeerror!(msgs::NodeAnnouncement::read(&mut reader));
-                                                                                               let should_forward = try_potential_handleerror!(self.message_handler.route_handler.handle_node_announcement(&msg));
-
-                                                                                               if should_forward {
-                                                                                                       // TODO: forward msg along to all our other peers!
-                                                                                               }
-                                                                                       },
-                                                                                       258 => {
-                                                                                               let msg = try_potential_decodeerror!(msgs::ChannelUpdate::read(&mut reader));
-                                                                                               let should_forward = try_potential_handleerror!(self.message_handler.route_handler.handle_channel_update(&msg));
-
-                                                                                               if should_forward {
-                                                                                                       // TODO: forward msg along to all our other peers!
-                                                                                               }
-                                                                                       },
-                                                                                       _ => {
-                                                                                               if (msg_type & 1) == 0 {
-                                                                                                       return Err(PeerHandleError{ no_connection_possible: true });
-                                                                                               }
-                                                                                       },
-                                                                               }
-                                                                       }
-                                                               }
-                                                       }
-                                               }
-                                       }
-
-                                       self.do_attempt_write_data(peer_descriptor, peer);
-
-                                       peer.pending_outbound_buffer.len() > 10 // pause_read
-                               }
-                       };
-
-                       pause_read
-               };
-
-               Ok(pause_read)
-       }
-
-       /// Checks for any events generated by our handlers and processes them. Includes sending most
-       /// response messages as well as messages generated by calls to handler functions directly (eg
-       /// functions like ChannelManager::process_pending_htlc_forward or send_payment).
-       pub fn process_events(&self) {
-               {
-                       // TODO: There are some DoS attacks here where you can flood someone's outbound send
-                       // buffer by doing things like announcing channels on another node. We should be willing to
-                       // drop optional-ish messages when send buffers get full!
-
-                       let mut events_generated = self.message_handler.chan_handler.get_and_clear_pending_msg_events();
-                       let mut peers_lock = self.peers.lock().unwrap();
-                       let peers = peers_lock.borrow_parts();
-                       for event in events_generated.drain(..) {
-                               macro_rules! get_peer_for_forwarding {
-                                       ($node_id: expr, $handle_no_such_peer: block) => {
-                                               {
-                                                       let descriptor = match peers.node_id_to_descriptor.get($node_id) {
-                                                               Some(descriptor) => descriptor.clone(),
-                                                               None => {
-                                                                       $handle_no_such_peer;
-                                                                       continue;
-                                                               },
-                                                       };
-                                                       match peers.peers.get_mut(&descriptor) {
-                                                               Some(peer) => {
-                                                                       if peer.their_global_features.is_none() {
-                                                                               $handle_no_such_peer;
-                                                                               continue;
-                                                                       }
-                                                                       (descriptor, peer)
-                                                               },
-                                                               None => panic!("Inconsistent peers set state!"),
-                                                       }
-                                               }
-                                       }
-                               }
-                               match event {
-                                       MessageSendEvent::SendAcceptChannel { ref node_id, ref msg } => {
-                                               log_trace!(self, "Handling SendAcceptChannel event in peer_handler for node {} for channel {}",
-                                                               log_pubkey!(node_id),
-                                                               log_bytes!(msg.temporary_channel_id));
-                                               let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
-                                                               //TODO: Drop the pending channel? (or just let it timeout, but that sucks)
-                                                       });
-                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 33)));
-                                               self.do_attempt_write_data(&mut descriptor, peer);
-                                       },
-                                       MessageSendEvent::SendOpenChannel { ref node_id, ref msg } => {
-                                               log_trace!(self, "Handling SendOpenChannel event in peer_handler for node {} for channel {}",
-                                                               log_pubkey!(node_id),
-                                                               log_bytes!(msg.temporary_channel_id));
-                                               let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
-                                                               //TODO: Drop the pending channel? (or just let it timeout, but that sucks)
-                                                       });
-                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 32)));
-                                               self.do_attempt_write_data(&mut descriptor, peer);
-                                       },
-                                       MessageSendEvent::SendFundingCreated { ref node_id, ref msg } => {
-                                               log_trace!(self, "Handling SendFundingCreated event in peer_handler for node {} for channel {} (which becomes {})",
-                                                               log_pubkey!(node_id),
-                                                               log_bytes!(msg.temporary_channel_id),
-                                                               log_funding_channel_id!(msg.funding_txid, msg.funding_output_index));
-                                               let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
-                                                               //TODO: generate a DiscardFunding event indicating to the wallet that
-                                                               //they should just throw away this funding transaction
-                                                       });
-                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 34)));
-                                               self.do_attempt_write_data(&mut descriptor, peer);
-                                       },
-                                       MessageSendEvent::SendFundingSigned { ref node_id, ref msg } => {
-                                               log_trace!(self, "Handling SendFundingSigned event in peer_handler for node {} for channel {}",
-                                                               log_pubkey!(node_id),
-                                                               log_bytes!(msg.channel_id));
-                                               let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
-                                                               //TODO: generate a DiscardFunding event indicating to the wallet that
-                                                               //they should just throw away this funding transaction
-                                                       });
-                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 35)));
-                                               self.do_attempt_write_data(&mut descriptor, peer);
-                                       },
-                                       MessageSendEvent::SendFundingLocked { ref node_id, ref msg } => {
-                                               log_trace!(self, "Handling SendFundingLocked event in peer_handler for node {} for channel {}",
-                                                               log_pubkey!(node_id),
-                                                               log_bytes!(msg.channel_id));
-                                               let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
-                                                               //TODO: Do whatever we're gonna do for handling dropped messages
-                                                       });
-                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 36)));
-                                               self.do_attempt_write_data(&mut descriptor, peer);
-                                       },
-                                       MessageSendEvent::SendAnnouncementSignatures { ref node_id, ref msg } => {
-                                               log_trace!(self, "Handling SendAnnouncementSignatures event in peer_handler for node {} for channel {})",
-                                                               log_pubkey!(node_id),
-                                                               log_bytes!(msg.channel_id));
-                                               let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
-                                                               //TODO: generate a DiscardFunding event indicating to the wallet that
-                                                               //they should just throw away this funding transaction
-                                                       });
-                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 259)));
-                                               self.do_attempt_write_data(&mut descriptor, peer);
-                                       },
-                                       MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => {
-                                               log_trace!(self, "Handling UpdateHTLCs event in peer_handler for node {} with {} adds, {} fulfills, {} fails for channel {}",
-                                                               log_pubkey!(node_id),
-                                                               update_add_htlcs.len(),
-                                                               update_fulfill_htlcs.len(),
-                                                               update_fail_htlcs.len(),
-                                                               log_bytes!(commitment_signed.channel_id));
-                                               let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
-                                                               //TODO: Do whatever we're gonna do for handling dropped messages
-                                                       });
-                                               for msg in update_add_htlcs {
-                                                       peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 128)));
-                                               }
-                                               for msg in update_fulfill_htlcs {
-                                                       peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 130)));
-                                               }
-                                               for msg in update_fail_htlcs {
-                                                       peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 131)));
-                                               }
-                                               for msg in update_fail_malformed_htlcs {
-                                                       peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 135)));
-                                               }
-                                               if let &Some(ref msg) = update_fee {
-                                                       peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 134)));
-                                               }
-                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(commitment_signed, 132)));
-                                               self.do_attempt_write_data(&mut descriptor, peer);
-                                       },
-                                       MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
-                                               log_trace!(self, "Handling SendRevokeAndACK event in peer_handler for node {} for channel {}",
-                                                               log_pubkey!(node_id),
-                                                               log_bytes!(msg.channel_id));
-                                               let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
-                                                               //TODO: Do whatever we're gonna do for handling dropped messages
-                                                       });
-                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 133)));
-                                               self.do_attempt_write_data(&mut descriptor, peer);
-                                       },
-                                       MessageSendEvent::SendClosingSigned { ref node_id, ref msg } => {
-                                               log_trace!(self, "Handling SendClosingSigned event in peer_handler for node {} for channel {}",
-                                                               log_pubkey!(node_id),
-                                                               log_bytes!(msg.channel_id));
-                                               let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
-                                                               //TODO: Do whatever we're gonna do for handling dropped messages
-                                                       });
-                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 39)));
-                                               self.do_attempt_write_data(&mut descriptor, peer);
-                                       },
-                                       MessageSendEvent::SendShutdown { ref node_id, ref msg } => {
-                                               log_trace!(self, "Handling Shutdown event in peer_handler for node {} for channel {}",
-                                                               log_pubkey!(node_id),
-                                                               log_bytes!(msg.channel_id));
-                                               let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
-                                                               //TODO: Do whatever we're gonna do for handling dropped messages
-                                                       });
-                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 38)));
-                                               self.do_attempt_write_data(&mut descriptor, peer);
-                                       },
-                                       MessageSendEvent::SendChannelReestablish { ref node_id, ref msg } => {
-                                               log_trace!(self, "Handling SendChannelReestablish event in peer_handler for node {} for channel {}",
-                                                               log_pubkey!(node_id),
-                                                               log_bytes!(msg.channel_id));
-                                               let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
-                                                               //TODO: Do whatever we're gonna do for handling dropped messages
-                                                       });
-                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 136)));
-                                               self.do_attempt_write_data(&mut descriptor, peer);
-                                       },
-                                       MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
-                                               log_trace!(self, "Handling BroadcastChannelAnnouncement event in peer_handler for short channel id {}", msg.contents.short_channel_id);
-                                               if self.message_handler.route_handler.handle_channel_announcement(msg).is_ok() && self.message_handler.route_handler.handle_channel_update(update_msg).is_ok() {
-                                                       let encoded_msg = encode_msg!(msg, 256);
-                                                       let encoded_update_msg = encode_msg!(update_msg, 258);
-
-                                                       for (ref descriptor, ref mut peer) in peers.peers.iter_mut() {
-                                                               if !peer.channel_encryptor.is_ready_for_encryption() || peer.their_global_features.is_none() ||
-                                                                               !peer.should_forward_channel(msg.contents.short_channel_id) {
-                                                                       continue
-                                                               }
-                                                               match peer.their_node_id {
-                                                                       None => continue,
-                                                                       Some(their_node_id) => {
-                                                                               if their_node_id == msg.contents.node_id_1 || their_node_id == msg.contents.node_id_2 {
-                                                                                       continue
-                                                                               }
-                                                                       }
-                                                               }
-                                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encoded_msg[..]));
-                                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encoded_update_msg[..]));
-                                                               self.do_attempt_write_data(&mut (*descriptor).clone(), peer);
-                                                       }
-                                               }
-                                       },
-                                       MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
-                                               log_trace!(self, "Handling BroadcastChannelUpdate event in peer_handler for short channel id {}", msg.contents.short_channel_id);
-                                               if self.message_handler.route_handler.handle_channel_update(msg).is_ok() {
-                                                       let encoded_msg = encode_msg!(msg, 258);
-
-                                                       for (ref descriptor, ref mut peer) in peers.peers.iter_mut() {
-                                                               if !peer.channel_encryptor.is_ready_for_encryption() || peer.their_global_features.is_none() ||
-                                                                               !peer.should_forward_channel(msg.contents.short_channel_id)  {
-                                                                       continue
-                                                               }
-                                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encoded_msg[..]));
-                                                               self.do_attempt_write_data(&mut (*descriptor).clone(), peer);
-                                                       }
-                                               }
-                                       },
-                                       MessageSendEvent::PaymentFailureNetworkUpdate { ref update } => {
-                                               self.message_handler.route_handler.handle_htlc_fail_channel_update(update);
-                                       },
-                                       MessageSendEvent::HandleError { ref node_id, ref action } => {
-                                               if let Some(ref action) = *action {
-                                                       match *action {
-                                                               msgs::ErrorAction::DisconnectPeer { ref msg } => {
-                                                                       if let Some(mut descriptor) = peers.node_id_to_descriptor.remove(node_id) {
-                                                                               peers.peers_needing_send.remove(&descriptor);
-                                                                               if let Some(mut peer) = peers.peers.remove(&descriptor) {
-                                                                                       if let Some(ref msg) = *msg {
-                                                                                               log_trace!(self, "Handling DisconnectPeer HandleError event in peer_handler for node {} with message {}",
-                                                                                                               log_pubkey!(node_id),
-                                                                                                               msg.data);
-                                                                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 17)));
-                                                                                               // This isn't guaranteed to work, but if there is enough free
-                                                                                               // room in the send buffer, put the error message there...
-                                                                                               self.do_attempt_write_data(&mut descriptor, &mut peer);
-                                                                                       } else {
-                                                                                               log_trace!(self, "Handling DisconnectPeer HandleError event in peer_handler for node {} with no message", log_pubkey!(node_id));
-                                                                                       }
-                                                                               }
-                                                                               descriptor.disconnect_socket();
-                                                                               self.message_handler.chan_handler.peer_disconnected(&node_id, false);
-                                                                       }
-                                                               },
-                                                               msgs::ErrorAction::IgnoreError => {},
-                                                               msgs::ErrorAction::SendErrorMessage { ref msg } => {
-                                                                       log_trace!(self, "Handling SendErrorMessage HandleError event in peer_handler for node {} with message {}",
-                                                                                       log_pubkey!(node_id),
-                                                                                       msg.data);
-                                                                       let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
-                                                                               //TODO: Do whatever we're gonna do for handling dropped messages
-                                                                       });
-                                                                       peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 17)));
-                                                                       self.do_attempt_write_data(&mut descriptor, peer);
-                                                               },
-                                                       }
-                                               } else {
-                                                       log_error!(self, "Got no-action HandleError Event in peer_handler for node {}, no such events should ever be generated!", log_pubkey!(node_id));
-                                               }
-                                       }
-                               }
-                       }
-
-                       for mut descriptor in peers.peers_needing_send.drain() {
-                               match peers.peers.get_mut(&descriptor) {
-                                       Some(peer) => self.do_attempt_write_data(&mut descriptor, peer),
-                                       None => panic!("Inconsistent peers set state!"),
-                               }
-                       }
-               }
-       }
-
-       /// Indicates that the given socket descriptor's connection is now closed.
-       ///
-       /// This must be called even if a PeerHandleError was given for a read_event or write_event,
-       /// but must NOT be called if a PeerHandleError was provided out of a new_\*\_connection event!
-       ///
-       /// Panics if the descriptor was not previously registered in a successful new_*_connection event.
-       pub fn disconnect_event(&self, descriptor: &Descriptor) {
-               self.disconnect_event_internal(descriptor, false);
-       }
-
-       fn disconnect_event_internal(&self, descriptor: &Descriptor, no_connection_possible: bool) {
-               let mut peers = self.peers.lock().unwrap();
-               peers.peers_needing_send.remove(descriptor);
-               let peer_option = peers.peers.remove(descriptor);
-               match peer_option {
-                       None => panic!("Descriptor for disconnect_event is not already known to PeerManager"),
-                       Some(peer) => {
-                               match peer.their_node_id {
-                                       Some(node_id) => {
-                                               peers.node_id_to_descriptor.remove(&node_id);
-                                               self.message_handler.chan_handler.peer_disconnected(&node_id, no_connection_possible);
-                                       },
-                                       None => {}
-                               }
-                       }
-               };
-       }
-}
-
-#[cfg(test)]
-mod tests {
-       use ln::peer_handler::{PeerManager, MessageHandler, SocketDescriptor};
-       use ln::msgs;
-       use util::events;
-       use util::test_utils;
-       use util::logger::Logger;
-
-       use secp256k1::Secp256k1;
-       use secp256k1::key::{SecretKey, PublicKey};
-
-       use rand::{thread_rng, Rng};
-
-       use std::sync::{Arc};
-
-       #[derive(PartialEq, Eq, Clone, Hash)]
-       struct FileDescriptor {
-               fd: u16,
-       }
-
-       impl SocketDescriptor for FileDescriptor {
-               fn send_data(&mut self, data: &[u8], _resume_read: bool) -> usize {
-                       data.len()
-               }
-
-               fn disconnect_socket(&mut self) {}
-       }
-
-       fn create_network(peer_count: usize) -> Vec<PeerManager<FileDescriptor>> {
-               let mut peers = Vec::new();
-               let mut rng = thread_rng();
-               let logger : Arc<Logger> = Arc::new(test_utils::TestLogger::new());
-               let mut ephemeral_bytes = [0; 32];
-               rng.fill_bytes(&mut ephemeral_bytes);
-
-               for _ in 0..peer_count {
-                       let chan_handler = test_utils::TestChannelMessageHandler::new();
-                       let router = test_utils::TestRoutingMessageHandler::new();
-                       let node_id = {
-                               let mut key_slice = [0;32];
-                               rng.fill_bytes(&mut key_slice);
-                               SecretKey::from_slice(&key_slice).unwrap()
-                       };
-                       let msg_handler = MessageHandler { chan_handler: Arc::new(chan_handler), route_handler: Arc::new(router) };
-                       let peer = PeerManager::new(msg_handler, node_id, &ephemeral_bytes, Arc::clone(&logger));
-                       peers.push(peer);
-               }
-
-               peers
-       }
-
-       fn establish_connection(peer_a: &PeerManager<FileDescriptor>, peer_b: &PeerManager<FileDescriptor>) {
-               let secp_ctx = Secp256k1::new();
-               let their_id = PublicKey::from_secret_key(&secp_ctx, &peer_b.our_node_secret);
-               let fd = FileDescriptor { fd: 1};
-               peer_a.new_inbound_connection(fd.clone()).unwrap();
-               peer_a.peers.lock().unwrap().node_id_to_descriptor.insert(their_id, fd.clone());
-       }
-
-       #[test]
-       fn test_disconnect_peer() {
-               // Simple test which builds a network of PeerManager, connects and brings them to NoiseState::Finished and
-               // push a DisconnectPeer event to remove the node flagged by id
-               let mut peers = create_network(2);
-               establish_connection(&peers[0], &peers[1]);
-               assert_eq!(peers[0].peers.lock().unwrap().peers.len(), 1);
-
-               let secp_ctx = Secp256k1::new();
-               let their_id = PublicKey::from_secret_key(&secp_ctx, &peers[1].our_node_secret);
-
-               let chan_handler = test_utils::TestChannelMessageHandler::new();
-               chan_handler.pending_events.lock().unwrap().push(events::MessageSendEvent::HandleError {
-                       node_id: their_id,
-                       action: Some(msgs::ErrorAction::DisconnectPeer { msg: None }),
-               });
-               assert_eq!(chan_handler.pending_events.lock().unwrap().len(), 1);
-               peers[0].message_handler.chan_handler = Arc::new(chan_handler);
-
-               peers[0].process_events();
-               assert_eq!(peers[0].peers.lock().unwrap().peers.len(), 0);
-       }
-}
diff --git a/src/ln/router.rs b/src/ln/router.rs
deleted file mode 100644 (file)
index bb20c31..0000000
+++ /dev/null
@@ -1,1633 +0,0 @@
-//! The top-level routing/network map tracking logic lives here.
-//!
-//! You probably want to create a Router and use that as your RoutingMessageHandler and then
-//! interrogate it to get routes for your own payments.
-
-use secp256k1::key::PublicKey;
-use secp256k1::Secp256k1;
-use secp256k1;
-
-use bitcoin_hashes::sha256d::Hash as Sha256dHash;
-use bitcoin_hashes::Hash;
-use bitcoin::blockdata::script::Builder;
-use bitcoin::blockdata::opcodes;
-
-use chain::chaininterface::{ChainError, ChainWatchInterface};
-use ln::channelmanager;
-use ln::msgs::{DecodeError,ErrorAction,HandleError,RoutingMessageHandler,NetAddress,GlobalFeatures};
-use ln::msgs;
-use util::ser::{Writeable, Readable, Writer, ReadableArgs};
-use util::logger::Logger;
-
-use std::cmp;
-use std::sync::{RwLock,Arc};
-use std::collections::{HashMap,BinaryHeap,BTreeMap};
-use std::collections::btree_map::Entry as BtreeEntry;
-use std;
-
-/// A hop in a route
-#[derive(Clone, PartialEq)]
-pub struct RouteHop {
-       /// The node_id of the node at this hop.
-       pub pubkey: PublicKey,
-       /// The channel that should be used from the previous hop to reach this node.
-       pub short_channel_id: u64,
-       /// The fee taken on this hop. For the last hop, this should be the full value of the payment.
-       pub fee_msat: u64,
-       /// The CLTV delta added for this hop. For the last hop, this should be the full CLTV value
-       /// expected at the destination, in excess of the current block height.
-       pub cltv_expiry_delta: u32,
-}
-
-/// A route from us through the network to a destination
-#[derive(Clone, PartialEq)]
-pub struct Route {
-       /// The list of hops, NOT INCLUDING our own, where the last hop is the destination. Thus, this
-       /// must always be at least length one. By protocol rules, this may not currently exceed 20 in
-       /// length.
-       pub hops: Vec<RouteHop>,
-}
-
-impl Writeable for Route {
-       fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
-               (self.hops.len() as u8).write(writer)?;
-               for hop in self.hops.iter() {
-                       hop.pubkey.write(writer)?;
-                       hop.short_channel_id.write(writer)?;
-                       hop.fee_msat.write(writer)?;
-                       hop.cltv_expiry_delta.write(writer)?;
-               }
-               Ok(())
-       }
-}
-
-impl<R: ::std::io::Read> Readable<R> for Route {
-       fn read(reader: &mut R) -> Result<Route, DecodeError> {
-               let hops_count: u8 = Readable::read(reader)?;
-               let mut hops = Vec::with_capacity(hops_count as usize);
-               for _ in 0..hops_count {
-                       hops.push(RouteHop {
-                               pubkey: Readable::read(reader)?,
-                               short_channel_id: Readable::read(reader)?,
-                               fee_msat: Readable::read(reader)?,
-                               cltv_expiry_delta: Readable::read(reader)?,
-                       });
-               }
-               Ok(Route {
-                       hops
-               })
-       }
-}
-
-#[derive(PartialEq)]
-struct DirectionalChannelInfo {
-       src_node_id: PublicKey,
-       last_update: u32,
-       enabled: bool,
-       cltv_expiry_delta: u16,
-       htlc_minimum_msat: u64,
-       fee_base_msat: u32,
-       fee_proportional_millionths: u32,
-       last_update_message: Option<msgs::ChannelUpdate>,
-}
-
-impl std::fmt::Display for DirectionalChannelInfo {
-       fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
-               write!(f, "src_node_id {}, last_update {}, enabled {}, cltv_expiry_delta {}, htlc_minimum_msat {}, fee_base_msat {}, fee_proportional_millionths {}", log_pubkey!(self.src_node_id), self.last_update, self.enabled, self.cltv_expiry_delta, self.htlc_minimum_msat, self.fee_base_msat, self.fee_proportional_millionths)?;
-               Ok(())
-       }
-}
-
-impl_writeable!(DirectionalChannelInfo, 0, {
-       src_node_id,
-       last_update,
-       enabled,
-       cltv_expiry_delta,
-       htlc_minimum_msat,
-       fee_base_msat,
-       fee_proportional_millionths,
-       last_update_message
-});
-
-#[derive(PartialEq)]
-struct ChannelInfo {
-       features: GlobalFeatures,
-       one_to_two: DirectionalChannelInfo,
-       two_to_one: DirectionalChannelInfo,
-       //this is cached here so we can send out it later if required by route_init_sync
-       //keep an eye on this to see if the extra memory is a problem
-       announcement_message: Option<msgs::ChannelAnnouncement>,
-}
-
-impl std::fmt::Display for ChannelInfo {
-       fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
-               write!(f, "features: {}, one_to_two: {}, two_to_one: {}", log_bytes!(self.features.encode()), self.one_to_two, self.two_to_one)?;
-               Ok(())
-       }
-}
-
-impl_writeable!(ChannelInfo, 0, {
-       features,
-       one_to_two,
-       two_to_one,
-       announcement_message
-});
-
-#[derive(PartialEq)]
-struct NodeInfo {
-       #[cfg(feature = "non_bitcoin_chain_hash_routing")]
-       channels: Vec<(u64, Sha256dHash)>,
-       #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
-       channels: Vec<u64>,
-
-       lowest_inbound_channel_fee_base_msat: u32,
-       lowest_inbound_channel_fee_proportional_millionths: u32,
-
-       features: GlobalFeatures,
-       last_update: u32,
-       rgb: [u8; 3],
-       alias: [u8; 32],
-       addresses: Vec<NetAddress>,
-       //this is cached here so we can send out it later if required by route_init_sync
-       //keep an eye on this to see if the extra memory is a problem
-       announcement_message: Option<msgs::NodeAnnouncement>,
-}
-
-impl std::fmt::Display for NodeInfo {
-       fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
-               write!(f, "features: {}, last_update: {}, lowest_inbound_channel_fee_base_msat: {}, lowest_inbound_channel_fee_proportional_millionths: {}, channels: {:?}", log_bytes!(self.features.encode()), self.last_update, self.lowest_inbound_channel_fee_base_msat, self.lowest_inbound_channel_fee_proportional_millionths, &self.channels[..])?;
-               Ok(())
-       }
-}
-
-impl Writeable for NodeInfo {
-       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
-               (self.channels.len() as u64).write(writer)?;
-               for ref chan in self.channels.iter() {
-                       chan.write(writer)?;
-               }
-               self.lowest_inbound_channel_fee_base_msat.write(writer)?;
-               self.lowest_inbound_channel_fee_proportional_millionths.write(writer)?;
-               self.features.write(writer)?;
-               self.last_update.write(writer)?;
-               self.rgb.write(writer)?;
-               self.alias.write(writer)?;
-               (self.addresses.len() as u64).write(writer)?;
-               for ref addr in &self.addresses {
-                       addr.write(writer)?;
-               }
-               self.announcement_message.write(writer)?;
-               Ok(())
-       }
-}
-
-const MAX_ALLOC_SIZE: u64 = 64*1024;
-
-impl<R: ::std::io::Read> Readable<R> for NodeInfo {
-       fn read(reader: &mut R) -> Result<NodeInfo, DecodeError> {
-               let channels_count: u64 = Readable::read(reader)?;
-               let mut channels = Vec::with_capacity(cmp::min(channels_count, MAX_ALLOC_SIZE / 8) as usize);
-               for _ in 0..channels_count {
-                       channels.push(Readable::read(reader)?);
-               }
-               let lowest_inbound_channel_fee_base_msat = Readable::read(reader)?;
-               let lowest_inbound_channel_fee_proportional_millionths = Readable::read(reader)?;
-               let features = Readable::read(reader)?;
-               let last_update = Readable::read(reader)?;
-               let rgb = Readable::read(reader)?;
-               let alias = Readable::read(reader)?;
-               let addresses_count: u64 = Readable::read(reader)?;
-               let mut addresses = Vec::with_capacity(cmp::min(addresses_count, MAX_ALLOC_SIZE / 40) as usize);
-               for _ in 0..addresses_count {
-                       match Readable::read(reader) {
-                               Ok(Ok(addr)) => { addresses.push(addr); },
-                               Ok(Err(_)) => return Err(DecodeError::InvalidValue),
-                               Err(DecodeError::ShortRead) => return Err(DecodeError::BadLengthDescriptor),
-                               _ => unreachable!(),
-                       }
-               }
-               let announcement_message = Readable::read(reader)?;
-               Ok(NodeInfo {
-                       channels,
-                       lowest_inbound_channel_fee_base_msat,
-                       lowest_inbound_channel_fee_proportional_millionths,
-                       features,
-                       last_update,
-                       rgb,
-                       alias,
-                       addresses,
-                       announcement_message
-               })
-       }
-}
-
-#[derive(PartialEq)]
-struct NetworkMap {
-       #[cfg(feature = "non_bitcoin_chain_hash_routing")]
-       channels: BTreeMap<(u64, Sha256dHash), ChannelInfo>,
-       #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
-       channels: BTreeMap<u64, ChannelInfo>,
-
-       our_node_id: PublicKey,
-       nodes: BTreeMap<PublicKey, NodeInfo>,
-}
-
-impl Writeable for NetworkMap {
-       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
-               (self.channels.len() as u64).write(writer)?;
-               for (ref chan_id, ref chan_info) in self.channels.iter() {
-                       (*chan_id).write(writer)?;
-                       chan_info.write(writer)?;
-               }
-               self.our_node_id.write(writer)?;
-               (self.nodes.len() as u64).write(writer)?;
-               for (ref node_id, ref node_info) in self.nodes.iter() {
-                       node_id.write(writer)?;
-                       node_info.write(writer)?;
-               }
-               Ok(())
-       }
-}
-
-impl<R: ::std::io::Read> Readable<R> for NetworkMap {
-       fn read(reader: &mut R) -> Result<NetworkMap, DecodeError> {
-               let channels_count: u64 = Readable::read(reader)?;
-               let mut channels = BTreeMap::new();
-               for _ in 0..channels_count {
-                       let chan_id: u64 = Readable::read(reader)?;
-                       let chan_info = Readable::read(reader)?;
-                       channels.insert(chan_id, chan_info);
-               }
-               let our_node_id = Readable::read(reader)?;
-               let nodes_count: u64 = Readable::read(reader)?;
-               let mut nodes = BTreeMap::new();
-               for _ in 0..nodes_count {
-                       let node_id = Readable::read(reader)?;
-                       let node_info = Readable::read(reader)?;
-                       nodes.insert(node_id, node_info);
-               }
-               Ok(NetworkMap {
-                       channels,
-                       our_node_id,
-                       nodes,
-               })
-       }
-}
-
-struct MutNetworkMap<'a> {
-       #[cfg(feature = "non_bitcoin_chain_hash_routing")]
-       channels: &'a mut BTreeMap<(u64, Sha256dHash), ChannelInfo>,
-       #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
-       channels: &'a mut BTreeMap<u64, ChannelInfo>,
-       nodes: &'a mut BTreeMap<PublicKey, NodeInfo>,
-}
-impl NetworkMap {
-       fn borrow_parts(&mut self) -> MutNetworkMap {
-               MutNetworkMap {
-                       channels: &mut self.channels,
-                       nodes: &mut self.nodes,
-               }
-       }
-}
-impl std::fmt::Display for NetworkMap {
-       fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
-               write!(f, "Node id {} network map\n[Channels]\n", log_pubkey!(self.our_node_id))?;
-               for (key, val) in self.channels.iter() {
-                       write!(f, " {}: {}\n", key, val)?;
-               }
-               write!(f, "[Nodes]\n")?;
-               for (key, val) in self.nodes.iter() {
-                       write!(f, " {}: {}\n", log_pubkey!(key), val)?;
-               }
-               Ok(())
-       }
-}
-
-impl NetworkMap {
-       #[cfg(feature = "non_bitcoin_chain_hash_routing")]
-       #[inline]
-       fn get_key(short_channel_id: u64, chain_hash: Sha256dHash) -> (u64, Sha256dHash) {
-               (short_channel_id, chain_hash)
-       }
-
-       #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
-       #[inline]
-       fn get_key(short_channel_id: u64, _: Sha256dHash) -> u64 {
-               short_channel_id
-       }
-
-       #[cfg(feature = "non_bitcoin_chain_hash_routing")]
-       #[inline]
-       fn get_short_id(id: &(u64, Sha256dHash)) -> &u64 {
-               &id.0
-       }
-
-       #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
-       #[inline]
-       fn get_short_id(id: &u64) -> &u64 {
-               id
-       }
-}
-
-/// A channel descriptor which provides a last-hop route to get_route
-pub struct RouteHint {
-       /// The node_id of the non-target end of the route
-       pub src_node_id: PublicKey,
-       /// The short_channel_id of this channel
-       pub short_channel_id: u64,
-       /// The static msat-denominated fee which must be paid to use this channel
-       pub fee_base_msat: u32,
-       /// The dynamic proportional fee which must be paid to use this channel, denominated in
-       /// millionths of the value being forwarded to the next hop.
-       pub fee_proportional_millionths: u32,
-       /// The difference in CLTV values between this node and the next node.
-       pub cltv_expiry_delta: u16,
-       /// The minimum value, in msat, which must be relayed to the next hop.
-       pub htlc_minimum_msat: u64,
-}
-
-/// Tracks a view of the network, receiving updates from peers and generating Routes to
-/// payment destinations.
-pub struct Router {
-       secp_ctx: Secp256k1<secp256k1::VerifyOnly>,
-       network_map: RwLock<NetworkMap>,
-       chain_monitor: Arc<ChainWatchInterface>,
-       logger: Arc<Logger>,
-}
-
-const SERIALIZATION_VERSION: u8 = 1;
-const MIN_SERIALIZATION_VERSION: u8 = 1;
-
-impl Writeable for Router {
-       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
-               writer.write_all(&[SERIALIZATION_VERSION; 1])?;
-               writer.write_all(&[MIN_SERIALIZATION_VERSION; 1])?;
-
-               let network = self.network_map.read().unwrap();
-               network.write(writer)?;
-               Ok(())
-       }
-}
-
-/// Arguments for the creation of a Router that are not deserialized.
-/// At a high-level, the process for deserializing a Router and resuming normal operation is:
-/// 1) Deserialize the Router by filling in this struct and calling <Router>::read(reaser, args).
-/// 2) Register the new Router with your ChainWatchInterface
-pub struct RouterReadArgs {
-       /// The ChainWatchInterface for use in the Router in the future.
-       ///
-       /// No calls to the ChainWatchInterface will be made during deserialization.
-       pub chain_monitor: Arc<ChainWatchInterface>,
-       /// The Logger for use in the ChannelManager and which may be used to log information during
-       /// deserialization.
-       pub logger: Arc<Logger>,
-}
-
-impl<R: ::std::io::Read> ReadableArgs<R, RouterReadArgs> for Router {
-       fn read(reader: &mut R, args: RouterReadArgs) -> Result<Router, DecodeError> {
-               let _ver: u8 = Readable::read(reader)?;
-               let min_ver: u8 = Readable::read(reader)?;
-               if min_ver > SERIALIZATION_VERSION {
-                       return Err(DecodeError::UnknownVersion);
-               }
-               let network_map = Readable::read(reader)?;
-               Ok(Router {
-                       secp_ctx: Secp256k1::verification_only(),
-                       network_map: RwLock::new(network_map),
-                       chain_monitor: args.chain_monitor,
-                       logger: args.logger,
-               })
-       }
-}
-
-macro_rules! secp_verify_sig {
-       ( $secp_ctx: expr, $msg: expr, $sig: expr, $pubkey: expr ) => {
-               match $secp_ctx.verify($msg, $sig, $pubkey) {
-                       Ok(_) => {},
-                       Err(_) => return Err(HandleError{err: "Invalid signature from remote node", action: None}),
-               }
-       };
-}
-
-impl RoutingMessageHandler for Router {
-       fn handle_node_announcement(&self, msg: &msgs::NodeAnnouncement) -> Result<bool, HandleError> {
-               let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
-               secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &msg.contents.node_id);
-
-               if msg.contents.features.requires_unknown_bits() {
-                       panic!("Unknown-required-features NodeAnnouncements should never deserialize!");
-               }
-
-               let mut network = self.network_map.write().unwrap();
-               match network.nodes.get_mut(&msg.contents.node_id) {
-                       None => Err(HandleError{err: "No existing channels for node_announcement", action: Some(ErrorAction::IgnoreError)}),
-                       Some(node) => {
-                               if node.last_update >= msg.contents.timestamp {
-                                       return Err(HandleError{err: "Update older than last processed update", action: Some(ErrorAction::IgnoreError)});
-                               }
-
-                               node.features = msg.contents.features.clone();
-                               node.last_update = msg.contents.timestamp;
-                               node.rgb = msg.contents.rgb;
-                               node.alias = msg.contents.alias;
-                               node.addresses = msg.contents.addresses.clone();
-
-                               let should_relay = msg.contents.excess_data.is_empty() && msg.contents.excess_address_data.is_empty() && !msg.contents.features.supports_unknown_bits();
-                               node.announcement_message = if should_relay { Some(msg.clone()) } else { None };
-                               Ok(should_relay)
-                       }
-               }
-       }
-
-       fn handle_channel_announcement(&self, msg: &msgs::ChannelAnnouncement) -> Result<bool, HandleError> {
-               if msg.contents.node_id_1 == msg.contents.node_id_2 || msg.contents.bitcoin_key_1 == msg.contents.bitcoin_key_2 {
-                       return Err(HandleError{err: "Channel announcement node had a channel with itself", action: Some(ErrorAction::IgnoreError)});
-               }
-
-               let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
-               secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_1, &msg.contents.node_id_1);
-               secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_2, &msg.contents.node_id_2);
-               secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_1, &msg.contents.bitcoin_key_1);
-               secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_2, &msg.contents.bitcoin_key_2);
-
-               if msg.contents.features.requires_unknown_bits() {
-                       panic!("Unknown-required-features ChannelAnnouncements should never deserialize!");
-               }
-
-               let checked_utxo = match self.chain_monitor.get_chain_utxo(msg.contents.chain_hash, msg.contents.short_channel_id) {
-                       Ok((script_pubkey, _value)) => {
-                               let expected_script = Builder::new().push_opcode(opcodes::all::OP_PUSHNUM_2)
-                                                                   .push_slice(&msg.contents.bitcoin_key_1.serialize())
-                                                                   .push_slice(&msg.contents.bitcoin_key_2.serialize())
-                                                                   .push_opcode(opcodes::all::OP_PUSHNUM_2)
-                                                                   .push_opcode(opcodes::all::OP_CHECKMULTISIG).into_script().to_v0_p2wsh();
-                               if script_pubkey != expected_script {
-                                       return Err(HandleError{err: "Channel announcement keys didn't match on-chain script", action: Some(ErrorAction::IgnoreError)});
-                               }
-                               //TODO: Check if value is worth storing, use it to inform routing, and compare it
-                               //to the new HTLC max field in channel_update
-                               true
-                       },
-                       Err(ChainError::NotSupported) => {
-                               // Tentatively accept, potentially exposing us to DoS attacks
-                               false
-                       },
-                       Err(ChainError::NotWatched) => {
-                               return Err(HandleError{err: "Channel announced on an unknown chain", action: Some(ErrorAction::IgnoreError)});
-                       },
-                       Err(ChainError::UnknownTx) => {
-                               return Err(HandleError{err: "Channel announced without corresponding UTXO entry", action: Some(ErrorAction::IgnoreError)});
-                       },
-               };
-
-               let mut network_lock = self.network_map.write().unwrap();
-               let network = network_lock.borrow_parts();
-
-               let should_relay = msg.contents.excess_data.is_empty() && !msg.contents.features.supports_unknown_bits();
-
-               let chan_info = ChannelInfo {
-                               features: msg.contents.features.clone(),
-                               one_to_two: DirectionalChannelInfo {
-                                       src_node_id: msg.contents.node_id_1.clone(),
-                                       last_update: 0,
-                                       enabled: false,
-                                       cltv_expiry_delta: u16::max_value(),
-                                       htlc_minimum_msat: u64::max_value(),
-                                       fee_base_msat: u32::max_value(),
-                                       fee_proportional_millionths: u32::max_value(),
-                                       last_update_message: None,
-                               },
-                               two_to_one: DirectionalChannelInfo {
-                                       src_node_id: msg.contents.node_id_2.clone(),
-                                       last_update: 0,
-                                       enabled: false,
-                                       cltv_expiry_delta: u16::max_value(),
-                                       htlc_minimum_msat: u64::max_value(),
-                                       fee_base_msat: u32::max_value(),
-                                       fee_proportional_millionths: u32::max_value(),
-                                       last_update_message: None,
-                               },
-                               announcement_message: if should_relay { Some(msg.clone()) } else { None },
-                       };
-
-               match network.channels.entry(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)) {
-                       BtreeEntry::Occupied(mut entry) => {
-                               //TODO: because asking the blockchain if short_channel_id is valid is only optional
-                               //in the blockchain API, we need to handle it smartly here, though it's unclear
-                               //exactly how...
-                               if checked_utxo {
-                                       // Either our UTXO provider is busted, there was a reorg, or the UTXO provider
-                                       // only sometimes returns results. In any case remove the previous entry. Note
-                                       // that the spec expects us to "blacklist" the node_ids involved, but we can't
-                                       // do that because
-                                       // a) we don't *require* a UTXO provider that always returns results.
-                                       // b) we don't track UTXOs of channels we know about and remove them if they
-                                       //    get reorg'd out.
-                                       // c) it's unclear how to do so without exposing ourselves to massive DoS risk.
-                                       Self::remove_channel_in_nodes(network.nodes, &entry.get(), msg.contents.short_channel_id);
-                                       *entry.get_mut() = chan_info;
-                               } else {
-                                       return Err(HandleError{err: "Already have knowledge of channel", action: Some(ErrorAction::IgnoreError)})
-                               }
-                       },
-                       BtreeEntry::Vacant(entry) => {
-                               entry.insert(chan_info);
-                       }
-               };
-
-               macro_rules! add_channel_to_node {
-                       ( $node_id: expr ) => {
-                               match network.nodes.entry($node_id) {
-                                       BtreeEntry::Occupied(node_entry) => {
-                                               node_entry.into_mut().channels.push(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash));
-                                       },
-                                       BtreeEntry::Vacant(node_entry) => {
-                                               node_entry.insert(NodeInfo {
-                                                       channels: vec!(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)),
-                                                       lowest_inbound_channel_fee_base_msat: u32::max_value(),
-                                                       lowest_inbound_channel_fee_proportional_millionths: u32::max_value(),
-                                                       features: GlobalFeatures::new(),
-                                                       last_update: 0,
-                                                       rgb: [0; 3],
-                                                       alias: [0; 32],
-                                                       addresses: Vec::new(),
-                                                       announcement_message: None,
-                                               });
-                                       }
-                               }
-                       };
-               }
-
-               add_channel_to_node!(msg.contents.node_id_1);
-               add_channel_to_node!(msg.contents.node_id_2);
-
-               Ok(should_relay)
-       }
-
-       fn handle_htlc_fail_channel_update(&self, update: &msgs::HTLCFailChannelUpdate) {
-               match update {
-                       &msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg } => {
-                               let _ = self.handle_channel_update(msg);
-                       },
-                       &msgs::HTLCFailChannelUpdate::ChannelClosed { ref short_channel_id, ref is_permanent } => {
-                               let mut network = self.network_map.write().unwrap();
-                               if *is_permanent {
-                                       if let Some(chan) = network.channels.remove(short_channel_id) {
-                                               Self::remove_channel_in_nodes(&mut network.nodes, &chan, *short_channel_id);
-                                       }
-                               } else {
-                                       if let Some(chan) = network.channels.get_mut(short_channel_id) {
-                                               chan.one_to_two.enabled = false;
-                                               chan.two_to_one.enabled = false;
-                                       }
-                               }
-                       },
-                       &msgs::HTLCFailChannelUpdate::NodeFailure { ref node_id, ref is_permanent } => {
-                               if *is_permanent {
-                                       //TODO: Wholly remove the node
-                               } else {
-                                       self.mark_node_bad(node_id, false);
-                               }
-                       },
-               }
-       }
-
-       fn handle_channel_update(&self, msg: &msgs::ChannelUpdate) -> Result<bool, HandleError> {
-               let mut network = self.network_map.write().unwrap();
-               let dest_node_id;
-               let chan_enabled = msg.contents.flags & (1 << 1) != (1 << 1);
-               let chan_was_enabled;
-
-               match network.channels.get_mut(&NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)) {
-                       None => return Err(HandleError{err: "Couldn't find channel for update", action: Some(ErrorAction::IgnoreError)}),
-                       Some(channel) => {
-                               macro_rules! maybe_update_channel_info {
-                                       ( $target: expr) => {
-                                               if $target.last_update >= msg.contents.timestamp {
-                                                       return Err(HandleError{err: "Update older than last processed update", action: Some(ErrorAction::IgnoreError)});
-                                               }
-                                               chan_was_enabled = $target.enabled;
-                                               $target.last_update = msg.contents.timestamp;
-                                               $target.enabled = chan_enabled;
-                                               $target.cltv_expiry_delta = msg.contents.cltv_expiry_delta;
-                                               $target.htlc_minimum_msat = msg.contents.htlc_minimum_msat;
-                                               $target.fee_base_msat = msg.contents.fee_base_msat;
-                                               $target.fee_proportional_millionths = msg.contents.fee_proportional_millionths;
-                                               $target.last_update_message = if msg.contents.excess_data.is_empty() {
-                                                       Some(msg.clone())
-                                               } else {
-                                                       None
-                                               };
-                                       }
-                               }
-                               let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
-                               if msg.contents.flags & 1 == 1 {
-                                       dest_node_id = channel.one_to_two.src_node_id.clone();
-                                       secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &channel.two_to_one.src_node_id);
-                                       maybe_update_channel_info!(channel.two_to_one);
-                               } else {
-                                       dest_node_id = channel.two_to_one.src_node_id.clone();
-                                       secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &channel.one_to_two.src_node_id);
-                                       maybe_update_channel_info!(channel.one_to_two);
-                               }
-                       }
-               }
-
-               if chan_enabled {
-                       let node = network.nodes.get_mut(&dest_node_id).unwrap();
-                       node.lowest_inbound_channel_fee_base_msat = cmp::min(node.lowest_inbound_channel_fee_base_msat, msg.contents.fee_base_msat);
-                       node.lowest_inbound_channel_fee_proportional_millionths = cmp::min(node.lowest_inbound_channel_fee_proportional_millionths, msg.contents.fee_proportional_millionths);
-               } else if chan_was_enabled {
-                       let mut lowest_inbound_channel_fee_base_msat = u32::max_value();
-                       let mut lowest_inbound_channel_fee_proportional_millionths = u32::max_value();
-
-                       {
-                               let node = network.nodes.get(&dest_node_id).unwrap();
-
-                               for chan_id in node.channels.iter() {
-                                       let chan = network.channels.get(chan_id).unwrap();
-                                       if chan.one_to_two.src_node_id == dest_node_id {
-                                               lowest_inbound_channel_fee_base_msat = cmp::min(lowest_inbound_channel_fee_base_msat, chan.two_to_one.fee_base_msat);
-                                               lowest_inbound_channel_fee_proportional_millionths = cmp::min(lowest_inbound_channel_fee_proportional_millionths, chan.two_to_one.fee_proportional_millionths);
-                                       } else {
-                                               lowest_inbound_channel_fee_base_msat = cmp::min(lowest_inbound_channel_fee_base_msat, chan.one_to_two.fee_base_msat);
-                                               lowest_inbound_channel_fee_proportional_millionths = cmp::min(lowest_inbound_channel_fee_proportional_millionths, chan.one_to_two.fee_proportional_millionths);
-                                       }
-                               }
-                       }
-
-                       //TODO: satisfy the borrow-checker without a double-map-lookup :(
-                       let mut_node = network.nodes.get_mut(&dest_node_id).unwrap();
-                       mut_node.lowest_inbound_channel_fee_base_msat = lowest_inbound_channel_fee_base_msat;
-                       mut_node.lowest_inbound_channel_fee_proportional_millionths = lowest_inbound_channel_fee_proportional_millionths;
-               }
-
-               Ok(msg.contents.excess_data.is_empty())
-       }
-
-
-       fn get_next_channel_announcements(&self, starting_point: u64, batch_amount: u8) -> Vec<(msgs::ChannelAnnouncement, msgs::ChannelUpdate,msgs::ChannelUpdate)> {
-               let mut result = Vec::with_capacity(batch_amount as usize);
-               let network = self.network_map.read().unwrap();
-               let mut iter = network.channels.range(starting_point..);
-               while result.len() < batch_amount as usize {
-                       if let Some((_, ref chan)) = iter.next() {
-                               if chan.announcement_message.is_some() &&
-                                               chan.one_to_two.last_update_message.is_some() &&
-                                               chan.two_to_one.last_update_message.is_some() {
-                                       result.push((chan.announcement_message.clone().unwrap(),
-                                               chan.one_to_two.last_update_message.clone().unwrap(),
-                                               chan.two_to_one.last_update_message.clone().unwrap()));
-                               } else {
-                                       // TODO: We may end up sending un-announced channel_updates if we are sending
-                                       // initial sync data while receiving announce/updates for this channel.
-                               }
-                       } else {
-                               return result;
-                       }
-               }
-               result
-       }
-
-       fn get_next_node_announcements(&self, starting_point: Option<&PublicKey>, batch_amount: u8) -> Vec<msgs::NodeAnnouncement> {
-               let mut result = Vec::with_capacity(batch_amount as usize);
-               let network = self.network_map.read().unwrap();
-               let mut iter = if let Some(pubkey) = starting_point {
-                               let mut iter = network.nodes.range((*pubkey)..);
-                               iter.next();
-                               iter
-                       } else {
-                               network.nodes.range(..)
-                       };
-               while result.len() < batch_amount as usize {
-                       if let Some((_, ref node)) = iter.next() {
-                               if node.announcement_message.is_some() {
-                                       result.push(node.announcement_message.clone().unwrap());
-                               }
-                       } else {
-                               return result;
-                       }
-               }
-               result
-       }
-}
-
-#[derive(Eq, PartialEq)]
-struct RouteGraphNode {
-       pubkey: PublicKey,
-       lowest_fee_to_peer_through_node: u64,
-       lowest_fee_to_node: u64,
-}
-
-impl cmp::Ord for RouteGraphNode {
-       fn cmp(&self, other: &RouteGraphNode) -> cmp::Ordering {
-               other.lowest_fee_to_peer_through_node.cmp(&self.lowest_fee_to_peer_through_node)
-                       .then_with(|| other.pubkey.serialize().cmp(&self.pubkey.serialize()))
-       }
-}
-
-impl cmp::PartialOrd for RouteGraphNode {
-       fn partial_cmp(&self, other: &RouteGraphNode) -> Option<cmp::Ordering> {
-               Some(self.cmp(other))
-       }
-}
-
-struct DummyDirectionalChannelInfo {
-       src_node_id: PublicKey,
-       cltv_expiry_delta: u32,
-       htlc_minimum_msat: u64,
-       fee_base_msat: u32,
-       fee_proportional_millionths: u32,
-}
-
-impl Router {
-       /// Creates a new router with the given node_id to be used as the source for get_route()
-       pub fn new(our_pubkey: PublicKey, chain_monitor: Arc<ChainWatchInterface>, logger: Arc<Logger>) -> Router {
-               let mut nodes = BTreeMap::new();
-               nodes.insert(our_pubkey.clone(), NodeInfo {
-                       channels: Vec::new(),
-                       lowest_inbound_channel_fee_base_msat: u32::max_value(),
-                       lowest_inbound_channel_fee_proportional_millionths: u32::max_value(),
-                       features: GlobalFeatures::new(),
-                       last_update: 0,
-                       rgb: [0; 3],
-                       alias: [0; 32],
-                       addresses: Vec::new(),
-                       announcement_message: None,
-               });
-               Router {
-                       secp_ctx: Secp256k1::verification_only(),
-                       network_map: RwLock::new(NetworkMap {
-                               channels: BTreeMap::new(),
-                               our_node_id: our_pubkey,
-                               nodes: nodes,
-                       }),
-                       chain_monitor,
-                       logger,
-               }
-       }
-
-       /// Dumps the entire network view of this Router to the logger provided in the constructor at
-       /// level Trace
-       pub fn trace_state(&self) {
-               log_trace!(self, "{}", self.network_map.read().unwrap());
-       }
-
-       /// Get network addresses by node id
-       pub fn get_addresses(&self, pubkey: &PublicKey) -> Option<Vec<NetAddress>> {
-               let network = self.network_map.read().unwrap();
-               network.nodes.get(pubkey).map(|n| n.addresses.clone())
-       }
-
-       /// Marks a node as having failed a route. This will avoid re-using the node in routes for now,
-       /// with an exponential decay in node "badness". Note that there is deliberately no
-       /// mark_channel_bad as a node may simply lie and suggest that an upstream channel from it is
-       /// what failed the route and not the node itself. Instead, setting the blamed_upstream_node
-       /// boolean will reduce the penalty, returning the node to usability faster. If the node is
-       /// behaving correctly, it will disable the failing channel and we will use it again next time.
-       pub fn mark_node_bad(&self, _node_id: &PublicKey, _blamed_upstream_node: bool) {
-               unimplemented!();
-       }
-
-       fn remove_channel_in_nodes(nodes: &mut BTreeMap<PublicKey, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
-               macro_rules! remove_from_node {
-                       ($node_id: expr) => {
-                               if let BtreeEntry::Occupied(mut entry) = nodes.entry($node_id) {
-                                       entry.get_mut().channels.retain(|chan_id| {
-                                               short_channel_id != *NetworkMap::get_short_id(chan_id)
-                                       });
-                                       if entry.get().channels.is_empty() {
-                                               entry.remove_entry();
-                                       }
-                               } else {
-                                       panic!("Had channel that pointed to unknown node (ie inconsistent network map)!");
-                               }
-                       }
-               }
-               remove_from_node!(chan.one_to_two.src_node_id);
-               remove_from_node!(chan.two_to_one.src_node_id);
-       }
-
-       /// Gets a route from us to the given target node.
-       ///
-       /// Extra routing hops between known nodes and the target will be used if they are included in
-       /// last_hops.
-       ///
-       /// If some channels aren't announced, it may be useful to fill in a first_hops with the
-       /// results from a local ChannelManager::list_usable_channels() call. If it is filled in, our
-       /// (this Router's) view of our local channels will be ignored, and only those in first_hops
-       /// will be used.
-       ///
-       /// Panics if first_hops contains channels without short_channel_ids
-       /// (ChannelManager::list_usable_channels will never include such channels).
-       ///
-       /// The fees on channels from us to next-hops are ignored (as they are assumed to all be
-       /// equal), however the enabled/disabled bit on such channels as well as the htlc_minimum_msat
-       /// *is* checked as they may change based on the receiving node.
-       pub fn get_route(&self, target: &PublicKey, first_hops: Option<&[channelmanager::ChannelDetails]>, last_hops: &[RouteHint], final_value_msat: u64, final_cltv: u32) -> Result<Route, HandleError> {
-               // TODO: Obviously *only* using total fee cost sucks. We should consider weighting by
-               // uptime/success in using a node in the past.
-               let network = self.network_map.read().unwrap();
-
-               if *target == network.our_node_id {
-                       return Err(HandleError{err: "Cannot generate a route to ourselves", action: None});
-               }
-
-               if final_value_msat > 21_000_000 * 1_0000_0000 * 1000 {
-                       return Err(HandleError{err: "Cannot generate a route of more value than all existing satoshis", action: None});
-               }
-
-               // We do a dest-to-source Dijkstra's sorting by each node's distance from the destination
-               // plus the minimum per-HTLC fee to get from it to another node (aka "shitty A*").
-               // TODO: There are a few tweaks we could do, including possibly pre-calculating more stuff
-               // to use as the A* heuristic beyond just the cost to get one node further than the current
-               // one.
-
-               let dummy_directional_info = DummyDirectionalChannelInfo { // used for first_hops routes
-                       src_node_id: network.our_node_id.clone(),
-                       cltv_expiry_delta: 0,
-                       htlc_minimum_msat: 0,
-                       fee_base_msat: 0,
-                       fee_proportional_millionths: 0,
-               };
-
-               let mut targets = BinaryHeap::new(); //TODO: Do we care about switching to eg Fibbonaci heap?
-               let mut dist = HashMap::with_capacity(network.nodes.len());
-
-               let mut first_hop_targets = HashMap::with_capacity(if first_hops.is_some() { first_hops.as_ref().unwrap().len() } else { 0 });
-               if let Some(hops) = first_hops {
-                       for chan in hops {
-                               let short_channel_id = chan.short_channel_id.expect("first_hops should be filled in with usable channels, not pending ones");
-                               if chan.remote_network_id == *target {
-                                       return Ok(Route {
-                                               hops: vec![RouteHop {
-                                                       pubkey: chan.remote_network_id,
-                                                       short_channel_id,
-                                                       fee_msat: final_value_msat,
-                                                       cltv_expiry_delta: final_cltv,
-                                               }],
-                                       });
-                               }
-                               first_hop_targets.insert(chan.remote_network_id, short_channel_id);
-                       }
-                       if first_hop_targets.is_empty() {
-                               return Err(HandleError{err: "Cannot route when there are no outbound routes away from us", action: None});
-                       }
-               }
-
-               macro_rules! add_entry {
-                       // Adds entry which goes from the node pointed to by $directional_info to
-                       // $dest_node_id over the channel with id $chan_id with fees described in
-                       // $directional_info.
-                       ( $chan_id: expr, $dest_node_id: expr, $directional_info: expr, $starting_fee_msat: expr ) => {
-                               //TODO: Explore simply adding fee to hit htlc_minimum_msat
-                               if $starting_fee_msat as u64 + final_value_msat >= $directional_info.htlc_minimum_msat {
-                                       let proportional_fee_millions = ($starting_fee_msat + final_value_msat).checked_mul($directional_info.fee_proportional_millionths as u64);
-                                       if let Some(new_fee) = proportional_fee_millions.and_then(|part| {
-                                                       ($directional_info.fee_base_msat as u64).checked_add(part / 1000000) })
-                                       {
-                                               let mut total_fee = $starting_fee_msat as u64;
-                                               let hm_entry = dist.entry(&$directional_info.src_node_id);
-                                               let old_entry = hm_entry.or_insert_with(|| {
-                                                       let node = network.nodes.get(&$directional_info.src_node_id).unwrap();
-                                                       (u64::max_value(),
-                                                               node.lowest_inbound_channel_fee_base_msat,
-                                                               node.lowest_inbound_channel_fee_proportional_millionths,
-                                                               RouteHop {
-                                                                       pubkey: $dest_node_id.clone(),
-                                                                       short_channel_id: 0,
-                                                                       fee_msat: 0,
-                                                                       cltv_expiry_delta: 0,
-                                                       })
-                                               });
-                                               if $directional_info.src_node_id != network.our_node_id {
-                                                       // Ignore new_fee for channel-from-us as we assume all channels-from-us
-                                                       // will have the same effective-fee
-                                                       total_fee += new_fee;
-                                                       if let Some(fee_inc) = final_value_msat.checked_add(total_fee).and_then(|inc| { (old_entry.2 as u64).checked_mul(inc) }) {
-                                                               total_fee += fee_inc / 1000000 + (old_entry.1 as u64);
-                                                       } else {
-                                                               // max_value means we'll always fail the old_entry.0 > total_fee check
-                                                               total_fee = u64::max_value();
-                                                       }
-                                               }
-                                               let new_graph_node = RouteGraphNode {
-                                                       pubkey: $directional_info.src_node_id,
-                                                       lowest_fee_to_peer_through_node: total_fee,
-                                                       lowest_fee_to_node: $starting_fee_msat as u64 + new_fee,
-                                               };
-                                               if old_entry.0 > total_fee {
-                                                       targets.push(new_graph_node);
-                                                       old_entry.0 = total_fee;
-                                                       old_entry.3 = RouteHop {
-                                                               pubkey: $dest_node_id.clone(),
-                                                               short_channel_id: $chan_id.clone(),
-                                                               fee_msat: new_fee, // This field is ignored on the last-hop anyway
-                                                               cltv_expiry_delta: $directional_info.cltv_expiry_delta as u32,
-                                                       }
-                                               }
-                                       }
-                               }
-                       };
-               }
-
-               macro_rules! add_entries_to_cheapest_to_target_node {
-                       ( $node: expr, $node_id: expr, $fee_to_target_msat: expr ) => {
-                               if first_hops.is_some() {
-                                       if let Some(first_hop) = first_hop_targets.get(&$node_id) {
-                                               add_entry!(first_hop, $node_id, dummy_directional_info, $fee_to_target_msat);
-                                       }
-                               }
-
-                               for chan_id in $node.channels.iter() {
-                                       let chan = network.channels.get(chan_id).unwrap();
-                                       if chan.one_to_two.src_node_id == *$node_id {
-                                               // ie $node is one, ie next hop in A* is two, via the two_to_one channel
-                                               if first_hops.is_none() || chan.two_to_one.src_node_id != network.our_node_id {
-                                                       if chan.two_to_one.enabled {
-                                                               add_entry!(chan_id, chan.one_to_two.src_node_id, chan.two_to_one, $fee_to_target_msat);
-                                                       }
-                                               }
-                                       } else {
-                                               if first_hops.is_none() || chan.one_to_two.src_node_id != network.our_node_id {
-                                                       if chan.one_to_two.enabled {
-                                                               add_entry!(chan_id, chan.two_to_one.src_node_id, chan.one_to_two, $fee_to_target_msat);
-                                                       }
-                                               }
-                                       }
-                               }
-                       };
-               }
-
-               match network.nodes.get(target) {
-                       None => {},
-                       Some(node) => {
-                               add_entries_to_cheapest_to_target_node!(node, target, 0);
-                       },
-               }
-
-               for hop in last_hops.iter() {
-                       if first_hops.is_none() || hop.src_node_id != network.our_node_id { // first_hop overrules last_hops
-                               if network.nodes.get(&hop.src_node_id).is_some() {
-                                       if first_hops.is_some() {
-                                               if let Some(first_hop) = first_hop_targets.get(&hop.src_node_id) {
-                                                       add_entry!(first_hop, hop.src_node_id, dummy_directional_info, 0);
-                                               }
-                                       }
-                                       add_entry!(hop.short_channel_id, target, hop, 0);
-                               }
-                       }
-               }
-
-               while let Some(RouteGraphNode { pubkey, lowest_fee_to_node, .. }) = targets.pop() {
-                       if pubkey == network.our_node_id {
-                               let mut res = vec!(dist.remove(&network.our_node_id).unwrap().3);
-                               while res.last().unwrap().pubkey != *target {
-                                       let new_entry = match dist.remove(&res.last().unwrap().pubkey) {
-                                               Some(hop) => hop.3,
-                                               None => return Err(HandleError{err: "Failed to find a non-fee-overflowing path to the given destination", action: None}),
-                                       };
-                                       res.last_mut().unwrap().fee_msat = new_entry.fee_msat;
-                                       res.last_mut().unwrap().cltv_expiry_delta = new_entry.cltv_expiry_delta;
-                                       res.push(new_entry);
-                               }
-                               res.last_mut().unwrap().fee_msat = final_value_msat;
-                               res.last_mut().unwrap().cltv_expiry_delta = final_cltv;
-                               let route = Route { hops: res };
-                               log_trace!(self, "Got route: {}", log_route!(route));
-                               return Ok(route);
-                       }
-
-                       match network.nodes.get(&pubkey) {
-                               None => {},
-                               Some(node) => {
-                                       add_entries_to_cheapest_to_target_node!(node, &pubkey, lowest_fee_to_node);
-                               },
-                       }
-               }
-
-               Err(HandleError{err: "Failed to find a path to the given destination", action: None})
-       }
-}
-
-#[cfg(test)]
-mod tests {
-       use chain::chaininterface;
-       use ln::channelmanager;
-       use ln::router::{Router,NodeInfo,NetworkMap,ChannelInfo,DirectionalChannelInfo,RouteHint};
-       use ln::msgs::GlobalFeatures;
-       use util::test_utils;
-       use util::test_utils::TestVecWriter;
-       use util::logger::Logger;
-       use util::ser::{Writeable, Readable};
-
-       use bitcoin_hashes::sha256d::Hash as Sha256dHash;
-       use bitcoin_hashes::Hash;
-       use bitcoin::network::constants::Network;
-
-       use hex;
-
-       use secp256k1::key::{PublicKey,SecretKey};
-       use secp256k1::Secp256k1;
-
-       use std::sync::Arc;
-
-       #[test]
-       fn route_test() {
-               let secp_ctx = Secp256k1::new();
-               let our_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap());
-               let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::new());
-               let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, Arc::clone(&logger)));
-               let router = Router::new(our_id, chain_monitor, Arc::clone(&logger));
-
-               // Build network from our_id to node8:
-               //
-               //        -1(1)2-  node1  -1(3)2-
-               //       /                       \
-               // our_id -1(12)2- node8 -1(13)2--- node3
-               //       \                       /
-               //        -1(2)2-  node2  -1(4)2-
-               //
-               //
-               // chan1  1-to-2: disabled
-               // chan1  2-to-1: enabled, 0 fee
-               //
-               // chan2  1-to-2: enabled, ignored fee
-               // chan2  2-to-1: enabled, 0 fee
-               //
-               // chan3  1-to-2: enabled, 0 fee
-               // chan3  2-to-1: enabled, 100 msat fee
-               //
-               // chan4  1-to-2: enabled, 100% fee
-               // chan4  2-to-1: enabled, 0 fee
-               //
-               // chan12 1-to-2: enabled, ignored fee
-               // chan12 2-to-1: enabled, 0 fee
-               //
-               // chan13 1-to-2: enabled, 200% fee
-               // chan13 2-to-1: enabled, 0 fee
-               //
-               //
-               //       -1(5)2- node4 -1(8)2--
-               //       |         2          |
-               //       |       (11)         |
-               //      /          1           \
-               // node3--1(6)2- node5 -1(9)2--- node7 (not in global route map)
-               //      \                      /
-               //       -1(7)2- node6 -1(10)2-
-               //
-               // chan5  1-to-2: enabled, 100 msat fee
-               // chan5  2-to-1: enabled, 0 fee
-               //
-               // chan6  1-to-2: enabled, 0 fee
-               // chan6  2-to-1: enabled, 0 fee
-               //
-               // chan7  1-to-2: enabled, 100% fee
-               // chan7  2-to-1: enabled, 0 fee
-               //
-               // chan8  1-to-2: enabled, variable fee (0 then 1000 msat)
-               // chan8  2-to-1: enabled, 0 fee
-               //
-               // chan9  1-to-2: enabled, 1001 msat fee
-               // chan9  2-to-1: enabled, 0 fee
-               //
-               // chan10 1-to-2: enabled, 0 fee
-               // chan10 2-to-1: enabled, 0 fee
-               //
-               // chan11 1-to-2: enabled, 0 fee
-               // chan11 2-to-1: enabled, 0 fee
-
-               let node1 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap()[..]).unwrap());
-               let node2 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0303030303030303030303030303030303030303030303030303030303030303").unwrap()[..]).unwrap());
-               let node3 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0404040404040404040404040404040404040404040404040404040404040404").unwrap()[..]).unwrap());
-               let node4 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0505050505050505050505050505050505050505050505050505050505050505").unwrap()[..]).unwrap());
-               let node5 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0606060606060606060606060606060606060606060606060606060606060606").unwrap()[..]).unwrap());
-               let node6 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0707070707070707070707070707070707070707070707070707070707070707").unwrap()[..]).unwrap());
-               let node7 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0808080808080808080808080808080808080808080808080808080808080808").unwrap()[..]).unwrap());
-               let node8 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0909090909090909090909090909090909090909090909090909090909090909").unwrap()[..]).unwrap());
-
-               let zero_hash = Sha256dHash::hash(&[0; 32]);
-
-               {
-                       let mut network = router.network_map.write().unwrap();
-
-                       network.nodes.insert(node1.clone(), NodeInfo {
-                               channels: vec!(NetworkMap::get_key(1, zero_hash.clone()), NetworkMap::get_key(3, zero_hash.clone())),
-                               lowest_inbound_channel_fee_base_msat: 100,
-                               lowest_inbound_channel_fee_proportional_millionths: 0,
-                               features: GlobalFeatures::new(),
-                               last_update: 1,
-                               rgb: [0; 3],
-                               alias: [0; 32],
-                               addresses: Vec::new(),
-                               announcement_message: None,
-                       });
-                       network.channels.insert(NetworkMap::get_key(1, zero_hash.clone()), ChannelInfo {
-                               features: GlobalFeatures::new(),
-                               one_to_two: DirectionalChannelInfo {
-                                       src_node_id: our_id.clone(),
-                                       last_update: 0,
-                                       enabled: false,
-                                       cltv_expiry_delta: u16::max_value(), // This value should be ignored
-                                       htlc_minimum_msat: 0,
-                                       fee_base_msat: u32::max_value(), // This value should be ignored
-                                       fee_proportional_millionths: u32::max_value(), // This value should be ignored
-                                       last_update_message: None,
-                               }, two_to_one: DirectionalChannelInfo {
-                                       src_node_id: node1.clone(),
-                                       last_update: 0,
-                                       enabled: true,
-                                       cltv_expiry_delta: 0,
-                                       htlc_minimum_msat: 0,
-                                       fee_base_msat: 0,
-                                       fee_proportional_millionths: 0,
-                                       last_update_message: None,
-                               },
-                               announcement_message: None,
-                       });
-                       network.nodes.insert(node2.clone(), NodeInfo {
-                               channels: vec!(NetworkMap::get_key(2, zero_hash.clone()), NetworkMap::get_key(4, zero_hash.clone())),
-                               lowest_inbound_channel_fee_base_msat: 0,
-                               lowest_inbound_channel_fee_proportional_millionths: 0,
-                               features: GlobalFeatures::new(),
-                               last_update: 1,
-                               rgb: [0; 3],
-                               alias: [0; 32],
-                               addresses: Vec::new(),
-                               announcement_message: None,
-                       });
-                       network.channels.insert(NetworkMap::get_key(2, zero_hash.clone()), ChannelInfo {
-                               features: GlobalFeatures::new(),
-                               one_to_two: DirectionalChannelInfo {
-                                       src_node_id: our_id.clone(),
-                                       last_update: 0,
-                                       enabled: true,
-                                       cltv_expiry_delta: u16::max_value(), // This value should be ignored
-                                       htlc_minimum_msat: 0,
-                                       fee_base_msat: u32::max_value(), // This value should be ignored
-                                       fee_proportional_millionths: u32::max_value(), // This value should be ignored
-                                       last_update_message: None,
-                               }, two_to_one: DirectionalChannelInfo {
-                                       src_node_id: node2.clone(),
-                                       last_update: 0,
-                                       enabled: true,
-                                       cltv_expiry_delta: 0,
-                                       htlc_minimum_msat: 0,
-                                       fee_base_msat: 0,
-                                       fee_proportional_millionths: 0,
-                                       last_update_message: None,
-                               },
-                               announcement_message: None,
-                       });
-                       network.nodes.insert(node8.clone(), NodeInfo {
-                               channels: vec!(NetworkMap::get_key(12, zero_hash.clone()), NetworkMap::get_key(13, zero_hash.clone())),
-                               lowest_inbound_channel_fee_base_msat: 0,
-                               lowest_inbound_channel_fee_proportional_millionths: 0,
-                               features: GlobalFeatures::new(),
-                               last_update: 1,
-                               rgb: [0; 3],
-                               alias: [0; 32],
-                               addresses: Vec::new(),
-                               announcement_message: None,
-                       });
-                       network.channels.insert(NetworkMap::get_key(12, zero_hash.clone()), ChannelInfo {
-                               features: GlobalFeatures::new(),
-                               one_to_two: DirectionalChannelInfo {
-                                       src_node_id: our_id.clone(),
-                                       last_update: 0,
-                                       enabled: true,
-                                       cltv_expiry_delta: u16::max_value(), // This value should be ignored
-                                       htlc_minimum_msat: 0,
-                                       fee_base_msat: u32::max_value(), // This value should be ignored
-                                       fee_proportional_millionths: u32::max_value(), // This value should be ignored
-                                       last_update_message: None,
-                               }, two_to_one: DirectionalChannelInfo {
-                                       src_node_id: node8.clone(),
-                                       last_update: 0,
-                                       enabled: true,
-                                       cltv_expiry_delta: 0,
-                                       htlc_minimum_msat: 0,
-                                       fee_base_msat: 0,
-                                       fee_proportional_millionths: 0,
-                                       last_update_message: None,
-                               },
-                               announcement_message: None,
-                       });
-                       network.nodes.insert(node3.clone(), NodeInfo {
-                               channels: vec!(
-                                       NetworkMap::get_key(3, zero_hash.clone()),
-                                       NetworkMap::get_key(4, zero_hash.clone()),
-                                       NetworkMap::get_key(13, zero_hash.clone()),
-                                       NetworkMap::get_key(5, zero_hash.clone()),
-                                       NetworkMap::get_key(6, zero_hash.clone()),
-                                       NetworkMap::get_key(7, zero_hash.clone())),
-                               lowest_inbound_channel_fee_base_msat: 0,
-                               lowest_inbound_channel_fee_proportional_millionths: 0,
-                               features: GlobalFeatures::new(),
-                               last_update: 1,
-                               rgb: [0; 3],
-                               alias: [0; 32],
-                               addresses: Vec::new(),
-                               announcement_message: None,
-                       });
-                       network.channels.insert(NetworkMap::get_key(3, zero_hash.clone()), ChannelInfo {
-                               features: GlobalFeatures::new(),
-                               one_to_two: DirectionalChannelInfo {
-                                       src_node_id: node1.clone(),
-                                       last_update: 0,
-                                       enabled: true,
-                                       cltv_expiry_delta: (3 << 8) | 1,
-                                       htlc_minimum_msat: 0,
-                                       fee_base_msat: 0,
-                                       fee_proportional_millionths: 0,
-                                       last_update_message: None,
-                               }, two_to_one: DirectionalChannelInfo {
-                                       src_node_id: node3.clone(),
-                                       last_update: 0,
-                                       enabled: true,
-                                       cltv_expiry_delta: (3 << 8) | 2,
-                                       htlc_minimum_msat: 0,
-                                       fee_base_msat: 100,
-                                       fee_proportional_millionths: 0,
-                                       last_update_message: None,
-                               },
-                               announcement_message: None,
-                       });
-                       network.channels.insert(NetworkMap::get_key(4, zero_hash.clone()), ChannelInfo {
-                               features: GlobalFeatures::new(),
-                               one_to_two: DirectionalChannelInfo {
-                                       src_node_id: node2.clone(),
-                                       last_update: 0,
-                                       enabled: true,
-                                       cltv_expiry_delta: (4 << 8) | 1,
-                                       htlc_minimum_msat: 0,
-                                       fee_base_msat: 0,
-                                       fee_proportional_millionths: 1000000,
-                                       last_update_message: None,
-                               }, two_to_one: DirectionalChannelInfo {
-                                       src_node_id: node3.clone(),
-                                       last_update: 0,
-                                       enabled: true,
-                                       cltv_expiry_delta: (4 << 8) | 2,
-                                       htlc_minimum_msat: 0,
-                                       fee_base_msat: 0,
-                                       fee_proportional_millionths: 0,
-                                       last_update_message: None,
-                               },
-                               announcement_message: None,
-                       });
-                       network.channels.insert(NetworkMap::get_key(13, zero_hash.clone()), ChannelInfo {
-                               features: GlobalFeatures::new(),
-                               one_to_two: DirectionalChannelInfo {
-                                       src_node_id: node8.clone(),
-                                       last_update: 0,
-                                       enabled: true,
-                                       cltv_expiry_delta: (13 << 8) | 1,
-                                       htlc_minimum_msat: 0,
-                                       fee_base_msat: 0,
-                                       fee_proportional_millionths: 2000000,
-                                       last_update_message: None,
-                               }, two_to_one: DirectionalChannelInfo {
-                                       src_node_id: node3.clone(),
-                                       last_update: 0,
-                                       enabled: true,
-                                       cltv_expiry_delta: (13 << 8) | 2,
-                                       htlc_minimum_msat: 0,
-                                       fee_base_msat: 0,
-                                       fee_proportional_millionths: 0,
-                                       last_update_message: None,
-                               },
-                               announcement_message: None,
-                       });
-                       network.nodes.insert(node4.clone(), NodeInfo {
-                               channels: vec!(NetworkMap::get_key(5, zero_hash.clone()), NetworkMap::get_key(11, zero_hash.clone())),
-                               lowest_inbound_channel_fee_base_msat: 0,
-                               lowest_inbound_channel_fee_proportional_millionths: 0,
-                               features: GlobalFeatures::new(),
-                               last_update: 1,
-                               rgb: [0; 3],
-                               alias: [0; 32],
-                               addresses: Vec::new(),
-                               announcement_message: None,
-                       });
-                       network.channels.insert(NetworkMap::get_key(5, zero_hash.clone()), ChannelInfo {
-                               features: GlobalFeatures::new(),
-                               one_to_two: DirectionalChannelInfo {
-                                       src_node_id: node3.clone(),
-                                       last_update: 0,
-                                       enabled: true,
-                                       cltv_expiry_delta: (5 << 8) | 1,
-                                       htlc_minimum_msat: 0,
-                                       fee_base_msat: 100,
-                                       fee_proportional_millionths: 0,
-                                       last_update_message: None,
-                               }, two_to_one: DirectionalChannelInfo {
-                                       src_node_id: node4.clone(),
-                                       last_update: 0,
-                                       enabled: true,
-                                       cltv_expiry_delta: (5 << 8) | 2,
-                                       htlc_minimum_msat: 0,
-                                       fee_base_msat: 0,
-                                       fee_proportional_millionths: 0,
-                                       last_update_message: None,
-                               },
-                               announcement_message: None,
-                       });
-                       network.nodes.insert(node5.clone(), NodeInfo {
-                               channels: vec!(NetworkMap::get_key(6, zero_hash.clone()), NetworkMap::get_key(11, zero_hash.clone())),
-                               lowest_inbound_channel_fee_base_msat: 0,
-                               lowest_inbound_channel_fee_proportional_millionths: 0,
-                               features: GlobalFeatures::new(),
-                               last_update: 1,
-                               rgb: [0; 3],
-                               alias: [0; 32],
-                               addresses: Vec::new(),
-                               announcement_message: None,
-                       });
-                       network.channels.insert(NetworkMap::get_key(6, zero_hash.clone()), ChannelInfo {
-                               features: GlobalFeatures::new(),
-                               one_to_two: DirectionalChannelInfo {
-                                       src_node_id: node3.clone(),
-                                       last_update: 0,
-                                       enabled: true,
-                                       cltv_expiry_delta: (6 << 8) | 1,
-                                       htlc_minimum_msat: 0,
-                                       fee_base_msat: 0,
-                                       fee_proportional_millionths: 0,
-                                       last_update_message: None,
-                               }, two_to_one: DirectionalChannelInfo {
-                                       src_node_id: node5.clone(),
-                                       last_update: 0,
-                                       enabled: true,
-                                       cltv_expiry_delta: (6 << 8) | 2,
-                                       htlc_minimum_msat: 0,
-                                       fee_base_msat: 0,
-                                       fee_proportional_millionths: 0,
-                                       last_update_message: None,
-                               },
-                               announcement_message: None,
-                       });
-                       network.channels.insert(NetworkMap::get_key(11, zero_hash.clone()), ChannelInfo {
-                               features: GlobalFeatures::new(),
-                               one_to_two: DirectionalChannelInfo {
-                                       src_node_id: node5.clone(),
-                                       last_update: 0,
-                                       enabled: true,
-                                       cltv_expiry_delta: (11 << 8) | 1,
-                                       htlc_minimum_msat: 0,
-                                       fee_base_msat: 0,
-                                       fee_proportional_millionths: 0,
-                                       last_update_message: None,
-                               }, two_to_one: DirectionalChannelInfo {
-                                       src_node_id: node4.clone(),
-                                       last_update: 0,
-                                       enabled: true,
-                                       cltv_expiry_delta: (11 << 8) | 2,
-                                       htlc_minimum_msat: 0,
-                                       fee_base_msat: 0,
-                                       fee_proportional_millionths: 0,
-                                       last_update_message: None,
-                               },
-                               announcement_message: None,
-                       });
-                       network.nodes.insert(node6.clone(), NodeInfo {
-                               channels: vec!(NetworkMap::get_key(7, zero_hash.clone())),
-                               lowest_inbound_channel_fee_base_msat: 0,
-                               lowest_inbound_channel_fee_proportional_millionths: 0,
-                               features: GlobalFeatures::new(),
-                               last_update: 1,
-                               rgb: [0; 3],
-                               alias: [0; 32],
-                               addresses: Vec::new(),
-                               announcement_message: None,
-                       });
-                       network.channels.insert(NetworkMap::get_key(7, zero_hash.clone()), ChannelInfo {
-                               features: GlobalFeatures::new(),
-                               one_to_two: DirectionalChannelInfo {
-                                       src_node_id: node3.clone(),
-                                       last_update: 0,
-                                       enabled: true,
-                                       cltv_expiry_delta: (7 << 8) | 1,
-                                       htlc_minimum_msat: 0,
-                                       fee_base_msat: 0,
-                                       fee_proportional_millionths: 1000000,
-                                       last_update_message: None,
-                               }, two_to_one: DirectionalChannelInfo {
-                                       src_node_id: node6.clone(),
-                                       last_update: 0,
-                                       enabled: true,
-                                       cltv_expiry_delta: (7 << 8) | 2,
-                                       htlc_minimum_msat: 0,
-                                       fee_base_msat: 0,
-                                       fee_proportional_millionths: 0,
-                                       last_update_message: None,
-                               },
-                               announcement_message: None,
-                       });
-               }
-
-               { // Simple route to 3 via 2
-                       let route = router.get_route(&node3, None, &Vec::new(), 100, 42).unwrap();
-                       assert_eq!(route.hops.len(), 2);
-
-                       assert_eq!(route.hops[0].pubkey, node2);
-                       assert_eq!(route.hops[0].short_channel_id, 2);
-                       assert_eq!(route.hops[0].fee_msat, 100);
-                       assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
-
-                       assert_eq!(route.hops[1].pubkey, node3);
-                       assert_eq!(route.hops[1].short_channel_id, 4);
-                       assert_eq!(route.hops[1].fee_msat, 100);
-                       assert_eq!(route.hops[1].cltv_expiry_delta, 42);
-               }
-
-               { // Route to 1 via 2 and 3 because our channel to 1 is disabled
-                       let route = router.get_route(&node1, None, &Vec::new(), 100, 42).unwrap();
-                       assert_eq!(route.hops.len(), 3);
-
-                       assert_eq!(route.hops[0].pubkey, node2);
-                       assert_eq!(route.hops[0].short_channel_id, 2);
-                       assert_eq!(route.hops[0].fee_msat, 200);
-                       assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
-
-                       assert_eq!(route.hops[1].pubkey, node3);
-                       assert_eq!(route.hops[1].short_channel_id, 4);
-                       assert_eq!(route.hops[1].fee_msat, 100);
-                       assert_eq!(route.hops[1].cltv_expiry_delta, (3 << 8) | 2);
-
-                       assert_eq!(route.hops[2].pubkey, node1);
-                       assert_eq!(route.hops[2].short_channel_id, 3);
-                       assert_eq!(route.hops[2].fee_msat, 100);
-                       assert_eq!(route.hops[2].cltv_expiry_delta, 42);
-               }
-
-               { // If we specify a channel to node8, that overrides our local channel view and that gets used
-                       let our_chans = vec![channelmanager::ChannelDetails {
-                               channel_id: [0; 32],
-                               short_channel_id: Some(42),
-                               remote_network_id: node8.clone(),
-                               channel_value_satoshis: 0,
-                               user_id: 0,
-                               outbound_capacity_msat: 0,
-                               inbound_capacity_msat: 0,
-                               is_live: true,
-                       }];
-                       let route = router.get_route(&node3, Some(&our_chans), &Vec::new(), 100, 42).unwrap();
-                       assert_eq!(route.hops.len(), 2);
-
-                       assert_eq!(route.hops[0].pubkey, node8);
-                       assert_eq!(route.hops[0].short_channel_id, 42);
-                       assert_eq!(route.hops[0].fee_msat, 200);
-                       assert_eq!(route.hops[0].cltv_expiry_delta, (13 << 8) | 1);
-
-                       assert_eq!(route.hops[1].pubkey, node3);
-                       assert_eq!(route.hops[1].short_channel_id, 13);
-                       assert_eq!(route.hops[1].fee_msat, 100);
-                       assert_eq!(route.hops[1].cltv_expiry_delta, 42);
-               }
-
-               let mut last_hops = vec!(RouteHint {
-                               src_node_id: node4.clone(),
-                               short_channel_id: 8,
-                               fee_base_msat: 0,
-                               fee_proportional_millionths: 0,
-                               cltv_expiry_delta: (8 << 8) | 1,
-                               htlc_minimum_msat: 0,
-                       }, RouteHint {
-                               src_node_id: node5.clone(),
-                               short_channel_id: 9,
-                               fee_base_msat: 1001,
-                               fee_proportional_millionths: 0,
-                               cltv_expiry_delta: (9 << 8) | 1,
-                               htlc_minimum_msat: 0,
-                       }, RouteHint {
-                               src_node_id: node6.clone(),
-                               short_channel_id: 10,
-                               fee_base_msat: 0,
-                               fee_proportional_millionths: 0,
-                               cltv_expiry_delta: (10 << 8) | 1,
-                               htlc_minimum_msat: 0,
-                       });
-
-               { // Simple test across 2, 3, 5, and 4 via a last_hop channel
-                       let route = router.get_route(&node7, None, &last_hops, 100, 42).unwrap();
-                       assert_eq!(route.hops.len(), 5);
-
-                       assert_eq!(route.hops[0].pubkey, node2);
-                       assert_eq!(route.hops[0].short_channel_id, 2);
-                       assert_eq!(route.hops[0].fee_msat, 100);
-                       assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
-
-                       assert_eq!(route.hops[1].pubkey, node3);
-                       assert_eq!(route.hops[1].short_channel_id, 4);
-                       assert_eq!(route.hops[1].fee_msat, 0);
-                       assert_eq!(route.hops[1].cltv_expiry_delta, (6 << 8) | 1);
-
-                       assert_eq!(route.hops[2].pubkey, node5);
-                       assert_eq!(route.hops[2].short_channel_id, 6);
-                       assert_eq!(route.hops[2].fee_msat, 0);
-                       assert_eq!(route.hops[2].cltv_expiry_delta, (11 << 8) | 1);
-
-                       assert_eq!(route.hops[3].pubkey, node4);
-                       assert_eq!(route.hops[3].short_channel_id, 11);
-                       assert_eq!(route.hops[3].fee_msat, 0);
-                       assert_eq!(route.hops[3].cltv_expiry_delta, (8 << 8) | 1);
-
-                       assert_eq!(route.hops[4].pubkey, node7);
-                       assert_eq!(route.hops[4].short_channel_id, 8);
-                       assert_eq!(route.hops[4].fee_msat, 100);
-                       assert_eq!(route.hops[4].cltv_expiry_delta, 42);
-               }
-
-               { // Simple test with outbound channel to 4 to test that last_hops and first_hops connect
-                       let our_chans = vec![channelmanager::ChannelDetails {
-                               channel_id: [0; 32],
-                               short_channel_id: Some(42),
-                               remote_network_id: node4.clone(),
-                               channel_value_satoshis: 0,
-                               user_id: 0,
-                               outbound_capacity_msat: 0,
-                               inbound_capacity_msat: 0,
-                               is_live: true,
-                       }];
-                       let route = router.get_route(&node7, Some(&our_chans), &last_hops, 100, 42).unwrap();
-                       assert_eq!(route.hops.len(), 2);
-
-                       assert_eq!(route.hops[0].pubkey, node4);
-                       assert_eq!(route.hops[0].short_channel_id, 42);
-                       assert_eq!(route.hops[0].fee_msat, 0);
-                       assert_eq!(route.hops[0].cltv_expiry_delta, (8 << 8) | 1);
-
-                       assert_eq!(route.hops[1].pubkey, node7);
-                       assert_eq!(route.hops[1].short_channel_id, 8);
-                       assert_eq!(route.hops[1].fee_msat, 100);
-                       assert_eq!(route.hops[1].cltv_expiry_delta, 42);
-               }
-
-               last_hops[0].fee_base_msat = 1000;
-
-               { // Revert to via 6 as the fee on 8 goes up
-                       let route = router.get_route(&node7, None, &last_hops, 100, 42).unwrap();
-                       assert_eq!(route.hops.len(), 4);
-
-                       assert_eq!(route.hops[0].pubkey, node2);
-                       assert_eq!(route.hops[0].short_channel_id, 2);
-                       assert_eq!(route.hops[0].fee_msat, 200); // fee increased as its % of value transferred across node
-                       assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
-
-                       assert_eq!(route.hops[1].pubkey, node3);
-                       assert_eq!(route.hops[1].short_channel_id, 4);
-                       assert_eq!(route.hops[1].fee_msat, 100);
-                       assert_eq!(route.hops[1].cltv_expiry_delta, (7 << 8) | 1);
-
-                       assert_eq!(route.hops[2].pubkey, node6);
-                       assert_eq!(route.hops[2].short_channel_id, 7);
-                       assert_eq!(route.hops[2].fee_msat, 0);
-                       assert_eq!(route.hops[2].cltv_expiry_delta, (10 << 8) | 1);
-
-                       assert_eq!(route.hops[3].pubkey, node7);
-                       assert_eq!(route.hops[3].short_channel_id, 10);
-                       assert_eq!(route.hops[3].fee_msat, 100);
-                       assert_eq!(route.hops[3].cltv_expiry_delta, 42);
-               }
-
-               { // ...but still use 8 for larger payments as 6 has a variable feerate
-                       let route = router.get_route(&node7, None, &last_hops, 2000, 42).unwrap();
-                       assert_eq!(route.hops.len(), 5);
-
-                       assert_eq!(route.hops[0].pubkey, node2);
-                       assert_eq!(route.hops[0].short_channel_id, 2);
-                       assert_eq!(route.hops[0].fee_msat, 3000);
-                       assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
-
-                       assert_eq!(route.hops[1].pubkey, node3);
-                       assert_eq!(route.hops[1].short_channel_id, 4);
-                       assert_eq!(route.hops[1].fee_msat, 0);
-                       assert_eq!(route.hops[1].cltv_expiry_delta, (6 << 8) | 1);
-
-                       assert_eq!(route.hops[2].pubkey, node5);
-                       assert_eq!(route.hops[2].short_channel_id, 6);
-                       assert_eq!(route.hops[2].fee_msat, 0);
-                       assert_eq!(route.hops[2].cltv_expiry_delta, (11 << 8) | 1);
-
-                       assert_eq!(route.hops[3].pubkey, node4);
-                       assert_eq!(route.hops[3].short_channel_id, 11);
-                       assert_eq!(route.hops[3].fee_msat, 1000);
-                       assert_eq!(route.hops[3].cltv_expiry_delta, (8 << 8) | 1);
-
-                       assert_eq!(route.hops[4].pubkey, node7);
-                       assert_eq!(route.hops[4].short_channel_id, 8);
-                       assert_eq!(route.hops[4].fee_msat, 2000);
-                       assert_eq!(route.hops[4].cltv_expiry_delta, 42);
-               }
-
-               { // Test Router serialization/deserialization
-                       let mut w = TestVecWriter(Vec::new());
-                       let network = router.network_map.read().unwrap();
-                       assert!(!network.channels.is_empty());
-                       assert!(!network.nodes.is_empty());
-                       network.write(&mut w).unwrap();
-                       assert!(<NetworkMap>::read(&mut ::std::io::Cursor::new(&w.0)).unwrap() == *network);
-               }
-       }
-}
diff --git a/src/util/byte_utils.rs b/src/util/byte_utils.rs
deleted file mode 100644 (file)
index 4444566..0000000
+++ /dev/null
@@ -1,106 +0,0 @@
-#[inline]
-pub fn slice_to_be16(v: &[u8]) -> u16 {
-       ((v[0] as u16) << 8*1) |
-       ((v[1] as u16) << 8*0)
-}
-#[inline]
-pub fn slice_to_be32(v: &[u8]) -> u32 {
-       ((v[0] as u32) << 8*3) |
-       ((v[1] as u32) << 8*2) |
-       ((v[2] as u32) << 8*1) |
-       ((v[3] as u32) << 8*0)
-}
-#[cfg(not(feature = "fuzztarget"))] // Used only by poly1305
-#[inline]
-pub fn slice_to_le32(v: &[u8]) -> u32 {
-       ((v[0] as u32) << 8*0) |
-       ((v[1] as u32) << 8*1) |
-       ((v[2] as u32) << 8*2) |
-       ((v[3] as u32) << 8*3)
-}
-#[inline]
-pub fn slice_to_be48(v: &[u8]) -> u64 {
-       ((v[0] as u64) << 8*5) |
-       ((v[1] as u64) << 8*4) |
-       ((v[2] as u64) << 8*3) |
-       ((v[3] as u64) << 8*2) |
-       ((v[4] as u64) << 8*1) |
-       ((v[5] as u64) << 8*0)
-}
-#[inline]
-pub fn slice_to_be64(v: &[u8]) -> u64 {
-       ((v[0] as u64) << 8*7) |
-       ((v[1] as u64) << 8*6) |
-       ((v[2] as u64) << 8*5) |
-       ((v[3] as u64) << 8*4) |
-       ((v[4] as u64) << 8*3) |
-       ((v[5] as u64) << 8*2) |
-       ((v[6] as u64) << 8*1) |
-       ((v[7] as u64) << 8*0)
-}
-
-#[inline]
-pub fn be16_to_array(u: u16) -> [u8; 2] {
-       let mut v = [0; 2];
-       v[0] = ((u >> 8*1) & 0xff) as u8;
-       v[1] = ((u >> 8*0) & 0xff) as u8;
-       v
-}
-#[inline]
-pub fn be32_to_array(u: u32) -> [u8; 4] {
-       let mut v = [0; 4];
-       v[0] = ((u >> 8*3) & 0xff) as u8;
-       v[1] = ((u >> 8*2) & 0xff) as u8;
-       v[2] = ((u >> 8*1) & 0xff) as u8;
-       v[3] = ((u >> 8*0) & 0xff) as u8;
-       v
-}
-#[cfg(not(feature = "fuzztarget"))] // Used only by poly1305
-#[inline]
-pub fn le32_to_array(u: u32) -> [u8; 4] {
-       let mut v = [0; 4];
-       v[0] = ((u >> 8*0) & 0xff) as u8;
-       v[1] = ((u >> 8*1) & 0xff) as u8;
-       v[2] = ((u >> 8*2) & 0xff) as u8;
-       v[3] = ((u >> 8*3) & 0xff) as u8;
-       v
-}
-#[inline]
-pub fn be48_to_array(u: u64) -> [u8; 6] {
-       assert!(u & 0xffff_0000_0000_0000 == 0);
-       let mut v = [0; 6];
-       v[0] = ((u >> 8*5) & 0xff) as u8;
-       v[1] = ((u >> 8*4) & 0xff) as u8;
-       v[2] = ((u >> 8*3) & 0xff) as u8;
-       v[3] = ((u >> 8*2) & 0xff) as u8;
-       v[4] = ((u >> 8*1) & 0xff) as u8;
-       v[5] = ((u >> 8*0) & 0xff) as u8;
-       v
-}
-#[inline]
-pub fn be64_to_array(u: u64) -> [u8; 8] {
-       let mut v = [0; 8];
-       v[0] = ((u >> 8*7) & 0xff) as u8;
-       v[1] = ((u >> 8*6) & 0xff) as u8;
-       v[2] = ((u >> 8*5) & 0xff) as u8;
-       v[3] = ((u >> 8*4) & 0xff) as u8;
-       v[4] = ((u >> 8*3) & 0xff) as u8;
-       v[5] = ((u >> 8*2) & 0xff) as u8;
-       v[6] = ((u >> 8*1) & 0xff) as u8;
-       v[7] = ((u >> 8*0) & 0xff) as u8;
-       v
-}
-
-#[inline]
-pub fn le64_to_array(u: u64) -> [u8; 8] {
-       let mut v = [0; 8];
-       v[0] = ((u >> 8*0) & 0xff) as u8;
-       v[1] = ((u >> 8*1) & 0xff) as u8;
-       v[2] = ((u >> 8*2) & 0xff) as u8;
-       v[3] = ((u >> 8*3) & 0xff) as u8;
-       v[4] = ((u >> 8*4) & 0xff) as u8;
-       v[5] = ((u >> 8*5) & 0xff) as u8;
-       v[6] = ((u >> 8*6) & 0xff) as u8;
-       v[7] = ((u >> 8*7) & 0xff) as u8;
-       v
-}
diff --git a/src/util/chacha20.rs b/src/util/chacha20.rs
deleted file mode 100644 (file)
index dd90b20..0000000
+++ /dev/null
@@ -1,550 +0,0 @@
-// This file was stolen from rust-crypto.
-// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-#[cfg(not(feature = "fuzztarget"))]
-mod real_chacha {
-       use std::cmp;
-       use util::byte_utils::{slice_to_le32, le32_to_array};
-
-       #[derive(Clone, Copy, PartialEq, Eq)]
-       #[allow(non_camel_case_types)]
-       struct u32x4(pub u32, pub u32, pub u32, pub u32);
-       impl ::std::ops::Add for u32x4 {
-               type Output = u32x4;
-               fn add(self, rhs: u32x4) -> u32x4 {
-                       u32x4(self.0.wrapping_add(rhs.0),
-                             self.1.wrapping_add(rhs.1),
-                             self.2.wrapping_add(rhs.2),
-                             self.3.wrapping_add(rhs.3))
-               }
-       }
-       impl ::std::ops::Sub for u32x4 {
-               type Output = u32x4;
-               fn sub(self, rhs: u32x4) -> u32x4 {
-                       u32x4(self.0.wrapping_sub(rhs.0),
-                             self.1.wrapping_sub(rhs.1),
-                             self.2.wrapping_sub(rhs.2),
-                             self.3.wrapping_sub(rhs.3))
-               }
-       }
-       impl ::std::ops::BitXor for u32x4 {
-               type Output = u32x4;
-               fn bitxor(self, rhs: u32x4) -> u32x4 {
-                       u32x4(self.0 ^ rhs.0, self.1 ^ rhs.1, self.2 ^ rhs.2, self.3 ^ rhs.3)
-               }
-       }
-       impl ::std::ops::Shr<u32x4> for u32x4 {
-               type Output = u32x4;
-               fn shr(self, rhs: u32x4) -> u32x4 {
-                       u32x4(self.0 >> rhs.0, self.1 >> rhs.1, self.2 >> rhs.2, self.3 >> rhs.3)
-               }
-       }
-       impl ::std::ops::Shl<u32x4> for u32x4 {
-               type Output = u32x4;
-               fn shl(self, rhs: u32x4) -> u32x4 {
-                       u32x4(self.0 << rhs.0, self.1 << rhs.1, self.2 << rhs.2, self.3 << rhs.3)
-               }
-       }
-
-       #[derive(Clone,Copy)]
-       struct ChaChaState {
-               a: u32x4,
-               b: u32x4,
-               c: u32x4,
-               d: u32x4
-       }
-
-       #[derive(Copy)]
-       pub struct ChaCha20 {
-               state  : ChaChaState,
-               output : [u8; 64],
-               offset : usize,
-       }
-
-       impl Clone for ChaCha20 { fn clone(&self) -> ChaCha20 { *self } }
-
-       macro_rules! swizzle {
-               ($b: expr, $c: expr, $d: expr) => {{
-                       let u32x4(b10, b11, b12, b13) = $b;
-                       $b = u32x4(b11, b12, b13, b10);
-                       let u32x4(c10, c11, c12, c13) = $c;
-                       $c = u32x4(c12, c13,c10, c11);
-                       let u32x4(d10, d11, d12, d13) = $d;
-                       $d = u32x4(d13, d10, d11, d12);
-               }}
-       }
-
-       macro_rules! state_to_buffer {
-               ($state: expr, $output: expr) => {{
-                       let u32x4(a1, a2, a3, a4) = $state.a;
-                       let u32x4(b1, b2, b3, b4) = $state.b;
-                       let u32x4(c1, c2, c3, c4) = $state.c;
-                       let u32x4(d1, d2, d3, d4) = $state.d;
-                       let lens = [
-                               a1,a2,a3,a4,
-                               b1,b2,b3,b4,
-                               c1,c2,c3,c4,
-                               d1,d2,d3,d4
-                       ];
-                       for i in 0..lens.len() {
-                               $output[i*4..(i+1)*4].copy_from_slice(&le32_to_array(lens[i]));
-                       }
-               }}
-       }
-
-       macro_rules! round{
-               ($state: expr) => {{
-                       $state.a = $state.a + $state.b;
-                       rotate!($state.d, $state.a, S16);
-                       $state.c = $state.c + $state.d;
-                       rotate!($state.b, $state.c, S12);
-                       $state.a = $state.a + $state.b;
-                       rotate!($state.d, $state.a, S8);
-                       $state.c = $state.c + $state.d;
-                       rotate!($state.b, $state.c, S7);
-               }}
-       }
-
-       macro_rules! rotate {
-               ($a: expr, $b: expr, $c:expr) => {{
-                       let v = $a ^ $b;
-                       let r = S32 - $c;
-                       let right = v >> r;
-                       $a = (v << $c) ^ right
-               }}
-       }
-
-       const S32:u32x4 = u32x4(32, 32, 32, 32);
-       const S16:u32x4 = u32x4(16, 16, 16, 16);
-       const S12:u32x4 = u32x4(12, 12, 12, 12);
-       const S8:u32x4 = u32x4(8, 8, 8, 8);
-       const S7:u32x4 = u32x4(7, 7, 7, 7);
-
-       impl ChaCha20 {
-               pub fn new(key: &[u8], nonce: &[u8]) -> ChaCha20 {
-                       assert!(key.len() == 16 || key.len() == 32);
-                       assert!(nonce.len() == 8 || nonce.len() == 12);
-
-                       ChaCha20{ state: ChaCha20::expand(key, nonce), output: [0u8; 64], offset: 64 }
-               }
-
-               fn expand(key: &[u8], nonce: &[u8]) -> ChaChaState {
-                       let constant = match key.len() {
-                               16 => b"expand 16-byte k",
-                               32 => b"expand 32-byte k",
-                               _  => unreachable!(),
-                       };
-                       ChaChaState {
-                               a: u32x4(
-                                       slice_to_le32(&constant[0..4]),
-                                       slice_to_le32(&constant[4..8]),
-                                       slice_to_le32(&constant[8..12]),
-                                       slice_to_le32(&constant[12..16])
-                               ),
-                               b: u32x4(
-                                       slice_to_le32(&key[0..4]),
-                                       slice_to_le32(&key[4..8]),
-                                       slice_to_le32(&key[8..12]),
-                                       slice_to_le32(&key[12..16])
-                               ),
-                               c: if key.len() == 16 {
-                                       u32x4(
-                                               slice_to_le32(&key[0..4]),
-                                               slice_to_le32(&key[4..8]),
-                                               slice_to_le32(&key[8..12]),
-                                               slice_to_le32(&key[12..16])
-                                       )
-                               } else {
-                                       u32x4(
-                                               slice_to_le32(&key[16..20]),
-                                               slice_to_le32(&key[20..24]),
-                                               slice_to_le32(&key[24..28]),
-                                               slice_to_le32(&key[28..32])
-                                       )
-                               },
-                               d: if nonce.len() == 16 {
-                                       u32x4(
-                                               slice_to_le32(&nonce[0..4]),
-                                               slice_to_le32(&nonce[4..8]),
-                                               slice_to_le32(&nonce[8..12]),
-                                               slice_to_le32(&nonce[12..16])
-                                       )
-                               } else if nonce.len() == 12 {
-                                       u32x4(
-                                               0,
-                                               slice_to_le32(&nonce[0..4]),
-                                               slice_to_le32(&nonce[4..8]),
-                                               slice_to_le32(&nonce[8..12])
-                                       )
-                               } else {
-                                       u32x4(
-                                               0,
-                                               0,
-                                               slice_to_le32(&nonce[0..4]),
-                                               slice_to_le32(&nonce[4..8])
-                                       )
-                               }
-                       }
-               }
-
-               // put the the next 64 keystream bytes into self.output
-               fn update(&mut self) {
-                       let mut state = self.state;
-
-                       for _ in 0..10 {
-                               round!(state);
-                               swizzle!(state.b, state.c, state.d);
-                               round!(state);
-                               swizzle!(state.d, state.c, state.b);
-                       }
-                       state.a = state.a + self.state.a;
-                       state.b = state.b + self.state.b;
-                       state.c = state.c + self.state.c;
-                       state.d = state.d + self.state.d;
-
-                       state_to_buffer!(state, self.output);
-
-                       self.state.d = self.state.d + u32x4(1, 0, 0, 0);
-                       let u32x4(c12, _, _, _) = self.state.d;
-                       if c12 == 0 {
-                               // we could increment the other counter word with an 8 byte nonce
-                               // but other implementations like boringssl have this same
-                               // limitation
-                               panic!("counter is exhausted");
-                       }
-
-                       self.offset = 0;
-               }
-
-               pub fn process(&mut self, input: &[u8], output: &mut [u8]) {
-                       assert!(input.len() == output.len());
-                       let len = input.len();
-                       let mut i = 0;
-                       while i < len {
-                               // If there is no keystream available in the output buffer,
-                               // generate the next block.
-                               if self.offset == 64 {
-                                       self.update();
-                               }
-
-                               // Process the min(available keystream, remaining input length).
-                               let count = cmp::min(64 - self.offset, len - i);
-                               // explicitly assert lengths to avoid bounds checks:
-                               assert!(output.len() >= i + count);
-                               assert!(input.len() >= i + count);
-                               assert!(self.output.len() >= self.offset + count);
-                               for j in 0..count {
-                                       output[i + j] = input[i + j] ^ self.output[self.offset + j];
-                               }
-                               i += count;
-                               self.offset += count;
-                       }
-               }
-       }
-}
-#[cfg(not(feature = "fuzztarget"))]
-pub use self::real_chacha::ChaCha20;
-
-#[cfg(feature = "fuzztarget")]
-mod fuzzy_chacha {
-       pub struct ChaCha20 {}
-
-       impl ChaCha20 {
-               pub fn new(key: &[u8], nonce: &[u8]) -> ChaCha20 {
-                       assert!(key.len() == 16 || key.len() == 32);
-                       assert!(nonce.len() == 8 || nonce.len() == 12);
-                       Self {}
-               }
-
-               pub fn process(&mut self, input: &[u8], output: &mut [u8]) {
-                       output.copy_from_slice(input);
-               }
-       }
-}
-#[cfg(feature = "fuzztarget")]
-pub use self::fuzzy_chacha::ChaCha20;
-
-#[cfg(test)]
-mod test {
-       use std::iter::repeat;
-
-       use super::ChaCha20;
-
-       #[test]
-       fn test_chacha20_256_tls_vectors() {
-               struct TestVector {
-                       key:   [u8; 32],
-                       nonce: [u8; 8],
-                       keystream: Vec<u8>,
-               };
-               // taken from http://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-04
-               let test_vectors = vec!(
-                       TestVector{
-                               key: [
-                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                               ],
-                               nonce: [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ],
-                               keystream: vec!(
-                                       0x76, 0xb8, 0xe0, 0xad, 0xa0, 0xf1, 0x3d, 0x90,
-                                       0x40, 0x5d, 0x6a, 0xe5, 0x53, 0x86, 0xbd, 0x28,
-                                       0xbd, 0xd2, 0x19, 0xb8, 0xa0, 0x8d, 0xed, 0x1a,
-                                       0xa8, 0x36, 0xef, 0xcc, 0x8b, 0x77, 0x0d, 0xc7,
-                                       0xda, 0x41, 0x59, 0x7c, 0x51, 0x57, 0x48, 0x8d,
-                                       0x77, 0x24, 0xe0, 0x3f, 0xb8, 0xd8, 0x4a, 0x37,
-                                       0x6a, 0x43, 0xb8, 0xf4, 0x15, 0x18, 0xa1, 0x1c,
-                                       0xc3, 0x87, 0xb6, 0x69, 0xb2, 0xee, 0x65, 0x86,
-                               ),
-                       }, TestVector{
-                               key: [
-                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
-                               ],
-                               nonce: [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ],
-                               keystream: vec!(
-                                       0x45, 0x40, 0xf0, 0x5a, 0x9f, 0x1f, 0xb2, 0x96,
-                                       0xd7, 0x73, 0x6e, 0x7b, 0x20, 0x8e, 0x3c, 0x96,
-                                       0xeb, 0x4f, 0xe1, 0x83, 0x46, 0x88, 0xd2, 0x60,
-                                       0x4f, 0x45, 0x09, 0x52, 0xed, 0x43, 0x2d, 0x41,
-                                       0xbb, 0xe2, 0xa0, 0xb6, 0xea, 0x75, 0x66, 0xd2,
-                                       0xa5, 0xd1, 0xe7, 0xe2, 0x0d, 0x42, 0xaf, 0x2c,
-                                       0x53, 0xd7, 0x92, 0xb1, 0xc4, 0x3f, 0xea, 0x81,
-                                       0x7e, 0x9a, 0xd2, 0x75, 0xae, 0x54, 0x69, 0x63,
-                               ),
-                       }, TestVector{
-                               key: [
-                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                               ],
-                               nonce: [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 ],
-                               keystream: vec!(
-                                       0xde, 0x9c, 0xba, 0x7b, 0xf3, 0xd6, 0x9e, 0xf5,
-                                       0xe7, 0x86, 0xdc, 0x63, 0x97, 0x3f, 0x65, 0x3a,
-                                       0x0b, 0x49, 0xe0, 0x15, 0xad, 0xbf, 0xf7, 0x13,
-                                       0x4f, 0xcb, 0x7d, 0xf1, 0x37, 0x82, 0x10, 0x31,
-                                       0xe8, 0x5a, 0x05, 0x02, 0x78, 0xa7, 0x08, 0x45,
-                                       0x27, 0x21, 0x4f, 0x73, 0xef, 0xc7, 0xfa, 0x5b,
-                                       0x52, 0x77, 0x06, 0x2e, 0xb7, 0xa0, 0x43, 0x3e,
-                                       0x44, 0x5f, 0x41, 0xe3,
-                               ),
-                       }, TestVector{
-                               key: [
-                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                               ],
-                               nonce: [ 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ],
-                               keystream: vec!(
-                                       0xef, 0x3f, 0xdf, 0xd6, 0xc6, 0x15, 0x78, 0xfb,
-                                       0xf5, 0xcf, 0x35, 0xbd, 0x3d, 0xd3, 0x3b, 0x80,
-                                       0x09, 0x63, 0x16, 0x34, 0xd2, 0x1e, 0x42, 0xac,
-                                       0x33, 0x96, 0x0b, 0xd1, 0x38, 0xe5, 0x0d, 0x32,
-                                       0x11, 0x1e, 0x4c, 0xaf, 0x23, 0x7e, 0xe5, 0x3c,
-                                       0xa8, 0xad, 0x64, 0x26, 0x19, 0x4a, 0x88, 0x54,
-                                       0x5d, 0xdc, 0x49, 0x7a, 0x0b, 0x46, 0x6e, 0x7d,
-                                       0x6b, 0xbd, 0xb0, 0x04, 0x1b, 0x2f, 0x58, 0x6b,
-                               ),
-                       }, TestVector{
-                               key: [
-                                       0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
-                                       0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
-                                       0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
-                                       0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
-                               ],
-                               nonce: [ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 ],
-                               keystream: vec!(
-                                       0xf7, 0x98, 0xa1, 0x89, 0xf1, 0x95, 0xe6, 0x69,
-                                       0x82, 0x10, 0x5f, 0xfb, 0x64, 0x0b, 0xb7, 0x75,
-                                       0x7f, 0x57, 0x9d, 0xa3, 0x16, 0x02, 0xfc, 0x93,
-                                       0xec, 0x01, 0xac, 0x56, 0xf8, 0x5a, 0xc3, 0xc1,
-                                       0x34, 0xa4, 0x54, 0x7b, 0x73, 0x3b, 0x46, 0x41,
-                                       0x30, 0x42, 0xc9, 0x44, 0x00, 0x49, 0x17, 0x69,
-                                       0x05, 0xd3, 0xbe, 0x59, 0xea, 0x1c, 0x53, 0xf1,
-                                       0x59, 0x16, 0x15, 0x5c, 0x2b, 0xe8, 0x24, 0x1a,
-                                       0x38, 0x00, 0x8b, 0x9a, 0x26, 0xbc, 0x35, 0x94,
-                                       0x1e, 0x24, 0x44, 0x17, 0x7c, 0x8a, 0xde, 0x66,
-                                       0x89, 0xde, 0x95, 0x26, 0x49, 0x86, 0xd9, 0x58,
-                                       0x89, 0xfb, 0x60, 0xe8, 0x46, 0x29, 0xc9, 0xbd,
-                                       0x9a, 0x5a, 0xcb, 0x1c, 0xc1, 0x18, 0xbe, 0x56,
-                                       0x3e, 0xb9, 0xb3, 0xa4, 0xa4, 0x72, 0xf8, 0x2e,
-                                       0x09, 0xa7, 0xe7, 0x78, 0x49, 0x2b, 0x56, 0x2e,
-                                       0xf7, 0x13, 0x0e, 0x88, 0xdf, 0xe0, 0x31, 0xc7,
-                                       0x9d, 0xb9, 0xd4, 0xf7, 0xc7, 0xa8, 0x99, 0x15,
-                                       0x1b, 0x9a, 0x47, 0x50, 0x32, 0xb6, 0x3f, 0xc3,
-                                       0x85, 0x24, 0x5f, 0xe0, 0x54, 0xe3, 0xdd, 0x5a,
-                                       0x97, 0xa5, 0xf5, 0x76, 0xfe, 0x06, 0x40, 0x25,
-                                       0xd3, 0xce, 0x04, 0x2c, 0x56, 0x6a, 0xb2, 0xc5,
-                                       0x07, 0xb1, 0x38, 0xdb, 0x85, 0x3e, 0x3d, 0x69,
-                                       0x59, 0x66, 0x09, 0x96, 0x54, 0x6c, 0xc9, 0xc4,
-                                       0xa6, 0xea, 0xfd, 0xc7, 0x77, 0xc0, 0x40, 0xd7,
-                                       0x0e, 0xaf, 0x46, 0xf7, 0x6d, 0xad, 0x39, 0x79,
-                                       0xe5, 0xc5, 0x36, 0x0c, 0x33, 0x17, 0x16, 0x6a,
-                                       0x1c, 0x89, 0x4c, 0x94, 0xa3, 0x71, 0x87, 0x6a,
-                                       0x94, 0xdf, 0x76, 0x28, 0xfe, 0x4e, 0xaa, 0xf2,
-                                       0xcc, 0xb2, 0x7d, 0x5a, 0xaa, 0xe0, 0xad, 0x7a,
-                                       0xd0, 0xf9, 0xd4, 0xb6, 0xad, 0x3b, 0x54, 0x09,
-                                       0x87, 0x46, 0xd4, 0x52, 0x4d, 0x38, 0x40, 0x7a,
-                                       0x6d, 0xeb, 0x3a, 0xb7, 0x8f, 0xab, 0x78, 0xc9,
-                               ),
-                       },
-               );
-
-               for tv in test_vectors.iter() {
-                       let mut c = ChaCha20::new(&tv.key, &tv.nonce);
-                       let input: Vec<u8> = repeat(0).take(tv.keystream.len()).collect();
-                       let mut output: Vec<u8> = repeat(0).take(input.len()).collect();
-                       c.process(&input[..], &mut output[..]);
-                       assert_eq!(output, tv.keystream);
-               }
-       }
-
-       #[test]
-       fn test_chacha20_256_tls_vectors_96_nonce() {
-               struct TestVector {
-                       key:   [u8; 32],
-                       nonce: [u8; 12],
-                       keystream: Vec<u8>,
-               };
-               // taken from http://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-04
-               let test_vectors = vec!(
-                       TestVector{
-                               key: [
-                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                               ],
-                               nonce: [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ],
-                               keystream: vec!(
-                                       0x76, 0xb8, 0xe0, 0xad, 0xa0, 0xf1, 0x3d, 0x90,
-                                       0x40, 0x5d, 0x6a, 0xe5, 0x53, 0x86, 0xbd, 0x28,
-                                       0xbd, 0xd2, 0x19, 0xb8, 0xa0, 0x8d, 0xed, 0x1a,
-                                       0xa8, 0x36, 0xef, 0xcc, 0x8b, 0x77, 0x0d, 0xc7,
-                                       0xda, 0x41, 0x59, 0x7c, 0x51, 0x57, 0x48, 0x8d,
-                                       0x77, 0x24, 0xe0, 0x3f, 0xb8, 0xd8, 0x4a, 0x37,
-                                       0x6a, 0x43, 0xb8, 0xf4, 0x15, 0x18, 0xa1, 0x1c,
-                                       0xc3, 0x87, 0xb6, 0x69, 0xb2, 0xee, 0x65, 0x86,
-                               ),
-                       }, TestVector{
-                               key: [
-                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
-                               ],
-                               nonce: [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ],
-                               keystream: vec!(
-                                       0x45, 0x40, 0xf0, 0x5a, 0x9f, 0x1f, 0xb2, 0x96,
-                                       0xd7, 0x73, 0x6e, 0x7b, 0x20, 0x8e, 0x3c, 0x96,
-                                       0xeb, 0x4f, 0xe1, 0x83, 0x46, 0x88, 0xd2, 0x60,
-                                       0x4f, 0x45, 0x09, 0x52, 0xed, 0x43, 0x2d, 0x41,
-                                       0xbb, 0xe2, 0xa0, 0xb6, 0xea, 0x75, 0x66, 0xd2,
-                                       0xa5, 0xd1, 0xe7, 0xe2, 0x0d, 0x42, 0xaf, 0x2c,
-                                       0x53, 0xd7, 0x92, 0xb1, 0xc4, 0x3f, 0xea, 0x81,
-                                       0x7e, 0x9a, 0xd2, 0x75, 0xae, 0x54, 0x69, 0x63,
-                               ),
-                       }, TestVector{
-                               key: [
-                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                               ],
-                               nonce: [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 ],
-                               keystream: vec!(
-                                       0xde, 0x9c, 0xba, 0x7b, 0xf3, 0xd6, 0x9e, 0xf5,
-                                       0xe7, 0x86, 0xdc, 0x63, 0x97, 0x3f, 0x65, 0x3a,
-                                       0x0b, 0x49, 0xe0, 0x15, 0xad, 0xbf, 0xf7, 0x13,
-                                       0x4f, 0xcb, 0x7d, 0xf1, 0x37, 0x82, 0x10, 0x31,
-                                       0xe8, 0x5a, 0x05, 0x02, 0x78, 0xa7, 0x08, 0x45,
-                                       0x27, 0x21, 0x4f, 0x73, 0xef, 0xc7, 0xfa, 0x5b,
-                                       0x52, 0x77, 0x06, 0x2e, 0xb7, 0xa0, 0x43, 0x3e,
-                                       0x44, 0x5f, 0x41, 0xe3,
-                               ),
-                       }, TestVector{
-                               key: [
-                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                               ],
-                               nonce: [ 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ],
-                               keystream: vec!(
-                                       0xef, 0x3f, 0xdf, 0xd6, 0xc6, 0x15, 0x78, 0xfb,
-                                       0xf5, 0xcf, 0x35, 0xbd, 0x3d, 0xd3, 0x3b, 0x80,
-                                       0x09, 0x63, 0x16, 0x34, 0xd2, 0x1e, 0x42, 0xac,
-                                       0x33, 0x96, 0x0b, 0xd1, 0x38, 0xe5, 0x0d, 0x32,
-                                       0x11, 0x1e, 0x4c, 0xaf, 0x23, 0x7e, 0xe5, 0x3c,
-                                       0xa8, 0xad, 0x64, 0x26, 0x19, 0x4a, 0x88, 0x54,
-                                       0x5d, 0xdc, 0x49, 0x7a, 0x0b, 0x46, 0x6e, 0x7d,
-                                       0x6b, 0xbd, 0xb0, 0x04, 0x1b, 0x2f, 0x58, 0x6b,
-                               ),
-                       }, TestVector{
-                               key: [
-                                       0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
-                                       0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
-                                       0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
-                                       0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
-                               ],
-                               nonce: [0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 ],
-                               keystream: vec!(
-                                       0xf7, 0x98, 0xa1, 0x89, 0xf1, 0x95, 0xe6, 0x69,
-                                       0x82, 0x10, 0x5f, 0xfb, 0x64, 0x0b, 0xb7, 0x75,
-                                       0x7f, 0x57, 0x9d, 0xa3, 0x16, 0x02, 0xfc, 0x93,
-                                       0xec, 0x01, 0xac, 0x56, 0xf8, 0x5a, 0xc3, 0xc1,
-                                       0x34, 0xa4, 0x54, 0x7b, 0x73, 0x3b, 0x46, 0x41,
-                                       0x30, 0x42, 0xc9, 0x44, 0x00, 0x49, 0x17, 0x69,
-                                       0x05, 0xd3, 0xbe, 0x59, 0xea, 0x1c, 0x53, 0xf1,
-                                       0x59, 0x16, 0x15, 0x5c, 0x2b, 0xe8, 0x24, 0x1a,
-                                       0x38, 0x00, 0x8b, 0x9a, 0x26, 0xbc, 0x35, 0x94,
-                                       0x1e, 0x24, 0x44, 0x17, 0x7c, 0x8a, 0xde, 0x66,
-                                       0x89, 0xde, 0x95, 0x26, 0x49, 0x86, 0xd9, 0x58,
-                                       0x89, 0xfb, 0x60, 0xe8, 0x46, 0x29, 0xc9, 0xbd,
-                                       0x9a, 0x5a, 0xcb, 0x1c, 0xc1, 0x18, 0xbe, 0x56,
-                                       0x3e, 0xb9, 0xb3, 0xa4, 0xa4, 0x72, 0xf8, 0x2e,
-                                       0x09, 0xa7, 0xe7, 0x78, 0x49, 0x2b, 0x56, 0x2e,
-                                       0xf7, 0x13, 0x0e, 0x88, 0xdf, 0xe0, 0x31, 0xc7,
-                                       0x9d, 0xb9, 0xd4, 0xf7, 0xc7, 0xa8, 0x99, 0x15,
-                                       0x1b, 0x9a, 0x47, 0x50, 0x32, 0xb6, 0x3f, 0xc3,
-                                       0x85, 0x24, 0x5f, 0xe0, 0x54, 0xe3, 0xdd, 0x5a,
-                                       0x97, 0xa5, 0xf5, 0x76, 0xfe, 0x06, 0x40, 0x25,
-                                       0xd3, 0xce, 0x04, 0x2c, 0x56, 0x6a, 0xb2, 0xc5,
-                                       0x07, 0xb1, 0x38, 0xdb, 0x85, 0x3e, 0x3d, 0x69,
-                                       0x59, 0x66, 0x09, 0x96, 0x54, 0x6c, 0xc9, 0xc4,
-                                       0xa6, 0xea, 0xfd, 0xc7, 0x77, 0xc0, 0x40, 0xd7,
-                                       0x0e, 0xaf, 0x46, 0xf7, 0x6d, 0xad, 0x39, 0x79,
-                                       0xe5, 0xc5, 0x36, 0x0c, 0x33, 0x17, 0x16, 0x6a,
-                                       0x1c, 0x89, 0x4c, 0x94, 0xa3, 0x71, 0x87, 0x6a,
-                                       0x94, 0xdf, 0x76, 0x28, 0xfe, 0x4e, 0xaa, 0xf2,
-                                       0xcc, 0xb2, 0x7d, 0x5a, 0xaa, 0xe0, 0xad, 0x7a,
-                                       0xd0, 0xf9, 0xd4, 0xb6, 0xad, 0x3b, 0x54, 0x09,
-                                       0x87, 0x46, 0xd4, 0x52, 0x4d, 0x38, 0x40, 0x7a,
-                                       0x6d, 0xeb, 0x3a, 0xb7, 0x8f, 0xab, 0x78, 0xc9,
-                               ),
-                       },
-               );
-
-               for tv in test_vectors.iter() {
-                       let mut c = ChaCha20::new(&tv.key, &tv.nonce);
-                       let input: Vec<u8> = repeat(0).take(tv.keystream.len()).collect();
-                       let mut output: Vec<u8> = repeat(0).take(input.len()).collect();
-                       c.process(&input[..], &mut output[..]);
-                       assert_eq!(output, tv.keystream);
-               }
-       }
-}
diff --git a/src/util/chacha20poly1305rfc.rs b/src/util/chacha20poly1305rfc.rs
deleted file mode 100644 (file)
index 1d3af1e..0000000
+++ /dev/null
@@ -1,147 +0,0 @@
-// ring has a garbage API so its use is avoided, but rust-crypto doesn't have RFC-variant poly1305
-// Instead, we steal rust-crypto's implementation and tweak it to match the RFC.
-
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-// This is a port of Andrew Moons poly1305-donna
-// https://github.com/floodyberry/poly1305-donna
-
-#[cfg(not(feature = "fuzztarget"))]
-mod real_chachapoly {
-       use util::chacha20::ChaCha20;
-       use util::poly1305::Poly1305;
-       use bitcoin_hashes::cmp::fixed_time_eq;
-
-       use util::byte_utils;
-
-       #[derive(Clone, Copy)]
-       pub struct ChaCha20Poly1305RFC {
-               cipher: ChaCha20,
-               mac: Poly1305,
-               finished: bool,
-               data_len: usize,
-               aad_len: u64,
-       }
-
-       impl ChaCha20Poly1305RFC {
-               #[inline]
-               fn pad_mac_16(mac: &mut Poly1305, len: usize) {
-                       if len % 16 != 0 {
-                               mac.input(&[0; 16][0..16 - (len % 16)]);
-                       }
-               }
-               pub fn new(key: &[u8], nonce: &[u8], aad: &[u8]) -> ChaCha20Poly1305RFC {
-                       assert!(key.len() == 16 || key.len() == 32);
-                       assert!(nonce.len() == 12);
-
-                       // Ehh, I'm too lazy to *also* tweak ChaCha20 to make it RFC-compliant
-                       assert!(nonce[0] == 0 && nonce[1] == 0 && nonce[2] == 0 && nonce[3] == 0);
-
-                       let mut cipher = ChaCha20::new(key, &nonce[4..]);
-                       let mut mac_key = [0u8; 64];
-                       let zero_key = [0u8; 64];
-                       cipher.process(&zero_key, &mut mac_key);
-
-                       let mut mac = Poly1305::new(&mac_key[..32]);
-                       mac.input(aad);
-                       ChaCha20Poly1305RFC::pad_mac_16(&mut mac, aad.len());
-
-                       ChaCha20Poly1305RFC {
-                               cipher: cipher,
-                               mac: mac,
-                               finished: false,
-                               data_len: 0,
-                               aad_len: aad.len() as u64,
-                       }
-               }
-
-               pub fn encrypt(&mut self, input: &[u8], output: &mut [u8], out_tag: &mut [u8]) {
-                       assert!(input.len() == output.len());
-                       assert!(self.finished == false);
-                       self.cipher.process(input, output);
-                       self.data_len += input.len();
-                       self.mac.input(output);
-                       ChaCha20Poly1305RFC::pad_mac_16(&mut self.mac, self.data_len);
-                       self.finished = true;
-                       self.mac.input(&byte_utils::le64_to_array(self.aad_len));
-                       self.mac.input(&byte_utils::le64_to_array(self.data_len as u64));
-                       self.mac.raw_result(out_tag);
-               }
-
-               pub fn decrypt(&mut self, input: &[u8], output: &mut [u8], tag: &[u8]) -> bool {
-                       assert!(input.len() == output.len());
-                       assert!(self.finished == false);
-
-                       self.finished = true;
-
-                       self.mac.input(input);
-
-                       self.data_len += input.len();
-                       ChaCha20Poly1305RFC::pad_mac_16(&mut self.mac, self.data_len);
-                       self.mac.input(&byte_utils::le64_to_array(self.aad_len));
-                       self.mac.input(&byte_utils::le64_to_array(self.data_len as u64));
-
-                       let mut calc_tag =  [0u8; 16];
-                       self.mac.raw_result(&mut calc_tag);
-                       if fixed_time_eq(&calc_tag, tag) {
-                               self.cipher.process(input, output);
-                               true
-                       } else {
-                               false
-                       }
-               }
-       }
-}
-#[cfg(not(feature = "fuzztarget"))]
-pub use self::real_chachapoly::ChaCha20Poly1305RFC;
-
-#[cfg(feature = "fuzztarget")]
-mod fuzzy_chachapoly {
-       #[derive(Clone, Copy)]
-       pub struct ChaCha20Poly1305RFC {
-               tag: [u8; 16],
-               finished: bool,
-       }
-       impl ChaCha20Poly1305RFC {
-               pub fn new(key: &[u8], nonce: &[u8], _aad: &[u8]) -> ChaCha20Poly1305RFC {
-                       assert!(key.len() == 16 || key.len() == 32);
-                       assert!(nonce.len() == 12);
-
-                       // Ehh, I'm too lazy to *also* tweak ChaCha20 to make it RFC-compliant
-                       assert!(nonce[0] == 0 && nonce[1] == 0 && nonce[2] == 0 && nonce[3] == 0);
-
-                       let mut tag = [0; 16];
-                       tag.copy_from_slice(&key[0..16]);
-
-                       ChaCha20Poly1305RFC {
-                               tag,
-                               finished: false,
-                       }
-               }
-
-               pub fn encrypt(&mut self, input: &[u8], output: &mut [u8], out_tag: &mut [u8]) {
-                       assert!(input.len() == output.len());
-                       assert!(self.finished == false);
-
-                       output.copy_from_slice(&input);
-                       out_tag.copy_from_slice(&self.tag);
-                       self.finished = true;
-               }
-
-               pub fn decrypt(&mut self, input: &[u8], output: &mut [u8], tag: &[u8]) -> bool {
-                       assert!(input.len() == output.len());
-                       assert!(self.finished == false);
-
-                       if tag[..] != self.tag[..] { return false; }
-                       output.copy_from_slice(input);
-                       self.finished = true;
-                       true
-               }
-       }
-}
-#[cfg(feature = "fuzztarget")]
-pub use self::fuzzy_chachapoly::ChaCha20Poly1305RFC;
diff --git a/src/util/config.rs b/src/util/config.rs
deleted file mode 100644 (file)
index 0b61a56..0000000
+++ /dev/null
@@ -1,183 +0,0 @@
-//! Various user-configurable channel limits and settings which ChannelManager
-//! applies for you.
-
-use ln::channelmanager::{BREAKDOWN_TIMEOUT, MAX_LOCAL_BREAKDOWN_TIMEOUT};
-
-/// Top-level config which holds ChannelHandshakeLimits and ChannelConfig.
-#[derive(Clone, Debug)]
-pub struct UserConfig {
-       /// Channel config that we propose to our counterparty.
-       pub own_channel_config: ChannelHandshakeConfig,
-       /// Limits applied to our counterparty's proposed channel config settings.
-       pub peer_channel_config_limits: ChannelHandshakeLimits,
-       /// Channel config which affects behavior during channel lifetime.
-       pub channel_options: ChannelConfig,
-}
-
-impl UserConfig {
-       /// Provides sane defaults for most configurations (but with 0 relay fees!)
-       pub fn new() -> Self{
-               UserConfig {
-                       own_channel_config: ChannelHandshakeConfig::new(),
-                       peer_channel_config_limits: ChannelHandshakeLimits::new(),
-                       channel_options: ChannelConfig::new(),
-               }
-       }
-}
-
-/// Configuration we set when applicable.
-#[derive(Clone, Debug)]
-pub struct ChannelHandshakeConfig {
-       /// Confirmations we will wait for before considering the channel locked in.
-       /// Applied only for inbound channels (see ChannelHandshakeLimits::max_minimum_depth for the
-       /// equivalent limit applied to outbound channels).
-       pub minimum_depth: u32,
-       /// Set to the amount of time we require our counterparty to wait to claim their money.
-       ///
-       /// It's one of the main parameter of our security model. We (or one of our watchtowers) MUST
-       /// be online to check for peer having broadcast a revoked transaction to steal our funds
-       /// at least once every our_to_self_delay blocks.
-       /// Default is BREAKDOWN_TIMEOUT, we enforce it as a minimum at channel opening so you can
-       /// tweak config to ask for more security, not less.
-       ///
-       /// Meanwhile, asking for a too high delay, we bother peer to freeze funds for nothing in
-       /// case of an honest unilateral channel close, which implicitly decrease the economic value of
-       /// our channel.
-       pub our_to_self_delay: u16,
-}
-
-impl ChannelHandshakeConfig {
-       /// Provides sane defaults for `ChannelHandshakeConfig`
-       pub fn new() -> ChannelHandshakeConfig {
-               ChannelHandshakeConfig {
-                       minimum_depth: 6,
-                       our_to_self_delay: BREAKDOWN_TIMEOUT,
-               }
-       }
-}
-
-/// Optional channel limits which are applied during channel creation.
-///
-/// These limits are only applied to our counterparty's limits, not our own.
-///
-/// Use 0/<type>::max_value() as appropriate to skip checking.
-#[derive(Copy, Clone, Debug)]
-pub struct ChannelHandshakeLimits {
-       /// Minimum allowed satoshis when a channel is funded, this is supplied by the sender and so
-       /// only applies to inbound channels.
-       pub min_funding_satoshis: u64,
-       /// The remote node sets a limit on the minimum size of HTLCs we can send to them. This allows
-       /// you to limit the maximum minimum-size they can require.
-       pub max_htlc_minimum_msat: u64,
-       /// The remote node sets a limit on the maximum value of pending HTLCs to them at any given
-       /// time to limit their funds exposure to HTLCs. This allows you to set a minimum such value.
-       pub min_max_htlc_value_in_flight_msat: u64,
-       /// The remote node will require we keep a certain amount in direct payment to ourselves at all
-       /// time, ensuring that we are able to be punished if we broadcast an old state. This allows to
-       /// you limit the amount which we will have to keep to ourselves (and cannot use for HTLCs).
-       pub max_channel_reserve_satoshis: u64,
-       /// The remote node sets a limit on the maximum number of pending HTLCs to them at any given
-       /// time. This allows you to set a minimum such value.
-       pub min_max_accepted_htlcs: u16,
-       /// Outputs below a certain value will not be added to on-chain transactions. The dust value is
-       /// required to always be higher than this value so this only applies to HTLC outputs (and
-       /// potentially to-self outputs before any payments have been made).
-       /// Thus, HTLCs below this amount plus HTLC transaction fees are not enforceable on-chain.
-       /// This setting allows you to set a minimum dust limit for their commitment transactions,
-       /// reflecting the reality that tiny outputs are not considered standard transactions and will
-       /// not propagate through the Bitcoin network.
-       /// Defaults to 546, or the current dust limit on the Bitcoin network.
-       pub min_dust_limit_satoshis: u64,
-       /// Maximum allowed threshold above which outputs will not be generated in their commitment
-       /// transactions.
-       /// HTLCs below this amount plus HTLC transaction fees are not enforceable on-chain.
-       pub max_dust_limit_satoshis: u64,
-       /// Before a channel is usable the funding transaction will need to be confirmed by at least a
-       /// certain number of blocks, specified by the node which is not the funder (as the funder can
-       /// assume they aren't going to double-spend themselves).
-       /// This config allows you to set a limit on the maximum amount of time to wait. Defaults to
-       /// 144 blocks or roughly one day and only applies to outbound channels.
-       pub max_minimum_depth: u32,
-       /// Set to force the incoming channel to match our announced channel preference in
-       /// ChannelConfig.
-       /// Defaults to true to make the default that no announced channels are possible (which is
-       /// appropriate for any nodes which are not online very reliably).
-       pub force_announced_channel_preference: bool,
-       /// Set to the amount of time we're willing to wait to claim money back to us.
-       ///
-       /// Not checking this value would be a security issue, as our peer would be able to set it to
-       /// max relative lock-time (a year) and we would "lose" money as it would be locked for a long time.
-       /// Default is MAX_LOCAL_BREAKDOWN_TIMEOUT, which we also enforce as a maximum value
-       /// so you can tweak config to reduce the loss of having useless locked funds (if your peer accepts)
-       pub their_to_self_delay: u16
-}
-
-impl ChannelHandshakeLimits {
-       /// Provides sane defaults for most configurations.
-       ///
-       /// Most additional limits are disabled except those with which specify a default in individual
-       /// field documentation. Note that this may result in barely-usable channels, but since they
-       /// are applied mostly only to incoming channels that's not much of a problem.
-       pub fn new() -> Self {
-               ChannelHandshakeLimits {
-                       min_funding_satoshis: 0,
-                       max_htlc_minimum_msat: <u64>::max_value(),
-                       min_max_htlc_value_in_flight_msat: 0,
-                       max_channel_reserve_satoshis: <u64>::max_value(),
-                       min_max_accepted_htlcs: 0,
-                       min_dust_limit_satoshis: 546,
-                       max_dust_limit_satoshis: <u64>::max_value(),
-                       max_minimum_depth: 144,
-                       force_announced_channel_preference: true,
-                       their_to_self_delay: MAX_LOCAL_BREAKDOWN_TIMEOUT,
-               }
-       }
-}
-
-/// Options which apply on a per-channel basis and may change at runtime or based on negotiation
-/// with our counterparty.
-#[derive(Copy, Clone, Debug)]
-pub struct ChannelConfig {
-       /// Amount (in millionths of a satoshi) the channel will charge per transferred satoshi.
-       /// This may be allowed to change at runtime in a later update, however doing so must result in
-       /// update messages sent to notify all nodes of our updated relay fee.
-       pub fee_proportional_millionths: u32,
-       /// Set to announce the channel publicly and notify all nodes that they can route via this
-       /// channel.
-       ///
-       /// This should only be set to true for nodes which expect to be online reliably.
-       ///
-       /// As the node which funds a channel picks this value this will only apply for new outbound
-       /// channels unless ChannelHandshakeLimits::force_announced_channel_preferences is set.
-       ///
-       /// This cannot be changed after the initial channel handshake.
-       pub announced_channel: bool,
-       /// When set, we commit to an upfront shutdown_pubkey at channel open. If our counterparty
-       /// supports it, they will then enforce the mutual-close output to us matches what we provided
-       /// at intialization, preventing us from closing to an alternate pubkey.
-       ///
-       /// This is set to true by default to provide a slight increase in security, though ultimately
-       /// any attacker who is able to take control of a channel can just as easily send the funds via
-       /// lightning payments, so we never require that our counterparties support this option.
-       ///
-       /// This cannot be changed after a channel has been initialized.
-       pub commit_upfront_shutdown_pubkey: bool
-}
-
-impl ChannelConfig {
-       /// Provides sane defaults for most configurations (but with zero relay fees!).
-       pub fn new() -> Self {
-               ChannelConfig {
-                       fee_proportional_millionths: 0,
-                       announced_channel: false,
-                       commit_upfront_shutdown_pubkey: true,
-               }
-       }
-}
-
-//Add write and readable traits to channelconfig
-impl_writeable!(ChannelConfig, 8+1+1, {
-       fee_proportional_millionths,
-       announced_channel,
-       commit_upfront_shutdown_pubkey
-});
diff --git a/src/util/errors.rs b/src/util/errors.rs
deleted file mode 100644 (file)
index 27f8377..0000000
+++ /dev/null
@@ -1,96 +0,0 @@
-//! Error types live here.
-
-use std::fmt;
-
-/// Indicates an error on the client's part (usually some variant of attempting to use too-low or
-/// too-high values)
-pub enum APIError {
-       /// Indicates the API was wholly misused (see err for more). Cases where these can be returned
-       /// are documented, but generally indicates some precondition of a function was violated.
-       APIMisuseError {
-               /// A human-readable error message
-               err: &'static str
-       },
-       /// Due to a high feerate, we were unable to complete the request.
-       /// For example, this may be returned if the feerate implies we cannot open a channel at the
-       /// requested value, but opening a larger channel would succeed.
-       FeeRateTooHigh {
-               /// A human-readable error message
-               err: String,
-               /// The feerate which was too high.
-               feerate: u64
-       },
-       /// A malformed Route was provided (eg overflowed value, node id mismatch, overly-looped route,
-       /// too-many-hops, etc).
-       RouteError {
-               /// A human-readable error message
-               err: &'static str
-       },
-       /// We were unable to complete the request as the Channel required to do so is unable to
-       /// complete the request (or was not found). This can take many forms, including disconnected
-       /// peer, channel at capacity, channel shutting down, etc.
-       ChannelUnavailable {
-               /// A human-readable error message
-               err: &'static str
-       },
-       /// An attempt to call add_update_monitor returned an Err (ie you did this!), causing the
-       /// attempted action to fail.
-       MonitorUpdateFailed,
-}
-
-impl fmt::Debug for APIError {
-       fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-               match *self {
-                       APIError::APIMisuseError {ref err} => f.write_str(err),
-                       APIError::FeeRateTooHigh {ref err, ref feerate} => write!(f, "{} feerate: {}", err, feerate),
-                       APIError::RouteError {ref err} => f.write_str(err),
-                       APIError::ChannelUnavailable {ref err} => f.write_str(err),
-                       APIError::MonitorUpdateFailed => f.write_str("Client indicated a channel monitor update failed"),
-               }
-       }
-}
-
-#[inline]
-pub(crate) fn get_onion_debug_field(error_code: u16) -> (&'static str, usize) {
-       match error_code & 0xff {
-               4|5|6 => ("sha256_of_onion", 32),
-               11|12 => ("htlc_msat", 8),
-               13|18 => ("cltv_expiry", 4),
-               19 => ("incoming_htlc_msat", 8),
-               20 => ("flags", 2),
-               _ => ("", 0),
-       }
-}
-
-#[inline]
-pub(crate) fn get_onion_error_description(error_code: u16) -> (&'static str, &'static str) {
-       const BADONION: u16 = 0x8000;
-       const PERM: u16 = 0x4000;
-       const NODE: u16 = 0x2000;
-       const UPDATE: u16 = 0x1000;
-       match error_code {
-               _c if _c == PERM|1 => ("The realm byte was not understood by the processing node", "invalid_realm"),
-               _c if _c == NODE|2 => ("Node indicated temporary node failure", "temporary_node_failure"),
-               _c if _c == PERM|NODE|2 => ("Node indicated permanent node failure", "permanent_node_failure"),
-               _c if _c == PERM|NODE|3 => ("Node indicated the required node feature is missing in the onion", "required_node_feature_missing"),
-               _c if _c == BADONION|PERM|4 => ("Node indicated the version by is not understood", "invalid_onion_version"),
-               _c if _c == BADONION|PERM|5  => ("Node indicated the HMAC of the onion is incorrect", "invalid_onion_hmac"),
-               _c if _c == BADONION|PERM|6 => ("Node indicated the ephemeral public keys is not parseable", "invalid_onion_key"),
-               _c if _c == UPDATE|7 => ("Node indicated the outgoing channel is unable to handle the HTLC temporarily", "temporary_channel_failure"),
-               _c if _c == PERM|8 => ("Node indicated the outgoing channel is unable to handle the HTLC peramanently", "permanent_channel_failure"),
-               _c if _c == PERM|9 => ("Node indicated the required feature for the outgoing channel is not satisfied", "required_channel_feature_missing"),
-               _c if _c == PERM|10 => ("Node indicated the outbound channel is not found for the specified short_channel_id in the onion packet", "unknown_next_peer"),
-               _c if _c == UPDATE|11 => ("Node indicated the HTLC amount was below the required minmum for the outbound channel", "amount_below_minimum"),
-               _c if _c == UPDATE|12 => ("Node indicated the fee amount does not meet the required level", "fee_insufficient"),
-               _c if _c == UPDATE|13 => ("Node indicated the cltv_expiry does not comply with the cltv_expiry_delta required by the outgoing channel", "incorrect_cltv_expiry"),
-               _c if _c == UPDATE|14 => ("Node indicated the CLTV expiry too close to the current block height for safe handling", "expiry_too_soon"),
-               _c if _c == PERM|15 => ("The final node indicated the payment hash is unknown or amount is incorrect", "incorrect_or_unknown_payment_details"),
-               _c if _c == PERM|16 => ("The final node indicated the payment amount is incorrect", "incorrect_payment_amount"),
-               _c if _c == 17 => ("The final node indicated the CLTV expiry is too close to the current block height for safe handling", "final_expiry_too_soon"),
-               _c if _c == 18 => ("The final node indicated the CLTV expiry in the HTLC does not match the value in the onion", "final_incorrect_cltv_expiry"),
-               _c if _c == 19 => ("The final node indicated the amount in the HTLC does not match the value in the onion", "final_incorrect_htlc_amount"),
-               _c if _c == UPDATE|20 => ("Node indicated the outbound channel has been disabled", "channel_disabled"),
-               _c if _c == 21 => ("Node indicated the CLTV expiry in the HTLC is too far in the future", "expiry_too_far"),
-               _ => ("Unknown", ""),
-       }
-}
diff --git a/src/util/events.rs b/src/util/events.rs
deleted file mode 100644 (file)
index 96a5b48..0000000
+++ /dev/null
@@ -1,235 +0,0 @@
-//! Events are returned from various bits in the library which indicate some action must be taken
-//! by the client.
-//!
-//! Because we don't have a built-in runtime, it's up to the client to call events at a time in the
-//! future, as well as generate and broadcast funding transactions handle payment preimages and a
-//! few other things.
-//!
-//! Note that many events are handled for you by PeerHandler, so in the common design of having a
-//! PeerManager which marshalls messages to ChannelManager and Router you only need to call
-//! process_events on the PeerHandler and then get_and_clear_pending_events and handle the events
-//! that bubble up to the surface. If, however, you do not have a PeerHandler managing a
-//! ChannelManager you need to handle all of the events which may be generated.
-//TODO: We need better separation of event types ^
-
-use ln::msgs;
-use ln::channelmanager::{PaymentPreimage, PaymentHash};
-use chain::transaction::OutPoint;
-use chain::keysinterface::SpendableOutputDescriptor;
-
-use bitcoin::blockdata::script::Script;
-
-use secp256k1::key::PublicKey;
-
-use std::time::Duration;
-
-/// An Event which you should probably take some action in response to.
-pub enum Event {
-       /// Used to indicate that the client should generate a funding transaction with the given
-       /// parameters and then call ChannelManager::funding_transaction_generated.
-       /// Generated in ChannelManager message handling.
-       /// Note that *all inputs* in the funding transaction must spend SegWit outputs or your
-       /// counterparty can steal your funds!
-       FundingGenerationReady {
-               /// The random channel_id we picked which you'll need to pass into
-               /// ChannelManager::funding_transaction_generated.
-               temporary_channel_id: [u8; 32],
-               /// The value, in satoshis, that the output should have.
-               channel_value_satoshis: u64,
-               /// The script which should be used in the transaction output.
-               output_script: Script,
-               /// The value passed in to ChannelManager::create_channel
-               user_channel_id: u64,
-       },
-       /// Used to indicate that the client may now broadcast the funding transaction it created for a
-       /// channel. Broadcasting such a transaction prior to this event may lead to our counterparty
-       /// trivially stealing all funds in the funding transaction!
-       FundingBroadcastSafe {
-               /// The output, which was passed to ChannelManager::funding_transaction_generated, which is
-               /// now safe to broadcast.
-               funding_txo: OutPoint,
-               /// The value passed in to ChannelManager::create_channel
-               user_channel_id: u64,
-       },
-       /// Indicates we've received money! Just gotta dig out that payment preimage and feed it to
-       /// ChannelManager::claim_funds to get it....
-       /// Note that if the preimage is not known or the amount paid is incorrect, you must call
-       /// ChannelManager::fail_htlc_backwards to free up resources for this HTLC.
-       /// The amount paid should be considered 'incorrect' when it is less than or more than twice
-       /// the amount expected.
-       PaymentReceived {
-               /// The hash for which the preimage should be handed to the ChannelManager.
-               payment_hash: PaymentHash,
-               /// The value, in thousandths of a satoshi, that this payment is for. Note that you must
-               /// compare this to the expected value before accepting the payment (as otherwise you are
-               /// providing proof-of-payment for less than the value you expected!).
-               amt: u64,
-       },
-       /// Indicates an outbound payment we made succeeded (ie it made it all the way to its target
-       /// and we got back the payment preimage for it).
-       /// Note that duplicative PaymentSent Events may be generated - it is your responsibility to
-       /// deduplicate them by payment_preimage (which MUST be unique)!
-       PaymentSent {
-               /// The preimage to the hash given to ChannelManager::send_payment.
-               /// Note that this serves as a payment receipt, if you wish to have such a thing, you must
-               /// store it somehow!
-               payment_preimage: PaymentPreimage,
-       },
-       /// Indicates an outbound payment we made failed. Probably some intermediary node dropped
-       /// something. You may wish to retry with a different route.
-       /// Note that duplicative PaymentFailed Events may be generated - it is your responsibility to
-       /// deduplicate them by payment_hash (which MUST be unique)!
-       PaymentFailed {
-               /// The hash which was given to ChannelManager::send_payment.
-               payment_hash: PaymentHash,
-               /// Indicates the payment was rejected for some reason by the recipient. This implies that
-               /// the payment has failed, not just the route in question. If this is not set, you may
-               /// retry the payment via a different route.
-               rejected_by_dest: bool,
-#[cfg(test)]
-               error_code: Option<u16>,
-       },
-       /// Used to indicate that ChannelManager::process_pending_htlc_forwards should be called at a
-       /// time in the future.
-       PendingHTLCsForwardable {
-               /// The minimum amount of time that should be waited prior to calling
-               /// process_pending_htlc_forwards. To increase the effort required to correlate payments,
-               /// you should wait a random amount of time in roughly the range (now + time_forwardable,
-               /// now + 5*time_forwardable).
-               time_forwardable: Duration,
-       },
-       /// Used to indicate that an output was generated on-chain which you should know how to spend.
-       /// Such an output will *not* ever be spent by rust-lightning, so you need to store them
-       /// somewhere and spend them when you create on-chain spends.
-       SpendableOutputs {
-               /// The outputs which you should store as spendable by you.
-               outputs: Vec<SpendableOutputDescriptor>,
-       },
-}
-
-/// An event generated by ChannelManager which indicates a message should be sent to a peer (or
-/// broadcast to most peers).
-/// These events are handled by PeerManager::process_events if you are using a PeerManager.
-#[derive(Clone)]
-pub enum MessageSendEvent {
-       /// Used to indicate that we've accepted a channel open and should send the accept_channel
-       /// message provided to the given peer.
-       SendAcceptChannel {
-               /// The node_id of the node which should receive this message
-               node_id: PublicKey,
-               /// The message which should be sent.
-               msg: msgs::AcceptChannel,
-       },
-       /// Used to indicate that we've initiated a channel open and should send the open_channel
-       /// message provided to the given peer.
-       SendOpenChannel {
-               /// The node_id of the node which should receive this message
-               node_id: PublicKey,
-               /// The message which should be sent.
-               msg: msgs::OpenChannel,
-       },
-       /// Used to indicate that a funding_created message should be sent to the peer with the given node_id.
-       SendFundingCreated {
-               /// The node_id of the node which should receive this message
-               node_id: PublicKey,
-               /// The message which should be sent.
-               msg: msgs::FundingCreated,
-       },
-       /// Used to indicate that a funding_signed message should be sent to the peer with the given node_id.
-       SendFundingSigned {
-               /// The node_id of the node which should receive this message
-               node_id: PublicKey,
-               /// The message which should be sent.
-               msg: msgs::FundingSigned,
-       },
-       /// Used to indicate that a funding_locked message should be sent to the peer with the given node_id.
-       SendFundingLocked {
-               /// The node_id of the node which should receive these message(s)
-               node_id: PublicKey,
-               /// The funding_locked message which should be sent.
-               msg: msgs::FundingLocked,
-       },
-       /// Used to indicate that an announcement_signatures message should be sent to the peer with the given node_id.
-       SendAnnouncementSignatures {
-               /// The node_id of the node which should receive these message(s)
-               node_id: PublicKey,
-               /// The announcement_signatures message which should be sent.
-               msg: msgs::AnnouncementSignatures,
-       },
-       /// Used to indicate that a series of HTLC update messages, as well as a commitment_signed
-       /// message should be sent to the peer with the given node_id.
-       UpdateHTLCs {
-               /// The node_id of the node which should receive these message(s)
-               node_id: PublicKey,
-               /// The update messages which should be sent. ALL messages in the struct should be sent!
-               updates: msgs::CommitmentUpdate,
-       },
-       /// Used to indicate that a revoke_and_ack message should be sent to the peer with the given node_id.
-       SendRevokeAndACK {
-               /// The node_id of the node which should receive this message
-               node_id: PublicKey,
-               /// The message which should be sent.
-               msg: msgs::RevokeAndACK,
-       },
-       /// Used to indicate that a closing_signed message should be sent to the peer with the given node_id.
-       SendClosingSigned {
-               /// The node_id of the node which should receive this message
-               node_id: PublicKey,
-               /// The message which should be sent.
-               msg: msgs::ClosingSigned,
-       },
-       /// Used to indicate that a shutdown message should be sent to the peer with the given node_id.
-       SendShutdown {
-               /// The node_id of the node which should receive this message
-               node_id: PublicKey,
-               /// The message which should be sent.
-               msg: msgs::Shutdown,
-       },
-       /// Used to indicate that a channel_reestablish message should be sent to the peer with the given node_id.
-       SendChannelReestablish {
-               /// The node_id of the node which should receive this message
-               node_id: PublicKey,
-               /// The message which should be sent.
-               msg: msgs::ChannelReestablish,
-       },
-       /// Used to indicate that a channel_announcement and channel_update should be broadcast to all
-       /// peers (except the peer with node_id either msg.contents.node_id_1 or msg.contents.node_id_2).
-       BroadcastChannelAnnouncement {
-               /// The channel_announcement which should be sent.
-               msg: msgs::ChannelAnnouncement,
-               /// The followup channel_update which should be sent.
-               update_msg: msgs::ChannelUpdate,
-       },
-       /// Used to indicate that a channel_update should be broadcast to all peers.
-       BroadcastChannelUpdate {
-               /// The channel_update which should be sent.
-               msg: msgs::ChannelUpdate,
-       },
-       /// Broadcast an error downstream to be handled
-       HandleError {
-               /// The node_id of the node which should receive this message
-               node_id: PublicKey,
-               /// The action which should be taken.
-               action: Option<msgs::ErrorAction>
-       },
-       /// When a payment fails we may receive updates back from the hop where it failed. In such
-       /// cases this event is generated so that we can inform the router of this information.
-       PaymentFailureNetworkUpdate {
-               /// The channel/node update which should be sent to router
-               update: msgs::HTLCFailChannelUpdate,
-       }
-}
-
-/// A trait indicating an object may generate message send events
-pub trait MessageSendEventsProvider {
-       /// Gets the list of pending events which were generated by previous actions, clearing the list
-       /// in the process.
-       fn get_and_clear_pending_msg_events(&self) -> Vec<MessageSendEvent>;
-}
-
-/// A trait indicating an object may generate events
-pub trait EventsProvider {
-       /// Gets the list of pending events which were generated by previous actions, clearing the list
-       /// in the process.
-       fn get_and_clear_pending_events(&self) -> Vec<Event>;
-}
diff --git a/src/util/fuzz_wrappers.rs b/src/util/fuzz_wrappers.rs
deleted file mode 100644 (file)
index 508acf0..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-macro_rules! hash_to_message {
-       ($slice: expr) => {
-               {
-                       #[cfg(not(feature = "fuzztarget"))]
-                       {
-                               ::secp256k1::Message::from_slice($slice).unwrap()
-                       }
-                       #[cfg(feature = "fuzztarget")]
-                       {
-                               match ::secp256k1::Message::from_slice($slice) {
-                                       Ok(msg) => msg,
-                                       Err(_) => ::secp256k1::Message::from_slice(&[1; 32]).unwrap()
-                               }
-                       }
-               }
-       }
-}
diff --git a/src/util/logger.rs b/src/util/logger.rs
deleted file mode 100644 (file)
index e727723..0000000
+++ /dev/null
@@ -1,167 +0,0 @@
-// Pruned copy of crate rust log, without global logger
-// https://github.com/rust-lang-nursery/log #7a60286
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-//! Log traits live here, which are called throughout the library to provide useful information for
-//! debugging purposes.
-//!
-//! There is currently 2 ways to filter log messages. First one, by using compilation features, e.g "max_level_off".
-//! The second one, client-side by implementing check against Record Level field.
-//! Each module may have its own Logger or share one.
-
-use std::cmp;
-use std::fmt;
-use std::sync::Arc;
-
-static LOG_LEVEL_NAMES: [&'static str; 6] = ["OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE"];
-
-/// An enum representing the available verbosity levels of the logger.
-#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
-pub enum Level {
-       ///Designates logger being silent
-       Off,
-       /// Designates very serious errors
-       Error,
-       /// Designates hazardous situations
-       Warn,
-       /// Designates useful information
-       Info,
-       /// Designates lower priority information
-       Debug,
-       /// Designates very low priority, often extremely verbose, information
-       Trace,
-}
-
-impl PartialOrd for Level {
-       #[inline]
-       fn partial_cmp(&self, other: &Level) -> Option<cmp::Ordering> {
-               Some(self.cmp(other))
-       }
-
-       #[inline]
-       fn lt(&self, other: &Level) -> bool {
-               (*self as usize) < *other as usize
-       }
-
-       #[inline]
-       fn le(&self, other: &Level) -> bool {
-               *self as usize <= *other as usize
-       }
-
-       #[inline]
-       fn gt(&self, other: &Level) -> bool {
-               *self as usize > *other as usize
-       }
-
-       #[inline]
-       fn ge(&self, other: &Level) -> bool {
-               *self as usize >= *other as usize
-       }
-}
-
-impl Ord for Level {
-       #[inline]
-       fn cmp(&self, other: &Level) -> cmp::Ordering {
-               (*self as usize).cmp(&(*other as usize))
-       }
-}
-
-impl fmt::Display for Level {
-       fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
-               fmt.pad(LOG_LEVEL_NAMES[*self as usize])
-       }
-}
-
-impl Level {
-       /// Returns the most verbose logging level.
-       #[inline]
-       pub fn max() -> Level {
-               Level::Trace
-       }
-}
-
-/// A Record, unit of logging output with Metadata to enable filtering
-/// Module_path, file, line to inform on log's source
-#[derive(Clone,Debug)]
-pub struct Record<'a> {
-       /// The verbosity level of the message.
-       pub level: Level,
-       /// The message body.
-       pub args: fmt::Arguments<'a>,
-       /// The module path of the message.
-       pub module_path: &'a str,
-       /// The source file containing the message.
-       pub file: &'a str,
-       /// The line containing the message.
-       pub line: u32,
-}
-
-impl<'a> Record<'a> {
-       /// Returns a new Record.
-       #[inline]
-       pub fn new(level: Level, args: fmt::Arguments<'a>, module_path: &'a str, file: &'a str, line: u32) -> Record<'a> {
-               Record {
-                       level,
-                       args,
-                       module_path,
-                       file,
-                       line
-               }
-       }
-}
-
-/// A trait encapsulating the operations required of a logger
-pub trait Logger: Sync + Send {
-       /// Logs the `Record`
-       fn log(&self, record: &Record);
-}
-
-pub(crate) struct LogHolder<'a> { pub(crate) logger: &'a Arc<Logger> }
-
-#[cfg(test)]
-mod tests {
-       use util::logger::{Logger, Level};
-       use util::test_utils::TestLogger;
-       use std::sync::{Arc};
-
-       #[test]
-       fn test_level_show() {
-               assert_eq!("INFO", Level::Info.to_string());
-               assert_eq!("ERROR", Level::Error.to_string());
-               assert_ne!("WARN", Level::Error.to_string());
-       }
-
-       struct WrapperLog {
-               logger: Arc<Logger>
-       }
-
-       impl WrapperLog {
-               fn new(logger: Arc<Logger>) -> WrapperLog {
-                       WrapperLog {
-                               logger,
-                       }
-               }
-
-               fn call_macros(&self) {
-                       log_error!(self, "This is an error");
-                       log_warn!(self, "This is a warning");
-                       log_info!(self, "This is an info");
-                       log_debug!(self, "This is a debug");
-                       log_trace!(self, "This is a trace");
-               }
-       }
-
-       #[test]
-       fn test_logging_macros() {
-               let mut logger = TestLogger::new();
-               logger.enable(Level::Trace);
-               let logger : Arc<Logger> = Arc::new(logger);
-               let wrapper = WrapperLog::new(Arc::clone(&logger));
-               wrapper.call_macros();
-       }
-}
diff --git a/src/util/macro_logger.rs b/src/util/macro_logger.rs
deleted file mode 100644 (file)
index 1f68542..0000000
+++ /dev/null
@@ -1,131 +0,0 @@
-use chain::transaction::OutPoint;
-
-use bitcoin_hashes::sha256d::Hash as Sha256dHash;
-use secp256k1::key::PublicKey;
-
-use ln::router::Route;
-
-use std;
-
-pub(crate) struct DebugPubKey<'a>(pub &'a PublicKey);
-impl<'a> std::fmt::Display for DebugPubKey<'a> {
-       fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
-               for i in self.0.serialize().iter() {
-                       write!(f, "{:02x}", i)?;
-               }
-               Ok(())
-       }
-}
-macro_rules! log_pubkey {
-       ($obj: expr) => {
-               ::util::macro_logger::DebugPubKey(&$obj)
-       }
-}
-
-pub(crate) struct DebugBytes<'a>(pub &'a [u8]);
-impl<'a> std::fmt::Display for DebugBytes<'a> {
-       fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
-               for i in self.0 {
-                       write!(f, "{:02x}", i)?;
-               }
-               Ok(())
-       }
-}
-macro_rules! log_bytes {
-       ($obj: expr) => {
-               ::util::macro_logger::DebugBytes(&$obj)
-       }
-}
-
-pub(crate) struct DebugFundingChannelId<'a>(pub &'a Sha256dHash, pub u16);
-impl<'a> std::fmt::Display for DebugFundingChannelId<'a> {
-       fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
-               for i in OutPoint::new(self.0.clone(), self.1).to_channel_id().iter() {
-                       write!(f, "{:02x}", i)?;
-               }
-               Ok(())
-       }
-}
-macro_rules! log_funding_channel_id {
-       ($funding_txid: expr, $funding_txo: expr) => {
-               ::util::macro_logger::DebugFundingChannelId(&$funding_txid, $funding_txo)
-       }
-}
-
-pub(crate) struct DebugFundingInfo<'a, T: 'a>(pub &'a Option<(OutPoint, T)>);
-impl<'a, T> std::fmt::Display for DebugFundingInfo<'a, T> {
-       fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
-               match self.0.as_ref() {
-                       Some(&(ref funding_output, _)) => DebugBytes(&funding_output.to_channel_id()[..]).fmt(f),
-                       None => write!(f, "without funding output set"),
-               }
-       }
-}
-macro_rules! log_funding_info {
-       ($key_storage: expr) => {
-               match $key_storage {
-                       Storage::Local { ref funding_info, .. } => {
-                               ::util::macro_logger::DebugFundingInfo(&funding_info)
-                       },
-                       Storage::Watchtower { .. } => {
-                               ::util::macro_logger::DebugFundingInfo(&None)
-                       }
-               }
-       }
-}
-
-pub(crate) struct DebugRoute<'a>(pub &'a Route);
-impl<'a> std::fmt::Display for DebugRoute<'a> {
-       fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
-               for h in self.0.hops.iter() {
-                       write!(f, "node_id: {}, short_channel_id: {}, fee_msat: {}, cltv_expiry_delta: {}\n", log_pubkey!(h.pubkey), h.short_channel_id, h.fee_msat, h.cltv_expiry_delta)?;
-               }
-               Ok(())
-       }
-}
-macro_rules! log_route {
-       ($obj: expr) => {
-               ::util::macro_logger::DebugRoute(&$obj)
-       }
-}
-
-macro_rules! log_internal {
-       ($self: ident, $lvl:expr, $($arg:tt)+) => (
-               &$self.logger.log(&::util::logger::Record::new($lvl, format_args!($($arg)+), module_path!(), file!(), line!()));
-       );
-}
-
-macro_rules! log_error {
-       ($self: ident, $($arg:tt)*) => (
-               #[cfg(not(any(feature = "max_level_off")))]
-               log_internal!($self, $crate::util::logger::Level::Error, $($arg)*);
-       )
-}
-
-macro_rules! log_warn {
-       ($self: ident, $($arg:tt)*) => (
-               #[cfg(not(any(feature = "max_level_off", feature = "max_level_error")))]
-               log_internal!($self, $crate::util::logger::Level::Warn, $($arg)*);
-       )
-}
-
-macro_rules! log_info {
-       ($self: ident, $($arg:tt)*) => (
-               #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn")))]
-               log_internal!($self, $crate::util::logger::Level::Info, $($arg)*);
-       )
-}
-
-macro_rules! log_debug {
-       ($self: ident, $($arg:tt)*) => (
-               #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info")))]
-               log_internal!($self, $crate::util::logger::Level::Debug, $($arg)*);
-       )
-}
-
-macro_rules! log_trace {
-       ($self: ident, $($arg:tt)*) => (
-               #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info", feature = "max_level_debug")))]
-               log_internal!($self, $crate::util::logger::Level::Trace, $($arg)*);
-       )
-}
diff --git a/src/util/mod.rs b/src/util/mod.rs
deleted file mode 100644 (file)
index aab7703..0000000
+++ /dev/null
@@ -1,27 +0,0 @@
-//! Some utility modules live here. See individual sub-modules for more info.
-
-pub mod events;
-pub mod errors;
-pub mod ser;
-
-pub(crate) mod byte_utils;
-pub(crate) mod chacha20;
-#[cfg(not(feature = "fuzztarget"))]
-pub(crate) mod poly1305;
-pub(crate) mod chacha20poly1305rfc;
-pub(crate) mod transaction_utils;
-
-#[macro_use]
-pub(crate) mod ser_macros;
-#[macro_use]
-pub(crate) mod macro_logger;
-
-// These have to come after macro_logger to build
-pub mod logger;
-pub mod config;
-
-#[cfg(test)]
-pub(crate) mod test_utils;
-
-#[macro_use]
-pub(crate) mod fuzz_wrappers;
diff --git a/src/util/poly1305.rs b/src/util/poly1305.rs
deleted file mode 100644 (file)
index 541c398..0000000
+++ /dev/null
@@ -1,340 +0,0 @@
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-// This is a port of Andrew Moons poly1305-donna
-// https://github.com/floodyberry/poly1305-donna
-
-use std::cmp::min;
-use util::byte_utils::{slice_to_le32, le32_to_array};
-
-#[derive(Clone, Copy)]
-pub struct Poly1305 {
-       r         : [u32; 5],
-       h         : [u32; 5],
-       pad       : [u32; 4],
-       leftover  : usize,
-       buffer    : [u8; 16],
-       finalized : bool,
-}
-
-impl Poly1305 {
-       pub fn new(key: &[u8]) -> Poly1305 {
-               assert!(key.len() == 32);
-               let mut poly = Poly1305{ r: [0u32; 5], h: [0u32; 5], pad: [0u32; 4], leftover: 0, buffer: [0u8; 16], finalized: false };
-
-               // r &= 0xffffffc0ffffffc0ffffffc0fffffff
-               poly.r[0] = (slice_to_le32(&key[0..4])       ) & 0x3ffffff;
-               poly.r[1] = (slice_to_le32(&key[3..7])   >> 2) & 0x3ffff03;
-               poly.r[2] = (slice_to_le32(&key[6..10])  >> 4) & 0x3ffc0ff;
-               poly.r[3] = (slice_to_le32(&key[9..13])  >> 6) & 0x3f03fff;
-               poly.r[4] = (slice_to_le32(&key[12..16]) >> 8) & 0x00fffff;
-
-               poly.pad[0] = slice_to_le32(&key[16..20]);
-               poly.pad[1] = slice_to_le32(&key[20..24]);
-               poly.pad[2] = slice_to_le32(&key[24..28]);
-               poly.pad[3] = slice_to_le32(&key[28..32]);
-
-               poly
-       }
-
-       fn block(&mut self, m: &[u8]) {
-               let hibit : u32 = if self.finalized { 0 } else { 1 << 24 };
-
-               let r0 = self.r[0];
-               let r1 = self.r[1];
-               let r2 = self.r[2];
-               let r3 = self.r[3];
-               let r4 = self.r[4];
-
-               let s1 = r1 * 5;
-               let s2 = r2 * 5;
-               let s3 = r3 * 5;
-               let s4 = r4 * 5;
-
-               let mut h0 = self.h[0];
-               let mut h1 = self.h[1];
-               let mut h2 = self.h[2];
-               let mut h3 = self.h[3];
-               let mut h4 = self.h[4];
-
-               // h += m
-               h0 += (slice_to_le32(&m[0..4])       ) & 0x3ffffff;
-               h1 += (slice_to_le32(&m[3..7])   >> 2) & 0x3ffffff;
-               h2 += (slice_to_le32(&m[6..10])  >> 4) & 0x3ffffff;
-               h3 += (slice_to_le32(&m[9..13])  >> 6) & 0x3ffffff;
-               h4 += (slice_to_le32(&m[12..16]) >> 8) | hibit;
-
-               // h *= r
-               let     d0 = (h0 as u64 * r0 as u64) + (h1 as u64 * s4 as u64) + (h2 as u64 * s3 as u64) + (h3 as u64 * s2 as u64) + (h4 as u64 * s1 as u64);
-               let mut d1 = (h0 as u64 * r1 as u64) + (h1 as u64 * r0 as u64) + (h2 as u64 * s4 as u64) + (h3 as u64 * s3 as u64) + (h4 as u64 * s2 as u64);
-               let mut d2 = (h0 as u64 * r2 as u64) + (h1 as u64 * r1 as u64) + (h2 as u64 * r0 as u64) + (h3 as u64 * s4 as u64) + (h4 as u64 * s3 as u64);
-               let mut d3 = (h0 as u64 * r3 as u64) + (h1 as u64 * r2 as u64) + (h2 as u64 * r1 as u64) + (h3 as u64 * r0 as u64) + (h4 as u64 * s4 as u64);
-               let mut d4 = (h0 as u64 * r4 as u64) + (h1 as u64 * r3 as u64) + (h2 as u64 * r2 as u64) + (h3 as u64 * r1 as u64) + (h4 as u64 * r0 as u64);
-
-               // (partial) h %= p
-               let mut c : u32;
-                               c = (d0 >> 26) as u32; h0 = d0 as u32 & 0x3ffffff;
-               d1 += c as u64; c = (d1 >> 26) as u32; h1 = d1 as u32 & 0x3ffffff;
-               d2 += c as u64; c = (d2 >> 26) as u32; h2 = d2 as u32 & 0x3ffffff;
-               d3 += c as u64; c = (d3 >> 26) as u32; h3 = d3 as u32 & 0x3ffffff;
-               d4 += c as u64; c = (d4 >> 26) as u32; h4 = d4 as u32 & 0x3ffffff;
-               h0 += c * 5;    c = h0 >> 26; h0 = h0 & 0x3ffffff;
-               h1 += c;
-
-               self.h[0] = h0;
-               self.h[1] = h1;
-               self.h[2] = h2;
-               self.h[3] = h3;
-               self.h[4] = h4;
-       }
-
-       pub fn finish(&mut self) {
-               if self.leftover > 0 {
-                       self.buffer[self.leftover] = 1;
-                       for i in self.leftover+1..16 {
-                               self.buffer[i] = 0;
-                       }
-                       self.finalized = true;
-                       let tmp = self.buffer;
-                       self.block(&tmp);
-               }
-
-               // fully carry h
-               let mut h0 = self.h[0];
-               let mut h1 = self.h[1];
-               let mut h2 = self.h[2];
-               let mut h3 = self.h[3];
-               let mut h4 = self.h[4];
-
-               let mut c : u32;
-                            c = h1 >> 26; h1 = h1 & 0x3ffffff;
-               h2 +=     c; c = h2 >> 26; h2 = h2 & 0x3ffffff;
-               h3 +=     c; c = h3 >> 26; h3 = h3 & 0x3ffffff;
-               h4 +=     c; c = h4 >> 26; h4 = h4 & 0x3ffffff;
-               h0 += c * 5; c = h0 >> 26; h0 = h0 & 0x3ffffff;
-               h1 +=     c;
-
-               // compute h + -p
-               let mut g0 = h0.wrapping_add(5); c = g0 >> 26; g0 &= 0x3ffffff;
-               let mut g1 = h1.wrapping_add(c); c = g1 >> 26; g1 &= 0x3ffffff;
-               let mut g2 = h2.wrapping_add(c); c = g2 >> 26; g2 &= 0x3ffffff;
-               let mut g3 = h3.wrapping_add(c); c = g3 >> 26; g3 &= 0x3ffffff;
-               let mut g4 = h4.wrapping_add(c).wrapping_sub(1 << 26);
-
-               // select h if h < p, or h + -p if h >= p
-               let mut mask = (g4 >> (32 - 1)).wrapping_sub(1);
-               g0 &= mask;
-               g1 &= mask;
-               g2 &= mask;
-               g3 &= mask;
-               g4 &= mask;
-               mask = !mask;
-               h0 = (h0 & mask) | g0;
-               h1 = (h1 & mask) | g1;
-               h2 = (h2 & mask) | g2;
-               h3 = (h3 & mask) | g3;
-               h4 = (h4 & mask) | g4;
-
-               // h = h % (2^128)
-               h0 = ((h0      ) | (h1 << 26)) & 0xffffffff;
-               h1 = ((h1 >>  6) | (h2 << 20)) & 0xffffffff;
-               h2 = ((h2 >> 12) | (h3 << 14)) & 0xffffffff;
-               h3 = ((h3 >> 18) | (h4 <<  8)) & 0xffffffff;
-
-               // h = mac = (h + pad) % (2^128)
-               let mut f : u64;
-               f = h0 as u64 + self.pad[0] as u64            ; h0 = f as u32;
-               f = h1 as u64 + self.pad[1] as u64 + (f >> 32); h1 = f as u32;
-               f = h2 as u64 + self.pad[2] as u64 + (f >> 32); h2 = f as u32;
-               f = h3 as u64 + self.pad[3] as u64 + (f >> 32); h3 = f as u32;
-
-               self.h[0] = h0;
-               self.h[1] = h1;
-               self.h[2] = h2;
-               self.h[3] = h3;
-       }
-
-       pub fn input(&mut self, data: &[u8]) {
-               assert!(!self.finalized);
-               let mut m = data;
-
-               if self.leftover > 0 {
-                       let want = min(16 - self.leftover, m.len());
-                       for i in 0..want {
-                               self.buffer[self.leftover+i] = m[i];
-                       }
-                       m = &m[want..];
-                       self.leftover += want;
-
-                       if self.leftover < 16 {
-                               return;
-                       }
-
-                       // self.block(self.buffer[..]);
-                       let tmp = self.buffer;
-                       self.block(&tmp);
-
-                       self.leftover = 0;
-               }
-
-               while m.len() >= 16 {
-                       self.block(&m[0..16]);
-                       m = &m[16..];
-               }
-
-               for i in 0..m.len() {
-                       self.buffer[i] = m[i];
-               }
-               self.leftover = m.len();
-       }
-
-       pub fn raw_result(&mut self, output: &mut [u8]) {
-               assert!(output.len() >= 16);
-               if !self.finalized{
-                       self.finish();
-               }
-               output[0..4].copy_from_slice(&le32_to_array(self.h[0]));
-               output[4..8].copy_from_slice(&le32_to_array(self.h[1]));
-               output[8..12].copy_from_slice(&le32_to_array(self.h[2]));
-               output[12..16].copy_from_slice(&le32_to_array(self.h[3]));
-       }
-}
-
-#[cfg(test)]
-mod test {
-       use std::iter::repeat;
-
-       use util::poly1305::Poly1305;
-
-       fn poly1305(key: &[u8], msg: &[u8], mac: &mut [u8]) {
-               let mut poly = Poly1305::new(key);
-               poly.input(msg);
-               poly.raw_result(mac);
-       }
-
-       #[test]
-       fn test_nacl_vector() {
-               let key = [
-                       0xee,0xa6,0xa7,0x25,0x1c,0x1e,0x72,0x91,
-                       0x6d,0x11,0xc2,0xcb,0x21,0x4d,0x3c,0x25,
-                       0x25,0x39,0x12,0x1d,0x8e,0x23,0x4e,0x65,
-                       0x2d,0x65,0x1f,0xa4,0xc8,0xcf,0xf8,0x80,
-               ];
-
-               let msg = [
-                       0x8e,0x99,0x3b,0x9f,0x48,0x68,0x12,0x73,
-                       0xc2,0x96,0x50,0xba,0x32,0xfc,0x76,0xce,
-                       0x48,0x33,0x2e,0xa7,0x16,0x4d,0x96,0xa4,
-                       0x47,0x6f,0xb8,0xc5,0x31,0xa1,0x18,0x6a,
-                       0xc0,0xdf,0xc1,0x7c,0x98,0xdc,0xe8,0x7b,
-                       0x4d,0xa7,0xf0,0x11,0xec,0x48,0xc9,0x72,
-                       0x71,0xd2,0xc2,0x0f,0x9b,0x92,0x8f,0xe2,
-                       0x27,0x0d,0x6f,0xb8,0x63,0xd5,0x17,0x38,
-                       0xb4,0x8e,0xee,0xe3,0x14,0xa7,0xcc,0x8a,
-                       0xb9,0x32,0x16,0x45,0x48,0xe5,0x26,0xae,
-                       0x90,0x22,0x43,0x68,0x51,0x7a,0xcf,0xea,
-                       0xbd,0x6b,0xb3,0x73,0x2b,0xc0,0xe9,0xda,
-                       0x99,0x83,0x2b,0x61,0xca,0x01,0xb6,0xde,
-                       0x56,0x24,0x4a,0x9e,0x88,0xd5,0xf9,0xb3,
-                       0x79,0x73,0xf6,0x22,0xa4,0x3d,0x14,0xa6,
-                       0x59,0x9b,0x1f,0x65,0x4c,0xb4,0x5a,0x74,
-                       0xe3,0x55,0xa5,
-               ];
-
-               let expected = [
-                       0xf3,0xff,0xc7,0x70,0x3f,0x94,0x00,0xe5,
-                       0x2a,0x7d,0xfb,0x4b,0x3d,0x33,0x05,0xd9,
-               ];
-
-               let mut mac = [0u8; 16];
-               poly1305(&key, &msg, &mut mac);
-               assert_eq!(&mac[..], &expected[..]);
-
-               let mut poly = Poly1305::new(&key);
-               poly.input(&msg[0..32]);
-               poly.input(&msg[32..96]);
-               poly.input(&msg[96..112]);
-               poly.input(&msg[112..120]);
-               poly.input(&msg[120..124]);
-               poly.input(&msg[124..126]);
-               poly.input(&msg[126..127]);
-               poly.input(&msg[127..128]);
-               poly.input(&msg[128..129]);
-               poly.input(&msg[129..130]);
-               poly.input(&msg[130..131]);
-               poly.raw_result(&mut mac);
-               assert_eq!(&mac[..], &expected[..]);
-       }
-
-       #[test]
-       fn donna_self_test() {
-               let wrap_key = [
-                       0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-               ];
-
-               let wrap_msg = [
-                       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
-                       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
-               ];
-
-               let wrap_mac = [
-                       0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-               ];
-
-               let mut mac = [0u8; 16];
-               poly1305(&wrap_key, &wrap_msg, &mut mac);
-               assert_eq!(&mac[..], &wrap_mac[..]);
-
-               let total_key = [
-                       0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0xff,
-                       0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xff, 0xff,
-                       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
-                       0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
-               ];
-
-               let total_mac = [
-                       0x64, 0xaf, 0xe2, 0xe8, 0xd6, 0xad, 0x7b, 0xbd,
-                       0xd2, 0x87, 0xf9, 0x7c, 0x44, 0x62, 0x3d, 0x39,
-               ];
-
-               let mut tpoly = Poly1305::new(&total_key);
-               for i in 0..256 {
-                       let key: Vec<u8> = repeat(i as u8).take(32).collect();
-                       let msg: Vec<u8> = repeat(i as u8).take(256).collect();
-                       let mut mac = [0u8; 16];
-                       poly1305(&key[..], &msg[0..i], &mut mac);
-                       tpoly.input(&mac);
-               }
-               tpoly.raw_result(&mut mac);
-               assert_eq!(&mac[..], &total_mac[..]);
-       }
-
-       #[test]
-       fn test_tls_vectors() {
-               // from http://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-04
-               let key = b"this is 32-byte key for Poly1305";
-               let msg = [0u8; 32];
-               let expected = [
-                       0x49, 0xec, 0x78, 0x09, 0x0e, 0x48, 0x1e, 0xc6,
-                       0xc2, 0x6b, 0x33, 0xb9, 0x1c, 0xcc, 0x03, 0x07,
-               ];
-               let mut mac = [0u8; 16];
-               poly1305(key, &msg, &mut mac);
-               assert_eq!(&mac[..], &expected[..]);
-
-               let msg = b"Hello world!";
-               let expected= [
-                       0xa6, 0xf7, 0x45, 0x00, 0x8f, 0x81, 0xc9, 0x16,
-                       0xa2, 0x0d, 0xcc, 0x74, 0xee, 0xf2, 0xb2, 0xf0,
-               ];
-               poly1305(key, msg, &mut mac);
-               assert_eq!(&mac[..], &expected[..]);
-       }
-}
diff --git a/src/util/ser.rs b/src/util/ser.rs
deleted file mode 100644 (file)
index a2ef16b..0000000
+++ /dev/null
@@ -1,444 +0,0 @@
-//! A very simple serialization framework which is used to serialize/deserialize messages as well
-//! as ChannelsManagers and ChannelMonitors.
-
-use std::result::Result;
-use std::io::{Read, Write};
-use std::collections::HashMap;
-use std::hash::Hash;
-
-use secp256k1::Signature;
-use secp256k1::key::{PublicKey, SecretKey};
-use bitcoin::blockdata::script::Script;
-use bitcoin::blockdata::transaction::OutPoint;
-use bitcoin_hashes::sha256d::Hash as Sha256dHash;
-use std::marker::Sized;
-use ln::msgs::DecodeError;
-use ln::channelmanager::{PaymentPreimage, PaymentHash};
-use util::byte_utils;
-
-use util::byte_utils::{be64_to_array, be48_to_array, be32_to_array, be16_to_array, slice_to_be16, slice_to_be32, slice_to_be48, slice_to_be64};
-
-const MAX_BUF_SIZE: usize = 64 * 1024;
-
-/// A trait that is similar to std::io::Write but has one extra function which can be used to size
-/// buffers being written into.
-/// An impl is provided for any type that also impls std::io::Write which simply ignores size
-/// hints.
-pub trait Writer {
-       /// Writes the given buf out. See std::io::Write::write_all for more
-       fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error>;
-       /// Hints that data of the given size is about the be written. This may not always be called
-       /// prior to data being written and may be safely ignored.
-       fn size_hint(&mut self, size: usize);
-}
-
-impl<W: Write> Writer for W {
-       #[inline]
-       fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
-               <Self as ::std::io::Write>::write_all(self, buf)
-       }
-       #[inline]
-       fn size_hint(&mut self, _size: usize) { }
-}
-
-pub(crate) struct WriterWriteAdaptor<'a, W: Writer + 'a>(pub &'a mut W);
-impl<'a, W: Writer + 'a> Write for WriterWriteAdaptor<'a, W> {
-       fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
-               self.0.write_all(buf)
-       }
-       fn write(&mut self, buf: &[u8]) -> Result<usize, ::std::io::Error> {
-               self.0.write_all(buf)?;
-               Ok(buf.len())
-       }
-       fn flush(&mut self) -> Result<(), ::std::io::Error> {
-               Ok(())
-       }
-}
-
-struct VecWriter(Vec<u8>);
-impl Writer for VecWriter {
-       fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
-               self.0.extend_from_slice(buf);
-               Ok(())
-       }
-       fn size_hint(&mut self, size: usize) {
-               self.0.reserve_exact(size);
-       }
-}
-
-/// A trait that various rust-lightning types implement allowing them to be written out to a Writer
-pub trait Writeable {
-       /// Writes self out to the given Writer
-       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error>;
-
-       /// Writes self out to a Vec<u8>
-       fn encode(&self) -> Vec<u8> {
-               let mut msg = VecWriter(Vec::new());
-               self.write(&mut msg).unwrap();
-               msg.0
-       }
-
-       /// Writes self out to a Vec<u8>
-       fn encode_with_len(&self) -> Vec<u8> {
-               let mut msg = VecWriter(Vec::new());
-               0u16.write(&mut msg).unwrap();
-               self.write(&mut msg).unwrap();
-               let len = msg.0.len();
-               msg.0[..2].copy_from_slice(&byte_utils::be16_to_array(len as u16 - 2));
-               msg.0
-       }
-}
-
-/// A trait that various rust-lightning types implement allowing them to be read in from a Read
-pub trait Readable<R>
-       where Self: Sized,
-             R: Read
-{
-       /// Reads a Self in from the given Read
-       fn read(reader: &mut R) -> Result<Self, DecodeError>;
-}
-
-/// A trait that various higher-level rust-lightning types implement allowing them to be read in
-/// from a Read given some additional set of arguments which is required to deserialize.
-pub trait ReadableArgs<R, P>
-       where Self: Sized,
-             R: Read
-{
-       /// Reads a Self in from the given Read
-       fn read(reader: &mut R, params: P) -> Result<Self, DecodeError>;
-}
-
-pub(crate) struct U48(pub u64);
-impl Writeable for U48 {
-       #[inline]
-       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
-               writer.write_all(&be48_to_array(self.0))
-       }
-}
-impl<R: Read> Readable<R> for U48 {
-       #[inline]
-       fn read(reader: &mut R) -> Result<U48, DecodeError> {
-               let mut buf = [0; 6];
-               reader.read_exact(&mut buf)?;
-               Ok(U48(slice_to_be48(&buf)))
-       }
-}
-
-macro_rules! impl_writeable_primitive {
-       ($val_type:ty, $meth_write:ident, $len: expr, $meth_read:ident) => {
-               impl Writeable for $val_type {
-                       #[inline]
-                       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
-                               writer.write_all(&$meth_write(*self))
-                       }
-               }
-               impl<R: Read> Readable<R> for $val_type {
-                       #[inline]
-                       fn read(reader: &mut R) -> Result<$val_type, DecodeError> {
-                               let mut buf = [0; $len];
-                               reader.read_exact(&mut buf)?;
-                               Ok($meth_read(&buf))
-                       }
-               }
-       }
-}
-
-impl_writeable_primitive!(u64, be64_to_array, 8, slice_to_be64);
-impl_writeable_primitive!(u32, be32_to_array, 4, slice_to_be32);
-impl_writeable_primitive!(u16, be16_to_array, 2, slice_to_be16);
-
-impl Writeable for u8 {
-       #[inline]
-       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
-               writer.write_all(&[*self])
-       }
-}
-impl<R: Read> Readable<R> for u8 {
-       #[inline]
-       fn read(reader: &mut R) -> Result<u8, DecodeError> {
-               let mut buf = [0; 1];
-               reader.read_exact(&mut buf)?;
-               Ok(buf[0])
-       }
-}
-
-impl Writeable for bool {
-       #[inline]
-       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
-               writer.write_all(&[if *self {1} else {0}])
-       }
-}
-impl<R: Read> Readable<R> for bool {
-       #[inline]
-       fn read(reader: &mut R) -> Result<bool, DecodeError> {
-               let mut buf = [0; 1];
-               reader.read_exact(&mut buf)?;
-               if buf[0] != 0 && buf[0] != 1 {
-                       return Err(DecodeError::InvalidValue);
-               }
-               Ok(buf[0] == 1)
-       }
-}
-
-// u8 arrays
-macro_rules! impl_array {
-       ( $size:expr ) => (
-               impl Writeable for [u8; $size]
-               {
-                       #[inline]
-                       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
-                               w.write_all(self)
-                       }
-               }
-
-               impl<R: Read> Readable<R> for [u8; $size]
-               {
-                       #[inline]
-                       fn read(r: &mut R) -> Result<Self, DecodeError> {
-                               let mut buf = [0u8; $size];
-                               r.read_exact(&mut buf)?;
-                               Ok(buf)
-                       }
-               }
-       );
-}
-
-//TODO: performance issue with [u8; size] with impl_array!()
-impl_array!(3); // for rgb
-impl_array!(4); // for IPv4
-impl_array!(10); // for OnionV2
-impl_array!(16); // for IPv6
-impl_array!(32); // for channel id & hmac
-impl_array!(33); // for PublicKey
-impl_array!(64); // for Signature
-impl_array!(1300); // for OnionPacket.hop_data
-
-// HashMap
-impl<K, V> Writeable for HashMap<K, V>
-       where K: Writeable + Eq + Hash,
-             V: Writeable
-{
-       #[inline]
-       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
-       (self.len() as u16).write(w)?;
-               for (key, value) in self.iter() {
-                       key.write(w)?;
-                       value.write(w)?;
-               }
-               Ok(())
-       }
-}
-
-impl<R, K, V> Readable<R> for HashMap<K, V>
-       where R: Read,
-             K: Readable<R> + Eq + Hash,
-             V: Readable<R>
-{
-       #[inline]
-       fn read(r: &mut R) -> Result<Self, DecodeError> {
-               let len: u16 = Readable::read(r)?;
-               let mut ret = HashMap::with_capacity(len as usize);
-               for _ in 0..len {
-                       ret.insert(K::read(r)?, V::read(r)?);
-               }
-               Ok(ret)
-       }
-}
-
-// Vectors
-impl Writeable for Vec<u8> {
-       #[inline]
-       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
-               (self.len() as u16).write(w)?;
-               w.write_all(&self)
-       }
-}
-
-impl<R: Read> Readable<R> for Vec<u8> {
-       #[inline]
-       fn read(r: &mut R) -> Result<Self, DecodeError> {
-               let len: u16 = Readable::read(r)?;
-               let mut ret = Vec::with_capacity(len as usize);
-               ret.resize(len as usize, 0);
-               r.read_exact(&mut ret)?;
-               Ok(ret)
-       }
-}
-impl Writeable for Vec<Signature> {
-       #[inline]
-       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
-               (self.len() as u16).write(w)?;
-               for e in self.iter() {
-                       e.write(w)?;
-               }
-               Ok(())
-       }
-}
-
-impl<R: Read> Readable<R> for Vec<Signature> {
-       #[inline]
-       fn read(r: &mut R) -> Result<Self, DecodeError> {
-               let len: u16 = Readable::read(r)?;
-               let byte_size = (len as usize)
-                               .checked_mul(33)
-                               .ok_or(DecodeError::BadLengthDescriptor)?;
-               if byte_size > MAX_BUF_SIZE {
-                       return Err(DecodeError::BadLengthDescriptor);
-               }
-               let mut ret = Vec::with_capacity(len as usize);
-               for _ in 0..len { ret.push(Signature::read(r)?); }
-               Ok(ret)
-       }
-}
-
-impl Writeable for Script {
-       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
-               (self.len() as u16).write(w)?;
-               w.write_all(self.as_bytes())
-       }
-}
-
-impl<R: Read> Readable<R> for Script {
-       fn read(r: &mut R) -> Result<Self, DecodeError> {
-               let len = <u16 as Readable<R>>::read(r)? as usize;
-               let mut buf = vec![0; len];
-               r.read_exact(&mut buf)?;
-               Ok(Script::from(buf))
-       }
-}
-
-impl Writeable for PublicKey {
-       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
-               self.serialize().write(w)
-       }
-}
-
-impl<R: Read> Readable<R> for PublicKey {
-       fn read(r: &mut R) -> Result<Self, DecodeError> {
-               let buf: [u8; 33] = Readable::read(r)?;
-               match PublicKey::from_slice(&buf) {
-                       Ok(key) => Ok(key),
-                       Err(_) => return Err(DecodeError::InvalidValue),
-               }
-       }
-}
-
-impl Writeable for SecretKey {
-       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
-               let mut ser = [0; 32];
-               ser.copy_from_slice(&self[..]);
-               ser.write(w)
-       }
-}
-
-impl<R: Read> Readable<R> for SecretKey {
-       fn read(r: &mut R) -> Result<Self, DecodeError> {
-               let buf: [u8; 32] = Readable::read(r)?;
-               match SecretKey::from_slice(&buf) {
-                       Ok(key) => Ok(key),
-                       Err(_) => return Err(DecodeError::InvalidValue),
-               }
-       }
-}
-
-impl Writeable for Sha256dHash {
-       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
-               w.write_all(&self[..])
-       }
-}
-
-impl<R: Read> Readable<R> for Sha256dHash {
-       fn read(r: &mut R) -> Result<Self, DecodeError> {
-               use bitcoin_hashes::Hash;
-
-               let buf: [u8; 32] = Readable::read(r)?;
-               Ok(Sha256dHash::from_slice(&buf[..]).unwrap())
-       }
-}
-
-impl Writeable for Signature {
-       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
-               self.serialize_compact().write(w)
-       }
-}
-
-impl<R: Read> Readable<R> for Signature {
-       fn read(r: &mut R) -> Result<Self, DecodeError> {
-               let buf: [u8; 64] = Readable::read(r)?;
-               match Signature::from_compact(&buf) {
-                       Ok(sig) => Ok(sig),
-                       Err(_) => return Err(DecodeError::InvalidValue),
-               }
-       }
-}
-
-impl Writeable for PaymentPreimage {
-       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
-               self.0.write(w)
-       }
-}
-
-impl<R: Read> Readable<R> for PaymentPreimage {
-       fn read(r: &mut R) -> Result<Self, DecodeError> {
-               let buf: [u8; 32] = Readable::read(r)?;
-               Ok(PaymentPreimage(buf))
-       }
-}
-
-impl Writeable for PaymentHash {
-       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
-               self.0.write(w)
-       }
-}
-
-impl<R: Read> Readable<R> for PaymentHash {
-       fn read(r: &mut R) -> Result<Self, DecodeError> {
-               let buf: [u8; 32] = Readable::read(r)?;
-               Ok(PaymentHash(buf))
-       }
-}
-
-impl<T: Writeable> Writeable for Option<T> {
-       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
-               match *self {
-                       None => 0u8.write(w)?,
-                       Some(ref data) => {
-                               1u8.write(w)?;
-                               data.write(w)?;
-                       }
-               }
-               Ok(())
-       }
-}
-
-impl<R, T> Readable<R> for Option<T>
-       where R: Read,
-             T: Readable<R>
-{
-       fn read(r: &mut R) -> Result<Self, DecodeError> {
-               match <u8 as Readable<R>>::read(r)? {
-                       0 => Ok(None),
-                       1 => Ok(Some(Readable::read(r)?)),
-                       _ => return Err(DecodeError::InvalidValue),
-               }
-       }
-}
-
-impl Writeable for OutPoint {
-       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
-               self.txid.write(w)?;
-               self.vout.write(w)?;
-               Ok(())
-       }
-}
-
-impl<R: Read> Readable<R> for OutPoint {
-       fn read(r: &mut R) -> Result<Self, DecodeError> {
-               let txid = Readable::read(r)?;
-               let vout = Readable::read(r)?;
-               Ok(OutPoint {
-                       txid,
-                       vout,
-               })
-       }
-}
diff --git a/src/util/ser_macros.rs b/src/util/ser_macros.rs
deleted file mode 100644 (file)
index 48e87b3..0000000
+++ /dev/null
@@ -1,42 +0,0 @@
-macro_rules! impl_writeable {
-       ($st:ident, $len: expr, {$($field:ident),*}) => {
-               impl ::util::ser::Writeable for $st {
-                       fn write<W: ::util::ser::Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
-                               if $len != 0 {
-                                       w.size_hint($len);
-                               }
-                               $( self.$field.write(w)?; )*
-                               Ok(())
-                       }
-               }
-
-               impl<R: ::std::io::Read> ::util::ser::Readable<R> for $st {
-                       fn read(r: &mut R) -> Result<Self, ::ln::msgs::DecodeError> {
-                               Ok(Self {
-                                       $($field: ::util::ser::Readable::read(r)?),*
-                               })
-                       }
-               }
-       }
-}
-macro_rules! impl_writeable_len_match {
-       ($st:ident, {$({$m: pat, $l: expr}),*}, {$($field:ident),*}) => {
-               impl Writeable for $st {
-                       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
-                               w.size_hint(match *self {
-                                       $($m => $l,)*
-                               });
-                               $( self.$field.write(w)?; )*
-                               Ok(())
-                       }
-               }
-
-               impl<R: ::std::io::Read> Readable<R> for $st {
-                       fn read(r: &mut R) -> Result<Self, DecodeError> {
-                               Ok(Self {
-                                       $($field: Readable::read(r)?),*
-                               })
-                       }
-               }
-       }
-}
diff --git a/src/util/test_utils.rs b/src/util/test_utils.rs
deleted file mode 100644 (file)
index cc6fe96..0000000
+++ /dev/null
@@ -1,253 +0,0 @@
-use chain::chaininterface;
-use chain::chaininterface::ConfirmationTarget;
-use chain::transaction::OutPoint;
-use chain::keysinterface;
-use ln::channelmonitor;
-use ln::msgs;
-use ln::msgs::LocalFeatures;
-use ln::msgs::{HandleError};
-use ln::channelmonitor::HTLCUpdate;
-use util::events;
-use util::logger::{Logger, Level, Record};
-use util::ser::{ReadableArgs, Writer};
-
-use bitcoin::blockdata::transaction::Transaction;
-use bitcoin::blockdata::script::Script;
-use bitcoin_hashes::sha256d::Hash as Sha256dHash;
-use bitcoin::network::constants::Network;
-
-use secp256k1::{SecretKey, PublicKey};
-
-use std::time::{SystemTime, UNIX_EPOCH};
-use std::sync::{Arc,Mutex};
-use std::{mem};
-
-pub struct TestVecWriter(pub Vec<u8>);
-impl Writer for TestVecWriter {
-       fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
-               self.0.extend_from_slice(buf);
-               Ok(())
-       }
-       fn size_hint(&mut self, size: usize) {
-               self.0.reserve_exact(size);
-       }
-}
-
-pub struct TestFeeEstimator {
-       pub sat_per_kw: u64,
-}
-impl chaininterface::FeeEstimator for TestFeeEstimator {
-       fn get_est_sat_per_1000_weight(&self, _confirmation_target: ConfirmationTarget) -> u64 {
-               self.sat_per_kw
-       }
-}
-
-pub struct TestChannelMonitor {
-       pub added_monitors: Mutex<Vec<(OutPoint, channelmonitor::ChannelMonitor)>>,
-       pub simple_monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint>>,
-       pub update_ret: Mutex<Result<(), channelmonitor::ChannelMonitorUpdateErr>>,
-}
-impl TestChannelMonitor {
-       pub fn new(chain_monitor: Arc<chaininterface::ChainWatchInterface>, broadcaster: Arc<chaininterface::BroadcasterInterface>, logger: Arc<Logger>, fee_estimator: Arc<chaininterface::FeeEstimator>) -> Self {
-               Self {
-                       added_monitors: Mutex::new(Vec::new()),
-                       simple_monitor: channelmonitor::SimpleManyChannelMonitor::new(chain_monitor, broadcaster, logger, fee_estimator),
-                       update_ret: Mutex::new(Ok(())),
-               }
-       }
-}
-impl channelmonitor::ManyChannelMonitor for TestChannelMonitor {
-       fn add_update_monitor(&self, funding_txo: OutPoint, monitor: channelmonitor::ChannelMonitor) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> {
-               // At every point where we get a monitor update, we should be able to send a useful monitor
-               // to a watchtower and disk...
-               let mut w = TestVecWriter(Vec::new());
-               monitor.write_for_disk(&mut w).unwrap();
-               assert!(<(Sha256dHash, channelmonitor::ChannelMonitor)>::read(
-                               &mut ::std::io::Cursor::new(&w.0), Arc::new(TestLogger::new())).unwrap().1 == monitor);
-               w.0.clear();
-               monitor.write_for_watchtower(&mut w).unwrap(); // This at least shouldn't crash...
-               self.added_monitors.lock().unwrap().push((funding_txo, monitor.clone()));
-               assert!(self.simple_monitor.add_update_monitor(funding_txo, monitor).is_ok());
-               self.update_ret.lock().unwrap().clone()
-       }
-
-       fn fetch_pending_htlc_updated(&self) -> Vec<HTLCUpdate> {
-               return self.simple_monitor.fetch_pending_htlc_updated();
-       }
-}
-
-pub struct TestBroadcaster {
-       pub txn_broadcasted: Mutex<Vec<Transaction>>,
-}
-impl chaininterface::BroadcasterInterface for TestBroadcaster {
-       fn broadcast_transaction(&self, tx: &Transaction) {
-               self.txn_broadcasted.lock().unwrap().push(tx.clone());
-       }
-}
-
-pub struct TestChannelMessageHandler {
-       pub pending_events: Mutex<Vec<events::MessageSendEvent>>,
-}
-
-impl TestChannelMessageHandler {
-       pub fn new() -> Self {
-               TestChannelMessageHandler {
-                       pending_events: Mutex::new(Vec::new()),
-               }
-       }
-}
-
-impl msgs::ChannelMessageHandler for TestChannelMessageHandler {
-       fn handle_open_channel(&self, _their_node_id: &PublicKey, _their_local_features: LocalFeatures, _msg: &msgs::OpenChannel) -> Result<(), HandleError> {
-               Err(HandleError { err: "", action: None })
-       }
-       fn handle_accept_channel(&self, _their_node_id: &PublicKey, _their_local_features: LocalFeatures, _msg: &msgs::AcceptChannel) -> Result<(), HandleError> {
-               Err(HandleError { err: "", action: None })
-       }
-       fn handle_funding_created(&self, _their_node_id: &PublicKey, _msg: &msgs::FundingCreated) -> Result<(), HandleError> {
-               Err(HandleError { err: "", action: None })
-       }
-       fn handle_funding_signed(&self, _their_node_id: &PublicKey, _msg: &msgs::FundingSigned) -> Result<(), HandleError> {
-               Err(HandleError { err: "", action: None })
-       }
-       fn handle_funding_locked(&self, _their_node_id: &PublicKey, _msg: &msgs::FundingLocked) -> Result<(), HandleError> {
-               Err(HandleError { err: "", action: None })
-       }
-       fn handle_shutdown(&self, _their_node_id: &PublicKey, _msg: &msgs::Shutdown) -> Result<(), HandleError> {
-               Err(HandleError { err: "", action: None })
-       }
-       fn handle_closing_signed(&self, _their_node_id: &PublicKey, _msg: &msgs::ClosingSigned) -> Result<(), HandleError> {
-               Err(HandleError { err: "", action: None })
-       }
-       fn handle_update_add_htlc(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateAddHTLC) -> Result<(), HandleError> {
-               Err(HandleError { err: "", action: None })
-       }
-       fn handle_update_fulfill_htlc(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateFulfillHTLC) -> Result<(), HandleError> {
-               Err(HandleError { err: "", action: None })
-       }
-       fn handle_update_fail_htlc(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateFailHTLC) -> Result<(), HandleError> {
-               Err(HandleError { err: "", action: None })
-       }
-       fn handle_update_fail_malformed_htlc(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateFailMalformedHTLC) -> Result<(), HandleError> {
-               Err(HandleError { err: "", action: None })
-       }
-       fn handle_commitment_signed(&self, _their_node_id: &PublicKey, _msg: &msgs::CommitmentSigned) -> Result<(), HandleError> {
-               Err(HandleError { err: "", action: None })
-       }
-       fn handle_revoke_and_ack(&self, _their_node_id: &PublicKey, _msg: &msgs::RevokeAndACK) -> Result<(), HandleError> {
-               Err(HandleError { err: "", action: None })
-       }
-       fn handle_update_fee(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateFee) -> Result<(), HandleError> {
-               Err(HandleError { err: "", action: None })
-       }
-       fn handle_announcement_signatures(&self, _their_node_id: &PublicKey, _msg: &msgs::AnnouncementSignatures) -> Result<(), HandleError> {
-               Err(HandleError { err: "", action: None })
-       }
-       fn handle_channel_reestablish(&self, _their_node_id: &PublicKey, _msg: &msgs::ChannelReestablish) -> Result<(), HandleError> {
-               Err(HandleError { err: "", action: None })
-       }
-       fn peer_disconnected(&self, _their_node_id: &PublicKey, _no_connection_possible: bool) {}
-       fn peer_connected(&self, _their_node_id: &PublicKey) {}
-       fn handle_error(&self, _their_node_id: &PublicKey, _msg: &msgs::ErrorMessage) {}
-}
-
-impl events::MessageSendEventsProvider for TestChannelMessageHandler {
-       fn get_and_clear_pending_msg_events(&self) -> Vec<events::MessageSendEvent> {
-               let mut pending_events = self.pending_events.lock().unwrap();
-               let mut ret = Vec::new();
-               mem::swap(&mut ret, &mut *pending_events);
-               ret
-       }
-}
-
-pub struct TestRoutingMessageHandler {}
-
-impl TestRoutingMessageHandler {
-       pub fn new() -> Self {
-               TestRoutingMessageHandler {}
-       }
-}
-impl msgs::RoutingMessageHandler for TestRoutingMessageHandler {
-       fn handle_node_announcement(&self, _msg: &msgs::NodeAnnouncement) -> Result<bool, HandleError> {
-               Err(HandleError { err: "", action: None })
-       }
-       fn handle_channel_announcement(&self, _msg: &msgs::ChannelAnnouncement) -> Result<bool, HandleError> {
-               Err(HandleError { err: "", action: None })
-       }
-       fn handle_channel_update(&self, _msg: &msgs::ChannelUpdate) -> Result<bool, HandleError> {
-               Err(HandleError { err: "", action: None })
-       }
-       fn handle_htlc_fail_channel_update(&self, _update: &msgs::HTLCFailChannelUpdate) {}
-       fn get_next_channel_announcements(&self, _starting_point: u64, _batch_amount: u8) -> Vec<(msgs::ChannelAnnouncement, msgs::ChannelUpdate,msgs::ChannelUpdate)> {
-               Vec::new()
-       }
-       fn get_next_node_announcements(&self, _starting_point: Option<&PublicKey>, _batch_amount: u8) -> Vec<msgs::NodeAnnouncement> {
-               Vec::new()
-       }
-}
-
-pub struct TestLogger {
-       level: Level,
-       id: String,
-}
-
-impl TestLogger {
-       pub fn new() -> TestLogger {
-               Self::with_id("".to_owned())
-       }
-       pub fn with_id(id: String) -> TestLogger {
-               TestLogger {
-                       level: Level::Trace,
-                       id,
-               }
-       }
-       pub fn enable(&mut self, level: Level) {
-               self.level = level;
-       }
-}
-
-impl Logger for TestLogger {
-       fn log(&self, record: &Record) {
-               if self.level >= record.level {
-                       println!("{:<5} {} [{} : {}, {}] {}", record.level.to_string(), self.id, record.module_path, record.file, record.line, record.args);
-               }
-       }
-}
-
-pub struct TestKeysInterface {
-       backing: keysinterface::KeysManager,
-       pub override_session_priv: Mutex<Option<SecretKey>>,
-       pub override_channel_id_priv: Mutex<Option<[u8; 32]>>,
-}
-
-impl keysinterface::KeysInterface for TestKeysInterface {
-       fn get_node_secret(&self) -> SecretKey { self.backing.get_node_secret() }
-       fn get_destination_script(&self) -> Script { self.backing.get_destination_script() }
-       fn get_shutdown_pubkey(&self) -> PublicKey { self.backing.get_shutdown_pubkey() }
-       fn get_channel_keys(&self, inbound: bool) -> keysinterface::ChannelKeys { self.backing.get_channel_keys(inbound) }
-
-       fn get_session_key(&self) -> SecretKey {
-               match *self.override_session_priv.lock().unwrap() {
-                       Some(key) => key.clone(),
-                       None => self.backing.get_session_key()
-               }
-       }
-
-       fn get_channel_id(&self) -> [u8; 32] {
-               match *self.override_channel_id_priv.lock().unwrap() {
-                       Some(key) => key.clone(),
-                       None => self.backing.get_channel_id()
-               }
-       }
-}
-
-impl TestKeysInterface {
-       pub fn new(seed: &[u8; 32], network: Network, logger: Arc<Logger>) -> Self {
-               let now = SystemTime::now().duration_since(UNIX_EPOCH).expect("Time went backwards");
-               Self {
-                       backing: keysinterface::KeysManager::new(seed, network, logger, now.as_secs(), now.subsec_nanos()),
-                       override_session_priv: Mutex::new(None),
-                       override_channel_id_priv: Mutex::new(None),
-               }
-       }
-}
diff --git a/src/util/transaction_utils.rs b/src/util/transaction_utils.rs
deleted file mode 100644 (file)
index a7c1e6b..0000000
+++ /dev/null
@@ -1,152 +0,0 @@
-use bitcoin::blockdata::transaction::TxOut;
-
-use std::cmp::Ordering;
-
-pub fn sort_outputs<T, C : Fn(&T, &T) -> Ordering>(outputs: &mut Vec<(TxOut, T)>, tie_breaker: C) {
-       outputs.sort_unstable_by(|a, b| {
-               a.0.value.cmp(&b.0.value).then_with(|| {
-                       a.0.script_pubkey[..].cmp(&b.0.script_pubkey[..]).then_with(|| {
-                               tie_breaker(&a.1, &b.1)
-                       })
-               })
-       });
-}
-
-#[cfg(test)]
-mod tests {
-       use super::*;
-
-       use bitcoin::blockdata::script::{Script, Builder};
-       use bitcoin::blockdata::transaction::TxOut;
-
-       use hex::decode;
-
-       #[test]
-       fn sort_output_by_value() {
-               let txout1 = TxOut {
-                       value:  100,
-                       script_pubkey: Builder::new().push_int(0).into_script()
-               };
-               let txout1_ = txout1.clone();
-
-               let txout2 = TxOut {
-                       value: 99,
-                       script_pubkey: Builder::new().push_int(0).into_script()
-               };
-               let txout2_ = txout2.clone();
-
-               let mut outputs = vec![(txout1, "ignore"), (txout2, "ignore")];
-               sort_outputs(&mut outputs, |_, _| { unreachable!(); });
-
-               assert_eq!(
-                       &outputs,
-                       &vec![(txout2_, "ignore"), (txout1_, "ignore")]
-                       );
-       }
-
-       #[test]
-       fn sort_output_by_script_pubkey() {
-               let txout1 = TxOut {
-                       value:  100,
-                       script_pubkey: Builder::new().push_int(3).into_script(),
-               };
-               let txout1_ = txout1.clone();
-
-               let txout2 = TxOut {
-                       value: 100,
-                       script_pubkey: Builder::new().push_int(1).push_int(2).into_script()
-               };
-               let txout2_ = txout2.clone();
-
-               let mut outputs = vec![(txout1, "ignore"), (txout2, "ignore")];
-               sort_outputs(&mut outputs, |_, _| { unreachable!(); });
-
-               assert_eq!(
-                       &outputs,
-                       &vec![(txout2_, "ignore"), (txout1_, "ignore")]
-                       );
-       }
-
-       #[test]
-       fn sort_output_by_bip_test() {
-               let txout1 = TxOut {
-                       value: 100000000,
-                       script_pubkey: script_from_hex("41046a0765b5865641ce08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a68aee3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac")
-               };
-               let txout1_ = txout1.clone();
-
-               // doesn't deserialize cleanly:
-               let txout2 = TxOut {
-                       value: 2400000000,
-                       script_pubkey: script_from_hex("41044a656f065871a353f216ca26cef8dde2f03e8c16202d2e8ad769f02032cb86a5eb5e56842e92e19141d60a01928f8dd2c875a390f67c1f6c94cfc617c0ea45afac")
-               };
-               let txout2_ = txout2.clone();
-
-               let mut outputs = vec![(txout1, "ignore"), (txout2, "ignore")];
-               sort_outputs(&mut outputs, |_, _| { unreachable!(); });
-
-               assert_eq!(&outputs, &vec![(txout1_, "ignore"), (txout2_, "ignore")]);
-       }
-
-       #[test]
-       fn sort_output_tie_breaker_test() {
-               let txout1 = TxOut {
-                       value:  100,
-                       script_pubkey: Builder::new().push_int(1).push_int(2).into_script()
-               };
-               let txout1_ = txout1.clone();
-
-               let txout2 = txout1.clone();
-               let txout2_ = txout1.clone();
-
-               let mut outputs = vec![(txout1, 420), (txout2, 69)];
-               sort_outputs(&mut outputs, |a, b| { a.cmp(b) });
-
-               assert_eq!(
-                       &outputs,
-                       &vec![(txout2_, 69), (txout1_, 420)]
-               );
-       }
-
-       fn script_from_hex(hex_str: &str) -> Script {
-               Script::from(decode(hex_str).unwrap())
-       }
-
-       macro_rules! bip_txout_tests {
-               ($($name:ident: $value:expr,)*) => {
-                       $(
-                               #[test]
-                               fn $name() {
-                                       let expected_raw: Vec<(u64, &str)> = $value;
-                                       let expected: Vec<(TxOut, &str)> = expected_raw.iter()
-                                               .map(|txout_raw| TxOut {
-                                                       value: txout_raw.0,
-                                                       script_pubkey: script_from_hex(txout_raw.1)
-                                               }).map(|txout| (txout, "ignore"))
-                                       .collect();
-
-                                       let mut outputs = expected.clone();
-                                       outputs.reverse(); // prep it
-
-                                       // actually do the work!
-                                       sort_outputs(&mut outputs, |_, _| { unreachable!(); });
-
-                                       assert_eq!(outputs, expected);
-                               }
-                       )*
-               }
-       }
-
-       const TXOUT1: [(u64, &str); 2] = [
-               (400057456, "76a9144a5fba237213a062f6f57978f796390bdcf8d01588ac"),
-               (40000000000, "76a9145be32612930b8323add2212a4ec03c1562084f8488ac"),
-       ];
-       const TXOUT2: [(u64, &str); 2] = [
-               (100000000, "41046a0765b5865641ce08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a68aee3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac"),
-               (2400000000, "41044a656f065871a353f216ca26cef8dde2f03e8c16202d2e8ad769f02032cb86a5eb5e56842e92e19141d60a01928f8dd2c875a390f67c1f6c94cfc617c0ea45afac"),
-       ];
-       bip_txout_tests! {
-               bip69_txout_test_1: TXOUT1.to_vec(),
-               bip69_txout_test_2: TXOUT2.to_vec(),
-       }
-}