Update findcheckpoint to support testnet and dbtype.

This commit is contained in:
Dave Collins 2013-10-07 20:53:25 -05:00
parent 1d2c48555b
commit d4e006e515
3 changed files with 53 additions and 20 deletions

View file

@ -6,6 +6,10 @@ package main
import ( import (
"fmt" "fmt"
"github.com/conformal/btcdb"
_ "github.com/conformal/btcdb/ldb"
_ "github.com/conformal/btcdb/sqlite3"
"github.com/conformal/btcwire"
"github.com/conformal/go-flags" "github.com/conformal/go-flags"
"os" "os"
"path/filepath" "path/filepath"
@ -15,10 +19,13 @@ const (
minCandidates = 1 minCandidates = 1
maxCandidates = 20 maxCandidates = 20
defaultNumCandidates = 5 defaultNumCandidates = 5
defaultDbType = "sqlite"
) )
var ( var (
defaultDataDir = filepath.Join(btcdHomeDir(), "data") defaultDataDir = filepath.Join(btcdHomeDir(), "data")
knownDbTypes = btcdb.SupportedDBs()
activeNetwork = btcwire.MainNet
) )
// config defines the configuration options for findcheckpoint. // config defines the configuration options for findcheckpoint.
@ -26,6 +33,8 @@ var (
// See loadConfig for details on the configuration load process. // See loadConfig for details on the configuration load process.
type config struct { type config struct {
DataDir string `short:"b" long:"datadir" description:"Location of the btcd data directory"` DataDir string `short:"b" long:"datadir" description:"Location of the btcd data directory"`
DbType string `long:"dbtype" description:"Database backend to use for the Block Chain"`
TestNet3 bool `long:"testnet" description:"Use the test network"`
NumCandidates int `short:"n" long:"numcandidates" description:"Max num of checkpoint candidates to show {1-20}"` NumCandidates int `short:"n" long:"numcandidates" description:"Max num of checkpoint candidates to show {1-20}"`
UseGoOutput bool `short:"g" long:"gooutput" description:"Display the candidates using Go syntax that is ready to insert into the btcchain checkpoint list"` UseGoOutput bool `short:"g" long:"gooutput" description:"Display the candidates using Go syntax that is ready to insert into the btcchain checkpoint list"`
} }
@ -48,21 +57,31 @@ func btcdHomeDir() string {
return "." return "."
} }
// validDbType returns whether or not dbType is a supported database type.
func validDbType(dbType string) bool {
for _, knownType := range knownDbTypes {
if dbType == knownType {
return true
}
}
return false
}
func netName(btcnet btcwire.BitcoinNet) string {
net := "mainnet"
if btcnet == btcwire.TestNet3 {
net = "testnet"
}
return net
}
// loadConfig initializes and parses the config using command line options. // loadConfig initializes and parses the config using command line options.
//
// The configuration proceeds as follows:
// 1) Start with a default config with sane settings
// 2) Pre-parse the command line to check for an alternative config file
// 3) Load configuration file overwriting defaults with any specified options
// 4) Parse CLI options and overwrite/add any specified options
//
// The above results in btcd functioning properly without any config settings
// while still allowing the user to override settings with config files and
// command line options. Command line options always take precedence.
func loadConfig() (*config, []string, error) { func loadConfig() (*config, []string, error) {
// Default config. // Default config.
cfg := config{ cfg := config{
DataDir: defaultDataDir, DataDir: defaultDataDir,
DbType: defaultDbType,
NumCandidates: defaultNumCandidates, NumCandidates: defaultNumCandidates,
} }
@ -76,11 +95,16 @@ func loadConfig() (*config, []string, error) {
return nil, nil, err return nil, nil, err
} }
// Validate the number of candidates. // Choose the active network based on the flags.
if cfg.NumCandidates < minCandidates || cfg.NumCandidates > maxCandidates { if cfg.TestNet3 {
str := "%s: The specified number of candidates is out of " + activeNetwork = btcwire.TestNet3
"range -- parsed [%v]" }
err = fmt.Errorf(str, "loadConfig", cfg.NumCandidates)
// Validate database type.
if !validDbType(cfg.DbType) {
str := "%s: The specified database type [%v] is invalid -- " +
"supported types %v"
err := fmt.Errorf(str, "loadConfig", cfg.DbType, knownDbTypes)
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
parser.WriteHelp(os.Stderr) parser.WriteHelp(os.Stderr)
return nil, nil, err return nil, nil, err
@ -92,8 +116,17 @@ func loadConfig() (*config, []string, error) {
// All data is specific to a network, so namespacing the data directory // All data is specific to a network, so namespacing the data directory
// means each individual piece of serialized data does not have to // means each individual piece of serialized data does not have to
// worry about changing names per network and such. // worry about changing names per network and such.
//cfg.DataDir = cleanAndExpandPath(cfg.DataDir) cfg.DataDir = filepath.Join(cfg.DataDir, netName(activeNetwork))
cfg.DataDir = filepath.Join(cfg.DataDir, "mainnet")
// Validate the number of candidates.
if cfg.NumCandidates < minCandidates || cfg.NumCandidates > maxCandidates {
str := "%s: The specified number of candidates is out of " +
"range -- parsed [%v]"
err = fmt.Errorf(str, "loadConfig", cfg.NumCandidates)
fmt.Fprintln(os.Stderr, err)
parser.WriteHelp(os.Stderr)
return nil, nil, err
}
return &cfg, remainingArgs, nil return &cfg, remainingArgs, nil
} }

View file

@ -24,7 +24,7 @@ var (
// loadBlockDB opens the block database and returns a handle to it. // loadBlockDB opens the block database and returns a handle to it.
func loadBlockDB() (btcdb.Db, error) { func loadBlockDB() (btcdb.Db, error) {
// The database name is based on the database type. // The database name is based on the database type.
dbType := "leveldb" dbType := cfg.DbType
dbName := blockDbNamePrefix + "_" + dbType dbName := blockDbNamePrefix + "_" + dbType
if dbType == "sqlite" { if dbType == "sqlite" {
dbName = dbName + ".db" dbName = dbName + ".db"
@ -52,7 +52,7 @@ func findCandidates(db btcdb.Db, latestHash *btcwire.ShaHash) ([]*btcchain.Check
// Setup chain and get the latest checkpoint. Ignore notifications // Setup chain and get the latest checkpoint. Ignore notifications
// since they aren't needed for this util. // since they aren't needed for this util.
chain := btcchain.New(db, btcwire.MainNet, nil) chain := btcchain.New(db, activeNetwork, nil)
latestCheckpoint := chain.LatestCheckpoint() latestCheckpoint := chain.LatestCheckpoint()
if latestCheckpoint == nil { if latestCheckpoint == nil {
return nil, fmt.Errorf("unable to retrieve latest checkpoint") return nil, fmt.Errorf("unable to retrieve latest checkpoint")

View file

@ -67,7 +67,7 @@ func main() {
if len(datadir) == 0 { if len(datadir) == 0 {
datadir = filepath.Join(btcdHomeDir(), "data") datadir = filepath.Join(btcdHomeDir(), "data")
} }
datadir = filepath.Join(datadir, "mainnet") datadir = filepath.Join(datadir, "testnet")
blockDbNamePrefix := "blocks" blockDbNamePrefix := "blocks"
dbName := blockDbNamePrefix + "_" + dbType dbName := blockDbNamePrefix + "_" + dbType