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
//! The `syncer_proxy` module provides an abstraction layer over multiple syncing mechanisms
//! used in Nimiq nodes. Each variant corresponds to a different type of node configuration.
//!
//! - `History`: History node syncs all the historical data.
//! - `Full`: Full node focused on state sync.
//! - `Light`: Light node using macro light sync.
//! - `Pico`: Pico node using minimal sync logic, with fallback.
#[cfg(feature = "full")]
use std::cmp::max;
use std::{
mem,
pin::Pin,
sync::{atomic::AtomicU32, Arc},
task::{Context, Poll},
};
use futures::{Stream, StreamExt};
use nimiq_block::Block;
use nimiq_blockchain_interface::AbstractBlockchain;
use nimiq_blockchain_proxy::BlockchainProxy;
use nimiq_genesis::NetworkInfo;
use nimiq_light_blockchain::LightBlockchain;
use nimiq_network_interface::network::{Network, SubscribeEvents};
#[cfg(feature = "full")]
use nimiq_primitives::policy::Policy;
use parking_lot::Mutex;
use pin_project::pin_project;
use super::{
pico::PicoMacroSync,
sync_interface::{MacroSync, MacroSyncReturn},
};
#[cfg(feature = "full")]
use crate::sync::{
history::HistoryMacroSync,
live::{diff_queue::DiffQueue, state_queue::StateQueue, StateLiveSync},
};
use crate::{
consensus::ResolveBlockRequest,
sync::{
light::LightMacroSync,
live::{
block_queue::{BlockQueue, BlockSource},
queue::QueueConfig,
BlockLiveSync,
},
sync_interface::LiveSyncPushEvent,
syncer::Syncer,
},
BlsCache,
};
macro_rules! gen_syncer_match {
($self: ident, $f: ident $(, $arg:expr )*) => {
match $self {
#[cfg(feature = "full")]
SyncerProxy::History(syncer) => syncer.$f($( $arg ),*),
#[cfg(feature = "full")]
SyncerProxy::Full(syncer) => syncer.$f($( $arg ),*),
SyncerProxy::Light(syncer) => syncer.$f($( $arg ),*),
SyncerProxy::Pico(syncer) => syncer.$f($( $arg ),*),
}
};
}
#[pin_project(project = SyncerProxyProj)]
/// The `SyncerProxy` is an abstraction over multiple types of `Syncer`s.
pub enum SyncerProxy<N: Network> {
#[cfg(feature = "full")]
/// History Syncer, uses history macro sync for macro sync and block live sync.
History(Syncer<N, HistoryMacroSync<N>, BlockLiveSync<N>>),
#[cfg(feature = "full")]
/// Full Syncer, uses light macro sync for macro sync and state live sync.
Full(Syncer<N, LightMacroSync<N>, StateLiveSync<N>>),
/// Light Syncer, uses light macro sync for macro sync and block live sync.
Light(Syncer<N, LightMacroSync<N>, BlockLiveSync<N>>),
/// Pico Syncer, uses pico macro sync, light macro sync and block live sync
Pico(Syncer<N, EitherSyncer<N>, BlockLiveSync<N>>),
}
/// Represents one of two possible macro sync mechanisms: LightMacroSync or PicoMacroSync
/// only one of them can be active at any point in time.
#[pin_project(project = EitherSyncerProj)]
pub enum EitherSyncer<N: Network> {
Fallback(LightMacroSync<N>),
Normal(PicoMacroSync<N>),
}
impl<TNetwork: Network> MacroSync<TNetwork::PeerId> for EitherSyncer<TNetwork> {
const MAX_REQUEST_EPOCHS: u16 = 1000; // TODO: Use other value
fn add_peer(&mut self, peer_id: TNetwork::PeerId) {
match self {
EitherSyncer::Fallback(macro_sync) => macro_sync.add_peer(peer_id),
EitherSyncer::Normal(macro_sync) => macro_sync.add_peer(peer_id),
}
}
fn fallback(&mut self, peers: Vec<TNetwork::PeerId>) {
// This functionality is only implemented for switching from Pico to Light macro sync.
let EitherSyncer::Normal(pico) = self else {
return;
};
log::info!("Falling back to light macro sync");
// Get all the peers from the pico macro sync move them to light macro sync
let network = &pico.network;
let network_event_rx = network.subscribe_events();
let blockchain = &pico.blockchain;
match blockchain {
#[cfg(feature = "full")]
BlockchainProxy::Full(_) => {
unimplemented!()
}
BlockchainProxy::Light(light_blockchain) => {
let blockchain = light_blockchain.upgradable_read();
// Reset the blockchain state
log::info!("Resetting the light blockchain state");
LightBlockchain::reset_blockchain(blockchain);
}
}
// For the pico sync fallback, bootstrap from the hardcoded checkpoint (if the network has
// one) instead of re-syncing the whole election chain from genesis.
let checkpoint =
NetworkInfo::from_network_id(pico.blockchain.read().network_id()).checkpoint();
log::info!("Initializing Light Macro Sync");
let mut light_macro_sync = LightMacroSync::new(
pico.blockchain.clone(),
Arc::clone(&pico.network),
network_event_rx,
0, // Since the light sync does not keep state, we ignore the threshold.
checkpoint,
);
for peer_id in peers {
info!(%peer_id, "Adding peer into fallback macro sync");
light_macro_sync.add_peer(peer_id);
}
let _ = mem::replace(self, EitherSyncer::Fallback(light_macro_sync));
}
/// Removes all peers from the macro sync and returns their IDs.
fn drain_peers(&mut self) -> Vec<TNetwork::PeerId> {
match self {
EitherSyncer::Fallback(macro_sync) => macro_sync.drain_peers(),
EitherSyncer::Normal(macro_sync) => macro_sync.drain_peers(),
}
}
}
impl<TNetwork: Network> Stream for EitherSyncer<TNetwork> {
type Item = MacroSyncReturn<TNetwork::PeerId>;
/// Polls the underlying macro sync variant for the next macro sync result.
///
/// This delegates to the currently active macro sync implementation (either `LightMacroSync`
/// or `PicoMacroSync`), allowing `EitherSyncer` to behave as a single stream interface.
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
match self.project() {
EitherSyncerProj::Fallback(macro_sync) => macro_sync.poll_next_unpin(cx),
EitherSyncerProj::Normal(macro_sync) => macro_sync.poll_next_unpin(cx),
}
}
}
impl<N: Network> SyncerProxy<N> {
#[cfg(feature = "full")]
/// Creates a new instance of a `SyncerProxy` for the `History` variant
pub async fn new_history(
blockchain_proxy: BlockchainProxy,
network: Arc<N>,
bls_cache: Arc<Mutex<BlsCache>>,
network_event_rx: SubscribeEvents<N::PeerId>,
) -> Self {
assert!(
matches!(blockchain_proxy, BlockchainProxy::Full(_)),
"History Syncer can only be created for a full blockchain"
);
// Extract the full blockchain from the proxy.
let blockchain = match &blockchain_proxy {
BlockchainProxy::Full(blockchain) => Arc::clone(blockchain),
BlockchainProxy::Light(_) => unreachable!(),
};
// Initialize the block queue for pending blocks to be processed during sync
let block_queue = BlockQueue::new(
Arc::clone(&network),
blockchain_proxy.clone(),
QueueConfig::default(),
)
.await;
// Set up the live sync component using the block queue
let live_sync = BlockLiveSync::with_queue(
blockchain_proxy.clone(),
Arc::clone(&network),
block_queue,
bls_cache,
);
// Set up the macro sync component for historical sync.
let macro_sync = HistoryMacroSync::new(blockchain, Arc::clone(&network), network_event_rx);
Self::History(Syncer::new(
blockchain_proxy,
network,
live_sync,
macro_sync,
))
}
#[cfg(feature = "full")]
/// Creates a new instance of a `SyncerProxy` for the `Full` variant
pub async fn new_full(
blockchain_proxy: BlockchainProxy,
network: Arc<N>,
bls_cache: Arc<Mutex<BlsCache>>,
network_event_rx: SubscribeEvents<N::PeerId>,
full_sync_threshold: u32,
syncer_tracker: Arc<AtomicU32>,
) -> Self {
// Start from the default configuration for the block queue.
let mut queue_config = QueueConfig::default();
// Ensure the queue size is large enough for full sync, based on the given threshold.
let min_queue_size = full_sync_threshold + Policy::blocks_per_batch() * 2;
queue_config.window_ahead_max = max(min_queue_size, queue_config.window_ahead_max);
queue_config.buffer_max = max(min_queue_size as usize, queue_config.buffer_max);
// Create the block queue used to buffer and manage incoming blocks.
let block_queue = BlockQueue::new(
Arc::clone(&network),
blockchain_proxy.clone(),
queue_config.clone(),
)
.await;
// Extract the full blockchain reference from the proxy.
let blockchain = match &blockchain_proxy {
BlockchainProxy::Full(blockchain) => Arc::clone(blockchain),
BlockchainProxy::Light(_) => unreachable!(),
};
// Create the diff queue, which buffers block differences before applying state changes.
let diff_queue = DiffQueue::with_block_queue(Arc::clone(&network), block_queue);
// Create the state queue, which applies state updates based on the diff queue.
let state_queue = StateQueue::with_diff_queue(
Arc::clone(&network),
Arc::clone(&blockchain),
diff_queue,
queue_config,
syncer_tracker,
);
// Set up live sync with state queue.
let live_sync = StateLiveSync::with_queue(
blockchain_proxy.clone(),
Arc::clone(&network),
state_queue,
bls_cache,
);
// The task executor that is supplied for the light macro sync variant is tokio
// because the full sync is not supported in wasm
let macro_sync = LightMacroSync::new(
blockchain_proxy.clone(),
Arc::clone(&network),
network_event_rx,
full_sync_threshold,
None, // Full nodes do not bootstrap from a checkpoint.
);
// Return the Full node variant of the SyncerProxy.
Self::Full(Syncer::new(
blockchain_proxy,
network,
live_sync,
macro_sync,
))
}
/// Creates a new instance of a `SyncerProxy` for the `Light` variant
pub async fn new_light(
blockchain_proxy: BlockchainProxy,
network: Arc<N>,
bls_cache: Arc<Mutex<BlsCache>>,
network_event_rx: SubscribeEvents<N::PeerId>,
) -> Self {
// Light sync does not require full block bodies, only headers and metadata.
let block_queue_config = QueueConfig {
include_body: false,
..Default::default()
};
// Create a block queue to manage block announcements and requests.
let block_queue = BlockQueue::new(
Arc::clone(&network),
blockchain_proxy.clone(),
block_queue_config,
)
.await;
// Live sync is responsible for syncing micro blocks using the block queue.
let live_sync = BlockLiveSync::with_queue(
blockchain_proxy.clone(),
Arc::clone(&network),
block_queue,
bls_cache,
);
// Set up LightMacroSync, which syncs the light node to the latest macro block.
let macro_sync = LightMacroSync::new(
blockchain_proxy.clone(),
Arc::clone(&network),
network_event_rx,
0, // Since the light sync does not keep state, we ignore the threshold.
None, // Light nodes sync the election chain from genesis; they only verify the checkpoint.
);
// Return the constructed Light node variant of the SyncerProxy.
Self::Light(Syncer::new(
blockchain_proxy,
network,
live_sync,
macro_sync,
))
}
/// Creates a new instance of a `SyncerProxy` for the `Pico` variant
pub async fn new_pico(
blockchain_proxy: BlockchainProxy,
network: Arc<N>,
bls_cache: Arc<Mutex<BlsCache>>,
network_event_rx: SubscribeEvents<N::PeerId>,
) -> Self {
// Configure the block queue to exclude block bodies
let block_queue_config = QueueConfig {
include_body: false,
..Default::default()
};
// Set up a block queue to be used by the live sync process.
let block_queue = BlockQueue::new(
Arc::clone(&network),
blockchain_proxy.clone(),
block_queue_config,
)
.await;
// Create a live sync instance using the configured block queue.
let live_sync = BlockLiveSync::with_queue(
blockchain_proxy.clone(),
Arc::clone(&network),
block_queue,
bls_cache,
);
// Set up PicoMacroSync
let pico_macro_sync = PicoMacroSync::new(
blockchain_proxy.clone(),
Arc::clone(&network),
network_event_rx,
);
// We start with the Pico Macro sync mechanism; can later fall back to LightMacroSync
let normal_syncer = EitherSyncer::Normal(pico_macro_sync);
// Return the SyncerProxy in the Pico node variant.
Self::Pico(Syncer::new(
blockchain_proxy,
network,
live_sync,
normal_syncer,
))
}
/// Pushes a block for the live sync method
pub fn push_block(&mut self, block: Block, block_source: BlockSource<N>) {
gen_syncer_match!(self, push_block, block, block_source)
}
/// Returns the number of peers doing live synchronization
pub fn num_peers(&self) -> usize {
gen_syncer_match!(self, num_peers)
}
/// Returns the peers doing live synchronization
pub fn peers(&self) -> Vec<N::PeerId> {
gen_syncer_match!(self, peers)
}
/// Returns the number of accepted block announcements seen
pub fn accepted_block_announcements(&self) -> usize {
gen_syncer_match!(self, accepted_block_announcements)
}
/// Returns whether the state sync has finished (or `true` if there is no state sync required)
pub fn state_complete(&self) -> bool {
gen_syncer_match!(self, state_complete)
}
/// Triggers resolution of a block.
pub fn resolve_block(&mut self, request: ResolveBlockRequest<N>) {
gen_syncer_match!(self, resolve_block, request)
}
}
impl<N: Network> Stream for SyncerProxy<N> {
type Item = LiveSyncPushEvent;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
match self.project() {
#[cfg(feature = "full")]
SyncerProxyProj::History(syncer) => syncer.poll_next_unpin(cx),
#[cfg(feature = "full")]
SyncerProxyProj::Full(syncer) => syncer.poll_next_unpin(cx),
SyncerProxyProj::Light(syncer) => syncer.poll_next_unpin(cx),
SyncerProxyProj::Pico(syncer) => syncer.poll_next_unpin(cx),
}
}
}