2023-06-23 12:42:55 +00:00
|
|
|
package test_helpers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-09-22 08:44:35 +00:00
|
|
|
"fmt"
|
|
|
|
"math/big"
|
2023-06-23 12:42:55 +00:00
|
|
|
"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-22 08:44:35 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
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
|
|
|
|
}
|
|
|
|
|
2023-09-22 08:44:35 +00:00
|
|
|
type CheckedRoots map[*types.Block][]byte
|
|
|
|
|
|
|
|
// Replicates the statediff object, but indexes nodes by CID
|
|
|
|
type normalizedStateDiff struct {
|
|
|
|
BlockNumber *big.Int
|
|
|
|
BlockHash common.Hash
|
|
|
|
Nodes map[string]sdtypes.StateLeafNode
|
|
|
|
IPLDs map[string]sdtypes.IPLD
|
|
|
|
}
|
2023-06-23 12:42:55 +00:00
|
|
|
|
|
|
|
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,
|
2023-09-22 08:44:35 +00:00
|
|
|
subtrieCounts []uint,
|
2023-06-23 12:42:55 +00:00
|
|
|
) {
|
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-22 08:44:35 +00:00
|
|
|
for _, subtries := range subtrieCounts {
|
|
|
|
t.Run(fmt.Sprintf("%s with %d subtries", test.Name, subtries), func(t *testing.T) {
|
|
|
|
builder.SetSubtrieWorkers(subtries)
|
|
|
|
diff, err := builder.BuildStateDiffObject(test.Args, params)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
require.Equal(t,
|
|
|
|
normalize(test.Expected),
|
|
|
|
normalize(&diff),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
2023-06-23 12:42:55 +00:00
|
|
|
}
|
2023-09-22 08:44:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (roots CheckedRoots) Check(t *testing.T) {
|
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
|
|
|
|
2023-09-22 08:44:35 +00:00
|
|
|
func normalize(diff *sdtypes.StateObject) normalizedStateDiff {
|
|
|
|
norm := normalizedStateDiff{
|
|
|
|
BlockNumber: diff.BlockNumber,
|
|
|
|
BlockHash: diff.BlockHash,
|
|
|
|
Nodes: make(map[string]sdtypes.StateLeafNode),
|
|
|
|
IPLDs: make(map[string]sdtypes.IPLD),
|
|
|
|
}
|
2023-09-20 03:22:15 +00:00
|
|
|
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
|
|
|
|
})
|
2023-09-22 08:44:35 +00:00
|
|
|
norm.Nodes[node.AccountWrapper.CID] = node
|
|
|
|
}
|
|
|
|
for _, ipld := range diff.IPLDs {
|
|
|
|
norm.IPLDs[ipld.CID] = ipld
|
2023-09-20 03:22:15 +00:00
|
|
|
}
|
2023-09-22 08:44:35 +00:00
|
|
|
return norm
|
2023-09-20 03:22:15 +00:00
|
|
|
}
|