Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

blockchain: Remove superfluous reorg tests. #1072

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
233 changes: 1 addition & 232 deletions blockchain/reorganization_test.go
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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.
Expand Down Expand Up @@ -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)
}