use bitcoin::hash_types::BlockHash;
use bitcoin::network::constants::Network;
-use lightning::chain::ChainListener;
+use lightning::chain;
/// 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.
/// 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;
/// 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),
+/// (monitor_block_hash, &mut monitor_listener as &mut dyn chain::Listen),
+/// (manager_block_hash, &mut manager as &mut dyn chain::Listen),
/// ];
/// let chain_tip =
/// init::sync_listeners(block_source, Network::Bitcoin, &mut cache, listeners).await.unwrap();
block_source: &mut B,
network: Network,
header_cache: &mut C,
- mut chain_listeners: Vec<(BlockHash, &mut dyn ChainListener)>,
+ mut chain_listeners: Vec<(BlockHash, &mut dyn chain::Listen)>,
) -> BlockSourceResult<ValidatedBlockHeader> {
let (best_block_hash, best_block_height) = block_source.get_best_block().await?;
let new_header = block_source
}
/// Wrapper for supporting dynamically sized chain listeners.
-struct DynamicChainListener<'a>(&'a mut dyn ChainListener);
+struct DynamicChainListener<'a>(&'a mut dyn chain::Listen);
-impl<'a> ChainListener for DynamicChainListener<'a> {
+impl<'a> chain::Listen for DynamicChainListener<'a> {
fn block_connected(&self, _block: &Block, _height: u32) {
unreachable!()
}
}
/// A set of dynamically sized chain listeners, each paired with a starting block height.
-struct ChainListenerSet<'a>(Vec<(u32, &'a mut dyn ChainListener)>);
+struct ChainListenerSet<'a>(Vec<(u32, &'a mut dyn chain::Listen)>);
-impl<'a> ChainListener for ChainListenerSet<'a> {
+impl<'a> chain::Listen for ChainListenerSet<'a> {
fn block_connected(&self, block: &Block, height: u32) {
for (starting_height, chain_listener) in self.0.iter() {
if height > *starting_height {
.expect_block_connected(*chain.at_height(4));
let listeners = vec![
- (chain.at_height(1).block_hash, &mut listener_1 as &mut dyn ChainListener),
- (chain.at_height(2).block_hash, &mut listener_2 as &mut dyn ChainListener),
- (chain.at_height(3).block_hash, &mut listener_3 as &mut dyn ChainListener),
+ (chain.at_height(1).block_hash, &mut listener_1 as &mut dyn chain::Listen),
+ (chain.at_height(2).block_hash, &mut listener_2 as &mut dyn chain::Listen),
+ (chain.at_height(3).block_hash, &mut listener_3 as &mut dyn chain::Listen),
];
let mut cache = chain.header_cache(0..=4);
match sync_listeners(&mut chain, Network::Bitcoin, &mut cache, listeners).await {
.expect_block_connected(*main_chain.at_height(4));
let listeners = vec![
- (fork_chain_1.tip().block_hash, &mut listener_1 as &mut dyn ChainListener),
- (fork_chain_2.tip().block_hash, &mut listener_2 as &mut dyn ChainListener),
- (fork_chain_3.tip().block_hash, &mut listener_3 as &mut dyn ChainListener),
+ (fork_chain_1.tip().block_hash, &mut listener_1 as &mut dyn chain::Listen),
+ (fork_chain_2.tip().block_hash, &mut listener_2 as &mut dyn chain::Listen),
+ (fork_chain_3.tip().block_hash, &mut listener_3 as &mut dyn chain::Listen),
];
let mut cache = fork_chain_1.header_cache(2..=4);
cache.extend(fork_chain_2.header_cache(3..=4));
.expect_block_connected(*main_chain.at_height(4));
let listeners = vec![
- (fork_chain_1.tip().block_hash, &mut listener_1 as &mut dyn ChainListener),
- (fork_chain_2.tip().block_hash, &mut listener_2 as &mut dyn ChainListener),
- (fork_chain_3.tip().block_hash, &mut listener_3 as &mut dyn ChainListener),
+ (fork_chain_1.tip().block_hash, &mut listener_1 as &mut dyn chain::Listen),
+ (fork_chain_2.tip().block_hash, &mut listener_2 as &mut dyn chain::Listen),
+ (fork_chain_3.tip().block_hash, &mut listener_3 as &mut dyn chain::Listen),
];
let mut cache = fork_chain_1.header_cache(2..=4);
cache.extend(fork_chain_2.header_cache(3..=4));
.expect_block_disconnected(*old_tip)
.expect_block_connected(*new_tip);
- let listeners = vec![(old_tip.block_hash, &mut listener as &mut dyn ChainListener)];
+ let listeners = vec![(old_tip.block_hash, &mut listener as &mut dyn chain::Listen)];
let mut cache = fork_chain.header_cache(2..=2);
match sync_listeners(&mut main_chain, Network::Bitcoin, &mut cache, listeners).await {
Ok(_) => {
use bitcoin::hash_types::BlockHash;
use bitcoin::util::uint::Uint256;
-use lightning::chain::ChainListener;
+use lightning::chain;
+use lightning::chain::Listen;
use std::future::Future;
use std::ops::Deref;
/// 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: Deref>
-where L::Target: ChainListener {
+where L::Target: chain::Listen {
chain_tip: ValidatedBlockHeader,
chain_poller: P,
chain_notifier: ChainNotifier<'a, C, L>,
}
}
-impl<'a, P: Poll, C: Cache, L: Deref> SpvClient<'a, P, C, L>
-where L::Target: ChainListener {
+impl<'a, P: Poll, C: Cache, L: Deref> SpvClient<'a, P, C, L> where L::Target: chain::Listen {
/// 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
/// Notifies [listeners] of blocks that have been connected or disconnected from the chain.
///
-/// [listeners]: trait.ChainListener.html
-pub struct ChainNotifier<'a, C: Cache, L: Deref> where L::Target: ChainListener {
+/// [listeners]: ../../lightning/chain/trait.Listen.html
+pub struct ChainNotifier<'a, C: Cache, L: Deref> where L::Target: chain::Listen {
/// Cache for looking up headers before fetching from a block source.
header_cache: &'a mut C,
connected_blocks: Vec<ValidatedBlockHeader>,
}
-impl<'a, C: Cache, L: Deref> ChainNotifier<'a, C, L> where L::Target: ChainListener {
+impl<'a, C: Cache, L: Deref> ChainNotifier<'a, C, L> where L::Target: chain::Listen {
/// 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`.
///
-use crate::{AsyncBlockSourceResult, BlockHeaderData, BlockSource, BlockSourceError, ChainListener, UnboundedCache};
+use crate::{AsyncBlockSourceResult, BlockHeaderData, BlockSource, BlockSourceError, UnboundedCache};
use crate::poll::{Validate, ValidatedBlockHeader};
use bitcoin::blockdata::block::{Block, BlockHeader};
use bitcoin::network::constants::Network;
use bitcoin::util::uint::Uint256;
+use lightning::chain;
+
use std::cell::RefCell;
use std::collections::VecDeque;
pub struct NullChainListener;
-impl ChainListener for NullChainListener {
+impl chain::Listen for NullChainListener {
fn block_connected(&self, _block: &Block, _height: u32) {}
fn block_disconnected(&self, _header: &BlockHeader, _height: u32) {}
}
}
}
-impl ChainListener for MockChainListener {
+impl chain::Listen for MockChainListener {
fn block_connected(&self, block: &Block, height: u32) {
match self.expected_blocks_connected.borrow_mut().pop_front() {
None => {
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>
+chain::Listen for ChainMonitor<ChannelSigner, C, T, F, L, P>
where
ChannelSigner: Sign,
C::Target: chain::Filter,
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;
use chain::chaininterface::{BroadcasterInterface, FeeEstimator};
use chain::transaction::{OutPoint, TransactionData};
use chain::keysinterface::{SpendableOutputDescriptor, StaticPaymentOutputDescriptor, DelayedPaymentOutputDescriptor, Sign, KeysInterface};
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)
+impl<Signer: Sign, T: Deref, F: Deref, L: Deref> chain::Listen for (RefCell<ChannelMonitor<Signer>>, T, F, L)
where
T::Target: BroadcasterInterface,
F::Target: FeeEstimator,
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.
+/// The `Listen` trait is used to be notified of 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 {
+/// Useful when needing to replay chain data upon startup or as new chain events occur.
+pub trait Listen {
/// Notifies the listener that a block was added at the given height.
fn block_connected(&self, block: &Block, height: u32);
fn register_output(&self, outpoint: &OutPoint, script_pubkey: &Script);
}
-impl<T: ChainListener> ChainListener for std::ops::Deref<Target = T> {
+impl<T: Listen> Listen for std::ops::Deref<Target = T> {
fn block_connected(&self, block: &Block, height: u32) {
(**self).block_connected(block, height);
}
}
}
-impl<T: std::ops::Deref, U: std::ops::Deref> ChainListener for (T, U)
+impl<T: std::ops::Deref, U: std::ops::Deref> Listen for (T, U)
where
- T::Target: ChainListener,
- U::Target: ChainListener,
+ T::Target: Listen,
+ U::Target: Listen,
{
fn block_connected(&self, block: &Block, height: u32) {
self.0.block_connected(block, height);
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>
+impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> chain::Listen for ChannelManager<Signer, M, T, K, F, L>
where
M::Target: chain::Watch<Signer>,
T::Target: BroadcasterInterface,