Skip to content
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

native bits #278

Merged
merged 46 commits into from
Dec 2, 2022
Merged
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
bf17327
wip
shekhirin Oct 23, 2022
3e3c9b5
fix comparisons with zero
shekhirin Oct 23, 2022
4f4ee35
new failing test in run()
shekhirin Oct 23, 2022
5a999b6
remove failing test testing
shekhirin Oct 23, 2022
5876d22
fix default BlockEnv values
shekhirin Oct 23, 2022
a86388b
splice -> copy_from_slice
shekhirin Oct 23, 2022
1611897
use more ruint algorithms
shekhirin Oct 24, 2022
e0fb329
revertme: use primitive_types for ADD/MUL/SUB which is better than ruint
shekhirin Oct 24, 2022
07956b2
Revert "revertme: use primitive_types for ADD/MUL/SUB which is better…
shekhirin Oct 24, 2022
3a2c2f1
4 * 8 -> 32 :kekw:
shekhirin Oct 24, 2022
c2fc13a
bump ruint
shekhirin Oct 24, 2022
86e7bdf
fix revm-js
shekhirin Oct 24, 2022
6df65f8
Use ruint 1.5.1.
recmo Oct 25, 2022
a107572
Better inlining
recmo Oct 25, 2022
5a72f4f
fix clippy
shekhirin Oct 25, 2022
391669e
fixes after remco's review
shekhirin Oct 26, 2022
037d94e
bump ruint, fix wasm example
shekhirin Oct 27, 2022
35c02bd
wip
shekhirin Oct 28, 2022
fd32ec2
replace exp implementation with pow from ruint
shekhirin Oct 28, 2022
46b92f2
revert B160 changes
shekhirin Oct 28, 2022
cc9756b
bump ruint
shekhirin Oct 29, 2022
a97ca40
Merge branch 'ruint' into ruint-bits
shekhirin Oct 29, 2022
b415c61
Merge remote-tracking branch 'upstream/main' into ruint-bits
shekhirin Oct 29, 2022
bc699ac
wip bits
shekhirin Oct 30, 2022
f304db3
Merge remote-tracking branch 'upstream/main' into ruint-bits
shekhirin Oct 30, 2022
724cd83
more fixes
shekhirin Nov 1, 2022
8b7440d
return primitive_types to revme
shekhirin Nov 1, 2022
2a37e07
more fixes
shekhirin Nov 1, 2022
346c3c0
use more ruint features
shekhirin Nov 1, 2022
3d8faf3
done?
shekhirin Nov 1, 2022
b57aeef
cleanup Cargo.toml
shekhirin Nov 1, 2022
a77db51
bits! -> uint! macro
shekhirin Nov 3, 2022
8dfc4c2
bump ruint
shekhirin Nov 3, 2022
fa4792b
bump ruint
shekhirin Nov 4, 2022
0e72595
import uint! macro
shekhirin Nov 4, 2022
137201b
fix clippy
shekhirin Nov 4, 2022
6f00eee
to_be_bytes_vec -> to_be_bytes
shekhirin Nov 4, 2022
dd82fa1
revm bits
rakita Nov 16, 2022
b9884a4
Progress on switching to revm bits
rakita Nov 20, 2022
e4ecab9
Flushing out bits
rakita Nov 26, 2022
febe333
Merge remote-tracking branch 'origin/main' into baseline
rakita Nov 26, 2022
9c87565
bits and nits. bugs squashed
rakita Nov 26, 2022
3628658
Merge remote-tracking branch 'origin/main' into baseline
rakita Nov 26, 2022
e32aafd
rm primitive_types from revm
rakita Nov 26, 2022
4da6f20
nits
rakita Dec 2, 2022
5a60bd6
Merge3855
rakita Dec 2, 2022
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
Prev Previous commit
Next Next commit
revm bits
rakita committed Nov 16, 2022
commit dd82fa160309df474ab808d0ac55c3579636f427
24 changes: 21 additions & 3 deletions Cargo.lock

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

3 changes: 1 addition & 2 deletions bins/revme/src/cli_env.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::str::FromStr;

use bytes::Bytes;
use revm::{Env, TransactTo};
use ruint::aliases::{B160, U256};
use revm::{Env, TransactTo,B160,U256};
use structopt::StructOpt;

