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

Implement CREATE2 gas changes and fix some potential overflowing #9694

Merged
merged 4 commits into from
Oct 4, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
2 changes: 1 addition & 1 deletion ethcore/evm/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl Finalize for Error {

/// Cost calculation type. For low-gas usage we calculate costs using usize instead of U256
pub trait CostType: Sized + From<usize> + Copy + Send
+ 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
20 changes: 16 additions & 4 deletions ethcore/evm/src/interpreter/gasometer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl<Gas: evm::CostType> Gasometer<Gas> {
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 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 @@ -232,8 +232,20 @@ impl<Gas: evm::CostType> Gasometer<Gas> {
Request::GasMemProvide(gas, mem, Some(requested))
},
instructions::CREATE | instructions::CREATE2 => {
let gas = Gas::from(schedule.create_gas);
let mem = mem_needed(stack.peek(1), stack.peek(2))?;
let start = stack.peek(1);
let len = stack.peek(2);

let gas = match instruction {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be better to just split instructions::CREATE and instructions::CREATE2 in the upper match instead of matching twice.

instructions::CREATE => Gas::from(schedule.create_gas),
instructions::CREATE2 => {
let base = Gas::from(schedule.create_gas);
let word = overflowing!(add_gas_usize(Gas::from_u256(*len)?, 31)) >> 5;
let word_gas = overflowing!(Gas::from(schedule.sha3_word_gas).overflow_mul(word));
overflowing!(base.overflow_add(word_gas))
},
_ => unreachable!("CREATE/CREATE2 branch is checked above; qed"),
};
let mem = mem_needed(start, len)?;

Request::GasMemProvide(gas, mem, None)
},
Expand Down Expand Up @@ -284,7 +296,7 @@ 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_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 Down