Skip to content

Commit

Permalink
Add mutex lock to map access in Store
Browse files Browse the repository at this point in the history
  • Loading branch information
jparyani committed Dec 16, 2021
1 parent 9ea4c37 commit c7be3e6
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/store.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package lib

import "github.com/ethereum/go-ethereum/common"
import (
"sync"

"github.com/ethereum/go-ethereum/common"
)

// Store stores payloads and retrieves them based on blockHash hashes
type Store interface {
Expand All @@ -13,15 +17,21 @@ type Store interface {
// TODO: clean this up periodically

type store struct {
mutex sync.RWMutex
payloads map[common.Hash]*ExecutionPayloadWithTxRootV1
}

// NewStore creates an in-mem store
func NewStore() Store {
return &store{map[common.Hash]*ExecutionPayloadWithTxRootV1{}}
return &store{
payloads: map[common.Hash]*ExecutionPayloadWithTxRootV1{},
}
}

func (s *store) Get(blockHash common.Hash) *ExecutionPayloadWithTxRootV1 {
s.mutex.RLock()
defer s.mutex.RUnlock()

payload, ok := s.payloads[blockHash]
if !ok {
return nil
Expand All @@ -31,6 +41,9 @@ func (s *store) Get(blockHash common.Hash) *ExecutionPayloadWithTxRootV1 {
}

func (s *store) Set(blockHash common.Hash, payload *ExecutionPayloadWithTxRootV1) {
s.mutex.Lock()
defer s.mutex.Unlock()

if payload == nil {
return
}
Expand Down

0 comments on commit c7be3e6

Please sign in to comment.