-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathchain_monitor.rs
362 lines (317 loc) · 13.1 KB
/
chain_monitor.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
//! Logic related to the ChainMonitor, the component in charge of querying block data from `bitcoind`.
//!
use std::ops::Deref;
use std::sync::{Arc, Condvar, Mutex};
use std::time;
use tokio::time::timeout;
use triggered::Listener;
use lightning::chain;
use lightning_block_sync::poll::{ChainTip, Poll, ValidatedBlockHeader};
use lightning_block_sync::{BlockSourceErrorKind, Cache, SpvClient};
use crate::dbm::DBM;
/// Component in charge of monitoring the chain for new blocks.
///
/// Takes care of polling `bitcoind` for new tips and hand it to subscribers.
/// It is mainly a wrapper around [chain::Listen] that provides some logging.
pub struct ChainMonitor<'a, P, C, L>
where
P: Poll,
C: Cache,
L: Deref,
L::Target: chain::Listen,
{
/// A bitcoin client to poll best tips from.
spv_client: SpvClient<'a, P, C, L>,
/// The lat known block header by the [ChainMonitor].
last_known_block_header: ValidatedBlockHeader,
/// A [DBM] (database manager) instance. Used to persist block data into disk.
dbm: Arc<Mutex<DBM>>,
/// The time between polls.
polling_delta: time::Duration,
/// A signal from the main thread indicating the tower is shuting down.
shutdown_signal: Listener,
/// A flag that indicates wether bitcoind is reachable or not.
bitcoind_reachable: Arc<(Mutex<bool>, Condvar)>,
}
impl<'a, P, C, L> ChainMonitor<'a, P, C, L>
where
P: Poll,
C: Cache,
L: Deref,
L::Target: chain::Listen,
{
/// Creates a new [ChainMonitor] instance.
pub async fn new(
spv_client: SpvClient<'a, P, C, L>,
last_known_block_header: ValidatedBlockHeader,
dbm: Arc<Mutex<DBM>>,
polling_delta_sec: u16,
shutdown_signal: Listener,
bitcoind_reachable: Arc<(Mutex<bool>, Condvar)>,
) -> ChainMonitor<'a, P, C, L> {
ChainMonitor {
spv_client,
last_known_block_header,
dbm,
polling_delta: time::Duration::from_secs(polling_delta_sec as u64),
shutdown_signal,
bitcoind_reachable,
}
}
/// Polls the best chain tip from bitcoind. Serves the data to its listeners (through [chain::Listen]) and logs data about the polled tips.
pub async fn poll_best_tip(&mut self) {
let (reachable, notifier) = &*self.bitcoind_reachable;
match self.spv_client.poll_best_tip().await {
Ok((chain_tip, _)) => {
match chain_tip {
ChainTip::Common => log::debug!("No new best tip found"),
ChainTip::Better(new_best) => {
log::debug!("Updating best tip: {}", new_best.header.block_hash());
self.last_known_block_header = new_best;
self.dbm
.lock()
.unwrap()
.store_last_known_block(&new_best.header.block_hash())
.unwrap();
}
ChainTip::Worse(worse) => {
// This would happen both if a block has less chainwork than the previous one, or if it has the same chainwork
// but it forks from the parent. In both cases, it'll be detected as a reorg once (if) the new chain grows past
// the current tip.
log::warn!("Worse tip found: {:?}", worse.header.block_hash());
if worse.chainwork == self.last_known_block_header.chainwork {
log::warn!("New tip has the same work as the previous one")
} else {
log::warn!("New tip has less work than the previous one")
}
}
}
*reachable.lock().unwrap() = true;
notifier.notify_all();
}
Err(e) => match e.kind() {
BlockSourceErrorKind::Persistent => {
// FIXME: This may need finer catching
log::error!("Unexpected persistent error: {:?}", e);
}
BlockSourceErrorKind::Transient => {
// Treating all transient as connection errors at least for now.
log::error!("Connection lost with bitcoind");
*reachable.lock().unwrap() = false;
}
},
};
}
/// Monitors `bitcoind` polling the best chain tip every [polling_delta](Self::polling_delta).
pub async fn monitor_chain(&mut self) {
loop {
self.poll_best_tip().await;
// Sleep for self.polling_delta seconds or shutdown if the signal is received.
if timeout(self.polling_delta, self.shutdown_signal.clone())
.await
.is_ok()
{
log::debug!("Received shutting down signal. Shutting down");
break;
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::cell::RefCell;
use std::collections::HashSet;
use std::iter::FromIterator;
use std::thread;
use bitcoin::network::constants::Network;
use bitcoin::BlockHash;
use lightning_block_sync::{poll::ChainPoller, SpvClient, UnboundedCache};
use crate::test_utils::{Blockchain, START_HEIGHT};
pub(crate) struct DummyListener {
pub connected_blocks: RefCell<HashSet<BlockHash>>,
pub disconnected_blocks: RefCell<HashSet<BlockHash>>,
}
impl DummyListener {
fn new() -> Self {
Self {
connected_blocks: RefCell::new(HashSet::new()),
disconnected_blocks: RefCell::new(HashSet::new()),
}
}
}
impl chain::Listen for DummyListener {
fn block_connected(&self, block: &bitcoin::Block, _: u32) {
self.connected_blocks
.borrow_mut()
.insert(block.block_hash());
}
fn block_disconnected(&self, header: &bitcoin::BlockHeader, _: u32) {
self.disconnected_blocks
.borrow_mut()
.insert(header.block_hash());
}
}
#[tokio::test]
async fn test_poll_best_tip_common() {
let mut chain = Blockchain::default().with_height(START_HEIGHT);
let tip = chain.tip();
let dbm = Arc::new(Mutex::new(DBM::in_memory().unwrap()));
let (_, shutdown_signal) = triggered::trigger();
let listener = DummyListener::new();
let poller = ChainPoller::new(&mut chain, Network::Bitcoin);
let cache = &mut UnboundedCache::new();
let spv_client = SpvClient::new(tip, poller, cache, &listener);
let bitcoind_reachable = Arc::new((Mutex::new(true), Condvar::new()));
let mut cm =
ChainMonitor::new(spv_client, tip, dbm, 1, shutdown_signal, bitcoind_reachable).await;
// If there's no new block nothing gets connected nor disconnected
cm.poll_best_tip().await;
assert!(listener.connected_blocks.borrow().is_empty());
assert!(listener.disconnected_blocks.borrow().is_empty());
}
#[tokio::test]
async fn test_poll_best_tip_better() {
let mut chain = Blockchain::default().with_height(START_HEIGHT);
let new_tip = chain.tip();
let old_tip = chain.at_height(START_HEIGHT - 1);
let dbm = Arc::new(Mutex::new(DBM::in_memory().unwrap()));
let (_, shutdown_signal) = triggered::trigger();
let listener = DummyListener::new();
let poller = ChainPoller::new(&mut chain, Network::Bitcoin);
let cache = &mut UnboundedCache::new();
let spv_client = SpvClient::new(old_tip, poller, cache, &listener);
let bitcoind_reachable = Arc::new((Mutex::new(true), Condvar::new()));
let mut cm = ChainMonitor::new(
spv_client,
old_tip,
dbm,
1,
shutdown_signal,
bitcoind_reachable,
)
.await;
// If a new (best) block gets mined, it should be connected
cm.poll_best_tip().await;
assert_eq!(cm.last_known_block_header, new_tip);
assert_eq!(
cm.dbm.lock().unwrap().load_last_known_block().unwrap(),
new_tip.deref().header.block_hash()
);
assert!(listener
.connected_blocks
.borrow()
.contains(&new_tip.deref().header.block_hash()));
assert!(listener.disconnected_blocks.borrow().is_empty());
}
#[tokio::test]
async fn test_poll_best_tip_worse() {
let mut chain = Blockchain::default().with_height(START_HEIGHT);
let best_tip = chain.tip();
chain.disconnect_tip();
let dbm = Arc::new(Mutex::new(DBM::in_memory().unwrap()));
let (_, shutdown_signal) = triggered::trigger();
let listener = DummyListener::new();
let poller = ChainPoller::new(&mut chain, Network::Bitcoin);
let cache = &mut UnboundedCache::new();
let spv_client = SpvClient::new(best_tip, poller, cache, &listener);
let bitcoind_reachable = Arc::new((Mutex::new(true), Condvar::new()));
let mut cm = ChainMonitor::new(
spv_client,
best_tip,
dbm,
1,
shutdown_signal,
bitcoind_reachable,
)
.await;
// If a new (worse, just one) block gets mined, nothing gets connected nor disconnected
cm.poll_best_tip().await;
assert_eq!(cm.last_known_block_header, best_tip);
assert!(matches!(
cm.dbm.lock().unwrap().load_last_known_block(),
Err { .. }
));
assert!(listener.connected_blocks.borrow().is_empty());
assert!(listener.disconnected_blocks.borrow().is_empty());
}
#[tokio::test]
async fn test_poll_best_tip_reorg() {
let mut chain = Blockchain::default().with_height(START_HEIGHT);
let old_best = chain.tip();
// Reorg
chain.disconnect_tip();
let new_blocks = (0..2)
.map(|_| chain.generate(None).block_hash())
.collect::<HashSet<BlockHash>>();
let new_best = chain.tip();
let dbm = Arc::new(Mutex::new(DBM::in_memory().unwrap()));
let (_, shutdown_signal) = triggered::trigger();
let listener = DummyListener::new();
let poller = ChainPoller::new(&mut chain, Network::Bitcoin);
let cache = &mut UnboundedCache::new();
let spv_client = SpvClient::new(old_best, poller, cache, &listener);
let bitcoind_reachable = Arc::new((Mutex::new(true), Condvar::new()));
let mut cm = ChainMonitor::new(
spv_client,
old_best,
dbm,
1,
shutdown_signal,
bitcoind_reachable,
)
.await;
// If a a reorg is found (tip is disconnected and a new best is found), both data should be connected and disconnected
cm.poll_best_tip().await;
assert_eq!(cm.last_known_block_header, new_best);
assert_eq!(
cm.dbm.lock().unwrap().load_last_known_block().unwrap(),
new_best.deref().header.block_hash()
);
assert_eq!(*listener.connected_blocks.borrow(), new_blocks);
assert_eq!(
*listener.disconnected_blocks.borrow(),
HashSet::from_iter([old_best.deref().header.block_hash()])
);
}
#[tokio::test]
async fn test_poll_best_tip_bitcoind_unreachable() {
let mut chain = Blockchain::default().unreachable();
let chain_offline = chain.unreachable.clone();
let tip = chain.tip();
let dbm = Arc::new(Mutex::new(DBM::in_memory().unwrap()));
let (_, shutdown_signal) = triggered::trigger();
let listener = DummyListener::new();
let poller = ChainPoller::new(&mut chain, Network::Bitcoin);
let cache = &mut UnboundedCache::new();
let spv_client = SpvClient::new(tip, poller, cache, &listener);
let bitcoind_reachable = Arc::new((Mutex::new(true), Condvar::new()));
let mut cm = ChainMonitor::new(
spv_client,
tip,
dbm,
1,
shutdown_signal,
bitcoind_reachable.clone(),
)
.await;
// Our block source was defined as unreachable (bitcoind is off). Check that the unreachable flag is set after polling.
cm.poll_best_tip().await;
let (reachable, _) = &*bitcoind_reachable.clone();
assert!(!*reachable.lock().unwrap());
// Set a thread to block on bitcoind unreachable to check that it gets notified once bitcoind comes back online
let t = thread::spawn(move || {
let (lock, notifier) = &*bitcoind_reachable;
let mut reachable = lock.lock().unwrap();
while !*reachable {
reachable = notifier.wait(reachable).unwrap();
}
});
// Set bitcoind as reachable again and check back
*chain_offline.lock().unwrap() = false;
cm.poll_best_tip().await;
assert!(*reachable.lock().unwrap());
// This would hang if the cm didn't notify their subscribers about the bitcoind status, so it serves as out assert.
t.join().unwrap();
}
}