Skip to content

Commit

Permalink
prevent concurrent writes on edsstore.Put
Browse files Browse the repository at this point in the history
  • Loading branch information
walldiss committed Aug 25, 2023
1 parent 1205437 commit 4b5dabc
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions share/eds/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"io"
"os"
"sync"
"sync/atomic"
"time"

Expand Down Expand Up @@ -64,6 +65,9 @@ type Store struct {
// lastGCResult is only stored on the store for testing purposes.
lastGCResult atomic.Pointer[dagstore.GCResult]

// skip is used to skip parallel operations
skip sync.Map

metrics *metrics
}

Expand Down Expand Up @@ -197,6 +201,12 @@ func (s *Store) Put(ctx context.Context, root share.DataHash, square *rsmt2d.Ext
}

func (s *Store) put(ctx context.Context, root share.DataHash, square *rsmt2d.ExtendedDataSquare) (err error) {
if _, exists := s.skip.LoadOrStore(root.String(), 1); exists {
// no need to do any work, another routine is putting eds for the same root
return nil
}
defer s.skip.Delete(root.String())

// if root already exists, short-circuit
if has, _ := s.Has(ctx, root); has {
return dagstore.ErrShardExists
Expand Down

0 comments on commit 4b5dabc

Please sign in to comment.