Skip to content

Commit

Permalink
fix: duplicate pin (#4638)
Browse files Browse the repository at this point in the history
  • Loading branch information
acha-bill authored Apr 17, 2024
1 parent 06f92b8 commit 86f9468
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
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

0 comments on commit 86f9468

Please sign in to comment.