Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Manual port of #10983
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdplm committed Sep 11, 2019
1 parent cfae6b9 commit 453b6a0
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 11 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.

1 change: 1 addition & 0 deletions ethcore/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ memory-cache = { path = "../../util/memory-cache" }
[dev-dependencies]
rustc-hex = "1.0"
criterion = "0.2"
hex-literal = "0.2.0"

[features]
evm-debug = []
Expand Down
2 changes: 1 addition & 1 deletion ethcore/evm/src/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl Factory {
/// for caching jump destinations.
pub fn new(evm: VMType, cache_size: usize) -> Self {
Factory {
evm: evm,
evm,
evm_cache: Arc::new(SharedCache::new(cache_size)),
}
}
Expand Down
10 changes: 4 additions & 6 deletions ethcore/evm/src/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ enum_with_from_u8! {
DIFFICULTY = 0x44,
#[doc = "get the block's gas limit"]
GASLIMIT = 0x45,
#[doc = "get chain ID"]
CHAINID = 0x46,

#[doc = "remove item from stack"]
POP = 0x50,
Expand Down Expand Up @@ -442,12 +444,7 @@ pub struct InstructionInfo {
impl InstructionInfo {
/// Create new instruction info.
pub fn new(name: &'static str, args: usize, ret: usize, tier: GasPriceTier) -> Self {
InstructionInfo {
name: name,
args: args,
ret: ret,
tier: tier
}
InstructionInfo { name, args, ret, tier }
}
}

Expand Down Expand Up @@ -504,6 +501,7 @@ lazy_static! {
arr[NUMBER as usize] = Some(InstructionInfo::new("NUMBER", 0, 1, GasPriceTier::Base));
arr[DIFFICULTY as usize] = Some(InstructionInfo::new("DIFFICULTY", 0, 1, GasPriceTier::Base));
arr[GASLIMIT as usize] = Some(InstructionInfo::new("GASLIMIT", 0, 1, GasPriceTier::Base));
arr[CHAINID as usize] = Some(InstructionInfo::new("CHAINID", 0, 1, GasPriceTier::Base));
arr[POP as usize] = Some(InstructionInfo::new("POP", 1, 0, GasPriceTier::Base));
arr[MLOAD as usize] = Some(InstructionInfo::new("MLOAD", 1, 1, GasPriceTier::VeryLow));
arr[MSTORE as usize] = Some(InstructionInfo::new("MSTORE", 2, 0, GasPriceTier::VeryLow));
Expand Down
6 changes: 5 additions & 1 deletion ethcore/evm/src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,8 @@ impl<Cost: CostType> Interpreter<Cost> {
((instruction == instructions::RETURNDATACOPY || instruction == instructions::RETURNDATASIZE) && !schedule.have_return_data) ||
(instruction == instructions::REVERT && !schedule.have_revert) ||
((instruction == instructions::SHL || instruction == instructions::SHR || instruction == instructions::SAR) && !schedule.have_bitwise_shifting) ||
(instruction == instructions::EXTCODEHASH && !schedule.have_extcodehash)
(instruction == instructions::EXTCODEHASH && !schedule.have_extcodehash) ||
(instruction == instructions::CHAINID && !schedule.have_chain_id)
{
return Err(vm::Error::BadInstruction {
instruction: instruction as u8
Expand Down Expand Up @@ -850,6 +851,9 @@ impl<Cost: CostType> Interpreter<Cost> {
instructions::GASLIMIT => {
self.stack.push(ext.env_info().gas_limit.clone());
},
instructions::CHAINID => {
self.stack.push(ext.chain_id().into())
},

// Stack instructions

Expand Down
2 changes: 2 additions & 0 deletions ethcore/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ extern crate log;

#[cfg(test)]
extern crate rustc_hex;
#[cfg(test)]
extern crate hex_literal;

pub mod evm;
pub mod interpreter;
Expand Down
22 changes: 22 additions & 0 deletions ethcore/evm/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use vm::{self, ActionParams, ActionValue, Ext};
use vm::tests::{FakeExt, FakeCall, FakeCallType, test_finalize};
use factory::Factory;
use vmtype::VMType;
use hex_literal::hex;

evm_test!{test_add: test_add_int}
fn test_add(factory: super::Factory) {
Expand Down Expand Up @@ -130,6 +131,27 @@ fn test_sender(factory: super::Factory) {
assert_store(&ext, 0, "000000000000000000000000cd1722f2947def4cf144679da39c4c32bdc35681");
}

evm_test!{test_chain_id: test_chain_id_int}
fn test_chain_id(factory: super::Factory) {
// 46 CHAINID
// 60 00 PUSH 0
// 55 SSTORE
let code = hex!("46 60 00 55").to_vec();

let mut params = ActionParams::default();
params.gas = U256::from(100_000);
params.code = Some(Arc::new(code));
let mut ext = FakeExt::new_istanbul().with_chain_id(9);

let gas_left = {
let vm = factory.create(params, ext.schedule(), ext.depth());
test_finalize(vm.exec(&mut ext).ok().unwrap()).unwrap()
};

assert_eq!(gas_left, U256::from(79_995));
assert_store(&ext, 0, "0000000000000000000000000000000000000000000000000000000000000009");
}

evm_test!{test_extcodecopy: test_extcodecopy_int}
fn test_extcodecopy(factory: super::Factory) {
// 33 - sender
Expand Down
4 changes: 4 additions & 0 deletions ethcore/src/externalities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,10 @@ impl<'a, T: 'a, V: 'a, B: 'a> Ext for Externalities<'a, T, V, B>
self.env_info
}

fn chain_id(&self) -> u64 {
self.machine.params().chain_id
}

fn depth(&self) -> usize {
self.depth
}
Expand Down
2 changes: 2 additions & 0 deletions ethcore/src/json_tests/executive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ impl<'a, T: 'a, V: 'a, B: 'a> Ext for TestExt<'a, T, V, B>
self.ext.env_info()
}

fn chain_id(&self) -> u64 { 0 }

fn depth(&self) -> usize {
0
}
Expand Down
7 changes: 7 additions & 0 deletions ethcore/src/spec/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ pub struct CommonParams {
pub eip1283_disable_transition: BlockNumber,
/// Number of first block where EIP-1014 rules begin.
pub eip1014_transition: BlockNumber,
/// Number of first block where EIP-1344 rules begin: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1344.md
pub eip1344_transition: BlockNumber,
/// Number of first block where EIP-2028 rules begin.
pub eip2028_transition: BlockNumber,
/// Number of first block where dust cleanup rules (EIP-168 and EIP169) begin.
Expand Down Expand Up @@ -191,6 +193,7 @@ impl CommonParams {
schedule.have_return_data = block_number >= self.eip211_transition;
schedule.have_bitwise_shifting = block_number >= self.eip145_transition;
schedule.have_extcodehash = block_number >= self.eip1052_transition;
schedule.have_chain_id = block_number >= self.eip1344_transition;
schedule.eip1283 = block_number >= self.eip1283_transition && !(block_number >= self.eip1283_disable_transition);
if block_number >= self.eip2028_transition {
schedule.tx_data_non_zero_gas = 16;
Expand Down Expand Up @@ -313,6 +316,10 @@ impl From<ethjson::spec::Params> for CommonParams {
BlockNumber::max_value,
Into::into,
),
eip1344_transition: p.eip1344_transition.map_or_else(
BlockNumber::max_value,
Into::into,
),
eip2028_transition: p.eip2028_transition.map_or_else(
BlockNumber::max_value,
Into::into,
Expand Down
2 changes: 1 addition & 1 deletion ethcore/vm/src/env_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl From<ethjson::vm::Env> for EnvInfo {
fn from(e: ethjson::vm::Env) -> Self {
let number = e.number.into();
EnvInfo {
number: number,
number,
author: e.author.into(),
difficulty: e.difficulty.into(),
gas_limit: e.gas_limit.into(),
Expand Down
3 changes: 3 additions & 0 deletions ethcore/vm/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ pub trait Ext {
/// Returns environment info.
fn env_info(&self) -> &EnvInfo;

/// Returns the chain ID of the blockchain
fn chain_id(&self) -> u64;

/// Returns current depth of execution.
///
/// If contract A calls contract B, and contract B calls C,
Expand Down
5 changes: 5 additions & 0 deletions ethcore/vm/src/schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ pub struct Schedule {
pub have_return_data: bool,
/// SHL, SHR, SAR opcodes enabled.
pub have_bitwise_shifting: bool,
/// CHAINID opcode enabled.
pub have_chain_id: bool,
/// Kill basic accounts below this balance if touched.
pub kill_dust: CleanDustMode,
/// Enable EIP-1283 rules
Expand Down Expand Up @@ -209,6 +211,7 @@ impl Schedule {
have_revert: false,
have_return_data: false,
have_bitwise_shifting: false,
have_chain_id: false,
have_extcodehash: false,
stack_limit: 1024,
max_depth: 1024,
Expand Down Expand Up @@ -278,6 +281,7 @@ impl Schedule {
/// Schedule for the Istanbul fork of the Ethereum main net.
pub fn new_istanbul() -> Schedule {
let mut schedule = Self::new_constantinople();
schedule.have_chain_id = true;
schedule.tx_data_non_zero_gas = 16;
schedule
}
Expand All @@ -290,6 +294,7 @@ impl Schedule {
have_revert: false,
have_return_data: false,
have_bitwise_shifting: false,
have_chain_id: false,
have_extcodehash: false,
stack_limit: 1024,
max_depth: 1024,
Expand Down
16 changes: 14 additions & 2 deletions ethcore/vm/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ pub struct FakeExt {
pub balances: HashMap<Address, U256>,
pub tracing: bool,
pub is_static: bool,

chain_id: u64,
}

// similar to the normal `finalize` function, but ignoring NeedsReturn.
Expand Down Expand Up @@ -98,7 +100,7 @@ impl FakeExt {
ext
}

/// New fake externalities with constantinople schedule rules
/// New fake externalities with Istanbul schedule rules
pub fn new_istanbul() -> Self {
let mut ext = FakeExt::default();
ext.schedule = Schedule::new_istanbul();
Expand All @@ -110,6 +112,12 @@ impl FakeExt {
self.schedule.wasm = Some(Default::default());
self
}

/// Set chain ID
pub fn with_chain_id(mut self, chain_id: u64) -> Self {
self.chain_id = chain_id;
self
}
}

impl Ext for FakeExt {
Expand Down Expand Up @@ -207,7 +215,7 @@ impl Ext for FakeExt {

fn log(&mut self, topics: Vec<H256>, data: &[u8]) -> Result<()> {
self.logs.push(FakeLogEntry {
topics: topics,
topics,
data: data.to_vec()
});
Ok(())
Expand All @@ -230,6 +238,10 @@ impl Ext for FakeExt {
&self.info
}

fn chain_id(&self) -> u64 {
self.chain_id
}

fn depth(&self) -> usize {
self.depth
}
Expand Down
2 changes: 2 additions & 0 deletions json/src/spec/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ pub struct Params {
/// See `CommonParams` docs.
pub eip1014_transition: Option<Uint>,
/// See `CommonParams` docs.
pub eip1344_transition: Option<Uint>,
/// See `CommonParams` docs.
pub eip2028_transition: Option<Uint>,
/// See `CommonParams` docs.
pub dust_protection_transition: Option<Uint>,
Expand Down

0 comments on commit 453b6a0

Please sign in to comment.