2014-01-09 06:52:54 +01:00
|
|
|
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
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.
|
|
|
|
|
|
|
|
package btcchain_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-07-02 18:04:59 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2013-10-10 01:29:53 +02:00
|
|
|
"github.com/conformal/btcchain"
|
|
|
|
"github.com/conformal/btcdb"
|
|
|
|
_ "github.com/conformal/btcdb/ldb"
|
2013-11-08 08:52:31 +01:00
|
|
|
_ "github.com/conformal/btcdb/memdb"
|
2014-05-26 17:27:50 +02:00
|
|
|
"github.com/conformal/btcnet"
|
2013-10-10 01:29:53 +02:00
|
|
|
"github.com/conformal/btcutil"
|
|
|
|
)
|
|
|
|
|
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 {
|
|
|
|
supportedDBs := btcdb.SupportedDBs()
|
|
|
|
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.
|
|
|
|
func chainSetup(dbName string) (*btcchain.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.
|
|
|
|
var db btcdb.Db
|
|
|
|
var teardown func()
|
|
|
|
if testDbType == "memdb" {
|
2014-01-20 03:13:53 +01:00
|
|
|
ndb, err := btcdb.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)
|
|
|
|
ndb, err := btcdb.CreateDB(testDbType, dbPath)
|
|
|
|
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.
|
2014-05-28 07:56:57 +02:00
|
|
|
genesisBlock := btcutil.NewBlock(btcnet.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
|
|
|
|
}
|
|
|
|
|
2014-05-27 17:15:35 +02:00
|
|
|
chain := btcchain.New(db, &btcnet.MainNetParams, nil)
|
2013-10-10 01:29:53 +02:00
|
|
|
return chain, teardown, nil
|
|
|
|
}
|