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): Add ots_ namespace and trait bindings for Otterscan Endpoints #3778

Merged
merged 9 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions crates/rpc/rpc-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ mod eth;
mod eth_filter;
mod eth_pubsub;
mod net;
mod otterscan;
mod rpc;
mod trace;
mod txpool;
Expand All @@ -44,6 +45,7 @@ pub mod servers {
eth_filter::EthFilterApiServer,
eth_pubsub::EthPubSubApiServer,
net::NetApiServer,
otterscan::OtterscanServer,
rpc::RpcApiServer,
trace::TraceApiServer,
txpool::TxPoolApiServer,
Expand All @@ -64,6 +66,7 @@ pub mod clients {
engine::{EngineApiClient, EngineEthApiClient},
eth::EthApiClient,
net::NetApiClient,
otterscan::OtterscanClient,
rpc::RpcApiServer,
trace::TraceApiClient,
txpool::TxPoolApiClient,
Expand Down
82 changes: 82 additions & 0 deletions crates/rpc/rpc-api/src/otterscan.rs
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,
Copy link
Contributor

@naps62 naps62 Jul 17, 2023

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 to u64 over there

Copy link
Collaborator

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

Copy link
Contributor Author

@ZePedroResende ZePedroResende Jul 18, 2023

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 explicit u64 ?
If that is ok , i think on our side it's ready to merge.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>;
}
12 changes: 11 additions & 1 deletion crates/rpc/rpc-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ use reth_rpc::{
gas_oracle::GasPriceOracle,
},
AdminApi, DebugApi, EngineEthApi, EthApi, EthFilter, EthPubSub, EthSubscriptionIdProvider,
NetApi, RPCApi, TraceApi, TracingCallGuard, TxPoolApi, Web3Api,
NetApi, OtterscanApi, RPCApi, TraceApi, TracingCallGuard, TxPoolApi, Web3Api,
};
use reth_rpc_api::{servers::*, EngineApiServer};
use reth_tasks::{TaskSpawner, TokioTaskExecutor};
Expand Down Expand Up @@ -646,6 +646,8 @@ pub enum RethRpcModule {
Web3,
/// `rpc_` module
Rpc,
/// `ots_` module
Ots,
ZePedroResende marked this conversation as resolved.
Show resolved Hide resolved
}

// === impl RethRpcModule ===
Expand Down Expand Up @@ -773,6 +775,13 @@ where
self
}

/// Register Otterscan Namespace
pub fn register_ots(&mut self) -> &mut Self {
let eth_api = self.eth_api();
self.modules.insert(RethRpcModule::Ots, OtterscanApi::new(eth_api).into_rpc().into());
self
}

/// Register Debug Namespace
pub fn register_debug(&mut self) -> &mut Self {
let eth_api = self.eth_api();
Expand Down Expand Up @@ -921,6 +930,7 @@ where
)
.into_rpc()
.into(),
RethRpcModule::Ots => OtterscanApi::new(eth_api.clone()).into_rpc().into(),
})
.clone()
})
Expand Down
2 changes: 2 additions & 0 deletions crates/rpc/rpc-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

mod admin;
mod eth;
mod otterscan;
mod rpc;

pub use admin::*;
pub use eth::*;
pub use otterscan::*;
pub use rpc::*;
72 changes: 72 additions & 0 deletions crates/rpc/rpc-types/src/otterscan.rs
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,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are likely U256 I believe?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 ,

type internalIssuance struct {
	BlockReward string `json:"blockReward,omitempty"`
	UncleReward string `json:"uncleReward,omitempty"`
	Issuance    string `json:"issuance,omitempty"`
}

this is the Go struct , and the values are the result of EncodeBig

	ret.BlockReward = hexutil.EncodeBig(minerReward.ToBig())
	ret.Issuance = hexutil.EncodeBig(issuance.ToBig())
	...
	ret.UncleReward = hexutil.EncodeBig(issuance.ToBig())

that from the documentation we get

// EncodeBig encodes bigint as a hex string with 0x prefix.

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 U256 and have a custom serializer or if the default U256 serializer behaviour is similar to the EncodeBig output !

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EncodeBig encodes bigint as a hex string with 0x prefix.

This is the same behaviour

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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,
Copy link
Contributor

Choose a reason for hiding this comment

The 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.: ots_getBlockDetails does not want the transactions attribute, and instead wants a transactionCount (inside the Block, not as another BlockDetails attribute, as I had understood initially)

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,
Copy link
Contributor

@naps62 naps62 Jul 16, 2023

Choose a reason for hiding this comment

The 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 ots_getBlockTransactions, they apparently expect something like:

{
fullblock: { transactions: [ ... ], transactionCount: xx, ... /* all the other usual fields */ },
receipts: [ ... ]
}

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
wdyt @mattsse ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad ! Should have doubled checked this.
We really need to write some tests , with some request responses then check that the serializations and deserializations matches up.

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,
}
2 changes: 2 additions & 0 deletions crates/rpc/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ mod engine;
pub mod eth;
mod layers;
mod net;
mod otterscan;
mod rpc;
mod trace;
mod txpool;
Expand All @@ -49,6 +50,7 @@ pub use engine::{EngineApi, EngineEthApi};
pub use eth::{EthApi, EthApiSpec, EthFilter, EthPubSub, EthSubscriptionIdProvider};
pub use layers::{AuthLayer, AuthValidator, Claims, JwtAuthValidator, JwtError, JwtSecret};
pub use net::NetApi;
pub use otterscan::OtterscanApi;
pub use rpc::RPCApi;
pub use trace::TraceApi;
pub use txpool::TxPoolApi;
Expand Down
108 changes: 108 additions & 0 deletions crates/rpc/rpc/src/otterscan.rs
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,
Copy link
Collaborator

@mattsse mattsse Jul 14, 2023

Choose a reason for hiding this comment

The 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"))
}
}