triedb/pathdb: use maps.Clone and maps.Keys (#29985)

This commit is contained in:
maskpp 2024-06-17 23:09:29 +08:00 committed by GitHub
parent 115d154392
commit c736b04d9b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 5 additions and 11 deletions

View File

@ -29,6 +29,7 @@ import (
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/trie/triestate" "github.com/ethereum/go-ethereum/trie/triestate"
"golang.org/x/exp/maps"
) )
// State history records the state changes involved in executing a block. The // State history records the state changes involved in executing a block. The
@ -244,19 +245,13 @@ type history struct {
// newHistory constructs the state history object with provided state change set. // newHistory constructs the state history object with provided state change set.
func newHistory(root common.Hash, parent common.Hash, block uint64, states *triestate.Set) *history { func newHistory(root common.Hash, parent common.Hash, block uint64, states *triestate.Set) *history {
var ( var (
accountList []common.Address accountList = maps.Keys(states.Accounts)
storageList = make(map[common.Address][]common.Hash) storageList = make(map[common.Address][]common.Hash)
) )
for addr := range states.Accounts {
accountList = append(accountList, addr)
}
slices.SortFunc(accountList, common.Address.Cmp) slices.SortFunc(accountList, common.Address.Cmp)
for addr, slots := range states.Storages { for addr, slots := range states.Storages {
slist := make([]common.Hash, 0, len(slots)) slist := maps.Keys(slots)
for slotHash := range slots {
slist = append(slist, slotHash)
}
slices.SortFunc(slist, common.Hash.Cmp) slices.SortFunc(slist, common.Hash.Cmp)
storageList[addr] = slist storageList[addr] = slist
} }

View File

@ -19,6 +19,7 @@ package pathdb
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"maps"
"time" "time"
"github.com/VictoriaMetrics/fastcache" "github.com/VictoriaMetrics/fastcache"
@ -90,12 +91,10 @@ func (b *nodebuffer) commit(nodes map[common.Hash]map[string]*trienode.Node) *no
// The nodes belong to original diff layer are still accessible even // The nodes belong to original diff layer are still accessible even
// after merging, thus the ownership of nodes map should still belong // after merging, thus the ownership of nodes map should still belong
// to original layer and any mutation on it should be prevented. // to original layer and any mutation on it should be prevented.
current = make(map[string]*trienode.Node, len(subset))
for path, n := range subset { for path, n := range subset {
current[path] = n
delta += int64(len(n.Blob) + len(path)) delta += int64(len(n.Blob) + len(path))
} }
b.nodes[owner] = current b.nodes[owner] = maps.Clone(subset)
continue continue
} }
for path, n := range subset { for path, n := range subset {