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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
use std::{
collections::BTreeMap,
sync::Arc,
task::{Context, Poll},
};
use futures::StreamExt;
use nimiq_blockchain::{interface::HistoryInterface, Blockchain, CHUNK_SIZE};
use nimiq_blockchain_interface::AbstractBlockchain;
use nimiq_blockchain_proxy::BlockchainProxy;
use nimiq_hash::Blake2bHash;
use nimiq_network_interface::{
network::{CloseReason, Network},
request::RequestError,
};
use nimiq_primitives::policy::Policy;
use nimiq_transaction::historic_transaction::HistoricTransaction;
use super::LightMacroSync;
use crate::{
messages::{HistoryChunk, HistoryChunkError, RequestHistoryChunk},
sync::{light::sync::ValidityChunkRequest, sync_interface::MacroSyncReturn},
};
impl<TNetwork: Network> LightMacroSync<TNetwork> {
fn reset_validity_synchronization(&mut self) {
self.validity_requests = None;
for peer_id in std::mem::take(&mut self.syncing_peers) {
self.validity_queue.remove_peer(&peer_id);
}
}
pub async fn request_validity_window_chunk(
network: Arc<TNetwork>,
peer_id: TNetwork::PeerId,
epoch_number: u32,
block_number: u32,
chunk_index: u64,
) -> Result<Result<HistoryChunk, HistoryChunkError>, RequestError> {
// A validity window chunk is simply a history chunk
network
.request::<RequestHistoryChunk>(
RequestHistoryChunk {
epoch_number,
block_number,
chunk_index,
},
peer_id,
)
.await
}
/// Determines the correct epoch and chunk index to begin syncing from, based on the current state of
/// the local history store. Sets up internal state to track progress and sends the first request.
fn start_validity_chunk_request(
&mut self,
peer_id: TNetwork::PeerId,
mut verifier_block_number: u32,
expected_root: Blake2bHash,
validity_window_start: u32,
mut election_in_window: bool,
) {
// By default we set the parameters assuming we are starting from the beginning of the epoch.
let mut epoch_number = Policy::epoch_at(verifier_block_number);
let mut chunk_index = 0;
let mut root_hash = expected_root.clone();
let mut last_chunk_items: Option<usize> = None;
let blockchain = match &self.blockchain {
BlockchainProxy::Full(blockchain) => blockchain,
BlockchainProxy::Light(_) => unreachable!(),
};
let blockchain_wr = blockchain.read();
// First we need to check if we already have items in the history store, and request only the remaining portion (if any)
let (first_bn, last_bn) = blockchain_wr.history_store.history_store_range(None);
// This means we already have items for the first epoch of the validity window we are interested in
// so we need to move to the next epoch to request the missing items.
if last_bn >= verifier_block_number {
epoch_number = Policy::epoch_at(last_bn);
// This length is based on number of leaves in the tree
let current_length = blockchain_wr
.history_store
.length_at(last_bn, None)
.expect("We must have items in the history store at this height")
as usize;
log::debug!(
first_bn = first_bn,
last_bn = last_bn,
current_items = current_length,
"We already have items in our history store, moving to the current epoch",
);
chunk_index = (current_length / CHUNK_SIZE) as u32;
last_chunk_items = Some(current_length % CHUNK_SIZE);
root_hash = blockchain_wr.macro_head().header.history_root.clone();
verifier_block_number = blockchain_wr.macro_head().block_number();
election_in_window = false;
} else if last_bn >= validity_window_start {
// This is the case where we already have items in the start of the validity window
// We need to request the missing items
epoch_number = Policy::epoch_at(last_bn);
// This length is based on number of leaves in the tree
let current_length = blockchain_wr
.history_store
.length_at(last_bn, None)
.expect("We must have items in the history store at this height")
as usize;
log::debug!(
first_bn = first_bn,
last_bn = last_bn,
current_items = current_length,
"We already have items in our history store",
);
chunk_index = (current_length / CHUNK_SIZE) as u32;
last_chunk_items = Some(current_length % CHUNK_SIZE);
}
// Request the macro chain at this height to know the history tree length at this height
self.validity_requests = Some(ValidityChunkRequest {
verifier_block_number,
root_hash,
chunk_index,
election_in_window,
last_chunk_items,
});
// Add the peer
self.validity_queue.add_peer(peer_id);
self.syncing_peers.insert(peer_id);
let request = RequestHistoryChunk {
epoch_number,
block_number: verifier_block_number,
chunk_index: chunk_index as u64,
};
log::info!(
target_macro = verifier_block_number,
chunk_index = chunk_index,
last_chunk_items = last_chunk_items,
epoch = epoch_number,
validity_start = validity_window_start,
election_in_between = election_in_window,
expected_root = %expected_root,
"Starting validity window synchronization process"
);
// Request the chunk
self.validity_queue.add_ids(vec![(request, None)]);
}
/// Begins the validity window sync process with the given peer from a specific starting point
pub fn start_validity_synchronization(&mut self, peer_id: TNetwork::PeerId) {
if let Some(validity_requests) = &self.validity_requests {
// We already have a synchoronization in progress, so we just add the peer
let current_chunk = validity_requests.chunk_index;
log::debug!(%peer_id,current_chunk,"Adding peer to existing validity synchronization process");
self.validity_queue.add_peer(peer_id);
self.syncing_peers.insert(peer_id);
return;
}
let macro_head = self.blockchain.read().macro_head().header.clone();
let validity_start = macro_head
.block_number
.saturating_sub(Policy::transaction_validity_window_blocks());
let validity_window_bn = if validity_start <= Policy::genesis_block_number() {
Policy::genesis_block_number()
} else {
// We move the validity window to the beginning of the epoch for simplicity reasons.
// There are two possible cases:
// A - The validity window is contained in the current epoch
// B - The validity window spans across the previous epoch and the current one
//
// For case A we move the validity window to the beginning of the epoch because
// apart from syncing the window, we also want to be able to verify the history root
// of blocks that belong to the current epoch; by doing the latter we can also verify the former.
//
// For case B it is not necessary to move the validity window to the beginning of the previous
// epoch because we already have an election block that can be used to proof txns within the
// previous epoch, but, for simplicity reasons we decided to still move it because otherwise
// we would need to start at a specific chunk of the previous epoch and consequently we will need a proof
// that the server is not omiting or adding information when starting from any chunk index.
Policy::election_block_before(validity_start)
};
// This must correspond to a macro block.
assert!(Policy::is_macro_block_at(validity_window_bn));
log::trace!(
macro_head = macro_head.block_number,
validity_start = validity_window_bn,
"Starting a new validity synchronization process"
);
let next_election = Policy::election_block_after(validity_window_bn);
// Now we determine which is the right root and block number to verify the first chunks
let (verifier_block_number, expected_root, election_in_window) =
if next_election < macro_head.block_number {
// This is the case where we are crossing an election block
let election = self
.blockchain
.read()
.get_block_at(next_election, false)
.unwrap();
(next_election, election.history_root().clone(), true)
} else {
// We don't have any election in between so we use the macro head
(macro_head.block_number, macro_head.history_root, false)
};
self.start_validity_chunk_request(
peer_id,
verifier_block_number,
expected_root,
validity_window_bn,
election_in_window,
);
}
/// Process the history chunks that are received as part of the validity window synchronization process
/// Each time a history chunk is received, it is verified and the history store is updated.
pub fn poll_validity_window_chunks(
&mut self,
cx: &mut Context<'_>,
) -> Poll<Option<MacroSyncReturn<TNetwork::PeerId>>> {
while let Poll::Ready(Some(queue_result)) = self.validity_queue.poll_next_unpin(cx) {
let (request, result, peer_id) = match queue_result {
Ok(result) => result,
Err(failed_request) => {
log::warn!(
epoch_number = failed_request.epoch_number,
block_number = failed_request.block_number,
chunk_index = failed_request.chunk_index,
"Validity sync request failed after exhausting all retry attempts, resetting validity synchronization state"
);
self.reset_validity_synchronization();
break;
}
};
log::trace!(%peer_id, chunk_index=request.chunk_index, block_number=request.block_number, "Processing response from validity queue");
match result {
Ok(chunk) => {
let peer_request = self.validity_requests.as_mut().unwrap();
let expected_root = peer_request.root_hash.clone();
let mut verifier_block_number = peer_request.verifier_block_number;
let leaf_index = peer_request.chunk_index * (CHUNK_SIZE as u32);
let chunk = chunk.chunk;
log::info!(
chunk_index = peer_request.chunk_index,
chunk_size = chunk.history.len(),
target_macro = verifier_block_number,
"Applying a new history chunk"
);
// Verify the history chunk
let valid_chunk = chunk
.verify(&expected_root, leaf_index as usize)
.is_some_and(|result| result);
if !valid_chunk {
// If the chunk doesn't verify we disconnect from the peer
log::warn!(%peer_id,
chunk=request.chunk_index,
verifier_block=request.block_number,
epoch=request.epoch_number,
%expected_root,
"Banning peer because the history chunk didn't verify");
// For debug purposes we print a summary of the chunk contents
let mut txns_per_block: BTreeMap<u32, Vec<HistoricTransaction>> =
BTreeMap::new();
for txn in chunk.history {
txns_per_block
.entry(txn.block_number)
.or_default()
.push(txn.clone());
}
log::debug!("Invalid chunk contents: ");
for (bn, hist_txs) in txns_per_block {
debug!(block = bn, num_transactions = hist_txs.len());
}
// Remove the peer from the syncing process
self.validity_queue.remove_peer(&peer_id);
self.syncing_peers.remove(&peer_id);
// Disconnect and ban the peer
self.disconnect_peer(peer_id, CloseReason::MaliciousPeer);
// Re add the request to the sync queue
self.validity_queue.add_ids(vec![(request, None)]);
break;
}
let blockchain = match &self.blockchain {
BlockchainProxy::Full(blockchain) => blockchain,
BlockchainProxy::Light(_) => unreachable!(),
};
let mut epoch_complete = {
// We need to check if there were previous items that belong to this chunk
let prev_items = peer_request.last_chunk_items.take().unwrap_or(0);
// First we calculate the beginning of the chunk that we want to apply
let starting_index = match prev_items.cmp(&chunk.history.len()) {
std::cmp::Ordering::Less => {
// If the chunk has new history items, we need to apply the delta
prev_items
}
std::cmp::Ordering::Equal => {
// If we recieved the same chunk (i.e nothing changed) we don't need to re-apply it
// So we set the start as the chunk length to apply an empty slice.
chunk.history.len()
}
std::cmp::Ordering::Greater => 0,
};
let history_root = Blockchain::extend_validity_sync(
blockchain.upgradable_read(),
&chunk.history[starting_index..],
);
history_root == expected_root
};
// Get ready for requesting the next chunk
let mut chunk_index = peer_request.chunk_index + 1;
// We need to check the latest macro head.
let (latest_macro_head_number, latest_history_root) = {
let blockchain = self.blockchain.read();
let macro_head = blockchain.macro_head();
(
macro_head.block_number(),
macro_head.header.history_root.clone(),
)
};
if latest_macro_head_number > verifier_block_number {
// A new macro head was adopted.
// TODO: We could keep track of the latest macro heads on a per peer basis
// because not all peers have the latest state.
if Policy::epoch_at(verifier_block_number)
< Policy::epoch_at(latest_macro_head_number)
{
if !peer_request.election_in_window {
// If the new macro head belongs to the next epoch, we still need to finish syncing the current epoch.
log::debug!(
new_macro_head = latest_macro_head_number,
new_epoch = Policy::epoch_at(latest_macro_head_number),
"We have a new macro head that belongs to the next epoch"
);
peer_request.election_in_window = true;
}
} else {
log::debug!(new_macro_head=latest_macro_head_number, new_history_root=%latest_history_root, current_epoch = Policy::epoch_at(latest_macro_head_number), "We have a new macro head, updating the validity sync target for our current epoch");
verifier_block_number = latest_macro_head_number;
peer_request.root_hash = latest_history_root.clone();
peer_request.verifier_block_number = latest_macro_head_number;
// We re-request the same chunk because applying a new macro head could potentially change the number of chunk items.
chunk_index = peer_request.chunk_index;
peer_request.last_chunk_items = Some(chunk.history.len());
// We are no longer complete
epoch_complete = false;
}
}
if epoch_complete {
// We need to check if there was an election in between, if so, we need to proceed to the next epoch
if peer_request.election_in_window {
log::trace!(
current_epoch = Policy::epoch_at(verifier_block_number),
new_verifier_bn = latest_macro_head_number,
new_expected_root = %latest_history_root,
next_epoch = Policy::epoch_at(latest_macro_head_number),
"Moving to the next epoch to continue validity syncing",
);
// Move to the next epoch:
// Note, when we move to the next epoch, we always select the latest macro head as our target
verifier_block_number = latest_macro_head_number;
chunk_index = 0;
peer_request.election_in_window = false;
peer_request.root_hash = latest_history_root.clone();
peer_request.verifier_block_number = latest_macro_head_number;
} else {
// We are done
log::info!(
synced_root = %expected_root,
synced_macro_head = verifier_block_number,
"Validity window syncing is complete"
);
self.synced_validity_peers.push(peer_id);
self.validity_queue.remove_peer(&peer_id);
self.syncing_peers.remove(&peer_id);
// We move all the peers from the sync queue to the synced peers.
for peer_id in self.syncing_peers.iter() {
self.synced_validity_peers.push(*peer_id);
self.validity_queue.remove_peer(peer_id);
}
// We are complete so we emit the peer
self.validity_requests = None;
self.syncing_peers.clear();
break;
}
}
// Update the peer tracker structure
peer_request.chunk_index = chunk_index;
let request = RequestHistoryChunk {
epoch_number: Policy::epoch_at(verifier_block_number),
block_number: verifier_block_number,
chunk_index: chunk_index as u64,
};
log::trace!(
verifier_bn = verifier_block_number,
epoch_number = Policy::epoch_at(verifier_block_number),
chunk_index = chunk_index,
"Adding a new validity window chunk request"
);
self.validity_queue.add_ids(vec![(request, None)]);
}
Err(err) => {
if request.epoch_number == 0 {
// A proof cannot be produced for epoch 0, so we emit the peer
return Poll::Ready(Some(MacroSyncReturn::Good(peer_id)));
}
{
log::warn!(%err, %peer_id, chunk_index=request.chunk_index,block_number=request.block_number,"The peer could not provide the requested history chunk, we emit it as outdated");
// Remove the peer from the syncing process
self.validity_queue.remove_peer(&peer_id);
self.syncing_peers.remove(&peer_id);
// Re add the request to the sync queue
self.validity_queue.add_ids(vec![(request, None)]);
return Poll::Ready(Some(MacroSyncReturn::Outdated(peer_id)));
}
}
}
}
Poll::Pending
}
}
#[cfg(test)]
mod tests {
use std::{
sync::Arc,
task::{Context, Poll},
};
use futures::{future, task::noop_waker_ref, FutureExt};
use nimiq_blockchain::{Blockchain, BlockchainConfig};
use nimiq_blockchain_interface::AbstractBlockchain;
use nimiq_blockchain_proxy::BlockchainProxy;
use nimiq_database::mdbx::MdbxDatabase;
use nimiq_network_interface::{
network::Network,
request::{OutboundRequestError, RequestError},
};
use nimiq_network_mock::{MockHub, MockNetwork};
use nimiq_primitives::{networks::NetworkId, policy::Policy};
use nimiq_test_log::test;
use nimiq_utils::time::OffsetTime;
use parking_lot::RwLock;
use crate::{
messages::RequestHistoryChunk,
sync::{
light::{sync::ValidityChunkRequest, LightMacroSync},
peer_list::PeerList,
sync_queue::SyncQueue,
},
};
fn blockchain() -> BlockchainProxy {
let time = Arc::new(OffsetTime::new());
let env = MdbxDatabase::new_volatile(Default::default()).unwrap();
BlockchainProxy::Full(Arc::new(RwLock::new(
Blockchain::new(
env,
BlockchainConfig::default(),
NetworkId::UnitAlbatross,
time,
)
.unwrap(),
)))
}
#[test(tokio::test)]
async fn it_resets_validity_sync_state_after_retry_exhaustion() {
let blockchain = blockchain();
let mut hub = MockHub::default();
let network = Arc::new(hub.new_network());
let mut sync = LightMacroSync::<MockNetwork>::new(
blockchain.clone(),
Arc::clone(&network),
network.subscribe_events(),
0,
None,
);
let peer_id = network.peer_id();
let macro_head = blockchain.read().macro_head().header.clone();
let request = RequestHistoryChunk {
epoch_number: Policy::epoch_at(macro_head.block_number),
block_number: macro_head.block_number,
chunk_index: 0,
};
sync.validity_queue = SyncQueue::new(
Arc::clone(&network),
vec![(request.clone(), None)],
Arc::new(RwLock::new(PeerList::default())),
1,
|_, _, _| {
future::ready(Err(RequestError::OutboundRequest(
OutboundRequestError::NoResponse,
)))
.boxed()
},
);
sync.validity_queue.add_peer(peer_id);
sync.validity_requests = Some(ValidityChunkRequest {
verifier_block_number: request.block_number,
root_hash: macro_head.history_root,
chunk_index: request.chunk_index as u32,
election_in_window: false,
last_chunk_items: None,
});
sync.syncing_peers.insert(peer_id);
let poll_result =
sync.poll_validity_window_chunks(&mut Context::from_waker(noop_waker_ref()));
assert!(matches!(poll_result, Poll::Pending));
assert!(sync.validity_requests.is_none());
assert!(sync.syncing_peers.is_empty());
assert_eq!(sync.validity_queue.num_peers(), 0);
assert!(sync.validity_queue.is_empty());
sync.start_validity_synchronization(peer_id);
let resumed_request = sync
.validity_requests
.as_ref()
.expect("validity sync should be restartable after reset");
assert_eq!(resumed_request.chunk_index, 0);
assert!(sync.syncing_peers.contains(&peer_id));
}
}