-
Notifications
You must be signed in to change notification settings - Fork 201
/
synchronization_phases.rs
672 lines (597 loc) · 22.6 KB
/
synchronization_phases.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
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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
// Copyright 2019 Conflux Foundation. All rights reserved.
// Conflux is free software and distributed under GNU General Public License.
// See http://www.gnu.org/licenses/
use crate::{
block_data_manager::StateAvailabilityBoundary,
consensus::{ConsensusGraph, ConsensusGraphInner, ConsensusGraphTrait},
parameters::{consensus::NULL, sync::CATCH_UP_EPOCH_LAG_THRESHOLD},
sync::{
message::DynamicCapability,
state::{SnapshotChunkSync, Status},
synchronization_protocol_handler::SynchronizationProtocolHandler,
synchronization_state::SynchronizationState,
SharedSynchronizationGraph, SynchronizationGraphInner,
},
};
use network::NetworkContext;
use parking_lot::RwLock;
use std::{
collections::HashMap,
sync::{
atomic::{AtomicBool, Ordering as AtomicOrdering},
Arc,
},
thread, time,
};
///
/// Archive node goes through the following phases:
/// CatchUpRecoverBlockFromDB --> CatchUpSyncBlock --> Normal
///
/// Full node goes through the following phases:
/// CatchUpRecoverBlockHeaderFromDB --> CatchUpSyncBlockHeader -->
/// CatchUpCheckpoint --> CatchUpRecoverBlockFromDB -->
/// CatchUpSyncBlock --> Normal
#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub enum SyncPhaseType {
CatchUpRecoverBlockHeaderFromDB = 0,
CatchUpSyncBlockHeader = 1,
CatchUpCheckpoint = 2,
CatchUpRecoverBlockFromDB = 3,
CatchUpSyncBlock = 4,
Normal = 5,
}
pub trait SynchronizationPhaseTrait: Send + Sync {
fn name(&self) -> &'static str;
fn phase_type(&self) -> SyncPhaseType;
fn next(
&self, _io: &dyn NetworkContext,
_sync_handler: &SynchronizationProtocolHandler,
) -> SyncPhaseType;
fn start(
&self, _io: &dyn NetworkContext,
_sync_handler: &SynchronizationProtocolHandler,
);
}
pub struct SynchronizationPhaseManagerInner {
initialized: bool,
current_phase: SyncPhaseType,
phases: HashMap<SyncPhaseType, Arc<dyn SynchronizationPhaseTrait>>,
}
impl SynchronizationPhaseManagerInner {
pub fn new(initial_phase_type: SyncPhaseType) -> Self {
SynchronizationPhaseManagerInner {
initialized: false,
current_phase: initial_phase_type,
phases: HashMap::new(),
}
}
pub fn register_phase(
&mut self, phase: Arc<dyn SynchronizationPhaseTrait>,
) {
self.phases.insert(phase.phase_type(), phase);
}
pub fn get_phase(
&self, phase_type: SyncPhaseType,
) -> Arc<dyn SynchronizationPhaseTrait> {
self.phases.get(&phase_type).unwrap().clone()
}
pub fn get_current_phase(&self) -> Arc<dyn SynchronizationPhaseTrait> {
self.get_phase(self.current_phase)
}
pub fn change_phase_to(&mut self, phase_type: SyncPhaseType) {
self.current_phase = phase_type;
}
pub fn try_initialize(&mut self) -> bool {
let initialized = self.initialized;
if !self.initialized {
self.initialized = true;
}
initialized
}
}
pub struct SynchronizationPhaseManager {
inner: RwLock<SynchronizationPhaseManagerInner>,
}
impl SynchronizationPhaseManager {
pub fn new(
initial_phase_type: SyncPhaseType,
sync_state: Arc<SynchronizationState>,
sync_graph: SharedSynchronizationGraph,
state_sync: Arc<SnapshotChunkSync>,
) -> Self
{
let sync_manager = SynchronizationPhaseManager {
inner: RwLock::new(SynchronizationPhaseManagerInner::new(
initial_phase_type,
)),
};
sync_manager.register_phase(Arc::new(
CatchUpRecoverBlockHeaderFromDbPhase::new(sync_graph.clone()),
));
sync_manager.register_phase(Arc::new(
CatchUpSyncBlockHeaderPhase::new(
sync_state.clone(),
sync_graph.clone(),
),
));
sync_manager
.register_phase(Arc::new(CatchUpCheckpointPhase::new(state_sync)));
sync_manager.register_phase(Arc::new(
CatchUpRecoverBlockFromDbPhase::new(sync_graph.clone()),
));
sync_manager.register_phase(Arc::new(CatchUpSyncBlockPhase::new(
sync_state.clone(),
sync_graph.clone(),
)));
sync_manager.register_phase(Arc::new(NormalSyncPhase::new()));
sync_manager
}
pub fn register_phase(&self, phase: Arc<dyn SynchronizationPhaseTrait>) {
self.inner.write().register_phase(phase);
}
pub fn get_phase(
&self, phase_type: SyncPhaseType,
) -> Arc<dyn SynchronizationPhaseTrait> {
self.inner.read().get_phase(phase_type)
}
pub fn get_current_phase(&self) -> Arc<dyn SynchronizationPhaseTrait> {
self.inner.read().get_current_phase()
}
pub fn change_phase_to(
&self, phase_type: SyncPhaseType, io: &dyn NetworkContext,
sync_handler: &SynchronizationProtocolHandler,
)
{
self.inner.write().change_phase_to(phase_type);
let current_phase = self.get_current_phase();
current_phase.start(io, sync_handler);
}
pub fn try_initialize(
&self, io: &dyn NetworkContext,
sync_handler: &SynchronizationProtocolHandler,
)
{
if !self.inner.write().try_initialize() {
// if not initialized
let current_phase = self.get_current_phase();
current_phase.start(io, sync_handler);
}
}
}
pub struct CatchUpRecoverBlockHeaderFromDbPhase {
pub graph: SharedSynchronizationGraph,
pub recovered: Arc<AtomicBool>,
}
impl CatchUpRecoverBlockHeaderFromDbPhase {
pub fn new(graph: SharedSynchronizationGraph) -> Self {
CatchUpRecoverBlockHeaderFromDbPhase {
graph,
recovered: Arc::new(AtomicBool::new(false)),
}
}
}
impl SynchronizationPhaseTrait for CatchUpRecoverBlockHeaderFromDbPhase {
fn name(&self) -> &'static str { "CatchUpRecoverBlockHeaderFromDbPhase" }
fn phase_type(&self) -> SyncPhaseType {
SyncPhaseType::CatchUpRecoverBlockHeaderFromDB
}
fn next(
&self, io: &dyn NetworkContext,
sync_handler: &SynchronizationProtocolHandler,
) -> SyncPhaseType
{
if self.recovered.load(AtomicOrdering::SeqCst) == false {
return self.phase_type();
}
DynamicCapability::ServeHeaders(true).broadcast(io, &sync_handler.syn);
SyncPhaseType::CatchUpSyncBlockHeader
}
fn start(
&self, _io: &dyn NetworkContext,
_sync_handler: &SynchronizationProtocolHandler,
)
{
info!("start phase {:?}", self.name());
self.recovered.store(false, AtomicOrdering::SeqCst);
let recovered = self.recovered.clone();
let graph = self.graph.clone();
std::thread::spawn(move || {
graph.recover_graph_from_db(true /* header_only */);
recovered.store(true, AtomicOrdering::SeqCst);
info!("finish recover header graph from db");
});
}
}
pub struct CatchUpSyncBlockHeaderPhase {
pub syn: Arc<SynchronizationState>,
pub graph: SharedSynchronizationGraph,
}
impl CatchUpSyncBlockHeaderPhase {
pub fn new(
syn: Arc<SynchronizationState>, graph: SharedSynchronizationGraph,
) -> Self {
CatchUpSyncBlockHeaderPhase { syn, graph }
}
}
impl SynchronizationPhaseTrait for CatchUpSyncBlockHeaderPhase {
fn name(&self) -> &'static str { "CatchUpSyncBlockHeaderPhase" }
fn phase_type(&self) -> SyncPhaseType {
SyncPhaseType::CatchUpSyncBlockHeader
}
fn next(
&self, _io: &dyn NetworkContext,
_sync_handler: &SynchronizationProtocolHandler,
) -> SyncPhaseType
{
// FIXME: use target_height instead.
let middle_epoch = self.syn.get_middle_epoch();
if middle_epoch.is_none() {
return self.phase_type();
}
let middle_epoch = middle_epoch.unwrap();
// FIXME: OK, what if the chain height is close, or even local height is
// FIXME: larger, but the chain forked earlier very far away?
if self.graph.consensus.best_epoch_number()
+ CATCH_UP_EPOCH_LAG_THRESHOLD
>= middle_epoch
{
return SyncPhaseType::CatchUpCheckpoint;
}
self.phase_type()
}
fn start(
&self, io: &dyn NetworkContext,
sync_handler: &SynchronizationProtocolHandler,
)
{
info!("start phase {:?}", self.name());
let (_, cur_era_genesis_height) =
self.graph.get_genesis_hash_and_height_in_current_era();
*sync_handler.latest_epoch_requested.lock() = cur_era_genesis_height;
sync_handler.request_initial_missed_block(io);
sync_handler.request_epochs(io);
}
}
pub struct CatchUpCheckpointPhase {
state_sync: Arc<SnapshotChunkSync>,
/// Is `true` if we have the state locally and do not need to sync
/// checkpoints. Only set when the phase starts.
has_state: AtomicBool,
}
impl CatchUpCheckpointPhase {
pub fn new(state_sync: Arc<SnapshotChunkSync>) -> Self {
CatchUpCheckpointPhase {
state_sync,
has_state: AtomicBool::new(false),
}
}
}
impl SynchronizationPhaseTrait for CatchUpCheckpointPhase {
fn name(&self) -> &'static str { "CatchUpCheckpointPhase" }
fn phase_type(&self) -> SyncPhaseType { SyncPhaseType::CatchUpCheckpoint }
fn next(
&self, io: &dyn NetworkContext,
sync_handler: &SynchronizationProtocolHandler,
) -> SyncPhaseType
{
if self.has_state.load(AtomicOrdering::SeqCst) {
return SyncPhaseType::CatchUpRecoverBlockFromDB;
}
let epoch_to_sync = sync_handler.graph.consensus.get_to_sync_epoch_id();
let current_era_genesis = sync_handler
.graph
.data_man
.get_cur_consensus_era_genesis_hash();
self.state_sync.update_status(
current_era_genesis,
epoch_to_sync,
io,
sync_handler,
);
if self.state_sync.status() == Status::Completed {
self.state_sync.restore_execution_state(sync_handler);
*sync_handler.synced_epoch_id.lock() = Some(epoch_to_sync);
SyncPhaseType::CatchUpRecoverBlockFromDB
} else {
self.phase_type()
}
}
fn start(
&self, io: &dyn NetworkContext,
sync_handler: &SynchronizationProtocolHandler,
)
{
info!("start phase {:?}", self.name());
let current_era_genesis = sync_handler
.graph
.data_man
.get_cur_consensus_era_genesis_hash();
let epoch_to_sync = sync_handler.graph.consensus.get_to_sync_epoch_id();
if let Some(commitment) = sync_handler
.graph
.data_man
.get_epoch_execution_commitment_with_db(&epoch_to_sync)
{
info!("CatchUpCheckpointPhase: commitment for epoch {:?} exists, skip state sync. \
commitment={:?}", epoch_to_sync, commitment);
self.has_state.store(true, AtomicOrdering::SeqCst);
// TODO Here has_state could mean we have the snapshot of the state
// or the last snapshot and the delta mpt. We only need to specially
// handle the case of snapshot-only state where we
// cannot compute state_valid because we do not have a
// valid state root.
if epoch_to_sync != sync_handler.graph.data_man.true_genesis.hash()
{
*sync_handler.synced_epoch_id.lock() = Some(epoch_to_sync);
}
return;
}
self.state_sync.update_status(
current_era_genesis,
epoch_to_sync,
io,
sync_handler,
);
}
}
pub struct CatchUpRecoverBlockFromDbPhase {
pub graph: SharedSynchronizationGraph,
pub recovered: Arc<AtomicBool>,
}
impl CatchUpRecoverBlockFromDbPhase {
pub fn new(graph: SharedSynchronizationGraph) -> Self {
CatchUpRecoverBlockFromDbPhase {
graph,
recovered: Arc::new(AtomicBool::new(false)),
}
}
}
impl SynchronizationPhaseTrait for CatchUpRecoverBlockFromDbPhase {
fn name(&self) -> &'static str { "CatchUpRecoverBlockFromDbPhase" }
fn phase_type(&self) -> SyncPhaseType {
SyncPhaseType::CatchUpRecoverBlockFromDB
}
fn next(
&self, _io: &dyn NetworkContext,
_sync_handler: &SynchronizationProtocolHandler,
) -> SyncPhaseType
{
if self.recovered.load(AtomicOrdering::SeqCst) == false {
return self.phase_type();
}
SyncPhaseType::CatchUpSyncBlock
}
fn start(
&self, _io: &dyn NetworkContext,
sync_handler: &SynchronizationProtocolHandler,
)
{
info!("start phase {:?}", self.name());
{
// FIXME: consider alliance tree graph
let consensus = self
.graph
.consensus
.as_any()
.downcast_ref::<ConsensusGraph>()
.expect("downcast should succeed");
// Acquire the lock of synchronization graph first to make sure no
// more blocks will be inserted into synchronization graph.
let old_sync_inner = &mut *self.graph.inner.write();
// Wait until all the graph ready blocks in queue are inserted into
// consensus graph.
while *self.graph.latest_graph_ready_block.lock()
!= consensus.latest_inserted_block()
{
thread::sleep(time::Duration::from_millis(100));
}
// Now, we can safely acquire the lock of consensus graph.
let old_consensus_inner = &mut *consensus.inner.write();
// We should assign a new instance_id here since we construct a new
// consensus graph.
old_sync_inner.data_man.initialize_instance_id();
let (cur_era_genesis_hash, cur_era_genesis_height) =
old_sync_inner.get_genesis_hash_and_height_in_current_era();
let (cur_era_stable_hash, cur_era_stable_height) =
old_sync_inner.get_stable_hash_and_height_in_current_era();
debug!("Build new consensus graph for sync-recovery with identified genesis {} height {} stable block {} height {}", cur_era_genesis_hash, cur_era_genesis_height, cur_era_stable_hash, cur_era_stable_height);
// TODO: Make sure that the checkpoint will not change between the
// end of CatchUpCheckpointPhase and the start of
// CatchUpRecoverBlockFromDbPhase.
let new_consensus_inner = ConsensusGraphInner::with_era_genesis(
old_consensus_inner.pow_config.clone(),
self.graph.data_man.clone(),
old_consensus_inner.inner_conf.clone(),
&cur_era_genesis_hash,
&cur_era_stable_hash,
);
// For archive node, this will be `None`.
// For full node, this is `None` when the state of checkpoint is
// already in disk and we didn't sync it from peer.
// In both cases, we should set `state_availability_boundary` to
// `[cur_era_stable_height, cur_era_stable_height]`.
if let Some(epoch_synced) = &*sync_handler.synced_epoch_id.lock() {
// TODO Implement a mechanism to ensure the expect.
// We need `for_snapshot` because the blocks within the next
// snapshot do not have correct state_root
// locally.
let trusted_blame_block = old_consensus_inner
.get_trusted_blame_block_for_snapshot(&epoch_synced)
.expect(
"It's ensured that a trusted blame block can be found for \
a stable epoch_to_sync",
);
let epoch_synced_height = self
.graph
.data_man
.block_header_by_hash(epoch_synced)
.expect("Header for checkpoint exists")
.height();
*self.graph.data_man.state_availability_boundary.write() =
StateAvailabilityBoundary::new(
*epoch_synced,
epoch_synced_height,
);
self.graph
.data_man
.state_availability_boundary
.write()
.set_synced_state_height(epoch_synced_height);
// This map will be used to recover `state_valid` info for each
// pivot block before `trusted_blame_block`.
let mut pivot_block_state_valid_map =
consensus.pivot_block_state_valid_map.lock();
let mut cur = *old_consensus_inner
.hash_to_arena_indices
.get(&trusted_blame_block)
.unwrap();
while cur != NULL {
let blame = self
.graph
.data_man
.block_header_by_hash(
&old_consensus_inner.arena[cur].hash,
)
.unwrap()
.blame();
for i in 0..blame + 1 {
trace!("Backup state_valid: hash={:?} height={} state_valid={},", old_consensus_inner.arena[cur].hash, old_consensus_inner.arena[cur].height,
i == 0);
pivot_block_state_valid_map.insert(
old_consensus_inner.arena[cur].hash,
i == 0,
);
cur = old_consensus_inner.arena[cur].parent;
if cur == NULL {
break;
}
}
}
} else {
let cur_era_stable_hash =
self.graph.data_man.get_cur_consensus_era_stable_hash();
let cur_era_stable_height = self
.graph
.data_man
.block_header_by_hash(&cur_era_stable_hash)
.expect("stable era block header must exist")
.height();
*self.graph.data_man.state_availability_boundary.write() =
StateAvailabilityBoundary::new(
cur_era_stable_hash,
cur_era_stable_height,
);
}
*old_consensus_inner = new_consensus_inner;
// FIXME: We may need to some information of `confirmation_meter`.
consensus.confirmation_meter.clear();
let new_sync_inner = SynchronizationGraphInner::with_genesis_block(
self.graph
.data_man
.block_header_by_hash(&cur_era_genesis_hash)
.expect("era genesis exists"),
old_sync_inner.pow_config.clone(),
old_sync_inner.config.clone(),
old_sync_inner.data_man.clone(),
);
*old_sync_inner = new_sync_inner;
self.graph
.statistics
.clear_sync_and_consensus_graph_statistics();
}
self.graph.consensus.update_best_info();
self.graph
.consensus
.get_tx_pool()
.notify_new_best_info(self.graph.consensus.best_info())
// FIXME: propogate error.
.expect(&concat!(file!(), ":", line!(), ":", column!()));
self.recovered.store(false, AtomicOrdering::SeqCst);
let recovered = self.recovered.clone();
let graph = self.graph.clone();
std::thread::Builder::new()
.name("recover_blocks".into())
.spawn(move || {
graph.recover_graph_from_db(false /* header_only */);
recovered.store(true, AtomicOrdering::SeqCst);
info!("finish recover block graph from db");
})
.expect("Thread spawn failure");
}
}
pub struct CatchUpSyncBlockPhase {
pub syn: Arc<SynchronizationState>,
pub graph: SharedSynchronizationGraph,
}
impl CatchUpSyncBlockPhase {
pub fn new(
syn: Arc<SynchronizationState>, graph: SharedSynchronizationGraph,
) -> Self {
CatchUpSyncBlockPhase { syn, graph }
}
}
impl SynchronizationPhaseTrait for CatchUpSyncBlockPhase {
fn name(&self) -> &'static str { "CatchUpSyncBlockPhase" }
fn phase_type(&self) -> SyncPhaseType { SyncPhaseType::CatchUpSyncBlock }
fn next(
&self, _io: &dyn NetworkContext,
_sync_handler: &SynchronizationProtocolHandler,
) -> SyncPhaseType
{
// FIXME: use target_height instead.
let middle_epoch = self.syn.get_middle_epoch();
if middle_epoch.is_none() {
if self.syn.is_dev_or_test_mode() {
return SyncPhaseType::Normal;
} else {
return self.phase_type();
}
}
let middle_epoch = middle_epoch.unwrap();
// FIXME: OK, what if the chain height is close, or even local height is
// FIXME: larger, but the chain forked earlier very far away?
if self.graph.consensus.best_epoch_number()
+ CATCH_UP_EPOCH_LAG_THRESHOLD
>= middle_epoch
{
return SyncPhaseType::Normal;
}
self.phase_type()
}
fn start(
&self, io: &dyn NetworkContext,
sync_handler: &SynchronizationProtocolHandler,
)
{
info!("start phase {:?}", self.name());
let (_, cur_era_genesis_height) =
self.graph.get_genesis_hash_and_height_in_current_era();
*sync_handler.latest_epoch_requested.lock() = cur_era_genesis_height;
sync_handler.request_initial_missed_block(io);
sync_handler.request_epochs(io);
}
}
pub struct NormalSyncPhase {}
impl NormalSyncPhase {
pub fn new() -> Self { NormalSyncPhase {} }
}
impl SynchronizationPhaseTrait for NormalSyncPhase {
fn name(&self) -> &'static str { "NormalSyncPhase" }
fn phase_type(&self) -> SyncPhaseType { SyncPhaseType::Normal }
fn next(
&self, _io: &dyn NetworkContext,
_sync_handler: &SynchronizationProtocolHandler,
) -> SyncPhaseType
{
// FIXME: handle the case where we need to switch back phase
self.phase_type()
}
fn start(
&self, io: &dyn NetworkContext,
sync_handler: &SynchronizationProtocolHandler,
)
{
info!("start phase {:?}", self.name());
sync_handler.request_missing_terminals(io);
}
}