Skip to content

Commit

Permalink
feat(Rundler): Add cors to the server
Browse files Browse the repository at this point in the history
  • Loading branch information
Blu-J committed Dec 9, 2024
1 parent d283ce1 commit 5e948fb
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 2 deletions.
25 changes: 23 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ tonic-health = "0.12.3"
tonic-reflection = "0.12.3"
tonic-types = "0.12.3"
tower = { version = "0.4.13", features = ["timeout"] }
tower-http = { version = "0.6.2", features = ["cors", "timeout"] }
tracing = "0.1.40"
strum = { version = "0.26.3", features = ["derive"] }
url = "2.5.2"
8 changes: 8 additions & 0 deletions bin/rundler/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,14 @@ pub struct CommonArgs {
default_value = "false"
)]
pub da_gas_tracking_enabled: bool,
#[arg(
long = "rpc.corsdomain",
name = "rpc.corsdomain",
env = "RPC_CORSDOMAIN",
default_value = "false",
global = true
)]
pub corsdomain: bool,
}

const SIMULATION_GAS_OVERHEAD: u64 = 100_000;
Expand Down
10 changes: 10 additions & 0 deletions bin/rundler/src/cli/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ pub struct RpcArgs {
default_value = "100"
)]
max_connections: u32,

#[arg(
long = "rpc.corsdomain",
name = "rpc.corsdomain",
env = "RPC_CORSDOMAIN",
default_value = "false",
global = true
)]
pub corsdomain: bool,
}

impl RpcArgs {
Expand Down Expand Up @@ -109,6 +118,7 @@ impl RpcArgs {
max_connections: self.max_connections,
entry_point_v0_6_enabled: !common.disable_entry_point_v0_6,
entry_point_v0_7_enabled: !common.disable_entry_point_v0_7,
corsdomain: common.corsdomain,
})
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ enum_dispatch = "0.3.13"
futures.workspace = true
futures-timer = "3.0.3"
futures-util.workspace = true
http = "1.1.0"
jsonrpsee = { workspace = true, features = [ "http-client" ] }
linked-hash-map = "0.5.6"
metrics.workspace = true
Expand All @@ -49,6 +50,7 @@ tokio-util.workspace = true
tonic.workspace = true
tonic-health.workspace = true
tonic-reflection.workspace = true
tower-http = { version = "0.6.2", features = ["cors", "timeout"] }
tracing.workspace = true

mockall = { workspace = true, optional = true }
Expand Down
9 changes: 9 additions & 0 deletions crates/builder/src/server/remote/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::net::SocketAddr;
use rundler_task::GracefulShutdown;
use rundler_types::builder::Builder;
use tonic::{async_trait, transport::Server, Request, Response, Status};
use tower_http::cors::Any;

