diff --git a/storage/sqlite3.go b/storage/sqlite3.go index 4bda13b..1dfe5a5 100644 --- a/storage/sqlite3.go +++ b/storage/sqlite3.go @@ -5,11 +5,10 @@ import "database/sql" func createBlockTable(conn *sql.DB) error { tableCreation := ` CREATE TABLE IF NOT EXISTS blocks ( - height INTEGER, + height INTEGER PRIMARY KEY, hash TEXT, has_sapling_tx BOOL, - compact_encoding BLOB, - PRIMARY KEY (height, hash) + compact_encoding BLOB ); ` _, err := conn.Exec(tableCreation) diff --git a/storage/sqlite3_test.go b/storage/sqlite3_test.go index d6438ad..1a4b552 100644 --- a/storage/sqlite3_test.go +++ b/storage/sqlite3_test.go @@ -2,13 +2,10 @@ package storage import ( "database/sql" - "encoding/base64" "encoding/hex" "encoding/json" "fmt" "io/ioutil" - "log" - "os" "testing" protobuf "github.com/golang/protobuf/proto" @@ -17,24 +14,6 @@ import ( "github.com/pkg/errors" ) -func TestMain(m *testing.M) { - conn, err := sql.Open("sqlite3", "testdata/blocks.db") - if err != nil { - log.Fatal(err) - } - - err = createBlockTable(conn) - if err != nil { - conn.Close() - log.Fatal(err) - } - - ret := m.Run() - conn.Close() - //os.Remove("testdata/blocks.db") - os.Exit(ret) -} - func TestFillDB(t *testing.T) { type compactTest struct { BlockHeight int `json:"block"` @@ -54,11 +33,15 @@ func TestFillDB(t *testing.T) { t.Fatal(err) } - conn, err := sql.Open("sqlite3", "testdata/blocks.db") + conn, err := sql.Open("sqlite3", ":memory:") if err != nil { t.Fatal(err) } defer conn.Close() + err = createBlockTable(conn) + if err != nil { + t.Fatal(err) + } for _, test := range compactTests { blockData, _ := hex.DecodeString(test.Full) @@ -73,13 +56,23 @@ func TestFillDB(t *testing.T) { hash := hex.EncodeToString(block.GetHash()) hasSapling := block.HasSaplingTransactions() marshaled, _ := protobuf.Marshal(block.ToCompact()) - compactB64 := base64.RawStdEncoding.EncodeToString(marshaled) insertBlock := "INSERT INTO blocks (height, hash, has_sapling_tx, compact_encoding) values (?, ?, ?, ?)" - _, err := conn.Exec(insertBlock, height, hash, hasSapling, compactB64) + _, err := conn.Exec(insertBlock, height, hash, hasSapling, marshaled) if err != nil { t.Error(errors.Wrap(err, fmt.Sprintf("storing compact block %d", height))) continue } } + + var count int + countBlocks := "SELECT count(*) FROM blocks" + conn.QueryRow(countBlocks).Scan(&count) + if err != nil { + t.Error(errors.Wrap(err, fmt.Sprintf("counting compact blocks"))) + } + + if count != len(compactTests) { + t.Errorf("Wrong row count, want %d got %d", len(compactTests), count) + } } diff --git a/storage/testdata/blocks.db b/storage/testdata/blocks.db deleted file mode 100644 index 8899e5f..0000000 Binary files a/storage/testdata/blocks.db and /dev/null differ