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

Integrate Config into admin_nodeInfo response #7206

Merged
merged 4 commits into from Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.

1 change: 1 addition & 0 deletions crates/net/network-api/src/noop.rs
Expand Up @@ -35,6 +35,7 @@ impl NetworkInfo for NoopNetwork {
head: Default::default(),
network: 1,
genesis: Default::default(),
config: Default::default(),
tcoratger marked this conversation as resolved.
Show resolved Hide resolved
},
})
}
Expand Down
1 change: 1 addition & 0 deletions crates/net/network/src/manager.rs
Expand Up @@ -359,6 +359,7 @@ where
head: status.blockhash,
network: status.chain.id(),
genesis: status.genesis,
config: Default::default(),
},
}
}
Expand Down
52 changes: 27 additions & 25 deletions crates/rpc/rpc-builder/src/lib.rs
Expand Up @@ -154,36 +154,22 @@
#![cfg_attr(not(test), warn(unused_crate_dependencies))]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

use std::{
collections::{HashMap, HashSet},
fmt,
net::{Ipv4Addr, SocketAddr, SocketAddrV4},
str::FromStr,
time::{Duration, SystemTime, UNIX_EPOCH},
use crate::{
auth::AuthRpcModule, error::WsHttpSamePortError, metrics::RpcServerMetrics,
RpcModuleSelection::Selection,
};

use constants::*;
use error::{RpcError, ServerKind};
use hyper::{header::AUTHORIZATION, HeaderMap};
pub use jsonrpsee::server::ServerBuilder;
use jsonrpsee::{
server::{IdProvider, Server, ServerHandle},
Methods, RpcModule,
};
use reth_node_api::{ConfigureEvmEnv, EngineTypes};
use serde::{Deserialize, Serialize, Serializer};
use strum::{AsRefStr, EnumIter, IntoStaticStr, ParseError, VariantArray, VariantNames};
use tower::layer::util::{Identity, Stack};
use tower_http::cors::CorsLayer;
use tracing::{instrument, trace};

use crate::{
auth::AuthRpcModule, error::WsHttpSamePortError, metrics::RpcServerMetrics,
RpcModuleSelection::Selection,
};
use constants::*;
use error::{RpcError, ServerKind};
use reth_ipc::server::IpcServer;
pub use reth_ipc::server::{Builder as IpcServerBuilder, Endpoint};
use reth_network_api::{noop::NoopNetwork, NetworkInfo, Peers};
use reth_node_api::{ConfigureEvmEnv, EngineTypes};
use reth_provider::{
AccountReader, BlockReader, BlockReaderIdExt, CanonStateSubscriptions, ChainSpecProvider,
ChangeSetReader, EvmEnvProvider, StateProviderFactory,
Expand All @@ -205,6 +191,19 @@ use reth_tasks::{
TaskSpawner, TokioTaskExecutor,
};
use reth_transaction_pool::{noop::NoopTransactionPool, TransactionPool};
use serde::{Deserialize, Serialize, Serializer};
use std::{
collections::{HashMap, HashSet},
fmt,
net::{Ipv4Addr, SocketAddr, SocketAddrV4},
str::FromStr,
time::{Duration, SystemTime, UNIX_EPOCH},
};
use strum::{AsRefStr, EnumIter, IntoStaticStr, ParseError, VariantArray, VariantNames};
use tower::layer::util::{Identity, Stack};
use tower_http::cors::CorsLayer;
use tracing::{instrument, trace};

// re-export for convenience
pub use crate::eth::{EthConfig, EthHandlers};

Expand Down Expand Up @@ -1015,14 +1014,14 @@ impl<Provider, Pool, Network, Tasks, Events, EvmConfig>
}
}

