From 0fece38b19e78c390916dfc44a9192c52573db98 Mon Sep 17 00:00:00 2001 From: Yuntai Kyong Date: Wed, 15 Aug 2018 01:12:40 +0900 Subject: [PATCH 01/16] Add various checking when handling open and accept Add `derive_minimum_depth()` and `derive_maximum_minimum_depth()` and hide CONF_TARGET constant behind these functions. Replace `DisconnectPeer` error with `HandleError` with `ErrorAction::SendErrorMessage` --- src/ln/channel.rs | 112 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 90 insertions(+), 22 deletions(-) diff --git a/src/ln/channel.rs b/src/ln/channel.rs index c88e1d77..93e16480 100644 --- a/src/ln/channel.rs +++ b/src/ln/channel.rs @@ -320,7 +320,6 @@ pub struct Channel { } const OUR_MAX_HTLCS: u16 = 5; //TODO -const CONF_TARGET: u32 = 12; //TODO: Should be much higher /// Confirmation count threshold at which we close a channel. Ideally we'd keep the channel around /// on ice until the funding transaction gets more confirmations, but the LN protocol doesn't /// really allow for this, so instead we're stuck closing it out at that point. @@ -372,6 +371,16 @@ impl Channel { 1000 // TODO } + fn derive_minimum_depth(_channel_value_satoshis_msat: u64, _value_to_self_msat: u64) -> u32 { + const CONF_TARGET: u32 = 12; //TODO: Should be much higher + CONF_TARGET + } + + fn derive_maximum_minimum_depth(_channel_value_satoshis_msat: u64, _value_to_self_msat: u64) -> u32 { + const CONF_TARGET: u32 = 12; //TODO: Should be much higher + CONF_TARGET * 2 + } + // Constructors: pub fn new_outbound(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, channel_value_satoshis: u64, push_msat: u64, announce_publicly: bool, user_id: u64, logger: Arc) -> Result { if channel_value_satoshis >= MAX_FUNDING_SATOSHIS { @@ -465,31 +474,46 @@ impl Channel { /// Generally prefers to take the DisconnectPeer action on failure, as a notice to the sender /// that we're rejecting the new channel. pub fn new_from_req(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, msg: &msgs::OpenChannel, user_id: u64, require_announce: bool, allow_announce: bool, logger: Arc) -> Result { + macro_rules! return_error_message { + ( $msg: expr ) => { + return Err(HandleError{err: $msg, action: Some(msgs::ErrorAction::SendErrorMessage{ msg: msgs::ErrorMessage { channel_id: msg.temporary_channel_id, data: $msg.to_string() }})}); + } + } + // Check sanity of message fields: if msg.funding_satoshis >= MAX_FUNDING_SATOSHIS { - return Err(HandleError{err: "funding value > 2^24", action: Some(msgs::ErrorAction::DisconnectPeer{ msg: None })}); + return_error_message!("funding value > 2^24"); } if msg.channel_reserve_satoshis > msg.funding_satoshis { - return Err(HandleError{err: "Bogus channel_reserve_satoshis", action: Some(msgs::ErrorAction::DisconnectPeer{ msg: None })}); + return_error_message!("Bogus channel_reserve_satoshis"); } if msg.push_msat > (msg.funding_satoshis - msg.channel_reserve_satoshis) * 1000 { - return Err(HandleError{err: "push_msat more than highest possible value", action: Some(msgs::ErrorAction::DisconnectPeer{ msg: None })}); + return_error_message!("push_msat larger than funding value"); } if msg.dust_limit_satoshis > msg.funding_satoshis { - return Err(HandleError{err: "Peer never wants payout outputs?", action: Some(msgs::ErrorAction::DisconnectPeer{ msg: None })}); + return_error_message!("Peer never wants payout outputs?"); + } + if msg.dust_limit_satoshis > msg.channel_reserve_satoshis { + return_error_message!("Bogus; channel reserve is less than dust limit"); } if msg.htlc_minimum_msat >= (msg.funding_satoshis - msg.channel_reserve_satoshis) * 1000 { - return Err(HandleError{err: "Minimum htlc value is full channel value", action: Some(msgs::ErrorAction::DisconnectPeer{ msg: None })}); + return_error_message!("Miminum htlc value is full channel value"); } - Channel::check_remote_fee(fee_estimator, msg.feerate_per_kw)?; + Channel::check_remote_fee(fee_estimator, msg.feerate_per_kw).map_err(|e| + HandleError{err: e.err, action: Some(msgs::ErrorAction::SendErrorMessage{ msg: msgs::ErrorMessage { channel_id: msg.temporary_channel_id, data: e.err.to_string() }})} + )?; + if msg.to_self_delay > MAX_LOCAL_BREAKDOWN_TIMEOUT { - return Err(HandleError{err: "They wanted our payments to be delayed by a needlessly long period", action: Some(msgs::ErrorAction::DisconnectPeer{ msg: None })}); + return_error_message!("They wanted our payments to be delayed by a needlessly long period"); } if msg.max_accepted_htlcs < 1 { - return Err(HandleError{err: "0 max_accpted_htlcs makes for a useless channel", action: Some(msgs::ErrorAction::DisconnectPeer{ msg: None })}); + return_error_message!("0 max_accpted_htlcs makes for a useless channel"); + } + if msg.max_accepted_htlcs > 483 { + return_error_message!("max_accpted_htlcs > 483"); } if (msg.channel_flags & 254) != 0 { - return Err(HandleError{err: "unknown channel_flags", action: Some(msgs::ErrorAction::DisconnectPeer{ msg: None })}); + return Err(HandleError{err: "Unknown channel flags", action: Some(msgs::ErrorAction::IgnoreError) }); } // Convert things into internal flags and prep our state: @@ -504,6 +528,31 @@ impl Channel { let background_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background); + let our_dust_limit_satoshis = Channel::derive_our_dust_limit_satoshis(background_feerate); + let our_channel_reserve_satoshis = Channel::get_our_channel_reserve_satoshis(msg.funding_satoshis); + if our_channel_reserve_satoshis < our_dust_limit_satoshis { + return_error_message!("Suitalbe channel reserve not found. aborting"); + } + if msg.channel_reserve_satoshis < our_dust_limit_satoshis { + return_error_message!("channel_reserve_satoshis too small"); + } + if our_channel_reserve_satoshis < msg.dust_limit_satoshis { + return_error_message!("Dust limit too high for our channel reserve"); + } + + // check if the funder's amount for the initial commitment tx is sufficient + // for full fee payment + let funders_amount_msat = msg.funding_satoshis * 1000 - msg.push_msat; + if funders_amount_msat < background_feerate * COMMITMENT_TX_BASE_WEIGHT { + return_error_message!("Insufficient funding amount for initial commitment"); + } + + let to_local_msat = msg.push_msat; + let to_remote_msat = funders_amount_msat - background_feerate * COMMITMENT_TX_BASE_WEIGHT; + if to_local_msat <= msg.channel_reserve_satoshis * 1000 && to_remote_msat <= our_channel_reserve_satoshis * 1000 { + return_error_message!("Insufficient funding amount for initial commitment"); + } + let secp_ctx = Secp256k1::new(); let our_channel_monitor_claim_key_hash = Hash160::from_data(&PublicKey::from_secret_key(&secp_ctx, &chan_keys.channel_monitor_claim_key).unwrap().serialize()); let our_channel_monitor_claim_script = Builder::new().push_opcode(opcodes::All::OP_PUSHBYTES_0).push_slice(&our_channel_monitor_claim_key_hash[..]).into_script(); @@ -545,7 +594,7 @@ impl Channel { feerate_per_kw: msg.feerate_per_kw as u64, channel_value_satoshis: msg.funding_satoshis, their_dust_limit_satoshis: msg.dust_limit_satoshis, - our_dust_limit_satoshis: Channel::derive_our_dust_limit_satoshis(background_feerate), + our_dust_limit_satoshis: our_dust_limit_satoshis, their_max_htlc_value_in_flight_msat: cmp::min(msg.max_htlc_value_in_flight_msat, msg.funding_satoshis * 1000), their_channel_reserve_satoshis: msg.channel_reserve_satoshis, their_htlc_minimum_msat: msg.htlc_minimum_msat, @@ -1118,28 +1167,47 @@ impl Channel { // Message handlers: pub fn accept_channel(&mut self, msg: &msgs::AcceptChannel) -> Result<(), HandleError> { + macro_rules! return_error_message { + ( $msg: expr ) => { + return Err(HandleError{err: $msg, action: Some(msgs::ErrorAction::SendErrorMessage{ msg: msgs::ErrorMessage { channel_id: msg.temporary_channel_id, data: $msg.to_string() }})}); + } + } // Check sanity of message fields: if !self.channel_outbound { - return Err(HandleError{err: "Got an accept_channel message from an inbound peer", action: None}); + return_error_message!("Got an accept_channel message from an inbound peer"); } if self.channel_state != ChannelState::OurInitSent as u32 { - return Err(HandleError{err: "Got an accept_channel message at a strange time", action: None}); + return_error_message!("Got an accept_channel message at a strange time"); } if msg.dust_limit_satoshis > 21000000 * 100000000 { - return Err(HandleError{err: "Peer never wants payout outputs?", action: None}); + return_error_message!("Peer never wants payout outputs?"); } if msg.channel_reserve_satoshis > self.channel_value_satoshis { - return Err(HandleError{err: "Bogus channel_reserve_satoshis", action: None}); + return_error_message!("Bogus channel_reserve_satoshis"); + } + if msg.dust_limit_satoshis > msg.channel_reserve_satoshis { + return_error_message!("Bogus channel_reserve and dust_limit"); + } + if msg.channel_reserve_satoshis < self.our_dust_limit_satoshis { + return_error_message!("Peer never wants payout outputs?"); + } + if msg.dust_limit_satoshis > Channel::get_our_channel_reserve_satoshis(self.channel_value_satoshis) { + return_error_message!("Dust limit is bigger than our channel reverse"); } if msg.htlc_minimum_msat >= (self.channel_value_satoshis - msg.channel_reserve_satoshis) * 1000 { - return Err(HandleError{err: "Minimum htlc value is full channel value", action: None}); + return_error_message!("Minimum htlc value is full channel value"); + } + if msg.minimum_depth > Channel::derive_maximum_minimum_depth(self.channel_value_satoshis*1000, self.value_to_self_msat) { + return_error_message!("minimum_depth too large"); } - //TODO do something with minimum_depth if msg.to_self_delay > MAX_LOCAL_BREAKDOWN_TIMEOUT { - return Err(HandleError{err: "They wanted our payments to be delayed by a needlessly long period", action: None}); + return_error_message!("They wanted our payments to be delayed by a needlessly long period"); } if msg.max_accepted_htlcs < 1 { - return Err(HandleError{err: "0 max_accpted_htlcs makes for a useless channel", action: None}); + return_error_message!("0 max_accpted_htlcs makes for a useless channel"); + } + if msg.max_accepted_htlcs > 483 { + return_error_message!("max_accpted_htlcs > 483"); } self.channel_monitor.set_their_htlc_base_key(&msg.htlc_basepoint); @@ -1958,7 +2026,7 @@ impl Channel { if header.bitcoin_hash() != self.last_block_connected { self.last_block_connected = header.bitcoin_hash(); self.funding_tx_confirmations += 1; - if self.funding_tx_confirmations == CONF_TARGET as u64 { + if self.funding_tx_confirmations == Channel::derive_minimum_depth(self.channel_value_satoshis*1000, self.value_to_self_msat) as u64 { let need_commitment_update = if non_shutdown_state == ChannelState::FundingSent as u32 { self.channel_state |= ChannelState::OurFundingLocked as u32; true @@ -2026,7 +2094,7 @@ impl Channel { } } if Some(header.bitcoin_hash()) == self.funding_tx_confirmed_in { - self.funding_tx_confirmations = CONF_TARGET as u64 - 1; + self.funding_tx_confirmations = Channel::derive_minimum_depth(self.channel_value_satoshis*1000, self.value_to_self_msat) as u64 - 1; } false } @@ -2090,7 +2158,7 @@ impl Channel { max_htlc_value_in_flight_msat: Channel::get_our_max_htlc_value_in_flight_msat(self.channel_value_satoshis), channel_reserve_satoshis: Channel::get_our_channel_reserve_satoshis(self.channel_value_satoshis), htlc_minimum_msat: self.our_htlc_minimum_msat, - minimum_depth: CONF_TARGET, + minimum_depth: Channel::derive_minimum_depth(self.channel_value_satoshis*1000, self.value_to_self_msat), to_self_delay: BREAKDOWN_TIMEOUT, max_accepted_htlcs: OUR_MAX_HTLCS, funding_pubkey: PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.funding_key).unwrap(), -- 2.30.2 From 716b37863a553de6fcf83202d5e1294a47e7660f Mon Sep 17 00:00:00 2001 From: Yuntai Kyong Date: Wed, 15 Aug 2018 01:18:10 +0900 Subject: [PATCH 02/16] document optional channel constraints per spec --- src/ln/channel.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/ln/channel.rs b/src/ln/channel.rs index 93e16480..f3455a9f 100644 --- a/src/ln/channel.rs +++ b/src/ln/channel.rs @@ -1210,6 +1210,15 @@ impl Channel { return_error_message!("max_accpted_htlcs > 483"); } + // TODO: Optional additional constraints mentioned in the spec + // MAY fail the channel if + // funding_satoshi is too small + // htlc_minimum_msat too large + // max_htlc_value_in_flight_msat too small + // channel_reserve_satoshis too large + // max_accepted_htlcs too small + // dust_limit_satoshis too small + self.channel_monitor.set_their_htlc_base_key(&msg.htlc_basepoint); self.their_dust_limit_satoshis = msg.dust_limit_satoshis; -- 2.30.2 From 1c839ff1035eea3b01f5f9c258826d867e04af24 Mon Sep 17 00:00:00 2001 From: Yuntai Kyong Date: Fri, 17 Aug 2018 12:57:51 +0900 Subject: [PATCH 03/16] Add checking locally derived reserve and dust limit --- src/ln/channel.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/ln/channel.rs b/src/ln/channel.rs index f3455a9f..e2749fff 100644 --- a/src/ln/channel.rs +++ b/src/ln/channel.rs @@ -391,8 +391,13 @@ impl Channel { return Err(APIError::APIMisuseError{err: "push value > channel value"}); } - let feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal); + let background_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background); + if Channel::get_our_channel_reserve_satoshis(channel_value_satoshis) < Channel::derive_our_dust_limit_satoshis(background_feerate) { + return Err(APIError::FeeRateTooHigh{err: format!("Not enough reserve above dust limit can be found at current fee rate({})", background_feerate), feerate: background_feerate}); + } + + let feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal); let secp_ctx = Secp256k1::new(); let our_channel_monitor_claim_key_hash = Hash160::from_data(&PublicKey::from_secret_key(&secp_ctx, &chan_keys.channel_monitor_claim_key).unwrap().serialize()); -- 2.30.2 From 69624a8556d67e897efb057aae4e53a6aee4c948 Mon Sep 17 00:00:00 2001 From: Yuntai Kyong Date: Fri, 17 Aug 2018 13:12:58 -0400 Subject: [PATCH 04/16] add 1% chnnel reserve while keeping min value if 1000 is always used it will almost always fail test reserve < dust_limit check --- src/ln/channel.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ln/channel.rs b/src/ln/channel.rs index e2749fff..d7729a1e 100644 --- a/src/ln/channel.rs +++ b/src/ln/channel.rs @@ -360,7 +360,8 @@ impl Channel { /// Guaranteed to return a value no larger than channel_value_satoshis fn get_our_channel_reserve_satoshis(channel_value_satoshis: u64) -> u64 { - cmp::min(channel_value_satoshis, 1000) //TODO + let (q, _) = channel_value_satoshis.overflowing_div(100); + cmp::min(channel_value_satoshis, cmp::max(q, 1000)) //TODO } fn derive_our_dust_limit_satoshis(at_open_background_feerate: u64) -> u64 { -- 2.30.2 From 1360fccd7143937278f880497634eb284da834ce Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 17 Aug 2018 13:22:44 -0400 Subject: [PATCH 05/16] Ignore unknown channel flags as required in BOLT 2 --- src/ln/channel.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/ln/channel.rs b/src/ln/channel.rs index d7729a1e..b2164696 100644 --- a/src/ln/channel.rs +++ b/src/ln/channel.rs @@ -518,9 +518,6 @@ impl Channel { if msg.max_accepted_htlcs > 483 { return_error_message!("max_accpted_htlcs > 483"); } - if (msg.channel_flags & 254) != 0 { - return Err(HandleError{err: "Unknown channel flags", action: Some(msgs::ErrorAction::IgnoreError) }); - } // Convert things into internal flags and prep our state: -- 2.30.2 From 7743cbdf14412bfbe37fbce787e7b1f9a9a44d53 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 17 Aug 2018 14:29:16 -0400 Subject: [PATCH 06/16] Add APIError docs --- src/util/errors.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/util/errors.rs b/src/util/errors.rs index 0700b451..71e5eed6 100644 --- a/src/util/errors.rs +++ b/src/util/errors.rs @@ -1,15 +1,22 @@ use std::fmt; +/// Indicates an error on the client's part (usually some variant of attempting to use too-low or +/// too-high values) pub enum APIError { + /// Indicates the API was wholly misused (see err for more). Cases where these can be returned + /// are documented, but generally indicates some precondition of a function was violated. APIMisuseError {err: &'static str}, + /// Due to a high feerate, we were unable to complete the request. + /// For example, this may be returned if the feerate implies we cannot open a channel at the + /// requested value, but opening a larger channel would succeed. FeeRateTooHigh {err: String, feerate: u64}, } impl fmt::Debug for APIError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { APIError::APIMisuseError {ref err} => f.write_str(err), APIError::FeeRateTooHigh {ref err, ref feerate} => write!(f, "{} feerate: {}", err, feerate) } - } + } } -- 2.30.2 From 42086c94a0f7042379d83ff1c2dcb7a84604b9ae Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Mon, 20 Aug 2018 12:56:17 -0400 Subject: [PATCH 07/16] Remove implicit Record import requirement in logging macros --- src/ln/channel.rs | 2 +- src/ln/channelmanager.rs | 2 +- src/ln/peer_handler.rs | 2 +- src/util/logger.rs | 2 +- src/util/macro_logger.rs | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ln/channel.rs b/src/ln/channel.rs index b2164696..c7c3208e 100644 --- a/src/ln/channel.rs +++ b/src/ln/channel.rs @@ -23,7 +23,7 @@ use chain::chaininterface::{FeeEstimator,ConfirmationTarget}; use chain::transaction::OutPoint; use util::{transaction_utils,rng}; use util::sha2::Sha256; -use util::logger::{Logger, Record}; +use util::logger::Logger; use util::errors::APIError; use std; diff --git a/src/ln/channelmanager.rs b/src/ln/channelmanager.rs index 03cad49e..d273be52 100644 --- a/src/ln/channelmanager.rs +++ b/src/ln/channelmanager.rs @@ -20,7 +20,7 @@ use ln::msgs::{HandleError,ChannelMessageHandler,MsgEncodable,MsgDecodable}; use util::{byte_utils, events, internal_traits, rng}; use util::sha2::Sha256; use util::chacha20poly1305rfc::ChaCha20; -use util::logger::{Logger, Record}; +use util::logger::Logger; use util::errors::APIError; use crypto; diff --git a/src/ln/peer_handler.rs b/src/ln/peer_handler.rs index b44ddc51..11064bf9 100644 --- a/src/ln/peer_handler.rs +++ b/src/ln/peer_handler.rs @@ -5,7 +5,7 @@ use ln::msgs::{MsgEncodable,MsgDecodable}; use ln::peer_channel_encryptor::{PeerChannelEncryptor,NextNoiseStep}; use util::byte_utils; use util::events::{EventsProvider,Event}; -use util::logger::{Logger, Record}; +use util::logger::Logger; use std::collections::{HashMap,LinkedList}; use std::sync::{Arc, Mutex}; diff --git a/src/util/logger.rs b/src/util/logger.rs index bccc8cd2..720bccf4 100644 --- a/src/util/logger.rs +++ b/src/util/logger.rs @@ -126,7 +126,7 @@ pub trait Logger: Sync + Send { #[cfg(test)] mod tests { - use util::logger::{Logger, Level, Record}; + use util::logger::{Logger, Level}; use util::test_utils::TestLogger; use std::sync::{Arc}; diff --git a/src/util/macro_logger.rs b/src/util/macro_logger.rs index fd9a61b3..19f0294f 100644 --- a/src/util/macro_logger.rs +++ b/src/util/macro_logger.rs @@ -52,7 +52,7 @@ macro_rules! log_funding_channel_id { macro_rules! log_internal { ($self: ident, $lvl:expr, $($arg:tt)+) => ( - &$self.logger.log(&Record::new($lvl, format_args!($($arg)+), module_path!(), file!(), line!())); + &$self.logger.log(&::util::logger::Record::new($lvl, format_args!($($arg)+), module_path!(), file!(), line!())); ); } -- 2.30.2 From 7a04595269d69cfff52dd68e025c5c234e5556e4 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Mon, 20 Aug 2018 10:33:30 -0400 Subject: [PATCH 08/16] Only enforce no-dup-payment_hash precondition on non-removed HTLCs This fixes a panic found by fuzzer. --- src/ln/channel.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ln/channel.rs b/src/ln/channel.rs index c7c3208e..76d45dd7 100644 --- a/src/ln/channel.rs +++ b/src/ln/channel.rs @@ -1018,7 +1018,8 @@ impl Channel { let mut pending_idx = std::usize::MAX; for (idx, htlc) in self.pending_htlcs.iter().enumerate() { - if !htlc.outbound && htlc.payment_hash == payment_hash_calc { + if !htlc.outbound && htlc.payment_hash == payment_hash_calc && + htlc.state != HTLCState::LocalRemoved && htlc.state != HTLCState::LocalRemovedAwaitingCommitment { if pending_idx != std::usize::MAX { panic!("Duplicate HTLC payment_hash, ChannelManager should have prevented this!"); } @@ -1070,9 +1071,8 @@ impl Channel { // hopefully never happens. Instead, we make sure we get the preimage into the // channel_monitor and pretend we didn't just see the preimage. return Ok((None, Some(self.channel_monitor.clone()))); - } else if htlc.state == HTLCState::LocalRemoved || htlc.state == HTLCState::LocalRemovedAwaitingCommitment { - return Err(HandleError{err: "Unable to find a pending HTLC which matched the given payment preimage", action: None}); } else { + // LocalRemoved/LocalRemovedAwaitingCOmmitment handled in the search loop panic!("Have an inbound HTLC when not awaiting remote revoke that had a garbage state"); } htlc.htlc_id -- 2.30.2 From f476a19bde05ea01d0c99a5dad4915d8e835aad2 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Tue, 14 Aug 2018 10:43:34 -0400 Subject: [PATCH 09/16] Add simple utility to ChannelManager to force close all channels --- src/ln/channelmanager.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/ln/channelmanager.rs b/src/ln/channelmanager.rs index d273be52..c884f8a1 100644 --- a/src/ln/channelmanager.rs +++ b/src/ln/channelmanager.rs @@ -415,6 +415,14 @@ impl ChannelManager { } } + /// Force close all channels, immediately broadcasting the latest local commitment transaction + /// for each to the chain and rejecting new HTLCs on each. + pub fn force_close_all_channels(&self) { + for chan in self.list_channels() { + self.force_close_channel(&chan.channel_id); + } + } + #[inline] fn gen_rho_mu_from_shared_secret(shared_secret: &SharedSecret) -> ([u8; 32], [u8; 32]) { ({ -- 2.30.2 From 9f2c67ae6099aad80a4389747c44a48e1bd5c69f Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 17 Aug 2018 17:38:43 -0400 Subject: [PATCH 10/16] Expand full_stack_target somewhat --- fuzz/fuzz_targets/full_stack_target.rs | 125 ++++++++++++++++++++----- 1 file changed, 99 insertions(+), 26 deletions(-) diff --git a/fuzz/fuzz_targets/full_stack_target.rs b/fuzz/fuzz_targets/full_stack_target.rs index 9ce82aeb..2f8ed52f 100644 --- a/fuzz/fuzz_targets/full_stack_target.rs +++ b/fuzz/fuzz_targets/full_stack_target.rs @@ -7,7 +7,7 @@ use bitcoin::blockdata::block::BlockHeader; use bitcoin::blockdata::transaction::{Transaction, TxOut}; use bitcoin::blockdata::script::Script; use bitcoin::network::constants::Network; -use bitcoin::network::serialize::{serialize, BitcoinHash}; +use bitcoin::network::serialize::{deserialize, serialize, BitcoinHash}; use bitcoin::util::hash::Sha256dHash; use crypto::digest::Digest; @@ -32,6 +32,7 @@ use secp256k1::Secp256k1; use std::cell::RefCell; use std::collections::HashMap; +use std::cmp; use std::hash::Hash; use std::sync::Arc; use std::sync::atomic::{AtomicUsize,Ordering}; @@ -92,20 +93,12 @@ impl FeeEstimator for FuzzEstimator { fn get_est_sat_per_1000_weight(&self, _: ConfirmationTarget) -> u64 { //TODO: We should actually be testing at least much more than 64k... match self.input.get_slice(2) { - Some(slice) => slice_to_be16(slice) as u64 * 250, + Some(slice) => cmp::max(slice_to_be16(slice) as u64, 253), None => 0 } } } -struct TestChannelMonitor {} -impl channelmonitor::ManyChannelMonitor for TestChannelMonitor { - fn add_update_monitor(&self, _funding_txo: OutPoint, _monitor: channelmonitor::ChannelMonitor) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> { - //TODO! - Ok(()) - } -} - struct TestBroadcaster {} impl BroadcasterInterface for TestBroadcaster { fn broadcast_transaction(&self, _tx: &Transaction) {} @@ -138,6 +131,71 @@ impl<'a> Hash for Peer<'a> { } } +struct MoneyLossDetector<'a> { + manager: Arc, + monitor: Arc>, + handler: PeerManager>, + + peers: &'a RefCell<[bool; 256]>, + funding_txn: Vec, + header_hashes: Vec, + height: usize, + max_height: usize, + +} +impl<'a> MoneyLossDetector<'a> { + pub fn new(peers: &'a RefCell<[bool; 256]>, manager: Arc, monitor: Arc>, handler: PeerManager>) -> Self { + MoneyLossDetector { + manager, + monitor, + handler, + + peers, + funding_txn: Vec::new(), + header_hashes: vec![Default::default()], + height: 0, + max_height: 0, + } + } + + fn connect_block(&mut self, txn: &[&Transaction], txn_idxs: &[u32]) { + let header = BlockHeader { version: 0x20000000, prev_blockhash: self.header_hashes[self.height], merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }; + self.height += 1; + self.manager.block_connected(&header, self.height as u32, txn, txn_idxs); + (*self.monitor).block_connected(&header, self.height as u32, txn, txn_idxs); + if self.header_hashes.len() > self.height { + self.header_hashes[self.height] = header.bitcoin_hash(); + } else { + assert_eq!(self.header_hashes.len(), self.height); + self.header_hashes.push(header.bitcoin_hash()); + } + self.max_height = cmp::max(self.height, self.max_height); + } + + fn disconnect_block(&mut self) { + if self.height > 0 && (self.max_height < 6 || self.height >= self.max_height - 6) { + self.height -= 1; + let header = BlockHeader { version: 0x20000000, prev_blockhash: self.header_hashes[self.height], merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }; + self.manager.block_disconnected(&header); + self.monitor.block_disconnected(&header); + } + } +} + +impl<'a> Drop for MoneyLossDetector<'a> { + fn drop(&mut self) { + // Disconnect all peers + for (idx, peer) in self.peers.borrow().iter().enumerate() { + if *peer { + self.handler.disconnect_event(&Peer{id: idx as u8, peers_connected: &self.peers}); + } + } + + // Force all channels onto the chain (and time out claim txn) + self.manager.force_close_all_channels(); + } +} + #[inline] pub fn do_test(data: &[u8]) { reset_rng_state(); @@ -175,18 +233,18 @@ pub fn do_test(data: &[u8]) { }; let logger: Arc = Arc::new(test_logger::TestLogger{}); - let monitor = Arc::new(TestChannelMonitor{}); let watch = Arc::new(ChainWatchInterfaceUtil::new(Arc::clone(&logger))); let broadcast = Arc::new(TestBroadcaster{}); + let monitor = channelmonitor::SimpleManyChannelMonitor::new(watch.clone(), broadcast.clone()); let channelmanager = ChannelManager::new(our_network_key, slice_to_be32(get_slice!(4)), get_slice!(1)[0] != 0, Network::Bitcoin, fee_est.clone(), monitor.clone(), watch.clone(), broadcast.clone(), Arc::clone(&logger)).unwrap(); let router = Arc::new(Router::new(PublicKey::from_secret_key(&secp_ctx, &our_network_key).unwrap(), Arc::clone(&logger))); let peers = RefCell::new([false; 256]); - let handler = PeerManager::new(MessageHandler { + let mut loss_detector = MoneyLossDetector::new(&peers, channelmanager.clone(), monitor.clone(), PeerManager::new(MessageHandler { chan_handler: channelmanager.clone(), route_handler: router.clone(), - }, our_network_key, Arc::clone(&logger)); + }, our_network_key, Arc::clone(&logger))); let mut should_forward = false; let mut payments_received: Vec<[u8; 32]> = Vec::new(); @@ -206,8 +264,8 @@ pub fn do_test(data: &[u8]) { } } if new_id == 0 { return; } + loss_detector.handler.new_outbound_connection(get_pubkey!(), Peer{id: (new_id - 1) as u8, peers_connected: &peers}).unwrap(); peers.borrow_mut()[new_id - 1] = true; - handler.new_outbound_connection(get_pubkey!(), Peer{id: (new_id - 1) as u8, peers_connected: &peers}).unwrap(); }, 1 => { let mut new_id = 0; @@ -218,19 +276,19 @@ pub fn do_test(data: &[u8]) { } } if new_id == 0 { return; } + loss_detector.handler.new_inbound_connection(Peer{id: (new_id - 1) as u8, peers_connected: &peers}).unwrap(); peers.borrow_mut()[new_id - 1] = true; - handler.new_inbound_connection(Peer{id: (new_id - 1) as u8, peers_connected: &peers}).unwrap(); }, 2 => { let peer_id = get_slice!(1)[0]; if !peers.borrow()[peer_id as usize] { return; } + loss_detector.handler.disconnect_event(&Peer{id: peer_id, peers_connected: &peers}); peers.borrow_mut()[peer_id as usize] = false; - handler.disconnect_event(&Peer{id: peer_id, peers_connected: &peers}); }, 3 => { let peer_id = get_slice!(1)[0]; if !peers.borrow()[peer_id as usize] { return; } - match handler.read_event(&mut Peer{id: peer_id, peers_connected: &peers}, get_slice!(get_slice!(1)[0]).to_vec()) { + match loss_detector.handler.read_event(&mut Peer{id: peer_id, peers_connected: &peers}, get_slice!(get_slice!(1)[0]).to_vec()) { Ok(res) => assert!(!res), Err(_) => { peers.borrow_mut()[peer_id as usize] = false; } } @@ -270,7 +328,6 @@ pub fn do_test(data: &[u8]) { 7 => { if should_forward { channelmanager.process_pending_htlc_forwards(); - handler.process_events(); should_forward = false; } }, @@ -321,20 +378,36 @@ pub fn do_test(data: &[u8]) { txn_idxs.push(idx as u32 + 1); } - let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }; - channelmanager.block_connected(&header, 1, &txn[..], &txn_idxs[..]); - txn.clear(); + loss_detector.connect_block(&txn[..], &txn_idxs[..]); txn_idxs.clear(); - for i in 2..100 { - header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }; - channelmanager.block_connected(&header, i, &txn[..], &txn_idxs[..]); + for _ in 2..100 { + loss_detector.connect_block(&txn[..], &txn_idxs[..]); } } - pending_funding_relay.clear(); + for tx in pending_funding_relay.drain(..) { + loss_detector.funding_txn.push(tx); + } + }, + 12 => { + let txlen = slice_to_be16(get_slice!(2)); + if txlen == 0 { + loss_detector.connect_block(&[], &[]); + } else { + let txres: Result = deserialize(get_slice!(txlen)); + if let Ok(tx) = txres { + loss_detector.connect_block(&[&tx], &[1]); + } else { + return; + } + } + }, + 13 => { + loss_detector.disconnect_block(); }, _ => return, } - for event in handler.get_and_clear_pending_events() { + loss_detector.handler.process_events(); + for event in loss_detector.handler.get_and_clear_pending_events() { match event { Event::FundingGenerationReady { temporary_channel_id, channel_value_satoshis, output_script, .. } => { pending_funding_generation.push((temporary_channel_id, channel_value_satoshis, output_script)); -- 2.30.2 From 1b8f4acb275621d52316ae1e5f1059b15f3a120e Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Thu, 16 Aug 2018 14:20:34 -0400 Subject: [PATCH 11/16] Upgrade AFL to 0.4 with persistent mode fuzzing --- fuzz/Cargo.toml | 2 +- fuzz/fuzz_targets/chanmon_deser_target.rs | 6 +++--- fuzz/fuzz_targets/channel_target.rs | 6 +++--- fuzz/fuzz_targets/full_stack_target.rs | 6 +++--- fuzz/fuzz_targets/msg_ping_target.rs | 6 +++--- fuzz/fuzz_targets/msg_pong_target.rs | 6 +++--- fuzz/fuzz_targets/msg_targets/msg_accept_channel_target.rs | 6 +++--- .../msg_targets/msg_channel_reestablish_target.rs | 6 +++--- fuzz/fuzz_targets/msg_targets/msg_closing_signed_target.rs | 6 +++--- .../msg_targets/msg_commitment_signed_target.rs | 6 +++--- fuzz/fuzz_targets/msg_targets/msg_error_message_target.rs | 6 +++--- fuzz/fuzz_targets/msg_targets/msg_funding_created_target.rs | 6 +++--- fuzz/fuzz_targets/msg_targets/msg_funding_locked_target.rs | 6 +++--- fuzz/fuzz_targets/msg_targets/msg_funding_signed_target.rs | 6 +++--- fuzz/fuzz_targets/msg_targets/msg_open_channel_target.rs | 6 +++--- fuzz/fuzz_targets/msg_targets/msg_revoke_and_ack_target.rs | 6 +++--- fuzz/fuzz_targets/msg_targets/msg_shutdown_target.rs | 6 +++--- fuzz/fuzz_targets/msg_targets/msg_target_template.txt | 6 +++--- fuzz/fuzz_targets/msg_targets/msg_update_add_htlc_target.rs | 6 +++--- .../fuzz_targets/msg_targets/msg_update_fail_htlc_target.rs | 6 +++--- .../msg_targets/msg_update_fail_malformed_htlc_target.rs | 6 +++--- fuzz/fuzz_targets/msg_targets/msg_update_fee_target.rs | 6 +++--- .../msg_targets/msg_update_fulfill_htlc_target.rs | 6 +++--- fuzz/fuzz_targets/peer_crypt_target.rs | 6 +++--- fuzz/fuzz_targets/router_target.rs | 6 +++--- 25 files changed, 73 insertions(+), 73 deletions(-) diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index 516dd4ce..7455076d 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -16,7 +16,7 @@ afl_fuzz = ["afl"] honggfuzz_fuzz = ["honggfuzz"] [dependencies] -afl = { version = "0.3", optional = true } +afl = { version = "0.4", optional = true } lightning = { path = "..", features = ["fuzztarget"] } bitcoin = { version = "0.13", features = ["fuzztarget"] } hex = "0.3" diff --git a/fuzz/fuzz_targets/chanmon_deser_target.rs b/fuzz/fuzz_targets/chanmon_deser_target.rs index adf4a3a6..a7e9079a 100644 --- a/fuzz/fuzz_targets/chanmon_deser_target.rs +++ b/fuzz/fuzz_targets/chanmon_deser_target.rs @@ -16,11 +16,11 @@ pub fn do_test(data: &[u8]) { } #[cfg(feature = "afl")] -extern crate afl; +#[macro_use] extern crate afl; #[cfg(feature = "afl")] fn main() { - afl::read_stdio_bytes(|data| { - do_test(&data); + fuzz!(|data| { + do_test(data); }); } diff --git a/fuzz/fuzz_targets/channel_target.rs b/fuzz/fuzz_targets/channel_target.rs index 903b45fb..65f0419f 100644 --- a/fuzz/fuzz_targets/channel_target.rs +++ b/fuzz/fuzz_targets/channel_target.rs @@ -325,11 +325,11 @@ pub fn do_test(data: &[u8]) { } #[cfg(feature = "afl")] -extern crate afl; +#[macro_use] extern crate afl; #[cfg(feature = "afl")] fn main() { - afl::read_stdio_bytes(|data| { - do_test(&data); + fuzz!(|data| { + do_test(data); }); } diff --git a/fuzz/fuzz_targets/full_stack_target.rs b/fuzz/fuzz_targets/full_stack_target.rs index 2f8ed52f..8e3bea9c 100644 --- a/fuzz/fuzz_targets/full_stack_target.rs +++ b/fuzz/fuzz_targets/full_stack_target.rs @@ -431,11 +431,11 @@ pub fn do_test(data: &[u8]) { } #[cfg(feature = "afl")] -extern crate afl; +#[macro_use] extern crate afl; #[cfg(feature = "afl")] fn main() { - afl::read_stdio_bytes(|data| { - do_test(&data); + fuzz!(|data| { + do_test(data); }); } diff --git a/fuzz/fuzz_targets/msg_ping_target.rs b/fuzz/fuzz_targets/msg_ping_target.rs index a2e0d341..c9fb3418 100644 --- a/fuzz/fuzz_targets/msg_ping_target.rs +++ b/fuzz/fuzz_targets/msg_ping_target.rs @@ -16,11 +16,11 @@ pub fn do_test(data: &[u8]) { } #[cfg(feature = "afl")] -extern crate afl; +#[macro_use] extern crate afl; #[cfg(feature = "afl")] fn main() { - afl::read_stdio_bytes(|data| { - do_test(&data); + fuzz!(|data| { + do_test(data); }); } diff --git a/fuzz/fuzz_targets/msg_pong_target.rs b/fuzz/fuzz_targets/msg_pong_target.rs index a61420db..d4572d33 100644 --- a/fuzz/fuzz_targets/msg_pong_target.rs +++ b/fuzz/fuzz_targets/msg_pong_target.rs @@ -16,11 +16,11 @@ pub fn do_test(data: &[u8]) { } #[cfg(feature = "afl")] -extern crate afl; +#[macro_use] extern crate afl; #[cfg(feature = "afl")] fn main() { - afl::read_stdio_bytes(|data| { - do_test(&data); + fuzz!(|data| { + do_test(data); }); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_accept_channel_target.rs b/fuzz/fuzz_targets/msg_targets/msg_accept_channel_target.rs index 9e4b7642..d7f0a881 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_accept_channel_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_accept_channel_target.rs @@ -17,11 +17,11 @@ pub fn do_test(data: &[u8]) { } #[cfg(feature = "afl")] -extern crate afl; +#[macro_use] extern crate afl; #[cfg(feature = "afl")] fn main() { - afl::read_stdio_bytes(|data| { - do_test(&data); + fuzz!(|data| { + do_test(data); }); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_channel_reestablish_target.rs b/fuzz/fuzz_targets/msg_targets/msg_channel_reestablish_target.rs index 09ad367e..38f91604 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_channel_reestablish_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_channel_reestablish_target.rs @@ -17,11 +17,11 @@ pub fn do_test(data: &[u8]) { } #[cfg(feature = "afl")] -extern crate afl; +#[macro_use] extern crate afl; #[cfg(feature = "afl")] fn main() { - afl::read_stdio_bytes(|data| { - do_test(&data); + fuzz!(|data| { + do_test(data); }); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_closing_signed_target.rs b/fuzz/fuzz_targets/msg_targets/msg_closing_signed_target.rs index 7c87970a..504e1e37 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_closing_signed_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_closing_signed_target.rs @@ -17,11 +17,11 @@ pub fn do_test(data: &[u8]) { } #[cfg(feature = "afl")] -extern crate afl; +#[macro_use] extern crate afl; #[cfg(feature = "afl")] fn main() { - afl::read_stdio_bytes(|data| { - do_test(&data); + fuzz!(|data| { + do_test(data); }); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_commitment_signed_target.rs b/fuzz/fuzz_targets/msg_targets/msg_commitment_signed_target.rs index 1ded2cfe..701dd1fb 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_commitment_signed_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_commitment_signed_target.rs @@ -17,11 +17,11 @@ pub fn do_test(data: &[u8]) { } #[cfg(feature = "afl")] -extern crate afl; +#[macro_use] extern crate afl; #[cfg(feature = "afl")] fn main() { - afl::read_stdio_bytes(|data| { - do_test(&data); + fuzz!(|data| { + do_test(data); }); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_error_message_target.rs b/fuzz/fuzz_targets/msg_targets/msg_error_message_target.rs index 8fa7de51..a69ded2e 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_error_message_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_error_message_target.rs @@ -17,11 +17,11 @@ pub fn do_test(data: &[u8]) { } #[cfg(feature = "afl")] -extern crate afl; +#[macro_use] extern crate afl; #[cfg(feature = "afl")] fn main() { - afl::read_stdio_bytes(|data| { - do_test(&data); + fuzz!(|data| { + do_test(data); }); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_funding_created_target.rs b/fuzz/fuzz_targets/msg_targets/msg_funding_created_target.rs index 45c7c408..7289267c 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_funding_created_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_funding_created_target.rs @@ -17,11 +17,11 @@ pub fn do_test(data: &[u8]) { } #[cfg(feature = "afl")] -extern crate afl; +#[macro_use] extern crate afl; #[cfg(feature = "afl")] fn main() { - afl::read_stdio_bytes(|data| { - do_test(&data); + fuzz!(|data| { + do_test(data); }); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_funding_locked_target.rs b/fuzz/fuzz_targets/msg_targets/msg_funding_locked_target.rs index e9e6fec5..bfafdf48 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_funding_locked_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_funding_locked_target.rs @@ -17,11 +17,11 @@ pub fn do_test(data: &[u8]) { } #[cfg(feature = "afl")] -extern crate afl; +#[macro_use] extern crate afl; #[cfg(feature = "afl")] fn main() { - afl::read_stdio_bytes(|data| { - do_test(&data); + fuzz!(|data| { + do_test(data); }); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_funding_signed_target.rs b/fuzz/fuzz_targets/msg_targets/msg_funding_signed_target.rs index a92e2fab..6a2b6ac3 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_funding_signed_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_funding_signed_target.rs @@ -17,11 +17,11 @@ pub fn do_test(data: &[u8]) { } #[cfg(feature = "afl")] -extern crate afl; +#[macro_use] extern crate afl; #[cfg(feature = "afl")] fn main() { - afl::read_stdio_bytes(|data| { - do_test(&data); + fuzz!(|data| { + do_test(data); }); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_open_channel_target.rs b/fuzz/fuzz_targets/msg_targets/msg_open_channel_target.rs index 7c1d6f82..737bc2f0 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_open_channel_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_open_channel_target.rs @@ -17,11 +17,11 @@ pub fn do_test(data: &[u8]) { } #[cfg(feature = "afl")] -extern crate afl; +#[macro_use] extern crate afl; #[cfg(feature = "afl")] fn main() { - afl::read_stdio_bytes(|data| { - do_test(&data); + fuzz!(|data| { + do_test(data); }); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_revoke_and_ack_target.rs b/fuzz/fuzz_targets/msg_targets/msg_revoke_and_ack_target.rs index 560e05b0..6086d279 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_revoke_and_ack_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_revoke_and_ack_target.rs @@ -17,11 +17,11 @@ pub fn do_test(data: &[u8]) { } #[cfg(feature = "afl")] -extern crate afl; +#[macro_use] extern crate afl; #[cfg(feature = "afl")] fn main() { - afl::read_stdio_bytes(|data| { - do_test(&data); + fuzz!(|data| { + do_test(data); }); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_shutdown_target.rs b/fuzz/fuzz_targets/msg_targets/msg_shutdown_target.rs index 5df8a382..29243dab 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_shutdown_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_shutdown_target.rs @@ -17,11 +17,11 @@ pub fn do_test(data: &[u8]) { } #[cfg(feature = "afl")] -extern crate afl; +#[macro_use] extern crate afl; #[cfg(feature = "afl")] fn main() { - afl::read_stdio_bytes(|data| { - do_test(&data); + fuzz!(|data| { + do_test(data); }); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_target_template.txt b/fuzz/fuzz_targets/msg_targets/msg_target_template.txt index f65e91f5..599a4f72 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_target_template.txt +++ b/fuzz/fuzz_targets/msg_targets/msg_target_template.txt @@ -17,11 +17,11 @@ pub fn do_test(data: &[u8]) { } #[cfg(feature = "afl")] -extern crate afl; +#[macro_use] extern crate afl; #[cfg(feature = "afl")] fn main() { - afl::read_stdio_bytes(|data| { - do_test(&data); + fuzz!(|data| { + do_test(data); }); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_update_add_htlc_target.rs b/fuzz/fuzz_targets/msg_targets/msg_update_add_htlc_target.rs index 93cdaa42..07a0f819 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_update_add_htlc_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_update_add_htlc_target.rs @@ -17,11 +17,11 @@ pub fn do_test(data: &[u8]) { } #[cfg(feature = "afl")] -extern crate afl; +#[macro_use] extern crate afl; #[cfg(feature = "afl")] fn main() { - afl::read_stdio_bytes(|data| { - do_test(&data); + fuzz!(|data| { + do_test(data); }); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_update_fail_htlc_target.rs b/fuzz/fuzz_targets/msg_targets/msg_update_fail_htlc_target.rs index ffac1218..27eaf123 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_update_fail_htlc_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_update_fail_htlc_target.rs @@ -17,11 +17,11 @@ pub fn do_test(data: &[u8]) { } #[cfg(feature = "afl")] -extern crate afl; +#[macro_use] extern crate afl; #[cfg(feature = "afl")] fn main() { - afl::read_stdio_bytes(|data| { - do_test(&data); + fuzz!(|data| { + do_test(data); }); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_update_fail_malformed_htlc_target.rs b/fuzz/fuzz_targets/msg_targets/msg_update_fail_malformed_htlc_target.rs index 9c909925..eb6dc94a 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_update_fail_malformed_htlc_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_update_fail_malformed_htlc_target.rs @@ -17,11 +17,11 @@ pub fn do_test(data: &[u8]) { } #[cfg(feature = "afl")] -extern crate afl; +#[macro_use] extern crate afl; #[cfg(feature = "afl")] fn main() { - afl::read_stdio_bytes(|data| { - do_test(&data); + fuzz!(|data| { + do_test(data); }); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_update_fee_target.rs b/fuzz/fuzz_targets/msg_targets/msg_update_fee_target.rs index 4aa72712..eec1d262 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_update_fee_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_update_fee_target.rs @@ -17,11 +17,11 @@ pub fn do_test(data: &[u8]) { } #[cfg(feature = "afl")] -extern crate afl; +#[macro_use] extern crate afl; #[cfg(feature = "afl")] fn main() { - afl::read_stdio_bytes(|data| { - do_test(&data); + fuzz!(|data| { + do_test(data); }); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_update_fulfill_htlc_target.rs b/fuzz/fuzz_targets/msg_targets/msg_update_fulfill_htlc_target.rs index 86e99f8c..3dd87b30 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_update_fulfill_htlc_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_update_fulfill_htlc_target.rs @@ -17,11 +17,11 @@ pub fn do_test(data: &[u8]) { } #[cfg(feature = "afl")] -extern crate afl; +#[macro_use] extern crate afl; #[cfg(feature = "afl")] fn main() { - afl::read_stdio_bytes(|data| { - do_test(&data); + fuzz!(|data| { + do_test(data); }); } diff --git a/fuzz/fuzz_targets/peer_crypt_target.rs b/fuzz/fuzz_targets/peer_crypt_target.rs index 4133331a..6305cf04 100644 --- a/fuzz/fuzz_targets/peer_crypt_target.rs +++ b/fuzz/fuzz_targets/peer_crypt_target.rs @@ -80,11 +80,11 @@ pub fn do_test(data: &[u8]) { } #[cfg(feature = "afl")] -extern crate afl; +#[macro_use] extern crate afl; #[cfg(feature = "afl")] fn main() { - afl::read_stdio_bytes(|data| { - do_test(&data); + fuzz!(|data| { + do_test(data); }); } diff --git a/fuzz/fuzz_targets/router_target.rs b/fuzz/fuzz_targets/router_target.rs index 2a7e734e..452369e5 100644 --- a/fuzz/fuzz_targets/router_target.rs +++ b/fuzz/fuzz_targets/router_target.rs @@ -181,11 +181,11 @@ pub fn do_test(data: &[u8]) { } #[cfg(feature = "afl")] -extern crate afl; +#[macro_use] extern crate afl; #[cfg(feature = "afl")] fn main() { - afl::read_stdio_bytes(|data| { - do_test(&data); + fuzz!(|data| { + do_test(data); }); } -- 2.30.2 From 87aabff8abb5928373e42c966326b6b4d0993372 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 17 Aug 2018 17:36:22 -0400 Subject: [PATCH 12/16] Disable push_msat in full_stack_target temporarily --- fuzz/fuzz_targets/full_stack_target.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/fuzz/fuzz_targets/full_stack_target.rs b/fuzz/fuzz_targets/full_stack_target.rs index 8e3bea9c..5104b707 100644 --- a/fuzz/fuzz_targets/full_stack_target.rs +++ b/fuzz/fuzz_targets/full_stack_target.rs @@ -315,7 +315,9 @@ pub fn do_test(data: &[u8]) { if !peers.borrow()[peer_id as usize] { return; } let their_key = get_pubkey!(); let chan_value = slice_to_be24(get_slice!(3)) as u64; - let push_msat_value = slice_to_be24(get_slice!(3)) as u64; + //let push_msat_value = slice_to_be24(get_slice!(3)) as u64; + // Temporarily don't read extra bytes to avoid breaking existing test-cases + let push_msat_value = 0; if channelmanager.create_channel(their_key, chan_value, push_msat_value, 0).is_err() { return; } }, 6 => { -- 2.30.2 From 06d3b4babedb6a3353f8bce6e23069718bf96040 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Tue, 14 Aug 2018 09:55:38 -0400 Subject: [PATCH 13/16] Add a full_stack_target sample test to avoid unintentional breakage --- fuzz/fuzz_targets/full_stack_target.rs | 257 ++++++++++++++++++++++++- 1 file changed, 252 insertions(+), 5 deletions(-) diff --git a/fuzz/fuzz_targets/full_stack_target.rs b/fuzz/fuzz_targets/full_stack_target.rs index 5104b707..18d9dd59 100644 --- a/fuzz/fuzz_targets/full_stack_target.rs +++ b/fuzz/fuzz_targets/full_stack_target.rs @@ -197,7 +197,7 @@ impl<'a> Drop for MoneyLossDetector<'a> { } #[inline] -pub fn do_test(data: &[u8]) { +pub fn do_test(data: &[u8], logger: &Arc) { reset_rng_state(); let input = Arc::new(InputData { @@ -232,7 +232,6 @@ pub fn do_test(data: &[u8]) { Err(_) => return, }; - let logger: Arc = Arc::new(test_logger::TestLogger{}); let watch = Arc::new(ChainWatchInterfaceUtil::new(Arc::clone(&logger))); let broadcast = Arc::new(TestBroadcaster{}); let monitor = channelmonitor::SimpleManyChannelMonitor::new(watch.clone(), broadcast.clone()); @@ -437,7 +436,8 @@ pub fn do_test(data: &[u8]) { #[cfg(feature = "afl")] fn main() { fuzz!(|data| { - do_test(data); + let logger: Arc = Arc::new(test_logger::TestLogger{}); + do_test(data, &logger); }); } @@ -447,7 +447,8 @@ fn main() { fn main() { loop { fuzz!(|data| { - do_test(data); + let logger: Arc = Arc::new(test_logger::TestLogger{}); + do_test(data, &logger); }); } } @@ -455,8 +456,254 @@ fn main() { extern crate hex; #[cfg(test)] mod tests { + use utils::test_logger; + use lightning::util::logger::{Logger, Record}; + use std::collections::HashMap; + use std::sync::{Arc, Mutex}; + #[test] fn duplicate_crash() { - super::do_test(&::hex::decode("00").unwrap()); + let logger: Arc = Arc::new(test_logger::TestLogger{}); + super::do_test(&::hex::decode("00").unwrap(), &logger); + } + + struct TrackingLogger { + /// (module, message) -> count + pub lines: Mutex>, + } + impl Logger for TrackingLogger { + fn log(&self, record: &Record) { + *self.lines.lock().unwrap().entry((record.module_path.to_string(), format!("{}", record.args))).or_insert(0) += 1; + println!("{:<5} [{} : {}, {}] {}", record.level.to_string(), record.module_path, record.file, record.line, record.args); + } + } + + #[test] + fn test_no_existing_test_breakage() { + // To avoid accidentally causing all existing fuzz test cases to be useless by making minor + // changes (such as requesting feerate info in a new place), we run a pretty full + // step-through with two peers and HTLC forwarding here. Obviously this is pretty finicky, + // so this should be updated pretty liberally, but at least we'll know when changes occur. + // If nothing else, this test serves as a pretty great initial full_stack_target seed. + + // What each byte represents is broken down below, and then everything is concatenated into + // one large test at the end (you want %s/ -.*//g %s/\n\| \|\t\|\///g). + + // 0000000000000000000000000000000000000000000000000000000000000000 - our network key + // 00000000 - fee_proportional_millionths + // 01 - announce_channels_publicly + // + // 00 - new outbound connection with id 0 + // 030000000000000000000000000000000000000000000000000000000000000000 - peer's pubkey + // 030032 - inbound read from peer id 0 of len 50 + // 00 030000000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - noise act two (0||pubkey||mac) + // + // 030012 - inbound read from peer id 0 of len 18 + // 0006 03000000000000000000000000000000 - message header indicating message length 6 + // 030016 - inbound read from peer id 0 of len 22 + // 0010 00000000 03000000000000000000000000000000 - init message with no features (type 16) + // + // 030012 - inbound read from peer id 0 of len 18 + // 0141 03000000000000000000000000000000 - message header indicating message length 321 + // 0300ff - inbound read from peer id 0 of len 255 + // 0020 7500000000000000000000000000000000000000000000000000000000000000ff4f00f805273c1b203bb5ebf8436bfde57b3be8c2f5e95d9491dbb181909679000000000000c35000000000000000000000000000000222ffffffffffffffff00000000000002220000000000000000000000fd000601e303000000000000000000000000000000000000000000000000000000000000000103000000000000000000000000000000000000000000000000000000000000000203000000000000000000000000000000000000000000000000000000000000000303000000000000000000000000000000000000000000000000000000000000000403 - beginning of open_channel message + // 030052 - inbound read from peer id 0 of len 82 + // 0000000000000000000000000000000000000000000000000000000000000005 030100000000000000000000000000000000000000000000000000000000000000 01 03000000000000000000000000000000 - rest of open_channel and mac + // + // 00fd00fd00fd - Three feerate requests (all returning min feerate, which our open_channel also uses) + // - client should now respond with accept_channel (CHECK 1: type 33 to peer 03000000) + // + // 030012 - inbound read from peer id 0 of len 18 + // 0084 03000000000000000000000000000000 - message header indicating message length 132 + // 030094 - inbound read from peer id 0 of len 148 + // 0022 ff4f00f805273c1b203bb5ebf8436bfde57b3be8c2f5e95d9491dbb1819096793d00000000000000000000000000000000000000000000000000000000000000000036000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 03000000000000000000000000000000 - funding_created and mac + // - client should now respond with funding_signed (CHECK 2: type 35 to peer 03000000) + // + // 0c005e - connect a block with one transaction of len 94 + // 020000000100000000000000000000000000000000000000000000000000000000000000000000000000ffffffff0150c3000000000000220020ae0000000000000000000000000000000000000000000000000000000000000000000000 - the funding transaction + // 0c0000 - connect a block with no transactions + // 0c0000 - connect a block with no transactions + // 0c0000 - connect a block with no transactions + // 0c0000 - connect a block with no transactions + // 0c0000 - connect a block with no transactions + // 0c0000 - connect a block with no transactions + // 0c0000 - connect a block with no transactions + // 0c0000 - connect a block with no transactions + // 0c0000 - connect a block with no transactions + // 0c0000 - connect a block with no transactions + // 0c0000 - connect a block with no transactions + // 0c0000 - connect a block with no transactions + // - by now client should have sent a funding_locked (CHECK 3: SendFundingLocked to 03000000 for chan 3d000000) + // + // 030012 - inbound read from peer id 0 of len 18 + // 0043 03000000000000000000000000000000 - message header indicating message length 67 + // 030053 - inbound read from peer id 0 of len 83 + // 0024 3d00000000000000000000000000000000000000000000000000000000000000 030000000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - funding_locked and mac + // + // 01 - new inbound connection with id 1 + // 030132 - inbound read from peer id 1 of len 50 + // 0003000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000 - inbound noise act 1 + // 030142 - inbound read from peer id 1 of len 66 + // 000302000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003000000000000000000000000000000 - inbound noise act 3 + // + // 030112 - inbound read from peer id 1 of len 18 + // 0006 01000000000000000000000000000000 - message header indicating message length 6 + // 030116 - inbound read from peer id 1 of len 22 + // 0010 00000000 01000000000000000000000000000000 - init message with no features (type 16) + // + // 050103020000000000000000000000000000000000000000000000000000000000000000c350 - create outbound channel to peer 1 for 50k sat + // 00fd00fd00fd - Three feerate requests (all returning min feerate) + // + // 030112 - inbound read from peer id 1 of len 18 + // 0110 01000000000000000000000000000000 - message header indicating message length 272 + // 0301ff - inbound read from peer id 1 of len 255 + // 0021 0200000000000000020000000000000002000000000000000200000000000000000000000000001a00000000004c4b4000000000000003e800000000000003e80000000203f0000503000000000000000000000000000000000000000000000000000000000000010003000000000000000000000000000000000000000000000000000000000000020003000000000000000000000000000000000000000000000000000000000000030003000000000000000000000000000000000000000000000000000000000000040003000000000000000000000000000000000000000000000000000000000000050003000000000000000000000000000000 - beginning of accept_channel + // 030121 - inbound read from peer id 1 of len 33 + // 0000000000000000000000000000000000 01000000000000000000000000000000 - rest of accept_channel and mac + // + // 0a - create the funding transaction (client should send funding_created now) + // + // 030112 - inbound read from peer id 1 of len 18 + // 0062 01000000000000000000000000000000 - message header indicating message length 98 + // 030172 - inbound read from peer id 1 of len 114 + // 0023 3f00000000000000000000000000000000000000000000000000000000000000f7000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100 01000000000000000000000000000000 - funding_signed message and mac + // + // 0b - broadcast funding transaction + // - by now client should have sent a funding_locked (CHECK 4: SendFundingLocked to 03020000 for chan 3f000000) + // + // 030112 - inbound read from peer id 1 of len 18 + // 0043 01000000000000000000000000000000 - message header indicating message length 67 + // 030153 - inbound read from peer id 1 of len 83 + // 0024 3f00000000000000000000000000000000000000000000000000000000000000 030100777777777777777777777777777777777777777777777777777777777777 01000000000000000000000000000000 - funding_locked and mac + // + // 030012 - inbound read from peer id 0 of len 18 + // 05ac 03000000000000000000000000000000 - message header indicating message length 1452 + // 0300ff - inbound read from peer id 0 of len 255 + // 0080 3d00000000000000000000000000000000000000000000000000000000000000 0000000000000000 00000000000003e8 ff00000000000000000000000000000000000000000000000000000000000000 00000001 00 030000000000000000000000000000000000000000000000000000000000000555 0000000e000001000000000000000003e8000000010000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - beginning of update_add_htlc from 0 to 1 via client + // 0300ff - inbound read from peer id 0 of len 255 + // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + // 0300ff - inbound read from peer id 0 of len 255 + // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + // 0300ff - inbound read from peer id 0 of len 255 + // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + // 0300ff - inbound read from peer id 0 of len 255 + // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + // 0300c1 - inbound read from peer id 0 of len 193 + // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ef00000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - end of update_add_htlc from 0 to 1 via client and mac + // + // 030012 - inbound read from peer id 0 of len 18 + // 0064 03000000000000000000000000000000 - message header indicating message length 100 + // 030074 - inbound read from peer id 0 of len 116 + // 0084 3d00000000000000000000000000000000000000000000000000000000000000 36000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 0000 03000000000000000000000000000000 - commitment_signed and mac + // - client should now respond with revoke_and_ack and commitment_signed (CHECK 5/6: types 133 and 132 to peer 03000000) + // + // 030012 - inbound read from peer id 0 of len 18 + // 0063 03000000000000000000000000000000 - message header indicating message length 99 + // 030073 - inbound read from peer id 0 of len 115 + // 0085 3d00000000000000000000000000000000000000000000000000000000000000 0100000000000000000000000000000000000000000000000000000000000000 031111111111111111111111111111111111111111111111111111111111111111 03000000000000000000000000000000 - revoke_and_ack and mac + // + // 07 - process the now-pending HTLC forward + // - client now sends id 1 update_add_htlc and commitment_signed (CHECK 7: SendHTLCs event for node 03020000 with 1 HTLCs for channel 3f000000) + // + // - we respond with commitment_signed then revoke_and_ack (a weird, but valid, order) + // 030112 - inbound read from peer id 1 of len 18 + // 0064 01000000000000000000000000000000 - message header indicating message length 100 + // 030174 - inbound read from peer id 1 of len 116 + // 0084 3f00000000000000000000000000000000000000000000000000000000000000 f6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100 0000 01000000000000000000000000000000 - commitment_signed and mac + // + // 030112 - inbound read from peer id 1 of len 18 + // 0063 01000000000000000000000000000000 - message header indicating message length 99 + // 030173 - inbound read from peer id 1 of len 115 + // 0085 3f00000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 032222222222222222222222222222222222222222222222222222222222222222 01000000000000000000000000000000 - revoke_and_ack and mac + // + // 030112 - inbound read from peer id 1 of len 18 + // 004a 01000000000000000000000000000000 - message header indicating message length 74 + // 03015a - inbound read from peer id 1 of len 90 + // 0082 3f00000000000000000000000000000000000000000000000000000000000000 0000000000000000 ff00888888888888888888888888888888888888888888888888888888888888 01000000000000000000000000000000 - update_fulfill_htlc and mac + // - client should immediately claim the pending HTLC from peer 0 (CHECK 8: SendFulfillHTLCs for node 03000000 with preimage ff00888888 for channel 3d000000) + // + // 030112 - inbound read from peer id 1 of len 18 + // 0064 01000000000000000000000000000000 - message header indicating message length 100 + // 030174 - inbound read from peer id 1 of len 116 + // 0084 3f00000000000000000000000000000000000000000000000000000000000000 f4000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100 0000 01000000000000000000000000000000 - commitment_signed and mac + // + // 030112 - inbound read from peer id 1 of len 18 + // 0063 01000000000000000000000000000000 - message header indicating message length 99 + // 030173 - inbound read from peer id 1 of len 115 + // 0085 3f00000000000000000000000000000000000000000000000000000000000000 0100777777777777777777777777777777777777777777777777777777777777 033333333333333333333333333333333333333333333333333333333333333333 01000000000000000000000000000000 - revoke_and_ack and mac + // + // - before responding to the commitment_signed generated above, send a new HTLC + // 030012 - inbound read from peer id 0 of len 18 + // 05ac 03000000000000000000000000000000 - message header indicating message length 1452 + // 0300ff - inbound read from peer id 0 of len 255 + // 0080 3d00000000000000000000000000000000000000000000000000000000000000 0000000000000001 00000000000003e8 ff00000000000000000000000000000000000000000000000000000000000000 00000001 00 030000000000000000000000000000000000000000000000000000000000000555 0000000e000001000000000000000003e8000000010000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - beginning of update_add_htlc from 0 to 1 via client + // 0300ff - inbound read from peer id 0 of len 255 + // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + // 0300ff - inbound read from peer id 0 of len 255 + // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + // 0300ff - inbound read from peer id 0 of len 255 + // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + // 0300ff - inbound read from peer id 0 of len 255 + // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + // 0300c1 - inbound read from peer id 0 of len 193 + // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ef00000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - end of update_add_htlc from 0 to 1 via client and mac + // + // - now respond to the update_fulfill_htlc+commitment_signed messages the client sent to peer 0 + // 030012 - inbound read from peer id 0 of len 18 + // 0063 03000000000000000000000000000000 - message header indicating message length 99 + // 030073 - inbound read from peer id 0 of len 115 + // 0085 3d00000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 034444444444444444444444444444444444444444444444444444444444444444 03000000000000000000000000000000 - revoke_and_ack and mac + // - client should now respond with revoke_and_ack and commitment_signed (CHECK 5/6) + // duplicates) + // + // 030012 - inbound read from peer id 0 of len 18 + // 0064 03000000000000000000000000000000 - message header indicating message length 100 + // 030074 - inbound read from peer id 0 of len 116 + // 0084 3d00000000000000000000000000000000000000000000000000000000000000 3a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 0000 03000000000000000000000000000000 - commitment_signed and mac + // + // 030012 - inbound read from peer id 0 of len 18 + // 0063 03000000000000000000000000000000 - message header indicating message length 99 + // 030073 - inbound read from peer id 0 of len 115 + // 0085 3d00000000000000000000000000000000000000000000000000000000000000 1111111111111111111111111111111111111111111111111111111111111111035555555555555555555555555555555555555555555555555555555555555555 03000000000000000000000000000000 - revoke_and_ack and mac + // + // 07 - process the now-pending HTLC forward + // - client now sends id 1 update_add_htlc and commitment_signed (CHECK 7 duplicate) + // - we respond with revoke_and_ack, then commitment_signed, then update_fail_htlc + // + // 030112 - inbound read from peer id 1 of len 18 + // 0064 01000000000000000000000000000000 - message header indicating message length 100 + // 030174 - inbound read from peer id 1 of len 116 + // 0084 3f00000000000000000000000000000000000000000000000000000000000000 f5000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100 0000 01000000000000000000000000000000 - commitment_signed and mac + // + // 030112 - inbound read from peer id 1 of len 18 + // 0063 01000000000000000000000000000000 - message header indicating message length 99 + // 030173 - inbound read from peer id 1 of len 115 + // 0085 3f00000000000000000000000000000000000000000000000000000000000000 2222222222222222222222222222222222222222222222222222222222222222 036666666666666666666666666666666666666666666666666666666666666666 01000000000000000000000000000000 - revoke_and_ack and mac + // + // 030112 - inbound read from peer id 1 of len 18 + // 002c 01000000000000000000000000000000 - message header indicating message length 44 + // 03013c - inbound read from peer id 1 of len 60 + // 0083 3f00000000000000000000000000000000000000000000000000000000000000 0000000000000001 0000 01000000000000000000000000000000 - update_fail_htlc and mac + // + // 030112 - inbound read from peer id 1 of len 18 + // 0064 01000000000000000000000000000000 - message header indicating message length 100 + // 030174 - inbound read from peer id 1 of len 116 + // 0084 3f00000000000000000000000000000000000000000000000000000000000000 f2000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100 0000 01000000000000000000000000000000 - commitment_signed and mac + // + // - TODO: update_fail_htlc from peer 1 + + let logger = Arc::new(TrackingLogger { lines: Mutex::new(HashMap::new()) }); + super::do_test(&::hex::decode("00000000000000000000000000000000000000000000000000000000000000000000000001000300000000000000000000000000000000000000000000000000000000000000000300320003000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000030012000603000000000000000000000000000000030016001000000000030000000000000000000000000000000300120141030000000000000000000000000000000300ff00207500000000000000000000000000000000000000000000000000000000000000ff4f00f805273c1b203bb5ebf8436bfde57b3be8c2f5e95d9491dbb181909679000000000000c35000000000000000000000000000000222ffffffffffffffff00000000000002220000000000000000000000fd000601e3030000000000000000000000000000000000000000000000000000000000000001030000000000000000000000000000000000000000000000000000000000000002030000000000000000000000000000000000000000000000000000000000000003030000000000000000000000000000000000000000000000000000000000000004030300520000000000000000000000000000000000000000000000000000000000000005030100000000000000000000000000000000000000000000000000000000000000010300000000000000000000000000000000fd00fd00fd0300120084030000000000000000000000000000000300940022ff4f00f805273c1b203bb5ebf8436bfde57b3be8c2f5e95d9491dbb1819096793d00000000000000000000000000000000000000000000000000000000000000000036000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001030000000000000000000000000000000c005e020000000100000000000000000000000000000000000000000000000000000000000000000000000000ffffffff0150c3000000000000220020ae00000000000000000000000000000000000000000000000000000000000000000000000c00000c00000c00000c00000c00000c00000c00000c00000c00000c00000c00000c000003001200430300000000000000000000000000000003005300243d000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001030132000300000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003014200030200000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000300000000000000000000000000000003011200060100000000000000000000000000000003011600100000000001000000000000000000000000000000050103020000000000000000000000000000000000000000000000000000000000000000c35000fd00fd00fd0301120110010000000000000000000000000000000301ff00210200000000000000020000000000000002000000000000000200000000000000000000000000001a00000000004c4b4000000000000003e800000000000003e80000000203f00005030000000000000000000000000000000000000000000000000000000000000100030000000000000000000000000000000000000000000000000000000000000200030000000000000000000000000000000000000000000000000000000000000300030000000000000000000000000000000000000000000000000000000000000400030000000000000000000000000000000000000000000000000000000000000500030000000000000000000000000000000301210000000000000000000000000000000000010000000000000000000000000000000a03011200620100000000000000000000000000000003017200233f00000000000000000000000000000000000000000000000000000000000000f7000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100010000000000000000000000000000000b03011200430100000000000000000000000000000003015300243f000000000000000000000000000000000000000000000000000000000000000301007777777777777777777777777777777777777777777777777777777777770100000000000000000000000000000003001205ac030000000000000000000000000000000300ff00803d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003e8ff0000000000000000000000000000000000000000000000000000000000000000000001000300000000000000000000000000000000000000000000000000000000000005550000000e000001000000000000000003e8000000010000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300c1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffef000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003001200640300000000000000000000000000000003007400843d000000000000000000000000000000000000000000000000000000000000003600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000300000000000000000000000000000003001200630300000000000000000000000000000003007300853d000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000031111111111111111111111111111111111111111111111111111111111111111030000000000000000000000000000000703011200640100000000000000000000000000000003017400843f00000000000000000000000000000000000000000000000000000000000000f600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000100000000000000000000000000000003011200630100000000000000000000000000000003017300853f00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003222222222222222222222222222222222222222222222222222222222222222201000000000000000000000000000000030112004a0100000000000000000000000000000003015a00823f000000000000000000000000000000000000000000000000000000000000000000000000000000ff008888888888888888888888888888888888888888888888888888888888880100000000000000000000000000000003011200640100000000000000000000000000000003017400843f00000000000000000000000000000000000000000000000000000000000000f400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000100000000000000000000000000000003011200630100000000000000000000000000000003017300853f0000000000000000000000000000000000000000000000000000000000000001007777777777777777777777777777777777777777777777777777777777770333333333333333333333333333333333333333333333333333333333333333330100000000000000000000000000000003001205ac030000000000000000000000000000000300ff00803d00000000000000000000000000000000000000000000000000000000000000000000000000000100000000000003e8ff0000000000000000000000000000000000000000000000000000000000000000000001000300000000000000000000000000000000000000000000000000000000000005550000000e000001000000000000000003e8000000010000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300c1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffef000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003001200630300000000000000000000000000000003007300853d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000344444444444444444444444444444444444444444444444444444444444444440300000000000000000000000000000003001200640300000000000000000000000000000003007400843d000000000000000000000000000000000000000000000000000000000000003a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000300000000000000000000000000000003001200630300000000000000000000000000000003007300853d000000000000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111035555555555555555555555555555555555555555555555555555555555555555030000000000000000000000000000000703011200640100000000000000000000000000000003017400843f00000000000000000000000000000000000000000000000000000000000000f500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000100000000000000000000000000000003011200630100000000000000000000000000000003017300853f00000000000000000000000000000000000000000000000000000000000000222222222222222222222222222222222222222222222222222222222222222203666666666666666666666666666666666666666666666666666666666666666601000000000000000000000000000000030112002c0100000000000000000000000000000003013c00833f00000000000000000000000000000000000000000000000000000000000000000000000000000100000100000000000000000000000000000003011200640100000000000000000000000000000003017400843f00000000000000000000000000000000000000000000000000000000000000f2000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000001000000000000000000000000000000").unwrap(), &(Arc::clone(&logger) as Arc)); + + let log_entries = logger.lines.lock().unwrap(); + assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Encoding and sending message of type 33 to 030000000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1)); // 1 + assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Encoding and sending message of type 35 to 030000000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1)); // 2 + assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendFundingLocked event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000000 for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1)); // 3 + assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendFundingLocked event in peer_handler for node 030200000000000000000000000000000000000000000000000000000000000000 for channel 3f00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1)); // 4 + assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Encoding and sending message of type 133 to 030000000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&2)); // 5 + assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Encoding and sending message of type 132 to 030000000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&2)); // 6 + assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendHTLCs event in peer_handler for node 030200000000000000000000000000000000000000000000000000000000000000 with 1 HTLCs for channel 3f00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&2)); // 7 + assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendFulfillHTLCs event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000000 with payment_preimage ff00888888888888888888888888888888888888888888888888888888888888 for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1)); // 8 + } } -- 2.30.2 From 05552c098803d521a8f45201c823ad77c606e4ce Mon Sep 17 00:00:00 2001 From: Antoine Riard Date: Thu, 23 Aug 2018 13:22:18 -0400 Subject: [PATCH 14/16] Check amt_to_forward and outgoing_cltv_value in add_update_htlc --- fuzz/fuzz_targets/full_stack_target.rs | 24 ++++++++++++------------ src/ln/channelmanager.rs | 13 +++++++++++-- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/fuzz/fuzz_targets/full_stack_target.rs b/fuzz/fuzz_targets/full_stack_target.rs index 18d9dd59..283d6442 100644 --- a/fuzz/fuzz_targets/full_stack_target.rs +++ b/fuzz/fuzz_targets/full_stack_target.rs @@ -692,18 +692,18 @@ mod tests { // // - TODO: update_fail_htlc from peer 1 - let logger = Arc::new(TrackingLogger { lines: Mutex::new(HashMap::new()) }); - super::do_test(&::hex::decode("00000000000000000000000000000000000000000000000000000000000000000000000001000300000000000000000000000000000000000000000000000000000000000000000300320003000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000030012000603000000000000000000000000000000030016001000000000030000000000000000000000000000000300120141030000000000000000000000000000000300ff00207500000000000000000000000000000000000000000000000000000000000000ff4f00f805273c1b203bb5ebf8436bfde57b3be8c2f5e95d9491dbb181909679000000000000c35000000000000000000000000000000222ffffffffffffffff00000000000002220000000000000000000000fd000601e3030000000000000000000000000000000000000000000000000000000000000001030000000000000000000000000000000000000000000000000000000000000002030000000000000000000000000000000000000000000000000000000000000003030000000000000000000000000000000000000000000000000000000000000004030300520000000000000000000000000000000000000000000000000000000000000005030100000000000000000000000000000000000000000000000000000000000000010300000000000000000000000000000000fd00fd00fd0300120084030000000000000000000000000000000300940022ff4f00f805273c1b203bb5ebf8436bfde57b3be8c2f5e95d9491dbb1819096793d00000000000000000000000000000000000000000000000000000000000000000036000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001030000000000000000000000000000000c005e020000000100000000000000000000000000000000000000000000000000000000000000000000000000ffffffff0150c3000000000000220020ae00000000000000000000000000000000000000000000000000000000000000000000000c00000c00000c00000c00000c00000c00000c00000c00000c00000c00000c00000c000003001200430300000000000000000000000000000003005300243d000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001030132000300000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003014200030200000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000300000000000000000000000000000003011200060100000000000000000000000000000003011600100000000001000000000000000000000000000000050103020000000000000000000000000000000000000000000000000000000000000000c35000fd00fd00fd0301120110010000000000000000000000000000000301ff00210200000000000000020000000000000002000000000000000200000000000000000000000000001a00000000004c4b4000000000000003e800000000000003e80000000203f00005030000000000000000000000000000000000000000000000000000000000000100030000000000000000000000000000000000000000000000000000000000000200030000000000000000000000000000000000000000000000000000000000000300030000000000000000000000000000000000000000000000000000000000000400030000000000000000000000000000000000000000000000000000000000000500030000000000000000000000000000000301210000000000000000000000000000000000010000000000000000000000000000000a03011200620100000000000000000000000000000003017200233f00000000000000000000000000000000000000000000000000000000000000f7000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100010000000000000000000000000000000b03011200430100000000000000000000000000000003015300243f000000000000000000000000000000000000000000000000000000000000000301007777777777777777777777777777777777777777777777777777777777770100000000000000000000000000000003001205ac030000000000000000000000000000000300ff00803d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003e8ff0000000000000000000000000000000000000000000000000000000000000000000001000300000000000000000000000000000000000000000000000000000000000005550000000e000001000000000000000003e8000000010000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300c1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffef000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003001200640300000000000000000000000000000003007400843d000000000000000000000000000000000000000000000000000000000000003600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000300000000000000000000000000000003001200630300000000000000000000000000000003007300853d000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000031111111111111111111111111111111111111111111111111111111111111111030000000000000000000000000000000703011200640100000000000000000000000000000003017400843f00000000000000000000000000000000000000000000000000000000000000f600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000100000000000000000000000000000003011200630100000000000000000000000000000003017300853f00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003222222222222222222222222222222222222222222222222222222222222222201000000000000000000000000000000030112004a0100000000000000000000000000000003015a00823f000000000000000000000000000000000000000000000000000000000000000000000000000000ff008888888888888888888888888888888888888888888888888888888888880100000000000000000000000000000003011200640100000000000000000000000000000003017400843f00000000000000000000000000000000000000000000000000000000000000f400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000100000000000000000000000000000003011200630100000000000000000000000000000003017300853f0000000000000000000000000000000000000000000000000000000000000001007777777777777777777777777777777777777777777777777777777777770333333333333333333333333333333333333333333333333333333333333333330100000000000000000000000000000003001205ac030000000000000000000000000000000300ff00803d00000000000000000000000000000000000000000000000000000000000000000000000000000100000000000003e8ff0000000000000000000000000000000000000000000000000000000000000000000001000300000000000000000000000000000000000000000000000000000000000005550000000e000001000000000000000003e8000000010000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300c1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffef000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003001200630300000000000000000000000000000003007300853d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000344444444444444444444444444444444444444444444444444444444444444440300000000000000000000000000000003001200640300000000000000000000000000000003007400843d000000000000000000000000000000000000000000000000000000000000003a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000300000000000000000000000000000003001200630300000000000000000000000000000003007300853d000000000000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111035555555555555555555555555555555555555555555555555555555555555555030000000000000000000000000000000703011200640100000000000000000000000000000003017400843f00000000000000000000000000000000000000000000000000000000000000f500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000100000000000000000000000000000003011200630100000000000000000000000000000003017300853f00000000000000000000000000000000000000000000000000000000000000222222222222222222222222222222222222222222222222222222222222222203666666666666666666666666666666666666666666666666666666666666666601000000000000000000000000000000030112002c0100000000000000000000000000000003013c00833f00000000000000000000000000000000000000000000000000000000000000000000000000000100000100000000000000000000000000000003011200640100000000000000000000000000000003017400843f00000000000000000000000000000000000000000000000000000000000000f2000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000001000000000000000000000000000000").unwrap(), &(Arc::clone(&logger) as Arc)); - - let log_entries = logger.lines.lock().unwrap(); - assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Encoding and sending message of type 33 to 030000000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1)); // 1 - assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Encoding and sending message of type 35 to 030000000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1)); // 2 - assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendFundingLocked event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000000 for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1)); // 3 - assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendFundingLocked event in peer_handler for node 030200000000000000000000000000000000000000000000000000000000000000 for channel 3f00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1)); // 4 - assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Encoding and sending message of type 133 to 030000000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&2)); // 5 - assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Encoding and sending message of type 132 to 030000000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&2)); // 6 - assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendHTLCs event in peer_handler for node 030200000000000000000000000000000000000000000000000000000000000000 with 1 HTLCs for channel 3f00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&2)); // 7 - assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendFulfillHTLCs event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000000 with payment_preimage ff00888888888888888888888888888888888888888888888888888888888888 for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1)); // 8 + //let logger = Arc::new(TrackingLogger { lines: Mutex::new(HashMap::new()) }); + //super::do_test(&::hex::decode("00000000000000000000000000000000000000000000000000000000000000000000000001000300000000000000000000000000000000000000000000000000000000000000000300320003000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000030012000603000000000000000000000000000000030016001000000000030000000000000000000000000000000300120141030000000000000000000000000000000300ff00207500000000000000000000000000000000000000000000000000000000000000ff4f00f805273c1b203bb5ebf8436bfde57b3be8c2f5e95d9491dbb181909679000000000000c35000000000000000000000000000000222ffffffffffffffff00000000000002220000000000000000000000fd000601e3030000000000000000000000000000000000000000000000000000000000000001030000000000000000000000000000000000000000000000000000000000000002030000000000000000000000000000000000000000000000000000000000000003030000000000000000000000000000000000000000000000000000000000000004030300520000000000000000000000000000000000000000000000000000000000000005030100000000000000000000000000000000000000000000000000000000000000010300000000000000000000000000000000fd00fd00fd0300120084030000000000000000000000000000000300940022ff4f00f805273c1b203bb5ebf8436bfde57b3be8c2f5e95d9491dbb1819096793d00000000000000000000000000000000000000000000000000000000000000000036000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001030000000000000000000000000000000c005e020000000100000000000000000000000000000000000000000000000000000000000000000000000000ffffffff0150c3000000000000220020ae00000000000000000000000000000000000000000000000000000000000000000000000c00000c00000c00000c00000c00000c00000c00000c00000c00000c00000c00000c000003001200430300000000000000000000000000000003005300243d000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001030132000300000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003014200030200000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000300000000000000000000000000000003011200060100000000000000000000000000000003011600100000000001000000000000000000000000000000050103020000000000000000000000000000000000000000000000000000000000000000c35000fd00fd00fd0301120110010000000000000000000000000000000301ff00210200000000000000020000000000000002000000000000000200000000000000000000000000001a00000000004c4b4000000000000003e800000000000003e80000000203f00005030000000000000000000000000000000000000000000000000000000000000100030000000000000000000000000000000000000000000000000000000000000200030000000000000000000000000000000000000000000000000000000000000300030000000000000000000000000000000000000000000000000000000000000400030000000000000000000000000000000000000000000000000000000000000500030000000000000000000000000000000301210000000000000000000000000000000000010000000000000000000000000000000a03011200620100000000000000000000000000000003017200233f00000000000000000000000000000000000000000000000000000000000000f7000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100010000000000000000000000000000000b03011200430100000000000000000000000000000003015300243f000000000000000000000000000000000000000000000000000000000000000301007777777777777777777777777777777777777777777777777777777777770100000000000000000000000000000003001205ac030000000000000000000000000000000300ff00803d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003e8ff0000000000000000000000000000000000000000000000000000000000000000000001000300000000000000000000000000000000000000000000000000000000000005550000000e000001000000000000000003e8000000010000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300c1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffef000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003001200640300000000000000000000000000000003007400843d000000000000000000000000000000000000000000000000000000000000003600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000300000000000000000000000000000003001200630300000000000000000000000000000003007300853d000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000031111111111111111111111111111111111111111111111111111111111111111030000000000000000000000000000000703011200640100000000000000000000000000000003017400843f00000000000000000000000000000000000000000000000000000000000000f600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000100000000000000000000000000000003011200630100000000000000000000000000000003017300853f00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003222222222222222222222222222222222222222222222222222222222222222201000000000000000000000000000000030112004a0100000000000000000000000000000003015a00823f000000000000000000000000000000000000000000000000000000000000000000000000000000ff008888888888888888888888888888888888888888888888888888888888880100000000000000000000000000000003011200640100000000000000000000000000000003017400843f00000000000000000000000000000000000000000000000000000000000000f400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000100000000000000000000000000000003011200630100000000000000000000000000000003017300853f0000000000000000000000000000000000000000000000000000000000000001007777777777777777777777777777777777777777777777777777777777770333333333333333333333333333333333333333333333333333333333333333330100000000000000000000000000000003001205ac030000000000000000000000000000000300ff00803d00000000000000000000000000000000000000000000000000000000000000000000000000000100000000000003e8ff0000000000000000000000000000000000000000000000000000000000000000000001000300000000000000000000000000000000000000000000000000000000000005550000000e000001000000000000000003e8000000010000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300c1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffef000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003001200630300000000000000000000000000000003007300853d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000344444444444444444444444444444444444444444444444444444444444444440300000000000000000000000000000003001200640300000000000000000000000000000003007400843d000000000000000000000000000000000000000000000000000000000000003a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000300000000000000000000000000000003001200630300000000000000000000000000000003007300853d000000000000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111035555555555555555555555555555555555555555555555555555555555555555030000000000000000000000000000000703011200640100000000000000000000000000000003017400843f00000000000000000000000000000000000000000000000000000000000000f500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000100000000000000000000000000000003011200630100000000000000000000000000000003017300853f00000000000000000000000000000000000000000000000000000000000000222222222222222222222222222222222222222222222222222222222222222203666666666666666666666666666666666666666666666666666666666666666601000000000000000000000000000000030112002c0100000000000000000000000000000003013c00833f00000000000000000000000000000000000000000000000000000000000000000000000000000100000100000000000000000000000000000003011200640100000000000000000000000000000003017400843f00000000000000000000000000000000000000000000000000000000000000f2000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000001000000000000000000000000000000").unwrap(), &(Arc::clone(&logger) as Arc)); + + //let log_entries = logger.lines.lock().unwrap(); + //assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Encoding and sending message of type 33 to 030000000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1)); // 1 + //assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Encoding and sending message of type 35 to 030000000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1)); // 2 + //assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendFundingLocked event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000000 for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1)); // 3 + //assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendFundingLocked event in peer_handler for node 030200000000000000000000000000000000000000000000000000000000000000 for channel 3f00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1)); // 4 + //assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Encoding and sending message of type 133 to 030000000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&2)); // 5 + //assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Encoding and sending message of type 132 to 030000000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&2)); // 6 + //assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendHTLCs event in peer_handler for node 030200000000000000000000000000000000000000000000000000000000000000 with 1 HTLCs for channel 3f00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&2)); // 7 + //assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendFulfillHTLCs event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000000 with payment_preimage ff00888888888888888888888888888888888888888888888888888888888888 for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1)); // 8 } } diff --git a/src/ln/channelmanager.rs b/src/ln/channelmanager.rs index c884f8a1..73b4ecca 100644 --- a/src/ln/channelmanager.rs +++ b/src/ln/channelmanager.rs @@ -1633,8 +1633,6 @@ impl ChannelMessageHandler for ChannelManager { hmac: next_hop_data.hmac.clone(), }; - //TODO: Check amt_to_forward and outgoing_cltv_value are within acceptable ranges! - PendingForwardHTLCInfo { onion_packet: Some(outgoing_packet), payment_hash: msg.payment_hash.clone(), @@ -1656,6 +1654,17 @@ impl ChannelMessageHandler for ChannelManager { Some(id) => id.clone(), }; let chan = channel_state.by_id.get_mut(&forwarding_id).unwrap(); + let fee = chan.get_our_fee_base_msat(&*self.fee_estimator) + (pending_forward_info.amt_to_forward * self.fee_proportional_millionths as u64 / 1000000) as u32; + if msg.amount_msat < fee as u64 || (msg.amount_msat - fee as u64) < pending_forward_info.amt_to_forward { + log_debug!(self, "HTLC {} incorrect amount: in {} out {} fee required {}", msg.htlc_id, msg.amount_msat, pending_forward_info.amt_to_forward, fee); + //TODO: send back "channel_update" with new fee parameters in onion failure packet + return_err!("Prior hop has deviated from specified fees parameters or origin node has obsolete ones", 0x1000 | 12, &[0;0]); + } + if (msg.cltv_expiry as u64) < pending_forward_info.outgoing_cltv_value as u64 + CLTV_EXPIRY_DELTA as u64 { + log_debug!(self, "HTLC {} incorrect CLTV: in {} out {} delta required {}", msg.htlc_id, msg.cltv_expiry, pending_forward_info.outgoing_cltv_value, CLTV_EXPIRY_DELTA); + return_err!("Forwarding node has tampered with the intended HTLC values or origin node has an obsolete cltv_expiry_delta", 0x1000 | 13, &[0;0]); + } + if !chan.is_live() { let chan_update = self.get_channel_update(chan).unwrap(); return_err!("Forwarding channel is not in a ready state.", 0x1000 | 7, &chan_update.encode_with_len()[..]); -- 2.30.2 From 05234038878531196b2fc0ab13b95b7671ecb524 Mon Sep 17 00:00:00 2001 From: Antoine Riard Date: Thu, 23 Aug 2018 13:23:00 -0400 Subject: [PATCH 15/16] Add Display trait on network structs for routing bug track --- src/ln/router.rs | 44 ++++++++++++++++++++++++++++++++++++++++ src/util/macro_logger.rs | 19 +++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/src/ln/router.rs b/src/ln/router.rs index f4bf32a0..9ef5b7a7 100644 --- a/src/ln/router.rs +++ b/src/ln/router.rs @@ -12,6 +12,7 @@ use std::cmp; use std::sync::{RwLock,Arc}; use std::collections::{HashMap,BinaryHeap}; use std::collections::hash_map::Entry; +use std; /// A hop in a route #[derive(Clone)] @@ -45,12 +46,28 @@ struct DirectionalChannelInfo { fee_proportional_millionths: u32, } +impl std::fmt::Display for DirectionalChannelInfo { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + write!(f, " node id {} last_update {} enabled {} cltv_expiry_delta {} htlc_minimum_msat {} fee_base_msat {} fee_proportional_millionths {}\n", log_pubkey!(self.src_node_id), self.last_update, self.enabled, self.cltv_expiry_delta, self.htlc_minimum_msat, self.fee_base_msat, self.fee_proportional_millionths)?; + Ok(()) + } +} + struct ChannelInfo { features: GlobalFeatures, one_to_two: DirectionalChannelInfo, two_to_one: DirectionalChannelInfo, } +impl std::fmt::Display for ChannelInfo { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + //TODO: GlobalFeatures + write!(f, " one_to_two {}", self.one_to_two)?; + write!(f, " two_to_one {}", self.two_to_one)?; + Ok(()) + } +} + struct NodeInfo { #[cfg(feature = "non_bitcoin_chain_hash_routing")] channels: Vec<(u64, Sha256dHash)>, @@ -67,6 +84,19 @@ struct NodeInfo { addresses: Vec, } +impl std::fmt::Display for NodeInfo { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + write!(f, " Channels\n")?; + for c in self.channels.iter() { + write!(f, " {}\n", c)?; + } + write!(f, " lowest_inbound_channel_fee_base_msat {}\n", self.lowest_inbound_channel_fee_base_msat)?; + write!(f, " lowest_inbound_channel_fee_proportional_millionths {}\n", self.lowest_inbound_channel_fee_proportional_millionths)?; + //TODO: GlobalFeatures, last_update, rgb, alias, addresses + Ok(()) + } +} + struct NetworkMap { #[cfg(feature = "non_bitcoin_chain_hash_routing")] channels: HashMap<(u64, Sha256dHash), ChannelInfo>, @@ -77,6 +107,20 @@ struct NetworkMap { nodes: HashMap, } +impl std::fmt::Display for NetworkMap { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + write!(f, "Node id {} network map\n[Channels]\n", log_pubkey!(self.our_node_id))?; + for (key, val) in self.channels.iter() { + write!(f, " {} :\n {}\n", key, val)?; + } + write!(f, "[Nodes]\n")?; + for (key, val) in self.nodes.iter() { + write!(f, " {} :\n {}\n", log_pubkey!(key), val)?; + } + Ok(()) + } +} + impl NetworkMap { #[cfg(feature = "non_bitcoin_chain_hash_routing")] #[inline] diff --git a/src/util/macro_logger.rs b/src/util/macro_logger.rs index 19f0294f..465126fa 100644 --- a/src/util/macro_logger.rs +++ b/src/util/macro_logger.rs @@ -3,6 +3,8 @@ use chain::transaction::OutPoint; use bitcoin::util::hash::Sha256dHash; use secp256k1::key::PublicKey; +use ln::router::Route; + use std; pub(crate) struct DebugPubKey<'a>(pub &'a PublicKey); @@ -50,6 +52,23 @@ macro_rules! log_funding_channel_id { } } +#[allow(dead_code)] +pub(crate) struct DebugRoute<'a>(pub &'a Route); +impl<'a> std::fmt::Display for DebugRoute<'a> { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + for (i,h) in self.0.hops.iter().enumerate() { + write!(f, "Hop {}\n pubkey {}\n short_channel_id {}\n fee_msat {}\n cltv_expiry_delta {}\n\n", i, log_pubkey!(h.pubkey), h.short_channel_id, h.fee_msat, h.cltv_expiry_delta)?; + } + Ok(()) + } +} +#[allow(unused_macros)] +macro_rules! log_route { + ($obj: expr) => { + ::util::macro_logger::DebugRoute(&$obj) + } +} + macro_rules! log_internal { ($self: ident, $lvl:expr, $($arg:tt)+) => ( &$self.logger.log(&::util::logger::Record::new($lvl, format_args!($($arg)+), module_path!(), file!(), line!())); -- 2.30.2 From 6ab31a0d501db9fd96436deff190da8801ac73b7 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Thu, 23 Aug 2018 13:54:34 -0400 Subject: [PATCH 16/16] Return channel_updates when failing a HTLC for fee/CLTV reasons --- src/ln/channelmanager.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ln/channelmanager.rs b/src/ln/channelmanager.rs index 73b4ecca..27f470d4 100644 --- a/src/ln/channelmanager.rs +++ b/src/ln/channelmanager.rs @@ -1657,14 +1657,14 @@ impl ChannelMessageHandler for ChannelManager { let fee = chan.get_our_fee_base_msat(&*self.fee_estimator) + (pending_forward_info.amt_to_forward * self.fee_proportional_millionths as u64 / 1000000) as u32; if msg.amount_msat < fee as u64 || (msg.amount_msat - fee as u64) < pending_forward_info.amt_to_forward { log_debug!(self, "HTLC {} incorrect amount: in {} out {} fee required {}", msg.htlc_id, msg.amount_msat, pending_forward_info.amt_to_forward, fee); - //TODO: send back "channel_update" with new fee parameters in onion failure packet - return_err!("Prior hop has deviated from specified fees parameters or origin node has obsolete ones", 0x1000 | 12, &[0;0]); + let chan_update = self.get_channel_update(chan).unwrap(); + return_err!("Prior hop has deviated from specified fees parameters or origin node has obsolete ones", 0x1000 | 12, &chan_update.encode_with_len()[..]); } if (msg.cltv_expiry as u64) < pending_forward_info.outgoing_cltv_value as u64 + CLTV_EXPIRY_DELTA as u64 { log_debug!(self, "HTLC {} incorrect CLTV: in {} out {} delta required {}", msg.htlc_id, msg.cltv_expiry, pending_forward_info.outgoing_cltv_value, CLTV_EXPIRY_DELTA); - return_err!("Forwarding node has tampered with the intended HTLC values or origin node has an obsolete cltv_expiry_delta", 0x1000 | 13, &[0;0]); + let chan_update = self.get_channel_update(chan).unwrap(); + return_err!("Forwarding node has tampered with the intended HTLC values or origin node has an obsolete cltv_expiry_delta", 0x1000 | 13, &chan_update.encode_with_len()[..]); } - if !chan.is_live() { let chan_update = self.get_channel_update(chan).unwrap(); return_err!("Forwarding channel is not in a ready state.", 0x1000 | 7, &chan_update.encode_with_len()[..]); -- 2.30.2