Skip to content

Commit fb6ea8b

Browse files
jsvisamattsse
andauthored
feat(rpc/admin): compatible with geth's admin_nodeInfo (#9448)
Signed-off-by: jsvisa <delweng@gmail.com> Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
1 parent 4b8a66f commit fb6ea8b

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

crates/ethereum-forks/src/hardforks/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ impl ChainHardforks {
6767
self.map.get(fork.name()).copied()
6868
}
6969

70+
/// Retrieves the fork block number or timestamp from `fork` if it exists, otherwise `None`.
71+
pub fn fork_block<H: Hardfork>(&self, fork: H) -> Option<u64> {
72+
match self.fork(fork) {
73+
ForkCondition::Block(block) => Some(block),
74+
ForkCondition::TTD { fork_block, .. } => fork_block,
75+
ForkCondition::Timestamp(ts) => Some(ts),
76+
ForkCondition::Never => None,
77+
}
78+
}
79+
7080
/// Get an iterator of all hardforks with their respective activation conditions.
7181
pub fn forks_iter(&self) -> impl Iterator<Item = (&dyn Hardfork, ForkCondition)> {
7282
self.forks.iter().map(|(f, b)| (&**f, *b))

crates/rpc/rpc/src/admin.rs

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use jsonrpsee::core::RpcResult;
66
use reth_chainspec::ChainSpec;
77
use reth_network_api::{NetworkInfo, PeerKind, Peers};
88
use reth_network_peers::{id2pk, AnyNode, NodeRecord};
9+
use reth_primitives::EthereumHardfork;
910
use reth_rpc_api::AdminApiServer;
1011
use reth_rpc_server_types::ToRpcResult;
1112
use reth_rpc_types::admin::{
@@ -105,17 +106,56 @@ where
105106
async fn node_info(&self) -> RpcResult<NodeInfo> {
106107
let enode = self.network.local_node_record();
107108
let status = self.network.network_status().await.to_rpc_result()?;
108-
let config = ChainConfig {
109+
let mut config = ChainConfig {
109110
chain_id: self.chain_spec.chain.id(),
110111
terminal_total_difficulty_passed: self
111112
.chain_spec
112113
.get_final_paris_total_difficulty()
113114
.is_some(),
115+
terminal_total_difficulty: self
116+
.chain_spec
117+
.hardforks
118+
.fork(EthereumHardfork::Paris)
119+
.ttd(),
114120
..self.chain_spec.genesis().config.clone()
115121
};
116122

117-
let node_info = NodeInfo {
118-
id: enode.id.to_string(),
123+
// helper macro to set the block or time for a hardfork if known
124+
macro_rules! set_block_or_time {
125+
($config:expr, [$( $field:ident => $fork:ident,)*]) => {
126+
$(
127+
// don't overwrite if already set
128+
if $config.$field.is_none() {
129+
$config.$field = self.chain_spec.hardforks.fork_block(EthereumHardfork::$fork);
130+
}
131+
)*
132+
};
133+
}
134+
135+
set_block_or_time!(config, [
136+
homestead_block => Homestead,
137+
dao_fork_block => Dao,
138+
eip150_block => Tangerine,
139+
eip155_block => SpuriousDragon,
140+
eip158_block => SpuriousDragon,
141+
byzantium_block => Byzantium,
142+
constantinople_block => Constantinople,
143+
petersburg_block => Petersburg,
144+
istanbul_block => Istanbul,
145+
muir_glacier_block => MuirGlacier,
146+
berlin_block => Berlin,
147+
london_block => London,
148+
arrow_glacier_block => ArrowGlacier,
149+
gray_glacier_block => GrayGlacier,
150+
shanghai_time => Shanghai,
151+
cancun_time => Cancun,
152+
prague_time => Prague,
153+
]);
154+
155+
Ok(NodeInfo {
156+
id: id2pk(enode.id)
157+
.map(|pk| pk.to_string())
158+
.unwrap_or_else(|_| alloy_primitives::hex::encode(enode.id.as_slice())),
119159
name: status.client_version,
120160
enode: enode.to_string(),
121161
enr: self.network.local_enr().to_string(),
@@ -132,9 +172,7 @@ where
132172
}),
133173
snap: None,
134174
},
135-
};
136-
137-
Ok(node_info)
175+
})
138176
}
139177

140178
/// Handler for `admin_peerEvents`

0 commit comments

Comments
 (0)