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

[MOON-2092] Implement call state override for eth_call #163

Open
wants to merge 11 commits into
base: moonbeam-polkadot-v0.9.37
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 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
4 changes: 4 additions & 0 deletions Cargo.lock

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

25 changes: 24 additions & 1 deletion client/rpc-core/src/types/call_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@

use crate::types::Bytes;
use ethereum::AccessListItem;
use ethereum_types::{H160, U256};
use ethereum_types::{H160, H256, U256};
use serde::Deserialize;
use std::collections::BTreeMap;

/// Call request
#[derive(Clone, Debug, Default, Eq, PartialEq, Deserialize)]
Expand Down Expand Up @@ -49,4 +50,26 @@ pub struct CallRequest {
/// EIP-2718 type
#[serde(rename = "type")]
pub transaction_type: Option<U256>,
/// Call state override
nbaztec marked this conversation as resolved.
Show resolved Hide resolved
#[serde(rename = "stateOverrides")]
pub state_overrides: Option<BTreeMap<H160, CallStateOverride>>,
}

// State override
#[derive(Clone, Debug, Default, Eq, PartialEq, Deserialize)]
#[serde(deny_unknown_fields)]
#[serde(rename_all = "camelCase")]
pub struct CallStateOverride {
/// Fake balance to set for the account before executing the call.
pub balance: Option<U256>,
/// Fake nonce to set for the account before executing the call.
pub nonce: Option<U256>,
/// Fake EVM bytecode to inject into the account before executing the call.
pub code: Option<Bytes>,
/// Fake key-value mapping to override all slots in the account storage before
/// executing the call.
pub state: Option<BTreeMap<H256, H256>>,
/// Fake key-value mapping to override individual slots in the account storage before
/// executing the call.
pub state_diff: Option<BTreeMap<H256, H256>>,
}
2 changes: 1 addition & 1 deletion client/rpc-core/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub use self::{
block::{Block, BlockTransactions, Header, Rich, RichBlock, RichHeader},
block_number::BlockNumber,
bytes::Bytes,
call_request::CallRequest,
call_request::{CallRequest, CallStateOverride},
fee::{FeeHistory, FeeHistoryCache, FeeHistoryCacheItem, FeeHistoryCacheLimit},
filter::{
Filter, FilterAddress, FilterChanges, FilterPool, FilterPoolItem, FilterType,
Expand Down
2 changes: 2 additions & 0 deletions client/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ sp-core = { workspace = true }
sp-io = { workspace = true }
sp-runtime = { workspace = true }
sp-storage = { workspace = true }
sp-state-machine = { workspace = true }
# Frontier
fc-db = { workspace = true }
fc-rpc-core = { workspace = true }
fp-ethereum = { workspace = true, features = ["std"] }
fp-rpc = { workspace = true, features = ["std"] }
fp-storage = { workspace = true, features = ["std"] }
fp-evm = { workspace = true }

[dev-dependencies]
tempfile = "3.3.0"
Expand Down
5 changes: 3 additions & 2 deletions client/rpc/src/eth/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ use sp_core::hashing::keccak_256;
use sp_runtime::traits::{BlakeTwo256, Block as BlockT};
// Frontier
use fc_rpc_core::types::*;
use fp_rpc::EthereumRuntimeRPCApi;
use fp_rpc::{EthereumRuntimeAddressMapping, EthereumRuntimeRPCApi};

use crate::{
eth::{rich_block_build, Eth},
frontier_backend_client, internal_err,
};

impl<B, C, P, CT, BE, H: ExHashT, A: ChainApi, EGA> Eth<B, C, P, CT, BE, H, A, EGA>
impl<B, C, P, CT, BE, H: ExHashT, A: ChainApi, M: EthereumRuntimeAddressMapping, EGA>
Eth<B, C, P, CT, BE, H, A, M, EGA>
where
B: BlockT<Hash = H256> + Send + Sync + 'static,
C: StorageProvider<B, BE> + HeaderBackend<B> + Send + Sync + 'static,
Expand Down
5 changes: 3 additions & 2 deletions client/rpc/src/eth/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ use sp_runtime::{
};
// Frontier
use fc_rpc_core::types::*;
use fp_rpc::EthereumRuntimeRPCApi;
use fp_rpc::{EthereumRuntimeAddressMapping, EthereumRuntimeRPCApi};

use crate::{eth::Eth, frontier_backend_client, internal_err};

impl<B, C, P, CT, BE, H: ExHashT, A: ChainApi, EGA> Eth<B, C, P, CT, BE, H, A, EGA>
impl<B, C, P, CT, BE, H: ExHashT, A: ChainApi, M: EthereumRuntimeAddressMapping, EGA>
Eth<B, C, P, CT, BE, H, A, M, EGA>
where
B: BlockT<Hash = H256> + Send + Sync + 'static,
C: ProvideRuntimeApi<B> + StorageProvider<B, BE>,
Expand Down
166 changes: 140 additions & 26 deletions client/rpc/src/eth/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,29 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use std::sync::Arc;
use std::{collections::BTreeMap, sync::Arc};

use ethereum_types::{H256, U256};
use ethereum_types::{H160, H256, U256};
use evm::{ExitError, ExitReason};
use jsonrpsee::core::RpcResult as Result;
use scale_codec::Encode;
// Substrate
use sc_client_api::backend::{Backend, StateBackend, StorageProvider};
use sc_network_common::ExHashT;
use sc_transaction_pool::ChainApi;
use sp_api::{ApiExt, ProvideRuntimeApi};
use sp_api::{ApiExt, CallApiAt, ProvideRuntimeApi};
use sp_block_builder::BlockBuilder as BlockBuilderApi;
use sp_blockchain::HeaderBackend;
use sp_io::hashing::{blake2_128, twox_128};
use sp_runtime::{
generic::BlockId,
traits::{BlakeTwo256, Block as BlockT},
SaturatedConversion,
};
// Frontier
use fc_rpc_core::types::*;
use fp_rpc::EthereumRuntimeRPCApi;
use fp_rpc::{EthereumRuntimeAddressMapping, EthereumRuntimeRPCApi};
use fp_storage::{EVM_ACCOUNT_CODES, PALLET_EVM};

use crate::{
eth::{pending_runtime_api, Eth},
Expand Down Expand Up @@ -63,11 +66,12 @@ impl EstimateGasAdapter for () {
}
}

impl<B, C, P, CT, BE, H: ExHashT, A: ChainApi, EGA> Eth<B, C, P, CT, BE, H, A, EGA>
impl<B, C, P, CT, BE, H: ExHashT, A: ChainApi, M: EthereumRuntimeAddressMapping, EGA>
Eth<B, C, P, CT, BE, H, A, M, EGA>
where
B: BlockT<Hash = H256> + Send + Sync + 'static,
C: ProvideRuntimeApi<B> + StorageProvider<B, BE>,
C: HeaderBackend<B> + Send + Sync + 'static,
C: HeaderBackend<B> + CallApiAt<B> + Send + Sync + 'static,
C::Api: BlockBuilderApi<B> + EthereumRuntimeRPCApi<B>,
BE: Backend<B> + 'static,
BE::State: StateBackend<BlakeTwo256>,
Expand All @@ -86,6 +90,7 @@ where
data,
nonce,
access_list,
state_overrides,
..
} = request;

Expand Down Expand Up @@ -205,26 +210,57 @@ where
Ok(Bytes(info.value))
} else if api_version == 4 {
// Post-london + access list support
let access_list = access_list.unwrap_or_default();
let info = api
.call(
&id,
from.unwrap_or_default(),
to,
data,
value.unwrap_or_default(),
gas_limit,
max_fee_per_gas,
max_priority_fee_per_gas,
nonce,
false,
Some(
access_list
.into_iter()
.map(|item| (item.address, item.storage_keys))
.collect(),
),
)
let encoded_params = sp_api::Encode::encode(&(
&from.unwrap_or_default(),
&to,
&data,
&value.unwrap_or_default(),
&gas_limit,
&max_fee_per_gas,
&max_priority_fee_per_gas,
&nonce,
&false,
&Some(
access_list
.unwrap_or_default()
.into_iter()
.map(|item| (item.address, item.storage_keys))
.collect::<Vec<(sp_core::H160, Vec<H256>)>>(),
),
));

let block_hash = self.client.expect_block_hash_from_id(&id).map_err(|err| {
internal_err(format!("failed retrieving block hash: {:?}", err))
})?;
let overlayed_changes =
self.create_overrides_overlay(block_hash, api_version, state_overrides);
let storage_transaction_cache = std::cell::RefCell::<
sp_api::StorageTransactionCache<B, C::StateBackend>,
>::default();
let params = sp_api::CallApiAtParams {
at: &id,
function: "EthereumRuntimeRPCApi_call",
arguments: encoded_params,
overlayed_changes: &std::cell::RefCell::new(overlayed_changes),
storage_transaction_cache: &storage_transaction_cache,
context: sp_api::ExecutionContext::OffchainCall(None),
recorder: &None,
};
let info = self
.client
.call_api_at(params)
.and_then(|r| {
std::result::Result::map_err(
<std::result::Result<
fp_evm::CallInfo,
sp_runtime::DispatchError,
> as sp_api::Decode>::decode(&mut &r[..]),
|error| sp_api::ApiError::FailedToDecodeReturnValue {
function: "EthereumRuntimeRPCApi_call",
error,
},
)
})
.map_err(|err| internal_err(format!("runtime error: {:?}", err)))?
.map_err(|err| internal_err(format!("execution fatal: {:?}", err)))?;

Expand Down Expand Up @@ -709,6 +745,84 @@ where
Ok(highest)
}
}

