diff --git a/core/state/database.go b/core/state/database.go index 6a4135940974..77c80f10dd9c 100644 --- a/core/state/database.go +++ b/core/state/database.go @@ -142,7 +142,7 @@ func NewDatabaseWithConfig(db ethdb.Database, config *trie.Config) Database { diskdb: db, codeSizeCache: csc, codeCache: fastcache.New(codeCacheSize), - addrToPoint: make(utils.PointCache), + addrToPoint: utils.NewPointCache(), } } return &cachingDB{ @@ -246,7 +246,7 @@ type VerkleDB struct { // Caches all the points that correspond to an address, // so they are not recalculated. - addrToPoint utils.PointCache + addrToPoint *utils.PointCache } func (db *VerkleDB) GetTreeKeyHeader(addr []byte) *verkle.Point { @@ -256,7 +256,7 @@ func (db *VerkleDB) GetTreeKeyHeader(addr []byte) *verkle.Point { // OpenTrie opens the main account trie. func (db *VerkleDB) OpenTrie(root common.Hash) (Trie, error) { if root == (common.Hash{}) || root == emptyRoot { - return trie.NewVerkleTrie(verkle.New(), db.db, &db.addrToPoint), nil + return trie.NewVerkleTrie(verkle.New(), db.db, db.addrToPoint), nil } payload, err := db.DiskDB().Get(root[:]) if err != nil { @@ -267,7 +267,7 @@ func (db *VerkleDB) OpenTrie(root common.Hash) (Trie, error) { if err != nil { panic(err) } - return trie.NewVerkleTrie(r, db.db, &db.addrToPoint), err + return trie.NewVerkleTrie(r, db.db, db.addrToPoint), err } // OpenStorageTrie opens the storage trie of an account. diff --git a/trie/utils/verkle.go b/trie/utils/verkle.go index 9ec41ebb090f..f40f5b71112c 100644 --- a/trie/utils/verkle.go +++ b/trie/utils/verkle.go @@ -17,6 +17,8 @@ package utils import ( + "sync" + "github.com/crate-crypto/go-ipa/bandersnatch/fr" "github.com/gballet/go-verkle" "github.com/holiman/uint256" @@ -41,19 +43,33 @@ var ( getTreePolyIndex0Point *verkle.Point ) -type PointCache map[string]*verkle.Point +type PointCache struct { + cache map[string]*verkle.Point + lock sync.RWMutex +} + +func NewPointCache() *PointCache { + return &PointCache{ + cache: make(map[string]*verkle.Point), + } +} -func (pc PointCache) GetTreeKeyHeader(addr []byte) *verkle.Point { - if point, ok := pc[string(addr)]; ok { +func (pc *PointCache) GetTreeKeyHeader(addr []byte) *verkle.Point { + pc.lock.RLock() + point, ok := pc.cache[string(addr)] + pc.lock.RUnlock() + if ok { return point } - point := EvaluateAddressPoint(addr) - pc[string(addr)] = point + point = EvaluateAddressPoint(addr) + pc.lock.Lock() + pc.cache[string(addr)] = point + pc.lock.Unlock() return point } -func (pc PointCache) GetTreeKeyVersionCached(addr []byte) []byte { +func (pc *PointCache) GetTreeKeyVersionCached(addr []byte) []byte { p := pc.GetTreeKeyHeader(addr) v := PointToHash(p, VersionLeafKey) return v[:]