Skip to content

Commit

Permalink
update is_precompile ref: rust-ethereum/evm#157
Browse files Browse the repository at this point in the history
  • Loading branch information
zjb0807 committed Jan 16, 2024
1 parent e5e853f commit 26ae671
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 26 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions modules/evm-utility/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ sha3 = { workspace = true }

sp-std = { workspace = true }

evm = { git = "https://github.com/rust-blockchain/evm", rev = "2e9c3b71f7b9ff241735f15145517d0627c469e6", default-features = false, features = ["with-codec"] }
evm-gasometer = { git = "https://github.com/rust-blockchain/evm", rev = "2e9c3b71f7b9ff241735f15145517d0627c469e6", default-features = false }
evm-runtime = { git = "https://github.com/rust-blockchain/evm", rev = "2e9c3b71f7b9ff241735f15145517d0627c469e6", default-features = false }
evm = { git = "https://github.com/rust-blockchain/evm", rev = "5fb8a4957c642134782e184c47f7d7b5a29f3629", default-features = false, features = ["with-codec"] }
evm-gasometer = { git = "https://github.com/rust-blockchain/evm", rev = "5fb8a4957c642134782e184c47f7d7b5a29f3629", default-features = false }
evm-runtime = { git = "https://github.com/rust-blockchain/evm", rev = "5fb8a4957c642134782e184c47f7d7b5a29f3629", default-features = false }
#evm = { version = "0.41.1", default-features = false, features = ["with-codec"] }
#evm-gasometer = { version = "0.41.0", default-features = false }
#evm-runtime = { version = "0.41.0", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion modules/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub use module_evm_utility::{
ethereum::{AccessListItem, Log, TransactionAction},
evm::{
self,
executor::stack::{PrecompileFailure, PrecompileHandle, PrecompileOutput, PrecompileSet},
executor::stack::{IsPrecompileResult, PrecompileFailure, PrecompileHandle, PrecompileOutput, PrecompileSet},
Config as EvmConfig, Context, ExitError, ExitFatal, ExitReason, ExitRevert, ExitSucceed,
},
Account,
Expand Down
50 changes: 38 additions & 12 deletions modules/evm/src/runner/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use crate::{
encode_revert_message,
runner::tagged_runtime::{RuntimeKind, TaggedRuntime},
PrecompileFailure, PrecompileHandle, PrecompileOutput, PrecompileSet, StorageMeter,
IsPrecompileResult, PrecompileFailure, PrecompileHandle, PrecompileOutput, PrecompileSet, StorageMeter,
};
use core::{cmp::min, convert::Infallible};
use module_evm_utility::{
Expand Down Expand Up @@ -1192,12 +1192,17 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> Handler

fn code(&self, address: H160) -> Vec<u8> {
let code = self.state.code(address);
if code.len().is_zero() && !self.precompile_set.is_precompile(address) {
log::debug!(
target: "evm",
"contract does not exist, address: {:?}",
address
);
if code.len().is_zero() {
if let IsPrecompileResult::Answer {
is_precompile: false, ..
} = self.precompile_set.is_precompile(address, u64::zero())
{
log::debug!(
target: "evm",
"contract does not exist, address: {:?}",
address
);
}
}
code
}
Expand All @@ -1218,11 +1223,27 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> Handler
}
}

fn is_cold(&self, address: H160, maybe_index: Option<H256>) -> bool {
match maybe_index {
None => !self.precompile_set.is_precompile(address) && self.state.is_cold(address),
fn is_cold(&mut self, address: H160, maybe_index: Option<H256>) -> Result<bool, ExitError> {
Ok(match maybe_index {
None => {
let is_precompile = match self
.precompile_set
.is_precompile(address, self.state.metadata().gasometer.gas())
{
IsPrecompileResult::Answer {
is_precompile,
extra_cost,
} => {
self.state.metadata_mut().gasometer.record_cost(extra_cost)?;
is_precompile
}
IsPrecompileResult::OutOfGas => return Err(ExitError::OutOfGas),
};

!is_precompile && self.state.is_cold(address)
}
Some(index) => self.state.is_storage_cold(address, index),
}
})
}

fn gas_left(&self) -> U256 {
Expand Down Expand Up @@ -1438,10 +1459,15 @@ impl<'inner, 'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> Pr
// Since we don't go through opcodes we need manually record the call
// cost. Not doing so will make the code panic as recording the call stipend
// will do an underflow.
let target_is_cold = match self.executor.is_cold(code_address, None) {
Ok(x) => x,
Err(err) => return (ExitReason::Error(err), Vec::new()),
};

let gas_cost = gasometer::GasCost::Call {
value: transfer.clone().map(|x| x.value).unwrap_or_else(U256::zero),
gas: U256::from(gas_limit.unwrap_or(u64::MAX)),
target_is_cold: self.executor.is_cold(code_address, None),
target_is_cold,
target_exists: self.executor.exists(code_address),
};

Expand Down
31 changes: 25 additions & 6 deletions runtime/common/src/precompile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ use module_evm::{
Blake2F, Bn128Add, Bn128Mul, Bn128Pairing, ECRecover, ECRecoverPublicKey, Identity, IstanbulModexp, Modexp,
Precompile, Ripemd160, Sha256, Sha3FIPS256, Sha3FIPS512,
},
ExitRevert, PrecompileFailure, PrecompileHandle, PrecompileResult, PrecompileSet,
ExitRevert, IsPrecompileResult, PrecompileFailure, PrecompileHandle, PrecompileResult, PrecompileSet,
};
use module_support::{PrecompileCallerFilter, PrecompilePauseFilter};
use sp_core::H160;
use sp_runtime::traits::Zero;
use sp_std::{collections::btree_set::BTreeSet, marker::PhantomData};

pub mod dex;
Expand Down Expand Up @@ -234,7 +235,11 @@ where
fn execute(&self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {
let context = handle.context();
let address = handle.code_address();
if !self.is_precompile(address) {

if let IsPrecompileResult::Answer {
is_precompile: false, ..
} = self.is_precompile(address, u64::zero())
{
return None;
}

Expand Down Expand Up @@ -343,8 +348,19 @@ where
result
}

fn is_precompile(&self, address: H160) -> bool {
self.set.contains(&address) || E::is_precompile(&Default::default(), address)
fn is_precompile(&self, address: H160, _remaining_gas: u64) -> IsPrecompileResult {
let is_precompile = {
self.set.contains(&address)
|| match E::is_precompile(&Default::default(), address, u64::zero()) {
IsPrecompileResult::Answer { is_precompile, .. } => is_precompile,
_ => false,
}
};

IsPrecompileResult::Answer {
is_precompile,
extra_cost: 0,
}
}
}

Expand All @@ -369,8 +385,11 @@ where
}
}

fn is_precompile(&self, address: H160) -> bool {
address == LIQUID_CROWDLOAN
fn is_precompile(&self, address: H160, _remaining_gas: u64) -> IsPrecompileResult {
IsPrecompileResult::Answer {
is_precompile: address == LIQUID_CROWDLOAN,
extra_cost: 0,
}
}
}

Expand Down

0 comments on commit 26ae671

Please sign in to comment.