Test NodeBlob
This commit is contained in:
parent
3625e22949
commit
3f0e36c0a3
@ -23,6 +23,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/core/rawdb"
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
||||||
"github.com/ethereum/go-ethereum/statediff/indexer/database/sql/postgres"
|
"github.com/ethereum/go-ethereum/statediff/indexer/database/sql/postgres"
|
||||||
geth_trie "github.com/ethereum/go-ethereum/trie"
|
geth_trie "github.com/ethereum/go-ethereum/trie"
|
||||||
@ -41,18 +42,26 @@ var (
|
|||||||
trieConfig = trie.Config{Cache: 256}
|
trieConfig = trie.Config{Cache: 256}
|
||||||
|
|
||||||
ctx = context.Background()
|
ctx = context.Background()
|
||||||
)
|
|
||||||
|
|
||||||
var testdata1 = []kvs{
|
testdata0 = []kvs{
|
||||||
{"barb", 0},
|
{"one", 1},
|
||||||
{"bard", 1},
|
{"two", 2},
|
||||||
{"bars", 2},
|
{"three", 3},
|
||||||
{"bar", 3},
|
{"four", 4},
|
||||||
{"fab", 4},
|
{"five", 5},
|
||||||
{"food", 5},
|
{"ten", 10},
|
||||||
{"foos", 6},
|
}
|
||||||
{"foo", 7},
|
testdata1 = []kvs{
|
||||||
}
|
{"barb", 0},
|
||||||
|
{"bard", 1},
|
||||||
|
{"bars", 2},
|
||||||
|
{"bar", 3},
|
||||||
|
{"fab", 4},
|
||||||
|
{"food", 5},
|
||||||
|
{"foos", 6},
|
||||||
|
{"foo", 7},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
func TestEmptyIterator(t *testing.T) {
|
func TestEmptyIterator(t *testing.T) {
|
||||||
trie := trie.NewEmpty(trie.NewDatabase(rawdb.NewMemoryDatabase()))
|
trie := trie.NewEmpty(trie.NewDatabase(rawdb.NewMemoryDatabase()))
|
||||||
@ -71,15 +80,7 @@ func TestIterator(t *testing.T) {
|
|||||||
edb := rawdb.NewMemoryDatabase()
|
edb := rawdb.NewMemoryDatabase()
|
||||||
db := geth_trie.NewDatabase(edb)
|
db := geth_trie.NewDatabase(edb)
|
||||||
origtrie := geth_trie.NewEmpty(db)
|
origtrie := geth_trie.NewEmpty(db)
|
||||||
vals := []kvs{
|
all, err := updateTrie(origtrie, testdata0)
|
||||||
{"one", 1},
|
|
||||||
{"two", 2},
|
|
||||||
{"three", 3},
|
|
||||||
{"four", 4},
|
|
||||||
{"five", 5},
|
|
||||||
{"ten", 10},
|
|
||||||
}
|
|
||||||
all, err := updateTrie(origtrie, vals)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -103,22 +104,6 @@ func TestIterator(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkIteratorOrder(want []kvs, it *trie.Iterator) error {
|
|
||||||
for it.Next() {
|
|
||||||
if len(want) == 0 {
|
|
||||||
return fmt.Errorf("didn't expect any more values, got key %q", it.Key)
|
|
||||||
}
|
|
||||||
if !bytes.Equal(it.Key, []byte(want[0].k)) {
|
|
||||||
return fmt.Errorf("wrong key: got %q, want %q", it.Key, want[0].k)
|
|
||||||
}
|
|
||||||
want = want[1:]
|
|
||||||
}
|
|
||||||
if len(want) > 0 {
|
|
||||||
return fmt.Errorf("iterator ended early, want key %q", want[0])
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestIteratorSeek(t *testing.T) {
|
func TestIteratorSeek(t *testing.T) {
|
||||||
edb := rawdb.NewMemoryDatabase()
|
edb := rawdb.NewMemoryDatabase()
|
||||||
db := geth_trie.NewDatabase(edb)
|
db := geth_trie.NewDatabase(edb)
|
||||||
@ -148,11 +133,56 @@ func TestIteratorSeek(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns a cache config with unique name (groupcache names are global)
|
func checkIteratorOrder(want []kvs, it *trie.Iterator) error {
|
||||||
func makeCacheConfig(t testing.TB) pgipfsethdb.CacheConfig {
|
for it.Next() {
|
||||||
return pgipfsethdb.CacheConfig{
|
if len(want) == 0 {
|
||||||
Name: t.Name(),
|
return fmt.Errorf("didn't expect any more values, got key %q", it.Key)
|
||||||
Size: 3000000, // 3MB
|
}
|
||||||
ExpiryDuration: time.Hour,
|
if !bytes.Equal(it.Key, []byte(want[0].k)) {
|
||||||
|
return fmt.Errorf("wrong key: got %q, want %q", it.Key, want[0].k)
|
||||||
|
}
|
||||||
|
want = want[1:]
|
||||||
|
}
|
||||||
|
if len(want) > 0 {
|
||||||
|
return fmt.Errorf("iterator ended early, want key %q", want[0])
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIteratorNodeBlob(t *testing.T) {
|
||||||
|
edb := rawdb.NewMemoryDatabase()
|
||||||
|
db := geth_trie.NewDatabase(edb)
|
||||||
|
orig := geth_trie.NewEmpty(geth_trie.NewDatabase(rawdb.NewMemoryDatabase()))
|
||||||
|
if _, err := updateTrie(orig, testdata1); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
root := commitTrie(t, db, orig)
|
||||||
|
trie := indexTrie(t, edb, root)
|
||||||
|
|
||||||
|
found := make(map[common.Hash][]byte)
|
||||||
|
it := trie.NodeIterator(nil)
|
||||||
|
for it.Next(true) {
|
||||||
|
if it.Hash() == (common.Hash{}) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
found[it.Hash()] = it.NodeBlob()
|
||||||
|
}
|
||||||
|
|
||||||
|
dbIter := edb.NewIterator(nil, nil)
|
||||||
|
defer dbIter.Release()
|
||||||
|
|
||||||
|
var count int
|
||||||
|
for dbIter.Next() {
|
||||||
|
got, present := found[common.BytesToHash(dbIter.Key())]
|
||||||
|
if !present {
|
||||||
|
t.Fatalf("Missing trie node %v", dbIter.Key())
|
||||||
|
}
|
||||||
|
if !bytes.Equal(got, dbIter.Value()) {
|
||||||
|
t.Fatalf("Unexpected trie node want %v got %v", dbIter.Value(), got)
|
||||||
|
}
|
||||||
|
count += 1
|
||||||
|
}
|
||||||
|
if count != len(found) {
|
||||||
|
t.Fatal("Find extra trie node via iterator")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"math/big"
|
"math/big"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
|
|
||||||
@ -170,3 +171,12 @@ func TearDownDB(db *sqlx.DB) error {
|
|||||||
}
|
}
|
||||||
return tx.Commit()
|
return tx.Commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// returns a cache config with unique name (groupcache names are global)
|
||||||
|
func makeCacheConfig(t testing.TB) pgipfsethdb.CacheConfig {
|
||||||
|
return pgipfsethdb.CacheConfig{
|
||||||
|
Name: t.Name(),
|
||||||
|
Size: 3000000, // 3MB
|
||||||
|
ExpiryDuration: time.Hour,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user