mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 02:42:27 +00:00
eth/protocols/snap: improve tests
This commit is contained in:
parent
e24280f847
commit
e635c8c0a8
1 changed files with 163 additions and 25 deletions
|
|
@ -63,7 +63,9 @@ func (r *replayer) decode(key []byte, value []byte) {
|
|||
}
|
||||
}
|
||||
|
||||
func (r *replayer) updates() map[string]common.Hash {
|
||||
// updates returns a set of effective mutations. Multiple mutations targeting
|
||||
// the same node path will be merged in FIFO order.
|
||||
func (r *replayer) modifies() map[string]common.Hash {
|
||||
set := make(map[string]common.Hash)
|
||||
for i, path := range r.paths {
|
||||
set[path] = r.hashes[i]
|
||||
|
|
@ -71,6 +73,18 @@ func (r *replayer) updates() map[string]common.Hash {
|
|||
return set
|
||||
}
|
||||
|
||||
// updates returns the number of updates.
|
||||
func (r *replayer) updates() int {
|
||||
var count int
|
||||
for _, hash := range r.modifies() {
|
||||
if hash == (common.Hash{}) {
|
||||
continue
|
||||
}
|
||||
count++
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
// Put inserts the given value into the key-value data store.
|
||||
func (r *replayer) Put(key []byte, value []byte) error {
|
||||
r.decode(key, value)
|
||||
|
|
@ -95,7 +109,7 @@ func byteToHex(str []byte) []byte {
|
|||
|
||||
// innerNodes returns the internal nodes narrowed by two boundaries along with
|
||||
// the leftmost and rightmost sub-trie roots.
|
||||
func innerNodes(first, last []byte, nodes map[string]common.Hash, t *testing.T) (map[string]common.Hash, []byte, []byte) {
|
||||
func innerNodes(first, last []byte, includeLeft, includeRight bool, nodes map[string]common.Hash, t *testing.T) (map[string]common.Hash, []byte, []byte) {
|
||||
var (
|
||||
leftRoot []byte
|
||||
rightRoot []byte
|
||||
|
|
@ -108,11 +122,11 @@ func innerNodes(first, last []byte, nodes map[string]common.Hash, t *testing.T)
|
|||
t.Fatalf("Unexpected deletion, %v", []byte(path))
|
||||
}
|
||||
// Filter out the siblings on the left side or the left boundary nodes.
|
||||
if bytes.Compare(firstHex, []byte(path)) > 0 || bytes.HasPrefix(firstHex, []byte(path)) {
|
||||
if !includeLeft && (bytes.Compare(firstHex, []byte(path)) > 0 || bytes.HasPrefix(firstHex, []byte(path))) {
|
||||
continue
|
||||
}
|
||||
// Filter out the siblings on the right side or the right boundary nodes.
|
||||
if bytes.Compare(lastHex, []byte(path)) < 0 || bytes.HasPrefix(lastHex, []byte(path)) {
|
||||
if !includeRight && (bytes.Compare(lastHex, []byte(path)) < 0 || bytes.HasPrefix(lastHex, []byte(path))) {
|
||||
continue
|
||||
}
|
||||
inner[path] = hash
|
||||
|
|
@ -180,8 +194,98 @@ func TestPartialGentree(t *testing.T) {
|
|||
db = rawdb.NewMemoryDatabase()
|
||||
batch = db.NewBatch()
|
||||
)
|
||||
inner, leftRoot, rightRoot := innerNodes(entries[first].k, entries[last].k, nodes, t)
|
||||
// Build the partial tree with specific boundaries
|
||||
r := buildPartial(common.Hash{}, db, batch, entries, first, last)
|
||||
if r.unknowns > 0 {
|
||||
t.Fatalf("Unknown database write: %d", r.unknowns)
|
||||
}
|
||||
|
||||
// Ensure all the internal nodes are produced
|
||||
var (
|
||||
set = r.modifies()
|
||||
inner, _, _ = innerNodes(entries[first].k, entries[last].k, first == 0, last == len(entries)-1, nodes, t)
|
||||
)
|
||||
for path, hash := range inner {
|
||||
if _, ok := set[path]; !ok {
|
||||
t.Fatalf("Missing nodes %v", []byte(path))
|
||||
}
|
||||
if hash != set[path] {
|
||||
t.Fatalf("Inconsistent node, want %x, got: %x", hash, set[path])
|
||||
}
|
||||
}
|
||||
if r.updates() != len(inner) {
|
||||
t.Fatalf("Unexpected node write detected, want: %d, got: %d", len(inner), r.updates())
|
||||
}
|
||||
}
|
||||
for j := 0; j < 100; j++ {
|
||||
var (
|
||||
first int
|
||||
last int
|
||||
)
|
||||
for {
|
||||
first = rand.Intn(len(entries))
|
||||
last = rand.Intn(len(entries))
|
||||
if first <= last {
|
||||
break
|
||||
}
|
||||
}
|
||||
check(first, last)
|
||||
}
|
||||
var cases = []struct {
|
||||
first int
|
||||
last int
|
||||
}{
|
||||
{0, len(entries) - 1}, // full
|
||||
{1, len(entries) - 1}, // no left
|
||||
{2, len(entries) - 1}, // no left
|
||||
{2, len(entries) - 2}, // no left and right
|
||||
{2, len(entries) - 2}, // no left and right
|
||||
{len(entries) / 2, len(entries) / 2}, // single
|
||||
{0, 0}, // single first
|
||||
{len(entries) - 1, len(entries) - 1}, // single last
|
||||
}
|
||||
for _, c := range cases {
|
||||
check(c.first, c.last)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestGentreeDanglingClearing tests if the dangling nodes falling within the
|
||||
// path space of constructed tree can be correctly removed.
|
||||
func TestGentreeDanglingClearing(t *testing.T) {
|
||||
for round := 0; round < 100; round++ {
|
||||
var (
|
||||
n = rand.Intn(1024) + 10
|
||||
entries []*kv
|
||||
)
|
||||
for i := 0; i < n; i++ {
|
||||
var val []byte
|
||||
if rand.Intn(3) == 0 {
|
||||
val = testrand.Bytes(3)
|
||||
} else {
|
||||
val = testrand.Bytes(32)
|
||||
}
|
||||
entries = append(entries, &kv{
|
||||
k: testrand.Bytes(32),
|
||||
v: val,
|
||||
})
|
||||
}
|
||||
slices.SortFunc(entries, (*kv).cmp)
|
||||
|
||||
nodes := make(map[string]common.Hash)
|
||||
tr := trie.NewStackTrie(func(path []byte, hash common.Hash, blob []byte) {
|
||||
nodes[string(path)] = hash
|
||||
})
|
||||
for i := 0; i < len(entries); i++ {
|
||||
tr.Update(entries[i].k, entries[i].v)
|
||||
}
|
||||
tr.Hash()
|
||||
|
||||
check := func(first, last int) {
|
||||
var (
|
||||
db = rawdb.NewMemoryDatabase()
|
||||
batch = db.NewBatch()
|
||||
)
|
||||
// Write the junk nodes as the dangling
|
||||
var injects []string
|
||||
for path := range nodes {
|
||||
|
|
@ -193,27 +297,23 @@ func TestPartialGentree(t *testing.T) {
|
|||
injects = append(injects, path[:i])
|
||||
}
|
||||
}
|
||||
if len(injects) == 0 {
|
||||
return
|
||||
}
|
||||
for _, path := range injects {
|
||||
rawdb.WriteAccountTrieNode(db, []byte(path), testrand.Bytes(32))
|
||||
}
|
||||
|
||||
// Build the partial tree with specific range
|
||||
replay := buildPartial(common.Hash{}, db, batch, entries, first, last)
|
||||
if replay.unknowns > 0 {
|
||||
t.Fatalf("Unknown database write: %d", replay.unknowns)
|
||||
}
|
||||
set := replay.modifies()
|
||||
|
||||
// Ensure all the internal nodes are produced
|
||||
set := replay.updates()
|
||||
for path, hash := range inner {
|
||||
if _, ok := set[path]; !ok {
|
||||
t.Fatalf("Missing nodes %v", []byte(path))
|
||||
}
|
||||
if hash != set[path] {
|
||||
t.Fatalf("Inconsistent node, want %x, got: %x", hash, set[path])
|
||||
}
|
||||
}
|
||||
|
||||
// Make sure all injected junks are correctly deleted
|
||||
// Make sure the injected junks falling within the path space of
|
||||
// committed trie nodes are correctly deleted.
|
||||
_, leftRoot, rightRoot := innerNodes(entries[first].k, entries[last].k, first == 0, last == len(entries)-1, nodes, t)
|
||||
for _, path := range injects {
|
||||
if bytes.Compare([]byte(path), leftRoot) < 0 && !bytes.HasPrefix(leftRoot, []byte(path)) {
|
||||
continue
|
||||
|
|
@ -260,7 +360,7 @@ func TestPartialGentree(t *testing.T) {
|
|||
}
|
||||
|
||||
// TestFlushPartialTree tests the gentrie can produce complete inner trie nodes
|
||||
// even with lots of batch flushes in the middle.
|
||||
// even with lots of batch flushes.
|
||||
func TestFlushPartialTree(t *testing.T) {
|
||||
var entries []*kv
|
||||
for i := 0; i < 1024; i++ {
|
||||
|
|
@ -305,7 +405,7 @@ func TestFlushPartialTree(t *testing.T) {
|
|||
batch = db.NewBatch()
|
||||
combined = db.NewBatch()
|
||||
)
|
||||
inner, _, _ := innerNodes(entries[c.first].k, entries[c.last].k, nodes, t)
|
||||
inner, _, _ := innerNodes(entries[c.first].k, entries[c.last].k, c.first == 0, c.last == len(entries)-1, nodes, t)
|
||||
|
||||
tr := newPathTrie(common.Hash{}, c.first != 0, db, batch)
|
||||
for i := c.first; i <= c.last; i++ {
|
||||
|
|
@ -328,7 +428,7 @@ func TestFlushPartialTree(t *testing.T) {
|
|||
combined.Replay(r)
|
||||
|
||||
// Ensure all the internal nodes are produced
|
||||
set := r.updates()
|
||||
set := r.modifies()
|
||||
for path, hash := range inner {
|
||||
if _, ok := set[path]; !ok {
|
||||
t.Fatalf("Missing nodes %v", []byte(path))
|
||||
|
|
@ -337,6 +437,9 @@ func TestFlushPartialTree(t *testing.T) {
|
|||
t.Fatalf("Inconsistent node, want %x, got: %x", hash, set[path])
|
||||
}
|
||||
}
|
||||
if r.updates() != len(inner) {
|
||||
t.Fatalf("Unexpected node write detected, want: %d, got: %d", len(inner), r.updates())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -373,20 +476,20 @@ func TestBoundSplit(t *testing.T) {
|
|||
last = rand.Intn(len(entries)-next) + next
|
||||
|
||||
r := buildPartial(common.Hash{}, db, db.NewBatch(), entries, next, last)
|
||||
updates := r.updates()
|
||||
set := r.modifies()
|
||||
|
||||
// Skip if the chunk is zero-size
|
||||
if len(updates) == 0 {
|
||||
if r.updates() == 0 {
|
||||
next = last + 1
|
||||
continue
|
||||
}
|
||||
|
||||
// Ensure the updates in two consecutive chunks are not overlapped.
|
||||
// The only overlapping part should be deletion.
|
||||
if lastRightRoot != nil && len(updates) > 0 {
|
||||
if lastRightRoot != nil && len(set) > 0 {
|
||||
// Derive the path of left-most node in this chunk
|
||||
var leftRoot []byte
|
||||
for path, hash := range r.updates() {
|
||||
for path, hash := range r.modifies() {
|
||||
if hash == (common.Hash{}) {
|
||||
t.Fatalf("Unexpected deletion %v", []byte(path))
|
||||
}
|
||||
|
|
@ -401,7 +504,7 @@ func TestBoundSplit(t *testing.T) {
|
|||
|
||||
// Track the updates as the last chunk
|
||||
var rightRoot []byte
|
||||
for path := range updates {
|
||||
for path := range set {
|
||||
if rightRoot == nil ||
|
||||
(bytes.Compare(rightRoot, []byte(path)) < 0) ||
|
||||
(bytes.Compare(rightRoot, []byte(path)) > 0 && bytes.HasPrefix(rightRoot, []byte(path))) {
|
||||
|
|
@ -413,3 +516,38 @@ func TestBoundSplit(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestTinyPartialTree tests if the partial tree is too tiny(has less than two
|
||||
// states), then nothing should be committed.
|
||||
func TestTinyPartialTree(t *testing.T) {
|
||||
var entries []*kv
|
||||
for i := 0; i < 1024; i++ {
|
||||
var val []byte
|
||||
if rand.Intn(3) == 0 {
|
||||
val = testrand.Bytes(3)
|
||||
} else {
|
||||
val = testrand.Bytes(32)
|
||||
}
|
||||
entries = append(entries, &kv{
|
||||
k: testrand.Bytes(32),
|
||||
v: val,
|
||||
})
|
||||
}
|
||||
slices.SortFunc(entries, (*kv).cmp)
|
||||
|
||||
for i := 0; i < len(entries); i++ {
|
||||
next := i
|
||||
last := i + 1
|
||||
if last >= len(entries) {
|
||||
last = len(entries) - 1
|
||||
}
|
||||
db := rawdb.NewMemoryDatabase()
|
||||
r := buildPartial(common.Hash{}, db, db.NewBatch(), entries, next, last)
|
||||
|
||||
if next != 0 && last != len(entries)-1 {
|
||||
if r.updates() != 0 {
|
||||
t.Fatalf("Unexpected data writes, got: %d", r.updates())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue