diff --git a/Cargo.lock b/Cargo.lock index 3e670818f31..d134367f9a5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1407,6 +1407,7 @@ dependencies = [ "bit-set 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "criterion 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", "ethereum-types 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", + "hex-literal 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "keccak-hash 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/ethcore/evm/Cargo.toml b/ethcore/evm/Cargo.toml index 43621cb37e6..52bfec42309 100644 --- a/ethcore/evm/Cargo.toml +++ b/ethcore/evm/Cargo.toml @@ -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 = [] diff --git a/ethcore/evm/src/factory.rs b/ethcore/evm/src/factory.rs index 484b2852fec..5dbaf4f829d 100644 --- a/ethcore/evm/src/factory.rs +++ b/ethcore/evm/src/factory.rs @@ -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)), } } diff --git a/ethcore/evm/src/instructions.rs b/ethcore/evm/src/instructions.rs index 0cdbb5687da..1580f7b591d 100644 --- a/ethcore/evm/src/instructions.rs +++ b/ethcore/evm/src/instructions.rs @@ -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, @@ -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 } } } @@ -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)); diff --git a/ethcore/evm/src/interpreter/mod.rs b/ethcore/evm/src/interpreter/mod.rs index 76e5d9c3a6d..ab5127371b1 100644 --- a/ethcore/evm/src/interpreter/mod.rs +++ b/ethcore/evm/src/interpreter/mod.rs @@ -435,7 +435,8 @@ impl Interpreter { ((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 @@ -850,6 +851,9 @@ impl Interpreter { instructions::GASLIMIT => { self.stack.push(ext.env_info().gas_limit.clone()); }, + instructions::CHAINID => { + self.stack.push(ext.chain_id().into()) + }, // Stack instructions diff --git a/ethcore/evm/src/lib.rs b/ethcore/evm/src/lib.rs index 7cfa5a69eaa..db029d15f54 100644 --- a/ethcore/evm/src/lib.rs +++ b/ethcore/evm/src/lib.rs @@ -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; diff --git a/ethcore/evm/src/tests.rs b/ethcore/evm/src/tests.rs index e9a18ecbdd7..49995fdb366 100644 --- a/ethcore/evm/src/tests.rs +++ b/ethcore/evm/src/tests.rs @@ -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) { @@ -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 diff --git a/ethcore/src/externalities.rs b/ethcore/src/externalities.rs index 77629231c02..c5b2f37387b 100644 --- a/ethcore/src/externalities.rs +++ b/ethcore/src/externalities.rs @@ -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 } diff --git a/ethcore/src/json_tests/executive.rs b/ethcore/src/json_tests/executive.rs index 3c2792740b6..6258648d994 100644 --- a/ethcore/src/json_tests/executive.rs +++ b/ethcore/src/json_tests/executive.rs @@ -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 } diff --git a/ethcore/src/spec/spec.rs b/ethcore/src/spec/spec.rs index 766d260a9b7..a3a8fb2307e 100644 --- a/ethcore/src/spec/spec.rs +++ b/ethcore/src/spec/spec.rs @@ -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. @@ -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; @@ -313,6 +316,10 @@ impl From 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, diff --git a/ethcore/vm/src/env_info.rs b/ethcore/vm/src/env_info.rs index 6c602d80fe6..6dc742938fe 100644 --- a/ethcore/vm/src/env_info.rs +++ b/ethcore/vm/src/env_info.rs @@ -65,7 +65,7 @@ impl From 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(), diff --git a/ethcore/vm/src/ext.rs b/ethcore/vm/src/ext.rs index a527a4fb293..3c3a2335bc1 100644 --- a/ethcore/vm/src/ext.rs +++ b/ethcore/vm/src/ext.rs @@ -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, diff --git a/ethcore/vm/src/schedule.rs b/ethcore/vm/src/schedule.rs index 91ec05ec073..764487e92b4 100644 --- a/ethcore/vm/src/schedule.rs +++ b/ethcore/vm/src/schedule.rs @@ -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 @@ -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, @@ -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 } @@ -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, diff --git a/ethcore/vm/src/tests.rs b/ethcore/vm/src/tests.rs index c6bcd5f93a6..5f673493854 100644 --- a/ethcore/vm/src/tests.rs +++ b/ethcore/vm/src/tests.rs @@ -67,6 +67,8 @@ pub struct FakeExt { pub balances: HashMap, pub tracing: bool, pub is_static: bool, + + chain_id: u64, } // similar to the normal `finalize` function, but ignoring NeedsReturn. @@ -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(); @@ -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 { @@ -207,7 +215,7 @@ impl Ext for FakeExt { fn log(&mut self, topics: Vec, data: &[u8]) -> Result<()> { self.logs.push(FakeLogEntry { - topics: topics, + topics, data: data.to_vec() }); Ok(()) @@ -230,6 +238,10 @@ impl Ext for FakeExt { &self.info } + fn chain_id(&self) -> u64 { + self.chain_id + } + fn depth(&self) -> usize { self.depth } diff --git a/json/src/spec/params.rs b/json/src/spec/params.rs index bd16fd813a9..7f48c3e9f45 100644 --- a/json/src/spec/params.rs +++ b/json/src/spec/params.rs @@ -94,6 +94,8 @@ pub struct Params { /// See `CommonParams` docs. pub eip1014_transition: Option, /// See `CommonParams` docs. + pub eip1344_transition: Option, + /// See `CommonParams` docs. pub eip2028_transition: Option, /// See `CommonParams` docs. pub dust_protection_transition: Option,