Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use a lock to protect the point cache #185

Merged
merged 1 commit into from
Mar 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions core/state/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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.
Expand Down
28 changes: 22 additions & 6 deletions trie/utils/verkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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[:]
Expand Down