Improve showblock options, add --testnet and change default db to leveldb

to match btcd
This commit is contained in:
David Hill 2013-10-10 13:42:21 -04:00
parent 486907c441
commit c4d7b2d2c6

View file

@ -7,12 +7,12 @@ package main
import ( import (
"encoding/binary" "encoding/binary"
"errors" "errors"
"flag"
"fmt" "fmt"
"github.com/conformal/btcdb" "github.com/conformal/btcdb"
_ "github.com/conformal/btcdb/ldb" _ "github.com/conformal/btcdb/ldb"
_ "github.com/conformal/btcdb/sqlite3" _ "github.com/conformal/btcdb/sqlite3"
"github.com/conformal/btcwire" "github.com/conformal/btcwire"
"github.com/conformal/go-flags"
"github.com/conformal/seelog" "github.com/conformal/seelog"
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
"io" "io"
@ -23,6 +23,19 @@ import (
type ShaHash btcwire.ShaHash type ShaHash btcwire.ShaHash
type config struct {
DataDir string `short:"b" long:"datadir" description:"Directory to store data"`
DbType string `long:"dbtype" description:"Database backend"`
TestNet3 bool `long:"testnet" description:"Use the test network"`
OutFile string `short:"o" description:"outfile"`
Progress bool `short:"p" description:"show progress"`
ShaString string `short:"s" description:"Block SHA to process"`
EShaString string `short:"e" description:"End Block SHA to process"`
RawBlock bool `short:"r" description:"Raw Block"`
FmtBlock bool `short:"f" description:"Format Block"`
ShowTx bool `short:"t" description:"Show transaction"`
}
var log seelog.LoggerInterface var log seelog.LoggerInterface
const ( const (
@ -31,26 +44,17 @@ const (
) )
func main() { func main() {
var err error
var dbType string
var datadir string
var shastring, eshastring, outfile string
var rflag, fflag, tflag, testnetflag bool
var progress int
end := int64(-1) end := int64(-1)
flag.StringVar(&dbType, "dbtype", "", "Database backend to use for the Block Chain")
flag.StringVar(&datadir, "datadir", ".", "Directory to store data")
flag.StringVar(&shastring, "s", "", "Block sha to process") cfg := config{DbType: "leveldb"}
flag.StringVar(&eshastring, "e", "", "Block sha to process") parser := flags.NewParser(&cfg, flags.Default)
flag.StringVar(&outfile, "o", "", "outfile") _, err := parser.Parse()
flag.BoolVar(&rflag, "r", false, "raw block") if err != nil {
flag.BoolVar(&fflag, "f", false, "fmt block") if e, ok := err.(*flags.Error); !ok || e.Type != flags.ErrHelp {
flag.BoolVar(&tflag, "t", false, "show transactions") parser.WriteHelp(os.Stderr)
flag.BoolVar(&testnetflag, "testnet", false, "use testnet db") }
flag.IntVar(&progress, "p", 0, "show progress") return
}
flag.Parse()
log, err = seelog.LoggerFromWriterWithMinLevel(os.Stdout, log, err = seelog.LoggerFromWriterWithMinLevel(os.Stdout,
seelog.InfoLvl) seelog.InfoLvl)
@ -61,28 +65,27 @@ func main() {
defer log.Flush() defer log.Flush()
btcdb.UseLogger(log) btcdb.UseLogger(log)
if len(dbType) == 0 { if len(cfg.DataDir) == 0 {
dbType = "sqlite" cfg.DataDir = filepath.Join(btcdHomeDir(), "data")
}
var testnet string
if cfg.TestNet3 {
testnet = "testnet"
} else {
testnet = "mainnet"
} }
if len(datadir) == 0 { cfg.DataDir = filepath.Join(cfg.DataDir, testnet)
datadir = filepath.Join(btcdHomeDir(), "data")
}
if testnetflag {
datadir = filepath.Join(datadir, "testnet")
} else {
datadir = filepath.Join(datadir, "mainnet")
}
blockDbNamePrefix := "blocks" blockDbNamePrefix := "blocks"
dbName := blockDbNamePrefix + "_" + dbType dbName := blockDbNamePrefix + "_" + cfg.DbType
if dbType == "sqlite" { if cfg.DbType == "sqlite" {
dbName = dbName + ".db" dbName = dbName + ".db"
} }
dbPath := filepath.Join(datadir, dbName) dbPath := filepath.Join(cfg.DataDir, dbName)
log.Infof("loading db %v", dbType) log.Infof("loading db %v", cfg.DbType)
db, err := btcdb.OpenDB(dbType, dbPath) db, err := btcdb.OpenDB(cfg.DbType, dbPath)
if err != nil { if err != nil {
log.Warnf("db open failed: %v", err) log.Warnf("db open failed: %v", err)
return return
@ -90,15 +93,15 @@ func main() {
defer db.Close() defer db.Close()
log.Infof("db load complete") log.Infof("db load complete")
height, err := getHeight(db, shastring) height, err := getHeight(db, cfg.ShaString)
if err != nil { if err != nil {
log.Infof("Invalid block %v", shastring) log.Infof("Invalid block %v", cfg.ShaString)
return return
} }
if eshastring != "" { if cfg.EShaString != "" {
end, err = getHeight(db, eshastring) end, err = getHeight(db, cfg.EShaString)
if err != nil { if err != nil {
log.Infof("Invalid end block %v", eshastring) log.Infof("Invalid end block %v", cfg.EShaString)
return return
} }
} else { } else {
@ -108,28 +111,28 @@ func main() {
log.Infof("height %v end %v", height, end) log.Infof("height %v end %v", height, end)
var fo io.WriteCloser var fo io.WriteCloser
if outfile != "" { if cfg.OutFile != "" {
fo, err = os.Create(outfile) fo, err = os.Create(cfg.OutFile)
if err != nil { if err != nil {
log.Warnf("failed to open file %v, err %v", outfile, err) log.Warnf("failed to open file %v, err %v", cfg.OutFile, err)
} }
defer func() { defer func() {
if err := fo.Close(); err != nil { if err := fo.Close(); err != nil {
log.Warn("failed to close file %v %v", outfile, err) log.Warn("failed to close file %v %v", cfg.OutFile, err)
} }
}() }()
} }
for ; height < end; height++ { for ; height < end; height++ {
if progress != 0 && height%int64(progress) == 0 { if cfg.Progress && height%int64(1) == 0 {
log.Infof("Processing block %v", height) log.Infof("Processing block %v", height)
} }
err = DumpBlock(db, height, fo, rflag, fflag, tflag) err = DumpBlock(db, height, fo, cfg.RawBlock, cfg.FmtBlock, cfg.ShowTx)
if err != nil { if err != nil {
break break
} }
} }
if progress != 0 { if cfg.Progress {
height-- height--
log.Infof("Processing block %v", height) log.Infof("Processing block %v", height)
} }