cfxcore/sync/
synchronization_service.rs1use super::{
6 Error, SharedSynchronizationGraph, SynchronizationProtocolHandler,
7};
8use crate::{
9 light_protocol::Provider as LightProvider,
10 sync::{
11 request_manager::RequestManager, synchronization_phases::SyncPhaseType,
12 synchronization_protocol_handler::ProtocolConfiguration,
13 StateSyncConfiguration, SynchronizationPhaseTrait,
14 },
15 ConsensusGraph, NodeType,
16};
17use cfx_types::H256;
18use malloc_size_of_derive::MallocSizeOf as DeriveMallocSizeOf;
19use network::{NetworkService, ProtocolId};
20use primitives::{transaction::SignedTransaction, Block};
21use std::sync::Arc;
22
23#[derive(DeriveMallocSizeOf)]
24pub struct SynchronizationService {
25 #[ignore_malloc_size_of = "channels are not handled in MallocSizeOf"]
26 pub network: Arc<NetworkService>,
27 protocol_handler: Arc<SynchronizationProtocolHandler>,
28 #[ignore_malloc_size_of = "insignificant"]
29 protocol: ProtocolId,
30}
31
32impl SynchronizationService {
33 pub fn new(
34 node_type: NodeType, network: Arc<NetworkService>,
35 sync_graph: SharedSynchronizationGraph,
36 protocol_config: ProtocolConfiguration,
37 state_sync_config: StateSyncConfiguration,
38 initial_sync_phase: SyncPhaseType, light_provider: Arc<LightProvider>,
39 consensus: Arc<ConsensusGraph>,
40 ) -> Self {
41 let sync_handler = Arc::new(SynchronizationProtocolHandler::new(
42 node_type,
43 protocol_config,
44 state_sync_config,
45 initial_sync_phase,
46 sync_graph.clone(),
47 light_provider,
48 consensus,
49 ));
50
51 assert_eq!(sync_handler.is_consortium(), sync_graph.is_consortium());
52
53 SynchronizationService {
54 network,
55 protocol_handler: sync_handler,
56 protocol: *b"cfx",
57 }
58 }
59
60 pub fn catch_up_mode(&self) -> bool {
61 self.protocol_handler.catch_up_mode()
62 }
63
64 pub fn get_synchronization_graph(&self) -> SharedSynchronizationGraph {
65 self.protocol_handler.get_synchronization_graph()
66 }
67
68 pub fn get_request_manager(&self) -> Arc<RequestManager> {
69 self.protocol_handler.get_request_manager()
70 }
71
72 pub fn current_sync_phase(&self) -> Arc<dyn SynchronizationPhaseTrait> {
73 self.protocol_handler.phase_manager.get_current_phase()
74 }
75
76 pub fn append_received_transactions(
77 &self, transactions: Vec<Arc<SignedTransaction>>,
78 ) {
79 self.protocol_handler
80 .append_received_transactions(transactions);
81 }
82
83 pub fn register(&self) -> Result<(), Error> {
84 self.network.register_protocol(
85 self.protocol_handler.clone(),
86 self.protocol,
87 self.protocol_handler.protocol_version,
88 )?;
89 Ok(())
90 }
91
92 fn relay_blocks(&self, need_to_relay: Vec<H256>) -> Result<(), Error> {
93 self.network.with_context(
94 self.protocol_handler.clone(),
95 self.protocol,
96 |io| self.protocol_handler.relay_blocks(io, need_to_relay),
97 )?
98 }
99
100 pub fn on_mined_block(&self, block: Block) -> Result<(), Error> {
101 let hash = block.hash();
102 self.protocol_handler.on_mined_block(block);
103 self.relay_blocks(vec![hash])
104 }
105
106 pub fn expire_block_gc(&self, timeout: u64) {
107 let _res = self.network.with_context(
108 self.protocol_handler.clone(),
109 self.protocol,
110 |io| self.protocol_handler.expire_block_gc(io, timeout),
111 );
112 }
113}
114
115pub type SharedSynchronizationService = Arc<SynchronizationService>;