Skip to content

Commit dec5e97

Browse files
committed
Feat: Instruction table. Plain and Boxed fn
1 parent 711c273 commit dec5e97

File tree

28 files changed

+617
-360
lines changed

28 files changed

+617
-360
lines changed

Cargo.lock

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bins/revm-test/Cargo.toml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[package]
2+
name = "revm-test"
3+
version = "0.1.0"
4+
edition = "2021"
5+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
6+
7+
[dependencies]
8+
bytes = "1.4"
9+
hex = "0.4"
10+
revm = { path = "../../crates/revm", version = "3.3.0",default-features=false }
11+
microbench = "0.5"
12+
13+
[[bin]]
14+
name = "analysis"
15+
16+
[[bin]]
17+
name = "snailtracer"
18+
19+
[[bin]]
20+
name = "transfer"

bins/revm-test/src/bin/analysis.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use std::time::Instant;
2+
3+
use revm::{
4+
db::BenchmarkDB,
5+
interpreter::analysis::to_analysed,
6+
primitives::{Bytecode, Bytes, TransactTo},
7+
};
8+
extern crate alloc;
9+
10+
fn main() {
11+
let contract_data : Bytes = hex::decode( "6060604052341561000f57600080fd5b604051610dd1380380610dd18339810160405280805190602001909190805182019190602001805190602001909190805182019190505083600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508360008190555082600390805190602001906100a79291906100e3565b5081600460006101000a81548160ff021916908360ff16021790555080600590805190602001906100d99291906100e3565b5050505050610188565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061012457805160ff1916838001178555610152565b82800160010185558215610152579182015b82811115610151578251825591602001919060010190610136565b5b50905061015f9190610163565b5090565b61018591905b80821115610181576000816000905550600101610169565b5090565b90565b610c3a806101976000396000f3006060604052600436106100af576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806306fdde03146100b4578063095ea7b31461014257806318160ddd1461019c57806323b872dd146101c557806327e235e31461023e578063313ce5671461028b5780635c658165146102ba57806370a082311461032657806395d89b4114610373578063a9059cbb14610401578063dd62ed3e1461045b575b600080fd5b34156100bf57600080fd5b6100c76104c7565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156101075780820151818401526020810190506100ec565b50505050905090810190601f1680156101345780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561014d57600080fd5b610182600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610565565b604051808215151515815260200191505060405180910390f35b34156101a757600080fd5b6101af610657565b6040518082815260200191505060405180910390f35b34156101d057600080fd5b610224600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803590602001909190505061065d565b604051808215151515815260200191505060405180910390f35b341561024957600080fd5b610275600480803573ffffffffffffffffffffffffffffffffffffffff169060200190919050506108f7565b6040518082815260200191505060405180910390f35b341561029657600080fd5b61029e61090f565b604051808260ff1660ff16815260200191505060405180910390f35b34156102c557600080fd5b610310600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050610922565b6040518082815260200191505060405180910390f35b341561033157600080fd5b61035d600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050610947565b6040518082815260200191505060405180910390f35b341561037e57600080fd5b610386610990565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156103c65780820151818401526020810190506103ab565b50505050905090810190601f1680156103f35780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561040c57600080fd5b610441600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610a2e565b604051808215151515815260200191505060405180910390f35b341561046657600080fd5b6104b1600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050610b87565b6040518082815260200191505060405180910390f35b60038054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561055d5780601f106105325761010080835404028352916020019161055d565b820191906000526020600020905b81548152906001019060200180831161054057829003601f168201915b505050505081565b600081600260003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925846040518082815260200191505060405180910390a36001905092915050565b60005481565b600080600260008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054905082600160008773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020541015801561072e5750828110155b151561073957600080fd5b82600160008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254019250508190555082600160008773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825403925050819055507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8110156108865782600260008773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825403925050819055505b8373ffffffffffffffffffffffffffffffffffffffff168573ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef856040518082815260200191505060405180910390a360019150509392505050565b60016020528060005260406000206000915090505481565b600460009054906101000a900460ff1681565b6002602052816000526040600020602052806000526040600020600091509150505481565b6000600160008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050919050565b60058054600181600116156101000203166002900480601f016020809104026020016040519081016040528092919081815260200182805460018160011615610100020316600290048015610a265780601f106109fb57610100808354040283529160200191610a26565b820191906000526020600020905b815481529060010190602001808311610a0957829003601f168201915b505050505081565b600081600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205410151515610a7e57600080fd5b81600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555081600160008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040518082815260200191505060405180910390a36001905092915050565b6000600260008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050929150505600a165627a7a72305820df254047bc8f2904ad3e966b6db116d703bebd40efadadb5e738c836ffc8f58a0029" ).unwrap().into();
12+
13+
// BenchmarkDB is dummy state that implements Database trait.
14+
let mut evm = revm::new();
15+
16+
// execution globals block hash/gas_limit/coinbase/timestamp..
17+
evm.env.tx.caller = "0x1000000000000000000000000000000000000000"
18+
.parse()
19+
.unwrap();
20+
evm.env.tx.transact_to = TransactTo::Call(
21+
"0x0000000000000000000000000000000000000000"
22+
.parse()
23+
.unwrap(),
24+
);
25+
//evm.env.tx.data = Bytes::from(hex::decode("30627b7c").unwrap());
26+
evm.env.tx.data = Bytes::from(hex::decode("8035F0CE").unwrap());
27+
28+
let bytecode_raw = Bytecode::new_raw(contract_data.clone());
29+
let bytecode_checked = Bytecode::new_raw(contract_data.clone()).to_checked();
30+
let bytecode_analysed = to_analysed(Bytecode::new_raw(contract_data));
31+
32+
evm.database(BenchmarkDB::new_bytecode(bytecode_raw));
33+
34+
// just to spead up processor.
35+
for _ in 0..10000 {
36+
let _ = evm.transact().unwrap();
37+
}
38+
39+
let timer = Instant::now();
40+
for _ in 0..30000 {
41+
let _ = evm.transact().unwrap();
42+
}
43+
println!("Raw elapsed time: {:?}", timer.elapsed());
44+
45+
evm.database(BenchmarkDB::new_bytecode(bytecode_checked));
46+
47+
let timer = Instant::now();
48+
for _ in 0..30000 {
49+
let _ = evm.transact().unwrap();
50+
}
51+
println!("Checked elapsed time: {:?}", timer.elapsed());
52+
53+
evm.database(BenchmarkDB::new_bytecode(bytecode_analysed));
54+
55+
let timer = Instant::now();
56+
for _ in 0..30000 {
57+
let _ = evm.transact().unwrap();
58+
}
59+
println!("Analysed elapsed time: {:?}", timer.elapsed());
60+
}

