Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/erigon live tracer port #52

Draft
wants to merge 31 commits into
base: devel
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
cbd1eb3
evm tracer updated to track all events
dhyaniarun1993 Oct 23, 2023
a61d19d
test bugfixes
dhyaniarun1993 Oct 23, 2023
dce97d1
tracing added in jsonrpc
dhyaniarun1993 Oct 25, 2023
7e543c5
code refactor
dhyaniarun1993 Oct 30, 2023
e273070
precompiled contarct exposed to public
dhyaniarun1993 Oct 30, 2023
d61649b
fix vm bugs
dhyaniarun1993 Nov 2, 2023
d9dba75
new changes and unit test fixes
dhyaniarun1993 Nov 6, 2023
c297b8a
devel merged and conflict resolved
dhyaniarun1993 Nov 6, 2023
ceba8e3
refactor
dhyaniarun1993 Nov 6, 2023
13ac227
tracer flag name updated
dhyaniarun1993 Nov 7, 2023
0c765e7
bugfixes
dhyaniarun1993 Nov 28, 2023
5d64c1d
statedb: precompile check before onNewAccount
dhyaniarun1993 Dec 8, 2023
122ccec
fix setBalance reasons
dhyaniarun1993 Dec 8, 2023
97c69bc
fix VMError.Unwrap
dhyaniarun1993 Dec 8, 2023
9f876d2
rm extra galloc capturing
dhyaniarun1993 Dec 11, 2023
312a0ed
add hooks for beacon block root processing
dhyaniarun1993 Jan 9, 2024
1616e34
add chainConfig to block events
dhyaniarun1993 Jan 10, 2024
18d7da4
assign values for balance reasons
dhyaniarun1993 Jan 10, 2024
d4428fb
Track burnt and selfdestruct withdraw bal changes
dhyaniarun1993 Jan 10, 2024
306266f
rename and document balance change reasons
dhyaniarun1993 Jan 10, 2024
61c3eae
fix value for Delegate call
dhyaniarun1993 Jan 11, 2024
0c3a9f0
fix comment
dhyaniarun1993 Feb 12, 2024
26fc6e7
has call reverted to snapshot
dhyaniarun1993 Feb 12, 2024
edf048c
emit onNewAccount when reseting account
dhyaniarun1993 Feb 12, 2024
e3855f6
fix self destruct burn condition
dhyaniarun1993 Feb 12, 2024
45af942
remove precompile check for newAccount
dhyaniarun1993 Feb 13, 2024
79bf2dc
live tracer err -> warn
dhyaniarun1993 Feb 13, 2024
93b61c7
rm OnNewAccount
dhyaniarun1993 Feb 13, 2024
5234bb4
devel merged and conflict resolved
dhyaniarun1993 Feb 14, 2024
8de8a48
merge bugfixes
dhyaniarun1993 Feb 16, 2024
07ee59c
added tracer to missing stages
dhyaniarun1993 Feb 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
"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/event"
"github.com/ledgerwatch/erigon/params"
"github.com/ledgerwatch/erigon/turbo/services"
Expand Down Expand Up @@ -714,7 +715,7 @@ func (b *SimulatedBackend) callContract(_ context.Context, call ethereum.CallMsg
}
// Set infinite balance to the fake caller account.
from := statedb.GetOrNewStateObject(call.From)
from.SetBalance(uint256.NewInt(0).SetAllOne())
from.SetBalance(uint256.NewInt(0).SetAllOne(), evmtypes.BalanceChangeUnspecified)
// Execute the call.
msg := callMsg{call}