/// Given an address mapped `CallStateOverride`, creates `OverlayedChanges` to be used for
/// `CallApiAt` eth_call.
fn create_overrides_overlay(
&self,
block_hash: B::Hash,
api_version: u32,
state_overrides: Option<BTreeMap<H160, CallStateOverride>>,
) -> sp_api::OverlayedChanges {
let mut overlayed_changes = sp_api::OverlayedChanges::default();
if let Some(state_overrides) = state_overrides {
for (address, state_override) in state_overrides {
if let Some(runtime_state_override) = self.runtime_state_override.as_ref() {
runtime_state_override.set_overlayed_changes(
self.client.as_ref(),
&mut overlayed_changes,
block_hash,
api_version,
address,
state_override.balance,
state_override.nonce,
);
}

if let Some(code) = &state_override.code {
let mut key = [twox_128(PALLET_EVM), twox_128(EVM_ACCOUNT_CODES)]
.concat()
.to_vec();
key.extend(blake2_128(address.as_bytes()));
key.extend(address.as_bytes());
let encoded_code = code.clone().into_vec().encode();
overlayed_changes.set_storage(key.clone(), Some(encoded_code));
}

let mut account_storage_key = [
twox_128(PALLET_EVM),
twox_128(fp_storage::EVM_ACCOUNT_STORAGES),
]
.concat()
.to_vec();
account_storage_key.extend(blake2_128(address.as_bytes()));
account_storage_key.extend(address.as_bytes());

// Use `state` first. If `stateDiff` is also present, it resolves consistently
if let Some(state) = &state_override.state {
// clear all storage
if let Ok(all_keys) = self.client.storage_keys(
block_hash,
&sp_storage::StorageKey(account_storage_key.clone()),
) {
for key in all_keys {
overlayed_changes.set_storage(key.0, None);
}
}
// set provided storage
for (k, v) in state {
let mut slot_key = account_storage_key.clone();
slot_key.extend(blake2_128(k.as_bytes()));
slot_key.extend(k.as_bytes());

overlayed_changes.set_storage(slot_key, Some(v.as_bytes().to_owned()));
}
}

if let Some(state_diff) = &state_override.state_diff {
for (k, v) in state_diff {
let mut slot_key = account_storage_key.clone();
slot_key.extend(blake2_128(k.as_bytes()));
slot_key.extend(k.as_bytes());

overlayed_changes.set_storage(slot_key, Some(v.as_bytes().to_owned()));
}
}
}
}

overlayed_changes
}
}

