Update claimtrie CLI to support other tools #42

Merged
roylee17 merged 3 commits from roylee/fix-default-app-dir-name into master 2022-05-27 06:52:15 +02:00
4 changed files with 53 additions and 28 deletions

View file

@ -23,12 +23,14 @@ func NewBlocCommands() *cobra.Command {
return cmd return cmd
} }
func NewBlockBestCommand() *cobra.Command { func NewBlockBestCommand() *cobra.Command {
var showHash bool
var showHeight bool
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "best", 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 { RunE: func(cmd *cobra.Command, args []string) error {
db, err := loadBlocksDB() db, err := loadBlocksDB()
@ -43,12 +45,23 @@ func NewBlockBestCommand() *cobra.Command {
} }
state := chain.BestSnapshot() 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 return nil
}, },
} }
cmd.Flags().BoolVar(&showHeight, "showheight", true, "Display block height")
cmd.Flags().BoolVar(&showHash, "showhash", true, "Display block hash")
return cmd return cmd
} }
@ -56,10 +69,12 @@ func NewBlockListCommand() *cobra.Command {
var fromHeight int32 var fromHeight int32
var toHeight int32 var toHeight int32
var showHash bool
var showHeight bool
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "list", 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, Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
@ -83,7 +98,14 @@ func NewBlockListCommand() *cobra.Command {
if err != nil { if err != nil {
return errors.Wrapf(err, "load hash for %d", ht) 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 return nil
@ -92,6 +114,8 @@ func NewBlockListCommand() *cobra.Command {
cmd.Flags().Int32Var(&fromHeight, "from", 0, "From height (inclusive)") cmd.Flags().Int32Var(&fromHeight, "from", 0, "From height (inclusive)")
cmd.Flags().Int32Var(&toHeight, "to", 0, "To 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 cmd.Flags().SortFlags = false
return cmd return cmd

View file

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

View file

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

View file

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