From 56faac01b71b9a2c837eaeb324b1c92ec4ef84b4 Mon Sep 17 00:00:00 2001 From: Acha Bill Date: Mon, 8 Apr 2024 11:22:40 +0100 Subject: [PATCH] fix: duplicate pin --- pkg/storer/internal/pinning/export_test.go | 1 - pkg/storer/internal/pinning/pinning.go | 7 +++---- pkg/storer/uploadstore.go | 6 +++++- pkg/storer/uploadstore_test.go | 20 +++++++++++++++++--- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/pkg/storer/internal/pinning/export_test.go b/pkg/storer/internal/pinning/export_test.go index ecce9d1b597..cba519a6d01 100644 --- a/pkg/storer/internal/pinning/export_test.go +++ b/pkg/storer/internal/pinning/export_test.go @@ -23,7 +23,6 @@ var ( ErrInvalidPinCollectionItemSize = errInvalidPinCollectionSize ErrPutterAlreadyClosed = errPutterAlreadyClosed ErrCollectionRootAddressIsZero = errCollectionRootAddressIsZero - ErrDuplicatePinCollection = errDuplicatePinCollection ) var NewUUID = newUUID diff --git a/pkg/storer/internal/pinning/pinning.go b/pkg/storer/internal/pinning/pinning.go index de33f9ffd93..184e106be2a 100644 --- a/pkg/storer/internal/pinning/pinning.go +++ b/pkg/storer/internal/pinning/pinning.go @@ -40,8 +40,8 @@ var ( // errCollectionRootAddressIsZero is returned if the putter is closed with a zero // swarm.Address. Root reference has to be set. errCollectionRootAddressIsZero = errors.New("pin store: collection root address is zero") - // errDuplicatePinCollection is returned when attempted to pin the same file repeatedly - errDuplicatePinCollection = errors.New("pin store: duplicate pin collection") + // ErrDuplicatePinCollection is returned when attempted to pin the same file repeatedly + ErrDuplicatePinCollection = errors.New("pin store: duplicate pin collection") ) // creates a new UUID and returns it as a byte slice @@ -274,8 +274,7 @@ func (c *collectionPutter) Close(st internal.Storage, writer storage.Writer, roo } if has { - // trigger the Cleanup - return errDuplicatePinCollection + return ErrDuplicatePinCollection } // Save the root pin reference. diff --git a/pkg/storer/uploadstore.go b/pkg/storer/uploadstore.go index d3596dbede0..43c7a0868b4 100644 --- a/pkg/storer/uploadstore.go +++ b/pkg/storer/uploadstore.go @@ -111,7 +111,11 @@ func (db *DB) Upload(ctx context.Context, pin bool, tagID uint64) (PutterSession uploadPutter.Close(s, b, address), func() error { if pinningPutter != nil { - return pinningPutter.Close(s, b, address) + pinErr := pinningPutter.Close(s, b, address) + if errors.Is(pinErr, pinstore.ErrDuplicatePinCollection) { + pinErr = pinningPutter.Cleanup(db) + } + return pinErr } return nil }(), diff --git a/pkg/storer/uploadstore_test.go b/pkg/storer/uploadstore_test.go index 04b1016027a..2671a7c5d87 100644 --- a/pkg/storer/uploadstore_test.go +++ b/pkg/storer/uploadstore_test.go @@ -55,9 +55,10 @@ func testUploadStore(t *testing.T, newStorer func() (*storer.DB, error)) { }) for _, tc := range []struct { - chunks []swarm.Chunk - pin bool - fail bool + chunks []swarm.Chunk + pin bool + fail bool + duplicate bool }{ { chunks: chunktesting.GenerateTestRandomChunks(10), @@ -82,6 +83,11 @@ func testUploadStore(t *testing.T, newStorer func() (*storer.DB, error)) { chunks: chunktesting.GenerateTestRandomChunks(30), pin: true, }, + { + chunks: chunktesting.GenerateTestRandomChunks(10), + pin: true, + duplicate: true, + }, } { tc := tc testName := fmt.Sprintf("upload_%d_chunks", len(tc.chunks)) @@ -126,6 +132,14 @@ func testUploadStore(t *testing.T, newStorer func() (*storer.DB, error)) { if err != nil { t.Fatalf("session.Done(...): unexpected error: %v", err) } + + // duplicate pin + if tc.pin && tc.duplicate { + err := session.Done(tc.chunks[0].Address()) + if err != nil { + t.Fatalf("session.Done(...): unexpected error: %v", err) + } + } } verifySessionInfo(t, lstore.Repo(), tag.TagID, tc.chunks, !tc.fail) if tc.pin {