Skip to content

Commit

Permalink
Use Reddcoin test data to test leveldb. Remove buggy Bitcoin tx.
Browse files Browse the repository at this point in the history
  • Loading branch information
laudney committed Nov 26, 2014
1 parent 2f083ae commit 37f3ace
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 103 deletions.
8 changes: 4 additions & 4 deletions common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ import (
)

var (
// network is the expected bitcoin network in the test block data.
// network is the expected Reddcoin network in the test block data.
network = rddwire.MainNet

// savedBlocks is used to store blocks loaded from the blockDataFile
// so multiple invocations to loadBlocks from the various test functions
// do not have to reload them from disk.
savedBlocks []*rddutil.Block

// blockDataFile is the path to a file containing the first 256 blocks
// blockDataFile is the path to a file containing the first 189 blocks
// of the block chain.
blockDataFile = filepath.Join("testdata", "blocks1-256.bz2")
blockDataFile = filepath.Join("testdata", "blocks1-189.bz2")
)

var zeroHash = rddwire.ShaHash{}
Expand Down Expand Up @@ -178,7 +178,7 @@ func loadBlocks(t *testing.T) ([]*rddutil.Block, error) {
}()

// Set the first block as the genesis block.
blocks := make([]*rddutil.Block, 0, 256)
blocks := make([]*rddutil.Block, 0, 189)
genesis := rddutil.NewBlock(rddnet.MainNetParams.GenesisBlock)
blocks = append(blocks, genesis)

