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 parking_lot::{Condvar, Mutex};
8use secret_store::SecretStore;
9
10use jsonrpc_http_server::Server as HttpServer;
11use jsonrpc_tcp_server::Server as TcpServer;
12use jsonrpc_ws_server::Server as WsServer;
13
14use crate::{
15    common::{initialize_common_modules, ClientComponents},
16    configuration::Configuration,
17    rpc::{
18        extractor::RpcExtractor, impls::light::RpcImpl,
19        setup_debug_rpc_apis_light, setup_public_rpc_apis_light,
20    },
21};
22use blockgen::BlockGenerator;
23use cfxcore::{
24    pow::PowComputer, ConsensusGraph, LightQueryService, NodeType,
25    TransactionPool,
26};
27use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
28
29pub struct LightClientExtraComponents {
30    pub consensus: Arc<ConsensusGraph>,
31    pub debug_rpc_http_server: Option<HttpServer>,
32    pub debug_rpc_tcp_server: Option<TcpServer>,
33    pub debug_rpc_ws_server: Option<WsServer>,
34    pub light: Arc<LightQueryService>,
35    pub rpc_http_server: Option<HttpServer>,
36    pub rpc_tcp_server: Option<TcpServer>,
37    pub rpc_ws_server: Option<WsServer>,
38    pub secret_store: Arc<SecretStore>,
39    pub txpool: Arc<TransactionPool>,
40    pub pow: Arc<PowComputer>,
41}
42
43impl MallocSizeOf for LightClientExtraComponents {
44    fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize { unimplemented!() }
45}
46
47pub struct LightClient {}
48
49impl LightClient {
50    // Start all key components of Conflux and pass out their handles
51    pub fn start(
52        mut conf: Configuration, exit: Arc<(Mutex<bool>, Condvar)>,
53    ) -> Result<
54        Box<ClientComponents<BlockGenerator, LightClientExtraComponents>>,
55        String,
56    > {
57        let (
58            _machine,
59            secret_store,
60            _genesis_accounts,
61            data_man,
62            pow,
63            pos_verifier,
64            txpool,
65            consensus,
66            sync_graph,
67            network,
68            common_impl,
69            accounts,
70            notifications,
71            pubsub,
72            _tokio_runtime,
73        ) = initialize_common_modules(
74            &mut conf,
75            exit.clone(),
76            NodeType::Light,
77        )?;
78
79        let light = Arc::new(LightQueryService::new(
80            consensus.clone(),
81            sync_graph.clone(),
82            network.clone(),
83            conf.raw_conf.throttling_conf.clone(),
84            notifications,
85            conf.light_node_config(),
86        ));
87        light.register().unwrap();
88
89        sync_graph.recover_graph_from_db();
90
91        let rpc_impl = Arc::new(RpcImpl::new(
92            light.clone(),
93            accounts,
94            consensus.clone(),
95            data_man.clone(),
96        ));
97
98        let debug_rpc_http_server = crate::rpc::start_http(
99            conf.local_http_config(),
100            setup_debug_rpc_apis_light(
101                common_impl.clone(),
102                rpc_impl.clone(),
103                pubsub.clone(),
104                &conf,
105            ),
106        )?;
107
108        let debug_rpc_tcp_server = crate::rpc::start_tcp(
109            conf.local_tcp_config(),
110            setup_debug_rpc_apis_light(
111                common_impl.clone(),
112                rpc_impl.clone(),
113                pubsub.clone(),
114                &conf,
115            ),
116            RpcExtractor,
117        )?;
118
119        let rpc_tcp_server = crate::rpc::start_tcp(
120            conf.tcp_config(),
121            setup_public_rpc_apis_light(
122                common_impl.clone(),
123                rpc_impl.clone(),
124                pubsub.clone(),
125                &conf,
126            ),
127            RpcExtractor,
128        )?;
129
130        let debug_rpc_ws_server = crate::rpc::start_ws(
131            conf.local_ws_config(),
132            setup_public_rpc_apis_light(
133                common_impl.clone(),
134                rpc_impl.clone(),
135                pubsub.clone(),
136                &conf,
137            ),
138            RpcExtractor,
139        )?;
140
141        let rpc_ws_server = crate::rpc::start_ws(
142            conf.ws_config(),
143            setup_public_rpc_apis_light(
144                common_impl.clone(),
145                rpc_impl.clone(),
146                pubsub.clone(),
147                &conf,
148            ),
149            RpcExtractor,
150        )?;
151
152        let rpc_http_server = crate::rpc::start_http(
153            conf.http_config(),
154            setup_public_rpc_apis_light(
155                common_impl,
156                rpc_impl,
157                pubsub.clone(),
158                &conf,
159            ),
160        )?;
161
162        network.start();
163
164        Ok(Box::new(ClientComponents {
165            data_manager_weak_ptr: Arc::downgrade(&data_man),
166            blockgen: None,
167            pos_handler: Some(pos_verifier),
168            other_components: LightClientExtraComponents {
169                consensus,
170                debug_rpc_http_server,
171                debug_rpc_tcp_server,
172                debug_rpc_ws_server,
173                light,
174                rpc_http_server,
175                rpc_tcp_server,
176                rpc_ws_server,
177                secret_store,
178                txpool,
179                pow,
180            },
181        }))
182    }
183}