Skip to content

Commit

Permalink
fix comment
Browse files Browse the repository at this point in the history
  • Loading branch information
Pana committed Jan 23, 2025
1 parent 391bdeb commit 7f1130f
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{AccountEntry, OverlayAccount};
use cfx_rpc_eth_types::AccountOverride;
use cfx_rpc_eth_types::{AccountOverride, AccountStateOverrideMode};
use cfx_types::{AddressWithSpace, Space, H256, U256};
use primitives::{Account, StorageValue};
use std::{collections::HashMap, sync::Arc};
Expand Down Expand Up @@ -44,17 +44,14 @@ impl OverlayAccount {
acc.init_code(code.clone(), address.address);
}

match (
acc_overrides.state.as_ref(),
acc_overrides.state_diff.as_ref(),
) {
(Some(state_override), None) => {
match &acc_overrides.state {
AccountStateOverrideMode::State(state_override) => {
acc.override_storage_read_cache(state_override, true);
}
(None, Some(diff)) => {
AccountStateOverrideMode::Diff(diff) => {
acc.override_storage_read_cache(diff, false);
}
(_, _) => {}
AccountStateOverrideMode::None => {}
}

if acc_overrides.move_precompile_to.is_some() {
Expand Down
18 changes: 8 additions & 10 deletions crates/client/src/rpc/impls/eth/eth_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::rpc::{
types::{
eth::{
AccountPendingTransactions, Block as RpcBlock, BlockNumber,
BlockOverrides, EthRpcLogFilter, EvmOverrides, Log, Receipt,
StateOverride, SyncStatus, Transaction, TransactionRequest,
BlockOverrides, EthRpcLogFilter, Log, Receipt, RpcStateOverride,
SyncStatus, Transaction, TransactionRequest,
},
Bytes, FeeHistory, Index, U64 as HexU64,
},
Expand Down Expand Up @@ -263,20 +263,18 @@ impl Eth for EthHandler {
fn call(
&self, request: TransactionRequest,
block_number_or_hash: Option<BlockNumber>,
state_overrides: Option<StateOverride>,
state_overrides: Option<RpcStateOverride>,
block_overrides: Option<Box<BlockOverrides>>,
) -> RpcResult<Bytes> {
debug!(
"RPC Request: eth_call(request={:?}, block_num={:?})",
request, block_number_or_hash
);

let evm_overrides = EvmOverrides::new(state_overrides, block_overrides);

let (execution, _estimation) = self.inner.exec_transaction(
request,
block_number_or_hash,
evm_overrides,
state_overrides,
block_overrides,
)?;

Ok(execution.output.into())
Expand All @@ -285,17 +283,17 @@ impl Eth for EthHandler {
fn estimate_gas(
&self, request: TransactionRequest,
block_number_or_hash: Option<BlockNumber>,
state_override: Option<StateOverride>,
state_overrides: Option<RpcStateOverride>,
) -> RpcResult<U256> {
debug!(
"RPC Request: eth_estimateGas(request={:?}, block_num={:?})",
request, block_number_or_hash
);
let evm_overrides = EvmOverrides::new(state_override, None);
let (_, estimated_gas) = self.inner.exec_transaction(
request,
block_number_or_hash,
evm_overrides,
state_overrides,
None,
)?;

Ok(estimated_gas)
Expand Down
8 changes: 4 additions & 4 deletions crates/client/src/rpc/traits/eth_space/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ use jsonrpc_derive::rpc;
use crate::rpc::types::{
eth::{
AccountPendingTransactions, Block, BlockNumber, BlockOverrides,
EthRpcLogFilter, Log, Receipt, StateOverride, SyncStatus, Transaction,
TransactionRequest,
EthRpcLogFilter, Log, Receipt, RpcStateOverride, SyncStatus,
Transaction, TransactionRequest,
},
Bytes, FeeHistory, Index,
};
Expand Down Expand Up @@ -164,15 +164,15 @@ pub trait Eth {
#[rpc(name = "eth_call")]
fn call(
&self, transaction: TransactionRequest, block: Option<BlockNumber>,
state_overrides: Option<StateOverride>,
state_overrides: Option<RpcStateOverride>,
block_overrides: Option<Box<BlockOverrides>>,
) -> Result<Bytes>;

/// Estimate gas needed for execution of given contract.
#[rpc(name = "eth_estimateGas")]
fn estimate_gas(
&self, transaction: TransactionRequest, block: Option<BlockNumber>,
state_override: Option<StateOverride>,
state_override: Option<RpcStateOverride>,
) -> Result<U256>;

/// Get transaction by its hash.
Expand Down
8 changes: 4 additions & 4 deletions crates/client/src/rpc/types/eth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
// See http://www.gnu.org/licenses/

pub use cfx_rpc_eth_types::{
eth_pubsub, trace_filter::TraceFilter, AccountPendingTransactions, Block,
BlockNumber, BlockOverrides, EthRpcLogFilter, EvmOverrides, FilterChanges,
Header, Log, Receipt, StateOverride, SyncInfo, SyncStatus, Transaction,
TransactionRequest,
eth_pubsub, trace_filter::TraceFilter, AccountOverride,
AccountPendingTransactions, Block, BlockNumber, BlockOverrides,
EthRpcLogFilter, EvmOverrides, FilterChanges, Header, Log, Receipt,
RpcStateOverride, SyncInfo, SyncStatus, Transaction, TransactionRequest,
};

pub use cfx_rpc_cfx_types::trace_eth::{LocalizedTrace, Res};
8 changes: 4 additions & 4 deletions crates/rpc/rpc-eth-api/src/eth.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cfx_rpc_eth_types::{
Block, BlockNumber as BlockId, BlockOverrides, EthRpcLogFilter as Filter,
FeeHistory, Header, Log, Receipt, StateOverride, SyncStatus, Transaction,
TransactionRequest,
FeeHistory, Header, Log, Receipt, RpcStateOverride, SyncStatus,
Transaction, TransactionRequest,
};
use cfx_rpc_primitives::{Bytes, Index};
use cfx_types::{Address, H256, H64, U256, U64};
Expand Down Expand Up @@ -201,7 +201,7 @@ pub trait EthApi {
#[method(name = "call")]
async fn call(
&self, request: TransactionRequest, block_number: Option<BlockId>,
state_overrides: Option<StateOverride>,
state_overrides: Option<RpcStateOverride>,
block_overrides: Option<Box<BlockOverrides>>,
) -> RpcResult<Bytes>;

Expand Down Expand Up @@ -243,7 +243,7 @@ pub trait EthApi {
#[method(name = "estimateGas")]
async fn estimate_gas(
&self, request: TransactionRequest, block_number: Option<BlockId>,
state_override: Option<StateOverride>,
state_override: Option<RpcStateOverride>,
) -> RpcResult<U256>;

/// Returns the current price per gas in wei.
Expand Down
63 changes: 40 additions & 23 deletions crates/rpc/rpc-eth-impl/src/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ use cfx_rpc_cfx_types::{
};
use cfx_rpc_eth_api::EthApiServer;
use cfx_rpc_eth_types::{
Block, BlockNumber as BlockId, BlockOverrides, EthRpcLogFilter,
EthRpcLogFilter as Filter, EvmOverrides, FeeHistory, Header, Log, Receipt,
StateOverride, SyncInfo, SyncStatus, Transaction, TransactionRequest,
AccountOverride, Block, BlockNumber as BlockId, BlockOverrides,
EthRpcLogFilter, EthRpcLogFilter as Filter, EvmOverrides, FeeHistory,
Header, Log, Receipt, RpcStateOverride, SyncInfo, SyncStatus, Transaction,
TransactionRequest,
};
use cfx_rpc_primitives::{Bytes, Index, U64 as HexU64};
use cfx_rpc_utils::error::{
Expand All @@ -39,6 +40,7 @@ use primitives::{
};
use rustc_hex::ToHex;
use solidity_abi::string_revert_reason_decode;
use std::collections::HashMap;

type BlockNumber = BlockId;
type BlockNumberOrTag = BlockId;
Expand Down Expand Up @@ -99,7 +101,9 @@ impl EthApi {

pub fn exec_transaction(
&self, mut request: TransactionRequest,
block_number_or_hash: Option<BlockNumber>, evm_overrides: EvmOverrides,
block_number_or_hash: Option<BlockNumber>,
state_overrides: Option<RpcStateOverride>,
block_overrides: Option<Box<BlockOverrides>>,
) -> CoreResult<(Executed, U256)> {
let consensus_graph = self.consensus_graph();

Expand All @@ -125,18 +129,25 @@ impl EthApi {
}
}

if evm_overrides.has_state() {
let state_override = evm_overrides.state.as_ref().unwrap();
let invalid_item = state_override.iter().any(|(_, item)| {
item.state.is_some() && item.state_diff.is_some()
});
if invalid_item {
return Err(invalid_params_msg(
"Cannot set both state and stateDiff in stateOverride",
)
.into());
let state_overrides = match state_overrides {
Some(states) => {
let mut state_overrides = HashMap::new();
for (address, rpc_account_override) in states {
let account_override =
AccountOverride::try_from(rpc_account_override)
.map_err(|err| {
CoreError::InvalidParam(
err.into(),
Default::default(),
)
})?;
state_overrides.insert(address, account_override);
}
Some(state_overrides)
}
}
None => None,
};
let evm_overrides = EvmOverrides::new(state_overrides, block_overrides);

let epoch = match block_number_or_hash.unwrap_or_default() {
BlockNumber::Hash { hash, .. } => {
Expand Down Expand Up @@ -1251,12 +1262,15 @@ impl EthApiServer for EthApi {
/// on the block chain.
async fn call(
&self, request: TransactionRequest, block_number: Option<BlockId>,
state_overrides: Option<StateOverride>,
state_overrides: Option<RpcStateOverride>,
block_overrides: Option<Box<BlockOverrides>>,
) -> RpcResult<Bytes> {
let evm_overrides = EvmOverrides::new(state_overrides, block_overrides);
let (execution, _estimation) =
self.exec_transaction(request, block_number, evm_overrides)?;
let (execution, _estimation) = self.exec_transaction(
request,
block_number,
state_overrides,
block_overrides,
)?;

Ok(execution.output.into())
}
Expand Down Expand Up @@ -1296,11 +1310,14 @@ impl EthApiServer for EthApi {
/// the transaction to complete.
async fn estimate_gas(
&self, request: TransactionRequest, block_number: Option<BlockId>,
state_override: Option<StateOverride>,
state_overrides: Option<RpcStateOverride>,
) -> RpcResult<U256> {
let evm_overrides = EvmOverrides::new(state_override, None);
let (_, estimated_gas) =
self.exec_transaction(request, block_number, evm_overrides)?;
let (_, estimated_gas) = self.exec_transaction(
request,
block_number,
state_overrides,
None,
)?;

Ok(estimated_gas)
}
Expand Down
5 changes: 4 additions & 1 deletion crates/rpc/rpc-eth-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ pub use fee_history::FeeHistory;
pub use filter::*;
pub use log::Log;
pub use receipt::Receipt;
pub use state::{AccountOverride, EvmOverrides, StateOverride};
pub use state::{
AccountOverride, AccountStateOverrideMode, EvmOverrides,
RpcAccountOverride, RpcStateOverride, StateOverride,
};
pub use sync::{SyncInfo, SyncStatus};
pub use trace_filter::TraceFilter;
pub use transaction::Transaction;
Expand Down
Loading

0 comments on commit 7f1130f

Please sign in to comment.