Integrate Config into admin_nodeInfo response (#7206)

This commit is contained in:
Thomas Coratger
2024-03-20 12:21:31 +01:00
committed by GitHub
parent c1f051a9dd
commit 0a803c49a9
7 changed files with 60 additions and 32 deletions

1
Cargo.lock generated
View File

@ -6539,6 +6539,7 @@ dependencies = [
name = "reth-rpc-types" name = "reth-rpc-types"
version = "0.2.0-beta.3" version = "0.2.0-beta.3"
dependencies = [ dependencies = [
"alloy-genesis",
"alloy-primitives", "alloy-primitives",
"alloy-rlp", "alloy-rlp",
"alloy-rpc-engine-types", "alloy-rpc-engine-types",

View File

@ -32,9 +32,10 @@ impl NetworkInfo for NoopNetwork {
protocol_version: ProtocolVersion::V5 as u64, protocol_version: ProtocolVersion::V5 as u64,
eth_protocol_info: EthProtocolInfo { eth_protocol_info: EthProtocolInfo {
difficulty: Default::default(), difficulty: Default::default(),
head: Default::default(),
network: 1, network: 1,
genesis: Default::default(), genesis: Default::default(),
config: Default::default(),
head: Default::default(),
}, },
}) })
} }

View File

@ -359,6 +359,7 @@ where
head: status.blockhash, head: status.blockhash,
network: status.chain.id(), network: status.chain.id(),
genesis: status.genesis, genesis: status.genesis,
config: Default::default(),
}, },
} }
} }

View File

