-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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): Add ots_ namespace and trait bindings for Otterscan Endpoints #3778
Changes from 3 commits
b0f2e11
92852f7
ab84d33
fdb04f3
a39b60a
33bc94e
6f286f8
77fe385
9babe78
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use jsonrpsee::{core::RpcResult, proc_macros::rpc}; | ||
use reth_primitives::{Address, BlockId, TxHash, H256, U256}; | ||
use reth_rpc_types::{ | ||
BlockDetails, ContractCreator, InternalOperation, TraceEntry, Transaction, | ||
TransactionsWithReceipts, | ||
}; | ||
|
||
/// Otterscan rpc interface. | ||
#[cfg_attr(not(feature = "client"), rpc(server, namespace = "ots"))] | ||
#[cfg_attr(feature = "client", rpc(server, client, namespace = "ots"))] | ||
pub trait Otterscan { | ||
/// 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>; | ||
|
||
/// Very simple API versioning scheme. Every time we add a new capability, the number is | ||
/// incremented. This allows for Otterscan to check if the node contains all API it | ||
/// needs. | ||
#[method(name = "getApiLevel")] | ||
async fn get_api_level(&self) -> RpcResult<u64>; | ||
|
||
/// Return the internal ETH transfers inside a transaction. | ||
#[method(name = "getInternalOperations")] | ||
async fn get_internal_operations(&self, tx_hash: TxHash) -> RpcResult<Vec<InternalOperation>>; | ||
|
||
/// Given a transaction hash, returns its raw revert reason. | ||
#[method(name = "getTransactionError")] | ||
async fn get_transaction_error(&self, tx_hash: TxHash) -> RpcResult<String>; | ||
|
||
/// Extract all variations of calls, contract creation and self-destructs and returns a call | ||
/// tree. | ||
#[method(name = "traceTransaction")] | ||
async fn trace_transaction(&self, tx_hash: TxHash) -> RpcResult<TraceEntry>; | ||
|
||
/// 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: U256) -> 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: H256) -> RpcResult<BlockDetails>; | ||
|
||
/// Get paginated transactions for a certain block. Also remove some verbose fields like logs. | ||
#[method(name = "getBlockTransactions")] | ||
async fn get_block_transactions( | ||
&self, | ||
block_number: U256, | ||
page_number: u8, | ||
page_size: u8, | ||
) -> RpcResult<Vec<Transaction>>; | ||
|
||
/// Gets paginated inbound/outbound transaction calls for a certain address. | ||
#[method(name = "searchTransactionsBefore")] | ||
async fn search_transactions_before( | ||
&self, | ||
address: Address, | ||
block_number: U256, | ||
page_size: u8, | ||
) -> RpcResult<TransactionsWithReceipts>; | ||
|
||
/// Gets paginated inbound/outbound transaction calls for a certain address. | ||
#[method(name = "searchTransactionsAfter")] | ||
async fn search_transactions_after( | ||
&self, | ||
address: Address, | ||
block_number: U256, | ||
page_size: u8, | ||
) -> RpcResult<TransactionsWithReceipts>; | ||
|
||
/// Gets the transaction hash for a certain sender address, given its nonce. | ||
#[method(name = "getTransactionBySenderAndNonce")] | ||
async fn get_transaction_by_sender_and_nonce( | ||
&self, | ||
sender: Address, | ||
nonce: u64, | ||
) -> RpcResult<Transaction>; | ||
|
||
/// Gets the transaction hash and the address who created a contract. | ||
#[method(name = "getContractCreator")] | ||
async fn get_contract_creator(&self, address: Address) -> RpcResult<ContractCreator>; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use crate::{Block, Transaction, TransactionReceipt}; | ||
use reth_primitives::{Address, Bytes}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
/// Operation type enum for `InternalOperation` struct | ||
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] | ||
pub enum OperationType { | ||
/// Operation Transfer | ||
OpTransfer, | ||
/// Operation Contract self destruct | ||
OpSelfDestruct, | ||
/// Operation Create | ||
OpCreate, | ||
/// Operation Create2 | ||
OpCreate2, | ||
} | ||
|
||
/// Custom struct for otterscan `getInternalOperations` RPC response | ||
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] | ||
pub struct InternalOperation { | ||
r#type: OperationType, | ||
from: Address, | ||
to: Address, | ||
value: u128, | ||
} | ||
|
||
/// Custom struct for otterscan `traceTransaction` RPC response | ||
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] | ||
pub struct TraceEntry { | ||
r#type: String, | ||
depth: u32, | ||
from: Address, | ||
to: Address, | ||
value: u128, | ||
input: Bytes, | ||
} | ||
|
||
/// Internal issuance struct for `BlockDetails` struct | ||
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct InternalIssuance { | ||
block_reward: String, | ||
uncle_reward: String, | ||
issuance: String, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these are likely U256 I believe? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great question ! Well to get this one i had to read the Go code ,
this is the Go struct , and the values are the result of
that from the documentation we get
I took the easy route of just reusing the Go type but i would love to know if it still makes sense to use a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This is the same behaviour There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great ! I'll change it over to U256 then , thanks for catching this ! |
||
} | ||
|
||
/// Custom struct for otterscan `getBlockDetails` RPC response | ||
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct BlockDetails { | ||
block: Block, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't notice this at first, but we need a custom serializer (or an entirely different struct even) for this block e.g.: |
||
issuance: InternalIssuance, | ||
total_fees: u64, | ||
} | ||
|
||
/// Custom struct for otterscan `searchTransactionsAfter`and `searchTransactionsBefore` RPC | ||
/// responses | ||
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct TransactionsWithReceipts { | ||
txs: Vec<Transaction>, | ||
receipts: TransactionReceipt, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be a Vec but more importantly, I'm finding now (while working on anvil) that the response otterscan expects doesn't quite match what their spec says, e.g. for
which is not what the spec says. overall, maybe it's better to hold off on these structs and instead add them 1 by 1 as we figure out these inconsistencies There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My bad ! Should have doubled checked this. |
||
first_page: bool, | ||
last_page: bool, | ||
} | ||
|
||
/// Custom struct for otterscan `getContractCreator` RPC responses | ||
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] | ||
pub struct ContractCreator { | ||
tx: Transaction, | ||
creator: Address, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#![allow(dead_code, unused_variables)] | ||
use crate::result::internal_rpc_err; | ||
use async_trait::async_trait; | ||
use jsonrpsee::core::RpcResult; | ||
use reth_primitives::{Address, BlockId, TxHash, H256, U256}; | ||
use reth_rpc_api::{EthApiServer, OtterscanServer}; | ||
use reth_rpc_types::{ | ||
BlockDetails, ContractCreator, InternalOperation, TraceEntry, Transaction, | ||
TransactionsWithReceipts, | ||
}; | ||
|
||
/// Otterscan Api | ||
#[derive(Debug)] | ||
pub struct OtterscanApi<Eth> { | ||
eth: Eth, | ||
} | ||
|
||
impl<Eth> OtterscanApi<Eth> { | ||
/// Creates a new instance of `Otterscan`. | ||
pub fn new(eth: Eth) -> Self { | ||
Self { eth } | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl<Eth> OtterscanServer for OtterscanApi<Eth> | ||
where | ||
Eth: EthApiServer, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this might not be enough to fully support the otterscan API, but it is sufficient for now, but can reevaluate later, see TraceApiImpl for example |
||
{ | ||
/// Handler for `ots_hasCode` | ||
async fn has_code(&self, address: Address, block_number: Option<BlockId>) -> RpcResult<bool> { | ||
Err(internal_rpc_err("unimplemented")) | ||
} | ||
|
||
/// Handler for `ots_getApiLevel` | ||
async fn get_api_level(&self) -> RpcResult<u64> { | ||
Err(internal_rpc_err("unimplmented")) | ||
ZePedroResende marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/// Handler for `ots_getInternalOperations` | ||
async fn get_internal_operations(&self, tx_hash: TxHash) -> RpcResult<Vec<InternalOperation>> { | ||
Err(internal_rpc_err("unimplemented")) | ||
} | ||
|
||
/// Handler for `ots_getTransactionError` | ||
async fn get_transaction_error(&self, tx_hash: TxHash) -> RpcResult<String> { | ||
Err(internal_rpc_err("unimplmented")) | ||
} | ||
|
||
/// Handler for `ots_traceTransaction` | ||
async fn trace_transaction(&self, tx_hash: TxHash) -> RpcResult<TraceEntry> { | ||
Err(internal_rpc_err("unimplmented")) | ||
} | ||
|
||
/// Handler for `ots_getBlockDetails` | ||
async fn get_block_details(&self, block_number: U256) -> RpcResult<BlockDetails> { | ||
Err(internal_rpc_err("unimplemented")) | ||
} | ||
|
||
/// Handler for `getBlockDetailsByHash` | ||
async fn get_block_details_by_hash(&self, block_hash: H256) -> RpcResult<BlockDetails> { | ||
Err(internal_rpc_err("unimplmented")) | ||
} | ||
|
||
/// Handler for `getBlockTransactions` | ||
async fn get_block_transactions( | ||
&self, | ||
block_number: U256, | ||
page_number: u8, | ||
page_size: u8, | ||
) -> RpcResult<Vec<Transaction>> { | ||
Err(internal_rpc_err("unimplmented")) | ||
} | ||
|
||
/// Handler for `searchTransactionsBefore` | ||
async fn search_transactions_before( | ||
&self, | ||
address: Address, | ||
block_number: U256, | ||
page_size: u8, | ||
) -> RpcResult<TransactionsWithReceipts> { | ||
Err(internal_rpc_err("unimplmented")) | ||
} | ||
|
||
/// Handler for `searchTransactionsAfter` | ||
async fn search_transactions_after( | ||
&self, | ||
address: Address, | ||
block_number: U256, | ||
page_size: u8, | ||
) -> RpcResult<TransactionsWithReceipts> { | ||
Err(internal_rpc_err("unimplmented")) | ||
} | ||
|
||
/// Handler for `getTransactionBySenderAndNonce` | ||
async fn get_transaction_by_sender_and_nonce( | ||
&self, | ||
sender: Address, | ||
nonce: u64, | ||
) -> RpcResult<Transaction> { | ||
Err(internal_rpc_err("unimplmented")) | ||
} | ||
|
||
/// Handler for `getContractCreator` | ||
async fn get_contract_creator(&self, address: Address) -> RpcResult<ContractCreator> { | ||
Err(internal_rpc_err("unimplmented")) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mattsse do these deserialize from integers in reth?
I had problems doing the equivalent in anvil (foundry-rs/foundry#5414)
Otterscan sends
1
, but the deserializer expected only"1"
. had to switch tou64
over thereThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
they don't only hex strings, blocknumber should be U64, and there's also
U64HexOrNumber
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used
BlockNumberOrTag
that is also used on the engine RCP endpoints is that ok or should we use the explicitu64
?If that is ok , i think on our side it's ready to merge.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this looks correct:
https://github.com/ledgerwatch/erigon/blob/ef4ccd47736019132c1967d6921f6c4b014eb515/turbo/jsonrpc/otterscan_api.go#L47-L47