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

View file

@ -24,21 +24,23 @@ const (
) )
var ( var (
btcdHomeDir = btcutil.AppDataDir("btcd", false) btcdHomeDir = btcutil.AppDataDir("btcd", false)
defaultDataDir = filepath.Join(btcdHomeDir, "data") defaultDataDir = filepath.Join(btcdHomeDir, "data")
knownDbTypes = btcdb.SupportedDBs() knownDbTypes = btcdb.SupportedDBs()
activeNetwork = &btcnet.MainNetParams activeNetParams = &btcnet.MainNetParams
) )
// config defines the configuration options for findcheckpoint. // config defines the configuration options for findcheckpoint.
// //
// 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"` DbType string `long:"dbtype" description:"Database backend to use for the Block Chain"`
TestNet3 bool `long:"testnet" description:"Use the test network"` TestNet3 bool `long:"testnet" description:"Use the test network"`
InFile string `short:"i" long:"infile" description:"File containing the block(s)"` RegressionTest bool `long:"regtest" description:"Use the regression test network"`
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"` 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. // filesExists reports whether the named file or directory exists.
@ -100,9 +102,30 @@ func loadConfig() (*config, []string, error) {
return nil, nil, err 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 { 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. // Validate database type.
@ -121,7 +144,7 @@ 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 = filepath.Join(cfg.DataDir, netName(activeNetwork)) cfg.DataDir = filepath.Join(cfg.DataDir, netName(activeNetParams))
// Ensure the specified block file exists. // Ensure the specified block file exists.
if !fileExists(cfg.InFile) { 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. // No block and no error means there are no more blocks to read.
return nil, nil return nil, nil
} }
if net != uint32(activeNetwork.Net) { if net != uint32(activeNetParams.Net) {
return nil, fmt.Errorf("network mismatch -- got %x, want %x", 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. // 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), doneChan: make(chan bool),
errChan: make(chan error), errChan: make(chan error),
quit: make(chan struct{}), quit: make(chan struct{}),
chain: btcchain.New(db, activeNetwork, nil), chain: btcchain.New(db, activeNetParams, nil),
lastLogTime: time.Now(), lastLogTime: time.Now(),
} }
} }

View file

