2022-01-31 09:51:17 +01:00
|
|
|
// Copyright 2022 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 native
|
|
|
|
|
|
|
|
import (
|
2022-10-06 08:05:43 +08:00
|
|
|
"bytes"
|
2022-01-31 09:51:17 +01:00
|
|
|
"encoding/json"
|
|
|
|
"math/big"
|
|
|
|
"sync/atomic"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2024-03-22 18:53:53 +01:00
|
|
|
"github.com/ethereum/go-ethereum/core/tracing"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2022-01-31 09:51:17 +01:00
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/eth/tracers"
|
2024-03-22 18:53:53 +01:00
|
|
|
"github.com/ethereum/go-ethereum/eth/tracers/internal"
|
2023-07-13 05:44:42 -07:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2022-01-31 09:51:17 +01:00
|
|
|
)
|
|
|
|
|
2022-09-26 18:35:44 +02:00
|
|
|
//go:generate go run github.com/fjl/gencodec -type account -field-override accountMarshaling -out gen_account_json.go
|
|
|
|
|
2022-01-31 09:51:17 +01:00
|
|
|
func init() {
|
2023-01-10 09:11:53 -05:00
|
|
|
tracers.DefaultDirectory.Register("prestateTracer", newPrestateTracer, false)
|
2022-01-31 09:51:17 +01:00
|
|
|
}
|
|
|
|
|
2024-03-22 18:53:53 +01:00
|
|
|
type stateMap = map[common.Address]*account
|
2022-10-06 08:05:43 +08:00
|
|
|
|
2022-01-31 09:51:17 +01:00
|
|
|
type account struct {
|
2022-10-06 08:05:43 +08:00
|
|
|
Balance *big.Int `json:"balance,omitempty"`
|
|
|
|
Code []byte `json:"code,omitempty"`
|
2022-10-21 17:55:15 +02:00
|
|
|
Nonce uint64 `json:"nonce,omitempty"`
|
2022-10-06 08:05:43 +08:00
|
|
|
Storage map[common.Hash]common.Hash `json:"storage,omitempty"`
|
2024-03-22 18:53:53 +01:00
|
|
|
empty bool
|
2022-01-31 09:51:17 +01:00
|
|
|
}
|
|
|
|
|
2022-10-21 17:48:53 +08:00
|
|
|
func (a *account) exists() bool {
|
2022-12-13 08:32:29 -05:00
|
|
|
return a.Nonce > 0 || len(a.Code) > 0 || len(a.Storage) > 0 || (a.Balance != nil && a.Balance.Sign() != 0)
|
2022-10-21 17:48:53 +08:00
|
|
|
}
|
|
|
|
|
2022-09-26 18:35:44 +02:00
|
|
|
type accountMarshaling struct {
|
|
|
|
Balance *hexutil.Big
|
|
|
|
Code hexutil.Bytes
|
|
|
|
}
|
|
|
|
|
2022-01-31 09:51:17 +01:00
|
|
|
type prestateTracer struct {
|
2024-03-22 18:53:53 +01:00
|
|
|
env *tracing.VMContext
|
|
|
|
pre stateMap
|
|
|
|
post stateMap
|
2022-01-31 09:51:17 +01:00
|
|
|
to common.Address
|
2022-10-06 08:05:43 +08:00
|
|
|
config prestateTracerConfig
|
2023-04-05 00:34:52 +08:00
|
|
|
interrupt atomic.Bool // Atomic flag to signal execution interruption
|
|
|
|
reason error // Textual reason for the interruption
|
2022-10-06 08:05:43 +08:00
|
|
|
created map[common.Address]bool
|
|
|
|
deleted map[common.Address]bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type prestateTracerConfig struct {
|
2022-11-01 16:55:44 +03:30
|
|
|
DiffMode bool `json:"diffMode"` // If true, this tracer will return state modifications
|
2022-01-31 09:51:17 +01:00
|
|
|
}
|
|
|
|
|
2024-03-22 18:53:53 +01:00
|
|
|
func newPrestateTracer(ctx *tracers.Context, cfg json.RawMessage) (*tracers.Tracer, error) {
|
2022-10-06 08:05:43 +08:00
|
|
|
var config prestateTracerConfig
|
|
|
|
if cfg != nil {
|
|
|
|
if err := json.Unmarshal(cfg, &config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2024-03-22 18:53:53 +01:00
|
|
|
t := &prestateTracer{
|
|
|
|
pre: stateMap{},
|
|
|
|
post: stateMap{},
|
2022-10-06 08:05:43 +08:00
|
|
|
config: config,
|
|
|
|
created: make(map[common.Address]bool),
|
|
|
|
deleted: make(map[common.Address]bool),
|
2022-01-31 09:51:17 +01:00
|
|
|
}
|
2024-03-22 18:53:53 +01:00
|
|
|
return &tracers.Tracer{
|
|
|
|
Hooks: &tracing.Hooks{
|
|
|
|
OnTxStart: t.OnTxStart,
|
|
|
|
OnTxEnd: t.OnTxEnd,
|
|
|
|
OnOpcode: t.OnOpcode,
|
|
|
|
},
|
|
|
|
GetResult: t.GetResult,
|
|
|
|
Stop: t.Stop,
|
|
|
|
}, nil
|
2022-01-31 09:51:17 +01:00
|
|
|
}
|
|
|
|
|
2024-03-22 18:53:53 +01:00
|
|
|
// OnOpcode implements the EVMLogger interface to trace a single step of VM execution.
|
|
|
|
func (t *prestateTracer) OnOpcode(pc uint64, opcode byte, gas, cost uint64, scope tracing.OpContext, rData []byte, depth int, err error) {
|
2023-03-28 02:46:53 -07:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-04-05 00:34:52 +08:00
|
|
|
// Skip if tracing was interrupted
|
|
|
|
if t.interrupt.Load() {
|
|
|
|
return
|
|
|
|
}
|
2024-03-22 18:53:53 +01:00
|
|
|
op := vm.OpCode(opcode)
|
|
|
|
stackData := scope.StackData()
|
2022-01-31 09:51:17 +01:00
|
|
|
stackLen := len(stackData)
|
2024-03-22 18:53:53 +01:00
|
|
|
caller := scope.Address()
|
2022-01-31 09:51:17 +01:00
|
|
|
switch {
|
|
|
|
case stackLen >= 1 && (op == vm.SLOAD || op == vm.SSTORE):
|
|
|
|
slot := common.Hash(stackData[stackLen-1].Bytes32())
|
2022-10-06 08:05:43 +08:00
|
|
|
t.lookupStorage(caller, slot)
|
2022-01-31 09:51:17 +01:00
|
|
|
case stackLen >= 1 && (op == vm.EXTCODECOPY || op == vm.EXTCODEHASH || op == vm.EXTCODESIZE || op == vm.BALANCE || op == vm.SELFDESTRUCT):
|
|
|
|
addr := common.Address(stackData[stackLen-1].Bytes20())
|
|
|
|
t.lookupAccount(addr)
|
2022-10-06 08:05:43 +08:00
|
|
|
if op == vm.SELFDESTRUCT {
|
|
|
|
t.deleted[caller] = true
|
|
|
|
}
|
2022-01-31 09:51:17 +01:00
|
|
|
case stackLen >= 5 && (op == vm.DELEGATECALL || op == vm.CALL || op == vm.STATICCALL || op == vm.CALLCODE):
|
|
|
|
addr := common.Address(stackData[stackLen-2].Bytes20())
|
|
|
|
t.lookupAccount(addr)
|
|
|
|
case op == vm.CREATE:
|
2022-10-06 08:05:43 +08:00
|
|
|
nonce := t.env.StateDB.GetNonce(caller)
|
|
|
|
addr := crypto.CreateAddress(caller, nonce)
|
|
|
|
t.lookupAccount(addr)
|
|
|
|
t.created[addr] = true
|
2022-01-31 09:51:17 +01:00
|
|
|
case stackLen >= 4 && op == vm.CREATE2:
|
|
|
|
offset := stackData[stackLen-2]
|
|
|
|
size := stackData[stackLen-3]
|
2024-03-22 18:53:53 +01:00
|
|
|
init, err := internal.GetMemoryCopyPadded(scope.MemoryData(), int64(offset.Uint64()), int64(size.Uint64()))
|
2023-07-13 05:44:42 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Warn("failed to copy CREATE2 input", "err", err, "tracer", "prestateTracer", "offset", offset, "size", size)
|
|
|
|
return
|
|
|
|
}
|
2022-01-31 09:51:17 +01:00
|
|
|
inithash := crypto.Keccak256(init)
|
|
|
|
salt := stackData[stackLen-4]
|
2022-10-06 08:05:43 +08:00
|
|
|
addr := crypto.CreateAddress2(caller, salt.Bytes32(), inithash)
|
|
|
|
t.lookupAccount(addr)
|
|
|
|
t.created[addr] = true
|
2022-01-31 09:51:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-22 18:53:53 +01:00
|
|
|
func (t *prestateTracer) OnTxStart(env *tracing.VMContext, tx *types.Transaction, from common.Address) {
|
|
|
|
t.env = env
|
|
|
|
if tx.To() == nil {
|
|
|
|
t.to = crypto.CreateAddress(from, env.StateDB.GetNonce(from))
|
|
|
|
t.created[t.to] = true
|
|
|
|
} else {
|
|
|
|
t.to = *tx.To()
|
|
|
|
}
|
|
|
|
|
|
|
|
t.lookupAccount(from)
|
|
|
|
t.lookupAccount(t.to)
|
|
|
|
t.lookupAccount(env.Coinbase)
|
2022-03-31 11:51:44 +02:00
|
|
|
}
|
|
|
|
|
2024-03-22 18:53:53 +01:00
|
|
|
func (t *prestateTracer) OnTxEnd(receipt *types.Receipt, err error) {
|
|
|
|
if err != nil {
|
2022-10-06 08:05:43 +08:00
|
|
|
return
|
|
|
|
}
|
2024-03-22 18:53:53 +01:00
|
|
|
if t.config.DiffMode {
|
|
|
|
t.processDiffState()
|
|
|
|
}
|
|
|
|
// the new created contracts' prestate were empty, so delete them
|
|
|
|
for a := range t.created {
|
|
|
|
// the created contract maybe exists in statedb before the creating tx
|
|
|
|
if s := t.pre[a]; s != nil && s.empty {
|
|
|
|
delete(t.pre, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-06 08:05:43 +08:00
|
|
|
|
2024-03-22 18:53:53 +01:00
|
|
|
// GetResult returns the json-encoded nested list of call traces, and any
|
|
|
|
// error arising from the encoding or forceful termination (via `Stop`).
|
|
|
|
func (t *prestateTracer) GetResult() (json.RawMessage, error) {
|
|
|
|
var res []byte
|
|
|
|
var err error
|
|
|
|
if t.config.DiffMode {
|
|
|
|
res, err = json.Marshal(struct {
|
|
|
|
Post stateMap `json:"post"`
|
|
|
|
Pre stateMap `json:"pre"`
|
|
|
|
}{t.post, t.pre})
|
|
|
|
} else {
|
|
|
|
res, err = json.Marshal(t.pre)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return json.RawMessage(res), t.reason
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop terminates execution of the tracer at the first opportune moment.
|
|
|
|
func (t *prestateTracer) Stop(err error) {
|
|
|
|
t.reason = err
|
|
|
|
t.interrupt.Store(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *prestateTracer) processDiffState() {
|
2022-10-06 08:05:43 +08:00
|
|
|
for addr, state := range t.pre {
|
2022-11-01 16:55:44 +03:30
|
|
|
// The deleted account's state is pruned from `post` but kept in `pre`
|
2022-10-06 08:05:43 +08:00
|
|
|
if _, ok := t.deleted[addr]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
modified := false
|
|
|
|
postAccount := &account{Storage: make(map[common.Hash]common.Hash)}
|
2024-01-23 14:51:58 +01:00
|
|
|
newBalance := t.env.StateDB.GetBalance(addr).ToBig()
|
2022-10-06 08:05:43 +08:00
|
|
|
newNonce := t.env.StateDB.GetNonce(addr)
|
|
|
|
newCode := t.env.StateDB.GetCode(addr)
|
|
|
|
|
|
|
|
if newBalance.Cmp(t.pre[addr].Balance) != 0 {
|
|
|
|
modified = true
|
|
|
|
postAccount.Balance = newBalance
|
|
|
|
}
|
|
|
|
if newNonce != t.pre[addr].Nonce {
|
|
|
|
modified = true
|
|
|
|
postAccount.Nonce = newNonce
|
|
|
|
}
|
|
|
|
if !bytes.Equal(newCode, t.pre[addr].Code) {
|
|
|
|
modified = true
|
|
|
|
postAccount.Code = newCode
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, val := range state.Storage {
|
|
|
|
// don't include the empty slot
|
|
|
|
if val == (common.Hash{}) {
|
|
|
|
delete(t.pre[addr].Storage, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
newVal := t.env.StateDB.GetState(addr, key)
|
2022-11-01 16:55:44 +03:30
|
|
|
if val == newVal {
|
|
|
|
// Omit unchanged slots
|
|
|
|
delete(t.pre[addr].Storage, key)
|
|
|
|
} else {
|
2022-10-06 08:05:43 +08:00
|
|
|
modified = true
|
|
|
|
if newVal != (common.Hash{}) {
|
|
|
|
postAccount.Storage[key] = newVal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if modified {
|
|
|
|
t.post[addr] = postAccount
|
|
|
|
} else {
|
|
|
|
// if state is not modified, then no need to include into the pre state
|
|
|
|
delete(t.pre, addr)
|
|
|
|
}
|
|
|
|
}
|
2022-01-31 09:51:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// lookupAccount fetches details of an account and adds it to the prestate
|
|
|
|
// if it doesn't exist there.
|
|
|
|
func (t *prestateTracer) lookupAccount(addr common.Address) {
|
2022-10-06 08:05:43 +08:00
|
|
|
if _, ok := t.pre[addr]; ok {
|
2022-01-31 09:51:17 +01:00
|
|
|
return
|
|
|
|
}
|
2022-10-06 08:05:43 +08:00
|
|
|
|
2024-03-22 18:53:53 +01:00
|
|
|
acc := &account{
|
2024-01-23 14:51:58 +01:00
|
|
|
Balance: t.env.StateDB.GetBalance(addr).ToBig(),
|
2022-01-31 09:51:17 +01:00
|
|
|
Nonce: t.env.StateDB.GetNonce(addr),
|
2022-09-26 18:35:44 +02:00
|
|
|
Code: t.env.StateDB.GetCode(addr),
|
2022-01-31 09:51:17 +01:00
|
|
|
Storage: make(map[common.Hash]common.Hash),
|
|
|
|
}
|
2024-03-22 18:53:53 +01:00
|
|
|
if !acc.exists() {
|
|
|
|
acc.empty = true
|
|
|
|
}
|
|
|
|
t.pre[addr] = acc
|
2022-01-31 09:51:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// lookupStorage fetches the requested storage slot and adds
|
|
|
|
// it to the prestate of the given contract. It assumes `lookupAccount`
|
|
|
|
// has been performed on the contract before.
|
|
|
|
func (t *prestateTracer) lookupStorage(addr common.Address, key common.Hash) {
|
2022-10-06 08:05:43 +08:00
|
|
|
if _, ok := t.pre[addr].Storage[key]; ok {
|
2022-01-31 09:51:17 +01:00
|
|
|
return
|
|
|
|
}
|
2022-10-06 08:05:43 +08:00
|
|
|
t.pre[addr].Storage[key] = t.env.StateDB.GetState(addr, key)
|
2022-01-31 09:51:17 +01:00
|
|
|
}
|