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

[stable] Backport constantinople changes #9723

Merged
merged 19 commits into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
eefc94e
Implement EIP-1052 (EXTCODEHASH) and fix several issues in state acco…
sorpaas Jul 31, 2018
72594db
Comply EIP-86 with the new definition (#9140)
sorpaas Aug 1, 2018
a86e973
Implement KIP4: create2 for wasm (#9277)
sorpaas Aug 6, 2018
a1e2d94
`gasleft` extern implemented for WASM runtime (kip-6) (#9357)
lexfrl Aug 24, 2018
b7d9ec6
Add EIP-1014 transition config flag (#9268)
sorpaas Aug 31, 2018
bec26cb
EIP 1283: Net gas metering for SSTORE without dirty maps (#9319)
sorpaas Sep 7, 2018
b360481
ethash: implement EIP-1234 (#9187)
5chdn Jul 22, 2018
8cb1874
Update state tests execution model (#9440)
tomusdrw Sep 10, 2018
6e30af2
Fix checkpointing when creating contract failed (#9514)
sorpaas Sep 11, 2018
05f5e48
In create memory calculation is the same for create2 because the addi…
cheme Sep 11, 2018
dd5a298
Enable all Constantinople hard fork changes in constantinople_test.js…
sorpaas Sep 11, 2018
6ba8eda
Add constantinople conf to EvmTestClient. (#9570)
cheme Sep 25, 2018
c898463
Hardfork the testnets (#9562)
5chdn Sep 30, 2018
5061017
Don't hash the init_code of CREATE. (#9688)
tomusdrw Oct 4, 2018
1556356
Implement CREATE2 gas changes and fix some potential overflowing (#9694)
sorpaas Oct 4, 2018
c079497
ethcore: delay ropsten hardfork (#9704)
5chdn Oct 5, 2018
4c74b4b
Add hardcoded headers (#9730)
Tbaut Oct 10, 2018
07ccad5
gitlab ci: releasable_branches: change variables condition to schedul…
gabreal Oct 10, 2018
2b3d34f
HF in POA Core (2018-10-22) (#9724)
varasev Oct 9, 2018
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
1 change: 1 addition & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ cache:
- stable
- beta
- tags
- schedules

.collect_artifacts: &collect_artifacts
artifacts:
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ daemonize = { git = "https://github.com/paritytech/daemonize" }
[features]
miner-debug = ["ethcore/miner-debug"]
json-tests = ["ethcore/json-tests"]
ci-skip-issue = ["ethcore/ci-skip-issue"]
test-heavy = ["ethcore/test-heavy"]
evm-debug = ["ethcore/evm-debug"]
evm-debug-tests = ["ethcore/evm-debug-tests"]
Expand Down
2 changes: 2 additions & 0 deletions ethcore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ evm-debug-tests = ["evm-debug", "evm/evm-debug-tests"]
slow-blocks = []
# Run JSON consensus tests.
json-tests = ["ethcore-transaction/json-tests", "test-helpers", "tempdir"]
# Skip JSON consensus tests with pending issues.
ci-skip-issue = []
# Run memory/cpu heavy tests.
test-heavy = []
# Compile benches
Expand Down
2 changes: 1 addition & 1 deletion ethcore/evm/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl Finalize for Result<GasLeft> {

/// Cost calculation type. For low-gas usage we calculate costs using usize instead of U256
pub trait CostType: Sized + From<usize> + Copy
+ ops::Mul<Output=Self> + ops::Div<Output=Self> + ops::Add<Output=Self> +ops::Sub<Output=Self>
+ ops::Mul<Output=Self> + ops::Div<Output=Self> + ops::Add<Output=Self> + ops::Sub<Output=Self>
+ ops::Shr<usize, Output=Self> + ops::Shl<usize, Output=Self>
+ cmp::Ord + fmt::Debug {
/// Converts this cost into `U256`
Expand Down
7 changes: 5 additions & 2 deletions ethcore/evm/src/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ enum_with_from_u8! {
RETURNDATASIZE = 0x3d,
#[doc = "copy return data buffer to memory"]
RETURNDATACOPY = 0x3e,
#[doc = "return the keccak256 hash of contract code"]
EXTCODEHASH = 0x3f,

#[doc = "get hash of most recent complete block"]
BLOCKHASH = 0x40,
Expand Down Expand Up @@ -326,7 +328,7 @@ enum_with_from_u8! {
#[doc = "like CALLCODE but keeps caller's value and sender"]
DELEGATECALL = 0xf4,
#[doc = "create a new account and set creation address to sha3(sender + sha3(init code)) % 2**160"]
CREATE2 = 0xfb,
CREATE2 = 0xf5,
#[doc = "stop execution and revert state changes. Return output data."]
REVERT = 0xfd,
#[doc = "like CALL but it does not take value, nor modify the state"]
Expand Down Expand Up @@ -492,6 +494,7 @@ lazy_static! {
arr[CALLDATALOAD as usize] = Some(InstructionInfo::new("CALLDATALOAD", 1, 1, GasPriceTier::VeryLow));
arr[CALLDATASIZE as usize] = Some(InstructionInfo::new("CALLDATASIZE", 0, 1, GasPriceTier::Base));
arr[CALLDATACOPY as usize] = Some(InstructionInfo::new("CALLDATACOPY", 3, 0, GasPriceTier::VeryLow));
arr[EXTCODEHASH as usize] = Some(InstructionInfo::new("EXTCODEHASH", 1, 1, GasPriceTier::Special));
arr[CODESIZE as usize] = Some(InstructionInfo::new("CODESIZE", 0, 1, GasPriceTier::Base));
arr[CODECOPY as usize] = Some(InstructionInfo::new("CODECOPY", 3, 0, GasPriceTier::VeryLow));
arr[GASPRICE as usize] = Some(InstructionInfo::new("GASPRICE", 0, 1, GasPriceTier::Base));
Expand Down Expand Up @@ -591,7 +594,7 @@ lazy_static! {
arr[DELEGATECALL as usize] = Some(InstructionInfo::new("DELEGATECALL", 6, 1, GasPriceTier::Special));
arr[STATICCALL as usize] = Some(InstructionInfo::new("STATICCALL", 6, 1, GasPriceTier::Special));
arr[SUICIDE as usize] = Some(InstructionInfo::new("SUICIDE", 1, 0, GasPriceTier::Special));
arr[CREATE2 as usize] = Some(InstructionInfo::new("CREATE2", 3, 1, GasPriceTier::Special));
arr[CREATE2 as usize] = Some(InstructionInfo::new("CREATE2", 4, 1, GasPriceTier::Special));
arr[REVERT as usize] = Some(InstructionInfo::new("REVERT", 2, 0, GasPriceTier::Zero));
arr
};
Expand Down
141 changes: 128 additions & 13 deletions ethcore/evm/src/interpreter/gasometer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,17 @@ impl<Gas: evm::CostType> Gasometer<Gas> {
let newval = stack.peek(1);
let val = U256::from(&*ext.storage_at(&address)?);

let gas = if val.is_zero() && !newval.is_zero() {
schedule.sstore_set_gas
let gas = if schedule.eip1283 {
let orig = U256::from(&*ext.initial_storage_at(&address)?);
calculate_eip1283_sstore_gas(schedule, &orig, &val, &newval)
} else {
// Refund for below case is added when actually executing sstore
// !is_zero(&val) && is_zero(newval)
schedule.sstore_reset_gas
if val.is_zero() && !newval.is_zero() {
schedule.sstore_set_gas
} else {
// Refund for below case is added when actually executing sstore
// !is_zero(&val) && is_zero(newval)
schedule.sstore_reset_gas
}
};
Request::Gas(Gas::from(gas))
},
Expand All @@ -143,6 +148,9 @@ impl<Gas: evm::CostType> Gasometer<Gas> {
instructions::EXTCODESIZE => {
Request::Gas(Gas::from(schedule.extcodesize_gas))
},
instructions::EXTCODEHASH => {
Request::Gas(Gas::from(schedule.extcodehash_gas))
},
instructions::SUICIDE => {
let mut gas = Gas::from(schedule.suicide_gas);

Expand All @@ -168,9 +176,8 @@ impl<Gas: evm::CostType> Gasometer<Gas> {
Request::GasMem(default_gas, mem_needed(stack.peek(0), stack.peek(1))?)
},
instructions::SHA3 => {
let w = overflowing!(add_gas_usize(Gas::from_u256(*stack.peek(1))?, 31));
let words = w >> 5;
let gas = Gas::from(schedule.sha3_gas) + (Gas::from(schedule.sha3_word_gas) * words);
let words = overflowing!(to_word_size(Gas::from_u256(*stack.peek(1))?));
let gas = overflowing!(Gas::from(schedule.sha3_gas).overflow_add(overflowing!(Gas::from(schedule.sha3_word_gas).overflow_mul(words))));
Request::GasMem(gas, mem_needed(stack.peek(0), stack.peek(1))?)
},
instructions::CALLDATACOPY | instructions::CODECOPY | instructions::RETURNDATACOPY => {
Expand Down Expand Up @@ -223,9 +230,24 @@ impl<Gas: evm::CostType> Gasometer<Gas> {

Request::GasMemProvide(gas, mem, Some(requested))
},
instructions::CREATE | instructions::CREATE2 => {
instructions::CREATE => {
let start = stack.peek(1);
let len = stack.peek(2);

let gas = Gas::from(schedule.create_gas);
let mem = mem_needed(stack.peek(1), stack.peek(2))?;
let mem = mem_needed(start, len)?;

Request::GasMemProvide(gas, mem, None)
},
instructions::CREATE2 => {
let start = stack.peek(1);
let len = stack.peek(2);

let base = Gas::from(schedule.create_gas);
let word = overflowing!(to_word_size(Gas::from_u256(*len)?));
let word_gas = overflowing!(Gas::from(schedule.sha3_word_gas).overflow_mul(word));
let gas = overflowing!(base.overflow_add(word_gas));
let mem = mem_needed(start, len)?;

Request::GasMemProvide(gas, mem, None)
},
Expand Down Expand Up @@ -275,8 +297,8 @@ impl<Gas: evm::CostType> Gasometer<Gas> {
},
Request::GasMemCopy(gas, mem_size, copy) => {
let (mem_gas_cost, new_mem_gas, new_mem_size) = self.mem_gas_cost(schedule, current_mem_size, &mem_size)?;
let copy = overflowing!(add_gas_usize(copy, 31)) >> 5;
let copy_gas = Gas::from(schedule.copy_gas) * copy;
let copy = overflowing!(to_word_size(copy));
let copy_gas = overflowing!(Gas::from(schedule.copy_gas).overflow_mul(copy));
let gas = overflowing!(gas.overflow_add(copy_gas));
let gas = overflowing!(gas.overflow_add(mem_gas_cost));

Expand All @@ -303,7 +325,7 @@ impl<Gas: evm::CostType> Gasometer<Gas> {
};

let current_mem_size = Gas::from(current_mem_size);
let req_mem_size_rounded = (overflowing!(mem_size.overflow_add(Gas::from(31 as usize))) >> 5) << 5;
let req_mem_size_rounded = overflowing!(to_word_size(*mem_size)) << 5;

let (mem_gas_cost, new_mem_gas) = if req_mem_size_rounded > current_mem_size {
let new_mem_gas = gas_for_mem(req_mem_size_rounded)?;
Expand Down Expand Up @@ -335,6 +357,99 @@ fn add_gas_usize<Gas: evm::CostType>(value: Gas, num: usize) -> (Gas, bool) {
value.overflow_add(Gas::from(num))
}

#[inline]
fn to_word_size<Gas: evm::CostType>(value: Gas) -> (Gas, bool) {
let (gas, overflow) = add_gas_usize(value, 31);
if overflow {
return (gas, overflow);
}

(gas >> 5, false)
}

#[inline]
fn calculate_eip1283_sstore_gas<Gas: evm::CostType>(schedule: &Schedule, original: &U256, current: &U256, new: &U256) -> Gas {
Gas::from(
if current == new {
// 1. If current value equals new value (this is a no-op), 200 gas is deducted.
schedule.sload_gas
} else {
// 2. If current value does not equal new value
if original == current {
// 2.1. If original value equals current value (this storage slot has not been changed by the current execution context)
if original.is_zero() {
// 2.1.1. If original value is 0, 20000 gas is deducted.
schedule.sstore_set_gas
} else {
// 2.1.2. Otherwise, 5000 gas is deducted.
schedule.sstore_reset_gas

// 2.1.2.1. If new value is 0, add 15000 gas to refund counter.
}
} else {
// 2.2. If original value does not equal current value (this storage slot is dirty), 200 gas is deducted. Apply both of the following clauses.
schedule.sload_gas

// 2.2.1. If original value is not 0
// 2.2.1.1. If current value is 0 (also means that new value is not 0), remove 15000 gas from refund counter. We can prove that refund counter will never go below 0.
// 2.2.1.2. If new value is 0 (also means that current value is not 0), add 15000 gas to refund counter.

// 2.2.2. If original value equals new value (this storage slot is reset)
// 2.2.2.1. If original value is 0, add 19800 gas to refund counter.
// 2.2.2.2. Otherwise, add 4800 gas to refund counter.
}
}
)
}

pub fn handle_eip1283_sstore_clears_refund(ext: &mut vm::Ext, original: &U256, current: &U256, new: &U256) {
let sstore_clears_schedule = U256::from(ext.schedule().sstore_refund_gas);

if current == new {
// 1. If current value equals new value (this is a no-op), 200 gas is deducted.
} else {
// 2. If current value does not equal new value
if original == current {
// 2.1. If original value equals current value (this storage slot has not been changed by the current execution context)
if original.is_zero() {
// 2.1.1. If original value is 0, 20000 gas is deducted.
} else {
// 2.1.2. Otherwise, 5000 gas is deducted.
if new.is_zero() {
// 2.1.2.1. If new value is 0, add 15000 gas to refund counter.
ext.add_sstore_refund(sstore_clears_schedule);
}
}
} else {
// 2.2. If original value does not equal current value (this storage slot is dirty), 200 gas is deducted. Apply both of the following clauses.

if !original.is_zero() {
// 2.2.1. If original value is not 0
if current.is_zero() {
// 2.2.1.1. If current value is 0 (also means that new value is not 0), remove 15000 gas from refund counter. We can prove that refund counter will never go below 0.
ext.sub_sstore_refund(sstore_clears_schedule);
} else if new.is_zero() {
// 2.2.1.2. If new value is 0 (also means that current value is not 0), add 15000 gas to refund counter.
ext.add_sstore_refund(sstore_clears_schedule);
}
}

if original == new {
// 2.2.2. If original value equals new value (this storage slot is reset)
if original.is_zero() {
// 2.2.2.1. If original value is 0, add 19800 gas to refund counter.
let refund = U256::from(ext.schedule().sstore_set_gas - ext.schedule().sload_gas);
ext.add_sstore_refund(refund);
} else {
// 2.2.2.2. Otherwise, add 4800 gas to refund counter.
let refund = U256::from(ext.schedule().sstore_reset_gas - ext.schedule().sload_gas);
ext.add_sstore_refund(refund);
}
}
}
}
}

#[test]
fn test_mem_gas_cost() {
// given
Expand Down
37 changes: 28 additions & 9 deletions ethcore/evm/src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ impl<Cost: CostType> vm::Vm for Interpreter<Cost> {
match result {
InstructionResult::JumpToPosition(position) => {
if valid_jump_destinations.is_none() {
let code_hash = params.code_hash.clone().unwrap_or_else(|| keccak(code.as_ref()));
valid_jump_destinations = Some(self.cache.jump_destinations(&code_hash, code));
valid_jump_destinations = Some(self.cache.jump_destinations(&params.code_hash, code));
}
let jump_destinations = valid_jump_destinations.as_ref().expect("jump_destinations are initialized on first jump; qed");
let pos = self.verify_jump(position, jump_destinations)?;
Expand Down Expand Up @@ -230,8 +229,9 @@ impl<Cost: CostType> Interpreter<Cost> {
(instruction == instructions::STATICCALL && !schedule.have_static_call) ||
((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::SHL || instruction == instructions::SHR || instruction == instructions::SAR) && !schedule.have_bitwise_shifting) ||
(instruction == instructions::EXTCODEHASH && !schedule.have_extcodehash)
{
return Err(vm::Error::BadInstruction {
instruction: instruction as u8
});
Expand Down Expand Up @@ -318,6 +318,11 @@ impl<Cost: CostType> Interpreter<Cost> {
let endowment = stack.pop_back();
let init_off = stack.pop_back();
let init_size = stack.pop_back();
let address_scheme = match instruction {
instructions::CREATE => CreateContractAddress::FromSenderAndNonce,
instructions::CREATE2 => CreateContractAddress::FromSenderSaltAndCodeHash(stack.pop_back().into()),
_ => unreachable!("instruction can only be CREATE/CREATE2 checked above; qed"),
};

let create_gas = provided.expect("`provided` comes through Self::exec from `Gasometer::get_gas_cost_mem`; `gas_gas_mem_cost` guarantees `Some` when instruction is `CALL`/`CALLCODE`/`DELEGATECALL`/`CREATE`; this is `CREATE`; qed");

Expand All @@ -335,7 +340,6 @@ impl<Cost: CostType> Interpreter<Cost> {
}

let contract_code = self.mem.read_slice(init_off, init_size);
let address_scheme = if instruction == instructions::CREATE { CreateContractAddress::FromSenderAndNonce } else { CreateContractAddress::FromSenderAndCodeHash };

let create_result = ext.create(&create_gas.as_u256(), &endowment, contract_code, address_scheme);
return match create_result {
Expand Down Expand Up @@ -510,8 +514,14 @@ impl<Cost: CostType> Interpreter<Cost> {

let current_val = U256::from(&*ext.storage_at(&address)?);
// Increase refund for clear
if !self.is_zero(&current_val) && self.is_zero(&val) {
ext.inc_sstore_clears();
if ext.schedule().eip1283 {
let original_val = U256::from(&*ext.initial_storage_at(&address)?);
gasometer::handle_eip1283_sstore_clears_refund(ext, &original_val, &current_val, &val);
} else {
if !current_val.is_zero() && val.is_zero() {
let sstore_clears_schedule = U256::from(ext.schedule().sstore_refund_gas);
ext.add_sstore_refund(sstore_clears_schedule);
}
}
ext.set_storage(address, H256::from(&val))?;
},
Expand Down Expand Up @@ -568,9 +578,14 @@ impl<Cost: CostType> Interpreter<Cost> {
},
instructions::EXTCODESIZE => {
let address = u256_to_address(&stack.pop_back());
let len = ext.extcodesize(&address)?;
let len = ext.extcodesize(&address)?.unwrap_or(0);
stack.push(U256::from(len));
},
instructions::EXTCODEHASH => {
let address = u256_to_address(&stack.pop_back());
let hash = ext.extcodehash(&address)?.unwrap_or_else(H256::zero);
stack.push(U256::from(hash));
},
instructions::CALLDATACOPY => {
Self::copy_data_to_memory(&mut self.mem, stack, params.data.as_ref().map_or_else(|| &[] as &[u8], |d| &*d as &[u8]));
},
Expand All @@ -591,7 +606,11 @@ impl<Cost: CostType> Interpreter<Cost> {
instructions::EXTCODECOPY => {
let address = u256_to_address(&stack.pop_back());
let code = ext.extcode(&address)?;
Self::copy_data_to_memory(&mut self.mem, stack, &code);
Self::copy_data_to_memory(
&mut self.mem,
stack,
code.as_ref().map(|c| &(*c)[..]).unwrap_or(&[])
);
},
instructions::GASPRICE => {
stack.push(params.gas_price.clone());
Expand Down
19 changes: 12 additions & 7 deletions ethcore/evm/src/interpreter/shared_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,22 @@ impl SharedCache {
}

/// Get jump destinations bitmap for a contract.
pub fn jump_destinations(&self, code_hash: &H256, code: &[u8]) -> Arc<BitSet> {
if code_hash == &KECCAK_EMPTY {
return Self::find_jump_destinations(code);
}
pub fn jump_destinations(&self, code_hash: &Option<H256>, code: &[u8]) -> Arc<BitSet> {
if let Some(ref code_hash) = code_hash {
if code_hash == &KECCAK_EMPTY {
return Self::find_jump_destinations(code);
}

if let Some(d) = self.jump_destinations.lock().get_mut(code_hash) {
return d.0.clone();
if let Some(d) = self.jump_destinations.lock().get_mut(code_hash) {
return d.0.clone();
}
}

let d = Self::find_jump_destinations(code);
self.jump_destinations.lock().insert(code_hash.clone(), Bits(d.clone()));

if let Some(ref code_hash) = code_hash {
self.jump_destinations.lock().insert(*code_hash, Bits(d.clone()));
}

d
}
Expand Down
Loading