Skip to content

Commit

Permalink
fix: check before writing root address
Browse files Browse the repository at this point in the history
  • Loading branch information
notanatol committed Jan 25, 2024
1 parent 657f7ab commit 6ca0ffe
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions pkg/storer/internal/pinning/pinning.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,16 @@ func (c *collectionPutter) Put(ctx context.Context, st internal.Storage, writer
return nil
}

rootCollection := &pinCollectionItem{Addr: ch.Address()}
has, err := st.IndexStore().Has(rootCollection)
if err != nil {
return fmt.Errorf("pin store: check previous root: %w", err)
}
if has {
// duplicate PUT of the same root address
return nil
}

err = writer.Put(collectionChunk)
if err != nil {
return fmt.Errorf("pin store: failed putting collection chunk: %w", err)
Expand All @@ -264,11 +274,19 @@ func (c *collectionPutter) Close(st internal.Storage, writer storage.Writer, roo
c.mtx.Lock()
defer c.mtx.Unlock()

// Save the root pin reference.
c.collection.Addr = root
err := writer.Put(c.collection)
collection := &pinCollectionItem{Addr: root}
has, err := st.IndexStore().Has(collection)
if err != nil {
return fmt.Errorf("pin store: failed updating collection: %w", err)
return fmt.Errorf("pin store: check previous root: %w", err)
}

if !has {
// Save the root pin reference.
c.collection.Addr = root
err := writer.Put(c.collection)
if err != nil {
return fmt.Errorf("pin store: failed updating collection: %w", err)
}
}

err = writer.Delete(&dirtyCollection{UUID: c.collection.UUID})
Expand Down

0 comments on commit 6ca0ffe

Please sign in to comment.