2023-07-22 07:09:26 +00:00
|
|
|
package utils_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-09-06 12:13:32 +00:00
|
|
|
"github.com/cerc-io/eth-test-data/fixture/mainnet"
|
|
|
|
"github.com/ethereum/go-ethereum/consensus/ethash"
|
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2023-07-22 07:09:26 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2023-09-06 12:13:32 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
2023-07-22 07:09:26 +00:00
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-09-06 12:13:32 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/cerc-io/plugeth-statediff/test_helpers"
|
|
|
|
"github.com/cerc-io/plugeth-statediff/utils"
|
2023-07-22 07:09:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type kvs struct{ k, v string }
|
|
|
|
|
|
|
|
var (
|
|
|
|
testdata1 = []kvs{
|
|
|
|
{"barb", "ba"},
|
|
|
|
{"bard", "bc"},
|
|
|
|
{"bars", "bb"},
|
2023-08-01 14:50:59 +00:00
|
|
|
{"bar", "b"},
|
2023-07-22 07:09:26 +00:00
|
|
|
{"fab", "z"},
|
|
|
|
{"food", "ab"},
|
2023-08-01 14:50:59 +00:00
|
|
|
{"foo", "a"},
|
2023-07-22 07:09:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
testdata2 = []kvs{
|
|
|
|
{"aardvark", "c"},
|
|
|
|
{"bar", "b"},
|
|
|
|
{"barb", "bd"},
|
|
|
|
{"bars", "be"},
|
|
|
|
{"fab", "z"},
|
|
|
|
{"foo", "a"},
|
|
|
|
{"foos", "aa"},
|
|
|
|
{"jars", "d"},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSymmetricDifferenceIterator(t *testing.T) {
|
|
|
|
t.Run("with no difference", func(t *testing.T) {
|
|
|
|
db := trie.NewDatabase(rawdb.NewMemoryDatabase())
|
2023-08-01 14:50:59 +00:00
|
|
|
triea := trie.NewEmpty(db)
|
|
|
|
di, count := utils.NewSymmetricDifferenceIterator(triea.NodeIterator(nil), triea.NodeIterator(nil))
|
2023-07-22 07:09:26 +00:00
|
|
|
for di.Next(true) {
|
|
|
|
t.Errorf("iterator should not yield any elements")
|
|
|
|
}
|
|
|
|
assert.Equal(t, 0, *count)
|
|
|
|
|
2023-08-01 14:50:59 +00:00
|
|
|
triea.MustUpdate([]byte("foo"), []byte("bar"))
|
|
|
|
di, count = utils.NewSymmetricDifferenceIterator(triea.NodeIterator(nil), triea.NodeIterator(nil))
|
2023-07-22 07:09:26 +00:00
|
|
|
for di.Next(true) {
|
|
|
|
t.Errorf("iterator should not yield any elements")
|
|
|
|
}
|
2023-09-06 12:12:59 +00:00
|
|
|
assert.Equal(t, 2, *count)
|
2023-08-01 14:50:59 +00:00
|
|
|
|
|
|
|
trieb := trie.NewEmpty(db)
|
2023-09-06 12:13:21 +00:00
|
|
|
di, count = utils.NewSymmetricDifferenceIterator(
|
|
|
|
triea.NodeIterator([]byte("jars")),
|
|
|
|
trieb.NodeIterator(nil))
|
2023-08-01 14:50:59 +00:00
|
|
|
for di.Next(true) {
|
2023-09-06 16:52:50 +00:00
|
|
|
t.Errorf("iterator should not yield any elements, but got key %s", di.Path())
|
2023-08-01 14:50:59 +00:00
|
|
|
}
|
|
|
|
assert.Equal(t, 0, *count)
|
2023-09-06 12:13:21 +00:00
|
|
|
|
2023-09-06 16:52:50 +00:00
|
|
|
// // TODO will fail until merged: https://github.com/ethereum/go-ethereum/pull/27838
|
2023-09-06 12:13:21 +00:00
|
|
|
// di, count = utils.NewSymmetricDifferenceIterator(
|
|
|
|
// triea.NodeIterator([]byte("food")),
|
|
|
|
// trieb.NodeIterator(nil))
|
|
|
|
// for di.Next(true) {
|
2023-09-06 16:52:50 +00:00
|
|
|
// t.Errorf("iterator should not yield any elements, but got key %s", di.Path())
|
2023-09-06 12:13:21 +00:00
|
|
|
// }
|
|
|
|
// assert.Equal(t, 0, *count)
|
2023-07-24 13:46:09 +00:00
|
|
|
})
|
|
|
|
|
2023-08-01 14:50:59 +00:00
|
|
|
t.Run("small difference", func(t *testing.T) {
|
2023-07-24 13:46:09 +00:00
|
|
|
dba := trie.NewDatabase(rawdb.NewMemoryDatabase())
|
|
|
|
triea := trie.NewEmpty(dba)
|
|
|
|
|
|
|
|
dbb := trie.NewDatabase(rawdb.NewMemoryDatabase())
|
|
|
|
trieb := trie.NewEmpty(dbb)
|
|
|
|
trieb.MustUpdate([]byte("foo"), []byte("bar"))
|
|
|
|
|
|
|
|
di, count := utils.NewSymmetricDifferenceIterator(triea.NodeIterator(nil), trieb.NodeIterator(nil))
|
2023-08-01 14:50:59 +00:00
|
|
|
leaves := 0
|
2023-07-24 13:46:09 +00:00
|
|
|
for di.Next(true) {
|
|
|
|
if di.Leaf() {
|
|
|
|
assert.False(t, di.CommonPath())
|
2023-08-01 14:50:59 +00:00
|
|
|
assert.Equal(t, "foo", string(di.LeafKey()))
|
|
|
|
assert.Equal(t, "bar", string(di.LeafBlob()))
|
|
|
|
leaves++
|
2023-07-24 13:46:09 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-01 14:50:59 +00:00
|
|
|
assert.Equal(t, 1, leaves)
|
2023-07-24 13:46:09 +00:00
|
|
|
assert.Equal(t, 2, *count)
|
2023-08-01 14:50:59 +00:00
|
|
|
|
|
|
|
trieb.MustUpdate([]byte("quux"), []byte("bars"))
|
|
|
|
di, count = utils.NewSymmetricDifferenceIterator(triea.NodeIterator(nil), trieb.NodeIterator([]byte("quux")))
|
|
|
|
leaves = 0
|
|
|
|
for di.Next(true) {
|
|
|
|
if di.Leaf() {
|
|
|
|
assert.False(t, di.CommonPath())
|
|
|
|
assert.Equal(t, "quux", string(di.LeafKey()))
|
|
|
|
assert.Equal(t, "bars", string(di.LeafBlob()))
|
|
|
|
leaves++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert.Equal(t, 1, leaves)
|
|
|
|
assert.Equal(t, 1, *count)
|
2023-07-22 07:09:26 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
dba := trie.NewDatabase(rawdb.NewMemoryDatabase())
|
|
|
|
triea := trie.NewEmpty(dba)
|
|
|
|
for _, val := range testdata1 {
|
|
|
|
triea.MustUpdate([]byte(val.k), []byte(val.v))
|
|
|
|
}
|
|
|
|
dbb := trie.NewDatabase(rawdb.NewMemoryDatabase())
|
|
|
|
trieb := trie.NewEmpty(dbb)
|
|
|
|
for _, val := range testdata2 {
|
|
|
|
trieb.MustUpdate([]byte(val.k), []byte(val.v))
|
|
|
|
}
|
|
|
|
|
|
|
|
onlyA := make(map[string]string)
|
|
|
|
onlyB := make(map[string]string)
|
2023-07-24 13:46:09 +00:00
|
|
|
var deletions, creations []string
|
2023-07-22 07:09:26 +00:00
|
|
|
it, _ := utils.NewSymmetricDifferenceIterator(triea.NodeIterator(nil), trieb.NodeIterator(nil))
|
|
|
|
for it.Next(true) {
|
|
|
|
if !it.Leaf() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
key, value := string(it.LeafKey()), string(it.LeafBlob())
|
|
|
|
if it.FromA() {
|
|
|
|
onlyA[key] = value
|
2023-07-24 13:46:09 +00:00
|
|
|
if !it.CommonPath() {
|
|
|
|
deletions = append(deletions, key)
|
|
|
|
}
|
2023-07-22 07:09:26 +00:00
|
|
|
} else {
|
|
|
|
onlyB[key] = value
|
2023-07-24 13:46:09 +00:00
|
|
|
if !it.CommonPath() {
|
|
|
|
creations = append(creations, key)
|
|
|
|
}
|
2023-07-22 07:09:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedOnlyA := map[string]string{
|
|
|
|
"barb": "ba",
|
|
|
|
"bard": "bc",
|
|
|
|
"bars": "bb",
|
|
|
|
"food": "ab",
|
|
|
|
}
|
|
|
|
expectedOnlyB := map[string]string{
|
|
|
|
"aardvark": "c",
|
|
|
|
"barb": "bd",
|
|
|
|
"bars": "be",
|
|
|
|
"foos": "aa",
|
|
|
|
"jars": "d",
|
|
|
|
}
|
2023-07-24 13:46:09 +00:00
|
|
|
expectedDeletions := []string{
|
|
|
|
"bard",
|
|
|
|
"food",
|
|
|
|
}
|
|
|
|
expectedCreations := []string{
|
|
|
|
"aardvark",
|
|
|
|
"foos",
|
|
|
|
"jars",
|
|
|
|
}
|
2023-07-22 07:09:26 +00:00
|
|
|
assert.Equal(t, expectedOnlyA, onlyA)
|
|
|
|
assert.Equal(t, expectedOnlyB, onlyB)
|
2023-07-24 13:46:09 +00:00
|
|
|
assert.Equal(t, expectedDeletions, deletions)
|
|
|
|
assert.Equal(t, expectedCreations, creations)
|
2023-07-22 07:09:26 +00:00
|
|
|
}
|
2023-09-06 12:13:32 +00:00
|
|
|
|
|
|
|
// compare the paths traversed by the geth difference iterator and symmetric difference iterator
|
|
|
|
// within a sample of mainnet data.
|
|
|
|
func TestCompareDifferenceIterators(t *testing.T) {
|
|
|
|
test_helpers.SilenceLogs()
|
|
|
|
|
|
|
|
db := rawdb.NewMemoryDatabase()
|
|
|
|
core.DefaultGenesisBlock().MustCommit(db)
|
|
|
|
blocks := mainnet.LoadBlocks(2)
|
|
|
|
chain, _ := core.NewBlockChain(db, nil, nil, nil, ethash.NewFaker(), vm.Config{}, nil, nil)
|
|
|
|
_, err := chain.InsertChain(blocks[1:])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
treeA, err := chain.StateCache().OpenTrie(blocks[1].Root())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
treeB, err := chain.StateCache().OpenTrie(blocks[2].Root())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// collect the paths of nodes exclusive to A and B separately, then make sure the symmetric
|
|
|
|
// iterator produces the same sets
|
|
|
|
var pathsA, pathsB [][]byte
|
|
|
|
itBonly, _ := trie.NewDifferenceIterator(treeA.NodeIterator(nil), treeB.NodeIterator(nil))
|
|
|
|
for itBonly.Next(true) {
|
|
|
|
pathsB = append(pathsB, itBonly.Path())
|
|
|
|
}
|
|
|
|
itAonly, _ := trie.NewDifferenceIterator(treeB.NodeIterator(nil), treeA.NodeIterator(nil))
|
|
|
|
for itAonly.Next(true) {
|
|
|
|
pathsA = append(pathsA, itAonly.Path())
|
|
|
|
}
|
|
|
|
|
|
|
|
itSym, _ := utils.NewSymmetricDifferenceIterator(treeA.NodeIterator(nil), treeB.NodeIterator(nil))
|
|
|
|
var idxA, idxB int
|
|
|
|
for itSym.Next(true) {
|
|
|
|
if itSym.FromA() {
|
|
|
|
require.Equal(t, pathsA[idxA], itSym.Path())
|
|
|
|
idxA++
|
|
|
|
} else {
|
|
|
|
require.Equal(t, pathsB[idxB], itSym.Path())
|
|
|
|
idxB++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.Equal(t, len(pathsA), idxA)
|
|
|
|
require.Equal(t, len(pathsB), idxB)
|
|
|
|
}
|