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 (
|
2014-12-25 00:55:14 +01:00
|
|
|
"bytes"
|
2013-08-03 17:20:05 +02:00
|
|
|
"compress/bzip2"
|
|
|
|
"encoding/binary"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2014-07-03 02:47:24 +02:00
|
|
|
|
2015-01-27 18:45:10 +01:00
|
|
|
"github.com/btcsuite/btcd/database"
|
2015-01-30 19:14:33 +01:00
|
|
|
"github.com/btcsuite/btcd/txscript"
|
2015-02-05 22:16:39 +01:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2015-01-17 00:27:16 +01:00
|
|
|
"github.com/btcsuite/btcnet"
|
2015-01-15 17:33:06 +01:00
|
|
|
"github.com/btcsuite/btcutil"
|
2014-12-25 00:55:14 +01:00
|
|
|
"golang.org/x/crypto/ripemd160"
|
2013-08-03 17:20:05 +02:00
|
|
|
)
|
|
|
|
|
2015-02-05 22:16:39 +01:00
|
|
|
var network = wire.MainNet
|
2013-08-03 17:20:05 +02:00
|
|
|
|
2014-12-25 00:55:14 +01:00
|
|
|
// testDb is used to store db related context for a running test.
|
|
|
|
// the `cleanUpFunc` *must* be called after each test to maintain db
|
|
|
|
// consistency across tests.
|
|
|
|
type testDb struct {
|
2015-01-27 18:45:10 +01:00
|
|
|
db database.Db
|
2014-12-25 00:55:14 +01:00
|
|
|
blocks []*btcutil.Block
|
|
|
|
dbName string
|
|
|
|
dbNameVer string
|
|
|
|
cleanUpFunc func()
|
2013-08-03 17:20:05 +02:00
|
|
|
}
|
|
|
|
|
2015-01-27 22:38:23 +01:00
|
|
|
func setUpTestDb(t *testing.T, dbname string) (*testDb, error) {
|
2013-08-03 17:20:05 +02:00
|
|
|
// Ignore db remove errors since it means we didn't have an old one.
|
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)
|
2015-01-27 18:45:10 +01:00
|
|
|
db, err := database.CreateDB("leveldb", dbname)
|
2013-08-03 17:20:05 +02:00
|
|
|
if err != nil {
|
2014-12-25 00:55:14 +01:00
|
|
|
return nil, 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)
|
2014-12-25 00:55:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanUp := func() {
|
|
|
|
db.Close()
|
|
|
|
os.RemoveAll(dbname)
|
|
|
|
os.RemoveAll(dbnamever)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &testDb{
|
|
|
|
db: db,
|
|
|
|
blocks: blocks,
|
|
|
|
dbName: dbname,
|
|
|
|
dbNameVer: dbnamever,
|
|
|
|
cleanUpFunc: cleanUp,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOperational(t *testing.T) {
|
|
|
|
testOperationalMode(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// testAddrIndexOperations ensures that all normal operations concerning
|
|
|
|
// the optional address index function correctly.
|
2015-02-05 22:16:39 +01:00
|
|
|
func testAddrIndexOperations(t *testing.T, db database.Db, newestBlock *btcutil.Block, newestSha *wire.ShaHash, newestBlockIdx int64) {
|
2014-12-25 00:55:14 +01:00
|
|
|
// Metadata about the current addr index state should be unset.
|
|
|
|
sha, height, err := db.FetchAddrIndexTip()
|
2015-01-27 18:45:10 +01:00
|
|
|
if err != database.ErrAddrIndexDoesNotExist {
|
2014-12-25 00:55:14 +01:00
|
|
|
t.Fatalf("Address index metadata shouldn't be in db, hasn't been built up yet.")
|
|
|
|
}
|
|
|
|
|
2015-02-05 22:16:39 +01:00
|
|
|
var zeroHash wire.ShaHash
|
2014-12-25 00:55:14 +01:00
|
|
|
if !sha.IsEqual(&zeroHash) {
|
|
|
|
t.Fatalf("AddrIndexTip wrong hash got: %s, want %s", sha, &zeroHash)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if height != -1 {
|
|
|
|
t.Fatalf("Addrindex not built up, yet a block index tip has been set to: %d.", height)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test enforcement of constraints for "limit" and "skip"
|
|
|
|
var fakeAddr btcutil.Address
|
|
|
|
_, err = db.FetchTxsForAddr(fakeAddr, -1, 0)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Negative value for skip passed, should return an error")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = db.FetchTxsForAddr(fakeAddr, 0, -1)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Negative value for limit passed, should return an error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Simple test to index outputs(s) of the first tx.
|
2015-01-27 18:45:10 +01:00
|
|
|
testIndex := make(database.BlockAddrIndex)
|
2014-12-25 00:55:14 +01:00
|
|
|
testTx, err := newestBlock.Tx(0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Block has no transactions, unable to test addr "+
|
|
|
|
"indexing, err %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract the dest addr from the tx.
|
2015-01-30 19:14:33 +01:00
|
|
|
_, testAddrs, _, err := txscript.ExtractPkScriptAddrs(testTx.MsgTx().TxOut[0].PkScript, &btcnet.MainNetParams)
|
2014-12-25 00:55:14 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to decode tx output, err %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract the hash160 from the output script.
|
|
|
|
var hash160Bytes [ripemd160.Size]byte
|
|
|
|
testHash160 := testAddrs[0].(*btcutil.AddressPubKey).AddressPubKeyHash().ScriptAddress()
|
|
|
|
copy(hash160Bytes[:], testHash160[:])
|
|
|
|
|
|
|
|
// Create a fake index.
|
|
|
|
blktxLoc, _ := newestBlock.TxLoc()
|
2015-02-05 22:16:39 +01:00
|
|
|
testIndex[hash160Bytes] = []*wire.TxLoc{&blktxLoc[0]}
|
2014-12-25 00:55:14 +01:00
|
|
|
|
|
|
|
// Insert our test addr index into the DB.
|
|
|
|
err = db.UpdateAddrIndexForBlock(newestSha, newestBlockIdx, testIndex)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("UpdateAddrIndexForBlock: failed to index"+
|
|
|
|
" addrs for block #%d (%s) "+
|
|
|
|
"err %v", newestBlockIdx, newestSha, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chain Tip of address should've been updated.
|
|
|
|
assertAddrIndexTipIsUpdated(db, t, newestSha, newestBlockIdx)
|
|
|
|
|
|
|
|
// Check index retrieval.
|
|
|
|
txReplies, err := db.FetchTxsForAddr(testAddrs[0], 0, 1000)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("FetchTxsForAddr failed to correctly fetch txs for an "+
|
|
|
|
"address, err %v", err)
|
|
|
|
}
|
|
|
|
// Should have one reply.
|
|
|
|
if len(txReplies) != 1 {
|
|
|
|
t.Fatalf("Failed to properly index tx by address.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Our test tx and indexed tx should have the same sha.
|
|
|
|
indexedTx := txReplies[0]
|
|
|
|
if !bytes.Equal(indexedTx.Sha.Bytes(), testTx.Sha().Bytes()) {
|
|
|
|
t.Fatalf("Failed to fetch proper indexed tx. Expected sha %v, "+
|
|
|
|
"fetched %v", testTx.Sha(), indexedTx.Sha)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shut down DB.
|
|
|
|
db.Sync()
|
|
|
|
db.Close()
|
|
|
|
|
|
|
|
// Re-Open, tip still should be updated to current height and sha.
|
2015-01-27 22:38:23 +01:00
|
|
|
db, err = database.OpenDB("leveldb", "tstdbopmode")
|
2014-12-25 00:55:14 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to re-open created db, err %v", err)
|
|
|
|
}
|
|
|
|
assertAddrIndexTipIsUpdated(db, t, newestSha, newestBlockIdx)
|
|
|
|
|
|
|
|
// Delete the entire index.
|
|
|
|
err = db.DeleteAddrIndex()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Couldn't delete address index, err %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Former index should no longer exist.
|
|
|
|
txReplies, err = db.FetchTxsForAddr(testAddrs[0], 0, 1000)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to fetch transactions for address: %v", err)
|
|
|
|
}
|
|
|
|
if len(txReplies) != 0 {
|
|
|
|
t.Fatalf("Address index was not successfully deleted. "+
|
|
|
|
"Should have 0 tx's indexed, %v were returned.",
|
|
|
|
len(txReplies))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tip should be blanked out.
|
2015-01-27 18:45:10 +01:00
|
|
|
if _, _, err := db.FetchAddrIndexTip(); err != database.ErrAddrIndexDoesNotExist {
|
2014-12-25 00:55:14 +01:00
|
|
|
t.Fatalf("Address index was not fully deleted.")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-02-05 22:16:39 +01:00
|
|
|
func assertAddrIndexTipIsUpdated(db database.Db, t *testing.T, newestSha *wire.ShaHash, newestBlockIdx int64) {
|
2014-12-25 00:55:14 +01:00
|
|
|
// Safe to ignore error, since height will be < 0 in "error" case.
|
|
|
|
sha, height, _ := db.FetchAddrIndexTip()
|
|
|
|
if newestBlockIdx != height {
|
|
|
|
t.Fatalf("Height of address index tip failed to update, "+
|
|
|
|
"expected %v, got %v", newestBlockIdx, height)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(newestSha.Bytes(), sha.Bytes()) {
|
|
|
|
t.Fatalf("Sha of address index tip failed to update, "+
|
|
|
|
"expected %v, got %v", newestSha, sha)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testOperationalMode(t *testing.T) {
|
|
|
|
// simplified basic operation is:
|
|
|
|
// 1) fetch block from remote server
|
|
|
|
// 2) look up all txin (except coinbase in db)
|
|
|
|
// 3) insert block
|
|
|
|
// 4) exercise the optional addridex
|
2015-01-27 22:38:23 +01:00
|
|
|
testDb, err := setUpTestDb(t, "tstdbopmode")
|
2013-08-03 17:20:05 +02:00
|
|
|
if err != nil {
|
2015-01-27 22:38:23 +01:00
|
|
|
t.Errorf("Failed to open test database %v", err)
|
2013-08-03 17:20:05 +02:00
|
|
|
return
|
|
|
|
}
|
2015-01-27 22:38:23 +01:00
|
|
|
defer testDb.cleanUpFunc()
|
2013-08-03 17:20:05 +02:00
|
|
|
err = nil
|
|
|
|
out:
|
2014-12-25 00:55:14 +01:00
|
|
|
for height := int64(0); height < int64(len(testDb.blocks)); height++ {
|
|
|
|
block := testDb.blocks[height]
|
2014-01-20 00:34:54 +01:00
|
|
|
mblock := block.MsgBlock()
|
2015-02-05 22:16:39 +01:00
|
|
|
var txneededList []*wire.ShaHash
|
2014-01-20 00:34:54 +01:00
|
|
|
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-12-25 00:55:14 +01:00
|
|
|
exists, err := testDb.db.ExistsTxSha(origintxsha)
|
2014-07-07 16:50:50 +02:00
|
|
|
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-12-25 00:55:14 +01:00
|
|
|
_, err = testDb.db.FetchTxBySha(origintxsha)
|
2014-01-20 00:34:54 +01:00
|
|
|
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
|
|
|
}
|
2014-12-25 00:55:14 +01:00
|
|
|
txlist := testDb.db.FetchUnSpentTxByShaList(txneededList)
|
2014-01-20 00:34:54 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-12-25 00:55:14 +01:00
|
|
|
newheight, err := testDb.db.InsertBlock(block)
|
2013-08-03 17:20:05 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-12-25 00:55:14 +01:00
|
|
|
newSha, blkid, err := testDb.db.NewestSha()
|
2013-08-03 17:20:05 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to obtain latest sha %v %v", height, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if blkid != height {
|
2014-12-25 00:55:14 +01:00
|
|
|
t.Errorf("height does 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-25 00:55:14 +01:00
|
|
|
// now that the db is populated, do some additional tests
|
|
|
|
testFetchHeightRange(t, testDb.db, testDb.blocks)
|
|
|
|
|
|
|
|
// Ensure all operations dealing with the optional address index behave
|
|
|
|
// correctly.
|
|
|
|
newSha, blkid, err := testDb.db.NewestSha()
|
|
|
|
testAddrIndexOperations(t, testDb.db, testDb.blocks[len(testDb.blocks)-1], newSha, blkid)
|
2013-08-03 17:20:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2015-01-27 22:38:23 +01:00
|
|
|
testDb, err := setUpTestDb(t, "tstdbbackout")
|
2013-08-03 17:20:05 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to open test database %v", err)
|
|
|
|
return
|
|
|
|
}
|
2015-01-27 22:38:23 +01:00
|
|
|
defer testDb.cleanUpFunc()
|
2013-08-03 17:20:05 +02:00
|
|
|
|
2014-12-25 00:55:14 +01:00
|
|
|
if len(testDb.blocks) < 120 {
|
2013-08-03 17:20:05 +02:00
|
|
|
t.Errorf("test data too small")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = nil
|
2014-12-25 00:55:14 +01:00
|
|
|
for height := int64(0); height < int64(len(testDb.blocks)); height++ {
|
2013-08-03 17:20:05 +02:00
|
|
|
if height == 100 {
|
|
|
|
t.Logf("Syncing at block height 100")
|
2014-12-25 00:55:14 +01:00
|
|
|
testDb.db.Sync()
|
2013-08-03 17:20:05 +02:00
|
|
|
}
|
|
|
|
if height == 120 {
|
|
|
|
t.Logf("Simulating unexpected application quit")
|
|
|
|
// Simulate unexpected application quit
|
2014-12-25 00:55:14 +01:00
|
|
|
testDb.db.RollbackClose()
|
2013-08-03 17:20:05 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2014-12-25 00:55:14 +01:00
|
|
|
block := testDb.blocks[height]
|
2013-08-03 17:20:05 +02:00
|
|
|
|
2014-12-25 00:55:14 +01:00
|
|
|
newheight, err := testDb.db.InsertBlock(block)
|
2013-08-03 17:20:05 +02:00
|
|
|
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
|
2015-01-27 18:45:10 +01:00
|
|
|
testDb.db, err = database.OpenDB("leveldb", testDb.dbName)
|
2013-08-03 17:20:05 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to open test database %v", err)
|
|
|
|
return
|
|
|
|
}
|
2014-07-07 16:50:50 +02:00
|
|
|
defer func() {
|
2014-12-25 00:55:14 +01:00
|
|
|
if err := testDb.db.Close(); err != nil {
|
2014-07-07 16:50:50 +02:00
|
|
|
t.Errorf("Close: unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2013-08-03 17:20:05 +02:00
|
|
|
|
2014-12-25 00:55:14 +01:00
|
|
|
sha, err := testDb.blocks[99].Sha()
|
2013-08-03 17:20:05 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to get block 99 sha err %v", err)
|
|
|
|
return
|
|
|
|
}
|
2014-12-25 00:55:14 +01:00
|
|
|
if _, err := testDb.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
|
|
|
}
|
2014-12-25 00:55:14 +01:00
|
|
|
_, err = testDb.db.FetchBlockBySha(sha)
|
2013-08-03 17:20:05 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-12-25 00:55:14 +01:00
|
|
|
sha, err = testDb.blocks[119].Sha()
|
2013-08-03 17:20:05 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to get block 110 sha err %v", err)
|
|
|
|
return
|
|
|
|
}
|
2014-12-25 00:55:14 +01:00
|
|
|
if _, err := testDb.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
|
|
|
}
|
2014-12-25 00:55:14 +01:00
|
|
|
_, err = testDb.db.FetchBlockBySha(sha)
|
2013-08-03 17:20:05 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("loaded block 119 from db")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-12-25 00:55:14 +01:00
|
|
|
block := testDb.blocks[119]
|
2013-08-03 17:20:05 +02:00
|
|
|
mblock := block.MsgBlock()
|
|
|
|
txsha, err := mblock.Transactions[0].TxSha()
|
2014-12-25 00:55:14 +01:00
|
|
|
exists, err := testDb.db.ExistsTxSha(&txsha)
|
2014-07-07 16:50:50 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2014-12-25 00:55:14 +01:00
|
|
|
_, err = testDb.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
|
|
|
|
}
|
|
|
|
|
2015-01-27 18:45:10 +01:00
|
|
|
func testFetchHeightRange(t *testing.T, db database.Db, blocks []*btcutil.Block) {
|
2013-08-03 17:20:05 +02:00
|
|
|
|
|
|
|
var testincrement int64 = 50
|
|
|
|
var testcnt int64 = 100
|
|
|
|
|
2015-02-05 22:16:39 +01:00
|
|
|
shanames := make([]*wire.ShaHash, len(blocks))
|
2013-08-03 17:20:05 +02:00
|
|
|
|
|
|
|
nBlocks := int64(len(blocks))
|
|
|
|
|
|
|
|
for i := range blocks {
|
|
|
|
blockSha, err := blocks[i].Sha()
|
|
|
|
if err != nil {
|
2014-12-25 00:55:14 +01:00
|
|
|
t.Errorf("FetchHeightRange: unexpected failure computing block sah %v", err)
|
2013-08-03 17:20:05 +02:00
|
|
|
}
|
|
|
|
shanames[i] = blockSha
|
|
|
|
}
|
|
|
|
|
|
|
|
for startheight := int64(0); startheight < nBlocks; startheight += testincrement {
|
|
|
|
endheight := startheight + testcnt
|
|
|
|
|
|
|
|
if endheight > nBlocks {
|
2015-01-27 18:45:10 +01:00
|
|
|
endheight = database.AllShas
|
2013-08-03 17:20:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
shalist, err := db.FetchHeightRange(startheight, endheight)
|
|
|
|
if err != nil {
|
2014-12-25 00:55:14 +01:00
|
|
|
t.Errorf("FetchHeightRange: unexpected failure looking up shas %v", err)
|
2013-08-03 17:20:05 +02:00
|
|
|
}
|
|
|
|
|
2015-01-27 18:45:10 +01:00
|
|
|
if endheight == database.AllShas {
|
2013-08-03 17:20:05 +02:00
|
|
|
if int64(len(shalist)) != nBlocks-startheight {
|
2014-12-25 00:55:14 +01:00
|
|
|
t.Errorf("FetchHeightRange: expected A %v shas, got %v", nBlocks-startheight, len(shalist))
|
2013-08-03 17:20:05 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if int64(len(shalist)) != testcnt {
|
2014-12-25 00:55:14 +01:00
|
|
|
t.Errorf("FetchHeightRange: expected %v shas, got %v", testcnt, len(shalist))
|
2013-08-03 17:20:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range shalist {
|
|
|
|
sha0 := *shanames[int64(i)+startheight]
|
|
|
|
sha1 := shalist[i]
|
|
|
|
if sha0 != sha1 {
|
2014-12-25 00:55:14 +01:00
|
|
|
t.Errorf("FetchHeightRange: mismatch sha at %v requested range %v %v: %v %v ", int64(i)+startheight, startheight, endheight, sha0, sha1)
|
2013-08-03 17:20:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-12-25 00:55:14 +01:00
|
|
|
|
|
|
|
func TestLimitAndSkipFetchTxsForAddr(t *testing.T) {
|
2015-01-27 22:38:23 +01:00
|
|
|
testDb, err := setUpTestDb(t, "tstdbtxaddr")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to open test database %v", err)
|
|
|
|
return
|
|
|
|
}
|
2014-12-25 00:55:14 +01:00
|
|
|
defer testDb.cleanUpFunc()
|
|
|
|
|
|
|
|
// Insert a block with some fake test transactions. The block will have
|
|
|
|
// 10 copies of a fake transaction involving same address.
|
|
|
|
addrString := "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa"
|
|
|
|
targetAddr, err := btcutil.DecodeAddress(addrString, &btcnet.MainNetParams)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to decode test address: %v", err)
|
|
|
|
}
|
2015-01-30 19:14:33 +01:00
|
|
|
outputScript, err := txscript.PayToAddrScript(targetAddr)
|
2014-12-25 00:55:14 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable make test pkScript %v", err)
|
|
|
|
}
|
2015-02-05 22:16:39 +01:00
|
|
|
fakeTxOut := wire.NewTxOut(10, outputScript)
|
|
|
|
var emptyHash wire.ShaHash
|
|
|
|
fakeHeader := wire.NewBlockHeader(&emptyHash, &emptyHash, 1, 1)
|
|
|
|
msgBlock := wire.NewMsgBlock(fakeHeader)
|
2014-12-25 00:55:14 +01:00
|
|
|
for i := 0; i < 10; i++ {
|
2015-02-05 22:16:39 +01:00
|
|
|
mtx := wire.NewMsgTx()
|
2014-12-25 00:55:14 +01:00
|
|
|
mtx.AddTxOut(fakeTxOut)
|
|
|
|
msgBlock.AddTransaction(mtx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert the test block into the DB.
|
|
|
|
testBlock := btcutil.NewBlock(msgBlock)
|
|
|
|
newheight, err := testDb.db.InsertBlock(testBlock)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to insert block into db: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create and insert an address index for out test addr.
|
|
|
|
txLoc, _ := testBlock.TxLoc()
|
2015-01-27 18:45:10 +01:00
|
|
|
index := make(database.BlockAddrIndex)
|
2014-12-25 00:55:14 +01:00
|
|
|
for i := range testBlock.Transactions() {
|
|
|
|
var hash160 [ripemd160.Size]byte
|
|
|
|
scriptAddr := targetAddr.ScriptAddress()
|
|
|
|
copy(hash160[:], scriptAddr[:])
|
|
|
|
index[hash160] = append(index[hash160], &txLoc[i])
|
|
|
|
}
|
|
|
|
blkSha, _ := testBlock.Sha()
|
|
|
|
err = testDb.db.UpdateAddrIndexForBlock(blkSha, newheight, index)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("UpdateAddrIndexForBlock: failed to index"+
|
|
|
|
" addrs for block #%d (%s) "+
|
|
|
|
"err %v", newheight, blkSha, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try skipping the first 4 results, should get 6 in return.
|
|
|
|
txReply, err := testDb.db.FetchTxsForAddr(targetAddr, 4, 100000)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to fetch transactions for address: %v", err)
|
|
|
|
}
|
|
|
|
if len(txReply) != 6 {
|
|
|
|
t.Fatalf("Did not correctly skip forward in txs for address reply"+
|
|
|
|
" got %v txs, expected %v", len(txReply), 6)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Limit the number of results to 3.
|
|
|
|
txReply, err = testDb.db.FetchTxsForAddr(targetAddr, 0, 3)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to fetch transactions for address: %v", err)
|
|
|
|
}
|
|
|
|
if len(txReply) != 3 {
|
|
|
|
t.Fatalf("Did not correctly limit in txs for address reply"+
|
|
|
|
" got %v txs, expected %v", len(txReply), 3)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip 1, limit 5.
|
|
|
|
txReply, err = testDb.db.FetchTxsForAddr(targetAddr, 1, 5)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to fetch transactions for address: %v", err)
|
|
|
|
}
|
|
|
|
if len(txReply) != 5 {
|
|
|
|
t.Fatalf("Did not correctly limit in txs for address reply"+
|
|
|
|
" got %v txs, expected %v", len(txReply), 5)
|
|
|
|
}
|
|
|
|
}
|