eth-statediff-service/cmd/util.go

91 lines
2.5 KiB
Go
Raw Normal View History

2021-10-21 17:06:06 +00:00
package cmd
import (
"context"
"encoding/json"
"fmt"
"os"
2021-10-25 19:06:05 +00:00
gethsd "github.com/cerc-io/plugeth-statediff"
ind "github.com/cerc-io/plugeth-statediff/indexer"
"github.com/cerc-io/plugeth-statediff/utils/log"
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"
sd "github.com/vulcanize/eth-statediff-service/pkg"
)
2021-10-25 19:06:05 +00:00
type blockRange [2]uint64
func createStateDiffService() *sd.Service {
2021-10-25 19:06:05 +00:00
// load some necessary params
2021-10-21 17:06:06 +00:00
logWithCommand.Info("Loading statediff service parameters")
mode := viper.GetString("leveldb.mode")
2021-10-21 17:06:06 +00:00
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")
2021-10-21 17:06:06 +00:00
}
nodeInfo := getEthNodeInfo()
var chainConf *params.ChainConfig
var err error
chainConfigPath := viper.GetString("ethereum.chainConfig")
chainConf, err = LoadConfig(chainConfigPath)
2021-10-21 17:06:06 +00:00
if err != nil {
logWithCommand.Fatal(err)
}
// create LevelDB reader
logWithCommand.Info("Creating LevelDB reader")
2021-10-25 19:06:05 +00:00
readerConf := sd.LvLDBReaderConfig{
2021-10-21 17:06:06 +00:00
TrieConfig: &trie.Config{
Cache: viper.GetInt("cache.trie"),
Journal: "",
Preimages: false,
},
2021-10-25 19:06:05 +00:00
ChainConfig: chainConf,
Mode: mode,
2021-10-21 17:06:06 +00:00
Path: path,
AncientPath: ancientPath,
Url: url,
2021-10-21 17:06:06 +00:00
DBCacheSize: viper.GetInt("cache.database"),
}
2021-10-25 19:06:05 +00:00
lvlDBReader, err := sd.NewLvlDBReader(readerConf)
2021-10-21 17:06:06 +00:00
if err != nil {
logWithCommand.Fatal(err)
}
// create statediff service
logWithCommand.Info("Setting up database")
conf, err := getConfig(nodeInfo)
2021-10-21 17:06:06 +00:00
if err != nil {
logWithCommand.Fatal(err)
}
logWithCommand.Info("Creating statediff indexer")
2022-05-10 06:06:14 +00:00
_, indexer, err := ind.NewStateDiffIndexer(context.Background(), chainConf, nodeInfo, conf)
2021-10-21 17:06:06 +00:00
if err != nil {
logWithCommand.Fatal(err)
}
logWithCommand.Info("Creating statediff service")
sdConf := sd.ServiceConfig{
2021-10-25 19:06:05 +00:00
ServiceWorkers: viper.GetUint("statediff.serviceWorkers"),
TrieWorkers: viper.GetUint("statediff.trieWorkers"),
WorkerQueueSize: viper.GetUint("statediff.workerQueueSize"),
}
return sd.NewStateDiffService(lvlDBReader, indexer, sdConf)
2021-10-21 17:06:06 +00:00
}