2015-05-01 08:28:01 +02:00
|
|
|
// Copyright (c) 2013-2014 The btcsuite developers
|
2013-10-10 01:29:53 +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:29:53 +02:00
|
|
|
|
|
|
|
import (
|
2015-01-08 12:08:11 +01:00
|
|
|
"compress/bzip2"
|
|
|
|
"encoding/binary"
|
2013-10-10 01:29:53 +02:00
|
|
|
"fmt"
|
2015-01-08 12:08:11 +01:00
|
|
|
"io"
|
2014-07-02 18:04:59 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-01-08 12:08:11 +01:00
|
|
|
"strings"
|
2014-07-02 18:04:59 +02:00
|
|
|
|
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"
|
2015-01-27 22:00:49 +01:00
|
|
|
"github.com/btcsuite/btcd/database"
|
|
|
|
_ "github.com/btcsuite/btcd/database/ldb"
|
|
|
|
_ "github.com/btcsuite/btcd/database/memdb"
|
2015-02-05 22:16:39 +01:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2015-01-15 17:23:47 +01:00
|
|
|
"github.com/btcsuite/btcutil"
|
2013-10-10 01:29:53 +02:00
|
|
|
)
|
|
|
|
|
2013-11-08 08:52:31 +01:00
|
|
|
// testDbType is the database backend type to use for the tests.
|
|
|
|
const testDbType = "memdb"
|
|
|
|
|
2013-10-10 01:29:53 +02:00
|
|
|
// testDbRoot is the root directory used to create all test databases.
|
|
|
|
const testDbRoot = "testdbs"
|
|
|
|
|
|
|
|
// filesExists returns whether or not the named file or directory exists.
|
|
|
|
func fileExists(name string) bool {
|
|
|
|
if _, err := os.Stat(name); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2013-11-08 08:52:31 +01:00
|
|
|
// isSupportedDbType returns whether or not the passed database type is
|
|
|
|
// currently supported.
|
|
|
|
func isSupportedDbType(dbType string) bool {
|
2015-01-27 22:00:49 +01:00
|
|
|
supportedDBs := database.SupportedDBs()
|
2013-11-08 08:52:31 +01:00
|
|
|
for _, sDbType := range supportedDBs {
|
|
|
|
if dbType == sDbType {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2013-10-10 01:29:53 +02:00
|
|
|
// chainSetup is used to create a new db and chain instance with the genesis
|
|
|
|
// block already inserted. In addition to the new chain instnce, it returns
|
|
|
|
// a teardown function the caller should invoke when done testing to clean up.
|
2015-01-30 21:54:30 +01:00
|
|
|
func chainSetup(dbName string) (*blockchain.BlockChain, func(), error) {
|
2013-11-08 08:52:31 +01:00
|
|
|
if !isSupportedDbType(testDbType) {
|
|
|
|
return nil, nil, fmt.Errorf("unsupported db type %v", testDbType)
|
2013-10-10 01:29:53 +02:00
|
|
|
}
|
|
|
|
|
2013-11-08 08:52:31 +01:00
|
|
|
// Handle memory database specially since it doesn't need the disk
|
|
|
|
// specific handling.
|
2015-01-27 22:00:49 +01:00
|
|
|
var db database.Db
|
2013-11-08 08:52:31 +01:00
|
|
|
var teardown func()
|
|
|
|
if testDbType == "memdb" {
|
2015-01-27 22:00:49 +01:00
|
|
|
ndb, err := database.CreateDB(testDbType)
|
2013-11-08 08:52:31 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("error creating db: %v", err)
|
|
|
|
}
|
|
|
|
db = ndb
|
|
|
|
|
|
|
|
// Setup a teardown function for cleaning up. This function is
|
|
|
|
// returned to the caller to be invoked when it is done testing.
|
|
|
|
teardown = func() {
|
|
|
|
db.Close()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Create the root directory for test databases.
|
|
|
|
if !fileExists(testDbRoot) {
|
|
|
|
if err := os.MkdirAll(testDbRoot, 0700); err != nil {
|
|
|
|
err := fmt.Errorf("unable to create test db "+
|
|
|
|
"root: %v", err)
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new database to store the accepted blocks into.
|
|
|
|
dbPath := filepath.Join(testDbRoot, dbName)
|
|
|
|
_ = os.RemoveAll(dbPath)
|
2015-01-27 22:00:49 +01:00
|
|
|
ndb, err := database.CreateDB(testDbType, dbPath)
|
2013-11-08 08:52:31 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("error creating db: %v", err)
|
|
|
|
}
|
|
|
|
db = ndb
|
2013-10-10 01:29:53 +02:00
|
|
|
|
2013-11-08 08:52:31 +01:00
|
|
|
// Setup a teardown function for cleaning up. This function is
|
|
|
|
// returned to the caller to be invoked when it is done testing.
|
|
|
|
teardown = func() {
|
|
|
|
dbVersionPath := filepath.Join(testDbRoot, dbName+".ver")
|
|
|
|
db.Sync()
|
|
|
|
db.Close()
|
|
|
|
os.RemoveAll(dbPath)
|
|
|
|
os.Remove(dbVersionPath)
|
|
|
|
os.RemoveAll(testDbRoot)
|
|
|
|
}
|
2013-10-10 01:29:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Insert the main network genesis block. This is part of the initial
|
|
|
|
// database setup.
|
2015-02-06 06:18:27 +01:00
|
|
|
genesisBlock := btcutil.NewBlock(chaincfg.MainNetParams.GenesisBlock)
|
2013-11-08 08:52:31 +01:00
|
|
|
_, err := db.InsertBlock(genesisBlock)
|
2013-10-10 01:29:53 +02:00
|
|
|
if err != nil {
|
|
|
|
teardown()
|
|
|
|
err := fmt.Errorf("failed to insert genesis block: %v", err)
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2015-02-06 06:18:27 +01:00
|
|
|
chain := blockchain.New(db, &chaincfg.MainNetParams, nil)
|
2013-10-10 01:29:53 +02:00
|
|
|
return chain, teardown, nil
|
|
|
|
}
|
2015-01-08 12:08:11 +01:00
|
|
|
|
|
|
|
// loadTxStore returns a transaction store loaded from a file.
|
2015-01-30 21:54:30 +01:00
|
|
|
func loadTxStore(filename string) (blockchain.TxStore, error) {
|
2015-01-08 12:08:11 +01:00
|
|
|
// The txstore file format is:
|
|
|
|
// <num tx data entries> <tx length> <serialized tx> <blk height>
|
|
|
|
// <num spent bits> <spent bits>
|
|
|
|
//
|
|
|
|
// All num and length fields are little-endian uint32s. The spent bits
|
|
|
|
// field is padded to a byte boundary.
|
|
|
|
|
|
|
|
filename = filepath.Join("testdata/", filename)
|
|
|
|
fi, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Choose read based on whether the file is compressed or not.
|
|
|
|
var r io.Reader
|
|
|
|
if strings.HasSuffix(filename, ".bz2") {
|
|
|
|
r = bzip2.NewReader(fi)
|
|
|
|
} else {
|
|
|
|
r = fi
|
|
|
|
}
|
|
|
|
defer fi.Close()
|
|
|
|
|
|
|
|
// Num of transaction store objects.
|
|
|
|
var numItems uint32
|
|
|
|
if err := binary.Read(r, binary.LittleEndian, &numItems); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-01-30 21:54:30 +01:00
|
|
|
txStore := make(blockchain.TxStore)
|
2015-01-08 12:08:11 +01:00
|
|
|
var uintBuf uint32
|
|
|
|
for height := uint32(0); height < numItems; height++ {
|
2015-01-30 21:54:30 +01:00
|
|
|
txD := blockchain.TxData{}
|
2015-01-08 12:08:11 +01:00
|
|
|
|
|
|
|
// Serialized transaction length.
|
|
|
|
err = binary.Read(r, binary.LittleEndian, &uintBuf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
serializedTxLen := uintBuf
|
2015-02-05 22:16:39 +01:00
|
|
|
if serializedTxLen > wire.MaxBlockPayload {
|
2015-01-08 12:08:11 +01:00
|
|
|
return nil, fmt.Errorf("Read serialized transaction "+
|
|
|
|
"length of %d is larger max allowed %d",
|
2015-02-05 22:16:39 +01:00
|
|
|
serializedTxLen, wire.MaxBlockPayload)
|
2015-01-08 12:08:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Transaction.
|
2015-02-05 22:16:39 +01:00
|
|
|
var msgTx wire.MsgTx
|
2015-01-08 12:08:11 +01:00
|
|
|
err = msgTx.Deserialize(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
txD.Tx = btcutil.NewTx(&msgTx)
|
|
|
|
|
|
|
|
// Transaction hash.
|
2015-04-17 08:09:21 +02:00
|
|
|
txHash := msgTx.TxSha()
|
2015-01-08 12:08:11 +01:00
|
|
|
txD.Hash = &txHash
|
|
|
|
|
|
|
|
// Block height the transaction came from.
|
|
|
|
err = binary.Read(r, binary.LittleEndian, &uintBuf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
txD.BlockHeight = int64(uintBuf)
|
|
|
|
|
|
|
|
// Num spent bits.
|
|
|
|
err = binary.Read(r, binary.LittleEndian, &uintBuf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
numSpentBits := uintBuf
|
|
|
|
numSpentBytes := numSpentBits / 8
|
|
|
|
if numSpentBits%8 != 0 {
|
|
|
|
numSpentBytes++
|
|
|
|
}
|
|
|
|
|
|
|
|
// Packed spent bytes.
|
|
|
|
spentBytes := make([]byte, numSpentBytes)
|
|
|
|
_, err = io.ReadFull(r, spentBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate spent data based on spent bits.
|
|
|
|
txD.Spent = make([]bool, numSpentBits)
|
|
|
|
for byteNum, spentByte := range spentBytes {
|
|
|
|
for bit := 0; bit < 8; bit++ {
|
|
|
|
if uint32((byteNum*8)+bit) < numSpentBits {
|
|
|
|
if spentByte&(1<<uint(bit)) != 0 {
|
|
|
|
txD.Spent[(byteNum*8)+bit] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
txStore[*txD.Hash] = &txD
|
|
|
|
}
|
|
|
|
|
|
|
|
return txStore, nil
|
|
|
|
}
|