Merge pull request #115 from zcash-hackworks/storage-GetTx-test

Storage get tx test
This commit is contained in:
Larry Ruane 2019-10-29 15:12:53 -06:00 committed by GitHub
commit 8cdb935805
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 135 additions and 8 deletions

View File

@ -48,10 +48,10 @@ type outputTestVector struct {
type txTestVector struct {
// Sprout and Sapling
header, nVersionGroupId, nLockTime, nExpiryHeight string
vin, vout [][]string
vJoinSplits []joinSplitTestVector
joinSplitPubKey, joinSplitSig string
txid, header, nVersionGroupId, nLockTime, nExpiryHeight string
vin, vout [][]string
vJoinSplits []joinSplitTestVector
joinSplitPubKey, joinSplitSig string
// Sapling-only
valueBalance string // encoded int64
@ -64,6 +64,7 @@ type txTestVector struct {
var zip143tests = []txTestVector{
{
// Test vector 1
txid: "f0b22277ac851b5f4df590fe6a128aad9d0ce8063235eb2b328c2dc6a23c1ec5",
header: "03000080",
nVersionGroupId: "7082c403",
nLockTime: "481cdd86",
@ -77,6 +78,7 @@ var zip143tests = []txTestVector{
{
// Test vector 2
//raw: "we have some raw data for this tx, which this comment is too small to contain",
txid: "39fe585a56b005f568c3171d22afa916e946e2a8aff5971d58ee8a6fc1482059",
header: "03000080",
nVersionGroupId: "7082c403",
nLockTime: "97b0e4e4",
@ -222,6 +224,9 @@ func TestSproutTransactionParser(t *testing.T) {
t.Errorf("Test %d: jsSig mismatch %x %x", i, testJSSig, tx.joinSplitSig)
continue
}
if hex.EncodeToString(tx.GetDisplayHash()) != tt.txid {
t.Errorf("Test %d: incorrect txid", i)
}
}
}
@ -507,6 +512,7 @@ func subTestTransparentOutputs(testOutputs [][]string, txOutputs []*txOut, t *te
var zip243tests = []txTestVector{
// Test vector 1
{
txid: "5fc4867a1b8bd5ab709799adf322a85d10607e053726d5f5ab4b1c9ab897e6bc",
header: "04000080",
nVersionGroupId: "85202f89",
vin: nil,
@ -609,6 +615,7 @@ var zip243tests = []txTestVector{
},
// Test vector 2
{
txid: "6732cf8d67aac5b82a2a0f0217a7d4aa245b2adb0b97fd2d923dfc674415e221",
header: "04000080",
nVersionGroupId: "85202f89",
vin: [][]string{
@ -749,6 +756,14 @@ func TestSaplingTransactionParser(t *testing.T) {
t.Errorf("Test %d: bindingSig %x %x", i, testBinding, tx.bindingSig)
continue
}
if hex.EncodeToString(tx.GetDisplayHash()) != tt.txid {
t.Errorf("Test %d: incorrect txid", i)
}
// test caching
if hex.EncodeToString(tx.GetDisplayHash()) != tt.txid {
t.Errorf("Test %d: incorrect cached txid", i)
}
}
}

View File

@ -69,7 +69,7 @@ func GetBlock(ctx context.Context, db *sql.DB, height int) ([]byte, error) {
query := "SELECT compact_encoding from blocks WHERE block_height = ?"
err := db.QueryRowContext(ctx, query, height).Scan(&blockBytes)
if err != nil {
return nil, err
return nil, errors.Wrap(err, fmt.Sprintf("getting block with height %d", height))
}
return blockBytes, err
}

View File

@ -1,12 +1,14 @@
package storage
import (
"bytes"
"context"
"database/sql"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"strings"
"testing"
"time"
@ -45,6 +47,8 @@ func TestSqliteStorage(t *testing.T) {
}
defer db.Close()
ctx := context.Background()
// Fill tables
{
err = CreateTables(db)
@ -72,6 +76,25 @@ func TestSqliteStorage(t *testing.T) {
t.Error(err)
continue
}
blockLookup, err := GetBlockByHash(ctx, db, hash)
if err != nil {
t.Error(errors.Wrap(err, fmt.Sprintf("GetBlockByHash block %d", test.BlockHeight)))
continue
}
if !bytes.Equal(blockLookup, marshaled) {
t.Errorf("GetBlockByHash unexpected result, block %d", test.BlockHeight)
}
// nonexistent hash
_, err = GetBlockByHash(ctx, db, "4ff234f7b51971cbeb7719a1c32d1c7e1ed92afafed266a7b1ae235717df0501")
if err == nil {
t.Fatal(errors.Wrap(err, fmt.Sprintf("GetBlockByHash unexpected success block %d", test.BlockHeight)))
continue
}
if !strings.Contains(err.Error(), "getting block with hash") ||
!strings.Contains(err.Error(), "no rows in result set") {
t.Error(errors.Wrap(err, fmt.Sprintf("GetBlockByHash wrong error block %d", test.BlockHeight)))
continue
}
}
}
@ -89,8 +112,6 @@ func TestSqliteStorage(t *testing.T) {
}
}
ctx := context.Background()
// Check height state is as expected
{
blockHeight, err := GetCurrentHeight(ctx, db)
@ -108,6 +129,15 @@ func TestSqliteStorage(t *testing.T) {
if err != nil {
t.Error(errors.Wrap(err, "retrieving stored block"))
}
_, err = GetBlock(ctx, db, blockHeight+1)
if err == nil {
t.Fatal(errors.Wrap(err, "GetBlock unexpected success"))
}
if !strings.Contains(err.Error(), "getting block with height") ||
!strings.Contains(err.Error(), "no rows in result set") {
t.Error(errors.Wrap(err, fmt.Sprintf("GetBlock wrong error string: %s", err.Error())))
}
cblock := &walletrpc.CompactBlock{}
err = proto.Unmarshal(storedBlock, cblock)
if err != nil {
@ -196,6 +226,26 @@ func TestSqliteStorage(t *testing.T) {
if count > 0 {
t.Errorf("got some blocks that shouldn't be there")
}
// Test requesting range that's too large
count = 0
go GetBlockRange(timeout, db, blockOut, errOut, 279465, 289465)
recvLoop4:
for {
select {
case <-blockOut:
count++
case err := <-errOut:
if err != ErrLotsOfBlocks {
t.Error(errors.Wrap(err, "in too-large blockrange"))
}
break recvLoop4
}
}
if count > 0 {
t.Errorf("got some blocks that shouldn't be there")
}
}
// Transaction storage
@ -230,7 +280,69 @@ func TestSqliteStorage(t *testing.T) {
if len(storedBytes) != len(tx.Bytes()) {
t.Errorf("Wrong tx size, want %d got %d", len(tx.Bytes()), storedBytes)
}
{
r, err := GetTxByHash(ctx, db, txHash)
if err != nil || !bytes.Equal(r, tx.Bytes()) {
t.Error("GetTxByHash() incorrect return")
}
// nonexistent tx hash
_, err = GetTxByHash(ctx, db, "42")
if err == nil {
t.Fatal(errors.Wrap(err, "GetTxByHash unexpected success"))
}
if !strings.Contains(err.Error(), "getting tx with hash") ||
!strings.Contains(err.Error(), "no rows in result set") {
t.Error(errors.Wrap(err, fmt.Sprintf("GetTxByHash wrong error string: %s", err.Error())))
}
}
{
r, err := GetTxByHeightAndIndex(ctx, db, block.GetHeight(), 0)
if err != nil || !bytes.Equal(r, tx.Bytes()) {
t.Error("GetTxByHeightAndIndex() incorrect return")
}
// nonexistent height
_, err = GetTxByHeightAndIndex(ctx, db, 47, 0)
if err == nil {
t.Fatal(errors.Wrap(err, "GetTxByHeightAndIndex unexpected success"))
}
if !strings.Contains(err.Error(), "getting tx (") ||
!strings.Contains(err.Error(), "no rows in result set") {
t.Error(errors.Wrap(err, fmt.Sprintf("GetTxByHeightAndIndex wrong error string: %s", err.Error())))
}
// nonexistent index
_, err = GetTxByHeightAndIndex(ctx, db, block.GetHeight(), 1)
if err == nil {
t.Fatal(errors.Wrap(err, "GetTxByHeightAndIndex unexpected success"))
}
if !strings.Contains(err.Error(), "getting tx (") ||
!strings.Contains(err.Error(), "no rows in result set") {
t.Error(errors.Wrap(err, fmt.Sprintf("GetTxByHeightAndIndex wrong error string: %s", err.Error())))
}
}
{
r, err := GetTxByHashAndIndex(ctx, db, blockHash, 0)
if err != nil || !bytes.Equal(r, tx.Bytes()) {
t.Error("GetTxByHashAndIndex() incorrect return")
}
// nonexistent block hash
_, err = GetTxByHashAndIndex(ctx, db, "43", 0)
if err == nil {
t.Fatal(errors.Wrap(err, "GetTxByHashAndIndex unexpected success"))
}
if !strings.Contains(err.Error(), "getting tx (") ||
!strings.Contains(err.Error(), "no rows in result set") {
t.Error(errors.Wrap(err, fmt.Sprintf("GetTxByHashAndIndex wrong error string: %s", err.Error())))
}
// nonexistent index
_, err = GetTxByHashAndIndex(ctx, db, blockHash, 1)
if err == nil {
t.Fatal(errors.Wrap(err, "GetTxByHashAndIndex unexpected success"))
}
if !strings.Contains(err.Error(), "getting tx (") ||
!strings.Contains(err.Error(), "no rows in result set") {
t.Error(errors.Wrap(err, fmt.Sprintf("GetTxByHashAndIndex wrong error string: %s", err.Error())))
}
}
}
}