Skip to content

Commit

Permalink
cleanup, add precompile address to JournalState
Browse files Browse the repository at this point in the history
  • Loading branch information
rakita committed Oct 19, 2023
1 parent 253b72a commit 36f271f
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 28 deletions.
4 changes: 3 additions & 1 deletion crates/precompile/src/bn128.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::{primitives::U256, Address, Error, Precompile, PrecompileWithAddress, PrecompileResult};
use crate::{
primitives::U256, Address, Error, Precompile, PrecompileResult, PrecompileWithAddress,
};
use alloc::vec::Vec;

pub mod add {
Expand Down
2 changes: 1 addition & 1 deletion crates/precompile/src/hash.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::calc_linear_cost_u32;
use crate::{Error, Precompile, PrecompileWithAddress, PrecompileResult, StandardPrecompileFn};
use crate::{Error, Precompile, PrecompileResult, PrecompileWithAddress, StandardPrecompileFn};
use sha2::*;

pub const SHA256: PrecompileWithAddress = PrecompileWithAddress(
Expand Down
2 changes: 1 addition & 1 deletion crates/precompile/src/identity.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::calc_linear_cost_u32;
use crate::{Error, Precompile, PrecompileWithAddress, PrecompileResult, StandardPrecompileFn};
use crate::{Error, Precompile, PrecompileResult, PrecompileWithAddress, StandardPrecompileFn};

pub const FUN: PrecompileWithAddress = PrecompileWithAddress(
crate::u64_to_address(4),
Expand Down
5 changes: 3 additions & 2 deletions crates/precompile/src/kzg_point_evaluation.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::{Address, Error, Precompile, PrecompileWithAddress, PrecompileResult};
use crate::{Address, Error, Precompile, PrecompileResult, PrecompileWithAddress};
use c_kzg::{Bytes32, Bytes48, KzgProof, KzgSettings};
use revm_primitives::{hex_literal::hex, Env};
use sha2::{Digest, Sha256};

pub const POINT_EVALUATION: PrecompileWithAddress = PrecompileWithAddress(ADDRESS, Precompile::Env(run));
pub const POINT_EVALUATION: PrecompileWithAddress =
PrecompileWithAddress(ADDRESS, Precompile::Env(run));

const ADDRESS: Address = crate::u64_to_address(0x0A);
const GAS_COST: u64 = 50_000;
Expand Down
2 changes: 1 addition & 1 deletion crates/precompile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,4 @@ const fn u64_to_address(x: u64) -> Address {
Address::new([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7],
])
}
}
2 changes: 1 addition & 1 deletion crates/precompile/src/modexp.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
primitives::U256,
utilities::{get_right_padded, get_right_padded_vec, left_padding, left_padding_vec},
Error, Precompile, PrecompileWithAddress, PrecompileResult, StandardPrecompileFn,
Error, Precompile, PrecompileResult, PrecompileWithAddress, StandardPrecompileFn,
};
use alloc::vec::Vec;
use aurora_engine_modexp::modexp;
Expand Down
10 changes: 8 additions & 2 deletions crates/revm/src/evm_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,14 @@ impl<'a, GSPEC: Spec + 'static, DB: Database> EVMImpl<'a, GSPEC, DB> {
inspector: Option<&'a mut dyn Inspector<DB>>,
precompiles: Precompiles,
) -> Self {
let journaled_state = JournaledState::new(precompiles.len(), GSPEC::SPEC_ID);
let journaled_state = JournaledState::new(
GSPEC::SPEC_ID,
precompiles
.addresses()
.into_iter()
.cloned()
.collect::<Vec<_>>(),
);
let instruction_table = if inspector.is_some() {
let instruction_table = make_boxed_instruction_table::<Self, GSPEC, _>(
make_instruction_table::<Self, GSPEC>(),
Expand Down Expand Up @@ -656,7 +663,6 @@ impl<'a, GSPEC: Spec + 'static, DB: Database> EVMImpl<'a, GSPEC, DB> {
mut gas: Gas,
) -> CallResult {
let input_data = &inputs.input;
let contract = inputs.contract;

let out = match precompile {
Precompile::Standard(fun) => fun(input_data, gas.limit()),
Expand Down
24 changes: 5 additions & 19 deletions crates/revm/src/journaled_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::primitives::{
use alloc::vec::Vec;
use core::mem;
use revm_interpreter::primitives::SpecId;
use revm_precompile::Precompiles;

#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand Down Expand Up @@ -40,11 +39,7 @@ impl JournaledState {
///
/// Note: This function will journal state after Spurious Dragon fork.
/// And will not take into account if account is not existing or empty.
pub fn new(
num_of_precompiles: usize,
spec: SpecId,
precompile_addresses: Vec<Address>,
) -> JournaledState {
pub fn new(spec: SpecId, precompile_addresses: Vec<Address>) -> JournaledState {
Self {
state: HashMap::new(),
transient_storage: TransientStorage::default(),
Expand All @@ -56,11 +51,6 @@ impl JournaledState {
}
}

/// Is address precompile
pub fn is_precompile(&self, address: &Address) -> bool {
self.precompile_addresses.binary_search(address).is_ok()
}

/// Return reference to state.
pub fn state(&mut self) -> &mut State {
&mut self.state
Expand Down Expand Up @@ -211,8 +201,8 @@ impl JournaledState {
let last_journal = self.journal.last_mut().unwrap();

// check if it is possible to create this account.

if Self::check_account_collision(address, account, self.is_precompile(&address)) {
let is_precompile = self.precompile_addresses.binary_search(&address).is_ok();
if Self::check_account_collision(account, is_precompile) {
self.checkpoint_revert(checkpoint);
return Err(InstructionResult::CreateCollision);
}
Expand Down Expand Up @@ -266,11 +256,7 @@ impl JournaledState {
}

#[inline]
pub fn check_account_collision(
address: Address,
account: &Account,
is_precompile: bool,
) -> bool {
pub fn check_account_collision(account: &Account, is_precompile: bool) -> bool {
// Check collision. Bytecode needs to be empty.
if account.info.code_hash != KECCAK_EMPTY {
return true;
Expand Down Expand Up @@ -553,7 +539,7 @@ impl JournaledState {
.push(JournalEntry::AccountLoaded { address });

// precompiles are warm loaded so we need to take that into account
let is_cold = self.is_precompile(&address);
let is_cold = self.precompile_addresses.binary_search(&address).is_ok();

(vac.insert(account), is_cold)
}
Expand Down

0 comments on commit 36f271f

Please sign in to comment.