use super::protos::{
builder_server::{Builder as GrpcBuilder, BuilderServer as GrpcBuilderServer},
Expand All @@ -42,13 +43,21 @@ pub(crate) async fn remote_builder_server_task(
.build_v1()
.expect("should build builder reflection service");

let cors = tower_http::cors::CorsLayer::new()
// allow `GET` and `POST` when accessing the resource
.allow_methods([http::Method::GET, http::Method::POST])
// allow requests from any origin
.allow_origin(Any)
.allow_headers([http::header::CONTENT_TYPE]);

// health service
let (mut health_reporter, health_service) = tonic_health::server::health_reporter();
health_reporter
.set_serving::<GrpcBuilderServer<GrpcBuilderServerImpl>>()
.await;

if let Err(e) = Server::builder()
.layer(cors)
.add_service(builder_server)
.add_service(reflection_service)
.add_service(health_service)
Expand Down
2 changes: 2 additions & 0 deletions crates/pool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ async-stream = "0.3.5"
async-trait.workspace = true
futures.workspace = true
futures-util.workspace = true
http = "1.1.0"
itertools.workspace = true
metrics.workspace = true
metrics-derive.workspace = true
Expand All @@ -37,6 +38,7 @@ tokio-util.workspace = true
tonic.workspace = true
tonic-health.workspace = true
tonic-reflection.workspace = true
tower-http = { workspace = true, features = ["cors"] }
tracing.workspace = true
url.workspace = true

Expand Down
9 changes: 9 additions & 0 deletions crates/pool/src/server/remote/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use rundler_types::{
use tokio::sync::mpsc;
use tokio_stream::wrappers::UnboundedReceiverStream;
use tonic::{transport::Server, Request, Response, Result, Status};
use tower_http::cors::Any;

use super::protos::{
add_op_response, admin_set_tracking_response, debug_clear_state_response,
Expand Down Expand Up @@ -84,7 +85,15 @@ pub(crate) async fn remote_mempool_server_task(

let metrics_layer = GrpcMetricsLayer::new("op_pool_service".to_string());

let cors = tower_http::cors::CorsLayer::new()
// allow `GET` and `POST` when accessing the resource
.allow_methods([http::Method::GET, http::Method::POST])
// allow requests from any origin
.allow_origin(Any)
.allow_headers([http::header::CONTENT_TYPE]);

if let Err(e) = Server::builder()
.layer(cors)
.layer(metrics_layer)
.add_service(op_pool_server)
.add_service(reflection_service)
Expand Down
1 change: 1 addition & 0 deletions crates/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ tokio.workspace = true
tokio-util.workspace = true
tonic.workspace = true
tower.workspace = true
tower-http = { workspace = true, features = ["cors"] }
tracing.workspace = true
url.workspace = true

Expand Down
14 changes: 14 additions & 0 deletions crates/rpc/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::{net::SocketAddr, sync::Arc, time::Duration};

use anyhow::Context;
use futures_util::FutureExt;
use http::header::CONTENT_TYPE;
use jsonrpsee::{
server::{middleware::http::ProxyGetRequestLayer, RpcServiceBuilder, ServerBuilder},
RpcModule,
Expand All @@ -29,6 +30,7 @@ use rundler_task::{
TaskSpawner,
};
use rundler_types::{builder::Builder as BuilderT, chain::ChainSpec, pool::Pool as PoolT};
use tower_http::cors::Any;
use tracing::info;

use crate::{
Expand Down Expand Up @@ -75,6 +77,8 @@ pub struct Args {
pub entry_point_v0_6_enabled: bool,
/// Whether to enable entry point v0.7.
pub entry_point_v0_7_enabled: bool,
/// Whether to enable corsdomain
pub corsdomain: bool,
}

/// JSON-RPC server task.
Expand Down Expand Up @@ -189,6 +193,16 @@ where

// Set up health check endpoint via GET /health registers the jsonrpc handler
let http_middleware = tower::ServiceBuilder::new()
.option_layer(self.args.corsdomain.then(|| {
tower::ServiceBuilder::new().layer(
tower_http::cors::CorsLayer::new()
// allow `GET` and `POST` when accessing the resource
.allow_methods([http::Method::GET, http::Method::POST])
// allow requests from any origin
.allow_origin(Any)
.allow_headers([CONTENT_TYPE]),
)
}))
// Proxy `GET /health` requests to internal `system_health` method.
.layer(ProxyGetRequestLayer::new("/health", "system_health")?)
.timeout(self.args.rpc_timeout)
Expand Down
2 changes: 2 additions & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ List of command line options for configuring the RPC API.
- env: *RPC_TIMEOUT_SECONDS*
- `--rpc.max_connections`: Maximum number of concurrent connections (default: `100`)
- env: *RPC_MAX_CONNECTIONS*
- `--rpc.corsdomain`: Enable the cors functionality on the server (default: `false`).
- env: *RPC_CORSDOMAIN*
- `--rpc.pool_url`: Pool URL for RPC (default: `http://localhost:50051`)
- env: *RPC_POOL_URL*
- *Only required when running in distributed mode*
Expand Down

0 comments on commit 5e948fb

Please sign in to comment.