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"
|
2020-11-08 23:25:22 +00:00
|
|
|
"sync"
|
2020-09-04 16:50:49 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
2023-03-13 22:35:54 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2020-11-17 01:37:36 +00:00
|
|
|
sd "github.com/ethereum/go-ethereum/statediff"
|
|
|
|
sdtypes "github.com/ethereum/go-ethereum/statediff/types"
|
2023-03-31 14:04:54 +00:00
|
|
|
iter "github.com/ethereum/go-ethereum/trie/concurrent_iterator"
|
2021-10-25 19:06:05 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-09-04 16:50:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type builder struct {
|
2022-06-24 08:42:58 +00:00
|
|
|
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
|
2022-06-24 08:42:58 +00:00
|
|
|
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{
|
2022-06-24 08:42:58 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-11-10 09:07:59 +00:00
|
|
|
// BuildStateDiffObject builds a statediff object from two blocks and the provided parameters
|
2021-11-13 00:45:08 +00:00
|
|
|
func (sdb *builder) BuildStateDiffObject(args sd.Args, params sd.Params) (sdtypes.StateObject, error) {
|
2023-04-09 16:01:05 +00:00
|
|
|
var stateNodes []sdtypes.StateLeafNode
|
|
|
|
var codeAndCodeHashes []sdtypes.IPLD
|
2020-11-10 09:07:59 +00:00
|
|
|
err := sdb.WriteStateDiffObject(
|
2023-03-13 22:35:54 +00:00
|
|
|
args,
|
2023-04-09 16:01:05 +00:00
|
|
|
params, sd.StateNodeAppender(&stateNodes), sd.IPLDMappingAppender(&codeAndCodeHashes))
|
2020-11-10 09:07:59 +00:00
|
|
|
if err != nil {
|
2021-11-13 00:45:08 +00:00
|
|
|
return sdtypes.StateObject{}, err
|
2020-11-10 09:07:59 +00:00
|
|
|
}
|
2021-11-13 00:45:08 +00:00
|
|
|
return sdtypes.StateObject{
|
2023-04-09 16:01:05 +00:00
|
|
|
BlockHash: args.BlockHash,
|
|
|
|
BlockNumber: args.BlockNumber,
|
|
|
|
Nodes: stateNodes,
|
|
|
|
IPLDs: codeAndCodeHashes,
|
2020-11-10 09:07:59 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-10-25 19:06:05 +00:00
|
|
|
// WriteStateDiffObject writes a statediff object to output callback
|
2023-04-09 16:01:05 +00:00
|
|
|
func (sdb *builder) WriteStateDiffObject(args sd.Args, params sd.Params, output sdtypes.StateNodeSink, codeOutput sdtypes.IPLDSink) error {
|
2020-09-04 16:50:49 +00:00
|
|
|
// Load tries for old and new states
|
2022-06-24 08:42:58 +00:00
|
|
|
oldTrie, err := sdb.StateCache.OpenTrie(args.OldStateRoot)
|
2020-09-04 16:50:49 +00:00
|
|
|
if err != nil {
|
2022-05-11 09:51:23 +00:00
|
|
|
return fmt.Errorf("error creating trie for oldStateRoot: %v", err)
|
2020-09-04 16:50:49 +00:00
|
|
|
}
|
2022-06-24 08:42:58 +00:00
|
|
|
newTrie, err := sdb.StateCache.OpenTrie(args.NewStateRoot)
|
2020-09-04 16:50:49 +00:00
|
|
|
if err != nil {
|
2022-05-11 09:51:23 +00:00
|
|
|
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
|
2023-05-15 16:34:22 +00:00
|
|
|
oldIters1 := iter.SubtrieIterators(oldTrie.NodeIterator, sdb.numWorkers)
|
|
|
|
oldIters2 := iter.SubtrieIterators(oldTrie.NodeIterator, sdb.numWorkers)
|
|
|
|
newIters1 := iter.SubtrieIterators(newTrie.NodeIterator, sdb.numWorkers)
|
|
|
|
newIters2 := iter.SubtrieIterators(newTrie.NodeIterator, 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
|
2020-11-10 09:07:59 +00:00
|
|
|
// We do two state iterations per subtrie: one for new/updated nodes,
|
|
|
|
// one for deleted/updated nodes; prepare 2 iterator instances for each task
|
2022-06-24 08:42:58 +00:00
|
|
|
var iterPairs [][]sd.IterPair
|
2020-09-15 03:46:50 +00:00
|
|
|
for i := uint(0); i < sdb.numWorkers; i++ {
|
2022-06-24 08:42:58 +00:00
|
|
|
iterPairs = append(iterPairs, []sd.IterPair{
|
|
|
|
{Older: oldIters1[i], Newer: newIters1[i]},
|
|
|
|
{Older: oldIters2[i], Newer: newIters2[i]},
|
2020-11-10 09:07:59 +00:00
|
|
|
})
|
2020-09-04 16:50:49 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 09:07:59 +00:00
|
|
|
// Dispatch workers to process trie data; sync and collect results here via channels
|
2023-04-09 16:01:05 +00:00
|
|
|
nodeChan := make(chan sdtypes.StateLeafNode)
|
|
|
|
codeChan := make(chan sdtypes.IPLD)
|
2020-11-08 23:25:22 +00:00
|
|
|
|
2020-11-10 09:07:59 +00:00
|
|
|
go func() {
|
2023-04-09 16:01:05 +00:00
|
|
|
nodeSender := func(node sdtypes.StateLeafNode) error { nodeChan <- node; return nil }
|
|
|
|
ipldSender := func(code sdtypes.IPLD) error { codeChan <- code; return nil }
|
2020-11-10 09:07:59 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
for w := uint(0); w < sdb.numWorkers; w++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(worker uint) {
|
|
|
|
defer wg.Done()
|
2022-06-24 08:42:58 +00:00
|
|
|
var err error
|
2023-03-13 22:35:54 +00:00
|
|
|
logger := log.New("hash", args.BlockHash.Hex(), "number", args.BlockNumber)
|
2023-05-15 16:34:22 +00:00
|
|
|
err = sdb.BuildStateDiffWithIntermediateStateNodes(iterPairs[worker], params, nodeSender, ipldSender, logger, nil)
|
2022-06-24 08:42:58 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("buildStateDiff error for worker %d, params %+v", worker, params)
|
2021-10-25 19:06:05 +00:00
|
|
|
}
|
2020-11-10 09:07:59 +00:00
|
|
|
}(w)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
close(nodeChan)
|
|
|
|
close(codeChan)
|
|
|
|
}()
|
2020-09-06 18:09:25 +00:00
|
|
|
|
2020-11-10 09:07:59 +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
|
|
|
|
}
|
2020-11-10 09:07:59 +00:00
|
|
|
} else {
|
|
|
|
nodeChan = nil
|
|
|
|
}
|
|
|
|
case codeAndCodeHash, more := <-codeChan:
|
|
|
|
if more {
|
|
|
|
if err := codeOutput(codeAndCodeHash); err != nil {
|
|
|
|
return err
|
2020-11-08 23:25:22 +00:00
|
|
|
}
|
2020-11-10 09:07:59 +00:00
|
|
|
} else {
|
|
|
|
codeChan = nil
|
2020-09-06 18:09:25 +00:00
|
|
|
}
|
2020-11-10 09:07:59 +00:00
|
|
|
}
|
2020-09-04 16:50:49 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 09:07:59 +00:00
|
|
|
return nil
|
2020-09-04 16:50:49 +00:00
|
|
|
}
|