Convert addblock utility to use btcnet.

This commit is contained in:
Dave Collins 2014-05-26 23:55:10 -05:00
parent 7b0116dfd0
commit dd8265c22c
2 changed files with 21 additions and 11 deletions

View file

@ -8,6 +8,7 @@ import (
"fmt" "fmt"
"github.com/conformal/btcdb" "github.com/conformal/btcdb"
_ "github.com/conformal/btcdb/ldb" _ "github.com/conformal/btcdb/ldb"
"github.com/conformal/btcnet"
"github.com/conformal/btcutil" "github.com/conformal/btcutil"
"github.com/conformal/btcwire" "github.com/conformal/btcwire"
"github.com/conformal/go-flags" "github.com/conformal/go-flags"
@ -25,7 +26,7 @@ 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 = btcwire.MainNet activeNetwork = &btcnet.MainNetParams
) )
// config defines the configuration options for findcheckpoint. // config defines the configuration options for findcheckpoint.
@ -60,13 +61,22 @@ func validDbType(dbType string) bool {
return false return false
} }
// netName returns a human-readable name for the passed bitcoin network. // netName returns the name used when referring to a bitcoin network. At the
func netName(btcnet btcwire.BitcoinNet) string { // time of writing, btcd currently places blocks for testnet version 3 in the
net := "mainnet" // data and log directory "testnet", which does not match the Name field of the
if btcnet == btcwire.TestNet3 { // btcnet parameters. This function can be used to override this directory name
net = "testnet" // 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
} }
return net
} }
// loadConfig initializes and parses the config using command line options. // loadConfig initializes and parses the config using command line options.
@ -91,7 +101,7 @@ func loadConfig() (*config, []string, error) {
// Choose the active network based on the flags. // Choose the active network based on the flags.
if cfg.TestNet3 { if cfg.TestNet3 {
activeNetwork = btcwire.TestNet3 activeNetwork = &btcnet.TestNet3Params
} }
// Validate database type. // Validate database type.

View file

@ -60,9 +60,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) { if net != uint32(activeNetwork.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, uint32(activeNetwork.Net))
} }
// Read the block length and ensure it is sane. // Read the block length and ensure it is sane.
@ -288,7 +288,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 bool), quit: make(chan bool),
chain: btcchain.New(db, activeNetwork, nil), chain: btcchain.New(db, activeNetwork.Net, nil),
lastLogTime: time.Now(), lastLogTime: time.Now(),
} }
} }