From 6ca0ffe8c98abe70531826a68c66e67d49593ffe Mon Sep 17 00:00:00 2001 From: notanatol Date: Wed, 24 Jan 2024 18:29:45 -0600 Subject: [PATCH] fix: check before writing root address --- pkg/storer/internal/pinning/pinning.go | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/pkg/storer/internal/pinning/pinning.go b/pkg/storer/internal/pinning/pinning.go index 03c47667180..1afe6de7807 100644 --- a/pkg/storer/internal/pinning/pinning.go +++ b/pkg/storer/internal/pinning/pinning.go @@ -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) @@ -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})