From b7831c7fdbba057744ad6bc0a6cce741993b6c52 Mon Sep 17 00:00:00 2001 From: Daniel Schiavini Date: Tue, 23 Apr 2024 18:26:09 +0200 Subject: [PATCH] Self-review and linting --- src/evm.rs | 14 +++++++------- src/lib.rs | 6 +++--- src/types/evm_env.rs | 39 ++++++++++++++++++++++++++------------- src/utils.rs | 7 +++---- tests/test_cancun.py | 2 +- 5 files changed, 40 insertions(+), 28 deletions(-) diff --git a/src/evm.rs b/src/evm.rs index c6d016c..318bf96 100644 --- a/src/evm.rs +++ b/src/evm.rs @@ -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] diff --git a/src/lib.rs b/src/lib.rs index f706425..ec6a876 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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] diff --git a/src/types/evm_env.rs b/src/types/evm_env.rs index 2bc2009..f001e55 100644 --- a/src/types/evm_env.rs +++ b/src/types/evm_env.rs @@ -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}; @@ -71,7 +74,7 @@ impl TxEnv { chain_id: Option, nonce: Option, salt: Option, - access_list: Option>, + access_list: Option>, blob_hashes: Option>, max_fee_per_blob_gas: Option, ) -> PyResult { @@ -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), PyErr>(( - addr(tuple.get_item(0)?.extract()?)?, - tuple.get_item(1)?.extract::>()? - ))) + .map(|tuple| { + Ok::<(Address, Vec), PyErr>(( + addr(tuple.get_item(0)?.extract()?)?, + tuple.get_item(1)?.extract::>()?, + )) + }) .collect::)>>>()?, blob_hashes: blob_hashes - .unwrap_or_else(Vec::default) + .unwrap_or_default() .iter() - .map(|b| Ok::(from_pybytes(b)?)) + .map(|b| from_pybytes(b)) .collect::>>()?, - max_fee_per_blob_gas + max_fee_per_blob_gas, })) } @@ -165,12 +170,20 @@ impl TxEnv { #[getter] fn access_list(&self) -> Vec<(String, Vec)> { - 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 { - 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] diff --git a/src/utils.rs b/src/utils.rs index c1bf2b2..53f68ee 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -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 { s.parse::
() @@ -22,7 +22,6 @@ pub(crate) fn pyerr(err: T) -> PyErr { PyRuntimeError::new_err(format!("{:?}", err)) } - pub(crate) fn from_pybytes(b: &PyBytes) -> PyResult { B256::try_from(b.as_bytes()).map_err(|e| PyTypeError::new_err(e.to_string())) } diff --git a/tests/test_cancun.py b/tests/test_cancun.py index 13a47ce..396636f 100644 --- a/tests/test_cancun.py +++ b/tests/test_cancun.py @@ -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"