2018-06-15 21:36:42 +00:00
|
|
|
// Copyright (c) 2018 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 (
|
2018-09-26 21:09:11 +00:00
|
|
|
"bytes"
|
|
|
|
|
"strings"
|
2018-06-15 21:36:42 +00:00
|
|
|
"testing"
|
|
|
|
|
|
2018-09-26 21:09:11 +00:00
|
|
|
"github.com/decred/base58"
|
2018-06-15 21:36:42 +00:00
|
|
|
"github.com/decred/dcrd/blockchain"
|
|
|
|
|
"github.com/decred/dcrd/chaincfg"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 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
|
2018-09-26 21:09:11 +00:00
|
|
|
func checkPowLimitsAreConsistent(t *testing.T, params *chaincfg.Params) {
|
2018-06-15 21:36:42 +00:00
|
|
|
powLimitBigInt := params.PowLimit
|
|
|
|
|
powLimitCompact := params.PowLimitBits
|
|
|
|
|
|
|
|
|
|
toBig := blockchain.CompactToBig(powLimitCompact)
|
|
|
|
|
toCompact := blockchain.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.
|
2018-09-26 21:09:11 +00:00
|
|
|
func checkGenesisBlockRespectsNetworkPowLimit(t *testing.T, params *chaincfg.Params) {
|
2018-06-15 21:36:42 +00:00
|
|
|
genesis := params.GenesisBlock
|
|
|
|
|
bits := genesis.Header.Bits
|
|
|
|
|
|
|
|
|
|
// Header bits as big.Int
|
|
|
|
|
bitsAsBigInt := blockchain.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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-26 21:09:11 +00:00
|
|
|
// 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)
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-15 21:36:42 +00:00
|
|
|
// TestDecredNetworkSettings checks Network-specific settings
|
|
|
|
|
func TestDecredNetworkSettings(t *testing.T) {
|
2018-09-26 21:09:11 +00:00
|
|
|
checkPowLimitsAreConsistent(t, &chaincfg.MainNetParams)
|
|
|
|
|
checkPowLimitsAreConsistent(t, &chaincfg.TestNet3Params)
|
|
|
|
|
checkPowLimitsAreConsistent(t, &chaincfg.SimNetParams)
|
multi: Resurrect regression network.
This resurrects the regression test network that was removed before
initial launch although it really should not have been. The simulation
test network and the regression test network do not serve the same
purpose. Specifically, the regression test network is intended for unit
tests, RPC server tests, and consensus tests. On the other hand, the
simulation test network is intended for private use within a group of
individuals doing simulation testing and full integration tests between
different applications such as wallets, voting service providers, mining
pools, block explorers, and other services that build on Decred.
Keeping the concerns separate will allow the simulation test network to
be modified in ways such as activating consensus changes that have been
successfully voted into mainnet without also needing to vote them in on
the simulation test network while still preserving the ability for the
unit tests to properly test the voting semantics and handling to help
prevent regressions.
In addition to resurrecting the regression test network, this also fully
fleshes out new values for the various addresses prefixes (Rk, Rs, Re,
etc), HD key prefixes (rprv, rpub), and treasury multisig details.
As a part of resurrecting the network, a new CLI flag `--regnet` is
added to allow the RPC test harness connect to a running instance, the
areas of the code which involve votes have been modified to allow the
votes to apply to the new network, and tests have been added to the
relevant modules.
This bumps the affected module versions as follows:
- github.com/decred/dcrd/wire@v1.2.0
- github.com/decred/dcrd/chaincfg@v1.2.0
- github.com/decred/dcrd/dcrutil@v1.2.0
- github.com/decred/dcrd/hdkeychain@v1.1.1
The blockchain module is also affected, but since its version has
already been bumped since the last release tag, it is not bumped again.
Finally, this does not include switching unit tests or the RPC test
harness over the new network since that will be done in a separate
commit.
2018-10-05 04:46:56 +00:00
|
|
|
checkPowLimitsAreConsistent(t, &chaincfg.RegNetParams)
|
2018-09-26 21:09:11 +00:00
|
|
|
|
|
|
|
|
checkGenesisBlockRespectsNetworkPowLimit(t, &chaincfg.MainNetParams)
|
|
|
|
|
checkGenesisBlockRespectsNetworkPowLimit(t, &chaincfg.TestNet3Params)
|
|
|
|
|
checkGenesisBlockRespectsNetworkPowLimit(t, &chaincfg.SimNetParams)
|
multi: Resurrect regression network.
This resurrects the regression test network that was removed before
initial launch although it really should not have been. The simulation
test network and the regression test network do not serve the same
purpose. Specifically, the regression test network is intended for unit
tests, RPC server tests, and consensus tests. On the other hand, the
simulation test network is intended for private use within a group of
individuals doing simulation testing and full integration tests between
different applications such as wallets, voting service providers, mining
pools, block explorers, and other services that build on Decred.
Keeping the concerns separate will allow the simulation test network to
be modified in ways such as activating consensus changes that have been
successfully voted into mainnet without also needing to vote them in on
the simulation test network while still preserving the ability for the
unit tests to properly test the voting semantics and handling to help
prevent regressions.
In addition to resurrecting the regression test network, this also fully
fleshes out new values for the various addresses prefixes (Rk, Rs, Re,
etc), HD key prefixes (rprv, rpub), and treasury multisig details.
As a part of resurrecting the network, a new CLI flag `--regnet` is
added to allow the RPC test harness connect to a running instance, the
areas of the code which involve votes have been modified to allow the
votes to apply to the new network, and tests have been added to the
relevant modules.
This bumps the affected module versions as follows:
- github.com/decred/dcrd/wire@v1.2.0
- github.com/decred/dcrd/chaincfg@v1.2.0
- github.com/decred/dcrd/dcrutil@v1.2.0
- github.com/decred/dcrd/hdkeychain@v1.1.1
The blockchain module is also affected, but since its version has
already been bumped since the last release tag, it is not bumped again.
Finally, this does not include switching unit tests or the RPC test
harness over the new network since that will be done in a separate
commit.
2018-10-05 04:46:56 +00:00
|
|
|
checkGenesisBlockRespectsNetworkPowLimit(t, &chaincfg.RegNetParams)
|
2018-06-15 21:36:42 +00:00
|
|
|
|
2018-09-26 21:09:11 +00:00
|
|
|
checkAddressPrefixesAreConsistent(t, "Pm", &chaincfg.MainNetParams)
|
|
|
|
|
checkAddressPrefixesAreConsistent(t, "Pt", &chaincfg.TestNet3Params)
|
|
|
|
|
checkAddressPrefixesAreConsistent(t, "Ps", &chaincfg.SimNetParams)
|
multi: Resurrect regression network.
This resurrects the regression test network that was removed before
initial launch although it really should not have been. The simulation
test network and the regression test network do not serve the same
purpose. Specifically, the regression test network is intended for unit
tests, RPC server tests, and consensus tests. On the other hand, the
simulation test network is intended for private use within a group of
individuals doing simulation testing and full integration tests between
different applications such as wallets, voting service providers, mining
pools, block explorers, and other services that build on Decred.
Keeping the concerns separate will allow the simulation test network to
be modified in ways such as activating consensus changes that have been
successfully voted into mainnet without also needing to vote them in on
the simulation test network while still preserving the ability for the
unit tests to properly test the voting semantics and handling to help
prevent regressions.
In addition to resurrecting the regression test network, this also fully
fleshes out new values for the various addresses prefixes (Rk, Rs, Re,
etc), HD key prefixes (rprv, rpub), and treasury multisig details.
As a part of resurrecting the network, a new CLI flag `--regnet` is
added to allow the RPC test harness connect to a running instance, the
areas of the code which involve votes have been modified to allow the
votes to apply to the new network, and tests have been added to the
relevant modules.
This bumps the affected module versions as follows:
- github.com/decred/dcrd/wire@v1.2.0
- github.com/decred/dcrd/chaincfg@v1.2.0
- github.com/decred/dcrd/dcrutil@v1.2.0
- github.com/decred/dcrd/hdkeychain@v1.1.1
The blockchain module is also affected, but since its version has
already been bumped since the last release tag, it is not bumped again.
Finally, this does not include switching unit tests or the RPC test
harness over the new network since that will be done in a separate
commit.
2018-10-05 04:46:56 +00:00
|
|
|
checkAddressPrefixesAreConsistent(t, "Pr", &chaincfg.RegNetParams)
|
2018-06-15 21:36:42 +00:00
|
|
|
}
|