mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-25 16:06:18 +00:00
Implements spec change https://github.com/ethereum/EIPs/pull/11807 This PR resolves the conflict between the EIP-7928 and EIP-8037. Specifically in contract deployment, EIP-7928 requires to not resolve the deployed account until it's accessed, while in EIP-8037, the early access is required to determine if the account-creation should be charged or not. This PR addresses this conflict by changing the EIP-8037 a bit, unconditionally charge the account creation in CREATE Family (CreateTx, Create/Create2 opcode) and refunds the associated gas cost if the account creation doesn't happen ultimately. Checkout https://hackmd.io/@bFEBbZiVSAO0IURh9qzEFg/BJmFYqCeGl for more details What's more, now the LIFO mechanism is used for refilling the state cost in frame revert, frame halt, state opcode refunds.
206 lines
7.2 KiB
Go
206 lines
7.2 KiB
Go
// Copyright 2015 The go-ethereum Authors
|
|
// This file is part of the go-ethereum library.
|
|
//
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
package vm
|
|
|
|
import (
|
|
"github.com/ethereum/go-ethereum/common"
|
|
"github.com/ethereum/go-ethereum/core/tracing"
|
|
"github.com/holiman/uint256"
|
|
)
|
|
|
|
// Contract represents an ethereum contract in the state database. It contains
|
|
// the contract code, calling arguments. Contract implements ContractRef
|
|
type Contract struct {
|
|
// caller is the result of the caller which initialised this
|
|
// contract. However, when the "call method" is delegated this
|
|
// value needs to be initialised to that of the caller's caller.
|
|
caller common.Address
|
|
address common.Address
|
|
|
|
jumpDests JumpDestCache // Aggregated result of JUMPDEST analysis.
|
|
analysis BitVec // Locally cached result of JUMPDEST analysis
|
|
|
|
Code []byte
|
|
CodeHash common.Hash
|
|
Input []byte
|
|
|
|
// is the execution frame represented by this object a contract deployment
|
|
IsDeployment bool
|
|
IsSystemCall bool
|
|
|
|
// Gas carries the unified gas state for this frame: running balance,
|
|
// reservoir, and per-frame usage accumulators. See GasBudget.
|
|
Gas GasBudget
|
|
value *uint256.Int
|
|
}
|
|
|
|
// NewContract returns a new contract environment for the execution of EVM.
|
|
func NewContract(caller common.Address, address common.Address, value *uint256.Int, gas GasBudget, jumpDests JumpDestCache) *Contract {
|
|
// Initialize the jump analysis cache if it's nil, mostly for tests
|
|
if jumpDests == nil {
|
|
jumpDests = newMapJumpDests()
|
|
}
|
|
return &Contract{
|
|
caller: caller,
|
|
address: address,
|
|
jumpDests: jumpDests,
|
|
Gas: gas,
|
|
value: value,
|
|
}
|
|
}
|
|
|
|
func (c *Contract) validJumpdest(dest *uint256.Int) bool {
|
|
udest, overflow := dest.Uint64WithOverflow()
|
|
// PC cannot go beyond len(code) and certainly can't be bigger than 63bits.
|
|
// Don't bother checking for JUMPDEST in that case.
|
|
if overflow || udest >= uint64(len(c.Code)) {
|
|
return false
|
|
}
|
|
// Only JUMPDESTs allowed for destinations
|
|
if OpCode(c.Code[udest]) != JUMPDEST {
|
|
return false
|
|
}
|
|
return c.isCode(udest)
|
|
}
|
|
|
|
// isCode returns true if the provided PC location is an actual opcode, as
|
|
// opposed to a data-segment following a PUSHN operation.
|
|
func (c *Contract) isCode(udest uint64) bool {
|
|
// Do we already have an analysis laying around?
|
|
if c.analysis != nil {
|
|
return c.analysis.codeSegment(udest)
|
|
}
|
|
// Do we have a contract hash already?
|
|
// If we do have a hash, that means it's a 'regular' contract. For regular
|
|
// contracts ( not temporary initcode), we store the analysis in a map
|
|
if c.CodeHash != (common.Hash{}) {
|
|
// Does parent context have the analysis?
|
|
analysis, exist := c.jumpDests.Load(c.CodeHash)
|
|
if !exist {
|
|
// Do the analysis and save in parent context
|
|
// We do not need to store it in c.analysis
|
|
analysis = codeBitmap(c.Code)
|
|
c.jumpDests.Store(c.CodeHash, analysis)
|
|
}
|
|
// Also stash it in current contract for faster access
|
|
c.analysis = analysis
|
|
return analysis.codeSegment(udest)
|
|
}
|
|
// We don't have the code hash, most likely a piece of initcode not already
|
|
// in state trie. In that case, we do an analysis, and save it locally, so
|
|
// we don't have to recalculate it for every JUMP instruction in the execution
|
|
// However, we don't save it within the parent context
|
|
if c.analysis == nil {
|
|
c.analysis = codeBitmap(c.Code)
|
|
}
|
|
return c.analysis.codeSegment(udest)
|
|
}
|
|
|
|
// GetOp returns the n'th element in the contract's byte array
|
|
func (c *Contract) GetOp(n uint64) OpCode {
|
|
if n < uint64(len(c.Code)) {
|
|
return OpCode(c.Code[n])
|
|
}
|
|
return STOP
|
|
}
|
|
|
|
// Caller returns the caller of the contract.
|
|
//
|
|
// Caller will recursively call caller when the contract is a delegate
|
|
// call, including that of caller's caller.
|
|
func (c *Contract) Caller() common.Address {
|
|
return c.caller
|
|
}
|
|
|
|
// chargeRegular deducts regular gas only, with tracer integration.
|
|
// Returns false on OOG. Delegates the arithmetic to GasBudget.ChargeRegular.
|
|
func (c *Contract) chargeRegular(r uint64, logger *tracing.Hooks, reason tracing.GasChangeReason) bool {
|
|
prior, ok := c.Gas.ChargeRegular(r)
|
|
if !ok {
|
|
return false
|
|
}
|
|
if logger.HasGasHook() && reason != tracing.GasChangeIgnored {
|
|
logger.EmitGasChange(prior.AsTracing(), c.Gas.AsTracing(), reason)
|
|
}
|
|
return true
|
|
}
|
|
|
|
// chargeState deducts state gas (spilling into regular when the reservoir is
|
|
// exhausted), with tracer integration. Returns false on OOG.
|
|
func (c *Contract) chargeState(s uint64, logger *tracing.Hooks, reason tracing.GasChangeReason) bool {
|
|
prior, ok := c.Gas.ChargeState(s)
|
|
if !ok {
|
|
return false
|
|
}
|
|
if logger.HasGasHook() && reason != tracing.GasChangeIgnored {
|
|
logger.EmitGasChange(prior.AsTracing(), c.Gas.AsTracing(), reason)
|
|
}
|
|
return true
|
|
}
|
|
|
|
// refundState refunds the pre-charged state gas back to state reservoir.
|
|
func (c *Contract) refundState(s uint64, logger *tracing.Hooks, reason tracing.GasChangeReason) {
|
|
prior := c.Gas
|
|
c.Gas.RefundState(s)
|
|
|
|
if s != 0 && logger.HasGasHook() && reason != tracing.GasChangeIgnored {
|
|
logger.EmitGasChange(prior.AsTracing(), c.Gas.AsTracing(), reason)
|
|
}
|
|
}
|
|
|
|
// refundGas absorbs a sub-call's leftover GasBudget into this contract's gas state.
|
|
func (c *Contract) refundGas(child GasBudget, logger *tracing.Hooks, reason tracing.GasChangeReason) {
|
|
prior := c.Gas
|
|
c.Gas.Absorb(child)
|
|
if logger.HasGasHook() && reason != tracing.GasChangeIgnored {
|
|
logger.EmitGasChange(prior.AsTracing(), c.Gas.AsTracing(), reason)
|
|
}
|
|
}
|
|
|
|
// forwardGas drains `regular` regular gas and the entire state reservoir
|
|
// from this contract's running budget and returns the initial GasBudget for
|
|
// a child frame. The caller's UsedRegularGas is bumped by the forwarded
|
|
// amount so that the absorb-on-return path correctly reclaims the unused
|
|
// portion. Thin wrapper around GasBudget.Forward with tracer integration.
|
|
//
|
|
// Caller must ensure `regular` is no larger than the running balance (the
|
|
// opcode's dynamic gas table is expected to validate that before invoking
|
|
// the opcode handler).
|
|
func (c *Contract) forwardGas(regular uint64, logger *tracing.Hooks, reason tracing.GasChangeReason) GasBudget {
|
|
prior := c.Gas
|
|
child := c.Gas.Forward(regular)
|
|
if logger.HasGasHook() && reason != tracing.GasChangeIgnored {
|
|
logger.EmitGasChange(prior.AsTracing(), c.Gas.AsTracing(), reason)
|
|
}
|
|
return child
|
|
}
|
|
|
|
// Address returns the contracts address
|
|
func (c *Contract) Address() common.Address {
|
|
return c.address
|
|
}
|
|
|
|
// Value returns the contract's value (sent to it from it's caller)
|
|
func (c *Contract) Value() *uint256.Int {
|
|
return c.value
|
|
}
|
|
|
|
// SetCallCode sets the code of the contract,
|
|
func (c *Contract) SetCallCode(hash common.Hash, code []byte) {
|
|
c.Code = code
|
|
c.CodeHash = hash
|
|
}
|