Skip to content

Commit

Permalink
feat: enr string in admin endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcosNicolau committed Jan 29, 2025
1 parent 21c4a34 commit 5e4856e
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

9 changes: 8 additions & 1 deletion cmd/ethrex/ethrex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use ethrex_core::types::{Block, Genesis};
use ethrex_net::{
node_id_from_signing_key, peer_table,
sync::{SyncManager, SyncMode},
types::Node,
types::{Node, NodeRecord},
KademliaTable,
};
use ethrex_rlp::decode::RLPDecode;
Expand Down Expand Up @@ -224,6 +224,12 @@ async fn main() {
tcp_port: tcp_socket_addr.port(),
node_id: local_node_id,
};
let enr_seq = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
let local_node_record = NodeRecord::from_node(local_p2p_node, enr_seq, &signer)
.expect("Node record could not be created from local node");
// Create Kademlia Table here so we can access it from rpc server (for syncing)
let peer_table = peer_table(signer.clone());
// Create SyncManager
Expand All @@ -237,6 +243,7 @@ async fn main() {
store.clone(),
jwt_secret,
local_p2p_node,
local_node_record,
syncer,
)
.into_future();
Expand Down
1 change: 1 addition & 0 deletions crates/networking/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jsonwebtoken.workspace = true
rand.workspace = true
tokio-util.workspace = true
reqwest.workspace = true
k256 = { version = "0.13.3", features = ["ecdh"] }

