2022-01-31 11:51:17 +03: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 03:05:43 +03:00
|
|
|
"bytes"
|
2022-01-31 11:51:17 +03:00
|
|
|
"encoding/json"
|
|
|
|
"math/big"
|
|
|
|
"sync/atomic"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/eth/tracers"
|
|
|
|
)
|
|
|
|
|
2022-09-26 19:35:44 +03:00
|
|
|
//go:generate go run github.com/fjl/gencodec -type account -field-override accountMarshaling -out gen_account_json.go
|
|
|
|
|
2022-01-31 11:51:17 +03:00
|
|
|
func init() {
|
2023-01-10 17:11:53 +03:00
|
|
|
tracers.DefaultDirectory.Register("prestateTracer", newPrestateTracer, false)
|
2022-01-31 11:51:17 +03:00
|
|
|
}
|
|
|
|
|
2022-10-06 03:05:43 +03:00
|
|
|
type state = map[common.Address]*account
|
|
|
|
|
2022-01-31 11:51:17 +03:00
|
|
|
type account struct {
|
2022-10-06 03:05:43 +03:00
|
|
|
Balance *big.Int `json:"balance,omitempty"`
|
|
|
|
Code []byte `json:"code,omitempty"`
|
2022-10-21 18:55:15 +03:00
|
|
|
Nonce uint64 `json:"nonce,omitempty"`
|
2022-10-06 03:05:43 +03:00
|
|
|
Storage map[common.Hash]common.Hash `json:"storage,omitempty"`
|
2022-01-31 11:51:17 +03:00
|
|
|
}
|
|
|
|
|
2022-10-21 12:48:53 +03:00
|
|
|
func (a *account) exists() bool {
|
2022-12-13 16:32:29 +03:00
|
|
|
return a.Nonce > 0 || len(a.Code) > 0 || len(a.Storage) > 0 || (a.Balance != nil && a.Balance.Sign() != 0)
|
2022-10-21 12:48:53 +03:00
|
|
|
}
|
|
|
|
|
2022-09-26 19:35:44 +03:00
|
|
|
type accountMarshaling struct {
|
|
|
|
Balance *hexutil.Big
|
|
|
|
Code hexutil.Bytes
|
|
|
|
}
|
|
|
|
|
2022-01-31 11:51:17 +03:00
|
|
|
type prestateTracer struct {
|
2022-11-17 11:27:48 +03:00
|
|
|
noopTracer
|
2022-01-31 11:51:17 +03:00
|
|
|
env *vm.EVM
|
2022-10-06 03:05:43 +03:00
|
|
|
pre state
|
|
|
|
post state
|
2022-01-31 11:51:17 +03:00
|
|
|
create bool
|
|
|
|
to common.Address
|
2022-03-31 12:51:44 +03:00
|
|
|
gasLimit uint64 // Amount of gas bought for the whole tx
|
2022-10-06 03:05:43 +03:00
|
|
|
config prestateTracerConfig
|
2022-01-31 11:51:17 +03:00
|
|
|
interrupt uint32 // Atomic flag to signal execution interruption
|
|
|
|
reason error // Textual reason for the interruption
|
2022-10-06 03:05:43 +03:00
|
|
|
created map[common.Address]bool
|
|
|
|
deleted map[common.Address]bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type prestateTracerConfig struct {
|
2022-11-01 16:25:44 +03:00
|
|
|
DiffMode bool `json:"diffMode"` // If true, this tracer will return state modifications
|
2022-01-31 11:51:17 +03:00
|
|
|
}
|
|
|
|
|
2022-10-06 03:05:43 +03:00
|
|
|
func newPrestateTracer(ctx *tracers.Context, cfg json.RawMessage) (tracers.Tracer, error) {
|
|
|
|
var config prestateTracerConfig
|
|
|
|
if cfg != nil {
|
|
|
|
if err := json.Unmarshal(cfg, &config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &prestateTracer{
|
|
|
|
pre: state{},
|
|
|
|
post: state{},
|
|
|
|
config: config,
|
|
|
|
created: make(map[common.Address]bool),
|
|
|
|
deleted: make(map[common.Address]bool),
|
|
|
|
}, nil
|
2022-01-31 11:51:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// CaptureStart implements the EVMLogger interface to initialize the tracing operation.
|
|
|
|
func (t *prestateTracer) CaptureStart(env *vm.EVM, from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) {
|
|
|
|
t.env = env
|
|
|
|
t.create = create
|
|
|
|
t.to = to
|
|
|
|
|
|
|
|
t.lookupAccount(from)
|
|
|
|
t.lookupAccount(to)
|
2022-10-06 03:05:43 +03:00
|
|
|
t.lookupAccount(env.Context.Coinbase)
|
2022-01-31 11:51:17 +03:00
|
|
|
|
|
|
|
// The recipient balance includes the value transferred.
|
2022-10-06 03:05:43 +03:00
|
|
|
toBal := new(big.Int).Sub(t.pre[to].Balance, value)
|
|
|
|
t.pre[to].Balance = toBal
|
2022-01-31 11:51:17 +03:00
|
|
|
|
2022-03-31 12:51:44 +03:00
|
|
|
// The sender balance is after reducing: value and gasLimit.
|
2022-01-31 11:51:17 +03:00
|
|
|
// We need to re-add them to get the pre-tx balance.
|
2022-10-06 03:05:43 +03:00
|
|
|
fromBal := new(big.Int).Set(t.pre[from].Balance)
|
2022-01-31 11:51:17 +03:00
|
|
|
gasPrice := env.TxContext.GasPrice
|
2022-03-31 12:51:44 +03:00
|
|
|
consumedGas := new(big.Int).Mul(gasPrice, new(big.Int).SetUint64(t.gasLimit))
|
2022-01-31 11:51:17 +03:00
|
|
|
fromBal.Add(fromBal, new(big.Int).Add(value, consumedGas))
|
2022-10-06 03:05:43 +03:00
|
|
|
t.pre[from].Balance = fromBal
|
|
|
|
t.pre[from].Nonce--
|
|
|
|
|
|
|
|
if create && t.config.DiffMode {
|
|
|
|
t.created[to] = true
|
|
|
|
}
|
2022-01-31 11:51:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// CaptureEnd is called after the call finishes to finalize the tracing.
|
2022-12-05 18:57:47 +03:00
|
|
|
func (t *prestateTracer) CaptureEnd(output []byte, gasUsed uint64, err error) {
|
2022-10-21 12:48:53 +03:00
|
|
|
if t.config.DiffMode {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.create {
|
|
|
|
// Keep existing account prior to contract creation at that address
|
|
|
|
if s := t.pre[t.to]; s != nil && !s.exists() {
|
|
|
|
// Exclude newly created contract.
|
|
|
|
delete(t.pre, t.to)
|
|
|
|
}
|
2022-01-31 11:51:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CaptureState implements the EVMLogger interface to trace a single step of VM execution.
|
|
|
|
func (t *prestateTracer) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, rData []byte, depth int, err error) {
|
|
|
|
stack := scope.Stack
|
|
|
|
stackData := stack.Data()
|
|
|
|
stackLen := len(stackData)
|
2022-10-06 03:05:43 +03:00
|
|
|
caller := scope.Contract.Address()
|
2022-01-31 11:51:17 +03:00
|
|
|
switch {
|
|
|
|
case stackLen >= 1 && (op == vm.SLOAD || op == vm.SSTORE):
|
|
|
|
slot := common.Hash(stackData[stackLen-1].Bytes32())
|
2022-10-06 03:05:43 +03:00
|
|
|
t.lookupStorage(caller, slot)
|
2022-01-31 11:51:17 +03: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 03:05:43 +03:00
|
|
|
if op == vm.SELFDESTRUCT {
|
|
|
|
t.deleted[caller] = true
|
|
|
|
}
|
2022-01-31 11:51:17 +03: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 03:05:43 +03:00
|
|
|
nonce := t.env.StateDB.GetNonce(caller)
|
|
|
|
addr := crypto.CreateAddress(caller, nonce)
|
|
|
|
t.lookupAccount(addr)
|
|
|
|
t.created[addr] = true
|
2022-01-31 11:51:17 +03:00
|
|
|
case stackLen >= 4 && op == vm.CREATE2:
|
|
|
|
offset := stackData[stackLen-2]
|
|
|
|
size := stackData[stackLen-3]
|
|
|
|
init := scope.Memory.GetCopy(int64(offset.Uint64()), int64(size.Uint64()))
|
|
|
|
inithash := crypto.Keccak256(init)
|
|
|
|
salt := stackData[stackLen-4]
|
2022-10-06 03:05:43 +03:00
|
|
|
addr := crypto.CreateAddress2(caller, salt.Bytes32(), inithash)
|
|
|
|
t.lookupAccount(addr)
|
|
|
|
t.created[addr] = true
|
2022-01-31 11:51:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-31 12:51:44 +03:00
|
|
|
func (t *prestateTracer) CaptureTxStart(gasLimit uint64) {
|
|
|
|
t.gasLimit = gasLimit
|
|
|
|
}
|
|
|
|
|
2022-10-06 03:05:43 +03:00
|
|
|
func (t *prestateTracer) CaptureTxEnd(restGas uint64) {
|
|
|
|
if !t.config.DiffMode {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for addr, state := range t.pre {
|
2022-11-01 16:25:44 +03:00
|
|
|
// The deleted account's state is pruned from `post` but kept in `pre`
|
2022-10-06 03:05:43 +03:00
|
|
|
if _, ok := t.deleted[addr]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
modified := false
|
|
|
|
postAccount := &account{Storage: make(map[common.Hash]common.Hash)}
|
|
|
|
newBalance := t.env.StateDB.GetBalance(addr)
|
|
|
|
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:25:44 +03:00
|
|
|
if val == newVal {
|
|
|
|
// Omit unchanged slots
|
|
|
|
delete(t.pre[addr].Storage, key)
|
|
|
|
} else {
|
2022-10-06 03:05:43 +03: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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 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
|
2022-10-21 12:48:53 +03:00
|
|
|
if s := t.pre[a]; s != nil && !s.exists() {
|
|
|
|
delete(t.pre, a)
|
2022-10-06 03:05:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-31 12:51:44 +03:00
|
|
|
|
2022-01-31 11:51:17 +03: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) {
|
2022-10-06 03:05:43 +03:00
|
|
|
var res []byte
|
|
|
|
var err error
|
|
|
|
if t.config.DiffMode {
|
|
|
|
res, err = json.Marshal(struct {
|
|
|
|
Post state `json:"post"`
|
2022-10-21 18:55:15 +03:00
|
|
|
Pre state `json:"pre"`
|
|
|
|
}{t.post, t.pre})
|
2022-10-06 03:05:43 +03:00
|
|
|
} else {
|
|
|
|
res, err = json.Marshal(t.pre)
|
|
|
|
}
|
2022-01-31 11:51:17 +03:00
|
|
|
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
|
|
|
|
atomic.StoreUint32(&t.interrupt, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 03:05:43 +03:00
|
|
|
if _, ok := t.pre[addr]; ok {
|
2022-01-31 11:51:17 +03:00
|
|
|
return
|
|
|
|
}
|
2022-10-06 03:05:43 +03:00
|
|
|
|
|
|
|
t.pre[addr] = &account{
|
2022-09-26 19:35:44 +03:00
|
|
|
Balance: t.env.StateDB.GetBalance(addr),
|
2022-01-31 11:51:17 +03:00
|
|
|
Nonce: t.env.StateDB.GetNonce(addr),
|
2022-09-26 19:35:44 +03:00
|
|
|
Code: t.env.StateDB.GetCode(addr),
|
2022-01-31 11:51:17 +03:00
|
|
|
Storage: make(map[common.Hash]common.Hash),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 03:05:43 +03:00
|
|
|
if _, ok := t.pre[addr].Storage[key]; ok {
|
2022-01-31 11:51:17 +03:00
|
|
|
return
|
|
|
|
}
|
2022-10-06 03:05:43 +03:00
|
|
|
t.pre[addr].Storage[key] = t.env.StateDB.GetState(addr, key)
|
2022-01-31 11:51:17 +03:00
|
|
|
}
|