Skip to content

Commit 5c084e6

Browse files
shohamc1prasannavl
andauthored
evm: Enable CORS on server (#2540)
* Enable CORS on server * Propagate error * Add tests --------- Co-authored-by: Prasanna Loganathar <pvl@prasannavl.com>
1 parent 366b897 commit 5c084e6

10 files changed

Lines changed: 232 additions & 16 deletions

File tree

lib/Cargo.lock

Lines changed: 152 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ hex-literal = "0.4"
6767
bincode = "1.3"
6868
serde_with = "3.0"
6969
heck = "0.4"
70+
tower-http = { version = "0.4.0", features = ["full"] }
71+
tower = "0.4.13"
72+
hyper = "0.14.20"
7073

7174
jsonrpsee = { version = "0.16", features = ["server", "macros", "http-client"] }
7275
jsonrpsee-core = "0.16"

lib/ain-cpp-imports/src/bridge.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,6 @@ pub mod ffi {
4848
fn getDST20Tokens(mnview_ptr: usize) -> Vec<DST20Token>;
4949
fn getClientVersion() -> String;
5050
fn getNumCores() -> i32;
51+
fn getCORSAllowedOrigin() -> String;
5152
}
5253
}

lib/ain-cpp-imports/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ mod ffi {
9494
pub fn getNumCores() -> i32 {
9595
unimplemented!("{}", UNIMPL_MSG)
9696
}
97+
pub fn getCORSAllowedOrigin() -> String {
98+
unimplemented!("{}", UNIMPL_MSG)
99+
}
97100
}
98101

99102
pub use ffi::Attributes;
@@ -196,5 +199,9 @@ pub fn get_num_cores() -> i32 {
196199
ffi::getNumCores()
197200
}
198201

202+
pub fn get_cors_allowed_origin() -> String {
203+
ffi::getCORSAllowedOrigin()
204+
}
205+
199206
#[cfg(test)]
200207
mod tests {}

lib/ain-grpc/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ rlp.workspace = true
2929
sha3.workspace = true
3030
serde_with.workspace = true
3131
anyhow.workspace = true
32-
32+
tower-http.workspace = true
33+
tower.workspace = true
34+
hyper.workspace = true
3335
[build-dependencies]
3436
cxx-gen.workspace = true
3537
heck.workspace = true

lib/ain-grpc/src/lib.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@ use std::{
2727

2828
use ain_evm::services::{Services, IS_SERVICES_INIT_CALL, SERVICES};
2929
use anyhow::{format_err, Result};
30+
use hyper::header::HeaderValue;
31+
use hyper::Method;
3032
use jsonrpsee::core::server::rpc_module::Methods;
3133
use jsonrpsee_server::ServerBuilder as HttpServerBuilder;
3234
use log::info;
3335
use logging::CppLogTarget;
36+
use tower_http::cors::CorsLayer;
3437

3538
use crate::rpc::{
3639
debug::{MetachainDebugRPCModule, MetachainDebugRPCServer},
@@ -72,9 +75,26 @@ pub fn init_network_json_rpc_service(runtime: &Services, addr: &str) -> Result<(
7275
let addr = addr.parse::<SocketAddr>()?;
7376
let max_connections = ain_cpp_imports::get_max_connections();
7477

78+
let middleware = if !ain_cpp_imports::get_cors_allowed_origin().is_empty() {
79+
info!(
80+
"Allowed origins: {}",
81+
ain_cpp_imports::get_cors_allowed_origin()
82+
);
83+
let cors = CorsLayer::new()
84+
.allow_methods([Method::POST, Method::GET, Method::OPTIONS])
85+
.allow_origin(ain_cpp_imports::get_cors_allowed_origin().parse::<HeaderValue>()?)
86+
.allow_headers([hyper::header::CONTENT_TYPE, hyper::header::AUTHORIZATION])
87+
.allow_credentials(true);
88+
89+
tower::ServiceBuilder::new().layer(cors)
90+
} else {
91+
tower::ServiceBuilder::new().layer(CorsLayer::new())
92+
};
93+
7594
let handle = runtime.tokio_runtime.clone();
7695
let server = runtime.tokio_runtime.block_on(
7796
HttpServerBuilder::default()
97+
.set_middleware(middleware)
7898
.max_connections(max_connections)
7999
.custom_tokio_runtime(handle)
80100
.build(addr),

src/ffi/ffiexports.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <key_io.h>
55
#include <logging.h>
66
#include <clientversion.h>
7+
#include <httprpc.h>
78

89
// TODO: Later switch this to u8 so we skip the
910
// conversion and is more efficient.
@@ -290,3 +291,7 @@ int32_t getNumCores() {
290291
const auto n = GetNumCores() - 1;
291292
return std::max(1, n);
292293
}
294+
295+
rust::string getCORSAllowedOrigin() {
296+
return gArgs.GetArg("-rpcallowcors", "");
297+
}

src/ffi/ffiexports.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <chainparams.h>
55
#include <ffi/cxx.h>
6+
#include <httprpc.h>
67

78
// Defaults for attributes relating to EVM functionality
89
static constexpr uint64_t DEFAULT_EVM_BLOCK_GAS_TARGET = 15000000;
@@ -68,5 +69,6 @@ void CppLogPrintf(rust::string message);
6869
rust::vec<DST20Token> getDST20Tokens(std::size_t mnview_ptr);
6970
rust::string getClientVersion();
7071
int32_t getNumCores();
72+
rust::string getCORSAllowedOrigin();
7173

7274
#endif // DEFI_FFI_FFIEXPORTS_H

0 commit comments

Comments
 (0)