Skip to content

Commit

Permalink
chore: change addresses to iterator and add into_addresses (#855)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Nov 10, 2023
1 parent e17c529 commit 6ab5124
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 95 deletions.
178 changes: 91 additions & 87 deletions crates/precompile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,83 +62,18 @@ pub struct Precompiles {
}

impl Precompiles {
/// Extends the precompiles with the given precompiles.
///
/// Other precompiles with overwrite existing precompiles.
pub fn extend(&mut self, other: impl IntoIterator<Item = PrecompileWithAddress>) {
self.inner = self
.inner
.iter()
.cloned()
.chain(other)
.map(|i| (i.0, i.1.clone()))
.collect::<BTreeMap<Address, Precompile>>()
.into_iter()
.map(|(k, v)| PrecompileWithAddress(k, v))
.collect::<Vec<_>>();
}
}

impl Default for Precompiles {
fn default() -> Self {
Self::new(SpecId::LATEST).clone() //berlin
}
}

#[derive(Clone)]
pub enum Precompile {
Standard(StandardPrecompileFn),
Env(EnvPrecompileFn),
}

impl fmt::Debug for Precompile {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Precompile::Standard(_) => f.write_str("Standard"),
Precompile::Env(_) => f.write_str("Env"),
}
}
}

#[derive(Clone, Debug)]
pub struct PrecompileWithAddress(Address, Precompile);

