Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat(l1): implement the net_version rpc method #1037

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions crates/networking/rpc/net/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use serde_json::Value;

use crate::{
utils::{RpcErr, RpcRequest},
RpcApiContext,
};

pub fn version(_req: &RpcRequest, context: RpcApiContext) -> Result<Value, RpcErr> {
let chain_spec = context
.storage
.get_chain_config()?;

let value = serde_json::to_value(format!("{}", chain_spec.chain_id))?;
Ok(value)
}
38 changes: 38 additions & 0 deletions crates/networking/rpc/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ mod admin;
mod authentication;
pub mod engine;
mod eth;
mod net;
pub mod types;
pub mod utils;
mod web3;
Expand Down Expand Up @@ -177,6 +178,7 @@ pub fn map_http_requests(req: &RpcRequest, context: RpcApiContext) -> Result<Val
Ok(RpcNamespace::Admin) => map_admin_requests(req, context),
Ok(RpcNamespace::Debug) => map_debug_requests(req, context),
Ok(RpcNamespace::Web3) => map_web3_requests(req, context),
Ok(RpcNamespace::Net) => map_net_requests(req, context),
_ => Err(RpcErr::MethodNotFound(req.method.clone())),
}
}
Expand Down Expand Up @@ -273,6 +275,13 @@ pub fn map_web3_requests(req: &RpcRequest, context: RpcApiContext) -> Result<Val
}
}

pub fn map_net_requests(req: &RpcRequest, contex: RpcApiContext) -> Result<Value, RpcErr> {
match req.method.as_str() {
"net_version" => net::version(req, contex),
unknown_net_method => Err(RpcErr::MethodNotFound(unknown_net_method.to_owned())),
}
}

fn rpc_response<E>(id: RpcRequestId, res: Result<Value, E>) -> Json<Value>
where
E: Into<RpcErrorMetadata>,
Expand Down Expand Up @@ -430,4 +439,33 @@ mod tests {
..Default::default()
}
}

#[test]
fn net_version_test() {
let body = r#"{"jsonrpc":"2.0","method":"net_version","params":[],"id":67}"#;
let request: RpcRequest = serde_json::from_str(body).expect("serde serialization failed");
// Setup initial storage
let storage =
Store::new("temp.db", EngineType::InMemory).expect("Failed to create test DB");
storage.set_chain_config(&example_chain_config()).unwrap();
let chain_id = storage
.get_chain_config()
.expect("failed to get chain_id")
.chain_id
.to_string();
let local_p2p_node = example_p2p_node();
let context = RpcApiContext {
storage,
local_p2p_node,
jwt_secret: Default::default(),
active_filters: Default::default(),
};
// Process request
let result = map_http_requests(&request, context);
let response = rpc_response(request.id, result);
let expected_response_string =
format!(r#"{{"id":67,"jsonrpc": "2.0","result": "{}"}}"#, chain_id);
let expected_response = to_rpc_response_success_value(&expected_response_string);
assert_eq!(response.to_string(), expected_response.to_string());
}
}
2 changes: 2 additions & 0 deletions crates/networking/rpc/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ pub enum RpcNamespace {
Admin,
Debug,
Web3,
Net,
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -170,6 +171,7 @@ impl RpcRequest {
"admin" => Ok(RpcNamespace::Admin),
"debug" => Ok(RpcNamespace::Debug),
"web3" => Ok(RpcNamespace::Web3),
"net" => Ok(RpcNamespace::Net),
_ => Err(RpcErr::MethodNotFound(self.method.clone())),
}
} else {
Expand Down