Merge pull request #454 from TheBlueMatt/2020-01-fuzz-mega-value
authorMatt Corallo <649246+TheBlueMatt@users.noreply.github.com>
Wed, 5 Feb 2020 01:23:44 +0000 (01:23 +0000)
committerGitHub <noreply@github.com>
Wed, 5 Feb 2020 01:23:44 +0000 (01:23 +0000)
Panic on txn with value > 21mill in ChannelMonitor::block_connected, Clean up fuzz targets a bit

1  2 
fuzz/src/chanmon_consistency.rs
fuzz/src/chanmon_deser.rs
fuzz/src/full_stack.rs
fuzz/src/router.rs
lightning/src/ln/channelmonitor.rs

index b961b23ac15134d48d55cdea881683467321a312,cc17ecc5b0e22b4dc7d312e742ee7c83e57bd434..cd22dc59f31ac0927ab623d22c3737c6e7fbd2e9
@@@ -29,8 -29,8 +29,8 @@@ use lightning::ln::channelmonitor
  use lightning::ln::channelmonitor::{ChannelMonitor, ChannelMonitorUpdateErr, HTLCUpdate};
  use lightning::ln::channelmanager::{ChannelManager, PaymentHash, PaymentPreimage, ChannelManagerReadArgs};
  use lightning::ln::router::{Route, RouteHop};
 -use lightning::ln::features::InitFeatures;
 -use lightning::ln::msgs::{CommitmentUpdate, ChannelMessageHandler, ErrorAction, UpdateAddHTLC};
 +use lightning::ln::features::{ChannelFeatures, InitFeatures, NodeFeatures};
 +use lightning::ln::msgs::{CommitmentUpdate, ChannelMessageHandler, ErrorAction, UpdateAddHTLC, Init};
  use lightning::util::enforcing_trait_impls::EnforcingChannelKeys;
  use lightning::util::events;
  use lightning::util::logger::Logger;
