diff --git a/crates/node-interaction/src/lib.rs b/crates/node-interaction/src/lib.rs index 2985cca..f054842 100644 --- a/crates/node-interaction/src/lib.rs +++ b/crates/node-interaction/src/lib.rs @@ -3,6 +3,7 @@ use alloy::primitives::{Address, ChainId}; use alloy::rpc::types::trace::geth::{DiffMode, GethTrace}; use alloy::rpc::types::{TransactionReceipt, TransactionRequest}; +use anyhow::Result; mod blocking_executor; pub use blocking_executor::*; @@ -10,25 +11,25 @@ pub use blocking_executor::*; /// An interface for all interactions with Ethereum compatible nodes. pub trait EthereumNode { /// Execute the [TransactionRequest] and return a [TransactionReceipt]. - fn execute_transaction( - &self, - transaction: TransactionRequest, - ) -> anyhow::Result; + fn execute_transaction(&self, transaction: TransactionRequest) -> Result; /// Trace the transaction in the [TransactionReceipt] and return a [GethTrace]. - fn trace_transaction(&self, transaction: TransactionReceipt) -> anyhow::Result; + fn trace_transaction(&self, transaction: TransactionReceipt) -> Result; /// Returns the state diff of the transaction hash in the [TransactionReceipt]. - fn state_diff(&self, transaction: TransactionReceipt) -> anyhow::Result; + fn state_diff(&self, transaction: TransactionReceipt) -> Result; /// Returns the next available nonce for the given [Address]. - fn fetch_add_nonce(&self, address: Address) -> anyhow::Result; + fn fetch_add_nonce(&self, address: Address) -> Result; /// Returns the ID of the chain that the node is on. - fn chain_id(&self) -> anyhow::Result; + fn chain_id(&self) -> 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; + fn gas_limit(&self) -> Result; + + /// Returns the coinbase of the last block. + fn coinbase(&self) -> Result
; } diff --git a/crates/node/src/geth.rs b/crates/node/src/geth.rs index cb506fa..0a18e6f 100644 --- a/crates/node/src/geth.rs +++ b/crates/node/src/geth.rs @@ -374,6 +374,19 @@ impl EthereumNode for Instance { .map(|block| block.header.gas_limit as _) })? } + + #[tracing::instrument(skip_all, fields(geth_node_id = self.id))] + fn coinbase(&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.beneficiary) + })? + } } impl Node for Instance { @@ -529,4 +542,17 @@ mod tests { let gas_limit = gas_limit.expect("Failed to get the gas limit"); assert_eq!(gas_limit, u32::MAX as u128) } + + #[test] + fn can_get_coinbase_from_node() { + // Arrange + let (node, _temp_dir) = new_node(); + + // Act + let coinbase = node.coinbase(); + + // Assert + let coinbase = coinbase.expect("Failed to get the gas limit"); + assert_eq!(coinbase, Address::new([0xFF; 20])) + } } diff --git a/crates/node/src/kitchensink.rs b/crates/node/src/kitchensink.rs index 74e810a..f055ae1 100644 --- a/crates/node/src/kitchensink.rs +++ b/crates/node/src/kitchensink.rs @@ -450,6 +450,19 @@ impl EthereumNode for KitchensinkNode { .map(|block| block.header.gas_limit) })? } + + #[tracing::instrument(skip_all, fields(geth_node_id = self.id))] + fn coinbase(&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.beneficiary) + })? + } } impl Node for KitchensinkNode { @@ -1202,4 +1215,17 @@ mod tests { let gas_limit = gas_limit.expect("Failed to get the gas limit"); assert_eq!(gas_limit, 52430300000000000000) } + + #[test] + fn can_get_coinbase_from_node() { + // Arrange + let (node, _temp_dir) = new_node(); + + // Act + let coinbase = node.coinbase(); + + // Assert + let coinbase = coinbase.expect("Failed to get the gas limit"); + assert_eq!(coinbase, Address::ZERO) + } }