From 8d8ff99d19508fb6ed58550813bf3ea33c565aab Mon Sep 17 00:00:00 2001 From: i-norden Date: Mon, 10 Oct 2022 13:38:55 -0500 Subject: [PATCH] cmd for checking latest header height/hash; update serve command to report the latest header height/hash --- cmd/serve.go | 14 ++++++- cmd/stats.go | 54 ++++++++++++++++++++++++ cmd/util.go | 116 ++++++++++++++++++++++++++------------------------- 3 files changed, 127 insertions(+), 57 deletions(-) create mode 100644 cmd/stats.go diff --git a/cmd/serve.go b/cmd/serve.go index d9133c0..c0e6068 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -63,7 +63,19 @@ func serve() { logWithCommand.Info("Running eth-statediff-service serve command") logWithCommand.Infof("Parallelism: %d", maxParallelism()) - statediffService, err := createStateDiffService() + reader, chainConf, nodeInfo := instantiateLevelDBReader() + + // report latest block info + 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.Infof("latest block found in the levelDB\r\nheight: %s, hash: %s", header.Number.String(), header.Hash().Hex()) + + statediffService, err := createStateDiffService(reader, chainConf, nodeInfo) if err != nil { logWithCommand.Fatal(err) } diff --git a/cmd/stats.go b/cmd/stats.go new file mode 100644 index 0000000..b4bd04a --- /dev/null +++ b/cmd/stats.go @@ -0,0 +1,54 @@ +// Copyright © 2022 Vulcanize, Inc +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package cmd + +import ( + "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +// statsCmd represents the serve command +var statsCmd = &cobra.Command{ + Use: "stats", + Short: "Report stats for cold levelDB", + Long: `Usage + +./eth-statediff-service stats --config={path to toml config file}`, + Run: func(cmd *cobra.Command, args []string) { + subCommand = cmd.CalledAs() + logWithCommand = *logrus.WithField("SubCommand", subCommand) + stats() + }, +} + +func init() { + rootCmd.AddCommand(statsCmd) +} + +func stats() { + logWithCommand.Info("Running eth-statediff-service stats command") + + reader, _, _ := instantiateLevelDBReader() + + 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.Infof("latest block found in the levelDB\r\nheight: %s, hash: %s", header.Number.String(), header.Hash().Hex()) +} diff --git a/cmd/util.go b/cmd/util.go index 078f803..ad1c814 100644 --- a/cmd/util.go +++ b/cmd/util.go @@ -7,6 +7,7 @@ import ( "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/statediff" ind "github.com/ethereum/go-ethereum/statediff/indexer" + "github.com/ethereum/go-ethereum/statediff/indexer/node" "github.com/ethereum/go-ethereum/statediff/indexer/shared" "github.com/ethereum/go-ethereum/trie" "github.com/spf13/viper" @@ -17,62 +18,7 @@ import ( type blockRange [2]uint64 -func createStateDiffService() (sd.StateDiffService, error) { - // 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") - - if chainConfigPath != "" { - chainConf, err = statediff.LoadConfig(chainConfigPath) - } else { - chainConf, err = statediff.ChainConfig(nodeInfo.ChainID) - } - - 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) - } - +func createStateDiffService(lvlDBReader sd.Reader, chainConf *params.ChainConfig, nodeInfo node.Info) (sd.StateDiffService, error) { // create statediff service logWithCommand.Info("Setting up database") conf, err := getConfig(nodeInfo) @@ -140,3 +86,61 @@ func setupPreRunRanges() []sd.RangeRequest { return blockRanges } + +func instantiateLevelDBReader() (sd.Reader, *params.ChainConfig, node.Info) { + // 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") + + if chainConfigPath != "" { + chainConf, err = statediff.LoadConfig(chainConfigPath) + } else { + chainConf, err = statediff.ChainConfig(nodeInfo.ChainID) + } + + if err != nil { + logWithCommand.Fatalf("unable to instantiate chain config: %s", err.Error()) + } + + // 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"), + } + reader, err := sd.NewLvlDBReader(readerConf) + if err != nil { + logWithCommand.Fatalf("unable to instantiate levelDB reader: %s", err.Error()) + } + return reader, chainConf, nodeInfo +}