forked from nervosnetwork/ckb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.rs
108 lines (97 loc) · 3.07 KB
/
server.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
use crate::config::Config;
use crate::module::{
ChainRpc, ChainRpcImpl, IntegrationTestRpc, IntegrationTestRpcImpl, MinerRpc, MinerRpcImpl,
NetworkRpc, NetworkRpcImpl, PoolRpc, PoolRpcImpl, TraceRpc, TraceRpcImpl,
};
use ckb_chain::chain::ChainController;
use ckb_miner::BlockAssemblerController;
use ckb_network::NetworkController;
use ckb_shared::shared::Shared;
use ckb_shared::store::ChainStore;
use jsonrpc_core::IoHandler;
use jsonrpc_http_server::{Server, ServerBuilder};
use jsonrpc_server_utils::cors::AccessControlAllowOrigin;
use jsonrpc_server_utils::hosts::DomainsValidation;
pub struct RpcServer {
server: Server,
}
impl RpcServer {
pub fn new<CS: ChainStore + 'static>(
config: Config,
network_controller: NetworkController,
shared: Shared<CS>,
chain: ChainController,
block_assembler: BlockAssemblerController,
) -> RpcServer
where
CS: ChainStore,
{
let mut io = IoHandler::new();
if config.chain_enable() {
io.extend_with(
ChainRpcImpl {
shared: shared.clone(),
}
.to_delegate(),
);
}
if config.pool_enable() {
io.extend_with(
PoolRpcImpl {
network_controller: network_controller.clone(),
shared: shared.clone(),
}
.to_delegate(),
);
}
if config.miner_enable() {
io.extend_with(
MinerRpcImpl {
shared: shared.clone(),
block_assembler,
chain,
network_controller: network_controller.clone(),
}
.to_delegate(),
);
}
if config.net_enable() {
io.extend_with(
NetworkRpcImpl {
network_controller: network_controller.clone(),
}
.to_delegate(),
);
}
if config.trace_enable() {
io.extend_with(
TraceRpcImpl {
network_controller: network_controller.clone(),
shared,
}
.to_delegate(),
);
}
if config.integration_test_enable() {
io.extend_with(IntegrationTestRpcImpl { network_controller }.to_delegate());
}
let server = ServerBuilder::new(io)
.cors(DomainsValidation::AllowOnly(vec![
AccessControlAllowOrigin::Null,
AccessControlAllowOrigin::Any,
]))
.threads(config.threads.unwrap_or_else(num_cpus::get))
.max_request_body_size(config.max_request_body_size)
.start_http(
&config
.listen_address
.parse()
.expect("config listen_address parsed"),
)
.expect("Jsonrpc initialize");
RpcServer { server }
}
pub fn close(self) {
self.server.close()
}
}