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:
parent
23f59144c7
commit
c7e5d56b58
2 changed files with 44 additions and 25 deletions
|
@ -337,31 +337,10 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
registeredNets = map[wire.BitcoinNet]struct{}{
|
registeredNets = make(map[wire.BitcoinNet]struct{})
|
||||||
MainNetParams.Net: {},
|
pubKeyHashAddrIDs = make(map[byte]struct{})
|
||||||
TestNet3Params.Net: {},
|
scriptHashAddrIDs = make(map[byte]struct{})
|
||||||
RegressionNetParams.Net: {},
|
hdPrivToPubKeyIDs = make(map[[4]byte][]byte)
|
||||||
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[:],
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Register registers the network parameters for a Bitcoin network. This may
|
// Register registers the network parameters for a Bitcoin network. This may
|
||||||
|
@ -384,6 +363,14 @@ func Register(params *Params) error {
|
||||||
return nil
|
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
|
// 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
|
// 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
|
// 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
|
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
24
chaincfg/params_test.go
Normal 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)
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue