Skip to content

Commit c48c67e

Browse files
feat(sdk): use findPieceIdsByCid in pieceStatus and _getPieceIdByCID
1 parent f36e2ff commit c48c67e

2 files changed

Lines changed: 17 additions & 12 deletions

File tree

packages/synapse-sdk/src/storage/context.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,15 +1048,16 @@ export class StorageContext {
10481048
throw createError('StorageContext', 'deletePiece', 'Invalid PieceCID provided')
10491049
}
10501050

1051-
const dataSetData = await SP.getDataSet({
1052-
serviceURL: this._pdpEndpoint,
1051+
const pieceIds = await PDPVerifier.findPieceIdsByCid(this._client, {
10531052
dataSetId: this.dataSetId,
1053+
pieceCid: parsedPieceCID,
1054+
startPieceId: 0n,
1055+
limit: 1n,
10541056
})
1055-
const pieceData = dataSetData.pieces.find((piece) => piece.pieceCid.toString() === parsedPieceCID.toString())
1056-
if (pieceData == null) {
1057+
if (pieceIds.length === 0) {
10571058
throw createError('StorageContext', 'deletePiece', 'Piece not found in data set')
10581059
}
1059-
return pieceData.pieceId
1060+
return pieceIds[0]
10601061
}
10611062

10621063
/**
@@ -1108,9 +1109,12 @@ export class StorageContext {
11081109
}
11091110

11101111
// Run multiple operations in parallel for better performance
1111-
const [activePieces, nextChallengeEpoch, currentEpoch, pdpConfig, providerInfo] = await Promise.all([
1112-
PDPVerifier.getActivePieces(this._client, {
1112+
const [pieceIds, nextChallengeEpoch, currentEpoch, pdpConfig, providerInfo] = await Promise.all([
1113+
PDPVerifier.findPieceIdsByCid(this._client, {
11131114
dataSetId: this.dataSetId,
1115+
pieceCid: parsedPieceCID,
1116+
startPieceId: 0n,
1117+
limit: 1n,
11141118
}),
11151119
PDPVerifier.getNextChallengeEpoch(this._client, {
11161120
dataSetId: this.dataSetId,
@@ -1123,14 +1127,13 @@ export class StorageContext {
11231127
this.getProviderInfo().catch(() => null),
11241128
])
11251129

1126-
const pieceData = activePieces.pieces.find((piece) => piece.cid.equals(parsedPieceCID))
1127-
if (pieceData === undefined) {
1130+
if (pieceIds.length === 0) {
11281131
return null
11291132
}
1133+
const pieceId = pieceIds[0]
11301134

11311135
// Initialize return values
11321136
let retrievalUrl: string | null = null
1133-
let pieceId: bigint | undefined
11341137
let lastProven: Date | null = null
11351138
let nextProofDue: Date | null = null
11361139
let inChallengeWindow = false
@@ -1147,8 +1150,6 @@ export class StorageContext {
11471150

11481151
// Process proof timing data if we have data set data and PDP config
11491152
if (pdpConfig != null) {
1150-
pieceId = pieceData.id
1151-
11521153
// Calculate timing based on nextChallengeEpoch
11531154
if (nextChallengeEpoch > 0n) {
11541155
// nextChallengeEpoch is when the challenge window STARTS, not ends!

packages/synapse-sdk/src/test/storage.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,6 +1415,10 @@ describe('StorageService', () => {
14151415
server.use(
14161416
Mocks.JSONRPC({
14171417
...Mocks.presets.basic,
1418+
pdpVerifier: {
1419+
...Mocks.presets.basic.pdpVerifier,
1420+
findPieceIdsByCid: () => [[]],
1421+
},
14181422
})
14191423
)
14201424
const synapse = new Synapse({ client, source: null })

0 commit comments

Comments
 (0)