Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

main: add address encoding magic constants test #1458

Merged
merged 3 commits into from
Sep 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dcrd.db
_obj
_test
vendor
.idea

# Architecture specific extensions/prefixes
*.[568vq]
Expand Down
75 changes: 66 additions & 9 deletions networkparams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
package main

import (
"bytes"
"strings"
"testing"

"github.com/decred/base58"
"github.com/decred/dcrd/blockchain"
"github.com/decred/dcrd/chaincfg"
)
Expand All @@ -16,7 +19,7 @@ import (
// PowLimit: mainPowLimit,// big int
// PowLimitBits: 0x1d00ffff, // conceptually the same
// // value, but in an obscure form
func checkPowLimitsAreConsistent(t *testing.T, params chaincfg.Params) {
func checkPowLimitsAreConsistent(t *testing.T, params *chaincfg.Params) {
powLimitBigInt := params.PowLimit
powLimitCompact := params.PowLimitBits

Expand Down Expand Up @@ -48,8 +51,7 @@ func checkPowLimitsAreConsistent(t *testing.T, params chaincfg.Params) {
// 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) {
func checkGenesisBlockRespectsNetworkPowLimit(t *testing.T, params *chaincfg.Params) {
genesis := params.GenesisBlock
bits := genesis.Header.Bits

Expand All @@ -70,13 +72,68 @@ func checkGenesisBlockRespectsNetworkPowLimit(
}
}

// 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) {
checkPowLimitsAreConsistent(t, chaincfg.MainNetParams)
checkPowLimitsAreConsistent(t, chaincfg.TestNet3Params)
checkPowLimitsAreConsistent(t, chaincfg.SimNetParams)
checkPowLimitsAreConsistent(t, &chaincfg.MainNetParams)
checkPowLimitsAreConsistent(t, &chaincfg.TestNet3Params)
checkPowLimitsAreConsistent(t, &chaincfg.SimNetParams)

checkGenesisBlockRespectsNetworkPowLimit(t, &chaincfg.MainNetParams)
checkGenesisBlockRespectsNetworkPowLimit(t, &chaincfg.TestNet3Params)
checkGenesisBlockRespectsNetworkPowLimit(t, &chaincfg.SimNetParams)

checkGenesisBlockRespectsNetworkPowLimit(t, chaincfg.MainNetParams)
checkGenesisBlockRespectsNetworkPowLimit(t, chaincfg.TestNet3Params)
checkGenesisBlockRespectsNetworkPowLimit(t, chaincfg.SimNetParams)
checkAddressPrefixesAreConsistent(t, "Pm", &chaincfg.MainNetParams)
checkAddressPrefixesAreConsistent(t, "Pt", &chaincfg.TestNet3Params)
checkAddressPrefixesAreConsistent(t, "Ps", &chaincfg.SimNetParams)
}