67394ec45d
This commit change the ProcessBlock function to accept a new type named BehaviorFlags in place of the current fastAdd parameter. This has been done to pave the way for adding more control over the checks that are performed such as avoiding the proof of work checks which will be in an upcoming commit. A bitmask was chosen because it will allow the ProcessBlock API to remain a little more stable since new flag additions that change the behavior are only likely to be used by new code because adding flags does not change the existing behavior. ok @jrick
132 lines
3.2 KiB
Go
132 lines
3.2 KiB
Go
// Copyright (c) 2013-2014 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 (
|
|
"compress/bzip2"
|
|
"encoding/binary"
|
|
"github.com/conformal/btcchain"
|
|
"github.com/conformal/btcutil"
|
|
"github.com/conformal/btcwire"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// TestReorganization loads a set of test blocks which force a chain
|
|
// reorganization to test the block chain handling code.
|
|
// The test blocks were originally from a post on the bitcoin talk forums:
|
|
// https://bitcointalk.org/index.php?topic=46370.msg577556#msg577556
|
|
func TestReorganization(t *testing.T) {
|
|
// Intentionally load the side chain blocks out of order to ensure
|
|
// orphans are handled properly along with chain reorganization.
|
|
testFiles := []string{
|
|
"blk_0_to_4.dat.bz2",
|
|
"blk_4A.dat.bz2",
|
|
"blk_5A.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)
|
|
}
|
|
for _, block := range blockTmp {
|
|
blocks = append(blocks, block)
|
|
}
|
|
}
|
|
|
|
t.Logf("Number of blocks: %v\n", len(blocks))
|
|
|
|
// Create a new database and chain instance to run tests against.
|
|
chain, teardownFunc, err := chainSetup("reorg")
|
|
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)
|
|
|
|
expectedOrphans := map[int]bool{5: true, 6: true}
|
|
for i := 1; i < len(blocks); i++ {
|
|
isOrphan, err := chain.ProcessBlock(blocks[i], btcchain.BFNone)
|
|
if err != nil {
|
|
t.Errorf("ProcessBlock fail on block %v: %v\n", i, err)
|
|
return
|
|
}
|
|
if isOrphan && !expectedOrphans[i] {
|
|
t.Errorf("ProcessBlock incorrectly returned block %v "+
|
|
"is an orphan\n", i)
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// loadBlocks reads files containing bitcoin block data (gzipped but otherwise
|
|
// in the format bitcoind writes) from disk and returns them as an array of
|
|
// btcutil.Block. This is largely borrowed from the test code in btcdb.
|
|
func loadBlocks(filename string) (blocks []*btcutil.Block, err error) {
|
|
filename = filepath.Join("testdata/", filename)
|
|
|
|
var network = btcwire.MainNet
|
|
var dr io.Reader
|
|
var fi io.ReadCloser
|
|
|
|
fi, err = os.Open(filename)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
if strings.HasSuffix(filename, ".bz2") {
|
|
dr = bzip2.NewReader(fi)
|
|
} else {
|
|
dr = fi
|
|
}
|
|
defer fi.Close()
|
|
|
|
var block *btcutil.Block
|
|
|
|
err = nil
|
|
for height := int64(1); err == nil; height++ {
|
|
var rintbuf uint32
|
|
err = binary.Read(dr, binary.LittleEndian, &rintbuf)
|
|
if err == io.EOF {
|
|
// hit end of file at expected offset: no warning
|
|
height--
|
|
err = nil
|
|
break
|
|
}
|
|
if err != nil {
|
|
break
|
|
}
|
|
if rintbuf != uint32(network) {
|
|
break
|
|
}
|
|
err = binary.Read(dr, binary.LittleEndian, &rintbuf)
|
|
blocklen := rintbuf
|
|
|
|
rbytes := make([]byte, blocklen)
|
|
|
|
// read block
|
|
dr.Read(rbytes)
|
|
|
|
block, err = btcutil.NewBlockFromBytes(rbytes)
|
|
if err != nil {
|
|
return
|
|
}
|
|
blocks = append(blocks, block)
|
|
}
|
|
|
|
return
|
|
}
|