Skip to content

Commit

Permalink
experiment: re-order mutexes
Browse files Browse the repository at this point in the history
  • Loading branch information
anatolie-ssv committed Oct 16, 2024
1 parent 3257ec3 commit 075e14d
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions registry/storage/shares.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,13 @@ func (s *sharesStorage) Save(rw basedb.ReadWriter, shares ...*types.SSVShare) er

s.logger.Debug("save validators to shares storage", zap.Int("count", len(shares)))

s.storageMtx.Lock()
defer s.storageMtx.Unlock()

// Update in-memory.
err := func() error {
s.memoryMtx.Lock()
defer s.memoryMtx.Unlock()
defer s.storageMtx.Lock()

updateShares := make([]*types.SSVShare, 0, len(shares))
addShares := make([]*types.SSVShare, 0, len(shares))
Expand Down Expand Up @@ -327,12 +327,12 @@ func (s *sharesStorage) storageShareToSpecShare(share *storageShare) (*types.SSV
var errShareNotFound = errors.New("share not found")

func (s *sharesStorage) Delete(rw basedb.ReadWriter, pubKey []byte) error {
s.storageMtx.Lock()
defer s.storageMtx.Unlock()

err := func() error {
s.memoryMtx.Lock()
defer s.memoryMtx.Unlock()
defer s.storageMtx.Lock()

share, found := s.shares[hex.EncodeToString(pubKey)]
if !found {
Expand Down Expand Up @@ -360,12 +360,12 @@ func (s *sharesStorage) Delete(rw basedb.ReadWriter, pubKey []byte) error {
func (s *sharesStorage) UpdateValidatorsMetadata(data map[spectypes.ValidatorPK]*beaconprotocol.ValidatorMetadata) error {
var shares []*types.SSVShare

s.storageMtx.Lock()
defer s.storageMtx.Unlock()

err := func() error {
s.memoryMtx.Lock()
defer s.memoryMtx.Unlock()
defer s.storageMtx.Lock()

for pk, metadata := range data {
if metadata == nil {
Expand Down Expand Up @@ -396,12 +396,12 @@ func (s *sharesStorage) UpdateValidatorsMetadata(data map[spectypes.ValidatorPK]

// Drop deletes all shares.
func (s *sharesStorage) Drop() error {
s.storageMtx.Lock()
defer s.storageMtx.Unlock()

func() {
s.memoryMtx.Lock()
defer s.memoryMtx.Unlock()
defer s.storageMtx.Lock()

s.shares = make(map[string]*types.SSVShare)
s.validatorStore.handleDrop()
Expand Down

0 comments on commit 075e14d

Please sign in to comment.