2019-08-22 09:47:24 +00:00
|
|
|
// Copyright 2019 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package dbtest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"reflect"
|
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/ethdb"
|
2019-08-22 09:47:24 +00:00
|
|
|
)
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
type dataStore interface {
|
|
|
|
ethdb.Database
|
|
|
|
}
|
|
|
|
|
2019-08-22 09:47:24 +00:00
|
|
|
// TestDatabaseSuite runs a suite of tests against a KeyValueStore database
|
|
|
|
// implementation.
|
2019-05-27 13:51:49 +00:00
|
|
|
func TestDatabaseSuite(t *testing.T, New func() dataStore) {
|
2019-08-22 09:47:24 +00:00
|
|
|
t.Run("Iterator", func(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
content map[string]string
|
|
|
|
prefix string
|
|
|
|
order []string
|
|
|
|
}{
|
|
|
|
// Empty databases should be iterable
|
|
|
|
{map[string]string{}, "", nil},
|
|
|
|
{map[string]string{}, "non-existent-prefix", nil},
|
|
|
|
|
|
|
|
// Single-item databases should be iterable
|
|
|
|
{map[string]string{"key": "val"}, "", []string{"key"}},
|
|
|
|
{map[string]string{"key": "val"}, "k", []string{"key"}},
|
|
|
|
{map[string]string{"key": "val"}, "l", nil},
|
|
|
|
|
|
|
|
// Multi-item databases should be fully iterable
|
|
|
|
{
|
|
|
|
map[string]string{"k1": "v1", "k5": "v5", "k2": "v2", "k4": "v4", "k3": "v3"},
|
|
|
|
"",
|
|
|
|
[]string{"k1", "k2", "k3", "k4", "k5"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
map[string]string{"k1": "v1", "k5": "v5", "k2": "v2", "k4": "v4", "k3": "v3"},
|
|
|
|
"k",
|
|
|
|
[]string{"k1", "k2", "k3", "k4", "k5"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
map[string]string{"k1": "v1", "k5": "v5", "k2": "v2", "k4": "v4", "k3": "v3"},
|
|
|
|
"l",
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
// Multi-item databases should be prefix-iterable
|
|
|
|
{
|
|
|
|
map[string]string{
|
|
|
|
"ka1": "va1", "ka5": "va5", "ka2": "va2", "ka4": "va4", "ka3": "va3",
|
|
|
|
"kb1": "vb1", "kb5": "vb5", "kb2": "vb2", "kb4": "vb4", "kb3": "vb3",
|
|
|
|
},
|
|
|
|
"ka",
|
|
|
|
[]string{"ka1", "ka2", "ka3", "ka4", "ka5"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
map[string]string{
|
|
|
|
"ka1": "va1", "ka5": "va5", "ka2": "va2", "ka4": "va4", "ka3": "va3",
|
|
|
|
"kb1": "vb1", "kb5": "vb5", "kb2": "vb2", "kb4": "vb4", "kb3": "vb3",
|
|
|
|
},
|
|
|
|
"kc",
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for i, tt := range tests {
|
|
|
|
// Create the key-value data store
|
|
|
|
db := New()
|
|
|
|
for key, val := range tt.content {
|
2019-05-27 13:51:49 +00:00
|
|
|
if err := db.Put(nil, []byte(key), []byte(val)); err != nil {
|
2019-08-22 09:47:24 +00:00
|
|
|
t.Fatalf("test %d: failed to insert item %s:%s into database: %v", i, key, val, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Iterate over the database with the given configs and verify the results
|
2019-05-27 13:51:49 +00:00
|
|
|
idx := 0
|
|
|
|
err := db.Walk(nil, []byte(tt.prefix), 0, func(key, val []byte) (bool, error) {
|
2019-08-22 09:47:24 +00:00
|
|
|
if len(tt.order) <= idx {
|
2019-05-27 13:51:49 +00:00
|
|
|
t.Errorf("test %d: prefix=%q more items than expected: checking idx=%d (key %q), expecting len=%d", i, tt.prefix, idx, key, len(tt.order))
|
|
|
|
return false, nil
|
2019-08-22 09:47:24 +00:00
|
|
|
}
|
2019-05-27 13:51:49 +00:00
|
|
|
if !bytes.Equal(key, []byte(tt.order[idx])) {
|
|
|
|
t.Errorf("test %d: item %d: key mismatch: have %s, want %s", i, idx, string(key), tt.order[idx])
|
2019-08-22 09:47:24 +00:00
|
|
|
}
|
2019-05-27 13:51:49 +00:00
|
|
|
if !bytes.Equal(val, []byte(tt.content[tt.order[idx]])) {
|
|
|
|
t.Errorf("test %d: item %d: value mismatch: have %s, want %s", i, idx, string(val), tt.content[tt.order[idx]])
|
2019-08-22 09:47:24 +00:00
|
|
|
}
|
|
|
|
idx++
|
2019-05-27 13:51:49 +00:00
|
|
|
return true, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2019-08-22 09:47:24 +00:00
|
|
|
t.Errorf("test %d: iteration failed: %v", i, err)
|
|
|
|
}
|
|
|
|
if idx != len(tt.order) {
|
|
|
|
t.Errorf("test %d: iteration terminated prematurely: have %d, want %d", i, idx, len(tt.order))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("IteratorWith", func(t *testing.T) {
|
|
|
|
db := New()
|
|
|
|
|
|
|
|
keys := []string{"1", "2", "3", "4", "6", "10", "11", "12", "20", "21", "22"}
|
|
|
|
sort.Strings(keys) // 1, 10, 11, etc
|
|
|
|
|
|
|
|
for _, k := range keys {
|
2019-05-27 13:51:49 +00:00
|
|
|
if err := db.Put(nil, []byte(k), nil); err != nil {
|
2019-08-22 09:47:24 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-05-27 13:51:49 +00:00
|
|
|
got, want := iterateKeys(db), keys
|
2019-08-22 09:47:24 +00:00
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("Iterator: got: %s; want: %s", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-05-27 13:51:49 +00:00
|
|
|
got, want := iterateKeysFromKey(db, []byte("1")), []string{"1", "10", "11", "12"}
|
2019-08-22 09:47:24 +00:00
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("IteratorWithPrefix(1): got: %s; want: %s", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-05-27 13:51:49 +00:00
|
|
|
got, want := iterateKeysFromKey(db, []byte("5")), []string{}
|
2019-08-22 09:47:24 +00:00
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("IteratorWithPrefix(1): got: %s; want: %s", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-05-27 13:51:49 +00:00
|
|
|
got, want := iterateKeysFromKey(db, []byte("2")), []string{"2", "20", "21", "22", "3", "4", "6"}
|
2019-08-22 09:47:24 +00:00
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("IteratorWithStart(2): got: %s; want: %s", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-05-27 13:51:49 +00:00
|
|
|
got, want := iterateKeysFromKey(db, []byte("5")), []string{"6"}
|
2019-08-22 09:47:24 +00:00
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("IteratorWithStart(2): got: %s; want: %s", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("KeyValueOperations", func(t *testing.T) {
|
|
|
|
db := New()
|
|
|
|
|
|
|
|
key := []byte("foo")
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
if got, err := db.Has(nil, key); err != nil {
|
2019-08-22 09:47:24 +00:00
|
|
|
t.Error(err)
|
|
|
|
} else if got {
|
|
|
|
t.Errorf("wrong value: %t", got)
|
|
|
|
}
|
|
|
|
|
|
|
|
value := []byte("hello world")
|
2019-05-27 13:51:49 +00:00
|
|
|
if err := db.Put(nil, key, value); err != nil {
|
2019-08-22 09:47:24 +00:00
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
if got, err := db.Has(nil, key); err != nil {
|
2019-08-22 09:47:24 +00:00
|
|
|
t.Error(err)
|
|
|
|
} else if !got {
|
|
|
|
t.Errorf("wrong value: %t", got)
|
|
|
|
}
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
if got, err := db.Get(nil, key); err != nil {
|
2019-08-22 09:47:24 +00:00
|
|
|
t.Error(err)
|
|
|
|
} else if !bytes.Equal(got, value) {
|
|
|
|
t.Errorf("wrong value: %q", got)
|
|
|
|
}
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
if err := db.Delete(nil, key); err != nil {
|
2019-08-22 09:47:24 +00:00
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
if got, err := db.Has(nil, key); err != nil {
|
2019-08-22 09:47:24 +00:00
|
|
|
t.Error(err)
|
|
|
|
} else if got {
|
|
|
|
t.Errorf("wrong value: %t", got)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Batch", func(t *testing.T) {
|
|
|
|
db := New()
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
b := db.NewBatch()
|
|
|
|
for _, k := range []string{"1", "2", "3", "4"} {
|
2019-05-27 13:51:49 +00:00
|
|
|
if err := b.Put(nil, []byte(k), nil); err != nil {
|
2019-08-22 09:47:24 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
if has, err := db.Has(nil, []byte("1")); err != nil {
|
2019-08-22 09:47:24 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
} else if has {
|
|
|
|
t.Error("db contains element before batch write")
|
|
|
|
}
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
if _, err := b.Commit(); err != nil {
|
2019-08-22 09:47:24 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-05-27 13:51:49 +00:00
|
|
|
if got, want := iterateKeys(db), []string{"1", "2", "3", "4"}; !reflect.DeepEqual(got, want) {
|
2019-08-22 09:47:24 +00:00
|
|
|
t.Errorf("got: %s; want: %s", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
b = db.NewBatch()
|
2019-08-22 09:47:24 +00:00
|
|
|
|
|
|
|
// Mix writes and deletes in batch
|
2019-05-27 13:51:49 +00:00
|
|
|
b.Put(nil, []byte("5"), nil)
|
|
|
|
b.Delete(nil, []byte("1"))
|
|
|
|
b.Put(nil, []byte("6"), nil)
|
|
|
|
b.Delete(nil, []byte("3"))
|
|
|
|
b.Put(nil, []byte("3"), nil)
|
2019-08-22 09:47:24 +00:00
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
if _, err := b.Commit(); err != nil {
|
2019-08-22 09:47:24 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-05-27 13:51:49 +00:00
|
|
|
if got, want := iterateKeys(db), []string{"2", "3", "4", "5", "6"}; !reflect.DeepEqual(got, want) {
|
2019-08-22 09:47:24 +00:00
|
|
|
t.Errorf("got: %s; want: %s", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
2019-08-22 09:47:24 +00:00
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
func iterateKeys(db ethdb.Database) []string {
|
|
|
|
return iterateKeysFromKey(db, []byte{})
|
2019-08-22 09:47:24 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
func iterateKeysFromKey(db ethdb.Database, fromKey []byte) []string {
|
2019-08-22 09:47:24 +00:00
|
|
|
keys := []string{}
|
2019-05-27 13:51:49 +00:00
|
|
|
db.Walk(nil, fromKey, 0, func(key, value []byte) (bool, error) {
|
|
|
|
keys = append(keys, string(key))
|
|
|
|
return true, nil
|
|
|
|
})
|
2019-08-22 09:47:24 +00:00
|
|
|
sort.Strings(keys)
|
|
|
|
return keys
|
|
|
|
}
|