Skip to content

Commit

Permalink
fix(api): return empty array for pins instead of null (#4971)
Browse files Browse the repository at this point in the history
  • Loading branch information
gacevicljubisa authored Feb 3, 2025
1 parent 941f4fc commit 7df2cc3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
10 changes: 10 additions & 0 deletions pkg/api/pin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ func TestPinHandlers(t *testing.T) {
checkPinHandlers(t, client, rootHash, true)
})

t.Run("no pins", func(t *testing.T) {
jsonhttptest.Request(t, client, http.MethodGet, "/pins", http.StatusOK,
jsonhttptest.WithExpectedJSONResponse(struct {
References []swarm.Address `json:"references"`
}{
References: make([]swarm.Address, 0),
}),
)
})

t.Run("bytes missing", func(t *testing.T) {
jsonhttptest.Request(t, client, http.MethodPost, "/pins/"+swarm.RandAddress(t).String(), http.StatusNotFound)
})
Expand Down
6 changes: 1 addition & 5 deletions pkg/storer/internal/pinning/pinning.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ type collectionPutter struct {
// Put adds a chunk to the pin collection.
// The user of the putter MUST mutex lock the call to prevent data-races across multiple upload sessions.
func (c *collectionPutter) Put(ctx context.Context, st transaction.Store, ch swarm.Chunk) error {

// do not allow any Puts after putter was closed
if c.closed {
return errPutterAlreadyClosed
Expand Down Expand Up @@ -129,7 +128,6 @@ func (c *collectionPutter) Close(st storage.IndexStore, root swarm.Address) erro

collection := &pinCollectionItem{Addr: root}
has, err := st.Has(collection)

if err != nil {
return fmt.Errorf("pin store: check previous root: %w", err)
}
Expand Down Expand Up @@ -176,7 +174,6 @@ func (c *collectionPutter) Cleanup(st transaction.Storage) error {

// CleanupDirty will iterate over all the dirty collections and delete them.
func CleanupDirty(st transaction.Storage) error {

dirtyCollections := make([]*dirtyCollection, 0)
err := st.IndexStore().Iterate(
storage.Query{
Expand Down Expand Up @@ -212,7 +209,7 @@ func HasPin(st storage.Reader, root swarm.Address) (bool, error) {

// Pins lists all the added pinning collections.
func Pins(st storage.Reader) ([]swarm.Address, error) {
var pins []swarm.Address
pins := make([]swarm.Address, 0)
err := st.Iterate(storage.Query{
Factory: func() storage.Item { return new(pinCollectionItem) },
ItemProperty: storage.QueryItemID,
Expand Down Expand Up @@ -258,7 +255,6 @@ func deleteCollectionChunks(ctx context.Context, st transaction.Storage, collect
)
})
})

}(item)
}

Expand Down
14 changes: 14 additions & 0 deletions pkg/storer/internal/pinning/pinning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,20 @@ func TestPinStore(t *testing.T) {
t.Fatalf("unexpected error on close, want: %v, got: %v", pinstore.ErrCollectionRootAddressIsZero, err)
}
})

t.Run("have 0 pins", func(t *testing.T) {
pins, err := pinstore.Pins(internal.NewInmemStorage().IndexStore())
if err != nil {
t.Fatal(err)
}
if len(pins) != 0 {
t.Fatalf("expected 0 pins, found %d", len(pins))
}

if pins == nil {
t.Fatal("pins is nil")
}
})
}

func TestCleanup(t *testing.T) {
Expand Down

0 comments on commit 7df2cc3

Please sign in to comment.