From 2c0db83037c50c3198e2172138e7dd4902df441d Mon Sep 17 00:00:00 2001 From: Rahul Tripathi Date: Tue, 18 Jun 2024 21:56:07 +0530 Subject: [PATCH] ft: update tracer --- entity/transactions.go | 1 + executor/executor.go | 17 +++++++++-------- services/eth_rpc.go | 9 ++++----- tests/execute_test.go | 12 ++++++------ 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/entity/transactions.go b/entity/transactions.go index 9fa62de..c512470 100644 --- a/entity/transactions.go +++ b/entity/transactions.go @@ -104,6 +104,7 @@ func NewTransactionStorage() *TransactionStorage { return &TransactionStorage{ txs: make(map[common.Hash]*types.Transaction), receipts: make(map[common.Hash]*types.Receipt), + traces: make(map[common.Hash]TransactionTraces), } } diff --git a/executor/executor.go b/executor/executor.go index 22c7187..ebbc96b 100644 --- a/executor/executor.go +++ b/executor/executor.go @@ -9,7 +9,6 @@ import ( "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/tracing" "github.com/holiman/uint256" "github.com/rahul0tripathi/smelter/config" "github.com/rahul0tripathi/smelter/entity" @@ -57,14 +56,14 @@ func NewExecutor( func (e *SerialExecutor) CallAndPersist( ctx context.Context, tx ethereum.CallMsg, - hooks *tracing.Hooks, + tracer entity.TraceProvider, overrides entity.StateOverrides, ) (txHash *common.Hash, ret []byte, leftOverGas uint64, err error) { e.mu.Lock() defer e.mu.Unlock() executionDB := statedb.NewDB(ctx, e.db) - chainCfg, evmCfg := e.cfg.ExecutionConfig(hooks) + chainCfg, evmCfg := e.cfg.ExecutionConfig(tracer.Hooks()) if err = executionDB.ApplyOverrides(overrides); err != nil { return nil, nil, 0, err } @@ -88,7 +87,7 @@ func (e *SerialExecutor) CallAndPersist( return } - txHash = e.roll(ctx, tx, leftOverGas, executionDB) + txHash = e.roll(ctx, tx, leftOverGas, executionDB, tracer) return } @@ -97,6 +96,7 @@ func (e *SerialExecutor) roll( msg ethereum.CallMsg, left uint64, executionDB *statedb.StateDB, + traceProvider entity.TraceProvider, ) *common.Hash { e.db.ApplyStorage(executionDB.Dirty().GetAccountStorage()) e.db.ApplyState(executionDB.Dirty().GetAccountState()) @@ -127,6 +127,7 @@ func (e *SerialExecutor) roll( e.prevBlockHash = hash e.prevBlockNum = block.Uint64() + e.txn.AddTrace(tx.Hash(), traceProvider.OtterTrace()) txHash := tx.Hash() return &txHash @@ -135,7 +136,7 @@ func (e *SerialExecutor) roll( func (e *SerialExecutor) Call( ctx context.Context, tx ethereum.CallMsg, - hooks *tracing.Hooks, + tracer entity.TraceProvider, overrides entity.StateOverrides, ) (ret []byte, leftOverGas uint64, err error) { e.mu.Lock() @@ -146,7 +147,7 @@ func (e *SerialExecutor) Call( return nil, 0, err } - chainCfg, evmCfg := e.cfg.ExecutionConfig(hooks) + chainCfg, evmCfg := e.cfg.ExecutionConfig(tracer.Hooks()) env := vm.NewEVM(e.cfg.BlockContext(new(big.Int).Add(e.cfg.ForkConfig.ForkBlock, new(big.Int).SetUint64(1)), new(big.Int), uint64(time.Now().Unix())), @@ -169,7 +170,7 @@ func (e *SerialExecutor) Call( func (e *SerialExecutor) CallWithDB( ctx context.Context, tx ethereum.CallMsg, - hooks *tracing.Hooks, + tracer entity.TraceProvider, db *fork.DB, overrides entity.StateOverrides, ) (ret []byte, leftOverGas uint64, err error) { @@ -181,7 +182,7 @@ func (e *SerialExecutor) CallWithDB( return nil, 0, err } - chainCfg, evmCfg := e.cfg.ExecutionConfig(hooks) + chainCfg, evmCfg := e.cfg.ExecutionConfig(tracer.Hooks()) env := vm.NewEVM(e.cfg.BlockContext(new(big.Int).Add(e.cfg.ForkConfig.ForkBlock, new(big.Int).SetUint64(1)), new(big.Int), uint64(time.Now().Unix())), diff --git a/services/eth_rpc.go b/services/eth_rpc.go index ca4a7de..f568bb5 100644 --- a/services/eth_rpc.go +++ b/services/eth_rpc.go @@ -183,7 +183,7 @@ func (r *EthRpc) Call( call, err := createEthCallMsg(msg) t := tracer.NewTracer(false) if block.Uint64() == latest { - ret, _, err := execCtx.Executor.Call(ctx, call, t.Hooks(), entity.StateOverrides{}) + ret, _, err := execCtx.Executor.Call(ctx, call, t, entity.StateOverrides{}) if err != nil { return "0x", err } @@ -198,7 +198,7 @@ func (r *EthRpc) Call( } db := fork.NewDB(r.readerAndCaller, r.cfg, storage.Accounts, storage.State) - ret, _, err := execCtx.Executor.CallWithDB(ctx, call, t.Hooks(), db, entity.StateOverrides{}) + ret, _, err := execCtx.Executor.CallWithDB(ctx, call, t, db, entity.StateOverrides{}) if err != nil { return "0x", err } @@ -249,13 +249,12 @@ func (r *EthRpc) SendRawTransaction( Data: tx.Data(), } - txHash, _, _, err := execCtx.Executor.CallAndPersist(ctx, msg, t.Hooks(), entity.StateOverrides{}) + txHash, _, _, err := execCtx.Executor.CallAndPersist(ctx, msg, t, execCtx.Overrides) + fmt.Println(t.Fmt()) if err != nil { return "0x", err } - fmt.Println(t.Fmt()) - return txHash.Hex(), nil } diff --git a/tests/execute_test.go b/tests/execute_test.go index b284438..104fef6 100644 --- a/tests/execute_test.go +++ b/tests/execute_test.go @@ -49,7 +49,7 @@ func TestExecuteE2E(t *testing.T) { Data: deposit, Gas: 30000000, Value: new(big.Int).SetInt64(6969), - }, stateTracer.Hooks(), map[common.Address]entity.StateOverride{ + }, stateTracer, map[common.Address]entity.StateOverride{ sender: {Balance: abi.MaxUint256}, }) require.NoError(t, err, "failed to deposit") @@ -63,7 +63,7 @@ func TestExecuteE2E(t *testing.T) { Data: balanceOfx06, Gas: 30000000, Value: new(big.Int).SetInt64(0), - }, stateTracer.Hooks(), nil) + }, stateTracer, nil) require.NoError(t, err, "failed to read 0x6 balance") require.Equal(t, new(big.Int).SetBytes(ret).Int64(), int64(6969), "invalid 0x6 balance received pre transfer") @@ -76,7 +76,7 @@ func TestExecuteE2E(t *testing.T) { Data: transferCall, Gas: 30000000, Value: new(big.Int).SetInt64(0), - }, stateTracer.Hooks(), map[common.Address]entity.StateOverride{ + }, stateTracer, map[common.Address]entity.StateOverride{ sender: {Balance: abi.MaxUint256}, }) require.NoError(t, err, "failed to transfer weth") @@ -90,7 +90,7 @@ func TestExecuteE2E(t *testing.T) { Data: balanceOfx07, Gas: 30000000, Value: new(big.Int).SetInt64(0), - }, stateTracer.Hooks(), nil) + }, stateTracer, nil) require.NoError(t, err, "failed to read 0x7 balance") require.Equal(t, new(big.Int).SetBytes(ret).Int64(), int64(6967), "invalid 0x7 balance received post transfer") @@ -101,7 +101,7 @@ func TestExecuteE2E(t *testing.T) { Data: balanceOfx06, Gas: 30000000, Value: new(big.Int).SetInt64(0), - }, stateTracer.Hooks(), nil) + }, stateTracer, nil) require.NoError(t, err, "failed to read 0x6 balance") require.Equal(t, new(big.Int).SetBytes(ret).Int64(), int64(2), "invalid 0x6 balance received post transfer") } @@ -136,7 +136,7 @@ func TestBlockProduction(t *testing.T) { Gas: 30000000, Value: new(big.Int).SetInt64(6969), } - hash, _, _, err := exec.CallAndPersist(ctx, msg, stateTracer.Hooks(), map[common.Address]entity.StateOverride{ + hash, _, _, err := exec.CallAndPersist(ctx, msg, stateTracer, map[common.Address]entity.StateOverride{ sender: {Balance: abi.MaxUint256}, }) require.NoError(t, err, "failed to deposit")