diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 76d6427fab..2d01e51eb9 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -201,6 +201,11 @@ var ( utils.MetricsInfluxDBBucketFlag, utils.MetricsInfluxDBOrganizationFlag, } + + txTraceFlags = []cli.Flag{ + utils.TxTraceEnabledFlag, + utils.TxTraceStoreFlag, + } ) func init() { @@ -246,7 +251,8 @@ func init() { rpcFlags, consoleFlags, debug.Flags, - metricsFlags) + metricsFlags, + txTraceFlags) app.Before = func(ctx *cli.Context) error { return debug.Setup(ctx) diff --git a/cmd/geth/usage.go b/cmd/geth/usage.go index 56a3d053d6..74c5643013 100644 --- a/cmd/geth/usage.go +++ b/cmd/geth/usage.go @@ -215,6 +215,10 @@ var AppHelpFlagGroups = []flags.FlagGroup{ Name: "METRICS AND STATS", Flags: metricsFlags, }, + { + Name: "TRANSACTION TRACING", + Flags: txTraceFlags, + }, { Name: "ALIASED (deprecated)", Flags: []cli.Flag{ diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index c80de61104..b9eb87a8fb 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -67,6 +67,7 @@ import ( "github.com/ethereum/go-ethereum/p2p/nat" "github.com/ethereum/go-ethereum/p2p/netutil" "github.com/ethereum/go-ethereum/params" + "github.com/ethereum/go-ethereum/txtrace" pcsclite "github.com/gballet/go-libpcsclite" gopsutil "github.com/shirou/gopsutil/mem" "gopkg.in/urfave/cli.v1" @@ -834,6 +835,16 @@ var ( Usage: "InfluxDB organization name (v2 only)", Value: metrics.DefaultConfig.InfluxDBOrganization, } + + // TxTraceEnabledFlag ... + TxTraceEnabledFlag = cli.BoolFlag{ + Name: "txtrace", + Usage: "Enable transaction trace while evm processing, default result is openEthereum style", + } + TxTraceStoreFlag = DirectoryFlag{ + Name: "txtrace.store", + Usage: "Data directory for store transaction trace result (default = inside datadir)", + } ) var ( @@ -1421,6 +1432,15 @@ func setGPO(ctx *cli.Context, cfg *gasprice.Config, light bool) { } } +func setTxTrace(ctx *cli.Context, cfg *txtrace.Config) { + if ctx.GlobalIsSet(TxTraceEnabledFlag.Name) { + cfg.Enabled = ctx.GlobalBool(TxTraceEnabledFlag.Name) + } + if ctx.GlobalIsSet(TxTraceStoreFlag.Name) { + cfg.StoreDir = ctx.GlobalString(TxTraceStoreFlag.Name) + } +} + func setTxPool(ctx *cli.Context, cfg *core.TxPoolConfig) { if ctx.GlobalIsSet(TxPoolLocalsFlag.Name) { locals := strings.Split(ctx.GlobalString(TxPoolLocalsFlag.Name), ",") @@ -1613,6 +1633,7 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) { setMiner(ctx, &cfg.Miner) setRequiredBlocks(ctx, cfg) setLes(ctx, cfg) + setTxTrace(ctx, &cfg.TxTrace) // Cap the cache allowance and tune the garbage collector mem, err := gopsutil.VirtualMemory() diff --git a/core/blockchain.go b/core/blockchain.go index 366782800d..dd4927a9bf 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -43,7 +43,10 @@ import ( "github.com/ethereum/go-ethereum/metrics" "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/trie" + "github.com/ethereum/go-ethereum/txtrace" lru "github.com/hashicorp/golang-lru" + + txtracelib "github.com/DeBankDeFi/etherlib/pkg/txtracev2" ) var ( @@ -171,8 +174,11 @@ var defaultCacheConfig = &CacheConfig{ // included in the canonical one where as GetBlockByNumber always represents the // canonical chain. type BlockChain struct { - chainConfig *params.ChainConfig // Chain & network configuration - cacheConfig *CacheConfig // Cache configuration for pruning + chainConfig *params.ChainConfig // Chain & network configuration + cacheConfig *CacheConfig // Cache configuration for pruning + txTraceConfig *txtrace.Config + // txtraceStore + txTraceStore txtracelib.Store db ethdb.Database // Low level persistent database to store final content in snaps *snapshot.Tree // Snapshot tree for fast trie leaf access @@ -225,6 +231,19 @@ type BlockChain struct { vmConfig vm.Config } +// 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, chainConfig *params.ChainConfig, txTraceConfig *txtrace.Config, txStore txtracelib.Store, engine consensus.Engine, vmConfig vm.Config, shouldPreserve func(block *types.Header) bool, txLookupLimit *uint64) (*BlockChain, error) { + bc, err := NewBlockChain(db, cacheConfig, chainConfig, engine, vmConfig, shouldPreserve, txLookupLimit) + if err != nil { + return nil, err + } + bc.txTraceConfig = txTraceConfig + bc.txTraceStore = txStore + return bc, nil +} + // NewBlockChain returns a fully initialised block chain using information // available in the database. It initialises the default Ethereum Validator // and Processor. @@ -441,6 +460,10 @@ 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 { @@ -2471,3 +2494,6 @@ func (bc *BlockChain) InsertHeaderChain(chain []*types.Header, checkFreq int) (i _, err := bc.hc.InsertHeaderChain(chain, start, bc.forker) return 0, err } + +// TxTraceStore retrieves the blockchain's tx-trace store. +func (bc *BlockChain) TxTraceStore() txtracelib.Store { return bc.txTraceStore } diff --git a/core/rawdb/accessors_trace.go b/core/rawdb/accessors_trace.go new file mode 100644 index 0000000000..5cac35d714 --- /dev/null +++ b/core/rawdb/accessors_trace.go @@ -0,0 +1,41 @@ +// 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 . + +package rawdb + +import ( + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/log" +) + +// ReadTxTrace retrieves the result of tx by evm-tracing which stores in db. +func ReadTxTrace(db ethdb.KeyValueReader, hash common.Hash) []byte { + data, err := db.Get(codeKey(hash)) + if err != nil { + log.Error("Failed to read tx trace result", "err", err) + } + return data +} + +// WriteTxTrace write the result of tx tracing by evm-tracing to db. +func WriteTxTrace(db ethdb.KeyValueWriter, hash common.Hash, data []byte) error { + if err := db.Put(codeKey(hash), data); err != nil { + log.Crit("Failed to write tx trace result", "err", err) + return err + } + return nil +} diff --git a/core/rawdb/accessors_trace_test.go b/core/rawdb/accessors_trace_test.go new file mode 100644 index 0000000000..d79a72576c --- /dev/null +++ b/core/rawdb/accessors_trace_test.go @@ -0,0 +1,57 @@ +// 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 . + +package rawdb + +import ( + "bytes" + "testing" + + "github.com/ethereum/go-ethereum/common" +) + +func TestTxTrace(t *testing.T) { + db := NewMemoryDatabase() + + testCases := []struct { + name string + txHash common.Hash + expectedData []byte + }{ + { + name: "test1", + txHash: common.Hash{}, + expectedData: []byte{}, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + data := ReadTxTrace(db, tc.txHash) + if !bytes.Equal(tc.expectedData, data) { + t.Fatalf("Unexpected tx trace data returned") + } + }) + } + + txHashStr := "0x5d763978db1d0aedce8cc7c97389fccd1be95e17da337aeec0dd8f8ff2417726" + txHash := common.HexToHash(txHashStr) + WriteTxTrace(db, txHash, []byte("hello world")) + data := ReadTxTrace(db, txHash) + if !bytes.Equal(data, []byte("hello world")) { + t.Fatalf("Unexpected tx trace data returned") + } +} diff --git a/eth/backend.go b/eth/backend.go index 48d7d1c249..99d8d25fe2 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -20,6 +20,7 @@ package eth import ( "errors" "fmt" + "github.com/ethereum/go-ethereum/txtrace" "math/big" "runtime" "strings" @@ -149,6 +150,16 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { log.Info(strings.Repeat("-", 153)) log.Info("") + // 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) } @@ -205,7 +216,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { AncientRecentLimit: config.AncientRecentLimit, } ) - eth.blockchain, err = core.NewBlockChain(chainDb, cacheConfig, chainConfig, eth.engine, vmConfig, eth.shouldPreserve, &config.TxLookupLimit) + eth.blockchain, err = core.NewBlockChainV2(chainDb, cacheConfig, chainConfig, &config.TxTrace, txStore, eth.engine, vmConfig, eth.shouldPreserve, &config.TxLookupLimit) if err != nil { return nil, err } diff --git a/eth/ethconfig/config.go b/eth/ethconfig/config.go index c37faa1688..50ce0618f4 100644 --- a/eth/ethconfig/config.go +++ b/eth/ethconfig/config.go @@ -38,6 +38,7 @@ import ( "github.com/ethereum/go-ethereum/miner" "github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/params" + "github.com/ethereum/go-ethereum/txtrace" ) // FullNodeGPO contains default gasprice oracle settings for full node. @@ -183,6 +184,9 @@ type Config struct { // Transaction pool options TxPool core.TxPoolConfig + // TxTrace + TxTrace txtrace.Config + // Gas Price Oracle options GPO gasprice.Config diff --git a/node/node.go b/node/node.go index c67bbd01d8..de4610279c 100644 --- a/node/node.go +++ b/node/node.go @@ -712,6 +712,28 @@ func (n *Node) OpenDatabase(name string, cache, handles int, namespace string, r return db, err } +// OpenDatabaseWithTrace same as OpenDatabaseWithFreezer but open txTrace dedicated database instead. +func (n *Node) OpenDatabaseWithTrace(cache, handles int, tracer, namespace string, readonly bool) (ethdb.Database, error) { + n.lock.Lock() + defer n.lock.Unlock() + if n.state == closedState { + return nil, ErrNodeStopped + } + + var db ethdb.Database + var err error + if tracer == "" { + tracer = "tracedb" + } + tracer = n.ResolvePath(tracer) + db, err = rawdb.NewLevelDBDatabase(tracer, cache, handles, namespace, readonly) + if err == nil { + db = n.wrapDatabase(db) + } + log.Info("Opened tx-trace database", "database", tracer) + return db, err +} + // OpenDatabaseWithFreezer opens an existing database with the given name (or // creates one if no previous can be found) from within the node's data directory, // also attaching a chain freezer to it that moves ancient chain data from the diff --git a/txtrace/config.go b/txtrace/config.go new file mode 100644 index 0000000000..2a4e463a53 --- /dev/null +++ b/txtrace/config.go @@ -0,0 +1,24 @@ +// 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 . + +package txtrace + +// Config is the configuration of tx tracer. +type Config struct { + Enabled bool + StoreDir string + Consistency bool +} diff --git a/txtrace/store.go b/txtrace/store.go new file mode 100644 index 0000000000..d88b8264a2 --- /dev/null +++ b/txtrace/store.go @@ -0,0 +1,92 @@ +// 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 . + +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 +}