|
|
|
@ -44,18 +44,19 @@ import (
|
|
|
|
|
var (
|
|
|
|
|
emptyNode, _ = rlp.EncodeToBytes(&[]byte{})
|
|
|
|
|
emptyContractRoot = crypto.Keccak256Hash(emptyNode)
|
|
|
|
|
nullCodeHash = crypto.Keccak256Hash([]byte{}).Bytes()
|
|
|
|
|
nullCodeHash = crypto.Keccak256([]byte{})
|
|
|
|
|
zeroHash common.Hash
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Builder interface exposes the method for building a state diff between two blocks
|
|
|
|
|
type Builder interface {
|
|
|
|
|
BuildStateDiffObject(args Args, params Params) (sdtypes.StateObject, error)
|
|
|
|
|
WriteStateDiffObject(args Args, params Params, output sdtypes.StateNodeSink, ipldOutput sdtypes.IPLDSink) error
|
|
|
|
|
BuildStateDiffObject(Args, Params) (sdtypes.StateObject, error)
|
|
|
|
|
WriteStateDiff(Args, Params, sdtypes.StateNodeSink, sdtypes.IPLDSink) error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type StateDiffBuilder struct {
|
|
|
|
|
StateCache adapt.StateView
|
|
|
|
|
// state cache is safe for concurrent reads
|
|
|
|
|
stateCache adapt.StateView
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type IterPair struct {
|
|
|
|
@ -84,7 +85,7 @@ func IPLDMappingAppender(iplds *[]sdtypes.IPLD) sdtypes.IPLDSink {
|
|
|
|
|
// NewBuilder is used to create a statediff builder
|
|
|
|
|
func NewBuilder(stateCache adapt.StateView) Builder {
|
|
|
|
|
return &StateDiffBuilder{
|
|
|
|
|
StateCache: stateCache, // state cache is safe for concurrent reads
|
|
|
|
|
stateCache: stateCache,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -93,7 +94,7 @@ func (sdb *StateDiffBuilder) BuildStateDiffObject(args Args, params Params) (sdt
|
|
|
|
|
defer metrics.UpdateDuration(time.Now(), metrics.IndexerMetrics.BuildStateDiffObjectTimer)
|
|
|
|
|
var stateNodes []sdtypes.StateLeafNode
|
|
|
|
|
var iplds []sdtypes.IPLD
|
|
|
|
|
err := sdb.WriteStateDiffObject(args, params, StateNodeAppender(&stateNodes), IPLDMappingAppender(&iplds))
|
|
|
|
|
err := sdb.WriteStateDiff(args, params, StateNodeAppender(&stateNodes), IPLDMappingAppender(&iplds))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return sdtypes.StateObject{}, err
|
|
|
|
|
}
|
|
|
|
@ -105,16 +106,16 @@ func (sdb *StateDiffBuilder) BuildStateDiffObject(args Args, params Params) (sdt
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WriteStateDiffObject writes a statediff object to output sinks
|
|
|
|
|
func (sdb *StateDiffBuilder) WriteStateDiffObject(args Args, params Params, output sdtypes.StateNodeSink,
|
|
|
|
|
ipldOutput sdtypes.IPLDSink) error {
|
|
|
|
|
defer metrics.UpdateDuration(time.Now(), metrics.IndexerMetrics.WriteStateDiffObjectTimer)
|
|
|
|
|
// WriteStateDiff writes a statediff object to output sinks
|
|
|
|
|
func (sdb *StateDiffBuilder) WriteStateDiff(args Args, params Params, nodeSink sdtypes.StateNodeSink,
|
|
|
|
|
ipldSink sdtypes.IPLDSink) error {
|
|
|
|
|
defer metrics.UpdateDuration(time.Now(), metrics.IndexerMetrics.WriteStateDiffTimer)
|
|
|
|
|
// Load tries for old and new states
|
|
|
|
|
oldTrie, err := sdb.StateCache.OpenTrie(args.OldStateRoot)
|
|
|
|
|
oldTrie, err := sdb.stateCache.OpenTrie(args.OldStateRoot)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("error creating trie for oldStateRoot: %w", err)
|
|
|
|
|
}
|
|
|
|
|
newTrie, err := sdb.StateCache.OpenTrie(args.NewStateRoot)
|
|
|
|
|
newTrie, err := sdb.stateCache.OpenTrie(args.NewStateRoot)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("error creating trie for newStateRoot: %w", err)
|
|
|
|
|
}
|
|
|
|
@ -134,28 +135,28 @@ func (sdb *StateDiffBuilder) WriteStateDiffObject(args Args, params Params, outp
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger := log.New("hash", args.BlockHash.String(), "number", args.BlockNumber)
|
|
|
|
|
return sdb.BuildStateDiff(iterPairs, params, output, ipldOutput, logger, nil)
|
|
|
|
|
logger := log.New("hash", args.BlockHash, "number", args.BlockNumber)
|
|
|
|
|
return sdb.BuildStateDiff(iterPairs, params, nodeSink, ipldSink, logger)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sdb *StateDiffBuilder) BuildStateDiff(iterPairs []IterPair, params Params,
|
|
|
|
|
output sdtypes.StateNodeSink, ipldOutput sdtypes.IPLDSink, logger log.Logger, prefixPath []byte) error {
|
|
|
|
|
sinkNode sdtypes.StateNodeSink, sinkIpld sdtypes.IPLDSink, logger log.Logger) error {
|
|
|
|
|
logger.Trace("statediff BEGIN BuildStateDiff")
|
|
|
|
|
defer metrics.ReportAndUpdateDuration("statediff END BuildStateDiff", time.Now(), logger, metrics.IndexerMetrics.BuildStateDiffTimer)
|
|
|
|
|
// collect a slice of all the nodes that were touched and exist at B (B-A)
|
|
|
|
|
// a map of their leafkey to all the accounts that were touched and exist at B
|
|
|
|
|
// and a slice of all the paths for the nodes in both of the above sets
|
|
|
|
|
diffAccountsAtB, err := sdb.createdAndUpdatedState(
|
|
|
|
|
iterPairs[0].Older, iterPairs[0].Newer, params.watchedAddressesLeafPaths, ipldOutput, logger, prefixPath)
|
|
|
|
|
iterPairs[0].Older, iterPairs[0].Newer, params.watchedAddressesLeafPaths, sinkIpld, logger)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("error collecting createdAndUpdatedNodes: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// collect a slice of all the nodes that existed at a path in A that doesn't exist in B
|
|
|
|
|
// a map of their leafkey to all the accounts that were touched and exist at A
|
|
|
|
|
diffAccountsAtA, err := sdb.deletedOrUpdatedState(
|
|
|
|
|
diffAccountsAtA, err := sdb.deletedState(
|
|
|
|
|
iterPairs[1].Older, iterPairs[1].Newer, diffAccountsAtB,
|
|
|
|
|
params.watchedAddressesLeafPaths, output, logger, prefixPath)
|
|
|
|
|
params.watchedAddressesLeafPaths, sinkNode, logger)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("error collecting deletedOrUpdatedNodes: %w", err)
|
|
|
|
|
}
|
|
|
|
@ -177,12 +178,12 @@ func (sdb *StateDiffBuilder) BuildStateDiff(iterPairs []IterPair, params Params,
|
|
|
|
|
"duration", time.Since(t))
|
|
|
|
|
|
|
|
|
|
// build the diff nodes for the updated accounts using the mappings at both A and B as directed by the keys found as the intersection of the two
|
|
|
|
|
err = sdb.buildAccountUpdates(diffAccountsAtB, diffAccountsAtA, updatedKeys, output, ipldOutput, logger)
|
|
|
|
|
err = sdb.buildAccountUpdates(diffAccountsAtB, diffAccountsAtA, updatedKeys, sinkNode, sinkIpld, logger)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("error building diff for updated accounts: %w", err)
|
|
|
|
|
}
|
|
|
|
|
// build the diff nodes for created accounts
|
|
|
|
|
err = sdb.buildAccountCreations(diffAccountsAtB, output, ipldOutput, logger)
|
|
|
|
|
err = sdb.buildAccountCreations(diffAccountsAtB, sinkNode, sinkIpld, logger)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("error building diff for created accounts: %w", err)
|
|
|
|
|
}
|
|
|
|
@ -194,7 +195,7 @@ func (sdb *StateDiffBuilder) BuildStateDiff(iterPairs []IterPair, params Params,
|
|
|
|
|
// a mapping of their leafkeys to all the accounts that exist in a different state at B than A
|
|
|
|
|
// and a slice of the paths for all of the nodes included in both
|
|
|
|
|
func (sdb *StateDiffBuilder) createdAndUpdatedState(a, b trie.NodeIterator,
|
|
|
|
|
watchedAddressesLeafPaths [][]byte, output sdtypes.IPLDSink, logger log.Logger, prefixPath []byte) (sdtypes.AccountMap, error) {
|
|
|
|
|
watchedAddressesLeafPaths [][]byte, ipldSink sdtypes.IPLDSink, logger log.Logger) (sdtypes.AccountMap, error) {
|
|
|
|
|
logger.Trace("statediff BEGIN createdAndUpdatedState")
|
|
|
|
|
defer metrics.ReportAndUpdateDuration("statediff END createdAndUpdatedState", time.Now(), logger, metrics.IndexerMetrics.CreatedAndUpdatedStateTimer)
|
|
|
|
|
diffAccountsAtB := make(sdtypes.AccountMap)
|
|
|
|
@ -246,7 +247,7 @@ func (sdb *StateDiffBuilder) createdAndUpdatedState(a, b trie.NodeIterator,
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if err := output(sdtypes.IPLD{
|
|
|
|
|
if err := ipldSink(sdtypes.IPLD{
|
|
|
|
|
CID: ipld.Keccak256ToCid(ipld.MEthStateTrie, it.Hash().Bytes()).String(),
|
|
|
|
|
Content: nodeVal,
|
|
|
|
|
}); err != nil {
|
|
|
|
@ -276,12 +277,12 @@ func (sdb *StateDiffBuilder) processStateValueNode(it trie.NodeIterator, parentB
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// deletedOrUpdatedState returns a slice of all the paths that are emptied at B
|
|
|
|
|
// deletedState returns a slice of all the paths that are emptied at B
|
|
|
|
|
// and a mapping of their leafkeys to all the accounts that exist in a different state at A than B
|
|
|
|
|
func (sdb *StateDiffBuilder) deletedOrUpdatedState(a, b trie.NodeIterator, diffAccountsAtB sdtypes.AccountMap,
|
|
|
|
|
watchedAddressesLeafPaths [][]byte, output sdtypes.StateNodeSink, logger log.Logger, prefixPath []byte) (sdtypes.AccountMap, error) {
|
|
|
|
|
logger.Trace("statediff BEGIN deletedOrUpdatedState")
|
|
|
|
|
defer metrics.ReportAndUpdateDuration("statediff END deletedOrUpdatedState", time.Now(), logger, metrics.IndexerMetrics.DeletedOrUpdatedStateTimer)
|
|
|
|
|
func (sdb *StateDiffBuilder) deletedState(a, b trie.NodeIterator, diffAccountsAtB sdtypes.AccountMap,
|
|
|
|
|
watchedAddressesLeafPaths [][]byte, nodeSink sdtypes.StateNodeSink, logger log.Logger) (sdtypes.AccountMap, error) {
|
|
|
|
|
logger.Trace("statediff BEGIN deletedState")
|
|
|
|
|
defer metrics.ReportAndUpdateDuration("statediff END deletedState", time.Now(), logger, metrics.IndexerMetrics.DeletedStateTimer)
|
|
|
|
|
diffAccountAtA := make(sdtypes.AccountMap)
|
|
|
|
|
|
|
|
|
|
var prevBlob []byte
|
|
|
|
@ -321,7 +322,7 @@ func (sdb *StateDiffBuilder) deletedOrUpdatedState(a, b trie.NodeIterator, diffA
|
|
|
|
|
return nil, fmt.Errorf("failed building storage diffs for removed state account with key %x\r\nerror: %w", leafKey, err)
|
|
|
|
|
}
|
|
|
|
|
diff.StorageDiff = storageDiff
|
|
|
|
|
if err := output(diff); err != nil {
|
|
|
|
|
if err := nodeSink(diff); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -338,7 +339,7 @@ func (sdb *StateDiffBuilder) deletedOrUpdatedState(a, b trie.NodeIterator, diffA
|
|
|
|
|
// needs to be called before building account creations and deletions as this mutates
|
|
|
|
|
// those account maps to remove the accounts which were updated
|
|
|
|
|
func (sdb *StateDiffBuilder) buildAccountUpdates(creations, deletions sdtypes.AccountMap, updatedKeys []string,
|
|
|
|
|
output sdtypes.StateNodeSink, ipldOutput sdtypes.IPLDSink, logger log.Logger) error {
|
|
|
|
|
nodeSink sdtypes.StateNodeSink, ipldSink sdtypes.IPLDSink, logger log.Logger) error {
|
|
|
|
|
logger.Trace("statediff BEGIN buildAccountUpdates",
|
|
|
|
|
"creations", len(creations), "deletions", len(deletions), "updated", len(updatedKeys))
|
|
|
|
|
defer metrics.ReportAndUpdateDuration("statediff END buildAccountUpdates ",
|
|
|
|
@ -351,13 +352,13 @@ func (sdb *StateDiffBuilder) buildAccountUpdates(creations, deletions sdtypes.Ac
|
|
|
|
|
if deletedAcc.Account != nil && createdAcc.Account != nil {
|
|
|
|
|
err = sdb.buildStorageNodesIncremental(
|
|
|
|
|
deletedAcc.Account.Root, createdAcc.Account.Root,
|
|
|
|
|
StorageNodeAppender(&storageDiff), ipldOutput,
|
|
|
|
|
StorageNodeAppender(&storageDiff), ipldSink,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed building incremental storage diffs for account with leafkey %x\r\nerror: %w", key, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if err = output(sdtypes.StateLeafNode{
|
|
|
|
|
if err = nodeSink(sdtypes.StateLeafNode{
|
|
|
|
|
AccountWrapper: createdAcc,
|
|
|
|
|
Removed: false,
|
|
|
|
|
StorageDiff: storageDiff,
|
|
|
|
@ -373,8 +374,8 @@ func (sdb *StateDiffBuilder) buildAccountUpdates(creations, deletions sdtypes.Ac
|
|
|
|
|
|
|
|
|
|
// buildAccountCreations returns the statediff node objects for all the accounts that exist at B but not at A
|
|
|
|
|
// it also returns the code and codehash for created contract accounts
|
|
|
|
|
func (sdb *StateDiffBuilder) buildAccountCreations(accounts sdtypes.AccountMap, output sdtypes.StateNodeSink,
|
|
|
|
|
ipldOutput sdtypes.IPLDSink, logger log.Logger) error {
|
|
|
|
|
func (sdb *StateDiffBuilder) buildAccountCreations(accounts sdtypes.AccountMap, nodeSink sdtypes.StateNodeSink,
|
|
|
|
|
ipldSink sdtypes.IPLDSink, logger log.Logger) error {
|
|
|
|
|
logger.Trace("statediff BEGIN buildAccountCreations")
|
|
|
|
|
defer metrics.ReportAndUpdateDuration("statediff END buildAccountCreations",
|
|
|
|
|
time.Now(), logger, metrics.IndexerMetrics.BuildAccountCreationsTimer)
|
|
|
|
@ -386,25 +387,25 @@ func (sdb *StateDiffBuilder) buildAccountCreations(accounts sdtypes.AccountMap,
|
|
|
|
|
if !bytes.Equal(val.Account.CodeHash, nullCodeHash) {
|
|
|
|
|
// For contract creations, any storage node contained is a diff
|
|
|
|
|
storageDiff := make([]sdtypes.StorageLeafNode, 0)
|
|
|
|
|
err := sdb.buildStorageNodesEventual(val.Account.Root, StorageNodeAppender(&storageDiff), ipldOutput)
|
|
|
|
|
err := sdb.buildStorageNodesEventual(val.Account.Root, StorageNodeAppender(&storageDiff), ipldSink)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed building eventual storage diffs for node with leaf key %x\r\nerror: %w", val.LeafKey, err)
|
|
|
|
|
}
|
|
|
|
|
diff.StorageDiff = storageDiff
|
|
|
|
|
// emit codehash => code mappings for contract
|
|
|
|
|
codeHash := common.BytesToHash(val.Account.CodeHash)
|
|
|
|
|
code, err := sdb.StateCache.ContractCode(codeHash)
|
|
|
|
|
code, err := sdb.stateCache.ContractCode(codeHash)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to retrieve code for codehash %x\r\n error: %w", codeHash, err)
|
|
|
|
|
return fmt.Errorf("failed to retrieve code for codehash %s\r\n error: %w", codeHash, err)
|
|
|
|
|
}
|
|
|
|
|
if err := ipldOutput(sdtypes.IPLD{
|
|
|
|
|
if err := ipldSink(sdtypes.IPLD{
|
|
|
|
|
CID: ipld.Keccak256ToCid(ipld.RawBinary, codeHash.Bytes()).String(),
|
|
|
|
|
Content: code,
|
|
|
|
|
}); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if err := output(diff); err != nil {
|
|
|
|
|
if err := nodeSink(diff); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -414,38 +415,31 @@ func (sdb *StateDiffBuilder) buildAccountCreations(accounts sdtypes.AccountMap,
|
|
|
|
|
|
|
|
|
|
// buildStorageNodesEventual builds the storage diff node objects for a created account
|
|
|
|
|
// i.e. it returns all the storage nodes at this state, since there is no previous state
|
|
|
|
|
func (sdb *StateDiffBuilder) buildStorageNodesEventual(sr common.Hash, output sdtypes.StorageNodeSink,
|
|
|
|
|
ipldOutput sdtypes.IPLDSink) error {
|
|
|
|
|
func (sdb *StateDiffBuilder) buildStorageNodesEventual(sr common.Hash, storageSink sdtypes.StorageNodeSink,
|
|
|
|
|
ipldSink sdtypes.IPLDSink) error {
|
|
|
|
|
defer metrics.UpdateDuration(time.Now(), metrics.IndexerMetrics.BuildStorageNodesEventualTimer)
|
|
|
|
|
if sr == emptyContractRoot {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
log.Debug("Storage root for eventual diff", "root", sr.String())
|
|
|
|
|
sTrie, err := sdb.StateCache.OpenTrie(sr)
|
|
|
|
|
log.Debug("Storage root for eventual diff", "root", sr)
|
|
|
|
|
sTrie, err := sdb.stateCache.OpenTrie(sr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Info("error in build storage diff eventual", "error", err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
it := sTrie.NodeIterator(make([]byte, 0))
|
|
|
|
|
return sdb.buildStorageNodesFromTrie(it, output, ipldOutput)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// buildStorageNodesFromTrie returns all the storage diff node objects in the provided node iterator
|
|
|
|
|
func (sdb *StateDiffBuilder) buildStorageNodesFromTrie(it trie.NodeIterator, output sdtypes.StorageNodeSink,
|
|
|
|
|
ipldOutput sdtypes.IPLDSink) error {
|
|
|
|
|
defer metrics.UpdateDuration(time.Now(), metrics.IndexerMetrics.BuildStorageNodesFromTrieTimer)
|
|
|
|
|
|
|
|
|
|
var prevBlob []byte
|
|
|
|
|
for it.Next(true) {
|
|
|
|
|
if it.Leaf() {
|
|
|
|
|
storageLeafNode := sdb.processStorageValueNode(it, prevBlob)
|
|
|
|
|
if err := output(storageLeafNode); err != nil {
|
|
|
|
|
if err := storageSink(storageLeafNode); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
nodeVal := make([]byte, len(it.NodeBlob()))
|
|
|
|
|
copy(nodeVal, it.NodeBlob())
|
|
|
|
|
if err := ipldOutput(sdtypes.IPLD{
|
|
|
|
|
if err := ipldSink(sdtypes.IPLD{
|
|
|
|
|
CID: ipld.Keccak256ToCid(ipld.MEthStorageTrie, it.Hash().Bytes()).String(),
|
|
|
|
|
Content: nodeVal,
|
|
|
|
|
}); err != nil {
|
|
|
|
@ -474,29 +468,29 @@ func (sdb *StateDiffBuilder) processStorageValueNode(it trie.NodeIterator, paren
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// buildRemovedAccountStorageNodes builds the "removed" diffs for all the storage nodes for a destroyed account
|
|
|
|
|
func (sdb *StateDiffBuilder) buildRemovedAccountStorageNodes(sr common.Hash, output sdtypes.StorageNodeSink) error {
|
|
|
|
|
func (sdb *StateDiffBuilder) buildRemovedAccountStorageNodes(sr common.Hash, storageSink sdtypes.StorageNodeSink) error {
|
|
|
|
|
defer metrics.UpdateDuration(time.Now(), metrics.IndexerMetrics.BuildRemovedAccountStorageNodesTimer)
|
|
|
|
|
if sr == emptyContractRoot {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
log.Debug("Storage root for removed diffs", "root", sr.String())
|
|
|
|
|
sTrie, err := sdb.StateCache.OpenTrie(sr)
|
|
|
|
|
log.Debug("Storage root for removed diffs", "root", sr)
|
|
|
|
|
sTrie, err := sdb.stateCache.OpenTrie(sr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Info("error in build removed account storage diffs", "error", err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
it := sTrie.NodeIterator(make([]byte, 0))
|
|
|
|
|
return sdb.buildRemovedStorageNodesFromTrie(it, output)
|
|
|
|
|
it := sTrie.NodeIterator(nil)
|
|
|
|
|
return sdb.buildRemovedStorageNodesFromTrie(it, storageSink)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// buildRemovedStorageNodesFromTrie returns diffs for all the storage nodes in the provided node interator
|
|
|
|
|
func (sdb *StateDiffBuilder) buildRemovedStorageNodesFromTrie(it trie.NodeIterator, output sdtypes.StorageNodeSink) error {
|
|
|
|
|
func (sdb *StateDiffBuilder) buildRemovedStorageNodesFromTrie(it trie.NodeIterator, storageSink sdtypes.StorageNodeSink) error {
|
|
|
|
|
defer metrics.UpdateDuration(time.Now(), metrics.IndexerMetrics.BuildRemovedStorageNodesFromTrieTimer)
|
|
|
|
|
for it.Next(true) {
|
|
|
|
|
if it.Leaf() { // only leaf values are indexed, don't need to demarcate removed intermediate nodes
|
|
|
|
|
leafKey := make([]byte, len(it.LeafKey()))
|
|
|
|
|
copy(leafKey, it.LeafKey())
|
|
|
|
|
if err := output(sdtypes.StorageLeafNode{
|
|
|
|
|
if err := storageSink(sdtypes.StorageLeafNode{
|
|
|
|
|
CID: shared.RemovedNodeStorageCID,
|
|
|
|
|
Removed: true,
|
|
|
|
|
LeafKey: leafKey,
|
|
|
|
@ -510,33 +504,33 @@ func (sdb *StateDiffBuilder) buildRemovedStorageNodesFromTrie(it trie.NodeIterat
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// buildStorageNodesIncremental builds the storage diff node objects for all nodes that exist in a different state at B than A
|
|
|
|
|
func (sdb *StateDiffBuilder) buildStorageNodesIncremental(oldroot common.Hash, newroot common.Hash, output sdtypes.StorageNodeSink,
|
|
|
|
|
ipldOutput sdtypes.IPLDSink) error {
|
|
|
|
|
func (sdb *StateDiffBuilder) buildStorageNodesIncremental(oldroot common.Hash, newroot common.Hash, storageSink sdtypes.StorageNodeSink,
|
|
|
|
|
ipldSink sdtypes.IPLDSink) error {
|
|
|
|
|
defer metrics.UpdateDuration(time.Now(), metrics.IndexerMetrics.BuildStorageNodesIncrementalTimer)
|
|
|
|
|
if newroot == oldroot {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
log.Trace("Storage roots for incremental diff", "old", oldroot.String(), "new", newroot.String())
|
|
|
|
|
oldTrie, err := sdb.StateCache.OpenTrie(oldroot)
|
|
|
|
|
log.Trace("Storage roots for incremental diff", "old", oldroot, "new", newroot)
|
|
|
|
|
oldTrie, err := sdb.stateCache.OpenTrie(oldroot)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
newTrie, err := sdb.StateCache.OpenTrie(newroot)
|
|
|
|
|
newTrie, err := sdb.stateCache.OpenTrie(newroot)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
diffSlotsAtB, err := sdb.createdAndUpdatedStorage(
|
|
|
|
|
oldTrie.NodeIterator([]byte{}), newTrie.NodeIterator([]byte{}), output, ipldOutput)
|
|
|
|
|
oldTrie.NodeIterator(nil), newTrie.NodeIterator(nil), storageSink, ipldSink)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return sdb.deletedOrUpdatedStorage(oldTrie.NodeIterator([]byte{}), newTrie.NodeIterator([]byte{}),
|
|
|
|
|
diffSlotsAtB, output)
|
|
|
|
|
return sdb.deletedOrUpdatedStorage(oldTrie.NodeIterator(nil), newTrie.NodeIterator(nil),
|
|
|
|
|
diffSlotsAtB, storageSink)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sdb *StateDiffBuilder) createdAndUpdatedStorage(a, b trie.NodeIterator, output sdtypes.StorageNodeSink,
|
|
|
|
|
ipldOutput sdtypes.IPLDSink) (map[string]bool, error) {
|
|
|
|
|
func (sdb *StateDiffBuilder) createdAndUpdatedStorage(a, b trie.NodeIterator, storageSink sdtypes.StorageNodeSink,
|
|
|
|
|
ipldSink sdtypes.IPLDSink) (map[string]bool, error) {
|
|
|
|
|
defer metrics.UpdateDuration(time.Now(), metrics.IndexerMetrics.CreatedAndUpdatedStorageTimer)
|
|
|
|
|
diffSlotsAtB := make(map[string]bool)
|
|
|
|
|
|
|
|
|
@ -545,7 +539,7 @@ func (sdb *StateDiffBuilder) createdAndUpdatedStorage(a, b trie.NodeIterator, ou
|
|
|
|
|
for it.Next(true) {
|
|
|
|
|
if it.Leaf() {
|
|
|
|
|
storageLeafNode := sdb.processStorageValueNode(it, prevBlob)
|
|
|
|
|
if err := output(storageLeafNode); err != nil {
|
|
|
|
|
if err := storageSink(storageLeafNode); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
diffSlotsAtB[hex.EncodeToString(storageLeafNode.LeafKey)] = true
|
|
|
|
@ -555,10 +549,8 @@ func (sdb *StateDiffBuilder) createdAndUpdatedStorage(a, b trie.NodeIterator, ou
|
|
|
|
|
}
|
|
|
|
|
nodeVal := make([]byte, len(it.NodeBlob()))
|
|
|
|
|
copy(nodeVal, it.NodeBlob())
|
|
|
|
|
nodeHash := make([]byte, len(it.Hash().Bytes()))
|
|
|
|
|
copy(nodeHash, it.Hash().Bytes())
|
|
|
|
|
if err := ipldOutput(sdtypes.IPLD{
|
|
|
|
|
CID: ipld.Keccak256ToCid(ipld.MEthStorageTrie, nodeHash).String(),
|
|
|
|
|
if err := ipldSink(sdtypes.IPLD{
|
|
|
|
|
CID: ipld.Keccak256ToCid(ipld.MEthStorageTrie, it.Hash().Bytes()).String(),
|
|
|
|
|
Content: nodeVal,
|
|
|
|
|
}); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
@ -569,7 +561,7 @@ func (sdb *StateDiffBuilder) createdAndUpdatedStorage(a, b trie.NodeIterator, ou
|
|
|
|
|
return diffSlotsAtB, it.Error()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sdb *StateDiffBuilder) deletedOrUpdatedStorage(a, b trie.NodeIterator, diffSlotsAtB map[string]bool, output sdtypes.StorageNodeSink) error {
|
|
|
|
|
func (sdb *StateDiffBuilder) deletedOrUpdatedStorage(a, b trie.NodeIterator, diffSlotsAtB map[string]bool, storageSink sdtypes.StorageNodeSink) error {
|
|
|
|
|
defer metrics.UpdateDuration(time.Now(), metrics.IndexerMetrics.DeletedOrUpdatedStorageTimer)
|
|
|
|
|
it, _ := trie.NewDifferenceIterator(b, a)
|
|
|
|
|
for it.Next(true) {
|
|
|
|
@ -580,7 +572,7 @@ func (sdb *StateDiffBuilder) deletedOrUpdatedStorage(a, b trie.NodeIterator, dif
|
|
|
|
|
// that means the storage slot was vacated
|
|
|
|
|
// in that case, emit an empty "removed" diff storage node
|
|
|
|
|
if _, ok := diffSlotsAtB[hex.EncodeToString(leafKey)]; !ok {
|
|
|
|
|
if err := output(sdtypes.StorageLeafNode{
|
|
|
|
|
if err := storageSink(sdtypes.StorageLeafNode{
|
|
|
|
|
CID: shared.RemovedNodeStorageCID,
|
|
|
|
|
Removed: true,
|
|
|
|
|
LeafKey: leafKey,
|
|
|
|
|