From: Matt Corallo <649246+TheBlueMatt@users.noreply.github.com> Date: Wed, 13 Jan 2021 22:40:41 +0000 (-0800) Subject: Merge pull request #771 from jkczyz/2021-01-bolt3-test-vectors X-Git-Tag: v0.0.13~46 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=d529a8827bd860ecfef73059cd5097e778f63ddc;hp=caa16e0fb549db56e432f84cc902cf1835a8e360;p=rust-lightning Merge pull request #771 from jkczyz/2021-01-bolt3-test-vectors Add BOLT 3 test vector for CLTV tiebreaker --- diff --git a/fuzz/src/chanmon_consistency.rs b/fuzz/src/chanmon_consistency.rs index 377ac8d7..5466414b 100644 --- a/fuzz/src/chanmon_consistency.rs +++ b/fuzz/src/chanmon_consistency.rs @@ -37,7 +37,7 @@ use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, use lightning::chain::keysinterface::{KeysInterface, InMemoryChannelKeys}; use lightning::ln::channelmanager::{ChannelManager, PaymentHash, PaymentPreimage, PaymentSecret, PaymentSendFailure, ChannelManagerReadArgs}; use lightning::ln::features::{ChannelFeatures, InitFeatures, NodeFeatures}; -use lightning::ln::msgs::{CommitmentUpdate, ChannelMessageHandler, ErrorAction, UpdateAddHTLC, Init}; +use lightning::ln::msgs::{CommitmentUpdate, ChannelMessageHandler, DecodeError, ErrorAction, UpdateAddHTLC, Init}; use lightning::util::enforcing_trait_impls::EnforcingChannelKeys; use lightning::util::errors::APIError; use lightning::util::events; @@ -45,6 +45,7 @@ use lightning::util::logger::Logger; use lightning::util::config::UserConfig; use lightning::util::events::{EventsProvider, MessageSendEventsProvider}; use lightning::util::ser::{Readable, ReadableArgs, Writeable, Writer}; +use lightning::util::test_utils::OnlyReadsKeysInterface; use lightning::routing::router::{Route, RouteHop}; @@ -112,7 +113,7 @@ impl chain::Watch for TestChainMonitor { fn watch_channel(&self, funding_txo: OutPoint, monitor: channelmonitor::ChannelMonitor) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> { let mut ser = VecWriter(Vec::new()); - monitor.serialize_for_disk(&mut ser).unwrap(); + monitor.write(&mut ser).unwrap(); if let Some(_) = self.latest_monitors.lock().unwrap().insert(funding_txo, (monitor.get_latest_update_id(), ser.0)) { panic!("Already had monitor pre-watch_channel"); } @@ -128,10 +129,10 @@ impl chain::Watch for TestChainMonitor { hash_map::Entry::Vacant(_) => panic!("Didn't have monitor on update call"), }; let mut deserialized_monitor = <(BlockHash, channelmonitor::ChannelMonitor)>:: - read(&mut Cursor::new(&map_entry.get().1)).unwrap().1; + read(&mut Cursor::new(&map_entry.get().1), &OnlyReadsKeysInterface {}).unwrap().1; deserialized_monitor.update_monitor(&update, &&TestBroadcaster{}, &&FuzzEstimator{}, &self.logger).unwrap(); let mut ser = VecWriter(Vec::new()); - deserialized_monitor.serialize_for_disk(&mut ser).unwrap(); + deserialized_monitor.write(&mut ser).unwrap(); map_entry.insert((update.update_id, ser.0)); self.should_update_manager.store(true, atomic::Ordering::Relaxed); self.update_ret.lock().unwrap().clone() @@ -184,6 +185,10 @@ impl KeysInterface for KeyProvider { let id = self.rand_bytes_id.fetch_add(1, atomic::Ordering::Relaxed); [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, id, 11, self.node_id] } + + fn read_chan_signer(&self, data: &[u8]) -> Result { + EnforcingChannelKeys::read(&mut std::io::Cursor::new(data)) + } } #[inline] @@ -307,7 +312,7 @@ pub fn do_test(data: &[u8], out: Out) { let mut monitors = HashMap::new(); let mut old_monitors = $old_monitors.latest_monitors.lock().unwrap(); for (outpoint, (update_id, monitor_ser)) in old_monitors.drain() { - monitors.insert(outpoint, <(BlockHash, ChannelMonitor)>::read(&mut Cursor::new(&monitor_ser)).expect("Failed to read monitor").1); + monitors.insert(outpoint, <(BlockHash, ChannelMonitor)>::read(&mut Cursor::new(&monitor_ser), &OnlyReadsKeysInterface {}).expect("Failed to read monitor").1); chain_monitor.latest_monitors.lock().unwrap().insert(outpoint, (update_id, monitor_ser)); } let mut monitor_refs = HashMap::new(); diff --git a/fuzz/src/chanmon_deser.rs b/fuzz/src/chanmon_deser.rs index fd326cc2..596ff275 100644 --- a/fuzz/src/chanmon_deser.rs +++ b/fuzz/src/chanmon_deser.rs @@ -5,7 +5,8 @@ use bitcoin::hash_types::BlockHash; use lightning::chain::channelmonitor; use lightning::util::enforcing_trait_impls::EnforcingChannelKeys; -use lightning::util::ser::{Readable, Writer}; +use lightning::util::ser::{ReadableArgs, Writer, Writeable}; +use lightning::util::test_utils::OnlyReadsKeysInterface; use utils::test_logger; @@ -24,10 +25,10 @@ impl Writer for VecWriter { #[inline] pub fn do_test(data: &[u8], _out: Out) { - if let Ok((latest_block_hash, monitor)) = <(BlockHash, channelmonitor::ChannelMonitor)>::read(&mut Cursor::new(data)) { + if let Ok((latest_block_hash, monitor)) = <(BlockHash, channelmonitor::ChannelMonitor)>::read(&mut Cursor::new(data), &OnlyReadsKeysInterface {}) { let mut w = VecWriter(Vec::new()); - monitor.serialize_for_disk(&mut w).unwrap(); - let deserialized_copy = <(BlockHash, channelmonitor::ChannelMonitor)>::read(&mut Cursor::new(&w.0)).unwrap(); + monitor.write(&mut w).unwrap(); + let deserialized_copy = <(BlockHash, channelmonitor::ChannelMonitor)>::read(&mut Cursor::new(&w.0), &OnlyReadsKeysInterface {}).unwrap(); assert!(latest_block_hash == deserialized_copy.0); assert!(monitor == deserialized_copy.1); } diff --git a/fuzz/src/full_stack.rs b/fuzz/src/full_stack.rs index 6375d076..915a6cba 100644 --- a/fuzz/src/full_stack.rs +++ b/fuzz/src/full_stack.rs @@ -33,12 +33,14 @@ use lightning::chain::transaction::OutPoint; use lightning::chain::keysinterface::{InMemoryChannelKeys, KeysInterface}; use lightning::ln::channelmanager::{ChannelManager, PaymentHash, PaymentPreimage, PaymentSecret}; use lightning::ln::peer_handler::{MessageHandler,PeerManager,SocketDescriptor}; +use lightning::ln::msgs::DecodeError; use lightning::routing::router::get_route; use lightning::routing::network_graph::NetGraphMsgHandler; +use lightning::util::config::UserConfig; use lightning::util::events::{EventsProvider,Event}; use lightning::util::enforcing_trait_impls::EnforcingChannelKeys; use lightning::util::logger::Logger; -use lightning::util::config::UserConfig; +use lightning::util::ser::Readable; use utils::test_logger; use utils::test_persister::TestPersister; @@ -299,6 +301,10 @@ impl KeysInterface for KeyProvider { [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, (ctr >> 8*7) as u8, (ctr >> 8*6) as u8, (ctr >> 8*5) as u8, (ctr >> 8*4) as u8, (ctr >> 8*3) as u8, (ctr >> 8*2) as u8, (ctr >> 8*1) as u8, 14, (ctr >> 8*0) as u8] } + + fn read_chan_signer(&self, data: &[u8]) -> Result { + EnforcingChannelKeys::read(&mut std::io::Cursor::new(data)) + } } #[inline] diff --git a/lightning-persister/src/lib.rs b/lightning-persister/src/lib.rs index 48d4d0aa..bb6eb54c 100644 --- a/lightning-persister/src/lib.rs +++ b/lightning-persister/src/lib.rs @@ -7,13 +7,15 @@ use lightning::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, Cha use lightning::chain::channelmonitor; use lightning::chain::keysinterface::ChannelKeys; use lightning::chain::transaction::OutPoint; -use lightning::util::ser::{Writeable, Readable}; +use lightning::util::ser::Writeable; use std::fs; use std::io::Error; use std::path::{Path, PathBuf}; #[cfg(test)] use { + lightning::chain::keysinterface::KeysInterface, + lightning::util::ser::ReadableArgs, bitcoin::{BlockHash, Txid}, bitcoin::hashes::hex::FromHex, std::collections::HashMap, @@ -43,9 +45,9 @@ trait DiskWriteable { fn write(&self, writer: &mut fs::File) -> Result<(), Error>; } -impl DiskWriteable for ChannelMonitor { +impl DiskWriteable for ChannelMonitor { fn write(&self, writer: &mut fs::File) -> Result<(), Error> { - self.serialize_for_disk(writer) + Writeable::write(self, writer) } } @@ -94,8 +96,8 @@ impl FilesystemPersister { } #[cfg(test)] - fn load_channel_data(&self) -> - Result>, ChannelMonitorUpdateErr> { + fn load_channel_data(&self, keys: &Keys) -> + Result>, ChannelMonitorUpdateErr> { if let Err(_) = fs::create_dir_all(&self.path_to_channel_data) { return Err(ChannelMonitorUpdateErr::PermanentFailure); } @@ -118,7 +120,7 @@ impl FilesystemPersister { if contents.is_err() { return Err(ChannelMonitorUpdateErr::PermanentFailure); } if let Ok((_, loaded_monitor)) = - <(BlockHash, ChannelMonitor)>::read(&mut Cursor::new(&contents.unwrap())) { + <(BlockHash, ChannelMonitor)>::read(&mut Cursor::new(&contents.unwrap()), keys) { res.insert(OutPoint { txid: txid.unwrap(), index: index.unwrap() }, loaded_monitor); } else { return Err(ChannelMonitorUpdateErr::PermanentFailure); @@ -128,7 +130,7 @@ impl FilesystemPersister { } } -impl channelmonitor::Persist for FilesystemPersister { +impl channelmonitor::Persist for FilesystemPersister { fn persist_new_channel(&self, funding_txo: OutPoint, monitor: &ChannelMonitor) -> Result<(), ChannelMonitorUpdateErr> { self.write_channel_data(funding_txo, monitor) .map_err(|_| ChannelMonitorUpdateErr::PermanentFailure) @@ -168,7 +170,6 @@ mod tests { use lightning::ln::features::InitFeatures; use lightning::ln::functional_test_utils::*; use lightning::ln::msgs::ErrorAction; - use lightning::util::enforcing_trait_impls::EnforcingChannelKeys; use lightning::util::events::{MessageSendEventsProvider, MessageSendEvent}; use lightning::util::ser::Writer; use lightning::util::test_utils; @@ -206,20 +207,20 @@ mod tests { // Check that the persisted channel data is empty before any channels are // open. - let mut persisted_chan_data_0 = persister_0.load_channel_data::().unwrap(); + let mut persisted_chan_data_0 = persister_0.load_channel_data(nodes[0].keys_manager).unwrap(); assert_eq!(persisted_chan_data_0.keys().len(), 0); - let mut persisted_chan_data_1 = persister_1.load_channel_data::().unwrap(); + let mut persisted_chan_data_1 = persister_1.load_channel_data(nodes[1].keys_manager).unwrap(); assert_eq!(persisted_chan_data_1.keys().len(), 0); // Helper to make sure the channel is on the expected update ID. macro_rules! check_persisted_data { ($expected_update_id: expr) => { - persisted_chan_data_0 = persister_0.load_channel_data::().unwrap(); + persisted_chan_data_0 = persister_0.load_channel_data(nodes[0].keys_manager).unwrap(); assert_eq!(persisted_chan_data_0.keys().len(), 1); for mon in persisted_chan_data_0.values() { assert_eq!(mon.get_latest_update_id(), $expected_update_id); } - persisted_chan_data_1 = persister_1.load_channel_data::().unwrap(); + persisted_chan_data_1 = persister_1.load_channel_data(nodes[1].keys_manager).unwrap(); assert_eq!(persisted_chan_data_1.keys().len(), 1); for mon in persisted_chan_data_1.values() { assert_eq!(mon.get_latest_update_id(), $expected_update_id); diff --git a/lightning/Cargo.toml b/lightning/Cargo.toml index 2d7d95dd..016399f8 100644 --- a/lightning/Cargo.toml +++ b/lightning/Cargo.toml @@ -11,7 +11,7 @@ Still missing tons of error-handling. See GitHub issues for suggested projects i """ [features] -fuzztarget = ["bitcoin/fuzztarget"] +fuzztarget = ["bitcoin/fuzztarget", "regex"] # Internal test utilities exposed to other repo crates _test_utils = ["hex", "regex"] # Unlog messages superior at targeted level. diff --git a/lightning/src/chain/channelmonitor.rs b/lightning/src/chain/channelmonitor.rs index d85ac51d..b87e73d9 100644 --- a/lightning/src/chain/channelmonitor.rs +++ b/lightning/src/chain/channelmonitor.rs @@ -43,9 +43,9 @@ use ln::channelmanager::{HTLCSource, PaymentPreimage, PaymentHash}; use ln::onchaintx::{OnchainTxHandler, InputDescriptors}; use chain::chaininterface::{BroadcasterInterface, FeeEstimator}; use chain::transaction::{OutPoint, TransactionData}; -use chain::keysinterface::{SpendableOutputDescriptor, ChannelKeys}; +use chain::keysinterface::{SpendableOutputDescriptor, ChannelKeys, KeysInterface}; use util::logger::Logger; -use util::ser::{Readable, MaybeReadable, Writer, Writeable, U48}; +use util::ser::{Readable, ReadableArgs, MaybeReadable, Writer, Writeable, U48}; use util::byte_utils; use util::events::Event; @@ -56,7 +56,7 @@ use std::io::Error; /// An update generated by the underlying Channel itself which contains some new information the /// ChannelMonitor should be made aware of. -#[cfg_attr(any(test, feature = "_test_utils"), derive(PartialEq))] +#[cfg_attr(any(test, feature = "fuzztarget", feature = "_test_utils"), derive(PartialEq))] #[derive(Clone)] #[must_use] pub struct ChannelMonitorUpdate { @@ -485,7 +485,7 @@ enum OnchainEvent { const SERIALIZATION_VERSION: u8 = 1; const MIN_SERIALIZATION_VERSION: u8 = 1; -#[cfg_attr(any(test, feature = "_test_utils"), derive(PartialEq))] +#[cfg_attr(any(test, feature = "fuzztarget", feature = "_test_utils"), derive(PartialEq))] #[derive(Clone)] pub(crate) enum ChannelMonitorUpdateStep { LatestHolderCommitmentTXInfo { @@ -617,6 +617,12 @@ impl Readable for ChannelMonitorUpdateStep { /// get_and_clear_pending_monitor_events or get_and_clear_pending_events are serialized to disk and /// reloaded at deserialize-time. Thus, you must ensure that, when handling events, all events /// gotten are fully handled before re-serializing the new state. +/// +/// 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 disconnecting blocks until you find a common ancestor from both the +/// returned block hash and the the current chain and then reconnecting blocks to get to the +/// best chain) upon deserializing the object! pub struct ChannelMonitor { latest_update_id: u64, commitment_transaction_number_obscure_factor: u64, @@ -626,7 +632,8 @@ pub struct ChannelMonitor { counterparty_payment_script: Script, shutdown_script: Script, - keys: ChanSigner, + key_derivation_params: (u64, u64), + holder_revocation_basepoint: PublicKey, funding_info: (OutPoint, Script), current_counterparty_commitment_txid: Option, prev_counterparty_commitment_txid: Option, @@ -721,7 +728,8 @@ impl PartialEq for ChannelMonitor { self.destination_script != other.destination_script || self.broadcasted_holder_revokable_script != other.broadcasted_holder_revokable_script || self.counterparty_payment_script != other.counterparty_payment_script || - self.keys.pubkeys() != other.keys.pubkeys() || + self.key_derivation_params != other.key_derivation_params || + self.holder_revocation_basepoint != other.holder_revocation_basepoint || self.funding_info != other.funding_info || self.current_counterparty_commitment_txid != other.current_counterparty_commitment_txid || self.prev_counterparty_commitment_txid != other.prev_counterparty_commitment_txid || @@ -753,15 +761,8 @@ impl PartialEq for ChannelMonitor { } } -impl ChannelMonitor { - /// 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 disconnecting blocks until you find a common ancestor from both the - /// returned block hash and the the current chain and then reconnecting blocks to get to the - /// best chain) upon deserializing the object! - pub fn serialize_for_disk(&self, writer: &mut W) -> Result<(), Error> { +impl Writeable for ChannelMonitor { + fn write(&self, writer: &mut W) -> Result<(), 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])?; @@ -785,7 +786,8 @@ impl ChannelMonitor { self.counterparty_payment_script.write(writer)?; self.shutdown_script.write(writer)?; - self.keys.write(writer)?; + self.key_derivation_params.write(writer)?; + self.holder_revocation_basepoint.write(writer)?; writer.write_all(&self.funding_info.0.txid[..])?; writer.write_all(&byte_utils::be16_to_array(self.funding_info.0.index))?; self.funding_info.1.write(writer)?; @@ -965,7 +967,9 @@ impl ChannelMonitor { let counterparty_htlc_base_key = counterparty_channel_parameters.pubkeys.htlc_basepoint; let counterparty_tx_cache = CounterpartyCommitmentTransaction { counterparty_delayed_payment_base_key, counterparty_htlc_base_key, on_counterparty_tx_csv, per_htlc: HashMap::new() }; - let mut onchain_tx_handler = OnchainTxHandler::new(destination_script.clone(), keys.clone(), channel_parameters.clone()); + let key_derivation_params = keys.key_derivation_params(); + let holder_revocation_basepoint = keys.pubkeys().revocation_basepoint; + let mut onchain_tx_handler = OnchainTxHandler::new(destination_script.clone(), keys, channel_parameters.clone()); let secp_ctx = Secp256k1::new(); @@ -1001,7 +1005,8 @@ impl ChannelMonitor { counterparty_payment_script, shutdown_script, - keys, + key_derivation_params, + holder_revocation_basepoint, funding_info, current_counterparty_commitment_txid: None, prev_counterparty_commitment_txid: None, @@ -1373,7 +1378,7 @@ impl ChannelMonitor { let secret = self.get_secret(commitment_number).unwrap(); 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 = ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &self.keys.pubkeys().revocation_basepoint)); + let revocation_pubkey = ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &self.holder_revocation_basepoint)); let delayed_key = ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key), &self.counterparty_tx_cache.counterparty_delayed_payment_base_key)); let revokeable_redeemscript = chan_utils::get_revokeable_redeemscript(&revocation_pubkey, self.counterparty_tx_cache.on_counterparty_tx_csv, &delayed_key); @@ -2205,7 +2210,7 @@ impl ChannelMonitor { per_commitment_point: broadcasted_holder_revokable_script.1, to_self_delay: self.on_holder_tx_csv, output: outp.clone(), - key_derivation_params: self.keys.key_derivation_params(), + key_derivation_params: self.key_derivation_params, revocation_pubkey: broadcasted_holder_revokable_script.2.clone(), }); break; @@ -2214,7 +2219,7 @@ impl ChannelMonitor { spendable_output = Some(SpendableOutputDescriptor::StaticOutputCounterpartyPayment { outpoint: OutPoint { txid: tx.txid(), index: i as u16 }, output: outp.clone(), - key_derivation_params: self.keys.key_derivation_params(), + key_derivation_params: self.key_derivation_params, }); break; } else if outp.script_pubkey == self.shutdown_script { @@ -2296,8 +2301,9 @@ pub trait Persist: Send + Sync { const MAX_ALLOC_SIZE: usize = 64*1024; -impl Readable for (BlockHash, ChannelMonitor) { - fn read(reader: &mut R) -> Result { +impl<'a, ChanSigner: ChannelKeys, K: KeysInterface> ReadableArgs<&'a K> + for (BlockHash, ChannelMonitor) { + fn read(reader: &mut R, keys_manager: &'a K) -> Result { macro_rules! unwrap_obj { ($key: expr) => { match $key { @@ -2330,7 +2336,8 @@ impl Readable for (BlockHash, ChannelMonitor let counterparty_payment_script = Readable::read(reader)?; let shutdown_script = Readable::read(reader)?; - let keys = Readable::read(reader)?; + let key_derivation_params = Readable::read(reader)?; + let holder_revocation_basepoint = Readable::read(reader)?; // Technically this can fail and serialize fail a round-trip, but only for serialization of // barely-init'd ChannelMonitors that we can't do anything with. let outpoint = OutPoint { @@ -2530,7 +2537,7 @@ impl Readable for (BlockHash, ChannelMonitor return Err(DecodeError::InvalidValue); } } - let onchain_tx_handler = Readable::read(reader)?; + let onchain_tx_handler = ReadableArgs::read(reader, keys_manager)?; let lockdown_from_offchain = Readable::read(reader)?; let holder_tx_signed = Readable::read(reader)?; @@ -2544,7 +2551,8 @@ impl Readable for (BlockHash, ChannelMonitor counterparty_payment_script, shutdown_script, - keys, + key_derivation_params, + holder_revocation_basepoint, funding_info, current_counterparty_commitment_txid, prev_counterparty_commitment_txid, diff --git a/lightning/src/chain/keysinterface.rs b/lightning/src/chain/keysinterface.rs index 085a2f87..4a3a937a 100644 --- a/lightning/src/chain/keysinterface.rs +++ b/lightning/src/chain/keysinterface.rs @@ -204,7 +204,7 @@ impl Readable for SpendableOutputDescriptor { // routine). // TODO: We should remove Clone by instead requesting a new ChannelKeys copy when we create // ChannelMonitors instead of expecting to clone the one out of the Channel into the monitors. -pub trait ChannelKeys : Send+Clone { +pub trait ChannelKeys : Send+Clone + Writeable { /// Gets the per-commitment point for a specific commitment number /// /// Note that the commitment number starts at (1 << 48) - 1 and counts backwards. @@ -344,6 +344,14 @@ pub trait KeysInterface: Send + Sync { /// onion packets and for temporary channel IDs. There is no requirement that these be /// persisted anywhere, though they must be unique across restarts. fn get_secure_random_bytes(&self) -> [u8; 32]; + + /// Reads a `ChanKeySigner` for this `KeysInterface` from the given input stream. + /// This is only called during deserialization of other objects which contain + /// `ChannelKeys`-implementing objects (ie `ChannelMonitor`s and `ChannelManager`s). + /// The bytes are exactly those which `::write()` writes, and + /// contain no versioning scheme. You may wish to include your own version prefix and ensure + /// you've read all of the provided bytes to ensure no corruption occurred. + fn read_chan_signer(&self, reader: &[u8]) -> Result; } #[derive(Clone)] @@ -809,4 +817,8 @@ impl KeysInterface for KeysManager { sha.input(b"Unique Secure Random Bytes Salt"); Sha256::from_engine(sha).into_inner() } + + fn read_chan_signer(&self, reader: &[u8]) -> Result { + InMemoryChannelKeys::read(&mut std::io::Cursor::new(reader)) + } } diff --git a/lightning/src/lib.rs b/lightning/src/lib.rs index c5d36926..e466205c 100644 --- a/lightning/src/lib.rs +++ b/lightning/src/lib.rs @@ -29,7 +29,7 @@ extern crate bitcoin; #[cfg(any(test, feature = "_test_utils"))] extern crate hex; -#[cfg(any(test, feature = "_test_utils"))] extern crate regex; +#[cfg(any(test, feature = "fuzztarget", feature = "_test_utils"))] extern crate regex; #[macro_use] pub mod util; diff --git a/lightning/src/ln/chanmon_update_fail_tests.rs b/lightning/src/ln/chanmon_update_fail_tests.rs index 689c3496..b9376029 100644 --- a/lightning/src/ln/chanmon_update_fail_tests.rs +++ b/lightning/src/ln/chanmon_update_fail_tests.rs @@ -26,7 +26,7 @@ use routing::router::get_route; use util::enforcing_trait_impls::EnforcingChannelKeys; use util::events::{Event, EventsProvider, MessageSendEvent, MessageSendEventsProvider}; use util::errors::APIError; -use util::ser::Readable; +use util::ser::{ReadableArgs, Writeable}; use bitcoin::hashes::sha256::Hash as Sha256; use bitcoin::hashes::Hash; @@ -105,9 +105,9 @@ fn test_monitor_and_persister_update_fail() { let monitors = nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap(); let monitor = monitors.get(&outpoint).unwrap(); let mut w = test_utils::TestVecWriter(Vec::new()); - monitor.serialize_for_disk(&mut w).unwrap(); + monitor.write(&mut w).unwrap(); let new_monitor = <(BlockHash, ChannelMonitor)>::read( - &mut ::std::io::Cursor::new(&w.0)).unwrap().1; + &mut ::std::io::Cursor::new(&w.0), &test_utils::OnlyReadsKeysInterface {}).unwrap().1; assert!(new_monitor == *monitor); let chain_mon = test_utils::TestChainMonitor::new(Some(&chain_source), &chanmon_cfgs[0].tx_broadcaster, &logger, &chanmon_cfgs[0].fee_estimator, &persister); assert!(chain_mon.watch_channel(outpoint, new_monitor).is_ok()); diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs index 10b6b28b..4ba8b28b 100644 --- a/lightning/src/ln/channel.rs +++ b/lightning/src/ln/channel.rs @@ -33,7 +33,7 @@ use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitor use chain::transaction::{OutPoint, TransactionData}; use chain::keysinterface::{ChannelKeys, KeysInterface}; use util::transaction_utils; -use util::ser::{Readable, Writeable, Writer}; +use util::ser::{Readable, ReadableArgs, Writeable, Writer, VecWriter}; use util::logger::Logger; use util::errors::APIError; use util::config::{UserConfig,ChannelConfig}; @@ -4055,7 +4055,7 @@ impl Readable for InboundHTLCRemovalReason { } } -impl Writeable for Channel { +impl Writeable for Channel { fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { // Note that we write out as if remove_uncommitted_htlcs_and_mark_paused had just been // called but include holding cell updates (and obviously we don't modify self). @@ -4072,7 +4072,13 @@ impl Writeable for Channel { self.latest_monitor_update_id.write(writer)?; - self.holder_keys.write(writer)?; + let mut key_data = VecWriter(Vec::new()); + self.holder_keys.write(&mut key_data)?; + assert!(key_data.0.len() < std::usize::MAX); + assert!(key_data.0.len() < std::u32::MAX as usize); + (key_data.0.len() as u32).write(writer)?; + writer.write_all(&key_data.0[..])?; + self.shutdown_pubkey.write(writer)?; self.destination_script.write(writer)?; @@ -4237,8 +4243,10 @@ impl Writeable for Channel { } } -impl Readable for Channel { - fn read(reader: &mut R) -> Result { +const MAX_ALLOC_SIZE: usize = 64*1024; +impl<'a, ChanSigner: ChannelKeys, K: Deref> ReadableArgs<&'a K> for Channel + where K::Target: KeysInterface { + fn read(reader: &mut R, keys_source: &'a K) -> Result { let _ver: u8 = Readable::read(reader)?; let min_ver: u8 = Readable::read(reader)?; if min_ver > SERIALIZATION_VERSION { @@ -4254,7 +4262,17 @@ impl Readable for Channel { let latest_monitor_update_id = Readable::read(reader)?; - let holder_keys = Readable::read(reader)?; + let keys_len: u32 = Readable::read(reader)?; + let mut keys_data = Vec::with_capacity(cmp::min(keys_len as usize, MAX_ALLOC_SIZE)); + while keys_data.len() != keys_len as usize { + // Read 1KB at a time to avoid accidentally allocating 4GB on corrupted channel keys + let mut data = [0; 1024]; + let read_slice = &mut data[0..cmp::min(1024, keys_len as usize - keys_data.len())]; + reader.read_exact(read_slice)?; + keys_data.extend_from_slice(read_slice); + } + let holder_keys = keys_source.read_chan_signer(&keys_data)?; + let shutdown_pubkey = Readable::read(reader)?; let destination_script = Readable::read(reader)?; @@ -4472,7 +4490,7 @@ mod tests { use ln::channel::{Channel,ChannelKeys,InboundHTLCOutput,OutboundHTLCOutput,InboundHTLCState,OutboundHTLCState,HTLCOutputInCommitment,TxCreationKeys}; use ln::channel::MAX_FUNDING_SATOSHIS; use ln::features::InitFeatures; - use ln::msgs::{OptionalField, DataLossProtect}; + use ln::msgs::{OptionalField, DataLossProtect, DecodeError}; use ln::chan_utils; use ln::chan_utils::{ChannelPublicKeys, HolderCommitmentTransaction, CounterpartyChannelTransactionParameters}; use chain::chaininterface::{FeeEstimator,ConfirmationTarget}; @@ -4528,6 +4546,7 @@ mod tests { self.chan_keys.clone() } fn get_secure_random_bytes(&self) -> [u8; 32] { [0; 32] } + fn read_chan_signer(&self, _data: &[u8]) -> Result { panic!(); } } fn public_from_secret_hex(secp_ctx: &Secp256k1, hex: &str) -> PublicKey { diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index c39eef3e..49c14a04 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -3693,7 +3693,7 @@ impl Readable for HTLCForwardInfo { } } -impl Writeable for ChannelManager +impl Writeable for ChannelManager where M::Target: chain::Watch, T::Target: BroadcasterInterface, K::Target: KeysInterface, @@ -3784,7 +3784,8 @@ pub struct ChannelManagerReadArgs<'a, ChanSigner: 'a + ChannelKeys, M: Deref, T: L::Target: Logger, { /// The keys provider which will give us relevant keys. Some keys will be loaded during - /// deserialization. + /// deserialization and KeysInterface::read_chan_signer will be used to read per-Channel + /// signing data. pub keys_manager: K, /// The fee_estimator for use in the ChannelManager in the future. @@ -3846,7 +3847,7 @@ impl<'a, ChanSigner: 'a + ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L // Implement ReadableArgs for an Arc'd ChannelManager to make it a bit easier to work with the // SipmleArcChannelManager type: -impl<'a, ChanSigner: ChannelKeys + Readable, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> +impl<'a, ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ReadableArgs> for (BlockHash, Arc>) where M::Target: chain::Watch, T::Target: BroadcasterInterface, @@ -3860,7 +3861,7 @@ impl<'a, ChanSigner: ChannelKeys + Readable, M: Deref, T: Deref, K: Deref, F: De } } -impl<'a, ChanSigner: ChannelKeys + Readable, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> +impl<'a, ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ReadableArgs> for (BlockHash, ChannelManager) where M::Target: chain::Watch, T::Target: BroadcasterInterface, @@ -3886,7 +3887,7 @@ impl<'a, ChanSigner: ChannelKeys + Readable, M: Deref, T: Deref, K: Deref, F: De let mut by_id = HashMap::with_capacity(cmp::min(channel_count as usize, 128)); let mut short_to_id = HashMap::with_capacity(cmp::min(channel_count as usize, 128)); for _ in 0..channel_count { - let mut channel: Channel = Readable::read(reader)?; + let mut channel: Channel = Channel::read(reader, &args.keys_manager)?; if channel.last_block_connected != Default::default() && channel.last_block_connected != last_block_hash { return Err(DecodeError::InvalidValue); } diff --git a/lightning/src/ln/functional_test_utils.rs b/lightning/src/ln/functional_test_utils.rs index d54045f8..7863286f 100644 --- a/lightning/src/ln/functional_test_utils.rs +++ b/lightning/src/ln/functional_test_utils.rs @@ -21,7 +21,7 @@ use ln::msgs; use ln::msgs::{ChannelMessageHandler,RoutingMessageHandler}; use util::enforcing_trait_impls::EnforcingChannelKeys; use util::test_utils; -use util::test_utils::TestChainMonitor; +use util::test_utils::{TestChainMonitor, OnlyReadsKeysInterface}; use util::events::{Event, EventsProvider, MessageSendEvent, MessageSendEventsProvider}; use util::errors::APIError; use util::config::UserConfig; @@ -170,9 +170,9 @@ impl<'a, 'b, 'c> Drop for Node<'a, 'b, 'c> { let old_monitors = self.chain_monitor.chain_monitor.monitors.lock().unwrap(); for (_, old_monitor) in old_monitors.iter() { let mut w = test_utils::TestVecWriter(Vec::new()); - old_monitor.serialize_for_disk(&mut w).unwrap(); + old_monitor.write(&mut w).unwrap(); let (_, deserialized_monitor) = <(BlockHash, ChannelMonitor)>::read( - &mut ::std::io::Cursor::new(&w.0)).unwrap(); + &mut ::std::io::Cursor::new(&w.0), &OnlyReadsKeysInterface {}).unwrap(); deserialized_monitors.push(deserialized_monitor); } } diff --git a/lightning/src/ln/functional_tests.rs b/lightning/src/ln/functional_tests.rs index 36709062..a9367d9c 100644 --- a/lightning/src/ln/functional_tests.rs +++ b/lightning/src/ln/functional_tests.rs @@ -28,7 +28,7 @@ use util::enforcing_trait_impls::EnforcingChannelKeys; use util::{byte_utils, test_utils}; use util::events::{Event, EventsProvider, MessageSendEvent, MessageSendEventsProvider}; use util::errors::APIError; -use util::ser::{Writeable, ReadableArgs, Readable}; +use util::ser::{Writeable, ReadableArgs}; use util::config::UserConfig; use bitcoin::hashes::sha256d::Hash as Sha256dHash; @@ -4279,20 +4279,21 @@ fn test_no_txn_manager_serialize_deserialize() { let nodes_0_serialized = nodes[0].node.encode(); let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new()); - nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap().iter().next().unwrap().1.serialize_for_disk(&mut chan_0_monitor_serialized).unwrap(); + nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap().iter().next().unwrap().1.write(&mut chan_0_monitor_serialized).unwrap(); logger = test_utils::TestLogger::new(); fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: 253 }; persister = test_utils::TestPersister::new(); new_chain_monitor = test_utils::TestChainMonitor::new(Some(nodes[0].chain_source), nodes[0].tx_broadcaster.clone(), &logger, &fee_estimator, &persister); nodes[0].chain_monitor = &new_chain_monitor; + keys_manager = test_utils::TestKeysInterface::new(&nodes[0].node_seed, Network::Testnet); let mut chan_0_monitor_read = &chan_0_monitor_serialized.0[..]; - let (_, mut chan_0_monitor) = <(BlockHash, ChannelMonitor)>::read(&mut chan_0_monitor_read).unwrap(); + let (_, mut chan_0_monitor) = <(BlockHash, ChannelMonitor)>::read( + &mut chan_0_monitor_read, &keys_manager).unwrap(); assert!(chan_0_monitor_read.is_empty()); let mut nodes_0_read = &nodes_0_serialized[..]; let config = UserConfig::default(); - keys_manager = test_utils::TestKeysInterface::new(&nodes[0].node_seed, Network::Testnet); let (_, nodes_0_deserialized_tmp) = { let mut channel_monitors = HashMap::new(); channel_monitors.insert(chan_0_monitor.get_funding_txo().0, &mut chan_0_monitor); @@ -4388,20 +4389,21 @@ fn test_manager_serialize_deserialize_events() { // Start the de/seriailization process mid-channel creation to check that the channel manager will hold onto events that are serialized let nodes_0_serialized = nodes[0].node.encode(); let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new()); - nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap().iter().next().unwrap().1.serialize_for_disk(&mut chan_0_monitor_serialized).unwrap(); + nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap().iter().next().unwrap().1.write(&mut chan_0_monitor_serialized).unwrap(); fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: 253 }; logger = test_utils::TestLogger::new(); persister = test_utils::TestPersister::new(); new_chain_monitor = test_utils::TestChainMonitor::new(Some(nodes[0].chain_source), nodes[0].tx_broadcaster.clone(), &logger, &fee_estimator, &persister); + keys_manager = test_utils::TestKeysInterface::new(&nodes[0].node_seed, Network::Testnet); nodes[0].chain_monitor = &new_chain_monitor; let mut chan_0_monitor_read = &chan_0_monitor_serialized.0[..]; - let (_, mut chan_0_monitor) = <(BlockHash, ChannelMonitor)>::read(&mut chan_0_monitor_read).unwrap(); + let (_, mut chan_0_monitor) = <(BlockHash, ChannelMonitor)>::read( + &mut chan_0_monitor_read, &keys_manager).unwrap(); assert!(chan_0_monitor_read.is_empty()); let mut nodes_0_read = &nodes_0_serialized[..]; let config = UserConfig::default(); - keys_manager = test_utils::TestKeysInterface::new(&nodes[0].node_seed, Network::Testnet); let (_, nodes_0_deserialized_tmp) = { let mut channel_monitors = HashMap::new(); channel_monitors.insert(chan_0_monitor.get_funding_txo().0, &mut chan_0_monitor); @@ -4480,19 +4482,20 @@ fn test_simple_manager_serialize_deserialize() { let nodes_0_serialized = nodes[0].node.encode(); let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new()); - nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap().iter().next().unwrap().1.serialize_for_disk(&mut chan_0_monitor_serialized).unwrap(); + nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap().iter().next().unwrap().1.write(&mut chan_0_monitor_serialized).unwrap(); logger = test_utils::TestLogger::new(); fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: 253 }; persister = test_utils::TestPersister::new(); new_chain_monitor = test_utils::TestChainMonitor::new(Some(nodes[0].chain_source), nodes[0].tx_broadcaster.clone(), &logger, &fee_estimator, &persister); + keys_manager = test_utils::TestKeysInterface::new(&nodes[0].node_seed, Network::Testnet); nodes[0].chain_monitor = &new_chain_monitor; let mut chan_0_monitor_read = &chan_0_monitor_serialized.0[..]; - let (_, mut chan_0_monitor) = <(BlockHash, ChannelMonitor)>::read(&mut chan_0_monitor_read).unwrap(); + let (_, mut chan_0_monitor) = <(BlockHash, ChannelMonitor)>::read( + &mut chan_0_monitor_read, &keys_manager).unwrap(); assert!(chan_0_monitor_read.is_empty()); let mut nodes_0_read = &nodes_0_serialized[..]; - keys_manager = test_utils::TestKeysInterface::new(&nodes[0].node_seed, Network::Testnet); let (_, nodes_0_deserialized_tmp) = { let mut channel_monitors = HashMap::new(); channel_monitors.insert(chan_0_monitor.get_funding_txo().0, &mut chan_0_monitor); @@ -4539,7 +4542,7 @@ fn test_manager_serialize_deserialize_inconsistent_monitor() { let mut node_0_stale_monitors_serialized = Vec::new(); for monitor in nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap().iter() { let mut writer = test_utils::TestVecWriter(Vec::new()); - monitor.1.serialize_for_disk(&mut writer).unwrap(); + monitor.1.write(&mut writer).unwrap(); node_0_stale_monitors_serialized.push(writer.0); } @@ -4558,7 +4561,7 @@ fn test_manager_serialize_deserialize_inconsistent_monitor() { let mut node_0_monitors_serialized = Vec::new(); for monitor in nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap().iter() { let mut writer = test_utils::TestVecWriter(Vec::new()); - monitor.1.serialize_for_disk(&mut writer).unwrap(); + monitor.1.write(&mut writer).unwrap(); node_0_monitors_serialized.push(writer.0); } @@ -4568,10 +4571,12 @@ fn test_manager_serialize_deserialize_inconsistent_monitor() { new_chain_monitor = test_utils::TestChainMonitor::new(Some(nodes[0].chain_source), nodes[0].tx_broadcaster.clone(), &logger, &fee_estimator, &persister); nodes[0].chain_monitor = &new_chain_monitor; + keys_manager = test_utils::TestKeysInterface::new(&nodes[0].node_seed, Network::Testnet); + let mut node_0_stale_monitors = Vec::new(); for serialized in node_0_stale_monitors_serialized.iter() { let mut read = &serialized[..]; - let (_, monitor) = <(BlockHash, ChannelMonitor)>::read(&mut read).unwrap(); + let (_, monitor) = <(BlockHash, ChannelMonitor)>::read(&mut read, &keys_manager).unwrap(); assert!(read.is_empty()); node_0_stale_monitors.push(monitor); } @@ -4579,13 +4584,11 @@ fn test_manager_serialize_deserialize_inconsistent_monitor() { let mut node_0_monitors = Vec::new(); for serialized in node_0_monitors_serialized.iter() { let mut read = &serialized[..]; - let (_, monitor) = <(BlockHash, ChannelMonitor)>::read(&mut read).unwrap(); + let (_, monitor) = <(BlockHash, ChannelMonitor)>::read(&mut read, &keys_manager).unwrap(); assert!(read.is_empty()); node_0_monitors.push(monitor); } - keys_manager = test_utils::TestKeysInterface::new(&nodes[0].node_seed, Network::Testnet); - let mut nodes_0_read = &nodes_0_serialized[..]; if let Err(msgs::DecodeError::InvalidValue) = <(BlockHash, ChannelManager)>::read(&mut nodes_0_read, ChannelManagerReadArgs { @@ -7322,7 +7325,7 @@ fn test_user_configurable_csv_delay() { let nodes = create_network(2, &node_cfgs, &node_chanmgrs); // We test config.our_to_self > BREAKDOWN_TIMEOUT is enforced in Channel::new_outbound() - let keys_manager: Arc> = Arc::new(test_utils::TestKeysInterface::new(&nodes[0].node_seed, Network::Testnet)); + let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&nodes[0].node_seed, Network::Testnet)); if let Err(error) = Channel::new_outbound(&&test_utils::TestFeeEstimator { sat_per_kw: 253 }, &keys_manager, nodes[1].node.get_our_node_id(), 1000000, 1000000, 0, &low_our_to_self_config) { match error { APIError::APIMisuseError { err } => { assert!(regex::Regex::new(r"Configured with an unreasonable our_to_self_delay \(\d+\) putting user funds at risks").unwrap().is_match(err.as_str())); }, @@ -7392,7 +7395,7 @@ fn test_data_loss_protect() { // Cache node A state before any channel update let previous_node_state = nodes[0].node.encode(); let mut previous_chain_monitor_state = test_utils::TestVecWriter(Vec::new()); - nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap().iter().next().unwrap().1.serialize_for_disk(&mut previous_chain_monitor_state).unwrap(); + nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap().iter().next().unwrap().1.write(&mut previous_chain_monitor_state).unwrap(); send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000, 8_000_000); send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000, 8_000_000); @@ -7402,11 +7405,11 @@ fn test_data_loss_protect() { // Restore node A from previous state logger = test_utils::TestLogger::with_id(format!("node {}", 0)); - let mut chain_monitor = <(BlockHash, ChannelMonitor)>::read(&mut ::std::io::Cursor::new(previous_chain_monitor_state.0)).unwrap().1; + keys_manager = test_utils::TestKeysInterface::new(&nodes[0].node_seed, Network::Testnet); + let mut chain_monitor = <(BlockHash, ChannelMonitor)>::read(&mut ::std::io::Cursor::new(previous_chain_monitor_state.0), &keys_manager).unwrap().1; chain_source = test_utils::TestChainSource::new(Network::Testnet); tx_broadcaster = test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new())}; fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: 253 }; - keys_manager = test_utils::TestKeysInterface::new(&nodes[0].node_seed, Network::Testnet); persister = test_utils::TestPersister::new(); monitor = test_utils::TestChainMonitor::new(Some(&chain_source), &tx_broadcaster, &logger, &fee_estimator, &persister); node_state_0 = { @@ -8274,9 +8277,9 @@ fn test_update_err_monitor_lockdown() { let monitors = nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap(); let monitor = monitors.get(&outpoint).unwrap(); let mut w = test_utils::TestVecWriter(Vec::new()); - monitor.serialize_for_disk(&mut w).unwrap(); + monitor.write(&mut w).unwrap(); let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor)>::read( - &mut ::std::io::Cursor::new(&w.0)).unwrap().1; + &mut ::std::io::Cursor::new(&w.0), &test_utils::OnlyReadsKeysInterface {}).unwrap().1; assert!(new_monitor == *monitor); let watchtower = test_utils::TestChainMonitor::new(Some(&chain_source), &chanmon_cfgs[0].tx_broadcaster, &logger, &chanmon_cfgs[0].fee_estimator, &persister); assert!(watchtower.watch_channel(outpoint, new_monitor).is_ok()); @@ -8333,9 +8336,9 @@ fn test_concurrent_monitor_claim() { let monitors = nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap(); let monitor = monitors.get(&outpoint).unwrap(); let mut w = test_utils::TestVecWriter(Vec::new()); - monitor.serialize_for_disk(&mut w).unwrap(); + monitor.write(&mut w).unwrap(); let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor)>::read( - &mut ::std::io::Cursor::new(&w.0)).unwrap().1; + &mut ::std::io::Cursor::new(&w.0), &test_utils::OnlyReadsKeysInterface {}).unwrap().1; assert!(new_monitor == *monitor); let watchtower = test_utils::TestChainMonitor::new(Some(&chain_source), &chanmon_cfgs[0].tx_broadcaster, &logger, &chanmon_cfgs[0].fee_estimator, &persister); assert!(watchtower.watch_channel(outpoint, new_monitor).is_ok()); @@ -8359,9 +8362,9 @@ fn test_concurrent_monitor_claim() { let monitors = nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap(); let monitor = monitors.get(&outpoint).unwrap(); let mut w = test_utils::TestVecWriter(Vec::new()); - monitor.serialize_for_disk(&mut w).unwrap(); + monitor.write(&mut w).unwrap(); let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor)>::read( - &mut ::std::io::Cursor::new(&w.0)).unwrap().1; + &mut ::std::io::Cursor::new(&w.0), &test_utils::OnlyReadsKeysInterface {}).unwrap().1; assert!(new_monitor == *monitor); let watchtower = test_utils::TestChainMonitor::new(Some(&chain_source), &chanmon_cfgs[0].tx_broadcaster, &logger, &chanmon_cfgs[0].fee_estimator, &persister); assert!(watchtower.watch_channel(outpoint, new_monitor).is_ok()); diff --git a/lightning/src/ln/onchaintx.rs b/lightning/src/ln/onchaintx.rs index 146a4d57..1b0060da 100644 --- a/lightning/src/ln/onchaintx.rs +++ b/lightning/src/ln/onchaintx.rs @@ -27,9 +27,9 @@ use ln::chan_utils; use ln::chan_utils::{TxCreationKeys, ChannelTransactionParameters, HolderCommitmentTransaction}; use chain::chaininterface::{FeeEstimator, BroadcasterInterface, ConfirmationTarget, MIN_RELAY_FEE_SAT_PER_1000_WEIGHT}; use chain::channelmonitor::{ANTI_REORG_DELAY, CLTV_SHARED_CLAIM_BUFFER, InputMaterial, ClaimRequest}; -use chain::keysinterface::ChannelKeys; +use chain::keysinterface::{ChannelKeys, KeysInterface}; use util::logger::Logger; -use util::ser::{Readable, Writer, Writeable}; +use util::ser::{Readable, ReadableArgs, Writer, Writeable, VecWriter}; use util::byte_utils; use std::collections::{HashMap, hash_map}; @@ -286,7 +286,7 @@ pub struct OnchainTxHandler { secp_ctx: Secp256k1, } -impl OnchainTxHandler { +impl OnchainTxHandler { pub(crate) fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { self.destination_script.write(writer)?; self.holder_commitment.write(writer)?; @@ -294,9 +294,15 @@ impl OnchainTxHandler { self.prev_holder_commitment.write(writer)?; self.prev_holder_htlc_sigs.write(writer)?; - self.key_storage.write(writer)?; self.channel_transaction_parameters.write(writer)?; + let mut key_data = VecWriter(Vec::new()); + self.key_storage.write(&mut key_data)?; + assert!(key_data.0.len() < std::usize::MAX); + assert!(key_data.0.len() < std::u32::MAX as usize); + (key_data.0.len() as u32).write(writer)?; + writer.write_all(&key_data.0[..])?; + 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)?; @@ -333,8 +339,8 @@ impl OnchainTxHandler { } } -impl Readable for OnchainTxHandler { - fn read(reader: &mut R) -> Result { +impl<'a, K: KeysInterface> ReadableArgs<&'a K> for OnchainTxHandler { + fn read(reader: &mut R, keys_manager: &'a K) -> Result { let destination_script = Readable::read(reader)?; let holder_commitment = Readable::read(reader)?; @@ -342,9 +348,19 @@ impl Readable for OnchainTxHandler SecretKey { unreachable!(); } + fn get_destination_script(&self) -> Script { unreachable!(); } + fn get_shutdown_pubkey(&self) -> PublicKey { unreachable!(); } + fn get_channel_keys(&self, _inbound: bool, _channel_value_satoshis: u64) -> EnforcingChannelKeys { unreachable!(); } + fn get_secure_random_bytes(&self) -> [u8; 32] { unreachable!(); } + + fn read_chan_signer(&self, reader: &[u8]) -> Result { + EnforcingChannelKeys::read(&mut std::io::Cursor::new(reader)) + } +} + pub struct TestChainMonitor<'a> { pub added_monitors: Mutex)>>, pub latest_monitor_update_id: Mutex>, @@ -87,9 +102,9 @@ impl<'a> chain::Watch for TestChainMonitor<'a> { // At every point where we get a monitor update, we should be able to send a useful monitor // to a watchtower and disk... let mut w = TestVecWriter(Vec::new()); - monitor.serialize_for_disk(&mut w).unwrap(); + monitor.write(&mut w).unwrap(); let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor)>::read( - &mut ::std::io::Cursor::new(&w.0)).unwrap().1; + &mut ::std::io::Cursor::new(&w.0), &OnlyReadsKeysInterface {}).unwrap().1; assert!(new_monitor == monitor); self.latest_monitor_update_id.lock().unwrap().insert(funding_txo.to_channel_id(), (funding_txo, monitor.get_latest_update_id())); self.added_monitors.lock().unwrap().push((funding_txo, monitor)); @@ -120,9 +135,9 @@ impl<'a> chain::Watch for TestChainMonitor<'a> { let monitors = self.chain_monitor.monitors.lock().unwrap(); let monitor = monitors.get(&funding_txo).unwrap(); w.0.clear(); - monitor.serialize_for_disk(&mut w).unwrap(); + monitor.write(&mut w).unwrap(); let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor)>::read( - &mut ::std::io::Cursor::new(&w.0)).unwrap().1; + &mut ::std::io::Cursor::new(&w.0), &OnlyReadsKeysInterface {}).unwrap().1; assert!(new_monitor == *monitor); self.added_monitors.lock().unwrap().push((funding_txo, new_monitor)); @@ -430,6 +445,10 @@ impl keysinterface::KeysInterface for TestKeysInterface { } self.backing.get_secure_random_bytes() } + + fn read_chan_signer(&self, reader: &[u8]) -> Result { + EnforcingChannelKeys::read(&mut std::io::Cursor::new(reader)) + } } impl TestKeysInterface {