2014-01-09 06:54:52 +01:00
|
|
|
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
2013-08-03 17:20:05 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package ldb_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"compress/bzip2"
|
|
|
|
"encoding/binary"
|
2013-08-22 17:40:59 +02:00
|
|
|
"fmt"
|
2013-08-03 17:20:05 +02:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2014-07-03 02:47:24 +02:00
|
|
|
|
2015-01-17 00:27:16 +01:00
|
|
|
"github.com/btcsuite/btcnet"
|
2015-01-15 17:33:06 +01:00
|
|
|
"github.com/btcsuite/btcutil"
|
2015-01-16 20:59:18 +01:00
|
|
|
"github.com/btcsuite/btcwire"
|
2014-07-03 02:47:24 +02:00
|
|
|
"github.com/conformal/btcdb"
|
2013-08-03 17:20:05 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var network = btcwire.MainNet
|
|
|
|
|
|
|
|
func TestOperational(t *testing.T) {
|
2014-01-20 00:34:54 +01:00
|
|
|
testOperationalMode(t)
|
2013-08-03 17:20:05 +02:00
|
|
|
}
|
|
|
|
|
2014-01-20 00:34:54 +01:00
|
|
|
func testOperationalMode(t *testing.T) {
|
2013-08-03 17:20:05 +02:00
|
|
|
// simplified basic operation is:
|
|
|
|
// 1) fetch block from remote server
|
|
|
|
// 2) look up all txin (except coinbase in db)
|
|
|
|
// 3) insert block
|
|
|
|
|
|
|
|
// Ignore db remove errors since it means we didn't have an old one.
|
2014-01-20 00:34:54 +01:00
|
|
|
dbname := fmt.Sprintf("tstdbop1")
|
2013-09-25 22:18:35 +02:00
|
|
|
dbnamever := dbname + ".ver"
|
2013-08-03 17:20:05 +02:00
|
|
|
_ = os.RemoveAll(dbname)
|
2013-09-25 22:18:35 +02:00
|
|
|
_ = os.RemoveAll(dbnamever)
|
2013-08-03 17:20:05 +02:00
|
|
|
db, err := btcdb.CreateDB("leveldb", dbname)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to open test database %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dbname)
|
2013-09-25 22:18:35 +02:00
|
|
|
defer os.RemoveAll(dbnamever)
|
2014-07-07 16:50:50 +02:00
|
|
|
defer func() {
|
|
|
|
if err := db.Close(); err != nil {
|
|
|
|
t.Errorf("Close: unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2013-08-03 17:20:05 +02:00
|
|
|
|
2013-10-12 08:48:27 +02:00
|
|
|
testdatafile := filepath.Join("..", "testdata", "blocks1-256.bz2")
|
2013-08-03 17:20:05 +02:00
|
|
|
blocks, err := loadBlocks(t, testdatafile)
|
|
|
|
if err != nil {
|
2014-01-20 00:34:54 +01:00
|
|
|
t.Errorf("Unable to load blocks from test data: %v", err)
|
2013-08-03 17:20:05 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = nil
|
|
|
|
out:
|
|
|
|
for height := int64(0); height < int64(len(blocks)); height++ {
|
|
|
|
block := blocks[height]
|
2014-01-20 00:34:54 +01:00
|
|
|
mblock := block.MsgBlock()
|
|
|
|
var txneededList []*btcwire.ShaHash
|
|
|
|
for _, tx := range mblock.Transactions {
|
|
|
|
for _, txin := range tx.TxIn {
|
2014-10-01 14:52:19 +02:00
|
|
|
if txin.PreviousOutPoint.Index == uint32(4294967295) {
|
2014-01-20 00:34:54 +01:00
|
|
|
continue
|
2013-08-03 17:20:05 +02:00
|
|
|
}
|
2014-10-01 14:52:19 +02:00
|
|
|
origintxsha := &txin.PreviousOutPoint.Hash
|
2014-01-20 00:34:54 +01:00
|
|
|
txneededList = append(txneededList, origintxsha)
|
|
|
|
|
2014-07-07 16:50:50 +02:00
|
|
|
exists, err := db.ExistsTxSha(origintxsha)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("ExistsTxSha: unexpected error %v ", err)
|
|
|
|
}
|
|
|
|
if !exists {
|
2014-01-20 00:34:54 +01:00
|
|
|
t.Errorf("referenced tx not found %v ", origintxsha)
|
|
|
|
}
|
2014-07-07 16:50:50 +02:00
|
|
|
|
2014-01-20 00:34:54 +01:00
|
|
|
_, err = db.FetchTxBySha(origintxsha)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("referenced tx not found %v err %v ", origintxsha, err)
|
2013-08-03 17:20:05 +02:00
|
|
|
}
|
|
|
|
}
|
2014-01-20 00:34:54 +01:00
|
|
|
}
|
|
|
|
txlist := db.FetchUnSpentTxByShaList(txneededList)
|
|
|
|
for _, txe := range txlist {
|
|
|
|
if txe.Err != nil {
|
|
|
|
t.Errorf("tx list fetch failed %v err %v ", txe.Sha, txe.Err)
|
|
|
|
break out
|
|
|
|
}
|
2013-08-03 17:20:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
newheight, err := db.InsertBlock(block)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to insert block %v err %v", height, err)
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
if newheight != height {
|
|
|
|
t.Errorf("height mismatch expect %v returned %v", height, newheight)
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
|
|
|
|
newSha, blkid, err := db.NewestSha()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to obtain latest sha %v %v", height, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if blkid != height {
|
2013-09-13 17:49:30 +02:00
|
|
|
t.Errorf("height doe not match latest block height %v %v %v", blkid, height, err)
|
2013-08-03 17:20:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
blkSha, _ := block.Sha()
|
|
|
|
if *newSha != *blkSha {
|
2013-09-13 17:49:30 +02:00
|
|
|
t.Errorf("Newest block sha does not match freshly inserted one %v %v %v ", newSha, blkSha, err)
|
2013-08-03 17:20:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// now that db is populated, do some additional test
|
|
|
|
testFetchRangeHeight(t, db, blocks)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackout(t *testing.T) {
|
2014-01-20 00:34:54 +01:00
|
|
|
testBackout(t)
|
2013-08-03 17:20:05 +02:00
|
|
|
}
|
|
|
|
|
2014-01-20 00:34:54 +01:00
|
|
|
func testBackout(t *testing.T) {
|
2013-08-03 17:20:05 +02:00
|
|
|
// simplified basic operation is:
|
|
|
|
// 1) fetch block from remote server
|
|
|
|
// 2) look up all txin (except coinbase in db)
|
|
|
|
// 3) insert block
|
|
|
|
|
|
|
|
// Ignore db remove errors since it means we didn't have an old one.
|
2014-01-20 00:34:54 +01:00
|
|
|
dbname := fmt.Sprintf("tstdbop2")
|
2013-09-25 22:18:35 +02:00
|
|
|
dbnamever := dbname + ".ver"
|
2013-08-03 17:20:05 +02:00
|
|
|
_ = os.RemoveAll(dbname)
|
2013-09-25 22:18:35 +02:00
|
|
|
_ = os.RemoveAll(dbnamever)
|
2013-08-03 17:20:05 +02:00
|
|
|
db, err := btcdb.CreateDB("leveldb", dbname)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to open test database %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dbname)
|
2013-09-25 22:18:35 +02:00
|
|
|
defer os.RemoveAll(dbnamever)
|
2013-08-03 17:20:05 +02:00
|
|
|
defer db.Close()
|
|
|
|
|
2013-10-12 08:48:27 +02:00
|
|
|
testdatafile := filepath.Join("..", "testdata", "blocks1-256.bz2")
|
2013-08-03 17:20:05 +02:00
|
|
|
blocks, err := loadBlocks(t, testdatafile)
|
|
|
|
if len(blocks) < 120 {
|
|
|
|
t.Errorf("test data too small")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = nil
|
|
|
|
for height := int64(0); height < int64(len(blocks)); height++ {
|
|
|
|
if height == 100 {
|
|
|
|
t.Logf("Syncing at block height 100")
|
|
|
|
db.Sync()
|
|
|
|
}
|
|
|
|
if height == 120 {
|
|
|
|
t.Logf("Simulating unexpected application quit")
|
|
|
|
// Simulate unexpected application quit
|
|
|
|
db.RollbackClose()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
block := blocks[height]
|
|
|
|
|
|
|
|
newheight, err := db.InsertBlock(block)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to insert block %v err %v", height, err)
|
2013-08-22 17:40:59 +02:00
|
|
|
return
|
2013-08-03 17:20:05 +02:00
|
|
|
}
|
|
|
|
if newheight != height {
|
|
|
|
t.Errorf("height mismatch expect %v returned %v", height, newheight)
|
2013-08-22 17:40:59 +02:00
|
|
|
return
|
2013-08-03 17:20:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// db was closed at height 120, so no cleanup is possible.
|
|
|
|
|
|
|
|
// reopen db
|
|
|
|
db, err = btcdb.OpenDB("leveldb", dbname)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to open test database %v", err)
|
|
|
|
return
|
|
|
|
}
|
2014-07-07 16:50:50 +02:00
|
|
|
defer func() {
|
|
|
|
if err := db.Close(); err != nil {
|
|
|
|
t.Errorf("Close: unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2013-08-03 17:20:05 +02:00
|
|
|
|
|
|
|
sha, err := blocks[99].Sha()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to get block 99 sha err %v", err)
|
|
|
|
return
|
|
|
|
}
|
2014-07-07 16:50:50 +02:00
|
|
|
if _, err := db.ExistsSha(sha); err != nil {
|
2014-12-23 03:05:11 +01:00
|
|
|
t.Errorf("ExistsSha: unexpected error: %v", err)
|
2014-07-07 16:50:50 +02:00
|
|
|
}
|
2013-08-03 17:20:05 +02:00
|
|
|
_, err = db.FetchBlockBySha(sha)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to load block 99 from db %v", err)
|
2013-08-22 17:40:59 +02:00
|
|
|
return
|
2013-08-03 17:20:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sha, err = blocks[119].Sha()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to get block 110 sha err %v", err)
|
|
|
|
return
|
|
|
|
}
|
2014-07-07 16:50:50 +02:00
|
|
|
if _, err := db.ExistsSha(sha); err != nil {
|
2014-12-23 03:05:11 +01:00
|
|
|
t.Errorf("ExistsSha: unexpected error: %v", err)
|
2014-07-07 16:50:50 +02:00
|
|
|
}
|
2013-08-03 17:20:05 +02:00
|
|
|
_, err = db.FetchBlockBySha(sha)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("loaded block 119 from db")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
block := blocks[119]
|
|
|
|
mblock := block.MsgBlock()
|
|
|
|
txsha, err := mblock.Transactions[0].TxSha()
|
2014-07-07 16:50:50 +02:00
|
|
|
exists, err := db.ExistsTxSha(&txsha)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("ExistsTxSha: unexpected error %v ", err)
|
|
|
|
}
|
2013-08-03 17:20:05 +02:00
|
|
|
if !exists {
|
|
|
|
t.Errorf("tx %v not located db\n", txsha)
|
|
|
|
}
|
|
|
|
|
2013-10-03 21:21:45 +02:00
|
|
|
_, err = db.FetchTxBySha(&txsha)
|
2013-08-03 17:20:05 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("tx %v not located db\n", txsha)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-22 17:40:59 +02:00
|
|
|
var savedblocks []*btcutil.Block
|
|
|
|
|
2013-08-03 17:20:05 +02:00
|
|
|
func loadBlocks(t *testing.T, file string) (blocks []*btcutil.Block, err error) {
|
2013-08-22 17:40:59 +02:00
|
|
|
if len(savedblocks) != 0 {
|
|
|
|
blocks = savedblocks
|
|
|
|
return
|
|
|
|
}
|
2013-10-12 08:48:27 +02:00
|
|
|
testdatafile := filepath.Join("..", "testdata", "blocks1-256.bz2")
|
2013-08-03 17:20:05 +02:00
|
|
|
var dr io.Reader
|
|
|
|
var fi io.ReadCloser
|
|
|
|
fi, err = os.Open(testdatafile)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to open file %v, err %v", testdatafile, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if strings.HasSuffix(testdatafile, ".bz2") {
|
|
|
|
z := bzip2.NewReader(fi)
|
|
|
|
dr = z
|
|
|
|
} else {
|
|
|
|
dr = fi
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := fi.Close(); err != nil {
|
|
|
|
t.Errorf("failed to close file %v %v", testdatafile, err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Set the first block as the genesis block.
|
2014-05-28 08:29:51 +02:00
|
|
|
genesis := btcutil.NewBlock(btcnet.MainNetParams.GenesisBlock)
|
2013-08-03 17:20:05 +02:00
|
|
|
blocks = append(blocks, genesis)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
t.Errorf("failed to load network type, err %v", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if rintbuf != uint32(network) {
|
|
|
|
t.Errorf("Block doesn't match network: %v expects %v",
|
|
|
|
rintbuf, 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 {
|
|
|
|
t.Errorf("failed to parse block %v", height)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
blocks = append(blocks, block)
|
|
|
|
}
|
2013-08-22 17:40:59 +02:00
|
|
|
savedblocks = blocks
|
2013-08-03 17:20:05 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func testFetchRangeHeight(t *testing.T, db btcdb.Db, blocks []*btcutil.Block) {
|
|
|
|
|
|
|
|
var testincrement int64 = 50
|
|
|
|
var testcnt int64 = 100
|
|
|
|
|
|
|
|
shanames := make([]*btcwire.ShaHash, len(blocks))
|
|
|
|
|
|
|
|
nBlocks := int64(len(blocks))
|
|
|
|
|
|
|
|
for i := range blocks {
|
|
|
|
blockSha, err := blocks[i].Sha()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("FetchRangeHeight: unexpected failure computing block sah %v", err)
|
|
|
|
}
|
|
|
|
shanames[i] = blockSha
|
|
|
|
}
|
|
|
|
|
|
|
|
for startheight := int64(0); startheight < nBlocks; startheight += testincrement {
|
|
|
|
endheight := startheight + testcnt
|
|
|
|
|
|
|
|
if endheight > nBlocks {
|
|
|
|
endheight = btcdb.AllShas
|
|
|
|
}
|
|
|
|
|
|
|
|
shalist, err := db.FetchHeightRange(startheight, endheight)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("FetchRangeHeight: unexpected failure looking up shas %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if endheight == btcdb.AllShas {
|
|
|
|
if int64(len(shalist)) != nBlocks-startheight {
|
|
|
|
t.Errorf("FetchRangeHeight: expected A %v shas, got %v", nBlocks-startheight, len(shalist))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if int64(len(shalist)) != testcnt {
|
|
|
|
t.Errorf("FetchRangeHeight: expected %v shas, got %v", testcnt, len(shalist))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range shalist {
|
|
|
|
sha0 := *shanames[int64(i)+startheight]
|
|
|
|
sha1 := shalist[i]
|
|
|
|
if sha0 != sha1 {
|
|
|
|
t.Errorf("FetchRangeHeight: mismatch sha at %v requested range %v %v: %v %v ", int64(i)+startheight, startheight, endheight, sha0, sha1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|