Skip to content

Commit

Permalink
chore: address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Nov 8, 2023
1 parent 363759a commit 9f33fc4
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 270 deletions.
6 changes: 2 additions & 4 deletions packages/capabilities/src/filecoin/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ import { Schema } from '@ucanto/validator'
/**
* @see https://github.com/filecoin-project/FIPs/pull/758/files
*/
export const FR32_SHA2_256_TRUNC254_PADDED_BINARY_TREE = /** @type {const} */ (
0x1011
)
const FR32_SHA2_256_TRUNC254_PADDED_BINARY_TREE = /** @type {const} */ (0x1011)
/**
* @see https://github.com/filecoin-project/FIPs/pull/758/files
*/
export const RAW_CODE = /** @type {const} */ (0x55)
const RAW_CODE = /** @type {const} */ (0x55)

export const PieceLink = /** @type {import('../types.js').PieceLinkSchema} */ (
Schema.link({
Expand Down
10 changes: 4 additions & 6 deletions packages/capabilities/src/filecoin/storefront.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,17 @@ export const filecoinInfo = capability({
*/
with: Schema.did(),
nb: Schema.struct({
/**
* CID of the content that resulted in Filecoin piece.
*/
content: Schema.link(),
/**
* CID of the piece.
*
* @see https://github.com/filecoin-project/FIPs/pull/758/files
*/
piece: PieceLink.optional(),
piece: PieceLink,
}),
derives: (claim, from) => {
return (
and(equalWith(claim, from)) ||
and(checkLink(claim.nb.content, from.nb.content, 'nb.content')) ||
and(checkLink(claim.nb.piece, from.nb.piece, 'nb.piece')) ||
ok({})
)
},
Expand Down
24 changes: 0 additions & 24 deletions packages/filecoin-api/src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,6 @@ export class RecordNotFound extends Server.Failure {
}
}

export const ContentNotFoundErrorName = /** @type {const} */ ('ContentNotFound')
export class ContentNotFound extends Server.Failure {
get reason() {
return this.message
}

get name() {
return ContentNotFoundErrorName
}
}

export const InvalidContentPieceErrorName = /** @type {const} */ (
'InvalidContentPiece'
)
export class InvalidContentPiece extends Server.Failure {
get reason() {
return this.message
}

get name() {
return InvalidContentPieceErrorName
}
}

export const EncodeRecordErrorName = /** @type {const} */ ('EncodeRecordFailed')
export class EncodeRecordFailed extends Server.Failure {
get reason() {
Expand Down
38 changes: 13 additions & 25 deletions packages/filecoin-api/src/storefront/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import * as API from '../types.js'
import {
QueueOperationFailed,
StoreOperationFailed,
ContentNotFound,
InvalidContentPiece,
RecordNotFoundErrorName,
} from '../errors.js'

/**
Expand Down Expand Up @@ -238,26 +237,16 @@ async function findDataAggregationProof({ taskStore, receiptStore }, task) {
* @returns {Promise<API.UcantoInterface.Result<API.FilecoinInfoSuccess, API.FilecoinInfoFailure> | API.UcantoInterface.JoinBuilder<API.FilecoinInfoSuccess>>}
*/
export const filecoinInfo = async ({ capability }, context) => {
const { piece, content } = capability.nb
const { piece } = capability.nb

const queryRecords = await context.pieceStore.query({ content })
if (queryRecords.error) {
return { error: new StoreOperationFailed(queryRecords.error.message) }
} else if (!queryRecords.ok.length) {
// Get piece in store
const getPiece = await context.pieceStore.get({ piece })
if (getPiece.error && getPiece.error.name === RecordNotFoundErrorName) {
return {
error: new ContentNotFound(
`no piece record was previously stored for content ${content.toString()}`
),
}
}
if (Boolean(piece) && !queryRecords.ok[0].piece.equals(piece)) {
return {
error: new InvalidContentPiece(
`received piece ${piece?.toString()} is not the same as previously computed ${
queryRecords.ok[0].piece
} for content ${content.toString()}`
),
error: getPiece.error,
}
} else if (getPiece.error) {
return { error: new StoreOperationFailed(getPiece.error.message) }
}

// Check if `piece/accept` receipt exists to get to know aggregate where it is included on a deal
Expand All @@ -267,8 +256,8 @@ export const filecoinInfo = async ({ capability }, context) => {
audience: context.id,
with: context.id.did(),
nb: {
piece: queryRecords.ok[0].piece,
content,
piece,
content: getPiece.ok.content,
},
expiration: Infinity,
})
Expand All @@ -278,10 +267,9 @@ export const filecoinInfo = async ({ capability }, context) => {
pieceAcceptInvocation.link()
)
if (pieceAcceptReceiptGet.error) {
// TODO: see receipt chain to report processing
/** @type {API.UcantoInterface.OkBuilder<API.FilecoinInfoSuccess, API.FilecoinInfoFailure>} */
const processingResult = Server.ok({
piece: queryRecords.ok[0].piece,
piece,
deals: [],
})
return processingResult
Expand All @@ -308,14 +296,14 @@ export const filecoinInfo = async ({ capability }, context) => {
// Should not happen if there is `piece/accept` receipt
return {
error: new Server.Failure(
`no deals were obtained for aggregate ${pieceAcceptOut.aggregate} where piece ${queryRecords.ok[0].piece} is included`
`no deals were obtained for aggregate ${pieceAcceptOut.aggregate} where piece ${piece} is included`
),
}
}

/** @type {API.UcantoInterface.OkBuilder<API.FilecoinInfoSuccess, API.FilecoinInfoFailure>} */
const result = Server.ok({
piece: queryRecords.ok[0].piece,
piece,
deals: deals.map(([dealId, dealDetails]) => ({
aggregate: pieceAcceptOut.aggregate,
provider: dealDetails.provider,
Expand Down
8 changes: 4 additions & 4 deletions packages/filecoin-api/test/context/store.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as API from '../../src/types.js'
import { RecordNotFound, StoreOperationFailed } from '../../src/errors.js'
import { StoreOperationFailed, RecordNotFound } from '../../src/errors.js'

/**
* @typedef {import('../../src/types.js').StorePutError} StorePutError
Expand Down Expand Up @@ -47,7 +47,7 @@ export class Store {
const t = this.getFn(this.items, item)
if (!t) {
return {
error: new RecordNotFound(),
error: new RecordNotFound('not found'),
}
}
return {
Expand Down Expand Up @@ -85,7 +85,7 @@ export class Store {
const t = this.queryFn(this.items, search)
if (!t) {
return {
error: new RecordNotFound(),
error: new RecordNotFound('not found'),
}
}
return {
Expand Down Expand Up @@ -123,7 +123,7 @@ export class UpdatableStore extends Store {
const t = this.updateFn(this.items, key, item)
if (!t) {
return {
error: new RecordNotFound(),
error: new RecordNotFound('not found'),
}
}
return {
Expand Down
Loading

0 comments on commit 9f33fc4

Please sign in to comment.