Skip to content

Commit

Permalink
fix(eof): output gas for eofcreate (#1540)
Browse files Browse the repository at this point in the history
  • Loading branch information
rakita authored Jun 18, 2024
1 parent 41e2f7f commit c2a04cb
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
5 changes: 5 additions & 0 deletions crates/interpreter/src/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ impl Gas {
self.remaining
}

/// Return remaining gas after subtracting 63/64 parts.
pub const fn remaining_63_of_64_parts(&self) -> u64 {
self.remaining - self.remaining / 64
}

/// Erases a gas cost from the totals.
#[inline]
pub fn erase_cost(&mut self, returned: u64) {
Expand Down
4 changes: 1 addition & 3 deletions crates/interpreter/src/instructions/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,8 @@ pub fn eofcreate<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H)
.target_address
.create2(salt.to_be_bytes(), keccak256(sub_container));

let gas_reduce = max(interpreter.gas.remaining() / 64, 5000);
let gas_limit = interpreter.gas().remaining().saturating_sub(gas_reduce);
let gas_limit = interpreter.gas().remaining_63_of_64_parts();
gas!(interpreter, gas_limit);

// Send container for execution container is preverified.
interpreter.instruction_result = InstructionResult::CallOrCreate;
interpreter.next_action = InterpreterAction::EOFCreate {
Expand Down
6 changes: 4 additions & 2 deletions crates/interpreter/src/instructions/contract/call_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ pub fn calc_call_gas<SPEC: Spec>(

// EIP-150: Gas cost changes for IO-heavy operations
let gas_limit = if SPEC::enabled(TANGERINE) {
let gas = interpreter.gas().remaining();
// take l64 part of gas_limit
min(gas - gas / 64, local_gas_limit)
min(
interpreter.gas().remaining_63_of_64_parts(),
local_gas_limit,
)
} else {
local_gas_limit
};
Expand Down
8 changes: 8 additions & 0 deletions crates/revm/src/context/inner_evm_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,14 @@ impl<DB: Database> InnerEvmContext<DB> {
return;
}

// deduct gas for code deployment.
let gas_for_code = interpreter_result.output.len() as u64 * gas::CODEDEPOSIT;
if !interpreter_result.gas.record_cost(gas_for_code) {
self.journaled_state.checkpoint_revert(journal_checkpoint);
interpreter_result.result = InstructionResult::OutOfGas;
return;
}

// commit changes reduces depth by -1.
self.journaled_state.checkpoint_commit();

Expand Down

0 comments on commit c2a04cb

Please sign in to comment.