diff --git a/core/vm/contracts_cache.go b/core/vm/contracts_cache.go index ccf4474e80..352a318d03 100644 --- a/core/vm/contracts_cache.go +++ b/core/vm/contracts_cache.go @@ -1,17 +1,24 @@ package vm import ( + "sync" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/lru" ) -var caches map[common.Address]*lru.Cache[string, []byte] +var ( + caches map[common.Address]*lru.Cache[string, []byte] + mu sync.RWMutex +) func init() { caches = make(map[common.Address]*lru.Cache[string, []byte]) } func addCache(precompile common.Address, input, output []byte) { + mu.Lock() + defer mu.Unlock() cache, ok := caches[precompile] if !ok { cache = lru.NewCache[string, []byte](128) @@ -21,6 +28,8 @@ func addCache(precompile common.Address, input, output []byte) { } func getCache(precompile common.Address, input []byte) ([]byte, bool) { + mu.RLock() + defer mu.RUnlock() cache, ok := caches[precompile] if !ok { caches[precompile] = lru.NewCache[string, []byte](128)