Skip to content

Commit

Permalink
feat: handle retrievals for nested identity CIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Sep 1, 2022
1 parent ee63003 commit 820872f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 14 deletions.
40 changes: 26 additions & 14 deletions retrievalmarket/impl/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,25 +557,37 @@ func linksFromIdentityCid(identityCid cid.Cid) ([]cid.Cid, error) {
return nil, fmt.Errorf("collecting links from identity CID %s: %w", identityCid, err)
}

if len(links) > MaxIdentityCIDLinks {
return nil, fmt.Errorf("refusing to process identity CID with too many links (%d)", len(links))
}

// dedupe and convert from Link to Cid
cids := make([]cid.Cid, 0)
// convert from Link to Cid, handle nested identity CIDs, and dedupe
resultCids := make([]cid.Cid, 0)
for _, link_ := range links {
cid := link_.(cidlink.Link).Cid
var found bool
for _, c := range cids {
if c.Equals(cid) {
found = true
cids := []cid.Cid{link_.(cidlink.Link).Cid}
if cids[0].Prefix().MhType == multihash.IDENTITY {
// nested, recurse
// (just because you can, it doesn't mean you should, nested identity CIDs are an extra layer of silly)
cids, err = linksFromIdentityCid(cids[0])
if err != nil {
return nil, err
}
}
if !found {
cids = append(cids, cid)
for _, c := range cids {
// dedupe
var found bool
for _, rc := range resultCids {
if rc.Equals(c) {
found = true
}
}
if !found {
resultCids = append(resultCids, c)
}
}
}
return cids, err

if len(resultCids) > MaxIdentityCIDLinks {
return nil, fmt.Errorf("refusing to process identity CID with too many links (%d)", len(resultCids))
}

return resultCids, err
}

func (p *Provider) pieceInUnsealedSector(ctx context.Context, pieceInfo piecestore.PieceInfo) bool {
Expand Down
21 changes: 21 additions & 0 deletions retrievalmarket/impl/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,8 @@ func TestHandleQueryStream(t *testing.T) {
require.NoError(t, err)
identityCidWithBoth, err := makeIdentityCidWith([]cid.Cid{payloadCID, payloadCID2}, []byte("and some padding"))
require.NoError(t, err)
identityCidWithBothNested, err := makeIdentityCidWith([]cid.Cid{identityCidWith1, payloadCID2})
require.NoError(t, err)
identityCidWithBogus, err := makeIdentityCidWith([]cid.Cid{payloadCID, tut.GenerateCids(1)[0]})
require.NoError(t, err)
identityCidTooBig, err := makeIdentityCidWith([]cid.Cid{payloadCID}, tut.RandomBytes(2048))
Expand Down Expand Up @@ -918,6 +920,25 @@ func TestHandleQueryStream(t *testing.T) {
expectedUnsealPrice: expectedUnsealPrice,
},

{name: "When PieceCID is not provided and PayloadCID is an identity CID with two links (one nested) that are found",
expFunc: func(t *testing.T, pieceStore *tut.TestPieceStore, dagStore *tut.MockDagStoreWrapper) {
pieceStore.ExpectPiece(expectedPieceCID, expectedPiece) // both only appear in this piece, expect just this call
dagStore.AddBlockToPieceIndex(payloadCID, expectedPieceCID)
dagStore.AddBlockToPieceIndex(payloadCID, expectedPieceCID2)
dagStore.AddBlockToPieceIndex(payloadCID2, expectedPieceCID)
},
query: retrievalmarket.Query{PayloadCID: identityCidWithBothNested},
expResp: retrievalmarket.QueryResponse{
Status: retrievalmarket.QueryResponseAvailable,
PieceCIDFound: retrievalmarket.QueryItemAvailable,
Size: expectedSize,
},
expectedPricePerByte: expectedPricePerByte,
expectedPaymentInterval: expectedPaymentInterval,
expectedPaymentIntervalIncrease: expectedPaymentIntervalIncrease,
expectedUnsealPrice: expectedUnsealPrice,
},

{name: "When PieceCID is not provided and PayloadCID is an identity CID with a link that is not found",
expFunc: func(t *testing.T, pieceStore *tut.TestPieceStore, dagStore *tut.MockDagStoreWrapper) {},
query: retrievalmarket.Query{PayloadCID: identityCidWithBogus},
Expand Down

0 comments on commit 820872f

Please sign in to comment.