Skip to content

feat: add --http.corsdomain#1305

Merged
mattsse merged 11 commits intoparadigmxyz:mainfrom
Tirthnp:main
Feb 21, 2023
Merged

feat: add --http.corsdomain#1305
mattsse merged 11 commits intoparadigmxyz:mainfrom
Tirthnp:main

Conversation

@Tirthnp
Copy link
Contributor

@Tirthnp Tirthnp commented Feb 13, 2023

Closes #1230

@Tirthnp
Copy link
Contributor Author

Tirthnp commented Feb 13, 2023

I have created a draft version as it's a first pass. I just want to know if I am doing it right. I think we need to add support for --http.corsdomain * and --http.cordomain. I think we need to have an enum RpcCorsConfig. Can someone please give me some guidance on it?

@Tirthnp Tirthnp changed the title first pass at adding the http.corsdomain #1230 first pass at adding the http.corsdomain Feb 13, 2023
@onbjerg onbjerg added C-enhancement New feature or request A-rpc Related to the RPC implementation labels Feb 13, 2023
@onbjerg
Copy link
Collaborator

onbjerg commented Feb 13, 2023

Hey! When opening up a PR that closes an issue, you can use special words to link the PR and the issue, which auto-closes the issue when it is merged. For example:

Closes #1 or fixes #1

It helps keep the issue tracker clean 😄 Thanks for taking this on!

@onbjerg onbjerg changed the title first pass at adding the http.corsdomain feat: add --http.corsdomain Feb 13, 2023
Copy link
Collaborator

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great!

left some suggestions

}