@@@ -75,7 -75,7 +75,7 @@@ impl Writer for VecWriter 
  
  static mut IN_RESTORE: bool = false;
  pub struct TestChannelMonitor {
 -      pub simple_monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint>>,
 +      pub simple_monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint, EnforcingChannelKeys>>,
        pub update_ret: Mutex<Result<(), channelmonitor::ChannelMonitorUpdateErr>>,
        pub latest_good_update: Mutex<HashMap<OutPoint, Vec<u8>>>,
        pub latest_update_good: Mutex<HashMap<OutPoint, bool>>,
@@@ -85,7 -85,7 +85,7 @@@
  impl TestChannelMonitor {
        pub fn new(chain_monitor: Arc<dyn chaininterface::ChainWatchInterface>, broadcaster: Arc<dyn chaininterface::BroadcasterInterface>, logger: Arc<dyn Logger>, feeest: Arc<dyn chaininterface::FeeEstimator>) -> Self {
                Self {
 -                      simple_monitor: channelmonitor::SimpleManyChannelMonitor::new(chain_monitor, broadcaster, logger, feeest),
 +                      simple_monitor: Arc::new(channelmonitor::SimpleManyChannelMonitor::new(chain_monitor, broadcaster, logger, feeest)),
                        update_ret: Mutex::new(Ok(())),
                        latest_good_update: Mutex::new(HashMap::new()),
                        latest_update_good: Mutex::new(HashMap::new()),
@@@ -94,8 -94,8 +94,8 @@@
                }
        }
  }
 -impl channelmonitor::ManyChannelMonitor for TestChannelMonitor {
 -      fn add_update_monitor(&self, funding_txo: OutPoint, monitor: channelmonitor::ChannelMonitor) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> {
 +impl channelmonitor::ManyChannelMonitor<EnforcingChannelKeys> for TestChannelMonitor {
 +      fn add_update_monitor(&self, funding_txo: OutPoint, monitor: channelmonitor::ChannelMonitor<EnforcingChannelKeys>) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> {
                let ret = self.update_ret.lock().unwrap().clone();
                if let Ok(()) = ret {
                        let mut ser = VecWriter(Vec::new());
@@@ -150,18 -150,16 +150,18 @@@ impl KeysInterface for KeyProvider 
                PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, self.node_id]).unwrap())
        }
  
 -      fn get_channel_keys(&self, _inbound: bool) -> EnforcingChannelKeys {
 -              EnforcingChannelKeys::new(InMemoryChannelKeys {
 -                      funding_key:               SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, self.node_id]).unwrap(),
 -                      revocation_base_key:       SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, self.node_id]).unwrap(),
 -                      payment_base_key:          SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, self.node_id]).unwrap(),
 -                      delayed_payment_base_key:  SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, self.node_id]).unwrap(),
 -                      htlc_base_key:             SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, self.node_id]).unwrap(),
 -                      commitment_seed: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, self.node_id],
 -                      remote_funding_pubkey: None,
 -              })
 +      fn get_channel_keys(&self, _inbound: bool, channel_value_satoshis: u64) -> EnforcingChannelKeys {
 +              let secp_ctx = Secp256k1::signing_only();
 +              EnforcingChannelKeys::new(InMemoryChannelKeys::new(
 +                      &secp_ctx,
 +                      SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, self.node_id]).unwrap(),
 +                      SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, self.node_id]).unwrap(),
 +                      SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, self.node_id]).unwrap(),
 +                      SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, self.node_id]).unwrap(),
 +                      SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, self.node_id]).unwrap(),
 +                      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, self.node_id],
 +                      channel_value_satoshis,
 +              ))
        }
  
        fn get_onion_rand(&self) -> (SecretKey, [u8; 32]) {
@@@ -192,7 -190,7 +192,7 @@@ pub fn do_test(data: &[u8]) 
                        config.channel_options.fee_proportional_millionths = 0;
                        config.channel_options.announced_channel = true;
                        config.peer_channel_config_limits.min_dust_limit_satoshis = 0;
 -                      (ChannelManager::new(Network::Bitcoin, fee_est.clone(), monitor.clone(), broadcast.clone(), Arc::clone(&logger), keys_manager.clone(), config, 0).unwrap(),
 +                      (Arc::new(ChannelManager::new(Network::Bitcoin, fee_est.clone(), monitor.clone() as Arc<channelmonitor::ManyChannelMonitor<EnforcingChannelKeys>>, broadcast.clone(), Arc::clone(&logger), keys_manager.clone(), config, 0).unwrap()),
                        monitor)
                } }
        }
                        let mut monitors = HashMap::new();
                        let mut old_monitors = $old_monitors.latest_good_update.lock().unwrap();
                        for (outpoint, monitor_ser) in old_monitors.drain() {
 -                              monitors.insert(outpoint, <(Sha256d, ChannelMonitor)>::read(&mut Cursor::new(&monitor_ser), Arc::clone(&logger)).expect("Failed to read monitor").1);
 +                              monitors.insert(outpoint, <(Sha256d, ChannelMonitor<EnforcingChannelKeys>)>::read(&mut Cursor::new(&monitor_ser), Arc::clone(&logger)).expect("Failed to read monitor").1);
                                monitor.latest_good_update.lock().unwrap().insert(outpoint, monitor_ser);
                        }
                        let mut monitor_refs = HashMap::new();
                        let read_args = ChannelManagerReadArgs {
                                keys_manager,
                                fee_estimator: fee_est.clone(),
 -                              monitor: monitor.clone(),
 +                              monitor: monitor.clone() as Arc<channelmonitor::ManyChannelMonitor<EnforcingChannelKeys>>,
                                tx_broadcaster: broadcast.clone(),
                                logger,
                                default_config: config,
                                channel_monitors: &mut monitor_refs,
                        };
  
 -                      let res = (<(Sha256d, ChannelManager<EnforcingChannelKeys>)>::read(&mut Cursor::new(&$ser.0), read_args).expect("Failed to read manager").1, monitor);
 +                      let res = (<(Sha256d, ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::ManyChannelMonitor<EnforcingChannelKeys>>>)>::read(&mut Cursor::new(&$ser.0), read_args).expect("Failed to read manager").1, monitor);
                        for (_, was_good) in $old_monitors.latest_updates_good_at_last_ser.lock().unwrap().iter() {
                                if !was_good {
                                        // If the last time we updated a monitor we didn't successfully update (and we
                                if let Err(_) = $source.send_payment(Route {
                                        hops: vec![RouteHop {
                                                pubkey: $dest.0.get_our_node_id(),
 +                                              node_features: NodeFeatures::empty(),
                                                short_channel_id: $dest.1,
 +                                              channel_features: ChannelFeatures::empty(),
                                                fee_msat: 5000000,
                                                cltv_expiry_delta: 200,
                                        }],
                                if let Err(_) = $source.send_payment(Route {
                                        hops: vec![RouteHop {
                                                pubkey: $middle.0.get_our_node_id(),
 +                                              node_features: NodeFeatures::empty(),
                                                short_channel_id: $middle.1,
 +                                              channel_features: ChannelFeatures::empty(),
                                                fee_msat: 50000,
                                                cltv_expiry_delta: 100,
                                        },RouteHop {
                                                pubkey: $dest.0.get_our_node_id(),
 +                                              node_features: NodeFeatures::empty(),
                                                short_channel_id: $dest.1,
 +                                              channel_features: ChannelFeatures::empty(),
                                                fee_msat: 5000000,
                                                cltv_expiry_delta: 200,
                                        }],
                        },
                        0x11 => {
                                if chan_a_disconnected {
 -                                      nodes[0].peer_connected(&nodes[1].get_our_node_id());
 -                                      nodes[1].peer_connected(&nodes[0].get_our_node_id());
 +                                      nodes[0].peer_connected(&nodes[1].get_our_node_id(), &Init { features: InitFeatures::empty() });
 +                                      nodes[1].peer_connected(&nodes[0].get_our_node_id(), &Init { features: InitFeatures::empty() });
                                        chan_a_disconnected = false;
                                }
                        },
                        0x12 => {
                                if chan_b_disconnected {
 -                                      nodes[1].peer_connected(&nodes[2].get_our_node_id());
 -                                      nodes[2].peer_connected(&nodes[1].get_our_node_id());
 +                                      nodes[1].peer_connected(&nodes[2].get_our_node_id(), &Init { features: InitFeatures::empty() });
 +                                      nodes[2].peer_connected(&nodes[1].get_our_node_id(), &Init { features: InitFeatures::empty() });
                                        chan_b_disconnected = false;
                                }
                        },
  pub extern "C" fn chanmon_consistency_run(data: *const u8, datalen: usize) {
        do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
  }
- #[cfg(test)]
- mod tests {
-       #[test]
-       fn duplicate_crash() {
-               super::do_test(&::hex::decode("00").unwrap());
-       }
- }
index dbd30266080627fa45fe89224e61ee5148424056,5d15f718fa7b19a9314f36e3d6bf2c80d9ff0908..c9042dd41dd089f0e28180731c5f7df12abe83bf
@@@ -3,7 -3,6 +3,7 @@@
  
  use bitcoin_hashes::sha256d::Hash as Sha256dHash;
  
 +use lightning::util::enforcing_trait_impls::EnforcingChannelKeys;
  use lightning::ln::channelmonitor;
  use lightning::util::ser::{ReadableArgs, Writer};
  
@@@ -26,10 -25,10 +26,10 @@@ impl Writer for VecWriter 
  #[inline]
  pub fn do_test(data: &[u8]) {
        let logger = Arc::new(test_logger::TestLogger::new("".to_owned()));
 -      if let Ok((latest_block_hash, monitor)) = <(Sha256dHash, channelmonitor::ChannelMonitor)>::read(&mut Cursor::new(data), logger.clone()) {
 +      if let Ok((latest_block_hash, monitor)) = <(Sha256dHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(&mut Cursor::new(data), logger.clone()) {
                let mut w = VecWriter(Vec::new());
                monitor.write_for_disk(&mut w).unwrap();
 -              let deserialized_copy = <(Sha256dHash, channelmonitor::ChannelMonitor)>::read(&mut Cursor::new(&w.0), logger.clone()).unwrap();
 +              let deserialized_copy = <(Sha256dHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(&mut Cursor::new(&w.0), logger.clone()).unwrap();
                assert!(latest_block_hash == deserialized_copy.0);
                assert!(monitor == deserialized_copy.1);
                w.0.clear();
  pub extern "C" fn chanmon_deser_run(data: *const u8, datalen: usize) {
        do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
  }
- #[cfg(test)]
- mod tests {
-       #[test]
-       fn duplicate_crash() {
-               super::do_test(&::hex::decode("00").unwrap());
-       }
- }
diff --combined fuzz/src/full_stack.rs
index 36e6d73186773d8afd24920cdd3582aa379353c5,5aa6d94ed81f47d796206673e02e96a23833a636..568f8085131d259282cb164fbb6c7cb0fc882d06
@@@ -136,9 -136,9 +136,9 @@@ impl<'a> Hash for Peer<'a> 
  }
  
  struct MoneyLossDetector<'a> {
 -      manager: Arc<ChannelManager<EnforcingChannelKeys>>,
 -      monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint>>,
 -      handler: PeerManager<Peer<'a>>,
 +      manager: Arc<ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::ManyChannelMonitor<EnforcingChannelKeys>>>>,
 +      monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint, EnforcingChannelKeys>>,
 +      handler: PeerManager<Peer<'a>, Arc<ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::ManyChannelMonitor<EnforcingChannelKeys>>>>>,
  
        peers: &'a RefCell<[bool; 256]>,
        funding_txn: Vec<Transaction>,
        blocks_connected: u32,
  }
  impl<'a> MoneyLossDetector<'a> {
 -      pub fn new(peers: &'a RefCell<[bool; 256]>, manager: Arc<ChannelManager<EnforcingChannelKeys>>, monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint>>, handler: PeerManager<Peer<'a>>) -> Self {
 +      pub fn new(peers: &'a RefCell<[bool; 256]>,
 +                 manager: Arc<ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::ManyChannelMonitor<EnforcingChannelKeys>>>>,
 +                 monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint, EnforcingChannelKeys>>,
 +                 handler: PeerManager<Peer<'a>, Arc<ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::ManyChannelMonitor<EnforcingChannelKeys>>>>>) -> Self {
                MoneyLossDetector {
                        manager,
                        monitor,
@@@ -250,31 -247,28 +250,31 @@@ impl KeysInterface for KeyProvider 
                PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]).unwrap())
        }
  
 -      fn get_channel_keys(&self, inbound: bool) -> EnforcingChannelKeys {
 +      fn get_channel_keys(&self, inbound: bool, channel_value_satoshis: u64) -> EnforcingChannelKeys {
                let ctr = self.counter.fetch_add(1, Ordering::Relaxed) as u8;
 +              let secp_ctx = Secp256k1::signing_only();
                EnforcingChannelKeys::new(if inbound {
 -                      InMemoryChannelKeys {
 -                              funding_key:               SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, ctr]).unwrap(),
 -                              revocation_base_key:       SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, ctr]).unwrap(),
 -                              payment_base_key:          SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, ctr]).unwrap(),
 -                              delayed_payment_base_key:  SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, ctr]).unwrap(),
 -                              htlc_base_key:             SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, ctr]).unwrap(),
 -                              commitment_seed: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, ctr],
 -                              remote_funding_pubkey: None,
 -                      }
 +                      InMemoryChannelKeys::new(
 +                              &secp_ctx,
 +                              SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, ctr]).unwrap(),
 +                              SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, ctr]).unwrap(),
 +                              SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, ctr]).unwrap(),
 +                              SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, ctr]).unwrap(),
 +                              SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, ctr]).unwrap(),
 +                              [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, ctr],
 +                              channel_value_satoshis,
 +                      )
                } else {
 -                      InMemoryChannelKeys {
 -                              funding_key:               SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, ctr]).unwrap(),
 -                              revocation_base_key:       SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, ctr]).unwrap(),
 -                              payment_base_key:          SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, ctr]).unwrap(),
 -                              delayed_payment_base_key:  SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, ctr]).unwrap(),
 -                              htlc_base_key:             SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, ctr]).unwrap(),
 -                              commitment_seed: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, ctr],
 -                              remote_funding_pubkey: None,
 -                      }
 +                      InMemoryChannelKeys::new(
 +                              &secp_ctx,
 +                              SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, ctr]).unwrap(),
 +                              SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, ctr]).unwrap(),
 +                              SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, ctr]).unwrap(),
 +                              SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, ctr]).unwrap(),
 +                              SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, ctr]).unwrap(),
 +                              [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, ctr],
 +                              channel_value_satoshis,
 +                      )
                })
        }
  