impl From<PrecompileWithAddress> for (Address, Precompile) {
fn from(value: PrecompileWithAddress) -> Self {
(value.0, value.1)
}
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub enum SpecId {
HOMESTEAD,
BYZANTIUM,
ISTANBUL,
BERLIN,
CANCUN,
LATEST,
}

impl SpecId {
/// Returns the appropriate precompile Spec for the primitive [SpecId](revm_primitives::SpecId)
pub const fn from_spec_id(spec_id: revm_primitives::SpecId) -> Self {
use revm_primitives::SpecId::*;
match spec_id {
FRONTIER | FRONTIER_THAWING | HOMESTEAD | DAO_FORK | TANGERINE | SPURIOUS_DRAGON => {
Self::HOMESTEAD
}
BYZANTIUM | CONSTANTINOPLE | PETERSBURG => Self::BYZANTIUM,
ISTANBUL | MUIR_GLACIER => Self::ISTANBUL,
BERLIN | LONDON | ARROW_GLACIER | GRAY_GLACIER | MERGE | SHANGHAI => Self::BERLIN,
CANCUN => Self::CANCUN,
LATEST => Self::LATEST,
#[cfg(feature = "optimism")]
BEDROCK | REGOLITH => Self::BERLIN,
/// Returns the precompiles for the given spec.
pub fn new(spec: SpecId) -> &'static Self {
match spec {
SpecId::HOMESTEAD => Self::homestead(),
SpecId::BYZANTIUM => Self::byzantium(),
SpecId::ISTANBUL => Self::istanbul(),
SpecId::BERLIN => Self::berlin(),
SpecId::CANCUN => Self::cancun(),
SpecId::LATEST => Self::latest(),
}
}
}

impl Precompiles {
/// Returns precompiles for Homestead spec.
pub fn homestead() -> &'static Self {
static INSTANCE: OnceBox<Precompiles> = OnceBox::new();
Expand Down Expand Up @@ -230,24 +165,18 @@ impl Precompiles {
Self::cancun()
}

/// Returns the precompiles for the given spec.
pub fn new(spec: SpecId) -> &'static Self {
match spec {
SpecId::HOMESTEAD => Self::homestead(),
SpecId::BYZANTIUM => Self::byzantium(),
SpecId::ISTANBUL => Self::istanbul(),
SpecId::BERLIN => Self::berlin(),
SpecId::CANCUN => Self::cancun(),
SpecId::LATEST => Self::latest(),
}
}

/// Returns an iterator over the precompiles addresses.
#[inline]
pub fn addresses(&self) -> impl IntoIterator<Item = &Address> {
pub fn addresses(&self) -> impl Iterator<Item = &Address> + '_ {
self.inner.iter().map(|i| &i.0)
}

/// Consumes the type and returns all precompile addresses.
#[inline]
pub fn into_addresses(self) -> impl Iterator<Item = Address> {
self.inner.into_iter().map(|precompile| precompile.0)
}

/// Is the given address a precompile.
#[inline]
pub fn contains(&self, address: &Address) -> bool {
Expand All @@ -273,6 +202,81 @@ impl Precompiles {
pub fn len(&self) -> usize {
self.inner.len()
}

/// Extends the precompiles with the given precompiles.
///
/// Other precompiles with overwrite existing precompiles.
pub fn extend(&mut self, other: impl IntoIterator<Item = PrecompileWithAddress>) {
self.inner = self
.inner
.iter()
.cloned()
.chain(other)
.map(|i| (i.0, i.1.clone()))
.collect::<BTreeMap<Address, Precompile>>()
.into_iter()
.map(|(k, v)| PrecompileWithAddress(k, v))
.collect::<Vec<_>>();
}
}

impl Default for Precompiles {
fn default() -> Self {
Self::new(SpecId::LATEST).clone() //berlin
}
}

#[derive(Clone)]
pub enum Precompile {
Standard(StandardPrecompileFn),
Env(EnvPrecompileFn),
}

impl fmt::Debug for Precompile {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Precompile::Standard(_) => f.write_str("Standard"),
Precompile::Env(_) => f.write_str("Env"),
}
}
}

#[derive(Clone, Debug)]
pub struct PrecompileWithAddress(Address, Precompile);

impl From<PrecompileWithAddress> for (Address, Precompile) {
fn from(value: PrecompileWithAddress) -> Self {
(value.0, value.1)
}
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub enum SpecId {
HOMESTEAD,
BYZANTIUM,
ISTANBUL,
BERLIN,
CANCUN,
LATEST,
}

impl SpecId {
/// Returns the appropriate precompile Spec for the primitive [SpecId](revm_primitives::SpecId)
pub const fn from_spec_id(spec_id: revm_primitives::SpecId) -> Self {
use revm_primitives::SpecId::*;
match spec_id {
FRONTIER | FRONTIER_THAWING | HOMESTEAD | DAO_FORK | TANGERINE | SPURIOUS_DRAGON => {
Self::HOMESTEAD
}
BYZANTIUM | CONSTANTINOPLE | PETERSBURG => Self::BYZANTIUM,
ISTANBUL | MUIR_GLACIER => Self::ISTANBUL,
BERLIN | LONDON | ARROW_GLACIER | GRAY_GLACIER | MERGE | SHANGHAI => Self::BERLIN,
CANCUN => Self::CANCUN,
LATEST => Self::LATEST,
#[cfg(feature = "optimism")]
BEDROCK | REGOLITH => Self::BERLIN,
}
}
}

/// Const function for making an address by concatenating the bytes from two given numbers.
Expand Down
10 changes: 2 additions & 8 deletions crates/revm/src/evm_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,8 @@ impl<'a, SPEC: Spec + 'static, DB: Database> EVMImpl<'a, SPEC, DB> {
inspector: Option<&'a mut dyn Inspector<DB>>,
precompiles: Precompiles,
) -> Self {
let journaled_state = JournaledState::new(
SPEC::SPEC_ID,
precompiles
.addresses()
.into_iter()
.cloned()
.collect::<Vec<_>>(),
);
let journaled_state =
JournaledState::new(SPEC::SPEC_ID, precompiles.addresses().copied().collect());
// If T is present it should be a generic T that modifies handler.
let instruction_table = if inspector.is_some() {
let instruction_table = make_boxed_instruction_table::<Self, SPEC, _>(
Expand Down

0 comments on commit 6ab5124

Please sign in to comment.