From 8c69c29d69b130520b8d40b1e83e2db5e3e13967 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Fri, 23 Feb 2018 21:55:33 -0600 Subject: [PATCH] blockchain: Consolidate tests into the main package. Putting the test code in the same package makes it easier for forks since they don't have to change the import paths as much and it also gets rid of the need for internal_test.go to bridge. --- blockchain/bench_test.go | 2 +- blockchain/chain_test.go | 25 +- blockchain/common_test.go | 64 +++- blockchain/fullblocks_test.go | 114 +++++++- blockchain/fullblocksstakeversion_test.go | 30 +- blockchain/internal_test.go | 44 --- blockchain/mediantime_test.go | 12 +- blockchain/merkle_test.go | 4 +- blockchain/stakeversion_test.go | 54 ---- blockchain/subsidy_test.go | 13 +- blockchain/thresholdstate_test.go | 74 +++-- blockchain/timesorter_test.go | 6 +- blockchain/validate_test.go | 341 ++++++++++------------ blockchain/votebits_test.go | 63 ---- 14 files changed, 388 insertions(+), 458 deletions(-) delete mode 100644 blockchain/internal_test.go diff --git a/blockchain/bench_test.go b/blockchain/bench_test.go index 21149f3c44..a7f8dc5577 100644 --- a/blockchain/bench_test.go +++ b/blockchain/bench_test.go @@ -3,7 +3,7 @@ // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. -package blockchain_test +package blockchain // TODO Make benchmarking tests for various functions, such as sidechain // evaluation. diff --git a/blockchain/chain_test.go b/blockchain/chain_test.go index b1f1351847..c7a70d0d8d 100644 --- a/blockchain/chain_test.go +++ b/blockchain/chain_test.go @@ -3,7 +3,7 @@ // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. -package blockchain_test +package blockchain import ( "bytes" @@ -14,10 +14,8 @@ import ( "path/filepath" "testing" - "github.com/decred/dcrd/blockchain" "github.com/decred/dcrd/blockchain/chaingen" "github.com/decred/dcrd/chaincfg" - "github.com/decred/dcrd/chaincfg/chainhash" "github.com/decred/dcrd/dcrutil" ) @@ -36,19 +34,6 @@ func cloneParams(params *chaincfg.Params) *chaincfg.Params { return ¶msCopy } -// mustParseHash converts the passed big-endian hex string into a -// chainhash.Hash and will panic if there is an error. It only differs from the -// one available in chainhash in that it will panic so errors in the source code -// be detected. It will only (and must only) be called with hard-coded, and -// therefore known good, hashes. -func mustParseHash(s string) *chainhash.Hash { - hash, err := chainhash.NewHashFromStr(s) - if err != nil { - panic("invalid hash in source file: " + s) - } - return hash -} - // TestBlockchainFunction tests the various blockchain API to ensure proper // functionality. func TestBlockchainFunctions(t *testing.T) { @@ -96,7 +81,7 @@ func TestBlockchainFunctions(t *testing.T) { t.Errorf("NewBlockFromBytes error: %v", err.Error()) } - _, _, err = chain.ProcessBlock(bl, blockchain.BFNone) + _, _, err = chain.ProcessBlock(bl, BFNone) if err != nil { t.Fatalf("ProcessBlock error at height %v: %v", i, err.Error()) } @@ -173,8 +158,7 @@ func TestForceHeadReorg(t *testing.T) { t.Logf("Testing block %s (hash %s, height %d)", g.TipName(), block.Hash(), blockHeight) - isMainChain, isOrphan, err := chain.ProcessBlock(block, - blockchain.BFNone) + isMainChain, isOrphan, err := chain.ProcessBlock(block, BFNone) if err != nil { t.Fatalf("block %q (hash %s, height %d) should "+ "have been accepted: %v", g.TipName(), @@ -213,8 +197,7 @@ func TestForceHeadReorg(t *testing.T) { t.Logf("Testing block %s (hash %s, height %d)", g.TipName(), block.Hash(), blockHeight) - isMainChain, isOrphan, err := chain.ProcessBlock(block, - blockchain.BFNone) + isMainChain, isOrphan, err := chain.ProcessBlock(block, BFNone) if err != nil { t.Fatalf("block %q (hash %s, height %d) should "+ "have been accepted: %v", g.TipName(), diff --git a/blockchain/common_test.go b/blockchain/common_test.go index efe811d575..2fc4b7e38c 100644 --- a/blockchain/common_test.go +++ b/blockchain/common_test.go @@ -3,15 +3,17 @@ // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. -package blockchain_test +package blockchain import ( "fmt" "os" "path/filepath" + "time" - "github.com/decred/dcrd/blockchain" + "github.com/decred/dcrd/blockchain/stake" "github.com/decred/dcrd/chaincfg" + "github.com/decred/dcrd/chaincfg/chainhash" "github.com/decred/dcrd/database" _ "github.com/decred/dcrd/database/ffldb" "github.com/decred/dcrd/txscript" @@ -55,7 +57,7 @@ func isSupportedDbType(dbType string) bool { // chainSetup is used to create a new db and chain instance with the genesis // block already inserted. In addition to the new chain instance, it returns // a teardown function the caller should invoke when done testing to clean up. -func chainSetup(dbName string, params *chaincfg.Params) (*blockchain.BlockChain, func(), error) { +func chainSetup(dbName string, params *chaincfg.Params) (*BlockChain, func(), error) { if !isSupportedDbType(testDbType) { return nil, nil, fmt.Errorf("unsupported db type %v", testDbType) } @@ -109,10 +111,10 @@ func chainSetup(dbName string, params *chaincfg.Params) (*blockchain.BlockChain, paramsCopy := *params // Create the main chain instance. - chain, err := blockchain.New(&blockchain.Config{ + chain, err := New(&Config{ DB: db, ChainParams: ¶msCopy, - TimeSource: blockchain.NewMedianTime(), + TimeSource: NewMedianTime(), SigCache: txscript.NewSigCache(1000), }) @@ -124,3 +126,55 @@ func chainSetup(dbName string, params *chaincfg.Params) (*blockchain.BlockChain, return chain, teardown, nil } + +// newFakeChain returns a chain that is usable for syntetic tests. It is +// important to note that this chain has no database associated with it, so +// it is not usable with all functions and the tests must take care when making +// use of it. +func newFakeChain(params *chaincfg.Params) *BlockChain { + // Create a genesis block node and block index populated with it for use + // when creating the fake chain below. + node := newBlockNode(¶ms.GenesisBlock.Header, nil) + node.inMainChain = true + index := newBlockIndex(nil, params) + index.AddNode(node) + + return &BlockChain{ + chainParams: params, + deploymentCaches: newThresholdCaches(params), + bestNode: node, + index: index, + isVoterMajorityVersionCache: make(map[[stakeMajorityCacheKeySize]byte]bool), + isStakeMajorityVersionCache: make(map[[stakeMajorityCacheKeySize]byte]bool), + calcPriorStakeVersionCache: make(map[[chainhash.HashSize]byte]uint32), + calcVoterVersionIntervalCache: make(map[[chainhash.HashSize]byte]uint32), + calcStakeVersionCache: make(map[[chainhash.HashSize]byte]uint32), + } +} + +// newFakeNode creates a block node connected to the passed parent with the +// provided fields populated and fake values for the other fields. +func newFakeNode(parent *blockNode, blockVersion int32, stakeVersion uint32, bits uint32, timestamp time.Time) *blockNode { + // Make up a header and create a block node from it. + header := &wire.BlockHeader{ + Version: blockVersion, + PrevBlock: parent.hash, + VoteBits: 0x01, + Bits: bits, + Height: uint32(parent.height) + 1, + Timestamp: timestamp, + StakeVersion: stakeVersion, + } + return newBlockNode(header, parent) +} + +// appendFakeVotes appends the passed number of votes to the node with the +// provided version and vote bits. +func appendFakeVotes(node *blockNode, numVotes uint16, voteVersion uint32, voteBits uint16) { + for i := uint16(0); i < numVotes; i++ { + node.votes = append(node.votes, stake.VoteVersionTuple{ + Version: voteVersion, + Bits: voteBits, + }) + } +} diff --git a/blockchain/fullblocks_test.go b/blockchain/fullblocks_test.go index a5e1fa56ae..e3fd5b59d8 100644 --- a/blockchain/fullblocks_test.go +++ b/blockchain/fullblocks_test.go @@ -1,5 +1,5 @@ // Copyright (c) 2016 The btcsuite developers -// Copyright (c) 2016-2017 The Decred developers +// Copyright (c) 2016-2018 The Decred developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. @@ -7,16 +7,128 @@ package blockchain_test import ( "bytes" + "fmt" + "os" + "path/filepath" "testing" "github.com/decred/dcrd/blockchain" "github.com/decred/dcrd/blockchain/fullblocktests" "github.com/decred/dcrd/chaincfg" "github.com/decred/dcrd/chaincfg/chainhash" + "github.com/decred/dcrd/database" "github.com/decred/dcrd/dcrutil" + "github.com/decred/dcrd/txscript" "github.com/decred/dcrd/wire" ) +const ( + // testDbType is the database backend type to use for the tests. + testDbType = "ffldb" + + // testDbRoot is the root directory used to create all test databases. + testDbRoot = "testdbs" + + // blockDataNet is the expected network in the test block data. + blockDataNet = wire.MainNet +) + +// filesExists returns whether or not the named file or directory exists. +func fileExists(name string) bool { + if _, err := os.Stat(name); err != nil { + if os.IsNotExist(err) { + return false + } + } + return true +} + +// isSupportedDbType returns whether or not the passed database type is +// currently supported. +func isSupportedDbType(dbType string) bool { + supportedDrivers := database.SupportedDrivers() + for _, driver := range supportedDrivers { + if dbType == driver { + return true + } + } + + return false +} + +// chainSetup is used to create a new db and chain instance with the genesis +// block already inserted. In addition to the new chain instance, it returns +// a teardown function the caller should invoke when done testing to clean up. +func chainSetup(dbName string, params *chaincfg.Params) (*blockchain.BlockChain, func(), error) { + if !isSupportedDbType(testDbType) { + return nil, nil, fmt.Errorf("unsupported db type %v", testDbType) + } + + // Handle memory database specially since it doesn't need the disk + // specific handling. + var db database.DB + var teardown func() + if testDbType == "memdb" { + ndb, err := database.Create(testDbType) + if err != nil { + return nil, nil, fmt.Errorf("error creating db: %v", err) + } + db = ndb + + // Setup a teardown function for cleaning up. This function is + // returned to the caller to be invoked when it is done testing. + teardown = func() { + db.Close() + } + } else { + // Create the root directory for test databases. + if !fileExists(testDbRoot) { + if err := os.MkdirAll(testDbRoot, 0700); err != nil { + err := fmt.Errorf("unable to create test db "+ + "root: %v", err) + return nil, nil, err + } + } + + // Create a new database to store the accepted blocks into. + dbPath := filepath.Join(testDbRoot, dbName) + _ = os.RemoveAll(dbPath) + ndb, err := database.Create(testDbType, dbPath, blockDataNet) + if err != nil { + return nil, nil, fmt.Errorf("error creating db: %v", err) + } + db = ndb + + // Setup a teardown function for cleaning up. This function is + // returned to the caller to be invoked when it is done testing. + teardown = func() { + db.Close() + os.RemoveAll(dbPath) + os.RemoveAll(testDbRoot) + } + } + + // Copy the chain params to ensure any modifications the tests do to + // the chain parameters do not affect the global instance. + paramsCopy := *params + + // Create the main chain instance. + chain, err := blockchain.New(&blockchain.Config{ + DB: db, + ChainParams: ¶msCopy, + TimeSource: blockchain.NewMedianTime(), + SigCache: txscript.NewSigCache(1000), + }) + + if err != nil { + teardown() + err := fmt.Errorf("failed to create chain instance: %v", err) + return nil, nil, err + } + + return chain, teardown, nil +} + // TestFullBlocks ensures all tests generated by the fullblocktests package // have the expected result when processed via ProcessBlock. func TestFullBlocks(t *testing.T) { diff --git a/blockchain/fullblocksstakeversion_test.go b/blockchain/fullblocksstakeversion_test.go index d48f9035a7..77b6c4308f 100644 --- a/blockchain/fullblocksstakeversion_test.go +++ b/blockchain/fullblocksstakeversion_test.go @@ -1,14 +1,13 @@ -// Copyright (c) 2016-2017 The Decred developers +// Copyright (c) 2016-2018 The Decred developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. -package blockchain_test +package blockchain import ( "fmt" "testing" - "github.com/decred/dcrd/blockchain" "github.com/decred/dcrd/blockchain/chaingen" "github.com/decred/dcrd/chaincfg" "github.com/decred/dcrd/dcrutil" @@ -47,8 +46,7 @@ func TestStakeVersion(t *testing.T) { t.Logf("Testing block %s (hash %s, height %d)", g.TipName(), block.Hash(), blockHeight) - isMainChain, isOrphan, err := chain.ProcessBlock(block, - blockchain.BFNone) + isMainChain, isOrphan, err := chain.ProcessBlock(block, BFNone) if err != nil { t.Fatalf("block %q (hash %s, height %d) should "+ "have been accepted: %v", g.TipName(), @@ -68,14 +66,14 @@ func TestStakeVersion(t *testing.T) { block.Hash(), blockHeight, isOrphan) } } - rejected := func(code blockchain.ErrorCode) { + rejected := func(code ErrorCode) { msgBlock := g.Tip() blockHeight := msgBlock.Header.Height block := dcrutil.NewBlock(msgBlock) t.Logf("Testing block %s (hash %s, height %d)", g.TipName(), block.Hash(), blockHeight) - _, _, err := chain.ProcessBlock(block, blockchain.BFNone) + _, _, err := chain.ProcessBlock(block, BFNone) if err == nil { t.Fatalf("block %q (hash %s, height %d) should not "+ "have been accepted", g.TipName(), block.Hash(), @@ -84,7 +82,7 @@ func TestStakeVersion(t *testing.T) { // Ensure the error code is of the expected type and the reject // code matches the value specified in the test instance. - rerr, ok := err.(blockchain.RuleError) + rerr, ok := err.(RuleError) if !ok { t.Fatalf("block %q (hash %s, height %d) returned "+ "unexpected error type -- got %T, want "+ @@ -270,7 +268,7 @@ func TestStakeVersion(t *testing.T) { g.AssertTipHeight(uint32(stakeValidationHeight + 2*stakeVerInterval)) g.AssertBlockVersion(3) g.AssertStakeVersion(2) - rejected(blockchain.ErrBadStakeVersion) + rejected(ErrBadStakeVersion) // --------------------------------------------------------------------- // Generate a single block with block version 3, stake version 1, and @@ -290,7 +288,7 @@ func TestStakeVersion(t *testing.T) { g.AssertTipHeight(uint32(stakeValidationHeight + 2*stakeVerInterval)) g.AssertBlockVersion(3) g.AssertStakeVersion(1) - rejected(blockchain.ErrBadStakeVersion) + rejected(ErrBadStakeVersion) // --------------------------------------------------------------------- // Generate enough blocks to reach one block before the next stake @@ -331,7 +329,7 @@ func TestStakeVersion(t *testing.T) { g.AssertTipHeight(uint32(stakeValidationHeight + 3*stakeVerInterval)) g.AssertBlockVersion(3) g.AssertStakeVersion(2) - rejected(blockchain.ErrBadStakeVersion) + rejected(ErrBadStakeVersion) // --------------------------------------------------------------------- // Generate a single block with block version 3, stake version 4, and @@ -349,7 +347,7 @@ func TestStakeVersion(t *testing.T) { g.AssertTipHeight(uint32(stakeValidationHeight + 3*stakeVerInterval)) g.AssertBlockVersion(3) g.AssertStakeVersion(4) - rejected(blockchain.ErrBadStakeVersion) + rejected(ErrBadStakeVersion) // --------------------------------------------------------------------- // Generate enough blocks to reach one block before the next stake @@ -391,7 +389,7 @@ func TestStakeVersion(t *testing.T) { g.AssertTipHeight(uint32(stakeValidationHeight + 4*stakeVerInterval)) g.AssertBlockVersion(3) g.AssertStakeVersion(2) - rejected(blockchain.ErrBadStakeVersion) + rejected(ErrBadStakeVersion) // --------------------------------------------------------------------- // Generate a single block with block version 3, stake version 4, and @@ -409,7 +407,7 @@ func TestStakeVersion(t *testing.T) { g.AssertTipHeight(uint32(stakeValidationHeight + 4*stakeVerInterval)) g.AssertBlockVersion(3) g.AssertStakeVersion(4) - rejected(blockchain.ErrBadStakeVersion) + rejected(ErrBadStakeVersion) // --------------------------------------------------------------------- // Generate enough blocks to reach one block before the next stake @@ -466,7 +464,7 @@ func TestStakeVersion(t *testing.T) { g.AssertTipHeight(uint32(stakeValidationHeight + 5*stakeVerInterval)) g.AssertBlockVersion(3) g.AssertStakeVersion(4) - rejected(blockchain.ErrBadStakeVersion) + rejected(ErrBadStakeVersion) // --------------------------------------------------------------------- // Generate enough blocks to reach one block before the next stake @@ -519,7 +517,7 @@ func TestStakeVersion(t *testing.T) { g.AssertTipHeight(uint32(stakeValidationHeight + 6*stakeVerInterval)) g.AssertBlockVersion(3) g.AssertStakeVersion(3) - rejected(blockchain.ErrBadStakeVersion) + rejected(ErrBadStakeVersion) // --------------------------------------------------------------------- // Generate a single block with block version 3, stake version 4, and diff --git a/blockchain/internal_test.go b/blockchain/internal_test.go deleted file mode 100644 index 47e1395cd2..0000000000 --- a/blockchain/internal_test.go +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (c) 2013-2016 The btcsuite developers -// Copyright (c) 2015-2018 The Decred developers -// Use of this source code is governed by an ISC -// license that can be found in the LICENSE file. - -/* -This test file is part of the blockchain package rather than than the -blockchain_test package so it can bridge access to the internals to properly -test cases which are either not possible or can't reliably be tested via the -public interface. The functions are only exported while the tests are being -run. -*/ - -package blockchain - -import ( - "sort" - - "github.com/decred/dcrd/wire" -) - -// TstTimeSorter makes the internal timeSorter type available to the test -// package. -func TstTimeSorter(times []int64) sort.Interface { - return timeSorter(times) -} - -// TstSetMaxMedianTimeEntries makes the ability to set the maximum number of -// median time entries available to the test package. -func TstSetMaxMedianTimeEntries(val int) { - maxMedianTimeEntries = val -} - -// TstCheckBlockHeaderContext makes the internal checkBlockHeaderContext -// function available to the test package. -func (b *BlockChain) TstCheckBlockHeaderContext(header *wire.BlockHeader, prevNode *blockNode, flags BehaviorFlags) error { - return b.checkBlockHeaderContext(header, prevNode, flags) -} - -// TstNewBlockNode makes the internal newBlockNode function available to the -// test package. -func TstNewBlockNode(blockHeader *wire.BlockHeader, parent *blockNode) *blockNode { - return newBlockNode(blockHeader, parent) -} diff --git a/blockchain/mediantime_test.go b/blockchain/mediantime_test.go index c229e06abc..1b8a828041 100644 --- a/blockchain/mediantime_test.go +++ b/blockchain/mediantime_test.go @@ -1,16 +1,14 @@ // Copyright (c) 2013-2014 The btcsuite developers -// Copyright (c) 2015-2016 The Decred developers +// Copyright (c) 2015-2018 The Decred developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. -package blockchain_test +package blockchain import ( "strconv" "testing" "time" - - "github.com/decred/dcrd/blockchain" ) // TestMedianTime tests the medianTime implementation. @@ -56,11 +54,11 @@ func TestMedianTime(t *testing.T) { } // Modify the max number of allowed median time entries for these tests. - blockchain.TstSetMaxMedianTimeEntries(10) - defer blockchain.TstSetMaxMedianTimeEntries(200) + maxMedianTimeEntries = 10 + defer func() { maxMedianTimeEntries = 200 }() for i, test := range tests { - filter := blockchain.NewMedianTime() + filter := NewMedianTime() for j, offset := range test.in { id := strconv.Itoa(j) now := time.Unix(time.Now().Unix(), 0) diff --git a/blockchain/merkle_test.go b/blockchain/merkle_test.go index ddcaee0598..b73488abc5 100644 --- a/blockchain/merkle_test.go +++ b/blockchain/merkle_test.go @@ -1,9 +1,9 @@ // Copyright (c) 2013-2014 The btcsuite developers -// Copyright (c) 2015-2016 The Decred developers +// Copyright (c) 2015-2018 The Decred developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. -package blockchain_test +package blockchain // TODO Make tests for merkle root calculation. Merkle root calculation and // corruption is already well tested in the blockchain error unit tests and diff --git a/blockchain/stakeversion_test.go b/blockchain/stakeversion_test.go index 33bdebca6f..1cce8f1884 100644 --- a/blockchain/stakeversion_test.go +++ b/blockchain/stakeversion_test.go @@ -9,64 +9,10 @@ import ( "testing" "time" - "github.com/decred/dcrd/blockchain/stake" "github.com/decred/dcrd/chaincfg" "github.com/decred/dcrd/chaincfg/chainhash" - "github.com/decred/dcrd/wire" ) -// newFakeChain returns a chain that is usable for syntetic tests. It is -// important to note that this chain has no database associated with it, so -// it is not usable with all functions and the tests must take care when making -// use of it. -func newFakeChain(params *chaincfg.Params) *BlockChain { - // Create a genesis block node and block index populated with it for use - // when creating the fake chain below. - node := newBlockNode(¶ms.GenesisBlock.Header, nil) - node.inMainChain = true - index := newBlockIndex(nil, params) - index.AddNode(node) - - return &BlockChain{ - chainParams: params, - deploymentCaches: newThresholdCaches(params), - bestNode: node, - index: index, - isVoterMajorityVersionCache: make(map[[stakeMajorityCacheKeySize]byte]bool), - isStakeMajorityVersionCache: make(map[[stakeMajorityCacheKeySize]byte]bool), - calcPriorStakeVersionCache: make(map[[chainhash.HashSize]byte]uint32), - calcVoterVersionIntervalCache: make(map[[chainhash.HashSize]byte]uint32), - calcStakeVersionCache: make(map[[chainhash.HashSize]byte]uint32), - } -} - -// newFakeNode creates a block node connected to the passed parent with the -// provided fields populated and fake values for the other fields. -func newFakeNode(parent *blockNode, blockVersion int32, stakeVersion uint32, bits uint32, timestamp time.Time) *blockNode { - // Make up a header and create a block node from it. - header := &wire.BlockHeader{ - Version: blockVersion, - PrevBlock: parent.hash, - VoteBits: 0x01, - Bits: bits, - Height: uint32(parent.height) + 1, - Timestamp: timestamp, - StakeVersion: stakeVersion, - } - return newBlockNode(header, parent) -} - -// appendFakeVotes appends the passed number of votes to the node with the -// provided version and vote bits. -func appendFakeVotes(node *blockNode, numVotes uint16, voteVersion uint32, voteBits uint16) { - for i := uint16(0); i < numVotes; i++ { - node.votes = append(node.votes, stake.VoteVersionTuple{ - Version: voteVersion, - Bits: voteBits, - }) - } -} - func TestCalcWantHeight(t *testing.T) { // For example, if StakeVersionInterval = 11 and StakeValidationHeight = 13 the // windows start at 13 + (11 * 2) 25 and are as follows: 24-34, 35-45, 46-56 ... diff --git a/blockchain/subsidy_test.go b/blockchain/subsidy_test.go index 6a8a3ba8e0..1c142b05d6 100644 --- a/blockchain/subsidy_test.go +++ b/blockchain/subsidy_test.go @@ -1,20 +1,19 @@ // Copyright (c) 2013-2015 The btcsuite developers -// Copyright (c) 2015-2016 The Decred developers +// Copyright (c) 2015-2018 The Decred developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. -package blockchain_test +package blockchain import ( "testing" - "github.com/decred/dcrd/blockchain" "github.com/decred/dcrd/chaincfg" ) func TestBlockSubsidy(t *testing.T) { mainnet := &chaincfg.MainNetParams - subsidyCache := blockchain.NewSubsidyCache(0, mainnet) + subsidyCache := NewSubsidyCache(0, mainnet) totalSubsidy := mainnet.BlockOneSubsidy() for i := int64(0); ; i++ { @@ -32,11 +31,11 @@ func TestBlockSubsidy(t *testing.T) { } height := i - numBlocks - work := blockchain.CalcBlockWorkSubsidy(subsidyCache, height, + work := CalcBlockWorkSubsidy(subsidyCache, height, mainnet.TicketsPerBlock, mainnet) - stake := blockchain.CalcStakeVoteSubsidy(subsidyCache, height, + stake := CalcStakeVoteSubsidy(subsidyCache, height, mainnet) * int64(mainnet.TicketsPerBlock) - tax := blockchain.CalcBlockTaxSubsidy(subsidyCache, height, + tax := CalcBlockTaxSubsidy(subsidyCache, height, mainnet.TicketsPerBlock, mainnet) if (work + stake + tax) == 0 { break diff --git a/blockchain/thresholdstate_test.go b/blockchain/thresholdstate_test.go index 597ba1344f..ea7c234c00 100644 --- a/blockchain/thresholdstate_test.go +++ b/blockchain/thresholdstate_test.go @@ -1,8 +1,8 @@ -// Copyright (c) 2017 The Decred developers +// Copyright (c) 2017-2018 The Decred developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. -package blockchain_test +package blockchain import ( "fmt" @@ -10,7 +10,6 @@ import ( "testing" "time" - "github.com/decred/dcrd/blockchain" "github.com/decred/dcrd/blockchain/chaingen" "github.com/decred/dcrd/chaincfg" "github.com/decred/dcrd/dcrutil" @@ -21,10 +20,6 @@ const ( // previous block being valid. vbPrevBlockValid = 0x01 - // invalidChoice is the value returns by thresholdState when not in a - // state where the choice can be valid. - invalidChoice = 0xffffffff - // testDummy1ID is the human-readable ID for the first test dummy voting // agenda. testDummy1ID = "testdummy1" @@ -167,8 +162,7 @@ func TestThresholdState(t *testing.T) { t.Logf("Testing block %s (hash %s, height %d)", g.TipName(), block.Hash(), blockHeight) - isMainChain, isOrphan, err := chain.ProcessBlock(block, - blockchain.BFNone) + isMainChain, isOrphan, err := chain.ProcessBlock(block, BFNone) if err != nil { t.Fatalf("block %q (hash %s, height %d) should "+ "have been accepted: %v", g.TipName(), @@ -192,7 +186,7 @@ func TestThresholdState(t *testing.T) { // testThresholdState queries the threshold state from the current // tip block associated with the generator and expects the returned // state and choice to match the provided values. - testThresholdState := func(id string, state blockchain.ThresholdState, choice uint32) { + testThresholdState := func(id string, state ThresholdState, choice uint32) { tipHash := g.Tip().BlockHash() s, err := chain.ThresholdState(&tipHash, posVersion, id) if err != nil { @@ -238,8 +232,8 @@ func TestThresholdState(t *testing.T) { g.CreatePremineBlock("bp", 0) g.AssertTipHeight(1) accepted() - testThresholdState(testDummy1ID, blockchain.ThresholdDefined, invalidChoice) - testThresholdState(testDummy2ID, blockchain.ThresholdDefined, invalidChoice) + testThresholdState(testDummy1ID, ThresholdDefined, invalidChoice) + testThresholdState(testDummy2ID, ThresholdDefined, invalidChoice) // --------------------------------------------------------------------- // Generate enough blocks to have mature coinbase outputs to work with. @@ -254,8 +248,8 @@ func TestThresholdState(t *testing.T) { accepted() } g.AssertTipHeight(uint32(coinbaseMaturity) + 1) - testThresholdState(testDummy1ID, blockchain.ThresholdDefined, invalidChoice) - testThresholdState(testDummy2ID, blockchain.ThresholdDefined, invalidChoice) + testThresholdState(testDummy1ID, ThresholdDefined, invalidChoice) + testThresholdState(testDummy2ID, ThresholdDefined, invalidChoice) // --------------------------------------------------------------------- // Generate enough blocks to reach the stake enabled height while @@ -276,8 +270,8 @@ func TestThresholdState(t *testing.T) { accepted() } g.AssertTipHeight(uint32(stakeEnabledHeight)) - testThresholdState(testDummy1ID, blockchain.ThresholdDefined, invalidChoice) - testThresholdState(testDummy2ID, blockchain.ThresholdDefined, invalidChoice) + testThresholdState(testDummy1ID, ThresholdDefined, invalidChoice) + testThresholdState(testDummy2ID, ThresholdDefined, invalidChoice) // --------------------------------------------------------------------- // Generate enough blocks to reach the stake validation height while @@ -311,8 +305,8 @@ func TestThresholdState(t *testing.T) { accepted() } g.AssertTipHeight(uint32(stakeValidationHeight)) - testThresholdState(testDummy1ID, blockchain.ThresholdDefined, invalidChoice) - testThresholdState(testDummy2ID, blockchain.ThresholdDefined, invalidChoice) + testThresholdState(testDummy1ID, ThresholdDefined, invalidChoice) + testThresholdState(testDummy2ID, ThresholdDefined, invalidChoice) // --------------------------------------------------------------------- // Generate enough blocks to reach one block before the next stake @@ -340,8 +334,8 @@ func TestThresholdState(t *testing.T) { g.AssertTipHeight(uint32(stakeValidationHeight + stakeVerInterval - 1)) g.AssertBlockVersion(3) g.AssertStakeVersion(0) - testThresholdState(testDummy1ID, blockchain.ThresholdDefined, invalidChoice) - testThresholdState(testDummy2ID, blockchain.ThresholdDefined, invalidChoice) + testThresholdState(testDummy1ID, ThresholdDefined, invalidChoice) + testThresholdState(testDummy2ID, ThresholdDefined, invalidChoice) // --------------------------------------------------------------------- // Generate enough blocks to reach one block before the next rule change @@ -368,8 +362,8 @@ func TestThresholdState(t *testing.T) { g.AssertTipHeight(uint32(stakeValidationHeight + ruleChangeInterval - 2)) g.AssertBlockVersion(3) g.AssertStakeVersion(3) - testThresholdState(testDummy1ID, blockchain.ThresholdDefined, invalidChoice) - testThresholdState(testDummy2ID, blockchain.ThresholdDefined, invalidChoice) + testThresholdState(testDummy1ID, ThresholdDefined, invalidChoice) + testThresholdState(testDummy2ID, ThresholdDefined, invalidChoice) // --------------------------------------------------------------------- // Generate enough blocks to reach one block before the next stake @@ -399,8 +393,8 @@ func TestThresholdState(t *testing.T) { g.AssertTipHeight(uint32(stakeValidationHeight + stakeVerInterval*4 - 1)) g.AssertBlockVersion(3) g.AssertStakeVersion(3) - testThresholdState(testDummy1ID, blockchain.ThresholdDefined, invalidChoice) - testThresholdState(testDummy2ID, blockchain.ThresholdDefined, invalidChoice) + testThresholdState(testDummy1ID, ThresholdDefined, invalidChoice) + testThresholdState(testDummy2ID, ThresholdDefined, invalidChoice) // --------------------------------------------------------------------- // Generate enough blocks to reach the next rule change interval with @@ -432,8 +426,8 @@ func TestThresholdState(t *testing.T) { g.AssertTipHeight(uint32(stakeValidationHeight + ruleChangeInterval*2 - 1)) g.AssertBlockVersion(4) g.AssertStakeVersion(4) - testThresholdState(testDummy1ID, blockchain.ThresholdDefined, invalidChoice) - testThresholdState(testDummy2ID, blockchain.ThresholdDefined, invalidChoice) + testThresholdState(testDummy1ID, ThresholdDefined, invalidChoice) + testThresholdState(testDummy2ID, ThresholdDefined, invalidChoice) // --------------------------------------------------------------------- // Generate enough blocks to achieve proof-of-work block version lockin @@ -466,8 +460,8 @@ func TestThresholdState(t *testing.T) { 1 + powNumToCheck)) g.AssertBlockVersion(4) g.AssertStakeVersion(4) - testThresholdState(testDummy1ID, blockchain.ThresholdDefined, invalidChoice) - testThresholdState(testDummy2ID, blockchain.ThresholdDefined, invalidChoice) + testThresholdState(testDummy1ID, ThresholdDefined, invalidChoice) + testThresholdState(testDummy2ID, ThresholdDefined, invalidChoice) // --------------------------------------------------------------------- // Generate enough blocks to reach the next rule change interval with @@ -498,8 +492,8 @@ func TestThresholdState(t *testing.T) { g.AssertTipHeight(uint32(stakeValidationHeight + ruleChangeInterval*3 - 1)) g.AssertBlockVersion(4) g.AssertStakeVersion(4) - testThresholdState(testDummy1ID, blockchain.ThresholdStarted, invalidChoice) - testThresholdState(testDummy2ID, blockchain.ThresholdStarted, invalidChoice) + testThresholdState(testDummy1ID, ThresholdStarted, invalidChoice) + testThresholdState(testDummy2ID, ThresholdStarted, invalidChoice) // --------------------------------------------------------------------- // Generate enough blocks to reach the next rule change interval with @@ -528,8 +522,8 @@ func TestThresholdState(t *testing.T) { g.AssertTipHeight(uint32(stakeValidationHeight + ruleChangeInterval*4 - 1)) g.AssertBlockVersion(4) g.AssertStakeVersion(4) - testThresholdState(testDummy1ID, blockchain.ThresholdStarted, invalidChoice) - testThresholdState(testDummy2ID, blockchain.ThresholdStarted, invalidChoice) + testThresholdState(testDummy1ID, ThresholdStarted, invalidChoice) + testThresholdState(testDummy2ID, ThresholdStarted, invalidChoice) // --------------------------------------------------------------------- // Generate enough blocks to reach the next rule change interval with @@ -564,8 +558,8 @@ func TestThresholdState(t *testing.T) { g.AssertTipHeight(uint32(stakeValidationHeight + ruleChangeInterval*5 - 1)) g.AssertBlockVersion(4) g.AssertStakeVersion(4) - testThresholdState(testDummy1ID, blockchain.ThresholdStarted, invalidChoice) - testThresholdState(testDummy2ID, blockchain.ThresholdStarted, invalidChoice) + testThresholdState(testDummy1ID, ThresholdStarted, invalidChoice) + testThresholdState(testDummy2ID, ThresholdStarted, invalidChoice) // --------------------------------------------------------------------- // Generate enough blocks to reach the next rule change interval with @@ -608,8 +602,8 @@ func TestThresholdState(t *testing.T) { g.AssertTipHeight(uint32(stakeValidationHeight + ruleChangeInterval*6 - 1)) g.AssertBlockVersion(4) g.AssertStakeVersion(4) - testThresholdState(testDummy1ID, blockchain.ThresholdStarted, invalidChoice) - testThresholdState(testDummy2ID, blockchain.ThresholdStarted, invalidChoice) + testThresholdState(testDummy1ID, ThresholdStarted, invalidChoice) + testThresholdState(testDummy2ID, ThresholdStarted, invalidChoice) // --------------------------------------------------------------------- // Generate enough blocks to reach the next rule change interval with @@ -639,8 +633,8 @@ func TestThresholdState(t *testing.T) { g.AssertTipHeight(uint32(stakeValidationHeight + ruleChangeInterval*7 - 1)) g.AssertBlockVersion(4) g.AssertStakeVersion(4) - testThresholdState(testDummy1ID, blockchain.ThresholdLockedIn, testDummy1YesIndex) - testThresholdState(testDummy2ID, blockchain.ThresholdFailed, testDummy2NoIndex) + testThresholdState(testDummy1ID, ThresholdLockedIn, testDummy1YesIndex) + testThresholdState(testDummy2ID, ThresholdFailed, testDummy2NoIndex) // --------------------------------------------------------------------- // Generate enough blocks to reach the next rule change interval with @@ -672,6 +666,6 @@ func TestThresholdState(t *testing.T) { g.AssertTipHeight(uint32(stakeValidationHeight + ruleChangeInterval*8 - 1)) g.AssertBlockVersion(4) g.AssertStakeVersion(4) - testThresholdState(testDummy1ID, blockchain.ThresholdActive, testDummy1YesIndex) - testThresholdState(testDummy2ID, blockchain.ThresholdFailed, testDummy2NoIndex) + testThresholdState(testDummy1ID, ThresholdActive, testDummy1YesIndex) + testThresholdState(testDummy2ID, ThresholdFailed, testDummy2NoIndex) } diff --git a/blockchain/timesorter_test.go b/blockchain/timesorter_test.go index c8fbd3fa05..afc85fb575 100644 --- a/blockchain/timesorter_test.go +++ b/blockchain/timesorter_test.go @@ -3,14 +3,12 @@ // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. -package blockchain_test +package blockchain import ( "reflect" "sort" "testing" - - "github.com/decred/dcrd/blockchain" ) // TestTimeSorter tests the timeSorter implementation. @@ -40,7 +38,7 @@ func TestTimeSorter(t *testing.T) { for i, test := range tests { result := make([]int64, len(test.in)) copy(result, test.in) - sort.Sort(blockchain.TstTimeSorter(result)) + sort.Sort(timeSorter(result)) if !reflect.DeepEqual(result, test.want) { t.Errorf("timeSorter #%d got %v want %v", i, result, test.want) diff --git a/blockchain/validate_test.go b/blockchain/validate_test.go index 6ca15fc58b..886a22bfdf 100644 --- a/blockchain/validate_test.go +++ b/blockchain/validate_test.go @@ -3,7 +3,7 @@ // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. -package blockchain_test +package blockchain import ( "bytes" @@ -16,7 +16,6 @@ import ( "testing" "time" - "github.com/decred/dcrd/blockchain" "github.com/decred/dcrd/blockchain/stake" "github.com/decred/dcrd/chaincfg" "github.com/decred/dcrd/chaincfg/chainhash" @@ -31,8 +30,8 @@ import ( func recalculateMsgBlockMerkleRootsSize(msgBlock *wire.MsgBlock) { tempBlock := dcrutil.NewBlock(msgBlock) - merkles := blockchain.BuildMerkleTreeStore(tempBlock.Transactions()) - merklesStake := blockchain.BuildMerkleTreeStore(tempBlock.STransactions()) + merkles := BuildMerkleTreeStore(tempBlock.Transactions()) + merklesStake := BuildMerkleTreeStore(tempBlock.STransactions()) msgBlock.Header.MerkleRoot = *merkles[len(merkles)-1] msgBlock.Header.StakeRoot = *merklesStake[len(merklesStake)-1] @@ -115,7 +114,7 @@ func TestBlockValidationRules(t *testing.T) { // Insert blocks 1 to 142 and perform various test. Block 1 has // special properties, so make sure those validate correctly first. block1Bytes := blockChain[int64(1)] - timeSource := blockchain.NewMedianTime() + timeSource := NewMedianTime() // ---------------------------------------------------------------------------- // ErrBlockOneOutputs 1 @@ -133,14 +132,13 @@ func TestBlockValidationRules(t *testing.T) { recalculateMsgBlockMerkleRootsSize(noCoinbaseOuts1) b1test := dcrutil.NewBlock(noCoinbaseOuts1) - err = blockchain.CheckWorklessBlockSanity(b1test, timeSource, params) + err = CheckWorklessBlockSanity(b1test, timeSource, params) if err != nil { t.Errorf("Got unexpected error for ErrBlockOneOutputs test 2: %v", err) } - err = chain.CheckConnectBlock(b1test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrBlockOneOutputs { + err = chain.CheckConnectBlock(b1test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrBlockOneOutputs { t.Errorf("Got no error or unexpected error for ErrBlockOneOutputs "+ "test 2: %v", err) } @@ -155,14 +153,13 @@ func TestBlockValidationRules(t *testing.T) { recalculateMsgBlockMerkleRootsSize(noCoinbaseOuts1) b1test = dcrutil.NewBlock(noCoinbaseOuts1) - err = blockchain.CheckWorklessBlockSanity(b1test, timeSource, params) + err = CheckWorklessBlockSanity(b1test, timeSource, params) if err != nil { t.Errorf("Got unexpected error for ErrBlockOneOutputs test 3: %v", err) } - err = chain.CheckConnectBlock(b1test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrBlockOneOutputs { + err = chain.CheckConnectBlock(b1test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrBlockOneOutputs { t.Errorf("Got no error or unexpected error for ErrBlockOneOutputs "+ "test 3: %v", err) } @@ -177,14 +174,13 @@ func TestBlockValidationRules(t *testing.T) { recalculateMsgBlockMerkleRootsSize(noCoinbaseOuts1) b1test = dcrutil.NewBlock(noCoinbaseOuts1) - err = blockchain.CheckWorklessBlockSanity(b1test, timeSource, params) + err = CheckWorklessBlockSanity(b1test, timeSource, params) if err != nil { t.Errorf("Got unexpected error for ErrBlockOneOutputs test 4: %v", err) } - err = chain.CheckConnectBlock(b1test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrBlockOneOutputs { + err = chain.CheckConnectBlock(b1test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrBlockOneOutputs { t.Errorf("Got no error or unexpected error for ErrBlockOneOutputs "+ "test 4: %v", err) } @@ -198,7 +194,7 @@ func TestBlockValidationRules(t *testing.T) { t.Errorf("NewBlockFromBytes error: %v", err.Error()) } - _, _, err = chain.ProcessBlock(bl, blockchain.BFNoPoWCheck) + _, _, err = chain.ProcessBlock(bl, BFNoPoWCheck) if err != nil { t.Fatalf("ProcessBlock error at height %v: %v", i, err.Error()) } @@ -229,9 +225,8 @@ func TestBlockValidationRules(t *testing.T) { recalculateMsgBlockMerkleRootsSize(earlySSGen142) b142test := dcrutil.NewBlock(earlySSGen142) - err = blockchain.CheckWorklessBlockSanity(b142test, timeSource, params) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrInvalidEarlyStakeTx { + err = CheckWorklessBlockSanity(b142test, timeSource, params) + if err == nil || err.(RuleError).ErrorCode != ErrInvalidEarlyStakeTx { t.Errorf("Got unexpected no error or wrong error for "+ "ErrInvalidEarlyStakeTx test: %v", err) } @@ -247,7 +242,7 @@ func TestBlockValidationRules(t *testing.T) { t.Errorf("NewBlockFromBytes error: %v", err.Error()) } - _, _, err = chain.ProcessBlock(bl, blockchain.BFNoPoWCheck) + _, _, err = chain.ProcessBlock(bl, BFNoPoWCheck) if err != nil { t.Errorf("ProcessBlock error at height %v: %v", i, err.Error()) @@ -259,7 +254,7 @@ func TestBlockValidationRules(t *testing.T) { if err != nil { t.Errorf("NewBlockFromBytes error: %v", err.Error()) } - err = chain.CheckConnectBlock(block153, blockchain.BFNoPoWCheck) + err = chain.CheckConnectBlock(block153, BFNoPoWCheck) if err != nil { t.Errorf("CheckConnectBlock error: %v", err.Error()) } @@ -274,15 +269,14 @@ func TestBlockValidationRules(t *testing.T) { recalculateMsgBlockMerkleRootsSize(missingParent153) b153test := dcrutil.NewBlock(missingParent153) - err = blockchain.CheckWorklessBlockSanity(b153test, timeSource, params) + err = CheckWorklessBlockSanity(b153test, timeSource, params) if err != nil { t.Errorf("Got unexpected sanity error for ErrMissingParent test: %v", err) } - err = chain.CheckConnectBlock(b153test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrMissingParent { + err = chain.CheckConnectBlock(b153test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrMissingParent { t.Errorf("Got no or unexpected error for ErrMissingParent test %v", err) } @@ -294,16 +288,14 @@ func TestBlockValidationRules(t *testing.T) { recalculateMsgBlockMerkleRootsSize(badCBFraudProof153) b153test = dcrutil.NewBlock(badCBFraudProof153) - err = blockchain.CheckWorklessBlockSanity(b153test, timeSource, params) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrBadCoinbaseFraudProof { + err = CheckWorklessBlockSanity(b153test, timeSource, params) + if err == nil || err.(RuleError).ErrorCode != ErrBadCoinbaseFraudProof { t.Errorf("Got no or unexpected sanity error for "+ "ErrBadCoinbaseFraudProof test: %v", err) } - err = chain.CheckConnectBlock(b153test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrBadCoinbaseFraudProof { + err = chain.CheckConnectBlock(b153test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrBadCoinbaseFraudProof { t.Errorf("Got no or unexpected sanity error for "+ "ErrBadCoinbaseFraudProof test: %v", err) } @@ -320,16 +312,15 @@ func TestBlockValidationRules(t *testing.T) { recalculateMsgBlockMerkleRootsSize(ssrtxPayeesMismatch153) b153test = dcrutil.NewBlock(ssrtxPayeesMismatch153) - err = blockchain.CheckWorklessBlockSanity(b153test, timeSource, params) + err = CheckWorklessBlockSanity(b153test, timeSource, params) if err != nil { t.Errorf("got unexpected error for ErrSSRtxPayeesMismatch sanity "+ "check: %v", err) } // Fails and hits ErrSSRtxPayeesMismatch. - err = chain.CheckConnectBlock(b153test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrSSRtxPayeesMismatch { + err = chain.CheckConnectBlock(b153test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrSSRtxPayeesMismatch { t.Errorf("Unexpected no or wrong error for ErrSSRtxPayeesMismatch "+ "test: %v", err) } @@ -344,16 +335,15 @@ func TestBlockValidationRules(t *testing.T) { recalculateMsgBlockMerkleRootsSize(badSSRtxPayee153) b153test = dcrutil.NewBlock(badSSRtxPayee153) - err = blockchain.CheckWorklessBlockSanity(b153test, timeSource, params) + err = CheckWorklessBlockSanity(b153test, timeSource, params) if err != nil { t.Errorf("got unexpected error for ErrSSRtxPayees sanity "+ "check 1: %v", err) } // Fails and hits ErrSSRtxPayees. - err = chain.CheckConnectBlock(b153test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrSSRtxPayees { + err = chain.CheckConnectBlock(b153test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrSSRtxPayees { t.Errorf("Unexpected no or wrong error for ErrSSRtxPayees "+ "test 1: %v", err) } @@ -370,16 +360,15 @@ func TestBlockValidationRules(t *testing.T) { recalculateMsgBlockMerkleRootsSize(badSSRtxPayee153) b153test = dcrutil.NewBlock(badSSRtxPayee153) - err = blockchain.CheckWorklessBlockSanity(b153test, timeSource, params) + err = CheckWorklessBlockSanity(b153test, timeSource, params) if err != nil { t.Errorf("got unexpected error for ErrSSRtxPayees sanity "+ "check 2: %v", err) } // Fails and hits ErrSSRtxPayees. - err = chain.CheckConnectBlock(b153test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrSSRtxPayees { + err = chain.CheckConnectBlock(b153test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrSSRtxPayees { t.Errorf("Unexpected no or wrong error for ErrSSRtxPayees "+ "test 2: %v", err) } @@ -404,16 +393,15 @@ func TestBlockValidationRules(t *testing.T) { recalculateMsgBlockMerkleRootsSize(badSSRtx153) b153test = dcrutil.NewBlock(badSSRtx153) - err = blockchain.CheckWorklessBlockSanity(b153test, timeSource, params) + err = CheckWorklessBlockSanity(b153test, timeSource, params) if err != nil { t.Errorf("got unexpected error for ErrInvalidSSRtx sanity check: %v", err) } // Fails and hits ErrInvalidSSRtx. - err = chain.CheckConnectBlock(b153test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrInvalidSSRtx { + err = chain.CheckConnectBlock(b153test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrInvalidSSRtx { t.Errorf("Unexpected no or wrong error for ErrInvalidSSRtx test: %v", err) } @@ -423,7 +411,7 @@ func TestBlockValidationRules(t *testing.T) { block153MsgBlock := new(wire.MsgBlock) block153MsgBlock.FromBytes(block153Bytes) b153test = dcrutil.NewBlock(block153MsgBlock) - _, _, err = chain.ProcessBlock(b153test, blockchain.BFNoPoWCheck) + _, _, err = chain.ProcessBlock(b153test, BFNoPoWCheck) if err != nil { t.Errorf("Got unexpected error processing block 153 %v", err) } @@ -433,12 +421,12 @@ func TestBlockValidationRules(t *testing.T) { b154test := dcrutil.NewBlock(block154MsgBlock) // The incoming block should pass fine. - err = blockchain.CheckWorklessBlockSanity(b154test, timeSource, params) + err = CheckWorklessBlockSanity(b154test, timeSource, params) if err != nil { t.Errorf("Unexpected error for check block 154 sanity: %v", err.Error()) } - err = chain.CheckConnectBlock(b154test, blockchain.BFNoPoWCheck) + err = chain.CheckConnectBlock(b154test, BFNoPoWCheck) if err != nil { t.Errorf("Unexpected error for check block 154 connect: %v", err.Error()) } @@ -471,7 +459,7 @@ func TestBlockValidationRules(t *testing.T) { recalculateMsgBlockMerkleRootsSize(nonChosenTicket154) b154test = dcrutil.NewBlock(nonChosenTicket154) - err = blockchain.CheckWorklessBlockSanity(b154test, timeSource, params) + err = CheckWorklessBlockSanity(b154test, timeSource, params) if err != nil { t.Errorf("got unexpected error for ErrTicketUnavailable sanity check"+ ": %v", @@ -479,9 +467,8 @@ func TestBlockValidationRules(t *testing.T) { } // Fails and hits ErrTooManyVotes. - err = chain.CheckConnectBlock(b154test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrTicketUnavailable { + err = chain.CheckConnectBlock(b154test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrTicketUnavailable { t.Errorf("Unexpected no or wrong error for ErrTicketUnavailable test: %v", err) } @@ -498,9 +485,8 @@ func TestBlockValidationRules(t *testing.T) { b154test = dcrutil.NewBlock(votesMismatch154) // Fails and hits ErrVotesMismatch. - err = blockchain.CheckWorklessBlockSanity(b154test, timeSource, params) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrVotesMismatch { + err = CheckWorklessBlockSanity(b154test, timeSource, params) + if err == nil || err.(RuleError).ErrorCode != ErrVotesMismatch { t.Errorf("got unexpected no or wrong error for ErrVotesMismatch "+ "sanity check: %v", err) } @@ -525,17 +511,15 @@ func TestBlockValidationRules(t *testing.T) { recalculateMsgBlockMerkleRootsSize(badVoteBit154) b154test = dcrutil.NewBlock(badVoteBit154) - err = blockchain.CheckWorklessBlockSanity(b154test, timeSource, params) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrIncongruentVotebit { + err = CheckWorklessBlockSanity(b154test, timeSource, params) + if err == nil || err.(RuleError).ErrorCode != ErrIncongruentVotebit { t.Errorf("Unexpected no or wrong error for ErrIncongruentVotebit "+ "test 5: %v", err) } // Fails and hits ErrIncongruentVotebit. - err = chain.CheckConnectBlock(b154test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrIncongruentVotebit { + err = chain.CheckConnectBlock(b154test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrIncongruentVotebit { t.Errorf("Unexpected no or wrong error for ErrIncongruentVotebit "+ "test 5: %v", err) } @@ -560,17 +544,15 @@ func TestBlockValidationRules(t *testing.T) { recalculateMsgBlockMerkleRootsSize(badVoteBit154) b154test = dcrutil.NewBlock(badVoteBit154) - err = blockchain.CheckWorklessBlockSanity(b154test, timeSource, params) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrIncongruentVotebit { + err = CheckWorklessBlockSanity(b154test, timeSource, params) + if err == nil || err.(RuleError).ErrorCode != ErrIncongruentVotebit { t.Errorf("Unexpected no or wrong error for ErrIncongruentVotebit "+ "test 6: %v", err) } // Fails and hits ErrIncongruentVotebit. - err = chain.CheckConnectBlock(b154test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrIncongruentVotebit { + err = chain.CheckConnectBlock(b154test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrIncongruentVotebit { t.Errorf("Unexpected no or wrong error for ErrIncongruentVotebit "+ "test 6: %v", err) } @@ -595,17 +577,15 @@ func TestBlockValidationRules(t *testing.T) { recalculateMsgBlockMerkleRootsSize(badVoteBit154) b154test = dcrutil.NewBlock(badVoteBit154) - err = blockchain.CheckWorklessBlockSanity(b154test, timeSource, params) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrIncongruentVotebit { + err = CheckWorklessBlockSanity(b154test, timeSource, params) + if err == nil || err.(RuleError).ErrorCode != ErrIncongruentVotebit { t.Errorf("Unexpected no or wrong error for ErrIncongruentVotebit "+ "test 7: %v", err) } // Fails and hits ErrIncongruentVotebit. - err = chain.CheckConnectBlock(b154test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrIncongruentVotebit { + err = chain.CheckConnectBlock(b154test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrIncongruentVotebit { t.Errorf("Unexpected no or wrong error for ErrIncongruentVotebit "+ "test 7: %v", err) } @@ -622,16 +602,15 @@ func TestBlockValidationRules(t *testing.T) { recalculateMsgBlockMerkleRootsSize(badSStxCommit154) b154test = dcrutil.NewBlock(badSStxCommit154) - err = blockchain.CheckWorklessBlockSanity(b154test, timeSource, params) + err = CheckWorklessBlockSanity(b154test, timeSource, params) if err != nil { t.Errorf("got unexpected error for ErrSStxCommitment sanity check: %v", err) } // Fails and hits ErrSStxCommitment. - err = chain.CheckConnectBlock(b154test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrSStxCommitment { + err = chain.CheckConnectBlock(b154test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrSStxCommitment { t.Errorf("Unexpected no or wrong error for ErrSStxCommitment test: %v", err) } @@ -651,16 +630,15 @@ func TestBlockValidationRules(t *testing.T) { recalculateMsgBlockMerkleRootsSize(badSSGenPayee154) b154test = dcrutil.NewBlock(badSSGenPayee154) - err = blockchain.CheckWorklessBlockSanity(b154test, timeSource, params) + err = CheckWorklessBlockSanity(b154test, timeSource, params) if err != nil { t.Errorf("got unexpected error for ErrSSGenPayeeOuts sanity "+ "check: %v", err) } // Fails and hits ErrSSGenPayeeOuts. - err = chain.CheckConnectBlock(b154test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrSSGenPayeeOuts { + err = chain.CheckConnectBlock(b154test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrSSGenPayeeOuts { t.Errorf("Unexpected no or wrong error for ErrSSGenPayeeOuts "+ "test: %v", err) } @@ -675,16 +653,15 @@ func TestBlockValidationRules(t *testing.T) { recalculateMsgBlockMerkleRootsSize(badSSGenPayee154) b154test = dcrutil.NewBlock(badSSGenPayee154) - err = blockchain.CheckWorklessBlockSanity(b154test, timeSource, params) + err = CheckWorklessBlockSanity(b154test, timeSource, params) if err != nil { t.Errorf("got unexpected error for ErrSSGenPayeeOuts sanity "+ "check2 : %v", err) } // Fails and hits ErrSSGenPayeeOuts. - err = chain.CheckConnectBlock(b154test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrSSGenPayeeOuts { + err = chain.CheckConnectBlock(b154test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrSSGenPayeeOuts { t.Errorf("Unexpected no or wrong error for ErrSSGenPayeeOuts "+ "test 2: %v", err) } @@ -736,16 +713,15 @@ func TestBlockValidationRules(t *testing.T) { recalculateMsgBlockMerkleRootsSize(spendTaggedIn154) b154test = dcrutil.NewBlock(spendTaggedIn154) - err = blockchain.CheckWorklessBlockSanity(b154test, timeSource, params) + err = CheckWorklessBlockSanity(b154test, timeSource, params) if err != nil { t.Errorf("got unexpected error for ErrTxSStxOutSpend sanity check: %v", err) } // Fails and hits ErrTxSStxOutSpend. - err = chain.CheckConnectBlock(b154test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrTxSStxOutSpend { + err = chain.CheckConnectBlock(b154test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrTxSStxOutSpend { t.Errorf("Unexpected no or wrong error for ErrTxSStxOutSpend test: %v", err) } @@ -764,16 +740,15 @@ func TestBlockValidationRules(t *testing.T) { recalculateMsgBlockMerkleRootsSize(spendTaggedOut154) b154test = dcrutil.NewBlock(spendTaggedOut154) - err = blockchain.CheckWorklessBlockSanity(b154test, timeSource, params) + err = CheckWorklessBlockSanity(b154test, timeSource, params) if err != nil { t.Errorf("got unexpected error for ErrRegTxSpendStakeOut sanity check: %v", err) } // Fails and hits ErrRegTxSpendStakeOut. - err = chain.CheckConnectBlock(b154test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrRegTxSpendStakeOut { + err = chain.CheckConnectBlock(b154test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrRegTxSpendStakeOut { t.Errorf("Unexpected no or wrong error for ErrRegTxSpendStakeOut test: %v", err) } @@ -785,16 +760,15 @@ func TestBlockValidationRules(t *testing.T) { badFinalState154.Header.FinalState[0] ^= 0x01 b154test = dcrutil.NewBlock(badFinalState154) - err = blockchain.CheckWorklessBlockSanity(b154test, timeSource, params) + err = CheckWorklessBlockSanity(b154test, timeSource, params) if err != nil { t.Errorf("got unexpected error for ErrInvalidFinalState sanity check: %v", err) } // Fails and hits ErrInvalidFinalState. - err = chain.CheckConnectBlock(b154test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrInvalidFinalState { + err = chain.CheckConnectBlock(b154test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrInvalidFinalState { t.Errorf("Unexpected no or wrong error for ErrInvalidFinalState test: %v", err) } @@ -806,16 +780,15 @@ func TestBlockValidationRules(t *testing.T) { badPoolSize154.Header.PoolSize++ b154test = dcrutil.NewBlock(badPoolSize154) - err = blockchain.CheckWorklessBlockSanity(b154test, timeSource, params) + err = CheckWorklessBlockSanity(b154test, timeSource, params) if err != nil { t.Errorf("got unexpected error for ErrPoolSize sanity check: %v", err) } // Fails and hits ErrPoolSize. - err = chain.CheckConnectBlock(b154test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrPoolSize { + err = chain.CheckConnectBlock(b154test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrPoolSize { t.Errorf("Unexpected no or wrong error for ErrPoolSize test: %v", err) } @@ -837,16 +810,15 @@ func TestBlockValidationRules(t *testing.T) { recalculateMsgBlockMerkleRootsSize(errTxTreeIn154) b154test = dcrutil.NewBlock(errTxTreeIn154) - err = blockchain.CheckWorklessBlockSanity(b154test, timeSource, params) + err = CheckWorklessBlockSanity(b154test, timeSource, params) if err != nil { t.Errorf("got unexpected error for ErrDiscordantTxTree sanity check: %v", err) } // Fails and hits ErrDiscordantTxTree. - err = chain.CheckConnectBlock(b154test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrDiscordantTxTree { + err = chain.CheckConnectBlock(b154test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrDiscordantTxTree { t.Errorf("Unexpected no or wrong error for ErrDiscordantTxTree test: %v", err) } @@ -867,9 +839,8 @@ func TestBlockValidationRules(t *testing.T) { b154test = dcrutil.NewBlock(badBlockHeight154) // Throws ProcessBlock error through checkBlockContext. - _, _, err = chain.ProcessBlock(b154test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrBadBlockHeight { + _, _, err = chain.ProcessBlock(b154test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrBadBlockHeight { t.Errorf("ProcessBlock ErrBadBlockHeight test no or unexpected "+ "error: %v", err) } @@ -884,15 +855,14 @@ func TestBlockValidationRules(t *testing.T) { recalculateMsgBlockMerkleRootsSize(taxMissing154) b154test = dcrutil.NewBlock(taxMissing154) - err = blockchain.CheckWorklessBlockSanity(b154test, timeSource, params) + err = CheckWorklessBlockSanity(b154test, timeSource, params) if err != nil { t.Errorf("Got unexpected error for ErrNoTax "+ "test 1: %v", err) } - err = chain.CheckConnectBlock(b154test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrNoTax { + err = chain.CheckConnectBlock(b154test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrNoTax { t.Errorf("Got no error or unexpected error for ErrNoTax "+ "test 1: %v", err) } @@ -906,14 +876,13 @@ func TestBlockValidationRules(t *testing.T) { recalculateMsgBlockMerkleRootsSize(taxMissing154) b154test = dcrutil.NewBlock(taxMissing154) - err = blockchain.CheckWorklessBlockSanity(b154test, timeSource, params) + err = CheckWorklessBlockSanity(b154test, timeSource, params) if err != nil { t.Errorf("Got unexpected error for ErrNoTax test 3: %v", err) } - err = chain.CheckConnectBlock(b154test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrNoTax { + err = chain.CheckConnectBlock(b154test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrNoTax { t.Errorf("Got no error or unexpected error for ErrNoTax "+ "test 3: %v", err) } @@ -930,16 +899,15 @@ func TestBlockValidationRules(t *testing.T) { recalculateMsgBlockMerkleRootsSize(expiredTx154) b154test = dcrutil.NewBlock(expiredTx154) - err = blockchain.CheckWorklessBlockSanity(b154test, timeSource, params) + err = CheckWorklessBlockSanity(b154test, timeSource, params) if err != nil { t.Errorf("got unexpected error for ErrExpiredTx sanity check: %v", err) } // Fails and hits ErrExpiredTx. - err = chain.CheckConnectBlock(b154test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrExpiredTx { + err = chain.CheckConnectBlock(b154test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrExpiredTx { t.Errorf("Unexpected no or wrong error for ErrExpiredTx test: %v", err) } @@ -956,16 +924,15 @@ func TestBlockValidationRules(t *testing.T) { recalculateMsgBlockMerkleRootsSize(badValueIn154) b154test = dcrutil.NewBlock(badValueIn154) - err = blockchain.CheckWorklessBlockSanity(b154test, timeSource, params) + err = CheckWorklessBlockSanity(b154test, timeSource, params) if err != nil { t.Errorf("got unexpected error for ErrFraudAmountIn sanity check: %v", err) } // Fails and hits ErrFraudAmountIn. - err = chain.CheckConnectBlock(b154test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrFraudAmountIn { + err = chain.CheckConnectBlock(b154test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrFraudAmountIn { t.Errorf("Unexpected no or wrong error for ErrFraudAmountIn test: %v", err) } @@ -982,16 +949,15 @@ func TestBlockValidationRules(t *testing.T) { recalculateMsgBlockMerkleRootsSize(badHeightProof154) b154test = dcrutil.NewBlock(badHeightProof154) - err = blockchain.CheckWorklessBlockSanity(b154test, timeSource, params) + err = CheckWorklessBlockSanity(b154test, timeSource, params) if err != nil { t.Errorf("got unexpected error for ErrFraudBlockHeight sanity check: %v", err) } // Fails and hits ErrFraudBlockHeight. - err = chain.CheckConnectBlock(b154test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrFraudBlockHeight { + err = chain.CheckConnectBlock(b154test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrFraudBlockHeight { t.Errorf("Unexpected no or wrong error for ErrFraudBlockHeight test: %v", err) } @@ -1008,16 +974,15 @@ func TestBlockValidationRules(t *testing.T) { recalculateMsgBlockMerkleRootsSize(badIndexProof154) b154test = dcrutil.NewBlock(badIndexProof154) - err = blockchain.CheckWorklessBlockSanity(b154test, timeSource, params) + err = CheckWorklessBlockSanity(b154test, timeSource, params) if err != nil { t.Errorf("got unexpected error for ErrFraudBlockIndex sanity check: %v", err) } // Fails and hits ErrFraudBlockIndex. - err = chain.CheckConnectBlock(b154test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrFraudBlockIndex { + err = chain.CheckConnectBlock(b154test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrFraudBlockIndex { t.Errorf("Unexpected no or wrong error for ErrFraudBlockIndex test: %v", err) } @@ -1034,16 +999,15 @@ func TestBlockValidationRules(t *testing.T) { recalculateMsgBlockMerkleRootsSize(badScrVal154) b154test = dcrutil.NewBlock(badScrVal154) - err = blockchain.CheckWorklessBlockSanity(b154test, timeSource, params) + err = CheckWorklessBlockSanity(b154test, timeSource, params) if err != nil { t.Errorf("got unexpected error for ErrScriptValidation sanity check: %v", err) } // Fails and hits ErrScriptValidation. - err = chain.CheckConnectBlock(b154test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrScriptValidation { + err = chain.CheckConnectBlock(b154test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrScriptValidation { t.Errorf("Unexpected no or wrong error for ErrScriptValidation test: %v", err) } @@ -1056,16 +1020,15 @@ func TestBlockValidationRules(t *testing.T) { recalculateMsgBlockMerkleRootsSize(badScrValS154) b154test = dcrutil.NewBlock(badScrValS154) - err = blockchain.CheckWorklessBlockSanity(b154test, timeSource, params) + err = CheckWorklessBlockSanity(b154test, timeSource, params) if err != nil { t.Errorf("got unexpected error for ErrScriptValidation sanity check: %v", err) } // Fails and hits ErrScriptValidation. - err = chain.CheckConnectBlock(b154test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrScriptValidation { + err = chain.CheckConnectBlock(b154test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrScriptValidation { t.Errorf("Unexpected no or wrong error for ErrScriptValidation test: %v", err) } @@ -1085,16 +1048,15 @@ func TestBlockValidationRules(t *testing.T) { recalculateMsgBlockMerkleRootsSize(invalMissingInsS154) b154test = dcrutil.NewBlock(invalMissingInsS154) - err = blockchain.CheckWorklessBlockSanity(b154test, timeSource, params) + err = CheckWorklessBlockSanity(b154test, timeSource, params) if err != nil { t.Errorf("got unexpected error for invalMissingInsS154 sanity check: %v", err) } // Fails and hits ErrMissingTxOut. - err = chain.CheckConnectBlock(b154test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrMissingTxOut { + err = chain.CheckConnectBlock(b154test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrMissingTxOut { t.Errorf("Unexpected no or wrong error for invalMissingInsS154 test: %v", err) } @@ -1111,16 +1073,15 @@ func TestBlockValidationRules(t *testing.T) { recalculateMsgBlockMerkleRootsSize(malformedScr154) b154test = dcrutil.NewBlock(malformedScr154) - err = blockchain.CheckWorklessBlockSanity(b154test, timeSource, params) + err = CheckWorklessBlockSanity(b154test, timeSource, params) if err != nil { t.Errorf("got unexpected error for ErrScriptValidation sanity check: %v", err) } // Fails and hits ErrScriptMalformed. - err = chain.CheckConnectBlock(b154test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrScriptMalformed { + err = chain.CheckConnectBlock(b154test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrScriptMalformed { t.Errorf("Unexpected no or wrong error for ErrScriptMalformed test: %v", err) } @@ -1151,16 +1112,15 @@ func TestBlockValidationRules(t *testing.T) { recalculateMsgBlockMerkleRootsSize(spendZeroValueIn154) b154test = dcrutil.NewBlock(spendZeroValueIn154) - err = blockchain.CheckWorklessBlockSanity(b154test, timeSource, params) + err = CheckWorklessBlockSanity(b154test, timeSource, params) if err != nil { t.Errorf("got unexpected error for ErrZeroValueOutputSpend sanity "+ "check: %v", err) } // Fails and hits ErrZeroValueOutputSpend. - err = chain.CheckConnectBlock(b154test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrMissingTxOut { + err = chain.CheckConnectBlock(b154test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrMissingTxOut { t.Errorf("Unexpected no or wrong error for "+ "ErrMissingTxOut test: %v", err) } @@ -1193,7 +1153,7 @@ func TestBlockValidationRules(t *testing.T) { } } - _, _, err = chain.ProcessBlock(bl, blockchain.BFNoPoWCheck) + _, _, err = chain.ProcessBlock(bl, BFNoPoWCheck) if err != nil { t.Errorf("ProcessBlock error: %v", err.Error()) } @@ -1216,16 +1176,15 @@ func TestBlockValidationRules(t *testing.T) { recalculateMsgBlockMerkleRootsSize(spendInvalid166) b166test := dcrutil.NewBlock(spendInvalid166) - err = blockchain.CheckWorklessBlockSanity(b166test, timeSource, params) + err = CheckWorklessBlockSanity(b166test, timeSource, params) if err != nil { t.Errorf("got unexpected error for ErrMissingTxOut test 1 sanity "+ "check: %v", err) } // Fails and hits ErrMissingTxOut. - err = chain.CheckConnectBlock(b166test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrMissingTxOut { + err = chain.CheckConnectBlock(b166test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrMissingTxOut { t.Errorf("Unexpected no or wrong error for "+ "ErrMissingTxOut test 1: %v", err) } @@ -1262,16 +1221,15 @@ func TestBlockValidationRules(t *testing.T) { recalculateMsgBlockMerkleRootsSize(sstxSpendInvalid166) b166test = dcrutil.NewBlock(sstxSpendInvalid166) - err = blockchain.CheckWorklessBlockSanity(b166test, timeSource, params) + err = CheckWorklessBlockSanity(b166test, timeSource, params) if err != nil { t.Errorf("got unexpected error for ErrMissingTxOut test 2 sanity "+ "check: %v", err) } // Fails and hits ErrMissingTxOut. - err = chain.CheckConnectBlock(b166test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrMissingTxOut { + err = chain.CheckConnectBlock(b166test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrMissingTxOut { t.Errorf("Unexpected no or wrong error for "+ "ErrMissingTxOut test 2: %v", err) } @@ -1305,7 +1263,7 @@ func TestBlockValidationRules(t *testing.T) { recalculateMsgBlockMerkleRootsSize(sstxSpend2Invalid166) b166test = dcrutil.NewBlock(sstxSpend2Invalid166) - err = blockchain.CheckWorklessBlockSanity(b166test, timeSource, params) + err = CheckWorklessBlockSanity(b166test, timeSource, params) if err != nil { t.Errorf("got unexpected error for ErrImmatureSpend test sanity "+ "check: %v", err) @@ -1317,9 +1275,8 @@ func TestBlockValidationRules(t *testing.T) { // doesn't even bother to populate the spent list in the txlookup // and instead just writes the transaction hash as being missing. // This output doesn't become legal to spend until the next block. - err = chain.CheckConnectBlock(b166test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrImmatureSpend { + err = chain.CheckConnectBlock(b166test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrImmatureSpend { t.Errorf("Unexpected no or wrong error for "+ "ErrImmatureSpend test: %v", err) } @@ -1335,16 +1292,15 @@ func TestBlockValidationRules(t *testing.T) { recalculateMsgBlockMerkleRootsSize(sstxSpend3Invalid166) b166test = dcrutil.NewBlock(sstxSpend3Invalid166) - err = blockchain.CheckWorklessBlockSanity(b166test, timeSource, params) + err = CheckWorklessBlockSanity(b166test, timeSource, params) if err != nil { t.Errorf("got unexpected error for double spend test 1 sanity "+ "check: %v", err) } // Fails and hits ErrMissingTxOut. - err = chain.CheckConnectBlock(b166test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrMissingTxOut { + err = chain.CheckConnectBlock(b166test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrMissingTxOut { t.Errorf("Unexpected no or wrong error for "+ "double spend test 1: %v", err) } @@ -1360,16 +1316,15 @@ func TestBlockValidationRules(t *testing.T) { recalculateMsgBlockMerkleRootsSize(regTxSpendStakeIn166) b166test = dcrutil.NewBlock(regTxSpendStakeIn166) - err = blockchain.CheckWorklessBlockSanity(b166test, timeSource, params) + err = CheckWorklessBlockSanity(b166test, timeSource, params) if err != nil { t.Errorf("got unexpected error for deouble spend test 2 sanity "+ "check: %v", err) } // Fails and hits ErrMissingTxOut. - err = chain.CheckConnectBlock(b166test, blockchain.BFNoPoWCheck) - if err == nil || err.(blockchain.RuleError).ErrorCode != - blockchain.ErrMissingTxOut { + err = chain.CheckConnectBlock(b166test, BFNoPoWCheck) + if err == nil || err.(RuleError).ErrorCode != ErrMissingTxOut { t.Errorf("Unexpected no or wrong error for "+ "double spend test 2: %v", err) } @@ -1417,7 +1372,7 @@ func TestBlockchainSpendJournal(t *testing.T) { t.Fatalf("NewBlockFromBytes error: %v", err.Error()) } - _, _, err = chain.ProcessBlock(bl, blockchain.BFNone) + _, _, err = chain.ProcessBlock(bl, BFNone) if err != nil { t.Fatalf("ProcessBlock error at height %v: %v", i, err.Error()) } @@ -1503,11 +1458,11 @@ func TestSequenceLocksActive(t *testing.T) { } for _, test := range tests { - seqLock := blockchain.SequenceLock{ + seqLock := SequenceLock{ MinHeight: test.seqLockHeight, MinTime: test.seqLockTime, } - got := blockchain.SequenceLockActive(&seqLock, test.blockHeight, + got := SequenceLockActive(&seqLock, test.blockHeight, time.Unix(test.medianTime, 0)) if got != test.want { t.Errorf("%s: mismatched seqence lock status - got %v, "+ @@ -1521,9 +1476,9 @@ func TestSequenceLocksActive(t *testing.T) { // not on a chain. func TestCheckBlockSanity(t *testing.T) { params := &chaincfg.SimNetParams - timeSource := blockchain.NewMedianTime() + timeSource := NewMedianTime() block := dcrutil.NewBlock(&badBlock) - err := blockchain.CheckBlockSanity(block, timeSource, params) + err := CheckBlockSanity(block, timeSource, params) if err == nil { t.Fatalf("block should fail.\n") } @@ -1533,9 +1488,9 @@ func TestCheckBlockSanity(t *testing.T) { // checks with blocks not on a chain. func TestCheckWorklessBlockSanity(t *testing.T) { params := &chaincfg.SimNetParams - timeSource := blockchain.NewMedianTime() + timeSource := NewMedianTime() block := dcrutil.NewBlock(&badBlock) - err := blockchain.CheckWorklessBlockSanity(block, timeSource, params) + err := CheckWorklessBlockSanity(block, timeSource, params) if err == nil { t.Fatalf("block should fail.\n") } @@ -1558,17 +1513,17 @@ func TestCheckBlockHeaderContext(t *testing.T) { // Create a new BlockChain instance using the underlying database for // the simnet network. - chain, err := blockchain.New(&blockchain.Config{ + chain, err := New(&Config{ DB: db, ChainParams: params, - TimeSource: blockchain.NewMedianTime(), + TimeSource: NewMedianTime(), }) if err != nil { t.Fatalf("Failed to create chain instance: %v\n", err) return } - err = chain.TstCheckBlockHeaderContext(¶ms.GenesisBlock.Header, nil, blockchain.BFNone) + err = chain.checkBlockHeaderContext(¶ms.GenesisBlock.Header, nil, BFNone) if err != nil { t.Fatalf("genesisblock should pass just by definition: %v\n", err) return @@ -1577,8 +1532,8 @@ func TestCheckBlockHeaderContext(t *testing.T) { // Test failing checkBlockHeaderContext when calcNextRequiredDifficulty // fails. block := dcrutil.NewBlock(&badBlock) - newNode := blockchain.TstNewBlockNode(&block.MsgBlock().Header, nil) - err = chain.TstCheckBlockHeaderContext(&block.MsgBlock().Header, newNode, blockchain.BFNone) + newNode := newBlockNode(&block.MsgBlock().Header, nil) + err = chain.checkBlockHeaderContext(&block.MsgBlock().Header, newNode, BFNone) if err == nil { t.Fatalf("Should fail due to bad diff in newNode\n") return @@ -1603,16 +1558,16 @@ func TestTxValidationErrors(t *testing.T) { } // Ensure transaction is rejected due to being too large. - err := blockchain.CheckTransactionSanity(tx, &chaincfg.MainNetParams) - rerr, ok := err.(blockchain.RuleError) + err := CheckTransactionSanity(tx, &chaincfg.MainNetParams) + rerr, ok := err.(RuleError) if !ok { t.Fatalf("CheckTransactionSanity: unexpected error type for "+ "transaction that is too large -- got %T", err) } - if rerr.ErrorCode != blockchain.ErrTxTooBig { + if rerr.ErrorCode != ErrTxTooBig { t.Fatalf("CheckTransactionSanity: unexpected error code for "+ "transaction that is too large -- got %v, want %v", - rerr.ErrorCode, blockchain.ErrTxTooBig) + rerr.ErrorCode, ErrTxTooBig) } } diff --git a/blockchain/votebits_test.go b/blockchain/votebits_test.go index ad5c155bee..c0b1d28691 100644 --- a/blockchain/votebits_test.go +++ b/blockchain/votebits_test.go @@ -1541,69 +1541,6 @@ func TestVoting(t *testing.T) { } } -// Parallel test. -const ( - testDummy1ID = "testdummy1" - vbTestDummy1Yes = 0x04 - - testDummy2ID = "testdummy2" - vbTestDummy2No = 0x08 -) - -var ( - // testDummy1 is a voting agenda used throughout these tests. - testDummy1 = chaincfg.Vote{ - Id: testDummy1ID, - Description: "", - Mask: 0x6, // 0b0110 - Choices: []chaincfg.Choice{{ - Id: "abstain", - Description: "abstain voting for change", - Bits: 0x0000, - IsAbstain: true, - IsNo: false, - }, { - Id: "no", - Description: "vote no", - Bits: 0x0002, // Bit 1 - IsAbstain: false, - IsNo: true, - }, { - Id: "yes", - Description: "vote yes", - Bits: 0x0004, // Bit 2 - IsAbstain: false, - IsNo: false, - }}, - } - - // testDummy2 is a voting agenda used throughout these tests. - testDummy2 = chaincfg.Vote{ - Id: testDummy2ID, - Description: "", - Mask: 0x18, // 0b11000 - Choices: []chaincfg.Choice{{ - Id: "abstain", - Description: "abstain voting for change", - Bits: 0x0000, - IsAbstain: true, - IsNo: false, - }, { - Id: "no", - Description: "vote no", - Bits: 0x0008, // Bit 3 - IsAbstain: false, - IsNo: true, - }, { - Id: "yes", - Description: "vote yes", - Bits: 0x0010, // Bit 4 - IsAbstain: false, - IsNo: false, - }}, - } -) - func defaultParallelParams() chaincfg.Params { params := chaincfg.SimNetParams params.Deployments = make(map[uint32][]chaincfg.ConsensusDeployment)