dcrd/networkparams_test.go
Dave Collins 25c14e046a
main: Update to use all new major module versions.
This updates all code in the main module to use the latest major modules
versions to pull in the latest updates.

A more general high level overview of the changes is provided below,
however, there is one semantic change worth calling out independently.

The verifymessage RPC will now return an error when provided with
an address that is not for the current active network and the RPC server
version has been bumped accordingly.

Previously, it would return false which indicated the signature is
invalid, even when the provided signature was actually valid for the
other network.  Said behavior was not really incorrect since the
address, signature, and message combination is in fact invalid for the
current active network, however, that result could be somewhat
misleading since a false result could easily be interpreted to mean the
signature is actually invalid altogether which is distinct from the case
of the address being for a different network.  Therefore, it is
preferable to explicitly return an error in the case of an address on
the wrong network to cleanly separate these cases.

The following is a high level overview of the changes:

- Replace all calls to removed blockchain merkle root, pow, subsidy, and
  coinbase funcs with their standalone module equivalents
  - Introduce a new local func named calcTxTreeMerkleRoot that accepts
    dcrutil.Tx as before and defers to the new standalone func
- Update block locator handling to match the new signature required by
  the peer/v2 module
  - Introduce a new local func named chainBlockLocatorToHashes which
    performs the necessary conversion
- Update all references to old v1 chaincfg params global instances to
  use the new v2 functions
- Modify all cases that parse addresses to provide the now required
  current network params
  - Include address params with the wsClientFilter
- Replace removed v1 chaincfg constants with local constants
- Create subsidy cache during server init and pass it to the relevant
  subsystems
  - blockManagerConfig
  - BlkTmplGenerator
  - rpcServer
  - VotingWallet
- Update mining code that creates the block one coinbase transaction to
  create the output scripts as defined in the v2 params
- Replace old v2 dcrjson constant references with new types module
- Fix various comment typos
- Update fees module to use the latest major module versions and bump it v2
2019-08-13 11:22:37 -05:00

148 lines
5.2 KiB
Go

