Concurrent statediff iteration #12

Merged
roysc merged 14 commits from use-concurrent-iterator into main 2023-09-22 08:44:38 +00:00
2 changed files with 29 additions and 16 deletions
Showing only changes of commit 00fbebb4b3 - Show all commits

View File

@ -566,7 +566,9 @@ func TestBuilderOnMainnetBlocks(t *testing.T) {
}, },
StorageDiff: emptyStorage, StorageDiff: emptyStorage,
}, },
{ // this is the new account created due to the coinbase mining a block, it's creation shouldn't affect 0x 0e 05 07 {
// this is the new account created due to the coinbase mining a block, its
// creation shouldn't affect 0x 0e 05 07
Removed: false, Removed: false,
AccountWrapper: sdtypes.AccountWrapper{ AccountWrapper: sdtypes.AccountWrapper{
Account: block3CoinbaseAccount, Account: block3CoinbaseAccount,

View File

@ -3,12 +3,14 @@ package test_helpers
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"math/big"
"sort" "sort"
"testing" "testing"
statediff "github.com/cerc-io/plugeth-statediff" statediff "github.com/cerc-io/plugeth-statediff"
"github.com/cerc-io/plugeth-statediff/adapt" "github.com/cerc-io/plugeth-statediff/adapt"
sdtypes "github.com/cerc-io/plugeth-statediff/types" sdtypes "github.com/cerc-io/plugeth-statediff/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
@ -23,6 +25,14 @@ type TestCase struct {
type CheckedRoots map[*types.Block][]byte type CheckedRoots map[*types.Block][]byte
// Recapitulates 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
}
func RunBuilderTests( func RunBuilderTests(
t *testing.T, t *testing.T,
sdb state.Database, sdb state.Database,
@ -39,10 +49,10 @@ func RunBuilderTests(
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
require.Equal(t,
normalize(test.Expected) normalize(test.Expected),
normalize(&diff) normalize(&diff),
require.Equal(t, *test.Expected, diff) )
}) })
} }
} }
@ -56,17 +66,13 @@ func (roots CheckedRoots) Check(t *testing.T) {
} }
} }
// Sorts contained state nodes, storage nodes, and IPLDs func normalize(diff *sdtypes.StateObject) normalizedStateDiff {
func normalize(diff *sdtypes.StateObject) { norm := normalizedStateDiff{
sort.Slice(diff.IPLDs, func(i, j int) bool { BlockNumber: diff.BlockNumber,
return diff.IPLDs[i].CID < diff.IPLDs[j].CID BlockHash: diff.BlockHash,
}) Nodes: make(map[string]sdtypes.StateLeafNode),
sort.Slice(diff.Nodes, func(i, j int) bool { IPLDs: make(map[string]sdtypes.IPLD),
return bytes.Compare( }
diff.Nodes[i].AccountWrapper.LeafKey,
diff.Nodes[j].AccountWrapper.LeafKey,
) < 0
})
for _, node := range diff.Nodes { for _, node := range diff.Nodes {
sort.Slice(node.StorageDiff, func(i, j int) bool { sort.Slice(node.StorageDiff, func(i, j int) bool {
return bytes.Compare( return bytes.Compare(
@ -74,5 +80,10 @@ func normalize(diff *sdtypes.StateObject) {
node.StorageDiff[j].LeafKey, node.StorageDiff[j].LeafKey,
) < 0 ) < 0
}) })
norm.Nodes[node.AccountWrapper.CID] = node
} }
for _, ipld := range diff.IPLDs {
norm.IPLDs[ipld.CID] = ipld
}
return norm
} }