mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-26 14:46:42 +00:00
add tx-trace store and CLI flags
This commit is contained in:
parent
531833ce20
commit
ab6a1c2ddb
11 changed files with 312 additions and 4 deletions
|
|
@ -201,6 +201,11 @@ var (
|
||||||
utils.MetricsInfluxDBBucketFlag,
|
utils.MetricsInfluxDBBucketFlag,
|
||||||
utils.MetricsInfluxDBOrganizationFlag,
|
utils.MetricsInfluxDBOrganizationFlag,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
txTraceFlags = []cli.Flag{
|
||||||
|
utils.TxTraceEnabledFlag,
|
||||||
|
utils.TxTraceStoreFlag,
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
@ -246,7 +251,8 @@ func init() {
|
||||||
rpcFlags,
|
rpcFlags,
|
||||||
consoleFlags,
|
consoleFlags,
|
||||||
debug.Flags,
|
debug.Flags,
|
||||||
metricsFlags)
|
metricsFlags,
|
||||||
|
txTraceFlags)
|
||||||
|
|
||||||
app.Before = func(ctx *cli.Context) error {
|
app.Before = func(ctx *cli.Context) error {
|
||||||
return debug.Setup(ctx)
|
return debug.Setup(ctx)
|
||||||
|
|
|
||||||
|
|
@ -215,6 +215,10 @@ var AppHelpFlagGroups = []flags.FlagGroup{
|
||||||
Name: "METRICS AND STATS",
|
Name: "METRICS AND STATS",
|
||||||
Flags: metricsFlags,
|
Flags: metricsFlags,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "TRANSACTION TRACING",
|
||||||
|
Flags: txTraceFlags,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
Name: "ALIASED (deprecated)",
|
Name: "ALIASED (deprecated)",
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
|
|
|
||||||
|
|
@ -67,6 +67,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/p2p/nat"
|
"github.com/ethereum/go-ethereum/p2p/nat"
|
||||||
"github.com/ethereum/go-ethereum/p2p/netutil"
|
"github.com/ethereum/go-ethereum/p2p/netutil"
|
||||||
"github.com/ethereum/go-ethereum/params"
|
"github.com/ethereum/go-ethereum/params"
|
||||||
|
"github.com/ethereum/go-ethereum/txtrace"
|
||||||
pcsclite "github.com/gballet/go-libpcsclite"
|
pcsclite "github.com/gballet/go-libpcsclite"
|
||||||
gopsutil "github.com/shirou/gopsutil/mem"
|
gopsutil "github.com/shirou/gopsutil/mem"
|
||||||
"gopkg.in/urfave/cli.v1"
|
"gopkg.in/urfave/cli.v1"
|
||||||
|
|
@ -834,6 +835,16 @@ var (
|
||||||
Usage: "InfluxDB organization name (v2 only)",
|
Usage: "InfluxDB organization name (v2 only)",
|
||||||
Value: metrics.DefaultConfig.InfluxDBOrganization,
|
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 (
|
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) {
|
func setTxPool(ctx *cli.Context, cfg *core.TxPoolConfig) {
|
||||||
if ctx.GlobalIsSet(TxPoolLocalsFlag.Name) {
|
if ctx.GlobalIsSet(TxPoolLocalsFlag.Name) {
|
||||||
locals := strings.Split(ctx.GlobalString(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)
|
setMiner(ctx, &cfg.Miner)
|
||||||
setRequiredBlocks(ctx, cfg)
|
setRequiredBlocks(ctx, cfg)
|
||||||
setLes(ctx, cfg)
|
setLes(ctx, cfg)
|
||||||
|
setTxTrace(ctx, &cfg.TxTrace)
|
||||||
|
|
||||||
// Cap the cache allowance and tune the garbage collector
|
// Cap the cache allowance and tune the garbage collector
|
||||||
mem, err := gopsutil.VirtualMemory()
|
mem, err := gopsutil.VirtualMemory()
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,10 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/metrics"
|
"github.com/ethereum/go-ethereum/metrics"
|
||||||
"github.com/ethereum/go-ethereum/params"
|
"github.com/ethereum/go-ethereum/params"
|
||||||
"github.com/ethereum/go-ethereum/trie"
|
"github.com/ethereum/go-ethereum/trie"
|
||||||
|
"github.com/ethereum/go-ethereum/txtrace"
|
||||||
lru "github.com/hashicorp/golang-lru"
|
lru "github.com/hashicorp/golang-lru"
|
||||||
|
|
||||||
|
txtracelib "github.com/DeBankDeFi/etherlib/pkg/txtracev2"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -171,8 +174,11 @@ var defaultCacheConfig = &CacheConfig{
|
||||||
// included in the canonical one where as GetBlockByNumber always represents the
|
// included in the canonical one where as GetBlockByNumber always represents the
|
||||||
// canonical chain.
|
// canonical chain.
|
||||||
type BlockChain struct {
|
type BlockChain struct {
|
||||||
chainConfig *params.ChainConfig // Chain & network configuration
|
chainConfig *params.ChainConfig // Chain & network configuration
|
||||||
cacheConfig *CacheConfig // Cache configuration for pruning
|
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
|
db ethdb.Database // Low level persistent database to store final content in
|
||||||
snaps *snapshot.Tree // Snapshot tree for fast trie leaf access
|
snaps *snapshot.Tree // Snapshot tree for fast trie leaf access
|
||||||
|
|
@ -225,6 +231,19 @@ type BlockChain struct {
|
||||||
vmConfig vm.Config
|
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
|
// NewBlockChain returns a fully initialised block chain using information
|
||||||
// available in the database. It initialises the default Ethereum Validator
|
// available in the database. It initialises the default Ethereum Validator
|
||||||
// and Processor.
|
// and Processor.
|
||||||
|
|
@ -441,6 +460,10 @@ func (bc *BlockChain) empty() bool {
|
||||||
return true
|
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
|
// loadLastState loads the last known chain state from the database. This method
|
||||||
// assumes that the chain manager mutex is held.
|
// assumes that the chain manager mutex is held.
|
||||||
func (bc *BlockChain) loadLastState() error {
|
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)
|
_, err := bc.hc.InsertHeaderChain(chain, start, bc.forker)
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TxTraceStore retrieves the blockchain's tx-trace store.
|
||||||
|
func (bc *BlockChain) TxTraceStore() txtracelib.Store { return bc.txTraceStore }
|
||||||
|
|
|
||||||
41
core/rawdb/accessors_trace.go
Normal file
41
core/rawdb/accessors_trace.go
Normal file
|
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
57
core/rawdb/accessors_trace_test.go
Normal file
57
core/rawdb/accessors_trace_test.go
Normal file
|
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -20,6 +20,7 @@ package eth
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/ethereum/go-ethereum/txtrace"
|
||||||
"math/big"
|
"math/big"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
@ -149,6 +150,16 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
|
||||||
log.Info(strings.Repeat("-", 153))
|
log.Info(strings.Repeat("-", 153))
|
||||||
log.Info("")
|
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 {
|
if err := pruner.RecoverPruning(stack.ResolvePath(""), chainDb, stack.ResolvePath(config.TrieCleanCacheJournal)); err != nil {
|
||||||
log.Error("Failed to recover state", "error", err)
|
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,
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -38,6 +38,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/miner"
|
"github.com/ethereum/go-ethereum/miner"
|
||||||
"github.com/ethereum/go-ethereum/node"
|
"github.com/ethereum/go-ethereum/node"
|
||||||
"github.com/ethereum/go-ethereum/params"
|
"github.com/ethereum/go-ethereum/params"
|
||||||
|
"github.com/ethereum/go-ethereum/txtrace"
|
||||||
)
|
)
|
||||||
|
|
||||||
// FullNodeGPO contains default gasprice oracle settings for full node.
|
// FullNodeGPO contains default gasprice oracle settings for full node.
|
||||||
|
|
@ -183,6 +184,9 @@ type Config struct {
|
||||||
// Transaction pool options
|
// Transaction pool options
|
||||||
TxPool core.TxPoolConfig
|
TxPool core.TxPoolConfig
|
||||||
|
|
||||||
|
// TxTrace
|
||||||
|
TxTrace txtrace.Config
|
||||||
|
|
||||||
// Gas Price Oracle options
|
// Gas Price Oracle options
|
||||||
GPO gasprice.Config
|
GPO gasprice.Config
|
||||||
|
|
||||||
|
|
|
||||||
22
node/node.go
22
node/node.go
|
|
@ -712,6 +712,28 @@ func (n *Node) OpenDatabase(name string, cache, handles int, namespace string, r
|
||||||
return db, err
|
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
|
// 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,
|
// 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
|
// also attaching a chain freezer to it that moves ancient chain data from the
|
||||||
|
|
|
||||||
24
txtrace/config.go
Normal file
24
txtrace/config.go
Normal file
|
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package txtrace
|
||||||
|
|
||||||
|
// Config is the configuration of tx tracer.
|
||||||
|
type Config struct {
|
||||||
|
Enabled bool
|
||||||
|
StoreDir string
|
||||||
|
Consistency bool
|
||||||
|
}
|
||||||
92
txtrace/store.go
Normal file
92
txtrace/store.go
Normal file
|
|
@ -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 <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