Expand Down
2 changes: 1 addition & 1 deletion cmd/caplin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func runCaplinNode(cliCtx *cli.Context) error {
log.Error("[Phase1] Could not initialize caplin", "err", err)
return err
}
if _, _, err := debug.Setup(cliCtx, true /* root logger */); err != nil {
if _, _, _, err := debug.Setup(cliCtx, true /* root logger */); err != nil {
return err
}
log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(cfg.LogLvl), log.StderrHandler))
Expand Down
6 changes: 4 additions & 2 deletions cmd/devnet/devnet/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/ledgerwatch/erigon/cmd/devnet/requests"
"github.com/ledgerwatch/erigon/diagnostics"
"github.com/ledgerwatch/erigon/eth/ethconfig"
"github.com/ledgerwatch/erigon/eth/tracers"
"github.com/ledgerwatch/erigon/node/nodecfg"
"github.com/ledgerwatch/erigon/params"
"github.com/ledgerwatch/erigon/turbo/debug"
Expand Down Expand Up @@ -138,6 +139,7 @@ func (n *devnetNode) EnableMetrics(int) {
// run configures, creates and serves an erigon node
func (n *devnetNode) run(ctx *cli.Context) error {
var logger log.Logger
var tracer tracers.Tracer
var err error
var metricsMux *http.ServeMux

Expand All @@ -152,7 +154,7 @@ func (n *devnetNode) run(ctx *cli.Context) error {
n.Unlock()
}()

if logger, metricsMux, err = debug.Setup(ctx, false /* rootLogger */); err != nil {
if logger, tracer, metricsMux, err = debug.Setup(ctx, false /* rootLogger */); err != nil {
return err
}

Expand Down Expand Up @@ -182,7 +184,7 @@ func (n *devnetNode) run(ctx *cli.Context) error {
logger.Warn("TODO: custom BorStateSyncDelay is not applied to BorConfig.StateSyncConfirmationDelay", "delay", stateSyncConfirmationDelay)
}

n.ethNode, err = enode.New(ctx.Context, n.nodeCfg, n.ethCfg, logger)
n.ethNode, err = enode.New(ctx.Context, n.nodeCfg, n.ethCfg, logger, tracer)

if metricsMux != nil {
diagnostics.Setup(ctx, metricsMux, n.ethNode)
Expand Down
6 changes: 4 additions & 2 deletions cmd/erigon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/ledgerwatch/erigon-lib/common/dbg"
"github.com/ledgerwatch/erigon-lib/metrics"
"github.com/ledgerwatch/erigon/diagnostics"
"github.com/ledgerwatch/erigon/eth/tracers"
"github.com/ledgerwatch/erigon/params"
erigonapp "github.com/ledgerwatch/erigon/turbo/app"
erigoncli "github.com/ledgerwatch/erigon/turbo/cli"
Expand Down Expand Up @@ -42,10 +43,11 @@ func main() {

func runErigon(cliCtx *cli.Context) error {
var logger log.Logger
var tracer tracers.Tracer
var err error
var metricsMux *http.ServeMux

if logger, metricsMux, err = debug.Setup(cliCtx, true /* root logger */); err != nil {
if logger, tracer, metricsMux, err = debug.Setup(cliCtx, true /* root logger */); err != nil {
return err
}

Expand All @@ -62,7 +64,7 @@ func runErigon(cliCtx *cli.Context) error {

ethCfg := node.NewEthConfigUrfave(cliCtx, nodeCfg, logger)

ethNode, err := node.New(cliCtx.Context, nodeCfg, ethCfg, logger)
ethNode, err := node.New(cliCtx.Context, nodeCfg, ethCfg, logger, tracer)
if err != nil {
log.Error("Erigon startup", "err", err)
return err
Expand Down
3 changes: 2 additions & 1 deletion cmd/evm/internal/t8ntool/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/ledgerwatch/erigon/consensus/ethash"
"github.com/ledgerwatch/erigon/core/state"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/core/vm/evmtypes"
"github.com/ledgerwatch/erigon/turbo/rpchelper"
)

Expand Down Expand Up @@ -83,7 +84,7 @@ func MakePreState(chainRules *chain.Rules, tx kv.RwTx, accounts types.GenesisAll
statedb.SetCode(addr, a.Code)
statedb.SetNonce(addr, a.Nonce)
balance, _ := uint256.FromBig(a.Balance)
statedb.SetBalance(addr, balance)
statedb.SetBalance(addr, balance, evmtypes.BalanceIncreaseGenesisBalance)
for k, v := range a.Storage {
key := k
val := uint256.NewInt(0).SetBytes(v.Bytes())
Expand Down
5 changes: 3 additions & 2 deletions cmd/evm/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import (
"github.com/ledgerwatch/erigon/core/state"
"github.com/ledgerwatch/erigon/core/vm"
"github.com/ledgerwatch/erigon/core/vm/runtime"
"github.com/ledgerwatch/erigon/eth/tracers"
"github.com/ledgerwatch/erigon/eth/tracers/logger"
"github.com/ledgerwatch/erigon/params"
)
Expand Down Expand Up @@ -134,7 +135,7 @@ func runCmd(ctx *cli.Context) error {
}

var (
tracer vm.EVMLogger
tracer tracers.Tracer
debugLogger *logger.StructLogger
statedb *state.IntraBlockState
chainConfig *chain.Config
Expand All @@ -154,7 +155,7 @@ func runCmd(ctx *cli.Context) error {
defer db.Close()
if ctx.String(GenesisFlag.Name) != "" {
gen := readGenesis(ctx.String(GenesisFlag.Name))
core.MustCommitGenesis(gen, db, "", log.Root())
core.MustCommitGenesis(gen, db, "", log.Root(), nil)
genesisConfig = gen
chainConfig = gen.Config
} else {
Expand Down
6 changes: 3 additions & 3 deletions cmd/integration/commands/stages.go
Original file line number Diff line number Diff line change
Expand Up @@ -1547,7 +1547,7 @@ func newDomains(ctx context.Context, db kv.RwDB, stepSize uint64, mode libstate.
//events := shards.NewEvents()
genesis := core.GenesisBlockByChainName(chain)

chainConfig, genesisBlock, genesisErr := core.CommitGenesisBlock(db, genesis, "", logger)
chainConfig, genesisBlock, genesisErr := core.CommitGenesisBlock(db, genesis, "", logger, nil)
_ = genesisBlock // TODO apply if needed here

if _, ok := genesisErr.(*chain2.ConfigCompatError); genesisErr != nil && !ok {
Expand Down Expand Up @@ -1587,7 +1587,7 @@ func newSync(ctx context.Context, db kv.RwDB, miningConfig *params.MiningConfig,
events := shards.NewEvents()

genesis := core.GenesisBlockByChainName(chain)
chainConfig, genesisBlock, genesisErr := core.CommitGenesisBlock(db, genesis, "", logger)
chainConfig, genesisBlock, genesisErr := core.CommitGenesisBlock(db, genesis, "", logger, nil)
if _, ok := genesisErr.(*chain2.ConfigCompatError); genesisErr != nil && !ok {
panic(genesisErr)
}
Expand Down Expand Up @@ -1649,7 +1649,7 @@ func newSync(ctx context.Context, db kv.RwDB, miningConfig *params.MiningConfig,
signatures = bor.Signatures
}
stages := stages2.NewDefaultStages(context.Background(), db, snapDb, p2p.Config{}, &cfg, sentryControlServer, notifications, nil, blockReader, blockRetire, agg, nil, nil,
heimdallClient, recents, signatures, logger)
heimdallClient, recents, signatures, logger, nil)
sync := stagedsync.New(cfg.Sync, stages, stagedsync.DefaultUnwindOrder, stagedsync.DefaultPruneOrder, logger)

miner := stagedsync.NewMiningState(&cfg.Miner)
Expand Down
2 changes: 1 addition & 1 deletion cmd/rpcdaemon/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ func (e *remoteConsensusEngine) Initialize(config *chain.Config, chain consensus
panic(err)
}

e.engine.Initialize(config, chain, header, state, syscall, logger)
e.engine.Initialize(config, chain, header, state, syscall, logger, nil)
}

func (e *remoteConsensusEngine) VerifyHeader(_ consensus.ChainHeaderReader, _ *types.Header, _ bool) error {
Expand Down
2 changes: 1 addition & 1 deletion cmd/silkworm_api/snapshot_idx.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func FindIf(segments []snaptype.FileInfo, predicate func(snaptype.FileInfo) bool
}

func buildIndex(cliCtx *cli.Context, dataDir string, snapshotPaths []string, minBlock uint64) error {
logger, _, err := debug.Setup(cliCtx, true /* rootLogger */)
logger, _, _, err := debug.Setup(cliCtx, true /* rootLogger */)
if err != nil {
return err
}
Expand Down
57 changes: 50 additions & 7 deletions cmd/state/commands/opcode_tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/gob"
"encoding/json"
"fmt"
"math/big"
"os"
"os/signal"
"strconv"
Expand All @@ -30,6 +31,7 @@ import (
"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/ethconfig"
"github.com/ledgerwatch/erigon/turbo/rpchelper"
"github.com/ledgerwatch/erigon/turbo/snapshotsync/freezeblocks"
Expand Down Expand Up @@ -162,9 +164,12 @@ type blockTxs struct {
Txs slicePtrTx
}

func (ot *opcodeTracer) CaptureTxStart(gasLimit uint64) {}
func (ot *opcodeTracer) CaptureTxStart(env *vm.EVM, tx types.Transaction) {
ot.env = env
ot.depth = 0
}

func (ot *opcodeTracer) CaptureTxEnd(restGas uint64) {}
func (ot *opcodeTracer) CaptureTxEnd(receipt *types.Receipt, err error) {}

func (ot *opcodeTracer) captureStartOrEnter(from, to libcommon.Address, create bool, input []byte) {
//fmt.Fprint(ot.summary, ot.lastLine)
Expand Down Expand Up @@ -194,8 +199,7 @@ func (ot *opcodeTracer) captureStartOrEnter(from, to libcommon.Address, create b
ot.stack = append(ot.stack, &newTx)
}

func (ot *opcodeTracer) CaptureStart(env *vm.EVM, from libcommon.Address, to libcommon.Address, precompile bool, create bool, input []byte, gas uint64, value *uint256.Int, code []byte) {
ot.env = env
func (ot *opcodeTracer) CaptureStart(from libcommon.Address, to libcommon.Address, precompile bool, create bool, input []byte, gas uint64, value *uint256.Int, code []byte) {
ot.depth = 0
ot.captureStartOrEnter(from, to, create, input)
}
Expand Down Expand Up @@ -237,11 +241,11 @@ func (ot *opcodeTracer) captureEndOrExit(err error) {
}
}

func (ot *opcodeTracer) CaptureEnd(output []byte, usedGas uint64, err error) {
func (ot *opcodeTracer) CaptureEnd(output []byte, usedGas uint64, err error, reverted bool) {
ot.captureEndOrExit(err)
}

func (ot *opcodeTracer) CaptureExit(output []byte, usedGas uint64, err error) {
func (ot *opcodeTracer) CaptureExit(output []byte, usedGas uint64, err error, reverted bool) {
ot.captureEndOrExit(err)
ot.depth--
}
Expand Down Expand Up @@ -388,6 +392,45 @@ func (ot *opcodeTracer) CaptureFault(pc uint64, op vm.OpCode, gas, cost uint64,

}

func (ot *opcodeTracer) OnBlockStart(b *types.Block, td *big.Int, finalized, safe *types.Header, chainConfig *chain2.Config) {
}

func (ot *opcodeTracer) OnBlockEnd(err error) {
}

func (ot *opcodeTracer) OnGenesisBlock(b *types.Block, alloc types.GenesisAlloc) {
}

func (ot *opcodeTracer) OnBeaconBlockRootStart(root libcommon.Hash) {}

func (ot *opcodeTracer) OnBeaconBlockRootEnd() {}

func (ot *opcodeTracer) CaptureKeccakPreimage(hash libcommon.Hash, data []byte) {}

func (ot *opcodeTracer) OnGasChange(old, new uint64, reason vm.GasChangeReason) {}

func (ot *opcodeTracer) OnBalanceChange(a libcommon.Address, prev, new *uint256.Int, reason evmtypes.BalanceChangeReason) {
}

func (ot *opcodeTracer) OnNonceChange(a libcommon.Address, prev, new uint64) {}

func (ot *opcodeTracer) OnCodeChange(a libcommon.Address, prevCodeHash libcommon.Hash, prev []byte, codeHash libcommon.Hash, code []byte) {
}

func (ot *opcodeTracer) OnStorageChange(a libcommon.Address, k *libcommon.Hash, prev, new uint256.Int) {
}

func (ot *opcodeTracer) OnLog(log *types.Log) {}

// GetResult returns an empty json object.
func (ot *opcodeTracer) GetResult() (json.RawMessage, error) {
return json.RawMessage(`{}`), nil
}

// Stop terminates execution of the tracer at the first opportune moment.
func (ot *opcodeTracer) Stop(err error) {
}

type segPrefix struct {
BlockNum uint64
NumTxs uint
Expand Down Expand Up @@ -712,7 +755,7 @@ func runBlock(engine consensus.Engine, ibs *state.IntraBlockState, txnWriter sta
usedGas := new(uint64)
usedBlobGas := new(uint64)
var receipts types.Receipts
core.InitializeBlockExecution(engine, nil, header, chainConfig, ibs, logger)
core.InitializeBlockExecution(engine, nil, header, chainConfig, ibs, logger, nil)
rules := chainConfig.Rules(block.NumberU64(), block.Time())
for i, tx := range block.Transactions() {
ibs.SetTxContext(tx.Hash(), block.Hash(), i)
Expand Down
2 changes: 1 addition & 1 deletion cmd/state/commands/state_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func StateRoot(ctx context.Context, genesis *types.Genesis, blockNum uint64, dat
if rwTx, err = db.BeginRw(ctx); err != nil {
return err
}
_, genesisIbs, err4 := core.GenesisToBlock(genesis, "", logger)
_, genesisIbs, err4 := core.GenesisToBlock(genesis, "", logger, nil)
if err4 != nil {
return err4
}
Expand Down
57 changes: 52 additions & 5 deletions cmd/state/exec3/calltracer_v3.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package exec3

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/types"
"github.com/ledgerwatch/erigon/core/vm"
"github.com/ledgerwatch/erigon/core/vm/evmtypes"
)

type CallTracer struct {
Expand All @@ -20,9 +26,11 @@ func (ct *CallTracer) Reset() {
func (ct *CallTracer) Froms() map[libcommon.Address]struct{} { return ct.froms }
func (ct *CallTracer) Tos() map[libcommon.Address]struct{} { return ct.tos }

func (ct *CallTracer) CaptureTxStart(gasLimit uint64) {}
func (ct *CallTracer) CaptureTxEnd(restGas uint64) {}
func (ct *CallTracer) CaptureStart(env *vm.EVM, from libcommon.Address, to libcommon.Address, precompile bool, create bool, input []byte, gas uint64, value *uint256.Int, code []byte) {
func (ct *CallTracer) CaptureTxStart(env *vm.EVM, tx types.Transaction) {}

func (ct *CallTracer) CaptureTxEnd(receipt *types.Receipt, err error) {}

func (ct *CallTracer) CaptureStart(from libcommon.Address, to libcommon.Address, precompile bool, create bool, input []byte, gas uint64, value *uint256.Int, code []byte) {
if ct.froms == nil {
ct.froms = map[libcommon.Address]struct{}{}
ct.tos = map[libcommon.Address]struct{}{}
Expand All @@ -40,7 +48,46 @@ func (ct *CallTracer) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64, sc
}
func (ct *CallTracer) CaptureFault(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, depth int, err error) {
}
func (ct *CallTracer) CaptureEnd(output []byte, usedGas uint64, err error) {
func (ct *CallTracer) CaptureEnd(output []byte, usedGas uint64, err error, reverted bool) {
}
func (ct *CallTracer) CaptureExit(output []byte, usedGas uint64, err error, reverted bool) {
}
func (ct *CallTracer) CaptureExit(output []byte, usedGas uint64, err error) {

func (ct *CallTracer) OnBlockStart(b *types.Block, td *big.Int, finalized, safe *types.Header, chainConfig *chain.Config) {
}

func (ct *CallTracer) OnBlockEnd(err error) {
}

func (ct *CallTracer) OnGenesisBlock(b *types.Block, alloc types.GenesisAlloc) {
}

func (ct *CallTracer) OnBeaconBlockRootStart(root libcommon.Hash) {}

func (ct *CallTracer) OnBeaconBlockRootEnd() {}

func (ct *CallTracer) CaptureKeccakPreimage(hash libcommon.Hash, data []byte) {}

func (ct *CallTracer) OnGasChange(old, new uint64, reason vm.GasChangeReason) {}

func (ct *CallTracer) OnBalanceChange(a libcommon.Address, prev, new *uint256.Int, reason evmtypes.BalanceChangeReason) {
}

func (ct *CallTracer) OnNonceChange(a libcommon.Address, prev, new uint64) {}

func (ct *CallTracer) OnCodeChange(a libcommon.Address, prevCodeHash libcommon.Hash, prev []byte, codeHash libcommon.Hash, code []byte) {
}

func (ct *CallTracer) OnStorageChange(a libcommon.Address, k *libcommon.Hash, prev, new uint256.Int) {
}

func (ct *CallTracer) OnLog(log *types.Log) {}

// GetResult returns an empty json object.
func (ct *CallTracer) GetResult() (json.RawMessage, error) {
return json.RawMessage(`{}`), nil
}

// Stop terminates execution of the tracer at the first opportune moment.
func (ct *CallTracer) Stop(err error) {
}
Loading
Loading