eth-statediff-service/cmd/util.go

153 lines
4.7 KiB
Go
Raw Normal View History

2021-10-21 17:06:06 +00:00
package cmd
import (
"context"
2021-10-25 19:06:05 +00:00
statediff "github.com/cerc-io/plugeth-statediff"
"github.com/cerc-io/plugeth-statediff/indexer"
"github.com/cerc-io/plugeth-statediff/indexer/node"
"github.com/cerc-io/plugeth-statediff/indexer/shared"
"github.com/cerc-io/plugeth-statediff/utils"
2021-10-25 19:06:05 +00:00
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/params"
2021-10-21 17:06:06 +00:00
"github.com/ethereum/go-ethereum/trie"
"github.com/spf13/viper"
pkg "github.com/cerc-io/eth-statediff-service/pkg"
"github.com/cerc-io/eth-statediff-service/pkg/prom"
2021-10-21 17:06:06 +00:00
)
2021-10-25 19:06:05 +00:00
type blockRange [2]uint64
func createStateDiffService(lvlDBReader pkg.Reader, chainConf *params.ChainConfig, nodeInfo node.Info) (*pkg.Service, error) {
2021-10-21 17:06:06 +00:00
// create statediff service
logWithCommand.Debug("Setting up database")
conf, err := getConfig(nodeInfo)
2021-10-21 17:06:06 +00:00
if err != nil {
logWithCommand.Fatal(err)
}
2022-07-19 09:30:01 +00:00
logWithCommand.Debug("Creating statediff indexer")
db, indexer, err := indexer.NewStateDiffIndexer(context.Background(), chainConf, nodeInfo, conf, true)
2021-10-21 17:06:06 +00:00
if err != nil {
logWithCommand.Fatal(err)
}
2022-07-19 09:30:01 +00:00
if conf.Type() == shared.POSTGRES && viper.GetBool("prom.dbStats") {
prom.RegisterDBCollector(viper.GetString("database.name"), db)
}
logWithCommand.Debug("Creating statediff service")
sdConf := pkg.ServiceConfig{
2021-10-25 19:06:05 +00:00
ServiceWorkers: viper.GetUint("statediff.serviceWorkers"),
TrieWorkers: viper.GetUint("statediff.trieWorkers"),
WorkerQueueSize: viper.GetUint("statediff.workerQueueSize"),
PreRuns: setupPreRunRanges(),
}
return pkg.NewStateDiffService(lvlDBReader, indexer, sdConf), nil
2021-10-21 17:06:06 +00:00
}
func setupPreRunRanges() []pkg.RangeRequest {
2021-10-25 19:06:05 +00:00
if !viper.GetBool("statediff.prerun") {
return nil
}
2022-07-19 09:30:01 +00:00
preRunParams := statediff.Params{
2023-04-09 16:01:12 +00:00
IncludeBlock: viper.GetBool("prerun.params.includeBlock"),
IncludeReceipts: viper.GetBool("prerun.params.includeReceipts"),
IncludeTD: viper.GetBool("prerun.params.includeTD"),
IncludeCode: viper.GetBool("prerun.params.includeCode"),
2021-10-25 19:06:05 +00:00
}
var addrStrs []string
viper.UnmarshalKey("prerun.params.watchedAddresses", &addrStrs)
addrs := make([]common.Address, len(addrStrs))
for i, addrStr := range addrStrs {
addrs[i] = common.HexToAddress(addrStr)
}
preRunParams.WatchedAddresses = addrs
var rawRanges []blockRange
viper.UnmarshalKey("prerun.ranges", &rawRanges)
blockRanges := make([]pkg.RangeRequest, len(rawRanges))
2021-10-25 19:06:05 +00:00
for i, rawRange := range rawRanges {
blockRanges[i] = pkg.RangeRequest{
2021-10-25 19:06:05 +00:00
Start: rawRange[0],
Stop: rawRange[1],
Params: preRunParams,
}
}
if viper.IsSet("prerun.start") && viper.IsSet("prerun.stop") {
hardStart := viper.GetInt("prerun.start")
hardStop := viper.GetInt("prerun.stop")
blockRanges = append(blockRanges, pkg.RangeRequest{
Start: uint64(hardStart),
Stop: uint64(hardStop),
Params: preRunParams,
})
}
2021-10-25 19:06:05 +00:00
return blockRanges
}
func instantiateLevelDBReader() (pkg.Reader, *params.ChainConfig, node.Info) {
// load some necessary params
logWithCommand.Debug("Loading statediff service parameters")
mode := viper.GetString("leveldb.mode")
path := viper.GetString("leveldb.path")
ancientPath := viper.GetString("leveldb.ancient")
url := viper.GetString("leveldb.url")
if mode == "local" {
if path == "" || ancientPath == "" {
logWithCommand.Fatal("Require a valid eth LevelDB primary datastore path and ancient datastore path")
}
} else if mode == "remote" {
if url == "" {
logWithCommand.Fatal("Require a valid RPC url for accessing LevelDB")
}
} else {
logWithCommand.Fatal("Invalid mode provided for LevelDB access")
}
nodeInfo := getEthNodeInfo()
chainConfigPath := viper.GetString("ethereum.chainConfig")
chainConf, err := utils.LoadConfig(chainConfigPath)
if err != nil {
logWithCommand.Fatalf("Unable to instantiate chain config: %s", err)
}
// create LevelDB reader
logWithCommand.Debug("Creating LevelDB reader")
readerConf := pkg.LvLDBReaderConfig{
TrieConfig: &trie.Config{
Cache: viper.GetInt("cache.trie"),
Journal: "",
Preimages: false,
},
ChainConfig: chainConf,
Mode: mode,
Path: path,
AncientPath: ancientPath,
Url: url,
DBCacheSize: viper.GetInt("cache.database"),
}
reader, err := pkg.NewLvlDBReader(readerConf)
if err != nil {
logWithCommand.Fatalf("Unable to instantiate levelDB reader: %s", err)
}
return reader, chainConf, nodeInfo
}
// report latest block info
func reportLatestBlock(reader pkg.Reader) {
header, err := reader.GetLatestHeader()
if err != nil {
logWithCommand.Fatalf("Unable to determine latest header height and hash: %s", err.Error())
}
if header.Number == nil {
logWithCommand.Fatal("Latest header found in levelDB has a nil block height")
}
logWithCommand.
WithField("height", header.Number).
WithField("hash", header.Hash()).
Info("Latest block found in levelDB")
}