mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-23 21:26:42 +00:00
eth/tracer,rpc: use build-in flatCallTracer (#1)
* eth/tracer,rpc: use build-in flatCallTracer
This commit is contained in:
parent
68ecc855a0
commit
d983fd998b
17 changed files with 173 additions and 199 deletions
8
.github/workflows/lint.yml
vendored
8
.github/workflows/lint.yml
vendored
|
|
@ -35,10 +35,10 @@ jobs:
|
|||
run: |
|
||||
git config --global url."https://${{ steps.generate-token.outputs.token }}:x-oauth-basic@github.com/DeBankDeFi".insteadOf "https://github.com/DeBankDeFi"
|
||||
|
||||
- name: Golangci-lint
|
||||
uses: golangci/golangci-lint-action@v2
|
||||
- name: golangci-lint
|
||||
uses: golangci/golangci-lint-action@v3
|
||||
with:
|
||||
version: v1.46.2
|
||||
version: v1.53
|
||||
only-new-issues: true
|
||||
skip-pkg-cache: true
|
||||
skip-build-cache: true
|
||||
skip-build-cache: true
|
||||
|
|
@ -49,8 +49,6 @@ import (
|
|||
"github.com/ethereum/go-ethereum/rlp"
|
||||
"github.com/ethereum/go-ethereum/trie"
|
||||
"github.com/ethereum/go-ethereum/txtrace"
|
||||
|
||||
txtracelib "github.com/DeBankDeFi/etherlib/pkg/txtracev2"
|
||||
)
|
||||
|
||||
var (
|
||||
|
|
@ -175,9 +173,8 @@ var defaultCacheConfig = &CacheConfig{
|
|||
type BlockChain struct {
|
||||
chainConfig *params.ChainConfig // Chain & network configuration
|
||||
cacheConfig *CacheConfig // Cache configuration for pruning
|
||||
txTraceConfig *txtrace.Config
|
||||
// txtraceStore
|
||||
txTraceStore txtracelib.Store
|
||||
txTraceConfig *txtrace.Config // Config for transaction tracing
|
||||
txTraceDb ethdb.Database // Db for transaction tracing
|
||||
|
||||
db ethdb.Database // Low level persistent database to store final content in
|
||||
snaps *snapshot.Tree // Snapshot tree for fast trie leaf access
|
||||
|
|
@ -239,13 +236,13 @@ type BlockChain struct {
|
|||
// NewBlockChainV2 returns a fully initialised blockchain using information
|
||||
// available in the database. It initialises the default Ethereum Validator and
|
||||
// Processor. The different between NewBlockChainV2 and NewBlockchain are additional txtrace.Config and txtracelib.Store parameter will passed.
|
||||
func NewBlockChainV2(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis, overrides *ChainOverrides, engine consensus.Engine, vmConfig vm.Config, shouldPreserve func(block *types.Header) bool, txLookupLimit *uint64, txTraceConfig *txtrace.Config, txStore txtracelib.Store) (*BlockChain, error) {
|
||||
func NewBlockChainV2(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis, overrides *ChainOverrides, engine consensus.Engine, vmConfig vm.Config, shouldPreserve func(block *types.Header) bool, txLookupLimit *uint64, txTraceConfig *txtrace.Config, txStore ethdb.Database) (*BlockChain, error) {
|
||||
bc, err := NewBlockChain(db, cacheConfig, genesis, overrides, engine, vmConfig, shouldPreserve, txLookupLimit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bc.txTraceConfig = txTraceConfig
|
||||
bc.txTraceStore = txStore
|
||||
bc.txTraceDb = txStore
|
||||
return bc, nil
|
||||
}
|
||||
|
||||
|
|
@ -481,10 +478,6 @@ func (bc *BlockChain) empty() bool {
|
|||
return true
|
||||
}
|
||||
|
||||
func (bc *BlockChain) tracingTx() bool {
|
||||
return bc.txTraceConfig != nil && bc.txTraceConfig.Enabled
|
||||
}
|
||||
|
||||
// loadLastState loads the last known chain state from the database. This method
|
||||
// assumes that the chain manager mutex is held.
|
||||
func (bc *BlockChain) loadLastState() error {
|
||||
|
|
@ -1792,7 +1785,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals, setHead bool)
|
|||
|
||||
// Process block using the parent state as reference point
|
||||
pstart := time.Now()
|
||||
receipts, logs, usedGas, err := bc.processor.Process(block, statedb, bc.vmConfig, bc.tracingTx())
|
||||
receipts, logs, usedGas, err := bc.processor.Process(block, statedb, bc.vmConfig)
|
||||
if err != nil {
|
||||
bc.reportBlock(block, receipts, err)
|
||||
followupInterrupt.Store(true)
|
||||
|
|
@ -2524,8 +2517,8 @@ func (bc *BlockChain) InsertHeaderChain(chain []*types.Header, checkFreq int) (i
|
|||
return 0, err
|
||||
}
|
||||
|
||||
// TxTraceStore retrieves the blockchain's tx-trace store.
|
||||
func (bc *BlockChain) TxTraceStore() txtracelib.Store { return bc.txTraceStore }
|
||||
// TxTraceDB retrieves the blockchain's tx-trace db.
|
||||
func (bc *BlockChain) TxTraceDB() ethdb.Database { return bc.txTraceDb }
|
||||
|
||||
// SetBlockValidatorAndProcessorForTesting sets the current validator and processor.
|
||||
// This method can be used to force an invalid blockchain to be verified for tests.
|
||||
|
|
|
|||
|
|
@ -162,7 +162,7 @@ func testBlockChainImport(chain types.Blocks, blockchain *BlockChain) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
receipts, _, usedGas, err := blockchain.processor.Process(block, statedb, vm.Config{}, false)
|
||||
receipts, _, usedGas, err := blockchain.processor.Process(block, statedb, vm.Config{})
|
||||
if err != nil {
|
||||
blockchain.reportBlock(block, receipts, err)
|
||||
return err
|
||||
|
|
@ -792,7 +792,7 @@ func TestFastVsFullChains(t *testing.T) {
|
|||
t.Fatalf("failed to insert receipt %d: %v", n, err)
|
||||
}
|
||||
// Freezer style fast import the chain.
|
||||
ancientDb, err := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), t.TempDir(), "", false)
|
||||
ancientDb, err := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), t.TempDir(), "", false, false)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create temp freezer db: %v", err)
|
||||
}
|
||||
|
|
@ -881,7 +881,7 @@ func TestLightVsFastVsFullChainHeads(t *testing.T) {
|
|||
|
||||
// makeDb creates a db instance for testing.
|
||||
makeDb := func() ethdb.Database {
|
||||
db, err := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), t.TempDir(), "", false)
|
||||
db, err := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), t.TempDir(), "", false, false)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create temp freezer db: %v", err)
|
||||
}
|
||||
|
|
@ -1770,7 +1770,7 @@ func TestBlockchainRecovery(t *testing.T) {
|
|||
_, blocks, receipts := GenerateChainWithGenesis(gspec, ethash.NewFaker(), int(height), nil)
|
||||
|
||||
// Import the chain as a ancient-first node and ensure all pointers are updated
|
||||
ancientDb, err := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), t.TempDir(), "", false)
|
||||
ancientDb, err := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), t.TempDir(), "", false, false)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create temp freezer db: %v", err)
|
||||
}
|
||||
|
|
@ -1836,7 +1836,7 @@ func TestInsertReceiptChainRollback(t *testing.T) {
|
|||
}
|
||||
|
||||
// Set up a BlockChain that uses the ancient store.
|
||||
ancientDb, err := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), t.TempDir(), "", false)
|
||||
ancientDb, err := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), t.TempDir(), "", false, false)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create temp freezer db: %v", err)
|
||||
}
|
||||
|
|
@ -2106,7 +2106,7 @@ func testInsertKnownChainData(t *testing.T, typ string) {
|
|||
b.OffsetTime(-9) // A higher difficulty
|
||||
})
|
||||
// Import the shared chain and the original canonical one
|
||||
chaindb, err := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), t.TempDir(), "", false)
|
||||
chaindb, err := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), t.TempDir(), "", false, false)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create temp freezer db: %v", err)
|
||||
}
|
||||
|
|
@ -2277,7 +2277,7 @@ func testInsertKnownChainDataWithMerging(t *testing.T, typ string, mergeHeight i
|
|||
}
|
||||
})
|
||||
// Import the shared chain and the original canonical one
|
||||
chaindb, err := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), t.TempDir(), "", false)
|
||||
chaindb, err := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), t.TempDir(), "", false, false)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create temp freezer db: %v", err)
|
||||
}
|
||||
|
|
@ -2581,7 +2581,7 @@ func TestTransactionIndices(t *testing.T) {
|
|||
limit := []uint64{0, 32, 64, 128}
|
||||
for _, l := range limit {
|
||||
frdir := t.TempDir()
|
||||
ancientDb, _ := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), frdir, "", false)
|
||||
ancientDb, _ := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), frdir, "", false, false)
|
||||
rawdb.WriteAncientBlocks(ancientDb, append([]*types.Block{gspec.ToBlock()}, blocks...), append([]types.Receipts{{}}, receipts...), big.NewInt(0))
|
||||
|
||||
l := l
|
||||
|
|
@ -2602,7 +2602,7 @@ func TestTransactionIndices(t *testing.T) {
|
|||
}
|
||||
|
||||
// Reconstruct a block chain which only reserves HEAD-64 tx indices
|
||||
ancientDb, _ := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), t.TempDir(), "", false)
|
||||
ancientDb, _ := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), t.TempDir(), "", false, false)
|
||||
defer ancientDb.Close()
|
||||
|
||||
rawdb.WriteAncientBlocks(ancientDb, append([]*types.Block{gspec.ToBlock()}, blocks...), append([]types.Receipts{{}}, receipts...), big.NewInt(0))
|
||||
|
|
@ -2674,7 +2674,7 @@ func TestSkipStaleTxIndicesInSnapSync(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
ancientDb, err := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), t.TempDir(), "", false)
|
||||
ancientDb, err := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), t.TempDir(), "", false, false)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create temp freezer db: %v", err)
|
||||
}
|
||||
|
|
@ -4017,7 +4017,7 @@ func TestTxIndexer(t *testing.T) {
|
|||
}
|
||||
for _, c := range cases {
|
||||
frdir := t.TempDir()
|
||||
db, _ := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), frdir, "", false)
|
||||
db, _ := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), frdir, "", false, false)
|
||||
rawdb.WriteAncientBlocks(db, append([]*types.Block{gspec.ToBlock()}, blocks...), append([]types.Receipts{{}}, receipts...), big.NewInt(0))
|
||||
|
||||
// Index the initial blocks from ancient store
|
||||
|
|
|
|||
|
|
@ -436,7 +436,7 @@ func TestAncientStorage(t *testing.T) {
|
|||
// Freezer style fast import the chain.
|
||||
frdir := t.TempDir()
|
||||
|
||||
db, err := NewDatabaseWithFreezer(NewMemoryDatabase(), frdir, "", false)
|
||||
db, err := NewDatabaseWithFreezer(NewMemoryDatabase(), frdir, "", false, false)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create database with ancient backend")
|
||||
}
|
||||
|
|
@ -573,7 +573,7 @@ func TestHashesInRange(t *testing.T) {
|
|||
func BenchmarkWriteAncientBlocks(b *testing.B) {
|
||||
// Open freezer database.
|
||||
frdir := b.TempDir()
|
||||
db, err := NewDatabaseWithFreezer(NewMemoryDatabase(), frdir, "", false)
|
||||
db, err := NewDatabaseWithFreezer(NewMemoryDatabase(), frdir, "", false, false)
|
||||
if err != nil {
|
||||
b.Fatalf("failed to create database with ancient backend")
|
||||
}
|
||||
|
|
@ -876,7 +876,7 @@ func TestHeadersRLPStorage(t *testing.T) {
|
|||
// Have N headers in the freezer
|
||||
frdir := t.TempDir()
|
||||
|
||||
db, err := NewDatabaseWithFreezer(NewMemoryDatabase(), frdir, "", false)
|
||||
db, err := NewDatabaseWithFreezer(NewMemoryDatabase(), frdir, "", false, false)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create database with ancient backend")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,14 +24,13 @@ import (
|
|||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/consensus"
|
||||
"github.com/ethereum/go-ethereum/consensus/misc"
|
||||
"github.com/ethereum/go-ethereum/core/rawdb"
|
||||
"github.com/ethereum/go-ethereum/core/state"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/core/vm"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
"github.com/ethereum/go-ethereum/params"
|
||||
|
||||
txtracelib "github.com/DeBankDeFi/etherlib/pkg/txtracev2"
|
||||
)
|
||||
|
||||
// StateProcessor is a basic Processor, which takes care of transitioning
|
||||
|
|
@ -60,7 +59,7 @@ func NewStateProcessor(config *params.ChainConfig, bc *BlockChain, engine consen
|
|||
// Process returns the receipts and logs accumulated during the process and
|
||||
// returns the amount of gas that was used in the process. If any of the
|
||||
// transactions failed to execute due to insufficient gas it will return an error.
|
||||
func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg vm.Config, tracing bool) (types.Receipts, []*types.Log, uint64, error) {
|
||||
func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg vm.Config) (types.Receipts, []*types.Log, uint64, error) {
|
||||
var (
|
||||
receipts types.Receipts
|
||||
usedGas = new(uint64)
|
||||
|
|
@ -79,8 +78,18 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
|
|||
// Iterate over and process the individual transactions
|
||||
start := time.Now()
|
||||
for i, tx := range block.Transactions() {
|
||||
if tracing {
|
||||
cfg.Tracer = txtracelib.NewOeTracer(p.bc.TxTraceStore(), header.Hash(), header.Number, tx.Hash(), uint64(statedb.TxIndex()))
|
||||
if cfg.TxTracerName != "" {
|
||||
txctx := &vm.TraceContext{
|
||||
BlockHash: blockHash,
|
||||
BlockNumber: blockNumber,
|
||||
TxIndex: i,
|
||||
TxHash: tx.Hash(),
|
||||
}
|
||||
tracer, err := cfg.TxTracerCreateFn(&cfg.TxTracerName, txctx, cfg.TxTracerConfig)
|
||||
if err != nil {
|
||||
return nil, nil, 0, fmt.Errorf("could not create txtracer: %w", err)
|
||||
}
|
||||
cfg.Tracer = tracer
|
||||
}
|
||||
vmenv := vm.NewEVM(blockContext, vm.TxContext{}, statedb, p.config, cfg)
|
||||
msg, err := TransactionToMessage(tx, types.MakeSigner(p.config, header.Number), header.BaseFee)
|
||||
|
|
@ -96,17 +105,24 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
|
|||
allLogs = append(allLogs, receipt.Logs...)
|
||||
|
||||
// Finalize trace logger result and save to underlying database if necessary.
|
||||
switch t := cfg.Tracer.(type) {
|
||||
case *txtracelib.OeTracer:
|
||||
log.Debug("Persist oe-style trace result to database", "blockNumber", header.Number.Int64(), "txHash", tx.Hash())
|
||||
t.PersistTrace()
|
||||
default:
|
||||
if cfg.TxTracerName != "" {
|
||||
tracer := (cfg.Tracer).(vm.EVMLoggerWithResult)
|
||||
if tracer != nil {
|
||||
res, err := tracer.GetResult()
|
||||
if err != nil {
|
||||
log.Error("could not get result from tracer", "err", err)
|
||||
} else {
|
||||
if res != nil {
|
||||
rawdb.WriteTxTrace(p.bc.txTraceDb, tx.Hash(), res)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if tracing {
|
||||
log.Info("Apply all transaction messages finished", "elapsed", common.PrettyDuration(time.Since(start)), "blockNumber", block.NumberU64(), "txs", len(block.Transactions()))
|
||||
if cfg.TxTracerName != "" {
|
||||
log.Debug("store all transaction trace messages finished", "elapsed", common.PrettyDuration(time.Since(start)), "blockNumber", block.NumberU64(), "txs", len(block.Transactions()))
|
||||
}
|
||||
|
||||
// Fail if Shanghai not enabled and len(withdrawals) is non-zero.
|
||||
|
|
@ -183,24 +199,5 @@ func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *commo
|
|||
return nil, err
|
||||
}
|
||||
|
||||
// Finalize trace logger result and save to underlying database if necessary.
|
||||
switch t := cfg.Tracer.(type) {
|
||||
case *txtracelib.OeTracer:
|
||||
log.Debug("Persist oe-style trace result to database", "blockNumber", header.Number.Int64(), "txHash", tx.Hash())
|
||||
t.PersistTrace()
|
||||
default:
|
||||
}
|
||||
|
||||
return receipt, nil
|
||||
}
|
||||
|
||||
func ApplyTransactionForPreExec(config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config) (*types.Receipt, error) {
|
||||
msg, err := TransactionToMessage(tx, types.MakeSigner(config, header.Number), header.BaseFee)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
blockContext := NewEVMBlockContext(header, bc, author)
|
||||
blockContext.BaseFee = big.NewInt(0)
|
||||
vmenv := vm.NewEVM(blockContext, vm.TxContext{}, statedb, config, cfg)
|
||||
return applyTransaction(msg, config, gp, statedb, header.Number, header.Hash(), tx, usedGas, vmenv)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,5 +49,5 @@ type Processor interface {
|
|||
// Process processes the state changes according to the Ethereum rules by running
|
||||
// the transaction messages using the statedb and applying any rewards to both
|
||||
// the processor (coinbase) and any included uncles.
|
||||
Process(block *types.Block, statedb *state.StateDB, cfg vm.Config, tracing bool) (types.Receipts, []*types.Log, uint64, error)
|
||||
Process(block *types.Block, statedb *state.StateDB, cfg vm.Config) (types.Receipts, []*types.Log, uint64, error)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,9 +17,12 @@
|
|||
package vm
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/common/math"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/ethereum/go-ethereum/ethdb"
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
)
|
||||
|
||||
|
|
@ -30,6 +33,11 @@ type Config struct {
|
|||
NoBaseFee bool // Forces the EIP-1559 baseFee to 0 (needed for 0 price calls)
|
||||
EnablePreimageRecording bool // Enables recording of SHA3/keccak preimages
|
||||
ExtraEips []int // Additional EIPS that are to be enabled
|
||||
|
||||
TxTraceDB ethdb.Database // TxTraceStore for tx tracing
|
||||
TxTracerName string // Name of the tracer
|
||||
TxTracerConfig json.RawMessage // Json config of the tracer
|
||||
TxTracerCreateFn func(*string, *TraceContext, json.RawMessage) (EVMLoggerWithResult, error) // fn for create tracer
|
||||
}
|
||||
|
||||
// ScopeContext contains the things that are per-call, such as stack and memory,
|
||||
|
|
|
|||
|
|
@ -17,6 +17,8 @@
|
|||
package vm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"math/big"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
|
|
@ -41,3 +43,27 @@ type EVMLogger interface {
|
|||
CaptureState(pc uint64, op OpCode, gas, cost uint64, scope *ScopeContext, rData []byte, depth int, err error)
|
||||
CaptureFault(pc uint64, op OpCode, gas, cost uint64, scope *ScopeContext, depth int, err error)
|
||||
}
|
||||
|
||||
// TraceContext contains some contextual infos for a transaction execution that is not
|
||||
// available from within the EVM object.
|
||||
type TraceContext struct {
|
||||
BlockHash common.Hash // Hash of the block the tx is contained within (zero if dangling tx or call)
|
||||
BlockNumber *big.Int // Number of the block the tx is contained within (zero if dangling tx or call)
|
||||
TxIndex int // Index of the transaction within a block (zero if dangling tx or call)
|
||||
TxHash common.Hash // Hash of the transaction being traced (zero if dangling call)
|
||||
}
|
||||
|
||||
// EVMLoggerWithResult interface extends vm.EVMLogger and additionally
|
||||
// allows collecting the tracing result.
|
||||
type EVMLoggerWithResult interface {
|
||||
EVMLogger
|
||||
GetResult() (json.RawMessage, error)
|
||||
}
|
||||
|
||||
// Store contains all the methods for tx-trace to interact with the underlying database.
|
||||
type TxTraceStore interface {
|
||||
// ReadTxTrace retrieve tracing result from underlying database.
|
||||
ReadTxTrace(ctx context.Context, txHash common.Hash) ([]byte, error)
|
||||
// WriteTxTrace write tracing result to underlying database.
|
||||
WriteTxTrace(ctx context.Context, txHash common.Hash, trace []byte) error
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,19 +2,20 @@ package eth
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math"
|
||||
"math/big"
|
||||
"math/rand"
|
||||
"strings"
|
||||
|
||||
txtrace2 "github.com/DeBankDeFi/etherlib/pkg/txtracev2"
|
||||
"github.com/ethereum/go-ethereum/accounts/abi"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
"github.com/ethereum/go-ethereum/core"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/core/vm"
|
||||
"github.com/ethereum/go-ethereum/eth/tracers"
|
||||
"github.com/ethereum/go-ethereum/internal/ethapi"
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
)
|
||||
|
|
@ -85,11 +86,11 @@ func toPreError(err error, result *core.ExecutionResult) PreError {
|
|||
}
|
||||
|
||||
type PreResult struct {
|
||||
Trace txtrace2.ActionTraceList `json:"trace"`
|
||||
Logs []*types.Log `json:"logs"`
|
||||
StateDiff txtrace2.StateDiff `json:"stateDiff"`
|
||||
Error PreError `json:"error,omitempty"`
|
||||
GasUsed uint64 `json:"gasUsed"`
|
||||
Trace interface{} `json:"trace"`
|
||||
Logs []*types.Log `json:"logs"`
|
||||
StateDiff interface{} `json:"stateDiff,omitempty"`
|
||||
Error PreError `json:"error,omitempty"`
|
||||
GasUsed uint64 `json:"gasUsed"`
|
||||
}
|
||||
|
||||
func (api *PreExecAPI) TraceMany(ctx context.Context, origins []PreArgs) ([]PreResult, error) {
|
||||
|
|
@ -142,7 +143,15 @@ func (api *PreExecAPI) TraceMany(ctx context.Context, origins []PreArgs) ([]PreR
|
|||
continue
|
||||
}
|
||||
txHash := common.BigToHash(big.NewInt(int64(i)))
|
||||
tracer := txtrace2.NewOeTracer(nil, header.Hash(), header.Number, txHash, uint64(i))
|
||||
tracer, err := tracers.DefaultDirectory.New("flatCallTracer", &tracers.Context{
|
||||
BlockHash: header.Hash(),
|
||||
BlockNumber: big.NewInt(0).Set(header.Number),
|
||||
TxIndex: 0,
|
||||
TxHash: txHash,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
evm, vmError, err := api.e.APIBackend.GetEVM(ctx, msg, state, header, &vm.Config{NoBaseFee: true, Tracer: tracer, PreExec: true})
|
||||
evm.Context.BaseFee = big.NewInt(0)
|
||||
evm.Context.BlockNumber.Add(evm.Context.BlockNumber, big.NewInt(rand.Int63n(6)+6))
|
||||
|
|
@ -180,10 +189,31 @@ func (api *PreExecAPI) TraceMany(ctx context.Context, origins []PreArgs) ([]PreR
|
|||
preResList = append(preResList, preRes)
|
||||
continue
|
||||
}
|
||||
rawRes, err := tracer.GetResult()
|
||||
if err != nil {
|
||||
preRes := PreResult{
|
||||
Error: toPreError(err, result),
|
||||
}
|
||||
if result != nil {
|
||||
preRes.GasUsed = result.UsedGas
|
||||
}
|
||||
preResList = append(preResList, preRes)
|
||||
continue
|
||||
}
|
||||
var res []map[string]interface{}
|
||||
if err := json.Unmarshal(rawRes, &res); err != nil {
|
||||
preRes := PreResult{
|
||||
Error: toPreError(err, result),
|
||||
}
|
||||
if result != nil {
|
||||
preRes.GasUsed = result.UsedGas
|
||||
}
|
||||
preResList = append(preResList, preRes)
|
||||
continue
|
||||
}
|
||||
preRes := PreResult{
|
||||
Trace: tracer.GetTraces(),
|
||||
Logs: state.GetLogs(txHash, header.Number.Uint64(), header.Hash()),
|
||||
StateDiff: tracer.GetStateDiff(),
|
||||
Trace: res,
|
||||
Logs: state.GetLogs(txHash, header.Number.Uint64(), header.Hash()),
|
||||
}
|
||||
if result != nil {
|
||||
preRes.GasUsed = result.UsedGas
|
||||
|
|
@ -192,10 +222,10 @@ func (api *PreExecAPI) TraceMany(ctx context.Context, origins []PreArgs) ([]PreR
|
|||
}
|
||||
}
|
||||
|
||||
if preRes.Error.Msg == "" && len(preRes.Trace) > 0 && (preRes.Trace)[0].Error != "" {
|
||||
if preRes.Error.Msg == "" && len(res) > 0 && (res)[0]["error"] != nil {
|
||||
preRes.Error = PreError{
|
||||
Code: Reverted,
|
||||
Msg: (preRes.Trace)[0].Error,
|
||||
Msg: fmt.Sprintf("%s", (res)[0]["error"]),
|
||||
}
|
||||
}
|
||||
preResList = append(preResList, preRes)
|
||||
|
|
|
|||
|
|
@ -21,12 +21,11 @@ package eth
|
|||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
|
||||
txtracelib "github.com/DeBankDeFi/etherlib/pkg/txtracev2"
|
||||
"github.com/ethereum/go-ethereum/core/rawdb"
|
||||
)
|
||||
|
||||
// PublicTxTraceAPI provides an API to tracing transaction or block information.
|
||||
|
|
@ -45,21 +44,15 @@ func (api *PublicTxTraceAPI) Transaction(ctx context.Context, txHash common.Hash
|
|||
if api.e.blockchain == nil {
|
||||
return []byte{}, fmt.Errorf("blockchain corruput")
|
||||
}
|
||||
|
||||
raw, err := api.e.blockchain.TxTraceStore().ReadTxTrace(ctx, txHash)
|
||||
if err != nil {
|
||||
return []byte{}, err
|
||||
}
|
||||
|
||||
traceDb := api.e.blockchain.TxTraceDB()
|
||||
raw := rawdb.ReadTxTrace(traceDb, txHash)
|
||||
if bytes.Equal(raw, []byte{}) { // empty response
|
||||
return nil, fmt.Errorf("trace result of tx {%#v} not found in tracedb", txHash)
|
||||
}
|
||||
|
||||
flatten := new(txtracelib.ActionTraceList)
|
||||
err = rlp.DecodeBytes(raw, flatten)
|
||||
var res interface{}
|
||||
err := json.Unmarshal(raw, &res)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to decode rlp flatten traces: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return *flatten, nil
|
||||
return res, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@
|
|||
package eth
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/big"
|
||||
|
|
@ -44,6 +45,7 @@ import (
|
|||
"github.com/ethereum/go-ethereum/eth/gasprice"
|
||||
"github.com/ethereum/go-ethereum/eth/protocols/eth"
|
||||
"github.com/ethereum/go-ethereum/eth/protocols/snap"
|
||||
"github.com/ethereum/go-ethereum/eth/tracers"
|
||||
"github.com/ethereum/go-ethereum/ethdb"
|
||||
"github.com/ethereum/go-ethereum/event"
|
||||
"github.com/ethereum/go-ethereum/internal/ethapi"
|
||||
|
|
@ -57,7 +59,6 @@ import (
|
|||
"github.com/ethereum/go-ethereum/params"
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
"github.com/ethereum/go-ethereum/txtrace"
|
||||
)
|
||||
|
||||
// Config contains the configuration options of the ETH protocol.
|
||||
|
|
@ -133,15 +134,6 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Init tx-trace underlying database and storage layer
|
||||
var traceDb ethdb.Database
|
||||
if config.TxTrace.Enabled {
|
||||
traceDb, err = stack.OpenDatabaseWithTrace(config.DatabaseCache, config.DatabaseHandles, config.TxTrace.StoreDir, "eth/db/tracedb", false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
txStore := txtrace.NewTraceStore(traceDb)
|
||||
|
||||
if err := pruner.RecoverPruning(stack.ResolvePath(""), chainDb, stack.ResolvePath(config.TrieCleanCacheJournal)); err != nil {
|
||||
log.Error("Failed to recover state", "error", err)
|
||||
|
|
@ -207,12 +199,35 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
|
|||
AncientPrune: config.AncientPrune,
|
||||
}
|
||||
)
|
||||
var traceDB ethdb.Database
|
||||
if config.TxTrace.Enabled {
|
||||
traceDB, err = stack.OpenDatabaseWithTrace(config.DatabaseCache, config.DatabaseHandles, config.TxTrace.StoreDir, "eth/db/tracedb", false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
vmConfig.TxTraceDB = traceDB
|
||||
vmConfig.TxTracerName = "flatCallTracer"
|
||||
tracerConfigdata, _ := json.Marshal(map[string]interface{}{
|
||||
"convertParityErrors": true,
|
||||
"includePrecompiles": true,
|
||||
})
|
||||
vmConfig.TxTracerConfig = tracerConfigdata
|
||||
vmConfig.TxTracerCreateFn = func(name *string, tc *vm.TraceContext, config json.RawMessage) (vm.EVMLoggerWithResult, error) {
|
||||
return tracers.DefaultDirectory.New(*name, &tracers.Context{
|
||||
BlockHash: tc.BlockHash,
|
||||
BlockNumber: tc.BlockNumber,
|
||||
TxIndex: tc.TxIndex,
|
||||
TxHash: tc.TxHash,
|
||||
}, config)
|
||||
}
|
||||
}
|
||||
|
||||
// Override the chain config with provided settings.
|
||||
var overrides core.ChainOverrides
|
||||
if config.OverrideShanghai != nil {
|
||||
overrides.OverrideShanghai = config.OverrideShanghai
|
||||
}
|
||||
eth.blockchain, err = core.NewBlockChainV2(chainDb, cacheConfig, config.Genesis, &overrides, eth.engine, vmConfig, eth.shouldPreserve, &config.TxLookupLimit, &config.TxTrace, txStore)
|
||||
eth.blockchain, err = core.NewBlockChainV2(chainDb, cacheConfig, config.Genesis, &overrides, eth.engine, vmConfig, eth.shouldPreserve, &config.TxLookupLimit, &config.TxTrace, traceDB)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -61,7 +61,7 @@ func newTester(t *testing.T) *downloadTester {
|
|||
// newTester creates a new downloader test mocker.
|
||||
func newTesterWithNotification(t *testing.T, success func()) *downloadTester {
|
||||
freezer := t.TempDir()
|
||||
db, err := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), freezer, "", false)
|
||||
db, err := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), freezer, "", false, false)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -159,7 +159,7 @@ func (eth *Ethereum) StateAtBlock(ctx context.Context, block *types.Block, reexe
|
|||
if current = eth.blockchain.GetBlockByNumber(next); current == nil {
|
||||
return nil, nil, fmt.Errorf("block #%d not found", next)
|
||||
}
|
||||
_, _, _, err := eth.blockchain.Processor().Process(current, statedb, vm.Config{}, false)
|
||||
_, _, _, err := eth.blockchain.Processor().Process(current, statedb, vm.Config{})
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("processing block %d failed: %v", current.NumberU64(), err)
|
||||
}
|
||||
|
|
|
|||
1
go.mod
1
go.mod
|
|
@ -4,7 +4,6 @@ go 1.19
|
|||
|
||||
require (
|
||||
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v0.3.0
|
||||
github.com/DeBankDeFi/etherlib v0.2.7
|
||||
github.com/VictoriaMetrics/fastcache v1.6.0
|
||||
github.com/aws/aws-sdk-go-v2 v1.2.0
|
||||
github.com/aws/aws-sdk-go-v2/config v1.1.1
|
||||
|
|
|
|||
2
go.sum
2
go.sum
|
|
@ -12,8 +12,6 @@ github.com/CloudyKit/fastprinter v0.0.0-20200109182630-33d98a066a53/go.mod h1:+3
|
|||
github.com/CloudyKit/jet/v3 v3.0.0/go.mod h1:HKQPgSJmdK8hdoAbKUUWajkHyHo4RaU5rMdUywE7VMo=
|
||||
github.com/DataDog/zstd v1.5.2 h1:vUG4lAyuPCXO0TLbXvPv7EB7cNK1QV/luu55UHLrrn8=
|
||||
github.com/DataDog/zstd v1.5.2/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw=
|
||||
github.com/DeBankDeFi/etherlib v0.2.7 h1:Yk3v2VlT22ifM/ARLT3HNtxMhIoKSWON47TzhYVkQkc=
|
||||
github.com/DeBankDeFi/etherlib v0.2.7/go.mod h1:Q/pkoKm4oZu9UOhLm07Zg7sxXvnLI3GdOoK4bE2t+xI=
|
||||
github.com/Joker/hpp v1.0.0/go.mod h1:8x5n+M1Hp5hC0g8okX3sR3vFQwynaX/UgSOM9MeBKzY=
|
||||
github.com/Shopify/goreferrer v0.0.0-20181106222321-ec9c9a553398/go.mod h1:a1uqRtAwp2Xwc6WNPJEufxJ7fx3npB4UV/JOLmbu5I0=
|
||||
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 h1:fLjPD/aNc3UIOA6tDi6QXUemppXK3P9BI7mr2hd6gx8=
|
||||
|
|
|
|||
|
|
@ -343,3 +343,10 @@ func (b *backendMock) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent)
|
|||
}
|
||||
|
||||
func (b *backendMock) Engine() consensus.Engine { return nil }
|
||||
|
||||
func (b *backendMock) GetCallCache(key string) (interface{}, bool) {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (b *backendMock) SetCallCache(key string, value interface{}, weight int64) {
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,92 +0,0 @@
|
|||
// Copyright 2021 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package txtrace
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/core/rawdb"
|
||||
"github.com/ethereum/go-ethereum/ethdb"
|
||||
"github.com/ethereum/go-ethereum/metrics"
|
||||
|
||||
txtrace "github.com/DeBankDeFi/etherlib/pkg/txtracev2"
|
||||
)
|
||||
|
||||
var (
|
||||
txTraceWriteSuccessCounter = metrics.NewRegisteredCounter("chain/txtraces/write/success", nil)
|
||||
txTraceWriteFailCounter = metrics.NewRegisteredCounter("chain/txtraces/write/fail", nil)
|
||||
)
|
||||
|
||||
var (
|
||||
once sync.Once
|
||||
defaultTraceStore *traceStore
|
||||
)
|
||||
|
||||
var _ txtrace.Store = (*traceStore)(nil)
|
||||
|
||||
type traceStore struct {
|
||||
db ethdb.Database
|
||||
}
|
||||
|
||||
// NewTraceStore creates a new trace store.
|
||||
func NewTraceStore(db ethdb.Database) txtrace.Store {
|
||||
if defaultTraceStore != nil {
|
||||
return defaultTraceStore
|
||||
}
|
||||
once.Do(func() {
|
||||
defaultTraceStore = &traceStore{db: db}
|
||||
})
|
||||
return defaultTraceStore
|
||||
}
|
||||
|
||||
// GetTraceStore get singleton traceStore.
|
||||
func GetTraceStore() *traceStore {
|
||||
return defaultTraceStore
|
||||
}
|
||||
|
||||
func (t *traceStore) guard() error {
|
||||
if t.db == nil {
|
||||
return fmt.Errorf("txtrace mode not enabled")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReadTxTrace retrieves the result of tx by evm-tracing which stores in db.
|
||||
func (t *traceStore) ReadTxTrace(ctx context.Context, txHash common.Hash) ([]byte, error) {
|
||||
if err := t.guard(); err != nil {
|
||||
return []byte{}, err
|
||||
}
|
||||
data := rawdb.ReadTxTrace(t.db, txHash)
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// WriteTxTrace write the result of tx tracing by evm-tracing to db.
|
||||
func (t *traceStore) WriteTxTrace(ctx context.Context, txHash common.Hash, trace []byte) error {
|
||||
if err := t.guard(); err != nil {
|
||||
return err
|
||||
}
|
||||
err := rawdb.WriteTxTrace(t.db, txHash, trace)
|
||||
if err == nil {
|
||||
txTraceWriteSuccessCounter.Inc(1)
|
||||
return nil
|
||||
}
|
||||
txTraceWriteFailCounter.Inc(1)
|
||||
return err
|
||||
}
|
||||
Loading…
Reference in a new issue