Skip to content

Commit

Permalink
support for golang tracers + add golang callTracer (#558)
Browse files Browse the repository at this point in the history
* feat: rename Tracer interface to EVMLogger;
minor changes in API
refine api_tracer.go
refine Tracer interface

* fix: broken tracer tests

* feat: add BenchmarkTransactionTrace

* feat: tracer CaptureEnter CaptureExit in evm

* feat: upgrade js tracers with geth upstream

* chore: clean test

* feat: eth/tracers: support for golang tracers + add golang callTracer
cf. ethereum#23708

* chore: clean testdata json

* fix: change test due to IntrinsicGas is not upgraded

* feat: make native Tracer the default Tracer

* fix: update tracers.New in api

* fix: addr prefix in callTracer

* fix: remove `native` in BenchmarkTracers

* fix: return consensus error of InsufficientBalance for tx, instead of vmerr

* chore: drop js tracers: call and noop
  • Loading branch information
wgr523 authored and wanwiset25 committed Aug 23, 2024
1 parent 7d92b24 commit 4b65378
Show file tree
Hide file tree
Showing 54 changed files with 2,877 additions and 561 deletions.
4 changes: 4 additions & 0 deletions cmd/XDC/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ import (
"github.com/XinFinOrg/XDPoSChain/log"
"github.com/XinFinOrg/XDPoSChain/metrics"
"github.com/XinFinOrg/XDPoSChain/node"

// Force-load the native, to trigger registration
_ "github.com/XinFinOrg/XDPoSChain/eth/tracers/native"

"gopkg.in/urfave/cli.v1"
)

Expand Down
90 changes: 0 additions & 90 deletions cmd/evm/json_logger.go

This file was deleted.

6 changes: 3 additions & 3 deletions cmd/evm/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,20 @@ func runCmd(ctx *cli.Context) error {
glogger.Verbosity(log.Lvl(ctx.GlobalInt(VerbosityFlag.Name)))
log.Root().SetHandler(glogger)
logconfig := &vm.LogConfig{
DisableMemory: ctx.GlobalBool(DisableMemoryFlag.Name),
EnableMemory: !ctx.GlobalBool(DisableMemoryFlag.Name),
DisableStack: ctx.GlobalBool(DisableStackFlag.Name),
}

var (
tracer vm.Tracer
tracer vm.EVMLogger
debugLogger *vm.StructLogger
statedb *state.StateDB
chainConfig *params.ChainConfig
sender = common.StringToAddress("sender")
receiver = common.StringToAddress("receiver")
)
if ctx.GlobalBool(MachineFlag.Name) {
tracer = NewJSONLogger(logconfig, os.Stdout)
tracer = vm.NewJSONLogger(logconfig, os.Stdout)
} else if ctx.GlobalBool(DebugFlag.Name) {
debugLogger = vm.NewStructLogger(logconfig)
tracer = debugLogger
Expand Down
8 changes: 4 additions & 4 deletions cmd/evm/staterunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ func stateTestCmd(ctx *cli.Context) error {

// Configure the EVM logger
config := &vm.LogConfig{
DisableMemory: ctx.GlobalBool(DisableMemoryFlag.Name),
DisableStack: ctx.GlobalBool(DisableStackFlag.Name),
EnableMemory: !ctx.GlobalBool(DisableMemoryFlag.Name),
DisableStack: ctx.GlobalBool(DisableStackFlag.Name),
}
var (
tracer vm.Tracer
tracer vm.EVMLogger
debugger *vm.StructLogger
)
switch {
case ctx.GlobalBool(MachineFlag.Name):
tracer = NewJSONLogger(config, os.Stderr)
tracer = vm.NewJSONLogger(config, os.Stderr)

case ctx.GlobalBool(DebugFlag.Name):
debugger = vm.NewStructLogger(config)
Expand Down
3 changes: 3 additions & 0 deletions core/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,7 @@ var (
// ErrTxTypeNotSupported is returned if a transaction is not supported in the
// current network configuration.
ErrTxTypeNotSupported = types.ErrTxTypeNotSupported

// ErrGasUintOverflow is returned when calculating gas usage.
ErrGasUintOverflow = errors.New("gas uint64 overflow")
)
21 changes: 7 additions & 14 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package core

import (
"errors"
"fmt"
"math"
"math/big"

Expand Down Expand Up @@ -100,13 +101,13 @@ func IntrinsicGas(data []byte, accessList types.AccessList, isContractCreation,
}
// Make sure we don't exceed uint64 for all data combinations
if (math.MaxUint64-gas)/params.TxDataNonZeroGas < nz {
return 0, vm.ErrOutOfGas
return 0, ErrGasUintOverflow
}
gas += nz * params.TxDataNonZeroGas

z := uint64(len(data)) - nz
if (math.MaxUint64-gas)/params.TxDataZeroGas < z {
return 0, vm.ErrOutOfGas
return 0, ErrGasUintOverflow
}
gas += z * params.TxDataZeroGas
}
Expand Down Expand Up @@ -169,15 +170,6 @@ func (st *StateTransition) to() vm.AccountRef {
return reference
}

func (st *StateTransition) useGas(amount uint64) error {
if st.gas < amount {
return vm.ErrOutOfGas
}
st.gas -= amount

return nil
}

func (st *StateTransition) buyGas() error {
var (
state = st.state
Expand Down Expand Up @@ -238,9 +230,10 @@ func (st *StateTransition) TransitionDb(owner common.Address) (ret []byte, usedG
if err != nil {
return nil, 0, false, err, nil
}
if err = st.useGas(gas); err != nil {
return nil, 0, false, err, nil
if st.gas < gas {
return nil, 0, false, fmt.Errorf("%w: have %d, want %d", ErrIntrinsicGas, st.gas, gas), nil
}
st.gas -= gas

if rules := st.evm.ChainConfig().Rules(st.evm.Context.BlockNumber); rules.IsEIP1559 {
st.state.PrepareAccessList(msg.From(), msg.To(), vm.ActivePrecompiles(rules), msg.AccessList())
Expand Down Expand Up @@ -286,7 +279,7 @@ func (st *StateTransition) TransitionDb(owner common.Address) (ret []byte, usedG
st.state.AddBalance(st.evm.Coinbase, new(big.Int).Mul(new(big.Int).SetUint64(st.gasUsed()), st.gasPrice))
}

return ret, st.gasUsed(), vmerr != nil, err, vmerr
return ret, st.gasUsed(), vmerr != nil, nil, vmerr
}

func (st *StateTransition) refundGas() {
Expand Down
7 changes: 6 additions & 1 deletion core/vm/access_list_tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (al accessList) equal(other accessList) bool {
func (al accessList) accessList() types.AccessList {
acl := make(types.AccessList, 0, len(al))
for addr, slots := range al {
tuple := types.AccessTuple{Address: addr}
tuple := types.AccessTuple{Address: addr, StorageKeys: []common.Hash{}}
for slot := range slots {
tuple.StorageKeys = append(tuple.StorageKeys, slot)
}
Expand Down Expand Up @@ -166,6 +166,11 @@ func (*AccessListTracer) CaptureFault(env *EVM, pc uint64, op OpCode, gas, cost

func (*AccessListTracer) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) {}

func (*AccessListTracer) CaptureEnter(typ OpCode, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) {
}

func (*AccessListTracer) CaptureExit(output []byte, gasUsed uint64, err error) {}

// AccessList returns the current accesslist maintained by the tracer.
func (a *AccessListTracer) AccessList() types.AccessList {
return a.list.accessList()
Expand Down
Loading

0 comments on commit 4b65378

Please sign in to comment.