Skip to content

Commit

Permalink
feat(rpc/ots): add rpc erigon_getHeaderByNumber (#9300)
Browse files Browse the repository at this point in the history
Signed-off-by: jsvisa <delweng@gmail.com>
  • Loading branch information
jsvisa authored Jul 4, 2024
1 parent af280b9 commit 610110b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
12 changes: 11 additions & 1 deletion crates/rpc/rpc-api/src/otterscan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,23 @@ use reth_rpc_types::{
BlockDetails, ContractCreator, InternalOperation, OtsBlockTransactions, TraceEntry,
TransactionsWithReceipts,
},
Transaction,
Header, Transaction,
};

/// Otterscan rpc interface.
#[cfg_attr(not(feature = "client"), rpc(server, namespace = "ots"))]
#[cfg_attr(feature = "client", rpc(server, client, namespace = "ots"))]
pub trait Otterscan {
/// Get the block header by block number, required by otterscan.
/// Otterscan currently requires this endpoint, used as:
///
/// 1. check if the node is Erigon or not
/// 2. get block header instead of the full block
///
/// Ref: <https://github.com/otterscan/otterscan/blob/071d8c55202badf01804f6f8d53ef9311d4a9e47/src/useProvider.ts#L71>
#[method(name = "getHeaderByNumber", aliases = ["erigon_getHeaderByNumber"])]
async fn get_header_by_number(&self, block_number: u64) -> RpcResult<Option<Header>>;

/// Check if a certain address contains a deployed code.
#[method(name = "hasCode")]
async fn has_code(&self, address: Address, block_number: Option<BlockId>) -> RpcResult<bool>;
Expand Down
2 changes: 2 additions & 0 deletions crates/rpc/rpc-builder/tests/it/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ where
let nonce = 1;
let block_hash = B256::default();

OtterscanClient::get_header_by_number(client, 1).await.unwrap();

OtterscanClient::has_code(client, address, None).await.unwrap();

OtterscanClient::get_api_level(client).await.unwrap();
Expand Down
7 changes: 6 additions & 1 deletion crates/rpc/rpc/src/otterscan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use reth_rpc_types::{
BlockDetails, ContractCreator, InternalOperation, OperationType, OtsBlockTransactions,
OtsReceipt, OtsTransactionReceipt, TraceEntry, TransactionsWithReceipts,
},
BlockTransactions, Transaction,
BlockTransactions, Header, Transaction,
};
use revm_inspectors::transfer::{TransferInspector, TransferKind};
use revm_primitives::ExecutionResult;
Expand All @@ -35,6 +35,11 @@ impl<Eth> OtterscanServer for OtterscanApi<Eth>
where
Eth: EthApiServer + TraceExt + 'static,
{
/// Handler for `{ots,erigon}_getHeaderByNumber`
async fn get_header_by_number(&self, block_number: u64) -> RpcResult<Option<Header>> {
self.eth.header_by_number(BlockNumberOrTag::Number(block_number)).await
}

/// Handler for `ots_hasCode`
async fn has_code(&self, address: Address, block_number: Option<BlockId>) -> RpcResult<bool> {
self.eth.get_code(address, block_number).await.map(|code| !code.is_empty())
Expand Down

0 comments on commit 610110b

Please sign in to comment.