2021-10-21 17:06:06 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2021-11-13 00:45:08 +00:00
|
|
|
"context"
|
2023-08-25 07:57:33 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2021-10-25 19:06:05 +00:00
|
|
|
|
2023-08-25 05:34:00 +00:00
|
|
|
gethsd "github.com/cerc-io/plugeth-statediff"
|
|
|
|
ind "github.com/cerc-io/plugeth-statediff/indexer"
|
2023-08-25 07:57:33 +00:00
|
|
|
"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
|
|
|
|
|
2023-08-25 07:57:33 +00:00
|
|
|
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")
|
2022-05-17 09:05:11 +00:00
|
|
|
mode := viper.GetString("leveldb.mode")
|
2021-10-21 17:06:06 +00:00
|
|
|
path := viper.GetString("leveldb.path")
|
|
|
|
ancientPath := viper.GetString("leveldb.ancient")
|
2022-05-17 09:05:11 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-11-13 00:45:08 +00:00
|
|
|
nodeInfo := getEthNodeInfo()
|
2022-05-12 14:28:08 +00:00
|
|
|
|
|
|
|
var chainConf *params.ChainConfig
|
|
|
|
var err error
|
|
|
|
chainConfigPath := viper.GetString("ethereum.chainConfig")
|
2023-08-25 07:57:33 +00:00
|
|
|
chainConf, err = LoadConfig(chainConfigPath)
|
2021-10-21 17:06:06 +00:00
|
|
|
if err != nil {
|
|
|
|
logWithCommand.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-05-17 09:05:11 +00:00
|
|
|
// 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,
|
2022-05-17 09:05:11 +00:00
|
|
|
Mode: mode,
|
2021-10-21 17:06:06 +00:00
|
|
|
Path: path,
|
|
|
|
AncientPath: ancientPath,
|
2022-05-17 09:05:11 +00:00
|
|
|
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
|
2021-11-13 00:45:08 +00:00
|
|
|
logWithCommand.Info("Setting up database")
|
|
|
|
conf, err := getConfig(nodeInfo)
|
2021-10-21 17:06:06 +00:00
|
|
|
if err != nil {
|
|
|
|
logWithCommand.Fatal(err)
|
|
|
|
}
|
2021-10-25 23:54:54 +00:00
|
|
|
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)
|
|
|
|
}
|
2021-10-25 23:54:54 +00:00
|
|
|
logWithCommand.Info("Creating statediff service")
|
2023-08-25 07:57:33 +00:00
|
|
|
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
|
|
|
}
|