Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: add param arguments for initializing websocket #992

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions book/src/developers/contributing/build_instructions/linux.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ test if it is in use (no response indicates it is ok to use):
$ sudo ss -tulpn | grep ':9008'
```

#### Optional flag to enable websocket rpc
`--ws`. This will allow you to run a websocket on port 8546
`--ws --ws-port 3334`. A custom websocket port can be configured like this


### Create the node service

Create a service to run the Trin node:
Expand Down
39 changes: 32 additions & 7 deletions ethportal-api/src/types/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,17 @@ pub struct TrinConfig {
)]
pub disable_poke: bool,

#[arg(long = "ws", help = "Used to enable WebSocket rpc.")]
pub ws: bool,

#[arg(
long = "ws-port",
help = "The WebSocket port to listen on.",
default_value_t = DEFAULT_WEB3_WS_PORT,
requires = "ws"
)]
pub ws_port: u16,

#[command(subcommand)]
pub command: Option<TrinConfigCommands>,
}
Expand Down Expand Up @@ -182,6 +193,8 @@ impl Default for TrinConfig {
ephemeral: false,
master_acc_path: PathBuf::from(DEFAULT_MASTER_ACC_PATH.to_string()),
disable_poke: false,
ws: false,
ws_port: DEFAULT_WEB3_WS_PORT,
command: None,
}
}
Expand All @@ -207,13 +220,25 @@ impl TrinConfig {
}

match config.web3_transport {
Web3TransportType::HTTP => match &config.web3_ipc_path.as_path().display().to_string()[..] {
DEFAULT_WEB3_IPC_PATH => {}
_ => return Err(Error::raw(ErrorKind::ArgumentConflict, "Must not supply an ipc path when using http protocol for json-rpc")),
},
Web3TransportType::IPC => match config.web3_http_address.as_str() {
DEFAULT_WEB3_HTTP_ADDRESS => {}
p => return Err(Error::raw(ErrorKind::ArgumentConflict,format!("Must not supply an http address when using ipc protocol for json-rpc (received: {p})"))),
Web3TransportType::HTTP => {
match &config.web3_ipc_path.as_path().display().to_string()[..] {
DEFAULT_WEB3_IPC_PATH => {}
_ => {
return Err(Error::raw(
ErrorKind::ArgumentConflict,
"Must not supply an ipc path when using http protocol for json-rpc",
))
}
}
}
Web3TransportType::IPC => {
match config.web3_http_address.as_str() {
DEFAULT_WEB3_HTTP_ADDRESS => {}
web3_http_address => return Err(Error::raw(ErrorKind::ArgumentConflict,format!("Must not supply an http address when using ipc protocol for json-rpc (received: {web3_http_address})"))),
}
if config.ws {
return Err(Error::raw(ErrorKind::ArgumentConflict,format!("Must not enable ws when using ipc protocol for json-rpc (received: {})", config.web3_http_address.as_str())));
}
}
}
Ok(config)
Expand Down
25 changes: 19 additions & 6 deletions rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use web3_rpc::Web3Api;
use crate::rpc_server::RpcServerConfig;
use portalnet::discovery::Discovery;
use reth_ipc::server::Builder as IpcServerBuilder;
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
use std::sync::Arc;
use tokio::sync::mpsc;

Expand Down Expand Up @@ -80,24 +81,36 @@ pub async fn launch_jsonrpc_server(
.await?
}
Web3TransportType::HTTP => {
let transport = TransportRpcModuleConfig::default().with_http(modules);
let transport = match trin_config.ws {
true => TransportRpcModuleConfig::default().with_ws(modules.clone()),
false => TransportRpcModuleConfig::default(),
};
let transport = transport.with_http(modules);

let transport_modules = RpcModuleBuilder::new(discv5)
.maybe_with_history(history_handler)
.maybe_with_beacon(beacon_handler)
.maybe_with_state(state_handler)
.build(transport);

RpcServerConfig::default()
let rpc_server_config = RpcServerConfig::default()
.with_http_address(
trin_config
.web3_http_address
.socket_addrs(|| None)
.expect("Invalid socket address")[0],
)
.with_http(ServerBuilder::default())
.with_ws(ServerBuilder::default())
.start(transport_modules)
.await?
.with_http(ServerBuilder::default());
let rpc_server_config = match trin_config.ws {
true => rpc_server_config
.with_ws_address(SocketAddr::V4(SocketAddrV4::new(
Ipv4Addr::UNSPECIFIED,
trin_config.ws_port,
)))
.with_ws(ServerBuilder::default()),
false => rpc_server_config,
};
rpc_server_config.start(transport_modules).await?
}
};

Expand Down
14 changes: 7 additions & 7 deletions rpc/src/rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,13 +375,6 @@ impl RpcServerConfig {
))
});

let ws_socket_addr = self.ws_addr.unwrap_or_else(|| {
SocketAddr::V4(SocketAddrV4::new(
Ipv4Addr::UNSPECIFIED,
DEFAULT_WEB3_WS_PORT,
))
});

// If both are configured on the same port, we combine them into one server.
if self.http_addr == self.ws_addr
&& self.http_server_config.is_some()
Expand Down Expand Up @@ -431,6 +424,13 @@ impl RpcServerConfig {
let mut ws_local_addr = None;
let mut ws_server = None;
if let Some(builder) = self.ws_server_config.take() {
let ws_socket_addr = self.ws_addr.unwrap_or_else(|| {
SocketAddr::V4(SocketAddrV4::new(
Ipv4Addr::UNSPECIFIED,
DEFAULT_WEB3_WS_PORT,
))
});

let builder = builder.ws_only();
let (server, addr) = WsHttpServerKind::build(
builder,
Expand Down