Update to no longer use deprecated TxShas.

The btcutil code was recently changed to provide a new Tx type which
provides hash caching (among other things).  As a result, the notion of
obtaining a transaction hashes via TxShas was deprecated as well.  This
commit updates the tests and backend implementation code accordingly.
This commit is contained in:
Dave Collins 2013-11-01 21:17:06 -05:00
parent 83b1e74d6c
commit 80981b4696
2 changed files with 16 additions and 29 deletions

View File

@ -180,15 +180,9 @@ func testFetchBlockShaByHeightErrors(tc *testContext) bool {
// testExistsTxSha ensures ExistsTxSha conforms to the interface contract. // testExistsTxSha ensures ExistsTxSha conforms to the interface contract.
func testExistsTxSha(tc *testContext) bool { func testExistsTxSha(tc *testContext) bool {
txHashes, err := tc.block.TxShas() for i, tx := range tc.block.Transactions() {
if err != nil {
tc.t.Errorf("block.TxShas: %v", err)
return false
}
for i := range txHashes {
// The transaction must exist in the database. // The transaction must exist in the database.
txHash := txHashes[i] txHash := tx.Sha()
if exists := tc.db.ExistsTxSha(txHash); !exists { if exists := tc.db.ExistsTxSha(txHash); !exists {
tc.t.Errorf("ExistsTxSha (%s): block #%d (%s) "+ tc.t.Errorf("ExistsTxSha (%s): block #%d (%s) "+
"tx #%d (%s) does not exist", tc.dbType, "tx #%d (%s) does not exist", tc.dbType,
@ -202,14 +196,8 @@ func testExistsTxSha(tc *testContext) bool {
// testFetchTxBySha ensures FetchTxBySha conforms to the interface contract. // testFetchTxBySha ensures FetchTxBySha conforms to the interface contract.
func testFetchTxBySha(tc *testContext) bool { func testFetchTxBySha(tc *testContext) bool {
txHashes, err := tc.block.TxShas() for i, tx := range tc.block.Transactions() {
if err != nil { txHash := tx.Sha()
tc.t.Errorf("block.TxShas: %v", err)
return false
}
for i, tx := range tc.block.MsgBlock().Transactions {
txHash := txHashes[i]
txReplyList, err := tc.db.FetchTxBySha(txHash) txReplyList, err := tc.db.FetchTxBySha(txHash)
if err != nil { if err != nil {
tc.t.Errorf("FetchTxBySha (%s): block #%d (%s) "+ tc.t.Errorf("FetchTxBySha (%s): block #%d (%s) "+
@ -225,12 +213,12 @@ func testFetchTxBySha(tc *testContext) bool {
return false return false
} }
txFromDb := txReplyList[len(txReplyList)-1].Tx txFromDb := txReplyList[len(txReplyList)-1].Tx
if !reflect.DeepEqual(tx, txFromDb) { if !reflect.DeepEqual(tx.MsgTx(), txFromDb) {
tc.t.Errorf("FetchTxBySha (%s): block #%d (%s) "+ tc.t.Errorf("FetchTxBySha (%s): block #%d (%s) "+
"tx #%d (%s) does not match stored tx\n"+ "tx #%d (%s) does not match stored tx\n"+
"got: %v\nwant: %v", tc.dbType, tc.blockHeight, "got: %v\nwant: %v", tc.dbType, tc.blockHeight,
tc.blockHash, i, txHash, spew.Sdump(txFromDb), tc.blockHash, i, txHash, spew.Sdump(txFromDb),
spew.Sdump(tx)) spew.Sdump(tx.MsgTx()))
return false return false
} }
} }
@ -286,10 +274,10 @@ func testFetchTxByShaListCommon(tc *testContext, includeSpent bool) bool {
funcName = "FetchTxByShaList" funcName = "FetchTxByShaList"
} }
txHashes, err := tc.block.TxShas() transactions := tc.block.Transactions()
if err != nil { txHashes := make([]*btcwire.ShaHash, len(transactions))
tc.t.Errorf("block.TxShas: %v", err) for i, tx := range transactions {
return false txHashes[i] = tx.Sha()
} }
txReplyList := fetchFunc(txHashes) txReplyList := fetchFunc(txHashes)
@ -300,8 +288,8 @@ func testFetchTxByShaListCommon(tc *testContext, includeSpent bool) bool {
len(txReplyList), len(txHashes)) len(txReplyList), len(txHashes))
return false return false
} }
for i, tx := range tc.block.MsgBlock().Transactions { for i, tx := range transactions {
txHash := txHashes[i] txHash := tx.Sha()
txD := txReplyList[i] txD := txReplyList[i]
// The transaction hash in the reply must be the expected value. // The transaction hash in the reply must be the expected value.
@ -324,12 +312,12 @@ func testFetchTxByShaListCommon(tc *testContext, includeSpent bool) bool {
// The transaction in the reply fetched from the database must // The transaction in the reply fetched from the database must
// be the same MsgTx that was stored. // be the same MsgTx that was stored.
if !reflect.DeepEqual(tx, txD.Tx) { if !reflect.DeepEqual(tx.MsgTx(), txD.Tx) {
tc.t.Errorf("%s (%s): block #%d (%s) tx #%d (%s) does "+ tc.t.Errorf("%s (%s): block #%d (%s) tx #%d (%s) does "+
"not match stored tx\ngot: %v\nwant: %v", "not match stored tx\ngot: %v\nwant: %v",
funcName, tc.dbType, tc.blockHeight, funcName, tc.dbType, tc.blockHeight,
tc.blockHash, i, txHash, spew.Sdump(txD.Tx), tc.blockHash, i, txHash, spew.Sdump(txD.Tx),
spew.Sdump(tx)) spew.Sdump(tx.MsgTx()))
return false return false
} }

View File

@ -288,11 +288,10 @@ func (db *LevelDb) DropAfterBlockBySha(sha *btcwire.ShaHash) (rerr error) {
} }
} }
// rather than iterate the list of tx backward, do it twice. // rather than iterate the list of tx backward, do it twice.
txShas, _ := blk.TxShas() for _, tx := range blk.Transactions() {
for _, txSha := range txShas {
var txUo txUpdateObj var txUo txUpdateObj
txUo.delete = true txUo.delete = true
db.txUpdateMap[*txSha] = &txUo db.txUpdateMap[*tx.Sha()] = &txUo
} }
db.lBatch().Delete(shaBlkToKey(blksha)) db.lBatch().Delete(shaBlkToKey(blksha))
db.lBatch().Delete(int64ToKey(height)) db.lBatch().Delete(int64ToKey(height))