Skip to content

Commit

Permalink
Self-review and linting
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSchiavini committed Apr 23, 2024
1 parent e0cde8f commit b7831c7
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 28 deletions.
14 changes: 7 additions & 7 deletions src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ use std::collections::{HashMap, HashSet};
use std::fmt::Debug;
use std::mem::replace;

use pyo3::{pyclass, pymethods, PyObject, PyResult, Python};
use pyo3::exceptions::{PyKeyError, PyOverflowError};
use pyo3::types::PyBytes;
use revm::{Evm, EvmContext, JournalCheckpoint as RevmCheckpoint, primitives::U256};
use pyo3::{pyclass, pymethods, PyObject, PyResult, Python};
use revm::precompile::{Address, Bytes};
use revm::primitives::ExecutionResult::Success;
use revm::primitives::{
BlockEnv as RevmBlockEnv, CreateScheme, Env as RevmEnv, ExecutionResult as RevmExecutionResult,
HandlerCfg, Output, SpecId, TransactTo, TxEnv as RevmTxEnv
HandlerCfg, Output, SpecId, TransactTo, TxEnv as RevmTxEnv,
};
use revm::primitives::ExecutionResult::Success;
use revm::{primitives::U256, Evm, EvmContext, JournalCheckpoint as RevmCheckpoint};
use tracing::trace;

use crate::database::DB;
use crate::executor::call_evm;
use crate::types::{PyByteVec, PyDB};
use crate::{
types::{AccountInfo, BlockEnv, Env, ExecutionResult, JournalCheckpoint, TxEnv},
utils::{addr, pyerr},
};
use crate::database::DB;
use crate::executor::call_evm;
use crate::types::{PyByteVec, PyDB};

#[derive(Debug)]
#[pyclass]
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@

use pyo3::prelude::*;

mod types;
mod evm;
mod database;
mod empty_db_wrapper;
mod evm;
mod executor;
mod pystdout;
mod types;
mod utils;

pub use types::*;
pub use evm::EVM;
pub use types::*;
pub use utils::fake_exponential;

#[pymodule]
Expand Down
39 changes: 26 additions & 13 deletions src/types/evm_env.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use std::default::Default;

use pyo3::{pyclass, PyErr, pymethods, PyObject, PyResult, Python, types::PyBytes};
use pyo3::types::PyTuple;
use revm::primitives::{Address, B256, BlobExcessGasAndPrice, BlockEnv as RevmBlockEnv, CfgEnv as RevmCfgEnv, CreateScheme, Env as RevmEnv, TransactTo, TxEnv as RevmTxEnv, U256};
use pyo3::{pyclass, pymethods, types::PyBytes, PyErr, PyObject, PyResult, Python};
use revm::primitives::{
Address, BlobExcessGasAndPrice, BlockEnv as RevmBlockEnv, CfgEnv as RevmCfgEnv, CreateScheme,
Env as RevmEnv, TransactTo, TxEnv as RevmTxEnv, B256, U256,
};

use crate::utils::{addr, addr_or_zero, from_pybytes};

Expand Down Expand Up @@ -71,7 +74,7 @@ impl TxEnv {
chain_id: Option<u64>,
nonce: Option<u64>,
salt: Option<U256>,
access_list: Option<Vec<&PyTuple/*str, list[int]*/>>,
access_list: Option<Vec<&PyTuple /*str, list[int]*/>>,
blob_hashes: Option<Vec<&PyBytes>>,
max_fee_per_blob_gas: Option<U256>,
) -> PyResult<Self> {
Expand All @@ -91,19 +94,21 @@ impl TxEnv {
chain_id,
nonce,
access_list: access_list
.unwrap_or_else(Vec::default)
.unwrap_or_default()
.iter()
.map(|tuple| Ok::<(Address, Vec<U256>), PyErr>((
addr(tuple.get_item(0)?.extract()?)?,
tuple.get_item(1)?.extract::<Vec<U256>>()?
)))
.map(|tuple| {
Ok::<(Address, Vec<U256>), PyErr>((
addr(tuple.get_item(0)?.extract()?)?,
tuple.get_item(1)?.extract::<Vec<U256>>()?,
))
})
.collect::<PyResult<Vec<(Address, Vec<U256>)>>>()?,
blob_hashes: blob_hashes
.unwrap_or_else(Vec::default)
.unwrap_or_default()
.iter()
.map(|b| Ok::<B256, PyErr>(from_pybytes(b)?))
.map(|b| from_pybytes(b))
.collect::<PyResult<Vec<B256>>>()?,
max_fee_per_blob_gas
max_fee_per_blob_gas,
}))
}

Expand Down Expand Up @@ -165,12 +170,20 @@ impl TxEnv {

#[getter]
fn access_list(&self) -> Vec<(String, Vec<U256>)> {
self.0.access_list.iter().map(|(a, b)| (a.to_string(), b.clone())).collect()
self.0
.access_list
.iter()
.map(|(a, b)| (a.to_string(), b.clone()))
.collect()
}

#[getter]
fn blob_hashes(&self, py: Python<'_>) -> Vec<PyObject> {
self.0.blob_hashes.iter().map(|i| PyBytes::new(py, i.0.as_ref()).into()).collect()
self.0
.blob_hashes
.iter()
.map(|i| PyBytes::new(py, i.0.as_ref()).into())
.collect()
}

#[getter]
Expand Down
7 changes: 3 additions & 4 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use pyo3::exceptions::PyRuntimeError;
use pyo3::{exceptions::PyTypeError, prelude::*};
use revm::primitives::{Address, fake_exponential as revm_fake_exponential};
use std::fmt::Debug;
use pyo3::types::PyBytes;
use pyo3::{exceptions::PyTypeError, prelude::*};
use revm::precompile::B256;
use revm::primitives::{fake_exponential as revm_fake_exponential, Address};
use std::fmt::Debug;

pub(crate) fn addr(s: &str) -> Result<Address, PyErr> {
s.parse::<Address>()
Expand All @@ -22,7 +22,6 @@ pub(crate) fn pyerr<T: Debug>(err: T) -> PyErr {
PyRuntimeError::new_err(format!("{:?}", err))
}


pub(crate) fn from_pybytes(b: &PyBytes) -> PyResult<B256> {
B256::try_from(b.as_bytes()).map_err(|e| PyTypeError::new_err(e.to_string()))
}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cancun.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


DEPLOYER = "0x1111111111111111111111111111111111111111"
TRUSTED_SETUP = os.path.join(os.path.dirname(__file__), "fixtures/kzg_trusted_setup.txt")
TRUSTED_SETUP = os.path.join(os.path.dirname(__file__), "fixtures/kzg_trusted_setup.txt") # file from eth_account
MIN_BLOB_BASE_FEE = 1
BLOB_BASE_FEE_UPDATE_FRACTION = 3_338_477
VERSIONED_HASH_VERSION_KZG = b"\x01"
Expand Down

0 comments on commit b7831c7

Please sign in to comment.