Skip to content

Commit

Permalink
Update go.mod
Browse files Browse the repository at this point in the history
  • Loading branch information
Austin Roberts committed Oct 2, 2023
2 parents 8e0c77d + 500d12d commit da6675b
Show file tree
Hide file tree
Showing 54 changed files with 4,110 additions and 2,180 deletions.
2 changes: 1 addition & 1 deletion core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func ApplyTransactionWithResult(config *params.ChainConfig, bc ChainContext, aut

func ApplyUnsignedTransactionWithResult(config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool, statedb *state.StateDB, header *types.Header, msg *Message, usedGas *uint64, cfg vm.Config) (*types.Receipt, *ExecutionResult, interface{}, error) {
// Create a blocknative tracer to get execution traces.
tracer, err := blocknative.NewTxnOpCodeTracer(nil)
tracer, err := blocknative.NewTracer(nil)
if err != nil {
return nil, nil, nil, err
}
Expand Down
71 changes: 64 additions & 7 deletions eth/filters/trace_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import (
"encoding/json"
"errors"
"fmt"
"math/big"
"sync"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/consensus/misc/eip1559"
Expand All @@ -14,10 +18,10 @@ import (
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/eth/tracers"
"github.com/ethereum/go-ethereum/eth/tracers/blocknative"
"github.com/ethereum/go-ethereum/eth/tracers/blocknative/cache"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc"
"math/big"
)

var defaultTxTraceOpts = blocknative.TracerOpts{
Expand Down Expand Up @@ -77,34 +81,43 @@ func (api *FilterAPI) NewPendingTransactionsWithTrace(ctx context.Context, trace
tracedTxs = make([]*RPCTransaction, 0, len(txs))
blockNumber = hexutil.Big(*header.Number)
blockHash = header.Hash()
txIndex = hexutil.Uint64(0)
)

statedb, err := api.sys.chain.State()
if err != nil {
log.Error("NewPendingTransactionsWithTrace failed to get state", "err", err)
log.Error("failed to get state", "err", err)
return
}

for _, tx := range txs {
msg, _ = core.TransactionToMessage(tx, signer, header.BaseFee)
if err != nil {
log.Error("NewPendingTransactionsWithTrace failed to create tx message", "err", err, "tx", tx.Hash())
log.Error("failed to create tx message", "err", err, "tx", tx.Hash())
continue
}

traceCtx.TxHash = tx.Hash()
trace, err := traceTx(msg, traceCtx, blockCtx, chainConfig, statedb, tracerOpts)
if err != nil {
log.Error("NewPendingTransactionsWithTrace failed to trace tx", "err", err, "tx", tx.Hash())
log.Error("failed to trace tx", "err", err, "tx", tx.Hash())
continue
}

gasPrice := hexutil.Big(*tx.GasPrice())
rpcTx := newRPCPendingTransaction(tx)
rpcTx.BlockHash = &blockHash
rpcTx.BlockNumber = &blockNumber
rpcTx.TransactionIndex = &txIndex
rpcTx.Trace = trace
rpcTx.GasPrice = &gasPrice
tracedTxs = append(tracedTxs, rpcTx)
}

if len(tracedTxs) == 0 {
continue
}

notifier.Notify(rpcSub.ID, tracedTxs)
case <-rpcSub.Err():
return
Expand Down Expand Up @@ -218,21 +231,65 @@ func (api *FilterAPI) NewFullBlocksWithTrace(ctx context.Context, tracerOptsJSON
return rpcSub, nil
}

var (
txTraceLocksMu sync.RWMutex
txTraceLocks = make(map[common.Hash]chan struct{})
txTraceLocksTimeout = 1 * time.Second
)

// traceTx traces a transaction with the given contexts.
func traceTx(message *core.Message, txCtx *tracers.Context, vmctx vm.BlockContext, chainConfig *params.ChainConfig, statedb *state.StateDB, tracerOpts blocknative.TracerOpts) (*blocknative.Trace, error) {
tracer, err := blocknative.NewTxnOpCodeTracerWithOpts(tracerOpts)
// Check cached trace or an in-progress trace before executing.
trace, ok := cache.GetTrace(txCtx.TxHash, txCtx.BlockHash)
if ok {
return trace, nil
}

// Check if there is an in-progress trace for this transaction.
// If there is then wait for it to finish and return it.
// If there is not then lock the trace and create a new one.
// Don't wait longer than a second for the cache to finish.
txTraceLocksMu.Lock()
if unlockCh, ok := txTraceLocks[txCtx.TxHash]; ok {
timeOut := time.NewTimer(txTraceLocksTimeout)
select {
case <-unlockCh:
if trace, ok := cache.GetTrace(txCtx.TxHash, txCtx.BlockHash); ok {
txTraceLocksMu.Unlock()
return trace, nil
}
case <-timeOut.C:
}
}
unlockCh := make(chan struct{})
txTraceLocks[txCtx.TxHash] = unlockCh
txTraceLocksMu.Unlock()

// No trace in cache or in-progress so create a new one.
tracer, err := blocknative.NewTracerWithOpts(tracerOpts)
if err != nil {
return nil, err
}

txContext := core.NewEVMTxContext(message)
vmenv := vm.NewEVM(vmctx, txContext, statedb, chainConfig, vm.Config{Tracer: tracer})
statedb.SetTxContext(txCtx.TxHash, txCtx.TxIndex)

if _, err = core.ApplyMessage(vmenv, message, new(core.GasPool).AddGas(message.GasLimit)); err != nil {
return nil, fmt.Errorf("tracing failed: %w", err)
}
return tracer.GetTrace()
trace, err = tracer.GetTrace()
if err != nil {
return nil, err
}

// Cache the result and unlock/delete the trace lock.
cache.PutTrace(txCtx.TxHash, txCtx.BlockHash, trace)
close(unlockCh)
txTraceLocksMu.Lock()
delete(txTraceLocks, txCtx.TxHash)
txTraceLocksMu.Unlock()

return trace, err
}

// traceBlock traces all transactions in a block.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# txnOpCodeTracer
# Blocknative Tracer

## Options

Expand Down
129 changes: 0 additions & 129 deletions eth/tracers/blocknative/balance_changes.go

This file was deleted.

Loading

0 comments on commit da6675b

Please sign in to comment.