Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rpc/otterscan): set total_fees in ots_getBlockDetails #9477

Merged
merged 5 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/rpc/rpc-api/src/otterscan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ pub trait Otterscan {
/// Tailor-made and expanded version of eth_getBlockByNumber for block details page in
/// Otterscan.
#[method(name = "getBlockDetails")]
async fn get_block_details(&self, block_number: u64) -> RpcResult<Option<BlockDetails>>;
async fn get_block_details(&self, block_number: u64) -> RpcResult<BlockDetails>;

/// Tailor-made and expanded version of eth_getBlockByHash for block details page in Otterscan.
#[method(name = "getBlockDetailsByHash")]
async fn get_block_details_by_hash(&self, block_hash: B256) -> RpcResult<Option<BlockDetails>>;
async fn get_block_details_by_hash(&self, block_hash: B256) -> RpcResult<BlockDetails>;

/// Get paginated transactions for a certain block. Also remove some verbose fields like logs.
#[method(name = "getBlockTransactions")]
Expand Down
4 changes: 2 additions & 2 deletions crates/rpc/rpc-builder/tests/it/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,9 @@ where

OtterscanClient::trace_transaction(client, tx_hash).await.unwrap();

OtterscanClient::get_block_details(client, block_number).await.unwrap();
OtterscanClient::get_block_details(client, block_number).await.unwrap_err();

OtterscanClient::get_block_details_by_hash(client, block_hash).await.unwrap();
OtterscanClient::get_block_details_by_hash(client, block_hash).await.unwrap_err();

OtterscanClient::get_block_transactions(client, block_number, page_number, page_size)
.await
Expand Down
3 changes: 2 additions & 1 deletion crates/rpc/rpc-builder/tests/it/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub async fn launch_ws(modules: impl Into<RpcModuleSelection>) -> RpcServerHandl
let server =
builder.build(TransportRpcModuleConfig::set_ws(modules), Box::new(EthApi::with_spawner));
RpcServerConfig::ws(Default::default())
.with_http_address(test_address())
.with_ws_address(test_address())
.start(&server)
.await
.unwrap()
Expand All @@ -84,6 +84,7 @@ pub async fn launch_http_ws(modules: impl Into<RpcModuleSelection>) -> RpcServer
Box::new(EthApi::with_spawner),
);
RpcServerConfig::ws(Default::default())
.with_ws_address(test_address())
.with_ws_address(test_address())
.with_http(Default::default())
.with_http_address(test_address())
Expand Down
40 changes: 31 additions & 9 deletions crates/rpc/rpc/src/otterscan.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use alloy_primitives::Bytes;
use async_trait::async_trait;
use jsonrpsee::core::RpcResult;
use reth_primitives::{Address, BlockNumberOrTag, TxHash, B256};
use reth_primitives::{Address, BlockNumberOrTag, TxHash, B256, U256};
use reth_rpc_api::{EthApiServer, OtterscanServer};
use reth_rpc_eth_api::helpers::TraceExt;
use reth_rpc_eth_types::EthApiError;
Expand All @@ -14,7 +14,7 @@ use reth_rpc_types::{
},
parity::{Action, CreateAction, CreateOutput, TraceOutput},
},
BlockTransactions, Header,
AnyTransactionReceipt, BlockTransactions, Header, RichBlock,
};
use revm_inspectors::{
tracing::{types::CallTraceNode, TracingInspectorConfig},
Expand All @@ -36,6 +36,24 @@ impl<Eth> OtterscanApi<Eth> {
pub const fn new(eth: Eth) -> Self {
Self { eth }
}

/// Constructs a `BlockDetails` from a block and its receipts.
fn block_details(
&self,
block: Option<RichBlock>,
receipts: Option<Vec<AnyTransactionReceipt>>,
) -> RpcResult<BlockDetails> {
let block = block.ok_or_else(|| EthApiError::UnknownBlockNumber)?;
let receipts = receipts.ok_or_else(|| EthApiError::UnknownBlockNumber)?;

// blob fee is burnt, so we don't need to calculate it
let total_fees = receipts
.iter()
.map(|receipt| receipt.gas_used.saturating_mul(receipt.effective_gas_price))
.sum::<u128>();

Ok(BlockDetails::new(block, Default::default(), U256::from(total_fees)))
}
}

#[async_trait]
Expand Down Expand Up @@ -133,15 +151,19 @@ where
}

/// Handler for `ots_getBlockDetails`
async fn get_block_details(&self, block_number: u64) -> RpcResult<Option<BlockDetails>> {
let block = self.eth.block_by_number(BlockNumberOrTag::Number(block_number), true).await?;
Ok(block.map(Into::into))
async fn get_block_details(&self, block_number: u64) -> RpcResult<BlockDetails> {
let block = self.eth.block_by_number(block_number.into(), true);
let receipts = self.eth.block_receipts(block_number.into());
let (block, receipts) = futures::try_join!(block, receipts)?;
self.block_details(block, receipts)
}

/// Handler for `getBlockDetailsByHash`
async fn get_block_details_by_hash(&self, block_hash: B256) -> RpcResult<Option<BlockDetails>> {
let block = self.eth.block_by_hash(block_hash, true).await?;
Ok(block.map(Into::into))
async fn get_block_details_by_hash(&self, block_hash: B256) -> RpcResult<BlockDetails> {
let block = self.eth.block_by_hash(block_hash, true);
let receipts = self.eth.block_receipts(block_hash.into());
let (block, receipts) = futures::try_join!(block, receipts)?;
self.block_details(block, receipts)
}

/// Handler for `getBlockTransactions`
Expand All @@ -156,7 +178,7 @@ where
let receipts = self.eth.block_receipts(block_number.into());
let (block, receipts) = futures::try_join!(block, receipts)?;

let mut block = block.ok_or_else(|| internal_rpc_err("block not found"))?;
let mut block = block.ok_or_else(|| EthApiError::UnknownBlockNumber)?;
let mut receipts = receipts.ok_or_else(|| internal_rpc_err("receipts not found"))?;

// check if the number of transactions matches the number of receipts
Expand Down
Loading