Skip to content

Commit a2a8fb0

Browse files
committed
Support custom ipc path / http port as cli args
1 parent 1197b0c commit a2a8fb0

File tree

6 files changed

+38
-12
lines changed

6 files changed

+38
-12
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ through github via pull requests.
2222
* Mark unfinished pull requests with the "Work in Progress" label.
2323
* Before submitting a pr for review, you should run the following commands
2424
locally and make sure they are passing, otherwise CI will raise an error.
25-
* `cargo fmt -- --check` and `cargo clippy -- --deny warnings` for linting checks
25+
* `cargo fmt --all -- --check` and `cargo clippy --all -- --deny warnings` for linting checks
2626
* `RUSTFLAGS='-D warnings' cargo test --workspace` to run all tests
2727
* Run the `ethportal-peertest` harness against a locally running node. Instructions
2828
can be found in [README](ethportal-peertest/README.md).

ethportal-peertest/src/cli.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::env;
22
use std::ffi::OsString;
33
use structopt::StructOpt;
4+
use trin_core::cli::DEFAULT_WEB3_HTTP_PORT as DEFAULT_TARGET_HTTP_PORT;
5+
use trin_core::cli::DEFAULT_WEB3_IPC_PATH as DEFAULT_TARGET_IPC_PATH;
46

57
const DEFAULT_LISTEN_PORT: &str = "9876";
68
const DEFAULT_WEB3_IPC_PATH: &str = "/tmp/json-rpc-peertest.ipc";
@@ -41,6 +43,20 @@ pub struct PeertestConfig {
4143
help = "Transport type of the node under test"
4244
)]
4345
pub target_transport: String,
46+
47+
#[structopt(
48+
default_value = DEFAULT_TARGET_IPC_PATH,
49+
long = "target-ipc-path",
50+
help = "IPC path of target node under test"
51+
)]
52+
pub target_ipc_path: String,
53+
54+
#[structopt(
55+
default_value = DEFAULT_TARGET_HTTP_PORT,
56+
long = "target-http-port",
57+
help = "HTTP port of target node under test"
58+
)]
59+
pub target_http_port: String,
4460
}
4561

4662
impl PeertestConfig {

ethportal-peertest/src/jsonrpc.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const ALL_ENDPOINTS: [JsonRpcEndpoint; 6] = [
4545
fn validate_endpoint_response(method: &str, result: &Value) {
4646
match method {
4747
"web3_clientVersion" => {
48-
assert_eq!(result.as_str().unwrap(), "trin 0.0.1-alpha");
48+
assert_eq!(result.as_str().unwrap(), "trin v0.1.0");
4949
}
5050
"discv5_nodeInfo" => {
5151
let enr = result.get("enr").unwrap();
@@ -92,10 +92,11 @@ impl JsonRpcEndpoint {
9292
}
9393
}
9494

95-
pub async fn test_jsonrpc_endpoints_over_ipc() {
95+
#[allow(clippy::never_loop)]
96+
pub async fn test_jsonrpc_endpoints_over_ipc(target_ipc_path: String) {
9697
for endpoint in JsonRpcEndpoint::all_endpoints() {
9798
info!("Testing over IPC: {:?}", endpoint.method);
98-
let mut stream = UnixStream::connect("/tmp/trin-jsonrpc.ipc").unwrap();
99+
let mut stream = UnixStream::connect(&target_ipc_path).unwrap();
99100
let v: Value = serde_json::from_str(&endpoint.to_jsonrpc()).unwrap();
100101
let data = serde_json::to_vec(&v).unwrap();
101102
stream.write_all(&data).unwrap();
@@ -110,6 +111,8 @@ pub async fn test_jsonrpc_endpoints_over_ipc() {
110111
endpoint.method, msg
111112
),
112113
}
114+
// break out of loop here since EOF is not sent, and loop will hang
115+
break;
113116
}
114117
}
115118
}
@@ -133,14 +136,14 @@ fn get_response_result(response: Value) -> Result<Value, JsonRpcResponseError> {
133136
}
134137
}
135138

136-
pub async fn test_jsonrpc_endpoints_over_http() {
139+
pub async fn test_jsonrpc_endpoints_over_http(target_http_port: String) {
137140
let client = Client::new();
138141
for endpoint in JsonRpcEndpoint::all_endpoints() {
139142
info!("Testing over HTTP: {:?}", endpoint.method);
140143
let json_string = endpoint.to_jsonrpc();
141144
let req = Request::builder()
142145
.method(Method::POST)
143-
.uri("http://127.0.0.1:8545")
146+
.uri(format!("http://127.0.0.1:{}", target_http_port))
144147
.header("content-type", "application/json")
145148
.body(Body::from(json_string))
146149
.unwrap();

ethportal-peertest/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
8383
info!("State network Ping result: {:?}", ping_result);
8484

8585
match peertest_config.target_transport.as_str() {
86-
"ipc" => test_jsonrpc_endpoints_over_ipc().await,
87-
"http" => test_jsonrpc_endpoints_over_http().await,
86+
"ipc" => test_jsonrpc_endpoints_over_ipc(peertest_config.target_ipc_path).await,
87+
"http" => test_jsonrpc_endpoints_over_http(peertest_config.target_http_port).await,
8888
_ => panic!(
8989
"Invalid target-transport provided: {:?}",
9090
peertest_config.target_transport

trin-core/src/cli.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use std::ffi::OsString;
66
use std::net::SocketAddr;
77
use structopt::StructOpt;
88

9-
const DEFAULT_WEB3_IPC_PATH: &str = "/tmp/trin-jsonrpc.ipc";
10-
const DEFAULT_WEB3_HTTP_PORT: &str = "8545";
9+
pub const DEFAULT_WEB3_IPC_PATH: &str = "/tmp/trin-jsonrpc.ipc";
10+
pub const DEFAULT_WEB3_HTTP_PORT: &str = "8545";
1111
const DEFAULT_DISCOVERY_PORT: &str = "9000";
1212
pub const HISTORY_NETWORK: &str = "history";
1313
pub const STATE_NETWORK: &str = "state";

trin-core/src/jsonrpc/service.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ fn serve_http_client(
188188
let http_body = match parse_http_body(received) {
189189
Ok(val) => val,
190190
Err(msg) => {
191-
warn!("Error parsing http request: {:?}", msg);
191+
respond_with_parsing_error(stream, msg.to_string());
192192
return;
193193
}
194194
};
@@ -197,7 +197,7 @@ fn serve_http_client(
197197
let obj = match obj {
198198
Ok(val) => val,
199199
Err(msg) => {
200-
warn!("Error parsing http request: {:?}", msg);
200+
respond_with_parsing_error(stream, msg.to_string());
201201
break;
202202
}
203203
};
@@ -210,6 +210,13 @@ fn serve_http_client(
210210
}
211211
}
212212

213+
fn respond_with_parsing_error(mut stream: TcpStream, msg: String) {
214+
warn!("Error parsing http request: {:?}", msg);
215+
let resp = format!("HTTP/1.1 400 BAD REQUEST\r\n\r\n{}", msg).into_bytes();
216+
stream.write_all(&resp).unwrap();
217+
stream.flush().unwrap();
218+
}
219+
213220
#[derive(Error, Debug)]
214221
pub enum HttpParseError {
215222
#[error("Unable to parse http request: {0}")]

0 commit comments

Comments
 (0)