blockchain: Remove superfluous reorg tests.

This removes a couple of the reorganization tests that rely on static
test data since that logic is much better handled by the dynamic full
block tests and thus is no longer needed.
This commit is contained in:
Dave Collins 2018-02-23 18:36:01 -06:00
parent 3d5646e54f
commit 7a9ce1ed6a
No known key found for this signature in database
GPG Key ID: B8904D9D9C93D1F2

View File

@ -1,5 +1,5 @@
// Copyright (c) 2013-2014 The btcsuite developers
// Copyright (c) 2015-2017 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.
@ -19,235 +19,6 @@ import (
"github.com/decred/dcrd/dcrutil"
)
// reorgTestLong does a single, large reorganization.
func reorgTestLong(t *testing.T, params *chaincfg.Params) {
// Create a new database and chain instance to run tests against.
chain, teardownFunc, err := chainSetup("reorgunittest", params)
if err != nil {
t.Errorf("Failed to setup chain instance: %v", err)
return
}
defer teardownFunc()
// The genesis block should fail to connect since it's already
// inserted.
err = chain.CheckConnectBlock(dcrutil.NewBlock(params.GenesisBlock),
blockchain.BFNone)
if err == nil {
t.Errorf("CheckConnectBlock: Did not receive expected error")
}
// Load up the rest of the blocks up to HEAD.
filename := filepath.Join("testdata/", "reorgto179.bz2")
fi, err := os.Open(filename)
if err != nil {
t.Errorf("Unable to open %s: %v", filename, err)
}
bcStream := bzip2.NewReader(fi)
defer fi.Close()
// Create a buffer of the read file
bcBuf := new(bytes.Buffer)
bcBuf.ReadFrom(bcStream)
// Create decoder from the buffer and a map to store the data
bcDecoder := gob.NewDecoder(bcBuf)
blockChain := make(map[int64][]byte)
// Decode the blockchain into the map
if err := bcDecoder.Decode(&blockChain); err != nil {
t.Errorf("error decoding test blockchain: %v", err.Error())
}
// Load up the short chain
finalIdx1 := 179
for i := 1; i < finalIdx1+1; i++ {
bl, err := dcrutil.NewBlockFromBytes(blockChain[int64(i)])
if err != nil {
t.Fatalf("NewBlockFromBytes error: %v", err.Error())
}
_, _, err = chain.ProcessBlock(bl, blockchain.BFNone)
if err != nil {
t.Fatalf("ProcessBlock error at height %v: %v", i, err.Error())
}
}
// Load the long chain and begin loading blocks from that too,
// forcing a reorganization
// Load up the rest of the blocks up to HEAD.
filename = filepath.Join("testdata/", "reorgto180.bz2")
fi, err = os.Open(filename)
if err != nil {
t.Errorf("Unable to open %s: %v", filename, err)
}
bcStream = bzip2.NewReader(fi)
defer fi.Close()
// Create a buffer of the read file
bcBuf = new(bytes.Buffer)
bcBuf.ReadFrom(bcStream)
// Create decoder from the buffer and a map to store the data
bcDecoder = gob.NewDecoder(bcBuf)
blockChain = make(map[int64][]byte)
// Decode the blockchain into the map
if err := bcDecoder.Decode(&blockChain); err != nil {
t.Errorf("error decoding test blockchain: %v", err.Error())
}
forkPoint := 131
finalIdx2 := 180
for i := forkPoint; i < finalIdx2+1; i++ {
bl, err := dcrutil.NewBlockFromBytes(blockChain[int64(i)])
if err != nil {
t.Fatalf("NewBlockFromBytes error: %v", err.Error())
}
_, _, err = chain.ProcessBlock(bl, blockchain.BFNone)
if err != nil {
t.Fatalf("ProcessBlock error: %v", err.Error())
}
}
// Ensure our blockchain is at the correct best tip
topBlock, _ := chain.GetTopBlock()
tipHash := topBlock.Hash()
expected, _ := chainhash.NewHashFromStr("5ab969d0afd8295b6cd1506f2a310d" +
"259322015c8bd5633f283a163ce0e50594")
if *tipHash != *expected {
t.Errorf("Failed to correctly reorg; expected tip %v, got tip %v",
expected, tipHash)
}
have, err := chain.HaveBlock(expected)
if !have {
t.Errorf("missing tip block after reorganization test")
}
if err != nil {
t.Errorf("unexpected error testing for presence of new tip block "+
"after reorg test: %v", err)
}
}
// reorgTestsShort does short reorganizations to test multiple, frequent
// reorganizations.
func reorgTestShort(t *testing.T, params *chaincfg.Params) {
// Create a new database and chain instance to run tests against.
chain, teardownFunc, err := chainSetup("reorgunittestshort", params)
if err != nil {
t.Errorf("Failed to setup chain instance: %v", err)
return
}
defer teardownFunc()
// The genesis block should fail to connect since it's already
// inserted.
err = chain.CheckConnectBlock(dcrutil.NewBlock(params.GenesisBlock),
blockchain.BFNone)
if err == nil {
t.Errorf("CheckConnectBlock: Did not receive expected error")
}
// Load up the rest of the blocks up to HEAD.
filename := filepath.Join("testdata/", "reorgto179.bz2")
fi, err := os.Open(filename)
if err != nil {
t.Errorf("Unable to open %s: %v", filename, err)
}
bcStream := bzip2.NewReader(fi)
defer fi.Close()
// Create a buffer of the read file
bcBuf := new(bytes.Buffer)
bcBuf.ReadFrom(bcStream)
// Create decoder from the buffer and a map to store the data
bcDecoder := gob.NewDecoder(bcBuf)
blockChain1 := make(map[int64][]byte)
// Decode the blockchain into the map
if err := bcDecoder.Decode(&blockChain1); err != nil {
t.Errorf("error decoding test blockchain: %v", err.Error())
}
// Load the long chain and begin loading blocks from that too,
// forcing a reorganization
// Load up the rest of the blocks up to HEAD.
filename = filepath.Join("testdata/", "reorgto180.bz2")
fi, err = os.Open(filename)
if err != nil {
t.Errorf("Unable to open %s: %v", filename, err)
}
bcStream = bzip2.NewReader(fi)
defer fi.Close()
// Create a buffer of the read file
bcBuf = new(bytes.Buffer)
bcBuf.ReadFrom(bcStream)
// Create decoder from the buffer and a map to store the data
bcDecoder = gob.NewDecoder(bcBuf)
blockChain2 := make(map[int64][]byte)
// Decode the blockchain into the map
if err := bcDecoder.Decode(&blockChain2); err != nil {
t.Errorf("error decoding test blockchain: %v", err.Error())
}
forkPoint := 131
finalIdx2 := 180
for i := 1; i < forkPoint+1; i++ {
bl, err := dcrutil.NewBlockFromBytes(blockChain1[int64(i)])
if err != nil {
t.Fatalf("NewBlockFromBytes error: %v", err.Error())
}
_, _, err = chain.ProcessBlock(bl, blockchain.BFNone)
if err != nil {
t.Fatalf("ProcessBlock error at height %v: %v", i, err.Error())
}
}
// Reorg each block.
dominant := blockChain2
orphaned := blockChain1
for i := forkPoint; i < finalIdx2; i++ {
for j := 0; j < 2; j++ {
bl, err := dcrutil.NewBlockFromBytes(dominant[int64(i+j)])
if err != nil {
t.Fatalf("NewBlockFromBytes error: %v", err.Error())
}
_, _, err = chain.ProcessBlock(bl, blockchain.BFNone)
if err != nil {
t.Fatalf("ProcessBlock error: %v", err.Error())
}
}
dominant, orphaned = orphaned, dominant
}
// Ensure our blockchain is at the correct best tip
topBlock, _ := chain.GetTopBlock()
tipHash := topBlock.Hash()
expected, _ := chainhash.NewHashFromStr("5ab969d0afd8295b6cd1506f2a310d" +
"259322015c8bd5633f283a163ce0e50594")
if *tipHash != *expected {
t.Errorf("Failed to correctly reorg; expected tip %v, got tip %v",
expected, tipHash)
}
have, err := chain.HaveBlock(expected)
if !have {
t.Errorf("missing tip block after reorganization test")
}
if err != nil {
t.Errorf("unexpected error testing for presence of new tip block "+
"after reorg test: %v", err)
}
}
// reorgTestsForced tests a forced reorganization of a single block at HEAD.
func reorgTestForced(t *testing.T, params *chaincfg.Params) {
// Create a new database and chain instance to run tests against.
@ -377,7 +148,5 @@ func TestReorganization(t *testing.T) {
genesisHash := params.GenesisBlock.BlockHash()
params.GenesisHash = &genesisHash
reorgTestLong(t, params)
reorgTestShort(t, params)
reorgTestForced(t, params)
}