Expose APIs for getting the info of a specific block

This commit is contained in:
Omar Abdulla
2025-07-14 23:21:53 +03:00
parent 68bda92465
commit 6d7cd67931
3 changed files with 41 additions and 40 deletions
+11 -10
View File
@@ -1,5 +1,6 @@
//! This crate implements all node interactions.
use alloy::eips::BlockNumberOrTag;
use alloy::primitives::{Address, BlockHash, BlockNumber, BlockTimestamp, ChainId, U256};
use alloy::rpc::types::trace::geth::{DiffMode, GethTrace};
use alloy::rpc::types::{TransactionReceipt, TransactionRequest};
@@ -27,20 +28,20 @@ pub trait EthereumNode {
// 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 block_gas_limit(&self) -> Result<u128>;
/// Returns the gas limit of the specified block.
fn block_gas_limit(&self, number: BlockNumberOrTag) -> Result<u128>;
/// Returns the coinbase of the last block.
fn block_coinbase(&self) -> Result<Address>;
/// Returns the coinbase of the specified block.
fn block_coinbase(&self, number: BlockNumberOrTag) -> Result<Address>;
/// Returns the difficulty of the last block.
fn block_difficulty(&self) -> Result<U256>;
/// Returns the difficulty of the specified block.
fn block_difficulty(&self, number: BlockNumberOrTag) -> Result<U256>;
/// Returns the hash of the last block.
fn block_hash(&self) -> Result<BlockHash>;
/// Returns the hash of the specified block.
fn block_hash(&self, number: BlockNumberOrTag) -> Result<BlockHash>;
/// Returns the timestamp of the last block,
fn block_timestamp(&self) -> Result<BlockTimestamp>;
/// Returns the timestamp of the specified block,
fn block_timestamp(&self, number: BlockNumberOrTag) -> Result<BlockTimestamp>;
/// Returns the number of the last block.
fn last_block_number(&self) -> Result<BlockNumber>;
+15 -15
View File
@@ -363,12 +363,12 @@ impl EthereumNode for Instance {
}
#[tracing::instrument(skip_all, fields(geth_node_id = self.id))]
fn block_gas_limit(&self) -> anyhow::Result<u128> {
fn block_gas_limit(&self, number: BlockNumberOrTag) -> anyhow::Result<u128> {
let provider = self.provider();
BlockingExecutor::execute(async move {
provider
.await?
.get_block_by_number(BlockNumberOrTag::Latest)
.get_block_by_number(number)
.await?
.ok_or(anyhow::Error::msg("Blockchain has no blocks"))
.map(|block| block.header.gas_limit as _)
@@ -376,12 +376,12 @@ impl EthereumNode for Instance {
}
#[tracing::instrument(skip_all, fields(geth_node_id = self.id))]
fn block_coinbase(&self) -> anyhow::Result<Address> {
fn block_coinbase(&self, number: BlockNumberOrTag) -> anyhow::Result<Address> {
let provider = self.provider();
BlockingExecutor::execute(async move {
provider
.await?
.get_block_by_number(BlockNumberOrTag::Latest)
.get_block_by_number(number)
.await?
.ok_or(anyhow::Error::msg("Blockchain has no blocks"))
.map(|block| block.header.beneficiary)
@@ -389,12 +389,12 @@ impl EthereumNode for Instance {
}
#[tracing::instrument(skip_all, fields(geth_node_id = self.id))]
fn block_difficulty(&self) -> anyhow::Result<U256> {
fn block_difficulty(&self, number: BlockNumberOrTag) -> anyhow::Result<U256> {
let provider = self.provider();
BlockingExecutor::execute(async move {
provider
.await?
.get_block_by_number(BlockNumberOrTag::Latest)
.get_block_by_number(number)
.await?
.ok_or(anyhow::Error::msg("Blockchain has no blocks"))
.map(|block| block.header.difficulty)
@@ -402,12 +402,12 @@ impl EthereumNode for Instance {
}
#[tracing::instrument(skip_all, fields(geth_node_id = self.id))]
fn block_hash(&self) -> anyhow::Result<BlockHash> {
fn block_hash(&self, number: BlockNumberOrTag) -> anyhow::Result<BlockHash> {
let provider = self.provider();
BlockingExecutor::execute(async move {
provider
.await?
.get_block_by_number(BlockNumberOrTag::Latest)
.get_block_by_number(number)
.await?
.ok_or(anyhow::Error::msg("Blockchain has no blocks"))
.map(|block| block.header.hash)
@@ -415,12 +415,12 @@ impl EthereumNode for Instance {
}
#[tracing::instrument(skip_all, fields(geth_node_id = self.id))]
fn block_timestamp(&self) -> anyhow::Result<BlockTimestamp> {
fn block_timestamp(&self, number: BlockNumberOrTag) -> anyhow::Result<BlockTimestamp> {
let provider = self.provider();
BlockingExecutor::execute(async move {
provider
.await?
.get_block_by_number(BlockNumberOrTag::Latest)
.get_block_by_number(number)
.await?
.ok_or(anyhow::Error::msg("Blockchain has no blocks"))
.map(|block| block.header.timestamp)
@@ -583,7 +583,7 @@ mod tests {
let (node, _temp_dir) = new_node();
// Act
let gas_limit = node.block_gas_limit();
let gas_limit = node.block_gas_limit(BlockNumberOrTag::Latest);
// Assert
let gas_limit = gas_limit.expect("Failed to get the gas limit");
@@ -596,7 +596,7 @@ mod tests {
let (node, _temp_dir) = new_node();
// Act
let coinbase = node.block_coinbase();
let coinbase = node.block_coinbase(BlockNumberOrTag::Latest);
// Assert
let coinbase = coinbase.expect("Failed to get the coinbase");
@@ -609,7 +609,7 @@ mod tests {
let (node, _temp_dir) = new_node();
// Act
let block_difficulty = node.block_difficulty();
let block_difficulty = node.block_difficulty(BlockNumberOrTag::Latest);
// Assert
let block_difficulty = block_difficulty.expect("Failed to get the block difficulty");
@@ -622,7 +622,7 @@ mod tests {
let (node, _temp_dir) = new_node();
// Act
let block_hash = node.block_hash();
let block_hash = node.block_hash(BlockNumberOrTag::Latest);
// Assert
let _ = block_hash.expect("Failed to get the block hash");
@@ -634,7 +634,7 @@ mod tests {
let (node, _temp_dir) = new_node();
// Act
let block_timestamp = node.block_timestamp();
let block_timestamp = node.block_timestamp(BlockNumberOrTag::Latest);
// Assert
let _ = block_timestamp.expect("Failed to get the block timestamp");
+15 -15
View File
@@ -439,12 +439,12 @@ impl EthereumNode for KitchensinkNode {
}
#[tracing::instrument(skip_all, fields(geth_node_id = self.id))]
fn block_gas_limit(&self) -> anyhow::Result<u128> {
fn block_gas_limit(&self, number: BlockNumberOrTag) -> anyhow::Result<u128> {
let provider = self.provider();
BlockingExecutor::execute(async move {
provider
.await?
.get_block_by_number(BlockNumberOrTag::Latest)
.get_block_by_number(number)
.await?
.ok_or(anyhow::Error::msg("Blockchain has no blocks"))
.map(|block| block.header.gas_limit)
@@ -452,12 +452,12 @@ impl EthereumNode for KitchensinkNode {
}
#[tracing::instrument(skip_all, fields(geth_node_id = self.id))]
fn block_coinbase(&self) -> anyhow::Result<Address> {
fn block_coinbase(&self, number: BlockNumberOrTag) -> anyhow::Result<Address> {
let provider = self.provider();
BlockingExecutor::execute(async move {
provider
.await?
.get_block_by_number(BlockNumberOrTag::Latest)
.get_block_by_number(number)
.await?
.ok_or(anyhow::Error::msg("Blockchain has no blocks"))
.map(|block| block.header.beneficiary)
@@ -465,12 +465,12 @@ impl EthereumNode for KitchensinkNode {
}
#[tracing::instrument(skip_all, fields(geth_node_id = self.id))]
fn block_difficulty(&self) -> anyhow::Result<U256> {
fn block_difficulty(&self, number: BlockNumberOrTag) -> anyhow::Result<U256> {
let provider = self.provider();
BlockingExecutor::execute(async move {
provider
.await?
.get_block_by_number(BlockNumberOrTag::Latest)
.get_block_by_number(number)
.await?
.ok_or(anyhow::Error::msg("Blockchain has no blocks"))
.map(|block| block.header.difficulty)
@@ -478,12 +478,12 @@ impl EthereumNode for KitchensinkNode {
}
#[tracing::instrument(skip_all, fields(geth_node_id = self.id))]
fn block_hash(&self) -> anyhow::Result<BlockHash> {
fn block_hash(&self, number: BlockNumberOrTag) -> anyhow::Result<BlockHash> {
let provider = self.provider();
BlockingExecutor::execute(async move {
provider
.await?
.get_block_by_number(BlockNumberOrTag::Latest)
.get_block_by_number(number)
.await?
.ok_or(anyhow::Error::msg("Blockchain has no blocks"))
.map(|block| block.header.hash)
@@ -491,12 +491,12 @@ impl EthereumNode for KitchensinkNode {
}
#[tracing::instrument(skip_all, fields(geth_node_id = self.id))]
fn block_timestamp(&self) -> anyhow::Result<BlockTimestamp> {
fn block_timestamp(&self, number: BlockNumberOrTag) -> anyhow::Result<BlockTimestamp> {
let provider = self.provider();
BlockingExecutor::execute(async move {
provider
.await?
.get_block_by_number(BlockNumberOrTag::Latest)
.get_block_by_number(number)
.await?
.ok_or(anyhow::Error::msg("Blockchain has no blocks"))
.map(|block| block.header.timestamp)
@@ -1256,7 +1256,7 @@ mod tests {
let (node, _temp_dir) = new_node();
// Act
let gas_limit = node.block_gas_limit();
let gas_limit = node.block_gas_limit(BlockNumberOrTag::Latest);
// Assert
let gas_limit = gas_limit.expect("Failed to get the gas limit");
@@ -1269,7 +1269,7 @@ mod tests {
let (node, _temp_dir) = new_node();
// Act
let coinbase = node.block_coinbase();
let coinbase = node.block_coinbase(BlockNumberOrTag::Latest);
// Assert
let coinbase = coinbase.expect("Failed to get the coinbase");
@@ -1282,7 +1282,7 @@ mod tests {
let (node, _temp_dir) = new_node();
// Act
let block_difficulty = node.block_difficulty();
let block_difficulty = node.block_difficulty(BlockNumberOrTag::Latest);
// Assert
let block_difficulty = block_difficulty.expect("Failed to get the block difficulty");
@@ -1295,7 +1295,7 @@ mod tests {
let (node, _temp_dir) = new_node();
// Act
let block_hash = node.block_hash();
let block_hash = node.block_hash(BlockNumberOrTag::Latest);
// Assert
let _ = block_hash.expect("Failed to get the block hash");
@@ -1307,7 +1307,7 @@ mod tests {
let (node, _temp_dir) = new_node();
// Act
let block_timestamp = node.block_timestamp();
let block_timestamp = node.block_timestamp(BlockNumberOrTag::Latest);
// Assert
let _ = block_timestamp.expect("Failed to get the block timestamp");