Add a way to get the coinbase address

This commit is contained in:
Omar Abdulla
2025-07-14 22:48:45 +03:00
parent 61540741e1
commit 02547b62ee
3 changed files with 62 additions and 9 deletions
+10 -9
View File
@@ -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<TransactionReceipt>;
fn execute_transaction(&self, transaction: TransactionRequest) -> Result<TransactionReceipt>;
/// Trace the transaction in the [TransactionReceipt] and return a [GethTrace].
fn trace_transaction(&self, transaction: TransactionReceipt) -> anyhow::Result<GethTrace>;
fn trace_transaction(&self, transaction: TransactionReceipt) -> Result<GethTrace>;
/// Returns the state diff of the transaction hash in the [TransactionReceipt].
fn state_diff(&self, transaction: TransactionReceipt) -> anyhow::Result<DiffMode>;
fn state_diff(&self, transaction: TransactionReceipt) -> Result<DiffMode>;
/// Returns the next available nonce for the given [Address].
fn fetch_add_nonce(&self, address: Address) -> anyhow::Result<u64>;
fn fetch_add_nonce(&self, address: Address) -> Result<u64>;
/// Returns the ID of the chain that the node is on.
fn chain_id(&self) -> anyhow::Result<ChainId>;
fn chain_id(&self) -> Result<ChainId>;
// 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<u128>;
fn gas_limit(&self) -> Result<u128>;
/// Returns the coinbase of the last block.
fn coinbase(&self) -> Result<Address>;
}
+26
View File
@@ -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<Address> {
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]))
}
}
+26
View File
@@ -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<Address> {
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)
}
}