trie: unskip tests

This commit is contained in:
Gary Rong 2024-01-23 16:27:58 +08:00
parent 141cd58b10
commit 46ed534515
3 changed files with 59 additions and 25 deletions

View file

@ -30,13 +30,10 @@ import (
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie" "github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-ethereum/trie/trienode" "github.com/ethereum/go-ethereum/trie/trienode"
<<<<<<< HEAD
"github.com/holiman/uint256"
=======
"github.com/ethereum/go-ethereum/triedb" "github.com/ethereum/go-ethereum/triedb"
"github.com/ethereum/go-ethereum/triedb/hashdb" "github.com/ethereum/go-ethereum/triedb/hashdb"
"github.com/ethereum/go-ethereum/triedb/pathdb" "github.com/ethereum/go-ethereum/triedb/pathdb"
>>>>>>> 0146f2e14f (all: remote the dependency from trie to triedb) "github.com/holiman/uint256"
"golang.org/x/crypto/sha3" "golang.org/x/crypto/sha3"
) )

View file

@ -27,8 +27,6 @@ import (
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie" "github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-ethereum/trie/triedb/hashdb"
"github.com/ethereum/go-ethereum/trie/triedb/pathdb"
"github.com/ethereum/go-ethereum/triedb" "github.com/ethereum/go-ethereum/triedb"
"github.com/ethereum/go-ethereum/triedb/hashdb" "github.com/ethereum/go-ethereum/triedb/hashdb"
"github.com/ethereum/go-ethereum/triedb/pathdb" "github.com/ethereum/go-ethereum/triedb/pathdb"

View file

@ -25,6 +25,7 @@ import (
"io" "io"
"math/rand" "math/rand"
"reflect" "reflect"
"sort"
"testing" "testing"
"testing/quick" "testing/quick"
@ -809,6 +810,8 @@ type spongeDb struct {
sponge hash.Hash sponge hash.Hash
id string id string
journal []string journal []string
keys []string
values map[string]string
} }
func (s *spongeDb) Has(key []byte) (bool, error) { panic("implement me") } func (s *spongeDb) Has(key []byte) (bool, error) { panic("implement me") }
@ -832,12 +835,27 @@ func (s *spongeDb) Put(key []byte, value []byte) error {
valbrief = valbrief[:8] valbrief = valbrief[:8]
} }
s.journal = append(s.journal, fmt.Sprintf("%v: PUT([%x...], [%d bytes] %x...)\n", s.id, keybrief, len(value), valbrief)) s.journal = append(s.journal, fmt.Sprintf("%v: PUT([%x...], [%d bytes] %x...)\n", s.id, keybrief, len(value), valbrief))
if s.values == nil {
s.sponge.Write(key) s.sponge.Write(key)
s.sponge.Write(value) s.sponge.Write(value)
} else {
s.keys = append(s.keys, string(key))
s.values[string(key)] = string(value)
}
return nil return nil
} }
func (s *spongeDb) NewIterator(prefix []byte, start []byte) ethdb.Iterator { panic("implement me") } func (s *spongeDb) NewIterator(prefix []byte, start []byte) ethdb.Iterator { panic("implement me") }
func (s *spongeDb) Flush() {
// Bottom-up, the longest path first
sort.Sort(sort.Reverse(sort.StringSlice(s.keys)))
for _, key := range s.keys {
s.sponge.Write([]byte(key))
s.sponge.Write([]byte(s.values[key]))
}
}
// spongeBatch is a dummy batch which immediately writes to the underlying spongedb // spongeBatch is a dummy batch which immediately writes to the underlying spongedb
type spongeBatch struct { type spongeBatch struct {
db *spongeDb db *spongeDb
@ -858,14 +876,13 @@ func (b *spongeBatch) Replay(w ethdb.KeyValueWriter) error { return nil }
// The test data was based on the 'master' code, and is basically random. It can be used // The test data was based on the 'master' code, and is basically random. It can be used
// to check whether changes to the trie modifies the write order or data in any way. // to check whether changes to the trie modifies the write order or data in any way.
func TestCommitSequence(t *testing.T) { func TestCommitSequence(t *testing.T) {
t.SkipNow()
for i, tc := range []struct { for i, tc := range []struct {
count int count int
expWriteSeqHash []byte expWriteSeqHash []byte
}{ }{
{20, common.FromHex("873c78df73d60e59d4a2bcf3716e8bfe14554549fea2fc147cb54129382a8066")}, {20, common.FromHex("330b0afae2853d96b9f015791fbe0fb7f239bf65f335f16dfc04b76c7536276d")},
{200, common.FromHex("ba03d891bb15408c940eea5ee3d54d419595102648d02774a0268d892add9c8e")}, {200, common.FromHex("5162b3735c06b5d606b043a3ee8adbdbbb408543f4966bca9dcc63da82684eeb")},
{2000, common.FromHex("f7a184f20df01c94f09537401d11e68d97ad0c00115233107f51b9c287ce60c7")}, {2000, common.FromHex("4574cd8e6b17f3fe8ad89140d1d0bf4f1bd7a87a8ac3fb623b33550544c77635")},
} { } {
addresses, accounts := makeAccounts(tc.count) addresses, accounts := makeAccounts(tc.count)
// This spongeDb is used to check the sequence of disk-db-writes // This spongeDb is used to check the sequence of disk-db-writes
@ -890,14 +907,13 @@ func TestCommitSequence(t *testing.T) {
// TestCommitSequenceRandomBlobs is identical to TestCommitSequence // TestCommitSequenceRandomBlobs is identical to TestCommitSequence
// but uses random blobs instead of 'accounts' // but uses random blobs instead of 'accounts'
func TestCommitSequenceRandomBlobs(t *testing.T) { func TestCommitSequenceRandomBlobs(t *testing.T) {
t.SkipNow()
for i, tc := range []struct { for i, tc := range []struct {
count int count int
expWriteSeqHash []byte expWriteSeqHash []byte
}{ }{
{20, common.FromHex("8e4a01548551d139fa9e833ebc4e66fc1ba40a4b9b7259d80db32cff7b64ebbc")}, {20, common.FromHex("8016650c7a50cf88485fd06cde52d634a89711051107f00d21fae98234f2f13d")},
{200, common.FromHex("6869b4e7b95f3097a19ddb30ff735f922b915314047e041614df06958fc50554")}, {200, common.FromHex("dde92ca9812e068e6982d04b40846dc65a61a9fd4996fc0f55f2fde172a8e13c")},
{2000, common.FromHex("444200e6f4e2df49f77752f629a96ccf7445d4698c164f962bbd85a0526ef424")}, {2000, common.FromHex("ab553a7f9aff82e3929c382908e30ef7dd17a332933e92ba3fe873fc661ef382")},
} { } {
prng := rand.New(rand.NewSource(int64(i))) prng := rand.New(rand.NewSource(int64(i)))
// This spongeDb is used to check the sequence of disk-db-writes // This spongeDb is used to check the sequence of disk-db-writes
@ -930,21 +946,29 @@ func TestCommitSequenceRandomBlobs(t *testing.T) {
} }
func TestCommitSequenceStackTrie(t *testing.T) { func TestCommitSequenceStackTrie(t *testing.T) {
t.SkipNow()
for count := 1; count < 200; count++ { for count := 1; count < 200; count++ {
prng := rand.New(rand.NewSource(int64(count))) prng := rand.New(rand.NewSource(int64(count)))
// This spongeDb is used to check the sequence of disk-db-writes // This spongeDb is used to check the sequence of disk-db-writes
s := &spongeDb{sponge: sha3.NewLegacyKeccak256(), id: "a"} s := &spongeDb{
sponge: sha3.NewLegacyKeccak256(),
id: "a",
values: make(map[string]string),
}
db := newTestDatabase(rawdb.NewDatabase(s), rawdb.HashScheme) db := newTestDatabase(rawdb.NewDatabase(s), rawdb.HashScheme)
trie := NewEmpty(db) trie := NewEmpty(db)
// Another sponge is used for the stacktrie commits
stackTrieSponge := &spongeDb{sponge: sha3.NewLegacyKeccak256(), id: "b"}
// Another sponge is used for the stacktrie commits
stackTrieSponge := &spongeDb{
sponge: sha3.NewLegacyKeccak256(),
id: "b",
values: make(map[string]string),
}
options := NewStackTrieOptions() options := NewStackTrieOptions()
options = options.WithWriter(func(path []byte, hash common.Hash, blob []byte) { options = options.WithWriter(func(path []byte, hash common.Hash, blob []byte) {
rawdb.WriteTrieNode(stackTrieSponge, common.Hash{}, path, hash, blob, db.Scheme()) rawdb.WriteTrieNode(stackTrieSponge, common.Hash{}, path, hash, blob, db.Scheme())
}) })
stTrie := NewStackTrie(options) stTrie := NewStackTrie(options)
// Fill the trie with elements // Fill the trie with elements
for i := 0; i < count; i++ { for i := 0; i < count; i++ {
// For the stack trie, we need to do inserts in proper order // For the stack trie, we need to do inserts in proper order
@ -966,11 +990,14 @@ func TestCommitSequenceStackTrie(t *testing.T) {
// Flush memdb -> disk (sponge) // Flush memdb -> disk (sponge)
db.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes)) db.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes))
db.Commit(root) db.Commit(root)
s.Flush()
// And flush stacktrie -> disk // And flush stacktrie -> disk
stRoot := stTrie.Commit() stRoot := stTrie.Commit()
if stRoot != root { if stRoot != root {
t.Fatalf("root wrong, got %x exp %x", stRoot, root) t.Fatalf("root wrong, got %x exp %x", stRoot, root)
} }
stackTrieSponge.Flush()
if got, exp := stackTrieSponge.sponge.Sum(nil), s.sponge.Sum(nil); !bytes.Equal(got, exp) { if got, exp := stackTrieSponge.sponge.Sum(nil), s.sponge.Sum(nil); !bytes.Equal(got, exp) {
// Show the journal // Show the journal
t.Logf("Expected:") t.Logf("Expected:")
@ -993,35 +1020,47 @@ func TestCommitSequenceStackTrie(t *testing.T) {
// that even a small trie which contains a leaf will have an extension making it // that even a small trie which contains a leaf will have an extension making it
// not fit into 32 bytes, rlp-encoded. However, it's still the correct thing to do. // not fit into 32 bytes, rlp-encoded. However, it's still the correct thing to do.
func TestCommitSequenceSmallRoot(t *testing.T) { func TestCommitSequenceSmallRoot(t *testing.T) {
t.SkipNow() s := &spongeDb{
s := &spongeDb{sponge: sha3.NewLegacyKeccak256(), id: "a"} sponge: sha3.NewLegacyKeccak256(),
id: "a",
values: make(map[string]string),
}
db := newTestDatabase(rawdb.NewDatabase(s), rawdb.HashScheme) db := newTestDatabase(rawdb.NewDatabase(s), rawdb.HashScheme)
trie := NewEmpty(db) trie := NewEmpty(db)
// Another sponge is used for the stacktrie commits
stackTrieSponge := &spongeDb{sponge: sha3.NewLegacyKeccak256(), id: "b"}
// Another sponge is used for the stacktrie commits
stackTrieSponge := &spongeDb{
sponge: sha3.NewLegacyKeccak256(),
id: "b",
values: make(map[string]string),
}
options := NewStackTrieOptions() options := NewStackTrieOptions()
options = options.WithWriter(func(path []byte, hash common.Hash, blob []byte) { options = options.WithWriter(func(path []byte, hash common.Hash, blob []byte) {
rawdb.WriteTrieNode(stackTrieSponge, common.Hash{}, path, hash, blob, db.Scheme()) rawdb.WriteTrieNode(stackTrieSponge, common.Hash{}, path, hash, blob, db.Scheme())
}) })
stTrie := NewStackTrie(options) stTrie := NewStackTrie(options)
// Add a single small-element to the trie(s) // Add a single small-element to the trie(s)
key := make([]byte, 5) key := make([]byte, 5)
key[0] = 1 key[0] = 1
trie.Update(key, []byte{0x1}) trie.Update(key, []byte{0x1})
stTrie.Update(key, []byte{0x1}) stTrie.Update(key, []byte{0x1})
// Flush trie -> database // Flush trie -> database
root, nodes, _ := trie.Commit(false) root, nodes, _ := trie.Commit(false)
// Flush memdb -> disk (sponge) // Flush memdb -> disk (sponge)
db.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes)) db.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes))
db.Commit(root) db.Commit(root)
// And flush stacktrie -> disk // And flush stacktrie -> disk
stRoot := stTrie.Commit() stRoot := stTrie.Commit()
if stRoot != root { if stRoot != root {
t.Fatalf("root wrong, got %x exp %x", stRoot, root) t.Fatalf("root wrong, got %x exp %x", stRoot, root)
} }
t.Logf("root: %x\n", stRoot) t.Logf("root: %x\n", stRoot)
s.Flush()
stackTrieSponge.Flush()
if got, exp := stackTrieSponge.sponge.Sum(nil), s.sponge.Sum(nil); !bytes.Equal(got, exp) { if got, exp := stackTrieSponge.sponge.Sum(nil), s.sponge.Sum(nil); !bytes.Equal(got, exp) {
t.Fatalf("test, disk write sequence wrong:\ngot %x exp %x\n", got, exp) t.Fatalf("test, disk write sequence wrong:\ngot %x exp %x\n", got, exp)
} }