@ -154,36 +154,22 @@
#![cfg_attr(not(test), warn(unused_crate_dependencies))] #![cfg_attr(not(test), warn(unused_crate_dependencies))]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] #![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 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::{ use crate::{
auth::AuthRpcModule, error::WsHttpSamePortError, metrics::RpcServerMetrics, auth::AuthRpcModule, error::WsHttpSamePortError, metrics::RpcServerMetrics,
RpcModuleSelection::Selection, RpcModuleSelection::Selection,
}; };
use constants::*; use constants::*;
use error::{RpcError, ServerKind}; use error::{RpcError, ServerKind};
use hyper::{header::AUTHORIZATION, HeaderMap};
pub use jsonrpsee::server::ServerBuilder;
use jsonrpsee::{
server::{IdProvider, Server, ServerHandle},
Methods, RpcModule,
};
use reth_ipc::server::IpcServer; use reth_ipc::server::IpcServer;
pub use reth_ipc::server::{Builder as IpcServerBuilder, Endpoint}; pub use reth_ipc::server::{Builder as IpcServerBuilder, Endpoint};
use reth_network_api::{noop::NoopNetwork, NetworkInfo, Peers}; use reth_network_api::{noop::NoopNetwork, NetworkInfo, Peers};
use reth_node_api::{ConfigureEvmEnv, EngineTypes};
use reth_provider::{ use reth_provider::{
AccountReader, BlockReader, BlockReaderIdExt, CanonStateSubscriptions, ChainSpecProvider, AccountReader, BlockReader, BlockReaderIdExt, CanonStateSubscriptions, ChainSpecProvider,
ChangeSetReader, EvmEnvProvider, StateProviderFactory, ChangeSetReader, EvmEnvProvider, StateProviderFactory,
@ -205,6 +191,19 @@ use reth_tasks::{
TaskSpawner, TokioTaskExecutor, TaskSpawner, TokioTaskExecutor,
}; };
use reth_transaction_pool::{noop::NoopTransactionPool, TransactionPool}; 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 // re-export for convenience
pub use crate::eth::{EthConfig, EthHandlers}; pub use crate::eth::{EthConfig, EthHandlers};
@ -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> RethModuleRegistry<Provider, Pool, Network, Tasks, Events, EvmConfig>
where where
Network: NetworkInfo + Peers + Clone + 'static, Network: NetworkInfo + Peers + Clone + 'static,
{ {
/// Instantiates AdminApi /// Instantiates AdminApi
pub fn admin_api(&mut self) -> AdminApi<Network> { pub fn admin_api(&mut self) -> AdminApi<Network> {
AdminApi::new(self.network.clone()) AdminApi::new(self.network.clone(), self.provider.chain_spec())
} }
/// Instantiates Web3Api /// Instantiates Web3Api
@ -1203,7 +1202,9 @@ where
.entry(namespace) .entry(namespace)
.or_insert_with(|| match namespace { .or_insert_with(|| match namespace {
RethRpcModule::Admin => { RethRpcModule::Admin => {
AdminApi::new(self.network.clone()).into_rpc().into() AdminApi::new(self.network.clone(), self.provider.chain_spec())
.into_rpc()
.into()
} }
RethRpcModule::Debug => DebugApi::new( RethRpcModule::Debug => DebugApi::new(
self.provider.clone(), self.provider.clone(),

View File

@ -20,6 +20,7 @@ alloy-rpc-trace-types.workspace = true
alloy-rpc-engine-types = { workspace = true, features = ["jsonrpsee-types"] } alloy-rpc-engine-types = { workspace = true, features = ["jsonrpsee-types"] }
ethereum_ssz_derive = { version = "0.5", optional = true } ethereum_ssz_derive = { version = "0.5", optional = true }
ethereum_ssz = { version = "0.5", optional = true } ethereum_ssz = { version = "0.5", optional = true }
alloy-genesis.workspace = true
# misc # misc
thiserror.workspace = true thiserror.workspace = true

View File

@ -1,4 +1,5 @@
use crate::{NodeRecord, PeerId}; use crate::{NodeRecord, PeerId};
use alloy_genesis::ChainConfig;
use alloy_primitives::{B256, U256}; use alloy_primitives::{B256, U256};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{ use std::{
@ -32,7 +33,7 @@ pub struct NodeInfo {
impl NodeInfo { impl NodeInfo {
/// Creates a new instance of `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 { NodeInfo {
enode: enr, enode: enr,
id: enr.id, id: enr.id,
@ -40,7 +41,10 @@ impl NodeInfo {
listen_addr: enr.tcp_addr(), listen_addr: enr.tcp_addr(),
ports: Ports { discovery: enr.udp_port, listener: enr.tcp_port }, ports: Ports { discovery: enr.udp_port, listener: enr.tcp_port },
name: status.client_version, 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(),
},
} }
} }
} }
@ -87,6 +91,21 @@ pub struct EthProtocolInfo {
pub network: u64, pub network: u64,
/// Genesis block of the current chain. /// Genesis block of the current chain.
pub genesis: B256, 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)] #[cfg(test)]
@ -95,7 +114,7 @@ mod tests {
#[test] #[test]
fn test_parse_node_info_roundtrip() { 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 info: NodeInfo = serde_json::from_str(sample).unwrap();
let serialized = serde_json::to_string_pretty(&info).unwrap(); let serialized = serde_json::to_string_pretty(&info).unwrap();

View File

@ -2,9 +2,10 @@ use crate::result::ToRpcResult;
use async_trait::async_trait; use async_trait::async_trait;
use jsonrpsee::core::RpcResult; use jsonrpsee::core::RpcResult;
use reth_network_api::{NetworkInfo, PeerKind, Peers}; use reth_network_api::{NetworkInfo, PeerKind, Peers};
use reth_primitives::NodeRecord; use reth_primitives::{ChainSpec, NodeRecord};
use reth_rpc_api::AdminApiServer; use reth_rpc_api::AdminApiServer;
use reth_rpc_types::{NodeInfo, PeerEthProtocolInfo, PeerInfo, PeerNetworkInfo, PeerProtocolsInfo}; use reth_rpc_types::{NodeInfo, PeerEthProtocolInfo, PeerInfo, PeerNetworkInfo, PeerProtocolsInfo};
use std::sync::Arc;
/// `admin` API implementation. /// `admin` API implementation.
/// ///
@ -12,12 +13,14 @@ use reth_rpc_types::{NodeInfo, PeerEthProtocolInfo, PeerInfo, PeerNetworkInfo, P
pub struct AdminApi<N> { pub struct AdminApi<N> {
/// An interface to interact with the network /// An interface to interact with the network
network: N, network: N,
/// The specification of the blockchain's configuration.
chain_spec: Arc<ChainSpec>,
} }
impl<N> AdminApi<N> { impl<N> AdminApi<N> {
/// Creates a new instance of `AdminApi`. /// Creates a new instance of `AdminApi`.
pub fn new(network: N) -> Self { pub fn new(network: N, chain_spec: Arc<ChainSpec>) -> Self {
AdminApi { network } AdminApi { network, chain_spec }
} }
} }
@ -83,8 +86,9 @@ where
async fn node_info(&self) -> RpcResult<NodeInfo> { async fn node_info(&self) -> RpcResult<NodeInfo> {
let enr = self.network.local_node_record(); let enr = self.network.local_node_record();
let status = self.network.network_status().await.to_rpc_result()?; 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` /// Handler for `admin_peerEvents`