#[derive(StructOpt, Clone, Debug)]
4 changes: 1 addition & 3 deletions bins/revme/src/statetest/merkle_trie.rs
Original file line number Diff line number Diff line change
@@ -2,10 +2,8 @@ use bytes::Bytes;
use hash_db::Hasher;
use plain_hasher::PlainHasher;
use primitive_types::{H160, H256};
use revm::common::keccak256;
use revm::{db::DbAccount, Log};
use revm::{db::DbAccount, Log ,U256,B160,B256,common::keccak256,};
use rlp::RlpStream;
use ruint::aliases::{B160, B256, U256};
use sha3::{Digest, Keccak256};
use triehash::sec_trie_root;

2 changes: 1 addition & 1 deletion bins/revme/src/statetest/models/deserializer.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use bytes::Bytes;
use ruint::aliases::{B160, U256};
use serde::{
de::{self, Error},
Deserialize,
};
use revm::{U256,B160};

pub fn deserialize_str_as_u64<'de, D>(deserializer: D) -> Result<u64, D::Error>
where
2 changes: 1 addition & 1 deletion bins/revme/src/statetest/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bytes::Bytes;
use ruint::aliases::{B160, B256, U256};
use revm::{B160, B256, U256};
use std::collections::{BTreeMap, HashMap};
mod deserializer;
mod spec;
3 changes: 1 addition & 2 deletions bins/revme/src/statetest/runner.rs
Original file line number Diff line number Diff line change
@@ -8,8 +8,7 @@ use std::{
};

use indicatif::ProgressBar;
use revm::{db::AccountState, Bytecode, CreateScheme, Env, ExecutionResult, SpecId, TransactTo};
use ruint::aliases::{B160, B256, U256};
use revm::{db::AccountState, U256,Bytecode, bits::{B160,B256},CreateScheme, Env, ExecutionResult, SpecId, TransactTo};
use std::sync::atomic::Ordering;
use walkdir::{DirEntry, WalkDir};

5 changes: 2 additions & 3 deletions bins/revme/src/statetest/trace.rs
Original file line number Diff line number Diff line change
@@ -3,9 +3,8 @@ pub use revm::Inspector;
use revm::{
opcode::{self},
CallInputs, CreateInputs, Database, EVMData, Gas, GasInspector, Return,
};
use ruint::aliases::B160;

bits::B160,
};
#[derive(Clone)]
pub struct CustomPrintTracer {
gas_inspector: GasInspector,
5 changes: 5 additions & 0 deletions crates/revm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -25,6 +25,11 @@ serde = { version = "1.0", features = ["derive","rc"], optional = true }
tokio = { version = "1.21", features = ["rt-multi-thread", "macros"], optional = true }
web3 = { git = "https://github.com/tomusdrw/rust-web3", version = "0.19", optional = true }

# bits B256 B160 crate
fixed-hash = { version = "0.8", default-features = false }
hex-literal = "0.3"
derive_more = "0.99"

# sha3 keccak hasher
sha3 = { version = "0.10", default-features = false, features = []}

18 changes: 18 additions & 0 deletions crates/revm/src/bits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use derive_more::{AsRef, Deref};
use fixed_hash::{construct_fixed_hash, impl_fixed_hash_conversions};

construct_fixed_hash! {
/// My 256 bit hash type.
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(AsRef,Deref)]
pub struct B256(32);
}

construct_fixed_hash! {
/// My 256 bit hash type.
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(AsRef,Deref)]
pub struct B160(20);
}

impl_fixed_hash_conversions!(B256, B160);
4 changes: 2 additions & 2 deletions crates/revm/src/common.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ruint::aliases::B256;
use crate::bits::B256;
use sha3::{Digest, Keccak256};

#[inline(always)]
pub fn keccak256(input: &[u8]) -> B256 {
B256::try_from_be_slice(Keccak256::digest(input).as_slice()).unwrap()
B256::from_slice(Keccak256::digest(input).as_slice())
}
11 changes: 5 additions & 6 deletions crates/revm/src/db.rs
Original file line number Diff line number Diff line change
@@ -5,14 +5,13 @@ pub mod web3db;
#[cfg(feature = "web3db")]
pub use web3db::Web3DB;

pub use in_memory_db::{AccountState, BenchmarkDB, CacheDB, DbAccount, EmptyDB, InMemoryDB};

use crate::{interpreter::bytecode::Bytecode, Account};
use hashbrown::HashMap as Map;
use ruint::aliases::{B160, B256, U256};

