111 lines
3.0 KiB
Go
111 lines
3.0 KiB
Go
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
|
|
ind "github.com/cerc-io/plugeth-statediff/indexer"
|
|
"github.com/cerc-io/plugeth-statediff/utils/log"
|
|
"github.com/ethereum/go-ethereum/params"
|
|
"github.com/ethereum/go-ethereum/trie"
|
|
"github.com/spf13/viper"
|
|
|
|
sd "github.com/vulcanize/eth-statediff-service/pkg"
|
|
)
|
|
|
|
type blockRange [2]uint64
|
|
|
|
func createStateDiffService() *sd.Service {
|
|
// load some necessary params
|
|
logWithCommand.Info("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()
|
|
|
|
var chainConf *params.ChainConfig
|
|
var err error
|
|
chainConfigPath := viper.GetString("ethereum.chainConfig")
|
|
chainConf, err = LoadConfig(chainConfigPath)
|
|
if err != nil {
|
|
logWithCommand.Fatal(err)
|
|
}
|
|
|
|
// create LevelDB reader
|
|
logWithCommand.Info("Creating LevelDB reader")
|
|
readerConf := sd.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"),
|
|
}
|
|
lvlDBReader, err := sd.NewLvlDBReader(readerConf)
|
|
if err != nil {
|
|
logWithCommand.Fatal(err)
|
|
}
|
|
|
|
// create statediff service
|
|
logWithCommand.Info("Setting up database")
|
|
conf, err := getConfig(nodeInfo)
|
|
if err != nil {
|
|
logWithCommand.Fatal(err)
|
|
}
|
|
logWithCommand.Info("Creating statediff indexer")
|
|
_, indexer, err := ind.NewStateDiffIndexer(context.Background(), chainConf, nodeInfo, conf)
|
|
if err != nil {
|
|
logWithCommand.Fatal(err)
|
|
}
|
|
logWithCommand.Info("Creating statediff service")
|
|
sdConf := sd.ServiceConfig{
|
|
ServiceWorkers: viper.GetUint("statediff.serviceWorkers"),
|
|
TrieWorkers: viper.GetUint("statediff.trieWorkers"),
|
|
WorkerQueueSize: viper.GetUint("statediff.workerQueueSize"),
|
|
}
|
|
return sd.NewStateDiffService(lvlDBReader, indexer, sdConf)
|
|
}
|
|
|
|
// LoadConfig loads chain config from json file
|
|
func LoadConfig(chainConfigPath string) (*params.ChainConfig, error) {
|
|
file, err := os.Open(chainConfigPath)
|
|
if err != nil {
|
|
log.Error(fmt.Sprintf("Failed to read chain config file: %v", err))
|
|
|
|
return nil, err
|
|
}
|
|
defer file.Close()
|
|
|
|
chainConfig := new(params.ChainConfig)
|
|
if err := json.NewDecoder(file).Decode(chainConfig); err != nil {
|
|
log.Error(fmt.Sprintf("invalid chain config file: %v", err))
|
|
|
|
return nil, err
|
|
}
|
|
|
|
log.Info(fmt.Sprintf("Using chain config from %s file. Content %+v", chainConfigPath, chainConfig))
|
|
|
|
return chainConfig, nil
|
|
}
|