Add ability to get the chain_id from node

This commit is contained in:
Omar Abdulla
2025-07-14 22:16:37 +03:00
parent 7d48d1600e
commit a4f5c4c8af
3 changed files with 69 additions and 3 deletions
+32 -1
View File
@@ -352,6 +352,14 @@ impl EthereumNode for Instance {
*current += 1;
Ok(value)
}
#[tracing::instrument(skip_all, fields(geth_node_id = self.id))]
fn chain_id(&self) -> anyhow::Result<alloy::primitives::ChainId> {
let provider = self.provider();
BlockingExecutor::execute(async move {
provider.await?.get_chain_id().await.map_err(Into::into)
})?
}
}
impl Node for Instance {
@@ -439,7 +447,7 @@ mod tests {
use crate::{GENESIS_JSON, Node};
use super::Instance;
use super::*;
fn test_config() -> (Arguments, TempDir) {
let mut config = Arguments::default();
@@ -449,6 +457,16 @@ mod tests {
(config, temp_dir)
}
fn new_node() -> (Instance, TempDir) {
let (args, temp_dir) = test_config();
let mut node = Instance::new(&args);
node.init(GENESIS_JSON.to_owned())
.expect("Failed to initialize the node")
.spawn_process()
.expect("Failed to spawn the node process");
(node, temp_dir)
}
#[test]
fn init_works() {
Instance::new(&test_config().0)
@@ -471,4 +489,17 @@ mod tests {
"expected version string, got: '{version}'"
);
}
#[test]
fn can_get_chain_id_from_node() {
// Arrange
let (node, _temp_dir) = new_node();
// Act
let chain_id = node.chain_id();
// Assert
let chain_id = chain_id.expect("Failed to get the chain id");
assert_eq!(chain_id, 420_420_420);
}
}
+33 -1
View File
@@ -229,6 +229,7 @@ impl KitchensinkNode {
Ok(())
}
#[tracing::instrument(skip_all, fields(kitchensink_node_id = self.id))]
fn extract_balance_from_genesis_file(
&self,
@@ -415,6 +416,14 @@ impl EthereumNode for KitchensinkNode {
*current += 1;
Ok(value)
}
#[tracing::instrument(skip_all, fields(geth_node_id = self.id))]
fn chain_id(&self) -> anyhow::Result<alloy::primitives::ChainId> {
let provider = self.provider();
BlockingExecutor::execute(async move {
provider.await?.get_chain_id().await.map_err(Into::into)
})?
}
}
impl Node for KitchensinkNode {
@@ -507,7 +516,7 @@ mod tests {
use std::fs;
use super::KitchensinkNode;
use super::*;
use crate::{GENESIS_JSON, Node};
fn test_config() -> (Arguments, TempDir) {
@@ -522,6 +531,16 @@ mod tests {
(config, temp_dir)
}
fn new_node() -> (KitchensinkNode, TempDir) {
let (args, temp_dir) = test_config();
let mut node = KitchensinkNode::new(&args);
node.init(GENESIS_JSON)
.expect("Failed to initialize the node")
.spawn_process()
.expect("Failed to spawn the node process");
(node, temp_dir)
}
#[test]
fn test_init_generates_chainspec_with_balances() {
let genesis_content = r#"
@@ -683,4 +702,17 @@ mod tests {
"Expected eth-rpc version string, got: {version}"
);
}
#[test]
fn can_get_chain_id_from_node() {
// Arrange
let (node, _temp_dir) = new_node();
// Act
let chain_id = node.chain_id();
// Assert
let chain_id = chain_id.expect("Failed to get the chain id");
assert_eq!(chain_id, 420_420_420);
}
}