[lbry] claimtrie: update CLI to support other tools

- fix default app dir name
- enable debug level for cli
- block sub command to output hash, height, or both
This commit is contained in:
Roy Lee 2022-05-26 21:52:15 -07:00 committed by GitHub
parent e48200f53a
commit 6e36118193
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 28 deletions

View file

@ -23,12 +23,14 @@ func NewBlocCommands() *cobra.Command {
return cmd
}
func NewBlockBestCommand() *cobra.Command {
var showHash bool
var showHeight bool
cmd := &cobra.Command{
Use: "best",
Short: "Show the height and hash of the best block",
Short: "Show the block hash and height of the best block",
RunE: func(cmd *cobra.Command, args []string) error {
db, err := loadBlocksDB()
@ -43,12 +45,23 @@ func NewBlockBestCommand() *cobra.Command {
}
state := chain.BestSnapshot()
fmt.Printf("Block %7d: %s\n", state.Height, state.Hash.String())
switch {
case showHeight && showHash:
fmt.Printf("%s:%d\n", state.Hash, state.Height)
case !showHeight && showHash:
fmt.Printf("%s\n", state.Hash)
case showHeight && !showHash:
fmt.Printf("%d\n", state.Height)
}
return nil
},
}
cmd.Flags().BoolVar(&showHeight, "showheight", true, "Display block height")
cmd.Flags().BoolVar(&showHash, "showhash", true, "Display block hash")
return cmd
}
@ -56,10 +69,12 @@ func NewBlockListCommand() *cobra.Command {
var fromHeight int32
var toHeight int32
var showHash bool
var showHeight bool
cmd := &cobra.Command{
Use: "list",
Short: "List merkle hash of blocks between <from_height> <to_height>",
Short: "List block hash and height between blocks <from_height> <to_height>",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
@ -83,7 +98,14 @@ func NewBlockListCommand() *cobra.Command {
if err != nil {
return errors.Wrapf(err, "load hash for %d", ht)
}
fmt.Printf("Block %7d: %s\n", ht, hash.String())
switch {
case showHeight && showHash:
fmt.Printf("%s:%d\n", hash, ht)
case !showHeight && showHash:
fmt.Printf("%s\n", hash)
case showHeight && !showHash:
fmt.Printf("%d\n", ht)
}
}
return nil
@ -92,6 +114,8 @@ func NewBlockListCommand() *cobra.Command {
cmd.Flags().Int32Var(&fromHeight, "from", 0, "From height (inclusive)")
cmd.Flags().Int32Var(&toHeight, "to", 0, "To height (inclusive)")
cmd.Flags().BoolVar(&showHeight, "showheight", true, "Display block height")
cmd.Flags().BoolVar(&showHash, "showhash", true, "Display block hash")
cmd.Flags().SortFlags = false
return cmd

View file

@ -15,7 +15,7 @@ import (
func loadBlocksDB() (database.DB, error) {
dbPath := filepath.Join(dataDir, netName, "blocks_ffldb")
log.Infof("Loading blocks database: %s", dbPath)
log.Debugf("Loading blocks database: %s", dbPath)
db, err := database.Open("ffldb", dbPath, chainPramas().Net)
if err != nil {
return nil, errors.Wrapf(err, "open blocks database")
@ -27,7 +27,7 @@ func loadBlocksDB() (database.DB, error) {
func loadChain(db database.DB) (*blockchain.BlockChain, error) {
paramsCopy := chaincfg.MainNetParams
log.Infof("Loading chain from database")
log.Debugf("Loading chain from database")
startTime := time.Now()
chain, err := blockchain.New(&blockchain.Config{
@ -40,7 +40,7 @@ func loadChain(db database.DB) (*blockchain.BlockChain, error) {
return nil, errors.Wrapf(err, "create blockchain")
}
log.Infof("Loaded chain from database (%s)", time.Since(startTime))
log.Debugf("Loaded chain from database (%s)", time.Since(startTime))
return chain, err

View file

@ -3,20 +3,21 @@ package cmd
import (
"os"
"github.com/btcsuite/btclog"
"github.com/lbryio/lbcd/claimtrie/config"
"github.com/lbryio/lbcd/claimtrie/param"
"github.com/lbryio/lbcd/limits"
"github.com/lbryio/lbcd/wire"
"github.com/btcsuite/btclog"
"github.com/spf13/cobra"
)
var (
log btclog.Logger
cfg = config.DefaultConfig
netName string
dataDir string
log = btclog.NewBackend(os.Stdout).Logger("CMDL")
cfg = config.DefaultConfig
netName string
dataDir string
debugLevel string
)
var rootCmd = NewRootCommand()
@ -28,6 +29,9 @@ func NewRootCommand() *cobra.Command {
Short: "ClaimTrie Command Line Interface",
SilenceUsage: true,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
level, _ := btclog.LevelFromString(debugLevel)
log.SetLevel(level)
switch netName {
case "mainnet":
param.SetNetwork(wire.MainNet)
@ -37,21 +41,20 @@ func NewRootCommand() *cobra.Command {
param.SetNetwork(wire.TestNet)
}
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
os.Stdout.Sync()
},
}
cmd.PersistentFlags().StringVar(&netName, "netname", "mainnet", "Net name")
cmd.PersistentFlags().StringVarP(&dataDir, "datadir", "b", cfg.DataDir, "Data dir")
cmd.PersistentFlags().StringVarP(&debugLevel, "debuglevel", "d", cfg.DebugLevel, "Debug level")
return cmd
}
func Execute() {
backendLogger := btclog.NewBackend(os.Stdout)
defer os.Stdout.Sync()
log = backendLogger.Logger("CMDL")
log.SetLevel(btclog.LevelDebug)
// Up some limits.
if err := limits.SetLimits(); err != nil {
log.Errorf("failed to set limits: %v\n", err)

View file

@ -8,11 +8,10 @@ import (
)
var DefaultConfig = Config{
Params: param.MainNet,
RamTrie: true, // as it stands the other trie uses more RAM, more time, and 40GB+ of disk space
DataDir: filepath.Join(btcutil.AppDataDir("chain", false), "data"),
Params: param.MainNet,
RamTrie: true, // as it stands the other trie uses more RAM, more time, and 40GB+ of disk space
DebugLevel: "info",
DataDir: filepath.Join(btcutil.AppDataDir("lbcd", false), "data"),
BlockRepoPebble: pebbleConfig{
Path: "blocks_pebble_db",
@ -30,11 +29,10 @@ var DefaultConfig = Config{
// Config is the container of all configurations.
type Config struct {
Params param.ClaimTrieParams
RamTrie bool
DataDir string
Params param.ClaimTrieParams
RamTrie bool
DataDir string
DebugLevel string
BlockRepoPebble pebbleConfig
NodeRepoPebble pebbleConfig