Skip to content

Commit

Permalink
restore salt and enabled parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
cassc committed Jul 17, 2024
1 parent 0ce9b80 commit 59dd383
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 29 deletions.
9 changes: 0 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,3 @@ test_global_snapshot[x_fns1-False-slowseq take and restore snapshot] 315.481
test_global_snapshot[x_fns3-True-slowseq no snapshot] 315.5748 (3.32) 328.8318 (3.24) 321.7635 (3.31) 5.0368 (2.12) 321.1631 (3.34) 7.1114 (2.42) 2;0 3.1079 (0.30) 5 1
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
```
## Changes
### 1.0.0
Breaking changes:
- `deterministic_deploy` signature is now changed to `deterministic_deploy(contract_deploy_code, owner=None, data=None, value=None, init_value=None, deploy_to_address=None)`, i.e., `salt` is removed. [Related to [REVM-1182](https://github.com/bluealloy/revm/issues/1182)]. If you want to similate the previous behaviour, it's suggested to use the `deploy_to_address` parameter to specify the address where the contract will be deployed.
- `enabled` in `REVMConfig` is removed. To disable all instrumentation, use
2 changes: 1 addition & 1 deletion benches/infinite_loop.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use criterion::{criterion_group, criterion_main, Criterion};
use primitive_types::{H160, U256};
use primitive_types::U256;
use revm::primitives::Address;
use tinyevm::{fn_sig_to_prefix, TinyEVM, UZERO};

Expand Down
33 changes: 24 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use lazy_static::lazy_static;
use num_bigint::BigInt;
use pyo3::prelude::*;
use response::{Response, SeenPcsMap, WrappedBug, WrappedHeuristics, WrappedMissedBranch};
use revm::inspector_handle_register;
use revm::{inspector_handle_register, primitives::B256};
use thread_local::ThreadLocal;
use tokio::runtime::Runtime;
use uuid::Uuid;
Expand All @@ -39,7 +39,7 @@ pub mod instrument;
/// Provide response data structure from EVM
pub mod response;
pub use common::*;
use hex::ToHex;
use hex::{FromHex, ToHex};
use instrument::{
bug_inspector::BugInspector, log_inspector::LogInspector, BugData, Heuristics, InstrumentConfig,
};
Expand Down Expand Up @@ -705,19 +705,20 @@ impl TinyEVM {
/// - `owner`: Owner address as a 20-byte array encoded as hex string
/// - `data`: (Optional, default empty) Constructor arguments encoded as hex string.
/// - `value`: (Optional, default 0) a U256. Set the value to be included in the contract creation transaction.
/// - `deploy_to_address`: when provided, change the address of the deployed contract to this address
/// - `deploy_to_address`: when provided, change the address of the deployed contract to this address, otherwise deploy to a an address created using `owner.CREATE2(a_fixed_salt, codehash)`.

/// - This requires the constructor to be payable.
/// - The transaction sender (owner) must have enough balance
/// - `init_value`: (Optional) BigInt. Override the initial balance of the contract to this value.
///
/// Returns a list consisting of 4 items `[reason, address-as-byte-array, bug_data, heuristics]`
#[pyo3(signature = (contract_deploy_code, owner=None, data=None, value=None, init_value=None, deploy_to_address=None))]
#[pyo3(signature = (contract_deploy_code, salt=None, owner=None, data=None, value=None, init_value=None, deploy_to_address=None))]
pub fn deterministic_deploy(
&mut self,
contract_deploy_code: String, // variable length
owner: Option<String>, // h160 as hex string
data: Option<String>, // variable length
salt: Option<String>, // h256 as hex string, has no effect if deploy_to_address is provided
owner: Option<String>, // h160 as hex string
data: Option<String>, // variable length
value: Option<BigInt>,
init_value: Option<BigInt>,
deploy_to_address: Option<String>,
Expand All @@ -742,17 +743,31 @@ impl TinyEVM {
let value = value.unwrap_or_default();
let mut contract_bytecode = contract_deploy_code.to_vec();
contract_bytecode.extend(data);
let force_address: Option<Address> = deploy_to_address

let salt = {
if let Some(salt) = salt {
let salt = &salt;
B256::from_hex(salt)?
} else {
B256::ZERO
}
};

let force_address: Address = deploy_to_address
.map(|s| Address::from_str(&s))
.transpose()?;
.transpose()?
.unwrap_or_else(|| {
let codehash = keccak256(&contract_bytecode);
owner.create2(salt, codehash)
});

let resp = {
let resp = self.deploy_helper(
owner,
contract_bytecode,
bigint_to_ruint_u256(&value)?,
None,
force_address,
Some(force_address),
)?;

if let Some(balance) = init_value {
Expand Down
3 changes: 2 additions & 1 deletion tests/test_account_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@ def deploy_contract(salt=None, owner='0x388C818CA8B9251b393131C08a736A67ccB19297

data = encode(['uint256', 'string', 'string', 'uint256'], [_initialSupply, _name, _symbol, _decimals]).hex()

salt = None
value = None
init_value = 0x223312323

tevm.set_balance(owner, 0xffff0000000000000000000000000000000000000000000000000000000000ff)

resp = tevm.deterministic_deploy(binary, owner, data, value, init_value)
resp = tevm.deterministic_deploy(binary, salt, owner, data, value, init_value)
tprint('Deployment resp: {}'.format(resp))

assert resp.success
Expand Down
3 changes: 2 additions & 1 deletion tests/test_global_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ def deploy_contract(salt=None, owner='0x388C818CA8B9251b393131C08a736A67ccB19297

tevm.set_balance(owner, 0xffff0000000000000000000000000000000000000000000000000000000000ff)

salt = None
data = ''
value = None
init_value = None
resp = tevm.deterministic_deploy(binary, owner, data, value, init_value)
resp = tevm.deterministic_deploy(binary, salt, owner, data, value, init_value)
tprint('Deployment resp: {}'.format(resp))

assert resp.success
Expand Down
9 changes: 4 additions & 5 deletions tests/test_multithreading.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from concurrent.futures import ProcessPoolExecutor


salt = None

def handle_exception(exc_type, exc_value, exc_traceback):
if issubclass(exc_type, KeyboardInterrupt):
sys.__excepthook__(exc_type, exc_value, exc_traceback)
Expand All @@ -33,7 +35,6 @@ def run_test():
tevm = tinyevm.TinyEVM()

contract_bytecode = open('tests/contracts/C.hex').read()
salt = None
owner = '0x388C818CA8B9251b393131C08a736A67ccB19297'
data = None
value = None
Expand All @@ -43,8 +44,7 @@ def run_test():
balance = tevm.get_balance(owner)
tprint('balance before deployment: {}'.format(balance))

# todo update response object
resp = tevm.deterministic_deploy(contract_bytecode, owner, data, value, init_value)
resp = tevm.deterministic_deploy(contract_bytecode, salt, owner, data, value, init_value)
tprint('Deployment resp: {}'.format(resp))

assert resp.success
Expand Down Expand Up @@ -75,7 +75,6 @@ def run_infinite_loop():
tevm = tinyevm.TinyEVM()

contract_bytecode = open('tests/contracts/infinite_loop_Test.hex').read()
salt = None
owner = '0x388C818CA8B9251b393131C08a736A67ccB19297'
data = None
value = None
Expand All @@ -86,7 +85,7 @@ def run_infinite_loop():
tprint('balance before deployment: {}'.format(balance))

# todo update response object
resp = tevm.deterministic_deploy(contract_bytecode, owner, data, value, init_value)
resp = tevm.deterministic_deploy(contract_bytecode, salt, owner, data, value, init_value)
tprint('Deployment resp: {}'.format(resp))

assert resp.success
Expand Down
7 changes: 4 additions & 3 deletions tests/test_tinyevm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import unittest
from Crypto.Hash import keccak

salt = None

def fn_sig(sig):
k = keccak.new(digest_bits=256)
k.update(sig.encode())
Expand Down Expand Up @@ -56,7 +58,7 @@ def test_get_change_tx_gas_limit(self):
init_value = 0x223312323

try:
tevm.deterministic_deploy(contract_bytecode, owner, data, value, init_value)
tevm.deterministic_deploy(contract_bytecode, salt, owner, data, value, init_value)
except RuntimeError as e:
tprint('Expected error: {}'.format(e))
assert 'OutOfGas' in str(e), 'should raise out of gas error'
Expand All @@ -79,7 +81,6 @@ def test_deployment(self):
tevm = tinyevm.TinyEVM()

contract_bytecode = open('tests/contracts/C.hex').read()
salt = None
owner = '0x388C818CA8B9251b393131C08a736A67ccB19297'
data = None
value = None
Expand All @@ -90,7 +91,7 @@ def test_deployment(self):
tprint('balance before deployment: {}'.format(balance))

# todo update response object
resp = tevm.deterministic_deploy(contract_bytecode, owner, data, value, init_value)
resp = tevm.deterministic_deploy(contract_bytecode, salt, owner, data, value, init_value)
tprint('Deployment resp: {}'.format(resp))

bugs = list(resp.bug_data)
Expand Down

0 comments on commit 59dd383

Please sign in to comment.