dcrd/chaincfg/mainnetparams_test.go
Dave Collins cba25b056d
chaincfg: Convert global param defs to funcs.
The current network parameter definitions are all package-level globals
which have a few undesirable characteristics:

- They are subject to mutation and thus can easily cause subtle and hard
  to find bugs
- Many of the fields are defined in terms of others, but due to the
  struct being defined at the top level, it's not possible to reference
  other fields within it
- They require a lot of unexported global variable state
- It makes it difficult to introduce a base set of parameters that would
  likely help highlight exactly what the differences are between the
  networks

Consequently, this converts all of the global network parameter
defintions to functions that return a new instance of the relevant
parameters which either directly addresses the aforementioned point, or
at the very least, allows them to be addresses in future commits.

The following is a high level overview of the changes:

- Move all default definitions into functions that return a new instance
  of the relevant params
- Move global defintions of variables used in the structs into the
  relevant funcs
- Improve consistency in the tests for each set of network params
- Update example in README.md
- Update example doc.go
2019-06-12 20:14:34 -05:00

53 lines
1.9 KiB
Go

// Copyright (c) 2014-2016 The btcsuite developers
// Copyright (c) 2015-2019 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package chaincfg
import (
"bytes"
"encoding/hex"
"testing"
"github.com/davecgh/go-spew/spew"
)
// TestGenesisBlock tests the genesis block of the main network for validity by
// checking the encoded bytes and hashes.
func TestGenesisBlock(t *testing.T) {
genesisBlockBytes, _ := hex.DecodeString("0100000000000000000000000000" +
"000000000000000000000000000000000000000000000dc101dfc3c6a2eb10ca0" +
"c5374e10d28feb53f7eabcc850511ceadb99174aa660000000000000000000000" +
"00000000000000000000000000000000000000000000000000000000000000000" +
"000000000ffff011b00c2eb0b000000000000000000000000a0d7b85600000000" +
"00000000000000000000000000000000000000000000000000000000000000000" +
"00000000101000000010000000000000000000000000000000000000000000000" +
"000000000000000000ffffffff00ffffffff01000000000000000000002080167" +
"9e98561ada96caec2949a5d41c4cab3851eb740d951c10ecbcf265c1fd9000000" +
"000000000001ffffffffffffffff00000000ffffffff02000000")
// Encode the genesis block to raw bytes.
params := MainNetParams()
var buf bytes.Buffer
err := params.GenesisBlock.Serialize(&buf)
if err != nil {
t.Fatalf("TestGenesisBlock: %v", err)
}
// Ensure the encoded block matches the expected bytes.
if !bytes.Equal(buf.Bytes(), genesisBlockBytes) {
t.Fatalf("TestGenesisBlock: Genesis block does not appear valid - "+
"got %v, want %v", spew.Sdump(buf.Bytes()),
spew.Sdump(genesisBlockBytes))
}
// Check hash of the block against expected hash.
hash := params.GenesisBlock.BlockHash()
if !params.GenesisHash.IsEqual(&hash) {
t.Fatalf("TestGenesisBlock: Genesis block hash does not "+
"appear valid - got %v, want %v", spew.Sdump(hash),
spew.Sdump(params.GenesisHash))
}
}