Skip to content

Commit

Permalink
feat: support "size" in eth_getBlockBy* (#1542)
Browse files Browse the repository at this point in the history
  • Loading branch information
morph-dev authored Oct 21, 2024
1 parent 454b3e0 commit 68ac212
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
29 changes: 24 additions & 5 deletions rpc/src/eth_rpc.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use alloy::{
primitives::{Address, Bytes, B256, U256},
rlp::{self, Encodable},
rpc::types::{
Block, BlockId, BlockNumberOrTag, BlockTransactions, TransactionRequest, Withdrawal,
},
};
use ethportal_api::{
jsonrpsee::types::{error::CALL_EXECUTION_FAILED_CODE, ErrorObjectOwned},
types::{
execution::{block_body::BlockBody, transaction::Transaction},
execution::{
block_body::BlockBody,
transaction::{Transaction, TransactionWithRlpHeader},
},
jsonrpc::{
endpoints::HistoryEndpoint,
request::{HistoryJsonRpcRequest, StateJsonRpcRequest},
Expand Down Expand Up @@ -210,17 +214,32 @@ impl EthApi {
.withdrawals()
.map(|withdrawals| withdrawals.iter().map(Withdrawal::from).collect());

// TODO: Add calculation for the block's size:
// Calculate block size:
// len(rlp(header, transactions, uncles, withdrawals))
// NOTE: Transactions should be encoded with envelope
let size = None;
// Note: transactions are encoded with header
let size = {
let payload_size = header.length()
+ body
.transactions()
.iter()
.cloned()
.map(TransactionWithRlpHeader)
.collect::<Vec<_>>()
.length()
+ rlp::list_length(body.uncles())
+ match body.withdrawals() {
Some(withdrawals) => rlp::list_length(withdrawals),
None => 0,
};
payload_size + rlp::length_of_length(payload_size)
};

// Combine header and block body into the single json representation of the block.
let block = Block {
header: header.into(),
transactions,
uncles,
size,
size: Some(U256::from(size)),
withdrawals,
};
Ok(block)
Expand Down
5 changes: 3 additions & 2 deletions tests/rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::fs;
use std::net::{IpAddr, Ipv4Addr};

use alloy::{
primitives::U256,
providers::{IpcConnect, Provider, ProviderBuilder, RootProvider},
pubsub::PubSubFrontend,
rpc::types::{BlockNumberOrTag, BlockTransactions, BlockTransactionsKind, Header as RpcHeader},
Expand Down Expand Up @@ -111,7 +112,7 @@ async fn test_eth_get_block_by_number() {
.expect("specified block not found");

assert_header(&block.header, &hwp.header);
assert_eq!(block.size, None);
assert_eq!(block.size, Some(U256::from(37890)));
assert_eq!(block.transactions.len(), body.transactions().len());
assert!(block.uncles.is_empty());
assert_eq!(
Expand Down Expand Up @@ -231,7 +232,7 @@ async fn test_eth_get_block_by_hash() {
.expect("specified block not found");

assert_header(&block.header, &hwp.header);
assert_eq!(block.size, None);
assert_eq!(block.size, Some(U256::from(37890)));
assert_eq!(block.transactions.len(), body.transactions().len());
assert!(block.uncles.is_empty());
assert_eq!(
Expand Down

0 comments on commit 68ac212

Please sign in to comment.