mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2024-12-26 21:47:18 +00:00
3359ba7a04
* no pools * Remove excessive logging * Revert * Initial commit * Cleanup * Fix some compile errors * Fix core tests * Fix more tests * Fix eth/downloader * go mod tidy * Fix core/vm * Fix formatting * Fix formatting * Fix lint * Fix lint * Switch tests to StagedSync * Cleanup * Reuse cache in stage4 * Fix * Fix formatting * Try to fix test
200 lines
6.3 KiB
Go
200 lines
6.3 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/holiman/uint256"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common"
|
|
)
|
|
|
|
// ContractRef is a reference to the contract's backing object
|
|
type ContractRef interface {
|
|
Address() common.Address
|
|
}
|
|
|
|
// AccountRef implements ContractRef.
|
|
//
|
|
// Account references are used during EVM initialisation and
|
|
// it's primary use is to fetch addresses. Removing this object
|
|
// proves difficult because of the cached jump destinations which
|
|
// are fetched from the parent contract (i.e. the caller), which
|
|
// is a ContractRef.
|
|
type AccountRef common.Address
|
|
|
|
// Address casts AccountRef to a Address
|
|
func (ar AccountRef) Address() common.Address { return (common.Address)(ar) }
|
|
|
|
// Contract represents an ethereum contract in the state database. It contains
|
|
// the contract code, calling arguments. Contract implements ContractRef
|
|
type Contract struct {
|
|
// CallerAddress 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.
|
|
CallerAddress common.Address
|
|
caller ContractRef
|
|
self ContractRef
|
|
analysis []uint64 // Locally cached result of JUMPDEST analysis
|
|
dests Cache
|
|
|
|
Code []byte
|
|
CodeHash common.Hash
|
|
CodeAddr *common.Address
|
|
Input []byte
|
|
|
|
Gas uint64
|
|
value *uint256.Int
|
|
}
|
|
|
|
// NewContract returns a new contract environment for the execution of EVM.
|
|
func NewContract(caller ContractRef, object ContractRef, value *uint256.Int, gas uint64, dests Cache) *Contract {
|
|
c := &Contract{CallerAddress: caller.Address(), caller: caller, self: object}
|
|
|
|
// Gas should be a pointer so it can safely be reduced through the run
|
|
// This pointer will be off the state transition
|
|
c.Gas = gas
|
|
// ensures a value is set
|
|
c.value = value
|
|
|
|
c.dests = dests
|
|
|
|
return c
|
|
}
|
|
|
|
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 64bits.
|
|
// 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)
|
|
}
|
|
|
|
func (c *Contract) validJumpSubdest(udest uint64) bool {
|
|
// PC cannot go beyond len(code) and certainly can't be bigger than 63 bits.
|
|
// Don't bother checking for BEGINSUB in that case.
|
|
if int64(udest) < 0 || udest >= uint64(len(c.Code)) {
|
|
return false
|
|
}
|
|
// Only BEGINSUBs allowed for destinations
|
|
if OpCode(c.Code[udest]) != BEGINSUB {
|
|
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 have a contract hash already?
|
|
if c.CodeHash != (common.Hash{}) {
|
|
// Does parent context have the analysis?
|
|
analysis, exist := c.dests.Get(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.dests.Set(c.CodeHash, analysis)
|
|
}
|
|
// Also stash it in current contract for faster access
|
|
c.analysis = analysis
|
|
return c.analysis[udest/64]&(uint64(1)<<(udest&63)) == 0
|
|
}
|
|
// 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[udest/64]&(uint64(1)<<(udest&63)) == 0
|
|
}
|
|
|
|
// AsDelegate sets the contract to be a delegate call and returns the current
|
|
// contract (for chaining calls)
|
|
func (c *Contract) AsDelegate() *Contract {
|
|
// NOTE: caller must, at all times be a contract. It should never happen
|
|
// that caller is something other than a Contract.
|
|
parent := c.caller.(*Contract)
|
|
c.CallerAddress = parent.CallerAddress
|
|
c.value = parent.value
|
|
|
|
return c
|
|
}
|
|
|
|
// GetOp returns the n'th element in the contract's byte array
|
|
func (c *Contract) GetOp(n uint64) OpCode {
|
|
return OpCode(c.GetByte(n))
|
|
}
|
|
|
|
// GetByte returns the n'th byte in the contract's byte array
|
|
func (c *Contract) GetByte(n uint64) byte {
|
|
if n < uint64(len(c.Code)) {
|
|
return c.Code[n]
|
|
}
|
|
|
|
return 0
|
|
}
|
|
|
|
// 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.CallerAddress
|
|
}
|
|
|
|
// UseGas attempts the use gas and subtracts it and returns true on success
|
|
func (c *Contract) UseGas(gas uint64) (ok bool) {
|
|
if c.Gas < gas {
|
|
return false
|
|
}
|
|
c.Gas -= gas
|
|
return true
|
|
}
|
|
|
|
// Address returns the contracts address
|
|
func (c *Contract) Address() common.Address {
|
|
return c.self.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 and address of the backing data
|
|
// object
|
|
func (c *Contract) SetCallCode(addr *common.Address, hash common.Hash, code []byte) {
|
|
c.Code = code
|
|
c.CodeHash = hash
|
|
c.CodeAddr = addr
|
|
}
|
|
|
|
// SetCodeOptionalHash can be used to provide code, but it's optional to provide hash.
|
|
// In case hash is not provided, the jumpdest analysis will not be saved to the parent context
|
|
func (c *Contract) SetCodeOptionalHash(addr *common.Address, codeAndHash *codeAndHash) {
|
|
c.Code = codeAndHash.code
|
|
c.CodeHash = codeAndHash.hash
|
|
c.CodeAddr = addr
|
|
}
|