Skip to content

Add insert storage #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pyrevm"
version = "0.3.3"
version = "0.3.4"
edition = "2021"

[lib]
Expand Down
8 changes: 8 additions & 0 deletions pyrevm.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,14 @@ class EVM:
:param info: The account info.
"""

def insert_account_storage(self: "EVM", address: str, index: int, value: int) -> None:
"""
Inserts the provided value for slot of in the database at the specified address
:param address: The address of the account.
:param index: slot in storage.
:param value: value for slot.
"""

def message_call(
self: "EVM",
caller: str,
Expand Down
16 changes: 16 additions & 0 deletions src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ impl DB {
}
}

pub(crate) fn insert_insert_account_storage(
&mut self,
address: Address,
slot: U256,
value: U256,
) -> PyResult<()> {
match self {
DB::Memory(db) => db
.insert_account_storage(address, slot, value)
.map_err(pyerr),
DB::Fork(db) => db
.insert_account_storage(address, slot, value)
.map_err(pyerr),
}
}

pub(crate) fn get_accounts(&self) -> &HashMap<Address, DbAccount> {
match self {
DB::Memory(db) => &db.accounts,
Expand Down
17 changes: 14 additions & 3 deletions src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ pub struct EVM {
impl EVM {
/// Create a new EVM instance.
#[new]
#[pyo3(signature = (env = None, fork_url = None, fork_block = None, gas_limit = 18446744073709551615, tracing = false, spec_id = "LATEST"))]
#[pyo3(signature = (env = None, fork_url = None, fork_block = None, gas_limit = 18446744073709551615, tracing = false, spec_id = "LATEST")
)]
fn new(
env: Option<Env>,
fork_url: Option<&str>,
Expand Down Expand Up @@ -151,6 +152,14 @@ impl EVM {
Ok(())
}

/// Inserts the provided value for slot of in the database at the specified address
fn insert_account_storage(&mut self, address: &str, index: U256, value: U256) -> PyResult<()> {
let target = addr(address)?;
self.context
.db
.insert_insert_account_storage(target, index, value)
}

/// Set the balance of a given address.
fn set_balance(&mut self, address: &str, balance: U256) -> PyResult<()> {
let address = addr(address)?;
Expand All @@ -166,7 +175,8 @@ impl EVM {
Ok(balance)
}

#[pyo3(signature = (caller, to, calldata = None, value = None, gas = None, gas_price = None, is_static = false))]
#[pyo3(signature = (caller, to, calldata = None, value = None, gas = None, gas_price = None, is_static = false)
)]
pub fn message_call(
&mut self,
caller: &str,
Expand All @@ -193,7 +203,8 @@ impl EVM {
}

/// Deploy a contract with the given code.
#[pyo3(signature = (deployer, code, value = None, gas = None, gas_price = None, is_static = false, _abi = None))]
#[pyo3(signature = (deployer, code, value = None, gas = None, gas_price = None, is_static = false, _abi = None)
)]
fn deploy(
&mut self,
deployer: &str,
Expand Down
14 changes: 11 additions & 3 deletions tests/test_evm.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

# use your own key during development to avoid rate limiting the CI job
fork_url = (
os.getenv("FORK_URL")
or "https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27"
os.getenv("FORK_URL")
or "https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27"
)

KWARG_CASES = [
Expand Down Expand Up @@ -60,6 +60,14 @@ def test_fork_storage():
assert value > 0


def test_set_into_storage():
weth = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
evm = EVM(fork_url=fork_url, fork_block="latest")
evm.insert_account_storage(weth, 0, 10)
value = evm.storage(weth, 0)
assert value == 10


def test_deploy():
evm = EVM()

Expand Down Expand Up @@ -265,7 +273,7 @@ def test_tx_setters():

@pytest.mark.parametrize(
"excess_blob_gas,expected_fee",
[(0, 1), (10**3, 1), (2**24, 152), (2**26, 537070730)],
[(0, 1), (10 ** 3, 1), (2 ** 24, 152), (2 ** 26, 537070730)],
)
def test_get_blobbasefee(excess_blob_gas, expected_fee):
evm = EVM()
Expand Down
Loading