mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 18:32:23 +00:00
Squash the main verkle PR ahead of rebase don't call Bytes() in GetTreeKey (#137) trie: avoid endianness conversion in GetTreeKey (#140) * trie/utils: add concrete expected value in trie key generation test Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> * mod: update to latest go-verkle Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> * trie/utils: avoid endianness conversions Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> * apply review changes & update to official go-verkle version Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> upgrade go-verkle to CoW version and get TestProcessVerkle to build (#138) updating ci to use self-hosted machine (#143) fix: storage offset in non-header group + reuse of value buffer (#145) dedup call to ChunkifyCode, same as replay branch (#156) * dedup call to ChunkifyCode, same as replay branch * fix some linter issues fix code offset in tree update (#157) fix REVERT in state processor test execution (#158) * fix code offset in tree update * fix REVERT in test execution save on key hashing: lump code size update with first code chunk group (#159) fix code chunk key calculation and storage key calculation (#161) * fix codeKey calculation * Remove * fix storageOffset * fix the fix to the fix to the offset fix * Remove copy/pasted, unused code in test * fix linter --------- Co-authored-by: Guillaume Ballet <3272758+gballet@users.noreply.github.com> fix: infinite loop when calling extcodecopy on empty code (#151) upgrade to latest go-verkle fix: only update code in the tree if it's dirty (#174) fix: read-touch the code size and Keccak of the origin (#175) List of changes for converting a sepolia database (#182) * naive conversion rebased on top of beverly hills * changes for the sepolia shadow fork conversion * fixes to please the linter * fixes to please the linter Unified point cache (#180) * Unified point cache * Use cache for Try*Account * alter Trie interface to use caching for slots (#181) * alter Trie interface to use caching for slots * fix: use a lock to protect the point cache (#185) * use fastest non-master go-verkle version & pull trie/Verkle.go changes to use new api (#184) * mod: update to fastest go-verkle version today Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> * trie/verkle: use new batch serialization api Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> --------- Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> --------- Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> Co-authored-by: Ignacio Hagopian <jsign.uy@gmail.com> * fix: TryDelete signature in unit tests --------- Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> Co-authored-by: Ignacio Hagopian <jsign.uy@gmail.com> trie/utils: fix potential overflow (#191) * trie/utils: fix potential overflow Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> * trie/utils: receive storage key as a byte slice Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> * revert formatter changes Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> * trie/utils: fix mod 256 Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> --------- Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> trie/utils: fix incorrect bigint assignment (#193) Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> upgrade precomp link to fix CI fix: add missing code size&keccak leaves in empty accounts (#192) fixes to use the latest go-verkle@master (#197) * fixes to use the latest go-verkle@master * linter fixes * linter fixes for tests * fix: use jsign's go-verkle fix refactor: remove unused (*StateDB).GetXLittleEndian methods (#204) fix gas accounting issue in state_processor_test.go (#207) update go-verkle not to use StatelessNode anymore (#206) * update go-verkle not to use StatelessNode anymore * update go-verkle to latest refactor: move verkle gas accounting to its own block in TransitionDB (#208) fix a panic in deserializeVerkleProof if GetProofItems returns a nil ProofElements use the cachingDB instead of a custom VerkleDB (#209) * use the cachingDB instead of a custom VerkleDB * fix stack trace in LES remove holiman from CODEOWNERS as he gets too many emails read from tree in state object if the snapshot is nil (#205) add missing error checks for the root node type (#214) implement OpenStorageTrie for verkle trees (#210) * implement OpenStorageTrie for verkle trees * add a few comments for future maintenance * fix linter issue fix: copy balance leaf to new buffer in TryGetAccount (#217) implement some heretofore unimplemented iterator methods (#219) params: move verkle params to their own file (#228) fix: proper number of chunk evals (#215) overlay transition (#244) * overlay transition Fix some bugs identified in the code review Co-authored-by: Ignacio Hagopian <jsign.uy@gmail.com> Include base -> overlay key-values migration logic (#199) * mod: add go-verkle version with key-value migration new apis Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> * core/stateprocessor: use constant for max number of migrated key-values Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> * core: add base->overlay key-values migration logic Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> * core: fix some compiler errors Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> * trie: consider removing transition trie api in the future Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> * mod: use latest go-verkle Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> --------- Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> fix some unit tests errors get convresion block from file fix compilation issues fix initialization issue in migrator fix: changes needed to run the first 28 blocks important sutff: fix the banner fix: use nonce instead of balance in nonce leaf (#202) fixes for performing the overlay transition (#203) * fixes for performing the overlay transition * fixes for the full replay * fix: deletion-and-recreation of EoA * fixes to replay 2M+ blocks * upgrade to go-verkle@master * fix: proper number of chunk evals * rewrite conversion loop to fix known issues changes to make replay work with the overlay method (#216) * fixes for performing the overlay transition fixes for the full replay fix: deletion-and-recreation of EoA fixes to replay 2M+ blocks upgrade to go-verkle@master fix: proper number of chunk evals rewrite conversion loop to fix known issues changes to make replay work with the overlay method fixes to replay 2M+ blocks update to latest go-verkle@master * use a PBSS-like scheme for internal nodes (#221) * use a PBSS-like scheme for internal nodes * a couple of fixes coming from debugging replay * fix: use an error to notify the transition tree that a deleted account was found in the overlay tree (#222) * fixes for pbss replay (#227) * fixes for pbss replay * trie/verkle: use capped batch size (#229) * trie/verkle: use capped batch size Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> * trie/verkle: avoid path variable allocation per db.Put Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> * don't keep more than 32 state root conversions in RAM (#230) --------- Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> Co-authored-by: Guillaume Ballet <3272758+gballet@users.noreply.github.com> * cleanup some code * mod: update go-verkle Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> * re-enable snapshot (#231) * re-enable cancun block / snapshot (#226) * clear storage conversion key upon translating account (#234) * clear storage conversion key upon translating account * mod: use latest go-verkle Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> --------- Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> Co-authored-by: Ignacio Hagopian <jsign.uy@gmail.com> * fix: self-deadlock with translated root map mutex (#236) * return compressed commitment as root commitment (#237) --------- Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> Co-authored-by: Ignacio Hagopian <jsign.uy@gmail.com> --------- Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> Co-authored-by: Ignacio Hagopian <jsign.uy@gmail.com> --------- Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> Co-authored-by: Ignacio Hagopian <jsign.uy@gmail.com> fix first panic in *TransitionTrie.Copy() upgrade go-verkle to latest master mod: update go-verkle (#239) Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> core: print state root every 100 blocks (#240) Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> fix: only Commit the account trie (#242) fixes to get TestProcessVerkle to work with the overlay branch (#238) * fixes to get TestProcessVerkle to work with the overlay branch * fix all panics in verkle state processor test * fix proof verification move transition management to cachingDB * fix: mark the verkle transition as started if it's ended without being started * fix the verkle state processing test * fix linter errors * Add a function to clear verkle params for replay * fix: handle TransitionTrie in OpenStorageTrie * fix linter issue * fix the deleted account error (#247) * code cleanup (#248) * fix: don't error on a missing conversion.txt (#249) * Overlay Tree preimages exporting and usage (#246) * export overlay preimages tool Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> * use preimages flat file in overlay tree migration logic Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> * cmd/geth: add --roothash to overlay tree preimage exporting command Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> * cleanup Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> * review feedback Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> --------- Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> * fix: reduce the PR footprint (#250) * fix: don't fail when preimages.bin is missing (#251) * fix: don't fail when preimages.bin is missing * fix: don't open the preimages file when outside of transition --------- Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com> Co-authored-by: Ignacio Hagopian <jsign.uy@gmail.com> review changes remove replay-specific code
1058 lines
35 KiB
Go
1058 lines
35 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/common/math"
|
|
"github.com/ethereum/go-ethereum/core/state"
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
"github.com/ethereum/go-ethereum/params"
|
|
"github.com/ethereum/go-ethereum/trie"
|
|
trieUtils "github.com/ethereum/go-ethereum/trie/utils"
|
|
"github.com/holiman/uint256"
|
|
)
|
|
|
|
func opAdd(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
x, y := scope.Stack.pop(), scope.Stack.peek()
|
|
y.Add(&x, y)
|
|
return nil, nil
|
|
}
|
|
|
|
func opSub(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
x, y := scope.Stack.pop(), scope.Stack.peek()
|
|
y.Sub(&x, y)
|
|
return nil, nil
|
|
}
|
|
|
|
func opMul(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
x, y := scope.Stack.pop(), scope.Stack.peek()
|
|
y.Mul(&x, y)
|
|
return nil, nil
|
|
}
|
|
|
|
func opDiv(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
x, y := scope.Stack.pop(), scope.Stack.peek()
|
|
y.Div(&x, y)
|
|
return nil, nil
|
|
}
|
|
|
|
func opSdiv(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
x, y := scope.Stack.pop(), scope.Stack.peek()
|
|
y.SDiv(&x, y)
|
|
return nil, nil
|
|
}
|
|
|
|
func opMod(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
x, y := scope.Stack.pop(), scope.Stack.peek()
|
|
y.Mod(&x, y)
|
|
return nil, nil
|
|
}
|
|
|
|
func opSmod(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
x, y := scope.Stack.pop(), scope.Stack.peek()
|
|
y.SMod(&x, y)
|
|
return nil, nil
|
|
}
|
|
|
|
func opExp(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
base, exponent := scope.Stack.pop(), scope.Stack.peek()
|
|
exponent.Exp(&base, exponent)
|
|
return nil, nil
|
|
}
|
|
|
|
func opSignExtend(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
back, num := scope.Stack.pop(), scope.Stack.peek()
|
|
num.ExtendSign(num, &back)
|
|
return nil, nil
|
|
}
|
|
|
|
func opNot(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
x := scope.Stack.peek()
|
|
x.Not(x)
|
|
return nil, nil
|
|
}
|
|
|
|
func opLt(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
x, y := scope.Stack.pop(), scope.Stack.peek()
|
|
if x.Lt(y) {
|
|
y.SetOne()
|
|
} else {
|
|
y.Clear()
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func opGt(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
x, y := scope.Stack.pop(), scope.Stack.peek()
|
|
if x.Gt(y) {
|
|
y.SetOne()
|
|
} else {
|
|
y.Clear()
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func opSlt(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
x, y := scope.Stack.pop(), scope.Stack.peek()
|
|
if x.Slt(y) {
|
|
y.SetOne()
|
|
} else {
|
|
y.Clear()
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func opSgt(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
x, y := scope.Stack.pop(), scope.Stack.peek()
|
|
if x.Sgt(y) {
|
|
y.SetOne()
|
|
} else {
|
|
y.Clear()
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func opEq(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
x, y := scope.Stack.pop(), scope.Stack.peek()
|
|
if x.Eq(y) {
|
|
y.SetOne()
|
|
} else {
|
|
y.Clear()
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func opIszero(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
x := scope.Stack.peek()
|
|
if x.IsZero() {
|
|
x.SetOne()
|
|
} else {
|
|
x.Clear()
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func opAnd(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
x, y := scope.Stack.pop(), scope.Stack.peek()
|
|
y.And(&x, y)
|
|
return nil, nil
|
|
}
|
|
|
|
func opOr(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
x, y := scope.Stack.pop(), scope.Stack.peek()
|
|
y.Or(&x, y)
|
|
return nil, nil
|
|
}
|
|
|
|
func opXor(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
x, y := scope.Stack.pop(), scope.Stack.peek()
|
|
y.Xor(&x, y)
|
|
return nil, nil
|
|
}
|
|
|
|
func opByte(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
th, val := scope.Stack.pop(), scope.Stack.peek()
|
|
val.Byte(&th)
|
|
return nil, nil
|
|
}
|
|
|
|
func opAddmod(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
x, y, z := scope.Stack.pop(), scope.Stack.pop(), scope.Stack.peek()
|
|
if z.IsZero() {
|
|
z.Clear()
|
|
} else {
|
|
z.AddMod(&x, &y, z)
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func opMulmod(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
x, y, z := scope.Stack.pop(), scope.Stack.pop(), scope.Stack.peek()
|
|
z.MulMod(&x, &y, z)
|
|
return nil, nil
|
|
}
|
|
|
|
// opSHL implements Shift Left
|
|
// The SHL instruction (shift left) pops 2 values from the stack, first arg1 and then arg2,
|
|
// and pushes on the stack arg2 shifted to the left by arg1 number of bits.
|
|
func opSHL(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
// Note, second operand is left in the stack; accumulate result into it, and no need to push it afterwards
|
|
shift, value := scope.Stack.pop(), scope.Stack.peek()
|
|
if shift.LtUint64(256) {
|
|
value.Lsh(value, uint(shift.Uint64()))
|
|
} else {
|
|
value.Clear()
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
// opSHR implements Logical Shift Right
|
|
// The SHR instruction (logical shift right) pops 2 values from the stack, first arg1 and then arg2,
|
|
// and pushes on the stack arg2 shifted to the right by arg1 number of bits with zero fill.
|
|
func opSHR(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
// Note, second operand is left in the stack; accumulate result into it, and no need to push it afterwards
|
|
shift, value := scope.Stack.pop(), scope.Stack.peek()
|
|
if shift.LtUint64(256) {
|
|
value.Rsh(value, uint(shift.Uint64()))
|
|
} else {
|
|
value.Clear()
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
// opSAR implements Arithmetic Shift Right
|
|
// The SAR instruction (arithmetic shift right) pops 2 values from the stack, first arg1 and then arg2,
|
|
// and pushes on the stack arg2 shifted to the right by arg1 number of bits with sign extension.
|
|
func opSAR(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
shift, value := scope.Stack.pop(), scope.Stack.peek()
|
|
if shift.GtUint64(256) {
|
|
if value.Sign() >= 0 {
|
|
value.Clear()
|
|
} else {
|
|
// Max negative shift: all bits set
|
|
value.SetAllOne()
|
|
}
|
|
return nil, nil
|
|
}
|
|
n := uint(shift.Uint64())
|
|
value.SRsh(value, n)
|
|
return nil, nil
|
|
}
|
|
|
|
func opKeccak256(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
offset, size := scope.Stack.pop(), scope.Stack.peek()
|
|
data := scope.Memory.GetPtr(int64(offset.Uint64()), int64(size.Uint64()))
|
|
|
|
if interpreter.hasher == nil {
|
|
interpreter.hasher = crypto.NewKeccakState()
|
|
} else {
|
|
interpreter.hasher.Reset()
|
|
}
|
|
interpreter.hasher.Write(data)
|
|
interpreter.hasher.Read(interpreter.hasherBuf[:])
|
|
|
|
evm := interpreter.evm
|
|
if evm.Config.EnablePreimageRecording {
|
|
evm.StateDB.AddPreimage(interpreter.hasherBuf, data)
|
|
}
|
|
|
|
size.SetBytes(interpreter.hasherBuf[:])
|
|
return nil, nil
|
|
}
|
|
func opAddress(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
scope.Stack.push(new(uint256.Int).SetBytes(scope.Contract.Address().Bytes()))
|
|
return nil, nil
|
|
}
|
|
|
|
func opBalance(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
slot := scope.Stack.peek()
|
|
address := common.Address(slot.Bytes20())
|
|
slot.SetFromBig(interpreter.evm.StateDB.GetBalance(address))
|
|
return nil, nil
|
|
}
|
|
|
|
func opOrigin(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
scope.Stack.push(new(uint256.Int).SetBytes(interpreter.evm.Origin.Bytes()))
|
|
return nil, nil
|
|
}
|
|
func opCaller(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
scope.Stack.push(new(uint256.Int).SetBytes(scope.Contract.Caller().Bytes()))
|
|
return nil, nil
|
|
}
|
|
|
|
func opCallValue(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
v, _ := uint256.FromBig(scope.Contract.value)
|
|
scope.Stack.push(v)
|
|
return nil, nil
|
|
}
|
|
|
|
func opCallDataLoad(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
x := scope.Stack.peek()
|
|
if offset, overflow := x.Uint64WithOverflow(); !overflow {
|
|
data := getData(scope.Contract.Input, offset, 32)
|
|
x.SetBytes(data)
|
|
} else {
|
|
x.Clear()
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func opCallDataSize(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
scope.Stack.push(new(uint256.Int).SetUint64(uint64(len(scope.Contract.Input))))
|
|
return nil, nil
|
|
}
|
|
|
|
func opCallDataCopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
var (
|
|
memOffset = scope.Stack.pop()
|
|
dataOffset = scope.Stack.pop()
|
|
length = scope.Stack.pop()
|
|
)
|
|
dataOffset64, overflow := dataOffset.Uint64WithOverflow()
|
|
if overflow {
|
|
dataOffset64 = 0xffffffffffffffff
|
|
}
|
|
// These values are checked for overflow during gas cost calculation
|
|
memOffset64 := memOffset.Uint64()
|
|
length64 := length.Uint64()
|
|
scope.Memory.Set(memOffset64, length64, getData(scope.Contract.Input, dataOffset64, length64))
|
|
|
|
return nil, nil
|
|
}
|
|
|
|
func opReturnDataSize(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
scope.Stack.push(new(uint256.Int).SetUint64(uint64(len(interpreter.returnData))))
|
|
return nil, nil
|
|
}
|
|
|
|
func opReturnDataCopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
var (
|
|
memOffset = scope.Stack.pop()
|
|
dataOffset = scope.Stack.pop()
|
|
length = scope.Stack.pop()
|
|
)
|
|
|
|
offset64, overflow := dataOffset.Uint64WithOverflow()
|
|
if overflow {
|
|
return nil, ErrReturnDataOutOfBounds
|
|
}
|
|
// we can reuse dataOffset now (aliasing it for clarity)
|
|
var end = dataOffset
|
|
end.Add(&dataOffset, &length)
|
|
end64, overflow := end.Uint64WithOverflow()
|
|
if overflow || uint64(len(interpreter.returnData)) < end64 {
|
|
return nil, ErrReturnDataOutOfBounds
|
|
}
|
|
scope.Memory.Set(memOffset.Uint64(), length.Uint64(), interpreter.returnData[offset64:end64])
|
|
return nil, nil
|
|
}
|
|
|
|
func opExtCodeSize(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
slot := scope.Stack.peek()
|
|
cs := uint64(interpreter.evm.StateDB.GetCodeSize(slot.Bytes20()))
|
|
if interpreter.evm.chainRules.IsCancun {
|
|
index := trieUtils.GetTreeKeyCodeSize(slot.Bytes())
|
|
statelessGas := interpreter.evm.Accesses.TouchAddressOnReadAndComputeGas(index)
|
|
scope.Contract.UseGas(statelessGas)
|
|
}
|
|
slot.SetUint64(cs)
|
|
return nil, nil
|
|
}
|
|
|
|
func opCodeSize(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
l := new(uint256.Int)
|
|
l.SetUint64(uint64(len(scope.Contract.Code)))
|
|
scope.Stack.push(l)
|
|
return nil, nil
|
|
}
|
|
|
|
func opCodeCopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
var (
|
|
memOffset = scope.Stack.pop()
|
|
codeOffset = scope.Stack.pop()
|
|
length = scope.Stack.pop()
|
|
)
|
|
uint64CodeOffset, overflow := codeOffset.Uint64WithOverflow()
|
|
if overflow {
|
|
uint64CodeOffset = 0xffffffffffffffff
|
|
}
|
|
|
|
paddedCodeCopy, copyOffset, nonPaddedCopyLength := getDataAndAdjustedBounds(scope.Contract.Code, uint64CodeOffset, length.Uint64())
|
|
if interpreter.evm.chainRules.IsCancun {
|
|
scope.Contract.UseGas(touchEachChunksOnReadAndChargeGas(copyOffset, nonPaddedCopyLength, scope.Contract, scope.Contract.Code, interpreter.evm.Accesses, scope.Contract.IsDeployment))
|
|
}
|
|
scope.Memory.Set(memOffset.Uint64(), uint64(len(paddedCodeCopy)), paddedCodeCopy)
|
|
return nil, nil
|
|
}
|
|
|
|
// touchChunkOnReadAndChargeGas is a helper function to touch every chunk in a code range and charge witness gas costs
|
|
func touchChunkOnReadAndChargeGas(chunks trie.ChunkedCode, offset uint64, evals [][]byte, code []byte, accesses *state.AccessWitness, deployment bool) uint64 {
|
|
// note that in the case where the executed code is outside the range of
|
|
// the contract code but touches the last leaf with contract code in it,
|
|
// we don't include the last leaf of code in the AccessWitness. The
|
|
// reason that we do not need the last leaf is the account's code size
|
|
// is already in the AccessWitness so a stateless verifier can see that
|
|
// the code from the last leaf is not needed.
|
|
if code != nil && offset > uint64(len(code)) {
|
|
return 0
|
|
}
|
|
var (
|
|
chunknr = offset / 31
|
|
statelessGasCharged uint64
|
|
)
|
|
|
|
// Build the chunk address from the evaluated address of its whole group
|
|
var index [32]byte
|
|
copy(index[:], evals[chunknr/256])
|
|
index[31] = byte((128 + chunknr) % 256)
|
|
|
|
var overflow bool
|
|
statelessGasCharged, overflow = math.SafeAdd(statelessGasCharged, accesses.TouchAddressOnReadAndComputeGas(index[:]))
|
|
if overflow {
|
|
panic("overflow when adding gas")
|
|
}
|
|
|
|
return statelessGasCharged
|
|
}
|
|
|
|
// touchEachChunksOnReadAndChargeGas is a helper function to touch every chunk in a code range and charge witness gas costs
|
|
func touchEachChunksOnReadAndChargeGas(offset, size uint64, contract *Contract, code []byte, accesses *state.AccessWitness, deployment bool) uint64 {
|
|
// note that in the case where the copied code is outside the range of the
|
|
// contract code but touches the last leaf with contract code in it,
|
|
// we don't include the last leaf of code in the AccessWitness. The
|
|
// reason that we do not need the last leaf is the account's code size
|
|
// is already in the AccessWitness so a stateless verifier can see that
|
|
// the code from the last leaf is not needed.
|
|
if len(code) == 0 && size == 0 || offset > uint64(len(code)) {
|
|
return 0
|
|
}
|
|
var (
|
|
statelessGasCharged uint64
|
|
endOffset uint64
|
|
)
|
|
if code != nil && offset+size > uint64(len(code)) {
|
|
endOffset = uint64(len(code))
|
|
} else {
|
|
endOffset = offset + size
|
|
}
|
|
|
|
// endOffset - 1 since if the end offset is aligned on a chunk boundary,
|
|
// the last chunk should not be included.
|
|
for i := offset / 31; i <= (endOffset-1)/31; i++ {
|
|
// only charge for+cache the chunk if it isn't already present
|
|
if !accesses.HasCodeChunk(contract.Address().Bytes(), i) {
|
|
index := trieUtils.GetTreeKeyCodeChunkWithEvaluatedAddress(contract.AddressPoint(), uint256.NewInt(i))
|
|
|
|
var overflow bool
|
|
statelessGasCharged, overflow = math.SafeAdd(statelessGasCharged, accesses.TouchAddressOnReadAndComputeGas(index))
|
|
if overflow {
|
|
panic("overflow when adding gas")
|
|
}
|
|
|
|
accesses.SetCachedCodeChunk(contract.Address().Bytes(), i)
|
|
}
|
|
}
|
|
|
|
return statelessGasCharged
|
|
}
|
|
|
|
func opExtCodeCopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
var (
|
|
stack = scope.Stack
|
|
a = stack.pop()
|
|
memOffset = stack.pop()
|
|
codeOffset = stack.pop()
|
|
length = stack.pop()
|
|
)
|
|
uint64CodeOffset, overflow := codeOffset.Uint64WithOverflow()
|
|
if overflow {
|
|
uint64CodeOffset = 0xffffffffffffffff
|
|
}
|
|
addr := common.Address(a.Bytes20())
|
|
if interpreter.evm.chainRules.IsCancun {
|
|
code := interpreter.evm.StateDB.GetCode(addr)
|
|
contract := &Contract{
|
|
Code: code,
|
|
Chunks: trie.ChunkedCode(code),
|
|
self: AccountRef(addr),
|
|
}
|
|
paddedCodeCopy, copyOffset, nonPaddedCopyLength := getDataAndAdjustedBounds(code, uint64CodeOffset, length.Uint64())
|
|
touchEachChunksOnReadAndChargeGas(copyOffset, nonPaddedCopyLength, contract, code, interpreter.evm.Accesses, false)
|
|
scope.Memory.Set(memOffset.Uint64(), length.Uint64(), paddedCodeCopy)
|
|
} else {
|
|
codeCopy := getData(interpreter.evm.StateDB.GetCode(addr), uint64CodeOffset, length.Uint64())
|
|
scope.Memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy)
|
|
}
|
|
|
|
return nil, nil
|
|
}
|
|
|
|
// opExtCodeHash returns the code hash of a specified account.
|
|
// There are several cases when the function is called, while we can relay everything
|
|
// to `state.GetCodeHash` function to ensure the correctness.
|
|
//
|
|
// 1. Caller tries to get the code hash of a normal contract account, state
|
|
// should return the relative code hash and set it as the result.
|
|
//
|
|
// 2. Caller tries to get the code hash of a non-existent account, state should
|
|
// return common.Hash{} and zero will be set as the result.
|
|
//
|
|
// 3. Caller tries to get the code hash for an account without contract code, state
|
|
// should return emptyCodeHash(0xc5d246...) as the result.
|
|
//
|
|
// 4. Caller tries to get the code hash of a precompiled account, the result should be
|
|
// zero or emptyCodeHash.
|
|
//
|
|
// It is worth noting that in order to avoid unnecessary create and clean, all precompile
|
|
// accounts on mainnet have been transferred 1 wei, so the return here should be
|
|
// emptyCodeHash. If the precompile account is not transferred any amount on a private or
|
|
// customized chain, the return value will be zero.
|
|
//
|
|
// 5. Caller tries to get the code hash for an account which is marked as self-destructed
|
|
// in the current transaction, the code hash of this account should be returned.
|
|
//
|
|
// 6. Caller tries to get the code hash for an account which is marked as deleted, this
|
|
// account should be regarded as a non-existent account and zero should be returned.
|
|
func opExtCodeHash(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
slot := scope.Stack.peek()
|
|
address := common.Address(slot.Bytes20())
|
|
if interpreter.evm.StateDB.Empty(address) {
|
|
slot.Clear()
|
|
} else {
|
|
slot.SetBytes(interpreter.evm.StateDB.GetCodeHash(address).Bytes())
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func opGasprice(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
v, _ := uint256.FromBig(interpreter.evm.GasPrice)
|
|
scope.Stack.push(v)
|
|
return nil, nil
|
|
}
|
|
|
|
func opBlockhash(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
num := scope.Stack.peek()
|
|
num64, overflow := num.Uint64WithOverflow()
|
|
if overflow {
|
|
num.Clear()
|
|
return nil, nil
|
|
}
|
|
var upper, lower uint64
|
|
upper = interpreter.evm.Context.BlockNumber.Uint64()
|
|
if upper < 257 {
|
|
lower = 0
|
|
} else {
|
|
lower = upper - 256
|
|
}
|
|
if num64 >= lower && num64 < upper {
|
|
num.SetBytes(interpreter.evm.Context.GetHash(num64).Bytes())
|
|
} else {
|
|
num.Clear()
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func opCoinbase(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
scope.Stack.push(new(uint256.Int).SetBytes(interpreter.evm.Context.Coinbase.Bytes()))
|
|
return nil, nil
|
|
}
|
|
|
|
func opTimestamp(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
scope.Stack.push(new(uint256.Int).SetUint64(interpreter.evm.Context.Time))
|
|
return nil, nil
|
|
}
|
|
|
|
func opNumber(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
v, _ := uint256.FromBig(interpreter.evm.Context.BlockNumber)
|
|
scope.Stack.push(v)
|
|
return nil, nil
|
|
}
|
|
|
|
func opDifficulty(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
v, _ := uint256.FromBig(interpreter.evm.Context.Difficulty)
|
|
scope.Stack.push(v)
|
|
return nil, nil
|
|
}
|
|
|
|
func opRandom(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
v := new(uint256.Int).SetBytes(interpreter.evm.Context.Random.Bytes())
|
|
scope.Stack.push(v)
|
|
return nil, nil
|
|
}
|
|
|
|
func opGasLimit(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
scope.Stack.push(new(uint256.Int).SetUint64(interpreter.evm.Context.GasLimit))
|
|
return nil, nil
|
|
}
|
|
|
|
func opPop(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
scope.Stack.pop()
|
|
return nil, nil
|
|
}
|
|
|
|
func opMload(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
v := scope.Stack.peek()
|
|
offset := int64(v.Uint64())
|
|
v.SetBytes(scope.Memory.GetPtr(offset, 32))
|
|
return nil, nil
|
|
}
|
|
|
|
func opMstore(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
// pop value of the stack
|
|
mStart, val := scope.Stack.pop(), scope.Stack.pop()
|
|
scope.Memory.Set32(mStart.Uint64(), &val)
|
|
return nil, nil
|
|
}
|
|
|
|
func opMstore8(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
off, val := scope.Stack.pop(), scope.Stack.pop()
|
|
scope.Memory.store[off.Uint64()] = byte(val.Uint64())
|
|
return nil, nil
|
|
}
|
|
|
|
func opSload(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
loc := scope.Stack.peek()
|
|
hash := common.Hash(loc.Bytes32())
|
|
val := interpreter.evm.StateDB.GetState(scope.Contract.Address(), hash)
|
|
|
|
loc.SetBytes(val.Bytes())
|
|
return nil, nil
|
|
}
|
|
|
|
func opSstore(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
if interpreter.readOnly {
|
|
return nil, ErrWriteProtection
|
|
}
|
|
loc := scope.Stack.pop()
|
|
val := scope.Stack.pop()
|
|
interpreter.evm.StateDB.SetState(scope.Contract.Address(), loc.Bytes32(), val.Bytes32())
|
|
return nil, nil
|
|
}
|
|
|
|
func opJump(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
if interpreter.evm.abort.Load() {
|
|
return nil, errStopToken
|
|
}
|
|
pos := scope.Stack.pop()
|
|
if !scope.Contract.validJumpdest(&pos) {
|
|
return nil, ErrInvalidJump
|
|
}
|
|
*pc = pos.Uint64() - 1 // pc will be increased by the interpreter loop
|
|
return nil, nil
|
|
}
|
|
|
|
func opJumpi(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
if interpreter.evm.abort.Load() {
|
|
return nil, errStopToken
|
|
}
|
|
pos, cond := scope.Stack.pop(), scope.Stack.pop()
|
|
if !cond.IsZero() {
|
|
if !scope.Contract.validJumpdest(&pos) {
|
|
return nil, ErrInvalidJump
|
|
}
|
|
*pc = pos.Uint64() - 1 // pc will be increased by the interpreter loop
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func opJumpdest(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func opPc(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
scope.Stack.push(new(uint256.Int).SetUint64(*pc))
|
|
return nil, nil
|
|
}
|
|
|
|
func opMsize(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
scope.Stack.push(new(uint256.Int).SetUint64(uint64(scope.Memory.Len())))
|
|
return nil, nil
|
|
}
|
|
|
|
func opGas(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
scope.Stack.push(new(uint256.Int).SetUint64(scope.Contract.Gas))
|
|
return nil, nil
|
|
}
|
|
|
|
func opCreate(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
if interpreter.readOnly {
|
|
return nil, ErrWriteProtection
|
|
}
|
|
var (
|
|
value = scope.Stack.pop()
|
|
offset, size = scope.Stack.pop(), scope.Stack.pop()
|
|
input = scope.Memory.GetCopy(int64(offset.Uint64()), int64(size.Uint64()))
|
|
gas = scope.Contract.Gas
|
|
)
|
|
if interpreter.evm.chainRules.IsCancun {
|
|
contractAddress := crypto.CreateAddress(scope.Contract.Address(), interpreter.evm.StateDB.GetNonce(scope.Contract.Address()))
|
|
statelessGas := interpreter.evm.Accesses.TouchAndChargeContractCreateInit(contractAddress.Bytes()[:], value.Sign() != 0)
|
|
if !tryConsumeGas(&gas, statelessGas) {
|
|
return nil, ErrExecutionReverted
|
|
}
|
|
}
|
|
if interpreter.evm.chainRules.IsEIP150 {
|
|
gas -= gas / 64
|
|
}
|
|
// reuse size int for stackvalue
|
|
stackvalue := size
|
|
|
|
scope.Contract.UseGas(gas)
|
|
//TODO: use uint256.Int instead of converting with toBig()
|
|
var bigVal = big0
|
|
if !value.IsZero() {
|
|
bigVal = value.ToBig()
|
|
}
|
|
|
|
res, addr, returnGas, suberr := interpreter.evm.Create(scope.Contract, input, gas, bigVal)
|
|
// Push item on the stack based on the returned error. If the ruleset is
|
|
// homestead we must check for CodeStoreOutOfGasError (homestead only
|
|
// rule) and treat as an error, if the ruleset is frontier we must
|
|
// ignore this error and pretend the operation was successful.
|
|
if interpreter.evm.chainRules.IsHomestead && suberr == ErrCodeStoreOutOfGas {
|
|
stackvalue.Clear()
|
|
} else if suberr != nil && suberr != ErrCodeStoreOutOfGas {
|
|
stackvalue.Clear()
|
|
} else {
|
|
stackvalue.SetBytes(addr.Bytes())
|
|
}
|
|
scope.Stack.push(&stackvalue)
|
|
scope.Contract.Gas += returnGas
|
|
|
|
if suberr == ErrExecutionReverted {
|
|
interpreter.returnData = res // set REVERT data to return data buffer
|
|
return res, nil
|
|
}
|
|
interpreter.returnData = nil // clear dirty return data buffer
|
|
return nil, nil
|
|
}
|
|
|
|
func opCreate2(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
if interpreter.readOnly {
|
|
return nil, ErrWriteProtection
|
|
}
|
|
var (
|
|
endowment = scope.Stack.pop()
|
|
offset, size = scope.Stack.pop(), scope.Stack.pop()
|
|
salt = scope.Stack.pop()
|
|
input = scope.Memory.GetCopy(int64(offset.Uint64()), int64(size.Uint64()))
|
|
gas = scope.Contract.Gas
|
|
)
|
|
if interpreter.evm.chainRules.IsCancun {
|
|
codeAndHash := &codeAndHash{code: input}
|
|
contractAddress := crypto.CreateAddress2(scope.Contract.Address(), salt.Bytes32(), codeAndHash.Hash().Bytes())
|
|
statelessGas := interpreter.evm.Accesses.TouchAndChargeContractCreateInit(contractAddress.Bytes()[:], endowment.Sign() != 0)
|
|
if !tryConsumeGas(&gas, statelessGas) {
|
|
return nil, ErrExecutionReverted
|
|
}
|
|
}
|
|
|
|
// Apply EIP150
|
|
gas -= gas / 64
|
|
scope.Contract.UseGas(gas)
|
|
// reuse size int for stackvalue
|
|
stackvalue := size
|
|
//TODO: use uint256.Int instead of converting with toBig()
|
|
bigEndowment := big0
|
|
if !endowment.IsZero() {
|
|
bigEndowment = endowment.ToBig()
|
|
}
|
|
res, addr, returnGas, suberr := interpreter.evm.Create2(scope.Contract, input, gas,
|
|
bigEndowment, &salt)
|
|
// Push item on the stack based on the returned error.
|
|
if suberr != nil {
|
|
stackvalue.Clear()
|
|
} else {
|
|
stackvalue.SetBytes(addr.Bytes())
|
|
}
|
|
scope.Stack.push(&stackvalue)
|
|
scope.Contract.Gas += returnGas
|
|
|
|
if suberr == ErrExecutionReverted {
|
|
interpreter.returnData = res // set REVERT data to return data buffer
|
|
return res, nil
|
|
}
|
|
interpreter.returnData = nil // clear dirty return data buffer
|
|
return nil, nil
|
|
}
|
|
|
|
func opCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
stack := scope.Stack
|
|
// Pop gas. The actual gas in interpreter.evm.callGasTemp.
|
|
// We can use this as a temporary value
|
|
temp := stack.pop()
|
|
gas := interpreter.evm.callGasTemp
|
|
// Pop other call parameters.
|
|
addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
|
|
toAddr := common.Address(addr.Bytes20())
|
|
// Get the arguments from the memory.
|
|
args := scope.Memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64()))
|
|
|
|
if interpreter.readOnly && !value.IsZero() {
|
|
return nil, ErrWriteProtection
|
|
}
|
|
var bigVal = big0
|
|
//TODO: use uint256.Int instead of converting with toBig()
|
|
// By using big0 here, we save an alloc for the most common case (non-ether-transferring contract calls),
|
|
// but it would make more sense to extend the usage of uint256.Int
|
|
if !value.IsZero() {
|
|
gas += params.CallStipend
|
|
bigVal = value.ToBig()
|
|
}
|
|
|
|
ret, returnGas, err := interpreter.evm.Call(scope.Contract, toAddr, args, gas, bigVal)
|
|
|
|
if err != nil {
|
|
temp.Clear()
|
|
} else {
|
|
temp.SetOne()
|
|
}
|
|
stack.push(&temp)
|
|
if err == nil || err == ErrExecutionReverted {
|
|
scope.Memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
|
|
}
|
|
scope.Contract.Gas += returnGas
|
|
|
|
interpreter.returnData = ret
|
|
return ret, nil
|
|
}
|
|
|
|
func opCallCode(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
// Pop gas. The actual gas is in interpreter.evm.callGasTemp.
|
|
stack := scope.Stack
|
|
// We use it as a temporary value
|
|
temp := stack.pop()
|
|
gas := interpreter.evm.callGasTemp
|
|
// Pop other call parameters.
|
|
addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
|
|
toAddr := common.Address(addr.Bytes20())
|
|
// Get arguments from the memory.
|
|
args := scope.Memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64()))
|
|
|
|
//TODO: use uint256.Int instead of converting with toBig()
|
|
var bigVal = big0
|
|
if !value.IsZero() {
|
|
gas += params.CallStipend
|
|
bigVal = value.ToBig()
|
|
}
|
|
|
|
ret, returnGas, err := interpreter.evm.CallCode(scope.Contract, toAddr, args, gas, bigVal)
|
|
if err != nil {
|
|
temp.Clear()
|
|
} else {
|
|
temp.SetOne()
|
|
}
|
|
stack.push(&temp)
|
|
if err == nil || err == ErrExecutionReverted {
|
|
scope.Memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
|
|
}
|
|
scope.Contract.Gas += returnGas
|
|
|
|
interpreter.returnData = ret
|
|
return ret, nil
|
|
}
|
|
|
|
func opDelegateCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
stack := scope.Stack
|
|
// Pop gas. The actual gas is in interpreter.evm.callGasTemp.
|
|
// We use it as a temporary value
|
|
temp := stack.pop()
|
|
gas := interpreter.evm.callGasTemp
|
|
// Pop other call parameters.
|
|
addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
|
|
toAddr := common.Address(addr.Bytes20())
|
|
// Get arguments from the memory.
|
|
args := scope.Memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64()))
|
|
|
|
ret, returnGas, err := interpreter.evm.DelegateCall(scope.Contract, toAddr, args, gas)
|
|
if err != nil {
|
|
temp.Clear()
|
|
} else {
|
|
temp.SetOne()
|
|
}
|
|
stack.push(&temp)
|
|
if err == nil || err == ErrExecutionReverted {
|
|
scope.Memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
|
|
}
|
|
scope.Contract.Gas += returnGas
|
|
|
|
interpreter.returnData = ret
|
|
return ret, nil
|
|
}
|
|
|
|
func opStaticCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
// Pop gas. The actual gas is in interpreter.evm.callGasTemp.
|
|
stack := scope.Stack
|
|
// We use it as a temporary value
|
|
temp := stack.pop()
|
|
gas := interpreter.evm.callGasTemp
|
|
// Pop other call parameters.
|
|
addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
|
|
toAddr := common.Address(addr.Bytes20())
|
|
// Get arguments from the memory.
|
|
args := scope.Memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64()))
|
|
|
|
ret, returnGas, err := interpreter.evm.StaticCall(scope.Contract, toAddr, args, gas)
|
|
if err != nil {
|
|
temp.Clear()
|
|
} else {
|
|
temp.SetOne()
|
|
}
|
|
stack.push(&temp)
|
|
if err == nil || err == ErrExecutionReverted {
|
|
scope.Memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
|
|
}
|
|
scope.Contract.Gas += returnGas
|
|
|
|
interpreter.returnData = ret
|
|
return ret, nil
|
|
}
|
|
|
|
func opReturn(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
offset, size := scope.Stack.pop(), scope.Stack.pop()
|
|
ret := scope.Memory.GetPtr(int64(offset.Uint64()), int64(size.Uint64()))
|
|
|
|
return ret, errStopToken
|
|
}
|
|
|
|
func opRevert(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
offset, size := scope.Stack.pop(), scope.Stack.pop()
|
|
ret := scope.Memory.GetPtr(int64(offset.Uint64()), int64(size.Uint64()))
|
|
|
|
interpreter.returnData = ret
|
|
return ret, ErrExecutionReverted
|
|
}
|
|
|
|
func opUndefined(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
return nil, &ErrInvalidOpCode{opcode: OpCode(scope.Contract.Code[*pc])}
|
|
}
|
|
|
|
func opStop(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
return nil, errStopToken
|
|
}
|
|
|
|
func opSelfdestruct(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
if interpreter.readOnly {
|
|
return nil, ErrWriteProtection
|
|
}
|
|
beneficiary := scope.Stack.pop()
|
|
balance := interpreter.evm.StateDB.GetBalance(scope.Contract.Address())
|
|
interpreter.evm.StateDB.AddBalance(beneficiary.Bytes20(), balance)
|
|
interpreter.evm.StateDB.SelfDestruct(scope.Contract.Address())
|
|
if tracer := interpreter.evm.Config.Tracer; tracer != nil {
|
|
tracer.CaptureEnter(SELFDESTRUCT, scope.Contract.Address(), beneficiary.Bytes20(), []byte{}, 0, balance)
|
|
tracer.CaptureExit([]byte{}, 0, nil)
|
|
}
|
|
return nil, errStopToken
|
|
}
|
|
|
|
func opSelfdestruct6780(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
if interpreter.readOnly {
|
|
return nil, ErrWriteProtection
|
|
}
|
|
beneficiary := scope.Stack.pop()
|
|
balance := interpreter.evm.StateDB.GetBalance(scope.Contract.Address())
|
|
interpreter.evm.StateDB.SubBalance(scope.Contract.Address(), balance)
|
|
interpreter.evm.StateDB.AddBalance(beneficiary.Bytes20(), balance)
|
|
interpreter.evm.StateDB.Selfdestruct6780(scope.Contract.Address())
|
|
if tracer := interpreter.evm.Config.Tracer; tracer != nil {
|
|
tracer.CaptureEnter(SELFDESTRUCT, scope.Contract.Address(), beneficiary.Bytes20(), []byte{}, 0, balance)
|
|
tracer.CaptureExit([]byte{}, 0, nil)
|
|
}
|
|
return nil, errStopToken
|
|
}
|
|
|
|
// following functions are used by the instruction jump table
|
|
|
|
// make log instruction function
|
|
func makeLog(size int) executionFunc {
|
|
return func(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
if interpreter.readOnly {
|
|
return nil, ErrWriteProtection
|
|
}
|
|
topics := make([]common.Hash, size)
|
|
stack := scope.Stack
|
|
mStart, mSize := stack.pop(), stack.pop()
|
|
for i := 0; i < size; i++ {
|
|
addr := stack.pop()
|
|
topics[i] = addr.Bytes32()
|
|
}
|
|
|
|
d := scope.Memory.GetCopy(int64(mStart.Uint64()), int64(mSize.Uint64()))
|
|
interpreter.evm.StateDB.AddLog(&types.Log{
|
|
Address: scope.Contract.Address(),
|
|
Topics: topics,
|
|
Data: d,
|
|
// This is a non-consensus field, but assigned here because
|
|
// core/state doesn't know the current block number.
|
|
BlockNumber: interpreter.evm.Context.BlockNumber.Uint64(),
|
|
})
|
|
|
|
return nil, nil
|
|
}
|
|
}
|
|
|
|
// opPush1 is a specialized version of pushN
|
|
func opPush1(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
var (
|
|
codeLen = uint64(len(scope.Contract.Code))
|
|
integer = new(uint256.Int)
|
|
)
|
|
*pc += 1
|
|
if *pc < codeLen {
|
|
scope.Stack.push(integer.SetUint64(uint64(scope.Contract.Code[*pc])))
|
|
|
|
if interpreter.evm.chainRules.IsCancun && *pc%31 == 0 {
|
|
// touch next chunk if PUSH1 is at the boundary. if so, *pc has
|
|
// advanced past this boundary.
|
|
statelessGas := touchEachChunksOnReadAndChargeGas(*pc+1, uint64(1), scope.Contract, scope.Contract.Code, interpreter.evm.Accesses, scope.Contract.IsDeployment)
|
|
scope.Contract.UseGas(statelessGas)
|
|
}
|
|
} else {
|
|
scope.Stack.push(integer.Clear())
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
// make push instruction function
|
|
func makePush(size uint64, pushByteSize int) executionFunc {
|
|
return func(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
codeLen := len(scope.Contract.Code)
|
|
|
|
startMin := codeLen
|
|
if int(*pc+1) < startMin {
|
|
startMin = int(*pc + 1)
|
|
}
|
|
|
|
endMin := codeLen
|
|
if startMin+pushByteSize < endMin {
|
|
endMin = startMin + pushByteSize
|
|
}
|
|
|
|
if interpreter.evm.chainRules.IsCancun {
|
|
statelessGas := touchEachChunksOnReadAndChargeGas(uint64(startMin), uint64(pushByteSize), scope.Contract, scope.Contract.Code, interpreter.evm.Accesses, scope.Contract.IsDeployment)
|
|
scope.Contract.UseGas(statelessGas)
|
|
}
|
|
|
|
integer := new(uint256.Int)
|
|
scope.Stack.push(integer.SetBytes(common.RightPadBytes(
|
|
scope.Contract.Code[startMin:endMin], pushByteSize)))
|
|
|
|
*pc += size
|
|
return nil, nil
|
|
}
|
|
}
|
|
|
|
// make dup instruction function
|
|
func makeDup(size int64) executionFunc {
|
|
return func(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
scope.Stack.dup(int(size))
|
|
return nil, nil
|
|
}
|
|
}
|
|
|
|
// make swap instruction function
|
|
func makeSwap(size int64) executionFunc {
|
|
// switch n + 1 otherwise n would be swapped with n
|
|
size++
|
|
return func(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
|
scope.Stack.swap(int(size))
|
|
return nil, nil
|
|
}
|
|
}
|