plugeth-statediff/test_helpers/builder.go
Roy Crihfield 981bfb5895
All checks were successful
Test / Run unit tests (push) Successful in 17m53s
Test / Run integration tests (push) Successful in 37m51s
Use symmetric difference iterator (#11)
Implements https://github.com/cerc-io/go-ethereum/issues/319

With this we can perform a single pass to process updates.

Also
* refactor code structure around single-pass iteration
* refactor builder metrics to match new set of functions
* fix unit tests by running sequentially
* update ipld-eth-db in compose
* factor out fixture data into external module
* some CI updates

Reviewed-on: #11
2023-09-20 03:22:15 +00:00

72 lines
1.8 KiB
Go

package test_helpers
import (
"bytes"
"sort"
"testing"
statediff "github.com/cerc-io/plugeth-statediff"
"github.com/cerc-io/plugeth-statediff/adapt"
sdtypes "github.com/cerc-io/plugeth-statediff/types"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/require"
)
type TestCase struct {
Name string
Args statediff.Args
Expected *sdtypes.StateObject
}
type CheckedRoots = map[*types.Block][]byte
func RunBuilderTests(
t *testing.T,
sdb state.Database,
tests []TestCase,
params statediff.Params,
roots CheckedRoots,
) {
builder := statediff.NewBuilder(adapt.GethStateView(sdb))
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
diff, err := builder.BuildStateDiffObject(test.Args, params)
if err != nil {
t.Error(err)
}
normalize(test.Expected)
normalize(&diff)
require.Equal(t, *test.Expected, diff)
})
}
// Let's also confirm that our root state nodes form the state root hash in the headers
for block, node := range roots {
require.Equal(t, block.Root(), crypto.Keccak256Hash(node),
"expected root does not match actual root", block.Number())
}
}
// Sorts contained state nodes, storage nodes, and IPLDs
func normalize(diff *sdtypes.StateObject) {
sort.Slice(diff.IPLDs, func(i, j int) bool {
return diff.IPLDs[i].CID < diff.IPLDs[j].CID
})
sort.Slice(diff.Nodes, func(i, j int) bool {
return bytes.Compare(
diff.Nodes[i].AccountWrapper.LeafKey,
diff.Nodes[j].AccountWrapper.LeafKey,
) < 0
})
for _, node := range diff.Nodes {
sort.Slice(node.StorageDiff, func(i, j int) bool {
return bytes.Compare(
node.StorageDiff[i].LeafKey,
node.StorageDiff[j].LeafKey,
) < 0
})
}
}