@ -14,6 +14,7 @@ import (
"github.com/conformal/btcdb" "github.com/conformal/btcdb"
_ "github.com/conformal/btcdb/ldb" _ "github.com/conformal/btcdb/ldb"
"github.com/conformal/btclog" "github.com/conformal/btclog"
"github.com/conformal/btcnet"
"github.com/conformal/btcutil" "github.com/conformal/btcutil"
"github.com/conformal/btcwire" "github.com/conformal/btcwire"
flags "github.com/conformal/go-flags" flags "github.com/conformal/go-flags"
@ -22,16 +23,19 @@ import (
type ShaHash btcwire.ShaHash type ShaHash btcwire.ShaHash
type config struct { type config struct {
DataDir string `short:"b" long:"datadir" description:"Directory to store data"` DataDir string `short:"b" long:"datadir" description:"Directory to store data"`
DbType string `long:"dbtype" description:"Database backend"` DbType string `long:"dbtype" description:"Database backend"`
TestNet3 bool `long:"testnet" description:"Use the test network"` TestNet3 bool `long:"testnet" description:"Use the test network"`
ShaString string `short:"s" description:"Block SHA to process" required:"true"` 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 ( var (
btcdHomeDir = btcutil.AppDataDir("btcd", false) btcdHomeDir = btcutil.AppDataDir("btcd", false)
defaultDataDir = filepath.Join(btcdHomeDir, "data") defaultDataDir = filepath.Join(btcdHomeDir, "data")
log btclog.Logger log btclog.Logger
activeNetParams = &btcnet.MainNetParams
) )
const ( const (
@ -39,6 +43,24 @@ const (
ArgHeight 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() { func main() {
cfg := config{ cfg := config{
DbType: "leveldb", DbType: "leveldb",
@ -58,14 +80,32 @@ func main() {
log = btclog.NewSubsystemLogger(backendLogger, "") log = btclog.NewSubsystemLogger(backendLogger, "")
btcdb.UseLogger(log) 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 { if cfg.TestNet3 {
testnet = "testnet" numNets++
} else { activeNetParams = &btcnet.TestNet3Params
testnet = "mainnet"
} }
if cfg.RegressionTest {
cfg.DataDir = filepath.Join(cfg.DataDir, testnet) 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" blockDbNamePrefix := "blocks"
dbName := blockDbNamePrefix + "_" + cfg.DbType dbName := blockDbNamePrefix + "_" + cfg.DbType

View file

@ -25,21 +25,23 @@ const (
) )
var ( var (
btcdHomeDir = btcutil.AppDataDir("btcd", false) btcdHomeDir = btcutil.AppDataDir("btcd", false)
defaultDataDir = filepath.Join(btcdHomeDir, "data") defaultDataDir = filepath.Join(btcdHomeDir, "data")
knownDbTypes = btcdb.SupportedDBs() knownDbTypes = btcdb.SupportedDBs()
activeNetwork = &btcnet.MainNetParams activeNetParams = &btcnet.MainNetParams
) )
// config defines the configuration options for findcheckpoint. // config defines the configuration options for findcheckpoint.
// //
// 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"` DbType string `long:"dbtype" description:"Database backend to use for the Block Chain"`
TestNet3 bool `long:"testnet" description:"Use the test network"` 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}"` RegressionTest bool `long:"regtest" description:"Use the regression test network"`
UseGoOutput bool `short:"g" long:"gooutput" description:"Display the candidates using Go syntax that is ready to insert into the btcchain checkpoint list"` 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. // validDbType returns whether or not dbType is a supported database type.
@ -90,9 +92,30 @@ func loadConfig() (*config, []string, error) {
return nil, nil, err 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 { 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. // Validate database type.
@ -111,7 +134,7 @@ 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 = filepath.Join(cfg.DataDir, netName(activeNetwork)) cfg.DataDir = filepath.Join(cfg.DataDir, netName(activeNetParams))
// Validate the number of candidates. // Validate the number of candidates.
if cfg.NumCandidates < minCandidates || cfg.NumCandidates > maxCandidates { 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 // 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, activeNetwork, nil) chain := btcchain.New(db, activeNetParams, 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

@ -7,6 +7,7 @@ package main
import ( import (
"encoding/binary" "encoding/binary"
"errors" "errors"
"fmt"
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
@ -15,6 +16,7 @@ import (
"github.com/conformal/btcdb" "github.com/conformal/btcdb"
_ "github.com/conformal/btcdb/ldb" _ "github.com/conformal/btcdb/ldb"
"github.com/conformal/btclog" "github.com/conformal/btclog"
"github.com/conformal/btcnet"
"github.com/conformal/btcutil" "github.com/conformal/btcutil"
"github.com/conformal/btcwire" "github.com/conformal/btcwire"
flags "github.com/conformal/go-flags" flags "github.com/conformal/go-flags"
@ -24,22 +26,25 @@ import (
type ShaHash btcwire.ShaHash type ShaHash btcwire.ShaHash
type config struct { type config struct {
DataDir string `short:"b" long:"datadir" description:"Directory to store data"` DataDir string `short:"b" long:"datadir" description:"Directory to store data"`
DbType string `long:"dbtype" description:"Database backend"` DbType string `long:"dbtype" description:"Database backend"`
TestNet3 bool `long:"testnet" description:"Use the test network"` TestNet3 bool `long:"testnet" description:"Use the test network"`
OutFile string `short:"o" description:"outfile"` RegressionTest bool `long:"regtest" description:"Use the regression test network"`
Progress bool `short:"p" description:"show progress"` SimNet bool `long:"simnet" description:"Use the simulation test network"`
ShaString string `short:"s" description:"Block SHA to process" required:"true"` OutFile string `short:"o" description:"outfile"`
EShaString string `short:"e" description:"End Block SHA to process"` Progress bool `short:"p" description:"show progress"`
RawBlock bool `short:"r" description:"Raw Block"` ShaString string `short:"s" description:"Block SHA to process" required:"true"`
FmtBlock bool `short:"f" description:"Format Block"` EShaString string `short:"e" description:"End Block SHA to process"`
ShowTx bool `short:"t" description:"Show transaction"` RawBlock bool `short:"r" description:"Raw Block"`
FmtBlock bool `short:"f" description:"Format Block"`
ShowTx bool `short:"t" description:"Show transaction"`
} }
var ( var (
btcdHomeDir = btcutil.AppDataDir("btcd", false) btcdHomeDir = btcutil.AppDataDir("btcd", false)
defaultDataDir = filepath.Join(btcdHomeDir, "data") defaultDataDir = filepath.Join(btcdHomeDir, "data")
log btclog.Logger log btclog.Logger
activeNetParams = &btcnet.MainNetParams
) )
const ( const (
@ -47,6 +52,24 @@ const (
ArgHeight 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() { func main() {
end := int64(-1) end := int64(-1)
@ -68,14 +91,32 @@ func main() {
log = btclog.NewSubsystemLogger(backendLogger, "") log = btclog.NewSubsystemLogger(backendLogger, "")
btcdb.UseLogger(log) 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 { if cfg.TestNet3 {
testnet = "testnet" numNets++
} else { activeNetParams = &btcnet.TestNet3Params
testnet = "mainnet"
} }
if cfg.RegressionTest {
cfg.DataDir = filepath.Join(cfg.DataDir, testnet) 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" blockDbNamePrefix := "blocks"
dbName := blockDbNamePrefix + "_" + cfg.DbType dbName := blockDbNamePrefix + "_" + cfg.DbType