impl<Provider, Pool, Network, Tasks, Events, EvmConfig>
impl<Provider: ChainSpecProvider, Pool, Network, Tasks, Events, EvmConfig>
RethModuleRegistry<Provider, Pool, Network, Tasks, Events, EvmConfig>
where
Network: NetworkInfo + Peers + Clone + 'static,
{
/// Instantiates AdminApi
pub fn admin_api(&mut self) -> AdminApi<Network> {
AdminApi::new(self.network.clone())
AdminApi::new(self.network.clone(), (*self.provider.chain_spec()).clone())
}

/// Instantiates Web3Api
Expand Down Expand Up @@ -1202,9 +1201,12 @@ where
self.modules
.entry(namespace)
.or_insert_with(|| match namespace {
RethRpcModule::Admin => {
AdminApi::new(self.network.clone()).into_rpc().into()
}
RethRpcModule::Admin => AdminApi::new(
self.network.clone(),
(*self.provider.chain_spec()).clone(),
)
.into_rpc()
.into(),
RethRpcModule::Debug => DebugApi::new(
self.provider.clone(),
eth_api.clone(),
Expand Down
1 change: 1 addition & 0 deletions crates/rpc/rpc-types/Cargo.toml
Expand Up @@ -20,6 +20,7 @@ alloy-rpc-trace-types.workspace = true
alloy-rpc-engine-types = { workspace = true, features = ["jsonrpsee-types"] }
ethereum_ssz_derive = { version = "0.5", optional = true }
ethereum_ssz = { version = "0.5", optional = true }
alloy-genesis.workspace = true

# misc
thiserror.workspace = true
Expand Down
25 changes: 22 additions & 3 deletions crates/rpc/rpc-types/src/admin.rs
@@ -1,4 +1,5 @@
use crate::{NodeRecord, PeerId};
use alloy_genesis::ChainConfig;
use alloy_primitives::{B256, U256};
use serde::{Deserialize, Serialize};
use std::{
Expand Down Expand Up @@ -32,15 +33,18 @@ pub struct NodeInfo {

impl NodeInfo {
/// Creates a new instance of `NodeInfo`.
pub fn new(enr: NodeRecord, status: NetworkStatus) -> NodeInfo {
pub fn new(enr: NodeRecord, status: NetworkStatus, config: ChainConfig) -> NodeInfo {
NodeInfo {
enode: enr,
id: enr.id,
ip: enr.address,
listen_addr: enr.tcp_addr(),
ports: Ports { discovery: enr.udp_port, listener: enr.tcp_port },
name: status.client_version,
protocols: Protocols { eth: status.eth_protocol_info, other: Default::default() },
protocols: Protocols {
eth: EthProtocolInfo::new(status.eth_protocol_info, config),
other: Default::default(),
},
}
}
}
Expand Down Expand Up @@ -87,6 +91,21 @@ pub struct EthProtocolInfo {
pub network: u64,
/// Genesis block of the current chain.
pub genesis: B256,
/// Configuration of the chain.
pub config: ChainConfig,
}

impl EthProtocolInfo {
/// Creates a new instance of `EthProtocolInfo`.
pub fn new(info: EthProtocolInfo, config: ChainConfig) -> EthProtocolInfo {
EthProtocolInfo {
difficulty: info.difficulty,
head: info.head,
network: info.network,
genesis: info.genesis,
config,
}
}
}

#[cfg(test)]
Expand All @@ -95,7 +114,7 @@ mod tests {

#[test]
fn test_parse_node_info_roundtrip() {
let sample = r#"{"enode":"enode://44826a5d6a55f88a18298bca4773fca5749cdc3a5c9f308aa7d810e9b31123f3e7c5fba0b1d70aac5308426f47df2a128a6747040a3815cc7dd7167d03be320d@[::]:30303","id":"44826a5d6a55f88a18298bca4773fca5749cdc3a5c9f308aa7d810e9b31123f3e7c5fba0b1d70aac5308426f47df2a128a6747040a3815cc7dd7167d03be320d","ip":"::","listenAddr":"[::]:30303","name":"reth","ports":{"discovery":30303,"listener":30303},"protocols":{"eth":{"difficulty":17334254859343145000,"genesis":"0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3","head":"0xb83f73fbe6220c111136aefd27b160bf4a34085c65ba89f24246b3162257c36a","network":1}}}"#;
let sample = r#"{"enode":"enode://44826a5d6a55f88a18298bca4773fca5749cdc3a5c9f308aa7d810e9b31123f3e7c5fba0b1d70aac5308426f47df2a128a6747040a3815cc7dd7167d03be320d@[::]:30303","id":"44826a5d6a55f88a18298bca4773fca5749cdc3a5c9f308aa7d810e9b31123f3e7c5fba0b1d70aac5308426f47df2a128a6747040a3815cc7dd7167d03be320d","ip":"::","listenAddr":"[::]:30303","name":"reth","ports":{"discovery":30303,"listener":30303},"protocols":{"eth":{"difficulty":17334254859343145000,"genesis":"0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3","head":"0xb83f73fbe6220c111136aefd27b160bf4a34085c65ba89f24246b3162257c36a","network":1, "config": {"chainId": 17000,"homesteadBlock": 0,"daoForkSupport": true,"eip150Block": 0,"eip155Block": 0,"eip158Block": 0,"byzantiumBlock": 0,"constantinopleBlock": 0,"petersburgBlock": 0,"istanbulBlock": 0,"berlinBlock": 0,"londonBlock": 0,"shanghaiTime": 1696000704,"cancunTime": 1707305664,"terminalTotalDifficulty": 0,"terminalTotalDifficultyPassed": true,"ethash": {}}}}}"#;

let info: NodeInfo = serde_json::from_str(sample).unwrap();
let serialized = serde_json::to_string_pretty(&info).unwrap();
Expand Down
11 changes: 7 additions & 4 deletions crates/rpc/rpc/src/admin.rs
Expand Up @@ -2,7 +2,7 @@ use crate::result::ToRpcResult;
use async_trait::async_trait;
use jsonrpsee::core::RpcResult;
use reth_network_api::{NetworkInfo, PeerKind, Peers};
use reth_primitives::NodeRecord;
use reth_primitives::{ChainSpec, NodeRecord};
use reth_rpc_api::AdminApiServer;
use reth_rpc_types::{NodeInfo, PeerEthProtocolInfo, PeerInfo, PeerNetworkInfo, PeerProtocolsInfo};

Expand All @@ -12,12 +12,14 @@ use reth_rpc_types::{NodeInfo, PeerEthProtocolInfo, PeerInfo, PeerNetworkInfo, P
pub struct AdminApi<N> {
/// An interface to interact with the network
network: N,
/// The specification of the blockchain's configuration.
chain_spec: ChainSpec,
tcoratger marked this conversation as resolved.
Show resolved Hide resolved
}

impl<N> AdminApi<N> {
/// Creates a new instance of `AdminApi`.
pub fn new(network: N) -> Self {
AdminApi { network }
pub fn new(network: N, chain_spec: ChainSpec) -> Self {
tcoratger marked this conversation as resolved.
Show resolved Hide resolved
AdminApi { network, chain_spec }
}
}

Expand Down Expand Up @@ -83,8 +85,9 @@ where
async fn node_info(&self) -> RpcResult<NodeInfo> {
let enr = self.network.local_node_record();
let status = self.network.network_status().await.to_rpc_result()?;
let config = self.chain_spec.genesis().config.clone();

Ok(NodeInfo::new(enr, status))
Ok(NodeInfo::new(enr, status, config))
}

/// Handler for `admin_peerEvents`
Expand Down