2023-06-23 12:42:55 +00:00
|
|
|
package test_helpers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
|
2023-07-19 06:35:54 +00:00
|
|
|
statediff "github.com/cerc-io/plugeth-statediff"
|
2023-06-23 12:42:55 +00:00
|
|
|
sdtypes "github.com/cerc-io/plugeth-statediff/types"
|
|
|
|
"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,
|
|
|
|
builder statediff.Builder,
|
|
|
|
tests []TestCase,
|
|
|
|
params statediff.Params,
|
|
|
|
roots CheckedRoots,
|
|
|
|
) {
|
|
|
|
for _, test := range tests {
|
|
|
|
diff, err := builder.BuildStateDiffObject(test.Args, params)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2023-07-19 06:35:54 +00:00
|
|
|
normalize(test.Expected)
|
|
|
|
normalize(&diff)
|
|
|
|
require.Equal(t, *test.Expected, diff, test.Name)
|
2023-06-23 12:42:55 +00:00
|
|
|
}
|
|
|
|
// 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())
|
|
|
|
}
|
|
|
|
}
|
2023-07-19 06:35:54 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|