2014-01-09 06:54:52 +01:00
|
|
|
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
2013-09-30 23:44:26 +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 (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2014-07-03 02:47:24 +02:00
|
|
|
|
2015-01-27 18:45:10 +01:00
|
|
|
"github.com/btcsuite/btcd/database"
|
2015-02-05 22:16:39 +01:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2015-01-15 17:33:06 +01:00
|
|
|
"github.com/btcsuite/btcutil"
|
2013-09-30 23:44:26 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_dupTx(t *testing.T) {
|
|
|
|
|
|
|
|
// Ignore db remove errors since it means we didn't have an old one.
|
|
|
|
dbname := fmt.Sprintf("tstdbdup0")
|
|
|
|
dbnamever := dbname + ".ver"
|
|
|
|
_ = os.RemoveAll(dbname)
|
|
|
|
_ = os.RemoveAll(dbnamever)
|
2015-01-27 18:45:10 +01:00
|
|
|
db, err := database.CreateDB("leveldb", dbname)
|
2013-09-30 23:44:26 +02:00
|
|
|
if err != nil {
|
2013-11-21 16:48:57 +01:00
|
|
|
t.Errorf("Failed to open test database %v", err)
|
2013-09-30 23:44:26 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dbname)
|
|
|
|
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-09-30 23:44:26 +02:00
|
|
|
|
|
|
|
testdatafile := filepath.Join("testdata", "blocks1-256.bz2")
|
|
|
|
blocks, err := loadBlocks(t, testdatafile)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unable to load blocks from test data for: %v",
|
|
|
|
err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-05 22:16:39 +01:00
|
|
|
var lastSha *wire.ShaHash
|
2013-09-30 23:44:26 +02:00
|
|
|
|
|
|
|
// Populate with the fisrt 256 blocks, so we have blocks to 'mess with'
|
|
|
|
err = nil
|
|
|
|
out:
|
|
|
|
for height := int64(0); height < int64(len(blocks)); height++ {
|
|
|
|
block := blocks[height]
|
|
|
|
|
|
|
|
// except for NoVerify which does not allow lookups check inputs
|
|
|
|
mblock := block.MsgBlock()
|
2015-02-05 22:16:39 +01:00
|
|
|
var txneededList []*wire.ShaHash
|
2013-09-30 23:44:26 +02: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) {
|
2013-09-30 23:44:26 +02:00
|
|
|
continue
|
|
|
|
}
|
2014-10-01 14:52:19 +02:00
|
|
|
origintxsha := &txin.PreviousOutPoint.Hash
|
2013-09-30 23:44:26 +02: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 {
|
2013-09-30 23:44:26 +02:00
|
|
|
t.Errorf("referenced tx not found %v ", origintxsha)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = db.FetchTxBySha(origintxsha)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("referenced tx not found %v err %v ", origintxsha, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
t.Errorf("height doe not match latest block height %v %v %v", blkid, height, err)
|
|
|
|
}
|
|
|
|
|
2015-04-17 07:58:45 +02:00
|
|
|
blkSha := block.Sha()
|
2013-09-30 23:44:26 +02:00
|
|
|
if *newSha != *blkSha {
|
|
|
|
t.Errorf("Newest block sha does not match freshly inserted one %v %v %v ", newSha, blkSha, err)
|
|
|
|
}
|
|
|
|
lastSha = blkSha
|
|
|
|
}
|
|
|
|
|
2014-12-25 00:55:14 +01:00
|
|
|
// generate a new block based on the last sha
|
2013-09-30 23:44:26 +02:00
|
|
|
// these block are not verified, so there are a bunch of garbage fields
|
|
|
|
// in the 'generated' block.
|
|
|
|
|
2015-02-05 22:16:39 +01:00
|
|
|
var bh wire.BlockHeader
|
2013-09-30 23:44:26 +02:00
|
|
|
|
|
|
|
bh.Version = 2
|
|
|
|
bh.PrevBlock = *lastSha
|
|
|
|
// Bits, Nonce are not filled in
|
|
|
|
|
2015-02-05 22:16:39 +01:00
|
|
|
mblk := wire.NewMsgBlock(&bh)
|
2013-09-30 23:44:26 +02:00
|
|
|
|
2015-02-05 22:16:39 +01:00
|
|
|
hash, _ := wire.NewShaHashFromStr("df2b060fa2e5e9c8ed5eaf6a45c13753ec8c63282b2688322eba40cd98ea067a")
|
2013-09-30 23:44:26 +02:00
|
|
|
|
2015-02-05 22:16:39 +01:00
|
|
|
po := wire.NewOutPoint(hash, 0)
|
|
|
|
txI := wire.NewTxIn(po, []byte("garbage"))
|
|
|
|
txO := wire.NewTxOut(50000000, []byte("garbageout"))
|
2013-09-30 23:44:26 +02:00
|
|
|
|
2015-02-05 22:16:39 +01:00
|
|
|
var tx wire.MsgTx
|
2013-09-30 23:44:26 +02:00
|
|
|
tx.AddTxIn(txI)
|
|
|
|
tx.AddTxOut(txO)
|
|
|
|
|
|
|
|
mblk.AddTransaction(&tx)
|
|
|
|
|
|
|
|
blk := btcutil.NewBlock(mblk)
|
|
|
|
|
2015-02-05 22:16:39 +01:00
|
|
|
fetchList := []*wire.ShaHash{hash}
|
2013-09-30 23:44:26 +02:00
|
|
|
listReply := db.FetchUnSpentTxByShaList(fetchList)
|
|
|
|
for _, lr := range listReply {
|
|
|
|
if lr.Err != nil {
|
|
|
|
t.Errorf("sha %v spent %v err %v\n", lr.Sha,
|
2013-11-21 16:48:57 +01:00
|
|
|
lr.TxSpent, lr.Err)
|
2013-09-30 23:44:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = db.InsertBlock(blk)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to insert phony block %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ok, did it 'spend' the tx ?
|
|
|
|
|
|
|
|
listReply = db.FetchUnSpentTxByShaList(fetchList)
|
|
|
|
for _, lr := range listReply {
|
2015-01-27 18:45:10 +01:00
|
|
|
if lr.Err != database.ErrTxShaMissing {
|
2013-09-30 23:44:26 +02:00
|
|
|
t.Errorf("sha %v spent %v err %v\n", lr.Sha,
|
2013-11-21 16:48:57 +01:00
|
|
|
lr.TxSpent, lr.Err)
|
2013-09-30 23:44:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-15 16:43:12 +02:00
|
|
|
txlist := blk.Transactions()
|
|
|
|
for _, tx := range txlist {
|
|
|
|
txsha := tx.Sha()
|
2013-09-30 23:44:26 +02:00
|
|
|
txReply, err := db.FetchTxBySha(txsha)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("fully spent lookup %v err %v\n", hash, err)
|
|
|
|
} else {
|
|
|
|
for _, lr := range txReply {
|
|
|
|
if lr.Err != nil {
|
2015-04-09 07:23:40 +02:00
|
|
|
t.Errorf("stx %v spent %v err %v\n", lr.Sha,
|
2013-11-21 16:48:57 +01:00
|
|
|
lr.TxSpent, lr.Err)
|
2013-09-30 23:44:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("Dropping block")
|
|
|
|
|
|
|
|
err = db.DropAfterBlockBySha(lastSha)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to drop spending block %v", err)
|
|
|
|
}
|
|
|
|
}
|