From 8de8a483e5183a8436b1e87ca200202a65456d44 Mon Sep 17 00:00:00 2001 From: Arun Dhyani Date: Fri, 16 Feb 2024 16:22:32 +0530 Subject: [PATCH] merge bugfixes --- consensus/mock/chain_header_reader_mock.go | 28 +++++ eth/tracers/native/prestate.go | 4 +- polygon/tracer/bor_state_sync_txn_tracer.go | 119 ++++++++++++++++++-- turbo/transactions/tracing.go | 16 ++- 4 files changed, 150 insertions(+), 17 deletions(-) diff --git a/consensus/mock/chain_header_reader_mock.go b/consensus/mock/chain_header_reader_mock.go index 5131b49e374..297c201d563 100644 --- a/consensus/mock/chain_header_reader_mock.go +++ b/consensus/mock/chain_header_reader_mock.go @@ -65,6 +65,20 @@ func (mr *MockChainHeaderReaderMockRecorder) Config() *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Config", reflect.TypeOf((*MockChainHeaderReader)(nil).Config)) } +// CurrentFinalizedHeader mocks base method. +func (m *MockChainHeaderReader) CurrentFinalizedHeader() *types.Header { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "CurrentFinalizedHeader") + ret0, _ := ret[0].(*types.Header) + return ret0 +} + +// CurrentFinalizedHeader indicates an expected call of CurrentFinalizedHeader. +func (mr *MockChainHeaderReaderMockRecorder) CurrentFinalizedHeader() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CurrentFinalizedHeader", reflect.TypeOf((*MockChainHeaderReader)(nil).CurrentFinalizedHeader)) +} + // CurrentHeader mocks base method. func (m *MockChainHeaderReader) CurrentHeader() *types.Header { m.ctrl.T.Helper() @@ -79,6 +93,20 @@ func (mr *MockChainHeaderReaderMockRecorder) CurrentHeader() *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CurrentHeader", reflect.TypeOf((*MockChainHeaderReader)(nil).CurrentHeader)) } +// CurrentSafeHeader mocks base method. +func (m *MockChainHeaderReader) CurrentSafeHeader() *types.Header { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "CurrentSafeHeader") + ret0, _ := ret[0].(*types.Header) + return ret0 +} + +// CurrentSafeHeader indicates an expected call of CurrentSafeHeader. +func (mr *MockChainHeaderReaderMockRecorder) CurrentSafeHeader() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CurrentSafeHeader", reflect.TypeOf((*MockChainHeaderReader)(nil).CurrentSafeHeader)) +} + // FrozenBlocks mocks base method. func (m *MockChainHeaderReader) FrozenBlocks() uint64 { m.ctrl.T.Helper() diff --git a/eth/tracers/native/prestate.go b/eth/tracers/native/prestate.go index 51326982ab9..8b980b0bf28 100644 --- a/eth/tracers/native/prestate.go +++ b/eth/tracers/native/prestate.go @@ -153,7 +153,7 @@ func (t *prestateTracer) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64, func (t *prestateTracer) CaptureTxStart(env *vm.EVM, tx types.Transaction) { t.env = env - signer := types.MakeSigner(env.ChainConfig(), env.Context().BlockNumber, env.Context().Time) + signer := types.MakeSigner(env.ChainConfig(), env.Context.BlockNumber, env.Context.Time) from, err := tx.Sender(*signer) if err != nil { t.Stop(fmt.Errorf("could not recover sender address: %v", err)) @@ -169,7 +169,7 @@ func (t *prestateTracer) CaptureTxStart(env *vm.EVM, tx types.Transaction) { t.lookupAccount(from) t.lookupAccount(t.to) - t.lookupAccount(env.Context().Coinbase) + t.lookupAccount(env.Context.Coinbase) if t.create && t.config.DiffMode { t.created[t.to] = true diff --git a/polygon/tracer/bor_state_sync_txn_tracer.go b/polygon/tracer/bor_state_sync_txn_tracer.go index 1beea7c312e..a70ff5ae4ae 100644 --- a/polygon/tracer/bor_state_sync_txn_tracer.go +++ b/polygon/tracer/bor_state_sync_txn_tracer.go @@ -2,12 +2,16 @@ package tracer import ( "encoding/json" + "math/big" "github.com/holiman/uint256" + "github.com/ledgerwatch/erigon-lib/chain" libcommon "github.com/ledgerwatch/erigon-lib/common" "github.com/ledgerwatch/erigon/core/state" + "github.com/ledgerwatch/erigon/core/types" "github.com/ledgerwatch/erigon/core/vm" + "github.com/ledgerwatch/erigon/core/vm/evmtypes" "github.com/ledgerwatch/erigon/eth/tracers" ) @@ -39,16 +43,15 @@ type borStateSyncTxnTracer struct { stateReceiverContractAddress libcommon.Address } -func (bsstt *borStateSyncTxnTracer) CaptureTxStart(_ uint64) { - bsstt.EVMLogger.CaptureTxStart(0) +func (bsstt *borStateSyncTxnTracer) CaptureTxStart(evm *vm.EVM, tx types.Transaction) { + bsstt.EVMLogger.CaptureTxStart(evm, tx) } -func (bsstt *borStateSyncTxnTracer) CaptureTxEnd(_ uint64) { - bsstt.EVMLogger.CaptureTxEnd(0) +func (bsstt *borStateSyncTxnTracer) CaptureTxEnd(receipt *types.Receipt, err error) { + bsstt.EVMLogger.CaptureTxEnd(receipt, err) } func (bsstt *borStateSyncTxnTracer) CaptureStart( - env *vm.EVM, from libcommon.Address, to libcommon.Address, precompile bool, @@ -63,7 +66,7 @@ func (bsstt *borStateSyncTxnTracer) CaptureStart( // perform a CaptureStart for the synthetic state sync transaction from := state.SystemAddress to := bsstt.stateReceiverContractAddress - bsstt.EVMLogger.CaptureStart(env, from, to, false, false, nil, 0, uint256.NewInt(0), nil) + bsstt.EVMLogger.CaptureStart(from, to, false, false, nil, 0, uint256.NewInt(0), nil) bsstt.captureStartCalledOnce = true } @@ -71,7 +74,7 @@ func (bsstt *borStateSyncTxnTracer) CaptureStart( bsstt.EVMLogger.CaptureEnter(vm.CALL, from, to, precompile, create, input, gas, value, code) } -func (bsstt *borStateSyncTxnTracer) CaptureEnd(output []byte, usedGas uint64, err error) { +func (bsstt *borStateSyncTxnTracer) CaptureEnd(output []byte, usedGas uint64, err error, reverted bool) { if bsstt.stateSyncEventsCount == 0 { // guard against unexpected use panic("unexpected extra call to borStateSyncTxnTracer.CaptureEnd") @@ -81,12 +84,12 @@ func (bsstt *borStateSyncTxnTracer) CaptureEnd(output []byte, usedGas uint64, er bsstt.stateSyncEventsCount-- // trick tracer to think it is a CaptureExit - bsstt.EVMLogger.CaptureExit(output, usedGas, err) + bsstt.EVMLogger.CaptureExit(output, usedGas, err, reverted) if bsstt.stateSyncEventsCount == 0 { // reached last event // perform a CaptureEnd for the synthetic state sync transaction - bsstt.EVMLogger.CaptureEnd(nil, 0, nil) + bsstt.EVMLogger.CaptureEnd(nil, 0, nil, reverted) } } @@ -132,3 +135,101 @@ func (bsstt *borStateSyncTxnTracer) Stop(err error) { panic("unexpected usage - borStateSyncTxnTracer.Stop called on a wrapped tracer which does not support it") } } + +// CaptureKeccakPreimage is called during the KECCAK256 opcode. +func (bsstt *borStateSyncTxnTracer) CaptureKeccakPreimage(hash libcommon.Hash, data []byte) { + if tracer, ok := bsstt.EVMLogger.(tracers.Tracer); ok { + tracer.CaptureKeccakPreimage(hash, data) + } else { + panic("unexpected usage - borStateSyncTxnTracer.CaptureKeccakPreimage called on a wrapped tracer which does not support it") + } +} + +// OnGasChange is called when gas is either consumed or refunded. +func (bsstt *borStateSyncTxnTracer) OnGasChange(old, new uint64, reason vm.GasChangeReason) { + if tracer, ok := bsstt.EVMLogger.(tracers.Tracer); ok { + tracer.OnGasChange(old, new, reason) + } else { + panic("unexpected usage - borStateSyncTxnTracer.OnGasChange called on a wrapped tracer which does not support it") + } +} + +func (bsstt *borStateSyncTxnTracer) OnBlockStart(b *types.Block, td *big.Int, finalized, safe *types.Header, chainConfig *chain.Config) { + if tracer, ok := bsstt.EVMLogger.(tracers.Tracer); ok { + tracer.OnBlockStart(b, td, finalized, safe, chainConfig) + } else { + panic("unexpected usage - borStateSyncTxnTracer.OnBlockStart called on a wrapped tracer which does not support it") + } +} + +func (bsstt *borStateSyncTxnTracer) OnBlockEnd(err error) { + if tracer, ok := bsstt.EVMLogger.(tracers.Tracer); ok { + tracer.OnBlockEnd(err) + } else { + panic("unexpected usage - borStateSyncTxnTracer.OnBlockEnd called on a wrapped tracer which does not support it") + } +} + +func (bsstt *borStateSyncTxnTracer) OnGenesisBlock(b *types.Block, alloc types.GenesisAlloc) { + if tracer, ok := bsstt.EVMLogger.(tracers.Tracer); ok { + tracer.OnGenesisBlock(b, alloc) + } else { + panic("unexpected usage - borStateSyncTxnTracer.OnGenesisBlock called on a wrapped tracer which does not support it") + } +} + +func (bsstt *borStateSyncTxnTracer) OnBeaconBlockRootStart(root libcommon.Hash) { + if tracer, ok := bsstt.EVMLogger.(tracers.Tracer); ok { + tracer.OnBeaconBlockRootStart(root) + } else { + panic("unexpected usage - borStateSyncTxnTracer.OnBeaconBlockRootStart called on a wrapped tracer which does not support it") + } +} + +func (bsstt *borStateSyncTxnTracer) OnBeaconBlockRootEnd() { + if tracer, ok := bsstt.EVMLogger.(tracers.Tracer); ok { + tracer.OnBeaconBlockRootEnd() + } else { + panic("unexpected usage - borStateSyncTxnTracer.OnBeaconBlockRootEnd called on a wrapped tracer which does not support it") + } +} + +func (bsstt *borStateSyncTxnTracer) OnBalanceChange(a libcommon.Address, prev, new *uint256.Int, reason evmtypes.BalanceChangeReason) { + if tracer, ok := bsstt.EVMLogger.(tracers.Tracer); ok { + tracer.OnBalanceChange(a, prev, new, reason) + } else { + panic("unexpected usage - borStateSyncTxnTracer.OnBalanceChange called on a wrapped tracer which does not support it") + } +} + +func (bsstt *borStateSyncTxnTracer) OnNonceChange(a libcommon.Address, prev, new uint64) { + if tracer, ok := bsstt.EVMLogger.(tracers.Tracer); ok { + tracer.OnNonceChange(a, prev, new) + } else { + panic("unexpected usage - borStateSyncTxnTracer.OnNonceChange called on a wrapped tracer which does not support it") + } +} + +func (bsstt *borStateSyncTxnTracer) OnCodeChange(a libcommon.Address, prevCodeHash libcommon.Hash, prev []byte, codeHash libcommon.Hash, code []byte) { + if tracer, ok := bsstt.EVMLogger.(tracers.Tracer); ok { + tracer.OnCodeChange(a, prevCodeHash, prev, codeHash, code) + } else { + panic("unexpected usage - borStateSyncTxnTracer.OnCodeChange called on a wrapped tracer which does not support it") + } +} + +func (bsstt *borStateSyncTxnTracer) OnStorageChange(a libcommon.Address, k *libcommon.Hash, prev, new uint256.Int) { + if tracer, ok := bsstt.EVMLogger.(tracers.Tracer); ok { + tracer.OnStorageChange(a, k, prev, new) + } else { + panic("unexpected usage - borStateSyncTxnTracer.OnStorageChange called on a wrapped tracer which does not support it") + } +} + +func (bsstt *borStateSyncTxnTracer) OnLog(log *types.Log) { + if tracer, ok := bsstt.EVMLogger.(tracers.Tracer); ok { + tracer.OnLog(log) + } else { + panic("unexpected usage - borStateSyncTxnTracer.OnLog called on a wrapped tracer which does not support it") + } +} diff --git a/turbo/transactions/tracing.go b/turbo/transactions/tracing.go index 06935d273b3..afb5d027a42 100644 --- a/turbo/transactions/tracing.go +++ b/turbo/transactions/tracing.go @@ -147,10 +147,17 @@ func TraceTx( execCb := func(evm *vm.EVM, refunds bool) (*core.ExecutionResult, error) { gp := new(core.GasPool).AddGas(message.Gas()).AddBlobGas(message.BlobGas()) - return core.ApplyMessage(evm, message, gp, refunds, false /* gasBailout */) + tracer.CaptureTxStart(evm, tx) + result, err := core.ApplyMessage(evm, message, gp, refunds, false /* gasBailout */) + if err != nil { + tracer.CaptureTxEnd(nil, err) + } else { + tracer.CaptureTxEnd(&types.Receipt{GasUsed: result.UsedGas}, nil) + } + return result, err } - return ExecuteTraceTx(tx, blockCtx, txCtx, ibs, config, chainConfig, stream, tracer, streaming, execCb) + return ExecuteTraceTx(blockCtx, txCtx, ibs, config, chainConfig, stream, tracer, streaming, execCb) } func AssembleTracer( @@ -199,7 +206,6 @@ func AssembleTracer( } func ExecuteTraceTx( - tx types.Transaction, blockCtx evmtypes.BlockContext, txCtx evmtypes.TxContext, ibs *state.IntraBlockState, @@ -224,10 +230,8 @@ func ExecuteTraceTx( stream.WriteArrayStart() } - tracer.CaptureTxStart(evm, tx) result, err := execCb(evm, refunds) if err != nil { - tracer.CaptureTxEnd(nil, err) if streaming { stream.WriteArrayEnd() stream.WriteObjectEnd() @@ -236,7 +240,7 @@ func ExecuteTraceTx( } return fmt.Errorf("tracing failed: %w", err) } - tracer.CaptureTxEnd(&types.Receipt{GasUsed: result.UsedGas}, nil) + // Depending on the tracer type, format and return the output if streaming { stream.WriteArrayEnd()