Skip to content

Commit 54e9c07

Browse files
Merge pull request #1453 from TheBlueMatt/2022-04-listen-filtered-blocks
Expand `chain::Listen` trivially to accept filtered block data
2 parents 7181b53 + d629a7e commit 54e9c07

File tree

6 files changed

+35
-28
lines changed

6 files changed

+35
-28
lines changed

lightning-block-sync/src/init.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use crate::{BlockSource, BlockSourceResult, Cache, ChainNotifier};
55
use crate::poll::{ChainPoller, Validate, ValidatedBlockHeader};
66

7-
use bitcoin::blockdata::block::{Block, BlockHeader};
7+
use bitcoin::blockdata::block::BlockHeader;
88
use bitcoin::hash_types::BlockHash;
99
use bitcoin::network::constants::Network;
1010

@@ -203,7 +203,7 @@ impl<'a, C: Cache> Cache for ReadOnlyCache<'a, C> {
203203
struct DynamicChainListener<'a, L: chain::Listen + ?Sized>(&'a L);
204204

205205
impl<'a, L: chain::Listen + ?Sized> chain::Listen for DynamicChainListener<'a, L> {
206-
fn block_connected(&self, _block: &Block, _height: u32) {
206+
fn filtered_block_connected(&self, _header: &BlockHeader, _txdata: &chain::transaction::TransactionData, _height: u32) {
207207
unreachable!()
208208
}
209209

@@ -216,10 +216,10 @@ impl<'a, L: chain::Listen + ?Sized> chain::Listen for DynamicChainListener<'a, L
216216
struct ChainListenerSet<'a, L: chain::Listen + ?Sized>(Vec<(u32, &'a L)>);
217217