Expand Down
2 changes: 1 addition & 1 deletion db.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var (
const AllShas = int64(^uint64(0) >> 1)

// Db defines a generic interface that is used to request and insert data into
// the bitcoin block chain. This interface is intended to be agnostic to actual
// the Reddcoin block chain. This interface is intended to be agnostic to actual
// mechanism used for backend data storage. The AddDBDriver function can be
// used to add a new backend data storage method.
type Db interface {
Expand Down
2 changes: 1 addition & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,6 @@ func ExampleDb_newestSha() {
fmt.Println("Latest height:", latestHeight)

// Output:
// Latest hash: 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
// Latest hash: b868e0d95a3c3c0e0dadc67ee587aaf9dc8acbf99e3b4b3110fad4eb74c1decc
// Latest height: 0
}
48 changes: 27 additions & 21 deletions interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ func testFetchTxBySha(tc *testContext) bool {
// expectedSpentBuf returns the expected transaction spend information depending
// on the block height and and transaction number. NOTE: These figures are
// only valid for the specific set of test data provided at the time these tests
// were written. In particular, this means the first 256 blocks of the mainnet
// were written. In particular, this means the first 189 blocks of the mainnet
// block chain.
//
// The first run through while the blocks are still being inserted, the tests
Expand All @@ -305,43 +305,49 @@ func expectedSpentBuf(tc *testContext, txNum int) []bool {
numTxOut := len(tc.block.MsgBlock().Transactions[txNum].TxOut)
spentBuf := make([]bool, numTxOut)
if tc.useSpends {
if tc.blockHeight == 9 && txNum == 0 {
// Spent by block 170, tx 1, input 0.
// tx f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16
if tc.blockHeight == 127 && txNum == 1 {
spentBuf[1] = true
}

if tc.blockHeight == 127 && txNum == 2 {
spentBuf[0] = true
}

if tc.blockHeight == 170 && txNum == 1 {
// Spent by block 181, tx 1, input 0.
// tx a16f3ce4dd5deb92d98ef5cf8afeaf0775ebca408f708b2146c4fb42b41e14be
if tc.blockHeight == 127 && txNum == 3 {
spentBuf[1] = true
}

if tc.blockHeight == 181 && txNum == 1 {
// Spent by block 182, tx 1, input 0.
// tx 591e91f809d716912ca1d4a9295e70c3e78bab077683f79350f101da64588073
if tc.blockHeight == 127 && txNum == 4 {
spentBuf[1] = true
}

if tc.blockHeight == 182 && txNum == 1 {
// Spent by block 221, tx 1, input 0.
// tx 298ca2045d174f8a158961806ffc4ef96fad02d71a6b84d9fa0491813a776160
if tc.blockHeight == 127 && txNum == 5 {
spentBuf[0] = true
}

// Spent by block 183, tx 1, input 0.
// tx 12b5633bad1f9c167d523ad1aa1947b2732a865bf5414eab2f9e5ae5d5c191ba
spentBuf[1] = true
if tc.blockHeight == 127 && txNum == 6 {
spentBuf[0] = true
}

if tc.blockHeight == 183 && txNum == 1 {
// Spent by block 187, tx 1, input 0.
// tx 4385fcf8b14497d0659adccfe06ae7e38e0b5dc95ff8a13d7c62035994a0cd79
if tc.blockHeight == 128 && txNum == 1 {
spentBuf[0] = true
}

// Spent by block 248, tx 1, input 0.
// tx 828ef3b079f9c23829c56fe86e85b4a69d9e06e5b54ea597eef5fb3ffef509fe
if tc.blockHeight == 128 && txNum == 2 {
spentBuf[0] = true
}

if tc.blockHeight == 155 && txNum == 1 {
spentBuf[1] = true
}
} else {
if tc.blockHeight == 127 && txNum == 4 {
spentBuf[1] = true
}

if tc.blockHeight == 128 && txNum == 1 {
spentBuf[0] = true
}
}

return spentBuf
Expand Down
9 changes: 5 additions & 4 deletions ldb/dup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func Test_dupTx(t *testing.T) {
}
}()

testdatafile := filepath.Join("testdata", "blocks1-256.bz2")
testdatafile := filepath.Join("testdata", "blocks1-189.bz2")
blocks, err := loadBlocks(t, testdatafile)
if err != nil {
t.Errorf("Unable to load blocks from test data for: %v",
Expand All @@ -45,18 +45,19 @@ func Test_dupTx(t *testing.T) {

var lastSha *rddwire.ShaHash

// Populate with the fisrt 256 blocks, so we have blocks to 'mess with'
// Populate with the fisrt 189 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()

var txneededList []*rddwire.ShaHash
for _, tx := range mblock.Transactions {
for _, txin := range tx.TxIn {
if txin.PreviousOutPoint.Index == uint32(4294967295) {
if txin.PreviousOutPoint.Index == ^uint32(0) {
continue
}
origintxsha := &txin.PreviousOutPoint.Hash
Expand Down Expand Up @@ -122,7 +123,7 @@ out:

mblk := rddwire.NewMsgBlock(&bh)

hash, _ := rddwire.NewShaHashFromStr("df2b060fa2e5e9c8ed5eaf6a45c13753ec8c63282b2688322eba40cd98ea067a")
hash, _ := rddwire.NewShaHashFromStr("898d55c6efdabcb5002900f62e85aba9bf77221f6b764537cda510384113df3d")

po := rddwire.NewOutPoint(hash, 0)
txI := rddwire.NewTxIn(po, []byte("garbage"))
Expand Down
2 changes: 1 addition & 1 deletion ldb/insertremove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func loadblocks(t *testing.T) []*rddutil.Block {
return tstBlocks
}

testdatafile := filepath.Join("..", "testdata", "blocks1-256.bz2")
testdatafile := filepath.Join("..", "testdata", "blocks1-189.bz2")
blocks, err := loadBlocks(t, testdatafile)
if err != nil {
t.Errorf("Unable to load blocks from test data: %v", err)
Expand Down
48 changes: 1 addition & 47 deletions ldb/leveldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,17 +367,13 @@ func (db *LevelDb) InsertBlock(block *rddutil.Block) (height int64, rerr error)
}

// Insert block into database
newheight, err := db.insertBlockData(blocksha, &mblock.Header.PrevBlock,
rawMsg)
newheight, err := db.insertBlockData(blocksha, &mblock.Header.PrevBlock, rawMsg)
if err != nil {
log.Warnf("Failed to insert block %v %v %v", blocksha,
&mblock.Header.PrevBlock, err)
return 0, err
}

// At least two blocks in the long past were generated by faulty
// miners, the sha of the transaction exists in a previous block,
// detect this condition and 'accept' the block.
for txidx, tx := range mblock.Transactions {
txsha, err := block.TxSha(txidx)
if err != nil {
Expand All @@ -398,48 +394,6 @@ func (db *LevelDb) InsertBlock(block *rddutil.Block) (height int64, rerr error)
return 0, err
}

// Some old blocks contain duplicate transactions
// Attempt to cleanly bypass this problem by marking the
// first as fully spent.
// http://blockexplorer.com/b/91812 dup in 91842
// http://blockexplorer.com/b/91722 dup in 91880
if newheight == 91812 {
dupsha, err := rddwire.NewShaHashFromStr("d5d27987d2a3dfc724e359870c6644b40e497bdc0589a033220fe15429d88599")
if err != nil {
panic("invalid sha string in source")
}
if txsha.IsEqual(dupsha) {
// marking TxOut[0] as spent
po := rddwire.NewOutPoint(dupsha, 0)
txI := rddwire.NewTxIn(po, []byte("garbage"))

var spendtx rddwire.MsgTx
spendtx.AddTxIn(txI)
err = db.doSpend(&spendtx)
if err != nil {
log.Warnf("block %v idx %v failed to spend tx %v %v err %v", blocksha, newheight, &txsha, txidx, err)
}
}
}
if newheight == 91722 {
dupsha, err := rddwire.NewShaHashFromStr("e3bf3d07d4b0375638d5f1db5255fe07ba2c4cb067cd81b84ee974b6585fb468")
if err != nil {
panic("invalid sha string in source")
}
if txsha.IsEqual(dupsha) {
// marking TxOut[0] as spent
po := rddwire.NewOutPoint(dupsha, 0)
txI := rddwire.NewTxIn(po, []byte("garbage"))

var spendtx rddwire.MsgTx
spendtx.AddTxIn(txI)
err = db.doSpend(&spendtx)
if err != nil {
log.Warnf("block %v idx %v failed to spend tx %v %v err %v", blocksha, newheight, &txsha, txidx, err)
}
}
}

err = db.doSpend(tx)
if err != nil {
log.Warnf("block %v idx %v failed to spend tx %v %v err %v", blocksha, newheight, txsha, txidx, err)
Expand Down
8 changes: 5 additions & 3 deletions ldb/operational_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"testing"

"github.com/reddcoin-project/rdddb"
_ "github.com/reddcoin-project/rdddb/ldb"
"github.com/reddcoin-project/rddnet"
"github.com/reddcoin-project/rddutil"
"github.com/reddcoin-project/rddwire"
Expand All @@ -23,6 +24,7 @@ import (
var network = rddwire.MainNet

func TestOperational(t *testing.T) {
rdddb.SetLogWriter(os.Stdout, "trace")
testOperationalMode(t)
}

Expand Down Expand Up @@ -50,7 +52,7 @@ func testOperationalMode(t *testing.T) {
}
}()

testdatafile := filepath.Join("..", "testdata", "blocks1-256.bz2")
testdatafile := filepath.Join("..", "testdata", "blocks1-189.bz2")
blocks, err := loadBlocks(t, testdatafile)
if err != nil {
t.Errorf("Unable to load blocks from test data: %v", err)
Expand Down Expand Up @@ -146,7 +148,7 @@ func testBackout(t *testing.T) {
defer os.RemoveAll(dbnamever)
defer db.Close()

testdatafile := filepath.Join("..", "testdata", "blocks1-256.bz2")
testdatafile := filepath.Join("..", "testdata", "blocks1-189.bz2")
blocks, err := loadBlocks(t, testdatafile)
if len(blocks) < 120 {
t.Errorf("test data too small")
Expand Down Expand Up @@ -246,7 +248,7 @@ func loadBlocks(t *testing.T, file string) (blocks []*rddutil.Block, err error)
blocks = savedblocks
return
}
testdatafile := filepath.Join("..", "testdata", "blocks1-256.bz2")
testdatafile := filepath.Join("..", "testdata", "blocks1-189.bz2")
var dr io.Reader
var fi io.ReadCloser
fi, err = os.Open(testdatafile)
Expand Down
21 changes: 0 additions & 21 deletions memdb/memdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ var (

var (
zeroHash = rddwire.ShaHash{}

// The following two hashes are ones that must be specially handled.
// See the comments where they're used for more details.
dupTxHash91842 = newShaHashFromStr("d5d27987d2a3dfc724e359870c6644b40e497bdc0589a033220fe15429d88599")
dupTxHash91880 = newShaHashFromStr("e3bf3d07d4b0375638d5f1db5255fe07ba2c4cb067cd81b84ee974b6585fb468")
)

// tTxInsertData holds information about the location and spent status of
Expand Down Expand Up @@ -568,22 +563,6 @@ func (db *MemDb) InsertBlock(block *rddutil.Block) (int64, error) {
// deal with rollback on errors.
newHeight := int64(len(db.blocks))
for i, tx := range transactions {
// Two old blocks contain duplicate transactions due to being
// mined by faulty miners and accepted by the origin Satoshi
// client. Rules have since been added to the ensure this
// problem can no longer happen, but the two duplicate
// transactions which were originally accepted are forever in
// the block chain history and must be dealth with specially.
// http://blockexplorer.com/b/91842
// http://blockexplorer.com/b/91880
if newHeight == 91842 && tx.Sha().IsEqual(dupTxHash91842) {
continue
}

if newHeight == 91880 && tx.Sha().IsEqual(dupTxHash91880) {
continue
}

for _, txIn := range tx.MsgTx().TxIn {
if isCoinbaseInput(txIn) {
continue
Expand Down
Binary file added testdata/blocks1-189.bz2
Binary file not shown.
Binary file removed testdata/blocks1-256.bz2
Binary file not shown.

0 comments on commit 37f3ace

Please sign in to comment.