2023-06-23 12:42:55 +00:00
|
|
|
package test_helpers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
|
2023-09-20 03:22:15 +00:00
|
|
|
statediff "github.com/cerc-io/plugeth-statediff"
|
|
|
|
"github.com/cerc-io/plugeth-statediff/adapt"
|
2023-06-23 12:42:55 +00:00
|
|
|
sdtypes "github.com/cerc-io/plugeth-statediff/types"
|
2023-09-20 03:22:15 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
2023-06-23 12:42:55 +00:00
|
|
|
"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,
|
2023-09-20 03:22:15 +00:00
|
|
|
sdb state.Database,
|
2023-06-23 12:42:55 +00:00
|
|
|
tests []TestCase,
|
|
|
|
params statediff.Params,
|
|
|
|
roots CheckedRoots,
|
|
|
|
) {
|
2023-09-20 03:22:15 +00:00
|
|
|
builder := statediff.NewBuilder(adapt.GethStateView(sdb))
|
2023-06-23 12:42:55 +00:00
|
|
|
for _, test := range tests {
|
2023-09-20 03:22:15 +00:00
|
|
|
t.Run(test.Name, func(t *testing.T) {
|
|
|
|
diff, err := builder.BuildStateDiffObject(test.Args, params)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2023-06-23 12:42:55 +00:00
|
|
|
|
2023-09-20 03:22:15 +00:00
|
|
|
normalize(test.Expected)
|
|
|
|
normalize(&diff)
|
|
|
|
require.Equal(t, *test.Expected, diff)
|
|
|
|
})
|
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-09-20 03:22:15 +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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|