Skip to content

Commit

Permalink
f - Rename ChainListener to chain::Listen
Browse files Browse the repository at this point in the history
  • Loading branch information
jkczyz committed Feb 24, 2021
1 parent e012c4c commit da79fb0
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 42 deletions.
37 changes: 18 additions & 19 deletions lightning-block-sync/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bitcoin::blockdata::block::{Block, BlockHeader};
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.
Expand All @@ -22,7 +22,6 @@ use lightning::chain::ChainListener;
/// 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;
Expand Down Expand Up @@ -83,8 +82,8 @@ use lightning::chain::ChainListener;
/// 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();
Expand All @@ -105,7 +104,7 @@ pub async fn sync_listeners<B: BlockSource, C: Cache>(
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
Expand Down Expand Up @@ -181,9 +180,9 @@ impl<'a, C: Cache> Cache for ReadOnlyCache<'a, C> {
}

/// 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!()
}
Expand All @@ -194,9 +193,9 @@ impl<'a> ChainListener for DynamicChainListener<'a> {
}

/// 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 {
Expand Down Expand Up @@ -232,9 +231,9 @@ mod tests {
.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 {
Expand Down Expand Up @@ -267,9 +266,9 @@ mod tests {
.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));
Expand Down Expand Up @@ -310,9 +309,9 @@ mod tests {
.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));
Expand All @@ -334,7 +333,7 @@ mod tests {
.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(_) => {
Expand Down
14 changes: 7 additions & 7 deletions lightning-block-sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ use bitcoin::blockdata::block::{Block, BlockHeader};
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;
Expand Down Expand Up @@ -159,7 +160,7 @@ pub struct BlockHeaderData {
/// 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>,
Expand Down Expand Up @@ -207,8 +208,7 @@ impl Cache for UnboundedCache {
}
}

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
Expand Down Expand Up @@ -271,8 +271,8 @@ where L::Target: ChainListener {

/// 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,

Expand All @@ -298,7 +298,7 @@ struct ChainDifference {
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`.
///
Expand Down
8 changes: 5 additions & 3 deletions lightning-block-sync/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
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};
Expand All @@ -7,6 +7,8 @@ use bitcoin::hash_types::BlockHash;
use bitcoin::network::constants::Network;
use bitcoin::util::uint::Uint256;

use lightning::chain;

use std::cell::RefCell;
use std::collections::VecDeque;

Expand Down Expand Up @@ -163,7 +165,7 @@ impl BlockSource for Blockchain {

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) {}
}
Expand Down Expand Up @@ -192,7 +194,7 @@ impl MockChainListener {
}
}

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 => {
Expand Down
3 changes: 1 addition & 2 deletions lightning/src/chain/chainmonitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
use bitcoin::blockdata::block::{Block, BlockHeader};

use chain;
use chain::ChainListener;
use chain::Filter;
use chain::chaininterface::{BroadcasterInterface, FeeEstimator};
use chain::channelmonitor;
Expand Down Expand Up @@ -142,7 +141,7 @@ where C::Target: chain::Filter,
}

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,
Expand Down
4 changes: 2 additions & 2 deletions lightning/src/chain/channelmonitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use ln::chan_utils;
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};
Expand Down Expand Up @@ -2299,7 +2299,7 @@ pub trait Persist<ChannelSigner: Sign>: Send + Sync {
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,
Expand Down
15 changes: 8 additions & 7 deletions lightning/src/chain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ pub trait Access: Send + Sync {
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);

Expand Down Expand Up @@ -136,7 +137,7 @@ pub trait Filter: Send + Sync {
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);
}
Expand All @@ -146,10 +147,10 @@ impl<T: ChainListener> ChainListener for std::ops::Deref<Target = T> {
}
}

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);
Expand Down
3 changes: 1 addition & 2 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ use bitcoin::secp256k1::ecdh::SharedSecret;
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};
Expand Down Expand Up @@ -3140,7 +3139,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> EventsProvi
}
}

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,
Expand Down

0 comments on commit da79fb0

Please sign in to comment.