Skip to content

Commit

Permalink
syncmap fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
anatolie-ssv committed Oct 10, 2024
1 parent d471e5e commit 3f06fec
Showing 1 changed file with 21 additions and 24 deletions.
45 changes: 21 additions & 24 deletions registry/storage/shares.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type sharesStorage struct {
prefix []byte
shares *sync.Map
validatorStore *validatorStore
dbmu sync.Mutex
mu sync.Mutex
}

type storageOperator struct {
Expand Down Expand Up @@ -132,8 +132,8 @@ func NewSharesStorage(logger *zap.Logger, db basedb.Database, prefix []byte) (Sh

// load reads all shares from db.
func (s *sharesStorage) load() error {
s.dbmu.Lock()
defer s.dbmu.Unlock()
s.mu.Lock()
defer s.mu.Unlock()

return s.db.GetAll(append(s.prefix, sharesPrefix...), func(i int, obj basedb.Obj) error {
val := &storageShare{}
Expand Down Expand Up @@ -207,19 +207,17 @@ func (s *sharesStorage) Save(rw basedb.ReadWriter, shares ...*types.SSVShare) er
}
}

err := func() error {
s.dbmu.Lock()
defer s.dbmu.Unlock()
s.mu.Lock()
defer s.mu.Unlock()

return s.db.Using(rw).SetMany(s.prefix, len(shares), func(i int) (basedb.Obj, error) {
share := specShareToStorageShare(shares[i])
value, err := share.Encode()
if err != nil {
return basedb.Obj{}, fmt.Errorf("failed to serialize share: %w", err)
}
return basedb.Obj{Key: s.storageKey(share.ValidatorPubKey[:]), Value: value}, nil
})
}()
err := s.db.SetMany(s.prefix, len(shares), func(i int) (basedb.Obj, error) {
share := specShareToStorageShare(shares[i])
value, err := share.Encode()
if err != nil {
return basedb.Obj{}, fmt.Errorf("failed to serialize share: %w", err)
}
return basedb.Obj{Key: s.storageKey(share.ValidatorPubKey[:]), Value: value}, nil
})
if err != nil {
return err
}
Expand Down Expand Up @@ -312,25 +310,24 @@ func (s *sharesStorage) storageShareToSpecShare(share *storageShare) (*types.SSV
}

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

// Delete the share from the database
if err := s.db.Using(rw).Delete(s.prefix, s.storageKey(pubKey)); err != nil {
return err
}

key := hex.EncodeToString(pubKey)
val, found := s.shares.Load(key)

// Remove the share from local storage map
val, found := s.shares.LoadAndDelete(key)
if !found {
return nil
}

share := val.(*types.SSVShare)

// Remove the share from local storage map
s.shares.Delete(key)

// Remove the share from the validator store. This method will handle its own locking.
if err := s.validatorStore.handleShareRemoved(share); err != nil {
return err
Expand Down Expand Up @@ -365,8 +362,8 @@ func (s *sharesStorage) UpdateValidatorsMetadata(data map[spectypes.ValidatorPK]

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

err := s.db.DropPrefix(bytes.Join(
[][]byte{s.prefix, sharesPrefix, []byte("/")},
Expand All @@ -376,7 +373,7 @@ func (s *sharesStorage) Drop() error {
return err
}

s.shares = new(sync.Map) // make(map[string]*types.SSVShare)
s.shares = new(sync.Map)
s.validatorStore.handleDrop()
return nil
}
Expand Down

0 comments on commit 3f06fec

Please sign in to comment.