// Confiugures the http server with cors domain
pub fn with_http_cors_domain(mut self, config: ServerBuilder, domains: Vec<&str>) -> Self {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather have this as a separate field (cors_domans: Option<...>)and then this is created in build

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please elaborate on this? I am a bit confused

Comment on lines +144 to +154
if let Some(domains) = self.rpc.http_corsdomain.as_ref() {
let domains_vec = domains.split(",").collect::<Vec<&str>>();
let _rpc_server = reth_rpc_builder::launch(
ShareableDatabase::new(db.clone()),
reth_transaction_pool::test_utils::testing_pool(),
network.clone(),
TransportRpcModuleConfig::default()
.with_http(vec![RethRpcModule::Admin, RethRpcModule::Eth]),
RpcServerConfig::default().with_http_cors_domain(Default::default(),domains_vec),
)
.await?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should leave this entirely up to the rpc builder.
It should handle Option<domain>

@Tirthnp
Copy link
Contributor Author

Tirthnp commented Feb 16, 2023

I have made some changes as per the comments but I am not exactly sure how to test them

@Tirthnp Tirthnp requested a review from mattsse February 16, 2023 01:30
Copy link
Collaborator

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

left some suggestions

.with_http(vec![RethRpcModule::Admin, RethRpcModule::Eth]),
RpcServerConfig::default().with_http(Default::default()),
RpcServerConfig::default()
.with_http(Default::default(), self.rpc.http_corsdomain.as_ref()),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the corsdomain should be configured in a separate function.
with_http should only take the server config imo.

Comment on lines +506 to +507
/// Configs for JSON-RPC that allow Corsdomains
http_cors_domain_server_config: Option<ServerBuilder<Stack<CorsLayer, Identity>>>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

separate for cors domain only

Comment on lines +539 to +550
if let Some(domains) = domains {
match domains.as_str() {
"*" => {
let cors = CorsLayer::new()
.allow_methods([Method::GET, Method::POST])
.allow_origin(Any)
.allow_headers(Any);

let middleware = tower::ServiceBuilder::new().layer(cors);
self.http_cors_domain_server_config =
Some(config.set_middleware(middleware).http_only());
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all of this needs to be set when the server is started

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah @Tirthnp what you want here is to just set the config variable, and then in the build/start function you actually consume them

Comment on lines +539 to +550
if let Some(domains) = domains {
match domains.as_str() {
"*" => {
let cors = CorsLayer::new()
.allow_methods([Method::GET, Method::POST])
.allow_origin(Any)
.allow_headers(Any);

let middleware = tower::ServiceBuilder::new().layer(cors);
self.http_cors_domain_server_config =
Some(config.set_middleware(middleware).http_only());
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah @Tirthnp what you want here is to just set the config variable, and then in the build/start function you actually consume them

/// Configures the http server
pub fn with_http(mut self, config: ServerBuilder) -> Self {
self.http_server_config = Some(config.http_only());
pub fn with_http(mut self, config: ServerBuilder, domains: Option<&String>) -> Self {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The domains method should be separate, so one should be able to do with_http(config).with_cors(Some("*"))

Copy link
Collaborator

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great progress.

mostly style nits, otherwise this is pretty close.

Comment on lines +623 to +642
match domains.as_str() {
"*" => {
cors = Some(CorsLayer::new()
.allow_methods([Method::GET, Method::POST])
.allow_origin(Any)
.allow_headers(Any));
}
"" => {}
_ => {
let domains_vec = domains.split(",").collect::<Vec<&str>>();

let origins = domains_vec
.into_iter()
.map(|domain| domain.parse().unwrap())
.collect::<Vec<HeaderValue>>();

cors = Some(CorsLayer::new()
.allow_methods([Method::GET, Method::POST])
.allow_origin(origins)
.allow_headers(Any));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please extract this to a standalone function that returns Result<Option<CorsLayer>

Comment on lines +632 to +636
let domains_vec = domains.split(",").collect::<Vec<&str>>();

let origins = domains_vec
.into_iter()
.map(|domain| domain.parse().unwrap())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be combined into domains.split(",").into_iter()

Comment on lines +637 to +638
.collect::<Vec<HeaderValue>>();

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

once move to function, collect into Result:

Suggested change
.collect::<Vec<HeaderValue>>();
.collect::<Result<Vec<HeaderValue>,_>();

Comment on lines +647 to +648
match cors {
Some(cors) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:
use if let Some() else instead of match

Comment on lines +796 to +802
/// Http Servers Enum
pub enum HttpServer {
/// Http server
Plain(Server),
/// Http server with cors
WithCors(Server<Stack<CorsLayer, Identity>>),
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is okay for now, perhaps we find a better solution but for now this is ok

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could dyn dispatch

Copy link
Member

@gakonst gakonst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good with me, couple nits which we can do in follow up

Comment on lines +671 to +674
let origins = domains
.split(",")
.map(|domain| domain.parse::<HeaderValue>())
.collect::<Result<Vec<HeaderValue>, _>>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit I'd add a CoraDomains type that impls FromStr and includes this logic, and do Option in the upstream cli code

Comment on lines +622 to +632
if let Some(cors) = cors {
let middleware = tower::ServiceBuilder::new().layer(cors);
let http_server =
builder.set_middleware(middleware).build(http_socket_addr).await?;
server.http_local_addr = http_server.local_addr().ok();
server.http = Some(HttpServer::WithCors(http_server));
} else {
let http_server = builder.build(http_socket_addr).await?;
server.http_local_addr = http_server.local_addr().ok();
server.http = Some(HttpServer::Plain(http_server));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Towers middlewares are so powerful. We should investigate what else is available @mattsse may let us do nice rate limiting etc features in the node

@gakonst gakonst marked this pull request as ready for review February 21, 2023 04:20
@gakonst gakonst requested a review from onbjerg as a code owner February 21, 2023 04:20
@codecov-commenter
Copy link

codecov-commenter commented Feb 21, 2023

Codecov Report

Merging #1305 (46e2bcf) into main (eb12991) will decrease coverage by 0.18%.
The diff coverage is 67.02%.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

@@            Coverage Diff             @@
##             main    #1305      +/-   ##
==========================================
- Coverage   76.23%   76.05%   -0.18%     
==========================================
  Files         357      358       +1     
  Lines       41118    42303    +1185     
==========================================
+ Hits        31347    32175     +828     
- Misses       9771    10128     +357     
Flag Coverage Δ
integration-tests 22.00% <9.18%> (-0.36%) ⬇️
unit-tests 70.49% <57.83%> (+0.05%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
bin/reth/src/node/mod.rs 0.00% <0.00%> (ø)
crates/rpc/rpc-builder/src/lib.rs 65.53% <52.17%> (-0.38%) ⬇️
bin/reth/src/args/rpc_server_args.rs 71.64% <80.58%> (+29.70%) ⬆️
crates/primitives/src/jsonu256.rs 93.67% <100.00%> (+0.24%) ⬆️
crates/primitives/src/bloom.rs 79.04% <0.00%> (-19.14%) ⬇️
crates/primitives/src/block.rs 78.53% <0.00%> (-4.20%) ⬇️
crates/net/network/src/session/mod.rs 76.36% <0.00%> (-1.37%) ⬇️
crates/net/network/src/eth_requests.rs 75.96% <0.00%> (-0.23%) ⬇️
crates/net/discv4/src/lib.rs 66.08% <0.00%> (-0.15%) ⬇️
crates/rpc/rpc/src/eth/api/server.rs 98.96% <0.00%> (-0.01%) ⬇️
... and 16 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Copy link
Collaborator

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm,

please enable edits by maintainer next PR

@mattsse mattsse merged commit 42b1fc1 into paradigmxyz:main Feb 21, 2023
mattsse pushed a commit that referenced this pull request Feb 21, 2023
@Tirthnp
Copy link
Contributor Author

Tirthnp commented Feb 21, 2023

please enable edits by maintainer next PR

I will, Thanks you so much for all the help :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-rpc Related to the RPC implementation C-enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add CORS domain support

5 participants