-
Notifications
You must be signed in to change notification settings - Fork 235
/
Copy pathfilter.rs
174 lines (159 loc) · 6.22 KB
/
filter.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use ckb_async_runtime::tokio::{self, task::block_in_place};
use ckb_logger::{debug, info, warn};
use ckb_shared::Shared;
use ckb_stop_handler::{CancellationToken, new_tokio_exit_rx};
use ckb_store::{ChainDB, ChainStore};
use ckb_types::{
core::HeaderView,
packed::{Byte32, CellOutput, OutPoint},
prelude::*,
utilities::{FilterDataProvider, build_filter_data},
};
const NAME: &str = "BlockFilter";
/// A block filter creation service
#[derive(Clone)]
pub struct BlockFilter {
shared: Shared,
}
struct WrappedChainDB<'a> {
inner: &'a ChainDB,
}
impl<'a> FilterDataProvider for WrappedChainDB<'a> {
fn cell(&self, out_point: &OutPoint) -> Option<CellOutput> {
self.inner
.get_transaction(&out_point.tx_hash())
.and_then(|(tx, _)| tx.outputs().get(out_point.index().unpack()))
}
}
impl<'a> WrappedChainDB<'a> {
fn new(inner: &'a ChainDB) -> Self {
Self { inner }
}
}
impl BlockFilter {
/// Create a new block filter service
pub fn new(shared: Shared) -> Self {
Self { shared }
}
/// start background single-threaded service to create block filter data
pub fn start(self) {
let notify_controller = self.shared.notify_controller().clone();
let async_handle = self.shared.async_handle().clone();
let stop_rx: CancellationToken = new_tokio_exit_rx();
let filter_data_builder = self.clone();
let build_filter_data =
async_handle.spawn_blocking(move || filter_data_builder.build_filter_data());
async_handle.spawn(async move {
let mut new_block_watcher = notify_controller.watch_new_block(NAME.to_string()).await;
let _build_filter_data_finished = build_filter_data.await;
loop {
tokio::select! {
Ok(_) = new_block_watcher.changed() => {
block_in_place(|| self.build_filter_data());
new_block_watcher.borrow_and_update();
}
_ = stop_rx.cancelled() => {
info!("BlockFilter received exit signal, exit now");
break
},
else => break,
}
}
});
}
/// build block filter data to the latest block
fn build_filter_data(&self) {
let snapshot = self.shared.snapshot();
let tip_header = snapshot.get_tip_header().expect("tip stored");
let start_number = match snapshot.get_latest_built_filter_data_block_hash() {
Some(block_hash) => {
debug!("Hash of the latest created block {:#x}", block_hash);
if snapshot.is_main_chain(&block_hash) {
let header = snapshot
.get_block_header(&block_hash)
.expect("header stored");
debug!(
"Latest created block on the main chain, starting from {}",
header.number() + 1
);
header.number() + 1
} else {
// find fork chain number
let mut header = snapshot
.get_block_header(&block_hash)
.expect("header stored");
while !snapshot.is_main_chain(&header.parent_hash()) {
header = snapshot
.get_block_header(&header.parent_hash())
.expect("parent header stored");
}
debug!(
"Block with the latest built filter data on the forked chain, starting from {}",
header.number()
);
header.number()
}
}
None => 0,
};
for block_number in start_number..=tip_header.number() {
if ckb_stop_handler::has_received_stop_signal() {
info!("ckb has received stop signal, BlockFilter exit now");
return;
}
let block_hash = snapshot.get_block_hash(block_number).expect("index stored");
let header = snapshot
.get_block_header(&block_hash)
.expect("header stored");
self.build_filter_data_for_block(&header);
}
}
fn build_filter_data_for_block(&self, header: &HeaderView) {
debug!(
"Start building filter data for block: {}, hash: {:#x}",
header.number(),
header.hash()
);
let db = self.shared.store();
if db.get_block_filter_hash(&header.hash()).is_some() {
debug!(
"Filter data for block {:#x} already exists. Skip building.",
header.hash()
);
return;
}
let parent_block_filter_hash = if header.is_genesis() {
Byte32::zero()
} else {
db.get_block_filter_hash(&header.parent_hash())
.expect("parent block filter data stored")
};
let transactions = db.get_block_body(&header.hash());
let transactions_size: usize = transactions.iter().map(|tx| tx.data().total_size()).sum();
let provider = WrappedChainDB::new(db);
let (filter_data, missing_out_points) = build_filter_data(provider, &transactions);
for out_point in missing_out_points {
warn!(
"Unable to find the input cell for the out_point: {:#x}, \
Skip adding it to the filter. This should only happen during testing.",
out_point
);
}
let db_transaction = db.begin_transaction();
db_transaction
.insert_block_filter(
&header.hash(),
&filter_data.pack(),
&parent_block_filter_hash,
)
.expect("insert_block_filter should be ok");
db_transaction.commit().expect("commit should be ok");
debug!(
"Inserted filter data for block: {}, hash: {:#x}, filter data size: {}, transactions size: {}",
header.number(),
header.hash(),
filter_data.len(),
transactions_size
);
}
}