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 2 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
39 changes: 38 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,18 @@ pub trait HeaderResponse {

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

/// Miner/Coinbase of the block
fn miner(&self) -> Address;
yash-atreya marked this conversation as resolved.
Show resolved Hide resolved

/// Gas limit of the block
fn gas(&self) -> u128;
yash-atreya marked this conversation as resolved.
Show resolved Hide resolved

/// Mix hash of the block
fn mix_hash(&self) -> Option<B256>;
yash-atreya marked this conversation as resolved.
Show resolved Hide resolved

/// Difficulty of the block
fn difficulty(&self) -> U256;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we don't need this, I assume this could eventually be removed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OP stack based L2s use block.difficulty for generating random values.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I guess block.prevrandao serves the same purpose on L2s

}

/// Block JSON-RPC response.
Expand All @@ -84,6 +96,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 +162,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 +192,20 @@ impl<T: HeaderResponse> HeaderResponse for WithOtherFields<T> {
fn next_block_blob_fee(&self) -> Option<u128> {
self.inner.next_block_blob_fee()
}

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

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

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 miner(&self) -> Address {
self.miner
}

fn gas(&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