Skip to content

Commit

Permalink
updated utils to handle regtest, simnet
Browse files Browse the repository at this point in the history
handle active network assignment in the same if-else
instead of having another switch-case
  • Loading branch information
tuxcanfly authored and davecgh committed Jul 22, 2014
1 parent 1184a4d commit 458a996
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 72 deletions.
19 changes: 7 additions & 12 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 " +
Expand All @@ -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.
Expand Down
47 changes: 35 additions & 12 deletions util/addblock/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions util/addblock/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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(),
}
}
66 changes: 53 additions & 13 deletions util/dropafter/dropafter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -22,23 +23,44 @@ 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 (
ArgSha = iota
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",
Expand All @@ -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
Expand Down
47 changes: 35 additions & 12 deletions util/findcheckpoint/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion util/findcheckpoint/findcheckpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading

0 comments on commit 458a996

Please sign in to comment.