diff --git a/chaincfg/params.go b/chaincfg/params.go index 0b1689939..8bcc5a8dd 100644 --- a/chaincfg/params.go +++ b/chaincfg/params.go @@ -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) +} diff --git a/chaincfg/params_test.go b/chaincfg/params_test.go new file mode 100644 index 000000000..b6651f62d --- /dev/null +++ b/chaincfg/params_test.go @@ -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) +}