Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(network-primitives): expose more fields via block response traits #1229

Merged
merged 3 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion crates/network-primitives/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloy_primitives::{Address, BlockHash, Bytes, TxHash, U256};
use alloy_primitives::{Address, BlockHash, Bytes, TxHash, B256, U256};
use alloy_serde::WithOtherFields;

use crate::BlockTransactions;
Expand Down Expand Up @@ -67,6 +67,30 @@ pub trait HeaderResponse {

/// Blob fee for the next block (if EIP-4844 is supported)
fn next_block_blob_fee(&self) -> Option<u128>;

/// Coinbase/Miner of the block
fn coinbase(&self) -> Address;

/// Gas limit of the block
fn gas_limit(&self) -> u128;

/// Mix hash of the block
///
/// Before the merge this proves, combined with the nonce, that a sufficient amount of
/// computation has been carried out on this block: the Proof-of-Work (PoW).
///
/// After the merge this is `prevRandao`: Randomness value for the generated payload.
///
/// This is an Option because it is not always set by non-ethereum networks.
///
/// See also <https://eips.ethereum.org/EIPS/eip-4399>
/// And <https://github.com/ethereum/execution-apis/issues/328>
fn mix_hash(&self) -> Option<B256>;
yash-atreya marked this conversation as resolved.
Show resolved Hide resolved

/// Difficulty of the block
///
/// Unused after the Paris (AKA the merge) upgrade, and replaced by `prevrandao`.
fn difficulty(&self) -> U256;
}

/// Block JSON-RPC response.
Expand All @@ -84,6 +108,11 @@ pub trait BlockResponse {

/// Mutable reference to block transactions
fn transactions_mut(&mut self) -> &mut BlockTransactions<Self::Transaction>;

/// Returns the `other` field from `WithOtherFields` type.
fn other_fields(&self) -> Option<&alloy_serde::OtherFields> {
None
}
}

impl<T: TransactionResponse> TransactionResponse for WithOtherFields<T> {
Expand Down Expand Up @@ -145,6 +174,10 @@ impl<T: BlockResponse> BlockResponse for WithOtherFields<T> {
fn transactions_mut(&mut self) -> &mut BlockTransactions<Self::Transaction> {
self.inner.transactions_mut()
}

fn other_fields(&self) -> Option<&alloy_serde::OtherFields> {
Some(&self.other)
}
}

impl<T: HeaderResponse> HeaderResponse for WithOtherFields<T> {
Expand All @@ -171,4 +204,20 @@ impl<T: HeaderResponse> HeaderResponse for WithOtherFields<T> {
fn next_block_blob_fee(&self) -> Option<u128> {
self.inner.next_block_blob_fee()
}

fn coinbase(&self) -> Address {
self.inner.coinbase()
}

fn gas_limit(&self) -> u128 {
self.inner.gas_limit()
}

fn mix_hash(&self) -> Option<B256> {
self.inner.mix_hash()
}

fn difficulty(&self) -> U256 {
self.inner.difficulty()
}
}
16 changes: 16 additions & 0 deletions crates/rpc-types-eth/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,22 @@ impl HeaderResponse for Header {
fn next_block_blob_fee(&self) -> Option<u128> {
self.next_block_blob_fee()
}

fn coinbase(&self) -> Address {
self.miner
}

fn gas_limit(&self) -> u128 {
self.gas_limit
}

fn mix_hash(&self) -> Option<B256> {
self.mix_hash
}

fn difficulty(&self) -> U256 {
self.difficulty
}
}

/// Error that can occur when converting other types to blocks
Expand Down