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): initial work in eth_call+estimate #1514

Merged
merged 1 commit into from
Feb 22, 2023
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
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions crates/rpc/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ reth-provider = { path = "../../storage/provider", features = ["test-utils"] }
reth-transaction-pool = { path = "../../transaction-pool", features = ["test-utils"]}
reth-network-api = { path = "../../net/network-api", features = ["test-utils"] }
reth-rpc-engine-api = { path = "../rpc-engine-api" }
reth-executor = { path = "../../executor" }
reth-tasks = { path = "../../tasks" }

# eth
revm = "3.0.0"

# rpc
jsonrpsee = { version = "0.16" }
http = "0.2.8"
Expand Down
64 changes: 64 additions & 0 deletions crates/rpc/rpc/src/eth/api/call.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//! Contains RPC handler implementations specific to endpoints that call/execute within evm.

#![allow(unused)] // TODO rm later

use crate::{eth::error::EthResult, EthApi};
use reth_executor::revm_wrap::{State, SubState};
use reth_primitives::{BlockId, U256};
use reth_provider::{BlockProvider, StateProvider, StateProviderFactory};
use reth_rpc_types::CallRequest;
use revm::primitives::{BlockEnv, Env, ResultAndState};
Copy link
Member

Choose a reason for hiding this comment

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

let's re-export these from reth_executor


impl<Client, Pool, Network> EthApi<Client, Pool, Network>
where
Client: BlockProvider + StateProviderFactory + 'static,
{
/// Creates the [Env] used by the [EVM](revm::EVM) when executing a call.
fn build_call_env(&self, request: CallRequest, block_env: BlockEnv) -> Env {
todo!()
}

/// Executes the call request against the given `state` without committing any changes to the
/// database
pub(crate) fn call_with<S>(
&self,
request: CallRequest,
state: S,
block_env: BlockEnv,
// TODO add overrides
) -> EthResult<ResultAndState>
where
S: StateProvider,
{
// the revm database impl
let mut db = SubState::new(State::new(state));
let mut evm = revm::EVM::new();
evm.env = self.build_call_env(request, block_env);
evm.database(db);
// TODO error conversion from EMVError to EthApiErr
let res = evm.transact_ref().unwrap();
Comment on lines +34 to +39
Copy link
Member

Choose a reason for hiding this comment

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

We'll probably want to push these helpers to the executor, and have the API maybe own an executor/reference to one?

Ok(res)
}

/// Estimate gas needed for execution of the `request` at the [BlockId] .
pub(crate) fn estimate_gas_at(&self, mut request: CallRequest, at: BlockId) -> EthResult<U256> {
// TODO get a StateProvider for the given blockId and BlockEnv
todo!()
}

/// Estimates the gas usage of the `request` with the state.
fn estimate_gas_with<S>(
&self,
mut request: CallRequest,
state: S,
block_env: BlockEnv,
) -> EthResult<U256>
where
S: StateProvider,
{
// TODO: check if transfer
// binary search over range to find best gas

todo!()
}
}
11 changes: 11 additions & 0 deletions crates/rpc/rpc/src/eth/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ use reth_primitives::{
use reth_provider::{BlockProvider, StateProviderFactory};
use std::num::NonZeroUsize;

use crate::eth::error::EthResult;
use reth_provider::providers::ChainState;
use reth_rpc_types::FeeHistoryCache;
use reth_transaction_pool::TransactionPool;
use std::sync::Arc;

mod block;
mod call;
mod server;
mod state;
mod transactions;
Expand Down Expand Up @@ -100,6 +103,14 @@ where
self.client().convert_block_number(num)
}

/// Helper function to execute a closure with the database at a specific block.
pub(crate) fn with_state_at<F, T>(&self, _at: BlockId, _f: F) -> EthResult<T>
where
F: FnOnce(ChainState<'_>) -> T,
{
unimplemented!()
}

/// Returns the state at the given [BlockId] enum or the latest.
pub(crate) fn state_at_block_id_or_latest(
&self,
Expand Down