[dev-dependencies]
hex-literal = "0.4.1"
Expand Down
11 changes: 9 additions & 2 deletions crates/networking/rpc/admin/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ethrex_core::types::ChainConfig;
use ethrex_net::types::Node;
use ethrex_net::types::{Node, NodeRecord};
use ethrex_storage::Store;
use serde::Serialize;
use serde_json::Value;
Expand All @@ -10,6 +10,7 @@ use crate::utils::RpcErr;
#[derive(Serialize, Debug)]
struct NodeInfo {
enode: String,
enr: String,
id: String,
ip: String,
name: String,
Expand All @@ -29,8 +30,13 @@ enum Protocol {
Eth(ChainConfig),
}

pub fn node_info(storage: Store, local_node: Node) -> Result<Value, RpcErr> {
pub fn node_info(
storage: Store,
local_node: Node,
local_node_record: NodeRecord,
) -> Result<Value, RpcErr> {
let enode_url = local_node.enode_url();
let enr_url = local_node_record.enr_url();
let mut protocols = HashMap::new();

let chain_config = storage
Expand All @@ -40,6 +46,7 @@ pub fn node_info(storage: Store, local_node: Node) -> Result<Value, RpcErr> {

let node_info = NodeInfo {
enode: enode_url,
enr: enr_url,
id: hex::encode(local_node.node_id),
name: "ethrex/0.1.0/rust1.81".to_string(),
ip: local_node.ip.to_string(),
Expand Down
17 changes: 14 additions & 3 deletions crates/networking/rpc/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use eth::{
GetTransactionByHashRequest, GetTransactionReceiptRequest,
},
};
use ethrex_net::sync::SyncManager;
use ethrex_net::{sync::SyncManager, types::NodeRecord};
use serde_json::Value;
use std::{
collections::HashMap,
Expand Down Expand Up @@ -70,6 +70,7 @@ pub struct RpcApiContext {
storage: Store,
jwt_secret: Bytes,
local_p2p_node: Node,
local_node_record: NodeRecord,
active_filters: ActiveFilters,
syncer: Arc<TokioMutex<SyncManager>>,
}
Expand Down Expand Up @@ -99,6 +100,7 @@ pub async fn start_api(
storage: Store,
jwt_secret: Bytes,
local_p2p_node: Node,
local_node_record: NodeRecord,
syncer: SyncManager,
) {
// TODO: Refactor how filters are handled,
Expand All @@ -108,6 +110,7 @@ pub async fn start_api(
storage: storage.clone(),
jwt_secret,
local_p2p_node,
local_node_record,
active_filters: active_filters.clone(),
syncer: Arc::new(TokioMutex::new(syncer)),
};
Expand Down Expand Up @@ -280,7 +283,11 @@ pub fn map_engine_requests(req: &RpcRequest, context: RpcApiContext) -> Result<V

pub fn map_admin_requests(req: &RpcRequest, context: RpcApiContext) -> Result<Value, RpcErr> {
match req.method.as_str() {
"admin_nodeInfo" => admin::node_info(context.storage, context.local_p2p_node),
"admin_nodeInfo" => admin::node_info(
context.storage,
context.local_p2p_node,
context.local_node_record,
),
unknown_admin_method => Err(RpcErr::MethodNotFound(unknown_admin_method.to_owned())),
}
}
Expand Down Expand Up @@ -326,7 +333,7 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::utils::test_utils::example_p2p_node;
use crate::utils::test_utils::{example_local_node_record, example_p2p_node};
use ethrex_core::types::{ChainConfig, Genesis};
use ethrex_storage::EngineType;
use std::fs::File;
Expand All @@ -348,6 +355,7 @@ mod tests {
storage.set_chain_config(&example_chain_config()).unwrap();
let context = RpcApiContext {
local_p2p_node,
local_node_record: example_local_node_record(),
storage,
jwt_secret: Default::default(),
active_filters: Default::default(),
Expand Down Expand Up @@ -386,6 +394,7 @@ mod tests {
// Process request
let context = RpcApiContext {
local_p2p_node,
local_node_record: example_local_node_record(),
storage,
jwt_secret: Default::default(),
active_filters: Default::default(),
Expand Down Expand Up @@ -416,6 +425,7 @@ mod tests {
// Process request
let context = RpcApiContext {
local_p2p_node,
local_node_record: example_local_node_record(),
storage,
jwt_secret: Default::default(),
active_filters: Default::default(),
Expand Down Expand Up @@ -477,6 +487,7 @@ mod tests {
let context = RpcApiContext {
storage,
local_p2p_node,
local_node_record: example_local_node_record(),
jwt_secret: Default::default(),
active_filters: Default::default(),
syncer: Arc::new(TokioMutex::new(SyncManager::dummy())),
Expand Down
20 changes: 19 additions & 1 deletion crates/networking/rpc/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,12 @@ pub mod test_utils {
use std::{net::SocketAddr, str::FromStr};

use ethrex_core::H512;
use ethrex_net::{sync::SyncManager, types::Node};
use ethrex_net::{
sync::SyncManager,
types::{Node, NodeRecord},
};
use ethrex_storage::{EngineType, Store};
use k256::ecdsa::SigningKey;

use crate::start_api;

Expand All @@ -271,6 +275,19 @@ pub mod test_utils {
}
}

pub fn example_local_node_record() -> NodeRecord {
let node_id_1 = H512::from_str("d860a01f9722d78051619d1e2351aba3f43f943f6f00718d1b9baa4101932a1f5011f16bb2b1bb35db20d6fe28fa0bf09636d26a87d31de9ec6203eeedb1f666").unwrap();
let node = Node {
ip: "127.0.0.1".parse().unwrap(),
udp_port: 30303,
tcp_port: 30303,
node_id: node_id_1,
};
let signer = SigningKey::random(&mut rand::rngs::OsRng);

NodeRecord::from_node(node, 0, &signer).unwrap()
}

// Util to start an api for testing on ports 8500 and 8501,
// mostly for when hive is missing some endpoints to test
// like eth_uninstallFilter.
Expand Down Expand Up @@ -299,6 +316,7 @@ pub mod test_utils {
storage,
jwt_secret,
local_p2p_node,
example_local_node_record(),
SyncManager::dummy(),
)
.await;
Expand Down

0 comments on commit 5e4856e

Please sign in to comment.