chaincfg: Register networks instead of hard coding. (#660)

This modifies the chaincfg package to register the default network
params via the init function instead of manually hard coding their data
into the maps.  This is less error prone when adding new default
networks.

A new function named mustRegister has been introduced that panics if
there are any errors when registering the network that the new code
makes use of and appropriate tests have been added.
This commit is contained in:
Dave Collins 2016-04-11 12:34:28 -05:00
parent 23f59144c7
commit c7e5d56b58
2 changed files with 44 additions and 25 deletions

View file

@ -337,31 +337,10 @@ var (
)
var (
registeredNets = map[wire.BitcoinNet]struct{}{
MainNetParams.Net: {},
TestNet3Params.Net: {},
RegressionNetParams.Net: {},
SimNetParams.Net: {},
}
pubKeyHashAddrIDs = map[byte]struct{}{
MainNetParams.PubKeyHashAddrID: {},
TestNet3Params.PubKeyHashAddrID: {}, // shared with regtest
SimNetParams.PubKeyHashAddrID: {},
}
scriptHashAddrIDs = map[byte]struct{}{
MainNetParams.ScriptHashAddrID: {},
TestNet3Params.ScriptHashAddrID: {}, // shared with regtest
SimNetParams.ScriptHashAddrID: {},
}
// Testnet is shared with regtest.
hdPrivToPubKeyIDs = map[[4]byte][]byte{
MainNetParams.HDPrivateKeyID: MainNetParams.HDPublicKeyID[:],
TestNet3Params.HDPrivateKeyID: TestNet3Params.HDPublicKeyID[:],
SimNetParams.HDPrivateKeyID: SimNetParams.HDPublicKeyID[:],
}
registeredNets = make(map[wire.BitcoinNet]struct{})
pubKeyHashAddrIDs = make(map[byte]struct{})
scriptHashAddrIDs = make(map[byte]struct{})
hdPrivToPubKeyIDs = make(map[[4]byte][]byte)
)
// Register registers the network parameters for a Bitcoin network. This may
@ -384,6 +363,14 @@ func Register(params *Params) error {
return nil
}
// mustRegister performs the same function as Register except it panics if there
// is an error. This should only be called from package init functions.
func mustRegister(params *Params) {
if err := Register(params); err != nil {
panic("failed to register network: " + err.Error())
}
}
// IsPubKeyHashAddrID returns whether the id is an identifier known to prefix a
// pay-to-pubkey-hash address on any default or registered network. This is
// used when decoding an address string into a specific address type. It is up
@ -442,3 +429,11 @@ func newShaHashFromStr(hexStr string) *wire.ShaHash {
}
return sha
}
func init() {
// Register all default networks when the package is initialized.
mustRegister(&MainNetParams)
mustRegister(&TestNet3Params)
mustRegister(&RegressionNetParams)
mustRegister(&SimNetParams)
}

24
chaincfg/params_test.go Normal file
View file

@ -0,0 +1,24 @@
// Copyright (c) 2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package chaincfg
import "testing"
// TestMustRegisterPanic ensures the mustRegister function panics when used to
// register an invalid network.
func TestMustRegisterPanic(t *testing.T) {
t.Parallel()
// Setup a defer to catch the expected panic to ensure it actually
// paniced.
defer func() {
if err := recover(); err == nil {
t.Error("mustRegister did not panic as expected")
}
}()
// Intentionally try to register duplicate params to force a panic.
mustRegister(&MainNetParams)
}