2016-08-08 21:04:33 +02:00
|
|
|
// Copyright (c) 2013-2016 The btcsuite developers
|
2013-10-10 01:43:22 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2015-01-30 21:54:30 +01:00
|
|
|
package blockchain_test
|
2013-10-10 01:43:22 +02:00
|
|
|
|
|
|
|
import (
|
2014-07-02 18:04:59 +02:00
|
|
|
"testing"
|
|
|
|
|
2015-01-30 21:54:30 +01:00
|
|
|
"github.com/btcsuite/btcd/blockchain"
|
2015-02-06 06:18:27 +01:00
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
2016-08-08 21:04:33 +02:00
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
2015-01-15 17:23:47 +01:00
|
|
|
"github.com/btcsuite/btcutil"
|
2013-10-10 01:43:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
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)
|
2015-01-30 21:54:30 +01:00
|
|
|
blockchain.TstSetCoinbaseMaturity(1)
|
2013-10-10 01:43:22 +02:00
|
|
|
|
|
|
|
for i := 1; i < len(blocks); i++ {
|
2016-07-14 02:36:36 +02:00
|
|
|
isOrphan, err := chain.ProcessBlock(blocks[i], blockchain.BFNone)
|
2013-10-10 01:43:22 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("ProcessBlock fail on block %v: %v\n", i, err)
|
|
|
|
return
|
|
|
|
}
|
2014-06-25 22:47:24 +02:00
|
|
|
if isOrphan {
|
|
|
|
t.Errorf("ProcessBlock incorrectly returned block %v "+
|
|
|
|
"is an orphan\n", i)
|
|
|
|
return
|
|
|
|
}
|
2013-10-10 01:43:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Insert an orphan block.
|
2014-10-03 22:29:25 +02:00
|
|
|
isOrphan, err := chain.ProcessBlock(btcutil.NewBlock(&Block100000),
|
2016-07-14 02:36:36 +02:00
|
|
|
blockchain.BFNone)
|
2014-06-25 22:47:24 +02:00
|
|
|
if err != nil {
|
2013-10-10 01:43:22 +02:00
|
|
|
t.Errorf("Unable to process block: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2014-06-25 22:47:24 +02:00
|
|
|
if !isOrphan {
|
|
|
|
t.Errorf("ProcessBlock indicated block is an not orphan when " +
|
|
|
|
"it should be\n")
|
|
|
|
return
|
|
|
|
}
|
2013-10-10 01:43:22 +02:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
hash string
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
// Genesis block should be present (in the main chain).
|
2015-02-06 06:18:27 +01:00
|
|
|
{hash: chaincfg.MainNetParams.GenesisHash.String(), want: true},
|
2013-10-10 01:43:22 +02:00
|
|
|
|
|
|
|
// 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},
|
|
|
|
|
2016-02-25 18:17:12 +01:00
|
|
|
// Random hashes should not be available.
|
2013-10-10 01:43:22 +02:00
|
|
|
{hash: "123", want: false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2016-08-08 21:04:33 +02:00
|
|
|
hash, err := chainhash.NewHashFromStr(test.hash)
|
2013-10-10 01:43:22 +02:00
|
|
|
if err != nil {
|
2016-08-08 21:04:33 +02:00
|
|
|
t.Errorf("NewHashFromStr: %v", err)
|
2013-10-10 01:43:22 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-07-07 18:42:28 +02:00
|
|
|
result, err := chain.HaveBlock(hash)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("HaveBlock #%d unexpected error: %v", i, err)
|
|
|
|
return
|
|
|
|
}
|
2013-10-10 01:43:22 +02:00
|
|
|
if result != test.want {
|
|
|
|
t.Errorf("HaveBlock #%d got %v want %v", i, result,
|
|
|
|
test.want)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|