Add tests for HaveBlock.
This commit adds tests to ensure HaveBlock works properly with blocks on the main chain, a side chain, orphans, and missing blocks.
This commit is contained in:
parent
6d078d8115
commit
32790d52d8
2 changed files with 116 additions and 21 deletions
95
chain_test.go
Normal file
95
chain_test.go
Normal file
|
@ -0,0 +1,95 @@
|
|||
// Copyright (c) 2013 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcchain_test
|
||||
|
||||
import (
|
||||
"github.com/conformal/btcchain"
|
||||
"github.com/conformal/btcutil"
|
||||
"github.com/conformal/btcwire"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestHaveBlock tests the HaveBlock API to ensure proper functionality.
|
||||
func TestHaveBlock(t *testing.T) {
|
||||
// Load up blocks such that there is a side chain.
|
||||
// (genesis block) -> 1 -> 2 -> 3 -> 4
|
||||
// \-> 3a
|
||||
// orphans are handled properly along with chain reorganization.
|
||||
testFiles := []string{
|
||||
"blk_0_to_4.dat.bz2",
|
||||
"blk_3A.dat.bz2",
|
||||
}
|
||||
|
||||
var blocks []*btcutil.Block
|
||||
for _, file := range testFiles {
|
||||
blockTmp, err := loadBlocks(file)
|
||||
if err != nil {
|
||||
t.Errorf("Error loading file: %v\n", err)
|
||||
return
|
||||
}
|
||||
for _, block := range blockTmp {
|
||||
blocks = append(blocks, block)
|
||||
}
|
||||
}
|
||||
|
||||
// Create a new database and chain instance to run tests against.
|
||||
chain, teardownFunc, err := chainSetup("haveblock")
|
||||
if err != nil {
|
||||
t.Errorf("Failed to setup chain instance: %v", err)
|
||||
return
|
||||
}
|
||||
defer teardownFunc()
|
||||
|
||||
// Since we're not dealing with the real block chain, disable
|
||||
// checkpoints and set the coinbase maturity to 1.
|
||||
chain.DisableCheckpoints(true)
|
||||
btcchain.TstSetCoinbaseMaturity(1)
|
||||
|
||||
for i := 1; i < len(blocks); i++ {
|
||||
err = chain.ProcessBlock(blocks[i])
|
||||
if err != nil {
|
||||
t.Errorf("ProcessBlock fail on block %v: %v\n", i, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Insert an orphan block.
|
||||
if err := chain.ProcessBlock(btcutil.NewBlock(&Block100000)); err != nil {
|
||||
t.Errorf("Unable to process block: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
hash string
|
||||
want bool
|
||||
}{
|
||||
// Genesis block should be present (in the main chain).
|
||||
{hash: btcwire.GenesisHash.String(), want: true},
|
||||
|
||||
// Block 3a should be present (on a side chain).
|
||||
{hash: "00000000474284d20067a4d33f6a02284e6ef70764a3a26d6a5b9df52ef663dd", want: true},
|
||||
|
||||
// Block 100000 should be present (as an orphan).
|
||||
{hash: "000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506", want: true},
|
||||
|
||||
// Random hashes should not be availble.
|
||||
{hash: "123", want: false},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
hash, err := btcwire.NewShaHashFromStr(test.hash)
|
||||
if err != nil {
|
||||
t.Errorf("NewShaHashFromStr: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
result := chain.HaveBlock(hash)
|
||||
if result != test.want {
|
||||
t.Errorf("HaveBlock #%d got %v want %v", i, result,
|
||||
test.want)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,38 +3,40 @@ github.com/conformal/btcchain/validate.go checkSerializedHeight 100.00% (17/
|
|||
github.com/conformal/btcchain/chain.go BlockChain.removeOrphanBlock 100.00% (16/16)
|
||||
github.com/conformal/btcchain/validate.go countSigOps 100.00% (8/8)
|
||||
github.com/conformal/btcchain/checkpoints.go init 100.00% (6/6)
|
||||
github.com/conformal/btcchain/chain.go BlockChain.IsKnownOrphan 100.00% (5/5)
|
||||
github.com/conformal/btcchain/difficulty.go ShaHashToBig 100.00% (5/5)
|
||||
github.com/conformal/btcchain/merkle.go hashMerkleBranches 100.00% (5/5)
|
||||
github.com/conformal/btcchain/chain.go newBlockNode 100.00% (4/4)
|
||||
github.com/conformal/btcchain/merkle.go nextPowerOfTwo 100.00% (4/4)
|
||||
github.com/conformal/btcchain/chain.go newBlockNode 100.00% (4/4)
|
||||
github.com/conformal/btcchain/process.go BlockChain.blockExists 100.00% (3/3)
|
||||
github.com/conformal/btcchain/checkpoints.go newShaHashFromStr 100.00% (2/2)
|
||||
github.com/conformal/btcchain/chain.go New 100.00% (2/2)
|
||||
github.com/conformal/btcchain/log.go init 100.00% (1/1)
|
||||
github.com/conformal/btcchain/timesorter.go timeSorter.Less 100.00% (1/1)
|
||||
github.com/conformal/btcchain/timesorter.go timeSorter.Swap 100.00% (1/1)
|
||||
github.com/conformal/btcchain/params.go BlockChain.chainParams 100.00% (1/1)
|
||||
github.com/conformal/btcchain/checkpoints.go BlockChain.DisableCheckpoints 100.00% (1/1)
|
||||
github.com/conformal/btcchain/validate.go calcBlockSubsidy 100.00% (1/1)
|
||||
github.com/conformal/btcchain/timesorter.go timeSorter.Len 100.00% (1/1)
|
||||
github.com/conformal/btcchain/checkpoints.go newShaHashFromStr 100.00% (2/2)
|
||||
github.com/conformal/btcchain/log.go DisableLog 100.00% (1/1)
|
||||
github.com/conformal/btcchain/validate.go calcBlockSubsidy 100.00% (1/1)
|
||||
github.com/conformal/btcchain/chain.go BlockChain.HaveBlock 100.00% (1/1)
|
||||
github.com/conformal/btcchain/timesorter.go timeSorter.Less 100.00% (1/1)
|
||||
github.com/conformal/btcchain/timesorter.go timeSorter.Len 100.00% (1/1)
|
||||
github.com/conformal/btcchain/timesorter.go timeSorter.Swap 100.00% (1/1)
|
||||
github.com/conformal/btcchain/checkpoints.go BlockChain.DisableCheckpoints 100.00% (1/1)
|
||||
github.com/conformal/btcchain/params.go BlockChain.chainParams 100.00% (1/1)
|
||||
github.com/conformal/btcchain/log.go init 100.00% (1/1)
|
||||
github.com/conformal/btcchain/merkle.go BuildMerkleTreeStore 94.12% (16/17)
|
||||
github.com/conformal/btcchain/txlookup.go disconnectTransactions 93.75% (15/16)
|
||||
github.com/conformal/btcchain/txlookup.go fetchTxListMain 93.33% (14/15)
|
||||
github.com/conformal/btcchain/chain.go BlockChain.getReorganizeNodes 92.86% (13/14)
|
||||
github.com/conformal/btcchain/process.go BlockChain.processOrphans 92.86% (13/14)
|
||||
github.com/conformal/btcchain/chain.go BlockChain.connectBestChain 90.00% (27/30)
|
||||
github.com/conformal/btcchain/chain.go BlockChain.getPrevNodeFromBlock 88.89% (8/9)
|
||||
github.com/conformal/btcchain/scriptval.go ValidateTransactionScripts 88.24% (30/34)
|
||||
github.com/conformal/btcchain/chain.go BlockChain.calcPastMedianTime 88.24% (15/17)
|
||||
github.com/conformal/btcchain/txlookup.go BlockChain.fetchTxList 86.36% (19/22)
|
||||
github.com/conformal/btcchain/scriptval.go checkBlockScripts 85.71% (6/7)
|
||||
github.com/conformal/btcchain/chain.go BlockChain.reorganizeChain 85.29% (29/34)
|
||||
github.com/conformal/btcchain/chain.go BlockChain.connectBlock 83.33% (10/12)
|
||||
github.com/conformal/btcchain/validate.go IsCoinBase 83.33% (5/6)
|
||||
github.com/conformal/btcchain/chain.go BlockChain.calcPastMedianTime 82.35% (14/17)
|
||||
github.com/conformal/btcchain/chain.go BlockChain.isMajorityVersion 80.00% (8/10)
|
||||
github.com/conformal/btcchain/difficulty.go calcWork 80.00% (4/5)
|
||||
github.com/conformal/btcchain/chain.go BlockChain.addOrphanBlock 77.78% (14/18)
|
||||
github.com/conformal/btcchain/chain.go BlockChain.getPrevNodeFromBlock 77.78% (7/9)
|
||||
github.com/conformal/btcchain/txlookup.go BlockChain.fetchInputTransactions 76.92% (20/26)
|
||||
github.com/conformal/btcchain/chain.go BlockChain.disconnectBlock 76.92% (10/13)
|
||||
github.com/conformal/btcchain/difficulty.go BigToCompact 75.00% (12/16)
|
||||
|
@ -50,41 +52,39 @@ github.com/conformal/btcchain/params.go ChainParams 60.00% (3/5)
|
|||
github.com/conformal/btcchain/validate.go isBIP0030Node 60.00% (3/5)
|
||||
github.com/conformal/btcchain/validate.go BlockChain.checkProofOfWork 58.82% (10/17)
|
||||
github.com/conformal/btcchain/validate.go BlockChain.checkBIP0030 57.14% (8/14)
|
||||
github.com/conformal/btcchain/process.go BlockChain.ProcessBlock 55.81% (24/43)
|
||||
github.com/conformal/btcchain/accept.go BlockChain.maybeAcceptBlock 53.52% (38/71)
|
||||
github.com/conformal/btcchain/process.go BlockChain.ProcessBlock 53.49% (23/43)
|
||||
github.com/conformal/btcchain/chain.go BlockChain.loadBlockNode 50.00% (11/22)
|
||||
github.com/conformal/btcchain/chain.go BlockChain.getPrevNodeFromNode 50.00% (4/8)
|
||||
github.com/conformal/btcchain/notifications.go BlockChain.sendNotification 50.00% (2/4)
|
||||
github.com/conformal/btcchain/checkpoints.go BlockChain.LatestCheckpoint 50.00% (2/4)
|
||||
github.com/conformal/btcchain/accept.go BlockChain.maybeAcceptBlock 49.30% (35/71)
|
||||
github.com/conformal/btcchain/chain.go BlockChain.pruneBlockNodes 41.18% (7/17)
|
||||
github.com/conformal/btcchain/checkpoints.go BlockChain.verifyCheckpoint 33.33% (2/6)
|
||||
github.com/conformal/btcchain/validate.go IsFinalizedTransaction 23.08% (3/13)
|
||||
github.com/conformal/btcchain/difficulty.go BlockChain.calcNextRequiredDifficulty 19.51% (8/41)
|
||||
github.com/conformal/btcchain/checkpoints.go BlockChain.findLatestKnownCheckpoint 18.18% (2/11)
|
||||
github.com/conformal/btcchain/difficulty.go BlockChain.calcNextRequiredDifficulty 17.07% (7/41)
|
||||
github.com/conformal/btcchain/blocklocator.go BlockChain.BlockLocatorFromHash 0.00% (0/39)
|
||||
github.com/conformal/btcchain/checkpoints.go BlockChain.IsCheckpointCandidate 0.00% (0/32)
|
||||
github.com/conformal/btcchain/validate.go countP2SHSigOps 0.00% (0/24)
|
||||
github.com/conformal/btcchain/chain.go BlockChain.GenerateInitialIndex 0.00% (0/17)
|
||||
github.com/conformal/btcchain/difficulty.go BlockChain.calcEasiestDifficulty 0.00% (0/15)
|
||||
github.com/conformal/btcchain/txlookup.go BlockChain.FetchTransactionStore 0.00% (0/15)
|
||||
github.com/conformal/btcchain/difficulty.go BlockChain.findPrevTestNetDifficulty 0.00% (0/12)
|
||||
github.com/conformal/btcchain/chain.go BlockChain.removeBlockNode 0.00% (0/12)
|
||||
github.com/conformal/btcchain/difficulty.go BlockChain.findPrevTestNetDifficulty 0.00% (0/12)
|
||||
github.com/conformal/btcchain/chain.go BlockChain.GetOrphanRoot 0.00% (0/11)
|
||||
github.com/conformal/btcchain/chain.go BlockChain.IsCurrent 0.00% (0/9)
|
||||
github.com/conformal/btcchain/chain.go removeChildNode 0.00% (0/8)
|
||||
github.com/conformal/btcchain/log.go SetLogWriter 0.00% (0/7)
|
||||
github.com/conformal/btcchain/chain.go BlockChain.HaveInventory 0.00% (0/7)
|
||||
github.com/conformal/btcchain/blocklocator.go BlockChain.LatestBlockLocator 0.00% (0/6)
|
||||
github.com/conformal/btcchain/checkpoints.go isNonstandardTransaction 0.00% (0/5)
|
||||
github.com/conformal/btcchain/chain.go BlockChain.IsKnownOrphan 0.00% (0/5)
|
||||
github.com/conformal/btcchain/validate.go isTransactionSpent 0.00% (0/4)
|
||||
github.com/conformal/btcchain/checkpoints.go BlockChain.checkpointData 0.00% (0/4)
|
||||
github.com/conformal/btcchain/chain.go addChildrenWork 0.00% (0/3)
|
||||
github.com/conformal/btcchain/notifications.go NotificationType.String 0.00% (0/3)
|
||||
github.com/conformal/btcchain/log.go UseLogger 0.00% (0/1)
|
||||
github.com/conformal/btcchain/log.go newLogClosure 0.00% (0/1)
|
||||
github.com/conformal/btcchain/log.go logClosure.String 0.00% (0/1)
|
||||
github.com/conformal/btcchain/process.go RuleError.Error 0.00% (0/1)
|
||||
github.com/conformal/btcchain/log.go newLogClosure 0.00% (0/1)
|
||||
github.com/conformal/btcchain/chain.go BlockChain.DisableVerify 0.00% (0/1)
|
||||
github.com/conformal/btcchain ------------------------------------- 55.20% (642/1163)
|
||||
github.com/conformal/btcchain/log.go UseLogger 0.00% (0/1)
|
||||
github.com/conformal/btcchain/process.go RuleError.Error 0.00% (0/1)
|
||||
github.com/conformal/btcchain ------------------------------------- 55.40% (641/1157)
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue