Skip to content

Commit

Permalink
perf(rpc): add optional block hash arg (#1502)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Feb 22, 2023
1 parent 81fec4b commit 281f139
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
20 changes: 15 additions & 5 deletions crates/rpc/rpc-types/src/eth/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<H256>,
) -> Result<Self, BlockError> {
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),
}
}

Expand All @@ -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<H256>,
) -> 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(
Expand All @@ -111,8 +120,9 @@ impl Block {
pub fn from_block_full(
block: PrimitiveBlock,
total_difficulty: U256,
block_hash: Option<H256>,
) -> Result<Self, BlockError> {
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() {
Expand Down
2 changes: 1 addition & 1 deletion crates/rpc/rpc/src/eth/api/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 281f139

Please sign in to comment.