updated utils to handle regtest, simnet

handle active network assignment in the same if-else
instead of having another switch-case
This commit is contained in:
Javed Khan 2014-07-21 11:12:41 +05:30 committed by Dave Collins
parent 1184a4d8d1
commit 458a996ae6
7 changed files with 194 additions and 72 deletions

View file

@ -387,14 +387,21 @@ func loadConfig() (*config, []string, error) {
// Multiple networks can't be selected simultaneously.
funcName := "loadConfig"
numNets := 0
// Count number of network flags passed; assign active network params
// while we're at it
if cfg.TestNet3 {
numNets++
activeNetParams = &testNet3Params
}
if cfg.RegressionTest {
numNets++
activeNetParams = &regressionNetParams
}
if cfg.SimNet {
numNets++
// Also disable dns seeding on the simulation test network.
activeNetParams = &simNetParams
cfg.DisableDNSSeed = true
}
if numNets > 1 {
str := "%s: The testnet, regtest, and simnet params can't be " +
@ -405,18 +412,6 @@ func loadConfig() (*config, []string, error) {
return nil, nil, err
}
// Choose the active network params based on the selected network.
switch {
case cfg.TestNet3:
activeNetParams = &testNet3Params
case cfg.RegressionTest:
activeNetParams = &regressionNetParams
case cfg.SimNet:
// Also disable dns seeding on the simulation test network.
activeNetParams = &simNetParams
cfg.DisableDNSSeed = true
}
// Append the network type to the data directory so it is "namespaced"
// per network. In addition to the block database, there are other
// pieces of data that are saved to disk such as address manager state.

View file

@ -24,21 +24,23 @@ const (
)
var (
btcdHomeDir = btcutil.AppDataDir("btcd", false)
defaultDataDir = filepath.Join(btcdHomeDir, "data")
knownDbTypes = btcdb.SupportedDBs()
activeNetwork = &btcnet.MainNetParams
btcdHomeDir = btcutil.AppDataDir("btcd", false)
defaultDataDir = filepath.Join(btcdHomeDir, "data")
knownDbTypes = btcdb.SupportedDBs()
activeNetParams = &btcnet.MainNetParams
)
// config defines the configuration options for findcheckpoint.
//
// See loadConfig for details on the configuration load process.
type config struct {
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"`
InFile string `short:"i" long:"infile" description:"File containing the block(s)"`
Progress int `short:"p" long:"progress" description:"Show a progress message each time this number of seconds have passed -- Use 0 to disable progress announcements"`
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"`
RegressionTest bool `long:"regtest" description:"Use the regression test network"`
SimNet bool `long:"simnet" description:"Use the simulation test network"`
InFile string `short:"i" long:"infile" description:"File containing the block(s)"`
Progress int `short:"p" long:"progress" description:"Show a progress message each time this number of seconds have passed -- Use 0 to disable progress announcements"`
}
// filesExists reports whether the named file or directory exists.
@ -100,9 +102,30 @@ func loadConfig() (*config, []string, error) {
return nil, nil, err
}
// Choose the active network based on the flags.
// Multiple networks can't be selected simultaneously.
funcName := "loadConfig"
numNets := 0
// Count number of network flags passed; assign active network params
// while we're at it
if cfg.TestNet3 {
activeNetwork = &btcnet.TestNet3Params
numNets++
activeNetParams = &btcnet.TestNet3Params
}
if cfg.RegressionTest {
numNets++
activeNetParams = &btcnet.RegressionNetParams
}
if cfg.SimNet {
numNets++
activeNetParams = &btcnet.SimNetParams
}
if numNets > 1 {
str := "%s: The testnet, regtest, and simnet params can't be " +
"used together -- choose one of the three"
err := fmt.Errorf(str, funcName)
fmt.Fprintln(os.Stderr, err)
parser.WriteHelp(os.Stderr)
return nil, nil, err
}
// Validate database type.
@ -121,7 +144,7 @@ func loadConfig() (*config, []string, error) {
// All data is specific to a network, so namespacing the data directory
// means each individual piece of serialized data does not have to
// worry about changing names per network and such.
cfg.DataDir = filepath.Join(cfg.DataDir, netName(activeNetwork))
cfg.DataDir = filepath.Join(cfg.DataDir, netName(activeNetParams))
// Ensure the specified block file exists.
if !fileExists(cfg.InFile) {

View file

@ -61,9 +61,9 @@ func (bi *blockImporter) readBlock() ([]byte, error) {
// No block and no error means there are no more blocks to read.
return nil, nil
}
if net != uint32(activeNetwork.Net) {
if net != uint32(activeNetParams.Net) {
return nil, fmt.Errorf("network mismatch -- got %x, want %x",
net, uint32(activeNetwork.Net))
net, uint32(activeNetParams.Net))
}
// Read the block length and ensure it is sane.
@ -305,7 +305,7 @@ func newBlockImporter(db btcdb.Db, r io.ReadSeeker) *blockImporter {
doneChan: make(chan bool),
errChan: make(chan error),
quit: make(chan struct{}),
chain: btcchain.New(db, activeNetwork, nil),
chain: btcchain.New(db, activeNetParams, nil),
lastLogTime: time.Now(),
}
}

View file

@ -14,6 +14,7 @@ import (
"github.com/conformal/btcdb"
_ "github.com/conformal/btcdb/ldb"
"github.com/conformal/btclog"
"github.com/conformal/btcnet"
"github.com/conformal/btcutil"
"github.com/conformal/btcwire"
flags "github.com/conformal/go-flags"
@ -22,16 +23,19 @@ import (
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"`
ShaString string `short:"s" description:"Block SHA to process" required:"true"`
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"`
RegressionTest bool `long:"regtest" description:"Use the regression test network"`
SimNet bool `long:"simnet" description:"Use the simulation test network"`
ShaString string `short:"s" description:"Block SHA to process" required:"true"`
}
var (
btcdHomeDir = btcutil.AppDataDir("btcd", false)
defaultDataDir = filepath.Join(btcdHomeDir, "data")
log btclog.Logger
btcdHomeDir = btcutil.AppDataDir("btcd", false)
defaultDataDir = filepath.Join(btcdHomeDir, "data")
log btclog.Logger
activeNetParams = &btcnet.MainNetParams
)
const (
@ -39,6 +43,24 @@ const (
ArgHeight
)
// netName returns the name used when referring to a bitcoin network. At the
// time of writing, btcd currently places blocks for testnet version 3 in the
// data and log directory "testnet", which does not match the Name field of the
// btcnet parameters. This function can be used to override this directory name
// as "testnet" when the passed active network matches btcwire.TestNet3.
//
// A proper upgrade to move the data and log directories for this network to
// "testnet3" is planned for the future, at which point this function can be
// removed and the network parameter's name used instead.
func netName(netParams *btcnet.Params) string {
switch netParams.Net {
case btcwire.TestNet3:
return "testnet"
default:
return netParams.Name
}
}
func main() {
cfg := config{
DbType: "leveldb",
@ -58,14 +80,32 @@ func main() {
log = btclog.NewSubsystemLogger(backendLogger, "")
btcdb.UseLogger(log)
var testnet string
// Multiple networks can't be selected simultaneously.
funcName := "main"
numNets := 0
// Count number of network flags passed; assign active network params
// while we're at it
if cfg.TestNet3 {
testnet = "testnet"
} else {
testnet = "mainnet"
numNets++
activeNetParams = &btcnet.TestNet3Params
}
cfg.DataDir = filepath.Join(cfg.DataDir, testnet)
if cfg.RegressionTest {
numNets++
activeNetParams = &btcnet.RegressionNetParams
}
if cfg.SimNet {
numNets++
activeNetParams = &btcnet.SimNetParams
}
if numNets > 1 {
str := "%s: The testnet, regtest, and simnet params can't be " +
"used together -- choose one of the three"
err := fmt.Errorf(str, funcName)
fmt.Fprintln(os.Stderr, err)
parser.WriteHelp(os.Stderr)
return
}
cfg.DataDir = filepath.Join(cfg.DataDir, netName(activeNetParams))
blockDbNamePrefix := "blocks"
dbName := blockDbNamePrefix + "_" + cfg.DbType

View file

@ -25,21 +25,23 @@ const (
)
var (
btcdHomeDir = btcutil.AppDataDir("btcd", false)
defaultDataDir = filepath.Join(btcdHomeDir, "data")
knownDbTypes = btcdb.SupportedDBs()
activeNetwork = &btcnet.MainNetParams
btcdHomeDir = btcutil.AppDataDir("btcd", false)
defaultDataDir = filepath.Join(btcdHomeDir, "data")
knownDbTypes = btcdb.SupportedDBs()
activeNetParams = &btcnet.MainNetParams
)
// config defines the configuration options for findcheckpoint.
//
// See loadConfig for details on the configuration load process.
type config struct {
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}"`
UseGoOutput bool `short:"g" long:"gooutput" description:"Display the candidates using Go syntax that is ready to insert into the btcchain checkpoint list"`
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"`
RegressionTest bool `long:"regtest" description:"Use the regression test network"`
SimNet bool `long:"simnet" description:"Use the simulation test network"`
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"`
}
// validDbType returns whether or not dbType is a supported database type.
@ -90,9 +92,30 @@ func loadConfig() (*config, []string, error) {
return nil, nil, err
}
// Choose the active network based on the flags.
// Multiple networks can't be selected simultaneously.
funcName := "loadConfig"
numNets := 0
// Count number of network flags passed; assign active network params
// while we're at it
if cfg.TestNet3 {
activeNetwork = &btcnet.TestNet3Params
numNets++
activeNetParams = &btcnet.TestNet3Params
}
if cfg.RegressionTest {
numNets++
activeNetParams = &btcnet.RegressionNetParams
}
if cfg.SimNet {
numNets++
activeNetParams = &btcnet.SimNetParams
}
if numNets > 1 {
str := "%s: The testnet, regtest, and simnet params can't be " +
"used together -- choose one of the three"
err := fmt.Errorf(str, funcName)
fmt.Fprintln(os.Stderr, err)
parser.WriteHelp(os.Stderr)
return nil, nil, err
}
// Validate database type.
@ -111,7 +134,7 @@ func loadConfig() (*config, []string, error) {
// All data is specific to a network, so namespacing the data directory
// means each individual piece of serialized data does not have to
// worry about changing names per network and such.
cfg.DataDir = filepath.Join(cfg.DataDir, netName(activeNetwork))
cfg.DataDir = filepath.Join(cfg.DataDir, netName(activeNetParams))
// Validate the number of candidates.
if cfg.NumCandidates < minCandidates || cfg.NumCandidates > maxCandidates {

View file

@ -53,7 +53,7 @@ func findCandidates(db btcdb.Db, latestHash *btcwire.ShaHash) ([]*btcnet.Checkpo
// Setup chain and get the latest checkpoint. Ignore notifications
// since they aren't needed for this util.
chain := btcchain.New(db, activeNetwork, nil)
chain := btcchain.New(db, activeNetParams, nil)
latestCheckpoint := chain.LatestCheckpoint()
if latestCheckpoint == nil {
return nil, fmt.Errorf("unable to retrieve latest checkpoint")

View file

@ -7,6 +7,7 @@ package main
import (
"encoding/binary"
"errors"
"fmt"
"io"
"os"
"path/filepath"
@ -15,6 +16,7 @@ import (
"github.com/conformal/btcdb"
_ "github.com/conformal/btcdb/ldb"
"github.com/conformal/btclog"
"github.com/conformal/btcnet"
"github.com/conformal/btcutil"
"github.com/conformal/btcwire"
flags "github.com/conformal/go-flags"
@ -24,22 +26,25 @@ import (
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" required:"true"`
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"`
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"`
RegressionTest bool `long:"regtest" description:"Use the regression test network"`
SimNet bool `long:"simnet" description:"Use the simulation test network"`
OutFile string `short:"o" description:"outfile"`
Progress bool `short:"p" description:"show progress"`
ShaString string `short:"s" description:"Block SHA to process" required:"true"`
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 (
btcdHomeDir = btcutil.AppDataDir("btcd", false)
defaultDataDir = filepath.Join(btcdHomeDir, "data")
log btclog.Logger
btcdHomeDir = btcutil.AppDataDir("btcd", false)
defaultDataDir = filepath.Join(btcdHomeDir, "data")
log btclog.Logger
activeNetParams = &btcnet.MainNetParams
)
const (
@ -47,6 +52,24 @@ const (
ArgHeight
)
// netName returns the name used when referring to a bitcoin network. At the
// time of writing, btcd currently places blocks for testnet version 3 in the
// data and log directory "testnet", which does not match the Name field of the
// btcnet parameters. This function can be used to override this directory name
// as "testnet" when the passed active network matches btcwire.TestNet3.
//
// A proper upgrade to move the data and log directories for this network to
// "testnet3" is planned for the future, at which point this function can be
// removed and the network parameter's name used instead.
func netName(netParams *btcnet.Params) string {
switch netParams.Net {
case btcwire.TestNet3:
return "testnet"
default:
return netParams.Name
}
}
func main() {
end := int64(-1)
@ -68,14 +91,32 @@ func main() {
log = btclog.NewSubsystemLogger(backendLogger, "")
btcdb.UseLogger(log)
var testnet string
// Multiple networks can't be selected simultaneously.
funcName := "main"
numNets := 0
// Count number of network flags passed; assign active network params
// while we're at it
if cfg.TestNet3 {
testnet = "testnet"
} else {
testnet = "mainnet"
numNets++
activeNetParams = &btcnet.TestNet3Params
}
cfg.DataDir = filepath.Join(cfg.DataDir, testnet)
if cfg.RegressionTest {
numNets++
activeNetParams = &btcnet.RegressionNetParams
}
if cfg.SimNet {
numNets++
activeNetParams = &btcnet.SimNetParams
}
if numNets > 1 {
str := "%s: The testnet, regtest, and simnet params can't be " +
"used together -- choose one of the three"
err := fmt.Errorf(str, funcName)
fmt.Fprintln(os.Stderr, err)
parser.WriteHelp(os.Stderr)
return
}
cfg.DataDir = filepath.Join(cfg.DataDir, netName(activeNetParams))
blockDbNamePrefix := "blocks"
dbName := blockDbNamePrefix + "_" + cfg.DbType