pub fn error_on_execution_failure(reason: &ExitReason, data: &[u8]) -> Result<()> {
Expand Down
5 changes: 3 additions & 2 deletions client/rpc/src/eth/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ use sp_runtime::{
};
// Frontier
use fc_rpc_core::types::*;
use fp_rpc::EthereumRuntimeRPCApi;
use fp_rpc::{EthereumRuntimeAddressMapping, EthereumRuntimeRPCApi};

use crate::{eth::Eth, frontier_backend_client, internal_err};

impl<B, C, P, CT, BE, H: ExHashT, A: ChainApi, EGA> Eth<B, C, P, CT, BE, H, A, EGA>
impl<B, C, P, CT, BE, H: ExHashT, A: ChainApi, M: EthereumRuntimeAddressMapping, EGA>
Eth<B, C, P, CT, BE, H, A, M, EGA>
where
B: BlockT<Hash = H256> + Send + Sync + 'static,
C: ProvideRuntimeApi<B> + StorageProvider<B, BE>,
Expand Down
5 changes: 4 additions & 1 deletion client/rpc/src/eth/mining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ use sc_transaction_pool::ChainApi;
use sp_runtime::traits::Block as BlockT;
// Frontier
use fc_rpc_core::types::*;
use fp_rpc::EthereumRuntimeAddressMapping;

use crate::eth::Eth;

impl<B: BlockT, C, P, CT, BE, H: ExHashT, A: ChainApi, EGA> Eth<B, C, P, CT, BE, H, A, EGA> {
impl<B: BlockT, C, P, CT, BE, H: ExHashT, A: ChainApi, M: EthereumRuntimeAddressMapping, EGA>
Eth<B, C, P, CT, BE, H, A, M, EGA>
{
pub fn is_mining(&self) -> Result<bool> {
Ok(self.is_authority)
}
Expand Down
Loading