Merge pull request #2307 from benthecarman/verify-funcs
authorMatt Corallo <649246+TheBlueMatt@users.noreply.github.com>
Sat, 8 Jul 2023 22:04:06 +0000 (22:04 +0000)
committerGitHub <noreply@github.com>
Sat, 8 Jul 2023 22:04:06 +0000 (22:04 +0000)
Add helper functions to verify node and channel annoucements

86 files changed:
.github/workflows/build.yml
.gitignore
Cargo.toml
SECURITY.md
bench/Cargo.toml [new file with mode: 0644]
bench/README.md [new file with mode: 0644]
bench/benches/bench.rs [new file with mode: 0644]
ci/ci-tests.sh
fuzz/src/chanmon_consistency.rs
fuzz/src/full_stack.rs
fuzz/src/invoice_request_deser.rs
fuzz/src/onion_message.rs
fuzz/src/refund_deser.rs
fuzz/src/router.rs
lightning-background-processor/Cargo.toml
lightning-background-processor/src/lib.rs
lightning-block-sync/Cargo.toml
lightning-custom-message/Cargo.toml
lightning-invoice/Cargo.toml
lightning-invoice/src/payment.rs
lightning-invoice/src/utils.rs
lightning-net-tokio/Cargo.toml
lightning-net-tokio/src/lib.rs
lightning-persister/Cargo.toml
lightning-persister/src/lib.rs
lightning-rapid-gossip-sync/Cargo.toml
lightning-rapid-gossip-sync/src/lib.rs
lightning-rapid-gossip-sync/src/processing.rs
lightning-transaction-sync/Cargo.toml
lightning/Cargo.toml
lightning/src/chain/chaininterface.rs
lightning/src/chain/chainmonitor.rs
lightning/src/chain/channelmonitor.rs
lightning/src/chain/mod.rs
lightning/src/chain/onchaintx.rs
lightning/src/chain/package.rs
lightning/src/events/bump_transaction.rs
lightning/src/events/mod.rs
lightning/src/lib.rs
lightning/src/ln/chan_utils.rs
lightning/src/ln/chanmon_update_fail_tests.rs
lightning/src/ln/channel.rs
lightning/src/ln/channelmanager.rs
lightning/src/ln/features.rs
lightning/src/ln/functional_test_utils.rs
lightning/src/ln/functional_tests.rs
lightning/src/ln/monitor_tests.rs
lightning/src/ln/msgs.rs
lightning/src/ln/onion_route_tests.rs
lightning/src/ln/outbound_payment.rs
lightning/src/ln/payment_tests.rs
lightning/src/ln/peer_handler.rs
lightning/src/ln/priv_short_conf_tests.rs
lightning/src/ln/reload_tests.rs
lightning/src/ln/reorg_tests.rs
lightning/src/ln/shutdown_tests.rs
lightning/src/ln/wire.rs
lightning/src/offers/invoice.rs
lightning/src/offers/invoice_error.rs [new file with mode: 0644]
lightning/src/offers/invoice_request.rs
lightning/src/offers/merkle.rs
lightning/src/offers/mod.rs
lightning/src/offers/offer.rs
lightning/src/offers/refund.rs
lightning/src/offers/test_utils.rs
lightning/src/onion_message/functional_tests.rs
lightning/src/onion_message/messenger.rs
lightning/src/onion_message/mod.rs
lightning/src/onion_message/offers.rs [new file with mode: 0644]
lightning/src/onion_message/packet.rs
lightning/src/routing/gossip.rs
lightning/src/routing/router.rs
lightning/src/routing/scoring.rs
lightning/src/sign/mod.rs
lightning/src/sync/mod.rs
lightning/src/sync/nostd_sync.rs
lightning/src/util/atomic_counter.rs
lightning/src/util/config.rs
lightning/src/util/enforcing_trait_impls.rs
lightning/src/util/mod.rs
lightning/src/util/ser.rs
lightning/src/util/ser_macros.rs
lightning/src/util/test_utils.rs
lightning/src/util/time.rs
pending_changelog/forward-underpaying-htlc.txt [new file with mode: 0644]
pending_changelog/no-legacy-payments.txt [new file with mode: 0644]

index 8e02ec9da75c803caa0dfd9b797ea6b892a13298..9ebfd1257216271357018313999e0d9976e85cd5 100644 (file)
@@ -37,11 +37,9 @@ jobs:
       - name: Checkout source code
         uses: actions/checkout@v3
       - name: Install Rust ${{ matrix.toolchain }} toolchain
-        uses: actions-rs/toolchain@v1
-        with:
-          toolchain: ${{ matrix.toolchain }}
-          override: true
-          profile: minimal
+        run: |
+          curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --profile=minimal --default-toolchain ${{ matrix.toolchain }}
+          rustup override set ${{ matrix.toolchain }}
       - name: Install no-std-check dependencies for ARM Embedded
         if: "matrix.platform == 'ubuntu-latest'"
         run: |
@@ -101,11 +99,9 @@ jobs:
       - name: Checkout source code
         uses: actions/checkout@v3
       - name: Install Rust ${{ env.TOOLCHAIN }} toolchain
-        uses: actions-rs/toolchain@v1
-        with:
-          toolchain: ${{ env.TOOLCHAIN }}
-          override: true
-          profile: minimal
+        run: |
+          curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --profile=minimal --default-toolchain ${{ env.TOOLCHAIN }}
+          rustup override set ${{ env.TOOLCHAIN }}
       - name: Cache routing graph snapshot
         id: cache-graph
         uses: actions/cache@v3
@@ -141,7 +137,12 @@ jobs:
           cd ..
       - name: Run benchmarks on Rust ${{ matrix.toolchain }}
         run: |
-          RUSTC_BOOTSTRAP=1 cargo bench --features _bench_unstable
+          cd bench
+          RUSTFLAGS="--cfg=ldk_bench --cfg=require_route_graph_test" cargo bench
+      - name: Run benchmarks with hashbrown on Rust ${{ matrix.toolchain }}
+        run: |
+          cd bench
+          RUSTFLAGS="--cfg=ldk_bench --cfg=require_route_graph_test" cargo bench --features hashbrown
 
   check_commits:
     runs-on: ubuntu-latest
@@ -153,11 +154,9 @@ jobs:
         with:
           fetch-depth: 0
       - name: Install Rust ${{ env.TOOLCHAIN }} toolchain
-        uses: actions-rs/toolchain@v1
-        with:
-          toolchain: ${{ env.TOOLCHAIN }}
-          override: true
-          profile: minimal
+        run: |
+          curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --profile=minimal --default-toolchain ${{ env.TOOLCHAIN }}
+          rustup override set ${{ env.TOOLCHAIN }}
       - name: Fetch full tree and rebase on upstream
         run: |
           git remote add upstream https://github.com/lightningdevkit/rust-lightning
@@ -178,18 +177,15 @@ jobs:
         with:
           fetch-depth: 0
       - name: Install Rust ${{ env.TOOLCHAIN }} toolchain
-        uses: actions-rs/toolchain@v1
-        with:
-          toolchain: ${{ env.TOOLCHAIN }}
-          override: true
-          profile: minimal
+        run: |
+          curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --profile=minimal --default-toolchain ${{ env.TOOLCHAIN }}
+          rustup override set ${{ env.TOOLCHAIN }}
       - name: Run cargo check for release build.
         run: |
           cargo check --release
           cargo check --no-default-features --features=no-std --release
           cargo check --no-default-features --features=futures --release
           cargo doc --release
-          RUSTDOCFLAGS="--cfg=anchors" cargo doc --release
       - name: Run cargo check for Taproot build.
         run: |
           cargo check --release
@@ -197,22 +193,20 @@ jobs:
           cargo check --no-default-features --features=futures --release
           cargo doc --release
         env:
-          RUSTFLAGS: '--cfg=anchors --cfg=taproot'
-          RUSTDOCFLAGS: '--cfg=anchors --cfg=taproot'
+          RUSTFLAGS: '--cfg=taproot'
+          RUSTDOCFLAGS: '--cfg=taproot'
 
   fuzz:
     runs-on: ubuntu-latest
     env:
-      TOOLCHAIN: stable
+      TOOLCHAIN: 1.58
     steps:
       - name: Checkout source code
         uses: actions/checkout@v3
-      - name: Install Rust 1.58 toolchain
-        uses: actions-rs/toolchain@v1
-        with:
-          toolchain: 1.58
-          override: true
-          profile: minimal
+      - name: Install Rust ${{ env.TOOLCHAIN }} toolchain
+        run: |
+          curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --profile=minimal --default-toolchain ${{ env.TOOLCHAIN }}
+          rustup override set ${{ env.TOOLCHAIN }}
       - name: Install dependencies for honggfuzz
         run: |
           sudo apt-get update
@@ -232,11 +226,9 @@ jobs:
       - name: Checkout source code
         uses: actions/checkout@v3
       - name: Install Rust ${{ env.TOOLCHAIN }} toolchain
-        uses: actions-rs/toolchain@v1
-        with:
-          toolchain: ${{ env.TOOLCHAIN }}
-          override: true
-          profile: minimal
+        run: |
+          curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --profile=minimal --default-toolchain ${{ env.TOOLCHAIN }}
+          rustup override set ${{ env.TOOLCHAIN }}
       - name: Install clippy
         run: |
           rustup component add clippy
index 28e55b41ce6430b1e15417d4debb0b86b10522e0..7a6dc4c793032bc2c1ddf8e3e465201df6cf3543 100644 (file)
@@ -8,7 +8,8 @@ lightning-c-bindings/a.out
 Cargo.lock
 .idea
 lightning/target
-lightning/ldk-net_graph-*.bin
+lightning/net_graph-*.bin
+lightning-rapid-gossip-sync/res/full_graph.lngossip
 lightning-custom-message/target
 lightning-transaction-sync/target
 no-std-check/target
index afc0092c72da50a1a9fecbf8bfb2edf5daa04104..a3acccfdaea91614b98421590dd567d1a678ef58 100644 (file)
@@ -14,6 +14,7 @@ exclude = [
     "lightning-custom-message",
     "lightning-transaction-sync",
     "no-std-check",
+    "bench",
 ]
 
 # Our tests do actual crypto and lots of work, the tradeoff for -O2 is well
@@ -35,8 +36,3 @@ lto = "off"
 opt-level = 3
 lto = true
 panic = "abort"
-
-[profile.bench]
-opt-level = 3
-codegen-units = 1
-lto = true
index 3f6b25fe8b993da805c3a8656527e74a50568fb8..ed19bc544aa9fa0e12fe9e58ce69cd0d746b0bf2 100644 (file)
@@ -17,4 +17,3 @@ your own public key as an attachment or inline for replies.
  * BD6EED4D339EDBF7E7CE7F8836153082BDF676FD (Elias Rohrer)
  * 6E0287D8849AE741E47CC586FD3E106A2CE099B4 (Valentine Wallace)
  * 69CFEA635D0E6E6F13FD9D9136D932FCAC0305F0 (Arik Sosman)
- * A5A6868D7AA91DD00AC1A67F817FFA028EF61C94 (Antoine Riard)
diff --git a/bench/Cargo.toml b/bench/Cargo.toml
new file mode 100644 (file)
index 0000000..e582d29
--- /dev/null
@@ -0,0 +1,25 @@
+[package]
+name = "lightning-bench"
+version = "0.0.1"
+authors = ["Matt Corallo"]
+edition = "2018"
+
+[[bench]]
+name = "bench"
+harness = false
+
+[features]
+hashbrown = ["lightning/hashbrown"]
+
+[dependencies]
+lightning = { path = "../lightning", features = ["_test_utils", "criterion"] }
+lightning-persister = { path = "../lightning-persister", features = ["criterion"] }
+lightning-rapid-gossip-sync = { path = "../lightning-rapid-gossip-sync", features = ["criterion"] }
+criterion = { version = "0.4", default-features = false }
+
+[profile.release]
+opt-level = 3
+codegen-units = 1
+lto = true
+panic = "abort"
+debug = true
diff --git a/bench/README.md b/bench/README.md
new file mode 100644 (file)
index 0000000..7d4cacc
--- /dev/null
@@ -0,0 +1,6 @@
+This crate uses criterion to benchmark various LDK functions.
+
+It can be run as `RUSTFLAGS=--cfg=ldk_bench cargo bench`.
+
+For routing or other HashMap-bottlenecked functions, the `hashbrown` feature
+should also be benchmarked.
diff --git a/bench/benches/bench.rs b/bench/benches/bench.rs
new file mode 100644 (file)
index 0000000..54799f4
--- /dev/null
@@ -0,0 +1,22 @@
+extern crate lightning;
+extern crate lightning_persister;
+
+extern crate criterion;
+
+use criterion::{criterion_group, criterion_main};
+
+criterion_group!(benches,
+       // Note that benches run in the order given here. Thus, they're sorted according to how likely
+       // developers are to be working on the specific code listed, then by runtime.
+       lightning::routing::router::benches::generate_routes_with_zero_penalty_scorer,
+       lightning::routing::router::benches::generate_mpp_routes_with_zero_penalty_scorer,
+       lightning::routing::router::benches::generate_routes_with_probabilistic_scorer,
+       lightning::routing::router::benches::generate_mpp_routes_with_probabilistic_scorer,
+       lightning::routing::router::benches::generate_large_mpp_routes_with_probabilistic_scorer,
+       lightning::sign::benches::bench_get_secure_random_bytes,
+       lightning::ln::channelmanager::bench::bench_sends,
+       lightning_persister::bench::bench_sends,
+       lightning_rapid_gossip_sync::bench::bench_reading_full_graph_from_file,
+       lightning::routing::gossip::benches::read_network_graph,
+       lightning::routing::gossip::benches::write_network_graph);
+criterion_main!(benches);
index f948cc0d63e925f94b803f2738ed0475f01cbc2b..40e118a2fd05094c965d6fecd4b6ae61505e1769 100755 (executable)
@@ -5,8 +5,15 @@ RUSTC_MINOR_VERSION=$(rustc --version | awk '{ split($2,a,"."); print a[2] }')
 HOST_PLATFORM="$(rustc --version --verbose | grep "host:" | awk '{ print $2 }')"
 
 # Tokio MSRV on versions 1.17 through 1.26 is rustc 1.49. Above 1.26 MSRV is 1.56.
-[ "$RUSTC_MINOR_VERSION" -lt 49 ] && cargo update -p tokio --precise "1.14.0" --verbose
-[[ "$RUSTC_MINOR_VERSION" -gt 48  &&  "$RUSTC_MINOR_VERSION" -lt 56 ]] && cargo update -p tokio --precise "1.26.0" --verbose
+[ "$RUSTC_MINOR_VERSION" -lt 49 ] && cargo update -p tokio --precise "1.14.1" --verbose
+[[ "$RUSTC_MINOR_VERSION" -gt 48  &&  "$RUSTC_MINOR_VERSION" -lt 56 ]] && cargo update -p tokio --precise "1.25.1" --verbose
+
+# Sadly the log crate is always a dependency of tokio until 1.20, and has no reasonable MSRV guarantees
+[ "$RUSTC_MINOR_VERSION" -lt 49 ] && cargo update -p log --precise "0.4.18" --verbose
+
+# The addr2line v0.20 crate (a dependency of `backtrace` starting with 0.3.68) relies on 1.55+
+[ "$RUSTC_MINOR_VERSION" -lt 55 ] && cargo update -p backtrace --precise "0.3.67" --verbose
+
 [ "$LDK_COVERAGE_BUILD" != "" ] && export RUSTFLAGS="-C link-dead-code"
 
 export RUST_BACKTRACE=1
@@ -97,9 +104,7 @@ if [ "$RUSTC_MINOR_VERSION" -gt 55 ]; then
        popd
 fi
 
-echo -e "\n\nTest anchors builds"
-pushd lightning
-RUSTFLAGS="$RUSTFLAGS --cfg=anchors" cargo test --verbose --color always -p lightning
 echo -e "\n\nTest Taproot builds"
-RUSTFLAGS="$RUSTFLAGS --cfg=anchors --cfg=taproot" cargo test --verbose --color always -p lightning
+pushd lightning
+RUSTFLAGS="$RUSTFLAGS --cfg=taproot" cargo test --verbose --color always -p lightning
 popd
index 7d507fa431f664eadc163eb802db41c0d3ac6c8a..8f2c30ffa0bc4b3cc0b0656e62b10d523c39e0ca 100644 (file)
@@ -100,7 +100,7 @@ impl Router for FuzzRouter {
 
 pub struct TestBroadcaster {}
 impl BroadcasterInterface for TestBroadcaster {
-       fn broadcast_transaction(&self, _tx: &Transaction) { }
+       fn broadcast_transactions(&self, _txs: &[&Transaction]) { }
 }
 
 pub struct VecWriter(pub Vec<u8>);
@@ -285,7 +285,7 @@ impl KeyProvider {
 }
 
 #[inline]
-fn check_api_err(api_err: APIError) {
+fn check_api_err(api_err: APIError, sendable_bounds_violated: bool) {
        match api_err {
                APIError::APIMisuseError { .. } => panic!("We can't misuse the API"),
                APIError::FeeRateTooHigh { .. } => panic!("We can't send too much fee?"),
@@ -296,15 +296,11 @@ fn check_api_err(api_err: APIError) {
                        // is probably just stale and you should add new messages here.
                        match err.as_str() {
                                "Peer for first hop currently disconnected" => {},
-                               _ if err.starts_with("Cannot push more than their max accepted HTLCs ") => {},
-                               _ if err.starts_with("Cannot send value that would put us over the max HTLC value in flight our peer will accept ") => {},
-                               _ if err.starts_with("Cannot send value that would put our balance under counterparty-announced channel reserve value") => {},
-                               _ if err.starts_with("Cannot send value that would put counterparty balance under holder-announced channel reserve value") => {},
-                               _ if err.starts_with("Cannot send value that would overdraw remaining funds.") => {},
-                               _ if err.starts_with("Cannot send value that would not leave enough to pay for fees.") => {},
-                               _ if err.starts_with("Cannot send value that would put our exposure to dust HTLCs at") => {},
+                               _ if err.starts_with("Cannot send less than our next-HTLC minimum - ") => {},
+                               _ if err.starts_with("Cannot send more than our next-HTLC maximum - ") => {},
                                _ => panic!("{}", err),
                        }
+                       assert!(sendable_bounds_violated);
                },
                APIError::MonitorUpdateInProgress => {
                        // We can (obviously) temp-fail a monitor update
@@ -313,17 +309,17 @@ fn check_api_err(api_err: APIError) {
        }
 }
 #[inline]
-fn check_payment_err(send_err: PaymentSendFailure) {
+fn check_payment_err(send_err: PaymentSendFailure, sendable_bounds_violated: bool) {
        match send_err {
-               PaymentSendFailure::ParameterError(api_err) => check_api_err(api_err),
+               PaymentSendFailure::ParameterError(api_err) => check_api_err(api_err, sendable_bounds_violated),
                PaymentSendFailure::PathParameterError(per_path_results) => {
-                       for res in per_path_results { if let Err(api_err) = res { check_api_err(api_err); } }
+                       for res in per_path_results { if let Err(api_err) = res { check_api_err(api_err, sendable_bounds_violated); } }
                },
                PaymentSendFailure::AllFailedResendSafe(per_path_results) => {
-                       for api_err in per_path_results { check_api_err(api_err); }
+                       for api_err in per_path_results { check_api_err(api_err, sendable_bounds_violated); }
                },
                PaymentSendFailure::PartialFailure { results, .. } => {
-                       for res in results { if let Err(api_err) = res { check_api_err(api_err); } }
+                       for res in results { if let Err(api_err) = res { check_api_err(api_err, sendable_bounds_violated); } }
                },
                PaymentSendFailure::DuplicatePayment => panic!(),
        }
@@ -351,6 +347,11 @@ fn send_payment(source: &ChanMan, dest: &ChanMan, dest_chan_id: u64, amt: u64, p
        let mut payment_id = [0; 32];
        payment_id[0..8].copy_from_slice(&payment_idx.to_ne_bytes());
        *payment_idx += 1;
+       let (min_value_sendable, max_value_sendable) = source.list_usable_channels()
+               .iter().find(|chan| chan.short_channel_id == Some(dest_chan_id))
+               .map(|chan|
+                       (chan.next_outbound_htlc_minimum_msat, chan.next_outbound_htlc_limit_msat))
+               .unwrap_or((0, 0));
        if let Err(err) = source.send_payment_with_route(&Route {
                paths: vec![Path { hops: vec![RouteHop {
                        pubkey: dest.get_our_node_id(),
@@ -362,9 +363,15 @@ fn send_payment(source: &ChanMan, dest: &ChanMan, dest_chan_id: u64, amt: u64, p
                }], blinded_tail: None }],
                payment_params: None,
        }, payment_hash, RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_id)) {
-               check_payment_err(err);
+               check_payment_err(err, amt > max_value_sendable || amt < min_value_sendable);
                false
-       } else { true }
+       } else {
+               // Note that while the max is a strict upper-bound, we can occasionally send substantially
+               // below the minimum, with some gap which is unusable immediately below the minimum. Thus,
+               // we don't check against min_value_sendable here.
+               assert!(amt <= max_value_sendable);
+               true
+       }
 }
 #[inline]
 fn send_hop_payment(source: &ChanMan, middle: &ChanMan, middle_chan_id: u64, dest: &ChanMan, dest_chan_id: u64, amt: u64, payment_id: &mut u8, payment_idx: &mut u64) -> bool {
@@ -373,13 +380,19 @@ fn send_hop_payment(source: &ChanMan, middle: &ChanMan, middle_chan_id: u64, des
        let mut payment_id = [0; 32];
        payment_id[0..8].copy_from_slice(&payment_idx.to_ne_bytes());
        *payment_idx += 1;
+       let (min_value_sendable, max_value_sendable) = source.list_usable_channels()
+               .iter().find(|chan| chan.short_channel_id == Some(middle_chan_id))
+               .map(|chan|
+                       (chan.next_outbound_htlc_minimum_msat, chan.next_outbound_htlc_limit_msat))
+               .unwrap_or((0, 0));
+       let first_hop_fee = 50_000;
        if let Err(err) = source.send_payment_with_route(&Route {
                paths: vec![Path { hops: vec![RouteHop {
                        pubkey: middle.get_our_node_id(),
                        node_features: middle.node_features(),
                        short_channel_id: middle_chan_id,
                        channel_features: middle.channel_features(),
-                       fee_msat: 50000,
+                       fee_msat: first_hop_fee,
                        cltv_expiry_delta: 100,
                },RouteHop {
                        pubkey: dest.get_our_node_id(),
@@ -391,9 +404,16 @@ fn send_hop_payment(source: &ChanMan, middle: &ChanMan, middle_chan_id: u64, des
                }], blinded_tail: None }],
                payment_params: None,
        }, payment_hash, RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_id)) {
-               check_payment_err(err);
+               let sent_amt = amt + first_hop_fee;
+               check_payment_err(err, sent_amt < min_value_sendable || sent_amt > max_value_sendable);
                false
-       } else { true }
+       } else {
+               // Note that while the max is a strict upper-bound, we can occasionally send substantially
+               // below the minimum, with some gap which is unusable immediately below the minimum. Thus,
+               // we don't check against min_value_sendable here.
+               assert!(amt + first_hop_fee <= max_value_sendable);
+               true
+       }
 }
 
 #[inline]
@@ -416,11 +436,12 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out) {
                        config.channel_config.forwarding_fee_proportional_millionths = 0;
                        config.channel_handshake_config.announced_channel = true;
                        let network = Network::Bitcoin;
+                       let best_block_timestamp = genesis_block(network).header.time;
                        let params = ChainParameters {
                                network,
                                best_block: BestBlock::from_network(network),
                        };
-                       (ChannelManager::new($fee_estimator.clone(), monitor.clone(), broadcast.clone(), &router, Arc::clone(&logger), keys_manager.clone(), keys_manager.clone(), keys_manager.clone(), config, params),
+                       (ChannelManager::new($fee_estimator.clone(), monitor.clone(), broadcast.clone(), &router, Arc::clone(&logger), keys_manager.clone(), keys_manager.clone(), keys_manager.clone(), config, params, best_block_timestamp),
                        monitor, keys_manager)
                } }
        }
@@ -474,8 +495,12 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out) {
        let mut channel_txn = Vec::new();
        macro_rules! make_channel {
                ($source: expr, $dest: expr, $chan_id: expr) => { {
-                       $source.peer_connected(&$dest.get_our_node_id(), &Init { features: $dest.init_features(), remote_network_address: None }, true).unwrap();
-                       $dest.peer_connected(&$source.get_our_node_id(), &Init { features: $source.init_features(), remote_network_address: None }, false).unwrap();
+                       $source.peer_connected(&$dest.get_our_node_id(), &Init {
+                               features: $dest.init_features(), networks: None, remote_network_address: None
+                       }, true).unwrap();
+                       $dest.peer_connected(&$source.get_our_node_id(), &Init {
+                               features: $source.init_features(), networks: None, remote_network_address: None
+                       }, false).unwrap();
 
                        $source.create_channel($dest.get_our_node_id(), 100_000, 42, 0, None).unwrap();
                        let open_channel = {
@@ -1006,15 +1031,23 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out) {
                        },
                        0x0e => {
                                if chan_a_disconnected {
-                                       nodes[0].peer_connected(&nodes[1].get_our_node_id(), &Init { features: nodes[1].init_features(), remote_network_address: None }, true).unwrap();
-                                       nodes[1].peer_connected(&nodes[0].get_our_node_id(), &Init { features: nodes[0].init_features(), remote_network_address: None }, false).unwrap();
+                                       nodes[0].peer_connected(&nodes[1].get_our_node_id(), &Init {
+                                               features: nodes[1].init_features(), networks: None, remote_network_address: None
+                                       }, true).unwrap();
+                                       nodes[1].peer_connected(&nodes[0].get_our_node_id(), &Init {
+                                               features: nodes[0].init_features(), networks: None, remote_network_address: None
+                                       }, false).unwrap();
                                        chan_a_disconnected = false;
                                }
                        },
                        0x0f => {
                                if chan_b_disconnected {
-                                       nodes[1].peer_connected(&nodes[2].get_our_node_id(), &Init { features: nodes[2].init_features(), remote_network_address: None }, true).unwrap();
-                                       nodes[2].peer_connected(&nodes[1].get_our_node_id(), &Init { features: nodes[1].init_features(), remote_network_address: None }, false).unwrap();
+                                       nodes[1].peer_connected(&nodes[2].get_our_node_id(), &Init {
+                                               features: nodes[2].init_features(), networks: None, remote_network_address: None
+                                       }, true).unwrap();
+                                       nodes[2].peer_connected(&nodes[1].get_our_node_id(), &Init {
+                                               features: nodes[1].init_features(), networks: None, remote_network_address: None
+                                       }, false).unwrap();
                                        chan_b_disconnected = false;
                                }
                        },
@@ -1209,13 +1242,21 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out) {
 
                                // Next, make sure peers are all connected to each other
                                if chan_a_disconnected {
-                                       nodes[0].peer_connected(&nodes[1].get_our_node_id(), &Init { features: nodes[1].init_features(), remote_network_address: None }, true).unwrap();
-                                       nodes[1].peer_connected(&nodes[0].get_our_node_id(), &Init { features: nodes[0].init_features(), remote_network_address: None }, false).unwrap();
+                                       nodes[0].peer_connected(&nodes[1].get_our_node_id(), &Init {
+                                               features: nodes[1].init_features(), networks: None, remote_network_address: None
+                                       }, true).unwrap();
+                                       nodes[1].peer_connected(&nodes[0].get_our_node_id(), &Init {
+                                               features: nodes[0].init_features(), networks: None, remote_network_address: None
+                                       }, false).unwrap();
                                        chan_a_disconnected = false;
                                }
                                if chan_b_disconnected {
-                                       nodes[1].peer_connected(&nodes[2].get_our_node_id(), &Init { features: nodes[2].init_features(), remote_network_address: None }, true).unwrap();
-                                       nodes[2].peer_connected(&nodes[1].get_our_node_id(), &Init { features: nodes[1].init_features(), remote_network_address: None }, false).unwrap();
+                                       nodes[1].peer_connected(&nodes[2].get_our_node_id(), &Init {
+                                               features: nodes[2].init_features(), networks: None, remote_network_address: None
+                                       }, true).unwrap();
+                                       nodes[2].peer_connected(&nodes[1].get_our_node_id(), &Init {
+                                               features: nodes[1].init_features(), networks: None, remote_network_address: None
+                                       }, false).unwrap();
                                        chan_b_disconnected = false;
                                }
 
index 5544dbdedf7a1bfa642c63575ad11f0c94a7a89c..9caf91040346c45436671cb0f4c3e74ad1cbb373 100644 (file)
@@ -43,7 +43,7 @@ use lightning::ln::functional_test_utils::*;
 use lightning::routing::gossip::{P2PGossipSync, NetworkGraph};
 use lightning::routing::utxo::UtxoLookup;
 use lightning::routing::router::{InFlightHtlcs, PaymentParameters, Route, RouteParameters, Router};
-use lightning::util::config::UserConfig;
+use lightning::util::config::{UserConfig, MaxDustHTLCExposure};
 use lightning::util::errors::APIError;
 use lightning::util::enforcing_trait_impls::{EnforcingSigner, EnforcementState};
 use lightning::util::logger::Logger;
@@ -144,8 +144,9 @@ struct TestBroadcaster {
        txn_broadcasted: Mutex<Vec<Transaction>>,
 }
 impl BroadcasterInterface for TestBroadcaster {
-       fn broadcast_transaction(&self, tx: &Transaction) {
-               self.txn_broadcasted.lock().unwrap().push(tx.clone());
+       fn broadcast_transactions(&self, txs: &[&Transaction]) {
+               let owned_txs: Vec<Transaction> = txs.iter().map(|tx| (*tx).clone()).collect();
+               self.txn_broadcasted.lock().unwrap().extend(owned_txs);
        }
 }
 
@@ -438,13 +439,15 @@ pub fn do_test(data: &[u8], logger: &Arc<dyn Logger>) {
        });
        let mut config = UserConfig::default();
        config.channel_config.forwarding_fee_proportional_millionths =  slice_to_be32(get_slice!(4));
+       config.channel_config.max_dust_htlc_exposure = MaxDustHTLCExposure::FeeRateMultiplier(5_000_000 / 253);
        config.channel_handshake_config.announced_channel = get_slice!(1)[0] != 0;
        let network = Network::Bitcoin;
+       let best_block_timestamp = genesis_block(network).header.time;
        let params = ChainParameters {
                network,
                best_block: BestBlock::from_network(network),
        };
-       let channelmanager = Arc::new(ChannelManager::new(fee_est.clone(), monitor.clone(), broadcast.clone(), &router, Arc::clone(&logger), keys_manager.clone(), keys_manager.clone(), keys_manager.clone(), config, params));
+       let channelmanager = Arc::new(ChannelManager::new(fee_est.clone(), monitor.clone(), broadcast.clone(), &router, Arc::clone(&logger), keys_manager.clone(), keys_manager.clone(), keys_manager.clone(), config, params, best_block_timestamp));
        // Adding new calls to `EntropySource::get_secure_random_bytes` during startup can change all the
        // keys subsequently generated in this test. Rather than regenerating all the messages manually,
        // it's easier to just increment the counter here so the keys don't change.
@@ -815,6 +818,8 @@ mod tests {
                //
                // 0a - create the funding transaction (client should send funding_created now)
                //
+               // 00fd00fd - Two feerate requests (calculating max dust exposure) (all returning min feerate) (gonna be ingested by FuzzEstimator)
+               //
                // 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
@@ -843,6 +848,8 @@ mod tests {
                // 0300c1 - inbound read from peer id 0 of len 193
                // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ab00000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - end of update_add_htlc from 0 to 1 via client and mac
                //
+               // 00fd - One feerate request (calculating max dust exposure) (all returning min feerate) (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
@@ -857,6 +864,8 @@ mod tests {
                // 07 - process the now-pending HTLC forward
                // - client now sends id 1 update_add_htlc and commitment_signed (CHECK 7: UpdateHTLCs event for node 03020000 with 1 HTLCs for channel 3f000000)
                //
+               // 00fd00fd - Two feerate requests (calculating max dust exposure) (all returning min feerate) (gonna be ingested by FuzzEstimator)
+               //
                // - 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
@@ -900,6 +909,8 @@ mod tests {
                // 0300c1 - inbound read from peer id 0 of len 193
                // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ab00000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - end of update_add_htlc from 0 to 1 via client and mac
                //
+               // 00fd - One feerate request (calculating max dust exposure) (all returning min feerate) (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
@@ -921,6 +932,8 @@ mod tests {
                // - 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
                //
+               // 00fd00fd - Two feerate requests (calculating max dust exposure) (all returning min feerate) (gonna be ingested by FuzzEstimator)
+               //
                // 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
@@ -976,6 +989,8 @@ mod tests {
                // 0300c1 - inbound read from peer id 0 of len 193
                // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff 5300000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - end of update_add_htlc from 0 to 1 via client and mac
                //
+               // 00fd - One feerate request (calculating max dust exposure) (all returning min feerate) (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
@@ -990,6 +1005,8 @@ mod tests {
                // 07 - process the now-pending HTLC forward
                // - client now sends id 1 update_add_htlc and commitment_signed (CHECK 7 duplicate)
                //
+               // 00fd00fd - Two feerate requests (calculating max dust exposure) (all returning min feerate) (gonna be ingested by FuzzEstimator)
+               //
                // 0c007d - connect a block with one transaction of len 125
                // 02000000013a000000000000000000000000000000000000000000000000000000000000000000000000000000800258020000000000002200204b0000000000000000000000000000000000000000000000000000000000000014c0000000000000160014280000000000000000000000000000000000000005000020 - the commitment transaction for channel 3f00000000000000000000000000000000000000000000000000000000000000
                //
@@ -1005,7 +1022,7 @@ mod tests {
                // - 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("01000000000000000000000000000000000000000000000000000000000000000000000001000300000000000000000000000000000000000000000000000000000000000000020300320003000000000000000000000000000000000000000000000000000000000000000203000000000000000000000000000000030012001003000000000000000000000000000000030020001000021aaa0008aaaaaaaaaaaa9aaa030000000000000000000000000000000300120147030000000000000000000000000000000300fe00207500000000000000000000000000000000000000000000000000000000000000ff4f00f805273c1b203bb5ebf8436bfde57b3be8c2f5e95d9491dbb181909679000000000000c35000000000000000000000000000000162ffffffffffffffff00000000000002220000000000000000000000fd000601e3030000000000000000000000000000000000000000000000000000000000000001030000000000000000000000000000000000000000000000000000000000000002030000000000000000000000000000000000000000000000000000000000000003030000000000000000000000000000000000000000000000000000000000000004030059030000000000000000000000000000000000000000000000000000000000000005020900000000000000000000000000000000000000000000000000000000000000010000010210000300000000000000000000000000000000fd00fd0300120084030000000000000000000000000000000300940022ff4f00f805273c1b203bb5ebf8436bfde57b3be8c2f5e95d9491dbb1819096793d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000210100000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000c005e020000000100000000000000000000000000000000000000000000000000000000000000000000000000ffffffff0150c3000000000000220020ae00000000000000000000000000000000000000000000000000000000000000000000000c00000c00000c00000c00000c00000c00000c00000c00000c00000c00000c00000c000003001200430300000000000000000000000000000003005300243d0000000000000000000000000000000000000000000000000000000000000002080000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000010301320003000000000000000000000000000000000000000000000000000000000000000703000000000000000000000000000000030142000302000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003000000000000000000000000000000030112001001000000000000000000000000000000030120001000021aaa0008aaaaaaaaaaaa9aaa01000000000000000000000000000000050103020000000000000000000000000000000000000000000000000000000000000000c3500003e800fd0301120112010000000000000000000000000000000301ff00210000000000000000000000000000000000000000000000000000000000000e05000000000000016200000000004c4b4000000000000003e800000000000003e80000000203f000050300000000000000000000000000000000000000000000000000000000000001000300000000000000000000000000000000000000000000000000000000000002000300000000000000000000000000000000000000000000000000000000000003000300000000000000000000000000000000000000000000000000000000000004000300000000000000000000000000000000000000000000000000000000000005000266000000000000000000000000000003012300000000000000000000000000000000000000010000000000000000000000000000000a03011200620100000000000000000000000000000003017200233a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007c0001000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000b03011200430100000000000000000000000000000003015300243a000000000000000000000000000000000000000000000000000000000000000267000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000003001205ac030000000000000000000000000000000300ff00803d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003e80ff00000000000000000000000000000000000000000000000000000000000000000003f00003000000000000000000000000000000000000000000000000000000000000055511020203e80401a0060800000e00000100000a00000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300c1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffab000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003001200640300000000000000000000000000000003007400843d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030010000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003001200630300000000000000000000000000000003007300853d000000000000000000000000000000000000000000000000000000000000000900000000000000000000000000000000000000000000000000000000000000020b00000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000703011200640100000000000000000000000000000003017400843a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006a000100000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000003011200630100000000000000000000000000000003017300853a00000000000000000000000000000000000000000000000000000000000000660000000000000000000000000000000000000000000000000000000000000002640000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000030112004a0100000000000000000000000000000003015a00823a000000000000000000000000000000000000000000000000000000000000000000000000000000ff008888888888888888888888888888888888888888888888888888888888880100000000000000000000000000000003011200640100000000000000000000000000000003017400843a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000100000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000003011200630100000000000000000000000000000003017300853a0000000000000000000000000000000000000000000000000000000000000067000000000000000000000000000000000000000000000000000000000000000265000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000003001205ac030000000000000000000000000000000300ff00803d0000000000000000000000000000000000000000000000000000000000000000000000000000010000000000003e80ff00000000000000000000000000000000000000000000000000000000000000000003f00003000000000000000000000000000000000000000000000000000000000000055511020203e80401a0060800000e00000100000a00000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300c1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffab000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003001200630300000000000000000000000000000003007300853d000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000020a000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003001200640300000000000000000000000000000003007400843d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c3010000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003001200630300000000000000000000000000000003007300853d000000000000000000000000000000000000000000000000000000000000000b00000000000000000000000000000000000000000000000000000000000000020d00000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000703011200640100000000000000000000000000000003017400843a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000039000100000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000003011200630100000000000000000000000000000003017300853a00000000000000000000000000000000000000000000000000000000000000640000000000000000000000000000000000000000000000000000000000000002700000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000030112002c0100000000000000000000000000000003013c00833a00000000000000000000000000000000000000000000000000000000000000000000000000000100000100000000000000000000000000000003011200640100000000000000000000000000000003017400843a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000039000100000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000003011200630100000000000000000000000000000003017300853a000000000000000000000000000000000000000000000000000000000000006500000000000000000000000000000000000000000000000000000000000000027100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000703001200630300000000000000000000000000000003007300853d000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000020c000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003001200640300000000000000000000000000000003007400843d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000032010000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003001205ac030000000000000000000000000000000300ff00803d00000000000000000000000000000000000000000000000000000000000000000000000000000200000000000b0838ff00000000000000000000000000000000000000000000000000000000000000000003f0000300000000000000000000000000000000000000000000000000000000000005551202030927c00401a0060800000e00000100000a00000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300c1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff53000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003001200a4030000000000000000000000000000000300b400843d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007501000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000006705000000000000000000000000000000000000000000000000000000000000060300000000000000000000000000000003001200630300000000000000000000000000000003007300853d000000000000000000000000000000000000000000000000000000000000000d00000000000000000000000000000000000000000000000000000000000000020f0000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000070c007d02000000013a000000000000000000000000000000000000000000000000000000000000000000000000000000800258020000000000002200204b0000000000000000000000000000000000000000000000000000000000000014c00000000000001600142800000000000000000000000000000000000000050000200c005e0200000001730000000000000000000000000000000000000000000000000000000000000000000000000000000001a701000000000000220020b200000000000000000000000000000000000000000000000000000000000000000000000c00000c00000c00000c00000c000007").unwrap(), &(Arc::clone(&logger) as Arc<dyn Logger>));
+               super::do_test(&::hex::decode("01000000000000000000000000000000000000000000000000000000000000000000000001000300000000000000000000000000000000000000000000000000000000000000020300320003000000000000000000000000000000000000000000000000000000000000000203000000000000000000000000000000030012001003000000000000000000000000000000030020001000021aaa0008aaaaaaaaaaaa9aaa030000000000000000000000000000000300120147030000000000000000000000000000000300fe00207500000000000000000000000000000000000000000000000000000000000000ff4f00f805273c1b203bb5ebf8436bfde57b3be8c2f5e95d9491dbb181909679000000000000c35000000000000000000000000000000162ffffffffffffffff00000000000002220000000000000000000000fd000601e3030000000000000000000000000000000000000000000000000000000000000001030000000000000000000000000000000000000000000000000000000000000002030000000000000000000000000000000000000000000000000000000000000003030000000000000000000000000000000000000000000000000000000000000004030059030000000000000000000000000000000000000000000000000000000000000005020900000000000000000000000000000000000000000000000000000000000000010000010210000300000000000000000000000000000000fd00fd0300120084030000000000000000000000000000000300940022ff4f00f805273c1b203bb5ebf8436bfde57b3be8c2f5e95d9491dbb1819096793d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000210100000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000c005e020000000100000000000000000000000000000000000000000000000000000000000000000000000000ffffffff0150c3000000000000220020ae00000000000000000000000000000000000000000000000000000000000000000000000c00000c00000c00000c00000c00000c00000c00000c00000c00000c00000c00000c000003001200430300000000000000000000000000000003005300243d0000000000000000000000000000000000000000000000000000000000000002080000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000010301320003000000000000000000000000000000000000000000000000000000000000000703000000000000000000000000000000030142000302000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003000000000000000000000000000000030112001001000000000000000000000000000000030120001000021aaa0008aaaaaaaaaaaa9aaa01000000000000000000000000000000050103020000000000000000000000000000000000000000000000000000000000000000c3500003e800fd0301120112010000000000000000000000000000000301ff00210000000000000000000000000000000000000000000000000000000000000e05000000000000016200000000004c4b4000000000000003e800000000000003e80000000203f000050300000000000000000000000000000000000000000000000000000000000001000300000000000000000000000000000000000000000000000000000000000002000300000000000000000000000000000000000000000000000000000000000003000300000000000000000000000000000000000000000000000000000000000004000300000000000000000000000000000000000000000000000000000000000005000266000000000000000000000000000003012300000000000000000000000000000000000000010000000000000000000000000000000a00fd00fd03011200620100000000000000000000000000000003017200233a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007c0001000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000b03011200430100000000000000000000000000000003015300243a000000000000000000000000000000000000000000000000000000000000000267000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000003001205ac030000000000000000000000000000000300ff00803d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003e80ff00000000000000000000000000000000000000000000000000000000000000000003f00003000000000000000000000000000000000000000000000000000000000000055511020203e80401a0060800000e00000100000a00000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300c1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffab000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000fd03001200640300000000000000000000000000000003007400843d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030010000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003001200630300000000000000000000000000000003007300853d000000000000000000000000000000000000000000000000000000000000000900000000000000000000000000000000000000000000000000000000000000020b00000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000700fd00fd03011200640100000000000000000000000000000003017400843a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006a000100000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000003011200630100000000000000000000000000000003017300853a00000000000000000000000000000000000000000000000000000000000000660000000000000000000000000000000000000000000000000000000000000002640000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000030112004a0100000000000000000000000000000003015a00823a000000000000000000000000000000000000000000000000000000000000000000000000000000ff008888888888888888888888888888888888888888888888888888888888880100000000000000000000000000000003011200640100000000000000000000000000000003017400843a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000100000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000003011200630100000000000000000000000000000003017300853a0000000000000000000000000000000000000000000000000000000000000067000000000000000000000000000000000000000000000000000000000000000265000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000003001205ac030000000000000000000000000000000300ff00803d0000000000000000000000000000000000000000000000000000000000000000000000000000010000000000003e80ff00000000000000000000000000000000000000000000000000000000000000000003f00003000000000000000000000000000000000000000000000000000000000000055511020203e80401a0060800000e00000100000a00000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300c1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffab000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000fd03001200630300000000000000000000000000000003007300853d000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000020a000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003001200640300000000000000000000000000000003007400843d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c3010000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003001200630300000000000000000000000000000003007300853d000000000000000000000000000000000000000000000000000000000000000b00000000000000000000000000000000000000000000000000000000000000020d00000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000700fd00fd03011200640100000000000000000000000000000003017400843a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000039000100000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000003011200630100000000000000000000000000000003017300853a00000000000000000000000000000000000000000000000000000000000000640000000000000000000000000000000000000000000000000000000000000002700000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000030112002c0100000000000000000000000000000003013c00833a00000000000000000000000000000000000000000000000000000000000000000000000000000100000100000000000000000000000000000003011200640100000000000000000000000000000003017400843a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000039000100000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000003011200630100000000000000000000000000000003017300853a000000000000000000000000000000000000000000000000000000000000006500000000000000000000000000000000000000000000000000000000000000027100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000703001200630300000000000000000000000000000003007300853d000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000020c000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003001200640300000000000000000000000000000003007400843d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000032010000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003001205ac030000000000000000000000000000000300ff00803d00000000000000000000000000000000000000000000000000000000000000000000000000000200000000000b0838ff00000000000000000000000000000000000000000000000000000000000000000003f0000300000000000000000000000000000000000000000000000000000000000005551202030927c00401a0060800000e00000100000a00000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300c1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff53000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000fd03001200a4030000000000000000000000000000000300b400843d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007501000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000006705000000000000000000000000000000000000000000000000000000000000060300000000000000000000000000000003001200630300000000000000000000000000000003007300853d000000000000000000000000000000000000000000000000000000000000000d00000000000000000000000000000000000000000000000000000000000000020f00000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000700fd00fd0c007d02000000013a000000000000000000000000000000000000000000000000000000000000000000000000000000800258020000000000002200204b0000000000000000000000000000000000000000000000000000000000000014c00000000000001600142800000000000000000000000000000000000000050000200c005e0200000001730000000000000000000000000000000000000000000000000000000000000000000000000000000001a701000000000000220020b200000000000000000000000000000000000000000000000000000000000000000000000c00000c00000c00000c00000c000007").unwrap(), &(Arc::clone(&logger) as Arc<dyn 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 030000000000000000000000000000000000000000000000000000000000000002 for channel ff4f00f805273c1b203bb5ebf8436bfde57b3be8c2f5e95d9491dbb181909679".to_string())), Some(&1)); // 1
index 394d57fcebc04c79d76a3894fda2ea7a8e5308e3..f8fe34f2b8b5d647140af18a53558f7ba07b6fb2 100644 (file)
@@ -97,7 +97,7 @@ fn build_response<'a, T: secp256k1::Signing + secp256k1::Verification>(
                },
        ];
 
-       let payment_paths = paths.into_iter().zip(payinfo.into_iter()).collect();
+       let payment_paths = payinfo.into_iter().zip(paths.into_iter()).collect();
        let payment_hash = PaymentHash([42; 32]);
        invoice_request.respond_with(payment_paths, payment_hash)?.build()
 }
index 5fb2122ced4a5485c6169111cda9a0d3b0aceae5..d323ecb21fb992474fe8a33bc334b30f7bfcc1b3 100644 (file)
@@ -11,7 +11,7 @@ use lightning::ln::script::ShutdownScript;
 use lightning::util::enforcing_trait_impls::EnforcingSigner;
 use lightning::util::logger::Logger;
 use lightning::util::ser::{Readable, Writeable, Writer};
-use lightning::onion_message::{CustomOnionMessageContents, CustomOnionMessageHandler, OnionMessenger};
+use lightning::onion_message::{CustomOnionMessageContents, CustomOnionMessageHandler, Destination, MessageRouter, OffersMessage, OffersMessageHandler, OnionMessagePath, OnionMessenger};
 
 use crate::utils::test_logger;
 
@@ -22,15 +22,20 @@ use std::sync::atomic::{AtomicU64, Ordering};
 /// Actual fuzz test, method signature and name are fixed
 pub fn do_test<L: Logger>(data: &[u8], logger: &L) {
        if let Ok(msg) = <msgs::OnionMessage as Readable>::read(&mut Cursor::new(data)) {
-               let mut secret_bytes = [0; 32];
+               let mut secret_bytes = [1; 32];
                secret_bytes[31] = 2;
                let secret = SecretKey::from_slice(&secret_bytes).unwrap();
                let keys_manager = KeyProvider {
                        node_secret: secret,
                        counter: AtomicU64::new(0),
                };
+               let message_router = TestMessageRouter {};
+               let offers_msg_handler = TestOffersMessageHandler {};
                let custom_msg_handler = TestCustomMessageHandler {};
-               let onion_messenger = OnionMessenger::new(&keys_manager, &keys_manager, logger, &custom_msg_handler);
+               let onion_messenger = OnionMessenger::new(
+                       &keys_manager, &keys_manager, logger, &message_router, &offers_msg_handler,
+                       &custom_msg_handler
+               );
                let mut pk = [2; 33]; pk[1] = 0xff;
                let peer_node_id_not_used = PublicKey::from_slice(&pk).unwrap();
                onion_messenger.handle_onion_message(&peer_node_id_not_used, &msg);
@@ -50,6 +55,27 @@ pub extern "C" fn onion_message_run(data: *const u8, datalen: usize) {
        do_test(unsafe { std::slice::from_raw_parts(data, datalen) }, &logger);
 }
 
+struct TestMessageRouter {}
+
+impl MessageRouter for TestMessageRouter {
+       fn find_path(
+               &self, _sender: PublicKey, _peers: Vec<PublicKey>, destination: Destination
+       ) -> Result<OnionMessagePath, ()> {
+               Ok(OnionMessagePath {
+                       intermediate_nodes: vec![],
+                       destination,
+               })
+       }
+}
+
+struct TestOffersMessageHandler {}
+
+impl OffersMessageHandler for TestOffersMessageHandler {
+       fn handle_message(&self, _message: OffersMessage) -> Option<OffersMessage> {
+               None
+       }
+}
+
 struct TestCustomMessage {}
 
 const CUSTOM_MESSAGE_TYPE: u64 = 4242;
@@ -71,7 +97,9 @@ struct TestCustomMessageHandler {}
 
 impl CustomOnionMessageHandler for TestCustomMessageHandler {
        type CustomMessage = TestCustomMessage;
-       fn handle_custom_message(&self, _msg: Self::CustomMessage) {}
+       fn handle_custom_message(&self, _msg: Self::CustomMessage) -> Option<Self::CustomMessage> {
+               Some(TestCustomMessage {})
+       }
        fn read_custom_message<R: io::Read>(&self, _message_type: u64, buffer: &mut R) -> Result<Option<Self::CustomMessage>, msgs::DecodeError> {
                let mut buf = Vec::new();
                buffer.read_to_end(&mut buf)?;
@@ -165,37 +193,41 @@ mod tests {
 
        #[test]
        fn test_no_onion_message_breakage() {
-               let one_hop_om = "020000000000000000000000000000000000000000000000000000000000000e01055600020000000000000000000000000000000000000000000000000000000000000e0136041095000000000000000000000000000000fd1092202a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e800000000000000000000000000000000000000000000000000000000000000";
+               let one_hop_om = "020000000000000000000000000000000000000000000000000000000000000e01055600020000000000000000000000000000000000000000000000000000000000000e01ae0276020000000000000000000000000000000000000000000000000000000000000002020000000000000000000000000000000000000000000000000000000000000e0101022a0000000000000000000000000000014551231950b75fc4402da1732fc9bebf00109500000000000000000000000000000004106d000000000000000000000000000000fd1092202a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005600000000000000000000000000000000000000000000000000000000000000";
                let logger = TrackingLogger { lines: Mutex::new(HashMap::new()) };
                super::do_test(&::hex::decode(one_hop_om).unwrap(), &logger);
                {
                        let log_entries = logger.lines.lock().unwrap();
                        assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(),
-                                               "Received an onion message with path_id None and no reply_path".to_string())), Some(&1));
+                                               "Received an onion message with path_id None and a reply_path".to_string())), Some(&1));
+                       assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(),
+                                               "Responding to onion message with path_id None".to_string())), Some(&1));
+                       assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(),
+                                               "Failed responding to onion message with path_id None: TooFewBlindedHops".to_string())), Some(&1));
                }
 
-               let two_unblinded_hops_om = "020000000000000000000000000000000000000000000000000000000000000e01055600020000000000000000000000000000000000000000000000000000000000000e0135043304210200000000000000000000000000000000000000000000000000000000000000029500000000000000000000000000000036000000000000000000000000000000000000000000000000000000000000003604104b000000000000000000000000000000fd1092202a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b200000000000000000000000000000000000000000000000000000000000000";
+               let two_unblinded_hops_om = "020000000000000000000000000000000000000000000000000000000000000e01055600020000000000000000000000000000000000000000000000000000000000000e0135043304210202020202020202020202020202020202020202020202020202020202020202026d000000000000000000000000000000eb0000000000000000000000000000000000000000000000000000000000000036041096000000000000000000000000000000fd1092202a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004800000000000000000000000000000000000000000000000000000000000000";
                let logger = TrackingLogger { lines: Mutex::new(HashMap::new()) };
                super::do_test(&::hex::decode(two_unblinded_hops_om).unwrap(), &logger);
                {
                        let log_entries = logger.lines.lock().unwrap();
-                       assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(), "Forwarding an onion message to peer 020000000000000000000000000000000000000000000000000000000000000002".to_string())), Some(&1));
+                       assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(), "Forwarding an onion message to peer 020202020202020202020202020202020202020202020202020202020202020202".to_string())), Some(&1));
                }
 
-               let two_unblinded_two_blinded_om = "020000000000000000000000000000000000000000000000000000000000000e01055600020000000000000000000000000000000000000000000000000000000000000e01350433042102000000000000000000000000000000000000000000000000000000000000000295000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000058045604210200000000000000000000000000000000000000000000000000000000000000020821020000000000000000000000000000000000000000000000000000000000000e014b000000000000000000000000000000b20000000000000000000000000000000000000000000000000000000000000035043304210200000000000000000000000000000000000000000000000000000000000000029500000000000000000000000000000036000000000000000000000000000000000000000000000000000000000000003604104b000000000000000000000000000000fd1092202a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b200000000000000000000000000000000000000000000000000000000000000";
+               let two_unblinded_two_blinded_om = "020000000000000000000000000000000000000000000000000000000000000e01055600020000000000000000000000000000000000000000000000000000000000000e0135043304210202020202020202020202020202020202020202020202020202020202020202026d0000000000000000000000000000009e0000000000000000000000000000000000000000000000000000000000000058045604210203030303030303030303030303030303030303030303030303030303030303020821020000000000000000000000000000000000000000000000000000000000000e0196000000000000000000000000000000e9000000000000000000000000000000000000000000000000000000000000003504330421020404040404040404040404040404040404040404040404040404040404040402ca00000000000000000000000000000042000000000000000000000000000000000000000000000000000000000000003604103f000000000000000000000000000000fd1092202a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004800000000000000000000000000000000000000000000000000000000000000";
                let logger = TrackingLogger { lines: Mutex::new(HashMap::new()) };
                super::do_test(&::hex::decode(two_unblinded_two_blinded_om).unwrap(), &logger);
                {
                        let log_entries = logger.lines.lock().unwrap();
-                       assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(), "Forwarding an onion message to peer 020000000000000000000000000000000000000000000000000000000000000002".to_string())), Some(&1));
+                       assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(), "Forwarding an onion message to peer 020202020202020202020202020202020202020202020202020202020202020202".to_string())), Some(&1));
                }
 
-               let three_blinded_om = "020000000000000000000000000000000000000000000000000000000000000e01055600020000000000000000000000000000000000000000000000000000000000000e013504330421020000000000000000000000000000000000000000000000000000000000000002950000000000000000000000000000006c0000000000000000000000000000000000000000000000000000000000000035043304210200000000000000000000000000000000000000000000000000000000000000024b000000000000000000000000000000ac00000000000000000000000000000000000000000000000000000000000000360410d1000000000000000000000000000000fd1092202a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b200000000000000000000000000000000000000000000000000000000000000";
+               let three_blinded_om = "020000000000000000000000000000000000000000000000000000000000000e01055600020000000000000000000000000000000000000000000000000000000000000e0135043304210202020202020202020202020202020202020202020202020202020202020202026d000000000000000000000000000000b20000000000000000000000000000000000000000000000000000000000000035043304210203030303030303030303030303030303030303030303030303030303030303029600000000000000000000000000000033000000000000000000000000000000000000000000000000000000000000003604104e000000000000000000000000000000fd1092202a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004800000000000000000000000000000000000000000000000000000000000000";
                let logger = TrackingLogger { lines: Mutex::new(HashMap::new()) };
                super::do_test(&::hex::decode(three_blinded_om).unwrap(), &logger);
                {
                        let log_entries = logger.lines.lock().unwrap();
-                       assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(), "Forwarding an onion message to peer 020000000000000000000000000000000000000000000000000000000000000002".to_string())), Some(&1));
+                       assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(), "Forwarding an onion message to peer 020202020202020202020202020202020202020202020202020202020202020202".to_string())), Some(&1));
                }
        }
 }
index 359bbcc739dcc8567b4bc3adb4f05f72679dde52..618b129f9a1e5517fa5dcddb09f739308feee436 100644 (file)
@@ -86,7 +86,7 @@ fn build_response<'a, T: secp256k1::Signing + secp256k1::Verification>(
                },
        ];
 
-       let payment_paths = paths.into_iter().zip(payinfo.into_iter()).collect();
+       let payment_paths = payinfo.into_iter().zip(paths.into_iter()).collect();
        let payment_hash = PaymentHash([42; 32]);
        refund.respond_with(payment_paths, payment_hash, signing_pubkey)?.build()
 }
index 00c53dfe58e5d2ca7ed365bc6381f0de6fb4427b..31732257c3f1a1c099161c8b982f1f5f35d701b2 100644 (file)
@@ -265,10 +265,12 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
                                                                balance_msat: 0,
                                                                outbound_capacity_msat: capacity.saturating_mul(1000),
                                                                next_outbound_htlc_limit_msat: capacity.saturating_mul(1000),
+                                                               next_outbound_htlc_minimum_msat: 0,
                                                                inbound_htlc_minimum_msat: None,
                                                                inbound_htlc_maximum_msat: None,
                                                                config: None,
                                                                feerate_sat_per_1000_weight: None,
+                                                               channel_shutdown_state: Some(channelmanager::ChannelShutdownState::NotShuttingDown),
                                                        });
                                                }
                                                Some(&first_hops_vec[..])
index 1f6509e6910d5451531ca31804769b0a2b3a2249..f16b0015cff5cee17abcbf62d6a19f1ec2720348 100644 (file)
@@ -1,6 +1,6 @@
 [package]
 name = "lightning-background-processor"
-version = "0.0.115"
+version = "0.0.116-alpha1"
 authors = ["Valentine Wallace <vwallace@protonmail.com>"]
 license = "MIT OR Apache-2.0"
 repository = "http://github.com/lightningdevkit/rust-lightning"
@@ -21,11 +21,11 @@ default = ["std"]
 
 [dependencies]
 bitcoin = { version = "0.29.0", default-features = false }
-lightning = { version = "0.0.115", path = "../lightning", default-features = false }
-lightning-rapid-gossip-sync = { version = "0.0.115", path = "../lightning-rapid-gossip-sync", default-features = false }
+lightning = { version = "0.0.116-alpha1", path = "../lightning", default-features = false }
+lightning-rapid-gossip-sync = { version = "0.0.116-alpha1", path = "../lightning-rapid-gossip-sync", default-features = false }
 
 [dev-dependencies]
 tokio = { version = "1.14", features = [ "macros", "rt", "rt-multi-thread", "sync", "time" ] }
-lightning = { version = "0.0.115", path = "../lightning", features = ["_test_utils"] }
-lightning-invoice = { version = "0.23.0", path = "../lightning-invoice" }
-lightning-persister = { version = "0.0.115", path = "../lightning-persister" }
+lightning = { version = "0.0.116-alpha1", path = "../lightning", features = ["_test_utils"] }
+lightning-invoice = { version = "0.24.0-alpha1", path = "../lightning-invoice" }
+lightning-persister = { version = "0.0.116-alpha1", path = "../lightning-persister" }
index fb2082578cb31981ef76747338b16d4fd80409d0..1ed6a2a8345bf8751ad6389e8c306804bedf1e91 100644 (file)
@@ -108,7 +108,7 @@ const PING_TIMER: u64 = 1;
 const NETWORK_PRUNE_TIMER: u64 = 60 * 60;
 
 #[cfg(not(test))]
-const SCORER_PERSIST_TIMER: u64 = 30;
+const SCORER_PERSIST_TIMER: u64 = 60 * 60;
 #[cfg(test)]
 const SCORER_PERSIST_TIMER: u64 = 1;
 
@@ -236,9 +236,11 @@ fn handle_network_graph_update<L: Deref>(
        }
 }
 
+/// Updates scorer based on event and returns whether an update occurred so we can decide whether
+/// to persist.
 fn update_scorer<'a, S: 'static + Deref<Target = SC> + Send + Sync, SC: 'a + WriteableScore<'a>>(
        scorer: &'a S, event: &Event
-) {
+) -> bool {
        let mut score = scorer.lock();
        match event {
                Event::PaymentPathFailed { ref path, short_channel_id: Some(scid), .. } => {
@@ -258,8 +260,9 @@ fn update_scorer<'a, S: 'static + Deref<Target = SC> + Send + Sync, SC: 'a + Wri
                Event::ProbeFailed { path, short_channel_id: Some(scid), .. } => {
                        score.probe_failed(path, *scid);
                },
-               _ => {},
+               _ => return false,
        }
+       true
 }
 
 macro_rules! define_run_body {
@@ -352,9 +355,15 @@ macro_rules! define_run_body {
                        // Note that we want to run a graph prune once not long after startup before
                        // falling back to our usual hourly prunes. This avoids short-lived clients never
                        // pruning their network graph. We run once 60 seconds after startup before
-                       // continuing our normal cadence.
+                       // continuing our normal cadence. For RGS, since 60 seconds is likely too long,
+                       // we prune after an initial sync completes.
                        let prune_timer = if have_pruned { NETWORK_PRUNE_TIMER } else { FIRST_NETWORK_PRUNE_TIMER };
-                       if $timer_elapsed(&mut last_prune_call, prune_timer) {
+                       let prune_timer_elapsed = $timer_elapsed(&mut last_prune_call, prune_timer);
+                       let should_prune = match $gossip_sync {
+                               GossipSync::Rapid(_) => !have_pruned || prune_timer_elapsed,
+                               _ => prune_timer_elapsed,
+                       };
+                       if should_prune {
                                // The network graph must not be pruned while rapid sync completion is pending
                                if let Some(network_graph) = $gossip_sync.prunable_network_graph() {
                                        #[cfg(feature = "std")] {
@@ -616,12 +625,19 @@ where
                let network_graph = gossip_sync.network_graph();
                let event_handler = &event_handler;
                let scorer = &scorer;
+               let logger = &logger;
+               let persister = &persister;
                async move {
                        if let Some(network_graph) = network_graph {
                                handle_network_graph_update(network_graph, &event)
                        }
                        if let Some(ref scorer) = scorer {
-                               update_scorer(scorer, &event);
+                               if update_scorer(scorer, &event) {
+                                       log_trace!(logger, "Persisting scorer after update");
+                                       if let Err(e) = persister.persist_scorer(&scorer) {
+                                               log_error!(logger, "Error: Failed to persist scorer, check your disk and permissions {}", e)
+                                       }
+                               }
                        }
                        event_handler(event).await;
                }
@@ -751,7 +767,12 @@ impl BackgroundProcessor {
                                        handle_network_graph_update(network_graph, &event)
                                }
                                if let Some(ref scorer) = scorer {
-                                       update_scorer(scorer, &event);
+                                       if update_scorer(scorer, &event) {
+                                               log_trace!(logger, "Persisting scorer after update");
+                                               if let Err(e) = persister.persist_scorer(&scorer) {
+                                                       log_error!(logger, "Error: Failed to persist scorer, check your disk and permissions {}", e)
+                                               }
+                                       }
                                }
                                event_handler.handle_event(event);
                        };
@@ -817,7 +838,7 @@ impl Drop for BackgroundProcessor {
 
 #[cfg(all(feature = "std", test))]
 mod tests {
-       use bitcoin::blockdata::constants::genesis_block;
+       use bitcoin::blockdata::constants::{genesis_block, ChainHash};
        use bitcoin::blockdata::locktime::PackedLockTime;
        use bitcoin::blockdata::transaction::{Transaction, TxOut};
        use bitcoin::network::constants::Network;
@@ -864,7 +885,22 @@ mod tests {
                fn disconnect_socket(&mut self) {}
        }
 
-       type ChannelManager = channelmanager::ChannelManager<Arc<ChainMonitor>, Arc<test_utils::TestBroadcaster>, Arc<KeysManager>, Arc<KeysManager>, Arc<KeysManager>, Arc<test_utils::TestFeeEstimator>, Arc<DefaultRouter<Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, Arc<test_utils::TestLogger>, Arc<Mutex<TestScorer>>, (), TestScorer>>, Arc<test_utils::TestLogger>>;
+       type ChannelManager =
+               channelmanager::ChannelManager<
+                       Arc<ChainMonitor>,
+                       Arc<test_utils::TestBroadcaster>,
+                       Arc<KeysManager>,
+                       Arc<KeysManager>,
+                       Arc<KeysManager>,
+                       Arc<test_utils::TestFeeEstimator>,
+                       Arc<DefaultRouter<
+                               Arc<NetworkGraph<Arc<test_utils::TestLogger>>>,
+                               Arc<test_utils::TestLogger>,
+                               Arc<Mutex<TestScorer>>,
+                               (),
+                               TestScorer>
+                       >,
+                       Arc<test_utils::TestLogger>>;
 
        type ChainMonitor = chainmonitor::ChainMonitor<InMemorySigner, Arc<test_utils::TestChainSource>, Arc<test_utils::TestBroadcaster>, Arc<test_utils::TestFeeEstimator>, Arc<test_utils::TestLogger>, Arc<FilesystemPersister>>;
 
@@ -1103,7 +1139,7 @@ mod tests {
        fn create_nodes(num_nodes: usize, persist_dir: &str) -> (String, Vec<Node>) {
                let persist_temp_path = env::temp_dir().join(persist_dir);
                let persist_dir = persist_temp_path.to_string_lossy().to_string();
-               let network = Network::Testnet;
+               let network = Network::Bitcoin;
                let mut nodes = Vec::new();
                for i in 0..num_nodes {
                        let tx_broadcaster = Arc::new(test_utils::TestBroadcaster::new(network));
@@ -1114,18 +1150,18 @@ mod tests {
                        let scorer = Arc::new(Mutex::new(TestScorer::new()));
                        let seed = [i as u8; 32];
                        let router = Arc::new(DefaultRouter::new(network_graph.clone(), logger.clone(), seed, scorer.clone(), ()));
-                       let chain_source = Arc::new(test_utils::TestChainSource::new(Network::Testnet));
+                       let chain_source = Arc::new(test_utils::TestChainSource::new(Network::Bitcoin));
                        let persister = Arc::new(FilesystemPersister::new(format!("{}_persister_{}", &persist_dir, i)));
                        let now = Duration::from_secs(genesis_block.header.time as u64);
                        let keys_manager = Arc::new(KeysManager::new(&seed, now.as_secs(), now.subsec_nanos()));
                        let chain_monitor = Arc::new(chainmonitor::ChainMonitor::new(Some(chain_source.clone()), tx_broadcaster.clone(), logger.clone(), fee_estimator.clone(), persister.clone()));
                        let best_block = BestBlock::from_network(network);
                        let params = ChainParameters { network, best_block };
-                       let manager = Arc::new(ChannelManager::new(fee_estimator.clone(), chain_monitor.clone(), tx_broadcaster.clone(), router.clone(), logger.clone(), keys_manager.clone(), keys_manager.clone(), keys_manager.clone(), UserConfig::default(), params));
+                       let manager = Arc::new(ChannelManager::new(fee_estimator.clone(), chain_monitor.clone(), tx_broadcaster.clone(), router.clone(), logger.clone(), keys_manager.clone(), keys_manager.clone(), keys_manager.clone(), UserConfig::default(), params, genesis_block.header.time));
                        let p2p_gossip_sync = Arc::new(P2PGossipSync::new(network_graph.clone(), Some(chain_source.clone()), logger.clone()));
                        let rapid_gossip_sync = Arc::new(RapidGossipSync::new(network_graph.clone(), logger.clone()));
                        let msg_handler = MessageHandler {
-                               chan_handler: Arc::new(test_utils::TestChannelMessageHandler::new()),
+                               chan_handler: Arc::new(test_utils::TestChannelMessageHandler::new(ChainHash::using_genesis_block(Network::Testnet))),
                                route_handler: Arc::new(test_utils::TestRoutingMessageHandler::new()),
                                onion_message_handler: IgnoringMessageHandler{}, custom_message_handler: IgnoringMessageHandler{}
                        };
@@ -1136,8 +1172,12 @@ mod tests {
 
                for i in 0..num_nodes {
                        for j in (i+1)..num_nodes {
-                               nodes[i].node.peer_connected(&nodes[j].node.get_our_node_id(), &Init { features: nodes[j].node.init_features(), remote_network_address: None }, true).unwrap();
-                               nodes[j].node.peer_connected(&nodes[i].node.get_our_node_id(), &Init { features: nodes[i].node.init_features(), remote_network_address: None }, false).unwrap();
+                               nodes[i].node.peer_connected(&nodes[j].node.get_our_node_id(), &Init {
+                                       features: nodes[j].node.init_features(), networks: None, remote_network_address: None
+                               }, true).unwrap();
+                               nodes[j].node.peer_connected(&nodes[i].node.get_our_node_id(), &Init {
+                                       features: nodes[i].node.init_features(), networks: None, remote_network_address: None
+                               }, false).unwrap();
                        }
                }
 
@@ -1708,6 +1748,10 @@ mod tests {
                if !std::thread::panicking() {
                        bg_processor.stop().unwrap();
                }
+
+               let log_entries = nodes[0].logger.lines.lock().unwrap();
+               let expected_log = "Persisting scorer after update".to_string();
+               assert_eq!(*log_entries.get(&("lightning_background_processor".to_string(), expected_log)).unwrap(), 5);
        }
 
        #[tokio::test]
@@ -1750,6 +1794,10 @@ mod tests {
                let t2 = tokio::spawn(async move {
                        do_test_payment_path_scoring!(nodes, receiver.recv().await);
                        exit_sender.send(()).unwrap();
+
+                       let log_entries = nodes[0].logger.lines.lock().unwrap();
+                       let expected_log = "Persisting scorer after update".to_string();
+                       assert_eq!(*log_entries.get(&("lightning_background_processor".to_string(), expected_log)).unwrap(), 5);
                });
 
                let (r1, r2) = tokio::join!(t1, t2);
index a19c3ff8a9dfc41640d325763c0056b6740fc046..7634bf6e8a3ad73868b0e05cd2e0ee996698af04 100644 (file)
@@ -1,6 +1,6 @@
 [package]
 name = "lightning-block-sync"
-version = "0.0.115"
+version = "0.0.116-alpha1"
 authors = ["Jeffrey Czyz", "Matt Corallo"]
 license = "MIT OR Apache-2.0"
 repository = "http://github.com/lightningdevkit/rust-lightning"
@@ -19,11 +19,11 @@ rpc-client = [ "serde_json", "chunked_transfer" ]
 
 [dependencies]
 bitcoin = "0.29.0"
-lightning = { version = "0.0.115", path = "../lightning" }
+lightning = { version = "0.0.116-alpha1", path = "../lightning" }
 tokio = { version = "1.0", features = [ "io-util", "net", "time" ], optional = true }
 serde_json = { version = "1.0", optional = true }
 chunked_transfer = { version = "1.4", optional = true }
 
 [dev-dependencies]
-lightning = { version = "0.0.115", path = "../lightning", features = ["_test_utils"] }
+lightning = { version = "0.0.116-alpha1", path = "../lightning", features = ["_test_utils"] }
 tokio = { version = "1.14", features = [ "macros", "rt" ] }
index 68aa2a1cb259393545c7637d4a8f08ef4bcce6a9..7fcea58f3b9b4afac3b7589e5dbf220b5350ac15 100644 (file)
@@ -1,6 +1,6 @@
 [package]
 name = "lightning-custom-message"
-version = "0.0.115"
+version = "0.0.116-alpha1"
 authors = ["Jeffrey Czyz"]
 license = "MIT OR Apache-2.0"
 repository = "http://github.com/lightningdevkit/rust-lightning"
@@ -15,4 +15,4 @@ rustdoc-args = ["--cfg", "docsrs"]
 
 [dependencies]
 bitcoin = "0.29.0"
-lightning = { version = "0.0.115", path = "../lightning" }
+lightning = { version = "0.0.116-alpha1", path = "../lightning" }
index 5179fdc148a5158e9e9e91d64371a09a243ada11..25219800cc5e2fa0ea523698e494e2d62ea9dc72 100644 (file)
@@ -1,7 +1,7 @@
 [package]
 name = "lightning-invoice"
 description = "Data structures to parse and serialize BOLT11 lightning invoices"
-version = "0.23.0"
+version = "0.24.0-alpha1"
 authors = ["Sebastian Geisler <sgeisler@wh2.tu-dresden.de>"]
 documentation = "https://docs.rs/lightning-invoice/"
 license = "MIT OR Apache-2.0"
@@ -21,7 +21,7 @@ std = ["bitcoin_hashes/std", "num-traits/std", "lightning/std", "bech32/std"]
 
 [dependencies]
 bech32 = { version = "0.9.0", default-features = false }
-lightning = { version = "0.0.115", path = "../lightning", default-features = false }
+lightning = { version = "0.0.116-alpha1", path = "../lightning", default-features = false }
 secp256k1 = { version = "0.24.0", default-features = false, features = ["recovery", "alloc"] }
 num-traits = { version = "0.2.8", default-features = false }
 bitcoin_hashes = { version = "0.11", default-features = false }
@@ -30,6 +30,6 @@ serde = { version = "1.0.118", optional = true }
 bitcoin = { version = "0.29.0", default-features = false }
 
 [dev-dependencies]
-lightning = { version = "0.0.115", path = "../lightning", default-features = false, features = ["_test_utils"] }
+lightning = { version = "0.0.116-alpha1", path = "../lightning", default-features = false, features = ["_test_utils"] }
 hex = "0.4"
 serde_json = { version = "1"}
index c08a00a0ca23ec18f31fcfbbe1afca9aff6ba57e..bf161dbbf0ebc34e78bf5309761621f4ad1662ee 100644 (file)
@@ -28,8 +28,8 @@ use core::time::Duration;
 /// Pays the given [`Invoice`], retrying if needed based on [`Retry`].
 ///
 /// [`Invoice::payment_hash`] is used as the [`PaymentId`], which ensures idempotency as long
-/// as the payment is still pending. Once the payment completes or fails, you must ensure that
-/// a second payment with the same [`PaymentHash`] is never sent.
+/// as the payment is still pending. If the payment succeeds, you must ensure that a second payment
+/// with the same [`PaymentHash`] is never sent.
 ///
 /// If you wish to use a different payment idempotency token, see [`pay_invoice_with_id`].
 pub fn pay_invoice<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref>(
@@ -82,8 +82,8 @@ where
 /// [`Retry`].
 ///
 /// [`Invoice::payment_hash`] is used as the [`PaymentId`], which ensures idempotency as long
-/// as the payment is still pending. Once the payment completes or fails, you must ensure that
-/// a second payment with the same [`PaymentHash`] is never sent.
+/// as the payment is still pending. If the payment succeeds, you must ensure that a second payment
+/// with the same [`PaymentHash`] is never sent.
 ///
 /// If you wish to use a different payment idempotency token, see
 /// [`pay_zero_value_invoice_with_id`].
@@ -108,7 +108,7 @@ where
 }
 
 /// Pays the given zero-value [`Invoice`] using the given amount and custom idempotency key,
-/// retrying if needed based on [`Retry`].
+/// retrying if needed based on [`Retry`].
 ///
 /// Note that idempotency is only guaranteed as long as the payment is still pending. Once the
 /// payment completes or fails, no idempotency guarantees are made.
@@ -169,7 +169,7 @@ fn expiry_time_from_unix_epoch(invoice: &Invoice) -> Duration {
 }
 
 /// An error that may occur when making a payment.
-#[derive(Clone, Debug)]
+#[derive(Clone, Debug, PartialEq, Eq)]
 pub enum PaymentError {
        /// An error resulting from the provided [`Invoice`] or payment hash.
        Invoice(&'static str),
index b3b7c2b91e8b2702ed5ad693cc8971143b625292..c052706975968105c95b7dbf08c4ec7132b6c6c5 100644 (file)
@@ -18,6 +18,7 @@ use lightning::util::logger::Logger;
 use secp256k1::PublicKey;
 use core::ops::Deref;
 use core::time::Duration;
+use core::iter::Iterator;
 
 /// Utility to create an invoice that can be paid to one of multiple nodes, or a "phantom invoice."
 /// See [`PhantomKeysManager`] for more information on phantom node payments.
@@ -132,6 +133,8 @@ where
        )
 }
 
+const MAX_CHANNEL_HINTS: usize = 3;
+
 fn _create_phantom_invoice<ES: Deref, NS: Deref, L: Deref>(
        amt_msat: Option<u64>, payment_hash: Option<PaymentHash>, description: InvoiceDescription,
        invoice_expiry_delta_secs: u32, phantom_route_hints: Vec<PhantomRouteHints>, entropy_source: ES,
@@ -202,7 +205,8 @@ where
                invoice = invoice.amount_milli_satoshis(amt);
        }
 
-       for route_hint in select_phantom_hints(amt_msat, phantom_route_hints, logger) {
+
+       for route_hint in select_phantom_hints(amt_msat, phantom_route_hints, logger).take(MAX_CHANNEL_HINTS) {
                invoice = invoice.private_route(route_hint);
        }
 
@@ -229,36 +233,48 @@ where
 ///
 /// [`PhantomKeysManager`]: lightning::sign::PhantomKeysManager
 fn select_phantom_hints<L: Deref>(amt_msat: Option<u64>, phantom_route_hints: Vec<PhantomRouteHints>,
-       logger: L) -> Vec<RouteHint>
+       logger: L) -> impl Iterator<Item = RouteHint>
 where
        L::Target: Logger,
 {
-       let mut phantom_hints: Vec<Vec<RouteHint>> = Vec::new();
+       let mut phantom_hints: Vec<_> = Vec::new();
 
        for PhantomRouteHints { channels, phantom_scid, real_node_pubkey } in phantom_route_hints {
                log_trace!(logger, "Generating phantom route hints for node {}",
                        log_pubkey!(real_node_pubkey));
-               let mut route_hints = sort_and_filter_channels(channels, amt_msat, &logger);
+               let route_hints = sort_and_filter_channels(channels, amt_msat, &logger);
 
                // If we have any public channel, the route hints from `sort_and_filter_channels` will be
                // empty. In that case we create a RouteHint on which we will push a single hop with the
                // phantom route into the invoice, and let the sender find the path to the `real_node_pubkey`
                // node by looking at our public channels.
-               if route_hints.is_empty() {
-                       route_hints.push(RouteHint(vec![]))
-               }
-               for route_hint in &mut route_hints {
-                       route_hint.0.push(RouteHintHop {
-                               src_node_id: real_node_pubkey,
-                               short_channel_id: phantom_scid,
-                               fees: RoutingFees {
-                                       base_msat: 0,
-                                       proportional_millionths: 0,
-                               },
-                               cltv_expiry_delta: MIN_CLTV_EXPIRY_DELTA,
-                               htlc_minimum_msat: None,
-                               htlc_maximum_msat: None,});
-               }
+               let empty_route_hints = route_hints.len() == 0;
+               let mut have_pushed_empty = false;
+               let route_hints = route_hints
+                       .chain(core::iter::from_fn(move || {
+                               if empty_route_hints && !have_pushed_empty {
+                                       // set flag of having handled the empty route_hints and ensure empty vector
+                                       // returned only once
+                                       have_pushed_empty = true;
+                                       Some(RouteHint(Vec::new()))
+                               } else {
+                                       None
+                               }
+                       }))
+                       .map(move |mut hint| {
+                               hint.0.push(RouteHintHop {
+                                       src_node_id: real_node_pubkey,
+                                       short_channel_id: phantom_scid,
+                                       fees: RoutingFees {
+                                               base_msat: 0,
+                                               proportional_millionths: 0,
+                                       },
+                                       cltv_expiry_delta: MIN_CLTV_EXPIRY_DELTA,
+                                       htlc_minimum_msat: None,
+                                       htlc_maximum_msat: None,
+                               });
+                               hint
+                       });
 
                phantom_hints.push(route_hints);
        }
@@ -267,29 +283,34 @@ where
        // the hints across our real nodes we add one hint from each in turn until no node has any hints
        // left (if one node has more hints than any other, these will accumulate at the end of the
        // vector).
-       let mut invoice_hints: Vec<RouteHint> = Vec::new();
-       let mut hint_idx = 0;
+       rotate_through_iterators(phantom_hints)
+}
 
-       loop {
-               let mut remaining_hints = false;
+/// Draw items iteratively from multiple iterators.  The items are retrieved by index and
+/// rotates through the iterators - first the zero index then the first index then second index, etc.
+fn rotate_through_iterators<T, I: Iterator<Item = T>>(mut vecs: Vec<I>) -> impl Iterator<Item = T> {
+       let mut iterations = 0;
 
-               for hints in phantom_hints.iter() {
-                       if invoice_hints.len() == 3 {
-                               return invoice_hints
+       core::iter::from_fn(move || {
+               let mut exhausted_iterators = 0;
+               loop {
+                       if vecs.is_empty() {
+                               return None;
                        }
-
-                       if hint_idx < hints.len() {
-                               invoice_hints.push(hints[hint_idx].clone());
-                               remaining_hints = true
+                       let next_idx = iterations % vecs.len();
+                       iterations += 1;
+                       if let Some(item) = vecs[next_idx].next() {
+                               return Some(item);
+                       }
+                       // exhausted_vectors increase when the "next_idx" vector is exhausted
+                       exhausted_iterators += 1;
+                       // The check for exhausted iterators gets reset to 0 after each yield of `Some()`
+                       // The loop will return None when all of the nested iterators are exhausted
+                       if exhausted_iterators == vecs.len() {
+                               return None;
                        }
                }
-
-               if !remaining_hints {
-                       return invoice_hints
-               }
-
-               hint_idx +=1;
-       }
+       })
 }
 
 #[cfg(feature = "std")]
@@ -575,8 +596,13 @@ fn _create_invoice_from_channelmanager_and_duration_since_epoch_with_payment_has
 /// * Sorted by lowest inbound capacity if an online channel with the minimum amount requested exists,
 ///   otherwise sort by highest inbound capacity to give the payment the best chance of succeeding.
 fn sort_and_filter_channels<L: Deref>(
-       channels: Vec<ChannelDetails>, min_inbound_capacity_msat: Option<u64>, logger: &L
-) -> Vec<RouteHint> where L::Target: Logger {
+       channels: Vec<ChannelDetails>,
+       min_inbound_capacity_msat: Option<u64>,
+       logger: &L,
+) -> impl ExactSizeIterator<Item = RouteHint>
+where
+       L::Target: Logger,
+{
        let mut filtered_channels: HashMap<PublicKey, ChannelDetails> = HashMap::new();
        let min_inbound_capacity = min_inbound_capacity_msat.unwrap_or(0);
        let mut min_capacity_channel_exists = false;
@@ -584,6 +610,20 @@ fn sort_and_filter_channels<L: Deref>(
        let mut online_min_capacity_channel_exists = false;
        let mut has_pub_unconf_chan = false;
 
+       let route_hint_from_channel = |channel: ChannelDetails| {
+               let forwarding_info = channel.counterparty.forwarding_info.as_ref().unwrap();
+               RouteHint(vec![RouteHintHop {
+                       src_node_id: channel.counterparty.node_id,
+                       short_channel_id: channel.get_inbound_payment_scid().unwrap(),
+                       fees: RoutingFees {
+                               base_msat: forwarding_info.fee_base_msat,
+                               proportional_millionths: forwarding_info.fee_proportional_millionths,
+                       },
+                       cltv_expiry_delta: forwarding_info.cltv_expiry_delta,
+                       htlc_minimum_msat: channel.inbound_htlc_minimum_msat,
+                       htlc_maximum_msat: channel.inbound_htlc_maximum_msat,}])
+       };
+
        log_trace!(logger, "Considering {} channels for invoice route hints", channels.len());
        for channel in channels.into_iter().filter(|chan| chan.is_channel_ready) {
                if channel.get_inbound_payment_scid().is_none() || channel.counterparty.forwarding_info.is_none() {
@@ -602,7 +642,7 @@ fn sort_and_filter_channels<L: Deref>(
                                // look at the public channels instead.
                                log_trace!(logger, "Not including channels in invoice route hints on account of public channel {}",
                                        log_bytes!(channel.channel_id));
-                               return vec![]
+                               return vec![].into_iter().take(MAX_CHANNEL_HINTS).map(route_hint_from_channel);
                        }
                }
 
@@ -662,19 +702,6 @@ fn sort_and_filter_channels<L: Deref>(
                }
        }
 
-       let route_hint_from_channel = |channel: ChannelDetails| {
-               let forwarding_info = channel.counterparty.forwarding_info.as_ref().unwrap();
-               RouteHint(vec![RouteHintHop {
-                       src_node_id: channel.counterparty.node_id,
-                       short_channel_id: channel.get_inbound_payment_scid().unwrap(),
-                       fees: RoutingFees {
-                               base_msat: forwarding_info.fee_base_msat,
-                               proportional_millionths: forwarding_info.fee_proportional_millionths,
-                       },
-                       cltv_expiry_delta: forwarding_info.cltv_expiry_delta,
-                       htlc_minimum_msat: channel.inbound_htlc_minimum_msat,
-                       htlc_maximum_msat: channel.inbound_htlc_maximum_msat,}])
-       };
        // If all channels are private, prefer to return route hints which have a higher capacity than
        // the payment value and where we're currently connected to the channel counterparty.
        // Even if we cannot satisfy both goals, always ensure we include *some* hints, preferring
@@ -724,7 +751,8 @@ fn sort_and_filter_channels<L: Deref>(
                        } else {
                                b.inbound_capacity_msat.cmp(&a.inbound_capacity_msat)
                        }});
-               eligible_channels.into_iter().take(3).map(route_hint_from_channel).collect::<Vec<RouteHint>>()
+
+               eligible_channels.into_iter().take(MAX_CHANNEL_HINTS).map(route_hint_from_channel)
 }
 
 /// prefer_current_channel chooses a channel to use for route hints between a currently selected and candidate
@@ -777,7 +805,7 @@ mod test {
        use lightning::routing::router::{PaymentParameters, RouteParameters};
        use lightning::util::test_utils;
        use lightning::util::config::UserConfig;
-       use crate::utils::create_invoice_from_channelmanager_and_duration_since_epoch;
+       use crate::utils::{create_invoice_from_channelmanager_and_duration_since_epoch, rotate_through_iterators};
        use std::collections::HashSet;
 
        #[test]
@@ -1271,13 +1299,14 @@ mod test {
                } else {
                        None
                };
+               let genesis_timestamp = bitcoin::blockdata::constants::genesis_block(bitcoin::Network::Testnet).header.time as u64;
                let non_default_invoice_expiry_secs = 4200;
 
                let invoice =
                        crate::utils::create_phantom_invoice::<&test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestLogger>(
                                Some(payment_amt), payment_hash, "test".to_string(), non_default_invoice_expiry_secs,
                                route_hints, nodes[1].keys_manager, nodes[1].keys_manager, nodes[1].logger,
-                               Currency::BitcoinTestnet, None, Duration::from_secs(1234567)
+                               Currency::BitcoinTestnet, None, Duration::from_secs(genesis_timestamp)
                        ).unwrap();
                let (payment_hash, payment_secret) = (PaymentHash(invoice.payment_hash().into_inner()), *invoice.payment_secret());
                let payment_preimage = if user_generated_pmt_hash {
@@ -1886,4 +1915,111 @@ mod test {
                        _ => panic!(),
                }
        }
+
+       #[test]
+       fn test_rotate_through_iterators() {
+               // two nested vectors
+               let a = vec![vec!["a0", "b0", "c0"].into_iter(), vec!["a1", "b1"].into_iter()];
+               let result = rotate_through_iterators(a).collect::<Vec<_>>();
+
+               let expected = vec!["a0", "a1", "b0", "b1", "c0"];
+               assert_eq!(expected, result);
+
+               // test single nested vector
+               let a = vec![vec!["a0", "b0", "c0"].into_iter()];
+               let result = rotate_through_iterators(a).collect::<Vec<_>>();
+
+               let expected = vec!["a0", "b0", "c0"];
+               assert_eq!(expected, result);
+
+               // test second vector with only one element
+               let a = vec![vec!["a0", "b0", "c0"].into_iter(), vec!["a1"].into_iter()];
+               let result = rotate_through_iterators(a).collect::<Vec<_>>();
+
+               let expected = vec!["a0", "a1", "b0", "c0"];
+               assert_eq!(expected, result);
+
+               // test three nestend vectors
+               let a = vec![vec!["a0"].into_iter(), vec!["a1", "b1", "c1"].into_iter(), vec!["a2"].into_iter()];
+               let result = rotate_through_iterators(a).collect::<Vec<_>>();
+
+               let expected = vec!["a0", "a1", "a2", "b1", "c1"];
+               assert_eq!(expected, result);
+
+               // test single nested vector with a single value
+               let a = vec![vec!["a0"].into_iter()];
+               let result = rotate_through_iterators(a).collect::<Vec<_>>();
+
+               let expected = vec!["a0"];
+               assert_eq!(expected, result);
+
+               // test single empty nested vector
+               let a:Vec<std::vec::IntoIter<&str>> = vec![vec![].into_iter()];
+               let result = rotate_through_iterators(a).collect::<Vec<&str>>();
+               let expected:Vec<&str> = vec![];
+
+               assert_eq!(expected, result);
+
+               // test first nested vector is empty
+               let a:Vec<std::vec::IntoIter<&str>>= vec![vec![].into_iter(), vec!["a1", "b1", "c1"].into_iter()];
+               let result = rotate_through_iterators(a).collect::<Vec<&str>>();
+
+               let expected = vec!["a1", "b1", "c1"];
+               assert_eq!(expected, result);
+
+               // test two empty vectors
+               let a:Vec<std::vec::IntoIter<&str>> = vec![vec![].into_iter(), vec![].into_iter()];
+               let result = rotate_through_iterators(a).collect::<Vec<&str>>();
+
+               let expected:Vec<&str> = vec![];
+               assert_eq!(expected, result);
+
+               // test an empty vector amongst other filled vectors
+               let a = vec![
+                       vec!["a0", "b0", "c0"].into_iter(),
+                       vec![].into_iter(),
+                       vec!["a1", "b1", "c1"].into_iter(),
+                       vec!["a2", "b2", "c2"].into_iter(),
+               ];
+               let result = rotate_through_iterators(a).collect::<Vec<_>>();
+
+               let expected = vec!["a0", "a1", "a2", "b0", "b1", "b2", "c0", "c1", "c2"];
+               assert_eq!(expected, result);
+
+               // test a filled vector between two empty vectors
+               let a = vec![vec![].into_iter(), vec!["a1", "b1", "c1"].into_iter(), vec![].into_iter()];
+               let result = rotate_through_iterators(a).collect::<Vec<_>>();
+
+               let expected = vec!["a1", "b1", "c1"];
+               assert_eq!(expected, result);
+
+               // test an empty vector at the end of the vectors
+               let a = vec![vec!["a0", "b0", "c0"].into_iter(), vec![].into_iter()];
+               let result = rotate_through_iterators(a).collect::<Vec<_>>();
+
+               let expected = vec!["a0", "b0", "c0"];
+               assert_eq!(expected, result);
+
+               // test multiple empty vectors amongst multiple filled vectors
+               let a = vec![
+                       vec![].into_iter(),
+                       vec!["a1", "b1", "c1"].into_iter(),
+                       vec![].into_iter(),
+                       vec!["a3", "b3"].into_iter(),
+                       vec![].into_iter(),
+               ];
+
+               let result = rotate_through_iterators(a).collect::<Vec<_>>();
+
+               let expected = vec!["a1", "a3", "b1", "b3", "c1"];
+               assert_eq!(expected, result);
+
+               // test one element in the first nested vectore and two elements in the second nested
+               // vector
+               let a = vec![vec!["a0"].into_iter(), vec!["a1", "b1"].into_iter()];
+               let result = rotate_through_iterators(a).collect::<Vec<_>>();
+
+               let expected = vec!["a0", "a1", "b1"];
+               assert_eq!(expected, result);
+       }
 }
index a0fdcf8399d8865134c00ced502d0bc476fe3575..b729061e64d96e1f60c7c134f110988b6d6ca4d2 100644 (file)
@@ -1,6 +1,6 @@
 [package]
 name = "lightning-net-tokio"
-version = "0.0.115"
+version = "0.0.116-alpha1"
 authors = ["Matt Corallo"]
 license = "MIT OR Apache-2.0"
 repository = "https://github.com/lightningdevkit/rust-lightning/"
@@ -16,9 +16,9 @@ rustdoc-args = ["--cfg", "docsrs"]
 
 [dependencies]
 bitcoin = "0.29.0"
-lightning = { version = "0.0.115", path = "../lightning" }
+lightning = { version = "0.0.116-alpha1", path = "../lightning" }
 tokio = { version = "1.0", features = [ "io-util", "macros", "rt", "sync", "net", "time" ] }
 
 [dev-dependencies]
 tokio = { version = "1.14", features = [ "io-util", "macros", "rt", "rt-multi-thread", "sync", "net", "time" ] }
-lightning = { version = "0.0.115", path = "../lightning", features = ["_test_utils"] }
+lightning = { version = "0.0.116-alpha1", path = "../lightning", features = ["_test_utils"] }
index 2f0c96396bf92f69d2c9f88e4a1526c4990c96d7..724a57fb15c19bb1ba9c9c09bd3fbe44fc3a6478 100644 (file)
@@ -474,6 +474,8 @@ mod tests {
        use lightning::routing::gossip::NodeId;
        use lightning::events::*;
        use lightning::util::test_utils::TestNodeSigner;
+       use bitcoin::Network;
+       use bitcoin::blockdata::constants::ChainHash;
        use bitcoin::secp256k1::{Secp256k1, SecretKey, PublicKey};
 
        use tokio::sync::mpsc;
@@ -556,6 +558,9 @@ mod tests {
                fn handle_error(&self, _their_node_id: &PublicKey, _msg: &ErrorMessage) {}
                fn provided_node_features(&self) -> NodeFeatures { NodeFeatures::empty() }
                fn provided_init_features(&self, _their_node_id: &PublicKey) -> InitFeatures { InitFeatures::empty() }
+               fn get_genesis_hashes(&self) -> Option<Vec<ChainHash>> {
+                       Some(vec![ChainHash::using_genesis_block(Network::Testnet)])
+               }
        }
        impl MessageSendEventsProvider for MsgHandler {
                fn get_and_clear_pending_msg_events(&self) -> Vec<MessageSendEvent> {
index 88132bdcfd660ed70e367ed63c420a08244e408c..9ef734285871911197140323d9b4d6aff3add652 100644 (file)
@@ -1,6 +1,6 @@
 [package]
 name = "lightning-persister"
-version = "0.0.115"
+version = "0.0.116-alpha1"
 authors = ["Valentine Wallace", "Matt Corallo"]
 license = "MIT OR Apache-2.0"
 repository = "https://github.com/lightningdevkit/rust-lightning/"
@@ -13,16 +13,16 @@ edition = "2018"
 all-features = true
 rustdoc-args = ["--cfg", "docsrs"]
 
-[features]
-_bench_unstable = ["lightning/_bench_unstable"]
-
 [dependencies]
 bitcoin = "0.29.0"
-lightning = { version = "0.0.115", path = "../lightning" }
+lightning = { version = "0.0.116-alpha1", path = "../lightning" }
 libc = "0.2"
 
 [target.'cfg(windows)'.dependencies]
 winapi = { version = "0.3", features = ["winbase"] }
 
+[target.'cfg(ldk_bench)'.dependencies]
+criterion = { version = "0.4", optional = true, default-features = false }
+
 [dev-dependencies]
-lightning = { version = "0.0.115", path = "../lightning", features = ["_test_utils"] }
+lightning = { version = "0.0.116-alpha1", path = "../lightning", features = ["_test_utils"] }
index d1a1e4a299031a352564728200cf1829b66c2f89..670a7369d8b92c7bb415125f3dc49186c4b2f3d3 100644 (file)
@@ -8,8 +8,7 @@
 
 #![cfg_attr(docsrs, feature(doc_auto_cfg))]
 
-#![cfg_attr(all(test, feature = "_bench_unstable"), feature(test))]
-#[cfg(all(test, feature = "_bench_unstable"))] extern crate test;
+#[cfg(ldk_bench)] extern crate criterion;
 
 mod util;
 
@@ -91,13 +90,13 @@ impl FilesystemPersister {
                                continue;
                        }
 
-                       let txid = Txid::from_hex(filename.split_at(64).0)
+                       let txid: Txid = Txid::from_hex(filename.split_at(64).0)
                                .map_err(|_| std::io::Error::new(
                                        std::io::ErrorKind::InvalidData,
                                        "Invalid tx ID in filename",
                                ))?;
 
-                       let index = filename.split_at(65).1.parse()
+                       let index: u16 = filename.split_at(65).1.parse()
                                .map_err(|_| std::io::Error::new(
                                        std::io::ErrorKind::InvalidData,
                                        "Invalid tx index in filename",
@@ -335,14 +334,16 @@ mod tests {
        }
 }
 
-#[cfg(all(test, feature = "_bench_unstable"))]
+#[cfg(ldk_bench)]
+/// Benches
 pub mod bench {
-       use test::Bencher;
+       use criterion::Criterion;
 
-       #[bench]
-       fn bench_sends(bench: &mut Bencher) {
+       /// Bench!
+       pub fn bench_sends(bench: &mut Criterion) {
                let persister_a = super::FilesystemPersister::new("bench_filesystem_persister_a".to_string());
                let persister_b = super::FilesystemPersister::new("bench_filesystem_persister_b".to_string());
-               lightning::ln::channelmanager::bench::bench_two_sends(bench, persister_a, persister_b);
+               lightning::ln::channelmanager::bench::bench_two_sends(
+                       bench, "bench_filesystem_persisted_sends", persister_a, persister_b);
        }
 }
index 6673431d93b210d24c87ac42e9c9561b4728d66b..7431c53f436db445b8393b47992c3f593a610730 100644 (file)
@@ -1,6 +1,6 @@
 [package]
 name = "lightning-rapid-gossip-sync"
-version = "0.0.115"
+version = "0.0.116-alpha1"
 authors = ["Arik Sosman <git@arik.io>"]
 license = "MIT OR Apache-2.0"
 repository = "https://github.com/lightningdevkit/rust-lightning"
@@ -13,11 +13,13 @@ Utility to process gossip routing data from Rapid Gossip Sync Server.
 default = ["std"]
 no-std = ["lightning/no-std"]
 std = ["lightning/std"]
-_bench_unstable = []
 
 [dependencies]
-lightning = { version = "0.0.115", path = "../lightning", default-features = false }
+lightning = { version = "0.0.116-alpha1", path = "../lightning", default-features = false }
 bitcoin = { version = "0.29.0", default-features = false }
 
+[target.'cfg(ldk_bench)'.dependencies]
+criterion = { version = "0.4", optional = true, default-features = false }
+
 [dev-dependencies]
-lightning = { version = "0.0.115", path = "../lightning", features = ["_test_utils"] }
+lightning = { version = "0.0.116-alpha1", path = "../lightning", features = ["_test_utils"] }
index c8f140cc18955d8225bbf300fcda2aee6ddae267..5a61be7990e2a17270b7c61deeb8944952255735 100644 (file)
 
 #![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
 
-// Allow and import test features for benching
-#![cfg_attr(all(test, feature = "_bench_unstable"), feature(test))]
-#[cfg(all(test, feature = "_bench_unstable"))]
-extern crate test;
+#[cfg(ldk_bench)] extern crate criterion;
 
 #[cfg(not(feature = "std"))]
 extern crate alloc;
@@ -287,36 +284,42 @@ mod tests {
        }
 }
 
-#[cfg(all(test, feature = "_bench_unstable"))]
+#[cfg(ldk_bench)]
+/// Benches
 pub mod bench {
-       use test::Bencher;
-
        use bitcoin::Network;
 
-       use lightning::ln::msgs::DecodeError;
+       use criterion::Criterion;
+
+       use std::fs;
+
        use lightning::routing::gossip::NetworkGraph;
        use lightning::util::test_utils::TestLogger;
 
        use crate::RapidGossipSync;
 
-       #[bench]
-       fn bench_reading_full_graph_from_file(b: &mut Bencher) {
+       /// Bench!
+       pub fn bench_reading_full_graph_from_file(b: &mut Criterion) {
                let logger = TestLogger::new();
-               b.iter(|| {
+               b.bench_function("read_full_graph_from_rgs", |b| b.iter(|| {
                        let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
                        let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
-                       let sync_result = rapid_sync.sync_network_graph_with_file_path("./res/full_graph.lngossip");
-                       if let Err(crate::error::GraphSyncError::DecodeError(DecodeError::Io(io_error))) = &sync_result {
-                               let error_string = format!("Input file lightning-rapid-gossip-sync/res/full_graph.lngossip is missing! Download it from https://bitcoin.ninja/ldk-compressed_graph-bc08df7542-2022-05-05.bin\n\n{:?}", io_error);
-                               #[cfg(not(require_route_graph_test))]
-                               {
-                                       println!("{}", error_string);
-                                       return;
-                               }
-                               #[cfg(require_route_graph_test)]
-                               panic!("{}", error_string);
-                       }
-                       assert!(sync_result.is_ok())
-               });
+                       let mut file = match fs::read("../lightning-rapid-gossip-sync/res/full_graph.lngossip") {
+                               Ok(f) => f,
+                               Err(io_error) => {
+                                       let error_string = format!(
+                                               "Input file lightning-rapid-gossip-sync/res/full_graph.lngossip is missing! Download it from https://bitcoin.ninja/ldk-compressed_graph-bc08df7542-2022-05-05.bin\n\n{:?}",
+                                               io_error);
+                                       #[cfg(not(require_route_graph_test))]
+                                       {
+                                               println!("{}", error_string);
+                                               return;
+                                       }
+                                       #[cfg(require_route_graph_test)]
+                                       panic!("{}", error_string);
+                               },
+                       };
+                       rapid_sync.update_network_graph_no_std(&mut file, None).unwrap();
+               }));
        }
 }
index b387d49998e9a38d6f1693e1a18d05c4da57e675..fca318a5fa5f8cfac735ff8d3880819e8b6d93a0 100644 (file)
@@ -68,6 +68,16 @@ impl<NG: Deref<Target=NetworkGraph<L>>, L: Deref> RapidGossipSync<NG, L> where L
                }
 
                let chain_hash: BlockHash = Readable::read(read_cursor)?;
+               let ng_genesis_hash = self.network_graph.get_genesis_hash();
+               if chain_hash != ng_genesis_hash {
+                       return Err(
+                               LightningError {
+                                       err: "Rapid Gossip Sync data's chain hash does not match the network graph's".to_owned(),
+                                       action: ErrorAction::IgnoreError,
+                               }.into()
+                       );
+               }
+
                let latest_seen_timestamp: u32 = Readable::read(read_cursor)?;
 
                if let Some(time) = current_time_unix {
@@ -667,4 +677,22 @@ mod tests {
                        panic!("Unexpected update result: {:?}", update_result)
                }
        }
+
+       #[test]
+       fn fails_early_on_chain_hash_mismatch() {
+               let logger = TestLogger::new();
+               // Set to testnet so that the VALID_RGS_BINARY chain hash of mainnet does not match.
+               let network_graph = NetworkGraph::new(Network::Testnet, &logger);
+
+               assert_eq!(network_graph.read_only().channels().len(), 0);
+
+               let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
+               let update_result = rapid_sync.update_network_graph_no_std(&VALID_RGS_BINARY, Some(0));
+               assert!(update_result.is_err());
+               if let Err(GraphSyncError::LightningError(err)) = update_result {
+                       assert_eq!(err.err, "Rapid Gossip Sync data's chain hash does not match the network graph's");
+               } else {
+                       panic!("Unexpected update result: {:?}", update_result)
+               }
+       }
 }
index 2e9e13296b6c993e1622c84370e20a2cd97e88b0..53221eddf69cb6f9f7f43c1b6658044bea26bf52 100644 (file)
@@ -1,6 +1,6 @@
 [package]
 name = "lightning-transaction-sync"
-version = "0.0.115"
+version = "0.0.116-alpha1"
 authors = ["Elias Rohrer"]
 license = "MIT OR Apache-2.0"
 repository = "http://github.com/lightningdevkit/rust-lightning"
@@ -21,7 +21,7 @@ esplora-blocking = ["esplora-client/blocking"]
 async-interface = []
 
 [dependencies]
-lightning = { version = "0.0.115", path = "../lightning", default-features = false }
+lightning = { version = "0.0.116-alpha1", path = "../lightning", default-features = false }
 bitcoin = { version = "0.29.0", default-features = false }
 bdk-macros = "0.6"
 futures = { version = "0.3", optional = true }
@@ -29,7 +29,7 @@ esplora-client = { version = "0.4", default-features = false, optional = true }
 reqwest = { version = "0.11", optional = true, default-features = false, features = ["json"] }
 
 [dev-dependencies]
-lightning = { version = "0.0.115", path = "../lightning", features = ["std"] }
+lightning = { version = "0.0.116-alpha1", path = "../lightning", features = ["std"] }
 electrsd = { version = "0.22.0", features = ["legacy", "esplora_a33e97e1", "bitcoind_23_0"] }
 electrum-client = "0.12.0"
 tokio = { version = "1.14.0", features = ["full"] }
index 1804be0f1bb50ac26b84c6472a7c1137f5b8ebf8..f02b605f86bf583d20164357018abc55448dc479 100644 (file)
@@ -1,6 +1,6 @@
 [package]
 name = "lightning"
-version = "0.0.115"
+version = "0.0.116-alpha1"
 authors = ["Matt Corallo"]
 license = "MIT OR Apache-2.0"
 repository = "https://github.com/lightningdevkit/rust-lightning/"
@@ -28,7 +28,6 @@ max_level_trace = []
 # Allow signing of local transactions that may have been revoked or will be revoked, for functional testing (e.g. justice tx handling).
 # This is unsafe to use in production because it may result in the counterparty publishing taking our funds.
 unsafe_revoked_tx_signing = []
-_bench_unstable = []
 # Override signing to not include randomness when generating signatures for test vectors.
 _test_vectors = []
 
@@ -59,5 +58,8 @@ version = "0.29.0"
 default-features = false
 features = ["bitcoinconsensus", "secp-recovery"]
 
+[target.'cfg(ldk_bench)'.dependencies]
+criterion = { version = "0.4", optional = true, default-features = false }
+
 [target.'cfg(taproot)'.dependencies]
 musig2 = { git = "https://github.com/arik-so/rust-musig2", rev = "27797d7" }
index e923a94bb6d3e7afb5f37376b6de132225821d21..d875dcce3e128c1443a2deaa46f1a8465a7cd06b 100644 (file)
@@ -19,8 +19,20 @@ use bitcoin::blockdata::transaction::Transaction;
 
 /// An interface to send a transaction to the Bitcoin network.
 pub trait BroadcasterInterface {
-       /// Sends a transaction out to (hopefully) be mined.
-       fn broadcast_transaction(&self, tx: &Transaction);
+       /// Sends a list of transactions out to (hopefully) be mined.
+       /// This only needs to handle the actual broadcasting of transactions, LDK will automatically
+       /// rebroadcast transactions that haven't made it into a block.
+       ///
+       /// In some cases LDK may attempt to broadcast a transaction which double-spends another
+       /// and this isn't a bug and can be safely ignored.
+       ///
+       /// If more than one transaction is given, these transactions should be considered to be a
+       /// package and broadcast together. Some of the transactions may or may not depend on each other,
+       /// be sure to manage both cases correctly.
+       ///
+       /// Bitcoin transaction packages are defined in BIP 331 and here:
+       /// https://github.com/bitcoin/bitcoin/blob/master/doc/policy/packages.md
+       fn broadcast_transactions(&self, txs: &[&Transaction]);
 }
 
 /// An enum that represents the speed at which we want a transaction to confirm used for feerate
index 261e5593b5b4600ad661142a4579308b68903316..562c76fa3e2c996f136c54d8fb4adb36baeba802 100644 (file)
@@ -502,7 +502,7 @@ where C::Target: chain::Filter,
                self.event_notifier.notify();
        }
 
-       #[cfg(any(test, fuzzing, feature = "_test_utils"))]
+       #[cfg(any(test, feature = "_test_utils"))]
        pub fn get_and_clear_pending_events(&self) -> Vec<events::Event> {
                use crate::events::EventsProvider;
                let events = core::cell::RefCell::new(Vec::new());
@@ -782,30 +782,13 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner, C: Deref, T: Deref, F: Deref, L
              L::Target: Logger,
              P::Target: Persist<ChannelSigner>,
 {
-       #[cfg(not(anchors))]
-       /// Processes [`SpendableOutputs`] events produced from each [`ChannelMonitor`] upon maturity.
-       ///
-       /// An [`EventHandler`] may safely call back to the provider, though this shouldn't be needed in
-       /// order to handle these events.
-       ///
-       /// [`SpendableOutputs`]: events::Event::SpendableOutputs
-       fn process_pending_events<H: Deref>(&self, handler: H) where H::Target: EventHandler {
-               let mut pending_events = Vec::new();
-               for monitor_state in self.monitors.read().unwrap().values() {
-                       pending_events.append(&mut monitor_state.monitor.get_and_clear_pending_events());
-               }
-               for event in pending_events {
-                       handler.handle_event(event);
-               }
-       }
-       #[cfg(anchors)]
        /// Processes [`SpendableOutputs`] events produced from each [`ChannelMonitor`] upon maturity.
        ///
        /// For channels featuring anchor outputs, this method will also process [`BumpTransaction`]
        /// events produced from each [`ChannelMonitor`] while there is a balance to claim onchain
        /// within each channel. As the confirmation of a commitment transaction may be critical to the
-       /// safety of funds, this method must be invoked frequently, ideally once for every chain tip
-       /// update (block connected or disconnected).
+       /// safety of funds, we recommend invoking this every 30 seconds, or lower if running in an
+       /// environment with spotty connections, like on mobile.
        ///
        /// An [`EventHandler`] may safely call back to the provider, though this shouldn't be needed in
        /// order to handle these events.
index a9ef37564277143c3bd4742c2d3376322ae01e55..904d9941349804f99cb31127e2757fcc51bbf473 100644 (file)
@@ -43,16 +43,13 @@ use crate::chain::{BestBlock, WatchedOutput};
 use crate::chain::chaininterface::{BroadcasterInterface, FeeEstimator, LowerBoundedFeeEstimator};
 use crate::chain::transaction::{OutPoint, TransactionData};
 use crate::sign::{SpendableOutputDescriptor, StaticPaymentOutputDescriptor, DelayedPaymentOutputDescriptor, WriteableEcdsaChannelSigner, SignerProvider, EntropySource};
-#[cfg(anchors)]
-use crate::chain::onchaintx::ClaimEvent;
-use crate::chain::onchaintx::OnchainTxHandler;
+use crate::chain::onchaintx::{ClaimEvent, OnchainTxHandler};
 use crate::chain::package::{CounterpartyOfferedHTLCOutput, CounterpartyReceivedHTLCOutput, HolderFundingOutput, HolderHTLCOutput, PackageSolvingData, PackageTemplate, RevokedOutput, RevokedHTLCOutput};
 use crate::chain::Filter;
 use crate::util::logger::Logger;
 use crate::util::ser::{Readable, ReadableArgs, RequiredWrapper, MaybeReadable, UpgradableRequired, Writer, Writeable, U48};
 use crate::util::byte_utils;
 use crate::events::Event;
-#[cfg(anchors)]
 use crate::events::bump_transaction::{AnchorDescriptor, HTLCDescriptor, BumpTransactionEvent};
 
 use crate::prelude::*;
@@ -268,7 +265,6 @@ impl_writeable_tlv_based!(HolderSignedTx, {
        (14, htlc_outputs, vec_type)
 });
 
-#[cfg(anchors)]
 impl HolderSignedTx {
        fn non_dust_htlcs(&self) -> Vec<HTLCOutputInCommitment> {
                self.htlc_outputs.iter().filter_map(|(htlc, _, _)| {
@@ -651,6 +647,40 @@ pub enum Balance {
        },
 }
 
+impl Balance {
+       /// The amount claimable, in satoshis. This excludes balances that we are unsure if we are able
+       /// to claim, this is because we are waiting for a preimage or for a timeout to expire. For more
+       /// information on these balances see [`Balance::MaybeTimeoutClaimableHTLC`] and
+       /// [`Balance::MaybePreimageClaimableHTLC`].
+       ///
+       /// On-chain fees required to claim the balance are not included in this amount.
+       pub fn claimable_amount_satoshis(&self) -> u64 {
+               match self {
+                       Balance::ClaimableOnChannelClose {
+                               claimable_amount_satoshis,
+                       } => *claimable_amount_satoshis,
+                       Balance::ClaimableAwaitingConfirmations {
+                               claimable_amount_satoshis,
+                               ..
+                       } => *claimable_amount_satoshis,
+                       Balance::ContentiousClaimable {
+                               claimable_amount_satoshis,
+                               ..
+                       } => *claimable_amount_satoshis,
+                       Balance::MaybeTimeoutClaimableHTLC {
+                               ..
+                       } => 0,
+                       Balance::MaybePreimageClaimableHTLC {
+                               ..
+                       } => 0,
+                       Balance::CounterpartyRevokedOutputClaimable {
+                               claimable_amount_satoshis,
+                               ..
+                       } => *claimable_amount_satoshis,
+               }
+       }
+}
+
 /// An HTLC which has been irrevocably resolved on-chain, and has reached ANTI_REORG_DELAY.
 #[derive(PartialEq, Eq)]
 struct IrrevocablyResolvedHTLC {
@@ -1566,7 +1596,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
                                        debug_assert!(htlc_input_idx_opt.is_some());
                                        BitcoinOutPoint::new(*txid, htlc_input_idx_opt.unwrap_or(0))
                                } else {
-                                       debug_assert!(!self.onchain_tx_handler.opt_anchors());
+                                       debug_assert!(!self.onchain_tx_handler.channel_type_features().supports_anchors_zero_fee_htlc_tx());
                                        BitcoinOutPoint::new(*txid, 0)
                                }
                        } else {
@@ -2327,10 +2357,13 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
                where B::Target: BroadcasterInterface,
                                        L::Target: Logger,
        {
-               for tx in self.get_latest_holder_commitment_txn(logger).iter() {
+               let commit_txs = self.get_latest_holder_commitment_txn(logger);
+               let mut txs = vec![];
+               for tx in commit_txs.iter() {
                        log_info!(logger, "Broadcasting local {}", log_tx!(tx));
-                       broadcaster.broadcast_transaction(tx);
+                       txs.push(tx);
                }
+               broadcaster.broadcast_transactions(&txs);
                self.pending_monitor_events.push(MonitorEvent::CommitmentTxConfirmed(self.funding_info.0));
        }
 
@@ -2422,10 +2455,10 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
                                                // If the channel supports anchor outputs, we'll need to emit an external
                                                // event to be consumed such that a child transaction is broadcast with a
                                                // high enough feerate for the parent commitment transaction to confirm.
-                                               if self.onchain_tx_handler.opt_anchors() {
+                                               if self.onchain_tx_handler.channel_type_features().supports_anchors_zero_fee_htlc_tx() {
                                                        let funding_output = HolderFundingOutput::build(
                                                                self.funding_redeemscript.clone(), self.channel_value_satoshis,
-                                                               self.onchain_tx_handler.opt_anchors(),
+                                                               self.onchain_tx_handler.channel_type_features().clone(),
                                                        );
                                                        let best_block_height = self.best_block.height();
                                                        let commitment_package = PackageTemplate::build_package(
@@ -2501,8 +2534,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
        pub fn get_and_clear_pending_events(&mut self) -> Vec<Event> {
                let mut ret = Vec::new();
                mem::swap(&mut ret, &mut self.pending_events);
-               #[cfg(anchors)]
-               for claim_event in self.onchain_tx_handler.get_and_clear_pending_claim_events().drain(..) {
+               for (claim_id, claim_event) in self.onchain_tx_handler.get_and_clear_pending_claim_events().drain(..) {
                        match claim_event {
                                ClaimEvent::BumpCommitment {
                                        package_target_feerate_sat_per_1000_weight, commitment_tx, anchor_output_idx,
@@ -2513,6 +2545,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
                                        let commitment_tx_fee_satoshis = self.channel_value_satoshis -
                                                commitment_tx.output.iter().fold(0u64, |sum, output| sum + output.value);
                                        ret.push(Event::BumpTransaction(BumpTransactionEvent::ChannelClose {
+                                               claim_id,
                                                package_target_feerate_sat_per_1000_weight,
                                                commitment_tx,
                                                commitment_tx_fee_satoshis,
@@ -2544,6 +2577,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
                                                });
                                        }
                                        ret.push(Event::BumpTransaction(BumpTransactionEvent::HTLCResolution {
+                                               claim_id,
                                                target_feerate_sat_per_1000_weight,
                                                htlc_descriptors,
                                                tx_lock_time,
@@ -2614,7 +2648,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
                        // First, process non-htlc outputs (to_holder & to_counterparty)
                        for (idx, outp) in tx.output.iter().enumerate() {
                                if outp.script_pubkey == revokeable_p2wsh {
-                                       let revk_outp = RevokedOutput::build(per_commitment_point, self.counterparty_commitment_params.counterparty_delayed_payment_base_key, self.counterparty_commitment_params.counterparty_htlc_base_key, per_commitment_key, outp.value, self.counterparty_commitment_params.on_counterparty_tx_csv, self.onchain_tx_handler.opt_anchors());
+                                       let revk_outp = RevokedOutput::build(per_commitment_point, self.counterparty_commitment_params.counterparty_delayed_payment_base_key, self.counterparty_commitment_params.counterparty_htlc_base_key, per_commitment_key, outp.value, self.counterparty_commitment_params.on_counterparty_tx_csv, self.onchain_tx_handler.channel_type_features().supports_anchors_zero_fee_htlc_tx());
                                        let justice_package = PackageTemplate::build_package(commitment_txid, idx as u32, PackageSolvingData::RevokedOutput(revk_outp), height + self.counterparty_commitment_params.on_counterparty_tx_csv as u32, height);
                                        claimable_outpoints.push(justice_package);
                                        to_counterparty_output_info =
@@ -2632,7 +2666,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
                                                        return (claimable_outpoints, (commitment_txid, watch_outputs),
                                                                to_counterparty_output_info);
                                                }
-                                               let revk_htlc_outp = RevokedHTLCOutput::build(per_commitment_point, self.counterparty_commitment_params.counterparty_delayed_payment_base_key, self.counterparty_commitment_params.counterparty_htlc_base_key, per_commitment_key, htlc.amount_msat / 1000, htlc.clone(), self.onchain_tx_handler.channel_transaction_parameters.opt_anchors.is_some());
+                                               let revk_htlc_outp = RevokedHTLCOutput::build(per_commitment_point, self.counterparty_commitment_params.counterparty_delayed_payment_base_key, self.counterparty_commitment_params.counterparty_htlc_base_key, per_commitment_key, htlc.amount_msat / 1000, htlc.clone(), &self.onchain_tx_handler.channel_transaction_parameters.channel_type_features);
                                                let justice_package = PackageTemplate::build_package(commitment_txid, transaction_output_index, PackageSolvingData::RevokedHTLCOutput(revk_htlc_outp), htlc.cltv_expiry, height);
                                                claimable_outpoints.push(justice_package);
                                        }
@@ -2750,13 +2784,13 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
                                                        CounterpartyOfferedHTLCOutput::build(*per_commitment_point,
                                                                self.counterparty_commitment_params.counterparty_delayed_payment_base_key,
                                                                self.counterparty_commitment_params.counterparty_htlc_base_key,
-                                                               preimage.unwrap(), htlc.clone(), self.onchain_tx_handler.opt_anchors()))
+                                                               preimage.unwrap(), htlc.clone(), self.onchain_tx_handler.channel_type_features().clone()))
                                        } else {
                                                PackageSolvingData::CounterpartyReceivedHTLCOutput(
                                                        CounterpartyReceivedHTLCOutput::build(*per_commitment_point,
                                                                self.counterparty_commitment_params.counterparty_delayed_payment_base_key,
                                                                self.counterparty_commitment_params.counterparty_htlc_base_key,
-                                                               htlc.clone(), self.onchain_tx_handler.opt_anchors()))
+                                                               htlc.clone(), self.onchain_tx_handler.channel_type_features().clone()))
                                        };
                                        let counterparty_package = PackageTemplate::build_package(commitment_txid, transaction_output_index, counterparty_htlc_outp, htlc.cltv_expiry, 0);
                                        claimable_outpoints.push(counterparty_package);
@@ -2827,7 +2861,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
                        if let Some(transaction_output_index) = htlc.transaction_output_index {
                                let htlc_output = if htlc.offered {
                                        let htlc_output = HolderHTLCOutput::build_offered(
-                                               htlc.amount_msat, htlc.cltv_expiry, self.onchain_tx_handler.opt_anchors()
+                                               htlc.amount_msat, htlc.cltv_expiry, self.onchain_tx_handler.channel_type_features().clone()
                                        );
                                        htlc_output
                                } else {
@@ -2838,7 +2872,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
                                                continue;
                                        };
                                        let htlc_output = HolderHTLCOutput::build_accepted(
-                                               payment_preimage, htlc.amount_msat, self.onchain_tx_handler.opt_anchors()
+                                               payment_preimage, htlc.amount_msat, self.onchain_tx_handler.channel_type_features().clone()
                                        );
                                        htlc_output
                                };
@@ -2922,7 +2956,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
                let mut holder_transactions = vec![commitment_tx];
                // When anchor outputs are present, the HTLC transactions are only valid once the commitment
                // transaction confirms.
-               if self.onchain_tx_handler.opt_anchors() {
+               if self.onchain_tx_handler.channel_type_features().supports_anchors_zero_fee_htlc_tx() {
                        return holder_transactions;
                }
                for htlc in self.current_holder_commitment_tx.htlc_outputs.iter() {
@@ -2960,7 +2994,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
                let mut holder_transactions = vec![commitment_tx];
                // When anchor outputs are present, the HTLC transactions are only final once the commitment
                // transaction confirms due to the CSV 1 encumberance.
-               if self.onchain_tx_handler.opt_anchors() {
+               if self.onchain_tx_handler.channel_type_features().supports_anchors_zero_fee_htlc_tx() {
                        return holder_transactions;
                }
                for htlc in self.current_holder_commitment_tx.htlc_outputs.iter() {
@@ -3184,7 +3218,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
 
                let should_broadcast = self.should_broadcast_holder_commitment_txn(logger);
                if should_broadcast {
-                       let funding_outp = HolderFundingOutput::build(self.funding_redeemscript.clone(), self.channel_value_satoshis, self.onchain_tx_handler.opt_anchors());
+                       let funding_outp = HolderFundingOutput::build(self.funding_redeemscript.clone(), self.channel_value_satoshis, self.onchain_tx_handler.channel_type_features().clone());
                        let commitment_package = PackageTemplate::build_package(self.funding_info.0.txid.clone(), self.funding_info.0.index as u32, PackageSolvingData::HolderFundingOutput(funding_outp), self.best_block.height(), self.best_block.height());
                        claimable_outpoints.push(commitment_package);
                        self.pending_monitor_events.push(MonitorEvent::CommitmentTxConfirmed(self.funding_info.0));
@@ -3193,7 +3227,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
                        // We can't broadcast our HTLC transactions while the commitment transaction is
                        // unconfirmed. We'll delay doing so until we detect the confirmed commitment in
                        // `transactions_confirmed`.
-                       if !self.onchain_tx_handler.opt_anchors() {
+                       if !self.onchain_tx_handler.channel_type_features().supports_anchors_zero_fee_htlc_tx() {
                                // Because we're broadcasting a commitment transaction, we should construct the package
                                // assuming it gets confirmed in the next block. Sadly, we have code which considers
                                // "not yet confirmed" things as discardable, so we cannot do that here.
@@ -4121,6 +4155,7 @@ mod tests {
        use crate::sync::{Arc, Mutex};
        use crate::io;
        use bitcoin::{PackedLockTime, Sequence, Witness};
+       use crate::ln::features::ChannelTypeFeatures;
        use crate::prelude::*;
 
        fn do_test_funding_spend_refuses_updates(use_local_txn: bool) {
@@ -4294,8 +4329,7 @@ mod tests {
                                selected_contest_delay: 67,
                        }),
                        funding_outpoint: Some(funding_outpoint),
-                       opt_anchors: None,
-                       opt_non_zero_fee_anchors: None,
+                       channel_type_features: ChannelTypeFeatures::only_static_remote_key()
                };
                // Prune with one old state and a holder commitment tx holding a few overlaps with the
                // old state.
@@ -4411,7 +4445,7 @@ mod tests {
                let txid = Txid::from_hex("56944c5d3f98413ef45cf54545538103cc9f298e0575820ad3591376e2e0f65d").unwrap();
 
                // Justice tx with 1 to_holder, 2 revoked offered HTLCs, 1 revoked received HTLCs
-               for &opt_anchors in [false, true].iter() {
+               for channel_type_features in [ChannelTypeFeatures::only_static_remote_key(), ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies()].iter() {
                        let mut claim_tx = Transaction { version: 0, lock_time: PackedLockTime::ZERO, input: Vec::new(), output: Vec::new() };
                        let mut sum_actual_sigs = 0;
                        for i in 0..4 {
@@ -4430,12 +4464,12 @@ mod tests {
                                value: 0,
                        });
                        let base_weight = claim_tx.weight();
-                       let inputs_weight = vec![WEIGHT_REVOKED_OUTPUT, weight_revoked_offered_htlc(opt_anchors), weight_revoked_offered_htlc(opt_anchors), weight_revoked_received_htlc(opt_anchors)];
+                       let inputs_weight = vec![WEIGHT_REVOKED_OUTPUT, weight_revoked_offered_htlc(channel_type_features), weight_revoked_offered_htlc(channel_type_features), weight_revoked_received_htlc(channel_type_features)];
                        let mut inputs_total_weight = 2; // count segwit flags
                        {
                                let mut sighash_parts = sighash::SighashCache::new(&mut claim_tx);
                                for (idx, inp) in inputs_weight.iter().enumerate() {
-                                       sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs, opt_anchors);
+                                       sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs, channel_type_features);
                                        inputs_total_weight += inp;
                                }
                        }
@@ -4443,7 +4477,7 @@ mod tests {
                }
 
                // Claim tx with 1 offered HTLCs, 3 received HTLCs
-               for &opt_anchors in [false, true].iter() {
+               for channel_type_features in [ChannelTypeFeatures::only_static_remote_key(), ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies()].iter() {
                        let mut claim_tx = Transaction { version: 0, lock_time: PackedLockTime::ZERO, input: Vec::new(), output: Vec::new() };
                        let mut sum_actual_sigs = 0;
                        for i in 0..4 {
@@ -4462,12 +4496,12 @@ mod tests {
                                value: 0,
                        });
                        let base_weight = claim_tx.weight();
-                       let inputs_weight = vec![weight_offered_htlc(opt_anchors), weight_received_htlc(opt_anchors), weight_received_htlc(opt_anchors), weight_received_htlc(opt_anchors)];
+                       let inputs_weight = vec![weight_offered_htlc(channel_type_features), weight_received_htlc(channel_type_features), weight_received_htlc(channel_type_features), weight_received_htlc(channel_type_features)];
                        let mut inputs_total_weight = 2; // count segwit flags
                        {
                                let mut sighash_parts = sighash::SighashCache::new(&mut claim_tx);
                                for (idx, inp) in inputs_weight.iter().enumerate() {
-                                       sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs, opt_anchors);
+                                       sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs, channel_type_features);
                                        inputs_total_weight += inp;
                                }
                        }
@@ -4475,7 +4509,7 @@ mod tests {
                }
 
                // Justice tx with 1 revoked HTLC-Success tx output
-               for &opt_anchors in [false, true].iter() {
+               for channel_type_features in [ChannelTypeFeatures::only_static_remote_key(), ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies()].iter() {
                        let mut claim_tx = Transaction { version: 0, lock_time: PackedLockTime::ZERO, input: Vec::new(), output: Vec::new() };
                        let mut sum_actual_sigs = 0;
                        claim_tx.input.push(TxIn {
@@ -4497,7 +4531,7 @@ mod tests {
                        {
                                let mut sighash_parts = sighash::SighashCache::new(&mut claim_tx);
                                for (idx, inp) in inputs_weight.iter().enumerate() {
-                                       sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs, opt_anchors);
+                                       sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs, channel_type_features);
                                        inputs_total_weight += inp;
                                }
                        }
index abd888b3cf28bc12847e149f0dae1d59a2d4aecb..a32bcb29901fc1182c21b8e3e02ce9c6f92b3bb7 100644 (file)
@@ -389,3 +389,7 @@ where
                self.1.block_disconnected(header, height);
        }
 }
+
+/// A unique identifier to track each pending output claim within a [`ChannelMonitor`].
+#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
+pub struct ClaimId(pub [u8; 32]);
index 21b4717e1d9786dfd2b0eb68092e41f7b3d19afd..6b0c7485610353917ef348f2ec54d1828070eea1 100644 (file)
 //! OnchainTxHandler objects are fully-part of ChannelMonitor and encapsulates all
 //! building, tracking, bumping and notifications functions.
 
-#[cfg(anchors)]
 use bitcoin::PackedLockTime;
 use bitcoin::blockdata::transaction::Transaction;
 use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
 use bitcoin::blockdata::script::Script;
-
+use bitcoin::hashes::{Hash, HashEngine};
+use bitcoin::hashes::sha256::Hash as Sha256;
 use bitcoin::hash_types::{Txid, BlockHash};
-
 use bitcoin::secp256k1::{Secp256k1, ecdsa::Signature};
 use bitcoin::secp256k1;
 
 use crate::sign::{ChannelSigner, EntropySource, SignerProvider};
 use crate::ln::msgs::DecodeError;
 use crate::ln::PaymentPreimage;
-#[cfg(anchors)]
-use crate::ln::chan_utils::{self, HTLCOutputInCommitment};
-use crate::ln::chan_utils::{ChannelTransactionParameters, HolderCommitmentTransaction};
-#[cfg(anchors)]
-use crate::chain::chaininterface::ConfirmationTarget;
-use crate::chain::chaininterface::{FeeEstimator, BroadcasterInterface, LowerBoundedFeeEstimator};
+use crate::ln::chan_utils::{self, ChannelTransactionParameters, HTLCOutputInCommitment, HolderCommitmentTransaction};
+use crate::chain::ClaimId;
+use crate::chain::chaininterface::{ConfirmationTarget, FeeEstimator, BroadcasterInterface, LowerBoundedFeeEstimator};
 use crate::chain::channelmonitor::{ANTI_REORG_DELAY, CLTV_SHARED_CLAIM_BUFFER};
 use crate::sign::WriteableEcdsaChannelSigner;
-#[cfg(anchors)]
-use crate::chain::package::PackageSolvingData;
-use crate::chain::package::PackageTemplate;
+use crate::chain::package::{PackageSolvingData, PackageTemplate};
 use crate::util::logger::Logger;
 use crate::util::ser::{Readable, ReadableArgs, MaybeReadable, UpgradableRequired, Writer, Writeable, VecWriter};
 
@@ -46,9 +40,8 @@ use alloc::collections::BTreeMap;
 use core::cmp;
 use core::ops::Deref;
 use core::mem::replace;
-#[cfg(anchors)]
 use core::mem::swap;
-use bitcoin::hashes::Hash;
+use crate::ln::features::ChannelTypeFeatures;
 
 const MAX_ALLOC_SIZE: usize = 64*1024;
 
@@ -83,7 +76,7 @@ enum OnchainEvent {
        /// transaction has met [`ANTI_REORG_DELAY`] confirmations, we consider it final and remove the
        /// pending request.
        Claim {
-               package_id: PackageID,
+               claim_id: ClaimId,
        },
        /// The counterparty has claimed an outpoint from one of our pending requests through a
        /// different transaction than ours. If our transaction was attempting to claim multiple
@@ -126,7 +119,7 @@ impl MaybeReadable for OnchainEventEntry {
 
 impl_writeable_tlv_based_enum_upgradable!(OnchainEvent,
        (0, Claim) => {
-               (0, package_id, required),
+               (0, claim_id, required),
        },
        (1, ContentiousOutpoint) => {
                (0, package, required),
@@ -177,7 +170,6 @@ impl Writeable for Option<Vec<Option<(usize, Signature)>>> {
        }
 }
 
-#[cfg(anchors)]
 /// The claim commonly referred to as the pre-signed second-stage HTLC transaction.
 pub(crate) struct ExternalHTLCClaim {
        pub(crate) commitment_txid: Txid,
@@ -189,7 +181,6 @@ pub(crate) struct ExternalHTLCClaim {
 
 // Represents the different types of claims for which events are yielded externally to satisfy said
 // claims.
-#[cfg(anchors)]
 pub(crate) enum ClaimEvent {
        /// Event yielded to signal that the commitment transaction fee must be bumped to claim any
        /// encumbered funds and proceed to HTLC resolution, if any HTLCs exist.
@@ -212,15 +203,11 @@ pub(crate) enum ClaimEvent {
 pub(crate) enum OnchainClaim {
        /// A finalized transaction pending confirmation spending the output to claim.
        Tx(Transaction),
-       #[cfg(anchors)]
        /// An event yielded externally to signal additional inputs must be added to a transaction
        /// pending confirmation spending the output to claim.
        Event(ClaimEvent),
 }
 
-/// An internal identifier to track pending package claims within the `OnchainTxHandler`.
-type PackageID = [u8; 32];
-
 /// OnchainTxHandler receives claiming requests, aggregates them if it's sound, broadcast and
 /// do RBF bumping if possible.
 pub struct OnchainTxHandler<ChannelSigner: WriteableEcdsaChannelSigner> {
@@ -248,13 +235,13 @@ pub struct OnchainTxHandler<ChannelSigner: WriteableEcdsaChannelSigner> {
        // us and is immutable until all outpoint of the claimable set are post-anti-reorg-delay solved.
        // Entry is cache of elements need to generate a bumped claiming transaction (see ClaimTxBumpMaterial)
        #[cfg(test)] // Used in functional_test to verify sanitization
-       pub(crate) pending_claim_requests: HashMap<PackageID, PackageTemplate>,
+       pub(crate) pending_claim_requests: HashMap<ClaimId, PackageTemplate>,
        #[cfg(not(test))]
-       pending_claim_requests: HashMap<PackageID, PackageTemplate>,
+       pending_claim_requests: HashMap<ClaimId, PackageTemplate>,
 
        // Used to track external events that need to be forwarded to the `ChainMonitor`. This `Vec`
        // essentially acts as an insertion-ordered `HashMap` â€“ there should only ever be one occurrence
-       // of a `PackageID`, which tracks its latest `ClaimEvent`, i.e., if a pending claim exists, and
+       // of a `ClaimId`, which tracks its latest `ClaimEvent`, i.e., if a pending claim exists, and
        // a new block has been connected, resulting in a new claim, the previous will be replaced with
        // the new.
        //
@@ -262,8 +249,7 @@ pub struct OnchainTxHandler<ChannelSigner: WriteableEcdsaChannelSigner> {
        //      - A channel has been force closed by broadcasting the holder's latest commitment transaction
        //      - A block being connected/disconnected
        //      - Learning the preimage for an HTLC we can claim onchain
-       #[cfg(anchors)]
-       pending_claim_events: Vec<(PackageID, ClaimEvent)>,
+       pending_claim_events: Vec<(ClaimId, ClaimEvent)>,
 
        // Used to link outpoints claimed in a connected block to a pending claim request. The keys
        // represent the outpoints that our `ChannelMonitor` has detected we have keys/scripts to
@@ -272,9 +258,9 @@ pub struct OnchainTxHandler<ChannelSigner: WriteableEcdsaChannelSigner> {
        // [`ANTI_REORG_DELAY`]. The initial confirmation block height is used to remove the entry if
        // the block gets disconnected.
        #[cfg(test)] // Used in functional_test to verify sanitization
-       pub claimable_outpoints: HashMap<BitcoinOutPoint, (PackageID, u32)>,
+       pub claimable_outpoints: HashMap<BitcoinOutPoint, (ClaimId, u32)>,
        #[cfg(not(test))]
-       claimable_outpoints: HashMap<BitcoinOutPoint, (PackageID, u32)>,
+       claimable_outpoints: HashMap<BitcoinOutPoint, (ClaimId, u32)>,
 
        locktimed_packages: BTreeMap<u32, Vec<PackageTemplate>>,
 
@@ -439,7 +425,6 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
                        locktimed_packages,
                        pending_claim_requests,
                        onchain_events_awaiting_threshold_conf,
-                       #[cfg(anchors)]
                        pending_claim_events: Vec::new(),
                        secp_ctx,
                })
@@ -460,7 +445,6 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
                        claimable_outpoints: HashMap::new(),
                        locktimed_packages: BTreeMap::new(),
                        onchain_events_awaiting_threshold_conf: Vec::new(),
-                       #[cfg(anchors)]
                        pending_claim_events: Vec::new(),
                        secp_ctx,
                }
@@ -474,11 +458,10 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
                self.holder_commitment.to_broadcaster_value_sat()
        }
 
-       #[cfg(anchors)]
-       pub(crate) fn get_and_clear_pending_claim_events(&mut self) -> Vec<ClaimEvent> {
+       pub(crate) fn get_and_clear_pending_claim_events(&mut self) -> Vec<(ClaimId, ClaimEvent)> {
                let mut events = Vec::new();
                swap(&mut events, &mut self.pending_claim_events);
-               events.into_iter().map(|(_, event)| event).collect()
+               events
        }
 
        /// Triggers rebroadcasts/fee-bumps of pending claims from a force-closed channel. This is
@@ -496,16 +479,16 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
                L::Target: Logger,
        {
                let mut bump_requests = Vec::with_capacity(self.pending_claim_requests.len());
-               for (package_id, request) in self.pending_claim_requests.iter() {
+               for (claim_id, request) in self.pending_claim_requests.iter() {
                        let inputs = request.outpoints();
                        log_info!(logger, "Triggering rebroadcast/fee-bump for request with inputs {:?}", inputs);
-                       bump_requests.push((*package_id, request.clone()));
+                       bump_requests.push((*claim_id, request.clone()));
                }
-               for (package_id, request) in bump_requests {
+               for (claim_id, request) in bump_requests {
                        self.generate_claim(current_height, &request, false /* force_feerate_bump */, fee_estimator, logger)
                                .map(|(_, new_feerate, claim)| {
                                        let mut bumped_feerate = false;
-                                       if let Some(mut_request) = self.pending_claim_requests.get_mut(&package_id) {
+                                       if let Some(mut_request) = self.pending_claim_requests.get_mut(&claim_id) {
                                                bumped_feerate = request.previous_feerate() > new_feerate;
                                                mut_request.set_feerate(new_feerate);
                                        }
@@ -513,9 +496,8 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
                                                OnchainClaim::Tx(tx) => {
                                                        let log_start = if bumped_feerate { "Broadcasting RBF-bumped" } else { "Rebroadcasting" };
                                                        log_info!(logger, "{} onchain {}", log_start, log_tx!(tx));
-                                                       broadcaster.broadcast_transaction(&tx);
+                                                       broadcaster.broadcast_transactions(&[&tx]);
                                                },
-                                               #[cfg(anchors)]
                                                OnchainClaim::Event(event) => {
                                                        let log_start = if bumped_feerate { "Yielding fee-bumped" } else { "Replaying" };
                                                        log_info!(logger, "{} onchain event to spend inputs {:?}", log_start,
@@ -523,11 +505,11 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
                                                        #[cfg(debug_assertions)] {
                                                                debug_assert!(request.requires_external_funding());
                                                                let num_existing = self.pending_claim_events.iter()
-                                                                       .filter(|entry| entry.0 == package_id).count();
+                                                                       .filter(|entry| entry.0 == claim_id).count();
                                                                assert!(num_existing == 0 || num_existing == 1);
                                                        }
-                                                       self.pending_claim_events.retain(|event| event.0 != package_id);
-                                                       self.pending_claim_events.push((package_id, event));
+                                                       self.pending_claim_events.retain(|event| event.0 != claim_id);
+                                                       self.pending_claim_events.push((claim_id, event));
                                                }
                                        }
                                });
@@ -564,12 +546,12 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
                // transaction is reorged out.
                let mut all_inputs_have_confirmed_spend = true;
                for outpoint in request_outpoints.iter() {
-                       if let Some((request_package_id, _)) = self.claimable_outpoints.get(*outpoint) {
+                       if let Some((request_claim_id, _)) = self.claimable_outpoints.get(*outpoint) {
                                // We check for outpoint spends within claims individually rather than as a set
                                // since requests can have outpoints split off.
                                if !self.onchain_events_awaiting_threshold_conf.iter()
-                                       .any(|event_entry| if let OnchainEvent::Claim { package_id } = event_entry.event {
-                                               *request_package_id == package_id
+                                       .any(|event_entry| if let OnchainEvent::Claim { claim_id } = event_entry.event {
+                                               *request_claim_id == claim_id
                                        } else {
                                                // The onchain event is not a claim, keep seeking until we find one.
                                                false
@@ -592,25 +574,22 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
                // didn't receive confirmation of it before, or not enough reorg-safe depth on top of it).
                let new_timer = cached_request.get_height_timer(cur_height);
                if cached_request.is_malleable() {
-                       #[cfg(anchors)]
-                       { // Attributes are not allowed on if expressions on our current MSRV of 1.41.
-                               if cached_request.requires_external_funding() {
-                                       let target_feerate_sat_per_1000_weight = cached_request.compute_package_feerate(
-                                               fee_estimator, ConfirmationTarget::HighPriority, force_feerate_bump
-                                       );
-                                       if let Some(htlcs) = cached_request.construct_malleable_package_with_external_funding(self) {
-                                               return Some((
-                                                       new_timer,
-                                                       target_feerate_sat_per_1000_weight as u64,
-                                                       OnchainClaim::Event(ClaimEvent::BumpHTLC {
-                                                               target_feerate_sat_per_1000_weight,
-                                                               htlcs,
-                                                               tx_lock_time: PackedLockTime(cached_request.package_locktime(cur_height)),
-                                                       }),
-                                               ));
-                                       } else {
-                                               return None;
-                                       }
+                       if cached_request.requires_external_funding() {
+                               let target_feerate_sat_per_1000_weight = cached_request.compute_package_feerate(
+                                       fee_estimator, ConfirmationTarget::HighPriority, force_feerate_bump
+                               );
+                               if let Some(htlcs) = cached_request.construct_malleable_package_with_external_funding(self) {
+                                       return Some((
+                                               new_timer,
+                                               target_feerate_sat_per_1000_weight as u64,
+                                               OnchainClaim::Event(ClaimEvent::BumpHTLC {
+                                                       target_feerate_sat_per_1000_weight,
+                                                       htlcs,
+                                                       tx_lock_time: PackedLockTime(cached_request.package_locktime(cur_height)),
+                                               }),
+                                       ));
+                               } else {
+                                       return None;
                                }
                        }
 
@@ -632,9 +611,6 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
                        // Untractable packages cannot have their fees bumped through Replace-By-Fee. Some
                        // packages may support fee bumping through Child-Pays-For-Parent, indicated by those
                        // which require external funding.
-                       #[cfg(not(anchors))]
-                       let inputs = cached_request.inputs();
-                       #[cfg(anchors)]
                        let mut inputs = cached_request.inputs();
                        debug_assert_eq!(inputs.len(), 1);
                        let tx = match cached_request.finalize_untractable_package(self, logger) {
@@ -644,7 +620,6 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
                        if !cached_request.requires_external_funding() {
                                return Some((new_timer, 0, OnchainClaim::Tx(tx)));
                        }
-                       #[cfg(anchors)]
                        return inputs.find_map(|input| match input {
                                // Commitment inputs with anchors support are the only untractable inputs supported
                                // thus far that require external funding.
@@ -764,39 +739,44 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
                        ) {
                                req.set_timer(new_timer);
                                req.set_feerate(new_feerate);
-                               let package_id = match claim {
+                               let claim_id = match claim {
                                        OnchainClaim::Tx(tx) => {
                                                log_info!(logger, "Broadcasting onchain {}", log_tx!(tx));
-                                               broadcaster.broadcast_transaction(&tx);
-                                               tx.txid().into_inner()
+                                               broadcaster.broadcast_transactions(&[&tx]);
+                                               ClaimId(tx.txid().into_inner())
                                        },
-                                       #[cfg(anchors)]
                                        OnchainClaim::Event(claim_event) => {
                                                log_info!(logger, "Yielding onchain event to spend inputs {:?}", req.outpoints());
-                                               let package_id = match claim_event {
-                                                       ClaimEvent::BumpCommitment { ref commitment_tx, .. } => commitment_tx.txid().into_inner(),
+                                               let claim_id = match claim_event {
+                                                       ClaimEvent::BumpCommitment { ref commitment_tx, .. } =>
+                                                               // For commitment claims, we can just use their txid as it should
+                                                               // already be unique.
+                                                               ClaimId(commitment_tx.txid().into_inner()),
                                                        ClaimEvent::BumpHTLC { ref htlcs, .. } => {
-                                                               // Use the same construction as a lightning channel id to generate
-                                                               // the package id for this request based on the first HTLC. It
-                                                               // doesn't matter what we use as long as it's unique per request.
-                                                               let mut package_id = [0; 32];
-                                                               package_id[..].copy_from_slice(&htlcs[0].commitment_txid[..]);
-                                                               let htlc_output_index = htlcs[0].htlc.transaction_output_index.unwrap();
-                                                               package_id[30] ^= ((htlc_output_index >> 8) & 0xff) as u8;
-                                                               package_id[31] ^= ((htlc_output_index >> 0) & 0xff) as u8;
-                                                               package_id
+                                                               // For HTLC claims, commit to the entire set of HTLC outputs to
+                                                               // claim, which will always be unique per request. Once a claim ID
+                                                               // is generated, it is assigned and remains unchanged, even if the
+                                                               // underlying set of HTLCs changes.
+                                                               let mut engine = Sha256::engine();
+                                                               for htlc in htlcs {
+                                                                       engine.input(&htlc.commitment_txid.into_inner());
+                                                                       engine.input(&htlc.htlc.transaction_output_index.unwrap().to_be_bytes());
+                                                               }
+                                                               ClaimId(Sha256::from_engine(engine).into_inner())
                                                        },
                                                };
-                                               debug_assert_eq!(self.pending_claim_events.iter().filter(|entry| entry.0 == package_id).count(), 0);
-                                               self.pending_claim_events.push((package_id, claim_event));
-                                               package_id
+                                               debug_assert!(self.pending_claim_requests.get(&claim_id).is_none());
+                                               debug_assert_eq!(self.pending_claim_events.iter().filter(|entry| entry.0 == claim_id).count(), 0);
+                                               self.pending_claim_events.push((claim_id, claim_event));
+                                               claim_id
                                        },
                                };
+                               debug_assert!(self.pending_claim_requests.get(&claim_id).is_none());
                                for k in req.outpoints() {
                                        log_info!(logger, "Registering claiming request for {}:{}", k.txid, k.vout);
-                                       self.claimable_outpoints.insert(k.clone(), (package_id, conf_height));
+                                       self.claimable_outpoints.insert(k.clone(), (claim_id, conf_height));
                                }
-                               self.pending_claim_requests.insert(package_id, req);
+                               self.pending_claim_requests.insert(claim_id, req);
                        }
                }
        }
@@ -823,9 +803,9 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
                        // Scan all input to verify is one of the outpoint spent is of interest for us
                        let mut claimed_outputs_material = Vec::new();
                        for inp in &tx.input {
-                               if let Some((package_id, _)) = self.claimable_outpoints.get(&inp.previous_output) {
+                               if let Some((claim_id, _)) = self.claimable_outpoints.get(&inp.previous_output) {
                                        // If outpoint has claim request pending on it...
-                                       if let Some(request) = self.pending_claim_requests.get_mut(package_id) {
+                                       if let Some(request) = self.pending_claim_requests.get_mut(claim_id) {
                                                //... we need to verify equality between transaction outpoints and claim request
                                                // outpoints to know if transaction is the original claim or a bumped one issued
                                                // by us.
@@ -845,7 +825,7 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
                                                                        txid: tx.txid(),
                                                                        height: conf_height,
                                                                        block_hash: Some(conf_hash),
-                                                                       event: OnchainEvent::Claim { package_id: *package_id }
+                                                                       event: OnchainEvent::Claim { claim_id: *claim_id }
                                                                };
                                                                if !self.onchain_events_awaiting_threshold_conf.contains(&entry) {
                                                                        self.onchain_events_awaiting_threshold_conf.push(entry);
@@ -872,21 +852,19 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
                                                        }
                                                        //TODO: recompute soonest_timelock to avoid wasting a bit on fees
                                                        if at_least_one_drop {
-                                                               bump_candidates.insert(*package_id, request.clone());
+                                                               bump_candidates.insert(*claim_id, request.clone());
                                                                // If we have any pending claim events for the request being updated
                                                                // that have yet to be consumed, we'll remove them since they will
                                                                // end up producing an invalid transaction by double spending
                                                                // input(s) that already have a confirmed spend. If such spend is
                                                                // reorged out of the chain, then we'll attempt to re-spend the
                                                                // inputs once we see it.
-                                                               #[cfg(anchors)] {
-                                                                       #[cfg(debug_assertions)] {
-                                                                               let existing = self.pending_claim_events.iter()
-                                                                                       .filter(|entry| entry.0 == *package_id).count();
-                                                                               assert!(existing == 0 || existing == 1);
-                                                                       }
-                                                                       self.pending_claim_events.retain(|entry| entry.0 != *package_id);
+                                                               #[cfg(debug_assertions)] {
+                                                                       let existing = self.pending_claim_events.iter()
+                                                                               .filter(|entry| entry.0 == *claim_id).count();
+                                                                       assert!(existing == 0 || existing == 1);
                                                                }
+                                                               self.pending_claim_events.retain(|entry| entry.0 != *claim_id);
                                                        }
                                                }
                                                break; //No need to iterate further, either tx is our or their
@@ -914,23 +892,21 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
                for entry in onchain_events_awaiting_threshold_conf {
                        if entry.has_reached_confirmation_threshold(cur_height) {
                                match entry.event {
-                                       OnchainEvent::Claim { package_id } => {
+                                       OnchainEvent::Claim { claim_id } => {
                                                // We may remove a whole set of claim outpoints here, as these one may have
                                                // been aggregated in a single tx and claimed so atomically
-                                               if let Some(request) = self.pending_claim_requests.remove(&package_id) {
+                                               if let Some(request) = self.pending_claim_requests.remove(&claim_id) {
                                                        for outpoint in request.outpoints() {
                                                                log_debug!(logger, "Removing claim tracking for {} due to maturation of claim package {}.",
-                                                                       outpoint, log_bytes!(package_id));
+                                                                       outpoint, log_bytes!(claim_id.0));
                                                                self.claimable_outpoints.remove(outpoint);
                                                        }
-                                                       #[cfg(anchors)] {
-                                                               #[cfg(debug_assertions)] {
-                                                                       let num_existing = self.pending_claim_events.iter()
-                                                                               .filter(|entry| entry.0 == package_id).count();
-                                                                       assert!(num_existing == 0 || num_existing == 1);
-                                                               }
-                                                               self.pending_claim_events.retain(|(id, _)| *id != package_id);
+                                                       #[cfg(debug_assertions)] {
+                                                               let num_existing = self.pending_claim_events.iter()
+                                                                       .filter(|entry| entry.0 == claim_id).count();
+                                                               assert!(num_existing == 0 || num_existing == 1);
                                                        }
+                                                       self.pending_claim_events.retain(|(id, _)| *id != claim_id);
                                                }
                                        },
                                        OnchainEvent::ContentiousOutpoint { package } => {
@@ -945,36 +921,35 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
                }
 
                // Check if any pending claim request must be rescheduled
-               for (package_id, request) in self.pending_claim_requests.iter() {
+               for (claim_id, request) in self.pending_claim_requests.iter() {
                        if cur_height >= request.timer() {
-                               bump_candidates.insert(*package_id, request.clone());
+                               bump_candidates.insert(*claim_id, request.clone());
                        }
                }
 
                // Build, bump and rebroadcast tx accordingly
                log_trace!(logger, "Bumping {} candidates", bump_candidates.len());
-               for (package_id, request) in bump_candidates.iter() {
+               for (claim_id, request) in bump_candidates.iter() {
                        if let Some((new_timer, new_feerate, bump_claim)) = self.generate_claim(
                                cur_height, &request, true /* force_feerate_bump */, &*fee_estimator, &*logger,
                        ) {
                                match bump_claim {
                                        OnchainClaim::Tx(bump_tx) => {
                                                log_info!(logger, "Broadcasting RBF-bumped onchain {}", log_tx!(bump_tx));
-                                               broadcaster.broadcast_transaction(&bump_tx);
+                                               broadcaster.broadcast_transactions(&[&bump_tx]);
                                        },
-                                       #[cfg(anchors)]
                                        OnchainClaim::Event(claim_event) => {
                                                log_info!(logger, "Yielding RBF-bumped onchain event to spend inputs {:?}", request.outpoints());
                                                #[cfg(debug_assertions)] {
                                                        let num_existing = self.pending_claim_events.iter().
-                                                               filter(|entry| entry.0 == *package_id).count();
+                                                               filter(|entry| entry.0 == *claim_id).count();
                                                        assert!(num_existing == 0 || num_existing == 1);
                                                }
-                                               self.pending_claim_events.retain(|event| event.0 != *package_id);
-                                               self.pending_claim_events.push((*package_id, claim_event));
+                                               self.pending_claim_events.retain(|event| event.0 != *claim_id);
+                                               self.pending_claim_events.push((*claim_id, claim_event));
                                        },
                                }
-                               if let Some(request) = self.pending_claim_requests.get_mut(package_id) {
+                               if let Some(request) = self.pending_claim_requests.get_mut(claim_id) {
                                        request.set_timer(new_timer);
                                        request.set_feerate(new_feerate);
                                }
@@ -1035,7 +1010,7 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
                                self.onchain_events_awaiting_threshold_conf.push(entry);
                        }
                }
-               for ((_package_id, _), ref mut request) in bump_candidates.iter_mut() {
+               for ((_claim_id, _), ref mut request) in bump_candidates.iter_mut() {
                        // `height` is the height being disconnected, so our `current_height` is 1 lower.
                        let current_height = height - 1;
                        if let Some((new_timer, new_feerate, bump_claim)) = self.generate_claim(
@@ -1046,18 +1021,17 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
                                match bump_claim {
                                        OnchainClaim::Tx(bump_tx) => {
                                                log_info!(logger, "Broadcasting onchain {}", log_tx!(bump_tx));
-                                               broadcaster.broadcast_transaction(&bump_tx);
+                                               broadcaster.broadcast_transactions(&[&bump_tx]);
                                        },
-                                       #[cfg(anchors)]
                                        OnchainClaim::Event(claim_event) => {
                                                log_info!(logger, "Yielding onchain event after reorg to spend inputs {:?}", request.outpoints());
                                                #[cfg(debug_assertions)] {
                                                        let num_existing = self.pending_claim_events.iter()
-                                                               .filter(|entry| entry.0 == *_package_id).count();
+                                                               .filter(|entry| entry.0 == *_claim_id).count();
                                                        assert!(num_existing == 0 || num_existing == 1);
                                                }
-                                               self.pending_claim_events.retain(|event| event.0 != *_package_id);
-                                               self.pending_claim_events.push((*_package_id, claim_event));
+                                               self.pending_claim_events.retain(|event| event.0 != *_claim_id);
+                                               self.pending_claim_events.push((*_claim_id, claim_event));
                                        },
                                }
                        }
@@ -1178,7 +1152,6 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
                htlc_tx
        }
 
-       #[cfg(anchors)]
        pub(crate) fn generate_external_htlc_claim(
                &self, outp: &::bitcoin::OutPoint, preimage: &Option<PaymentPreimage>
        ) -> Option<ExternalHTLCClaim> {
@@ -1209,8 +1182,8 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
                        .or_else(|| self.prev_holder_commitment.as_ref().map(|c| find_htlc(c)).flatten())
        }
 
-       pub(crate) fn opt_anchors(&self) -> bool {
-               self.channel_transaction_parameters.opt_anchors.is_some()
+       pub(crate) fn channel_type_features(&self) -> &ChannelTypeFeatures {
+               &self.channel_transaction_parameters.channel_type_features
        }
 
        #[cfg(any(test,feature = "unsafe_revoked_tx_signing"))]
index 4604a164cd634534169e46f0df40670f346172c3..9e61cbc4598772c1a9fa8cdd31a2f330dbd8c548 100644 (file)
@@ -26,52 +26,74 @@ use crate::ln::chan_utils;
 use crate::ln::msgs::DecodeError;
 use crate::chain::chaininterface::{FeeEstimator, ConfirmationTarget, MIN_RELAY_FEE_SAT_PER_1000_WEIGHT};
 use crate::sign::WriteableEcdsaChannelSigner;
-#[cfg(anchors)]
-use crate::chain::onchaintx::ExternalHTLCClaim;
-use crate::chain::onchaintx::OnchainTxHandler;
+use crate::chain::onchaintx::{ExternalHTLCClaim, OnchainTxHandler};
 use crate::util::logger::Logger;
-use crate::util::ser::{Readable, Writer, Writeable};
+use crate::util::ser::{Readable, Writer, Writeable, RequiredWrapper};
 
 use crate::io;
 use crate::prelude::*;
 use core::cmp;
-#[cfg(anchors)]
 use core::convert::TryInto;
 use core::mem;
 use core::ops::Deref;
 use bitcoin::{PackedLockTime, Sequence, Witness};
+use crate::ln::features::ChannelTypeFeatures;
 
 use super::chaininterface::LowerBoundedFeeEstimator;
 
 const MAX_ALLOC_SIZE: usize = 64*1024;
 
 
-pub(crate) fn weight_revoked_offered_htlc(opt_anchors: bool) -> u64 {
+pub(crate) fn weight_revoked_offered_htlc(channel_type_features: &ChannelTypeFeatures) -> u64 {
        // number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
        const WEIGHT_REVOKED_OFFERED_HTLC: u64 = 1 + 1 + 73 + 1 + 33 + 1 + 133;
        const WEIGHT_REVOKED_OFFERED_HTLC_ANCHORS: u64 = WEIGHT_REVOKED_OFFERED_HTLC + 3; // + OP_1 + OP_CSV + OP_DROP
-       if opt_anchors { WEIGHT_REVOKED_OFFERED_HTLC_ANCHORS } else { WEIGHT_REVOKED_OFFERED_HTLC }
+       if channel_type_features.supports_anchors_zero_fee_htlc_tx() { WEIGHT_REVOKED_OFFERED_HTLC_ANCHORS } else { WEIGHT_REVOKED_OFFERED_HTLC }
 }
 
-pub(crate) fn weight_revoked_received_htlc(opt_anchors: bool) -> u64 {
+pub(crate) fn weight_revoked_received_htlc(channel_type_features: &ChannelTypeFeatures) -> u64 {
        // number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
        const WEIGHT_REVOKED_RECEIVED_HTLC: u64 = 1 + 1 + 73 + 1 + 33 + 1 +  139;
        const WEIGHT_REVOKED_RECEIVED_HTLC_ANCHORS: u64 = WEIGHT_REVOKED_RECEIVED_HTLC + 3; // + OP_1 + OP_CSV + OP_DROP
-       if opt_anchors { WEIGHT_REVOKED_RECEIVED_HTLC_ANCHORS } else { WEIGHT_REVOKED_RECEIVED_HTLC }
+       if channel_type_features.supports_anchors_zero_fee_htlc_tx() { WEIGHT_REVOKED_RECEIVED_HTLC_ANCHORS } else { WEIGHT_REVOKED_RECEIVED_HTLC }
 }
 
-pub(crate) fn weight_offered_htlc(opt_anchors: bool) -> u64 {
+pub(crate) fn weight_offered_htlc(channel_type_features: &ChannelTypeFeatures) -> u64 {
        // number_of_witness_elements + sig_length + counterpartyhtlc_sig  + preimage_length + preimage + witness_script_length + witness_script
        const WEIGHT_OFFERED_HTLC: u64 = 1 + 1 + 73 + 1 + 32 + 1 + 133;
        const WEIGHT_OFFERED_HTLC_ANCHORS: u64 = WEIGHT_OFFERED_HTLC + 3; // + OP_1 + OP_CSV + OP_DROP
-       if opt_anchors { WEIGHT_OFFERED_HTLC_ANCHORS } else { WEIGHT_OFFERED_HTLC }
+       if channel_type_features.supports_anchors_zero_fee_htlc_tx() { WEIGHT_OFFERED_HTLC_ANCHORS } else { WEIGHT_OFFERED_HTLC }
 }
 
-pub(crate) fn weight_received_htlc(opt_anchors: bool) -> u64 {
+pub(crate) fn weight_received_htlc(channel_type_features: &ChannelTypeFeatures) -> u64 {
        // number_of_witness_elements + sig_length + counterpartyhtlc_sig + empty_vec_length + empty_vec + witness_script_length + witness_script
        const WEIGHT_RECEIVED_HTLC: u64 = 1 + 1 + 73 + 1 + 1 + 1 + 139;
        const WEIGHT_RECEIVED_HTLC_ANCHORS: u64 = WEIGHT_RECEIVED_HTLC + 3; // + OP_1 + OP_CSV + OP_DROP
-       if opt_anchors { WEIGHT_RECEIVED_HTLC_ANCHORS } else { WEIGHT_RECEIVED_HTLC }
+       if channel_type_features.supports_anchors_zero_fee_htlc_tx() { WEIGHT_RECEIVED_HTLC_ANCHORS } else { WEIGHT_RECEIVED_HTLC }
+}
+
+/// Verifies deserializable channel type features
+pub(crate) fn verify_channel_type_features(channel_type_features: &Option<ChannelTypeFeatures>, additional_permitted_features: Option<&ChannelTypeFeatures>) -> Result<(), DecodeError> {
+       if let Some(features) = channel_type_features.as_ref() {
+               if features.requires_unknown_bits() {
+                       return Err(DecodeError::UnknownRequiredFeature);
+               }
+
+               let mut supported_feature_set = ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies();
+               supported_feature_set.set_scid_privacy_required();
+               supported_feature_set.set_zero_conf_required();
+
+               // allow the passing of an additional necessary permitted flag
+               if let Some(additional_permitted_features) = additional_permitted_features {
+                       supported_feature_set |= additional_permitted_features;
+               }
+
+               if !features.is_subset(&supported_feature_set) {
+                       return Err(DecodeError::UnknownRequiredFeature);
+               }
+       }
+
+       Ok(())
 }
 
 // number_of_witness_elements + sig_length + revocation_sig + true_length + op_true + witness_script_length + witness_script
@@ -147,8 +169,8 @@ pub(crate) struct RevokedHTLCOutput {
 }
 
 impl RevokedHTLCOutput {
-       pub(crate) fn build(per_commitment_point: PublicKey, counterparty_delayed_payment_base_key: PublicKey, counterparty_htlc_base_key: PublicKey, per_commitment_key: SecretKey, amount: u64, htlc: HTLCOutputInCommitment, opt_anchors: bool) -> Self {
-               let weight = if htlc.offered { weight_revoked_offered_htlc(opt_anchors) } else { weight_revoked_received_htlc(opt_anchors) };
+       pub(crate) fn build(per_commitment_point: PublicKey, counterparty_delayed_payment_base_key: PublicKey, counterparty_htlc_base_key: PublicKey, per_commitment_key: SecretKey, amount: u64, htlc: HTLCOutputInCommitment, channel_type_features: &ChannelTypeFeatures) -> Self {
+               let weight = if htlc.offered { weight_revoked_offered_htlc(channel_type_features) } else { weight_revoked_received_htlc(channel_type_features) };
                RevokedHTLCOutput {
                        per_commitment_point,
                        counterparty_delayed_payment_base_key,
@@ -177,6 +199,8 @@ impl_writeable_tlv_based!(RevokedHTLCOutput, {
 /// witnessScript.
 ///
 /// The preimage is used as part of the witness.
+///
+/// Note that on upgrades, some features of existing outputs may be missed.
 #[derive(Clone, PartialEq, Eq)]
 pub(crate) struct CounterpartyOfferedHTLCOutput {
        per_commitment_point: PublicKey,
@@ -184,146 +208,278 @@ pub(crate) struct CounterpartyOfferedHTLCOutput {
        counterparty_htlc_base_key: PublicKey,
        preimage: PaymentPreimage,
        htlc: HTLCOutputInCommitment,
-       opt_anchors: Option<()>,
+       channel_type_features: ChannelTypeFeatures,
 }
 
 impl CounterpartyOfferedHTLCOutput {
-       pub(crate) fn build(per_commitment_point: PublicKey, counterparty_delayed_payment_base_key: PublicKey, counterparty_htlc_base_key: PublicKey, preimage: PaymentPreimage, htlc: HTLCOutputInCommitment, opt_anchors: bool) -> Self {
+       pub(crate) fn build(per_commitment_point: PublicKey, counterparty_delayed_payment_base_key: PublicKey, counterparty_htlc_base_key: PublicKey, preimage: PaymentPreimage, htlc: HTLCOutputInCommitment, channel_type_features: ChannelTypeFeatures) -> Self {
                CounterpartyOfferedHTLCOutput {
                        per_commitment_point,
                        counterparty_delayed_payment_base_key,
                        counterparty_htlc_base_key,
                        preimage,
                        htlc,
-                       opt_anchors: if opt_anchors { Some(()) } else { None },
+                       channel_type_features,
                }
        }
+}
 
-       fn opt_anchors(&self) -> bool {
-               self.opt_anchors.is_some()
+impl Writeable for CounterpartyOfferedHTLCOutput {
+       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
+               let legacy_deserialization_prevention_marker = chan_utils::legacy_deserialization_prevention_marker_for_channel_type_features(&self.channel_type_features);
+               write_tlv_fields!(writer, {
+                       (0, self.per_commitment_point, required),
+                       (2, self.counterparty_delayed_payment_base_key, required),
+                       (4, self.counterparty_htlc_base_key, required),
+                       (6, self.preimage, required),
+                       (8, self.htlc, required),
+                       (10, legacy_deserialization_prevention_marker, option),
+                       (11, self.channel_type_features, required),
+               });
+               Ok(())
        }
 }
 
-impl_writeable_tlv_based!(CounterpartyOfferedHTLCOutput, {
-       (0, per_commitment_point, required),
-       (2, counterparty_delayed_payment_base_key, required),
-       (4, counterparty_htlc_base_key, required),
-       (6, preimage, required),
-       (8, htlc, required),
-       (10, opt_anchors, option),
-});
+impl Readable for CounterpartyOfferedHTLCOutput {
+       fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
+               let mut per_commitment_point = RequiredWrapper(None);
+               let mut counterparty_delayed_payment_base_key = RequiredWrapper(None);
+               let mut counterparty_htlc_base_key = RequiredWrapper(None);
+               let mut preimage = RequiredWrapper(None);
+               let mut htlc = RequiredWrapper(None);
+               let mut _legacy_deserialization_prevention_marker: Option<()> = None;
+               let mut channel_type_features = None;
+
+               read_tlv_fields!(reader, {
+                       (0, per_commitment_point, required),
+                       (2, counterparty_delayed_payment_base_key, required),
+                       (4, counterparty_htlc_base_key, required),
+                       (6, preimage, required),
+                       (8, htlc, required),
+                       (10, _legacy_deserialization_prevention_marker, option),
+                       (11, channel_type_features, option),
+               });
+
+               verify_channel_type_features(&channel_type_features, None)?;
+
+               Ok(Self {
+                       per_commitment_point: per_commitment_point.0.unwrap(),
+                       counterparty_delayed_payment_base_key: counterparty_delayed_payment_base_key.0.unwrap(),
+                       counterparty_htlc_base_key: counterparty_htlc_base_key.0.unwrap(),
+                       preimage: preimage.0.unwrap(),
+                       htlc: htlc.0.unwrap(),
+                       channel_type_features: channel_type_features.unwrap_or(ChannelTypeFeatures::only_static_remote_key())
+               })
+       }
+}
 
 /// A struct to describe a HTLC output on a counterparty commitment transaction.
 ///
 /// HTLCOutputInCommitment (hash, timelock, directon) and pubkeys are used to generate a suitable
 /// witnessScript.
+///
+/// Note that on upgrades, some features of existing outputs may be missed.
 #[derive(Clone, PartialEq, Eq)]
 pub(crate) struct CounterpartyReceivedHTLCOutput {
        per_commitment_point: PublicKey,
        counterparty_delayed_payment_base_key: PublicKey,
        counterparty_htlc_base_key: PublicKey,
        htlc: HTLCOutputInCommitment,
-       opt_anchors: Option<()>,
+       channel_type_features: ChannelTypeFeatures,
 }
 
 impl CounterpartyReceivedHTLCOutput {
-       pub(crate) fn build(per_commitment_point: PublicKey, counterparty_delayed_payment_base_key: PublicKey, counterparty_htlc_base_key: PublicKey, htlc: HTLCOutputInCommitment, opt_anchors: bool) -> Self {
+       pub(crate) fn build(per_commitment_point: PublicKey, counterparty_delayed_payment_base_key: PublicKey, counterparty_htlc_base_key: PublicKey, htlc: HTLCOutputInCommitment, channel_type_features: ChannelTypeFeatures) -> Self {
                CounterpartyReceivedHTLCOutput {
                        per_commitment_point,
                        counterparty_delayed_payment_base_key,
                        counterparty_htlc_base_key,
                        htlc,
-                       opt_anchors: if opt_anchors { Some(()) } else { None },
+                       channel_type_features
                }
        }
+}
 
-       fn opt_anchors(&self) -> bool {
-               self.opt_anchors.is_some()
+impl Writeable for CounterpartyReceivedHTLCOutput {
+       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
+               let legacy_deserialization_prevention_marker = chan_utils::legacy_deserialization_prevention_marker_for_channel_type_features(&self.channel_type_features);
+               write_tlv_fields!(writer, {
+                       (0, self.per_commitment_point, required),
+                       (2, self.counterparty_delayed_payment_base_key, required),
+                       (4, self.counterparty_htlc_base_key, required),
+                       (6, self.htlc, required),
+                       (8, legacy_deserialization_prevention_marker, option),
+                       (9, self.channel_type_features, required),
+               });
+               Ok(())
        }
 }
 
-impl_writeable_tlv_based!(CounterpartyReceivedHTLCOutput, {
-       (0, per_commitment_point, required),
-       (2, counterparty_delayed_payment_base_key, required),
-       (4, counterparty_htlc_base_key, required),
-       (6, htlc, required),
-       (8, opt_anchors, option),
-});
+impl Readable for CounterpartyReceivedHTLCOutput {
+       fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
+               let mut per_commitment_point = RequiredWrapper(None);
+               let mut counterparty_delayed_payment_base_key = RequiredWrapper(None);
+               let mut counterparty_htlc_base_key = RequiredWrapper(None);
+               let mut htlc = RequiredWrapper(None);
+               let mut _legacy_deserialization_prevention_marker: Option<()> = None;
+               let mut channel_type_features = None;
+
+               read_tlv_fields!(reader, {
+                       (0, per_commitment_point, required),
+                       (2, counterparty_delayed_payment_base_key, required),
+                       (4, counterparty_htlc_base_key, required),
+                       (6, htlc, required),
+                       (8, _legacy_deserialization_prevention_marker, option),
+                       (9, channel_type_features, option),
+               });
+
+               verify_channel_type_features(&channel_type_features, None)?;
+
+               Ok(Self {
+                       per_commitment_point: per_commitment_point.0.unwrap(),
+                       counterparty_delayed_payment_base_key: counterparty_delayed_payment_base_key.0.unwrap(),
+                       counterparty_htlc_base_key: counterparty_htlc_base_key.0.unwrap(),
+                       htlc: htlc.0.unwrap(),
+                       channel_type_features: channel_type_features.unwrap_or(ChannelTypeFeatures::only_static_remote_key())
+               })
+       }
+}
 
 /// A struct to describe a HTLC output on holder commitment transaction.
 ///
 /// Either offered or received, the amount is always used as part of the bip143 sighash.
 /// Preimage is only included as part of the witness in former case.
+///
+/// Note that on upgrades, some features of existing outputs may be missed.
 #[derive(Clone, PartialEq, Eq)]
 pub(crate) struct HolderHTLCOutput {
        preimage: Option<PaymentPreimage>,
        amount_msat: u64,
        /// Defaults to 0 for HTLC-Success transactions, which have no expiry
        cltv_expiry: u32,
-       opt_anchors: Option<()>,
+       channel_type_features: ChannelTypeFeatures,
 }
 
 impl HolderHTLCOutput {
-       pub(crate) fn build_offered(amount_msat: u64, cltv_expiry: u32, opt_anchors: bool) -> Self {
+       pub(crate) fn build_offered(amount_msat: u64, cltv_expiry: u32, channel_type_features: ChannelTypeFeatures) -> Self {
                HolderHTLCOutput {
                        preimage: None,
                        amount_msat,
                        cltv_expiry,
-                       opt_anchors: if opt_anchors { Some(()) } else { None } ,
+                       channel_type_features,
                }
        }
 
-       pub(crate) fn build_accepted(preimage: PaymentPreimage, amount_msat: u64, opt_anchors: bool) -> Self {
+       pub(crate) fn build_accepted(preimage: PaymentPreimage, amount_msat: u64, channel_type_features: ChannelTypeFeatures) -> Self {
                HolderHTLCOutput {
                        preimage: Some(preimage),
                        amount_msat,
                        cltv_expiry: 0,
-                       opt_anchors: if opt_anchors { Some(()) } else { None } ,
+                       channel_type_features,
                }
        }
+}
 
-       fn opt_anchors(&self) -> bool {
-               self.opt_anchors.is_some()
+impl Writeable for HolderHTLCOutput {
+       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
+               let legacy_deserialization_prevention_marker = chan_utils::legacy_deserialization_prevention_marker_for_channel_type_features(&self.channel_type_features);
+               write_tlv_fields!(writer, {
+                       (0, self.amount_msat, required),
+                       (2, self.cltv_expiry, required),
+                       (4, self.preimage, option),
+                       (6, legacy_deserialization_prevention_marker, option),
+                       (7, self.channel_type_features, required),
+               });
+               Ok(())
        }
 }
 
-impl_writeable_tlv_based!(HolderHTLCOutput, {
-       (0, amount_msat, required),
-       (2, cltv_expiry, required),
-       (4, preimage, option),
-       (6, opt_anchors, option)
-});
+impl Readable for HolderHTLCOutput {
+       fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
+               let mut amount_msat = RequiredWrapper(None);
+               let mut cltv_expiry = RequiredWrapper(None);
+               let mut preimage = None;
+               let mut _legacy_deserialization_prevention_marker: Option<()> = None;
+               let mut channel_type_features = None;
+
+               read_tlv_fields!(reader, {
+                       (0, amount_msat, required),
+                       (2, cltv_expiry, required),
+                       (4, preimage, option),
+                       (6, _legacy_deserialization_prevention_marker, option),
+                       (7, channel_type_features, option),
+               });
+
+               verify_channel_type_features(&channel_type_features, None)?;
+
+               Ok(Self {
+                       amount_msat: amount_msat.0.unwrap(),
+                       cltv_expiry: cltv_expiry.0.unwrap(),
+                       preimage,
+                       channel_type_features: channel_type_features.unwrap_or(ChannelTypeFeatures::only_static_remote_key())
+               })
+       }
+}
 
 /// A struct to describe the channel output on the funding transaction.
 ///
 /// witnessScript is used as part of the witness redeeming the funding utxo.
+///
+/// Note that on upgrades, some features of existing outputs may be missed.
 #[derive(Clone, PartialEq, Eq)]
 pub(crate) struct HolderFundingOutput {
        funding_redeemscript: Script,
        funding_amount: Option<u64>,
-       opt_anchors: Option<()>,
+       channel_type_features: ChannelTypeFeatures,
 }
 
 
 impl HolderFundingOutput {
-       pub(crate) fn build(funding_redeemscript: Script, funding_amount: u64, opt_anchors: bool) -> Self {
+       pub(crate) fn build(funding_redeemscript: Script, funding_amount: u64, channel_type_features: ChannelTypeFeatures) -> Self {
                HolderFundingOutput {
                        funding_redeemscript,
                        funding_amount: Some(funding_amount),
-                       opt_anchors: if opt_anchors { Some(()) } else { None },
+                       channel_type_features,
                }
        }
+}
 
-       fn opt_anchors(&self) -> bool {
-               self.opt_anchors.is_some()
+impl Writeable for HolderFundingOutput {
+       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
+               let legacy_deserialization_prevention_marker = chan_utils::legacy_deserialization_prevention_marker_for_channel_type_features(&self.channel_type_features);
+               write_tlv_fields!(writer, {
+                       (0, self.funding_redeemscript, required),
+                       (1, self.channel_type_features, required),
+                       (2, legacy_deserialization_prevention_marker, option),
+                       (3, self.funding_amount, option),
+               });
+               Ok(())
        }
 }
 
-impl_writeable_tlv_based!(HolderFundingOutput, {
-       (0, funding_redeemscript, required),
-       (2, opt_anchors, option),
-       (3, funding_amount, option),
-});
+impl Readable for HolderFundingOutput {
+       fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
+               let mut funding_redeemscript = RequiredWrapper(None);
+               let mut _legacy_deserialization_prevention_marker: Option<()> = None;
+               let mut channel_type_features = None;
+               let mut funding_amount = None;
+
+               read_tlv_fields!(reader, {
+                       (0, funding_redeemscript, required),
+                       (1, channel_type_features, option),
+                       (2, _legacy_deserialization_prevention_marker, option),
+                       (3, funding_amount, option)
+               });
+
+               verify_channel_type_features(&channel_type_features, None)?;
+
+               Ok(Self {
+                       funding_redeemscript: funding_redeemscript.0.unwrap(),
+                       channel_type_features: channel_type_features.unwrap_or(ChannelTypeFeatures::only_static_remote_key()),
+                       funding_amount
+               })
+       }
+}
 
 /// A wrapper encapsulating all in-protocol differing outputs types.
 ///
@@ -347,11 +503,11 @@ impl PackageSolvingData {
                        PackageSolvingData::CounterpartyOfferedHTLCOutput(ref outp) => outp.htlc.amount_msat / 1000,
                        PackageSolvingData::CounterpartyReceivedHTLCOutput(ref outp) => outp.htlc.amount_msat / 1000,
                        PackageSolvingData::HolderHTLCOutput(ref outp) => {
-                               debug_assert!(outp.opt_anchors());
+                               debug_assert!(outp.channel_type_features.supports_anchors_zero_fee_htlc_tx());
                                outp.amount_msat / 1000
                        },
                        PackageSolvingData::HolderFundingOutput(ref outp) => {
-                               debug_assert!(outp.opt_anchors());
+                               debug_assert!(outp.channel_type_features.supports_anchors_zero_fee_htlc_tx());
                                outp.funding_amount.unwrap()
                        }
                };
@@ -361,14 +517,14 @@ impl PackageSolvingData {
                match self {
                        PackageSolvingData::RevokedOutput(ref outp) => outp.weight as usize,
                        PackageSolvingData::RevokedHTLCOutput(ref outp) => outp.weight as usize,
-                       PackageSolvingData::CounterpartyOfferedHTLCOutput(ref outp) => weight_offered_htlc(outp.opt_anchors()) as usize,
-                       PackageSolvingData::CounterpartyReceivedHTLCOutput(ref outp) => weight_received_htlc(outp.opt_anchors()) as usize,
+                       PackageSolvingData::CounterpartyOfferedHTLCOutput(ref outp) => weight_offered_htlc(&outp.channel_type_features) as usize,
+                       PackageSolvingData::CounterpartyReceivedHTLCOutput(ref outp) => weight_received_htlc(&outp.channel_type_features) as usize,
                        PackageSolvingData::HolderHTLCOutput(ref outp) => {
-                               debug_assert!(outp.opt_anchors());
+                               debug_assert!(outp.channel_type_features.supports_anchors_zero_fee_htlc_tx());
                                if outp.preimage.is_none() {
-                                       weight_offered_htlc(true) as usize
+                                       weight_offered_htlc(&outp.channel_type_features) as usize
                                } else {
-                                       weight_received_htlc(true) as usize
+                                       weight_received_htlc(&outp.channel_type_features) as usize
                                }
                        },
                        // Since HolderFundingOutput maps to an untractable package that is already signed, its
@@ -411,7 +567,7 @@ impl PackageSolvingData {
                        },
                        PackageSolvingData::RevokedHTLCOutput(ref outp) => {
                                let chan_keys = TxCreationKeys::derive_new(&onchain_handler.secp_ctx, &outp.per_commitment_point, &outp.counterparty_delayed_payment_base_key, &outp.counterparty_htlc_base_key, &onchain_handler.signer.pubkeys().revocation_basepoint, &onchain_handler.signer.pubkeys().htlc_basepoint);
-                               let witness_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&outp.htlc, onchain_handler.opt_anchors(), &chan_keys.broadcaster_htlc_key, &chan_keys.countersignatory_htlc_key, &chan_keys.revocation_key);
+                               let witness_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&outp.htlc, &onchain_handler.channel_type_features(), &chan_keys.broadcaster_htlc_key, &chan_keys.countersignatory_htlc_key, &chan_keys.revocation_key);
                                //TODO: should we panic on signer failure ?
                                if let Ok(sig) = onchain_handler.signer.sign_justice_revoked_htlc(&bumped_tx, i, outp.amount, &outp.per_commitment_key, &outp.htlc, &onchain_handler.secp_ctx) {
                                        let mut ser_sig = sig.serialize_der().to_vec();
@@ -423,7 +579,7 @@ impl PackageSolvingData {
                        },
                        PackageSolvingData::CounterpartyOfferedHTLCOutput(ref outp) => {
                                let chan_keys = TxCreationKeys::derive_new(&onchain_handler.secp_ctx, &outp.per_commitment_point, &outp.counterparty_delayed_payment_base_key, &outp.counterparty_htlc_base_key, &onchain_handler.signer.pubkeys().revocation_basepoint, &onchain_handler.signer.pubkeys().htlc_basepoint);
-                               let witness_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&outp.htlc, onchain_handler.opt_anchors(), &chan_keys.broadcaster_htlc_key, &chan_keys.countersignatory_htlc_key, &chan_keys.revocation_key);
+                               let witness_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&outp.htlc, &onchain_handler.channel_type_features(), &chan_keys.broadcaster_htlc_key, &chan_keys.countersignatory_htlc_key, &chan_keys.revocation_key);
 
                                if let Ok(sig) = onchain_handler.signer.sign_counterparty_htlc_transaction(&bumped_tx, i, &outp.htlc.amount_msat / 1000, &outp.per_commitment_point, &outp.htlc, &onchain_handler.secp_ctx) {
                                        let mut ser_sig = sig.serialize_der().to_vec();
@@ -435,7 +591,7 @@ impl PackageSolvingData {
                        },
                        PackageSolvingData::CounterpartyReceivedHTLCOutput(ref outp) => {
                                let chan_keys = TxCreationKeys::derive_new(&onchain_handler.secp_ctx, &outp.per_commitment_point, &outp.counterparty_delayed_payment_base_key, &outp.counterparty_htlc_base_key, &onchain_handler.signer.pubkeys().revocation_basepoint, &onchain_handler.signer.pubkeys().htlc_basepoint);
-                               let witness_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&outp.htlc, onchain_handler.opt_anchors(), &chan_keys.broadcaster_htlc_key, &chan_keys.countersignatory_htlc_key, &chan_keys.revocation_key);
+                               let witness_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&outp.htlc, &onchain_handler.channel_type_features(), &chan_keys.broadcaster_htlc_key, &chan_keys.countersignatory_htlc_key, &chan_keys.revocation_key);
 
                                if let Ok(sig) = onchain_handler.signer.sign_counterparty_htlc_transaction(&bumped_tx, i, &outp.htlc.amount_msat / 1000, &outp.per_commitment_point, &outp.htlc, &onchain_handler.secp_ctx) {
                                        let mut ser_sig = sig.serialize_der().to_vec();
@@ -453,7 +609,7 @@ impl PackageSolvingData {
        fn get_finalized_tx<Signer: WriteableEcdsaChannelSigner>(&self, outpoint: &BitcoinOutPoint, onchain_handler: &mut OnchainTxHandler<Signer>) -> Option<Transaction> {
                match self {
                        PackageSolvingData::HolderHTLCOutput(ref outp) => {
-                               debug_assert!(!outp.opt_anchors());
+                               debug_assert!(!outp.channel_type_features.supports_anchors_zero_fee_htlc_tx());
                                return onchain_handler.get_fully_signed_htlc_tx(outpoint, &outp.preimage);
                        }
                        PackageSolvingData::HolderFundingOutput(ref outp) => {
@@ -491,7 +647,7 @@ impl PackageSolvingData {
                        PackageSolvingData::RevokedHTLCOutput(..) => { (PackageMalleability::Malleable, true) },
                        PackageSolvingData::CounterpartyOfferedHTLCOutput(..) => { (PackageMalleability::Malleable, true) },
                        PackageSolvingData::CounterpartyReceivedHTLCOutput(..) => { (PackageMalleability::Malleable, false) },
-                       PackageSolvingData::HolderHTLCOutput(ref outp) => if outp.opt_anchors() {
+                       PackageSolvingData::HolderHTLCOutput(ref outp) => if outp.channel_type_features.supports_anchors_zero_fee_htlc_tx() {
                                (PackageMalleability::Malleable, outp.preimage.is_some())
                        } else {
                                (PackageMalleability::Untractable, false)
@@ -707,7 +863,6 @@ impl PackageTemplate {
                let output_weight = (8 + 1 + destination_script.len()) * WITNESS_SCALE_FACTOR;
                inputs_weight + witnesses_weight + transaction_weight + output_weight
        }
-       #[cfg(anchors)]
        pub(crate) fn construct_malleable_package_with_external_funding<Signer: WriteableEcdsaChannelSigner>(
                &self, onchain_handler: &mut OnchainTxHandler<Signer>,
        ) -> Option<Vec<ExternalHTLCClaim>> {
@@ -716,7 +871,7 @@ impl PackageTemplate {
                for (previous_output, input) in &self.inputs {
                        match input {
                                PackageSolvingData::HolderHTLCOutput(ref outp) => {
-                                       debug_assert!(outp.opt_anchors());
+                                       debug_assert!(outp.channel_type_features.supports_anchors_zero_fee_htlc_tx());
                                        onchain_handler.generate_external_htlc_claim(&previous_output, &outp.preimage).map(|htlc| {
                                                htlcs.get_or_insert_with(|| Vec::with_capacity(self.inputs.len())).push(htlc);
                                        });
@@ -812,7 +967,6 @@ impl PackageTemplate {
                None
        }
 
-       #[cfg(anchors)]
        /// Computes a feerate based on the given confirmation target. If a previous feerate was used,
        /// the new feerate is below it, and `force_feerate_bump` is set, we'll use a 25% increase of
        /// the previous feerate instead of the new feerate.
@@ -840,8 +994,8 @@ impl PackageTemplate {
        /// attached to help the spending transaction reach confirmation.
        pub(crate) fn requires_external_funding(&self) -> bool {
                self.inputs.iter().find(|input| match input.1 {
-                       PackageSolvingData::HolderFundingOutput(ref outp) => outp.opt_anchors(),
-                       PackageSolvingData::HolderHTLCOutput(ref outp) => outp.opt_anchors(),
+                       PackageSolvingData::HolderFundingOutput(ref outp) => outp.channel_type_features.supports_anchors_zero_fee_htlc_tx(),
+                       PackageSolvingData::HolderHTLCOutput(ref outp) => outp.channel_type_features.supports_anchors_zero_fee_htlc_tx(),
                        _ => false,
                }).is_some()
        }
@@ -1025,6 +1179,7 @@ mod tests {
 
        use bitcoin::secp256k1::{PublicKey,SecretKey};
        use bitcoin::secp256k1::Secp256k1;
+       use crate::ln::features::ChannelTypeFeatures;
 
        macro_rules! dumb_revk_output {
                ($secp_ctx: expr, $is_counterparty_balance_on_anchors: expr) => {
@@ -1065,7 +1220,7 @@ mod tests {
                () => {
                        {
                                let preimage = PaymentPreimage([2;32]);
-                               PackageSolvingData::HolderHTLCOutput(HolderHTLCOutput::build_accepted(preimage, 0, false))
+                               PackageSolvingData::HolderHTLCOutput(HolderHTLCOutput::build_accepted(preimage, 0, ChannelTypeFeatures::only_static_remote_key()))
                        }
                }
        }
@@ -1153,7 +1308,7 @@ mod tests {
                let txid = Txid::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
                let secp_ctx = Secp256k1::new();
                let revk_outp = dumb_revk_output!(secp_ctx, false);
-               let counterparty_outp = dumb_counterparty_output!(secp_ctx, 0, false);
+               let counterparty_outp = dumb_counterparty_output!(secp_ctx, 0, ChannelTypeFeatures::only_static_remote_key());
 
                let mut revoked_package = PackageTemplate::build_package(txid, 0, revk_outp, 1000, 100);
                let counterparty_package = PackageTemplate::build_package(txid, 1, counterparty_outp, 1000, 100);
@@ -1214,7 +1369,7 @@ mod tests {
        fn test_package_amounts() {
                let txid = Txid::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
                let secp_ctx = Secp256k1::new();
-               let counterparty_outp = dumb_counterparty_output!(secp_ctx, 1_000_000, false);
+               let counterparty_outp = dumb_counterparty_output!(secp_ctx, 1_000_000, ChannelTypeFeatures::only_static_remote_key());
 
                let package = PackageTemplate::build_package(txid, 0, counterparty_outp, 1000, 100);
                assert_eq!(package.package_amount(), 1000);
@@ -1235,18 +1390,18 @@ mod tests {
                }
 
                {
-                       for &opt_anchors in [false, true].iter() {
-                               let counterparty_outp = dumb_counterparty_output!(secp_ctx, 1_000_000, opt_anchors);
+                       for channel_type_features in [ChannelTypeFeatures::only_static_remote_key(), ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies()].iter() {
+                               let counterparty_outp = dumb_counterparty_output!(secp_ctx, 1_000_000, channel_type_features.clone());
                                let package = PackageTemplate::build_package(txid, 0, counterparty_outp, 1000, 100);
-                               assert_eq!(package.package_weight(&Script::new()), weight_sans_output + weight_received_htlc(opt_anchors) as usize);
+                               assert_eq!(package.package_weight(&Script::new()), weight_sans_output + weight_received_htlc(channel_type_features) as usize);
                        }
                }
 
                {
-                       for &opt_anchors in [false, true].iter() {
-                               let counterparty_outp = dumb_counterparty_offered_output!(secp_ctx, 1_000_000, opt_anchors);
+                       for channel_type_features in [ChannelTypeFeatures::only_static_remote_key(), ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies()].iter() {
+                               let counterparty_outp = dumb_counterparty_offered_output!(secp_ctx, 1_000_000, channel_type_features.clone());
                                let package = PackageTemplate::build_package(txid, 0, counterparty_outp, 1000, 100);
-                               assert_eq!(package.package_weight(&Script::new()), weight_sans_output + weight_offered_htlc(opt_anchors) as usize);
+                               assert_eq!(package.package_weight(&Script::new()), weight_sans_output + weight_offered_htlc(channel_type_features) as usize);
                        }
                }
        }
index 950a31af37d8480a6eb080fc6d874360c19798cd..6d8a5bb6c65f55341a4a4160408e1949570ed998 100644 (file)
@@ -9,15 +9,46 @@
 
 //! Utitilies for bumping transactions originating from [`super::Event`]s.
 
-use crate::ln::PaymentPreimage;
+use core::convert::TryInto;
+use core::ops::Deref;
+
+use crate::chain::chaininterface::BroadcasterInterface;
+use crate::chain::ClaimId;
+use crate::events::Event;
+use crate::io_extras::sink;
 use crate::ln::chan_utils;
-use crate::ln::chan_utils::{ChannelTransactionParameters, HTLCOutputInCommitment};
+use crate::ln::chan_utils::{
+       ANCHOR_INPUT_WITNESS_WEIGHT, HTLC_SUCCESS_INPUT_ANCHOR_WITNESS_WEIGHT,
+       HTLC_TIMEOUT_INPUT_ANCHOR_WITNESS_WEIGHT, ChannelTransactionParameters, HTLCOutputInCommitment
+};
+use crate::ln::features::ChannelTypeFeatures;
+use crate::ln::PaymentPreimage;
+use crate::prelude::*;
+use crate::sign::{ChannelSigner, EcdsaChannelSigner, SignerProvider};
+use crate::sync::Mutex;
+use crate::util::logger::Logger;
 
-use bitcoin::{OutPoint, PackedLockTime, Script, Transaction, Txid, TxIn, TxOut, Witness};
+use bitcoin::{OutPoint, PackedLockTime, PubkeyHash, Sequence, Script, Transaction, Txid, TxIn, TxOut, Witness, WPubkeyHash};
+use bitcoin::blockdata::constants::WITNESS_SCALE_FACTOR;
+use bitcoin::consensus::Encodable;
 use bitcoin::secp256k1;
 use bitcoin::secp256k1::{PublicKey, Secp256k1};
 use bitcoin::secp256k1::ecdsa::Signature;
 
+const EMPTY_SCRIPT_SIG_WEIGHT: u64 = 1 /* empty script_sig */ * WITNESS_SCALE_FACTOR as u64;
+
+const BASE_INPUT_SIZE: u64 = 32 /* txid */ + 4 /* vout */ + 4 /* sequence */;
+
+const BASE_INPUT_WEIGHT: u64 = BASE_INPUT_SIZE * WITNESS_SCALE_FACTOR as u64;
+
+// TODO: Define typed abstraction over feerates to handle their conversions.
+fn compute_feerate_sat_per_1000_weight(fee_sat: u64, weight: u64) -> u32 {
+       (fee_sat * 1000 / weight).try_into().unwrap_or(u32::max_value())
+}
+const fn fee_for_weight(feerate_sat_per_1000_weight: u32, weight: u64) -> u64 {
+       ((feerate_sat_per_1000_weight as u64 * weight) + 1000 - 1) / 1000
+}
+
 /// A descriptor used to sign for a commitment transaction's anchor output.
 #[derive(Clone, Debug, PartialEq, Eq)]
 pub struct AnchorDescriptor {
@@ -74,7 +105,7 @@ impl HTLCDescriptor {
        /// Returns the unsigned transaction input spending the HTLC output in the commitment
        /// transaction.
        pub fn unsigned_tx_input(&self) -> TxIn {
-               chan_utils::build_htlc_input(&self.commitment_txid, &self.htlc, true /* opt_anchors */)
+               chan_utils::build_htlc_input(&self.commitment_txid, &self.htlc, &ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies())
        }
 
        /// Returns the delayed output created as a result of spending the HTLC output in the commitment
@@ -92,8 +123,8 @@ impl HTLCDescriptor {
                        secp, per_commitment_point, &counterparty_keys.revocation_basepoint
                );
                chan_utils::build_htlc_output(
-                       0 /* feerate_per_kw */, channel_params.contest_delay(), &self.htlc, true /* opt_anchors */,
-                       false /* use_non_zero_fee_anchors */, &broadcaster_delayed_key, &counterparty_revocation_key
+                       0 /* feerate_per_kw */, channel_params.contest_delay(), &self.htlc,
+                       &ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies(), &broadcaster_delayed_key, &counterparty_revocation_key
                )
        }
 
@@ -114,7 +145,7 @@ impl HTLCDescriptor {
                        secp, per_commitment_point, &counterparty_keys.revocation_basepoint
                );
                chan_utils::get_htlc_redeemscript_with_explicit_keys(
-                       &self.htlc, true /* opt_anchors */, &broadcaster_htlc_key, &counterparty_htlc_key,
+                       &self.htlc, &ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies(), &broadcaster_htlc_key, &counterparty_htlc_key,
                        &counterparty_revocation_key,
                )
        }
@@ -123,7 +154,7 @@ impl HTLCDescriptor {
        /// transaction.
        pub fn tx_input_witness(&self, signature: &Signature, witness_script: &Script) -> Witness {
                chan_utils::build_htlc_input_witness(
-                       signature, &self.counterparty_sig, &self.preimage, witness_script, true /* opt_anchors */
+                       signature, &self.counterparty_sig, &self.preimage, witness_script, &ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies() /* opt_anchors */
                )
        }
 }
@@ -173,6 +204,12 @@ pub enum BumpTransactionEvent {
        /// [`EcdsaChannelSigner::sign_holder_anchor_input`]: crate::sign::EcdsaChannelSigner::sign_holder_anchor_input
        /// [`build_anchor_input_witness`]: crate::ln::chan_utils::build_anchor_input_witness
        ChannelClose {
+               /// The unique identifier for the claim of the anchor output in the commitment transaction.
+               ///
+               /// The identifier must map to the set of external UTXOs assigned to the claim, such that
+               /// they can be reused when a new claim with the same identifier needs to be made, resulting
+               /// in a fee-bumping attempt.
+               claim_id: ClaimId,
                /// The target feerate that the transaction package, which consists of the commitment
                /// transaction and the to-be-crafted child anchor transaction, must meet.
                package_target_feerate_sat_per_1000_weight: u32,
@@ -222,6 +259,13 @@ pub enum BumpTransactionEvent {
        /// [`EcdsaChannelSigner::sign_holder_htlc_transaction`]: crate::sign::EcdsaChannelSigner::sign_holder_htlc_transaction
        /// [`HTLCDescriptor::tx_input_witness`]: HTLCDescriptor::tx_input_witness
        HTLCResolution {
+               /// The unique identifier for the claim of the HTLCs in the confirmed commitment
+               /// transaction.
+               ///
+               /// The identifier must map to the set of external UTXOs assigned to the claim, such that
+               /// they can be reused when a new claim with the same identifier needs to be made, resulting
+               /// in a fee-bumping attempt.
+               claim_id: ClaimId,
                /// The target feerate that the resulting HTLC transaction must meet.
                target_feerate_sat_per_1000_weight: u32,
                /// The set of pending HTLCs on the confirmed commitment that need to be claimed, preferably
@@ -231,3 +275,510 @@ pub enum BumpTransactionEvent {
                tx_lock_time: PackedLockTime,
        },
 }
+
+/// An input that must be included in a transaction when performing coin selection through
+/// [`CoinSelectionSource::select_confirmed_utxos`]. It is guaranteed to be a SegWit input, so it
+/// must have an empty [`TxIn::script_sig`] when spent.
+pub struct Input {
+       /// The unique identifier of the input.
+       pub outpoint: OutPoint,
+       /// The upper-bound weight consumed by the input's full [`TxIn::script_sig`] and
+       /// [`TxIn::witness`], each with their lengths included, required to satisfy the output's
+       /// script.
+       pub satisfaction_weight: u64,
+}
+
+/// An unspent transaction output that is available to spend resulting from a successful
+/// [`CoinSelection`] attempt.
+#[derive(Clone, Debug)]
+pub struct Utxo {
+       /// The unique identifier of the output.
+       pub outpoint: OutPoint,
+       /// The output to spend.
+       pub output: TxOut,
+       /// The upper-bound weight consumed by the input's full [`TxIn::script_sig`] and [`TxIn::witness`], each
+       /// with their lengths included, required to satisfy the output's script. The weight consumed by
+       /// the input's `script_sig` must account for [`WITNESS_SCALE_FACTOR`].
+       pub satisfaction_weight: u64,
+}
+
+impl Utxo {
+       const P2WPKH_WITNESS_WEIGHT: u64 = 1 /* num stack items */ +
+               1 /* sig length */ +
+               73 /* sig including sighash flag */ +
+               1 /* pubkey length */ +
+               33 /* pubkey */;
+
+       /// Returns a `Utxo` with the `satisfaction_weight` estimate for a legacy P2PKH output.
+       pub fn new_p2pkh(outpoint: OutPoint, value: u64, pubkey_hash: &PubkeyHash) -> Self {
+               let script_sig_size = 1 /* script_sig length */ +
+                       1 /* OP_PUSH73 */ +
+                       73 /* sig including sighash flag */ +
+                       1 /* OP_PUSH33 */ +
+                       33 /* pubkey */;
+               Self {
+                       outpoint,
+                       output: TxOut {
+                               value,
+                               script_pubkey: Script::new_p2pkh(pubkey_hash),
+                       },
+                       satisfaction_weight: script_sig_size * WITNESS_SCALE_FACTOR as u64 + 1 /* empty witness */,
+               }
+       }
+
+       /// Returns a `Utxo` with the `satisfaction_weight` estimate for a P2WPKH nested in P2SH output.
+       pub fn new_nested_p2wpkh(outpoint: OutPoint, value: u64, pubkey_hash: &WPubkeyHash) -> Self {
+               let script_sig_size = 1 /* script_sig length */ +
+                       1 /* OP_0 */ +
+                       1 /* OP_PUSH20 */ +
+                       20 /* pubkey_hash */;
+               Self {
+                       outpoint,
+                       output: TxOut {
+                               value,
+                               script_pubkey: Script::new_p2sh(&Script::new_v0_p2wpkh(pubkey_hash).script_hash()),
+                       },
+                       satisfaction_weight: script_sig_size * WITNESS_SCALE_FACTOR as u64 + Self::P2WPKH_WITNESS_WEIGHT,
+               }
+       }
+
+       /// Returns a `Utxo` with the `satisfaction_weight` estimate for a SegWit v0 P2WPKH output.
+       pub fn new_v0_p2wpkh(outpoint: OutPoint, value: u64, pubkey_hash: &WPubkeyHash) -> Self {
+               Self {
+                       outpoint,
+                       output: TxOut {
+                               value,
+                               script_pubkey: Script::new_v0_p2wpkh(pubkey_hash),
+                       },
+                       satisfaction_weight: EMPTY_SCRIPT_SIG_WEIGHT + Self::P2WPKH_WITNESS_WEIGHT,
+               }
+       }
+}
+
+/// The result of a successful coin selection attempt for a transaction requiring additional UTXOs
+/// to cover its fees.
+pub struct CoinSelection {
+       /// The set of UTXOs (with at least 1 confirmation) to spend and use within a transaction
+       /// requiring additional fees.
+       confirmed_utxos: Vec<Utxo>,
+       /// An additional output tracking whether any change remained after coin selection. This output
+       /// should always have a value above dust for its given `script_pubkey`. It should not be
+       /// spent until the transaction it belongs to confirms to ensure mempool descendant limits are
+       /// not met. This implies no other party should be able to spend it except us.
+       change_output: Option<TxOut>,
+}
+
+/// An abstraction over a bitcoin wallet that can perform coin selection over a set of UTXOs and can
+/// sign for them. The coin selection method aims to mimic Bitcoin Core's `fundrawtransaction` RPC,
+/// which most wallets should be able to satisfy. Otherwise, consider implementing [`WalletSource`],
+/// which can provide a default implementation of this trait when used with [`Wallet`].
+pub trait CoinSelectionSource {
+       /// Performs coin selection of a set of UTXOs, with at least 1 confirmation each, that are
+       /// available to spend. Implementations are free to pick their coin selection algorithm of
+       /// choice, as long as the following requirements are met:
+       ///
+       /// 1. `must_spend` contains a set of [`Input`]s that must be included in the transaction
+       ///    throughout coin selection, but must not be returned as part of the result.
+       /// 2. `must_pay_to` contains a set of [`TxOut`]s that must be included in the transaction
+       ///    throughout coin selection. In some cases, like when funding an anchor transaction, this
+       ///    set is empty. Implementations should ensure they handle this correctly on their end,
+       ///    e.g., Bitcoin Core's `fundrawtransaction` RPC requires at least one output to be
+       ///    provided, in which case a zero-value empty OP_RETURN output can be used instead.
+       /// 3. Enough inputs must be selected/contributed for the resulting transaction (including the
+       ///    inputs and outputs noted above) to meet `target_feerate_sat_per_1000_weight`.
+       ///
+       /// Implementations must take note that [`Input::satisfaction_weight`] only tracks the weight of
+       /// the input's `script_sig` and `witness`. Some wallets, like Bitcoin Core's, may require
+       /// providing the full input weight. Failing to do so may lead to underestimating fee bumps and
+       /// delaying block inclusion.
+       ///
+       /// The `claim_id` must map to the set of external UTXOs assigned to the claim, such that they
+       /// can be re-used within new fee-bumped iterations of the original claiming transaction,
+       /// ensuring that claims don't double spend each other. If a specific `claim_id` has never had a
+       /// transaction associated with it, and all of the available UTXOs have already been assigned to
+       /// other claims, implementations must be willing to double spend their UTXOs. The choice of
+       /// which UTXOs to double spend is left to the implementation, but it must strive to keep the
+       /// set of other claims being double spent to a minimum.
+       fn select_confirmed_utxos(
+               &self, claim_id: ClaimId, must_spend: &[Input], must_pay_to: &[TxOut],
+               target_feerate_sat_per_1000_weight: u32,
+       ) -> Result<CoinSelection, ()>;
+       /// Signs and provides the full witness for all inputs within the transaction known to the
+       /// trait (i.e., any provided via [`CoinSelectionSource::select_confirmed_utxos`]).
+       fn sign_tx(&self, tx: &mut Transaction) -> Result<(), ()>;
+}
+
+/// An alternative to [`CoinSelectionSource`] that can be implemented and used along [`Wallet`] to
+/// provide a default implementation to [`CoinSelectionSource`].
+pub trait WalletSource {
+       /// Returns all UTXOs, with at least 1 confirmation each, that are available to spend.
+       fn list_confirmed_utxos(&self) -> Result<Vec<Utxo>, ()>;
+       /// Returns a script to use for change above dust resulting from a successful coin selection
+       /// attempt.
+       fn get_change_script(&self) -> Result<Script, ()>;
+       /// Signs and provides the full [`TxIn::script_sig`] and [`TxIn::witness`] for all inputs within
+       /// the transaction known to the wallet (i.e., any provided via
+       /// [`WalletSource::list_confirmed_utxos`]).
+       fn sign_tx(&self, tx: &mut Transaction) -> Result<(), ()>;
+}
+
+/// A wrapper over [`WalletSource`] that implements [`CoinSelection`] by preferring UTXOs that would
+/// avoid conflicting double spends. If not enough UTXOs are available to do so, conflicting double
+/// spends may happen.
+pub struct Wallet<W: Deref> where W::Target: WalletSource {
+       source: W,
+       // TODO: Do we care about cleaning this up once the UTXOs have a confirmed spend? We can do so
+       // by checking whether any UTXOs that exist in the map are no longer returned in
+       // `list_confirmed_utxos`.
+       locked_utxos: Mutex<HashMap<OutPoint, ClaimId>>,
+}
+
+impl<W: Deref> Wallet<W> where W::Target: WalletSource {
+       /// Returns a new instance backed by the given [`WalletSource`] that serves as an implementation
+       /// of [`CoinSelectionSource`].
+       pub fn new(source: W) -> Self {
+               Self { source, locked_utxos: Mutex::new(HashMap::new()) }
+       }
+
+       /// Performs coin selection on the set of UTXOs obtained from
+       /// [`WalletSource::list_confirmed_utxos`]. Its algorithm can be described as "smallest
+       /// above-dust-after-spend first", with a slight twist: we may skip UTXOs that are above dust at
+       /// the target feerate after having spent them in a separate claim transaction if
+       /// `force_conflicting_utxo_spend` is unset to avoid producing conflicting transactions. If
+       /// `tolerate_high_network_feerates` is set, we'll attempt to spend UTXOs that contribute at
+       /// least 1 satoshi at the current feerate, otherwise, we'll only attempt to spend those which
+       /// contribute at least twice their fee.
+       fn select_confirmed_utxos_internal(
+               &self, utxos: &[Utxo], claim_id: ClaimId, force_conflicting_utxo_spend: bool,
+               tolerate_high_network_feerates: bool, target_feerate_sat_per_1000_weight: u32,
+               preexisting_tx_weight: u64, target_amount_sat: u64,
+       ) -> Result<CoinSelection, ()> {
+               let mut locked_utxos = self.locked_utxos.lock().unwrap();
+               let mut eligible_utxos = utxos.iter().filter_map(|utxo| {
+                       if let Some(utxo_claim_id) = locked_utxos.get(&utxo.outpoint) {
+                               if *utxo_claim_id != claim_id && !force_conflicting_utxo_spend {
+                                       return None;
+                               }
+                       }
+                       let fee_to_spend_utxo = fee_for_weight(
+                               target_feerate_sat_per_1000_weight, BASE_INPUT_WEIGHT as u64 + utxo.satisfaction_weight,
+                       );
+                       let should_spend = if tolerate_high_network_feerates {
+                               utxo.output.value > fee_to_spend_utxo
+                       } else {
+                               utxo.output.value >= fee_to_spend_utxo * 2
+                       };
+                       if should_spend {
+                               Some((utxo, fee_to_spend_utxo))
+                       } else {
+                               None
+                       }
+               }).collect::<Vec<_>>();
+               eligible_utxos.sort_unstable_by_key(|(utxo, _)| utxo.output.value);
+
+               let mut selected_amount = 0;
+               let mut total_fees = fee_for_weight(target_feerate_sat_per_1000_weight, preexisting_tx_weight);
+               let mut selected_utxos = Vec::new();
+               for (utxo, fee_to_spend_utxo) in eligible_utxos {
+                       if selected_amount >= target_amount_sat + total_fees {
+                               break;
+                       }
+                       selected_amount += utxo.output.value;
+                       total_fees += fee_to_spend_utxo;
+                       selected_utxos.push(utxo.clone());
+               }
+               if selected_amount < target_amount_sat + total_fees {
+                       return Err(());
+               }
+               for utxo in &selected_utxos {
+                       locked_utxos.insert(utxo.outpoint, claim_id);
+               }
+               core::mem::drop(locked_utxos);
+
+               let remaining_amount = selected_amount - target_amount_sat - total_fees;
+               let change_script = self.source.get_change_script()?;
+               let change_output_fee = fee_for_weight(
+                       target_feerate_sat_per_1000_weight,
+                       (8 /* value */ + change_script.consensus_encode(&mut sink()).unwrap() as u64) *
+                               WITNESS_SCALE_FACTOR as u64,
+               );
+               let change_output_amount = remaining_amount.saturating_sub(change_output_fee);
+               let change_output = if change_output_amount < change_script.dust_value().to_sat() {
+                       None
+               } else {
+                       Some(TxOut { script_pubkey: change_script, value: change_output_amount })
+               };
+
+               Ok(CoinSelection {
+                       confirmed_utxos: selected_utxos,
+                       change_output,
+               })
+       }
+}
+
+impl<W: Deref> CoinSelectionSource for Wallet<W> where W::Target: WalletSource {
+       fn select_confirmed_utxos(
+               &self, claim_id: ClaimId, must_spend: &[Input], must_pay_to: &[TxOut],
+               target_feerate_sat_per_1000_weight: u32,
+       ) -> Result<CoinSelection, ()> {
+               let utxos = self.source.list_confirmed_utxos()?;
+               // TODO: Use fee estimation utils when we upgrade to bitcoin v0.30.0.
+               const BASE_TX_SIZE: u64 = 4 /* version */ + 1 /* input count */ + 1 /* output count */ + 4 /* locktime */;
+               let total_output_size: u64 = must_pay_to.iter().map(|output|
+                       8 /* value */ + 1 /* script len */ + output.script_pubkey.len() as u64
+               ).sum();
+               let total_satisfaction_weight: u64 = must_spend.iter().map(|input| input.satisfaction_weight).sum();
+               let total_input_weight = (BASE_INPUT_WEIGHT * must_spend.len() as u64) + total_satisfaction_weight;
+
+               let preexisting_tx_weight = 2 /* segwit marker & flag */ + total_input_weight +
+                       ((BASE_TX_SIZE + total_output_size) * WITNESS_SCALE_FACTOR as u64);
+               let target_amount_sat = must_pay_to.iter().map(|output| output.value).sum();
+               let do_coin_selection = |force_conflicting_utxo_spend: bool, tolerate_high_network_feerates: bool| {
+                       self.select_confirmed_utxos_internal(
+                               &utxos, claim_id, force_conflicting_utxo_spend, tolerate_high_network_feerates,
+                               target_feerate_sat_per_1000_weight, preexisting_tx_weight, target_amount_sat,
+                       )
+               };
+               do_coin_selection(false, false)
+                       .or_else(|_| do_coin_selection(false, true))
+                       .or_else(|_| do_coin_selection(true, false))
+                       .or_else(|_| do_coin_selection(true, true))
+       }
+
+       fn sign_tx(&self, tx: &mut Transaction) -> Result<(), ()> {
+               self.source.sign_tx(tx)
+       }
+}
+
+/// A handler for [`Event::BumpTransaction`] events that sources confirmed UTXOs from a
+/// [`CoinSelectionSource`] to fee bump transactions via Child-Pays-For-Parent (CPFP) or
+/// Replace-By-Fee (RBF).
+pub struct BumpTransactionEventHandler<B: Deref, C: Deref, SP: Deref, L: Deref>
+where
+       B::Target: BroadcasterInterface,
+       C::Target: CoinSelectionSource,
+       SP::Target: SignerProvider,
+       L::Target: Logger,
+{
+       broadcaster: B,
+       utxo_source: C,
+       signer_provider: SP,
+       logger: L,
+       secp: Secp256k1<secp256k1::All>,
+}
+
+impl<B: Deref, C: Deref, SP: Deref, L: Deref> BumpTransactionEventHandler<B, C, SP, L>
+where
+       B::Target: BroadcasterInterface,
+       C::Target: CoinSelectionSource,
+       SP::Target: SignerProvider,
+       L::Target: Logger,
+{
+       /// Returns a new instance capable of handling [`Event::BumpTransaction`] events.
+       pub fn new(broadcaster: B, utxo_source: C, signer_provider: SP, logger: L) -> Self {
+               Self {
+                       broadcaster,
+                       utxo_source,
+                       signer_provider,
+                       logger,
+                       secp: Secp256k1::new(),
+               }
+       }
+
+       /// Updates a transaction with the result of a successful coin selection attempt.
+       fn process_coin_selection(&self, tx: &mut Transaction, mut coin_selection: CoinSelection) {
+               for utxo in coin_selection.confirmed_utxos.drain(..) {
+                       tx.input.push(TxIn {
+                               previous_output: utxo.outpoint,
+                               script_sig: Script::new(),
+                               sequence: Sequence::ZERO,
+                               witness: Witness::new(),
+                       });
+               }
+               if let Some(change_output) = coin_selection.change_output.take() {
+                       tx.output.push(change_output);
+               } else if tx.output.is_empty() {
+                       // We weren't provided a change output, likely because the input set was a perfect
+                       // match, but we still need to have at least one output in the transaction for it to be
+                       // considered standard. We choose to go with an empty OP_RETURN as it is the cheapest
+                       // way to include a dummy output.
+                       tx.output.push(TxOut {
+                               value: 0,
+                               script_pubkey: Script::new_op_return(&[]),
+                       });
+               }
+       }
+
+       /// Returns an unsigned transaction spending an anchor output of the commitment transaction, and
+       /// any additional UTXOs sourced, to bump the commitment transaction's fee.
+       fn build_anchor_tx(
+               &self, claim_id: ClaimId, target_feerate_sat_per_1000_weight: u32,
+               commitment_tx: &Transaction, anchor_descriptor: &AnchorDescriptor,
+       ) -> Result<Transaction, ()> {
+               let must_spend = vec![Input {
+                       outpoint: anchor_descriptor.outpoint,
+                       satisfaction_weight: commitment_tx.weight() as u64 + ANCHOR_INPUT_WITNESS_WEIGHT + EMPTY_SCRIPT_SIG_WEIGHT,
+               }];
+               let coin_selection = self.utxo_source.select_confirmed_utxos(
+                       claim_id, &must_spend, &[], target_feerate_sat_per_1000_weight,
+               )?;
+
+               let mut tx = Transaction {
+                       version: 2,
+                       lock_time: PackedLockTime::ZERO, // TODO: Use next best height.
+                       input: vec![TxIn {
+                               previous_output: anchor_descriptor.outpoint,
+                               script_sig: Script::new(),
+                               sequence: Sequence::ZERO,
+                               witness: Witness::new(),
+                       }],
+                       output: vec![],
+               };
+               self.process_coin_selection(&mut tx, coin_selection);
+               Ok(tx)
+       }
+
+       /// Handles a [`BumpTransactionEvent::ChannelClose`] event variant by producing a fully-signed
+       /// transaction spending an anchor output of the commitment transaction to bump its fee and
+       /// broadcasts them to the network as a package.
+       fn handle_channel_close(
+               &self, claim_id: ClaimId, package_target_feerate_sat_per_1000_weight: u32,
+               commitment_tx: &Transaction, commitment_tx_fee_sat: u64, anchor_descriptor: &AnchorDescriptor,
+       ) -> Result<(), ()> {
+               // Compute the feerate the anchor transaction must meet to meet the overall feerate for the
+               // package (commitment + anchor transactions).
+               let commitment_tx_sat_per_1000_weight: u32 = compute_feerate_sat_per_1000_weight(
+                       commitment_tx_fee_sat, commitment_tx.weight() as u64,
+               );
+               if commitment_tx_sat_per_1000_weight >= package_target_feerate_sat_per_1000_weight {
+                       // If the commitment transaction already has a feerate high enough on its own, broadcast
+                       // it as is without a child.
+                       self.broadcaster.broadcast_transactions(&[&commitment_tx]);
+                       return Ok(());
+               }
+
+               let mut anchor_tx = self.build_anchor_tx(
+                       claim_id, package_target_feerate_sat_per_1000_weight, commitment_tx, anchor_descriptor,
+               )?;
+               debug_assert_eq!(anchor_tx.output.len(), 1);
+
+               self.utxo_source.sign_tx(&mut anchor_tx)?;
+               let signer = self.signer_provider.derive_channel_signer(
+                       anchor_descriptor.channel_value_satoshis, anchor_descriptor.channel_keys_id,
+               );
+               let anchor_sig = signer.sign_holder_anchor_input(&anchor_tx, 0, &self.secp)?;
+               anchor_tx.input[0].witness =
+                       chan_utils::build_anchor_input_witness(&signer.pubkeys().funding_pubkey, &anchor_sig);
+
+               self.broadcaster.broadcast_transactions(&[&commitment_tx, &anchor_tx]);
+               Ok(())
+       }
+
+       /// Returns an unsigned, fee-bumped HTLC transaction, along with the set of signers required to
+       /// fulfill the witness for each HTLC input within it.
+       fn build_htlc_tx(
+               &self, claim_id: ClaimId, target_feerate_sat_per_1000_weight: u32,
+               htlc_descriptors: &[HTLCDescriptor], tx_lock_time: PackedLockTime,
+       ) -> Result<(Transaction, HashMap<[u8; 32], <SP::Target as SignerProvider>::Signer>), ()> {
+               let mut tx = Transaction {
+                       version: 2,
+                       lock_time: tx_lock_time,
+                       input: vec![],
+                       output: vec![],
+               };
+               // Unfortunately, we need to derive the signer for each HTLC ahead of time to obtain its
+               // input.
+               let mut signers = HashMap::new();
+               let mut must_spend = Vec::with_capacity(htlc_descriptors.len());
+               for htlc_descriptor in htlc_descriptors {
+                       let signer = signers.entry(htlc_descriptor.channel_keys_id)
+                               .or_insert_with(||
+                                       self.signer_provider.derive_channel_signer(
+                                               htlc_descriptor.channel_value_satoshis, htlc_descriptor.channel_keys_id,
+                                       )
+                               );
+                       let per_commitment_point = signer.get_per_commitment_point(
+                               htlc_descriptor.per_commitment_number, &self.secp
+                       );
+
+                       let htlc_input = htlc_descriptor.unsigned_tx_input();
+                       must_spend.push(Input {
+                               outpoint: htlc_input.previous_output.clone(),
+                               satisfaction_weight: EMPTY_SCRIPT_SIG_WEIGHT + if htlc_descriptor.preimage.is_some() {
+                                       HTLC_SUCCESS_INPUT_ANCHOR_WITNESS_WEIGHT
+                               } else {
+                                       HTLC_TIMEOUT_INPUT_ANCHOR_WITNESS_WEIGHT
+                               },
+                       });
+                       tx.input.push(htlc_input);
+                       let htlc_output = htlc_descriptor.tx_output(&per_commitment_point, &self.secp);
+                       tx.output.push(htlc_output);
+               }
+
+               let coin_selection = self.utxo_source.select_confirmed_utxos(
+                       claim_id, &must_spend, &tx.output, target_feerate_sat_per_1000_weight,
+               )?;
+               self.process_coin_selection(&mut tx, coin_selection);
+               Ok((tx, signers))
+       }
+
+       /// Handles a [`BumpTransactionEvent::HTLCResolution`] event variant by producing a
+       /// fully-signed, fee-bumped HTLC transaction that is broadcast to the network.
+       fn handle_htlc_resolution(
+               &self, claim_id: ClaimId, target_feerate_sat_per_1000_weight: u32,
+               htlc_descriptors: &[HTLCDescriptor], tx_lock_time: PackedLockTime,
+       ) -> Result<(), ()> {
+               let (mut htlc_tx, signers) = self.build_htlc_tx(
+                       claim_id, target_feerate_sat_per_1000_weight, htlc_descriptors, tx_lock_time,
+               )?;
+
+               self.utxo_source.sign_tx(&mut htlc_tx)?;
+               for (idx, htlc_descriptor) in htlc_descriptors.iter().enumerate() {
+                       let signer = signers.get(&htlc_descriptor.channel_keys_id).unwrap();
+                       let htlc_sig = signer.sign_holder_htlc_transaction(
+                               &htlc_tx, idx, htlc_descriptor, &self.secp
+                       )?;
+                       let per_commitment_point = signer.get_per_commitment_point(
+                               htlc_descriptor.per_commitment_number, &self.secp
+                       );
+                       let witness_script = htlc_descriptor.witness_script(&per_commitment_point, &self.secp);
+                       htlc_tx.input[idx].witness = htlc_descriptor.tx_input_witness(&htlc_sig, &witness_script);
+               }
+
+               self.broadcaster.broadcast_transactions(&[&htlc_tx]);
+               Ok(())
+       }
+
+       /// Handles all variants of [`BumpTransactionEvent`], immediately returning otherwise.
+       pub fn handle_event(&self, event: &Event) {
+               let event = if let Event::BumpTransaction(event) = event {
+                       event
+               } else {
+                       return;
+               };
+               match event {
+                       BumpTransactionEvent::ChannelClose {
+                               claim_id, package_target_feerate_sat_per_1000_weight, commitment_tx,
+                               anchor_descriptor, commitment_tx_fee_satoshis,  ..
+                       } => {
+                               if let Err(_) = self.handle_channel_close(
+                                       *claim_id, *package_target_feerate_sat_per_1000_weight, commitment_tx,
+                                       *commitment_tx_fee_satoshis, anchor_descriptor,
+                               ) {
+                                       log_error!(self.logger, "Failed bumping commitment transaction fee for {}",
+                                               commitment_tx.txid());
+                               }
+                       }
+                       BumpTransactionEvent::HTLCResolution {
+                               claim_id, target_feerate_sat_per_1000_weight, htlc_descriptors, tx_lock_time,
+                       } => {
+                               if let Err(_) = self.handle_htlc_resolution(
+                                       *claim_id, *target_feerate_sat_per_1000_weight, htlc_descriptors, *tx_lock_time,
+                               ) {
+                                       log_error!(self.logger, "Failed bumping HTLC transaction fee for commitment {}",
+                                               htlc_descriptors[0].commitment_txid);
+                               }
+                       }
+               }
+       }
+}
index 76a7f884ad27ceb3a1550e1b7235bf7dc8795f6d..a6601e4d99c21653ee991614d6662fefba92176b 100644 (file)
 //! future, as well as generate and broadcast funding transactions handle payment preimages and a
 //! few other things.
 
-#[cfg(anchors)]
 pub mod bump_transaction;
 
-#[cfg(anchors)]
 pub use bump_transaction::BumpTransactionEvent;
 
 use crate::sign::SpendableOutputDescriptor;
@@ -33,8 +31,6 @@ use crate::util::string::UntrustedString;
 use crate::routing::router::{BlindedTail, Path, RouteHop, RouteParameters};
 
 use bitcoin::{PackedLockTime, Transaction, OutPoint};
-#[cfg(anchors)]
-use bitcoin::{Txid, TxIn, TxOut, Witness};
 use bitcoin::blockdata::script::Script;
 use bitcoin::hashes::Hash;
 use bitcoin::hashes::sha256::Hash as Sha256;
@@ -387,8 +383,25 @@ pub enum Event {
                ///
                /// Payments received on LDK versions prior to 0.0.115 will have this field unset.
                onion_fields: Option<RecipientOnionFields>,
-               /// The value, in thousandths of a satoshi, that this payment is for.
+               /// The value, in thousandths of a satoshi, that this payment is claimable for. May be greater
+               /// than the invoice amount.
+               ///
+               /// May be less than the invoice amount if [`ChannelConfig::accept_underpaying_htlcs`] is set
+               /// and the previous hop took an extra fee.
+               ///
+               /// # Note
+               /// If [`ChannelConfig::accept_underpaying_htlcs`] is set and you claim without verifying this
+               /// field, you may lose money!
+               ///
+               /// [`ChannelConfig::accept_underpaying_htlcs`]: crate::util::config::ChannelConfig::accept_underpaying_htlcs
                amount_msat: u64,
+               /// The value, in thousands of a satoshi, that was skimmed off of this payment as an extra fee
+               /// taken by our channel counterparty.
+               ///
+               /// Will always be 0 unless [`ChannelConfig::accept_underpaying_htlcs`] is set.
+               ///
+               /// [`ChannelConfig::accept_underpaying_htlcs`]: crate::util::config::ChannelConfig::accept_underpaying_htlcs
+               counterparty_skimmed_fee_msat: u64,
                /// Information for claiming this received payment, based on whether the purpose of the
                /// payment is to pay an invoice or to send a spontaneous payment.
                purpose: PaymentPurpose,
@@ -430,7 +443,8 @@ pub enum Event {
                /// The payment hash of the claimed payment. Note that LDK will not stop you from
                /// registering duplicate payment hashes for inbound payments.
                payment_hash: PaymentHash,
-               /// The value, in thousandths of a satoshi, that this payment is for.
+               /// The value, in thousandths of a satoshi, that this payment is for. May be greater than the
+               /// invoice amount.
                amount_msat: u64,
                /// The purpose of the claimed payment, i.e. whether the payment was for an invoice or a
                /// spontaneous payment.
@@ -623,6 +637,7 @@ pub enum Event {
                inbound_amount_msat: u64,
                /// How many msats the payer intended to route to the next node. Depending on the reason you are
                /// intercepting this payment, you might take a fee by forwarding less than this amount.
+               /// Forwarding less than this amount may break compatibility with LDK versions prior to 0.0.116.
                ///
                /// Note that LDK will NOT check that expected fees were factored into this value. You MUST
                /// check that whatever fee you want has been included here or subtract it as required. Further,
@@ -815,12 +830,14 @@ pub enum Event {
                /// Destination of the HTLC that failed to be processed.
                failed_next_destination: HTLCDestination,
        },
-       #[cfg(anchors)]
        /// Indicates that a transaction originating from LDK needs to have its fee bumped. This event
        /// requires confirmed external funds to be readily available to spend.
        ///
-       /// LDK does not currently generate this event. It is limited to the scope of channels with
-       /// anchor outputs, which will be introduced in a future release.
+       /// LDK does not currently generate this event unless the
+       /// [`ChannelHandshakeConfig::negotiate_anchors_zero_fee_htlc_tx`] config flag is set to true.
+       /// It is limited to the scope of channels with anchor outputs.
+       ///
+       /// [`ChannelHandshakeConfig::negotiate_anchors_zero_fee_htlc_tx`]: crate::util::config::ChannelHandshakeConfig::negotiate_anchors_zero_fee_htlc_tx
        BumpTransaction(BumpTransactionEvent),
 }
 
@@ -832,8 +849,8 @@ impl Writeable for Event {
                                // We never write out FundingGenerationReady events as, upon disconnection, peers
                                // drop any channels which have not yet exchanged funding_signed.
                        },
-                       &Event::PaymentClaimable { ref payment_hash, ref amount_msat, ref purpose,
-                               ref receiver_node_id, ref via_channel_id, ref via_user_channel_id,
+                       &Event::PaymentClaimable { ref payment_hash, ref amount_msat, counterparty_skimmed_fee_msat,
+                               ref purpose, ref receiver_node_id, ref via_channel_id, ref via_user_channel_id,
                                ref claim_deadline, ref onion_fields
                        } => {
                                1u8.write(writer)?;
@@ -848,6 +865,8 @@ impl Writeable for Event {
                                                payment_preimage = Some(*preimage);
                                        }
                                }
+                               let skimmed_fee_opt = if counterparty_skimmed_fee_msat == 0 { None }
+                                       else { Some(counterparty_skimmed_fee_msat) };
                                write_tlv_fields!(writer, {
                                        (0, payment_hash, required),
                                        (1, receiver_node_id, option),
@@ -855,10 +874,11 @@ impl Writeable for Event {
                                        (3, via_channel_id, option),
                                        (4, amount_msat, required),
                                        (5, via_user_channel_id, option),
-                                       (6, 0u64, required), // user_payment_id required for compatibility with 0.0.103 and earlier
+                                       // Type 6 was `user_payment_id` on 0.0.103 and earlier
                                        (7, claim_deadline, option),
                                        (8, payment_preimage, option),
                                        (9, onion_fields, option),
+                                       (10, skimmed_fee_opt, option),
                                });
                        },
                        &Event::PaymentSent { ref payment_id, ref payment_preimage, ref payment_hash, ref fee_paid_msat } => {
@@ -1009,7 +1029,6 @@ impl Writeable for Event {
                                        (2, failed_next_destination, required),
                                })
                        },
-                       #[cfg(anchors)]
                        &Event::BumpTransaction(ref event)=> {
                                27u8.write(writer)?;
                                match event {
@@ -1058,8 +1077,9 @@ impl MaybeReadable for Event {
                                        let mut payment_preimage = None;
                                        let mut payment_secret = None;
                                        let mut amount_msat = 0;
+                                       let mut counterparty_skimmed_fee_msat_opt = None;
                                        let mut receiver_node_id = None;
-                                       let mut _user_payment_id = None::<u64>; // For compatibility with 0.0.103 and earlier
+                                       let mut _user_payment_id = None::<u64>; // Used in 0.0.103 and earlier, no longer written in 0.0.116+.
                                        let mut via_channel_id = None;
                                        let mut claim_deadline = None;
                                        let mut via_user_channel_id = None;
@@ -1075,6 +1095,7 @@ impl MaybeReadable for Event {
                                                (7, claim_deadline, option),
                                                (8, payment_preimage, option),
                                                (9, onion_fields, option),
+                                               (10, counterparty_skimmed_fee_msat_opt, option),
                                        });
                                        let purpose = match payment_secret {
                                                Some(secret) => PaymentPurpose::InvoicePayment {
@@ -1088,6 +1109,7 @@ impl MaybeReadable for Event {
                                                receiver_node_id,
                                                payment_hash,
                                                amount_msat,
+                                               counterparty_skimmed_fee_msat: counterparty_skimmed_fee_msat_opt.unwrap_or(0),
                                                purpose,
                                                via_channel_id,
                                                via_user_channel_id,
index e7e7e0ede6bb137a802cfdd016bf86c0fa4df5f2..cf0a04aab081f458b586e1c9f87c0ab7cc305e55 100644 (file)
@@ -38,7 +38,7 @@
 //!     * `max_level_trace`
 
 #![cfg_attr(not(any(test, fuzzing, feature = "_test_utils")), deny(missing_docs))]
-#![cfg_attr(not(any(test, fuzzing, feature = "_test_utils")), forbid(unsafe_code))]
+#![cfg_attr(not(any(test, feature = "_test_utils")), forbid(unsafe_code))]
 
 // Prefix these with `rustdoc::` when we update our MSRV to be >= 1.52 to remove warnings.
 #![deny(broken_intra_doc_links)]
@@ -54,9 +54,6 @@
 
 #![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
 
-#![cfg_attr(all(any(test, feature = "_test_utils"), feature = "_bench_unstable"), feature(test))]
-#[cfg(all(any(test, feature = "_test_utils"), feature = "_bench_unstable"))] extern crate test;
-
 #[cfg(not(any(feature = "std", feature = "no-std")))]
 compile_error!("at least one of the `std` or `no-std` features must be enabled");
 
@@ -70,10 +67,12 @@ extern crate bitcoin;
 extern crate core;
 
 #[cfg(any(test, feature = "_test_utils"))] extern crate hex;
-#[cfg(any(test, fuzzing, feature = "_test_utils"))] extern crate regex;
+#[cfg(any(test, feature = "_test_utils"))] extern crate regex;
 
 #[cfg(not(feature = "std"))] extern crate core2;
 
+#[cfg(ldk_bench)] extern crate criterion;
+
 #[macro_use]
 pub mod util;
 pub mod chain;
@@ -177,7 +176,7 @@ mod prelude {
        pub use alloc::string::ToString;
 }
 
-#[cfg(all(not(feature = "_bench_unstable"), feature = "backtrace", feature = "std", test))]
+#[cfg(all(not(ldk_bench), feature = "backtrace", feature = "std", test))]
 extern crate backtrace;
 
 mod sync;
index b3b87146792af6bed47fcbf236192e7cb8aa7982..fd27d2308664469034564691395c263adb1808bb 100644 (file)
@@ -24,7 +24,7 @@ use bitcoin::hash_types::{Txid, PubkeyHash};
 use crate::sign::EntropySource;
 use crate::ln::{PaymentHash, PaymentPreimage};
 use crate::ln::msgs::DecodeError;
-use crate::util::ser::{Readable, Writeable, Writer};
+use crate::util::ser::{Readable, RequiredWrapper, Writeable, Writer};
 use crate::util::transaction_utils;
 
 use bitcoin::secp256k1::{SecretKey, PublicKey, Scalar};
@@ -40,6 +40,7 @@ use crate::util::transaction_utils::sort_outputs;
 use crate::ln::channel::{INITIAL_COMMITMENT_NUMBER, ANCHOR_OUTPUT_VALUE_SATOSHI};
 use core::ops::Deref;
 use crate::chain;
+use crate::ln::features::ChannelTypeFeatures;
 use crate::util::crypto::{sign, sign_with_aux_rand};
 
 /// Maximum number of one-way in-flight HTLC (protocol-level value).
@@ -57,20 +58,29 @@ pub(crate) const MIN_ACCEPTED_HTLC_SCRIPT_WEIGHT: usize = 136;
 /// This is the maximum post-anchor value.
 pub const MAX_ACCEPTED_HTLC_SCRIPT_WEIGHT: usize = 143;
 
+/// The upper bound weight of an anchor input.
+pub const ANCHOR_INPUT_WITNESS_WEIGHT: u64 = 116;
+/// The upper bound weight of an HTLC timeout input from a commitment transaction with anchor
+/// outputs.
+pub const HTLC_TIMEOUT_INPUT_ANCHOR_WITNESS_WEIGHT: u64 = 288;
+/// The upper bound weight of an HTLC success input from a commitment transaction with anchor
+/// outputs.
+pub const HTLC_SUCCESS_INPUT_ANCHOR_WITNESS_WEIGHT: u64 = 327;
+
 /// Gets the weight for an HTLC-Success transaction.
 #[inline]
-pub fn htlc_success_tx_weight(opt_anchors: bool) -> u64 {
+pub fn htlc_success_tx_weight(channel_type_features: &ChannelTypeFeatures) -> u64 {
        const HTLC_SUCCESS_TX_WEIGHT: u64 = 703;
        const HTLC_SUCCESS_ANCHOR_TX_WEIGHT: u64 = 706;
-       if opt_anchors { HTLC_SUCCESS_ANCHOR_TX_WEIGHT } else { HTLC_SUCCESS_TX_WEIGHT }
+       if channel_type_features.supports_anchors_zero_fee_htlc_tx() { HTLC_SUCCESS_ANCHOR_TX_WEIGHT } else { HTLC_SUCCESS_TX_WEIGHT }
 }
 
 /// Gets the weight for an HTLC-Timeout transaction.
 #[inline]
-pub fn htlc_timeout_tx_weight(opt_anchors: bool) -> u64 {
+pub fn htlc_timeout_tx_weight(channel_type_features: &ChannelTypeFeatures) -> u64 {
        const HTLC_TIMEOUT_TX_WEIGHT: u64 = 663;
        const HTLC_TIMEOUT_ANCHOR_TX_WEIGHT: u64 = 666;
-       if opt_anchors { HTLC_TIMEOUT_ANCHOR_TX_WEIGHT } else { HTLC_TIMEOUT_TX_WEIGHT }
+       if channel_type_features.supports_anchors_zero_fee_htlc_tx() { HTLC_TIMEOUT_ANCHOR_TX_WEIGHT } else { HTLC_TIMEOUT_TX_WEIGHT }
 }
 
 /// Describes the type of HTLC claim as determined by analyzing the witness.
@@ -574,7 +584,7 @@ impl_writeable_tlv_based!(HTLCOutputInCommitment, {
 });
 
 #[inline]
-pub(crate) fn get_htlc_redeemscript_with_explicit_keys(htlc: &HTLCOutputInCommitment, opt_anchors: bool, broadcaster_htlc_key: &PublicKey, countersignatory_htlc_key: &PublicKey, revocation_key: &PublicKey) -> Script {
+pub(crate) fn get_htlc_redeemscript_with_explicit_keys(htlc: &HTLCOutputInCommitment, channel_type_features: &ChannelTypeFeatures, broadcaster_htlc_key: &PublicKey, countersignatory_htlc_key: &PublicKey, revocation_key: &PublicKey) -> Script {
        let payment_hash160 = Ripemd160::hash(&htlc.payment_hash.0[..]).into_inner();
        if htlc.offered {
                let mut bldr = Builder::new().push_opcode(opcodes::all::OP_DUP)
@@ -602,7 +612,7 @@ pub(crate) fn get_htlc_redeemscript_with_explicit_keys(htlc: &HTLCOutputInCommit
                              .push_opcode(opcodes::all::OP_EQUALVERIFY)
                              .push_opcode(opcodes::all::OP_CHECKSIG)
                              .push_opcode(opcodes::all::OP_ENDIF);
-               if opt_anchors {
+               if channel_type_features.supports_anchors_zero_fee_htlc_tx() {
                        bldr = bldr.push_opcode(opcodes::all::OP_PUSHNUM_1)
                                .push_opcode(opcodes::all::OP_CSV)
                                .push_opcode(opcodes::all::OP_DROP);
@@ -638,7 +648,7 @@ pub(crate) fn get_htlc_redeemscript_with_explicit_keys(htlc: &HTLCOutputInCommit
                              .push_opcode(opcodes::all::OP_DROP)
                              .push_opcode(opcodes::all::OP_CHECKSIG)
                              .push_opcode(opcodes::all::OP_ENDIF);
-               if opt_anchors {
+               if channel_type_features.supports_anchors_zero_fee_htlc_tx() {
                        bldr = bldr.push_opcode(opcodes::all::OP_PUSHNUM_1)
                                .push_opcode(opcodes::all::OP_CSV)
                                .push_opcode(opcodes::all::OP_DROP);
@@ -651,8 +661,8 @@ pub(crate) fn get_htlc_redeemscript_with_explicit_keys(htlc: &HTLCOutputInCommit
 /// Gets the witness redeemscript for an HTLC output in a commitment transaction. Note that htlc
 /// does not need to have its previous_output_index filled.
 #[inline]
-pub fn get_htlc_redeemscript(htlc: &HTLCOutputInCommitment, opt_anchors: bool, keys: &TxCreationKeys) -> Script {
-       get_htlc_redeemscript_with_explicit_keys(htlc, opt_anchors, &keys.broadcaster_htlc_key, &keys.countersignatory_htlc_key, &keys.revocation_key)
+pub fn get_htlc_redeemscript(htlc: &HTLCOutputInCommitment, channel_type_features: &ChannelTypeFeatures, keys: &TxCreationKeys) -> Script {
+       get_htlc_redeemscript_with_explicit_keys(htlc, channel_type_features, &keys.broadcaster_htlc_key, &keys.countersignatory_htlc_key, &keys.revocation_key)
 }
 
 /// Gets the redeemscript for a funding output from the two funding public keys.
@@ -682,13 +692,13 @@ pub(crate) fn make_funding_redeemscript_from_slices(broadcaster_funding_key: &[u
 ///
 /// Panics if htlc.transaction_output_index.is_none() (as such HTLCs do not appear in the
 /// commitment transaction).
-pub fn build_htlc_transaction(commitment_txid: &Txid, feerate_per_kw: u32, contest_delay: u16, htlc: &HTLCOutputInCommitment, opt_anchors: bool, use_non_zero_fee_anchors: bool, broadcaster_delayed_payment_key: &PublicKey, revocation_key: &PublicKey) -> Transaction {
+pub fn build_htlc_transaction(commitment_txid: &Txid, feerate_per_kw: u32, contest_delay: u16, htlc: &HTLCOutputInCommitment, channel_type_features: &ChannelTypeFeatures, broadcaster_delayed_payment_key: &PublicKey, revocation_key: &PublicKey) -> Transaction {
        let mut txins: Vec<TxIn> = Vec::new();
-       txins.push(build_htlc_input(commitment_txid, htlc, opt_anchors));
+       txins.push(build_htlc_input(commitment_txid, htlc, channel_type_features));
 
        let mut txouts: Vec<TxOut> = Vec::new();
        txouts.push(build_htlc_output(
-               feerate_per_kw, contest_delay, htlc, opt_anchors, use_non_zero_fee_anchors,
+               feerate_per_kw, contest_delay, htlc, channel_type_features,
                broadcaster_delayed_payment_key, revocation_key
        ));
 
@@ -700,28 +710,27 @@ pub fn build_htlc_transaction(commitment_txid: &Txid, feerate_per_kw: u32, conte
        }
 }
 
-pub(crate) fn build_htlc_input(commitment_txid: &Txid, htlc: &HTLCOutputInCommitment, opt_anchors: bool) -> TxIn {
+pub(crate) fn build_htlc_input(commitment_txid: &Txid, htlc: &HTLCOutputInCommitment, channel_type_features: &ChannelTypeFeatures) -> TxIn {
        TxIn {
                previous_output: OutPoint {
                        txid: commitment_txid.clone(),
                        vout: htlc.transaction_output_index.expect("Can't build an HTLC transaction for a dust output"),
                },
                script_sig: Script::new(),
-               sequence: Sequence(if opt_anchors { 1 } else { 0 }),
+               sequence: Sequence(if channel_type_features.supports_anchors_zero_fee_htlc_tx() { 1 } else { 0 }),
                witness: Witness::new(),
        }
 }
 
 pub(crate) fn build_htlc_output(
-       feerate_per_kw: u32, contest_delay: u16, htlc: &HTLCOutputInCommitment, opt_anchors: bool,
-       use_non_zero_fee_anchors: bool, broadcaster_delayed_payment_key: &PublicKey, revocation_key: &PublicKey
+       feerate_per_kw: u32, contest_delay: u16, htlc: &HTLCOutputInCommitment, channel_type_features: &ChannelTypeFeatures, broadcaster_delayed_payment_key: &PublicKey, revocation_key: &PublicKey
 ) -> TxOut {
        let weight = if htlc.offered {
-               htlc_timeout_tx_weight(opt_anchors)
+               htlc_timeout_tx_weight(channel_type_features)
        } else {
-               htlc_success_tx_weight(opt_anchors)
+               htlc_success_tx_weight(channel_type_features)
        };
-       let output_value = if opt_anchors && !use_non_zero_fee_anchors {
+       let output_value = if channel_type_features.supports_anchors_zero_fee_htlc_tx() && !channel_type_features.supports_anchors_nonzero_fee_htlc_tx() {
                htlc.amount_msat / 1000
        } else {
                let total_fee = feerate_per_kw as u64 * weight / 1000;
@@ -737,9 +746,9 @@ pub(crate) fn build_htlc_output(
 /// Returns the witness required to satisfy and spend a HTLC input.
 pub fn build_htlc_input_witness(
        local_sig: &Signature, remote_sig: &Signature, preimage: &Option<PaymentPreimage>,
-       redeem_script: &Script, opt_anchors: bool,
+       redeem_script: &Script, channel_type_features: &ChannelTypeFeatures,
 ) -> Witness {
-       let remote_sighash_type = if opt_anchors {
+       let remote_sighash_type = if channel_type_features.supports_anchors_zero_fee_htlc_tx() {
                EcdsaSighashType::SinglePlusAnyoneCanPay
        } else {
                EcdsaSighashType::All
@@ -760,6 +769,37 @@ pub fn build_htlc_input_witness(
        witness
 }
 
+/// Pre-anchors channel type features did not use to get serialized in the following six structs:
+/// â€” [`ChannelTransactionParameters`]
+/// â€” [`CommitmentTransaction`]
+/// â€” [`CounterpartyOfferedHTLCOutput`]
+/// â€” [`CounterpartyReceivedHTLCOutput`]
+/// â€” [`HolderHTLCOutput`]
+/// â€” [`HolderFundingOutput`]
+///
+/// To ensure a forwards-compatible serialization, we use odd TLV fields. However, if new features
+/// are used that could break security, where old signers should be prevented from handling the
+/// serialized data, an optional even-field TLV will be used as a stand-in to break compatibility.
+///
+/// This method determines whether or not that option needs to be set based on the chanenl type
+/// features, and returns it.
+///
+/// [`CounterpartyOfferedHTLCOutput`]: crate::chain::package::CounterpartyOfferedHTLCOutput
+/// [`CounterpartyReceivedHTLCOutput`]: crate::chain::package::CounterpartyReceivedHTLCOutput
+/// [`HolderHTLCOutput`]: crate::chain::package::HolderHTLCOutput
+/// [`HolderFundingOutput`]: crate::chain::package::HolderFundingOutput
+pub(crate) fn legacy_deserialization_prevention_marker_for_channel_type_features(features: &ChannelTypeFeatures) -> Option<()> {
+       let mut legacy_version_bit_set = ChannelTypeFeatures::only_static_remote_key();
+       legacy_version_bit_set.set_scid_privacy_required();
+       legacy_version_bit_set.set_zero_conf_required();
+
+       if features.is_subset(&legacy_version_bit_set) {
+               None
+       } else {
+               Some(())
+       }
+}
+
 /// Gets the witnessScript for the to_remote output when anchors are enabled.
 #[inline]
 pub fn get_to_countersignatory_with_anchors_redeemscript(payment_point: &PublicKey) -> Script {
@@ -789,7 +829,6 @@ pub fn get_anchor_redeemscript(funding_pubkey: &PublicKey) -> Script {
                .into_script()
 }
 
-#[cfg(anchors)]
 /// Locates the output with an anchor script paying to `funding_pubkey` within `commitment_tx`.
 pub(crate) fn get_anchor_output<'a>(commitment_tx: &'a Transaction, funding_pubkey: &PublicKey) -> Option<(u32, &'a TxOut)> {
        let anchor_script = chan_utils::get_anchor_redeemscript(funding_pubkey).to_v0_p2wsh();
@@ -826,13 +865,9 @@ pub struct ChannelTransactionParameters {
        pub counterparty_parameters: Option<CounterpartyChannelTransactionParameters>,
        /// The late-bound funding outpoint
        pub funding_outpoint: Option<chain::transaction::OutPoint>,
-       /// Are anchors (zero fee HTLC transaction variant) used for this channel. Boolean is
-       /// serialization backwards-compatible.
-       pub opt_anchors: Option<()>,
-       /// Are non-zero-fee anchors are enabled (used in conjuction with opt_anchors)
-       /// It is intended merely for backwards compatibility with signers that need it.
-       /// There is no support for this feature in LDK channel negotiation.
-       pub opt_non_zero_fee_anchors: Option<()>,
+       /// This channel's type, as negotiated during channel open. For old objects where this field
+       /// wasn't serialized, it will default to static_remote_key at deserialization.
+       pub channel_type_features: ChannelTypeFeatures
 }
 
 /// Late-bound per-channel counterparty data used to build transactions.
@@ -880,15 +915,56 @@ impl_writeable_tlv_based!(CounterpartyChannelTransactionParameters, {
        (2, selected_contest_delay, required),
 });
 
-impl_writeable_tlv_based!(ChannelTransactionParameters, {
-       (0, holder_pubkeys, required),
-       (2, holder_selected_contest_delay, required),
-       (4, is_outbound_from_holder, required),
-       (6, counterparty_parameters, option),
-       (8, funding_outpoint, option),
-       (10, opt_anchors, option),
-       (12, opt_non_zero_fee_anchors, option),
-});
+impl Writeable for ChannelTransactionParameters {
+       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
+               let legacy_deserialization_prevention_marker = legacy_deserialization_prevention_marker_for_channel_type_features(&self.channel_type_features);
+               write_tlv_fields!(writer, {
+                       (0, self.holder_pubkeys, required),
+                       (2, self.holder_selected_contest_delay, required),
+                       (4, self.is_outbound_from_holder, required),
+                       (6, self.counterparty_parameters, option),
+                       (8, self.funding_outpoint, option),
+                       (10, legacy_deserialization_prevention_marker, option),
+                       (11, self.channel_type_features, required),
+               });
+               Ok(())
+       }
+}
+
+impl Readable for ChannelTransactionParameters {
+       fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
+               let mut holder_pubkeys = RequiredWrapper(None);
+               let mut holder_selected_contest_delay = RequiredWrapper(None);
+               let mut is_outbound_from_holder = RequiredWrapper(None);
+               let mut counterparty_parameters = None;
+               let mut funding_outpoint = None;
+               let mut _legacy_deserialization_prevention_marker: Option<()> = None;
+               let mut channel_type_features = None;
+
+               read_tlv_fields!(reader, {
+                       (0, holder_pubkeys, required),
+                       (2, holder_selected_contest_delay, required),
+                       (4, is_outbound_from_holder, required),
+                       (6, counterparty_parameters, option),
+                       (8, funding_outpoint, option),
+                       (10, _legacy_deserialization_prevention_marker, option),
+                       (11, channel_type_features, option),
+               });
+
+               let mut additional_features = ChannelTypeFeatures::empty();
+               additional_features.set_anchors_nonzero_fee_htlc_tx_required();
+               chain::package::verify_channel_type_features(&channel_type_features, Some(&additional_features))?;
+
+               Ok(Self {
+                       holder_pubkeys: holder_pubkeys.0.unwrap(),
+                       holder_selected_contest_delay: holder_selected_contest_delay.0.unwrap(),
+                       is_outbound_from_holder: is_outbound_from_holder.0.unwrap(),
+                       counterparty_parameters,
+                       funding_outpoint,
+                       channel_type_features: channel_type_features.unwrap_or(ChannelTypeFeatures::only_static_remote_key())
+               })
+       }
+}
 
 /// Static channel fields used to build transactions given per-commitment fields, organized by
 /// broadcaster/countersignatory.
@@ -942,8 +1018,8 @@ impl<'a> DirectedChannelTransactionParameters<'a> {
        }
 
        /// Whether to use anchors for this channel
-       pub fn opt_anchors(&self) -> bool {
-               self.inner.opt_anchors.is_some()
+       pub fn channel_type_features(&self) -> &ChannelTypeFeatures {
+               &self.inner.channel_type_features
        }
 }
 
@@ -1010,14 +1086,13 @@ impl HolderCommitmentTransaction {
                        is_outbound_from_holder: false,
                        counterparty_parameters: Some(CounterpartyChannelTransactionParameters { pubkeys: channel_pubkeys.clone(), selected_contest_delay: 0 }),
                        funding_outpoint: Some(chain::transaction::OutPoint { txid: Txid::all_zeros(), index: 0 }),
-                       opt_anchors: None,
-                       opt_non_zero_fee_anchors: None,
+                       channel_type_features: ChannelTypeFeatures::only_static_remote_key(),
                };
                let mut counterparty_htlc_sigs = Vec::new();
                for _ in 0..htlcs.len() {
                        counterparty_htlc_sigs.push(dummy_sig);
                }
-               let inner = CommitmentTransaction::new_with_auxiliary_htlc_data(0, 0, 0, false, dummy_key.clone(), dummy_key.clone(), keys, 0, htlcs, &channel_parameters.as_counterparty_broadcastable());
+               let inner = CommitmentTransaction::new_with_auxiliary_htlc_data(0, 0, 0, dummy_key.clone(), dummy_key.clone(), keys, 0, htlcs, &channel_parameters.as_counterparty_broadcastable());
                htlcs.sort_by_key(|htlc| htlc.0.transaction_output_index);
                HolderCommitmentTransaction {
                        inner,
@@ -1235,10 +1310,8 @@ pub struct CommitmentTransaction {
        to_countersignatory_value_sat: u64,
        feerate_per_kw: u32,
        htlcs: Vec<HTLCOutputInCommitment>,
-       // A boolean that is serialization backwards-compatible
-       opt_anchors: Option<()>,
-       // Whether non-zero-fee anchors should be used
-       opt_non_zero_fee_anchors: Option<()>,
+       // Note that on upgrades, some features of existing outputs may be missed.
+       channel_type_features: ChannelTypeFeatures,
        // A cache of the parties' pubkeys required to construct the transaction, see doc for trust()
        keys: TxCreationKeys,
        // For access to the pre-built transaction, see doc for trust()
@@ -1253,7 +1326,7 @@ impl PartialEq for CommitmentTransaction {
                        self.to_countersignatory_value_sat == o.to_countersignatory_value_sat &&
                        self.feerate_per_kw == o.feerate_per_kw &&
                        self.htlcs == o.htlcs &&
-                       self.opt_anchors == o.opt_anchors &&
+                       self.channel_type_features == o.channel_type_features &&
                        self.keys == o.keys;
                if eq {
                        debug_assert_eq!(self.built.transaction, o.built.transaction);
@@ -1263,17 +1336,64 @@ impl PartialEq for CommitmentTransaction {
        }
 }
 
-impl_writeable_tlv_based!(CommitmentTransaction, {
-       (0, commitment_number, required),
-       (2, to_broadcaster_value_sat, required),
-       (4, to_countersignatory_value_sat, required),
-       (6, feerate_per_kw, required),
-       (8, keys, required),
-       (10, built, required),
-       (12, htlcs, vec_type),
-       (14, opt_anchors, option),
-       (16, opt_non_zero_fee_anchors, option),
-});
+impl Writeable for CommitmentTransaction {
+       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
+               let legacy_deserialization_prevention_marker = legacy_deserialization_prevention_marker_for_channel_type_features(&self.channel_type_features);
+               write_tlv_fields!(writer, {
+                       (0, self.commitment_number, required),
+                       (2, self.to_broadcaster_value_sat, required),
+                       (4, self.to_countersignatory_value_sat, required),
+                       (6, self.feerate_per_kw, required),
+                       (8, self.keys, required),
+                       (10, self.built, required),
+                       (12, self.htlcs, vec_type),
+                       (14, legacy_deserialization_prevention_marker, option),
+                       (15, self.channel_type_features, required),
+               });
+               Ok(())
+       }
+}
+
+impl Readable for CommitmentTransaction {
+       fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
+               let mut commitment_number = RequiredWrapper(None);
+               let mut to_broadcaster_value_sat = RequiredWrapper(None);
+               let mut to_countersignatory_value_sat = RequiredWrapper(None);
+               let mut feerate_per_kw = RequiredWrapper(None);
+               let mut keys = RequiredWrapper(None);
+               let mut built = RequiredWrapper(None);
+               _init_tlv_field_var!(htlcs, vec_type);
+               let mut _legacy_deserialization_prevention_marker: Option<()> = None;
+               let mut channel_type_features = None;
+
+               read_tlv_fields!(reader, {
+                       (0, commitment_number, required),
+                       (2, to_broadcaster_value_sat, required),
+                       (4, to_countersignatory_value_sat, required),
+                       (6, feerate_per_kw, required),
+                       (8, keys, required),
+                       (10, built, required),
+                       (12, htlcs, vec_type),
+                       (14, _legacy_deserialization_prevention_marker, option),
+                       (15, channel_type_features, option),
+               });
+
+               let mut additional_features = ChannelTypeFeatures::empty();
+               additional_features.set_anchors_nonzero_fee_htlc_tx_required();
+               chain::package::verify_channel_type_features(&channel_type_features, Some(&additional_features))?;
+
+               Ok(Self {
+                       commitment_number: commitment_number.0.unwrap(),
+                       to_broadcaster_value_sat: to_broadcaster_value_sat.0.unwrap(),
+                       to_countersignatory_value_sat: to_countersignatory_value_sat.0.unwrap(),
+                       feerate_per_kw: feerate_per_kw.0.unwrap(),
+                       keys: keys.0.unwrap(),
+                       built: built.0.unwrap(),
+                       htlcs: _init_tlv_based_struct_field!(htlcs, vec_type),
+                       channel_type_features: channel_type_features.unwrap_or(ChannelTypeFeatures::only_static_remote_key())
+               })
+       }
+}
 
 impl CommitmentTransaction {
        /// Construct an object of the class while assigning transaction output indices to HTLCs.
@@ -1286,9 +1406,9 @@ impl CommitmentTransaction {
        /// Only include HTLCs that are above the dust limit for the channel.
        ///
        /// This is not exported to bindings users due to the generic though we likely should expose a version without
-       pub fn new_with_auxiliary_htlc_data<T>(commitment_number: u64, to_broadcaster_value_sat: u64, to_countersignatory_value_sat: u64, opt_anchors: bool, broadcaster_funding_key: PublicKey, countersignatory_funding_key: PublicKey, keys: TxCreationKeys, feerate_per_kw: u32, htlcs_with_aux: &mut Vec<(HTLCOutputInCommitment, T)>, channel_parameters: &DirectedChannelTransactionParameters) -> CommitmentTransaction {
+       pub fn new_with_auxiliary_htlc_data<T>(commitment_number: u64, to_broadcaster_value_sat: u64, to_countersignatory_value_sat: u64, broadcaster_funding_key: PublicKey, countersignatory_funding_key: PublicKey, keys: TxCreationKeys, feerate_per_kw: u32, htlcs_with_aux: &mut Vec<(HTLCOutputInCommitment, T)>, channel_parameters: &DirectedChannelTransactionParameters) -> CommitmentTransaction {
                // Sort outputs and populate output indices while keeping track of the auxiliary data
-               let (outputs, htlcs) = Self::internal_build_outputs(&keys, to_broadcaster_value_sat, to_countersignatory_value_sat, htlcs_with_aux, channel_parameters, opt_anchors, &broadcaster_funding_key, &countersignatory_funding_key).unwrap();
+               let (outputs, htlcs) = Self::internal_build_outputs(&keys, to_broadcaster_value_sat, to_countersignatory_value_sat, htlcs_with_aux, channel_parameters, &broadcaster_funding_key, &countersignatory_funding_key).unwrap();
 
                let (obscured_commitment_transaction_number, txins) = Self::internal_build_inputs(commitment_number, channel_parameters);
                let transaction = Self::make_transaction(obscured_commitment_transaction_number, txins, outputs);
@@ -1299,13 +1419,12 @@ impl CommitmentTransaction {
                        to_countersignatory_value_sat,
                        feerate_per_kw,
                        htlcs,
-                       opt_anchors: if opt_anchors { Some(()) } else { None },
+                       channel_type_features: channel_parameters.channel_type_features().clone(),
                        keys,
                        built: BuiltCommitmentTransaction {
                                transaction,
                                txid
                        },
-                       opt_non_zero_fee_anchors: None,
                }
        }
 
@@ -1313,7 +1432,7 @@ impl CommitmentTransaction {
        ///
        /// This is not exported to bindings users due to move, and also not likely to be useful for binding users
        pub fn with_non_zero_fee_anchors(mut self) -> Self {
-               self.opt_non_zero_fee_anchors = Some(());
+               self.channel_type_features.set_anchors_nonzero_fee_htlc_tx_required();
                self
        }
 
@@ -1321,7 +1440,7 @@ impl CommitmentTransaction {
                let (obscured_commitment_transaction_number, txins) = Self::internal_build_inputs(self.commitment_number, channel_parameters);
 
                let mut htlcs_with_aux = self.htlcs.iter().map(|h| (h.clone(), ())).collect();
-               let (outputs, _) = Self::internal_build_outputs(keys, self.to_broadcaster_value_sat, self.to_countersignatory_value_sat, &mut htlcs_with_aux, channel_parameters, self.opt_anchors.is_some(), broadcaster_funding_key, countersignatory_funding_key)?;
+               let (outputs, _) = Self::internal_build_outputs(keys, self.to_broadcaster_value_sat, self.to_countersignatory_value_sat, &mut htlcs_with_aux, channel_parameters, broadcaster_funding_key, countersignatory_funding_key)?;
 
                let transaction = Self::make_transaction(obscured_commitment_transaction_number, txins, outputs);
                let txid = transaction.txid();
@@ -1345,14 +1464,14 @@ impl CommitmentTransaction {
        // - initial sorting of outputs / HTLCs in the constructor, in which case T is auxiliary data the
        //   caller needs to have sorted together with the HTLCs so it can keep track of the output index
        // - building of a bitcoin transaction during a verify() call, in which case T is just ()
-       fn internal_build_outputs<T>(keys: &TxCreationKeys, to_broadcaster_value_sat: u64, to_countersignatory_value_sat: u64, htlcs_with_aux: &mut Vec<(HTLCOutputInCommitment, T)>, channel_parameters: &DirectedChannelTransactionParameters, opt_anchors: bool, broadcaster_funding_key: &PublicKey, countersignatory_funding_key: &PublicKey) -> Result<(Vec<TxOut>, Vec<HTLCOutputInCommitment>), ()> {
+       fn internal_build_outputs<T>(keys: &TxCreationKeys, to_broadcaster_value_sat: u64, to_countersignatory_value_sat: u64, htlcs_with_aux: &mut Vec<(HTLCOutputInCommitment, T)>, channel_parameters: &DirectedChannelTransactionParameters, broadcaster_funding_key: &PublicKey, countersignatory_funding_key: &PublicKey) -> Result<(Vec<TxOut>, Vec<HTLCOutputInCommitment>), ()> {
                let countersignatory_pubkeys = channel_parameters.countersignatory_pubkeys();
                let contest_delay = channel_parameters.contest_delay();
 
                let mut txouts: Vec<(TxOut, Option<&mut HTLCOutputInCommitment>)> = Vec::new();
 
                if to_countersignatory_value_sat > 0 {
-                       let script = if opt_anchors {
+                       let script = if channel_parameters.channel_type_features().supports_anchors_zero_fee_htlc_tx() {
                            get_to_countersignatory_with_anchors_redeemscript(&countersignatory_pubkeys.payment_point).to_v0_p2wsh()
                        } else {
                            Payload::p2wpkh(&BitcoinPublicKey::new(countersignatory_pubkeys.payment_point)).unwrap().script_pubkey()
@@ -1381,7 +1500,7 @@ impl CommitmentTransaction {
                        ));
                }
 
-               if opt_anchors {
+               if channel_parameters.channel_type_features().supports_anchors_zero_fee_htlc_tx() {
                        if to_broadcaster_value_sat > 0 || !htlcs_with_aux.is_empty() {
                                let anchor_script = get_anchor_redeemscript(broadcaster_funding_key);
                                txouts.push((
@@ -1407,7 +1526,7 @@ impl CommitmentTransaction {
 
                let mut htlcs = Vec::with_capacity(htlcs_with_aux.len());
                for (htlc, _) in htlcs_with_aux {
-                       let script = chan_utils::get_htlc_redeemscript(&htlc, opt_anchors, &keys);
+                       let script = chan_utils::get_htlc_redeemscript(&htlc, &channel_parameters.channel_type_features(), &keys);
                        let txout = TxOut {
                                script_pubkey: script.to_v0_p2wsh(),
                                value: htlc.amount_msat / 1000,
@@ -1562,8 +1681,8 @@ impl<'a> TrustedCommitmentTransaction<'a> {
        }
 
        /// Should anchors be used.
-       pub fn opt_anchors(&self) -> bool {
-               self.opt_anchors.is_some()
+       pub fn channel_type_features(&self) -> &ChannelTypeFeatures {
+               &self.inner.channel_type_features
        }
 
        /// Get a signature for each HTLC which was included in the commitment transaction (ie for
@@ -1584,9 +1703,9 @@ impl<'a> TrustedCommitmentTransaction<'a> {
 
                for this_htlc in inner.htlcs.iter() {
                        assert!(this_htlc.transaction_output_index.is_some());
-                       let htlc_tx = build_htlc_transaction(&txid, inner.feerate_per_kw, channel_parameters.contest_delay(), &this_htlc, self.opt_anchors(), self.opt_non_zero_fee_anchors.is_some(), &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
+                       let htlc_tx = build_htlc_transaction(&txid, inner.feerate_per_kw, channel_parameters.contest_delay(), &this_htlc, &self.channel_type_features, &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
 
-                       let htlc_redeemscript = get_htlc_redeemscript_with_explicit_keys(&this_htlc, self.opt_anchors(), &keys.broadcaster_htlc_key, &keys.countersignatory_htlc_key, &keys.revocation_key);
+                       let htlc_redeemscript = get_htlc_redeemscript_with_explicit_keys(&this_htlc, &self.channel_type_features, &keys.broadcaster_htlc_key, &keys.countersignatory_htlc_key, &keys.revocation_key);
 
                        let sighash = hash_to_message!(&sighash::SighashCache::new(&htlc_tx).segwit_signature_hash(0, &htlc_redeemscript, this_htlc.amount_msat / 1000, EcdsaSighashType::All).unwrap()[..]);
                        ret.push(sign_with_aux_rand(secp_ctx, &sighash, &holder_htlc_key, entropy_source));
@@ -1606,12 +1725,12 @@ impl<'a> TrustedCommitmentTransaction<'a> {
                // Further, we should never be provided the preimage for an HTLC-Timeout transaction.
                if  this_htlc.offered && preimage.is_some() { unreachable!(); }
 
-               let mut htlc_tx = build_htlc_transaction(&txid, inner.feerate_per_kw, channel_parameters.contest_delay(), &this_htlc, self.opt_anchors(), self.opt_non_zero_fee_anchors.is_some(), &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
+               let mut htlc_tx = build_htlc_transaction(&txid, inner.feerate_per_kw, channel_parameters.contest_delay(), &this_htlc, &self.channel_type_features, &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
 
-               let htlc_redeemscript = get_htlc_redeemscript_with_explicit_keys(&this_htlc, self.opt_anchors(), &keys.broadcaster_htlc_key, &keys.countersignatory_htlc_key, &keys.revocation_key);
+               let htlc_redeemscript = get_htlc_redeemscript_with_explicit_keys(&this_htlc, &self.channel_type_features, &keys.broadcaster_htlc_key, &keys.countersignatory_htlc_key, &keys.revocation_key);
 
                htlc_tx.input[0].witness = chan_utils::build_htlc_input_witness(
-                       signature, counterparty_signature, preimage, &htlc_redeemscript, self.opt_anchors(),
+                       signature, counterparty_signature, preimage, &htlc_redeemscript, &self.channel_type_features,
                );
                htlc_tx
        }
@@ -1662,6 +1781,7 @@ mod tests {
        use bitcoin::hashes::hex::ToHex;
        use bitcoin::util::address::Payload;
        use bitcoin::PublicKey as BitcoinPublicKey;
+       use crate::ln::features::ChannelTypeFeatures;
 
        #[test]
        fn test_anchors() {
@@ -1685,8 +1805,7 @@ mod tests {
                        is_outbound_from_holder: false,
                        counterparty_parameters: Some(CounterpartyChannelTransactionParameters { pubkeys: counterparty_pubkeys.clone(), selected_contest_delay: 0 }),
                        funding_outpoint: Some(chain::transaction::OutPoint { txid: Txid::all_zeros(), index: 0 }),
-                       opt_anchors: None,
-                       opt_non_zero_fee_anchors: None,
+                       channel_type_features: ChannelTypeFeatures::only_static_remote_key(),
                };
 
                let mut htlcs_with_aux: Vec<(_, ())> = Vec::new();
@@ -1694,7 +1813,6 @@ mod tests {
                // Generate broadcaster and counterparty outputs
                let tx = CommitmentTransaction::new_with_auxiliary_htlc_data(
                        0, 1000, 2000,
-                       false,
                        holder_pubkeys.funding_pubkey,
                        counterparty_pubkeys.funding_pubkey,
                        keys.clone(), 1,
@@ -1704,9 +1822,9 @@ mod tests {
                assert_eq!(tx.built.transaction.output[1].script_pubkey, Payload::p2wpkh(&BitcoinPublicKey::new(counterparty_pubkeys.payment_point)).unwrap().script_pubkey());
 
                // Generate broadcaster and counterparty outputs as well as two anchors
+               channel_parameters.channel_type_features = ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies();
                let tx = CommitmentTransaction::new_with_auxiliary_htlc_data(
                        0, 1000, 2000,
-                       true,
                        holder_pubkeys.funding_pubkey,
                        counterparty_pubkeys.funding_pubkey,
                        keys.clone(), 1,
@@ -1718,7 +1836,6 @@ mod tests {
                // Generate broadcaster output and anchor
                let tx = CommitmentTransaction::new_with_auxiliary_htlc_data(
                        0, 3000, 0,
-                       true,
                        holder_pubkeys.funding_pubkey,
                        counterparty_pubkeys.funding_pubkey,
                        keys.clone(), 1,
@@ -1729,7 +1846,6 @@ mod tests {
                // Generate counterparty output and anchor
                let tx = CommitmentTransaction::new_with_auxiliary_htlc_data(
                        0, 0, 3000,
-                       true,
                        holder_pubkeys.funding_pubkey,
                        counterparty_pubkeys.funding_pubkey,
                        keys.clone(), 1,
@@ -1754,9 +1870,9 @@ mod tests {
                };
 
                // Generate broadcaster output and received and offered HTLC outputs,  w/o anchors
+               channel_parameters.channel_type_features = ChannelTypeFeatures::only_static_remote_key();
                let tx = CommitmentTransaction::new_with_auxiliary_htlc_data(
                        0, 3000, 0,
-                       false,
                        holder_pubkeys.funding_pubkey,
                        counterparty_pubkeys.funding_pubkey,
                        keys.clone(), 1,
@@ -1764,18 +1880,17 @@ mod tests {
                        &channel_parameters.as_holder_broadcastable()
                );
                assert_eq!(tx.built.transaction.output.len(), 3);
-               assert_eq!(tx.built.transaction.output[0].script_pubkey, get_htlc_redeemscript(&received_htlc, false, &keys).to_v0_p2wsh());
-               assert_eq!(tx.built.transaction.output[1].script_pubkey, get_htlc_redeemscript(&offered_htlc, false, &keys).to_v0_p2wsh());
-               assert_eq!(get_htlc_redeemscript(&received_htlc, false, &keys).to_v0_p2wsh().to_hex(),
+               assert_eq!(tx.built.transaction.output[0].script_pubkey, get_htlc_redeemscript(&received_htlc, &ChannelTypeFeatures::only_static_remote_key(), &keys).to_v0_p2wsh());
+               assert_eq!(tx.built.transaction.output[1].script_pubkey, get_htlc_redeemscript(&offered_htlc, &ChannelTypeFeatures::only_static_remote_key(), &keys).to_v0_p2wsh());
+               assert_eq!(get_htlc_redeemscript(&received_htlc, &ChannelTypeFeatures::only_static_remote_key(), &keys).to_v0_p2wsh().to_hex(),
                                   "0020e43a7c068553003fe68fcae424fb7b28ec5ce48cd8b6744b3945631389bad2fb");
-               assert_eq!(get_htlc_redeemscript(&offered_htlc, false, &keys).to_v0_p2wsh().to_hex(),
+               assert_eq!(get_htlc_redeemscript(&offered_htlc, &ChannelTypeFeatures::only_static_remote_key(), &keys).to_v0_p2wsh().to_hex(),
                                   "0020215d61bba56b19e9eadb6107f5a85d7f99c40f65992443f69229c290165bc00d");
 
                // Generate broadcaster output and received and offered HTLC outputs,  with anchors
-               channel_parameters.opt_anchors = Some(());
+               channel_parameters.channel_type_features = ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies();
                let tx = CommitmentTransaction::new_with_auxiliary_htlc_data(
                        0, 3000, 0,
-                       true,
                        holder_pubkeys.funding_pubkey,
                        counterparty_pubkeys.funding_pubkey,
                        keys.clone(), 1,
@@ -1783,11 +1898,11 @@ mod tests {
                        &channel_parameters.as_holder_broadcastable()
                );
                assert_eq!(tx.built.transaction.output.len(), 5);
-               assert_eq!(tx.built.transaction.output[2].script_pubkey, get_htlc_redeemscript(&received_htlc, true, &keys).to_v0_p2wsh());
-               assert_eq!(tx.built.transaction.output[3].script_pubkey, get_htlc_redeemscript(&offered_htlc, true, &keys).to_v0_p2wsh());
-               assert_eq!(get_htlc_redeemscript(&received_htlc, true, &keys).to_v0_p2wsh().to_hex(),
+               assert_eq!(tx.built.transaction.output[2].script_pubkey, get_htlc_redeemscript(&received_htlc, &ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies(), &keys).to_v0_p2wsh());
+               assert_eq!(tx.built.transaction.output[3].script_pubkey, get_htlc_redeemscript(&offered_htlc, &ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies(), &keys).to_v0_p2wsh());
+               assert_eq!(get_htlc_redeemscript(&received_htlc, &ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies(), &keys).to_v0_p2wsh().to_hex(),
                                   "0020b70d0649c72b38756885c7a30908d912a7898dd5d79457a7280b8e9a20f3f2bc");
-               assert_eq!(get_htlc_redeemscript(&offered_htlc, true, &keys).to_v0_p2wsh().to_hex(),
+               assert_eq!(get_htlc_redeemscript(&offered_htlc, &ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies(), &keys).to_v0_p2wsh().to_hex(),
                                   "002087a3faeb1950a469c0e2db4a79b093a41b9526e5a6fc6ef5cb949bde3be379c7");
        }
 
index e59cf47f17600963c9852389fc5723d8189816ef..707c27908eddabd5efdbabeda8278af225b02950 100644 (file)
@@ -350,10 +350,14 @@ fn do_test_monitor_temporary_update_fail(disconnect_count: usize) {
                nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
                nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
 
-               nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: nodes[1].node.init_features(), remote_network_address: None }, true).unwrap();
+               nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
+                       features: nodes[1].node.init_features(), networks: None, remote_network_address: None
+               }, true).unwrap();
                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(), &msgs::Init { features: nodes[0].node.init_features(), remote_network_address: None }, false).unwrap();
+               nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
+                       features: nodes[0].node.init_features(), networks: None, remote_network_address: None
+               }, false).unwrap();
                let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
                assert_eq!(reestablish_2.len(), 1);
 
@@ -372,10 +376,14 @@ fn do_test_monitor_temporary_update_fail(disconnect_count: usize) {
                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(), &msgs::Init { features: nodes[1].node.init_features(), remote_network_address: None }, true).unwrap();
+               nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
+                       features: nodes[1].node.init_features(), networks: None, remote_network_address: None
+               }, true).unwrap();
                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(), &msgs::Init { features: nodes[0].node.init_features(), remote_network_address: None }, false).unwrap();
+               nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
+                       features: nodes[0].node.init_features(), networks: None, remote_network_address: None
+               }, false).unwrap();
                let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
                assert_eq!(reestablish_2.len(), 1);
 
@@ -1136,8 +1144,12 @@ fn test_monitor_update_fail_reestablish() {
        commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false);
 
        chanmon_cfgs[1].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
-       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: nodes[1].node.init_features(), remote_network_address: None }, true).unwrap();
-       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: nodes[0].node.init_features(), remote_network_address: None }, false).unwrap();
+       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
+               features: nodes[1].node.init_features(), networks: None, remote_network_address: None
+       }, true).unwrap();
+       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
+               features: nodes[0].node.init_features(), networks: None, remote_network_address: None
+       }, false).unwrap();
 
        let as_reestablish = get_chan_reestablish_msgs!(nodes[0], nodes[1]).pop().unwrap();
        let bs_reestablish = get_chan_reestablish_msgs!(nodes[1], nodes[0]).pop().unwrap();
@@ -1155,8 +1167,12 @@ fn test_monitor_update_fail_reestablish() {
        nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
        nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
 
-       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: nodes[1].node.init_features(), remote_network_address: None }, true).unwrap();
-       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: nodes[0].node.init_features(), remote_network_address: None }, false).unwrap();
+       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
+               features: nodes[1].node.init_features(), networks: None, remote_network_address: None
+       }, true).unwrap();
+       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
+               features: nodes[0].node.init_features(), networks: None, remote_network_address: None
+       }, false).unwrap();
 
        assert_eq!(get_chan_reestablish_msgs!(nodes[0], nodes[1]).pop().unwrap(), as_reestablish);
        assert_eq!(get_chan_reestablish_msgs!(nodes[1], nodes[0]).pop().unwrap(), bs_reestablish);
@@ -1331,8 +1347,12 @@ fn claim_while_disconnected_monitor_update_fail() {
        check_added_monitors!(nodes[1], 1);
        expect_payment_claimed!(nodes[1], payment_hash_1, 1_000_000);
 
-       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: nodes[1].node.init_features(), remote_network_address: None }, true).unwrap();
-       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: nodes[0].node.init_features(), remote_network_address: None }, false).unwrap();
+       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
+               features: nodes[1].node.init_features(), networks: None, remote_network_address: None
+       }, true).unwrap();
+       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
+               features: nodes[0].node.init_features(), networks: None, remote_network_address: None
+       }, false).unwrap();
 
        let as_reconnect = get_chan_reestablish_msgs!(nodes[0], nodes[1]).pop().unwrap();
        let bs_reconnect = get_chan_reestablish_msgs!(nodes[1], nodes[0]).pop().unwrap();
@@ -1436,12 +1456,12 @@ fn monitor_failed_no_reestablish_response() {
        {
                let mut node_0_per_peer_lock;
                let mut node_0_peer_state_lock;
-               get_channel_ref!(nodes[0], nodes[1], node_0_per_peer_lock, node_0_peer_state_lock, channel_id).announcement_sigs_state = AnnouncementSigsState::PeerReceived;
+               get_channel_ref!(nodes[0], nodes[1], node_0_per_peer_lock, node_0_peer_state_lock, channel_id).context.announcement_sigs_state = AnnouncementSigsState::PeerReceived;
        }
        {
                let mut node_1_per_peer_lock;
                let mut node_1_peer_state_lock;
-               get_channel_ref!(nodes[1], nodes[0], node_1_per_peer_lock, node_1_peer_state_lock, channel_id).announcement_sigs_state = AnnouncementSigsState::PeerReceived;
+               get_channel_ref!(nodes[1], nodes[0], node_1_per_peer_lock, node_1_peer_state_lock, channel_id).context.announcement_sigs_state = AnnouncementSigsState::PeerReceived;
        }
 
        // Route the payment and deliver the initial commitment_signed (with a monitor update failure
@@ -1467,8 +1487,12 @@ fn monitor_failed_no_reestablish_response() {
        nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
        nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
 
-       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: nodes[1].node.init_features(), remote_network_address: None }, true).unwrap();
-       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: nodes[0].node.init_features(), remote_network_address: None }, false).unwrap();
+       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
+               features: nodes[1].node.init_features(), networks: None, remote_network_address: None
+       }, true).unwrap();
+       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
+               features: nodes[0].node.init_features(), networks: None, remote_network_address: None
+       }, false).unwrap();
 
        let as_reconnect = get_chan_reestablish_msgs!(nodes[0], nodes[1]).pop().unwrap();
        let bs_reconnect = get_chan_reestablish_msgs!(nodes[1], nodes[0]).pop().unwrap();
@@ -2076,9 +2100,13 @@ fn test_pending_update_fee_ack_on_reconnect() {
        nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
        nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
 
-       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: nodes[1].node.init_features(), remote_network_address: None }, true).unwrap();
+       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
+               features: nodes[1].node.init_features(), networks: None, remote_network_address: None
+       }, true).unwrap();
        let as_connect_msg = get_chan_reestablish_msgs!(nodes[0], nodes[1]).pop().unwrap();
-       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: nodes[0].node.init_features(), remote_network_address: None }, false).unwrap();
+       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
+               features: nodes[0].node.init_features(), networks: None, remote_network_address: None
+       }, false).unwrap();
        let bs_connect_msg = get_chan_reestablish_msgs!(nodes[1], nodes[0]).pop().unwrap();
 
        nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &as_connect_msg);
@@ -2204,9 +2232,13 @@ fn do_update_fee_resend_test(deliver_update: bool, parallel_updates: bool) {
        nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
        nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
 
-       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: nodes[1].node.init_features(), remote_network_address: None }, true).unwrap();
+       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
+               features: nodes[1].node.init_features(), networks: None, remote_network_address: None
+       }, true).unwrap();
        let as_connect_msg = get_chan_reestablish_msgs!(nodes[0], nodes[1]).pop().unwrap();
-       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: nodes[0].node.init_features(), remote_network_address: None }, false).unwrap();
+       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
+               features: nodes[0].node.init_features(), networks: None, remote_network_address: None
+       }, false).unwrap();
        let bs_connect_msg = get_chan_reestablish_msgs!(nodes[1], nodes[0]).pop().unwrap();
 
        nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &as_connect_msg);
@@ -2341,10 +2373,14 @@ fn do_channel_holding_cell_serialize(disconnect: bool, reload_a: bool) {
                nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
 
                // Now reconnect the two
-               nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: nodes[1].node.init_features(), remote_network_address: None }, true).unwrap();
+               nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
+                       features: nodes[1].node.init_features(), networks: None, remote_network_address: None
+               }, true).unwrap();
                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(), &msgs::Init { features: nodes[0].node.init_features(), remote_network_address: None }, false).unwrap();
+               nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
+                       features: nodes[0].node.init_features(), networks: None, remote_network_address: None
+               }, false).unwrap();
                let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
                assert_eq!(reestablish_2.len(), 1);
 
index 11f0261d677a3df37b2fa36f94ea142e5ce8f16c..09943845829afb78f08e8b098cee34efaa363e1f 100644 (file)
@@ -27,7 +27,7 @@ use crate::ln::features::{ChannelTypeFeatures, InitFeatures};
 use crate::ln::msgs;
 use crate::ln::msgs::DecodeError;
 use crate::ln::script::{self, ShutdownScript};
-use crate::ln::channelmanager::{self, CounterpartyForwardingInfo, PendingHTLCStatus, HTLCSource, SentHTLCId, HTLCFailureMsg, PendingHTLCInfo, RAACommitmentOrder, BREAKDOWN_TIMEOUT, MIN_CLTV_EXPIRY_DELTA, MAX_LOCAL_BREAKDOWN_TIMEOUT};
+use crate::ln::channelmanager::{self, CounterpartyForwardingInfo, PendingHTLCStatus, HTLCSource, SentHTLCId, HTLCFailureMsg, PendingHTLCInfo, RAACommitmentOrder, BREAKDOWN_TIMEOUT, MIN_CLTV_EXPIRY_DELTA, MAX_LOCAL_BREAKDOWN_TIMEOUT, ChannelShutdownState};
 use crate::ln::chan_utils::{CounterpartyCommitmentSecrets, TxCreationKeys, HTLCOutputInCommitment, htlc_success_tx_weight, htlc_timeout_tx_weight, make_funding_redeemscript, ChannelPublicKeys, CommitmentTransaction, HolderCommitmentTransaction, ChannelTransactionParameters, CounterpartyChannelTransactionParameters, MAX_HTLCS, get_commitment_transaction_number_obscure_factor, ClosingTransaction};
 use crate::ln::chan_utils;
 use crate::ln::onion_utils::HTLCFailReason;
@@ -41,7 +41,7 @@ use crate::routing::gossip::NodeId;
 use crate::util::ser::{Readable, ReadableArgs, Writeable, Writer, VecWriter};
 use crate::util::logger::Logger;
 use crate::util::errors::APIError;
-use crate::util::config::{UserConfig, ChannelConfig, LegacyChannelConfig, ChannelHandshakeConfig, ChannelHandshakeLimits};
+use crate::util::config::{UserConfig, ChannelConfig, LegacyChannelConfig, ChannelHandshakeConfig, ChannelHandshakeLimits, MaxDustHTLCExposure};
 use crate::util::scid_utils::scid_from_parts;
 
 use crate::io;
@@ -73,6 +73,8 @@ pub struct AvailableBalances {
        pub outbound_capacity_msat: u64,
        /// The maximum value we can assign to the next outbound HTLC
        pub next_outbound_htlc_limit_msat: u64,
+       /// The minimum value we can assign to the next outbound HTLC
+       pub next_outbound_htlc_minimum_msat: u64,
 }
 
 #[derive(Debug, Clone, Copy, PartialEq)]
@@ -222,6 +224,7 @@ struct OutboundHTLCOutput {
        payment_hash: PaymentHash,
        state: OutboundHTLCState,
        source: HTLCSource,
+       skimmed_fee_msat: Option<u64>,
 }
 
 /// See AwaitingRemoteRevoke ChannelState for more info
@@ -233,6 +236,8 @@ enum HTLCUpdateAwaitingACK {
                payment_hash: PaymentHash,
                source: HTLCSource,
                onion_routing_packet: msgs::OnionPacket,
+               // The extra fee we're skimming off the top of this HTLC.
+               skimmed_fee_msat: Option<u64>,
        },
        ClaimHTLC {
                payment_preimage: PaymentPreimage,
@@ -302,6 +307,95 @@ const MULTI_STATE_FLAGS: u32 = BOTH_SIDES_SHUTDOWN_MASK | ChannelState::PeerDisc
 
 pub const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
 
+pub const DEFAULT_MAX_HTLCS: u16 = 50;
+
+pub(crate) fn commitment_tx_base_weight(channel_type_features: &ChannelTypeFeatures) -> u64 {
+       const COMMITMENT_TX_BASE_WEIGHT: u64 = 724;
+       const COMMITMENT_TX_BASE_ANCHOR_WEIGHT: u64 = 1124;
+       if channel_type_features.supports_anchors_zero_fee_htlc_tx() { COMMITMENT_TX_BASE_ANCHOR_WEIGHT } else { COMMITMENT_TX_BASE_WEIGHT }
+}
+
+#[cfg(not(test))]
+const COMMITMENT_TX_WEIGHT_PER_HTLC: u64 = 172;
+#[cfg(test)]
+pub const COMMITMENT_TX_WEIGHT_PER_HTLC: u64 = 172;
+
+pub const ANCHOR_OUTPUT_VALUE_SATOSHI: u64 = 330;
+
+/// The percentage of the channel value `holder_max_htlc_value_in_flight_msat` used to be set to,
+/// before this was made configurable. The percentage was made configurable in LDK 0.0.107,
+/// although LDK 0.0.104+ enabled serialization of channels with a different value set for
+/// `holder_max_htlc_value_in_flight_msat`.
+pub const MAX_IN_FLIGHT_PERCENT_LEGACY: u8 = 10;
+
+/// Maximum `funding_satoshis` value according to the BOLT #2 specification, if
+/// `option_support_large_channel` (aka wumbo channels) is not supported.
+/// It's 2^24 - 1.
+pub const MAX_FUNDING_SATOSHIS_NO_WUMBO: u64 = (1 << 24) - 1;
+
+/// Total bitcoin supply in satoshis.
+pub const TOTAL_BITCOIN_SUPPLY_SATOSHIS: u64 = 21_000_000 * 1_0000_0000;
+
+/// The maximum network dust limit for standard script formats. This currently represents the
+/// minimum output value for a P2SH output before Bitcoin Core 22 considers the entire
+/// transaction non-standard and thus refuses to relay it.
+/// We also use this as the maximum counterparty `dust_limit_satoshis` allowed, given many
+/// implementations use this value for their dust limit today.
+pub const MAX_STD_OUTPUT_DUST_LIMIT_SATOSHIS: u64 = 546;
+
+/// The maximum channel dust limit we will accept from our counterparty.
+pub const MAX_CHAN_DUST_LIMIT_SATOSHIS: u64 = MAX_STD_OUTPUT_DUST_LIMIT_SATOSHIS;
+
+/// The dust limit is used for both the commitment transaction outputs as well as the closing
+/// transactions. For cooperative closing transactions, we require segwit outputs, though accept
+/// *any* segwit scripts, which are allowed to be up to 42 bytes in length.
+/// In order to avoid having to concern ourselves with standardness during the closing process, we
+/// simply require our counterparty to use a dust limit which will leave any segwit output
+/// standard.
+/// See <https://github.com/lightning/bolts/issues/905> for more details.
+pub const MIN_CHAN_DUST_LIMIT_SATOSHIS: u64 = 354;
+
+// Just a reasonable implementation-specific safe lower bound, higher than the dust limit.
+pub const MIN_THEIR_CHAN_RESERVE_SATOSHIS: u64 = 1000;
+
+/// 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(String),
+       Warn(String),
+       Close(String),
+}
+
+impl fmt::Debug for ChannelError {
+       fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+               match self {
+                       &ChannelError::Ignore(ref e) => write!(f, "Ignore : {}", e),
+                       &ChannelError::Warn(ref e) => write!(f, "Warn : {}", e),
+                       &ChannelError::Close(ref e) => write!(f, "Close : {}", e),
+               }
+       }
+}
+
+impl fmt::Display for ChannelError {
+       fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+               match self {
+                       &ChannelError::Ignore(ref e) => write!(f, "{}", e),
+                       &ChannelError::Warn(ref e) => write!(f, "{}", e),
+                       &ChannelError::Close(ref e) => write!(f, "{}", e),
+               }
+       }
+}
+
+macro_rules! secp_check {
+       ($res: expr, $err: expr) => {
+               match $res {
+                       Ok(thing) => thing,
+                       Err(_) => return Err(ChannelError::Close($err)),
+               }
+       };
+}
+
 /// The "channel disabled" bit in channel_update must be set based on whether we are connected to
 /// our counterparty or not. However, we don't want to announce updates right away to avoid
 /// spamming the network with updates if the connection is flapping. Instead, we "stage" updates to
@@ -394,13 +488,13 @@ enum UpdateFulfillFetch {
 }
 
 /// The return type of get_update_fulfill_htlc_and_commit.
-pub enum UpdateFulfillCommitFetch<'a> {
+pub enum UpdateFulfillCommitFetch {
        /// Indicates the HTLC fulfill is new, and either generated an update_fulfill message, placed
        /// it in the holding cell, or re-generated the update_fulfill message after the same claim was
        /// previously placed in the holding cell (and has since been removed).
        NewClaim {
                /// The ChannelMonitorUpdate which places the new payment preimage in the channel monitor
-               monitor_update: &'a ChannelMonitorUpdate,
+               monitor_update: ChannelMonitorUpdate,
                /// The value of the HTLC which was claimed, in msat.
                htlc_value_msat: u64,
        },
@@ -432,6 +526,12 @@ pub(super) struct ReestablishResponses {
        pub shutdown_msg: Option<msgs::Shutdown>,
 }
 
+/// The return type of `force_shutdown`
+pub(crate) type ShutdownResult = (
+       Option<(PublicKey, OutPoint, ChannelMonitorUpdate)>,
+       Vec<(HTLCSource, PaymentHash, PublicKey, [u8; 32])>
+);
+
 /// If the majority of the channels funds are to the fundee and the initiator holds only just
 /// enough funds to cover their reserve value, channels are at risk of getting "stuck". Because the
 /// initiator controls the feerate, if they then go to increase the channel fee, they may have no
@@ -479,29 +579,23 @@ pub(crate) const MIN_AFFORDABLE_HTLC_COUNT: usize = 4;
 ///   * `EXPIRE_PREV_CONFIG_TICKS` = convergence_delay / tick_interval
 pub(crate) const EXPIRE_PREV_CONFIG_TICKS: usize = 5;
 
+/// The number of ticks that may elapse while we're waiting for a response to a
+/// [`msgs::RevokeAndACK`] or [`msgs::ChannelReestablish`] message before we attempt to disconnect
+/// them.
+///
+/// See [`ChannelContext::sent_message_awaiting_response`] for more information.
+pub(crate) const DISCONNECT_PEER_AWAITING_RESPONSE_TICKS: usize = 2;
+
 struct PendingChannelMonitorUpdate {
        update: ChannelMonitorUpdate,
-       /// In some cases we need to delay letting the [`ChannelMonitorUpdate`] go until after an
-       /// `Event` is processed by the user. This bool indicates the [`ChannelMonitorUpdate`] is
-       /// blocked on some external event and the [`ChannelManager`] will update us when we're ready.
-       ///
-       /// [`ChannelManager`]: super::channelmanager::ChannelManager
-       blocked: bool,
 }
 
 impl_writeable_tlv_based!(PendingChannelMonitorUpdate, {
        (0, update, required),
-       (2, blocked, required),
 });
 
-// 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.
-//
-// Holder designates channel data owned for the benefice of the user client.
-// Counterparty designates channel data owned by the another channel participant entity.
-pub(super) struct Channel<Signer: ChannelSigner> {
+/// Contains everything about the channel including state, and various flags.
+pub(super) struct ChannelContext<Signer: ChannelSigner> {
        config: LegacyChannelConfig,
 
        // Track the previous `ChannelConfig` so that we can continue forwarding HTLCs that were
@@ -715,6 +809,19 @@ pub(super) struct Channel<Signer: ChannelSigner> {
        /// See-also <https://github.com/lightningnetwork/lnd/issues/4006>
        pub workaround_lnd_bug_4006: Option<msgs::ChannelReady>,
 
+       /// An option set when we wish to track how many ticks have elapsed while waiting for a response
+       /// from our counterparty after sending a message. If the peer has yet to respond after reaching
+       /// `DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`, a reconnection should be attempted to try to
+       /// unblock the state machine.
+       ///
+       /// This behavior is mostly motivated by a lnd bug in which we don't receive a message we expect
+       /// to in a timely manner, which may lead to channels becoming unusable and/or force-closed. An
+       /// example of such can be found at <https://github.com/lightningnetwork/lnd/issues/7682>.
+       ///
+       /// This is currently only used when waiting for a [`msgs::ChannelReestablish`] or
+       /// [`msgs::RevokeAndACK`] message from the counterparty.
+       sent_message_awaiting_response: Option<usize>,
+
        #[cfg(any(test, fuzzing))]
        // When we receive an HTLC fulfill on an outbound path, we may immediately fulfill the
        // corresponding HTLC on the inbound path. If, then, the outbound path channel is
@@ -755,757 +862,314 @@ pub(super) struct Channel<Signer: ChannelSigner> {
        /// [`SignerProvider::derive_channel_signer`].
        channel_keys_id: [u8; 32],
 
-       /// When we generate [`ChannelMonitorUpdate`]s to persist, they may not be persisted immediately.
-       /// If we then persist the [`channelmanager::ChannelManager`] and crash before the persistence
-       /// completes we still need to be able to complete the persistence. Thus, we have to keep a
-       /// copy of the [`ChannelMonitorUpdate`] here until it is complete.
-       pending_monitor_updates: Vec<PendingChannelMonitorUpdate>,
+       /// If we can't release a [`ChannelMonitorUpdate`] until some external action completes, we
+       /// store it here and only release it to the `ChannelManager` once it asks for it.
+       blocked_monitor_updates: Vec<PendingChannelMonitorUpdate>,
 }
 
-#[cfg(any(test, fuzzing))]
-struct CommitmentTxInfoCached {
-       fee: u64,
-       total_pending_htlcs: usize,
-       next_holder_htlc_id: u64,
-       next_counterparty_htlc_id: u64,
-       feerate: u32,
-}
+impl<Signer: ChannelSigner> ChannelContext<Signer> {
+       /// Allowed in any state (including after shutdown)
+       pub fn get_update_time_counter(&self) -> u32 {
+               self.update_time_counter
+       }
 
-pub const DEFAULT_MAX_HTLCS: u16 = 50;
+       pub fn get_latest_monitor_update_id(&self) -> u64 {
+               self.latest_monitor_update_id
+       }
 
-pub(crate) fn commitment_tx_base_weight(opt_anchors: bool) -> u64 {
-       const COMMITMENT_TX_BASE_WEIGHT: u64 = 724;
-       const COMMITMENT_TX_BASE_ANCHOR_WEIGHT: u64 = 1124;
-       if opt_anchors { COMMITMENT_TX_BASE_ANCHOR_WEIGHT } else { COMMITMENT_TX_BASE_WEIGHT }
-}
+       pub fn should_announce(&self) -> bool {
+               self.config.announced_channel
+       }
 
-#[cfg(not(test))]
-const COMMITMENT_TX_WEIGHT_PER_HTLC: u64 = 172;
-#[cfg(test)]
-pub const COMMITMENT_TX_WEIGHT_PER_HTLC: u64 = 172;
+       pub fn is_outbound(&self) -> bool {
+               self.channel_transaction_parameters.is_outbound_from_holder
+       }
 
-pub const ANCHOR_OUTPUT_VALUE_SATOSHI: u64 = 330;
+       /// 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_outbound_forwarding_fee_base_msat(&self) -> u32 {
+               self.config.options.forwarding_fee_base_msat
+       }
 
-/// The percentage of the channel value `holder_max_htlc_value_in_flight_msat` used to be set to,
-/// before this was made configurable. The percentage was made configurable in LDK 0.0.107,
-/// although LDK 0.0.104+ enabled serialization of channels with a different value set for
-/// `holder_max_htlc_value_in_flight_msat`.
-pub const MAX_IN_FLIGHT_PERCENT_LEGACY: u8 = 10;
+       /// 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)
+       }
 
-/// Maximum `funding_satoshis` value according to the BOLT #2 specification, if
-/// `option_support_large_channel` (aka wumbo channels) is not supported.
-/// It's 2^24 - 1.
-pub const MAX_FUNDING_SATOSHIS_NO_WUMBO: u64 = (1 << 24) - 1;
+       /// 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::ChannelReady as u32 | BOTH_SIDES_SHUTDOWN_MASK;
+               (self.channel_state & mask) == (ChannelState::ChannelReady as u32) && !self.monitor_pending_channel_ready
+       }
 
-/// Total bitcoin supply in satoshis.
-pub const TOTAL_BITCOIN_SUPPLY_SATOSHIS: u64 = 21_000_000 * 1_0000_0000;
+       /// shutdown state returns the state of the channel in its various stages of shutdown
+       pub fn shutdown_state(&self) -> ChannelShutdownState {
+               if self.channel_state & (ChannelState::ShutdownComplete as u32) != 0 {
+                       return ChannelShutdownState::ShutdownComplete;
+               }
+               if self.channel_state & (ChannelState::LocalShutdownSent as u32) != 0 &&  self.channel_state & (ChannelState::RemoteShutdownSent as u32) == 0 {
+                       return ChannelShutdownState::ShutdownInitiated;
+               }
+               if (self.channel_state & BOTH_SIDES_SHUTDOWN_MASK != 0) && !self.closing_negotiation_ready() {
+                       return ChannelShutdownState::ResolvingHTLCs;
+               }
+               if (self.channel_state & BOTH_SIDES_SHUTDOWN_MASK != 0) && self.closing_negotiation_ready() {
+                       return ChannelShutdownState::NegotiatingClosingFee;
+               }
+               return ChannelShutdownState::NotShuttingDown;
+       }
 
-/// The maximum network dust limit for standard script formats. This currently represents the
-/// minimum output value for a P2SH output before Bitcoin Core 22 considers the entire
-/// transaction non-standard and thus refuses to relay it.
-/// We also use this as the maximum counterparty `dust_limit_satoshis` allowed, given many
-/// implementations use this value for their dust limit today.
-pub const MAX_STD_OUTPUT_DUST_LIMIT_SATOSHIS: u64 = 546;
+       fn closing_negotiation_ready(&self) -> bool {
+               self.pending_inbound_htlcs.is_empty() &&
+               self.pending_outbound_htlcs.is_empty() &&
+               self.pending_update_fee.is_none() &&
+               self.channel_state &
+               (BOTH_SIDES_SHUTDOWN_MASK |
+                       ChannelState::AwaitingRemoteRevoke as u32 |
+                       ChannelState::PeerDisconnected as u32 |
+                       ChannelState::MonitorUpdateInProgress as u32) == BOTH_SIDES_SHUTDOWN_MASK
+       }
 
-/// The maximum channel dust limit we will accept from our counterparty.
-pub const MAX_CHAN_DUST_LIMIT_SATOSHIS: u64 = MAX_STD_OUTPUT_DUST_LIMIT_SATOSHIS;
+       /// 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) == 0)
+       }
 
-/// The dust limit is used for both the commitment transaction outputs as well as the closing
-/// transactions. For cooperative closing transactions, we require segwit outputs, though accept
-/// *any* segwit scripts, which are allowed to be up to 42 bytes in length.
-/// In order to avoid having to concern ourselves with standardness during the closing process, we
-/// simply require our counterparty to use a dust limit which will leave any segwit output
-/// standard.
-/// See <https://github.com/lightning/bolts/issues/905> for more details.
-pub const MIN_CHAN_DUST_LIMIT_SATOSHIS: u64 = 354;
+       // Public utilities:
 
-// Just a reasonable implementation-specific safe lower bound, higher than the dust limit.
-pub const MIN_THEIR_CHAN_RESERVE_SATOSHIS: u64 = 1000;
+       pub fn channel_id(&self) -> [u8; 32] {
+               self.channel_id
+       }
 
-/// 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(String),
-       Warn(String),
-       Close(String),
-}
+       // Return the `temporary_channel_id` used during channel establishment.
+       //
+       // Will return `None` for channels created prior to LDK version 0.0.115.
+       pub fn temporary_channel_id(&self) -> Option<[u8; 32]> {
+               self.temporary_channel_id
+       }
 
-impl fmt::Debug for ChannelError {
-       fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-               match self {
-                       &ChannelError::Ignore(ref e) => write!(f, "Ignore : {}", e),
-                       &ChannelError::Warn(ref e) => write!(f, "Warn : {}", e),
-                       &ChannelError::Close(ref e) => write!(f, "Close : {}", e),
-               }
+       pub fn minimum_depth(&self) -> Option<u32> {
+               self.minimum_depth
        }
-}
 
-macro_rules! secp_check {
-       ($res: expr, $err: expr) => {
-               match $res {
-                       Ok(thing) => thing,
-                       Err(_) => return Err(ChannelError::Close($err)),
-               }
-       };
-}
+       /// 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) -> u128 {
+               self.user_id
+       }
 
-impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
-       /// Returns the value to use for `holder_max_htlc_value_in_flight_msat` as a percentage of the
-       /// `channel_value_satoshis` in msat, set through
-       /// [`ChannelHandshakeConfig::max_inbound_htlc_value_in_flight_percent_of_channel`]
-       ///
-       /// The effective percentage is lower bounded by 1% and upper bounded by 100%.
-       ///
-       /// [`ChannelHandshakeConfig::max_inbound_htlc_value_in_flight_percent_of_channel`]: crate::util::config::ChannelHandshakeConfig::max_inbound_htlc_value_in_flight_percent_of_channel
-       fn get_holder_max_htlc_value_in_flight_msat(channel_value_satoshis: u64, config: &ChannelHandshakeConfig) -> u64 {
-               let configured_percent = if config.max_inbound_htlc_value_in_flight_percent_of_channel < 1 {
-                       1
-               } else if config.max_inbound_htlc_value_in_flight_percent_of_channel > 100 {
-                       100
-               } else {
-                       config.max_inbound_htlc_value_in_flight_percent_of_channel as u64
-               };
-               channel_value_satoshis * 10 * configured_percent
+       /// Gets the channel's type
+       pub fn get_channel_type(&self) -> &ChannelTypeFeatures {
+               &self.channel_type
        }
 
-       /// Returns a minimum channel reserve value the remote needs to maintain,
-       /// required by us according to the configured or default
-       /// [`ChannelHandshakeConfig::their_channel_reserve_proportional_millionths`]
-       ///
-       /// Guaranteed to return a value no larger than channel_value_satoshis
-       ///
-       /// This is used both for outbound and inbound channels and has lower bound
-       /// of `MIN_THEIR_CHAN_RESERVE_SATOSHIS`.
-       pub(crate) fn get_holder_selected_channel_reserve_satoshis(channel_value_satoshis: u64, config: &UserConfig) -> u64 {
-               let calculated_reserve = channel_value_satoshis.saturating_mul(config.channel_handshake_config.their_channel_reserve_proportional_millionths as u64) / 1_000_000;
-               cmp::min(channel_value_satoshis, cmp::max(calculated_reserve, MIN_THEIR_CHAN_RESERVE_SATOSHIS))
+       /// Guaranteed to be Some after both ChannelReady 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
        }
 
-       /// This is for legacy reasons, present for forward-compatibility.
-       /// LDK versions older than 0.0.104 don't know how read/handle values other than default
-       /// from storage. Hence, we use this function to not persist default values of
-       /// `holder_selected_channel_reserve_satoshis` for channels into storage.
-       pub(crate) fn get_legacy_default_holder_selected_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))
+       /// Allowed in any state (including after shutdown)
+       pub fn latest_inbound_scid_alias(&self) -> Option<u64> {
+               self.latest_inbound_scid_alias
        }
 
-       pub(crate) fn opt_anchors(&self) -> bool {
-               self.channel_transaction_parameters.opt_anchors.is_some()
+       /// Allowed in any state (including after shutdown)
+       pub fn outbound_scid_alias(&self) -> u64 {
+               self.outbound_scid_alias
        }
 
-       fn get_initial_channel_type(config: &UserConfig, their_features: &InitFeatures) -> ChannelTypeFeatures {
-               // The default channel type (ie the first one we try) depends on whether the channel is
-               // public - if it is, we just go with `only_static_remotekey` as it's the only option
-               // available. If it's private, we first try `scid_privacy` as it provides better privacy
-               // with no other changes, and fall back to `only_static_remotekey`.
-               let mut ret = ChannelTypeFeatures::only_static_remote_key();
-               if !config.channel_handshake_config.announced_channel &&
-                       config.channel_handshake_config.negotiate_scid_privacy &&
-                       their_features.supports_scid_privacy() {
-                       ret.set_scid_privacy_required();
-               }
-
-               // Optionally, if the user would like to negotiate the `anchors_zero_fee_htlc_tx` option, we
-               // set it now. If they don't understand it, we'll fall back to our default of
-               // `only_static_remotekey`.
-               #[cfg(anchors)]
-               { // Attributes are not allowed on if expressions on our current MSRV of 1.41.
-                       if config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx &&
-                               their_features.supports_anchors_zero_fee_htlc_tx() {
-                               ret.set_anchors_zero_fee_htlc_tx_required();
-                       }
-               }
-
-               ret
+       /// Only allowed immediately after deserialization if get_outbound_scid_alias returns 0,
+       /// indicating we were written by LDK prior to 0.0.106 which did not set outbound SCID aliases.
+       pub fn set_outbound_scid_alias(&mut self, outbound_scid_alias: u64) {
+               assert_eq!(self.outbound_scid_alias, 0);
+               self.outbound_scid_alias = outbound_scid_alias;
        }
 
-       /// If we receive an error message, it may only be a rejection of the channel type we tried,
-       /// not of our ability to open any channel at all. Thus, on error, we should first call this
-       /// and see if we get a new `OpenChannel` message, otherwise the channel is failed.
-       pub(crate) fn maybe_handle_error_without_close(&mut self, chain_hash: BlockHash) -> Result<msgs::OpenChannel, ()> {
-               if !self.is_outbound() || self.channel_state != ChannelState::OurInitSent as u32 { return Err(()); }
-               if self.channel_type == ChannelTypeFeatures::only_static_remote_key() {
-                       // We've exhausted our options
-                       return Err(());
-               }
-               // We support opening a few different types of channels. Try removing our additional
-               // features one by one until we've either arrived at our default or the counterparty has
-               // accepted one.
-               //
-               // Due to the order below, we may not negotiate `option_anchors_zero_fee_htlc_tx` if the
-               // counterparty doesn't support `option_scid_privacy`. Since `get_initial_channel_type`
-               // checks whether the counterparty supports every feature, this would only happen if the
-               // counterparty is advertising the feature, but rejecting channels proposing the feature for
-               // whatever reason.
-               if self.channel_type.supports_anchors_zero_fee_htlc_tx() {
-                       self.channel_type.clear_anchors_zero_fee_htlc_tx();
-                       assert!(self.channel_transaction_parameters.opt_non_zero_fee_anchors.is_none());
-                       self.channel_transaction_parameters.opt_anchors = None;
-               } else if self.channel_type.supports_scid_privacy() {
-                       self.channel_type.clear_scid_privacy();
-               } else {
-                       self.channel_type = ChannelTypeFeatures::only_static_remote_key();
-               }
-               Ok(self.get_open_channel(chain_hash))
+       /// 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_transaction_parameters.funding_outpoint
        }
 
-       // Constructors:
-       pub fn new_outbound<ES: Deref, SP: Deref, F: Deref>(
-               fee_estimator: &LowerBoundedFeeEstimator<F>, entropy_source: &ES, signer_provider: &SP, counterparty_node_id: PublicKey, their_features: &InitFeatures,
-               channel_value_satoshis: u64, push_msat: u64, user_id: u128, config: &UserConfig, current_chain_height: u32,
-               outbound_scid_alias: u64
-       ) -> Result<Channel<Signer>, APIError>
-       where ES::Target: EntropySource,
-             SP::Target: SignerProvider<Signer = Signer>,
-             F::Target: FeeEstimator,
-       {
-               let holder_selected_contest_delay = config.channel_handshake_config.our_to_self_delay;
-               let channel_keys_id = signer_provider.generate_channel_keys_id(false, channel_value_satoshis, user_id);
-               let holder_signer = signer_provider.derive_channel_signer(channel_value_satoshis, channel_keys_id);
-               let pubkeys = holder_signer.pubkeys().clone();
+       /// Returns the block hash in which our funding transaction was confirmed.
+       pub fn get_funding_tx_confirmed_in(&self) -> Option<BlockHash> {
+               self.funding_tx_confirmed_in
+       }
 
-               if !their_features.supports_wumbo() && channel_value_satoshis > MAX_FUNDING_SATOSHIS_NO_WUMBO {
-                       return Err(APIError::APIMisuseError{err: format!("funding_value must not exceed {}, it was {}", MAX_FUNDING_SATOSHIS_NO_WUMBO, channel_value_satoshis)});
-               }
-               if channel_value_satoshis >= TOTAL_BITCOIN_SUPPLY_SATOSHIS {
-                       return Err(APIError::APIMisuseError{err: format!("funding_value must be smaller than the total bitcoin supply, it was {}", channel_value_satoshis)});
-               }
-               let channel_value_msat = channel_value_satoshis * 1000;
-               if push_msat > channel_value_msat {
-                       return Err(APIError::APIMisuseError { err: format!("Push value ({}) was larger than channel_value ({})", push_msat, channel_value_msat) });
-               }
-               if holder_selected_contest_delay < BREAKDOWN_TIMEOUT {
-                       return Err(APIError::APIMisuseError {err: format!("Configured with an unreasonable our_to_self_delay ({}) putting user funds at risks", holder_selected_contest_delay)});
-               }
-               let holder_selected_channel_reserve_satoshis = Channel::<Signer>::get_holder_selected_channel_reserve_satoshis(channel_value_satoshis, config);
-               if holder_selected_channel_reserve_satoshis < MIN_CHAN_DUST_LIMIT_SATOSHIS {
-                       // Protocol level safety check in place, although it should never happen because
-                       // of `MIN_THEIR_CHAN_RESERVE_SATOSHIS`
-                       return Err(APIError::APIMisuseError { err: format!("Holder selected channel  reserve below implemention limit dust_limit_satoshis {}", holder_selected_channel_reserve_satoshis) });
+       /// Returns the current number of confirmations on the funding transaction.
+       pub fn get_funding_tx_confirmations(&self, height: u32) -> u32 {
+               if self.funding_tx_confirmation_height == 0 {
+                       // We either haven't seen any confirmation yet, or observed a reorg.
+                       return 0;
                }
 
-               let channel_type = Self::get_initial_channel_type(&config, their_features);
-               debug_assert!(channel_type.is_subset(&channelmanager::provided_channel_type_features(&config)));
+               height.checked_sub(self.funding_tx_confirmation_height).map_or(0, |c| c + 1)
+       }
 
-               let feerate = fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Normal);
+       fn get_holder_selected_contest_delay(&self) -> u16 {
+               self.channel_transaction_parameters.holder_selected_contest_delay
+       }
 
-               let value_to_self_msat = channel_value_satoshis * 1000 - push_msat;
-               let commitment_tx_fee = Self::commit_tx_fee_msat(feerate, MIN_AFFORDABLE_HTLC_COUNT, channel_type.requires_anchors_zero_fee_htlc_tx());
-               if value_to_self_msat < commitment_tx_fee {
-                       return Err(APIError::APIMisuseError{ err: format!("Funding amount ({}) can't even pay fee for initial commitment transaction fee of {}.", value_to_self_msat / 1000, commitment_tx_fee / 1000) });
-               }
+       fn get_holder_pubkeys(&self) -> &ChannelPublicKeys {
+               &self.channel_transaction_parameters.holder_pubkeys
+       }
 
-               let mut secp_ctx = Secp256k1::new();
-               secp_ctx.seeded_randomize(&entropy_source.get_secure_random_bytes());
+       pub fn get_counterparty_selected_contest_delay(&self) -> Option<u16> {
+               self.channel_transaction_parameters.counterparty_parameters
+                       .as_ref().map(|params| params.selected_contest_delay)
+       }
 
-               let shutdown_scriptpubkey = if config.channel_handshake_config.commit_upfront_shutdown_pubkey {
-                       match signer_provider.get_shutdown_scriptpubkey() {
-                               Ok(scriptpubkey) => Some(scriptpubkey),
-                               Err(_) => return Err(APIError::ChannelUnavailable { err: "Failed to get shutdown scriptpubkey".to_owned()}),
-                       }
-               } else { None };
+       fn get_counterparty_pubkeys(&self) -> &ChannelPublicKeys {
+               &self.channel_transaction_parameters.counterparty_parameters.as_ref().unwrap().pubkeys
+       }
 
-               if let Some(shutdown_scriptpubkey) = &shutdown_scriptpubkey {
-                       if !shutdown_scriptpubkey.is_compatible(&their_features) {
-                               return Err(APIError::IncompatibleShutdownScript { script: shutdown_scriptpubkey.clone() });
-                       }
-               }
+       /// Allowed in any state (including after shutdown)
+       pub fn get_counterparty_node_id(&self) -> PublicKey {
+               self.counterparty_node_id
+       }
 
-               let destination_script = match signer_provider.get_destination_script() {
-                       Ok(script) => script,
-                       Err(_) => return Err(APIError::ChannelUnavailable { err: "Failed to get destination script".to_owned()}),
-               };
+       /// Allowed in any state (including after shutdown)
+       pub fn get_holder_htlc_minimum_msat(&self) -> u64 {
+               self.holder_htlc_minimum_msat
+       }
 
-               let temporary_channel_id = entropy_source.get_secure_random_bytes();
+       /// Allowed in any state (including after shutdown), but will return none before TheirInitSent
+       pub fn get_holder_htlc_maximum_msat(&self) -> Option<u64> {
+               self.get_htlc_maximum_msat(self.holder_max_htlc_value_in_flight_msat)
+       }
 
-               Ok(Channel {
-                       user_id,
+       /// Allowed in any state (including after shutdown)
+       pub fn get_announced_htlc_max_msat(&self) -> u64 {
+               return cmp::min(
+                       // Upper bound by capacity. We make it a bit less than full capacity to prevent attempts
+                       // to use full capacity. This is an effort to reduce routing failures, because in many cases
+                       // channel might have been used to route very small values (either by honest users or as DoS).
+                       self.channel_value_satoshis * 1000 * 9 / 10,
 
-                       config: LegacyChannelConfig {
-                               options: config.channel_config.clone(),
-                               announced_channel: config.channel_handshake_config.announced_channel,
-                               commit_upfront_shutdown_pubkey: config.channel_handshake_config.commit_upfront_shutdown_pubkey,
-                       },
+                       self.counterparty_max_htlc_value_in_flight_msat
+               );
+       }
 
-                       prev_config: None,
-
-                       inbound_handshake_limits_override: Some(config.channel_handshake_limits.clone()),
-
-                       channel_id: temporary_channel_id,
-                       temporary_channel_id: Some(temporary_channel_id),
-                       channel_state: ChannelState::OurInitSent as u32,
-                       announcement_sigs_state: AnnouncementSigsState::NotSent,
-                       secp_ctx,
-                       channel_value_satoshis,
-
-                       latest_monitor_update_id: 0,
-
-                       holder_signer,
-                       shutdown_scriptpubkey,
-                       destination_script,
-
-                       cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
-                       cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
-                       value_to_self_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_holder_htlc_id: 0,
-                       next_counterparty_htlc_id: 0,
-                       update_time_counter: 1,
-
-                       resend_order: RAACommitmentOrder::CommitmentFirst,
-
-                       monitor_pending_channel_ready: false,
-                       monitor_pending_revoke_and_ack: false,
-                       monitor_pending_commitment_signed: false,
-                       monitor_pending_forwards: Vec::new(),
-                       monitor_pending_failures: Vec::new(),
-                       monitor_pending_finalized_fulfills: Vec::new(),
-
-                       #[cfg(debug_assertions)]
-                       holder_max_commitment_tx_output: Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
-                       #[cfg(debug_assertions)]
-                       counterparty_max_commitment_tx_output: Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
-
-                       last_sent_closing_fee: None,
-                       pending_counterparty_closing_signed: None,
-                       closing_fee_limits: None,
-                       target_closing_feerate_sats_per_kw: None,
-
-                       inbound_awaiting_accept: false,
-
-                       funding_tx_confirmed_in: None,
-                       funding_tx_confirmation_height: 0,
-                       short_channel_id: None,
-                       channel_creation_height: current_chain_height,
-
-                       feerate_per_kw: feerate,
-                       counterparty_dust_limit_satoshis: 0,
-                       holder_dust_limit_satoshis: MIN_CHAN_DUST_LIMIT_SATOSHIS,
-                       counterparty_max_htlc_value_in_flight_msat: 0,
-                       holder_max_htlc_value_in_flight_msat: Self::get_holder_max_htlc_value_in_flight_msat(channel_value_satoshis, &config.channel_handshake_config),
-                       counterparty_selected_channel_reserve_satoshis: None, // Filled in in accept_channel
-                       holder_selected_channel_reserve_satoshis,
-                       counterparty_htlc_minimum_msat: 0,
-                       holder_htlc_minimum_msat: if config.channel_handshake_config.our_htlc_minimum_msat == 0 { 1 } else { config.channel_handshake_config.our_htlc_minimum_msat },
-                       counterparty_max_accepted_htlcs: 0,
-                       holder_max_accepted_htlcs: cmp::min(config.channel_handshake_config.our_max_accepted_htlcs, MAX_HTLCS),
-                       minimum_depth: None, // Filled in in accept_channel
-
-                       counterparty_forwarding_info: None,
-
-                       channel_transaction_parameters: ChannelTransactionParameters {
-                               holder_pubkeys: pubkeys,
-                               holder_selected_contest_delay: config.channel_handshake_config.our_to_self_delay,
-                               is_outbound_from_holder: true,
-                               counterparty_parameters: None,
-                               funding_outpoint: None,
-                               opt_anchors: if channel_type.requires_anchors_zero_fee_htlc_tx() { Some(()) } else { None },
-                               opt_non_zero_fee_anchors: None
-                       },
-                       funding_transaction: None,
+       /// Allowed in any state (including after shutdown)
+       pub fn get_counterparty_htlc_minimum_msat(&self) -> u64 {
+               self.counterparty_htlc_minimum_msat
+       }
 
-                       counterparty_cur_commitment_point: None,
-                       counterparty_prev_commitment_point: None,
-                       counterparty_node_id,
+       /// Allowed in any state (including after shutdown), but will return none before TheirInitSent
+       pub fn get_counterparty_htlc_maximum_msat(&self) -> Option<u64> {
+               self.get_htlc_maximum_msat(self.counterparty_max_htlc_value_in_flight_msat)
+       }
 
-                       counterparty_shutdown_scriptpubkey: None,
+       fn get_htlc_maximum_msat(&self, party_max_htlc_value_in_flight_msat: u64) -> Option<u64> {
+               self.counterparty_selected_channel_reserve_satoshis.map(|counterparty_reserve| {
+                       let holder_reserve = self.holder_selected_channel_reserve_satoshis;
+                       cmp::min(
+                               (self.channel_value_satoshis - counterparty_reserve - holder_reserve) * 1000,
+                               party_max_htlc_value_in_flight_msat
+                       )
+               })
+       }
 
-                       commitment_secrets: CounterpartyCommitmentSecrets::new(),
+       pub fn get_value_satoshis(&self) -> u64 {
+               self.channel_value_satoshis
+       }
 
-                       channel_update_status: ChannelUpdateStatus::Enabled,
-                       closing_signed_in_flight: false,
+       pub fn get_fee_proportional_millionths(&self) -> u32 {
+               self.config.options.forwarding_fee_proportional_millionths
+       }
 
-                       announcement_sigs: None,
+       pub fn get_cltv_expiry_delta(&self) -> u16 {
+               cmp::max(self.config.options.cltv_expiry_delta, MIN_CLTV_EXPIRY_DELTA)
+       }
 
-                       #[cfg(any(test, fuzzing))]
-                       next_local_commitment_tx_fee_info_cached: Mutex::new(None),
-                       #[cfg(any(test, fuzzing))]
-                       next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
+       pub fn get_max_dust_htlc_exposure_msat<F: Deref>(&self,
+               fee_estimator: &LowerBoundedFeeEstimator<F>) -> u64
+       where F::Target: FeeEstimator
+       {
+               match self.config.options.max_dust_htlc_exposure {
+                       MaxDustHTLCExposure::FeeRateMultiplier(multiplier) => {
+                               let feerate_per_kw = fee_estimator.bounded_sat_per_1000_weight(
+                                       ConfirmationTarget::HighPriority);
+                               feerate_per_kw as u64 * multiplier
+                       },
+                       MaxDustHTLCExposure::FixedLimitMsat(limit) => limit,
+               }
+       }
 
-                       workaround_lnd_bug_4006: None,
+       /// Returns the previous [`ChannelConfig`] applied to this channel, if any.
+       pub fn prev_config(&self) -> Option<ChannelConfig> {
+               self.prev_config.map(|prev_config| prev_config.0)
+       }
 
-                       latest_inbound_scid_alias: None,
-                       outbound_scid_alias,
+       // Checks whether we should emit a `ChannelPending` event.
+       pub(crate) fn should_emit_channel_pending_event(&mut self) -> bool {
+               self.is_funding_initiated() && !self.channel_pending_event_emitted
+       }
 
-                       channel_pending_event_emitted: false,
-                       channel_ready_event_emitted: false,
+       // Returns whether we already emitted a `ChannelPending` event.
+       pub(crate) fn channel_pending_event_emitted(&self) -> bool {
+               self.channel_pending_event_emitted
+       }
 
-                       #[cfg(any(test, fuzzing))]
-                       historical_inbound_htlc_fulfills: HashSet::new(),
+       // Remembers that we already emitted a `ChannelPending` event.
+       pub(crate) fn set_channel_pending_event_emitted(&mut self) {
+               self.channel_pending_event_emitted = true;
+       }
 
-                       channel_type,
-                       channel_keys_id,
+       // Checks whether we should emit a `ChannelReady` event.
+       pub(crate) fn should_emit_channel_ready_event(&mut self) -> bool {
+               self.is_usable() && !self.channel_ready_event_emitted
+       }
 
-                       pending_monitor_updates: Vec::new(),
-               })
+       // Remembers that we already emitted a `ChannelReady` event.
+       pub(crate) fn set_channel_ready_event_emitted(&mut self) {
+               self.channel_ready_event_emitted = true;
        }
 
-       fn check_remote_fee<F: Deref, L: Deref>(fee_estimator: &LowerBoundedFeeEstimator<F>,
-               feerate_per_kw: u32, cur_feerate_per_kw: Option<u32>, logger: &L)
-               -> Result<(), ChannelError> where F::Target: FeeEstimator, L::Target: Logger,
-       {
-               // We only bound the fee updates on the upper side to prevent completely absurd feerates,
-               // always accepting up to 25 sat/vByte or 10x our fee estimator's "High Priority" fee.
-               // We generally don't care too much if they set the feerate to something very high, but it
-               // could result in the channel being useless due to everything being dust.
-               let upper_limit = cmp::max(250 * 25,
-                       fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::HighPriority) as u64 * 10);
-               if feerate_per_kw as u64 > upper_limit {
-                       return Err(ChannelError::Close(format!("Peer's feerate much too high. Actual: {}. Our expected upper limit: {}", feerate_per_kw, upper_limit)));
+       /// Tracks the number of ticks elapsed since the previous [`ChannelConfig`] was updated. Once
+       /// [`EXPIRE_PREV_CONFIG_TICKS`] is reached, the previous config is considered expired and will
+       /// no longer be considered when forwarding HTLCs.
+       pub fn maybe_expire_prev_config(&mut self) {
+               if self.prev_config.is_none() {
+                       return;
                }
-               let lower_limit = fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Background);
-               // Some fee estimators round up to the next full sat/vbyte (ie 250 sats per kw), causing
-               // occasional issues with feerate disagreements between an initiator that wants a feerate
-               // of 1.1 sat/vbyte and a receiver that wants 1.1 rounded up to 2. Thus, we always add 250
-               // sat/kw before the comparison here.
-               if feerate_per_kw + 250 < lower_limit {
-                       if let Some(cur_feerate) = cur_feerate_per_kw {
-                               if feerate_per_kw > cur_feerate {
-                                       log_warn!(logger,
-                                               "Accepting feerate that may prevent us from closing this channel because it's higher than what we have now. Had {} s/kW, now {} s/kW.",
-                                               cur_feerate, feerate_per_kw);
-                                       return Ok(());
-                               }
-                       }
-                       return Err(ChannelError::Close(format!("Peer's feerate much too low. Actual: {}. Our expected lower limit: {} (- 250)", feerate_per_kw, lower_limit)));
+               let prev_config = self.prev_config.as_mut().unwrap();
+               prev_config.1 += 1;
+               if prev_config.1 == EXPIRE_PREV_CONFIG_TICKS {
+                       self.prev_config = None;
                }
-               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<ES: Deref, SP: Deref, F: Deref, L: Deref>(
-               fee_estimator: &LowerBoundedFeeEstimator<F>, entropy_source: &ES, signer_provider: &SP,
-               counterparty_node_id: PublicKey, our_supported_features: &ChannelTypeFeatures,
-               their_features: &InitFeatures, msg: &msgs::OpenChannel, user_id: u128, config: &UserConfig,
-               current_chain_height: u32, logger: &L, outbound_scid_alias: u64
-       ) -> Result<Channel<Signer>, ChannelError>
-               where ES::Target: EntropySource,
-                         SP::Target: SignerProvider<Signer = Signer>,
-                         F::Target: FeeEstimator,
-                         L::Target: Logger,
-       {
-               let announced_channel = if (msg.channel_flags & 1) == 1 { true } else { false };
+       /// Returns the current [`ChannelConfig`] applied to the channel.
+       pub fn config(&self) -> ChannelConfig {
+               self.config.options
+       }
 
-               // First check the channel type is known, failing before we do anything else if we don't
-               // support this channel type.
-               let channel_type = if let Some(channel_type) = &msg.channel_type {
-                       if channel_type.supports_any_optional_bits() {
-                               return Err(ChannelError::Close("Channel Type field contained optional bits - this is not allowed".to_owned()));
-                       }
-
-                       // We only support the channel types defined by the `ChannelManager` in
-                       // `provided_channel_type_features`. The channel type must always support
-                       // `static_remote_key`.
-                       if !channel_type.requires_static_remote_key() {
-                               return Err(ChannelError::Close("Channel Type was not understood - we require static remote key".to_owned()));
-                       }
-                       // Make sure we support all of the features behind the channel type.
-                       if !channel_type.is_subset(our_supported_features) {
-                               return Err(ChannelError::Close("Channel Type contains unsupported features".to_owned()));
-                       }
-                       if channel_type.requires_scid_privacy() && announced_channel {
-                               return Err(ChannelError::Close("SCID Alias/Privacy Channel Type cannot be set on a public channel".to_owned()));
-                       }
-                       channel_type.clone()
-               } else {
-                       let channel_type = ChannelTypeFeatures::from_init(&their_features);
-                       if channel_type != ChannelTypeFeatures::only_static_remote_key() {
-                               return Err(ChannelError::Close("Only static_remote_key is supported for non-negotiated channel types".to_owned()));
-                       }
-                       channel_type
-               };
-               let opt_anchors = channel_type.supports_anchors_zero_fee_htlc_tx();
-
-               let channel_keys_id = signer_provider.generate_channel_keys_id(true, msg.funding_satoshis, user_id);
-               let holder_signer = signer_provider.derive_channel_signer(msg.funding_satoshis, channel_keys_id);
-               let pubkeys = holder_signer.pubkeys().clone();
-               let counterparty_pubkeys = ChannelPublicKeys {
-                       funding_pubkey: msg.funding_pubkey,
-                       revocation_basepoint: msg.revocation_basepoint,
-                       payment_point: msg.payment_point,
-                       delayed_payment_basepoint: msg.delayed_payment_basepoint,
-                       htlc_basepoint: msg.htlc_basepoint
-               };
-
-               if config.channel_handshake_config.our_to_self_delay < BREAKDOWN_TIMEOUT {
-                       return Err(ChannelError::Close(format!("Configured with an unreasonable our_to_self_delay ({}) putting user funds at risks. It must be greater than {}", config.channel_handshake_config.our_to_self_delay, BREAKDOWN_TIMEOUT)));
-               }
-
-               // Check sanity of message fields:
-               if msg.funding_satoshis > config.channel_handshake_limits.max_funding_satoshis {
-                       return Err(ChannelError::Close(format!("Per our config, funding must be at most {}. It was {}", config.channel_handshake_limits.max_funding_satoshis, msg.funding_satoshis)));
-               }
-               if msg.funding_satoshis >= TOTAL_BITCOIN_SUPPLY_SATOSHIS {
-                       return Err(ChannelError::Close(format!("Funding must be smaller than the total bitcoin supply. It was {}", msg.funding_satoshis)));
-               }
-               if msg.channel_reserve_satoshis > msg.funding_satoshis {
-                       return Err(ChannelError::Close(format!("Bogus channel_reserve_satoshis ({}). Must be not greater than funding_satoshis: {}", msg.channel_reserve_satoshis, msg.funding_satoshis)));
-               }
-               let full_channel_value_msat = (msg.funding_satoshis - msg.channel_reserve_satoshis) * 1000;
-               if msg.push_msat > full_channel_value_msat {
-                       return Err(ChannelError::Close(format!("push_msat {} was larger than channel amount minus reserve ({})", msg.push_msat, full_channel_value_msat)));
-               }
-               if msg.dust_limit_satoshis > msg.funding_satoshis {
-                       return Err(ChannelError::Close(format!("dust_limit_satoshis {} was larger than funding_satoshis {}. Peer never wants payout outputs?", msg.dust_limit_satoshis, msg.funding_satoshis)));
-               }
-               if msg.htlc_minimum_msat >= full_channel_value_msat {
-                       return Err(ChannelError::Close(format!("Minimum htlc value ({}) was larger than full channel value ({})", msg.htlc_minimum_msat, full_channel_value_msat)));
-               }
-               Channel::<Signer>::check_remote_fee(fee_estimator, msg.feerate_per_kw, None, logger)?;
-
-               let max_counterparty_selected_contest_delay = u16::min(config.channel_handshake_limits.their_to_self_delay, MAX_LOCAL_BREAKDOWN_TIMEOUT);
-               if msg.to_self_delay > max_counterparty_selected_contest_delay {
-                       return Err(ChannelError::Close(format!("They wanted our payments to be delayed by a needlessly long period. Upper limit: {}. Actual: {}", max_counterparty_selected_contest_delay, msg.to_self_delay)));
-               }
-               if msg.max_accepted_htlcs < 1 {
-                       return Err(ChannelError::Close("0 max_accepted_htlcs makes for a useless channel".to_owned()));
-               }
-               if msg.max_accepted_htlcs > MAX_HTLCS {
-                       return Err(ChannelError::Close(format!("max_accepted_htlcs was {}. It must not be larger than {}", msg.max_accepted_htlcs, MAX_HTLCS)));
-               }
-
-               // Now check against optional parameters as set by config...
-               if msg.funding_satoshis < config.channel_handshake_limits.min_funding_satoshis {
-                       return Err(ChannelError::Close(format!("Funding satoshis ({}) is less than the user specified limit ({})", msg.funding_satoshis, config.channel_handshake_limits.min_funding_satoshis)));
-               }
-               if msg.htlc_minimum_msat > config.channel_handshake_limits.max_htlc_minimum_msat {
-                       return Err(ChannelError::Close(format!("htlc_minimum_msat ({}) is higher than the user specified limit ({})", msg.htlc_minimum_msat,  config.channel_handshake_limits.max_htlc_minimum_msat)));
-               }
-               if msg.max_htlc_value_in_flight_msat < config.channel_handshake_limits.min_max_htlc_value_in_flight_msat {
-                       return Err(ChannelError::Close(format!("max_htlc_value_in_flight_msat ({}) is less than the user specified limit ({})", msg.max_htlc_value_in_flight_msat, config.channel_handshake_limits.min_max_htlc_value_in_flight_msat)));
-               }
-               if msg.channel_reserve_satoshis > config.channel_handshake_limits.max_channel_reserve_satoshis {
-                       return Err(ChannelError::Close(format!("channel_reserve_satoshis ({}) is higher than the user specified limit ({})", msg.channel_reserve_satoshis, config.channel_handshake_limits.max_channel_reserve_satoshis)));
-               }
-               if msg.max_accepted_htlcs < config.channel_handshake_limits.min_max_accepted_htlcs {
-                       return Err(ChannelError::Close(format!("max_accepted_htlcs ({}) is less than the user specified limit ({})", msg.max_accepted_htlcs, config.channel_handshake_limits.min_max_accepted_htlcs)));
-               }
-               if msg.dust_limit_satoshis < MIN_CHAN_DUST_LIMIT_SATOSHIS {
-                       return Err(ChannelError::Close(format!("dust_limit_satoshis ({}) is less than the implementation limit ({})", msg.dust_limit_satoshis, MIN_CHAN_DUST_LIMIT_SATOSHIS)));
-               }
-               if msg.dust_limit_satoshis >  MAX_CHAN_DUST_LIMIT_SATOSHIS {
-                       return Err(ChannelError::Close(format!("dust_limit_satoshis ({}) is greater than the implementation limit ({})", msg.dust_limit_satoshis, MAX_CHAN_DUST_LIMIT_SATOSHIS)));
-               }
-
-               // Convert things into internal flags and prep our state:
-
-               if config.channel_handshake_limits.force_announced_channel_preference {
-                       if config.channel_handshake_config.announced_channel != announced_channel {
-                               return Err(ChannelError::Close("Peer tried to open channel but their announcement preference is different from ours".to_owned()));
-                       }
-               }
-
-               let holder_selected_channel_reserve_satoshis = Channel::<Signer>::get_holder_selected_channel_reserve_satoshis(msg.funding_satoshis, config);
-               if holder_selected_channel_reserve_satoshis < MIN_CHAN_DUST_LIMIT_SATOSHIS {
-                       // Protocol level safety check in place, although it should never happen because
-                       // of `MIN_THEIR_CHAN_RESERVE_SATOSHIS`
-                       return Err(ChannelError::Close(format!("Suitable channel reserve not found. remote_channel_reserve was ({}). dust_limit_satoshis is ({}).", holder_selected_channel_reserve_satoshis, MIN_CHAN_DUST_LIMIT_SATOSHIS)));
-               }
-               if holder_selected_channel_reserve_satoshis * 1000 >= full_channel_value_msat {
-                       return Err(ChannelError::Close(format!("Suitable channel reserve not found. remote_channel_reserve was ({})msats. Channel value is ({} - {})msats.", holder_selected_channel_reserve_satoshis * 1000, full_channel_value_msat, msg.push_msat)));
-               }
-               if msg.channel_reserve_satoshis < MIN_CHAN_DUST_LIMIT_SATOSHIS {
-                       log_debug!(logger, "channel_reserve_satoshis ({}) is smaller than our dust limit ({}). We can broadcast stale states without any risk, implying this channel is very insecure for our counterparty.",
-                               msg.channel_reserve_satoshis, MIN_CHAN_DUST_LIMIT_SATOSHIS);
-               }
-               if holder_selected_channel_reserve_satoshis < msg.dust_limit_satoshis {
-                       return Err(ChannelError::Close(format!("Dust limit ({}) too high for the channel reserve we require the remote to keep ({})", msg.dust_limit_satoshis, holder_selected_channel_reserve_satoshis)));
-               }
-
-               // check if the funder's amount for the initial commitment tx is sufficient
-               // for full fee payment plus a few HTLCs to ensure the channel will be useful.
-               let funders_amount_msat = msg.funding_satoshis * 1000 - msg.push_msat;
-               let commitment_tx_fee = Self::commit_tx_fee_msat(msg.feerate_per_kw, MIN_AFFORDABLE_HTLC_COUNT, opt_anchors) / 1000;
-               if funders_amount_msat / 1000 < commitment_tx_fee {
-                       return Err(ChannelError::Close(format!("Funding amount ({} sats) can't even pay fee for initial commitment transaction fee of {} sats.", funders_amount_msat / 1000, commitment_tx_fee)));
-               }
-
-               let to_remote_satoshis = funders_amount_msat / 1000 - commitment_tx_fee;
-               // While it's reasonable for us to not meet the channel reserve initially (if they don't
-               // want to push much to us), our counterparty should always have more than our reserve.
-               if to_remote_satoshis < holder_selected_channel_reserve_satoshis {
-                       return Err(ChannelError::Close("Insufficient funding amount for initial reserve".to_owned()));
-               }
-
-               let counterparty_shutdown_scriptpubkey = if their_features.supports_upfront_shutdown_script() {
-                       match &msg.shutdown_scriptpubkey {
-                               &Some(ref script) => {
-                                       // Peer is signaling upfront_shutdown and has opt-out with a 0-length script. We don't enforce anything
-                                       if script.len() == 0 {
-                                               None
-                                       } else {
-                                               if !script::is_bolt2_compliant(&script, their_features) {
-                                                       return Err(ChannelError::Close(format!("Peer is signaling upfront_shutdown but has provided an unacceptable scriptpubkey format: {}", script)))
-                                               }
-                                               Some(script.clone())
-                                       }
-                               },
-                               // 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
-                               &None => {
-                                       return Err(ChannelError::Close("Peer is signaling upfront_shutdown but we don't get any script. Use 0-length script to opt-out".to_owned()));
-                               }
-                       }
-               } else { None };
-
-               let shutdown_scriptpubkey = if config.channel_handshake_config.commit_upfront_shutdown_pubkey {
-                       match signer_provider.get_shutdown_scriptpubkey() {
-                               Ok(scriptpubkey) => Some(scriptpubkey),
-                               Err(_) => return Err(ChannelError::Close("Failed to get upfront shutdown scriptpubkey".to_owned())),
-                       }
-               } else { None };
-
-               if let Some(shutdown_scriptpubkey) = &shutdown_scriptpubkey {
-                       if !shutdown_scriptpubkey.is_compatible(&their_features) {
-                               return Err(ChannelError::Close(format!("Provided a scriptpubkey format not accepted by peer: {}", shutdown_scriptpubkey)));
-                       }
+       /// Updates the channel's config. A bool is returned indicating whether the config update
+       /// applied resulted in a new ChannelUpdate message.
+       pub fn update_config(&mut self, config: &ChannelConfig) -> bool {
+               let did_channel_update =
+                       self.config.options.forwarding_fee_proportional_millionths != config.forwarding_fee_proportional_millionths ||
+                       self.config.options.forwarding_fee_base_msat != config.forwarding_fee_base_msat ||
+                       self.config.options.cltv_expiry_delta != config.cltv_expiry_delta;
+               if did_channel_update {
+                       self.prev_config = Some((self.config.options, 0));
+                       // Update the counter, which backs the ChannelUpdate timestamp, to allow the relay
+                       // policy change to propagate throughout the network.
+                       self.update_time_counter += 1;
                }
+               self.config.options = *config;
+               did_channel_update
+       }
 
-               let destination_script = match signer_provider.get_destination_script() {
-                       Ok(script) => script,
-                       Err(_) => return Err(ChannelError::Close("Failed to get destination script".to_owned())),
-               };
-
-               let mut secp_ctx = Secp256k1::new();
-               secp_ctx.seeded_randomize(&entropy_source.get_secure_random_bytes());
-
-               let chan = Channel {
-                       user_id,
-
-                       config: LegacyChannelConfig {
-                               options: config.channel_config.clone(),
-                               announced_channel,
-                               commit_upfront_shutdown_pubkey: config.channel_handshake_config.commit_upfront_shutdown_pubkey,
-                       },
-
-                       prev_config: None,
-
-                       inbound_handshake_limits_override: None,
-
-                       channel_id: msg.temporary_channel_id,
-                       temporary_channel_id: Some(msg.temporary_channel_id),
-                       channel_state: (ChannelState::OurInitSent as u32) | (ChannelState::TheirInitSent as u32),
-                       announcement_sigs_state: AnnouncementSigsState::NotSent,
-                       secp_ctx,
-
-                       latest_monitor_update_id: 0,
-
-                       holder_signer,
-                       shutdown_scriptpubkey,
-                       destination_script,
-
-                       cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
-                       cur_counterparty_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_holder_htlc_id: 0,
-                       next_counterparty_htlc_id: 0,
-                       update_time_counter: 1,
-
-                       resend_order: RAACommitmentOrder::CommitmentFirst,
-
-                       monitor_pending_channel_ready: false,
-                       monitor_pending_revoke_and_ack: false,
-                       monitor_pending_commitment_signed: false,
-                       monitor_pending_forwards: Vec::new(),
-                       monitor_pending_failures: Vec::new(),
-                       monitor_pending_finalized_fulfills: Vec::new(),
-
-                       #[cfg(debug_assertions)]
-                       holder_max_commitment_tx_output: Mutex::new((msg.push_msat, msg.funding_satoshis * 1000 - msg.push_msat)),
-                       #[cfg(debug_assertions)]
-                       counterparty_max_commitment_tx_output: Mutex::new((msg.push_msat, msg.funding_satoshis * 1000 - msg.push_msat)),
-
-                       last_sent_closing_fee: None,
-                       pending_counterparty_closing_signed: None,
-                       closing_fee_limits: None,
-                       target_closing_feerate_sats_per_kw: None,
-
-                       inbound_awaiting_accept: true,
-
-                       funding_tx_confirmed_in: None,
-                       funding_tx_confirmation_height: 0,
-                       short_channel_id: None,
-                       channel_creation_height: current_chain_height,
-
-                       feerate_per_kw: msg.feerate_per_kw,
-                       channel_value_satoshis: msg.funding_satoshis,
-                       counterparty_dust_limit_satoshis: msg.dust_limit_satoshis,
-                       holder_dust_limit_satoshis: MIN_CHAN_DUST_LIMIT_SATOSHIS,
-                       counterparty_max_htlc_value_in_flight_msat: cmp::min(msg.max_htlc_value_in_flight_msat, msg.funding_satoshis * 1000),
-                       holder_max_htlc_value_in_flight_msat: Self::get_holder_max_htlc_value_in_flight_msat(msg.funding_satoshis, &config.channel_handshake_config),
-                       counterparty_selected_channel_reserve_satoshis: Some(msg.channel_reserve_satoshis),
-                       holder_selected_channel_reserve_satoshis,
-                       counterparty_htlc_minimum_msat: msg.htlc_minimum_msat,
-                       holder_htlc_minimum_msat: if config.channel_handshake_config.our_htlc_minimum_msat == 0 { 1 } else { config.channel_handshake_config.our_htlc_minimum_msat },
-                       counterparty_max_accepted_htlcs: msg.max_accepted_htlcs,
-                       holder_max_accepted_htlcs: cmp::min(config.channel_handshake_config.our_max_accepted_htlcs, MAX_HTLCS),
-                       minimum_depth: Some(cmp::max(config.channel_handshake_config.minimum_depth, 1)),
-
-                       counterparty_forwarding_info: None,
-
-                       channel_transaction_parameters: ChannelTransactionParameters {
-                               holder_pubkeys: pubkeys,
-                               holder_selected_contest_delay: config.channel_handshake_config.our_to_self_delay,
-                               is_outbound_from_holder: false,
-                               counterparty_parameters: Some(CounterpartyChannelTransactionParameters {
-                                       selected_contest_delay: msg.to_self_delay,
-                                       pubkeys: counterparty_pubkeys,
-                               }),
-                               funding_outpoint: None,
-                               opt_anchors: if opt_anchors { Some(()) } else { None },
-                               opt_non_zero_fee_anchors: None
-                       },
-                       funding_transaction: None,
-
-                       counterparty_cur_commitment_point: Some(msg.first_per_commitment_point),
-                       counterparty_prev_commitment_point: None,
-                       counterparty_node_id,
-
-                       counterparty_shutdown_scriptpubkey,
-
-                       commitment_secrets: CounterpartyCommitmentSecrets::new(),
-
-                       channel_update_status: ChannelUpdateStatus::Enabled,
-                       closing_signed_in_flight: false,
-
-                       announcement_sigs: None,
-
-                       #[cfg(any(test, fuzzing))]
-                       next_local_commitment_tx_fee_info_cached: Mutex::new(None),
-                       #[cfg(any(test, fuzzing))]
-                       next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
-
-                       workaround_lnd_bug_4006: None,
-
-                       latest_inbound_scid_alias: None,
-                       outbound_scid_alias,
-
-                       channel_pending_event_emitted: false,
-                       channel_ready_event_emitted: false,
-
-                       #[cfg(any(test, fuzzing))]
-                       historical_inbound_htlc_fulfills: HashSet::new(),
-
-                       channel_type,
-                       channel_keys_id,
-
-                       pending_monitor_updates: Vec::new(),
-               };
-
-               Ok(chan)
+       /// Returns true if funding_created was sent/received.
+       pub fn is_funding_initiated(&self) -> bool {
+               self.channel_state >= ChannelState::FundingSent as u32
        }
 
        /// Transaction nomenclature is somewhat confusing here as there are many different cases - a
@@ -1568,10 +1232,10 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                        ($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);
-                                       let htlc_tx_fee = if self.opt_anchors() {
+                                       let htlc_tx_fee = if self.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
                                                0
                                        } else {
-                                               feerate_per_kw as u64 * htlc_timeout_tx_weight(false) / 1000
+                                               feerate_per_kw as u64 * htlc_timeout_tx_weight(self.get_channel_type()) / 1000
                                        };
                                        if $htlc.amount_msat / 1000 >= broadcaster_dust_limit_satoshis + htlc_tx_fee {
                                                log_trace!(logger, "   ...including {} {} HTLC {} (hash {}) with value {}", if $outbound { "outbound" } else { "inbound" }, $state_name, $htlc.htlc_id, log_bytes!($htlc.payment_hash.0), $htlc.amount_msat);
@@ -1582,10 +1246,10 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                                        }
                                } else {
                                        let htlc_in_tx = get_htlc_in_commitment!($htlc, false);
-                                       let htlc_tx_fee = if self.opt_anchors() {
+                                       let htlc_tx_fee = if self.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
                                                0
                                        } else {
-                                               feerate_per_kw as u64 * htlc_success_tx_weight(false) / 1000
+                                               feerate_per_kw as u64 * htlc_success_tx_weight(self.get_channel_type()) / 1000
                                        };
                                        if $htlc.amount_msat / 1000 >= broadcaster_dust_limit_satoshis + htlc_tx_fee {
                                                log_trace!(logger, "   ...including {} {} HTLC {} (hash {}) with value {}", if $outbound { "outbound" } else { "inbound" }, $state_name, $htlc.htlc_id, log_bytes!($htlc.payment_hash.0), $htlc.amount_msat);
@@ -1690,8 +1354,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                        broadcaster_max_commitment_tx_output.1 = cmp::max(broadcaster_max_commitment_tx_output.1, value_to_remote_msat as u64);
                }
 
-               let total_fee_sat = Channel::<Signer>::commit_tx_fee_sat(feerate_per_kw, included_non_dust_htlcs.len(), self.channel_transaction_parameters.opt_anchors.is_some());
-               let anchors_val = if self.channel_transaction_parameters.opt_anchors.is_some() { ANCHOR_OUTPUT_VALUE_SATOSHI * 2 } else { 0 } as i64;
+               let total_fee_sat = commit_tx_fee_sat(feerate_per_kw, included_non_dust_htlcs.len(), &self.channel_transaction_parameters.channel_type_features);
+               let anchors_val = if self.channel_transaction_parameters.channel_type_features.supports_anchors_zero_fee_htlc_tx() { ANCHOR_OUTPUT_VALUE_SATOSHI * 2 } else { 0 } as i64;
                let (value_to_self, value_to_remote) = if self.is_outbound() {
                        (value_to_self_msat / 1000 - anchors_val - total_fee_sat as i64, value_to_remote_msat / 1000)
                } else {
@@ -1726,7 +1390,6 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                let tx = CommitmentTransaction::new_with_auxiliary_htlc_data(commitment_number,
                                                                             value_to_a as u64,
                                                                             value_to_b as u64,
-                                                                            self.channel_transaction_parameters.opt_anchors.is_some(),
                                                                             funding_pubkey_a,
                                                                             funding_pubkey_b,
                                                                             keys.clone(),
@@ -1756,1258 +1419,1291 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
        }
 
        #[inline]
-       fn get_closing_scriptpubkey(&self) -> Script {
-               // The shutdown scriptpubkey is set on channel opening when option_upfront_shutdown_script
-               // is signaled. Otherwise, it is set when sending a shutdown message. Calling this method
-               // outside of those situations will fail.
-               self.shutdown_scriptpubkey.clone().unwrap().into_inner()
-       }
+       /// 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 broadcastership of (ie a "local" transaction)
+       /// TODO Some magic rust shit to compile-time check this?
+       fn build_holder_transaction_keys(&self, commitment_number: u64) -> TxCreationKeys {
+               let per_commitment_point = self.holder_signer.get_per_commitment_point(commitment_number, &self.secp_ctx);
+               let delayed_payment_base = &self.get_holder_pubkeys().delayed_payment_basepoint;
+               let htlc_basepoint = &self.get_holder_pubkeys().htlc_basepoint;
+               let counterparty_pubkeys = self.get_counterparty_pubkeys();
 
-       #[inline]
-       fn get_closing_transaction_weight(&self, a_scriptpubkey: Option<&Script>, b_scriptpubkey: Option<&Script>) -> u64 {
-               let mut ret =
-               (4 +                                           // version
-                1 +                                           // input count
-                36 +                                          // prevout
-                1 +                                           // script length (0)
-                4 +                                           // sequence
-                1 +                                           // output count
-                4                                             // lock time
-                )*4 +                                         // * 4 for non-witness parts
-               2 +                                            // witness marker and flag
-               1 +                                            // witness element count
-               4 +                                            // 4 element lengths (2 sigs, multisig dummy, and witness script)
-               self.get_funding_redeemscript().len() as u64 + // funding witness script
-               2*(1 + 71);                                    // two signatures + sighash type flags
-               if let Some(spk) = a_scriptpubkey {
-                       ret += ((8+1) +                            // output values and script length
-                               spk.len() as u64) * 4;                 // scriptpubkey and witness multiplier
-               }
-               if let Some(spk) = b_scriptpubkey {
-                       ret += ((8+1) +                            // output values and script length
-                               spk.len() as u64) * 4;                 // scriptpubkey and witness multiplier
-               }
-               ret
+               TxCreationKeys::derive_new(&self.secp_ctx, &per_commitment_point, delayed_payment_base, htlc_basepoint, &counterparty_pubkeys.revocation_basepoint, &counterparty_pubkeys.htlc_basepoint)
        }
 
        #[inline]
-       fn build_closing_transaction(&self, proposed_total_fee_satoshis: u64, skip_remote_output: bool) -> (ClosingTransaction, u64) {
-               assert!(self.pending_inbound_htlcs.is_empty());
-               assert!(self.pending_outbound_htlcs.is_empty());
-               assert!(self.pending_update_fee.is_none());
+       /// 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) -> TxCreationKeys {
+               //TODO: Ensure that the payment_key derived here ends up in the library users' wallet as we
+               //may see payments to it!
+               let revocation_basepoint = &self.get_holder_pubkeys().revocation_basepoint;
+               let htlc_basepoint = &self.get_holder_pubkeys().htlc_basepoint;
+               let counterparty_pubkeys = self.get_counterparty_pubkeys();
 
-               let mut total_fee_satoshis = proposed_total_fee_satoshis;
-               let mut value_to_holder: i64 = (self.value_to_self_msat as i64) / 1000 - if self.is_outbound() { total_fee_satoshis as i64 } else { 0 };
-               let mut value_to_counterparty: i64 = ((self.channel_value_satoshis * 1000 - self.value_to_self_msat) as i64 / 1000) - if self.is_outbound() { 0 } else { total_fee_satoshis as i64 };
+               TxCreationKeys::derive_new(&self.secp_ctx, &self.counterparty_cur_commitment_point.unwrap(), &counterparty_pubkeys.delayed_payment_basepoint, &counterparty_pubkeys.htlc_basepoint, revocation_basepoint, htlc_basepoint)
+       }
 
-               if value_to_holder < 0 {
-                       assert!(self.is_outbound());
-                       total_fee_satoshis += (-value_to_holder) as u64;
-               } else if value_to_counterparty < 0 {
-                       assert!(!self.is_outbound());
-                       total_fee_satoshis += (-value_to_counterparty) as u64;
-               }
-
-               if skip_remote_output || value_to_counterparty as u64 <= self.holder_dust_limit_satoshis {
-                       value_to_counterparty = 0;
-               }
-
-               if value_to_holder as u64 <= self.holder_dust_limit_satoshis {
-                       value_to_holder = 0;
-               }
-
-               assert!(self.shutdown_scriptpubkey.is_some());
-               let holder_shutdown_script = self.get_closing_scriptpubkey();
-               let counterparty_shutdown_script = self.counterparty_shutdown_scriptpubkey.clone().unwrap();
-               let funding_outpoint = self.funding_outpoint().into_bitcoin_outpoint();
+       /// 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/InboundV1Channel::new
+       pub fn get_funding_redeemscript(&self) -> Script {
+               make_funding_redeemscript(&self.get_holder_pubkeys().funding_pubkey, self.counterparty_funding_pubkey())
+       }
 
-               let closing_transaction = ClosingTransaction::new(value_to_holder as u64, value_to_counterparty as u64, holder_shutdown_script, counterparty_shutdown_script, funding_outpoint);
-               (closing_transaction, total_fee_satoshis)
+       fn counterparty_funding_pubkey(&self) -> &PublicKey {
+               &self.get_counterparty_pubkeys().funding_pubkey
        }
 
-       fn funding_outpoint(&self) -> OutPoint {
-               self.channel_transaction_parameters.funding_outpoint.unwrap()
+       pub fn get_feerate_sat_per_1000_weight(&self) -> u32 {
+               self.feerate_per_kw
        }
 
-       #[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 broadcastership of (ie a "local" transaction)
-       /// TODO Some magic rust shit to compile-time check this?
-       fn build_holder_transaction_keys(&self, commitment_number: u64) -> TxCreationKeys {
-               let per_commitment_point = self.holder_signer.get_per_commitment_point(commitment_number, &self.secp_ctx);
-               let delayed_payment_base = &self.get_holder_pubkeys().delayed_payment_basepoint;
-               let htlc_basepoint = &self.get_holder_pubkeys().htlc_basepoint;
-               let counterparty_pubkeys = self.get_counterparty_pubkeys();
+       pub fn get_dust_buffer_feerate(&self, outbound_feerate_update: Option<u32>) -> u32 {
+               // When calculating our exposure to dust HTLCs, we assume that the channel feerate
+               // may, at any point, increase by at least 10 sat/vB (i.e 2530 sat/kWU) or 25%,
+               // whichever is higher. This ensures that we aren't suddenly exposed to significantly
+               // more dust balance if the feerate increases when we have several HTLCs pending
+               // which are near the dust limit.
+               let mut feerate_per_kw = self.feerate_per_kw;
+               // If there's a pending update fee, use it to ensure we aren't under-estimating
+               // potential feerate updates coming soon.
+               if let Some((feerate, _)) = self.pending_update_fee {
+                       feerate_per_kw = cmp::max(feerate_per_kw, feerate);
+               }
+               if let Some(feerate) = outbound_feerate_update {
+                       feerate_per_kw = cmp::max(feerate_per_kw, feerate);
+               }
+               cmp::max(2530, feerate_per_kw * 1250 / 1000)
+       }
 
-               TxCreationKeys::derive_new(&self.secp_ctx, &per_commitment_point, delayed_payment_base, htlc_basepoint, &counterparty_pubkeys.revocation_basepoint, &counterparty_pubkeys.htlc_basepoint)
+       /// Get forwarding information for the counterparty.
+       pub fn counterparty_forwarding_info(&self) -> Option<CounterpartyForwardingInfo> {
+               self.counterparty_forwarding_info.clone()
        }
 
-       #[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) -> TxCreationKeys {
-               //TODO: Ensure that the payment_key derived here ends up in the library users' wallet as we
-               //may see payments to it!
-               let revocation_basepoint = &self.get_holder_pubkeys().revocation_basepoint;
-               let htlc_basepoint = &self.get_holder_pubkeys().htlc_basepoint;
-               let counterparty_pubkeys = self.get_counterparty_pubkeys();
+       /// Returns a HTLCStats about inbound pending htlcs
+       fn get_inbound_pending_htlc_stats(&self, outbound_feerate_update: Option<u32>) -> HTLCStats {
+               let context = self;
+               let mut stats = HTLCStats {
+                       pending_htlcs: context.pending_inbound_htlcs.len() as u32,
+                       pending_htlcs_value_msat: 0,
+                       on_counterparty_tx_dust_exposure_msat: 0,
+                       on_holder_tx_dust_exposure_msat: 0,
+                       holding_cell_msat: 0,
+                       on_holder_tx_holding_cell_htlcs_count: 0,
+               };
 
-               TxCreationKeys::derive_new(&self.secp_ctx, &self.counterparty_cur_commitment_point.unwrap(), &counterparty_pubkeys.delayed_payment_basepoint, &counterparty_pubkeys.htlc_basepoint, revocation_basepoint, htlc_basepoint)
+               let (htlc_timeout_dust_limit, htlc_success_dust_limit) = if context.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
+                       (0, 0)
+               } else {
+                       let dust_buffer_feerate = context.get_dust_buffer_feerate(outbound_feerate_update) as u64;
+                       (dust_buffer_feerate * htlc_timeout_tx_weight(context.get_channel_type()) / 1000,
+                               dust_buffer_feerate * htlc_success_tx_weight(context.get_channel_type()) / 1000)
+               };
+               let counterparty_dust_limit_timeout_sat = htlc_timeout_dust_limit + context.counterparty_dust_limit_satoshis;
+               let holder_dust_limit_success_sat = htlc_success_dust_limit + context.holder_dust_limit_satoshis;
+               for ref htlc in context.pending_inbound_htlcs.iter() {
+                       stats.pending_htlcs_value_msat += htlc.amount_msat;
+                       if htlc.amount_msat / 1000 < counterparty_dust_limit_timeout_sat {
+                               stats.on_counterparty_tx_dust_exposure_msat += htlc.amount_msat;
+                       }
+                       if htlc.amount_msat / 1000 < holder_dust_limit_success_sat {
+                               stats.on_holder_tx_dust_exposure_msat += htlc.amount_msat;
+                       }
+               }
+               stats
        }
 
-       /// 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 {
-               make_funding_redeemscript(&self.get_holder_pubkeys().funding_pubkey, self.counterparty_funding_pubkey())
-       }
+       /// Returns a HTLCStats about pending outbound htlcs, *including* pending adds in our holding cell.
+       fn get_outbound_pending_htlc_stats(&self, outbound_feerate_update: Option<u32>) -> HTLCStats {
+               let context = self;
+               let mut stats = HTLCStats {
+                       pending_htlcs: context.pending_outbound_htlcs.len() as u32,
+                       pending_htlcs_value_msat: 0,
+                       on_counterparty_tx_dust_exposure_msat: 0,
+                       on_holder_tx_dust_exposure_msat: 0,
+                       holding_cell_msat: 0,
+                       on_holder_tx_holding_cell_htlcs_count: 0,
+               };
 
-       /// Claims an HTLC while we're disconnected from a peer, dropping the [`ChannelMonitorUpdate`]
-       /// entirely.
-       ///
-       /// The [`ChannelMonitor`] for this channel MUST be updated out-of-band with the preimage
-       /// provided (i.e. without calling [`crate::chain::Watch::update_channel`]).
-       ///
-       /// The HTLC claim will end up in the holding cell (because the caller must ensure the peer is
-       /// disconnected).
-       pub fn claim_htlc_while_disconnected_dropping_mon_update<L: Deref>
-               (&mut self, htlc_id_arg: u64, payment_preimage_arg: PaymentPreimage, logger: &L)
-       where L::Target: Logger {
-               // Assert that we'll add the HTLC claim to the holding cell in `get_update_fulfill_htlc`
-               // (see equivalent if condition there).
-               assert!(self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32 | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress as u32) != 0);
-               let mon_update_id = self.latest_monitor_update_id; // Forget the ChannelMonitor update
-               let fulfill_resp = self.get_update_fulfill_htlc(htlc_id_arg, payment_preimage_arg, logger);
-               self.latest_monitor_update_id = mon_update_id;
-               if let UpdateFulfillFetch::NewClaim { msg, .. } = fulfill_resp {
-                       assert!(msg.is_none()); // The HTLC must have ended up in the holding cell.
+               let (htlc_timeout_dust_limit, htlc_success_dust_limit) = if context.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
+                       (0, 0)
+               } else {
+                       let dust_buffer_feerate = context.get_dust_buffer_feerate(outbound_feerate_update) as u64;
+                       (dust_buffer_feerate * htlc_timeout_tx_weight(context.get_channel_type()) / 1000,
+                               dust_buffer_feerate * htlc_success_tx_weight(context.get_channel_type()) / 1000)
+               };
+               let counterparty_dust_limit_success_sat = htlc_success_dust_limit + context.counterparty_dust_limit_satoshis;
+               let holder_dust_limit_timeout_sat = htlc_timeout_dust_limit + context.holder_dust_limit_satoshis;
+               for ref htlc in context.pending_outbound_htlcs.iter() {
+                       stats.pending_htlcs_value_msat += htlc.amount_msat;
+                       if htlc.amount_msat / 1000 < counterparty_dust_limit_success_sat {
+                               stats.on_counterparty_tx_dust_exposure_msat += htlc.amount_msat;
+                       }
+                       if htlc.amount_msat / 1000 < holder_dust_limit_timeout_sat {
+                               stats.on_holder_tx_dust_exposure_msat += htlc.amount_msat;
+                       }
+               }
+
+               for update in context.holding_cell_htlc_updates.iter() {
+                       if let &HTLCUpdateAwaitingACK::AddHTLC { ref amount_msat, .. } = update {
+                               stats.pending_htlcs += 1;
+                               stats.pending_htlcs_value_msat += amount_msat;
+                               stats.holding_cell_msat += amount_msat;
+                               if *amount_msat / 1000 < counterparty_dust_limit_success_sat {
+                                       stats.on_counterparty_tx_dust_exposure_msat += amount_msat;
+                               }
+                               if *amount_msat / 1000 < holder_dust_limit_timeout_sat {
+                                       stats.on_holder_tx_dust_exposure_msat += amount_msat;
+                               } else {
+                                       stats.on_holder_tx_holding_cell_htlcs_count += 1;
+                               }
+                       }
                }
+               stats
        }
 
-       fn get_update_fulfill_htlc<L: Deref>(&mut self, htlc_id_arg: u64, payment_preimage_arg: PaymentPreimage, logger: &L) -> UpdateFulfillFetch where L::Target: Logger {
-               // Either ChannelReady 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::ChannelReady as u32)) != (ChannelState::ChannelReady as u32) {
-                       panic!("Was asked to fulfill an HTLC when channel was not in an operational state");
+       /// Get the available balances, see [`AvailableBalances`]'s fields for more info.
+       /// 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_available_balances<F: Deref>(&self, fee_estimator: &LowerBoundedFeeEstimator<F>)
+       -> AvailableBalances
+       where F::Target: FeeEstimator
+       {
+               let context = &self;
+               // Note that we have to handle overflow due to the above case.
+               let inbound_stats = context.get_inbound_pending_htlc_stats(None);
+               let outbound_stats = context.get_outbound_pending_htlc_stats(None);
+
+               let mut balance_msat = context.value_to_self_msat;
+               for ref htlc in context.pending_inbound_htlcs.iter() {
+                       if let InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::Fulfill(_)) = htlc.state {
+                               balance_msat += htlc.amount_msat;
+                       }
                }
-               assert_eq!(self.channel_state & ChannelState::ShutdownComplete as u32, 0);
+               balance_msat -= outbound_stats.pending_htlcs_value_msat;
 
-               let payment_hash_calc = PaymentHash(Sha256::hash(&payment_preimage_arg.0[..]).into_inner());
+               let outbound_capacity_msat = context.value_to_self_msat
+                               .saturating_sub(outbound_stats.pending_htlcs_value_msat)
+                               .saturating_sub(
+                                       context.counterparty_selected_channel_reserve_satoshis.unwrap_or(0) * 1000);
 
-               // 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 available_capacity_msat = outbound_capacity_msat;
 
-               let mut pending_idx = core::usize::MAX;
-               let mut htlc_value_msat = 0;
-               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!(logger, "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()));
-                                                       debug_assert!(false, "Tried to fulfill an HTLC that was already failed");
-                                               }
-                                               return UpdateFulfillFetch::DuplicateClaim {};
-                                       },
-                                       _ => {
-                                               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;
-                               htlc_value_msat = htlc.amount_msat;
-                               break;
+               if context.is_outbound() {
+                       // We should mind channel commit tx fee when computing how much of the available capacity
+                       // can be used in the next htlc. Mirrors the logic in send_htlc.
+                       //
+                       // The fee depends on whether the amount we will be sending is above dust or not,
+                       // and the answer will in turn change the amount itself â€” making it a circular
+                       // dependency.
+                       // This complicates the computation around dust-values, up to the one-htlc-value.
+                       let mut real_dust_limit_timeout_sat = context.holder_dust_limit_satoshis;
+                       if !context.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
+                               real_dust_limit_timeout_sat += context.feerate_per_kw as u64 * htlc_timeout_tx_weight(context.get_channel_type()) / 1000;
+                       }
+
+                       let htlc_above_dust = HTLCCandidate::new(real_dust_limit_timeout_sat * 1000, HTLCInitiator::LocalOffered);
+                       let max_reserved_commit_tx_fee_msat = FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE * context.next_local_commit_tx_fee_msat(htlc_above_dust, Some(()));
+                       let htlc_dust = HTLCCandidate::new(real_dust_limit_timeout_sat * 1000 - 1, HTLCInitiator::LocalOffered);
+                       let min_reserved_commit_tx_fee_msat = FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE * context.next_local_commit_tx_fee_msat(htlc_dust, Some(()));
+
+                       // We will first subtract the fee as if we were above-dust. Then, if the resulting
+                       // value ends up being below dust, we have this fee available again. In that case,
+                       // match the value to right-below-dust.
+                       let mut capacity_minus_commitment_fee_msat: i64 = (available_capacity_msat as i64) - (max_reserved_commit_tx_fee_msat as i64);
+                       if capacity_minus_commitment_fee_msat < (real_dust_limit_timeout_sat as i64) * 1000 {
+                               let one_htlc_difference_msat = max_reserved_commit_tx_fee_msat - min_reserved_commit_tx_fee_msat;
+                               debug_assert!(one_htlc_difference_msat != 0);
+                               capacity_minus_commitment_fee_msat += one_htlc_difference_msat as i64;
+                               capacity_minus_commitment_fee_msat = cmp::min(real_dust_limit_timeout_sat as i64 * 1000 - 1, capacity_minus_commitment_fee_msat);
+                               available_capacity_msat = cmp::max(0, cmp::min(capacity_minus_commitment_fee_msat, available_capacity_msat as i64)) as u64;
+                       } else {
+                               available_capacity_msat = capacity_minus_commitment_fee_msat as u64;
+                       }
+               } else {
+                       // If the channel is inbound (i.e. counterparty pays the fee), we need to make sure
+                       // sending a new HTLC won't reduce their balance below our reserve threshold.
+                       let mut real_dust_limit_success_sat = context.counterparty_dust_limit_satoshis;
+                       if !context.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
+                               real_dust_limit_success_sat += context.feerate_per_kw as u64 * htlc_success_tx_weight(context.get_channel_type()) / 1000;
+                       }
+
+                       let htlc_above_dust = HTLCCandidate::new(real_dust_limit_success_sat * 1000, HTLCInitiator::LocalOffered);
+                       let max_reserved_commit_tx_fee_msat = context.next_remote_commit_tx_fee_msat(htlc_above_dust, None);
+
+                       let holder_selected_chan_reserve_msat = context.holder_selected_channel_reserve_satoshis * 1000;
+                       let remote_balance_msat = (context.channel_value_satoshis * 1000 - context.value_to_self_msat)
+                               .saturating_sub(inbound_stats.pending_htlcs_value_msat);
+
+                       if remote_balance_msat < max_reserved_commit_tx_fee_msat + holder_selected_chan_reserve_msat {
+                               // If another HTLC's fee would reduce the remote's balance below the reserve limit
+                               // we've selected for them, we can only send dust HTLCs.
+                               available_capacity_msat = cmp::min(available_capacity_msat, real_dust_limit_success_sat * 1000 - 1);
                        }
-               }
-               if pending_idx == core::usize::MAX {
-                       #[cfg(any(test, fuzzing))]
-                       // If we failed to find an HTLC to fulfill, make sure it was previously fulfilled and
-                       // this is simply a duplicate claim, not previously failed and we lost funds.
-                       debug_assert!(self.historical_inbound_htlc_fulfills.contains(&htlc_id_arg));
-                       return UpdateFulfillFetch::DuplicateClaim {};
                }
 
-               // 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.latest_monitor_update_id += 1;
-               let monitor_update = ChannelMonitorUpdate {
-                       update_id: self.latest_monitor_update_id,
-                       updates: vec![ChannelMonitorUpdateStep::PaymentPreimage {
-                               payment_preimage: payment_preimage_arg.clone(),
-                       }],
+               let mut next_outbound_htlc_minimum_msat = context.counterparty_htlc_minimum_msat;
+
+               // If we get close to our maximum dust exposure, we end up in a situation where we can send
+               // between zero and the remaining dust exposure limit remaining OR above the dust limit.
+               // Because we cannot express this as a simple min/max, we prefer to tell the user they can
+               // send above the dust limit (as the router can always overpay to meet the dust limit).
+               let mut remaining_msat_below_dust_exposure_limit = None;
+               let mut dust_exposure_dust_limit_msat = 0;
+               let max_dust_htlc_exposure_msat = context.get_max_dust_htlc_exposure_msat(fee_estimator);
+
+               let (htlc_success_dust_limit, htlc_timeout_dust_limit) = if context.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
+                       (context.counterparty_dust_limit_satoshis, context.holder_dust_limit_satoshis)
+               } else {
+                       let dust_buffer_feerate = context.get_dust_buffer_feerate(None) as u64;
+                       (context.counterparty_dust_limit_satoshis + dust_buffer_feerate * htlc_success_tx_weight(context.get_channel_type()) / 1000,
+                        context.holder_dust_limit_satoshis       + dust_buffer_feerate * htlc_timeout_tx_weight(context.get_channel_type()) / 1000)
                };
+               let on_counterparty_dust_htlc_exposure_msat = inbound_stats.on_counterparty_tx_dust_exposure_msat + outbound_stats.on_counterparty_tx_dust_exposure_msat;
+               if on_counterparty_dust_htlc_exposure_msat as i64 + htlc_success_dust_limit as i64 * 1000 - 1 > max_dust_htlc_exposure_msat as i64 {
+                       remaining_msat_below_dust_exposure_limit =
+                               Some(max_dust_htlc_exposure_msat.saturating_sub(on_counterparty_dust_htlc_exposure_msat));
+                       dust_exposure_dust_limit_msat = cmp::max(dust_exposure_dust_limit_msat, htlc_success_dust_limit * 1000);
+               }
 
-               if (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32 | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress as u32)) != 0 {
-                       // Note that this condition is the same as the assertion in
-                       // `claim_htlc_while_disconnected_dropping_mon_update` and must match exactly -
-                       // `claim_htlc_while_disconnected_dropping_mon_update` would not work correctly if we
-                       // do not not get into this branch.
-                       for pending_update in self.holding_cell_htlc_updates.iter() {
-                               match pending_update {
-                                       &HTLCUpdateAwaitingACK::ClaimHTLC { htlc_id, .. } => {
-                                               if htlc_id_arg == htlc_id {
-                                                       // Make sure we don't leave latest_monitor_update_id incremented here:
-                                                       self.latest_monitor_update_id -= 1;
-                                                       #[cfg(any(test, fuzzing))]
-                                                       debug_assert!(self.historical_inbound_htlc_fulfills.contains(&htlc_id_arg));
-                                                       return UpdateFulfillFetch::DuplicateClaim {};
-                                               }
-                                       },
-                                       &HTLCUpdateAwaitingACK::FailHTLC { htlc_id, .. } => {
-                                               if htlc_id_arg == htlc_id {
-                                                       log_warn!(logger, "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.
-                                                       debug_assert!(false, "Tried to fulfill an HTLC that was already failed");
-                                                       return UpdateFulfillFetch::NewClaim { monitor_update, htlc_value_msat, msg: None };
-                                               }
-                                       },
-                                       _ => {}
-                               }
-                       }
-                       log_trace!(logger, "Adding HTLC claim to holding_cell in channel {}! Current state: {}", log_bytes!(self.channel_id()), self.channel_state);
-                       self.holding_cell_htlc_updates.push(HTLCUpdateAwaitingACK::ClaimHTLC {
-                               payment_preimage: payment_preimage_arg, htlc_id: htlc_id_arg,
-                       });
-                       #[cfg(any(test, fuzzing))]
-                       self.historical_inbound_htlc_fulfills.insert(htlc_id_arg);
-                       return UpdateFulfillFetch::NewClaim { monitor_update, htlc_value_msat, msg: None };
+               let on_holder_dust_htlc_exposure_msat = inbound_stats.on_holder_tx_dust_exposure_msat + outbound_stats.on_holder_tx_dust_exposure_msat;
+               if on_holder_dust_htlc_exposure_msat as i64 + htlc_timeout_dust_limit as i64 * 1000 - 1 > max_dust_htlc_exposure_msat as i64 {
+                       remaining_msat_below_dust_exposure_limit = Some(cmp::min(
+                               remaining_msat_below_dust_exposure_limit.unwrap_or(u64::max_value()),
+                               max_dust_htlc_exposure_msat.saturating_sub(on_holder_dust_htlc_exposure_msat)));
+                       dust_exposure_dust_limit_msat = cmp::max(dust_exposure_dust_limit_msat, htlc_timeout_dust_limit * 1000);
                }
-               #[cfg(any(test, fuzzing))]
-               self.historical_inbound_htlc_fulfills.insert(htlc_id_arg);
 
-               {
-                       let htlc = &mut self.pending_inbound_htlcs[pending_idx];
-                       if let InboundHTLCState::Committed = htlc.state {
+               if let Some(remaining_limit_msat) = remaining_msat_below_dust_exposure_limit {
+                       if available_capacity_msat < dust_exposure_dust_limit_msat {
+                               available_capacity_msat = cmp::min(available_capacity_msat, remaining_limit_msat);
                        } else {
-                               debug_assert!(false, "Have an inbound HTLC we tried to claim before it was fully committed to");
-                               return UpdateFulfillFetch::NewClaim { monitor_update, htlc_value_msat, msg: None };
+                               next_outbound_htlc_minimum_msat = cmp::max(next_outbound_htlc_minimum_msat, dust_exposure_dust_limit_msat);
                        }
-                       log_trace!(logger, "Upgrading HTLC {} to LocalRemoved with a Fulfill in channel {}!", log_bytes!(htlc.payment_hash.0), log_bytes!(self.channel_id));
-                       htlc.state = InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::Fulfill(payment_preimage_arg.clone()));
                }
 
-               UpdateFulfillFetch::NewClaim {
-                       monitor_update,
-                       htlc_value_msat,
-                       msg: Some(msgs::UpdateFulfillHTLC {
-                               channel_id: self.channel_id(),
-                               htlc_id: htlc_id_arg,
-                               payment_preimage: payment_preimage_arg,
-                       }),
+               available_capacity_msat = cmp::min(available_capacity_msat,
+                       context.counterparty_max_htlc_value_in_flight_msat - outbound_stats.pending_htlcs_value_msat);
+
+               if outbound_stats.pending_htlcs + 1 > context.counterparty_max_accepted_htlcs as u32 {
+                       available_capacity_msat = 0;
                }
-       }
 
-       pub fn get_update_fulfill_htlc_and_commit<L: Deref>(&mut self, htlc_id: u64, payment_preimage: PaymentPreimage, logger: &L) -> UpdateFulfillCommitFetch where L::Target: Logger {
-               let release_cs_monitor = self.pending_monitor_updates.iter().all(|upd| !upd.blocked);
-               match self.get_update_fulfill_htlc(htlc_id, payment_preimage, logger) {
-                       UpdateFulfillFetch::NewClaim { mut monitor_update, htlc_value_msat, msg } => {
-                               // Even if we aren't supposed to let new monitor updates with commitment state
-                               // updates run, we still need to push the preimage ChannelMonitorUpdateStep no
-                               // matter what. Sadly, to push a new monitor update which flies before others
-                               // already queued, we have to insert it into the pending queue and update the
-                               // update_ids of all the following monitors.
-                               let unblocked_update_pos = if release_cs_monitor && msg.is_some() {
-                                       let mut additional_update = self.build_commitment_no_status_check(logger);
-                                       // build_commitment_no_status_check may bump latest_monitor_id but we want them
-                                       // to be strictly increasing by one, so decrement it here.
-                                       self.latest_monitor_update_id = monitor_update.update_id;
-                                       monitor_update.updates.append(&mut additional_update.updates);
-                                       self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
-                                               update: monitor_update, blocked: false,
-                                       });
-                                       self.pending_monitor_updates.len() - 1
-                               } else {
-                                       let insert_pos = self.pending_monitor_updates.iter().position(|upd| upd.blocked)
-                                               .unwrap_or(self.pending_monitor_updates.len());
-                                       let new_mon_id = self.pending_monitor_updates.get(insert_pos)
-                                               .map(|upd| upd.update.update_id).unwrap_or(monitor_update.update_id);
-                                       monitor_update.update_id = new_mon_id;
-                                       self.pending_monitor_updates.insert(insert_pos, PendingChannelMonitorUpdate {
-                                               update: monitor_update, blocked: false,
-                                       });
-                                       for held_update in self.pending_monitor_updates.iter_mut().skip(insert_pos + 1) {
-                                               held_update.update.update_id += 1;
-                                       }
-                                       if msg.is_some() {
-                                               debug_assert!(false, "If there is a pending blocked monitor we should have MonitorUpdateInProgress set");
-                                               let update = self.build_commitment_no_status_check(logger);
-                                               self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
-                                                       update, blocked: true,
-                                               });
-                                       }
-                                       insert_pos
-                               };
-                               self.monitor_updating_paused(false, msg.is_some(), false, Vec::new(), Vec::new(), Vec::new());
-                               UpdateFulfillCommitFetch::NewClaim {
-                                       monitor_update: &self.pending_monitor_updates.get(unblocked_update_pos)
-                                               .expect("We just pushed the monitor update").update,
-                                       htlc_value_msat,
-                               }
-                       },
-                       UpdateFulfillFetch::DuplicateClaim {} => UpdateFulfillCommitFetch::DuplicateClaim {},
+               AvailableBalances {
+                       inbound_capacity_msat: cmp::max(context.channel_value_satoshis as i64 * 1000
+                                       - context.value_to_self_msat as i64
+                                       - context.get_inbound_pending_htlc_stats(None).pending_htlcs_value_msat as i64
+                                       - context.holder_selected_channel_reserve_satoshis as i64 * 1000,
+                               0) as u64,
+                       outbound_capacity_msat,
+                       next_outbound_htlc_limit_msat: available_capacity_msat,
+                       next_outbound_htlc_minimum_msat,
+                       balance_msat,
                }
        }
 
-       /// We can only have one resolution per HTLC. In some cases around reconnect, we may fulfill
-       /// an HTLC more than once or fulfill once and then attempt to fail after reconnect. We cannot,
-       /// however, fail more than once as we wait for an upstream failure to be irrevocably committed
-       /// before we fail backwards.
-       ///
-       /// If we do fail twice, we `debug_assert!(false)` and return `Ok(None)`. Thus, this will always
-       /// return `Ok(_)` if preconditions are met. In any case, `Err`s will only be
-       /// [`ChannelError::Ignore`].
-       pub fn queue_fail_htlc<L: Deref>(&mut self, htlc_id_arg: u64, err_packet: msgs::OnionErrorPacket, logger: &L)
-       -> Result<(), ChannelError> where L::Target: Logger {
-               self.fail_htlc(htlc_id_arg, err_packet, true, logger)
-                       .map(|msg_opt| assert!(msg_opt.is_none(), "We forced holding cell?"))
+       pub fn get_holder_counterparty_selected_channel_reserve_satoshis(&self) -> (u64, Option<u64>) {
+               let context = &self;
+               (context.holder_selected_channel_reserve_satoshis, context.counterparty_selected_channel_reserve_satoshis)
        }
 
-       /// We can only have one resolution per HTLC. In some cases around reconnect, we may fulfill
-       /// an HTLC more than once or fulfill once and then attempt to fail after reconnect. We cannot,
-       /// however, fail more than once as we wait for an upstream failure to be irrevocably committed
-       /// before we fail backwards.
+       /// Get the commitment tx fee for the local's (i.e. our) next commitment transaction based on the
+       /// number of pending HTLCs that are on track to be in our next commitment tx.
        ///
-       /// If we do fail twice, we `debug_assert!(false)` and return `Ok(None)`. Thus, this will always
-       /// return `Ok(_)` if preconditions are met. In any case, `Err`s will only be
-       /// [`ChannelError::Ignore`].
-       fn fail_htlc<L: Deref>(&mut self, htlc_id_arg: u64, err_packet: msgs::OnionErrorPacket, mut force_holding_cell: bool, logger: &L)
-       -> Result<Option<msgs::UpdateFailHTLC>, ChannelError> where L::Target: Logger {
-               if (self.channel_state & (ChannelState::ChannelReady as u32)) != (ChannelState::ChannelReady 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);
+       /// Optionally includes the `HTLCCandidate` given by `htlc` and an additional non-dust HTLC if
+       /// `fee_spike_buffer_htlc` is `Some`.
+       ///
+       /// The first extra HTLC is useful for determining whether we can accept a further HTLC, the
+       /// second allows for creating a buffer to ensure a further HTLC can always be accepted/added.
+       ///
+       /// Dust HTLCs are excluded.
+       fn next_local_commit_tx_fee_msat(&self, htlc: HTLCCandidate, fee_spike_buffer_htlc: Option<()>) -> u64 {
+               let context = &self;
+               assert!(context.is_outbound());
 
-               // 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 (htlc_success_dust_limit, htlc_timeout_dust_limit) = if context.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
+                       (0, 0)
+               } else {
+                       (context.feerate_per_kw as u64 * htlc_success_tx_weight(context.get_channel_type()) / 1000,
+                               context.feerate_per_kw as u64 * htlc_timeout_tx_weight(context.get_channel_type()) / 1000)
+               };
+               let real_dust_limit_success_sat = htlc_success_dust_limit + context.holder_dust_limit_satoshis;
+               let real_dust_limit_timeout_sat = htlc_timeout_dust_limit + context.holder_dust_limit_satoshis;
 
-               let mut pending_idx = core::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(ref reason) => {
-                                               if let &InboundHTLCRemovalReason::Fulfill(_) = reason {
-                                               } else {
-                                                       debug_assert!(false, "Tried to fail an HTLC that was already failed");
-                                               }
-                                               return Ok(None);
-                                       },
-                                       _ => {
-                                               debug_assert!(false, "Have an inbound HTLC we tried to claim before it was fully committed to");
-                                               return Err(ChannelError::Ignore(format!("Unable to find a pending HTLC which matched the given HTLC ID ({})", htlc.htlc_id)));
-                                       }
+               let mut addl_htlcs = 0;
+               if fee_spike_buffer_htlc.is_some() { addl_htlcs += 1; }
+               match htlc.origin {
+                       HTLCInitiator::LocalOffered => {
+                               if htlc.amount_msat / 1000 >= real_dust_limit_timeout_sat {
+                                       addl_htlcs += 1;
+                               }
+                       },
+                       HTLCInitiator::RemoteOffered => {
+                               if htlc.amount_msat / 1000 >= real_dust_limit_success_sat {
+                                       addl_htlcs += 1;
                                }
-                               pending_idx = idx;
                        }
                }
-               if pending_idx == core::usize::MAX {
-                       #[cfg(any(test, fuzzing))]
-                       // If we failed to find an HTLC to fail, make sure it was previously fulfilled and this
-                       // is simply a duplicate fail, not previously failed and we failed-back too early.
-                       debug_assert!(self.historical_inbound_htlc_fulfills.contains(&htlc_id_arg));
-                       return Ok(None);
+
+               let mut included_htlcs = 0;
+               for ref htlc in context.pending_inbound_htlcs.iter() {
+                       if htlc.amount_msat / 1000 < real_dust_limit_success_sat {
+                               continue
+                       }
+                       // We include LocalRemoved HTLCs here because we may still need to broadcast a commitment
+                       // transaction including this HTLC if it times out before they RAA.
+                       included_htlcs += 1;
                }
 
-               if (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32 | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress as u32)) != 0 {
-                       debug_assert!(force_holding_cell, "!force_holding_cell is only called when emptying the holding cell, so we shouldn't end up back in it!");
-                       force_holding_cell = true;
+               for ref htlc in context.pending_outbound_htlcs.iter() {
+                       if htlc.amount_msat / 1000 < real_dust_limit_timeout_sat {
+                               continue
+                       }
+                       match htlc.state {
+                               OutboundHTLCState::LocalAnnounced {..} => included_htlcs += 1,
+                               OutboundHTLCState::Committed => included_htlcs += 1,
+                               OutboundHTLCState::RemoteRemoved {..} => included_htlcs += 1,
+                               // We don't include AwaitingRemoteRevokeToRemove HTLCs because our next commitment
+                               // transaction won't be generated until they send us their next RAA, which will mean
+                               // dropping any HTLCs in this state.
+                               _ => {},
+                       }
                }
 
-               // Now update local state:
-               if force_holding_cell {
-                       for pending_update in self.holding_cell_htlc_updates.iter() {
-                               match pending_update {
-                                       &HTLCUpdateAwaitingACK::ClaimHTLC { htlc_id, .. } => {
-                                               if htlc_id_arg == htlc_id {
-                                                       #[cfg(any(test, fuzzing))]
-                                                       debug_assert!(self.historical_inbound_htlc_fulfills.contains(&htlc_id_arg));
-                                                       return Ok(None);
-                                               }
-                                       },
-                                       &HTLCUpdateAwaitingACK::FailHTLC { htlc_id, .. } => {
-                                               if htlc_id_arg == htlc_id {
-                                                       debug_assert!(false, "Tried to fail an HTLC that was already failed");
-                                                       return Err(ChannelError::Ignore("Unable to find a pending HTLC which matched the given HTLC ID".to_owned()));
-                                               }
-                                       },
-                                       _ => {}
-                               }
+               for htlc in context.holding_cell_htlc_updates.iter() {
+                       match htlc {
+                               &HTLCUpdateAwaitingACK::AddHTLC { amount_msat, .. } => {
+                                       if amount_msat / 1000 < real_dust_limit_timeout_sat {
+                                               continue
+                                       }
+                                       included_htlcs += 1
+                               },
+                               _ => {}, // Don't include claims/fails that are awaiting ack, because once we get the
+                                        // ack we're guaranteed to never include them in commitment txs anymore.
                        }
-                       log_trace!(logger, "Placing failure for HTLC ID {} in holding cell in channel {}.", htlc_id_arg, log_bytes!(self.channel_id()));
-                       self.holding_cell_htlc_updates.push(HTLCUpdateAwaitingACK::FailHTLC {
-                               htlc_id: htlc_id_arg,
-                               err_packet,
-                       });
-                       return Ok(None);
                }
 
-               log_trace!(logger, "Failing HTLC ID {} back with a update_fail_htlc message in channel {}.", htlc_id_arg, log_bytes!(self.channel_id()));
+               let num_htlcs = included_htlcs + addl_htlcs;
+               let res = commit_tx_fee_msat(context.feerate_per_kw, num_htlcs, &context.channel_type);
+               #[cfg(any(test, fuzzing))]
                {
-                       let htlc = &mut self.pending_inbound_htlcs[pending_idx];
-                       htlc.state = InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::FailRelay(err_packet.clone()));
+                       let mut fee = res;
+                       if fee_spike_buffer_htlc.is_some() {
+                               fee = commit_tx_fee_msat(context.feerate_per_kw, num_htlcs - 1, &context.channel_type);
+                       }
+                       let total_pending_htlcs = context.pending_inbound_htlcs.len() + context.pending_outbound_htlcs.len()
+                               + context.holding_cell_htlc_updates.len();
+                       let commitment_tx_info = CommitmentTxInfoCached {
+                               fee,
+                               total_pending_htlcs,
+                               next_holder_htlc_id: match htlc.origin {
+                                       HTLCInitiator::LocalOffered => context.next_holder_htlc_id + 1,
+                                       HTLCInitiator::RemoteOffered => context.next_holder_htlc_id,
+                               },
+                               next_counterparty_htlc_id: match htlc.origin {
+                                       HTLCInitiator::LocalOffered => context.next_counterparty_htlc_id,
+                                       HTLCInitiator::RemoteOffered => context.next_counterparty_htlc_id + 1,
+                               },
+                               feerate: context.feerate_per_kw,
+                       };
+                       *context.next_local_commitment_tx_fee_info_cached.lock().unwrap() = Some(commitment_tx_info);
                }
-
-               Ok(Some(msgs::UpdateFailHTLC {
-                       channel_id: self.channel_id(),
-                       htlc_id: htlc_id_arg,
-                       reason: err_packet
-               }))
+               res
        }
 
-       // Message handlers:
+       /// Get the commitment tx fee for the remote's next commitment transaction based on the number of
+       /// pending HTLCs that are on track to be in their next commitment tx
+       ///
+       /// Optionally includes the `HTLCCandidate` given by `htlc` and an additional non-dust HTLC if
+       /// `fee_spike_buffer_htlc` is `Some`.
+       ///
+       /// The first extra HTLC is useful for determining whether we can accept a further HTLC, the
+       /// second allows for creating a buffer to ensure a further HTLC can always be accepted/added.
+       ///
+       /// Dust HTLCs are excluded.
+       fn next_remote_commit_tx_fee_msat(&self, htlc: HTLCCandidate, fee_spike_buffer_htlc: Option<()>) -> u64 {
+               let context = &self;
+               assert!(!context.is_outbound());
 
-       pub fn accept_channel(&mut self, msg: &msgs::AcceptChannel, default_limits: &ChannelHandshakeLimits, their_features: &InitFeatures) -> Result<(), ChannelError> {
-               let peer_limits = if let Some(ref limits) = self.inbound_handshake_limits_override { limits } else { default_limits };
+               let (htlc_success_dust_limit, htlc_timeout_dust_limit) = if context.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
+                       (0, 0)
+               } else {
+                       (context.feerate_per_kw as u64 * htlc_success_tx_weight(context.get_channel_type()) / 1000,
+                               context.feerate_per_kw as u64 * htlc_timeout_tx_weight(context.get_channel_type()) / 1000)
+               };
+               let real_dust_limit_success_sat = htlc_success_dust_limit + context.counterparty_dust_limit_satoshis;
+               let real_dust_limit_timeout_sat = htlc_timeout_dust_limit + context.counterparty_dust_limit_satoshis;
 
-               // Check sanity of message fields:
-               if !self.is_outbound() {
-                       return Err(ChannelError::Close("Got an accept_channel message from an inbound peer".to_owned()));
-               }
-               if self.channel_state != ChannelState::OurInitSent as u32 {
-                       return Err(ChannelError::Close("Got an accept_channel message at a strange time".to_owned()));
-               }
-               if msg.dust_limit_satoshis > 21000000 * 100000000 {
-                       return Err(ChannelError::Close(format!("Peer never wants payout outputs? dust_limit_satoshis was {}", msg.dust_limit_satoshis)));
-               }
-               if msg.channel_reserve_satoshis > self.channel_value_satoshis {
-                       return Err(ChannelError::Close(format!("Bogus channel_reserve_satoshis ({}). Must not be greater than ({})", msg.channel_reserve_satoshis, self.channel_value_satoshis)));
-               }
-               if msg.dust_limit_satoshis > self.holder_selected_channel_reserve_satoshis {
-                       return Err(ChannelError::Close(format!("Dust limit ({}) is bigger than our channel reserve ({})", msg.dust_limit_satoshis, self.holder_selected_channel_reserve_satoshis)));
-               }
-               if msg.channel_reserve_satoshis > self.channel_value_satoshis - self.holder_selected_channel_reserve_satoshis {
-                       return Err(ChannelError::Close(format!("Bogus channel_reserve_satoshis ({}). Must not be greater than channel value minus our reserve ({})",
-                               msg.channel_reserve_satoshis, self.channel_value_satoshis - self.holder_selected_channel_reserve_satoshis)));
-               }
-               let full_channel_value_msat = (self.channel_value_satoshis - msg.channel_reserve_satoshis) * 1000;
-               if msg.htlc_minimum_msat >= full_channel_value_msat {
-                       return Err(ChannelError::Close(format!("Minimum htlc value ({}) is full channel value ({})", msg.htlc_minimum_msat, full_channel_value_msat)));
+               let mut addl_htlcs = 0;
+               if fee_spike_buffer_htlc.is_some() { addl_htlcs += 1; }
+               match htlc.origin {
+                       HTLCInitiator::LocalOffered => {
+                               if htlc.amount_msat / 1000 >= real_dust_limit_success_sat {
+                                       addl_htlcs += 1;
+                               }
+                       },
+                       HTLCInitiator::RemoteOffered => {
+                               if htlc.amount_msat / 1000 >= real_dust_limit_timeout_sat {
+                                       addl_htlcs += 1;
+                               }
+                       }
                }
-               let max_delay_acceptable = u16::min(peer_limits.their_to_self_delay, MAX_LOCAL_BREAKDOWN_TIMEOUT);
-               if msg.to_self_delay > max_delay_acceptable {
-                       return Err(ChannelError::Close(format!("They wanted our payments to be delayed by a needlessly long period. Upper limit: {}. Actual: {}", max_delay_acceptable, msg.to_self_delay)));
+
+               // When calculating the set of HTLCs which will be included in their next commitment_signed, all
+               // non-dust inbound HTLCs are included (as all states imply it will be included) and only
+               // committed outbound HTLCs, see below.
+               let mut included_htlcs = 0;
+               for ref htlc in context.pending_inbound_htlcs.iter() {
+                       if htlc.amount_msat / 1000 <= real_dust_limit_timeout_sat {
+                               continue
+                       }
+                       included_htlcs += 1;
                }
-               if msg.max_accepted_htlcs < 1 {
-                       return Err(ChannelError::Close("0 max_accepted_htlcs makes for a useless channel".to_owned()));
+
+               for ref htlc in context.pending_outbound_htlcs.iter() {
+                       if htlc.amount_msat / 1000 <= real_dust_limit_success_sat {
+                               continue
+                       }
+                       // We only include outbound HTLCs if it will not be included in their next commitment_signed,
+                       // i.e. if they've responded to us with an RAA after announcement.
+                       match htlc.state {
+                               OutboundHTLCState::Committed => included_htlcs += 1,
+                               OutboundHTLCState::RemoteRemoved {..} => included_htlcs += 1,
+                               OutboundHTLCState::LocalAnnounced { .. } => included_htlcs += 1,
+                               _ => {},
+                       }
                }
-               if msg.max_accepted_htlcs > MAX_HTLCS {
-                       return Err(ChannelError::Close(format!("max_accepted_htlcs was {}. It must not be larger than {}", msg.max_accepted_htlcs, MAX_HTLCS)));
+
+               let num_htlcs = included_htlcs + addl_htlcs;
+               let res = commit_tx_fee_msat(context.feerate_per_kw, num_htlcs, &context.channel_type);
+               #[cfg(any(test, fuzzing))]
+               {
+                       let mut fee = res;
+                       if fee_spike_buffer_htlc.is_some() {
+                               fee = commit_tx_fee_msat(context.feerate_per_kw, num_htlcs - 1, &context.channel_type);
+                       }
+                       let total_pending_htlcs = context.pending_inbound_htlcs.len() + context.pending_outbound_htlcs.len();
+                       let commitment_tx_info = CommitmentTxInfoCached {
+                               fee,
+                               total_pending_htlcs,
+                               next_holder_htlc_id: match htlc.origin {
+                                       HTLCInitiator::LocalOffered => context.next_holder_htlc_id + 1,
+                                       HTLCInitiator::RemoteOffered => context.next_holder_htlc_id,
+                               },
+                               next_counterparty_htlc_id: match htlc.origin {
+                                       HTLCInitiator::LocalOffered => context.next_counterparty_htlc_id,
+                                       HTLCInitiator::RemoteOffered => context.next_counterparty_htlc_id + 1,
+                               },
+                               feerate: context.feerate_per_kw,
+                       };
+                       *context.next_remote_commitment_tx_fee_info_cached.lock().unwrap() = Some(commitment_tx_info);
                }
+               res
+       }
 
-               // Now check against optional parameters as set by config...
-               if msg.htlc_minimum_msat > peer_limits.max_htlc_minimum_msat {
-                       return Err(ChannelError::Close(format!("htlc_minimum_msat ({}) is higher than the user specified limit ({})", msg.htlc_minimum_msat, peer_limits.max_htlc_minimum_msat)));
-               }
-               if msg.max_htlc_value_in_flight_msat < peer_limits.min_max_htlc_value_in_flight_msat {
-                       return Err(ChannelError::Close(format!("max_htlc_value_in_flight_msat ({}) is less than the user specified limit ({})", msg.max_htlc_value_in_flight_msat, peer_limits.min_max_htlc_value_in_flight_msat)));
-               }
-               if msg.channel_reserve_satoshis > peer_limits.max_channel_reserve_satoshis {
-                       return Err(ChannelError::Close(format!("channel_reserve_satoshis ({}) is higher than the user specified limit ({})", msg.channel_reserve_satoshis, peer_limits.max_channel_reserve_satoshis)));
-               }
-               if msg.max_accepted_htlcs < peer_limits.min_max_accepted_htlcs {
-                       return Err(ChannelError::Close(format!("max_accepted_htlcs ({}) is less than the user specified limit ({})", msg.max_accepted_htlcs, peer_limits.min_max_accepted_htlcs)));
-               }
-               if msg.dust_limit_satoshis < MIN_CHAN_DUST_LIMIT_SATOSHIS {
-                       return Err(ChannelError::Close(format!("dust_limit_satoshis ({}) is less than the implementation limit ({})", msg.dust_limit_satoshis, MIN_CHAN_DUST_LIMIT_SATOSHIS)));
-               }
-               if msg.dust_limit_satoshis > MAX_CHAN_DUST_LIMIT_SATOSHIS {
-                       return Err(ChannelError::Close(format!("dust_limit_satoshis ({}) is greater than the implementation limit ({})", msg.dust_limit_satoshis, MAX_CHAN_DUST_LIMIT_SATOSHIS)));
-               }
-               if msg.minimum_depth > peer_limits.max_minimum_depth {
-                       return Err(ChannelError::Close(format!("We consider the minimum depth to be unreasonably large. Expected minimum: ({}). Actual: ({})", peer_limits.max_minimum_depth, msg.minimum_depth)));
+       /// Returns transaction if there is pending funding transaction that is yet to broadcast
+       pub fn unbroadcasted_funding(&self) -> Option<Transaction> {
+               if self.channel_state & (ChannelState::FundingCreated as u32) != 0 {
+                       self.funding_transaction.clone()
+               } else {
+                       None
                }
+       }
 
-               if let Some(ty) = &msg.channel_type {
-                       if *ty != self.channel_type {
-                               return Err(ChannelError::Close("Channel Type in accept_channel didn't match the one sent in open_channel.".to_owned()));
-                       }
-               } else if their_features.supports_channel_type() {
-                       // Assume they've accepted the channel type as they said they understand it.
-               } else {
-                       let channel_type = ChannelTypeFeatures::from_init(&their_features);
-                       if channel_type != ChannelTypeFeatures::only_static_remote_key() {
-                               return Err(ChannelError::Close("Only static_remote_key is supported for non-negotiated channel types".to_owned()));
-                       }
-                       self.channel_type = channel_type;
-               }
+       /// 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, should_broadcast: bool) -> ShutdownResult {
+               // Note that we MUST only generate a monitor update that indicates force-closure - we're
+               // called during initialization prior to the chain_monitor in the encompassing ChannelManager
+               // being fully configured in some cases. Thus, its likely any monitor events we generate will
+               // be delayed in being processed! See the docs for `ChannelManagerReadArgs` for more.
+               assert!(self.channel_state != ChannelState::ShutdownComplete as u32);
 
-               let counterparty_shutdown_scriptpubkey = if their_features.supports_upfront_shutdown_script() {
-                       match &msg.shutdown_scriptpubkey {
-                               &Some(ref script) => {
-                                       // Peer is signaling upfront_shutdown and has opt-out with a 0-length script. We don't enforce anything
-                                       if script.len() == 0 {
-                                               None
-                                       } else {
-                                               if !script::is_bolt2_compliant(&script, their_features) {
-                                                       return Err(ChannelError::Close(format!("Peer is signaling upfront_shutdown but has provided an unacceptable scriptpubkey format: {}", script)));
-                                               }
-                                               Some(script.clone())
-                                       }
+               // 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());
+               let counterparty_node_id = self.get_counterparty_node_id();
+               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, counterparty_node_id, self.channel_id));
                                },
-                               // 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
-                               &None => {
-                                       return Err(ChannelError::Close("Peer is signaling upfront_shutdown but we don't get any script. Use 0-length script to opt-out".to_owned()));
-                               }
+                               _ => {}
                        }
-               } else { None };
-
-               self.counterparty_dust_limit_satoshis = msg.dust_limit_satoshis;
-               self.counterparty_max_htlc_value_in_flight_msat = cmp::min(msg.max_htlc_value_in_flight_msat, self.channel_value_satoshis * 1000);
-               self.counterparty_selected_channel_reserve_satoshis = Some(msg.channel_reserve_satoshis);
-               self.counterparty_htlc_minimum_msat = msg.htlc_minimum_msat;
-               self.counterparty_max_accepted_htlcs = msg.max_accepted_htlcs;
-
-               if peer_limits.trust_own_funding_0conf {
-                       self.minimum_depth = Some(msg.minimum_depth);
-               } else {
-                       self.minimum_depth = Some(cmp::max(1, msg.minimum_depth));
                }
+               let monitor_update = if let Some(funding_txo) = self.get_funding_txo() {
+                       // If we haven't yet exchanged funding signatures (ie channel_state < FundingSent),
+                       // returning a channel monitor update here would imply a channel monitor update before
+                       // we even registered the channel monitor to begin with, which is invalid.
+                       // Thus, if we aren't actually at a point where we could conceivably broadcast the
+                       // funding transaction, don't return a funding txo (which prevents providing the
+                       // monitor update to the user, even if we return one).
+                       // See test_duplicate_chan_id and test_pre_lockin_no_chan_closed_update for more.
+                       if self.channel_state & (ChannelState::FundingSent as u32 | ChannelState::ChannelReady as u32 | ChannelState::ShutdownComplete as u32) != 0 {
+                               self.latest_monitor_update_id = CLOSED_CHANNEL_UPDATE_ID;
+                               Some((self.get_counterparty_node_id(), funding_txo, ChannelMonitorUpdate {
+                                       update_id: self.latest_monitor_update_id,
+                                       updates: vec![ChannelMonitorUpdateStep::ChannelForceClosed { should_broadcast }],
+                               }))
+                       } else { None }
+               } else { None };
 
-               let counterparty_pubkeys = ChannelPublicKeys {
-                       funding_pubkey: msg.funding_pubkey,
-                       revocation_basepoint: msg.revocation_basepoint,
-                       payment_point: msg.payment_point,
-                       delayed_payment_basepoint: msg.delayed_payment_basepoint,
-                       htlc_basepoint: msg.htlc_basepoint
-               };
-
-               self.channel_transaction_parameters.counterparty_parameters = Some(CounterpartyChannelTransactionParameters {
-                       selected_contest_delay: msg.to_self_delay,
-                       pubkeys: counterparty_pubkeys,
-               });
+               self.channel_state = ChannelState::ShutdownComplete as u32;
+               self.update_time_counter += 1;
+               (monitor_update, dropped_outbound_htlcs)
+       }
+}
 
-               self.counterparty_cur_commitment_point = Some(msg.first_per_commitment_point);
-               self.counterparty_shutdown_scriptpubkey = counterparty_shutdown_scriptpubkey;
+// Internal utility functions for channels
 
-               self.channel_state = ChannelState::OurInitSent as u32 | ChannelState::TheirInitSent as u32;
-               self.inbound_handshake_limits_override = None; // We're done enforcing limits on our peer's handshake now.
+/// Returns the value to use for `holder_max_htlc_value_in_flight_msat` as a percentage of the
+/// `channel_value_satoshis` in msat, set through
+/// [`ChannelHandshakeConfig::max_inbound_htlc_value_in_flight_percent_of_channel`]
+///
+/// The effective percentage is lower bounded by 1% and upper bounded by 100%.
+///
+/// [`ChannelHandshakeConfig::max_inbound_htlc_value_in_flight_percent_of_channel`]: crate::util::config::ChannelHandshakeConfig::max_inbound_htlc_value_in_flight_percent_of_channel
+fn get_holder_max_htlc_value_in_flight_msat(channel_value_satoshis: u64, config: &ChannelHandshakeConfig) -> u64 {
+       let configured_percent = if config.max_inbound_htlc_value_in_flight_percent_of_channel < 1 {
+               1
+       } else if config.max_inbound_htlc_value_in_flight_percent_of_channel > 100 {
+               100
+       } else {
+               config.max_inbound_htlc_value_in_flight_percent_of_channel as u64
+       };
+       channel_value_satoshis * 10 * configured_percent
+}
 
-               Ok(())
-       }
+/// Returns a minimum channel reserve value the remote needs to maintain,
+/// required by us according to the configured or default
+/// [`ChannelHandshakeConfig::their_channel_reserve_proportional_millionths`]
+///
+/// Guaranteed to return a value no larger than channel_value_satoshis
+///
+/// This is used both for outbound and inbound channels and has lower bound
+/// of `MIN_THEIR_CHAN_RESERVE_SATOSHIS`.
+pub(crate) fn get_holder_selected_channel_reserve_satoshis(channel_value_satoshis: u64, config: &UserConfig) -> u64 {
+       let calculated_reserve = channel_value_satoshis.saturating_mul(config.channel_handshake_config.their_channel_reserve_proportional_millionths as u64) / 1_000_000;
+       cmp::min(channel_value_satoshis, cmp::max(calculated_reserve, MIN_THEIR_CHAN_RESERVE_SATOSHIS))
+}
 
-       fn funding_created_signature<L: Deref>(&mut self, sig: &Signature, logger: &L) -> Result<(Txid, CommitmentTransaction, Signature), ChannelError> where L::Target: Logger {
-               let funding_script = self.get_funding_redeemscript();
+/// This is for legacy reasons, present for forward-compatibility.
+/// LDK versions older than 0.0.104 don't know how read/handle values other than default
+/// from storage. Hence, we use this function to not persist default values of
+/// `holder_selected_channel_reserve_satoshis` for channels into storage.
+pub(crate) fn get_legacy_default_holder_selected_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))
+}
 
-               let keys = self.build_holder_transaction_keys(self.cur_holder_commitment_transaction_number);
-               let initial_commitment_tx = self.build_commitment_transaction(self.cur_holder_commitment_transaction_number, &keys, true, false, logger).tx;
-               {
-                       let trusted_tx = initial_commitment_tx.trust();
-                       let initial_commitment_bitcoin_tx = trusted_tx.built_transaction();
-                       let sighash = initial_commitment_bitcoin_tx.get_sighash_all(&funding_script, self.channel_value_satoshis);
-                       // They sign the holder commitment transaction...
-                       log_trace!(logger, "Checking funding_created tx signature {} by key {} against tx {} (sighash {}) with redeemscript {} for channel {}.",
-                               log_bytes!(sig.serialize_compact()[..]), log_bytes!(self.counterparty_funding_pubkey().serialize()),
-                               encode::serialize_hex(&initial_commitment_bitcoin_tx.transaction), log_bytes!(sighash[..]),
-                               encode::serialize_hex(&funding_script), log_bytes!(self.channel_id()));
-                       secp_check!(self.secp_ctx.verify_ecdsa(&sighash, &sig, self.counterparty_funding_pubkey()), "Invalid funding_created signature from peer".to_owned());
-               }
+// Get the fee cost in SATS of a commitment tx with a given number of HTLC outputs.
+// Note that num_htlcs should not include dust HTLCs.
+#[inline]
+fn commit_tx_fee_sat(feerate_per_kw: u32, num_htlcs: usize, channel_type_features: &ChannelTypeFeatures) -> u64 {
+       feerate_per_kw as u64 * (commitment_tx_base_weight(channel_type_features) + num_htlcs as u64 * COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000
+}
 
-               let counterparty_keys = self.build_remote_transaction_keys();
-               let counterparty_initial_commitment_tx = self.build_commitment_transaction(self.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, false, logger).tx;
+// Get the fee cost in MSATS of a commitment tx with a given number of HTLC outputs.
+// Note that num_htlcs should not include dust HTLCs.
+fn commit_tx_fee_msat(feerate_per_kw: u32, num_htlcs: usize, channel_type_features: &ChannelTypeFeatures) -> u64 {
+       // Note that we need to divide before multiplying to round properly,
+       // since the lowest denomination of bitcoin on-chain is the satoshi.
+       (commitment_tx_base_weight(channel_type_features) + num_htlcs as u64 * COMMITMENT_TX_WEIGHT_PER_HTLC) * feerate_per_kw as u64 / 1000 * 1000
+}
 
-               let counterparty_trusted_tx = counterparty_initial_commitment_tx.trust();
-               let counterparty_initial_bitcoin_tx = counterparty_trusted_tx.built_transaction();
-               log_trace!(logger, "Initial counterparty tx for channel {} is: txid {} tx {}",
-                       log_bytes!(self.channel_id()), counterparty_initial_bitcoin_tx.txid, encode::serialize_hex(&counterparty_initial_bitcoin_tx.transaction));
+// 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.
+//
+// Holder designates channel data owned for the benefit of the user client.
+// Counterparty designates channel data owned by the another channel participant entity.
+pub(super) struct Channel<Signer: ChannelSigner> {
+       pub context: ChannelContext<Signer>,
+}
 
-               let counterparty_signature = self.holder_signer.sign_counterparty_commitment(&counterparty_initial_commitment_tx, Vec::new(), &self.secp_ctx)
-                               .map_err(|_| ChannelError::Close("Failed to get signatures for new commitment_signed".to_owned()))?.0;
+#[cfg(any(test, fuzzing))]
+struct CommitmentTxInfoCached {
+       fee: u64,
+       total_pending_htlcs: usize,
+       next_holder_htlc_id: u64,
+       next_counterparty_htlc_id: u64,
+       feerate: u32,
+}
 
-               // We sign "counterparty" commitment transaction, allowing them to broadcast the tx if they wish.
-               Ok((counterparty_initial_bitcoin_tx.txid, initial_commitment_tx, counterparty_signature))
+impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
+       fn check_remote_fee<F: Deref, L: Deref>(fee_estimator: &LowerBoundedFeeEstimator<F>,
+               feerate_per_kw: u32, cur_feerate_per_kw: Option<u32>, logger: &L)
+               -> Result<(), ChannelError> where F::Target: FeeEstimator, L::Target: Logger,
+       {
+               // We only bound the fee updates on the upper side to prevent completely absurd feerates,
+               // always accepting up to 25 sat/vByte or 10x our fee estimator's "High Priority" fee.
+               // We generally don't care too much if they set the feerate to something very high, but it
+               // could result in the channel being useless due to everything being dust.
+               let upper_limit = cmp::max(250 * 25,
+                       fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::HighPriority) as u64 * 10);
+               if feerate_per_kw as u64 > upper_limit {
+                       return Err(ChannelError::Close(format!("Peer's feerate much too high. Actual: {}. Our expected upper limit: {}", feerate_per_kw, upper_limit)));
+               }
+               let lower_limit = fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Background);
+               // Some fee estimators round up to the next full sat/vbyte (ie 250 sats per kw), causing
+               // occasional issues with feerate disagreements between an initiator that wants a feerate
+               // of 1.1 sat/vbyte and a receiver that wants 1.1 rounded up to 2. Thus, we always add 250
+               // sat/kw before the comparison here.
+               if feerate_per_kw + 250 < lower_limit {
+                       if let Some(cur_feerate) = cur_feerate_per_kw {
+                               if feerate_per_kw > cur_feerate {
+                                       log_warn!(logger,
+                                               "Accepting feerate that may prevent us from closing this channel because it's higher than what we have now. Had {} s/kW, now {} s/kW.",
+                                               cur_feerate, feerate_per_kw);
+                                       return Ok(());
+                               }
+                       }
+                       return Err(ChannelError::Close(format!("Peer's feerate much too low. Actual: {}. Our expected lower limit: {} (- 250)", feerate_per_kw, lower_limit)));
+               }
+               Ok(())
        }
 
-       fn counterparty_funding_pubkey(&self) -> &PublicKey {
-               &self.get_counterparty_pubkeys().funding_pubkey
+       #[inline]
+       fn get_closing_scriptpubkey(&self) -> Script {
+               // The shutdown scriptpubkey is set on channel opening when option_upfront_shutdown_script
+               // is signaled. Otherwise, it is set when sending a shutdown message. Calling this method
+               // outside of those situations will fail.
+               self.context.shutdown_scriptpubkey.clone().unwrap().into_inner()
        }
 
-       pub fn funding_created<SP: Deref, L: Deref>(
-               &mut self, msg: &msgs::FundingCreated, best_block: BestBlock, signer_provider: &SP, logger: &L
-       ) -> Result<(msgs::FundingSigned, ChannelMonitor<Signer>), ChannelError>
-       where
-               SP::Target: SignerProvider<Signer = Signer>,
-               L::Target: Logger
-       {
-               if self.is_outbound() {
-                       return Err(ChannelError::Close("Received funding_created for an outbound channel?".to_owned()));
-               }
-               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!".to_owned()));
-               }
-               if self.inbound_awaiting_accept {
-                       return Err(ChannelError::Close("FundingCreated message received before the channel was accepted".to_owned()));
+       #[inline]
+       fn get_closing_transaction_weight(&self, a_scriptpubkey: Option<&Script>, b_scriptpubkey: Option<&Script>) -> u64 {
+               let mut ret =
+               (4 +                                                   // version
+                1 +                                                   // input count
+                36 +                                                  // prevout
+                1 +                                                   // script length (0)
+                4 +                                                   // sequence
+                1 +                                                   // output count
+                4                                                     // lock time
+                )*4 +                                                 // * 4 for non-witness parts
+               2 +                                                    // witness marker and flag
+               1 +                                                    // witness element count
+               4 +                                                    // 4 element lengths (2 sigs, multisig dummy, and witness script)
+               self.context.get_funding_redeemscript().len() as u64 + // funding witness script
+               2*(1 + 71);                                            // two signatures + sighash type flags
+               if let Some(spk) = a_scriptpubkey {
+                       ret += ((8+1) +                                    // output values and script length
+                               spk.len() as u64) * 4;                         // scriptpubkey and witness multiplier
                }
-               if self.commitment_secrets.get_min_seen_secret() != (1 << 48) ||
-                               self.cur_counterparty_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER ||
-                               self.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
-                       panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
+               if let Some(spk) = b_scriptpubkey {
+                       ret += ((8+1) +                                    // output values and script length
+                               spk.len() as u64) * 4;                         // scriptpubkey and witness multiplier
                }
+               ret
+       }
 
-               let funding_txo = OutPoint { txid: msg.funding_txid, index: msg.funding_output_index };
-               self.channel_transaction_parameters.funding_outpoint = Some(funding_txo);
-               // This is an externally observable change before we finish all our checks.  In particular
-               // funding_created_signature may fail.
-               self.holder_signer.provide_channel_parameters(&self.channel_transaction_parameters);
-
-               let (counterparty_initial_commitment_txid, initial_commitment_tx, signature) = match self.funding_created_signature(&msg.signature, logger) {
-                       Ok(res) => res,
-                       Err(ChannelError::Close(e)) => {
-                               self.channel_transaction_parameters.funding_outpoint = None;
-                               return Err(ChannelError::Close(e));
-                       },
-                       Err(e) => {
-                               // The only error we know how to handle is ChannelError::Close, so we fall over here
-                               // to make sure we don't continue with an inconsistent state.
-                               panic!("unexpected error type from funding_created_signature {:?}", e);
-                       }
-               };
-
-               let holder_commitment_tx = HolderCommitmentTransaction::new(
-                       initial_commitment_tx,
-                       msg.signature,
-                       Vec::new(),
-                       &self.get_holder_pubkeys().funding_pubkey,
-                       self.counterparty_funding_pubkey()
-               );
-
-               self.holder_signer.validate_holder_commitment(&holder_commitment_tx, Vec::new())
-                       .map_err(|_| ChannelError::Close("Failed to validate our commitment".to_owned()))?;
+       #[inline]
+       fn build_closing_transaction(&self, proposed_total_fee_satoshis: u64, skip_remote_output: bool) -> (ClosingTransaction, u64) {
+               assert!(self.context.pending_inbound_htlcs.is_empty());
+               assert!(self.context.pending_outbound_htlcs.is_empty());
+               assert!(self.context.pending_update_fee.is_none());
 
-               // Now that we're past error-generating stuff, update our local state:
+               let mut total_fee_satoshis = proposed_total_fee_satoshis;
+               let mut value_to_holder: i64 = (self.context.value_to_self_msat as i64) / 1000 - if self.context.is_outbound() { total_fee_satoshis as i64 } else { 0 };
+               let mut value_to_counterparty: i64 = ((self.context.channel_value_satoshis * 1000 - self.context.value_to_self_msat) as i64 / 1000) - if self.context.is_outbound() { 0 } else { total_fee_satoshis as i64 };
 
-               let funding_redeemscript = self.get_funding_redeemscript();
-               let funding_txo_script = funding_redeemscript.to_v0_p2wsh();
-               let obscure_factor = get_commitment_transaction_number_obscure_factor(&self.get_holder_pubkeys().payment_point, &self.get_counterparty_pubkeys().payment_point, self.is_outbound());
-               let shutdown_script = self.shutdown_scriptpubkey.clone().map(|script| script.into_inner());
-               let mut monitor_signer = signer_provider.derive_channel_signer(self.channel_value_satoshis, self.channel_keys_id);
-               monitor_signer.provide_channel_parameters(&self.channel_transaction_parameters);
-               let channel_monitor = ChannelMonitor::new(self.secp_ctx.clone(), monitor_signer,
-                                                         shutdown_script, self.get_holder_selected_contest_delay(),
-                                                         &self.destination_script, (funding_txo, funding_txo_script.clone()),
-                                                         &self.channel_transaction_parameters,
-                                                         funding_redeemscript.clone(), self.channel_value_satoshis,
-                                                         obscure_factor,
-                                                         holder_commitment_tx, best_block, self.counterparty_node_id);
+               if value_to_holder < 0 {
+                       assert!(self.context.is_outbound());
+                       total_fee_satoshis += (-value_to_holder) as u64;
+               } else if value_to_counterparty < 0 {
+                       assert!(!self.context.is_outbound());
+                       total_fee_satoshis += (-value_to_counterparty) as u64;
+               }
 
-               channel_monitor.provide_latest_counterparty_commitment_tx(counterparty_initial_commitment_txid, Vec::new(), self.cur_counterparty_commitment_transaction_number, self.counterparty_cur_commitment_point.unwrap(), logger);
+               if skip_remote_output || value_to_counterparty as u64 <= self.context.holder_dust_limit_satoshis {
+                       value_to_counterparty = 0;
+               }
 
-               self.channel_state = ChannelState::FundingSent as u32;
-               self.channel_id = funding_txo.to_channel_id();
-               self.cur_counterparty_commitment_transaction_number -= 1;
-               self.cur_holder_commitment_transaction_number -= 1;
+               if value_to_holder as u64 <= self.context.holder_dust_limit_satoshis {
+                       value_to_holder = 0;
+               }
 
-               log_info!(logger, "Generated funding_signed for peer for channel {}", log_bytes!(self.channel_id()));
+               assert!(self.context.shutdown_scriptpubkey.is_some());
+               let holder_shutdown_script = self.get_closing_scriptpubkey();
+               let counterparty_shutdown_script = self.context.counterparty_shutdown_scriptpubkey.clone().unwrap();
+               let funding_outpoint = self.funding_outpoint().into_bitcoin_outpoint();
 
-               let need_channel_ready = self.check_get_channel_ready(0).is_some();
-               self.monitor_updating_paused(false, false, need_channel_ready, Vec::new(), Vec::new(), Vec::new());
+               let closing_transaction = ClosingTransaction::new(value_to_holder as u64, value_to_counterparty as u64, holder_shutdown_script, counterparty_shutdown_script, funding_outpoint);
+               (closing_transaction, total_fee_satoshis)
+       }
 
-               Ok((msgs::FundingSigned {
-                       channel_id: self.channel_id,
-                       signature,
-                       #[cfg(taproot)]
-                       partial_signature_with_nonce: None,
-               }, channel_monitor))
+       fn funding_outpoint(&self) -> OutPoint {
+               self.context.channel_transaction_parameters.funding_outpoint.unwrap()
        }
 
-       /// 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<SP: Deref, L: Deref>(
-               &mut self, msg: &msgs::FundingSigned, best_block: BestBlock, signer_provider: &SP, logger: &L
-       ) -> Result<ChannelMonitor<Signer>, ChannelError>
-       where
-               SP::Target: SignerProvider<Signer = Signer>,
-               L::Target: Logger
-       {
-               if !self.is_outbound() {
-                       return Err(ChannelError::Close("Received funding_signed for an inbound channel?".to_owned()));
-               }
-               if self.channel_state & !(ChannelState::MonitorUpdateInProgress as u32) != ChannelState::FundingCreated as u32 {
-                       return Err(ChannelError::Close("Received funding_signed in strange state!".to_owned()));
-               }
-               if self.commitment_secrets.get_min_seen_secret() != (1 << 48) ||
-                               self.cur_counterparty_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER ||
-                               self.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
-                       panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
+       /// Claims an HTLC while we're disconnected from a peer, dropping the [`ChannelMonitorUpdate`]
+       /// entirely.
+       ///
+       /// The [`ChannelMonitor`] for this channel MUST be updated out-of-band with the preimage
+       /// provided (i.e. without calling [`crate::chain::Watch::update_channel`]).
+       ///
+       /// The HTLC claim will end up in the holding cell (because the caller must ensure the peer is
+       /// disconnected).
+       pub fn claim_htlc_while_disconnected_dropping_mon_update<L: Deref>
+               (&mut self, htlc_id_arg: u64, payment_preimage_arg: PaymentPreimage, logger: &L)
+       where L::Target: Logger {
+               // Assert that we'll add the HTLC claim to the holding cell in `get_update_fulfill_htlc`
+               // (see equivalent if condition there).
+               assert!(self.context.channel_state & (ChannelState::AwaitingRemoteRevoke as u32 | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress as u32) != 0);
+               let mon_update_id = self.context.latest_monitor_update_id; // Forget the ChannelMonitor update
+               let fulfill_resp = self.get_update_fulfill_htlc(htlc_id_arg, payment_preimage_arg, logger);
+               self.context.latest_monitor_update_id = mon_update_id;
+               if let UpdateFulfillFetch::NewClaim { msg, .. } = fulfill_resp {
+                       assert!(msg.is_none()); // The HTLC must have ended up in the holding cell.
                }
+       }
 
-               let funding_script = self.get_funding_redeemscript();
+       fn get_update_fulfill_htlc<L: Deref>(&mut self, htlc_id_arg: u64, payment_preimage_arg: PaymentPreimage, logger: &L) -> UpdateFulfillFetch where L::Target: Logger {
+               // Either ChannelReady 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.context.channel_state & (ChannelState::ChannelReady as u32)) != (ChannelState::ChannelReady as u32) {
+                       panic!("Was asked to fulfill an HTLC when channel was not in an operational state");
+               }
+               assert_eq!(self.context.channel_state & ChannelState::ShutdownComplete as u32, 0);
 
-               let counterparty_keys = self.build_remote_transaction_keys();
-               let counterparty_initial_commitment_tx = self.build_commitment_transaction(self.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, false, logger).tx;
-               let counterparty_trusted_tx = counterparty_initial_commitment_tx.trust();
-               let counterparty_initial_bitcoin_tx = counterparty_trusted_tx.built_transaction();
+               let payment_hash_calc = PaymentHash(Sha256::hash(&payment_preimage_arg.0[..]).into_inner());
 
-               log_trace!(logger, "Initial counterparty tx for channel {} is: txid {} tx {}",
-                       log_bytes!(self.channel_id()), counterparty_initial_bitcoin_tx.txid, encode::serialize_hex(&counterparty_initial_bitcoin_tx.transaction));
+               // 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 holder_signer = self.build_holder_transaction_keys(self.cur_holder_commitment_transaction_number);
-               let initial_commitment_tx = self.build_commitment_transaction(self.cur_holder_commitment_transaction_number, &holder_signer, true, false, logger).tx;
-               {
-                       let trusted_tx = initial_commitment_tx.trust();
-                       let initial_commitment_bitcoin_tx = trusted_tx.built_transaction();
-                       let sighash = initial_commitment_bitcoin_tx.get_sighash_all(&funding_script, self.channel_value_satoshis);
-                       // They sign our commitment transaction, allowing us to broadcast the tx if we wish.
-                       if let Err(_) = self.secp_ctx.verify_ecdsa(&sighash, &msg.signature, &self.get_counterparty_pubkeys().funding_pubkey) {
-                               return Err(ChannelError::Close("Invalid funding_signed signature from peer".to_owned()));
+               let mut pending_idx = core::usize::MAX;
+               let mut htlc_value_msat = 0;
+               for (idx, htlc) in self.context.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!(logger, "Have preimage and want to fulfill HTLC with payment hash {} we already failed against channel {}", log_bytes!(htlc.payment_hash.0), log_bytes!(self.context.channel_id()));
+                                                       debug_assert!(false, "Tried to fulfill an HTLC that was already failed");
+                                               }
+                                               return UpdateFulfillFetch::DuplicateClaim {};
+                                       },
+                                       _ => {
+                                               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;
+                               htlc_value_msat = htlc.amount_msat;
+                               break;
                        }
                }
+               if pending_idx == core::usize::MAX {
+                       #[cfg(any(test, fuzzing))]
+                       // If we failed to find an HTLC to fulfill, make sure it was previously fulfilled and
+                       // this is simply a duplicate claim, not previously failed and we lost funds.
+                       debug_assert!(self.context.historical_inbound_htlc_fulfills.contains(&htlc_id_arg));
+                       return UpdateFulfillFetch::DuplicateClaim {};
+               }
 
-               let holder_commitment_tx = HolderCommitmentTransaction::new(
-                       initial_commitment_tx,
-                       msg.signature,
-                       Vec::new(),
-                       &self.get_holder_pubkeys().funding_pubkey,
-                       self.counterparty_funding_pubkey()
-               );
-
-               self.holder_signer.validate_holder_commitment(&holder_commitment_tx, Vec::new())
-                       .map_err(|_| ChannelError::Close("Failed to validate our commitment".to_owned()))?;
-
+               // 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.context.latest_monitor_update_id += 1;
+               let monitor_update = ChannelMonitorUpdate {
+                       update_id: self.context.latest_monitor_update_id,
+                       updates: vec![ChannelMonitorUpdateStep::PaymentPreimage {
+                               payment_preimage: payment_preimage_arg.clone(),
+                       }],
+               };
 
-               let funding_redeemscript = self.get_funding_redeemscript();
-               let funding_txo = self.get_funding_txo().unwrap();
-               let funding_txo_script = funding_redeemscript.to_v0_p2wsh();
-               let obscure_factor = get_commitment_transaction_number_obscure_factor(&self.get_holder_pubkeys().payment_point, &self.get_counterparty_pubkeys().payment_point, self.is_outbound());
-               let shutdown_script = self.shutdown_scriptpubkey.clone().map(|script| script.into_inner());
-               let mut monitor_signer = signer_provider.derive_channel_signer(self.channel_value_satoshis, self.channel_keys_id);
-               monitor_signer.provide_channel_parameters(&self.channel_transaction_parameters);
-               let channel_monitor = ChannelMonitor::new(self.secp_ctx.clone(), monitor_signer,
-                                                         shutdown_script, self.get_holder_selected_contest_delay(),
-                                                         &self.destination_script, (funding_txo, funding_txo_script),
-                                                         &self.channel_transaction_parameters,
-                                                         funding_redeemscript.clone(), self.channel_value_satoshis,
-                                                         obscure_factor,
-                                                         holder_commitment_tx, best_block, self.counterparty_node_id);
+               if (self.context.channel_state & (ChannelState::AwaitingRemoteRevoke as u32 | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress as u32)) != 0 {
+                       // Note that this condition is the same as the assertion in
+                       // `claim_htlc_while_disconnected_dropping_mon_update` and must match exactly -
+                       // `claim_htlc_while_disconnected_dropping_mon_update` would not work correctly if we
+                       // do not not get into this branch.
+                       for pending_update in self.context.holding_cell_htlc_updates.iter() {
+                               match pending_update {
+                                       &HTLCUpdateAwaitingACK::ClaimHTLC { htlc_id, .. } => {
+                                               if htlc_id_arg == htlc_id {
+                                                       // Make sure we don't leave latest_monitor_update_id incremented here:
+                                                       self.context.latest_monitor_update_id -= 1;
+                                                       #[cfg(any(test, fuzzing))]
+                                                       debug_assert!(self.context.historical_inbound_htlc_fulfills.contains(&htlc_id_arg));
+                                                       return UpdateFulfillFetch::DuplicateClaim {};
+                                               }
+                                       },
+                                       &HTLCUpdateAwaitingACK::FailHTLC { htlc_id, .. } => {
+                                               if htlc_id_arg == htlc_id {
+                                                       log_warn!(logger, "Have preimage and want to fulfill HTLC with pending failure against channel {}", log_bytes!(self.context.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.
+                                                       debug_assert!(false, "Tried to fulfill an HTLC that was already failed");
+                                                       return UpdateFulfillFetch::NewClaim { monitor_update, htlc_value_msat, msg: None };
+                                               }
+                                       },
+                                       _ => {}
+                               }
+                       }
+                       log_trace!(logger, "Adding HTLC claim to holding_cell in channel {}! Current state: {}", log_bytes!(self.context.channel_id()), self.context.channel_state);
+                       self.context.holding_cell_htlc_updates.push(HTLCUpdateAwaitingACK::ClaimHTLC {
+                               payment_preimage: payment_preimage_arg, htlc_id: htlc_id_arg,
+                       });
+                       #[cfg(any(test, fuzzing))]
+                       self.context.historical_inbound_htlc_fulfills.insert(htlc_id_arg);
+                       return UpdateFulfillFetch::NewClaim { monitor_update, htlc_value_msat, msg: None };
+               }
+               #[cfg(any(test, fuzzing))]
+               self.context.historical_inbound_htlc_fulfills.insert(htlc_id_arg);
 
-               channel_monitor.provide_latest_counterparty_commitment_tx(counterparty_initial_bitcoin_tx.txid, Vec::new(), self.cur_counterparty_commitment_transaction_number, self.counterparty_cur_commitment_point.unwrap(), logger);
+               {
+                       let htlc = &mut self.context.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 UpdateFulfillFetch::NewClaim { monitor_update, htlc_value_msat, msg: None };
+                       }
+                       log_trace!(logger, "Upgrading HTLC {} to LocalRemoved with a Fulfill in channel {}!", log_bytes!(htlc.payment_hash.0), log_bytes!(self.context.channel_id));
+                       htlc.state = InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::Fulfill(payment_preimage_arg.clone()));
+               }
 
-               assert_eq!(self.channel_state & (ChannelState::MonitorUpdateInProgress as u32), 0); // We have no had any monitor(s) yet to fail update!
-               self.channel_state = ChannelState::FundingSent as u32;
-               self.cur_holder_commitment_transaction_number -= 1;
-               self.cur_counterparty_commitment_transaction_number -= 1;
+               UpdateFulfillFetch::NewClaim {
+                       monitor_update,
+                       htlc_value_msat,
+                       msg: Some(msgs::UpdateFulfillHTLC {
+                               channel_id: self.context.channel_id(),
+                               htlc_id: htlc_id_arg,
+                               payment_preimage: payment_preimage_arg,
+                       }),
+               }
+       }
 
-               log_info!(logger, "Received funding_signed from peer for channel {}", log_bytes!(self.channel_id()));
+       pub fn get_update_fulfill_htlc_and_commit<L: Deref>(&mut self, htlc_id: u64, payment_preimage: PaymentPreimage, logger: &L) -> UpdateFulfillCommitFetch where L::Target: Logger {
+               let release_cs_monitor = self.context.blocked_monitor_updates.is_empty();
+               match self.get_update_fulfill_htlc(htlc_id, payment_preimage, logger) {
+                       UpdateFulfillFetch::NewClaim { mut monitor_update, htlc_value_msat, msg } => {
+                               // Even if we aren't supposed to let new monitor updates with commitment state
+                               // updates run, we still need to push the preimage ChannelMonitorUpdateStep no
+                               // matter what. Sadly, to push a new monitor update which flies before others
+                               // already queued, we have to insert it into the pending queue and update the
+                               // update_ids of all the following monitors.
+                               if release_cs_monitor && msg.is_some() {
+                                       let mut additional_update = self.build_commitment_no_status_check(logger);
+                                       // build_commitment_no_status_check may bump latest_monitor_id but we want them
+                                       // to be strictly increasing by one, so decrement it here.
+                                       self.context.latest_monitor_update_id = monitor_update.update_id;
+                                       monitor_update.updates.append(&mut additional_update.updates);
+                               } else {
+                                       let new_mon_id = self.context.blocked_monitor_updates.get(0)
+                                               .map(|upd| upd.update.update_id).unwrap_or(monitor_update.update_id);
+                                       monitor_update.update_id = new_mon_id;
+                                       for held_update in self.context.blocked_monitor_updates.iter_mut() {
+                                               held_update.update.update_id += 1;
+                                       }
+                                       if msg.is_some() {
+                                               debug_assert!(false, "If there is a pending blocked monitor we should have MonitorUpdateInProgress set");
+                                               let update = self.build_commitment_no_status_check(logger);
+                                               self.context.blocked_monitor_updates.push(PendingChannelMonitorUpdate {
+                                                       update,
+                                               });
+                                       }
+                               }
 
-               let need_channel_ready = self.check_get_channel_ready(0).is_some();
-               self.monitor_updating_paused(false, false, need_channel_ready, Vec::new(), Vec::new(), Vec::new());
-               Ok(channel_monitor)
+                               self.monitor_updating_paused(false, msg.is_some(), false, Vec::new(), Vec::new(), Vec::new());
+                               UpdateFulfillCommitFetch::NewClaim { monitor_update, htlc_value_msat, }
+                       },
+                       UpdateFulfillFetch::DuplicateClaim {} => UpdateFulfillCommitFetch::DuplicateClaim {},
+               }
        }
 
-       /// Handles a channel_ready message from our peer. If we've already sent our channel_ready
-       /// and the channel is now usable (and public), this may generate an announcement_signatures to
-       /// reply with.
-       pub fn channel_ready<NS: Deref, L: Deref>(
-               &mut self, msg: &msgs::ChannelReady, node_signer: &NS, genesis_block_hash: BlockHash,
-               user_config: &UserConfig, best_block: &BestBlock, logger: &L
-       ) -> Result<Option<msgs::AnnouncementSignatures>, ChannelError>
-       where
-               NS::Target: NodeSigner,
-               L::Target: Logger
-       {
-               if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
-                       self.workaround_lnd_bug_4006 = Some(msg.clone());
-                       return Err(ChannelError::Ignore("Peer sent channel_ready when we needed a channel_reestablish. The peer is likely lnd, see https://github.com/lightningnetwork/lnd/issues/4006".to_owned()));
-               }
+       /// We can only have one resolution per HTLC. In some cases around reconnect, we may fulfill
+       /// an HTLC more than once or fulfill once and then attempt to fail after reconnect. We cannot,
+       /// however, fail more than once as we wait for an upstream failure to be irrevocably committed
+       /// before we fail backwards.
+       ///
+       /// If we do fail twice, we `debug_assert!(false)` and return `Ok(None)`. Thus, this will always
+       /// return `Ok(_)` if preconditions are met. In any case, `Err`s will only be
+       /// [`ChannelError::Ignore`].
+       pub fn queue_fail_htlc<L: Deref>(&mut self, htlc_id_arg: u64, err_packet: msgs::OnionErrorPacket, logger: &L)
+       -> Result<(), ChannelError> where L::Target: Logger {
+               self.fail_htlc(htlc_id_arg, err_packet, true, logger)
+                       .map(|msg_opt| assert!(msg_opt.is_none(), "We forced holding cell?"))
+       }
 
-               if let Some(scid_alias) = msg.short_channel_id_alias {
-                       if Some(scid_alias) != self.short_channel_id {
-                               // The scid alias provided can be used to route payments *from* our counterparty,
-                               // i.e. can be used for inbound payments and provided in invoices, but is not used
-                               // when routing outbound payments.
-                               self.latest_inbound_scid_alias = Some(scid_alias);
-                       }
+       /// We can only have one resolution per HTLC. In some cases around reconnect, we may fulfill
+       /// an HTLC more than once or fulfill once and then attempt to fail after reconnect. We cannot,
+       /// however, fail more than once as we wait for an upstream failure to be irrevocably committed
+       /// before we fail backwards.
+       ///
+       /// If we do fail twice, we `debug_assert!(false)` and return `Ok(None)`. Thus, this will always
+       /// return `Ok(_)` if preconditions are met. In any case, `Err`s will only be
+       /// [`ChannelError::Ignore`].
+       fn fail_htlc<L: Deref>(&mut self, htlc_id_arg: u64, err_packet: msgs::OnionErrorPacket, mut force_holding_cell: bool, logger: &L)
+       -> Result<Option<msgs::UpdateFailHTLC>, ChannelError> where L::Target: Logger {
+               if (self.context.channel_state & (ChannelState::ChannelReady as u32)) != (ChannelState::ChannelReady as u32) {
+                       panic!("Was asked to fail an HTLC when channel was not in an operational state");
                }
+               assert_eq!(self.context.channel_state & ChannelState::ShutdownComplete as u32, 0);
 
-               let non_shutdown_state = self.channel_state & (!MULTI_STATE_FLAGS);
+               // 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.
 
-               if non_shutdown_state == ChannelState::FundingSent as u32 {
-                       self.channel_state |= ChannelState::TheirChannelReady as u32;
-               } else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::OurChannelReady as u32) {
-                       self.channel_state = ChannelState::ChannelReady as u32 | (self.channel_state & MULTI_STATE_FLAGS);
-                       self.update_time_counter += 1;
-               } else if self.channel_state & (ChannelState::ChannelReady as u32) != 0 ||
-                       // If we reconnected before sending our `channel_ready` they may still resend theirs:
-                       (self.channel_state & (ChannelState::FundingSent as u32 | ChannelState::TheirChannelReady as u32) ==
-                                             (ChannelState::FundingSent as u32 | ChannelState::TheirChannelReady as u32))
-               {
-                       // They probably disconnected/reconnected and re-sent the channel_ready, which is
-                       // required, or they're sending a fresh SCID alias.
-                       let expected_point =
-                               if self.cur_counterparty_commitment_transaction_number == INITIAL_COMMITMENT_NUMBER - 1 {
-                                       // If they haven't ever sent an updated point, the point they send should match
-                                       // the current one.
-                                       self.counterparty_cur_commitment_point
-                               } else if self.cur_counterparty_commitment_transaction_number == INITIAL_COMMITMENT_NUMBER - 2 {
-                                       // If we've advanced the commitment number once, the second commitment point is
-                                       // at `counterparty_prev_commitment_point`, which is not yet revoked.
-                                       debug_assert!(self.counterparty_prev_commitment_point.is_some());
-                                       self.counterparty_prev_commitment_point
-                               } else {
-                                       // If they have sent updated points, channel_ready is always supposed to match
-                                       // their "first" point, which we re-derive here.
-                                       Some(PublicKey::from_secret_key(&self.secp_ctx, &SecretKey::from_slice(
-                                                       &self.commitment_secrets.get_secret(INITIAL_COMMITMENT_NUMBER - 1).expect("We should have all prev secrets available")
-                                               ).expect("We already advanced, so previous secret keys should have been validated already")))
-                               };
-                       if expected_point != Some(msg.next_per_commitment_point) {
-                               return Err(ChannelError::Close("Peer sent a reconnect channel_ready with a different point".to_owned()));
+               let mut pending_idx = core::usize::MAX;
+               for (idx, htlc) in self.context.pending_inbound_htlcs.iter().enumerate() {
+                       if htlc.htlc_id == htlc_id_arg {
+                               match htlc.state {
+                                       InboundHTLCState::Committed => {},
+                                       InboundHTLCState::LocalRemoved(ref reason) => {
+                                               if let &InboundHTLCRemovalReason::Fulfill(_) = reason {
+                                               } else {
+                                                       debug_assert!(false, "Tried to fail an HTLC that was already failed");
+                                               }
+                                               return Ok(None);
+                                       },
+                                       _ => {
+                                               debug_assert!(false, "Have an inbound HTLC we tried to claim before it was fully committed to");
+                                               return Err(ChannelError::Ignore(format!("Unable to find a pending HTLC which matched the given HTLC ID ({})", htlc.htlc_id)));
+                                       }
+                               }
+                               pending_idx = idx;
                        }
+               }
+               if pending_idx == core::usize::MAX {
+                       #[cfg(any(test, fuzzing))]
+                       // If we failed to find an HTLC to fail, make sure it was previously fulfilled and this
+                       // is simply a duplicate fail, not previously failed and we failed-back too early.
+                       debug_assert!(self.context.historical_inbound_htlc_fulfills.contains(&htlc_id_arg));
                        return Ok(None);
-               } else {
-                       return Err(ChannelError::Close("Peer sent a channel_ready at a strange time".to_owned()));
                }
 
-               self.counterparty_prev_commitment_point = self.counterparty_cur_commitment_point;
-               self.counterparty_cur_commitment_point = Some(msg.next_per_commitment_point);
+               if (self.context.channel_state & (ChannelState::AwaitingRemoteRevoke as u32 | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress as u32)) != 0 {
+                       debug_assert!(force_holding_cell, "!force_holding_cell is only called when emptying the holding cell, so we shouldn't end up back in it!");
+                       force_holding_cell = true;
+               }
 
-               log_info!(logger, "Received channel_ready from peer for channel {}", log_bytes!(self.channel_id()));
+               // Now update local state:
+               if force_holding_cell {
+                       for pending_update in self.context.holding_cell_htlc_updates.iter() {
+                               match pending_update {
+                                       &HTLCUpdateAwaitingACK::ClaimHTLC { htlc_id, .. } => {
+                                               if htlc_id_arg == htlc_id {
+                                                       #[cfg(any(test, fuzzing))]
+                                                       debug_assert!(self.context.historical_inbound_htlc_fulfills.contains(&htlc_id_arg));
+                                                       return Ok(None);
+                                               }
+                                       },
+                                       &HTLCUpdateAwaitingACK::FailHTLC { htlc_id, .. } => {
+                                               if htlc_id_arg == htlc_id {
+                                                       debug_assert!(false, "Tried to fail an HTLC that was already failed");
+                                                       return Err(ChannelError::Ignore("Unable to find a pending HTLC which matched the given HTLC ID".to_owned()));
+                                               }
+                                       },
+                                       _ => {}
+                               }
+                       }
+                       log_trace!(logger, "Placing failure for HTLC ID {} in holding cell in channel {}.", htlc_id_arg, log_bytes!(self.context.channel_id()));
+                       self.context.holding_cell_htlc_updates.push(HTLCUpdateAwaitingACK::FailHTLC {
+                               htlc_id: htlc_id_arg,
+                               err_packet,
+                       });
+                       return Ok(None);
+               }
 
-               Ok(self.get_announcement_sigs(node_signer, genesis_block_hash, user_config, best_block.height(), logger))
+               log_trace!(logger, "Failing HTLC ID {} back with a update_fail_htlc message in channel {}.", htlc_id_arg, log_bytes!(self.context.channel_id()));
+               {
+                       let htlc = &mut self.context.pending_inbound_htlcs[pending_idx];
+                       htlc.state = InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::FailRelay(err_packet.clone()));
+               }
+
+               Ok(Some(msgs::UpdateFailHTLC {
+                       channel_id: self.context.channel_id(),
+                       htlc_id: htlc_id_arg,
+                       reason: err_packet
+               }))
        }
 
-       /// Returns transaction if there is pending funding transaction that is yet to broadcast
-       pub fn unbroadcasted_funding(&self) -> Option<Transaction> {
-               if self.channel_state & (ChannelState::FundingCreated as u32) != 0 {
-                       self.funding_transaction.clone()
-               } else {
-                       None
+       // Message handlers:
+
+       /// 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<SP: Deref, L: Deref>(
+               &mut self, msg: &msgs::FundingSigned, best_block: BestBlock, signer_provider: &SP, logger: &L
+       ) -> Result<ChannelMonitor<Signer>, ChannelError>
+       where
+               SP::Target: SignerProvider<Signer = Signer>,
+               L::Target: Logger
+       {
+               if !self.context.is_outbound() {
+                       return Err(ChannelError::Close("Received funding_signed for an inbound channel?".to_owned()));
+               }
+               if self.context.channel_state & !(ChannelState::MonitorUpdateInProgress as u32) != ChannelState::FundingCreated as u32 {
+                       return Err(ChannelError::Close("Received funding_signed in strange state!".to_owned()));
+               }
+               if self.context.commitment_secrets.get_min_seen_secret() != (1 << 48) ||
+                               self.context.cur_counterparty_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER ||
+                               self.context.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
+                       panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
                }
-       }
 
-       /// Returns a HTLCStats about inbound pending htlcs
-       fn get_inbound_pending_htlc_stats(&self, outbound_feerate_update: Option<u32>) -> HTLCStats {
-               let mut stats = HTLCStats {
-                       pending_htlcs: self.pending_inbound_htlcs.len() as u32,
-                       pending_htlcs_value_msat: 0,
-                       on_counterparty_tx_dust_exposure_msat: 0,
-                       on_holder_tx_dust_exposure_msat: 0,
-                       holding_cell_msat: 0,
-                       on_holder_tx_holding_cell_htlcs_count: 0,
-               };
+               let funding_script = self.context.get_funding_redeemscript();
 
-               let (htlc_timeout_dust_limit, htlc_success_dust_limit) = if self.opt_anchors() {
-                       (0, 0)
-               } else {
-                       let dust_buffer_feerate = self.get_dust_buffer_feerate(outbound_feerate_update) as u64;
-                       (dust_buffer_feerate * htlc_timeout_tx_weight(false) / 1000,
-                               dust_buffer_feerate * htlc_success_tx_weight(false) / 1000)
-               };
-               let counterparty_dust_limit_timeout_sat = htlc_timeout_dust_limit + self.counterparty_dust_limit_satoshis;
-               let holder_dust_limit_success_sat = htlc_success_dust_limit + self.holder_dust_limit_satoshis;
-               for ref htlc in self.pending_inbound_htlcs.iter() {
-                       stats.pending_htlcs_value_msat += htlc.amount_msat;
-                       if htlc.amount_msat / 1000 < counterparty_dust_limit_timeout_sat {
-                               stats.on_counterparty_tx_dust_exposure_msat += htlc.amount_msat;
-                       }
-                       if htlc.amount_msat / 1000 < holder_dust_limit_success_sat {
-                               stats.on_holder_tx_dust_exposure_msat += htlc.amount_msat;
+               let counterparty_keys = self.context.build_remote_transaction_keys();
+               let counterparty_initial_commitment_tx = self.context.build_commitment_transaction(self.context.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, false, logger).tx;
+               let counterparty_trusted_tx = counterparty_initial_commitment_tx.trust();
+               let counterparty_initial_bitcoin_tx = counterparty_trusted_tx.built_transaction();
+
+               log_trace!(logger, "Initial counterparty tx for channel {} is: txid {} tx {}",
+                       log_bytes!(self.context.channel_id()), counterparty_initial_bitcoin_tx.txid, encode::serialize_hex(&counterparty_initial_bitcoin_tx.transaction));
+
+               let holder_signer = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number);
+               let initial_commitment_tx = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &holder_signer, true, false, logger).tx;
+               {
+                       let trusted_tx = initial_commitment_tx.trust();
+                       let initial_commitment_bitcoin_tx = trusted_tx.built_transaction();
+                       let sighash = initial_commitment_bitcoin_tx.get_sighash_all(&funding_script, self.context.channel_value_satoshis);
+                       // They sign our commitment transaction, allowing us to broadcast the tx if we wish.
+                       if let Err(_) = self.context.secp_ctx.verify_ecdsa(&sighash, &msg.signature, &self.context.get_counterparty_pubkeys().funding_pubkey) {
+                               return Err(ChannelError::Close("Invalid funding_signed signature from peer".to_owned()));
                        }
                }
-               stats
-       }
 
-       /// Returns a HTLCStats about pending outbound htlcs, *including* pending adds in our holding cell.
-       fn get_outbound_pending_htlc_stats(&self, outbound_feerate_update: Option<u32>) -> HTLCStats {
-               let mut stats = HTLCStats {
-                       pending_htlcs: self.pending_outbound_htlcs.len() as u32,
-                       pending_htlcs_value_msat: 0,
-                       on_counterparty_tx_dust_exposure_msat: 0,
-                       on_holder_tx_dust_exposure_msat: 0,
-                       holding_cell_msat: 0,
-                       on_holder_tx_holding_cell_htlcs_count: 0,
-               };
+               let holder_commitment_tx = HolderCommitmentTransaction::new(
+                       initial_commitment_tx,
+                       msg.signature,
+                       Vec::new(),
+                       &self.context.get_holder_pubkeys().funding_pubkey,
+                       self.context.counterparty_funding_pubkey()
+               );
 
-               let (htlc_timeout_dust_limit, htlc_success_dust_limit) = if self.opt_anchors() {
-                       (0, 0)
-               } else {
-                       let dust_buffer_feerate = self.get_dust_buffer_feerate(outbound_feerate_update) as u64;
-                       (dust_buffer_feerate * htlc_timeout_tx_weight(false) / 1000,
-                               dust_buffer_feerate * htlc_success_tx_weight(false) / 1000)
-               };
-               let counterparty_dust_limit_success_sat = htlc_success_dust_limit + self.counterparty_dust_limit_satoshis;
-               let holder_dust_limit_timeout_sat = htlc_timeout_dust_limit + self.holder_dust_limit_satoshis;
-               for ref htlc in self.pending_outbound_htlcs.iter() {
-                       stats.pending_htlcs_value_msat += htlc.amount_msat;
-                       if htlc.amount_msat / 1000 < counterparty_dust_limit_success_sat {
-                               stats.on_counterparty_tx_dust_exposure_msat += htlc.amount_msat;
-                       }
-                       if htlc.amount_msat / 1000 < holder_dust_limit_timeout_sat {
-                               stats.on_holder_tx_dust_exposure_msat += htlc.amount_msat;
-                       }
+               self.context.holder_signer.validate_holder_commitment(&holder_commitment_tx, Vec::new())
+                       .map_err(|_| ChannelError::Close("Failed to validate our commitment".to_owned()))?;
+
+
+               let funding_redeemscript = self.context.get_funding_redeemscript();
+               let funding_txo = self.context.get_funding_txo().unwrap();
+               let funding_txo_script = funding_redeemscript.to_v0_p2wsh();
+               let obscure_factor = get_commitment_transaction_number_obscure_factor(&self.context.get_holder_pubkeys().payment_point, &self.context.get_counterparty_pubkeys().payment_point, self.context.is_outbound());
+               let shutdown_script = self.context.shutdown_scriptpubkey.clone().map(|script| script.into_inner());
+               let mut monitor_signer = signer_provider.derive_channel_signer(self.context.channel_value_satoshis, self.context.channel_keys_id);
+               monitor_signer.provide_channel_parameters(&self.context.channel_transaction_parameters);
+               let channel_monitor = ChannelMonitor::new(self.context.secp_ctx.clone(), monitor_signer,
+                                                         shutdown_script, self.context.get_holder_selected_contest_delay(),
+                                                         &self.context.destination_script, (funding_txo, funding_txo_script),
+                                                         &self.context.channel_transaction_parameters,
+                                                         funding_redeemscript.clone(), self.context.channel_value_satoshis,
+                                                         obscure_factor,
+                                                         holder_commitment_tx, best_block, self.context.counterparty_node_id);
+
+               channel_monitor.provide_latest_counterparty_commitment_tx(counterparty_initial_bitcoin_tx.txid, Vec::new(), self.context.cur_counterparty_commitment_transaction_number, self.context.counterparty_cur_commitment_point.unwrap(), logger);
+
+               assert_eq!(self.context.channel_state & (ChannelState::MonitorUpdateInProgress as u32), 0); // We have no had any monitor(s) yet to fail update!
+               self.context.channel_state = ChannelState::FundingSent as u32;
+               self.context.cur_holder_commitment_transaction_number -= 1;
+               self.context.cur_counterparty_commitment_transaction_number -= 1;
+
+               log_info!(logger, "Received funding_signed from peer for channel {}", log_bytes!(self.context.channel_id()));
+
+               let need_channel_ready = self.check_get_channel_ready(0).is_some();
+               self.monitor_updating_paused(false, false, need_channel_ready, Vec::new(), Vec::new(), Vec::new());
+               Ok(channel_monitor)
+       }
+
+       /// Handles a channel_ready message from our peer. If we've already sent our channel_ready
+       /// and the channel is now usable (and public), this may generate an announcement_signatures to
+       /// reply with.
+       pub fn channel_ready<NS: Deref, L: Deref>(
+               &mut self, msg: &msgs::ChannelReady, node_signer: &NS, genesis_block_hash: BlockHash,
+               user_config: &UserConfig, best_block: &BestBlock, logger: &L
+       ) -> Result<Option<msgs::AnnouncementSignatures>, ChannelError>
+       where
+               NS::Target: NodeSigner,
+               L::Target: Logger
+       {
+               if self.context.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
+                       self.context.workaround_lnd_bug_4006 = Some(msg.clone());
+                       return Err(ChannelError::Ignore("Peer sent channel_ready when we needed a channel_reestablish. The peer is likely lnd, see https://github.com/lightningnetwork/lnd/issues/4006".to_owned()));
                }
 
-               for update in self.holding_cell_htlc_updates.iter() {
-                       if let &HTLCUpdateAwaitingACK::AddHTLC { ref amount_msat, .. } = update {
-                               stats.pending_htlcs += 1;
-                               stats.pending_htlcs_value_msat += amount_msat;
-                               stats.holding_cell_msat += amount_msat;
-                               if *amount_msat / 1000 < counterparty_dust_limit_success_sat {
-                                       stats.on_counterparty_tx_dust_exposure_msat += amount_msat;
-                               }
-                               if *amount_msat / 1000 < holder_dust_limit_timeout_sat {
-                                       stats.on_holder_tx_dust_exposure_msat += amount_msat;
-                               } else {
-                                       stats.on_holder_tx_holding_cell_htlcs_count += 1;
-                               }
+               if let Some(scid_alias) = msg.short_channel_id_alias {
+                       if Some(scid_alias) != self.context.short_channel_id {
+                               // The scid alias provided can be used to route payments *from* our counterparty,
+                               // i.e. can be used for inbound payments and provided in invoices, but is not used
+                               // when routing outbound payments.
+                               self.context.latest_inbound_scid_alias = Some(scid_alias);
                        }
                }
-               stats
-       }
 
-       /// Get the available balances, see [`AvailableBalances`]'s fields for more info.
-       /// 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_available_balances(&self) -> AvailableBalances {
-               // Note that we have to handle overflow due to the above case.
-               let outbound_stats = self.get_outbound_pending_htlc_stats(None);
+               let non_shutdown_state = self.context.channel_state & (!MULTI_STATE_FLAGS);
 
-               let mut balance_msat = self.value_to_self_msat;
-               for ref htlc in self.pending_inbound_htlcs.iter() {
-                       if let InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::Fulfill(_)) = htlc.state {
-                               balance_msat += htlc.amount_msat;
+               if non_shutdown_state == ChannelState::FundingSent as u32 {
+                       self.context.channel_state |= ChannelState::TheirChannelReady as u32;
+               } else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::OurChannelReady as u32) {
+                       self.context.channel_state = ChannelState::ChannelReady as u32 | (self.context.channel_state & MULTI_STATE_FLAGS);
+                       self.context.update_time_counter += 1;
+               } else if self.context.channel_state & (ChannelState::ChannelReady as u32) != 0 ||
+                       // If we reconnected before sending our `channel_ready` they may still resend theirs:
+                       (self.context.channel_state & (ChannelState::FundingSent as u32 | ChannelState::TheirChannelReady as u32) ==
+                                             (ChannelState::FundingSent as u32 | ChannelState::TheirChannelReady as u32))
+               {
+                       // They probably disconnected/reconnected and re-sent the channel_ready, which is
+                       // required, or they're sending a fresh SCID alias.
+                       let expected_point =
+                               if self.context.cur_counterparty_commitment_transaction_number == INITIAL_COMMITMENT_NUMBER - 1 {
+                                       // If they haven't ever sent an updated point, the point they send should match
+                                       // the current one.
+                                       self.context.counterparty_cur_commitment_point
+                               } else if self.context.cur_counterparty_commitment_transaction_number == INITIAL_COMMITMENT_NUMBER - 2 {
+                                       // If we've advanced the commitment number once, the second commitment point is
+                                       // at `counterparty_prev_commitment_point`, which is not yet revoked.
+                                       debug_assert!(self.context.counterparty_prev_commitment_point.is_some());
+                                       self.context.counterparty_prev_commitment_point
+                               } else {
+                                       // If they have sent updated points, channel_ready is always supposed to match
+                                       // their "first" point, which we re-derive here.
+                                       Some(PublicKey::from_secret_key(&self.context.secp_ctx, &SecretKey::from_slice(
+                                                       &self.context.commitment_secrets.get_secret(INITIAL_COMMITMENT_NUMBER - 1).expect("We should have all prev secrets available")
+                                               ).expect("We already advanced, so previous secret keys should have been validated already")))
+                               };
+                       if expected_point != Some(msg.next_per_commitment_point) {
+                               return Err(ChannelError::Close("Peer sent a reconnect channel_ready with a different point".to_owned()));
                        }
+                       return Ok(None);
+               } else {
+                       return Err(ChannelError::Close("Peer sent a channel_ready at a strange time".to_owned()));
                }
-               balance_msat -= outbound_stats.pending_htlcs_value_msat;
 
-               let outbound_capacity_msat = cmp::max(self.value_to_self_msat as i64
-                               - outbound_stats.pending_htlcs_value_msat as i64
-                               - self.counterparty_selected_channel_reserve_satoshis.unwrap_or(0) as i64 * 1000,
-                       0) as u64;
-               AvailableBalances {
-                       inbound_capacity_msat: cmp::max(self.channel_value_satoshis as i64 * 1000
-                                       - self.value_to_self_msat as i64
-                                       - self.get_inbound_pending_htlc_stats(None).pending_htlcs_value_msat as i64
-                                       - self.holder_selected_channel_reserve_satoshis as i64 * 1000,
-                               0) as u64,
-                       outbound_capacity_msat,
-                       next_outbound_htlc_limit_msat: cmp::max(cmp::min(outbound_capacity_msat as i64,
-                                       self.counterparty_max_htlc_value_in_flight_msat as i64
-                                               - outbound_stats.pending_htlcs_value_msat as i64),
-                               0) as u64,
-                       balance_msat,
-               }
-       }
+               self.context.counterparty_prev_commitment_point = self.context.counterparty_cur_commitment_point;
+               self.context.counterparty_cur_commitment_point = Some(msg.next_per_commitment_point);
 
-       pub fn get_holder_counterparty_selected_channel_reserve_satoshis(&self) -> (u64, Option<u64>) {
-               (self.holder_selected_channel_reserve_satoshis, self.counterparty_selected_channel_reserve_satoshis)
-       }
+               log_info!(logger, "Received channel_ready from peer for channel {}", log_bytes!(self.context.channel_id()));
 
-       // Get the fee cost in MSATS of a commitment tx with a given number of HTLC outputs.
-       // Note that num_htlcs should not include dust HTLCs.
-       fn commit_tx_fee_msat(feerate_per_kw: u32, num_htlcs: usize, opt_anchors: bool) -> u64 {
-               // Note that we need to divide before multiplying to round properly,
-               // since the lowest denomination of bitcoin on-chain is the satoshi.
-               (commitment_tx_base_weight(opt_anchors) + num_htlcs as u64 * COMMITMENT_TX_WEIGHT_PER_HTLC) * feerate_per_kw as u64 / 1000 * 1000
+               Ok(self.get_announcement_sigs(node_signer, genesis_block_hash, user_config, best_block.height(), logger))
        }
 
-       // Get the fee cost in SATS of a commitment tx with a given number of HTLC outputs.
-       // Note that num_htlcs should not include dust HTLCs.
-       #[inline]
-       fn commit_tx_fee_sat(feerate_per_kw: u32, num_htlcs: usize, opt_anchors: bool) -> u64 {
-               feerate_per_kw as u64 * (commitment_tx_base_weight(opt_anchors) + num_htlcs as u64 * COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000
-       }
+       pub fn update_add_htlc<F, FE: Deref, L: Deref>(
+               &mut self, msg: &msgs::UpdateAddHTLC, mut pending_forward_status: PendingHTLCStatus,
+               create_pending_htlc_status: F, fee_estimator: &LowerBoundedFeeEstimator<FE>, logger: &L
+       ) -> Result<(), ChannelError>
+       where F: for<'a> Fn(&'a Self, PendingHTLCStatus, u16) -> PendingHTLCStatus,
+               FE::Target: FeeEstimator, L::Target: Logger,
+       {
+               // We can't accept HTLCs sent after we've sent a shutdown.
+               let local_sent_shutdown = (self.context.channel_state & (ChannelState::ChannelReady as u32 | ChannelState::LocalShutdownSent as u32)) != (ChannelState::ChannelReady as u32);
+               if local_sent_shutdown {
+                       pending_forward_status = create_pending_htlc_status(self, pending_forward_status, 0x4000|8);
+               }
+               // If the remote has sent a shutdown prior to adding this HTLC, then they are in violation of the spec.
+               let remote_sent_shutdown = (self.context.channel_state & (ChannelState::ChannelReady as u32 | ChannelState::RemoteShutdownSent as u32)) != (ChannelState::ChannelReady as u32);
+               if remote_sent_shutdown {
+                       return Err(ChannelError::Close("Got add HTLC message when channel was not in an operational state".to_owned()));
+               }
+               if self.context.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".to_owned()));
+               }
+               if msg.amount_msat > self.context.channel_value_satoshis * 1000 {
+                       return Err(ChannelError::Close("Remote side tried to send more than the total value of the channel".to_owned()));
+               }
+               if msg.amount_msat == 0 {
+                       return Err(ChannelError::Close("Remote side tried to send a 0-msat HTLC".to_owned()));
+               }
+               if msg.amount_msat < self.context.holder_htlc_minimum_msat {
+                       return Err(ChannelError::Close(format!("Remote side tried to send less than our minimum HTLC value. Lower limit: ({}). Actual: ({})", self.context.holder_htlc_minimum_msat, msg.amount_msat)));
+               }
 
-       // Get the commitment tx fee for the local's (i.e. our) next commitment transaction based on the
-       // number of pending HTLCs that are on track to be in our next commitment tx, plus an additional
-       // HTLC if `fee_spike_buffer_htlc` is Some, plus a new HTLC given by `new_htlc_amount`. Dust HTLCs
-       // are excluded.
-       fn next_local_commit_tx_fee_msat(&self, htlc: HTLCCandidate, fee_spike_buffer_htlc: Option<()>) -> u64 {
-               assert!(self.is_outbound());
+               let inbound_stats = self.context.get_inbound_pending_htlc_stats(None);
+               let outbound_stats = self.context.get_outbound_pending_htlc_stats(None);
+               if inbound_stats.pending_htlcs + 1 > self.context.holder_max_accepted_htlcs as u32 {
+                       return Err(ChannelError::Close(format!("Remote tried to push more than our max accepted HTLCs ({})", self.context.holder_max_accepted_htlcs)));
+               }
+               if inbound_stats.pending_htlcs_value_msat + msg.amount_msat > self.context.holder_max_htlc_value_in_flight_msat {
+                       return Err(ChannelError::Close(format!("Remote HTLC add would put them over our max HTLC value ({})", self.context.holder_max_htlc_value_in_flight_msat)));
+               }
+               // Check holder_selected_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.context.pending_outbound_htlcs.iter() {
+                       if let OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_)) = htlc.state {
+                               removed_outbound_total_msat += htlc.amount_msat;
+                       } else if let OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_)) = htlc.state {
+                               removed_outbound_total_msat += htlc.amount_msat;
+                       }
+               }
 
-               let (htlc_success_dust_limit, htlc_timeout_dust_limit) = if self.opt_anchors() {
+               let max_dust_htlc_exposure_msat = self.context.get_max_dust_htlc_exposure_msat(fee_estimator);
+               let (htlc_timeout_dust_limit, htlc_success_dust_limit) = if self.context.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
                        (0, 0)
                } else {
-                       (self.feerate_per_kw as u64 * htlc_success_tx_weight(false) / 1000,
-                               self.feerate_per_kw as u64 * htlc_timeout_tx_weight(false) / 1000)
+                       let dust_buffer_feerate = self.context.get_dust_buffer_feerate(None) as u64;
+                       (dust_buffer_feerate * htlc_timeout_tx_weight(self.context.get_channel_type()) / 1000,
+                               dust_buffer_feerate * htlc_success_tx_weight(self.context.get_channel_type()) / 1000)
                };
-               let real_dust_limit_success_sat = htlc_success_dust_limit + self.holder_dust_limit_satoshis;
-               let real_dust_limit_timeout_sat = htlc_timeout_dust_limit + self.holder_dust_limit_satoshis;
-
-               let mut addl_htlcs = 0;
-               if fee_spike_buffer_htlc.is_some() { addl_htlcs += 1; }
-               match htlc.origin {
-                       HTLCInitiator::LocalOffered => {
-                               if htlc.amount_msat / 1000 >= real_dust_limit_timeout_sat {
-                                       addl_htlcs += 1;
-                               }
-                       },
-                       HTLCInitiator::RemoteOffered => {
-                               if htlc.amount_msat / 1000 >= real_dust_limit_success_sat {
-                                       addl_htlcs += 1;
-                               }
+               let exposure_dust_limit_timeout_sats = htlc_timeout_dust_limit + self.context.counterparty_dust_limit_satoshis;
+               if msg.amount_msat / 1000 < exposure_dust_limit_timeout_sats {
+                       let on_counterparty_tx_dust_htlc_exposure_msat = inbound_stats.on_counterparty_tx_dust_exposure_msat + outbound_stats.on_counterparty_tx_dust_exposure_msat + msg.amount_msat;
+                       if on_counterparty_tx_dust_htlc_exposure_msat > max_dust_htlc_exposure_msat {
+                               log_info!(logger, "Cannot accept value that would put our exposure to dust HTLCs at {} over the limit {} on counterparty commitment tx",
+                                       on_counterparty_tx_dust_htlc_exposure_msat, max_dust_htlc_exposure_msat);
+                               pending_forward_status = create_pending_htlc_status(self, pending_forward_status, 0x1000|7);
                        }
                }
 
-               let mut included_htlcs = 0;
-               for ref htlc in self.pending_inbound_htlcs.iter() {
-                       if htlc.amount_msat / 1000 < real_dust_limit_success_sat {
-                               continue
-                       }
-                       // We include LocalRemoved HTLCs here because we may still need to broadcast a commitment
-                       // transaction including this HTLC if it times out before they RAA.
-                       included_htlcs += 1;
-               }
-
-               for ref htlc in self.pending_outbound_htlcs.iter() {
-                       if htlc.amount_msat / 1000 < real_dust_limit_timeout_sat {
-                               continue
-                       }
-                       match htlc.state {
-                               OutboundHTLCState::LocalAnnounced {..} => included_htlcs += 1,
-                               OutboundHTLCState::Committed => included_htlcs += 1,
-                               OutboundHTLCState::RemoteRemoved {..} => included_htlcs += 1,
-                               // We don't include AwaitingRemoteRevokeToRemove HTLCs because our next commitment
-                               // transaction won't be generated until they send us their next RAA, which will mean
-                               // dropping any HTLCs in this state.
-                               _ => {},
-                       }
-               }
-
-               for htlc in self.holding_cell_htlc_updates.iter() {
-                       match htlc {
-                               &HTLCUpdateAwaitingACK::AddHTLC { amount_msat, .. } => {
-                                       if amount_msat / 1000 < real_dust_limit_timeout_sat {
-                                               continue
-                                       }
-                                       included_htlcs += 1
-                               },
-                               _ => {}, // Don't include claims/fails that are awaiting ack, because once we get the
-                                        // ack we're guaranteed to never include them in commitment txs anymore.
-                       }
-               }
-
-               let num_htlcs = included_htlcs + addl_htlcs;
-               let res = Self::commit_tx_fee_msat(self.feerate_per_kw, num_htlcs, self.opt_anchors());
-               #[cfg(any(test, fuzzing))]
-               {
-                       let mut fee = res;
-                       if fee_spike_buffer_htlc.is_some() {
-                               fee = Self::commit_tx_fee_msat(self.feerate_per_kw, num_htlcs - 1, self.opt_anchors());
-                       }
-                       let total_pending_htlcs = self.pending_inbound_htlcs.len() + self.pending_outbound_htlcs.len()
-                               + self.holding_cell_htlc_updates.len();
-                       let commitment_tx_info = CommitmentTxInfoCached {
-                               fee,
-                               total_pending_htlcs,
-                               next_holder_htlc_id: match htlc.origin {
-                                       HTLCInitiator::LocalOffered => self.next_holder_htlc_id + 1,
-                                       HTLCInitiator::RemoteOffered => self.next_holder_htlc_id,
-                               },
-                               next_counterparty_htlc_id: match htlc.origin {
-                                       HTLCInitiator::LocalOffered => self.next_counterparty_htlc_id,
-                                       HTLCInitiator::RemoteOffered => self.next_counterparty_htlc_id + 1,
-                               },
-                               feerate: self.feerate_per_kw,
-                       };
-                       *self.next_local_commitment_tx_fee_info_cached.lock().unwrap() = Some(commitment_tx_info);
-               }
-               res
-       }
-
-       // Get the commitment tx fee for the remote's next commitment transaction based on the number of
-       // pending HTLCs that are on track to be in their next commitment tx, plus an additional HTLC if
-       // `fee_spike_buffer_htlc` is Some, plus a new HTLC given by `new_htlc_amount`. Dust HTLCs are
-       // excluded.
-       fn next_remote_commit_tx_fee_msat(&self, htlc: HTLCCandidate, fee_spike_buffer_htlc: Option<()>) -> u64 {
-               assert!(!self.is_outbound());
-
-               let (htlc_success_dust_limit, htlc_timeout_dust_limit) = if self.opt_anchors() {
-                       (0, 0)
-               } else {
-                       (self.feerate_per_kw as u64 * htlc_success_tx_weight(false) / 1000,
-                               self.feerate_per_kw as u64 * htlc_timeout_tx_weight(false) / 1000)
-               };
-               let real_dust_limit_success_sat = htlc_success_dust_limit + self.counterparty_dust_limit_satoshis;
-               let real_dust_limit_timeout_sat = htlc_timeout_dust_limit + self.counterparty_dust_limit_satoshis;
-
-               let mut addl_htlcs = 0;
-               if fee_spike_buffer_htlc.is_some() { addl_htlcs += 1; }
-               match htlc.origin {
-                       HTLCInitiator::LocalOffered => {
-                               if htlc.amount_msat / 1000 >= real_dust_limit_success_sat {
-                                       addl_htlcs += 1;
-                               }
-                       },
-                       HTLCInitiator::RemoteOffered => {
-                               if htlc.amount_msat / 1000 >= real_dust_limit_timeout_sat {
-                                       addl_htlcs += 1;
-                               }
-                       }
-               }
-
-               // When calculating the set of HTLCs which will be included in their next commitment_signed, all
-               // non-dust inbound HTLCs are included (as all states imply it will be included) and only
-               // committed outbound HTLCs, see below.
-               let mut included_htlcs = 0;
-               for ref htlc in self.pending_inbound_htlcs.iter() {
-                       if htlc.amount_msat / 1000 <= real_dust_limit_timeout_sat {
-                               continue
-                       }
-                       included_htlcs += 1;
-               }
-
-               for ref htlc in self.pending_outbound_htlcs.iter() {
-                       if htlc.amount_msat / 1000 <= real_dust_limit_success_sat {
-                               continue
-                       }
-                       // We only include outbound HTLCs if it will not be included in their next commitment_signed,
-                       // i.e. if they've responded to us with an RAA after announcement.
-                       match htlc.state {
-                               OutboundHTLCState::Committed => included_htlcs += 1,
-                               OutboundHTLCState::RemoteRemoved {..} => included_htlcs += 1,
-                               OutboundHTLCState::LocalAnnounced { .. } => included_htlcs += 1,
-                               _ => {},
-                       }
-               }
-
-               let num_htlcs = included_htlcs + addl_htlcs;
-               let res = Self::commit_tx_fee_msat(self.feerate_per_kw, num_htlcs, self.opt_anchors());
-               #[cfg(any(test, fuzzing))]
-               {
-                       let mut fee = res;
-                       if fee_spike_buffer_htlc.is_some() {
-                               fee = Self::commit_tx_fee_msat(self.feerate_per_kw, num_htlcs - 1, self.opt_anchors());
-                       }
-                       let total_pending_htlcs = self.pending_inbound_htlcs.len() + self.pending_outbound_htlcs.len();
-                       let commitment_tx_info = CommitmentTxInfoCached {
-                               fee,
-                               total_pending_htlcs,
-                               next_holder_htlc_id: match htlc.origin {
-                                       HTLCInitiator::LocalOffered => self.next_holder_htlc_id + 1,
-                                       HTLCInitiator::RemoteOffered => self.next_holder_htlc_id,
-                               },
-                               next_counterparty_htlc_id: match htlc.origin {
-                                       HTLCInitiator::LocalOffered => self.next_counterparty_htlc_id,
-                                       HTLCInitiator::RemoteOffered => self.next_counterparty_htlc_id + 1,
-                               },
-                               feerate: self.feerate_per_kw,
-                       };
-                       *self.next_remote_commitment_tx_fee_info_cached.lock().unwrap() = Some(commitment_tx_info);
-               }
-               res
-       }
-
-       pub fn update_add_htlc<F, L: Deref>(&mut self, msg: &msgs::UpdateAddHTLC, mut pending_forward_status: PendingHTLCStatus, create_pending_htlc_status: F, logger: &L) -> Result<(), ChannelError>
-       where F: for<'a> Fn(&'a Self, PendingHTLCStatus, u16) -> PendingHTLCStatus, L::Target: Logger {
-               // We can't accept HTLCs sent after we've sent a shutdown.
-               let local_sent_shutdown = (self.channel_state & (ChannelState::ChannelReady as u32 | ChannelState::LocalShutdownSent as u32)) != (ChannelState::ChannelReady as u32);
-               if local_sent_shutdown {
-                       pending_forward_status = create_pending_htlc_status(self, pending_forward_status, 0x4000|8);
-               }
-               // If the remote has sent a shutdown prior to adding this HTLC, then they are in violation of the spec.
-               let remote_sent_shutdown = (self.channel_state & (ChannelState::ChannelReady as u32 | ChannelState::RemoteShutdownSent as u32)) != (ChannelState::ChannelReady as u32);
-               if remote_sent_shutdown {
-                       return Err(ChannelError::Close("Got add HTLC message when channel was not in an operational state".to_owned()));
-               }
-               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".to_owned()));
-               }
-               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".to_owned()));
-               }
-               if msg.amount_msat == 0 {
-                       return Err(ChannelError::Close("Remote side tried to send a 0-msat HTLC".to_owned()));
-               }
-               if msg.amount_msat < self.holder_htlc_minimum_msat {
-                       return Err(ChannelError::Close(format!("Remote side tried to send less than our minimum HTLC value. Lower limit: ({}). Actual: ({})", self.holder_htlc_minimum_msat, msg.amount_msat)));
-               }
-
-               let inbound_stats = self.get_inbound_pending_htlc_stats(None);
-               let outbound_stats = self.get_outbound_pending_htlc_stats(None);
-               if inbound_stats.pending_htlcs + 1 > self.holder_max_accepted_htlcs as u32 {
-                       return Err(ChannelError::Close(format!("Remote tried to push more than our max accepted HTLCs ({})", self.holder_max_accepted_htlcs)));
-               }
-               if inbound_stats.pending_htlcs_value_msat + msg.amount_msat > self.holder_max_htlc_value_in_flight_msat {
-                       return Err(ChannelError::Close(format!("Remote HTLC add would put them over our max HTLC value ({})", self.holder_max_htlc_value_in_flight_msat)));
-               }
-               // Check holder_selected_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(OutboundHTLCOutcome::Success(_)) = htlc.state {
-                               removed_outbound_total_msat += htlc.amount_msat;
-                       } else if let OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_)) = htlc.state {
-                               removed_outbound_total_msat += htlc.amount_msat;
-                       }
-               }
-
-               let (htlc_timeout_dust_limit, htlc_success_dust_limit) = if self.opt_anchors() {
-                       (0, 0)
-               } else {
-                       let dust_buffer_feerate = self.get_dust_buffer_feerate(None) as u64;
-                       (dust_buffer_feerate * htlc_timeout_tx_weight(false) / 1000,
-                               dust_buffer_feerate * htlc_success_tx_weight(false) / 1000)
-               };
-               let exposure_dust_limit_timeout_sats = htlc_timeout_dust_limit + self.counterparty_dust_limit_satoshis;
-               if msg.amount_msat / 1000 < exposure_dust_limit_timeout_sats {
-                       let on_counterparty_tx_dust_htlc_exposure_msat = inbound_stats.on_counterparty_tx_dust_exposure_msat + outbound_stats.on_counterparty_tx_dust_exposure_msat + msg.amount_msat;
-                       if on_counterparty_tx_dust_htlc_exposure_msat > self.get_max_dust_htlc_exposure_msat() {
-                               log_info!(logger, "Cannot accept value that would put our exposure to dust HTLCs at {} over the limit {} on counterparty commitment tx",
-                                       on_counterparty_tx_dust_htlc_exposure_msat, self.get_max_dust_htlc_exposure_msat());
-                               pending_forward_status = create_pending_htlc_status(self, pending_forward_status, 0x1000|7);
-                       }
-               }
-
-               let exposure_dust_limit_success_sats = htlc_success_dust_limit + self.holder_dust_limit_satoshis;
+               let exposure_dust_limit_success_sats = htlc_success_dust_limit + self.context.holder_dust_limit_satoshis;
                if msg.amount_msat / 1000 < exposure_dust_limit_success_sats {
                        let on_holder_tx_dust_htlc_exposure_msat = inbound_stats.on_holder_tx_dust_exposure_msat + outbound_stats.on_holder_tx_dust_exposure_msat + msg.amount_msat;
-                       if on_holder_tx_dust_htlc_exposure_msat > self.get_max_dust_htlc_exposure_msat() {
+                       if on_holder_tx_dust_htlc_exposure_msat > max_dust_htlc_exposure_msat {
                                log_info!(logger, "Cannot accept value that would put our exposure to dust HTLCs at {} over the limit {} on holder commitment tx",
-                                       on_holder_tx_dust_htlc_exposure_msat, self.get_max_dust_htlc_exposure_msat());
+                                       on_holder_tx_dust_htlc_exposure_msat, max_dust_htlc_exposure_msat);
                                pending_forward_status = create_pending_htlc_status(self, pending_forward_status, 0x1000|7);
                        }
                }
 
                let pending_value_to_self_msat =
-                       self.value_to_self_msat + inbound_stats.pending_htlcs_value_msat - removed_outbound_total_msat;
+                       self.context.value_to_self_msat + inbound_stats.pending_htlcs_value_msat - removed_outbound_total_msat;
                let pending_remote_value_msat =
-                       self.channel_value_satoshis * 1000 - pending_value_to_self_msat;
+                       self.context.channel_value_satoshis * 1000 - pending_value_to_self_msat;
                if pending_remote_value_msat < msg.amount_msat {
                        return Err(ChannelError::Close("Remote HTLC add would overdraw remaining funds".to_owned()));
                }
 
                // Check that the remote can afford to pay for this HTLC on-chain at the current
                // feerate_per_kw, while maintaining their channel reserve (as required by the spec).
-               let remote_commit_tx_fee_msat = if self.is_outbound() { 0 } else {
+               let remote_commit_tx_fee_msat = if self.context.is_outbound() { 0 } else {
                        let htlc_candidate = HTLCCandidate::new(msg.amount_msat, HTLCInitiator::RemoteOffered);
-                       self.next_remote_commit_tx_fee_msat(htlc_candidate, None) // Don't include the extra fee spike buffer HTLC in calculations
+                       self.context.next_remote_commit_tx_fee_msat(htlc_candidate, None) // Don't include the extra fee spike buffer HTLC in calculations
                };
                if pending_remote_value_msat - msg.amount_msat < remote_commit_tx_fee_msat {
                        return Err(ChannelError::Close("Remote HTLC add would not leave enough to pay for fees".to_owned()));
                };
 
-               if pending_remote_value_msat - msg.amount_msat - remote_commit_tx_fee_msat < self.holder_selected_channel_reserve_satoshis * 1000 {
+               if pending_remote_value_msat - msg.amount_msat - remote_commit_tx_fee_msat < self.context.holder_selected_channel_reserve_satoshis * 1000 {
                        return Err(ChannelError::Close("Remote HTLC add would put them under remote reserve value".to_owned()));
                }
 
-               if !self.is_outbound() {
+               if !self.context.is_outbound() {
                        // `2 *` and `Some(())` is for the fee spike buffer we keep for the remote. This deviates from
                        // the spec because in the spec, the fee spike buffer requirement doesn't exist on the
                        // receiver's side, only on the sender's.
@@ -3017,37 +2713,37 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                        // still be able to afford adding this HTLC plus one more future HTLC, regardless of being
                        // sensitive to fee spikes.
                        let htlc_candidate = HTLCCandidate::new(msg.amount_msat, HTLCInitiator::RemoteOffered);
-                       let remote_fee_cost_incl_stuck_buffer_msat = 2 * self.next_remote_commit_tx_fee_msat(htlc_candidate, Some(()));
-                       if pending_remote_value_msat - msg.amount_msat - self.holder_selected_channel_reserve_satoshis * 1000 < remote_fee_cost_incl_stuck_buffer_msat {
+                       let remote_fee_cost_incl_stuck_buffer_msat = 2 * self.context.next_remote_commit_tx_fee_msat(htlc_candidate, Some(()));
+                       if pending_remote_value_msat - msg.amount_msat - self.context.holder_selected_channel_reserve_satoshis * 1000 < remote_fee_cost_incl_stuck_buffer_msat {
                                // Note that if the pending_forward_status is not updated here, then it's because we're already failing
                                // the HTLC, i.e. its status is already set to failing.
-                               log_info!(logger, "Attempting to fail HTLC due to fee spike buffer violation in channel {}. Rebalancing is required.", log_bytes!(self.channel_id()));
+                               log_info!(logger, "Attempting to fail HTLC due to fee spike buffer violation in channel {}. Rebalancing is required.", log_bytes!(self.context.channel_id()));
                                pending_forward_status = create_pending_htlc_status(self, pending_forward_status, 0x1000|7);
                        }
                } else {
                        // Check that they won't violate our local required channel reserve by adding this HTLC.
                        let htlc_candidate = HTLCCandidate::new(msg.amount_msat, HTLCInitiator::RemoteOffered);
-                       let local_commit_tx_fee_msat = self.next_local_commit_tx_fee_msat(htlc_candidate, None);
-                       if self.value_to_self_msat < self.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000 + local_commit_tx_fee_msat {
+                       let local_commit_tx_fee_msat = self.context.next_local_commit_tx_fee_msat(htlc_candidate, None);
+                       if self.context.value_to_self_msat < self.context.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000 + local_commit_tx_fee_msat {
                                return Err(ChannelError::Close("Cannot accept HTLC that would put our balance under counterparty-announced channel reserve value".to_owned()));
                        }
                }
-               if self.next_counterparty_htlc_id != msg.htlc_id {
-                       return Err(ChannelError::Close(format!("Remote skipped HTLC ID (skipped ID: {})", self.next_counterparty_htlc_id)));
+               if self.context.next_counterparty_htlc_id != msg.htlc_id {
+                       return Err(ChannelError::Close(format!("Remote skipped HTLC ID (skipped ID: {})", self.context.next_counterparty_htlc_id)));
                }
                if msg.cltv_expiry >= 500000000 {
                        return Err(ChannelError::Close("Remote provided CLTV expiry in seconds instead of block height".to_owned()));
                }
 
-               if self.channel_state & ChannelState::LocalShutdownSent as u32 != 0 {
+               if self.context.channel_state & ChannelState::LocalShutdownSent as u32 != 0 {
                        if let PendingHTLCStatus::Forward(_) = pending_forward_status {
                                panic!("ChannelManager shouldn't be trying to add a forwardable HTLC after we've started closing");
                        }
                }
 
                // Now update local state:
-               self.next_counterparty_htlc_id += 1;
-               self.pending_inbound_htlcs.push(InboundHTLCOutput {
+               self.context.next_counterparty_htlc_id += 1;
+               self.context.pending_inbound_htlcs.push(InboundHTLCOutput {
                        htlc_id: msg.htlc_id,
                        amount_msat: msg.amount_msat,
                        payment_hash: msg.payment_hash,
@@ -3061,7 +2757,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
        #[inline]
        fn mark_outbound_htlc_removed(&mut self, htlc_id: u64, check_preimage: Option<PaymentPreimage>, fail_reason: Option<HTLCFailReason>) -> Result<&OutboundHTLCOutput, ChannelError> {
                assert!(!(check_preimage.is_some() && fail_reason.is_some()), "cannot fail while we have a preimage");
-               for htlc in self.pending_outbound_htlcs.iter_mut() {
+               for htlc in self.context.pending_outbound_htlcs.iter_mut() {
                        if htlc.htlc_id == htlc_id {
                                let outcome = match check_preimage {
                                        None => fail_reason.into(),
@@ -3089,10 +2785,10 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
        }
 
        pub fn update_fulfill_htlc(&mut self, msg: &msgs::UpdateFulfillHTLC) -> Result<(HTLCSource, u64), ChannelError> {
-               if (self.channel_state & (ChannelState::ChannelReady as u32)) != (ChannelState::ChannelReady as u32) {
+               if (self.context.channel_state & (ChannelState::ChannelReady as u32)) != (ChannelState::ChannelReady as u32) {
                        return Err(ChannelError::Close("Got fulfill HTLC message when channel was not in an operational state".to_owned()));
                }
-               if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
+               if self.context.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".to_owned()));
                }
 
@@ -3100,10 +2796,10 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
        }
 
        pub fn update_fail_htlc(&mut self, msg: &msgs::UpdateFailHTLC, fail_reason: HTLCFailReason) -> Result<(), ChannelError> {
-               if (self.channel_state & (ChannelState::ChannelReady as u32)) != (ChannelState::ChannelReady as u32) {
+               if (self.context.channel_state & (ChannelState::ChannelReady as u32)) != (ChannelState::ChannelReady as u32) {
                        return Err(ChannelError::Close("Got fail HTLC message when channel was not in an operational state".to_owned()));
                }
-               if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
+               if self.context.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".to_owned()));
                }
 
@@ -3112,10 +2808,10 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
        }
 
        pub fn update_fail_malformed_htlc(&mut self, msg: &msgs::UpdateFailMalformedHTLC, fail_reason: HTLCFailReason) -> Result<(), ChannelError> {
-               if (self.channel_state & (ChannelState::ChannelReady as u32)) != (ChannelState::ChannelReady as u32) {
+               if (self.context.channel_state & (ChannelState::ChannelReady as u32)) != (ChannelState::ChannelReady as u32) {
                        return Err(ChannelError::Close("Got fail malformed HTLC message when channel was not in an operational state".to_owned()));
                }
-               if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
+               if self.context.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".to_owned()));
                }
 
@@ -3123,34 +2819,34 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                Ok(())
        }
 
-       pub fn commitment_signed<L: Deref>(&mut self, msg: &msgs::CommitmentSigned, logger: &L) -> Result<Option<&ChannelMonitorUpdate>, ChannelError>
+       pub fn commitment_signed<L: Deref>(&mut self, msg: &msgs::CommitmentSigned, logger: &L) -> Result<Option<ChannelMonitorUpdate>, ChannelError>
                where L::Target: Logger
        {
-               if (self.channel_state & (ChannelState::ChannelReady as u32)) != (ChannelState::ChannelReady as u32) {
+               if (self.context.channel_state & (ChannelState::ChannelReady as u32)) != (ChannelState::ChannelReady as u32) {
                        return Err(ChannelError::Close("Got commitment signed message when channel was not in an operational state".to_owned()));
                }
-               if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
+               if self.context.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
                        return Err(ChannelError::Close("Peer sent commitment_signed when we needed a channel_reestablish".to_owned()));
                }
-               if self.channel_state & BOTH_SIDES_SHUTDOWN_MASK == BOTH_SIDES_SHUTDOWN_MASK && self.last_sent_closing_fee.is_some() {
+               if self.context.channel_state & BOTH_SIDES_SHUTDOWN_MASK == BOTH_SIDES_SHUTDOWN_MASK && self.context.last_sent_closing_fee.is_some() {
                        return Err(ChannelError::Close("Peer sent commitment_signed after we'd started exchanging closing_signeds".to_owned()));
                }
 
-               let funding_script = self.get_funding_redeemscript();
+               let funding_script = self.context.get_funding_redeemscript();
 
-               let keys = self.build_holder_transaction_keys(self.cur_holder_commitment_transaction_number);
+               let keys = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number);
 
-               let commitment_stats = self.build_commitment_transaction(self.cur_holder_commitment_transaction_number, &keys, true, false, logger);
+               let commitment_stats = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &keys, true, false, logger);
                let commitment_txid = {
                        let trusted_tx = commitment_stats.tx.trust();
                        let bitcoin_tx = trusted_tx.built_transaction();
-                       let sighash = bitcoin_tx.get_sighash_all(&funding_script, self.channel_value_satoshis);
+                       let sighash = bitcoin_tx.get_sighash_all(&funding_script, self.context.channel_value_satoshis);
 
                        log_trace!(logger, "Checking commitment tx signature {} by key {} against tx {} (sighash {}) with redeemscript {} in channel {}",
                                log_bytes!(msg.signature.serialize_compact()[..]),
-                               log_bytes!(self.counterparty_funding_pubkey().serialize()), encode::serialize_hex(&bitcoin_tx.transaction),
-                               log_bytes!(sighash[..]), encode::serialize_hex(&funding_script), log_bytes!(self.channel_id()));
-                       if let Err(_) = self.secp_ctx.verify_ecdsa(&sighash, &msg.signature, &self.counterparty_funding_pubkey()) {
+                               log_bytes!(self.context.counterparty_funding_pubkey().serialize()), encode::serialize_hex(&bitcoin_tx.transaction),
+                               log_bytes!(sighash[..]), encode::serialize_hex(&funding_script), log_bytes!(self.context.channel_id()));
+                       if let Err(_) = self.context.secp_ctx.verify_ecdsa(&sighash, &msg.signature, &self.context.counterparty_funding_pubkey()) {
                                return Err(ChannelError::Close("Invalid commitment tx signature from peer".to_owned()));
                        }
                        bitcoin_tx.txid
@@ -3159,28 +2855,28 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
 
                // If our counterparty updated the channel fee in this commitment transaction, check that
                // they can actually afford the new fee now.
-               let update_fee = if let Some((_, update_state)) = self.pending_update_fee {
+               let update_fee = if let Some((_, update_state)) = self.context.pending_update_fee {
                        update_state == FeeUpdateState::RemoteAnnounced
                } else { false };
                if update_fee {
-                       debug_assert!(!self.is_outbound());
-                       let counterparty_reserve_we_require_msat = self.holder_selected_channel_reserve_satoshis * 1000;
+                       debug_assert!(!self.context.is_outbound());
+                       let counterparty_reserve_we_require_msat = self.context.holder_selected_channel_reserve_satoshis * 1000;
                        if commitment_stats.remote_balance_msat < commitment_stats.total_fee_sat * 1000 + counterparty_reserve_we_require_msat {
                                return Err(ChannelError::Close("Funding remote cannot afford proposed new fee".to_owned()));
                        }
                }
                #[cfg(any(test, fuzzing))]
                {
-                       if self.is_outbound() {
-                               let projected_commit_tx_info = self.next_local_commitment_tx_fee_info_cached.lock().unwrap().take();
-                               *self.next_remote_commitment_tx_fee_info_cached.lock().unwrap() = None;
+                       if self.context.is_outbound() {
+                               let projected_commit_tx_info = self.context.next_local_commitment_tx_fee_info_cached.lock().unwrap().take();
+                               *self.context.next_remote_commitment_tx_fee_info_cached.lock().unwrap() = None;
                                if let Some(info) = projected_commit_tx_info {
-                                       let total_pending_htlcs = self.pending_inbound_htlcs.len() + self.pending_outbound_htlcs.len()
-                                               + self.holding_cell_htlc_updates.len();
+                                       let total_pending_htlcs = self.context.pending_inbound_htlcs.len() + self.context.pending_outbound_htlcs.len()
+                                               + self.context.holding_cell_htlc_updates.len();
                                        if info.total_pending_htlcs == total_pending_htlcs
-                                               && info.next_holder_htlc_id == self.next_holder_htlc_id
-                                               && info.next_counterparty_htlc_id == self.next_counterparty_htlc_id
-                                               && info.feerate == self.feerate_per_kw {
+                                               && info.next_holder_htlc_id == self.context.next_holder_htlc_id
+                                               && info.next_counterparty_htlc_id == self.context.next_counterparty_htlc_id
+                                               && info.feerate == self.context.feerate_per_kw {
                                                        assert_eq!(commitment_stats.total_fee_sat, info.fee / 1000);
                                                }
                                }
@@ -3211,16 +2907,16 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                for (idx, (htlc, mut source_opt)) in htlcs_cloned.drain(..).enumerate() {
                        if let Some(_) = htlc.transaction_output_index {
                                let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, commitment_stats.feerate_per_kw,
-                                       self.get_counterparty_selected_contest_delay().unwrap(), &htlc, self.opt_anchors(),
-                                       false, &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
+                                       self.context.get_counterparty_selected_contest_delay().unwrap(), &htlc, &self.context.channel_type,
+                                       &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
 
-                               let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, self.opt_anchors(), &keys);
-                               let htlc_sighashtype = if self.opt_anchors() { EcdsaSighashType::SinglePlusAnyoneCanPay } else { EcdsaSighashType::All };
+                               let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &self.context.channel_type, &keys);
+                               let htlc_sighashtype = if self.context.channel_type.supports_anchors_zero_fee_htlc_tx() { EcdsaSighashType::SinglePlusAnyoneCanPay } else { EcdsaSighashType::All };
                                let htlc_sighash = hash_to_message!(&sighash::SighashCache::new(&htlc_tx).segwit_signature_hash(0, &htlc_redeemscript, htlc.amount_msat / 1000, htlc_sighashtype).unwrap()[..]);
                                log_trace!(logger, "Checking HTLC tx signature {} by key {} against tx {} (sighash {}) with redeemscript {} in channel {}.",
                                        log_bytes!(msg.htlc_signatures[idx].serialize_compact()[..]), log_bytes!(keys.countersignatory_htlc_key.serialize()),
-                                       encode::serialize_hex(&htlc_tx), log_bytes!(htlc_sighash[..]), encode::serialize_hex(&htlc_redeemscript), log_bytes!(self.channel_id()));
-                               if let Err(_) = self.secp_ctx.verify_ecdsa(&htlc_sighash, &msg.htlc_signatures[idx], &keys.countersignatory_htlc_key) {
+                                       encode::serialize_hex(&htlc_tx), log_bytes!(htlc_sighash[..]), encode::serialize_hex(&htlc_redeemscript), log_bytes!(self.context.channel_id()));
+                               if let Err(_) = self.context.secp_ctx.verify_ecdsa(&htlc_sighash, &msg.htlc_signatures[idx], &keys.countersignatory_htlc_key) {
                                        return Err(ChannelError::Close("Invalid HTLC tx signature from peer".to_owned()));
                                }
                                if !separate_nondust_htlc_sources {
@@ -3241,38 +2937,38 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                        commitment_stats.tx,
                        msg.signature,
                        msg.htlc_signatures.clone(),
-                       &self.get_holder_pubkeys().funding_pubkey,
-                       self.counterparty_funding_pubkey()
+                       &self.context.get_holder_pubkeys().funding_pubkey,
+                       self.context.counterparty_funding_pubkey()
                );
 
-               self.holder_signer.validate_holder_commitment(&holder_commitment_tx, commitment_stats.preimages)
+               self.context.holder_signer.validate_holder_commitment(&holder_commitment_tx, commitment_stats.preimages)
                        .map_err(|_| ChannelError::Close("Failed to validate our commitment".to_owned()))?;
 
                // Update state now that we've passed all the can-fail calls...
                let mut need_commitment = false;
-               if let &mut Some((_, ref mut update_state)) = &mut self.pending_update_fee {
+               if let &mut Some((_, ref mut update_state)) = &mut self.context.pending_update_fee {
                        if *update_state == FeeUpdateState::RemoteAnnounced {
                                *update_state = FeeUpdateState::AwaitingRemoteRevokeToAnnounce;
                                need_commitment = true;
                        }
                }
 
-               for htlc in self.pending_inbound_htlcs.iter_mut() {
+               for htlc in self.context.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 {
                                log_trace!(logger, "Updating HTLC {} to AwaitingRemoteRevokeToAnnounce due to commitment_signed in channel {}.",
-                                       log_bytes!(htlc.payment_hash.0), log_bytes!(self.channel_id));
+                                       log_bytes!(htlc.payment_hash.0), log_bytes!(self.context.channel_id));
                                htlc.state = InboundHTLCState::AwaitingRemoteRevokeToAnnounce(forward_info);
                                need_commitment = true;
                        }
                }
                let mut claimed_htlcs = Vec::new();
-               for htlc in self.pending_outbound_htlcs.iter_mut() {
+               for htlc in self.context.pending_outbound_htlcs.iter_mut() {
                        if let &mut OutboundHTLCState::RemoteRemoved(ref mut outcome) = &mut htlc.state {
                                log_trace!(logger, "Updating HTLC {} to AwaitingRemoteRevokeToRemove due to commitment_signed in channel {}.",
-                                       log_bytes!(htlc.payment_hash.0), log_bytes!(self.channel_id));
+                                       log_bytes!(htlc.payment_hash.0), log_bytes!(self.context.channel_id));
                                // Grab the preimage, if it exists, instead of cloning
                                let mut reason = OutboundHTLCOutcome::Success(None);
                                mem::swap(outcome, &mut reason);
@@ -3290,9 +2986,9 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                        }
                }
 
-               self.latest_monitor_update_id += 1;
+               self.context.latest_monitor_update_id += 1;
                let mut monitor_update = ChannelMonitorUpdate {
-                       update_id: self.latest_monitor_update_id,
+                       update_id: self.context.latest_monitor_update_id,
                        updates: vec![ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo {
                                commitment_tx: holder_commitment_tx,
                                htlc_outputs: htlcs_and_sigs,
@@ -3301,45 +2997,45 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                        }]
                };
 
-               self.cur_holder_commitment_transaction_number -= 1;
+               self.context.cur_holder_commitment_transaction_number -= 1;
                // Note that if we need_commitment & !AwaitingRemoteRevoke we'll call
                // build_commitment_no_status_check() next which will reset this to RAAFirst.
-               self.resend_order = RAACommitmentOrder::CommitmentFirst;
+               self.context.resend_order = RAACommitmentOrder::CommitmentFirst;
 
-               if (self.channel_state & ChannelState::MonitorUpdateInProgress as u32) != 0 {
+               if (self.context.channel_state & ChannelState::MonitorUpdateInProgress 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_commitment && (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32)) == 0 {
+                       self.context.monitor_pending_revoke_and_ack = true;
+                       if need_commitment && (self.context.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.
-                               self.monitor_pending_commitment_signed = true;
+                               self.context.monitor_pending_commitment_signed = true;
                                let mut additional_update = self.build_commitment_no_status_check(logger);
                                // build_commitment_no_status_check may bump latest_monitor_id but we want them to be
                                // strictly increasing by one, so decrement it here.
-                               self.latest_monitor_update_id = monitor_update.update_id;
+                               self.context.latest_monitor_update_id = monitor_update.update_id;
                                monitor_update.updates.append(&mut additional_update.updates);
                        }
                        log_debug!(logger, "Received valid commitment_signed from peer in channel {}, updated HTLC state but awaiting a monitor update resolution to reply.",
-                               log_bytes!(self.channel_id));
+                               log_bytes!(self.context.channel_id));
                        return Ok(self.push_ret_blockable_mon_update(monitor_update));
                }
 
-               let need_commitment_signed = if need_commitment && (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32)) == 0 {
+               let need_commitment_signed = if need_commitment && (self.context.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 mut additional_update = self.build_commitment_no_status_check(logger);
                        // build_commitment_no_status_check may bump latest_monitor_id but we want them to be
                        // strictly increasing by one, so decrement it here.
-                       self.latest_monitor_update_id = monitor_update.update_id;
+                       self.context.latest_monitor_update_id = monitor_update.update_id;
                        monitor_update.updates.append(&mut additional_update.updates);
                        true
                } else { false };
 
                log_debug!(logger, "Received valid commitment_signed from peer in channel {}, updating HTLC state and responding with{} a revoke_and_ack.",
-                       log_bytes!(self.channel_id()), if need_commitment_signed { " our own commitment_signed and" } else { "" });
+                       log_bytes!(self.context.channel_id()), if need_commitment_signed { " our own commitment_signed and" } else { "" });
                self.monitor_updating_paused(true, need_commitment_signed, false, Vec::new(), Vec::new(), Vec::new());
                return Ok(self.push_ret_blockable_mon_update(monitor_update));
        }
@@ -3347,28 +3043,36 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
        /// Public version of the below, checking relevant preconditions first.
        /// If we're not in a state where freeing the holding cell makes sense, this is a no-op and
        /// returns `(None, Vec::new())`.
-       pub fn maybe_free_holding_cell_htlcs<L: Deref>(&mut self, logger: &L) -> (Option<&ChannelMonitorUpdate>, Vec<(HTLCSource, PaymentHash)>) where L::Target: Logger {
-               if self.channel_state >= ChannelState::ChannelReady as u32 &&
-                  (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32 | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress as u32)) == 0 {
-                       self.free_holding_cell_htlcs(logger)
+       pub fn maybe_free_holding_cell_htlcs<F: Deref, L: Deref>(
+               &mut self, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L
+       ) -> (Option<ChannelMonitorUpdate>, Vec<(HTLCSource, PaymentHash)>)
+       where F::Target: FeeEstimator, L::Target: Logger
+       {
+               if self.context.channel_state >= ChannelState::ChannelReady as u32 &&
+                  (self.context.channel_state & (ChannelState::AwaitingRemoteRevoke as u32 | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress as u32)) == 0 {
+                       self.free_holding_cell_htlcs(fee_estimator, logger)
                } else { (None, Vec::new()) }
        }
 
        /// Frees any pending commitment updates in the holding cell, generating the relevant messages
        /// for our counterparty.
-       fn free_holding_cell_htlcs<L: Deref>(&mut self, logger: &L) -> (Option<&ChannelMonitorUpdate>, Vec<(HTLCSource, PaymentHash)>) where L::Target: Logger {
-               assert_eq!(self.channel_state & ChannelState::MonitorUpdateInProgress as u32, 0);
-               if self.holding_cell_htlc_updates.len() != 0 || self.holding_cell_update_fee.is_some() {
-                       log_trace!(logger, "Freeing holding cell with {} HTLC updates{} in channel {}", self.holding_cell_htlc_updates.len(),
-                               if self.holding_cell_update_fee.is_some() { " and a fee update" } else { "" }, log_bytes!(self.channel_id()));
+       fn free_holding_cell_htlcs<F: Deref, L: Deref>(
+               &mut self, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L
+       ) -> (Option<ChannelMonitorUpdate>, Vec<(HTLCSource, PaymentHash)>)
+       where F::Target: FeeEstimator, L::Target: Logger
+       {
+               assert_eq!(self.context.channel_state & ChannelState::MonitorUpdateInProgress as u32, 0);
+               if self.context.holding_cell_htlc_updates.len() != 0 || self.context.holding_cell_update_fee.is_some() {
+                       log_trace!(logger, "Freeing holding cell with {} HTLC updates{} in channel {}", self.context.holding_cell_htlc_updates.len(),
+                               if self.context.holding_cell_update_fee.is_some() { " and a fee update" } else { "" }, log_bytes!(self.context.channel_id()));
 
                        let mut monitor_update = ChannelMonitorUpdate {
-                               update_id: self.latest_monitor_update_id + 1, // We don't increment this yet!
+                               update_id: self.context.latest_monitor_update_id + 1, // We don't increment this yet!
                                updates: Vec::new(),
                        };
 
                        let mut htlc_updates = Vec::new();
-                       mem::swap(&mut htlc_updates, &mut self.holding_cell_htlc_updates);
+                       mem::swap(&mut htlc_updates, &mut self.context.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());
@@ -3380,14 +3084,19 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                                // handling this case better and maybe fulfilling some of the HTLCs while attempting
                                // to rebalance channels.
                                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(), false, logger) {
+                                       &HTLCUpdateAwaitingACK::AddHTLC {
+                                               amount_msat, cltv_expiry, ref payment_hash, ref source, ref onion_routing_packet,
+                                               skimmed_fee_msat, ..
+                                       } => {
+                                               match self.send_htlc(amount_msat, *payment_hash, cltv_expiry, source.clone(),
+                                                       onion_routing_packet.clone(), false, skimmed_fee_msat, fee_estimator, logger)
+                                               {
                                                        Ok(update_add_msg_option) => update_add_htlcs.push(update_add_msg_option.unwrap()),
                                                        Err(e) => {
                                                                match e {
                                                                        ChannelError::Ignore(ref msg) => {
                                                                                log_info!(logger, "Failed to send HTLC with payment_hash {} due to {} in channel {}",
-                                                                                       log_bytes!(payment_hash.0), msg, log_bytes!(self.channel_id()));
+                                                                                       log_bytes!(payment_hash.0), msg, log_bytes!(self.context.channel_id()));
                                                                                // If we fail to send here, then this HTLC should
                                                                                // be failed backwards. Failing to send here
                                                                                // indicates that this HTLC may keep being put back
@@ -3436,11 +3145,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                                        },
                                }
                        }
-                       if update_add_htlcs.is_empty() && update_fulfill_htlcs.is_empty() && update_fail_htlcs.is_empty() && self.holding_cell_update_fee.is_none() {
+                       if update_add_htlcs.is_empty() && update_fulfill_htlcs.is_empty() && update_fail_htlcs.is_empty() && self.context.holding_cell_update_fee.is_none() {
                                return (None, htlcs_to_fail);
                        }
-                       let update_fee = if let Some(feerate) = self.holding_cell_update_fee.take() {
-                               self.send_update_fee(feerate, false, logger)
+                       let update_fee = if let Some(feerate) = self.context.holding_cell_update_fee.take() {
+                               self.send_update_fee(feerate, false, fee_estimator, logger)
                        } else {
                                None
                        };
@@ -3448,11 +3157,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                        let mut additional_update = self.build_commitment_no_status_check(logger);
                        // build_commitment_no_status_check and get_update_fulfill_htlc may bump latest_monitor_id
                        // but we want them to be strictly increasing by one, so reset it here.
-                       self.latest_monitor_update_id = monitor_update.update_id;
+                       self.context.latest_monitor_update_id = monitor_update.update_id;
                        monitor_update.updates.append(&mut additional_update.updates);
 
                        log_debug!(logger, "Freeing holding cell in channel {} resulted in {}{} HTLCs added, {} HTLCs fulfilled, and {} HTLCs failed.",
-                               log_bytes!(self.channel_id()), if update_fee.is_some() { "a fee update, " } else { "" },
+                               log_bytes!(self.context.channel_id()), if update_fee.is_some() { "a fee update, " } else { "" },
                                update_add_htlcs.len(), update_fulfill_htlcs.len(), update_fail_htlcs.len());
 
                        self.monitor_updating_paused(false, true, false, Vec::new(), Vec::new(), Vec::new());
@@ -3467,28 +3176,30 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
        /// 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<L: Deref>(&mut self, msg: &msgs::RevokeAndACK, logger: &L) -> Result<(Vec<(HTLCSource, PaymentHash)>, Option<&ChannelMonitorUpdate>), ChannelError>
-               where L::Target: Logger,
+       pub fn revoke_and_ack<F: Deref, L: Deref>(&mut self, msg: &msgs::RevokeAndACK,
+               fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L
+       ) -> Result<(Vec<(HTLCSource, PaymentHash)>, Option<ChannelMonitorUpdate>), ChannelError>
+       where F::Target: FeeEstimator, L::Target: Logger,
        {
-               if (self.channel_state & (ChannelState::ChannelReady as u32)) != (ChannelState::ChannelReady as u32) {
+               if (self.context.channel_state & (ChannelState::ChannelReady as u32)) != (ChannelState::ChannelReady as u32) {
                        return Err(ChannelError::Close("Got revoke/ACK message when channel was not in an operational state".to_owned()));
                }
-               if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
+               if self.context.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".to_owned()));
                }
-               if self.channel_state & BOTH_SIDES_SHUTDOWN_MASK == BOTH_SIDES_SHUTDOWN_MASK && self.last_sent_closing_fee.is_some() {
+               if self.context.channel_state & BOTH_SIDES_SHUTDOWN_MASK == BOTH_SIDES_SHUTDOWN_MASK && self.context.last_sent_closing_fee.is_some() {
                        return Err(ChannelError::Close("Peer sent revoke_and_ack after we'd started exchanging closing_signeds".to_owned()));
                }
 
                let secret = secp_check!(SecretKey::from_slice(&msg.per_commitment_secret), "Peer provided an invalid per_commitment_secret".to_owned());
 
-               if let Some(counterparty_prev_commitment_point) = self.counterparty_prev_commitment_point {
-                       if PublicKey::from_secret_key(&self.secp_ctx, &secret) != counterparty_prev_commitment_point {
+               if let Some(counterparty_prev_commitment_point) = self.context.counterparty_prev_commitment_point {
+                       if PublicKey::from_secret_key(&self.context.secp_ctx, &secret) != counterparty_prev_commitment_point {
                                return Err(ChannelError::Close("Got a revoke commitment secret which didn't correspond to their current pubkey".to_owned()));
                        }
                }
 
-               if self.channel_state & ChannelState::AwaitingRemoteRevoke as u32 == 0 {
+               if self.context.channel_state & ChannelState::AwaitingRemoteRevoke as u32 == 0 {
                        // Our counterparty seems to have burned their coins to us (by revoking a state when we
                        // haven't given them a new commitment transaction to broadcast). We should probably
                        // take advantage of this by updating our channel monitor, sending them an error, and
@@ -3501,22 +3212,22 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
 
                #[cfg(any(test, fuzzing))]
                {
-                       *self.next_local_commitment_tx_fee_info_cached.lock().unwrap() = None;
-                       *self.next_remote_commitment_tx_fee_info_cached.lock().unwrap() = None;
+                       *self.context.next_local_commitment_tx_fee_info_cached.lock().unwrap() = None;
+                       *self.context.next_remote_commitment_tx_fee_info_cached.lock().unwrap() = None;
                }
 
-               self.holder_signer.validate_counterparty_revocation(
-                       self.cur_counterparty_commitment_transaction_number + 1,
+               self.context.holder_signer.validate_counterparty_revocation(
+                       self.context.cur_counterparty_commitment_transaction_number + 1,
                        &secret
                ).map_err(|_| ChannelError::Close("Failed to validate revocation from peer".to_owned()))?;
 
-               self.commitment_secrets.provide_secret(self.cur_counterparty_commitment_transaction_number + 1, msg.per_commitment_secret)
+               self.context.commitment_secrets.provide_secret(self.context.cur_counterparty_commitment_transaction_number + 1, msg.per_commitment_secret)
                        .map_err(|_| ChannelError::Close("Previous secrets did not match new one".to_owned()))?;
-               self.latest_monitor_update_id += 1;
+               self.context.latest_monitor_update_id += 1;
                let mut monitor_update = ChannelMonitorUpdate {
-                       update_id: self.latest_monitor_update_id,
+                       update_id: self.context.latest_monitor_update_id,
                        updates: vec![ChannelMonitorUpdateStep::CommitmentSecret {
-                               idx: self.cur_counterparty_commitment_transaction_number + 1,
+                               idx: self.context.cur_counterparty_commitment_transaction_number + 1,
                                secret: msg.per_commitment_secret,
                        }],
                };
@@ -3525,16 +3236,17 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                // (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.counterparty_prev_commitment_point = self.counterparty_cur_commitment_point;
-               self.counterparty_cur_commitment_point = Some(msg.next_per_commitment_point);
-               self.cur_counterparty_commitment_transaction_number -= 1;
+               self.context.channel_state &= !(ChannelState::AwaitingRemoteRevoke as u32);
+               self.context.sent_message_awaiting_response = None;
+               self.context.counterparty_prev_commitment_point = self.context.counterparty_cur_commitment_point;
+               self.context.counterparty_cur_commitment_point = Some(msg.next_per_commitment_point);
+               self.context.cur_counterparty_commitment_transaction_number -= 1;
 
-               if self.announcement_sigs_state == AnnouncementSigsState::Committed {
-                       self.announcement_sigs_state = AnnouncementSigsState::PeerReceived;
+               if self.context.announcement_sigs_state == AnnouncementSigsState::Committed {
+                       self.context.announcement_sigs_state = AnnouncementSigsState::PeerReceived;
                }
 
-               log_trace!(logger, "Updating HTLCs on receipt of RAA in channel {}...", log_bytes!(self.channel_id()));
+               log_trace!(logger, "Updating HTLCs on receipt of RAA in channel {}...", log_bytes!(self.context.channel_id()));
                let mut to_forward_infos = Vec::new();
                let mut revoked_htlcs = Vec::new();
                let mut finalized_claimed_htlcs = Vec::new();
@@ -3544,9 +3256,9 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                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;
+                       // Take references explicitly so that we can hold multiple references to self.context.
+                       let pending_inbound_htlcs: &mut Vec<_> = &mut self.context.pending_inbound_htlcs;
+                       let pending_outbound_htlcs: &mut Vec<_> = &mut self.context.pending_outbound_htlcs;
 
                        // We really shouldnt have two passes here, but retain gives a non-mutable ref (Rust bug)
                        pending_inbound_htlcs.retain(|htlc| {
@@ -3625,54 +3337,53 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                                }
                        }
                }
-               self.value_to_self_msat = (self.value_to_self_msat as i64 + value_to_self_msat_diff) as u64;
+               self.context.value_to_self_msat = (self.context.value_to_self_msat as i64 + value_to_self_msat_diff) as u64;
 
-               if let Some((feerate, update_state)) = self.pending_update_fee {
+               if let Some((feerate, update_state)) = self.context.pending_update_fee {
                        match update_state {
                                FeeUpdateState::Outbound => {
-                                       debug_assert!(self.is_outbound());
+                                       debug_assert!(self.context.is_outbound());
                                        log_trace!(logger, " ...promoting outbound fee update {} to Committed", feerate);
-                                       self.feerate_per_kw = feerate;
-                                       self.pending_update_fee = None;
+                                       self.context.feerate_per_kw = feerate;
+                                       self.context.pending_update_fee = None;
                                },
-                               FeeUpdateState::RemoteAnnounced => { debug_assert!(!self.is_outbound()); },
+                               FeeUpdateState::RemoteAnnounced => { debug_assert!(!self.context.is_outbound()); },
                                FeeUpdateState::AwaitingRemoteRevokeToAnnounce => {
-                                       debug_assert!(!self.is_outbound());
+                                       debug_assert!(!self.context.is_outbound());
                                        log_trace!(logger, " ...promoting inbound AwaitingRemoteRevokeToAnnounce fee update {} to Committed", feerate);
                                        require_commitment = true;
-                                       self.feerate_per_kw = feerate;
-                                       self.pending_update_fee = None;
+                                       self.context.feerate_per_kw = feerate;
+                                       self.context.pending_update_fee = None;
                                },
                        }
                }
 
-               if (self.channel_state & ChannelState::MonitorUpdateInProgress as u32) == ChannelState::MonitorUpdateInProgress as u32 {
+               if (self.context.channel_state & ChannelState::MonitorUpdateInProgress as u32) == ChannelState::MonitorUpdateInProgress 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;
+                               self.context.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.
                                let mut additional_update = self.build_commitment_no_status_check(logger);
                                // build_commitment_no_status_check may bump latest_monitor_id but we want them to be
                                // strictly increasing by one, so decrement it here.
-                               self.latest_monitor_update_id = monitor_update.update_id;
+                               self.context.latest_monitor_update_id = monitor_update.update_id;
                                monitor_update.updates.append(&mut additional_update.updates);
                        }
-                       self.monitor_pending_forwards.append(&mut to_forward_infos);
-                       self.monitor_pending_failures.append(&mut revoked_htlcs);
-                       self.monitor_pending_finalized_fulfills.append(&mut finalized_claimed_htlcs);
-                       log_debug!(logger, "Received a valid revoke_and_ack for channel {} but awaiting a monitor update resolution to reply.", log_bytes!(self.channel_id()));
+                       self.context.monitor_pending_forwards.append(&mut to_forward_infos);
+                       self.context.monitor_pending_failures.append(&mut revoked_htlcs);
+                       self.context.monitor_pending_finalized_fulfills.append(&mut finalized_claimed_htlcs);
+                       log_debug!(logger, "Received a valid revoke_and_ack for channel {} but awaiting a monitor update resolution to reply.", log_bytes!(self.context.channel_id()));
                        return Ok((Vec::new(), self.push_ret_blockable_mon_update(monitor_update)));
                }
 
-               match self.free_holding_cell_htlcs(logger) {
-                       (Some(_), htlcs_to_fail) => {
-                               let mut additional_update = self.pending_monitor_updates.pop().unwrap().update;
+               match self.free_holding_cell_htlcs(fee_estimator, logger) {
+                       (Some(mut additional_update), htlcs_to_fail) => {
                                // free_holding_cell_htlcs may bump latest_monitor_id multiple times but we want them to be
                                // strictly increasing by one, so decrement it here.
-                               self.latest_monitor_update_id = monitor_update.update_id;
+                               self.context.latest_monitor_update_id = monitor_update.update_id;
                                monitor_update.updates.append(&mut additional_update.updates);
 
                                self.monitor_updating_paused(false, true, false, to_forward_infos, revoked_htlcs, finalized_claimed_htlcs);
@@ -3684,15 +3395,15 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
 
                                        // build_commitment_no_status_check may bump latest_monitor_id but we want them to be
                                        // strictly increasing by one, so decrement it here.
-                                       self.latest_monitor_update_id = monitor_update.update_id;
+                                       self.context.latest_monitor_update_id = monitor_update.update_id;
                                        monitor_update.updates.append(&mut additional_update.updates);
 
                                        log_debug!(logger, "Received a valid revoke_and_ack for channel {}. Responding with a commitment update with {} HTLCs failed.",
-                                               log_bytes!(self.channel_id()), update_fail_htlcs.len() + update_fail_malformed_htlcs.len());
+                                               log_bytes!(self.context.channel_id()), update_fail_htlcs.len() + update_fail_malformed_htlcs.len());
                                        self.monitor_updating_paused(false, true, false, to_forward_infos, revoked_htlcs, finalized_claimed_htlcs);
                                        Ok((htlcs_to_fail, self.push_ret_blockable_mon_update(monitor_update)))
                                } else {
-                                       log_debug!(logger, "Received a valid revoke_and_ack for channel {} with no reply necessary.", log_bytes!(self.channel_id()));
+                                       log_debug!(logger, "Received a valid revoke_and_ack for channel {} with no reply necessary.", log_bytes!(self.context.channel_id()));
                                        self.monitor_updating_paused(false, false, false, to_forward_infos, revoked_htlcs, finalized_claimed_htlcs);
                                        Ok((htlcs_to_fail, self.push_ret_blockable_mon_update(monitor_update)))
                                }
@@ -3703,8 +3414,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
        /// Queues up an outbound update fee by placing it in the holding cell. You should call
        /// [`Self::maybe_free_holding_cell_htlcs`] in order to actually generate and send the
        /// commitment update.
-       pub fn queue_update_fee<L: Deref>(&mut self, feerate_per_kw: u32, logger: &L) where L::Target: Logger {
-               let msg_opt = self.send_update_fee(feerate_per_kw, true, logger);
+       pub fn queue_update_fee<F: Deref, L: Deref>(&mut self, feerate_per_kw: u32,
+               fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L)
+       where F::Target: FeeEstimator, L::Target: Logger
+       {
+               let msg_opt = self.send_update_fee(feerate_per_kw, true, fee_estimator, logger);
                assert!(msg_opt.is_none(), "We forced holding cell?");
        }
 
@@ -3715,25 +3429,30 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
        ///
        /// You MUST call [`Self::send_commitment_no_state_update`] prior to any other calls on this
        /// [`Channel`] if `force_holding_cell` is false.
-       fn send_update_fee<L: Deref>(&mut self, feerate_per_kw: u32, mut force_holding_cell: bool, logger: &L) -> Option<msgs::UpdateFee> where L::Target: Logger {
-               if !self.is_outbound() {
+       fn send_update_fee<F: Deref, L: Deref>(
+               &mut self, feerate_per_kw: u32, mut force_holding_cell: bool,
+               fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L
+       ) -> Option<msgs::UpdateFee>
+       where F::Target: FeeEstimator, L::Target: Logger
+       {
+               if !self.context.is_outbound() {
                        panic!("Cannot send fee from inbound channel");
                }
-               if !self.is_usable() {
+               if !self.context.is_usable() {
                        panic!("Cannot update fee until channel is fully established and we haven't started shutting down");
                }
-               if !self.is_live() {
+               if !self.context.is_live() {
                        panic!("Cannot update fee while peer is disconnected/we're awaiting a monitor update (ChannelManager should have caught this)");
                }
 
                // Before proposing a feerate update, check that we can actually afford the new fee.
-               let inbound_stats = self.get_inbound_pending_htlc_stats(Some(feerate_per_kw));
-               let outbound_stats = self.get_outbound_pending_htlc_stats(Some(feerate_per_kw));
-               let keys = self.build_holder_transaction_keys(self.cur_holder_commitment_transaction_number);
-               let commitment_stats = self.build_commitment_transaction(self.cur_holder_commitment_transaction_number, &keys, true, true, logger);
-               let buffer_fee_msat = Channel::<Signer>::commit_tx_fee_sat(feerate_per_kw, commitment_stats.num_nondust_htlcs + outbound_stats.on_holder_tx_holding_cell_htlcs_count as usize + CONCURRENT_INBOUND_HTLC_FEE_BUFFER as usize, self.opt_anchors()) * 1000;
+               let inbound_stats = self.context.get_inbound_pending_htlc_stats(Some(feerate_per_kw));
+               let outbound_stats = self.context.get_outbound_pending_htlc_stats(Some(feerate_per_kw));
+               let keys = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number);
+               let commitment_stats = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &keys, true, true, logger);
+               let buffer_fee_msat = commit_tx_fee_sat(feerate_per_kw, commitment_stats.num_nondust_htlcs + outbound_stats.on_holder_tx_holding_cell_htlcs_count as usize + CONCURRENT_INBOUND_HTLC_FEE_BUFFER as usize, self.context.get_channel_type()) * 1000;
                let holder_balance_msat = commitment_stats.local_balance_msat - outbound_stats.holding_cell_msat;
-               if holder_balance_msat < buffer_fee_msat  + self.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000 {
+               if holder_balance_msat < buffer_fee_msat  + self.context.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000 {
                        //TODO: auto-close after a number of failures?
                        log_debug!(logger, "Cannot afford to send new feerate at {}", feerate_per_kw);
                        return None;
@@ -3742,29 +3461,30 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                // Note, we evaluate pending htlc "preemptive" trimmed-to-dust threshold at the proposed `feerate_per_kw`.
                let holder_tx_dust_exposure = inbound_stats.on_holder_tx_dust_exposure_msat + outbound_stats.on_holder_tx_dust_exposure_msat;
                let counterparty_tx_dust_exposure = inbound_stats.on_counterparty_tx_dust_exposure_msat + outbound_stats.on_counterparty_tx_dust_exposure_msat;
-               if holder_tx_dust_exposure > self.get_max_dust_htlc_exposure_msat() {
+               let max_dust_htlc_exposure_msat = self.context.get_max_dust_htlc_exposure_msat(fee_estimator);
+               if holder_tx_dust_exposure > max_dust_htlc_exposure_msat {
                        log_debug!(logger, "Cannot afford to send new feerate at {} without infringing max dust htlc exposure", feerate_per_kw);
                        return None;
                }
-               if counterparty_tx_dust_exposure > self.get_max_dust_htlc_exposure_msat() {
+               if counterparty_tx_dust_exposure > max_dust_htlc_exposure_msat {
                        log_debug!(logger, "Cannot afford to send new feerate at {} without infringing max dust htlc exposure", feerate_per_kw);
                        return None;
                }
 
-               if (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32 | ChannelState::MonitorUpdateInProgress as u32)) != 0 {
+               if (self.context.channel_state & (ChannelState::AwaitingRemoteRevoke as u32 | ChannelState::MonitorUpdateInProgress as u32)) != 0 {
                        force_holding_cell = true;
                }
 
                if force_holding_cell {
-                       self.holding_cell_update_fee = Some(feerate_per_kw);
+                       self.context.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, FeeUpdateState::Outbound));
+               debug_assert!(self.context.pending_update_fee.is_none());
+               self.context.pending_update_fee = Some((feerate_per_kw, FeeUpdateState::Outbound));
 
                Some(msgs::UpdateFee {
-                       channel_id: self.channel_id,
+                       channel_id: self.context.channel_id,
                        feerate_per_kw,
                })
        }
@@ -3775,30 +3495,30 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
        /// No further message handling calls may be made until a channel_reestablish dance has
        /// completed.
        pub fn remove_uncommitted_htlcs_and_mark_paused<L: Deref>(&mut self, logger: &L)  where L::Target: Logger {
-               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;
+               assert_eq!(self.context.channel_state & ChannelState::ShutdownComplete as u32, 0);
+               if self.context.channel_state < ChannelState::FundingSent as u32 {
+                       self.context.channel_state = ChannelState::ShutdownComplete as u32;
                        return;
                }
 
-               if self.channel_state & (ChannelState::PeerDisconnected as u32) == (ChannelState::PeerDisconnected as u32) {
+               if self.context.channel_state & (ChannelState::PeerDisconnected as u32) == (ChannelState::PeerDisconnected as u32) {
                        // While the below code should be idempotent, it's simpler to just return early, as
                        // redundant disconnect events can fire, though they should be rare.
                        return;
                }
 
-               if self.announcement_sigs_state == AnnouncementSigsState::MessageSent || self.announcement_sigs_state == AnnouncementSigsState::Committed {
-                       self.announcement_sigs_state = AnnouncementSigsState::NotSent;
+               if self.context.announcement_sigs_state == AnnouncementSigsState::MessageSent || self.context.announcement_sigs_state == AnnouncementSigsState::Committed {
+                       self.context.announcement_sigs_state = AnnouncementSigsState::NotSent;
                }
 
                // Upon reconnect we have to start the closing_signed dance over, but shutdown messages
                // will be retransmitted.
-               self.last_sent_closing_fee = None;
-               self.pending_counterparty_closing_signed = None;
-               self.closing_fee_limits = None;
+               self.context.last_sent_closing_fee = None;
+               self.context.pending_counterparty_closing_signed = None;
+               self.context.closing_fee_limits = None;
 
                let mut inbound_drop_count = 0;
-               self.pending_inbound_htlcs.retain(|htlc| {
+               self.context.pending_inbound_htlcs.retain(|htlc| {
                        match htlc.state {
                                InboundHTLCState::RemoteAnnounced(_) => {
                                        // They sent us an update_add_htlc but we never got the commitment_signed.
@@ -3823,16 +3543,16 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                                },
                        }
                });
-               self.next_counterparty_htlc_id -= inbound_drop_count;
+               self.context.next_counterparty_htlc_id -= inbound_drop_count;
 
-               if let Some((_, update_state)) = self.pending_update_fee {
+               if let Some((_, update_state)) = self.context.pending_update_fee {
                        if update_state == FeeUpdateState::RemoteAnnounced {
-                               debug_assert!(!self.is_outbound());
-                               self.pending_update_fee = None;
+                               debug_assert!(!self.context.is_outbound());
+                               self.context.pending_update_fee = None;
                        }
                }
 
-               for htlc in self.pending_outbound_htlcs.iter_mut() {
+               for htlc in self.context.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
@@ -3841,8 +3561,10 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                        }
                }
 
-               self.channel_state |= ChannelState::PeerDisconnected as u32;
-               log_trace!(logger, "Peer disconnection resulted in {} remote-announced HTLC drops on channel {}", inbound_drop_count, log_bytes!(self.channel_id()));
+               self.context.sent_message_awaiting_response = None;
+
+               self.context.channel_state |= ChannelState::PeerDisconnected as u32;
+               log_trace!(logger, "Peer disconnection resulted in {} remote-announced HTLC drops on channel {}", inbound_drop_count, log_bytes!(self.context.channel_id()));
        }
 
        /// Indicates that a ChannelMonitor update is in progress and has not yet been fully persisted.
@@ -3861,13 +3583,13 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                mut pending_fails: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>,
                mut pending_finalized_claimed_htlcs: Vec<HTLCSource>
        ) {
-               self.monitor_pending_revoke_and_ack |= resend_raa;
-               self.monitor_pending_commitment_signed |= resend_commitment;
-               self.monitor_pending_channel_ready |= resend_channel_ready;
-               self.monitor_pending_forwards.append(&mut pending_forwards);
-               self.monitor_pending_failures.append(&mut pending_fails);
-               self.monitor_pending_finalized_fulfills.append(&mut pending_finalized_claimed_htlcs);
-               self.channel_state |= ChannelState::MonitorUpdateInProgress as u32;
+               self.context.monitor_pending_revoke_and_ack |= resend_raa;
+               self.context.monitor_pending_commitment_signed |= resend_commitment;
+               self.context.monitor_pending_channel_ready |= resend_channel_ready;
+               self.context.monitor_pending_forwards.append(&mut pending_forwards);
+               self.context.monitor_pending_failures.append(&mut pending_fails);
+               self.context.monitor_pending_finalized_fulfills.append(&mut pending_finalized_claimed_htlcs);
+               self.context.channel_state |= ChannelState::MonitorUpdateInProgress as u32;
        }
 
        /// Indicates that the latest ChannelMonitor update has been committed by the client
@@ -3881,25 +3603,19 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                L::Target: Logger,
                NS::Target: NodeSigner
        {
-               assert_eq!(self.channel_state & ChannelState::MonitorUpdateInProgress as u32, ChannelState::MonitorUpdateInProgress as u32);
-               self.channel_state &= !(ChannelState::MonitorUpdateInProgress as u32);
-               let mut found_blocked = false;
-               self.pending_monitor_updates.retain(|upd| {
-                       if found_blocked { debug_assert!(upd.blocked, "No mons may be unblocked after a blocked one"); }
-                       if upd.blocked { found_blocked = true; }
-                       upd.blocked
-               });
+               assert_eq!(self.context.channel_state & ChannelState::MonitorUpdateInProgress as u32, ChannelState::MonitorUpdateInProgress as u32);
+               self.context.channel_state &= !(ChannelState::MonitorUpdateInProgress as u32);
 
                // If we're past (or at) the FundingSent stage on an outbound channel, try to
                // (re-)broadcast the funding transaction as we may have declined to broadcast it when we
                // first received the funding_signed.
                let mut funding_broadcastable =
-                       if self.is_outbound() && self.channel_state & !MULTI_STATE_FLAGS >= ChannelState::FundingSent as u32 {
-                               self.funding_transaction.take()
+                       if self.context.is_outbound() && self.context.channel_state & !MULTI_STATE_FLAGS >= ChannelState::FundingSent as u32 {
+                               self.context.funding_transaction.take()
                        } else { None };
                // That said, if the funding transaction is already confirmed (ie we're active with a
                // minimum_depth over 0) don't bother re-broadcasting the confirmed funding tx.
-               if self.channel_state & !MULTI_STATE_FLAGS >= ChannelState::ChannelReady as u32 && self.minimum_depth != Some(0) {
+               if self.context.channel_state & !MULTI_STATE_FLAGS >= ChannelState::ChannelReady as u32 && self.context.minimum_depth != Some(0) {
                        funding_broadcastable = None;
                }
 
@@ -3909,48 +3625,49 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                // * an inbound channel that failed to persist the monitor on funding_created and we got
                //   the funding transaction confirmed before the monitor was persisted, or
                // * a 0-conf channel and intended to send the channel_ready before any broadcast at all.
-               let channel_ready = if self.monitor_pending_channel_ready {
-                       assert!(!self.is_outbound() || self.minimum_depth == Some(0),
+               let channel_ready = if self.context.monitor_pending_channel_ready {
+                       assert!(!self.context.is_outbound() || self.context.minimum_depth == Some(0),
                                "Funding transaction broadcast by the local client before it should have - LDK didn't do it!");
-                       self.monitor_pending_channel_ready = false;
-                       let next_per_commitment_point = self.holder_signer.get_per_commitment_point(self.cur_holder_commitment_transaction_number, &self.secp_ctx);
+                       self.context.monitor_pending_channel_ready = false;
+                       let next_per_commitment_point = self.context.holder_signer.get_per_commitment_point(self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
                        Some(msgs::ChannelReady {
-                               channel_id: self.channel_id(),
+                               channel_id: self.context.channel_id(),
                                next_per_commitment_point,
-                               short_channel_id_alias: Some(self.outbound_scid_alias),
+                               short_channel_id_alias: Some(self.context.outbound_scid_alias),
                        })
                } else { None };
 
                let announcement_sigs = self.get_announcement_sigs(node_signer, genesis_block_hash, user_config, best_block_height, logger);
 
                let mut accepted_htlcs = Vec::new();
-               mem::swap(&mut accepted_htlcs, &mut self.monitor_pending_forwards);
+               mem::swap(&mut accepted_htlcs, &mut self.context.monitor_pending_forwards);
                let mut failed_htlcs = Vec::new();
-               mem::swap(&mut failed_htlcs, &mut self.monitor_pending_failures);
+               mem::swap(&mut failed_htlcs, &mut self.context.monitor_pending_failures);
                let mut finalized_claimed_htlcs = Vec::new();
-               mem::swap(&mut finalized_claimed_htlcs, &mut self.monitor_pending_finalized_fulfills);
+               mem::swap(&mut finalized_claimed_htlcs, &mut self.context.monitor_pending_finalized_fulfills);
 
-               if self.channel_state & (ChannelState::PeerDisconnected as u32) != 0 {
-                       self.monitor_pending_revoke_and_ack = false;
-                       self.monitor_pending_commitment_signed = false;
+               if self.context.channel_state & (ChannelState::PeerDisconnected as u32) != 0 {
+                       self.context.monitor_pending_revoke_and_ack = false;
+                       self.context.monitor_pending_commitment_signed = false;
                        return MonitorRestoreUpdates {
                                raa: None, commitment_update: None, order: RAACommitmentOrder::RevokeAndACKFirst,
                                accepted_htlcs, failed_htlcs, finalized_claimed_htlcs, funding_broadcastable, channel_ready, announcement_sigs
                        };
                }
 
-               let raa = if self.monitor_pending_revoke_and_ack {
+               let raa = if self.context.monitor_pending_revoke_and_ack {
                        Some(self.get_last_revoke_and_ack())
                } else { None };
-               let commitment_update = if self.monitor_pending_commitment_signed {
+               let commitment_update = if self.context.monitor_pending_commitment_signed {
+                       self.mark_awaiting_response();
                        Some(self.get_last_commitment_update(logger))
                } else { None };
 
-               self.monitor_pending_revoke_and_ack = false;
-               self.monitor_pending_commitment_signed = false;
-               let order = self.resend_order.clone();
+               self.context.monitor_pending_revoke_and_ack = false;
+               self.context.monitor_pending_commitment_signed = false;
+               let order = self.context.resend_order.clone();
                log_debug!(logger, "Restored monitor updating in channel {} resulting in {}{} commitment update and {} RAA, with {} first",
-                       log_bytes!(self.channel_id()), if funding_broadcastable.is_some() { "a funding broadcastable, " } else { "" },
+                       log_bytes!(self.context.channel_id()), if funding_broadcastable.is_some() { "a funding broadcastable, " } else { "" },
                        if commitment_update.is_some() { "a" } else { "no" }, if raa.is_some() { "an" } else { "no" },
                        match order { RAACommitmentOrder::CommitmentFirst => "commitment", RAACommitmentOrder::RevokeAndACKFirst => "RAA"});
                MonitorRestoreUpdates {
@@ -3961,30 +3678,31 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
        pub fn update_fee<F: Deref, L: Deref>(&mut self, fee_estimator: &LowerBoundedFeeEstimator<F>, msg: &msgs::UpdateFee, logger: &L) -> Result<(), ChannelError>
                where F::Target: FeeEstimator, L::Target: Logger
        {
-               if self.is_outbound() {
+               if self.context.is_outbound() {
                        return Err(ChannelError::Close("Non-funding remote tried to update channel fee".to_owned()));
                }
-               if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
+               if self.context.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
                        return Err(ChannelError::Close("Peer sent update_fee when we needed a channel_reestablish".to_owned()));
                }
-               Channel::<Signer>::check_remote_fee(fee_estimator, msg.feerate_per_kw, Some(self.feerate_per_kw), logger)?;
-               let feerate_over_dust_buffer = msg.feerate_per_kw > self.get_dust_buffer_feerate(None);
+               Channel::<Signer>::check_remote_fee(fee_estimator, msg.feerate_per_kw, Some(self.context.feerate_per_kw), logger)?;
+               let feerate_over_dust_buffer = msg.feerate_per_kw > self.context.get_dust_buffer_feerate(None);
 
-               self.pending_update_fee = Some((msg.feerate_per_kw, FeeUpdateState::RemoteAnnounced));
-               self.update_time_counter += 1;
+               self.context.pending_update_fee = Some((msg.feerate_per_kw, FeeUpdateState::RemoteAnnounced));
+               self.context.update_time_counter += 1;
                // If the feerate has increased over the previous dust buffer (note that
                // `get_dust_buffer_feerate` considers the `pending_update_fee` status), check that we
                // won't be pushed over our dust exposure limit by the feerate increase.
                if feerate_over_dust_buffer {
-                       let inbound_stats = self.get_inbound_pending_htlc_stats(None);
-                       let outbound_stats = self.get_outbound_pending_htlc_stats(None);
+                       let inbound_stats = self.context.get_inbound_pending_htlc_stats(None);
+                       let outbound_stats = self.context.get_outbound_pending_htlc_stats(None);
                        let holder_tx_dust_exposure = inbound_stats.on_holder_tx_dust_exposure_msat + outbound_stats.on_holder_tx_dust_exposure_msat;
                        let counterparty_tx_dust_exposure = inbound_stats.on_counterparty_tx_dust_exposure_msat + outbound_stats.on_counterparty_tx_dust_exposure_msat;
-                       if holder_tx_dust_exposure > self.get_max_dust_htlc_exposure_msat() {
+                       let max_dust_htlc_exposure_msat = self.context.get_max_dust_htlc_exposure_msat(fee_estimator);
+                       if holder_tx_dust_exposure > max_dust_htlc_exposure_msat {
                                return Err(ChannelError::Close(format!("Peer sent update_fee with a feerate ({}) which may over-expose us to dust-in-flight on our own transactions (totaling {} msat)",
                                        msg.feerate_per_kw, holder_tx_dust_exposure)));
                        }
-                       if counterparty_tx_dust_exposure > self.get_max_dust_htlc_exposure_msat() {
+                       if counterparty_tx_dust_exposure > max_dust_htlc_exposure_msat {
                                return Err(ChannelError::Close(format!("Peer sent update_fee with a feerate ({}) which may over-expose us to dust-in-flight on our counterparty's transactions (totaling {} msat)",
                                        msg.feerate_per_kw, counterparty_tx_dust_exposure)));
                        }
@@ -3993,10 +3711,10 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
        }
 
        fn get_last_revoke_and_ack(&self) -> msgs::RevokeAndACK {
-               let next_per_commitment_point = self.holder_signer.get_per_commitment_point(self.cur_holder_commitment_transaction_number, &self.secp_ctx);
-               let per_commitment_secret = self.holder_signer.release_commitment_secret(self.cur_holder_commitment_transaction_number + 2);
+               let next_per_commitment_point = self.context.holder_signer.get_per_commitment_point(self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
+               let per_commitment_secret = self.context.holder_signer.release_commitment_secret(self.context.cur_holder_commitment_transaction_number + 2);
                msgs::RevokeAndACK {
-                       channel_id: self.channel_id,
+                       channel_id: self.context.channel_id,
                        per_commitment_secret,
                        next_per_commitment_point,
                        #[cfg(taproot)]
@@ -4010,32 +3728,33 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                let mut update_fail_htlcs = Vec::new();
                let mut update_fail_malformed_htlcs = Vec::new();
 
-               for htlc in self.pending_outbound_htlcs.iter() {
+               for htlc in self.context.pending_outbound_htlcs.iter() {
                        if let &OutboundHTLCState::LocalAnnounced(ref onion_packet) = &htlc.state {
                                update_add_htlcs.push(msgs::UpdateAddHTLC {
-                                       channel_id: self.channel_id(),
+                                       channel_id: self.context.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(),
+                                       skimmed_fee_msat: htlc.skimmed_fee_msat,
                                });
                        }
                }
 
-               for htlc in self.pending_inbound_htlcs.iter() {
+               for htlc in self.context.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(),
+                                                       channel_id: self.context.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(),
+                                                       channel_id: self.context.channel_id(),
                                                        htlc_id: htlc.htlc_id,
                                                        sha256_of_onion: sha256_of_onion.clone(),
                                                        failure_code: failure_code.clone(),
@@ -4043,7 +3762,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                                        },
                                        &InboundHTLCRemovalReason::Fulfill(ref payment_preimage) => {
                                                update_fulfill_htlcs.push(msgs::UpdateFulfillHTLC {
-                                                       channel_id: self.channel_id(),
+                                                       channel_id: self.context.channel_id(),
                                                        htlc_id: htlc.htlc_id,
                                                        payment_preimage: payment_preimage.clone(),
                                                });
@@ -4052,15 +3771,15 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                        }
                }
 
-               let update_fee = if self.is_outbound() && self.pending_update_fee.is_some() {
+               let update_fee = if self.context.is_outbound() && self.context.pending_update_fee.is_some() {
                        Some(msgs::UpdateFee {
-                               channel_id: self.channel_id(),
-                               feerate_per_kw: self.pending_update_fee.unwrap().0,
+                               channel_id: self.context.channel_id(),
+                               feerate_per_kw: self.context.pending_update_fee.unwrap().0,
                        })
                } else { None };
 
                log_trace!(logger, "Regenerated latest commitment update in channel {} with{} {} update_adds, {} update_fulfills, {} update_fails, and {} update_fail_malformeds",
-                               log_bytes!(self.channel_id()), if update_fee.is_some() { " update_fee," } else { "" },
+                               log_bytes!(self.context.channel_id()), if update_fee.is_some() { " update_fee," } else { "" },
                                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,
@@ -4083,7 +3802,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                L::Target: Logger,
                NS::Target: NodeSigner
        {
-               if self.channel_state & (ChannelState::PeerDisconnected as u32) == 0 {
+               if self.context.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.
@@ -4096,17 +3815,17 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                }
 
                if msg.next_remote_commitment_number > 0 {
-                       let expected_point = self.holder_signer.get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - msg.next_remote_commitment_number + 1, &self.secp_ctx);
+                       let expected_point = self.context.holder_signer.get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - msg.next_remote_commitment_number + 1, &self.context.secp_ctx);
                        let given_secret = SecretKey::from_slice(&msg.your_last_per_commitment_secret)
                                .map_err(|_| ChannelError::Close("Peer sent a garbage channel_reestablish with unparseable secret key".to_owned()))?;
-                       if expected_point != PublicKey::from_secret_key(&self.secp_ctx, &given_secret) {
+                       if expected_point != PublicKey::from_secret_key(&self.context.secp_ctx, &given_secret) {
                                return Err(ChannelError::Close("Peer sent a garbage channel_reestablish with secret key not matching the commitment height provided".to_owned()));
                        }
-                       if msg.next_remote_commitment_number > INITIAL_COMMITMENT_NUMBER - self.cur_holder_commitment_transaction_number {
+                       if msg.next_remote_commitment_number > INITIAL_COMMITMENT_NUMBER - self.context.cur_holder_commitment_transaction_number {
                                macro_rules! log_and_panic {
                                        ($err_msg: expr) => {
-                                               log_error!(logger, $err_msg, log_bytes!(self.channel_id), log_pubkey!(self.counterparty_node_id));
-                                               panic!($err_msg, log_bytes!(self.channel_id), log_pubkey!(self.counterparty_node_id));
+                                               log_error!(logger, $err_msg, log_bytes!(self.context.channel_id), log_pubkey!(self.context.counterparty_node_id));
+                                               panic!($err_msg, log_bytes!(self.context.channel_id), log_pubkey!(self.context.counterparty_node_id));
                                        }
                                }
                                log_and_panic!("We have fallen behind - we have received proof that if we broadcast our counterparty is going to claim all our funds.\n\
@@ -4122,7 +3841,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
 
                // Before we change the state of the channel, we check if the peer is sending a very old
                // commitment transaction number, if yes we send a warning message.
-               let our_commitment_transaction = INITIAL_COMMITMENT_NUMBER - self.cur_holder_commitment_transaction_number - 1;
+               let our_commitment_transaction = INITIAL_COMMITMENT_NUMBER - self.context.cur_holder_commitment_transaction_number - 1;
                if  msg.next_remote_commitment_number + 1 < our_commitment_transaction {
                        return Err(
                                ChannelError::Warn(format!("Peer attempted to reestablish channel with a very old local commitment transaction: {} (received) vs {} (expected)", msg.next_remote_commitment_number, our_commitment_transaction))
@@ -4131,22 +3850,23 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
 
                // 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);
+               self.context.channel_state &= !(ChannelState::PeerDisconnected as u32);
+               self.context.sent_message_awaiting_response = None;
 
-               let shutdown_msg = if self.channel_state & (ChannelState::LocalShutdownSent as u32) != 0 {
-                       assert!(self.shutdown_scriptpubkey.is_some());
+               let shutdown_msg = if self.context.channel_state & (ChannelState::LocalShutdownSent as u32) != 0 {
+                       assert!(self.context.shutdown_scriptpubkey.is_some());
                        Some(msgs::Shutdown {
-                               channel_id: self.channel_id,
+                               channel_id: self.context.channel_id,
                                scriptpubkey: self.get_closing_scriptpubkey(),
                        })
                } else { None };
 
                let announcement_sigs = self.get_announcement_sigs(node_signer, genesis_block_hash, user_config, best_block.height(), logger);
 
-               if self.channel_state & (ChannelState::FundingSent as u32) == ChannelState::FundingSent as u32 {
+               if self.context.channel_state & (ChannelState::FundingSent as u32) == ChannelState::FundingSent as u32 {
                        // If we're waiting on a monitor update, we shouldn't re-send any channel_ready's.
-                       if self.channel_state & (ChannelState::OurChannelReady as u32) == 0 ||
-                                       self.channel_state & (ChannelState::MonitorUpdateInProgress as u32) != 0 {
+                       if self.context.channel_state & (ChannelState::OurChannelReady as u32) == 0 ||
+                                       self.context.channel_state & (ChannelState::MonitorUpdateInProgress 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 channel_ready yet".to_owned()));
                                }
@@ -4160,12 +3880,12 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                        }
 
                        // We have OurChannelReady set!
-                       let next_per_commitment_point = self.holder_signer.get_per_commitment_point(self.cur_holder_commitment_transaction_number, &self.secp_ctx);
+                       let next_per_commitment_point = self.context.holder_signer.get_per_commitment_point(self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
                        return Ok(ReestablishResponses {
                                channel_ready: Some(msgs::ChannelReady {
-                                       channel_id: self.channel_id(),
+                                       channel_id: self.context.channel_id(),
                                        next_per_commitment_point,
-                                       short_channel_id_alias: Some(self.outbound_scid_alias),
+                                       short_channel_id_alias: Some(self.context.outbound_scid_alias),
                                }),
                                raa: None, commitment_update: None,
                                order: RAACommitmentOrder::CommitmentFirst,
@@ -4173,13 +3893,13 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                        });
                }
 
-               let required_revoke = if msg.next_remote_commitment_number + 1 == INITIAL_COMMITMENT_NUMBER - self.cur_holder_commitment_transaction_number {
+               let required_revoke = if msg.next_remote_commitment_number + 1 == INITIAL_COMMITMENT_NUMBER - self.context.cur_holder_commitment_transaction_number {
                        // Remote isn't waiting on any RevokeAndACK from us!
                        // Note that if we need to repeat our ChannelReady we'll do that in the next if block.
                        None
-               } else if msg.next_remote_commitment_number + 1 == (INITIAL_COMMITMENT_NUMBER - 1) - self.cur_holder_commitment_transaction_number {
-                       if self.channel_state & (ChannelState::MonitorUpdateInProgress as u32) != 0 {
-                               self.monitor_pending_revoke_and_ack = true;
+               } else if msg.next_remote_commitment_number + 1 == (INITIAL_COMMITMENT_NUMBER - 1) - self.context.cur_holder_commitment_transaction_number {
+                       if self.context.channel_state & (ChannelState::MonitorUpdateInProgress as u32) != 0 {
+                               self.context.monitor_pending_revoke_and_ack = true;
                                None
                        } else {
                                Some(self.get_last_revoke_and_ack())
@@ -4192,51 +3912,55 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                // 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 next_counterparty_commitment_number = INITIAL_COMMITMENT_NUMBER - self.cur_counterparty_commitment_transaction_number + if (self.channel_state & ChannelState::AwaitingRemoteRevoke as u32) != 0 { 1 } else { 0 };
+               let is_awaiting_remote_revoke = self.context.channel_state & ChannelState::AwaitingRemoteRevoke as u32 != 0;
+               if is_awaiting_remote_revoke && !self.is_awaiting_monitor_update() {
+                       self.mark_awaiting_response();
+               }
+               let next_counterparty_commitment_number = INITIAL_COMMITMENT_NUMBER - self.context.cur_counterparty_commitment_transaction_number + if is_awaiting_remote_revoke { 1 } else { 0 };
 
-               let channel_ready = if msg.next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self.cur_holder_commitment_transaction_number == 1 {
+               let channel_ready = if msg.next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self.context.cur_holder_commitment_transaction_number == 1 {
                        // We should never have to worry about MonitorUpdateInProgress resending ChannelReady
-                       let next_per_commitment_point = self.holder_signer.get_per_commitment_point(self.cur_holder_commitment_transaction_number, &self.secp_ctx);
+                       let next_per_commitment_point = self.context.holder_signer.get_per_commitment_point(self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
                        Some(msgs::ChannelReady {
-                               channel_id: self.channel_id(),
+                               channel_id: self.context.channel_id(),
                                next_per_commitment_point,
-                               short_channel_id_alias: Some(self.outbound_scid_alias),
+                               short_channel_id_alias: Some(self.context.outbound_scid_alias),
                        })
                } else { None };
 
                if msg.next_local_commitment_number == next_counterparty_commitment_number {
                        if required_revoke.is_some() {
-                               log_debug!(logger, "Reconnected channel {} with only lost outbound RAA", log_bytes!(self.channel_id()));
+                               log_debug!(logger, "Reconnected channel {} with only lost outbound RAA", log_bytes!(self.context.channel_id()));
                        } else {
-                               log_debug!(logger, "Reconnected channel {} with no loss", log_bytes!(self.channel_id()));
+                               log_debug!(logger, "Reconnected channel {} with no loss", log_bytes!(self.context.channel_id()));
                        }
 
                        Ok(ReestablishResponses {
                                channel_ready, shutdown_msg, announcement_sigs,
                                raa: required_revoke,
                                commitment_update: None,
-                               order: self.resend_order.clone(),
+                               order: self.context.resend_order.clone(),
                        })
                } else if msg.next_local_commitment_number == next_counterparty_commitment_number - 1 {
                        if required_revoke.is_some() {
-                               log_debug!(logger, "Reconnected channel {} with lost outbound RAA and lost remote commitment tx", log_bytes!(self.channel_id()));
+                               log_debug!(logger, "Reconnected channel {} with lost outbound RAA and lost remote commitment tx", log_bytes!(self.context.channel_id()));
                        } else {
-                               log_debug!(logger, "Reconnected channel {} with only lost remote commitment tx", log_bytes!(self.channel_id()));
+                               log_debug!(logger, "Reconnected channel {} with only lost remote commitment tx", log_bytes!(self.context.channel_id()));
                        }
 
-                       if self.channel_state & (ChannelState::MonitorUpdateInProgress as u32) != 0 {
-                               self.monitor_pending_commitment_signed = true;
+                       if self.context.channel_state & (ChannelState::MonitorUpdateInProgress as u32) != 0 {
+                               self.context.monitor_pending_commitment_signed = true;
                                Ok(ReestablishResponses {
                                        channel_ready, shutdown_msg, announcement_sigs,
                                        commitment_update: None, raa: None,
-                                       order: self.resend_order.clone(),
+                                       order: self.context.resend_order.clone(),
                                })
                        } else {
                                Ok(ReestablishResponses {
                                        channel_ready, shutdown_msg, announcement_sigs,
                                        raa: required_revoke,
                                        commitment_update: Some(self.get_last_commitment_update(logger)),
-                                       order: self.resend_order.clone(),
+                                       order: self.context.resend_order.clone(),
                                })
                        }
                } else {
@@ -4251,14 +3975,14 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                -> (u64, u64)
                where F::Target: FeeEstimator
        {
-               if let Some((min, max)) = self.closing_fee_limits { return (min, max); }
+               if let Some((min, max)) = self.context.closing_fee_limits { return (min, max); }
 
                // Propose a range from our current Background feerate to our Normal feerate plus our
                // force_close_avoidance_max_fee_satoshis.
                // If we fail to come to consensus, we'll have to force-close.
                let mut proposed_feerate = fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Background);
                let normal_feerate = fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Normal);
-               let mut proposed_max_feerate = if self.is_outbound() { normal_feerate } else { u32::max_value() };
+               let mut proposed_max_feerate = if self.context.is_outbound() { normal_feerate } else { u32::max_value() };
 
                // The spec requires that (when the channel does not have anchors) we only send absolute
                // channel fees no greater than the absolute channel fee on the current commitment
@@ -4266,8 +3990,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                // very good reason to apply such a limit in any case. We don't bother doing so, risking
                // some force-closure by old nodes, but we wanted to close the channel anyway.
 
-               if let Some(target_feerate) = self.target_closing_feerate_sats_per_kw {
-                       let min_feerate = if self.is_outbound() { target_feerate } else { cmp::min(self.feerate_per_kw, target_feerate) };
+               if let Some(target_feerate) = self.context.target_closing_feerate_sats_per_kw {
+                       let min_feerate = if self.context.is_outbound() { target_feerate } else { cmp::min(self.context.feerate_per_kw, target_feerate) };
                        proposed_feerate = cmp::max(proposed_feerate, min_feerate);
                        proposed_max_feerate = cmp::max(proposed_max_feerate, min_feerate);
                }
@@ -4279,20 +4003,20 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                // come to consensus with our counterparty on appropriate fees, however it should be a
                // relatively rare case. We can revisit this later, though note that in order to determine
                // if the funders' output is dust we have to know the absolute fee we're going to use.
-               let tx_weight = self.get_closing_transaction_weight(Some(&self.get_closing_scriptpubkey()), Some(self.counterparty_shutdown_scriptpubkey.as_ref().unwrap()));
+               let tx_weight = self.get_closing_transaction_weight(Some(&self.get_closing_scriptpubkey()), Some(self.context.counterparty_shutdown_scriptpubkey.as_ref().unwrap()));
                let proposed_total_fee_satoshis = proposed_feerate as u64 * tx_weight / 1000;
-               let proposed_max_total_fee_satoshis = if self.is_outbound() {
+               let proposed_max_total_fee_satoshis = if self.context.is_outbound() {
                                // We always add force_close_avoidance_max_fee_satoshis to our normal
                                // feerate-calculated fee, but allow the max to be overridden if we're using a
                                // target feerate-calculated fee.
-                               cmp::max(normal_feerate as u64 * tx_weight / 1000 + self.config.options.force_close_avoidance_max_fee_satoshis,
+                               cmp::max(normal_feerate as u64 * tx_weight / 1000 + self.context.config.options.force_close_avoidance_max_fee_satoshis,
                                        proposed_max_feerate as u64 * tx_weight / 1000)
                        } else {
-                               self.channel_value_satoshis - (self.value_to_self_msat + 999) / 1000
+                               self.context.channel_value_satoshis - (self.context.value_to_self_msat + 999) / 1000
                        };
 
-               self.closing_fee_limits = Some((proposed_total_fee_satoshis, proposed_max_total_fee_satoshis));
-               self.closing_fee_limits.clone().unwrap()
+               self.context.closing_fee_limits = Some((proposed_total_fee_satoshis, proposed_max_total_fee_satoshis));
+               self.context.closing_fee_limits.clone().unwrap()
        }
 
        /// Returns true if we're ready to commence the closing_signed negotiation phase. This is true
@@ -4300,12 +4024,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
        /// this point if we're the funder we should send the initial closing_signed, and in any case
        /// shutdown should complete within a reasonable timeframe.
        fn closing_negotiation_ready(&self) -> bool {
-               self.pending_inbound_htlcs.is_empty() && self.pending_outbound_htlcs.is_empty() &&
-                       self.channel_state &
-                               (BOTH_SIDES_SHUTDOWN_MASK | ChannelState::AwaitingRemoteRevoke as u32 |
-                                ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress as u32)
-                               == BOTH_SIDES_SHUTDOWN_MASK &&
-                       self.pending_update_fee.is_none()
+               self.context.closing_negotiation_ready()
        }
 
        /// Checks if the closing_signed negotiation is making appropriate progress, possibly returning
@@ -4313,10 +4032,10 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
        /// Should be called on a one-minute timer.
        pub fn timer_check_closing_negotiation_progress(&mut self) -> Result<(), ChannelError> {
                if self.closing_negotiation_ready() {
-                       if self.closing_signed_in_flight {
+                       if self.context.closing_signed_in_flight {
                                return Err(ChannelError::Close("closing_signed negotiation failed to finish within two timer ticks".to_owned()));
                        } else {
-                               self.closing_signed_in_flight = true;
+                               self.context.closing_signed_in_flight = true;
                        }
                }
                Ok(())
@@ -4327,12 +4046,12 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                -> Result<(Option<msgs::ClosingSigned>, Option<Transaction>), ChannelError>
                where F::Target: FeeEstimator, L::Target: Logger
        {
-               if self.last_sent_closing_fee.is_some() || !self.closing_negotiation_ready() {
+               if self.context.last_sent_closing_fee.is_some() || !self.closing_negotiation_ready() {
                        return Ok((None, None));
                }
 
-               if !self.is_outbound() {
-                       if let Some(msg) = &self.pending_counterparty_closing_signed.take() {
+               if !self.context.is_outbound() {
+                       if let Some(msg) = &self.context.pending_counterparty_closing_signed.take() {
                                return self.closing_signed(fee_estimator, &msg);
                        }
                        return Ok((None, None));
@@ -4340,18 +4059,18 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
 
                let (our_min_fee, our_max_fee) = self.calculate_closing_fee_limits(fee_estimator);
 
-               assert!(self.shutdown_scriptpubkey.is_some());
+               assert!(self.context.shutdown_scriptpubkey.is_some());
                let (closing_tx, total_fee_satoshis) = self.build_closing_transaction(our_min_fee, false);
                log_trace!(logger, "Proposing initial closing_signed for our counterparty with a fee range of {}-{} sat (with initial proposal {} sats)",
                        our_min_fee, our_max_fee, total_fee_satoshis);
 
-               let sig = self.holder_signer
-                       .sign_closing_transaction(&closing_tx, &self.secp_ctx)
+               let sig = self.context.holder_signer
+                       .sign_closing_transaction(&closing_tx, &self.context.secp_ctx)
                        .map_err(|()| ChannelError::Close("Failed to get signature for closing transaction.".to_owned()))?;
 
-               self.last_sent_closing_fee = Some((total_fee_satoshis, sig.clone()));
+               self.context.last_sent_closing_fee = Some((total_fee_satoshis, sig.clone()));
                Ok((Some(msgs::ClosingSigned {
-                       channel_id: self.channel_id,
+                       channel_id: self.context.channel_id,
                        fee_satoshis: total_fee_satoshis,
                        signature: sig,
                        fee_range: Some(msgs::ClosingSignedFeeRange {
@@ -4361,45 +4080,67 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                }), None))
        }
 
+       // Marks a channel as waiting for a response from the counterparty. If it's not received
+       // [`DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`] after sending our own to them, then we'll attempt
+       // a reconnection.
+       fn mark_awaiting_response(&mut self) {
+               self.context.sent_message_awaiting_response = Some(0);
+       }
+
+       /// Determines whether we should disconnect the counterparty due to not receiving a response
+       /// within our expected timeframe.
+       ///
+       /// This should be called on every [`super::channelmanager::ChannelManager::timer_tick_occurred`].
+       pub fn should_disconnect_peer_awaiting_response(&mut self) -> bool {
+               let ticks_elapsed = if let Some(ticks_elapsed) = self.context.sent_message_awaiting_response.as_mut() {
+                       ticks_elapsed
+               } else {
+                       // Don't disconnect when we're not waiting on a response.
+                       return false;
+               };
+               *ticks_elapsed += 1;
+               *ticks_elapsed >= DISCONNECT_PEER_AWAITING_RESPONSE_TICKS
+       }
+
        pub fn shutdown<SP: Deref>(
                &mut self, signer_provider: &SP, their_features: &InitFeatures, msg: &msgs::Shutdown
-       ) -> Result<(Option<msgs::Shutdown>, Option<&ChannelMonitorUpdate>, Vec<(HTLCSource, PaymentHash)>), ChannelError>
+       ) -> Result<(Option<msgs::Shutdown>, Option<ChannelMonitorUpdate>, Vec<(HTLCSource, PaymentHash)>), ChannelError>
        where SP::Target: SignerProvider
        {
-               if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
+               if self.context.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
                        return Err(ChannelError::Close("Peer sent shutdown when we needed a channel_reestablish".to_owned()));
                }
-               if self.channel_state < ChannelState::FundingSent as u32 {
+               if self.context.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".to_owned()));
                }
-               for htlc in self.pending_inbound_htlcs.iter() {
+               for htlc in self.context.pending_inbound_htlcs.iter() {
                        if let InboundHTLCState::RemoteAnnounced(_) = htlc.state {
                                return Err(ChannelError::Close("Got shutdown with remote pending HTLCs".to_owned()));
                        }
                }
-               assert_eq!(self.channel_state & ChannelState::ShutdownComplete as u32, 0);
+               assert_eq!(self.context.channel_state & ChannelState::ShutdownComplete as u32, 0);
 
                if !script::is_bolt2_compliant(&msg.scriptpubkey, their_features) {
                        return Err(ChannelError::Warn(format!("Got a nonstandard scriptpubkey ({}) from remote peer", msg.scriptpubkey.to_bytes().to_hex())));
                }
 
-               if self.counterparty_shutdown_scriptpubkey.is_some() {
-                       if Some(&msg.scriptpubkey) != self.counterparty_shutdown_scriptpubkey.as_ref() {
+               if self.context.counterparty_shutdown_scriptpubkey.is_some() {
+                       if Some(&msg.scriptpubkey) != self.context.counterparty_shutdown_scriptpubkey.as_ref() {
                                return Err(ChannelError::Warn(format!("Got shutdown request with a scriptpubkey ({}) which did not match their previous scriptpubkey.", msg.scriptpubkey.to_bytes().to_hex())));
                        }
                } else {
-                       self.counterparty_shutdown_scriptpubkey = Some(msg.scriptpubkey.clone());
+                       self.context.counterparty_shutdown_scriptpubkey = Some(msg.scriptpubkey.clone());
                }
 
                // If we have any LocalAnnounced updates we'll probably just get back an update_fail_htlc
                // immediately after the commitment dance, but we can send a Shutdown because we won't send
                // any further commitment updates after we set LocalShutdownSent.
-               let send_shutdown = (self.channel_state & ChannelState::LocalShutdownSent as u32) != ChannelState::LocalShutdownSent as u32;
+               let send_shutdown = (self.context.channel_state & ChannelState::LocalShutdownSent as u32) != ChannelState::LocalShutdownSent as u32;
 
-               let update_shutdown_script = match self.shutdown_scriptpubkey {
+               let update_shutdown_script = match self.context.shutdown_scriptpubkey {
                        Some(_) => false,
                        None => {
                                assert!(send_shutdown);
@@ -4410,32 +4151,30 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                                if !shutdown_scriptpubkey.is_compatible(their_features) {
                                        return Err(ChannelError::Close(format!("Provided a scriptpubkey format not accepted by peer: {}", shutdown_scriptpubkey)));
                                }
-                               self.shutdown_scriptpubkey = Some(shutdown_scriptpubkey);
+                               self.context.shutdown_scriptpubkey = Some(shutdown_scriptpubkey);
                                true
                        },
                };
 
                // From here on out, we may not fail!
 
-               self.channel_state |= ChannelState::RemoteShutdownSent as u32;
-               self.update_time_counter += 1;
+               self.context.channel_state |= ChannelState::RemoteShutdownSent as u32;
+               self.context.update_time_counter += 1;
 
                let monitor_update = if update_shutdown_script {
-                       self.latest_monitor_update_id += 1;
+                       self.context.latest_monitor_update_id += 1;
                        let monitor_update = ChannelMonitorUpdate {
-                               update_id: self.latest_monitor_update_id,
+                               update_id: self.context.latest_monitor_update_id,
                                updates: vec![ChannelMonitorUpdateStep::ShutdownScript {
                                        scriptpubkey: self.get_closing_scriptpubkey(),
                                }],
                        };
                        self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
-                       if self.push_blockable_mon_update(monitor_update) {
-                               self.pending_monitor_updates.last().map(|upd| &upd.update)
-                       } else { None }
+                       self.push_ret_blockable_mon_update(monitor_update)
                } else { None };
                let shutdown = if send_shutdown {
                        Some(msgs::Shutdown {
-                               channel_id: self.channel_id,
+                               channel_id: self.context.channel_id,
                                scriptpubkey: self.get_closing_scriptpubkey(),
                        })
                } else { None };
@@ -4443,9 +4182,9 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                // 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| {
+               self.context.holding_cell_update_fee = None;
+               let mut dropped_outbound_htlcs = Vec::with_capacity(self.context.holding_cell_htlc_updates.len());
+               self.context.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()));
@@ -4455,8 +4194,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                        }
                });
 
-               self.channel_state |= ChannelState::LocalShutdownSent as u32;
-               self.update_time_counter += 1;
+               self.context.channel_state |= ChannelState::LocalShutdownSent as u32;
+               self.context.update_time_counter += 1;
 
                Ok((shutdown, monitor_update, dropped_outbound_htlcs))
        }
@@ -4466,8 +4205,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
 
                tx.input[0].witness.push(Vec::new()); // First is the multisig dummy
 
-               let funding_key = self.get_holder_pubkeys().funding_pubkey.serialize();
-               let counterparty_funding_key = self.counterparty_funding_pubkey().serialize();
+               let funding_key = self.context.get_holder_pubkeys().funding_pubkey.serialize();
+               let counterparty_funding_key = self.context.counterparty_funding_pubkey().serialize();
                let mut holder_sig = sig.serialize_der().to_vec();
                holder_sig.push(EcdsaSighashType::All as u8);
                let mut cp_sig = counterparty_sig.serialize_der().to_vec();
@@ -4480,7 +4219,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                        tx.input[0].witness.push(holder_sig);
                }
 
-               tx.input[0].witness.push(self.get_funding_redeemscript().into_bytes());
+               tx.input[0].witness.push(self.context.get_funding_redeemscript().into_bytes());
                tx
        }
 
@@ -4489,43 +4228,43 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                -> Result<(Option<msgs::ClosingSigned>, Option<Transaction>), ChannelError>
                where F::Target: FeeEstimator
        {
-               if self.channel_state & BOTH_SIDES_SHUTDOWN_MASK != BOTH_SIDES_SHUTDOWN_MASK {
+               if self.context.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".to_owned()));
                }
-               if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
+               if self.context.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
                        return Err(ChannelError::Close("Peer sent closing_signed when we needed a channel_reestablish".to_owned()));
                }
-               if !self.pending_inbound_htlcs.is_empty() || !self.pending_outbound_htlcs.is_empty() {
+               if !self.context.pending_inbound_htlcs.is_empty() || !self.context.pending_outbound_htlcs.is_empty() {
                        return Err(ChannelError::Close("Remote end sent us a closing_signed while there were still pending HTLCs".to_owned()));
                }
                if msg.fee_satoshis > TOTAL_BITCOIN_SUPPLY_SATOSHIS { // 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".to_owned()));
                }
 
-               if self.is_outbound() && self.last_sent_closing_fee.is_none() {
+               if self.context.is_outbound() && self.context.last_sent_closing_fee.is_none() {
                        return Err(ChannelError::Close("Remote tried to send a closing_signed when we were supposed to propose the first one".to_owned()));
                }
 
-               if self.channel_state & ChannelState::MonitorUpdateInProgress as u32 != 0 {
-                       self.pending_counterparty_closing_signed = Some(msg.clone());
+               if self.context.channel_state & ChannelState::MonitorUpdateInProgress as u32 != 0 {
+                       self.context.pending_counterparty_closing_signed = Some(msg.clone());
                        return Ok((None, None));
                }
 
-               let funding_redeemscript = self.get_funding_redeemscript();
+               let funding_redeemscript = self.context.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(format!("Remote sent us a closing_signed with a fee other than the value they can claim. Fee in message: {}. Actual closing tx fee: {}", msg.fee_satoshis, used_total_fee)));
                }
-               let sighash = closing_tx.trust().get_sighash_all(&funding_redeemscript, self.channel_value_satoshis);
+               let sighash = closing_tx.trust().get_sighash_all(&funding_redeemscript, self.context.channel_value_satoshis);
 
-               match self.secp_ctx.verify_ecdsa(&sighash, &msg.signature, &self.get_counterparty_pubkeys().funding_pubkey) {
+               match self.context.secp_ctx.verify_ecdsa(&sighash, &msg.signature, &self.context.get_counterparty_pubkeys().funding_pubkey) {
                        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;
-                               let sighash = closing_tx.trust().get_sighash_all(&funding_redeemscript, self.channel_value_satoshis);
-                               secp_check!(self.secp_ctx.verify_ecdsa(&sighash, &msg.signature, self.counterparty_funding_pubkey()), "Invalid closing tx signature from peer".to_owned());
+                               let sighash = closing_tx.trust().get_sighash_all(&funding_redeemscript, self.context.channel_value_satoshis);
+                               secp_check!(self.context.secp_ctx.verify_ecdsa(&sighash, &msg.signature, self.context.counterparty_funding_pubkey()), "Invalid closing tx signature from peer".to_owned());
                        },
                };
 
@@ -4535,12 +4274,12 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                        }
                }
 
-               assert!(self.shutdown_scriptpubkey.is_some());
-               if let Some((last_fee, sig)) = self.last_sent_closing_fee {
+               assert!(self.context.shutdown_scriptpubkey.is_some());
+               if let Some((last_fee, sig)) = self.context.last_sent_closing_fee {
                        if last_fee == msg.fee_satoshis {
                                let tx = self.build_signed_closing_transaction(&mut closing_tx, &msg.signature, &sig);
-                               self.channel_state = ChannelState::ShutdownComplete as u32;
-                               self.update_time_counter += 1;
+                               self.context.channel_state = ChannelState::ShutdownComplete as u32;
+                               self.context.update_time_counter += 1;
                                return Ok((None, Some(tx)));
                        }
                }
@@ -4555,20 +4294,20 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                                        self.build_closing_transaction($new_fee, false)
                                };
 
-                               let sig = self.holder_signer
-                                       .sign_closing_transaction(&closing_tx, &self.secp_ctx)
+                               let sig = self.context.holder_signer
+                                       .sign_closing_transaction(&closing_tx, &self.context.secp_ctx)
                                        .map_err(|_| ChannelError::Close("External signer refused to sign closing transaction".to_owned()))?;
 
                                let signed_tx = if $new_fee == msg.fee_satoshis {
-                                       self.channel_state = ChannelState::ShutdownComplete as u32;
-                                       self.update_time_counter += 1;
+                                       self.context.channel_state = ChannelState::ShutdownComplete as u32;
+                                       self.context.update_time_counter += 1;
                                        let tx = self.build_signed_closing_transaction(&closing_tx, &msg.signature, &sig);
                                        Some(tx)
                                } else { None };
 
-                               self.last_sent_closing_fee = Some((used_fee, sig.clone()));
+                               self.context.last_sent_closing_fee = Some((used_fee, sig.clone()));
                                return Ok((Some(msgs::ClosingSigned {
-                                       channel_id: self.channel_id,
+                                       channel_id: self.context.channel_id,
                                        fee_satoshis: used_fee,
                                        signature: sig,
                                        fee_range: Some(msgs::ClosingSignedFeeRange {
@@ -4590,10 +4329,10 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                                return Err(ChannelError::Warn(format!("Unable to come to consensus about closing feerate, remote's min fee ({} sat) was greater than our max fee ({} sat)", min_fee_satoshis, our_max_fee)));
                        }
 
-                       if !self.is_outbound() {
+                       if !self.context.is_outbound() {
                                // They have to pay, so pick the highest fee in the overlapping range.
                                // We should never set an upper bound aside from their full balance
-                               debug_assert_eq!(our_max_fee, self.channel_value_satoshis - (self.value_to_self_msat + 999) / 1000);
+                               debug_assert_eq!(our_max_fee, self.context.channel_value_satoshis - (self.context.value_to_self_msat + 999) / 1000);
                                propose_fee!(cmp::min(max_fee_satoshis, our_max_fee));
                        } else {
                                if msg.fee_satoshis < our_min_fee || msg.fee_satoshis > our_max_fee {
@@ -4606,7 +4345,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                } else {
                        // Old fee style negotiation. We don't bother to enforce whether they are complying
                        // with the "making progress" requirements, we just comply and hope for the best.
-                       if let Some((last_fee, _)) = self.last_sent_closing_fee {
+                       if let Some((last_fee, _)) = self.context.last_sent_closing_fee {
                                if msg.fee_satoshis > last_fee {
                                        if msg.fee_satoshis < our_max_fee {
                                                propose_fee!(msg.fee_satoshis);
@@ -4636,552 +4375,253 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                }
        }
 
-       // Public utilities:
-
-       pub fn channel_id(&self) -> [u8; 32] {
-               self.channel_id
-       }
-
-       // Return the `temporary_channel_id` used during channel establishment.
-       //
-       // Will return `None` for channels created prior to LDK version 0.0.115.
-       pub fn temporary_channel_id(&self) -> Option<[u8; 32]> {
-               self.temporary_channel_id
+       fn internal_htlc_satisfies_config(
+               &self, htlc: &msgs::UpdateAddHTLC, amt_to_forward: u64, outgoing_cltv_value: u32, config: &ChannelConfig,
+       ) -> Result<(), (&'static str, u16)> {
+               let fee = amt_to_forward.checked_mul(config.forwarding_fee_proportional_millionths as u64)
+                       .and_then(|prop_fee| (prop_fee / 1000000).checked_add(config.forwarding_fee_base_msat as u64));
+               if fee.is_none() || htlc.amount_msat < fee.unwrap() ||
+                       (htlc.amount_msat - fee.unwrap()) < amt_to_forward {
+                       return Err((
+                               "Prior hop has deviated from specified fees parameters or origin node has obsolete ones",
+                               0x1000 | 12, // fee_insufficient
+                       ));
+               }
+               if (htlc.cltv_expiry as u64) < outgoing_cltv_value as u64 + config.cltv_expiry_delta as u64 {
+                       return Err((
+                               "Forwarding node has tampered with the intended HTLC values or origin node has an obsolete cltv_expiry_delta",
+                               0x1000 | 13, // incorrect_cltv_expiry
+                       ));
+               }
+               Ok(())
        }
 
-       pub fn minimum_depth(&self) -> Option<u32> {
-               self.minimum_depth
+       /// Determines whether the parameters of an incoming HTLC to be forwarded satisfy the channel's
+       /// [`ChannelConfig`]. This first looks at the channel's current [`ChannelConfig`], and if
+       /// unsuccessful, falls back to the previous one if one exists.
+       pub fn htlc_satisfies_config(
+               &self, htlc: &msgs::UpdateAddHTLC, amt_to_forward: u64, outgoing_cltv_value: u32,
+       ) -> Result<(), (&'static str, u16)> {
+               self.internal_htlc_satisfies_config(&htlc, amt_to_forward, outgoing_cltv_value, &self.context.config())
+                       .or_else(|err| {
+                               if let Some(prev_config) = self.context.prev_config() {
+                                       self.internal_htlc_satisfies_config(htlc, amt_to_forward, outgoing_cltv_value, &prev_config)
+                               } else {
+                                       Err(err)
+                               }
+                       })
        }
 
-       /// 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) -> u128 {
-               self.user_id
+       pub fn get_cur_holder_commitment_transaction_number(&self) -> u64 {
+               self.context.cur_holder_commitment_transaction_number + 1
        }
 
-       /// Gets the channel's type
-       pub fn get_channel_type(&self) -> &ChannelTypeFeatures {
-               &self.channel_type
+       pub fn get_cur_counterparty_commitment_transaction_number(&self) -> u64 {
+               self.context.cur_counterparty_commitment_transaction_number + 1 - if self.context.channel_state & (ChannelState::AwaitingRemoteRevoke as u32) != 0 { 1 } else { 0 }
        }
 
-       /// Guaranteed to be Some after both ChannelReady 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
+       pub fn get_revoked_counterparty_commitment_transaction_number(&self) -> u64 {
+               self.context.cur_counterparty_commitment_transaction_number + 2
        }
 
-       /// Allowed in any state (including after shutdown)
-       pub fn latest_inbound_scid_alias(&self) -> Option<u64> {
-               self.latest_inbound_scid_alias
+       #[cfg(test)]
+       pub fn get_signer(&self) -> &Signer {
+               &self.context.holder_signer
        }
 
-       /// Allowed in any state (including after shutdown)
-       pub fn outbound_scid_alias(&self) -> u64 {
-               self.outbound_scid_alias
-       }
-       /// Only allowed immediately after deserialization if get_outbound_scid_alias returns 0,
-       /// indicating we were written by LDK prior to 0.0.106 which did not set outbound SCID aliases.
-       pub fn set_outbound_scid_alias(&mut self, outbound_scid_alias: u64) {
-               assert_eq!(self.outbound_scid_alias, 0);
-               self.outbound_scid_alias = outbound_scid_alias;
-       }
-
-       /// 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_transaction_parameters.funding_outpoint
-       }
-
-       /// Returns the block hash in which our funding transaction was confirmed.
-       pub fn get_funding_tx_confirmed_in(&self) -> Option<BlockHash> {
-               self.funding_tx_confirmed_in
-       }
-
-       /// Returns the current number of confirmations on the funding transaction.
-       pub fn get_funding_tx_confirmations(&self, height: u32) -> u32 {
-               if self.funding_tx_confirmation_height == 0 {
-                       // We either haven't seen any confirmation yet, or observed a reorg.
-                       return 0;
+       #[cfg(test)]
+       pub fn get_value_stat(&self) -> ChannelValueStat {
+               ChannelValueStat {
+                       value_to_self_msat: self.context.value_to_self_msat,
+                       channel_value_msat: self.context.channel_value_satoshis * 1000,
+                       channel_reserve_msat: self.context.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000,
+                       pending_outbound_htlcs_amount_msat: self.context.pending_outbound_htlcs.iter().map(|ref h| h.amount_msat).sum::<u64>(),
+                       pending_inbound_htlcs_amount_msat: self.context.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.context.holding_cell_htlc_updates.iter() {
+                                       match h {
+                                               &HTLCUpdateAwaitingACK::AddHTLC{amount_msat, .. } => {
+                                                       res += amount_msat;
+                                               }
+                                               _ => {}
+                                       }
+                               }
+                               res
+                       },
+                       counterparty_max_htlc_value_in_flight_msat: self.context.counterparty_max_htlc_value_in_flight_msat,
+                       counterparty_dust_limit_msat: self.context.counterparty_dust_limit_satoshis * 1000,
                }
-
-               height.checked_sub(self.funding_tx_confirmation_height).map_or(0, |c| c + 1)
-       }
-
-       fn get_holder_selected_contest_delay(&self) -> u16 {
-               self.channel_transaction_parameters.holder_selected_contest_delay
-       }
-
-       fn get_holder_pubkeys(&self) -> &ChannelPublicKeys {
-               &self.channel_transaction_parameters.holder_pubkeys
-       }
-
-       pub fn get_counterparty_selected_contest_delay(&self) -> Option<u16> {
-               self.channel_transaction_parameters.counterparty_parameters
-                       .as_ref().map(|params| params.selected_contest_delay)
-       }
-
-       fn get_counterparty_pubkeys(&self) -> &ChannelPublicKeys {
-               &self.channel_transaction_parameters.counterparty_parameters.as_ref().unwrap().pubkeys
-       }
-
-       /// Allowed in any state (including after shutdown)
-       pub fn get_counterparty_node_id(&self) -> PublicKey {
-               self.counterparty_node_id
-       }
-
-       /// Allowed in any state (including after shutdown)
-       pub fn get_holder_htlc_minimum_msat(&self) -> u64 {
-               self.holder_htlc_minimum_msat
-       }
-
-       /// Allowed in any state (including after shutdown), but will return none before TheirInitSent
-       pub fn get_holder_htlc_maximum_msat(&self) -> Option<u64> {
-               self.get_htlc_maximum_msat(self.holder_max_htlc_value_in_flight_msat)
-       }
-
-       /// Allowed in any state (including after shutdown)
-       pub fn get_announced_htlc_max_msat(&self) -> u64 {
-               return cmp::min(
-                       // Upper bound by capacity. We make it a bit less than full capacity to prevent attempts
-                       // to use full capacity. This is an effort to reduce routing failures, because in many cases
-                       // channel might have been used to route very small values (either by honest users or as DoS).
-                       self.channel_value_satoshis * 1000 * 9 / 10,
-
-                       self.counterparty_max_htlc_value_in_flight_msat
-               );
        }
 
+       /// 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 get_counterparty_htlc_minimum_msat(&self) -> u64 {
-               self.counterparty_htlc_minimum_msat
+       pub fn is_awaiting_monitor_update(&self) -> bool {
+               (self.context.channel_state & ChannelState::MonitorUpdateInProgress as u32) != 0
        }
 
-       /// Allowed in any state (including after shutdown), but will return none before TheirInitSent
-       pub fn get_counterparty_htlc_maximum_msat(&self) -> Option<u64> {
-               self.get_htlc_maximum_msat(self.counterparty_max_htlc_value_in_flight_msat)
+       /// Gets the latest [`ChannelMonitorUpdate`] ID which has been released and is in-flight.
+       pub fn get_latest_unblocked_monitor_update_id(&self) -> u64 {
+               if self.context.blocked_monitor_updates.is_empty() { return self.context.get_latest_monitor_update_id(); }
+               self.context.blocked_monitor_updates[0].update.update_id - 1
        }
 
-       fn get_htlc_maximum_msat(&self, party_max_htlc_value_in_flight_msat: u64) -> Option<u64> {
-               self.counterparty_selected_channel_reserve_satoshis.map(|counterparty_reserve| {
-                       let holder_reserve = self.holder_selected_channel_reserve_satoshis;
-                       cmp::min(
-                               (self.channel_value_satoshis - counterparty_reserve - holder_reserve) * 1000,
-                               party_max_htlc_value_in_flight_msat
-                       )
-               })
+       /// Returns the next blocked monitor update, if one exists, and a bool which indicates a
+       /// further blocked monitor update exists after the next.
+       pub fn unblock_next_blocked_monitor_update(&mut self) -> Option<(ChannelMonitorUpdate, bool)> {
+               if self.context.blocked_monitor_updates.is_empty() { return None; }
+               Some((self.context.blocked_monitor_updates.remove(0).update,
+                       !self.context.blocked_monitor_updates.is_empty()))
        }
 
-       pub fn get_value_satoshis(&self) -> u64 {
-               self.channel_value_satoshis
+       /// Pushes a new monitor update into our monitor update queue, returning it if it should be
+       /// immediately given to the user for persisting or `None` if it should be held as blocked.
+       fn push_ret_blockable_mon_update(&mut self, update: ChannelMonitorUpdate)
+       -> Option<ChannelMonitorUpdate> {
+               let release_monitor = self.context.blocked_monitor_updates.is_empty();
+               if !release_monitor {
+                       self.context.blocked_monitor_updates.push(PendingChannelMonitorUpdate {
+                               update,
+                       });
+                       None
+               } else {
+                       Some(update)
+               }
        }
 
-       pub fn get_fee_proportional_millionths(&self) -> u32 {
-               self.config.options.forwarding_fee_proportional_millionths
+       pub fn blocked_monitor_updates_pending(&self) -> usize {
+               self.context.blocked_monitor_updates.len()
        }
 
-       pub fn get_cltv_expiry_delta(&self) -> u16 {
-               cmp::max(self.config.options.cltv_expiry_delta, MIN_CLTV_EXPIRY_DELTA)
+       /// Returns true if the channel is awaiting the persistence of the initial ChannelMonitor.
+       /// If the channel is outbound, this implies we have not yet broadcasted the funding
+       /// transaction. If the channel is inbound, this implies simply that the channel has not
+       /// advanced state.
+       pub fn is_awaiting_initial_mon_persist(&self) -> bool {
+               if !self.is_awaiting_monitor_update() { return false; }
+               if self.context.channel_state &
+                       !(ChannelState::TheirChannelReady as u32 | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress as u32)
+                               == ChannelState::FundingSent as u32 {
+                       // If we're not a 0conf channel, we'll be waiting on a monitor update with only
+                       // FundingSent set, though our peer could have sent their channel_ready.
+                       debug_assert!(self.context.minimum_depth.unwrap_or(1) > 0);
+                       return true;
+               }
+               if self.context.cur_holder_commitment_transaction_number == INITIAL_COMMITMENT_NUMBER - 1 &&
+                       self.context.cur_counterparty_commitment_transaction_number == INITIAL_COMMITMENT_NUMBER - 1 {
+                       // If we're a 0-conf channel, we'll move beyond FundingSent immediately even while
+                       // waiting for the initial monitor persistence. Thus, we check if our commitment
+                       // transaction numbers have both been iterated only exactly once (for the
+                       // funding_signed), and we're awaiting monitor update.
+                       //
+                       // If we got here, we shouldn't have yet broadcasted the funding transaction (as the
+                       // only way to get an awaiting-monitor-update state during initial funding is if the
+                       // initial monitor persistence is still pending).
+                       //
+                       // Because deciding we're awaiting initial broadcast spuriously could result in
+                       // funds-loss (as we don't have a monitor, but have the funding transaction confirmed),
+                       // we hard-assert here, even in production builds.
+                       if self.context.is_outbound() { assert!(self.context.funding_transaction.is_some()); }
+                       assert!(self.context.monitor_pending_channel_ready);
+                       assert_eq!(self.context.latest_monitor_update_id, 0);
+                       return true;
+               }
+               false
        }
 
-       pub fn get_max_dust_htlc_exposure_msat(&self) -> u64 {
-               self.config.options.max_dust_htlc_exposure_msat
+       /// Returns true if our channel_ready has been sent
+       pub fn is_our_channel_ready(&self) -> bool {
+               (self.context.channel_state & ChannelState::OurChannelReady as u32) != 0 || self.context.channel_state >= ChannelState::ChannelReady as u32
        }
 
-       /// Returns the previous [`ChannelConfig`] applied to this channel, if any.
-       pub fn prev_config(&self) -> Option<ChannelConfig> {
-               self.prev_config.map(|prev_config| prev_config.0)
+       /// Returns true if our peer has either initiated or agreed to shut down the channel.
+       pub fn received_shutdown(&self) -> bool {
+               (self.context.channel_state & ChannelState::RemoteShutdownSent as u32) != 0
        }
 
-       // Checks whether we should emit a `ChannelPending` event.
-       pub(crate) fn should_emit_channel_pending_event(&mut self) -> bool {
-               self.is_funding_initiated() && !self.channel_pending_event_emitted
+       /// Returns true if we either initiated or agreed to shut down the channel.
+       pub fn sent_shutdown(&self) -> bool {
+               (self.context.channel_state & ChannelState::LocalShutdownSent as u32) != 0
        }
 
-       // Returns whether we already emitted a `ChannelPending` event.
-       pub(crate) fn channel_pending_event_emitted(&self) -> bool {
-               self.channel_pending_event_emitted
+       /// 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.context.channel_state & ChannelState::ShutdownComplete as u32) == ChannelState::ShutdownComplete as u32  {
+                       assert!(self.context.channel_state == ChannelState::ShutdownComplete as u32);
+                       true
+               } else { false }
        }
 
-       // Remembers that we already emitted a `ChannelPending` event.
-       pub(crate) fn set_channel_pending_event_emitted(&mut self) {
-               self.channel_pending_event_emitted = true;
+       pub fn channel_update_status(&self) -> ChannelUpdateStatus {
+               self.context.channel_update_status
        }
 
-       // Checks whether we should emit a `ChannelReady` event.
-       pub(crate) fn should_emit_channel_ready_event(&mut self) -> bool {
-               self.is_usable() && !self.channel_ready_event_emitted
+       pub fn set_channel_update_status(&mut self, status: ChannelUpdateStatus) {
+               self.context.update_time_counter += 1;
+               self.context.channel_update_status = status;
        }
 
-       // Remembers that we already emitted a `ChannelReady` event.
-       pub(crate) fn set_channel_ready_event_emitted(&mut self) {
-               self.channel_ready_event_emitted = true;
-       }
+       fn check_get_channel_ready(&mut self, height: u32) -> Option<msgs::ChannelReady> {
+               // Called:
+               //  * always when a new block/transactions are confirmed with the new height
+               //  * when funding is signed with a height of 0
+               if self.context.funding_tx_confirmation_height == 0 && self.context.minimum_depth != Some(0) {
+                       return None;
+               }
 
-       /// Tracks the number of ticks elapsed since the previous [`ChannelConfig`] was updated. Once
-       /// [`EXPIRE_PREV_CONFIG_TICKS`] is reached, the previous config is considered expired and will
-       /// no longer be considered when forwarding HTLCs.
-       pub fn maybe_expire_prev_config(&mut self) {
-               if self.prev_config.is_none() {
-                       return;
+               let funding_tx_confirmations = height as i64 - self.context.funding_tx_confirmation_height as i64 + 1;
+               if funding_tx_confirmations <= 0 {
+                       self.context.funding_tx_confirmation_height = 0;
                }
-               let prev_config = self.prev_config.as_mut().unwrap();
-               prev_config.1 += 1;
-               if prev_config.1 == EXPIRE_PREV_CONFIG_TICKS {
-                       self.prev_config = None;
+
+               if funding_tx_confirmations < self.context.minimum_depth.unwrap_or(0) as i64 {
+                       return None;
                }
-       }
 
-       /// Returns the current [`ChannelConfig`] applied to the channel.
-       pub fn config(&self) -> ChannelConfig {
-               self.config.options
-       }
+               let non_shutdown_state = self.context.channel_state & (!MULTI_STATE_FLAGS);
+               let need_commitment_update = if non_shutdown_state == ChannelState::FundingSent as u32 {
+                       self.context.channel_state |= ChannelState::OurChannelReady as u32;
+                       true
+               } else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::TheirChannelReady as u32) {
+                       self.context.channel_state = ChannelState::ChannelReady as u32 | (self.context.channel_state & MULTI_STATE_FLAGS);
+                       self.context.update_time_counter += 1;
+                       true
+               } else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::OurChannelReady as u32) {
+                       // We got a reorg but not enough to trigger a force close, just ignore.
+                       false
+               } else {
+                       if self.context.funding_tx_confirmation_height != 0 && self.context.channel_state < ChannelState::ChannelReady as u32 {
+                               // We should never see a funding transaction on-chain until we've received
+                               // funding_signed (if we're an outbound channel), or seen funding_generated (if we're
+                               // an inbound channel - before that we have no known funding TXID). The fuzzer,
+                               // however, may do this and we shouldn't treat it as a bug.
+                               #[cfg(not(fuzzing))]
+                               panic!("Started confirming a channel in a state pre-FundingSent: {}.\n\
+                                       Do NOT broadcast a funding transaction manually - let LDK do it for you!",
+                                       self.context.channel_state);
+                       }
+                       // We got a reorg but not enough to trigger a force close, just ignore.
+                       false
+               };
 
-       /// Updates the channel's config. A bool is returned indicating whether the config update
-       /// applied resulted in a new ChannelUpdate message.
-       pub fn update_config(&mut self, config: &ChannelConfig) -> bool {
-               let did_channel_update =
-                       self.config.options.forwarding_fee_proportional_millionths != config.forwarding_fee_proportional_millionths ||
-                       self.config.options.forwarding_fee_base_msat != config.forwarding_fee_base_msat ||
-                       self.config.options.cltv_expiry_delta != config.cltv_expiry_delta;
-               if did_channel_update {
-                       self.prev_config = Some((self.config.options, 0));
-                       // Update the counter, which backs the ChannelUpdate timestamp, to allow the relay
-                       // policy change to propagate throughout the network.
-                       self.update_time_counter += 1;
+               if need_commitment_update {
+                       if self.context.channel_state & (ChannelState::MonitorUpdateInProgress as u32) == 0 {
+                               if self.context.channel_state & (ChannelState::PeerDisconnected as u32) == 0 {
+                                       let next_per_commitment_point =
+                                               self.context.holder_signer.get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, &self.context.secp_ctx);
+                                       return Some(msgs::ChannelReady {
+                                               channel_id: self.context.channel_id,
+                                               next_per_commitment_point,
+                                               short_channel_id_alias: Some(self.context.outbound_scid_alias),
+                                       });
+                               }
+                       } else {
+                               self.context.monitor_pending_channel_ready = true;
+                       }
                }
-               self.config.options = *config;
-               did_channel_update
-       }
-
-       fn internal_htlc_satisfies_config(
-               &self, htlc: &msgs::UpdateAddHTLC, amt_to_forward: u64, outgoing_cltv_value: u32, config: &ChannelConfig,
-       ) -> Result<(), (&'static str, u16)> {
-               let fee = amt_to_forward.checked_mul(config.forwarding_fee_proportional_millionths as u64)
-                       .and_then(|prop_fee| (prop_fee / 1000000).checked_add(config.forwarding_fee_base_msat as u64));
-               if fee.is_none() || htlc.amount_msat < fee.unwrap() ||
-                       (htlc.amount_msat - fee.unwrap()) < amt_to_forward {
-                       return Err((
-                               "Prior hop has deviated from specified fees parameters or origin node has obsolete ones",
-                               0x1000 | 12, // fee_insufficient
-                       ));
-               }
-               if (htlc.cltv_expiry as u64) < outgoing_cltv_value as u64 + config.cltv_expiry_delta as u64 {
-                       return Err((
-                               "Forwarding node has tampered with the intended HTLC values or origin node has an obsolete cltv_expiry_delta",
-                               0x1000 | 13, // incorrect_cltv_expiry
-                       ));
-               }
-               Ok(())
-       }
-
-       /// Determines whether the parameters of an incoming HTLC to be forwarded satisfy the channel's
-       /// [`ChannelConfig`]. This first looks at the channel's current [`ChannelConfig`], and if
-       /// unsuccessful, falls back to the previous one if one exists.
-       pub fn htlc_satisfies_config(
-               &self, htlc: &msgs::UpdateAddHTLC, amt_to_forward: u64, outgoing_cltv_value: u32,
-       ) -> Result<(), (&'static str, u16)> {
-               self.internal_htlc_satisfies_config(&htlc, amt_to_forward, outgoing_cltv_value, &self.config())
-                       .or_else(|err| {
-                               if let Some(prev_config) = self.prev_config() {
-                                       self.internal_htlc_satisfies_config(htlc, amt_to_forward, outgoing_cltv_value, &prev_config)
-                               } else {
-                                       Err(err)
-                               }
-                       })
-       }
-
-       pub fn get_feerate_sat_per_1000_weight(&self) -> u32 {
-               self.feerate_per_kw
-       }
-
-       pub fn get_dust_buffer_feerate(&self, outbound_feerate_update: Option<u32>) -> u32 {
-               // When calculating our exposure to dust HTLCs, we assume that the channel feerate
-               // may, at any point, increase by at least 10 sat/vB (i.e 2530 sat/kWU) or 25%,
-               // whichever is higher. This ensures that we aren't suddenly exposed to significantly
-               // more dust balance if the feerate increases when we have several HTLCs pending
-               // which are near the dust limit.
-               let mut feerate_per_kw = self.feerate_per_kw;
-               // If there's a pending update fee, use it to ensure we aren't under-estimating
-               // potential feerate updates coming soon.
-               if let Some((feerate, _)) = self.pending_update_fee {
-                       feerate_per_kw = cmp::max(feerate_per_kw, feerate);
-               }
-               if let Some(feerate) = outbound_feerate_update {
-                       feerate_per_kw = cmp::max(feerate_per_kw, feerate);
-               }
-               cmp::max(2530, feerate_per_kw * 1250 / 1000)
-       }
-
-       pub fn get_cur_holder_commitment_transaction_number(&self) -> u64 {
-               self.cur_holder_commitment_transaction_number + 1
-       }
-
-       pub fn get_cur_counterparty_commitment_transaction_number(&self) -> u64 {
-               self.cur_counterparty_commitment_transaction_number + 1 - if self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32) != 0 { 1 } else { 0 }
-       }
-
-       pub fn get_revoked_counterparty_commitment_transaction_number(&self) -> u64 {
-               self.cur_counterparty_commitment_transaction_number + 2
-       }
-
-       #[cfg(test)]
-       pub fn get_signer(&self) -> &Signer {
-               &self.holder_signer
-       }
-
-       #[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.counterparty_selected_channel_reserve_satoshis.unwrap() * 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
-                       },
-                       counterparty_max_htlc_value_in_flight_msat: self.counterparty_max_htlc_value_in_flight_msat,
-                       counterparty_dust_limit_msat: self.counterparty_dust_limit_satoshis * 1000,
-               }
-       }
-
-       /// Allowed in any state (including after shutdown)
-       pub fn get_update_time_counter(&self) -> u32 {
-               self.update_time_counter
-       }
-
-       pub fn get_latest_monitor_update_id(&self) -> u64 {
-               self.latest_monitor_update_id
-       }
-
-       pub fn should_announce(&self) -> bool {
-               self.config.announced_channel
-       }
-
-       pub fn is_outbound(&self) -> bool {
-               self.channel_transaction_parameters.is_outbound_from_holder
-       }
-
-       /// 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_outbound_forwarding_fee_base_msat(&self) -> u32 {
-               self.config.options.forwarding_fee_base_msat
-       }
-
-       /// 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::ChannelReady as u32 | BOTH_SIDES_SHUTDOWN_MASK;
-               (self.channel_state & mask) == (ChannelState::ChannelReady as u32) && !self.monitor_pending_channel_ready
-       }
-
-       /// 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) == 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::MonitorUpdateInProgress as u32) != 0
-       }
-
-       pub fn get_latest_complete_monitor_update_id(&self) -> u64 {
-               if self.pending_monitor_updates.is_empty() { return self.get_latest_monitor_update_id(); }
-               self.pending_monitor_updates[0].update.update_id - 1
-       }
-
-       /// Returns the next blocked monitor update, if one exists, and a bool which indicates a
-       /// further blocked monitor update exists after the next.
-       pub fn unblock_next_blocked_monitor_update(&mut self) -> Option<(&ChannelMonitorUpdate, bool)> {
-               for i in 0..self.pending_monitor_updates.len() {
-                       if self.pending_monitor_updates[i].blocked {
-                               self.pending_monitor_updates[i].blocked = false;
-                               return Some((&self.pending_monitor_updates[i].update,
-                                       self.pending_monitor_updates.len() > i + 1));
-                       }
-               }
-               None
-       }
-
-       /// Pushes a new monitor update into our monitor update queue, returning whether it should be
-       /// immediately given to the user for persisting or if it should be held as blocked.
-       fn push_blockable_mon_update(&mut self, update: ChannelMonitorUpdate) -> bool {
-               let release_monitor = self.pending_monitor_updates.iter().all(|upd| !upd.blocked);
-               self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
-                       update, blocked: !release_monitor
-               });
-               release_monitor
-       }
-
-       /// Pushes a new monitor update into our monitor update queue, returning a reference to it if
-       /// it should be immediately given to the user for persisting or `None` if it should be held as
-       /// blocked.
-       fn push_ret_blockable_mon_update(&mut self, update: ChannelMonitorUpdate)
-       -> Option<&ChannelMonitorUpdate> {
-               let release_monitor = self.push_blockable_mon_update(update);
-               if release_monitor { self.pending_monitor_updates.last().map(|upd| &upd.update) } else { None }
-       }
-
-       pub fn no_monitor_updates_pending(&self) -> bool {
-               self.pending_monitor_updates.is_empty()
-       }
-
-       pub fn complete_one_mon_update(&mut self, update_id: u64) {
-               self.pending_monitor_updates.retain(|upd| upd.update.update_id != update_id);
-       }
-
-       /// Returns true if funding_created was sent/received.
-       pub fn is_funding_initiated(&self) -> bool {
-               self.channel_state >= ChannelState::FundingSent as u32
-       }
-
-       /// Returns true if the channel is awaiting the persistence of the initial ChannelMonitor.
-       /// If the channel is outbound, this implies we have not yet broadcasted the funding
-       /// transaction. If the channel is inbound, this implies simply that the channel has not
-       /// advanced state.
-       pub fn is_awaiting_initial_mon_persist(&self) -> bool {
-               if !self.is_awaiting_monitor_update() { return false; }
-               if self.channel_state &
-                       !(ChannelState::TheirChannelReady as u32 | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress as u32)
-                               == ChannelState::FundingSent as u32 {
-                       // If we're not a 0conf channel, we'll be waiting on a monitor update with only
-                       // FundingSent set, though our peer could have sent their channel_ready.
-                       debug_assert!(self.minimum_depth.unwrap_or(1) > 0);
-                       return true;
-               }
-               if self.cur_holder_commitment_transaction_number == INITIAL_COMMITMENT_NUMBER - 1 &&
-                       self.cur_counterparty_commitment_transaction_number == INITIAL_COMMITMENT_NUMBER - 1 {
-                       // If we're a 0-conf channel, we'll move beyond FundingSent immediately even while
-                       // waiting for the initial monitor persistence. Thus, we check if our commitment
-                       // transaction numbers have both been iterated only exactly once (for the
-                       // funding_signed), and we're awaiting monitor update.
-                       //
-                       // If we got here, we shouldn't have yet broadcasted the funding transaction (as the
-                       // only way to get an awaiting-monitor-update state during initial funding is if the
-                       // initial monitor persistence is still pending).
-                       //
-                       // Because deciding we're awaiting initial broadcast spuriously could result in
-                       // funds-loss (as we don't have a monitor, but have the funding transaction confirmed),
-                       // we hard-assert here, even in production builds.
-                       if self.is_outbound() { assert!(self.funding_transaction.is_some()); }
-                       assert!(self.monitor_pending_channel_ready);
-                       assert_eq!(self.latest_monitor_update_id, 0);
-                       return true;
-               }
-               false
-       }
-
-       /// Returns true if our channel_ready has been sent
-       pub fn is_our_channel_ready(&self) -> bool {
-               (self.channel_state & ChannelState::OurChannelReady as u32) != 0 || self.channel_state >= ChannelState::ChannelReady as u32
-       }
-
-       /// Returns true if our peer has either initiated or agreed to shut down the channel.
-       pub fn received_shutdown(&self) -> bool {
-               (self.channel_state & ChannelState::RemoteShutdownSent as u32) != 0
-       }
-
-       /// Returns true if we either initiated or agreed to shut down the channel.
-       pub fn sent_shutdown(&self) -> bool {
-               (self.channel_state & ChannelState::LocalShutdownSent as u32) != 0
-       }
-
-       /// 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 }
-       }
-
-       pub fn channel_update_status(&self) -> ChannelUpdateStatus {
-               self.channel_update_status
-       }
-
-       pub fn set_channel_update_status(&mut self, status: ChannelUpdateStatus) {
-               self.update_time_counter += 1;
-               self.channel_update_status = status;
-       }
-
-       fn check_get_channel_ready(&mut self, height: u32) -> Option<msgs::ChannelReady> {
-               // Called:
-               //  * always when a new block/transactions are confirmed with the new height
-               //  * when funding is signed with a height of 0
-               if self.funding_tx_confirmation_height == 0 && self.minimum_depth != Some(0) {
-                       return None;
-               }
-
-               let funding_tx_confirmations = height as i64 - self.funding_tx_confirmation_height as i64 + 1;
-               if funding_tx_confirmations <= 0 {
-                       self.funding_tx_confirmation_height = 0;
-               }
-
-               if funding_tx_confirmations < self.minimum_depth.unwrap_or(0) as i64 {
-                       return None;
-               }
-
-               let non_shutdown_state = self.channel_state & (!MULTI_STATE_FLAGS);
-               let need_commitment_update = if non_shutdown_state == ChannelState::FundingSent as u32 {
-                       self.channel_state |= ChannelState::OurChannelReady as u32;
-                       true
-               } else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::TheirChannelReady as u32) {
-                       self.channel_state = ChannelState::ChannelReady as u32 | (self.channel_state & MULTI_STATE_FLAGS);
-                       self.update_time_counter += 1;
-                       true
-               } else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::OurChannelReady as u32) {
-                       // We got a reorg but not enough to trigger a force close, just ignore.
-                       false
-               } else {
-                       if self.funding_tx_confirmation_height != 0 && self.channel_state < ChannelState::ChannelReady as u32 {
-                               // We should never see a funding transaction on-chain until we've received
-                               // funding_signed (if we're an outbound channel), or seen funding_generated (if we're
-                               // an inbound channel - before that we have no known funding TXID). The fuzzer,
-                               // however, may do this and we shouldn't treat it as a bug.
-                               #[cfg(not(fuzzing))]
-                               panic!("Started confirming a channel in a state pre-FundingSent: {}.\n\
-                                       Do NOT broadcast a funding transaction manually - let LDK do it for you!",
-                                       self.channel_state);
-                       }
-                       // We got a reorg but not enough to trigger a force close, just ignore.
-                       false
-               };
-
-               if need_commitment_update {
-                       if self.channel_state & (ChannelState::MonitorUpdateInProgress as u32) == 0 {
-                               if self.channel_state & (ChannelState::PeerDisconnected as u32) == 0 {
-                                       let next_per_commitment_point =
-                                               self.holder_signer.get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, &self.secp_ctx);
-                                       return Some(msgs::ChannelReady {
-                                               channel_id: self.channel_id,
-                                               next_per_commitment_point,
-                                               short_channel_id_alias: Some(self.outbound_scid_alias),
-                                       });
-                               }
-                       } else {
-                               self.monitor_pending_channel_ready = true;
-                       }
-               }
-               None
+               None
        }
 
        /// When a transaction is confirmed, we check whether it is or spends the funding transaction
@@ -5195,16 +4635,16 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                NS::Target: NodeSigner,
                L::Target: Logger
        {
-               if let Some(funding_txo) = self.get_funding_txo() {
+               if let Some(funding_txo) = self.context.get_funding_txo() {
                        for &(index_in_block, tx) in txdata.iter() {
                                // Check if the transaction is the expected funding transaction, and if it is,
                                // check that it pays the right amount to the right script.
-                               if self.funding_tx_confirmation_height == 0 {
+                               if self.context.funding_tx_confirmation_height == 0 {
                                        if tx.txid() == funding_txo.txid {
                                                let txo_idx = funding_txo.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.is_outbound() {
+                                               if txo_idx >= tx.output.len() || tx.output[txo_idx].script_pubkey != self.context.get_funding_redeemscript().to_v0_p2wsh() ||
+                                                               tx.output[txo_idx].value != self.context.channel_value_satoshis {
+                                                       if self.context.is_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
@@ -5213,11 +4653,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                                                                #[cfg(not(fuzzing))]
                                                                panic!("Client called ChannelManager::funding_transaction_generated with bogus transaction!");
                                                        }
-                                                       self.update_time_counter += 1;
+                                                       self.context.update_time_counter += 1;
                                                        let err_reason = "funding tx had wrong script/value or output index";
                                                        return Err(ClosureReason::ProcessingError { err: err_reason.to_owned() });
                                                } else {
-                                                       if self.is_outbound() {
+                                                       if self.context.is_outbound() {
                                                                for input in tx.input.iter() {
                                                                        if input.witness.is_empty() {
                                                                                // We generated a malleable funding transaction, implying we've
@@ -5227,9 +4667,9 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                                                                        }
                                                                }
                                                        }
-                                                       self.funding_tx_confirmation_height = height;
-                                                       self.funding_tx_confirmed_in = Some(*block_hash);
-                                                       self.short_channel_id = match scid_from_parts(height as u64, index_in_block as u64, txo_idx as u64) {
+                                                       self.context.funding_tx_confirmation_height = height;
+                                                       self.context.funding_tx_confirmed_in = Some(*block_hash);
+                                                       self.context.short_channel_id = match scid_from_parts(height as u64, index_in_block as u64, txo_idx as u64) {
                                                                Ok(scid) => Some(scid),
                                                                Err(_) => panic!("Block was bogus - either height was > 16 million, had > 16 million transactions, or had > 65k outputs"),
                                                        }
@@ -5239,14 +4679,14 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                                        // send it immediately instead of waiting for a best_block_updated call (which
                                        // may have already happened for this block).
                                        if let Some(channel_ready) = self.check_get_channel_ready(height) {
-                                               log_info!(logger, "Sending a channel_ready to our peer for channel {}", log_bytes!(self.channel_id));
+                                               log_info!(logger, "Sending a channel_ready to our peer for channel {}", log_bytes!(self.context.channel_id));
                                                let announcement_sigs = self.get_announcement_sigs(node_signer, genesis_block_hash, user_config, height, logger);
                                                return Ok((Some(channel_ready), announcement_sigs));
                                        }
                                }
                                for inp in tx.input.iter() {
                                        if inp.previous_output == funding_txo.into_bitcoin_outpoint() {
-                                               log_info!(logger, "Detected channel-closing tx {} spending {}:{}, closing channel {}", tx.txid(), inp.previous_output.txid, inp.previous_output.vout, log_bytes!(self.channel_id()));
+                                               log_info!(logger, "Detected channel-closing tx {} spending {}:{}, closing channel {}", tx.txid(), inp.previous_output.txid, inp.previous_output.vout, log_bytes!(self.context.channel_id()));
                                                return Err(ClosureReason::CommitmentTxConfirmed);
                                        }
                                }
@@ -5290,7 +4730,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                // forward an HTLC when our counterparty should almost certainly just fail it for expiring
                // ~now.
                let unforwarded_htlc_cltv_limit = height + LATENCY_GRACE_PERIOD_BLOCKS;
-               self.holding_cell_htlc_updates.retain(|htlc_update| {
+               self.context.holding_cell_htlc_updates.retain(|htlc_update| {
                        match htlc_update {
                                &HTLCUpdateAwaitingACK::AddHTLC { ref payment_hash, ref source, ref cltv_expiry, .. } => {
                                        if *cltv_expiry <= unforwarded_htlc_cltv_limit {
@@ -5302,21 +4742,21 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                        }
                });
 
-               self.update_time_counter = cmp::max(self.update_time_counter, highest_header_time);
+               self.context.update_time_counter = cmp::max(self.context.update_time_counter, highest_header_time);
 
                if let Some(channel_ready) = self.check_get_channel_ready(height) {
                        let announcement_sigs = if let Some((genesis_block_hash, node_signer, user_config)) = genesis_node_signer {
                                self.get_announcement_sigs(node_signer, genesis_block_hash, user_config, height, logger)
                        } else { None };
-                       log_info!(logger, "Sending a channel_ready to our peer for channel {}", log_bytes!(self.channel_id));
+                       log_info!(logger, "Sending a channel_ready to our peer for channel {}", log_bytes!(self.context.channel_id));
                        return Ok((Some(channel_ready), timed_out_htlcs, announcement_sigs));
                }
 
-               let non_shutdown_state = self.channel_state & (!MULTI_STATE_FLAGS);
+               let non_shutdown_state = self.context.channel_state & (!MULTI_STATE_FLAGS);
                if non_shutdown_state >= ChannelState::ChannelReady as u32 ||
                   (non_shutdown_state & ChannelState::OurChannelReady as u32) == ChannelState::OurChannelReady as u32 {
-                       let mut funding_tx_confirmations = height as i64 - self.funding_tx_confirmation_height as i64 + 1;
-                       if self.funding_tx_confirmation_height == 0 {
+                       let mut funding_tx_confirmations = height as i64 - self.context.funding_tx_confirmation_height as i64 + 1;
+                       if self.context.funding_tx_confirmation_height == 0 {
                                // Note that check_get_channel_ready may reset funding_tx_confirmation_height to
                                // zero if it has been reorged out, however in either case, our state flags
                                // indicate we've already sent a channel_ready
@@ -5332,14 +4772,14 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                        // 0-conf channel, but not doing so may lead to the
                        // `ChannelManager::short_to_chan_info` map  being inconsistent, so we currently have
                        // to.
-                       if funding_tx_confirmations == 0 && self.funding_tx_confirmed_in.is_some() {
+                       if funding_tx_confirmations == 0 && self.context.funding_tx_confirmed_in.is_some() {
                                let err_reason = format!("Funding transaction was un-confirmed. Locked at {} confs, now have {} confs.",
-                                       self.minimum_depth.unwrap(), funding_tx_confirmations);
+                                       self.context.minimum_depth.unwrap(), funding_tx_confirmations);
                                return Err(ClosureReason::ProcessingError { err: err_reason });
                        }
-               } else if !self.is_outbound() && self.funding_tx_confirmed_in.is_none() &&
-                               height >= self.channel_creation_height + FUNDING_CONF_DEADLINE_BLOCKS {
-                       log_info!(logger, "Closing channel {} due to funding timeout", log_bytes!(self.channel_id));
+               } else if !self.context.is_outbound() && self.context.funding_tx_confirmed_in.is_none() &&
+                               height >= self.context.channel_creation_height + FUNDING_CONF_DEADLINE_BLOCKS {
+                       log_info!(logger, "Closing channel {} due to funding timeout", log_bytes!(self.context.channel_id));
                        // If funding_tx_confirmed_in is unset, the channel must not be active
                        assert!(non_shutdown_state <= ChannelState::ChannelReady as u32);
                        assert_eq!(non_shutdown_state & ChannelState::OurChannelReady as u32, 0);
@@ -5356,14 +4796,14 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
        /// force-close the channel, but may also indicate a harmless reorganization of a block or two
        /// before the channel has reached channel_ready and we can just wait for more blocks.
        pub fn funding_transaction_unconfirmed<L: Deref>(&mut self, logger: &L) -> Result<(), ClosureReason> where L::Target: Logger {
-               if self.funding_tx_confirmation_height != 0 {
+               if self.context.funding_tx_confirmation_height != 0 {
                        // We handle the funding disconnection by calling best_block_updated with a height one
                        // below where our funding was connected, implying a reorg back to conf_height - 1.
-                       let reorg_height = self.funding_tx_confirmation_height - 1;
+                       let reorg_height = self.context.funding_tx_confirmation_height - 1;
                        // We use the time field to bump the current time we set on channel updates if its
                        // larger. If we don't know that time has moved forward, we can just set it to the last
                        // time we saw and it will be ignored.
-                       let best_time = self.update_time_counter;
+                       let best_time = self.context.update_time_counter;
                        match self.do_best_block_updated(reorg_height, best_time, None::<(BlockHash, &&NodeSigner, &UserConfig)>, logger) {
                                Ok((channel_ready, timed_out_htlcs, announcement_sigs)) => {
                                        assert!(channel_ready.is_none(), "We can't generate a funding with 0 confirmations?");
@@ -5382,217 +4822,38 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
        // 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: BlockHash) -> msgs::OpenChannel {
-               if !self.is_outbound() {
-                       panic!("Tried to open a channel for an inbound channel?");
+       /// Gets an UnsignedChannelAnnouncement for this channel. The channel must be publicly
+       /// announceable and available for use (have exchanged ChannelReady messages in both
+       /// directions). Should be used for both broadcasted announcements 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).
+       ///
+       /// This will only return ChannelError::Ignore upon failure.
+       fn get_channel_announcement<NS: Deref>(
+               &self, node_signer: &NS, chain_hash: BlockHash, user_config: &UserConfig,
+       ) -> Result<msgs::UnsignedChannelAnnouncement, ChannelError> where NS::Target: NodeSigner {
+               if !self.context.config.announced_channel {
+                       return Err(ChannelError::Ignore("Channel is not available for public announcements".to_owned()));
                }
-               if self.channel_state != ChannelState::OurInitSent as u32 {
-                       panic!("Cannot generate an open_channel after we've moved forward");
-               }
-
-               if self.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
-                       panic!("Tried to send an open_channel for a channel that has already advanced");
-               }
-
-               let first_per_commitment_point = self.holder_signer.get_per_commitment_point(self.cur_holder_commitment_transaction_number, &self.secp_ctx);
-               let keys = self.get_holder_pubkeys();
-
-               msgs::OpenChannel {
-                       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.holder_dust_limit_satoshis,
-                       max_htlc_value_in_flight_msat: self.holder_max_htlc_value_in_flight_msat,
-                       channel_reserve_satoshis: self.holder_selected_channel_reserve_satoshis,
-                       htlc_minimum_msat: self.holder_htlc_minimum_msat,
-                       feerate_per_kw: self.feerate_per_kw as u32,
-                       to_self_delay: self.get_holder_selected_contest_delay(),
-                       max_accepted_htlcs: self.holder_max_accepted_htlcs,
-                       funding_pubkey: keys.funding_pubkey,
-                       revocation_basepoint: keys.revocation_basepoint,
-                       payment_point: keys.payment_point,
-                       delayed_payment_basepoint: keys.delayed_payment_basepoint,
-                       htlc_basepoint: keys.htlc_basepoint,
-                       first_per_commitment_point,
-                       channel_flags: if self.config.announced_channel {1} else {0},
-                       shutdown_scriptpubkey: Some(match &self.shutdown_scriptpubkey {
-                               Some(script) => script.clone().into_inner(),
-                               None => Builder::new().into_script(),
-                       }),
-                       channel_type: Some(self.channel_type.clone()),
-               }
-       }
-
-       pub fn inbound_is_awaiting_accept(&self) -> bool {
-               self.inbound_awaiting_accept
-       }
-
-       /// Sets this channel to accepting 0conf, must be done before `get_accept_channel`
-       pub fn set_0conf(&mut self) {
-               assert!(self.inbound_awaiting_accept);
-               self.minimum_depth = Some(0);
-       }
-
-       /// Marks an inbound channel as accepted and generates a [`msgs::AcceptChannel`] message which
-       /// should be sent back to the counterparty node.
-       ///
-       /// [`msgs::AcceptChannel`]: crate::ln::msgs::AcceptChannel
-       pub fn accept_inbound_channel(&mut self, user_id: u128) -> msgs::AcceptChannel {
-               if self.is_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_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
-                       panic!("Tried to send an accept_channel for a channel that has already advanced");
-               }
-               if !self.inbound_awaiting_accept {
-                       panic!("The inbound channel has already been accepted");
-               }
-
-               self.user_id = user_id;
-               self.inbound_awaiting_accept = false;
-
-               self.generate_accept_channel_message()
-       }
-
-       /// This function is used to explicitly generate a [`msgs::AcceptChannel`] message for an
-       /// inbound channel. If the intention is to accept an inbound channel, use
-       /// [`Channel::accept_inbound_channel`] instead.
-       ///
-       /// [`msgs::AcceptChannel`]: crate::ln::msgs::AcceptChannel
-       fn generate_accept_channel_message(&self) -> msgs::AcceptChannel {
-               let first_per_commitment_point = self.holder_signer.get_per_commitment_point(self.cur_holder_commitment_transaction_number, &self.secp_ctx);
-               let keys = self.get_holder_pubkeys();
-
-               msgs::AcceptChannel {
-                       temporary_channel_id: self.channel_id,
-                       dust_limit_satoshis: self.holder_dust_limit_satoshis,
-                       max_htlc_value_in_flight_msat: self.holder_max_htlc_value_in_flight_msat,
-                       channel_reserve_satoshis: self.holder_selected_channel_reserve_satoshis,
-                       htlc_minimum_msat: self.holder_htlc_minimum_msat,
-                       minimum_depth: self.minimum_depth.unwrap(),
-                       to_self_delay: self.get_holder_selected_contest_delay(),
-                       max_accepted_htlcs: self.holder_max_accepted_htlcs,
-                       funding_pubkey: keys.funding_pubkey,
-                       revocation_basepoint: keys.revocation_basepoint,
-                       payment_point: keys.payment_point,
-                       delayed_payment_basepoint: keys.delayed_payment_basepoint,
-                       htlc_basepoint: keys.htlc_basepoint,
-                       first_per_commitment_point,
-                       shutdown_scriptpubkey: Some(match &self.shutdown_scriptpubkey {
-                               Some(script) => script.clone().into_inner(),
-                               None => Builder::new().into_script(),
-                       }),
-                       channel_type: Some(self.channel_type.clone()),
-                       #[cfg(taproot)]
-                       next_local_nonce: None,
-               }
-       }
-
-       /// Enables the possibility for tests to extract a [`msgs::AcceptChannel`] message for an
-       /// inbound channel without accepting it.
-       ///
-       /// [`msgs::AcceptChannel`]: crate::ln::msgs::AcceptChannel
-       #[cfg(test)]
-       pub fn get_accept_channel_message(&self) -> msgs::AcceptChannel {
-               self.generate_accept_channel_message()
-       }
-
-       /// If an Err is returned, it is a ChannelError::Close (for get_outbound_funding_created)
-       fn get_outbound_funding_created_signature<L: Deref>(&mut self, logger: &L) -> Result<Signature, ChannelError> where L::Target: Logger {
-               let counterparty_keys = self.build_remote_transaction_keys();
-               let counterparty_initial_commitment_tx = self.build_commitment_transaction(self.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, false, logger).tx;
-               Ok(self.holder_signer.sign_counterparty_commitment(&counterparty_initial_commitment_tx, Vec::new(), &self.secp_ctx)
-                               .map_err(|_| ChannelError::Close("Failed to get signatures for new commitment_signed".to_owned()))?.0)
-       }
-
-       /// 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<L: Deref>(&mut self, funding_transaction: Transaction, funding_txo: OutPoint, logger: &L) -> Result<msgs::FundingCreated, ChannelError> where L::Target: Logger {
-               if !self.is_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.commitment_secrets.get_min_seen_secret() != (1 << 48) ||
-                               self.cur_counterparty_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER ||
-                               self.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
-                       panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
-               }
-
-               self.channel_transaction_parameters.funding_outpoint = Some(funding_txo);
-               self.holder_signer.provide_channel_parameters(&self.channel_transaction_parameters);
-
-               let signature = match self.get_outbound_funding_created_signature(logger) {
-                       Ok(res) => res,
-                       Err(e) => {
-                               log_error!(logger, "Got bad signatures: {:?}!", e);
-                               self.channel_transaction_parameters.funding_outpoint = None;
-                               return Err(e);
-                       }
-               };
-
-               let temporary_channel_id = self.channel_id;
-
-               // Now that we're past error-generating stuff, update our local state:
-
-               self.channel_state = ChannelState::FundingCreated as u32;
-               self.channel_id = funding_txo.to_channel_id();
-               self.funding_transaction = Some(funding_transaction);
-
-               Ok(msgs::FundingCreated {
-                       temporary_channel_id,
-                       funding_txid: funding_txo.txid,
-                       funding_output_index: funding_txo.index,
-                       signature,
-                       #[cfg(taproot)]
-                       partial_signature_with_nonce: None,
-                       #[cfg(taproot)]
-                       next_local_nonce: None,
-               })
-       }
-
-       /// Gets an UnsignedChannelAnnouncement for this channel. The channel must be publicly
-       /// announceable and available for use (have exchanged ChannelReady messages in both
-       /// directions). Should be used for both broadcasted announcements 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).
-       ///
-       /// This will only return ChannelError::Ignore upon failure.
-       fn get_channel_announcement<NS: Deref>(
-               &self, node_signer: &NS, chain_hash: BlockHash, user_config: &UserConfig,
-       ) -> Result<msgs::UnsignedChannelAnnouncement, ChannelError> where NS::Target: NodeSigner {
-               if !self.config.announced_channel {
-                       return Err(ChannelError::Ignore("Channel is not available for public announcements".to_owned()));
-               }
-               if !self.is_usable() {
+               if !self.context.is_usable() {
                        return Err(ChannelError::Ignore("Cannot get a ChannelAnnouncement if the channel is not currently usable".to_owned()));
                }
 
                let node_id = NodeId::from_pubkey(&node_signer.get_node_id(Recipient::Node)
                        .map_err(|_| ChannelError::Ignore("Failed to retrieve own public key".to_owned()))?);
-               let counterparty_node_id = NodeId::from_pubkey(&self.get_counterparty_node_id());
+               let counterparty_node_id = NodeId::from_pubkey(&self.context.get_counterparty_node_id());
                let were_node_one = node_id.as_slice() < counterparty_node_id.as_slice();
 
                let msg = msgs::UnsignedChannelAnnouncement {
                        features: channelmanager::provided_channel_features(&user_config),
                        chain_hash,
-                       short_channel_id: self.get_short_channel_id().unwrap(),
+                       short_channel_id: self.context.get_short_channel_id().unwrap(),
                        node_id_1: if were_node_one { node_id } else { counterparty_node_id },
                        node_id_2: if were_node_one { counterparty_node_id } else { node_id },
-                       bitcoin_key_1: NodeId::from_pubkey(if were_node_one { &self.get_holder_pubkeys().funding_pubkey } else { self.counterparty_funding_pubkey() }),
-                       bitcoin_key_2: NodeId::from_pubkey(if were_node_one { self.counterparty_funding_pubkey() } else { &self.get_holder_pubkeys().funding_pubkey }),
+                       bitcoin_key_1: NodeId::from_pubkey(if were_node_one { &self.context.get_holder_pubkeys().funding_pubkey } else { self.context.counterparty_funding_pubkey() }),
+                       bitcoin_key_2: NodeId::from_pubkey(if were_node_one { self.context.counterparty_funding_pubkey() } else { &self.context.get_holder_pubkeys().funding_pubkey }),
                        excess_data: Vec::new(),
                };
 
@@ -5607,24 +4868,24 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                NS::Target: NodeSigner,
                L::Target: Logger
        {
-               if self.funding_tx_confirmation_height == 0 || self.funding_tx_confirmation_height + 5 > best_block_height {
+               if self.context.funding_tx_confirmation_height == 0 || self.context.funding_tx_confirmation_height + 5 > best_block_height {
                        return None;
                }
 
-               if !self.is_usable() {
+               if !self.context.is_usable() {
                        return None;
                }
 
-               if self.channel_state & ChannelState::PeerDisconnected as u32 != 0 {
+               if self.context.channel_state & ChannelState::PeerDisconnected as u32 != 0 {
                        log_trace!(logger, "Cannot create an announcement_signatures as our peer is disconnected");
                        return None;
                }
 
-               if self.announcement_sigs_state != AnnouncementSigsState::NotSent {
+               if self.context.announcement_sigs_state != AnnouncementSigsState::NotSent {
                        return None;
                }
 
-               log_trace!(logger, "Creating an announcement_signatures message for channel {}", log_bytes!(self.channel_id()));
+               log_trace!(logger, "Creating an announcement_signatures message for channel {}", log_bytes!(self.context.channel_id()));
                let announcement = match self.get_channel_announcement(node_signer, genesis_block_hash, user_config) {
                        Ok(a) => a,
                        Err(e) => {
@@ -5639,18 +4900,18 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                        },
                        Ok(v) => v
                };
-               let our_bitcoin_sig = match self.holder_signer.sign_channel_announcement_with_funding_key(&announcement, &self.secp_ctx) {
+               let our_bitcoin_sig = match self.context.holder_signer.sign_channel_announcement_with_funding_key(&announcement, &self.context.secp_ctx) {
                        Err(_) => {
                                log_error!(logger, "Signer rejected channel_announcement signing. Channel will not be announced!");
                                return None;
                        },
                        Ok(v) => v
                };
-               self.announcement_sigs_state = AnnouncementSigsState::MessageSent;
+               self.context.announcement_sigs_state = AnnouncementSigsState::MessageSent;
 
                Some(msgs::AnnouncementSignatures {
-                       channel_id: self.channel_id(),
-                       short_channel_id: self.get_short_channel_id().unwrap(),
+                       channel_id: self.context.channel_id(),
+                       short_channel_id: self.context.get_short_channel_id().unwrap(),
                        node_signature: our_node_sig,
                        bitcoin_signature: our_bitcoin_sig,
                })
@@ -5661,14 +4922,14 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
        fn sign_channel_announcement<NS: Deref>(
                &self, node_signer: &NS, announcement: msgs::UnsignedChannelAnnouncement
        ) -> Result<msgs::ChannelAnnouncement, ChannelError> where NS::Target: NodeSigner {
-               if let Some((their_node_sig, their_bitcoin_sig)) = self.announcement_sigs {
+               if let Some((their_node_sig, their_bitcoin_sig)) = self.context.announcement_sigs {
                        let our_node_key = NodeId::from_pubkey(&node_signer.get_node_id(Recipient::Node)
                                .map_err(|_| ChannelError::Ignore("Signer failed to retrieve own public key".to_owned()))?);
                        let were_node_one = announcement.node_id_1 == our_node_key;
 
                        let our_node_sig = node_signer.sign_gossip_message(msgs::UnsignedGossipMessage::ChannelAnnouncement(&announcement))
                                .map_err(|_| ChannelError::Ignore("Failed to generate node signature for channel_announcement".to_owned()))?;
-                       let our_bitcoin_sig = self.holder_signer.sign_channel_announcement_with_funding_key(&announcement, &self.secp_ctx)
+                       let our_bitcoin_sig = self.context.holder_signer.sign_channel_announcement_with_funding_key(&announcement, &self.context.secp_ctx)
                                .map_err(|_| ChannelError::Ignore("Signer rejected channel_announcement".to_owned()))?;
                        Ok(msgs::ChannelAnnouncement {
                                node_signature_1: if were_node_one { our_node_sig } else { their_node_sig },
@@ -5693,19 +4954,19 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
 
                let msghash = hash_to_message!(&Sha256d::hash(&announcement.encode()[..])[..]);
 
-               if self.secp_ctx.verify_ecdsa(&msghash, &msg.node_signature, &self.get_counterparty_node_id()).is_err() {
+               if self.context.secp_ctx.verify_ecdsa(&msghash, &msg.node_signature, &self.context.get_counterparty_node_id()).is_err() {
                        return Err(ChannelError::Close(format!(
                                "Bad announcement_signatures. Failed to verify node_signature. UnsignedChannelAnnouncement used for verification is {:?}. their_node_key is {:?}",
-                                &announcement, self.get_counterparty_node_id())));
+                                &announcement, self.context.get_counterparty_node_id())));
                }
-               if self.secp_ctx.verify_ecdsa(&msghash, &msg.bitcoin_signature, self.counterparty_funding_pubkey()).is_err() {
+               if self.context.secp_ctx.verify_ecdsa(&msghash, &msg.bitcoin_signature, self.context.counterparty_funding_pubkey()).is_err() {
                        return Err(ChannelError::Close(format!(
                                "Bad announcement_signatures. Failed to verify bitcoin_signature. UnsignedChannelAnnouncement used for verification is {:?}. their_bitcoin_key is ({:?})",
-                               &announcement, self.counterparty_funding_pubkey())));
+                               &announcement, self.context.counterparty_funding_pubkey())));
                }
 
-               self.announcement_sigs = Some((msg.node_signature, msg.bitcoin_signature));
-               if self.funding_tx_confirmation_height == 0 || self.funding_tx_confirmation_height + 5 > best_block_height {
+               self.context.announcement_sigs = Some((msg.node_signature, msg.bitcoin_signature));
+               if self.context.funding_tx_confirmation_height == 0 || self.context.funding_tx_confirmation_height + 5 > best_block_height {
                        return Err(ChannelError::Ignore(
                                "Got announcement_signatures prior to the required six confirmations - we may not have received a block yet that our peer has".to_owned()));
                }
@@ -5718,7 +4979,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
        pub fn get_signed_channel_announcement<NS: Deref>(
                &self, node_signer: &NS, chain_hash: BlockHash, best_block_height: u32, user_config: &UserConfig
        ) -> Option<msgs::ChannelAnnouncement> where NS::Target: NodeSigner {
-               if self.funding_tx_confirmation_height == 0 || self.funding_tx_confirmation_height + 5 > best_block_height {
+               if self.context.funding_tx_confirmation_height == 0 || self.context.funding_tx_confirmation_height + 5 > best_block_height {
                        return None;
                }
                let announcement = match self.get_channel_announcement(node_signer, chain_hash, user_config) {
@@ -5733,9 +4994,9 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
 
        /// 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<L: Deref>(&self, logger: &L) -> msgs::ChannelReestablish where L::Target: Logger {
-               assert_eq!(self.channel_state & ChannelState::PeerDisconnected as u32, ChannelState::PeerDisconnected as u32);
-               assert_ne!(self.cur_counterparty_commitment_transaction_number, INITIAL_COMMITMENT_NUMBER);
+       pub fn get_channel_reestablish<L: Deref>(&mut self, logger: &L) -> msgs::ChannelReestablish where L::Target: Logger {
+               assert_eq!(self.context.channel_state & ChannelState::PeerDisconnected as u32, ChannelState::PeerDisconnected as u32);
+               assert_ne!(self.context.cur_counterparty_commitment_transaction_number, INITIAL_COMMITMENT_NUMBER);
                // Prior to static_remotekey, my_current_per_commitment_point was critical to claiming
                // current to_remote balances. However, it no longer has any use, and thus is now simply
                // set to a dummy (but valid, as required by the spec) public key.
@@ -5744,16 +5005,17 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                // valid, and valid in fuzzing mode's arbitrary validity criteria:
                let mut pk = [2; 33]; pk[1] = 0xff;
                let dummy_pubkey = PublicKey::from_slice(&pk).unwrap();
-               let remote_last_secret = if self.cur_counterparty_commitment_transaction_number + 1 < INITIAL_COMMITMENT_NUMBER {
-                       let remote_last_secret = self.commitment_secrets.get_secret(self.cur_counterparty_commitment_transaction_number + 2).unwrap();
-                       log_trace!(logger, "Enough info to generate a Data Loss Protect with per_commitment_secret {} for channel {}", log_bytes!(remote_last_secret), log_bytes!(self.channel_id()));
+               let remote_last_secret = if self.context.cur_counterparty_commitment_transaction_number + 1 < INITIAL_COMMITMENT_NUMBER {
+                       let remote_last_secret = self.context.commitment_secrets.get_secret(self.context.cur_counterparty_commitment_transaction_number + 2).unwrap();
+                       log_trace!(logger, "Enough info to generate a Data Loss Protect with per_commitment_secret {} for channel {}", log_bytes!(remote_last_secret), log_bytes!(self.context.channel_id()));
                        remote_last_secret
                } else {
-                       log_info!(logger, "Sending a data_loss_protect with no previous remote per_commitment_secret for channel {}", log_bytes!(self.channel_id()));
+                       log_info!(logger, "Sending a data_loss_protect with no previous remote per_commitment_secret for channel {}", log_bytes!(self.context.channel_id()));
                        [0;32]
                };
+               self.mark_awaiting_response();
                msgs::ChannelReestablish {
-                       channel_id: self.channel_id(),
+                       channel_id: self.context.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
@@ -5763,7 +5025,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
 
                        // 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_holder_commitment_transaction_number,
+                       next_local_commitment_number: INITIAL_COMMITMENT_NUMBER - self.context.cur_holder_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
@@ -5771,7 +5033,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                        // cur_counterparty_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_counterparty_commitment_transaction_number - 1,
+                       next_remote_commitment_number: INITIAL_COMMITMENT_NUMBER - self.context.cur_counterparty_commitment_transaction_number - 1,
                        your_last_per_commitment_secret: remote_last_secret,
                        my_current_per_commitment_point: dummy_pubkey,
                        // TODO(dual_funding): If we've sent `commtiment_signed` for an interactive transaction
@@ -5789,11 +5051,16 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
        /// commitment update.
        ///
        /// `Err`s will only be [`ChannelError::Ignore`].
-       pub fn queue_add_htlc<L: Deref>(&mut self, amount_msat: u64, payment_hash: PaymentHash, cltv_expiry: u32, source: HTLCSource,
-               onion_routing_packet: msgs::OnionPacket, logger: &L)
-       -> Result<(), ChannelError> where L::Target: Logger {
+       pub fn queue_add_htlc<F: Deref, L: Deref>(
+               &mut self, amount_msat: u64, payment_hash: PaymentHash, cltv_expiry: u32, source: HTLCSource,
+               onion_routing_packet: msgs::OnionPacket, skimmed_fee_msat: Option<u64>,
+               fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L
+       ) -> Result<(), ChannelError>
+       where F::Target: FeeEstimator, L::Target: Logger
+       {
                self
-                       .send_htlc(amount_msat, payment_hash, cltv_expiry, source, onion_routing_packet, true, logger)
+                       .send_htlc(amount_msat, payment_hash, cltv_expiry, source, onion_routing_packet, true,
+                               skimmed_fee_msat, fee_estimator, logger)
                        .map(|msg_opt| assert!(msg_opt.is_none(), "We forced holding cell?"))
                        .map_err(|err| {
                                if let ChannelError::Ignore(_) = err { /* fine */ }
@@ -5818,467 +5085,1412 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
        /// on this [`Channel`] if `force_holding_cell` is false.
        ///
        /// `Err`s will only be [`ChannelError::Ignore`].
-       fn send_htlc<L: Deref>(&mut self, amount_msat: u64, payment_hash: PaymentHash, cltv_expiry: u32, source: HTLCSource,
-               onion_routing_packet: msgs::OnionPacket, mut force_holding_cell: bool, logger: &L)
-       -> Result<Option<msgs::UpdateAddHTLC>, ChannelError> where L::Target: Logger {
-               if (self.channel_state & (ChannelState::ChannelReady as u32 | BOTH_SIDES_SHUTDOWN_MASK)) != (ChannelState::ChannelReady as u32) {
+       fn send_htlc<F: Deref, L: Deref>(
+               &mut self, amount_msat: u64, payment_hash: PaymentHash, cltv_expiry: u32, source: HTLCSource,
+               onion_routing_packet: msgs::OnionPacket, mut force_holding_cell: bool,
+               skimmed_fee_msat: Option<u64>, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L
+       ) -> Result<Option<msgs::UpdateAddHTLC>, ChannelError>
+       where F::Target: FeeEstimator, L::Target: Logger
+       {
+               if (self.context.channel_state & (ChannelState::ChannelReady as u32 | BOTH_SIDES_SHUTDOWN_MASK)) != (ChannelState::ChannelReady as u32) {
                        return Err(ChannelError::Ignore("Cannot send HTLC until channel is fully established and we haven't started shutting down".to_owned()));
                }
-               let channel_total_msat = self.channel_value_satoshis * 1000;
+               let channel_total_msat = self.context.channel_value_satoshis * 1000;
                if amount_msat > channel_total_msat {
                        return Err(ChannelError::Ignore(format!("Cannot send amount {}, because it is more than the total value of the channel {}", amount_msat, channel_total_msat)));
                }
-
-               if amount_msat == 0 {
-                       return Err(ChannelError::Ignore("Cannot send 0-msat HTLC".to_owned()));
+
+               if amount_msat == 0 {
+                       return Err(ChannelError::Ignore("Cannot send 0-msat HTLC".to_owned()));
+               }
+
+               let available_balances = self.context.get_available_balances(fee_estimator);
+               if amount_msat < available_balances.next_outbound_htlc_minimum_msat {
+                       return Err(ChannelError::Ignore(format!("Cannot send less than our next-HTLC minimum - {} msat",
+                               available_balances.next_outbound_htlc_minimum_msat)));
+               }
+
+               if amount_msat > available_balances.next_outbound_htlc_limit_msat {
+                       return Err(ChannelError::Ignore(format!("Cannot send more than our next-HTLC maximum - {} msat",
+                               available_balances.next_outbound_htlc_limit_msat)));
+               }
+
+               if (self.context.channel_state & (ChannelState::PeerDisconnected 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 from channel counterparty".to_owned()));
+               }
+
+               let need_holding_cell = (self.context.channel_state & (ChannelState::AwaitingRemoteRevoke as u32 | ChannelState::MonitorUpdateInProgress as u32)) != 0;
+               log_debug!(logger, "Pushing new outbound HTLC for {} msat {}", amount_msat,
+                       if force_holding_cell { "into holding cell" }
+                       else if need_holding_cell { "into holding cell as we're awaiting an RAA or monitor" }
+                       else { "to peer" });
+
+               if need_holding_cell {
+                       force_holding_cell = true;
+               }
+
+               // Now update local state:
+               if force_holding_cell {
+                       self.context.holding_cell_htlc_updates.push(HTLCUpdateAwaitingACK::AddHTLC {
+                               amount_msat,
+                               payment_hash,
+                               cltv_expiry,
+                               source,
+                               onion_routing_packet,
+                               skimmed_fee_msat,
+                       });
+                       return Ok(None);
+               }
+
+               self.context.pending_outbound_htlcs.push(OutboundHTLCOutput {
+                       htlc_id: self.context.next_holder_htlc_id,
+                       amount_msat,
+                       payment_hash: payment_hash.clone(),
+                       cltv_expiry,
+                       state: OutboundHTLCState::LocalAnnounced(Box::new(onion_routing_packet.clone())),
+                       source,
+                       skimmed_fee_msat,
+               });
+
+               let res = msgs::UpdateAddHTLC {
+                       channel_id: self.context.channel_id,
+                       htlc_id: self.context.next_holder_htlc_id,
+                       amount_msat,
+                       payment_hash,
+                       cltv_expiry,
+                       onion_routing_packet,
+                       skimmed_fee_msat,
+               };
+               self.context.next_holder_htlc_id += 1;
+
+               Ok(Some(res))
+       }
+
+       fn build_commitment_no_status_check<L: Deref>(&mut self, logger: &L) -> ChannelMonitorUpdate where L::Target: Logger {
+               log_trace!(logger, "Updating HTLC state for a newly-sent commitment_signed...");
+               // 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.context.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 {
+                               log_trace!(logger, " ...promoting inbound AwaitingRemoteRevokeToAnnounce {} to AwaitingAnnouncedRemoteRevoke", log_bytes!(htlc.payment_hash.0));
+                               htlc.state = state;
+                       }
+               }
+               for htlc in self.context.pending_outbound_htlcs.iter_mut() {
+                       if let &mut OutboundHTLCState::AwaitingRemoteRevokeToRemove(ref mut outcome) = &mut htlc.state {
+                               log_trace!(logger, " ...promoting outbound AwaitingRemoteRevokeToRemove {} to AwaitingRemovedRemoteRevoke", log_bytes!(htlc.payment_hash.0));
+                               // Grab the preimage, if it exists, instead of cloning
+                               let mut reason = OutboundHTLCOutcome::Success(None);
+                               mem::swap(outcome, &mut reason);
+                               htlc.state = OutboundHTLCState::AwaitingRemovedRemoteRevoke(reason);
+                       }
+               }
+               if let Some((feerate, update_state)) = self.context.pending_update_fee {
+                       if update_state == FeeUpdateState::AwaitingRemoteRevokeToAnnounce {
+                               debug_assert!(!self.context.is_outbound());
+                               log_trace!(logger, " ...promoting inbound AwaitingRemoteRevokeToAnnounce fee update {} to Committed", feerate);
+                               self.context.feerate_per_kw = feerate;
+                               self.context.pending_update_fee = None;
+                       }
+               }
+               self.context.resend_order = RAACommitmentOrder::RevokeAndACKFirst;
+
+               let (counterparty_commitment_txid, mut htlcs_ref) = self.build_commitment_no_state_update(logger);
+               let htlcs: Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)> =
+                       htlcs_ref.drain(..).map(|(htlc, htlc_source)| (htlc, htlc_source.map(|source_ref| Box::new(source_ref.clone())))).collect();
+
+               if self.context.announcement_sigs_state == AnnouncementSigsState::MessageSent {
+                       self.context.announcement_sigs_state = AnnouncementSigsState::Committed;
+               }
+
+               self.context.latest_monitor_update_id += 1;
+               let monitor_update = ChannelMonitorUpdate {
+                       update_id: self.context.latest_monitor_update_id,
+                       updates: vec![ChannelMonitorUpdateStep::LatestCounterpartyCommitmentTXInfo {
+                               commitment_txid: counterparty_commitment_txid,
+                               htlc_outputs: htlcs.clone(),
+                               commitment_number: self.context.cur_counterparty_commitment_transaction_number,
+                               their_per_commitment_point: self.context.counterparty_cur_commitment_point.unwrap()
+                       }]
+               };
+               self.context.channel_state |= ChannelState::AwaitingRemoteRevoke as u32;
+               monitor_update
+       }
+
+       fn build_commitment_no_state_update<L: Deref>(&self, logger: &L) -> (Txid, Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)>) where L::Target: Logger {
+               let counterparty_keys = self.context.build_remote_transaction_keys();
+               let commitment_stats = self.context.build_commitment_transaction(self.context.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, true, logger);
+               let counterparty_commitment_txid = commitment_stats.tx.trust().txid();
+
+               #[cfg(any(test, fuzzing))]
+               {
+                       if !self.context.is_outbound() {
+                               let projected_commit_tx_info = self.context.next_remote_commitment_tx_fee_info_cached.lock().unwrap().take();
+                               *self.context.next_local_commitment_tx_fee_info_cached.lock().unwrap() = None;
+                               if let Some(info) = projected_commit_tx_info {
+                                       let total_pending_htlcs = self.context.pending_inbound_htlcs.len() + self.context.pending_outbound_htlcs.len();
+                                       if info.total_pending_htlcs == total_pending_htlcs
+                                               && info.next_holder_htlc_id == self.context.next_holder_htlc_id
+                                               && info.next_counterparty_htlc_id == self.context.next_counterparty_htlc_id
+                                               && info.feerate == self.context.feerate_per_kw {
+                                                       let actual_fee = commit_tx_fee_msat(self.context.feerate_per_kw, commitment_stats.num_nondust_htlcs, self.context.get_channel_type());
+                                                       assert_eq!(actual_fee, info.fee);
+                                               }
+                               }
+                       }
+               }
+
+               (counterparty_commitment_txid, commitment_stats.htlcs_included)
+       }
+
+       /// Only fails in case of signer rejection. Used for channel_reestablish commitment_signed
+       /// generation when we shouldn't change HTLC/channel state.
+       fn send_commitment_no_state_update<L: Deref>(&self, logger: &L) -> Result<(msgs::CommitmentSigned, (Txid, Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)>)), ChannelError> where L::Target: Logger {
+               // Get the fee tests from `build_commitment_no_state_update`
+               #[cfg(any(test, fuzzing))]
+               self.build_commitment_no_state_update(logger);
+
+               let counterparty_keys = self.context.build_remote_transaction_keys();
+               let commitment_stats = self.context.build_commitment_transaction(self.context.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, true, logger);
+               let counterparty_commitment_txid = commitment_stats.tx.trust().txid();
+               let (signature, htlc_signatures);
+
+               {
+                       let mut htlcs = Vec::with_capacity(commitment_stats.htlcs_included.len());
+                       for &(ref htlc, _) in commitment_stats.htlcs_included.iter() {
+                               htlcs.push(htlc);
+                       }
+
+                       let res = self.context.holder_signer.sign_counterparty_commitment(&commitment_stats.tx, commitment_stats.preimages, &self.context.secp_ctx)
+                               .map_err(|_| ChannelError::Close("Failed to get signatures for new commitment_signed".to_owned()))?;
+                       signature = res.0;
+                       htlc_signatures = res.1;
+
+                       log_trace!(logger, "Signed remote commitment tx {} (txid {}) with redeemscript {} -> {} in channel {}",
+                               encode::serialize_hex(&commitment_stats.tx.trust().built_transaction().transaction),
+                               &counterparty_commitment_txid, encode::serialize_hex(&self.context.get_funding_redeemscript()),
+                               log_bytes!(signature.serialize_compact()[..]), log_bytes!(self.context.channel_id()));
+
+                       for (ref htlc_sig, ref htlc) in htlc_signatures.iter().zip(htlcs) {
+                               log_trace!(logger, "Signed remote HTLC tx {} with redeemscript {} with pubkey {} -> {} in channel {}",
+                                       encode::serialize_hex(&chan_utils::build_htlc_transaction(&counterparty_commitment_txid, commitment_stats.feerate_per_kw, self.context.get_holder_selected_contest_delay(), htlc, &self.context.channel_type, &counterparty_keys.broadcaster_delayed_payment_key, &counterparty_keys.revocation_key)),
+                                       encode::serialize_hex(&chan_utils::get_htlc_redeemscript(&htlc, &self.context.channel_type, &counterparty_keys)),
+                                       log_bytes!(counterparty_keys.broadcaster_htlc_key.serialize()),
+                                       log_bytes!(htlc_sig.serialize_compact()[..]), log_bytes!(self.context.channel_id()));
+                       }
+               }
+
+               Ok((msgs::CommitmentSigned {
+                       channel_id: self.context.channel_id,
+                       signature,
+                       htlc_signatures,
+                       #[cfg(taproot)]
+                       partial_signature_with_nonce: None,
+               }, (counterparty_commitment_txid, commitment_stats.htlcs_included)))
+       }
+
+       /// Adds a pending outbound HTLC to this channel, and builds a new remote commitment
+       /// transaction and generates the corresponding [`ChannelMonitorUpdate`] in one go.
+       ///
+       /// Shorthand for calling [`Self::send_htlc`] followed by a commitment update, see docs on
+       /// [`Self::send_htlc`] and [`Self::build_commitment_no_state_update`] for more info.
+       pub fn send_htlc_and_commit<F: Deref, L: Deref>(
+               &mut self, amount_msat: u64, payment_hash: PaymentHash, cltv_expiry: u32,
+               source: HTLCSource, onion_routing_packet: msgs::OnionPacket, skimmed_fee_msat: Option<u64>,
+               fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L
+       ) -> Result<Option<ChannelMonitorUpdate>, ChannelError>
+       where F::Target: FeeEstimator, L::Target: Logger
+       {
+               let send_res = self.send_htlc(amount_msat, payment_hash, cltv_expiry, source,
+                       onion_routing_packet, false, skimmed_fee_msat, fee_estimator, logger);
+               if let Err(e) = &send_res { if let ChannelError::Ignore(_) = e {} else { debug_assert!(false, "Sending cannot trigger channel failure"); } }
+               match send_res? {
+                       Some(_) => {
+                               let monitor_update = self.build_commitment_no_status_check(logger);
+                               self.monitor_updating_paused(false, true, false, Vec::new(), Vec::new(), Vec::new());
+                               Ok(self.push_ret_blockable_mon_update(monitor_update))
+                       },
+                       None => Ok(None)
+               }
+       }
+
+       pub fn channel_update(&mut self, msg: &msgs::ChannelUpdate) -> Result<(), ChannelError> {
+               if msg.contents.htlc_minimum_msat >= self.context.channel_value_satoshis * 1000 {
+                       return Err(ChannelError::Close("Minimum htlc value is greater than channel value".to_string()));
+               }
+               self.context.counterparty_forwarding_info = Some(CounterpartyForwardingInfo {
+                       fee_base_msat: msg.contents.fee_base_msat,
+                       fee_proportional_millionths: msg.contents.fee_proportional_millionths,
+                       cltv_expiry_delta: msg.contents.cltv_expiry_delta
+               });
+
+               Ok(())
+       }
+
+       /// Begins the shutdown process, getting a message for the remote peer and returning all
+       /// holding cell HTLCs for payment failure.
+       ///
+       /// May jump to the channel being fully shutdown (see [`Self::is_shutdown`]) in which case no
+       /// [`ChannelMonitorUpdate`] will be returned).
+       pub fn get_shutdown<SP: Deref>(&mut self, signer_provider: &SP, their_features: &InitFeatures,
+               target_feerate_sats_per_kw: Option<u32>, override_shutdown_script: Option<ShutdownScript>)
+       -> Result<(msgs::Shutdown, Option<ChannelMonitorUpdate>, Vec<(HTLCSource, PaymentHash)>), APIError>
+       where SP::Target: SignerProvider {
+               for htlc in self.context.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".to_owned()});
+                       }
+               }
+               if self.context.channel_state & BOTH_SIDES_SHUTDOWN_MASK != 0 {
+                       if (self.context.channel_state & ChannelState::LocalShutdownSent as u32) == ChannelState::LocalShutdownSent as u32 {
+                               return Err(APIError::APIMisuseError{err: "Shutdown already in progress".to_owned()});
+                       }
+                       else if (self.context.channel_state & ChannelState::RemoteShutdownSent as u32) == ChannelState::RemoteShutdownSent as u32 {
+                               return Err(APIError::ChannelUnavailable{err: "Shutdown already in progress by remote".to_owned()});
+                       }
+               }
+               if self.context.shutdown_scriptpubkey.is_some() && override_shutdown_script.is_some() {
+                       return Err(APIError::APIMisuseError{err: "Cannot override shutdown script for a channel with one already set".to_owned()});
+               }
+               assert_eq!(self.context.channel_state & ChannelState::ShutdownComplete as u32, 0);
+               if self.context.channel_state & (ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress 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?".to_owned()});
+               }
+
+               // If we haven't funded the channel yet, we don't need to bother ensuring the shutdown
+               // script is set, we just force-close and call it a day.
+               let mut chan_closed = false;
+               if self.context.channel_state < ChannelState::FundingSent as u32 {
+                       chan_closed = true;
+               }
+
+               let update_shutdown_script = match self.context.shutdown_scriptpubkey {
+                       Some(_) => false,
+                       None if !chan_closed => {
+                               // use override shutdown script if provided
+                               let shutdown_scriptpubkey = match override_shutdown_script {
+                                       Some(script) => script,
+                                       None => {
+                                               // otherwise, use the shutdown scriptpubkey provided by the signer
+                                               match signer_provider.get_shutdown_scriptpubkey() {
+                                                       Ok(scriptpubkey) => scriptpubkey,
+                                                       Err(_) => return Err(APIError::ChannelUnavailable{err: "Failed to get shutdown scriptpubkey".to_owned()}),
+                                               }
+                                       },
+                               };
+                               if !shutdown_scriptpubkey.is_compatible(their_features) {
+                                       return Err(APIError::IncompatibleShutdownScript { script: shutdown_scriptpubkey.clone() });
+                               }
+                               self.context.shutdown_scriptpubkey = Some(shutdown_scriptpubkey);
+                               true
+                       },
+                       None => false,
+               };
+
+               // From here on out, we may not fail!
+               self.context.target_closing_feerate_sats_per_kw = target_feerate_sats_per_kw;
+               if self.context.channel_state < ChannelState::FundingSent as u32 {
+                       self.context.channel_state = ChannelState::ShutdownComplete as u32;
+               } else {
+                       self.context.channel_state |= ChannelState::LocalShutdownSent as u32;
+               }
+               self.context.update_time_counter += 1;
+
+               let monitor_update = if update_shutdown_script {
+                       self.context.latest_monitor_update_id += 1;
+                       let monitor_update = ChannelMonitorUpdate {
+                               update_id: self.context.latest_monitor_update_id,
+                               updates: vec![ChannelMonitorUpdateStep::ShutdownScript {
+                                       scriptpubkey: self.get_closing_scriptpubkey(),
+                               }],
+                       };
+                       self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
+                       self.push_ret_blockable_mon_update(monitor_update)
+               } else { None };
+               let shutdown = msgs::Shutdown {
+                       channel_id: self.context.channel_id,
+                       scriptpubkey: self.get_closing_scriptpubkey(),
+               };
+
+               // 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.context.holding_cell_update_fee = None;
+               let mut dropped_outbound_htlcs = Vec::with_capacity(self.context.holding_cell_htlc_updates.len());
+               self.context.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
+                       }
+               });
+
+               debug_assert!(!self.is_shutdown() || monitor_update.is_none(),
+                       "we can't both complete shutdown and return a monitor update");
+
+               Ok((shutdown, monitor_update, dropped_outbound_htlcs))
+       }
+
+       pub fn inflight_htlc_sources(&self) -> impl Iterator<Item=(&HTLCSource, &PaymentHash)> {
+               self.context.holding_cell_htlc_updates.iter()
+                       .flat_map(|htlc_update| {
+                               match htlc_update {
+                                       HTLCUpdateAwaitingACK::AddHTLC { source, payment_hash, .. }
+                                               => Some((source, payment_hash)),
+                                       _ => None,
+                               }
+                       })
+                       .chain(self.context.pending_outbound_htlcs.iter().map(|htlc| (&htlc.source, &htlc.payment_hash)))
+       }
+}
+
+/// A not-yet-funded outbound (from holder) channel using V1 channel establishment.
+pub(super) struct OutboundV1Channel<Signer: ChannelSigner> {
+       pub context: ChannelContext<Signer>,
+}
+
+impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
+       pub fn new<ES: Deref, SP: Deref, F: Deref>(
+               fee_estimator: &LowerBoundedFeeEstimator<F>, entropy_source: &ES, signer_provider: &SP, counterparty_node_id: PublicKey, their_features: &InitFeatures,
+               channel_value_satoshis: u64, push_msat: u64, user_id: u128, config: &UserConfig, current_chain_height: u32,
+               outbound_scid_alias: u64
+       ) -> Result<OutboundV1Channel<Signer>, APIError>
+       where ES::Target: EntropySource,
+             SP::Target: SignerProvider<Signer = Signer>,
+             F::Target: FeeEstimator,
+       {
+               let holder_selected_contest_delay = config.channel_handshake_config.our_to_self_delay;
+               let channel_keys_id = signer_provider.generate_channel_keys_id(false, channel_value_satoshis, user_id);
+               let holder_signer = signer_provider.derive_channel_signer(channel_value_satoshis, channel_keys_id);
+               let pubkeys = holder_signer.pubkeys().clone();
+
+               if !their_features.supports_wumbo() && channel_value_satoshis > MAX_FUNDING_SATOSHIS_NO_WUMBO {
+                       return Err(APIError::APIMisuseError{err: format!("funding_value must not exceed {}, it was {}", MAX_FUNDING_SATOSHIS_NO_WUMBO, channel_value_satoshis)});
+               }
+               if channel_value_satoshis >= TOTAL_BITCOIN_SUPPLY_SATOSHIS {
+                       return Err(APIError::APIMisuseError{err: format!("funding_value must be smaller than the total bitcoin supply, it was {}", channel_value_satoshis)});
+               }
+               let channel_value_msat = channel_value_satoshis * 1000;
+               if push_msat > channel_value_msat {
+                       return Err(APIError::APIMisuseError { err: format!("Push value ({}) was larger than channel_value ({})", push_msat, channel_value_msat) });
+               }
+               if holder_selected_contest_delay < BREAKDOWN_TIMEOUT {
+                       return Err(APIError::APIMisuseError {err: format!("Configured with an unreasonable our_to_self_delay ({}) putting user funds at risks", holder_selected_contest_delay)});
+               }
+               let holder_selected_channel_reserve_satoshis = get_holder_selected_channel_reserve_satoshis(channel_value_satoshis, config);
+               if holder_selected_channel_reserve_satoshis < MIN_CHAN_DUST_LIMIT_SATOSHIS {
+                       // Protocol level safety check in place, although it should never happen because
+                       // of `MIN_THEIR_CHAN_RESERVE_SATOSHIS`
+                       return Err(APIError::APIMisuseError { err: format!("Holder selected channel  reserve below implemention limit dust_limit_satoshis {}", holder_selected_channel_reserve_satoshis) });
+               }
+
+               let channel_type = Self::get_initial_channel_type(&config, their_features);
+               debug_assert!(channel_type.is_subset(&channelmanager::provided_channel_type_features(&config)));
+
+               let feerate = fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Normal);
+
+               let value_to_self_msat = channel_value_satoshis * 1000 - push_msat;
+               let commitment_tx_fee = commit_tx_fee_msat(feerate, MIN_AFFORDABLE_HTLC_COUNT, &channel_type);
+               if value_to_self_msat < commitment_tx_fee {
+                       return Err(APIError::APIMisuseError{ err: format!("Funding amount ({}) can't even pay fee for initial commitment transaction fee of {}.", value_to_self_msat / 1000, commitment_tx_fee / 1000) });
+               }
+
+               let mut secp_ctx = Secp256k1::new();
+               secp_ctx.seeded_randomize(&entropy_source.get_secure_random_bytes());
+
+               let shutdown_scriptpubkey = if config.channel_handshake_config.commit_upfront_shutdown_pubkey {
+                       match signer_provider.get_shutdown_scriptpubkey() {
+                               Ok(scriptpubkey) => Some(scriptpubkey),
+                               Err(_) => return Err(APIError::ChannelUnavailable { err: "Failed to get shutdown scriptpubkey".to_owned()}),
+                       }
+               } else { None };
+
+               if let Some(shutdown_scriptpubkey) = &shutdown_scriptpubkey {
+                       if !shutdown_scriptpubkey.is_compatible(&their_features) {
+                               return Err(APIError::IncompatibleShutdownScript { script: shutdown_scriptpubkey.clone() });
+                       }
+               }
+
+               let destination_script = match signer_provider.get_destination_script() {
+                       Ok(script) => script,
+                       Err(_) => return Err(APIError::ChannelUnavailable { err: "Failed to get destination script".to_owned()}),
+               };
+
+               let temporary_channel_id = entropy_source.get_secure_random_bytes();
+
+               Ok(Self {
+                       context: ChannelContext {
+                               user_id,
+
+                               config: LegacyChannelConfig {
+                                       options: config.channel_config.clone(),
+                                       announced_channel: config.channel_handshake_config.announced_channel,
+                                       commit_upfront_shutdown_pubkey: config.channel_handshake_config.commit_upfront_shutdown_pubkey,
+                               },
+
+                               prev_config: None,
+
+                               inbound_handshake_limits_override: Some(config.channel_handshake_limits.clone()),
+
+                               channel_id: temporary_channel_id,
+                               temporary_channel_id: Some(temporary_channel_id),
+                               channel_state: ChannelState::OurInitSent as u32,
+                               announcement_sigs_state: AnnouncementSigsState::NotSent,
+                               secp_ctx,
+                               channel_value_satoshis,
+
+                               latest_monitor_update_id: 0,
+
+                               holder_signer,
+                               shutdown_scriptpubkey,
+                               destination_script,
+
+                               cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
+                               cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
+                               value_to_self_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_holder_htlc_id: 0,
+                               next_counterparty_htlc_id: 0,
+                               update_time_counter: 1,
+
+                               resend_order: RAACommitmentOrder::CommitmentFirst,
+
+                               monitor_pending_channel_ready: false,
+                               monitor_pending_revoke_and_ack: false,
+                               monitor_pending_commitment_signed: false,
+                               monitor_pending_forwards: Vec::new(),
+                               monitor_pending_failures: Vec::new(),
+                               monitor_pending_finalized_fulfills: Vec::new(),
+
+                               #[cfg(debug_assertions)]
+                               holder_max_commitment_tx_output: Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
+                               #[cfg(debug_assertions)]
+                               counterparty_max_commitment_tx_output: Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
+
+                               last_sent_closing_fee: None,
+                               pending_counterparty_closing_signed: None,
+                               closing_fee_limits: None,
+                               target_closing_feerate_sats_per_kw: None,
+
+                               inbound_awaiting_accept: false,
+
+                               funding_tx_confirmed_in: None,
+                               funding_tx_confirmation_height: 0,
+                               short_channel_id: None,
+                               channel_creation_height: current_chain_height,
+
+                               feerate_per_kw: feerate,
+                               counterparty_dust_limit_satoshis: 0,
+                               holder_dust_limit_satoshis: MIN_CHAN_DUST_LIMIT_SATOSHIS,
+                               counterparty_max_htlc_value_in_flight_msat: 0,
+                               holder_max_htlc_value_in_flight_msat: get_holder_max_htlc_value_in_flight_msat(channel_value_satoshis, &config.channel_handshake_config),
+                               counterparty_selected_channel_reserve_satoshis: None, // Filled in in accept_channel
+                               holder_selected_channel_reserve_satoshis,
+                               counterparty_htlc_minimum_msat: 0,
+                               holder_htlc_minimum_msat: if config.channel_handshake_config.our_htlc_minimum_msat == 0 { 1 } else { config.channel_handshake_config.our_htlc_minimum_msat },
+                               counterparty_max_accepted_htlcs: 0,
+                               holder_max_accepted_htlcs: cmp::min(config.channel_handshake_config.our_max_accepted_htlcs, MAX_HTLCS),
+                               minimum_depth: None, // Filled in in accept_channel
+
+                               counterparty_forwarding_info: None,
+
+                               channel_transaction_parameters: ChannelTransactionParameters {
+                                       holder_pubkeys: pubkeys,
+                                       holder_selected_contest_delay: config.channel_handshake_config.our_to_self_delay,
+                                       is_outbound_from_holder: true,
+                                       counterparty_parameters: None,
+                                       funding_outpoint: None,
+                                       channel_type_features: channel_type.clone()
+                               },
+                               funding_transaction: None,
+
+                               counterparty_cur_commitment_point: None,
+                               counterparty_prev_commitment_point: None,
+                               counterparty_node_id,
+
+                               counterparty_shutdown_scriptpubkey: None,
+
+                               commitment_secrets: CounterpartyCommitmentSecrets::new(),
+
+                               channel_update_status: ChannelUpdateStatus::Enabled,
+                               closing_signed_in_flight: false,
+
+                               announcement_sigs: None,
+
+                               #[cfg(any(test, fuzzing))]
+                               next_local_commitment_tx_fee_info_cached: Mutex::new(None),
+                               #[cfg(any(test, fuzzing))]
+                               next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
+
+                               workaround_lnd_bug_4006: None,
+                               sent_message_awaiting_response: None,
+
+                               latest_inbound_scid_alias: None,
+                               outbound_scid_alias,
+
+                               channel_pending_event_emitted: false,
+                               channel_ready_event_emitted: false,
+
+                               #[cfg(any(test, fuzzing))]
+                               historical_inbound_htlc_fulfills: HashSet::new(),
+
+                               channel_type,
+                               channel_keys_id,
+
+                               blocked_monitor_updates: Vec::new(),
+                       }
+               })
+       }
+
+       /// If an Err is returned, it is a ChannelError::Close (for get_outbound_funding_created)
+       fn get_outbound_funding_created_signature<L: Deref>(&mut self, logger: &L) -> Result<Signature, ChannelError> where L::Target: Logger {
+               let counterparty_keys = self.context.build_remote_transaction_keys();
+               let counterparty_initial_commitment_tx = self.context.build_commitment_transaction(self.context.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, false, logger).tx;
+               Ok(self.context.holder_signer.sign_counterparty_commitment(&counterparty_initial_commitment_tx, Vec::new(), &self.context.secp_ctx)
+                               .map_err(|_| ChannelError::Close("Failed to get signatures for new commitment_signed".to_owned()))?.0)
+       }
+
+       /// 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<L: Deref>(mut self, funding_transaction: Transaction, funding_txo: OutPoint, logger: &L)
+       -> Result<(Channel<Signer>, msgs::FundingCreated), (Self, ChannelError)> where L::Target: Logger {
+               if !self.context.is_outbound() {
+                       panic!("Tried to create outbound funding_created message on an inbound channel!");
+               }
+               if self.context.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.context.commitment_secrets.get_min_seen_secret() != (1 << 48) ||
+                               self.context.cur_counterparty_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER ||
+                               self.context.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
+                       panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
+               }
+
+               self.context.channel_transaction_parameters.funding_outpoint = Some(funding_txo);
+               self.context.holder_signer.provide_channel_parameters(&self.context.channel_transaction_parameters);
+
+               let signature = match self.get_outbound_funding_created_signature(logger) {
+                       Ok(res) => res,
+                       Err(e) => {
+                               log_error!(logger, "Got bad signatures: {:?}!", e);
+                               self.context.channel_transaction_parameters.funding_outpoint = None;
+                               return Err((self, e));
+                       }
+               };
+
+               let temporary_channel_id = self.context.channel_id;
+
+               // Now that we're past error-generating stuff, update our local state:
+
+               self.context.channel_state = ChannelState::FundingCreated as u32;
+               self.context.channel_id = funding_txo.to_channel_id();
+               self.context.funding_transaction = Some(funding_transaction);
+
+               let channel = Channel {
+                       context: self.context,
+               };
+
+               Ok((channel, msgs::FundingCreated {
+                       temporary_channel_id,
+                       funding_txid: funding_txo.txid,
+                       funding_output_index: funding_txo.index,
+                       signature,
+                       #[cfg(taproot)]
+                       partial_signature_with_nonce: None,
+                       #[cfg(taproot)]
+                       next_local_nonce: None,
+               }))
+       }
+
+       fn get_initial_channel_type(config: &UserConfig, their_features: &InitFeatures) -> ChannelTypeFeatures {
+               // The default channel type (ie the first one we try) depends on whether the channel is
+               // public - if it is, we just go with `only_static_remotekey` as it's the only option
+               // available. If it's private, we first try `scid_privacy` as it provides better privacy
+               // with no other changes, and fall back to `only_static_remotekey`.
+               let mut ret = ChannelTypeFeatures::only_static_remote_key();
+               if !config.channel_handshake_config.announced_channel &&
+                       config.channel_handshake_config.negotiate_scid_privacy &&
+                       their_features.supports_scid_privacy() {
+                       ret.set_scid_privacy_required();
+               }
+
+               // Optionally, if the user would like to negotiate the `anchors_zero_fee_htlc_tx` option, we
+               // set it now. If they don't understand it, we'll fall back to our default of
+               // `only_static_remotekey`.
+               if config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx &&
+                       their_features.supports_anchors_zero_fee_htlc_tx() {
+                       ret.set_anchors_zero_fee_htlc_tx_required();
+               }
+
+               ret
+       }
+
+       /// If we receive an error message, it may only be a rejection of the channel type we tried,
+       /// not of our ability to open any channel at all. Thus, on error, we should first call this
+       /// and see if we get a new `OpenChannel` message, otherwise the channel is failed.
+       pub(crate) fn maybe_handle_error_without_close(&mut self, chain_hash: BlockHash) -> Result<msgs::OpenChannel, ()> {
+               if !self.context.is_outbound() || self.context.channel_state != ChannelState::OurInitSent as u32 { return Err(()); }
+               if self.context.channel_type == ChannelTypeFeatures::only_static_remote_key() {
+                       // We've exhausted our options
+                       return Err(());
+               }
+               // We support opening a few different types of channels. Try removing our additional
+               // features one by one until we've either arrived at our default or the counterparty has
+               // accepted one.
+               //
+               // Due to the order below, we may not negotiate `option_anchors_zero_fee_htlc_tx` if the
+               // counterparty doesn't support `option_scid_privacy`. Since `get_initial_channel_type`
+               // checks whether the counterparty supports every feature, this would only happen if the
+               // counterparty is advertising the feature, but rejecting channels proposing the feature for
+               // whatever reason.
+               if self.context.channel_type.supports_anchors_zero_fee_htlc_tx() {
+                       self.context.channel_type.clear_anchors_zero_fee_htlc_tx();
+                       assert!(!self.context.channel_transaction_parameters.channel_type_features.supports_anchors_nonzero_fee_htlc_tx());
+               } else if self.context.channel_type.supports_scid_privacy() {
+                       self.context.channel_type.clear_scid_privacy();
+               } else {
+                       self.context.channel_type = ChannelTypeFeatures::only_static_remote_key();
+               }
+               self.context.channel_transaction_parameters.channel_type_features = self.context.channel_type.clone();
+               Ok(self.get_open_channel(chain_hash))
+       }
+
+       pub fn get_open_channel(&self, chain_hash: BlockHash) -> msgs::OpenChannel {
+               if !self.context.is_outbound() {
+                       panic!("Tried to open a channel for an inbound channel?");
+               }
+               if self.context.channel_state != ChannelState::OurInitSent as u32 {
+                       panic!("Cannot generate an open_channel after we've moved forward");
+               }
+
+               if self.context.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
+                       panic!("Tried to send an open_channel for a channel that has already advanced");
+               }
+
+               let first_per_commitment_point = self.context.holder_signer.get_per_commitment_point(self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
+               let keys = self.context.get_holder_pubkeys();
+
+               msgs::OpenChannel {
+                       chain_hash,
+                       temporary_channel_id: self.context.channel_id,
+                       funding_satoshis: self.context.channel_value_satoshis,
+                       push_msat: self.context.channel_value_satoshis * 1000 - self.context.value_to_self_msat,
+                       dust_limit_satoshis: self.context.holder_dust_limit_satoshis,
+                       max_htlc_value_in_flight_msat: self.context.holder_max_htlc_value_in_flight_msat,
+                       channel_reserve_satoshis: self.context.holder_selected_channel_reserve_satoshis,
+                       htlc_minimum_msat: self.context.holder_htlc_minimum_msat,
+                       feerate_per_kw: self.context.feerate_per_kw as u32,
+                       to_self_delay: self.context.get_holder_selected_contest_delay(),
+                       max_accepted_htlcs: self.context.holder_max_accepted_htlcs,
+                       funding_pubkey: keys.funding_pubkey,
+                       revocation_basepoint: keys.revocation_basepoint,
+                       payment_point: keys.payment_point,
+                       delayed_payment_basepoint: keys.delayed_payment_basepoint,
+                       htlc_basepoint: keys.htlc_basepoint,
+                       first_per_commitment_point,
+                       channel_flags: if self.context.config.announced_channel {1} else {0},
+                       shutdown_scriptpubkey: Some(match &self.context.shutdown_scriptpubkey {
+                               Some(script) => script.clone().into_inner(),
+                               None => Builder::new().into_script(),
+                       }),
+                       channel_type: Some(self.context.channel_type.clone()),
+               }
+       }
+
+       // Message handlers
+       pub fn accept_channel(&mut self, msg: &msgs::AcceptChannel, default_limits: &ChannelHandshakeLimits, their_features: &InitFeatures) -> Result<(), ChannelError> {
+               let peer_limits = if let Some(ref limits) = self.context.inbound_handshake_limits_override { limits } else { default_limits };
+
+               // Check sanity of message fields:
+               if !self.context.is_outbound() {
+                       return Err(ChannelError::Close("Got an accept_channel message from an inbound peer".to_owned()));
+               }
+               if self.context.channel_state != ChannelState::OurInitSent as u32 {
+                       return Err(ChannelError::Close("Got an accept_channel message at a strange time".to_owned()));
+               }
+               if msg.dust_limit_satoshis > 21000000 * 100000000 {
+                       return Err(ChannelError::Close(format!("Peer never wants payout outputs? dust_limit_satoshis was {}", msg.dust_limit_satoshis)));
+               }
+               if msg.channel_reserve_satoshis > self.context.channel_value_satoshis {
+                       return Err(ChannelError::Close(format!("Bogus channel_reserve_satoshis ({}). Must not be greater than ({})", msg.channel_reserve_satoshis, self.context.channel_value_satoshis)));
+               }
+               if msg.dust_limit_satoshis > self.context.holder_selected_channel_reserve_satoshis {
+                       return Err(ChannelError::Close(format!("Dust limit ({}) is bigger than our channel reserve ({})", msg.dust_limit_satoshis, self.context.holder_selected_channel_reserve_satoshis)));
+               }
+               if msg.channel_reserve_satoshis > self.context.channel_value_satoshis - self.context.holder_selected_channel_reserve_satoshis {
+                       return Err(ChannelError::Close(format!("Bogus channel_reserve_satoshis ({}). Must not be greater than channel value minus our reserve ({})",
+                               msg.channel_reserve_satoshis, self.context.channel_value_satoshis - self.context.holder_selected_channel_reserve_satoshis)));
+               }
+               let full_channel_value_msat = (self.context.channel_value_satoshis - msg.channel_reserve_satoshis) * 1000;
+               if msg.htlc_minimum_msat >= full_channel_value_msat {
+                       return Err(ChannelError::Close(format!("Minimum htlc value ({}) is full channel value ({})", msg.htlc_minimum_msat, full_channel_value_msat)));
+               }
+               let max_delay_acceptable = u16::min(peer_limits.their_to_self_delay, MAX_LOCAL_BREAKDOWN_TIMEOUT);
+               if msg.to_self_delay > max_delay_acceptable {
+                       return Err(ChannelError::Close(format!("They wanted our payments to be delayed by a needlessly long period. Upper limit: {}. Actual: {}", max_delay_acceptable, msg.to_self_delay)));
+               }
+               if msg.max_accepted_htlcs < 1 {
+                       return Err(ChannelError::Close("0 max_accepted_htlcs makes for a useless channel".to_owned()));
+               }
+               if msg.max_accepted_htlcs > MAX_HTLCS {
+                       return Err(ChannelError::Close(format!("max_accepted_htlcs was {}. It must not be larger than {}", msg.max_accepted_htlcs, MAX_HTLCS)));
+               }
+
+               // Now check against optional parameters as set by config...
+               if msg.htlc_minimum_msat > peer_limits.max_htlc_minimum_msat {
+                       return Err(ChannelError::Close(format!("htlc_minimum_msat ({}) is higher than the user specified limit ({})", msg.htlc_minimum_msat, peer_limits.max_htlc_minimum_msat)));
+               }
+               if msg.max_htlc_value_in_flight_msat < peer_limits.min_max_htlc_value_in_flight_msat {
+                       return Err(ChannelError::Close(format!("max_htlc_value_in_flight_msat ({}) is less than the user specified limit ({})", msg.max_htlc_value_in_flight_msat, peer_limits.min_max_htlc_value_in_flight_msat)));
+               }
+               if msg.channel_reserve_satoshis > peer_limits.max_channel_reserve_satoshis {
+                       return Err(ChannelError::Close(format!("channel_reserve_satoshis ({}) is higher than the user specified limit ({})", msg.channel_reserve_satoshis, peer_limits.max_channel_reserve_satoshis)));
+               }
+               if msg.max_accepted_htlcs < peer_limits.min_max_accepted_htlcs {
+                       return Err(ChannelError::Close(format!("max_accepted_htlcs ({}) is less than the user specified limit ({})", msg.max_accepted_htlcs, peer_limits.min_max_accepted_htlcs)));
+               }
+               if msg.dust_limit_satoshis < MIN_CHAN_DUST_LIMIT_SATOSHIS {
+                       return Err(ChannelError::Close(format!("dust_limit_satoshis ({}) is less than the implementation limit ({})", msg.dust_limit_satoshis, MIN_CHAN_DUST_LIMIT_SATOSHIS)));
+               }
+               if msg.dust_limit_satoshis > MAX_CHAN_DUST_LIMIT_SATOSHIS {
+                       return Err(ChannelError::Close(format!("dust_limit_satoshis ({}) is greater than the implementation limit ({})", msg.dust_limit_satoshis, MAX_CHAN_DUST_LIMIT_SATOSHIS)));
+               }
+               if msg.minimum_depth > peer_limits.max_minimum_depth {
+                       return Err(ChannelError::Close(format!("We consider the minimum depth to be unreasonably large. Expected minimum: ({}). Actual: ({})", peer_limits.max_minimum_depth, msg.minimum_depth)));
+               }
+
+               if let Some(ty) = &msg.channel_type {
+                       if *ty != self.context.channel_type {
+                               return Err(ChannelError::Close("Channel Type in accept_channel didn't match the one sent in open_channel.".to_owned()));
+                       }
+               } else if their_features.supports_channel_type() {
+                       // Assume they've accepted the channel type as they said they understand it.
+               } else {
+                       let channel_type = ChannelTypeFeatures::from_init(&their_features);
+                       if channel_type != ChannelTypeFeatures::only_static_remote_key() {
+                               return Err(ChannelError::Close("Only static_remote_key is supported for non-negotiated channel types".to_owned()));
+                       }
+                       self.context.channel_type = channel_type.clone();
+                       self.context.channel_transaction_parameters.channel_type_features = channel_type;
+               }
+
+               let counterparty_shutdown_scriptpubkey = if their_features.supports_upfront_shutdown_script() {
+                       match &msg.shutdown_scriptpubkey {
+                               &Some(ref script) => {
+                                       // Peer is signaling upfront_shutdown and has opt-out with a 0-length script. We don't enforce anything
+                                       if script.len() == 0 {
+                                               None
+                                       } else {
+                                               if !script::is_bolt2_compliant(&script, their_features) {
+                                                       return Err(ChannelError::Close(format!("Peer is signaling upfront_shutdown but has provided an unacceptable scriptpubkey format: {}", script)));
+                                               }
+                                               Some(script.clone())
+                                       }
+                               },
+                               // 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
+                               &None => {
+                                       return Err(ChannelError::Close("Peer is signaling upfront_shutdown but we don't get any script. Use 0-length script to opt-out".to_owned()));
+                               }
+                       }
+               } else { None };
+
+               self.context.counterparty_dust_limit_satoshis = msg.dust_limit_satoshis;
+               self.context.counterparty_max_htlc_value_in_flight_msat = cmp::min(msg.max_htlc_value_in_flight_msat, self.context.channel_value_satoshis * 1000);
+               self.context.counterparty_selected_channel_reserve_satoshis = Some(msg.channel_reserve_satoshis);
+               self.context.counterparty_htlc_minimum_msat = msg.htlc_minimum_msat;
+               self.context.counterparty_max_accepted_htlcs = msg.max_accepted_htlcs;
+
+               if peer_limits.trust_own_funding_0conf {
+                       self.context.minimum_depth = Some(msg.minimum_depth);
+               } else {
+                       self.context.minimum_depth = Some(cmp::max(1, msg.minimum_depth));
+               }
+
+               let counterparty_pubkeys = ChannelPublicKeys {
+                       funding_pubkey: msg.funding_pubkey,
+                       revocation_basepoint: msg.revocation_basepoint,
+                       payment_point: msg.payment_point,
+                       delayed_payment_basepoint: msg.delayed_payment_basepoint,
+                       htlc_basepoint: msg.htlc_basepoint
+               };
+
+               self.context.channel_transaction_parameters.counterparty_parameters = Some(CounterpartyChannelTransactionParameters {
+                       selected_contest_delay: msg.to_self_delay,
+                       pubkeys: counterparty_pubkeys,
+               });
+
+               self.context.counterparty_cur_commitment_point = Some(msg.first_per_commitment_point);
+               self.context.counterparty_shutdown_scriptpubkey = counterparty_shutdown_scriptpubkey;
+
+               self.context.channel_state = ChannelState::OurInitSent as u32 | ChannelState::TheirInitSent as u32;
+               self.context.inbound_handshake_limits_override = None; // We're done enforcing limits on our peer's handshake now.
+
+               Ok(())
+       }
+}
+
+/// A not-yet-funded inbound (from counterparty) channel using V1 channel establishment.
+pub(super) struct InboundV1Channel<Signer: ChannelSigner> {
+       pub context: ChannelContext<Signer>,
+}
+
+impl<Signer: WriteableEcdsaChannelSigner> InboundV1Channel<Signer> {
+       /// 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<ES: Deref, SP: Deref, F: Deref, L: Deref>(
+               fee_estimator: &LowerBoundedFeeEstimator<F>, entropy_source: &ES, signer_provider: &SP,
+               counterparty_node_id: PublicKey, our_supported_features: &ChannelTypeFeatures,
+               their_features: &InitFeatures, msg: &msgs::OpenChannel, user_id: u128, config: &UserConfig,
+               current_chain_height: u32, logger: &L, outbound_scid_alias: u64
+       ) -> Result<InboundV1Channel<Signer>, ChannelError>
+               where ES::Target: EntropySource,
+                         SP::Target: SignerProvider<Signer = Signer>,
+                         F::Target: FeeEstimator,
+                         L::Target: Logger,
+       {
+               let announced_channel = if (msg.channel_flags & 1) == 1 { true } else { false };
+
+               // First check the channel type is known, failing before we do anything else if we don't
+               // support this channel type.
+               let channel_type = if let Some(channel_type) = &msg.channel_type {
+                       if channel_type.supports_any_optional_bits() {
+                               return Err(ChannelError::Close("Channel Type field contained optional bits - this is not allowed".to_owned()));
+                       }
+
+                       // We only support the channel types defined by the `ChannelManager` in
+                       // `provided_channel_type_features`. The channel type must always support
+                       // `static_remote_key`.
+                       if !channel_type.requires_static_remote_key() {
+                               return Err(ChannelError::Close("Channel Type was not understood - we require static remote key".to_owned()));
+                       }
+                       // Make sure we support all of the features behind the channel type.
+                       if !channel_type.is_subset(our_supported_features) {
+                               return Err(ChannelError::Close("Channel Type contains unsupported features".to_owned()));
+                       }
+                       if channel_type.requires_scid_privacy() && announced_channel {
+                               return Err(ChannelError::Close("SCID Alias/Privacy Channel Type cannot be set on a public channel".to_owned()));
+                       }
+                       channel_type.clone()
+               } else {
+                       let channel_type = ChannelTypeFeatures::from_init(&their_features);
+                       if channel_type != ChannelTypeFeatures::only_static_remote_key() {
+                               return Err(ChannelError::Close("Only static_remote_key is supported for non-negotiated channel types".to_owned()));
+                       }
+                       channel_type
+               };
+
+               let channel_keys_id = signer_provider.generate_channel_keys_id(true, msg.funding_satoshis, user_id);
+               let holder_signer = signer_provider.derive_channel_signer(msg.funding_satoshis, channel_keys_id);
+               let pubkeys = holder_signer.pubkeys().clone();
+               let counterparty_pubkeys = ChannelPublicKeys {
+                       funding_pubkey: msg.funding_pubkey,
+                       revocation_basepoint: msg.revocation_basepoint,
+                       payment_point: msg.payment_point,
+                       delayed_payment_basepoint: msg.delayed_payment_basepoint,
+                       htlc_basepoint: msg.htlc_basepoint
+               };
+
+               if config.channel_handshake_config.our_to_self_delay < BREAKDOWN_TIMEOUT {
+                       return Err(ChannelError::Close(format!("Configured with an unreasonable our_to_self_delay ({}) putting user funds at risks. It must be greater than {}", config.channel_handshake_config.our_to_self_delay, BREAKDOWN_TIMEOUT)));
+               }
+
+               // Check sanity of message fields:
+               if msg.funding_satoshis > config.channel_handshake_limits.max_funding_satoshis {
+                       return Err(ChannelError::Close(format!("Per our config, funding must be at most {}. It was {}", config.channel_handshake_limits.max_funding_satoshis, msg.funding_satoshis)));
+               }
+               if msg.funding_satoshis >= TOTAL_BITCOIN_SUPPLY_SATOSHIS {
+                       return Err(ChannelError::Close(format!("Funding must be smaller than the total bitcoin supply. It was {}", msg.funding_satoshis)));
+               }
+               if msg.channel_reserve_satoshis > msg.funding_satoshis {
+                       return Err(ChannelError::Close(format!("Bogus channel_reserve_satoshis ({}). Must be not greater than funding_satoshis: {}", msg.channel_reserve_satoshis, msg.funding_satoshis)));
+               }
+               let full_channel_value_msat = (msg.funding_satoshis - msg.channel_reserve_satoshis) * 1000;
+               if msg.push_msat > full_channel_value_msat {
+                       return Err(ChannelError::Close(format!("push_msat {} was larger than channel amount minus reserve ({})", msg.push_msat, full_channel_value_msat)));
+               }
+               if msg.dust_limit_satoshis > msg.funding_satoshis {
+                       return Err(ChannelError::Close(format!("dust_limit_satoshis {} was larger than funding_satoshis {}. Peer never wants payout outputs?", msg.dust_limit_satoshis, msg.funding_satoshis)));
+               }
+               if msg.htlc_minimum_msat >= full_channel_value_msat {
+                       return Err(ChannelError::Close(format!("Minimum htlc value ({}) was larger than full channel value ({})", msg.htlc_minimum_msat, full_channel_value_msat)));
+               }
+               Channel::<Signer>::check_remote_fee(fee_estimator, msg.feerate_per_kw, None, logger)?;
+
+               let max_counterparty_selected_contest_delay = u16::min(config.channel_handshake_limits.their_to_self_delay, MAX_LOCAL_BREAKDOWN_TIMEOUT);
+               if msg.to_self_delay > max_counterparty_selected_contest_delay {
+                       return Err(ChannelError::Close(format!("They wanted our payments to be delayed by a needlessly long period. Upper limit: {}. Actual: {}", max_counterparty_selected_contest_delay, msg.to_self_delay)));
+               }
+               if msg.max_accepted_htlcs < 1 {
+                       return Err(ChannelError::Close("0 max_accepted_htlcs makes for a useless channel".to_owned()));
+               }
+               if msg.max_accepted_htlcs > MAX_HTLCS {
+                       return Err(ChannelError::Close(format!("max_accepted_htlcs was {}. It must not be larger than {}", msg.max_accepted_htlcs, MAX_HTLCS)));
                }
 
-               if amount_msat < self.counterparty_htlc_minimum_msat {
-                       return Err(ChannelError::Ignore(format!("Cannot send less than their minimum HTLC value ({})", self.counterparty_htlc_minimum_msat)));
+               // Now check against optional parameters as set by config...
+               if msg.funding_satoshis < config.channel_handshake_limits.min_funding_satoshis {
+                       return Err(ChannelError::Close(format!("Funding satoshis ({}) is less than the user specified limit ({})", msg.funding_satoshis, config.channel_handshake_limits.min_funding_satoshis)));
                }
-
-               if (self.channel_state & (ChannelState::PeerDisconnected 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 from channel counterparty".to_owned()));
+               if msg.htlc_minimum_msat > config.channel_handshake_limits.max_htlc_minimum_msat {
+                       return Err(ChannelError::Close(format!("htlc_minimum_msat ({}) is higher than the user specified limit ({})", msg.htlc_minimum_msat,  config.channel_handshake_limits.max_htlc_minimum_msat)));
                }
-
-               let inbound_stats = self.get_inbound_pending_htlc_stats(None);
-               let outbound_stats = self.get_outbound_pending_htlc_stats(None);
-               if outbound_stats.pending_htlcs + 1 > self.counterparty_max_accepted_htlcs as u32 {
-                       return Err(ChannelError::Ignore(format!("Cannot push more than their max accepted HTLCs ({})", self.counterparty_max_accepted_htlcs)));
+               if msg.max_htlc_value_in_flight_msat < config.channel_handshake_limits.min_max_htlc_value_in_flight_msat {
+                       return Err(ChannelError::Close(format!("max_htlc_value_in_flight_msat ({}) is less than the user specified limit ({})", msg.max_htlc_value_in_flight_msat, config.channel_handshake_limits.min_max_htlc_value_in_flight_msat)));
                }
-               // Check their_max_htlc_value_in_flight_msat
-               if outbound_stats.pending_htlcs_value_msat + amount_msat > self.counterparty_max_htlc_value_in_flight_msat {
-                       return Err(ChannelError::Ignore(format!("Cannot send value that would put us over the max HTLC value in flight our peer will accept ({})", self.counterparty_max_htlc_value_in_flight_msat)));
+               if msg.channel_reserve_satoshis > config.channel_handshake_limits.max_channel_reserve_satoshis {
+                       return Err(ChannelError::Close(format!("channel_reserve_satoshis ({}) is higher than the user specified limit ({})", msg.channel_reserve_satoshis, config.channel_handshake_limits.max_channel_reserve_satoshis)));
                }
-
-               let keys = self.build_holder_transaction_keys(self.cur_holder_commitment_transaction_number);
-               let commitment_stats = self.build_commitment_transaction(self.cur_holder_commitment_transaction_number, &keys, true, true, logger);
-               if !self.is_outbound() {
-                       // Check that we won't violate the remote channel reserve by adding this HTLC.
-                       let htlc_candidate = HTLCCandidate::new(amount_msat, HTLCInitiator::LocalOffered);
-                       let counterparty_commit_tx_fee_msat = self.next_remote_commit_tx_fee_msat(htlc_candidate, None);
-                       let holder_selected_chan_reserve_msat = self.holder_selected_channel_reserve_satoshis * 1000;
-                       if commitment_stats.remote_balance_msat < counterparty_commit_tx_fee_msat + holder_selected_chan_reserve_msat {
-                               return Err(ChannelError::Ignore("Cannot send value that would put counterparty balance under holder-announced channel reserve value".to_owned()));
-                       }
+               if msg.max_accepted_htlcs < config.channel_handshake_limits.min_max_accepted_htlcs {
+                       return Err(ChannelError::Close(format!("max_accepted_htlcs ({}) is less than the user specified limit ({})", msg.max_accepted_htlcs, config.channel_handshake_limits.min_max_accepted_htlcs)));
                }
-
-               let (htlc_success_dust_limit, htlc_timeout_dust_limit) = if self.opt_anchors() {
-                       (0, 0)
-               } else {
-                       let dust_buffer_feerate = self.get_dust_buffer_feerate(None) as u64;
-                       (dust_buffer_feerate * htlc_success_tx_weight(false) / 1000,
-                               dust_buffer_feerate * htlc_timeout_tx_weight(false) / 1000)
-               };
-               let exposure_dust_limit_success_sats = htlc_success_dust_limit + self.counterparty_dust_limit_satoshis;
-               if amount_msat / 1000 < exposure_dust_limit_success_sats {
-                       let on_counterparty_dust_htlc_exposure_msat = inbound_stats.on_counterparty_tx_dust_exposure_msat + outbound_stats.on_counterparty_tx_dust_exposure_msat + amount_msat;
-                       if on_counterparty_dust_htlc_exposure_msat > self.get_max_dust_htlc_exposure_msat() {
-                               return Err(ChannelError::Ignore(format!("Cannot send value that would put our exposure to dust HTLCs at {} over the limit {} on counterparty commitment tx",
-                                       on_counterparty_dust_htlc_exposure_msat, self.get_max_dust_htlc_exposure_msat())));
-                       }
+               if msg.dust_limit_satoshis < MIN_CHAN_DUST_LIMIT_SATOSHIS {
+                       return Err(ChannelError::Close(format!("dust_limit_satoshis ({}) is less than the implementation limit ({})", msg.dust_limit_satoshis, MIN_CHAN_DUST_LIMIT_SATOSHIS)));
+               }
+               if msg.dust_limit_satoshis >  MAX_CHAN_DUST_LIMIT_SATOSHIS {
+                       return Err(ChannelError::Close(format!("dust_limit_satoshis ({}) is greater than the implementation limit ({})", msg.dust_limit_satoshis, MAX_CHAN_DUST_LIMIT_SATOSHIS)));
                }
 
-               let exposure_dust_limit_timeout_sats = htlc_timeout_dust_limit + self.holder_dust_limit_satoshis;
-               if amount_msat / 1000 <  exposure_dust_limit_timeout_sats {
-                       let on_holder_dust_htlc_exposure_msat = inbound_stats.on_holder_tx_dust_exposure_msat + outbound_stats.on_holder_tx_dust_exposure_msat + amount_msat;
-                       if on_holder_dust_htlc_exposure_msat > self.get_max_dust_htlc_exposure_msat() {
-                               return Err(ChannelError::Ignore(format!("Cannot send value that would put our exposure to dust HTLCs at {} over the limit {} on holder commitment tx",
-                                       on_holder_dust_htlc_exposure_msat, self.get_max_dust_htlc_exposure_msat())));
+               // Convert things into internal flags and prep our state:
+
+               if config.channel_handshake_limits.force_announced_channel_preference {
+                       if config.channel_handshake_config.announced_channel != announced_channel {
+                               return Err(ChannelError::Close("Peer tried to open channel but their announcement preference is different from ours".to_owned()));
                        }
                }
 
-               let holder_balance_msat = commitment_stats.local_balance_msat - outbound_stats.holding_cell_msat;
-               if holder_balance_msat < amount_msat {
-                       return Err(ChannelError::Ignore(format!("Cannot send value that would overdraw remaining funds. Amount: {}, pending value to self {}", amount_msat, holder_balance_msat)));
+               let holder_selected_channel_reserve_satoshis = get_holder_selected_channel_reserve_satoshis(msg.funding_satoshis, config);
+               if holder_selected_channel_reserve_satoshis < MIN_CHAN_DUST_LIMIT_SATOSHIS {
+                       // Protocol level safety check in place, although it should never happen because
+                       // of `MIN_THEIR_CHAN_RESERVE_SATOSHIS`
+                       return Err(ChannelError::Close(format!("Suitable channel reserve not found. remote_channel_reserve was ({}). dust_limit_satoshis is ({}).", holder_selected_channel_reserve_satoshis, MIN_CHAN_DUST_LIMIT_SATOSHIS)));
                }
-
-               // `2 *` and extra HTLC are for the fee spike buffer.
-               let commit_tx_fee_msat = if self.is_outbound() {
-                       let htlc_candidate = HTLCCandidate::new(amount_msat, HTLCInitiator::LocalOffered);
-                       FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE * self.next_local_commit_tx_fee_msat(htlc_candidate, Some(()))
-               } else { 0 };
-               if holder_balance_msat - amount_msat < commit_tx_fee_msat {
-                       return Err(ChannelError::Ignore(format!("Cannot send value that would not leave enough to pay for fees. Pending value to self: {}. local_commit_tx_fee {}", holder_balance_msat, commit_tx_fee_msat)));
+               if holder_selected_channel_reserve_satoshis * 1000 >= full_channel_value_msat {
+                       return Err(ChannelError::Close(format!("Suitable channel reserve not found. remote_channel_reserve was ({})msats. Channel value is ({} - {})msats.", holder_selected_channel_reserve_satoshis * 1000, full_channel_value_msat, msg.push_msat)));
                }
-
-               // Check self.counterparty_selected_channel_reserve_satoshis (the amount we must keep as
-               // reserve for the remote to have something to claim if we misbehave)
-               let chan_reserve_msat = self.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000;
-               if holder_balance_msat - amount_msat - commit_tx_fee_msat < chan_reserve_msat {
-                       return Err(ChannelError::Ignore(format!("Cannot send value that would put our balance under counterparty-announced channel reserve value ({})", chan_reserve_msat)));
+               if msg.channel_reserve_satoshis < MIN_CHAN_DUST_LIMIT_SATOSHIS {
+                       log_debug!(logger, "channel_reserve_satoshis ({}) is smaller than our dust limit ({}). We can broadcast stale states without any risk, implying this channel is very insecure for our counterparty.",
+                               msg.channel_reserve_satoshis, MIN_CHAN_DUST_LIMIT_SATOSHIS);
                }
-
-               if (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32 | ChannelState::MonitorUpdateInProgress as u32)) != 0 {
-                       force_holding_cell = true;
+               if holder_selected_channel_reserve_satoshis < msg.dust_limit_satoshis {
+                       return Err(ChannelError::Close(format!("Dust limit ({}) too high for the channel reserve we require the remote to keep ({})", msg.dust_limit_satoshis, holder_selected_channel_reserve_satoshis)));
                }
 
-               // Now update local state:
-               if force_holding_cell {
-                       self.holding_cell_htlc_updates.push(HTLCUpdateAwaitingACK::AddHTLC {
-                               amount_msat,
-                               payment_hash,
-                               cltv_expiry,
-                               source,
-                               onion_routing_packet,
-                       });
-                       return Ok(None);
+               // check if the funder's amount for the initial commitment tx is sufficient
+               // for full fee payment plus a few HTLCs to ensure the channel will be useful.
+               let funders_amount_msat = msg.funding_satoshis * 1000 - msg.push_msat;
+               let commitment_tx_fee = commit_tx_fee_msat(msg.feerate_per_kw, MIN_AFFORDABLE_HTLC_COUNT, &channel_type) / 1000;
+               if funders_amount_msat / 1000 < commitment_tx_fee {
+                       return Err(ChannelError::Close(format!("Funding amount ({} sats) can't even pay fee for initial commitment transaction fee of {} sats.", funders_amount_msat / 1000, commitment_tx_fee)));
                }
 
-               self.pending_outbound_htlcs.push(OutboundHTLCOutput {
-                       htlc_id: self.next_holder_htlc_id,
-                       amount_msat,
-                       payment_hash: payment_hash.clone(),
-                       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_holder_htlc_id,
-                       amount_msat,
-                       payment_hash,
-                       cltv_expiry,
-                       onion_routing_packet,
-               };
-               self.next_holder_htlc_id += 1;
-
-               Ok(Some(res))
-       }
+               let to_remote_satoshis = funders_amount_msat / 1000 - commitment_tx_fee;
+               // While it's reasonable for us to not meet the channel reserve initially (if they don't
+               // want to push much to us), our counterparty should always have more than our reserve.
+               if to_remote_satoshis < holder_selected_channel_reserve_satoshis {
+                       return Err(ChannelError::Close("Insufficient funding amount for initial reserve".to_owned()));
+               }
 
-       fn build_commitment_no_status_check<L: Deref>(&mut self, logger: &L) -> ChannelMonitorUpdate where L::Target: Logger {
-               log_trace!(logger, "Updating HTLC state for a newly-sent commitment_signed...");
-               // 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 {
-                               log_trace!(logger, " ...promoting inbound AwaitingRemoteRevokeToAnnounce {} to AwaitingAnnouncedRemoteRevoke", log_bytes!(htlc.payment_hash.0));
-                               htlc.state = state;
+               let counterparty_shutdown_scriptpubkey = if their_features.supports_upfront_shutdown_script() {
+                       match &msg.shutdown_scriptpubkey {
+                               &Some(ref script) => {
+                                       // Peer is signaling upfront_shutdown and has opt-out with a 0-length script. We don't enforce anything
+                                       if script.len() == 0 {
+                                               None
+                                       } else {
+                                               if !script::is_bolt2_compliant(&script, their_features) {
+                                                       return Err(ChannelError::Close(format!("Peer is signaling upfront_shutdown but has provided an unacceptable scriptpubkey format: {}", script)))
+                                               }
+                                               Some(script.clone())
+                                       }
+                               },
+                               // 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
+                               &None => {
+                                       return Err(ChannelError::Close("Peer is signaling upfront_shutdown but we don't get any script. Use 0-length script to opt-out".to_owned()));
+                               }
                        }
-               }
-               for htlc in self.pending_outbound_htlcs.iter_mut() {
-                       if let &mut OutboundHTLCState::AwaitingRemoteRevokeToRemove(ref mut outcome) = &mut htlc.state {
-                               log_trace!(logger, " ...promoting outbound AwaitingRemoteRevokeToRemove {} to AwaitingRemovedRemoteRevoke", log_bytes!(htlc.payment_hash.0));
-                               // Grab the preimage, if it exists, instead of cloning
-                               let mut reason = OutboundHTLCOutcome::Success(None);
-                               mem::swap(outcome, &mut reason);
-                               htlc.state = OutboundHTLCState::AwaitingRemovedRemoteRevoke(reason);
+               } else { None };
+
+               let shutdown_scriptpubkey = if config.channel_handshake_config.commit_upfront_shutdown_pubkey {
+                       match signer_provider.get_shutdown_scriptpubkey() {
+                               Ok(scriptpubkey) => Some(scriptpubkey),
+                               Err(_) => return Err(ChannelError::Close("Failed to get upfront shutdown scriptpubkey".to_owned())),
                        }
-               }
-               if let Some((feerate, update_state)) = self.pending_update_fee {
-                       if update_state == FeeUpdateState::AwaitingRemoteRevokeToAnnounce {
-                               debug_assert!(!self.is_outbound());
-                               log_trace!(logger, " ...promoting inbound AwaitingRemoteRevokeToAnnounce fee update {} to Committed", feerate);
-                               self.feerate_per_kw = feerate;
-                               self.pending_update_fee = None;
+               } else { None };
+
+               if let Some(shutdown_scriptpubkey) = &shutdown_scriptpubkey {
+                       if !shutdown_scriptpubkey.is_compatible(&their_features) {
+                               return Err(ChannelError::Close(format!("Provided a scriptpubkey format not accepted by peer: {}", shutdown_scriptpubkey)));
                        }
                }
-               self.resend_order = RAACommitmentOrder::RevokeAndACKFirst;
 
-               let (counterparty_commitment_txid, mut htlcs_ref) = self.build_commitment_no_state_update(logger);
-               let htlcs: Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)> =
-                       htlcs_ref.drain(..).map(|(htlc, htlc_source)| (htlc, htlc_source.map(|source_ref| Box::new(source_ref.clone())))).collect();
+               let destination_script = match signer_provider.get_destination_script() {
+                       Ok(script) => script,
+                       Err(_) => return Err(ChannelError::Close("Failed to get destination script".to_owned())),
+               };
 
-               if self.announcement_sigs_state == AnnouncementSigsState::MessageSent {
-                       self.announcement_sigs_state = AnnouncementSigsState::Committed;
-               }
+               let mut secp_ctx = Secp256k1::new();
+               secp_ctx.seeded_randomize(&entropy_source.get_secure_random_bytes());
 
-               self.latest_monitor_update_id += 1;
-               let monitor_update = ChannelMonitorUpdate {
-                       update_id: self.latest_monitor_update_id,
-                       updates: vec![ChannelMonitorUpdateStep::LatestCounterpartyCommitmentTXInfo {
-                               commitment_txid: counterparty_commitment_txid,
-                               htlc_outputs: htlcs.clone(),
-                               commitment_number: self.cur_counterparty_commitment_transaction_number,
-                               their_per_commitment_point: self.counterparty_cur_commitment_point.unwrap()
-                       }]
-               };
-               self.channel_state |= ChannelState::AwaitingRemoteRevoke as u32;
-               monitor_update
-       }
+               let chan = Self {
+                       context: ChannelContext {
+                               user_id,
 
-       fn build_commitment_no_state_update<L: Deref>(&self, logger: &L) -> (Txid, Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)>) where L::Target: Logger {
-               let counterparty_keys = self.build_remote_transaction_keys();
-               let commitment_stats = self.build_commitment_transaction(self.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, true, logger);
-               let counterparty_commitment_txid = commitment_stats.tx.trust().txid();
+                               config: LegacyChannelConfig {
+                                       options: config.channel_config.clone(),
+                                       announced_channel,
+                                       commit_upfront_shutdown_pubkey: config.channel_handshake_config.commit_upfront_shutdown_pubkey,
+                               },
 
-               #[cfg(any(test, fuzzing))]
-               {
-                       if !self.is_outbound() {
-                               let projected_commit_tx_info = self.next_remote_commitment_tx_fee_info_cached.lock().unwrap().take();
-                               *self.next_local_commitment_tx_fee_info_cached.lock().unwrap() = None;
-                               if let Some(info) = projected_commit_tx_info {
-                                       let total_pending_htlcs = self.pending_inbound_htlcs.len() + self.pending_outbound_htlcs.len();
-                                       if info.total_pending_htlcs == total_pending_htlcs
-                                               && info.next_holder_htlc_id == self.next_holder_htlc_id
-                                               && info.next_counterparty_htlc_id == self.next_counterparty_htlc_id
-                                               && info.feerate == self.feerate_per_kw {
-                                                       let actual_fee = Self::commit_tx_fee_msat(self.feerate_per_kw, commitment_stats.num_nondust_htlcs, self.opt_anchors());
-                                                       assert_eq!(actual_fee, info.fee);
-                                               }
-                               }
-                       }
-               }
+                               prev_config: None,
+
+                               inbound_handshake_limits_override: None,
+
+                               temporary_channel_id: Some(msg.temporary_channel_id),
+                               channel_id: msg.temporary_channel_id,
+                               channel_state: (ChannelState::OurInitSent as u32) | (ChannelState::TheirInitSent as u32),
+                               announcement_sigs_state: AnnouncementSigsState::NotSent,
+                               secp_ctx,
+
+                               latest_monitor_update_id: 0,
+
+                               holder_signer,
+                               shutdown_scriptpubkey,
+                               destination_script,
+
+                               cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
+                               cur_counterparty_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_holder_htlc_id: 0,
+                               next_counterparty_htlc_id: 0,
+                               update_time_counter: 1,
+
+                               resend_order: RAACommitmentOrder::CommitmentFirst,
+
+                               monitor_pending_channel_ready: false,
+                               monitor_pending_revoke_and_ack: false,
+                               monitor_pending_commitment_signed: false,
+                               monitor_pending_forwards: Vec::new(),
+                               monitor_pending_failures: Vec::new(),
+                               monitor_pending_finalized_fulfills: Vec::new(),
+
+                               #[cfg(debug_assertions)]
+                               holder_max_commitment_tx_output: Mutex::new((msg.push_msat, msg.funding_satoshis * 1000 - msg.push_msat)),
+                               #[cfg(debug_assertions)]
+                               counterparty_max_commitment_tx_output: Mutex::new((msg.push_msat, msg.funding_satoshis * 1000 - msg.push_msat)),
+
+                               last_sent_closing_fee: None,
+                               pending_counterparty_closing_signed: None,
+                               closing_fee_limits: None,
+                               target_closing_feerate_sats_per_kw: None,
+
+                               inbound_awaiting_accept: true,
+
+                               funding_tx_confirmed_in: None,
+                               funding_tx_confirmation_height: 0,
+                               short_channel_id: None,
+                               channel_creation_height: current_chain_height,
+
+                               feerate_per_kw: msg.feerate_per_kw,
+                               channel_value_satoshis: msg.funding_satoshis,
+                               counterparty_dust_limit_satoshis: msg.dust_limit_satoshis,
+                               holder_dust_limit_satoshis: MIN_CHAN_DUST_LIMIT_SATOSHIS,
+                               counterparty_max_htlc_value_in_flight_msat: cmp::min(msg.max_htlc_value_in_flight_msat, msg.funding_satoshis * 1000),
+                               holder_max_htlc_value_in_flight_msat: get_holder_max_htlc_value_in_flight_msat(msg.funding_satoshis, &config.channel_handshake_config),
+                               counterparty_selected_channel_reserve_satoshis: Some(msg.channel_reserve_satoshis),
+                               holder_selected_channel_reserve_satoshis,
+                               counterparty_htlc_minimum_msat: msg.htlc_minimum_msat,
+                               holder_htlc_minimum_msat: if config.channel_handshake_config.our_htlc_minimum_msat == 0 { 1 } else { config.channel_handshake_config.our_htlc_minimum_msat },
+                               counterparty_max_accepted_htlcs: msg.max_accepted_htlcs,
+                               holder_max_accepted_htlcs: cmp::min(config.channel_handshake_config.our_max_accepted_htlcs, MAX_HTLCS),
+                               minimum_depth: Some(cmp::max(config.channel_handshake_config.minimum_depth, 1)),
+
+                               counterparty_forwarding_info: None,
+
+                               channel_transaction_parameters: ChannelTransactionParameters {
+                                       holder_pubkeys: pubkeys,
+                                       holder_selected_contest_delay: config.channel_handshake_config.our_to_self_delay,
+                                       is_outbound_from_holder: false,
+                                       counterparty_parameters: Some(CounterpartyChannelTransactionParameters {
+                                               selected_contest_delay: msg.to_self_delay,
+                                               pubkeys: counterparty_pubkeys,
+                                       }),
+                                       funding_outpoint: None,
+                                       channel_type_features: channel_type.clone()
+                               },
+                               funding_transaction: None,
 
-               (counterparty_commitment_txid, commitment_stats.htlcs_included)
-       }
+                               counterparty_cur_commitment_point: Some(msg.first_per_commitment_point),
+                               counterparty_prev_commitment_point: None,
+                               counterparty_node_id,
 
-       /// Only fails in case of signer rejection. Used for channel_reestablish commitment_signed
-       /// generation when we shouldn't change HTLC/channel state.
-       fn send_commitment_no_state_update<L: Deref>(&self, logger: &L) -> Result<(msgs::CommitmentSigned, (Txid, Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)>)), ChannelError> where L::Target: Logger {
-               // Get the fee tests from `build_commitment_no_state_update`
-               #[cfg(any(test, fuzzing))]
-               self.build_commitment_no_state_update(logger);
+                               counterparty_shutdown_scriptpubkey,
 
-               let counterparty_keys = self.build_remote_transaction_keys();
-               let commitment_stats = self.build_commitment_transaction(self.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, true, logger);
-               let counterparty_commitment_txid = commitment_stats.tx.trust().txid();
-               let (signature, htlc_signatures);
+                               commitment_secrets: CounterpartyCommitmentSecrets::new(),
 
-               {
-                       let mut htlcs = Vec::with_capacity(commitment_stats.htlcs_included.len());
-                       for &(ref htlc, _) in commitment_stats.htlcs_included.iter() {
-                               htlcs.push(htlc);
-                       }
+                               channel_update_status: ChannelUpdateStatus::Enabled,
+                               closing_signed_in_flight: false,
 
-                       let res = self.holder_signer.sign_counterparty_commitment(&commitment_stats.tx, commitment_stats.preimages, &self.secp_ctx)
-                               .map_err(|_| ChannelError::Close("Failed to get signatures for new commitment_signed".to_owned()))?;
-                       signature = res.0;
-                       htlc_signatures = res.1;
+                               announcement_sigs: None,
 
-                       log_trace!(logger, "Signed remote commitment tx {} (txid {}) with redeemscript {} -> {} in channel {}",
-                               encode::serialize_hex(&commitment_stats.tx.trust().built_transaction().transaction),
-                               &counterparty_commitment_txid, encode::serialize_hex(&self.get_funding_redeemscript()),
-                               log_bytes!(signature.serialize_compact()[..]), log_bytes!(self.channel_id()));
+                               #[cfg(any(test, fuzzing))]
+                               next_local_commitment_tx_fee_info_cached: Mutex::new(None),
+                               #[cfg(any(test, fuzzing))]
+                               next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
 
-                       for (ref htlc_sig, ref htlc) in htlc_signatures.iter().zip(htlcs) {
-                               log_trace!(logger, "Signed remote HTLC tx {} with redeemscript {} with pubkey {} -> {} in channel {}",
-                                       encode::serialize_hex(&chan_utils::build_htlc_transaction(&counterparty_commitment_txid, commitment_stats.feerate_per_kw, self.get_holder_selected_contest_delay(), htlc, self.opt_anchors(), false, &counterparty_keys.broadcaster_delayed_payment_key, &counterparty_keys.revocation_key)),
-                                       encode::serialize_hex(&chan_utils::get_htlc_redeemscript(&htlc, self.opt_anchors(), &counterparty_keys)),
-                                       log_bytes!(counterparty_keys.broadcaster_htlc_key.serialize()),
-                                       log_bytes!(htlc_sig.serialize_compact()[..]), log_bytes!(self.channel_id()));
+                               workaround_lnd_bug_4006: None,
+                               sent_message_awaiting_response: None,
+
+                               latest_inbound_scid_alias: None,
+                               outbound_scid_alias,
+
+                               channel_pending_event_emitted: false,
+                               channel_ready_event_emitted: false,
+
+                               #[cfg(any(test, fuzzing))]
+                               historical_inbound_htlc_fulfills: HashSet::new(),
+
+                               channel_type,
+                               channel_keys_id,
+
+                               blocked_monitor_updates: Vec::new(),
                        }
+               };
+
+               Ok(chan)
+       }
+
+       pub fn is_awaiting_accept(&self) -> bool {
+               self.context.inbound_awaiting_accept
+       }
+
+       /// Sets this channel to accepting 0conf, must be done before `get_accept_channel`
+       pub fn set_0conf(&mut self) {
+               assert!(self.context.inbound_awaiting_accept);
+               self.context.minimum_depth = Some(0);
+       }
+
+       /// Marks an inbound channel as accepted and generates a [`msgs::AcceptChannel`] message which
+       /// should be sent back to the counterparty node.
+       ///
+       /// [`msgs::AcceptChannel`]: crate::ln::msgs::AcceptChannel
+       pub fn accept_inbound_channel(&mut self, user_id: u128) -> msgs::AcceptChannel {
+               if self.context.is_outbound() {
+                       panic!("Tried to send accept_channel for an outbound channel?");
+               }
+               if self.context.channel_state != (ChannelState::OurInitSent as u32) | (ChannelState::TheirInitSent as u32) {
+                       panic!("Tried to send accept_channel after channel had moved forward");
+               }
+               if self.context.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
+                       panic!("Tried to send an accept_channel for a channel that has already advanced");
+               }
+               if !self.context.inbound_awaiting_accept {
+                       panic!("The inbound channel has already been accepted");
                }
 
-               Ok((msgs::CommitmentSigned {
-                       channel_id: self.channel_id,
-                       signature,
-                       htlc_signatures,
-                       #[cfg(taproot)]
-                       partial_signature_with_nonce: None,
-               }, (counterparty_commitment_txid, commitment_stats.htlcs_included)))
+               self.context.user_id = user_id;
+               self.context.inbound_awaiting_accept = false;
+
+               self.generate_accept_channel_message()
        }
 
-       /// Adds a pending outbound HTLC to this channel, and builds a new remote commitment
-       /// transaction and generates the corresponding [`ChannelMonitorUpdate`] in one go.
+       /// This function is used to explicitly generate a [`msgs::AcceptChannel`] message for an
+       /// inbound channel. If the intention is to accept an inbound channel, use
+       /// [`InboundV1Channel::accept_inbound_channel`] instead.
        ///
-       /// Shorthand for calling [`Self::send_htlc`] followed by a commitment update, see docs on
-       /// [`Self::send_htlc`] and [`Self::build_commitment_no_state_update`] for more info.
-       pub fn send_htlc_and_commit<L: Deref>(&mut self, amount_msat: u64, payment_hash: PaymentHash, cltv_expiry: u32, source: HTLCSource, onion_routing_packet: msgs::OnionPacket, logger: &L) -> Result<Option<&ChannelMonitorUpdate>, ChannelError> where L::Target: Logger {
-               let send_res = self.send_htlc(amount_msat, payment_hash, cltv_expiry, source, onion_routing_packet, false, logger);
-               if let Err(e) = &send_res { if let ChannelError::Ignore(_) = e {} else { debug_assert!(false, "Sending cannot trigger channel failure"); } }
-               match send_res? {
-                       Some(_) => {
-                               let monitor_update = self.build_commitment_no_status_check(logger);
-                               self.monitor_updating_paused(false, true, false, Vec::new(), Vec::new(), Vec::new());
-                               Ok(self.push_ret_blockable_mon_update(monitor_update))
-                       },
-                       None => Ok(None)
+       /// [`msgs::AcceptChannel`]: crate::ln::msgs::AcceptChannel
+       fn generate_accept_channel_message(&self) -> msgs::AcceptChannel {
+               let first_per_commitment_point = self.context.holder_signer.get_per_commitment_point(self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
+               let keys = self.context.get_holder_pubkeys();
+
+               msgs::AcceptChannel {
+                       temporary_channel_id: self.context.channel_id,
+                       dust_limit_satoshis: self.context.holder_dust_limit_satoshis,
+                       max_htlc_value_in_flight_msat: self.context.holder_max_htlc_value_in_flight_msat,
+                       channel_reserve_satoshis: self.context.holder_selected_channel_reserve_satoshis,
+                       htlc_minimum_msat: self.context.holder_htlc_minimum_msat,
+                       minimum_depth: self.context.minimum_depth.unwrap(),
+                       to_self_delay: self.context.get_holder_selected_contest_delay(),
+                       max_accepted_htlcs: self.context.holder_max_accepted_htlcs,
+                       funding_pubkey: keys.funding_pubkey,
+                       revocation_basepoint: keys.revocation_basepoint,
+                       payment_point: keys.payment_point,
+                       delayed_payment_basepoint: keys.delayed_payment_basepoint,
+                       htlc_basepoint: keys.htlc_basepoint,
+                       first_per_commitment_point,
+                       shutdown_scriptpubkey: Some(match &self.context.shutdown_scriptpubkey {
+                               Some(script) => script.clone().into_inner(),
+                               None => Builder::new().into_script(),
+                       }),
+                       channel_type: Some(self.context.channel_type.clone()),
+                       #[cfg(taproot)]
+                       next_local_nonce: None,
                }
        }
 
-       /// Get forwarding information for the counterparty.
-       pub fn counterparty_forwarding_info(&self) -> Option<CounterpartyForwardingInfo> {
-               self.counterparty_forwarding_info.clone()
+       /// Enables the possibility for tests to extract a [`msgs::AcceptChannel`] message for an
+       /// inbound channel without accepting it.
+       ///
+       /// [`msgs::AcceptChannel`]: crate::ln::msgs::AcceptChannel
+       #[cfg(test)]
+       pub fn get_accept_channel_message(&self) -> msgs::AcceptChannel {
+               self.generate_accept_channel_message()
        }
 
-       pub fn channel_update(&mut self, msg: &msgs::ChannelUpdate) -> Result<(), ChannelError> {
-               if msg.contents.htlc_minimum_msat >= self.channel_value_satoshis * 1000 {
-                       return Err(ChannelError::Close("Minimum htlc value is greater than channel value".to_string()));
+       fn funding_created_signature<L: Deref>(&mut self, sig: &Signature, logger: &L) -> Result<(Txid, CommitmentTransaction, Signature), ChannelError> where L::Target: Logger {
+               let funding_script = self.context.get_funding_redeemscript();
+
+               let keys = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number);
+               let initial_commitment_tx = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &keys, true, false, logger).tx;
+               {
+                       let trusted_tx = initial_commitment_tx.trust();
+                       let initial_commitment_bitcoin_tx = trusted_tx.built_transaction();
+                       let sighash = initial_commitment_bitcoin_tx.get_sighash_all(&funding_script, self.context.channel_value_satoshis);
+                       // They sign the holder commitment transaction...
+                       log_trace!(logger, "Checking funding_created tx signature {} by key {} against tx {} (sighash {}) with redeemscript {} for channel {}.",
+                               log_bytes!(sig.serialize_compact()[..]), log_bytes!(self.context.counterparty_funding_pubkey().serialize()),
+                               encode::serialize_hex(&initial_commitment_bitcoin_tx.transaction), log_bytes!(sighash[..]),
+                               encode::serialize_hex(&funding_script), log_bytes!(self.context.channel_id()));
+                       secp_check!(self.context.secp_ctx.verify_ecdsa(&sighash, &sig, self.context.counterparty_funding_pubkey()), "Invalid funding_created signature from peer".to_owned());
                }
-               self.counterparty_forwarding_info = Some(CounterpartyForwardingInfo {
-                       fee_base_msat: msg.contents.fee_base_msat,
-                       fee_proportional_millionths: msg.contents.fee_proportional_millionths,
-                       cltv_expiry_delta: msg.contents.cltv_expiry_delta
-               });
 
-               Ok(())
+               let counterparty_keys = self.context.build_remote_transaction_keys();
+               let counterparty_initial_commitment_tx = self.context.build_commitment_transaction(self.context.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, false, logger).tx;
+
+               let counterparty_trusted_tx = counterparty_initial_commitment_tx.trust();
+               let counterparty_initial_bitcoin_tx = counterparty_trusted_tx.built_transaction();
+               log_trace!(logger, "Initial counterparty tx for channel {} is: txid {} tx {}",
+                       log_bytes!(self.context.channel_id()), counterparty_initial_bitcoin_tx.txid, encode::serialize_hex(&counterparty_initial_bitcoin_tx.transaction));
+
+               let counterparty_signature = self.context.holder_signer.sign_counterparty_commitment(&counterparty_initial_commitment_tx, Vec::new(), &self.context.secp_ctx)
+                               .map_err(|_| ChannelError::Close("Failed to get signatures for new commitment_signed".to_owned()))?.0;
+
+               // We sign "counterparty" commitment transaction, allowing them to broadcast the tx if they wish.
+               Ok((counterparty_initial_bitcoin_tx.txid, initial_commitment_tx, counterparty_signature))
        }
 
-       /// Begins the shutdown process, getting a message for the remote peer and returning all
-       /// holding cell HTLCs for payment failure.
-       ///
-       /// May jump to the channel being fully shutdown (see [`Self::is_shutdown`]) in which case no
-       /// [`ChannelMonitorUpdate`] will be returned).
-       pub fn get_shutdown<SP: Deref>(&mut self, signer_provider: &SP, their_features: &InitFeatures,
-               target_feerate_sats_per_kw: Option<u32>, override_shutdown_script: Option<ShutdownScript>)
-       -> Result<(msgs::Shutdown, Option<&ChannelMonitorUpdate>, Vec<(HTLCSource, PaymentHash)>), APIError>
-       where SP::Target: SignerProvider {
-               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".to_owned()});
-                       }
+       pub fn funding_created<SP: Deref, L: Deref>(
+               mut self, msg: &msgs::FundingCreated, best_block: BestBlock, signer_provider: &SP, logger: &L
+       ) -> Result<(Channel<Signer>, msgs::FundingSigned, ChannelMonitor<Signer>), (Self, ChannelError)>
+       where
+               SP::Target: SignerProvider<Signer = Signer>,
+               L::Target: Logger
+       {
+               if self.context.is_outbound() {
+                       return Err((self, ChannelError::Close("Received funding_created for an outbound channel?".to_owned())));
                }
-               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".to_owned()});
-                       }
-                       else if (self.channel_state & ChannelState::RemoteShutdownSent as u32) == ChannelState::RemoteShutdownSent as u32 {
-                               return Err(APIError::ChannelUnavailable{err: "Shutdown already in progress by remote".to_owned()});
-                       }
+               if self.context.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((self, ChannelError::Close("Received funding_created after we got the channel!".to_owned())));
                }
-               if self.shutdown_scriptpubkey.is_some() && override_shutdown_script.is_some() {
-                       return Err(APIError::APIMisuseError{err: "Cannot override shutdown script for a channel with one already set".to_owned()});
+               if self.context.inbound_awaiting_accept {
+                       return Err((self, ChannelError::Close("FundingCreated message received before the channel was accepted".to_owned())));
                }
-               assert_eq!(self.channel_state & ChannelState::ShutdownComplete as u32, 0);
-               if self.channel_state & (ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress 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?".to_owned()});
+               if self.context.commitment_secrets.get_min_seen_secret() != (1 << 48) ||
+                               self.context.cur_counterparty_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER ||
+                               self.context.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
+                       panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
                }
 
-               // If we haven't funded the channel yet, we don't need to bother ensuring the shutdown
-               // script is set, we just force-close and call it a day.
-               let mut chan_closed = false;
-               if self.channel_state < ChannelState::FundingSent as u32 {
-                       chan_closed = true;
-               }
+               let funding_txo = OutPoint { txid: msg.funding_txid, index: msg.funding_output_index };
+               self.context.channel_transaction_parameters.funding_outpoint = Some(funding_txo);
+               // This is an externally observable change before we finish all our checks.  In particular
+               // funding_created_signature may fail.
+               self.context.holder_signer.provide_channel_parameters(&self.context.channel_transaction_parameters);
 
-               let update_shutdown_script = match self.shutdown_scriptpubkey {
-                       Some(_) => false,
-                       None if !chan_closed => {
-                               // use override shutdown script if provided
-                               let shutdown_scriptpubkey = match override_shutdown_script {
-                                       Some(script) => script,
-                                       None => {
-                                               // otherwise, use the shutdown scriptpubkey provided by the signer
-                                               match signer_provider.get_shutdown_scriptpubkey() {
-                                                       Ok(scriptpubkey) => scriptpubkey,
-                                                       Err(_) => return Err(APIError::ChannelUnavailable{err: "Failed to get shutdown scriptpubkey".to_owned()}),
-                                               }
-                                       },
-                               };
-                               if !shutdown_scriptpubkey.is_compatible(their_features) {
-                                       return Err(APIError::IncompatibleShutdownScript { script: shutdown_scriptpubkey.clone() });
-                               }
-                               self.shutdown_scriptpubkey = Some(shutdown_scriptpubkey);
-                               true
+               let (counterparty_initial_commitment_txid, initial_commitment_tx, signature) = match self.funding_created_signature(&msg.signature, logger) {
+                       Ok(res) => res,
+                       Err(ChannelError::Close(e)) => {
+                               self.context.channel_transaction_parameters.funding_outpoint = None;
+                               return Err((self, ChannelError::Close(e)));
                        },
-                       None => false,
+                       Err(e) => {
+                               // The only error we know how to handle is ChannelError::Close, so we fall over here
+                               // to make sure we don't continue with an inconsistent state.
+                               panic!("unexpected error type from funding_created_signature {:?}", e);
+                       }
                };
 
-               // From here on out, we may not fail!
-               self.target_closing_feerate_sats_per_kw = target_feerate_sats_per_kw;
-               if self.channel_state < ChannelState::FundingSent as u32 {
-                       self.channel_state = ChannelState::ShutdownComplete as u32;
-               } else {
-                       self.channel_state |= ChannelState::LocalShutdownSent as u32;
-               }
-               self.update_time_counter += 1;
+               let holder_commitment_tx = HolderCommitmentTransaction::new(
+                       initial_commitment_tx,
+                       msg.signature,
+                       Vec::new(),
+                       &self.context.get_holder_pubkeys().funding_pubkey,
+                       self.context.counterparty_funding_pubkey()
+               );
 
-               let monitor_update = if update_shutdown_script {
-                       self.latest_monitor_update_id += 1;
-                       let monitor_update = ChannelMonitorUpdate {
-                               update_id: self.latest_monitor_update_id,
-                               updates: vec![ChannelMonitorUpdateStep::ShutdownScript {
-                                       scriptpubkey: self.get_closing_scriptpubkey(),
-                               }],
-                       };
-                       self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
-                       if self.push_blockable_mon_update(monitor_update) {
-                               self.pending_monitor_updates.last().map(|upd| &upd.update)
-                       } else { None }
-               } else { None };
-               let shutdown = msgs::Shutdown {
-                       channel_id: self.channel_id,
-                       scriptpubkey: self.get_closing_scriptpubkey(),
-               };
+               if let Err(_) = self.context.holder_signer.validate_holder_commitment(&holder_commitment_tx, Vec::new()) {
+                       return Err((self, ChannelError::Close("Failed to validate our commitment".to_owned())));
+               }
 
-               // 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
-                       }
-               });
+               // Now that we're past error-generating stuff, update our local state:
 
-               debug_assert!(!self.is_shutdown() || monitor_update.is_none(),
-                       "we can't both complete shutdown and return a monitor update");
+               let funding_redeemscript = self.context.get_funding_redeemscript();
+               let funding_txo_script = funding_redeemscript.to_v0_p2wsh();
+               let obscure_factor = get_commitment_transaction_number_obscure_factor(&self.context.get_holder_pubkeys().payment_point, &self.context.get_counterparty_pubkeys().payment_point, self.context.is_outbound());
+               let shutdown_script = self.context.shutdown_scriptpubkey.clone().map(|script| script.into_inner());
+               let mut monitor_signer = signer_provider.derive_channel_signer(self.context.channel_value_satoshis, self.context.channel_keys_id);
+               monitor_signer.provide_channel_parameters(&self.context.channel_transaction_parameters);
+               let channel_monitor = ChannelMonitor::new(self.context.secp_ctx.clone(), monitor_signer,
+                                                         shutdown_script, self.context.get_holder_selected_contest_delay(),
+                                                         &self.context.destination_script, (funding_txo, funding_txo_script.clone()),
+                                                         &self.context.channel_transaction_parameters,
+                                                         funding_redeemscript.clone(), self.context.channel_value_satoshis,
+                                                         obscure_factor,
+                                                         holder_commitment_tx, best_block, self.context.counterparty_node_id);
 
-               Ok((shutdown, monitor_update, dropped_outbound_htlcs))
-       }
+               channel_monitor.provide_latest_counterparty_commitment_tx(counterparty_initial_commitment_txid, Vec::new(), self.context.cur_counterparty_commitment_transaction_number, self.context.counterparty_cur_commitment_point.unwrap(), logger);
 
-       /// 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, should_broadcast: bool) -> (Option<(OutPoint, ChannelMonitorUpdate)>, Vec<(HTLCSource, PaymentHash, PublicKey, [u8; 32])>) {
-               // Note that we MUST only generate a monitor update that indicates force-closure - we're
-               // called during initialization prior to the chain_monitor in the encompassing ChannelManager
-               // being fully configured in some cases. Thus, its likely any monitor events we generate will
-               // be delayed in being processed! See the docs for `ChannelManagerReadArgs` for more.
-               assert!(self.channel_state != ChannelState::ShutdownComplete as u32);
+               self.context.channel_state = ChannelState::FundingSent as u32;
+               self.context.channel_id = funding_txo.to_channel_id();
+               self.context.cur_counterparty_commitment_transaction_number -= 1;
+               self.context.cur_holder_commitment_transaction_number -= 1;
 
-               // 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());
-               let counterparty_node_id = self.get_counterparty_node_id();
-               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, counterparty_node_id, self.channel_id));
-                               },
-                               _ => {}
-                       }
-               }
-               let monitor_update = if let Some(funding_txo) = self.get_funding_txo() {
-                       // If we haven't yet exchanged funding signatures (ie channel_state < FundingSent),
-                       // returning a channel monitor update here would imply a channel monitor update before
-                       // we even registered the channel monitor to begin with, which is invalid.
-                       // Thus, if we aren't actually at a point where we could conceivably broadcast the
-                       // funding transaction, don't return a funding txo (which prevents providing the
-                       // monitor update to the user, even if we return one).
-                       // See test_duplicate_chan_id and test_pre_lockin_no_chan_closed_update for more.
-                       if self.channel_state & (ChannelState::FundingSent as u32 | ChannelState::ChannelReady as u32 | ChannelState::ShutdownComplete as u32) != 0 {
-                               self.latest_monitor_update_id = CLOSED_CHANNEL_UPDATE_ID;
-                               Some((funding_txo, ChannelMonitorUpdate {
-                                       update_id: self.latest_monitor_update_id,
-                                       updates: vec![ChannelMonitorUpdateStep::ChannelForceClosed { should_broadcast }],
-                               }))
-                       } else { None }
-               } else { None };
+               log_info!(logger, "Generated funding_signed for peer for channel {}", log_bytes!(self.context.channel_id()));
 
-               self.channel_state = ChannelState::ShutdownComplete as u32;
-               self.update_time_counter += 1;
-               (monitor_update, dropped_outbound_htlcs)
-       }
+               // Promote the channel to a full-fledged one now that we have updated the state and have a
+               // `ChannelMonitor`.
+               let mut channel = Channel {
+                       context: self.context,
+               };
+               let channel_id = channel.context.channel_id.clone();
+               let need_channel_ready = channel.check_get_channel_ready(0).is_some();
+               channel.monitor_updating_paused(false, false, need_channel_ready, Vec::new(), Vec::new(), Vec::new());
 
-       pub fn inflight_htlc_sources(&self) -> impl Iterator<Item=(&HTLCSource, &PaymentHash)> {
-               self.holding_cell_htlc_updates.iter()
-                       .flat_map(|htlc_update| {
-                               match htlc_update {
-                                       HTLCUpdateAwaitingACK::AddHTLC { source, payment_hash, .. }
-                                               => Some((source, payment_hash)),
-                                       _ => None,
-                               }
-                       })
-                       .chain(self.pending_outbound_htlcs.iter().map(|htlc| (&htlc.source, &htlc.payment_hash)))
+               Ok((channel, msgs::FundingSigned {
+                       channel_id,
+                       signature,
+                       #[cfg(taproot)]
+                       partial_signature_with_nonce: None,
+               }, channel_monitor))
        }
 }
 
@@ -6349,7 +6561,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for Channel<Signer> {
                // `user_id` used to be a single u64 value. In order to remain backwards compatible with
                // versions prior to 0.0.113, the u128 is serialized as two separate u64 values. We write
                // the low bytes now and the optional high bytes later.
-               let user_id_low = self.user_id as u64;
+               let user_id_low = self.context.user_id as u64;
                user_id_low.write(writer)?;
 
                // Version 1 deserializers expected to read parts of the config object here. Version 2
@@ -6357,14 +6569,14 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for Channel<Signer> {
                // `minimum_depth` we simply write dummy values here.
                writer.write_all(&[0; 8])?;
 
-               self.channel_id.write(writer)?;
-               (self.channel_state | ChannelState::PeerDisconnected as u32).write(writer)?;
-               self.channel_value_satoshis.write(writer)?;
+               self.context.channel_id.write(writer)?;
+               (self.context.channel_state | ChannelState::PeerDisconnected as u32).write(writer)?;
+               self.context.channel_value_satoshis.write(writer)?;
 
-               self.latest_monitor_update_id.write(writer)?;
+               self.context.latest_monitor_update_id.write(writer)?;
 
                let mut key_data = VecWriter(Vec::new());
-               self.holder_signer.write(&mut key_data)?;
+               self.context.holder_signer.write(&mut key_data)?;
                assert!(key_data.0.len() < core::usize::MAX);
                assert!(key_data.0.len() < core::u32::MAX as usize);
                (key_data.0.len() as u32).write(writer)?;
@@ -6372,24 +6584,24 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for Channel<Signer> {
 
                // Write out the old serialization for shutdown_pubkey for backwards compatibility, if
                // deserialized from that format.
-               match self.shutdown_scriptpubkey.as_ref().and_then(|script| script.as_legacy_pubkey()) {
+               match self.context.shutdown_scriptpubkey.as_ref().and_then(|script| script.as_legacy_pubkey()) {
                        Some(shutdown_pubkey) => shutdown_pubkey.write(writer)?,
                        None => [0u8; PUBLIC_KEY_SIZE].write(writer)?,
                }
-               self.destination_script.write(writer)?;
+               self.context.destination_script.write(writer)?;
 
-               self.cur_holder_commitment_transaction_number.write(writer)?;
-               self.cur_counterparty_commitment_transaction_number.write(writer)?;
-               self.value_to_self_msat.write(writer)?;
+               self.context.cur_holder_commitment_transaction_number.write(writer)?;
+               self.context.cur_counterparty_commitment_transaction_number.write(writer)?;
+               self.context.value_to_self_msat.write(writer)?;
 
                let mut dropped_inbound_htlcs = 0;
-               for htlc in self.pending_inbound_htlcs.iter() {
+               for htlc in self.context.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() {
+               (self.context.pending_inbound_htlcs.len() as u64 - dropped_inbound_htlcs).write(writer)?;
+               for htlc in self.context.pending_inbound_htlcs.iter() {
                        if let &InboundHTLCState::RemoteAnnounced(_) = &htlc.state {
                                continue; // Drop
                        }
@@ -6418,9 +6630,10 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for Channel<Signer> {
                }
 
                let mut preimages: Vec<&Option<PaymentPreimage>> = vec![];
+               let mut pending_outbound_skimmed_fees: Vec<Option<u64>> = Vec::new();
 
-               (self.pending_outbound_htlcs.len() as u64).write(writer)?;
-               for htlc in self.pending_outbound_htlcs.iter() {
+               (self.context.pending_outbound_htlcs.len() as u64).write(writer)?;
+               for (idx, htlc) in self.context.pending_outbound_htlcs.iter().enumerate() {
                        htlc.htlc_id.write(writer)?;
                        htlc.amount_msat.write(writer)?;
                        htlc.cltv_expiry.write(writer)?;
@@ -6456,18 +6669,37 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for Channel<Signer> {
                                        reason.write(writer)?;
                                }
                        }
+                       if let Some(skimmed_fee) = htlc.skimmed_fee_msat {
+                               if pending_outbound_skimmed_fees.is_empty() {
+                                       for _ in 0..idx { pending_outbound_skimmed_fees.push(None); }
+                               }
+                               pending_outbound_skimmed_fees.push(Some(skimmed_fee));
+                       } else if !pending_outbound_skimmed_fees.is_empty() {
+                               pending_outbound_skimmed_fees.push(None);
+                       }
                }
 
-               (self.holding_cell_htlc_updates.len() as u64).write(writer)?;
-               for update in self.holding_cell_htlc_updates.iter() {
+               let mut holding_cell_skimmed_fees: Vec<Option<u64>> = Vec::new();
+               (self.context.holding_cell_htlc_updates.len() as u64).write(writer)?;
+               for (idx, update) in self.context.holding_cell_htlc_updates.iter().enumerate() {
                        match update {
-                               &HTLCUpdateAwaitingACK::AddHTLC { ref amount_msat, ref cltv_expiry, ref payment_hash, ref source, ref onion_routing_packet } => {
+                               &HTLCUpdateAwaitingACK::AddHTLC {
+                                       ref amount_msat, ref cltv_expiry, ref payment_hash, ref source, ref onion_routing_packet,
+                                       skimmed_fee_msat,
+                               } => {
                                        0u8.write(writer)?;
                                        amount_msat.write(writer)?;
                                        cltv_expiry.write(writer)?;
                                        payment_hash.write(writer)?;
                                        source.write(writer)?;
                                        onion_routing_packet.write(writer)?;
+
+                                       if let Some(skimmed_fee) = skimmed_fee_msat {
+                                               if holding_cell_skimmed_fees.is_empty() {
+                                                       for _ in 0..idx { holding_cell_skimmed_fees.push(None); }
+                                               }
+                                               holding_cell_skimmed_fees.push(Some(skimmed_fee));
+                                       } else if !holding_cell_skimmed_fees.is_empty() { holding_cell_skimmed_fees.push(None); }
                                },
                                &HTLCUpdateAwaitingACK::ClaimHTLC { ref payment_preimage, ref htlc_id } => {
                                        1u8.write(writer)?;
@@ -6482,43 +6714,43 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for Channel<Signer> {
                        }
                }
 
-               match self.resend_order {
+               match self.context.resend_order {
                        RAACommitmentOrder::CommitmentFirst => 0u8.write(writer)?,
                        RAACommitmentOrder::RevokeAndACKFirst => 1u8.write(writer)?,
                }
 
-               self.monitor_pending_channel_ready.write(writer)?;
-               self.monitor_pending_revoke_and_ack.write(writer)?;
-               self.monitor_pending_commitment_signed.write(writer)?;
+               self.context.monitor_pending_channel_ready.write(writer)?;
+               self.context.monitor_pending_revoke_and_ack.write(writer)?;
+               self.context.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() {
+               (self.context.monitor_pending_forwards.len() as u64).write(writer)?;
+               for &(ref pending_forward, ref htlc_id) in self.context.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() {
+               (self.context.monitor_pending_failures.len() as u64).write(writer)?;
+               for &(ref htlc_source, ref payment_hash, ref fail_reason) in self.context.monitor_pending_failures.iter() {
                        htlc_source.write(writer)?;
                        payment_hash.write(writer)?;
                        fail_reason.write(writer)?;
                }
 
-               if self.is_outbound() {
-                       self.pending_update_fee.map(|(a, _)| a).write(writer)?;
-               } else if let Some((feerate, FeeUpdateState::AwaitingRemoteRevokeToAnnounce)) = self.pending_update_fee {
+               if self.context.is_outbound() {
+                       self.context.pending_update_fee.map(|(a, _)| a).write(writer)?;
+               } else if let Some((feerate, FeeUpdateState::AwaitingRemoteRevokeToAnnounce)) = self.context.pending_update_fee {
                        Some(feerate).write(writer)?;
                } else {
                        // As for inbound HTLCs, if the update was only announced and never committed in a
                        // commitment_signed, drop it.
                        None::<u32>.write(writer)?;
                }
-               self.holding_cell_update_fee.write(writer)?;
+               self.context.holding_cell_update_fee.write(writer)?;
 
-               self.next_holder_htlc_id.write(writer)?;
-               (self.next_counterparty_htlc_id - dropped_inbound_htlcs).write(writer)?;
-               self.update_time_counter.write(writer)?;
-               self.feerate_per_kw.write(writer)?;
+               self.context.next_holder_htlc_id.write(writer)?;
+               (self.context.next_counterparty_htlc_id - dropped_inbound_htlcs).write(writer)?;
+               self.context.update_time_counter.write(writer)?;
+               self.context.feerate_per_kw.write(writer)?;
 
                // Versions prior to 0.0.100 expected to read the fields of `last_sent_closing_fee` here,
                // however we are supposed to restart shutdown fee negotiation on reconnect (and wipe
@@ -6526,25 +6758,25 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for Channel<Signer> {
                // consider the stale state on reload.
                0u8.write(writer)?;
 
-               self.funding_tx_confirmed_in.write(writer)?;
-               self.funding_tx_confirmation_height.write(writer)?;
-               self.short_channel_id.write(writer)?;
+               self.context.funding_tx_confirmed_in.write(writer)?;
+               self.context.funding_tx_confirmation_height.write(writer)?;
+               self.context.short_channel_id.write(writer)?;
 
-               self.counterparty_dust_limit_satoshis.write(writer)?;
-               self.holder_dust_limit_satoshis.write(writer)?;
-               self.counterparty_max_htlc_value_in_flight_msat.write(writer)?;
+               self.context.counterparty_dust_limit_satoshis.write(writer)?;
+               self.context.holder_dust_limit_satoshis.write(writer)?;
+               self.context.counterparty_max_htlc_value_in_flight_msat.write(writer)?;
 
                // Note that this field is ignored by 0.0.99+ as the TLV Optional variant is used instead.
-               self.counterparty_selected_channel_reserve_satoshis.unwrap_or(0).write(writer)?;
+               self.context.counterparty_selected_channel_reserve_satoshis.unwrap_or(0).write(writer)?;
 
-               self.counterparty_htlc_minimum_msat.write(writer)?;
-               self.holder_htlc_minimum_msat.write(writer)?;
-               self.counterparty_max_accepted_htlcs.write(writer)?;
+               self.context.counterparty_htlc_minimum_msat.write(writer)?;
+               self.context.holder_htlc_minimum_msat.write(writer)?;
+               self.context.counterparty_max_accepted_htlcs.write(writer)?;
 
                // Note that this field is ignored by 0.0.99+ as the TLV Optional variant is used instead.
-               self.minimum_depth.unwrap_or(0).write(writer)?;
+               self.context.minimum_depth.unwrap_or(0).write(writer)?;
 
-               match &self.counterparty_forwarding_info {
+               match &self.context.counterparty_forwarding_info {
                        Some(info) => {
                                1u8.write(writer)?;
                                info.fee_base_msat.write(writer)?;
@@ -6554,23 +6786,23 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for Channel<Signer> {
                        None => 0u8.write(writer)?
                }
 
-               self.channel_transaction_parameters.write(writer)?;
-               self.funding_transaction.write(writer)?;
+               self.context.channel_transaction_parameters.write(writer)?;
+               self.context.funding_transaction.write(writer)?;
 
-               self.counterparty_cur_commitment_point.write(writer)?;
-               self.counterparty_prev_commitment_point.write(writer)?;
-               self.counterparty_node_id.write(writer)?;
+               self.context.counterparty_cur_commitment_point.write(writer)?;
+               self.context.counterparty_prev_commitment_point.write(writer)?;
+               self.context.counterparty_node_id.write(writer)?;
 
-               self.counterparty_shutdown_scriptpubkey.write(writer)?;
+               self.context.counterparty_shutdown_scriptpubkey.write(writer)?;
 
-               self.commitment_secrets.write(writer)?;
+               self.context.commitment_secrets.write(writer)?;
 
-               self.channel_update_status.write(writer)?;
+               self.context.channel_update_status.write(writer)?;
 
                #[cfg(any(test, fuzzing))]
-               (self.historical_inbound_htlc_fulfills.len() as u64).write(writer)?;
+               (self.context.historical_inbound_htlc_fulfills.len() as u64).write(writer)?;
                #[cfg(any(test, fuzzing))]
-               for htlc in self.historical_inbound_htlc_fulfills.iter() {
+               for htlc in self.context.historical_inbound_htlc_fulfills.iter() {
                        htlc.write(writer)?;
                }
 
@@ -6578,62 +6810,64 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for Channel<Signer> {
                // older clients fail to deserialize this channel at all. If the type is
                // only-static-remote-key, we simply consider it "default" and don't write the channel type
                // out at all.
-               let chan_type = if self.channel_type != ChannelTypeFeatures::only_static_remote_key() {
-                       Some(&self.channel_type) } else { None };
+               let chan_type = if self.context.channel_type != ChannelTypeFeatures::only_static_remote_key() {
+                       Some(&self.context.channel_type) } else { None };
 
                // The same logic applies for `holder_selected_channel_reserve_satoshis` values other than
                // the default, and when `holder_max_htlc_value_in_flight_msat` is configured to be set to
                // a different percentage of the channel value then 10%, which older versions of LDK used
                // to set it to before the percentage was made configurable.
                let serialized_holder_selected_reserve =
-                       if self.holder_selected_channel_reserve_satoshis != Self::get_legacy_default_holder_selected_channel_reserve_satoshis(self.channel_value_satoshis)
-                       { Some(self.holder_selected_channel_reserve_satoshis) } else { None };
+                       if self.context.holder_selected_channel_reserve_satoshis != get_legacy_default_holder_selected_channel_reserve_satoshis(self.context.channel_value_satoshis)
+                       { Some(self.context.holder_selected_channel_reserve_satoshis) } else { None };
 
                let mut old_max_in_flight_percent_config = UserConfig::default().channel_handshake_config;
                old_max_in_flight_percent_config.max_inbound_htlc_value_in_flight_percent_of_channel = MAX_IN_FLIGHT_PERCENT_LEGACY;
                let serialized_holder_htlc_max_in_flight =
-                       if self.holder_max_htlc_value_in_flight_msat != Self::get_holder_max_htlc_value_in_flight_msat(self.channel_value_satoshis, &old_max_in_flight_percent_config)
-                       { Some(self.holder_max_htlc_value_in_flight_msat) } else { None };
+                       if self.context.holder_max_htlc_value_in_flight_msat != get_holder_max_htlc_value_in_flight_msat(self.context.channel_value_satoshis, &old_max_in_flight_percent_config)
+                       { Some(self.context.holder_max_htlc_value_in_flight_msat) } else { None };
 
-               let channel_pending_event_emitted = Some(self.channel_pending_event_emitted);
-               let channel_ready_event_emitted = Some(self.channel_ready_event_emitted);
+               let channel_pending_event_emitted = Some(self.context.channel_pending_event_emitted);
+               let channel_ready_event_emitted = Some(self.context.channel_ready_event_emitted);
 
                // `user_id` used to be a single u64 value. In order to remain backwards compatible with
                // versions prior to 0.0.113, the u128 is serialized as two separate u64 values. Therefore,
                // we write the high bytes as an option here.
-               let user_id_high_opt = Some((self.user_id >> 64) as u64);
+               let user_id_high_opt = Some((self.context.user_id >> 64) as u64);
 
-               let holder_max_accepted_htlcs = if self.holder_max_accepted_htlcs == DEFAULT_MAX_HTLCS { None } else { Some(self.holder_max_accepted_htlcs) };
+               let holder_max_accepted_htlcs = if self.context.holder_max_accepted_htlcs == DEFAULT_MAX_HTLCS { None } else { Some(self.context.holder_max_accepted_htlcs) };
 
                write_tlv_fields!(writer, {
-                       (0, self.announcement_sigs, option),
+                       (0, self.context.announcement_sigs, option),
                        // minimum_depth and counterparty_selected_channel_reserve_satoshis used to have a
                        // default value instead of being Option<>al. Thus, to maintain compatibility we write
                        // them twice, once with their original default values above, and once as an option
                        // here. On the read side, old versions will simply ignore the odd-type entries here,
                        // and new versions map the default values to None and allow the TLV entries here to
                        // override that.
-                       (1, self.minimum_depth, option),
+                       (1, self.context.minimum_depth, option),
                        (2, chan_type, option),
-                       (3, self.counterparty_selected_channel_reserve_satoshis, option),
+                       (3, self.context.counterparty_selected_channel_reserve_satoshis, option),
                        (4, serialized_holder_selected_reserve, option),
-                       (5, self.config, required),
+                       (5, self.context.config, required),
                        (6, serialized_holder_htlc_max_in_flight, option),
-                       (7, self.shutdown_scriptpubkey, option),
-                       (9, self.target_closing_feerate_sats_per_kw, option),
-                       (11, self.monitor_pending_finalized_fulfills, vec_type),
-                       (13, self.channel_creation_height, required),
+                       (7, self.context.shutdown_scriptpubkey, option),
+                       (8, self.context.blocked_monitor_updates, vec_type),
+                       (9, self.context.target_closing_feerate_sats_per_kw, option),
+                       (11, self.context.monitor_pending_finalized_fulfills, vec_type),
+                       (13, self.context.channel_creation_height, required),
                        (15, preimages, vec_type),
-                       (17, self.announcement_sigs_state, required),
-                       (19, self.latest_inbound_scid_alias, option),
-                       (21, self.outbound_scid_alias, required),
+                       (17, self.context.announcement_sigs_state, required),
+                       (19, self.context.latest_inbound_scid_alias, option),
+                       (21, self.context.outbound_scid_alias, required),
                        (23, channel_ready_event_emitted, option),
                        (25, user_id_high_opt, option),
-                       (27, self.channel_keys_id, required),
+                       (27, self.context.channel_keys_id, required),
                        (28, holder_max_accepted_htlcs, option),
-                       (29, self.temporary_channel_id, option),
+                       (29, self.context.temporary_channel_id, option),
                        (31, channel_pending_event_emitted, option),
-                       (33, self.pending_monitor_updates, vec_type),
+                       (35, pending_outbound_skimmed_fees, optional_vec),
+                       (37, holding_cell_skimmed_fees, optional_vec),
                });
 
                Ok(())
@@ -6744,6 +6978,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
                                        },
                                        _ => return Err(DecodeError::InvalidValue),
                                },
+                               skimmed_fee_msat: None,
                        });
                }
 
@@ -6757,6 +6992,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
                                        payment_hash: Readable::read(reader)?,
                                        source: Readable::read(reader)?,
                                        onion_routing_packet: Readable::read(reader)?,
+                                       skimmed_fee_msat: None,
                                },
                                1 => HTLCUpdateAwaitingACK::ClaimHTLC {
                                        payment_preimage: Readable::read(reader)?,
@@ -6853,7 +7089,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
                        _ => return Err(DecodeError::InvalidValue),
                };
 
-               let channel_parameters: ChannelTransactionParameters = Readable::read(reader)?;
+               let mut channel_parameters: ChannelTransactionParameters = Readable::read(reader)?;
                let funding_transaction = Readable::read(reader)?;
 
                let counterparty_cur_commitment_point = Readable::read(reader)?;
@@ -6889,8 +7125,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
                let mut announcement_sigs = None;
                let mut target_closing_feerate_sats_per_kw = None;
                let mut monitor_pending_finalized_fulfills = Some(Vec::new());
-               let mut holder_selected_channel_reserve_satoshis = Some(Self::get_legacy_default_holder_selected_channel_reserve_satoshis(channel_value_satoshis));
-               let mut holder_max_htlc_value_in_flight_msat = Some(Self::get_holder_max_htlc_value_in_flight_msat(channel_value_satoshis, &UserConfig::default().channel_handshake_config));
+               let mut holder_selected_channel_reserve_satoshis = Some(get_legacy_default_holder_selected_channel_reserve_satoshis(channel_value_satoshis));
+               let mut holder_max_htlc_value_in_flight_msat = Some(get_holder_max_htlc_value_in_flight_msat(channel_value_satoshis, &UserConfig::default().channel_handshake_config));
                // Prior to supporting channel type negotiation, all of our channels were static_remotekey
                // only, so we default to that if none was written.
                let mut channel_type = Some(ChannelTypeFeatures::only_static_remote_key());
@@ -6910,7 +7146,10 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
                let mut temporary_channel_id: Option<[u8; 32]> = None;
                let mut holder_max_accepted_htlcs: Option<u16> = None;
 
-               let mut pending_monitor_updates = Some(Vec::new());
+               let mut blocked_monitor_updates = Some(Vec::new());
+
+               let mut pending_outbound_skimmed_fees_opt: Option<Vec<Option<u64>>> = None;
+               let mut holding_cell_skimmed_fees_opt: Option<Vec<Option<u64>>> = None;
 
                read_tlv_fields!(reader, {
                        (0, announcement_sigs, option),
@@ -6921,6 +7160,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
                        (5, config, option), // Note that if none is provided we will *not* overwrite the existing one.
                        (6, holder_max_htlc_value_in_flight_msat, option),
                        (7, shutdown_scriptpubkey, option),
+                       (8, blocked_monitor_updates, vec_type),
                        (9, target_closing_feerate_sats_per_kw, option),
                        (11, monitor_pending_finalized_fulfills, vec_type),
                        (13, channel_creation_height, option),
@@ -6934,7 +7174,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
                        (28, holder_max_accepted_htlcs, option),
                        (29, temporary_channel_id, option),
                        (31, channel_pending_event_emitted, option),
-                       (33, pending_monitor_updates, vec_type),
+                       (35, pending_outbound_skimmed_fees_opt, optional_vec),
+                       (37, holding_cell_skimmed_fees_opt, optional_vec),
                });
 
                let (channel_keys_id, holder_signer) = if let Some(channel_keys_id) = channel_keys_id {
@@ -6979,6 +7220,10 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
                        return Err(DecodeError::UnknownRequiredFeature);
                }
 
+               // ChannelTransactionParameters may have had an empty features set upon deserialization.
+               // To account for that, we're proactively setting/overriding the field here.
+               channel_parameters.channel_type_features = chan_features.clone();
+
                let mut secp_ctx = Secp256k1::new();
                secp_ctx.seeded_randomize(&entropy_source.get_secure_random_bytes());
 
@@ -6989,122 +7234,144 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
 
                let holder_max_accepted_htlcs = holder_max_accepted_htlcs.unwrap_or(DEFAULT_MAX_HTLCS);
 
+               if let Some(skimmed_fees) = pending_outbound_skimmed_fees_opt {
+                       let mut iter = skimmed_fees.into_iter();
+                       for htlc in pending_outbound_htlcs.iter_mut() {
+                               htlc.skimmed_fee_msat = iter.next().ok_or(DecodeError::InvalidValue)?;
+                       }
+                       // We expect all skimmed fees to be consumed above
+                       if iter.next().is_some() { return Err(DecodeError::InvalidValue) }
+               }
+               if let Some(skimmed_fees) = holding_cell_skimmed_fees_opt {
+                       let mut iter = skimmed_fees.into_iter();
+                       for htlc in holding_cell_htlc_updates.iter_mut() {
+                               if let HTLCUpdateAwaitingACK::AddHTLC { ref mut skimmed_fee_msat, .. } = htlc {
+                                       *skimmed_fee_msat = iter.next().ok_or(DecodeError::InvalidValue)?;
+                               }
+                       }
+                       // We expect all skimmed fees to be consumed above
+                       if iter.next().is_some() { return Err(DecodeError::InvalidValue) }
+               }
+
                Ok(Channel {
-                       user_id,
+                       context: ChannelContext {
+                               user_id,
 
-                       config: config.unwrap(),
+                               config: config.unwrap(),
 
-                       prev_config: None,
+                               prev_config: None,
 
-                       // Note that we don't care about serializing handshake limits as we only ever serialize
-                       // channel data after the handshake has completed.
-                       inbound_handshake_limits_override: None,
+                               // Note that we don't care about serializing handshake limits as we only ever serialize
+                               // channel data after the handshake has completed.
+                               inbound_handshake_limits_override: None,
 
-                       channel_id,
-                       temporary_channel_id,
-                       channel_state,
-                       announcement_sigs_state: announcement_sigs_state.unwrap(),
-                       secp_ctx,
-                       channel_value_satoshis,
-
-                       latest_monitor_update_id,
-
-                       holder_signer,
-                       shutdown_scriptpubkey,
-                       destination_script,
-
-                       cur_holder_commitment_transaction_number,
-                       cur_counterparty_commitment_transaction_number,
-                       value_to_self_msat,
-
-                       holder_max_accepted_htlcs,
-                       pending_inbound_htlcs,
-                       pending_outbound_htlcs,
-                       holding_cell_htlc_updates,
-
-                       resend_order,
-
-                       monitor_pending_channel_ready,
-                       monitor_pending_revoke_and_ack,
-                       monitor_pending_commitment_signed,
-                       monitor_pending_forwards,
-                       monitor_pending_failures,
-                       monitor_pending_finalized_fulfills: monitor_pending_finalized_fulfills.unwrap(),
-
-                       pending_update_fee,
-                       holding_cell_update_fee,
-                       next_holder_htlc_id,
-                       next_counterparty_htlc_id,
-                       update_time_counter,
-                       feerate_per_kw,
+                               channel_id,
+                               temporary_channel_id,
+                               channel_state,
+                               announcement_sigs_state: announcement_sigs_state.unwrap(),
+                               secp_ctx,
+                               channel_value_satoshis,
 
-                       #[cfg(debug_assertions)]
-                       holder_max_commitment_tx_output: Mutex::new((0, 0)),
-                       #[cfg(debug_assertions)]
-                       counterparty_max_commitment_tx_output: Mutex::new((0, 0)),
+                               latest_monitor_update_id,
 
-                       last_sent_closing_fee: None,
-                       pending_counterparty_closing_signed: None,
-                       closing_fee_limits: None,
-                       target_closing_feerate_sats_per_kw,
+                               holder_signer,
+                               shutdown_scriptpubkey,
+                               destination_script,
 
-                       inbound_awaiting_accept: false,
+                               cur_holder_commitment_transaction_number,
+                               cur_counterparty_commitment_transaction_number,
+                               value_to_self_msat,
 
-                       funding_tx_confirmed_in,
-                       funding_tx_confirmation_height,
-                       short_channel_id,
-                       channel_creation_height: channel_creation_height.unwrap(),
+                               holder_max_accepted_htlcs,
+                               pending_inbound_htlcs,
+                               pending_outbound_htlcs,
+                               holding_cell_htlc_updates,
 
-                       counterparty_dust_limit_satoshis,
-                       holder_dust_limit_satoshis,
-                       counterparty_max_htlc_value_in_flight_msat,
-                       holder_max_htlc_value_in_flight_msat: holder_max_htlc_value_in_flight_msat.unwrap(),
-                       counterparty_selected_channel_reserve_satoshis,
-                       holder_selected_channel_reserve_satoshis: holder_selected_channel_reserve_satoshis.unwrap(),
-                       counterparty_htlc_minimum_msat,
-                       holder_htlc_minimum_msat,
-                       counterparty_max_accepted_htlcs,
-                       minimum_depth,
+                               resend_order,
 
-                       counterparty_forwarding_info,
+                               monitor_pending_channel_ready,
+                               monitor_pending_revoke_and_ack,
+                               monitor_pending_commitment_signed,
+                               monitor_pending_forwards,
+                               monitor_pending_failures,
+                               monitor_pending_finalized_fulfills: monitor_pending_finalized_fulfills.unwrap(),
 
-                       channel_transaction_parameters: channel_parameters,
-                       funding_transaction,
+                               pending_update_fee,
+                               holding_cell_update_fee,
+                               next_holder_htlc_id,
+                               next_counterparty_htlc_id,
+                               update_time_counter,
+                               feerate_per_kw,
 
-                       counterparty_cur_commitment_point,
-                       counterparty_prev_commitment_point,
-                       counterparty_node_id,
+                               #[cfg(debug_assertions)]
+                               holder_max_commitment_tx_output: Mutex::new((0, 0)),
+                               #[cfg(debug_assertions)]
+                               counterparty_max_commitment_tx_output: Mutex::new((0, 0)),
 
-                       counterparty_shutdown_scriptpubkey,
+                               last_sent_closing_fee: None,
+                               pending_counterparty_closing_signed: None,
+                               closing_fee_limits: None,
+                               target_closing_feerate_sats_per_kw,
 
-                       commitment_secrets,
+                               inbound_awaiting_accept: false,
 
-                       channel_update_status,
-                       closing_signed_in_flight: false,
+                               funding_tx_confirmed_in,
+                               funding_tx_confirmation_height,
+                               short_channel_id,
+                               channel_creation_height: channel_creation_height.unwrap(),
 
-                       announcement_sigs,
+                               counterparty_dust_limit_satoshis,
+                               holder_dust_limit_satoshis,
+                               counterparty_max_htlc_value_in_flight_msat,
+                               holder_max_htlc_value_in_flight_msat: holder_max_htlc_value_in_flight_msat.unwrap(),
+                               counterparty_selected_channel_reserve_satoshis,
+                               holder_selected_channel_reserve_satoshis: holder_selected_channel_reserve_satoshis.unwrap(),
+                               counterparty_htlc_minimum_msat,
+                               holder_htlc_minimum_msat,
+                               counterparty_max_accepted_htlcs,
+                               minimum_depth,
 
-                       #[cfg(any(test, fuzzing))]
-                       next_local_commitment_tx_fee_info_cached: Mutex::new(None),
-                       #[cfg(any(test, fuzzing))]
-                       next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
+                               counterparty_forwarding_info,
 
-                       workaround_lnd_bug_4006: None,
+                               channel_transaction_parameters: channel_parameters,
+                               funding_transaction,
 
-                       latest_inbound_scid_alias,
-                       // Later in the ChannelManager deserialization phase we scan for channels and assign scid aliases if its missing
-                       outbound_scid_alias: outbound_scid_alias.unwrap_or(0),
+                               counterparty_cur_commitment_point,
+                               counterparty_prev_commitment_point,
+                               counterparty_node_id,
 
-                       channel_pending_event_emitted: channel_pending_event_emitted.unwrap_or(true),
-                       channel_ready_event_emitted: channel_ready_event_emitted.unwrap_or(true),
+                               counterparty_shutdown_scriptpubkey,
 
-                       #[cfg(any(test, fuzzing))]
-                       historical_inbound_htlc_fulfills,
+                               commitment_secrets,
 
-                       channel_type: channel_type.unwrap(),
-                       channel_keys_id,
+                               channel_update_status,
+                               closing_signed_in_flight: false,
 
-                       pending_monitor_updates: pending_monitor_updates.unwrap(),
+                               announcement_sigs,
+
+                               #[cfg(any(test, fuzzing))]
+                               next_local_commitment_tx_fee_info_cached: Mutex::new(None),
+                               #[cfg(any(test, fuzzing))]
+                               next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
+
+                               workaround_lnd_bug_4006: None,
+                               sent_message_awaiting_response: None,
+
+                               latest_inbound_scid_alias,
+                               // Later in the ChannelManager deserialization phase we scan for channels and assign scid aliases if its missing
+                               outbound_scid_alias: outbound_scid_alias.unwrap_or(0),
+
+                               channel_pending_event_emitted: channel_pending_event_emitted.unwrap_or(true),
+                               channel_ready_event_emitted: channel_ready_event_emitted.unwrap_or(true),
+
+                               #[cfg(any(test, fuzzing))]
+                               historical_inbound_htlc_fulfills,
+
+                               channel_type: channel_type.unwrap(),
+                               channel_keys_id,
+
+                               blocked_monitor_updates: blocked_monitor_updates.unwrap(),
+                       }
                })
        }
 }
@@ -7120,9 +7387,8 @@ mod tests {
        use hex;
        use crate::ln::PaymentHash;
        use crate::ln::channelmanager::{self, HTLCSource, PaymentId};
-       #[cfg(anchors)]
        use crate::ln::channel::InitFeatures;
-       use crate::ln::channel::{Channel, InboundHTLCOutput, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator};
+       use crate::ln::channel::{Channel, InboundHTLCOutput, OutboundV1Channel, InboundV1Channel, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator, commit_tx_fee_msat};
        use crate::ln::channel::{MAX_FUNDING_SATOSHIS_NO_WUMBO, TOTAL_BITCOIN_SUPPLY_SATOSHIS, MIN_THEIR_CHAN_RESERVE_SATOSHIS};
        use crate::ln::features::ChannelTypeFeatures;
        use crate::ln::msgs::{ChannelUpdate, DecodeError, UnsignedChannelUpdate, MAX_VALUE_MSAT};
@@ -7210,7 +7476,7 @@ mod tests {
                }
        }
 
-       #[cfg(not(feature = "grind_signatures"))]
+       #[cfg(all(feature = "_test_vectors", not(feature = "grind_signatures")))]
        fn public_from_secret_hex(secp_ctx: &Secp256k1<bitcoin::secp256k1::All>, hex: &str) -> PublicKey {
                PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode(hex).unwrap()[..]).unwrap())
        }
@@ -7231,7 +7497,7 @@ mod tests {
                let secp_ctx = Secp256k1::new();
                let node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
                let config = UserConfig::default();
-               match Channel::<EnforcingSigner>::new_outbound(&LowerBoundedFeeEstimator::new(&TestFeeEstimator { fee_est: 253 }), &&keys_provider, &&keys_provider, node_id, &features, 10000000, 100000, 42, &config, 0, 42) {
+               match OutboundV1Channel::<EnforcingSigner>::new(&LowerBoundedFeeEstimator::new(&TestFeeEstimator { fee_est: 253 }), &&keys_provider, &&keys_provider, node_id, &features, 10000000, 100000, 42, &config, 0, 42) {
                        Err(APIError::IncompatibleShutdownScript { script }) => {
                                assert_eq!(script.into_inner(), non_v0_segwit_shutdown_script.into_inner());
                        },
@@ -7254,7 +7520,7 @@ mod tests {
 
                let node_a_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
                let config = UserConfig::default();
-               let node_a_chan = Channel::<EnforcingSigner>::new_outbound(&bounded_fee_estimator, &&keys_provider, &&keys_provider, node_a_node_id, &channelmanager::provided_init_features(&config), 10000000, 100000, 42, &config, 0, 42).unwrap();
+               let node_a_chan = OutboundV1Channel::<EnforcingSigner>::new(&bounded_fee_estimator, &&keys_provider, &&keys_provider, node_a_node_id, &channelmanager::provided_init_features(&config), 10000000, 100000, 42, &config, 0, 42).unwrap();
 
                // Now change the fee so we can check that the fee in the open_channel message is the
                // same as the old fee.
@@ -7273,6 +7539,7 @@ mod tests {
                let network = Network::Testnet;
                let keys_provider = test_utils::TestKeysInterface::new(&seed, network);
                let logger = test_utils::TestLogger::new();
+               let best_block = BestBlock::from_network(network);
 
                // Go through the flow of opening a channel between two nodes, making sure
                // they have different dust limits.
@@ -7280,23 +7547,35 @@ mod tests {
                // Create Node A's channel pointing to Node B's pubkey
                let node_b_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
                let config = UserConfig::default();
-               let mut node_a_chan = Channel::<EnforcingSigner>::new_outbound(&feeest, &&keys_provider, &&keys_provider, node_b_node_id, &channelmanager::provided_init_features(&config), 10000000, 100000, 42, &config, 0, 42).unwrap();
+               let mut node_a_chan = OutboundV1Channel::<EnforcingSigner>::new(&feeest, &&keys_provider, &&keys_provider, node_b_node_id, &channelmanager::provided_init_features(&config), 10000000, 100000, 42, &config, 0, 42).unwrap();
 
                // Create Node B's channel by receiving Node A's open_channel message
                // Make sure A's dust limit is as we expect.
                let open_channel_msg = node_a_chan.get_open_channel(genesis_block(network).header.block_hash());
                let node_b_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[7; 32]).unwrap());
-               let mut node_b_chan = Channel::<EnforcingSigner>::new_from_req(&feeest, &&keys_provider, &&keys_provider, node_b_node_id, &channelmanager::provided_channel_type_features(&config), &channelmanager::provided_init_features(&config), &open_channel_msg, 7, &config, 0, &&logger, 42).unwrap();
+               let mut node_b_chan = InboundV1Channel::<EnforcingSigner>::new(&feeest, &&keys_provider, &&keys_provider, node_b_node_id, &channelmanager::provided_channel_type_features(&config), &channelmanager::provided_init_features(&config), &open_channel_msg, 7, &config, 0, &&logger, 42).unwrap();
 
                // Node B --> Node A: accept channel, explicitly setting B's dust limit.
                let mut accept_channel_msg = node_b_chan.accept_inbound_channel(0);
                accept_channel_msg.dust_limit_satoshis = 546;
                node_a_chan.accept_channel(&accept_channel_msg, &config.channel_handshake_limits, &channelmanager::provided_init_features(&config)).unwrap();
-               node_a_chan.holder_dust_limit_satoshis = 1560;
+               node_a_chan.context.holder_dust_limit_satoshis = 1560;
+
+               // Node A --> Node B: funding created
+               let output_script = node_a_chan.context.get_funding_redeemscript();
+               let tx = Transaction { version: 1, lock_time: PackedLockTime::ZERO, input: Vec::new(), output: vec![TxOut {
+                       value: 10000000, script_pubkey: output_script.clone(),
+               }]};
+               let funding_outpoint = OutPoint{ txid: tx.txid(), index: 0 };
+               let (mut node_a_chan, funding_created_msg) = node_a_chan.get_outbound_funding_created(tx.clone(), funding_outpoint, &&logger).map_err(|_| ()).unwrap();
+               let (_, funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&keys_provider, &&logger).map_err(|_| ()).unwrap();
+
+               // Node B --> Node A: funding signed
+               let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, &&logger).unwrap();
 
                // Put some inbound and outbound HTLCs in A's channel.
                let htlc_amount_msat = 11_092_000; // put an amount below A's effective dust limit but above B's.
-               node_a_chan.pending_inbound_htlcs.push(InboundHTLCOutput {
+               node_a_chan.context.pending_inbound_htlcs.push(InboundHTLCOutput {
                        htlc_id: 0,
                        amount_msat: htlc_amount_msat,
                        payment_hash: PaymentHash(Sha256::hash(&[42; 32]).into_inner()),
@@ -7304,7 +7583,7 @@ mod tests {
                        state: InboundHTLCState::Committed,
                });
 
-               node_a_chan.pending_outbound_htlcs.push(OutboundHTLCOutput {
+               node_a_chan.context.pending_outbound_htlcs.push(OutboundHTLCOutput {
                        htlc_id: 1,
                        amount_msat: htlc_amount_msat, // put an amount below A's dust amount but above B's.
                        payment_hash: PaymentHash(Sha256::hash(&[43; 32]).into_inner()),
@@ -7315,22 +7594,23 @@ mod tests {
                                session_priv: SecretKey::from_slice(&hex::decode("0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap()[..]).unwrap(),
                                first_hop_htlc_msat: 548,
                                payment_id: PaymentId([42; 32]),
-                       }
+                       },
+                       skimmed_fee_msat: None,
                });
 
                // Make sure when Node A calculates their local commitment transaction, none of the HTLCs pass
                // the dust limit check.
                let htlc_candidate = HTLCCandidate::new(htlc_amount_msat, HTLCInitiator::LocalOffered);
-               let local_commit_tx_fee = node_a_chan.next_local_commit_tx_fee_msat(htlc_candidate, None);
-               let local_commit_fee_0_htlcs = Channel::<EnforcingSigner>::commit_tx_fee_msat(node_a_chan.feerate_per_kw, 0, node_a_chan.opt_anchors());
+               let local_commit_tx_fee = node_a_chan.context.next_local_commit_tx_fee_msat(htlc_candidate, None);
+               let local_commit_fee_0_htlcs = commit_tx_fee_msat(node_a_chan.context.feerate_per_kw, 0, node_a_chan.context.get_channel_type());
                assert_eq!(local_commit_tx_fee, local_commit_fee_0_htlcs);
 
                // Finally, make sure that when Node A calculates the remote's commitment transaction fees, all
                // of the HTLCs are seen to be above the dust limit.
-               node_a_chan.channel_transaction_parameters.is_outbound_from_holder = false;
-               let remote_commit_fee_3_htlcs = Channel::<EnforcingSigner>::commit_tx_fee_msat(node_a_chan.feerate_per_kw, 3, node_a_chan.opt_anchors());
+               node_a_chan.context.channel_transaction_parameters.is_outbound_from_holder = false;
+               let remote_commit_fee_3_htlcs = commit_tx_fee_msat(node_a_chan.context.feerate_per_kw, 3, node_a_chan.context.get_channel_type());
                let htlc_candidate = HTLCCandidate::new(htlc_amount_msat, HTLCInitiator::LocalOffered);
-               let remote_commit_tx_fee = node_a_chan.next_remote_commit_tx_fee_msat(htlc_candidate, None);
+               let remote_commit_tx_fee = node_a_chan.context.next_remote_commit_tx_fee_msat(htlc_candidate, None);
                assert_eq!(remote_commit_tx_fee, remote_commit_fee_3_htlcs);
        }
 
@@ -7348,36 +7628,36 @@ mod tests {
 
                let node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
                let config = UserConfig::default();
-               let mut chan = Channel::<EnforcingSigner>::new_outbound(&fee_est, &&keys_provider, &&keys_provider, node_id, &channelmanager::provided_init_features(&config), 10000000, 100000, 42, &config, 0, 42).unwrap();
+               let mut chan = OutboundV1Channel::<EnforcingSigner>::new(&fee_est, &&keys_provider, &&keys_provider, node_id, &channelmanager::provided_init_features(&config), 10000000, 100000, 42, &config, 0, 42).unwrap();
 
-               let commitment_tx_fee_0_htlcs = Channel::<EnforcingSigner>::commit_tx_fee_msat(chan.feerate_per_kw, 0, chan.opt_anchors());
-               let commitment_tx_fee_1_htlc = Channel::<EnforcingSigner>::commit_tx_fee_msat(chan.feerate_per_kw, 1, chan.opt_anchors());
+               let commitment_tx_fee_0_htlcs = commit_tx_fee_msat(chan.context.feerate_per_kw, 0, chan.context.get_channel_type());
+               let commitment_tx_fee_1_htlc = commit_tx_fee_msat(chan.context.feerate_per_kw, 1, chan.context.get_channel_type());
 
                // If HTLC_SUCCESS_TX_WEIGHT and HTLC_TIMEOUT_TX_WEIGHT were swapped: then this HTLC would be
                // counted as dust when it shouldn't be.
-               let htlc_amt_above_timeout = ((253 * htlc_timeout_tx_weight(chan.opt_anchors()) / 1000) + chan.holder_dust_limit_satoshis + 1) * 1000;
+               let htlc_amt_above_timeout = ((253 * htlc_timeout_tx_weight(chan.context.get_channel_type()) / 1000) + chan.context.holder_dust_limit_satoshis + 1) * 1000;
                let htlc_candidate = HTLCCandidate::new(htlc_amt_above_timeout, HTLCInitiator::LocalOffered);
-               let commitment_tx_fee = chan.next_local_commit_tx_fee_msat(htlc_candidate, None);
+               let commitment_tx_fee = chan.context.next_local_commit_tx_fee_msat(htlc_candidate, None);
                assert_eq!(commitment_tx_fee, commitment_tx_fee_1_htlc);
 
                // If swapped: this HTLC would be counted as non-dust when it shouldn't be.
-               let dust_htlc_amt_below_success = ((253 * htlc_success_tx_weight(chan.opt_anchors()) / 1000) + chan.holder_dust_limit_satoshis - 1) * 1000;
+               let dust_htlc_amt_below_success = ((253 * htlc_success_tx_weight(chan.context.get_channel_type()) / 1000) + chan.context.holder_dust_limit_satoshis - 1) * 1000;
                let htlc_candidate = HTLCCandidate::new(dust_htlc_amt_below_success, HTLCInitiator::RemoteOffered);
-               let commitment_tx_fee = chan.next_local_commit_tx_fee_msat(htlc_candidate, None);
+               let commitment_tx_fee = chan.context.next_local_commit_tx_fee_msat(htlc_candidate, None);
                assert_eq!(commitment_tx_fee, commitment_tx_fee_0_htlcs);
 
-               chan.channel_transaction_parameters.is_outbound_from_holder = false;
+               chan.context.channel_transaction_parameters.is_outbound_from_holder = false;
 
                // If swapped: this HTLC would be counted as non-dust when it shouldn't be.
-               let dust_htlc_amt_above_timeout = ((253 * htlc_timeout_tx_weight(chan.opt_anchors()) / 1000) + chan.counterparty_dust_limit_satoshis + 1) * 1000;
+               let dust_htlc_amt_above_timeout = ((253 * htlc_timeout_tx_weight(chan.context.get_channel_type()) / 1000) + chan.context.counterparty_dust_limit_satoshis + 1) * 1000;
                let htlc_candidate = HTLCCandidate::new(dust_htlc_amt_above_timeout, HTLCInitiator::LocalOffered);
-               let commitment_tx_fee = chan.next_remote_commit_tx_fee_msat(htlc_candidate, None);
+               let commitment_tx_fee = chan.context.next_remote_commit_tx_fee_msat(htlc_candidate, None);
                assert_eq!(commitment_tx_fee, commitment_tx_fee_0_htlcs);
 
                // If swapped: this HTLC would be counted as dust when it shouldn't be.
-               let htlc_amt_below_success = ((253 * htlc_success_tx_weight(chan.opt_anchors()) / 1000) + chan.counterparty_dust_limit_satoshis - 1) * 1000;
+               let htlc_amt_below_success = ((253 * htlc_success_tx_weight(chan.context.get_channel_type()) / 1000) + chan.context.counterparty_dust_limit_satoshis - 1) * 1000;
                let htlc_candidate = HTLCCandidate::new(htlc_amt_below_success, HTLCInitiator::RemoteOffered);
-               let commitment_tx_fee = chan.next_remote_commit_tx_fee_msat(htlc_candidate, None);
+               let commitment_tx_fee = chan.context.next_remote_commit_tx_fee_msat(htlc_candidate, None);
                assert_eq!(commitment_tx_fee, commitment_tx_fee_1_htlc);
        }
 
@@ -7397,28 +7677,28 @@ mod tests {
                // Create Node A's channel pointing to Node B's pubkey
                let node_b_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
                let config = UserConfig::default();
-               let mut node_a_chan = Channel::<EnforcingSigner>::new_outbound(&feeest, &&keys_provider, &&keys_provider, node_b_node_id, &channelmanager::provided_init_features(&config), 10000000, 100000, 42, &config, 0, 42).unwrap();
+               let mut node_a_chan = OutboundV1Channel::<EnforcingSigner>::new(&feeest, &&keys_provider, &&keys_provider, node_b_node_id, &channelmanager::provided_init_features(&config), 10000000, 100000, 42, &config, 0, 42).unwrap();
 
                // Create Node B's channel by receiving Node A's open_channel message
                let open_channel_msg = node_a_chan.get_open_channel(chain_hash);
                let node_b_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[7; 32]).unwrap());
-               let mut node_b_chan = Channel::<EnforcingSigner>::new_from_req(&feeest, &&keys_provider, &&keys_provider, node_b_node_id, &channelmanager::provided_channel_type_features(&config), &channelmanager::provided_init_features(&config), &open_channel_msg, 7, &config, 0, &&logger, 42).unwrap();
+               let mut node_b_chan = InboundV1Channel::<EnforcingSigner>::new(&feeest, &&keys_provider, &&keys_provider, node_b_node_id, &channelmanager::provided_channel_type_features(&config), &channelmanager::provided_init_features(&config), &open_channel_msg, 7, &config, 0, &&logger, 42).unwrap();
 
                // Node B --> Node A: accept channel
                let accept_channel_msg = node_b_chan.accept_inbound_channel(0);
                node_a_chan.accept_channel(&accept_channel_msg, &config.channel_handshake_limits, &channelmanager::provided_init_features(&config)).unwrap();
 
                // Node A --> Node B: funding created
-               let output_script = node_a_chan.get_funding_redeemscript();
+               let output_script = node_a_chan.context.get_funding_redeemscript();
                let tx = Transaction { version: 1, lock_time: PackedLockTime::ZERO, input: Vec::new(), output: vec![TxOut {
                        value: 10000000, script_pubkey: output_script.clone(),
                }]};
                let funding_outpoint = OutPoint{ txid: tx.txid(), index: 0 };
-               let funding_created_msg = node_a_chan.get_outbound_funding_created(tx.clone(), funding_outpoint, &&logger).unwrap();
-               let (funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&keys_provider, &&logger).unwrap();
+               let (mut node_a_chan, funding_created_msg) = node_a_chan.get_outbound_funding_created(tx.clone(), funding_outpoint, &&logger).map_err(|_| ()).unwrap();
+               let (mut node_b_chan, funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&keys_provider, &&logger).map_err(|_| ()).unwrap();
 
                // Node B --> Node A: funding signed
-               let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, &&logger);
+               let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, &&logger).unwrap();
 
                // Now disconnect the two nodes and check that the commitment point in
                // Node B's channel_reestablish message is sane.
@@ -7457,63 +7737,63 @@ mod tests {
                let mut config_101_percent = UserConfig::default();
                config_101_percent.channel_handshake_config.max_inbound_htlc_value_in_flight_percent_of_channel = 101;
 
-               // Test that `new_outbound` creates a channel with the correct value for
+               // Test that `OutboundV1Channel::new` creates a channel with the correct value for
                // `holder_max_htlc_value_in_flight_msat`, when configured with a valid percentage value,
                // which is set to the lower bound + 1 (2%) of the `channel_value`.
-               let chan_1 = Channel::<EnforcingSigner>::new_outbound(&feeest, &&keys_provider, &&keys_provider, outbound_node_id, &channelmanager::provided_init_features(&config_2_percent), 10000000, 100000, 42, &config_2_percent, 0, 42).unwrap();
-               let chan_1_value_msat = chan_1.channel_value_satoshis * 1000;
-               assert_eq!(chan_1.holder_max_htlc_value_in_flight_msat, (chan_1_value_msat as f64 * 0.02) as u64);
+               let chan_1 = OutboundV1Channel::<EnforcingSigner>::new(&feeest, &&keys_provider, &&keys_provider, outbound_node_id, &channelmanager::provided_init_features(&config_2_percent), 10000000, 100000, 42, &config_2_percent, 0, 42).unwrap();
+               let chan_1_value_msat = chan_1.context.channel_value_satoshis * 1000;
+               assert_eq!(chan_1.context.holder_max_htlc_value_in_flight_msat, (chan_1_value_msat as f64 * 0.02) as u64);
 
                // Test with the upper bound - 1 of valid values (99%).
-               let chan_2 = Channel::<EnforcingSigner>::new_outbound(&feeest, &&keys_provider, &&keys_provider, outbound_node_id, &channelmanager::provided_init_features(&config_99_percent), 10000000, 100000, 42, &config_99_percent, 0, 42).unwrap();
-               let chan_2_value_msat = chan_2.channel_value_satoshis * 1000;
-               assert_eq!(chan_2.holder_max_htlc_value_in_flight_msat, (chan_2_value_msat as f64 * 0.99) as u64);
+               let chan_2 = OutboundV1Channel::<EnforcingSigner>::new(&feeest, &&keys_provider, &&keys_provider, outbound_node_id, &channelmanager::provided_init_features(&config_99_percent), 10000000, 100000, 42, &config_99_percent, 0, 42).unwrap();
+               let chan_2_value_msat = chan_2.context.channel_value_satoshis * 1000;
+               assert_eq!(chan_2.context.holder_max_htlc_value_in_flight_msat, (chan_2_value_msat as f64 * 0.99) as u64);
 
                let chan_1_open_channel_msg = chan_1.get_open_channel(genesis_block(network).header.block_hash());
 
-               // Test that `new_from_req` creates a channel with the correct value for
+               // Test that `InboundV1Channel::new` creates a channel with the correct value for
                // `holder_max_htlc_value_in_flight_msat`, when configured with a valid percentage value,
                // which is set to the lower bound - 1 (2%) of the `channel_value`.
-               let chan_3 = Channel::<EnforcingSigner>::new_from_req(&feeest, &&keys_provider, &&keys_provider, inbound_node_id, &channelmanager::provided_channel_type_features(&config_2_percent), &channelmanager::provided_init_features(&config_2_percent), &chan_1_open_channel_msg, 7, &config_2_percent, 0, &&logger, 42).unwrap();
-               let chan_3_value_msat = chan_3.channel_value_satoshis * 1000;
-               assert_eq!(chan_3.holder_max_htlc_value_in_flight_msat, (chan_3_value_msat as f64 * 0.02) as u64);
+               let chan_3 = InboundV1Channel::<EnforcingSigner>::new(&feeest, &&keys_provider, &&keys_provider, inbound_node_id, &channelmanager::provided_channel_type_features(&config_2_percent), &channelmanager::provided_init_features(&config_2_percent), &chan_1_open_channel_msg, 7, &config_2_percent, 0, &&logger, 42).unwrap();
+               let chan_3_value_msat = chan_3.context.channel_value_satoshis * 1000;
+               assert_eq!(chan_3.context.holder_max_htlc_value_in_flight_msat, (chan_3_value_msat as f64 * 0.02) as u64);
 
                // Test with the upper bound - 1 of valid values (99%).
-               let chan_4 = Channel::<EnforcingSigner>::new_from_req(&feeest, &&keys_provider, &&keys_provider, inbound_node_id, &channelmanager::provided_channel_type_features(&config_99_percent), &channelmanager::provided_init_features(&config_99_percent), &chan_1_open_channel_msg, 7, &config_99_percent, 0, &&logger, 42).unwrap();
-               let chan_4_value_msat = chan_4.channel_value_satoshis * 1000;
-               assert_eq!(chan_4.holder_max_htlc_value_in_flight_msat, (chan_4_value_msat as f64 * 0.99) as u64);
+               let chan_4 = InboundV1Channel::<EnforcingSigner>::new(&feeest, &&keys_provider, &&keys_provider, inbound_node_id, &channelmanager::provided_channel_type_features(&config_99_percent), &channelmanager::provided_init_features(&config_99_percent), &chan_1_open_channel_msg, 7, &config_99_percent, 0, &&logger, 42).unwrap();
+               let chan_4_value_msat = chan_4.context.channel_value_satoshis * 1000;
+               assert_eq!(chan_4.context.holder_max_htlc_value_in_flight_msat, (chan_4_value_msat as f64 * 0.99) as u64);
 
-               // Test that `new_outbound` uses the lower bound of the configurable percentage values (1%)
+               // Test that `OutboundV1Channel::new` uses the lower bound of the configurable percentage values (1%)
                // if `max_inbound_htlc_value_in_flight_percent_of_channel` is set to a value less than 1.
-               let chan_5 = Channel::<EnforcingSigner>::new_outbound(&feeest, &&keys_provider, &&keys_provider, outbound_node_id, &channelmanager::provided_init_features(&config_0_percent), 10000000, 100000, 42, &config_0_percent, 0, 42).unwrap();
-               let chan_5_value_msat = chan_5.channel_value_satoshis * 1000;
-               assert_eq!(chan_5.holder_max_htlc_value_in_flight_msat, (chan_5_value_msat as f64 * 0.01) as u64);
+               let chan_5 = OutboundV1Channel::<EnforcingSigner>::new(&feeest, &&keys_provider, &&keys_provider, outbound_node_id, &channelmanager::provided_init_features(&config_0_percent), 10000000, 100000, 42, &config_0_percent, 0, 42).unwrap();
+               let chan_5_value_msat = chan_5.context.channel_value_satoshis * 1000;
+               assert_eq!(chan_5.context.holder_max_htlc_value_in_flight_msat, (chan_5_value_msat as f64 * 0.01) as u64);
 
-               // Test that `new_outbound` uses the upper bound of the configurable percentage values
+               // Test that `OutboundV1Channel::new` uses the upper bound of the configurable percentage values
                // (100%) if `max_inbound_htlc_value_in_flight_percent_of_channel` is set to a larger value
                // than 100.
-               let chan_6 = Channel::<EnforcingSigner>::new_outbound(&feeest, &&keys_provider, &&keys_provider, outbound_node_id, &channelmanager::provided_init_features(&config_101_percent), 10000000, 100000, 42, &config_101_percent, 0, 42).unwrap();
-               let chan_6_value_msat = chan_6.channel_value_satoshis * 1000;
-               assert_eq!(chan_6.holder_max_htlc_value_in_flight_msat, chan_6_value_msat);
+               let chan_6 = OutboundV1Channel::<EnforcingSigner>::new(&feeest, &&keys_provider, &&keys_provider, outbound_node_id, &channelmanager::provided_init_features(&config_101_percent), 10000000, 100000, 42, &config_101_percent, 0, 42).unwrap();
+               let chan_6_value_msat = chan_6.context.channel_value_satoshis * 1000;
+               assert_eq!(chan_6.context.holder_max_htlc_value_in_flight_msat, chan_6_value_msat);
 
-               // Test that `new_from_req` uses the lower bound of the configurable percentage values (1%)
+               // Test that `InboundV1Channel::new` uses the lower bound of the configurable percentage values (1%)
                // if `max_inbound_htlc_value_in_flight_percent_of_channel` is set to a value less than 1.
-               let chan_7 = Channel::<EnforcingSigner>::new_from_req(&feeest, &&keys_provider, &&keys_provider, inbound_node_id, &channelmanager::provided_channel_type_features(&config_0_percent), &channelmanager::provided_init_features(&config_0_percent), &chan_1_open_channel_msg, 7, &config_0_percent, 0, &&logger, 42).unwrap();
-               let chan_7_value_msat = chan_7.channel_value_satoshis * 1000;
-               assert_eq!(chan_7.holder_max_htlc_value_in_flight_msat, (chan_7_value_msat as f64 * 0.01) as u64);
+               let chan_7 = InboundV1Channel::<EnforcingSigner>::new(&feeest, &&keys_provider, &&keys_provider, inbound_node_id, &channelmanager::provided_channel_type_features(&config_0_percent), &channelmanager::provided_init_features(&config_0_percent), &chan_1_open_channel_msg, 7, &config_0_percent, 0, &&logger, 42).unwrap();
+               let chan_7_value_msat = chan_7.context.channel_value_satoshis * 1000;
+               assert_eq!(chan_7.context.holder_max_htlc_value_in_flight_msat, (chan_7_value_msat as f64 * 0.01) as u64);
 
-               // Test that `new_from_req` uses the upper bound of the configurable percentage values
+               // Test that `InboundV1Channel::new` uses the upper bound of the configurable percentage values
                // (100%) if `max_inbound_htlc_value_in_flight_percent_of_channel` is set to a larger value
                // than 100.
-               let chan_8 = Channel::<EnforcingSigner>::new_from_req(&feeest, &&keys_provider, &&keys_provider, inbound_node_id, &channelmanager::provided_channel_type_features(&config_101_percent), &channelmanager::provided_init_features(&config_101_percent), &chan_1_open_channel_msg, 7, &config_101_percent, 0, &&logger, 42).unwrap();
-               let chan_8_value_msat = chan_8.channel_value_satoshis * 1000;
-               assert_eq!(chan_8.holder_max_htlc_value_in_flight_msat, chan_8_value_msat);
+               let chan_8 = InboundV1Channel::<EnforcingSigner>::new(&feeest, &&keys_provider, &&keys_provider, inbound_node_id, &channelmanager::provided_channel_type_features(&config_101_percent), &channelmanager::provided_init_features(&config_101_percent), &chan_1_open_channel_msg, 7, &config_101_percent, 0, &&logger, 42).unwrap();
+               let chan_8_value_msat = chan_8.context.channel_value_satoshis * 1000;
+               assert_eq!(chan_8.context.holder_max_htlc_value_in_flight_msat, chan_8_value_msat);
        }
 
        #[test]
        fn test_configured_holder_selected_channel_reserve_satoshis() {
 
-               // Test that `new_outbound` and `new_from_req` create a channel with the correct
+               // Test that `OutboundV1Channel::new` and `InboundV1Channel::new` create a channel with the correct
                // channel reserves, when `their_channel_reserve_proportional_millionths` is configured.
                test_self_and_counterparty_channel_reserve(10_000_000, 0.02, 0.02);
 
@@ -7545,25 +7825,25 @@ mod tests {
 
                let mut outbound_node_config = UserConfig::default();
                outbound_node_config.channel_handshake_config.their_channel_reserve_proportional_millionths = (outbound_selected_channel_reserve_perc * 1_000_000.0) as u32;
-               let chan = Channel::<EnforcingSigner>::new_outbound(&&fee_est, &&keys_provider, &&keys_provider, outbound_node_id, &channelmanager::provided_init_features(&outbound_node_config), channel_value_satoshis, 100_000, 42, &outbound_node_config, 0, 42).unwrap();
+               let chan = OutboundV1Channel::<EnforcingSigner>::new(&&fee_est, &&keys_provider, &&keys_provider, outbound_node_id, &channelmanager::provided_init_features(&outbound_node_config), channel_value_satoshis, 100_000, 42, &outbound_node_config, 0, 42).unwrap();
 
-               let expected_outbound_selected_chan_reserve = cmp::max(MIN_THEIR_CHAN_RESERVE_SATOSHIS, (chan.channel_value_satoshis as f64 * outbound_selected_channel_reserve_perc) as u64);
-               assert_eq!(chan.holder_selected_channel_reserve_satoshis, expected_outbound_selected_chan_reserve);
+               let expected_outbound_selected_chan_reserve = cmp::max(MIN_THEIR_CHAN_RESERVE_SATOSHIS, (chan.context.channel_value_satoshis as f64 * outbound_selected_channel_reserve_perc) as u64);
+               assert_eq!(chan.context.holder_selected_channel_reserve_satoshis, expected_outbound_selected_chan_reserve);
 
                let chan_open_channel_msg = chan.get_open_channel(genesis_block(network).header.block_hash());
                let mut inbound_node_config = UserConfig::default();
                inbound_node_config.channel_handshake_config.their_channel_reserve_proportional_millionths = (inbound_selected_channel_reserve_perc * 1_000_000.0) as u32;
 
                if outbound_selected_channel_reserve_perc + inbound_selected_channel_reserve_perc < 1.0 {
-                       let chan_inbound_node = Channel::<EnforcingSigner>::new_from_req(&&fee_est, &&keys_provider, &&keys_provider, inbound_node_id, &channelmanager::provided_channel_type_features(&inbound_node_config), &channelmanager::provided_init_features(&outbound_node_config), &chan_open_channel_msg, 7, &inbound_node_config, 0, &&logger, 42).unwrap();
+                       let chan_inbound_node = InboundV1Channel::<EnforcingSigner>::new(&&fee_est, &&keys_provider, &&keys_provider, inbound_node_id, &channelmanager::provided_channel_type_features(&inbound_node_config), &channelmanager::provided_init_features(&outbound_node_config), &chan_open_channel_msg, 7, &inbound_node_config, 0, &&logger, 42).unwrap();
 
-                       let expected_inbound_selected_chan_reserve = cmp::max(MIN_THEIR_CHAN_RESERVE_SATOSHIS, (chan.channel_value_satoshis as f64 * inbound_selected_channel_reserve_perc) as u64);
+                       let expected_inbound_selected_chan_reserve = cmp::max(MIN_THEIR_CHAN_RESERVE_SATOSHIS, (chan.context.channel_value_satoshis as f64 * inbound_selected_channel_reserve_perc) as u64);
 
-                       assert_eq!(chan_inbound_node.holder_selected_channel_reserve_satoshis, expected_inbound_selected_chan_reserve);
-                       assert_eq!(chan_inbound_node.counterparty_selected_channel_reserve_satoshis.unwrap(), expected_outbound_selected_chan_reserve);
+                       assert_eq!(chan_inbound_node.context.holder_selected_channel_reserve_satoshis, expected_inbound_selected_chan_reserve);
+                       assert_eq!(chan_inbound_node.context.counterparty_selected_channel_reserve_satoshis.unwrap(), expected_outbound_selected_chan_reserve);
                } else {
                        // Channel Negotiations failed
-                       let result = Channel::<EnforcingSigner>::new_from_req(&&fee_est, &&keys_provider, &&keys_provider, inbound_node_id, &channelmanager::provided_channel_type_features(&inbound_node_config), &channelmanager::provided_init_features(&outbound_node_config), &chan_open_channel_msg, 7, &inbound_node_config, 0, &&logger, 42);
+                       let result = InboundV1Channel::<EnforcingSigner>::new(&&fee_est, &&keys_provider, &&keys_provider, inbound_node_id, &channelmanager::provided_channel_type_features(&inbound_node_config), &channelmanager::provided_init_features(&outbound_node_config), &chan_open_channel_msg, 7, &inbound_node_config, 0, &&logger, 42);
                        assert!(result.is_err());
                }
        }
@@ -7571,19 +7851,42 @@ mod tests {
        #[test]
        fn channel_update() {
                let feeest = LowerBoundedFeeEstimator::new(&TestFeeEstimator{fee_est: 15000});
+               let logger = test_utils::TestLogger::new();
                let secp_ctx = Secp256k1::new();
                let seed = [42; 32];
                let network = Network::Testnet;
+               let best_block = BestBlock::from_network(network);
                let chain_hash = genesis_block(network).header.block_hash();
                let keys_provider = test_utils::TestKeysInterface::new(&seed, network);
 
-               // Create a channel.
+               // Create Node A's channel pointing to Node B's pubkey
                let node_b_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
                let config = UserConfig::default();
-               let mut node_a_chan = Channel::<EnforcingSigner>::new_outbound(&feeest, &&keys_provider, &&keys_provider, node_b_node_id, &channelmanager::provided_init_features(&config), 10000000, 100000, 42, &config, 0, 42).unwrap();
-               assert!(node_a_chan.counterparty_forwarding_info.is_none());
-               assert_eq!(node_a_chan.holder_htlc_minimum_msat, 1); // the default
-               assert!(node_a_chan.counterparty_forwarding_info().is_none());
+               let mut node_a_chan = OutboundV1Channel::<EnforcingSigner>::new(&feeest, &&keys_provider, &&keys_provider, node_b_node_id, &channelmanager::provided_init_features(&config), 10000000, 100000, 42, &config, 0, 42).unwrap();
+
+               // Create Node B's channel by receiving Node A's open_channel message
+               // Make sure A's dust limit is as we expect.
+               let open_channel_msg = node_a_chan.get_open_channel(genesis_block(network).header.block_hash());
+               let node_b_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[7; 32]).unwrap());
+               let mut node_b_chan = InboundV1Channel::<EnforcingSigner>::new(&feeest, &&keys_provider, &&keys_provider, node_b_node_id, &channelmanager::provided_channel_type_features(&config), &channelmanager::provided_init_features(&config), &open_channel_msg, 7, &config, 0, &&logger, 42).unwrap();
+
+               // Node B --> Node A: accept channel, explicitly setting B's dust limit.
+               let mut accept_channel_msg = node_b_chan.accept_inbound_channel(0);
+               accept_channel_msg.dust_limit_satoshis = 546;
+               node_a_chan.accept_channel(&accept_channel_msg, &config.channel_handshake_limits, &channelmanager::provided_init_features(&config)).unwrap();
+               node_a_chan.context.holder_dust_limit_satoshis = 1560;
+
+               // Node A --> Node B: funding created
+               let output_script = node_a_chan.context.get_funding_redeemscript();
+               let tx = Transaction { version: 1, lock_time: PackedLockTime::ZERO, input: Vec::new(), output: vec![TxOut {
+                       value: 10000000, script_pubkey: output_script.clone(),
+               }]};
+               let funding_outpoint = OutPoint{ txid: tx.txid(), index: 0 };
+               let (mut node_a_chan, funding_created_msg) = node_a_chan.get_outbound_funding_created(tx.clone(), funding_outpoint, &&logger).map_err(|_| ()).unwrap();
+               let (_, funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&keys_provider, &&logger).map_err(|_| ()).unwrap();
+
+               // Node B --> Node A: funding signed
+               let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, &&logger).unwrap();
 
                // Make sure that receiving a channel update will update the Channel as expected.
                let update = ChannelUpdate {
@@ -7605,8 +7908,8 @@ mod tests {
 
                // The counterparty can send an update with a higher minimum HTLC, but that shouldn't
                // change our official htlc_minimum_msat.
-               assert_eq!(node_a_chan.holder_htlc_minimum_msat, 1);
-               match node_a_chan.counterparty_forwarding_info() {
+               assert_eq!(node_a_chan.context.holder_htlc_minimum_msat, 1);
+               match node_a_chan.context.counterparty_forwarding_info() {
                        Some(info) => {
                                assert_eq!(info.cltv_expiry_delta, 100);
                                assert_eq!(info.fee_base_msat, 110);
@@ -7659,9 +7962,9 @@ mod tests {
                let counterparty_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
                let mut config = UserConfig::default();
                config.channel_handshake_config.announced_channel = false;
-               let mut chan = Channel::<InMemorySigner>::new_outbound(&LowerBoundedFeeEstimator::new(&feeest), &&keys_provider, &&keys_provider, counterparty_node_id, &channelmanager::provided_init_features(&config), 10_000_000, 0, 42, &config, 0, 42).unwrap(); // Nothing uses their network key in this test
-               chan.holder_dust_limit_satoshis = 546;
-               chan.counterparty_selected_channel_reserve_satoshis = Some(0); // Filled in in accept_channel
+               let mut chan = OutboundV1Channel::<InMemorySigner>::new(&LowerBoundedFeeEstimator::new(&feeest), &&keys_provider, &&keys_provider, counterparty_node_id, &channelmanager::provided_init_features(&config), 10_000_000, 0, 42, &config, 0, 42).unwrap(); // Nothing uses their network key in this test
+               chan.context.holder_dust_limit_satoshis = 546;
+               chan.context.counterparty_selected_channel_reserve_satoshis = Some(0); // Filled in in accept_channel
 
                let funding_info = OutPoint{ txid: Txid::from_hex("8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be").unwrap(), index: 0 };
 
@@ -7672,13 +7975,13 @@ mod tests {
                        delayed_payment_basepoint: public_from_secret_hex(&secp_ctx, "1552dfba4f6cf29a62a0af13c8d6981d36d0ef8d61ba10fb0fe90da7634d7e13"),
                        htlc_basepoint: public_from_secret_hex(&secp_ctx, "4444444444444444444444444444444444444444444444444444444444444444")
                };
-               chan.channel_transaction_parameters.counterparty_parameters = Some(
+               chan.context.channel_transaction_parameters.counterparty_parameters = Some(
                        CounterpartyChannelTransactionParameters {
                                pubkeys: counterparty_pubkeys.clone(),
                                selected_contest_delay: 144
                        });
-               chan.channel_transaction_parameters.funding_outpoint = Some(funding_info);
-               signer.provide_channel_parameters(&chan.channel_transaction_parameters);
+               chan.context.channel_transaction_parameters.funding_outpoint = Some(funding_info);
+               signer.provide_channel_parameters(&chan.context.channel_transaction_parameters);
 
                assert_eq!(counterparty_pubkeys.payment_point.serialize()[..],
                           hex::decode("032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991").unwrap()[..]);
@@ -7692,23 +7995,23 @@ mod tests {
                // We can't just use build_holder_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 = &chan.holder_signer.pubkeys().delayed_payment_basepoint;
+               let delayed_payment_base = &chan.context.holder_signer.pubkeys().delayed_payment_basepoint;
                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 = &chan.holder_signer.pubkeys().htlc_basepoint;
+               let htlc_basepoint = &chan.context.holder_signer.pubkeys().htlc_basepoint;
                let keys = TxCreationKeys::derive_new(&secp_ctx, &per_commitment_point, delayed_payment_base, htlc_basepoint, &counterparty_pubkeys.revocation_basepoint, &counterparty_pubkeys.htlc_basepoint);
 
                macro_rules! test_commitment {
                        ( $counterparty_sig_hex: expr, $sig_hex: expr, $tx_hex: expr, $($remain:tt)* ) => {
-                               chan.channel_transaction_parameters.opt_anchors = None;
-                               test_commitment_common!($counterparty_sig_hex, $sig_hex, $tx_hex, false, $($remain)*);
+                               chan.context.channel_transaction_parameters.channel_type_features = ChannelTypeFeatures::only_static_remote_key();
+                               test_commitment_common!($counterparty_sig_hex, $sig_hex, $tx_hex, &ChannelTypeFeatures::only_static_remote_key(), $($remain)*);
                        };
                }
 
                macro_rules! test_commitment_with_anchors {
                        ( $counterparty_sig_hex: expr, $sig_hex: expr, $tx_hex: expr, $($remain:tt)* ) => {
-                               chan.channel_transaction_parameters.opt_anchors = Some(());
-                               test_commitment_common!($counterparty_sig_hex, $sig_hex, $tx_hex, true, $($remain)*);
+                               chan.context.channel_transaction_parameters.channel_type_features = ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies();
+                               test_commitment_common!($counterparty_sig_hex, $sig_hex, $tx_hex, &ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies(), $($remain)*);
                        };
                }
 
@@ -7717,7 +8020,7 @@ mod tests {
                                $( { $htlc_idx: expr, $counterparty_htlc_sig_hex: expr, $htlc_sig_hex: expr, $htlc_tx_hex: expr } ), *
                        } ) => { {
                                let (commitment_tx, htlcs): (_, Vec<HTLCOutputInCommitment>) = {
-                                       let mut commitment_stats = chan.build_commitment_transaction(0xffffffffffff - 42, &keys, true, false, &logger);
+                                       let mut commitment_stats = chan.context.build_commitment_transaction(0xffffffffffff - 42, &keys, true, false, &logger);
 
                                        let htlcs = commitment_stats.htlcs_included.drain(..)
                                                .filter_map(|(htlc, _)| if htlc.transaction_output_index.is_some() { Some(htlc) } else { None })
@@ -7726,11 +8029,11 @@ mod tests {
                                };
                                let trusted_tx = commitment_tx.trust();
                                let unsigned_tx = trusted_tx.built_transaction();
-                               let redeemscript = chan.get_funding_redeemscript();
+                               let redeemscript = chan.context.get_funding_redeemscript();
                                let counterparty_signature = Signature::from_der(&hex::decode($counterparty_sig_hex).unwrap()[..]).unwrap();
-                               let sighash = unsigned_tx.get_sighash_all(&redeemscript, chan.channel_value_satoshis);
+                               let sighash = unsigned_tx.get_sighash_all(&redeemscript, chan.context.channel_value_satoshis);
                                log_trace!(logger, "unsigned_tx = {}", hex::encode(serialize(&unsigned_tx.transaction)));
-                               assert!(secp_ctx.verify_ecdsa(&sighash, &counterparty_signature, chan.counterparty_funding_pubkey()).is_ok(), "verify counterparty commitment sig");
+                               assert!(secp_ctx.verify_ecdsa(&sighash, &counterparty_signature, chan.context.counterparty_funding_pubkey()).is_ok(), "verify counterparty commitment sig");
 
                                let mut per_htlc: Vec<(HTLCOutputInCommitment, Option<Signature>)> = Vec::new();
                                per_htlc.clear(); // Don't warn about excess mut for no-HTLC calls
@@ -7747,13 +8050,13 @@ mod tests {
                                        commitment_tx.clone(),
                                        counterparty_signature,
                                        counterparty_htlc_sigs,
-                                       &chan.holder_signer.pubkeys().funding_pubkey,
-                                       chan.counterparty_funding_pubkey()
+                                       &chan.context.holder_signer.pubkeys().funding_pubkey,
+                                       chan.context.counterparty_funding_pubkey()
                                );
                                let (holder_sig, htlc_sigs) = signer.sign_holder_commitment_and_htlcs(&holder_commitment_tx, &secp_ctx).unwrap();
                                assert_eq!(Signature::from_der(&hex::decode($sig_hex).unwrap()[..]).unwrap(), holder_sig, "holder_sig");
 
-                               let funding_redeemscript = chan.get_funding_redeemscript();
+                               let funding_redeemscript = chan.context.get_funding_redeemscript();
                                let tx = holder_commitment_tx.add_holder_sig(&funding_redeemscript, holder_sig);
                                assert_eq!(serialize(&tx)[..], hex::decode($tx_hex).unwrap()[..], "tx");
 
@@ -7765,11 +8068,11 @@ mod tests {
                                        let remote_signature = Signature::from_der(&hex::decode($counterparty_htlc_sig_hex).unwrap()[..]).unwrap();
 
                                        let ref htlc = htlcs[$htlc_idx];
-                                       let htlc_tx = chan_utils::build_htlc_transaction(&unsigned_tx.txid, chan.feerate_per_kw,
-                                               chan.get_counterparty_selected_contest_delay().unwrap(),
-                                               &htlc, $opt_anchors, false, &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
+                                       let htlc_tx = chan_utils::build_htlc_transaction(&unsigned_tx.txid, chan.context.feerate_per_kw,
+                                               chan.context.get_counterparty_selected_contest_delay().unwrap(),
+                                               &htlc, $opt_anchors, &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
                                        let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, $opt_anchors, &keys);
-                                       let htlc_sighashtype = if $opt_anchors { EcdsaSighashType::SinglePlusAnyoneCanPay } else { EcdsaSighashType::All };
+                                       let htlc_sighashtype = if $opt_anchors.supports_anchors_zero_fee_htlc_tx() { EcdsaSighashType::SinglePlusAnyoneCanPay } else { EcdsaSighashType::All };
                                        let htlc_sighash = Message::from_slice(&sighash::SighashCache::new(&htlc_tx).segwit_signature_hash(0, &htlc_redeemscript, htlc.amount_msat / 1000, htlc_sighashtype).unwrap()[..]).unwrap();
                                        assert!(secp_ctx.verify_ecdsa(&htlc_sighash, &remote_signature, &keys.countersignatory_htlc_key).is_ok(), "verify counterparty htlc sig");
 
@@ -7786,13 +8089,13 @@ mod tests {
                                        }
 
                                        let htlc_sig = htlc_sig_iter.next().unwrap();
-                                       let num_anchors = if $opt_anchors { 2 } else { 0 };
+                                       let num_anchors = if $opt_anchors.supports_anchors_zero_fee_htlc_tx() { 2 } else { 0 };
                                        assert_eq!((htlc_sig.0).0.transaction_output_index, Some($htlc_idx + num_anchors), "output index");
 
                                        let signature = Signature::from_der(&hex::decode($htlc_sig_hex).unwrap()[..]).unwrap();
                                        assert_eq!(signature, *(htlc_sig.1).1, "htlc sig");
                                        let index = (htlc_sig.1).0;
-                                       let channel_parameters = chan.channel_transaction_parameters.as_holder_broadcastable();
+                                       let channel_parameters = chan.context.channel_transaction_parameters.as_holder_broadcastable();
                                        let trusted_tx = holder_commitment_tx.trust();
                                        log_trace!(logger, "htlc_tx = {}", hex::encode(serialize(&trusted_tx.get_signed_htlc_tx(&channel_parameters, index, &(htlc_sig.0).1, (htlc_sig.1).1, &preimage))));
                                        assert_eq!(serialize(&trusted_tx.get_signed_htlc_tx(&channel_parameters, index, &(htlc_sig.0).1, (htlc_sig.1).1, &preimage))[..],
@@ -7808,7 +8111,7 @@ mod tests {
                                                 "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b80024a010000000000002200202b1b5854183c12d3316565972c4668929d314d81c5dcdbb21cb45fe8a9a8114f10529800000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0400473044022007cf6b405e9c9b4f527b0ecad9d8bb661fabb8b12abf7d1c0b3ad1855db3ed490220616d5c1eeadccc63bd775a131149455d62d95a42c2a1b01cc7821fc42dce7778014730440220655bf909fb6fa81d086f1336ac72c97906dce29d1b166e305c99152d810e26e1022051f577faa46412c46707aaac46b65d50053550a66334e00a44af2706f27a865801475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220", {});
 
                // simple commitment tx with no HTLCs
-               chan.value_to_self_msat = 7000000000;
+               chan.context.value_to_self_msat = 7000000000;
 
                test_commitment!("3045022100c3127b33dcc741dd6b05b1e63cbd1a9a7d816f37af9b6756fa2376b056f032370220408b96279808fe57eb7e463710804cdf4f108388bc5cf722d8c848d2c7f9f3b0",
                                                 "30440220616210b2cc4d3afb601013c373bbd8aac54febd9f15400379a8cb65ce7deca60022034236c010991beb7ff770510561ae8dc885b8d38d1947248c38f2ae055647142",
@@ -7819,7 +8122,7 @@ mod tests {
                                                 "30450221008266ac6db5ea71aac3c95d97b0e172ff596844851a3216eb88382a8dddfd33d2022050e240974cfd5d708708b4365574517c18e7ae535ef732a3484d43d0d82be9f7",
                                                 "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b80044a010000000000002200202b1b5854183c12d3316565972c4668929d314d81c5dcdbb21cb45fe8a9a8114f4a01000000000000220020e9e86e4823faa62e222ebc858a226636856158f07e69898da3b0d1af0ddb3994c0c62d0000000000220020f3394e1e619b0eca1f91be2fb5ab4dfc59ba5b84ebe014ad1d43a564d012994a508b6a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e04004830450221008266ac6db5ea71aac3c95d97b0e172ff596844851a3216eb88382a8dddfd33d2022050e240974cfd5d708708b4365574517c18e7ae535ef732a3484d43d0d82be9f701483045022100f89034eba16b2be0e5581f750a0a6309192b75cce0f202f0ee2b4ec0cc394850022076c65dc507fe42276152b7a3d90e961e678adbe966e916ecfe85e64d430e75f301475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220", {});
 
-               chan.pending_inbound_htlcs.push({
+               chan.context.pending_inbound_htlcs.push({
                        let mut out = InboundHTLCOutput{
                                htlc_id: 0,
                                amount_msat: 1000000,
@@ -7830,7 +8133,7 @@ mod tests {
                        out.payment_hash.0 = Sha256::hash(&hex::decode("0000000000000000000000000000000000000000000000000000000000000000").unwrap()).into_inner();
                        out
                });
-               chan.pending_inbound_htlcs.push({
+               chan.context.pending_inbound_htlcs.push({
                        let mut out = InboundHTLCOutput{
                                htlc_id: 1,
                                amount_msat: 2000000,
@@ -7841,7 +8144,7 @@ mod tests {
                        out.payment_hash.0 = Sha256::hash(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()).into_inner();
                        out
                });
-               chan.pending_outbound_htlcs.push({
+               chan.context.pending_outbound_htlcs.push({
                        let mut out = OutboundHTLCOutput{
                                htlc_id: 2,
                                amount_msat: 2000000,
@@ -7849,11 +8152,12 @@ mod tests {
                                payment_hash: PaymentHash([0; 32]),
                                state: OutboundHTLCState::Committed,
                                source: HTLCSource::dummy(),
+                               skimmed_fee_msat: None,
                        };
                        out.payment_hash.0 = Sha256::hash(&hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap()).into_inner();
                        out
                });
-               chan.pending_outbound_htlcs.push({
+               chan.context.pending_outbound_htlcs.push({
                        let mut out = OutboundHTLCOutput{
                                htlc_id: 3,
                                amount_msat: 3000000,
@@ -7861,11 +8165,12 @@ mod tests {
                                payment_hash: PaymentHash([0; 32]),
                                state: OutboundHTLCState::Committed,
                                source: HTLCSource::dummy(),
+                               skimmed_fee_msat: None,
                        };
                        out.payment_hash.0 = Sha256::hash(&hex::decode("0303030303030303030303030303030303030303030303030303030303030303").unwrap()).into_inner();
                        out
                });
-               chan.pending_inbound_htlcs.push({
+               chan.context.pending_inbound_htlcs.push({
                        let mut out = InboundHTLCOutput{
                                htlc_id: 4,
                                amount_msat: 4000000,
@@ -7878,8 +8183,8 @@ mod tests {
                });
 
                // commitment tx with all five HTLCs untrimmed (minimum feerate)
-               chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
-               chan.feerate_per_kw = 0;
+               chan.context.value_to_self_msat = 6993000000; // 7000000000 - 7000000
+               chan.context.feerate_per_kw = 0;
 
                test_commitment!("3044022009b048187705a8cbc9ad73adbe5af148c3d012e1f067961486c822c7af08158c022006d66f3704cfab3eb2dc49dae24e4aa22a6910fc9b424007583204e3621af2e5",
                                 "304402206fc2d1f10ea59951eefac0b4b7c396a3c3d87b71ff0b019796ef4535beaf36f902201765b0181e514d04f4c8ad75659d7037be26cdb3f8bb6f78fe61decef484c3ea",
@@ -7912,8 +8217,8 @@ mod tests {
                } );
 
                // commitment tx with seven outputs untrimmed (maximum feerate)
-               chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
-               chan.feerate_per_kw = 647;
+               chan.context.value_to_self_msat = 6993000000; // 7000000000 - 7000000
+               chan.context.feerate_per_kw = 647;
 
                test_commitment!("3045022100a135f9e8a5ed25f7277446c67956b00ce6f610ead2bdec2c2f686155b7814772022059f1f6e1a8b336a68efcc1af3fe4d422d4827332b5b067501b099c47b7b5b5ee",
                                 "30450221009ec15c687898bb4da8b3a833e5ab8bfc51ec6e9202aaa8e66611edfd4a85ed1102203d7183e45078b9735c93450bc3415d3e5a8c576141a711ec6ddcb4a893926bb7",
@@ -7946,8 +8251,8 @@ mod tests {
                } );
 
                // commitment tx with six outputs untrimmed (minimum feerate)
-               chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
-               chan.feerate_per_kw = 648;
+               chan.context.value_to_self_msat = 6993000000; // 7000000000 - 7000000
+               chan.context.feerate_per_kw = 648;
 
                test_commitment!("304402203948f900a5506b8de36a4d8502f94f21dd84fd9c2314ab427d52feaa7a0a19f2022059b6a37a4adaa2c5419dc8aea63c6e2a2ec4c4bde46207f6dc1fcd22152fc6e5",
                                 "3045022100b15f72908ba3382a34ca5b32519240a22300cc6015b6f9418635fb41f3d01d8802207adb331b9ed1575383dca0f2355e86c173802feecf8298fbea53b9d4610583e9",
@@ -7975,9 +8280,9 @@ mod tests {
                } );
 
                // anchors: commitment tx with six outputs untrimmed (minimum dust limit)
-               chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
-               chan.feerate_per_kw = 645;
-               chan.holder_dust_limit_satoshis = 1001;
+               chan.context.value_to_self_msat = 6993000000; // 7000000000 - 7000000
+               chan.context.feerate_per_kw = 645;
+               chan.context.holder_dust_limit_satoshis = 1001;
 
                test_commitment_with_anchors!("3044022025d97466c8049e955a5afce28e322f4b34d2561118e52332fb400f9b908cc0a402205dc6fba3a0d67ee142c428c535580cd1f2ff42e2f89b47e0c8a01847caffc312",
                                 "3045022100d57697c707b6f6d053febf24b98e8989f186eea42e37e9e91663ec2c70bb8f70022079b0715a472118f262f43016a674f59c015d9cafccec885968e76d9d9c5d0051",
@@ -8005,9 +8310,9 @@ mod tests {
                } );
 
                // commitment tx with six outputs untrimmed (maximum feerate)
-               chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
-               chan.feerate_per_kw = 2069;
-               chan.holder_dust_limit_satoshis = 546;
+               chan.context.value_to_self_msat = 6993000000; // 7000000000 - 7000000
+               chan.context.feerate_per_kw = 2069;
+               chan.context.holder_dust_limit_satoshis = 546;
 
                test_commitment!("304502210090b96a2498ce0c0f2fadbec2aab278fed54c1a7838df793ec4d2c78d96ec096202204fdd439c50f90d483baa7b68feeef4bd33bc277695405447bcd0bfb2ca34d7bc",
                                 "3045022100ad9a9bbbb75d506ca3b716b336ee3cf975dd7834fcf129d7dd188146eb58a8b4022061a759ee417339f7fe2ea1e8deb83abb6a74db31a09b7648a932a639cda23e33",
@@ -8035,8 +8340,8 @@ mod tests {
                } );
 
                // commitment tx with five outputs untrimmed (minimum feerate)
-               chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
-               chan.feerate_per_kw = 2070;
+               chan.context.value_to_self_msat = 6993000000; // 7000000000 - 7000000
+               chan.context.feerate_per_kw = 2070;
 
                test_commitment!("304402204ca1ba260dee913d318271d86e10ca0f5883026fb5653155cff600fb40895223022037b145204b7054a40e08bb1fefbd826f827b40838d3e501423bcc57924bcb50c",
                                 "3044022001014419b5ba00e083ac4e0a85f19afc848aacac2d483b4b525d15e2ae5adbfe022015ebddad6ee1e72b47cb09f3e78459da5be01ccccd95dceca0e056a00cc773c1",
@@ -8059,8 +8364,8 @@ mod tests {
                } );
 
                // commitment tx with five outputs untrimmed (maximum feerate)
-               chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
-               chan.feerate_per_kw = 2194;
+               chan.context.value_to_self_msat = 6993000000; // 7000000000 - 7000000
+               chan.context.feerate_per_kw = 2194;
 
                test_commitment!("304402204bb3d6e279d71d9da414c82de42f1f954267c762b2e2eb8b76bc3be4ea07d4b0022014febc009c5edc8c3fc5d94015de163200f780046f1c293bfed8568f08b70fb3",
                                 "3044022072c2e2b1c899b2242656a537dde2892fa3801be0d6df0a87836c550137acde8302201654aa1974d37a829083c3ba15088689f30b56d6a4f6cb14c7bad0ee3116d398",
@@ -8083,8 +8388,8 @@ mod tests {
                } );
 
                // commitment tx with four outputs untrimmed (minimum feerate)
-               chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
-               chan.feerate_per_kw = 2195;
+               chan.context.value_to_self_msat = 6993000000; // 7000000000 - 7000000
+               chan.context.feerate_per_kw = 2195;
 
                test_commitment!("304402201a8c1b1f9671cd9e46c7323a104d7047cc48d3ee80d40d4512e0c72b8dc65666022066d7f9a2ce18c9eb22d2739ffcce05721c767f9b607622a31b6ea5793ddce403",
                                 "3044022044d592025b610c0d678f65032e87035cdfe89d1598c522cc32524ae8172417c30220749fef9d5b2ae8cdd91ece442ba8809bc891efedae2291e578475f97715d1767",
@@ -8102,9 +8407,11 @@ mod tests {
                } );
 
                // anchors: commitment tx with four outputs untrimmed (minimum dust limit)
-               chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
-               chan.feerate_per_kw = 2185;
-               chan.holder_dust_limit_satoshis = 2001;
+               chan.context.value_to_self_msat = 6993000000; // 7000000000 - 7000000
+               chan.context.feerate_per_kw = 2185;
+               chan.context.holder_dust_limit_satoshis = 2001;
+               let cached_channel_type = chan.context.channel_type;
+               chan.context.channel_type = ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies();
 
                test_commitment_with_anchors!("3044022040f63a16148cf35c8d3d41827f5ae7f7c3746885bb64d4d1b895892a83812b3e02202fcf95c2bf02c466163b3fa3ced6a24926fbb4035095a96842ef516e86ba54c0",
                                 "3045022100cd8479cfe1edb1e5a1d487391e0451a469c7171e51e680183f19eb4321f20e9b02204eab7d5a6384b1b08e03baa6e4d9748dfd2b5ab2bae7e39604a0d0055bbffdd5",
@@ -8122,9 +8429,10 @@ mod tests {
                } );
 
                // commitment tx with four outputs untrimmed (maximum feerate)
-               chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
-               chan.feerate_per_kw = 3702;
-               chan.holder_dust_limit_satoshis = 546;
+               chan.context.value_to_self_msat = 6993000000; // 7000000000 - 7000000
+               chan.context.feerate_per_kw = 3702;
+               chan.context.holder_dust_limit_satoshis = 546;
+               chan.context.channel_type = cached_channel_type.clone();
 
                test_commitment!("304502210092a587aeb777f869e7ff0d7898ea619ee26a3dacd1f3672b945eea600be431100220077ee9eae3528d15251f2a52b607b189820e57a6ccfac8d1af502b132ee40169",
                                 "3045022100e5efb73c32d32da2d79702299b6317de6fb24a60476e3855926d78484dd1b3c802203557cb66a42c944ef06e00bcc4da35a5bcb2f185aab0f8e403e519e1d66aaf75",
@@ -8142,8 +8450,8 @@ mod tests {
                } );
 
                // commitment tx with three outputs untrimmed (minimum feerate)
-               chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
-               chan.feerate_per_kw = 3703;
+               chan.context.value_to_self_msat = 6993000000; // 7000000000 - 7000000
+               chan.context.feerate_per_kw = 3703;
 
                test_commitment!("3045022100b495d239772a237ff2cf354b1b11be152fd852704cb184e7356d13f2fb1e5e430220723db5cdb9cbd6ead7bfd3deb419cf41053a932418cbb22a67b581f40bc1f13e",
                                 "304402201b736d1773a124c745586217a75bed5f66c05716fbe8c7db4fdb3c3069741cdd02205083f39c321c1bcadfc8d97e3c791a66273d936abac0c6a2fde2ed46019508e1",
@@ -8156,9 +8464,10 @@ mod tests {
                } );
 
                // anchors: commitment tx with three outputs untrimmed (minimum dust limit)
-               chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
-               chan.feerate_per_kw = 3687;
-               chan.holder_dust_limit_satoshis = 3001;
+               chan.context.value_to_self_msat = 6993000000; // 7000000000 - 7000000
+               chan.context.feerate_per_kw = 3687;
+               chan.context.holder_dust_limit_satoshis = 3001;
+               chan.context.channel_type = ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies();
 
                test_commitment_with_anchors!("3045022100ad6c71569856b2d7ff42e838b4abe74a713426b37f22fa667a195a4c88908c6902202b37272b02a42dc6d9f4f82cab3eaf84ac882d9ed762859e1e75455c2c228377",
                                 "3045022100c970799bcb33f43179eb43b3378a0a61991cf2923f69b36ef12548c3df0e6d500220413dc27d2e39ee583093adfcb7799be680141738babb31cc7b0669a777a31f5d",
@@ -8171,9 +8480,10 @@ mod tests {
                } );
 
                // commitment tx with three outputs untrimmed (maximum feerate)
-               chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
-               chan.feerate_per_kw = 4914;
-               chan.holder_dust_limit_satoshis = 546;
+               chan.context.value_to_self_msat = 6993000000; // 7000000000 - 7000000
+               chan.context.feerate_per_kw = 4914;
+               chan.context.holder_dust_limit_satoshis = 546;
+               chan.context.channel_type = cached_channel_type.clone();
 
                test_commitment!("3045022100b4b16d5f8cc9fc4c1aff48831e832a0d8990e133978a66e302c133550954a44d022073573ce127e2200d316f6b612803a5c0c97b8d20e1e44dbe2ac0dd2fb8c95244",
                                 "3045022100d72638bc6308b88bb6d45861aae83e5b9ff6e10986546e13bce769c70036e2620220320be7c6d66d22f30b9fcd52af66531505b1310ca3b848c19285b38d8a1a8c19",
@@ -8186,63 +8496,67 @@ mod tests {
                } );
 
                // commitment tx with two outputs untrimmed (minimum feerate)
-               chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
-               chan.feerate_per_kw = 4915;
-               chan.holder_dust_limit_satoshis = 546;
+               chan.context.value_to_self_msat = 6993000000; // 7000000000 - 7000000
+               chan.context.feerate_per_kw = 4915;
+               chan.context.holder_dust_limit_satoshis = 546;
 
                test_commitment!("304402203a286936e74870ca1459c700c71202af0381910a6bfab687ef494ef1bc3e02c902202506c362d0e3bee15e802aa729bf378e051644648253513f1c085b264cc2a720",
                                 "30450221008a953551f4d67cb4df3037207fc082ddaf6be84d417b0bd14c80aab66f1b01a402207508796dc75034b2dee876fe01dc05a08b019f3e5d689ac8842ade2f1befccf5",
                                 "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8002c0c62d0000000000160014cc1b07838e387deacd0e5232e1e8b49f4c29e484fa926a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e04004830450221008a953551f4d67cb4df3037207fc082ddaf6be84d417b0bd14c80aab66f1b01a402207508796dc75034b2dee876fe01dc05a08b019f3e5d689ac8842ade2f1befccf50147304402203a286936e74870ca1459c700c71202af0381910a6bfab687ef494ef1bc3e02c902202506c362d0e3bee15e802aa729bf378e051644648253513f1c085b264cc2a72001475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220", {});
 
                // anchors: commitment tx with two outputs untrimmed (minimum dust limit)
-               chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
-               chan.feerate_per_kw = 4894;
-               chan.holder_dust_limit_satoshis = 4001;
+               chan.context.value_to_self_msat = 6993000000; // 7000000000 - 7000000
+               chan.context.feerate_per_kw = 4894;
+               chan.context.holder_dust_limit_satoshis = 4001;
+               chan.context.channel_type = ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies();
 
                test_commitment_with_anchors!("3045022100e784a66b1588575801e237d35e510fd92a81ae3a4a2a1b90c031ad803d07b3f3022021bc5f16501f167607d63b681442da193eb0a76b4b7fd25c2ed4f8b28fd35b95",
                                 "30450221009f16ac85d232e4eddb3fcd750a68ebf0b58e3356eaada45d3513ede7e817bf4c02207c2b043b4e5f971261975406cb955219fa56bffe5d834a833694b5abc1ce4cfd",
                                 "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b80044a010000000000002200202b1b5854183c12d3316565972c4668929d314d81c5dcdbb21cb45fe8a9a8114f4a01000000000000220020e9e86e4823faa62e222ebc858a226636856158f07e69898da3b0d1af0ddb3994c0c62d0000000000220020f3394e1e619b0eca1f91be2fb5ab4dfc59ba5b84ebe014ad1d43a564d012994ad0886a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e04004830450221009f16ac85d232e4eddb3fcd750a68ebf0b58e3356eaada45d3513ede7e817bf4c02207c2b043b4e5f971261975406cb955219fa56bffe5d834a833694b5abc1ce4cfd01483045022100e784a66b1588575801e237d35e510fd92a81ae3a4a2a1b90c031ad803d07b3f3022021bc5f16501f167607d63b681442da193eb0a76b4b7fd25c2ed4f8b28fd35b9501475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220", {});
 
                // commitment tx with two outputs untrimmed (maximum feerate)
-               chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
-               chan.feerate_per_kw = 9651180;
-               chan.holder_dust_limit_satoshis = 546;
+               chan.context.value_to_self_msat = 6993000000; // 7000000000 - 7000000
+               chan.context.feerate_per_kw = 9651180;
+               chan.context.holder_dust_limit_satoshis = 546;
+               chan.context.channel_type = cached_channel_type.clone();
 
                test_commitment!("304402200a8544eba1d216f5c5e530597665fa9bec56943c0f66d98fc3d028df52d84f7002201e45fa5c6bc3a506cc2553e7d1c0043a9811313fc39c954692c0d47cfce2bbd3",
                                 "3045022100e11b638c05c650c2f63a421d36ef8756c5ce82f2184278643520311cdf50aa200220259565fb9c8e4a87ccaf17f27a3b9ca4f20625754a0920d9c6c239d8156a11de",
                                 "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b800222020000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80ec0c62d0000000000160014cc1b07838e387deacd0e5232e1e8b49f4c29e4840400483045022100e11b638c05c650c2f63a421d36ef8756c5ce82f2184278643520311cdf50aa200220259565fb9c8e4a87ccaf17f27a3b9ca4f20625754a0920d9c6c239d8156a11de0147304402200a8544eba1d216f5c5e530597665fa9bec56943c0f66d98fc3d028df52d84f7002201e45fa5c6bc3a506cc2553e7d1c0043a9811313fc39c954692c0d47cfce2bbd301475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220", {});
 
                // commitment tx with one output untrimmed (minimum feerate)
-               chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
-               chan.feerate_per_kw = 9651181;
+               chan.context.value_to_self_msat = 6993000000; // 7000000000 - 7000000
+               chan.context.feerate_per_kw = 9651181;
 
                test_commitment!("304402202ade0142008309eb376736575ad58d03e5b115499709c6db0b46e36ff394b492022037b63d78d66404d6504d4c4ac13be346f3d1802928a6d3ad95a6a944227161a2",
                                 "304402207e8d51e0c570a5868a78414f4e0cbfaed1106b171b9581542c30718ee4eb95ba02203af84194c97adf98898c9afe2f2ed4a7f8dba05a2dfab28ac9d9c604aa49a379",
                                 "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8001c0c62d0000000000160014cc1b07838e387deacd0e5232e1e8b49f4c29e484040047304402207e8d51e0c570a5868a78414f4e0cbfaed1106b171b9581542c30718ee4eb95ba02203af84194c97adf98898c9afe2f2ed4a7f8dba05a2dfab28ac9d9c604aa49a3790147304402202ade0142008309eb376736575ad58d03e5b115499709c6db0b46e36ff394b492022037b63d78d66404d6504d4c4ac13be346f3d1802928a6d3ad95a6a944227161a201475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220", {});
 
                // anchors: commitment tx with one output untrimmed (minimum dust limit)
-               chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
-               chan.feerate_per_kw = 6216010;
-               chan.holder_dust_limit_satoshis = 4001;
+               chan.context.value_to_self_msat = 6993000000; // 7000000000 - 7000000
+               chan.context.feerate_per_kw = 6216010;
+               chan.context.holder_dust_limit_satoshis = 4001;
+               chan.context.channel_type = ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies();
 
                test_commitment_with_anchors!("30450221008fd5dbff02e4b59020d4cd23a3c30d3e287065fda75a0a09b402980adf68ccda022001e0b8b620cd915ddff11f1de32addf23d81d51b90e6841b2cb8dcaf3faa5ecf",
                                 "30450221009ad80792e3038fe6968d12ff23e6888a565c3ddd065037f357445f01675d63f3022018384915e5f1f4ae157e15debf4f49b61c8d9d2b073c7d6f97c4a68caa3ed4c1",
                                 "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b80024a01000000000000220020e9e86e4823faa62e222ebc858a226636856158f07e69898da3b0d1af0ddb3994c0c62d0000000000220020f3394e1e619b0eca1f91be2fb5ab4dfc59ba5b84ebe014ad1d43a564d012994a04004830450221009ad80792e3038fe6968d12ff23e6888a565c3ddd065037f357445f01675d63f3022018384915e5f1f4ae157e15debf4f49b61c8d9d2b073c7d6f97c4a68caa3ed4c1014830450221008fd5dbff02e4b59020d4cd23a3c30d3e287065fda75a0a09b402980adf68ccda022001e0b8b620cd915ddff11f1de32addf23d81d51b90e6841b2cb8dcaf3faa5ecf01475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220", {});
 
                // commitment tx with fee greater than funder amount
-               chan.value_to_self_msat = 6993000000; // 7000000000 - 7000000
-               chan.feerate_per_kw = 9651936;
-               chan.holder_dust_limit_satoshis = 546;
+               chan.context.value_to_self_msat = 6993000000; // 7000000000 - 7000000
+               chan.context.feerate_per_kw = 9651936;
+               chan.context.holder_dust_limit_satoshis = 546;
+               chan.context.channel_type = cached_channel_type;
 
                test_commitment!("304402202ade0142008309eb376736575ad58d03e5b115499709c6db0b46e36ff394b492022037b63d78d66404d6504d4c4ac13be346f3d1802928a6d3ad95a6a944227161a2",
                                 "304402207e8d51e0c570a5868a78414f4e0cbfaed1106b171b9581542c30718ee4eb95ba02203af84194c97adf98898c9afe2f2ed4a7f8dba05a2dfab28ac9d9c604aa49a379",
                                 "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8001c0c62d0000000000160014cc1b07838e387deacd0e5232e1e8b49f4c29e484040047304402207e8d51e0c570a5868a78414f4e0cbfaed1106b171b9581542c30718ee4eb95ba02203af84194c97adf98898c9afe2f2ed4a7f8dba05a2dfab28ac9d9c604aa49a3790147304402202ade0142008309eb376736575ad58d03e5b115499709c6db0b46e36ff394b492022037b63d78d66404d6504d4c4ac13be346f3d1802928a6d3ad95a6a944227161a201475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220", {});
 
                // commitment tx with 3 htlc outputs, 2 offered having the same amount and preimage
-               chan.value_to_self_msat = 7_000_000_000 - 2_000_000;
-               chan.feerate_per_kw = 253;
-               chan.pending_inbound_htlcs.clear();
-               chan.pending_inbound_htlcs.push({
+               chan.context.value_to_self_msat = 7_000_000_000 - 2_000_000;
+               chan.context.feerate_per_kw = 253;
+               chan.context.pending_inbound_htlcs.clear();
+               chan.context.pending_inbound_htlcs.push({
                        let mut out = InboundHTLCOutput{
                                htlc_id: 1,
                                amount_msat: 2000000,
@@ -8253,8 +8567,8 @@ mod tests {
                        out.payment_hash.0 = Sha256::hash(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()).into_inner();
                        out
                });
-               chan.pending_outbound_htlcs.clear();
-               chan.pending_outbound_htlcs.push({
+               chan.context.pending_outbound_htlcs.clear();
+               chan.context.pending_outbound_htlcs.push({
                        let mut out = OutboundHTLCOutput{
                                htlc_id: 6,
                                amount_msat: 5000001,
@@ -8262,11 +8576,12 @@ mod tests {
                                payment_hash: PaymentHash([0; 32]),
                                state: OutboundHTLCState::Committed,
                                source: HTLCSource::dummy(),
+                               skimmed_fee_msat: None,
                        };
                        out.payment_hash.0 = Sha256::hash(&hex::decode("0505050505050505050505050505050505050505050505050505050505050505").unwrap()).into_inner();
                        out
                });
-               chan.pending_outbound_htlcs.push({
+               chan.context.pending_outbound_htlcs.push({
                        let mut out = OutboundHTLCOutput{
                                htlc_id: 5,
                                amount_msat: 5000000,
@@ -8274,6 +8589,7 @@ mod tests {
                                payment_hash: PaymentHash([0; 32]),
                                state: OutboundHTLCState::Committed,
                                source: HTLCSource::dummy(),
+                               skimmed_fee_msat: None,
                        };
                        out.payment_hash.0 = Sha256::hash(&hex::decode("0505050505050505050505050505050505050505050505050505050505050505").unwrap()).into_inner();
                        out
@@ -8297,6 +8613,7 @@ mod tests {
                                  "020000000001014bdccf28653066a2c554cafeffdfe1e678e64a69b056684deb0c4fba909423ec02000000000000000001e1120000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e05004730440220471c9f3ad92e49b13b7b8059f43ecf8f7887b0dccbb9fdb54bfe23d62a8ae332022024bd22fae0740e86a44228c35330da9526fd7306dffb2b9dc362d5e78abef7cc0147304402207157f452f2506d73c315192311893800cfb3cc235cc1185b1cfcc136b55230db022014be242dbc6c5da141fec4034e7f387f74d6ff1899453d72ba957467540e1ecb01008576a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c820120876475527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae67a9142002cc93ebefbb1b73f0af055dcc27a0b504ad7688ac6868fa010000" }
                } );
 
+               chan.context.channel_type = ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies();
                test_commitment_with_anchors!("3044022027b38dfb654c34032ffb70bb43022981652fce923cbbe3cbe7394e2ade8b34230220584195b78da6e25c2e8da6b4308d9db25b65b64975db9266163ef592abb7c725",
                                 "3045022100b4014970d9d7962853f3f85196144671d7d5d87426250f0a5fdaf9a55292e92502205360910c9abb397467e19dbd63d081deb4a3240903114c98cec0a23591b79b76",
                                 "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b80074a010000000000002200202b1b5854183c12d3316565972c4668929d314d81c5dcdbb21cb45fe8a9a8114f4a01000000000000220020e9e86e4823faa62e222ebc858a226636856158f07e69898da3b0d1af0ddb3994d007000000000000220020fe0598d74fee2205cc3672e6e6647706b4f3099713b4661b62482c3addd04a5e881300000000000022002018e40f9072c44350f134bdc887bab4d9bdfc8aa468a25616c80e21757ba5dac7881300000000000022002018e40f9072c44350f134bdc887bab4d9bdfc8aa468a25616c80e21757ba5dac7c0c62d0000000000220020f3394e1e619b0eca1f91be2fb5ab4dfc59ba5b84ebe014ad1d43a564d012994aad9c6a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0400483045022100b4014970d9d7962853f3f85196144671d7d5d87426250f0a5fdaf9a55292e92502205360910c9abb397467e19dbd63d081deb4a3240903114c98cec0a23591b79b7601473044022027b38dfb654c34032ffb70bb43022981652fce923cbbe3cbe7394e2ade8b34230220584195b78da6e25c2e8da6b4308d9db25b65b64975db9266163ef592abb7c72501475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220", {
@@ -8378,7 +8695,7 @@ mod tests {
 
                let node_b_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
                let config = UserConfig::default();
-               let node_a_chan = Channel::<EnforcingSigner>::new_outbound(&feeest, &&keys_provider, &&keys_provider,
+               let node_a_chan = OutboundV1Channel::<EnforcingSigner>::new(&feeest, &&keys_provider, &&keys_provider,
                        node_b_node_id, &channelmanager::provided_init_features(&config), 10000000, 100000, 42, &config, 0, 42).unwrap();
 
                let mut channel_type_features = ChannelTypeFeatures::only_static_remote_key();
@@ -8387,13 +8704,12 @@ mod tests {
                let mut open_channel_msg = node_a_chan.get_open_channel(genesis_block(network).header.block_hash());
                open_channel_msg.channel_type = Some(channel_type_features);
                let node_b_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[7; 32]).unwrap());
-               let res = Channel::<EnforcingSigner>::new_from_req(&feeest, &&keys_provider, &&keys_provider,
+               let res = InboundV1Channel::<EnforcingSigner>::new(&feeest, &&keys_provider, &&keys_provider,
                        node_b_node_id, &channelmanager::provided_channel_type_features(&config),
                        &channelmanager::provided_init_features(&config), &open_channel_msg, 7, &config, 0, &&logger, 42);
                assert!(res.is_ok());
        }
 
-       #[cfg(anchors)]
        #[test]
        fn test_supports_anchors_zero_htlc_tx_fee() {
                // Tests that if both sides support and negotiate `anchors_zero_fee_htlc_tx`, it is the
@@ -8412,34 +8728,33 @@ mod tests {
 
                // It is not enough for just the initiator to signal `option_anchors_zero_fee_htlc_tx`, both
                // need to signal it.
-               let channel_a = Channel::<EnforcingSigner>::new_outbound(
+               let channel_a = OutboundV1Channel::<EnforcingSigner>::new(
                        &fee_estimator, &&keys_provider, &&keys_provider, node_id_b,
                        &channelmanager::provided_init_features(&UserConfig::default()), 10000000, 100000, 42,
                        &config, 0, 42
                ).unwrap();
-               assert!(!channel_a.channel_type.supports_anchors_zero_fee_htlc_tx());
+               assert!(!channel_a.context.channel_type.supports_anchors_zero_fee_htlc_tx());
 
                let mut expected_channel_type = ChannelTypeFeatures::empty();
                expected_channel_type.set_static_remote_key_required();
                expected_channel_type.set_anchors_zero_fee_htlc_tx_required();
 
-               let channel_a = Channel::<EnforcingSigner>::new_outbound(
+               let channel_a = OutboundV1Channel::<EnforcingSigner>::new(
                        &fee_estimator, &&keys_provider, &&keys_provider, node_id_b,
                        &channelmanager::provided_init_features(&config), 10000000, 100000, 42, &config, 0, 42
                ).unwrap();
 
                let open_channel_msg = channel_a.get_open_channel(genesis_block(network).header.block_hash());
-               let channel_b = Channel::<EnforcingSigner>::new_from_req(
+               let channel_b = InboundV1Channel::<EnforcingSigner>::new(
                        &fee_estimator, &&keys_provider, &&keys_provider, node_id_a,
                        &channelmanager::provided_channel_type_features(&config), &channelmanager::provided_init_features(&config),
                        &open_channel_msg, 7, &config, 0, &&logger, 42
                ).unwrap();
 
-               assert_eq!(channel_a.channel_type, expected_channel_type);
-               assert_eq!(channel_b.channel_type, expected_channel_type);
+               assert_eq!(channel_a.context.channel_type, expected_channel_type);
+               assert_eq!(channel_b.context.channel_type, expected_channel_type);
        }
 
-       #[cfg(anchors)]
        #[test]
        fn test_rejects_implicit_simple_anchors() {
                // Tests that if `option_anchors` is being negotiated implicitly through the intersection of
@@ -8461,7 +8776,7 @@ mod tests {
                let raw_init_features = static_remote_key_required | simple_anchors_required;
                let init_features_with_simple_anchors = InitFeatures::from_le_bytes(raw_init_features.to_le_bytes().to_vec());
 
-               let channel_a = Channel::<EnforcingSigner>::new_outbound(
+               let channel_a = OutboundV1Channel::<EnforcingSigner>::new(
                        &fee_estimator, &&keys_provider, &&keys_provider, node_id_b,
                        &channelmanager::provided_init_features(&config), 10000000, 100000, 42, &config, 0, 42
                ).unwrap();
@@ -8472,7 +8787,7 @@ mod tests {
 
                // Since A supports both `static_remote_key` and `option_anchors`, but B only accepts
                // `static_remote_key`, it will fail the channel.
-               let channel_b = Channel::<EnforcingSigner>::new_from_req(
+               let channel_b = InboundV1Channel::<EnforcingSigner>::new(
                        &fee_estimator, &&keys_provider, &&keys_provider, node_id_a,
                        &channelmanager::provided_channel_type_features(&config), &init_features_with_simple_anchors,
                        &open_channel_msg, 7, &config, 0, &&logger, 42
@@ -8480,7 +8795,6 @@ mod tests {
                assert!(channel_b.is_err());
        }
 
-       #[cfg(anchors)]
        #[test]
        fn test_rejects_simple_anchors_channel_type() {
                // Tests that if `option_anchors` is being negotiated through the `channel_type` feature,
@@ -8502,13 +8816,13 @@ mod tests {
                let simple_anchors_raw_features = static_remote_key_required | simple_anchors_required;
                let simple_anchors_init = InitFeatures::from_le_bytes(simple_anchors_raw_features.to_le_bytes().to_vec());
                let simple_anchors_channel_type = ChannelTypeFeatures::from_le_bytes(simple_anchors_raw_features.to_le_bytes().to_vec());
-               assert!(simple_anchors_init.requires_unknown_bits());
-               assert!(simple_anchors_channel_type.requires_unknown_bits());
+               assert!(!simple_anchors_init.requires_unknown_bits());
+               assert!(!simple_anchors_channel_type.requires_unknown_bits());
 
                // First, we'll try to open a channel between A and B where A requests a channel type for
                // the original `option_anchors` feature (non zero fee htlc tx). This should be rejected by
                // B as it's not supported by LDK.
-               let channel_a = Channel::<EnforcingSigner>::new_outbound(
+               let channel_a = OutboundV1Channel::<EnforcingSigner>::new(
                        &fee_estimator, &&keys_provider, &&keys_provider, node_id_b,
                        &channelmanager::provided_init_features(&config), 10000000, 100000, 42, &config, 0, 42
                ).unwrap();
@@ -8516,7 +8830,7 @@ mod tests {
                let mut open_channel_msg = channel_a.get_open_channel(genesis_block(network).header.block_hash());
                open_channel_msg.channel_type = Some(simple_anchors_channel_type.clone());
 
-               let res = Channel::<EnforcingSigner>::new_from_req(
+               let res = InboundV1Channel::<EnforcingSigner>::new(
                        &fee_estimator, &&keys_provider, &&keys_provider, node_id_a,
                        &channelmanager::provided_channel_type_features(&config), &simple_anchors_init,
                        &open_channel_msg, 7, &config, 0, &&logger, 42
@@ -8527,14 +8841,14 @@ mod tests {
                // `anchors_zero_fee_htlc_tx`. B is malicious and tries to downgrade the channel type to the
                // original `option_anchors` feature, which should be rejected by A as it's not supported by
                // LDK.
-               let mut channel_a = Channel::<EnforcingSigner>::new_outbound(
+               let mut channel_a = OutboundV1Channel::<EnforcingSigner>::new(
                        &fee_estimator, &&keys_provider, &&keys_provider, node_id_b, &simple_anchors_init,
                        10000000, 100000, 42, &config, 0, 42
                ).unwrap();
 
                let open_channel_msg = channel_a.get_open_channel(genesis_block(network).header.block_hash());
 
-               let channel_b = Channel::<EnforcingSigner>::new_from_req(
+               let channel_b = InboundV1Channel::<EnforcingSigner>::new(
                        &fee_estimator, &&keys_provider, &&keys_provider, node_id_a,
                        &channelmanager::provided_channel_type_features(&config), &channelmanager::provided_init_features(&config),
                        &open_channel_msg, 7, &config, 0, &&logger, 42
index f6cb81376e2490a205127760d5d6f14f0798bf0c..1fa5bd4d519f3bc49e566c7f055b4b46f1a04c0f 100644 (file)
@@ -19,7 +19,7 @@
 
 use bitcoin::blockdata::block::BlockHeader;
 use bitcoin::blockdata::transaction::Transaction;
-use bitcoin::blockdata::constants::genesis_block;
+use bitcoin::blockdata::constants::{genesis_block, ChainHash};
 use bitcoin::network::constants::Network;
 
 use bitcoin::hashes::Hash;
@@ -40,7 +40,7 @@ use crate::events::{Event, EventHandler, EventsProvider, MessageSendEvent, Messa
 // Since this struct is returned in `list_channels` methods, expose it here in case users want to
 // construct one themselves.
 use crate::ln::{inbound_payment, PaymentHash, PaymentPreimage, PaymentSecret};
-use crate::ln::channel::{Channel, ChannelError, ChannelUpdateStatus, UpdateFulfillCommitFetch};
+use crate::ln::channel::{Channel, ChannelContext, ChannelError, ChannelUpdateStatus, ShutdownResult, UpdateFulfillCommitFetch, OutboundV1Channel, InboundV1Channel};
 use crate::ln::features::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, NodeFeatures};
 #[cfg(any(feature = "_test_utils", test))]
 use crate::ln::features::InvoiceFeatures;
@@ -50,13 +50,13 @@ use crate::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringFeeParame
 use crate::ln::msgs;
 use crate::ln::onion_utils;
 use crate::ln::onion_utils::HTLCFailReason;
-use crate::ln::msgs::{ChannelMessageHandler, DecodeError, LightningError, MAX_VALUE_MSAT};
+use crate::ln::msgs::{ChannelMessageHandler, DecodeError, LightningError};
 #[cfg(test)]
 use crate::ln::outbound_payment;
 use crate::ln::outbound_payment::{OutboundPayments, PaymentAttempts, PendingOutboundPayment};
 use crate::ln::wire::Encode;
 use crate::sign::{EntropySource, KeysManager, NodeSigner, Recipient, SignerProvider, ChannelSigner, WriteableEcdsaChannelSigner};
-use crate::util::config::{UserConfig, ChannelConfig};
+use crate::util::config::{UserConfig, ChannelConfig, ChannelConfigUpdate};
 use crate::util::wakers::{Future, Notifier};
 use crate::util::scid_utils::fake_scid;
 use crate::util::string::UntrustedString;
@@ -112,6 +112,8 @@ pub(super) enum PendingHTLCRouting {
                phantom_shared_secret: Option<[u8; 32]>,
        },
        ReceiveKeysend {
+               /// This was added in 0.0.116 and will break deserialization on downgrades.
+               payment_data: Option<msgs::FinalOnionHopData>,
                payment_preimage: PaymentPreimage,
                payment_metadata: Option<Vec<u8>>,
                incoming_cltv_expiry: u32, // Used to track when we should expire pending HTLCs that go unclaimed
@@ -129,6 +131,9 @@ pub(super) struct PendingHTLCInfo {
        /// may overshoot this in either case)
        pub(super) outgoing_amt_msat: u64,
        pub(super) outgoing_cltv_value: u32,
+       /// The fee being skimmed off the top of this HTLC. If this is a forward, it'll be the fee we are
+       /// skimming. If we're receiving this HTLC, it's the fee that our counterparty skimmed.
+       pub(super) skimmed_fee_msat: Option<u64>,
 }
 
 #[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
@@ -208,6 +213,8 @@ struct ClaimableHTLC {
        total_value_received: Option<u64>,
        /// The sender intended sum total of all MPP parts specified in the onion
        total_msat: u64,
+       /// The extra fee our counterparty skimmed off the top of this HTLC.
+       counterparty_skimmed_fee_msat: Option<u64>,
 }
 
 /// A payment identifier used to uniquely identify a payment to LDK.
@@ -310,7 +317,7 @@ impl core::hash::Hash for HTLCSource {
        }
 }
 impl HTLCSource {
-       #[cfg(not(feature = "grind_signatures"))]
+       #[cfg(all(feature = "_test_vectors", not(feature = "grind_signatures")))]
        #[cfg(test)]
        pub fn dummy() -> Self {
                HTLCSource::OutboundRoute {
@@ -359,8 +366,6 @@ pub enum FailureCode {
        IncorrectOrUnknownPaymentDetails = 0x4000 | 15,
 }
 
-type ShutdownResult = (Option<(OutPoint, ChannelMonitorUpdate)>, Vec<(HTLCSource, PaymentHash, PublicKey, [u8; 32])>);
-
 /// Error type returned across the peer_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
@@ -497,15 +502,34 @@ struct ClaimablePayments {
        pending_claiming_payments: HashMap<PaymentHash, ClaimingPayment>,
 }
 
-/// Events which we process internally but cannot be procsesed immediately at the generation site
-/// for some reason. They are handled in timer_tick_occurred, so may be processed with
-/// quite some time lag.
+/// Events which we process internally but cannot be processed immediately at the generation site
+/// usually because we're running pre-full-init. They are handled immediately once we detect we are
+/// running normally, and specifically must be processed before any other non-background
+/// [`ChannelMonitorUpdate`]s are applied.
 enum BackgroundEvent {
-       /// Handle a ChannelMonitorUpdate
+       /// Handle a ChannelMonitorUpdate which closes the channel. This is only separated from
+       /// [`Self::MonitorUpdateRegeneratedOnStartup`] as the maybe-non-closing variant needs a public
+       /// key to handle channel resumption, whereas if the channel has been force-closed we do not
+       /// need the counterparty node_id.
+       ///
+       /// Note that any such events are lost on shutdown, so in general they must be updates which
+       /// are regenerated on startup.
+       ClosingMonitorUpdateRegeneratedOnStartup((OutPoint, ChannelMonitorUpdate)),
+       /// Handle a ChannelMonitorUpdate which may or may not close the channel and may unblock the
+       /// channel to continue normal operation.
+       ///
+       /// In general this should be used rather than
+       /// [`Self::ClosingMonitorUpdateRegeneratedOnStartup`], however in cases where the
+       /// `counterparty_node_id` is not available as the channel has closed from a [`ChannelMonitor`]
+       /// error the other variant is acceptable.
        ///
        /// Note that any such events are lost on shutdown, so in general they must be updates which
        /// are regenerated on startup.
-       MonitorUpdateRegeneratedOnStartup((OutPoint, ChannelMonitorUpdate)),
+       MonitorUpdateRegeneratedOnStartup {
+               counterparty_node_id: PublicKey,
+               funding_txo: OutPoint,
+               update: ChannelMonitorUpdate
+       },
 }
 
 #[derive(Debug)]
@@ -515,13 +539,31 @@ pub(crate) enum MonitorUpdateCompletionAction {
        /// this payment. Note that this is only best-effort. On restart it's possible such a duplicate
        /// event can be generated.
        PaymentClaimed { payment_hash: PaymentHash },
-       /// Indicates an [`events::Event`] should be surfaced to the user.
-       EmitEvent { event: events::Event },
+       /// Indicates an [`events::Event`] should be surfaced to the user and possibly resume the
+       /// operation of another channel.
+       ///
+       /// This is usually generated when we've forwarded an HTLC and want to block the outbound edge
+       /// from completing a monitor update which removes the payment preimage until the inbound edge
+       /// completes a monitor update containing the payment preimage. In that case, after the inbound
+       /// edge completes, we will surface an [`Event::PaymentForwarded`] as well as unblock the
+       /// outbound edge.
+       EmitEventAndFreeOtherChannel {
+               event: events::Event,
+               downstream_counterparty_and_funding_outpoint: Option<(PublicKey, OutPoint, RAAMonitorUpdateBlockingAction)>,
+       },
 }
 
 impl_writeable_tlv_based_enum_upgradable!(MonitorUpdateCompletionAction,
        (0, PaymentClaimed) => { (0, payment_hash, required) },
-       (2, EmitEvent) => { (0, event, upgradable_required) },
+       (2, EmitEventAndFreeOtherChannel) => {
+               (0, event, upgradable_required),
+               // LDK prior to 0.0.116 did not have this field as the monitor update application order was
+               // required by clients. If we downgrade to something prior to 0.0.116 this may result in
+               // monitor updates which aren't properly blocked or resumed, however that's fine - we don't
+               // support async monitor updates even in LDK 0.0.116 and once we do we'll require no
+               // downgrades to prior versions.
+               (1, downstream_counterparty_and_funding_outpoint, option),
+       },
 );
 
 #[derive(Clone, Debug, PartialEq, Eq)]
@@ -538,19 +580,66 @@ impl_writeable_tlv_based_enum!(EventCompletionAction,
        };
 );
 
+#[derive(Clone, PartialEq, Eq, Debug)]
+/// If something is blocked on the completion of an RAA-generated [`ChannelMonitorUpdate`] we track
+/// the blocked action here. See enum variants for more info.
+pub(crate) enum RAAMonitorUpdateBlockingAction {
+       /// A forwarded payment was claimed. We block the downstream channel completing its monitor
+       /// update which removes the HTLC preimage until the upstream channel has gotten the preimage
+       /// durably to disk.
+       ForwardedPaymentInboundClaim {
+               /// The upstream channel ID (i.e. the inbound edge).
+               channel_id: [u8; 32],
+               /// The HTLC ID on the inbound edge.
+               htlc_id: u64,
+       },
+}
+
+impl RAAMonitorUpdateBlockingAction {
+       #[allow(unused)]
+       fn from_prev_hop_data(prev_hop: &HTLCPreviousHopData) -> Self {
+               Self::ForwardedPaymentInboundClaim {
+                       channel_id: prev_hop.outpoint.to_channel_id(),
+                       htlc_id: prev_hop.htlc_id,
+               }
+       }
+}
+
+impl_writeable_tlv_based_enum!(RAAMonitorUpdateBlockingAction,
+       (0, ForwardedPaymentInboundClaim) => { (0, channel_id, required), (2, htlc_id, required) }
+;);
+
+
 /// State we hold per-peer.
 pub(super) struct PeerState<Signer: ChannelSigner> {
-       /// `temporary_channel_id` or `channel_id` -> `channel`.
+       /// `channel_id` -> `Channel`.
        ///
-       /// Holds all channels where the peer is the counterparty. Once a channel has been assigned a
-       /// `channel_id`, the `temporary_channel_id` key in the map is updated and is replaced by the
-       /// `channel_id`.
+       /// Holds all funded channels where the peer is the counterparty.
        pub(super) channel_by_id: HashMap<[u8; 32], Channel<Signer>>,
+       /// `temporary_channel_id` -> `OutboundV1Channel`.
+       ///
+       /// Holds all outbound V1 channels where the peer is the counterparty. Once an outbound channel has
+       /// been assigned a `channel_id`, the entry in this map is removed and one is created in
+       /// `channel_by_id`.
+       pub(super) outbound_v1_channel_by_id: HashMap<[u8; 32], OutboundV1Channel<Signer>>,
+       /// `temporary_channel_id` -> `InboundV1Channel`.
+       ///
+       /// Holds all inbound V1 channels where the peer is the counterparty. Once an inbound channel has
+       /// been assigned a `channel_id`, the entry in this map is removed and one is created in
+       /// `channel_by_id`.
+       pub(super) inbound_v1_channel_by_id: HashMap<[u8; 32], InboundV1Channel<Signer>>,
        /// The latest `InitFeatures` we heard from the peer.
        latest_features: InitFeatures,
        /// Messages to send to the peer - 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<MessageSendEvent>,
+       /// Map from Channel IDs to pending [`ChannelMonitorUpdate`]s which have been passed to the
+       /// user but which have not yet completed.
+       ///
+       /// Note that the channel may no longer exist. For example if the channel was closed but we
+       /// later needed to claim an HTLC which is pending on-chain, we may generate a monitor update
+       /// for a missing channel.
+       in_flight_monitor_updates: BTreeMap<OutPoint, Vec<ChannelMonitorUpdate>>,
        /// Map from a specific channel to some action(s) that should be taken when all pending
        /// [`ChannelMonitorUpdate`]s for the channel complete updating.
        ///
@@ -566,6 +655,11 @@ pub(super) struct PeerState<Signer: ChannelSigner> {
        /// to funding appearing on-chain), the downstream `ChannelMonitor` set is required to ensure
        /// duplicates do not occur, so such channels should fail without a monitor update completing.
        monitor_update_blocked_actions: BTreeMap<[u8; 32], Vec<MonitorUpdateCompletionAction>>,
+       /// If another channel's [`ChannelMonitorUpdate`] needs to complete before a channel we have
+       /// with this peer can complete an RAA [`ChannelMonitorUpdate`] (e.g. because the RAA update
+       /// will remove a preimage that needs to be durably in an upstream channel first), we put an
+       /// entry here to note that the channel with the key's ID is blocked on a set of actions.
+       actions_blocking_raa_monitor_updates: BTreeMap<[u8; 32], Vec<RAAMonitorUpdateBlockingAction>>,
        /// The peer is currently connected (i.e. we've seen a
        /// [`ChannelMessageHandler::peer_connected`] and no corresponding
        /// [`ChannelMessageHandler::peer_disconnected`].
@@ -581,6 +675,21 @@ impl <Signer: ChannelSigner> PeerState<Signer> {
                        return false
                }
                self.channel_by_id.is_empty() && self.monitor_update_blocked_actions.is_empty()
+                       && self.in_flight_monitor_updates.is_empty()
+       }
+
+       // Returns a count of all channels we have with this peer, including pending channels.
+       fn total_channel_count(&self) -> usize {
+               self.channel_by_id.len() +
+                       self.outbound_v1_channel_by_id.len() +
+                       self.inbound_v1_channel_by_id.len()
+       }
+
+       // Returns a bool indicating if the given `channel_id` matches a channel we have with this peer.
+       fn has_channel(&self, channel_id: &[u8; 32]) -> bool {
+               self.channel_by_id.contains_key(channel_id) ||
+                       self.outbound_v1_channel_by_id.contains_key(channel_id) ||
+                       self.inbound_v1_channel_by_id.contains_key(channel_id)
        }
 }
 
@@ -643,42 +752,62 @@ pub type SimpleArcChannelManager<M, T, F, L> = ChannelManager<
 /// of [`KeysManager`] and [`DefaultRouter`].
 ///
 /// This is not exported to bindings users as Arcs don't make sense in bindings
-pub type SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, M, T, F, L> = ChannelManager<&'a M, &'b T, &'c KeysManager, &'c KeysManager, &'c KeysManager, &'d F, &'e DefaultRouter<&'f NetworkGraph<&'g L>, &'g L, &'h Mutex<ProbabilisticScorer<&'f NetworkGraph<&'g L>, &'g L>>, ProbabilisticScoringFeeParameters, ProbabilisticScorer<&'f NetworkGraph<&'g L>, &'g L>>, &'g L>;
-
+pub type SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, M, T, F, L> =
+       ChannelManager<
+               &'a M,
+               &'b T,
+               &'c KeysManager,
+               &'c KeysManager,
+               &'c KeysManager,
+               &'d F,
+               &'e DefaultRouter<
+                       &'f NetworkGraph<&'g L>,
+                       &'g L,
+                       &'h Mutex<ProbabilisticScorer<&'f NetworkGraph<&'g L>, &'g L>>,
+                       ProbabilisticScoringFeeParameters,
+                       ProbabilisticScorer<&'f NetworkGraph<&'g L>, &'g L>
+               >,
+               &'g L
+       >;
+
+macro_rules! define_test_pub_trait { ($vis: vis) => {
 /// A trivial trait which describes any [`ChannelManager`] used in testing.
-#[cfg(any(test, feature = "_test_utils"))]
-pub trait AChannelManager {
-       type Watch: chain::Watch<Self::Signer>;
+$vis trait AChannelManager {
+       type Watch: chain::Watch<Self::Signer> + ?Sized;
        type M: Deref<Target = Self::Watch>;
-       type Broadcaster: BroadcasterInterface;
+       type Broadcaster: BroadcasterInterface + ?Sized;
        type T: Deref<Target = Self::Broadcaster>;
-       type EntropySource: EntropySource;
+       type EntropySource: EntropySource + ?Sized;
        type ES: Deref<Target = Self::EntropySource>;
-       type NodeSigner: NodeSigner;
+       type NodeSigner: NodeSigner + ?Sized;
        type NS: Deref<Target = Self::NodeSigner>;
-       type Signer: WriteableEcdsaChannelSigner;
-       type SignerProvider: SignerProvider<Signer = Self::Signer>;
+       type Signer: WriteableEcdsaChannelSigner + Sized;
+       type SignerProvider: SignerProvider<Signer = Self::Signer> + ?Sized;
        type SP: Deref<Target = Self::SignerProvider>;
-       type FeeEstimator: FeeEstimator;
+       type FeeEstimator: FeeEstimator + ?Sized;
        type F: Deref<Target = Self::FeeEstimator>;
-       type Router: Router;
+       type Router: Router + ?Sized;
        type R: Deref<Target = Self::Router>;
-       type Logger: Logger;
+       type Logger: Logger + ?Sized;
        type L: Deref<Target = Self::Logger>;
        fn get_cm(&self) -> &ChannelManager<Self::M, Self::T, Self::ES, Self::NS, Self::SP, Self::F, Self::R, Self::L>;
 }
+} }
 #[cfg(any(test, feature = "_test_utils"))]
+define_test_pub_trait!(pub);
+#[cfg(not(any(test, feature = "_test_utils")))]
+define_test_pub_trait!(pub(crate));
 impl<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref> AChannelManager
 for ChannelManager<M, T, ES, NS, SP, F, R, L>
 where
-       M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer> + Sized,
-       T::Target: BroadcasterInterface + Sized,
-       ES::Target: EntropySource + Sized,
-       NS::Target: NodeSigner + Sized,
-       SP::Target: SignerProvider + Sized,
-       F::Target: FeeEstimator + Sized,
-       R::Target: Router + Sized,
-       L::Target: Logger + Sized,
+       M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer>,
+       T::Target: BroadcasterInterface,
+       ES::Target: EntropySource,
+       NS::Target: NodeSigner,
+       SP::Target: SignerProvider,
+       F::Target: FeeEstimator,
+       R::Target: Router,
+       L::Target: Logger,
 {
        type Watch = M::Target;
        type M = M;
@@ -964,7 +1093,18 @@ where
        pending_events: Mutex<VecDeque<(events::Event, Option<EventCompletionAction>)>>,
        /// A simple atomic flag to ensure only one task at a time can be processing events asynchronously.
        pending_events_processor: AtomicBool,
+
+       /// If we are running during init (either directly during the deserialization method or in
+       /// block connection methods which run after deserialization but before normal operation) we
+       /// cannot provide the user with [`ChannelMonitorUpdate`]s through the normal update flow -
+       /// prior to normal operation the user may not have loaded the [`ChannelMonitor`]s into their
+       /// [`ChainMonitor`] and thus attempting to update it will fail or panic.
+       ///
+       /// Thus, we place them here to be handled as soon as possible once we are running normally.
+       ///
        /// See `ChannelManager` struct-level documentation for lock order requirements.
+       ///
+       /// [`ChainMonitor`]: crate::chain::chainmonitor::ChainMonitor
        pending_background_events: Mutex<Vec<BackgroundEvent>>,
        /// Used when we have to take a BIG lock to make sure everything is self-consistent.
        /// Essentially just when we're serializing ourselves out.
@@ -974,6 +1114,9 @@ where
        /// Notifier the lock contains sends out a notification when the lock is released.
        total_consistency_lock: RwLock<()>,
 
+       #[cfg(debug_assertions)]
+       background_events_processed_since_startup: AtomicBool,
+
        persistence_notifier: Notifier,
 
        entropy_source: ES,
@@ -1000,6 +1143,7 @@ pub struct ChainParameters {
 }
 
 #[derive(Copy, Clone, PartialEq)]
+#[must_use]
 enum NotifyOption {
        DoPersist,
        SkipPersist,
@@ -1023,10 +1167,20 @@ struct PersistenceNotifierGuard<'a, F: Fn() -> NotifyOption> {
 }
 
 impl<'a> PersistenceNotifierGuard<'a, fn() -> NotifyOption> { // We don't care what the concrete F is here, it's unused
-       fn notify_on_drop(lock: &'a RwLock<()>, notifier: &'a Notifier) -> PersistenceNotifierGuard<'a, impl Fn() -> NotifyOption> {
-               PersistenceNotifierGuard::optionally_notify(lock, notifier, || -> NotifyOption { NotifyOption::DoPersist })
+       fn notify_on_drop<C: AChannelManager>(cm: &'a C) -> PersistenceNotifierGuard<'a, impl Fn() -> NotifyOption> {
+               let read_guard = cm.get_cm().total_consistency_lock.read().unwrap();
+               let _ = cm.get_cm().process_background_events(); // We always persist
+
+               PersistenceNotifierGuard {
+                       persistence_notifier: &cm.get_cm().persistence_notifier,
+                       should_persist: || -> NotifyOption { NotifyOption::DoPersist },
+                       _read_guard: read_guard,
+               }
+
        }
 
+       /// Note that if any [`ChannelMonitorUpdate`]s are possibly generated,
+       /// [`ChannelManager::process_background_events`] MUST be called first.
        fn optionally_notify<F: Fn() -> NotifyOption>(lock: &'a RwLock<()>, notifier: &'a Notifier, persist_check: F) -> PersistenceNotifierGuard<'a, F> {
                let read_guard = lock.read().unwrap();
 
@@ -1274,8 +1428,14 @@ pub struct ChannelDetails {
        /// the current state and per-HTLC limit(s). This is intended for use when routing, allowing us
        /// to use a limit as close as possible to the HTLC limit we can currently send.
        ///
-       /// See also [`ChannelDetails::balance_msat`] and [`ChannelDetails::outbound_capacity_msat`].
+       /// See also [`ChannelDetails::next_outbound_htlc_minimum_msat`],
+       /// [`ChannelDetails::balance_msat`], and [`ChannelDetails::outbound_capacity_msat`].
        pub next_outbound_htlc_limit_msat: u64,
+       /// The minimum value for sending a single HTLC to the remote peer. This is the equivalent of
+       /// [`ChannelDetails::next_outbound_htlc_limit_msat`] but represents a lower-bound, rather than
+       /// an upper-bound. This is intended for use when routing, allowing us to ensure we pick a
+       /// route which is valid.
+       pub next_outbound_htlc_minimum_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, whose balance is not
        /// available for inclusion in new inbound HTLCs).
@@ -1320,6 +1480,9 @@ pub struct ChannelDetails {
        ///
        /// [`confirmations_required`]: ChannelDetails::confirmations_required
        pub is_channel_ready: bool,
+       /// The stage of the channel's shutdown.
+       /// `None` for `ChannelDetails` serialized on LDK versions prior to 0.0.116.
+       pub channel_shutdown_state: Option<ChannelShutdownState>,
        /// True if the channel is (a) confirmed and channel_ready messages have been exchanged, (b)
        /// the peer is connected, and (c) the channel is not currently negotiating a shutdown.
        ///
@@ -1359,57 +1522,84 @@ impl ChannelDetails {
                self.short_channel_id.or(self.outbound_scid_alias)
        }
 
-       fn from_channel<Signer: WriteableEcdsaChannelSigner>(channel: &Channel<Signer>,
-               best_block_height: u32, latest_features: InitFeatures) -> Self {
-
-               let balance = channel.get_available_balances();
+       fn from_channel_context<Signer: WriteableEcdsaChannelSigner, F: Deref>(
+               context: &ChannelContext<Signer>, best_block_height: u32, latest_features: InitFeatures,
+               fee_estimator: &LowerBoundedFeeEstimator<F>
+       ) -> Self
+       where F::Target: FeeEstimator
+       {
+               let balance = context.get_available_balances(fee_estimator);
                let (to_remote_reserve_satoshis, to_self_reserve_satoshis) =
-                       channel.get_holder_counterparty_selected_channel_reserve_satoshis();
+                       context.get_holder_counterparty_selected_channel_reserve_satoshis();
                ChannelDetails {
-                       channel_id: channel.channel_id(),
+                       channel_id: context.channel_id(),
                        counterparty: ChannelCounterparty {
-                               node_id: channel.get_counterparty_node_id(),
+                               node_id: context.get_counterparty_node_id(),
                                features: latest_features,
                                unspendable_punishment_reserve: to_remote_reserve_satoshis,
-                               forwarding_info: channel.counterparty_forwarding_info(),
+                               forwarding_info: context.counterparty_forwarding_info(),
                                // Ensures that we have actually received the `htlc_minimum_msat` value
                                // from the counterparty through the `OpenChannel` or `AcceptChannel`
                                // message (as they are always the first message from the counterparty).
                                // Else `Channel::get_counterparty_htlc_minimum_msat` could return the
                                // default `0` value set by `Channel::new_outbound`.
-                               outbound_htlc_minimum_msat: if channel.have_received_message() {
-                                       Some(channel.get_counterparty_htlc_minimum_msat()) } else { None },
-                               outbound_htlc_maximum_msat: channel.get_counterparty_htlc_maximum_msat(),
+                               outbound_htlc_minimum_msat: if context.have_received_message() {
+                                       Some(context.get_counterparty_htlc_minimum_msat()) } else { None },
+                               outbound_htlc_maximum_msat: context.get_counterparty_htlc_maximum_msat(),
                        },
-                       funding_txo: channel.get_funding_txo(),
+                       funding_txo: context.get_funding_txo(),
                        // Note that accept_channel (or open_channel) is always the first message, so
                        // `have_received_message` indicates that type negotiation has completed.
-                       channel_type: if channel.have_received_message() { Some(channel.get_channel_type().clone()) } else { None },
-                       short_channel_id: channel.get_short_channel_id(),
-                       outbound_scid_alias: if channel.is_usable() { Some(channel.outbound_scid_alias()) } else { None },
-                       inbound_scid_alias: channel.latest_inbound_scid_alias(),
-                       channel_value_satoshis: channel.get_value_satoshis(),
-                       feerate_sat_per_1000_weight: Some(channel.get_feerate_sat_per_1000_weight()),
+                       channel_type: if context.have_received_message() { Some(context.get_channel_type().clone()) } else { None },
+                       short_channel_id: context.get_short_channel_id(),
+                       outbound_scid_alias: if context.is_usable() { Some(context.outbound_scid_alias()) } else { None },
+                       inbound_scid_alias: context.latest_inbound_scid_alias(),
+                       channel_value_satoshis: context.get_value_satoshis(),
+                       feerate_sat_per_1000_weight: Some(context.get_feerate_sat_per_1000_weight()),
                        unspendable_punishment_reserve: to_self_reserve_satoshis,
                        balance_msat: balance.balance_msat,
                        inbound_capacity_msat: balance.inbound_capacity_msat,
                        outbound_capacity_msat: balance.outbound_capacity_msat,
                        next_outbound_htlc_limit_msat: balance.next_outbound_htlc_limit_msat,
-                       user_channel_id: channel.get_user_id(),
-                       confirmations_required: channel.minimum_depth(),
-                       confirmations: Some(channel.get_funding_tx_confirmations(best_block_height)),
-                       force_close_spend_delay: channel.get_counterparty_selected_contest_delay(),
-                       is_outbound: channel.is_outbound(),
-                       is_channel_ready: channel.is_usable(),
-                       is_usable: channel.is_live(),
-                       is_public: channel.should_announce(),
-                       inbound_htlc_minimum_msat: Some(channel.get_holder_htlc_minimum_msat()),
-                       inbound_htlc_maximum_msat: channel.get_holder_htlc_maximum_msat(),
-                       config: Some(channel.config()),
+                       next_outbound_htlc_minimum_msat: balance.next_outbound_htlc_minimum_msat,
+                       user_channel_id: context.get_user_id(),
+                       confirmations_required: context.minimum_depth(),
+                       confirmations: Some(context.get_funding_tx_confirmations(best_block_height)),
+                       force_close_spend_delay: context.get_counterparty_selected_contest_delay(),
+                       is_outbound: context.is_outbound(),
+                       is_channel_ready: context.is_usable(),
+                       is_usable: context.is_live(),
+                       is_public: context.should_announce(),
+                       inbound_htlc_minimum_msat: Some(context.get_holder_htlc_minimum_msat()),
+                       inbound_htlc_maximum_msat: context.get_holder_htlc_maximum_msat(),
+                       config: Some(context.config()),
+                       channel_shutdown_state: Some(context.shutdown_state()),
                }
        }
 }
 
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+/// Further information on the details of the channel shutdown.
+/// Upon channels being forced closed (i.e. commitment transaction confirmation detected
+/// by `ChainMonitor`), ChannelShutdownState will be set to `ShutdownComplete` or
+/// the channel will be removed shortly.
+/// Also note, that in normal operation, peers could disconnect at any of these states
+/// and require peer re-connection before making progress onto other states
+pub enum ChannelShutdownState {
+       /// Channel has not sent or received a shutdown message.
+       NotShuttingDown,
+       /// Local node has sent a shutdown message for this channel.
+       ShutdownInitiated,
+       /// Shutdown message exchanges have concluded and the channels are in the midst of
+       /// resolving all existing open HTLCs before closing can continue.
+       ResolvingHTLCs,
+       /// All HTLCs have been resolved, nodes are currently negotiating channel close onchain fee rates.
+       NegotiatingClosingFee,
+       /// We've successfully negotiated a closing_signed dance. At this point `ChannelManager` is about
+       /// to drop the channel.
+       ShutdownComplete,
+}
+
 /// Used by [`ChannelManager::list_recent_payments`] to express the status of recent payments.
 /// These include payments that have yet to find a successful path, or have unresolved HTLCs.
 #[derive(Debug, PartialEq)]
@@ -1502,14 +1692,23 @@ macro_rules! handle_error {
                                Err(err)
                        },
                }
-       } }
+       } };
+       ($self: ident, $internal: expr) => {
+               match $internal {
+                       Ok(res) => Ok(res),
+                       Err((chan, msg_handle_err)) => {
+                               let counterparty_node_id = chan.get_counterparty_node_id();
+                               handle_error!($self, Err(msg_handle_err), counterparty_node_id).map_err(|err| (chan, err))
+                       },
+               }
+       };
 }
 
 macro_rules! update_maps_on_chan_removal {
-       ($self: expr, $channel: expr) => {{
-               $self.id_to_peer.lock().unwrap().remove(&$channel.channel_id());
+       ($self: expr, $channel_context: expr) => {{
+               $self.id_to_peer.lock().unwrap().remove(&$channel_context.channel_id());
                let mut short_to_chan_info = $self.short_to_chan_info.write().unwrap();
-               if let Some(short_id) = $channel.get_short_channel_id() {
+               if let Some(short_id) = $channel_context.get_short_channel_id() {
                        short_to_chan_info.remove(&short_id);
                } else {
                        // If the channel was never confirmed on-chain prior to its closure, remove the
@@ -1518,10 +1717,10 @@ macro_rules! update_maps_on_chan_removal {
                        // also don't want a counterparty to be able to trivially cause a memory leak by simply
                        // opening a million channels with us which are closed before we ever reach the funding
                        // stage.
-                       let alias_removed = $self.outbound_scid_aliases.lock().unwrap().remove(&$channel.outbound_scid_alias());
+                       let alias_removed = $self.outbound_scid_aliases.lock().unwrap().remove(&$channel_context.outbound_scid_alias());
                        debug_assert!(alias_removed);
                }
-               short_to_chan_info.remove(&$channel.outbound_scid_alias());
+               short_to_chan_info.remove(&$channel_context.outbound_scid_alias());
        }}
 }
 
@@ -1537,12 +1736,25 @@ macro_rules! convert_chan_err {
                        },
                        ChannelError::Close(msg) => {
                                log_error!($self.logger, "Closing channel {} due to close-required error: {}", log_bytes!($channel_id[..]), msg);
-                               update_maps_on_chan_removal!($self, $channel);
-                               let shutdown_res = $channel.force_shutdown(true);
-                               (true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, $channel.get_user_id(),
+                               update_maps_on_chan_removal!($self, &$channel.context);
+                               let shutdown_res = $channel.context.force_shutdown(true);
+                               (true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, $channel.context.get_user_id(),
                                        shutdown_res, $self.get_channel_update_for_broadcast(&$channel).ok()))
                        },
                }
+       };
+       ($self: ident, $err: expr, $channel_context: expr, $channel_id: expr, PREFUNDED) => {
+               match $err {
+                       // We should only ever have `ChannelError::Close` when prefunded channels error.
+                       // In any case, just close the channel.
+                       ChannelError::Warn(msg) | ChannelError::Ignore(msg) | ChannelError::Close(msg) => {
+                               log_error!($self.logger, "Closing prefunded channel {} due to an error: {}", log_bytes!($channel_id[..]), msg);
+                               update_maps_on_chan_removal!($self, &$channel_context);
+                               let shutdown_res = $channel_context.force_shutdown(false);
+                               (true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, $channel_context.get_user_id(),
+                                       shutdown_res, None))
+                       },
+               }
        }
 }
 
@@ -1561,6 +1773,21 @@ macro_rules! break_chan_entry {
        }
 }
 
+macro_rules! try_v1_outbound_chan_entry {
+       ($self: ident, $res: expr, $entry: expr) => {
+               match $res {
+                       Ok(res) => res,
+                       Err(e) => {
+                               let (drop, res) = convert_chan_err!($self, e, $entry.get_mut().context, $entry.key(), PREFUNDED);
+                               if drop {
+                                       $entry.remove_entry();
+                               }
+                               return Err(res);
+                       }
+               }
+       }
+}
+
 macro_rules! try_chan_entry {
        ($self: ident, $res: expr, $entry: expr) => {
                match $res {
@@ -1580,7 +1807,7 @@ macro_rules! remove_channel {
        ($self: expr, $entry: expr) => {
                {
                        let channel = $entry.remove_entry().1;
-                       update_maps_on_chan_removal!($self, channel);
+                       update_maps_on_chan_removal!($self, &channel.context);
                        channel
                }
        }
@@ -1589,18 +1816,18 @@ macro_rules! remove_channel {
 macro_rules! send_channel_ready {
        ($self: ident, $pending_msg_events: expr, $channel: expr, $channel_ready_msg: expr) => {{
                $pending_msg_events.push(events::MessageSendEvent::SendChannelReady {
-                       node_id: $channel.get_counterparty_node_id(),
+                       node_id: $channel.context.get_counterparty_node_id(),
                        msg: $channel_ready_msg,
                });
                // Note that we may send a `channel_ready` multiple times for a channel if we reconnect, so
                // we allow collisions, but we shouldn't ever be updating the channel ID pointed to.
                let mut short_to_chan_info = $self.short_to_chan_info.write().unwrap();
-               let outbound_alias_insert = short_to_chan_info.insert($channel.outbound_scid_alias(), ($channel.get_counterparty_node_id(), $channel.channel_id()));
-               assert!(outbound_alias_insert.is_none() || outbound_alias_insert.unwrap() == ($channel.get_counterparty_node_id(), $channel.channel_id()),
+               let outbound_alias_insert = short_to_chan_info.insert($channel.context.outbound_scid_alias(), ($channel.context.get_counterparty_node_id(), $channel.context.channel_id()));
+               assert!(outbound_alias_insert.is_none() || outbound_alias_insert.unwrap() == ($channel.context.get_counterparty_node_id(), $channel.context.channel_id()),
                        "SCIDs should never collide - ensure you weren't behind the chain tip by a full month when creating channels");
-               if let Some(real_scid) = $channel.get_short_channel_id() {
-                       let scid_insert = short_to_chan_info.insert(real_scid, ($channel.get_counterparty_node_id(), $channel.channel_id()));
-                       assert!(scid_insert.is_none() || scid_insert.unwrap() == ($channel.get_counterparty_node_id(), $channel.channel_id()),
+               if let Some(real_scid) = $channel.context.get_short_channel_id() {
+                       let scid_insert = short_to_chan_info.insert(real_scid, ($channel.context.get_counterparty_node_id(), $channel.context.channel_id()));
+                       assert!(scid_insert.is_none() || scid_insert.unwrap() == ($channel.context.get_counterparty_node_id(), $channel.context.channel_id()),
                                "SCIDs should never collide - ensure you weren't behind the chain tip by a full month when creating channels");
                }
        }}
@@ -1608,41 +1835,41 @@ macro_rules! send_channel_ready {
 
 macro_rules! emit_channel_pending_event {
        ($locked_events: expr, $channel: expr) => {
-               if $channel.should_emit_channel_pending_event() {
+               if $channel.context.should_emit_channel_pending_event() {
                        $locked_events.push_back((events::Event::ChannelPending {
-                               channel_id: $channel.channel_id(),
-                               former_temporary_channel_id: $channel.temporary_channel_id(),
-                               counterparty_node_id: $channel.get_counterparty_node_id(),
-                               user_channel_id: $channel.get_user_id(),
-                               funding_txo: $channel.get_funding_txo().unwrap().into_bitcoin_outpoint(),
+                               channel_id: $channel.context.channel_id(),
+                               former_temporary_channel_id: $channel.context.temporary_channel_id(),
+                               counterparty_node_id: $channel.context.get_counterparty_node_id(),
+                               user_channel_id: $channel.context.get_user_id(),
+                               funding_txo: $channel.context.get_funding_txo().unwrap().into_bitcoin_outpoint(),
                        }, None));
-                       $channel.set_channel_pending_event_emitted();
+                       $channel.context.set_channel_pending_event_emitted();
                }
        }
 }
 
 macro_rules! emit_channel_ready_event {
        ($locked_events: expr, $channel: expr) => {
-               if $channel.should_emit_channel_ready_event() {
-                       debug_assert!($channel.channel_pending_event_emitted());
+               if $channel.context.should_emit_channel_ready_event() {
+                       debug_assert!($channel.context.channel_pending_event_emitted());
                        $locked_events.push_back((events::Event::ChannelReady {
-                               channel_id: $channel.channel_id(),
-                               user_channel_id: $channel.get_user_id(),
-                               counterparty_node_id: $channel.get_counterparty_node_id(),
-                               channel_type: $channel.get_channel_type().clone(),
+                               channel_id: $channel.context.channel_id(),
+                               user_channel_id: $channel.context.get_user_id(),
+                               counterparty_node_id: $channel.context.get_counterparty_node_id(),
+                               channel_type: $channel.context.get_channel_type().clone(),
                        }, None));
-                       $channel.set_channel_ready_event_emitted();
+                       $channel.context.set_channel_ready_event_emitted();
                }
        }
 }
 
 macro_rules! handle_monitor_update_completion {
-       ($self: ident, $update_id: expr, $peer_state_lock: expr, $peer_state: expr, $per_peer_state_lock: expr, $chan: expr) => { {
+       ($self: ident, $peer_state_lock: expr, $peer_state: expr, $per_peer_state_lock: expr, $chan: expr) => { {
                let mut updates = $chan.monitor_updating_restored(&$self.logger,
                        &$self.node_signer, $self.genesis_hash, &$self.default_configuration,
                        $self.best_block.read().unwrap().height());
-               let counterparty_node_id = $chan.get_counterparty_node_id();
-               let channel_update = if updates.channel_ready.is_some() && $chan.is_usable() {
+               let counterparty_node_id = $chan.context.get_counterparty_node_id();
+               let channel_update = if updates.channel_ready.is_some() && $chan.context.is_usable() {
                        // We only send a channel_update in the case where we are just now sending a
                        // channel_ready and the channel is in a usable state. We may re-send a
                        // channel_update later through the announcement_signatures process for public
@@ -1657,7 +1884,7 @@ macro_rules! handle_monitor_update_completion {
                } else { None };
 
                let update_actions = $peer_state.monitor_update_blocked_actions
-                       .remove(&$chan.channel_id()).unwrap_or(Vec::new());
+                       .remove(&$chan.context.channel_id()).unwrap_or(Vec::new());
 
                let htlc_forwards = $self.handle_channel_resumption(
                        &mut $peer_state.pending_msg_events, $chan, updates.raa,
@@ -1668,7 +1895,7 @@ macro_rules! handle_monitor_update_completion {
                        $peer_state.pending_msg_events.push(upd);
                }
 
-               let channel_id = $chan.channel_id();
+               let channel_id = $chan.context.channel_id();
                core::mem::drop($peer_state_lock);
                core::mem::drop($per_peer_state_lock);
 
@@ -1686,38 +1913,67 @@ macro_rules! handle_monitor_update_completion {
 }
 
 macro_rules! handle_new_monitor_update {
-       ($self: ident, $update_res: expr, $update_id: expr, $peer_state_lock: expr, $peer_state: expr, $per_peer_state_lock: expr, $chan: expr, MANUALLY_REMOVING, $remove: expr) => { {
+       ($self: ident, $update_res: expr, $peer_state_lock: expr, $peer_state: expr, $per_peer_state_lock: expr, $chan: expr, _internal, $remove: expr, $completed: expr) => { {
                // update_maps_on_chan_removal needs to be able to take id_to_peer, so make sure we can in
                // any case so that it won't deadlock.
                debug_assert_ne!($self.id_to_peer.held_by_thread(), LockHeldState::HeldByThread);
+               #[cfg(debug_assertions)] {
+                       debug_assert!($self.background_events_processed_since_startup.load(Ordering::Acquire));
+               }
                match $update_res {
                        ChannelMonitorUpdateStatus::InProgress => {
                                log_debug!($self.logger, "ChannelMonitor update for {} in flight, holding messages until the update completes.",
-                                       log_bytes!($chan.channel_id()[..]));
-                               Ok(())
+                                       log_bytes!($chan.context.channel_id()[..]));
+                               Ok(false)
                        },
                        ChannelMonitorUpdateStatus::PermanentFailure => {
                                log_error!($self.logger, "Closing channel {} due to monitor update ChannelMonitorUpdateStatus::PermanentFailure",
-                                       log_bytes!($chan.channel_id()[..]));
-                               update_maps_on_chan_removal!($self, $chan);
-                               let res: Result<(), _> = Err(MsgHandleErrInternal::from_finish_shutdown(
-                                       "ChannelMonitor storage failure".to_owned(), $chan.channel_id(),
-                                       $chan.get_user_id(), $chan.force_shutdown(false),
+                                       log_bytes!($chan.context.channel_id()[..]));
+                               update_maps_on_chan_removal!($self, &$chan.context);
+                               let res = Err(MsgHandleErrInternal::from_finish_shutdown(
+                                       "ChannelMonitor storage failure".to_owned(), $chan.context.channel_id(),
+                                       $chan.context.get_user_id(), $chan.context.force_shutdown(false),
                                        $self.get_channel_update_for_broadcast(&$chan).ok()));
                                $remove;
                                res
                        },
                        ChannelMonitorUpdateStatus::Completed => {
-                               $chan.complete_one_mon_update($update_id);
-                               if $chan.no_monitor_updates_pending() {
-                                       handle_monitor_update_completion!($self, $update_id, $peer_state_lock, $peer_state, $per_peer_state_lock, $chan);
-                               }
-                               Ok(())
+                               $completed;
+                               Ok(true)
                        },
                }
        } };
-       ($self: ident, $update_res: expr, $update_id: expr, $peer_state_lock: expr, $peer_state: expr, $per_peer_state_lock: expr, $chan_entry: expr) => {
-               handle_new_monitor_update!($self, $update_res, $update_id, $peer_state_lock, $peer_state, $per_peer_state_lock, $chan_entry.get_mut(), MANUALLY_REMOVING, $chan_entry.remove_entry())
+       ($self: ident, $update_res: expr, $peer_state_lock: expr, $peer_state: expr, $per_peer_state_lock: expr, $chan: expr, MANUALLY_REMOVING_INITIAL_MONITOR, $remove: expr) => {
+               handle_new_monitor_update!($self, $update_res, $peer_state_lock, $peer_state,
+                       $per_peer_state_lock, $chan, _internal, $remove,
+                       handle_monitor_update_completion!($self, $peer_state_lock, $peer_state, $per_peer_state_lock, $chan))
+       };
+       ($self: ident, $update_res: expr, $peer_state_lock: expr, $peer_state: expr, $per_peer_state_lock: expr, $chan_entry: expr, INITIAL_MONITOR) => {
+               handle_new_monitor_update!($self, $update_res, $peer_state_lock, $peer_state, $per_peer_state_lock, $chan_entry.get_mut(), MANUALLY_REMOVING_INITIAL_MONITOR, $chan_entry.remove_entry())
+       };
+       ($self: ident, $funding_txo: expr, $update: expr, $peer_state_lock: expr, $peer_state: expr, $per_peer_state_lock: expr, $chan: expr, MANUALLY_REMOVING, $remove: expr) => { {
+               let in_flight_updates = $peer_state.in_flight_monitor_updates.entry($funding_txo)
+                       .or_insert_with(Vec::new);
+               // During startup, we push monitor updates as background events through to here in
+               // order to replay updates that were in-flight when we shut down. Thus, we have to
+               // filter for uniqueness here.
+               let idx = in_flight_updates.iter().position(|upd| upd == &$update)
+                       .unwrap_or_else(|| {
+                               in_flight_updates.push($update);
+                               in_flight_updates.len() - 1
+                       });
+               let update_res = $self.chain_monitor.update_channel($funding_txo, &in_flight_updates[idx]);
+               handle_new_monitor_update!($self, update_res, $peer_state_lock, $peer_state,
+                       $per_peer_state_lock, $chan, _internal, $remove,
+                       {
+                               let _ = in_flight_updates.remove(idx);
+                               if in_flight_updates.is_empty() && $chan.blocked_monitor_updates_pending() == 0 {
+                                       handle_monitor_update_completion!($self, $peer_state_lock, $peer_state, $per_peer_state_lock, $chan);
+                               }
+                       })
+       } };
+       ($self: ident, $funding_txo: expr, $update: expr, $peer_state_lock: expr, $peer_state: expr, $per_peer_state_lock: expr, $chan_entry: expr) => {
+               handle_new_monitor_update!($self, $funding_txo, $update, $peer_state_lock, $peer_state, $per_peer_state_lock, $chan_entry.get_mut(), MANUALLY_REMOVING, $chan_entry.remove_entry())
        }
 }
 
@@ -1736,6 +1992,10 @@ macro_rules! process_events_body {
                                // persists happen while processing monitor events.
                                let _read_guard = $self.total_consistency_lock.read().unwrap();
 
+                               // Because `handle_post_event_actions` may send `ChannelMonitorUpdate`s to the user we must
+                               // ensure any startup-generated background events are handled first.
+                               if $self.process_background_events() == NotifyOption::DoPersist { result = NotifyOption::DoPersist; }
+
                                // TODO: This behavior should be documented. It's unintuitive that we query
                                // ChannelMonitors when clearing other events.
                                if $self.process_pending_monitor_events() {
@@ -1792,6 +2052,8 @@ where
 {
        /// Constructs a new `ChannelManager` to hold several channels and route between them.
        ///
+       /// The current time or latest block header time can be provided as the `current_timestamp`.
+       ///
        /// This is the main "logic hub" for all channel-related actions, and implements
        /// [`ChannelMessageHandler`].
        ///
@@ -1805,7 +2067,11 @@ where
        /// [`block_connected`]: chain::Listen::block_connected
        /// [`block_disconnected`]: chain::Listen::block_disconnected
        /// [`params.best_block.block_hash`]: chain::BestBlock::block_hash
-       pub fn new(fee_est: F, chain_monitor: M, tx_broadcaster: T, router: R, logger: L, entropy_source: ES, node_signer: NS, signer_provider: SP, config: UserConfig, params: ChainParameters) -> Self {
+       pub fn new(
+               fee_est: F, chain_monitor: M, tx_broadcaster: T, router: R, logger: L, entropy_source: ES,
+               node_signer: NS, signer_provider: SP, config: UserConfig, params: ChainParameters,
+               current_timestamp: u32,
+       ) -> Self {
                let mut secp_ctx = Secp256k1::new();
                secp_ctx.seeded_randomize(&entropy_source.get_secure_random_bytes());
                let inbound_pmt_key_material = node_signer.get_inbound_payment_key_material();
@@ -1837,7 +2103,7 @@ where
 
                        probing_cookie_secret: entropy_source.get_secure_random_bytes(),
 
-                       highest_seen_timestamp: AtomicUsize::new(0),
+                       highest_seen_timestamp: AtomicUsize::new(current_timestamp as usize),
 
                        per_peer_state: FairRwLock::new(HashMap::new()),
 
@@ -1845,6 +2111,8 @@ where
                        pending_events_processor: AtomicBool::new(false),
                        pending_background_events: Mutex::new(Vec::new()),
                        total_consistency_lock: RwLock::new(()),
+                       #[cfg(debug_assertions)]
+                       background_events_processed_since_startup: AtomicBool::new(false),
                        persistence_notifier: Notifier::new(),
 
                        entropy_source,
@@ -1913,7 +2181,7 @@ where
                        return Err(APIError::APIMisuseError { err: format!("Channel value must be at least 1000 satoshis. It was {}", channel_value_satoshis) });
                }
 
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
                // We want to make sure the lock is actually acquired by PersistenceNotifierGuard.
                debug_assert!(&self.total_consistency_lock.try_write().is_err());
 
@@ -1927,7 +2195,7 @@ where
                        let outbound_scid_alias = self.create_and_insert_outbound_scid_alias();
                        let their_features = &peer_state.latest_features;
                        let config = if override_config.is_some() { override_config.as_ref().unwrap() } else { &self.default_configuration };
-                       match Channel::new_outbound(&self.fee_estimator, &self.entropy_source, &self.signer_provider, their_network_key,
+                       match OutboundV1Channel::new(&self.fee_estimator, &self.entropy_source, &self.signer_provider, their_network_key,
                                their_features, channel_value_satoshis, push_msat, user_channel_id, config,
                                self.best_block.read().unwrap().height(), outbound_scid_alias)
                        {
@@ -1940,8 +2208,8 @@ where
                };
                let res = channel.get_open_channel(self.genesis_hash.clone());
 
-               let temporary_channel_id = channel.channel_id();
-               match peer_state.channel_by_id.entry(temporary_channel_id) {
+               let temporary_channel_id = channel.context.channel_id();
+               match peer_state.outbound_v1_channel_by_id.entry(temporary_channel_id) {
                        hash_map::Entry::Occupied(_) => {
                                if cfg!(fuzzing) {
                                        return Err(APIError::APIMisuseError { err: "Fuzzy bad RNG".to_owned() });
@@ -1959,7 +2227,7 @@ where
                Ok(temporary_channel_id)
        }
 
-       fn list_channels_with_filter<Fn: FnMut(&(&[u8; 32], &Channel<<SP::Target as SignerProvider>::Signer>)) -> bool + Copy>(&self, f: Fn) -> Vec<ChannelDetails> {
+       fn list_funded_channels_with_filter<Fn: FnMut(&(&[u8; 32], &Channel<<SP::Target as SignerProvider>::Signer>)) -> bool + Copy>(&self, f: Fn) -> Vec<ChannelDetails> {
                // Allocate our best estimate of the number of channels we have in the `res`
                // Vec. Sadly the `short_to_chan_info` map doesn't cover channels without
                // a scid or a scid alias, and the `id_to_peer` shouldn't be used outside
@@ -1974,8 +2242,8 @@ where
                                let mut peer_state_lock = peer_state_mutex.lock().unwrap();
                                let peer_state = &mut *peer_state_lock;
                                for (_channel_id, channel) in peer_state.channel_by_id.iter().filter(f) {
-                                       let details = ChannelDetails::from_channel(channel, best_block_height,
-                                               peer_state.latest_features.clone());
+                                       let details = ChannelDetails::from_channel_context(&channel.context, best_block_height,
+                                               peer_state.latest_features.clone(), &self.fee_estimator);
                                        res.push(details);
                                }
                        }
@@ -1986,7 +2254,37 @@ where
        /// Gets the list of open channels, in random order. See [`ChannelDetails`] field documentation for
        /// more information.
        pub fn list_channels(&self) -> Vec<ChannelDetails> {
-               self.list_channels_with_filter(|_| true)
+               // Allocate our best estimate of the number of channels we have in the `res`
+               // Vec. Sadly the `short_to_chan_info` map doesn't cover channels without
+               // a scid or a scid alias, and the `id_to_peer` shouldn't be used outside
+               // of the ChannelMonitor handling. Therefore reallocations may still occur, but is
+               // unlikely as the `short_to_chan_info` map often contains 2 entries for
+               // the same channel.
+               let mut res = Vec::with_capacity(self.short_to_chan_info.read().unwrap().len());
+               {
+                       let best_block_height = self.best_block.read().unwrap().height();
+                       let per_peer_state = self.per_peer_state.read().unwrap();
+                       for (_cp_id, peer_state_mutex) in per_peer_state.iter() {
+                               let mut peer_state_lock = peer_state_mutex.lock().unwrap();
+                               let peer_state = &mut *peer_state_lock;
+                               for (_channel_id, channel) in peer_state.channel_by_id.iter() {
+                                       let details = ChannelDetails::from_channel_context(&channel.context, best_block_height,
+                                               peer_state.latest_features.clone(), &self.fee_estimator);
+                                       res.push(details);
+                               }
+                               for (_channel_id, channel) in peer_state.inbound_v1_channel_by_id.iter() {
+                                       let details = ChannelDetails::from_channel_context(&channel.context, best_block_height,
+                                               peer_state.latest_features.clone(), &self.fee_estimator);
+                                       res.push(details);
+                               }
+                               for (_channel_id, channel) in peer_state.outbound_v1_channel_by_id.iter() {
+                                       let details = ChannelDetails::from_channel_context(&channel.context, best_block_height,
+                                               peer_state.latest_features.clone(), &self.fee_estimator);
+                                       res.push(details);
+                               }
+                       }
+               }
+               res
        }
 
        /// Gets the list of usable channels, in random order. Useful as an argument to
@@ -1999,7 +2297,7 @@ where
                // 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.
-               self.list_channels_with_filter(|&(_, ref channel)| channel.is_live())
+               self.list_funded_channels_with_filter(|&(_, ref channel)| channel.context.is_live())
        }
 
        /// Gets the list of channels we have with a given counterparty, in random order.
@@ -2014,7 +2312,8 @@ where
                        return peer_state.channel_by_id
                                .iter()
                                .map(|(_, channel)|
-                                       ChannelDetails::from_channel(channel, best_block_height, features.clone()))
+                                       ChannelDetails::from_channel_context(&channel.context, best_block_height,
+                                       features.clone(), &self.fee_estimator))
                                .collect();
                }
                vec![]
@@ -2049,25 +2348,25 @@ where
        }
 
        /// Helper function that issues the channel close events
-       fn issue_channel_close_events(&self, channel: &Channel<<SP::Target as SignerProvider>::Signer>, closure_reason: ClosureReason) {
+       fn issue_channel_close_events(&self, context: &ChannelContext<<SP::Target as SignerProvider>::Signer>, closure_reason: ClosureReason) {
                let mut pending_events_lock = self.pending_events.lock().unwrap();
-               match channel.unbroadcasted_funding() {
+               match context.unbroadcasted_funding() {
                        Some(transaction) => {
                                pending_events_lock.push_back((events::Event::DiscardFunding {
-                                       channel_id: channel.channel_id(), transaction
+                                       channel_id: context.channel_id(), transaction
                                }, None));
                        },
                        None => {},
                }
                pending_events_lock.push_back((events::Event::ChannelClosed {
-                       channel_id: channel.channel_id(),
-                       user_channel_id: channel.get_user_id(),
+                       channel_id: context.channel_id(),
+                       user_channel_id: context.get_user_id(),
                        reason: closure_reason
                }, None));
        }
 
        fn close_channel_internal(&self, channel_id: &[u8; 32], counterparty_node_id: &PublicKey, target_feerate_sats_per_1000_weight: Option<u32>, override_shutdown_script: Option<ShutdownScript>) -> Result<(), APIError> {
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
 
                let mut failed_htlcs: Vec<(HTLCSource, PaymentHash)>;
                let result: Result<(), _> = loop {
@@ -2080,7 +2379,7 @@ where
                        let peer_state = &mut *peer_state_lock;
                        match peer_state.channel_by_id.entry(channel_id.clone()) {
                                hash_map::Entry::Occupied(mut chan_entry) => {
-                                       let funding_txo_opt = chan_entry.get().get_funding_txo();
+                                       let funding_txo_opt = chan_entry.get().context.get_funding_txo();
                                        let their_features = &peer_state.latest_features;
                                        let (shutdown_msg, mut monitor_update_opt, htlcs) = chan_entry.get_mut()
                                                .get_shutdown(&self.signer_provider, their_features, target_feerate_sats_per_1000_weight, override_shutdown_script)?;
@@ -2096,9 +2395,8 @@ where
 
                                        // Update the monitor with the shutdown script if necessary.
                                        if let Some(monitor_update) = monitor_update_opt.take() {
-                                               let update_id = monitor_update.update_id;
-                                               let update_res = self.chain_monitor.update_channel(funding_txo_opt.unwrap(), monitor_update);
-                                               break handle_new_monitor_update!(self, update_res, update_id, peer_state_lock, peer_state, per_peer_state, chan_entry);
+                                               break handle_new_monitor_update!(self, funding_txo_opt.unwrap(), monitor_update,
+                                                       peer_state_lock, peer_state, per_peer_state, chan_entry).map(|_| ());
                                        }
 
                                        if chan_entry.get().is_shutdown() {
@@ -2108,7 +2406,7 @@ where
                                                                msg: channel_update
                                                        });
                                                }
-                                               self.issue_channel_close_events(&channel, ClosureReason::HolderForceClosed);
+                                               self.issue_channel_close_events(&channel.context, ClosureReason::HolderForceClosed);
                                        }
                                        break Ok(());
                                },
@@ -2197,7 +2495,7 @@ where
                        let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id), channel_id };
                        self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, receiver);
                }
-               if let Some((funding_txo, monitor_update)) = monitor_update_option {
+               if let Some((_, funding_txo, monitor_update)) = monitor_update_option {
                        // There isn't anything we can do if we get an update failure - we're already
                        // force-closing. The monitor update on the required in-memory copy should broadcast
                        // the latest local state, which is the best we can do anyway. Thus, it is safe to
@@ -2213,34 +2511,50 @@ where
                let per_peer_state = self.per_peer_state.read().unwrap();
                let peer_state_mutex = per_peer_state.get(peer_node_id)
                        .ok_or_else(|| APIError::ChannelUnavailable { err: format!("Can't find a peer matching the passed counterparty node_id {}", peer_node_id) })?;
-               let mut chan = {
+               let (update_opt, counterparty_node_id) = {
                        let mut peer_state_lock = peer_state_mutex.lock().unwrap();
                        let peer_state = &mut *peer_state_lock;
+                       let closure_reason = if let Some(peer_msg) = peer_msg {
+                               ClosureReason::CounterpartyForceClosed { peer_msg: UntrustedString(peer_msg.to_string()) }
+                       } else {
+                               ClosureReason::HolderForceClosed
+                       };
                        if let hash_map::Entry::Occupied(chan) = peer_state.channel_by_id.entry(channel_id.clone()) {
-                               if let Some(peer_msg) = peer_msg {
-                                       self.issue_channel_close_events(chan.get(),ClosureReason::CounterpartyForceClosed { peer_msg: UntrustedString(peer_msg.to_string()) });
-                               } else {
-                                       self.issue_channel_close_events(chan.get(),ClosureReason::HolderForceClosed);
-                               }
-                               remove_channel!(self, chan)
+                               log_error!(self.logger, "Force-closing channel {}", log_bytes!(channel_id[..]));
+                               self.issue_channel_close_events(&chan.get().context, closure_reason);
+                               let mut chan = remove_channel!(self, chan);
+                               self.finish_force_close_channel(chan.context.force_shutdown(broadcast));
+                               (self.get_channel_update_for_broadcast(&chan).ok(), chan.context.get_counterparty_node_id())
+                       } else if let hash_map::Entry::Occupied(chan) = peer_state.outbound_v1_channel_by_id.entry(channel_id.clone()) {
+                               log_error!(self.logger, "Force-closing channel {}", log_bytes!(channel_id[..]));
+                               self.issue_channel_close_events(&chan.get().context, closure_reason);
+                               let mut chan = remove_channel!(self, chan);
+                               self.finish_force_close_channel(chan.context.force_shutdown(false));
+                               // Prefunded channel has no update
+                               (None, chan.context.get_counterparty_node_id())
+                       } else if let hash_map::Entry::Occupied(chan) = peer_state.inbound_v1_channel_by_id.entry(channel_id.clone()) {
+                               log_error!(self.logger, "Force-closing channel {}", log_bytes!(channel_id[..]));
+                               self.issue_channel_close_events(&chan.get().context, closure_reason);
+                               let mut chan = remove_channel!(self, chan);
+                               self.finish_force_close_channel(chan.context.force_shutdown(false));
+                               // Prefunded channel has no update
+                               (None, chan.context.get_counterparty_node_id())
                        } else {
                                return Err(APIError::ChannelUnavailable{ err: format!("Channel with id {} not found for the passed counterparty node_id {}", log_bytes!(*channel_id), peer_node_id) });
                        }
                };
-               log_error!(self.logger, "Force-closing channel {}", log_bytes!(channel_id[..]));
-               self.finish_force_close_channel(chan.force_shutdown(broadcast));
-               if let Ok(update) = self.get_channel_update_for_broadcast(&chan) {
+               if let Some(update) = update_opt {
                        let mut peer_state = peer_state_mutex.lock().unwrap();
                        peer_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
                                msg: update
                        });
                }
 
-               Ok(chan.get_counterparty_node_id())
+               Ok(counterparty_node_id)
        }
 
        fn force_close_sending_error(&self, channel_id: &[u8; 32], counterparty_node_id: &PublicKey, broadcast: bool) -> Result<(), APIError> {
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
                match self.force_close_channel_with_peer(channel_id, counterparty_node_id, None, broadcast) {
                        Ok(counterparty_node_id) => {
                                let per_peer_state = self.per_peer_state.read().unwrap();
@@ -2297,9 +2611,11 @@ where
                }
        }
 
-       fn construct_recv_pending_htlc_info(&self, hop_data: msgs::OnionHopData, shared_secret: [u8; 32],
-               payment_hash: PaymentHash, amt_msat: u64, cltv_expiry: u32, phantom_shared_secret: Option<[u8; 32]>) -> Result<PendingHTLCInfo, ReceiveError>
-       {
+       fn construct_recv_pending_htlc_info(
+               &self, hop_data: msgs::OnionHopData, shared_secret: [u8; 32], payment_hash: PaymentHash,
+               amt_msat: u64, cltv_expiry: u32, phantom_shared_secret: Option<[u8; 32]>, allow_underpay: bool,
+               counterparty_skimmed_fee_msat: Option<u64>,
+       ) -> Result<PendingHTLCInfo, ReceiveError> {
                // final_incorrect_cltv_expiry
                if hop_data.outgoing_cltv_value > cltv_expiry {
                        return Err(ReceiveError {
@@ -2325,7 +2641,10 @@ where
                                msg: "The final CLTV expiry is too soon to handle",
                        });
                }
-               if hop_data.amt_to_forward > amt_msat {
+               if (!allow_underpay && hop_data.amt_to_forward > amt_msat) ||
+                       (allow_underpay && hop_data.amt_to_forward >
+                        amt_msat.saturating_add(counterparty_skimmed_fee_msat.unwrap_or(0)))
+               {
                        return Err(ReceiveError {
                                err_code: 19,
                                err_data: amt_msat.to_be_bytes().to_vec(),
@@ -2342,20 +2661,7 @@ where
                                });
                        },
                        msgs::OnionHopDataFormat::FinalNode { payment_data, keysend_preimage, payment_metadata } => {
-                               if payment_data.is_some() && keysend_preimage.is_some() {
-                                       return Err(ReceiveError {
-                                               err_code: 0x4000|22,
-                                               err_data: Vec::new(),
-                                               msg: "We don't support MPP keysend payments",
-                                       });
-                               } else if let Some(data) = payment_data {
-                                       PendingHTLCRouting::Receive {
-                                               payment_data: data,
-                                               payment_metadata,
-                                               incoming_cltv_expiry: hop_data.outgoing_cltv_value,
-                                               phantom_shared_secret,
-                                       }
-                               } else if let Some(payment_preimage) = keysend_preimage {
+                               if let Some(payment_preimage) = keysend_preimage {
                                        // We need to check that the sender knows the keysend preimage before processing this
                                        // payment further. Otherwise, an intermediary routing hop forwarding non-keysend-HTLC X
                                        // could discover the final destination of X, by probing the adjacent nodes on the route
@@ -2369,12 +2675,26 @@ where
                                                        msg: "Payment preimage didn't match payment hash",
                                                });
                                        }
-
+                                       if !self.default_configuration.accept_mpp_keysend && payment_data.is_some() {
+                                               return Err(ReceiveError {
+                                                       err_code: 0x4000|22,
+                                                       err_data: Vec::new(),
+                                                       msg: "We don't support MPP keysend payments",
+                                               });
+                                       }
                                        PendingHTLCRouting::ReceiveKeysend {
+                                               payment_data,
                                                payment_preimage,
                                                payment_metadata,
                                                incoming_cltv_expiry: hop_data.outgoing_cltv_value,
                                        }
+                               } else if let Some(data) = payment_data {
+                                       PendingHTLCRouting::Receive {
+                                               payment_data: data,
+                                               payment_metadata,
+                                               incoming_cltv_expiry: hop_data.outgoing_cltv_value,
+                                               phantom_shared_secret,
+                                       }
                                } else {
                                        return Err(ReceiveError {
                                                err_code: 0x4000|0x2000|3,
@@ -2391,15 +2711,18 @@ where
                        incoming_amt_msat: Some(amt_msat),
                        outgoing_amt_msat: hop_data.amt_to_forward,
                        outgoing_cltv_value: hop_data.outgoing_cltv_value,
+                       skimmed_fee_msat: counterparty_skimmed_fee_msat,
                })
        }
 
-       fn decode_update_add_htlc_onion(&self, msg: &msgs::UpdateAddHTLC) -> PendingHTLCStatus {
+       fn decode_update_add_htlc_onion(
+               &self, msg: &msgs::UpdateAddHTLC
+       ) -> Result<(onion_utils::Hop, [u8; 32], Option<Result<PublicKey, secp256k1::Error>>), HTLCFailureMsg> {
                macro_rules! return_malformed_err {
                        ($msg: expr, $err_code: expr) => {
                                {
                                        log_info!(self.logger, "Failed to accept/forward incoming HTLC: {}", $msg);
-                                       return PendingHTLCStatus::Fail(HTLCFailureMsg::Malformed(msgs::UpdateFailMalformedHTLC {
+                                       return Err(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(),
@@ -2430,7 +2753,7 @@ where
                        ($msg: expr, $err_code: expr, $data: expr) => {
                                {
                                        log_info!(self.logger, "Failed to accept/forward incoming HTLC: {}", $msg);
-                                       return PendingHTLCStatus::Fail(HTLCFailureMsg::Relay(msgs::UpdateFailHTLC {
+                                       return Err(HTLCFailureMsg::Relay(msgs::UpdateFailHTLC {
                                                channel_id: msg.channel_id,
                                                htlc_id: msg.htlc_id,
                                                reason: HTLCFailReason::reason($err_code, $data.to_vec())
@@ -2449,11 +2772,186 @@ where
                                return_err!(err_msg, err_code, &[0; 0]);
                        },
                };
+               let (outgoing_scid, outgoing_amt_msat, outgoing_cltv_value, next_packet_pk_opt) = match next_hop {
+                       onion_utils::Hop::Forward {
+                               next_hop_data: msgs::OnionHopData {
+                                       format: msgs::OnionHopDataFormat::NonFinalNode { short_channel_id }, amt_to_forward,
+                                       outgoing_cltv_value,
+                               }, ..
+                       } => {
+                               let next_pk = onion_utils::next_hop_packet_pubkey(&self.secp_ctx,
+                                       msg.onion_routing_packet.public_key.unwrap(), &shared_secret);
+                               (short_channel_id, amt_to_forward, outgoing_cltv_value, Some(next_pk))
+                       },
+                       // We'll do receive checks in [`Self::construct_pending_htlc_info`] so we have access to the
+                       // inbound channel's state.
+                       onion_utils::Hop::Receive { .. } => return Ok((next_hop, shared_secret, None)),
+                       onion_utils::Hop::Forward {
+                               next_hop_data: msgs::OnionHopData { format: msgs::OnionHopDataFormat::FinalNode { .. }, .. }, ..
+                       } => {
+                               return_err!("Final Node OnionHopData provided for us as an intermediary node", 0x4000 | 22, &[0; 0]);
+                       }
+               };
 
-               let pending_forward_info = match next_hop {
+               // Perform outbound checks here instead of in [`Self::construct_pending_htlc_info`] because we
+               // can't hold the outbound peer state lock at the same time as the inbound peer state lock.
+               if let Some((err, mut code, chan_update)) = loop {
+                       let id_option = self.short_to_chan_info.read().unwrap().get(&outgoing_scid).cloned();
+                       let forwarding_chan_info_opt = match id_option {
+                               None => { // unknown_next_peer
+                                       // Note that this is likely a timing oracle for detecting whether an scid is a
+                                       // phantom or an intercept.
+                                       if (self.default_configuration.accept_intercept_htlcs &&
+                                               fake_scid::is_valid_intercept(&self.fake_scid_rand_bytes, outgoing_scid, &self.genesis_hash)) ||
+                                               fake_scid::is_valid_phantom(&self.fake_scid_rand_bytes, outgoing_scid, &self.genesis_hash)
+                                       {
+                                               None
+                                       } else {
+                                               break Some(("Don't have available channel for forwarding as requested.", 0x4000 | 10, None));
+                                       }
+                               },
+                               Some((cp_id, id)) => Some((cp_id.clone(), id.clone())),
+                       };
+                       let chan_update_opt = if let Some((counterparty_node_id, forwarding_id)) = forwarding_chan_info_opt {
+                               let per_peer_state = self.per_peer_state.read().unwrap();
+                               let peer_state_mutex_opt = per_peer_state.get(&counterparty_node_id);
+                               if peer_state_mutex_opt.is_none() {
+                                       break Some(("Don't have available channel for forwarding as requested.", 0x4000 | 10, None));
+                               }
+                               let mut peer_state_lock = peer_state_mutex_opt.unwrap().lock().unwrap();
+                               let peer_state = &mut *peer_state_lock;
+                               let chan = match peer_state.channel_by_id.get_mut(&forwarding_id) {
+                                       None => {
+                                               // Channel was removed. The short_to_chan_info and channel_by_id maps
+                                               // have no consistency guarantees.
+                                               break Some(("Don't have available channel for forwarding as requested.", 0x4000 | 10, None));
+                                       },
+                                       Some(chan) => chan
+                               };
+                               if !chan.context.should_announce() && !self.default_configuration.accept_forwards_to_priv_channels {
+                                       // Note that the behavior here should be identical to the above block - we
+                                       // should NOT reveal the existence or non-existence of a private channel if
+                                       // we don't allow forwards outbound over them.
+                                       break Some(("Refusing to forward to a private channel based on our config.", 0x4000 | 10, None));
+                               }
+                               if chan.context.get_channel_type().supports_scid_privacy() && outgoing_scid != chan.context.outbound_scid_alias() {
+                                       // `option_scid_alias` (referred to in LDK as `scid_privacy`) means
+                                       // "refuse to forward unless the SCID alias was used", so we pretend
+                                       // we don't have the channel here.
+                                       break Some(("Refusing to forward over real channel SCID as our counterparty requested.", 0x4000 | 10, None));
+                               }
+                               let chan_update_opt = self.get_channel_update_for_onion(outgoing_scid, chan).ok();
+
+                               // 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.context.is_live() { // channel_disabled
+                                       // If the channel_update we're going to return is disabled (i.e. the
+                                       // peer has been disabled for some time), return `channel_disabled`,
+                                       // otherwise return `temporary_channel_failure`.
+                                       if chan_update_opt.as_ref().map(|u| u.contents.flags & 2 == 2).unwrap_or(false) {
+                                               break Some(("Forwarding channel has been disconnected for some time.", 0x1000 | 20, chan_update_opt));
+                                       } else {
+                                               break Some(("Forwarding channel is not in a ready state.", 0x1000 | 7, chan_update_opt));
+                                       }
+                               }
+                               if outgoing_amt_msat < chan.context.get_counterparty_htlc_minimum_msat() { // amount_below_minimum
+                                       break Some(("HTLC amount was below the htlc_minimum_msat", 0x1000 | 11, chan_update_opt));
+                               }
+                               if let Err((err, code)) = chan.htlc_satisfies_config(&msg, outgoing_amt_msat, outgoing_cltv_value) {
+                                       break Some((err, code, chan_update_opt));
+                               }
+                               chan_update_opt
+                       } else {
+                               if (msg.cltv_expiry as u64) < (outgoing_cltv_value) as u64 + MIN_CLTV_EXPIRY_DELTA as u64 {
+                                       // We really should set `incorrect_cltv_expiry` here but as we're not
+                                       // forwarding over a real channel we can't generate a channel_update
+                                       // for it. Instead we just return a generic temporary_node_failure.
+                                       break Some((
+                                                       "Forwarding node has tampered with the intended HTLC values or origin node has an obsolete cltv_expiry_delta",
+                                                       0x2000 | 2, None,
+                                       ));
+                               }
+                               None
+                       };
+
+                       let cur_height = self.best_block.read().unwrap().height() + 1;
+                       // Theoretically, channel counterparty shouldn't send us a HTLC expiring now,
+                       // but we want to be robust wrt to counterparty packet sanitization (see
+                       // HTLC_FAIL_BACK_BUFFER rationale).
+                       if msg.cltv_expiry <= cur_height + HTLC_FAIL_BACK_BUFFER as u32 { // expiry_too_soon
+                               break Some(("CLTV expiry is too close", 0x1000 | 14, chan_update_opt));
+                       }
+                       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));
+                       }
+                       // If the HTLC expires ~now, don't bother trying to forward it to our
+                       // counterparty. They should fail it anyway, but we don't want to bother with
+                       // the round-trips or risk them deciding they definitely want the HTLC and
+                       // force-closing to ensure they get it if we're offline.
+                       // We previously had a much more aggressive check here which tried to ensure
+                       // our counterparty receives an HTLC which has *our* risk threshold met on it,
+                       // but there is no need to do that, and since we're a bit conservative with our
+                       // risk threshold it just results in failing to forward payments.
+                       if (outgoing_cltv_value) as u64 <= (cur_height + LATENCY_GRACE_PERIOD_BLOCKS) as u64 {
+                               break Some(("Outgoing CLTV value is too soon", 0x1000 | 14, chan_update_opt));
+                       }
+
+                       break None;
+               }
+               {
+                       let mut res = VecWriter(Vec::with_capacity(chan_update.serialized_length() + 2 + 8 + 2));
+                       if let Some(chan_update) = chan_update {
+                               if code == 0x1000 | 11 || code == 0x1000 | 12 {
+                                       msg.amount_msat.write(&mut res).expect("Writes cannot fail");
+                               }
+                               else if code == 0x1000 | 13 {
+                                       msg.cltv_expiry.write(&mut res).expect("Writes cannot fail");
+                               }
+                               else if code == 0x1000 | 20 {
+                                       // TODO: underspecified, follow https://github.com/lightning/bolts/issues/791
+                                       0u16.write(&mut res).expect("Writes cannot fail");
+                               }
+                               (chan_update.serialized_length() as u16 + 2).write(&mut res).expect("Writes cannot fail");
+                               msgs::ChannelUpdate::TYPE.write(&mut res).expect("Writes cannot fail");
+                               chan_update.write(&mut res).expect("Writes cannot fail");
+                       } else if code & 0x1000 == 0x1000 {
+                               // If we're trying to return an error that requires a `channel_update` but
+                               // we're forwarding to a phantom or intercept "channel" (i.e. cannot
+                               // generate an update), just use the generic "temporary_node_failure"
+                               // instead.
+                               code = 0x2000 | 2;
+                       }
+                       return_err!(err, code, &res.0[..]);
+               }
+               Ok((next_hop, shared_secret, next_packet_pk_opt))
+       }
+
+       fn construct_pending_htlc_status<'a>(
+               &self, msg: &msgs::UpdateAddHTLC, shared_secret: [u8; 32], decoded_hop: onion_utils::Hop,
+               allow_underpay: bool, next_packet_pubkey_opt: Option<Result<PublicKey, secp256k1::Error>>
+       ) -> PendingHTLCStatus {
+               macro_rules! return_err {
+                       ($msg: expr, $err_code: expr, $data: expr) => {
+                               {
+                                       log_info!(self.logger, "Failed to accept/forward incoming HTLC: {}", $msg);
+                                       return PendingHTLCStatus::Fail(HTLCFailureMsg::Relay(msgs::UpdateFailHTLC {
+                                               channel_id: msg.channel_id,
+                                               htlc_id: msg.htlc_id,
+                                               reason: HTLCFailReason::reason($err_code, $data.to_vec())
+                                                       .get_encrypted_failure_packet(&shared_secret, &None),
+                                       }));
+                               }
+                       }
+               }
+               match decoded_hop {
                        onion_utils::Hop::Receive(next_hop_data) => {
                                // OUR PAYMENT!
-                               match self.construct_recv_pending_htlc_info(next_hop_data, shared_secret, msg.payment_hash, msg.amount_msat, msg.cltv_expiry, None) {
+                               match self.construct_recv_pending_htlc_info(next_hop_data, shared_secret, msg.payment_hash,
+                                       msg.amount_msat, msg.cltv_expiry, None, allow_underpay, msg.skimmed_fee_msat)
+                               {
                                        Ok(info) => {
                                                // 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
@@ -2465,10 +2963,10 @@ where
                                }
                        },
                        onion_utils::Hop::Forward { next_hop_data, next_hop_hmac, new_packet_bytes } => {
-                               let new_pubkey = msg.onion_routing_packet.public_key.unwrap();
+                               debug_assert!(next_packet_pubkey_opt.is_some());
                                let outgoing_packet = msgs::OnionPacket {
                                        version: 0,
-                                       public_key: onion_utils::next_hop_packet_pubkey(&self.secp_ctx, new_pubkey, &shared_secret),
+                                       public_key: next_packet_pubkey_opt.unwrap_or(Err(secp256k1::Error::InvalidPublicKey)),
                                        hop_data: new_packet_bytes,
                                        hmac: next_hop_hmac.clone(),
                                };
@@ -2490,150 +2988,10 @@ where
                                        incoming_amt_msat: Some(msg.amount_msat),
                                        outgoing_amt_msat: next_hop_data.amt_to_forward,
                                        outgoing_cltv_value: next_hop_data.outgoing_cltv_value,
+                                       skimmed_fee_msat: None,
                                })
                        }
-               };
-
-               if let &PendingHTLCStatus::Forward(PendingHTLCInfo { ref routing, ref outgoing_amt_msat, ref outgoing_cltv_value, .. }) = &pending_forward_info {
-                       // If short_channel_id is 0 here, we'll reject the HTLC as there cannot be a channel
-                       // with a short_channel_id of 0. This is important as various things later assume
-                       // short_channel_id is non-0 in any ::Forward.
-                       if let &PendingHTLCRouting::Forward { ref short_channel_id, .. } = routing {
-                               if let Some((err, mut code, chan_update)) = loop {
-                                       let id_option = self.short_to_chan_info.read().unwrap().get(short_channel_id).cloned();
-                                       let forwarding_chan_info_opt = match id_option {
-                                               None => { // unknown_next_peer
-                                                       // Note that this is likely a timing oracle for detecting whether an scid is a
-                                                       // phantom or an intercept.
-                                                       if (self.default_configuration.accept_intercept_htlcs &&
-                                                          fake_scid::is_valid_intercept(&self.fake_scid_rand_bytes, *short_channel_id, &self.genesis_hash)) ||
-                                                          fake_scid::is_valid_phantom(&self.fake_scid_rand_bytes, *short_channel_id, &self.genesis_hash)
-                                                       {
-                                                               None
-                                                       } else {
-                                                               break Some(("Don't have available channel for forwarding as requested.", 0x4000 | 10, None));
-                                                       }
-                                               },
-                                               Some((cp_id, id)) => Some((cp_id.clone(), id.clone())),
-                                       };
-                                       let chan_update_opt = if let Some((counterparty_node_id, forwarding_id)) = forwarding_chan_info_opt {
-                                               let per_peer_state = self.per_peer_state.read().unwrap();
-                                               let peer_state_mutex_opt = per_peer_state.get(&counterparty_node_id);
-                                               if peer_state_mutex_opt.is_none() {
-                                                       break Some(("Don't have available channel for forwarding as requested.", 0x4000 | 10, None));
-                                               }
-                                               let mut peer_state_lock = peer_state_mutex_opt.unwrap().lock().unwrap();
-                                               let peer_state = &mut *peer_state_lock;
-                                               let chan = match peer_state.channel_by_id.get_mut(&forwarding_id) {
-                                                       None => {
-                                                               // Channel was removed. The short_to_chan_info and channel_by_id maps
-                                                               // have no consistency guarantees.
-                                                               break Some(("Don't have available channel for forwarding as requested.", 0x4000 | 10, None));
-                                                       },
-                                                       Some(chan) => chan
-                                               };
-                                               if !chan.should_announce() && !self.default_configuration.accept_forwards_to_priv_channels {
-                                                       // Note that the behavior here should be identical to the above block - we
-                                                       // should NOT reveal the existence or non-existence of a private channel if
-                                                       // we don't allow forwards outbound over them.
-                                                       break Some(("Refusing to forward to a private channel based on our config.", 0x4000 | 10, None));
-                                               }
-                                               if chan.get_channel_type().supports_scid_privacy() && *short_channel_id != chan.outbound_scid_alias() {
-                                                       // `option_scid_alias` (referred to in LDK as `scid_privacy`) means
-                                                       // "refuse to forward unless the SCID alias was used", so we pretend
-                                                       // we don't have the channel here.
-                                                       break Some(("Refusing to forward over real channel SCID as our counterparty requested.", 0x4000 | 10, None));
-                                               }
-                                               let chan_update_opt = self.get_channel_update_for_onion(*short_channel_id, chan).ok();
-
-                                               // 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
-                                                       // If the channel_update we're going to return is disabled (i.e. the
-                                                       // peer has been disabled for some time), return `channel_disabled`,
-                                                       // otherwise return `temporary_channel_failure`.
-                                                       if chan_update_opt.as_ref().map(|u| u.contents.flags & 2 == 2).unwrap_or(false) {
-                                                               break Some(("Forwarding channel has been disconnected for some time.", 0x1000 | 20, chan_update_opt));
-                                                       } else {
-                                                               break Some(("Forwarding channel is not in a ready state.", 0x1000 | 7, chan_update_opt));
-                                                       }
-                                               }
-                                               if *outgoing_amt_msat < chan.get_counterparty_htlc_minimum_msat() { // amount_below_minimum
-                                                       break Some(("HTLC amount was below the htlc_minimum_msat", 0x1000 | 11, chan_update_opt));
-                                               }
-                                               if let Err((err, code)) = chan.htlc_satisfies_config(&msg, *outgoing_amt_msat, *outgoing_cltv_value) {
-                                                       break Some((err, code, chan_update_opt));
-                                               }
-                                               chan_update_opt
-                                       } else {
-                                               if (msg.cltv_expiry as u64) < (*outgoing_cltv_value) as u64 + MIN_CLTV_EXPIRY_DELTA as u64 {
-                                                       // We really should set `incorrect_cltv_expiry` here but as we're not
-                                                       // forwarding over a real channel we can't generate a channel_update
-                                                       // for it. Instead we just return a generic temporary_node_failure.
-                                                       break Some((
-                                                               "Forwarding node has tampered with the intended HTLC values or origin node has an obsolete cltv_expiry_delta",
-                                                               0x2000 | 2, None,
-                                                       ));
-                                               }
-                                               None
-                                       };
-
-                                       let cur_height = self.best_block.read().unwrap().height() + 1;
-                                       // Theoretically, channel counterparty shouldn't send us a HTLC expiring now,
-                                       // but we want to be robust wrt to counterparty packet sanitization (see
-                                       // HTLC_FAIL_BACK_BUFFER rationale).
-                                       if msg.cltv_expiry <= cur_height + HTLC_FAIL_BACK_BUFFER as u32 { // expiry_too_soon
-                                               break Some(("CLTV expiry is too close", 0x1000 | 14, chan_update_opt));
-                                       }
-                                       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));
-                                       }
-                                       // If the HTLC expires ~now, don't bother trying to forward it to our
-                                       // counterparty. They should fail it anyway, but we don't want to bother with
-                                       // the round-trips or risk them deciding they definitely want the HTLC and
-                                       // force-closing to ensure they get it if we're offline.
-                                       // We previously had a much more aggressive check here which tried to ensure
-                                       // our counterparty receives an HTLC which has *our* risk threshold met on it,
-                                       // but there is no need to do that, and since we're a bit conservative with our
-                                       // risk threshold it just results in failing to forward payments.
-                                       if (*outgoing_cltv_value) as u64 <= (cur_height + LATENCY_GRACE_PERIOD_BLOCKS) as u64 {
-                                               break Some(("Outgoing CLTV value is too soon", 0x1000 | 14, chan_update_opt));
-                                       }
-
-                                       break None;
-                               }
-                               {
-                                       let mut res = VecWriter(Vec::with_capacity(chan_update.serialized_length() + 2 + 8 + 2));
-                                       if let Some(chan_update) = chan_update {
-                                               if code == 0x1000 | 11 || code == 0x1000 | 12 {
-                                                       msg.amount_msat.write(&mut res).expect("Writes cannot fail");
-                                               }
-                                               else if code == 0x1000 | 13 {
-                                                       msg.cltv_expiry.write(&mut res).expect("Writes cannot fail");
-                                               }
-                                               else if code == 0x1000 | 20 {
-                                                       // TODO: underspecified, follow https://github.com/lightning/bolts/issues/791
-                                                       0u16.write(&mut res).expect("Writes cannot fail");
-                                               }
-                                               (chan_update.serialized_length() as u16 + 2).write(&mut res).expect("Writes cannot fail");
-                                               msgs::ChannelUpdate::TYPE.write(&mut res).expect("Writes cannot fail");
-                                               chan_update.write(&mut res).expect("Writes cannot fail");
-                                       } else if code & 0x1000 == 0x1000 {
-                                               // If we're trying to return an error that requires a `channel_update` but
-                                               // we're forwarding to a phantom or intercept "channel" (i.e. cannot
-                                               // generate an update), just use the generic "temporary_node_failure"
-                                               // instead.
-                                               code = 0x2000 | 2;
-                                       }
-                                       return_err!(err, code, &res.0[..]);
-                               }
-                       }
                }
-
-               pending_forward_info
        }
 
        /// Gets the current [`channel_update`] for the given channel. This first checks if the channel is
@@ -2647,16 +3005,16 @@ where
        /// [`channel_update`]: msgs::ChannelUpdate
        /// [`internal_closing_signed`]: Self::internal_closing_signed
        fn get_channel_update_for_broadcast(&self, chan: &Channel<<SP::Target as SignerProvider>::Signer>) -> Result<msgs::ChannelUpdate, LightningError> {
-               if !chan.should_announce() {
+               if !chan.context.should_announce() {
                        return Err(LightningError {
                                err: "Cannot broadcast a channel_update for a private channel".to_owned(),
                                action: msgs::ErrorAction::IgnoreError
                        });
                }
-               if chan.get_short_channel_id().is_none() {
+               if chan.context.get_short_channel_id().is_none() {
                        return Err(LightningError{err: "Channel not yet established".to_owned(), action: msgs::ErrorAction::IgnoreError});
                }
-               log_trace!(self.logger, "Attempting to generate broadcast channel update for channel {}", log_bytes!(chan.channel_id()));
+               log_trace!(self.logger, "Attempting to generate broadcast channel update for channel {}", log_bytes!(chan.context.channel_id()));
                self.get_channel_update_for_unicast(chan)
        }
 
@@ -2672,19 +3030,20 @@ where
        /// [`channel_update`]: msgs::ChannelUpdate
        /// [`internal_closing_signed`]: Self::internal_closing_signed
        fn get_channel_update_for_unicast(&self, chan: &Channel<<SP::Target as SignerProvider>::Signer>) -> Result<msgs::ChannelUpdate, LightningError> {
-               log_trace!(self.logger, "Attempting to generate channel update for channel {}", log_bytes!(chan.channel_id()));
-               let short_channel_id = match chan.get_short_channel_id().or(chan.latest_inbound_scid_alias()) {
+               log_trace!(self.logger, "Attempting to generate channel update for channel {}", log_bytes!(chan.context.channel_id()));
+               let short_channel_id = match chan.context.get_short_channel_id().or(chan.context.latest_inbound_scid_alias()) {
                        None => return Err(LightningError{err: "Channel not yet established".to_owned(), action: msgs::ErrorAction::IgnoreError}),
                        Some(id) => id,
                };
 
                self.get_channel_update_for_onion(short_channel_id, chan)
        }
+
        fn get_channel_update_for_onion(&self, short_channel_id: u64, chan: &Channel<<SP::Target as SignerProvider>::Signer>) -> Result<msgs::ChannelUpdate, LightningError> {
-               log_trace!(self.logger, "Generating channel update for channel {}", log_bytes!(chan.channel_id()));
-               let were_node_one = self.our_network_pubkey.serialize()[..] < chan.get_counterparty_node_id().serialize()[..];
+               log_trace!(self.logger, "Generating channel update for channel {}", log_bytes!(chan.context.channel_id()));
+               let were_node_one = self.our_network_pubkey.serialize()[..] < chan.context.get_counterparty_node_id().serialize()[..];
 
-               let enabled = chan.is_usable() && match chan.channel_update_status() {
+               let enabled = chan.context.is_usable() && match chan.channel_update_status() {
                        ChannelUpdateStatus::Enabled => true,
                        ChannelUpdateStatus::DisabledStaged(_) => true,
                        ChannelUpdateStatus::Disabled => false,
@@ -2694,13 +3053,13 @@ where
                let unsigned = msgs::UnsignedChannelUpdate {
                        chain_hash: self.genesis_hash,
                        short_channel_id,
-                       timestamp: chan.get_update_time_counter(),
+                       timestamp: chan.context.get_update_time_counter(),
                        flags: (!were_node_one) as u8 | ((!enabled as u8) << 1),
-                       cltv_expiry_delta: chan.get_cltv_expiry_delta(),
-                       htlc_minimum_msat: chan.get_counterparty_htlc_minimum_msat(),
-                       htlc_maximum_msat: chan.get_announced_htlc_max_msat(),
-                       fee_base_msat: chan.get_outbound_forwarding_fee_base_msat(),
-                       fee_proportional_millionths: chan.get_fee_proportional_millionths(),
+                       cltv_expiry_delta: chan.context.get_cltv_expiry_delta(),
+                       htlc_minimum_msat: chan.context.get_counterparty_htlc_minimum_msat(),
+                       htlc_maximum_msat: chan.context.get_announced_htlc_max_msat(),
+                       fee_base_msat: chan.context.get_outbound_forwarding_fee_base_msat(),
+                       fee_proportional_millionths: chan.context.get_fee_proportional_millionths(),
                        excess_data: Vec::new(),
                };
                // Panic on failure to signal LDK should be restarted to retry signing the `ChannelUpdate`.
@@ -2748,32 +3107,31 @@ where
                        let mut peer_state_lock = peer_state_mutex.lock().unwrap();
                        let peer_state = &mut *peer_state_lock;
                        if let hash_map::Entry::Occupied(mut chan) = peer_state.channel_by_id.entry(id) {
-                               if !chan.get().is_live() {
+                               if !chan.get().context.is_live() {
                                        return Err(APIError::ChannelUnavailable{err: "Peer for first hop currently disconnected".to_owned()});
                                }
-                               let funding_txo = chan.get().get_funding_txo().unwrap();
+                               let funding_txo = chan.get().context.get_funding_txo().unwrap();
                                let send_res = chan.get_mut().send_htlc_and_commit(htlc_msat, payment_hash.clone(),
                                        htlc_cltv, HTLCSource::OutboundRoute {
                                                path: path.clone(),
                                                session_priv: session_priv.clone(),
                                                first_hop_htlc_msat: htlc_msat,
                                                payment_id,
-                                       }, onion_packet, &self.logger);
+                                       }, onion_packet, None, &self.fee_estimator, &self.logger);
                                match break_chan_entry!(self, send_res, chan) {
                                        Some(monitor_update) => {
-                                               let update_id = monitor_update.update_id;
-                                               let update_res = self.chain_monitor.update_channel(funding_txo, monitor_update);
-                                               if let Err(e) = handle_new_monitor_update!(self, update_res, update_id, peer_state_lock, peer_state, per_peer_state, chan) {
-                                                       break Err(e);
-                                               }
-                                               if update_res == ChannelMonitorUpdateStatus::InProgress {
-                                                       // Note that MonitorUpdateInProgress here indicates (per function
-                                                       // docs) that we will resend the commitment update once monitor
-                                                       // updating completes. Therefore, we must return an error
-                                                       // indicating that it is unsafe to retry the payment wholesale,
-                                                       // which we do in the send_payment check for
-                                                       // MonitorUpdateInProgress, below.
-                                                       return Err(APIError::MonitorUpdateInProgress);
+                                               match handle_new_monitor_update!(self, funding_txo, monitor_update, peer_state_lock, peer_state, per_peer_state, chan) {
+                                                       Err(e) => break Err(e),
+                                                       Ok(false) => {
+                                                               // Note that MonitorUpdateInProgress here indicates (per function
+                                                               // docs) that we will resend the commitment update once monitor
+                                                               // updating completes. Therefore, we must return an error
+                                                               // indicating that it is unsafe to retry the payment wholesale,
+                                                               // which we do in the send_payment check for
+                                                               // MonitorUpdateInProgress, below.
+                                                               return Err(APIError::MonitorUpdateInProgress);
+                                                       },
+                                                       Ok(true) => {},
                                                }
                                        },
                                        None => { },
@@ -2849,18 +3207,18 @@ where
        /// [`ChannelMonitorUpdateStatus::InProgress`]: crate::chain::ChannelMonitorUpdateStatus::InProgress
        pub fn send_payment_with_route(&self, route: &Route, payment_hash: PaymentHash, recipient_onion: RecipientOnionFields, payment_id: PaymentId) -> Result<(), PaymentSendFailure> {
                let best_block_height = self.best_block.read().unwrap().height();
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
                self.pending_outbound_payments
                        .send_payment_with_route(route, payment_hash, recipient_onion, payment_id, &self.entropy_source, &self.node_signer, best_block_height,
                                |path, payment_hash, recipient_onion, total_value, cur_height, payment_id, keysend_preimage, session_priv|
                                self.send_payment_along_path(path, payment_hash, recipient_onion, total_value, cur_height, payment_id, keysend_preimage, session_priv))
        }
 
-       /// Similar to [`ChannelManager::send_payment`], but will automatically find a route based on
+       /// Similar to [`ChannelManager::send_payment_with_route`], but will automatically find a route based on
        /// `route_params` and retry failed payment paths based on `retry_strategy`.
        pub fn send_payment(&self, payment_hash: PaymentHash, recipient_onion: RecipientOnionFields, payment_id: PaymentId, route_params: RouteParameters, retry_strategy: Retry) -> Result<(), RetryableSendFailure> {
                let best_block_height = self.best_block.read().unwrap().height();
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
                self.pending_outbound_payments
                        .send_payment(payment_hash, recipient_onion, payment_id, retry_strategy, route_params,
                                &self.router, self.list_usable_channels(), || self.compute_inflight_htlcs(),
@@ -2873,7 +3231,7 @@ where
        #[cfg(test)]
        pub(super) fn test_send_payment_internal(&self, route: &Route, payment_hash: PaymentHash, recipient_onion: RecipientOnionFields, keysend_preimage: Option<PaymentPreimage>, payment_id: PaymentId, recv_value_msat: Option<u64>, onion_session_privs: Vec<[u8; 32]>) -> Result<(), PaymentSendFailure> {
                let best_block_height = self.best_block.read().unwrap().height();
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
                self.pending_outbound_payments.test_send_payment_internal(route, payment_hash, recipient_onion, keysend_preimage, payment_id, recv_value_msat, onion_session_privs, &self.node_signer, best_block_height,
                        |path, payment_hash, recipient_onion, total_value, cur_height, payment_id, keysend_preimage, session_priv|
                        self.send_payment_along_path(path, payment_hash, recipient_onion, total_value, cur_height, payment_id, keysend_preimage, session_priv))
@@ -2908,7 +3266,7 @@ where
        /// [`Event::PaymentFailed`]: events::Event::PaymentFailed
        /// [`Event::PaymentSent`]: events::Event::PaymentSent
        pub fn abandon_payment(&self, payment_id: PaymentId) {
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
                self.pending_outbound_payments.abandon_payment(payment_id, PaymentFailureReason::UserAbandoned, &self.pending_events);
        }
 
@@ -2924,12 +3282,10 @@ where
        /// Similar to regular payments, you MUST NOT reuse a `payment_preimage` value. See
        /// [`send_payment`] for more information about the risks of duplicate preimage usage.
        ///
-       /// Note that `route` must have exactly one path.
-       ///
        /// [`send_payment`]: Self::send_payment
        pub fn send_spontaneous_payment(&self, route: &Route, payment_preimage: Option<PaymentPreimage>, recipient_onion: RecipientOnionFields, payment_id: PaymentId) -> Result<PaymentHash, PaymentSendFailure> {
                let best_block_height = self.best_block.read().unwrap().height();
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
                self.pending_outbound_payments.send_spontaneous_payment_with_route(
                        route, payment_preimage, recipient_onion, payment_id, &self.entropy_source,
                        &self.node_signer, best_block_height,
@@ -2946,7 +3302,7 @@ where
        /// [`PaymentParameters::for_keysend`]: crate::routing::router::PaymentParameters::for_keysend
        pub fn send_spontaneous_payment_with_retry(&self, payment_preimage: Option<PaymentPreimage>, recipient_onion: RecipientOnionFields, payment_id: PaymentId, route_params: RouteParameters, retry_strategy: Retry) -> Result<PaymentHash, RetryableSendFailure> {
                let best_block_height = self.best_block.read().unwrap().height();
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
                self.pending_outbound_payments.send_spontaneous_payment(payment_preimage, recipient_onion,
                        payment_id, retry_strategy, route_params, &self.router, self.list_usable_channels(),
                        || self.compute_inflight_htlcs(),  &self.entropy_source, &self.node_signer, best_block_height,
@@ -2960,7 +3316,7 @@ where
        /// us to easily discern them from real payments.
        pub fn send_probe(&self, path: Path) -> Result<(PaymentHash, PaymentId), PaymentSendFailure> {
                let best_block_height = self.best_block.read().unwrap().height();
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
                self.pending_outbound_payments.send_probe(path, self.probing_cookie_secret, &self.entropy_source, &self.node_signer, best_block_height,
                        |path, payment_hash, recipient_onion, total_value, cur_height, payment_id, keysend_preimage, session_priv|
                        self.send_payment_along_path(path, payment_hash, recipient_onion, total_value, cur_height, payment_id, keysend_preimage, session_priv))
@@ -2975,7 +3331,7 @@ where
 
        /// Handles the generation of a funding transaction, optionally (for tests) with a function
        /// which checks the correctness of the funding transaction given the associated channel.
-       fn funding_transaction_generated_intern<FundingOutput: Fn(&Channel<<SP::Target as SignerProvider>::Signer>, &Transaction) -> Result<OutPoint, APIError>>(
+       fn funding_transaction_generated_intern<FundingOutput: Fn(&OutboundV1Channel<<SP::Target as SignerProvider>::Signer>, &Transaction) -> Result<OutPoint, APIError>>(
                &self, temporary_channel_id: &[u8; 32], counterparty_node_id: &PublicKey, funding_transaction: Transaction, find_funding_output: FundingOutput
        ) -> Result<(), APIError> {
                let per_peer_state = self.per_peer_state.read().unwrap();
@@ -2984,21 +3340,24 @@ where
 
                let mut peer_state_lock = peer_state_mutex.lock().unwrap();
                let peer_state = &mut *peer_state_lock;
-               let (msg, chan) = match peer_state.channel_by_id.remove(temporary_channel_id) {
-                       Some(mut chan) => {
+               let (chan, msg) = match peer_state.outbound_v1_channel_by_id.remove(temporary_channel_id) {
+                       Some(chan) => {
                                let funding_txo = find_funding_output(&chan, &funding_transaction)?;
 
                                let funding_res = chan.get_outbound_funding_created(funding_transaction, funding_txo, &self.logger)
-                                       .map_err(|e| if let ChannelError::Close(msg) = e {
-                                               MsgHandleErrInternal::from_finish_shutdown(msg, chan.channel_id(), chan.get_user_id(), chan.force_shutdown(true), None)
+                                       .map_err(|(mut chan, e)| if let ChannelError::Close(msg) = e {
+                                               let channel_id = chan.context.channel_id();
+                                               let user_id = chan.context.get_user_id();
+                                               let shutdown_res = chan.context.force_shutdown(false);
+                                               (chan, MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, user_id, shutdown_res, None))
                                        } else { unreachable!(); });
                                match funding_res {
-                                       Ok(funding_msg) => (funding_msg, chan),
-                                       Err(_) => {
+                                       Ok((chan, funding_msg)) => (chan, funding_msg),
+                                       Err((chan, err)) => {
                                                mem::drop(peer_state_lock);
                                                mem::drop(per_peer_state);
 
-                                               let _ = handle_error!(self, funding_res, chan.get_counterparty_node_id());
+                                               let _: Result<(), _> = handle_error!(self, Err(err), chan.context.get_counterparty_node_id());
                                                return Err(APIError::ChannelUnavailable {
                                                        err: "Signer refused to sign the initial commitment transaction".to_owned()
                                                });
@@ -3015,16 +3374,16 @@ where
                };
 
                peer_state.pending_msg_events.push(events::MessageSendEvent::SendFundingCreated {
-                       node_id: chan.get_counterparty_node_id(),
+                       node_id: chan.context.get_counterparty_node_id(),
                        msg,
                });
-               match peer_state.channel_by_id.entry(chan.channel_id()) {
+               match peer_state.channel_by_id.entry(chan.context.channel_id()) {
                        hash_map::Entry::Occupied(_) => {
                                panic!("Generated duplicate funding txid?");
                        },
                        hash_map::Entry::Vacant(e) => {
                                let mut id_to_peer = self.id_to_peer.lock().unwrap();
-                               if id_to_peer.insert(chan.channel_id(), chan.get_counterparty_node_id()).is_some() {
+                               if id_to_peer.insert(chan.context.channel_id(), chan.context.get_counterparty_node_id()).is_some() {
                                        panic!("id_to_peer map already contained funding txid, which shouldn't be possible");
                                }
                                e.insert(chan);
@@ -3071,7 +3430,7 @@ where
        /// [`Event::FundingGenerationReady`]: crate::events::Event::FundingGenerationReady
        /// [`Event::ChannelClosed`]: crate::events::Event::ChannelClosed
        pub fn funding_transaction_generated(&self, temporary_channel_id: &[u8; 32], counterparty_node_id: &PublicKey, funding_transaction: Transaction) -> Result<(), APIError> {
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
 
                for inp in funding_transaction.input.iter() {
                        if inp.witness.is_empty() {
@@ -3100,9 +3459,9 @@ where
                        }
 
                        let mut output_index = None;
-                       let expected_spk = chan.get_funding_redeemscript().to_v0_p2wsh();
+                       let expected_spk = chan.context.get_funding_redeemscript().to_v0_p2wsh();
                        for (idx, outp) in tx.output.iter().enumerate() {
-                               if outp.script_pubkey == expected_spk && outp.value == chan.get_value_satoshis() {
+                               if outp.script_pubkey == expected_spk && outp.value == chan.context.get_value_satoshis() {
                                        if output_index.is_some() {
                                                return Err(APIError::APIMisuseError {
                                                        err: "Multiple outputs matched the expected script and value".to_owned()
@@ -3120,7 +3479,7 @@ where
                })
        }
 
-       /// Atomically updates the [`ChannelConfig`] for the given channels.
+       /// Atomically applies partial updates to the [`ChannelConfig`] of the given channels.
        ///
        /// Once the updates are applied, each eligible channel (advertised with a known short channel
        /// ID and a change in [`forwarding_fee_proportional_millionths`], [`forwarding_fee_base_msat`],
@@ -3142,18 +3501,16 @@ where
        /// [`ChannelUpdate`]: msgs::ChannelUpdate
        /// [`ChannelUnavailable`]: APIError::ChannelUnavailable
        /// [`APIMisuseError`]: APIError::APIMisuseError
-       pub fn update_channel_config(
-               &self, counterparty_node_id: &PublicKey, channel_ids: &[[u8; 32]], config: &ChannelConfig,
+       pub fn update_partial_channel_config(
+               &self, counterparty_node_id: &PublicKey, channel_ids: &[[u8; 32]], config_update: &ChannelConfigUpdate,
        ) -> Result<(), APIError> {
-               if config.cltv_expiry_delta < MIN_CLTV_EXPIRY_DELTA {
+               if config_update.cltv_expiry_delta.map(|delta| delta < MIN_CLTV_EXPIRY_DELTA).unwrap_or(false) {
                        return Err(APIError::APIMisuseError {
                                err: format!("The chosen CLTV expiry delta is below the minimum of {}", MIN_CLTV_EXPIRY_DELTA),
                        });
                }
 
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(
-                       &self.total_consistency_lock, &self.persistence_notifier,
-               );
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
                let per_peer_state = self.per_peer_state.read().unwrap();
                let peer_state_mutex = per_peer_state.get(counterparty_node_id)
                        .ok_or_else(|| APIError::ChannelUnavailable { err: format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id) })?;
@@ -3168,14 +3525,16 @@ where
                }
                for channel_id in channel_ids {
                        let channel = peer_state.channel_by_id.get_mut(channel_id).unwrap();
-                       if !channel.update_config(config) {
+                       let mut config = channel.context.config();
+                       config.apply(config_update);
+                       if !channel.context.update_config(&config) {
                                continue;
                        }
                        if let Ok(msg) = self.get_channel_update_for_broadcast(channel) {
                                peer_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate { msg });
                        } else if let Ok(msg) = self.get_channel_update_for_unicast(channel) {
                                peer_state.pending_msg_events.push(events::MessageSendEvent::SendChannelUpdate {
-                                       node_id: channel.get_counterparty_node_id(),
+                                       node_id: channel.context.get_counterparty_node_id(),
                                        msg,
                                });
                        }
@@ -3183,6 +3542,34 @@ where
                Ok(())
        }
 
+       /// Atomically updates the [`ChannelConfig`] for the given channels.
+       ///
+       /// Once the updates are applied, each eligible channel (advertised with a known short channel
+       /// ID and a change in [`forwarding_fee_proportional_millionths`], [`forwarding_fee_base_msat`],
+       /// or [`cltv_expiry_delta`]) has a [`BroadcastChannelUpdate`] event message generated
+       /// containing the new [`ChannelUpdate`] message which should be broadcast to the network.
+       ///
+       /// Returns [`ChannelUnavailable`] when a channel is not found or an incorrect
+       /// `counterparty_node_id` is provided.
+       ///
+       /// Returns [`APIMisuseError`] when a [`cltv_expiry_delta`] update is to be applied with a value
+       /// below [`MIN_CLTV_EXPIRY_DELTA`].
+       ///
+       /// If an error is returned, none of the updates should be considered applied.
+       ///
+       /// [`forwarding_fee_proportional_millionths`]: ChannelConfig::forwarding_fee_proportional_millionths
+       /// [`forwarding_fee_base_msat`]: ChannelConfig::forwarding_fee_base_msat
+       /// [`cltv_expiry_delta`]: ChannelConfig::cltv_expiry_delta
+       /// [`BroadcastChannelUpdate`]: events::MessageSendEvent::BroadcastChannelUpdate
+       /// [`ChannelUpdate`]: msgs::ChannelUpdate
+       /// [`ChannelUnavailable`]: APIError::ChannelUnavailable
+       /// [`APIMisuseError`]: APIError::APIMisuseError
+       pub fn update_channel_config(
+               &self, counterparty_node_id: &PublicKey, channel_ids: &[[u8; 32]], config: &ChannelConfig,
+       ) -> Result<(), APIError> {
+               return self.update_partial_channel_config(counterparty_node_id, channel_ids, &(*config).into());
+       }
+
        /// Attempts to forward an intercepted HTLC over the provided channel id and with the provided
        /// amount to forward. Should only be called in response to an [`HTLCIntercepted`] event.
        ///
@@ -3196,17 +3583,20 @@ where
        /// [`ChannelManager::fail_intercepted_htlc`] MUST be called in response to the event.
        ///
        /// Note that LDK does not enforce fee requirements in `amt_to_forward_msat`, and will not stop
-       /// you from forwarding more than you received.
+       /// you from forwarding more than you received. See
+       /// [`HTLCIntercepted::expected_outbound_amount_msat`] for more on forwarding a different amount
+       /// than expected.
        ///
        /// Errors if the event was not handled in time, in which case the HTLC was automatically failed
        /// backwards.
        ///
        /// [`UserConfig::accept_intercept_htlcs`]: crate::util::config::UserConfig::accept_intercept_htlcs
        /// [`HTLCIntercepted`]: events::Event::HTLCIntercepted
+       /// [`HTLCIntercepted::expected_outbound_amount_msat`]: events::Event::HTLCIntercepted::expected_outbound_amount_msat
        // TODO: when we move to deciding the best outbound channel at forward time, only take
        // `next_node_id` and not `next_hop_channel_id`
        pub fn forward_intercepted_htlc(&self, intercept_id: InterceptId, next_hop_channel_id: &[u8; 32], next_node_id: PublicKey, amt_to_forward_msat: u64) -> Result<(), APIError> {
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
 
                let next_hop_scid = {
                        let peer_state_lock = self.per_peer_state.read().unwrap();
@@ -3216,15 +3606,16 @@ where
                        let peer_state = &mut *peer_state_lock;
                        match peer_state.channel_by_id.get(next_hop_channel_id) {
                                Some(chan) => {
-                                       if !chan.is_usable() {
+                                       if !chan.context.is_usable() {
                                                return Err(APIError::ChannelUnavailable {
                                                        err: format!("Channel with id {} not fully established", log_bytes!(*next_hop_channel_id))
                                                })
                                        }
-                                       chan.get_short_channel_id().unwrap_or(chan.outbound_scid_alias())
+                                       chan.context.get_short_channel_id().unwrap_or(chan.context.outbound_scid_alias())
                                },
                                None => return Err(APIError::ChannelUnavailable {
-                                       err: format!("Channel with id {} not found for the passed counterparty node_id {}", log_bytes!(*next_hop_channel_id), next_node_id)
+                                       err: format!("Funded channel with id {} not found for the passed counterparty node_id {}. Channel may still be opening.",
+                                               log_bytes!(*next_hop_channel_id), next_node_id)
                                })
                        }
                };
@@ -3240,7 +3631,10 @@ where
                        },
                        _ => unreachable!() // Only `PendingHTLCRouting::Forward`s are intercepted
                };
+               let skimmed_fee_msat =
+                       payment.forward_info.outgoing_amt_msat.saturating_sub(amt_to_forward_msat);
                let pending_htlc_info = PendingHTLCInfo {
+                       skimmed_fee_msat: if skimmed_fee_msat == 0 { None } else { Some(skimmed_fee_msat) },
                        outgoing_amt_msat: amt_to_forward_msat, routing, ..payment.forward_info
                };
 
@@ -3262,7 +3656,7 @@ where
        ///
        /// [`HTLCIntercepted`]: events::Event::HTLCIntercepted
        pub fn fail_intercepted_htlc(&self, intercept_id: InterceptId) -> Result<(), APIError> {
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
 
                let payment = self.pending_intercepted_htlcs.lock().unwrap().remove(&intercept_id)
                        .ok_or_else(|| APIError::APIMisuseError {
@@ -3291,7 +3685,7 @@ where
        /// 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 _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
 
                let mut new_events = VecDeque::new();
                let mut failed_forwards = Vec::new();
@@ -3310,7 +3704,7 @@ where
                                                                                prev_short_channel_id, prev_htlc_id, prev_funding_outpoint, prev_user_channel_id,
                                                                                forward_info: PendingHTLCInfo {
                                                                                        routing, incoming_shared_secret, payment_hash, outgoing_amt_msat,
-                                                                                       outgoing_cltv_value, incoming_amt_msat: _
+                                                                                       outgoing_cltv_value, ..
                                                                                }
                                                                        }) => {
                                                                                macro_rules! failure_handler {
@@ -3372,7 +3766,10 @@ where
                                                                                                };
                                                                                                match next_hop {
                                                                                                        onion_utils::Hop::Receive(hop_data) => {
-                                                                                                               match self.construct_recv_pending_htlc_info(hop_data, incoming_shared_secret, payment_hash, outgoing_amt_msat, outgoing_cltv_value, Some(phantom_shared_secret)) {
+                                                                                                               match self.construct_recv_pending_htlc_info(hop_data,
+                                                                                                                       incoming_shared_secret, payment_hash, outgoing_amt_msat,
+                                                                                                                       outgoing_cltv_value, Some(phantom_shared_secret), false, None)
+                                                                                                               {
                                                                                                                        Ok(info) => phantom_receives.push((prev_short_channel_id, prev_funding_outpoint, prev_user_channel_id, vec![(info, prev_htlc_id)])),
                                                                                                                        Err(ReceiveError { err_code, err_data, msg }) => failed_payment!(msg, err_code, err_data, Some(phantom_shared_secret))
                                                                                                                }
@@ -3423,7 +3820,7 @@ where
                                                                                prev_short_channel_id, prev_htlc_id, prev_funding_outpoint, prev_user_channel_id: _,
                                                                                forward_info: PendingHTLCInfo {
                                                                                        incoming_shared_secret, payment_hash, outgoing_amt_msat, outgoing_cltv_value,
-                                                                                       routing: PendingHTLCRouting::Forward { onion_packet, .. }, incoming_amt_msat: _,
+                                                                                       routing: PendingHTLCRouting::Forward { onion_packet, .. }, skimmed_fee_msat, ..
                                                                                },
                                                                        }) => {
                                                                                log_trace!(self.logger, "Adding HTLC from short id {} with payment_hash {} to channel with short id {} after delay", prev_short_channel_id, log_bytes!(payment_hash.0), short_chan_id);
@@ -3437,7 +3834,8 @@ where
                                                                                });
                                                                                if let Err(e) = chan.get_mut().queue_add_htlc(outgoing_amt_msat,
                                                                                        payment_hash, outgoing_cltv_value, htlc_source.clone(),
-                                                                                       onion_packet, &self.logger)
+                                                                                       onion_packet, skimmed_fee_msat, &self.fee_estimator,
+                                                                                       &self.logger)
                                                                                {
                                                                                        if let ChannelError::Ignore(msg) = e {
                                                                                                log_trace!(self.logger, "Failed to forward HTLC with payment_hash {}: {}", log_bytes!(payment_hash.0), msg);
@@ -3447,7 +3845,7 @@ where
                                                                                        let (failure_code, data) = self.get_htlc_temp_fail_err_and_data(0x1000|7, short_chan_id, chan.get());
                                                                                        failed_forwards.push((htlc_source, payment_hash,
                                                                                                HTLCFailReason::reason(failure_code, data),
-                                                                                               HTLCDestination::NextHopChannel { node_id: Some(chan.get().get_counterparty_node_id()), channel_id: forward_chan_id }
+                                                                                               HTLCDestination::NextHopChannel { node_id: Some(chan.get().context.get_counterparty_node_id()), channel_id: forward_chan_id }
                                                                                        ));
                                                                                        continue;
                                                                                }
@@ -3481,7 +3879,8 @@ where
                                                        HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
                                                                prev_short_channel_id, prev_htlc_id, prev_funding_outpoint, prev_user_channel_id,
                                                                forward_info: PendingHTLCInfo {
-                                                                       routing, incoming_shared_secret, payment_hash, incoming_amt_msat, outgoing_amt_msat, ..
+                                                                       routing, incoming_shared_secret, payment_hash, incoming_amt_msat, outgoing_amt_msat,
+                                                                       skimmed_fee_msat, ..
                                                                }
                                                        }) => {
                                                                let (cltv_expiry, onion_payload, payment_data, phantom_shared_secret, mut onion_fields) = match routing {
@@ -3492,16 +3891,19 @@ where
                                                                                (incoming_cltv_expiry, OnionPayload::Invoice { _legacy_hop_data },
                                                                                        Some(payment_data), phantom_shared_secret, onion_fields)
                                                                        },
-                                                                       PendingHTLCRouting::ReceiveKeysend { payment_preimage, payment_metadata, incoming_cltv_expiry } => {
-                                                                               let onion_fields = RecipientOnionFields { payment_secret: None, payment_metadata };
+                                                                       PendingHTLCRouting::ReceiveKeysend { payment_data, payment_preimage, payment_metadata, incoming_cltv_expiry } => {
+                                                                               let onion_fields = RecipientOnionFields {
+                                                                                       payment_secret: payment_data.as_ref().map(|data| data.payment_secret),
+                                                                                       payment_metadata
+                                                                               };
                                                                                (incoming_cltv_expiry, OnionPayload::Spontaneous(payment_preimage),
-                                                                                       None, None, onion_fields)
+                                                                                       payment_data, None, onion_fields)
                                                                        },
                                                                        _ => {
                                                                                panic!("short_channel_id == 0 should imply any pending_forward entries are of type Receive");
                                                                        }
                                                                };
-                                                               let mut claimable_htlc = ClaimableHTLC {
+                                                               let claimable_htlc = ClaimableHTLC {
                                                                        prev_hop: HTLCPreviousHopData {
                                                                                short_channel_id: prev_short_channel_id,
                                                                                outpoint: prev_funding_outpoint,
@@ -3519,6 +3921,7 @@ where
                                                                        total_msat: if let Some(data) = &payment_data { data.total_msat } else { outgoing_amt_msat },
                                                                        cltv_expiry,
                                                                        onion_payload,
+                                                                       counterparty_skimmed_fee_msat: skimmed_fee_msat,
                                                                };
 
                                                                let mut committed_to_claimable = false;
@@ -3551,13 +3954,11 @@ where
                                                                }
 
                                                                macro_rules! check_total_value {
-                                                                       ($payment_data: expr, $payment_preimage: expr) => {{
+                                                                       ($purpose: expr) => {{
                                                                                let mut payment_claimable_generated = false;
-                                                                               let purpose = || {
-                                                                                       events::PaymentPurpose::InvoicePayment {
-                                                                                               payment_preimage: $payment_preimage,
-                                                                                               payment_secret: $payment_data.payment_secret,
-                                                                                       }
+                                                                               let is_keysend = match $purpose {
+                                                                                       events::PaymentPurpose::SpontaneousPayment(_) => true,
+                                                                                       events::PaymentPurpose::InvoicePayment { .. } => false,
                                                                                };
                                                                                let mut claimable_payments = self.claimable_payments.lock().unwrap();
                                                                                if claimable_payments.pending_claiming_payments.contains_key(&payment_hash) {
@@ -3569,9 +3970,18 @@ where
                                                                                        .or_insert_with(|| {
                                                                                                committed_to_claimable = true;
                                                                                                ClaimablePayment {
-                                                                                                       purpose: purpose(), htlcs: Vec::new(), onion_fields: None,
+                                                                                                       purpose: $purpose.clone(), htlcs: Vec::new(), onion_fields: None,
                                                                                                }
                                                                                        });
+                                                                               if $purpose != claimable_payment.purpose {
+                                                                                       let log_keysend = |keysend| if keysend { "keysend" } else { "non-keysend" };
+                                                                                       log_trace!(self.logger, "Failing new {} HTLC with payment_hash {} as we already had an existing {} HTLC with the same payment hash", log_keysend(is_keysend), log_bytes!(payment_hash.0), log_keysend(!is_keysend));
+                                                                                       fail_htlc!(claimable_htlc, payment_hash);
+                                                                               }
+                                                                               if !self.default_configuration.accept_mpp_keysend && is_keysend && !claimable_payment.htlcs.is_empty() {
+                                                                                       log_trace!(self.logger, "Failing new keysend HTLC with payment_hash {} as we already had an existing keysend HTLC with the same payment hash and our config states we don't accept MPP keysend", log_bytes!(payment_hash.0));
+                                                                                       fail_htlc!(claimable_htlc, payment_hash);
+                                                                               }
                                                                                if let Some(earlier_fields) = &mut claimable_payment.onion_fields {
                                                                                        if earlier_fields.check_merge(&mut onion_fields).is_err() {
                                                                                                fail_htlc!(claimable_htlc, payment_hash);
@@ -3580,38 +3990,27 @@ where
                                                                                        claimable_payment.onion_fields = Some(onion_fields);
                                                                                }
                                                                                let ref mut htlcs = &mut claimable_payment.htlcs;
-                                                                               if htlcs.len() == 1 {
-                                                                                       if let OnionPayload::Spontaneous(_) = htlcs[0].onion_payload {
-                                                                                               log_trace!(self.logger, "Failing new HTLC with payment_hash {} as we already had an existing keysend HTLC with the same payment hash", log_bytes!(payment_hash.0));
-                                                                                               fail_htlc!(claimable_htlc, payment_hash);
-                                                                                       }
-                                                                               }
                                                                                let mut total_value = claimable_htlc.sender_intended_value;
                                                                                let mut earliest_expiry = claimable_htlc.cltv_expiry;
                                                                                for htlc in htlcs.iter() {
                                                                                        total_value += htlc.sender_intended_value;
                                                                                        earliest_expiry = cmp::min(earliest_expiry, htlc.cltv_expiry);
-                                                                                       match &htlc.onion_payload {
-                                                                                               OnionPayload::Invoice { .. } => {
-                                                                                                       if htlc.total_msat != $payment_data.total_msat {
-                                                                                                               log_trace!(self.logger, "Failing HTLCs with payment_hash {} as the HTLCs had inconsistent total values (eg {} and {})",
-                                                                                                                       log_bytes!(payment_hash.0), $payment_data.total_msat, htlc.total_msat);
-                                                                                                               total_value = msgs::MAX_VALUE_MSAT;
-                                                                                                       }
-                                                                                                       if total_value >= msgs::MAX_VALUE_MSAT { break; }
-                                                                                               },
-                                                                                               _ => unreachable!(),
+                                                                                       if htlc.total_msat != claimable_htlc.total_msat {
+                                                                                               log_trace!(self.logger, "Failing HTLCs with payment_hash {} as the HTLCs had inconsistent total values (eg {} and {})",
+                                                                                                       log_bytes!(payment_hash.0), claimable_htlc.total_msat, htlc.total_msat);
+                                                                                               total_value = msgs::MAX_VALUE_MSAT;
                                                                                        }
+                                                                                       if total_value >= msgs::MAX_VALUE_MSAT { break; }
                                                                                }
                                                                                // The condition determining whether an MPP is complete must
                                                                                // match exactly the condition used in `timer_tick_occurred`
                                                                                if total_value >= msgs::MAX_VALUE_MSAT {
                                                                                        fail_htlc!(claimable_htlc, payment_hash);
-                                                                               } else if total_value - claimable_htlc.sender_intended_value >= $payment_data.total_msat {
+                                                                               } else if total_value - claimable_htlc.sender_intended_value >= claimable_htlc.total_msat {
                                                                                        log_trace!(self.logger, "Failing HTLC with payment_hash {} as payment is already claimable",
                                                                                                log_bytes!(payment_hash.0));
                                                                                        fail_htlc!(claimable_htlc, payment_hash);
-                                                                               } else if total_value >= $payment_data.total_msat {
+                                                                               } else if total_value >= claimable_htlc.total_msat {
                                                                                        #[allow(unused_assignments)] {
                                                                                                committed_to_claimable = true;
                                                                                        }
@@ -3619,11 +4018,16 @@ where
                                                                                        htlcs.push(claimable_htlc);
                                                                                        let amount_msat = htlcs.iter().map(|htlc| htlc.value).sum();
                                                                                        htlcs.iter_mut().for_each(|htlc| htlc.total_value_received = Some(amount_msat));
+                                                                                       let counterparty_skimmed_fee_msat = htlcs.iter()
+                                                                                               .map(|htlc| htlc.counterparty_skimmed_fee_msat.unwrap_or(0)).sum();
+                                                                                       debug_assert!(total_value.saturating_sub(amount_msat) <=
+                                                                                               counterparty_skimmed_fee_msat);
                                                                                        new_events.push_back((events::Event::PaymentClaimable {
                                                                                                receiver_node_id: Some(receiver_node_id),
                                                                                                payment_hash,
-                                                                                               purpose: purpose(),
+                                                                                               purpose: $purpose,
                                                                                                amount_msat,
+                                                                                               counterparty_skimmed_fee_msat,
                                                                                                via_channel_id: Some(prev_channel_id),
                                                                                                via_user_channel_id: Some(prev_user_channel_id),
                                                                                                claim_deadline: Some(earliest_expiry - HTLC_FAIL_BACK_BUFFER),
@@ -3670,49 +4074,23 @@ where
                                                                                                                fail_htlc!(claimable_htlc, payment_hash);
                                                                                                        }
                                                                                                }
-                                                                                               check_total_value!(payment_data, payment_preimage);
+                                                                                               let purpose = events::PaymentPurpose::InvoicePayment {
+                                                                                                       payment_preimage: payment_preimage.clone(),
+                                                                                                       payment_secret: payment_data.payment_secret,
+                                                                                               };
+                                                                                               check_total_value!(purpose);
                                                                                        },
                                                                                        OnionPayload::Spontaneous(preimage) => {
-                                                                                               let mut claimable_payments = self.claimable_payments.lock().unwrap();
-                                                                                               if claimable_payments.pending_claiming_payments.contains_key(&payment_hash) {
-                                                                                                       fail_htlc!(claimable_htlc, payment_hash);
-                                                                                               }
-                                                                                               match claimable_payments.claimable_payments.entry(payment_hash) {
-                                                                                                       hash_map::Entry::Vacant(e) => {
-                                                                                                               let amount_msat = claimable_htlc.value;
-                                                                                                               claimable_htlc.total_value_received = Some(amount_msat);
-                                                                                                               let claim_deadline = Some(claimable_htlc.cltv_expiry - HTLC_FAIL_BACK_BUFFER);
-                                                                                                               let purpose = events::PaymentPurpose::SpontaneousPayment(preimage);
-                                                                                                               e.insert(ClaimablePayment {
-                                                                                                                       purpose: purpose.clone(),
-                                                                                                                       onion_fields: Some(onion_fields.clone()),
-                                                                                                                       htlcs: vec![claimable_htlc],
-                                                                                                               });
-                                                                                                               let prev_channel_id = prev_funding_outpoint.to_channel_id();
-                                                                                                               new_events.push_back((events::Event::PaymentClaimable {
-                                                                                                                       receiver_node_id: Some(receiver_node_id),
-                                                                                                                       payment_hash,
-                                                                                                                       amount_msat,
-                                                                                                                       purpose,
-                                                                                                                       via_channel_id: Some(prev_channel_id),
-                                                                                                                       via_user_channel_id: Some(prev_user_channel_id),
-                                                                                                                       claim_deadline,
-                                                                                                                       onion_fields: Some(onion_fields),
-                                                                                                               }, None));
-                                                                                                       },
-                                                                                                       hash_map::Entry::Occupied(_) => {
-                                                                                                               log_trace!(self.logger, "Failing new keysend HTLC with payment_hash {} for a duplicative payment hash", log_bytes!(payment_hash.0));
-                                                                                                               fail_htlc!(claimable_htlc, payment_hash);
-                                                                                                       }
-                                                                                               }
+                                                                                               let purpose = events::PaymentPurpose::SpontaneousPayment(preimage);
+                                                                                               check_total_value!(purpose);
                                                                                        }
                                                                                }
                                                                        },
                                                                        hash_map::Entry::Occupied(inbound_payment) => {
-                                                                               if payment_data.is_none() {
+                                                                               if let OnionPayload::Spontaneous(_) = claimable_htlc.onion_payload {
                                                                                        log_trace!(self.logger, "Failing new keysend HTLC with payment_hash {} because we already have an inbound payment with the same payment hash", log_bytes!(payment_hash.0));
                                                                                        fail_htlc!(claimable_htlc, payment_hash);
-                                                                               };
+                                                                               }
                                                                                let payment_data = payment_data.unwrap();
                                                                                if inbound_payment.get().payment_secret != payment_data.payment_secret {
                                                                                        log_trace!(self.logger, "Failing new HTLC with payment_hash {} as it didn't match our expected payment secret.", log_bytes!(payment_hash.0));
@@ -3722,7 +4100,11 @@ where
                                                                                                log_bytes!(payment_hash.0), payment_data.total_msat, inbound_payment.get().min_value_msat.unwrap());
                                                                                        fail_htlc!(claimable_htlc, payment_hash);
                                                                                } else {
-                                                                                       let payment_claimable_generated = check_total_value!(payment_data, inbound_payment.get().payment_preimage);
+                                                                                       let purpose = events::PaymentPurpose::InvoicePayment {
+                                                                                               payment_preimage: inbound_payment.get().payment_preimage,
+                                                                                               payment_secret: payment_data.payment_secret,
+                                                                                       };
+                                                                                       let payment_claimable_generated = check_total_value!(purpose);
                                                                                        if payment_claimable_generated {
                                                                                                inbound_payment.remove_entry();
                                                                                        }
@@ -3762,54 +4144,87 @@ where
                events.append(&mut new_events);
        }
 
-       /// Free the background events, generally called from timer_tick_occurred.
-       ///
-       /// Exposed for testing to allow us to process events quickly without generating accidental
-       /// BroadcastChannelUpdate events in timer_tick_occurred.
+       /// Free the background events, generally called from [`PersistenceNotifierGuard`] constructors.
        ///
        /// Expects the caller to have a total_consistency_lock read lock.
-       fn process_background_events(&self) -> bool {
+       fn process_background_events(&self) -> NotifyOption {
+               debug_assert_ne!(self.total_consistency_lock.held_by_thread(), LockHeldState::NotHeldByThread);
+
+               #[cfg(debug_assertions)]
+               self.background_events_processed_since_startup.store(true, Ordering::Release);
+
                let mut background_events = Vec::new();
                mem::swap(&mut *self.pending_background_events.lock().unwrap(), &mut background_events);
                if background_events.is_empty() {
-                       return false;
+                       return NotifyOption::SkipPersist;
                }
 
                for event in background_events.drain(..) {
                        match event {
-                               BackgroundEvent::MonitorUpdateRegeneratedOnStartup((funding_txo, update)) => {
+                               BackgroundEvent::ClosingMonitorUpdateRegeneratedOnStartup((funding_txo, update)) => {
                                        // The channel has already been closed, so no use bothering to care about the
                                        // monitor updating completing.
                                        let _ = self.chain_monitor.update_channel(funding_txo, &update);
                                },
+                               BackgroundEvent::MonitorUpdateRegeneratedOnStartup { counterparty_node_id, funding_txo, update } => {
+                                       let mut updated_chan = false;
+                                       let res = {
+                                               let per_peer_state = self.per_peer_state.read().unwrap();
+                                               if let Some(peer_state_mutex) = per_peer_state.get(&counterparty_node_id) {
+                                                       let mut peer_state_lock = peer_state_mutex.lock().unwrap();
+                                                       let peer_state = &mut *peer_state_lock;
+                                                       match peer_state.channel_by_id.entry(funding_txo.to_channel_id()) {
+                                                               hash_map::Entry::Occupied(mut chan) => {
+                                                                       updated_chan = true;
+                                                                       handle_new_monitor_update!(self, funding_txo, update.clone(),
+                                                                               peer_state_lock, peer_state, per_peer_state, chan).map(|_| ())
+                                                               },
+                                                               hash_map::Entry::Vacant(_) => Ok(()),
+                                                       }
+                                               } else { Ok(()) }
+                                       };
+                                       if !updated_chan {
+                                               // TODO: Track this as in-flight even though the channel is closed.
+                                               let _ = self.chain_monitor.update_channel(funding_txo, &update);
+                                       }
+                                       // TODO: If this channel has since closed, we're likely providing a payment
+                                       // preimage update, which we must ensure is durable! We currently don't,
+                                       // however, ensure that.
+                                       if res.is_err() {
+                                               log_error!(self.logger,
+                                                       "Failed to provide ChannelMonitorUpdate to closed channel! This likely lost us a payment preimage!");
+                                       }
+                                       let _ = handle_error!(self, res, counterparty_node_id);
+                               },
                        }
                }
-               true
+               NotifyOption::DoPersist
        }
 
        #[cfg(any(test, feature = "_test_utils"))]
        /// Process background events, for functional testing
        pub fn test_process_background_events(&self) {
-               self.process_background_events();
+               let _lck = self.total_consistency_lock.read().unwrap();
+               let _ = self.process_background_events();
        }
 
        fn update_channel_fee(&self, chan_id: &[u8; 32], chan: &mut Channel<<SP::Target as SignerProvider>::Signer>, new_feerate: u32) -> NotifyOption {
-               if !chan.is_outbound() { return NotifyOption::SkipPersist; }
+               if !chan.context.is_outbound() { return NotifyOption::SkipPersist; }
                // If the feerate has decreased by less than half, don't bother
-               if new_feerate <= chan.get_feerate_sat_per_1000_weight() && new_feerate * 2 > chan.get_feerate_sat_per_1000_weight() {
+               if new_feerate <= chan.context.get_feerate_sat_per_1000_weight() && new_feerate * 2 > chan.context.get_feerate_sat_per_1000_weight() {
                        log_trace!(self.logger, "Channel {} does not qualify for a feerate change from {} to {}.",
-                               log_bytes!(chan_id[..]), chan.get_feerate_sat_per_1000_weight(), new_feerate);
+                               log_bytes!(chan_id[..]), chan.context.get_feerate_sat_per_1000_weight(), new_feerate);
                        return NotifyOption::SkipPersist;
                }
-               if !chan.is_live() {
+               if !chan.context.is_live() {
                        log_trace!(self.logger, "Channel {} does not qualify for a feerate change from {} to {} as it cannot currently be updated (probably the peer is disconnected).",
-                               log_bytes!(chan_id[..]), chan.get_feerate_sat_per_1000_weight(), new_feerate);
+                               log_bytes!(chan_id[..]), chan.context.get_feerate_sat_per_1000_weight(), new_feerate);
                        return NotifyOption::SkipPersist;
                }
                log_trace!(self.logger, "Channel {} qualifies for a feerate change from {} to {}.",
-                       log_bytes!(chan_id[..]), chan.get_feerate_sat_per_1000_weight(), new_feerate);
+                       log_bytes!(chan_id[..]), chan.context.get_feerate_sat_per_1000_weight(), new_feerate);
 
-               chan.queue_update_fee(new_feerate, &self.logger);
+               chan.queue_update_fee(new_feerate, &self.fee_estimator, &self.logger);
                NotifyOption::DoPersist
        }
 
@@ -3820,7 +4235,7 @@ where
        /// it wants to detect). Thus, we have a variant exposed here for its benefit.
        pub fn maybe_update_chan_fees(&self) {
                PersistenceNotifierGuard::optionally_notify(&self.total_consistency_lock, &self.persistence_notifier, || {
-                       let mut should_persist = NotifyOption::SkipPersist;
+                       let mut should_persist = self.process_background_events();
 
                        let new_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Normal);
 
@@ -3856,8 +4271,7 @@ where
        /// [`ChannelConfig`]: crate::util::config::ChannelConfig
        pub fn timer_tick_occurred(&self) {
                PersistenceNotifierGuard::optionally_notify(&self.total_consistency_lock, &self.persistence_notifier, || {
-                       let mut should_persist = NotifyOption::SkipPersist;
-                       if self.process_background_events() { should_persist = NotifyOption::DoPersist; }
+                       let mut should_persist = self.process_background_events();
 
                        let new_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Normal);
 
@@ -3882,13 +4296,13 @@ where
                                                }
 
                                                match chan.channel_update_status() {
-                                                       ChannelUpdateStatus::Enabled if !chan.is_live() => chan.set_channel_update_status(ChannelUpdateStatus::DisabledStaged(0)),
-                                                       ChannelUpdateStatus::Disabled if chan.is_live() => chan.set_channel_update_status(ChannelUpdateStatus::EnabledStaged(0)),
-                                                       ChannelUpdateStatus::DisabledStaged(_) if chan.is_live()
+                                                       ChannelUpdateStatus::Enabled if !chan.context.is_live() => chan.set_channel_update_status(ChannelUpdateStatus::DisabledStaged(0)),
+                                                       ChannelUpdateStatus::Disabled if chan.context.is_live() => chan.set_channel_update_status(ChannelUpdateStatus::EnabledStaged(0)),
+                                                       ChannelUpdateStatus::DisabledStaged(_) if chan.context.is_live()
                                                                => chan.set_channel_update_status(ChannelUpdateStatus::Enabled),
-                                                       ChannelUpdateStatus::EnabledStaged(_) if !chan.is_live()
+                                                       ChannelUpdateStatus::EnabledStaged(_) if !chan.context.is_live()
                                                                => chan.set_channel_update_status(ChannelUpdateStatus::Disabled),
-                                                       ChannelUpdateStatus::DisabledStaged(mut n) if !chan.is_live() => {
+                                                       ChannelUpdateStatus::DisabledStaged(mut n) if !chan.context.is_live() => {
                                                                n += 1;
                                                                if n >= DISABLE_GOSSIP_TICKS {
                                                                        chan.set_channel_update_status(ChannelUpdateStatus::Disabled);
@@ -3902,7 +4316,7 @@ where
                                                                        chan.set_channel_update_status(ChannelUpdateStatus::DisabledStaged(n));
                                                                }
                                                        },
-                                                       ChannelUpdateStatus::EnabledStaged(mut n) if chan.is_live() => {
+                                                       ChannelUpdateStatus::EnabledStaged(mut n) if chan.context.is_live() => {
                                                                n += 1;
                                                                if n >= ENABLE_GOSSIP_TICKS {
                                                                        chan.set_channel_update_status(ChannelUpdateStatus::Enabled);
@@ -3919,7 +4333,21 @@ where
                                                        _ => {},
                                                }
 
-                                               chan.maybe_expire_prev_config();
+                                               chan.context.maybe_expire_prev_config();
+
+                                               if chan.should_disconnect_peer_awaiting_response() {
+                                                       log_debug!(self.logger, "Disconnecting peer {} due to not making any progress on channel {}",
+                                                                       counterparty_node_id, log_bytes!(*chan_id));
+                                                       pending_msg_events.push(MessageSendEvent::HandleError {
+                                                               node_id: counterparty_node_id,
+                                                               action: msgs::ErrorAction::DisconnectPeerWithWarning {
+                                                                       msg: msgs::WarningMessage {
+                                                                               channel_id: *chan_id,
+                                                                               data: "Disconnecting due to timeout awaiting response".to_owned(),
+                                                                       },
+                                                               },
+                                                       });
+                                               }
 
                                                true
                                        });
@@ -4029,7 +4457,7 @@ where
        ///
        /// See [`FailureCode`] for valid failure codes.
        pub fn fail_htlc_backwards_with_reason(&self, payment_hash: &PaymentHash, failure_code: FailureCode) {
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
 
                let removed_source = self.claimable_payments.lock().unwrap().claimable_payments.remove(payment_hash);
                if let Some(payment) = removed_source {
@@ -4065,10 +4493,10 @@ where
                // guess somewhat. If its a public channel, we figure best to just use the real SCID (as
                // we're not leaking that we have a channel with the counterparty), otherwise we try to use
                // an inbound SCID alias before the real SCID.
-               let scid_pref = if chan.should_announce() {
-                       chan.get_short_channel_id().or(chan.latest_inbound_scid_alias())
+               let scid_pref = if chan.context.should_announce() {
+                       chan.context.get_short_channel_id().or(chan.context.latest_inbound_scid_alias())
                } else {
-                       chan.latest_inbound_scid_alias().or(chan.get_short_channel_id())
+                       chan.context.latest_inbound_scid_alias().or(chan.context.get_short_channel_id())
                };
                if let Some(scid) = scid_pref {
                        self.get_htlc_temp_fail_err_and_data(desired_err_code, scid, chan)
@@ -4206,7 +4634,7 @@ where
        pub fn claim_funds(&self, payment_preimage: PaymentPreimage) {
                let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0).into_inner());
 
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
 
                let mut sources = {
                        let mut claimable_payments = self.claimable_payments.lock().unwrap();
@@ -4257,22 +4685,10 @@ where
                        if expected_amt_msat.is_some() && expected_amt_msat != htlc.total_value_received {
                                log_error!(self.logger, "Somehow ended up with an MPP payment with different received total amounts - this should not be reachable!");
                                debug_assert!(false);
-                               valid_mpp = false;
-                               break;
-                       }
-                       expected_amt_msat = htlc.total_value_received;
-
-                       if let OnionPayload::Spontaneous(_) = &htlc.onion_payload {
-                               // We don't currently support MPP for spontaneous payments, so just check
-                               // that there's one payment here and move on.
-                               if sources.len() != 1 {
-                                       log_error!(self.logger, "Somehow ended up with an MPP spontaneous payment - this should not be reachable!");
-                                       debug_assert!(false);
-                                       valid_mpp = false;
-                                       break;
-                               }
+                               valid_mpp = false;
+                               break;
                        }
-
+                       expected_amt_msat = htlc.total_value_received;
                        claimable_amt_msat += htlc.value;
                }
                mem::drop(per_peer_state);
@@ -4342,7 +4758,7 @@ where
                                let mut peer_state_lock = peer_state_opt.unwrap();
                                let peer_state = &mut *peer_state_lock;
                                if let hash_map::Entry::Occupied(mut chan) = peer_state.channel_by_id.entry(chan_id) {
-                                       let counterparty_node_id = chan.get().get_counterparty_node_id();
+                                       let counterparty_node_id = chan.get().context.get_counterparty_node_id();
                                        let fulfill_res = chan.get_mut().get_update_fulfill_htlc_and_commit(prev_hop.htlc_id, payment_preimage, &self.logger);
 
                                        if let UpdateFulfillCommitFetch::NewClaim { htlc_value_msat, monitor_update } = fulfill_res {
@@ -4351,9 +4767,7 @@ where
                                                                log_bytes!(chan_id), action);
                                                        peer_state.monitor_update_blocked_actions.entry(chan_id).or_insert(Vec::new()).push(action);
                                                }
-                                               let update_id = monitor_update.update_id;
-                                               let update_res = self.chain_monitor.update_channel(prev_hop.outpoint, monitor_update);
-                                               let res = handle_new_monitor_update!(self, update_res, update_id, peer_state_lock,
+                                               let res = handle_new_monitor_update!(self, prev_hop.outpoint, monitor_update, peer_state_lock,
                                                        peer_state, per_peer_state, chan);
                                                if let Err(e) = res {
                                                        // TODO: This is a *critical* error - we probably updated the outbound edge
@@ -4411,16 +4825,16 @@ where
                                                                Some(claimed_htlc_value - forwarded_htlc_value)
                                                        } else { None };
 
-                                                       let prev_channel_id = Some(prev_outpoint.to_channel_id());
-                                                       let next_channel_id = Some(next_channel_id);
-
-                                                       Some(MonitorUpdateCompletionAction::EmitEvent { event: events::Event::PaymentForwarded {
-                                                               fee_earned_msat,
-                                                               claim_from_onchain_tx: from_onchain,
-                                                               prev_channel_id,
-                                                               next_channel_id,
-                                                               outbound_amount_forwarded_msat: forwarded_htlc_value_msat,
-                                                       }})
+                                                       Some(MonitorUpdateCompletionAction::EmitEventAndFreeOtherChannel {
+                                                               event: events::Event::PaymentForwarded {
+                                                                       fee_earned_msat,
+                                                                       claim_from_onchain_tx: from_onchain,
+                                                                       prev_channel_id: Some(prev_outpoint.to_channel_id()),
+                                                                       next_channel_id: Some(next_channel_id),
+                                                                       outbound_amount_forwarded_msat: forwarded_htlc_value_msat,
+                                                               },
+                                                               downstream_counterparty_and_funding_outpoint: None,
+                                                       })
                                                } else { None }
                                        });
                                if let Err((pk, err)) = res {
@@ -4447,8 +4861,13 @@ where
                                                }, None));
                                        }
                                },
-                               MonitorUpdateCompletionAction::EmitEvent { event } => {
+                               MonitorUpdateCompletionAction::EmitEventAndFreeOtherChannel {
+                                       event, downstream_counterparty_and_funding_outpoint
+                               } => {
                                        self.pending_events.lock().unwrap().push_back((event, None));
+                                       if let Some((node_id, funding_outpoint, blocker)) = downstream_counterparty_and_funding_outpoint {
+                                               self.handle_monitor_update_release(node_id, funding_outpoint, Some(blocker));
+                                       }
                                },
                        }
                }
@@ -4463,7 +4882,7 @@ where
                channel_ready: Option<msgs::ChannelReady>, announcement_sigs: Option<msgs::AnnouncementSignatures>)
        -> Option<(u64, OutPoint, u128, Vec<(PendingHTLCInfo, u64)>)> {
                log_trace!(self.logger, "Handling channel resumption for channel {} with {} RAA, {} commitment update, {} pending forwards, {}broadcasting funding, {} channel ready, {} announcement",
-                       log_bytes!(channel.channel_id()),
+                       log_bytes!(channel.context.channel_id()),
                        if raa.is_some() { "an" } else { "no" },
                        if commitment_update.is_some() { "a" } else { "no" }, pending_forwards.len(),
                        if funding_broadcastable.is_some() { "" } else { "not " },
@@ -4472,10 +4891,10 @@ where
 
                let mut htlc_forwards = None;
 
-               let counterparty_node_id = channel.get_counterparty_node_id();
+               let counterparty_node_id = channel.context.get_counterparty_node_id();
                if !pending_forwards.is_empty() {
-                       htlc_forwards = Some((channel.get_short_channel_id().unwrap_or(channel.outbound_scid_alias()),
-                               channel.get_funding_txo().unwrap(), channel.get_user_id(), pending_forwards));
+                       htlc_forwards = Some((channel.context.get_short_channel_id().unwrap_or(channel.context.outbound_scid_alias()),
+                               channel.context.get_funding_txo().unwrap(), channel.context.get_user_id(), pending_forwards));
                }
 
                if let Some(msg) = channel_ready {
@@ -4517,7 +4936,7 @@ where
 
                if let Some(tx) = funding_broadcastable {
                        log_info!(self.logger, "Broadcasting funding transaction with txid {}", tx.txid());
-                       self.tx_broadcaster.broadcast_transaction(&tx);
+                       self.tx_broadcaster.broadcast_transactions(&[&tx]);
                }
 
                {
@@ -4556,12 +4975,18 @@ where
                                hash_map::Entry::Vacant(_) => return,
                        }
                };
-               log_trace!(self.logger, "ChannelMonitor updated to {}. Current highest is {}",
-                       highest_applied_update_id, channel.get().get_latest_monitor_update_id());
-               if !channel.get().is_awaiting_monitor_update() || channel.get().get_latest_monitor_update_id() != highest_applied_update_id {
+               let remaining_in_flight =
+                       if let Some(pending) = peer_state.in_flight_monitor_updates.get_mut(funding_txo) {
+                               pending.retain(|upd| upd.update_id > highest_applied_update_id);
+                               pending.len()
+                       } else { 0 };
+               log_trace!(self.logger, "ChannelMonitor updated to {}. Current highest is {}. {} pending in-flight updates.",
+                       highest_applied_update_id, channel.get().context.get_latest_monitor_update_id(),
+                       remaining_in_flight);
+               if !channel.get().is_awaiting_monitor_update() || channel.get().context.get_latest_monitor_update_id() != highest_applied_update_id {
                        return;
                }
-               handle_monitor_update_completion!(self, highest_applied_update_id, peer_state_lock, peer_state, per_peer_state, channel.get_mut());
+               handle_monitor_update_completion!(self, peer_state_lock, peer_state, per_peer_state, channel.get_mut());
        }
 
        /// Accepts a request to open a channel after a [`Event::OpenChannelRequest`].
@@ -4607,25 +5032,26 @@ where
        }
 
        fn do_accept_inbound_channel(&self, temporary_channel_id: &[u8; 32], counterparty_node_id: &PublicKey, accept_0conf: bool, user_channel_id: u128) -> Result<(), APIError> {
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
 
-               let peers_without_funded_channels = self.peers_without_funded_channels(|peer| !peer.channel_by_id.is_empty());
+               let peers_without_funded_channels =
+                       self.peers_without_funded_channels(|peer| { peer.total_channel_count() > 0 });
                let per_peer_state = self.per_peer_state.read().unwrap();
                let peer_state_mutex = per_peer_state.get(counterparty_node_id)
                        .ok_or_else(|| APIError::ChannelUnavailable { err: format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id) })?;
                let mut peer_state_lock = peer_state_mutex.lock().unwrap();
                let peer_state = &mut *peer_state_lock;
-               let is_only_peer_channel = peer_state.channel_by_id.len() == 1;
-               match peer_state.channel_by_id.entry(temporary_channel_id.clone()) {
+               let is_only_peer_channel = peer_state.total_channel_count() == 1;
+               match peer_state.inbound_v1_channel_by_id.entry(temporary_channel_id.clone()) {
                        hash_map::Entry::Occupied(mut channel) => {
-                               if !channel.get().inbound_is_awaiting_accept() {
+                               if !channel.get().is_awaiting_accept() {
                                        return Err(APIError::APIMisuseError { err: "The channel isn't currently awaiting to be accepted.".to_owned() });
                                }
                                if accept_0conf {
                                        channel.get_mut().set_0conf();
-                               } else if channel.get().get_channel_type().requires_zero_conf() {
+                               } else if channel.get().context.get_channel_type().requires_zero_conf() {
                                        let send_msg_err_event = events::MessageSendEvent::HandleError {
-                                               node_id: channel.get().get_counterparty_node_id(),
+                                               node_id: channel.get().context.get_counterparty_node_id(),
                                                action: msgs::ErrorAction::SendErrorMessage{
                                                        msg: msgs::ErrorMessage { channel_id: temporary_channel_id.clone(), data: "No zero confirmation channels accepted".to_owned(), }
                                                }
@@ -4639,7 +5065,7 @@ where
                                        // channels per-peer we can accept channels from a peer with existing ones.
                                        if is_only_peer_channel && peers_without_funded_channels >= MAX_UNFUNDED_CHANNEL_PEERS {
                                                let send_msg_err_event = events::MessageSendEvent::HandleError {
-                                                       node_id: channel.get().get_counterparty_node_id(),
+                                                       node_id: channel.get().context.get_counterparty_node_id(),
                                                        action: msgs::ErrorAction::SendErrorMessage{
                                                                msg: msgs::ErrorMessage { channel_id: temporary_channel_id.clone(), data: "Have too many peers with unfunded channels, not accepting new ones".to_owned(), }
                                                        }
@@ -4651,7 +5077,7 @@ where
                                }
 
                                peer_state.pending_msg_events.push(events::MessageSendEvent::SendAcceptChannel {
-                                       node_id: channel.get().get_counterparty_node_id(),
+                                       node_id: channel.get().context.get_counterparty_node_id(),
                                        msg: channel.get_mut().accept_inbound_channel(user_channel_id),
                                });
                        }
@@ -4677,7 +5103,7 @@ where
                                let peer = peer_mtx.lock().unwrap();
                                if !maybe_count_peer(&*peer) { continue; }
                                let num_unfunded_channels = Self::unfunded_channel_count(&peer, best_block_height);
-                               if num_unfunded_channels == peer.channel_by_id.len() {
+                               if num_unfunded_channels == peer.total_channel_count() {
                                        peers_without_funded_channels += 1;
                                }
                        }
@@ -4690,12 +5116,19 @@ where
        ) -> usize {
                let mut num_unfunded_channels = 0;
                for (_, chan) in peer.channel_by_id.iter() {
-                       if !chan.is_outbound() && chan.minimum_depth().unwrap_or(1) != 0 &&
-                               chan.get_funding_tx_confirmations(best_block_height) == 0
+                       // This covers non-zero-conf inbound `Channel`s that we are currently monitoring, but those
+                       // which have not yet had any confirmations on-chain.
+                       if !chan.context.is_outbound() && chan.context.minimum_depth().unwrap_or(1) != 0 &&
+                               chan.context.get_funding_tx_confirmations(best_block_height) == 0
                        {
                                num_unfunded_channels += 1;
                        }
                }
+               for (_, chan) in peer.inbound_v1_channel_by_id.iter() {
+                       if chan.context.minimum_depth().unwrap_or(1) != 0 {
+                               num_unfunded_channels += 1;
+                       }
+               }
                num_unfunded_channels
        }
 
@@ -4716,7 +5149,8 @@ where
                // Get the number of peers with channels, but without funded ones. We don't care too much
                // about peers that never open a channel, so we filter by peers that have at least one
                // channel, and then limit the number of those with unfunded channels.
-               let channeled_peers_without_funding = self.peers_without_funded_channels(|node| !node.channel_by_id.is_empty());
+               let channeled_peers_without_funding =
+                       self.peers_without_funded_channels(|node| node.total_channel_count() > 0);
 
                let per_peer_state = self.per_peer_state.read().unwrap();
                let peer_state_mutex = per_peer_state.get(counterparty_node_id)
@@ -4730,7 +5164,7 @@ where
                // If this peer already has some channels, a new channel won't increase our number of peers
                // with unfunded channels, so as long as we aren't over the maximum number of unfunded
                // channels per-peer we can accept channels from a peer with existing ones.
-               if peer_state.channel_by_id.is_empty() &&
+               if peer_state.total_channel_count() == 0 &&
                        channeled_peers_without_funding >= MAX_UNFUNDED_CHANNEL_PEERS &&
                        !self.default_configuration.manually_accept_inbound_channels
                {
@@ -4746,7 +5180,7 @@ where
                                msg.temporary_channel_id.clone()));
                }
 
-               let mut channel = match Channel::new_from_req(&self.fee_estimator, &self.entropy_source, &self.signer_provider,
+               let mut channel = match InboundV1Channel::new(&self.fee_estimator, &self.entropy_source, &self.signer_provider,
                        counterparty_node_id.clone(), &self.channel_type_features(), &peer_state.latest_features, msg, user_channel_id,
                        &self.default_configuration, best_block_height, &self.logger, outbound_scid_alias)
                {
@@ -4756,33 +5190,35 @@ where
                        },
                        Ok(res) => res
                };
-               match peer_state.channel_by_id.entry(channel.channel_id()) {
-                       hash_map::Entry::Occupied(_) => {
-                               self.outbound_scid_aliases.lock().unwrap().remove(&outbound_scid_alias);
-                               return Err(MsgHandleErrInternal::send_err_msg_no_close("temporary_channel_id collision for the same peer!".to_owned(), msg.temporary_channel_id.clone()))
-                       },
-                       hash_map::Entry::Vacant(entry) => {
-                               if !self.default_configuration.manually_accept_inbound_channels {
-                                       if channel.get_channel_type().requires_zero_conf() {
-                                               return Err(MsgHandleErrInternal::send_err_msg_no_close("No zero confirmation channels accepted".to_owned(), msg.temporary_channel_id.clone()));
-                                       }
-                                       peer_state.pending_msg_events.push(events::MessageSendEvent::SendAcceptChannel {
-                                               node_id: counterparty_node_id.clone(),
-                                               msg: channel.accept_inbound_channel(user_channel_id),
-                                       });
-                               } else {
-                                       let mut pending_events = self.pending_events.lock().unwrap();
-                                       pending_events.push_back((events::Event::OpenChannelRequest {
-                                               temporary_channel_id: msg.temporary_channel_id.clone(),
-                                               counterparty_node_id: counterparty_node_id.clone(),
-                                               funding_satoshis: msg.funding_satoshis,
-                                               push_msat: msg.push_msat,
-                                               channel_type: channel.get_channel_type().clone(),
-                                       }, None));
+               let channel_id = channel.context.channel_id();
+               let channel_exists = peer_state.has_channel(&channel_id);
+               if channel_exists {
+                       self.outbound_scid_aliases.lock().unwrap().remove(&outbound_scid_alias);
+                       return Err(MsgHandleErrInternal::send_err_msg_no_close("temporary_channel_id collision for the same peer!".to_owned(), msg.temporary_channel_id.clone()))
+               } else {
+                       if !self.default_configuration.manually_accept_inbound_channels {
+                               let channel_type = channel.context.get_channel_type();
+                               if channel_type.requires_zero_conf() {
+                                       return Err(MsgHandleErrInternal::send_err_msg_no_close("No zero confirmation channels accepted".to_owned(), msg.temporary_channel_id.clone()));
                                }
-
-                               entry.insert(channel);
+                               if channel_type.requires_anchors_zero_fee_htlc_tx() {
+                                       return Err(MsgHandleErrInternal::send_err_msg_no_close("No channels with anchor outputs accepted".to_owned(), msg.temporary_channel_id.clone()));
+                               }
+                               peer_state.pending_msg_events.push(events::MessageSendEvent::SendAcceptChannel {
+                                       node_id: counterparty_node_id.clone(),
+                                       msg: channel.accept_inbound_channel(user_channel_id),
+                               });
+                       } else {
+                               let mut pending_events = self.pending_events.lock().unwrap();
+                               pending_events.push_back((events::Event::OpenChannelRequest {
+                                       temporary_channel_id: msg.temporary_channel_id.clone(),
+                                       counterparty_node_id: counterparty_node_id.clone(),
+                                       funding_satoshis: msg.funding_satoshis,
+                                       push_msat: msg.push_msat,
+                                       channel_type: channel.context.get_channel_type().clone(),
+                               }, None));
                        }
+                       peer_state.inbound_v1_channel_by_id.insert(channel_id, channel);
                }
                Ok(())
        }
@@ -4797,10 +5233,10 @@ where
                                })?;
                        let mut peer_state_lock = peer_state_mutex.lock().unwrap();
                        let peer_state = &mut *peer_state_lock;
-                       match peer_state.channel_by_id.entry(msg.temporary_channel_id) {
+                       match peer_state.outbound_v1_channel_by_id.entry(msg.temporary_channel_id) {
                                hash_map::Entry::Occupied(mut chan) => {
-                                       try_chan_entry!(self, chan.get_mut().accept_channel(&msg, &self.default_configuration.channel_handshake_limits, &peer_state.latest_features), chan);
-                                       (chan.get().get_value_satoshis(), chan.get().get_funding_redeemscript().to_v0_p2wsh(), chan.get().get_user_id())
+                                       try_v1_outbound_chan_entry!(self, chan.get_mut().accept_channel(&msg, &self.default_configuration.channel_handshake_limits, &peer_state.latest_features), chan);
+                                       (chan.get().context.get_value_satoshis(), chan.get().context.get_funding_redeemscript().to_v0_p2wsh(), chan.get().context.get_user_id())
                                },
                                hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id), msg.temporary_channel_id))
                        }
@@ -4828,12 +5264,24 @@ where
 
                let mut peer_state_lock = peer_state_mutex.lock().unwrap();
                let peer_state = &mut *peer_state_lock;
-               let ((funding_msg, monitor), chan) =
-                       match peer_state.channel_by_id.entry(msg.temporary_channel_id) {
-                               hash_map::Entry::Occupied(mut chan) => {
-                                       (try_chan_entry!(self, chan.get_mut().funding_created(msg, best_block, &self.signer_provider, &self.logger), chan), chan.remove())
+               let (chan, funding_msg, monitor) =
+                       match peer_state.inbound_v1_channel_by_id.remove(&msg.temporary_channel_id) {
+                               Some(inbound_chan) => {
+                                       match inbound_chan.funding_created(msg, best_block, &self.signer_provider, &self.logger) {
+                                               Ok(res) => res,
+                                               Err((mut inbound_chan, err)) => {
+                                                       // We've already removed this inbound channel from the map in `PeerState`
+                                                       // above so at this point we just need to clean up any lingering entries
+                                                       // concerning this channel as it is safe to do so.
+                                                       update_maps_on_chan_removal!(self, &inbound_chan.context);
+                                                       let user_id = inbound_chan.context.get_user_id();
+                                                       let shutdown_res = inbound_chan.context.force_shutdown(false);
+                                                       return Err(MsgHandleErrInternal::from_finish_shutdown(format!("{}", err),
+                                                               msg.temporary_channel_id, user_id, shutdown_res, None));
+                                               },
+                                       }
                                },
-                               hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id), msg.temporary_channel_id))
+                               None => return Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id), msg.temporary_channel_id))
                        };
 
                match peer_state.channel_by_id.entry(funding_msg.channel_id) {
@@ -4841,14 +5289,14 @@ where
                                Err(MsgHandleErrInternal::send_err_msg_no_close("Already had channel with the new channel_id".to_owned(), funding_msg.channel_id))
                        },
                        hash_map::Entry::Vacant(e) => {
-                               match self.id_to_peer.lock().unwrap().entry(chan.channel_id()) {
+                               match self.id_to_peer.lock().unwrap().entry(chan.context.channel_id()) {
                                        hash_map::Entry::Occupied(_) => {
                                                return Err(MsgHandleErrInternal::send_err_msg_no_close(
                                                        "The funding_created message had the same funding_txid as an existing channel - funding is not possible".to_owned(),
                                                        funding_msg.channel_id))
                                        },
                                        hash_map::Entry::Vacant(i_e) => {
-                                               i_e.insert(chan.get_counterparty_node_id());
+                                               i_e.insert(chan.context.get_counterparty_node_id());
                                        }
                                }
 
@@ -4865,8 +5313,9 @@ where
                                let monitor_res = self.chain_monitor.watch_channel(monitor.get_funding_txo().0, monitor);
 
                                let chan = e.insert(chan);
-                               let mut res = handle_new_monitor_update!(self, monitor_res, 0, peer_state_lock, peer_state,
-                                       per_peer_state, chan, MANUALLY_REMOVING, { peer_state.channel_by_id.remove(&new_channel_id) });
+                               let mut res = handle_new_monitor_update!(self, monitor_res, peer_state_lock, peer_state,
+                                       per_peer_state, chan, MANUALLY_REMOVING_INITIAL_MONITOR,
+                                       { peer_state.channel_by_id.remove(&new_channel_id) });
 
                                // 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
@@ -4878,7 +5327,7 @@ where
                                if let Err(MsgHandleErrInternal { shutdown_finish: Some((res, _)), .. }) = &mut res {
                                        res.0 = None;
                                }
-                               res
+                               res.map(|_| ())
                        }
                }
        }
@@ -4898,8 +5347,8 @@ where
                        hash_map::Entry::Occupied(mut chan) => {
                                let monitor = try_chan_entry!(self,
                                        chan.get_mut().funding_signed(&msg, best_block, &self.signer_provider, &self.logger), chan);
-                               let update_res = self.chain_monitor.watch_channel(chan.get().get_funding_txo().unwrap(), monitor);
-                               let mut res = handle_new_monitor_update!(self, update_res, 0, peer_state_lock, peer_state, per_peer_state, chan);
+                               let update_res = self.chain_monitor.watch_channel(chan.get().context.get_funding_txo().unwrap(), monitor);
+                               let mut res = handle_new_monitor_update!(self, update_res, peer_state_lock, peer_state, per_peer_state, chan, INITIAL_MONITOR);
                                if let Err(MsgHandleErrInternal { ref mut shutdown_finish, .. }) = res {
                                        // We weren't able to watch the channel to begin with, so no updates should be made on
                                        // it. Previously, full_stack_target found an (unreachable) panic when the
@@ -4908,7 +5357,7 @@ where
                                                shutdown_finish.0.take();
                                        }
                                }
-                               res
+                               res.map(|_| ())
                        },
                        hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id))
                }
@@ -4928,18 +5377,18 @@ where
                                let announcement_sigs_opt = try_chan_entry!(self, chan.get_mut().channel_ready(&msg, &self.node_signer,
                                        self.genesis_hash.clone(), &self.default_configuration, &self.best_block.read().unwrap(), &self.logger), chan);
                                if let Some(announcement_sigs) = announcement_sigs_opt {
-                                       log_trace!(self.logger, "Sending announcement_signatures for channel {}", log_bytes!(chan.get().channel_id()));
+                                       log_trace!(self.logger, "Sending announcement_signatures for channel {}", log_bytes!(chan.get().context.channel_id()));
                                        peer_state.pending_msg_events.push(events::MessageSendEvent::SendAnnouncementSignatures {
                                                node_id: counterparty_node_id.clone(),
                                                msg: announcement_sigs,
                                        });
-                               } else if chan.get().is_usable() {
+                               } else if chan.get().context.is_usable() {
                                        // If we're sending an announcement_signatures, we'll send the (public)
                                        // channel_update after sending a channel_announcement when we receive our
                                        // counterparty's announcement_signatures. Thus, we only bother to send a
                                        // channel_update here if the channel is not public, i.e. we're not sending an
                                        // announcement_signatures.
-                                       log_trace!(self.logger, "Sending private initial channel_update for our counterparty on channel {}", log_bytes!(chan.get().channel_id()));
+                                       log_trace!(self.logger, "Sending private initial channel_update for our counterparty on channel {}", log_bytes!(chan.get().context.channel_id()));
                                        if let Ok(msg) = self.get_channel_update_for_unicast(chan.get()) {
                                                peer_state.pending_msg_events.push(events::MessageSendEvent::SendChannelUpdate {
                                                        node_id: counterparty_node_id.clone(),
@@ -4979,7 +5428,7 @@ where
                                                        if chan_entry.get().sent_shutdown() { " after we initiated shutdown" } else { "" });
                                        }
 
-                                       let funding_txo_opt = chan_entry.get().get_funding_txo();
+                                       let funding_txo_opt = chan_entry.get().context.get_funding_txo();
                                        let (shutdown, monitor_update_opt, htlcs) = try_chan_entry!(self,
                                                chan_entry.get_mut().shutdown(&self.signer_provider, &peer_state.latest_features, &msg), chan_entry);
                                        dropped_htlcs = htlcs;
@@ -4996,9 +5445,8 @@ where
 
                                        // Update the monitor with the shutdown script if necessary.
                                        if let Some(monitor_update) = monitor_update_opt {
-                                               let update_id = monitor_update.update_id;
-                                               let update_res = self.chain_monitor.update_channel(funding_txo_opt.unwrap(), monitor_update);
-                                               break handle_new_monitor_update!(self, update_res, update_id, peer_state_lock, peer_state, per_peer_state, chan_entry);
+                                               break handle_new_monitor_update!(self, funding_txo_opt.unwrap(), monitor_update,
+                                                       peer_state_lock, peer_state, per_peer_state, chan_entry).map(|_| ());
                                        }
                                        break Ok(());
                                },
@@ -5047,7 +5495,7 @@ where
                };
                if let Some(broadcast_tx) = tx {
                        log_info!(self.logger, "Broadcasting {}", log_tx!(broadcast_tx));
-                       self.tx_broadcaster.broadcast_transaction(&broadcast_tx);
+                       self.tx_broadcaster.broadcast_transactions(&[&broadcast_tx]);
                }
                if let Some(chan) = chan_option {
                        if let Ok(update) = self.get_channel_update_for_broadcast(&chan) {
@@ -5057,7 +5505,7 @@ where
                                        msg: update
                                });
                        }
-                       self.issue_channel_close_events(&chan, ClosureReason::CooperativeClosure);
+                       self.issue_channel_close_events(&chan.context, ClosureReason::CooperativeClosure);
                }
                Ok(())
        }
@@ -5072,7 +5520,7 @@ where
                //encrypted with the same key. It's not immediately obvious how to usefully exploit that,
                //but we should prevent it anyway.
 
-               let pending_forward_info = self.decode_update_add_htlc_onion(msg);
+               let decoded_hop_res = self.decode_update_add_htlc_onion(msg);
                let per_peer_state = self.per_peer_state.read().unwrap();
                let peer_state_mutex = per_peer_state.get(counterparty_node_id)
                        .ok_or_else(|| {
@@ -5084,6 +5532,12 @@ where
                match peer_state.channel_by_id.entry(msg.channel_id) {
                        hash_map::Entry::Occupied(mut chan) => {
 
+                               let pending_forward_info = match decoded_hop_res {
+                                       Ok((next_hop, shared_secret, next_packet_pk_opt)) =>
+                                               self.construct_pending_htlc_status(msg, shared_secret, next_hop,
+                                                       chan.get().context.config().accept_underpaying_htlcs, next_packet_pk_opt),
+                                       Err(e) => PendingHTLCStatus::Fail(e)
+                               };
                                let create_pending_htlc_status = |chan: &Channel<<SP::Target as SignerProvider>::Signer>, pending_forward_info: PendingHTLCStatus, error_code: u16| {
                                        // 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
@@ -5106,7 +5560,7 @@ where
                                                _ => pending_forward_info
                                        }
                                };
-                               try_chan_entry!(self, chan.get_mut().update_add_htlc(&msg, pending_forward_info, create_pending_htlc_status, &self.logger), chan);
+                               try_chan_entry!(self, chan.get_mut().update_add_htlc(&msg, pending_forward_info, create_pending_htlc_status, &self.fee_estimator, &self.logger), chan);
                        },
                        hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id), msg.channel_id))
                }
@@ -5185,13 +5639,11 @@ where
                let peer_state = &mut *peer_state_lock;
                match peer_state.channel_by_id.entry(msg.channel_id) {
                        hash_map::Entry::Occupied(mut chan) => {
-                               let funding_txo = chan.get().get_funding_txo();
+                               let funding_txo = chan.get().context.get_funding_txo();
                                let monitor_update_opt = try_chan_entry!(self, chan.get_mut().commitment_signed(&msg, &self.logger), chan);
                                if let Some(monitor_update) = monitor_update_opt {
-                                       let update_res = self.chain_monitor.update_channel(funding_txo.unwrap(), monitor_update);
-                                       let update_id = monitor_update.update_id;
-                                       handle_new_monitor_update!(self, update_res, update_id, peer_state_lock,
-                                               peer_state, per_peer_state, chan)
+                                       handle_new_monitor_update!(self, funding_txo.unwrap(), monitor_update, peer_state_lock,
+                                               peer_state, per_peer_state, chan).map(|_| ())
                                } else { Ok(()) }
                        },
                        hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id), msg.channel_id))
@@ -5295,6 +5747,24 @@ where
                }
        }
 
+       /// Checks whether [`ChannelMonitorUpdate`]s generated by the receipt of a remote
+       /// [`msgs::RevokeAndACK`] should be held for the given channel until some other event
+       /// completes. Note that this needs to happen in the same [`PeerState`] mutex as any release of
+       /// the [`ChannelMonitorUpdate`] in question.
+       fn raa_monitor_updates_held(&self,
+               actions_blocking_raa_monitor_updates: &BTreeMap<[u8; 32], Vec<RAAMonitorUpdateBlockingAction>>,
+               channel_funding_outpoint: OutPoint, counterparty_node_id: PublicKey
+       ) -> bool {
+               actions_blocking_raa_monitor_updates
+                       .get(&channel_funding_outpoint.to_channel_id()).map(|v| !v.is_empty()).unwrap_or(false)
+               || self.pending_events.lock().unwrap().iter().any(|(_, action)| {
+                       action == &Some(EventCompletionAction::ReleaseRAAChannelMonitorUpdate {
+                               channel_funding_outpoint,
+                               counterparty_node_id,
+                       })
+               })
+       }
+
        fn internal_revoke_and_ack(&self, counterparty_node_id: &PublicKey, msg: &msgs::RevokeAndACK) -> Result<(), MsgHandleErrInternal> {
                let (htlcs_to_fail, res) = {
                        let per_peer_state = self.per_peer_state.read().unwrap();
@@ -5306,13 +5776,11 @@ where
                        let peer_state = &mut *peer_state_lock;
                        match peer_state.channel_by_id.entry(msg.channel_id) {
                                hash_map::Entry::Occupied(mut chan) => {
-                                       let funding_txo = chan.get().get_funding_txo();
-                                       let (htlcs_to_fail, monitor_update_opt) = try_chan_entry!(self, chan.get_mut().revoke_and_ack(&msg, &self.logger), chan);
+                                       let funding_txo = chan.get().context.get_funding_txo();
+                                       let (htlcs_to_fail, monitor_update_opt) = try_chan_entry!(self, chan.get_mut().revoke_and_ack(&msg, &self.fee_estimator, &self.logger), chan);
                                        let res = if let Some(monitor_update) = monitor_update_opt {
-                                               let update_res = self.chain_monitor.update_channel(funding_txo.unwrap(), monitor_update);
-                                               let update_id = monitor_update.update_id;
-                                               handle_new_monitor_update!(self, update_res, update_id,
-                                                       peer_state_lock, peer_state, per_peer_state, chan)
+                                               handle_new_monitor_update!(self, funding_txo.unwrap(), monitor_update,
+                                                       peer_state_lock, peer_state, per_peer_state, chan).map(|_| ())
                                        } else { Ok(()) };
                                        (htlcs_to_fail, res)
                                },
@@ -5352,7 +5820,7 @@ where
                let peer_state = &mut *peer_state_lock;
                match peer_state.channel_by_id.entry(msg.channel_id) {
                        hash_map::Entry::Occupied(mut chan) => {
-                               if !chan.get().is_usable() {
+                               if !chan.get().context.is_usable() {
                                        return Err(MsgHandleErrInternal::from_no_close(LightningError{err: "Got an announcement_signatures before we were ready for it".to_owned(), action: msgs::ErrorAction::IgnoreError}));
                                }
 
@@ -5389,8 +5857,8 @@ where
                let peer_state = &mut *peer_state_lock;
                match peer_state.channel_by_id.entry(chan_id) {
                        hash_map::Entry::Occupied(mut chan) => {
-                               if chan.get().get_counterparty_node_id() != *counterparty_node_id {
-                                       if chan.get().should_announce() {
+                               if chan.get().context.get_counterparty_node_id() != *counterparty_node_id {
+                                       if chan.get().context.should_announce() {
                                                // If the announcement is about a channel of ours which is public, some
                                                // other peer may simply be forwarding all its gossip to us. Don't provide
                                                // a scary-looking error message and return Ok instead.
@@ -5398,7 +5866,7 @@ where
                                        }
                                        return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a channel_update for a channel from the wrong node - it shouldn't know about our private channels!".to_owned(), chan_id));
                                }
-                               let were_node_one = self.get_our_node_id().serialize()[..] < chan.get().get_counterparty_node_id().serialize()[..];
+                               let were_node_one = self.get_our_node_id().serialize()[..] < chan.get().context.get_counterparty_node_id().serialize()[..];
                                let msg_from_node_one = msg.contents.flags & 1 == 0;
                                if were_node_one == msg_from_node_one {
                                        return Ok(NotifyOption::SkipPersist);
@@ -5439,18 +5907,18 @@ where
                                                        node_id: counterparty_node_id.clone(),
                                                        msg,
                                                });
-                                       } else if chan.get().is_usable() {
+                                       } else if chan.get().context.is_usable() {
                                                // If the channel is in a usable state (ie the channel is not being shut
                                                // down), send a unicast channel_update to our counterparty to make sure
                                                // they have the latest channel parameters.
                                                if let Ok(msg) = self.get_channel_update_for_unicast(chan.get()) {
                                                        channel_update = Some(events::MessageSendEvent::SendChannelUpdate {
-                                                               node_id: chan.get().get_counterparty_node_id(),
+                                                               node_id: chan.get().context.get_counterparty_node_id(),
                                                                msg,
                                                        });
                                                }
                                        }
-                                       let need_lnd_workaround = chan.get_mut().workaround_lnd_bug_4006.take();
+                                       let need_lnd_workaround = chan.get_mut().context.workaround_lnd_bug_4006.take();
                                        htlc_forwards = self.handle_channel_resumption(
                                                &mut peer_state.pending_msg_events, chan.get_mut(), responses.raa, responses.commitment_update, responses.order,
                                                Vec::new(), None, responses.channel_ready, responses.announcement_sigs);
@@ -5513,7 +5981,7 @@ where
                                                                let pending_msg_events = &mut peer_state.pending_msg_events;
                                                                if let hash_map::Entry::Occupied(chan_entry) = peer_state.channel_by_id.entry(funding_outpoint.to_channel_id()) {
                                                                        let mut chan = remove_channel!(self, chan_entry);
-                                                                       failed_channels.push(chan.force_shutdown(false));
+                                                                       failed_channels.push(chan.context.force_shutdown(false));
                                                                        if let Ok(update) = self.get_channel_update_for_broadcast(&chan) {
                                                                                pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
                                                                                        msg: update
@@ -5524,11 +5992,11 @@ where
                                                                        } else {
                                                                                ClosureReason::CommitmentTxConfirmed
                                                                        };
-                                                                       self.issue_channel_close_events(&chan, reason);
+                                                                       self.issue_channel_close_events(&chan.context, reason);
                                                                        pending_msg_events.push(events::MessageSendEvent::HandleError {
-                                                                               node_id: chan.get_counterparty_node_id(),
+                                                                               node_id: chan.context.get_counterparty_node_id(),
                                                                                action: msgs::ErrorAction::SendErrorMessage {
-                                                                                       msg: msgs::ErrorMessage { channel_id: chan.channel_id(), data: "Channel force-closed".to_owned() }
+                                                                                       msg: msgs::ErrorMessage { channel_id: chan.context.channel_id(), data: "Channel force-closed".to_owned() }
                                                                                },
                                                                        });
                                                                }
@@ -5554,13 +6022,8 @@ where
        /// update events as a separate process method here.
        #[cfg(fuzzing)]
        pub fn process_monitor_events(&self) {
-               PersistenceNotifierGuard::optionally_notify(&self.total_consistency_lock, &self.persistence_notifier, || {
-                       if self.process_pending_monitor_events() {
-                               NotifyOption::DoPersist
-                       } else {
-                               NotifyOption::SkipPersist
-                       }
-               });
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
+               self.process_pending_monitor_events();
        }
 
        /// Check the holding cell in each channel and free any pending HTLCs in them if possible.
@@ -5582,21 +6045,18 @@ where
                                        let mut peer_state_lock = peer_state_mutex.lock().unwrap();
                                        let peer_state: &mut PeerState<_> = &mut *peer_state_lock;
                                        for (channel_id, chan) in peer_state.channel_by_id.iter_mut() {
-                                               let counterparty_node_id = chan.get_counterparty_node_id();
-                                               let funding_txo = chan.get_funding_txo();
+                                               let counterparty_node_id = chan.context.get_counterparty_node_id();
+                                               let funding_txo = chan.context.get_funding_txo();
                                                let (monitor_opt, holding_cell_failed_htlcs) =
-                                                       chan.maybe_free_holding_cell_htlcs(&self.logger);
+                                                       chan.maybe_free_holding_cell_htlcs(&self.fee_estimator, &self.logger);
                                                if !holding_cell_failed_htlcs.is_empty() {
                                                        failed_htlcs.push((holding_cell_failed_htlcs, *channel_id, counterparty_node_id));
                                                }
                                                if let Some(monitor_update) = monitor_opt {
                                                        has_monitor_update = true;
 
-                                                       let update_res = self.chain_monitor.update_channel(
-                                                               funding_txo.expect("channel is live"), monitor_update);
-                                                       let update_id = monitor_update.update_id;
                                                        let channel_id: [u8; 32] = *channel_id;
-                                                       let res = handle_new_monitor_update!(self, update_res, update_id,
+                                                       let res = handle_new_monitor_update!(self, funding_txo.unwrap(), monitor_update,
                                                                peer_state_lock, peer_state, per_peer_state, chan, MANUALLY_REMOVING,
                                                                peer_state.channel_by_id.remove(&channel_id));
                                                        if res.is_err() {
@@ -5642,7 +6102,7 @@ where
                                                        if let Some(msg) = msg_opt {
                                                                has_update = true;
                                                                pending_msg_events.push(events::MessageSendEvent::SendClosingSigned {
-                                                                       node_id: chan.get_counterparty_node_id(), msg,
+                                                                       node_id: chan.context.get_counterparty_node_id(), msg,
                                                                });
                                                        }
                                                        if let Some(tx) = tx_opt {
@@ -5654,18 +6114,18 @@ where
                                                                        });
                                                                }
 
-                                                               self.issue_channel_close_events(chan, ClosureReason::CooperativeClosure);
+                                                               self.issue_channel_close_events(&chan.context, ClosureReason::CooperativeClosure);
 
                                                                log_info!(self.logger, "Broadcasting {}", log_tx!(tx));
-                                                               self.tx_broadcaster.broadcast_transaction(&tx);
-                                                               update_maps_on_chan_removal!(self, chan);
+                                                               self.tx_broadcaster.broadcast_transactions(&[&tx]);
+                                                               update_maps_on_chan_removal!(self, &chan.context);
                                                                false
                                                        } else { true }
                                                },
                                                Err(e) => {
                                                        has_update = true;
                                                        let (close_channel, res) = convert_chan_err!(self, e, chan, channel_id);
-                                                       handle_errors.push((chan.get_counterparty_node_id(), Err(res)));
+                                                       handle_errors.push((chan.context.get_counterparty_node_id(), Err(res)));
                                                        !close_channel
                                                }
                                        }
@@ -5692,48 +6152,20 @@ where
                        // Channel::force_shutdown tries to make us do) as we may still be in initialization,
                        // so we track the update internally and handle it when the user next calls
                        // timer_tick_occurred, guaranteeing we're running normally.
-                       if let Some((funding_txo, update)) = failure.0.take() {
+                       if let Some((counterparty_node_id, funding_txo, update)) = failure.0.take() {
                                assert_eq!(update.updates.len(), 1);
                                if let ChannelMonitorUpdateStep::ChannelForceClosed { should_broadcast } = update.updates[0] {
                                        assert!(should_broadcast);
                                } else { unreachable!(); }
-                               self.pending_background_events.lock().unwrap().push(BackgroundEvent::MonitorUpdateRegeneratedOnStartup((funding_txo, update)));
+                               self.pending_background_events.lock().unwrap().push(
+                                       BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
+                                               counterparty_node_id, funding_txo, update
+                                       });
                        }
                        self.finish_force_close_channel(failure);
                }
        }
 
-       fn set_payment_hash_secret_map(&self, payment_hash: PaymentHash, payment_preimage: Option<PaymentPreimage>, min_value_msat: Option<u64>, invoice_expiry_delta_secs: u32) -> Result<PaymentSecret, APIError> {
-               assert!(invoice_expiry_delta_secs <= 60*60*24*365); // Sadly bitcoin timestamps are u32s, so panic before 2106
-
-               if min_value_msat.is_some() && min_value_msat.unwrap() > MAX_VALUE_MSAT {
-                       return Err(APIError::APIMisuseError { err: format!("min_value_msat of {} greater than total 21 million bitcoin supply", min_value_msat.unwrap()) });
-               }
-
-               let payment_secret = PaymentSecret(self.entropy_source.get_secure_random_bytes());
-
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
-               let mut payment_secrets = self.pending_inbound_payments.lock().unwrap();
-               match payment_secrets.entry(payment_hash) {
-                       hash_map::Entry::Vacant(e) => {
-                               e.insert(PendingInboundPayment {
-                                       payment_secret, min_value_msat, payment_preimage,
-                                       user_payment_id: 0, // For compatibility with version 0.0.103 and earlier
-                                       // We assume that highest_seen_timestamp is pretty close to the current time -
-                                       // it's updated when we receive a new block with the maximum time we've seen in
-                                       // a header. It should never be more than two hours in the future.
-                                       // Thus, we add two hours here as a buffer to ensure we absolutely
-                                       // never fail a payment too early.
-                                       // Note that we assume that received blocks have reasonably up-to-date
-                                       // timestamps.
-                                       expiry_time: self.highest_seen_timestamp.load(Ordering::Acquire) as u64 + invoice_expiry_delta_secs as u64 + 7200,
-                               });
-                       },
-                       hash_map::Entry::Occupied(_) => return Err(APIError::APIMisuseError { err: "Duplicate payment hash".to_owned() }),
-               }
-               Ok(payment_secret)
-       }
-
        /// Gets a payment secret and payment hash for use in an invoice given to a third party wishing
        /// to pay us.
        ///
@@ -5773,23 +6205,6 @@ where
                        min_final_cltv_expiry_delta)
        }
 
-       /// Legacy version of [`create_inbound_payment`]. Use this method if you wish to share
-       /// serialized state with LDK node(s) running 0.0.103 and earlier.
-       ///
-       /// May panic if `invoice_expiry_delta_secs` is greater than one year.
-       ///
-       /// # Note
-       /// This method is deprecated and will be removed soon.
-       ///
-       /// [`create_inbound_payment`]: Self::create_inbound_payment
-       #[deprecated]
-       pub fn create_inbound_payment_legacy(&self, min_value_msat: Option<u64>, invoice_expiry_delta_secs: u32) -> Result<(PaymentHash, PaymentSecret), APIError> {
-               let payment_preimage = PaymentPreimage(self.entropy_source.get_secure_random_bytes());
-               let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0).into_inner());
-               let payment_secret = self.set_payment_hash_secret_map(payment_hash, Some(payment_preimage), min_value_msat, invoice_expiry_delta_secs)?;
-               Ok((payment_hash, payment_secret))
-       }
-
        /// Gets a [`PaymentSecret`] for a given [`PaymentHash`], for which the payment preimage is
        /// stored external to LDK.
        ///
@@ -5843,20 +6258,6 @@ where
                        min_final_cltv_expiry)
        }
 
-       /// Legacy version of [`create_inbound_payment_for_hash`]. Use this method if you wish to share
-       /// serialized state with LDK node(s) running 0.0.103 and earlier.
-       ///
-       /// May panic if `invoice_expiry_delta_secs` is greater than one year.
-       ///
-       /// # Note
-       /// This method is deprecated and will be removed soon.
-       ///
-       /// [`create_inbound_payment_for_hash`]: Self::create_inbound_payment_for_hash
-       #[deprecated]
-       pub fn create_inbound_payment_for_hash_legacy(&self, payment_hash: PaymentHash, min_value_msat: Option<u64>, invoice_expiry_delta_secs: u32) -> Result<PaymentSecret, APIError> {
-               self.set_payment_hash_secret_map(payment_hash, None, min_value_msat, invoice_expiry_delta_secs)
-       }
-
        /// Gets an LDK-generated payment preimage from a payment hash and payment secret that were
        /// previously returned from [`create_inbound_payment`].
        ///
@@ -5931,7 +6332,7 @@ where
                inflight_htlcs
        }
 
-       #[cfg(any(test, fuzzing, feature = "_test_utils"))]
+       #[cfg(any(test, feature = "_test_utils"))]
        pub fn get_and_clear_pending_events(&self) -> Vec<events::Event> {
                let events = core::cell::RefCell::new(Vec::new());
                let event_handler = |event: events::Event| events.borrow_mut().push(event);
@@ -5961,33 +6362,43 @@ where
                self.pending_outbound_payments.clear_pending_payments()
        }
 
-       fn handle_monitor_update_release(&self, counterparty_node_id: PublicKey, channel_funding_outpoint: OutPoint) {
+       /// When something which was blocking a channel from updating its [`ChannelMonitor`] (e.g. an
+       /// [`Event`] being handled) completes, this should be called to restore the channel to normal
+       /// operation. It will double-check that nothing *else* is also blocking the same channel from
+       /// making progress and then any blocked [`ChannelMonitorUpdate`]s fly.
+       fn handle_monitor_update_release(&self, counterparty_node_id: PublicKey, channel_funding_outpoint: OutPoint, mut completed_blocker: Option<RAAMonitorUpdateBlockingAction>) {
                let mut errors = Vec::new();
                loop {
                        let per_peer_state = self.per_peer_state.read().unwrap();
                        if let Some(peer_state_mtx) = per_peer_state.get(&counterparty_node_id) {
                                let mut peer_state_lck = peer_state_mtx.lock().unwrap();
                                let peer_state = &mut *peer_state_lck;
-                               if self.pending_events.lock().unwrap().iter()
-                                       .any(|(_ev, action_opt)| action_opt == &Some(EventCompletionAction::ReleaseRAAChannelMonitorUpdate {
-                                               channel_funding_outpoint, counterparty_node_id
-                                       }))
-                               {
-                                       // Check that, while holding the peer lock, we don't have another event
-                                       // blocking any monitor updates for this channel. If we do, let those
-                                       // events be the ones that ultimately release the monitor update(s).
-                                       log_trace!(self.logger, "Delaying monitor unlock for channel {} as another event is pending",
+
+                               if let Some(blocker) = completed_blocker.take() {
+                                       // Only do this on the first iteration of the loop.
+                                       if let Some(blockers) = peer_state.actions_blocking_raa_monitor_updates
+                                               .get_mut(&channel_funding_outpoint.to_channel_id())
+                                       {
+                                               blockers.retain(|iter| iter != &blocker);
+                                       }
+                               }
+
+                               if self.raa_monitor_updates_held(&peer_state.actions_blocking_raa_monitor_updates,
+                                       channel_funding_outpoint, counterparty_node_id) {
+                                       // Check that, while holding the peer lock, we don't have anything else
+                                       // blocking monitor updates for this channel. If we do, release the monitor
+                                       // update(s) when those blockers complete.
+                                       log_trace!(self.logger, "Delaying monitor unlock for channel {} as another channel's mon update needs to complete first",
                                                log_bytes!(&channel_funding_outpoint.to_channel_id()[..]));
                                        break;
                                }
+
                                if let hash_map::Entry::Occupied(mut chan) = peer_state.channel_by_id.entry(channel_funding_outpoint.to_channel_id()) {
-                                       debug_assert_eq!(chan.get().get_funding_txo().unwrap(), channel_funding_outpoint);
+                                       debug_assert_eq!(chan.get().context.get_funding_txo().unwrap(), channel_funding_outpoint);
                                        if let Some((monitor_update, further_update_exists)) = chan.get_mut().unblock_next_blocked_monitor_update() {
                                                log_debug!(self.logger, "Unlocking monitor updating for channel {} and updating monitor",
                                                        log_bytes!(&channel_funding_outpoint.to_channel_id()[..]));
-                                               let update_res = self.chain_monitor.update_channel(channel_funding_outpoint, monitor_update);
-                                               let update_id = monitor_update.update_id;
-                                               if let Err(e) = handle_new_monitor_update!(self, update_res, update_id,
+                                               if let Err(e) = handle_new_monitor_update!(self, channel_funding_outpoint, monitor_update,
                                                        peer_state_lck, peer_state, per_peer_state, chan)
                                                {
                                                        errors.push((e, counterparty_node_id));
@@ -6021,7 +6432,7 @@ where
                                EventCompletionAction::ReleaseRAAChannelMonitorUpdate {
                                        channel_funding_outpoint, counterparty_node_id
                                } => {
-                                       self.handle_monitor_update_release(counterparty_node_id, channel_funding_outpoint);
+                                       self.handle_monitor_update_release(counterparty_node_id, channel_funding_outpoint, None);
                                }
                        }
                }
@@ -6066,7 +6477,7 @@ where
        fn get_and_clear_pending_msg_events(&self) -> Vec<MessageSendEvent> {
                let events = RefCell::new(Vec::new());
                PersistenceNotifierGuard::optionally_notify(&self.total_consistency_lock, &self.persistence_notifier, || {
-                       let mut result = NotifyOption::SkipPersist;
+                       let mut result = self.process_background_events();
 
                        // TODO: This behavior should be documented. It's unintuitive that we query
                        // ChannelMonitors when clearing other events.
@@ -6147,7 +6558,8 @@ where
        }
 
        fn block_disconnected(&self, header: &BlockHeader, height: u32) {
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::optionally_notify(&self.total_consistency_lock,
+                       &self.persistence_notifier, || -> NotifyOption { NotifyOption::DoPersist });
                let new_height = height - 1;
                {
                        let mut best_block = self.best_block.write().unwrap();
@@ -6181,7 +6593,8 @@ where
                let block_hash = header.block_hash();
                log_trace!(self.logger, "{} transactions included in block {} at height {} provided", txdata.len(), block_hash, height);
 
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::optionally_notify(&self.total_consistency_lock,
+                       &self.persistence_notifier, || -> NotifyOption { NotifyOption::DoPersist });
                self.do_chain_event(Some(height), |channel| channel.transactions_confirmed(&block_hash, height, txdata, self.genesis_hash.clone(), &self.node_signer, &self.default_configuration, &self.logger)
                        .map(|(a, b)| (a, Vec::new(), b)));
 
@@ -6200,8 +6613,8 @@ where
                let block_hash = header.block_hash();
                log_trace!(self.logger, "New best block: {} at height {}", block_hash, height);
 
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
-
+               let _persistence_guard = PersistenceNotifierGuard::optionally_notify(&self.total_consistency_lock,
+                       &self.persistence_notifier, || -> NotifyOption { NotifyOption::DoPersist });
                *self.best_block.write().unwrap() = BestBlock::new(block_hash, height);
 
                self.do_chain_event(Some(height), |channel| channel.best_block_updated(height, header.time, self.genesis_hash.clone(), &self.node_signer, &self.default_configuration, &self.logger));
@@ -6235,7 +6648,7 @@ where
                        let mut peer_state_lock = peer_state_mutex.lock().unwrap();
                        let peer_state = &mut *peer_state_lock;
                        for chan in peer_state.channel_by_id.values() {
-                               if let (Some(funding_txo), Some(block_hash)) = (chan.get_funding_txo(), chan.get_funding_tx_confirmed_in()) {
+                               if let (Some(funding_txo), Some(block_hash)) = (chan.context.get_funding_txo(), chan.context.get_funding_tx_confirmed_in()) {
                                        res.push((funding_txo.txid, Some(block_hash)));
                                }
                        }
@@ -6244,9 +6657,10 @@ where
        }
 
        fn transaction_unconfirmed(&self, txid: &Txid) {
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::optionally_notify(&self.total_consistency_lock,
+                       &self.persistence_notifier, || -> NotifyOption { NotifyOption::DoPersist });
                self.do_chain_event(None, |channel| {
-                       if let Some(funding_txo) = channel.get_funding_txo() {
+                       if let Some(funding_txo) = channel.context.get_funding_txo() {
                                if funding_txo.txid == *txid {
                                        channel.funding_transaction_unconfirmed(&self.logger).map(|()| (None, Vec::new(), None))
                                } else { Ok((None, Vec::new(), None)) }
@@ -6289,20 +6703,20 @@ where
                                                for (source, payment_hash) in timed_out_pending_htlcs.drain(..) {
                                                        let (failure_code, data) = self.get_htlc_inbound_temp_fail_err_and_data(0x1000|14 /* expiry_too_soon */, &channel);
                                                        timed_out_htlcs.push((source, payment_hash, HTLCFailReason::reason(failure_code, data),
-                                                               HTLCDestination::NextHopChannel { node_id: Some(channel.get_counterparty_node_id()), channel_id: channel.channel_id() }));
+                                                               HTLCDestination::NextHopChannel { node_id: Some(channel.context.get_counterparty_node_id()), channel_id: channel.context.channel_id() }));
                                                }
                                                if let Some(channel_ready) = channel_ready_opt {
                                                        send_channel_ready!(self, pending_msg_events, channel, channel_ready);
-                                                       if channel.is_usable() {
-                                                               log_trace!(self.logger, "Sending channel_ready with private initial channel_update for our counterparty on channel {}", log_bytes!(channel.channel_id()));
+                                                       if channel.context.is_usable() {
+                                                               log_trace!(self.logger, "Sending channel_ready with private initial channel_update for our counterparty on channel {}", log_bytes!(channel.context.channel_id()));
                                                                if let Ok(msg) = self.get_channel_update_for_unicast(channel) {
                                                                        pending_msg_events.push(events::MessageSendEvent::SendChannelUpdate {
-                                                                               node_id: channel.get_counterparty_node_id(),
+                                                                               node_id: channel.context.get_counterparty_node_id(),
                                                                                msg,
                                                                        });
                                                                }
                                                        } else {
-                                                               log_trace!(self.logger, "Sending channel_ready WITHOUT channel_update for {}", log_bytes!(channel.channel_id()));
+                                                               log_trace!(self.logger, "Sending channel_ready WITHOUT channel_update for {}", log_bytes!(channel.context.channel_id()));
                                                        }
                                                }
 
@@ -6312,9 +6726,9 @@ where
                                                }
 
                                                if let Some(announcement_sigs) = announcement_sigs {
-                                                       log_trace!(self.logger, "Sending announcement_signatures for channel {}", log_bytes!(channel.channel_id()));
+                                                       log_trace!(self.logger, "Sending announcement_signatures for channel {}", log_bytes!(channel.context.channel_id()));
                                                        pending_msg_events.push(events::MessageSendEvent::SendAnnouncementSignatures {
-                                                               node_id: channel.get_counterparty_node_id(),
+                                                               node_id: channel.context.get_counterparty_node_id(),
                                                                msg: announcement_sigs,
                                                        });
                                                        if let Some(height) = height_opt {
@@ -6329,7 +6743,7 @@ where
                                                        }
                                                }
                                                if channel.is_our_channel_ready() {
-                                                       if let Some(real_scid) = channel.get_short_channel_id() {
+                                                       if let Some(real_scid) = channel.context.get_short_channel_id() {
                                                                // If we sent a 0conf channel_ready, and now have an SCID, we add it
                                                                // to the short_to_chan_info map here. Note that we check whether we
                                                                // can relay using the real SCID at relay-time (i.e.
@@ -6337,28 +6751,28 @@ where
                                                                // un-confirmed we force-close the channel, ensuring short_to_chan_info
                                                                // is always consistent.
                                                                let mut short_to_chan_info = self.short_to_chan_info.write().unwrap();
-                                                               let scid_insert = short_to_chan_info.insert(real_scid, (channel.get_counterparty_node_id(), channel.channel_id()));
-                                                               assert!(scid_insert.is_none() || scid_insert.unwrap() == (channel.get_counterparty_node_id(), channel.channel_id()),
+                                                               let scid_insert = short_to_chan_info.insert(real_scid, (channel.context.get_counterparty_node_id(), channel.context.channel_id()));
+                                                               assert!(scid_insert.is_none() || scid_insert.unwrap() == (channel.context.get_counterparty_node_id(), channel.context.channel_id()),
                                                                        "SCIDs should never collide - ensure you weren't behind by a full {} blocks when creating channels",
                                                                        fake_scid::MAX_SCID_BLOCKS_FROM_NOW);
                                                        }
                                                }
                                        } else if let Err(reason) = res {
-                                               update_maps_on_chan_removal!(self, channel);
+                                               update_maps_on_chan_removal!(self, &channel.context);
                                                // It looks like our counterparty went on-chain or funding transaction was
                                                // reorged out of the main chain. Close the channel.
-                                               failed_channels.push(channel.force_shutdown(true));
+                                               failed_channels.push(channel.context.force_shutdown(true));
                                                if let Ok(update) = self.get_channel_update_for_broadcast(&channel) {
                                                        pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
                                                                msg: update
                                                        });
                                                }
                                                let reason_message = format!("{}", reason);
-                                               self.issue_channel_close_events(channel, reason);
+                                               self.issue_channel_close_events(&channel.context, reason);
                                                pending_msg_events.push(events::MessageSendEvent::HandleError {
-                                                       node_id: channel.get_counterparty_node_id(),
+                                                       node_id: channel.context.get_counterparty_node_id(),
                                                        action: msgs::ErrorAction::SendErrorMessage { msg: msgs::ErrorMessage {
-                                                               channel_id: channel.channel_id(),
+                                                               channel_id: channel.context.channel_id(),
                                                                data: reason_message,
                                                        } },
                                                });
@@ -6488,7 +6902,7 @@ where
        L::Target: Logger,
 {
        fn handle_open_channel(&self, counterparty_node_id: &PublicKey, msg: &msgs::OpenChannel) {
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
                let _ = handle_error!(self, self.internal_open_channel(counterparty_node_id, msg), *counterparty_node_id);
        }
 
@@ -6499,7 +6913,7 @@ where
        }
 
        fn handle_accept_channel(&self, counterparty_node_id: &PublicKey, msg: &msgs::AcceptChannel) {
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
                let _ = handle_error!(self, self.internal_accept_channel(counterparty_node_id, msg), *counterparty_node_id);
        }
 
@@ -6510,74 +6924,75 @@ where
        }
 
        fn handle_funding_created(&self, counterparty_node_id: &PublicKey, msg: &msgs::FundingCreated) {
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
                let _ = handle_error!(self, self.internal_funding_created(counterparty_node_id, msg), *counterparty_node_id);
        }
 
        fn handle_funding_signed(&self, counterparty_node_id: &PublicKey, msg: &msgs::FundingSigned) {
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
                let _ = handle_error!(self, self.internal_funding_signed(counterparty_node_id, msg), *counterparty_node_id);
        }
 
        fn handle_channel_ready(&self, counterparty_node_id: &PublicKey, msg: &msgs::ChannelReady) {
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
                let _ = handle_error!(self, self.internal_channel_ready(counterparty_node_id, msg), *counterparty_node_id);
        }
 
        fn handle_shutdown(&self, counterparty_node_id: &PublicKey, msg: &msgs::Shutdown) {
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
                let _ = handle_error!(self, self.internal_shutdown(counterparty_node_id, msg), *counterparty_node_id);
        }
 
        fn handle_closing_signed(&self, counterparty_node_id: &PublicKey, msg: &msgs::ClosingSigned) {
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
                let _ = handle_error!(self, self.internal_closing_signed(counterparty_node_id, msg), *counterparty_node_id);
        }
 
        fn handle_update_add_htlc(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateAddHTLC) {
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
                let _ = handle_error!(self, self.internal_update_add_htlc(counterparty_node_id, msg), *counterparty_node_id);
        }
 
        fn handle_update_fulfill_htlc(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateFulfillHTLC) {
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
                let _ = handle_error!(self, self.internal_update_fulfill_htlc(counterparty_node_id, msg), *counterparty_node_id);
        }
 
        fn handle_update_fail_htlc(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateFailHTLC) {
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
                let _ = handle_error!(self, self.internal_update_fail_htlc(counterparty_node_id, msg), *counterparty_node_id);
        }
 
        fn handle_update_fail_malformed_htlc(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateFailMalformedHTLC) {
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
                let _ = handle_error!(self, self.internal_update_fail_malformed_htlc(counterparty_node_id, msg), *counterparty_node_id);
        }
 
        fn handle_commitment_signed(&self, counterparty_node_id: &PublicKey, msg: &msgs::CommitmentSigned) {
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
                let _ = handle_error!(self, self.internal_commitment_signed(counterparty_node_id, msg), *counterparty_node_id);
        }
 
        fn handle_revoke_and_ack(&self, counterparty_node_id: &PublicKey, msg: &msgs::RevokeAndACK) {
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
                let _ = handle_error!(self, self.internal_revoke_and_ack(counterparty_node_id, msg), *counterparty_node_id);
        }
 
        fn handle_update_fee(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateFee) {
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
                let _ = handle_error!(self, self.internal_update_fee(counterparty_node_id, msg), *counterparty_node_id);
        }
 
        fn handle_announcement_signatures(&self, counterparty_node_id: &PublicKey, msg: &msgs::AnnouncementSignatures) {
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
                let _ = handle_error!(self, self.internal_announcement_signatures(counterparty_node_id, msg), *counterparty_node_id);
        }
 
        fn handle_channel_update(&self, counterparty_node_id: &PublicKey, msg: &msgs::ChannelUpdate) {
                PersistenceNotifierGuard::optionally_notify(&self.total_consistency_lock, &self.persistence_notifier, || {
+                       let force_persist = self.process_background_events();
                        if let Ok(persist) = handle_error!(self, self.internal_channel_update(counterparty_node_id, msg), *counterparty_node_id) {
-                               persist
+                               if force_persist == NotifyOption::DoPersist { NotifyOption::DoPersist } else { persist }
                        } else {
                                NotifyOption::SkipPersist
                        }
@@ -6585,12 +7000,12 @@ where
        }
 
        fn handle_channel_reestablish(&self, counterparty_node_id: &PublicKey, msg: &msgs::ChannelReestablish) {
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
                let _ = handle_error!(self, self.internal_channel_reestablish(counterparty_node_id, msg), *counterparty_node_id);
        }
 
        fn peer_disconnected(&self, counterparty_node_id: &PublicKey) {
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
                let mut failed_channels = Vec::new();
                let mut per_peer_state = self.per_peer_state.write().unwrap();
                let remove_peer = {
@@ -6603,12 +7018,22 @@ where
                                peer_state.channel_by_id.retain(|_, chan| {
                                        chan.remove_uncommitted_htlcs_and_mark_paused(&self.logger);
                                        if chan.is_shutdown() {
-                                               update_maps_on_chan_removal!(self, chan);
-                                               self.issue_channel_close_events(chan, ClosureReason::DisconnectedPeer);
+                                               update_maps_on_chan_removal!(self, &chan.context);
+                                               self.issue_channel_close_events(&chan.context, ClosureReason::DisconnectedPeer);
                                                return false;
                                        }
                                        true
                                });
+                               peer_state.inbound_v1_channel_by_id.retain(|_, chan| {
+                                       update_maps_on_chan_removal!(self, &chan.context);
+                                       self.issue_channel_close_events(&chan.context, ClosureReason::DisconnectedPeer);
+                                       false
+                               });
+                               peer_state.outbound_v1_channel_by_id.retain(|_, chan| {
+                                       update_maps_on_chan_removal!(self, &chan.context);
+                                       self.issue_channel_close_events(&chan.context, ClosureReason::DisconnectedPeer);
+                                       false
+                               });
                                pending_msg_events.retain(|msg| {
                                        match msg {
                                                // V1 Channel Establishment
@@ -6672,7 +7097,7 @@ where
                        return Err(());
                }
 
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
 
                // If we have too many peers connected which don't have funded channels, disconnect the
                // peer immediately (as long as it doesn't have funded channels). If we have a bunch of
@@ -6690,9 +7115,13 @@ where
                                        }
                                        e.insert(Mutex::new(PeerState {
                                                channel_by_id: HashMap::new(),
+                                               outbound_v1_channel_by_id: HashMap::new(),
+                                               inbound_v1_channel_by_id: HashMap::new(),
                                                latest_features: init_msg.features.clone(),
                                                pending_msg_events: Vec::new(),
+                                               in_flight_monitor_updates: BTreeMap::new(),
                                                monitor_update_blocked_actions: BTreeMap::new(),
+                                               actions_blocking_raa_monitor_updates: BTreeMap::new(),
                                                is_connected: true,
                                        }));
                                },
@@ -6722,8 +7151,8 @@ where
                        let peer_state = &mut *peer_state_lock;
                        let pending_msg_events = &mut peer_state.pending_msg_events;
                        peer_state.channel_by_id.retain(|_, chan| {
-                               let retain = if chan.get_counterparty_node_id() == *counterparty_node_id {
-                                       if !chan.have_received_message() {
+                               let retain = if chan.context.get_counterparty_node_id() == *counterparty_node_id {
+                                       if !chan.context.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
@@ -6731,13 +7160,13 @@ where
                                                false
                                        } else {
                                                pending_msg_events.push(events::MessageSendEvent::SendChannelReestablish {
-                                                       node_id: chan.get_counterparty_node_id(),
+                                                       node_id: chan.context.get_counterparty_node_id(),
                                                        msg: chan.get_channel_reestablish(&self.logger),
                                                });
                                                true
                                        }
                                } else { true };
-                               if retain && chan.get_counterparty_node_id() != *counterparty_node_id {
+                               if retain && chan.context.get_counterparty_node_id() != *counterparty_node_id {
                                        if let Some(msg) = chan.get_signed_channel_announcement(&self.node_signer, self.genesis_hash.clone(), self.best_block.read().unwrap().height(), &self.default_configuration) {
                                                if let Ok(update_msg) = self.get_channel_update_for_broadcast(chan) {
                                                        pending_msg_events.push(events::MessageSendEvent::SendChannelAnnouncement {
@@ -6755,7 +7184,7 @@ where
        }
 
        fn handle_error(&self, counterparty_node_id: &PublicKey, msg: &msgs::ErrorMessage) {
-               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
+               let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
 
                if msg.channel_id == [0; 32] {
                        let channel_ids: Vec<[u8; 32]> = {
@@ -6764,7 +7193,9 @@ where
                                if peer_state_mutex_opt.is_none() { return; }
                                let mut peer_state_lock = peer_state_mutex_opt.unwrap().lock().unwrap();
                                let peer_state = &mut *peer_state_lock;
-                               peer_state.channel_by_id.keys().cloned().collect()
+                               peer_state.channel_by_id.keys().cloned()
+                                       .chain(peer_state.outbound_v1_channel_by_id.keys().cloned())
+                                       .chain(peer_state.inbound_v1_channel_by_id.keys().cloned()).collect()
                        };
                        for channel_id in channel_ids {
                                // Untrusted messages from peer, we throw away the error if id points to a non-existent channel
@@ -6778,7 +7209,7 @@ where
                                if peer_state_mutex_opt.is_none() { return; }
                                let mut peer_state_lock = peer_state_mutex_opt.unwrap().lock().unwrap();
                                let peer_state = &mut *peer_state_lock;
-                               if let Some(chan) = peer_state.channel_by_id.get_mut(&msg.channel_id) {
+                               if let Some(chan) = peer_state.outbound_v1_channel_by_id.get_mut(&msg.channel_id) {
                                        if let Ok(msg) = chan.maybe_handle_error_without_close(self.genesis_hash) {
                                                peer_state.pending_msg_events.push(events::MessageSendEvent::SendOpenChannel {
                                                        node_id: *counterparty_node_id,
@@ -6802,6 +7233,10 @@ where
                provided_init_features(&self.default_configuration)
        }
 
+       fn get_genesis_hashes(&self) -> Option<Vec<ChainHash>> {
+               Some(vec![ChainHash::from(&self.genesis_hash[..])])
+       }
+
        fn handle_tx_add_input(&self, counterparty_node_id: &PublicKey, msg: &msgs::TxAddInput) {
                let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
                        "Dual-funded channels not supported".to_owned(),
@@ -6887,7 +7322,7 @@ pub(crate) fn provided_channel_type_features(config: &UserConfig) -> ChannelType
 
 /// Fetches the set of [`InitFeatures`] flags which are provided by or required by
 /// [`ChannelManager`].
-pub fn provided_init_features(_config: &UserConfig) -> InitFeatures {
+pub fn provided_init_features(config: &UserConfig) -> InitFeatures {
        // Note that if new features are added here which other peers may (eventually) require, we
        // should also add the corresponding (optional) bit to the [`ChannelMessageHandler`] impl for
        // [`ErroringMessageHandler`].
@@ -6903,11 +7338,8 @@ pub fn provided_init_features(_config: &UserConfig) -> InitFeatures {
        features.set_channel_type_optional();
        features.set_scid_privacy_optional();
        features.set_zero_conf_optional();
-       #[cfg(anchors)]
-       { // Attributes are not allowed on if expressions on our current MSRV of 1.41.
-               if _config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx {
-                       features.set_anchors_zero_fee_htlc_tx_optional();
-               }
+       if config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx {
+               features.set_anchors_zero_fee_htlc_tx_optional();
        }
        features
 }
@@ -6951,10 +7383,9 @@ impl Writeable for ChannelDetails {
                        (14, user_channel_id_low, required),
                        (16, self.balance_msat, required),
                        (18, self.outbound_capacity_msat, required),
-                       // Note that by the time we get past the required read above, outbound_capacity_msat will be
-                       // filled in, so we can safely unwrap it here.
-                       (19, self.next_outbound_htlc_limit_msat, (default_value, outbound_capacity_msat.0.unwrap() as u64)),
+                       (19, self.next_outbound_htlc_limit_msat, required),
                        (20, self.inbound_capacity_msat, required),
+                       (21, self.next_outbound_htlc_minimum_msat, required),
                        (22, self.confirmations_required, option),
                        (24, self.force_close_spend_delay, option),
                        (26, self.is_outbound, required),
@@ -6965,6 +7396,7 @@ impl Writeable for ChannelDetails {
                        (35, self.inbound_htlc_maximum_msat, option),
                        (37, user_channel_id_high_opt, option),
                        (39, self.feerate_sat_per_1000_weight, option),
+                       (41, self.channel_shutdown_state, option),
                });
                Ok(())
        }
@@ -6991,6 +7423,7 @@ impl Readable for ChannelDetails {
                        // filled in, so we can safely unwrap it here.
                        (19, next_outbound_htlc_limit_msat, (default_value, outbound_capacity_msat.0.unwrap() as u64)),
                        (20, inbound_capacity_msat, required),
+                       (21, next_outbound_htlc_minimum_msat, (default_value, 0)),
                        (22, confirmations_required, option),
                        (24, force_close_spend_delay, option),
                        (26, is_outbound, required),
@@ -7001,6 +7434,7 @@ impl Readable for ChannelDetails {
                        (35, inbound_htlc_maximum_msat, option),
                        (37, user_channel_id_high_opt, option),
                        (39, feerate_sat_per_1000_weight, option),
+                       (41, channel_shutdown_state, option),
                });
 
                // `user_channel_id` used to be a single u64 value. In order to remain backwards compatible with
@@ -7024,6 +7458,7 @@ impl Readable for ChannelDetails {
                        balance_msat: balance_msat.0.unwrap(),
                        outbound_capacity_msat: outbound_capacity_msat.0.unwrap(),
                        next_outbound_htlc_limit_msat: next_outbound_htlc_limit_msat.0.unwrap(),
+                       next_outbound_htlc_minimum_msat: next_outbound_htlc_minimum_msat.0.unwrap(),
                        inbound_capacity_msat: inbound_capacity_msat.0.unwrap(),
                        confirmations_required,
                        confirmations,
@@ -7035,6 +7470,7 @@ impl Readable for ChannelDetails {
                        inbound_htlc_minimum_msat,
                        inbound_htlc_maximum_msat,
                        feerate_sat_per_1000_weight,
+                       channel_shutdown_state,
                })
        }
 }
@@ -7060,6 +7496,7 @@ impl_writeable_tlv_based_enum!(PendingHTLCRouting,
                (0, payment_preimage, required),
                (2, incoming_cltv_expiry, required),
                (3, payment_metadata, option),
+               (4, payment_data, option), // Added in 0.0.116
        },
 ;);
 
@@ -7070,6 +7507,7 @@ impl_writeable_tlv_based!(PendingHTLCInfo, {
        (6, outgoing_amt_msat, required),
        (8, outgoing_cltv_value, required),
        (9, incoming_amt_msat, option),
+       (10, skimmed_fee_msat, option),
 });
 
 
@@ -7168,6 +7606,7 @@ impl Writeable for ClaimableHTLC {
                        (5, self.total_value_received, option),
                        (6, self.cltv_expiry, required),
                        (8, keysend_preimage, option),
+                       (10, self.counterparty_skimmed_fee_msat, option),
                });
                Ok(())
        }
@@ -7175,24 +7614,19 @@ impl Writeable for ClaimableHTLC {
 
 impl Readable for ClaimableHTLC {
        fn read<R: Read>(reader: &mut R) -> Result<Self, DecodeError> {
-               let mut prev_hop = crate::util::ser::RequiredWrapper(None);
-               let mut value = 0;
-               let mut sender_intended_value = None;
-               let mut payment_data: Option<msgs::FinalOnionHopData> = None;
-               let mut cltv_expiry = 0;
-               let mut total_value_received = None;
-               let mut total_msat = None;
-               let mut keysend_preimage: Option<PaymentPreimage> = None;
-               read_tlv_fields!(reader, {
+               _init_and_read_tlv_fields!(reader, {
                        (0, prev_hop, required),
                        (1, total_msat, option),
-                       (2, value, required),
+                       (2, value_ser, required),
                        (3, sender_intended_value, option),
-                       (4, payment_data, option),
+                       (4, payment_data_opt, option),
                        (5, total_value_received, option),
                        (6, cltv_expiry, required),
-                       (8, keysend_preimage, option)
+                       (8, keysend_preimage, option),
+                       (10, counterparty_skimmed_fee_msat, option),
                });
+               let payment_data: Option<msgs::FinalOnionHopData> = payment_data_opt;
+               let value = value_ser.0.unwrap();
                let onion_payload = match keysend_preimage {
                        Some(p) => {
                                if payment_data.is_some() {
@@ -7221,7 +7655,8 @@ impl Readable for ClaimableHTLC {
                        total_value_received,
                        total_msat: total_msat.unwrap(),
                        onion_payload,
-                       cltv_expiry,
+                       cltv_expiry: cltv_expiry.0.unwrap(),
+                       counterparty_skimmed_fee_msat,
                })
        }
 }
@@ -7359,7 +7794,7 @@ where
                                }
                                number_of_channels += peer_state.channel_by_id.len();
                                for (_, channel) in peer_state.channel_by_id.iter() {
-                                       if !channel.is_funding_initiated() {
+                                       if !channel.context.is_funding_initiated() {
                                                unfunded_channels += 1;
                                        }
                                }
@@ -7371,7 +7806,7 @@ where
                                let mut peer_state_lock = peer_state_mutex.lock().unwrap();
                                let peer_state = &mut *peer_state_lock;
                                for (_, channel) in peer_state.channel_by_id.iter() {
-                                       if channel.is_funding_initiated() {
+                                       if channel.context.is_funding_initiated() {
                                                channel.write(writer)?;
                                        }
                                }
@@ -7516,6 +7951,16 @@ where
                        pending_claiming_payments = None;
                }
 
+               let mut in_flight_monitor_updates: Option<HashMap<(&PublicKey, &OutPoint), &Vec<ChannelMonitorUpdate>>> = None;
+               for ((counterparty_id, _), peer_state) in per_peer_state.iter().zip(peer_states.iter()) {
+                       for (funding_outpoint, updates) in peer_state.in_flight_monitor_updates.iter() {
+                               if !updates.is_empty() {
+                                       if in_flight_monitor_updates.is_none() { in_flight_monitor_updates = Some(HashMap::new()); }
+                                       in_flight_monitor_updates.as_mut().unwrap().insert((counterparty_id, funding_outpoint), updates);
+                               }
+                       }
+               }
+
                write_tlv_fields!(writer, {
                        (1, pending_outbound_payments_no_retry, required),
                        (2, pending_intercepted_htlcs, option),
@@ -7526,6 +7971,7 @@ where
                        (7, self.fake_scid_rand_bytes, required),
                        (8, if events_not_backwards_compatible { Some(&*events) } else { None }, option),
                        (9, htlc_purposes, vec_type),
+                       (10, in_flight_monitor_updates, option),
                        (11, self.probing_cookie_secret, required),
                        (13, htlc_onion_fields, optional_vec),
                });
@@ -7575,6 +8021,14 @@ impl Readable for VecDeque<(Event, Option<EventCompletionAction>)> {
        }
 }
 
+impl_writeable_tlv_based_enum!(ChannelShutdownState,
+       (0, NotShuttingDown) => {},
+       (2, ShutdownInitiated) => {},
+       (4, ResolvingHTLCs) => {},
+       (6, NegotiatingClosingFee) => {},
+       (8, ShutdownComplete) => {}, ;
+);
+
 /// 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
@@ -7657,7 +8111,7 @@ where
        pub default_config: UserConfig,
 
        /// A map from channel funding outpoints to ChannelMonitors for those channels (ie
-       /// value.get_funding_txo() should be the key).
+       /// value.context.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
@@ -7742,41 +8196,33 @@ where
                let mut id_to_peer = HashMap::with_capacity(cmp::min(channel_count as usize, 128));
                let mut short_to_chan_info = HashMap::with_capacity(cmp::min(channel_count as usize, 128));
                let mut channel_closures = VecDeque::new();
-               let mut pending_background_events = Vec::new();
+               let mut close_background_events = Vec::new();
                for _ in 0..channel_count {
                        let mut channel: Channel<<SP::Target as SignerProvider>::Signer> = Channel::read(reader, (
                                &args.entropy_source, &args.signer_provider, best_block_height, &provided_channel_type_features(&args.default_config)
                        ))?;
-                       let funding_txo = channel.get_funding_txo().ok_or(DecodeError::InvalidValue)?;
+                       let funding_txo = channel.context.get_funding_txo().ok_or(DecodeError::InvalidValue)?;
                        funding_txo_set.insert(funding_txo.clone());
                        if let Some(ref mut monitor) = args.channel_monitors.get_mut(&funding_txo) {
-                               if channel.get_latest_complete_monitor_update_id() > monitor.get_latest_update_id() {
-                                       // If the channel is ahead of the monitor, return InvalidValue:
-                                       log_error!(args.logger, "A ChannelMonitor is stale compared to the current ChannelManager! This indicates a potentially-critical violation of the chain::Watch API!");
-                                       log_error!(args.logger, " The ChannelMonitor for channel {} is at update_id {} but the ChannelManager is at update_id {}.",
-                                               log_bytes!(channel.channel_id()), monitor.get_latest_update_id(), channel.get_latest_complete_monitor_update_id());
-                                       log_error!(args.logger, " The chain::Watch API *requires* that monitors are persisted durably before returning,");
-                                       log_error!(args.logger, " client applications must ensure that ChannelMonitor data is always available and the latest to avoid funds loss!");
-                                       log_error!(args.logger, " Without the latest ChannelMonitor we cannot continue without risking funds.");
-                                       log_error!(args.logger, " Please ensure the chain::Watch API requirements are met and file a bug report at https://github.com/lightningdevkit/rust-lightning");
-                                       return Err(DecodeError::InvalidValue);
-                               } else if channel.get_cur_holder_commitment_transaction_number() > monitor.get_cur_holder_commitment_number() ||
+                               if channel.get_cur_holder_commitment_transaction_number() > monitor.get_cur_holder_commitment_number() ||
                                                channel.get_revoked_counterparty_commitment_transaction_number() > monitor.get_min_seen_secret() ||
                                                channel.get_cur_counterparty_commitment_transaction_number() > monitor.get_cur_counterparty_commitment_number() ||
-                                               channel.get_latest_monitor_update_id() < monitor.get_latest_update_id() {
+                                               channel.context.get_latest_monitor_update_id() < monitor.get_latest_update_id() {
                                        // But if the channel is behind of the monitor, close the channel:
                                        log_error!(args.logger, "A ChannelManager is stale compared to the current ChannelMonitor!");
                                        log_error!(args.logger, " The channel will be force-closed and the latest commitment transaction from the ChannelMonitor broadcast.");
                                        log_error!(args.logger, " The ChannelMonitor for channel {} is at update_id {} but the ChannelManager is at update_id {}.",
-                                               log_bytes!(channel.channel_id()), monitor.get_latest_update_id(), channel.get_latest_monitor_update_id());
-                                       let (monitor_update, mut new_failed_htlcs) = channel.force_shutdown(true);
-                                       if let Some(monitor_update) = monitor_update {
-                                               pending_background_events.push(BackgroundEvent::MonitorUpdateRegeneratedOnStartup(monitor_update));
+                                               log_bytes!(channel.context.channel_id()), monitor.get_latest_update_id(), channel.context.get_latest_monitor_update_id());
+                                       let (monitor_update, mut new_failed_htlcs) = channel.context.force_shutdown(true);
+                                       if let Some((counterparty_node_id, funding_txo, update)) = monitor_update {
+                                               close_background_events.push(BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
+                                                       counterparty_node_id, funding_txo, update
+                                               });
                                        }
                                        failed_htlcs.append(&mut new_failed_htlcs);
                                        channel_closures.push_back((events::Event::ChannelClosed {
-                                               channel_id: channel.channel_id(),
-                                               user_channel_id: channel.get_user_id(),
+                                               channel_id: channel.context.channel_id(),
+                                               user_channel_id: channel.context.get_user_id(),
                                                reason: ClosureReason::OutdatedChannelManager
                                        }, None));
                                        for (channel_htlc_source, payment_hash) in channel.inflight_htlc_sources() {
@@ -7794,26 +8240,28 @@ where
                                                        // backwards leg of the HTLC will simply be rejected.
                                                        log_info!(args.logger,
                                                                "Failing HTLC with hash {} as it is missing in the ChannelMonitor for channel {} but was present in the (stale) ChannelManager",
-                                                               log_bytes!(channel.channel_id()), log_bytes!(payment_hash.0));
-                                                       failed_htlcs.push((channel_htlc_source.clone(), *payment_hash, channel.get_counterparty_node_id(), channel.channel_id()));
+                                                               log_bytes!(channel.context.channel_id()), log_bytes!(payment_hash.0));
+                                                       failed_htlcs.push((channel_htlc_source.clone(), *payment_hash, channel.context.get_counterparty_node_id(), channel.context.channel_id()));
                                                }
                                        }
                                } else {
-                                       log_info!(args.logger, "Successfully loaded channel {}", log_bytes!(channel.channel_id()));
-                                       if let Some(short_channel_id) = channel.get_short_channel_id() {
-                                               short_to_chan_info.insert(short_channel_id, (channel.get_counterparty_node_id(), channel.channel_id()));
+                                       log_info!(args.logger, "Successfully loaded channel {} at update_id {} against monitor at update id {}",
+                                               log_bytes!(channel.context.channel_id()), channel.context.get_latest_monitor_update_id(),
+                                               monitor.get_latest_update_id());
+                                       if let Some(short_channel_id) = channel.context.get_short_channel_id() {
+                                               short_to_chan_info.insert(short_channel_id, (channel.context.get_counterparty_node_id(), channel.context.channel_id()));
                                        }
-                                       if channel.is_funding_initiated() {
-                                               id_to_peer.insert(channel.channel_id(), channel.get_counterparty_node_id());
+                                       if channel.context.is_funding_initiated() {
+                                               id_to_peer.insert(channel.context.channel_id(), channel.context.get_counterparty_node_id());
                                        }
-                                       match peer_channels.entry(channel.get_counterparty_node_id()) {
+                                       match peer_channels.entry(channel.context.get_counterparty_node_id()) {
                                                hash_map::Entry::Occupied(mut entry) => {
                                                        let by_id_map = entry.get_mut();
-                                                       by_id_map.insert(channel.channel_id(), channel);
+                                                       by_id_map.insert(channel.context.channel_id(), channel);
                                                },
                                                hash_map::Entry::Vacant(entry) => {
                                                        let mut by_id_map = HashMap::new();
-                                                       by_id_map.insert(channel.channel_id(), channel);
+                                                       by_id_map.insert(channel.context.channel_id(), channel);
                                                        entry.insert(by_id_map);
                                                }
                                        }
@@ -7822,14 +8270,14 @@ where
                                // If we were persisted and shut down while the initial ChannelMonitor persistence
                                // was in-progress, we never broadcasted the funding transaction and can still
                                // safely discard the channel.
-                               let _ = channel.force_shutdown(false);
+                               let _ = channel.context.force_shutdown(false);
                                channel_closures.push_back((events::Event::ChannelClosed {
-                                       channel_id: channel.channel_id(),
-                                       user_channel_id: channel.get_user_id(),
+                                       channel_id: channel.context.channel_id(),
+                                       user_channel_id: channel.context.get_user_id(),
                                        reason: ClosureReason::DisconnectedPeer,
                                }, None));
                        } else {
-                               log_error!(args.logger, "Missing ChannelMonitor for channel {} needed by ChannelManager.", log_bytes!(channel.channel_id()));
+                               log_error!(args.logger, "Missing ChannelMonitor for channel {} needed by ChannelManager.", log_bytes!(channel.context.channel_id()));
                                log_error!(args.logger, " The chain::Watch API *requires* that monitors are persisted durably before returning,");
                                log_error!(args.logger, " client applications must ensure that ChannelMonitor data is always available and the latest to avoid funds loss!");
                                log_error!(args.logger, " Without the ChannelMonitor we cannot continue without risking funds.");
@@ -7846,7 +8294,7 @@ where
                                        update_id: CLOSED_CHANNEL_UPDATE_ID,
                                        updates: vec![ChannelMonitorUpdateStep::ChannelForceClosed { should_broadcast: true }],
                                };
-                               pending_background_events.push(BackgroundEvent::MonitorUpdateRegeneratedOnStartup((*funding_txo, monitor_update)));
+                               close_background_events.push(BackgroundEvent::ClosingMonitorUpdateRegeneratedOnStartup((*funding_txo, monitor_update)));
                        }
                }
 
@@ -7875,17 +8323,27 @@ where
                        claimable_htlcs_list.push((payment_hash, previous_hops));
                }
 
+               let peer_state_from_chans = |channel_by_id| {
+                       PeerState {
+                               channel_by_id,
+                               outbound_v1_channel_by_id: HashMap::new(),
+                               inbound_v1_channel_by_id: HashMap::new(),
+                               latest_features: InitFeatures::empty(),
+                               pending_msg_events: Vec::new(),
+                               in_flight_monitor_updates: BTreeMap::new(),
+                               monitor_update_blocked_actions: BTreeMap::new(),
+                               actions_blocking_raa_monitor_updates: BTreeMap::new(),
+                               is_connected: false,
+                       }
+               };
+
                let peer_count: u64 = Readable::read(reader)?;
                let mut per_peer_state = HashMap::with_capacity(cmp::min(peer_count as usize, MAX_ALLOC_SIZE/mem::size_of::<(PublicKey, Mutex<PeerState<<SP::Target as SignerProvider>::Signer>>)>()));
                for _ in 0..peer_count {
                        let peer_pubkey = Readable::read(reader)?;
-                       let peer_state = PeerState {
-                               channel_by_id: peer_channels.remove(&peer_pubkey).unwrap_or(HashMap::new()),
-                               latest_features: Readable::read(reader)?,
-                               pending_msg_events: Vec::new(),
-                               monitor_update_blocked_actions: BTreeMap::new(),
-                               is_connected: false,
-                       };
+                       let peer_chans = peer_channels.remove(&peer_pubkey).unwrap_or(HashMap::new());
+                       let mut peer_state = peer_state_from_chans(peer_chans);
+                       peer_state.latest_features = Readable::read(reader)?;
                        per_peer_state.insert(peer_pubkey, Mutex::new(peer_state));
                }
 
@@ -7947,8 +8405,9 @@ where
                let mut claimable_htlc_purposes = None;
                let mut claimable_htlc_onion_fields = None;
                let mut pending_claiming_payments = Some(HashMap::new());
-               let mut monitor_update_blocked_actions_per_peer = Some(Vec::new());
+               let mut monitor_update_blocked_actions_per_peer: Option<Vec<(_, BTreeMap<_, Vec<_>>)>> = Some(Vec::new());
                let mut events_override = None;
+               let mut in_flight_monitor_updates: Option<HashMap<(PublicKey, OutPoint), Vec<ChannelMonitorUpdate>>> = None;
                read_tlv_fields!(reader, {
                        (1, pending_outbound_payments_no_retry, option),
                        (2, pending_intercepted_htlcs, option),
@@ -7959,6 +8418,7 @@ where
                        (7, fake_scid_rand_bytes, option),
                        (8, events_override, option),
                        (9, claimable_htlc_purposes, vec_type),
+                       (10, in_flight_monitor_updates, option),
                        (11, probing_cookie_secret, option),
                        (13, claimable_htlc_onion_fields, optional_vec),
                });
@@ -7992,6 +8452,103 @@ where
                        retry_lock: Mutex::new(())
                };
 
+               // We have to replay (or skip, if they were completed after we wrote the `ChannelManager`)
+               // each `ChannelMonitorUpdate` in `in_flight_monitor_updates`. After doing so, we have to
+               // check that each channel we have isn't newer than the latest `ChannelMonitorUpdate`(s) we
+               // replayed, and for each monitor update we have to replay we have to ensure there's a
+               // `ChannelMonitor` for it.
+               //
+               // In order to do so we first walk all of our live channels (so that we can check their
+               // state immediately after doing the update replays, when we have the `update_id`s
+               // available) and then walk any remaining in-flight updates.
+               //
+               // Because the actual handling of the in-flight updates is the same, it's macro'ized here:
+               let mut pending_background_events = Vec::new();
+               macro_rules! handle_in_flight_updates {
+                       ($counterparty_node_id: expr, $chan_in_flight_upds: expr, $funding_txo: expr,
+                        $monitor: expr, $peer_state: expr, $channel_info_log: expr
+                       ) => { {
+                               let mut max_in_flight_update_id = 0;
+                               $chan_in_flight_upds.retain(|upd| upd.update_id > $monitor.get_latest_update_id());
+                               for update in $chan_in_flight_upds.iter() {
+                                       log_trace!(args.logger, "Replaying ChannelMonitorUpdate {} for {}channel {}",
+                                               update.update_id, $channel_info_log, log_bytes!($funding_txo.to_channel_id()));
+                                       max_in_flight_update_id = cmp::max(max_in_flight_update_id, update.update_id);
+                                       pending_background_events.push(
+                                               BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
+                                                       counterparty_node_id: $counterparty_node_id,
+                                                       funding_txo: $funding_txo,
+                                                       update: update.clone(),
+                                               });
+                               }
+                               if $peer_state.in_flight_monitor_updates.insert($funding_txo, $chan_in_flight_upds).is_some() {
+                                       log_error!(args.logger, "Duplicate in-flight monitor update set for the same channel!");
+                                       return Err(DecodeError::InvalidValue);
+                               }
+                               max_in_flight_update_id
+                       } }
+               }
+
+               for (counterparty_id, peer_state_mtx) in per_peer_state.iter_mut() {
+                       let mut peer_state_lock = peer_state_mtx.lock().unwrap();
+                       let peer_state = &mut *peer_state_lock;
+                       for (_, chan) in peer_state.channel_by_id.iter() {
+                               // Channels that were persisted have to be funded, otherwise they should have been
+                               // discarded.
+                               let funding_txo = chan.context.get_funding_txo().ok_or(DecodeError::InvalidValue)?;
+                               let monitor = args.channel_monitors.get(&funding_txo)
+                                       .expect("We already checked for monitor presence when loading channels");
+                               let mut max_in_flight_update_id = monitor.get_latest_update_id();
+                               if let Some(in_flight_upds) = &mut in_flight_monitor_updates {
+                                       if let Some(mut chan_in_flight_upds) = in_flight_upds.remove(&(*counterparty_id, funding_txo)) {
+                                               max_in_flight_update_id = cmp::max(max_in_flight_update_id,
+                                                       handle_in_flight_updates!(*counterparty_id, chan_in_flight_upds,
+                                                               funding_txo, monitor, peer_state, ""));
+                                       }
+                               }
+                               if chan.get_latest_unblocked_monitor_update_id() > max_in_flight_update_id {
+                                       // If the channel is ahead of the monitor, return InvalidValue:
+                                       log_error!(args.logger, "A ChannelMonitor is stale compared to the current ChannelManager! This indicates a potentially-critical violation of the chain::Watch API!");
+                                       log_error!(args.logger, " The ChannelMonitor for channel {} is at update_id {} with update_id through {} in-flight",
+                                               log_bytes!(chan.context.channel_id()), monitor.get_latest_update_id(), max_in_flight_update_id);
+                                       log_error!(args.logger, " but the ChannelManager is at update_id {}.", chan.get_latest_unblocked_monitor_update_id());
+                                       log_error!(args.logger, " The chain::Watch API *requires* that monitors are persisted durably before returning,");
+                                       log_error!(args.logger, " client applications must ensure that ChannelMonitor data is always available and the latest to avoid funds loss!");
+                                       log_error!(args.logger, " Without the latest ChannelMonitor we cannot continue without risking funds.");
+                                       log_error!(args.logger, " Please ensure the chain::Watch API requirements are met and file a bug report at https://github.com/lightningdevkit/rust-lightning");
+                                       return Err(DecodeError::InvalidValue);
+                               }
+                       }
+               }
+
+               if let Some(in_flight_upds) = in_flight_monitor_updates {
+                       for ((counterparty_id, funding_txo), mut chan_in_flight_updates) in in_flight_upds {
+                               if let Some(monitor) = args.channel_monitors.get(&funding_txo) {
+                                       // Now that we've removed all the in-flight monitor updates for channels that are
+                                       // still open, we need to replay any monitor updates that are for closed channels,
+                                       // creating the neccessary peer_state entries as we go.
+                                       let peer_state_mutex = per_peer_state.entry(counterparty_id).or_insert_with(|| {
+                                               Mutex::new(peer_state_from_chans(HashMap::new()))
+                                       });
+                                       let mut peer_state = peer_state_mutex.lock().unwrap();
+                                       handle_in_flight_updates!(counterparty_id, chan_in_flight_updates,
+                                               funding_txo, monitor, peer_state, "closed ");
+                               } else {
+                                       log_error!(args.logger, "A ChannelMonitor is missing even though we have in-flight updates for it! This indicates a potentially-critical violation of the chain::Watch API!");
+                                       log_error!(args.logger, " The ChannelMonitor for channel {} is missing.",
+                                               log_bytes!(funding_txo.to_channel_id()));
+                                       log_error!(args.logger, " The chain::Watch API *requires* that monitors are persisted durably before returning,");
+                                       log_error!(args.logger, " client applications must ensure that ChannelMonitor data is always available and the latest to avoid funds loss!");
+                                       log_error!(args.logger, " Without the latest ChannelMonitor we cannot continue without risking funds.");
+                                       log_error!(args.logger, " Please ensure the chain::Watch API requirements are met and file a bug report at https://github.com/lightningdevkit/rust-lightning");
+                                       return Err(DecodeError::InvalidValue);
+                               }
+                       }
+               }
+
+               // Note that we have to do the above replays before we push new monitor updates.
+               pending_background_events.append(&mut close_background_events);
+
                {
                        // If we're tracking pending payments, ensure we haven't lost any by looking at the
                        // ChannelMonitor data for any channels for which we do not have authorative state
@@ -8191,25 +8748,25 @@ where
                        let mut peer_state_lock = peer_state_mutex.lock().unwrap();
                        let peer_state = &mut *peer_state_lock;
                        for (chan_id, chan) in peer_state.channel_by_id.iter_mut() {
-                               if chan.outbound_scid_alias() == 0 {
+                               if chan.context.outbound_scid_alias() == 0 {
                                        let mut outbound_scid_alias;
                                        loop {
                                                outbound_scid_alias = fake_scid::Namespace::OutboundAlias
                                                        .get_fake_scid(best_block_height, &genesis_hash, fake_scid_rand_bytes.as_ref().unwrap(), &args.entropy_source);
                                                if outbound_scid_aliases.insert(outbound_scid_alias) { break; }
                                        }
-                                       chan.set_outbound_scid_alias(outbound_scid_alias);
-                               } else if !outbound_scid_aliases.insert(chan.outbound_scid_alias()) {
+                                       chan.context.set_outbound_scid_alias(outbound_scid_alias);
+                               } else if !outbound_scid_aliases.insert(chan.context.outbound_scid_alias()) {
                                        // Note that in rare cases its possible to hit this while reading an older
                                        // channel if we just happened to pick a colliding outbound alias above.
-                                       log_error!(args.logger, "Got duplicate outbound SCID alias; {}", chan.outbound_scid_alias());
+                                       log_error!(args.logger, "Got duplicate outbound SCID alias; {}", chan.context.outbound_scid_alias());
                                        return Err(DecodeError::InvalidValue);
                                }
-                               if chan.is_usable() {
-                                       if short_to_chan_info.insert(chan.outbound_scid_alias(), (chan.get_counterparty_node_id(), *chan_id)).is_some() {
+                               if chan.context.is_usable() {
+                                       if short_to_chan_info.insert(chan.context.outbound_scid_alias(), (chan.context.get_counterparty_node_id(), *chan_id)).is_some() {
                                                // Note that in rare cases its possible to hit this while reading an older
                                                // channel if we just happened to pick a colliding outbound alias above.
-                                               log_error!(args.logger, "Got duplicate outbound SCID alias; {}", chan.outbound_scid_alias());
+                                               log_error!(args.logger, "Got duplicate outbound SCID alias; {}", chan.context.outbound_scid_alias());
                                                return Err(DecodeError::InvalidValue);
                                        }
                                }
@@ -8272,7 +8829,21 @@ where
                }
 
                for (node_id, monitor_update_blocked_actions) in monitor_update_blocked_actions_per_peer.unwrap() {
-                       if let Some(peer_state) = per_peer_state.get_mut(&node_id) {
+                       if let Some(peer_state) = per_peer_state.get(&node_id) {
+                               for (_, actions) in monitor_update_blocked_actions.iter() {
+                                       for action in actions.iter() {
+                                               if let MonitorUpdateCompletionAction::EmitEventAndFreeOtherChannel {
+                                                       downstream_counterparty_and_funding_outpoint:
+                                                               Some((blocked_node_id, blocked_channel_outpoint, blocking_action)), ..
+                                               } = action {
+                                                       if let Some(blocked_peer_state) = per_peer_state.get(&blocked_node_id) {
+                                                               blocked_peer_state.lock().unwrap().actions_blocking_raa_monitor_updates
+                                                                       .entry(blocked_channel_outpoint.to_channel_id())
+                                                                       .or_insert_with(Vec::new).push(blocking_action.clone());
+                                                       }
+                                               }
+                                       }
+                               }
                                peer_state.lock().unwrap().monitor_update_blocked_actions = monitor_update_blocked_actions;
                        } else {
                                log_error!(args.logger, "Got blocked actions without a per-peer-state for {}", node_id);
@@ -8314,6 +8885,8 @@ where
                        pending_events_processor: AtomicBool::new(false),
                        pending_background_events: Mutex::new(pending_background_events),
                        total_consistency_lock: RwLock::new(()),
+                       #[cfg(debug_assertions)]
+                       background_events_processed_since_startup: AtomicBool::new(false),
                        persistence_notifier: Notifier::new(),
 
                        entropy_source: args.entropy_source,
@@ -8348,12 +8921,12 @@ mod tests {
        use crate::ln::{PaymentPreimage, PaymentHash, PaymentSecret};
        use crate::ln::channelmanager::{inbound_payment, PaymentId, PaymentSendFailure, RecipientOnionFields, InterceptId};
        use crate::ln::functional_test_utils::*;
-       use crate::ln::msgs;
+       use crate::ln::msgs::{self, ErrorAction};
        use crate::ln::msgs::ChannelMessageHandler;
        use crate::routing::router::{PaymentParameters, RouteParameters, find_route};
        use crate::util::errors::APIError;
        use crate::util::test_utils;
-       use crate::util::config::ChannelConfig;
+       use crate::util::config::{ChannelConfig, ChannelConfigUpdate};
        use crate::sign::EntropySource;
 
        #[test]
@@ -8562,13 +9135,26 @@ mod tests {
 
        #[test]
        fn test_keysend_dup_payment_hash() {
+               do_test_keysend_dup_payment_hash(false);
+               do_test_keysend_dup_payment_hash(true);
+       }
+
+       fn do_test_keysend_dup_payment_hash(accept_mpp_keysend: bool) {
                // (1): Test that a keysend payment with a duplicate payment hash to an existing pending
                //      outbound regular payment fails as expected.
                // (2): Test that a regular payment with a duplicate payment hash to an existing keysend payment
                //      fails as expected.
+               // (3): Test that a keysend payment with a duplicate payment hash to an existing keysend
+               //      payment fails as expected. When `accept_mpp_keysend` is false, this tests that we
+               //      reject MPP keysend payments, since in this case where the payment has no payment
+               //      secret, a keysend payment with a duplicate hash is basically an MPP keysend. If
+               //      `accept_mpp_keysend` is true, this tests that we only accept MPP keysends with
+               //      payment secrets and reject otherwise.
                let chanmon_cfgs = create_chanmon_cfgs(2);
                let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
-               let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
+               let mut mpp_keysend_cfg = test_default_channel_config();
+               mpp_keysend_cfg.accept_mpp_keysend = accept_mpp_keysend;
+               let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(mpp_keysend_cfg)]);
                let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
                create_announced_chan_between_nodes(&nodes, 0, 1);
                let scorer = test_utils::TestScorer::new();
@@ -8580,7 +9166,7 @@ mod tests {
 
                // Next, attempt a keysend payment and make sure it fails.
                let route_params = RouteParameters {
-                       payment_params: PaymentParameters::for_keysend(expected_route.last().unwrap().node.get_our_node_id(), TEST_FINAL_CLTV),
+                       payment_params: PaymentParameters::for_keysend(expected_route.last().unwrap().node.get_our_node_id(), TEST_FINAL_CLTV, false),
                        final_value_msat: 100_000,
                };
                let route = find_route(
@@ -8657,6 +9243,53 @@ mod tests {
 
                // Finally, succeed the keysend payment.
                claim_payment(&nodes[0], &expected_route, payment_preimage);
+
+               // To start (3), send a keysend payment but don't claim it.
+               let payment_id_1 = PaymentId([44; 32]);
+               let payment_hash = nodes[0].node.send_spontaneous_payment(&route, Some(payment_preimage),
+                       RecipientOnionFields::spontaneous_empty(), payment_id_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 event = events.pop().unwrap();
+               let path = vec![&nodes[1]];
+               pass_along_path(&nodes[0], &path, 100_000, payment_hash, None, event, true, Some(payment_preimage));
+
+               // Next, attempt a keysend payment and make sure it fails.
+               let route_params = RouteParameters {
+                       payment_params: PaymentParameters::for_keysend(expected_route.last().unwrap().node.get_our_node_id(), TEST_FINAL_CLTV, false),
+                       final_value_msat: 100_000,
+               };
+               let route = find_route(
+                       &nodes[0].node.get_our_node_id(), &route_params, &nodes[0].network_graph,
+                       None, nodes[0].logger, &scorer, &(), &random_seed_bytes
+               ).unwrap();
+               let payment_id_2 = PaymentId([45; 32]);
+               nodes[0].node.send_spontaneous_payment(&route, Some(payment_preimage),
+                       RecipientOnionFields::spontaneous_empty(), payment_id_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 ev = events.drain(..).next().unwrap();
+               let payment_event = SendEvent::from_event(ev);
+               nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
+               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_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::FailedPayment { payment_hash }]);
+               check_added_monitors!(nodes[1], 1);
+               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_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[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
+               commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, true, true);
+               expect_payment_failed!(nodes[0], payment_hash, true);
+
+               // Finally, claim the original payment.
+               claim_payment(&nodes[0], &expected_route, payment_preimage);
        }
 
        #[test]
@@ -8673,7 +9306,7 @@ mod tests {
 
                let _chan = create_chan_between_nodes(&nodes[0], &nodes[1]);
                let route_params = RouteParameters {
-                       payment_params: PaymentParameters::for_keysend(payee_pubkey, 40),
+                       payment_params: PaymentParameters::for_keysend(payee_pubkey, 40, false),
                        final_value_msat: 10_000,
                };
                let network_graph = nodes[0].network_graph.clone();
@@ -8706,10 +9339,13 @@ mod tests {
 
        #[test]
        fn test_keysend_msg_with_secret_err() {
-               // Test that we error as expected if we receive a keysend payment that includes a payment secret.
+               // Test that we error as expected if we receive a keysend payment that includes a payment
+               // secret when we don't support MPP keysend.
+               let mut reject_mpp_keysend_cfg = test_default_channel_config();
+               reject_mpp_keysend_cfg.accept_mpp_keysend = false;
                let chanmon_cfgs = create_chanmon_cfgs(2);
                let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
-               let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
+               let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(reject_mpp_keysend_cfg)]);
                let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
 
                let payer_pubkey = nodes[0].node.get_our_node_id();
@@ -8717,7 +9353,7 @@ mod tests {
 
                let _chan = create_chan_between_nodes(&nodes[0], &nodes[1]);
                let route_params = RouteParameters {
-                       payment_params: PaymentParameters::for_keysend(payee_pubkey, 40),
+                       payment_params: PaymentParameters::for_keysend(payee_pubkey, 40, false),
                        final_value_msat: 10_000,
                };
                let network_graph = nodes[0].network_graph.clone();
@@ -9072,12 +9708,14 @@ mod tests {
                                &SecretKey::from_slice(&nodes[1].keys_manager.get_secure_random_bytes()).unwrap());
                        peer_pks.push(random_pk);
                        nodes[1].node.peer_connected(&random_pk, &msgs::Init {
-                               features: nodes[0].node.init_features(), remote_network_address: None }, true).unwrap();
+                               features: nodes[0].node.init_features(), networks: None, remote_network_address: None
+                       }, true).unwrap();
                }
                let last_random_pk = PublicKey::from_secret_key(&nodes[0].node.secp_ctx,
                        &SecretKey::from_slice(&nodes[1].keys_manager.get_secure_random_bytes()).unwrap());
                nodes[1].node.peer_connected(&last_random_pk, &msgs::Init {
-                       features: nodes[0].node.init_features(), remote_network_address: None }, true).unwrap_err();
+                       features: nodes[0].node.init_features(), networks: None, remote_network_address: None
+               }, true).unwrap_err();
 
                // Also importantly, because nodes[0] isn't "protected", we will refuse a reconnection from
                // them if we have too many un-channel'd peers.
@@ -9088,13 +9726,16 @@ mod tests {
                        if let Event::ChannelClosed { .. } = ev { } else { panic!(); }
                }
                nodes[1].node.peer_connected(&last_random_pk, &msgs::Init {
-                       features: nodes[0].node.init_features(), remote_network_address: None }, true).unwrap();
+                       features: nodes[0].node.init_features(), networks: None, remote_network_address: None
+               }, true).unwrap();
                nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
-                       features: nodes[0].node.init_features(), remote_network_address: None }, true).unwrap_err();
+                       features: nodes[0].node.init_features(), networks: None, remote_network_address: None
+               }, true).unwrap_err();
 
                // but of course if the connection is outbound its allowed...
                nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
-                       features: nodes[0].node.init_features(), remote_network_address: None }, false).unwrap();
+                       features: nodes[0].node.init_features(), networks: None, remote_network_address: None
+               }, false).unwrap();
                nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
 
                // Now nodes[0] is disconnected but still has a pending, un-funded channel lying around.
@@ -9118,7 +9759,8 @@ mod tests {
                // "protected" and can connect again.
                mine_transaction(&nodes[1], funding_tx.as_ref().unwrap());
                nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
-                       features: nodes[0].node.init_features(), remote_network_address: None }, true).unwrap();
+                       features: nodes[0].node.init_features(), networks: None, remote_network_address: None
+               }, true).unwrap();
                get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id());
 
                // Further, because the first channel was funded, we can open another channel with
@@ -9183,7 +9825,8 @@ mod tests {
                        let random_pk = PublicKey::from_secret_key(&nodes[0].node.secp_ctx,
                                &SecretKey::from_slice(&nodes[1].keys_manager.get_secure_random_bytes()).unwrap());
                        nodes[1].node.peer_connected(&random_pk, &msgs::Init {
-                               features: nodes[0].node.init_features(), remote_network_address: None }, true).unwrap();
+                               features: nodes[0].node.init_features(), networks: None, remote_network_address: None
+                       }, true).unwrap();
 
                        nodes[1].node.handle_open_channel(&random_pk, &open_channel_msg);
                        let events = nodes[1].node.get_and_clear_pending_events();
@@ -9201,7 +9844,8 @@ mod tests {
                let last_random_pk = PublicKey::from_secret_key(&nodes[0].node.secp_ctx,
                        &SecretKey::from_slice(&nodes[1].keys_manager.get_secure_random_bytes()).unwrap());
                nodes[1].node.peer_connected(&last_random_pk, &msgs::Init {
-                       features: nodes[0].node.init_features(), remote_network_address: None }, true).unwrap();
+                       features: nodes[0].node.init_features(), networks: None, remote_network_address: None
+               }, true).unwrap();
                nodes[1].node.handle_open_channel(&last_random_pk, &open_channel_msg);
                let events = nodes[1].node.get_and_clear_pending_events();
                match events[0] {
@@ -9229,7 +9873,94 @@ mod tests {
                get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, last_random_pk);
        }
 
-       #[cfg(anchors)]
+       #[test]
+       fn reject_excessively_underpaying_htlcs() {
+               let chanmon_cfg = create_chanmon_cfgs(1);
+               let node_cfg = create_node_cfgs(1, &chanmon_cfg);
+               let node_chanmgr = create_node_chanmgrs(1, &node_cfg, &[None]);
+               let node = create_network(1, &node_cfg, &node_chanmgr);
+               let sender_intended_amt_msat = 100;
+               let extra_fee_msat = 10;
+               let hop_data = msgs::OnionHopData {
+                       amt_to_forward: 100,
+                       outgoing_cltv_value: 42,
+                       format: msgs::OnionHopDataFormat::FinalNode {
+                               keysend_preimage: None,
+                               payment_metadata: None,
+                               payment_data: Some(msgs::FinalOnionHopData {
+                                       payment_secret: PaymentSecret([0; 32]), total_msat: sender_intended_amt_msat,
+                               }),
+                       }
+               };
+               // Check that if the amount we received + the penultimate hop extra fee is less than the sender
+               // intended amount, we fail the payment.
+               if let Err(crate::ln::channelmanager::ReceiveError { err_code, .. }) =
+                       node[0].node.construct_recv_pending_htlc_info(hop_data, [0; 32], PaymentHash([0; 32]),
+                               sender_intended_amt_msat - extra_fee_msat - 1, 42, None, true, Some(extra_fee_msat))
+               {
+                       assert_eq!(err_code, 19);
+               } else { panic!(); }
+
+               // If amt_received + extra_fee is equal to the sender intended amount, we're fine.
+               let hop_data = msgs::OnionHopData { // This is the same hop_data as above, OnionHopData doesn't implement Clone
+                       amt_to_forward: 100,
+                       outgoing_cltv_value: 42,
+                       format: msgs::OnionHopDataFormat::FinalNode {
+                               keysend_preimage: None,
+                               payment_metadata: None,
+                               payment_data: Some(msgs::FinalOnionHopData {
+                                       payment_secret: PaymentSecret([0; 32]), total_msat: sender_intended_amt_msat,
+                               }),
+                       }
+               };
+               assert!(node[0].node.construct_recv_pending_htlc_info(hop_data, [0; 32], PaymentHash([0; 32]),
+                       sender_intended_amt_msat - extra_fee_msat, 42, None, true, Some(extra_fee_msat)).is_ok());
+       }
+
+       #[test]
+       fn test_inbound_anchors_manual_acceptance() {
+               // Tests that we properly limit inbound channels when we have the manual-channel-acceptance
+               // flag set and (sometimes) accept channels as 0conf.
+               let mut anchors_cfg = test_default_channel_config();
+               anchors_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
+
+               let mut anchors_manual_accept_cfg = anchors_cfg.clone();
+               anchors_manual_accept_cfg.manually_accept_inbound_channels = true;
+
+               let chanmon_cfgs = create_chanmon_cfgs(3);
+               let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
+               let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs,
+                       &[Some(anchors_cfg.clone()), Some(anchors_cfg.clone()), Some(anchors_manual_accept_cfg.clone())]);
+               let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
+
+               nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 42, None).unwrap();
+               let open_channel_msg = 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(), &open_channel_msg);
+               assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
+               let msg_events = nodes[1].node.get_and_clear_pending_msg_events();
+               match &msg_events[0] {
+                       MessageSendEvent::HandleError { node_id, action } => {
+                               assert_eq!(*node_id, nodes[0].node.get_our_node_id());
+                               match action {
+                                       ErrorAction::SendErrorMessage { msg } =>
+                                               assert_eq!(msg.data, "No channels with anchor outputs accepted".to_owned()),
+                                       _ => panic!("Unexpected error action"),
+                               }
+                       }
+                       _ => panic!("Unexpected event"),
+               }
+
+               nodes[2].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_msg);
+               let events = nodes[2].node.get_and_clear_pending_events();
+               match events[0] {
+                       Event::OpenChannelRequest { temporary_channel_id, .. } =>
+                               nodes[2].node.accept_inbound_channel(&temporary_channel_id, &nodes[0].node.get_our_node_id(), 23).unwrap(),
+                       _ => panic!("Unexpected event"),
+               }
+               get_event_msg!(nodes[2], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
+       }
+
        #[test]
        fn test_anchors_zero_fee_htlc_tx_fallback() {
                // Tests that if both nodes support anchors, but the remote node does not want to accept
@@ -9264,9 +9995,65 @@ mod tests {
 
                check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed);
        }
+
+       #[test]
+       fn test_update_channel_config() {
+               let chanmon_cfg = create_chanmon_cfgs(2);
+               let node_cfg = create_node_cfgs(2, &chanmon_cfg);
+               let mut user_config = test_default_channel_config();
+               let node_chanmgr = create_node_chanmgrs(2, &node_cfg, &[Some(user_config), Some(user_config)]);
+               let nodes = create_network(2, &node_cfg, &node_chanmgr);
+               let _ = create_announced_chan_between_nodes(&nodes, 0, 1);
+               let channel = &nodes[0].node.list_channels()[0];
+
+               nodes[0].node.update_channel_config(&channel.counterparty.node_id, &[channel.channel_id], &user_config.channel_config).unwrap();
+               let events = nodes[0].node.get_and_clear_pending_msg_events();
+               assert_eq!(events.len(), 0);
+
+               user_config.channel_config.forwarding_fee_base_msat += 10;
+               nodes[0].node.update_channel_config(&channel.counterparty.node_id, &[channel.channel_id], &user_config.channel_config).unwrap();
+               assert_eq!(nodes[0].node.list_channels()[0].config.unwrap().forwarding_fee_base_msat, user_config.channel_config.forwarding_fee_base_msat);
+               let events = nodes[0].node.get_and_clear_pending_msg_events();
+               assert_eq!(events.len(), 1);
+               match &events[0] {
+                       MessageSendEvent::BroadcastChannelUpdate { .. } => {},
+                       _ => panic!("expected BroadcastChannelUpdate event"),
+               }
+
+               nodes[0].node.update_partial_channel_config(&channel.counterparty.node_id, &[channel.channel_id], &ChannelConfigUpdate::default()).unwrap();
+               let events = nodes[0].node.get_and_clear_pending_msg_events();
+               assert_eq!(events.len(), 0);
+
+               let new_cltv_expiry_delta = user_config.channel_config.cltv_expiry_delta + 6;
+               nodes[0].node.update_partial_channel_config(&channel.counterparty.node_id, &[channel.channel_id], &ChannelConfigUpdate {
+                       cltv_expiry_delta: Some(new_cltv_expiry_delta),
+                       ..Default::default()
+               }).unwrap();
+               assert_eq!(nodes[0].node.list_channels()[0].config.unwrap().cltv_expiry_delta, new_cltv_expiry_delta);
+               let events = nodes[0].node.get_and_clear_pending_msg_events();
+               assert_eq!(events.len(), 1);
+               match &events[0] {
+                       MessageSendEvent::BroadcastChannelUpdate { .. } => {},
+                       _ => panic!("expected BroadcastChannelUpdate event"),
+               }
+
+               let new_fee = user_config.channel_config.forwarding_fee_proportional_millionths + 100;
+               nodes[0].node.update_partial_channel_config(&channel.counterparty.node_id, &[channel.channel_id], &ChannelConfigUpdate {
+                       forwarding_fee_proportional_millionths: Some(new_fee),
+                       ..Default::default()
+               }).unwrap();
+               assert_eq!(nodes[0].node.list_channels()[0].config.unwrap().cltv_expiry_delta, new_cltv_expiry_delta);
+               assert_eq!(nodes[0].node.list_channels()[0].config.unwrap().forwarding_fee_proportional_millionths, new_fee);
+               let events = nodes[0].node.get_and_clear_pending_msg_events();
+               assert_eq!(events.len(), 1);
+               match &events[0] {
+                       MessageSendEvent::BroadcastChannelUpdate { .. } => {},
+                       _ => panic!("expected BroadcastChannelUpdate event"),
+               }
+       }
 }
 
-#[cfg(all(any(test, feature = "_test_utils"), feature = "_bench_unstable"))]
+#[cfg(ldk_bench)]
 pub mod bench {
        use crate::chain::Listen;
        use crate::chain::chainmonitor::{ChainMonitor, Persist};
@@ -9278,7 +10065,7 @@ pub mod bench {
        use crate::routing::gossip::NetworkGraph;
        use crate::routing::router::{PaymentParameters, RouteParameters};
        use crate::util::test_utils;
-       use crate::util::config::UserConfig;
+       use crate::util::config::{UserConfig, MaxDustHTLCExposure};
 
        use bitcoin::hashes::Hash;
        use bitcoin::hashes::sha256::Hash as Sha256;
@@ -9286,7 +10073,7 @@ pub mod bench {
 
        use crate::sync::{Arc, Mutex};
 
-       use test::Bencher;
+       use criterion::Criterion;
 
        type Manager<'a, P> = ChannelManager<
                &'a ChainMonitor<InMemorySigner, &'a test_utils::TestChainSource,
@@ -9307,17 +10094,16 @@ pub mod bench {
                fn chain_monitor(&self) -> Option<&test_utils::TestChainMonitor> { None }
        }
 
-       #[cfg(test)]
-       #[bench]
-       fn bench_sends(bench: &mut Bencher) {
-               bench_two_sends(bench, test_utils::TestPersister::new(), test_utils::TestPersister::new());
+       pub fn bench_sends(bench: &mut Criterion) {
+               bench_two_sends(bench, "bench_sends", test_utils::TestPersister::new(), test_utils::TestPersister::new());
        }
 
-       pub fn bench_two_sends<P: Persist<InMemorySigner>>(bench: &mut Bencher, persister_a: P, persister_b: P) {
+       pub fn bench_two_sends<P: Persist<InMemorySigner>>(bench: &mut Criterion, bench_name: &str, persister_a: P, persister_b: P) {
                // Do a simple benchmark of sending a payment back and forth between two nodes.
                // Note that this is unrealistic as each payment send will require at least two fsync
                // calls per node.
                let network = bitcoin::Network::Testnet;
+               let genesis_block = bitcoin::blockdata::constants::genesis_block(network);
 
                let tx_broadcaster = test_utils::TestBroadcaster::new(network);
                let fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) };
@@ -9326,6 +10112,7 @@ pub mod bench {
                let router = test_utils::TestRouter::new(Arc::new(NetworkGraph::new(network, &logger_a)), &scorer);
 
                let mut config: UserConfig = Default::default();
+               config.channel_config.max_dust_htlc_exposure = MaxDustHTLCExposure::FeeRateMultiplier(5_000_000 / 253);
                config.channel_handshake_config.minimum_depth = 1;
 
                let chain_monitor_a = ChainMonitor::new(None, &tx_broadcaster, &logger_a, &fee_estimator, &persister_a);
@@ -9334,7 +10121,7 @@ pub mod bench {
                let node_a = ChannelManager::new(&fee_estimator, &chain_monitor_a, &tx_broadcaster, &router, &logger_a, &keys_manager_a, &keys_manager_a, &keys_manager_a, config.clone(), ChainParameters {
                        network,
                        best_block: BestBlock::from_network(network),
-               });
+               }, genesis_block.header.time);
                let node_a_holder = ANodeHolder { node: &node_a };
 
                let logger_b = test_utils::TestLogger::with_id("node a".to_owned());
@@ -9344,11 +10131,15 @@ pub mod bench {
                let node_b = ChannelManager::new(&fee_estimator, &chain_monitor_b, &tx_broadcaster, &router, &logger_b, &keys_manager_b, &keys_manager_b, &keys_manager_b, config.clone(), ChainParameters {
                        network,
                        best_block: BestBlock::from_network(network),
-               });
+               }, genesis_block.header.time);
                let node_b_holder = ANodeHolder { node: &node_b };
 
-               node_a.peer_connected(&node_b.get_our_node_id(), &Init { features: node_b.init_features(), remote_network_address: None }, true).unwrap();
-               node_b.peer_connected(&node_a.get_our_node_id(), &Init { features: node_a.init_features(), remote_network_address: None }, false).unwrap();
+               node_a.peer_connected(&node_b.get_our_node_id(), &Init {
+                       features: node_b.init_features(), networks: None, remote_network_address: None
+               }, true).unwrap();
+               node_b.peer_connected(&node_a.get_our_node_id(), &Init {
+                       features: node_a.init_features(), networks: None, remote_network_address: None
+               }, false).unwrap();
                node_a.create_channel(node_b.get_our_node_id(), 8_000_000, 100_000_000, 42, None).unwrap();
                node_b.handle_open_channel(&node_a.get_our_node_id(), &get_event_msg!(node_a_holder, MessageSendEvent::SendOpenChannel, node_b.get_our_node_id()));
                node_a.handle_accept_channel(&node_b.get_our_node_id(), &get_event_msg!(node_b_holder, MessageSendEvent::SendAcceptChannel, node_a.get_our_node_id()));
@@ -9466,9 +10257,9 @@ pub mod bench {
                        }
                }
 
-               bench.iter(|| {
+               bench.bench_function(bench_name, |b| b.iter(|| {
                        send_payment!(node_a, node_b);
                        send_payment!(node_b, node_a);
-               });
+               }));
        }
 }
index b8087546c7445060230a5ad776a9b3f13c113d37..ca6ea70b61da7312de22b1e28749a51cb3f28941 100644 (file)
 //!     [BOLT-3](https://github.com/lightning/bolts/blob/master/03-transactions.md) for more
 //!     information).
 //!
+//! LDK knows about the following features, but does not support them:
+//! - `AnchorsNonzeroFeeHtlcTx` - the initial version of anchor outputs, which was later found to be
+//!     vulnerable (see this
+//!     [mailing list post](https://lists.linuxfoundation.org/pipermail/lightning-dev/2020-September/002796.html)
+//!     for more information).
+//!
 //! [BOLT #9]: https://github.com/lightning/bolts/blob/master/09-features.md
 //! [messages]: crate::ln::msgs
 
 use crate::{io, io_extras};
 use crate::prelude::*;
 use core::{cmp, fmt};
+use core::borrow::Borrow;
 use core::hash::{Hash, Hasher};
 use core::marker::PhantomData;
 
@@ -134,7 +141,7 @@ mod sealed {
                // Byte 1
                VariableLengthOnion | StaticRemoteKey | PaymentSecret,
                // Byte 2
-               BasicMPP | Wumbo | AnchorsZeroFeeHtlcTx,
+               BasicMPP | Wumbo | AnchorsNonzeroFeeHtlcTx | AnchorsZeroFeeHtlcTx,
                // Byte 3
                ShutdownAnySegwit,
                // Byte 4
@@ -150,7 +157,7 @@ mod sealed {
                // Byte 1
                VariableLengthOnion | StaticRemoteKey | PaymentSecret,
                // Byte 2
-               BasicMPP | Wumbo | AnchorsZeroFeeHtlcTx,
+               BasicMPP | Wumbo | AnchorsNonzeroFeeHtlcTx | AnchorsZeroFeeHtlcTx,
                // Byte 3
                ShutdownAnySegwit,
                // Byte 4
@@ -196,7 +203,7 @@ mod sealed {
                // Byte 1
                StaticRemoteKey,
                // Byte 2
-               AnchorsZeroFeeHtlcTx,
+               AnchorsNonzeroFeeHtlcTx | AnchorsZeroFeeHtlcTx,
                // Byte 3
                ,
                // Byte 4
@@ -378,6 +385,9 @@ mod sealed {
        define_feature!(19, Wumbo, [InitContext, NodeContext],
                "Feature flags for `option_support_large_channel` (aka wumbo channels).", set_wumbo_optional, set_wumbo_required,
                supports_wumbo, requires_wumbo);
+       define_feature!(21, AnchorsNonzeroFeeHtlcTx, [InitContext, NodeContext, ChannelTypeContext],
+               "Feature flags for `option_anchors_nonzero_fee_htlc_tx`.", set_anchors_nonzero_fee_htlc_tx_optional,
+               set_anchors_nonzero_fee_htlc_tx_required, supports_anchors_nonzero_fee_htlc_tx, requires_anchors_nonzero_fee_htlc_tx);
        define_feature!(23, AnchorsZeroFeeHtlcTx, [InitContext, NodeContext, ChannelTypeContext],
                "Feature flags for `option_anchors_zero_fee_htlc_tx`.", set_anchors_zero_fee_htlc_tx_optional,
                set_anchors_zero_fee_htlc_tx_required, supports_anchors_zero_fee_htlc_tx, requires_anchors_zero_fee_htlc_tx);
@@ -422,15 +432,21 @@ pub struct Features<T: sealed::Context> {
        mark: PhantomData<T>,
 }
 
+impl<T: sealed::Context, Rhs: Borrow<Self>> core::ops::BitOrAssign<Rhs> for Features<T> {
+       fn bitor_assign(&mut self, rhs: Rhs) {
+               let total_feature_len = cmp::max(self.flags.len(), rhs.borrow().flags.len());
+               self.flags.resize(total_feature_len, 0u8);
+               for (byte, rhs_byte) in self.flags.iter_mut().zip(rhs.borrow().flags.iter()) {
+                       *byte |= *rhs_byte;
+               }
+       }
+}
+
 impl<T: sealed::Context> core::ops::BitOr for Features<T> {
        type Output = Self;
 
        fn bitor(mut self, o: Self) -> Self {
-               let total_feature_len = cmp::max(self.flags.len(), o.flags.len());
-               self.flags.resize(total_feature_len, 0u8);
-               for (byte, o_byte) in self.flags.iter_mut().zip(o.flags.iter()) {
-                       *byte |= *o_byte;
-               }
+               self |= o;
                self
        }
 }
@@ -535,11 +551,17 @@ impl InvoiceFeatures {
        /// [`PaymentParameters::for_keysend`], thus omitting the need for payers to manually construct an
        /// `InvoiceFeatures` for [`find_route`].
        ///
+       /// MPP keysend is not widely supported yet, so we parameterize support to allow the user to
+       /// choose whether their router should find multi-part routes.
+       ///
        /// [`PaymentParameters::for_keysend`]: crate::routing::router::PaymentParameters::for_keysend
        /// [`find_route`]: crate::routing::router::find_route
-       pub(crate) fn for_keysend() -> InvoiceFeatures {
+       pub(crate) fn for_keysend(allow_mpp: bool) -> InvoiceFeatures {
                let mut res = InvoiceFeatures::empty();
                res.set_variable_length_onion_optional();
+               if allow_mpp {
+                       res.set_basic_mpp_optional();
+               }
                res
        }
 }
@@ -572,6 +594,14 @@ impl ChannelTypeFeatures {
                <sealed::ChannelTypeContext as sealed::StaticRemoteKey>::set_required_bit(&mut ret.flags);
                ret
        }
+
+       /// Constructs a ChannelTypeFeatures with anchors support
+       pub(crate) fn anchors_zero_htlc_fee_and_dependencies() -> Self {
+               let mut ret = Self::empty();
+               <sealed::ChannelTypeContext as sealed::StaticRemoteKey>::set_required_bit(&mut ret.flags);
+               <sealed::ChannelTypeContext as sealed::AnchorsZeroFeeHtlcTx>::set_required_bit(&mut ret.flags);
+               ret
+       }
 }
 
 impl ToBase32 for InvoiceFeatures {
index fa8bdcc58be01ef57547cbf9beaf548be38ca474..694e8d7a7d9c5756c0f02ad7d2a8ea8b255ad1e4 100644 (file)
@@ -27,7 +27,7 @@ use crate::util::scid_utils;
 use crate::util::test_utils;
 use crate::util::test_utils::{panicking, TestChainMonitor, TestScorer, TestKeysInterface};
 use crate::util::errors::APIError;
-use crate::util::config::UserConfig;
+use crate::util::config::{UserConfig, MaxDustHTLCExposure};
 use crate::util::ser::{ReadableArgs, Writeable};
 
 use bitcoin::blockdata::block::{Block, BlockHeader};
@@ -783,6 +783,28 @@ macro_rules! get_channel_ref {
        }
 }
 
+#[cfg(test)]
+macro_rules! get_outbound_v1_channel_ref {
+       ($node: expr, $counterparty_node: expr, $per_peer_state_lock: ident, $peer_state_lock: ident, $channel_id: expr) => {
+               {
+                       $per_peer_state_lock = $node.node.per_peer_state.read().unwrap();
+                       $peer_state_lock = $per_peer_state_lock.get(&$counterparty_node.node.get_our_node_id()).unwrap().lock().unwrap();
+                       $peer_state_lock.outbound_v1_channel_by_id.get_mut(&$channel_id).unwrap()
+               }
+       }
+}
+
+#[cfg(test)]
+macro_rules! get_inbound_v1_channel_ref {
+       ($node: expr, $counterparty_node: expr, $per_peer_state_lock: ident, $peer_state_lock: ident, $channel_id: expr) => {
+               {
+                       $per_peer_state_lock = $node.node.per_peer_state.read().unwrap();
+                       $peer_state_lock = $per_peer_state_lock.get(&$counterparty_node.node.get_our_node_id()).unwrap().lock().unwrap();
+                       $peer_state_lock.inbound_v1_channel_by_id.get_mut(&$channel_id).unwrap()
+               }
+       }
+}
+
 #[cfg(test)]
 macro_rules! get_feerate {
        ($node: expr, $counterparty_node: expr, $channel_id: expr) => {
@@ -790,19 +812,19 @@ macro_rules! get_feerate {
                        let mut per_peer_state_lock;
                        let mut peer_state_lock;
                        let chan = get_channel_ref!($node, $counterparty_node, per_peer_state_lock, peer_state_lock, $channel_id);
-                       chan.get_feerate_sat_per_1000_weight()
+                       chan.context.get_feerate_sat_per_1000_weight()
                }
        }
 }
 
 #[cfg(test)]
-macro_rules! get_opt_anchors {
+macro_rules! get_channel_type_features {
        ($node: expr, $counterparty_node: expr, $channel_id: expr) => {
                {
                        let mut per_peer_state_lock;
                        let mut peer_state_lock;
                        let chan = get_channel_ref!($node, $counterparty_node, per_peer_state_lock, peer_state_lock, $channel_id);
-                       chan.opt_anchors()
+                       chan.context.get_channel_type().clone()
                }
        }
 }
@@ -1078,6 +1100,15 @@ pub fn create_chan_between_nodes_with_value_init<'a, 'b, 'c>(node_a: &Node<'a, '
        assert_eq!(open_channel_msg.temporary_channel_id, create_chan_id);
        assert_eq!(node_a.node.list_channels().iter().find(|channel| channel.channel_id == create_chan_id).unwrap().user_channel_id, 42);
        node_b.node.handle_open_channel(&node_a.node.get_our_node_id(), &open_channel_msg);
+       if node_b.node.get_current_default_configuration().manually_accept_inbound_channels {
+               let events = node_b.node.get_and_clear_pending_events();
+               assert_eq!(events.len(), 1);
+               match &events[0] {
+                       Event::OpenChannelRequest { temporary_channel_id, counterparty_node_id, .. } =>
+                               node_b.node.accept_inbound_channel(temporary_channel_id, counterparty_node_id, 42).unwrap(),
+                       _ => panic!("Unexpected event"),
+               };
+       }
        let accept_channel_msg = get_event_msg!(node_b, MessageSendEvent::SendAcceptChannel, node_a.node.get_our_node_id());
        assert_eq!(accept_channel_msg.temporary_channel_id, create_chan_id);
        node_a.node.handle_accept_channel(&node_b.node.get_our_node_id(), &accept_channel_msg);
@@ -1776,7 +1807,7 @@ macro_rules! get_route_and_payment_hash {
 }
 
 #[macro_export]
-#[cfg(any(test, feature = "_bench_unstable", feature = "_test_utils"))]
+#[cfg(any(test, ldk_bench, feature = "_test_utils"))]
 macro_rules! expect_payment_claimable {
        ($node: expr, $expected_payment_hash: expr, $expected_payment_secret: expr, $expected_recv_value: expr) => {
                expect_payment_claimable!($node, $expected_payment_hash, $expected_payment_secret, $expected_recv_value, None, $node.node.get_our_node_id())
@@ -1803,7 +1834,7 @@ macro_rules! expect_payment_claimable {
 }
 
 #[macro_export]
-#[cfg(any(test, feature = "_bench_unstable", feature = "_test_utils"))]
+#[cfg(any(test, ldk_bench, feature = "_test_utils"))]
 macro_rules! expect_payment_claimed {
        ($node: expr, $expected_payment_hash: expr, $expected_recv_value: expr) => {
                let events = $node.node.get_and_clear_pending_events();
@@ -1920,7 +1951,17 @@ macro_rules! expect_payment_forwarded {
        }
 }
 
-#[cfg(any(test, feature = "_bench_unstable", feature = "_test_utils"))]
+#[cfg(test)]
+#[macro_export]
+macro_rules! expect_channel_shutdown_state {
+       ($node: expr, $chan_id: expr, $state: path) => {
+               let chan_details = $node.node.list_channels().into_iter().filter(|cd| cd.channel_id == $chan_id).collect::<Vec<ChannelDetails>>();
+               assert_eq!(chan_details.len(), 1);
+               assert_eq!(chan_details[0].channel_shutdown_state, Some($state));
+       }
+}
+
+#[cfg(any(test, ldk_bench, feature = "_test_utils"))]
 pub fn expect_channel_pending_event<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, expected_counterparty_node_id: &PublicKey) {
        let events = node.node.get_and_clear_pending_events();
        assert_eq!(events.len(), 1);
@@ -1932,7 +1973,7 @@ pub fn expect_channel_pending_event<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>,
        }
 }
 
-#[cfg(any(test, feature = "_bench_unstable", feature = "_test_utils"))]
+#[cfg(any(test, ldk_bench, feature = "_test_utils"))]
 pub fn expect_channel_ready_event<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, expected_counterparty_node_id: &PublicKey) {
        let events = node.node.get_and_clear_pending_events();
        assert_eq!(events.len(), 1);
@@ -2102,7 +2143,7 @@ pub fn do_pass_along_path<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_p
                                match &events_2[0] {
                                        Event::PaymentClaimable { ref payment_hash, ref purpose, amount_msat,
                                                receiver_node_id, ref via_channel_id, ref via_user_channel_id,
-                                               claim_deadline, onion_fields,
+                                               claim_deadline, onion_fields, ..
                                        } => {
                                                assert_eq!(our_payment_hash, *payment_hash);
                                                assert_eq!(node.node.get_our_node_id(), receiver_node_id.unwrap());
@@ -2115,7 +2156,7 @@ pub fn do_pass_along_path<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_p
                                                        },
                                                        PaymentPurpose::SpontaneousPayment(payment_preimage) => {
                                                                assert_eq!(expected_preimage.unwrap(), *payment_preimage);
-                                                               assert!(our_payment_secret.is_none());
+                                                               assert_eq!(our_payment_secret, onion_fields.as_ref().unwrap().payment_secret);
                                                        },
                                                }
                                                assert_eq!(*amount_msat, recv_value);
@@ -2164,7 +2205,20 @@ pub fn send_along_route<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, route: Route
        (our_payment_preimage, our_payment_hash, our_payment_secret, payment_id)
 }
 
-pub fn do_claim_payment_along_route<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_paths: &[&[&Node<'a, 'b, 'c>]], skip_last: bool, our_payment_preimage: PaymentPreimage) -> u64 {
+pub fn do_claim_payment_along_route<'a, 'b, 'c>(
+       origin_node: &Node<'a, 'b, 'c>, expected_paths: &[&[&Node<'a, 'b, 'c>]], skip_last: bool,
+       our_payment_preimage: PaymentPreimage
+) -> u64 {
+       let extra_fees = vec![0; expected_paths.len()];
+       do_claim_payment_along_route_with_extra_penultimate_hop_fees(origin_node, expected_paths,
+               &extra_fees[..], skip_last, our_payment_preimage)
+}
+
+pub fn do_claim_payment_along_route_with_extra_penultimate_hop_fees<'a, 'b, 'c>(
+       origin_node: &Node<'a, 'b, 'c>, expected_paths: &[&[&Node<'a, 'b, 'c>]], expected_extra_fees:
+       &[u32], skip_last: bool, our_payment_preimage: PaymentPreimage
+) -> u64 {
+       assert_eq!(expected_paths.len(), expected_extra_fees.len());
        for path in expected_paths.iter() {
                assert_eq!(path.last().unwrap().node.get_our_node_id(), expected_paths[0].last().unwrap().node.get_our_node_id());
        }
@@ -2214,7 +2268,7 @@ pub fn do_claim_payment_along_route<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>,
                }
        }
 
-       for (expected_route, (path_msgs, next_hop)) in expected_paths.iter().zip(per_path_msgs.drain(..)) {
+       for (i, (expected_route, (path_msgs, next_hop))) in expected_paths.iter().zip(per_path_msgs.drain(..)).enumerate() {
                let mut next_msgs = Some(path_msgs);
                let mut expected_next_node = next_hop;
 
@@ -2229,20 +2283,21 @@ pub fn do_claim_payment_along_route<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>,
                        }
                }
                macro_rules! mid_update_fulfill_dance {
-                       ($node: expr, $prev_node: expr, $next_node: expr, $new_msgs: expr) => {
+                       ($idx: expr, $node: expr, $prev_node: expr, $next_node: expr, $new_msgs: expr) => {
                                {
                                        $node.node.handle_update_fulfill_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0);
-                                       let fee = {
+                                       let mut fee = {
                                                let per_peer_state = $node.node.per_peer_state.read().unwrap();
                                                let peer_state = per_peer_state.get(&$prev_node.node.get_our_node_id())
                                                        .unwrap().lock().unwrap();
                                                let channel = peer_state.channel_by_id.get(&next_msgs.as_ref().unwrap().0.channel_id).unwrap();
-                                               if let Some(prev_config) = channel.prev_config() {
+                                               if let Some(prev_config) = channel.context.prev_config() {
                                                        prev_config.forwarding_fee_base_msat
                                                } else {
-                                                       channel.config().forwarding_fee_base_msat
+                                                       channel.context.config().forwarding_fee_base_msat
                                                }
                                        };
+                                       if $idx == 1 { fee += expected_extra_fees[i]; }
                                        expect_payment_forwarded!($node, $next_node, $prev_node, Some(fee as u64), false, false);
                                        expected_total_fee_msat += fee as u64;
                                        check_added_monitors!($node, 1);
@@ -2274,7 +2329,7 @@ pub fn do_claim_payment_along_route<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>,
                                } else {
                                        next_node = expected_route[expected_route.len() - 1 - idx - 1];
                                }
-                               mid_update_fulfill_dance!(node, prev_node, next_node, update_next_msgs);
+                               mid_update_fulfill_dance!(idx, node, prev_node, next_node, update_next_msgs);
                        } else {
                                assert!(!update_next_msgs);
                                assert!(node.node.get_and_clear_pending_msg_events().is_empty());
@@ -2527,8 +2582,10 @@ pub fn test_default_channel_config() -> UserConfig {
        // It now defaults to 1, so we simply set it to the expected value here.
        default_config.channel_handshake_config.our_htlc_minimum_msat = 1000;
        // When most of our tests were written, we didn't have the notion of a `max_dust_htlc_exposure_msat`,
-       // It now defaults to 5_000_000 msat; to avoid interfering with tests we bump it to 50_000_000 msat.
-       default_config.channel_config.max_dust_htlc_exposure_msat = 50_000_000;
+       // to avoid interfering with tests we bump it to 50_000_000 msat (assuming the default test
+       // feerate of 253).
+       default_config.channel_config.max_dust_htlc_exposure =
+               MaxDustHTLCExposure::FeeRateMultiplier(50_000_000 / 253);
        default_config
 }
 
@@ -2536,12 +2593,13 @@ pub fn create_node_chanmgrs<'a, 'b>(node_count: usize, cfgs: &'a Vec<NodeCfg<'b>
        let mut chanmgrs = Vec::new();
        for i in 0..node_count {
                let network = Network::Testnet;
+               let genesis_block = bitcoin::blockdata::constants::genesis_block(network);
                let params = ChainParameters {
                        network,
                        best_block: BestBlock::from_network(network),
                };
                let node = ChannelManager::new(cfgs[i].fee_estimator, &cfgs[i].chain_monitor, cfgs[i].tx_broadcaster, &cfgs[i].router, cfgs[i].logger, cfgs[i].keys_manager,
-                       cfgs[i].keys_manager, cfgs[i].keys_manager, if node_config[i].is_some() { node_config[i].clone().unwrap() } else { test_default_channel_config() }, params);
+                       cfgs[i].keys_manager, cfgs[i].keys_manager, if node_config[i].is_some() { node_config[i].clone().unwrap() } else { test_default_channel_config() }, params, genesis_block.header.time);
                chanmgrs.push(node);
        }
 
@@ -2571,8 +2629,16 @@ pub fn create_network<'a, 'b: 'a, 'c: 'b>(node_count: usize, cfgs: &'b Vec<NodeC
 
        for i in 0..node_count {
                for j in (i+1)..node_count {
-                       nodes[i].node.peer_connected(&nodes[j].node.get_our_node_id(), &msgs::Init { features: nodes[j].override_init_features.borrow().clone().unwrap_or_else(|| nodes[j].node.init_features()), remote_network_address: None }, true).unwrap();
-                       nodes[j].node.peer_connected(&nodes[i].node.get_our_node_id(), &msgs::Init { features: nodes[i].override_init_features.borrow().clone().unwrap_or_else(|| nodes[i].node.init_features()), remote_network_address: None }, false).unwrap();
+                       nodes[i].node.peer_connected(&nodes[j].node.get_our_node_id(), &msgs::Init {
+                               features: nodes[j].override_init_features.borrow().clone().unwrap_or_else(|| nodes[j].node.init_features()),
+                               networks: None,
+                               remote_network_address: None,
+                       }, true).unwrap();
+                       nodes[j].node.peer_connected(&nodes[i].node.get_our_node_id(), &msgs::Init {
+                               features: nodes[i].override_init_features.borrow().clone().unwrap_or_else(|| nodes[i].node.init_features()),
+                               networks: None,
+                               remote_network_address: None,
+                       }, false).unwrap();
                }
        }
 
@@ -2856,9 +2922,13 @@ macro_rules! handle_chan_reestablish_msgs {
 /// 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<'a, 'b, 'c>(node_a: &Node<'a, 'b, 'c>, node_b: &Node<'a, 'b, 'c>, send_channel_ready: (bool, bool), pending_htlc_adds: (i64, i64), pending_htlc_claims: (usize, usize), pending_htlc_fails: (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(), &msgs::Init { features: node_b.node.init_features(), remote_network_address: None }, true).unwrap();
+       node_a.node.peer_connected(&node_b.node.get_our_node_id(), &msgs::Init {
+               features: node_b.node.init_features(), networks: None, remote_network_address: None
+       }, true).unwrap();
        let reestablish_1 = get_chan_reestablish_msgs!(node_a, node_b);
-       node_b.node.peer_connected(&node_a.node.get_our_node_id(), &msgs::Init { features: node_a.node.init_features(), remote_network_address: None }, false).unwrap();
+       node_b.node.peer_connected(&node_a.node.get_our_node_id(), &msgs::Init {
+               features: node_a.node.init_features(), networks: None, remote_network_address: None
+       }, false).unwrap();
        let reestablish_2 = get_chan_reestablish_msgs!(node_b, node_a);
 
        if send_channel_ready.0 {
index 98fc4bd95b6d58f100ae09aaa32519e57237598c..271ff541a84981733090b30600956886c2845d40 100644 (file)
@@ -20,14 +20,14 @@ use crate::chain::transaction::OutPoint;
 use crate::sign::{ChannelSigner, EcdsaChannelSigner, EntropySource};
 use crate::events::{Event, MessageSendEvent, MessageSendEventsProvider, PathFailure, PaymentPurpose, ClosureReason, HTLCDestination, PaymentFailureReason};
 use crate::ln::{PaymentPreimage, PaymentSecret, PaymentHash};
-use crate::ln::channel::{commitment_tx_base_weight, COMMITMENT_TX_WEIGHT_PER_HTLC, CONCURRENT_INBOUND_HTLC_FEE_BUFFER, FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE, MIN_AFFORDABLE_HTLC_COUNT};
+use crate::ln::channel::{commitment_tx_base_weight, COMMITMENT_TX_WEIGHT_PER_HTLC, CONCURRENT_INBOUND_HTLC_FEE_BUFFER, FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE, MIN_AFFORDABLE_HTLC_COUNT, get_holder_selected_channel_reserve_satoshis, OutboundV1Channel, InboundV1Channel};
 use crate::ln::channelmanager::{self, PaymentId, RAACommitmentOrder, PaymentSendFailure, RecipientOnionFields, BREAKDOWN_TIMEOUT, ENABLE_GOSSIP_TICKS, DISABLE_GOSSIP_TICKS, MIN_CLTV_EXPIRY_DELTA};
-use crate::ln::channel::{Channel, ChannelError};
+use crate::ln::channel::{DISCONNECT_PEER_AWAITING_RESPONSE_TICKS, ChannelError};
 use crate::ln::{chan_utils, onion_utils};
 use crate::ln::chan_utils::{OFFERED_HTLC_SCRIPT_WEIGHT, htlc_success_tx_weight, htlc_timeout_tx_weight, HTLCOutputInCommitment};
 use crate::routing::gossip::{NetworkGraph, NetworkUpdate};
 use crate::routing::router::{Path, PaymentParameters, Route, RouteHop, RouteParameters, find_route, get_route};
-use crate::ln::features::{ChannelFeatures, NodeFeatures};
+use crate::ln::features::{ChannelFeatures, ChannelTypeFeatures, NodeFeatures};
 use crate::ln::msgs;
 use crate::ln::msgs::{ChannelMessageHandler, RoutingMessageHandler, ErrorAction};
 use crate::util::enforcing_trait_impls::EnforcingSigner;
@@ -35,7 +35,7 @@ use crate::util::test_utils;
 use crate::util::errors::APIError;
 use crate::util::ser::{Writeable, ReadableArgs};
 use crate::util::string::UntrustedString;
-use crate::util::config::UserConfig;
+use crate::util::config::{UserConfig, MaxDustHTLCExposure};
 
 use bitcoin::hash_types::BlockHash;
 use bitcoin::blockdata::script::{Builder, Script};
@@ -75,7 +75,7 @@ fn test_insane_channel_opens() {
        // 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::<EnforcingSigner>::get_holder_selected_channel_reserve_satoshis(channel_value_sat, &cfg);
+       let channel_reserve_satoshis = get_holder_selected_channel_reserve_satoshis(channel_value_sat, &cfg);
        let push_msat = (channel_value_sat - channel_reserve_satoshis) * 1000;
 
        // Have node0 initiate a channel to node1 with aforementioned parameters
@@ -155,9 +155,9 @@ fn do_test_counterparty_no_reserve(send_from_initiator: bool) {
        // Have node0 initiate a channel to node1 with aforementioned parameters
        let mut push_amt = 100_000_000;
        let feerate_per_kw = 253;
-       let opt_anchors = false;
-       push_amt -= feerate_per_kw as u64 * (commitment_tx_base_weight(opt_anchors) + 4 * COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000 * 1000;
-       push_amt -= Channel::<EnforcingSigner>::get_holder_selected_channel_reserve_satoshis(100_000, &default_config) * 1000;
+       let channel_type_features = ChannelTypeFeatures::only_static_remote_key();
+       push_amt -= feerate_per_kw as u64 * (commitment_tx_base_weight(&channel_type_features) + 4 * COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000 * 1000;
+       push_amt -= get_holder_selected_channel_reserve_satoshis(100_000, &default_config) * 1000;
 
        let temp_channel_id = nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, if send_from_initiator { 0 } else { push_amt }, 42, None).unwrap();
        let mut open_channel_message = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
@@ -179,9 +179,15 @@ fn do_test_counterparty_no_reserve(send_from_initiator: bool) {
                let counterparty_node = if send_from_initiator { &nodes[0] } else { &nodes[1] };
                let mut sender_node_per_peer_lock;
                let mut sender_node_peer_state_lock;
-               let mut chan = get_channel_ref!(sender_node, counterparty_node, sender_node_per_peer_lock, sender_node_peer_state_lock, temp_channel_id);
-               chan.holder_selected_channel_reserve_satoshis = 0;
-               chan.holder_max_htlc_value_in_flight_msat = 100_000_000;
+               if send_from_initiator {
+                       let chan = get_inbound_v1_channel_ref!(sender_node, counterparty_node, sender_node_per_peer_lock, sender_node_peer_state_lock, temp_channel_id);
+                       chan.context.holder_selected_channel_reserve_satoshis = 0;
+                       chan.context.holder_max_htlc_value_in_flight_msat = 100_000_000;
+               } else {
+                       let chan = get_outbound_v1_channel_ref!(sender_node, counterparty_node, sender_node_per_peer_lock, sender_node_peer_state_lock, temp_channel_id);
+                       chan.context.holder_selected_channel_reserve_satoshis = 0;
+                       chan.context.holder_max_htlc_value_in_flight_msat = 100_000_000;
+               }
        }
 
        let funding_tx = sign_funding_transaction(&nodes[0], &nodes[1], 100_000, temp_channel_id);
@@ -195,7 +201,7 @@ fn do_test_counterparty_no_reserve(send_from_initiator: bool) {
                        // Note that for outbound channels we have to consider the commitment tx fee and the
                        // "fee spike buffer", which is currently a multiple of the total commitment tx fee as
                        // well as an additional HTLC.
-                       - FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE * commit_tx_fee_msat(feerate_per_kw, 2, opt_anchors));
+                       - FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE * commit_tx_fee_msat(feerate_per_kw, 2, &channel_type_features));
        } else {
                send_payment(&nodes[1], &[&nodes[0]], push_amt);
        }
@@ -643,16 +649,16 @@ fn test_update_fee_that_funder_cannot_afford() {
        let channel_id = chan.2;
        let secp_ctx = Secp256k1::new();
        let default_config = UserConfig::default();
-       let bs_channel_reserve_sats = Channel::<EnforcingSigner>::get_holder_selected_channel_reserve_satoshis(channel_value, &default_config);
+       let bs_channel_reserve_sats = get_holder_selected_channel_reserve_satoshis(channel_value, &default_config);
 
-       let opt_anchors = false;
+       let channel_type_features = ChannelTypeFeatures::only_static_remote_key();
 
        // Calculate the maximum feerate that A can afford. Note that we don't send an update_fee
        // CONCURRENT_INBOUND_HTLC_FEE_BUFFER HTLCs before actually running out of local balance, so we
        // calculate two different feerates here - the expected local limit as well as the expected
        // remote limit.
-       let feerate = ((channel_value - bs_channel_reserve_sats - push_sats) * 1000 / (commitment_tx_base_weight(opt_anchors) + CONCURRENT_INBOUND_HTLC_FEE_BUFFER as u64 * COMMITMENT_TX_WEIGHT_PER_HTLC)) as u32;
-       let non_buffer_feerate = ((channel_value - bs_channel_reserve_sats - push_sats) * 1000 / commitment_tx_base_weight(opt_anchors)) as u32;
+       let feerate = ((channel_value - bs_channel_reserve_sats - push_sats) * 1000 / (commitment_tx_base_weight(&channel_type_features) + CONCURRENT_INBOUND_HTLC_FEE_BUFFER as u64 * COMMITMENT_TX_WEIGHT_PER_HTLC)) as u32;
+       let non_buffer_feerate = ((channel_value - bs_channel_reserve_sats - push_sats) * 1000 / commitment_tx_base_weight(&channel_type_features)) as u32;
        {
                let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
                *feerate_lock = feerate;
@@ -671,7 +677,7 @@ fn test_update_fee_that_funder_cannot_afford() {
 
                //We made sure neither party's funds are below the dust limit and there are no HTLCs here
                assert_eq!(commitment_tx.output.len(), 2);
-               let total_fee: u64 = commit_tx_fee_msat(feerate, 0, opt_anchors) / 1000;
+               let total_fee: u64 = commit_tx_fee_msat(feerate, 0, &channel_type_features) / 1000;
                let mut actual_fee = commitment_tx.output.iter().fold(0, |acc, output| acc + output.value);
                actual_fee = channel_value - actual_fee;
                assert_eq!(total_fee, actual_fee);
@@ -723,12 +729,12 @@ fn test_update_fee_that_funder_cannot_afford() {
                let commitment_tx = CommitmentTransaction::new_with_auxiliary_htlc_data(
                        INITIAL_COMMITMENT_NUMBER - 1,
                        push_sats,
-                       channel_value - push_sats - commit_tx_fee_msat(non_buffer_feerate + 4, 0, opt_anchors) / 1000,
-                       opt_anchors, local_funding, remote_funding,
+                       channel_value - push_sats - commit_tx_fee_msat(non_buffer_feerate + 4, 0, &channel_type_features) / 1000,
+                       local_funding, remote_funding,
                        commit_tx_keys.clone(),
                        non_buffer_feerate + 4,
                        &mut htlcs,
-                       &local_chan.channel_transaction_parameters.as_counterparty_broadcastable()
+                       &local_chan.context.channel_transaction_parameters.as_counterparty_broadcastable()
                );
                local_chan_signer.sign_counterparty_commitment(&commitment_tx, Vec::new(), &secp_ctx).unwrap()
        };
@@ -1102,6 +1108,9 @@ fn holding_cell_htlc_counting() {
        create_announced_chan_between_nodes(&nodes, 0, 1);
        let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
 
+       // Fetch a route in advance as we will be unable to once we're unable to send.
+       let (route, payment_hash_1, _, payment_secret_1) = get_route_and_payment_hash!(nodes[1], nodes[2], 100000);
+
        let mut payments = Vec::new();
        for _ in 0..50 {
                let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[2], 100000);
@@ -1119,14 +1128,11 @@ fn holding_cell_htlc_counting() {
        // 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, payment_hash_1, _, payment_secret_1) = get_route_and_payment_hash!(nodes[1], nodes[2], 100000);
        {
                unwrap_send_err!(nodes[1].node.send_payment_with_route(&route, payment_hash_1,
                                RecipientOnionFields::secret_only(payment_secret_1), PaymentId(payment_hash_1.0)
-                       ), true, APIError::ChannelUnavailable { ref err },
-                       assert!(regex::Regex::new(r"Cannot push more than their max accepted HTLCs \(\d+\)").unwrap().is_match(err)));
+                       ), true, APIError::ChannelUnavailable { .. }, {});
                assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-               nodes[1].logger.assert_log_contains("lightning::ln::channelmanager", "Cannot push more than their max accepted HTLCs", 1);
        }
 
        // This should also be true if we try to forward a payment.
@@ -1340,23 +1346,21 @@ fn test_basic_channel_reserve() {
        let channel_reserve = chan_stat.channel_reserve_msat;
 
        // The 2* and +1 are for the fee spike reserve.
-       let commit_tx_fee = 2 * commit_tx_fee_msat(get_feerate!(nodes[0], nodes[1], chan.2), 1 + 1, get_opt_anchors!(nodes[0], nodes[1], chan.2));
+       let commit_tx_fee = 2 * commit_tx_fee_msat(get_feerate!(nodes[0], nodes[1], chan.2), 1 + 1, &get_channel_type_features!(nodes[0], nodes[1], chan.2));
        let max_can_send = 5000000 - channel_reserve - commit_tx_fee;
-       let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], max_can_send + 1);
+       let (mut route, our_payment_hash, _, our_payment_secret) =
+               get_route_and_payment_hash!(nodes[0], nodes[1], max_can_send);
+       route.paths[0].hops.last_mut().unwrap().fee_msat += 1;
        let err = nodes[0].node.send_payment_with_route(&route, our_payment_hash,
                RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).err().unwrap();
        match err {
                PaymentSendFailure::AllFailedResendSafe(ref fails) => {
-                       match &fails[0] {
-                               &APIError::ChannelUnavailable{ref err} =>
-                                       assert!(regex::Regex::new(r"Cannot send value that would put our balance under counterparty-announced channel reserve value \(\d+\)").unwrap().is_match(err)),
-                               _ => panic!("Unexpected error variant"),
-                       }
+                       if let &APIError::ChannelUnavailable { .. } = &fails[0] {}
+                       else { panic!("Unexpected error variant"); }
                },
                _ => panic!("Unexpected error variant"),
        }
        assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
-       nodes[0].logger.assert_log_contains("lightning::ln::channelmanager", "Cannot send value that would put our balance under counterparty-announced channel reserve value", 1);
 
        send_payment(&nodes[0], &vec![&nodes[1]], max_can_send);
 }
@@ -1369,7 +1373,9 @@ fn test_fee_spike_violation_fails_htlc() {
        let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
        let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
 
-       let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 3460001);
+       let (mut route, payment_hash, _, payment_secret) =
+               get_route_and_payment_hash!(nodes[0], nodes[1], 3460000);
+       route.paths[0].hops[0].fee_msat += 1;
        // Need to manually create the 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(&[42; 32]).expect("RNG is bad!");
@@ -1387,6 +1393,7 @@ fn test_fee_spike_violation_fails_htlc() {
                payment_hash: payment_hash,
                cltv_expiry: htlc_cltv,
                onion_routing_packet: onion_packet,
+               skimmed_fee_msat: None,
        };
 
        nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &msg);
@@ -1453,11 +1460,11 @@ fn test_fee_spike_violation_fails_htlc() {
                        commitment_number,
                        95000,
                        local_chan_balance,
-                       local_chan.opt_anchors(), local_funding, remote_funding,
+                       local_funding, remote_funding,
                        commit_tx_keys.clone(),
                        feerate_per_kw,
                        &mut vec![(accepted_htlc_info, ())],
-                       &local_chan.channel_transaction_parameters.as_counterparty_broadcastable()
+                       &local_chan.context.channel_transaction_parameters.as_counterparty_broadcastable()
                );
                local_chan_signer.sign_counterparty_commitment(&commitment_tx, Vec::new(), &secp_ctx).unwrap()
        };
@@ -1512,28 +1519,27 @@ fn test_chan_reserve_violation_outbound_htlc_inbound_chan() {
        let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
        let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
        let default_config = UserConfig::default();
-       let opt_anchors = false;
+       let channel_type_features = ChannelTypeFeatures::only_static_remote_key();
 
        let mut push_amt = 100_000_000;
-       push_amt -= commit_tx_fee_msat(feerate_per_kw, MIN_AFFORDABLE_HTLC_COUNT as u64, opt_anchors);
+       push_amt -= commit_tx_fee_msat(feerate_per_kw, MIN_AFFORDABLE_HTLC_COUNT as u64, &channel_type_features);
 
-       push_amt -= Channel::<EnforcingSigner>::get_holder_selected_channel_reserve_satoshis(100_000, &default_config) * 1000;
+       push_amt -= get_holder_selected_channel_reserve_satoshis(100_000, &default_config) * 1000;
 
        let _ = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, push_amt);
 
+       // Fetch a route in advance as we will be unable to once we're unable to send.
+       let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 1_000_000);
        // Sending exactly enough to hit the reserve amount should be accepted
        for _ in 0..MIN_AFFORDABLE_HTLC_COUNT {
                let (_, _, _) = route_payment(&nodes[1], &[&nodes[0]], 1_000_000);
        }
 
        // However one more HTLC should be significantly over the reserve amount and fail.
-       let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 1_000_000);
        unwrap_send_err!(nodes[1].node.send_payment_with_route(&route, our_payment_hash,
                        RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
-               ), true, APIError::ChannelUnavailable { ref err },
-               assert_eq!(err, "Cannot send value that would put counterparty balance under holder-announced channel reserve value"));
+               ), true, APIError::ChannelUnavailable { .. }, {});
        assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
-       nodes[1].logger.assert_log("lightning::ln::channelmanager".to_string(), "Cannot send value that would put counterparty balance under holder-announced channel reserve value".to_string(), 1);
 }
 
 #[test]
@@ -1544,14 +1550,14 @@ fn test_chan_reserve_violation_inbound_htlc_outbound_channel() {
        let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
        let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
        let default_config = UserConfig::default();
-       let opt_anchors = false;
+       let channel_type_features = ChannelTypeFeatures::only_static_remote_key();
 
        // Set nodes[0]'s balance such that they will consider any above-dust received HTLC to be a
        // channel reserve violation (so their balance is channel reserve (1000 sats) + commitment
        // transaction fee with 0 HTLCs (183 sats)).
        let mut push_amt = 100_000_000;
-       push_amt -= commit_tx_fee_msat(feerate_per_kw, MIN_AFFORDABLE_HTLC_COUNT as u64, opt_anchors);
-       push_amt -= Channel::<EnforcingSigner>::get_holder_selected_channel_reserve_satoshis(100_000, &default_config) * 1000;
+       push_amt -= commit_tx_fee_msat(feerate_per_kw, MIN_AFFORDABLE_HTLC_COUNT as u64, &channel_type_features);
+       push_amt -= get_holder_selected_channel_reserve_satoshis(100_000, &default_config) * 1000;
        let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, push_amt);
 
        // Send four HTLCs to cover the initial push_msat buffer we're required to include
@@ -1559,7 +1565,9 @@ fn test_chan_reserve_violation_inbound_htlc_outbound_channel() {
                let (_, _, _) = route_payment(&nodes[1], &[&nodes[0]], 1_000_000);
        }
 
-       let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 700_000);
+       let (mut route, payment_hash, _, payment_secret) =
+               get_route_and_payment_hash!(nodes[1], nodes[0], 1000);
+       route.paths[0].hops[0].fee_msat = 700_000;
        // Need to manually create the 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(&[42; 32]).unwrap();
@@ -1575,6 +1583,7 @@ fn test_chan_reserve_violation_inbound_htlc_outbound_channel() {
                payment_hash: payment_hash,
                cltv_expiry: htlc_cltv,
                onion_routing_packet: onion_packet,
+               skimmed_fee_msat: None,
        };
 
        nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &msg);
@@ -1598,18 +1607,18 @@ fn test_chan_reserve_dust_inbound_htlcs_outbound_chan() {
        let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None, None]);
        let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
        let default_config = UserConfig::default();
-       let opt_anchors = false;
+       let channel_type_features = ChannelTypeFeatures::only_static_remote_key();
 
        // Set nodes[0]'s balance such that they will consider any above-dust received HTLC to be a
        // channel reserve violation (so their balance is channel reserve (1000 sats) + commitment
        // transaction fee with 0 HTLCs (183 sats)).
        let mut push_amt = 100_000_000;
-       push_amt -= commit_tx_fee_msat(feerate_per_kw, MIN_AFFORDABLE_HTLC_COUNT as u64, opt_anchors);
-       push_amt -= Channel::<EnforcingSigner>::get_holder_selected_channel_reserve_satoshis(100_000, &default_config) * 1000;
+       push_amt -= commit_tx_fee_msat(feerate_per_kw, MIN_AFFORDABLE_HTLC_COUNT as u64, &channel_type_features);
+       push_amt -= get_holder_selected_channel_reserve_satoshis(100_000, &default_config) * 1000;
        create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, push_amt);
 
        let dust_amt = crate::ln::channel::MIN_CHAN_DUST_LIMIT_SATOSHIS * 1000
-               + feerate_per_kw as u64 * htlc_success_tx_weight(opt_anchors) / 1000 * 1000 - 1;
+               + feerate_per_kw as u64 * htlc_success_tx_weight(&channel_type_features) / 1000 * 1000 - 1;
        // In the previous code, routing this dust payment would cause nodes[0] to perceive a channel
        // reserve violation even though it's a dust HTLC and therefore shouldn't count towards the
        // commitment transaction fee.
@@ -1621,11 +1630,12 @@ fn test_chan_reserve_dust_inbound_htlcs_outbound_chan() {
        }
 
        // One more than the dust amt should fail, however.
-       let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], dust_amt + 1);
+       let (mut route, our_payment_hash, _, our_payment_secret) =
+               get_route_and_payment_hash!(nodes[1], nodes[0], dust_amt);
+       route.paths[0].hops[0].fee_msat += 1;
        unwrap_send_err!(nodes[1].node.send_payment_with_route(&route, our_payment_hash,
                        RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
-               ), true, APIError::ChannelUnavailable { ref err },
-               assert_eq!(err, "Cannot send value that would put counterparty balance under holder-announced channel reserve value"));
+               ), true, APIError::ChannelUnavailable { .. }, {});
 }
 
 #[test]
@@ -1638,18 +1648,18 @@ fn test_chan_init_feerate_unaffordability() {
        let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
        let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
        let default_config = UserConfig::default();
-       let opt_anchors = false;
+       let channel_type_features = ChannelTypeFeatures::only_static_remote_key();
 
        // Set the push_msat amount such that nodes[0] will not be able to afford to add even a single
        // HTLC.
        let mut push_amt = 100_000_000;
-       push_amt -= commit_tx_fee_msat(feerate_per_kw, MIN_AFFORDABLE_HTLC_COUNT as u64, opt_anchors);
+       push_amt -= commit_tx_fee_msat(feerate_per_kw, MIN_AFFORDABLE_HTLC_COUNT as u64, &channel_type_features);
        assert_eq!(nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, push_amt + 1, 42, None).unwrap_err(),
                APIError::APIMisuseError { err: "Funding amount (356) can't even pay fee for initial commitment transaction fee of 357.".to_string() });
 
        // During open, we don't have a "counterparty channel reserve" to check against, so that
        // requirement only comes into play on the open_channel handling side.
-       push_amt -= Channel::<EnforcingSigner>::get_holder_selected_channel_reserve_satoshis(100_000, &default_config) * 1000;
+       push_amt -= get_holder_selected_channel_reserve_satoshis(100_000, &default_config) * 1000;
        nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, push_amt, 42, None).unwrap();
        let mut open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
        open_channel_msg.push_msat += 1;
@@ -1708,10 +1718,10 @@ fn test_chan_reserve_violation_inbound_htlc_inbound_chan() {
        let total_routing_fee_msat = (nodes.len() - 2) as u64 * feemsat;
        let chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
        let feerate = get_feerate!(nodes[0], nodes[1], chan.2);
-       let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan.2);
+       let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan.2);
 
        // Add a 2* and +1 for the fee spike reserve.
-       let commit_tx_fee_2_htlc = 2*commit_tx_fee_msat(feerate, 2 + 1, opt_anchors);
+       let commit_tx_fee_2_htlc = 2*commit_tx_fee_msat(feerate, 2 + 1, &channel_type_features);
        let recv_value_1 = (chan_stat.value_to_self_msat - chan_stat.channel_reserve_msat - total_routing_fee_msat - commit_tx_fee_2_htlc)/2;
        let amt_msat_1 = recv_value_1 + total_routing_fee_msat;
 
@@ -1729,10 +1739,11 @@ fn test_chan_reserve_violation_inbound_htlc_inbound_chan() {
        nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event_1.msgs[0]);
 
        // Attempt to trigger a channel reserve violation --> payment failure.
-       let commit_tx_fee_2_htlcs = commit_tx_fee_msat(feerate, 2, opt_anchors);
+       let commit_tx_fee_2_htlcs = commit_tx_fee_msat(feerate, 2, &channel_type_features);
        let recv_value_2 = chan_stat.value_to_self_msat - amt_msat_1 - chan_stat.channel_reserve_msat - total_routing_fee_msat - commit_tx_fee_2_htlcs + 1;
        let amt_msat_2 = recv_value_2 + total_routing_fee_msat;
-       let (route_2, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[2], amt_msat_2);
+       let mut route_2 = route_1.clone();
+       route_2.paths[0].hops.last_mut().unwrap().fee_msat = amt_msat_2;
 
        // Need to manually create the update_add_htlc message to go around the channel reserve check in send_htlc()
        let secp_ctx = Secp256k1::new();
@@ -1749,6 +1760,7 @@ fn test_chan_reserve_violation_inbound_htlc_inbound_chan() {
                payment_hash: our_payment_hash_1,
                cltv_expiry: htlc_cltv,
                onion_routing_packet: onion_packet,
+               skimmed_fee_msat: None,
        };
 
        nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &msg);
@@ -1774,7 +1786,7 @@ fn test_inbound_outbound_capacity_is_not_zero() {
        assert_eq!(channels0.len(), 1);
        assert_eq!(channels1.len(), 1);
 
-       let reserve = Channel::<EnforcingSigner>::get_holder_selected_channel_reserve_satoshis(100_000, &default_config);
+       let reserve = get_holder_selected_channel_reserve_satoshis(100_000, &default_config);
        assert_eq!(channels0[0].inbound_capacity_msat, 95000000 - reserve*1000);
        assert_eq!(channels1[0].outbound_capacity_msat, 95000000 - reserve*1000);
 
@@ -1782,8 +1794,8 @@ fn test_inbound_outbound_capacity_is_not_zero() {
        assert_eq!(channels1[0].inbound_capacity_msat, 100000 * 1000 - 95000000 - reserve*1000);
 }
 
-fn commit_tx_fee_msat(feerate: u32, num_htlcs: u64, opt_anchors: bool) -> u64 {
-       (commitment_tx_base_weight(opt_anchors) + num_htlcs * COMMITMENT_TX_WEIGHT_PER_HTLC) * feerate as u64 / 1000 * 1000
+fn commit_tx_fee_msat(feerate: u32, num_htlcs: u64, channel_type_features: &ChannelTypeFeatures) -> u64 {
+       (commitment_tx_base_weight(channel_type_features) + num_htlcs * COMMITMENT_TX_WEIGHT_PER_HTLC) * feerate as u64 / 1000 * 1000
 }
 
 #[test]
@@ -1818,7 +1830,7 @@ fn test_channel_reserve_holding_cell_htlcs() {
        let feemsat = 239; // set above
        let total_fee_msat = (nodes.len() - 2) as u64 * feemsat;
        let feerate = get_feerate!(nodes[0], nodes[1], chan_1.2);
-       let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_1.2);
+       let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan_1.2);
 
        let recv_value_0 = stat01.counterparty_max_htlc_value_in_flight_msat - total_fee_msat;
 
@@ -1832,10 +1844,8 @@ fn test_channel_reserve_holding_cell_htlcs() {
 
                unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
                                RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
-                       ), true, APIError::ChannelUnavailable { ref err },
-                       assert!(regex::Regex::new(r"Cannot send value that would put us over the max HTLC value in flight our peer will accept \(\d+\)").unwrap().is_match(err)));
+                       ), true, APIError::ChannelUnavailable { .. }, {});
                assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
-               nodes[0].logger.assert_log_contains("lightning::ln::channelmanager", "Cannot send value that would put us over the max HTLC value in flight our peer will accept", 1);
        }
 
        // channel reserve is bigger than their_max_htlc_value_in_flight_msat so loop to deplete
@@ -1845,7 +1855,7 @@ fn test_channel_reserve_holding_cell_htlcs() {
                // 3 for the 3 HTLCs that will be sent, 2* and +1 for the fee spike reserve.
                // Also, ensure that each payment has enough to be over the dust limit to
                // ensure it'll be included in each commit tx fee calculation.
-               let commit_tx_fee_all_htlcs = 2*commit_tx_fee_msat(feerate, 3 + 1, opt_anchors);
+               let commit_tx_fee_all_htlcs = 2*commit_tx_fee_msat(feerate, 3 + 1, &channel_type_features);
                let ensure_htlc_amounts_above_dust_buffer = 3 * (stat01.counterparty_dust_limit_msat + 1000);
                if stat01.value_to_self_msat < stat01.channel_reserve_msat + commit_tx_fee_all_htlcs + ensure_htlc_amounts_above_dust_buffer + amt_msat {
                        break;
@@ -1882,7 +1892,7 @@ fn test_channel_reserve_holding_cell_htlcs() {
        // the amount of the first of these aforementioned 3 payments. The reason we split into 3 payments
        // is to test the behavior of the holding cell with respect to channel reserve and commit tx fee
        // policy.
-       let commit_tx_fee_2_htlcs = 2*commit_tx_fee_msat(feerate, 2 + 1, opt_anchors);
+       let commit_tx_fee_2_htlcs = 2*commit_tx_fee_msat(feerate, 2 + 1, &channel_type_features);
        let recv_value_1 = (stat01.value_to_self_msat - stat01.channel_reserve_msat - total_fee_msat - commit_tx_fee_2_htlcs)/2;
        let amt_msat_1 = recv_value_1 + total_fee_msat;
 
@@ -1901,16 +1911,17 @@ fn test_channel_reserve_holding_cell_htlcs() {
        // 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 - commit_tx_fee_2_htlcs;
        {
-               let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], recv_value_2 + 1);
+               let mut route = route_1.clone();
+               route.paths[0].hops.last_mut().unwrap().fee_msat = recv_value_2 + 1;
+               let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[2]);
                unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
                                RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
-                       ), true, APIError::ChannelUnavailable { ref err },
-                       assert!(regex::Regex::new(r"Cannot send value that would put our balance under counterparty-announced channel reserve value \(\d+\)").unwrap().is_match(err)));
+                       ), true, APIError::ChannelUnavailable { .. }, {});
                assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
        }
 
        // split the rest to test holding cell
-       let commit_tx_fee_3_htlcs = 2*commit_tx_fee_msat(feerate, 3 + 1, opt_anchors);
+       let commit_tx_fee_3_htlcs = 2*commit_tx_fee_msat(feerate, 3 + 1, &channel_type_features);
        let additional_htlc_cost_msat = commit_tx_fee_3_htlcs - commit_tx_fee_2_htlcs;
        let recv_value_21 = recv_value_2/2 - additional_htlc_cost_msat/2;
        let recv_value_22 = recv_value_2 - recv_value_21 - total_fee_msat - additional_htlc_cost_msat;
@@ -1930,13 +1941,13 @@ fn test_channel_reserve_holding_cell_htlcs() {
 
        // test with outbound holding cell amount > 0
        {
-               let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], recv_value_22+1);
+               let (mut route, our_payment_hash, _, our_payment_secret) =
+                       get_route_and_payment_hash!(nodes[0], nodes[2], recv_value_22);
+               route.paths[0].hops.last_mut().unwrap().fee_msat += 1;
                unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
                                RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
-                       ), true, APIError::ChannelUnavailable { ref err },
-                       assert!(regex::Regex::new(r"Cannot send value that would put our balance under counterparty-announced channel reserve value \(\d+\)").unwrap().is_match(err)));
+                       ), true, APIError::ChannelUnavailable { .. }, {});
                assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
-               nodes[0].logger.assert_log_contains("lightning::ln::channelmanager", "Cannot send value that would put our balance under counterparty-announced channel reserve value", 2);
        }
 
        let (route_22, our_payment_hash_22, our_payment_preimage_22, our_payment_secret_22) = get_route_and_payment_hash!(nodes[0], nodes[2], recv_value_22);
@@ -2029,11 +2040,11 @@ fn test_channel_reserve_holding_cell_htlcs() {
        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 commit_tx_fee_0_htlcs = 2*commit_tx_fee_msat(feerate, 1, opt_anchors);
+       let commit_tx_fee_0_htlcs = 2*commit_tx_fee_msat(feerate, 1, &channel_type_features);
        let recv_value_3 = commit_tx_fee_2_htlcs - commit_tx_fee_0_htlcs - total_fee_msat;
        send_payment(&nodes[0], &vec![&nodes[1], &nodes[2]][..], recv_value_3);
 
-       let commit_tx_fee_1_htlc = 2*commit_tx_fee_msat(feerate, 1 + 1, opt_anchors);
+       let commit_tx_fee_1_htlc = 2*commit_tx_fee_msat(feerate, 1 + 1, &channel_type_features);
        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) - (recv_value_3 + total_fee_msat);
        let stat0 = get_channel_value_stat!(nodes[0], nodes[1], chan_1.2);
        assert_eq!(stat0.value_to_self_msat, expected_value_to_self);
@@ -3120,7 +3131,7 @@ fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool, use
                // 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.per_peer_state.read().unwrap().get(&nodes[1].node.get_our_node_id())
-                       .unwrap().lock().unwrap().channel_by_id.get(&chan_2.2).unwrap().holder_dust_limit_satoshis * 1000
+                       .unwrap().lock().unwrap().channel_by_id.get(&chan_2.2).unwrap().context.holder_dust_limit_satoshis * 1000
        } else { 3000000 };
 
        let (_, first_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], value);
@@ -3402,6 +3413,7 @@ fn fail_backward_pending_htlc_upon_channel_failure() {
                        payment_hash,
                        cltv_expiry,
                        onion_routing_packet,
+                       skimmed_fee_msat: None,
                };
                nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &update_add_htlc);
        }
@@ -3612,8 +3624,8 @@ fn test_peer_disconnected_before_funding_broadcasted() {
        nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
        nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
 
-       check_closed_event!(nodes[0], 1, ClosureReason::DisconnectedPeer);
-       check_closed_event!(nodes[1], 1, ClosureReason::DisconnectedPeer);
+       check_closed_event(&nodes[0], 1, ClosureReason::DisconnectedPeer, false);
+       check_closed_event(&nodes[1], 1, ClosureReason::DisconnectedPeer, false);
 }
 
 #[test]
@@ -4025,10 +4037,14 @@ fn test_drop_messages_peer_disconnect_dual_htlc() {
        nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
        nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
 
-       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: nodes[1].node.init_features(), remote_network_address: None }, true).unwrap();
+       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
+               features: nodes[1].node.init_features(), networks: None, remote_network_address: None
+       }, true).unwrap();
        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(), &msgs::Init { features: nodes[0].node.init_features(), remote_network_address: None }, false).unwrap();
+       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
+               features: nodes[0].node.init_features(), networks: None, remote_network_address: None
+       }, false).unwrap();
        let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
        assert_eq!(reestablish_2.len(), 1);
 
@@ -4984,7 +5000,7 @@ fn do_test_fail_backwards_unrevoked_remote_announce(deliver_last_raa: bool, anno
        assert_eq!(get_local_commitment_txn!(nodes[3], chan_2_3.2)[0].output.len(), 2);
 
        let ds_dust_limit = nodes[3].node.per_peer_state.read().unwrap().get(&nodes[2].node.get_our_node_id())
-               .unwrap().lock().unwrap().channel_by_id.get(&chan_2_3.2).unwrap().holder_dust_limit_satoshis;
+               .unwrap().lock().unwrap().channel_by_id.get(&chan_2_3.2).unwrap().context.holder_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:
@@ -5694,10 +5710,10 @@ fn test_fail_holding_cell_htlc_upon_free() {
        let mut chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
        let channel_reserve = chan_stat.channel_reserve_msat;
        let feerate = get_feerate!(nodes[0], nodes[1], chan.2);
-       let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan.2);
+       let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan.2);
 
        // 2* and +1 HTLCs on the commit tx fee calculation for the fee spike reserve.
-       let max_can_send = 5000000 - channel_reserve - 2*commit_tx_fee_msat(feerate, 1 + 1, opt_anchors);
+       let max_can_send = 5000000 - channel_reserve - 2*commit_tx_fee_msat(feerate, 1 + 1, &channel_type_features);
        let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], max_can_send);
 
        // Send a payment which passes reserve checks but gets stuck in the holding cell.
@@ -5719,9 +5735,6 @@ fn test_fail_holding_cell_htlc_upon_free() {
        chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
        assert_eq!(chan_stat.holding_cell_outbound_amount_msat, 0);
        nodes[0].logger.assert_log("lightning::ln::channel".to_string(), format!("Freeing holding cell with 1 HTLC updates in channel {}", hex::encode(chan.2)), 1);
-       let failure_log = format!("Failed to send HTLC with payment_hash {} due to Cannot send value that would put our balance under counterparty-announced channel reserve value ({}) in channel {}",
-               hex::encode(our_payment_hash.0), chan_stat.channel_reserve_msat, hex::encode(chan.2));
-       nodes[0].logger.assert_log("lightning::ln::channel".to_string(), failure_log.to_string(), 1);
 
        // Check that the payment failed to be sent out.
        let events = nodes[0].node.get_and_clear_pending_events();
@@ -5777,11 +5790,11 @@ fn test_free_and_fail_holding_cell_htlcs() {
        let mut chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
        let channel_reserve = chan_stat.channel_reserve_msat;
        let feerate = get_feerate!(nodes[0], nodes[1], chan.2);
-       let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan.2);
+       let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan.2);
 
        // 2* and +1 HTLCs on the commit tx fee calculation for the fee spike reserve.
        let amt_1 = 20000;
-       let amt_2 = 5000000 - channel_reserve - 2*commit_tx_fee_msat(feerate, 2 + 1, opt_anchors) - amt_1;
+       let amt_2 = 5000000 - channel_reserve - 2*commit_tx_fee_msat(feerate, 2 + 1, &channel_type_features) - amt_1;
        let (route_1, payment_hash_1, payment_preimage_1, payment_secret_1) = get_route_and_payment_hash!(nodes[0], nodes[1], amt_1);
        let (route_2, payment_hash_2, _, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[1], amt_2);
 
@@ -5810,9 +5823,6 @@ fn test_free_and_fail_holding_cell_htlcs() {
        chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
        assert_eq!(chan_stat.holding_cell_outbound_amount_msat, 0);
        nodes[0].logger.assert_log("lightning::ln::channel".to_string(), format!("Freeing holding cell with 2 HTLC updates in channel {}", hex::encode(chan.2)), 1);
-       let failure_log = format!("Failed to send HTLC with payment_hash {} due to Cannot send value that would put our balance under counterparty-announced channel reserve value ({}) in channel {}",
-               hex::encode(payment_hash_2.0), chan_stat.channel_reserve_msat, hex::encode(chan.2));
-       nodes[0].logger.assert_log("lightning::ln::channel".to_string(), failure_log.to_string(), 1);
 
        // Check that the second payment failed to be sent out.
        let events = nodes[0].node.get_and_clear_pending_events();
@@ -5878,10 +5888,10 @@ fn test_free_and_fail_holding_cell_htlcs() {
 fn test_fail_holding_cell_htlc_upon_free_multihop() {
        let chanmon_cfgs = create_chanmon_cfgs(3);
        let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
-       // When this test was written, the default base fee floated based on the HTLC count.
-       // It is now fixed, so we simply set the fee to the expected value here.
+       // Avoid having to include routing fees in calculations
        let mut config = test_default_channel_config();
-       config.channel_config.forwarding_fee_base_msat = 196;
+       config.channel_config.forwarding_fee_base_msat = 0;
+       config.channel_config.forwarding_fee_proportional_millionths = 0;
        let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[Some(config.clone()), Some(config.clone()), Some(config.clone())]);
        let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
        let chan_0_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
@@ -5910,12 +5920,10 @@ fn test_fail_holding_cell_htlc_upon_free_multihop() {
        let mut chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan_0_1.2);
        let channel_reserve = chan_stat.channel_reserve_msat;
        let feerate = get_feerate!(nodes[0], nodes[1], chan_0_1.2);
-       let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_0_1.2);
+       let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan_0_1.2);
 
        // Send a payment which passes reserve checks but gets stuck in the holding cell.
-       let feemsat = 239;
-       let total_routing_fee_msat = (nodes.len() - 2) as u64 * feemsat;
-       let max_can_send = 5000000 - channel_reserve - 2*commit_tx_fee_msat(feerate, 1 + 1, opt_anchors) - total_routing_fee_msat;
+       let max_can_send = 5000000 - channel_reserve - 2*commit_tx_fee_msat(feerate, 1 + 1, &channel_type_features);
        let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], max_can_send);
        let payment_event = {
                nodes[0].node.send_payment_with_route(&route, our_payment_hash,
@@ -6023,10 +6031,8 @@ fn test_update_add_htlc_bolt2_sender_value_below_minimum_msat() {
 
        unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
                        RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
-               ), true, APIError::ChannelUnavailable { ref err },
-               assert!(regex::Regex::new(r"Cannot send less than their minimum HTLC value \(\d+\)").unwrap().is_match(err)));
+               ), true, APIError::ChannelUnavailable { .. }, {});
        assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
-       nodes[0].logger.assert_log_contains("lightning::ln::channelmanager", "Cannot send less than their minimum HTLC value", 1);
 }
 
 #[test]
@@ -6103,8 +6109,10 @@ fn test_update_add_htlc_bolt2_sender_exceed_max_htlc_num_and_htlc_id_increment()
        let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
        let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 0);
        let max_accepted_htlcs = nodes[1].node.per_peer_state.read().unwrap().get(&nodes[0].node.get_our_node_id())
-               .unwrap().lock().unwrap().channel_by_id.get(&chan.2).unwrap().counterparty_max_accepted_htlcs as u64;
+               .unwrap().lock().unwrap().channel_by_id.get(&chan.2).unwrap().context.counterparty_max_accepted_htlcs as u64;
 
+       // Fetch a route in advance as we will be unable to once we're unable to send.
+       let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100000);
        for i in 0..max_accepted_htlcs {
                let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100000);
                let payment_event = {
@@ -6128,14 +6136,11 @@ fn test_update_add_htlc_bolt2_sender_exceed_max_htlc_num_and_htlc_id_increment()
                expect_pending_htlcs_forwardable!(nodes[1]);
                expect_payment_claimable!(nodes[1], our_payment_hash, our_payment_secret, 100000);
        }
-       let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100000);
        unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
                        RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
-               ), true, APIError::ChannelUnavailable { ref err },
-               assert!(regex::Regex::new(r"Cannot push more than their max accepted HTLCs \(\d+\)").unwrap().is_match(err)));
+               ), true, APIError::ChannelUnavailable { .. }, {});
 
        assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
-       nodes[0].logger.assert_log_contains("lightning::ln::channelmanager", "Cannot push more than their max accepted HTLCs", 1);
 }
 
 #[test]
@@ -6157,11 +6162,8 @@ fn test_update_add_htlc_bolt2_sender_exceed_max_htlc_value_in_flight() {
        route.paths[0].hops[0].fee_msat =  max_in_flight + 1;
        unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
                        RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
-               ), true, APIError::ChannelUnavailable { ref err },
-               assert!(regex::Regex::new(r"Cannot send value that would put us over the max HTLC value in flight our peer will accept \(\d+\)").unwrap().is_match(err)));
-
+               ), true, APIError::ChannelUnavailable { .. }, {});
        assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
-       nodes[0].logger.assert_log_contains("lightning::ln::channelmanager", "Cannot send value that would put us over the max HTLC value in flight our peer will accept", 1);
 
        send_payment(&nodes[0], &[&nodes[1]], max_in_flight);
 }
@@ -6180,7 +6182,7 @@ fn test_update_add_htlc_bolt2_receiver_check_amount_received_more_than_min() {
                let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
                let chan_lock = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
                let channel = chan_lock.channel_by_id.get(&chan.2).unwrap();
-               htlc_minimum_msat = channel.get_holder_htlc_minimum_msat();
+               htlc_minimum_msat = channel.context.get_holder_htlc_minimum_msat();
        }
 
        let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], htlc_minimum_msat);
@@ -6209,9 +6211,9 @@ fn test_update_add_htlc_bolt2_receiver_sender_can_afford_amount_sent() {
        let chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
        let channel_reserve = chan_stat.channel_reserve_msat;
        let feerate = get_feerate!(nodes[0], nodes[1], chan.2);
-       let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan.2);
+       let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan.2);
        // The 2* and +1 are for the fee spike reserve.
-       let commit_tx_fee_outbound = 2 * commit_tx_fee_msat(feerate, 1 + 1, opt_anchors);
+       let commit_tx_fee_outbound = 2 * commit_tx_fee_msat(feerate, 1 + 1, &channel_type_features);
 
        let max_can_send = 5000000 - channel_reserve - commit_tx_fee_outbound;
        let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], max_can_send);
@@ -6243,12 +6245,15 @@ fn test_update_add_htlc_bolt2_receiver_check_max_htlc_limit() {
        let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
        let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
 
-       let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 3999999);
+       let send_amt = 3999999;
+       let (mut route, our_payment_hash, _, our_payment_secret) =
+               get_route_and_payment_hash!(nodes[0], nodes[1], 1000);
+       route.paths[0].hops[0].fee_msat = send_amt;
        let session_priv = SecretKey::from_slice(&[42; 32]).unwrap();
        let cur_height = nodes[0].node.best_block.read().unwrap().height() + 1;
        let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::signing_only(), &route.paths[0], &session_priv).unwrap();
        let (onion_payloads, _htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(
-               &route.paths[0], 3999999, RecipientOnionFields::secret_only(our_payment_secret), cur_height, &None).unwrap();
+               &route.paths[0], send_amt, RecipientOnionFields::secret_only(our_payment_secret), cur_height, &None).unwrap();
        let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &our_payment_hash).unwrap();
 
        let mut msg = msgs::UpdateAddHTLC {
@@ -6258,6 +6263,7 @@ fn test_update_add_htlc_bolt2_receiver_check_max_htlc_limit() {
                payment_hash: our_payment_hash,
                cltv_expiry: htlc_cltv,
                onion_routing_packet: onion_packet.clone(),
+               skimmed_fee_msat: None,
        };
 
        for i in 0..50 {
@@ -6343,10 +6349,14 @@ fn test_update_add_htlc_bolt2_receiver_check_repeated_id_ignore() {
        //Disconnect and Reconnect
        nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
        nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
-       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: nodes[1].node.init_features(), remote_network_address: None }, true).unwrap();
+       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
+               features: nodes[1].node.init_features(), networks: None, remote_network_address: None
+       }, true).unwrap();
        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(), &msgs::Init { features: nodes[0].node.init_features(), remote_network_address: None }, false).unwrap();
+       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
+               features: nodes[0].node.init_features(), networks: None, remote_network_address: None
+       }, false).unwrap();
        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]);
@@ -6775,7 +6785,7 @@ fn do_test_failure_delay_dust_htlc_local_commitment(announce_latest: bool) {
        let chan =create_announced_chan_between_nodes(&nodes, 0, 1);
 
        let bs_dust_limit = nodes[1].node.per_peer_state.read().unwrap().get(&nodes[0].node.get_our_node_id())
-               .unwrap().lock().unwrap().channel_by_id.get(&chan.2).unwrap().holder_dust_limit_satoshis;
+               .unwrap().lock().unwrap().channel_by_id.get(&chan.2).unwrap().context.holder_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);
@@ -6868,7 +6878,7 @@ fn do_test_sweep_outbound_htlc_failure_update(revoked: bool, local: bool) {
        let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
 
        let bs_dust_limit = nodes[1].node.per_peer_state.read().unwrap().get(&nodes[0].node.get_our_node_id())
-               .unwrap().lock().unwrap().channel_by_id.get(&chan.2).unwrap().holder_dust_limit_satoshis;
+               .unwrap().lock().unwrap().channel_by_id.get(&chan.2).unwrap().context.holder_dust_limit_satoshis;
 
        let (_payment_preimage_1, dust_hash, _payment_secret_1) = route_payment(&nodes[0], &[&nodes[1]], bs_dust_limit*1000);
        let (_payment_preimage_2, non_dust_hash, _payment_secret_2) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
@@ -6951,8 +6961,8 @@ fn test_user_configurable_csv_delay() {
        let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &user_cfgs);
        let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
 
-       // We test config.our_to_self > BREAKDOWN_TIMEOUT is enforced in Channel::new_outbound()
-       if let Err(error) = Channel::new_outbound(&LowerBoundedFeeEstimator::new(&test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) }),
+       // We test config.our_to_self > BREAKDOWN_TIMEOUT is enforced in OutboundV1Channel::new()
+       if let Err(error) = OutboundV1Channel::new(&LowerBoundedFeeEstimator::new(&test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) }),
                &nodes[0].keys_manager, &nodes[0].keys_manager, nodes[1].node.get_our_node_id(), &nodes[1].node.init_features(), 1000000, 1000000, 0,
                &low_our_to_self_config, 0, 42)
        {
@@ -6962,11 +6972,11 @@ fn test_user_configurable_csv_delay() {
                }
        } else { assert!(false) }
 
-       // We test config.our_to_self > BREAKDOWN_TIMEOUT is enforced in Channel::new_from_req()
+       // We test config.our_to_self > BREAKDOWN_TIMEOUT is enforced in InboundV1Channel::new()
        nodes[1].node.create_channel(nodes[0].node.get_our_node_id(), 1000000, 1000000, 42, None).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(&LowerBoundedFeeEstimator::new(&test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) }),
+       if let Err(error) = InboundV1Channel::new(&LowerBoundedFeeEstimator::new(&test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) }),
                &nodes[0].keys_manager, &nodes[0].keys_manager, nodes[1].node.get_our_node_id(), &nodes[0].node.channel_type_features(), &nodes[1].node.init_features(), &open_channel, 0,
                &low_our_to_self_config, 0, &nodes[0].logger, 42)
        {
@@ -6994,11 +7004,11 @@ fn test_user_configurable_csv_delay() {
        } else { panic!(); }
        check_closed_event!(nodes[0], 1, ClosureReason::ProcessingError { err: reason_msg });
 
-       // We test msg.to_self_delay <= config.their_to_self_delay is enforced in Channel::new_from_req()
+       // We test msg.to_self_delay <= config.their_to_self_delay is enforced in InboundV1Channel::new()
        nodes[1].node.create_channel(nodes[0].node.get_our_node_id(), 1000000, 1000000, 42, None).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(&LowerBoundedFeeEstimator::new(&test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) }),
+       if let Err(error) = InboundV1Channel::new(&LowerBoundedFeeEstimator::new(&test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) }),
                &nodes[0].keys_manager, &nodes[0].keys_manager, nodes[1].node.get_our_node_id(), &nodes[0].node.channel_type_features(), &nodes[1].node.init_features(), &open_channel, 0,
                &high_their_to_self_config, 0, &nodes[0].logger, 42)
        {
@@ -7109,10 +7119,14 @@ fn test_announce_disable_channels() {
                }
        }
        // Reconnect peers
-       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: nodes[1].node.init_features(), remote_network_address: None }, true).unwrap();
+       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
+               features: nodes[1].node.init_features(), networks: None, remote_network_address: None
+       }, true).unwrap();
        let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
        assert_eq!(reestablish_1.len(), 3);
-       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: nodes[0].node.init_features(), remote_network_address: None }, false).unwrap();
+       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
+               features: nodes[0].node.init_features(), networks: None, remote_network_address: None
+       }, false).unwrap();
        let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
        assert_eq!(reestablish_2.len(), 3);
 
@@ -7618,45 +7632,6 @@ fn test_bump_txn_sanitize_tracking_maps() {
        }
 }
 
-#[test]
-fn test_pending_claimed_htlc_no_balance_underflow() {
-       // Tests that if we have a pending outbound HTLC as well as a claimed-but-not-fully-removed
-       // HTLC we will not underflow when we call `Channel::get_balance_msat()`.
-       let chanmon_cfgs = create_chanmon_cfgs(2);
-       let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
-       let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
-       let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
-       create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, 0);
-
-       let (payment_preimage, payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 1_010_000);
-       nodes[1].node.claim_funds(payment_preimage);
-       expect_payment_claimed!(nodes[1], payment_hash, 1_010_000);
-       check_added_monitors!(nodes[1], 1);
-       let fulfill_ev = 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(), &fulfill_ev.update_fulfill_htlcs[0]);
-       expect_payment_sent_without_paths!(nodes[0], payment_preimage);
-       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &fulfill_ev.commitment_signed);
-       check_added_monitors!(nodes[0], 1);
-       let (_raa, _cs) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
-
-       // At this point nodes[1] has received 1,010k msat (10k msat more than their reserve) and can
-       // send an HTLC back (though it will go in the holding cell). Send an HTLC back and check we
-       // can get our balance.
-
-       // Get a route from nodes[1] to nodes[0] by getting a route going the other way and then flip
-       // the public key of the only hop. This works around ChannelDetails not showing the
-       // almost-claimed HTLC as available balance.
-       let (mut route, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[1], 10_000);
-       route.payment_params = None; // This is all wrong, but unnecessary
-       route.paths[0].hops[0].pubkey = nodes[0].node.get_our_node_id();
-       let (_, payment_hash_2, payment_secret_2) = get_payment_preimage_hash!(nodes[0]);
-       nodes[1].node.send_payment_with_route(&route, payment_hash_2,
-               RecipientOnionFields::secret_only(payment_secret_2), PaymentId(payment_hash_2.0)).unwrap();
-
-       assert_eq!(nodes[1].node.list_channels()[0].balance_msat, 1_000_000);
-}
-
 #[test]
 fn test_channel_conf_timeout() {
        // Tests that, for inbound channels, we give up on them if the funding transaction does not
@@ -7904,7 +7879,7 @@ fn test_reject_funding_before_inbound_channel_accepted() {
        let accept_chan_msg = {
                let mut node_1_per_peer_lock;
                let mut node_1_peer_state_lock;
-               let channel =  get_channel_ref!(&nodes[1], nodes[0], node_1_per_peer_lock, node_1_peer_state_lock, temp_channel_id);
+               let channel =  get_inbound_v1_channel_ref!(&nodes[1], nodes[0], node_1_per_peer_lock, node_1_peer_state_lock, temp_channel_id);
                channel.get_accept_channel_message()
        };
        nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_chan_msg);
@@ -8232,67 +8207,6 @@ fn test_preimage_storage() {
        }
 }
 
-#[test]
-#[allow(deprecated)]
-fn test_secret_timeout() {
-       // Simple test of payment secret storage time outs. After
-       // `create_inbound_payment(_for_hash)_legacy` is removed, this test will be removed as well.
-       let chanmon_cfgs = create_chanmon_cfgs(2);
-       let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
-       let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
-       let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
-
-       create_announced_chan_between_nodes(&nodes, 0, 1).0.contents.short_channel_id;
-
-       let (payment_hash, payment_secret_1) = nodes[1].node.create_inbound_payment_legacy(Some(100_000), 2).unwrap();
-
-       // We should fail to register the same payment hash twice, at least until we've connected a
-       // block with time 7200 + CHAN_CONFIRM_DEPTH + 1.
-       if let Err(APIError::APIMisuseError { err }) = nodes[1].node.create_inbound_payment_for_hash_legacy(payment_hash, Some(100_000), 2) {
-               assert_eq!(err, "Duplicate payment hash");
-       } else { panic!(); }
-       let mut block = {
-               let node_1_blocks = nodes[1].blocks.lock().unwrap();
-               create_dummy_block(node_1_blocks.last().unwrap().0.block_hash(), node_1_blocks.len() as u32 + 7200, Vec::new())
-       };
-       connect_block(&nodes[1], &block);
-       if let Err(APIError::APIMisuseError { err }) = nodes[1].node.create_inbound_payment_for_hash_legacy(payment_hash, Some(100_000), 2) {
-               assert_eq!(err, "Duplicate payment hash");
-       } else { panic!(); }
-
-       // If we then connect the second block, we should be able to register the same payment hash
-       // again (this time getting a new payment secret).
-       block.header.prev_blockhash = block.header.block_hash();
-       block.header.time += 1;
-       connect_block(&nodes[1], &block);
-       let our_payment_secret = nodes[1].node.create_inbound_payment_for_hash_legacy(payment_hash, Some(100_000), 2).unwrap();
-       assert_ne!(payment_secret_1, our_payment_secret);
-
-       {
-               let (route, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[1], 100_000);
-               nodes[0].node.send_payment_with_route(&route, payment_hash,
-                       RecipientOnionFields::secret_only(our_payment_secret), PaymentId(payment_hash.0)).unwrap();
-               check_added_monitors!(nodes[0], 1);
-               let mut events = nodes[0].node.get_and_clear_pending_msg_events();
-               let mut 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]);
-               commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
-       }
-       // Note that after leaving the above scope we have no knowledge of any arguments or return
-       // values from previous calls.
-       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::PaymentClaimable { purpose: PaymentPurpose::InvoicePayment { payment_preimage, payment_secret }, .. } => {
-                       assert!(payment_preimage.is_none());
-                       assert_eq!(payment_secret, our_payment_secret);
-                       // We don't actually have the payment preimage with which to claim this payment!
-               },
-               _ => panic!("Unexpected event"),
-       }
-}
-
 #[test]
 fn test_bad_secret_hash() {
        // Simple test of unregistered payment hash/invalid payment secret handling
@@ -8964,16 +8878,16 @@ fn test_duplicate_chan_id() {
        nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id()));
        create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100000, 42); // Get and check the FundingGenerationReady event
 
-       let funding_created = {
+       let (_, funding_created) = {
                let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
                let mut a_peer_state = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
                // Once we call `get_outbound_funding_created` the channel has a duplicate channel_id as
                // another channel in the ChannelManager - an invalid state. Thus, we'd panic later when we
                // try to create another channel. Instead, we drop the channel entirely here (leaving the
                // channelmanager in a possibly nonsense state instead).
-               let mut as_chan = a_peer_state.channel_by_id.remove(&open_chan_2_msg.temporary_channel_id).unwrap();
+               let mut as_chan = a_peer_state.outbound_v1_channel_by_id.remove(&open_chan_2_msg.temporary_channel_id).unwrap();
                let logger = test_utils::TestLogger::new();
-               as_chan.get_outbound_funding_created(tx.clone(), funding_outpoint, &&logger).unwrap()
+               as_chan.get_outbound_funding_created(tx.clone(), funding_outpoint, &&logger).map_err(|_| ()).unwrap()
        };
        check_added_monitors!(nodes[0], 0);
        nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created);
@@ -9478,7 +9392,7 @@ fn test_keysend_payments_to_public_node() {
        let payer_pubkey = nodes[0].node.get_our_node_id();
        let payee_pubkey = nodes[1].node.get_our_node_id();
        let route_params = RouteParameters {
-               payment_params: PaymentParameters::for_keysend(payee_pubkey, 40),
+               payment_params: PaymentParameters::for_keysend(payee_pubkey, 40, false),
                final_value_msat: 10000,
        };
        let scorer = test_utils::TestScorer::new();
@@ -9509,7 +9423,7 @@ fn test_keysend_payments_to_private_node() {
 
        let _chan = create_chan_between_nodes(&nodes[0], &nodes[1]);
        let route_params = RouteParameters {
-               payment_params: PaymentParameters::for_keysend(payee_pubkey, 40),
+               payment_params: PaymentParameters::for_keysend(payee_pubkey, 40, false),
                final_value_msat: 10000,
        };
        let network_graph = nodes[0].network_graph.clone();
@@ -9601,7 +9515,7 @@ enum ExposureEvent {
        AtUpdateFeeOutbound,
 }
 
-fn do_test_max_dust_htlc_exposure(dust_outbound_balance: bool, exposure_breach_event: ExposureEvent, on_holder_tx: bool) {
+fn do_test_max_dust_htlc_exposure(dust_outbound_balance: bool, exposure_breach_event: ExposureEvent, on_holder_tx: bool, multiplier_dust_limit: bool) {
        // Test that we properly reject dust HTLC violating our `max_dust_htlc_exposure_msat`
        // policy.
        //
@@ -9616,7 +9530,12 @@ fn do_test_max_dust_htlc_exposure(dust_outbound_balance: bool, exposure_breach_e
 
        let chanmon_cfgs = create_chanmon_cfgs(2);
        let mut config = test_default_channel_config();
-       config.channel_config.max_dust_htlc_exposure_msat = 5_000_000; // default setting value
+       config.channel_config.max_dust_htlc_exposure = if multiplier_dust_limit {
+               // Default test fee estimator rate is 253 sat/kw, so we set the multiplier to 5_000_000 / 253
+               // to get roughly the same initial value as the default setting when this test was
+               // originally written.
+               MaxDustHTLCExposure::FeeRateMultiplier(5_000_000 / 253)
+       } else { MaxDustHTLCExposure::FixedLimitMsat(5_000_000) }; // initial default setting value
        let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
        let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(config), None]);
        let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
@@ -9632,15 +9551,15 @@ fn do_test_max_dust_htlc_exposure(dust_outbound_balance: bool, exposure_breach_e
        let mut accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
        nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel);
 
-       let opt_anchors = false;
+       let channel_type_features = ChannelTypeFeatures::only_static_remote_key();
 
        let (temporary_channel_id, tx, _) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 1_000_000, 42);
 
        if on_holder_tx {
                let mut node_0_per_peer_lock;
                let mut node_0_peer_state_lock;
-               let mut chan = get_channel_ref!(nodes[0], nodes[1], node_0_per_peer_lock, node_0_peer_state_lock, temporary_channel_id);
-               chan.holder_dust_limit_satoshis = 546;
+               let mut chan = get_outbound_v1_channel_ref!(nodes[0], nodes[1], node_0_per_peer_lock, node_0_peer_state_lock, temporary_channel_id);
+               chan.context.holder_dust_limit_satoshis = 546;
        }
 
        nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).unwrap();
@@ -9656,20 +9575,25 @@ fn do_test_max_dust_htlc_exposure(dust_outbound_balance: bool, exposure_breach_e
        let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &channel_ready);
        update_nodes_with_chan_announce(&nodes, 0, 1, &announcement, &as_update, &bs_update);
 
-       let dust_buffer_feerate = {
+       // Fetch a route in advance as we will be unable to once we're unable to send.
+       let (mut route, payment_hash, _, payment_secret) =
+               get_route_and_payment_hash!(nodes[0], nodes[1], 1000);
+
+       let (dust_buffer_feerate, max_dust_htlc_exposure_msat) = {
                let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
                let chan_lock = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
                let chan = chan_lock.channel_by_id.get(&channel_id).unwrap();
-               chan.get_dust_buffer_feerate(None) as u64
+               (chan.context.get_dust_buffer_feerate(None) as u64,
+               chan.context.get_max_dust_htlc_exposure_msat(&LowerBoundedFeeEstimator(nodes[0].fee_estimator)))
        };
-       let dust_outbound_htlc_on_holder_tx_msat: u64 = (dust_buffer_feerate * htlc_timeout_tx_weight(opt_anchors) / 1000 + open_channel.dust_limit_satoshis - 1) * 1000;
-       let dust_outbound_htlc_on_holder_tx: u64 = config.channel_config.max_dust_htlc_exposure_msat / dust_outbound_htlc_on_holder_tx_msat;
+       let dust_outbound_htlc_on_holder_tx_msat: u64 = (dust_buffer_feerate * htlc_timeout_tx_weight(&channel_type_features) / 1000 + open_channel.dust_limit_satoshis - 1) * 1000;
+       let dust_outbound_htlc_on_holder_tx: u64 = max_dust_htlc_exposure_msat / dust_outbound_htlc_on_holder_tx_msat;
 
-       let dust_inbound_htlc_on_holder_tx_msat: u64 = (dust_buffer_feerate * htlc_success_tx_weight(opt_anchors) / 1000 + open_channel.dust_limit_satoshis - 1) * 1000;
-       let dust_inbound_htlc_on_holder_tx: u64 = config.channel_config.max_dust_htlc_exposure_msat / dust_inbound_htlc_on_holder_tx_msat;
+       let dust_inbound_htlc_on_holder_tx_msat: u64 = (dust_buffer_feerate * htlc_success_tx_weight(&channel_type_features) / 1000 + open_channel.dust_limit_satoshis - 1) * 1000;
+       let dust_inbound_htlc_on_holder_tx: u64 = max_dust_htlc_exposure_msat / dust_inbound_htlc_on_holder_tx_msat;
 
-       let dust_htlc_on_counterparty_tx: u64 = 25;
-       let dust_htlc_on_counterparty_tx_msat: u64 = config.channel_config.max_dust_htlc_exposure_msat / dust_htlc_on_counterparty_tx;
+       let dust_htlc_on_counterparty_tx: u64 = 4;
+       let dust_htlc_on_counterparty_tx_msat: u64 = max_dust_htlc_exposure_msat / dust_htlc_on_counterparty_tx;
 
        if on_holder_tx {
                if dust_outbound_balance {
@@ -9693,7 +9617,7 @@ fn do_test_max_dust_htlc_exposure(dust_outbound_balance: bool, exposure_breach_e
                if dust_outbound_balance {
                        // Outbound dust threshold: 2132 sats (`dust_buffer_feerate` * HTLC_TIMEOUT_TX_WEIGHT / 1000 + counteparty's `dust_limit_satoshis`)
                        // Outbound dust balance: 5000 sats
-                       for _ in 0..dust_htlc_on_counterparty_tx {
+                       for _ in 0..dust_htlc_on_counterparty_tx - 1 {
                                let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], dust_htlc_on_counterparty_tx_msat);
                                nodes[0].node.send_payment_with_route(&route, payment_hash,
                                        RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
@@ -9701,32 +9625,27 @@ fn do_test_max_dust_htlc_exposure(dust_outbound_balance: bool, exposure_breach_e
                } else {
                        // Inbound dust threshold: 2031 sats (`dust_buffer_feerate` * HTLC_TIMEOUT_TX_WEIGHT / 1000 + counteparty's `dust_limit_satoshis`)
                        // Inbound dust balance: 5000 sats
-                       for _ in 0..dust_htlc_on_counterparty_tx {
+                       for _ in 0..dust_htlc_on_counterparty_tx - 1 {
                                route_payment(&nodes[1], &[&nodes[0]], dust_htlc_on_counterparty_tx_msat);
                        }
                }
        }
 
-       let dust_overflow = dust_htlc_on_counterparty_tx_msat * (dust_htlc_on_counterparty_tx + 1);
        if exposure_breach_event == ExposureEvent::AtHTLCForward {
-               let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], if on_holder_tx { dust_outbound_htlc_on_holder_tx_msat } else { dust_htlc_on_counterparty_tx_msat });
-               let mut config = UserConfig::default();
+               route.paths[0].hops.last_mut().unwrap().fee_msat =
+                       if on_holder_tx { dust_outbound_htlc_on_holder_tx_msat } else { dust_htlc_on_counterparty_tx_msat + 1 };
                // With default dust exposure: 5000 sats
                if on_holder_tx {
-                       let dust_outbound_overflow = dust_outbound_htlc_on_holder_tx_msat * (dust_outbound_htlc_on_holder_tx + 1);
-                       let dust_inbound_overflow = dust_inbound_htlc_on_holder_tx_msat * dust_inbound_htlc_on_holder_tx + dust_outbound_htlc_on_holder_tx_msat;
                        unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, payment_hash,
                                        RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)
-                               ), true, APIError::ChannelUnavailable { ref err },
-                               assert_eq!(err, &format!("Cannot send value that would put our exposure to dust HTLCs at {} over the limit {} on holder commitment tx", if dust_outbound_balance { dust_outbound_overflow } else { dust_inbound_overflow }, config.channel_config.max_dust_htlc_exposure_msat)));
+                               ), true, APIError::ChannelUnavailable { .. }, {});
                } else {
                        unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, payment_hash,
                                        RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)
-                               ), true, APIError::ChannelUnavailable { ref err },
-                               assert_eq!(err, &format!("Cannot send value that would put our exposure to dust HTLCs at {} over the limit {} on counterparty commitment tx", dust_overflow, config.channel_config.max_dust_htlc_exposure_msat)));
+                               ), true, APIError::ChannelUnavailable { .. }, {});
                }
        } else if exposure_breach_event == ExposureEvent::AtHTLCReception {
-               let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], if on_holder_tx { dust_inbound_htlc_on_holder_tx_msat } else { dust_htlc_on_counterparty_tx_msat });
+               let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], if on_holder_tx { dust_inbound_htlc_on_holder_tx_msat } else { dust_htlc_on_counterparty_tx_msat + 4 });
                nodes[1].node.send_payment_with_route(&route, payment_hash,
                        RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
                check_added_monitors!(nodes[1], 1);
@@ -9739,15 +9658,24 @@ fn do_test_max_dust_htlc_exposure(dust_outbound_balance: bool, exposure_breach_e
                        // Outbound dust balance: 6399 sats
                        let dust_inbound_overflow = dust_inbound_htlc_on_holder_tx_msat * (dust_inbound_htlc_on_holder_tx + 1);
                        let dust_outbound_overflow = dust_outbound_htlc_on_holder_tx_msat * dust_outbound_htlc_on_holder_tx + dust_inbound_htlc_on_holder_tx_msat;
-                       nodes[0].logger.assert_log("lightning::ln::channel".to_string(), format!("Cannot accept value that would put our exposure to dust HTLCs at {} over the limit {} on holder commitment tx", if dust_outbound_balance { dust_outbound_overflow } else { dust_inbound_overflow }, config.channel_config.max_dust_htlc_exposure_msat), 1);
+                       nodes[0].logger.assert_log("lightning::ln::channel".to_string(), format!("Cannot accept value that would put our exposure to dust HTLCs at {} over the limit {} on holder commitment tx", if dust_outbound_balance { dust_outbound_overflow } else { dust_inbound_overflow }, max_dust_htlc_exposure_msat), 1);
                } else {
                        // Outbound dust balance: 5200 sats
-                       nodes[0].logger.assert_log("lightning::ln::channel".to_string(), format!("Cannot accept value that would put our exposure to dust HTLCs at {} over the limit {} on counterparty commitment tx", dust_overflow, config.channel_config.max_dust_htlc_exposure_msat), 1);
+                       nodes[0].logger.assert_log("lightning::ln::channel".to_string(),
+                               format!("Cannot accept value that would put our exposure to dust HTLCs at {} over the limit {} on counterparty commitment tx",
+                                       dust_htlc_on_counterparty_tx_msat * (dust_htlc_on_counterparty_tx - 1) + dust_htlc_on_counterparty_tx_msat + 4,
+                                       max_dust_htlc_exposure_msat), 1);
                }
        } else if exposure_breach_event == ExposureEvent::AtUpdateFeeOutbound {
-               let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 2_500_000);
-               nodes[0].node.send_payment_with_route(&route, payment_hash,
-                       RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
+               route.paths[0].hops.last_mut().unwrap().fee_msat = 2_500_000;
+               // For the multiplier dust exposure limit, since it scales with feerate,
+               // we need to add a lot of HTLCs that will become dust at the new feerate
+               // to cross the threshold.
+               for _ in 0..20 {
+                       let (_, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[1], Some(1_000), None);
+                       nodes[0].node.send_payment_with_route(&route, payment_hash,
+                               RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
+               }
                {
                        let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
                        *feerate_lock = *feerate_lock * 10;
@@ -9762,20 +9690,25 @@ fn do_test_max_dust_htlc_exposure(dust_outbound_balance: bool, exposure_breach_e
        added_monitors.clear();
 }
 
+fn do_test_max_dust_htlc_exposure_by_threshold_type(multiplier_dust_limit: bool) {
+       do_test_max_dust_htlc_exposure(true, ExposureEvent::AtHTLCForward, true, multiplier_dust_limit);
+       do_test_max_dust_htlc_exposure(false, ExposureEvent::AtHTLCForward, true, multiplier_dust_limit);
+       do_test_max_dust_htlc_exposure(false, ExposureEvent::AtHTLCReception, true, multiplier_dust_limit);
+       do_test_max_dust_htlc_exposure(false, ExposureEvent::AtHTLCReception, false, multiplier_dust_limit);
+       do_test_max_dust_htlc_exposure(true, ExposureEvent::AtHTLCForward, false, multiplier_dust_limit);
+       do_test_max_dust_htlc_exposure(true, ExposureEvent::AtHTLCReception, false, multiplier_dust_limit);
+       do_test_max_dust_htlc_exposure(true, ExposureEvent::AtHTLCReception, true, multiplier_dust_limit);
+       do_test_max_dust_htlc_exposure(false, ExposureEvent::AtHTLCForward, false, multiplier_dust_limit);
+       do_test_max_dust_htlc_exposure(true, ExposureEvent::AtUpdateFeeOutbound, true, multiplier_dust_limit);
+       do_test_max_dust_htlc_exposure(true, ExposureEvent::AtUpdateFeeOutbound, false, multiplier_dust_limit);
+       do_test_max_dust_htlc_exposure(false, ExposureEvent::AtUpdateFeeOutbound, false, multiplier_dust_limit);
+       do_test_max_dust_htlc_exposure(false, ExposureEvent::AtUpdateFeeOutbound, true, multiplier_dust_limit);
+}
+
 #[test]
 fn test_max_dust_htlc_exposure() {
-       do_test_max_dust_htlc_exposure(true, ExposureEvent::AtHTLCForward, true);
-       do_test_max_dust_htlc_exposure(false, ExposureEvent::AtHTLCForward, true);
-       do_test_max_dust_htlc_exposure(false, ExposureEvent::AtHTLCReception, true);
-       do_test_max_dust_htlc_exposure(false, ExposureEvent::AtHTLCReception, false);
-       do_test_max_dust_htlc_exposure(true, ExposureEvent::AtHTLCForward, false);
-       do_test_max_dust_htlc_exposure(true, ExposureEvent::AtHTLCReception, false);
-       do_test_max_dust_htlc_exposure(true, ExposureEvent::AtHTLCReception, true);
-       do_test_max_dust_htlc_exposure(false, ExposureEvent::AtHTLCForward, false);
-       do_test_max_dust_htlc_exposure(true, ExposureEvent::AtUpdateFeeOutbound, true);
-       do_test_max_dust_htlc_exposure(true, ExposureEvent::AtUpdateFeeOutbound, false);
-       do_test_max_dust_htlc_exposure(false, ExposureEvent::AtUpdateFeeOutbound, false);
-       do_test_max_dust_htlc_exposure(false, ExposureEvent::AtUpdateFeeOutbound, true);
+       do_test_max_dust_htlc_exposure_by_threshold_type(false);
+       do_test_max_dust_htlc_exposure_by_threshold_type(true);
 }
 
 #[test]
@@ -9955,3 +9888,132 @@ fn test_payment_with_custom_min_cltv_expiry_delta() {
        do_payment_with_custom_min_final_cltv_expiry(true, false);
        do_payment_with_custom_min_final_cltv_expiry(true, true);
 }
+
+#[test]
+fn test_disconnects_peer_awaiting_response_ticks() {
+       // Tests that nodes which are awaiting on a response critical for channel responsiveness
+       // disconnect their counterparty after `DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`.
+       let mut chanmon_cfgs = create_chanmon_cfgs(2);
+       let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
+       let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
+       let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
+
+       // Asserts a disconnect event is queued to the user.
+       let check_disconnect_event = |node: &Node, should_disconnect: bool| {
+               let disconnect_event = node.node.get_and_clear_pending_msg_events().iter().find_map(|event|
+                       if let MessageSendEvent::HandleError { action, .. } = event {
+                               if let msgs::ErrorAction::DisconnectPeerWithWarning { .. } = action {
+                                       Some(())
+                               } else {
+                                       None
+                               }
+                       } else {
+                               None
+                       }
+               );
+               assert_eq!(disconnect_event.is_some(), should_disconnect);
+       };
+
+       // Fires timer ticks ensuring we only attempt to disconnect peers after reaching
+       // `DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`.
+       let check_disconnect = |node: &Node| {
+               // No disconnect without any timer ticks.
+               check_disconnect_event(node, false);
+
+               // No disconnect with 1 timer tick less than required.
+               for _ in 0..DISCONNECT_PEER_AWAITING_RESPONSE_TICKS - 1 {
+                       node.node.timer_tick_occurred();
+                       check_disconnect_event(node, false);
+               }
+
+               // Disconnect after reaching the required ticks.
+               node.node.timer_tick_occurred();
+               check_disconnect_event(node, true);
+
+               // Disconnect again on the next tick if the peer hasn't been disconnected yet.
+               node.node.timer_tick_occurred();
+               check_disconnect_event(node, true);
+       };
+
+       create_chan_between_nodes(&nodes[0], &nodes[1]);
+
+       // We'll start by performing a fee update with Alice (nodes[0]) on the channel.
+       *nodes[0].fee_estimator.sat_per_kw.lock().unwrap() *= 2;
+       nodes[0].node.timer_tick_occurred();
+       check_added_monitors!(&nodes[0], 1);
+       let alice_fee_update = 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(), alice_fee_update.update_fee.as_ref().unwrap());
+       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &alice_fee_update.commitment_signed);
+       check_added_monitors!(&nodes[1], 1);
+
+       // This will prompt Bob (nodes[1]) to respond with his `CommitmentSigned` and `RevokeAndACK`.
+       let (bob_revoke_and_ack, bob_commitment_signed) = 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(), &bob_revoke_and_ack);
+       check_added_monitors!(&nodes[0], 1);
+       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bob_commitment_signed);
+       check_added_monitors(&nodes[0], 1);
+
+       // Alice then needs to send her final `RevokeAndACK` to complete the commitment dance. We
+       // pretend Bob hasn't received the message and check whether he'll disconnect Alice after
+       // reaching `DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`.
+       let alice_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
+       check_disconnect(&nodes[1]);
+
+       // Now, we'll reconnect them to test awaiting a `ChannelReestablish` message.
+       //
+       // Note that since the commitment dance didn't complete above, Alice is expected to resend her
+       // final `RevokeAndACK` to Bob to complete it.
+       nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
+       nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
+       let bob_init = msgs::Init {
+               features: nodes[1].node.init_features(), networks: None, remote_network_address: None
+       };
+       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &bob_init, true).unwrap();
+       let alice_init = msgs::Init {
+               features: nodes[0].node.init_features(), networks: None, remote_network_address: None
+       };
+       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &alice_init, true).unwrap();
+
+       // Upon reconnection, Alice sends her `ChannelReestablish` to Bob. Alice, however, hasn't
+       // received Bob's yet, so she should disconnect him after reaching
+       // `DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`.
+       let alice_channel_reestablish = get_event_msg!(
+               nodes[0], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id()
+       );
+       nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &alice_channel_reestablish);
+       check_disconnect(&nodes[0]);
+
+       // Bob now sends his `ChannelReestablish` to Alice to resume the channel and consider it "live".
+       let bob_channel_reestablish = nodes[1].node.get_and_clear_pending_msg_events().iter().find_map(|event|
+               if let MessageSendEvent::SendChannelReestablish { node_id, msg } = event {
+                       assert_eq!(*node_id, nodes[0].node.get_our_node_id());
+                       Some(msg.clone())
+               } else {
+                       None
+               }
+       ).unwrap();
+       nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bob_channel_reestablish);
+
+       // Sanity check that Alice won't disconnect Bob since she's no longer waiting for any messages.
+       for _ in 0..DISCONNECT_PEER_AWAITING_RESPONSE_TICKS {
+               nodes[0].node.timer_tick_occurred();
+               check_disconnect_event(&nodes[0], false);
+       }
+
+       // However, Bob is still waiting on Alice's `RevokeAndACK`, so he should disconnect her after
+       // reaching `DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`.
+       check_disconnect(&nodes[1]);
+
+       // Finally, have Bob process the last message.
+       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &alice_revoke_and_ack);
+       check_added_monitors(&nodes[1], 1);
+
+       // At this point, neither node should attempt to disconnect each other, since they aren't
+       // waiting on any messages.
+       for node in &nodes {
+               for _ in 0..DISCONNECT_PEER_AWAITING_RESPONSE_TICKS {
+                       node.node.timer_tick_occurred();
+                       check_disconnect_event(node, false);
+               }
+       }
+}
index 5fa39137cf4577f41f85a57b95aedf067ea43e5b..2a3f5849b95d07f46e6e393bed08d4919d3f1272 100644 (file)
@@ -9,41 +9,26 @@
 
 //! Further functional tests which test blockchain reorganizations.
 
-#[cfg(anchors)]
 use crate::sign::{ChannelSigner, EcdsaChannelSigner};
-#[cfg(anchors)]
-use crate::chain::channelmonitor::LATENCY_GRACE_PERIOD_BLOCKS;
-use crate::chain::channelmonitor::{ANTI_REORG_DELAY, Balance};
+use crate::chain::channelmonitor::{ANTI_REORG_DELAY, LATENCY_GRACE_PERIOD_BLOCKS, Balance};
 use crate::chain::transaction::OutPoint;
 use crate::chain::chaininterface::LowerBoundedFeeEstimator;
-#[cfg(anchors)]
 use crate::events::bump_transaction::BumpTransactionEvent;
 use crate::events::{Event, MessageSendEvent, MessageSendEventsProvider, ClosureReason, HTLCDestination};
 use crate::ln::channel;
-#[cfg(anchors)]
 use crate::ln::chan_utils;
-#[cfg(anchors)]
-use crate::ln::channelmanager::ChannelManager;
-use crate::ln::channelmanager::{BREAKDOWN_TIMEOUT, PaymentId, RecipientOnionFields};
+use crate::ln::channelmanager::{BREAKDOWN_TIMEOUT, ChannelManager, PaymentId, RecipientOnionFields};
 use crate::ln::msgs::ChannelMessageHandler;
-#[cfg(anchors)]
 use crate::util::config::UserConfig;
-#[cfg(anchors)]
 use crate::util::crypto::sign;
 use crate::util::ser::Writeable;
 use crate::util::test_utils;
 
-#[cfg(anchors)]
 use bitcoin::blockdata::transaction::EcdsaSighashType;
 use bitcoin::blockdata::script::Builder;
 use bitcoin::blockdata::opcodes;
-use bitcoin::secp256k1::Secp256k1;
-#[cfg(anchors)]
-use bitcoin::secp256k1::SecretKey;
-#[cfg(anchors)]
-use bitcoin::{Amount, PublicKey, Script, TxIn, TxOut, PackedLockTime, Witness};
-use bitcoin::Transaction;
-#[cfg(anchors)]
+use bitcoin::secp256k1::{Secp256k1, SecretKey};
+use bitcoin::{Amount, PublicKey, Script, Transaction, TxIn, TxOut, PackedLockTime, Witness};
 use bitcoin::util::sighash::SighashCache;
 
 use crate::prelude::*;
@@ -184,10 +169,10 @@ fn chanmon_claim_value_coop_close() {
        assert_eq!(funding_outpoint.to_channel_id(), chan_id);
 
        let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
-       let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
+       let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan_id);
 
        assert_eq!(vec![Balance::ClaimableOnChannelClose {
-                       claimable_amount_satoshis: 1_000_000 - 1_000 - chan_feerate * channel::commitment_tx_base_weight(opt_anchors) / 1000
+                       claimable_amount_satoshis: 1_000_000 - 1_000 - chan_feerate * channel::commitment_tx_base_weight(&channel_type_features) / 1000
                }],
                nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
        assert_eq!(vec![Balance::ClaimableOnChannelClose { claimable_amount_satoshis: 1_000, }],
@@ -222,7 +207,7 @@ fn chanmon_claim_value_coop_close() {
        assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
 
        assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
-                       claimable_amount_satoshis: 1_000_000 - 1_000 - chan_feerate * channel::commitment_tx_base_weight(opt_anchors) / 1000,
+                       claimable_amount_satoshis: 1_000_000 - 1_000 - chan_feerate * channel::commitment_tx_base_weight(&channel_type_features) / 1000,
                        confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
                }],
                nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
@@ -295,7 +280,7 @@ fn do_test_claim_value_force_close(prev_commitment_tx: bool) {
        let htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
 
        let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
-       let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
+       let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan_id);
 
        let remote_txn = get_local_commitment_txn!(nodes[1], chan_id);
        let sent_htlc_balance = Balance::MaybeTimeoutClaimableHTLC {
@@ -335,7 +320,7 @@ fn do_test_claim_value_force_close(prev_commitment_tx: bool) {
        // as claimable. A lists both its to-self balance and the (possibly-claimable) HTLCs.
        assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
                        claimable_amount_satoshis: 1_000_000 - 3_000 - 4_000 - 1_000 - 3 - chan_feerate *
-                               (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
+                               (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
                }, sent_htlc_balance.clone(), sent_htlc_timeout_balance.clone()]),
                sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
        assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
@@ -382,7 +367,7 @@ fn do_test_claim_value_force_close(prev_commitment_tx: bool) {
                                1_000 - // The push_msat value in satoshis
                                3 - // The dust HTLC value in satoshis
                                // The commitment transaction fee with two HTLC outputs:
-                               chan_feerate * (channel::commitment_tx_base_weight(opt_anchors) +
+                               chan_feerate * (channel::commitment_tx_base_weight(&channel_type_features) +
                                                                if prev_commitment_tx { 1 } else { 2 } *
                                                                channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
                }, sent_htlc_timeout_balance.clone()];
@@ -432,7 +417,7 @@ fn do_test_claim_value_force_close(prev_commitment_tx: bool) {
 
        assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
                        claimable_amount_satoshis: 1_000_000 - 3_000 - 4_000 - 1_000 - 3 - chan_feerate *
-                               (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
+                               (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
                        confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
                }, sent_htlc_balance.clone(), sent_htlc_timeout_balance.clone()]),
                sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
@@ -622,7 +607,7 @@ fn test_balances_on_local_commitment_htlcs() {
        expect_payment_claimed!(nodes[1], payment_hash_2, 20_000_000);
 
        let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
-       let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
+       let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan_id);
 
        // Get nodes[0]'s commitment transaction and HTLC-Timeout transactions
        let as_txn = get_local_commitment_txn!(nodes[0], chan_id);
@@ -652,7 +637,7 @@ fn test_balances_on_local_commitment_htlcs() {
 
        assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
                        claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
-                               (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
+                               (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
                        confirmation_height: node_a_commitment_claimable,
                }, htlc_balance_known_preimage.clone(), htlc_balance_unknown_preimage.clone()]),
                sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
@@ -671,7 +656,7 @@ fn test_balances_on_local_commitment_htlcs() {
        connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1);
        assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
                        claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
-                               (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
+                               (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
                        confirmation_height: node_a_commitment_claimable,
                }, htlc_balance_known_preimage.clone(), htlc_balance_unknown_preimage.clone()]),
                sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
@@ -686,7 +671,7 @@ fn test_balances_on_local_commitment_htlcs() {
        // call, as described, two hunks down.
        assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
                        claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
-                               (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
+                               (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
                        confirmation_height: node_a_commitment_claimable,
                }, Balance::ClaimableAwaitingConfirmations {
                        claimable_amount_satoshis: 10_000,
@@ -700,7 +685,7 @@ fn test_balances_on_local_commitment_htlcs() {
        expect_payment_sent!(nodes[0], payment_preimage_2);
        assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
                        claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
-                               (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
+                               (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
                        confirmation_height: node_a_commitment_claimable,
                }, Balance::ClaimableAwaitingConfirmations {
                        claimable_amount_satoshis: 10_000,
@@ -716,7 +701,7 @@ fn test_balances_on_local_commitment_htlcs() {
 
        assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
                        claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
-                               (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
+                               (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
                        confirmation_height: node_a_commitment_claimable,
                }, Balance::ClaimableAwaitingConfirmations {
                        claimable_amount_satoshis: 10_000,
@@ -767,7 +752,7 @@ fn test_no_preimage_inbound_htlc_balances() {
        let htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
 
        let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
-       let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
+       let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan_id);
 
        let a_sent_htlc_balance = Balance::MaybeTimeoutClaimableHTLC {
                claimable_amount_satoshis: 10_000,
@@ -796,7 +781,7 @@ fn test_no_preimage_inbound_htlc_balances() {
 
        assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
                        claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
-                               (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
+                               (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
                }, a_received_htlc_balance.clone(), a_sent_htlc_balance.clone()]),
                sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
 
@@ -816,7 +801,7 @@ fn test_no_preimage_inbound_htlc_balances() {
        let node_a_commitment_claimable = nodes[0].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
        let as_pre_spend_claims = sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
                        claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
-                               (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
+                               (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
                        confirmation_height: node_a_commitment_claimable,
                }, a_received_htlc_balance.clone(), a_sent_htlc_balance.clone()]);
 
@@ -888,7 +873,7 @@ fn test_no_preimage_inbound_htlc_balances() {
        let as_timeout_claimable_height = nodes[0].best_block_info().1 + (BREAKDOWN_TIMEOUT as u32) - 1;
        assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
                        claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
-                               (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
+                               (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
                        confirmation_height: node_a_commitment_claimable,
                }, a_received_htlc_balance.clone(), Balance::ClaimableAwaitingConfirmations {
                        claimable_amount_satoshis: 10_000,
@@ -899,7 +884,7 @@ fn test_no_preimage_inbound_htlc_balances() {
        mine_transaction(&nodes[0], &bs_htlc_timeout_claim[0]);
        assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
                        claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
-                               (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
+                               (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
                        confirmation_height: node_a_commitment_claimable,
                }, a_received_htlc_balance.clone(), Balance::ClaimableAwaitingConfirmations {
                        claimable_amount_satoshis: 10_000,
@@ -915,7 +900,7 @@ fn test_no_preimage_inbound_htlc_balances() {
        connect_blocks(&nodes[0], 1);
        assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
                        claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
-                               (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
+                               (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
                        confirmation_height: node_a_commitment_claimable,
                }, Balance::ClaimableAwaitingConfirmations {
                        claimable_amount_satoshis: 10_000,
@@ -1024,7 +1009,7 @@ fn do_test_revoked_counterparty_commitment_balances(confirm_htlc_spend_first: bo
 
        // Get the latest commitment transaction from A and then update the fee to revoke it
        let as_revoked_txn = get_local_commitment_txn!(nodes[0], chan_id);
-       let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
+       let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan_id);
 
        let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
 
@@ -1123,7 +1108,7 @@ fn do_test_revoked_counterparty_commitment_balances(confirm_htlc_spend_first: bo
 
        let to_self_unclaimed_balance = Balance::CounterpartyRevokedOutputClaimable {
                claimable_amount_satoshis: 1_000_000 - 100_000 - 3_000 - chan_feerate *
-                       (channel::commitment_tx_base_weight(opt_anchors) + 3 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
+                       (channel::commitment_tx_base_weight(&channel_type_features) + 3 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
        };
        let to_self_claimed_avail_height;
        let largest_htlc_unclaimed_balance = Balance::CounterpartyRevokedOutputClaimable {
@@ -1153,7 +1138,7 @@ fn do_test_revoked_counterparty_commitment_balances(confirm_htlc_spend_first: bo
        };
        let to_self_claimed_balance = Balance::ClaimableAwaitingConfirmations {
                claimable_amount_satoshis: 1_000_000 - 100_000 - 3_000 - chan_feerate *
-                       (channel::commitment_tx_base_weight(opt_anchors) + 3 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000
+                       (channel::commitment_tx_base_weight(&channel_type_features) + 3 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000
                        - chan_feerate * claim_txn[3].weight() as u64 / 1000,
                confirmation_height: to_self_claimed_avail_height,
        };
@@ -1185,7 +1170,7 @@ fn do_test_revoked_counterparty_commitment_balances(confirm_htlc_spend_first: bo
                        confirmation_height: nodes[1].best_block_info().1 + 1,
                }, Balance::ClaimableAwaitingConfirmations {
                        claimable_amount_satoshis: 1_000_000 - 100_000 - 3_000 - chan_feerate *
-                               (channel::commitment_tx_base_weight(opt_anchors) + 3 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000
+                               (channel::commitment_tx_base_weight(&channel_type_features) + 3 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000
                                - chan_feerate * claim_txn[3].weight() as u64 / 1000,
                        confirmation_height: to_self_claimed_avail_height,
                }, Balance::ClaimableAwaitingConfirmations {
@@ -1263,7 +1248,7 @@ fn test_revoked_counterparty_htlc_tx_balances() {
        claim_payment(&nodes[0], &[&nodes[1]], payment_preimage);
 
        let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
-       let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
+       let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan_id);
 
        // B will generate an HTLC-Success from its revoked commitment tx
        mine_transaction(&nodes[1], &revoked_local_txn[0]);
@@ -1311,7 +1296,7 @@ fn test_revoked_counterparty_htlc_tx_balances() {
        let as_balances = sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
                        // to_remote output in B's revoked commitment
                        claimable_amount_satoshis: 1_000_000 - 11_000 - 3_000 - chan_feerate *
-                               (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
+                               (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
                        confirmation_height: to_remote_conf_height,
                }, Balance::CounterpartyRevokedOutputClaimable {
                        // to_self output in B's revoked commitment
@@ -1342,7 +1327,7 @@ fn test_revoked_counterparty_htlc_tx_balances() {
        assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
                        // to_remote output in B's revoked commitment
                        claimable_amount_satoshis: 1_000_000 - 11_000 - 3_000 - chan_feerate *
-                               (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
+                               (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
                        confirmation_height: to_remote_conf_height,
                }, Balance::CounterpartyRevokedOutputClaimable {
                        // to_self output in B's revoked commitment
@@ -1491,7 +1476,7 @@ fn test_revoked_counterparty_aggregated_claims() {
        check_spends!(as_revoked_txn[0], funding_tx);
        check_spends!(as_revoked_txn[1], as_revoked_txn[0]); // The HTLC-Claim transaction
 
-       let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
+       let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan_id);
        let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
 
        {
@@ -1543,7 +1528,7 @@ fn test_revoked_counterparty_aggregated_claims() {
                }, Balance::CounterpartyRevokedOutputClaimable {
                        // to_self output in A's revoked commitment
                        claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
-                               (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
+                               (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
                }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
                        claimable_amount_satoshis: 4_000,
                }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
@@ -1572,7 +1557,7 @@ fn test_revoked_counterparty_aggregated_claims() {
                }, Balance::CounterpartyRevokedOutputClaimable {
                        // to_self output in A's revoked commitment
                        claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
-                               (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
+                               (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
                }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
                        claimable_amount_satoshis: 4_000,
                }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
@@ -1589,7 +1574,7 @@ fn test_revoked_counterparty_aggregated_claims() {
        assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
                        // to_self output in A's revoked commitment
                        claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
-                               (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
+                               (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
                }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
                        claimable_amount_satoshis: 4_000,
                }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
@@ -1606,7 +1591,7 @@ fn test_revoked_counterparty_aggregated_claims() {
        assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
                        // to_self output in A's revoked commitment
                        claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
-                               (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
+                               (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
                }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
                        claimable_amount_satoshis: 4_000,
                }, Balance::ClaimableAwaitingConfirmations { // HTLC 2
@@ -1621,7 +1606,7 @@ fn test_revoked_counterparty_aggregated_claims() {
        assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
                        // to_self output in A's revoked commitment
                        claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
-                               (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
+                               (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
                }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
                        claimable_amount_satoshis: 4_000,
                }]),
@@ -1731,16 +1716,12 @@ fn test_restored_packages_retry() {
 fn do_test_monitor_rebroadcast_pending_claims(anchors: bool) {
        // Test that we will retry broadcasting pending claims for a force-closed channel on every
        // `ChainMonitor::rebroadcast_pending_claims` call.
-       if anchors {
-               assert!(cfg!(anchors));
-       }
        let mut chanmon_cfgs = create_chanmon_cfgs(2);
        let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
        let mut config = test_default_channel_config();
        if anchors {
-               #[cfg(anchors)] {
-                       config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
-               }
+               config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
+               config.manually_accept_inbound_channels = true;
        }
        let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(config), Some(config)]);
        let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
@@ -1783,34 +1764,32 @@ fn do_test_monitor_rebroadcast_pending_claims(anchors: bool) {
                        };
                        #[allow(unused_assignments)]
                        let mut feerate = 0;
-                       #[cfg(anchors)] {
-                               feerate = if let Event::BumpTransaction(BumpTransactionEvent::HTLCResolution {
-                                       target_feerate_sat_per_1000_weight, mut htlc_descriptors, tx_lock_time,
-                               }) = events.pop().unwrap() {
-                                       let secp = Secp256k1::new();
-                                       assert_eq!(htlc_descriptors.len(), 1);
-                                       let descriptor = htlc_descriptors.pop().unwrap();
-                                       assert_eq!(descriptor.commitment_txid, commitment_txn[0].txid());
-                                       let htlc_output_idx = descriptor.htlc.transaction_output_index.unwrap() as usize;
-                                       assert!(htlc_output_idx < commitment_txn[0].output.len());
-                                       tx.lock_time = tx_lock_time;
-                                       // Note that we don't care about actually making the HTLC transaction meet the
-                                       // feerate for the test, we just want to make sure the feerates we receive from
-                                       // the events never decrease.
-                                       tx.input.push(descriptor.unsigned_tx_input());
-                                       let signer = nodes[0].keys_manager.derive_channel_keys(
-                                               descriptor.channel_value_satoshis, &descriptor.channel_keys_id,
-                                       );
-                                       let per_commitment_point = signer.get_per_commitment_point(
-                                               descriptor.per_commitment_number, &secp
-                                       );
-                                       tx.output.push(descriptor.tx_output(&per_commitment_point, &secp));
-                                       let our_sig = signer.sign_holder_htlc_transaction(&mut tx, 0, &descriptor, &secp).unwrap();
-                                       let witness_script = descriptor.witness_script(&per_commitment_point, &secp);
-                                       tx.input[0].witness = descriptor.tx_input_witness(&our_sig, &witness_script);
-                                       target_feerate_sat_per_1000_weight as u64
-                               } else { panic!("unexpected event"); };
-                       }
+                       feerate = if let Event::BumpTransaction(BumpTransactionEvent::HTLCResolution {
+                               target_feerate_sat_per_1000_weight, mut htlc_descriptors, tx_lock_time, ..
+                       }) = events.pop().unwrap() {
+                               let secp = Secp256k1::new();
+                               assert_eq!(htlc_descriptors.len(), 1);
+                               let descriptor = htlc_descriptors.pop().unwrap();
+                               assert_eq!(descriptor.commitment_txid, commitment_txn[0].txid());
+                               let htlc_output_idx = descriptor.htlc.transaction_output_index.unwrap() as usize;
+                               assert!(htlc_output_idx < commitment_txn[0].output.len());
+                               tx.lock_time = tx_lock_time;
+                               // Note that we don't care about actually making the HTLC transaction meet the
+                               // feerate for the test, we just want to make sure the feerates we receive from
+                               // the events never decrease.
+                               tx.input.push(descriptor.unsigned_tx_input());
+                               let signer = nodes[0].keys_manager.derive_channel_keys(
+                                       descriptor.channel_value_satoshis, &descriptor.channel_keys_id,
+                               );
+                               let per_commitment_point = signer.get_per_commitment_point(
+                                       descriptor.per_commitment_number, &secp
+                               );
+                               tx.output.push(descriptor.tx_output(&per_commitment_point, &secp));
+                               let our_sig = signer.sign_holder_htlc_transaction(&mut tx, 0, &descriptor, &secp).unwrap();
+                               let witness_script = descriptor.witness_script(&per_commitment_point, &secp);
+                               tx.input[0].witness = descriptor.tx_input_witness(&our_sig, &witness_script);
+                               target_feerate_sat_per_1000_weight as u64
+                       } else { panic!("unexpected event"); };
                        (tx, feerate)
                } else {
                        assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
@@ -1875,11 +1854,9 @@ fn do_test_monitor_rebroadcast_pending_claims(anchors: bool) {
 #[test]
 fn test_monitor_timer_based_claim() {
        do_test_monitor_rebroadcast_pending_claims(false);
-       #[cfg(anchors)]
        do_test_monitor_rebroadcast_pending_claims(true);
 }
 
-#[cfg(anchors)]
 #[test]
 fn test_yield_anchors_events() {
        // Tests that two parties supporting anchor outputs can open a channel, route payments over
@@ -1894,6 +1871,7 @@ fn test_yield_anchors_events() {
        let mut anchors_config = UserConfig::default();
        anchors_config.channel_handshake_config.announced_channel = true;
        anchors_config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
+       anchors_config.manually_accept_inbound_channels = true;
        let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(anchors_config), Some(anchors_config)]);
        let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
 
@@ -2013,7 +1991,6 @@ fn test_yield_anchors_events() {
        nodes[0].node.get_and_clear_pending_events();
 }
 
-#[cfg(anchors)]
 #[test]
 fn test_anchors_aggregated_revoked_htlc_tx() {
        // Test that `ChannelMonitor`s can properly detect and claim funds from a counterparty claiming
@@ -2027,6 +2004,7 @@ fn test_anchors_aggregated_revoked_htlc_tx() {
        let mut anchors_config = UserConfig::default();
        anchors_config.channel_handshake_config.announced_channel = true;
        anchors_config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
+       anchors_config.manually_accept_inbound_channels = true;
        let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(anchors_config), Some(anchors_config)]);
 
        let bob_persister: test_utils::TestPersister;
index 76ed56635ac6a57bd22cb82f5bba93e4c210b5c6..672c6ae6e96e8387cd840ae97f19080a5d4d8016 100644 (file)
@@ -24,6 +24,7 @@
 //! 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 bitcoin::blockdata::constants::ChainHash;
 use bitcoin::secp256k1::PublicKey;
 use bitcoin::secp256k1::ecdsa::Signature;
 use bitcoin::{secp256k1, Witness};
@@ -88,6 +89,10 @@ pub enum DecodeError {
 pub struct Init {
        /// The relevant features which the sender supports.
        pub features: InitFeatures,
+       /// Indicates chains the sender is interested in.
+       ///
+       /// If there are no common chains, the connection will be closed.
+       pub networks: Option<Vec<ChainHash>>,
        /// The receipient's network address.
        ///
        /// This adds the option to report a remote IP address back to a connecting peer using the init
@@ -605,6 +610,11 @@ pub struct UpdateAddHTLC {
        pub payment_hash: PaymentHash,
        /// The expiry height of the HTLC
        pub cltv_expiry: u32,
+       /// The extra fee skimmed by the sender of this message. See
+       /// [`ChannelConfig::accept_underpaying_htlcs`].
+       ///
+       /// [`ChannelConfig::accept_underpaying_htlcs`]: crate::util::config::ChannelConfig::accept_underpaying_htlcs
+       pub skimmed_fee_msat: Option<u64>,
        pub(crate) onion_routing_packet: OnionPacket,
 }
 
@@ -1137,6 +1147,11 @@ pub enum ErrorAction {
                /// An error message which we should make an effort to send before we disconnect.
                msg: Option<ErrorMessage>
        },
+       /// The peer did something incorrect. Tell them without closing any channels and disconnect them.
+       DisconnectPeerWithWarning {
+               /// A warning message which we should make an effort to send before we disconnect.
+               msg: WarningMessage,
+       },
        /// The peer did something harmless that we weren't able to process, just log and ignore
        // New code should *not* use this. New code must use IgnoreAndLog, below!
        IgnoreError,
@@ -1290,6 +1305,12 @@ pub trait ChannelMessageHandler : MessageSendEventsProvider {
        ///
        /// Note that this method is called before [`Self::peer_connected`].
        fn provided_init_features(&self, their_node_id: &PublicKey) -> InitFeatures;
+
+       /// Gets the genesis hashes for this `ChannelMessageHandler` indicating which chains it supports.
+       ///
+       /// If it's `None`, then no particular network chain hash compatibility will be enforced when
+       /// connecting to peers.
+       fn get_genesis_hashes(&self) -> Option<Vec<ChainHash>>;
 }
 
 /// A trait to describe an object which can receive routing messages.
@@ -1723,7 +1744,8 @@ impl Writeable for Init {
                self.features.write_up_to_13(w)?;
                self.features.write(w)?;
                encode_tlv_stream!(w, {
-                       (3, self.remote_network_address, option)
+                       (1, self.networks.as_ref().map(|n| WithoutLength(n)), option),
+                       (3, self.remote_network_address, option),
                });
                Ok(())
        }
@@ -1734,11 +1756,14 @@ impl Readable for Init {
                let global_features: InitFeatures = Readable::read(r)?;
                let features: InitFeatures = Readable::read(r)?;
                let mut remote_network_address: Option<NetAddress> = None;
+               let mut networks: Option<WithoutLength<Vec<ChainHash>>> = None;
                decode_tlv_stream!(r, {
+                       (1, networks, option),
                        (3, remote_network_address, option)
                });
                Ok(Init {
                        features: features | global_features,
+                       networks: networks.map(|n| n.0),
                        remote_network_address,
                })
        }
@@ -1883,8 +1908,10 @@ impl_writeable_msg!(UpdateAddHTLC, {
        amount_msat,
        payment_hash,
        cltv_expiry,
-       onion_routing_packet
-}, {});
+       onion_routing_packet,
+}, {
+       (65537, skimmed_fee_msat, option)
+});
 
 impl Readable for OnionMessage {
        fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
@@ -2411,6 +2438,7 @@ impl_writeable_msg!(GossipTimestampFilter, {
 
 #[cfg(test)]
 mod tests {
+       use bitcoin::blockdata::constants::ChainHash;
        use bitcoin::{Transaction, PackedLockTime, TxIn, Script, Sequence, Witness, TxOut};
        use hex;
        use crate::ln::{PaymentPreimage, PaymentHash, PaymentSecret};
@@ -3309,7 +3337,8 @@ mod tests {
                        amount_msat: 3608586615801332854,
                        payment_hash: PaymentHash([1; 32]),
                        cltv_expiry: 821716,
-                       onion_routing_packet
+                       onion_routing_packet,
+                       skimmed_fee_msat: None,
                };
                let encoded_value = update_add_htlc.encode();
                let target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020200083a840000034d32144668701144760101010101010101010101010101010101010101010101010101010101010101000c89d4ff031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010202020202020202020202020202020202020202020202020202020202020202").unwrap();
@@ -3418,27 +3447,36 @@ mod tests {
 
        #[test]
        fn encoding_init() {
+               let mainnet_hash = ChainHash::from_hex("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap();
                assert_eq!(msgs::Init {
                        features: InitFeatures::from_le_bytes(vec![0xFF, 0xFF, 0xFF]),
+                       networks: Some(vec![mainnet_hash]),
                        remote_network_address: None,
-               }.encode(), hex::decode("00023fff0003ffffff").unwrap());
+               }.encode(), hex::decode("00023fff0003ffffff01206fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap());
                assert_eq!(msgs::Init {
                        features: InitFeatures::from_le_bytes(vec![0xFF]),
+                       networks: None,
                        remote_network_address: None,
                }.encode(), hex::decode("0001ff0001ff").unwrap());
                assert_eq!(msgs::Init {
                        features: InitFeatures::from_le_bytes(vec![]),
+                       networks: Some(vec![mainnet_hash]),
                        remote_network_address: None,
-               }.encode(), hex::decode("00000000").unwrap());
-
+               }.encode(), hex::decode("0000000001206fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap());
+               assert_eq!(msgs::Init {
+                       features: InitFeatures::from_le_bytes(vec![]),
+                       networks: Some(vec![ChainHash::from(&[1; 32][..]), ChainHash::from(&[2; 32][..])]),
+                       remote_network_address: None,
+               }.encode(), hex::decode("00000000014001010101010101010101010101010101010101010101010101010101010101010202020202020202020202020202020202020202020202020202020202020202").unwrap());
                let init_msg = msgs::Init { features: InitFeatures::from_le_bytes(vec![]),
+                       networks: Some(vec![mainnet_hash]),
                        remote_network_address: Some(msgs::NetAddress::IPv4 {
                                addr: [127, 0, 0, 1],
                                port: 1000,
                        }),
                };
                let encoded_value = init_msg.encode();
-               let target_value = hex::decode("000000000307017f00000103e8").unwrap();
+               let target_value = hex::decode("0000000001206fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d61900000000000307017f00000103e8").unwrap();
                assert_eq!(encoded_value, target_value);
                assert_eq!(msgs::Init::read(&mut Cursor::new(&target_value)).unwrap(), init_msg);
        }
index 36e1bd753e294272150a403b5d24c5bd339d819d..e5faedfe7fc39c958cc52f1c40fbcbc09bcebf27 100644 (file)
@@ -26,7 +26,7 @@ use crate::ln::msgs::{ChannelMessageHandler, ChannelUpdate};
 use crate::ln::wire::Encode;
 use crate::util::ser::{Writeable, Writer};
 use crate::util::test_utils;
-use crate::util::config::{UserConfig, ChannelConfig};
+use crate::util::config::{UserConfig, ChannelConfig, MaxDustHTLCExposure};
 use crate::util::errors::APIError;
 
 use bitcoin::hash_types::BlockHash;
@@ -510,7 +510,7 @@ fn test_onion_failure() {
        let short_channel_id = channels[1].0.contents.short_channel_id;
        let amt_to_forward = nodes[1].node.per_peer_state.read().unwrap().get(&nodes[2].node.get_our_node_id())
                .unwrap().lock().unwrap().channel_by_id.get(&channels[1].2).unwrap()
-               .get_counterparty_htlc_minimum_msat() - 1;
+               .context.get_counterparty_htlc_minimum_msat() - 1;
        let mut bogus_route = route.clone();
        let route_len = bogus_route.paths[0].hops.len();
        bogus_route.paths[0].hops[route_len-1].fee_msat = amt_to_forward;
@@ -671,6 +671,7 @@ fn do_test_onion_failure_stale_channel_update(announced_channel: bool) {
        config.channel_handshake_config.announced_channel = announced_channel;
        config.channel_handshake_limits.force_announced_channel_preference = false;
        config.accept_forwards_to_priv_channels = !announced_channel;
+       config.channel_config.max_dust_htlc_exposure = MaxDustHTLCExposure::FeeRateMultiplier(5_000_000 / 253);
        let chanmon_cfgs = create_chanmon_cfgs(3);
        let persister;
        let chain_monitor;
@@ -1371,10 +1372,19 @@ fn test_phantom_failure_too_low_recv_amt() {
 
 #[test]
 fn test_phantom_dust_exposure_failure() {
+       do_test_phantom_dust_exposure_failure(false);
+       do_test_phantom_dust_exposure_failure(true);
+}
+
+fn do_test_phantom_dust_exposure_failure(multiplier_dust_limit: bool) {
        // Set the max dust exposure to the dust limit.
        let max_dust_exposure = 546;
        let mut receiver_config = UserConfig::default();
-       receiver_config.channel_config.max_dust_htlc_exposure_msat = max_dust_exposure;
+       // Default test fee estimator rate is 253, so to set the max dust exposure to the dust limit,
+       // we need to set the multiplier to 2.
+       receiver_config.channel_config.max_dust_htlc_exposure =
+               if multiplier_dust_limit { MaxDustHTLCExposure::FeeRateMultiplier(2) }
+               else { MaxDustHTLCExposure::FixedLimitMsat(max_dust_exposure) };
        receiver_config.channel_handshake_config.announced_channel = true;
 
        let chanmon_cfgs = create_chanmon_cfgs(2);
index f107f3b558395fe7ea9da8f8a8194f8f52a9f634..546dc6c5bcd93171411263ff1c77229a08ea2ae1 100644 (file)
@@ -239,7 +239,7 @@ impl Retry {
                        },
                        #[cfg(all(not(feature = "no-std"), not(test)))]
                        (Retry::Timeout(max_duration), PaymentAttempts { first_attempted_at, .. }) =>
-                               *max_duration >= std::time::Instant::now().duration_since(*first_attempted_at),
+                               *max_duration >= crate::util::time::MonotonicTime::now().duration_since(*first_attempted_at),
                        #[cfg(all(not(feature = "no-std"), test))]
                        (Retry::Timeout(max_duration), PaymentAttempts { first_attempted_at, .. }) =>
                                *max_duration >= SinceEpoch::now().duration_since(*first_attempted_at),
@@ -274,7 +274,7 @@ pub(crate) struct PaymentAttemptsUsingTime<T: Time> {
 }
 
 #[cfg(not(any(feature = "no-std", test)))]
-type ConfiguredTime = std::time::Instant;
+type ConfiguredTime = crate::util::time::MonotonicTime;
 #[cfg(feature = "no-std")]
 type ConfiguredTime = crate::util::time::Eternity;
 #[cfg(all(not(feature = "no-std"), test))]
@@ -312,7 +312,7 @@ impl<T: Time> Display for PaymentAttemptsUsingTime<T> {
 /// [`ChannelManager::send_payment`]: crate::ln::channelmanager::ChannelManager::send_payment
 /// [`Event::PaymentPathFailed`]: crate::events::Event::PaymentPathFailed
 /// [`Event::PaymentFailed`]: crate::events::Event::PaymentFailed
-#[derive(Clone, Debug)]
+#[derive(Clone, Debug, PartialEq, Eq)]
 pub enum RetryableSendFailure {
        /// The provided [`PaymentParameters::expiry_time`] indicated that the payment has expired. Note
        /// that this error is *not* caused by [`Retry::Timeout`].
@@ -414,9 +414,9 @@ pub struct RecipientOnionFields {
        /// If you do not have one, the [`Route`] you pay over must not contain multiple paths as
        /// multi-path payments require a recipient-provided secret.
        ///
-       /// Note that for spontaneous payments most lightning nodes do not currently support MPP
-       /// receives, thus you should generally never be providing a secret here for spontaneous
-       /// payments.
+       /// Some implementations may reject spontaneous payments with payment secrets, so you may only
+       /// want to provide a secret for a spontaneous payment if MPP is needed and you know your
+       /// recipient will not reject it.
        pub payment_secret: Option<PaymentSecret>,
        /// The payment metadata serves a similar purpose as [`Self::payment_secret`] but is of
        /// arbitrary length. This gives recipients substantially more flexibility to receive
@@ -447,10 +447,13 @@ impl RecipientOnionFields {
        }
 
        /// Creates a new [`RecipientOnionFields`] with no fields. This generally does not create
-       /// payable HTLCs except for spontaneous payments, i.e. this should generally only be used for
-       /// calls to [`ChannelManager::send_spontaneous_payment`].
+       /// payable HTLCs except for single-path spontaneous payments, i.e. this should generally
+       /// only be used for calls to [`ChannelManager::send_spontaneous_payment`]. If you are sending
+       /// a spontaneous MPP this will not work as all MPP require payment secrets; you may
+       /// instead want to use [`RecipientOnionFields::secret_only`].
        ///
        /// [`ChannelManager::send_spontaneous_payment`]: super::channelmanager::ChannelManager::send_spontaneous_payment
+       /// [`RecipientOnionFields::secret_only`]: RecipientOnionFields::secret_only
        pub fn spontaneous_empty() -> Self {
                Self { payment_secret: None, payment_metadata: None }
        }
index 9e044d1c92d686479d6f76187001ac75c55d92aa..fa607f680981f3d68d81229834ad1becb6ed8b66 100644 (file)
@@ -17,13 +17,13 @@ use crate::sign::EntropySource;
 use crate::chain::transaction::OutPoint;
 use crate::events::{ClosureReason, Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider, PathFailure, PaymentFailureReason};
 use crate::ln::channel::EXPIRE_PREV_CONFIG_TICKS;
-use crate::ln::channelmanager::{BREAKDOWN_TIMEOUT, ChannelManager, MPP_TIMEOUT_TICKS, MIN_CLTV_EXPIRY_DELTA, PaymentId, PaymentSendFailure, IDEMPOTENCY_TIMEOUT_TICKS, RecentPaymentDetails, RecipientOnionFields};
+use crate::ln::channelmanager::{BREAKDOWN_TIMEOUT, ChannelManager, MPP_TIMEOUT_TICKS, MIN_CLTV_EXPIRY_DELTA, PaymentId, PaymentSendFailure, IDEMPOTENCY_TIMEOUT_TICKS, RecentPaymentDetails, RecipientOnionFields, HTLCForwardInfo, PendingHTLCRouting, PendingAddHTLCInfo};
 use crate::ln::features::InvoiceFeatures;
-use crate::ln::msgs;
+use crate::ln::{msgs, PaymentSecret, PaymentPreimage};
 use crate::ln::msgs::ChannelMessageHandler;
 use crate::ln::outbound_payment::Retry;
 use crate::routing::gossip::{EffectiveCapacity, RoutingFees};
-use crate::routing::router::{get_route, Path, PaymentParameters, Route, Router, RouteHint, RouteHintHop, RouteHop, RouteParameters};
+use crate::routing::router::{get_route, Path, PaymentParameters, Route, Router, RouteHint, RouteHintHop, RouteHop, RouteParameters, find_route};
 use crate::routing::scoring::ChannelUsage;
 use crate::util::test_utils;
 use crate::util::errors::APIError;
@@ -236,6 +236,177 @@ fn mpp_receive_timeout() {
        do_mpp_receive_timeout(false);
 }
 
+#[test]
+fn test_mpp_keysend() {
+       let mut mpp_keysend_config = test_default_channel_config();
+       mpp_keysend_config.accept_mpp_keysend = true;
+       let chanmon_cfgs = create_chanmon_cfgs(4);
+       let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
+       let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, Some(mpp_keysend_config)]);
+       let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
+
+       create_announced_chan_between_nodes(&nodes, 0, 1);
+       create_announced_chan_between_nodes(&nodes, 0, 2);
+       create_announced_chan_between_nodes(&nodes, 1, 3);
+       create_announced_chan_between_nodes(&nodes, 2, 3);
+       let network_graph = nodes[0].network_graph.clone();
+
+       let payer_pubkey = nodes[0].node.get_our_node_id();
+       let payee_pubkey = nodes[3].node.get_our_node_id();
+       let recv_value = 15_000_000;
+       let route_params = RouteParameters {
+               payment_params: PaymentParameters::for_keysend(payee_pubkey, 40, true),
+               final_value_msat: recv_value,
+       };
+       let scorer = test_utils::TestScorer::new();
+       let random_seed_bytes = chanmon_cfgs[0].keys_manager.get_secure_random_bytes();
+       let route = find_route(&payer_pubkey, &route_params, &network_graph, None, nodes[0].logger,
+               &scorer, &(), &random_seed_bytes).unwrap();
+
+       let payment_preimage = PaymentPreimage([42; 32]);
+       let payment_secret = PaymentSecret(payment_preimage.0);
+       let payment_hash = nodes[0].node.send_spontaneous_payment(&route, Some(payment_preimage),
+               RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_preimage.0)).unwrap();
+       check_added_monitors!(nodes[0], 2);
+
+       let expected_route: &[&[&Node]] = &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]];
+       let mut events = nodes[0].node.get_and_clear_pending_msg_events();
+       assert_eq!(events.len(), 2);
+
+       let ev = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
+       pass_along_path(&nodes[0], expected_route[0], recv_value, payment_hash.clone(),
+               Some(payment_secret), ev.clone(), false, Some(payment_preimage));
+
+       let ev = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut events);
+       pass_along_path(&nodes[0], expected_route[1], recv_value, payment_hash.clone(),
+               Some(payment_secret), ev.clone(), true, Some(payment_preimage));
+       claim_payment_along_route(&nodes[0], expected_route, false, payment_preimage);
+}
+
+#[test]
+fn test_reject_mpp_keysend_htlc() {
+       // This test enforces that we reject MPP keysend HTLCs if our config states we don't support
+       // MPP keysend. When receiving a payment, if we don't support MPP keysend we'll reject the
+       // payment if it's keysend and has a payment secret, never reaching our payment validation
+       // logic. To check that we enforce rejecting MPP keysends in our payment logic, here we send
+       // keysend payments without payment secrets, then modify them by adding payment secrets in the
+       // final node in between receiving the HTLCs and actually processing them.
+       let mut reject_mpp_keysend_cfg = test_default_channel_config();
+       reject_mpp_keysend_cfg.accept_mpp_keysend = false;
+
+       let chanmon_cfgs = create_chanmon_cfgs(4);
+       let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
+       let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, Some(reject_mpp_keysend_cfg)]);
+       let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
+       let chan_1_id = create_announced_chan_between_nodes(&nodes, 0, 1).0.contents.short_channel_id;
+       let chan_2_id = create_announced_chan_between_nodes(&nodes, 0, 2).0.contents.short_channel_id;
+       let chan_3_id = create_announced_chan_between_nodes(&nodes, 1, 3).0.contents.short_channel_id;
+       let (update_a, _, chan_4_channel_id, _) = create_announced_chan_between_nodes(&nodes, 2, 3);
+       let chan_4_id = update_a.contents.short_channel_id;
+       let amount = 40_000;
+       let (mut route, payment_hash, payment_preimage, _) = get_route_and_payment_hash!(nodes[0], nodes[3], amount);
+
+       // Pay along nodes[1]
+       route.paths[0].hops[0].pubkey = nodes[1].node.get_our_node_id();
+       route.paths[0].hops[0].short_channel_id = chan_1_id;
+       route.paths[0].hops[1].short_channel_id = chan_3_id;
+
+       let payment_id_0 = PaymentId(nodes[0].keys_manager.backing.get_secure_random_bytes());
+       nodes[0].node.send_spontaneous_payment(&route, Some(payment_preimage), RecipientOnionFields::spontaneous_empty(), payment_id_0).unwrap();
+       check_added_monitors!(nodes[0], 1);
+
+       let update_0 = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
+       let update_add_0 = update_0.update_add_htlcs[0].clone();
+       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &update_add_0);
+       commitment_signed_dance!(nodes[1], nodes[0], &update_0.commitment_signed, false, true);
+       expect_pending_htlcs_forwardable!(nodes[1]);
+
+       check_added_monitors!(&nodes[1], 1);
+       let update_1 = get_htlc_update_msgs!(nodes[1], nodes[3].node.get_our_node_id());
+       let update_add_1 = update_1.update_add_htlcs[0].clone();
+       nodes[3].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &update_add_1);
+       commitment_signed_dance!(nodes[3], nodes[1], update_1.commitment_signed, false, true);
+
+       assert!(nodes[3].node.get_and_clear_pending_msg_events().is_empty());
+       for (_, pending_forwards) in nodes[3].node.forward_htlcs.lock().unwrap().iter_mut() {
+               for f in pending_forwards.iter_mut() {
+                       match f {
+                               &mut HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo { ref mut forward_info, .. }) => {
+                                       match forward_info.routing {
+                                               PendingHTLCRouting::ReceiveKeysend { ref mut payment_data, .. } => {
+                                                       *payment_data = Some(msgs::FinalOnionHopData {
+                                                               payment_secret: PaymentSecret([42; 32]),
+                                                               total_msat: amount * 2,
+                                                       });
+                                               },
+                                               _ => panic!("Expected PendingHTLCRouting::ReceiveKeysend"),
+                                       }
+                               },
+                               _ => {},
+                       }
+               }
+       }
+       expect_pending_htlcs_forwardable!(nodes[3]);
+
+       // Pay along nodes[2]
+       route.paths[0].hops[0].pubkey = nodes[2].node.get_our_node_id();
+       route.paths[0].hops[0].short_channel_id = chan_2_id;
+       route.paths[0].hops[1].short_channel_id = chan_4_id;
+
+       let payment_id_1 = PaymentId(nodes[0].keys_manager.backing.get_secure_random_bytes());
+       nodes[0].node.send_spontaneous_payment(&route, Some(payment_preimage), RecipientOnionFields::spontaneous_empty(), payment_id_1).unwrap();
+       check_added_monitors!(nodes[0], 1);
+
+       let update_2 = get_htlc_update_msgs!(nodes[0], nodes[2].node.get_our_node_id());
+       let update_add_2 = update_2.update_add_htlcs[0].clone();
+       nodes[2].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &update_add_2);
+       commitment_signed_dance!(nodes[2], nodes[0], &update_2.commitment_signed, false, true);
+       expect_pending_htlcs_forwardable!(nodes[2]);
+
+       check_added_monitors!(&nodes[2], 1);
+       let update_3 = get_htlc_update_msgs!(nodes[2], nodes[3].node.get_our_node_id());
+       let update_add_3 = update_3.update_add_htlcs[0].clone();
+       nodes[3].node.handle_update_add_htlc(&nodes[2].node.get_our_node_id(), &update_add_3);
+       commitment_signed_dance!(nodes[3], nodes[2], update_3.commitment_signed, false, true);
+
+       assert!(nodes[3].node.get_and_clear_pending_msg_events().is_empty());
+       for (_, pending_forwards) in nodes[3].node.forward_htlcs.lock().unwrap().iter_mut() {
+               for f in pending_forwards.iter_mut() {
+                       match f {
+                               &mut HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo { ref mut forward_info, .. }) => {
+                                       match forward_info.routing {
+                                               PendingHTLCRouting::ReceiveKeysend { ref mut payment_data, .. } => {
+                                                       *payment_data = Some(msgs::FinalOnionHopData {
+                                                               payment_secret: PaymentSecret([42; 32]),
+                                                               total_msat: amount * 2,
+                                                       });
+                                               },
+                                               _ => panic!("Expected PendingHTLCRouting::ReceiveKeysend"),
+                                       }
+                               },
+                               _ => {},
+                       }
+               }
+       }
+       expect_pending_htlcs_forwardable!(nodes[3]);
+       check_added_monitors!(nodes[3], 1);
+
+       // Fail back along nodes[2]
+       let update_fail_0 = 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(), &update_fail_0.update_fail_htlcs[0]);
+       commitment_signed_dance!(nodes[2], nodes[3], update_fail_0.commitment_signed, false);
+       expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[2], vec![HTLCDestination::NextHopChannel { node_id: Some(nodes[3].node.get_our_node_id()), channel_id: chan_4_channel_id }]);
+       check_added_monitors!(nodes[2], 1);
+
+       let update_fail_1 = get_htlc_update_msgs!(nodes[2], nodes[0].node.get_our_node_id());
+       nodes[0].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &update_fail_1.update_fail_htlcs[0]);
+       commitment_signed_dance!(nodes[0], nodes[2], update_fail_1.commitment_signed, false);
+
+       expect_payment_failed_conditions(&nodes[0], payment_hash, true, PaymentFailedConditions::new());
+       expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[3], vec![HTLCDestination::FailedPayment { payment_hash }]);
+}
+
+
 #[test]
 fn no_pending_leak_on_initial_send_failure() {
        // In an earlier version of our payment tracking, we'd have a retry entry even when the initial
@@ -348,12 +519,16 @@ fn do_retry_with_no_persist(confirm_before_reload: bool) {
        check_added_monitors!(nodes[0], 1);
 
        nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
-       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: nodes[1].node.init_features(), remote_network_address: None }, true).unwrap();
+       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
+               features: nodes[1].node.init_features(), networks: None, remote_network_address: None
+       }, true).unwrap();
        assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
 
        // Now nodes[1] should send a channel reestablish, which nodes[0] will respond to with an
        // error, as the channel has hit the chain.
-       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: nodes[0].node.init_features(), remote_network_address: None }, false).unwrap();
+       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
+               features: nodes[0].node.init_features(), networks: None, remote_network_address: None
+       }, false).unwrap();
        let bs_reestablish = get_chan_reestablish_msgs!(nodes[1], nodes[0]).pop().unwrap();
        nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bs_reestablish);
        let as_err = nodes[0].node.get_and_clear_pending_msg_events();
@@ -432,9 +607,9 @@ fn do_retry_with_no_persist(confirm_before_reload: bool) {
                let mut peer_state = per_peer_state.get(&nodes[2].node.get_our_node_id())
                        .unwrap().lock().unwrap();
                let mut channel = peer_state.channel_by_id.get_mut(&chan_id_2).unwrap();
-               let mut new_config = channel.config();
+               let mut new_config = channel.context.config();
                new_config.forwarding_fee_base_msat += 100_000;
-               channel.update_config(&new_config);
+               channel.context.update_config(&new_config);
                new_route.paths[0].hops[0].fee_msat += 100_000;
        }
 
@@ -518,12 +693,16 @@ fn do_test_completed_payment_not_retryable_on_reload(use_dust: bool) {
        assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0).len(), 1);
        check_added_monitors!(nodes[0], 1);
 
-       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: nodes[1].node.init_features(), remote_network_address: None }, true).unwrap();
+       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
+               features: nodes[1].node.init_features(), networks: None, remote_network_address: None
+       }, true).unwrap();
        assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
 
        // Now nodes[1] should send a channel reestablish, which nodes[0] will respond to with an
        // error, as the channel has hit the chain.
-       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: nodes[0].node.init_features(), remote_network_address: None }, false).unwrap();
+       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
+               features: nodes[0].node.init_features(), networks: None, remote_network_address: None
+       }, false).unwrap();
        let bs_reestablish = get_chan_reestablish_msgs!(nodes[1], nodes[0]).pop().unwrap();
        nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bs_reestablish);
        let as_err = nodes[0].node.get_and_clear_pending_msg_events();
@@ -607,6 +786,9 @@ fn do_test_completed_payment_not_retryable_on_reload(use_dust: bool) {
        reload_node!(nodes[0], test_default_channel_config(), nodes_0_serialized, &[&chan_0_monitor_serialized, &chan_1_monitor_serialized], second_persister, second_new_chain_monitor, second_nodes_0_deserialized);
        nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
 
+       nodes[0].node.test_process_background_events();
+       check_added_monitors(&nodes[0], 1);
+
        reconnect_nodes(&nodes[0], &nodes[1], (true, true), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
 
        // Now resend the payment, delivering the HTLC and actually claiming it this time. This ensures
@@ -632,6 +814,9 @@ fn do_test_completed_payment_not_retryable_on_reload(use_dust: bool) {
        reload_node!(nodes[0], test_default_channel_config(), nodes_0_serialized, &[&chan_0_monitor_serialized, &chan_1_monitor_serialized], third_persister, third_new_chain_monitor, third_nodes_0_deserialized);
        nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
 
+       nodes[0].node.test_process_background_events();
+       check_added_monitors(&nodes[0], 1);
+
        reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
 
        match nodes[0].node.send_payment_with_route(&new_route, payment_hash, RecipientOnionFields::secret_only(payment_secret), payment_id) {
@@ -777,6 +962,7 @@ fn do_test_dup_htlc_onchain_fails_on_reload(persist_manager_post_event: bool, co
        let height = nodes[0].blocks.lock().unwrap().len() as u32 - 1;
        nodes[0].chain_monitor.chain_monitor.block_connected(&claim_block, height);
        assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
+       check_added_monitors(&nodes[0], 1);
 }
 
 #[test]
@@ -1223,7 +1409,7 @@ fn test_trivial_inflight_htlc_tracking(){
                let chan_1_used_liquidity = inflight_htlcs.used_liquidity_msat(
                        &NodeId::from_pubkey(&nodes[0].node.get_our_node_id()) ,
                        &NodeId::from_pubkey(&nodes[1].node.get_our_node_id()),
-                       channel_1.get_short_channel_id().unwrap()
+                       channel_1.context.get_short_channel_id().unwrap()
                );
                assert_eq!(chan_1_used_liquidity, None);
        }
@@ -1235,7 +1421,7 @@ fn test_trivial_inflight_htlc_tracking(){
                let chan_2_used_liquidity = inflight_htlcs.used_liquidity_msat(
                        &NodeId::from_pubkey(&nodes[1].node.get_our_node_id()) ,
                        &NodeId::from_pubkey(&nodes[2].node.get_our_node_id()),
-                       channel_2.get_short_channel_id().unwrap()
+                       channel_2.context.get_short_channel_id().unwrap()
                );
 
                assert_eq!(chan_2_used_liquidity, None);
@@ -1260,7 +1446,7 @@ fn test_trivial_inflight_htlc_tracking(){
                let chan_1_used_liquidity = inflight_htlcs.used_liquidity_msat(
                        &NodeId::from_pubkey(&nodes[0].node.get_our_node_id()) ,
                        &NodeId::from_pubkey(&nodes[1].node.get_our_node_id()),
-                       channel_1.get_short_channel_id().unwrap()
+                       channel_1.context.get_short_channel_id().unwrap()
                );
                // First hop accounts for expected 1000 msat fee
                assert_eq!(chan_1_used_liquidity, Some(501000));
@@ -1273,7 +1459,7 @@ fn test_trivial_inflight_htlc_tracking(){
                let chan_2_used_liquidity = inflight_htlcs.used_liquidity_msat(
                        &NodeId::from_pubkey(&nodes[1].node.get_our_node_id()) ,
                        &NodeId::from_pubkey(&nodes[2].node.get_our_node_id()),
-                       channel_2.get_short_channel_id().unwrap()
+                       channel_2.context.get_short_channel_id().unwrap()
                );
 
                assert_eq!(chan_2_used_liquidity, Some(500000));
@@ -1299,7 +1485,7 @@ fn test_trivial_inflight_htlc_tracking(){
                let chan_1_used_liquidity = inflight_htlcs.used_liquidity_msat(
                        &NodeId::from_pubkey(&nodes[0].node.get_our_node_id()) ,
                        &NodeId::from_pubkey(&nodes[1].node.get_our_node_id()),
-                       channel_1.get_short_channel_id().unwrap()
+                       channel_1.context.get_short_channel_id().unwrap()
                );
                assert_eq!(chan_1_used_liquidity, None);
        }
@@ -1311,7 +1497,7 @@ fn test_trivial_inflight_htlc_tracking(){
                let chan_2_used_liquidity = inflight_htlcs.used_liquidity_msat(
                        &NodeId::from_pubkey(&nodes[1].node.get_our_node_id()) ,
                        &NodeId::from_pubkey(&nodes[2].node.get_our_node_id()),
-                       channel_2.get_short_channel_id().unwrap()
+                       channel_2.context.get_short_channel_id().unwrap()
                );
                assert_eq!(chan_2_used_liquidity, None);
        }
@@ -1352,7 +1538,7 @@ fn test_holding_cell_inflight_htlcs() {
                let used_liquidity = inflight_htlcs.used_liquidity_msat(
                        &NodeId::from_pubkey(&nodes[0].node.get_our_node_id()) ,
                        &NodeId::from_pubkey(&nodes[1].node.get_our_node_id()),
-                       channel.get_short_channel_id().unwrap()
+                       channel.context.get_short_channel_id().unwrap()
                );
 
                assert_eq!(used_liquidity, Some(2000000));
@@ -1449,7 +1635,9 @@ fn do_test_intercepted_payment(test: InterceptTest) {
 
        // Check for unknown channel id error.
        let unknown_chan_id_err = nodes[1].node.forward_intercepted_htlc(intercept_id, &[42; 32], nodes[2].node.get_our_node_id(), expected_outbound_amount_msat).unwrap_err();
-       assert_eq!(unknown_chan_id_err , APIError::ChannelUnavailable  { err: format!("Channel with id {} not found for the passed counterparty node_id {}", log_bytes!([42; 32]), nodes[2].node.get_our_node_id()) });
+       assert_eq!(unknown_chan_id_err , APIError::ChannelUnavailable  {
+               err: format!("Funded channel with id {} not found for the passed counterparty node_id {}. Channel may still be opening.",
+                       log_bytes!([42; 32]), nodes[2].node.get_our_node_id()) });
 
        if test == InterceptTest::Fail {
                // Ensure we can fail the intercepted payment back.
@@ -1473,7 +1661,9 @@ fn do_test_intercepted_payment(test: InterceptTest) {
                // Check that we'll fail as expected when sending to a channel that isn't in `ChannelReady` yet.
                let temp_chan_id = nodes[1].node.create_channel(nodes[2].node.get_our_node_id(), 100_000, 0, 42, None).unwrap();
                let unusable_chan_err = nodes[1].node.forward_intercepted_htlc(intercept_id, &temp_chan_id, nodes[2].node.get_our_node_id(), expected_outbound_amount_msat).unwrap_err();
-               assert_eq!(unusable_chan_err , APIError::ChannelUnavailable { err: format!("Channel with id {} not fully established", log_bytes!(temp_chan_id)) });
+               assert_eq!(unusable_chan_err , APIError::ChannelUnavailable {
+                       err: format!("Funded channel with id {} not found for the passed counterparty node_id {}. Channel may still be opening.",
+                               log_bytes!(temp_chan_id), nodes[2].node.get_our_node_id()) });
                assert_eq!(nodes[1].node.get_and_clear_pending_msg_events().len(), 1);
 
                // Open the just-in-time channel so the payment can then be forwarded.
@@ -1546,6 +1736,133 @@ fn do_test_intercepted_payment(test: InterceptTest) {
        }
 }
 
+#[test]
+fn accept_underpaying_htlcs_config() {
+       do_accept_underpaying_htlcs_config(1);
+       do_accept_underpaying_htlcs_config(2);
+       do_accept_underpaying_htlcs_config(3);
+}
+
+fn do_accept_underpaying_htlcs_config(num_mpp_parts: usize) {
+       let chanmon_cfgs = create_chanmon_cfgs(3);
+       let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
+       let mut intercept_forwards_config = test_default_channel_config();
+       intercept_forwards_config.accept_intercept_htlcs = true;
+       let mut underpay_config = test_default_channel_config();
+       underpay_config.channel_config.accept_underpaying_htlcs = true;
+       let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(intercept_forwards_config), Some(underpay_config)]);
+       let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
+
+       let mut chan_ids = Vec::new();
+       for _ in 0..num_mpp_parts {
+               let _ = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000, 0);
+               let channel_id = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 2_000_000, 0).0.channel_id;
+               chan_ids.push(channel_id);
+       }
+
+       // Send the initial payment.
+       let amt_msat = 900_000;
+       let skimmed_fee_msat = 20;
+       let mut route_hints = Vec::new();
+       for _ in 0..num_mpp_parts {
+               route_hints.push(RouteHint(vec![RouteHintHop {
+                       src_node_id: nodes[1].node.get_our_node_id(),
+                       short_channel_id: nodes[1].node.get_intercept_scid(),
+                       fees: RoutingFees {
+                               base_msat: 1000,
+                               proportional_millionths: 0,
+                       },
+                       cltv_expiry_delta: MIN_CLTV_EXPIRY_DELTA,
+                       htlc_minimum_msat: None,
+                       htlc_maximum_msat: Some(amt_msat / num_mpp_parts as u64 + 5),
+               }]));
+       }
+       let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), TEST_FINAL_CLTV)
+               .with_route_hints(route_hints).unwrap()
+               .with_bolt11_features(nodes[2].node.invoice_features()).unwrap();
+       let route_params = RouteParameters {
+               payment_params,
+               final_value_msat: amt_msat,
+       };
+       let (payment_hash, payment_secret) = nodes[2].node.create_inbound_payment(Some(amt_msat), 60 * 60, None).unwrap();
+       nodes[0].node.send_payment(payment_hash, RecipientOnionFields::secret_only(payment_secret),
+               PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap();
+       check_added_monitors!(nodes[0], num_mpp_parts); // one monitor per path
+       let mut events: Vec<SendEvent> = nodes[0].node.get_and_clear_pending_msg_events().into_iter().map(|e| SendEvent::from_event(e)).collect();
+       assert_eq!(events.len(), num_mpp_parts);
+
+       // Forward the intercepted payments.
+       for (idx, ev) in events.into_iter().enumerate() {
+               nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &ev.msgs[0]);
+               do_commitment_signed_dance(&nodes[1], &nodes[0], &ev.commitment_msg, false, true);
+
+               let events = nodes[1].node.get_and_clear_pending_events();
+               assert_eq!(events.len(), 1);
+               let (intercept_id, expected_outbound_amt_msat) = match events[0] {
+                       crate::events::Event::HTLCIntercepted {
+                               intercept_id, expected_outbound_amount_msat, payment_hash: pmt_hash, ..
+                       } => {
+                               assert_eq!(pmt_hash, payment_hash);
+                               (intercept_id, expected_outbound_amount_msat)
+                       },
+                       _ => panic!()
+               };
+               nodes[1].node.forward_intercepted_htlc(intercept_id, &chan_ids[idx],
+                       nodes[2].node.get_our_node_id(), expected_outbound_amt_msat - skimmed_fee_msat).unwrap();
+               expect_pending_htlcs_forwardable!(nodes[1]);
+               let payment_event = {
+                       {
+                               let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
+                               assert_eq!(added_monitors.len(), 1);
+                               added_monitors.clear();
+                       }
+                       let mut events = nodes[1].node.get_and_clear_pending_msg_events();
+                       assert_eq!(events.len(), 1);
+                       SendEvent::from_event(events.remove(0))
+               };
+               nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
+               do_commitment_signed_dance(&nodes[2], &nodes[1], &payment_event.commitment_msg, false, true);
+               if idx == num_mpp_parts - 1 {
+                       expect_pending_htlcs_forwardable!(nodes[2]);
+               }
+       }
+
+       // Claim the payment and check that the skimmed fee is as expected.
+       let payment_preimage = nodes[2].node.get_payment_preimage(payment_hash, payment_secret).unwrap();
+       let events = nodes[2].node.get_and_clear_pending_events();
+       assert_eq!(events.len(), 1);
+       match events[0] {
+               crate::events::Event::PaymentClaimable {
+                       ref payment_hash, ref purpose, amount_msat, counterparty_skimmed_fee_msat, receiver_node_id, ..
+               } => {
+                       assert_eq!(payment_hash, payment_hash);
+                       assert_eq!(amt_msat - skimmed_fee_msat * num_mpp_parts as u64, amount_msat);
+                       assert_eq!(skimmed_fee_msat * num_mpp_parts as u64, counterparty_skimmed_fee_msat);
+                       assert_eq!(nodes[2].node.get_our_node_id(), receiver_node_id.unwrap());
+                       match purpose {
+                               crate::events::PaymentPurpose::InvoicePayment { payment_preimage: ev_payment_preimage,
+                                       payment_secret: ev_payment_secret, .. } =>
+                               {
+                                       assert_eq!(payment_preimage, ev_payment_preimage.unwrap());
+                                       assert_eq!(payment_secret, *ev_payment_secret);
+                               },
+                               _ => panic!(),
+                       }
+               },
+               _ => panic!("Unexpected event"),
+       }
+       let mut expected_paths_vecs = Vec::new();
+       let mut expected_paths = Vec::new();
+       for _ in 0..num_mpp_parts { expected_paths_vecs.push(vec!(&nodes[1], &nodes[2])); }
+       for i in 0..num_mpp_parts { expected_paths.push(&expected_paths_vecs[i][..]); }
+       let total_fee_msat = do_claim_payment_along_route_with_extra_penultimate_hop_fees(
+               &nodes[0], &expected_paths[..], &vec![skimmed_fee_msat as u32; num_mpp_parts][..], false,
+               payment_preimage);
+       // The sender doesn't know that the penultimate hop took an extra fee.
+       expect_payment_sent(&nodes[0], payment_preimage,
+               Some(Some(total_fee_msat - skimmed_fee_msat * num_mpp_parts as u64)), true);
+}
+
 #[derive(PartialEq)]
 enum AutoRetry {
        Success,
@@ -2167,12 +2484,11 @@ fn retry_multi_path_single_failed_payment() {
        assert_eq!(events.len(), 1);
        match events[0] {
                Event::PaymentPathFailed { payment_hash: ev_payment_hash, payment_failed_permanently: false,
-                       failure: PathFailure::InitialSend { err: APIError::ChannelUnavailable { err: ref err_msg }},
+                       failure: PathFailure::InitialSend { err: APIError::ChannelUnavailable { .. }},
                        short_channel_id: Some(expected_scid), .. } =>
                {
                        assert_eq!(payment_hash, ev_payment_hash);
                        assert_eq!(expected_scid, route.paths[1].hops[0].short_channel_id);
-                       assert!(err_msg.contains("max HTLC"));
                },
                _ => panic!("Unexpected event"),
        }
@@ -2242,12 +2558,11 @@ fn immediate_retry_on_failure() {
        assert_eq!(events.len(), 1);
        match events[0] {
                Event::PaymentPathFailed { payment_hash: ev_payment_hash, payment_failed_permanently: false,
-                       failure: PathFailure::InitialSend { err: APIError::ChannelUnavailable { err: ref err_msg }},
+                       failure: PathFailure::InitialSend { err: APIError::ChannelUnavailable { .. }},
                        short_channel_id: Some(expected_scid), .. } =>
                {
                        assert_eq!(payment_hash, ev_payment_hash);
                        assert_eq!(expected_scid, route.paths[1].hops[0].short_channel_id);
-                       assert!(err_msg.contains("max HTLC"));
                },
                _ => panic!("Unexpected event"),
        }
@@ -2861,6 +3176,7 @@ fn do_no_missing_sent_on_midpoint_reload(persist_manager_with_payment: bool) {
        reload_node!(nodes[0], test_default_channel_config(), &nodes[0].node.encode(), &[&chan_0_monitor_serialized], persister_c, chain_monitor_c, nodes_0_deserialized_c);
        let events = nodes[0].node.get_and_clear_pending_events();
        assert!(events.is_empty());
+       check_added_monitors(&nodes[0], 1);
 }
 
 #[test]
index a20b316eb0a93e997a162e9857ef3cd60ad02373..60230af78eff6bf0ccf062a37a68926cdb96e926 100644 (file)
@@ -15,6 +15,7 @@
 //! call into the provided message handlers (probably a ChannelManager and P2PGossipSync) with
 //! messages they should handle, and encoding/sending response messages.
 
+use bitcoin::blockdata::constants::ChainHash;
 use bitcoin::secp256k1::{self, Secp256k1, SecretKey, PublicKey};
 
 use crate::sign::{KeysManager, NodeSigner, Recipient};
@@ -26,17 +27,18 @@ use crate::ln::channelmanager::{SimpleArcChannelManager, SimpleRefChannelManager
 use crate::util::ser::{VecWriter, Writeable, Writer};
 use crate::ln::peer_channel_encryptor::{PeerChannelEncryptor,NextNoiseStep};
 use crate::ln::wire;
-use crate::ln::wire::Encode;
-use crate::onion_message::{CustomOnionMessageContents, CustomOnionMessageHandler, SimpleArcOnionMessenger, SimpleRefOnionMessenger};
+use crate::ln::wire::{Encode, Type};
+use crate::onion_message::{CustomOnionMessageContents, CustomOnionMessageHandler, OffersMessage, OffersMessageHandler, SimpleArcOnionMessenger, SimpleRefOnionMessenger};
 use crate::routing::gossip::{NetworkGraph, P2PGossipSync, NodeId, NodeAlias};
 use crate::util::atomic_counter::AtomicCounter;
 use crate::util::logger::Logger;
+use crate::util::string::PrintableString;
 
 use crate::prelude::*;
 use crate::io;
 use alloc::collections::LinkedList;
 use crate::sync::{Arc, Mutex, MutexGuard, FairRwLock};
-use core::sync::atomic::{AtomicBool, AtomicU32, Ordering};
+use core::sync::atomic::{AtomicBool, AtomicU32, AtomicI32, Ordering};
 use core::{cmp, hash, fmt, mem};
 use core::ops::Deref;
 use core::convert::Infallible;
@@ -116,9 +118,12 @@ impl OnionMessageHandler for IgnoringMessageHandler {
                InitFeatures::empty()
        }
 }
+impl OffersMessageHandler for IgnoringMessageHandler {
+       fn handle_message(&self, _msg: OffersMessage) -> Option<OffersMessage> { None }
+}
 impl CustomOnionMessageHandler for IgnoringMessageHandler {
        type CustomMessage = Infallible;
-       fn handle_custom_message(&self, _msg: Infallible) {
+       fn handle_custom_message(&self, _msg: Infallible) -> Option<Infallible> {
                // Since we always return `None` in the read the handle method should never be called.
                unreachable!();
        }
@@ -273,6 +278,13 @@ impl ChannelMessageHandler for ErroringMessageHandler {
                features
        }
 
+       fn get_genesis_hashes(&self) -> Option<Vec<ChainHash>> {
+               // We don't enforce any chains upon peer connection for `ErroringMessageHandler` and leave it up
+               // to users of `ErroringMessageHandler` to make decisions on network compatiblility.
+               // There's not really any way to pull in specific networks here, and hardcoding can cause breakages.
+               None
+       }
+
        fn handle_open_channel_v2(&self, their_node_id: &PublicKey, msg: &msgs::OpenChannelV2) {
                ErroringMessageHandler::push_error(self, their_node_id, msg.temporary_channel_id);
        }
@@ -595,7 +607,15 @@ impl Peer {
 /// issues such as overly long function definitions.
 ///
 /// This is not exported to bindings users as `Arc`s don't make sense in bindings.
-pub type SimpleArcPeerManager<SD, M, T, F, C, L> = PeerManager<SD, Arc<SimpleArcChannelManager<M, T, F, L>>, Arc<P2PGossipSync<Arc<NetworkGraph<Arc<L>>>, Arc<C>, Arc<L>>>, Arc<SimpleArcOnionMessenger<L>>, Arc<L>, IgnoringMessageHandler, Arc<KeysManager>>;
+pub type SimpleArcPeerManager<SD, M, T, F, C, L> = PeerManager<
+       SD,
+       Arc<SimpleArcChannelManager<M, T, F, L>>,
+       Arc<P2PGossipSync<Arc<NetworkGraph<Arc<L>>>, Arc<C>, Arc<L>>>,
+       Arc<SimpleArcOnionMessenger<L>>,
+       Arc<L>,
+       IgnoringMessageHandler,
+       Arc<KeysManager>
+>;
 
 /// SimpleRefPeerManager is a type alias for a PeerManager reference, and is the reference
 /// counterpart to the SimpleArcPeerManager type alias. Use this type by default when you don't
@@ -605,7 +625,17 @@ pub type SimpleArcPeerManager<SD, M, T, F, C, L> = PeerManager<SD, Arc<SimpleArc
 /// helps with issues such as long function definitions.
 ///
 /// This is not exported to bindings users as general type aliases don't make sense in bindings.
-pub type SimpleRefPeerManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k, 'l, 'm, SD, M, T, F, C, L> = PeerManager<SD, SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'm, M, T, F, L>, &'f P2PGossipSync<&'g NetworkGraph<&'f L>, &'h C, &'f L>, &'i SimpleRefOnionMessenger<'j, 'k, L>, &'f L, IgnoringMessageHandler, &'c KeysManager>;
+pub type SimpleRefPeerManager<
+       'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k, 'l, 'm, 'n, SD, M, T, F, C, L
+> = PeerManager<
+       SD,
+       &'n SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'm, M, T, F, L>,
+       &'f P2PGossipSync<&'g NetworkGraph<&'f L>, &'h C, &'f L>,
+       &'i SimpleRefOnionMessenger<'g, 'm, 'n, L>,
+       &'f L,
+       IgnoringMessageHandler,
+       &'c KeysManager
+>;
 
 
 /// A generic trait which is implemented for all [`PeerManager`]s. This makes bounding functions or
@@ -696,15 +726,18 @@ pub struct PeerManager<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: D
        /// lock held. Entries may be added with only the `peers` read lock held (though the
        /// `Descriptor` value must already exist in `peers`).
        node_id_to_descriptor: Mutex<HashMap<PublicKey, Descriptor>>,
-       /// We can only have one thread processing events at once, but we don't usually need the full
-       /// `peers` write lock to do so, so instead we block on this empty mutex when entering
-       /// `process_events`.
-       event_processing_lock: Mutex<()>,
-       /// Because event processing is global and always does all available work before returning,
-       /// there is no reason for us to have many event processors waiting on the lock at once.
-       /// Instead, we limit the total blocked event processors to always exactly one by setting this
-       /// when an event process call is waiting.
-       blocked_event_processors: AtomicBool,
+       /// We can only have one thread processing events at once, but if a second call to
+       /// `process_events` happens while a first call is in progress, one of the two calls needs to
+       /// start from the top to ensure any new messages are also handled.
+       ///
+       /// Because the event handler calls into user code which may block, we don't want to block a
+       /// second thread waiting for another thread to handle events which is then blocked on user
+       /// code, so we store an atomic counter here:
+       ///  * 0 indicates no event processor is running
+       ///  * 1 indicates an event processor is running
+       ///  * > 1 indicates an event processor is running but needs to start again from the top once
+       ///        it finishes as another thread tried to start processing events but returned early.
+       event_processing_state: AtomicI32,
 
        /// Used to track the last value sent in a node_announcement "timestamp" field. We ensure this
        /// value increases strictly since we don't assume access to a time source.
@@ -874,8 +907,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
                        message_handler,
                        peers: FairRwLock::new(HashMap::new()),
                        node_id_to_descriptor: Mutex::new(HashMap::new()),
-                       event_processing_lock: Mutex::new(()),
-                       blocked_event_processors: AtomicBool::new(false),
+                       event_processing_state: AtomicI32::new(0),
                        ephemeral_key_midstate,
                        peer_counter: AtomicCounter::new(),
                        gossip_processing_backlogged: AtomicBool::new(false),
@@ -1228,8 +1260,21 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
                                                                Ok(x) => x,
                                                                Err(e) => {
                                                                        match e.action {
-                                                                               msgs::ErrorAction::DisconnectPeer { msg: _ } => {
-                                                                                       //TODO: Try to push msg
+                                                                               msgs::ErrorAction::DisconnectPeer { .. } => {
+                                                                                       // We may have an `ErrorMessage` to send to the peer,
+                                                                                       // but writing to the socket while reading can lead to
+                                                                                       // re-entrant code and possibly unexpected behavior. The
+                                                                                       // message send is optimistic anyway, and in this case
+                                                                                       // we immediately disconnect the peer.
+                                                                                       log_debug!(self.logger, "Error handling message{}; disconnecting peer with: {}", OptionalFromDebugger(&peer_node_id), e.err);
+                                                                                       return Err(PeerHandleError { });
+                                                                               },
+                                                                               msgs::ErrorAction::DisconnectPeerWithWarning { .. } => {
+                                                                                       // We have a `WarningMessage` to send to the peer, but
+                                                                                       // writing to the socket while reading can lead to
+                                                                                       // re-entrant code and possibly unexpected behavior. The
+                                                                                       // message send is optimistic anyway, and in this case
+                                                                                       // we immediately disconnect the peer.
                                                                                        log_debug!(self.logger, "Error handling message{}; disconnecting peer with: {}", OptionalFromDebugger(&peer_node_id), e.err);
                                                                                        return Err(PeerHandleError { });
                                                                                },
@@ -1318,7 +1363,8 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
                                                                peer.set_their_node_id(their_node_id);
                                                                insert_node_id!();
                                                                let features = self.init_features(&their_node_id);
-                                                               let resp = msgs::Init { features, remote_network_address: filter_addresses(peer.their_net_address.clone()) };
+                                                               let networks = self.message_handler.chan_handler.get_genesis_hashes();
+                                                               let resp = msgs::Init { features, networks, remote_network_address: filter_addresses(peer.their_net_address.clone()) };
                                                                self.enqueue_message(peer, &resp);
                                                                peer.awaiting_pong_timer_tick_intervals = 0;
                                                        },
@@ -1330,7 +1376,8 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
                                                                peer.set_their_node_id(their_node_id);
                                                                insert_node_id!();
                                                                let features = self.init_features(&their_node_id);
-                                                               let resp = msgs::Init { features, remote_network_address: filter_addresses(peer.their_net_address.clone()) };
+                                                               let networks = self.message_handler.chan_handler.get_genesis_hashes();
+                                                               let resp = msgs::Init { features, networks, remote_network_address: filter_addresses(peer.their_net_address.clone()) };
                                                                self.enqueue_message(peer, &resp);
                                                                peer.awaiting_pong_timer_tick_intervals = 0;
                                                        },
@@ -1360,7 +1407,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
                                                                                Ok(x) => x,
                                                                                Err(e) => {
                                                                                        match e {
-                                                                                               // Note that to avoid recursion we never call
+                                                                                               // Note that to avoid re-entrancy we never call
                                                                                                // `do_attempt_write_data` from here, causing
                                                                                                // the messages enqueued here to not actually
                                                                                                // be sent before the peer is disconnected.
@@ -1381,9 +1428,8 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
                                                                                                        });
                                                                                                        continue;
                                                                                                }
-                                                                                               (msgs::DecodeError::UnknownRequiredFeature, ty) => {
-                                                                                                       log_gossip!(self.logger, "Received a message with an unknown required feature flag or TLV, you may want to update!");
-                                                                                                       self.enqueue_message(peer, &msgs::WarningMessage { channel_id: [0; 32], data: format!("Received an unknown required feature/TLV in message type {:?}", ty) });
+                                                                                               (msgs::DecodeError::UnknownRequiredFeature, _) => {
+                                                                                                       log_debug!(self.logger, "Received a message with an unknown required feature flag or TLV, you may want to update!");
                                                                                                        return Err(PeerHandleError { });
                                                                                                }
                                                                                                (msgs::DecodeError::UnknownVersion, _) => return Err(PeerHandleError { }),
@@ -1446,6 +1492,25 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
 
                // Need an Init as first message
                if let wire::Message::Init(msg) = message {
+                       // Check if we have any compatible chains if the `networks` field is specified.
+                       if let Some(networks) = &msg.networks {
+                               if let Some(our_chains) = self.message_handler.chan_handler.get_genesis_hashes() {
+                                       let mut have_compatible_chains = false;
+                                       'our_chains: for our_chain in our_chains.iter() {
+                                               for their_chain in networks {
+                                                       if our_chain == their_chain {
+                                                               have_compatible_chains = true;
+                                                               break 'our_chains;
+                                                       }
+                                               }
+                                       }
+                                       if !have_compatible_chains {
+                                               log_debug!(self.logger, "Peer does not support any of our supported chains");
+                                               return Err(PeerHandleError { }.into());
+                                       }
+                               }
+                       }
+
                        let our_features = self.init_features(&their_node_id);
                        if msg.features.requires_unknown_bits_from(&our_features) {
                                log_debug!(self.logger, "Peer requires features unknown to us");
@@ -1522,38 +1587,14 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
                                // Handled above
                        },
                        wire::Message::Error(msg) => {
-                               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.logger, "Got Err message from {}: {}", log_pubkey!(their_node_id), msg.data);
-                               } else {
-                                       log_debug!(self.logger, "Got Err message from {} with non-ASCII error message", log_pubkey!(their_node_id));
-                               }
+                               log_debug!(self.logger, "Got Err message from {}: {}", log_pubkey!(their_node_id), PrintableString(&msg.data));
                                self.message_handler.chan_handler.handle_error(&their_node_id, &msg);
                                if msg.channel_id == [0; 32] {
                                        return Err(PeerHandleError { }.into());
                                }
                        },
                        wire::Message::Warning(msg) => {
-                               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.logger, "Got warning message from {}: {}", log_pubkey!(their_node_id), msg.data);
-                               } else {
-                                       log_debug!(self.logger, "Got warning message from {} with non-ASCII error message", log_pubkey!(their_node_id));
-                               }
+                               log_debug!(self.logger, "Got warning message from {}: {}", log_pubkey!(their_node_id), PrintableString(&msg.data));
                        },
 
                        wire::Message::Ping(msg) => {
@@ -1814,356 +1855,364 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
        /// [`ChannelManager::process_pending_htlc_forwards`]: crate::ln::channelmanager::ChannelManager::process_pending_htlc_forwards
        /// [`send_data`]: SocketDescriptor::send_data
        pub fn process_events(&self) {
-               let mut _single_processor_lock = self.event_processing_lock.try_lock();
-               if _single_processor_lock.is_err() {
-                       // While we could wake the older sleeper here with a CV and make more even waiting
-                       // times, that would be a lot of overengineering for a simple "reduce total waiter
-                       // count" goal.
-                       match self.blocked_event_processors.compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire) {
-                               Err(val) => {
-                                       debug_assert!(val, "compare_exchange failed spuriously?");
-                                       return;
-                               },
-                               Ok(val) => {
-                                       debug_assert!(!val, "compare_exchange succeeded spuriously?");
-                                       // We're the only waiter, as the running process_events may have emptied the
-                                       // pending events "long" ago and there are new events for us to process, wait until
-                                       // its done and process any leftover events before returning.
-                                       _single_processor_lock = Ok(self.event_processing_lock.lock().unwrap());
-                                       self.blocked_event_processors.store(false, Ordering::Release);
-                               }
-                       }
+               if self.event_processing_state.fetch_add(1, Ordering::AcqRel) > 0 {
+                       // If we're not the first event processor to get here, just return early, the increment
+                       // we just did will be treated as "go around again" at the end.
+                       return;
                }
 
-               self.update_gossip_backlogged();
-               let flush_read_disabled = self.gossip_processing_backlog_lifted.swap(false, Ordering::Relaxed);
-
-               let mut peers_to_disconnect = HashMap::new();
-               let mut events_generated = self.message_handler.chan_handler.get_and_clear_pending_msg_events();
-               events_generated.append(&mut self.message_handler.route_handler.get_and_clear_pending_msg_events());
+               loop {
+                       self.update_gossip_backlogged();
+                       let flush_read_disabled = self.gossip_processing_backlog_lifted.swap(false, Ordering::Relaxed);
 
-               {
-                       // 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 peers_to_disconnect = HashMap::new();
+                       let mut events_generated = self.message_handler.chan_handler.get_and_clear_pending_msg_events();
+                       events_generated.append(&mut self.message_handler.route_handler.get_and_clear_pending_msg_events());
 
-                       let peers_lock = self.peers.read().unwrap();
-                       let peers = &*peers_lock;
-                       macro_rules! get_peer_for_forwarding {
-                               ($node_id: expr) => {
-                                       {
-                                               if peers_to_disconnect.get($node_id).is_some() {
-                                                       // If we've "disconnected" this peer, do not send to it.
-                                                       continue;
-                                               }
-                                               let descriptor_opt = self.node_id_to_descriptor.lock().unwrap().get($node_id).cloned();
-                                               match descriptor_opt {
-                                                       Some(descriptor) => match peers.get(&descriptor) {
-                                                               Some(peer_mutex) => {
-                                                                       let peer_lock = peer_mutex.lock().unwrap();
-                                                                       if !peer_lock.handshake_complete() {
+                       {
+                               // 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 peers_lock = self.peers.read().unwrap();
+                               let peers = &*peers_lock;
+                               macro_rules! get_peer_for_forwarding {
+                                       ($node_id: expr) => {
+                                               {
+                                                       if peers_to_disconnect.get($node_id).is_some() {
+                                                               // If we've "disconnected" this peer, do not send to it.
+                                                               continue;
+                                                       }
+                                                       let descriptor_opt = self.node_id_to_descriptor.lock().unwrap().get($node_id).cloned();
+                                                       match descriptor_opt {
+                                                               Some(descriptor) => match peers.get(&descriptor) {
+                                                                       Some(peer_mutex) => {
+                                                                               let peer_lock = peer_mutex.lock().unwrap();
+                                                                               if !peer_lock.handshake_complete() {
+                                                                                       continue;
+                                                                               }
+                                                                               peer_lock
+                                                                       },
+                                                                       None => {
+                                                                               debug_assert!(false, "Inconsistent peers set state!");
                                                                                continue;
                                                                        }
-                                                                       peer_lock
                                                                },
                                                                None => {
-                                                                       debug_assert!(false, "Inconsistent peers set state!");
                                                                        continue;
-                                                               }
-                                                       },
-                                                       None => {
-                                                               continue;
-                                                       },
+                                                               },
+                                                       }
                                                }
                                        }
                                }
-                       }
-                       for event in events_generated.drain(..) {
-                               match event {
-                                       MessageSendEvent::SendAcceptChannel { ref node_id, ref msg } => {
-                                               log_debug!(self.logger, "Handling SendAcceptChannel event in peer_handler for node {} for channel {}",
-                                                               log_pubkey!(node_id),
-                                                               log_bytes!(msg.temporary_channel_id));
-                                               self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
-                                       },
-                                       MessageSendEvent::SendAcceptChannelV2 { ref node_id, ref msg } => {
-                                               log_debug!(self.logger, "Handling SendAcceptChannelV2 event in peer_handler for node {} for channel {}",
-                                                               log_pubkey!(node_id),
-                                                               log_bytes!(msg.temporary_channel_id));
-                                               self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
-                                       },
-                                       MessageSendEvent::SendOpenChannel { ref node_id, ref msg } => {
-                                               log_debug!(self.logger, "Handling SendOpenChannel event in peer_handler for node {} for channel {}",
-                                                               log_pubkey!(node_id),
-                                                               log_bytes!(msg.temporary_channel_id));
-                                               self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
-                                       },
-                                       MessageSendEvent::SendOpenChannelV2 { ref node_id, ref msg } => {
-                                               log_debug!(self.logger, "Handling SendOpenChannelV2 event in peer_handler for node {} for channel {}",
-                                                               log_pubkey!(node_id),
-                                                               log_bytes!(msg.temporary_channel_id));
-                                               self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
-                                       },
-                                       MessageSendEvent::SendFundingCreated { ref node_id, ref msg } => {
-                                               log_debug!(self.logger, "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));
-                                               // TODO: If the peer is gone we should generate a DiscardFunding event
-                                               // indicating to the wallet that they should just throw away this funding transaction
-                                               self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
-                                       },
-                                       MessageSendEvent::SendFundingSigned { ref node_id, ref msg } => {
-                                               log_debug!(self.logger, "Handling SendFundingSigned event in peer_handler for node {} for channel {}",
-                                                               log_pubkey!(node_id),
-                                                               log_bytes!(msg.channel_id));
-                                               self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
-                                       },
-                                       MessageSendEvent::SendChannelReady { ref node_id, ref msg } => {
-                                               log_debug!(self.logger, "Handling SendChannelReady event in peer_handler for node {} for channel {}",
-                                                               log_pubkey!(node_id),
-                                                               log_bytes!(msg.channel_id));
-                                               self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
-                                       },
-                                       MessageSendEvent::SendTxAddInput { ref node_id, ref msg } => {
-                                               log_debug!(self.logger, "Handling SendTxAddInput event in peer_handler for node {} for channel {}",
-                                                               log_pubkey!(node_id),
-                                                               log_bytes!(msg.channel_id));
-                                               self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
-                                       },
-                                       MessageSendEvent::SendTxAddOutput { ref node_id, ref msg } => {
-                                               log_debug!(self.logger, "Handling SendTxAddOutput event in peer_handler for node {} for channel {}",
-                                                               log_pubkey!(node_id),
-                                                               log_bytes!(msg.channel_id));
-                                               self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
-                                       },
-                                       MessageSendEvent::SendTxRemoveInput { ref node_id, ref msg } => {
-                                               log_debug!(self.logger, "Handling SendTxRemoveInput event in peer_handler for node {} for channel {}",
-                                                               log_pubkey!(node_id),
-                                                               log_bytes!(msg.channel_id));
-                                               self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
-                                       },
-                                       MessageSendEvent::SendTxRemoveOutput { ref node_id, ref msg } => {
-                                               log_debug!(self.logger, "Handling SendTxRemoveOutput event in peer_handler for node {} for channel {}",
-                                                               log_pubkey!(node_id),
-                                                               log_bytes!(msg.channel_id));
-                                               self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
-                                       },
-                                       MessageSendEvent::SendTxComplete { ref node_id, ref msg } => {
-                                               log_debug!(self.logger, "Handling SendTxComplete event in peer_handler for node {} for channel {}",
-                                                               log_pubkey!(node_id),
-                                                               log_bytes!(msg.channel_id));
-                                               self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
-                                       },
-                                       MessageSendEvent::SendTxSignatures { ref node_id, ref msg } => {
-                                               log_debug!(self.logger, "Handling SendTxSignatures event in peer_handler for node {} for channel {}",
-                                                               log_pubkey!(node_id),
-                                                               log_bytes!(msg.channel_id));
-                                               self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
-                                       },
-                                       MessageSendEvent::SendTxInitRbf { ref node_id, ref msg } => {
-                                               log_debug!(self.logger, "Handling SendTxInitRbf event in peer_handler for node {} for channel {}",
-                                                               log_pubkey!(node_id),
-                                                               log_bytes!(msg.channel_id));
-                                               self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
-                                       },
-                                       MessageSendEvent::SendTxAckRbf { ref node_id, ref msg } => {
-                                               log_debug!(self.logger, "Handling SendTxAckRbf event in peer_handler for node {} for channel {}",
-                                                               log_pubkey!(node_id),
-                                                               log_bytes!(msg.channel_id));
-                                               self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
-                                       },
-                                       MessageSendEvent::SendTxAbort { ref node_id, ref msg } => {
-                                               log_debug!(self.logger, "Handling SendTxAbort event in peer_handler for node {} for channel {}",
-                                                               log_pubkey!(node_id),
-                                                               log_bytes!(msg.channel_id));
-                                               self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
-                                       },
-                                       MessageSendEvent::SendAnnouncementSignatures { ref node_id, ref msg } => {
-                                               log_debug!(self.logger, "Handling SendAnnouncementSignatures event in peer_handler for node {} for channel {})",
-                                                               log_pubkey!(node_id),
-                                                               log_bytes!(msg.channel_id));
-                                               self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
-                                       },
-                                       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_debug!(self.logger, "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 peer = get_peer_for_forwarding!(node_id);
-                                               for msg in update_add_htlcs {
-                                                       self.enqueue_message(&mut *peer, msg);
-                                               }
-                                               for msg in update_fulfill_htlcs {
-                                                       self.enqueue_message(&mut *peer, msg);
-                                               }
-                                               for msg in update_fail_htlcs {
-                                                       self.enqueue_message(&mut *peer, msg);
-                                               }
-                                               for msg in update_fail_malformed_htlcs {
-                                                       self.enqueue_message(&mut *peer, msg);
-                                               }
-                                               if let &Some(ref msg) = update_fee {
-                                                       self.enqueue_message(&mut *peer, msg);
-                                               }
-                                               self.enqueue_message(&mut *peer, commitment_signed);
-                                       },
-                                       MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
-                                               log_debug!(self.logger, "Handling SendRevokeAndACK event in peer_handler for node {} for channel {}",
-                                                               log_pubkey!(node_id),
-                                                               log_bytes!(msg.channel_id));
-                                               self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
-                                       },
-                                       MessageSendEvent::SendClosingSigned { ref node_id, ref msg } => {
-                                               log_debug!(self.logger, "Handling SendClosingSigned event in peer_handler for node {} for channel {}",
-                                                               log_pubkey!(node_id),
-                                                               log_bytes!(msg.channel_id));
-                                               self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
-                                       },
-                                       MessageSendEvent::SendShutdown { ref node_id, ref msg } => {
-                                               log_debug!(self.logger, "Handling Shutdown event in peer_handler for node {} for channel {}",
-                                                               log_pubkey!(node_id),
-                                                               log_bytes!(msg.channel_id));
-                                               self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
-                                       },
-                                       MessageSendEvent::SendChannelReestablish { ref node_id, ref msg } => {
-                                               log_debug!(self.logger, "Handling SendChannelReestablish event in peer_handler for node {} for channel {}",
-                                                               log_pubkey!(node_id),
-                                                               log_bytes!(msg.channel_id));
-                                               self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
-                                       },
-                                       MessageSendEvent::SendChannelAnnouncement { ref node_id, ref msg, ref update_msg } => {
-                                               log_debug!(self.logger, "Handling SendChannelAnnouncement event in peer_handler for node {} for short channel id {}",
-                                                               log_pubkey!(node_id),
-                                                               msg.contents.short_channel_id);
-                                               self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
-                                               self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), update_msg);
-                                       },
-                                       MessageSendEvent::BroadcastChannelAnnouncement { msg, update_msg } => {
-                                               log_debug!(self.logger, "Handling BroadcastChannelAnnouncement event in peer_handler for short channel id {}", msg.contents.short_channel_id);
-                                               match self.message_handler.route_handler.handle_channel_announcement(&msg) {
-                                                       Ok(_) | Err(LightningError { action: msgs::ErrorAction::IgnoreDuplicateGossip, .. }) =>
-                                                               self.forward_broadcast_msg(peers, &wire::Message::ChannelAnnouncement(msg), None),
-                                                       _ => {},
-                                               }
-                                               if let Some(msg) = update_msg {
+                               for event in events_generated.drain(..) {
+                                       match event {
+                                               MessageSendEvent::SendAcceptChannel { ref node_id, ref msg } => {
+                                                       log_debug!(self.logger, "Handling SendAcceptChannel event in peer_handler for node {} for channel {}",
+                                                                       log_pubkey!(node_id),
+                                                                       log_bytes!(msg.temporary_channel_id));
+                                                       self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
+                                               },
+                                               MessageSendEvent::SendAcceptChannelV2 { ref node_id, ref msg } => {
+                                                       log_debug!(self.logger, "Handling SendAcceptChannelV2 event in peer_handler for node {} for channel {}",
+                                                                       log_pubkey!(node_id),
+                                                                       log_bytes!(msg.temporary_channel_id));
+                                                       self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
+                                               },
+                                               MessageSendEvent::SendOpenChannel { ref node_id, ref msg } => {
+                                                       log_debug!(self.logger, "Handling SendOpenChannel event in peer_handler for node {} for channel {}",
+                                                                       log_pubkey!(node_id),
+                                                                       log_bytes!(msg.temporary_channel_id));
+                                                       self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
+                                               },
+                                               MessageSendEvent::SendOpenChannelV2 { ref node_id, ref msg } => {
+                                                       log_debug!(self.logger, "Handling SendOpenChannelV2 event in peer_handler for node {} for channel {}",
+                                                                       log_pubkey!(node_id),
+                                                                       log_bytes!(msg.temporary_channel_id));
+                                                       self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
+                                               },
+                                               MessageSendEvent::SendFundingCreated { ref node_id, ref msg } => {
+                                                       log_debug!(self.logger, "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));
+                                                       // TODO: If the peer is gone we should generate a DiscardFunding event
+                                                       // indicating to the wallet that they should just throw away this funding transaction
+                                                       self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
+                                               },
+                                               MessageSendEvent::SendFundingSigned { ref node_id, ref msg } => {
+                                                       log_debug!(self.logger, "Handling SendFundingSigned event in peer_handler for node {} for channel {}",
+                                                                       log_pubkey!(node_id),
+                                                                       log_bytes!(msg.channel_id));
+                                                       self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
+                                               },
+                                               MessageSendEvent::SendChannelReady { ref node_id, ref msg } => {
+                                                       log_debug!(self.logger, "Handling SendChannelReady event in peer_handler for node {} for channel {}",
+                                                                       log_pubkey!(node_id),
+                                                                       log_bytes!(msg.channel_id));
+                                                       self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
+                                               },
+                                               MessageSendEvent::SendTxAddInput { ref node_id, ref msg } => {
+                                                       log_debug!(self.logger, "Handling SendTxAddInput event in peer_handler for node {} for channel {}",
+                                                                       log_pubkey!(node_id),
+                                                                       log_bytes!(msg.channel_id));
+                                                       self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
+                                               },
+                                               MessageSendEvent::SendTxAddOutput { ref node_id, ref msg } => {
+                                                       log_debug!(self.logger, "Handling SendTxAddOutput event in peer_handler for node {} for channel {}",
+                                                                       log_pubkey!(node_id),
+                                                                       log_bytes!(msg.channel_id));
+                                                       self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
+                                               },
+                                               MessageSendEvent::SendTxRemoveInput { ref node_id, ref msg } => {
+                                                       log_debug!(self.logger, "Handling SendTxRemoveInput event in peer_handler for node {} for channel {}",
+                                                                       log_pubkey!(node_id),
+                                                                       log_bytes!(msg.channel_id));
+                                                       self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
+                                               },
+                                               MessageSendEvent::SendTxRemoveOutput { ref node_id, ref msg } => {
+                                                       log_debug!(self.logger, "Handling SendTxRemoveOutput event in peer_handler for node {} for channel {}",
+                                                                       log_pubkey!(node_id),
+                                                                       log_bytes!(msg.channel_id));
+                                                       self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
+                                               },
+                                               MessageSendEvent::SendTxComplete { ref node_id, ref msg } => {
+                                                       log_debug!(self.logger, "Handling SendTxComplete event in peer_handler for node {} for channel {}",
+                                                                       log_pubkey!(node_id),
+                                                                       log_bytes!(msg.channel_id));
+                                                       self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
+                                               },
+                                               MessageSendEvent::SendTxSignatures { ref node_id, ref msg } => {
+                                                       log_debug!(self.logger, "Handling SendTxSignatures event in peer_handler for node {} for channel {}",
+                                                                       log_pubkey!(node_id),
+                                                                       log_bytes!(msg.channel_id));
+                                                       self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
+                                               },
+                                               MessageSendEvent::SendTxInitRbf { ref node_id, ref msg } => {
+                                                       log_debug!(self.logger, "Handling SendTxInitRbf event in peer_handler for node {} for channel {}",
+                                                                       log_pubkey!(node_id),
+                                                                       log_bytes!(msg.channel_id));
+                                                       self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
+                                               },
+                                               MessageSendEvent::SendTxAckRbf { ref node_id, ref msg } => {
+                                                       log_debug!(self.logger, "Handling SendTxAckRbf event in peer_handler for node {} for channel {}",
+                                                                       log_pubkey!(node_id),
+                                                                       log_bytes!(msg.channel_id));
+                                                       self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
+                                               },
+                                               MessageSendEvent::SendTxAbort { ref node_id, ref msg } => {
+                                                       log_debug!(self.logger, "Handling SendTxAbort event in peer_handler for node {} for channel {}",
+                                                                       log_pubkey!(node_id),
+                                                                       log_bytes!(msg.channel_id));
+                                                       self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
+                                               },
+                                               MessageSendEvent::SendAnnouncementSignatures { ref node_id, ref msg } => {
+                                                       log_debug!(self.logger, "Handling SendAnnouncementSignatures event in peer_handler for node {} for channel {})",
+                                                                       log_pubkey!(node_id),
+                                                                       log_bytes!(msg.channel_id));
+                                                       self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
+                                               },
+                                               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_debug!(self.logger, "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 peer = get_peer_for_forwarding!(node_id);
+                                                       for msg in update_add_htlcs {
+                                                               self.enqueue_message(&mut *peer, msg);
+                                                       }
+                                                       for msg in update_fulfill_htlcs {
+                                                               self.enqueue_message(&mut *peer, msg);
+                                                       }
+                                                       for msg in update_fail_htlcs {
+                                                               self.enqueue_message(&mut *peer, msg);
+                                                       }
+                                                       for msg in update_fail_malformed_htlcs {
+                                                               self.enqueue_message(&mut *peer, msg);
+                                                       }
+                                                       if let &Some(ref msg) = update_fee {
+                                                               self.enqueue_message(&mut *peer, msg);
+                                                       }
+                                                       self.enqueue_message(&mut *peer, commitment_signed);
+                                               },
+                                               MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
+                                                       log_debug!(self.logger, "Handling SendRevokeAndACK event in peer_handler for node {} for channel {}",
+                                                                       log_pubkey!(node_id),
+                                                                       log_bytes!(msg.channel_id));
+                                                       self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
+                                               },
+                                               MessageSendEvent::SendClosingSigned { ref node_id, ref msg } => {
+                                                       log_debug!(self.logger, "Handling SendClosingSigned event in peer_handler for node {} for channel {}",
+                                                                       log_pubkey!(node_id),
+                                                                       log_bytes!(msg.channel_id));
+                                                       self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
+                                               },
+                                               MessageSendEvent::SendShutdown { ref node_id, ref msg } => {
+                                                       log_debug!(self.logger, "Handling Shutdown event in peer_handler for node {} for channel {}",
+                                                                       log_pubkey!(node_id),
+                                                                       log_bytes!(msg.channel_id));
+                                                       self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
+                                               },
+                                               MessageSendEvent::SendChannelReestablish { ref node_id, ref msg } => {
+                                                       log_debug!(self.logger, "Handling SendChannelReestablish event in peer_handler for node {} for channel {}",
+                                                                       log_pubkey!(node_id),
+                                                                       log_bytes!(msg.channel_id));
+                                                       self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
+                                               },
+                                               MessageSendEvent::SendChannelAnnouncement { ref node_id, ref msg, ref update_msg } => {
+                                                       log_debug!(self.logger, "Handling SendChannelAnnouncement event in peer_handler for node {} for short channel id {}",
+                                                                       log_pubkey!(node_id),
+                                                                       msg.contents.short_channel_id);
+                                                       self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
+                                                       self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), update_msg);
+                                               },
+                                               MessageSendEvent::BroadcastChannelAnnouncement { msg, update_msg } => {
+                                                       log_debug!(self.logger, "Handling BroadcastChannelAnnouncement event in peer_handler for short channel id {}", msg.contents.short_channel_id);
+                                                       match self.message_handler.route_handler.handle_channel_announcement(&msg) {
+                                                               Ok(_) | Err(LightningError { action: msgs::ErrorAction::IgnoreDuplicateGossip, .. }) =>
+                                                                       self.forward_broadcast_msg(peers, &wire::Message::ChannelAnnouncement(msg), None),
+                                                               _ => {},
+                                                       }
+                                                       if let Some(msg) = update_msg {
+                                                               match self.message_handler.route_handler.handle_channel_update(&msg) {
+                                                                       Ok(_) | Err(LightningError { action: msgs::ErrorAction::IgnoreDuplicateGossip, .. }) =>
+                                                                               self.forward_broadcast_msg(peers, &wire::Message::ChannelUpdate(msg), None),
+                                                                       _ => {},
+                                                               }
+                                                       }
+                                               },
+                                               MessageSendEvent::BroadcastChannelUpdate { msg } => {
+                                                       log_debug!(self.logger, "Handling BroadcastChannelUpdate event in peer_handler for short channel id {}", msg.contents.short_channel_id);
                                                        match self.message_handler.route_handler.handle_channel_update(&msg) {
                                                                Ok(_) | Err(LightningError { action: msgs::ErrorAction::IgnoreDuplicateGossip, .. }) =>
                                                                        self.forward_broadcast_msg(peers, &wire::Message::ChannelUpdate(msg), None),
                                                                _ => {},
                                                        }
+                                               },
+                                               MessageSendEvent::BroadcastNodeAnnouncement { msg } => {
+                                                       log_debug!(self.logger, "Handling BroadcastNodeAnnouncement event in peer_handler for node {}", msg.contents.node_id);
+                                                       match self.message_handler.route_handler.handle_node_announcement(&msg) {
+                                                               Ok(_) | Err(LightningError { action: msgs::ErrorAction::IgnoreDuplicateGossip, .. }) =>
+                                                                       self.forward_broadcast_msg(peers, &wire::Message::NodeAnnouncement(msg), None),
+                                                               _ => {},
+                                                       }
+                                               },
+                                               MessageSendEvent::SendChannelUpdate { ref node_id, ref msg } => {
+                                                       log_trace!(self.logger, "Handling SendChannelUpdate event in peer_handler for node {} for channel {}",
+                                                                       log_pubkey!(node_id), msg.contents.short_channel_id);
+                                                       self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
+                                               },
+                                               MessageSendEvent::HandleError { node_id, action } => {
+                                                       match action {
+                                                               msgs::ErrorAction::DisconnectPeer { msg } => {
+                                                                       if let Some(msg) = msg.as_ref() {
+                                                                               log_trace!(self.logger, "Handling DisconnectPeer HandleError event in peer_handler for node {} with message {}",
+                                                                                       log_pubkey!(node_id), msg.data);
+                                                                       } else {
+                                                                               log_trace!(self.logger, "Handling DisconnectPeer HandleError event in peer_handler for node {}",
+                                                                                       log_pubkey!(node_id));
+                                                                       }
+                                                                       // We do not have the peers write lock, so we just store that we're
+                                                                       // about to disconenct the peer and do it after we finish
+                                                                       // processing most messages.
+                                                                       let msg = msg.map(|msg| wire::Message::<<<CMH as core::ops::Deref>::Target as wire::CustomMessageReader>::CustomMessage>::Error(msg));
+                                                                       peers_to_disconnect.insert(node_id, msg);
+                                                               },
+                                                               msgs::ErrorAction::DisconnectPeerWithWarning { msg } => {
+                                                                       log_trace!(self.logger, "Handling DisconnectPeer HandleError event in peer_handler for node {} with message {}",
+                                                                               log_pubkey!(node_id), msg.data);
+                                                                       // We do not have the peers write lock, so we just store that we're
+                                                                       // about to disconenct the peer and do it after we finish
+                                                                       // processing most messages.
+                                                                       peers_to_disconnect.insert(node_id, Some(wire::Message::Warning(msg)));
+                                                               },
+                                                               msgs::ErrorAction::IgnoreAndLog(level) => {
+                                                                       log_given_level!(self.logger, level, "Received a HandleError event to be ignored for node {}", log_pubkey!(node_id));
+                                                               },
+                                                               msgs::ErrorAction::IgnoreDuplicateGossip => {},
+                                                               msgs::ErrorAction::IgnoreError => {
+                                                                               log_debug!(self.logger, "Received a HandleError event to be ignored for node {}", log_pubkey!(node_id));
+                                                                       },
+                                                               msgs::ErrorAction::SendErrorMessage { ref msg } => {
+                                                                       log_trace!(self.logger, "Handling SendErrorMessage HandleError event in peer_handler for node {} with message {}",
+                                                                                       log_pubkey!(node_id),
+                                                                                       msg.data);
+                                                                       self.enqueue_message(&mut *get_peer_for_forwarding!(&node_id), msg);
+                                                               },
+                                                               msgs::ErrorAction::SendWarningMessage { ref msg, ref log_level } => {
+                                                                       log_given_level!(self.logger, *log_level, "Handling SendWarningMessage HandleError event in peer_handler for node {} with message {}",
+                                                                                       log_pubkey!(node_id),
+                                                                                       msg.data);
+                                                                       self.enqueue_message(&mut *get_peer_for_forwarding!(&node_id), msg);
+                                                               },
+                                                       }
+                                               },
+                                               MessageSendEvent::SendChannelRangeQuery { ref node_id, ref msg } => {
+                                                       self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
+                                               },
+                                               MessageSendEvent::SendShortIdsQuery { ref node_id, ref msg } => {
+                                                       self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
                                                }
-                                       },
-                                       MessageSendEvent::BroadcastChannelUpdate { msg } => {
-                                               log_debug!(self.logger, "Handling BroadcastChannelUpdate event in peer_handler for short channel id {}", msg.contents.short_channel_id);
-                                               match self.message_handler.route_handler.handle_channel_update(&msg) {
-                                                       Ok(_) | Err(LightningError { action: msgs::ErrorAction::IgnoreDuplicateGossip, .. }) =>
-                                                               self.forward_broadcast_msg(peers, &wire::Message::ChannelUpdate(msg), None),
-                                                       _ => {},
-                                               }
-                                       },
-                                       MessageSendEvent::BroadcastNodeAnnouncement { msg } => {
-                                               log_debug!(self.logger, "Handling BroadcastNodeAnnouncement event in peer_handler for node {}", msg.contents.node_id);
-                                               match self.message_handler.route_handler.handle_node_announcement(&msg) {
-                                                       Ok(_) | Err(LightningError { action: msgs::ErrorAction::IgnoreDuplicateGossip, .. }) =>
-                                                               self.forward_broadcast_msg(peers, &wire::Message::NodeAnnouncement(msg), None),
-                                                       _ => {},
+                                               MessageSendEvent::SendReplyChannelRange { ref node_id, ref msg } => {
+                                                       log_gossip!(self.logger, "Handling SendReplyChannelRange event in peer_handler for node {} with num_scids={} first_blocknum={} number_of_blocks={}, sync_complete={}",
+                                                               log_pubkey!(node_id),
+                                                               msg.short_channel_ids.len(),
+                                                               msg.first_blocknum,
+                                                               msg.number_of_blocks,
+                                                               msg.sync_complete);
+                                                       self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
                                                }
-                                       },
-                                       MessageSendEvent::SendChannelUpdate { ref node_id, ref msg } => {
-                                               log_trace!(self.logger, "Handling SendChannelUpdate event in peer_handler for node {} for channel {}",
-                                                               log_pubkey!(node_id), msg.contents.short_channel_id);
-                                               self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
-                                       },
-                                       MessageSendEvent::HandleError { ref node_id, ref action } => {
-                                               match *action {
-                                                       msgs::ErrorAction::DisconnectPeer { ref msg } => {
-                                                               // We do not have the peers write lock, so we just store that we're
-                                                               // about to disconenct the peer and do it after we finish
-                                                               // processing most messages.
-                                                               peers_to_disconnect.insert(*node_id, msg.clone());
-                                                       },
-                                                       msgs::ErrorAction::IgnoreAndLog(level) => {
-                                                               log_given_level!(self.logger, level, "Received a HandleError event to be ignored for node {}", log_pubkey!(node_id));
-                                                       },
-                                                       msgs::ErrorAction::IgnoreDuplicateGossip => {},
-                                                       msgs::ErrorAction::IgnoreError => {
-                                                               log_debug!(self.logger, "Received a HandleError event to be ignored for node {}", log_pubkey!(node_id));
-                                                       },
-                                                       msgs::ErrorAction::SendErrorMessage { ref msg } => {
-                                                               log_trace!(self.logger, "Handling SendErrorMessage HandleError event in peer_handler for node {} with message {}",
-                                                                               log_pubkey!(node_id),
-                                                                               msg.data);
-                                                               self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
-                                                       },
-                                                       msgs::ErrorAction::SendWarningMessage { ref msg, ref log_level } => {
-                                                               log_given_level!(self.logger, *log_level, "Handling SendWarningMessage HandleError event in peer_handler for node {} with message {}",
-                                                                               log_pubkey!(node_id),
-                                                                               msg.data);
-                                                               self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
-                                                       },
+                                               MessageSendEvent::SendGossipTimestampFilter { ref node_id, ref msg } => {
+                                                       self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
                                                }
-                                       },
-                                       MessageSendEvent::SendChannelRangeQuery { ref node_id, ref msg } => {
-                                               self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
-                                       },
-                                       MessageSendEvent::SendShortIdsQuery { ref node_id, ref msg } => {
-                                               self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
-                                       }
-                                       MessageSendEvent::SendReplyChannelRange { ref node_id, ref msg } => {
-                                               log_gossip!(self.logger, "Handling SendReplyChannelRange event in peer_handler for node {} with num_scids={} first_blocknum={} number_of_blocks={}, sync_complete={}",
-                                                       log_pubkey!(node_id),
-                                                       msg.short_channel_ids.len(),
-                                                       msg.first_blocknum,
-                                                       msg.number_of_blocks,
-                                                       msg.sync_complete);
-                                               self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
-                                       }
-                                       MessageSendEvent::SendGossipTimestampFilter { ref node_id, ref msg } => {
-                                               self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
                                        }
                                }
-                       }
 
-                       for (node_id, msg) in self.message_handler.custom_message_handler.get_and_clear_pending_msg() {
-                               if peers_to_disconnect.get(&node_id).is_some() { continue; }
-                               self.enqueue_message(&mut *get_peer_for_forwarding!(&node_id), &msg);
-                       }
+                               for (node_id, msg) in self.message_handler.custom_message_handler.get_and_clear_pending_msg() {
+                                       if peers_to_disconnect.get(&node_id).is_some() { continue; }
+                                       self.enqueue_message(&mut *get_peer_for_forwarding!(&node_id), &msg);
+                               }
 
-                       for (descriptor, peer_mutex) in peers.iter() {
-                               let mut peer = peer_mutex.lock().unwrap();
-                               if flush_read_disabled { peer.received_channel_announce_since_backlogged = false; }
-                               self.do_attempt_write_data(&mut (*descriptor).clone(), &mut *peer, flush_read_disabled);
+                               for (descriptor, peer_mutex) in peers.iter() {
+                                       let mut peer = peer_mutex.lock().unwrap();
+                                       if flush_read_disabled { peer.received_channel_announce_since_backlogged = false; }
+                                       self.do_attempt_write_data(&mut (*descriptor).clone(), &mut *peer, flush_read_disabled);
+                               }
                        }
-               }
-               if !peers_to_disconnect.is_empty() {
-                       let mut peers_lock = self.peers.write().unwrap();
-                       let peers = &mut *peers_lock;
-                       for (node_id, msg) in peers_to_disconnect.drain() {
-                               // Note that since we are holding the peers *write* lock we can
-                               // remove from node_id_to_descriptor immediately (as no other
-                               // thread can be holding the peer lock if we have the global write
-                               // lock).
-
-                               let descriptor_opt = self.node_id_to_descriptor.lock().unwrap().remove(&node_id);
-                               if let Some(mut descriptor) = descriptor_opt {
-                                       if let Some(peer_mutex) = peers.remove(&descriptor) {
-                                               let mut peer = peer_mutex.lock().unwrap();
-                                               if let Some(msg) = msg {
-                                                       log_trace!(self.logger, "Handling DisconnectPeer HandleError event in peer_handler for node {} with message {}",
-                                                                       log_pubkey!(node_id),
-                                                                       msg.data);
-                                                       self.enqueue_message(&mut *peer, &msg);
-                                                       // 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, false);
-                                               }
-                                               self.do_disconnect(descriptor, &*peer, "DisconnectPeer HandleError");
-                                       } else { debug_assert!(false, "Missing connection for peer"); }
+                       if !peers_to_disconnect.is_empty() {
+                               let mut peers_lock = self.peers.write().unwrap();
+                               let peers = &mut *peers_lock;
+                               for (node_id, msg) in peers_to_disconnect.drain() {
+                                       // Note that since we are holding the peers *write* lock we can
+                                       // remove from node_id_to_descriptor immediately (as no other
+                                       // thread can be holding the peer lock if we have the global write
+                                       // lock).
+
+                                       let descriptor_opt = self.node_id_to_descriptor.lock().unwrap().remove(&node_id);
+                                       if let Some(mut descriptor) = descriptor_opt {
+                                               if let Some(peer_mutex) = peers.remove(&descriptor) {
+                                                       let mut peer = peer_mutex.lock().unwrap();
+                                                       if let Some(msg) = msg {
+                                                               self.enqueue_message(&mut *peer, &msg);
+                                                               // 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, false);
+                                                       }
+                                                       self.do_disconnect(descriptor, &*peer, "DisconnectPeer HandleError");
+                                               } else { debug_assert!(false, "Missing connection for peer"); }
+                                       }
                                }
                        }
+
+                       if self.event_processing_state.fetch_sub(1, Ordering::AcqRel) != 1 {
+                               // If another thread incremented the state while we were running we should go
+                               // around again, but only once.
+                               self.event_processing_state.store(1, Ordering::Release);
+                               continue;
+                       }
+                       break;
                }
        }
 
@@ -2437,6 +2486,8 @@ mod tests {
        use crate::ln::msgs::{LightningError, NetAddress};
        use crate::util::test_utils;
 
+       use bitcoin::Network;
+       use bitcoin::blockdata::constants::ChainHash;
        use bitcoin::secp256k1::{PublicKey, SecretKey};
 
        use crate::prelude::*;
@@ -2515,7 +2566,7 @@ mod tests {
                        };
                        cfgs.push(
                                PeerManagerCfg{
-                                       chan_handler: test_utils::TestChannelMessageHandler::new(),
+                                       chan_handler: test_utils::TestChannelMessageHandler::new(ChainHash::using_genesis_block(Network::Testnet)),
                                        logger: test_utils::TestLogger::new(),
                                        routing_handler: test_utils::TestRoutingMessageHandler::new(),
                                        custom_handler: TestCustomMessageHandler { features },
@@ -2527,7 +2578,7 @@ mod tests {
                cfgs
        }
 
-       fn create_incompatible_peermgr_cfgs(peer_count: usize) -> Vec<PeerManagerCfg> {
+       fn create_feature_incompatible_peermgr_cfgs(peer_count: usize) -> Vec<PeerManagerCfg> {
                let mut cfgs = Vec::new();
                for i in 0..peer_count {
                        let node_secret = SecretKey::from_slice(&[42 + i as u8; 32]).unwrap();
@@ -2538,7 +2589,27 @@ mod tests {
                        };
                        cfgs.push(
                                PeerManagerCfg{
-                                       chan_handler: test_utils::TestChannelMessageHandler::new(),
+                                       chan_handler: test_utils::TestChannelMessageHandler::new(ChainHash::using_genesis_block(Network::Testnet)),
+                                       logger: test_utils::TestLogger::new(),
+                                       routing_handler: test_utils::TestRoutingMessageHandler::new(),
+                                       custom_handler: TestCustomMessageHandler { features },
+                                       node_signer: test_utils::TestNodeSigner::new(node_secret),
+                               }
+                       );
+               }
+
+               cfgs
+       }
+
+       fn create_chain_incompatible_peermgr_cfgs(peer_count: usize) -> Vec<PeerManagerCfg> {
+               let mut cfgs = Vec::new();
+               for i in 0..peer_count {
+                       let node_secret = SecretKey::from_slice(&[42 + i as u8; 32]).unwrap();
+                       let features = InitFeatures::from_le_bytes(vec![0u8; 33]);
+                       let network = ChainHash::from(&[i as u8; 32][..]);
+                       cfgs.push(
+                               PeerManagerCfg{
+                                       chan_handler: test_utils::TestChannelMessageHandler::new(network),
                                        logger: test_utils::TestLogger::new(),
                                        routing_handler: test_utils::TestRoutingMessageHandler::new(),
                                        custom_handler: TestCustomMessageHandler { features },
@@ -2681,9 +2752,9 @@ mod tests {
        }
 
        #[test]
-       fn test_incompatible_peers() {
+       fn test_feature_incompatible_peers() {
                let cfgs = create_peermgr_cfgs(2);
-               let incompatible_cfgs = create_incompatible_peermgr_cfgs(2);
+               let incompatible_cfgs = create_feature_incompatible_peermgr_cfgs(2);
 
                let peers = create_network(2, &cfgs);
                let incompatible_peers = create_network(2, &incompatible_cfgs);
@@ -2716,6 +2787,42 @@ mod tests {
                }
        }
 
+       #[test]
+       fn test_chain_incompatible_peers() {
+               let cfgs = create_peermgr_cfgs(2);
+               let incompatible_cfgs = create_chain_incompatible_peermgr_cfgs(2);
+
+               let peers = create_network(2, &cfgs);
+               let incompatible_peers = create_network(2, &incompatible_cfgs);
+               let peer_pairs = [(&peers[0], &incompatible_peers[0]), (&incompatible_peers[1], &peers[1])];
+               for (peer_a, peer_b) in peer_pairs.iter() {
+                       let id_a = peer_a.node_signer.get_node_id(Recipient::Node).unwrap();
+                       let mut fd_a = FileDescriptor {
+                               fd: 1, outbound_data: Arc::new(Mutex::new(Vec::new())),
+                               disconnect: Arc::new(AtomicBool::new(false)),
+                       };
+                       let addr_a = NetAddress::IPv4{addr: [127, 0, 0, 1], port: 1000};
+                       let mut fd_b = FileDescriptor {
+                               fd: 1, outbound_data: Arc::new(Mutex::new(Vec::new())),
+                               disconnect: Arc::new(AtomicBool::new(false)),
+                       };
+                       let addr_b = NetAddress::IPv4{addr: [127, 0, 0, 1], port: 1001};
+                       let initial_data = peer_b.new_outbound_connection(id_a, fd_b.clone(), Some(addr_a.clone())).unwrap();
+                       peer_a.new_inbound_connection(fd_a.clone(), Some(addr_b.clone())).unwrap();
+                       assert_eq!(peer_a.read_event(&mut fd_a, &initial_data).unwrap(), false);
+                       peer_a.process_events();
+
+                       let a_data = fd_a.outbound_data.lock().unwrap().split_off(0);
+                       assert_eq!(peer_b.read_event(&mut fd_b, &a_data).unwrap(), false);
+
+                       peer_b.process_events();
+                       let b_data = fd_b.outbound_data.lock().unwrap().split_off(0);
+
+                       // Should fail because of incompatible chains
+                       assert!(peer_a.read_event(&mut fd_a, &b_data).is_err());
+               }
+       }
+
        #[test]
        fn test_disconnect_peer() {
                // Simple test which builds a network of PeerManager, connects and brings them to NoiseState::Finished and
@@ -2740,8 +2847,8 @@ mod tests {
                // Simple test which builds a network of PeerManager, connects and brings them to NoiseState::Finished and
                // push a message from one peer to another.
                let cfgs = create_peermgr_cfgs(2);
-               let a_chan_handler = test_utils::TestChannelMessageHandler::new();
-               let b_chan_handler = test_utils::TestChannelMessageHandler::new();
+               let a_chan_handler = test_utils::TestChannelMessageHandler::new(ChainHash::using_genesis_block(Network::Testnet));
+               let b_chan_handler = test_utils::TestChannelMessageHandler::new(ChainHash::using_genesis_block(Network::Testnet));
                let mut peers = create_network(2, &cfgs);
                let (fd_a, mut fd_b) = establish_connection(&peers[0], &peers[1]);
                assert_eq!(peers[0].peers.read().unwrap().len(), 1);
@@ -3004,4 +3111,53 @@ mod tests {
                // For (None)
                assert_eq!(filter_addresses(None), None);
        }
+
+       #[test]
+       #[cfg(feature = "std")]
+       fn test_process_events_multithreaded() {
+               use std::time::{Duration, Instant};
+               // Test that `process_events` getting called on multiple threads doesn't generate too many
+               // loop iterations.
+               // Each time `process_events` goes around the loop we call
+               // `get_and_clear_pending_msg_events`, which we count using the `TestMessageHandler`.
+               // Because the loop should go around once more after a call which fails to take the
+               // single-threaded lock, if we write zero to the counter before calling `process_events` we
+               // should never observe there having been more than 2 loop iterations.
+               // Further, because the last thread to exit will call `process_events` before returning, we
+               // should always have at least one count at the end.
+               let cfg = Arc::new(create_peermgr_cfgs(1));
+               // Until we have std::thread::scoped we have to unsafe { turn off the borrow checker }.
+               let peer = Arc::new(create_network(1, unsafe { &*(&*cfg as *const _) as &'static _ }).pop().unwrap());
+
+               let exit_flag = Arc::new(AtomicBool::new(false));
+               macro_rules! spawn_thread { () => { {
+                       let thread_cfg = Arc::clone(&cfg);
+                       let thread_peer = Arc::clone(&peer);
+                       let thread_exit = Arc::clone(&exit_flag);
+                       std::thread::spawn(move || {
+                               while !thread_exit.load(Ordering::Acquire) {
+                                       thread_cfg[0].chan_handler.message_fetch_counter.store(0, Ordering::Release);
+                                       thread_peer.process_events();
+                                       std::thread::sleep(Duration::from_micros(1));
+                               }
+                       })
+               } } }
+
+               let thread_a = spawn_thread!();
+               let thread_b = spawn_thread!();
+               let thread_c = spawn_thread!();
+
+               let start_time = Instant::now();
+               while start_time.elapsed() < Duration::from_millis(100) {
+                       let val = cfg[0].chan_handler.message_fetch_counter.load(Ordering::Acquire);
+                       assert!(val <= 2);
+                       std::thread::yield_now(); // Winblowz seemingly doesn't ever interrupt threads?!
+               }
+
+               exit_flag.store(true, Ordering::Release);
+               thread_a.join().unwrap();
+               thread_b.join().unwrap();
+               thread_c.join().unwrap();
+               assert!(cfg[0].chan_handler.message_fetch_counter.load(Ordering::Acquire) >= 1);
+       }
 }
index cfcc46dfedace4a7048341ad495ca437ae9b1122..2c8f824eab61512651e2a59e0f40355c8549f522 100644 (file)
@@ -21,7 +21,7 @@ use crate::ln::features::ChannelTypeFeatures;
 use crate::ln::msgs;
 use crate::ln::msgs::{ChannelMessageHandler, RoutingMessageHandler, ChannelUpdate, ErrorAction};
 use crate::ln::wire::Encode;
-use crate::util::config::UserConfig;
+use crate::util::config::{UserConfig, MaxDustHTLCExposure};
 use crate::util::ser::Writeable;
 use crate::util::test_utils;
 
@@ -101,8 +101,12 @@ fn test_priv_forwarding_rejection() {
        no_announce_cfg.accept_forwards_to_priv_channels = true;
        reload_node!(nodes[1], no_announce_cfg, &nodes_1_serialized, &[&monitor_a_serialized, &monitor_b_serialized], persister, new_chain_monitor, nodes_1_deserialized);
 
-       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: nodes[1].node.init_features(), remote_network_address: None }, true).unwrap();
-       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: nodes[0].node.init_features(), remote_network_address: None }, false).unwrap();
+       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
+               features: nodes[1].node.init_features(), networks: None, remote_network_address: None
+       }, true).unwrap();
+       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
+               features: nodes[0].node.init_features(), networks: None, remote_network_address: None
+       }, false).unwrap();
        let as_reestablish = get_chan_reestablish_msgs!(nodes[0], nodes[1]).pop().unwrap();
        let bs_reestablish = get_chan_reestablish_msgs!(nodes[1], nodes[0]).pop().unwrap();
        nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &as_reestablish);
@@ -110,8 +114,12 @@ fn test_priv_forwarding_rejection() {
        get_event_msg!(nodes[0], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());
        get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[0].node.get_our_node_id());
 
-       nodes[1].node.peer_connected(&nodes[2].node.get_our_node_id(), &msgs::Init { features: nodes[2].node.init_features(), remote_network_address: None }, true).unwrap();
-       nodes[2].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: nodes[1].node.init_features(), remote_network_address: None }, false).unwrap();
+       nodes[1].node.peer_connected(&nodes[2].node.get_our_node_id(), &msgs::Init {
+               features: nodes[2].node.init_features(), networks: None, remote_network_address: None
+       }, true).unwrap();
+       nodes[2].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
+               features: nodes[1].node.init_features(), networks: None, remote_network_address: None
+       }, false).unwrap();
        let bs_reestablish = get_chan_reestablish_msgs!(nodes[1], nodes[2]).pop().unwrap();
        let cs_reestablish = get_chan_reestablish_msgs!(nodes[2], nodes[1]).pop().unwrap();
        nodes[2].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bs_reestablish);
@@ -133,10 +141,12 @@ fn do_test_1_conf_open(connect_style: ConnectStyle) {
        alice_config.channel_handshake_config.minimum_depth = 1;
        alice_config.channel_handshake_config.announced_channel = true;
        alice_config.channel_handshake_limits.force_announced_channel_preference = false;
+       alice_config.channel_config.max_dust_htlc_exposure = MaxDustHTLCExposure::FeeRateMultiplier(5_000_000 / 253);
        let mut bob_config = UserConfig::default();
        bob_config.channel_handshake_config.minimum_depth = 1;
        bob_config.channel_handshake_config.announced_channel = true;
        bob_config.channel_handshake_limits.force_announced_channel_preference = false;
+       bob_config.channel_config.max_dust_htlc_exposure = MaxDustHTLCExposure::FeeRateMultiplier(5_000_000 / 253);
        let chanmon_cfgs = create_chanmon_cfgs(2);
        let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
        let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(alice_config), Some(bob_config)]);
@@ -853,10 +863,12 @@ fn test_0conf_channel_reorg() {
                err: "Funding transaction was un-confirmed. Locked at 0 confs, now have 0 confs.".to_owned()
        });
        check_closed_broadcast!(nodes[0], true);
+       check_added_monitors(&nodes[0], 1);
        check_closed_event!(&nodes[1], 1, ClosureReason::ProcessingError {
                err: "Funding transaction was un-confirmed. Locked at 0 confs, now have 0 confs.".to_owned()
        });
        check_closed_broadcast!(nodes[1], true);
+       check_added_monitors(&nodes[1], 1);
 }
 
 #[test]
index 9b694c7b869b4709ea213a820070896bf483fb59..b53c617916a16deb8efc52b0197dacd61f5af780 100644 (file)
@@ -61,9 +61,13 @@ fn test_funding_peer_disconnect() {
        let events_2 = nodes[1].node.get_and_clear_pending_msg_events();
        assert!(events_2.is_empty());
 
-       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: nodes[1].node.init_features(), remote_network_address: None }, true).unwrap();
+       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
+               features: nodes[1].node.init_features(), networks: None, remote_network_address: None
+       }, true).unwrap();
        let as_reestablish = get_chan_reestablish_msgs!(nodes[0], nodes[1]).pop().unwrap();
-       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: nodes[0].node.init_features(), remote_network_address: None }, false).unwrap();
+       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
+               features: nodes[0].node.init_features(), networks: None, remote_network_address: None
+       }, false).unwrap();
        let bs_reestablish = get_chan_reestablish_msgs!(nodes[1], nodes[0]).pop().unwrap();
 
        // nodes[0] hasn't yet received a channel_ready, so it only sends that on reconnect.
@@ -197,9 +201,13 @@ fn test_no_txn_manager_serialize_deserialize() {
                get_monitor!(nodes[0], OutPoint { txid: tx.txid(), index: 0 }.to_channel_id()).encode();
        reload_node!(nodes[0], nodes[0].node.encode(), &[&chan_0_monitor_serialized], persister, new_chain_monitor, nodes_0_deserialized);
 
-       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: nodes[1].node.init_features(), remote_network_address: None }, true).unwrap();
+       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
+               features: nodes[1].node.init_features(), networks: None, remote_network_address: None
+       }, true).unwrap();
        let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
-       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: nodes[0].node.init_features(), remote_network_address: None }, false).unwrap();
+       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
+               features: nodes[0].node.init_features(), networks: None, remote_network_address: None
+       }, false).unwrap();
        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]);
@@ -282,9 +290,13 @@ fn test_manager_serialize_deserialize_events() {
        // Make sure the channel is functioning as though the de/serialization never happened
        assert_eq!(nodes[0].node.list_channels().len(), 1);
 
-       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: nodes[1].node.init_features(), remote_network_address: None }, true).unwrap();
+       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
+               features: nodes[1].node.init_features(), networks: None, remote_network_address: None
+       }, true).unwrap();
        let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
-       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: nodes[0].node.init_features(), remote_network_address: None }, false).unwrap();
+       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
+               features: nodes[0].node.init_features(), networks: None, remote_network_address: None
+       }, false).unwrap();
        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]);
@@ -372,7 +384,7 @@ fn test_manager_serialize_deserialize_inconsistent_monitor() {
        fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) };
        persister = test_utils::TestPersister::new();
        let keys_manager = &chanmon_cfgs[0].keys_manager;
-       new_chain_monitor = test_utils::TestChainMonitor::new(Some(nodes[0].chain_source), nodes[0].tx_broadcaster.clone(), &logger, &fee_estimator, &persister, keys_manager);
+       new_chain_monitor = test_utils::TestChainMonitor::new(Some(nodes[0].chain_source), nodes[0].tx_broadcaster, &logger, &fee_estimator, &persister, keys_manager);
        nodes[0].chain_monitor = &new_chain_monitor;
 
 
@@ -449,9 +461,13 @@ fn test_manager_serialize_deserialize_inconsistent_monitor() {
        //... 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(), &msgs::Init { features: nodes[0].node.init_features(), remote_network_address: None }, true).unwrap();
+       nodes[3].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
+               features: nodes[0].node.init_features(), networks: None, remote_network_address: None
+       }, true).unwrap();
        let reestablish = get_chan_reestablish_msgs!(nodes[3], nodes[0]).pop().unwrap();
-       nodes[0].node.peer_connected(&nodes[3].node.get_our_node_id(), &msgs::Init { features: nodes[3].node.init_features(), remote_network_address: None }, false).unwrap();
+       nodes[0].node.peer_connected(&nodes[3].node.get_our_node_id(), &msgs::Init {
+               features: nodes[3].node.init_features(), networks: None, remote_network_address: None
+       }, false).unwrap();
        nodes[0].node.handle_channel_reestablish(&nodes[3].node.get_our_node_id(), &reestablish);
        let mut found_err = false;
        for msg_event in nodes[0].node.get_and_clear_pending_msg_events() {
@@ -500,8 +516,12 @@ fn do_test_data_loss_protect(reconnect_panicing: bool) {
        reload_node!(nodes[0], previous_node_state, &[&previous_chain_monitor_state], persister, new_chain_monitor, nodes_0_deserialized);
 
        if reconnect_panicing {
-               nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: nodes[1].node.init_features(), remote_network_address: None }, true).unwrap();
-               nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: nodes[0].node.init_features(), remote_network_address: None }, false).unwrap();
+               nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
+                       features: nodes[1].node.init_features(), networks: None, remote_network_address: None
+               }, true).unwrap();
+               nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
+                       features: nodes[0].node.init_features(), networks: None, remote_network_address: None
+               }, false).unwrap();
 
                let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
 
@@ -549,8 +569,12 @@ fn do_test_data_loss_protect(reconnect_panicing: bool) {
        // after the warning message sent by B, we should not able to
        // use the channel, or reconnect with success to the channel.
        assert!(nodes[0].node.list_usable_channels().is_empty());
-       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: nodes[1].node.init_features(), remote_network_address: None }, true).unwrap();
-       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: nodes[0].node.init_features(), remote_network_address: None }, false).unwrap();
+       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
+               features: nodes[1].node.init_features(), networks: None, remote_network_address: None
+       }, true).unwrap();
+       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
+               features: nodes[0].node.init_features(), networks: None, remote_network_address: None
+       }, false).unwrap();
        let retry_reestablish = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
 
        nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &retry_reestablish[0]);
@@ -774,6 +798,9 @@ fn do_test_partial_claim_before_restart(persist_both_monitors: bool) {
        if let Event::ChannelClosed { reason: ClosureReason::OutdatedChannelManager, .. } = events[1] { } else { panic!(); }
        if persist_both_monitors {
                if let Event::ChannelClosed { reason: ClosureReason::OutdatedChannelManager, .. } = events[2] { } else { panic!(); }
+               check_added_monitors(&nodes[3], 2);
+       } else {
+               check_added_monitors(&nodes[3], 1);
        }
 
        // On restart, we should also get a duplicate PaymentClaimed event as we persisted the
@@ -788,9 +815,13 @@ fn do_test_partial_claim_before_restart(persist_both_monitors: bool) {
        if !persist_both_monitors {
                // If one of the two channels is still live, reveal the payment preimage over it.
 
-               nodes[3].node.peer_connected(&nodes[2].node.get_our_node_id(), &msgs::Init { features: nodes[2].node.init_features(), remote_network_address: None }, true).unwrap();
+               nodes[3].node.peer_connected(&nodes[2].node.get_our_node_id(), &msgs::Init {
+                       features: nodes[2].node.init_features(), networks: None, remote_network_address: None
+               }, true).unwrap();
                let reestablish_1 = get_chan_reestablish_msgs!(nodes[3], nodes[2]);
-               nodes[2].node.peer_connected(&nodes[3].node.get_our_node_id(), &msgs::Init { features: nodes[3].node.init_features(), remote_network_address: None }, false).unwrap();
+               nodes[2].node.peer_connected(&nodes[3].node.get_our_node_id(), &msgs::Init {
+                       features: nodes[3].node.init_features(), networks: None, remote_network_address: None
+               }, false).unwrap();
                let reestablish_2 = get_chan_reestablish_msgs!(nodes[2], nodes[3]);
 
                nodes[2].node.handle_channel_reestablish(&nodes[3].node.get_our_node_id(), &reestablish_1[0]);
@@ -1047,6 +1078,9 @@ fn removed_payment_no_manager_persistence() {
                _ => panic!("Unexpected event"),
        }
 
+       nodes[1].node.test_process_background_events();
+       check_added_monitors(&nodes[1], 1);
+
        // Now that the ChannelManager has force-closed the channel which had the HTLC removed, it is
        // now forgotten everywhere. The ChannelManager should have, as a side-effect of reload,
        // learned that the HTLC is gone from the ChannelMonitor and added it to the to-fail-back set.
index 633eceddbd8721c19e5623cbc334bc648e57362d..be756840adc7ce2a26961193c85d87e1010b18ba 100644 (file)
@@ -289,8 +289,6 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
                let relevant_txids = nodes[0].node.get_relevant_txids();
                assert_eq!(relevant_txids.len(), 0);
 
-               handle_announce_close_broadcast_events(&nodes, 0, 1, true, "Channel closed because of an exception: Funding transaction was un-confirmed. Locked at 6 confs, now have 0 confs.");
-               check_added_monitors!(nodes[1], 1);
                {
                        let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
                        let peer_state = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
@@ -336,8 +334,6 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
                let relevant_txids = nodes[0].node.get_relevant_txids();
                assert_eq!(relevant_txids.len(), 0);
 
-               handle_announce_close_broadcast_events(&nodes, 0, 1, true, "Channel closed because of an exception: Funding transaction was un-confirmed. Locked at 6 confs, now have 0 confs.");
-               check_added_monitors!(nodes[1], 1);
                {
                        let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
                        let peer_state = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
@@ -351,7 +347,12 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
        nodes[0].node.test_process_background_events(); // Required to free the pending background monitor update
        check_added_monitors!(nodes[0], 1);
        let expected_err = "Funding transaction was un-confirmed. Locked at 6 confs, now have 0 confs.";
-       check_closed_event!(nodes[1], 1, ClosureReason::CounterpartyForceClosed { peer_msg: UntrustedString(format!("Channel closed because of an exception: {}", expected_err)) });
+       if reorg_after_reload || !reload_node {
+               handle_announce_close_broadcast_events(&nodes, 0, 1, true, "Channel closed because of an exception: Funding transaction was un-confirmed. Locked at 6 confs, now have 0 confs.");
+               check_added_monitors!(nodes[1], 1);
+               check_closed_event!(nodes[1], 1, ClosureReason::CounterpartyForceClosed { peer_msg: UntrustedString(format!("Channel closed because of an exception: {}", expected_err)) });
+       }
+
        check_closed_event!(nodes[0], 1, ClosureReason::ProcessingError { err: expected_err.to_owned() });
        assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
        nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
@@ -361,7 +362,9 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
                // If we dropped the channel before reloading the node, nodes[1] was also dropped from
                // nodes[0] storage, and hence not connected again on startup. We therefore need to
                // reconnect to the node before attempting to create a new channel.
-               nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &Init { features: nodes[1].node.init_features(), remote_network_address: None }, true).unwrap();
+               nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &Init {
+                       features: nodes[1].node.init_features(), networks: None, remote_network_address: None
+               }, true).unwrap();
        }
        create_announced_chan_between_nodes(&nodes, 0, 1);
        send_payment(&nodes[0], &[&nodes[1]], 8000000);
index 1063bf76c2dab6749635168fd5c0dfe766cf1451..3aa48c1b45d2f8f49b17746db0aa7c33d0d2fca1 100644 (file)
@@ -12,7 +12,7 @@
 use crate::sign::{EntropySource, SignerProvider};
 use crate::chain::transaction::OutPoint;
 use crate::events::{Event, MessageSendEvent, MessageSendEventsProvider, ClosureReason};
-use crate::ln::channelmanager::{self, PaymentSendFailure, PaymentId, RecipientOnionFields};
+use crate::ln::channelmanager::{self, PaymentSendFailure, PaymentId, RecipientOnionFields, ChannelShutdownState, ChannelDetails};
 use crate::routing::router::{PaymentParameters, get_route};
 use crate::ln::msgs;
 use crate::ln::msgs::{ChannelMessageHandler, ErrorAction};
@@ -67,6 +67,169 @@ fn pre_funding_lock_shutdown_test() {
        check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure);
 }
 
+#[test]
+fn expect_channel_shutdown_state() {
+       // Test sending a shutdown prior to channel_ready after funding generation
+       let chanmon_cfgs = create_chanmon_cfgs(2);
+       let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
+       let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
+       let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
+       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
+
+       expect_channel_shutdown_state!(nodes[0], chan_1.2, ChannelShutdownState::NotShuttingDown);
+
+       nodes[0].node.close_channel(&chan_1.2, &nodes[1].node.get_our_node_id()).unwrap();
+
+       expect_channel_shutdown_state!(nodes[0], chan_1.2, ChannelShutdownState::ShutdownInitiated);
+       expect_channel_shutdown_state!(nodes[1], chan_1.2, ChannelShutdownState::NotShuttingDown);
+
+       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);
+
+       // node1 goes into NegotiatingClosingFee since there are no HTLCs in flight, note that it
+       // doesnt mean that node1 has sent/recved its closing signed message
+       expect_channel_shutdown_state!(nodes[0], chan_1.2, ChannelShutdownState::ShutdownInitiated);
+       expect_channel_shutdown_state!(nodes[1], chan_1.2, ChannelShutdownState::NegotiatingClosingFee);
+
+       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);
+
+       expect_channel_shutdown_state!(nodes[0], chan_1.2, ChannelShutdownState::NegotiatingClosingFee);
+       expect_channel_shutdown_state!(nodes[1], chan_1.2, ChannelShutdownState::NegotiatingClosingFee);
+
+       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);
+       let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
+       nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
+       let (_, node_0_2nd_closing_signed) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
+       nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed.unwrap());
+       let (_, node_1_none) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
+       assert!(node_1_none.is_none());
+
+       assert!(nodes[0].node.list_channels().is_empty());
+       assert!(nodes[1].node.list_channels().is_empty());
+       check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure);
+       check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure);
+}
+
+#[test]
+fn expect_channel_shutdown_state_with_htlc() {
+       // Test sending a shutdown with outstanding updates pending.
+       let chanmon_cfgs = create_chanmon_cfgs(3);
+       let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
+       let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
+       let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
+       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
+       let _chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
+
+       let (payment_preimage_0, payment_hash_0, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 100_000);
+
+       expect_channel_shutdown_state!(nodes[0], chan_1.2, ChannelShutdownState::NotShuttingDown);
+       expect_channel_shutdown_state!(nodes[1], chan_1.2, ChannelShutdownState::NotShuttingDown);
+
+       nodes[0].node.close_channel(&chan_1.2, &nodes[1].node.get_our_node_id()).unwrap();
+
+       expect_channel_shutdown_state!(nodes[0], chan_1.2, ChannelShutdownState::ShutdownInitiated);
+       expect_channel_shutdown_state!(nodes[1], chan_1.2, ChannelShutdownState::NotShuttingDown);
+
+       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);
+
+       expect_channel_shutdown_state!(nodes[0], chan_1.2, ChannelShutdownState::ShutdownInitiated);
+       expect_channel_shutdown_state!(nodes[1], chan_1.2, ChannelShutdownState::ResolvingHTLCs);
+
+       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);
+
+       expect_channel_shutdown_state!(nodes[0], chan_1.2, ChannelShutdownState::ResolvingHTLCs);
+       expect_channel_shutdown_state!(nodes[1], chan_1.2, ChannelShutdownState::ResolvingHTLCs);
+
+       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+
+       // Claim Funds on Node2
+       nodes[2].node.claim_funds(payment_preimage_0);
+       check_added_monitors!(nodes[2], 1);
+       expect_payment_claimed!(nodes[2], payment_hash_0, 100_000);
+
+       // Fulfil HTLCs on node1 and node0
+       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]);
+       expect_payment_forwarded!(nodes[1], nodes[0], nodes[2], Some(1000), false, false);
+       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);
+
+       // Still in "resolvingHTLCs" on chan1 after htlc removed on chan2
+       expect_channel_shutdown_state!(nodes[0], chan_1.2, ChannelShutdownState::ResolvingHTLCs);
+       expect_channel_shutdown_state!(nodes[1], chan_1.2, ChannelShutdownState::ResolvingHTLCs);
+
+       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]);
+       commitment_signed_dance!(nodes[0], nodes[1], updates_2.commitment_signed, false, true);
+       expect_payment_sent!(nodes[0], payment_preimage_0);
+
+       // all htlcs removed, chan1 advances to NegotiatingClosingFee
+       expect_channel_shutdown_state!(nodes[0], chan_1.2, ChannelShutdownState::NegotiatingClosingFee);
+       expect_channel_shutdown_state!(nodes[1], chan_1.2, ChannelShutdownState::NegotiatingClosingFee);
+
+       // ClosingSignNegotion process
+       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);
+       let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
+       nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
+       let (_, node_0_2nd_closing_signed) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
+       nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed.unwrap());
+       let (_, node_1_none) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
+       assert!(node_1_none.is_none());
+       check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure);
+       check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure);
+
+       // Shutdown basically removes the channelDetails, testing of shutdowncomplete state unnecessary
+       assert!(nodes[0].node.list_channels().is_empty());
+}
+
+#[test]
+fn expect_channel_shutdown_state_with_force_closure() {
+       // Test sending a shutdown prior to channel_ready after funding generation
+       let chanmon_cfgs = create_chanmon_cfgs(2);
+       let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
+       let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
+       let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
+       let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
+
+       expect_channel_shutdown_state!(nodes[0], chan_1.2, ChannelShutdownState::NotShuttingDown);
+       expect_channel_shutdown_state!(nodes[1], chan_1.2, ChannelShutdownState::NotShuttingDown);
+
+       nodes[1].node.force_close_broadcasting_latest_txn(&chan_1.2, &nodes[0].node.get_our_node_id()).unwrap();
+       check_closed_broadcast!(nodes[1], true);
+       check_added_monitors!(nodes[1], 1);
+
+       expect_channel_shutdown_state!(nodes[0], chan_1.2, ChannelShutdownState::NotShuttingDown);
+       assert!(nodes[1].node.list_channels().is_empty());
+
+       let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
+       assert_eq!(node_txn.len(), 1);
+       check_spends!(node_txn[0], chan_1.3);
+       mine_transaction(&nodes[0], &node_txn[0]);
+       check_added_monitors!(nodes[0], 1);
+
+       assert!(nodes[0].node.list_channels().is_empty());
+       assert!(nodes[1].node.list_channels().is_empty());
+       check_closed_broadcast!(nodes[0], true);
+       check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
+       check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed);
+}
+
 #[test]
 fn updates_shutdown_wait() {
        // Test sending a shutdown with outstanding updates pending
@@ -252,9 +415,13 @@ fn do_test_shutdown_rebroadcast(recv_count: u8) {
        nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
        nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
 
-       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: nodes[1].node.init_features(), remote_network_address: None }, true).unwrap();
+       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
+               features: nodes[1].node.init_features(), networks: None, remote_network_address: None
+       }, true).unwrap();
        let node_0_reestablish = get_chan_reestablish_msgs!(nodes[0], nodes[1]).pop().unwrap();
-       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: nodes[0].node.init_features(), remote_network_address: None }, false).unwrap();
+       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
+               features: nodes[0].node.init_features(), networks: None, remote_network_address: None
+       }, false).unwrap();
        let node_1_reestablish = get_chan_reestablish_msgs!(nodes[1], nodes[0]).pop().unwrap();
 
        nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &node_0_reestablish);
@@ -314,9 +481,13 @@ fn do_test_shutdown_rebroadcast(recv_count: u8) {
        nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
        nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
 
-       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: nodes[0].node.init_features(), remote_network_address: None }, true).unwrap();
+       nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
+               features: nodes[0].node.init_features(), networks: None, remote_network_address: None
+       }, true).unwrap();
        let node_1_2nd_reestablish = get_chan_reestablish_msgs!(nodes[1], nodes[0]).pop().unwrap();
-       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: nodes[1].node.init_features(), remote_network_address: None }, false).unwrap();
+       nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
+               features: nodes[1].node.init_features(), networks: None, remote_network_address: None
+       }, false).unwrap();
        if recv_count == 0 {
                // If all closing_signeds weren't delivered we can just resume where we left off...
                let node_0_2nd_reestablish = get_chan_reestablish_msgs!(nodes[0], nodes[1]).pop().unwrap();
@@ -829,7 +1000,7 @@ fn do_test_closing_signed_reinit_timeout(timeout_step: TimeoutStep) {
                {
                        let mut node_0_per_peer_lock;
                        let mut node_0_peer_state_lock;
-                       get_channel_ref!(nodes[0], nodes[1], node_0_per_peer_lock, node_0_peer_state_lock, chan_id).closing_fee_limits.as_mut().unwrap().1 *= 10;
+                       get_channel_ref!(nodes[0], nodes[1], node_0_per_peer_lock, node_0_peer_state_lock, chan_id).context.closing_fee_limits.as_mut().unwrap().1 *= 10;
                }
                nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
                let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
index 1a01e33826dbb9790086afb1063219ea2f42c98c..88e2ad7c1daee03ef245c795f0119eb229a10520 100644 (file)
@@ -96,9 +96,59 @@ pub(crate) enum Message<T> where T: core::fmt::Debug + Type + TestEq {
        Custom(T),
 }
 
-impl<T> Message<T> where T: core::fmt::Debug + Type + TestEq {
+impl<T> Writeable for Message<T> where T: core::fmt::Debug + Type + TestEq {
+       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
+               match self {
+                       &Message::Init(ref msg) => msg.write(writer),
+                       &Message::Error(ref msg) => msg.write(writer),
+                       &Message::Warning(ref msg) => msg.write(writer),
+                       &Message::Ping(ref msg) => msg.write(writer),
+                       &Message::Pong(ref msg) => msg.write(writer),
+                       &Message::OpenChannel(ref msg) => msg.write(writer),
+                       &Message::OpenChannelV2(ref msg) => msg.write(writer),
+                       &Message::AcceptChannel(ref msg) => msg.write(writer),
+                       &Message::AcceptChannelV2(ref msg) => msg.write(writer),
+                       &Message::FundingCreated(ref msg) => msg.write(writer),
+                       &Message::FundingSigned(ref msg) => msg.write(writer),
+                       &Message::TxAddInput(ref msg) => msg.write(writer),
+                       &Message::TxAddOutput(ref msg) => msg.write(writer),
+                       &Message::TxRemoveInput(ref msg) => msg.write(writer),
+                       &Message::TxRemoveOutput(ref msg) => msg.write(writer),
+                       &Message::TxComplete(ref msg) => msg.write(writer),
+                       &Message::TxSignatures(ref msg) => msg.write(writer),
+                       &Message::TxInitRbf(ref msg) => msg.write(writer),
+                       &Message::TxAckRbf(ref msg) => msg.write(writer),
+                       &Message::TxAbort(ref msg) => msg.write(writer),
+                       &Message::ChannelReady(ref msg) => msg.write(writer),
+                       &Message::Shutdown(ref msg) => msg.write(writer),
+                       &Message::ClosingSigned(ref msg) => msg.write(writer),
+                       &Message::OnionMessage(ref msg) => msg.write(writer),
+                       &Message::UpdateAddHTLC(ref msg) => msg.write(writer),
+                       &Message::UpdateFulfillHTLC(ref msg) => msg.write(writer),
+                       &Message::UpdateFailHTLC(ref msg) => msg.write(writer),
+                       &Message::UpdateFailMalformedHTLC(ref msg) => msg.write(writer),
+                       &Message::CommitmentSigned(ref msg) => msg.write(writer),
+                       &Message::RevokeAndACK(ref msg) => msg.write(writer),
+                       &Message::UpdateFee(ref msg) => msg.write(writer),
+                       &Message::ChannelReestablish(ref msg) => msg.write(writer),
+                       &Message::AnnouncementSignatures(ref msg) => msg.write(writer),
+                       &Message::ChannelAnnouncement(ref msg) => msg.write(writer),
+                       &Message::NodeAnnouncement(ref msg) => msg.write(writer),
+                       &Message::ChannelUpdate(ref msg) => msg.write(writer),
+                       &Message::QueryShortChannelIds(ref msg) => msg.write(writer),
+                       &Message::ReplyShortChannelIdsEnd(ref msg) => msg.write(writer),
+                       &Message::QueryChannelRange(ref msg) => msg.write(writer),
+                       &Message::ReplyChannelRange(ref msg) => msg.write(writer),
+                       &Message::GossipTimestampFilter(ref msg) => msg.write(writer),
+                       &Message::Unknown(_) => { Ok(()) },
+                       &Message::Custom(ref msg) => msg.write(writer),
+               }
+       }
+}
+
+impl<T> Type for Message<T> where T: core::fmt::Debug + Type + TestEq {
        /// Returns the type that was used to decode the message payload.
-       pub fn type_id(&self) -> u16 {
+       fn type_id(&self) -> u16 {
                match self {
                        &Message::Init(ref msg) => msg.type_id(),
                        &Message::Error(ref msg) => msg.type_id(),
@@ -145,7 +195,9 @@ impl<T> Message<T> where T: core::fmt::Debug + Type + TestEq {
                        &Message::Custom(ref msg) => msg.type_id(),
                }
        }
+}
 
+impl<T> Message<T> where T: core::fmt::Debug + Type + TestEq {
        /// Returns whether the message's type is even, indicating both endpoints must support it.
        pub fn is_even(&self) -> bool {
                (self.type_id() & 1) == 0
index 4d1398644ce0d24483205b0ac04e428ba3d52482..fb1f78fd64445c406d582830eebd97d4c107c16b 100644 (file)
@@ -31,7 +31,7 @@
 //! # use lightning::offers::invoice::BlindedPayInfo;
 //! # use lightning::blinded_path::BlindedPath;
 //! #
-//! # fn create_payment_paths() -> Vec<(BlindedPath, BlindedPayInfo)> { unimplemented!() }
+//! # fn create_payment_paths() -> Vec<(BlindedPayInfo, BlindedPath)> { unimplemented!() }
 //! # fn create_payment_hash() -> PaymentHash { unimplemented!() }
 //! #
 //! # fn parse_invoice_request(bytes: Vec<u8>) -> Result<(), lightning::offers::parse::ParseError> {
@@ -166,7 +166,7 @@ impl SigningPubkeyStrategy for DerivedSigningPubkey {}
 
 impl<'a> InvoiceBuilder<'a, ExplicitSigningPubkey> {
        pub(super) fn for_offer(
-               invoice_request: &'a InvoiceRequest, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>,
+               invoice_request: &'a InvoiceRequest, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>,
                created_at: Duration, payment_hash: PaymentHash
        ) -> Result<Self, SemanticError> {
                let amount_msats = Self::check_amount_msats(invoice_request)?;
@@ -182,7 +182,7 @@ impl<'a> InvoiceBuilder<'a, ExplicitSigningPubkey> {
        }
 
        pub(super) fn for_refund(
-               refund: &'a Refund, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, created_at: Duration,
+               refund: &'a Refund, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, created_at: Duration,
                payment_hash: PaymentHash, signing_pubkey: PublicKey
        ) -> Result<Self, SemanticError> {
                let amount_msats = refund.amount_msats();
@@ -199,7 +199,7 @@ impl<'a> InvoiceBuilder<'a, ExplicitSigningPubkey> {
 
 impl<'a> InvoiceBuilder<'a, DerivedSigningPubkey> {
        pub(super) fn for_offer_using_keys(
-               invoice_request: &'a InvoiceRequest, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>,
+               invoice_request: &'a InvoiceRequest, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>,
                created_at: Duration, payment_hash: PaymentHash, keys: KeyPair
        ) -> Result<Self, SemanticError> {
                let amount_msats = Self::check_amount_msats(invoice_request)?;
@@ -215,7 +215,7 @@ impl<'a> InvoiceBuilder<'a, DerivedSigningPubkey> {
        }
 
        pub(super) fn for_refund_using_keys(
-               refund: &'a Refund, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, created_at: Duration,
+               refund: &'a Refund, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, created_at: Duration,
                payment_hash: PaymentHash, keys: KeyPair,
        ) -> Result<Self, SemanticError> {
                let amount_msats = refund.amount_msats();
@@ -247,7 +247,7 @@ impl<'a, S: SigningPubkeyStrategy> InvoiceBuilder<'a, S> {
        }
 
        fn fields(
-               payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, created_at: Duration,
+               payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, created_at: Duration,
                payment_hash: PaymentHash, amount_msats: u64, signing_pubkey: PublicKey
        ) -> InvoiceFields {
                InvoiceFields {
@@ -454,7 +454,7 @@ enum InvoiceContents {
 /// Invoice-specific fields for an `invoice` message.
 #[derive(Clone, Debug, PartialEq)]
 struct InvoiceFields {
-       payment_paths: Vec<(BlindedPath, BlindedPayInfo)>,
+       payment_paths: Vec<(BlindedPayInfo, BlindedPath)>,
        created_at: Duration,
        relative_expiry: Option<Duration>,
        payment_hash: PaymentHash,
@@ -476,7 +476,7 @@ impl Invoice {
        ///
        /// Blinded paths provide recipient privacy by obfuscating its node id. Note, however, that this
        /// privacy is lost if a public node id is used for [`Invoice::signing_pubkey`].
-       pub fn payment_paths(&self) -> &[(BlindedPath, BlindedPayInfo)] {
+       pub fn payment_paths(&self) -> &[(BlindedPayInfo, BlindedPath)] {
                &self.contents.fields().payment_paths[..]
        }
 
@@ -703,8 +703,8 @@ impl InvoiceFields {
                };
 
                InvoiceTlvStreamRef {
-                       paths: Some(Iterable(self.payment_paths.iter().map(|(path, _)| path))),
-                       blindedpay: Some(Iterable(self.payment_paths.iter().map(|(_, payinfo)| payinfo))),
+                       paths: Some(Iterable(self.payment_paths.iter().map(|(_, path)| path))),
+                       blindedpay: Some(Iterable(self.payment_paths.iter().map(|(payinfo, _)| payinfo))),
                        created_at: Some(self.created_at.as_secs()),
                        relative_expiry: self.relative_expiry.map(|duration| duration.as_secs() as u32),
                        payment_hash: Some(&self.payment_hash),
@@ -750,13 +750,13 @@ tlv_stream!(InvoiceTlvStream, InvoiceTlvStreamRef, 160..240, {
 });
 
 type BlindedPathIter<'a> = core::iter::Map<
-       core::slice::Iter<'a, (BlindedPath, BlindedPayInfo)>,
-       for<'r> fn(&'r (BlindedPath, BlindedPayInfo)) -> &'r BlindedPath,
+       core::slice::Iter<'a, (BlindedPayInfo, BlindedPath)>,
+       for<'r> fn(&'r (BlindedPayInfo, BlindedPath)) -> &'r BlindedPath,
 >;
 
 type BlindedPayInfoIter<'a> = core::iter::Map<
-       core::slice::Iter<'a, (BlindedPath, BlindedPayInfo)>,
-       for<'r> fn(&'r (BlindedPath, BlindedPayInfo)) -> &'r BlindedPayInfo,
+       core::slice::Iter<'a, (BlindedPayInfo, BlindedPath)>,
+       for<'r> fn(&'r (BlindedPayInfo, BlindedPath)) -> &'r BlindedPayInfo,
 >;
 
 /// Information needed to route a payment across a [`BlindedPath`].
@@ -878,15 +878,15 @@ impl TryFrom<PartialInvoiceTlvStream> for InvoiceContents {
                        },
                ) = tlv_stream;
 
-               let payment_paths = match (paths, blindedpay) {
-                       (None, _) => return Err(SemanticError::MissingPaths),
-                       (_, None) => return Err(SemanticError::InvalidPayInfo),
-                       (Some(paths), _) if paths.is_empty() => return Err(SemanticError::MissingPaths),
-                       (Some(paths), Some(blindedpay)) if paths.len() != blindedpay.len() => {
+               let payment_paths = match (blindedpay, paths) {
+                       (_, None) => return Err(SemanticError::MissingPaths),
+                       (None, _) => return Err(SemanticError::InvalidPayInfo),
+                       (_, Some(paths)) if paths.is_empty() => return Err(SemanticError::MissingPaths),
+                       (Some(blindedpay), Some(paths)) if paths.len() != blindedpay.len() => {
                                return Err(SemanticError::InvalidPayInfo);
                        },
-                       (Some(paths), Some(blindedpay)) => {
-                               paths.into_iter().zip(blindedpay.into_iter()).collect::<Vec<_>>()
+                       (Some(blindedpay), Some(paths)) => {
+                               blindedpay.into_iter().zip(paths.into_iter()).collect::<Vec<_>>()
                        },
                };
 
@@ -1052,8 +1052,8 @@ mod tests {
                                        payer_note: None,
                                },
                                InvoiceTlvStreamRef {
-                                       paths: Some(Iterable(payment_paths.iter().map(|(path, _)| path))),
-                                       blindedpay: Some(Iterable(payment_paths.iter().map(|(_, payinfo)| payinfo))),
+                                       paths: Some(Iterable(payment_paths.iter().map(|(_, path)| path))),
+                                       blindedpay: Some(Iterable(payment_paths.iter().map(|(payinfo, _)| payinfo))),
                                        created_at: Some(now.as_secs()),
                                        relative_expiry: None,
                                        payment_hash: Some(&payment_hash),
@@ -1130,8 +1130,8 @@ mod tests {
                                        payer_note: None,
                                },
                                InvoiceTlvStreamRef {
-                                       paths: Some(Iterable(payment_paths.iter().map(|(path, _)| path))),
-                                       blindedpay: Some(Iterable(payment_paths.iter().map(|(_, payinfo)| payinfo))),
+                                       paths: Some(Iterable(payment_paths.iter().map(|(_, path)| path))),
+                                       blindedpay: Some(Iterable(payment_paths.iter().map(|(payinfo, _)| payinfo))),
                                        created_at: Some(now.as_secs()),
                                        relative_expiry: None,
                                        payment_hash: Some(&payment_hash),
@@ -1516,7 +1516,7 @@ mod tests {
 
                let empty_payment_paths = vec![];
                let mut tlv_stream = invoice.as_tlv_stream();
-               tlv_stream.3.paths = Some(Iterable(empty_payment_paths.iter().map(|(path, _)| path)));
+               tlv_stream.3.paths = Some(Iterable(empty_payment_paths.iter().map(|(_, path)| path)));
 
                match Invoice::try_from(tlv_stream.to_bytes()) {
                        Ok(_) => panic!("expected error"),
@@ -1526,7 +1526,7 @@ mod tests {
                let mut payment_paths = payment_paths();
                payment_paths.pop();
                let mut tlv_stream = invoice.as_tlv_stream();
-               tlv_stream.3.blindedpay = Some(Iterable(payment_paths.iter().map(|(_, payinfo)| payinfo)));
+               tlv_stream.3.blindedpay = Some(Iterable(payment_paths.iter().map(|(payinfo, _)| payinfo)));
 
                match Invoice::try_from(tlv_stream.to_bytes()) {
                        Ok(_) => panic!("expected error"),
diff --git a/lightning/src/offers/invoice_error.rs b/lightning/src/offers/invoice_error.rs
new file mode 100644 (file)
index 0000000..e843264
--- /dev/null
@@ -0,0 +1,233 @@
+// This file is Copyright its original authors, visible in version control
+// history.
+//
+// This file is 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.
+// You may not use this file except in accordance with one or both of these
+// licenses.
+
+//! Data structures and encoding for `invoice_error` messages.
+
+use crate::io;
+use crate::ln::msgs::DecodeError;
+use crate::offers::parse::SemanticError;
+use crate::util::ser::{HighZeroBytesDroppedBigSize, Readable, WithoutLength, Writeable, Writer};
+use crate::util::string::UntrustedString;
+
+use crate::prelude::*;
+
+/// An error in response to an [`InvoiceRequest`] or an [`Invoice`].
+///
+/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
+/// [`Invoice`]: crate::offers::invoice::Invoice
+#[derive(Clone, Debug)]
+#[cfg_attr(test, derive(PartialEq))]
+pub struct InvoiceError {
+       /// The field in the [`InvoiceRequest`] or the [`Invoice`] that contained an error.
+       ///
+       /// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
+       /// [`Invoice`]: crate::offers::invoice::Invoice
+       pub erroneous_field: Option<ErroneousField>,
+
+       /// An explanation of the error.
+       pub message: UntrustedString,
+}
+
+/// The field in the [`InvoiceRequest`] or the [`Invoice`] that contained an error.
+///
+/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
+/// [`Invoice`]: crate::offers::invoice::Invoice
+#[derive(Clone, Debug)]
+#[cfg_attr(test, derive(PartialEq))]
+pub struct ErroneousField {
+       /// The type number of the TLV field containing the error.
+       pub tlv_fieldnum: u64,
+
+       /// A value to use for the TLV field to avoid the error.
+       pub suggested_value: Option<Vec<u8>>,
+}
+
+impl core::fmt::Display for InvoiceError {
+       fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
+               self.message.fmt(f)
+       }
+}
+
+impl Writeable for InvoiceError {
+       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
+               let tlv_fieldnum = self.erroneous_field.as_ref().map(|f| f.tlv_fieldnum);
+               let suggested_value =
+                       self.erroneous_field.as_ref().and_then(|f| f.suggested_value.as_ref());
+               write_tlv_fields!(writer, {
+                       (1, tlv_fieldnum, (option, encoding: (u64, HighZeroBytesDroppedBigSize))),
+                       (3, suggested_value, (option, encoding: (Vec<u8>, WithoutLength))),
+                       (5, WithoutLength(&self.message), required),
+               });
+               Ok(())
+       }
+}
+
+impl Readable for InvoiceError {
+       fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
+               _init_and_read_tlv_fields!(reader, {
+                       (1, erroneous_field, (option, encoding: (u64, HighZeroBytesDroppedBigSize))),
+                       (3, suggested_value, (option, encoding: (Vec<u8>, WithoutLength))),
+                       (5, error, (option, encoding: (UntrustedString, WithoutLength))),
+               });
+
+               let erroneous_field = match (erroneous_field, suggested_value) {
+                       (None, None) => None,
+                       (None, Some(_)) => return Err(DecodeError::InvalidValue),
+                       (Some(tlv_fieldnum), suggested_value) => {
+                               Some(ErroneousField { tlv_fieldnum, suggested_value })
+                       },
+               };
+
+               let message = match error {
+                       None => return Err(DecodeError::InvalidValue),
+                       Some(error) => error,
+               };
+
+               Ok(InvoiceError { erroneous_field, message })
+       }
+}
+
+impl From<SemanticError> for InvoiceError {
+       fn from(error: SemanticError) -> Self {
+               InvoiceError {
+                       erroneous_field: None,
+                       message: UntrustedString(format!("{:?}", error)),
+               }
+       }
+}
+
+#[cfg(test)]
+mod tests {
+       use super::{ErroneousField, InvoiceError};
+
+       use crate::ln::msgs::DecodeError;
+       use crate::util::ser::{HighZeroBytesDroppedBigSize, Readable, VecWriter, WithoutLength, Writeable};
+       use crate::util::string::UntrustedString;
+
+       #[test]
+       fn parses_invoice_error_without_erroneous_field() {
+               let mut writer = VecWriter(Vec::new());
+               let invoice_error = InvoiceError {
+                       erroneous_field: None,
+                       message: UntrustedString("Invalid value".to_string()),
+               };
+               invoice_error.write(&mut writer).unwrap();
+
+               let buffer = writer.0;
+               match InvoiceError::read(&mut &buffer[..]) {
+                       Ok(invoice_error) => {
+                               assert_eq!(invoice_error.message, UntrustedString("Invalid value".to_string()));
+                               assert_eq!(invoice_error.erroneous_field, None);
+                       }
+                       Err(e) => panic!("Unexpected error: {:?}", e),
+               }
+       }
+
+       #[test]
+       fn parses_invoice_error_with_erroneous_field() {
+               let mut writer = VecWriter(Vec::new());
+               let invoice_error = InvoiceError {
+                       erroneous_field: Some(ErroneousField {
+                               tlv_fieldnum: 42,
+                               suggested_value: Some(vec![42; 32]),
+                       }),
+                       message: UntrustedString("Invalid value".to_string()),
+               };
+               invoice_error.write(&mut writer).unwrap();
+
+               let buffer = writer.0;
+               match InvoiceError::read(&mut &buffer[..]) {
+                       Ok(invoice_error) => {
+                               assert_eq!(invoice_error.message, UntrustedString("Invalid value".to_string()));
+                               assert_eq!(
+                                       invoice_error.erroneous_field,
+                                       Some(ErroneousField { tlv_fieldnum: 42, suggested_value: Some(vec![42; 32]) }),
+                               );
+                       }
+                       Err(e) => panic!("Unexpected error: {:?}", e),
+               }
+       }
+
+       #[test]
+       fn parses_invoice_error_without_suggested_value() {
+               let mut writer = VecWriter(Vec::new());
+               let invoice_error = InvoiceError {
+                       erroneous_field: Some(ErroneousField {
+                               tlv_fieldnum: 42,
+                               suggested_value: None,
+                       }),
+                       message: UntrustedString("Invalid value".to_string()),
+               };
+               invoice_error.write(&mut writer).unwrap();
+
+               let buffer = writer.0;
+               match InvoiceError::read(&mut &buffer[..]) {
+                       Ok(invoice_error) => {
+                               assert_eq!(invoice_error.message, UntrustedString("Invalid value".to_string()));
+                               assert_eq!(
+                                       invoice_error.erroneous_field,
+                                       Some(ErroneousField { tlv_fieldnum: 42, suggested_value: None }),
+                               );
+                       }
+                       Err(e) => panic!("Unexpected error: {:?}", e),
+               }
+       }
+
+       #[test]
+       fn fails_parsing_invoice_error_without_message() {
+               let tlv_fieldnum: Option<u64> = None;
+               let suggested_value: Option<&Vec<u8>> = None;
+               let error: Option<&String> = None;
+
+               let mut writer = VecWriter(Vec::new());
+               let mut write_tlv = || -> Result<(), DecodeError> {
+                       write_tlv_fields!(&mut writer, {
+                               (1, tlv_fieldnum, (option, encoding: (u64, HighZeroBytesDroppedBigSize))),
+                               (3, suggested_value, (option, encoding: (Vec<u8>, WithoutLength))),
+                               (5, error, (option, encoding: (String, WithoutLength))),
+                       });
+                       Ok(())
+               };
+               write_tlv().unwrap();
+
+               let buffer = writer.0;
+               match InvoiceError::read(&mut &buffer[..]) {
+                       Ok(_) => panic!("Expected error"),
+                       Err(e) => {
+                               assert_eq!(e, DecodeError::InvalidValue);
+                       },
+               }
+       }
+
+       #[test]
+       fn fails_parsing_invoice_error_without_field() {
+               let tlv_fieldnum: Option<u64> = None;
+               let suggested_value = vec![42; 32];
+               let error = "Invalid value".to_string();
+
+               let mut writer = VecWriter(Vec::new());
+               let mut write_tlv = || -> Result<(), DecodeError> {
+                       write_tlv_fields!(&mut writer, {
+                               (1, tlv_fieldnum, (option, encoding: (u64, HighZeroBytesDroppedBigSize))),
+                               (3, Some(&suggested_value), (option, encoding: (Vec<u8>, WithoutLength))),
+                               (5, Some(&error), (option, encoding: (String, WithoutLength))),
+                       });
+                       Ok(())
+               };
+               write_tlv().unwrap();
+
+               let buffer = writer.0;
+               match InvoiceError::read(&mut &buffer[..]) {
+                       Ok(_) => panic!("Expected error"),
+                       Err(e) => {
+                               assert_eq!(e, DecodeError::InvalidValue);
+                       },
+               }
+       }
+}
index ed884848f4a015a85f365a8801998afc10801224..f51d2526781270d9e17b059b2a71fdc3bd1b04ad 100644 (file)
@@ -480,7 +480,7 @@ impl InvoiceRequest {
        /// [`Duration`]: core::time::Duration
        #[cfg(feature = "std")]
        pub fn respond_with(
-               &self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, payment_hash: PaymentHash
+               &self, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, payment_hash: PaymentHash
        ) -> Result<InvoiceBuilder<ExplicitSigningPubkey>, SemanticError> {
                let created_at = std::time::SystemTime::now()
                        .duration_since(std::time::SystemTime::UNIX_EPOCH)
@@ -509,7 +509,7 @@ impl InvoiceRequest {
        ///
        /// [`Invoice::created_at`]: crate::offers::invoice::Invoice::created_at
        pub fn respond_with_no_std(
-               &self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, payment_hash: PaymentHash,
+               &self, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, payment_hash: PaymentHash,
                created_at: core::time::Duration
        ) -> Result<InvoiceBuilder<ExplicitSigningPubkey>, SemanticError> {
                if self.features().requires_unknown_bits() {
@@ -530,7 +530,7 @@ impl InvoiceRequest {
        /// [`Invoice`]: crate::offers::invoice::Invoice
        #[cfg(feature = "std")]
        pub fn verify_and_respond_using_derived_keys<T: secp256k1::Signing>(
-               &self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, payment_hash: PaymentHash,
+               &self, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, payment_hash: PaymentHash,
                expanded_key: &ExpandedKey, secp_ctx: &Secp256k1<T>
        ) -> Result<InvoiceBuilder<DerivedSigningPubkey>, SemanticError> {
                let created_at = std::time::SystemTime::now()
@@ -552,7 +552,7 @@ impl InvoiceRequest {
        ///
        /// [`Invoice`]: crate::offers::invoice::Invoice
        pub fn verify_and_respond_using_derived_keys_no_std<T: secp256k1::Signing>(
-               &self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, payment_hash: PaymentHash,
+               &self, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, payment_hash: PaymentHash,
                created_at: core::time::Duration, expanded_key: &ExpandedKey, secp_ctx: &Secp256k1<T>
        ) -> Result<InvoiceBuilder<DerivedSigningPubkey>, SemanticError> {
                if self.features().requires_unknown_bits() {
index 3b05899a8f59214872a1075179fc9428b48412fa..f7c33902c51441cd3e2a358637c9ddb4d2cc31e6 100644 (file)
@@ -226,6 +226,7 @@ mod tests {
 
        use bitcoin::hashes::{Hash, sha256};
        use bitcoin::secp256k1::{KeyPair, Secp256k1, SecretKey};
+       use bitcoin::secp256k1::schnorr::Signature;
        use core::convert::Infallible;
        use crate::offers::offer::{Amount, OfferBuilder};
        use crate::offers::invoice_request::InvoiceRequest;
@@ -280,6 +281,10 @@ mod tests {
                        super::root_hash(&invoice_request.bytes[..]),
                        sha256::Hash::from_slice(&hex::decode("608407c18ad9a94d9ea2bcdbe170b6c20c462a7833a197621c916f78cf18e624").unwrap()).unwrap(),
                );
+               assert_eq!(
+                       invoice_request.signature(),
+                       Signature::from_slice(&hex::decode("b8f83ea3288cfd6ea510cdb481472575141e8d8744157f98562d162cc1c472526fdb24befefbdebab4dbb726bbd1b7d8aec057f8fa805187e5950d2bbe0e5642").unwrap()).unwrap(),
+               );
        }
 
        #[test]
index 0fb20f42d79e61b394cf46b59e6794e7b42a76fe..31d8bf9cbdf38e8590ce17fc1937bf8130a5f537 100644 (file)
@@ -13,6 +13,7 @@
 //! Offers are a flexible protocol for Lightning payments.
 
 pub mod invoice;
+pub mod invoice_error;
 pub mod invoice_request;
 mod merkle;
 pub mod offer;
index aa53f045d5b58cfb908aad8eee247bddc2d58aed..9c8489778b08eaee1e444fe3f2584345c5224582 100644 (file)
@@ -1454,30 +1454,23 @@ mod bech32_tests {
        use bitcoin::bech32;
        use crate::ln::msgs::DecodeError;
 
-       // TODO: Remove once test vectors are updated.
-       #[ignore]
        #[test]
        fn encodes_offer_as_bech32_without_checksum() {
-               let encoded_offer = "lno1qcp4256ypqpq86q2pucnq42ngssx2an9wfujqerp0y2pqun4wd68jtn00fkxzcnn9ehhyec6qgqsz83qfwdpl28qqmc78ymlvhmxcsywdk5wrjnj36jryg488qwlrnzyjczlqsp9nyu4phcg6dqhlhzgxagfu7zh3d9re0sqp9ts2yfugvnnm9gxkcnnnkdpa084a6t520h5zhkxsdnghvpukvd43lastpwuh73k29qsy";
+               let encoded_offer = "lno1pqps7sjqpgtyzm3qv4uxzmtsd3jjqer9wd3hy6tsw35k7msjzfpy7nz5yqcnygrfdej82um5wf5k2uckyypwa3eyt44h6txtxquqh7lz5djge4afgfjn7k4rgrkuag0jsd5xvxg";
                let offer = dbg!(encoded_offer.parse::<Offer>().unwrap());
                let reencoded_offer = offer.to_string();
                dbg!(reencoded_offer.parse::<Offer>().unwrap());
                assert_eq!(reencoded_offer, encoded_offer);
        }
 
-       // TODO: Remove once test vectors are updated.
-       #[ignore]
        #[test]
        fn parses_bech32_encoded_offers() {
                let offers = [
                        // BOLT 12 test vectors
-                       "lno1qcp4256ypqpq86q2pucnq42ngssx2an9wfujqerp0y2pqun4wd68jtn00fkxzcnn9ehhyec6qgqsz83qfwdpl28qqmc78ymlvhmxcsywdk5wrjnj36jryg488qwlrnzyjczlqsp9nyu4phcg6dqhlhzgxagfu7zh3d9re0sqp9ts2yfugvnnm9gxkcnnnkdpa084a6t520h5zhkxsdnghvpukvd43lastpwuh73k29qsy",
-                       "l+no1qcp4256ypqpq86q2pucnq42ngssx2an9wfujqerp0y2pqun4wd68jtn00fkxzcnn9ehhyec6qgqsz83qfwdpl28qqmc78ymlvhmxcsywdk5wrjnj36jryg488qwlrnzyjczlqsp9nyu4phcg6dqhlhzgxagfu7zh3d9re0sqp9ts2yfugvnnm9gxkcnnnkdpa084a6t520h5zhkxsdnghvpukvd43lastpwuh73k29qsy",
-                       "l+no1qcp4256ypqpq86q2pucnq42ngssx2an9wfujqerp0y2pqun4wd68jtn00fkxzcnn9ehhyec6qgqsz83qfwdpl28qqmc78ymlvhmxcsywdk5wrjnj36jryg488qwlrnzyjczlqsp9nyu4phcg6dqhlhzgxagfu7zh3d9re0sqp9ts2yfugvnnm9gxkcnnnkdpa084a6t520h5zhkxsdnghvpukvd43lastpwuh73k29qsy",
-                       "lno1qcp4256ypqpq+86q2pucnq42ngssx2an9wfujqerp0y2pqun4wd68jtn0+0fkxzcnn9ehhyec6qgqsz83qfwdpl28qqmc78ymlvhmxcsywdk5wrjnj36jryg488qwlrnzyjczlqsp9nyu4phcg6dqhlhzgxagfu7zh3d9re0+sqp9ts2yfugvnnm9gxkcnnnkdpa084a6t520h5zhkxsdnghvpukvd43lastpwuh73k29qs+y",
-                       "lno1qcp4256ypqpq+ 86q2pucnq42ngssx2an9wfujqerp0y2pqun4wd68jtn0+  0fkxzcnn9ehhyec6qgqsz83qfwdpl28qqmc78ymlvhmxcsywdk5wrjnj36jryg488qwlrnzyjczlqsp9nyu4phcg6dqhlhzgxagfu7zh3d9re0+\nsqp9ts2yfugvnnm9gxkcnnnkdpa084a6t520h5zhkxsdnghvpukvd43l+\r\nastpwuh73k29qs+\r  y",
-                       // Two blinded paths
-                       "lno1qcp4256ypqpq86q2pucnq42ngssx2an9wfujqerp0yg06qg2qdd7t628sgykwj5kuc837qmlv9m9gr7sq8ap6erfgacv26nhp8zzcqgzhdvttlk22pw8fmwqqrvzst792mj35ypylj886ljkcmug03wg6heqqsqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq6muh550qsfva9fdes0ruph7ctk2s8aqq06r4jxj3msc448wzwy9sqs9w6ckhlv55zuwnkuqqxc9qhu24h9rggzflyw04l9d3hcslzu340jqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq2pqun4wd68jtn00fkxzcnn9ehhyec6qgqsz83qfwdpl28qqmc78ymlvhmxcsywdk5wrjnj36jryg488qwlrnzyjczlqsp9nyu4phcg6dqhlhzgxagfu7zh3d9re0sqp9ts2yfugvnnm9gxkcnnnkdpa084a6t520h5zhkxsdnghvpukvd43lastpwuh73k29qsy",
+                       "lno1pqps7sjqpgtyzm3qv4uxzmtsd3jjqer9wd3hy6tsw35k7msjzfpy7nz5yqcnygrfdej82um5wf5k2uckyypwa3eyt44h6txtxquqh7lz5djge4afgfjn7k4rgrkuag0jsd5xvxg",
+                       "l+no1pqps7sjqpgtyzm3qv4uxzmtsd3jjqer9wd3hy6tsw35k7msjzfpy7nz5yqcnygrfdej82um5wf5k2uckyypwa3eyt44h6txtxquqh7lz5djge4afgfjn7k4rgrkuag0jsd5xvxg",
+                       "lno1pqps7sjqpgt+yzm3qv4uxzmtsd3jjqer9wd3hy6tsw3+5k7msjzfpy7nz5yqcn+ygrfdej82um5wf5k2uckyypwa3eyt44h6txtxquqh7lz5djge4afgfjn7k4rgrkuag0jsd+5xvxg",
+                       "lno1pqps7sjqpgt+ yzm3qv4uxzmtsd3jjqer9wd3hy6tsw3+  5k7msjzfpy7nz5yqcn+\nygrfdej82um5wf5k2uckyypwa3eyt44h6txtxquqh7lz5djge4afgfjn7k4rgrkuag0jsd+\r\n 5xvxg",
                ];
                for encoded_offer in &offers {
                        if let Err(e) = encoded_offer.parse::<Offer>() {
@@ -1490,11 +1483,11 @@ mod bech32_tests {
        fn fails_parsing_bech32_encoded_offers_with_invalid_continuations() {
                let offers = [
                        // BOLT 12 test vectors
-                       "lno1qcp4256ypqpq86q2pucnq42ngssx2an9wfujqerp0y2pqun4wd68jtn00fkxzcnn9ehhyec6qgqsz83qfwdpl28qqmc78ymlvhmxcsywdk5wrjnj36jryg488qwlrnzyjczlqsp9nyu4phcg6dqhlhzgxagfu7zh3d9re0sqp9ts2yfugvnnm9gxkcnnnkdpa084a6t520h5zhkxsdnghvpukvd43lastpwuh73k29qsy+",
-                       "lno1qcp4256ypqpq86q2pucnq42ngssx2an9wfujqerp0y2pqun4wd68jtn00fkxzcnn9ehhyec6qgqsz83qfwdpl28qqmc78ymlvhmxcsywdk5wrjnj36jryg488qwlrnzyjczlqsp9nyu4phcg6dqhlhzgxagfu7zh3d9re0sqp9ts2yfugvnnm9gxkcnnnkdpa084a6t520h5zhkxsdnghvpukvd43lastpwuh73k29qsy+ ",
-                       "+lno1qcp4256ypqpq86q2pucnq42ngssx2an9wfujqerp0y2pqun4wd68jtn00fkxzcnn9ehhyec6qgqsz83qfwdpl28qqmc78ymlvhmxcsywdk5wrjnj36jryg488qwlrnzyjczlqsp9nyu4phcg6dqhlhzgxagfu7zh3d9re0sqp9ts2yfugvnnm9gxkcnnnkdpa084a6t520h5zhkxsdnghvpukvd43lastpwuh73k29qsy",
-                       "+ lno1qcp4256ypqpq86q2pucnq42ngssx2an9wfujqerp0y2pqun4wd68jtn00fkxzcnn9ehhyec6qgqsz83qfwdpl28qqmc78ymlvhmxcsywdk5wrjnj36jryg488qwlrnzyjczlqsp9nyu4phcg6dqhlhzgxagfu7zh3d9re0sqp9ts2yfugvnnm9gxkcnnnkdpa084a6t520h5zhkxsdnghvpukvd43lastpwuh73k29qsy",
-                       "ln++o1qcp4256ypqpq86q2pucnq42ngssx2an9wfujqerp0y2pqun4wd68jtn00fkxzcnn9ehhyec6qgqsz83qfwdpl28qqmc78ymlvhmxcsywdk5wrjnj36jryg488qwlrnzyjczlqsp9nyu4phcg6dqhlhzgxagfu7zh3d9re0sqp9ts2yfugvnnm9gxkcnnnkdpa084a6t520h5zhkxsdnghvpukvd43lastpwuh73k29qsy",
+                       "lno1pqps7sjqpgtyzm3qv4uxzmtsd3jjqer9wd3hy6tsw35k7msjzfpy7nz5yqcnygrfdej82um5wf5k2uckyypwa3eyt44h6txtxquqh7lz5djge4afgfjn7k4rgrkuag0jsd5xvxg+",
+                       "lno1pqps7sjqpgtyzm3qv4uxzmtsd3jjqer9wd3hy6tsw35k7msjzfpy7nz5yqcnygrfdej82um5wf5k2uckyypwa3eyt44h6txtxquqh7lz5djge4afgfjn7k4rgrkuag0jsd5xvxg+ ",
+                       "+lno1pqps7sjqpgtyzm3qv4uxzmtsd3jjqer9wd3hy6tsw35k7msjzfpy7nz5yqcnygrfdej82um5wf5k2uckyypwa3eyt44h6txtxquqh7lz5djge4afgfjn7k4rgrkuag0jsd5xvxg",
+                       "+ lno1pqps7sjqpgtyzm3qv4uxzmtsd3jjqer9wd3hy6tsw35k7msjzfpy7nz5yqcnygrfdej82um5wf5k2uckyypwa3eyt44h6txtxquqh7lz5djge4afgfjn7k4rgrkuag0jsd5xvxg",
+                       "ln++o1pqps7sjqpgtyzm3qv4uxzmtsd3jjqer9wd3hy6tsw35k7msjzfpy7nz5yqcnygrfdej82um5wf5k2uckyypwa3eyt44h6txtxquqh7lz5djge4afgfjn7k4rgrkuag0jsd5xvxg",
                ];
                for encoded_offer in &offers {
                        match encoded_offer.parse::<Offer>() {
@@ -1507,7 +1500,7 @@ mod bech32_tests {
 
        #[test]
        fn fails_parsing_bech32_encoded_offer_with_invalid_hrp() {
-               let encoded_offer = "lni1qcp4256ypqpq86q2pucnq42ngssx2an9wfujqerp0y2pqun4wd68jtn00fkxzcnn9ehhyec6qgqsz83qfwdpl28qqmc78ymlvhmxcsywdk5wrjnj36jryg488qwlrnzyjczlqsp9nyu4phcg6dqhlhzgxagfu7zh3d9re0sqp9ts2yfugvnnm9gxkcnnnkdpa084a6t520h5zhkxsdnghvpukvd43lastpwuh73k29qsy";
+               let encoded_offer = "lni1pqps7sjqpgtyzm3qv4uxzmtsd3jjqer9wd3hy6tsw35k7msjzfpy7nz5yqcnygrfdej82um5wf5k2uckyypwa3eyt44h6txtxquqh7lz5djge4afgfjn7k4rgrkuag0jsd5xvxg";
                match encoded_offer.parse::<Offer>() {
                        Ok(_) => panic!("Valid offer: {}", encoded_offer),
                        Err(e) => assert_eq!(e, ParseError::InvalidBech32Hrp),
@@ -1516,7 +1509,7 @@ mod bech32_tests {
 
        #[test]
        fn fails_parsing_bech32_encoded_offer_with_invalid_bech32_data() {
-               let encoded_offer = "lno1qcp4256ypqpq86q2pucnq42ngssx2an9wfujqerp0y2pqun4wd68jtn00fkxzcnn9ehhyec6qgqsz83qfwdpl28qqmc78ymlvhmxcsywdk5wrjnj36jryg488qwlrnzyjczlqsp9nyu4phcg6dqhlhzgxagfu7zh3d9re0sqp9ts2yfugvnnm9gxkcnnnkdpa084a6t520h5zhkxsdnghvpukvd43lastpwuh73k29qso";
+               let encoded_offer = "lno1pqps7sjqpgtyzm3qv4uxzmtsd3jjqer9wd3hy6tsw35k7msjzfpy7nz5yqcnygrfdej82um5wf5k2uckyypwa3eyt44h6txtxquqh7lz5djge4afgfjn7k4rgrkuag0jsd5xvxo";
                match encoded_offer.parse::<Offer>() {
                        Ok(_) => panic!("Valid offer: {}", encoded_offer),
                        Err(e) => assert_eq!(e, ParseError::Bech32(bech32::Error::InvalidChar('o'))),
@@ -1525,7 +1518,7 @@ mod bech32_tests {
 
        #[test]
        fn fails_parsing_bech32_encoded_offer_with_invalid_tlv_data() {
-               let encoded_offer = "lno1qcp4256ypqpq86q2pucnq42ngssx2an9wfujqerp0y2pqun4wd68jtn00fkxzcnn9ehhyec6qgqsz83qfwdpl28qqmc78ymlvhmxcsywdk5wrjnj36jryg488qwlrnzyjczlqsp9nyu4phcg6dqhlhzgxagfu7zh3d9re0sqp9ts2yfugvnnm9gxkcnnnkdpa084a6t520h5zhkxsdnghvpukvd43lastpwuh73k29qsyqqqqq";
+               let encoded_offer = "lno1pqps7sjqpgtyzm3qv4uxzmtsd3jjqer9wd3hy6tsw35k7msjzfpy7nz5yqcnygrfdej82um5wf5k2uckyypwa3eyt44h6txtxquqh7lz5djge4afgfjn7k4rgrkuag0jsd5xvxgqqqqq";
                match encoded_offer.parse::<Offer>() {
                        Ok(_) => panic!("Valid offer: {}", encoded_offer),
                        Err(e) => assert_eq!(e, ParseError::Decode(DecodeError::InvalidValue)),
index 8fbc47d122cc88983694d0c5235b58e33e970169..07e759917acc54d5973c613e3e8d9826297f805c 100644 (file)
@@ -394,7 +394,7 @@ impl Refund {
        /// [`Duration`]: core::time::Duration
        #[cfg(feature = "std")]
        pub fn respond_with(
-               &self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, payment_hash: PaymentHash,
+               &self, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, payment_hash: PaymentHash,
                signing_pubkey: PublicKey,
        ) -> Result<InvoiceBuilder<ExplicitSigningPubkey>, SemanticError> {
                let created_at = std::time::SystemTime::now()
@@ -427,7 +427,7 @@ impl Refund {
        ///
        /// [`Invoice::created_at`]: crate::offers::invoice::Invoice::created_at
        pub fn respond_with_no_std(
-               &self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, payment_hash: PaymentHash,
+               &self, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, payment_hash: PaymentHash,
                signing_pubkey: PublicKey, created_at: Duration
        ) -> Result<InvoiceBuilder<ExplicitSigningPubkey>, SemanticError> {
                if self.features().requires_unknown_bits() {
@@ -447,7 +447,7 @@ impl Refund {
        /// [`Invoice`]: crate::offers::invoice::Invoice
        #[cfg(feature = "std")]
        pub fn respond_using_derived_keys<ES: Deref>(
-               &self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, payment_hash: PaymentHash,
+               &self, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, payment_hash: PaymentHash,
                expanded_key: &ExpandedKey, entropy_source: ES
        ) -> Result<InvoiceBuilder<DerivedSigningPubkey>, SemanticError>
        where
@@ -471,7 +471,7 @@ impl Refund {
        ///
        /// [`Invoice`]: crate::offers::invoice::Invoice
        pub fn respond_using_derived_keys_no_std<ES: Deref>(
-               &self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, payment_hash: PaymentHash,
+               &self, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, payment_hash: PaymentHash,
                created_at: core::time::Duration, expanded_key: &ExpandedKey, entropy_source: ES
        ) -> Result<InvoiceBuilder<DerivedSigningPubkey>, SemanticError>
        where
index 26c0d051223a6c26787055257a187fdfd95f1da7..230c6aa1628d96a180276d228d5742369062886f 100644 (file)
@@ -58,7 +58,7 @@ pub(super) fn privkey(byte: u8) -> SecretKey {
        SecretKey::from_slice(&[byte; 32]).unwrap()
 }
 
-pub(super) fn payment_paths() -> Vec<(BlindedPath, BlindedPayInfo)> {
+pub(super) fn payment_paths() -> Vec<(BlindedPayInfo, BlindedPath)> {
        let paths = vec![
                BlindedPath {
                        introduction_node_id: pubkey(40),
@@ -97,7 +97,7 @@ pub(super) fn payment_paths() -> Vec<(BlindedPath, BlindedPayInfo)> {
                },
        ];
 
-       paths.into_iter().zip(payinfo.into_iter()).collect()
+       payinfo.into_iter().zip(paths.into_iter()).collect()
 }
 
 pub(super) fn payment_hash() -> PaymentHash {
index 98fcdb680ee03abd1f2c8f3730c0b740bb75977f..d1b01b71eef0e041ef37007712bfd0eecd014ee9 100644 (file)
@@ -13,23 +13,30 @@ use crate::blinded_path::BlindedPath;
 use crate::sign::{NodeSigner, Recipient};
 use crate::ln::features::InitFeatures;
 use crate::ln::msgs::{self, DecodeError, OnionMessageHandler};
-use super::{CustomOnionMessageContents, CustomOnionMessageHandler, Destination, OnionMessageContents, OnionMessenger, SendError};
+use super::{CustomOnionMessageContents, CustomOnionMessageHandler, Destination, MessageRouter, OffersMessage, OffersMessageHandler, OnionMessageContents, OnionMessagePath, OnionMessenger, SendError};
 use crate::util::ser::{Writeable, Writer};
 use crate::util::test_utils;
 
 use bitcoin::network::constants::Network;
 use bitcoin::secp256k1::{PublicKey, Secp256k1};
 
-use core::sync::atomic::{AtomicU16, Ordering};
 use crate::io;
 use crate::io_extras::read_to_end;
-use crate::sync::Arc;
+use crate::sync::{Arc, Mutex};
+
+use crate::prelude::*;
 
 struct MessengerNode {
        keys_manager: Arc<test_utils::TestKeysInterface>,
-       messenger: OnionMessenger<Arc<test_utils::TestKeysInterface>, Arc<test_utils::TestKeysInterface>, Arc<test_utils::TestLogger>, Arc<TestCustomMessageHandler>>,
+       messenger: OnionMessenger<
+               Arc<test_utils::TestKeysInterface>,
+               Arc<test_utils::TestKeysInterface>,
+               Arc<test_utils::TestLogger>,
+               Arc<TestMessageRouter>,
+               Arc<TestOffersMessageHandler>,
+               Arc<TestCustomMessageHandler>
+       >,
        custom_message_handler: Arc<TestCustomMessageHandler>,
-       logger: Arc<test_utils::TestLogger>,
 }
 
 impl MessengerNode {
@@ -38,31 +45,67 @@ impl MessengerNode {
        }
 }
 
-#[derive(Clone)]
-struct TestCustomMessage {}
+struct TestMessageRouter {}
+
+impl MessageRouter for TestMessageRouter {
+       fn find_path(
+               &self, _sender: PublicKey, _peers: Vec<PublicKey>, destination: Destination
+       ) -> Result<OnionMessagePath, ()> {
+               Ok(OnionMessagePath {
+                       intermediate_nodes: vec![],
+                       destination,
+               })
+       }
+}
+
+struct TestOffersMessageHandler {}
+
+impl OffersMessageHandler for TestOffersMessageHandler {
+       fn handle_message(&self, _message: OffersMessage) -> Option<OffersMessage> {
+               None
+       }
+}
+
+#[derive(Clone, Debug, PartialEq)]
+enum TestCustomMessage {
+       Request,
+       Response,
+}
 
-const CUSTOM_MESSAGE_TYPE: u64 = 4242;
-const CUSTOM_MESSAGE_CONTENTS: [u8; 32] = [42; 32];
+const CUSTOM_REQUEST_MESSAGE_TYPE: u64 = 4242;
+const CUSTOM_RESPONSE_MESSAGE_TYPE: u64 = 4343;
+const CUSTOM_REQUEST_MESSAGE_CONTENTS: [u8; 32] = [42; 32];
+const CUSTOM_RESPONSE_MESSAGE_CONTENTS: [u8; 32] = [43; 32];
 
 impl CustomOnionMessageContents for TestCustomMessage {
        fn tlv_type(&self) -> u64 {
-               CUSTOM_MESSAGE_TYPE
+               match self {
+                       TestCustomMessage::Request => CUSTOM_REQUEST_MESSAGE_TYPE,
+                       TestCustomMessage::Response => CUSTOM_RESPONSE_MESSAGE_TYPE,
+               }
        }
 }
 
 impl Writeable for TestCustomMessage {
        fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
-               Ok(CUSTOM_MESSAGE_CONTENTS.write(w)?)
+               match self {
+                       TestCustomMessage::Request => Ok(CUSTOM_REQUEST_MESSAGE_CONTENTS.write(w)?),
+                       TestCustomMessage::Response => Ok(CUSTOM_RESPONSE_MESSAGE_CONTENTS.write(w)?),
+               }
        }
 }
 
 struct TestCustomMessageHandler {
-       num_messages_expected: AtomicU16,
+       expected_messages: Mutex<VecDeque<TestCustomMessage>>,
 }
 
 impl TestCustomMessageHandler {
        fn new() -> Self {
-               Self { num_messages_expected: AtomicU16::new(0) }
+               Self { expected_messages: Mutex::new(VecDeque::new()) }
+       }
+
+       fn expect_message(&self, message: TestCustomMessage) {
+               self.expected_messages.lock().unwrap().push_back(message);
        }
 }
 
@@ -73,22 +116,37 @@ impl Drop for TestCustomMessageHandler {
                                return;
                        }
                }
-               assert_eq!(self.num_messages_expected.load(Ordering::SeqCst), 0);
+               assert!(self.expected_messages.lock().unwrap().is_empty());
        }
 }
 
 impl CustomOnionMessageHandler for TestCustomMessageHandler {
        type CustomMessage = TestCustomMessage;
-       fn handle_custom_message(&self, _msg: Self::CustomMessage) {
-               self.num_messages_expected.fetch_sub(1, Ordering::SeqCst);
+       fn handle_custom_message(&self, msg: Self::CustomMessage) -> Option<Self::CustomMessage> {
+               match self.expected_messages.lock().unwrap().pop_front() {
+                       Some(expected_msg) => assert_eq!(expected_msg, msg),
+                       None => panic!("Unexpected message: {:?}", msg),
+               }
+
+               match msg {
+                       TestCustomMessage::Request => Some(TestCustomMessage::Response),
+                       TestCustomMessage::Response => None,
+               }
        }
        fn read_custom_message<R: io::Read>(&self, message_type: u64, buffer: &mut R) -> Result<Option<Self::CustomMessage>, DecodeError> where Self: Sized {
-               if message_type == CUSTOM_MESSAGE_TYPE {
-                       let buf = read_to_end(buffer)?;
-                       assert_eq!(buf, CUSTOM_MESSAGE_CONTENTS);
-                       return Ok(Some(TestCustomMessage {}))
+               match message_type {
+                       CUSTOM_REQUEST_MESSAGE_TYPE => {
+                               let buf = read_to_end(buffer)?;
+                               assert_eq!(buf, CUSTOM_REQUEST_MESSAGE_CONTENTS);
+                               Ok(Some(TestCustomMessage::Request))
+                       },
+                       CUSTOM_RESPONSE_MESSAGE_TYPE => {
+                               let buf = read_to_end(buffer)?;
+                               assert_eq!(buf, CUSTOM_RESPONSE_MESSAGE_CONTENTS);
+                               Ok(Some(TestCustomMessage::Response))
+                       },
+                       _ => Ok(None),
                }
-               Ok(None)
        }
 }
 
@@ -98,19 +156,23 @@ fn create_nodes(num_messengers: u8) -> Vec<MessengerNode> {
                let logger = Arc::new(test_utils::TestLogger::with_id(format!("node {}", i)));
                let seed = [i as u8; 32];
                let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&seed, Network::Testnet));
+               let message_router = Arc::new(TestMessageRouter {});
+               let offers_message_handler = Arc::new(TestOffersMessageHandler {});
                let custom_message_handler = Arc::new(TestCustomMessageHandler::new());
                nodes.push(MessengerNode {
                        keys_manager: keys_manager.clone(),
-                       messenger: OnionMessenger::new(keys_manager.clone(), keys_manager.clone(), logger.clone(), custom_message_handler.clone()),
+                       messenger: OnionMessenger::new(
+                               keys_manager.clone(), keys_manager, logger.clone(), message_router,
+                               offers_message_handler, custom_message_handler.clone()
+                       ),
                        custom_message_handler,
-                       logger,
                });
        }
        for idx in 0..num_messengers - 1 {
                let i = idx as usize;
                let mut features = InitFeatures::empty();
                features.set_onion_messages_optional();
-               let init_msg = msgs::Init { features, remote_network_address: None };
+               let init_msg = msgs::Init { features, networks: None, remote_network_address: None };
                nodes[i].messenger.peer_connected(&nodes[i + 1].get_node_pk(), &init_msg.clone(), true).unwrap();
                nodes[i + 1].messenger.peer_connected(&nodes[i].get_node_pk(), &init_msg.clone(), false).unwrap();
        }
@@ -118,7 +180,6 @@ fn create_nodes(num_messengers: u8) -> Vec<MessengerNode> {
 }
 
 fn pass_along_path(path: &Vec<MessengerNode>) {
-       path[path.len() - 1].custom_message_handler.num_messages_expected.fetch_add(1, Ordering::SeqCst);
        let mut prev_node = &path[0];
        for node in path.into_iter().skip(1) {
                let events = prev_node.messenger.release_pending_msgs();
@@ -135,42 +196,62 @@ fn pass_along_path(path: &Vec<MessengerNode>) {
 #[test]
 fn one_hop() {
        let nodes = create_nodes(2);
-       let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
-
-       nodes[0].messenger.send_onion_message(&[], Destination::Node(nodes[1].get_node_pk()), test_msg, None).unwrap();
+       let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
+
+       let path = OnionMessagePath {
+               intermediate_nodes: vec![],
+               destination: Destination::Node(nodes[1].get_node_pk()),
+       };
+       nodes[0].messenger.send_onion_message(path, test_msg, None).unwrap();
+       nodes[1].custom_message_handler.expect_message(TestCustomMessage::Response);
        pass_along_path(&nodes);
 }
 
 #[test]
 fn two_unblinded_hops() {
        let nodes = create_nodes(3);
-       let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
-
-       nodes[0].messenger.send_onion_message(&[nodes[1].get_node_pk()], Destination::Node(nodes[2].get_node_pk()), test_msg, None).unwrap();
+       let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
+
+       let path = OnionMessagePath {
+               intermediate_nodes: vec![nodes[1].get_node_pk()],
+               destination: Destination::Node(nodes[2].get_node_pk()),
+       };
+       nodes[0].messenger.send_onion_message(path, test_msg, None).unwrap();
+       nodes[2].custom_message_handler.expect_message(TestCustomMessage::Response);
        pass_along_path(&nodes);
 }
 
 #[test]
 fn two_unblinded_two_blinded() {
        let nodes = create_nodes(5);
-       let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
+       let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
 
        let secp_ctx = Secp256k1::new();
        let blinded_path = BlindedPath::new_for_message(&[nodes[3].get_node_pk(), nodes[4].get_node_pk()], &*nodes[4].keys_manager, &secp_ctx).unwrap();
+       let path = OnionMessagePath {
+               intermediate_nodes: vec![nodes[1].get_node_pk(), nodes[2].get_node_pk()],
+               destination: Destination::BlindedPath(blinded_path),
+       };
 
-       nodes[0].messenger.send_onion_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk()], Destination::BlindedPath(blinded_path), test_msg, None).unwrap();
+       nodes[0].messenger.send_onion_message(path, test_msg, None).unwrap();
+       nodes[4].custom_message_handler.expect_message(TestCustomMessage::Response);
        pass_along_path(&nodes);
 }
 
 #[test]
 fn three_blinded_hops() {
        let nodes = create_nodes(4);
-       let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
+       let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
 
        let secp_ctx = Secp256k1::new();
        let blinded_path = BlindedPath::new_for_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk(), nodes[3].get_node_pk()], &*nodes[3].keys_manager, &secp_ctx).unwrap();
+       let path = OnionMessagePath {
+               intermediate_nodes: vec![],
+               destination: Destination::BlindedPath(blinded_path),
+       };
 
-       nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), test_msg, None).unwrap();
+       nodes[0].messenger.send_onion_message(path, test_msg, None).unwrap();
+       nodes[3].custom_message_handler.expect_message(TestCustomMessage::Response);
        pass_along_path(&nodes);
 }
 
@@ -178,11 +259,15 @@ fn three_blinded_hops() {
 fn too_big_packet_error() {
        // Make sure we error as expected if a packet is too big to send.
        let nodes = create_nodes(2);
-       let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
+       let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
 
        let hop_node_id = nodes[1].get_node_pk();
-       let hops = [hop_node_id; 400];
-       let err = nodes[0].messenger.send_onion_message(&hops, Destination::Node(hop_node_id), test_msg, None).unwrap_err();
+       let hops = vec![hop_node_id; 400];
+       let path = OnionMessagePath {
+               intermediate_nodes: hops,
+               destination: Destination::Node(hop_node_id),
+       };
+       let err = nodes[0].messenger.send_onion_message(path, test_msg, None).unwrap_err();
        assert_eq!(err, SendError::TooBigPacket);
 }
 
@@ -191,17 +276,27 @@ fn we_are_intro_node() {
        // If we are sending straight to a blinded path and we are the introduction node, we need to
        // advance the blinded path by 1 hop so the second hop is the new introduction node.
        let mut nodes = create_nodes(3);
-       let test_msg = TestCustomMessage {};
+       let test_msg = TestCustomMessage::Response;
 
        let secp_ctx = Secp256k1::new();
        let blinded_path = BlindedPath::new_for_message(&[nodes[0].get_node_pk(), nodes[1].get_node_pk(), nodes[2].get_node_pk()], &*nodes[2].keys_manager, &secp_ctx).unwrap();
+       let path = OnionMessagePath {
+               intermediate_nodes: vec![],
+               destination: Destination::BlindedPath(blinded_path),
+       };
 
-       nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), OnionMessageContents::Custom(test_msg.clone()), None).unwrap();
+       nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg.clone()), None).unwrap();
+       nodes[2].custom_message_handler.expect_message(TestCustomMessage::Response);
        pass_along_path(&nodes);
 
        // Try with a two-hop blinded path where we are the introduction node.
        let blinded_path = BlindedPath::new_for_message(&[nodes[0].get_node_pk(), nodes[1].get_node_pk()], &*nodes[1].keys_manager, &secp_ctx).unwrap();
-       nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), OnionMessageContents::Custom(test_msg), None).unwrap();
+       let path = OnionMessagePath {
+               intermediate_nodes: vec![],
+               destination: Destination::BlindedPath(blinded_path),
+       };
+       nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg), None).unwrap();
+       nodes[1].custom_message_handler.expect_message(TestCustomMessage::Response);
        nodes.remove(2);
        pass_along_path(&nodes);
 }
@@ -210,47 +305,67 @@ fn we_are_intro_node() {
 fn invalid_blinded_path_error() {
        // Make sure we error as expected if a provided blinded path has 0 or 1 hops.
        let nodes = create_nodes(3);
-       let test_msg = TestCustomMessage {};
+       let test_msg = TestCustomMessage::Response;
 
        // 0 hops
        let secp_ctx = Secp256k1::new();
        let mut blinded_path = BlindedPath::new_for_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk()], &*nodes[2].keys_manager, &secp_ctx).unwrap();
        blinded_path.blinded_hops.clear();
-       let err = nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), OnionMessageContents::Custom(test_msg.clone()), None).unwrap_err();
+       let path = OnionMessagePath {
+               intermediate_nodes: vec![],
+               destination: Destination::BlindedPath(blinded_path),
+       };
+       let err = nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg.clone()), None).unwrap_err();
        assert_eq!(err, SendError::TooFewBlindedHops);
 
        // 1 hop
        let mut blinded_path = BlindedPath::new_for_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk()], &*nodes[2].keys_manager, &secp_ctx).unwrap();
        blinded_path.blinded_hops.remove(0);
        assert_eq!(blinded_path.blinded_hops.len(), 1);
-       let err = nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), OnionMessageContents::Custom(test_msg), None).unwrap_err();
+       let path = OnionMessagePath {
+               intermediate_nodes: vec![],
+               destination: Destination::BlindedPath(blinded_path),
+       };
+       let err = nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg), None).unwrap_err();
        assert_eq!(err, SendError::TooFewBlindedHops);
 }
 
 #[test]
 fn reply_path() {
-       let nodes = create_nodes(4);
-       let test_msg = TestCustomMessage {};
+       let mut nodes = create_nodes(4);
+       let test_msg = TestCustomMessage::Request;
        let secp_ctx = Secp256k1::new();
 
        // Destination::Node
+       let path = OnionMessagePath {
+               intermediate_nodes: vec![nodes[1].get_node_pk(), nodes[2].get_node_pk()],
+               destination: Destination::Node(nodes[3].get_node_pk()),
+       };
        let reply_path = BlindedPath::new_for_message(&[nodes[2].get_node_pk(), nodes[1].get_node_pk(), nodes[0].get_node_pk()], &*nodes[0].keys_manager, &secp_ctx).unwrap();
-       nodes[0].messenger.send_onion_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk()], Destination::Node(nodes[3].get_node_pk()), OnionMessageContents::Custom(test_msg.clone()), Some(reply_path)).unwrap();
+       nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg.clone()), Some(reply_path)).unwrap();
+       nodes[3].custom_message_handler.expect_message(TestCustomMessage::Request);
        pass_along_path(&nodes);
        // Make sure the last node successfully decoded the reply path.
-       nodes[3].logger.assert_log_contains(
-               "lightning::onion_message::messenger",
-               &format!("Received an onion message with path_id None and a reply_path"), 1);
+       nodes[0].custom_message_handler.expect_message(TestCustomMessage::Response);
+       nodes.reverse();
+       pass_along_path(&nodes);
 
        // Destination::BlindedPath
        let blinded_path = BlindedPath::new_for_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk(), nodes[3].get_node_pk()], &*nodes[3].keys_manager, &secp_ctx).unwrap();
+       let path = OnionMessagePath {
+               intermediate_nodes: vec![],
+               destination: Destination::BlindedPath(blinded_path),
+       };
        let reply_path = BlindedPath::new_for_message(&[nodes[2].get_node_pk(), nodes[1].get_node_pk(), nodes[0].get_node_pk()], &*nodes[0].keys_manager, &secp_ctx).unwrap();
 
-       nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), OnionMessageContents::Custom(test_msg), Some(reply_path)).unwrap();
+       nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg), Some(reply_path)).unwrap();
+       nodes[3].custom_message_handler.expect_message(TestCustomMessage::Request);
+       pass_along_path(&nodes);
+
+       // Make sure the last node successfully decoded the reply path.
+       nodes[0].custom_message_handler.expect_message(TestCustomMessage::Response);
+       nodes.reverse();
        pass_along_path(&nodes);
-       nodes[3].logger.assert_log_contains(
-               "lightning::onion_message::messenger",
-               &format!("Received an onion message with path_id None and a reply_path"), 2);
 }
 
 #[test]
@@ -270,18 +385,26 @@ fn invalid_custom_message_type() {
        }
 
        let test_msg = OnionMessageContents::Custom(InvalidCustomMessage {});
-       let err = nodes[0].messenger.send_onion_message(&[], Destination::Node(nodes[1].get_node_pk()), test_msg, None).unwrap_err();
+       let path = OnionMessagePath {
+               intermediate_nodes: vec![],
+               destination: Destination::Node(nodes[1].get_node_pk()),
+       };
+       let err = nodes[0].messenger.send_onion_message(path, test_msg, None).unwrap_err();
        assert_eq!(err, SendError::InvalidMessage);
 }
 
 #[test]
 fn peer_buffer_full() {
        let nodes = create_nodes(2);
-       let test_msg = TestCustomMessage {};
+       let test_msg = TestCustomMessage::Request;
+       let path = OnionMessagePath {
+               intermediate_nodes: vec![],
+               destination: Destination::Node(nodes[1].get_node_pk()),
+       };
        for _ in 0..188 { // Based on MAX_PER_PEER_BUFFER_SIZE in OnionMessenger
-               nodes[0].messenger.send_onion_message(&[], Destination::Node(nodes[1].get_node_pk()), OnionMessageContents::Custom(test_msg.clone()), None).unwrap();
+               nodes[0].messenger.send_onion_message(path.clone(), OnionMessageContents::Custom(test_msg.clone()), None).unwrap();
        }
-       let err = nodes[0].messenger.send_onion_message(&[], Destination::Node(nodes[1].get_node_pk()), OnionMessageContents::Custom(test_msg), None).unwrap_err();
+       let err = nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg), None).unwrap_err();
        assert_eq!(err, SendError::BufferFull);
 }
 
@@ -291,13 +414,18 @@ fn many_hops() {
        // of size [`crate::onion_message::packet::BIG_PACKET_HOP_DATA_LEN`].
        let num_nodes: usize = 25;
        let nodes = create_nodes(num_nodes as u8);
-       let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
+       let test_msg = TestCustomMessage::Response;
 
-       let mut intermediates = vec![];
+       let mut intermediate_nodes = vec![];
        for i in 1..(num_nodes-1) {
-               intermediates.push(nodes[i].get_node_pk());
+               intermediate_nodes.push(nodes[i].get_node_pk());
        }
 
-       nodes[0].messenger.send_onion_message(&intermediates, Destination::Node(nodes[num_nodes-1].get_node_pk()), test_msg, None).unwrap();
+       let path = OnionMessagePath {
+               intermediate_nodes,
+               destination: Destination::Node(nodes[num_nodes-1].get_node_pk()),
+       };
+       nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg), None).unwrap();
+       nodes[num_nodes-1].custom_message_handler.expect_message(TestCustomMessage::Response);
        pass_along_path(&nodes);
 }
index 5171422cb895ac45ace605871fb44c0630104e41..a3613605cdebbd1ff71014d827b70782e043a160 100644 (file)
@@ -23,6 +23,7 @@ use crate::ln::msgs::{self, OnionMessageHandler};
 use crate::ln::onion_utils;
 use crate::ln::peer_handler::IgnoringMessageHandler;
 pub use super::packet::{CustomOnionMessageContents, OnionMessageContents};
+use super::offers::OffersMessageHandler;
 use super::packet::{BIG_PACKET_HOP_DATA_LEN, ForwardControlTlvs, Packet, Payload, ReceiveControlTlvs, SMALL_PACKET_HOP_DATA_LEN};
 use crate::util::logger::Logger;
 use crate::util::ser::Writeable;
@@ -45,7 +46,7 @@ use crate::prelude::*;
 /// # use lightning::blinded_path::BlindedPath;
 /// # use lightning::sign::KeysManager;
 /// # use lightning::ln::peer_handler::IgnoringMessageHandler;
-/// # use lightning::onion_message::{CustomOnionMessageContents, Destination, OnionMessageContents, OnionMessenger};
+/// # use lightning::onion_message::{CustomOnionMessageContents, Destination, MessageRouter, OnionMessageContents, OnionMessagePath, OnionMessenger};
 /// # use lightning::util::logger::{Logger, Record};
 /// # use lightning::util::ser::{Writeable, Writer};
 /// # use lightning::io;
@@ -54,6 +55,12 @@ use crate::prelude::*;
 /// # impl Logger for FakeLogger {
 /// #     fn log(&self, record: &Record) { unimplemented!() }
 /// # }
+/// # struct FakeMessageRouter {}
+/// # impl MessageRouter for FakeMessageRouter {
+/// #     fn find_path(&self, sender: PublicKey, peers: Vec<PublicKey>, destination: Destination) -> Result<OnionMessagePath, ()> {
+/// #         unimplemented!()
+/// #     }
+/// # }
 /// # let seed = [42u8; 32];
 /// # let time = Duration::from_secs(123456);
 /// # let keys_manager = KeysManager::new(&seed, time.as_secs(), time.subsec_nanos());
@@ -63,10 +70,15 @@ use crate::prelude::*;
 /// # let hop_node_id1 = PublicKey::from_secret_key(&secp_ctx, &node_secret);
 /// # let (hop_node_id2, hop_node_id3, hop_node_id4) = (hop_node_id1, hop_node_id1, hop_node_id1);
 /// # let destination_node_id = hop_node_id1;
-/// # let your_custom_message_handler = IgnoringMessageHandler {};
+/// # let message_router = Arc::new(FakeMessageRouter {});
+/// # let custom_message_handler = IgnoringMessageHandler {};
+/// # let offers_message_handler = IgnoringMessageHandler {};
 /// // Create the onion messenger. This must use the same `keys_manager` as is passed to your
 /// // ChannelManager.
-/// let onion_messenger = OnionMessenger::new(&keys_manager, &keys_manager, logger, &your_custom_message_handler);
+/// let onion_messenger = OnionMessenger::new(
+///     &keys_manager, &keys_manager, logger, message_router, &offers_message_handler,
+///     &custom_message_handler
+/// );
 ///
 /// # struct YourCustomMessage {}
 /// impl Writeable for YourCustomMessage {
@@ -82,11 +94,14 @@ use crate::prelude::*;
 ///    }
 /// }
 /// // Send a custom onion message to a node id.
-/// let intermediate_hops = [hop_node_id1, hop_node_id2];
+/// let path = OnionMessagePath {
+///    intermediate_nodes: vec![hop_node_id1, hop_node_id2],
+///    destination: Destination::Node(destination_node_id),
+/// };
 /// let reply_path = None;
 /// # let your_custom_message = YourCustomMessage {};
 /// let message = OnionMessageContents::Custom(your_custom_message);
-/// onion_messenger.send_onion_message(&intermediate_hops, Destination::Node(destination_node_id), message, reply_path);
+/// onion_messenger.send_onion_message(path, message, reply_path);
 ///
 /// // Create a blinded path to yourself, for someone to send an onion message to.
 /// # let your_node_id = hop_node_id1;
@@ -94,32 +109,72 @@ use crate::prelude::*;
 /// let blinded_path = BlindedPath::new_for_message(&hops, &keys_manager, &secp_ctx).unwrap();
 ///
 /// // Send a custom onion message to a blinded path.
-/// # let intermediate_hops = [hop_node_id1, hop_node_id2];
+/// let path = OnionMessagePath {
+///    intermediate_nodes: vec![hop_node_id1, hop_node_id2],
+///    destination: Destination::BlindedPath(blinded_path),
+/// };
 /// let reply_path = None;
 /// # let your_custom_message = YourCustomMessage {};
 /// let message = OnionMessageContents::Custom(your_custom_message);
-/// onion_messenger.send_onion_message(&intermediate_hops, Destination::BlindedPath(blinded_path), message, reply_path);
+/// onion_messenger.send_onion_message(path, message, reply_path);
 /// ```
 ///
 /// [offers]: <https://github.com/lightning/bolts/pull/798>
 /// [`OnionMessenger`]: crate::onion_message::OnionMessenger
-pub struct OnionMessenger<ES: Deref, NS: Deref, L: Deref, CMH: Deref>
-       where ES::Target: EntropySource,
-                 NS::Target: NodeSigner,
-                 L::Target: Logger,
-                 CMH:: Target: CustomOnionMessageHandler,
+pub struct OnionMessenger<ES: Deref, NS: Deref, L: Deref, MR: Deref, OMH: Deref, CMH: Deref>
+where
+       ES::Target: EntropySource,
+       NS::Target: NodeSigner,
+       L::Target: Logger,
+       MR::Target: MessageRouter,
+       OMH::Target: OffersMessageHandler,
+       CMH:: Target: CustomOnionMessageHandler,
 {
        entropy_source: ES,
        node_signer: NS,
        logger: L,
        pending_messages: Mutex<HashMap<PublicKey, VecDeque<msgs::OnionMessage>>>,
        secp_ctx: Secp256k1<secp256k1::All>,
+       message_router: MR,
+       offers_handler: OMH,
        custom_handler: CMH,
-       // Coming soon:
-       // invoice_handler: InvoiceHandler,
+}
+
+/// A trait defining behavior for routing an [`OnionMessage`].
+///
+/// [`OnionMessage`]: msgs::OnionMessage
+pub trait MessageRouter {
+       /// Returns a route for sending an [`OnionMessage`] to the given [`Destination`].
+       ///
+       /// [`OnionMessage`]: msgs::OnionMessage
+       fn find_path(
+               &self, sender: PublicKey, peers: Vec<PublicKey>, destination: Destination
+       ) -> Result<OnionMessagePath, ()>;
+}
+
+/// A [`MessageRouter`] that always fails.
+pub struct DefaultMessageRouter;
+
+impl MessageRouter for DefaultMessageRouter {
+       fn find_path(
+               &self, _sender: PublicKey, _peers: Vec<PublicKey>, _destination: Destination
+       ) -> Result<OnionMessagePath, ()> {
+               Err(())
+       }
+}
+
+/// A path for sending an [`msgs::OnionMessage`].
+#[derive(Clone)]
+pub struct OnionMessagePath {
+       /// Nodes on the path between the sender and the destination.
+       pub intermediate_nodes: Vec<PublicKey>,
+
+       /// The recipient of the message.
+       pub destination: Destination,
 }
 
 /// The destination of an onion message.
+#[derive(Clone)]
 pub enum Destination {
        /// We're sending this onion message to a node.
        Node(PublicKey),
@@ -180,22 +235,31 @@ pub trait CustomOnionMessageHandler {
        /// The message known to the handler. To support multiple message types, you may want to make this
        /// an enum with a variant for each supported message.
        type CustomMessage: CustomOnionMessageContents;
-       /// Called with the custom message that was received.
-       fn handle_custom_message(&self, msg: Self::CustomMessage);
+
+       /// Called with the custom message that was received, returning a response to send, if any.
+       fn handle_custom_message(&self, msg: Self::CustomMessage) -> Option<Self::CustomMessage>;
+
        /// Read a custom message of type `message_type` from `buffer`, returning `Ok(None)` if the
        /// message type is unknown.
        fn read_custom_message<R: io::Read>(&self, message_type: u64, buffer: &mut R) -> Result<Option<Self::CustomMessage>, msgs::DecodeError>;
 }
 
-impl<ES: Deref, NS: Deref, L: Deref, CMH: Deref> OnionMessenger<ES, NS, L, CMH>
-       where ES::Target: EntropySource,
-                 NS::Target: NodeSigner,
-                 L::Target: Logger,
-                 CMH::Target: CustomOnionMessageHandler,
+impl<ES: Deref, NS: Deref, L: Deref, MR: Deref, OMH: Deref, CMH: Deref>
+OnionMessenger<ES, NS, L, MR, OMH, CMH>
+where
+       ES::Target: EntropySource,
+       NS::Target: NodeSigner,
+       L::Target: Logger,
+       MR::Target: MessageRouter,
+       OMH::Target: OffersMessageHandler,
+       CMH::Target: CustomOnionMessageHandler,
 {
        /// Constructs a new `OnionMessenger` to send, forward, and delegate received onion messages to
        /// their respective handlers.
-       pub fn new(entropy_source: ES, node_signer: NS, logger: L, custom_handler: CMH) -> Self {
+       pub fn new(
+               entropy_source: ES, node_signer: NS, logger: L, message_router: MR, offers_handler: OMH,
+               custom_handler: CMH
+       ) -> Self {
                let mut secp_ctx = Secp256k1::new();
                secp_ctx.seeded_randomize(&entropy_source.get_secure_random_bytes());
                OnionMessenger {
@@ -204,20 +268,27 @@ impl<ES: Deref, NS: Deref, L: Deref, CMH: Deref> OnionMessenger<ES, NS, L, CMH>
                        pending_messages: Mutex::new(HashMap::new()),
                        secp_ctx,
                        logger,
+                       message_router,
+                       offers_handler,
                        custom_handler,
                }
        }
 
-       /// Send an onion message with contents `message` to `destination`, routing it through `intermediate_nodes`.
+       /// Send an onion message with contents `message` to the destination of `path`.
+       ///
        /// See [`OnionMessenger`] for example usage.
-       pub fn send_onion_message<T: CustomOnionMessageContents>(&self, intermediate_nodes: &[PublicKey], mut destination: Destination, message: OnionMessageContents<T>, reply_path: Option<BlindedPath>) -> Result<(), SendError> {
+       pub fn send_onion_message<T: CustomOnionMessageContents>(
+               &self, path: OnionMessagePath, message: OnionMessageContents<T>,
+               reply_path: Option<BlindedPath>
+       ) -> Result<(), SendError> {
+               let OnionMessagePath { intermediate_nodes, mut destination } = path;
                if let Destination::BlindedPath(BlindedPath { ref blinded_hops, .. }) = destination {
                        if blinded_hops.len() < 2 {
                                return Err(SendError::TooFewBlindedHops);
                        }
                }
-               let OnionMessageContents::Custom(ref msg) = message;
-               if msg.tlv_type() < 64 { return Err(SendError::InvalidMessage) }
+
+               if message.tlv_type() < 64 { return Err(SendError::InvalidMessage) }
 
                // If we are sending straight to a blinded path and we are the introduction node, we need to
                // advance the blinded path by 1 hop so the second hop is the new introduction node.
@@ -244,7 +315,7 @@ impl<ES: Deref, NS: Deref, L: Deref, CMH: Deref> OnionMessenger<ES, NS, L, CMH>
                        }
                };
                let (packet_payloads, packet_keys) = packet_payloads_and_keys(
-                       &self.secp_ctx, intermediate_nodes, destination, message, reply_path, &blinding_secret)
+                       &self.secp_ctx, &intermediate_nodes, destination, message, reply_path, &blinding_secret)
                        .map_err(|e| SendError::Secp256k1(e))?;
 
                let prng_seed = self.entropy_source.get_secure_random_bytes();
@@ -262,6 +333,56 @@ impl<ES: Deref, NS: Deref, L: Deref, CMH: Deref> OnionMessenger<ES, NS, L, CMH>
                }
        }
 
+       fn respond_with_onion_message<T: CustomOnionMessageContents>(
+               &self, response: OnionMessageContents<T>, path_id: Option<[u8; 32]>,
+               reply_path: Option<BlindedPath>
+       ) {
+               let sender = match self.node_signer.get_node_id(Recipient::Node) {
+                       Ok(node_id) => node_id,
+                       Err(_) => {
+                               log_warn!(
+                                       self.logger, "Unable to retrieve node id when responding to onion message with \
+                                       path_id {:02x?}", path_id
+                               );
+                               return;
+                       }
+               };
+
+               let peers = self.pending_messages.lock().unwrap().keys().copied().collect();
+
+               let destination = match reply_path {
+                       Some(reply_path) => Destination::BlindedPath(reply_path),
+                       None => {
+                               log_trace!(
+                                       self.logger, "Missing reply path when responding to onion message with path_id \
+                                       {:02x?}", path_id
+                               );
+                               return;
+                       },
+               };
+
+               let path = match self.message_router.find_path(sender, peers, destination) {
+                       Ok(path) => path,
+                       Err(()) => {
+                               log_trace!(
+                                       self.logger, "Failed to find path when responding to onion message with \
+                                       path_id {:02x?}", path_id
+                               );
+                               return;
+                       },
+               };
+
+               log_trace!(self.logger, "Responding to onion message with path_id {:02x?}", path_id);
+
+               if let Err(e) = self.send_onion_message(path, response, None) {
+                       log_trace!(
+                               self.logger, "Failed responding to onion message with path_id {:02x?}: {:?}",
+                               path_id, e
+                       );
+                       return;
+               }
+       }
+
        #[cfg(test)]
        pub(super) fn release_pending_msgs(&self) -> HashMap<PublicKey, VecDeque<msgs::OnionMessage>> {
                let mut pending_msgs = self.pending_messages.lock().unwrap();
@@ -298,11 +419,15 @@ fn outbound_buffer_full(peer_node_id: &PublicKey, buffer: &HashMap<PublicKey, Ve
        false
 }
 
-impl<ES: Deref, NS: Deref, L: Deref, CMH: Deref> OnionMessageHandler for OnionMessenger<ES, NS, L, CMH>
-       where ES::Target: EntropySource,
-                 NS::Target: NodeSigner,
-                 L::Target: Logger,
-                 CMH::Target: CustomOnionMessageHandler + Sized,
+impl<ES: Deref, NS: Deref, L: Deref, MR: Deref, OMH: Deref, CMH: Deref> OnionMessageHandler
+for OnionMessenger<ES, NS, L, MR, OMH, CMH>
+where
+       ES::Target: EntropySource,
+       NS::Target: NodeSigner,
+       L::Target: Logger,
+       MR::Target: MessageRouter,
+       OMH::Target: OffersMessageHandler,
+       CMH::Target: CustomOnionMessageHandler,
 {
        /// Handle an incoming onion message. Currently, if a message was destined for us we will log, but
        /// soon we'll delegate the onion message to a handler that can generate invoices or send
@@ -331,17 +456,30 @@ impl<ES: Deref, NS: Deref, L: Deref, CMH: Deref> OnionMessageHandler for OnionMe
                                }
                        }
                };
-               match onion_utils::decode_next_untagged_hop(onion_decode_ss, &msg.onion_routing_packet.hop_data[..],
-                       msg.onion_routing_packet.hmac, (control_tlvs_ss, &*self.custom_handler))
-               {
+               match onion_utils::decode_next_untagged_hop(
+                       onion_decode_ss, &msg.onion_routing_packet.hop_data[..], msg.onion_routing_packet.hmac,
+                       (control_tlvs_ss, &*self.custom_handler, &*self.logger)
+               ) {
                        Ok((Payload::Receive::<<<CMH as Deref>::Target as CustomOnionMessageHandler>::CustomMessage> {
                                message, control_tlvs: ReceiveControlTlvs::Unblinded(ReceiveTlvs { path_id }), reply_path,
                        }, None)) => {
-                               log_info!(self.logger,
+                               log_trace!(self.logger,
                                        "Received an onion message with path_id {:02x?} and {} reply_path",
                                                path_id, if reply_path.is_some() { "a" } else { "no" });
-                               match message {
-                                       OnionMessageContents::Custom(msg) => self.custom_handler.handle_custom_message(msg),
+
+                               let response = match message {
+                                       OnionMessageContents::Offers(msg) => {
+                                               self.offers_handler.handle_message(msg)
+                                                       .map(|msg| OnionMessageContents::Offers(msg))
+                                       },
+                                       OnionMessageContents::Custom(msg) => {
+                                               self.custom_handler.handle_custom_message(msg)
+                                                       .map(|msg| OnionMessageContents::Custom(msg))
+                                       },
+                               };
+
+                               if let Some(response) = response {
+                                       self.respond_with_onion_message(response, path_id, reply_path);
                                }
                        },
                        Ok((Payload::Forward(ForwardControlTlvs::Unblinded(ForwardTlvs {
@@ -443,11 +581,15 @@ impl<ES: Deref, NS: Deref, L: Deref, CMH: Deref> OnionMessageHandler for OnionMe
        }
 }
 
-impl<ES: Deref, NS: Deref, L: Deref, CMH: Deref> OnionMessageProvider for OnionMessenger<ES, NS, L, CMH>
-       where ES::Target: EntropySource,
-                 NS::Target: NodeSigner,
-                 L::Target: Logger,
-                 CMH::Target: CustomOnionMessageHandler,
+impl<ES: Deref, NS: Deref, L: Deref, MR: Deref, OMH: Deref, CMH: Deref> OnionMessageProvider
+for OnionMessenger<ES, NS, L, MR, OMH, CMH>
+where
+       ES::Target: EntropySource,
+       NS::Target: NodeSigner,
+       L::Target: Logger,
+       MR::Target: MessageRouter,
+       OMH::Target: OffersMessageHandler,
+       CMH::Target: CustomOnionMessageHandler,
 {
        fn next_onion_message_for_peer(&self, peer_node_id: PublicKey) -> Option<msgs::OnionMessage> {
                let mut pending_msgs = self.pending_messages.lock().unwrap();
@@ -467,7 +609,15 @@ impl<ES: Deref, NS: Deref, L: Deref, CMH: Deref> OnionMessageProvider for OnionM
 ///
 /// [`SimpleArcChannelManager`]: crate::ln::channelmanager::SimpleArcChannelManager
 /// [`SimpleArcPeerManager`]: crate::ln::peer_handler::SimpleArcPeerManager
-pub type SimpleArcOnionMessenger<L> = OnionMessenger<Arc<KeysManager>, Arc<KeysManager>, Arc<L>, IgnoringMessageHandler>;
+pub type SimpleArcOnionMessenger<L> = OnionMessenger<
+       Arc<KeysManager>,
+       Arc<KeysManager>,
+       Arc<L>,
+       Arc<DefaultMessageRouter>,
+       IgnoringMessageHandler,
+       IgnoringMessageHandler
+>;
+
 /// Useful for simplifying the parameters of [`SimpleRefChannelManager`] and
 /// [`SimpleRefPeerManager`]. See their docs for more details.
 ///
@@ -475,7 +625,14 @@ pub type SimpleArcOnionMessenger<L> = OnionMessenger<Arc<KeysManager>, Arc<KeysM
 ///
 /// [`SimpleRefChannelManager`]: crate::ln::channelmanager::SimpleRefChannelManager
 /// [`SimpleRefPeerManager`]: crate::ln::peer_handler::SimpleRefPeerManager
-pub type SimpleRefOnionMessenger<'a, 'b, L> = OnionMessenger<&'a KeysManager, &'a KeysManager, &'b L, IgnoringMessageHandler>;
+pub type SimpleRefOnionMessenger<'a, 'b, 'c, L> = OnionMessenger<
+       &'a KeysManager,
+       &'a KeysManager,
+       &'b L,
+       &'c DefaultMessageRouter,
+       IgnoringMessageHandler,
+       IgnoringMessageHandler
+>;
 
 /// Construct onion packet payloads and keys for sending an onion message along the given
 /// `unblinded_path` to the given `destination`.
index 713b83c62d67d3e0a2fdcc39fd5a004d8a744e6c..806f832ff73f557de046d08ebe67e5d87e0a028f 100644 (file)
 //! [blinded paths]: crate::blinded_path::BlindedPath
 
 mod messenger;
+mod offers;
 mod packet;
 #[cfg(test)]
 mod functional_tests;
 
 // Re-export structs so they can be imported with just the `onion_message::` module prefix.
-pub use self::messenger::{CustomOnionMessageContents, CustomOnionMessageHandler, Destination, OnionMessageContents, OnionMessenger, SendError, SimpleArcOnionMessenger, SimpleRefOnionMessenger};
+pub use self::messenger::{CustomOnionMessageContents, CustomOnionMessageHandler, DefaultMessageRouter, Destination, MessageRouter, OnionMessageContents, OnionMessagePath, OnionMessenger, SendError, SimpleArcOnionMessenger, SimpleRefOnionMessenger};
+pub use self::offers::{OffersMessage, OffersMessageHandler};
 pub(crate) use self::packet::{ControlTlvs, Packet};
diff --git a/lightning/src/onion_message/offers.rs b/lightning/src/onion_message/offers.rs
new file mode 100644 (file)
index 0000000..f82afdd
--- /dev/null
@@ -0,0 +1,118 @@
+// This file is Copyright its original authors, visible in version control
+// history.
+//
+// This file is 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.
+// You may not use this file except in accordance with one or both of these
+// licenses.
+
+//! Message handling for BOLT 12 Offers.
+
+use core::convert::TryFrom;
+use crate::io::{self, Read};
+use crate::ln::msgs::DecodeError;
+use crate::offers::invoice_error::InvoiceError;
+use crate::offers::invoice_request::InvoiceRequest;
+use crate::offers::invoice::Invoice;
+use crate::offers::parse::ParseError;
+use crate::util::logger::Logger;
+use crate::util::ser::{Readable, ReadableArgs, Writeable, Writer};
+
+use crate::prelude::*;
+
+// TLV record types for the `onionmsg_tlv` TLV stream as defined in BOLT 4.
+const INVOICE_REQUEST_TLV_TYPE: u64 = 64;
+const INVOICE_TLV_TYPE: u64 = 66;
+const INVOICE_ERROR_TLV_TYPE: u64 = 68;
+
+/// A handler for an [`OnionMessage`] containing a BOLT 12 Offers message as its payload.
+///
+/// [`OnionMessage`]: crate::ln::msgs::OnionMessage
+pub trait OffersMessageHandler {
+       /// Handles the given message by either responding with an [`Invoice`], sending a payment, or
+       /// replying with an error.
+       fn handle_message(&self, message: OffersMessage) -> Option<OffersMessage>;
+}
+
+/// Possible BOLT 12 Offers messages sent and received via an [`OnionMessage`].
+///
+/// [`OnionMessage`]: crate::ln::msgs::OnionMessage
+#[derive(Debug)]
+pub enum OffersMessage {
+       /// A request for an [`Invoice`] for a particular [`Offer`].
+       ///
+       /// [`Offer`]: crate::offers::offer::Offer
+       InvoiceRequest(InvoiceRequest),
+
+       /// An [`Invoice`] sent in response to an [`InvoiceRequest`] or a [`Refund`].
+       ///
+       /// [`Refund`]: crate::offers::refund::Refund
+       Invoice(Invoice),
+
+       /// An error from handling an [`OffersMessage`].
+       InvoiceError(InvoiceError),
+}
+
+impl OffersMessage {
+       /// Returns whether `tlv_type` corresponds to a TLV record for Offers.
+       pub fn is_known_type(tlv_type: u64) -> bool {
+               match tlv_type {
+                       INVOICE_REQUEST_TLV_TYPE | INVOICE_TLV_TYPE | INVOICE_ERROR_TLV_TYPE => true,
+                       _ => false,
+               }
+       }
+
+       /// The TLV record type for the message as used in an `onionmsg_tlv` TLV stream.
+       pub fn tlv_type(&self) -> u64 {
+               match self {
+                       OffersMessage::InvoiceRequest(_) => INVOICE_REQUEST_TLV_TYPE,
+                       OffersMessage::Invoice(_) => INVOICE_TLV_TYPE,
+                       OffersMessage::InvoiceError(_) => INVOICE_ERROR_TLV_TYPE,
+               }
+       }
+
+       fn parse(tlv_type: u64, bytes: Vec<u8>) -> Result<Self, ParseError> {
+               match tlv_type {
+                       INVOICE_REQUEST_TLV_TYPE => Ok(Self::InvoiceRequest(InvoiceRequest::try_from(bytes)?)),
+                       INVOICE_TLV_TYPE => Ok(Self::Invoice(Invoice::try_from(bytes)?)),
+                       _ => Err(ParseError::Decode(DecodeError::InvalidValue)),
+               }
+       }
+}
+
+impl Writeable for OffersMessage {
+       fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
+               match self {
+                       OffersMessage::InvoiceRequest(message) => message.write(w),
+                       OffersMessage::Invoice(message) => message.write(w),
+                       OffersMessage::InvoiceError(message) => message.write(w),
+               }
+       }
+}
+
+impl<L: Logger + ?Sized> ReadableArgs<(u64, &L)> for OffersMessage {
+       fn read<R: Read>(r: &mut R, read_args: (u64, &L)) -> Result<Self, DecodeError> {
+               let (tlv_type, logger) = read_args;
+               if tlv_type == INVOICE_ERROR_TLV_TYPE {
+                       return Ok(Self::InvoiceError(InvoiceError::read(r)?));
+               }
+
+               let mut bytes = Vec::new();
+               r.read_to_end(&mut bytes).unwrap();
+
+               match Self::parse(tlv_type, bytes) {
+                       Ok(message) => Ok(message),
+                       Err(ParseError::Decode(e)) => Err(e),
+                       Err(ParseError::InvalidSemantics(e)) => {
+                               log_trace!(logger, "Invalid semantics for TLV type {}: {:?}", tlv_type, e);
+                               Err(DecodeError::InvalidValue)
+                       },
+                       Err(ParseError::InvalidSignature(e)) => {
+                               log_trace!(logger, "Invalid signature for TLV type {}: {:?}", tlv_type, e);
+                               Err(DecodeError::InvalidValue)
+                       },
+                       Err(_) => Err(DecodeError::InvalidValue),
+               }
+       }
+}
index 2fb2407dbdd093bdbe4ae260e31f12fbe4722d8a..1c3595c3712526191e0e5f1707dfa51d82ca7505 100644 (file)
@@ -16,7 +16,9 @@ use crate::blinded_path::{BlindedPath, ForwardTlvs, ReceiveTlvs};
 use crate::ln::msgs::DecodeError;
 use crate::ln::onion_utils;
 use super::messenger::CustomOnionMessageHandler;
+use super::offers::OffersMessage;
 use crate::util::chacha20poly1305rfc::{ChaChaPolyReadAdapter, ChaChaPolyWriteAdapter};
+use crate::util::logger::Logger;
 use crate::util::ser::{BigSize, FixedLengthReader, LengthRead, LengthReadable, LengthReadableArgs, Readable, ReadableArgs, Writeable, Writer};
 
 use core::cmp;
@@ -108,10 +110,8 @@ pub(super) enum Payload<T: CustomOnionMessageContents> {
 /// The contents of an onion message. In the context of offers, this would be the invoice, invoice
 /// request, or invoice error.
 pub enum OnionMessageContents<T: CustomOnionMessageContents> {
-       // Coming soon:
-       // Invoice,
-       // InvoiceRequest,
-       // InvoiceError,
+       /// A message related to BOLT 12 Offers.
+       Offers(OffersMessage),
        /// A custom onion message specified by the user.
        Custom(T),
 }
@@ -122,6 +122,7 @@ impl<T: CustomOnionMessageContents> OnionMessageContents<T> {
        /// This is not exported to bindings users as methods on non-cloneable enums are not currently exportable
        pub fn tlv_type(&self) -> u64 {
                match self {
+                       &OnionMessageContents::Offers(ref msg) => msg.tlv_type(),
                        &OnionMessageContents::Custom(ref msg) => msg.tlv_type(),
                }
        }
@@ -131,6 +132,7 @@ impl<T: CustomOnionMessageContents> OnionMessageContents<T> {
 impl<T: CustomOnionMessageContents> Writeable for OnionMessageContents<T> {
        fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
                match self {
+                       OnionMessageContents::Offers(msg) => Ok(msg.write(w)?),
                        OnionMessageContents::Custom(msg) => Ok(msg.write(w)?),
                }
        }
@@ -201,9 +203,10 @@ impl<T: CustomOnionMessageContents> Writeable for (Payload<T>, [u8; 32]) {
 }
 
 // Uses the provided secret to simultaneously decode and decrypt the control TLVs and data TLV.
-impl<H: CustomOnionMessageHandler> ReadableArgs<(SharedSecret, &H)> for Payload<<H as CustomOnionMessageHandler>::CustomMessage> {
-       fn read<R: Read>(r: &mut R, args: (SharedSecret, &H)) -> Result<Self, DecodeError> {
-               let (encrypted_tlvs_ss, handler) = args;
+impl<H: CustomOnionMessageHandler + ?Sized, L: Logger + ?Sized>
+ReadableArgs<(SharedSecret, &H, &L)> for Payload<<H as CustomOnionMessageHandler>::CustomMessage> {
+       fn read<R: Read>(r: &mut R, args: (SharedSecret, &H, &L)) -> Result<Self, DecodeError> {
+               let (encrypted_tlvs_ss, handler, logger) = args;
 
                let v: BigSize = Readable::read(r)?;
                let mut rd = FixedLengthReader::new(r, v.0);
@@ -221,13 +224,19 @@ impl<H: CustomOnionMessageHandler> ReadableArgs<(SharedSecret, &H)> for Payload<
                        if message_type.is_some() { return Err(DecodeError::InvalidValue) }
 
                        message_type = Some(msg_type);
-                       match handler.read_custom_message(msg_type, msg_reader) {
-                               Ok(Some(msg)) => {
-                                       message = Some(msg);
+                       match msg_type {
+                               tlv_type if OffersMessage::is_known_type(tlv_type) => {
+                                       let msg = OffersMessage::read(msg_reader, (tlv_type, logger))?;
+                                       message = Some(OnionMessageContents::Offers(msg));
                                        Ok(true)
                                },
-                               Ok(None) => Ok(false),
-                               Err(e) => Err(e),
+                               _ => match handler.read_custom_message(msg_type, msg_reader)? {
+                                       Some(msg) => {
+                                               message = Some(OnionMessageContents::Custom(msg));
+                                               Ok(true)
+                                       },
+                                       None => Ok(false),
+                               },
                        }
                });
                rd.eat_remaining().map_err(|_| DecodeError::ShortRead)?;
@@ -241,13 +250,12 @@ impl<H: CustomOnionMessageHandler> ReadableArgs<(SharedSecret, &H)> for Payload<
                                Ok(Payload::Forward(ForwardControlTlvs::Unblinded(tlvs)))
                        },
                        Some(ChaChaPolyReadAdapter { readable: ControlTlvs::Receive(tlvs)}) => {
-                               if message.is_none() { return Err(DecodeError::InvalidValue) }
                                Ok(Payload::Receive {
                                        control_tlvs: ReceiveControlTlvs::Unblinded(tlvs),
                                        reply_path,
-                                       message: OnionMessageContents::Custom(message.unwrap()),
+                                       message: message.ok_or(DecodeError::InvalidValue)?,
                                })
-                       }
+                       },
                }
        }
 }
index 2cc746f221c678e3a434d4105d988fc113ff52c4..5ef107e299cfc1d3d1de02dfa043fc0a58b5df3d 100644 (file)
@@ -40,7 +40,7 @@ use crate::io_extras::{copy, sink};
 use crate::prelude::*;
 use core::{cmp, fmt};
 use core::convert::TryFrom;
-use crate::sync::{RwLock, RwLockReadGuard};
+use crate::sync::{RwLock, RwLockReadGuard, LockTestExt};
 #[cfg(feature = "std")]
 use core::sync::atomic::{AtomicUsize, Ordering};
 use crate::sync::Mutex;
@@ -366,6 +366,11 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
                        },
                }
        }
+
+       /// Gets the genesis hash for this network graph.
+       pub fn get_genesis_hash(&self) -> BlockHash {
+               self.genesis_hash
+       }
 }
 
 macro_rules! secp_verify_sig {
@@ -988,7 +993,7 @@ impl<'a> DirectedChannelInfo<'a> {
                                htlc_maximum_msat = cmp::min(htlc_maximum_msat, capacity_msat);
                                EffectiveCapacity::Total { capacity_msat, htlc_maximum_msat: htlc_maximum_msat }
                        },
-                       None => EffectiveCapacity::MaximumHTLC { amount_msat: htlc_maximum_msat },
+                       None => EffectiveCapacity::AdvertisedMaxHTLC { amount_msat: htlc_maximum_msat },
                };
 
                Self {
@@ -1042,7 +1047,7 @@ pub enum EffectiveCapacity {
                liquidity_msat: u64,
        },
        /// The maximum HTLC amount in one direction as advertised on the gossip network.
-       MaximumHTLC {
+       AdvertisedMaxHTLC {
                /// The maximum HTLC amount denominated in millisatoshi.
                amount_msat: u64,
        },
@@ -1056,6 +1061,11 @@ pub enum EffectiveCapacity {
        /// A capacity sufficient to route any payment, typically used for private channels provided by
        /// an invoice.
        Infinite,
+       /// The maximum HTLC amount as provided by an invoice route hint.
+       HintMaxHTLC {
+               /// The maximum HTLC amount denominated in millisatoshi.
+               amount_msat: u64,
+       },
        /// A capacity that is unknown possibly because either the chain state is unavailable to know
        /// the total capacity or the `htlc_maximum_msat` was not advertised on the gossip network.
        Unknown,
@@ -1070,8 +1080,9 @@ impl EffectiveCapacity {
        pub fn as_msat(&self) -> u64 {
                match self {
                        EffectiveCapacity::ExactLiquidity { liquidity_msat } => *liquidity_msat,
-                       EffectiveCapacity::MaximumHTLC { amount_msat } => *amount_msat,
+                       EffectiveCapacity::AdvertisedMaxHTLC { amount_msat } => *amount_msat,
                        EffectiveCapacity::Total { capacity_msat, .. } => *capacity_msat,
+                       EffectiveCapacity::HintMaxHTLC { amount_msat } => *amount_msat,
                        EffectiveCapacity::Infinite => u64::max_value(),
                        EffectiveCapacity::Unknown => UNKNOWN_CHANNEL_CAPACITY_MSAT,
                }
@@ -1350,9 +1361,14 @@ impl<L: Deref> fmt::Display for NetworkGraph<L> where L::Target: Logger {
 impl<L: Deref> Eq for NetworkGraph<L> where L::Target: Logger {}
 impl<L: Deref> PartialEq for NetworkGraph<L> where L::Target: Logger {
        fn eq(&self, other: &Self) -> bool {
-               self.genesis_hash == other.genesis_hash &&
-                       *self.channels.read().unwrap() == *other.channels.read().unwrap() &&
-                       *self.nodes.read().unwrap() == *other.nodes.read().unwrap()
+               // For a total lockorder, sort by position in memory and take the inner locks in that order.
+               // (Assumes that we can't move within memory while a lock is held).
+               let ord = ((self as *const _) as usize) < ((other as *const _) as usize);
+               let a = if ord { (&self.channels, &self.nodes) } else { (&other.channels, &other.nodes) };
+               let b = if ord { (&other.channels, &other.nodes) } else { (&self.channels, &self.nodes) };
+               let (channels_a, channels_b) = (a.0.unsafe_well_ordered_double_lock_self(), b.0.unsafe_well_ordered_double_lock_self());
+               let (nodes_a, nodes_b) = (a.1.unsafe_well_ordered_double_lock_self(), b.1.unsafe_well_ordered_double_lock_self());
+               self.genesis_hash.eq(&other.genesis_hash) && channels_a.eq(&channels_b) && nodes_a.eq(&nodes_b)
        }
 }
 
@@ -1593,7 +1609,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
 
                if msg.chain_hash != self.genesis_hash {
                        return Err(LightningError {
-                               err: "Channel announcement chain hash does not match genesis hash".to_owned(), 
+                               err: "Channel announcement chain hash does not match genesis hash".to_owned(),
                                action: ErrorAction::IgnoreAndLog(Level::Debug),
                        });
                }
@@ -2903,7 +2919,7 @@ pub(crate) mod tests {
 
                // It should ignore if gossip_queries feature is not enabled
                {
-                       let init_msg = Init { features: InitFeatures::empty(), remote_network_address: None };
+                       let init_msg = Init { features: InitFeatures::empty(), networks: None, remote_network_address: None };
                        gossip_sync.peer_connected(&node_id_1, &init_msg, true).unwrap();
                        let events = gossip_sync.get_and_clear_pending_msg_events();
                        assert_eq!(events.len(), 0);
@@ -2913,7 +2929,7 @@ pub(crate) mod tests {
                {
                        let mut features = InitFeatures::empty();
                        features.set_gossip_queries_optional();
-                       let init_msg = Init { features, remote_network_address: None };
+                       let init_msg = Init { features, networks: None, remote_network_address: None };
                        gossip_sync.peer_connected(&node_id_1, &init_msg, true).unwrap();
                        let events = gossip_sync.get_and_clear_pending_msg_events();
                        assert_eq!(events.len(), 1);
@@ -3406,31 +3422,28 @@ pub(crate) mod tests {
        }
 }
 
-#[cfg(all(test, feature = "_bench_unstable"))]
-mod benches {
+#[cfg(ldk_bench)]
+pub mod benches {
        use super::*;
-
-       use test::Bencher;
        use std::io::Read;
+       use criterion::{black_box, Criterion};
 
-       #[bench]
-       fn read_network_graph(bench: &mut Bencher) {
+       pub fn read_network_graph(bench: &mut Criterion) {
                let logger = crate::util::test_utils::TestLogger::new();
                let mut d = crate::routing::router::bench_utils::get_route_file().unwrap();
                let mut v = Vec::new();
                d.read_to_end(&mut v).unwrap();
-               bench.iter(|| {
-                       let _ = NetworkGraph::read(&mut std::io::Cursor::new(&v), &logger).unwrap();
-               });
+               bench.bench_function("read_network_graph", |b| b.iter(||
+                       NetworkGraph::read(&mut std::io::Cursor::new(black_box(&v)), &logger).unwrap()
+               ));
        }
 
-       #[bench]
-       fn write_network_graph(bench: &mut Bencher) {
+       pub fn write_network_graph(bench: &mut Criterion) {
                let logger = crate::util::test_utils::TestLogger::new();
                let mut d = crate::routing::router::bench_utils::get_route_file().unwrap();
                let net_graph = NetworkGraph::read(&mut d, &logger).unwrap();
-               bench.iter(|| {
-                       let _ = net_graph.encode();
-               });
+               bench.bench_function("write_network_graph", |b| b.iter(||
+                       black_box(&net_graph).encode()
+               ));
        }
 }
index b33e021ab4fc30357a1124885c45b076419aed61..a83af28fcd62f9cae1ae268011ea5218d274c136 100644 (file)
@@ -18,7 +18,7 @@ use crate::ln::PaymentHash;
 use crate::ln::channelmanager::{ChannelDetails, PaymentId};
 use crate::ln::features::{Bolt12InvoiceFeatures, ChannelFeatures, InvoiceFeatures, NodeFeatures};
 use crate::ln::msgs::{DecodeError, ErrorAction, LightningError, MAX_VALUE_MSAT};
-use crate::offers::invoice::BlindedPayInfo;
+use crate::offers::invoice::{BlindedPayInfo, Invoice as Bolt12Invoice};
 use crate::routing::gossip::{DirectedChannelInfo, EffectiveCapacity, ReadOnlyNetworkGraph, NetworkGraph, NodeId, RoutingFees};
 use crate::routing::scoring::{ChannelUsage, LockableScore, Score};
 use crate::util::ser::{Writeable, Readable, ReadableArgs, Writer};
@@ -27,15 +27,15 @@ use crate::util::chacha20::ChaCha20;
 
 use crate::io;
 use crate::prelude::*;
-use crate::sync::{Mutex, MutexGuard};
+use crate::sync::{Mutex};
 use alloc::collections::BinaryHeap;
 use core::{cmp, fmt};
-use core::ops::Deref;
+use core::ops::{Deref, DerefMut};
 
 /// A [`Router`] implemented using [`find_route`].
 pub struct DefaultRouter<G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref, SP: Sized, Sc: Score<ScoreParams = SP>> where
        L::Target: Logger,
-       S::Target: for <'a> LockableScore<'a, Locked = MutexGuard<'a, Sc>>,
+       S::Target: for <'a> LockableScore<'a, Score = Sc>,
 {
        network_graph: G,
        logger: L,
@@ -46,7 +46,7 @@ pub struct DefaultRouter<G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref,
 
 impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref, SP: Sized, Sc: Score<ScoreParams = SP>> DefaultRouter<G, L, S, SP, Sc> where
        L::Target: Logger,
-       S::Target: for <'a> LockableScore<'a, Locked = MutexGuard<'a, Sc>>,
+       S::Target: for <'a> LockableScore<'a, Score = Sc>,
 {
        /// Creates a new router.
        pub fn new(network_graph: G, logger: L, random_seed_bytes: [u8; 32], scorer: S, score_params: SP) -> Self {
@@ -55,9 +55,9 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref, SP: Sized, Sc: Scor
        }
 }
 
-impl< G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref,  SP: Sized, Sc: Score<ScoreParams = SP>> Router for DefaultRouter<G, L, S, SP, Sc> where
+impl< G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref, SP: Sized, Sc: Score<ScoreParams = SP>> Router for DefaultRouter<G, L, S, SP, Sc> where
        L::Target: Logger,
-       S::Target: for <'a> LockableScore<'a, Locked = MutexGuard<'a, Sc>>,
+       S::Target: for <'a> LockableScore<'a, Score = Sc>,
 {
        fn find_route(
                &self,
@@ -73,7 +73,7 @@ impl< G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref,  SP: Sized, Sc: Sc
                };
                find_route(
                        payer, params, &self.network_graph, first_hops, &*self.logger,
-                       &ScorerAccountingForInFlightHtlcs::new(self.scorer.lock(), inflight_htlcs),
+                       &ScorerAccountingForInFlightHtlcs::new(self.scorer.lock().deref_mut(), inflight_htlcs),
                        &self.score_params,
                        &random_seed_bytes
                )
@@ -104,15 +104,15 @@ pub trait Router {
 /// [`find_route`].
 ///
 /// [`Score`]: crate::routing::scoring::Score
-pub struct ScorerAccountingForInFlightHtlcs<'a, S: Score> {
-       scorer: S,
+pub struct ScorerAccountingForInFlightHtlcs<'a, S: Score<ScoreParams = SP>, SP: Sized> {
+       scorer: &'a mut S,
        // Maps a channel's short channel id and its direction to the liquidity used up.
        inflight_htlcs: &'a InFlightHtlcs,
 }
 
-impl<'a, S: Score> ScorerAccountingForInFlightHtlcs<'a, S> {
+impl<'a, S: Score<ScoreParams = SP>, SP: Sized> ScorerAccountingForInFlightHtlcs<'a, S, SP> {
        /// Initialize a new `ScorerAccountingForInFlightHtlcs`.
-       pub fn new(scorer: S, inflight_htlcs: &'a InFlightHtlcs) -> Self {
+       pub fn new(scorer:  &'a mut S, inflight_htlcs: &'a InFlightHtlcs) -> Self {
                ScorerAccountingForInFlightHtlcs {
                        scorer,
                        inflight_htlcs
@@ -121,11 +121,11 @@ impl<'a, S: Score> ScorerAccountingForInFlightHtlcs<'a, S> {
 }
 
 #[cfg(c_bindings)]
-impl<'a, S: Score> Writeable for ScorerAccountingForInFlightHtlcs<'a, S> {
+impl<'a, S: Score<ScoreParams = SP>, SP: Sized> Writeable for ScorerAccountingForInFlightHtlcs<'a, S, SP> {
        fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> { self.scorer.write(writer) }
 }
 
-impl<'a, S: Score> Score for ScorerAccountingForInFlightHtlcs<'a, S> {
+impl<'a, S: Score<ScoreParams = SP>, SP: Sized> Score for ScorerAccountingForInFlightHtlcs<'a, S, SP>  {
        type ScoreParams = S::ScoreParams;
        fn channel_penalty_msat(&self, short_channel_id: u64, source: &NodeId, target: &NodeId, usage: ChannelUsage, score_params: &Self::ScoreParams) -> u64 {
                if let Some(used_liquidity) = self.inflight_htlcs.used_liquidity_msat(
@@ -204,6 +204,15 @@ impl InFlightHtlcs {
                }
        }
 
+       /// Adds a known HTLC given the public key of the HTLC source, target, and short channel
+       /// id.
+       pub fn add_inflight_htlc(&mut self, source: &NodeId, target: &NodeId, channel_scid: u64, used_msat: u64){
+               self.0
+                       .entry((channel_scid, source < target))
+                       .and_modify(|used_liquidity_msat| *used_liquidity_msat += used_msat)
+                       .or_insert(used_msat);
+       }
+
        /// Returns liquidity in msat given the public key of the HTLC source, target, and short channel
        /// id.
        pub fn used_liquidity_msat(&self, source: &NodeId, target: &NodeId, channel_scid: u64) -> Option<u64> {
@@ -417,7 +426,7 @@ impl Readable for Route {
                let blinded_tails = blinded_tails.unwrap_or(Vec::new());
                if blinded_tails.len() != 0 {
                        if blinded_tails.len() != paths.len() { return Err(DecodeError::InvalidValue) }
-                       for (mut path, blinded_tail_opt) in paths.iter_mut().zip(blinded_tails.into_iter()) {
+                       for (path, blinded_tail_opt) in paths.iter_mut().zip(blinded_tails.into_iter()) {
                                path.blinded_tail = blinded_tail_opt;
                        }
                }
@@ -481,6 +490,8 @@ pub const DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA: u32 = 1008;
 // limits, but for now more than 10 paths likely carries too much one-path failure.
 pub const DEFAULT_MAX_PATH_COUNT: u8 = 10;
 
+const DEFAULT_MAX_CHANNEL_SATURATION_POW_HALF: u8 = 2;
+
 // The median hop CLTV expiry delta currently seen in the network.
 const MEDIAN_HOP_CLTV_EXPIRY_DELTA: u32 = 40;
 
@@ -567,7 +578,7 @@ impl ReadableArgs<u32> for PaymentParameters {
                        (2, features, (option: ReadableArgs, payee_pubkey.is_some())),
                        (3, max_path_count, (default_value, DEFAULT_MAX_PATH_COUNT)),
                        (4, route_hints, vec_type),
-                       (5, max_channel_saturation_power_of_half, (default_value, 2)),
+                       (5, max_channel_saturation_power_of_half, (default_value, DEFAULT_MAX_CHANNEL_SATURATION_POW_HALF)),
                        (6, expiry_time, option),
                        (7, previously_failed_channels, vec_type),
                        (8, blinded_route_hints, optional_vec),
@@ -612,7 +623,7 @@ impl PaymentParameters {
                        expiry_time: None,
                        max_total_cltv_expiry_delta: DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA,
                        max_path_count: DEFAULT_MAX_PATH_COUNT,
-                       max_channel_saturation_power_of_half: 2,
+                       max_channel_saturation_power_of_half: DEFAULT_MAX_CHANNEL_SATURATION_POW_HALF,
                        previously_failed_channels: Vec::new(),
                }
        }
@@ -621,12 +632,53 @@ impl PaymentParameters {
        ///
        /// The `final_cltv_expiry_delta` should match the expected final CLTV delta the recipient has
        /// provided.
-       pub fn for_keysend(payee_pubkey: PublicKey, final_cltv_expiry_delta: u32) -> Self {
-               Self::from_node_id(payee_pubkey, final_cltv_expiry_delta).with_bolt11_features(InvoiceFeatures::for_keysend()).expect("PaymentParameters::from_node_id should always initialize the payee as unblinded")
+       ///
+       /// Note that MPP keysend is not widely supported yet. The `allow_mpp` lets you choose
+       /// whether your router will be allowed to find a multi-part route for this payment. If you
+       /// set `allow_mpp` to true, you should ensure a payment secret is set on send, likely via
+       /// [`RecipientOnionFields::secret_only`].
+       ///
+       /// [`RecipientOnionFields::secret_only`]: crate::ln::channelmanager::RecipientOnionFields::secret_only
+       pub fn for_keysend(payee_pubkey: PublicKey, final_cltv_expiry_delta: u32, allow_mpp: bool) -> Self {
+               Self::from_node_id(payee_pubkey, final_cltv_expiry_delta)
+                       .with_bolt11_features(InvoiceFeatures::for_keysend(allow_mpp))
+                       .expect("PaymentParameters::from_node_id should always initialize the payee as unblinded")
+       }
+
+       /// Creates parameters for paying to a blinded payee from the provided invoice. Sets
+       /// [`Payee::Blinded::route_hints`], [`Payee::Blinded::features`], and
+       /// [`PaymentParameters::expiry_time`].
+       pub fn from_bolt12_invoice(invoice: &Bolt12Invoice) -> Self {
+               Self::blinded(invoice.payment_paths().to_vec())
+                       .with_bolt12_features(invoice.features().clone()).unwrap()
+                       .with_expiry_time(invoice.created_at().as_secs().saturating_add(invoice.relative_expiry().as_secs()))
+       }
+
+       fn blinded(blinded_route_hints: Vec<(BlindedPayInfo, BlindedPath)>) -> Self {
+               Self {
+                       payee: Payee::Blinded { route_hints: blinded_route_hints, features: None },
+                       expiry_time: None,
+                       max_total_cltv_expiry_delta: DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA,
+                       max_path_count: DEFAULT_MAX_PATH_COUNT,
+                       max_channel_saturation_power_of_half: DEFAULT_MAX_CHANNEL_SATURATION_POW_HALF,
+                       previously_failed_channels: Vec::new(),
+               }
        }
 
-       /// Includes the payee's features. Errors if the parameters were initialized with blinded payment
-       /// paths.
+       /// Includes the payee's features. Errors if the parameters were not initialized with
+       /// [`PaymentParameters::from_bolt12_invoice`].
+       ///
+       /// This is not exported to bindings users since bindings don't support move semantics
+       pub fn with_bolt12_features(self, features: Bolt12InvoiceFeatures) -> Result<Self, ()> {
+               match self.payee {
+                       Payee::Clear { .. } => Err(()),
+                       Payee::Blinded { route_hints, .. } =>
+                               Ok(Self { payee: Payee::Blinded { route_hints, features: Some(features) }, ..self })
+               }
+       }
+
+       /// Includes the payee's features. Errors if the parameters were initialized with
+       /// [`PaymentParameters::from_bolt12_invoice`].
        ///
        /// This is not exported to bindings users since bindings don't support move semantics
        pub fn with_bolt11_features(self, features: InvoiceFeatures) -> Result<Self, ()> {
@@ -642,7 +694,7 @@ impl PaymentParameters {
        }
 
        /// Includes hints for routing to the payee. Errors if the parameters were initialized with
-       /// blinded payment paths.
+       /// [`PaymentParameters::from_bolt12_invoice`].
        ///
        /// This is not exported to bindings users since bindings don't support move semantics
        pub fn with_route_hints(self, route_hints: Vec<RouteHint>) -> Result<Self, ()> {
@@ -678,7 +730,8 @@ impl PaymentParameters {
                Self { max_path_count, ..self }
        }
 
-       /// Includes a limit for the maximum number of payment paths that may be used.
+       /// Includes a limit for the maximum share of a channel's total capacity that can be sent over, as
+       /// a power of 1/2. See [`PaymentParameters::max_channel_saturation_power_of_half`].
        ///
        /// This is not exported to bindings users since bindings don't support move semantics
        pub fn with_max_channel_saturation_power_of_half(self, max_channel_saturation_power_of_half: u8) -> Self {
@@ -751,6 +804,19 @@ impl Payee {
                        _ => None,
                }
        }
+       fn blinded_route_hints(&self) -> &[(BlindedPayInfo, BlindedPath)] {
+               match self {
+                       Self::Blinded { route_hints, .. } => &route_hints[..],
+                       Self::Clear { .. } => &[]
+               }
+       }
+
+       fn unblinded_route_hints(&self) -> &[RouteHint] {
+               match self {
+                       Self::Blinded { .. } => &[],
+                       Self::Clear { route_hints, .. } => &route_hints[..]
+               }
+       }
 }
 
 enum FeaturesRef<'a> {
@@ -895,18 +961,34 @@ enum CandidateRouteHop<'a> {
                info: DirectedChannelInfo<'a>,
                short_channel_id: u64,
        },
-       /// A hop to the payee found in the payment invoice, though not necessarily a direct channel.
+       /// A hop to the payee found in the BOLT 11 payment invoice, though not necessarily a direct
+       /// channel.
        PrivateHop {
                hint: &'a RouteHintHop,
-       }
+       },
+       /// The payee's identity is concealed behind blinded paths provided in a BOLT 12 invoice.
+       Blinded {
+               hint: &'a (BlindedPayInfo, BlindedPath),
+               hint_idx: usize,
+       },
+       /// Similar to [`Self::Blinded`], but the path here has 1 blinded hop. `BlindedPayInfo` provided
+       /// for 1-hop blinded paths is ignored because it is meant to apply to the hops *between* the
+       /// introduction node and the destination. Useful for tracking that we need to include a blinded
+       /// path at the end of our [`Route`].
+       OneHopBlinded {
+               hint: &'a (BlindedPayInfo, BlindedPath),
+               hint_idx: usize,
+       },
 }
 
 impl<'a> CandidateRouteHop<'a> {
-       fn short_channel_id(&self) -> u64 {
+       fn short_channel_id(&self) -> Option<u64> {
                match self {
-                       CandidateRouteHop::FirstHop { details } => details.get_outbound_payment_scid().unwrap(),
-                       CandidateRouteHop::PublicHop { short_channel_id, .. } => *short_channel_id,
-                       CandidateRouteHop::PrivateHop { hint } => hint.short_channel_id,
+                       CandidateRouteHop::FirstHop { details } => Some(details.get_outbound_payment_scid().unwrap()),
+                       CandidateRouteHop::PublicHop { short_channel_id, .. } => Some(*short_channel_id),
+                       CandidateRouteHop::PrivateHop { hint } => Some(hint.short_channel_id),
+                       CandidateRouteHop::Blinded { .. } => None,
+                       CandidateRouteHop::OneHopBlinded { .. } => None,
                }
        }
 
@@ -916,6 +998,8 @@ impl<'a> CandidateRouteHop<'a> {
                        CandidateRouteHop::FirstHop { details } => details.counterparty.features.to_context(),
                        CandidateRouteHop::PublicHop { info, .. } => info.channel().features.clone(),
                        CandidateRouteHop::PrivateHop { .. } => ChannelFeatures::empty(),
+                       CandidateRouteHop::Blinded { .. } => ChannelFeatures::empty(),
+                       CandidateRouteHop::OneHopBlinded { .. } => ChannelFeatures::empty(),
                }
        }
 
@@ -924,14 +1008,18 @@ impl<'a> CandidateRouteHop<'a> {
                        CandidateRouteHop::FirstHop { .. } => 0,
                        CandidateRouteHop::PublicHop { info, .. } => info.direction().cltv_expiry_delta as u32,
                        CandidateRouteHop::PrivateHop { hint } => hint.cltv_expiry_delta as u32,
+                       CandidateRouteHop::Blinded { hint, .. } => hint.0.cltv_expiry_delta as u32,
+                       CandidateRouteHop::OneHopBlinded { .. } => 0,
                }
        }
 
        fn htlc_minimum_msat(&self) -> u64 {
                match self {
-                       CandidateRouteHop::FirstHop { .. } => 0,
+                       CandidateRouteHop::FirstHop { details } => details.next_outbound_htlc_minimum_msat,
                        CandidateRouteHop::PublicHop { info, .. } => info.direction().htlc_minimum_msat,
                        CandidateRouteHop::PrivateHop { hint } => hint.htlc_minimum_msat.unwrap_or(0),
+                       CandidateRouteHop::Blinded { hint, .. } => hint.0.htlc_minimum_msat,
+                       CandidateRouteHop::OneHopBlinded { .. } => 0,
                }
        }
 
@@ -942,6 +1030,14 @@ impl<'a> CandidateRouteHop<'a> {
                        },
                        CandidateRouteHop::PublicHop { info, .. } => info.direction().fees,
                        CandidateRouteHop::PrivateHop { hint } => hint.fees,
+                       CandidateRouteHop::Blinded { hint, .. } => {
+                               RoutingFees {
+                                       base_msat: hint.0.fee_base_msat,
+                                       proportional_millionths: hint.0.fee_proportional_millionths
+                               }
+                       },
+                       CandidateRouteHop::OneHopBlinded { .. } =>
+                               RoutingFees { base_msat: 0, proportional_millionths: 0 },
                }
        }
 
@@ -951,11 +1047,41 @@ impl<'a> CandidateRouteHop<'a> {
                                liquidity_msat: details.next_outbound_htlc_limit_msat,
                        },
                        CandidateRouteHop::PublicHop { info, .. } => info.effective_capacity(),
-                       CandidateRouteHop::PrivateHop { .. } => EffectiveCapacity::Infinite,
+                       CandidateRouteHop::PrivateHop { hint: RouteHintHop { htlc_maximum_msat: Some(max), .. }} =>
+                               EffectiveCapacity::HintMaxHTLC { amount_msat: *max },
+                       CandidateRouteHop::PrivateHop { hint: RouteHintHop { htlc_maximum_msat: None, .. }} =>
+                               EffectiveCapacity::Infinite,
+                       CandidateRouteHop::Blinded { hint, .. } =>
+                               EffectiveCapacity::HintMaxHTLC { amount_msat: hint.0.htlc_maximum_msat },
+                       CandidateRouteHop::OneHopBlinded { .. } => EffectiveCapacity::Infinite,
+               }
+       }
+
+       fn id(&self, channel_direction: bool /* src_node_id < target_node_id */) -> CandidateHopId {
+               match self {
+                       CandidateRouteHop::Blinded { hint_idx, .. } => CandidateHopId::Blinded(*hint_idx),
+                       CandidateRouteHop::OneHopBlinded { hint_idx, .. } => CandidateHopId::Blinded(*hint_idx),
+                       _ => CandidateHopId::Clear((self.short_channel_id().unwrap(), channel_direction)),
+               }
+       }
+       fn blinded_path(&self) -> Option<&'a BlindedPath> {
+               match self {
+                       CandidateRouteHop::Blinded { hint, .. } | CandidateRouteHop::OneHopBlinded { hint, .. } => {
+                               Some(&hint.1)
+                       },
+                       _ => None,
                }
        }
 }
 
+#[derive(Clone, Copy, Eq, Hash, Ord, PartialOrd, PartialEq)]
+enum CandidateHopId {
+       /// Contains (scid, src_node_id < target_node_id)
+       Clear((u64, bool)),
+       /// Index of the blinded route hint in [`Payee::Blinded::route_hints`].
+       Blinded(usize),
+}
+
 #[inline]
 fn max_htlc_from_capacity(capacity: EffectiveCapacity, max_channel_saturation_power_of_half: u8) -> u64 {
        let saturation_shift: u32 = max_channel_saturation_power_of_half as u32;
@@ -963,8 +1089,11 @@ fn max_htlc_from_capacity(capacity: EffectiveCapacity, max_channel_saturation_po
                EffectiveCapacity::ExactLiquidity { liquidity_msat } => liquidity_msat,
                EffectiveCapacity::Infinite => u64::max_value(),
                EffectiveCapacity::Unknown => EffectiveCapacity::Unknown.as_msat(),
-               EffectiveCapacity::MaximumHTLC { amount_msat } =>
+               EffectiveCapacity::AdvertisedMaxHTLC { amount_msat } =>
                        amount_msat.checked_shr(saturation_shift).unwrap_or(0),
+               // Treat htlc_maximum_msat from a route hint as an exact liquidity amount, since the invoice is
+               // expected to have been generated from up-to-date capacity information.
+               EffectiveCapacity::HintMaxHTLC { amount_msat } => amount_msat,
                EffectiveCapacity::Total { capacity_msat, htlc_maximum_msat } =>
                        cmp::min(capacity_msat.checked_shr(saturation_shift).unwrap_or(0), htlc_maximum_msat),
        }
@@ -1015,7 +1144,7 @@ struct PathBuildingHop<'a> {
        /// decrease as well. Thus, we have to explicitly track which nodes have been processed and
        /// avoid processing them again.
        was_processed: bool,
-       #[cfg(all(not(feature = "_bench_unstable"), any(test, fuzzing)))]
+       #[cfg(all(not(ldk_bench), any(test, fuzzing)))]
        // In tests, we apply further sanity checks on cases where we skip nodes we already processed
        // to ensure it is specifically in cases where the fee has gone down because of a decrease in
        // value_contribution_msat, which requires tracking it here. See comments below where it is
@@ -1036,7 +1165,7 @@ impl<'a> core::fmt::Debug for PathBuildingHop<'a> {
                        .field("path_penalty_msat", &self.path_penalty_msat)
                        .field("path_htlc_minimum_msat", &self.path_htlc_minimum_msat)
                        .field("cltv_expiry_delta", &self.candidate.cltv_expiry_delta());
-               #[cfg(all(not(feature = "_bench_unstable"), any(test, fuzzing)))]
+               #[cfg(all(not(ldk_bench), any(test, fuzzing)))]
                let debug_struct = debug_struct
                        .field("value_contribution_msat", &self.value_contribution_msat);
                debug_struct.finish()
@@ -1101,7 +1230,7 @@ impl<'a> PaymentPath<'a> {
                                cur_hop_fees_msat = self.hops.get(i + 1).unwrap().0.hop_use_fee_msat;
                        }
 
-                       let mut cur_hop = &mut self.hops.get_mut(i).unwrap().0;
+                       let cur_hop = &mut self.hops.get_mut(i).unwrap().0;
                        cur_hop.next_hops_fee_msat = total_fee_paid_msat;
                        // Overpay in fees if we can't save these funds due to htlc_minimum_msat.
                        // We try to account for htlc_minimum_msat in scoring (add_entry!), so that nodes don't
@@ -1192,6 +1321,59 @@ impl fmt::Display for LoggedPayeePubkey {
        }
 }
 
+struct LoggedCandidateHop<'a>(&'a CandidateRouteHop<'a>);
+impl<'a> fmt::Display for LoggedCandidateHop<'a> {
+       fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+               match self.0 {
+                       CandidateRouteHop::Blinded { hint, .. } | CandidateRouteHop::OneHopBlinded { hint, .. } => {
+                               "blinded route hint with introduction node id ".fmt(f)?;
+                               hint.1.introduction_node_id.fmt(f)?;
+                               " and blinding point ".fmt(f)?;
+                               hint.1.blinding_point.fmt(f)
+                       },
+                       _ => {
+                               "SCID ".fmt(f)?;
+                               self.0.short_channel_id().unwrap().fmt(f)
+                       },
+               }
+       }
+}
+
+#[inline]
+fn sort_first_hop_channels(
+       channels: &mut Vec<&ChannelDetails>, used_liquidities: &HashMap<CandidateHopId, u64>,
+       recommended_value_msat: u64, our_node_pubkey: &PublicKey
+) {
+       // Sort the first_hops channels to the same node(s) in priority order of which channel we'd
+       // most like to use.
+       //
+       // First, if channels are below `recommended_value_msat`, sort them in descending order,
+       // preferring larger channels to avoid splitting the payment into more MPP parts than is
+       // required.
+       //
+       // Second, because simply always sorting in descending order would always use our largest
+       // available outbound capacity, needlessly fragmenting our available channel capacities,
+       // sort channels above `recommended_value_msat` in ascending order, preferring channels
+       // which have enough, but not too much, capacity for the payment.
+       //
+       // Available outbound balances factor in liquidity already reserved for previously found paths.
+       channels.sort_unstable_by(|chan_a, chan_b| {
+               let chan_a_outbound_limit_msat = chan_a.next_outbound_htlc_limit_msat
+                       .saturating_sub(*used_liquidities.get(&CandidateHopId::Clear((chan_a.get_outbound_payment_scid().unwrap(),
+                       our_node_pubkey < &chan_a.counterparty.node_id))).unwrap_or(&0));
+               let chan_b_outbound_limit_msat = chan_b.next_outbound_htlc_limit_msat
+                       .saturating_sub(*used_liquidities.get(&CandidateHopId::Clear((chan_b.get_outbound_payment_scid().unwrap(),
+                       our_node_pubkey < &chan_b.counterparty.node_id))).unwrap_or(&0));
+               if chan_b_outbound_limit_msat < recommended_value_msat || chan_a_outbound_limit_msat < recommended_value_msat {
+                       // Sort in descending order
+                       chan_b_outbound_limit_msat.cmp(&chan_a_outbound_limit_msat)
+               } else {
+                       // Sort in ascending order
+                       chan_a_outbound_limit_msat.cmp(&chan_b_outbound_limit_msat)
+               }
+       });
+}
+
 /// Finds a route from us (payer) to the given target node (payee).
 ///
 /// If the payee provided features in their invoice, they should be provided via `params.payee`.
@@ -1245,7 +1427,7 @@ where L::Target: Logger {
        // unblinded payee id as an option. We also need a non-optional "payee id" for path construction,
        // so use a dummy id for this in the blinded case.
        let payee_node_id_opt = payment_params.payee.node_id().map(|pk| NodeId::from_pubkey(&pk));
-       const DUMMY_BLINDED_PAYEE_ID: [u8; 33] = [42u8; 33];
+       const DUMMY_BLINDED_PAYEE_ID: [u8; 33] = [2; 33];
        let maybe_dummy_payee_pk = payment_params.payee.node_id().unwrap_or_else(|| PublicKey::from_slice(&DUMMY_BLINDED_PAYEE_ID).unwrap());
        let maybe_dummy_payee_node_id = NodeId::from_pubkey(&maybe_dummy_payee_pk);
        let our_node_id = NodeId::from_pubkey(&our_node_pubkey);
@@ -1272,8 +1454,23 @@ where L::Target: Logger {
                                }
                        }
                },
-               _ => return Err(LightningError{err: "Routing to blinded paths isn't supported yet".to_owned(), action: ErrorAction::IgnoreError}),
-
+               Payee::Blinded { route_hints, .. } => {
+                       if route_hints.iter().all(|(_, path)| &path.introduction_node_id == our_node_pubkey) {
+                               return Err(LightningError{err: "Cannot generate a route to blinded paths if we are the introduction node to all of them".to_owned(), action: ErrorAction::IgnoreError});
+                       }
+                       for (_, blinded_path) in route_hints.iter() {
+                               if blinded_path.blinded_hops.len() == 0 {
+                                       return Err(LightningError{err: "0-hop blinded path provided".to_owned(), action: ErrorAction::IgnoreError});
+                               } else if &blinded_path.introduction_node_id == our_node_pubkey {
+                                       log_info!(logger, "Got blinded path with ourselves as the introduction node, ignoring");
+                               } else if blinded_path.blinded_hops.len() == 1 &&
+                                       route_hints.iter().any( |(_, p)| p.blinded_hops.len() == 1
+                                               && p.introduction_node_id != blinded_path.introduction_node_id)
+                               {
+                                       return Err(LightningError{err: format!("1-hop blinded paths must all have matching introduction node ids"), action: ErrorAction::IgnoreError});
+                               }
+                       }
+               }
        }
        let final_cltv_expiry_delta = payment_params.payee.final_cltv_expiry_delta().unwrap_or(0);
        if payment_params.max_total_cltv_expiry_delta <= final_cltv_expiry_delta {
@@ -1425,11 +1622,12 @@ where L::Target: Logger {
        // drop the requirement by setting this to 0.
        let mut channel_saturation_pow_half = payment_params.max_channel_saturation_power_of_half;
 
-       // Keep track of how much liquidity has been used in selected channels. Used to determine
-       // if the channel can be used by additional MPP paths or to inform path finding decisions. It is
-       // aware of direction *only* to ensure that the correct htlc_maximum_msat value is used. Hence,
-       // liquidity used in one direction will not offset any used in the opposite direction.
-       let mut used_channel_liquidities: HashMap<(u64, bool), u64> =
+       // Keep track of how much liquidity has been used in selected channels or blinded paths. Used to
+       // determine if the channel can be used by additional MPP paths or to inform path finding
+       // decisions. It is aware of direction *only* to ensure that the correct htlc_maximum_msat value
+       // is used. Hence, liquidity used in one direction will not offset any used in the opposite
+       // direction.
+       let mut used_liquidities: HashMap<CandidateHopId, u64> =
                HashMap::with_capacity(network_nodes.len());
 
        // Keeping track of how much value we already collected across other paths. Helps to decide
@@ -1437,26 +1635,8 @@ where L::Target: Logger {
        let mut already_collected_value_msat = 0;
 
        for (_, channels) in first_hop_targets.iter_mut() {
-               // Sort the first_hops channels to the same node(s) in priority order of which channel we'd
-               // most like to use.
-               //
-               // First, if channels are below `recommended_value_msat`, sort them in descending order,
-               // preferring larger channels to avoid splitting the payment into more MPP parts than is
-               // required.
-               //
-               // Second, because simply always sorting in descending order would always use our largest
-               // available outbound capacity, needlessly fragmenting our available channel capacities,
-               // sort channels above `recommended_value_msat` in ascending order, preferring channels
-               // which have enough, but not too much, capacity for the payment.
-               channels.sort_unstable_by(|chan_a, chan_b| {
-                       if chan_b.next_outbound_htlc_limit_msat < recommended_value_msat || chan_a.next_outbound_htlc_limit_msat < recommended_value_msat {
-                               // Sort in descending order
-                               chan_b.next_outbound_htlc_limit_msat.cmp(&chan_a.next_outbound_htlc_limit_msat)
-                       } else {
-                               // Sort in ascending order
-                               chan_a.next_outbound_htlc_limit_msat.cmp(&chan_b.next_outbound_htlc_limit_msat)
-                       }
-               });
+               sort_first_hop_channels(channels, &used_liquidities, recommended_value_msat,
+                       our_node_pubkey);
        }
 
        log_trace!(logger, "Building path from {} to payer {} for value {} msat.",
@@ -1470,14 +1650,15 @@ where L::Target: Logger {
                ( $candidate: expr, $src_node_id: expr, $dest_node_id: expr, $next_hops_fee_msat: expr,
                        $next_hops_value_contribution: expr, $next_hops_path_htlc_minimum_msat: expr,
                        $next_hops_path_penalty_msat: expr, $next_hops_cltv_delta: expr, $next_hops_path_length: expr ) => { {
-                       // We "return" whether we updated the path at the end, via this:
-                       let mut did_add_update_path_to_src_node = false;
+                       // We "return" whether we updated the path at the end, and how much we can route via
+                       // this channel, via this:
+                       let mut did_add_update_path_to_src_node = None;
                        // Channels to self should not be used. This is more of belt-and-suspenders, because in
                        // practice these cases should be caught earlier:
                        // - for regular channels at channel announcement (TODO)
                        // - for first and last hops early in get_route
                        if $src_node_id != $dest_node_id {
-                               let short_channel_id = $candidate.short_channel_id();
+                               let scid_opt = $candidate.short_channel_id();
                                let effective_capacity = $candidate.effective_capacity();
                                let htlc_maximum_msat = max_htlc_from_capacity(effective_capacity, channel_saturation_pow_half);
 
@@ -1489,8 +1670,8 @@ where L::Target: Logger {
                                // if the amount being transferred over this path is lower.
                                // We do this for now, but this is a subject for removal.
                                if let Some(mut available_value_contribution_msat) = htlc_maximum_msat.checked_sub($next_hops_fee_msat) {
-                                       let used_liquidity_msat = used_channel_liquidities
-                                               .get(&(short_channel_id, $src_node_id < $dest_node_id))
+                                       let used_liquidity_msat = used_liquidities
+                                               .get(&$candidate.id($src_node_id < $dest_node_id))
                                                .map_or(0, |used_liquidity_msat| {
                                                        available_value_contribution_msat = available_value_contribution_msat
                                                                .saturating_sub(*used_liquidity_msat);
@@ -1532,8 +1713,8 @@ where L::Target: Logger {
                                                 (amount_to_transfer_over_msat < $next_hops_path_htlc_minimum_msat &&
                                                  recommended_value_msat > $next_hops_path_htlc_minimum_msat));
 
-                                       let payment_failed_on_this_channel =
-                                               payment_params.previously_failed_channels.contains(&short_channel_id);
+                                       let payment_failed_on_this_channel = scid_opt.map_or(false,
+                                               |scid| payment_params.previously_failed_channels.contains(&scid));
 
                                        // If HTLC minimum is larger than the amount we're going to transfer, we shouldn't
                                        // bother considering this channel. If retrying with recommended_value_msat may
@@ -1570,14 +1751,14 @@ where L::Target: Logger {
                                                                path_htlc_minimum_msat,
                                                                path_penalty_msat: u64::max_value(),
                                                                was_processed: false,
-                                                               #[cfg(all(not(feature = "_bench_unstable"), any(test, fuzzing)))]
+                                                               #[cfg(all(not(ldk_bench), any(test, fuzzing)))]
                                                                value_contribution_msat,
                                                        }
                                                });
 
                                                #[allow(unused_mut)] // We only use the mut in cfg(test)
                                                let mut should_process = !old_entry.was_processed;
-                                               #[cfg(all(not(feature = "_bench_unstable"), any(test, fuzzing)))]
+                                               #[cfg(all(not(ldk_bench), any(test, fuzzing)))]
                                                {
                                                        // In test/fuzzing builds, we do extra checks to make sure the skipping
                                                        // of already-seen nodes only happens in cases we expect (see below).
@@ -1602,9 +1783,9 @@ where L::Target: Logger {
                                                                inflight_htlc_msat: used_liquidity_msat,
                                                                effective_capacity,
                                                        };
-                                                       let channel_penalty_msat = scorer.channel_penalty_msat(
-                                                               short_channel_id, &$src_node_id, &$dest_node_id, channel_usage, score_params
-                                                       );
+                                                       let channel_penalty_msat = scid_opt.map_or(0,
+                                                               |scid| scorer.channel_penalty_msat(scid, &$src_node_id, &$dest_node_id,
+                                                                       channel_usage, score_params));
                                                        let path_penalty_msat = $next_hops_path_penalty_msat
                                                                .saturating_add(channel_penalty_msat);
                                                        let new_graph_node = RouteGraphNode {
@@ -1648,13 +1829,13 @@ where L::Target: Logger {
                                                                old_entry.fee_msat = 0; // This value will be later filled with hop_use_fee_msat of the following channel
                                                                old_entry.path_htlc_minimum_msat = path_htlc_minimum_msat;
                                                                old_entry.path_penalty_msat = path_penalty_msat;
-                                                               #[cfg(all(not(feature = "_bench_unstable"), any(test, fuzzing)))]
+                                                               #[cfg(all(not(ldk_bench), any(test, fuzzing)))]
                                                                {
                                                                        old_entry.value_contribution_msat = value_contribution_msat;
                                                                }
-                                                               did_add_update_path_to_src_node = true;
+                                                               did_add_update_path_to_src_node = Some(value_contribution_msat);
                                                        } else if old_entry.was_processed && new_cost < old_cost {
-                                                               #[cfg(all(not(feature = "_bench_unstable"), any(test, fuzzing)))]
+                                                               #[cfg(all(not(ldk_bench), any(test, fuzzing)))]
                                                                {
                                                                        // If we're skipping processing a node which was previously
                                                                        // processed even though we found another path to it with a
@@ -1761,7 +1942,7 @@ where L::Target: Logger {
 
        // TODO: diversify by nodes (so that all paths aren't doomed if one node is offline).
        'paths_collection: loop {
-               // For every new path, start from scratch, except for used_channel_liquidities, which
+               // For every new path, start from scratch, except for used_liquidities, which
                // helps to avoid reusing previously selected paths in future iterations.
                targets.clear();
                dist.clear();
@@ -1773,9 +1954,9 @@ where L::Target: Logger {
                        for details in first_channels {
                                let candidate = CandidateRouteHop::FirstHop { details };
                                let added = add_entry!(candidate, our_node_id, payee, 0, path_value_msat,
-                                                                       0, 0u64, 0, 0);
-                               log_trace!(logger, "{} direct route to payee via SCID {}",
-                                               if added { "Added" } else { "Skipped" }, candidate.short_channel_id());
+                                                                       0, 0u64, 0, 0).is_some();
+                               log_trace!(logger, "{} direct route to payee via {}",
+                                               if added { "Added" } else { "Skipped" }, LoggedCandidateHop(&candidate));
                        }
                }));
 
@@ -1796,11 +1977,37 @@ where L::Target: Logger {
                // If a caller provided us with last hops, add them to routing targets. Since this happens
                // earlier than general path finding, they will be somewhat prioritized, although currently
                // it matters only if the fees are exactly the same.
-               let route_hints = match &payment_params.payee {
-                       Payee::Clear { route_hints, .. } => route_hints,
-                       _ => return Err(LightningError{err: "Routing to blinded paths isn't supported yet".to_owned(), action: ErrorAction::IgnoreError}),
-               };
-               for route in route_hints.iter().filter(|route| !route.0.is_empty()) {
+               for (hint_idx, hint) in payment_params.payee.blinded_route_hints().iter().enumerate() {
+                       let intro_node_id = NodeId::from_pubkey(&hint.1.introduction_node_id);
+                       let have_intro_node_in_graph =
+                               // Only add the hops in this route to our candidate set if either
+                               // we have a direct channel to the first hop or the first hop is
+                               // in the regular network graph.
+                               first_hop_targets.get(&intro_node_id).is_some() ||
+                               network_nodes.get(&intro_node_id).is_some();
+                       if !have_intro_node_in_graph { continue }
+                       let candidate = if hint.1.blinded_hops.len() == 1 {
+                               CandidateRouteHop::OneHopBlinded { hint, hint_idx }
+                       } else { CandidateRouteHop::Blinded { hint, hint_idx } };
+                       let mut path_contribution_msat = path_value_msat;
+                       if let Some(hop_used_msat) = add_entry!(candidate, intro_node_id, maybe_dummy_payee_node_id,
+                               0, path_contribution_msat, 0, 0_u64, 0, 0)
+                       {
+                               path_contribution_msat = hop_used_msat;
+                       } else { continue }
+                       if let Some(first_channels) = first_hop_targets.get_mut(&NodeId::from_pubkey(&hint.1.introduction_node_id)) {
+                               sort_first_hop_channels(first_channels, &used_liquidities, recommended_value_msat,
+                                       our_node_pubkey);
+                               for details in first_channels {
+                                       let first_hop_candidate = CandidateRouteHop::FirstHop { details };
+                                       add_entry!(first_hop_candidate, our_node_id, intro_node_id, 0, path_contribution_msat, 0,
+                                               0_u64, 0, 0);
+                               }
+                       }
+               }
+               for route in payment_params.payee.unblinded_route_hints().iter()
+                       .filter(|route| !route.0.is_empty())
+               {
                        let first_hop_in_route = &(route.0)[0];
                        let have_hop_src_in_graph =
                                // Only add the hops in this route to our candidate set if either
@@ -1820,6 +2027,7 @@ where L::Target: Logger {
                                let mut aggregate_next_hops_path_penalty_msat: u64 = 0;
                                let mut aggregate_next_hops_cltv_delta: u32 = 0;
                                let mut aggregate_next_hops_path_length: u8 = 0;
+                               let mut aggregate_path_contribution_msat = path_value_msat;
 
                                for (idx, (hop, prev_hop_id)) in hop_iter.zip(prev_hop_iter).enumerate() {
                                        let source = NodeId::from_pubkey(&hop.src_node_id);
@@ -1833,18 +2041,22 @@ where L::Target: Logger {
                                                })
                                                .unwrap_or_else(|| CandidateRouteHop::PrivateHop { hint: hop });
 
-                                       if !add_entry!(candidate, source, target, aggregate_next_hops_fee_msat,
-                                                               path_value_msat, aggregate_next_hops_path_htlc_minimum_msat,
-                                                               aggregate_next_hops_path_penalty_msat,
-                                                               aggregate_next_hops_cltv_delta, aggregate_next_hops_path_length) {
+                                       if let Some(hop_used_msat) = add_entry!(candidate, source, target,
+                                               aggregate_next_hops_fee_msat, aggregate_path_contribution_msat,
+                                               aggregate_next_hops_path_htlc_minimum_msat, aggregate_next_hops_path_penalty_msat,
+                                               aggregate_next_hops_cltv_delta, aggregate_next_hops_path_length)
+                                       {
+                                               aggregate_path_contribution_msat = hop_used_msat;
+                                       } else {
                                                // If this hop was not used then there is no use checking the preceding
                                                // hops in the RouteHint. We can break by just searching for a direct
                                                // channel between last checked hop and first_hop_targets.
                                                hop_used = false;
                                        }
 
-                                       let used_liquidity_msat = used_channel_liquidities
-                                               .get(&(hop.short_channel_id, source < target)).copied().unwrap_or(0);
+                                       let used_liquidity_msat = used_liquidities
+                                               .get(&candidate.id(source < target)).copied()
+                                               .unwrap_or(0);
                                        let channel_usage = ChannelUsage {
                                                amount_msat: final_value_msat + aggregate_next_hops_fee_msat,
                                                inflight_htlc_msat: used_liquidity_msat,
@@ -1863,14 +2075,15 @@ where L::Target: Logger {
                                                .saturating_add(1);
 
                                        // Searching for a direct channel between last checked hop and first_hop_targets
-                                       if let Some(first_channels) = first_hop_targets.get(&NodeId::from_pubkey(&prev_hop_id)) {
+                                       if let Some(first_channels) = first_hop_targets.get_mut(&NodeId::from_pubkey(&prev_hop_id)) {
+                                               sort_first_hop_channels(first_channels, &used_liquidities,
+                                                       recommended_value_msat, our_node_pubkey);
                                                for details in first_channels {
-                                                       let candidate = CandidateRouteHop::FirstHop { details };
-                                                       add_entry!(candidate, our_node_id, NodeId::from_pubkey(&prev_hop_id),
-                                                               aggregate_next_hops_fee_msat, path_value_msat,
-                                                               aggregate_next_hops_path_htlc_minimum_msat,
-                                                               aggregate_next_hops_path_penalty_msat, aggregate_next_hops_cltv_delta,
-                                                               aggregate_next_hops_path_length);
+                                                       let first_hop_candidate = CandidateRouteHop::FirstHop { details };
+                                                       add_entry!(first_hop_candidate, our_node_id, NodeId::from_pubkey(&prev_hop_id),
+                                                               aggregate_next_hops_fee_msat, aggregate_path_contribution_msat,
+                                                               aggregate_next_hops_path_htlc_minimum_msat, aggregate_next_hops_path_penalty_msat,
+                                                               aggregate_next_hops_cltv_delta, aggregate_next_hops_path_length);
                                                }
                                        }
 
@@ -1903,12 +2116,15 @@ where L::Target: Logger {
                                                // Note that we *must* check if the last hop was added as `add_entry`
                                                // always assumes that the third argument is a node to which we have a
                                                // path.
-                                               if let Some(first_channels) = first_hop_targets.get(&NodeId::from_pubkey(&hop.src_node_id)) {
+                                               if let Some(first_channels) = first_hop_targets.get_mut(&NodeId::from_pubkey(&hop.src_node_id)) {
+                                                       sort_first_hop_channels(first_channels, &used_liquidities,
+                                                               recommended_value_msat, our_node_pubkey);
                                                        for details in first_channels {
-                                                               let candidate = CandidateRouteHop::FirstHop { details };
-                                                               add_entry!(candidate, our_node_id,
+                                                               let first_hop_candidate = CandidateRouteHop::FirstHop { details };
+                                                               add_entry!(first_hop_candidate, our_node_id,
                                                                        NodeId::from_pubkey(&hop.src_node_id),
-                                                                       aggregate_next_hops_fee_msat, path_value_msat,
+                                                                       aggregate_next_hops_fee_msat,
+                                                                       aggregate_path_contribution_msat,
                                                                        aggregate_next_hops_path_htlc_minimum_msat,
                                                                        aggregate_next_hops_path_penalty_msat,
                                                                        aggregate_next_hops_cltv_delta,
@@ -1947,10 +2163,12 @@ where L::Target: Logger {
                                        let mut features_set = false;
                                        if let Some(first_channels) = first_hop_targets.get(&ordered_hops.last().unwrap().0.node_id) {
                                                for details in first_channels {
-                                                       if details.get_outbound_payment_scid().unwrap() == ordered_hops.last().unwrap().0.candidate.short_channel_id() {
-                                                               ordered_hops.last_mut().unwrap().1 = details.counterparty.features.to_context();
-                                                               features_set = true;
-                                                               break;
+                                                       if let Some(scid) = ordered_hops.last().unwrap().0.candidate.short_channel_id() {
+                                                               if details.get_outbound_payment_scid().unwrap() == scid {
+                                                                       ordered_hops.last_mut().unwrap().1 = details.counterparty.features.to_context();
+                                                                       features_set = true;
+                                                                       break;
+                                                               }
                                                        }
                                                }
                                        }
@@ -2019,8 +2237,8 @@ where L::Target: Logger {
                                        .chain(payment_path.hops.iter().map(|(hop, _)| &hop.node_id));
                                for (prev_hop, (hop, _)) in prev_hop_iter.zip(payment_path.hops.iter()) {
                                        let spent_on_hop_msat = value_contribution_msat + hop.next_hops_fee_msat;
-                                       let used_liquidity_msat = used_channel_liquidities
-                                               .entry((hop.candidate.short_channel_id(), *prev_hop < hop.node_id))
+                                       let used_liquidity_msat = used_liquidities
+                                               .entry(hop.candidate.id(*prev_hop < hop.node_id))
                                                .and_modify(|used_liquidity_msat| *used_liquidity_msat += spent_on_hop_msat)
                                                .or_insert(spent_on_hop_msat);
                                        let hop_capacity = hop.candidate.effective_capacity();
@@ -2036,11 +2254,12 @@ where L::Target: Logger {
                                        // If we weren't capped by hitting a liquidity limit on a channel in the path,
                                        // we'll probably end up picking the same path again on the next iteration.
                                        // Decrease the available liquidity of a hop in the middle of the path.
-                                       let victim_scid = payment_path.hops[(payment_path.hops.len()) / 2].0.candidate.short_channel_id();
+                                       let victim_candidate = &payment_path.hops[(payment_path.hops.len()) / 2].0.candidate;
                                        let exhausted = u64::max_value();
-                                       log_trace!(logger, "Disabling channel {} for future path building iterations to avoid duplicates.", victim_scid);
-                                       *used_channel_liquidities.entry((victim_scid, false)).or_default() = exhausted;
-                                       *used_channel_liquidities.entry((victim_scid, true)).or_default() = exhausted;
+                                       log_trace!(logger, "Disabling route candidate {} for future path building iterations to
+                                               avoid duplicates.", LoggedCandidateHop(victim_candidate));
+                                       *used_liquidities.entry(victim_candidate.id(false)).or_default() = exhausted;
+                                       *used_liquidities.entry(victim_candidate.id(true)).or_default() = exhausted;
                                }
 
                                // Track the total amount all our collected paths allow to send so that we know
@@ -2168,63 +2387,68 @@ where L::Target: Logger {
        // compare both SCIDs and NodeIds as individual nodes may use random aliases causing collisions
        // across nodes.
        selected_route.sort_unstable_by_key(|path| {
-               let mut key = [0u64; MAX_PATH_LENGTH_ESTIMATE as usize];
+               let mut key = [CandidateHopId::Clear((42, true)) ; MAX_PATH_LENGTH_ESTIMATE as usize];
                debug_assert!(path.hops.len() <= key.len());
-               for (scid, key) in path.hops.iter().map(|h| h.0.candidate.short_channel_id()).zip(key.iter_mut()) {
+               for (scid, key) in path.hops.iter() .map(|h| h.0.candidate.id(true)).zip(key.iter_mut()) {
                        *key = scid;
                }
                key
        });
        for idx in 0..(selected_route.len() - 1) {
                if idx + 1 >= selected_route.len() { break; }
-               if iter_equal(selected_route[idx    ].hops.iter().map(|h| (h.0.candidate.short_channel_id(), h.0.node_id)),
-                             selected_route[idx + 1].hops.iter().map(|h| (h.0.candidate.short_channel_id(), h.0.node_id))) {
+               if iter_equal(selected_route[idx    ].hops.iter().map(|h| (h.0.candidate.id(true), h.0.node_id)),
+                             selected_route[idx + 1].hops.iter().map(|h| (h.0.candidate.id(true), h.0.node_id))) {
                        let new_value = selected_route[idx].get_value_msat() + selected_route[idx + 1].get_value_msat();
                        selected_route[idx].update_value_and_recompute_fees(new_value);
                        selected_route.remove(idx + 1);
                }
        }
 
-       let mut selected_paths = Vec::<Vec<Result<RouteHop, LightningError>>>::new();
+       let mut paths = Vec::new();
        for payment_path in selected_route {
-               let mut path = payment_path.hops.iter().map(|(payment_hop, node_features)| {
-                       Ok(RouteHop {
-                               pubkey: PublicKey::from_slice(payment_hop.node_id.as_slice()).map_err(|_| LightningError{err: format!("Public key {:?} is invalid", &payment_hop.node_id), action: ErrorAction::IgnoreAndLog(Level::Trace)})?,
+               let mut hops = Vec::with_capacity(payment_path.hops.len());
+               for (hop, node_features) in payment_path.hops.iter()
+                       .filter(|(h, _)| h.candidate.short_channel_id().is_some())
+               {
+                       hops.push(RouteHop {
+                               pubkey: PublicKey::from_slice(hop.node_id.as_slice()).map_err(|_| LightningError{err: format!("Public key {:?} is invalid", &hop.node_id), action: ErrorAction::IgnoreAndLog(Level::Trace)})?,
                                node_features: node_features.clone(),
-                               short_channel_id: payment_hop.candidate.short_channel_id(),
-                               channel_features: payment_hop.candidate.features(),
-                               fee_msat: payment_hop.fee_msat,
-                               cltv_expiry_delta: payment_hop.candidate.cltv_expiry_delta(),
-                       })
-               }).collect::<Vec<_>>();
+                               short_channel_id: hop.candidate.short_channel_id().unwrap(),
+                               channel_features: hop.candidate.features(),
+                               fee_msat: hop.fee_msat,
+                               cltv_expiry_delta: hop.candidate.cltv_expiry_delta(),
+                       });
+               }
+               let mut final_cltv_delta = final_cltv_expiry_delta;
+               let blinded_tail = payment_path.hops.last().and_then(|(h, _)| {
+                       if let Some(blinded_path) = h.candidate.blinded_path() {
+                               final_cltv_delta = h.candidate.cltv_expiry_delta();
+                               Some(BlindedTail {
+                                       hops: blinded_path.blinded_hops.clone(),
+                                       blinding_point: blinded_path.blinding_point,
+                                       excess_final_cltv_expiry_delta: 0,
+                                       final_value_msat: h.fee_msat,
+                               })
+                       } else { None }
+               });
                // Propagate the cltv_expiry_delta one hop backwards since the delta from the current hop is
                // applicable for the previous hop.
-               path.iter_mut().rev().fold(final_cltv_expiry_delta, |prev_cltv_expiry_delta, hop| {
-                       core::mem::replace(&mut hop.as_mut().unwrap().cltv_expiry_delta, prev_cltv_expiry_delta)
+               hops.iter_mut().rev().fold(final_cltv_delta, |prev_cltv_expiry_delta, hop| {
+                       core::mem::replace(&mut hop.cltv_expiry_delta, prev_cltv_expiry_delta)
                });
-               selected_paths.push(path);
+
+               paths.push(Path { hops, blinded_tail });
        }
        // Make sure we would never create a route with more paths than we allow.
-       debug_assert!(selected_paths.len() <= payment_params.max_path_count.into());
+       debug_assert!(paths.len() <= payment_params.max_path_count.into());
 
        if let Some(node_features) = payment_params.payee.node_features() {
-               for path in selected_paths.iter_mut() {
-                       if let Ok(route_hop) = path.last_mut().unwrap() {
-                               route_hop.node_features = node_features.clone();
-                       }
+               for path in paths.iter_mut() {
+                       path.hops.last_mut().unwrap().node_features = node_features.clone();
                }
        }
 
-       let mut paths: Vec<Path> = Vec::new();
-       for results_vec in selected_paths {
-               let mut hops = Vec::with_capacity(results_vec.len());
-               for res in results_vec { hops.push(res?); }
-               paths.push(Path { hops, blinded_tail: None });
-       }
-       let route = Route {
-               paths,
-               payment_params: Some(payment_params.clone()),
-       };
+       let route = Route { paths, payment_params: Some(payment_params.clone()) };
        log_info!(logger, "Got route: {}", log_route!(route));
        Ok(route)
 }
@@ -2410,9 +2634,10 @@ mod tests {
        use crate::routing::test_utils::{add_channel, add_or_update_node, build_graph, build_line_graph, id_to_feature_flags, get_nodes, update_channel};
        use crate::chain::transaction::OutPoint;
        use crate::sign::EntropySource;
-       use crate::ln::features::{ChannelFeatures, InitFeatures, NodeFeatures};
+       use crate::ln::features::{BlindedHopFeatures, Bolt12InvoiceFeatures, ChannelFeatures, InitFeatures, NodeFeatures};
        use crate::ln::msgs::{ErrorAction, LightningError, UnsignedChannelUpdate, MAX_VALUE_MSAT};
        use crate::ln::channelmanager;
+       use crate::offers::invoice::BlindedPayInfo;
        use crate::util::config::UserConfig;
        use crate::util::test_utils as ln_test_utils;
        use crate::util::chacha20::ChaCha20;
@@ -2460,6 +2685,7 @@ mod tests {
                        balance_msat: 0,
                        outbound_capacity_msat,
                        next_outbound_htlc_limit_msat: outbound_capacity_msat,
+                       next_outbound_htlc_minimum_msat: 0,
                        inbound_capacity_msat: 42,
                        unspendable_punishment_reserve: None,
                        confirmations_required: None,
@@ -2470,7 +2696,8 @@ mod tests {
                        inbound_htlc_minimum_msat: None,
                        inbound_htlc_maximum_msat: None,
                        config: None,
-                       feerate_sat_per_1000_weight: None
+                       feerate_sat_per_1000_weight: None,
+                       channel_shutdown_state: Some(channelmanager::ChannelShutdownState::NotShuttingDown),
                }
        }
 
@@ -4078,14 +4305,66 @@ mod tests {
 
        #[test]
        fn simple_mpp_route_test() {
+               let (secp_ctx, _, _, _, _) = build_graph();
+               let (_, _, _, nodes) = get_nodes(&secp_ctx);
+               let config = UserConfig::default();
+               let clear_payment_params = PaymentParameters::from_node_id(nodes[2], 42)
+                       .with_bolt11_features(channelmanager::provided_invoice_features(&config)).unwrap();
+               do_simple_mpp_route_test(clear_payment_params);
+
+               // MPP to a 1-hop blinded path for nodes[2]
+               let bolt12_features: Bolt12InvoiceFeatures = channelmanager::provided_invoice_features(&config).to_context();
+               let blinded_path = BlindedPath {
+                       introduction_node_id: nodes[2],
+                       blinding_point: ln_test_utils::pubkey(42),
+                       blinded_hops: vec![BlindedHop { blinded_node_id: ln_test_utils::pubkey(42 as u8), encrypted_payload: Vec::new() }],
+               };
+               let blinded_payinfo = BlindedPayInfo { // These fields are ignored for 1-hop blinded paths
+                       fee_base_msat: 0,
+                       fee_proportional_millionths: 0,
+                       htlc_minimum_msat: 0,
+                       htlc_maximum_msat: 0,
+                       cltv_expiry_delta: 0,
+                       features: BlindedHopFeatures::empty(),
+               };
+               let one_hop_blinded_payment_params = PaymentParameters::blinded(vec![(blinded_payinfo.clone(), blinded_path.clone())])
+                       .with_bolt12_features(bolt12_features.clone()).unwrap();
+               do_simple_mpp_route_test(one_hop_blinded_payment_params.clone());
+
+               // MPP to 3 2-hop blinded paths
+               let mut blinded_path_node_0 = blinded_path.clone();
+               blinded_path_node_0.introduction_node_id = nodes[0];
+               blinded_path_node_0.blinded_hops.push(blinded_path.blinded_hops[0].clone());
+               let mut node_0_payinfo = blinded_payinfo.clone();
+               node_0_payinfo.htlc_maximum_msat = 50_000;
+
+               let mut blinded_path_node_7 = blinded_path_node_0.clone();
+               blinded_path_node_7.introduction_node_id = nodes[7];
+               let mut node_7_payinfo = blinded_payinfo.clone();
+               node_7_payinfo.htlc_maximum_msat = 60_000;
+
+               let mut blinded_path_node_1 = blinded_path_node_0.clone();
+               blinded_path_node_1.introduction_node_id = nodes[1];
+               let mut node_1_payinfo = blinded_payinfo.clone();
+               node_1_payinfo.htlc_maximum_msat = 180_000;
+
+               let two_hop_blinded_payment_params = PaymentParameters::blinded(
+                       vec![
+                               (node_0_payinfo, blinded_path_node_0),
+                               (node_7_payinfo, blinded_path_node_7),
+                               (node_1_payinfo, blinded_path_node_1)
+                       ])
+                       .with_bolt12_features(bolt12_features).unwrap();
+               do_simple_mpp_route_test(two_hop_blinded_payment_params);
+       }
+
+
+       fn do_simple_mpp_route_test(payment_params: PaymentParameters) {
                let (secp_ctx, network_graph, gossip_sync, _, logger) = build_graph();
                let (our_privkey, our_id, privkeys, nodes) = get_nodes(&secp_ctx);
                let scorer = ln_test_utils::TestScorer::new();
                let keys_manager = ln_test_utils::TestKeysInterface::new(&[0u8; 32], Network::Testnet);
                let random_seed_bytes = keys_manager.get_secure_random_bytes();
-               let config = UserConfig::default();
-               let payment_params = PaymentParameters::from_node_id(nodes[2], 42)
-                       .with_bolt11_features(channelmanager::provided_invoice_features(&config)).unwrap();
 
                // We need a route consisting of 3 paths:
                // From our node to node2 via node0, node7, node1 (three paths one hop each).
@@ -4214,8 +4493,12 @@ mod tests {
                        assert_eq!(route.paths.len(), 3);
                        let mut total_amount_paid_msat = 0;
                        for path in &route.paths {
-                               assert_eq!(path.hops.len(), 2);
-                               assert_eq!(path.hops.last().unwrap().pubkey, nodes[2]);
+                               if let Some(bt) = &path.blinded_tail {
+                                       assert_eq!(path.hops.len() + if bt.hops.len() == 1 { 0 } else { 1 }, 2);
+                               } else {
+                                       assert_eq!(path.hops.len(), 2);
+                                       assert_eq!(path.hops.last().unwrap().pubkey, nodes[2]);
+                               }
                                total_amount_paid_msat += path.final_value_msat();
                        }
                        assert_eq!(total_amount_paid_msat, 250_000);
@@ -4228,8 +4511,22 @@ mod tests {
                        assert_eq!(route.paths.len(), 3);
                        let mut total_amount_paid_msat = 0;
                        for path in &route.paths {
-                               assert_eq!(path.hops.len(), 2);
-                               assert_eq!(path.hops.last().unwrap().pubkey, nodes[2]);
+                               if payment_params.payee.blinded_route_hints().len() != 0 {
+                                       assert!(path.blinded_tail.is_some()) } else { assert!(path.blinded_tail.is_none()) }
+                               if let Some(bt) = &path.blinded_tail {
+                                       assert_eq!(path.hops.len() + if bt.hops.len() == 1 { 0 } else { 1 }, 2);
+                                       if bt.hops.len() > 1 {
+                                               assert_eq!(path.hops.last().unwrap().pubkey,
+                                                       payment_params.payee.blinded_route_hints().iter()
+                                                               .find(|(p, _)| p.htlc_maximum_msat == path.final_value_msat())
+                                                               .map(|(_, p)| p.introduction_node_id).unwrap());
+                                       } else {
+                                               assert_eq!(path.hops.last().unwrap().pubkey, nodes[2]);
+                                       }
+                               } else {
+                                       assert_eq!(path.hops.len(), 2);
+                                       assert_eq!(path.hops.last().unwrap().pubkey, nodes[2]);
+                               }
                                total_amount_paid_msat += path.final_value_msat();
                        }
                        assert_eq!(total_amount_paid_msat, 290_000);
@@ -5791,44 +6088,26 @@ mod tests {
                println!("Using seed of {}", seed);
                seed
        }
-       #[cfg(not(feature = "no-std"))]
-       use crate::util::ser::ReadableArgs;
 
        #[test]
        #[cfg(not(feature = "no-std"))]
        fn generate_routes() {
                use crate::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringFeeParameters};
 
-               let mut d = match super::bench_utils::get_route_file() {
+               let logger = ln_test_utils::TestLogger::new();
+               let graph = match super::bench_utils::read_network_graph(&logger) {
                        Ok(f) => f,
                        Err(e) => {
                                eprintln!("{}", e);
                                return;
                        },
                };
-               let logger = ln_test_utils::TestLogger::new();
-               let graph = NetworkGraph::read(&mut d, &logger).unwrap();
-               let keys_manager = ln_test_utils::TestKeysInterface::new(&[0u8; 32], Network::Testnet);
-               let random_seed_bytes = keys_manager.get_secure_random_bytes();
 
-               // First, get 100 (source, destination) pairs for which route-getting actually succeeds...
-               let mut seed = random_init_seed() as usize;
-               let nodes = graph.read_only().nodes().clone();
-               'load_endpoints: for _ in 0..10 {
-                       loop {
-                               seed = seed.overflowing_mul(0xdeadbeef).0;
-                               let src = &PublicKey::from_slice(nodes.unordered_keys().skip(seed % nodes.len()).next().unwrap().as_slice()).unwrap();
-                               seed = seed.overflowing_mul(0xdeadbeef).0;
-                               let dst = PublicKey::from_slice(nodes.unordered_keys().skip(seed % nodes.len()).next().unwrap().as_slice()).unwrap();
-                               let payment_params = PaymentParameters::from_node_id(dst, 42);
-                               let amt = seed as u64 % 200_000_000;
-                               let params = ProbabilisticScoringFeeParameters::default();
-                               let scorer = ProbabilisticScorer::new(ProbabilisticScoringDecayParameters::default(), &graph, &logger);
-                               if get_route(src, &payment_params, &graph.read_only(), None, amt, &logger, &scorer, &params, &random_seed_bytes).is_ok() {
-                                       continue 'load_endpoints;
-                               }
-                       }
-               }
+               let params = ProbabilisticScoringFeeParameters::default();
+               let mut scorer = ProbabilisticScorer::new(ProbabilisticScoringDecayParameters::default(), &graph, &logger);
+               let features = super::InvoiceFeatures::empty();
+
+               super::bench_utils::generate_test_routes(&graph, &mut scorer, &params, features, random_init_seed(), 0, 2);
        }
 
        #[test]
@@ -5836,37 +6115,41 @@ mod tests {
        fn generate_routes_mpp() {
                use crate::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringFeeParameters};
 
-               let mut d = match super::bench_utils::get_route_file() {
+               let logger = ln_test_utils::TestLogger::new();
+               let graph = match super::bench_utils::read_network_graph(&logger) {
                        Ok(f) => f,
                        Err(e) => {
                                eprintln!("{}", e);
                                return;
                        },
                };
+
+               let params = ProbabilisticScoringFeeParameters::default();
+               let mut scorer = ProbabilisticScorer::new(ProbabilisticScoringDecayParameters::default(), &graph, &logger);
+               let features = channelmanager::provided_invoice_features(&UserConfig::default());
+
+               super::bench_utils::generate_test_routes(&graph, &mut scorer, &params, features, random_init_seed(), 0, 2);
+       }
+
+       #[test]
+       #[cfg(not(feature = "no-std"))]
+       fn generate_large_mpp_routes() {
+               use crate::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringFeeParameters};
+
                let logger = ln_test_utils::TestLogger::new();
-               let graph = NetworkGraph::read(&mut d, &logger).unwrap();
-               let keys_manager = ln_test_utils::TestKeysInterface::new(&[0u8; 32], Network::Testnet);
-               let random_seed_bytes = keys_manager.get_secure_random_bytes();
-               let config = UserConfig::default();
+               let graph = match super::bench_utils::read_network_graph(&logger) {
+                       Ok(f) => f,
+                       Err(e) => {
+                               eprintln!("{}", e);
+                               return;
+                       },
+               };
 
-               // First, get 100 (source, destination) pairs for which route-getting actually succeeds...
-               let mut seed = random_init_seed() as usize;
-               let nodes = graph.read_only().nodes().clone();
-               'load_endpoints: for _ in 0..10 {
-                       loop {
-                               seed = seed.overflowing_mul(0xdeadbeef).0;
-                               let src = &PublicKey::from_slice(nodes.unordered_keys().skip(seed % nodes.len()).next().unwrap().as_slice()).unwrap();
-                               seed = seed.overflowing_mul(0xdeadbeef).0;
-                               let dst = PublicKey::from_slice(nodes.unordered_keys().skip(seed % nodes.len()).next().unwrap().as_slice()).unwrap();
-                               let payment_params = PaymentParameters::from_node_id(dst, 42).with_bolt11_features(channelmanager::provided_invoice_features(&config)).unwrap();
-                               let amt = seed as u64 % 200_000_000;
-                               let params = ProbabilisticScoringFeeParameters::default();
-                               let scorer = ProbabilisticScorer::new(ProbabilisticScoringDecayParameters::default(), &graph, &logger);
-                               if get_route(src, &payment_params, &graph.read_only(), None, amt, &logger, &scorer, &params, &random_seed_bytes).is_ok() {
-                                       continue 'load_endpoints;
-                               }
-                       }
-               }
+               let params = ProbabilisticScoringFeeParameters::default();
+               let mut scorer = ProbabilisticScorer::new(ProbabilisticScoringDecayParameters::default(), &graph, &logger);
+               let features = channelmanager::provided_invoice_features(&UserConfig::default());
+
+               super::bench_utils::generate_test_routes(&graph, &mut scorer, &params, features, random_init_seed(), 1_000_000, 2);
        }
 
        #[test]
@@ -5906,6 +6189,157 @@ mod tests {
                assert!(route.is_ok());
        }
 
+       #[test]
+       fn abide_by_route_hint_max_htlc() {
+               // Check that we abide by any htlc_maximum_msat provided in the route hints of the payment
+               // params in the final route.
+               let (secp_ctx, network_graph, _, _, logger) = build_graph();
+               let netgraph = network_graph.read_only();
+               let (_, our_id, _, nodes) = get_nodes(&secp_ctx);
+               let scorer = ln_test_utils::TestScorer::new();
+               let keys_manager = ln_test_utils::TestKeysInterface::new(&[0u8; 32], Network::Testnet);
+               let random_seed_bytes = keys_manager.get_secure_random_bytes();
+               let config = UserConfig::default();
+
+               let max_htlc_msat = 50_000;
+               let route_hint_1 = RouteHint(vec![RouteHintHop {
+                       src_node_id: nodes[2],
+                       short_channel_id: 42,
+                       fees: RoutingFees {
+                               base_msat: 100,
+                               proportional_millionths: 0,
+                       },
+                       cltv_expiry_delta: 10,
+                       htlc_minimum_msat: None,
+                       htlc_maximum_msat: Some(max_htlc_msat),
+               }]);
+               let dest_node_id = ln_test_utils::pubkey(42);
+               let payment_params = PaymentParameters::from_node_id(dest_node_id, 42)
+                       .with_route_hints(vec![route_hint_1.clone()]).unwrap()
+                       .with_bolt11_features(channelmanager::provided_invoice_features(&config)).unwrap();
+
+               // Make sure we'll error if our route hints don't have enough liquidity according to their
+               // htlc_maximum_msat.
+               if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = get_route(&our_id,
+                       &payment_params, &netgraph, None, max_htlc_msat + 1, Arc::clone(&logger), &scorer, &(),
+                       &random_seed_bytes)
+               {
+                       assert_eq!(err, "Failed to find a sufficient route to the given destination");
+               } else { panic!(); }
+
+               // Make sure we'll split an MPP payment across route hints if their htlc_maximum_msat warrants.
+               let mut route_hint_2 = route_hint_1.clone();
+               route_hint_2.0[0].short_channel_id = 43;
+               let payment_params = PaymentParameters::from_node_id(dest_node_id, 42)
+                       .with_route_hints(vec![route_hint_1, route_hint_2]).unwrap()
+                       .with_bolt11_features(channelmanager::provided_invoice_features(&config)).unwrap();
+               let route = get_route(&our_id, &payment_params, &netgraph, None, max_htlc_msat + 1,
+                       Arc::clone(&logger), &scorer, &(), &random_seed_bytes).unwrap();
+               assert_eq!(route.paths.len(), 2);
+               assert!(route.paths[0].hops.last().unwrap().fee_msat <= max_htlc_msat);
+               assert!(route.paths[1].hops.last().unwrap().fee_msat <= max_htlc_msat);
+       }
+
+       #[test]
+       fn direct_channel_to_hints_with_max_htlc() {
+               // Check that if we have a first hop channel peer that's connected to multiple provided route
+               // hints, that we properly split the payment between the route hints if needed.
+               let logger = Arc::new(ln_test_utils::TestLogger::new());
+               let network_graph = Arc::new(NetworkGraph::new(Network::Testnet, Arc::clone(&logger)));
+               let scorer = ln_test_utils::TestScorer::new();
+               let keys_manager = ln_test_utils::TestKeysInterface::new(&[0u8; 32], Network::Testnet);
+               let random_seed_bytes = keys_manager.get_secure_random_bytes();
+               let config = UserConfig::default();
+
+               let our_node_id = ln_test_utils::pubkey(42);
+               let intermed_node_id = ln_test_utils::pubkey(43);
+               let first_hop = vec![get_channel_details(Some(42), intermed_node_id, InitFeatures::from_le_bytes(vec![0b11]), 10_000_000)];
+
+               let amt_msat = 900_000;
+               let max_htlc_msat = 500_000;
+               let route_hint_1 = RouteHint(vec![RouteHintHop {
+                       src_node_id: intermed_node_id,
+                       short_channel_id: 44,
+                       fees: RoutingFees {
+                               base_msat: 100,
+                               proportional_millionths: 0,
+                       },
+                       cltv_expiry_delta: 10,
+                       htlc_minimum_msat: None,
+                       htlc_maximum_msat: Some(max_htlc_msat),
+               }, RouteHintHop {
+                       src_node_id: intermed_node_id,
+                       short_channel_id: 45,
+                       fees: RoutingFees {
+                               base_msat: 100,
+                               proportional_millionths: 0,
+                       },
+                       cltv_expiry_delta: 10,
+                       htlc_minimum_msat: None,
+                       // Check that later route hint max htlcs don't override earlier ones
+                       htlc_maximum_msat: Some(max_htlc_msat - 50),
+               }]);
+               let mut route_hint_2 = route_hint_1.clone();
+               route_hint_2.0[0].short_channel_id = 46;
+               route_hint_2.0[1].short_channel_id = 47;
+               let dest_node_id = ln_test_utils::pubkey(44);
+               let payment_params = PaymentParameters::from_node_id(dest_node_id, 42)
+                       .with_route_hints(vec![route_hint_1, route_hint_2]).unwrap()
+                       .with_bolt11_features(channelmanager::provided_invoice_features(&config)).unwrap();
+
+               let route = get_route(&our_node_id, &payment_params, &network_graph.read_only(),
+                       Some(&first_hop.iter().collect::<Vec<_>>()), amt_msat, Arc::clone(&logger), &scorer, &(),
+                       &random_seed_bytes).unwrap();
+               assert_eq!(route.paths.len(), 2);
+               assert!(route.paths[0].hops.last().unwrap().fee_msat <= max_htlc_msat);
+               assert!(route.paths[1].hops.last().unwrap().fee_msat <= max_htlc_msat);
+               assert_eq!(route.get_total_amount(), amt_msat);
+
+               // Re-run but with two first hop channels connected to the same route hint peers that must be
+               // split between.
+               let first_hops = vec![
+                       get_channel_details(Some(42), intermed_node_id, InitFeatures::from_le_bytes(vec![0b11]), amt_msat - 10),
+                       get_channel_details(Some(43), intermed_node_id, InitFeatures::from_le_bytes(vec![0b11]), amt_msat - 10),
+               ];
+               let route = get_route(&our_node_id, &payment_params, &network_graph.read_only(),
+                       Some(&first_hops.iter().collect::<Vec<_>>()), amt_msat, Arc::clone(&logger), &scorer, &(),
+                       &random_seed_bytes).unwrap();
+               assert_eq!(route.paths.len(), 2);
+               assert!(route.paths[0].hops.last().unwrap().fee_msat <= max_htlc_msat);
+               assert!(route.paths[1].hops.last().unwrap().fee_msat <= max_htlc_msat);
+               assert_eq!(route.get_total_amount(), amt_msat);
+
+               // Make sure this works for blinded route hints.
+               let blinded_path = BlindedPath {
+                       introduction_node_id: intermed_node_id,
+                       blinding_point: ln_test_utils::pubkey(42),
+                       blinded_hops: vec![
+                               BlindedHop { blinded_node_id: ln_test_utils::pubkey(42), encrypted_payload: vec![] },
+                               BlindedHop { blinded_node_id: ln_test_utils::pubkey(43), encrypted_payload: vec![] },
+                       ],
+               };
+               let blinded_payinfo = BlindedPayInfo {
+                       fee_base_msat: 100,
+                       fee_proportional_millionths: 0,
+                       htlc_minimum_msat: 1,
+                       htlc_maximum_msat: max_htlc_msat,
+                       cltv_expiry_delta: 10,
+                       features: BlindedHopFeatures::empty(),
+               };
+               let bolt12_features: Bolt12InvoiceFeatures = channelmanager::provided_invoice_features(&config).to_context();
+               let payment_params = PaymentParameters::blinded(vec![
+                       (blinded_payinfo.clone(), blinded_path.clone()),
+                       (blinded_payinfo.clone(), blinded_path.clone())])
+                       .with_bolt12_features(bolt12_features).unwrap();
+               let route = get_route(&our_node_id, &payment_params, &network_graph.read_only(),
+                       Some(&first_hops.iter().collect::<Vec<_>>()), amt_msat, Arc::clone(&logger), &scorer, &(),
+                       &random_seed_bytes).unwrap();
+               assert_eq!(route.paths.len(), 2);
+               assert!(route.paths[0].hops.last().unwrap().fee_msat <= max_htlc_msat);
+               assert!(route.paths[1].hops.last().unwrap().fee_msat <= max_htlc_msat);
+               assert_eq!(route.get_total_amount(), amt_msat);
+       }
+
        #[test]
        fn blinded_route_ser() {
                let blinded_path_1 = BlindedPath {
@@ -6050,11 +6484,209 @@ mod tests {
                assert_eq!(route.paths[0].blinded_tail.as_ref().unwrap().excess_final_cltv_expiry_delta, 40);
                assert_eq!(route.paths[0].hops.last().unwrap().cltv_expiry_delta, 40);
        }
+
+       #[test]
+       fn simple_blinded_route_hints() {
+               do_simple_blinded_route_hints(1);
+               do_simple_blinded_route_hints(2);
+               do_simple_blinded_route_hints(3);
+       }
+
+       fn do_simple_blinded_route_hints(num_blinded_hops: usize) {
+               // Check that we can generate a route to a blinded path with the expected hops.
+               let (secp_ctx, network, _, _, logger) = build_graph();
+               let (_, our_id, _, nodes) = get_nodes(&secp_ctx);
+               let network_graph = network.read_only();
+
+               let scorer = ln_test_utils::TestScorer::new();
+               let keys_manager = ln_test_utils::TestKeysInterface::new(&[0u8; 32], Network::Testnet);
+               let random_seed_bytes = keys_manager.get_secure_random_bytes();
+
+               let mut blinded_path = BlindedPath {
+                       introduction_node_id: nodes[2],
+                       blinding_point: ln_test_utils::pubkey(42),
+                       blinded_hops: Vec::with_capacity(num_blinded_hops),
+               };
+               for i in 0..num_blinded_hops {
+                       blinded_path.blinded_hops.push(
+                               BlindedHop { blinded_node_id: ln_test_utils::pubkey(42 + i as u8), encrypted_payload: Vec::new() },
+                       );
+               }
+               let blinded_payinfo = BlindedPayInfo {
+                       fee_base_msat: 100,
+                       fee_proportional_millionths: 500,
+                       htlc_minimum_msat: 1000,
+                       htlc_maximum_msat: 100_000_000,
+                       cltv_expiry_delta: 15,
+                       features: BlindedHopFeatures::empty(),
+               };
+
+               let final_amt_msat = 1001;
+               let payment_params = PaymentParameters::blinded(vec![(blinded_payinfo.clone(), blinded_path.clone())]);
+               let route = get_route(&our_id, &payment_params, &network_graph, None, final_amt_msat , Arc::clone(&logger),
+                       &scorer, &(), &random_seed_bytes).unwrap();
+               assert_eq!(route.paths.len(), 1);
+               assert_eq!(route.paths[0].hops.len(), 2);
+
+               let tail = route.paths[0].blinded_tail.as_ref().unwrap();
+               assert_eq!(tail.hops, blinded_path.blinded_hops);
+               assert_eq!(tail.excess_final_cltv_expiry_delta, 0);
+               assert_eq!(tail.final_value_msat, 1001);
+
+               let final_hop = route.paths[0].hops.last().unwrap();
+               assert_eq!(final_hop.pubkey, blinded_path.introduction_node_id);
+               if tail.hops.len() > 1 {
+                       assert_eq!(final_hop.fee_msat,
+                               blinded_payinfo.fee_base_msat as u64 + blinded_payinfo.fee_proportional_millionths as u64 * tail.final_value_msat / 1000000);
+                       assert_eq!(final_hop.cltv_expiry_delta, blinded_payinfo.cltv_expiry_delta as u32);
+               } else {
+                       assert_eq!(final_hop.fee_msat, 0);
+                       assert_eq!(final_hop.cltv_expiry_delta, 0);
+               }
+       }
+
+       #[test]
+       fn blinded_path_routing_errors() {
+               // Check that we can generate a route to a blinded path with the expected hops.
+               let (secp_ctx, network, _, _, logger) = build_graph();
+               let (_, our_id, _, nodes) = get_nodes(&secp_ctx);
+               let network_graph = network.read_only();
+
+               let scorer = ln_test_utils::TestScorer::new();
+               let keys_manager = ln_test_utils::TestKeysInterface::new(&[0u8; 32], Network::Testnet);
+               let random_seed_bytes = keys_manager.get_secure_random_bytes();
+
+               let mut invalid_blinded_path = BlindedPath {
+                       introduction_node_id: nodes[2],
+                       blinding_point: ln_test_utils::pubkey(42),
+                       blinded_hops: vec![
+                               BlindedHop { blinded_node_id: ln_test_utils::pubkey(43), encrypted_payload: vec![0; 43] },
+                       ],
+               };
+               let blinded_payinfo = BlindedPayInfo {
+                       fee_base_msat: 100,
+                       fee_proportional_millionths: 500,
+                       htlc_minimum_msat: 1000,
+                       htlc_maximum_msat: 100_000_000,
+                       cltv_expiry_delta: 15,
+                       features: BlindedHopFeatures::empty(),
+               };
+
+               let mut invalid_blinded_path_2 = invalid_blinded_path.clone();
+               invalid_blinded_path_2.introduction_node_id = ln_test_utils::pubkey(45);
+               let payment_params = PaymentParameters::blinded(vec![
+                       (blinded_payinfo.clone(), invalid_blinded_path.clone()),
+                       (blinded_payinfo.clone(), invalid_blinded_path_2)]);
+               match get_route(&our_id, &payment_params, &network_graph, None, 1001, Arc::clone(&logger),
+                       &scorer, &(), &random_seed_bytes)
+               {
+                       Err(LightningError { err, .. }) => {
+                               assert_eq!(err, "1-hop blinded paths must all have matching introduction node ids");
+                       },
+                       _ => panic!("Expected error")
+               }
+
+               invalid_blinded_path.introduction_node_id = our_id;
+               let payment_params = PaymentParameters::blinded(vec![(blinded_payinfo.clone(), invalid_blinded_path.clone())]);
+               match get_route(&our_id, &payment_params, &network_graph, None, 1001, Arc::clone(&logger),
+                       &scorer, &(), &random_seed_bytes)
+               {
+                       Err(LightningError { err, .. }) => {
+                               assert_eq!(err, "Cannot generate a route to blinded paths if we are the introduction node to all of them");
+                       },
+                       _ => panic!("Expected error")
+               }
+
+               invalid_blinded_path.introduction_node_id = ln_test_utils::pubkey(46);
+               invalid_blinded_path.blinded_hops.clear();
+               let payment_params = PaymentParameters::blinded(vec![(blinded_payinfo, invalid_blinded_path)]);
+               match get_route(&our_id, &payment_params, &network_graph, None, 1001, Arc::clone(&logger),
+                       &scorer, &(), &random_seed_bytes)
+               {
+                       Err(LightningError { err, .. }) => {
+                               assert_eq!(err, "0-hop blinded path provided");
+                       },
+                       _ => panic!("Expected error")
+               }
+       }
+
+       #[test]
+       fn matching_intro_node_paths_provided() {
+               // Check that if multiple blinded paths with the same intro node are provided in payment
+               // parameters, we'll return the correct paths in the resulting MPP route.
+               let (secp_ctx, network, _, _, logger) = build_graph();
+               let (_, our_id, _, nodes) = get_nodes(&secp_ctx);
+               let network_graph = network.read_only();
+
+               let scorer = ln_test_utils::TestScorer::new();
+               let keys_manager = ln_test_utils::TestKeysInterface::new(&[0u8; 32], Network::Testnet);
+               let random_seed_bytes = keys_manager.get_secure_random_bytes();
+               let config = UserConfig::default();
+
+               let bolt12_features: Bolt12InvoiceFeatures = channelmanager::provided_invoice_features(&config).to_context();
+               let blinded_path_1 = BlindedPath {
+                       introduction_node_id: nodes[2],
+                       blinding_point: ln_test_utils::pubkey(42),
+                       blinded_hops: vec![
+                               BlindedHop { blinded_node_id: ln_test_utils::pubkey(42 as u8), encrypted_payload: Vec::new() },
+                               BlindedHop { blinded_node_id: ln_test_utils::pubkey(42 as u8), encrypted_payload: Vec::new() }
+                       ],
+               };
+               let blinded_payinfo_1 = BlindedPayInfo {
+                       fee_base_msat: 0,
+                       fee_proportional_millionths: 0,
+                       htlc_minimum_msat: 0,
+                       htlc_maximum_msat: 30_000,
+                       cltv_expiry_delta: 0,
+                       features: BlindedHopFeatures::empty(),
+               };
+
+               let mut blinded_path_2 = blinded_path_1.clone();
+               blinded_path_2.blinding_point = ln_test_utils::pubkey(43);
+               let mut blinded_payinfo_2 = blinded_payinfo_1.clone();
+               blinded_payinfo_2.htlc_maximum_msat = 70_000;
+
+               let blinded_hints = vec![
+                       (blinded_payinfo_1.clone(), blinded_path_1.clone()),
+                       (blinded_payinfo_2.clone(), blinded_path_2.clone()),
+               ];
+               let payment_params = PaymentParameters::blinded(blinded_hints.clone())
+                       .with_bolt12_features(bolt12_features.clone()).unwrap();
+
+               let route = get_route(&our_id, &payment_params, &network_graph, None,
+                       100_000, Arc::clone(&logger), &scorer, &(), &random_seed_bytes).unwrap();
+               assert_eq!(route.paths.len(), 2);
+               let mut total_amount_paid_msat = 0;
+               for path in route.paths.into_iter() {
+                       assert_eq!(path.hops.last().unwrap().pubkey, nodes[2]);
+                       if let Some(bt) = &path.blinded_tail {
+                               assert_eq!(bt.blinding_point,
+                                       blinded_hints.iter().find(|(p, _)| p.htlc_maximum_msat == path.final_value_msat())
+                                               .map(|(_, bp)| bp.blinding_point).unwrap());
+                       } else { panic!(); }
+                       total_amount_paid_msat += path.final_value_msat();
+               }
+               assert_eq!(total_amount_paid_msat, 100_000);
+       }
 }
 
-#[cfg(all(test, not(feature = "no-std")))]
+#[cfg(all(any(test, ldk_bench), not(feature = "no-std")))]
 pub(crate) mod bench_utils {
+       use super::*;
        use std::fs::File;
+
+       use bitcoin::hashes::Hash;
+       use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
+
+       use crate::chain::transaction::OutPoint;
+       use crate::sign::{EntropySource, KeysManager};
+       use crate::ln::channelmanager::{self, ChannelCounterparty, ChannelDetails};
+       use crate::ln::features::InvoiceFeatures;
+       use crate::routing::gossip::NetworkGraph;
+       use crate::util::config::UserConfig;
+       use crate::util::ser::ReadableArgs;
+       use crate::util::test_utils::TestLogger;
+
        /// Tries to open a network graph file, or panics with a URL to fetch it.
        pub(crate) fn get_route_file() -> Result<std::fs::File, &'static str> {
                let res = File::open("net_graph-2023-01-18.bin") // By default we're run in RL/lightning
@@ -6068,7 +6700,18 @@ pub(crate) mod bench_utils {
                                path.pop(); // target
                                path.push("lightning");
                                path.push("net_graph-2023-01-18.bin");
-                               eprintln!("{}", path.to_str().unwrap());
+                               File::open(path)
+                       })
+                       .or_else(|_| { // Fall back to guessing based on the binary location for a subcrate
+                               // path is likely something like .../rust-lightning/bench/target/debug/deps/bench..
+                               let mut path = std::env::current_exe().unwrap();
+                               path.pop(); // bench...
+                               path.pop(); // deps
+                               path.pop(); // debug
+                               path.pop(); // target
+                               path.pop(); // bench
+                               path.push("lightning");
+                               path.push("net_graph-2023-01-18.bin");
                                File::open(path)
                        })
                .map_err(|_| "Please fetch https://bitcoin.ninja/ldk-net_graph-v0.0.113-2023-01-18.bin and place it at lightning/net_graph-2023-01-18.bin");
@@ -6077,42 +6720,18 @@ pub(crate) mod bench_utils {
                #[cfg(not(require_route_graph_test))]
                return res;
        }
-}
 
-#[cfg(all(test, feature = "_bench_unstable", not(feature = "no-std")))]
-mod benches {
-       use super::*;
-       use bitcoin::hashes::Hash;
-       use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
-       use crate::chain::transaction::OutPoint;
-       use crate::sign::{EntropySource, KeysManager};
-       use crate::ln::channelmanager::{self, ChannelCounterparty, ChannelDetails};
-       use crate::ln::features::InvoiceFeatures;
-       use crate::routing::gossip::NetworkGraph;
-       use crate::routing::scoring::{FixedPenaltyScorer, ProbabilisticScorer, ProbabilisticScoringFeeParameters, ProbabilisticScoringDecayParameters};
-       use crate::util::config::UserConfig;
-       use crate::util::logger::{Logger, Record};
-       use crate::util::ser::ReadableArgs;
-
-       use test::Bencher;
-
-       struct DummyLogger {}
-       impl Logger for DummyLogger {
-               fn log(&self, _record: &Record) {}
+       pub(crate) fn read_network_graph(logger: &TestLogger) -> Result<NetworkGraph<&TestLogger>, &'static str> {
+               get_route_file().map(|mut f| NetworkGraph::read(&mut f, logger).unwrap())
        }
 
-       fn read_network_graph(logger: &DummyLogger) -> NetworkGraph<&DummyLogger> {
-               let mut d = bench_utils::get_route_file().unwrap();
-               NetworkGraph::read(&mut d, logger).unwrap()
-       }
-
-       fn payer_pubkey() -> PublicKey {
+       pub(crate) fn payer_pubkey() -> PublicKey {
                let secp_ctx = Secp256k1::new();
                PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap())
        }
 
        #[inline]
-       fn first_hop(node_id: PublicKey) -> ChannelDetails {
+       pub(crate) fn first_hop(node_id: PublicKey) -> ChannelDetails {
                ChannelDetails {
                        channel_id: [0; 32],
                        counterparty: ChannelCounterparty {
@@ -6130,11 +6749,12 @@ mod benches {
                        short_channel_id: Some(1),
                        inbound_scid_alias: None,
                        outbound_scid_alias: None,
-                       channel_value_satoshis: 10_000_000,
+                       channel_value_satoshis: 10_000_000_000,
                        user_channel_id: 0,
-                       balance_msat: 10_000_000,
-                       outbound_capacity_msat: 10_000_000,
-                       next_outbound_htlc_limit_msat: 10_000_000,
+                       balance_msat: 10_000_000_000,
+                       outbound_capacity_msat: 10_000_000_000,
+                       next_outbound_htlc_minimum_msat: 0,
+                       next_outbound_htlc_limit_msat: 10_000_000_000,
                        inbound_capacity_msat: 0,
                        unspendable_punishment_reserve: None,
                        confirmations_required: None,
@@ -6148,103 +6768,171 @@ mod benches {
                        inbound_htlc_maximum_msat: None,
                        config: None,
                        feerate_sat_per_1000_weight: None,
+                       channel_shutdown_state: Some(channelmanager::ChannelShutdownState::NotShuttingDown),
                }
        }
 
-       #[bench]
-       fn generate_routes_with_zero_penalty_scorer(bench: &mut Bencher) {
-               let logger = DummyLogger {};
-               let network_graph = read_network_graph(&logger);
+       pub(crate) fn generate_test_routes<S: Score>(graph: &NetworkGraph<&TestLogger>, scorer: &mut S,
+               score_params: &S::ScoreParams, features: InvoiceFeatures, mut seed: u64,
+               starting_amount: u64, route_count: usize,
+       ) -> Vec<(ChannelDetails, PaymentParameters, u64)> {
+               let payer = payer_pubkey();
+               let keys_manager = KeysManager::new(&[0u8; 32], 42, 42);
+               let random_seed_bytes = keys_manager.get_secure_random_bytes();
+
+               let nodes = graph.read_only().nodes().clone();
+               let mut route_endpoints = Vec::new();
+               // Fetch 1.5x more routes than we need as after we do some scorer updates we may end up
+               // with some routes we picked being un-routable.
+               for _ in 0..route_count * 3 / 2 {
+                       loop {
+                               seed = seed.overflowing_mul(6364136223846793005).0.overflowing_add(1).0;
+                               let src = PublicKey::from_slice(nodes.unordered_keys()
+                                       .skip((seed as usize) % nodes.len()).next().unwrap().as_slice()).unwrap();
+                               seed = seed.overflowing_mul(6364136223846793005).0.overflowing_add(1).0;
+                               let dst = PublicKey::from_slice(nodes.unordered_keys()
+                                       .skip((seed as usize) % nodes.len()).next().unwrap().as_slice()).unwrap();
+                               let params = PaymentParameters::from_node_id(dst, 42)
+                                       .with_bolt11_features(features.clone()).unwrap();
+                               let first_hop = first_hop(src);
+                               let amt = starting_amount + seed % 1_000_000;
+                               let path_exists =
+                                       get_route(&payer, &params, &graph.read_only(), Some(&[&first_hop]),
+                                               amt, &TestLogger::new(), &scorer, score_params, &random_seed_bytes).is_ok();
+                               if path_exists {
+                                       // ...and seed the scorer with success and failure data...
+                                       seed = seed.overflowing_mul(6364136223846793005).0.overflowing_add(1).0;
+                                       let mut score_amt = seed % 1_000_000_000;
+                                       loop {
+                                               // Generate fail/success paths for a wider range of potential amounts with
+                                               // MPP enabled to give us a chance to apply penalties for more potential
+                                               // routes.
+                                               let mpp_features = channelmanager::provided_invoice_features(&UserConfig::default());
+                                               let params = PaymentParameters::from_node_id(dst, 42)
+                                                       .with_bolt11_features(mpp_features).unwrap();
+
+                                               let route_res = get_route(&payer, &params, &graph.read_only(),
+                                                       Some(&[&first_hop]), score_amt, &TestLogger::new(), &scorer,
+                                                       score_params, &random_seed_bytes);
+                                               if let Ok(route) = route_res {
+                                                       for path in route.paths {
+                                                               if seed & 0x80 == 0 {
+                                                                       scorer.payment_path_successful(&path);
+                                                               } else {
+                                                                       let short_channel_id = path.hops[path.hops.len() / 2].short_channel_id;
+                                                                       scorer.payment_path_failed(&path, short_channel_id);
+                                                               }
+                                                               seed = seed.overflowing_mul(6364136223846793005).0.overflowing_add(1).0;
+                                                       }
+                                                       break;
+                                               }
+                                               // If we couldn't find a path with a higer amount, reduce and try again.
+                                               score_amt /= 100;
+                                       }
+
+                                       route_endpoints.push((first_hop, params, amt));
+                                       break;
+                               }
+                       }
+               }
+
+               // Because we've changed channel scores, it's possible we'll take different routes to the
+               // selected destinations, possibly causing us to fail because, eg, the newly-selected path
+               // requires a too-high CLTV delta.
+               route_endpoints.retain(|(first_hop, params, amt)| {
+                       get_route(&payer, params, &graph.read_only(), Some(&[first_hop]), *amt,
+                               &TestLogger::new(), &scorer, score_params, &random_seed_bytes).is_ok()
+               });
+               route_endpoints.truncate(route_count);
+               assert_eq!(route_endpoints.len(), route_count);
+               route_endpoints
+       }
+}
+
+#[cfg(ldk_bench)]
+pub mod benches {
+       use super::*;
+       use crate::sign::{EntropySource, KeysManager};
+       use crate::ln::channelmanager;
+       use crate::ln::features::InvoiceFeatures;
+       use crate::routing::gossip::NetworkGraph;
+       use crate::routing::scoring::{FixedPenaltyScorer, ProbabilisticScorer, ProbabilisticScoringFeeParameters, ProbabilisticScoringDecayParameters};
+       use crate::util::config::UserConfig;
+       use crate::util::logger::{Logger, Record};
+       use crate::util::test_utils::TestLogger;
+
+       use criterion::Criterion;
+
+       struct DummyLogger {}
+       impl Logger for DummyLogger {
+               fn log(&self, _record: &Record) {}
+       }
+
+       pub fn generate_routes_with_zero_penalty_scorer(bench: &mut Criterion) {
+               let logger = TestLogger::new();
+               let network_graph = bench_utils::read_network_graph(&logger).unwrap();
                let scorer = FixedPenaltyScorer::with_penalty(0);
-               generate_routes(bench, &network_graph, scorer, &(), InvoiceFeatures::empty());
+               generate_routes(bench, &network_graph, scorer, &(), InvoiceFeatures::empty(), 0,
+                       "generate_routes_with_zero_penalty_scorer");
        }
 
-       #[bench]
-       fn generate_mpp_routes_with_zero_penalty_scorer(bench: &mut Bencher) {
-               let logger = DummyLogger {};
-               let network_graph = read_network_graph(&logger);
+       pub fn generate_mpp_routes_with_zero_penalty_scorer(bench: &mut Criterion) {
+               let logger = TestLogger::new();
+               let network_graph = bench_utils::read_network_graph(&logger).unwrap();
                let scorer = FixedPenaltyScorer::with_penalty(0);
-               generate_routes(bench, &network_graph, scorer, &(), channelmanager::provided_invoice_features(&UserConfig::default()));
+               generate_routes(bench, &network_graph, scorer, &(),
+                       channelmanager::provided_invoice_features(&UserConfig::default()), 0,
+                       "generate_mpp_routes_with_zero_penalty_scorer");
        }
 
-       #[bench]
-       fn generate_routes_with_probabilistic_scorer(bench: &mut Bencher) {
-               let logger = DummyLogger {};
-               let network_graph = read_network_graph(&logger);
+       pub fn generate_routes_with_probabilistic_scorer(bench: &mut Criterion) {
+               let logger = TestLogger::new();
+               let network_graph = bench_utils::read_network_graph(&logger).unwrap();
                let params = ProbabilisticScoringFeeParameters::default();
                let scorer = ProbabilisticScorer::new(ProbabilisticScoringDecayParameters::default(), &network_graph, &logger);
-               generate_routes(bench, &network_graph, scorer, &params, InvoiceFeatures::empty());
+               generate_routes(bench, &network_graph, scorer, &params, InvoiceFeatures::empty(), 0,
+                       "generate_routes_with_probabilistic_scorer");
        }
 
-       #[bench]
-       fn generate_mpp_routes_with_probabilistic_scorer(bench: &mut Bencher) {
-               let logger = DummyLogger {};
-               let network_graph = read_network_graph(&logger);
+       pub fn generate_mpp_routes_with_probabilistic_scorer(bench: &mut Criterion) {
+               let logger = TestLogger::new();
+               let network_graph = bench_utils::read_network_graph(&logger).unwrap();
                let params = ProbabilisticScoringFeeParameters::default();
                let scorer = ProbabilisticScorer::new(ProbabilisticScoringDecayParameters::default(), &network_graph, &logger);
-               generate_routes(bench, &network_graph, scorer, &params, channelmanager::provided_invoice_features(&UserConfig::default()));
+               generate_routes(bench, &network_graph, scorer, &params,
+                       channelmanager::provided_invoice_features(&UserConfig::default()), 0,
+                       "generate_mpp_routes_with_probabilistic_scorer");
+       }
+
+       pub fn generate_large_mpp_routes_with_probabilistic_scorer(bench: &mut Criterion) {
+               let logger = TestLogger::new();
+               let network_graph = bench_utils::read_network_graph(&logger).unwrap();
+               let params = ProbabilisticScoringFeeParameters::default();
+               let scorer = ProbabilisticScorer::new(ProbabilisticScoringDecayParameters::default(), &network_graph, &logger);
+               generate_routes(bench, &network_graph, scorer, &params,
+                       channelmanager::provided_invoice_features(&UserConfig::default()), 100_000_000,
+                       "generate_large_mpp_routes_with_probabilistic_scorer");
        }
 
        fn generate_routes<S: Score>(
-               bench: &mut Bencher, graph: &NetworkGraph<&DummyLogger>, mut scorer: S, score_params: &S::ScoreParams,
-               features: InvoiceFeatures
+               bench: &mut Criterion, graph: &NetworkGraph<&TestLogger>, mut scorer: S,
+               score_params: &S::ScoreParams, features: InvoiceFeatures, starting_amount: u64,
+               bench_name: &'static str,
        ) {
-               let nodes = graph.read_only().nodes().clone();
-               let payer = payer_pubkey();
+               let payer = bench_utils::payer_pubkey();
                let keys_manager = KeysManager::new(&[0u8; 32], 42, 42);
                let random_seed_bytes = keys_manager.get_secure_random_bytes();
 
                // First, get 100 (source, destination) pairs for which route-getting actually succeeds...
-               let mut routes = Vec::new();
-               let mut route_endpoints = Vec::new();
-               let mut seed: usize = 0xdeadbeef;
-               'load_endpoints: for _ in 0..150 {
-                       loop {
-                               seed *= 0xdeadbeef;
-                               let src = PublicKey::from_slice(nodes.unordered_keys().skip(seed % nodes.len()).next().unwrap().as_slice()).unwrap();
-                               seed *= 0xdeadbeef;
-                               let dst = PublicKey::from_slice(nodes.unordered_keys().skip(seed % nodes.len()).next().unwrap().as_slice()).unwrap();
-                               let params = PaymentParameters::from_node_id(dst, 42).with_bolt11_features(features.clone()).unwrap();
-                               let first_hop = first_hop(src);
-                               let amt = seed as u64 % 1_000_000;
-                               if let Ok(route) = get_route(&payer, &params, &graph.read_only(), Some(&[&first_hop]), amt, &DummyLogger{}, &scorer, score_params, &random_seed_bytes) {
-                                       routes.push(route);
-                                       route_endpoints.push((first_hop, params, amt));
-                                       continue 'load_endpoints;
-                               }
-                       }
-               }
-
-               // ...and seed the scorer with success and failure data...
-               for route in routes {
-                       let amount = route.get_total_amount();
-                       if amount < 250_000 {
-                               for path in route.paths {
-                                       scorer.payment_path_successful(&path);
-                               }
-                       } else if amount > 750_000 {
-                               for path in route.paths {
-                                       let short_channel_id = path.hops[path.hops.len() / 2].short_channel_id;
-                                       scorer.payment_path_failed(&path, short_channel_id);
-                               }
-                       }
-               }
-
-               // Because we've changed channel scores, its possible we'll take different routes to the
-               // selected destinations, possibly causing us to fail because, eg, the newly-selected path
-               // requires a too-high CLTV delta.
-               route_endpoints.retain(|(first_hop, params, amt)| {
-                       get_route(&payer, params, &graph.read_only(), Some(&[first_hop]), *amt, &DummyLogger{}, &scorer, score_params, &random_seed_bytes).is_ok()
-               });
-               route_endpoints.truncate(100);
-               assert_eq!(route_endpoints.len(), 100);
+               let route_endpoints = bench_utils::generate_test_routes(graph, &mut scorer, score_params, features, 0xdeadbeef, starting_amount, 50);
 
                // ...then benchmark finding paths between the nodes we learned.
                let mut idx = 0;
-               bench.iter(|| {
+               bench.bench_function(bench_name, |b| b.iter(|| {
                        let (first_hop, params, amt) = &route_endpoints[idx % route_endpoints.len()];
-                       assert!(get_route(&payer, params, &graph.read_only(), Some(&[first_hop]), *amt, &DummyLogger{}, &scorer, score_params, &random_seed_bytes).is_ok());
+                       assert!(get_route(&payer, params, &graph.read_only(), Some(&[first_hop]), *amt,
+                               &DummyLogger{}, &scorer, score_params, &random_seed_bytes).is_ok());
                        idx += 1;
-               });
+               }));
        }
 }
index 235b1a148a5824ad020cf88dc3fc652679e3ba1f..d83adaf97f6603e321743a96f277e2bea6a10fac 100644 (file)
@@ -157,8 +157,11 @@ define_score!();
 ///
 /// [`find_route`]: crate::routing::router::find_route
 pub trait LockableScore<'a> {
+       /// The [`Score`] type.
+       type Score: 'a + Score;
+
        /// The locked [`Score`] type.
-       type Locked: 'a + Score;
+       type Locked: DerefMut<Target = Self::Score> + Sized;
 
        /// Returns the locked scorer.
        fn lock(&'a self) -> Self::Locked;
@@ -174,60 +177,35 @@ pub trait WriteableScore<'a>: LockableScore<'a> + Writeable {}
 impl<'a, T> WriteableScore<'a> for T where T: LockableScore<'a> + Writeable {}
 /// This is not exported to bindings users
 impl<'a, T: 'a + Score> LockableScore<'a> for Mutex<T> {
+       type Score = T;
        type Locked = MutexGuard<'a, T>;
 
-       fn lock(&'a self) -> MutexGuard<'a, T> {
+       fn lock(&'a self) -> Self::Locked {
                Mutex::lock(self).unwrap()
        }
 }
 
 impl<'a, T: 'a + Score> LockableScore<'a> for RefCell<T> {
+       type Score = T;
        type Locked = RefMut<'a, T>;
 
-       fn lock(&'a self) -> RefMut<'a, T> {
+       fn lock(&'a self) -> Self::Locked {
                self.borrow_mut()
        }
 }
 
 #[cfg(c_bindings)]
 /// A concrete implementation of [`LockableScore`] which supports multi-threading.
-pub struct MultiThreadedLockableScore<S: Score> {
-       score: Mutex<S>,
-}
-#[cfg(c_bindings)]
-/// A locked `MultiThreadedLockableScore`.
-pub struct MultiThreadedScoreLock<'a, S: Score>(MutexGuard<'a, S>);
-#[cfg(c_bindings)]
-impl<'a, T: Score + 'a> Score for MultiThreadedScoreLock<'a, T> {
-       type ScoreParams = <T as Score>::ScoreParams;
-       fn channel_penalty_msat(&self, scid: u64, source: &NodeId, target: &NodeId, usage: ChannelUsage, score_params: &Self::ScoreParams) -> u64 {
-               self.0.channel_penalty_msat(scid, source, target, usage, score_params)
-       }
-       fn payment_path_failed(&mut self, path: &Path, short_channel_id: u64) {
-               self.0.payment_path_failed(path, short_channel_id)
-       }
-       fn payment_path_successful(&mut self, path: &Path) {
-               self.0.payment_path_successful(path)
-       }
-       fn probe_failed(&mut self, path: &Path, short_channel_id: u64) {
-               self.0.probe_failed(path, short_channel_id)
-       }
-       fn probe_successful(&mut self, path: &Path) {
-               self.0.probe_successful(path)
-       }
-}
-#[cfg(c_bindings)]
-impl<'a, T: Score + 'a> Writeable for MultiThreadedScoreLock<'a, T> {
-       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
-               self.0.write(writer)
-       }
+pub struct MultiThreadedLockableScore<T: Score> {
+       score: Mutex<T>,
 }
 
 #[cfg(c_bindings)]
-impl<'a, T: Score + 'a> LockableScore<'a> for MultiThreadedLockableScore<T> {
+impl<'a, T: 'a + Score> LockableScore<'a> for MultiThreadedLockableScore<T> {
+       type Score = T;
        type Locked = MultiThreadedScoreLock<'a, T>;
 
-       fn lock(&'a self) -> MultiThreadedScoreLock<'a, T> {
+       fn lock(&'a self) -> Self::Locked {
                MultiThreadedScoreLock(Mutex::lock(&self.score).unwrap())
        }
 }
@@ -240,7 +218,7 @@ impl<T: Score> Writeable for MultiThreadedLockableScore<T> {
 }
 
 #[cfg(c_bindings)]
-impl<'a, T: Score + 'a> WriteableScore<'a> for MultiThreadedLockableScore<T> {}
+impl<'a, T: 'a + Score> WriteableScore<'a> for MultiThreadedLockableScore<T> {}
 
 #[cfg(c_bindings)]
 impl<T: Score> MultiThreadedLockableScore<T> {
@@ -250,6 +228,33 @@ impl<T: Score> MultiThreadedLockableScore<T> {
        }
 }
 
+#[cfg(c_bindings)]
+/// A locked `MultiThreadedLockableScore`.
+pub struct MultiThreadedScoreLock<'a, T: Score>(MutexGuard<'a, T>);
+
+#[cfg(c_bindings)]
+impl<'a, T: 'a + Score> Writeable for MultiThreadedScoreLock<'a, T> {
+       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
+               self.0.write(writer)
+       }
+}
+
+#[cfg(c_bindings)]
+impl<'a, T: 'a + Score> DerefMut for MultiThreadedScoreLock<'a, T> {
+    fn deref_mut(&mut self) -> &mut Self::Target {
+        self.0.deref_mut()
+    }
+}
+
+#[cfg(c_bindings)]
+impl<'a, T: 'a + Score> Deref for MultiThreadedScoreLock<'a, T> {
+       type Target = T;
+
+    fn deref(&self) -> &Self::Target {
+        self.0.deref()
+    }
+}
+
 #[cfg(c_bindings)]
 /// This is not exported to bindings users
 impl<'a, T: Writeable> Writeable for RefMut<'a, T> {
@@ -325,7 +330,7 @@ impl ReadableArgs<u64> for FixedPenaltyScorer {
 }
 
 #[cfg(not(feature = "no-std"))]
-type ConfiguredTime = std::time::Instant;
+type ConfiguredTime = crate::util::time::MonotonicTime;
 #[cfg(feature = "no-std")]
 use crate::util::time::Eternity;
 #[cfg(feature = "no-std")]
@@ -1243,8 +1248,10 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> Score for Probabilis
 
                let mut anti_probing_penalty_msat = 0;
                match usage.effective_capacity {
-                       EffectiveCapacity::ExactLiquidity { liquidity_msat } => {
-                               if usage.amount_msat > liquidity_msat {
+                       EffectiveCapacity::ExactLiquidity { liquidity_msat: amount_msat } |
+                               EffectiveCapacity::HintMaxHTLC { amount_msat } =>
+                       {
+                               if usage.amount_msat > amount_msat {
                                        return u64::max_value();
                                } else {
                                        return base_penalty_msat;
@@ -2869,7 +2876,7 @@ mod tests {
                let usage = ChannelUsage {
                        amount_msat: 1,
                        inflight_htlc_msat: 0,
-                       effective_capacity: EffectiveCapacity::MaximumHTLC { amount_msat: 0 },
+                       effective_capacity: EffectiveCapacity::AdvertisedMaxHTLC { amount_msat: 0 },
                };
                assert_eq!(scorer.channel_penalty_msat(42, &target, &source, usage, &params), 2048);
 
index cd898f12b32e41ca7523f79843f4a1ceff3407be..66217de28ac7f7dd15ba9d9ca354c1c06afe9d2b 100644 (file)
@@ -36,7 +36,6 @@ use crate::util::transaction_utils;
 use crate::util::crypto::{hkdf_extract_expand_twice, sign, sign_with_aux_rand};
 use crate::util::ser::{Writeable, Writer, Readable, ReadableArgs};
 use crate::chain::transaction::OutPoint;
-#[cfg(anchors)]
 use crate::events::bump_transaction::HTLCDescriptor;
 use crate::ln::channel::ANCHOR_OUTPUT_VALUE_SATOSHI;
 use crate::ln::{chan_utils, PaymentPreimage};
@@ -49,6 +48,7 @@ use core::convert::TryInto;
 use core::ops::Deref;
 use core::sync::atomic::{AtomicUsize, Ordering};
 use crate::io::{self, Error};
+use crate::ln::features::ChannelTypeFeatures;
 use crate::ln::msgs::{DecodeError, MAX_VALUE_MSAT};
 use crate::util::atomic_counter::AtomicCounter;
 use crate::util::chacha20::ChaCha20;
@@ -488,7 +488,6 @@ pub trait EcdsaChannelSigner: ChannelSigner {
        fn sign_justice_revoked_htlc(&self, justice_tx: &Transaction, input: usize, amount: u64,
                per_commitment_key: &SecretKey, htlc: &HTLCOutputInCommitment,
                secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()>;
-       #[cfg(anchors)]
        /// Computes the signature for a commitment transaction's HTLC output used as an input within
        /// `htlc_tx`, which spends the commitment transaction at index `input`. The signature returned
        /// must be be computed using [`EcdsaSighashType::All`]. Note that this should only be used to
@@ -689,6 +688,7 @@ pub trait SignerProvider {
 ///
 /// This implementation performs no policy checks and is insufficient by itself as
 /// a secure external signer.
+#[derive(Debug)]
 pub struct InMemorySigner {
        /// Holder secret key in the 2-of-2 multisig script of a channel. This key also backs the
        /// holder's anchor output in a commitment transaction, if one is present.
@@ -718,6 +718,21 @@ pub struct InMemorySigner {
        rand_bytes_index: AtomicCounter,
 }
 
+impl PartialEq for InMemorySigner {
+       fn eq(&self, other: &Self) -> bool {
+               self.funding_key == other.funding_key &&
+                       self.revocation_base_key == other.revocation_base_key &&
+                       self.payment_key == other.payment_key &&
+                       self.delayed_payment_base_key == other.delayed_payment_base_key &&
+                       self.htlc_base_key == other.htlc_base_key &&
+                       self.commitment_seed == other.commitment_seed &&
+                       self.holder_channel_pubkeys == other.holder_channel_pubkeys &&
+                       self.channel_parameters == other.channel_parameters &&
+                       self.channel_value_satoshis == other.channel_value_satoshis &&
+                       self.channel_keys_id == other.channel_keys_id
+       }
+}
+
 impl Clone for InMemorySigner {
        fn clone(&self) -> Self {
                Self {
@@ -818,11 +833,12 @@ impl InMemorySigner {
        pub fn get_channel_parameters(&self) -> &ChannelTransactionParameters {
                self.channel_parameters.as_ref().unwrap()
        }
-       /// Returns whether anchors should be used.
+       /// Returns the channel type features of the channel parameters. Should be helpful for
+       /// determining a channel's category, i. e. legacy/anchors/taproot/etc.
        ///
        /// Will panic if [`ChannelSigner::provide_channel_parameters`] has not been called before.
-       pub fn opt_anchors(&self) -> bool {
-               self.get_channel_parameters().opt_anchors.is_some()
+       pub fn channel_type_features(&self) -> &ChannelTypeFeatures {
+               &self.get_channel_parameters().channel_type_features
        }
        /// Sign the single input of `spend_tx` at index `input_idx`, which spends the output described
        /// by `descriptor`, returning the witness stack for the input.
@@ -947,9 +963,9 @@ impl EcdsaChannelSigner for InMemorySigner {
                let mut htlc_sigs = Vec::with_capacity(commitment_tx.htlcs().len());
                for htlc in commitment_tx.htlcs() {
                        let channel_parameters = self.get_channel_parameters();
-                       let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, commitment_tx.feerate_per_kw(), self.holder_selected_contest_delay(), htlc, self.opt_anchors(), channel_parameters.opt_non_zero_fee_anchors.is_some(), &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
-                       let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, self.opt_anchors(), &keys);
-                       let htlc_sighashtype = if self.opt_anchors() { EcdsaSighashType::SinglePlusAnyoneCanPay } else { EcdsaSighashType::All };
+                       let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, commitment_tx.feerate_per_kw(), self.holder_selected_contest_delay(), htlc, &channel_parameters.channel_type_features, &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
+                       let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, self.channel_type_features(), &keys);
+                       let htlc_sighashtype = if self.channel_type_features().supports_anchors_zero_fee_htlc_tx() { EcdsaSighashType::SinglePlusAnyoneCanPay } else { EcdsaSighashType::All };
                        let htlc_sighash = hash_to_message!(&sighash::SighashCache::new(&htlc_tx).segwit_signature_hash(0, &htlc_redeemscript, htlc.amount_msat / 1000, htlc_sighashtype).unwrap()[..]);
                        let holder_htlc_key = chan_utils::derive_private_key(&secp_ctx, &keys.per_commitment_point, &self.htlc_base_key);
                        htlc_sigs.push(sign(secp_ctx, &htlc_sighash, &holder_htlc_key));
@@ -1003,14 +1019,13 @@ impl EcdsaChannelSigner for InMemorySigner {
                let witness_script = {
                        let counterparty_htlcpubkey = chan_utils::derive_public_key(&secp_ctx, &per_commitment_point, &self.counterparty_pubkeys().htlc_basepoint);
                        let holder_htlcpubkey = chan_utils::derive_public_key(&secp_ctx, &per_commitment_point, &self.pubkeys().htlc_basepoint);
-                       chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, self.opt_anchors(), &counterparty_htlcpubkey, &holder_htlcpubkey, &revocation_pubkey)
+                       chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, self.channel_type_features(), &counterparty_htlcpubkey, &holder_htlcpubkey, &revocation_pubkey)
                };
                let mut sighash_parts = sighash::SighashCache::new(justice_tx);
                let sighash = hash_to_message!(&sighash_parts.segwit_signature_hash(input, &witness_script, amount, EcdsaSighashType::All).unwrap()[..]);
                return Ok(sign_with_aux_rand(secp_ctx, &sighash, &revocation_key, &self))
        }
 
-       #[cfg(anchors)]
        fn sign_holder_htlc_transaction(
                &self, htlc_tx: &Transaction, input: usize, htlc_descriptor: &HTLCDescriptor,
                secp_ctx: &Secp256k1<secp256k1::All>
@@ -1033,7 +1048,7 @@ impl EcdsaChannelSigner for InMemorySigner {
                let revocation_pubkey = chan_utils::derive_public_revocation_key(&secp_ctx, &per_commitment_point, &self.pubkeys().revocation_basepoint);
                let counterparty_htlcpubkey = chan_utils::derive_public_key(&secp_ctx, &per_commitment_point, &self.counterparty_pubkeys().htlc_basepoint);
                let htlcpubkey = chan_utils::derive_public_key(&secp_ctx, &per_commitment_point, &self.pubkeys().htlc_basepoint);
-               let witness_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, self.opt_anchors(), &counterparty_htlcpubkey, &htlcpubkey, &revocation_pubkey);
+               let witness_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, self.channel_type_features(), &counterparty_htlcpubkey, &htlcpubkey, &revocation_pubkey);
                let mut sighash_parts = sighash::SighashCache::new(htlc_tx);
                let sighash = hash_to_message!(&sighash_parts.segwit_signature_hash(input, &witness_script, amount, EcdsaSighashType::All).unwrap()[..]);
                Ok(sign_with_aux_rand(secp_ctx, &sighash, &htlc_key, &self))
@@ -1628,8 +1643,8 @@ pub fn dyn_sign() {
        let _signer: Box<dyn EcdsaChannelSigner>;
 }
 
-#[cfg(all(test, feature = "_bench_unstable", not(feature = "no-std")))]
-mod benches {
+#[cfg(ldk_bench)]
+pub mod benches {
        use std::sync::{Arc, mpsc};
        use std::sync::mpsc::TryRecvError;
        use std::thread;
@@ -1638,10 +1653,9 @@ mod benches {
        use bitcoin::Network;
        use crate::sign::{EntropySource, KeysManager};
 
-       use test::Bencher;
+       use criterion::Criterion;
 
-       #[bench]
-       fn bench_get_secure_random_bytes(bench: &mut Bencher) {
+       pub fn bench_get_secure_random_bytes(bench: &mut Criterion) {
                let seed = [0u8; 32];
                let now = Duration::from_secs(genesis_block(Network::Testnet).header.time as u64);
                let keys_manager = Arc::new(KeysManager::new(&seed, now.as_secs(), now.subsec_micros()));
@@ -1667,11 +1681,8 @@ mod benches {
                        stops.push(stop_sender);
                }
 
-               bench.iter(|| {
-                       for _ in 1..100 {
-                               keys_manager.get_secure_random_bytes();
-                       }
-               });
+               bench.bench_function("get_secure_random_bytes", |b| b.iter(||
+                       keys_manager.get_secure_random_bytes()));
 
                for stop in stops {
                        let _ = stop.send(());
@@ -1680,5 +1691,4 @@ mod benches {
                        handle.join().unwrap();
                }
        }
-
 }
index 1b2b9a739b8c5d3078204917f55b0fef86e87f13..348bd90274ae1632b1cec63cf98e9249d9ac7425 100644 (file)
@@ -3,7 +3,7 @@
 pub(crate) enum LockHeldState {
        HeldByThread,
        NotHeldByThread,
-       #[cfg(any(feature = "_bench_unstable", not(test)))]
+       #[cfg(any(ldk_bench, not(test)))]
        Unsupported,
 }
 
@@ -20,20 +20,20 @@ pub(crate) trait LockTestExt<'a> {
        fn unsafe_well_ordered_double_lock_self(&'a self) -> Self::ExclLock;
 }
 
-#[cfg(all(feature = "std", not(feature = "_bench_unstable"), test))]
+#[cfg(all(feature = "std", not(ldk_bench), test))]
 mod debug_sync;
-#[cfg(all(feature = "std", not(feature = "_bench_unstable"), test))]
+#[cfg(all(feature = "std", not(ldk_bench), test))]
 pub use debug_sync::*;
-#[cfg(all(feature = "std", not(feature = "_bench_unstable"), test))]
+#[cfg(all(feature = "std", not(ldk_bench), test))]
 // Note that to make debug_sync's regex work this must not contain `debug_string` in the module name
 mod test_lockorder_checks;
 
-#[cfg(all(feature = "std", any(feature = "_bench_unstable", not(test))))]
+#[cfg(all(feature = "std", any(ldk_bench, not(test))))]
 pub(crate) mod fairrwlock;
-#[cfg(all(feature = "std", any(feature = "_bench_unstable", not(test))))]
+#[cfg(all(feature = "std", any(ldk_bench, not(test))))]
 pub use {std::sync::{Arc, Mutex, Condvar, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard}, fairrwlock::FairRwLock};
 
-#[cfg(all(feature = "std", any(feature = "_bench_unstable", not(test))))]
+#[cfg(all(feature = "std", any(ldk_bench, not(test))))]
 mod ext_impl {
        use super::*;
        impl<'a, T: 'a> LockTestExt<'a> for Mutex<T> {
index 08d54a939be66ecccb83d30c0569d25f31a523e1..27cfb9b8f782c4bafabecd5180a5d0d2256105c3 100644 (file)
@@ -49,7 +49,7 @@ impl<T> Mutex<T> {
 impl<'a, T: 'a> LockTestExt<'a> for Mutex<T> {
        #[inline]
        fn held_by_thread(&self) -> LockHeldState {
-               if self.lock().is_err() { return LockHeldState::HeldByThread; }
+               if self.inner.try_borrow_mut().is_err() { return LockHeldState::HeldByThread; }
                else { return LockHeldState::NotHeldByThread; }
        }
        type ExclLock = MutexGuard<'a, T>;
@@ -115,7 +115,7 @@ impl<T> RwLock<T> {
 impl<'a, T: 'a> LockTestExt<'a> for RwLock<T> {
        #[inline]
        fn held_by_thread(&self) -> LockHeldState {
-               if self.write().is_err() { return LockHeldState::HeldByThread; }
+               if self.inner.try_borrow_mut().is_err() { return LockHeldState::HeldByThread; }
                else { return LockHeldState::NotHeldByThread; }
        }
        type ExclLock = RwLockWriteGuard<'a, T>;
index 81cc1f496c36472d407964fc3a47ef07511fd239..d2e01411313d3c18c6dc7720c7cf68b0d0866e6a 100644 (file)
@@ -5,6 +5,7 @@ compile_error!("We need at least 32-bit pointers for atomic counter (and to have
 
 use core::sync::atomic::{AtomicUsize, Ordering};
 
+#[derive(Debug)]
 pub(crate) struct AtomicCounter {
        // 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:
index 1e678152cccd9d3cfa4bdcc1d40dd441b739913e..267774481b4b98df545112c5f7468721ebb90de6 100644 (file)
@@ -149,11 +149,18 @@ pub struct ChannelHandshakeConfig {
        /// Maximum value: 1,000,000, any values larger than 1 Million will be treated as 1 Million (or 100%)
        ///                instead, although channel negotiations will fail in that case.
        pub their_channel_reserve_proportional_millionths: u32,
-       #[cfg(anchors)]
-       /// If set, we attempt to negotiate the `anchors_zero_fee_htlc_tx`option for outbound channels.
+       /// If set, we attempt to negotiate the `anchors_zero_fee_htlc_tx`option for all future
+       /// channels. This feature requires having a reserve of onchain funds readily available to bump
+       /// transactions in the event of a channel force close to avoid the possibility of losing funds.
+       ///
+       /// Note that if you wish accept inbound channels with anchor outputs, you must enable
+       /// [`UserConfig::manually_accept_inbound_channels`] and manually accept them with
+       /// [`ChannelManager::accept_inbound_channel`]. This is done to give you the chance to check
+       /// whether your reserve of onchain funds is enough to cover the fees for all existing and new
+       /// channels featuring anchor outputs in the event of a force close.
        ///
        /// If this option is set, channels may be created that will not be readable by LDK versions
-       /// prior to 0.0.114, causing [`ChannelManager`]'s read method to return a
+       /// prior to 0.0.116, causing [`ChannelManager`]'s read method to return a
        /// [`DecodeError::InvalidValue`].
        ///
        /// Note that setting this to true does *not* prevent us from opening channels with
@@ -167,6 +174,7 @@ pub struct ChannelHandshakeConfig {
        /// Default value: false. This value is likely to change to true in the future.
        ///
        /// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
+       /// [`ChannelManager::accept_inbound_channel`]: crate::ln::channelmanager::ChannelManager::accept_inbound_channel
        /// [`DecodeError::InvalidValue`]: crate::ln::msgs::DecodeError::InvalidValue
        /// [`SIGHASH_SINGLE + update_fee Considered Harmful`]: https://lists.linuxfoundation.org/pipermail/lightning-dev/2020-September/002796.html
        pub negotiate_anchors_zero_fee_htlc_tx: bool,
@@ -196,7 +204,6 @@ impl Default for ChannelHandshakeConfig {
                        announced_channel: false,
                        commit_upfront_shutdown_pubkey: true,
                        their_channel_reserve_proportional_millionths: 10_000,
-                       #[cfg(anchors)]
                        negotiate_anchors_zero_fee_htlc_tx: false,
                        our_max_accepted_htlcs: 50,
                }
@@ -308,6 +315,55 @@ impl Default for ChannelHandshakeLimits {
        }
 }
 
+/// Options for how to set the max dust HTLC exposure allowed on a channel. See
+/// [`ChannelConfig::max_dust_htlc_exposure`] for details.
+#[derive(Copy, Clone, Debug, PartialEq, Eq)]
+pub enum MaxDustHTLCExposure {
+       /// This sets a fixed limit on the total dust exposure in millisatoshis. Setting this too low
+       /// may prevent the sending or receipt of low-value HTLCs on high-traffic nodes, however this
+       /// limit is very important to prevent stealing of large amounts of dust HTLCs by miners
+       /// through [fee griefing
+       /// attacks](https://lists.linuxfoundation.org/pipermail/lightning-dev/2020-May/002714.html).
+       ///
+       /// Note that if the feerate increases significantly, without a manual increase
+       /// to this maximum the channel may be unable to send/receive HTLCs between the maximum dust
+       /// exposure and the new minimum value for HTLCs to be economically viable to claim.
+       FixedLimitMsat(u64),
+       /// This sets a multiplier on the estimated high priority feerate (sats/KW, as obtained from
+       /// [`FeeEstimator`]) to determine the maximum allowed dust exposure. If this variant is used
+       /// then the maximum dust exposure in millisatoshis is calculated as:
+       /// `high_priority_feerate_per_kw * value`. For example, with our default value
+       /// `FeeRateMultiplier(5000)`:
+       ///
+       /// - For the minimum fee rate of 1 sat/vByte (250 sat/KW, although the minimum
+       /// defaults to 253 sats/KW for rounding, see [`FeeEstimator`]), the max dust exposure would
+       /// be 253 * 5000 = 1,265,000 msats.
+       /// - For a fee rate of 30 sat/vByte (7500 sat/KW), the max dust exposure would be
+       /// 7500 * 5000 = 37,500,000 msats.
+       ///
+       /// This allows the maximum dust exposure to automatically scale with fee rate changes.
+       ///
+       /// Note, if you're using a third-party fee estimator, this may leave you more exposed to a
+       /// fee griefing attack, where your fee estimator may purposely overestimate the fee rate,
+       /// causing you to accept more dust HTLCs than you would otherwise.
+       ///
+       /// This variant is primarily meant to serve pre-anchor channels, as HTLC fees being included
+       /// on HTLC outputs means your channel may be subject to more dust exposure in the event of
+       /// increases in fee rate.
+       ///
+       /// # Backwards Compatibility
+       /// This variant only became available in LDK 0.0.116, so if you downgrade to a prior version
+       /// by default this will be set to a [`Self::FixedLimitMsat`] of 5,000,000 msat.
+       ///
+       /// [`FeeEstimator`]: crate::chain::chaininterface::FeeEstimator
+       FeeRateMultiplier(u64),
+}
+
+impl_writeable_tlv_based_enum!(MaxDustHTLCExposure, ;
+       (1, FixedLimitMsat),
+       (3, FeeRateMultiplier),
+);
+
 /// Options which apply on a per-channel basis and may change at runtime or based on negotiation
 /// with our counterparty.
 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
@@ -365,15 +421,15 @@ pub struct ChannelConfig {
        /// channel negotiated throughout the channel open process, along with the fees required to have
        /// a broadcastable HTLC spending transaction. When a channel supports anchor outputs
        /// (specifically the zero fee HTLC transaction variant), this threshold no longer takes into
-       /// account the HTLC transaction fee as it is zero.
+       /// account the HTLC transaction fee as it is zero. Because of this, you may want to set this
+       /// value to a fixed limit for channels using anchor outputs, while the fee rate multiplier
+       /// variant is primarily intended for use with pre-anchor channels.
        ///
-       /// This limit is applied for sent, forwarded, and received HTLCs and limits the total
-       /// exposure across all three types per-channel. Setting this too low may prevent the
-       /// sending or receipt of low-value HTLCs on high-traffic nodes, and this limit is very
-       /// important to prevent stealing of dust HTLCs by miners.
+       /// The selected limit is applied for sent, forwarded, and received HTLCs and limits the total
+       /// exposure across all three types per-channel.
        ///
-       /// Default value: 5_000_000 msat.
-       pub max_dust_htlc_exposure_msat: u64,
+       /// Default value: [`MaxDustHTLCExposure::FeeRateMultiplier`] with a multiplier of 5000.
+       pub max_dust_htlc_exposure: MaxDustHTLCExposure,
        /// The additional fee we're willing to pay to avoid waiting for the counterparty's
        /// `to_self_delay` to reclaim funds.
        ///
@@ -397,6 +453,59 @@ pub struct ChannelConfig {
        /// [`Normal`]: crate::chain::chaininterface::ConfirmationTarget::Normal
        /// [`Background`]: crate::chain::chaininterface::ConfirmationTarget::Background
        pub force_close_avoidance_max_fee_satoshis: u64,
+       /// If set, allows this channel's counterparty to skim an additional fee off this node's inbound
+       /// HTLCs. Useful for liquidity providers to offload on-chain channel costs to end users.
+       ///
+       /// Usage:
+       /// - The payee will set this option and set its invoice route hints to use [intercept scids]
+       ///   generated by this channel's counterparty.
+       /// - The counterparty will get an [`HTLCIntercepted`] event upon payment forward, and call
+       ///   [`forward_intercepted_htlc`] with less than the amount provided in
+       ///   [`HTLCIntercepted::expected_outbound_amount_msat`]. The difference between the expected and
+       ///   actual forward amounts is their fee.
+       // TODO: link to LSP JIT channel invoice generation spec when it's merged
+       ///
+       /// # Note
+       /// It's important for payee wallet software to verify that [`PaymentClaimable::amount_msat`] is
+       /// as-expected if this feature is activated, otherwise they may lose money!
+       /// [`PaymentClaimable::counterparty_skimmed_fee_msat`] provides the fee taken by the
+       /// counterparty.
+       ///
+       /// # Note
+       /// Switching this config flag on may break compatibility with versions of LDK prior to 0.0.116.
+       /// Unsetting this flag between restarts may lead to payment receive failures.
+       ///
+       /// Default value: false.
+       ///
+       /// [intercept scids]: crate::ln::channelmanager::ChannelManager::get_intercept_scid
+       /// [`forward_intercepted_htlc`]: crate::ln::channelmanager::ChannelManager::forward_intercepted_htlc
+       /// [`HTLCIntercepted`]: crate::events::Event::HTLCIntercepted
+       /// [`HTLCIntercepted::expected_outbound_amount_msat`]: crate::events::Event::HTLCIntercepted::expected_outbound_amount_msat
+       /// [`PaymentClaimable::amount_msat`]: crate::events::Event::PaymentClaimable::amount_msat
+       /// [`PaymentClaimable::counterparty_skimmed_fee_msat`]: crate::events::Event::PaymentClaimable::counterparty_skimmed_fee_msat
+       //  TODO: link to bLIP when it's merged
+       pub accept_underpaying_htlcs: bool,
+}
+
+impl ChannelConfig {
+       /// Applies the given [`ChannelConfigUpdate`] as a partial update to the [`ChannelConfig`].
+       pub fn apply(&mut self, update: &ChannelConfigUpdate) {
+               if let Some(forwarding_fee_proportional_millionths) = update.forwarding_fee_proportional_millionths {
+                       self.forwarding_fee_proportional_millionths = forwarding_fee_proportional_millionths;
+               }
+               if let Some(forwarding_fee_base_msat) = update.forwarding_fee_base_msat {
+                       self.forwarding_fee_base_msat = forwarding_fee_base_msat;
+               }
+               if let Some(cltv_expiry_delta) = update.cltv_expiry_delta {
+                       self.cltv_expiry_delta = cltv_expiry_delta;
+               }
+               if let Some(max_dust_htlc_exposure_msat) = update.max_dust_htlc_exposure_msat {
+                       self.max_dust_htlc_exposure = max_dust_htlc_exposure_msat;
+               }
+               if let Some(force_close_avoidance_max_fee_satoshis) = update.force_close_avoidance_max_fee_satoshis {
+                       self.force_close_avoidance_max_fee_satoshis = force_close_avoidance_max_fee_satoshis;
+               }
+       }
 }
 
 impl Default for ChannelConfig {
@@ -406,22 +515,101 @@ impl Default for ChannelConfig {
                        forwarding_fee_proportional_millionths: 0,
                        forwarding_fee_base_msat: 1000,
                        cltv_expiry_delta: 6 * 12, // 6 blocks/hour * 12 hours
-                       max_dust_htlc_exposure_msat: 5_000_000,
+                       max_dust_htlc_exposure: MaxDustHTLCExposure::FeeRateMultiplier(5000),
                        force_close_avoidance_max_fee_satoshis: 1000,
+                       accept_underpaying_htlcs: false,
                }
        }
 }
 
-impl_writeable_tlv_based!(ChannelConfig, {
-       (0, forwarding_fee_proportional_millionths, required),
-       (2, forwarding_fee_base_msat, required),
-       (4, cltv_expiry_delta, required),
-       (6, max_dust_htlc_exposure_msat, required),
-       // ChannelConfig serialized this field with a required type of 8 prior to the introduction of
-       // LegacyChannelConfig. To make sure that serialization is not compatible with this one, we use
-       // the next required type of 10, which if seen by the old serialization will always fail.
-       (10, force_close_avoidance_max_fee_satoshis, required),
-});
+impl crate::util::ser::Writeable for ChannelConfig {
+       fn write<W: crate::util::ser::Writer>(&self, writer: &mut W) -> Result<(), crate::io::Error> {
+               let max_dust_htlc_exposure_msat_fixed_limit = match self.max_dust_htlc_exposure {
+                       MaxDustHTLCExposure::FixedLimitMsat(limit) => limit,
+                       MaxDustHTLCExposure::FeeRateMultiplier(_) => 5_000_000,
+               };
+               write_tlv_fields!(writer, {
+                       (0, self.forwarding_fee_proportional_millionths, required),
+                       (1, self.accept_underpaying_htlcs, (default_value, false)),
+                       (2, self.forwarding_fee_base_msat, required),
+                       (3, self.max_dust_htlc_exposure, required),
+                       (4, self.cltv_expiry_delta, required),
+                       (6, max_dust_htlc_exposure_msat_fixed_limit, required),
+                       // ChannelConfig serialized this field with a required type of 8 prior to the introduction of
+                       // LegacyChannelConfig. To make sure that serialization is not compatible with this one, we use
+                       // the next required type of 10, which if seen by the old serialization will always fail.
+                       (10, self.force_close_avoidance_max_fee_satoshis, required),
+               });
+               Ok(())
+       }
+}
+
+impl crate::util::ser::Readable for ChannelConfig {
+       fn read<R: crate::io::Read>(reader: &mut R) -> Result<Self, crate::ln::msgs::DecodeError> {
+               let mut forwarding_fee_proportional_millionths = 0;
+               let mut accept_underpaying_htlcs = false;
+               let mut forwarding_fee_base_msat = 1000;
+               let mut cltv_expiry_delta = 6 * 12;
+               let mut max_dust_htlc_exposure_msat = None;
+               let mut max_dust_htlc_exposure_enum = None;
+               let mut force_close_avoidance_max_fee_satoshis = 1000;
+               read_tlv_fields!(reader, {
+                       (0, forwarding_fee_proportional_millionths, required),
+                       (1, accept_underpaying_htlcs, (default_value, false)),
+                       (2, forwarding_fee_base_msat, required),
+                       (3, max_dust_htlc_exposure_enum, option),
+                       (4, cltv_expiry_delta, required),
+                       // Has always been written, but became optionally read in 0.0.116
+                       (6, max_dust_htlc_exposure_msat, option),
+                       (10, force_close_avoidance_max_fee_satoshis, required),
+               });
+               let max_dust_htlc_fixed_limit = max_dust_htlc_exposure_msat.unwrap_or(5_000_000);
+               let max_dust_htlc_exposure_msat = max_dust_htlc_exposure_enum
+                       .unwrap_or(MaxDustHTLCExposure::FixedLimitMsat(max_dust_htlc_fixed_limit));
+               Ok(Self {
+                       forwarding_fee_proportional_millionths,
+                       accept_underpaying_htlcs,
+                       forwarding_fee_base_msat,
+                       cltv_expiry_delta,
+                       max_dust_htlc_exposure: max_dust_htlc_exposure_msat,
+                       force_close_avoidance_max_fee_satoshis,
+               })
+       }
+}
+
+/// A parallel struct to [`ChannelConfig`] to define partial updates.
+#[allow(missing_docs)]
+pub struct ChannelConfigUpdate {
+       pub forwarding_fee_proportional_millionths: Option<u32>,
+       pub forwarding_fee_base_msat: Option<u32>,
+       pub cltv_expiry_delta: Option<u16>,
+       pub max_dust_htlc_exposure_msat: Option<MaxDustHTLCExposure>,
+       pub force_close_avoidance_max_fee_satoshis: Option<u64>,
+}
+
+impl Default for ChannelConfigUpdate {
+       fn default() -> ChannelConfigUpdate {
+               ChannelConfigUpdate {
+                       forwarding_fee_proportional_millionths: None,
+                       forwarding_fee_base_msat: None,
+                       cltv_expiry_delta: None,
+                       max_dust_htlc_exposure_msat: None,
+                       force_close_avoidance_max_fee_satoshis: None,
+               }
+       }
+}
+
+impl From<ChannelConfig> for ChannelConfigUpdate {
+       fn from(config: ChannelConfig) -> ChannelConfigUpdate {
+               ChannelConfigUpdate {
+                       forwarding_fee_proportional_millionths: Some(config.forwarding_fee_proportional_millionths),
+                       forwarding_fee_base_msat: Some(config.forwarding_fee_base_msat),
+                       cltv_expiry_delta: Some(config.cltv_expiry_delta),
+                       max_dust_htlc_exposure_msat: Some(config.max_dust_htlc_exposure),
+                       force_close_avoidance_max_fee_satoshis: Some(config.force_close_avoidance_max_fee_satoshis),
+               }
+       }
+}
 
 /// Legacy version of [`ChannelConfig`] that stored the static
 /// [`ChannelHandshakeConfig::announced_channel`] and
@@ -450,12 +638,17 @@ impl Default for LegacyChannelConfig {
 
 impl crate::util::ser::Writeable for LegacyChannelConfig {
        fn write<W: crate::util::ser::Writer>(&self, writer: &mut W) -> Result<(), crate::io::Error> {
+               let max_dust_htlc_exposure_msat_fixed_limit = match self.options.max_dust_htlc_exposure {
+                       MaxDustHTLCExposure::FixedLimitMsat(limit) => limit,
+                       MaxDustHTLCExposure::FeeRateMultiplier(_) => 5_000_000,
+               };
                write_tlv_fields!(writer, {
                        (0, self.options.forwarding_fee_proportional_millionths, required),
-                       (1, self.options.max_dust_htlc_exposure_msat, (default_value, 5_000_000)),
+                       (1, max_dust_htlc_exposure_msat_fixed_limit, required),
                        (2, self.options.cltv_expiry_delta, required),
                        (3, self.options.force_close_avoidance_max_fee_satoshis, (default_value, 1000)),
                        (4, self.announced_channel, required),
+                       (5, self.options.max_dust_htlc_exposure, required),
                        (6, self.commit_upfront_shutdown_pubkey, required),
                        (8, self.options.forwarding_fee_base_msat, required),
                });
@@ -466,28 +659,36 @@ impl crate::util::ser::Writeable for LegacyChannelConfig {
 impl crate::util::ser::Readable for LegacyChannelConfig {
        fn read<R: crate::io::Read>(reader: &mut R) -> Result<Self, crate::ln::msgs::DecodeError> {
                let mut forwarding_fee_proportional_millionths = 0;
-               let mut max_dust_htlc_exposure_msat = 5_000_000;
+               let mut max_dust_htlc_exposure_msat_fixed_limit = None;
                let mut cltv_expiry_delta = 0;
                let mut force_close_avoidance_max_fee_satoshis = 1000;
                let mut announced_channel = false;
                let mut commit_upfront_shutdown_pubkey = false;
                let mut forwarding_fee_base_msat = 0;
+               let mut max_dust_htlc_exposure_enum = None;
                read_tlv_fields!(reader, {
                        (0, forwarding_fee_proportional_millionths, required),
-                       (1, max_dust_htlc_exposure_msat, (default_value, 5_000_000u64)),
+                       // Has always been written, but became optionally read in 0.0.116
+                       (1, max_dust_htlc_exposure_msat_fixed_limit, option),
                        (2, cltv_expiry_delta, required),
                        (3, force_close_avoidance_max_fee_satoshis, (default_value, 1000u64)),
                        (4, announced_channel, required),
+                       (5, max_dust_htlc_exposure_enum, option),
                        (6, commit_upfront_shutdown_pubkey, required),
                        (8, forwarding_fee_base_msat, required),
                });
+               let max_dust_htlc_exposure_msat_fixed_limit =
+                       max_dust_htlc_exposure_msat_fixed_limit.unwrap_or(5_000_000);
+               let max_dust_htlc_exposure_msat = max_dust_htlc_exposure_enum
+                       .unwrap_or(MaxDustHTLCExposure::FixedLimitMsat(max_dust_htlc_exposure_msat_fixed_limit));
                Ok(Self {
                        options: ChannelConfig {
                                forwarding_fee_proportional_millionths,
-                               max_dust_htlc_exposure_msat,
+                               max_dust_htlc_exposure: max_dust_htlc_exposure_msat,
                                cltv_expiry_delta,
                                force_close_avoidance_max_fee_satoshis,
                                forwarding_fee_base_msat,
+                               accept_underpaying_htlcs: false,
                        },
                        announced_channel,
                        commit_upfront_shutdown_pubkey,
@@ -552,6 +753,17 @@ pub struct UserConfig {
        /// [`ChannelManager::get_intercept_scid`]: crate::ln::channelmanager::ChannelManager::get_intercept_scid
        /// [`Event::HTLCIntercepted`]: crate::events::Event::HTLCIntercepted
        pub accept_intercept_htlcs: bool,
+       /// If this is set to false, when receiving a keysend payment we'll fail it if it has multiple
+       /// parts. If this is set to true, we'll accept the payment.
+       ///
+       /// Setting this to true will break backwards compatibility upon downgrading to an LDK
+       /// version < 0.0.116 while receiving an MPP keysend. If we have already received an MPP
+       /// keysend, downgrading will cause us to fail to deserialize [`ChannelManager`].
+       ///
+       /// Default value: false.
+       ///
+       /// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
+       pub accept_mpp_keysend: bool,
 }
 
 impl Default for UserConfig {
@@ -564,6 +776,7 @@ impl Default for UserConfig {
                        accept_inbound_channels: true,
                        manually_accept_inbound_channels: false,
                        accept_intercept_htlcs: false,
+                       accept_mpp_keysend: false,
                }
        }
 }
index b96a02afe4b12613cd5f74de774cb87068d19732..a9a6e8e09333efc9b7cf2feef27184684ddea709 100644 (file)
@@ -23,10 +23,10 @@ use bitcoin::util::sighash;
 use bitcoin::secp256k1;
 use bitcoin::secp256k1::{SecretKey, PublicKey};
 use bitcoin::secp256k1::{Secp256k1, ecdsa::Signature};
-#[cfg(anchors)]
 use crate::events::bump_transaction::HTLCDescriptor;
 use crate::util::ser::{Writeable, Writer};
 use crate::io::Error;
+use crate::ln::features::ChannelTypeFeatures;
 
 /// Initial value for revoked commitment downward counter
 pub const INITIAL_REVOKED_COMMITMENT_NUMBER: u64 = 1 << 48;
@@ -88,7 +88,7 @@ impl EnforcingSigner {
                }
        }
 
-       pub fn opt_anchors(&self) -> bool { self.inner.opt_anchors() }
+       pub fn channel_type_features(&self) -> &ChannelTypeFeatures { self.inner.channel_type_features() }
 
        #[cfg(test)]
        pub fn get_enforcement_state(&self) -> MutexGuard<EnforcementState> {
@@ -172,11 +172,11 @@ impl EcdsaChannelSigner for EnforcingSigner {
                for (this_htlc, sig) in trusted_tx.htlcs().iter().zip(&commitment_tx.counterparty_htlc_sigs) {
                        assert!(this_htlc.transaction_output_index.is_some());
                        let keys = trusted_tx.keys();
-                       let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, trusted_tx.feerate_per_kw(), holder_csv, &this_htlc, self.opt_anchors(), false, &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
+                       let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, trusted_tx.feerate_per_kw(), holder_csv, &this_htlc, self.channel_type_features(), &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
 
-                       let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&this_htlc, self.opt_anchors(), &keys);
+                       let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&this_htlc, self.channel_type_features(), &keys);
 
-                       let sighash_type = if self.opt_anchors() {
+                       let sighash_type = if self.channel_type_features().supports_anchors_zero_fee_htlc_tx() {
                                EcdsaSighashType::SinglePlusAnyoneCanPay
                        } else {
                                EcdsaSighashType::All
@@ -205,7 +205,6 @@ impl EcdsaChannelSigner for EnforcingSigner {
                Ok(self.inner.sign_justice_revoked_htlc(justice_tx, input, amount, per_commitment_key, htlc, secp_ctx).unwrap())
        }
 
-       #[cfg(anchors)]
        fn sign_holder_htlc_transaction(
                &self, htlc_tx: &Transaction, input: usize, htlc_descriptor: &HTLCDescriptor,
                secp_ctx: &Secp256k1<secp256k1::All>
index 3dbf4f89634b9b2c7330f774b8f0bd624414dd2c..dd9d2744e1c7927ba82bab9244a355020b5d9660 100644 (file)
@@ -50,11 +50,11 @@ pub(crate) mod crypto;
 pub mod logger;
 pub mod config;
 
-#[cfg(any(test, fuzzing, feature = "_test_utils"))]
+#[cfg(any(test, feature = "_test_utils"))]
 pub mod test_utils;
 
 /// impls of traits that add exra enforcement on the way they're called. Useful for detecting state
 /// machine errors and used in fuzz targets and tests.
-#[cfg(any(test, fuzzing, feature = "_test_utils"))]
+#[cfg(any(test, feature = "_test_utils"))]
 pub mod enforcing_trait_impls;
 
index cbdb5485e7bd984312c5a139e220835769f8f73b..0e510760e62a3725055cecf3394a17b5b879af9a 100644 (file)
@@ -37,12 +37,14 @@ use bitcoin::hashes::sha256d::Hash as Sha256dHash;
 use bitcoin::hash_types::{Txid, BlockHash};
 use core::marker::Sized;
 use core::time::Duration;
+use crate::chain::ClaimId;
 use crate::ln::msgs::DecodeError;
 #[cfg(taproot)]
 use crate::ln::msgs::PartialSignatureWithNonce;
 use crate::ln::{PaymentPreimage, PaymentHash, PaymentSecret};
 
 use crate::util::byte_utils::{be48_to_array, slice_to_be48};
+use crate::util::string::UntrustedString;
 
 /// serialization buffer size
 pub const MAX_BUF_SIZE: usize = 64 * 1024;
@@ -629,6 +631,21 @@ impl<'a> From<&'a String> for WithoutLength<&'a String> {
        fn from(s: &'a String) -> Self { Self(s) }
 }
 
+
+impl Writeable for WithoutLength<&UntrustedString> {
+       #[inline]
+       fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
+               WithoutLength(&self.0.0).write(w)
+       }
+}
+impl Readable for WithoutLength<UntrustedString> {
+       #[inline]
+       fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
+               let s: WithoutLength<String> = Readable::read(r)?;
+               Ok(Self(UntrustedString(s.0)))
+       }
+}
+
 impl<'a, T: Writeable> Writeable for WithoutLength<&'a Vec<T>> {
        #[inline]
        fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
@@ -831,6 +848,7 @@ impl Readable for Vec<u8> {
 }
 
 impl_for_vec!(ecdsa::Signature);
+impl_for_vec!(crate::chain::channelmonitor::ChannelMonitorUpdate);
 impl_for_vec!(crate::ln::channelmanager::MonitorUpdateCompletionAction);
 impl_for_vec!((A, B), A, B);
 impl_writeable_for_vec!(&crate::routing::router::BlindedTail);
@@ -1393,6 +1411,18 @@ impl Readable for TransactionU16LenLimited {
        }
 }
 
+impl Writeable for ClaimId {
+       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
+               self.0.write(writer)
+       }
+}
+
+impl Readable for ClaimId {
+       fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
+               Ok(Self(Readable::read(reader)?))
+       }
+}
+
 #[cfg(test)]
 mod tests {
        use core::convert::TryFrom;
index d6a03a88fbe827efc72f6fcd96aa1e928aaa2c17..742ea25714dafb4d291f249e30376366cfeebbaa 100644 (file)
@@ -178,6 +178,9 @@ macro_rules! _get_varint_length_prefixed_tlv_length {
        ($len: expr, $type: expr, $field: expr, (option: $trait: ident $(, $read_arg: expr)?)) => {
                $crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, option);
        };
+       ($len: expr, $type: expr, $field: expr, (option, encoding: ($fieldty: ty, $encoding: ident))) => {
+               $crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field.map(|f| $encoding(f)), option);
+       };
        ($len: expr, $type: expr, $field: expr, upgradable_required) => {
                $crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, required);
        };
@@ -978,7 +981,7 @@ macro_rules! impl_writeable_tlv_based_enum {
                                                f()
                                        }),*
                                        $($tuple_variant_id => {
-                                               Ok($st::$tuple_variant_name(Readable::read(reader)?))
+                                               Ok($st::$tuple_variant_name($crate::util::ser::Readable::read(reader)?))
                                        }),*
                                        _ => {
                                                Err($crate::ln::msgs::DecodeError::UnknownRequiredFeature)
index e51bbb9271983986650ac31fef823473ee498f0a..212f1f4b60a392f6ad605fe599f200b3c543b778 100644 (file)
@@ -32,6 +32,7 @@ use crate::util::enforcing_trait_impls::{EnforcingSigner, EnforcementState};
 use crate::util::logger::{Logger, Level, Record};
 use crate::util::ser::{Readable, ReadableArgs, Writer, Writeable};
 
+use bitcoin::blockdata::constants::ChainHash;
 use bitcoin::blockdata::constants::genesis_block;
 use bitcoin::blockdata::transaction::{Transaction, TxOut};
 use bitcoin::blockdata::script::{Builder, Script};
@@ -44,11 +45,13 @@ use bitcoin::secp256k1::{SecretKey, PublicKey, Secp256k1, ecdsa::Signature, Scal
 use bitcoin::secp256k1::ecdh::SharedSecret;
 use bitcoin::secp256k1::ecdsa::RecoverableSignature;
 
+#[cfg(any(test, feature = "_test_utils"))]
 use regex;
 
 use crate::io;
 use crate::prelude::*;
 use core::cell::RefCell;
+use core::ops::DerefMut;
 use core::time::Duration;
 use crate::sync::{Mutex, Arc};
 use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
@@ -111,8 +114,8 @@ impl<'a> Router for TestRouter<'a> {
                if let Some((find_route_query, find_route_res)) = self.next_routes.lock().unwrap().pop_front() {
                        assert_eq!(find_route_query, *params);
                        if let Ok(ref route) = find_route_res {
-                               let locked_scorer = self.scorer.lock().unwrap();
-                               let scorer = ScorerAccountingForInFlightHtlcs::new(locked_scorer, inflight_htlcs);
+                               let mut binding = self.scorer.lock().unwrap();
+                               let scorer = ScorerAccountingForInFlightHtlcs::new(binding.deref_mut(), inflight_htlcs);
                                for path in &route.paths {
                                        let mut aggregate_msat = 0u64;
                                        for (idx, hop) in path.hops.iter().rev().enumerate() {
@@ -137,10 +140,9 @@ impl<'a> Router for TestRouter<'a> {
                        return find_route_res;
                }
                let logger = TestLogger::new();
-               let scorer = self.scorer.lock().unwrap();
                find_route(
                        payer, params, &self.network_graph, first_hops, &logger,
-                       &ScorerAccountingForInFlightHtlcs::new(scorer, &inflight_htlcs), &(),
+                       &ScorerAccountingForInFlightHtlcs::new(self.scorer.lock().unwrap().deref_mut(), &inflight_htlcs), &(),
                        &[42; 32]
                )
        }
@@ -341,17 +343,20 @@ impl TestBroadcaster {
 }
 
 impl chaininterface::BroadcasterInterface for TestBroadcaster {
-       fn broadcast_transaction(&self, tx: &Transaction) {
-               let lock_time = tx.lock_time.0;
-               assert!(lock_time < 1_500_000_000);
-               if bitcoin::LockTime::from(tx.lock_time).is_block_height() && lock_time > self.blocks.lock().unwrap().last().unwrap().1 {
-                       for inp in tx.input.iter() {
-                               if inp.sequence != Sequence::MAX {
-                                       panic!("We should never broadcast a transaction before its locktime ({})!", tx.lock_time);
+       fn broadcast_transactions(&self, txs: &[&Transaction]) {
+               for tx in txs {
+                       let lock_time = tx.lock_time.0;
+                       assert!(lock_time < 1_500_000_000);
+                       if bitcoin::LockTime::from(tx.lock_time).is_block_height() && lock_time > self.blocks.lock().unwrap().last().unwrap().1 {
+                               for inp in tx.input.iter() {
+                                       if inp.sequence != Sequence::MAX {
+                                               panic!("We should never broadcast a transaction before its locktime ({})!", tx.lock_time);
+                                       }
                                }
                        }
                }
-               self.txn_broadcasted.lock().unwrap().push(tx.clone());
+               let owned_txs: Vec<Transaction> = txs.iter().map(|tx| (*tx).clone()).collect();
+               self.txn_broadcasted.lock().unwrap().extend(owned_txs);
        }
 }
 
@@ -359,14 +364,18 @@ pub struct TestChannelMessageHandler {
        pub pending_events: Mutex<Vec<events::MessageSendEvent>>,
        expected_recv_msgs: Mutex<Option<Vec<wire::Message<()>>>>,
        connected_peers: Mutex<HashSet<PublicKey>>,
+       pub message_fetch_counter: AtomicUsize,
+       genesis_hash: ChainHash,
 }
 
 impl TestChannelMessageHandler {
-       pub fn new() -> Self {
+       pub fn new(genesis_hash: ChainHash) -> Self {
                TestChannelMessageHandler {
                        pending_events: Mutex::new(Vec::new()),
                        expected_recv_msgs: Mutex::new(None),
                        connected_peers: Mutex::new(HashSet::new()),
+                       message_fetch_counter: AtomicUsize::new(0),
+                       genesis_hash,
                }
        }
 
@@ -470,6 +479,10 @@ impl msgs::ChannelMessageHandler for TestChannelMessageHandler {
                channelmanager::provided_init_features(&UserConfig::default())
        }
 
+       fn get_genesis_hashes(&self) -> Option<Vec<ChainHash>> {
+               Some(vec![self.genesis_hash])
+       }
+
        fn handle_open_channel_v2(&self, _their_node_id: &PublicKey, msg: &msgs::OpenChannelV2) {
                self.received_msg(wire::Message::OpenChannelV2(msg.clone()));
        }
@@ -517,6 +530,7 @@ impl msgs::ChannelMessageHandler for TestChannelMessageHandler {
 
 impl events::MessageSendEventsProvider for TestChannelMessageHandler {
        fn get_and_clear_pending_msg_events(&self) -> Vec<events::MessageSendEvent> {
+               self.message_fetch_counter.fetch_add(1, Ordering::AcqRel);
                let mut pending_events = self.pending_events.lock().unwrap();
                let mut ret = Vec::new();
                mem::swap(&mut ret, &mut *pending_events);
@@ -725,6 +739,7 @@ impl TestLogger {
        /// 1. belong to the specified module and
        /// 2. match the given regex pattern.
        /// Assert that the number of occurrences equals the given `count`
+       #[cfg(any(test, feature = "_test_utils"))]
        pub fn assert_log_regex(&self, module: &str, pattern: regex::Regex, count: usize) {
                let log_entries = self.lines.lock().unwrap();
                let l: usize = log_entries.iter().filter(|&(&(ref m, ref l), _c)| {
@@ -738,7 +753,7 @@ impl Logger for TestLogger {
        fn log(&self, record: &Record) {
                *self.lines.lock().unwrap().entry((record.module_path.to_string(), format!("{}", record.args))).or_insert(0) += 1;
                if record.level >= self.level {
-                       #[cfg(feature = "std")]
+                       #[cfg(all(not(ldk_bench), feature = "std"))]
                        println!("{:<5} {} [{} : {}, {}] {}", record.level.to_string(), self.id, record.module_path, record.file, record.line, record.args);
                }
        }
index f450dc2c3015ae4da00faa2d609d01542753b85d..0d969e7470952625d8db04965220c25df589f30f 100644 (file)
@@ -58,10 +58,20 @@ impl Sub<Duration> for Eternity {
        }
 }
 
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+#[cfg(not(feature = "no-std"))]
+pub struct MonotonicTime(std::time::Instant);
+
+/// The amount of time to shift `Instant` forward to prevent overflow when subtracting a `Duration`
+/// from `Instant::now` on some operating systems (e.g., iOS representing `Instance` as `u64`).
+#[cfg(not(feature = "no-std"))]
+const SHIFT: Duration = Duration::from_secs(10 * 365 * 24 * 60 * 60); // 10 years.
+
 #[cfg(not(feature = "no-std"))]
-impl Time for std::time::Instant {
+impl Time for MonotonicTime {
        fn now() -> Self {
-               std::time::Instant::now()
+               let instant = std::time::Instant::now().checked_add(SHIFT).expect("Overflow on MonotonicTime instantiation");
+               Self(instant)
        }
 
        fn duration_since(&self, earlier: Self) -> Duration {
@@ -70,15 +80,26 @@ impl Time for std::time::Instant {
                // clocks" that go backwards in practice (likely relatively ancient kernels/etc). Thus, we
                // manually check for time going backwards here and return a duration of zero in that case.
                let now = Self::now();
-               if now > earlier { now - earlier } else { Duration::from_secs(0) }
+               if now.0 > earlier.0 { now.0 - earlier.0 } else { Duration::from_secs(0) }
        }
 
        fn duration_since_epoch() -> Duration {
                use std::time::SystemTime;
                SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap()
        }
+
        fn elapsed(&self) -> Duration {
-               std::time::Instant::elapsed(self)
+               Self::now().0 - self.0
+       }
+}
+
+#[cfg(not(feature = "no-std"))]
+impl Sub<Duration> for MonotonicTime {
+       type Output = Self;
+
+       fn sub(self, other: Duration) -> Self {
+               let instant = self.0.checked_sub(other).expect("MonotonicTime is not supposed to go backward futher than 10 years");
+               Self(instant)
        }
 }
 
@@ -154,4 +175,15 @@ pub mod tests {
                assert_eq!(now.elapsed(), Duration::from_secs(0));
                assert_eq!(later - elapsed, now);
        }
+
+       #[test]
+       #[cfg(not(feature = "no-std"))]
+       fn monotonic_time_subtracts() {
+               let now = super::MonotonicTime::now();
+               assert!(now.elapsed() < Duration::from_secs(10));
+
+               let ten_years = Duration::from_secs(10 * 365 * 24 * 60 * 60);
+               let past = now - ten_years;
+               assert!(past.elapsed() >= ten_years);
+       }
 }
diff --git a/pending_changelog/forward-underpaying-htlc.txt b/pending_changelog/forward-underpaying-htlc.txt
new file mode 100644 (file)
index 0000000..5b6c223
--- /dev/null
@@ -0,0 +1,6 @@
+## Backwards Compat
+
+* Forwarding less than the expected amount in `ChannelManager::forward_intercepted_htlc` may break
+       compatibility with versions of LDK prior to 0.0.116
+* Setting `ChannelConfig::accept_underpaying_htlcs` may break compatibility with versions of LDK
+       prior to 0.0.116, and unsetting the feature between restarts may lead to payment failures.
diff --git a/pending_changelog/no-legacy-payments.txt b/pending_changelog/no-legacy-payments.txt
new file mode 100644 (file)
index 0000000..8ca3aef
--- /dev/null
@@ -0,0 +1,6 @@
+ * Legacy inbound payment creation has been removed, thus there is no way to
+   create a pending inbound payment which will still be claimable on LDK
+   0.0.103 or earlier. Support for claiming such payments is still retained,
+   however is likely to be removed in the next release (#2351).
+ * Some fields required in 0.0.103 and earlier are no longer written, thus
+   deserializing objects written in 0.0.116 with 0.0.103 may now fail (#2351).