fix TxStart for traceCall

This commit is contained in:
Sina Mahmoodi 2023-11-08 19:28:42 +03:00
parent da8b36276f
commit 0d268b796c
20 changed files with 29 additions and 34 deletions

View file

@ -234,7 +234,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
)
evm := vm.NewEVM(vmContext, txContext, statedb, chainConfig, vmConfig)
tracer.CaptureTxStart(evm, tx)
tracer.CaptureTxStart(evm, tx, msg.From)
// (ret []byte, usedGas uint64, failed bool, err error)
msgResult, err := core.ApplyMessage(evm, msg, gaspool)
if err != nil {

View file

@ -113,7 +113,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
// this method takes an already created EVM instance as input.
func ApplyTransactionWithEVM(msg *Message, config *params.ChainConfig, gp *GasPool, statedb *state.StateDB, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64, evm *vm.EVM) (receipt *types.Receipt, err error) {
if evm.Config.Tracer != nil {
evm.Config.Tracer.CaptureTxStart(evm, tx)
evm.Config.Tracer.CaptureTxStart(evm, tx, msg.From)
defer func() {
evm.Config.Tracer.CaptureTxEnd(receipt, err)
}()

View file

@ -30,7 +30,9 @@ import (
// if you need to retain them beyond the current call.
type EVMLogger interface {
// Transaction level
CaptureTxStart(evm *EVM, tx *types.Transaction)
// Call simulations don't come with a valid signature. `from` field
// to be used for address of the caller.
CaptureTxStart(evm *EVM, tx *types.Transaction, from common.Address)
CaptureTxEnd(receipt *types.Receipt, err error)
// Top call frame
CaptureStart(from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int)

View file

@ -122,7 +122,7 @@ func Execute(code, input []byte, cfg *Config) ([]byte, *state.StateDB, error) {
rules = cfg.ChainConfig.Rules(vmenv.Context.BlockNumber, vmenv.Context.Random != nil, vmenv.Context.Time)
)
if cfg.EVMConfig.Tracer != nil {
cfg.EVMConfig.Tracer.CaptureTxStart(vmenv, types.NewTx(&types.LegacyTx{To: &address, Data: input, Value: cfg.Value, Gas: cfg.GasLimit}))
cfg.EVMConfig.Tracer.CaptureTxStart(vmenv, types.NewTx(&types.LegacyTx{To: &address, Data: input, Value: cfg.Value, Gas: cfg.GasLimit}), cfg.Origin)
}
// Execute the preparatory steps for state transition which includes:
// - prepare accessList(post-berlin)
@ -158,7 +158,7 @@ func Create(input []byte, cfg *Config) ([]byte, common.Address, uint64, error) {
rules = cfg.ChainConfig.Rules(vmenv.Context.BlockNumber, vmenv.Context.Random != nil, vmenv.Context.Time)
)
if cfg.EVMConfig.Tracer != nil {
cfg.EVMConfig.Tracer.CaptureTxStart(vmenv, types.NewTx(&types.LegacyTx{Data: input, Value: cfg.Value, Gas: cfg.GasLimit}))
cfg.EVMConfig.Tracer.CaptureTxStart(vmenv, types.NewTx(&types.LegacyTx{Data: input, Value: cfg.Value, Gas: cfg.GasLimit}), cfg.Origin)
}
// Execute the preparatory steps for state transition which includes:
// - prepare accessList(post-berlin)
@ -189,7 +189,7 @@ func Call(address common.Address, input []byte, cfg *Config) ([]byte, uint64, er
rules = cfg.ChainConfig.Rules(vmenv.Context.BlockNumber, vmenv.Context.Random != nil, vmenv.Context.Time)
)
if cfg.EVMConfig.Tracer != nil {
cfg.EVMConfig.Tracer.CaptureTxStart(vmenv, types.NewTx(&types.LegacyTx{To: &address, Data: input, Value: cfg.Value, Gas: cfg.GasLimit}))
cfg.EVMConfig.Tracer.CaptureTxStart(vmenv, types.NewTx(&types.LegacyTx{To: &address, Data: input, Value: cfg.Value, Gas: cfg.GasLimit}), cfg.Origin)
}
// Execute the preparatory steps for state transition which includes:
// - prepare accessList(post-berlin)

View file

@ -784,7 +784,7 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block
// Execute the transaction and flush any traces to disk
vmenv := vm.NewEVM(vmctx, txContext, statedb, chainConfig, vmConf)
statedb.SetTxContext(tx.Hash(), i)
vmConf.Tracer.CaptureTxStart(vmenv, tx)
vmConf.Tracer.CaptureTxStart(vmenv, tx, msg.From)
vmRet, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.GasLimit))
vmConf.Tracer.CaptureTxEnd(&types.Receipt{GasUsed: vmRet.UsedGas}, err)
if writer != nil {

View file

@ -70,7 +70,7 @@ func (t *NoopTracer) CaptureEnter(typ vm.OpCode, from common.Address, to common.
func (t *NoopTracer) CaptureExit(output []byte, gasUsed uint64, err error) {
}
func (*NoopTracer) CaptureTxStart(env *vm.EVM, tx *types.Transaction) {}
func (*NoopTracer) CaptureTxStart(env *vm.EVM, tx *types.Transaction, from common.Address) {}
func (*NoopTracer) CaptureTxEnd(receipt *types.Receipt, err error) {}

View file

@ -153,7 +153,7 @@ func testCallTracer(tracerName string, dirPath string, t *testing.T) {
if err != nil {
t.Fatalf("failed to prepare transaction for tracing: %v", err)
}
tracer.CaptureTxStart(evm, tx)
tracer.CaptureTxStart(evm, tx, msg.From)
vmRet, err := core.ApplyMessage(evm, msg, new(core.GasPool).AddGas(tx.Gas()))
if err != nil {
t.Fatalf("failed to execute transaction: %v", err)
@ -401,7 +401,7 @@ func TestInternals(t *testing.T) {
if err != nil {
t.Fatalf("test %v: failed to create message: %v", tc.name, err)
}
tc.tracer.CaptureTxStart(evm, tx)
tc.tracer.CaptureTxStart(evm, tx, msg.From)
vmRet, err := core.ApplyMessage(evm, msg, new(core.GasPool).AddGas(tx.Gas()))
if err != nil {
t.Fatalf("test %v: failed to execute transaction: %v", tc.name, err)

View file

@ -113,7 +113,7 @@ func flatCallTracerTestRunner(tracerName string, filename string, dirPath string
if err != nil {
return fmt.Errorf("failed to prepare transaction for tracing: %v", err)
}
tracer.CaptureTxStart(evm, tx)
tracer.CaptureTxStart(evm, tx, msg.From)
vmRet, err := core.ApplyMessage(evm, msg, new(core.GasPool).AddGas(tx.Gas()))
if err != nil {
return fmt.Errorf("failed to execute transaction: %v", err)

View file

@ -122,7 +122,7 @@ func testPrestateDiffTracer(tracerName string, dirPath string, t *testing.T) {
if err != nil {
t.Fatalf("failed to prepare transaction for tracing: %v", err)
}
tracer.CaptureTxStart(evm, tx)
tracer.CaptureTxStart(evm, tx, msg.From)
vmRet, err := core.ApplyMessage(evm, msg, new(core.GasPool).AddGas(tx.Gas()))
if err != nil {
t.Fatalf("failed to execute transaction: %v", err)

View file

@ -214,7 +214,7 @@ func newJsTracer(code string, ctx *directory.Context, cfg json.RawMessage) (dire
// CaptureTxStart implements the Tracer interface and is invoked at the beginning of
// transaction processing.
func (t *jsTracer) CaptureTxStart(env *vm.EVM, tx *types.Transaction) {
func (t *jsTracer) CaptureTxStart(env *vm.EVM, tx *types.Transaction, from common.Address) {
t.env = env
// Need statedb access for db object
db := &dbObj{db: env.StateDB, vm: t.vm, toBig: t.toBig, toBuf: t.toBuf, fromBuf: t.fromBuf}

View file

@ -74,7 +74,7 @@ func runTrace(tracer directory.Tracer, vmctx *vmContext, chaincfg *params.ChainC
contract.Code = contractCode
}
tracer.CaptureTxStart(env, types.NewTx(&types.LegacyTx{Gas: gasLimit}))
tracer.CaptureTxStart(env, types.NewTx(&types.LegacyTx{Gas: gasLimit}), contract.Caller())
tracer.CaptureStart(contract.Caller(), contract.Address(), false, []byte{}, startGas, value)
ret, err := env.Interpreter().Run(contract, []byte{}, false)
tracer.CaptureEnd(ret, startGas-contract.Gas, err)
@ -185,7 +185,7 @@ func TestHaltBetweenSteps(t *testing.T) {
Contract: vm.NewContract(&account{}, &account{}, big.NewInt(0), 0),
}
env := vm.NewEVM(vm.BlockContext{BlockNumber: big.NewInt(1)}, vm.TxContext{GasPrice: big.NewInt(1)}, &dummyStatedb{}, params.TestChainConfig, vm.Config{Tracer: tracer})
tracer.CaptureTxStart(env, types.NewTx(&types.LegacyTx{}))
tracer.CaptureTxStart(env, types.NewTx(&types.LegacyTx{}), common.Address{})
tracer.CaptureStart(common.Address{}, common.Address{}, false, []byte{}, 0, big.NewInt(0))
tracer.CaptureState(0, 0, 0, 0, scope, nil, 0, nil)
timeout := errors.New("stahp")
@ -207,7 +207,7 @@ func TestNoStepExec(t *testing.T) {
t.Fatal(err)
}
env := vm.NewEVM(vm.BlockContext{BlockNumber: big.NewInt(1)}, vm.TxContext{GasPrice: big.NewInt(100)}, &dummyStatedb{}, params.TestChainConfig, vm.Config{Tracer: tracer})
tracer.CaptureTxStart(env, types.NewTx(&types.LegacyTx{}))
tracer.CaptureTxStart(env, types.NewTx(&types.LegacyTx{}), common.Address{})
tracer.CaptureStart(common.Address{}, common.Address{}, false, []byte{}, 1000, big.NewInt(0))
tracer.CaptureEnd(nil, 0, nil)
ret, err := tracer.GetResult()

View file

@ -58,7 +58,7 @@ func (p *Printer) CaptureExit(output []byte, gasUsed uint64, err error) {
fmt.Printf("CaptureExit: output=%s, gasUsed=%v, err=%v\n", hexutil.Bytes(output), gasUsed, err)
}
func (p *Printer) CaptureTxStart(env *vm.EVM, tx *types.Transaction) {
func (p *Printer) CaptureTxStart(env *vm.EVM, tx *types.Transaction, from common.Address) {
buf, err := json.Marshal(tx)
if err != nil {
fmt.Printf("err: %v\n", err)

View file

@ -246,7 +246,7 @@ func (l *StructLogger) Stop(err error) {
l.interrupt.Store(true)
}
func (l *StructLogger) CaptureTxStart(env *vm.EVM, tx *types.Transaction) {
func (l *StructLogger) CaptureTxStart(env *vm.EVM, tx *types.Transaction, from common.Address) {
l.env = env
}

View file

@ -90,6 +90,6 @@ func (l *JSONLogger) CaptureEnd(output []byte, gasUsed uint64, err error) {
l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), errMsg})
}
func (l *JSONLogger) CaptureTxStart(env *vm.EVM, tx *types.Transaction) {
func (l *JSONLogger) CaptureTxStart(env *vm.EVM, tx *types.Transaction, from common.Address) {
l.env = env
}

View file

@ -60,7 +60,7 @@ func TestStoreCapture(t *testing.T) {
)
contract.Code = []byte{byte(vm.PUSH1), 0x1, byte(vm.PUSH1), 0x0, byte(vm.SSTORE)}
var index common.Hash
logger.CaptureTxStart(env, nil)
logger.CaptureTxStart(env, nil, common.Address{})
logger.CaptureStart(common.Address{}, contract.Address(), false, nil, 0, nil)
_, err := env.Interpreter().Run(contract, []byte{}, false)
if err != nil {

View file

@ -80,7 +80,7 @@ func (t *fourByteTracer) store(id []byte, size int) {
t.ids[key] += 1
}
func (t *fourByteTracer) CaptureTxStart(env *vm.EVM, tx *types.Transaction) {
func (t *fourByteTracer) CaptureTxStart(env *vm.EVM, tx *types.Transaction, from common.Address) {
t.env = env
// Update list of precompiles based on current block
rules := t.env.ChainConfig().Rules(t.env.Context.BlockNumber, t.env.Context.Random != nil, t.env.Context.Time)

View file

@ -196,7 +196,7 @@ func (t *callTracer) CaptureExit(output []byte, gasUsed uint64, err error) {
t.callstack[size-1].Calls = append(t.callstack[size-1].Calls, call)
}
func (t *callTracer) CaptureTxStart(env *vm.EVM, tx *types.Transaction) {
func (t *callTracer) CaptureTxStart(env *vm.EVM, tx *types.Transaction, from common.Address) {
t.gasLimit = tx.Gas()
}

View file

@ -200,8 +200,8 @@ func (t *flatCallTracer) CaptureExit(output []byte, gasUsed uint64, err error) {
}
}
func (t *flatCallTracer) CaptureTxStart(env *vm.EVM, tx *types.Transaction) {
t.tracer.CaptureTxStart(env, tx)
func (t *flatCallTracer) CaptureTxStart(env *vm.EVM, tx *types.Transaction, from common.Address) {
t.tracer.CaptureTxStart(env, tx, from)
// Update list of precompiles based on current block
rules := env.ChainConfig().Rules(env.Context.BlockNumber, env.Context.Random != nil, env.Context.Time)
t.activePrecompiles = vm.ActivePrecompiles(rules)

View file

@ -117,9 +117,9 @@ func (t *muxTracer) CaptureExit(output []byte, gasUsed uint64, err error) {
}
}
func (t *muxTracer) CaptureTxStart(env *vm.EVM, tx *types.Transaction) {
func (t *muxTracer) CaptureTxStart(env *vm.EVM, tx *types.Transaction, from common.Address) {
for _, t := range t.tracers {
t.CaptureTxStart(env, tx)
t.CaptureTxStart(env, tx, from)
}
}

View file

@ -19,7 +19,6 @@ package native
import (
"bytes"
"encoding/json"
"fmt"
"math/big"
"sync/atomic"
@ -156,14 +155,8 @@ func (t *prestateTracer) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64,
}
}
func (t *prestateTracer) CaptureTxStart(env *vm.EVM, tx *types.Transaction) {
func (t *prestateTracer) CaptureTxStart(env *vm.EVM, tx *types.Transaction, from common.Address) {
t.env = env
signer := types.MakeSigner(env.ChainConfig(), env.Context.BlockNumber, env.Context.Time)
from, err := types.Sender(signer, tx)
if err != nil {
t.Stop(fmt.Errorf("could not recover sender address: %v", err))
return
}
if tx.To() == nil {
t.create = true
t.to = crypto.CreateAddress(from, env.StateDB.GetNonce(from))