Skip to content

Commit

Permalink
chore: includes to libs (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
rakita authored Jan 23, 2023
1 parent 6bdc8b9 commit 81534ad
Show file tree
Hide file tree
Showing 30 changed files with 178 additions and 183 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

5 changes: 2 additions & 3 deletions bins/revm-test/src/bin/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ use std::time::Instant;

use bytes::Bytes;
use revm::{
analysis::to_analysed,
db::BenchmarkDB,
primitives::{Bytecode, LondonSpec},
TransactTo,
interpreter::analysis::to_analysed,
primitives::{Bytecode, LondonSpec, TransactTo},
};
extern crate alloc;

Expand Down
5 changes: 2 additions & 3 deletions bins/revm-test/src/bin/snailtracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ use std::time::Duration;

use bytes::Bytes;
use revm::{
analysis::to_analysed,
db::BenchmarkDB,
primitives::{BerlinSpec, Bytecode},
BytecodeLocked, DummyHost, TransactTo,
interpreter::{analysis::to_analysed, BytecodeLocked, DummyHost},
primitives::{BerlinSpec, Bytecode, TransactTo},
};
extern crate alloc;

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

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

#[derive(StructOpt, Clone, Debug)]
Expand Down
6 changes: 4 additions & 2 deletions bins/revme/src/statetest/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ use std::{

use indicatif::ProgressBar;
use revm::{
db::AccountState, inspectors::CustomPrintTracer, Bytecode, CreateScheme, Env, ExecutionResult,
SpecId, TransactTo, B160, B256, U256,
db::AccountState,
inspectors::CustomPrintTracer,
interpreter::CreateScheme,
primitives::{Bytecode, Env, ExecutionResult, SpecId, TransactTo, B160, B256, U256},
};
use std::sync::atomic::Ordering;
use walkdir::{DirEntry, WalkDir};
Expand Down
86 changes: 86 additions & 0 deletions crates/interpreter/src/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,89 @@ mod constants;

pub use calc::*;
pub use constants::*;

#[derive(Clone, Copy, Debug)]
pub struct Gas {
/// Gas Limit
limit: u64,
/// used+memory gas.
all_used_gas: u64,
/// Used gas without memory
used: u64,
/// Used gas for memory expansion
memory: u64,
/// Refunded gas. This gas is used only at the end of execution.
refunded: i64,
}
impl Gas {
pub fn new(limit: u64) -> Self {
Self {
limit,
used: 0,
memory: 0,
refunded: 0,
all_used_gas: 0,
}
}

pub fn limit(&self) -> u64 {
self.limit
}

pub fn memory(&self) -> u64 {
self.memory
}

pub fn refunded(&self) -> i64 {
self.refunded
}

pub fn spend(&self) -> u64 {
self.all_used_gas
}

pub fn remaining(&self) -> u64 {
self.limit - self.all_used_gas
}

pub fn erase_cost(&mut self, returned: u64) {
self.used -= returned;
self.all_used_gas -= returned;
}

pub fn record_refund(&mut self, refund: i64) {
self.refunded += refund;
}

/// Record an explicit cost.
#[inline(always)]
pub fn record_cost(&mut self, cost: u64) -> bool {
let (all_used_gas, overflow) = self.all_used_gas.overflowing_add(cost);
if overflow || self.limit < all_used_gas {
return false;
}

self.used += cost;
self.all_used_gas = all_used_gas;
true
}

/// used in memory_resize! macro to record gas used for memory expansion.
pub fn record_memory(&mut self, gas_memory: u64) -> bool {
if gas_memory > self.memory {
let (all_used_gas, overflow) = self.used.overflowing_add(gas_memory);
if overflow || self.limit < all_used_gas {
return false;
}
self.memory = gas_memory;
self.all_used_gas = all_used_gas;
}
true
}

/// used in gas_refund! macro to record refund value.
/// Refund can be negative but self.refunded is always positive.
pub fn gas_refund(&mut self, refund: i64) {
self.refunded += refund;
}
}
6 changes: 5 additions & 1 deletion crates/interpreter/src/gas/calc.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use super::constants::*;
use crate::{inner_models::SelfDestructResult, primitives::Spec, primitives::SpecId::*, U256};
use crate::{
inner_models::SelfDestructResult,
primitives::Spec,
primitives::{SpecId::*, U256},
};

#[allow(clippy::collapsible_else_if)]
pub fn sstore_refund<SPEC: Spec>(original: U256, current: U256, new: U256) -> i64 {
Expand Down
4 changes: 2 additions & 2 deletions crates/interpreter/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ mod dummy_host;

use crate::primitives::Bytecode;
use crate::{
primitives::{Bytes, Env, Gas, B160, B256, U256},
CallInputs, CreateInputs, InstructionResult, Interpreter, SelfDestructResult,
primitives::{Bytes, Env, B160, B256, U256},
CallInputs, CreateInputs, Gas, InstructionResult, Interpreter, SelfDestructResult,
};
pub use dummy_host::DummyHost;

Expand Down
4 changes: 2 additions & 2 deletions crates/interpreter/src/host/dummy_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use ruint::aliases::U256;

use crate::primitives::Bytecode;
use crate::{
primitives::{Env, Gas, Log, B160, B256, KECCAK_EMPTY},
CallInputs, CreateInputs, Host, InstructionResult, Interpreter, SelfDestructResult,
primitives::{Env, Log, B160, B256, KECCAK_EMPTY},
CallInputs, CreateInputs, Gas, Host, InstructionResult, Interpreter, SelfDestructResult,
};

pub struct DummyHost {
Expand Down
6 changes: 5 additions & 1 deletion crates/interpreter/src/instructions/arithmetic.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use super::i256::{i256_div, i256_mod};
use crate::{gas, primitives::Spec, Host, InstructionResult, Interpreter, U256};
use crate::{
gas,
primitives::{Spec, U256},
Host, InstructionResult, Interpreter,
};

pub fn wrapped_add(interpreter: &mut Interpreter, _host: &mut dyn Host) {
pop_top!(interpreter, op1, op2);
Expand Down
5 changes: 3 additions & 2 deletions crates/interpreter/src/instructions/bitwise.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use super::i256::{i256_cmp, i256_sign, two_compl, Sign};
use crate::{
primitives::Spec, primitives::SpecId::CONSTANTINOPLE, Host, InstructionResult, Interpreter,
U256,
primitives::SpecId::CONSTANTINOPLE,
primitives::{Spec, U256},
Host, InstructionResult, Interpreter,
};
use core::cmp::Ordering;
use core::ops::{BitAnd, BitOr, BitXor};
Expand Down
4 changes: 2 additions & 2 deletions crates/interpreter/src/instructions/control.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
gas, interpreter::Interpreter, primitives::Spec, primitives::SpecId::*, Host,
InstructionResult, U256,
gas, interpreter::Interpreter, primitives::Spec, primitives::SpecId::*, primitives::U256, Host,
InstructionResult,
};

pub fn jump(interpreter: &mut Interpreter, _host: &mut dyn Host) {
Expand Down
4 changes: 2 additions & 2 deletions crates/interpreter/src/instructions/i256.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::U256;
use crate::primitives::U256;
use core::cmp::Ordering;
use ruint::uint;

Expand Down Expand Up @@ -135,7 +135,7 @@ pub fn i256_mod(mut first: U256, mut second: U256) -> U256 {
#[cfg(test)]
mod tests {
use super::*;
use crate::U256;
use crate::primitives::U256;
use core::num::Wrapping;

#[test]
Expand Down
2 changes: 1 addition & 1 deletion crates/interpreter/src/instructions/memory.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{interpreter::Interpreter, Host, InstructionResult, U256};
use crate::{interpreter::Interpreter, primitives::U256, Host, InstructionResult};

pub fn mload(interpreter: &mut Interpreter, _host: &mut dyn Host) {
// gas!(interp, gas::VERYLOW);
Expand Down
4 changes: 2 additions & 2 deletions crates/interpreter/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ pub use contract::Contract;
pub use memory::Memory;
pub use stack::Stack;

use crate::primitives::{Gas, Spec};
use crate::primitives::Spec;
use crate::{
instructions::{eval, InstructionResult},
Host, USE_GAS,
Gas, Host, USE_GAS,
};
use bytes::Bytes;
use core::ops::Range;
Expand Down
2 changes: 1 addition & 1 deletion crates/interpreter/src/interpreter/memory.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{alloc::vec::Vec, U256};
use crate::{alloc::vec::Vec, primitives::U256};
use core::{
cmp::min,
ops::{BitAnd, Not},
Expand Down
6 changes: 1 addition & 5 deletions crates/interpreter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,12 @@ extern crate alloc;
pub(crate) const USE_GAS: bool = !cfg!(feature = "no_gas_measuring");

// Reexport primary types.
pub use gas::Gas;
pub use host::{DummyHost, Host};
pub use inner_models::*;
pub use instruction_result::InstructionResult;
pub use instructions::opcode::{self, spec_opcode_gas, OpCode, OPCODE_JUMPMAP};
pub use interpreter::*;
pub use interpreter::{BytecodeLocked, Contract, Interpreter, Memory, Stack};
pub use ruint;
pub use ruint::aliases::U256;

pub use hashbrown::hash_map;
pub use hashbrown::HashMap;

pub use revm_primitives as primitives;
1 change: 1 addition & 0 deletions crates/precompiles/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ repository = "https://github.com/bluealloy/revm"
version = "1.1.2"

[dependencies]
revm-primitives = { path = "../primitives", default-features = false }
bn = { package = "substrate-bn", version = "0.6", default-features = false }
bytes = { version = "1.1", default-features = false }
hashbrown = { version = "0.13" }
Expand Down
22 changes: 7 additions & 15 deletions crates/precompiles/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
#![no_std]

use bytes::Bytes;
use once_cell::sync::OnceCell;

pub type B160 = [u8; 20];
pub type B256 = [u8; 32];

mod blake2;
mod bn128;
mod error;
mod hash;
mod identity;
mod modexp;
mod secp256k1;

pub use error::Error;
use bytes::Bytes;
use once_cell::sync::OnceCell;
pub use revm_primitives::precompile::{PrecompileError as Error, *};

pub type B160 = [u8; 20];
pub type B256 = [u8; 32];

/// libraries for no_std flag
#[macro_use]
Expand Down Expand Up @@ -52,15 +50,9 @@ impl PrecompileOutput {
}
}

/// A precompile operation result.
pub type PrecompileResult = Result<(u64, Vec<u8>), Error>;

pub type StandardPrecompileFn = fn(&[u8], u64) -> PrecompileResult;
pub type CustomPrecompileFn = fn(&[u8], u64) -> PrecompileResult;

#[derive(Clone, Debug)]
pub struct Precompiles {
fun: HashMap<B160, Precompile>,
pub fun: HashMap<B160, Precompile>,
}

impl Default for Precompiles {
Expand Down
Loading

0 comments on commit 81534ad

Please sign in to comment.