// Copyright (c) 2018-2019 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"strings"
"testing"
"github.com/decred/base58"
"github.com/decred/dcrd/blockchain/standalone"
"github.com/decred/dcrd/chaincfg/v2"
)
// checkPowLimitsAreConsistent ensures PowLimit and PowLimitBits are consistent
// with each other
// PowLimit: mainPowLimit,// big int
// PowLimitBits: 0x1d00ffff, // conceptually the same
// // value, but in an obscure form
func checkPowLimitsAreConsistent(t *testing.T, params *chaincfg.Params) {
powLimitBigInt := params.PowLimit
powLimitCompact := params.PowLimitBits
toBig := standalone.CompactToBig(powLimitCompact)
toCompact := standalone.BigToCompact(powLimitBigInt)
// Check params.PowLimitBits matches params.PowLimit converted
// into the compact form
if toCompact != powLimitCompact {
t.Fatalf("PowLimit values mismatch:\n"+
"params.PowLimit :%064x\n"+
" :%x\n"+
"params.PowLimitBits:%064x\n"+
" :%x\n"+
"params.PowLimit is not consistent with the params.PowLimitBits",
powLimitBigInt, toCompact, toBig, powLimitCompact)
}
}
// checkGenesisBlockRespectsNetworkPowLimit ensures genesis.Header.Bits value
// is within the network PoW limit.
//
// Genesis header bits define starting difficulty of the network.
// Header bits of each block define target difficulty of the subsequent block.
//
// The first few solved blocks of the network will inherit the genesis block
// bits value before the difficulty reajustment takes place.
//
// Solved block shouldn't be rejected due to the PoW limit check.
//
// This test ensures these blocks will respect the network PoW limit.
func checkGenesisBlockRespectsNetworkPowLimit(t *testing.T, params *chaincfg.Params) {
genesis := params.GenesisBlock
bits := genesis.Header.Bits
// Header bits as big.Int
bitsAsBigInt := standalone.CompactToBig(bits)
// network PoW limit
powLimitBigInt := params.PowLimit
if bitsAsBigInt.Cmp(powLimitBigInt) > 0 {
t.Fatalf("Genesis block fails the consensus:\n"+
"genesis.Header.Bits:%x\n"+
" :%064x\n"+
"params.PowLimit :%064x\n"+
"genesis.Header.Bits "+
"should respect network PoW limit",
bits, bitsAsBigInt, powLimitBigInt)
}
}
// checkPrefix checks if targetString starts with the given prefix
func checkPrefix(t *testing.T, prefix string, targetString, networkName string) {
if strings.Index(targetString, prefix) != 0 {
t.Logf("Address prefix mismatch for <%s>: expected <%s> received <%s>",
networkName, prefix, targetString)
t.FailNow()
}
}
// checkInterval creates two corner cases defining interval
// of all key values: [ xxxx000000000...0cccc , xxxx111111111...1cccc ],
// where xxxx - is the encoding magic, and cccc is a checksum.
// The interval is mapped to corresponding interval in base 58.
// Then prefixes are checked for mismatch.
func checkInterval(t *testing.T, desiredPrefix string, keySize int, networkName string, magic [2]byte) {
// min and max possible keys
// all zeroes
minKey := bytes.Repeat([]byte{0x00}, keySize)
// all ones
maxKey := bytes.Repeat([]byte{0xff}, keySize)
base58interval := [2]string{
base58.CheckEncode(minKey, magic),
base58.CheckEncode(maxKey, magic),
}
checkPrefix(t, desiredPrefix, base58interval[0], networkName)
checkPrefix(t, desiredPrefix, base58interval[1], networkName)
}
// checkAddressPrefixesAreConsistent ensures address encoding magics and
// NetworkAddressPrefix are consistent with each other.
// This test will light red when a new network is started with incorrect values.
func checkAddressPrefixesAreConsistent(t *testing.T, privateKeyPrefix string, params *chaincfg.Params) {
P := params.NetworkAddressPrefix
// Desired prefixes
Pk := P + "k"
Ps := P + "s"
Pe := P + "e"
PS := P + "S"
Pc := P + "c"
pk := privateKeyPrefix
checkInterval(t, Pk, 33, params.Name, params.PubKeyAddrID)
checkInterval(t, Ps, 20, params.Name, params.PubKeyHashAddrID)
checkInterval(t, Pe, 20, params.Name, params.PKHEdwardsAddrID)
checkInterval(t, PS, 20, params.Name, params.PKHSchnorrAddrID)
checkInterval(t, Pc, 20, params.Name, params.ScriptHashAddrID)
checkInterval(t, pk, 33, params.Name, params.PrivateKeyID)
}
// TestDecredNetworkSettings checks Network-specific settings
func TestDecredNetworkSettings(t *testing.T) {
mainNetParams := chaincfg.MainNetParams()
testNet3Params := chaincfg.TestNet3Params()
simNetParams := chaincfg.SimNetParams()
regNetParams := chaincfg.RegNetParams()
checkPowLimitsAreConsistent(t, mainNetParams)
checkPowLimitsAreConsistent(t, testNet3Params)
checkPowLimitsAreConsistent(t, simNetParams)
checkPowLimitsAreConsistent(t, regNetParams)
checkGenesisBlockRespectsNetworkPowLimit(t, mainNetParams)
checkGenesisBlockRespectsNetworkPowLimit(t, testNet3Params)
checkGenesisBlockRespectsNetworkPowLimit(t, simNetParams)
checkGenesisBlockRespectsNetworkPowLimit(t, regNetParams)
checkAddressPrefixesAreConsistent(t, "Pm", mainNetParams)
checkAddressPrefixesAreConsistent(t, "Pt", testNet3Params)
checkAddressPrefixesAreConsistent(t, "Ps", simNetParams)
checkAddressPrefixesAreConsistent(t, "Pr", regNetParams)
}