diff --git a/storagemarket/impl/provider.go b/storagemarket/impl/provider.go index e09f5b8f..a1582e5c 100644 --- a/storagemarket/impl/provider.go +++ b/storagemarket/impl/provider.go @@ -478,8 +478,8 @@ func (p *Provider) GetLocalDeal(propCid cid.Cid) (storagemarket.MinerDeal, error return d, err } -func (p *Provider) ListLocalDealsPage(offsetPropCid *cid.Cid, count int) ([]storagemarket.MinerDeal, error) { - if count == 0 { +func (p *Provider) ListLocalDealsPage(startPropCid *cid.Cid, offset int, limit int) ([]storagemarket.MinerDeal, error) { + if limit == 0 { return []storagemarket.MinerDeal{}, nil } @@ -495,14 +495,23 @@ func (p *Provider) ListLocalDealsPage(offsetPropCid *cid.Cid, count int) ([]stor }) // Iterate through deals until we reach the target signed proposal cid, - // then add deals from that point up to count - page := make([]storagemarket.MinerDeal, 0, count) - for _, dl := range deals { - if offsetPropCid == nil || dl.ProposalCid == *offsetPropCid { + // find the offset from there, then add deals from that point up to limit + page := make([]storagemarket.MinerDeal, 0, limit) + startIndex := -1 + if startPropCid == nil { + startIndex = 0 + } + for i, dl := range deals { + // Find the deal with a proposal cid matching startPropCid + if startPropCid != nil && dl.ProposalCid == *startPropCid { + // Start adding deals from offset after the first matching deal + startIndex = i + offset + } + + if startIndex >= 0 && i >= startIndex { page = append(page, dl) - offsetPropCid = nil // add all deals from this point on } - if len(page) == count { + if len(page) == limit { return page, nil } } diff --git a/storagemarket/impl/provider_test.go b/storagemarket/impl/provider_test.go index 8162bb3f..69bfb520 100644 --- a/storagemarket/impl/provider_test.go +++ b/storagemarket/impl/provider_test.go @@ -179,8 +179,8 @@ func TestProvider_Migrations(t *testing.T) { require.NoError(t, err) require.Equal(t, len(deals), count) - // Verify get a page of deals without a nil offset proposal cid - listedDeals, err := provider.ListLocalDealsPage(nil, len(deals)) + // Verify get a page of deals without a nil start proposal cid + listedDeals, err := provider.ListLocalDealsPage(nil, 0, len(deals)) require.NoError(t, err) require.Len(t, listedDeals, len(deals)) for i, dl := range listedDeals { @@ -194,21 +194,28 @@ func TestProvider_Migrations(t *testing.T) { secondDeal := listedDeals[1] thirdDeal := listedDeals[2] - // Verify get a page of deals with a nil offset proposal cid and with a limit - listedDeals, err = provider.ListLocalDealsPage(nil, 2) + // Verify get a page of deals with a nil start proposal cid and with a limit + listedDeals, err = provider.ListLocalDealsPage(nil, 0, 2) require.NoError(t, err) require.Len(t, listedDeals, 2) // Verify correct deals require.Equal(t, firstDeal.ProposalCid, listedDeals[0].ProposalCid) require.Equal(t, secondDeal.ProposalCid, listedDeals[1].ProposalCid) - // Verify get a page of deals with an offset proposal cid and with a limit - listedDeals, err = provider.ListLocalDealsPage(&secondDeal.ProposalCid, 2) + // Verify get a page of deals with a start proposal cid and with a limit + listedDeals, err = provider.ListLocalDealsPage(&secondDeal.ProposalCid, 0, 2) require.NoError(t, err) require.Len(t, listedDeals, 2) // Verify correct deals require.Equal(t, secondDeal.ProposalCid, listedDeals[0].ProposalCid) require.Equal(t, thirdDeal.ProposalCid, listedDeals[1].ProposalCid) + + // Verify get a page of deals with a start proposal cid, and offset and a limit + listedDeals, err = provider.ListLocalDealsPage(&secondDeal.ProposalCid, 1, 1) + require.NoError(t, err) + require.Len(t, listedDeals, 1) + // Verify correct deals + require.Equal(t, thirdDeal.ProposalCid, listedDeals[0].ProposalCid) } func TestHandleDealStream(t *testing.T) { diff --git a/storagemarket/provider.go b/storagemarket/provider.go index 88df9ca6..b3a2213b 100644 --- a/storagemarket/provider.go +++ b/storagemarket/provider.go @@ -46,9 +46,9 @@ type StorageProvider interface { ListLocalDeals() ([]MinerDeal, error) // ListLocalDealsPage lists deals by creation time descending, starting - // at the deal with the given signed proposal cid and returning up to - // count deals - ListLocalDealsPage(offsetPropCid *cid.Cid, count int) ([]MinerDeal, error) + // at the deal with the given signed proposal cid, skipping offset deals + // and returning up to limit deals + ListLocalDealsPage(startPropCid *cid.Cid, offset int, limit int) ([]MinerDeal, error) // AddStorageCollateral adds storage collateral AddStorageCollateral(ctx context.Context, amount abi.TokenAmount) error