From 5ba3bfafdfc34976249e63de59bfb6f652fc30ae Mon Sep 17 00:00:00 2001 From: Yash Atreya <44857776+yash-atreya@users.noreply.github.com> Date: Tue, 3 Sep 2024 12:15:24 +0530 Subject: [PATCH 1/3] feat(network-primitives): expose more fields in HeaderResponse --- crates/network-primitives/src/traits.rs | 30 ++++++++++++++++++++++++- crates/rpc-types-eth/src/block.rs | 16 +++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/crates/network-primitives/src/traits.rs b/crates/network-primitives/src/traits.rs index 64fc692cfa6..28b75c350d3 100644 --- a/crates/network-primitives/src/traits.rs +++ b/crates/network-primitives/src/traits.rs @@ -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; @@ -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; + + /// Miner/Coinbase of the block + fn miner(&self) -> Address; + + /// Gas limit of the block + fn gas(&self) -> u128; + + /// Mix hash of the block + fn mix_hash(&self) -> Option; + + /// Difficulty of the block + fn difficulty(&self) -> U256; } /// Block JSON-RPC response. @@ -171,4 +183,20 @@ impl HeaderResponse for WithOtherFields { fn next_block_blob_fee(&self) -> Option { 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 { + self.inner.mix_hash() + } + + fn difficulty(&self) -> U256 { + self.inner.difficulty() + } } diff --git a/crates/rpc-types-eth/src/block.rs b/crates/rpc-types-eth/src/block.rs index 42474cdb549..87ccde07547 100644 --- a/crates/rpc-types-eth/src/block.rs +++ b/crates/rpc-types-eth/src/block.rs @@ -228,6 +228,22 @@ impl HeaderResponse for Header { fn next_block_blob_fee(&self) -> Option { self.next_block_blob_fee() } + + fn miner(&self) -> Address { + self.miner + } + + fn gas(&self) -> u128 { + self.gas_limit + } + + fn mix_hash(&self) -> Option { + self.mix_hash + } + + fn difficulty(&self) -> U256 { + self.difficulty + } } /// Error that can occur when converting other types to blocks From fb42b9f98d7e4e41cf107d090bbf54d09e9f6257 Mon Sep 17 00:00:00 2001 From: Yash Atreya <44857776+yash-atreya@users.noreply.github.com> Date: Tue, 3 Sep 2024 12:54:11 +0530 Subject: [PATCH 2/3] feat(network-primitives): expose `other` fields in BlockResponse --- crates/network-primitives/src/traits.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/crates/network-primitives/src/traits.rs b/crates/network-primitives/src/traits.rs index 28b75c350d3..1a9b7fd1277 100644 --- a/crates/network-primitives/src/traits.rs +++ b/crates/network-primitives/src/traits.rs @@ -96,6 +96,11 @@ pub trait BlockResponse { /// Mutable reference to block transactions fn transactions_mut(&mut self) -> &mut BlockTransactions; + + /// Returns the `other` field from `WithOtherFields` type. + fn other_fields(&self) -> Option<&alloy_serde::OtherFields> { + None + } } impl TransactionResponse for WithOtherFields { @@ -157,6 +162,10 @@ impl BlockResponse for WithOtherFields { fn transactions_mut(&mut self) -> &mut BlockTransactions { self.inner.transactions_mut() } + + fn other_fields(&self) -> Option<&alloy_serde::OtherFields> { + Some(&self.other) + } } impl HeaderResponse for WithOtherFields { From 0a9e0f08874dff82b54602b171bc0bc44cd38a3a Mon Sep 17 00:00:00 2001 From: Yash Atreya <44857776+yash-atreya@users.noreply.github.com> Date: Tue, 3 Sep 2024 16:51:15 +0530 Subject: [PATCH 3/3] nits --- crates/network-primitives/src/traits.rs | 26 ++++++++++++++++++------- crates/rpc-types-eth/src/block.rs | 4 ++-- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/crates/network-primitives/src/traits.rs b/crates/network-primitives/src/traits.rs index 1a9b7fd1277..de6f4788796 100644 --- a/crates/network-primitives/src/traits.rs +++ b/crates/network-primitives/src/traits.rs @@ -68,16 +68,28 @@ pub trait HeaderResponse { /// Blob fee for the next block (if EIP-4844 is supported) fn next_block_blob_fee(&self) -> Option; - /// Miner/Coinbase of the block - fn miner(&self) -> Address; + /// Coinbase/Miner of the block + fn coinbase(&self) -> Address; /// Gas limit of the block - fn gas(&self) -> u128; + 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 + /// And fn mix_hash(&self) -> Option; /// Difficulty of the block + /// + /// Unused after the Paris (AKA the merge) upgrade, and replaced by `prevrandao`. fn difficulty(&self) -> U256; } @@ -193,12 +205,12 @@ impl HeaderResponse for WithOtherFields { self.inner.next_block_blob_fee() } - fn miner(&self) -> Address { - self.inner.miner() + fn coinbase(&self) -> Address { + self.inner.coinbase() } - fn gas(&self) -> u128 { - self.inner.gas() + fn gas_limit(&self) -> u128 { + self.inner.gas_limit() } fn mix_hash(&self) -> Option { diff --git a/crates/rpc-types-eth/src/block.rs b/crates/rpc-types-eth/src/block.rs index 87ccde07547..5fd1cfa00e3 100644 --- a/crates/rpc-types-eth/src/block.rs +++ b/crates/rpc-types-eth/src/block.rs @@ -229,11 +229,11 @@ impl HeaderResponse for Header { self.next_block_blob_fee() } - fn miner(&self) -> Address { + fn coinbase(&self) -> Address { self.miner } - fn gas(&self) -> u128 { + fn gas_limit(&self) -> u128 { self.gas_limit }