diff --git a/ARCH.md b/ARCH.md index c4f94280c48..5b9304cce00 100644 --- a/ARCH.md +++ b/ARCH.md @@ -6,7 +6,7 @@ need to use are `ChannelManager` and `ChannelMonitor`. `ChannelManager` holds mu channels, routes payments between them, and exposes a simple API to make and receive payments. Individual `ChannelMonitor`s monitor the on-chain state of a channel, punish counterparties if they misbehave, and force-close channels if they contain unresolved -HTLCs which are near expiration. The `ManyChannelMonitor` API provides a way for you to +HTLCs which are near expiration. The `chain::Watch` interface provides a way for you to receive `ChannelMonitorUpdate`s from `ChannelManager` and persist them to disk before the channel steps forward. @@ -37,26 +37,26 @@ At a high level, some of the common interfaces fit together as follows: ----------------- | KeysInterface | -------------- ----------------- | UserConfig | - -------------------- | -------------- - /------| MessageSendEvent | | | ---------------- - | -------------------- | | | FeeEstimator | - | (as MessageSendEventsProvider) | | ---------------- - | ^ | | / | ------------------------ - | \ | | / ---------> | BroadcasterInterface | - | \ | | / / | ------------------------ - | \ v v v / v ^ - | (as ------------------ ---------------------- - | ChannelMessageHandler)-> | ChannelManager | ----> | ManyChannelMonitor | - v / ------------------ ---------------------- ---------------- / ^ (as EventsProvider) ^ -| PeerManager |- | \ / / ---------------- | -------\---/---------- - | ----------------------- / \ / - | | ChainWatchInterface | - v - | ----------------------- --------- - | | | Event | -(as RoutingMessageHandler) v --------- - \ -------------------- - -----------------> | NetGraphMsgHandler | - -------------------- + -------------------- ^ -------------- + ------| MessageSendEvent | | ^ ---------------- + / -------------------- | | | FeeEstimator | <----------------------- + | (as MessageSendEventsProvider) | | ---------------- \ + | ^ | | ^ ------------------------ | + | \ | | / ---------> | BroadcasterInterface | | + | \ | | / / ------------------------ | + | \ | | / / ^ | + | (as ------------------ ---------------- | | + | ChannelMessageHandler)-> | ChannelManager | ----> | chain::Watch | | | + v / ------------------ ---------------- | | +--------------- / (as EventsProvider) ^ | | +| PeerManager |- \ | | | +--------------- \ | (is-a) | | + | ----------------- \ _---------------- / / + | | chain::Access | \ / | ChainMonitor |--------------- + | ----------------- \ / ---------------- + | ^ \ / | +(as RoutingMessageHandler) | v v + \ ---------------------- --------- ----------------- + -----------------> | NetGraphMsgHandler | | Event | | chain::Filter | + ---------------------- --------- ----------------- ``` diff --git a/fuzz/src/chanmon_consistency.rs b/fuzz/src/chanmon_consistency.rs index ca05e5db942..1650e2e25f7 100644 --- a/fuzz/src/chanmon_consistency.rs +++ b/fuzz/src/chanmon_consistency.rs @@ -28,12 +28,13 @@ use bitcoin::hashes::Hash as TraitImport; use bitcoin::hashes::sha256::Hash as Sha256; use bitcoin::hash_types::{BlockHash, WPubkeyHash}; -use lightning::chain::chaininterface; +use lightning::chain; +use lightning::chain::chainmonitor; +use lightning::chain::channelmonitor; +use lightning::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdateErr, MonitorEvent}; use lightning::chain::transaction::OutPoint; -use lightning::chain::chaininterface::{BroadcasterInterface,ConfirmationTarget,ChainListener,FeeEstimator,ChainWatchInterfaceUtil,ChainWatchInterface}; +use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator}; use lightning::chain::keysinterface::{KeysInterface, InMemoryChannelKeys}; -use lightning::ln::channelmonitor; -use lightning::ln::channelmonitor::{ChannelMonitor, ChannelMonitorUpdateErr, MonitorEvent}; use lightning::ln::channelmanager::{ChannelManager, PaymentHash, PaymentPreimage, PaymentSecret, ChannelManagerReadArgs}; use lightning::ln::features::{ChannelFeatures, InitFeatures, NodeFeatures}; use lightning::ln::msgs::{CommitmentUpdate, ChannelMessageHandler, ErrorAction, UpdateAddHTLC, Init}; @@ -81,9 +82,9 @@ impl Writer for VecWriter { } } -struct TestChannelMonitor { +struct TestChainMonitor { pub logger: Arc, - pub simple_monitor: Arc, Arc, Arc, Arc>>, + pub chain_monitor: Arc, Arc, Arc, Arc>>, pub update_ret: Mutex>, // If we reload a node with an old copy of ChannelMonitors, the ChannelManager deserialization // logic will automatically force-close our channels for us (as we don't have an up-to-date @@ -93,10 +94,10 @@ struct TestChannelMonitor { pub latest_monitors: Mutex)>>, pub should_update_manager: atomic::AtomicBool, } -impl TestChannelMonitor { - pub fn new(chain_monitor: Arc, broadcaster: Arc, logger: Arc, feeest: Arc) -> Self { +impl TestChainMonitor { + pub fn new(broadcaster: Arc, logger: Arc, feeest: Arc) -> Self { Self { - simple_monitor: Arc::new(channelmonitor::SimpleManyChannelMonitor::new(chain_monitor, broadcaster, logger.clone(), feeest)), + chain_monitor: Arc::new(chainmonitor::ChainMonitor::new(None, broadcaster, logger.clone(), feeest)), logger, update_ret: Mutex::new(Ok(())), latest_monitors: Mutex::new(HashMap::new()), @@ -104,21 +105,21 @@ impl TestChannelMonitor { } } } -impl channelmonitor::ManyChannelMonitor for TestChannelMonitor { +impl chain::Watch for TestChainMonitor { type Keys = EnforcingChannelKeys; - fn add_monitor(&self, funding_txo: OutPoint, monitor: channelmonitor::ChannelMonitor) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> { + fn watch_channel(&self, funding_txo: OutPoint, monitor: channelmonitor::ChannelMonitor) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> { let mut ser = VecWriter(Vec::new()); monitor.write_for_disk(&mut ser).unwrap(); if let Some(_) = self.latest_monitors.lock().unwrap().insert(funding_txo, (monitor.get_latest_update_id(), ser.0)) { - panic!("Already had monitor pre-add_monitor"); + panic!("Already had monitor pre-watch_channel"); } self.should_update_manager.store(true, atomic::Ordering::Relaxed); - assert!(self.simple_monitor.add_monitor(funding_txo, monitor).is_ok()); + assert!(self.chain_monitor.watch_channel(funding_txo, monitor).is_ok()); self.update_ret.lock().unwrap().clone() } - fn update_monitor(&self, funding_txo: OutPoint, update: channelmonitor::ChannelMonitorUpdate) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> { + fn update_channel(&self, funding_txo: OutPoint, update: channelmonitor::ChannelMonitorUpdate) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> { let mut map_lock = self.latest_monitors.lock().unwrap(); let mut map_entry = match map_lock.entry(funding_txo) { hash_map::Entry::Occupied(entry) => entry, @@ -134,8 +135,8 @@ impl channelmonitor::ManyChannelMonitor for TestChannelMonitor { self.update_ret.lock().unwrap().clone() } - fn get_and_clear_pending_monitor_events(&self) -> Vec { - return self.simple_monitor.get_and_clear_pending_monitor_events(); + fn release_pending_monitor_events(&self) -> Vec { + return self.chain_monitor.release_pending_monitor_events(); } } @@ -191,8 +192,7 @@ pub fn do_test(data: &[u8], out: Out) { macro_rules! make_node { ($node_id: expr) => { { let logger: Arc = Arc::new(test_logger::TestLogger::new($node_id.to_string(), out.clone())); - let watch = Arc::new(ChainWatchInterfaceUtil::new(Network::Bitcoin)); - let monitor = Arc::new(TestChannelMonitor::new(watch.clone(), broadcast.clone(), logger.clone(), fee_est.clone())); + let monitor = Arc::new(TestChainMonitor::new(broadcast.clone(), logger.clone(), fee_est.clone())); let keys_manager = Arc::new(KeyProvider { node_id: $node_id, rand_bytes_id: atomic::AtomicU8::new(0) }); let mut config = UserConfig::default(); @@ -207,8 +207,7 @@ pub fn do_test(data: &[u8], out: Out) { macro_rules! reload_node { ($ser: expr, $node_id: expr, $old_monitors: expr) => { { let logger: Arc = Arc::new(test_logger::TestLogger::new($node_id.to_string(), out.clone())); - let watch = Arc::new(ChainWatchInterfaceUtil::new(Network::Bitcoin)); - let monitor = Arc::new(TestChannelMonitor::new(watch.clone(), broadcast.clone(), logger.clone(), fee_est.clone())); + let chain_monitor = Arc::new(TestChainMonitor::new(broadcast.clone(), logger.clone(), fee_est.clone())); let keys_manager = Arc::new(KeyProvider { node_id: $node_id, rand_bytes_id: atomic::AtomicU8::new(0) }); let mut config = UserConfig::default(); @@ -220,7 +219,7 @@ pub fn do_test(data: &[u8], out: Out) { let mut old_monitors = $old_monitors.latest_monitors.lock().unwrap(); for (outpoint, (update_id, monitor_ser)) in old_monitors.drain() { monitors.insert(outpoint, <(BlockHash, ChannelMonitor)>::read(&mut Cursor::new(&monitor_ser)).expect("Failed to read monitor").1); - monitor.latest_monitors.lock().unwrap().insert(outpoint, (update_id, monitor_ser)); + chain_monitor.latest_monitors.lock().unwrap().insert(outpoint, (update_id, monitor_ser)); } let mut monitor_refs = HashMap::new(); for (outpoint, monitor) in monitors.iter_mut() { @@ -230,14 +229,14 @@ pub fn do_test(data: &[u8], out: Out) { let read_args = ChannelManagerReadArgs { keys_manager, fee_estimator: fee_est.clone(), - monitor: monitor.clone(), + chain_monitor: chain_monitor.clone(), tx_broadcaster: broadcast.clone(), logger, default_config: config, channel_monitors: monitor_refs, }; - (<(BlockHash, ChannelManager, Arc, Arc, Arc, Arc>)>::read(&mut Cursor::new(&$ser.0), read_args).expect("Failed to read manager").1, monitor) + (<(BlockHash, ChannelManager, Arc, Arc, Arc, Arc>)>::read(&mut Cursor::new(&$ser.0), read_args).expect("Failed to read manager").1, chain_monitor) } } } @@ -308,16 +307,11 @@ pub fn do_test(data: &[u8], out: Out) { macro_rules! confirm_txn { ($node: expr) => { { let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }; - let mut txn = Vec::with_capacity(channel_txn.len()); - let mut posn = Vec::with_capacity(channel_txn.len()); - for i in 0..channel_txn.len() { - txn.push(&channel_txn[i]); - posn.push(i + 1); - } - $node.block_connected(&header, 1, &txn, &posn); + let txdata: Vec<_> = channel_txn.iter().enumerate().map(|(i, tx)| (i + 1, tx)).collect(); + $node.block_connected(&header, &txdata, 1); for i in 2..100 { header = BlockHeader { version: 0x20000000, prev_blockhash: header.block_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }; - $node.block_connected(&header, i, &Vec::new(), &[0; 0]); + $node.block_connected(&header, &[], i); } } } } diff --git a/fuzz/src/chanmon_deser.rs b/fuzz/src/chanmon_deser.rs index 3f4ff5ad0a1..5a76340ff30 100644 --- a/fuzz/src/chanmon_deser.rs +++ b/fuzz/src/chanmon_deser.rs @@ -3,8 +3,8 @@ use bitcoin::hash_types::BlockHash; +use lightning::chain::channelmonitor; use lightning::util::enforcing_trait_impls::EnforcingChannelKeys; -use lightning::ln::channelmonitor; use lightning::util::ser::{Readable, Writer}; use utils::test_logger; diff --git a/fuzz/src/full_stack.rs b/fuzz/src/full_stack.rs index 42da4d90312..1ed17b9ea3f 100644 --- a/fuzz/src/full_stack.rs +++ b/fuzz/src/full_stack.rs @@ -25,10 +25,11 @@ use bitcoin::hashes::HashEngine as TraitImportEngine; use bitcoin::hashes::sha256::Hash as Sha256; use bitcoin::hash_types::{Txid, BlockHash, WPubkeyHash}; -use lightning::chain::chaininterface::{BroadcasterInterface,ConfirmationTarget,ChainListener,FeeEstimator,ChainWatchInterfaceUtil}; +use lightning::chain; +use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator}; +use lightning::chain::chainmonitor; use lightning::chain::transaction::OutPoint; use lightning::chain::keysinterface::{InMemoryChannelKeys, KeysInterface}; -use lightning::ln::channelmonitor; use lightning::ln::channelmanager::{ChannelManager, PaymentHash, PaymentPreimage, PaymentSecret}; use lightning::ln::peer_handler::{MessageHandler,PeerManager,SocketDescriptor}; use lightning::routing::router::get_route; @@ -144,14 +145,13 @@ impl<'a> std::hash::Hash for Peer<'a> { type ChannelMan = ChannelManager< EnforcingChannelKeys, - Arc, Arc, Arc, Arc>>, + Arc, Arc, Arc, Arc>>, Arc, Arc, Arc, Arc>; -type PeerMan<'a> = PeerManager, Arc, Arc, Arc>>, Arc>; +type PeerMan<'a> = PeerManager, Arc, Arc, Arc>>, Arc>; struct MoneyLossDetector<'a> { manager: Arc, - monitor: Arc, Arc, Arc, Arc>>, + monitor: Arc, Arc, Arc, Arc>>, handler: PeerMan<'a>, peers: &'a RefCell<[bool; 256]>, @@ -165,7 +165,7 @@ struct MoneyLossDetector<'a> { impl<'a> MoneyLossDetector<'a> { pub fn new(peers: &'a RefCell<[bool; 256]>, manager: Arc, - monitor: Arc, Arc, Arc, Arc>>, + monitor: Arc, Arc, Arc, Arc>>, handler: PeerMan<'a>) -> Self { MoneyLossDetector { manager, @@ -183,15 +183,13 @@ impl<'a> MoneyLossDetector<'a> { } fn connect_block(&mut self, all_txn: &[Transaction]) { - let mut txn = Vec::with_capacity(all_txn.len()); - let mut txn_idxs = Vec::with_capacity(all_txn.len()); + let mut txdata = Vec::with_capacity(all_txn.len()); for (idx, tx) in all_txn.iter().enumerate() { let txid = tx.txid(); match self.txids_confirmed.entry(txid) { hash_map::Entry::Vacant(e) => { e.insert(self.height); - txn.push(tx); - txn_idxs.push(idx + 1); + txdata.push((idx + 1, tx)); }, _ => {}, } @@ -200,8 +198,8 @@ impl<'a> MoneyLossDetector<'a> { let header = BlockHeader { version: 0x20000000, prev_blockhash: self.header_hashes[self.height], merkle_root: Default::default(), time: self.blocks_connected, bits: 42, nonce: 42 }; self.height += 1; self.blocks_connected += 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[..]); + self.manager.block_connected(&header, &txdata, self.height as u32); + (*self.monitor).block_connected(&header, &txdata, self.height as u32); if self.header_hashes.len() > self.height { self.header_hashes[self.height] = header.block_hash(); } else { @@ -214,7 +212,7 @@ impl<'a> MoneyLossDetector<'a> { fn disconnect_block(&mut self) { if self.height > 0 && (self.max_height < 6 || self.height >= self.max_height - 6) { 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.height as u32); + self.manager.block_disconnected(&header); self.monitor.block_disconnected(&header, self.height as u32); self.height -= 1; let removal_height = self.height; @@ -334,9 +332,8 @@ pub fn do_test(data: &[u8], logger: &Arc) { Err(_) => return, }; - let watch = Arc::new(ChainWatchInterfaceUtil::new(Network::Bitcoin)); let broadcast = Arc::new(TestBroadcaster{}); - let monitor = Arc::new(channelmonitor::SimpleManyChannelMonitor::new(watch.clone(), broadcast.clone(), Arc::clone(&logger), fee_est.clone())); + let monitor = Arc::new(chainmonitor::ChainMonitor::new(None, broadcast.clone(), Arc::clone(&logger), fee_est.clone())); let keys_manager = Arc::new(KeyProvider { node_secret: our_network_key.clone(), counter: AtomicU64::new(0) }); let mut config = UserConfig::default(); @@ -345,7 +342,7 @@ pub fn do_test(data: &[u8], logger: &Arc) { config.peer_channel_config_limits.min_dust_limit_satoshis = 0; let channelmanager = Arc::new(ChannelManager::new(Network::Bitcoin, fee_est.clone(), monitor.clone(), broadcast.clone(), Arc::clone(&logger), keys_manager.clone(), config, 0)); let our_id = PublicKey::from_secret_key(&Secp256k1::signing_only(), &keys_manager.get_node_secret()); - let net_graph_msg_handler = Arc::new(NetGraphMsgHandler::new(watch.clone(), Arc::clone(&logger))); + let net_graph_msg_handler = Arc::new(NetGraphMsgHandler::new(None, Arc::clone(&logger))); let peers = RefCell::new([false; 256]); let mut loss_detector = MoneyLossDetector::new(&peers, channelmanager.clone(), monitor.clone(), PeerManager::new(MessageHandler { @@ -903,6 +900,6 @@ mod tests { assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling UpdateHTLCs event in peer_handler for node 030200000000000000000000000000000000000000000000000000000000000000 with 1 adds, 0 fulfills, 0 fails for channel 3900000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&3)); // 7 assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling UpdateHTLCs event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000000 with 0 adds, 1 fulfills, 0 fails for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1)); // 8 assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling UpdateHTLCs event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000000 with 0 adds, 0 fulfills, 1 fails for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&2)); // 9 - assert_eq!(log_entries.get(&("lightning::ln::channelmonitor".to_string(), "Input spending counterparty commitment tx (00000000000000000000000000000000000000000000000000000000000000a1:0) in 0000000000000000000000000000000000000000000000000000000000000018 resolves outbound HTLC with payment hash ff00000000000000000000000000000000000000000000000000000000000000 with timeout".to_string())), Some(&1)); // 10 + assert_eq!(log_entries.get(&("lightning::chain::channelmonitor".to_string(), "Input spending counterparty commitment tx (00000000000000000000000000000000000000000000000000000000000000a1:0) in 0000000000000000000000000000000000000000000000000000000000000018 resolves outbound HTLC with payment hash ff00000000000000000000000000000000000000000000000000000000000000 with timeout".to_string())), Some(&1)); // 10 } } diff --git a/fuzz/src/router.rs b/fuzz/src/router.rs index ffc4608376b..5f2114b64e1 100644 --- a/fuzz/src/router.rs +++ b/fuzz/src/router.rs @@ -7,11 +7,11 @@ // You may not use this file except in accordance with one or both of these // licenses. -use bitcoin::blockdata::script::{Script, Builder}; -use bitcoin::blockdata::block::Block; -use bitcoin::hash_types::{Txid, BlockHash}; +use bitcoin::blockdata::script::Builder; +use bitcoin::blockdata::transaction::TxOut; +use bitcoin::hash_types::BlockHash; -use lightning::chain::chaininterface::{ChainError,ChainWatchInterface}; +use lightning::chain; use lightning::ln::channelmanager::ChannelDetails; use lightning::ln::features::InitFeatures; use lightning::ln::msgs; @@ -76,26 +76,16 @@ impl InputData { } } -struct DummyChainWatcher { +struct FuzzChainSource { input: Arc, } - -impl ChainWatchInterface for DummyChainWatcher { - fn install_watch_tx(&self, _txid: &Txid, _script_pub_key: &Script) { } - fn install_watch_outpoint(&self, _outpoint: (Txid, u32), _out_script: &Script) { } - fn watch_all_txn(&self) { } - fn filter_block(&self, _block: &Block) -> Vec { - Vec::new() - } - fn reentered(&self) -> usize { 0 } - - fn get_chain_utxo(&self, _genesis_hash: BlockHash, _unspent_tx_output_identifier: u64) -> Result<(Script, u64), ChainError> { +impl chain::Access for FuzzChainSource { + fn get_utxo(&self, _genesis_hash: &BlockHash, _short_channel_id: u64) -> Result { match self.input.get_slice(2) { - Some(&[0, _]) => Err(ChainError::NotSupported), - Some(&[1, _]) => Err(ChainError::NotWatched), - Some(&[2, _]) => Err(ChainError::UnknownTx), - Some(&[_, x]) => Ok((Builder::new().push_int(x as i64).into_script().to_v0_p2wsh(), 0)), - None => Err(ChainError::UnknownTx), + Some(&[0, _]) => Err(chain::AccessError::UnknownChain), + Some(&[1, _]) => Err(chain::AccessError::UnknownTx), + Some(&[_, x]) => Ok(TxOut { value: 0, script_pubkey: Builder::new().push_int(x as i64).into_script().to_v0_p2wsh() }), + None => Err(chain::AccessError::UnknownTx), _ => unreachable!(), } } @@ -160,12 +150,16 @@ pub fn do_test(data: &[u8], out: Out) { } let logger: Arc = Arc::new(test_logger::TestLogger::new("".to_owned(), out)); - let chain_monitor = Arc::new(DummyChainWatcher { - input: Arc::clone(&input), - }); + let chain_source = if get_slice!(1)[0] % 2 == 0 { + None + } else { + Some(Arc::new(FuzzChainSource { + input: Arc::clone(&input), + })) + }; let our_pubkey = get_pubkey!(); - let net_graph_msg_handler = NetGraphMsgHandler::new(chain_monitor, Arc::clone(&logger)); + let net_graph_msg_handler = NetGraphMsgHandler::new(chain_source, Arc::clone(&logger)); loop { match get_slice!(1)[0] { diff --git a/lightning-net-tokio/src/lib.rs b/lightning-net-tokio/src/lib.rs index a7818d7b85c..e84ee76229f 100644 --- a/lightning-net-tokio/src/lib.rs +++ b/lightning-net-tokio/src/lib.rs @@ -34,13 +34,14 @@ //! type TxBroadcaster = dyn lightning::chain::chaininterface::BroadcasterInterface; //! type FeeEstimator = dyn lightning::chain::chaininterface::FeeEstimator; //! type Logger = dyn lightning::util::logger::Logger; -//! type ChainWatchInterface = dyn lightning::chain::chaininterface::ChainWatchInterface; -//! type ChannelMonitor = lightning::ln::channelmonitor::SimpleManyChannelMonitor, Arc, Arc, Arc>; -//! type ChannelManager = lightning::ln::channelmanager::SimpleArcChannelManager; -//! type PeerManager = lightning::ln::peer_handler::SimpleArcPeerManager; +//! type ChainAccess = dyn lightning::chain::Access; +//! type ChainFilter = dyn lightning::chain::Filter; +//! type ChainMonitor = lightning::chain::chainmonitor::ChainMonitor, Arc, Arc, Arc>; +//! type ChannelManager = lightning::ln::channelmanager::SimpleArcChannelManager; +//! type PeerManager = lightning::ln::peer_handler::SimpleArcPeerManager; //! //! // Connect to node with pubkey their_node_id at addr: -//! async fn connect_to_node(peer_manager: PeerManager, channel_monitor: Arc, channel_manager: ChannelManager, their_node_id: PublicKey, addr: SocketAddr) { +//! async fn connect_to_node(peer_manager: PeerManager, chain_monitor: Arc, channel_manager: ChannelManager, their_node_id: PublicKey, addr: SocketAddr) { //! let (sender, mut receiver) = mpsc::channel(2); //! lightning_net_tokio::connect_outbound(peer_manager, sender, their_node_id, addr).await; //! loop { @@ -48,14 +49,14 @@ //! for _event in channel_manager.get_and_clear_pending_events().drain(..) { //! // Handle the event! //! } -//! for _event in channel_monitor.get_and_clear_pending_events().drain(..) { +//! for _event in chain_monitor.get_and_clear_pending_events().drain(..) { //! // Handle the event! //! } //! } //! } //! //! // Begin reading from a newly accepted socket and talk to the peer: -//! async fn accept_socket(peer_manager: PeerManager, channel_monitor: Arc, channel_manager: ChannelManager, socket: TcpStream) { +//! async fn accept_socket(peer_manager: PeerManager, chain_monitor: Arc, channel_manager: ChannelManager, socket: TcpStream) { //! let (sender, mut receiver) = mpsc::channel(2); //! lightning_net_tokio::setup_inbound(peer_manager, sender, socket); //! loop { @@ -63,7 +64,7 @@ //! for _event in channel_manager.get_and_clear_pending_events().drain(..) { //! // Handle the event! //! } -//! for _event in channel_monitor.get_and_clear_pending_events().drain(..) { +//! for _event in chain_monitor.get_and_clear_pending_events().drain(..) { //! // Handle the event! //! } //! } diff --git a/lightning/src/chain/chaininterface.rs b/lightning/src/chain/chaininterface.rs index f0a4b648ed5..91e604838ec 100644 --- a/lightning/src/chain/chaininterface.rs +++ b/lightning/src/chain/chaininterface.rs @@ -13,63 +13,7 @@ //! Includes traits for monitoring and receiving notifications of new blocks and block //! disconnections, transaction broadcasting, and feerate information requests. -use bitcoin::blockdata::block::{Block, BlockHeader}; use bitcoin::blockdata::transaction::Transaction; -use bitcoin::blockdata::script::Script; -use bitcoin::blockdata::constants::genesis_block; -use bitcoin::network::constants::Network; -use bitcoin::hash_types::{Txid, BlockHash}; - -use std::sync::{Mutex, MutexGuard, Arc}; -use std::sync::atomic::{AtomicUsize, Ordering}; -use std::collections::HashSet; -use std::ops::Deref; -use std::marker::PhantomData; -use std::ptr; - -/// Used to give chain error details upstream -#[derive(Clone)] -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 -/// called from inside the library in response to ChainListener events, P2P events, or timer -/// events). -pub trait ChainWatchInterface: Sync + Send { - /// Provides a txid/random-scriptPubKey-in-the-tx which much be watched for. - fn install_watch_tx(&self, txid: &Txid, script_pub_key: &Script); - - /// Provides an outpoint which must be watched for, providing any transactions which spend the - /// given outpoint. - fn install_watch_outpoint(&self, outpoint: (Txid, u32), out_script: &Script); - - /// Indicates that a listener needs to see all transactions. - fn watch_all_txn(&self); - - /// 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: BlockHash, unspent_tx_output_identifier: u64) -> Result<(Script, u64), ChainError>; - - /// Gets the list of transaction indices within a given block that the ChainWatchInterface is - /// watching for. - fn filter_block(&self, block: &Block) -> Vec; - - /// Returns a usize that changes when the ChainWatchInterface's watched data is modified. - /// Users of `filter_block` should pre-save a copy of `reentered`'s return value and use it to - /// determine whether they need to re-filter a given block. - fn reentered(&self) -> usize; -} /// An interface to send a transaction to the Bitcoin network. pub trait BroadcasterInterface: Sync + Send { @@ -77,30 +21,6 @@ pub trait BroadcasterInterface: Sync + Send { fn broadcast_transaction(&self, tx: &Transaction); } -/// A trait indicating a desire to listen for events from the chain -pub trait ChainListener: Sync + Send { - /// Notifies a listener that a block was connected. - /// - /// The txn_matched array should be set to references to transactions which matched the - /// relevant installed watch outpoints/txn, or the full set of transactions in the block. - /// - /// Note that if txn_matched includes only matched transactions, and a new - /// transaction/outpoint is watched during a block_connected call, the block *must* be - /// re-scanned with the new transaction/outpoints and block_connected should be called - /// again with the same header and (at least) the new transactions. - /// - /// Note that if non-new transaction/outpoints are be registered during a call, a second call - /// *must not* happen. - /// - /// This also means those counting confirmations using block_connected callbacks should watch - /// for duplicate headers and not count them towards confirmations! - fn block_connected(&self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], indexes_of_txn_matched: &[usize]); - /// Notifies a listener that a block was disconnected. - /// Unlike block_connected, this *must* never be called twice for the same disconnect event. - /// Height must be the one of the block which was disconnected (not new height of the best chain) - fn block_disconnected(&self, header: &BlockHeader, disconnected_height: u32); -} - /// An enum that represents the speed at which we want a transaction to confirm used for feerate /// estimation. pub enum ConfirmationTarget { @@ -116,8 +36,7 @@ pub enum ConfirmationTarget { /// horizons. /// /// Note that all of the functions implemented here *must* be reentrant-safe (obviously - they're -/// called from inside the library in response to ChainListener events, P2P events, or timer -/// events). +/// called from inside the library in response to chain events, P2P events, or timer events). pub trait FeeEstimator: Sync + Send { /// Gets estimated satoshis of fee required per 1000 Weight-Units. /// @@ -132,373 +51,3 @@ pub trait FeeEstimator: Sync + Send { /// Minimum relay fee as required by bitcoin network mempool policy. pub const MIN_RELAY_FEE_SAT_PER_1000_WEIGHT: u64 = 4000; - -/// Utility for tracking registered txn/outpoints and checking for matches -#[cfg_attr(test, derive(PartialEq))] -pub struct ChainWatchedUtil { - watch_all: bool, - - // We are more conservative in matching during testing to ensure everything matches *exactly*, - // even though during normal runtime we take more optimized match approaches... - #[cfg(test)] - watched_txn: HashSet<(Txid, Script)>, - #[cfg(not(test))] - watched_txn: HashSet