diff --git a/fuzz/fuzz_targets/full_stack_target.rs b/fuzz/fuzz_targets/full_stack_target.rs index e5c12539ece..ced754acb51 100644 --- a/fuzz/fuzz_targets/full_stack_target.rs +++ b/fuzz/fuzz_targets/full_stack_target.rs @@ -232,12 +232,12 @@ pub fn do_test(data: &[u8], logger: &Arc) { Err(_) => return, }; - let watch = Arc::new(ChainWatchInterfaceUtil::new(Arc::clone(&logger))); + let watch = Arc::new(ChainWatchInterfaceUtil::new(Network::Bitcoin, Arc::clone(&logger))); let broadcast = Arc::new(TestBroadcaster{}); let monitor = channelmonitor::SimpleManyChannelMonitor::new(watch.clone(), broadcast.clone()); 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), Arc::clone(&logger))); + let router = Arc::new(Router::new(PublicKey::from_secret_key(&secp_ctx, &our_network_key), watch.clone(), Arc::clone(&logger))); let peers = RefCell::new([false; 256]); let mut loss_detector = MoneyLossDetector::new(&peers, channelmanager.clone(), monitor.clone(), PeerManager::new(MessageHandler { diff --git a/fuzz/fuzz_targets/router_target.rs b/fuzz/fuzz_targets/router_target.rs index 469db990ffc..f88ea9e6a29 100644 --- a/fuzz/fuzz_targets/router_target.rs +++ b/fuzz/fuzz_targets/router_target.rs @@ -2,6 +2,11 @@ extern crate bitcoin; extern crate lightning; extern crate secp256k1; +use bitcoin::util::hash::Sha256dHash; +use bitcoin::blockdata::script::{Script, Builder}; +use bitcoin::blockdata::opcodes; + +use lightning::chain::chaininterface::{ChainError,ChainWatchInterface, ChainListener}; use lightning::ln::channelmanager::ChannelDetails; use lightning::ln::msgs; use lightning::ln::msgs::{MsgDecodable, RoutingMessageHandler}; @@ -16,7 +21,8 @@ mod utils; use utils::test_logger; -use std::sync::Arc; +use std::sync::{Weak, Arc}; +use std::sync::atomic::{AtomicUsize, Ordering}; #[inline] pub fn slice_to_be16(v: &[u8]) -> u16 { @@ -44,6 +50,46 @@ pub fn slice_to_be64(v: &[u8]) -> u64 { ((v[7] as u64) << 8*0) } + +struct InputData { + data: Vec, + read_pos: AtomicUsize, +} +impl InputData { + fn get_slice(&self, len: usize) -> Option<&[u8]> { + let old_pos = self.read_pos.fetch_add(len, Ordering::AcqRel); + if self.data.len() < old_pos + len { + return None; + } + Some(&self.data[old_pos..old_pos + len]) + } +} + +struct DummyChainWatcher { + input: Arc, +} + +impl ChainWatchInterface for DummyChainWatcher { + fn install_watch_script(&self, _script_pub_key: &Script) { + } + + fn install_watch_outpoint(&self, _outpoint: (Sha256dHash, u32), _out_script: &Script) { + } + + fn watch_all_txn(&self) { + } + + fn register_listener(&self, _listener: Weak) { + } + + fn get_chain_utxo(&self, _genesis_hash: Sha256dHash, _unspent_tx_output_identifier: u64) -> Result<(Script, u64), ChainError> { + match self.input.get_slice(1) { + Some(slice) => Ok((Builder::new().push_opcode(opcodes::All::OP_PUSHBYTES_0).into_script().to_v0_p2wsh(), 0)), + None => Err(ChainError::UnknownTx), + } + } +} + #[inline] pub fn do_test(data: &[u8]) { reset_rng_state(); @@ -107,9 +153,16 @@ pub fn do_test(data: &[u8]) { } let logger: Arc = Arc::new(test_logger::TestLogger{}); + let input = Arc::new(InputData { + data: data.to_vec(), + read_pos: AtomicUsize::new(0), + }); + let chain_monitor = Arc::new(DummyChainWatcher { + input: input, + }); let our_pubkey = get_pubkey!(); - let router = Router::new(our_pubkey.clone(), Arc::clone(&logger)); + let router = Router::new(our_pubkey.clone(), chain_monitor, Arc::clone(&logger)); loop { match get_slice!(1)[0] { diff --git a/src/chain/chaininterface.rs b/src/chain/chaininterface.rs index bda99b2d83c..78a40099c35 100644 --- a/src/chain/chaininterface.rs +++ b/src/chain/chaininterface.rs @@ -1,11 +1,24 @@ use bitcoin::blockdata::block::{Block, BlockHeader}; use bitcoin::blockdata::transaction::Transaction; use bitcoin::blockdata::script::Script; +use bitcoin::blockdata::constants::genesis_block; use bitcoin::util::hash::Sha256dHash; +use bitcoin::network::constants::Network; +use bitcoin::network::serialize::BitcoinHash; use util::logger::Logger; use std::sync::{Mutex,Weak,MutexGuard,Arc}; use std::sync::atomic::{AtomicUsize, Ordering}; +/// Used to give chain error details upstream +pub enum ChainError { + /// Client doesn't support UTXO lookup (but the chain hash matches our genesis block hash) + NotSupported, + /// Chain isn't the one watched + NotWatched, + /// Tx doesn't exist or is unconfirmed + UnknownTx, +} + /// An interface to request notification of certain scripts as they appear the /// chain. /// Note that all of the functions implemented here *must* be reentrant-safe (obviously - they're @@ -24,6 +37,12 @@ pub trait ChainWatchInterface: Sync + Send { fn register_listener(&self, listener: Weak); //TODO: unregister + + /// Gets the script and value in satoshis for a given unspent transaction output given a + /// short_channel_id (aka unspent_tx_output_identier). For BTC/tBTC channels the top three + /// bytes are the block height, the next 3 the transaction index within the block, and the + /// final two the output within the transaction. + fn get_chain_utxo(&self, genesis_hash: Sha256dHash, unspent_tx_output_identifier: u64) -> Result<(Script, u64), ChainError>; } /// An interface to send a transaction to the Bitcoin network. @@ -69,6 +88,7 @@ pub trait FeeEstimator: Sync + Send { /// Utility to capture some common parts of ChainWatchInterface implementors. /// Keeping a local copy of this in a ChainWatchInterface implementor is likely useful. pub struct ChainWatchInterfaceUtil { + network: Network, watched: Mutex<(Vec