eth-statediff-service/pkg/builder.go

156 lines
5.0 KiB
Go
Raw Normal View History

2020-09-04 16:50:49 +00:00
// Copyright 2019 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// Contains a batch of utility type declarations used by the tests. As the node
// operates on unique types, a lot of them are needed to check various features.
package statediff
import (
"fmt"
2020-09-15 06:03:46 +00:00
"math/bits"
"sync"
2020-09-04 16:50:49 +00:00
"github.com/ethereum/go-ethereum/core/state"
sd "github.com/ethereum/go-ethereum/statediff"
sdtypes "github.com/ethereum/go-ethereum/statediff/types"
2021-10-25 19:06:05 +00:00
"github.com/sirupsen/logrus"
2021-10-14 06:47:38 +00:00
iter "github.com/cerc-io/go-eth-state-node-iterator"
2020-09-04 16:50:49 +00:00
)
type builder struct {
sd.StateDiffBuilder
2020-09-15 03:46:50 +00:00
numWorkers uint
2020-09-04 16:50:49 +00:00
}
// NewBuilder is used to create a statediff builder
func NewBuilder(stateCache state.Database, workers uint) (sd.Builder, error) {
2020-09-15 03:46:50 +00:00
if workers == 0 {
workers = 1
}
2020-09-15 06:03:46 +00:00
if bits.OnesCount(workers) != 1 {
return nil, fmt.Errorf("workers must be a power of 2")
}
2020-09-04 16:50:49 +00:00
return &builder{
StateDiffBuilder: sd.StateDiffBuilder{
StateCache: stateCache,
},
2020-09-15 03:46:50 +00:00
numWorkers: workers,
2020-09-15 06:03:46 +00:00
}, nil
2020-09-04 16:50:49 +00:00
}
// BuildStateDiffObject builds a statediff object from two blocks and the provided parameters
func (sdb *builder) BuildStateDiffObject(args sd.Args, params sd.Params) (sdtypes.StateObject, error) {
var stateNodes []sdtypes.StateNode
var codeAndCodeHashes []sdtypes.CodeAndCodeHash
err := sdb.WriteStateDiffObject(
sdtypes.StateRoots{OldStateRoot: args.OldStateRoot, NewStateRoot: args.NewStateRoot},
params, sd.StateNodeAppender(&stateNodes), sd.CodeMappingAppender(&codeAndCodeHashes))
if err != nil {
return sdtypes.StateObject{}, err
}
return sdtypes.StateObject{
BlockHash: args.BlockHash,
BlockNumber: args.BlockNumber,
Nodes: stateNodes,
CodeAndCodeHashes: codeAndCodeHashes,
}, nil
}
2021-10-25 19:06:05 +00:00
// WriteStateDiffObject writes a statediff object to output callback
func (sdb *builder) WriteStateDiffObject(args sdtypes.StateRoots, params sd.Params, output sdtypes.StateNodeSink, codeOutput sdtypes.CodeSink) error {
2020-09-04 16:50:49 +00:00
// Load tries for old and new states
oldTrie, err := sdb.StateCache.OpenTrie(args.OldStateRoot)
2020-09-04 16:50:49 +00:00
if err != nil {
return fmt.Errorf("error creating trie for oldStateRoot: %v", err)
2020-09-04 16:50:49 +00:00
}
newTrie, err := sdb.StateCache.OpenTrie(args.NewStateRoot)
2020-09-04 16:50:49 +00:00
if err != nil {
return fmt.Errorf("error creating trie for newStateRoot: %v", err)
2020-09-04 16:50:49 +00:00
}
2020-09-06 18:09:25 +00:00
// Split old and new tries into corresponding subtrie iterators
oldIters1 := iter.SubtrieIterators(oldTrie, sdb.numWorkers)
oldIters2 := iter.SubtrieIterators(oldTrie, sdb.numWorkers)
newIters1 := iter.SubtrieIterators(newTrie, sdb.numWorkers)
newIters2 := iter.SubtrieIterators(newTrie, sdb.numWorkers)
2020-09-06 18:09:25 +00:00
2020-09-06 17:52:48 +00:00
// Create iterators ahead of time to avoid race condition in state.Trie access
// We do two state iterations per subtrie: one for new/updated nodes,
// one for deleted/updated nodes; prepare 2 iterator instances for each task
var iterPairs [][]sd.IterPair
2020-09-15 03:46:50 +00:00
for i := uint(0); i < sdb.numWorkers; i++ {
iterPairs = append(iterPairs, []sd.IterPair{
{Older: oldIters1[i], Newer: newIters1[i]},
{Older: oldIters2[i], Newer: newIters2[i]},
})
2020-09-04 16:50:49 +00:00
}
// Dispatch workers to process trie data; sync and collect results here via channels
nodeChan := make(chan sdtypes.StateNode)
codeChan := make(chan sdtypes.CodeAndCodeHash)
go func() {
nodeSender := func(node sdtypes.StateNode) error { nodeChan <- node; return nil }
codeSender := func(code sdtypes.CodeAndCodeHash) error { codeChan <- code; return nil }
var wg sync.WaitGroup
for w := uint(0); w < sdb.numWorkers; w++ {
wg.Add(1)
go func(worker uint) {
defer wg.Done()
var err error
if !params.IntermediateStateNodes {
err = sdb.BuildStateDiffWithoutIntermediateStateNodes(iterPairs[worker], params, nodeSender, codeSender)
} else {
err = sdb.BuildStateDiffWithIntermediateStateNodes(iterPairs[worker], params, nodeSender, codeSender)
}
if err != nil {
logrus.Errorf("buildStateDiff error for worker %d, params %+v", worker, params)
2021-10-25 19:06:05 +00:00
}
}(w)
}
wg.Wait()
close(nodeChan)
close(codeChan)
}()
2020-09-06 18:09:25 +00:00
for nodeChan != nil || codeChan != nil {
select {
case node, more := <-nodeChan:
if more {
if err := output(node); err != nil {
2020-09-06 18:09:25 +00:00
return err
}
} else {
nodeChan = nil
}
case codeAndCodeHash, more := <-codeChan:
if more {
if err := codeOutput(codeAndCodeHash); err != nil {
return err
}
} else {
codeChan = nil
2020-09-06 18:09:25 +00:00
}
}
2020-09-04 16:50:49 +00:00
}
return nil
2020-09-04 16:50:49 +00:00
}