Skip to content

Commit 80e6282

Browse files
remagpieforiequal0
authored andcommitted
Refactor rpc startup code
1 parent fc6a3de commit 80e6282

File tree

3 files changed

+64
-76
lines changed

3 files changed

+64
-76
lines changed

codechain/rpc.rs

Lines changed: 25 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

1717
use std::io;
18-
use std::net::SocketAddr;
1918

19+
use crate::config::Config;
2020
use crate::rpc_apis;
2121
use crpc::{
2222
jsonrpc_core, start_http, start_ipc, start_ws, HttpServer, IpcServer, MetaIoHandler, Middleware, WsError, WsServer,
@@ -33,38 +33,27 @@ pub struct RpcHttpConfig {
3333
}
3434

3535
pub fn rpc_http_start(
36-
cfg: RpcHttpConfig,
37-
enable_devel_api: bool,
38-
deps: &rpc_apis::ApiDependencies,
36+
server: MetaIoHandler<(), impl Middleware<()>>,
37+
config: RpcHttpConfig,
3938
) -> Result<HttpServer, String> {
40-
let url = format!("{}:{}", cfg.interface, cfg.port);
39+
let url = format!("{}:{}", config.interface, config.port);
4140
let addr = url.parse().map_err(|_| format!("Invalid JSONRPC listen host/port given: {}", url))?;
42-
let server = setup_http_rpc_server(&addr, cfg.cors.clone(), cfg.hosts.clone(), enable_devel_api, deps)?;
43-
cinfo!(RPC, "RPC Listening on {}", url);
44-
if let Some(hosts) = cfg.hosts {
45-
cinfo!(RPC, "Allowed hosts are {:?}", hosts);
46-
}
47-
if let Some(cors) = cfg.cors {
48-
cinfo!(RPC, "CORS domains are {:?}", cors);
49-
}
50-
Ok(server)
51-
}
52-
53-
fn setup_http_rpc_server(
54-
url: &SocketAddr,
55-
cors_domains: Option<Vec<String>>,
56-
allowed_hosts: Option<Vec<String>>,
57-
enable_devel_api: bool,
58-
deps: &rpc_apis::ApiDependencies,
59-
) -> Result<HttpServer, String> {
60-
let server = setup_rpc_server(enable_devel_api, deps);
61-
let start_result = start_http(url, cors_domains, allowed_hosts, server);
41+
let start_result = start_http(&addr, config.cors.clone(), config.hosts.clone(), server);
6242
match start_result {
6343
Err(ref err) if err.kind() == io::ErrorKind::AddrInUse => {
6444
Err(format!("RPC address {} is already in use, make sure that another instance of a CodeChain node is not running or change the address using the --jsonrpc-port option.", url))
6545
},
6646
Err(e) => Err(format!("RPC error: {:?}", e)),
67-
Ok(server) => Ok(server),
47+
Ok(server) => {
48+
cinfo!(RPC, "RPC Listening on {}", url);
49+
if let Some(hosts) = config.hosts {
50+
cinfo!(RPC, "Allowed hosts are {:?}", hosts);
51+
}
52+
if let Some(cors) = config.cors {
53+
cinfo!(RPC, "CORS domains are {:?}", cors);
54+
}
55+
Ok(server)
56+
},
6857
}
6958
}
7059

@@ -74,19 +63,17 @@ pub struct RpcIpcConfig {
7463
}
7564

7665
pub fn rpc_ipc_start(
77-
cfg: &RpcIpcConfig,
78-
enable_devel_api: bool,
79-
deps: &rpc_apis::ApiDependencies,
66+
server: MetaIoHandler<(), impl Middleware<()>>,
67+
config: RpcIpcConfig,
8068
) -> Result<IpcServer, String> {
81-
let server = setup_rpc_server(enable_devel_api, deps);
82-
let start_result = start_ipc(&cfg.socket_addr, server);
69+
let start_result = start_ipc(&config.socket_addr, server);
8370
match start_result {
8471
Err(ref err) if err.kind() == io::ErrorKind::AddrInUse => {
85-
Err(format!("IPC address {} is already in use, make sure that another instance of a Codechain node is not running or change the address using the --ipc-path options.", cfg.socket_addr))
72+
Err(format!("IPC address {} is already in use, make sure that another instance of a Codechain node is not running or change the address using the --ipc-path options.", config.socket_addr))
8673
},
8774
Err(e) => Err(format!("IPC error: {:?}", e)),
8875
Ok(server) => {
89-
cinfo!(RPC, "IPC Listening on {}", cfg.socket_addr);
76+
cinfo!(RPC, "IPC Listening on {}", config.socket_addr);
9077
Ok(server)
9178
},
9279
}
@@ -99,15 +86,10 @@ pub struct RpcWsConfig {
9986
pub max_connections: usize,
10087
}
10188

102-
pub fn rpc_ws_start(
103-
cfg: &RpcWsConfig,
104-
enable_devel_api: bool,
105-
deps: &rpc_apis::ApiDependencies,
106-
) -> Result<WsServer, String> {
107-
let server = setup_rpc_server(enable_devel_api, deps);
108-
let url = format!("{}:{}", cfg.interface, cfg.port);
89+
pub fn rpc_ws_start(server: MetaIoHandler<(), impl Middleware<()>>, config: RpcWsConfig) -> Result<WsServer, String> {
90+
let url = format!("{}:{}", config.interface, config.port);
10991
let addr = url.parse().map_err(|_| format!("Invalid WebSockets listen host/port given: {}", url))?;
110-
let start_result = start_ws(&addr, server, cfg.max_connections);
92+
let start_result = start_ws(&addr, server, config.max_connections);
11193
match start_result {
11294
Err(WsError::Io(ref err)) if err.kind() == io::ErrorKind::AddrInUse => {
11395
Err(format!("WebSockets address {} is already in use, make sure that another instance of a Codechain node is not running or change the address using the --ws-port options.", addr))
@@ -120,12 +102,9 @@ pub fn rpc_ws_start(
120102
}
121103
}
122104

123-
fn setup_rpc_server(
124-
enable_devel_api: bool,
125-
deps: &rpc_apis::ApiDependencies,
126-
) -> MetaIoHandler<(), impl Middleware<()>> {
105+
pub fn setup_rpc_server(config: &Config, deps: &rpc_apis::ApiDependencies) -> MetaIoHandler<(), impl Middleware<()>> {
127106
let mut handler = MetaIoHandler::with_middleware(LogMiddleware::new());
128-
deps.extend_api(enable_devel_api, &mut handler);
107+
deps.extend_api(config, &mut handler);
129108
rpc_apis::setup_rpc(handler)
130109
}
131110

codechain/rpc_apis.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ use cnetwork::{EventSender, NetworkControl};
2222
use crpc::{MetaIoHandler, Middleware, Params, Value};
2323
use csync::BlockSyncEvent;
2424

25+
use crate::config::Config;
26+
2527
pub struct ApiDependencies {
2628
pub client: Arc<Client>,
2729
pub miner: Arc<Miner>,
@@ -31,11 +33,11 @@ pub struct ApiDependencies {
3133
}
3234

3335
impl ApiDependencies {
34-
pub fn extend_api(&self, enable_devel_api: bool, handler: &mut MetaIoHandler<(), impl Middleware<()>>) {
36+
pub fn extend_api(&self, config: &Config, handler: &mut MetaIoHandler<(), impl Middleware<()>>) {
3537
use crpc::v1::*;
3638
handler.extend_with(ChainClient::new(Arc::clone(&self.client)).to_delegate());
3739
handler.extend_with(MempoolClient::new(Arc::clone(&self.client)).to_delegate());
38-
if enable_devel_api {
40+
if config.rpc.enable_devel_api {
3941
handler.extend_with(
4042
DevelClient::new(Arc::clone(&self.client), Arc::clone(&self.miner), self.block_sync.clone())
4143
.to_delegate(),

codechain/run_node.rs

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use crate::config::{self, load_config};
4242
use crate::constants::{DEFAULT_DB_PATH, DEFAULT_KEYS_PATH};
4343
use crate::dummy_network_service::DummyNetworkService;
4444
use crate::json::PasswordFile;
45-
use crate::rpc::{rpc_http_start, rpc_ipc_start, rpc_ws_start};
45+
use crate::rpc::{rpc_http_start, rpc_ipc_start, rpc_ws_start, setup_rpc_server};
4646
use crate::rpc_apis::ApiDependencies;
4747

4848
fn network_start(
@@ -316,36 +316,43 @@ pub fn run_node(matches: &ArgMatches) -> Result<(), String> {
316316
}
317317
};
318318

319-
let rpc_apis_deps = ApiDependencies {
320-
client: client.client(),
321-
miner: Arc::clone(&miner),
322-
network_control: Arc::clone(&network_service),
323-
account_provider: ap,
324-
block_sync: maybe_sync_sender,
325-
};
319+
let (rpc_server, ipc_server, ws_server) = {
320+
let rpc_apis_deps = ApiDependencies {
321+
client: client.client(),
322+
miner: Arc::clone(&miner),
323+
network_control: Arc::clone(&network_service),
324+
account_provider: ap,
325+
block_sync: maybe_sync_sender,
326+
};
327+
328+
let rpc_server = {
329+
if !config.rpc.disable.unwrap() {
330+
let server = setup_rpc_server(&config, &rpc_apis_deps);
331+
Some(rpc_http_start(server, config.rpc_http_config())?)
332+
} else {
333+
None
334+
}
335+
};
326336

327-
let rpc_server = {
328-
if !config.rpc.disable.unwrap() {
329-
Some(rpc_http_start(config.rpc_http_config(), config.rpc.enable_devel_api, &rpc_apis_deps)?)
330-
} else {
331-
None
332-
}
333-
};
337+
let ipc_server = {
338+
if !config.ipc.disable.unwrap() {
339+
let server = setup_rpc_server(&config, &rpc_apis_deps);
340+
Some(rpc_ipc_start(server, config.rpc_ipc_config())?)
341+
} else {
342+
None
343+
}
344+
};
334345

335-
let ipc_server = {
336-
if !config.ipc.disable.unwrap() {
337-
Some(rpc_ipc_start(&config.rpc_ipc_config(), config.rpc.enable_devel_api, &rpc_apis_deps)?)
338-
} else {
339-
None
340-
}
341-
};
346+
let ws_server = {
347+
if !config.ws.disable.unwrap() {
348+
let server = setup_rpc_server(&config, &rpc_apis_deps);
349+
Some(rpc_ws_start(server, config.rpc_ws_config())?)
350+
} else {
351+
None
352+
}
353+
};
342354

343-
let ws_server = {
344-
if !config.ws.disable.unwrap() {
345-
Some(rpc_ws_start(&config.rpc_ws_config(), config.rpc.enable_devel_api, &rpc_apis_deps)?)
346-
} else {
347-
None
348-
}
355+
(rpc_server, ipc_server, ws_server)
349356
};
350357

351358
if (!config.stratum.disable.unwrap()) && (miner.engine_type() == EngineType::PoW) {

0 commit comments

Comments
 (0)