use crate::bits::{B160, B256};
use crate::AccountInfo;
use crate::U256;
use crate::{interpreter::bytecode::Bytecode, Account};
use auto_impl::auto_impl;
use hashbrown::HashMap as Map;
pub use in_memory_db::{AccountState, BenchmarkDB, CacheDB, DbAccount, EmptyDB, InMemoryDB};

#[auto_impl(& mut, Box)]
pub trait Database {
8 changes: 4 additions & 4 deletions crates/revm/src/db/in_memory_db.rs
Original file line number Diff line number Diff line change
@@ -2,10 +2,10 @@ use super::{DatabaseCommit, DatabaseRef};
use crate::common::keccak256;
use crate::{interpreter::bytecode::Bytecode, Database, KECCAK_EMPTY};
use crate::{Account, AccountInfo, Log};
use crate::{U256,B160,B256};
use alloc::vec::Vec;
use core::convert::Infallible;
use hashbrown::{hash_map::Entry, HashMap as Map};
use ruint::aliases::{B160, B256, U256};

pub type InMemoryDB = CacheDB<EmptyDB>;

@@ -95,7 +95,7 @@ impl<ExtDB: DatabaseRef> CacheDB<ExtDB> {
pub fn new(db: ExtDB) -> Self {
let mut contracts = Map::new();
contracts.insert(KECCAK_EMPTY, Bytecode::new());
contracts.insert(B256::ZERO, Bytecode::new());
contracts.insert(B256::zero(), Bytecode::new());
Self {
accounts: Map::new(),
contracts,
@@ -114,7 +114,7 @@ impl<ExtDB: DatabaseRef> CacheDB<ExtDB> {
.or_insert_with(|| code.clone());
}
}
if account.code_hash == B256::ZERO {
if account.code_hash == B256::zero() {
account.code_hash = KECCAK_EMPTY;
}
}
@@ -363,7 +363,7 @@ impl Database for BenchmarkDB {
type Error = Infallible;
/// Get basic account information.
fn basic(&mut self, address: B160) -> Result<Option<AccountInfo>, Self::Error> {
if address == B160::ZERO {
if address == B160::zero() {
return Ok(Some(AccountInfo {
nonce: 1,
balance: U256::from(10000000),
11 changes: 6 additions & 5 deletions crates/revm/src/db/web3db.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{interpreter::bytecode::Bytecode, AccountInfo, Database, KECCAK_EMPTY};
use crate::{
interpreter::bytecode::Bytecode, AccountInfo, Database, B160, B256, KECCAK_EMPTY, U256,
};
use bytes::Bytes;
use ruint::aliases::{B160, B256, U256};
use tokio::runtime::{Handle, Runtime};
use web3::{
transports::Http,
@@ -51,7 +52,7 @@ impl Database for Web3DB {
type Error = ();

fn basic(&mut self, address: B160) -> Result<Option<AccountInfo>, Self::Error> {
let add = wH160::from(address);
let add = wH160::from(address.0);
let f = async {
let nonce = self.web3.eth().transaction_count(add, self.block_number);
let balance = self.web3.eth().balance(add, self.block_number);
@@ -82,7 +83,7 @@ impl Database for Web3DB {
}

fn storage(&mut self, address: B160, index: U256) -> Result<U256, Self::Error> {
let add = wH160::from(address);
let add = wH160::from(address.0);
let index = wU256::from(index);
let f = async {
let storage = self
@@ -116,6 +117,6 @@ impl Database for Web3DB {
.ok()
.flatten()
};
Ok(self.block_on(f).unwrap().hash.unwrap().into())
Ok(B256(self.block_on(f).unwrap().hash.unwrap().0))
}
}
11 changes: 5 additions & 6 deletions crates/revm/src/evm_impl.rs
Original file line number Diff line number Diff line change
@@ -10,13 +10,14 @@ use crate::{
CreateInputs, CreateScheme, Env, ExecutionResult, Gas, Inspector, Log, Return, Spec,
SpecId::{self, *},
TransactOut, TransactTo, Transfer, KECCAK_EMPTY,
bits::{B160,B256},
U256,
};
use alloc::vec::Vec;
use bytes::Bytes;
use core::{cmp::min, marker::PhantomData};
use hashbrown::HashMap as Map;
use revm_precompiles::{Precompile, PrecompileOutput, Precompiles};
use ruint::aliases::{B160, B256, U160, U256};

pub struct EVMData<'a, DB: Database> {
pub env: &'a mut Env,
@@ -647,7 +648,7 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB,
} else {
Return::PrecompileError
};
self.data.journaled_state.checkpoint_revert(checkpoint); //TODO check if we are discarding or reverting
self.data.journaled_state.checkpoint_revert(checkpoint);
(ret, gas, Bytes::new())
}
}
@@ -765,7 +766,7 @@ impl<'a, GSPEC: Spec, DB: Database + 'a, const INSPECT: bool> Host
}
if acc.is_empty() {
// TODO check this for pre tangerine fork
return Some((B256::ZERO, is_cold));
return Some((B256::zero(), is_cold));
}

Some((acc.info.code_hash, is_cold))
@@ -834,9 +835,7 @@ pub fn create_address(caller: B160, nonce: u64) -> B160 {
stream.append(&caller);
stream.append(&nonce);
let out = keccak256(&stream.out());
U160::try_from_be_slice(&out.to_be_bytes_vec()[12..])
.unwrap()
.into()
B160::from_slice(&out.to_be_bytes_vec()[12..])
}

/// Returns the address for the `CREATE2` scheme: [`CreateScheme::Create2`]
3 changes: 1 addition & 2 deletions crates/revm/src/gas/calc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::constants::*;
use crate::{models::SelfDestructResult, Spec, SpecId::*};
use ruint::aliases::U256;
use crate::{models::SelfDestructResult, Spec, SpecId::*, U256};

#[allow(clippy::collapsible_else_if)]
pub fn sstore_refund<SPEC: Spec>(original: U256, current: U256, new: U256) -> i64 {
12 changes: 5 additions & 7 deletions crates/revm/src/inspector.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use bytes::Bytes;
use ruint::aliases::{B160, B256};

use crate::{
evm_impl::EVMData, opcode, spec_opcode_gas, CallInputs, CreateInputs, Database, Gas,
Interpreter, Return,
bits::{B160, B256},
evm_impl::EVMData,
opcode, spec_opcode_gas, CallInputs, CreateInputs, Database, Gas, Interpreter, Return,
};
use auto_impl::auto_impl;
use bytes::Bytes;

#[auto_impl(&mut, Box)]
pub trait Inspector<DB: Database> {
@@ -244,10 +243,9 @@ mod tests {
use crate::db::BenchmarkDB;
use crate::{
opcode, Bytecode, CallInputs, CreateInputs, Database, EVMData, Gas, GasInspector,
Inspector, Interpreter, OpCode, Return, TransactTo,
Inspector, Interpreter, OpCode, Return, TransactTo,B160, B256,
};
use bytes::Bytes;
use ruint::aliases::{B160, B256};

#[derive(Default, Debug)]
struct StackInspector {
3 changes: 1 addition & 2 deletions crates/revm/src/instructions.rs
Original file line number Diff line number Diff line change
@@ -13,9 +13,8 @@ mod system;

pub use opcode::{OpCode, OPCODE_JUMPMAP};

use crate::{interpreter::Interpreter, CallScheme, Host, Spec, SpecId::*};
use crate::{interpreter::Interpreter, CallScheme, Host, U256,Spec, SpecId::*};
use core::ops::{BitAnd, BitOr, BitXor};
use ruint::aliases::U256;

#[macro_export]
macro_rules! return_ok {
4 changes: 1 addition & 3 deletions crates/revm/src/instructions/arithmetic.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::{gas, Interpreter, Return, Spec};

use crate::{gas, Interpreter, Return, Spec,U256};
use super::i256::{i256_div, i256_mod};
use ruint::aliases::U256;

pub fn div(op1: U256, op2: U256) -> U256 {
op1.checked_div(op2).unwrap_or_default()
3 changes: 1 addition & 2 deletions crates/revm/src/instructions/bitwise.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use core::cmp::Ordering;

use super::i256::{i256_cmp, i256_sign, two_compl, Sign};
use ruint::aliases::U256;
use crate::U256;

pub fn slt(op1: U256, op2: U256) -> U256 {
if i256_cmp(op1, op2) == Ordering::Less {
3 changes: 1 addition & 2 deletions crates/revm/src/instructions/control.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::{gas, interpreter::Interpreter, Return, Spec, SpecId::*};
use ruint::aliases::U256;
use crate::{gas, U256, interpreter::Interpreter, Return, Spec, SpecId::*};

pub fn jump(interp: &mut Interpreter) -> Return {
// gas!(interp, gas::MID);
Loading