diff --git a/crates/rpc/rpc-types/src/eth/block.rs b/crates/rpc/rpc-types/src/eth/block.rs index 3e4080722..fc045e331 100644 --- a/crates/rpc/rpc-types/src/eth/block.rs +++ b/crates/rpc/rpc-types/src/eth/block.rs @@ -73,16 +73,21 @@ pub struct Block { impl Block { /// Converts the given primitive block into a [Block] response with the given /// [BlockTransactionsKind] + /// + /// If a `block_hash` is provided, then this is used, otherwise the block hash is computed. pub fn from_block( block: PrimitiveBlock, total_difficulty: U256, kind: BlockTransactionsKind, + block_hash: Option, ) -> Result { match kind { BlockTransactionsKind::Hashes => { - Ok(Self::from_block_hashes_only(block, total_difficulty)) + Ok(Self::from_block_with_tx_hashes(block, total_difficulty, block_hash)) + } + BlockTransactionsKind::Full => { + Self::from_block_full(block, total_difficulty, block_hash) } - BlockTransactionsKind::Full => Self::from_block_full(block, total_difficulty), } } @@ -91,8 +96,12 @@ impl Block { /// /// This will populate the `transactions` field with only the hashes of the transactions in the /// block: [BlockTransactions::Hashes] - pub fn from_block_hashes_only(block: PrimitiveBlock, total_difficulty: U256) -> Self { - let block_hash = block.header.hash_slow(); + pub fn from_block_with_tx_hashes( + block: PrimitiveBlock, + total_difficulty: U256, + block_hash: Option, + ) -> Self { + let block_hash = block_hash.unwrap_or_else(|| block.header.hash_slow()); let transactions = block.body.iter().map(|tx| tx.hash).collect(); Self::from_block_with_transactions( @@ -111,8 +120,9 @@ impl Block { pub fn from_block_full( block: PrimitiveBlock, total_difficulty: U256, + block_hash: Option, ) -> Result { - let block_hash = block.header.hash_slow(); + let block_hash = block_hash.unwrap_or_else(|| block.header.hash_slow()); let block_number = block.number; let mut transactions = Vec::with_capacity(block.body.len()); for (idx, tx) in block.body.iter().enumerate() { diff --git a/crates/rpc/rpc/src/eth/api/block.rs b/crates/rpc/rpc/src/eth/api/block.rs index 62b77ea4d..861b36f5e 100644 --- a/crates/rpc/rpc/src/eth/api/block.rs +++ b/crates/rpc/rpc/src/eth/api/block.rs @@ -43,7 +43,7 @@ where .client() .header_td(&block_hash)? .ok_or_else(|| EthApiError::UnknownBlockNumber)?; - let block = Block::from_block(block, total_difficulty, full.into())?; + let block = Block::from_block(block, total_difficulty, full.into(), Some(block_hash))?; Ok(Some(block.into())) } else { Ok(None)