218218
impl<'a, L: chain::Listen + ?Sized> chain::Listen for ChainListenerSet<'a, L> {
219-
fn block_connected(&self, block: &Block, height: u32) {
219+
fn filtered_block_connected(&self, header: &BlockHeader, txdata: &chain::transaction::TransactionData, height: u32) {
220220
for (starting_height, chain_listener) in self.0.iter() {
221221
if height > *starting_height {
222-
chain_listener.block_connected(block, height);
222+
chain_listener.filtered_block_connected(header, txdata, height);
223223
}
224224
}
225225
}

lightning-block-sync/src/test_utils.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ impl BlockSource for Blockchain {
166166
pub struct NullChainListener;
167167

168168
impl chain::Listen for NullChainListener {
169-
fn block_connected(&self, _block: &Block, _height: u32) {}
169+
fn filtered_block_connected(&self, _header: &BlockHeader, _txdata: &chain::transaction::TransactionData, _height: u32) {}
170170
fn block_disconnected(&self, _header: &BlockHeader, _height: u32) {}
171171
}
172172

@@ -195,13 +195,13 @@ impl MockChainListener {
195195
}
196196

197197
impl chain::Listen for MockChainListener {
198-
fn block_connected(&self, block: &Block, height: u32) {
198+
fn filtered_block_connected(&self, header: &BlockHeader, _txdata: &chain::transaction::TransactionData, height: u32) {
199199
match self.expected_blocks_connected.borrow_mut().pop_front() {
200200
None => {
201-
panic!("Unexpected block connected: {:?}", block.block_hash());
201+
panic!("Unexpected block connected: {:?}", header.block_hash());
202202
},
203203
Some(expected_block) => {
204-
assert_eq!(block.block_hash(), expected_block.header.block_hash());
204+
assert_eq!(header.block_hash(), expected_block.header.block_hash());
205205
assert_eq!(height, expected_block.height);
206206
},
207207
}

lightning/src/chain/chainmonitor.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
//! events. The remote server would make use of [`ChainMonitor`] for block processing and for
2424
//! servicing [`ChannelMonitor`] updates from the client.
2525
26-
use bitcoin::blockdata::block::{Block, BlockHeader};
26+
use bitcoin::blockdata::block::BlockHeader;
2727
use bitcoin::hash_types::Txid;
2828

2929
use chain;
@@ -501,9 +501,7 @@ where
501501
L::Target: Logger,
502502
P::Target: Persist<ChannelSigner>,
503503
{
504-
fn block_connected(&self, block: &Block, height: u32) {
505-
let header = &block.header;
506-
let txdata: Vec<_> = block.txdata.iter().enumerate().collect();
504+
fn filtered_block_connected(&self, header: &BlockHeader, txdata: &TransactionData, height: u32) {
507505
log_debug!(self.logger, "New best block {} at height {} provided via block_connected", header.block_hash(), height);
508506
self.process_chain_data(header, Some(height), &txdata, |monitor, txdata| {
509507
monitor.block_connected(

lightning/src/chain/channelmonitor.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
//! security-domain-separated system design, you should consider having multiple paths for
2121
//! ChannelMonitors to get out of the HSM and onto monitoring devices.
2222
23-
use bitcoin::blockdata::block::{Block, BlockHeader};
23+
use bitcoin::blockdata::block::BlockHeader;
2424
use bitcoin::blockdata::transaction::{TxOut,Transaction};
2525
use bitcoin::blockdata::script::{Script, Builder};
2626
use bitcoin::blockdata::opcodes;
@@ -3007,9 +3007,8 @@ where
30073007
F::Target: FeeEstimator,
30083008
L::Target: Logger,
30093009
{
3010-
fn block_connected(&self, block: &Block, height: u32) {
3011-
let txdata: Vec<_> = block.txdata.iter().enumerate().collect();
3012-
self.0.block_connected(&block.header, &txdata, height, &*self.1, &*self.2, &*self.3);
3010+
fn filtered_block_connected(&self, header: &BlockHeader, txdata: &TransactionData, height: u32) {
3011+
self.0.block_connected(header, txdata, height, &*self.1, &*self.2, &*self.3);
30133012
}
30143013

30153014
fn block_disconnected(&self, header: &BlockHeader, height: u32) {

lightning/src/chain/mod.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,20 @@ pub trait Access {
8787
/// sourcing chain data using a block-oriented API should prefer this interface over [`Confirm`].
8888
/// Such clients fetch the entire header chain whereas clients using [`Confirm`] only fetch headers
8989
/// when needed.
90+
///
91+
/// By using [`Listen::filtered_block_connected`] this interface supports clients fetching the
92+
/// entire header chain and only blocks with matching transaction data using BIP 157 filters or
93+
/// other similar filtering.
9094
pub trait Listen {
95+
/// Notifies the listener that a block was added at the given height, with the transaction data
96+
/// possibly filtered.
97+
fn filtered_block_connected(&self, header: &BlockHeader, txdata: &TransactionData, height: u32);
98+
9199
/// Notifies the listener that a block was added at the given height.
92-
fn block_connected(&self, block: &Block, height: u32);
100+
fn block_connected(&self, block: &Block, height: u32) {
101+
let txdata: Vec<_> = block.txdata.iter().enumerate().collect();
102+
self.filtered_block_connected(&block.header, &txdata, height);
103+
}
93104

94105
/// Notifies the listener that a block was removed at the given height.
95106
fn block_disconnected(&self, header: &BlockHeader, height: u32);
@@ -355,8 +366,8 @@ pub struct WatchedOutput {
355366
}
356367

357368
impl<T: Listen> Listen for core::ops::Deref<Target = T> {
358-
fn block_connected(&self, block: &Block, height: u32) {
359-
(**self).block_connected(block, height);
369+
fn filtered_block_connected(&self, header: &BlockHeader, txdata: &TransactionData, height: u32) {
370+
(**self).filtered_block_connected(header, txdata, height);
360371
}
361372

362373
fn block_disconnected(&self, header: &BlockHeader, height: u32) {
@@ -369,9 +380,9 @@ where
369380
T::Target: Listen,
370381
U::Target: Listen,
371382
{
372-
fn block_connected(&self, block: &Block, height: u32) {
373-
self.0.block_connected(block, height);
374-
self.1.block_connected(block, height);
383+
fn filtered_block_connected(&self, header: &BlockHeader, txdata: &TransactionData, height: u32) {
384+
self.0.filtered_block_connected(header, txdata, height);
385+
self.1.filtered_block_connected(header, txdata, height);
375386
}
376387

377388
fn block_disconnected(&self, header: &BlockHeader, height: u32) {

lightning/src/ln/channelmanager.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
//! imply it needs to fail HTLCs/payments/channels it manages).
1919
//!
2020
21-
use bitcoin::blockdata::block::{Block, BlockHeader};
21+
use bitcoin::blockdata::block::BlockHeader;
2222
use bitcoin::blockdata::transaction::Transaction;
2323
use bitcoin::blockdata::constants::genesis_block;
2424
use bitcoin::network::constants::Network;
@@ -5303,18 +5303,17 @@ where
53035303
F::Target: FeeEstimator,
53045304
L::Target: Logger,
53055305
{
5306-
fn block_connected(&self, block: &Block, height: u32) {
5306+
fn filtered_block_connected(&self, header: &BlockHeader, txdata: &TransactionData, height: u32) {
53075307
{
53085308
let best_block = self.best_block.read().unwrap();
5309-
assert_eq!(best_block.block_hash(), block.header.prev_blockhash,
5309+
assert_eq!(best_block.block_hash(), header.prev_blockhash,
53105310
"Blocks must be connected in chain-order - the connected header must build on the last connected header");
53115311
assert_eq!(best_block.height(), height - 1,
53125312
"Blocks must be connected in chain-order - the connected block height must be one greater than the previous height");
53135313
}
53145314

5315-
let txdata: Vec<_> = block.txdata.iter().enumerate().collect();
5316-
self.transactions_confirmed(&block.header, &txdata, height);
5317-
self.best_block_updated(&block.header, height);
5315+
self.transactions_confirmed(header, txdata, height);
5316+
self.best_block_updated(header, height);
53185317
}
53195318

53205319
fn block_disconnected(&self, header: &BlockHeader, height: u32) {

0 commit comments

Comments
 (0)