-use crate::{BlockSource, BlockSourceResult, Cache, ChainListener, ChainNotifier};
+use crate::{BlockSource, BlockSourceResult, Cache, ChainNotifier};
use crate::poll::{ChainPoller, Validate, ValidatedBlockHeader};
use bitcoin::blockdata::block::{Block, BlockHeader};
use bitcoin::hash_types::BlockHash;
use bitcoin::network::constants::Network;
+use lightning::chain::ChainListener;
+
/// Performs a one-time sync of chain listeners using a single *trusted* block source, bringing each
/// listener's view of the chain from its paired block hash to `block_source`'s best chain tip.
///
/// paired with.
///
/// Useful during startup to bring the [`ChannelManager`] and each [`ChannelMonitor`] in sync before
-/// switching to [`SpvClient`].
+/// switching to [`SpvClient`]. For example:
+///
+/// ```
+/// use bitcoin::hash_types::BlockHash;
+/// use bitcoin::network::constants::Network;
+///
+/// use lightning::chain;
+/// use lightning::chain::ChainListener;
+/// use lightning::chain::Watch;
+/// use lightning::chain::chainmonitor::ChainMonitor;
+/// use lightning::chain::channelmonitor;
+/// use lightning::chain::channelmonitor::ChannelMonitor;
+/// use lightning::chain::chaininterface::BroadcasterInterface;
+/// use lightning::chain::chaininterface::FeeEstimator;
+/// use lightning::chain::keysinterface;
+/// use lightning::chain::keysinterface::KeysInterface;
+/// use lightning::ln::channelmanager::ChannelManager;
+/// use lightning::ln::channelmanager::ChannelManagerReadArgs;
+/// use lightning::util::config::UserConfig;
+/// use lightning::util::logger::Logger;
+/// use lightning::util::ser::ReadableArgs;
+///
+/// use lightning_block_sync::*;
+///
+/// use std::cell::RefCell;
+/// use std::io::Cursor;
+///
+/// async fn init_sync<
+/// B: BlockSource,
+/// K: KeysInterface<Signer = S>,
+/// S: keysinterface::Sign,
+/// T: BroadcasterInterface,
+/// F: FeeEstimator,
+/// L: Logger,
+/// C: chain::Filter,
+/// P: channelmonitor::Persist<S>,
+/// >(
+/// block_source: &mut B,
+/// chain_monitor: &ChainMonitor<S, &C, &T, &F, &L, &P>,
+/// config: UserConfig,
+/// keys_manager: &K,
+/// tx_broadcaster: &T,
+/// fee_estimator: &F,
+/// logger: &L,
+/// persister: &P,
+/// ) {
+/// let serialized_monitor = "...";
+/// let (monitor_block_hash, mut monitor) = <(BlockHash, ChannelMonitor<S>)>::read(
+/// &mut Cursor::new(&serialized_monitor), keys_manager).unwrap();
+///
+/// let serialized_manager = "...";
+/// let (manager_block_hash, mut manager) = {
+/// let read_args = ChannelManagerReadArgs::new(
+/// keys_manager,
+/// fee_estimator,
+/// chain_monitor,
+/// tx_broadcaster,
+/// logger,
+/// config,
+/// vec![&mut monitor],
+/// );
+/// <(BlockHash, ChannelManager<S, &ChainMonitor<S, &C, &T, &F, &L, &P>, &T, &K, &F, &L>)>::read(
+/// &mut Cursor::new(&serialized_manager), read_args).unwrap()
+/// };
+///
+/// let mut cache = UnboundedCache::new();
+/// let mut monitor_listener = (RefCell::new(monitor), &*tx_broadcaster, &*fee_estimator, &*logger);
+/// let listeners = vec![
+/// (monitor_block_hash, &mut monitor_listener as &mut dyn ChainListener),
+/// (manager_block_hash, &mut manager as &mut dyn ChainListener),
+/// ];
+/// let chain_tip =
+/// init::sync_listeners(block_source, Network::Bitcoin, &mut cache, listeners).await.unwrap();
+///
+/// let monitor = monitor_listener.0.into_inner();
+/// chain_monitor.watch_channel(monitor.get_funding_txo().0, monitor);
+///
+/// let chain_poller = poll::ChainPoller::new(block_source, Network::Bitcoin);
+/// let mut chain_listener = (chain_monitor, &manager);
+/// let spv_client = SpvClient::new(chain_tip, chain_poller, &mut cache, &chain_listener);
+/// }
+/// ```
///
/// [`SpvClient`]: ../struct.SpvClient.html
/// [`ChannelManager`]: ../../lightning/ln/channelmanager/struct.ChannelManager.html
for (old_header, chain_listener) in chain_listeners_with_old_headers.drain(..) {
// Disconnect any stale blocks, but keep them in the cache for the next iteration.
let header_cache = &mut ReadOnlyCache(header_cache);
- let mut chain_notifier = ChainNotifier { header_cache };
- let difference =
- chain_notifier.find_difference(new_header, &old_header, &mut chain_poller).await?;
- chain_notifier.disconnect_blocks(
- difference.disconnected_blocks,
- &mut DynamicChainListener(chain_listener),
- );
+ let (common_ancestor, connected_blocks) = {
+ let chain_listener = &DynamicChainListener(chain_listener);
+ let mut chain_notifier = ChainNotifier { header_cache, chain_listener };
+ let difference =
+ chain_notifier.find_difference(new_header, &old_header, &mut chain_poller).await?;
+ chain_notifier.disconnect_blocks(difference.disconnected_blocks);
+ (difference.common_ancestor, difference.connected_blocks)
+ };
// Keep track of the most common ancestor and all blocks connected across all listeners.
- chain_listeners_at_height.push((difference.common_ancestor.height, chain_listener));
- if difference.connected_blocks.len() > most_connected_blocks.len() {
- most_common_ancestor = Some(difference.common_ancestor);
- most_connected_blocks = difference.connected_blocks;
+ chain_listeners_at_height.push((common_ancestor.height, chain_listener));
+ if connected_blocks.len() > most_connected_blocks.len() {
+ most_common_ancestor = Some(common_ancestor);
+ most_connected_blocks = connected_blocks;
}
}
// Connect new blocks for all listeners at once to avoid re-fetching blocks.
if let Some(common_ancestor) = most_common_ancestor {
- let mut chain_notifier = ChainNotifier { header_cache };
- let mut chain_listener = ChainListenerSet(chain_listeners_at_height);
- chain_notifier.connect_blocks(
- common_ancestor,
- most_connected_blocks,
- &mut chain_poller,
- &mut chain_listener,
- ).await.or_else(|(e, _)| Err(e))?;
+ let chain_listener = &ChainListenerSet(chain_listeners_at_height);
+ let mut chain_notifier = ChainNotifier { header_cache, chain_listener };
+ chain_notifier.connect_blocks(common_ancestor, most_connected_blocks, &mut chain_poller)
+ .await.or_else(|(e, _)| Err(e))?;
}
Ok(new_header)
struct DynamicChainListener<'a>(&'a mut dyn ChainListener);
impl<'a> ChainListener for DynamicChainListener<'a> {
- fn block_connected(&mut self, _block: &Block, _height: u32) {
+ fn block_connected(&self, _block: &Block, _height: u32) {
unreachable!()
}
- fn block_disconnected(&mut self, header: &BlockHeader, height: u32) {
+ fn block_disconnected(&self, header: &BlockHeader, height: u32) {
self.0.block_disconnected(header, height)
}
}
struct ChainListenerSet<'a>(Vec<(u32, &'a mut dyn ChainListener)>);
impl<'a> ChainListener for ChainListenerSet<'a> {
- fn block_connected(&mut self, block: &Block, height: u32) {
- for (starting_height, chain_listener) in self.0.iter_mut() {
+ fn block_connected(&self, block: &Block, height: u32) {
+ for (starting_height, chain_listener) in self.0.iter() {
if height > *starting_height {
chain_listener.block_connected(block, height);
}
}
}
- fn block_disconnected(&mut self, _header: &BlockHeader, _height: u32) {
+ fn block_disconnected(&self, _header: &BlockHeader, _height: u32) {
unreachable!()
}
}
use bitcoin::hash_types::BlockHash;
use bitcoin::util::uint::Uint256;
+use lightning::chain::ChainListener;
+
use std::future::Future;
+use std::ops::Deref;
use std::pin::Pin;
/// Abstract type for retrieving block headers and data.
/// custom cache eviction policy. This offers flexibility to those sensitive to resource usage.
/// Hence, there is a trade-off between a lower memory footprint and potentially increased network
/// I/O as headers are re-fetched during fork detection.
-pub struct SpvClient<'a, P: Poll, C: Cache, L: ChainListener> {
+pub struct SpvClient<'a, P: Poll, C: Cache, L: Deref>
+where L::Target: ChainListener {
chain_tip: ValidatedBlockHeader,
chain_poller: P,
- chain_notifier: ChainNotifier<'a, C>,
- chain_listener: L,
-}
-
-/// Adaptor used for notifying when blocks have been connected or disconnected from the chain.
-///
-/// Used when needing to replay chain data upon startup or as new chain events occur.
-pub trait ChainListener {
- /// Notifies the listener that a block was added at the given height.
- fn block_connected(&mut self, block: &Block, height: u32);
-
- /// Notifies the listener that a block was removed at the given height.
- fn block_disconnected(&mut self, header: &BlockHeader, height: u32);
+ chain_notifier: ChainNotifier<'a, C, L>,
}
/// The `Cache` trait defines behavior for managing a block header cache, where block headers are
}
}
-impl<'a, P: Poll, C: Cache, L: ChainListener> SpvClient<'a, P, C, L> {
+impl<'a, P: Poll, C: Cache, L: Deref> SpvClient<'a, P, C, L>
+where L::Target: ChainListener {
/// Creates a new SPV client using `chain_tip` as the best known chain tip.
///
/// Subsequent calls to [`poll_best_tip`] will poll for the best chain tip using the given chain
header_cache: &'a mut C,
chain_listener: L,
) -> Self {
- let chain_notifier = ChainNotifier { header_cache };
- Self { chain_tip, chain_poller, chain_notifier, chain_listener }
+ let chain_notifier = ChainNotifier { header_cache, chain_listener };
+ Self { chain_tip, chain_poller, chain_notifier }
}
/// Polls for the best tip and updates the chain listener with any connected or disconnected
/// Updates the chain tip, syncing the chain listener with any connected or disconnected
/// blocks. Returns whether there were any such blocks.
async fn update_chain_tip(&mut self, best_chain_tip: ValidatedBlockHeader) -> bool {
- match self.chain_notifier.sync_listener(best_chain_tip, &self.chain_tip, &mut self.chain_poller, &mut self.chain_listener).await {
+ match self.chain_notifier.sync_listener(best_chain_tip, &self.chain_tip, &mut self.chain_poller).await {
Ok(_) => {
self.chain_tip = best_chain_tip;
true
/// Notifies [listeners] of blocks that have been connected or disconnected from the chain.
///
/// [listeners]: trait.ChainListener.html
-pub struct ChainNotifier<'a, C: Cache> {
+pub struct ChainNotifier<'a, C: Cache, L: Deref> where L::Target: ChainListener {
/// Cache for looking up headers before fetching from a block source.
header_cache: &'a mut C,
+
+ /// Listener that will be notified of connected or disconnected blocks.
+ chain_listener: L,
}
/// Changes made to the chain between subsequent polls that transformed it from having one chain tip
connected_blocks: Vec<ValidatedBlockHeader>,
}
-impl<'a, C: Cache> ChainNotifier<'a, C> {
+impl<'a, C: Cache, L: Deref> ChainNotifier<'a, C, L> where L::Target: ChainListener {
/// Finds the first common ancestor between `new_header` and `old_header`, disconnecting blocks
/// from `old_header` to get to that point and then connecting blocks until `new_header`.
///
/// disconnected to the fork point. Thus, this may return an `Err` that includes where the tip
/// ended up which may not be `new_header`. Note that iff the returned `Err` contains `Some`
/// header then the transition from `old_header` to `new_header` is valid.
- async fn sync_listener<L: ChainListener, P: Poll>(
+ async fn sync_listener<P: Poll>(
&mut self,
new_header: ValidatedBlockHeader,
old_header: &ValidatedBlockHeader,
chain_poller: &mut P,
- chain_listener: &mut L,
) -> Result<(), (BlockSourceError, Option<ValidatedBlockHeader>)> {
let difference = self.find_difference(new_header, old_header, chain_poller).await
.map_err(|e| (e, None))?;
- self.disconnect_blocks(difference.disconnected_blocks, chain_listener);
+ self.disconnect_blocks(difference.disconnected_blocks);
self.connect_blocks(
difference.common_ancestor,
difference.connected_blocks,
chain_poller,
- chain_listener,
).await
}
}
/// Notifies the chain listeners of disconnected blocks.
- fn disconnect_blocks<L: ChainListener>(
- &mut self,
- mut disconnected_blocks: Vec<ValidatedBlockHeader>,
- chain_listener: &mut L,
- ) {
+ fn disconnect_blocks(&mut self, mut disconnected_blocks: Vec<ValidatedBlockHeader>) {
for header in disconnected_blocks.drain(..) {
if let Some(cached_header) = self.header_cache.block_disconnected(&header.block_hash) {
assert_eq!(cached_header, header);
}
- chain_listener.block_disconnected(&header.header, header.height);
+ self.chain_listener.block_disconnected(&header.header, header.height);
}
}
/// Notifies the chain listeners of connected blocks.
- async fn connect_blocks<L: ChainListener, P: Poll>(
+ async fn connect_blocks<P: Poll>(
&mut self,
mut new_tip: ValidatedBlockHeader,
mut connected_blocks: Vec<ValidatedBlockHeader>,
chain_poller: &mut P,
- chain_listener: &mut L,
) -> Result<(), (BlockSourceError, Option<ValidatedBlockHeader>)> {
for header in connected_blocks.drain(..).rev() {
let block = chain_poller
debug_assert_eq!(block.block_hash, header.block_hash);
self.header_cache.block_connected(header.block_hash, header);
- chain_listener.block_connected(&block, header.height);
+ self.chain_listener.block_connected(&block, header.height);
new_tip = header;
}
let poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
let mut cache = UnboundedCache::new();
- let mut client = SpvClient::new(best_tip, poller, &mut cache, NullChainListener {});
+ let mut listener = NullChainListener {};
+ let mut client = SpvClient::new(best_tip, poller, &mut cache, &mut listener);
match client.poll_best_tip().await {
Err(e) => {
assert_eq!(e.kind(), BlockSourceErrorKind::Persistent);
let poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
let mut cache = UnboundedCache::new();
- let mut client = SpvClient::new(common_tip, poller, &mut cache, NullChainListener {});
+ let mut listener = NullChainListener {};
+ let mut client = SpvClient::new(common_tip, poller, &mut cache, &mut listener);
match client.poll_best_tip().await {
Err(e) => panic!("Unexpected error: {:?}", e),
Ok((chain_tip, blocks_connected)) => {
let poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
let mut cache = UnboundedCache::new();
- let mut client = SpvClient::new(old_tip, poller, &mut cache, NullChainListener {});
+ let mut listener = NullChainListener {};
+ let mut client = SpvClient::new(old_tip, poller, &mut cache, &mut listener);
match client.poll_best_tip().await {
Err(e) => panic!("Unexpected error: {:?}", e),
Ok((chain_tip, blocks_connected)) => {
let poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
let mut cache = UnboundedCache::new();
- let mut client = SpvClient::new(old_tip, poller, &mut cache, NullChainListener {});
+ let mut listener = NullChainListener {};
+ let mut client = SpvClient::new(old_tip, poller, &mut cache, &mut listener);
match client.poll_best_tip().await {
Err(e) => panic!("Unexpected error: {:?}", e),
Ok((chain_tip, blocks_connected)) => {
let poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
let mut cache = UnboundedCache::new();
- let mut client = SpvClient::new(old_tip, poller, &mut cache, NullChainListener {});
+ let mut listener = NullChainListener {};
+ let mut client = SpvClient::new(old_tip, poller, &mut cache, &mut listener);
match client.poll_best_tip().await {
Err(e) => panic!("Unexpected error: {:?}", e),
Ok((chain_tip, blocks_connected)) => {
let poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
let mut cache = UnboundedCache::new();
- let mut client = SpvClient::new(best_tip, poller, &mut cache, NullChainListener {});
+ let mut listener = NullChainListener {};
+ let mut client = SpvClient::new(best_tip, poller, &mut cache, &mut listener);
match client.poll_best_tip().await {
Err(e) => panic!("Unexpected error: {:?}", e),
Ok((chain_tip, blocks_connected)) => {
let new_tip = chain.tip();
let old_tip = chain.at_height(1);
- let mut listener = MockChainListener::new()
+ let chain_listener = &MockChainListener::new()
.expect_block_connected(*chain.at_height(2))
.expect_block_connected(*new_tip);
- let mut notifier = ChainNotifier { header_cache: &mut chain.header_cache(0..=1) };
+ let mut notifier = ChainNotifier {
+ header_cache: &mut chain.header_cache(0..=1),
+ chain_listener,
+ };
let mut poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
- match notifier.sync_listener(new_tip, &old_tip, &mut poller, &mut listener).await {
+ match notifier.sync_listener(new_tip, &old_tip, &mut poller).await {
Err((e, _)) => panic!("Unexpected error: {:?}", e),
Ok(_) => {},
}
let new_tip = test_chain.tip();
let old_tip = main_chain.tip();
- let mut listener = MockChainListener::new();
- let mut notifier = ChainNotifier { header_cache: &mut main_chain.header_cache(0..=1) };
+ let chain_listener = &MockChainListener::new();
+ let mut notifier = ChainNotifier {
+ header_cache: &mut main_chain.header_cache(0..=1),
+ chain_listener,
+ };
let mut poller = poll::ChainPoller::new(&mut test_chain, Network::Testnet);
- match notifier.sync_listener(new_tip, &old_tip, &mut poller, &mut listener).await {
+ match notifier.sync_listener(new_tip, &old_tip, &mut poller).await {
Err((e, _)) => {
assert_eq!(e.kind(), BlockSourceErrorKind::Persistent);
assert_eq!(e.into_inner().as_ref().to_string(), "genesis block reached");
let new_tip = fork_chain.tip();
let old_tip = main_chain.tip();
- let mut listener = MockChainListener::new()
+ let chain_listener = &MockChainListener::new()
.expect_block_disconnected(*old_tip)
.expect_block_connected(*new_tip);
- let mut notifier = ChainNotifier { header_cache: &mut main_chain.header_cache(0..=2) };
+ let mut notifier = ChainNotifier {
+ header_cache: &mut main_chain.header_cache(0..=2),
+ chain_listener,
+ };
let mut poller = poll::ChainPoller::new(&mut fork_chain, Network::Testnet);
- match notifier.sync_listener(new_tip, &old_tip, &mut poller, &mut listener).await {
+ match notifier.sync_listener(new_tip, &old_tip, &mut poller).await {
Err((e, _)) => panic!("Unexpected error: {:?}", e),
Ok(_) => {},
}
let new_tip = fork_chain.tip();
let old_tip = main_chain.tip();
- let mut listener = MockChainListener::new()
+ let chain_listener = &MockChainListener::new()
.expect_block_disconnected(*old_tip)
.expect_block_disconnected(*main_chain.at_height(2))
.expect_block_connected(*new_tip);
- let mut notifier = ChainNotifier { header_cache: &mut main_chain.header_cache(0..=3) };
+ let mut notifier = ChainNotifier {
+ header_cache: &mut main_chain.header_cache(0..=3),
+ chain_listener,
+ };
let mut poller = poll::ChainPoller::new(&mut fork_chain, Network::Testnet);
- match notifier.sync_listener(new_tip, &old_tip, &mut poller, &mut listener).await {
+ match notifier.sync_listener(new_tip, &old_tip, &mut poller).await {
Err((e, _)) => panic!("Unexpected error: {:?}", e),
Ok(_) => {},
}
let new_tip = fork_chain.tip();
let old_tip = main_chain.tip();
- let mut listener = MockChainListener::new()
+ let chain_listener = &MockChainListener::new()
.expect_block_disconnected(*old_tip)
.expect_block_connected(*fork_chain.at_height(2))
.expect_block_connected(*new_tip);
- let mut notifier = ChainNotifier { header_cache: &mut main_chain.header_cache(0..=2) };
+ let mut notifier = ChainNotifier {
+ header_cache: &mut main_chain.header_cache(0..=2),
+ chain_listener,
+ };
let mut poller = poll::ChainPoller::new(&mut fork_chain, Network::Testnet);
- match notifier.sync_listener(new_tip, &old_tip, &mut poller, &mut listener).await {
+ match notifier.sync_listener(new_tip, &old_tip, &mut poller).await {
Err((e, _)) => panic!("Unexpected error: {:?}", e),
Ok(_) => {},
}
let new_tip = chain.tip();
let old_tip = chain.at_height(1);
- let mut listener = MockChainListener::new();
- let mut notifier = ChainNotifier { header_cache: &mut chain.header_cache(0..=1) };
+ let chain_listener = &MockChainListener::new();
+ let mut notifier = ChainNotifier {
+ header_cache: &mut chain.header_cache(0..=1),
+ chain_listener,
+ };
let mut poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
- match notifier.sync_listener(new_tip, &old_tip, &mut poller, &mut listener).await {
+ match notifier.sync_listener(new_tip, &old_tip, &mut poller).await {
Err((_, tip)) => assert_eq!(tip, None),
Ok(_) => panic!("Expected error"),
}
let new_tip = chain.tip();
let old_tip = chain.at_height(1);
- let mut listener = MockChainListener::new();
- let mut notifier = ChainNotifier { header_cache: &mut chain.header_cache(0..=3) };
+ let chain_listener = &MockChainListener::new();
+ let mut notifier = ChainNotifier {
+ header_cache: &mut chain.header_cache(0..=3),
+ chain_listener,
+ };
let mut poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
- match notifier.sync_listener(new_tip, &old_tip, &mut poller, &mut listener).await {
+ match notifier.sync_listener(new_tip, &old_tip, &mut poller).await {
Err((_, tip)) => assert_eq!(tip, Some(old_tip)),
Ok(_) => panic!("Expected error"),
}
let new_tip = chain.tip();
let old_tip = chain.at_height(1);
- let mut listener = MockChainListener::new()
+ let chain_listener = &MockChainListener::new()
.expect_block_connected(*chain.at_height(2));
- let mut notifier = ChainNotifier { header_cache: &mut chain.header_cache(0..=3) };
+ let mut notifier = ChainNotifier {
+ header_cache: &mut chain.header_cache(0..=3),
+ chain_listener,
+ };
let mut poller = poll::ChainPoller::new(&mut chain, Network::Testnet);
- match notifier.sync_listener(new_tip, &old_tip, &mut poller, &mut listener).await {
+ match notifier.sync_listener(new_tip, &old_tip, &mut poller).await {
Err((_, tip)) => assert_eq!(tip, Some(chain.at_height(2))),
Ok(_) => panic!("Expected error"),
}
use bitcoin::network::constants::Network;
use bitcoin::util::uint::Uint256;
+use std::cell::RefCell;
use std::collections::VecDeque;
#[derive(Default)]
pub struct NullChainListener;
impl ChainListener for NullChainListener {
- fn block_connected(&mut self, _block: &Block, _height: u32) {}
- fn block_disconnected(&mut self, _header: &BlockHeader, _height: u32) {}
+ fn block_connected(&self, _block: &Block, _height: u32) {}
+ fn block_disconnected(&self, _header: &BlockHeader, _height: u32) {}
}
pub struct MockChainListener {
- expected_blocks_connected: VecDeque<BlockHeaderData>,
- expected_blocks_disconnected: VecDeque<BlockHeaderData>,
+ expected_blocks_connected: RefCell<VecDeque<BlockHeaderData>>,
+ expected_blocks_disconnected: RefCell<VecDeque<BlockHeaderData>>,
}
impl MockChainListener {
pub fn new() -> Self {
Self {
- expected_blocks_connected: VecDeque::new(),
- expected_blocks_disconnected: VecDeque::new(),
+ expected_blocks_connected: RefCell::new(VecDeque::new()),
+ expected_blocks_disconnected: RefCell::new(VecDeque::new()),
}
}
- pub fn expect_block_connected(mut self, block: BlockHeaderData) -> Self {
- self.expected_blocks_connected.push_back(block);
+ pub fn expect_block_connected(self, block: BlockHeaderData) -> Self {
+ self.expected_blocks_connected.borrow_mut().push_back(block);
self
}
- pub fn expect_block_disconnected(mut self, block: BlockHeaderData) -> Self {
- self.expected_blocks_disconnected.push_back(block);
+ pub fn expect_block_disconnected(self, block: BlockHeaderData) -> Self {
+ self.expected_blocks_disconnected.borrow_mut().push_back(block);
self
}
}
impl ChainListener for MockChainListener {
- fn block_connected(&mut self, block: &Block, height: u32) {
- match self.expected_blocks_connected.pop_front() {
+ fn block_connected(&self, block: &Block, height: u32) {
+ match self.expected_blocks_connected.borrow_mut().pop_front() {
None => {
panic!("Unexpected block connected: {:?}", block.block_hash());
},
}
}
- fn block_disconnected(&mut self, header: &BlockHeader, height: u32) {
- match self.expected_blocks_disconnected.pop_front() {
+ fn block_disconnected(&self, header: &BlockHeader, height: u32) {
+ match self.expected_blocks_disconnected.borrow_mut().pop_front() {
None => {
panic!("Unexpected block disconnected: {:?}", header.block_hash());
},
if std::thread::panicking() {
return;
}
- if !self.expected_blocks_connected.is_empty() {
- panic!("Expected blocks connected: {:?}", self.expected_blocks_connected);
+
+ let expected_blocks_connected = self.expected_blocks_connected.borrow();
+ if !expected_blocks_connected.is_empty() {
+ panic!("Expected blocks connected: {:?}", expected_blocks_connected);
}
- if !self.expected_blocks_disconnected.is_empty() {
- panic!("Expected blocks disconnected: {:?}", self.expected_blocks_disconnected);
+
+ let expected_blocks_disconnected = self.expected_blocks_disconnected.borrow();
+ if !expected_blocks_disconnected.is_empty() {
+ panic!("Expected blocks disconnected: {:?}", expected_blocks_disconnected);
}
}
}
//! [`ChannelMonitor`]: ../channelmonitor/struct.ChannelMonitor.html
//! [`MonitorEvent`]: ../channelmonitor/enum.MonitorEvent.html
-use bitcoin::blockdata::block::BlockHeader;
+use bitcoin::blockdata::block::{Block, BlockHeader};
use chain;
+use chain::ChainListener;
use chain::Filter;
use chain::chaininterface::{BroadcasterInterface, FeeEstimator};
use chain::channelmonitor;
}
}
+impl<ChannelSigner: Sign, C: Deref + Send + Sync, T: Deref + Send + Sync, F: Deref + Send + Sync, L: Deref + Send + Sync, P: Deref + Send + Sync>
+ChainListener for ChainMonitor<ChannelSigner, C, T, F, L, P>
+where
+ ChannelSigner: Sign,
+ C::Target: chain::Filter,
+ T::Target: BroadcasterInterface,
+ F::Target: FeeEstimator,
+ L::Target: Logger,
+ P::Target: channelmonitor::Persist<ChannelSigner>,
+{
+ fn block_connected(&self, block: &Block, height: u32) {
+ let txdata: Vec<_> = block.txdata.iter().enumerate().collect();
+ ChainMonitor::block_connected(self, &block.header, &txdata, height);
+ }
+
+ fn block_disconnected(&self, header: &BlockHeader, height: u32) {
+ ChainMonitor::block_disconnected(self, header, height);
+ }
+}
+
impl<ChannelSigner: Sign, C: Deref + Sync + Send, T: Deref + Sync + Send, F: Deref + Sync + Send, L: Deref + Sync + Send, P: Deref + Sync + Send>
chain::Watch<ChannelSigner> for ChainMonitor<ChannelSigner, C, T, F, L, P>
where C::Target: chain::Filter,
//!
//! [`chain::Watch`]: ../trait.Watch.html
-use bitcoin::blockdata::block::BlockHeader;
+use bitcoin::blockdata::block::{Block, BlockHeader};
use bitcoin::blockdata::transaction::{TxOut,Transaction};
use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
use bitcoin::blockdata::script::{Script, Builder};
use ln::chan_utils::{CounterpartyCommitmentSecrets, HTLCOutputInCommitment, HTLCType, ChannelTransactionParameters, HolderCommitmentTransaction};
use ln::channelmanager::{HTLCSource, PaymentPreimage, PaymentHash};
use ln::onchaintx::{OnchainTxHandler, InputDescriptors};
+use chain::ChainListener;
use chain::chaininterface::{BroadcasterInterface, FeeEstimator};
use chain::transaction::{OutPoint, TransactionData};
use chain::keysinterface::{SpendableOutputDescriptor, StaticPaymentOutputDescriptor, DelayedPaymentOutputDescriptor, Sign, KeysInterface};
use util::byte_utils;
use util::events::Event;
+use std::cell::RefCell;
use std::collections::{HashMap, HashSet, hash_map};
use std::{cmp, mem};
use std::ops::Deref;
fn update_persisted_channel(&self, id: OutPoint, update: &ChannelMonitorUpdate, data: &ChannelMonitor<ChannelSigner>) -> Result<(), ChannelMonitorUpdateErr>;
}
+impl<Signer: Sign, T: Deref, F: Deref, L: Deref> ChainListener for (RefCell<ChannelMonitor<Signer>>, T, F, L)
+where
+ T::Target: BroadcasterInterface,
+ F::Target: FeeEstimator,
+ L::Target: Logger,
+{
+ fn block_connected(&self, block: &Block, height: u32) {
+ let txdata: Vec<_> = block.txdata.iter().enumerate().collect();
+ self.0.borrow_mut().block_connected(&block.header, &txdata, height, &*self.1, &*self.2, &*self.3);
+ }
+
+ fn block_disconnected(&self, header: &BlockHeader, height: u32) {
+ self.0.borrow_mut().block_disconnected(header, height, &*self.1, &*self.2, &*self.3);
+ }
+}
+
const MAX_ALLOC_SIZE: usize = 64*1024;
impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
//! Structs and traits which allow other parts of rust-lightning to interact with the blockchain.
+use bitcoin::blockdata::block::{Block, BlockHeader};
use bitcoin::blockdata::script::Script;
use bitcoin::blockdata::transaction::TxOut;
use bitcoin::hash_types::{BlockHash, Txid};
fn get_utxo(&self, genesis_hash: &BlockHash, short_channel_id: u64) -> Result<TxOut, AccessError>;
}
+/// Adaptor used for notifying when blocks have been connected or disconnected from the chain.
+///
+/// Used when needing to replay chain data upon startup or as new chain events occur.
+pub trait ChainListener {
+ /// Notifies the listener that a block was added at the given height.
+ fn block_connected(&self, block: &Block, height: u32);
+
+ /// Notifies the listener that a block was removed at the given height.
+ fn block_disconnected(&self, header: &BlockHeader, height: u32);
+}
+
/// The `Watch` trait defines behavior for watching on-chain activity pertaining to channels as
/// blocks are connected and disconnected.
///
/// `script_pubkey` as the spending condition.
fn register_output(&self, outpoint: &OutPoint, script_pubkey: &Script);
}
+
+impl<T: ChainListener> ChainListener for std::ops::Deref<Target = T> {
+ fn block_connected(&self, block: &Block, height: u32) {
+ (**self).block_connected(block, height);
+ }
+
+ fn block_disconnected(&self, header: &BlockHeader, height: u32) {
+ (**self).block_disconnected(header, height);
+ }
+}
+
+impl<T: std::ops::Deref, U: std::ops::Deref> ChainListener for (T, U)
+where
+ T::Target: ChainListener,
+ U::Target: ChainListener,
+{
+ fn block_connected(&self, block: &Block, height: u32) {
+ self.0.block_connected(block, height);
+ self.1.block_connected(block, height);
+ }
+
+ fn block_disconnected(&self, header: &BlockHeader, height: u32) {
+ self.0.block_disconnected(header, height);
+ self.1.block_disconnected(header, height);
+ }
+}
//! imply it needs to fail HTLCs/payments/channels it manages).
//!
-use bitcoin::blockdata::block::BlockHeader;
+use bitcoin::blockdata::block::{Block, BlockHeader};
use bitcoin::blockdata::constants::genesis_block;
use bitcoin::network::constants::Network;
use bitcoin::secp256k1;
use chain;
+use chain::ChainListener;
use chain::Watch;
use chain::chaininterface::{BroadcasterInterface, FeeEstimator};
use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, ChannelMonitorUpdateErr, HTLC_FAIL_BACK_BUFFER, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ANTI_REORG_DELAY, MonitorEvent, CLOSED_CHANNEL_UPDATE_ID};
}
}
+impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChainListener for ChannelManager<Signer, M, T, K, F, L>
+where
+ M::Target: chain::Watch<Signer>,
+ T::Target: BroadcasterInterface,
+ K::Target: KeysInterface<Signer = Signer>,
+ F::Target: FeeEstimator,
+ L::Target: Logger,
+{
+ fn block_connected(&self, block: &Block, height: u32) {
+ let txdata: Vec<_> = block.txdata.iter().enumerate().collect();
+ ChannelManager::block_connected(self, &block.header, &txdata, height);
+ }
+
+ fn block_disconnected(&self, header: &BlockHeader, _height: u32) {
+ ChannelManager::block_disconnected(self, header);
+ }
+}
+
impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<Signer, M, T, K, F, L>
where M::Target: chain::Watch<Signer>,
T::Target: BroadcasterInterface,