2020-09-22 14:53:52 +02:00
|
|
|
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of OpenEthereum.
|
2016-02-05 13:40:41 +01:00
|
|
|
|
2020-09-22 14:53:52 +02:00
|
|
|
// OpenEthereum is free software: you can redistribute it and/or modify
|
2016-02-05 13:40:41 +01:00
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
2020-09-22 14:53:52 +02:00
|
|
|
// OpenEthereum is distributed in the hope that it will be useful,
|
2016-02-05 13:40:41 +01:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2020-09-22 14:53:52 +02:00
|
|
|
// along with OpenEthereum. If not, see <http://www.gnu.org/licenses/>.
|
2016-02-05 13:40:41 +01:00
|
|
|
|
2020-09-22 14:53:52 +02:00
|
|
|
//! OpenEthereum JSON-RPC Servers (WS, HTTP, IPC).
|
2017-05-24 12:24:07 +02:00
|
|
|
|
2019-03-19 16:37:24 +01:00
|
|
|
#![warn(missing_docs, unused_extern_crates)]
|
2019-03-22 12:01:11 +01:00
|
|
|
#![cfg_attr(feature = "cargo-clippy", warn(clippy::all, clippy::pedantic))]
|
|
|
|
#![cfg_attr(
|
|
|
|
feature = "cargo-clippy",
|
|
|
|
allow(
|
|
|
|
// things are often more readable this way
|
|
|
|
clippy::cast_lossless,
|
|
|
|
clippy::module_name_repetitions,
|
|
|
|
clippy::single_match_else,
|
|
|
|
clippy::type_complexity,
|
|
|
|
clippy::use_self,
|
|
|
|
// not practical
|
|
|
|
clippy::match_bool,
|
|
|
|
clippy::needless_pass_by_value,
|
|
|
|
clippy::similar_names,
|
|
|
|
// don't require markdown syntax for docs
|
|
|
|
clippy::doc_markdown,
|
|
|
|
),
|
|
|
|
warn(clippy::indexing_slicing)
|
|
|
|
)]
|
2016-01-26 13:14:22 +01:00
|
|
|
|
2017-10-17 14:50:53 +02:00
|
|
|
#[macro_use]
|
|
|
|
extern crate futures;
|
|
|
|
|
2017-09-01 16:57:57 +02:00
|
|
|
extern crate ansi_term;
|
2017-08-17 16:05:26 +02:00
|
|
|
extern crate itertools;
|
2017-03-22 07:02:14 +01:00
|
|
|
extern crate order_stat;
|
2017-09-02 20:09:13 +02:00
|
|
|
extern crate parking_lot;
|
2017-05-24 12:24:07 +02:00
|
|
|
extern crate rand;
|
2017-07-06 11:26:14 +02:00
|
|
|
extern crate rustc_hex;
|
2016-01-26 13:14:22 +01:00
|
|
|
extern crate serde;
|
2016-01-26 19:24:33 +01:00
|
|
|
extern crate serde_json;
|
2017-05-06 13:24:18 +02:00
|
|
|
extern crate tokio_timer;
|
2017-03-22 07:02:14 +01:00
|
|
|
extern crate transient_hashmap;
|
|
|
|
|
2016-01-21 01:19:29 +01:00
|
|
|
extern crate jsonrpc_core;
|
2019-02-05 14:31:19 +01:00
|
|
|
extern crate jsonrpc_derive;
|
2017-04-03 10:27:37 +02:00
|
|
|
extern crate jsonrpc_http_server as http;
|
|
|
|
extern crate jsonrpc_ipc_server as ipc;
|
2017-05-06 13:24:18 +02:00
|
|
|
extern crate jsonrpc_pubsub;
|
2016-09-01 14:49:12 +02:00
|
|
|
|
2019-01-04 14:05:46 +01:00
|
|
|
extern crate common_types as types;
|
2020-08-05 06:08:03 +02:00
|
|
|
extern crate eip_712;
|
2017-03-22 07:02:14 +01:00
|
|
|
extern crate ethash;
|
2016-01-26 13:14:22 +01:00
|
|
|
extern crate ethcore;
|
2018-04-10 12:13:49 +02:00
|
|
|
extern crate ethcore_logger;
|
2018-01-11 17:49:10 +01:00
|
|
|
extern crate ethcore_miner as miner;
|
2019-02-07 15:27:09 +01:00
|
|
|
extern crate ethcore_network as network;
|
2018-04-10 12:13:49 +02:00
|
|
|
extern crate ethcore_sync as sync;
|
2018-01-10 13:35:18 +01:00
|
|
|
extern crate ethereum_types;
|
2017-03-22 07:02:14 +01:00
|
|
|
extern crate ethkey;
|
2016-08-10 17:57:40 +02:00
|
|
|
extern crate ethstore;
|
2016-09-27 16:27:06 +02:00
|
|
|
extern crate fetch;
|
2018-06-26 09:03:38 +02:00
|
|
|
extern crate keccak_hash as hash;
|
2020-08-05 06:08:03 +02:00
|
|
|
extern crate parity_bytes as bytes;
|
|
|
|
extern crate parity_crypto as crypto;
|
2018-10-22 09:40:50 +02:00
|
|
|
extern crate parity_runtime;
|
2017-12-22 14:37:39 +01:00
|
|
|
extern crate parity_version as version;
|
2017-03-22 07:02:14 +01:00
|
|
|
extern crate rlp;
|
2017-02-17 16:18:31 +01:00
|
|
|
extern crate stats;
|
2019-03-19 16:37:24 +01:00
|
|
|
extern crate tempdir;
|
2018-06-26 09:03:38 +02:00
|
|
|
extern crate vm;
|
|
|
|
|
2019-02-07 14:34:24 +01:00
|
|
|
#[cfg(any(test, feature = "ethcore-accounts"))]
|
|
|
|
extern crate ethcore_accounts as accounts;
|
2016-09-01 14:49:12 +02:00
|
|
|
|
2019-03-19 16:37:24 +01:00
|
|
|
#[cfg(any(test, feature = "ethcore-accounts"))]
|
|
|
|
extern crate tiny_keccak;
|
|
|
|
|
2016-09-01 14:49:12 +02:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
2016-12-13 14:59:19 +01:00
|
|
|
#[macro_use]
|
2017-02-13 16:38:47 +01:00
|
|
|
extern crate serde_derive;
|
2016-01-21 01:19:29 +01:00
|
|
|
|
2016-05-24 16:56:09 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
extern crate ethjson;
|
|
|
|
|
2017-04-27 18:23:22 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
#[macro_use]
|
|
|
|
extern crate pretty_assertions;
|
|
|
|
|
2017-10-10 20:01:27 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
#[macro_use]
|
|
|
|
extern crate macros;
|
|
|
|
|
2018-04-11 11:59:04 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
extern crate fake_fetch;
|
|
|
|
|
2019-03-19 16:37:24 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
extern crate ethcore_io as io;
|
2018-01-19 17:32:53 +01:00
|
|
|
|
2017-04-13 16:32:07 +02:00
|
|
|
pub extern crate jsonrpc_ws_server as ws;
|
|
|
|
|
2017-05-24 12:24:07 +02:00
|
|
|
mod authcodes;
|
|
|
|
mod http_common;
|
2016-03-02 12:42:08 +01:00
|
|
|
pub mod v1;
|
2017-03-22 07:02:14 +01:00
|
|
|
|
2017-05-24 12:24:07 +02:00
|
|
|
pub mod tests;
|
|
|
|
|
2017-04-03 10:27:37 +02:00
|
|
|
pub use http::{
|
2020-08-05 06:08:03 +02:00
|
|
|
cors::AccessControlAllowHeaders, hyper, AccessControlAllowOrigin, DomainsValidation, Host,
|
|
|
|
RequestMiddleware, RequestMiddlewareAction,
|
2017-04-03 10:27:37 +02:00
|
|
|
};
|
2020-08-05 06:08:03 +02:00
|
|
|
pub use ipc::{
|
|
|
|
MetaExtractor as IpcMetaExtractor, RequestContext as IpcRequestContext, Server as IpcServer,
|
|
|
|
};
|
|
|
|
pub use jsonrpc_core::{FutureOutput, FutureResponse, FutureResult, FutureRpcResult};
|
|
|
|
pub use jsonrpc_pubsub::Session as PubSubSession;
|
2017-03-22 07:02:14 +01:00
|
|
|
|
2017-05-24 12:24:07 +02:00
|
|
|
pub use authcodes::{AuthCodes, TimeProvider};
|
|
|
|
pub use http_common::HttpMetaExtractor;
|
2020-08-05 06:08:03 +02:00
|
|
|
pub use v1::{
|
|
|
|
block_import::{is_major_importing, is_major_importing_or_waiting},
|
|
|
|
dispatch,
|
|
|
|
extractors::{RpcExtractor, WsDispatcher, WsExtractor, WsStats},
|
|
|
|
informant, signer, Metadata, NetworkSettings, Origin,
|
|
|
|
};
|
2016-06-01 19:37:34 +02:00
|
|
|
|
2017-03-22 07:02:14 +01:00
|
|
|
use std::net::SocketAddr;
|
|
|
|
|
2017-04-03 10:27:37 +02:00
|
|
|
/// RPC HTTP Server instance
|
2017-10-05 12:35:01 +02:00
|
|
|
pub type HttpServer = http::Server;
|
2017-04-03 10:27:37 +02:00
|
|
|
|
2017-01-11 20:02:27 +01:00
|
|
|
/// Start http server asynchronously and returns result with `Server` handle on success or an error.
|
2018-07-11 12:19:54 +02:00
|
|
|
pub fn start_http<M, S, H, T>(
|
2020-08-05 06:08:03 +02:00
|
|
|
addr: &SocketAddr,
|
|
|
|
cors_domains: http::DomainsValidation<http::AccessControlAllowOrigin>,
|
|
|
|
allowed_hosts: http::DomainsValidation<http::Host>,
|
|
|
|
handler: H,
|
|
|
|
extractor: T,
|
|
|
|
threads: usize,
|
|
|
|
max_payload: usize,
|
|
|
|
keep_alive: bool,
|
|
|
|
) -> ::std::io::Result<HttpServer>
|
|
|
|
where
|
|
|
|
M: jsonrpc_core::Metadata,
|
|
|
|
S: jsonrpc_core::Middleware<M>,
|
|
|
|
H: Into<jsonrpc_core::MetaIoHandler<M, S>>,
|
|
|
|
T: HttpMetaExtractor<Metadata = M>,
|
2017-02-14 22:45:43 +01:00
|
|
|
{
|
2020-08-05 06:08:03 +02:00
|
|
|
let extractor = http_common::MetaExtractor::new(extractor);
|
|
|
|
Ok(http::ServerBuilder::with_meta_extractor(handler, extractor)
|
|
|
|
.keep_alive(keep_alive)
|
|
|
|
.threads(threads)
|
|
|
|
.cors(cors_domains)
|
|
|
|
.allowed_hosts(allowed_hosts)
|
|
|
|
.health_api(("/api/health", "parity_nodeStatus"))
|
|
|
|
.cors_allow_headers(AccessControlAllowHeaders::Any)
|
|
|
|
.max_request_body_size(max_payload * 1024 * 1024)
|
|
|
|
.start_http(addr)?)
|
2018-07-11 12:19:54 +02:00
|
|
|
}
|
2017-10-05 12:35:01 +02:00
|
|
|
|
2018-07-11 12:19:54 +02:00
|
|
|
/// Same as `start_http`, but takes an additional `middleware` parameter that is introduced as a
|
|
|
|
/// hyper middleware.
|
|
|
|
pub fn start_http_with_middleware<M, S, H, T, R>(
|
2020-08-05 06:08:03 +02:00
|
|
|
addr: &SocketAddr,
|
|
|
|
cors_domains: http::DomainsValidation<http::AccessControlAllowOrigin>,
|
|
|
|
allowed_hosts: http::DomainsValidation<http::Host>,
|
|
|
|
handler: H,
|
|
|
|
extractor: T,
|
|
|
|
middleware: R,
|
|
|
|
threads: usize,
|
|
|
|
max_payload: usize,
|
|
|
|
keep_alive: bool,
|
|
|
|
) -> ::std::io::Result<HttpServer>
|
|
|
|
where
|
|
|
|
M: jsonrpc_core::Metadata,
|
|
|
|
S: jsonrpc_core::Middleware<M>,
|
|
|
|
H: Into<jsonrpc_core::MetaIoHandler<M, S>>,
|
|
|
|
T: HttpMetaExtractor<Metadata = M>,
|
|
|
|
R: RequestMiddleware,
|
2018-07-11 12:19:54 +02:00
|
|
|
{
|
2020-08-05 06:08:03 +02:00
|
|
|
let extractor = http_common::MetaExtractor::new(extractor);
|
|
|
|
Ok(http::ServerBuilder::with_meta_extractor(handler, extractor)
|
|
|
|
.keep_alive(keep_alive)
|
|
|
|
.threads(threads)
|
|
|
|
.cors(cors_domains)
|
|
|
|
.allowed_hosts(allowed_hosts)
|
|
|
|
.cors_allow_headers(AccessControlAllowHeaders::Any)
|
|
|
|
.max_request_body_size(max_payload * 1024 * 1024)
|
|
|
|
.request_middleware(middleware)
|
|
|
|
.start_http(addr)?)
|
2017-01-11 20:02:27 +01:00
|
|
|
}
|
2016-05-04 15:37:09 +02:00
|
|
|
|
2017-01-11 20:02:27 +01:00
|
|
|
/// Start ipc server asynchronously and returns result with `Server` handle on success or an error.
|
2020-08-05 06:08:03 +02:00
|
|
|
pub fn start_ipc<M, S, H, T>(addr: &str, handler: H, extractor: T) -> ::std::io::Result<ipc::Server>
|
|
|
|
where
|
|
|
|
M: jsonrpc_core::Metadata,
|
|
|
|
S: jsonrpc_core::Middleware<M>,
|
|
|
|
H: Into<jsonrpc_core::MetaIoHandler<M, S>>,
|
|
|
|
T: IpcMetaExtractor<M>,
|
2017-03-22 07:02:14 +01:00
|
|
|
{
|
2020-08-05 06:08:03 +02:00
|
|
|
ipc::ServerBuilder::with_meta_extractor(handler, extractor).start(addr)
|
2016-03-02 12:42:08 +01:00
|
|
|
}
|
2017-04-13 16:32:07 +02:00
|
|
|
|
|
|
|
/// Start WS server and return `Server` handle.
|
2017-05-24 12:24:07 +02:00
|
|
|
pub fn start_ws<M, S, H, T, U, V>(
|
2020-08-05 06:08:03 +02:00
|
|
|
addr: &SocketAddr,
|
|
|
|
handler: H,
|
|
|
|
allowed_origins: ws::DomainsValidation<ws::Origin>,
|
|
|
|
allowed_hosts: ws::DomainsValidation<ws::Host>,
|
|
|
|
max_connections: usize,
|
|
|
|
extractor: T,
|
|
|
|
middleware: V,
|
|
|
|
stats: U,
|
2020-12-09 11:48:27 +01:00
|
|
|
max_payload: usize,
|
2020-08-05 06:08:03 +02:00
|
|
|
) -> Result<ws::Server, ws::Error>
|
|
|
|
where
|
|
|
|
M: jsonrpc_core::Metadata,
|
|
|
|
S: jsonrpc_core::Middleware<M>,
|
|
|
|
H: Into<jsonrpc_core::MetaIoHandler<M, S>>,
|
|
|
|
T: ws::MetaExtractor<M>,
|
|
|
|
U: ws::SessionStats,
|
|
|
|
V: ws::RequestMiddleware,
|
2017-04-13 16:32:07 +02:00
|
|
|
{
|
2020-08-05 06:08:03 +02:00
|
|
|
ws::ServerBuilder::with_meta_extractor(handler, extractor)
|
|
|
|
.request_middleware(middleware)
|
|
|
|
.allowed_origins(allowed_origins)
|
|
|
|
.allowed_hosts(allowed_hosts)
|
|
|
|
.max_connections(max_connections)
|
2020-12-09 11:48:27 +01:00
|
|
|
.max_payload(max_payload * 1024 * 1024)
|
2020-08-05 06:08:03 +02:00
|
|
|
.session_stats(stats)
|
|
|
|
.start(addr)
|
2017-04-13 16:32:07 +02:00
|
|
|
}
|