client/node_types/
light.rs

1// Copyright 2019 Conflux Foundation. All rights reserved.
2// Conflux is free software and distributed under GNU General Public License.
3// See http://www.gnu.org/licenses/
4
5use std::sync::Arc;
6
7use cfx_rpc_cfx_types::apis::ApiSet;
8use parking_lot::{Condvar, Mutex};
9use secret_store::SecretStore;
10use tokio::runtime::Runtime as TokioRuntime;
11
12use cfx_rpc_builder::RpcServerHandle;
13
14use crate::{
15    common::{initialize_common_modules, ClientComponents},
16    configuration::Configuration,
17    rpc_starter::launch_cfx_light_async_rpc_servers,
18};
19use blockgen::BlockGenerator;
20use cfx_tasks::TaskManager;
21use cfxcore::{
22    pow::PowComputer, ConsensusGraph, LightQueryService, NodeType,
23    TransactionPool,
24};
25use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
26
27pub struct LightClientExtraComponents {
28    pub consensus: Arc<ConsensusGraph>,
29    pub cfx_rpc_server_handle: Option<RpcServerHandle>,
30    pub debug_cfx_rpc_server_handle: Option<RpcServerHandle>,
31    pub light: Arc<LightQueryService>,
32    pub secret_store: Arc<SecretStore>,
33    pub txpool: Arc<TransactionPool>,
34    pub pow: Arc<PowComputer>,
35    /// Keep the tokio runtime alive for the lifetime of the node so that the
36    /// jsonrpsee server (and any other tasks spawned on this runtime) keeps
37    /// processing requests. Without this, the runtime would be dropped at the
38    /// end of `LightClient::start`, silently cancelling the RPC accept loop
39    /// and causing every RPC call (e.g. `cfx_getBestBlockHash`) to hang.
40    pub tokio_runtime: Arc<TokioRuntime>,
41    /// Keep the task manager alive so that tasks spawned via its executor
42    /// (e.g. async RPC handlers, pubsub) are not shut down prematurely.
43    pub task_manager: TaskManager,
44}
45
46impl MallocSizeOf for LightClientExtraComponents {
47    fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize { unimplemented!() }
48}
49
50pub struct LightClient {}
51
52impl LightClient {
53    // Start all key components of Conflux and pass out
54    // their handles
55    pub fn start(
56        mut conf: Configuration, exit: Arc<(Mutex<bool>, Condvar)>,
57    ) -> Result<
58        Box<ClientComponents<BlockGenerator, LightClientExtraComponents>>,
59        String,
60    > {
61        let (
62            _machine,
63            secret_store,
64            _genesis_accounts,
65            data_man,
66            pow,
67            pos_verifier,
68            txpool,
69            consensus,
70            sync_graph,
71            network,
72            accounts,
73            notifications,
74            tokio_runtime,
75        ) = initialize_common_modules(
76            &mut conf,
77            exit.clone(),
78            NodeType::Light,
79        )?;
80
81        let light = Arc::new(LightQueryService::new(
82            consensus.clone(),
83            sync_graph.clone(),
84            network.clone(),
85            conf.raw_conf.throttling_conf.clone(),
86            notifications.clone(),
87            conf.light_node_config(),
88        ));
89        light.register().unwrap();
90
91        sync_graph.recover_graph_from_db();
92
93        // Create task executor for async RPC
94        let task_manager = TaskManager::new(tokio_runtime.handle().clone());
95        let task_executor = task_manager.executor();
96
97        // Start the new jsonrpsee-based core space RPC
98        // servers for the light node.
99        let cfx_rpc_server_handle =
100            tokio_runtime.block_on(launch_cfx_light_async_rpc_servers(
101                consensus.clone(),
102                txpool.clone(),
103                data_man.clone(),
104                network.clone(),
105                pos_verifier.clone(),
106                accounts.clone(),
107                light.clone(),
108                exit.clone(),
109                task_executor.clone(),
110                notifications.clone(),
111                &conf,
112                conf.raw_conf.public_rpc_apis.clone(),
113                false,
114            ))?;
115
116        let debug_cfx_rpc_server_handle =
117            tokio_runtime.block_on(launch_cfx_light_async_rpc_servers(
118                consensus.clone(),
119                txpool.clone(),
120                data_man.clone(),
121                network.clone(),
122                pos_verifier.clone(),
123                accounts,
124                light.clone(),
125                exit,
126                task_executor,
127                notifications,
128                &conf,
129                ApiSet::All,
130                true,
131            ))?;
132
133        network.start();
134
135        Ok(Box::new(ClientComponents {
136            data_manager_weak_ptr: Arc::downgrade(&data_man),
137            blockgen: None,
138            pos_handler: Some(pos_verifier),
139            other_components: LightClientExtraComponents {
140                consensus,
141                cfx_rpc_server_handle,
142                debug_cfx_rpc_server_handle,
143                light,
144                secret_store,
145                txpool,
146                pow,
147                tokio_runtime,
148                task_manager,
149            },
150        }))
151    }
152}