@@@ -326,14 -320,14 +326,14 @@@ pub fn do_test(data: &[u8], logger: &Ar
  
        let watch = Arc::new(ChainWatchInterfaceUtil::new(Network::Bitcoin, Arc::clone(&logger)));
        let broadcast = Arc::new(TestBroadcaster{});
 -      let monitor = channelmonitor::SimpleManyChannelMonitor::new(watch.clone(), broadcast.clone(), Arc::clone(&logger), fee_est.clone());
 +      let monitor = Arc::new(channelmonitor::SimpleManyChannelMonitor::new(watch.clone(), broadcast.clone(), Arc::clone(&logger), fee_est.clone()));
  
        let keys_manager = Arc::new(KeyProvider { node_secret: our_network_key.clone(), counter: AtomicU64::new(0) });
        let mut config = UserConfig::default();
        config.channel_options.fee_proportional_millionths =  slice_to_be32(get_slice!(4));
        config.channel_options.announced_channel = get_slice!(1)[0] != 0;
        config.peer_channel_config_limits.min_dust_limit_satoshis = 0;
 -      let channelmanager = ChannelManager::new(Network::Bitcoin, fee_est.clone(), monitor.clone(), broadcast.clone(), Arc::clone(&logger), keys_manager.clone(), config, 0).unwrap();
 +      let channelmanager = Arc::new(ChannelManager::new(Network::Bitcoin, fee_est.clone(), monitor.clone() as Arc<channelmonitor::ManyChannelMonitor<EnforcingChannelKeys>>, broadcast.clone(), Arc::clone(&logger), keys_manager.clone(), config, 0).unwrap());
        let router = Arc::new(Router::new(PublicKey::from_secret_key(&Secp256k1::signing_only(), &keys_manager.get_node_secret()), watch.clone(), Arc::clone(&logger)));
  
        let peers = RefCell::new([false; 256]);
                                } else {
                                        let txres: Result<Transaction, _> = deserialize(get_slice!(txlen));
                                        if let Ok(tx) = txres {
+                                               let mut output_val = 0;
+                                               for out in tx.output.iter() {
+                                                       if out.value > 21_000_000_0000_0000 { return; }
+                                                       output_val += out.value;
+                                                       if output_val > 21_000_000_0000_0000 { return; }
+                                               }
                                                loss_detector.connect_block(&[tx]);
                                        } else {
                                                return;
@@@ -546,12 -546,6 +552,6 @@@ mod tests 
        use std::collections::HashMap;
        use std::sync::{Arc, Mutex};
  
-       #[test]
-       fn duplicate_crash() {
-               let logger: Arc<dyn Logger> = Arc::new(test_logger::TestLogger::new("".to_owned()));
-               super::do_test(&::hex::decode("00").unwrap(), &logger);
-       }
        struct TrackingLogger {
                /// (module, message) -> count
                pub lines: Mutex<HashMap<(String, String), usize>>,
diff --combined fuzz/src/router.rs
index fbb629220112a63f543135c1be2537908c6ad017,99013d87e6e8bc97d710799bcbe1199ae7a85c6c..434e1b657d135de9d49dffdf1a7d83c4be2d2cc5
@@@ -5,9 -5,8 +5,9 @@@ use bitcoin::blockdata::transaction::Tr
  
  use lightning::chain::chaininterface::{ChainError,ChainWatchInterface};
  use lightning::ln::channelmanager::ChannelDetails;
 +use lightning::ln::features::InitFeatures;
  use lightning::ln::msgs;
 -use lightning::ln::msgs::{RoutingMessageHandler};
 +use lightning::ln::msgs::RoutingMessageHandler;
  use lightning::ln::router::{Router, RouteHint};
  use lightning::util::logger::Logger;
  use lightning::util::ser::Readable;
@@@ -199,7 -198,6 +199,7 @@@ pub fn do_test(data: &[u8]) 
                                                                channel_id: [0; 32],
                                                                short_channel_id: Some(slice_to_be64(get_slice!(8))),
                                                                remote_network_id: get_pubkey!(),
 +                                                              counterparty_features: InitFeatures::empty(),
                                                                channel_value_satoshis: slice_to_be64(get_slice!(8)),
                                                                user_id: 0,
                                                                inbound_capacity_msat: 0,
  pub extern "C" fn router_run(data: *const u8, datalen: usize) {
        do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
  }
- #[cfg(test)]
- mod tests {
-       #[test]
-       fn duplicate_crash() {
-               super::do_test(&::hex::decode("00").unwrap());
-       }
- }
index 0a70eb0e676bb34d84e521d92524664530a6243e,c96c30cbeee448f0c8067eef0561b51b3a8b0455..51446ccc57162d80fb1a1f56472959ddaa97d803
@@@ -35,7 -35,7 +35,7 @@@ use ln::chan_utils::{HTLCOutputInCommit
  use ln::channelmanager::{HTLCSource, PaymentPreimage, PaymentHash};
  use chain::chaininterface::{ChainListener, ChainWatchInterface, BroadcasterInterface, FeeEstimator, ConfirmationTarget, MIN_RELAY_FEE_SAT_PER_1000_WEIGHT};
  use chain::transaction::OutPoint;
 -use chain::keysinterface::SpendableOutputDescriptor;
 +use chain::keysinterface::{SpendableOutputDescriptor, ChannelKeys};
  use util::logger::Logger;
  use util::ser::{ReadableArgs, Readable, Writer, Writeable, U48};
  use util::{byte_utils, events};
@@@ -114,13 -114,13 +114,13 @@@ pub struct HTLCUpdate 
  /// than calling these methods directly, the user should register implementors as listeners to the
  /// BlockNotifier and call the BlockNotifier's `block_(dis)connected` methods, which will notify
  /// all registered listeners in one go.
 -pub trait ManyChannelMonitor: Send + Sync {
 +pub trait ManyChannelMonitor<ChanSigner: ChannelKeys>: Send + Sync {
        /// Adds or updates a monitor for the given `funding_txo`.
        ///
        /// Implementor must also ensure that the funding_txo outpoint is registered with any relevant
        /// ChainWatchInterfaces such that the provided monitor receives block_connected callbacks with
        /// any spends of it.
 -      fn add_update_monitor(&self, funding_txo: OutPoint, monitor: ChannelMonitor) -> Result<(), ChannelMonitorUpdateErr>;
 +      fn add_update_monitor(&self, funding_txo: OutPoint, monitor: ChannelMonitor<ChanSigner>) -> Result<(), ChannelMonitorUpdateErr>;
  
        /// Used by ChannelManager to get list of HTLC resolved onchain and which needed to be updated
        /// with success or failure backward
  ///
  /// If you're using this for local monitoring of your own channels, you probably want to use
  /// `OutPoint` as the key, which will give you a ManyChannelMonitor implementation.
 -pub struct SimpleManyChannelMonitor<Key> {
 +pub struct SimpleManyChannelMonitor<Key, ChanSigner: ChannelKeys> {
        #[cfg(test)] // Used in ChannelManager tests to manipulate channels directly
 -      pub monitors: Mutex<HashMap<Key, ChannelMonitor>>,
 +      pub monitors: Mutex<HashMap<Key, ChannelMonitor<ChanSigner>>>,
        #[cfg(not(test))]
 -      monitors: Mutex<HashMap<Key, ChannelMonitor>>,
 +      monitors: Mutex<HashMap<Key, ChannelMonitor<ChanSigner>>>,
        chain_monitor: Arc<ChainWatchInterface>,
        broadcaster: Arc<BroadcasterInterface>,
        pending_events: Mutex<Vec<events::Event>>,
        fee_estimator: Arc<FeeEstimator>
  }
  
 -impl<'a, Key : Send + cmp::Eq + hash::Hash> ChainListener for SimpleManyChannelMonitor<Key> {
 -
 +impl<'a, Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys> ChainListener for SimpleManyChannelMonitor<Key, ChanSigner> {
        fn block_connected(&self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], _indexes_of_txn_matched: &[u32]) {
                let block_hash = header.bitcoin_hash();
                let mut new_events: Vec<events::Event> = Vec::with_capacity(0);
        }
  }
  
 -impl<Key : Send + cmp::Eq + hash::Hash + 'static> SimpleManyChannelMonitor<Key> {
 +impl<Key : Send + cmp::Eq + hash::Hash + 'static, ChanSigner: ChannelKeys> SimpleManyChannelMonitor<Key, ChanSigner> {
        /// Creates a new object which can be used to monitor several channels given the chain
        /// interface with which to register to receive notifications.
 -      pub fn new(chain_monitor: Arc<ChainWatchInterface>, broadcaster: Arc<BroadcasterInterface>, logger: Arc<Logger>, feeest: Arc<FeeEstimator>) -> Arc<SimpleManyChannelMonitor<Key>> {
 -              let res = Arc::new(SimpleManyChannelMonitor {
 +      pub fn new(chain_monitor: Arc<ChainWatchInterface>, broadcaster: Arc<BroadcasterInterface>, logger: Arc<Logger>, feeest: Arc<FeeEstimator>) -> SimpleManyChannelMonitor<Key, ChanSigner> {
 +              let res = SimpleManyChannelMonitor {
                        monitors: Mutex::new(HashMap::new()),
                        chain_monitor,
                        broadcaster,
                        pending_htlc_updated: Mutex::new(HashMap::new()),
                        logger,
                        fee_estimator: feeest,
 -              });
 +              };
  
                res
        }
  
        /// Adds or updates the monitor which monitors the channel referred to by the given key.
 -      pub fn add_update_monitor_by_key(&self, key: Key, monitor: ChannelMonitor) -> Result<(), MonitorUpdateError> {
 +      pub fn add_update_monitor_by_key(&self, key: Key, monitor: ChannelMonitor<ChanSigner>) -> Result<(), MonitorUpdateError> {
                let mut monitors = self.monitors.lock().unwrap();
                match monitors.get_mut(&key) {
                        Some(orig_monitor) => {
        }
  }
  
 -impl ManyChannelMonitor for SimpleManyChannelMonitor<OutPoint> {
 -      fn add_update_monitor(&self, funding_txo: OutPoint, monitor: ChannelMonitor) -> Result<(), ChannelMonitorUpdateErr> {
 +impl<ChanSigner: ChannelKeys> ManyChannelMonitor<ChanSigner> for SimpleManyChannelMonitor<OutPoint, ChanSigner> {
 +      fn add_update_monitor(&self, funding_txo: OutPoint, monitor: ChannelMonitor<ChanSigner>) -> Result<(), ChannelMonitorUpdateErr> {
                match self.add_update_monitor_by_key(funding_txo, monitor) {
                        Ok(_) => Ok(()),
                        Err(_) => Err(ChannelMonitorUpdateErr::PermanentFailure),
        }
  }
  
 -impl<Key : Send + cmp::Eq + hash::Hash> events::EventsProvider for SimpleManyChannelMonitor<Key> {
 +impl<Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys> events::EventsProvider for SimpleManyChannelMonitor<Key, ChanSigner> {
        fn get_and_clear_pending_events(&self) -> Vec<events::Event> {
                let mut pending_events = self.pending_events.lock().unwrap();
                let mut ret = Vec::new();
@@@ -326,10 -327,9 +326,10 @@@ pub(crate) const LATENCY_GRACE_PERIOD_B
  /// keeping bumping another claim tx to solve the outpoint.
  pub(crate) const ANTI_REORG_DELAY: u32 = 6;
  
 -#[derive(Clone, PartialEq)]
 -enum Storage {
 +#[derive(Clone)]
 +enum Storage<ChanSigner: ChannelKeys> {
        Local {
 +              keys: ChanSigner,
                funding_key: SecretKey,
                revocation_base_key: SecretKey,
                htlc_base_key: SecretKey,
        }
  }
  
 +#[cfg(any(test, feature = "fuzztarget"))]
 +impl<ChanSigner: ChannelKeys> PartialEq for Storage<ChanSigner> {
 +      fn eq(&self, other: &Self) -> bool {
 +              match *self {
 +                      Storage::Local { ref keys, .. } => {
 +                              let k = keys;
 +                              match *other {
 +                                      Storage::Local { ref keys, .. } => keys.pubkeys() == k.pubkeys(),
 +                                      Storage::Watchtower { .. } => false,
 +                              }
 +                      },
 +                      Storage::Watchtower {ref revocation_base_key, ref htlc_base_key} => {
 +                              let (rbk, hbk) = (revocation_base_key, htlc_base_key);
 +                              match *other {
 +                                      Storage::Local { .. } => false,
 +                                      Storage::Watchtower {ref revocation_base_key, ref htlc_base_key} =>
 +                                              revocation_base_key == rbk && htlc_base_key == hbk,
 +                              }
 +                      },
 +              }
 +      }
 +}
 +
  #[derive(Clone, PartialEq)]
  struct LocalSignedTx {
        /// txid of the transaction in tx, just used to make comparison faster
@@@ -587,10 -564,10 +587,10 @@@ const MIN_SERIALIZATION_VERSION: u8 = 1
  /// You MUST ensure that no ChannelMonitors for a given channel anywhere contain out-of-date
  /// information and are actively monitoring the chain.
  #[derive(Clone)]
 -pub struct ChannelMonitor {
 +pub struct ChannelMonitor<ChanSigner: ChannelKeys> {
        commitment_transaction_number_obscure_factor: u64,
  
 -      key_storage: Storage,
 +      key_storage: Storage<ChanSigner>,
        their_htlc_base_key: Option<PublicKey>,
        their_delayed_payment_base_key: Option<PublicKey>,
        funding_redeemscript: Option<Script>,
@@@ -714,7 -691,7 +714,7 @@@ macro_rules! subtract_high_prio_fee 
  #[cfg(any(test, feature = "fuzztarget"))]
  /// Used only in testing and fuzztarget to check serialization roundtrips don't change the
  /// underlying object
 -impl PartialEq for ChannelMonitor {
 +impl<ChanSigner: ChannelKeys> PartialEq for ChannelMonitor<ChanSigner> {
        fn eq(&self, other: &Self) -> bool {
                if self.commitment_transaction_number_obscure_factor != other.commitment_transaction_number_obscure_factor ||
                        self.key_storage != other.key_storage ||
        }
  }
  
 -impl ChannelMonitor {
 -      pub(super) fn new(funding_key: &SecretKey, revocation_base_key: &SecretKey, delayed_payment_base_key: &SecretKey, htlc_base_key: &SecretKey, payment_base_key: &SecretKey, shutdown_pubkey: &PublicKey, our_to_self_delay: u16, destination_script: Script, logger: Arc<Logger>) -> ChannelMonitor {
 +impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
 +      /// Serializes into a vec, with various modes for the exposed pub fns
 +      fn write<W: Writer>(&self, writer: &mut W, for_local_storage: bool) -> Result<(), ::std::io::Error> {
 +              //TODO: We still write out all the serialization here manually instead of using the fancy
 +              //serialization framework we have, we should migrate things over to it.
 +              writer.write_all(&[SERIALIZATION_VERSION; 1])?;
 +              writer.write_all(&[MIN_SERIALIZATION_VERSION; 1])?;
 +
 +              // Set in initial Channel-object creation, so should always be set by now:
 +              U48(self.commitment_transaction_number_obscure_factor).write(writer)?;
 +
 +              macro_rules! write_option {
 +                      ($thing: expr) => {
 +                              match $thing {
 +                                      &Some(ref t) => {
 +                                              1u8.write(writer)?;
 +                                              t.write(writer)?;
 +                                      },
 +                                      &None => 0u8.write(writer)?,
 +                              }
 +                      }
 +              }
 +
 +              match self.key_storage {
 +                      Storage::Local { ref keys, ref funding_key, ref revocation_base_key, ref htlc_base_key, ref delayed_payment_base_key, ref payment_base_key, ref shutdown_pubkey, ref funding_info, ref current_remote_commitment_txid, ref prev_remote_commitment_txid } => {
 +                              writer.write_all(&[0; 1])?;
 +                              keys.write(writer)?;
 +                              writer.write_all(&funding_key[..])?;
 +                              writer.write_all(&revocation_base_key[..])?;
 +                              writer.write_all(&htlc_base_key[..])?;
 +                              writer.write_all(&delayed_payment_base_key[..])?;
 +                              writer.write_all(&payment_base_key[..])?;
 +                              writer.write_all(&shutdown_pubkey.serialize())?;
 +                              match funding_info  {
 +                                      &Some((ref outpoint, ref script)) => {
 +                                              writer.write_all(&outpoint.txid[..])?;
 +                                              writer.write_all(&byte_utils::be16_to_array(outpoint.index))?;
 +                                              script.write(writer)?;
 +                                      },
 +                                      &None => {
 +                                              debug_assert!(false, "Try to serialize a useless Local monitor !");
 +                                      },
 +                              }
 +                              current_remote_commitment_txid.write(writer)?;
 +                              prev_remote_commitment_txid.write(writer)?;
 +                      },
 +                      Storage::Watchtower { .. } => unimplemented!(),
 +              }
 +
 +              writer.write_all(&self.their_htlc_base_key.as_ref().unwrap().serialize())?;
 +              writer.write_all(&self.their_delayed_payment_base_key.as_ref().unwrap().serialize())?;
 +              self.funding_redeemscript.as_ref().unwrap().write(writer)?;
 +              self.channel_value_satoshis.unwrap().write(writer)?;
 +
 +              match self.their_cur_revocation_points {
 +                      Some((idx, pubkey, second_option)) => {
 +                              writer.write_all(&byte_utils::be48_to_array(idx))?;
 +                              writer.write_all(&pubkey.serialize())?;
 +                              match second_option {
 +                                      Some(second_pubkey) => {
 +                                              writer.write_all(&second_pubkey.serialize())?;
 +                                      },
 +                                      None => {
 +                                              writer.write_all(&[0; 33])?;
 +                                      },
 +                              }
 +                      },
 +                      None => {
 +                              writer.write_all(&byte_utils::be48_to_array(0))?;
 +                      },
 +              }
 +
 +              writer.write_all(&byte_utils::be16_to_array(self.our_to_self_delay))?;
 +              writer.write_all(&byte_utils::be16_to_array(self.their_to_self_delay.unwrap()))?;
 +
 +              for &(ref secret, ref idx) in self.old_secrets.iter() {
 +                      writer.write_all(secret)?;
 +                      writer.write_all(&byte_utils::be64_to_array(*idx))?;
 +              }
 +
 +              macro_rules! serialize_htlc_in_commitment {
 +                      ($htlc_output: expr) => {
 +                              writer.write_all(&[$htlc_output.offered as u8; 1])?;
 +                              writer.write_all(&byte_utils::be64_to_array($htlc_output.amount_msat))?;
 +                              writer.write_all(&byte_utils::be32_to_array($htlc_output.cltv_expiry))?;
 +                              writer.write_all(&$htlc_output.payment_hash.0[..])?;
 +                              $htlc_output.transaction_output_index.write(writer)?;
 +                      }
 +              }
 +
 +              writer.write_all(&byte_utils::be64_to_array(self.remote_claimable_outpoints.len() as u64))?;
 +              for (ref txid, ref htlc_infos) in self.remote_claimable_outpoints.iter() {
 +                      writer.write_all(&txid[..])?;
 +                      writer.write_all(&byte_utils::be64_to_array(htlc_infos.len() as u64))?;
 +                      for &(ref htlc_output, ref htlc_source) in htlc_infos.iter() {
 +                              serialize_htlc_in_commitment!(htlc_output);
 +                              write_option!(htlc_source);
 +                      }
 +              }
 +
 +              writer.write_all(&byte_utils::be64_to_array(self.remote_commitment_txn_on_chain.len() as u64))?;
 +              for (ref txid, &(commitment_number, ref txouts)) in self.remote_commitment_txn_on_chain.iter() {
 +                      writer.write_all(&txid[..])?;
 +                      writer.write_all(&byte_utils::be48_to_array(commitment_number))?;
 +                      (txouts.len() as u64).write(writer)?;
 +                      for script in txouts.iter() {
 +                              script.write(writer)?;
 +                      }
 +              }
 +
 +              if for_local_storage {
 +                      writer.write_all(&byte_utils::be64_to_array(self.remote_hash_commitment_number.len() as u64))?;
 +                      for (ref payment_hash, commitment_number) in self.remote_hash_commitment_number.iter() {
 +                              writer.write_all(&payment_hash.0[..])?;
 +                              writer.write_all(&byte_utils::be48_to_array(*commitment_number))?;
 +                      }
 +              } else {
 +                      writer.write_all(&byte_utils::be64_to_array(0))?;
 +              }
 +
 +              macro_rules! serialize_local_tx {
 +                      ($local_tx: expr) => {
 +                              $local_tx.tx.write(writer)?;
 +                              writer.write_all(&$local_tx.revocation_key.serialize())?;
 +                              writer.write_all(&$local_tx.a_htlc_key.serialize())?;
 +                              writer.write_all(&$local_tx.b_htlc_key.serialize())?;
 +                              writer.write_all(&$local_tx.delayed_payment_key.serialize())?;
 +                              writer.write_all(&$local_tx.per_commitment_point.serialize())?;
 +
 +                              writer.write_all(&byte_utils::be64_to_array($local_tx.feerate_per_kw))?;
 +                              writer.write_all(&byte_utils::be64_to_array($local_tx.htlc_outputs.len() as u64))?;
 +                              for &(ref htlc_output, ref sig, ref htlc_source) in $local_tx.htlc_outputs.iter() {
 +                                      serialize_htlc_in_commitment!(htlc_output);
 +                                      if let &Some(ref their_sig) = sig {
 +                                              1u8.write(writer)?;
 +                                              writer.write_all(&their_sig.serialize_compact())?;
 +                                      } else {
 +                                              0u8.write(writer)?;
 +                                      }
 +                                      write_option!(htlc_source);
 +                              }
 +                      }
 +              }
 +
 +              if let Some(ref prev_local_tx) = self.prev_local_signed_commitment_tx {
 +                      writer.write_all(&[1; 1])?;
 +                      serialize_local_tx!(prev_local_tx);
 +              } else {
 +                      writer.write_all(&[0; 1])?;
 +              }
 +
 +              if let Some(ref cur_local_tx) = self.current_local_signed_commitment_tx {
 +                      writer.write_all(&[1; 1])?;
 +                      serialize_local_tx!(cur_local_tx);
 +              } else {
 +                      writer.write_all(&[0; 1])?;
 +              }
 +
 +              if for_local_storage {
 +                      writer.write_all(&byte_utils::be48_to_array(self.current_remote_commitment_number))?;
 +              } else {
 +                      writer.write_all(&byte_utils::be48_to_array(0))?;
 +              }
 +
 +              writer.write_all(&byte_utils::be64_to_array(self.payment_preimages.len() as u64))?;
 +              for payment_preimage in self.payment_preimages.values() {
 +                      writer.write_all(&payment_preimage.0[..])?;
 +              }
 +
 +              self.last_block_hash.write(writer)?;
 +              self.destination_script.write(writer)?;
 +              if let Some((ref to_remote_script, ref local_key)) = self.to_remote_rescue {
 +                      writer.write_all(&[1; 1])?;
 +                      to_remote_script.write(writer)?;
 +                      local_key.write(writer)?;
 +              } else {
 +                      writer.write_all(&[0; 1])?;
 +              }
 +
 +              writer.write_all(&byte_utils::be64_to_array(self.pending_claim_requests.len() as u64))?;
 +              for (ref ancestor_claim_txid, claim_tx_data) in self.pending_claim_requests.iter() {
 +                      ancestor_claim_txid.write(writer)?;
 +                      claim_tx_data.write(writer)?;
 +              }
 +
 +              writer.write_all(&byte_utils::be64_to_array(self.claimable_outpoints.len() as u64))?;
 +              for (ref outp, ref claim_and_height) in self.claimable_outpoints.iter() {
 +                      outp.write(writer)?;
 +                      claim_and_height.0.write(writer)?;
 +                      claim_and_height.1.write(writer)?;
 +              }
 +
 +              writer.write_all(&byte_utils::be64_to_array(self.onchain_events_waiting_threshold_conf.len() as u64))?;
 +              for (ref target, ref events) in self.onchain_events_waiting_threshold_conf.iter() {
 +                      writer.write_all(&byte_utils::be32_to_array(**target))?;
 +                      writer.write_all(&byte_utils::be64_to_array(events.len() as u64))?;
 +                      for ev in events.iter() {
 +                              match *ev {
 +                                      OnchainEvent::Claim { ref claim_request } => {
 +                                              writer.write_all(&[0; 1])?;
 +                                              claim_request.write(writer)?;
 +                                      },
 +                                      OnchainEvent::HTLCUpdate { ref htlc_update } => {
 +                                              writer.write_all(&[1; 1])?;
 +                                              htlc_update.0.write(writer)?;
 +                                              htlc_update.1.write(writer)?;
 +                                      },
 +                                      OnchainEvent::ContentiousOutpoint { ref outpoint, ref input_material } => {
 +                                              writer.write_all(&[2; 1])?;
 +                                              outpoint.write(writer)?;
 +                                              input_material.write(writer)?;
 +                                      }
 +                              }
 +                      }
 +              }
 +
 +              Ok(())
 +      }
 +
 +      /// Writes this monitor into the given writer, suitable for writing to disk.
 +      ///
 +      /// Note that the deserializer is only implemented for (Sha256dHash, ChannelMonitor), which
 +      /// tells you the last block hash which was block_connect()ed. You MUST rescan any blocks along
 +      /// the "reorg path" (ie not just starting at the same height but starting at the highest
 +      /// common block that appears on your best chain as well as on the chain which contains the
 +      /// last block hash returned) upon deserializing the object!
 +      pub fn write_for_disk<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
 +              self.write(writer, true)
 +      }
 +
 +      /// Encodes this monitor into the given writer, suitable for sending to a remote watchtower
 +      ///
 +      /// Note that the deserializer is only implemented for (Sha256dHash, ChannelMonitor), which
 +      /// tells you the last block hash which was block_connect()ed. You MUST rescan any blocks along
 +      /// the "reorg path" (ie not just starting at the same height but starting at the highest
 +      /// common block that appears on your best chain as well as on the chain which contains the
 +      /// last block hash returned) upon deserializing the object!
 +      pub fn write_for_watchtower<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
 +              self.write(writer, false)
 +      }
 +}
 +
 +impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
 +      pub(super) fn new(keys: ChanSigner, funding_key: &SecretKey, revocation_base_key: &SecretKey, delayed_payment_base_key: &SecretKey, htlc_base_key: &SecretKey, payment_base_key: &SecretKey, shutdown_pubkey: &PublicKey, our_to_self_delay: u16, destination_script: Script, logger: Arc<Logger>) -> ChannelMonitor<ChanSigner> {
                ChannelMonitor {
                        commitment_transaction_number_obscure_factor: 0,
  
                        key_storage: Storage::Local {
 +                              keys,
                                funding_key: funding_key.clone(),
                                revocation_base_key: revocation_base_key.clone(),
                                htlc_base_key: htlc_base_key.clone(),
        /// needed by local commitment transactions HTCLs nor by remote ones. Unless we haven't already seen remote
        /// commitment transaction's secret, they are de facto pruned (we can use revocation key).
        pub(super) fn provide_secret(&mut self, idx: u64, secret: [u8; 32]) -> Result<(), MonitorUpdateError> {
 -              let pos = ChannelMonitor::place_secret(idx);
 +              let pos = ChannelMonitor::<ChanSigner>::place_secret(idx);
                for i in 0..pos {
                        let (old_secret, old_idx) = self.old_secrets[i as usize];
 -                      if ChannelMonitor::derive_secret(secret, pos, old_idx) != old_secret {
 +                      if ChannelMonitor::<ChanSigner>::derive_secret(secret, pos, old_idx) != old_secret {
                                return Err(MonitorUpdateError("Previous secret did not match new one"));
                        }
                }
  
        pub(super) fn provide_rescue_remote_commitment_tx_info(&mut self, their_revocation_point: PublicKey) {
                match self.key_storage {
 -                      Storage::Local { ref payment_base_key, .. } => {
 -                              if let Ok(payment_key) = chan_utils::derive_public_key(&self.secp_ctx, &their_revocation_point, &PublicKey::from_secret_key(&self.secp_ctx, &payment_base_key)) {
 +                      Storage::Local { ref payment_base_key, ref keys, .. } => {
 +                              if let Ok(payment_key) = chan_utils::derive_public_key(&self.secp_ctx, &their_revocation_point, &keys.pubkeys().payment_basepoint) {
                                        let to_remote_script =  Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0)
                                                .push_slice(&Hash160::hash(&payment_key.serialize())[..])
                                                .into_script();
        /// Combines this ChannelMonitor with the information contained in the other ChannelMonitor.
        /// After a successful call this ChannelMonitor is up-to-date and is safe to use to monitor the
        /// chain for new blocks/transactions.
 -      pub fn insert_combine(&mut self, mut other: ChannelMonitor) -> Result<(), MonitorUpdateError> {
 +      pub fn insert_combine(&mut self, mut other: ChannelMonitor<ChanSigner>) -> Result<(), MonitorUpdateError> {
                match self.key_storage {
                        Storage::Local { ref funding_info, .. } => {
                                if funding_info.is_none() { return Err(MonitorUpdateError("Try to combine a Local monitor without funding_info")); }
                res
        }
  
 -      /// Serializes into a vec, with various modes for the exposed pub fns
 -      fn write<W: Writer>(&self, writer: &mut W, for_local_storage: bool) -> Result<(), ::std::io::Error> {
 -              //TODO: We still write out all the serialization here manually instead of using the fancy
 -              //serialization framework we have, we should migrate things over to it.
 -              writer.write_all(&[SERIALIZATION_VERSION; 1])?;
 -              writer.write_all(&[MIN_SERIALIZATION_VERSION; 1])?;
 -
 -              // Set in initial Channel-object creation, so should always be set by now:
 -              U48(self.commitment_transaction_number_obscure_factor).write(writer)?;
 -
 -              macro_rules! write_option {
 -                      ($thing: expr) => {
 -                              match $thing {
 -                                      &Some(ref t) => {
 -                                              1u8.write(writer)?;
 -                                              t.write(writer)?;
 -                                      },
 -                                      &None => 0u8.write(writer)?,
 -                              }
 -                      }
 -              }
 -
 -              match self.key_storage {
 -                      Storage::Local { ref funding_key, ref revocation_base_key, ref htlc_base_key, ref delayed_payment_base_key, ref payment_base_key, ref shutdown_pubkey, ref funding_info, ref current_remote_commitment_txid, ref prev_remote_commitment_txid } => {
 -                              writer.write_all(&[0; 1])?;
 -                              writer.write_all(&funding_key[..])?;
 -                              writer.write_all(&revocation_base_key[..])?;
 -                              writer.write_all(&htlc_base_key[..])?;
 -                              writer.write_all(&delayed_payment_base_key[..])?;
 -                              writer.write_all(&payment_base_key[..])?;
 -                              writer.write_all(&shutdown_pubkey.serialize())?;
 -                              match funding_info  {
 -                                      &Some((ref outpoint, ref script)) => {
 -                                              writer.write_all(&outpoint.txid[..])?;
 -                                              writer.write_all(&byte_utils::be16_to_array(outpoint.index))?;
 -                                              script.write(writer)?;
 -                                      },
 -                                      &None => {
 -                                              debug_assert!(false, "Try to serialize a useless Local monitor !");
 -                                      },
 -                              }
 -                              current_remote_commitment_txid.write(writer)?;
 -                              prev_remote_commitment_txid.write(writer)?;
 -                      },
 -                      Storage::Watchtower { .. } => unimplemented!(),
 -              }
 -
 -              writer.write_all(&self.their_htlc_base_key.as_ref().unwrap().serialize())?;
 -              writer.write_all(&self.their_delayed_payment_base_key.as_ref().unwrap().serialize())?;
 -              self.funding_redeemscript.as_ref().unwrap().write(writer)?;
 -              self.channel_value_satoshis.unwrap().write(writer)?;
 -
 -              match self.their_cur_revocation_points {
 -                      Some((idx, pubkey, second_option)) => {
 -                              writer.write_all(&byte_utils::be48_to_array(idx))?;
 -                              writer.write_all(&pubkey.serialize())?;
 -                              match second_option {
 -                                      Some(second_pubkey) => {
 -                                              writer.write_all(&second_pubkey.serialize())?;
 -                                      },
 -                                      None => {
 -                                              writer.write_all(&[0; 33])?;
 -                                      },
 -                              }
 -                      },
 -                      None => {
 -                              writer.write_all(&byte_utils::be48_to_array(0))?;
 -                      },
 -              }
 -
 -              writer.write_all(&byte_utils::be16_to_array(self.our_to_self_delay))?;
 -              writer.write_all(&byte_utils::be16_to_array(self.their_to_self_delay.unwrap()))?;
 -
 -              for &(ref secret, ref idx) in self.old_secrets.iter() {
 -                      writer.write_all(secret)?;
 -                      writer.write_all(&byte_utils::be64_to_array(*idx))?;
 -              }
 -
 -              macro_rules! serialize_htlc_in_commitment {
 -                      ($htlc_output: expr) => {
 -                              writer.write_all(&[$htlc_output.offered as u8; 1])?;
 -                              writer.write_all(&byte_utils::be64_to_array($htlc_output.amount_msat))?;
 -                              writer.write_all(&byte_utils::be32_to_array($htlc_output.cltv_expiry))?;
 -                              writer.write_all(&$htlc_output.payment_hash.0[..])?;
 -                              $htlc_output.transaction_output_index.write(writer)?;
 -                      }
 -              }
 -
 -              writer.write_all(&byte_utils::be64_to_array(self.remote_claimable_outpoints.len() as u64))?;
 -              for (ref txid, ref htlc_infos) in self.remote_claimable_outpoints.iter() {
 -                      writer.write_all(&txid[..])?;
 -                      writer.write_all(&byte_utils::be64_to_array(htlc_infos.len() as u64))?;
 -                      for &(ref htlc_output, ref htlc_source) in htlc_infos.iter() {
 -                              serialize_htlc_in_commitment!(htlc_output);
 -                              write_option!(htlc_source);
 -                      }
 -              }
 -
 -              writer.write_all(&byte_utils::be64_to_array(self.remote_commitment_txn_on_chain.len() as u64))?;
 -              for (ref txid, &(commitment_number, ref txouts)) in self.remote_commitment_txn_on_chain.iter() {
 -                      writer.write_all(&txid[..])?;
 -                      writer.write_all(&byte_utils::be48_to_array(commitment_number))?;
 -                      (txouts.len() as u64).write(writer)?;
 -                      for script in txouts.iter() {
 -                              script.write(writer)?;
 -                      }
 -              }
 -
 -              if for_local_storage {
 -                      writer.write_all(&byte_utils::be64_to_array(self.remote_hash_commitment_number.len() as u64))?;
 -                      for (ref payment_hash, commitment_number) in self.remote_hash_commitment_number.iter() {
 -                              writer.write_all(&payment_hash.0[..])?;
 -                              writer.write_all(&byte_utils::be48_to_array(*commitment_number))?;
 -                      }
 -              } else {
 -                      writer.write_all(&byte_utils::be64_to_array(0))?;
 -              }
 -
 -              macro_rules! serialize_local_tx {
 -                      ($local_tx: expr) => {
 -                              $local_tx.tx.write(writer)?;
 -                              writer.write_all(&$local_tx.revocation_key.serialize())?;
 -                              writer.write_all(&$local_tx.a_htlc_key.serialize())?;
 -                              writer.write_all(&$local_tx.b_htlc_key.serialize())?;
 -                              writer.write_all(&$local_tx.delayed_payment_key.serialize())?;
 -                              writer.write_all(&$local_tx.per_commitment_point.serialize())?;
 -
 -                              writer.write_all(&byte_utils::be64_to_array($local_tx.feerate_per_kw))?;
 -                              writer.write_all(&byte_utils::be64_to_array($local_tx.htlc_outputs.len() as u64))?;
 -                              for &(ref htlc_output, ref sig, ref htlc_source) in $local_tx.htlc_outputs.iter() {
 -                                      serialize_htlc_in_commitment!(htlc_output);
 -                                      if let &Some(ref their_sig) = sig {
 -                                              1u8.write(writer)?;
 -                                              writer.write_all(&their_sig.serialize_compact())?;
 -                                      } else {
 -                                              0u8.write(writer)?;
 -                                      }
 -                                      write_option!(htlc_source);
 -                              }
 -                      }
 -              }
 -
 -              if let Some(ref prev_local_tx) = self.prev_local_signed_commitment_tx {
 -                      writer.write_all(&[1; 1])?;
 -                      serialize_local_tx!(prev_local_tx);
 -              } else {
 -                      writer.write_all(&[0; 1])?;
 -              }
 -
 -              if let Some(ref cur_local_tx) = self.current_local_signed_commitment_tx {
 -                      writer.write_all(&[1; 1])?;
 -                      serialize_local_tx!(cur_local_tx);
 -              } else {
 -                      writer.write_all(&[0; 1])?;
 -              }
 -
 -              if for_local_storage {
 -                      writer.write_all(&byte_utils::be48_to_array(self.current_remote_commitment_number))?;
 -              } else {
 -                      writer.write_all(&byte_utils::be48_to_array(0))?;
 -              }
 -
 -              writer.write_all(&byte_utils::be64_to_array(self.payment_preimages.len() as u64))?;
 -              for payment_preimage in self.payment_preimages.values() {
 -                      writer.write_all(&payment_preimage.0[..])?;
 -              }
 -
 -              self.last_block_hash.write(writer)?;
 -              self.destination_script.write(writer)?;
 -              if let Some((ref to_remote_script, ref local_key)) = self.to_remote_rescue {
 -                      writer.write_all(&[1; 1])?;
 -                      to_remote_script.write(writer)?;
 -                      local_key.write(writer)?;
 -              } else {
 -                      writer.write_all(&[0; 1])?;
 -              }
 -
 -              writer.write_all(&byte_utils::be64_to_array(self.pending_claim_requests.len() as u64))?;
 -              for (ref ancestor_claim_txid, claim_tx_data) in self.pending_claim_requests.iter() {
 -                      ancestor_claim_txid.write(writer)?;
 -                      claim_tx_data.write(writer)?;
 -              }
 -
 -              writer.write_all(&byte_utils::be64_to_array(self.claimable_outpoints.len() as u64))?;
 -              for (ref outp, ref claim_and_height) in self.claimable_outpoints.iter() {
 -                      outp.write(writer)?;
 -                      claim_and_height.0.write(writer)?;
 -                      claim_and_height.1.write(writer)?;
 -              }
 -
 -              writer.write_all(&byte_utils::be64_to_array(self.onchain_events_waiting_threshold_conf.len() as u64))?;
 -              for (ref target, ref events) in self.onchain_events_waiting_threshold_conf.iter() {
 -                      writer.write_all(&byte_utils::be32_to_array(**target))?;
 -                      writer.write_all(&byte_utils::be64_to_array(events.len() as u64))?;
 -                      for ev in events.iter() {
 -                              match *ev {
 -                                      OnchainEvent::Claim { ref claim_request } => {
 -                                              writer.write_all(&[0; 1])?;
 -                                              claim_request.write(writer)?;
 -                                      },
 -                                      OnchainEvent::HTLCUpdate { ref htlc_update } => {
 -                                              writer.write_all(&[1; 1])?;
 -                                              htlc_update.0.write(writer)?;
 -                                              htlc_update.1.write(writer)?;
 -                                      },
 -                                      OnchainEvent::ContentiousOutpoint { ref outpoint, ref input_material } => {
 -                                              writer.write_all(&[2; 1])?;
 -                                              outpoint.write(writer)?;
 -                                              input_material.write(writer)?;
 -                                      }
 -                              }
 -                      }
 -              }
 -
 -              Ok(())
 -      }
 -
 -      /// Writes this monitor into the given writer, suitable for writing to disk.
 -      ///
 -      /// Note that the deserializer is only implemented for (Sha256dHash, ChannelMonitor), which
 -      /// tells you the last block hash which was block_connect()ed. You MUST rescan any blocks along
 -      /// the "reorg path" (ie not just starting at the same height but starting at the highest
 -      /// common block that appears on your best chain as well as on the chain which contains the
 -      /// last block hash returned) upon deserializing the object!
 -      pub fn write_for_disk<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
 -              self.write(writer, true)
 -      }
 -
 -      /// Encodes this monitor into the given writer, suitable for sending to a remote watchtower
 -      ///
 -      /// Note that the deserializer is only implemented for (Sha256dHash, ChannelMonitor), which
 -      /// tells you the last block hash which was block_connect()ed. You MUST rescan any blocks along
 -      /// the "reorg path" (ie not just starting at the same height but starting at the highest
 -      /// common block that appears on your best chain as well as on the chain which contains the
 -      /// last block hash returned) upon deserializing the object!
 -      pub fn write_for_watchtower<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
 -              self.write(writer, false)
 -      }
 -
        /// Can only fail if idx is < get_min_seen_secret
        pub(super) fn get_secret(&self, idx: u64) -> Option<[u8; 32]> {
                for i in 0..self.old_secrets.len() {
                        if (idx & (!((1 << i) - 1))) == self.old_secrets[i].1 {
 -                              return Some(ChannelMonitor::derive_secret(self.old_secrets[i].0, i as u8, idx))
 +                              return Some(ChannelMonitor::<ChanSigner>::derive_secret(self.old_secrets[i].0, i as u8, idx))
                        }
                }
                assert!(idx < self.get_min_seen_secret());
                        let secret = self.get_secret(commitment_number).unwrap();
                        let per_commitment_key = ignore_error!(SecretKey::from_slice(&secret));
                        let (revocation_pubkey, b_htlc_key, local_payment_key) = match self.key_storage {
 -                              Storage::Local { ref revocation_base_key, ref htlc_base_key, ref payment_base_key, .. } => {
 +                              Storage::Local { ref keys, ref payment_base_key, .. } => {
                                        let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key);
 -                                      (ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &PublicKey::from_secret_key(&self.secp_ctx, &revocation_base_key))),
 -                                      ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &per_commitment_point, &PublicKey::from_secret_key(&self.secp_ctx, &htlc_base_key))),
 +                                      (ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &keys.pubkeys().revocation_basepoint)),
 +                                      ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &per_commitment_point, &keys.pubkeys().htlc_basepoint)),
                                        Some(ignore_error!(chan_utils::derive_private_key(&self.secp_ctx, &per_commitment_point, &payment_base_key))))
                                },
                                Storage::Watchtower { ref revocation_base_key, ref htlc_base_key, .. } => {
                                        } else { None };
                                if let Some(revocation_point) = revocation_point_option {
                                        let (revocation_pubkey, b_htlc_key) = match self.key_storage {
 -                                              Storage::Local { ref revocation_base_key, ref htlc_base_key, .. } => {
 -                                                      (ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, revocation_point, &PublicKey::from_secret_key(&self.secp_ctx, &revocation_base_key))),
 -                                                      ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, revocation_point, &PublicKey::from_secret_key(&self.secp_ctx, &htlc_base_key))))
 +                                              Storage::Local { ref keys, .. } => {
 +                                                      (ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, revocation_point, &keys.pubkeys().revocation_basepoint)),
 +                                                      ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, revocation_point, &keys.pubkeys().htlc_basepoint)))
                                                },
                                                Storage::Watchtower { ref revocation_base_key, ref htlc_base_key, .. } => {
                                                        (ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, revocation_point, &revocation_base_key)),
                let per_commitment_key = ignore_error!(SecretKey::from_slice(&secret));
                let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key);
                let revocation_pubkey = match self.key_storage {
 -                      Storage::Local { ref revocation_base_key, .. } => {
 -                              ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &PublicKey::from_secret_key(&self.secp_ctx, &revocation_base_key)))
 +                      Storage::Local { ref keys, .. } => {
 +                              ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &keys.pubkeys().revocation_basepoint))
                        },
                        Storage::Watchtower { ref revocation_base_key, .. } => {
                                ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &revocation_base_key))
                        assert!(predicted_weight >= spend_tx.get_weight());
                        let outpoint = BitcoinOutPoint { txid: spend_tx.txid(), vout: 0 };
                        let output = spend_tx.output[0].clone();
 -                      let height_timer = Self::get_height_timer(height, self.their_to_self_delay.unwrap() as u32); // We can safely unwrap given we are past channel opening
 +                      let height_timer = Self::get_height_timer(height, height + self.our_to_self_delay as u32);
                        log_trace!(self, "Outpoint {}:{} is being being claimed, if it doesn't succeed, a bumped claiming txn is going to be broadcast at height {}", spend_tx.input[0].previous_output.txid, spend_tx.input[0].previous_output.vout, height_timer);
                        let mut per_input_material = HashMap::with_capacity(1);
                        per_input_material.insert(spend_tx.input[0].previous_output, InputMaterial::Revoked { script: redeemscript, pubkey: None, key: revocation_key, is_htlc: false, amount: tx.output[0].value });
                                assert!(local_tx.tx.has_local_sig());
                                match self.key_storage {
                                        Storage::Local { ref delayed_payment_base_key, .. } => {
 -                                              append_onchain_update!(self.broadcast_by_local_state(local_tx, delayed_payment_base_key, height));
 +                                              let mut res = self.broadcast_by_local_state(local_tx, delayed_payment_base_key, height);
 +                                              append_onchain_update!(res);
                                        },
                                        Storage::Watchtower { .. } => { }
                                }
                                assert!(local_tx.tx.has_local_sig());
                                match self.key_storage {
                                        Storage::Local { ref delayed_payment_base_key, .. } => {
 -                                              append_onchain_update!(self.broadcast_by_local_state(local_tx, delayed_payment_base_key, height));
 +                                              let mut res = self.broadcast_by_local_state(local_tx, delayed_payment_base_key, height);
 +                                              append_onchain_update!(res);
                                        },
                                        Storage::Watchtower { .. } => { }
                                }
        }
  
        fn block_connected(&mut self, txn_matched: &[&Transaction], height: u32, block_hash: &Sha256dHash, broadcaster: &BroadcasterInterface, fee_estimator: &FeeEstimator)-> (Vec<(Sha256dHash, Vec<TxOut>)>, Vec<SpendableOutputDescriptor>, Vec<(HTLCSource, Option<PaymentPreimage>, PaymentHash)>) {
+               for tx in txn_matched {
+                       let mut output_val = 0;
+                       for out in tx.output.iter() {
+                               if out.value > 21_000_000_0000_0000 { panic!("Value-overflowing transaction provided to block connected"); }
+                               output_val += out.value;
+                               if output_val > 21_000_000_0000_0000 { panic!("Value-overflowing transaction provided to block connected"); }
+                       }
+               }
                log_trace!(self, "Block {} at height {} connected with {} txn matched", block_hash, height, txn_matched.len());
                let mut watch_outputs = Vec::new();
                let mut spendable_outputs = Vec::new();
  
  const MAX_ALLOC_SIZE: usize = 64*1024;
  
 -impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelMonitor) {
 +impl<R: ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelMonitor<ChanSigner>) {
        fn read(reader: &mut R, logger: Arc<Logger>) -> Result<Self, DecodeError> {
                let secp_ctx = Secp256k1::new();
                macro_rules! unwrap_obj {
  
                let key_storage = match <u8 as Readable<R>>::read(reader)? {
                        0 => {
 +                              let keys = Readable::read(reader)?;
                                let funding_key = Readable::read(reader)?;
                                let revocation_base_key = Readable::read(reader)?;
                                let htlc_base_key = Readable::read(reader)?;
                                let current_remote_commitment_txid = Readable::read(reader)?;
                                let prev_remote_commitment_txid = Readable::read(reader)?;
                                Storage::Local {
 +                                      keys,
                                        funding_key,
                                        revocation_base_key,
                                        htlc_base_key,
@@@ -3295,14 -3273,12 +3304,14 @@@ mod tests 
        use secp256k1::Secp256k1;
        use rand::{thread_rng,Rng};
        use std::sync::Arc;
 +      use chain::keysinterface::InMemoryChannelKeys;
 +
  
        #[test]
        fn test_per_commitment_storage() {
                // Test vectors from BOLT 3:
                let mut secrets: Vec<[u8; 32]> = Vec::new();
 -              let mut monitor: ChannelMonitor;
 +              let mut monitor: ChannelMonitor<InMemoryChannelKeys>;
                let secp_ctx = Secp256k1::new();
                let logger = Arc::new(TestLogger::new());
  
                        };
                }
  
 +              let keys = InMemoryChannelKeys::new(
 +                      &secp_ctx,
 +                      SecretKey::from_slice(&[41; 32]).unwrap(),
 +                      SecretKey::from_slice(&[41; 32]).unwrap(),
 +                      SecretKey::from_slice(&[41; 32]).unwrap(),
 +                      SecretKey::from_slice(&[41; 32]).unwrap(),
 +                      SecretKey::from_slice(&[41; 32]).unwrap(),
 +                      [41; 32],
 +                      0,
 +              );
 +
                {
                        // insert_secret correct sequence
 -                      monitor = ChannelMonitor::new(&SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
 +                      monitor = ChannelMonitor::new(keys.clone(), &SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
                        secrets.clear();
  
                        secrets.push([0; 32]);
  
                {
                        // insert_secret #1 incorrect
 -                      monitor = ChannelMonitor::new(&SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
 +                      monitor = ChannelMonitor::new(keys.clone(), &SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
                        secrets.clear();
  
                        secrets.push([0; 32]);
  
                {
                        // insert_secret #2 incorrect (#1 derived from incorrect)
 -                      monitor = ChannelMonitor::new(&SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
 +                      monitor = ChannelMonitor::new(keys.clone(), &SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
                        secrets.clear();
  
                        secrets.push([0; 32]);
  
                {
                        // insert_secret #3 incorrect
 -                      monitor = ChannelMonitor::new(&SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
 +                      monitor = ChannelMonitor::new(keys.clone(), &SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
                        secrets.clear();
  
                        secrets.push([0; 32]);
  
                {
                        // insert_secret #4 incorrect (1,2,3 derived from incorrect)
 -                      monitor = ChannelMonitor::new(&SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
 +                      monitor = ChannelMonitor::new(keys.clone(), &SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
                        secrets.clear();
  
                        secrets.push([0; 32]);
  
                {
                        // insert_secret #5 incorrect
 -                      monitor = ChannelMonitor::new(&SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
 +                      monitor = ChannelMonitor::new(keys.clone(), &SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
                        secrets.clear();
  
                        secrets.push([0; 32]);
  
                {
                        // insert_secret #6 incorrect (5 derived from incorrect)
 -                      monitor = ChannelMonitor::new(&SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
 +                      monitor = ChannelMonitor::new(keys.clone(), &SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
                        secrets.clear();
  
                        secrets.push([0; 32]);
  
                {
                        // insert_secret #7 incorrect
 -                      monitor = ChannelMonitor::new(&SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
 +                      monitor = ChannelMonitor::new(keys.clone(), &SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
                        secrets.clear();
  
                        secrets.push([0; 32]);
  
                {
                        // insert_secret #8 incorrect
 -                      monitor = ChannelMonitor::new(&SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
 +                      monitor = ChannelMonitor::new(keys.clone(), &SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
                        secrets.clear();
  
                        secrets.push([0; 32]);
                        }
                }
  
 +              let keys = InMemoryChannelKeys::new(
 +                      &secp_ctx,
 +                      SecretKey::from_slice(&[41; 32]).unwrap(),
 +                      SecretKey::from_slice(&[41; 32]).unwrap(),
 +                      SecretKey::from_slice(&[41; 32]).unwrap(),
 +                      SecretKey::from_slice(&[41; 32]).unwrap(),
 +                      SecretKey::from_slice(&[41; 32]).unwrap(),
 +                      [41; 32],
 +                      0,
 +              );
 +
                // Prune with one old state and a local commitment tx holding a few overlaps with the
                // old state.
 -              let mut monitor = ChannelMonitor::new(&SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
 +              let mut monitor = ChannelMonitor::new(keys, &SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
                monitor.their_to_self_delay = Some(10);
  
                monitor.provide_latest_local_commitment_tx_info(LocalCommitmentTransaction::dummy(), dummy_keys!(), 0, preimages_to_local_htlcs!(preimages[0..10]));
                for (idx, inp) in claim_tx.input.iter_mut().zip(inputs_des.iter()).enumerate() {
                        sign_input!(sighash_parts, inp.0, idx as u32, 0, inp.1, sum_actual_sigs);
                }
 -              assert_eq!(base_weight + ChannelMonitor::get_witnesses_weight(&inputs_des[..]),  claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() - sum_actual_sigs));
 +              assert_eq!(base_weight + ChannelMonitor::<InMemoryChannelKeys>::get_witnesses_weight(&inputs_des[..]),  claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() - sum_actual_sigs));
  
                // Claim tx with 1 offered HTLCs, 3 received HTLCs
                claim_tx.input.clear();
                for (idx, inp) in claim_tx.input.iter_mut().zip(inputs_des.iter()).enumerate() {
                        sign_input!(sighash_parts, inp.0, idx as u32, 0, inp.1, sum_actual_sigs);
                }
 -              assert_eq!(base_weight + ChannelMonitor::get_witnesses_weight(&inputs_des[..]),  claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() - sum_actual_sigs));
 +              assert_eq!(base_weight + ChannelMonitor::<InMemoryChannelKeys>::get_witnesses_weight(&inputs_des[..]),  claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() - sum_actual_sigs));
  
                // Justice tx with 1 revoked HTLC-Success tx output
                claim_tx.input.clear();
                for (idx, inp) in claim_tx.input.iter_mut().zip(inputs_des.iter()).enumerate() {
                        sign_input!(sighash_parts, inp.0, idx as u32, 0, inp.1, sum_actual_sigs);
                }
 -              assert_eq!(base_weight + ChannelMonitor::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_isg */ (73 * inputs_des.len() - sum_actual_sigs));
 +              assert_eq!(base_weight + ChannelMonitor::<InMemoryChannelKeys>::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_isg */ (73 * inputs_des.len() - sum_actual_sigs));
        }
  
        // Further testing is done in the ChannelManager integration tests.