bins/revm-test/src/bin/snailtracer.rs

Lines changed: 59 additions & 0 deletions
Large diffs are not rendered by default.

bins/revm-test/src/bin/transfer.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use revm::{
2+
db::BenchmarkDB,
3+
primitives::{Bytecode, TransactTo, U256},
4+
};
5+
use std::time::{Duration, Instant};
6+
extern crate alloc;
7+
8+
fn main() {
9+
// BenchmarkDB is dummy state that implements Database trait.
10+
let mut evm = revm::new();
11+
12+
// execution globals block hash/gas_limit/coinbase/timestamp..
13+
evm.env.tx.caller = "0x0000000000000000000000000000000000000001"
14+
.parse()
15+
.unwrap();
16+
evm.env.tx.value = U256::from(10);
17+
evm.env.tx.transact_to = TransactTo::Call(
18+
"0x0000000000000000000000000000000000000000"
19+
.parse()
20+
.unwrap(),
21+
);
22+
//evm.env.tx.data = Bytes::from(hex::decode("30627b7c").unwrap());
23+
24+
evm.database(BenchmarkDB::new_bytecode(Bytecode::new()));
25+
26+
// Microbenchmark
27+
let bench_options = microbench::Options::default().time(Duration::from_secs(1));
28+
29+
microbench::bench(&bench_options, "Simple value transfer", || {
30+
let _ = evm.transact().unwrap();
31+
});
32+
33+
let time = Instant::now();
34+
for _ in 0..10000 {
35+
let _ = evm.transact().unwrap();
36+
}
37+
let elapsed = time.elapsed();
38+
println!("10k runs in {:?}", elapsed.as_nanos() / 10_000);
39+
}

