Skip to content

feat: use PDPVerifier.findPieceIdsByCid for efficient CID→ID lookups#718

Open
Chaitu-Tatipamula wants to merge 2 commits intoFilOzone:masterfrom
Chaitu-Tatipamula:find-piece-ids-by-cid
Open

feat: use PDPVerifier.findPieceIdsByCid for efficient CID→ID lookups#718
Chaitu-Tatipamula wants to merge 2 commits intoFilOzone:masterfrom
Chaitu-Tatipamula:find-piece-ids-by-cid

Conversation

@Chaitu-Tatipamula
Copy link
Copy Markdown
Contributor

Closes #667

Summary

Replaces expensive full-piece-list fetches with the new PDPVerifier.findPieceIdsByCid contract method for direct CID→ID lookups.

Changes

synapse-core

  • New: findPieceIdsByCid + findPieceIdsByCidCall in pdp-verifier/
  • Mocks: Added findPieceIdsByCid handler and default preset

synapse-sdk

  • pieceStatus(): Replaced getActivePieces() + .find() with single findPieceIdsByCid() call
  • _getPieceIdByCID(): Replaced SP API getDataSet() + .find() with findPieceIdsByCid() call

Tests

  • New unit tests for findPieceIdsByCid call builder and mocked RPC
  • Updated pieceStatus test for "piece not found" scenario

Why

Previously pieceStatus() fetched all active pieces then searched linearly — O(n) and potentially fails on large datasets. Now it's a single targeted contract call.

@BigLep BigLep moved this from 📌 Triage to 🔎 Awaiting review in FOC Apr 8, 2026
@BigLep BigLep requested a review from Copilot April 8, 2026 05:48
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves CID→pieceId lookups by switching from “fetch all pieces then search” patterns to a direct on-chain PDPVerifier.findPieceIdsByCid call, reducing RPC payload size and avoiding O(n) scans in the SDK.

Changes:

  • Added findPieceIdsByCid + findPieceIdsByCidCall to synapse-core PDP verifier helpers (with mocks + presets support).
  • Updated synapse-sdk piece lookup logic (pieceStatus() and _getPieceIdByCID()) to use findPieceIdsByCid(..., limit: 1n) instead of fetching full piece lists / SP dataset metadata.
  • Added/updated unit tests to cover the new call builder + mocked RPC behavior and the “piece not found” scenario.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
packages/synapse-sdk/src/storage/context.ts Replaces linear searches / SP dataset fetch with PDPVerifier.findPieceIdsByCid for piece existence + ID resolution.
packages/synapse-sdk/src/test/storage.test.ts Updates pieceStatus() test setup to mock findPieceIdsByCid returning an empty result.
packages/synapse-core/src/pdp-verifier/find-piece-ids-by-cid.ts Introduces the new contract read helper + call builder with sensible defaults.
packages/synapse-core/src/pdp-verifier/index.ts Re-exports the new helper from the PDP verifier module entrypoint.
packages/synapse-core/src/mocks/jsonrpc/pdp.ts Adds JSON-RPC mock handler support for findPieceIdsByCid.
packages/synapse-core/src/mocks/jsonrpc/index.ts Extends the default mock preset to include findPieceIdsByCid.
packages/synapse-core/test/find-piece-ids-by-cid.test.ts Adds unit tests for call builder defaults and mocked RPC return shapes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 1046 to 1049
const parsedPieceCID = Piece.asPieceCID(pieceCid)
if (parsedPieceCID == null) {
throw createError('StorageContext', 'deletePiece', 'Invalid PieceCID provided')
}
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In _getPieceIdByCID, the error context uses 'deletePiece' as the method name. This makes thrown errors misleading (they originate from getPieceIdByCID). Update the createError call to use 'getPieceIdByCID' here for accurate error reporting.

Copilot uses AI. Check for mistakes.
Comment on lines +1057 to 1059
if (pieceIds.length === 0) {
throw createError('StorageContext', 'deletePiece', 'Piece not found in data set')
}
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This Piece not found in data set error is thrown from _getPieceIdByCID, but the createError context labels it as 'deletePiece'. Rename the method in the error context to 'getPieceIdByCID' so consumers can identify the real source of the failure.

Copilot uses AI. Check for mistakes.
Comment on lines 1412 to 1416
describe('pieceStatus()', () => {
const mockPieceCID = 'bafkzcibeqcad6efnpwn62p5vvs5x3nh3j7xkzfgb3xtitcdm2hulmty3xx4tl3wace'
it('should return exists=false when piece not in data set', async () => {
server.use(
Mocks.JSONRPC({
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test name says it returns exists=false, but the assertion expects null (the current pieceStatus() API). Consider renaming the test to reflect the actual behavior (e.g., “returns null when piece not in data set”) to avoid confusion when reading failures.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: 🔎 Awaiting review

Development

Successfully merging this pull request may close these issues.

Use PDPVerifier#findPieceIdsByCid to avoid piece ID lookup by search

3 participants