Skip to content

Commit

Permalink
merge bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dhyaniarun1993 committed Feb 16, 2024
1 parent 5234bb4 commit 8de8a48
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 17 deletions.
28 changes: 28 additions & 0 deletions consensus/mock/chain_header_reader_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions eth/tracers/native/prestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
Expand Down
119 changes: 110 additions & 9 deletions polygon/tracer/bor_state_sync_txn_tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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,
Expand All @@ -63,15 +66,15 @@ 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
}

// trick the tracer to think it is a CaptureEnter
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")
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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")
}
}
16 changes: 10 additions & 6 deletions turbo/transactions/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -199,7 +206,6 @@ func AssembleTracer(
}

func ExecuteTraceTx(
tx types.Transaction,
blockCtx evmtypes.BlockContext,
txCtx evmtypes.TxContext,
ibs *state.IntraBlockState,
Expand All @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit 8de8a48

Please sign in to comment.