Skip to content

Commit

Permalink
fix: make Block::hash required (#1205)
Browse files Browse the repository at this point in the history
fix: make block.hash required
  • Loading branch information
klkvr authored Aug 27, 2024
1 parent b81dec1 commit 05cc4e0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
7 changes: 7 additions & 0 deletions crates/network-primitives/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ pub trait TransactionResponse {

/// Header JSON-RPC response.
pub trait HeaderResponse {
/// Block hash
fn hash(&self) -> BlockHash;

/// Block number
fn number(&self) -> u64;

Expand Down Expand Up @@ -145,6 +148,10 @@ impl<T: BlockResponse> BlockResponse for WithOtherFields<T> {
}

impl<T: HeaderResponse> HeaderResponse for WithOtherFields<T> {
fn hash(&self) -> BlockHash {
self.inner.hash()
}

fn number(&self) -> u64 {
self.inner.number()
}
Expand Down
5 changes: 3 additions & 2 deletions crates/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use alloy_consensus::TxReceipt;
use alloy_eips::eip2718::{Eip2718Envelope, Eip2718Error};
use alloy_json_rpc::RpcObject;
use alloy_network_primitives::{BlockResponse, HeaderResponse};
use core::fmt::{Debug, Display};

mod transaction;
Expand All @@ -25,7 +24,9 @@ mod any;
pub use any::{AnyNetwork, AnyTxType};

pub use alloy_eips::eip2718;
pub use alloy_network_primitives::{self as primitives, ReceiptResponse, TransactionResponse};
pub use alloy_network_primitives::{
self as primitives, BlockResponse, HeaderResponse, ReceiptResponse, TransactionResponse,
};

/// Captures type info for network-specific RPC requests/responses.
///
Expand Down
8 changes: 4 additions & 4 deletions crates/provider/src/provider/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1277,10 +1277,10 @@ mod tests {
let num = 0;
let tag: BlockNumberOrTag = num.into();
let block = provider.get_block_by_number(tag, true).await.unwrap().unwrap();
let hash = block.header.hash.unwrap();
let hash = block.header.hash;
let block =
provider.get_block_by_hash(hash, BlockTransactionsKind::Full).await.unwrap().unwrap();
assert_eq!(block.header.hash.unwrap(), hash);
assert_eq!(block.header.hash, hash);
}

#[tokio::test]
Expand All @@ -1290,12 +1290,12 @@ mod tests {
let num = 0;
let tag: BlockNumberOrTag = num.into();
let block = provider.get_block_by_number(tag, true).await.unwrap().unwrap();
let hash = block.header.hash.unwrap();
let hash = block.header.hash;
let block: Block = provider
.raw_request::<(B256, bool), Block>("eth_getBlockByHash".into(), (hash, true))
.await
.unwrap();
assert_eq!(block.header.hash.unwrap(), hash);
assert_eq!(block.header.hash, hash);
}

#[tokio::test]
Expand Down
16 changes: 10 additions & 6 deletions crates/rpc-types-eth/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl<T: TransactionResponse, H> Block<T, H> {
#[serde(rename_all = "camelCase")]
pub struct Header {
/// Hash of the block
pub hash: Option<BlockHash>,
pub hash: BlockHash,
/// Hash of the parent
pub parent_hash: B256,
/// Hash of the uncles
Expand Down Expand Up @@ -205,6 +205,10 @@ impl TryFrom<Header> for alloy_consensus::Header {
}

impl HeaderResponse for Header {
fn hash(&self) -> BlockHash {
self.hash
}

fn number(&self) -> u64 {
self.number
}
Expand Down Expand Up @@ -337,7 +341,7 @@ mod tests {
fn serde_block() {
let block = Block {
header: Header {
hash: Some(B256::with_last_byte(1)),
hash: B256::with_last_byte(1),
parent_hash: B256::with_last_byte(2),
uncles_hash: B256::with_last_byte(3),
miner: Address::with_last_byte(4),
Expand Down Expand Up @@ -379,7 +383,7 @@ mod tests {
fn serde_uncle_block() {
let block = Block {
header: Header {
hash: Some(B256::with_last_byte(1)),
hash: B256::with_last_byte(1),
parent_hash: B256::with_last_byte(2),
uncles_hash: B256::with_last_byte(3),
miner: Address::with_last_byte(4),
Expand Down Expand Up @@ -421,7 +425,7 @@ mod tests {
fn serde_block_with_withdrawals_set_as_none() {
let block = Block {
header: Header {
hash: Some(B256::with_last_byte(1)),
hash: B256::with_last_byte(1),
parent_hash: B256::with_last_byte(2),
uncles_hash: B256::with_last_byte(3),
miner: Address::with_last_byte(4),
Expand Down Expand Up @@ -639,7 +643,7 @@ mod tests {
let block = serde_json::from_str::<Block>(s).unwrap();
let header: alloy_consensus::Header = block.clone().header.try_into().unwrap();
let recomputed_hash = keccak256(alloy_rlp::encode(&header));
assert_eq!(recomputed_hash, block.header.hash.unwrap());
assert_eq!(recomputed_hash, block.header.hash);

let s2 = r#"{
"baseFeePerGas":"0x886b221ad",
Expand Down Expand Up @@ -676,6 +680,6 @@ mod tests {
let block2 = serde_json::from_str::<Block>(s2).unwrap();
let header: alloy_consensus::Header = block2.clone().header.try_into().unwrap();
let recomputed_hash = keccak256(alloy_rlp::encode(&header));
assert_eq!(recomputed_hash, block2.header.hash.unwrap());
assert_eq!(recomputed_hash, block2.header.hash);
}
}

0 comments on commit 05cc4e0

Please sign in to comment.