-
Notifications
You must be signed in to change notification settings - Fork 27
feat: use PDPVerifier.findPieceIdsByCid for efficient CID→ID lookups #718
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
hugomrdias
merged 2 commits into
FilOzone:master
from
Chaitu-Tatipamula:find-piece-ids-by-cid
Apr 14, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
packages/synapse-core/src/pdp-verifier/find-piece-ids-by-cid.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| import type { Simplify } from 'type-fest' | ||
| import { | ||
| type Address, | ||
| type Chain, | ||
| type Client, | ||
| type ContractFunctionParameters, | ||
| type ContractFunctionReturnType, | ||
| type ReadContractErrorType, | ||
| type Transport, | ||
| toHex, | ||
| } from 'viem' | ||
| import { readContract } from 'viem/actions' | ||
| import type { pdpVerifierAbi } from '../abis/generated.ts' | ||
| import { asChain } from '../chains.ts' | ||
| import type { PieceCID } from '../piece/piece.ts' | ||
| import type { ActionCallChain } from '../types.ts' | ||
|
|
||
| export namespace findPieceIdsByCid { | ||
| export type OptionsType = { | ||
| /** The ID of the data set to search in. */ | ||
| dataSetId: bigint | ||
| /** The PieceCID to search for. */ | ||
| pieceCid: PieceCID | ||
| /** The starting piece ID for the search. @default 0n */ | ||
| startPieceId?: bigint | ||
| /** The maximum number of results to return. @default 1n */ | ||
| limit?: bigint | ||
| /** PDP Verifier contract address. If not provided, the default is the PDP Verifier contract address for the chain. */ | ||
| contractAddress?: Address | ||
| } | ||
|
|
||
| export type OutputType = readonly bigint[] | ||
|
|
||
| /** | ||
| * `uint256[]` - Array of piece IDs matching the given CID | ||
| */ | ||
| export type ContractOutputType = ContractFunctionReturnType< | ||
| typeof pdpVerifierAbi, | ||
| 'pure' | 'view', | ||
| 'findPieceIdsByCid' | ||
| > | ||
|
|
||
| export type ErrorType = asChain.ErrorType | ReadContractErrorType | ||
| } | ||
|
|
||
| /** | ||
| * Find piece IDs for a given PieceCID in a data set. | ||
| * | ||
| * Uses the on-chain `findPieceIdsByCid` function for efficient CID→ID lookup. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * import { findPieceIdsByCid } from '@filoz/synapse-core/pdp-verifier' | ||
| * import { calibration } from '@filoz/synapse-core/chains' | ||
| * import { createPublicClient, http } from 'viem' | ||
| * import * as Piece from '@filoz/synapse-core/piece' | ||
| * | ||
| * const client = createPublicClient({ | ||
| * chain: calibration, | ||
| * transport: http(), | ||
| * }) | ||
| * | ||
| * const pieceCid = Piece.parse('bafkzcibcd4bdomn3tgwgrh3g532zopskstnbrd2n3sxfqbze7rxt7vqn7veigmy') | ||
| * const pieceIds = await findPieceIdsByCid(client, { | ||
| * dataSetId: 1n, | ||
| * pieceCid, | ||
| * }) | ||
| * // pieceIds is an array of bigint IDs matching the CID | ||
| * ``` | ||
| * | ||
| * @param client - The client to use to find piece IDs. | ||
| * @param options - {@link findPieceIdsByCid.OptionsType} | ||
| * @returns Array of piece IDs matching the CID {@link findPieceIdsByCid.OutputType} | ||
| * @throws Errors {@link findPieceIdsByCid.ErrorType} | ||
| */ | ||
| export async function findPieceIdsByCid( | ||
| client: Client<Transport, Chain>, | ||
| options: findPieceIdsByCid.OptionsType | ||
| ): Promise<findPieceIdsByCid.OutputType> { | ||
| return await readContract( | ||
| client, | ||
| findPieceIdsByCidCall({ | ||
| chain: client.chain, | ||
| dataSetId: options.dataSetId, | ||
| pieceCid: options.pieceCid, | ||
| startPieceId: options.startPieceId, | ||
| limit: options.limit, | ||
| contractAddress: options.contractAddress, | ||
| }) | ||
| ) | ||
| } | ||
|
|
||
| export namespace findPieceIdsByCidCall { | ||
| export type OptionsType = Simplify<findPieceIdsByCid.OptionsType & ActionCallChain> | ||
| export type ErrorType = asChain.ErrorType | ||
| export type OutputType = ContractFunctionParameters<typeof pdpVerifierAbi, 'pure' | 'view', 'findPieceIdsByCid'> | ||
| } | ||
|
|
||
| /** | ||
| * Create a call to the {@link findPieceIdsByCid} function for use with the multicall or readContract function. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * import { findPieceIdsByCidCall } from '@filoz/synapse-core/pdp-verifier' | ||
| * import { calibration } from '@filoz/synapse-core/chains' | ||
| * import { createPublicClient, http } from 'viem' | ||
| * import { readContract } from 'viem/actions' | ||
| * import * as Piece from '@filoz/synapse-core/piece' | ||
| * | ||
| * const client = createPublicClient({ | ||
| * chain: calibration, | ||
| * transport: http(), | ||
| * }) | ||
| * | ||
| * const pieceCid = Piece.parse('bafkzcibcd4bdomn3tgwgrh3g532zopskstnbrd2n3sxfqbze7rxt7vqn7veigmy') | ||
| * const result = await readContract(client, findPieceIdsByCidCall({ | ||
| * chain: calibration, | ||
| * dataSetId: 1n, | ||
| * pieceCid, | ||
| * })) | ||
| * ``` | ||
| * | ||
| * @param options - {@link findPieceIdsByCidCall.OptionsType} | ||
| * @returns The call to the findPieceIdsByCid function {@link findPieceIdsByCidCall.OutputType} | ||
| * @throws Errors {@link findPieceIdsByCidCall.ErrorType} | ||
| */ | ||
| export function findPieceIdsByCidCall(options: findPieceIdsByCidCall.OptionsType) { | ||
| const chain = asChain(options.chain) | ||
| return { | ||
| abi: chain.contracts.pdp.abi, | ||
| address: options.contractAddress ?? chain.contracts.pdp.address, | ||
| functionName: 'findPieceIdsByCid', | ||
| args: [options.dataSetId, { data: toHex(options.pieceCid.bytes) }, options.startPieceId ?? 0n, options.limit ?? 1n], | ||
| } satisfies findPieceIdsByCidCall.OutputType | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
packages/synapse-core/test/find-piece-ids-by-cid.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| import assert from 'assert' | ||
| import { setup } from 'iso-web/msw' | ||
| import { createPublicClient, http, toHex } from 'viem' | ||
| import { calibration, mainnet } from '../src/chains.ts' | ||
| import { JSONRPC, presets } from '../src/mocks/jsonrpc/index.ts' | ||
| import { findPieceIdsByCid, findPieceIdsByCidCall } from '../src/pdp-verifier/find-piece-ids-by-cid.ts' | ||
| import * as Piece from '../src/piece/piece.ts' | ||
|
|
||
| describe('findPieceIdsByCid', () => { | ||
| const server = setup() | ||
|
|
||
| before(async () => { | ||
| await server.start() | ||
| }) | ||
|
|
||
| after(() => { | ||
| server.stop() | ||
| }) | ||
|
|
||
| beforeEach(() => { | ||
| server.resetHandlers() | ||
| }) | ||
|
|
||
| describe('findPieceIdsByCidCall', () => { | ||
| const pieceCid = Piece.parse('bafkzcibcd4bdomn3tgwgrh3g532zopskstnbrd2n3sxfqbze7rxt7vqn7veigmy') | ||
|
|
||
| it('should create call with calibration chain defaults', () => { | ||
| const call = findPieceIdsByCidCall({ | ||
| chain: calibration, | ||
| dataSetId: 1n, | ||
| pieceCid, | ||
| }) | ||
|
|
||
| assert.equal(call.functionName, 'findPieceIdsByCid') | ||
| assert.deepEqual(call.args, [1n, { data: toHex(pieceCid.bytes) }, 0n, 1n]) | ||
| assert.equal(call.address, calibration.contracts.pdp.address) | ||
| assert.equal(call.abi, calibration.contracts.pdp.abi) | ||
| }) | ||
|
|
||
| it('should create call with mainnet chain defaults', () => { | ||
| const call = findPieceIdsByCidCall({ | ||
| chain: mainnet, | ||
| dataSetId: 1n, | ||
| pieceCid, | ||
| }) | ||
|
|
||
| assert.equal(call.functionName, 'findPieceIdsByCid') | ||
| assert.deepEqual(call.args, [1n, { data: toHex(pieceCid.bytes) }, 0n, 1n]) | ||
| assert.equal(call.address, mainnet.contracts.pdp.address) | ||
| assert.equal(call.abi, mainnet.contracts.pdp.abi) | ||
| }) | ||
|
|
||
| it('should use provided startPieceId and limit', () => { | ||
| const call = findPieceIdsByCidCall({ | ||
| chain: calibration, | ||
| dataSetId: 1n, | ||
| pieceCid, | ||
| startPieceId: 10n, | ||
| limit: 5n, | ||
| }) | ||
|
|
||
| assert.deepEqual(call.args, [1n, { data: toHex(pieceCid.bytes) }, 10n, 5n]) | ||
| }) | ||
|
|
||
| it('should use custom address when provided', () => { | ||
| const customAddress = '0x1234567890123456789012345678901234567890' | ||
| const call = findPieceIdsByCidCall({ | ||
| chain: calibration, | ||
| dataSetId: 1n, | ||
| pieceCid, | ||
| contractAddress: customAddress, | ||
| }) | ||
|
|
||
| assert.equal(call.address, customAddress) | ||
| }) | ||
| }) | ||
|
|
||
| describe('findPieceIdsByCid (with mocked RPC)', () => { | ||
| it('should find piece IDs by CID', async () => { | ||
| server.use(JSONRPC(presets.basic)) | ||
|
|
||
| const client = createPublicClient({ | ||
| chain: calibration, | ||
| transport: http(), | ||
| }) | ||
|
|
||
| const pieceCid = Piece.parse('bafkzcibcd4bdomn3tgwgrh3g532zopskstnbrd2n3sxfqbze7rxt7vqn7veigmy') | ||
| const result = await findPieceIdsByCid(client, { dataSetId: 1n, pieceCid }) | ||
|
|
||
| assert.ok(Array.isArray(result)) | ||
| assert.equal(result.length, 1) | ||
| assert.equal(result[0], 0n) | ||
| }) | ||
|
|
||
| it('should return empty array when piece not found', async () => { | ||
| server.use( | ||
| JSONRPC({ | ||
| ...presets.basic, | ||
| pdpVerifier: { | ||
| ...presets.basic.pdpVerifier, | ||
| findPieceIdsByCid: () => [[]], | ||
| }, | ||
| }) | ||
| ) | ||
|
|
||
| const client = createPublicClient({ | ||
| chain: calibration, | ||
| transport: http(), | ||
| }) | ||
|
|
||
| const pieceCid = Piece.parse('bafkzcibcd4bdomn3tgwgrh3g532zopskstnbrd2n3sxfqbze7rxt7vqn7veigmy') | ||
| const result = await findPieceIdsByCid(client, { dataSetId: 1n, pieceCid }) | ||
|
|
||
| assert.ok(Array.isArray(result)) | ||
| assert.equal(result.length, 0) | ||
| }) | ||
|
|
||
| it('should return multiple piece IDs', async () => { | ||
| server.use( | ||
| JSONRPC({ | ||
| ...presets.basic, | ||
| pdpVerifier: { | ||
| ...presets.basic.pdpVerifier, | ||
| findPieceIdsByCid: () => [[42n, 99n]], | ||
| }, | ||
| }) | ||
| ) | ||
|
|
||
| const client = createPublicClient({ | ||
| chain: calibration, | ||
| transport: http(), | ||
| }) | ||
|
|
||
| const pieceCid = Piece.parse('bafkzcibcd4bdomn3tgwgrh3g532zopskstnbrd2n3sxfqbze7rxt7vqn7veigmy') | ||
| const result = await findPieceIdsByCid(client, { dataSetId: 1n, pieceCid, limit: 10n }) | ||
|
|
||
| assert.ok(Array.isArray(result)) | ||
| assert.equal(result.length, 2) | ||
| assert.equal(result[0], 42n) | ||
| assert.equal(result[1], 99n) | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 seterror is thrown from_getPieceIdByCID, but thecreateErrorcontext labels it as'deletePiece'. Rename the method in the error context to'getPieceIdByCID'so consumers can identify the real source of the failure.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same applies here as per above comment