Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: duplicate pin #4638

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion pkg/storer/internal/pinning/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ var (
ErrInvalidPinCollectionItemSize = errInvalidPinCollectionSize
ErrPutterAlreadyClosed = errPutterAlreadyClosed
ErrCollectionRootAddressIsZero = errCollectionRootAddressIsZero
ErrDuplicatePinCollection = errDuplicatePinCollection
)

var NewUUID = newUUID
Expand Down
7 changes: 3 additions & 4 deletions pkg/storer/internal/pinning/pinning.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion pkg/storer/uploadstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}(),
Expand Down
20 changes: 17 additions & 3 deletions pkg/storer/uploadstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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))
Expand Down Expand Up @@ -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 {
Expand Down
Loading