Skip to content

Commit

Permalink
RPC: call and estimateGas at specific block height (#2005)
Browse files Browse the repository at this point in the history
Co-authored-by: Shoham Chakraborty <[email protected]>
  • Loading branch information
Jouzo and shohamc1 authored May 23, 2023
1 parent 825355a commit fedc73d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
9 changes: 7 additions & 2 deletions lib/ain-evm/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,17 @@ impl EVMHandler {
data: &[u8],
gas_limit: u64,
access_list: AccessList,
block_number: U256,
) -> Result<TxResponse, Box<dyn Error>> {
let (state_root, block_number) = self
.storage
.get_latest_block()
.get_block_by_number(&block_number)
.map(|block| (block.header.state_root, block.header.number))
.unwrap_or_default();
println!("state_root : {:#?}", state_root);
debug!(
"Calling EVM at block number : {:#x}, state_root : {:#x}",
block_number, state_root
);

let vicinity = Vicinity {
block_number,
Expand Down Expand Up @@ -183,6 +187,7 @@ impl EVMHandler {
signed_tx.data(),
signed_tx.gas_limit().as_u64(),
signed_tx.access_list(),
block_number,
) {
Ok(TxResponse { exit_reason, .. }) if exit_reason.is_succeed() => Ok(signed_tx),
Ok(TxResponse { exit_reason, .. }) => {
Expand Down
39 changes: 25 additions & 14 deletions lib/ain-grpc/src/rpc/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,8 @@ pub trait MetachainRPC {
#[method(name = "getTransactionReceipt")]
fn get_receipt(&self, hash: H256) -> RpcResult<Option<ReceiptResult>>;

/// Retrieves the number of transactions sent from a specific address.
#[method(name = "getTransactionCount")]
fn get_transaction_count(
&self,
address: H160,
block_number: Option<BlockNumber>,
) -> RpcResult<U256>;

// ----------------------------------------
// State
// Account state
// ----------------------------------------

/// Retrieves the balance of a specific Ethereum address at a given block number.
Expand All @@ -163,6 +155,14 @@ pub trait MetachainRPC {
block_number: Option<BlockNumber>,
) -> RpcResult<H256>;

/// Retrieves the number of transactions sent from a specific address.
#[method(name = "eth_getTransactionCount")]
fn get_transaction_count(
&self,
address: H160,
block_number: Option<BlockNumber>,
) -> RpcResult<U256>;

// ----------------------------------------
// Send
// ----------------------------------------
Expand All @@ -182,8 +182,12 @@ pub trait MetachainRPC {
// ----------------------------------------

/// Estimate gas needed for execution of given contract.
#[method(name = "estimateGas")]
fn estimate_gas(&self, input: CallRequest) -> RpcResult<U256>;
#[method(name = "eth_estimateGas")]
fn estimate_gas(
&self,
input: CallRequest,
block_number: Option<BlockNumber>,
) -> RpcResult<U256>;

/// Returns current gas_price.
#[method(name = "gasPrice")]
Expand Down Expand Up @@ -235,7 +239,7 @@ impl MetachainRPCModule {
}

impl MetachainRPCServer for MetachainRPCModule {
fn call(&self, input: CallRequest, _block_number: Option<BlockNumber>) -> RpcResult<Bytes> {
fn call(&self, input: CallRequest, block_number: Option<BlockNumber>) -> RpcResult<Bytes> {
debug!(target:"rpc", "[RPC] Call input {:#?}", input);
let CallRequest {
from,
Expand All @@ -255,6 +259,7 @@ impl MetachainRPCServer for MetachainRPCModule {
&data.map(|d| d.0).unwrap_or_default(),
gas.unwrap_or_default().as_u64(),
vec![],
self.block_number_to_u256(block_number),
)
.map_err(|e| Error::Custom(format!("Error calling EVM : {e:?}")))?;

Expand Down Expand Up @@ -600,7 +605,11 @@ impl MetachainRPCServer for MetachainRPCModule {
Ok(nonce)
}

fn estimate_gas(&self, input: CallRequest) -> RpcResult<U256> {
fn estimate_gas(
&self,
input: CallRequest,
block_number: Option<BlockNumber>,
) -> RpcResult<U256> {
let CallRequest {
from,
to,
Expand All @@ -610,6 +619,7 @@ impl MetachainRPCServer for MetachainRPCModule {
..
} = input;

let block_number = self.block_number_to_u256(block_number);
let TxResponse { used_gas, .. } = self
.handler
.evm
Expand All @@ -620,10 +630,11 @@ impl MetachainRPCServer for MetachainRPCModule {
&data.map(|d| d.0).unwrap_or_default(),
gas.unwrap_or(U256::from(u64::MAX)).as_u64(),
vec![],
block_number,
)
.map_err(|e| Error::Custom(format!("Error calling EVM : {e:?}")))?;

debug!(target:"rpc","estimateGas: {:#?}", used_gas);
debug!(target:"rpc", "estimateGas: {:#?} at block {:#x}", used_gas, block_number);
Ok(U256::from(used_gas))
}

Expand Down

0 comments on commit fedc73d

Please sign in to comment.