2015-08-26 06:03:18 +02:00
|
|
|
// Copyright (c) 2013-2016 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"
|
2016-08-08 21:04:33 +02:00
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
2015-08-26 11:54:55 +02:00
|
|
|
"github.com/btcsuite/btcd/database"
|
|
|
|
_ "github.com/btcsuite/btcd/database/ffldb"
|
2016-09-14 01:11:12 +02:00
|
|
|
"github.com/btcsuite/btcd/txscript"
|
2015-02-05 22:16:39 +01:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2013-10-10 01:29:53 +02:00
|
|
|
)
|
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
const (
|
|
|
|
// testDbType is the database backend type to use for the tests.
|
|
|
|
testDbType = "ffldb"
|
2013-11-08 08:52:31 +01:00
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
// testDbRoot is the root directory used to create all test databases.
|
|
|
|
testDbRoot = "testdbs"
|
|
|
|
|
|
|
|
// blockDataNet is the expected network in the test block data.
|
|
|
|
blockDataNet = wire.MainNet
|
|
|
|
)
|
2013-10-10 01:29:53 +02:00
|
|
|
|
|
|
|
// 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-08-26 06:03:18 +02:00
|
|
|
supportedDrivers := database.SupportedDrivers()
|
|
|
|
for _, driver := range supportedDrivers {
|
|
|
|
if dbType == driver {
|
2013-11-08 08:52:31 +01:00
|
|
|
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
|
2016-09-14 01:11:12 +02:00
|
|
|
// block already inserted. In addition to the new chain instance, it returns
|
2013-10-10 01:29:53 +02:00
|
|
|
// a teardown function the caller should invoke when done testing to clean up.
|
2016-09-14 01:11:12 +02:00
|
|
|
func chainSetup(dbName string, params *chaincfg.Params) (*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-08-26 06:03:18 +02:00
|
|
|
var db database.DB
|
2013-11-08 08:52:31 +01:00
|
|
|
var teardown func()
|
|
|
|
if testDbType == "memdb" {
|
2015-08-26 06:03:18 +02:00
|
|
|
ndb, err := database.Create(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-08-26 06:03:18 +02:00
|
|
|
ndb, err := database.Create(testDbType, dbPath, blockDataNet)
|
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() {
|
|
|
|
db.Close()
|
|
|
|
os.RemoveAll(dbPath)
|
|
|
|
os.RemoveAll(testDbRoot)
|
|
|
|
}
|
2013-10-10 01:29:53 +02:00
|
|
|
}
|
|
|
|
|
2016-08-10 23:02:23 +02:00
|
|
|
// Copy the chain params to ensure any modifications the tests do to
|
|
|
|
// the chain parameters do not affect the global instance.
|
2016-09-14 01:11:12 +02:00
|
|
|
paramsCopy := *params
|
2016-08-10 23:02:23 +02:00
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
// Create the main chain instance.
|
|
|
|
chain, err := blockchain.New(&blockchain.Config{
|
|
|
|
DB: db,
|
2016-09-14 01:11:12 +02:00
|
|
|
ChainParams: ¶msCopy,
|
2016-07-14 02:36:36 +02:00
|
|
|
TimeSource: blockchain.NewMedianTime(),
|
2016-09-14 01:11:12 +02:00
|
|
|
SigCache: txscript.NewSigCache(1000),
|
2015-08-26 06:03:18 +02:00
|
|
|
})
|
2013-10-10 01:29:53 +02:00
|
|
|
if err != nil {
|
|
|
|
teardown()
|
2015-08-26 06:03:18 +02:00
|
|
|
err := fmt.Errorf("failed to create chain instance: %v", err)
|
2013-10-10 01:29:53 +02:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return chain, teardown, nil
|
|
|
|
}
|
2015-01-08 12:08:11 +01:00
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
// loadUtxoView returns a utxo view loaded from a file.
|
|
|
|
func loadUtxoView(filename string) (*blockchain.UtxoViewpoint, error) {
|
|
|
|
// The utxostore file format is:
|
|
|
|
// <tx hash><serialized utxo len><serialized utxo>
|
2015-01-08 12:08:11 +01:00
|
|
|
//
|
2015-08-26 06:03:18 +02:00
|
|
|
// The serialized utxo len is a little endian uint32 and the serialized
|
|
|
|
// utxo uses the format described in chainio.go.
|
2015-01-08 12:08:11 +01:00
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
filename = filepath.Join("testdata", filename)
|
2015-01-08 12:08:11 +01:00
|
|
|
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()
|
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
view := blockchain.NewUtxoViewpoint()
|
|
|
|
for {
|
|
|
|
// Hash of the utxo entry.
|
2016-08-08 21:04:33 +02:00
|
|
|
var hash chainhash.Hash
|
2015-08-26 06:03:18 +02:00
|
|
|
_, err := io.ReadAtLeast(r, hash[:], len(hash[:]))
|
2015-01-08 12:08:11 +01:00
|
|
|
if err != nil {
|
2015-08-26 06:03:18 +02:00
|
|
|
// Expected EOF at the right offset.
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
2015-01-08 12:08:11 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
// Num of serialize utxo entry bytes.
|
|
|
|
var numBytes uint32
|
|
|
|
err = binary.Read(r, binary.LittleEndian, &numBytes)
|
2015-01-08 12:08:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
// Serialized utxo entry.
|
|
|
|
serialized := make([]byte, numBytes)
|
|
|
|
_, err = io.ReadAtLeast(r, serialized, int(numBytes))
|
2015-01-08 12:08:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
// Deserialize it and add it to the view.
|
|
|
|
utxoEntry, err := blockchain.TstDeserializeUtxoEntry(serialized)
|
2015-01-08 12:08:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-08-26 06:03:18 +02:00
|
|
|
view.Entries()[hash] = utxoEntry
|
2015-01-08 12:08:11 +01:00
|
|
|
}
|
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
return view, nil
|
2015-01-08 12:08:11 +01:00
|
|
|
}
|