diff --git a/crates/node-interaction/src/lib.rs b/crates/node-interaction/src/lib.rs index d73d838..2985cca 100644 --- a/crates/node-interaction/src/lib.rs +++ b/crates/node-interaction/src/lib.rs @@ -26,4 +26,9 @@ pub trait EthereumNode { /// Returns the ID of the chain that the node is on. fn chain_id(&self) -> anyhow::Result; + + // TODO: This is currently a u128 due to Kitchensink needing more than 64 bits for its gas limit + // when we implement the changes to the gas we need to adjust this to be a u64. + /// Returns the gas limit of the last block. + fn gas_limit(&self) -> anyhow::Result; } diff --git a/crates/node/src/geth.rs b/crates/node/src/geth.rs index e6f500a..cb506fa 100644 --- a/crates/node/src/geth.rs +++ b/crates/node/src/geth.rs @@ -14,6 +14,7 @@ use std::{ }; use alloy::{ + eips::BlockNumberOrTag, network::{Ethereum, EthereumWallet}, primitives::Address, providers::{ @@ -360,6 +361,19 @@ impl EthereumNode for Instance { provider.await?.get_chain_id().await.map_err(Into::into) })? } + + #[tracing::instrument(skip_all, fields(geth_node_id = self.id))] + fn gas_limit(&self) -> anyhow::Result { + let provider = self.provider(); + BlockingExecutor::execute(async move { + provider + .await? + .get_block_by_number(BlockNumberOrTag::Latest) + .await? + .ok_or(anyhow::Error::msg("Blockchain has no blocks")) + .map(|block| block.header.gas_limit as _) + })? + } } impl Node for Instance { @@ -502,4 +516,17 @@ mod tests { let chain_id = chain_id.expect("Failed to get the chain id"); assert_eq!(chain_id, 420_420_420); } + + #[test] + fn can_get_gas_limit_from_node() { + // Arrange + let (node, _temp_dir) = new_node(); + + // Act + let gas_limit = node.gas_limit(); + + // Assert + let gas_limit = gas_limit.expect("Failed to get the gas limit"); + assert_eq!(gas_limit, u32::MAX as u128) + } } diff --git a/crates/node/src/kitchensink.rs b/crates/node/src/kitchensink.rs index eca3a77..74e810a 100644 --- a/crates/node/src/kitchensink.rs +++ b/crates/node/src/kitchensink.rs @@ -13,6 +13,7 @@ use std::{ use alloy::{ consensus::{BlockHeader, TxEnvelope}, + eips::BlockNumberOrTag, hex, network::{ Ethereum, EthereumWallet, Network, TransactionBuilder, TransactionBuilderError, @@ -436,6 +437,19 @@ impl EthereumNode for KitchensinkNode { provider.await?.get_chain_id().await.map_err(Into::into) })? } + + #[tracing::instrument(skip_all, fields(geth_node_id = self.id))] + fn gas_limit(&self) -> anyhow::Result { + let provider = self.provider(); + BlockingExecutor::execute(async move { + provider + .await? + .get_block_by_number(BlockNumberOrTag::Latest) + .await? + .ok_or(anyhow::Error::msg("Blockchain has no blocks")) + .map(|block| block.header.gas_limit) + })? + } } impl Node for KitchensinkNode { @@ -1175,4 +1189,17 @@ mod tests { let chain_id = chain_id.expect("Failed to get the chain id"); assert_eq!(chain_id, 420_420_420); } + + #[test] + fn can_get_gas_limit_from_node() { + // Arrange + let (node, _temp_dir) = new_node(); + + // Act + let gas_limit = node.gas_limit(); + + // Assert + let gas_limit = gas_limit.expect("Failed to get the gas limit"); + assert_eq!(gas_limit, 52430300000000000000) + } }