mirror of
https://github.com/LBRYFoundation/lbcd.git
synced 2025-09-02 10:15:15 +00:00
Combine two test functions used to fetch tx lists.
The two functions are nearly identical, so rather than repeating the test code, refactor it into a separate function that takes a flag.
This commit is contained in:
parent
a9d719f0c4
commit
54ad820133
1 changed files with 42 additions and 123 deletions
|
@ -201,21 +201,26 @@ func testFetchTxBySha(tc *testContext) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// testFetchTxByShaList ensures FetchTxByShaList conforms to the interface
|
func testFetchTxByShaListCommon(tc *testContext, includeSpent bool) bool {
|
||||||
// contract.
|
fetchFunc := tc.db.FetchUnSpentTxByShaList
|
||||||
func testFetchTxByShaList(tc *testContext) bool {
|
funcName := "FetchUnSpentTxByShaList"
|
||||||
|
if includeSpent {
|
||||||
|
fetchFunc = tc.db.FetchTxByShaList
|
||||||
|
funcName = "FetchTxByShaList"
|
||||||
|
}
|
||||||
|
|
||||||
txHashes, err := tc.block.TxShas()
|
txHashes, err := tc.block.TxShas()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tc.t.Errorf("block.TxShas: %v", err)
|
tc.t.Errorf("block.TxShas: %v", err)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
txReplyList := tc.db.FetchTxByShaList(txHashes)
|
txReplyList := fetchFunc(txHashes)
|
||||||
if len(txReplyList) != len(txHashes) {
|
if len(txReplyList) != len(txHashes) {
|
||||||
tc.t.Errorf("FetchTxByShaList (%s): block #%d (%s) "+
|
tc.t.Errorf("%s (%s): block #%d (%s) tx reply list does not "+
|
||||||
"tx reply list does not match expected length "+
|
" match expected length - got: %v, want: %v", funcName,
|
||||||
"- got: %v, want: %v", tc.dbType, tc.blockHeight,
|
tc.dbType, tc.blockHeight, tc.blockHash,
|
||||||
tc.blockHash, len(txReplyList), len(txHashes))
|
len(txReplyList), len(txHashes))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
for i, tx := range tc.block.MsgBlock().Transactions {
|
for i, tx := range tc.block.MsgBlock().Transactions {
|
||||||
|
@ -224,18 +229,18 @@ func testFetchTxByShaList(tc *testContext) bool {
|
||||||
|
|
||||||
// The transaction hash in the reply must be the expected value.
|
// The transaction hash in the reply must be the expected value.
|
||||||
if !txD.Sha.IsEqual(txHash) {
|
if !txD.Sha.IsEqual(txHash) {
|
||||||
tc.t.Errorf("FetchTxByShaList (%s): block #%d (%s) "+
|
tc.t.Errorf("%s (%s): block #%d (%s) tx #%d (%s) "+
|
||||||
"tx #%d (%s) hash does not match expected "+
|
"hash does not match expected value - got %v",
|
||||||
"value - got %v", tc.dbType, tc.blockHeight,
|
funcName, tc.dbType, tc.blockHeight,
|
||||||
tc.blockHash, i, txHash, txD.Sha)
|
tc.blockHash, i, txHash, txD.Sha)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// The reply must not indicate any errors.
|
// The reply must not indicate any errors.
|
||||||
if txD.Err != nil {
|
if txD.Err != nil {
|
||||||
tc.t.Errorf("FetchTxByShaList (%s): block #%d (%s) "+
|
tc.t.Errorf("%s (%s): block #%d (%s) tx #%d (%s) "+
|
||||||
"tx #%d (%s) returned unexpected error - "+
|
"returned unexpected error - got %v, want nil",
|
||||||
"got %v, want nil", tc.dbType, tc.blockHeight,
|
funcName, tc.dbType, tc.blockHeight,
|
||||||
tc.blockHash, i, txHash, txD.Err)
|
tc.blockHash, i, txHash, txD.Err)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -243,9 +248,9 @@ func testFetchTxByShaList(tc *testContext) 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, txD.Tx) {
|
||||||
tc.t.Errorf("FetchTxByShaList (%s): block #%d (%s) "+
|
tc.t.Errorf("%s (%s): block #%d (%s) tx #%d (%s) does "+
|
||||||
"tx #%d (%s) does not match stored tx\n"+
|
"not match stored tx\ngot: %v\nwant: %v",
|
||||||
"got: %v\nwant: %v", 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))
|
||||||
return false
|
return false
|
||||||
|
@ -254,15 +259,15 @@ func testFetchTxByShaList(tc *testContext) bool {
|
||||||
// The block hash in the reply from the database must be the
|
// The block hash in the reply from the database must be the
|
||||||
// expected value.
|
// expected value.
|
||||||
if txD.BlkSha == nil {
|
if txD.BlkSha == nil {
|
||||||
tc.t.Errorf("FetchTxByShaList (%s): block #%d (%s) "+
|
tc.t.Errorf("%s (%s): block #%d (%s) tx #%d (%s) "+
|
||||||
"tx #%d (%s) returned nil block hash", tc.dbType,
|
"returned nil block hash", funcName, tc.dbType,
|
||||||
tc.blockHeight, tc.blockHash, i, txHash)
|
tc.blockHeight, tc.blockHash, i, txHash)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if !txD.BlkSha.IsEqual(tc.blockHash) {
|
if !txD.BlkSha.IsEqual(tc.blockHash) {
|
||||||
tc.t.Errorf("FetchTxByShaList (%s): block #%d (%s) "+
|
tc.t.Errorf("%s (%s): block #%d (%s) tx #%d (%s)"+
|
||||||
"tx #%d (%s) returned unexpected block hash - "+
|
"returned unexpected block hash - got %v",
|
||||||
"got %v", tc.dbType, tc.blockHeight,
|
funcName, tc.dbType, tc.blockHeight,
|
||||||
tc.blockHash, i, txHash, txD.BlkSha)
|
tc.blockHash, i, txHash, txD.BlkSha)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -270,9 +275,9 @@ func testFetchTxByShaList(tc *testContext) bool {
|
||||||
// The block height in the reply from the database must be the
|
// The block height in the reply from the database must be the
|
||||||
// expected value.
|
// expected value.
|
||||||
if txD.Height != tc.blockHeight {
|
if txD.Height != tc.blockHeight {
|
||||||
tc.t.Errorf("FetchTxByShaList (%s): block #%d (%s) "+
|
tc.t.Errorf("%s (%s): block #%d (%s) tx #%d (%s) "+
|
||||||
"tx #%d (%s) returned unexpected block height "+
|
"returned unexpected block height - got %v",
|
||||||
"- got %v", tc.dbType, tc.blockHeight,
|
funcName, tc.dbType, tc.blockHeight,
|
||||||
tc.blockHash, i, txHash, txD.Height)
|
tc.blockHash, i, txHash, txD.Height)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -281,16 +286,16 @@ func testFetchTxByShaList(tc *testContext) bool {
|
||||||
// indicate any of the transactions that were just inserted are
|
// indicate any of the transactions that were just inserted are
|
||||||
// spent.
|
// spent.
|
||||||
if txD.TxSpent == nil {
|
if txD.TxSpent == nil {
|
||||||
tc.t.Errorf("FetchTxByShaList (%s): block #%d (%s) "+
|
tc.t.Errorf("%s (%s): block #%d (%s) tx #%d (%s) "+
|
||||||
"tx #%d (%s) returned nil spend data", tc.dbType,
|
"returned nil spend data", funcName, tc.dbType,
|
||||||
tc.blockHeight, tc.blockHash, i, txHash)
|
tc.blockHeight, tc.blockHash, i, txHash)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
noSpends := make([]bool, len(tx.TxOut))
|
noSpends := make([]bool, len(tx.TxOut))
|
||||||
if !reflect.DeepEqual(txD.TxSpent, noSpends) {
|
if !reflect.DeepEqual(txD.TxSpent, noSpends) {
|
||||||
tc.t.Errorf("FetchTxByShaList (%s): block #%d (%s) "+
|
tc.t.Errorf("%s (%s): block #%d (%s) tx #%d (%s) "+
|
||||||
"tx #%d (%s) returned unexpected spend data - "+
|
"returned unexpected spend data - got %v, "+
|
||||||
"got %v, want %v", tc.dbType, tc.blockHeight,
|
"want %v", funcName, tc.dbType, tc.blockHeight,
|
||||||
tc.blockHash, i, txHash, txD.TxSpent, noSpends)
|
tc.blockHash, i, txHash, txD.TxSpent, noSpends)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -299,102 +304,16 @@ func testFetchTxByShaList(tc *testContext) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// testFetchTxByShaList ensures FetchTxByShaList conforms to the interface
|
||||||
|
// contract.
|
||||||
|
func testFetchTxByShaList(tc *testContext) bool {
|
||||||
|
return testFetchTxByShaListCommon(tc, true)
|
||||||
|
}
|
||||||
|
|
||||||
// testFetchUnSpentTxByShaList ensures FetchUnSpentTxByShaList conforms to the
|
// testFetchUnSpentTxByShaList ensures FetchUnSpentTxByShaList conforms to the
|
||||||
// interface contract.
|
// interface contract.
|
||||||
func testFetchUnSpentTxByShaList(tc *testContext) bool {
|
func testFetchUnSpentTxByShaList(tc *testContext) bool {
|
||||||
txHashes, err := tc.block.TxShas()
|
return testFetchTxByShaListCommon(tc, false)
|
||||||
if err != nil {
|
|
||||||
tc.t.Errorf("block.TxShas: %v", err)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
txReplyList := tc.db.FetchUnSpentTxByShaList(txHashes)
|
|
||||||
if len(txReplyList) != len(txHashes) {
|
|
||||||
tc.t.Errorf("FetchUnSpentTxByShaList (%s): block #%d (%s) "+
|
|
||||||
"tx reply list does not match expected length "+
|
|
||||||
"- got: %v, want: %v", tc.dbType, tc.blockHeight,
|
|
||||||
tc.blockHash, len(txReplyList), len(txHashes))
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
for i, tx := range tc.block.MsgBlock().Transactions {
|
|
||||||
txHash := txHashes[i]
|
|
||||||
txD := txReplyList[i]
|
|
||||||
|
|
||||||
// The transaction hash in the reply must be the expected value.
|
|
||||||
if !txD.Sha.IsEqual(txHash) {
|
|
||||||
tc.t.Errorf("FetchUnSpentTxByShaList (%s): block #%d (%s) "+
|
|
||||||
"tx #%d (%s) hash does not match expected "+
|
|
||||||
"value - got %v", tc.dbType, tc.blockHeight,
|
|
||||||
tc.blockHash, i, txHash, txD.Sha)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// The reply must not indicate any errors.
|
|
||||||
if txD.Err != nil {
|
|
||||||
tc.t.Errorf("FetchUnSpentTxByShaList (%s): block #%d (%s) "+
|
|
||||||
"tx #%d (%s) returned unexpected error - "+
|
|
||||||
"got %v, want nil", tc.dbType, tc.blockHeight,
|
|
||||||
tc.blockHash, i, txHash, txD.Err)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// The transaction in the reply fetched from the database must
|
|
||||||
// be the same MsgTx that was stored.
|
|
||||||
if !reflect.DeepEqual(tx, txD.Tx) {
|
|
||||||
tc.t.Errorf("FetchUnSpentTxByShaList (%s): block #%d (%s) "+
|
|
||||||
"tx #%d (%s) does not match stored tx\n"+
|
|
||||||
"got: %v\nwant: %v", tc.dbType, tc.blockHeight,
|
|
||||||
tc.blockHash, i, txHash, spew.Sdump(txD.Tx),
|
|
||||||
spew.Sdump(tx))
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// The block hash in the reply from the database must be the
|
|
||||||
// expected value.
|
|
||||||
if txD.BlkSha == nil {
|
|
||||||
tc.t.Errorf("FetchUnSpentTxByShaList (%s): block #%d (%s) "+
|
|
||||||
"tx #%d (%s) returned nil block hash", tc.dbType,
|
|
||||||
tc.blockHeight, tc.blockHash, i, txHash)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if !txD.BlkSha.IsEqual(tc.blockHash) {
|
|
||||||
tc.t.Errorf("FetchUnSpentTxByShaList (%s): block #%d (%s) "+
|
|
||||||
"tx #%d (%s) returned unexpected block hash - "+
|
|
||||||
"got %v", tc.dbType, tc.blockHeight,
|
|
||||||
tc.blockHash, i, txHash, txD.BlkSha)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// The block height in the reply from the database must be the
|
|
||||||
// expected value.
|
|
||||||
if txD.Height != tc.blockHeight {
|
|
||||||
tc.t.Errorf("FetchUnSpentTxByShaList (%s): block #%d (%s) "+
|
|
||||||
"tx #%d (%s) returned unexpected block height "+
|
|
||||||
"- got %v", tc.dbType, tc.blockHeight,
|
|
||||||
tc.blockHash, i, txHash, txD.Height)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// The spend data in the reply from the database must not
|
|
||||||
// indicate any of the transactions that were just inserted are
|
|
||||||
// spent.
|
|
||||||
if txD.TxSpent == nil {
|
|
||||||
tc.t.Errorf("FetchUnSpentTxByShaList (%s): block #%d (%s) "+
|
|
||||||
"tx #%d (%s) returned nil spend data", tc.dbType,
|
|
||||||
tc.blockHeight, tc.blockHash, i, txHash)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
noSpends := make([]bool, len(tx.TxOut))
|
|
||||||
if !reflect.DeepEqual(txD.TxSpent, noSpends) {
|
|
||||||
tc.t.Errorf("FetchUnSpentTxByShaList (%s): block #%d (%s) "+
|
|
||||||
"tx #%d (%s) returned unexpected spend data - "+
|
|
||||||
"got %v, want %v", tc.dbType, tc.blockHeight,
|
|
||||||
tc.blockHash, i, txHash, txD.TxSpent, noSpends)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// testIntegrity performs a series of tests against the interface functions
|
// testIntegrity performs a series of tests against the interface functions
|
||||||
|
|
Loading…
Add table
Reference in a new issue