Skip to content

Commit

Permalink
Support custom ipc path / http port as cli args
Browse files Browse the repository at this point in the history
  • Loading branch information
njgheorghita committed Oct 21, 2021
1 parent 1197b0c commit a2a8fb0
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 12 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ through github via pull requests.
* Mark unfinished pull requests with the "Work in Progress" label.
* Before submitting a pr for review, you should run the following commands
locally and make sure they are passing, otherwise CI will raise an error.
* `cargo fmt -- --check` and `cargo clippy -- --deny warnings` for linting checks
* `cargo fmt --all -- --check` and `cargo clippy --all -- --deny warnings` for linting checks
* `RUSTFLAGS='-D warnings' cargo test --workspace` to run all tests
* Run the `ethportal-peertest` harness against a locally running node. Instructions
can be found in [README](ethportal-peertest/README.md).
Expand Down
16 changes: 16 additions & 0 deletions ethportal-peertest/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::env;
use std::ffi::OsString;
use structopt::StructOpt;
use trin_core::cli::DEFAULT_WEB3_HTTP_PORT as DEFAULT_TARGET_HTTP_PORT;
use trin_core::cli::DEFAULT_WEB3_IPC_PATH as DEFAULT_TARGET_IPC_PATH;

const DEFAULT_LISTEN_PORT: &str = "9876";
const DEFAULT_WEB3_IPC_PATH: &str = "/tmp/json-rpc-peertest.ipc";
Expand Down Expand Up @@ -41,6 +43,20 @@ pub struct PeertestConfig {
help = "Transport type of the node under test"
)]
pub target_transport: String,

#[structopt(
default_value = DEFAULT_TARGET_IPC_PATH,
long = "target-ipc-path",
help = "IPC path of target node under test"
)]
pub target_ipc_path: String,

#[structopt(
default_value = DEFAULT_TARGET_HTTP_PORT,
long = "target-http-port",
help = "HTTP port of target node under test"
)]
pub target_http_port: String,
}

impl PeertestConfig {
Expand Down
13 changes: 8 additions & 5 deletions ethportal-peertest/src/jsonrpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const ALL_ENDPOINTS: [JsonRpcEndpoint; 6] = [
fn validate_endpoint_response(method: &str, result: &Value) {
match method {
"web3_clientVersion" => {
assert_eq!(result.as_str().unwrap(), "trin 0.0.1-alpha");
assert_eq!(result.as_str().unwrap(), "trin v0.1.0");
}
"discv5_nodeInfo" => {
let enr = result.get("enr").unwrap();
Expand Down Expand Up @@ -92,10 +92,11 @@ impl JsonRpcEndpoint {
}
}

pub async fn test_jsonrpc_endpoints_over_ipc() {
#[allow(clippy::never_loop)]
pub async fn test_jsonrpc_endpoints_over_ipc(target_ipc_path: String) {
for endpoint in JsonRpcEndpoint::all_endpoints() {
info!("Testing over IPC: {:?}", endpoint.method);
let mut stream = UnixStream::connect("/tmp/trin-jsonrpc.ipc").unwrap();
let mut stream = UnixStream::connect(&target_ipc_path).unwrap();
let v: Value = serde_json::from_str(&endpoint.to_jsonrpc()).unwrap();
let data = serde_json::to_vec(&v).unwrap();
stream.write_all(&data).unwrap();
Expand All @@ -110,6 +111,8 @@ pub async fn test_jsonrpc_endpoints_over_ipc() {
endpoint.method, msg
),
}
// break out of loop here since EOF is not sent, and loop will hang
break;
}
}
}
Expand All @@ -133,14 +136,14 @@ fn get_response_result(response: Value) -> Result<Value, JsonRpcResponseError> {
}
}

pub async fn test_jsonrpc_endpoints_over_http() {
pub async fn test_jsonrpc_endpoints_over_http(target_http_port: String) {
let client = Client::new();
for endpoint in JsonRpcEndpoint::all_endpoints() {
info!("Testing over HTTP: {:?}", endpoint.method);
let json_string = endpoint.to_jsonrpc();
let req = Request::builder()
.method(Method::POST)
.uri("http://127.0.0.1:8545")
.uri(format!("http://127.0.0.1:{}", target_http_port))
.header("content-type", "application/json")
.body(Body::from(json_string))
.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions ethportal-peertest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
info!("State network Ping result: {:?}", ping_result);

match peertest_config.target_transport.as_str() {
"ipc" => test_jsonrpc_endpoints_over_ipc().await,
"http" => test_jsonrpc_endpoints_over_http().await,
"ipc" => test_jsonrpc_endpoints_over_ipc(peertest_config.target_ipc_path).await,
"http" => test_jsonrpc_endpoints_over_http(peertest_config.target_http_port).await,
_ => panic!(
"Invalid target-transport provided: {:?}",
peertest_config.target_transport
Expand Down
4 changes: 2 additions & 2 deletions trin-core/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use std::ffi::OsString;
use std::net::SocketAddr;
use structopt::StructOpt;

const DEFAULT_WEB3_IPC_PATH: &str = "/tmp/trin-jsonrpc.ipc";
const DEFAULT_WEB3_HTTP_PORT: &str = "8545";
pub const DEFAULT_WEB3_IPC_PATH: &str = "/tmp/trin-jsonrpc.ipc";
pub const DEFAULT_WEB3_HTTP_PORT: &str = "8545";
const DEFAULT_DISCOVERY_PORT: &str = "9000";
pub const HISTORY_NETWORK: &str = "history";
pub const STATE_NETWORK: &str = "state";
Expand Down
11 changes: 9 additions & 2 deletions trin-core/src/jsonrpc/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ fn serve_http_client(
let http_body = match parse_http_body(received) {
Ok(val) => val,
Err(msg) => {
warn!("Error parsing http request: {:?}", msg);
respond_with_parsing_error(stream, msg.to_string());
return;
}
};
Expand All @@ -197,7 +197,7 @@ fn serve_http_client(
let obj = match obj {
Ok(val) => val,
Err(msg) => {
warn!("Error parsing http request: {:?}", msg);
respond_with_parsing_error(stream, msg.to_string());
break;
}
};
Expand All @@ -210,6 +210,13 @@ fn serve_http_client(
}
}

fn respond_with_parsing_error(mut stream: TcpStream, msg: String) {
warn!("Error parsing http request: {:?}", msg);
let resp = format!("HTTP/1.1 400 BAD REQUEST\r\n\r\n{}", msg).into_bytes();
stream.write_all(&resp).unwrap();
stream.flush().unwrap();
}

#[derive(Error, Debug)]
pub enum HttpParseError {
#[error("Unable to parse http request: {0}")]
Expand Down

0 comments on commit a2a8fb0

Please sign in to comment.