crates/interpreter/src/host.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::primitives::Bytecode;
22
use crate::{
33
primitives::{Address, Bytes, Env, B256, U256},
4-
CallInputs, CreateInputs, Gas, InstructionResult, Interpreter, SelfDestructResult,
4+
CallInputs, CreateInputs, Gas, InstructionResult, SelfDestructResult,
55
};
66
use alloc::vec::Vec;
77
pub use dummy::DummyHost;
@@ -10,16 +10,6 @@ mod dummy;
1010

1111
/// EVM context host.
1212
pub trait Host {
13-
/// Called before the interpreter executes an instruction.
14-
fn step(&mut self, interpreter: &mut Interpreter) -> InstructionResult;
15-
16-
/// Called after the interpreter executes an instruction.
17-
fn step_end(
18-
&mut self,
19-
interpreter: &mut Interpreter,
20-
ret: InstructionResult,
21-
) -> InstructionResult;
22-
2313
/// Returns a mutable reference to the environment.
2414
fn env(&mut self) -> &mut Env;
2515

crates/interpreter/src/host/dummy.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::primitives::{hash_map::Entry, Bytecode, Bytes, HashMap, U256};
22
use crate::{
33
primitives::{Address, Env, Log, B256, KECCAK_EMPTY},
4-
CallInputs, CreateInputs, Gas, Host, InstructionResult, Interpreter, SelfDestructResult,
4+
CallInputs, CreateInputs, Gas, Host, InstructionResult, SelfDestructResult,
55
};
66
use alloc::vec::Vec;
77

@@ -34,20 +34,6 @@ impl DummyHost {
3434
}
3535

3636
impl Host for DummyHost {
37-
#[inline]
38-
fn step(&mut self, _interp: &mut Interpreter) -> InstructionResult {
39-
InstructionResult::Continue
40-
}
41-
42-
#[inline]
43-
fn step_end(
44-
&mut self,
45-
_interp: &mut Interpreter,
46-
_ret: InstructionResult,
47-
) -> InstructionResult {
48-
InstructionResult::Continue
49-
}
50-
5137
#[inline]
5238
fn env(&mut self) -> &mut Env {
5339
&mut self.env

crates/interpreter/src/instructions/arithmetic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub fn mulmod<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
6565
*op3 = op1.mul_mod(op2, *op3)
6666
}
6767

68-
pub fn exp<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut H) {
68+
pub fn exp<SPEC: Spec, H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
6969
pop_top!(interpreter, op1, op2);
7070
gas_or_fail!(interpreter, gas::exp_cost::<SPEC>(*op2));
7171
*op2 = op1.pow(*op2);

crates/interpreter/src/instructions/bitwise.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,23 +80,23 @@ pub fn byte<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
8080
}
8181

8282
/// EIP-145: Bitwise shifting instructions in EVM
83-
pub fn shl<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut H) {
83+
pub fn shl<SPEC: Spec, H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
8484
check!(interpreter, CONSTANTINOPLE);
8585
gas!(interpreter, gas::VERYLOW);
8686
pop_top!(interpreter, op1, op2);
8787
*op2 <<= as_usize_saturated!(op1);
8888
}
8989

9090
/// EIP-145: Bitwise shifting instructions in EVM
91-
pub fn shr<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut H) {
91+
pub fn shr<SPEC: Spec, H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
9292
check!(interpreter, CONSTANTINOPLE);
9393
gas!(interpreter, gas::VERYLOW);
9494
pop_top!(interpreter, op1, op2);
9595
*op2 >>= as_usize_saturated!(op1);
9696
}
9797

9898
/// EIP-145: Bitwise shifting instructions in EVM
99-
pub fn sar<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut H) {
99+
pub fn sar<SPEC: Spec, H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
100100
check!(interpreter, CONSTANTINOPLE);
101101
gas!(interpreter, gas::VERYLOW);
102102
pop_top!(interpreter, op1, op2);

crates/interpreter/src/instructions/control.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub fn ret<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
6565
}
6666

6767
/// EIP-140: REVERT instruction
68-
pub fn revert<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut H) {
68+
pub fn revert<SPEC: Spec, H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
6969
check!(interpreter, BYZANTIUM);
7070
return_inner(interpreter, InstructionResult::Revert)
7171
}

0 commit comments

Comments
 (0)