Skip to content

Commit

Permalink
fix: receipt return types
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Oct 23, 2023
1 parent 7cacbbe commit 20a7a38
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 14 deletions.
9 changes: 7 additions & 2 deletions packages/capabilities/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,10 @@ export interface FilecoinSubmitSuccess {

export type FilecoinSubmitFailure = InvalidPieceCID | Ucanto.Failure

export type FilecoinAcceptSuccess = DataAggregationProof
export interface FilecoinAcceptSuccess extends DataAggregationProof {
aggregate: PieceLink
piece: PieceLink
}

export type FilecoinAcceptFailure =
| InvalidContentPiece
Expand Down Expand Up @@ -280,7 +283,9 @@ export interface AggregateOfferSuccess {
}
export type AggregateOfferFailure = Ucanto.Failure

export type AggregateAcceptSuccess = DealMetadata
export interface AggregateAcceptSuccess extends DealMetadata {
aggregate: PieceLink
}
export type AggregateAcceptFailure = InvalidPiece | Ucanto.Failure

export interface InvalidPiece extends Ucanto.Failure {
Expand Down
1 change: 1 addition & 0 deletions packages/filecoin-api/src/dealer/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export const aggregateAccept = async ({ capability }, context) => {

return {
ok: {
aggregate,
dataSource: get.ok.deal.dataSource,
dataType: get.ok.deal.dataType,
},
Expand Down
38 changes: 31 additions & 7 deletions packages/filecoin-api/src/storefront/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,22 @@ export const filecoinAccept = async ({ capability }, context) => {
})
.delegate()

return await findDataAggregationProof(context, fx.link())
const dataAggregationProof = await findDataAggregationProof(
context,
fx.link()
)
if (dataAggregationProof.error) {
return { error: new ProofNotFound(dataAggregationProof.error.message) }
}

return {
ok: {
aux: dataAggregationProof.ok.aux,
inclusion: dataAggregationProof.ok.inclusion,
piece,
aggregate: dataAggregationProof.ok.aggregate,
},
}
}

/**
Expand All @@ -154,13 +169,13 @@ export const filecoinAccept = async ({ capability }, context) => {
* receiptStore: API.Store<import('@ucanto/interface').UnknownLink, API.UcantoInterface.Receipt>
* }} stores
* @param {API.UcantoInterface.Link} task
* @returns {Promise<API.UcantoInterface.Result<API.DataAggregationProof, API.StoreOperationError|ProofNotFound>>}
* @returns {Promise<API.UcantoInterface.Result<API.DataAggregationProof & { aggregate: import('@web3-storage/data-segment').PieceLink}, API.StoreOperationError|ProofNotFound>>}
*/
async function findDataAggregationProof({ taskStore, receiptStore }, task) {
/** @type {API.InclusionProof|undefined} */
let inclusion
/** @type {API.DealMetadata|undefined} */
let dataAggregation
/** @type {API.AggregateAcceptSuccess|undefined} */
let aggregateAcceptReceipt
while (true) {
const [taskRes, receiptRes] = await Promise.all([
taskStore.get(task),
Expand All @@ -184,7 +199,7 @@ async function findDataAggregationProof({ taskStore, receiptStore }, task) {
if (ability === 'piece/accept' && receiptRes.ok.out.ok) {
inclusion = receiptRes.ok.out.ok.inclusion
} else if (ability === 'aggregate/accept' && receiptRes.ok.out.ok) {
dataAggregation = receiptRes.ok.out.ok
aggregateAcceptReceipt = receiptRes.ok.out.ok
}
if (!receiptRes.ok.fx.join) break
task = receiptRes.ok.fx.join
Expand All @@ -196,10 +211,19 @@ async function findDataAggregationProof({ taskStore, receiptStore }, task) {
),
}
}
if (!dataAggregation) {
if (!aggregateAcceptReceipt) {
return { error: new ProofNotFound('missing data aggregation proof') }
}
return { ok: { aux: dataAggregation, inclusion } }
return {
ok: {
aux: {
dataSource: aggregateAcceptReceipt.dataSource,
dataType: aggregateAcceptReceipt.dataType,
},
aggregate: aggregateAcceptReceipt.aggregate,
inclusion,
},
}
}

export const ProofNotFoundName = /** @type {const} */ ('ProofNotFound')
Expand Down
6 changes: 3 additions & 3 deletions packages/filecoin-api/test/context/receipts.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import * as API from '../../src/types.js'
* @param {API.PieceLink} context.piece
* @param {import('@ucanto/interface').Block} context.piecesBlock
* @param {API.InclusionProof} context.inclusionProof
* @param {API.AggregateAcceptSuccess} context.dealMetadata
* @param {API.AggregateAcceptSuccess} context.aggregateAcceptStatus
*/
export async function createInvocationsAndReceiptsForDealDataProofChain({
storefront,
Expand All @@ -25,7 +25,7 @@ export async function createInvocationsAndReceiptsForDealDataProofChain({
piece,
piecesBlock,
inclusionProof,
dealMetadata,
aggregateAcceptStatus,
}) {
const pieceOfferInvocation = await AggregatorCaps.pieceOffer
.invoke({
Expand Down Expand Up @@ -124,7 +124,7 @@ export async function createInvocationsAndReceiptsForDealDataProofChain({
issuer: dealer,
ran: aggregateAcceptInvocation.cid,
result: {
ok: /** @type {API.AggregateAcceptSuccess} */ (dealMetadata),
ok: /** @type {API.AggregateAcceptSuccess} */ (aggregateAcceptStatus),
},
})

Expand Down
3 changes: 3 additions & 0 deletions packages/filecoin-api/test/context/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export function getMockService() {
}

return Server.ok({
aggregate: invCap.nb.aggregate,
dataSource: {
dealID: 15151n,
},
Expand Down Expand Up @@ -159,6 +160,8 @@ export function getMockService() {

/** @type {API.UcantoInterface.OkBuilder<API.FilecoinAcceptSuccess, API.FilecoinAcceptFailure>} */
const result = Server.ok({
piece: invCap.nb.piece,
aggregate: invCap.nb.piece,
inclusion: {
subtree:
/** @type {import('@web3-storage/data-segment').ProofData} */ [
Expand Down
5 changes: 4 additions & 1 deletion packages/filecoin-api/test/events/storefront.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,10 @@ export const test = {
subtree: inclusionProof.ok[0],
index: inclusionProof.ok[1],
},
dealMetadata,
aggregateAcceptStatus: {
...dealMetadata,
aggregate: aggregate.link,
},
})

// Store all invocations and receipts, except for the very last receipt on the chain
Expand Down
5 changes: 4 additions & 1 deletion packages/filecoin-api/test/services/storefront.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,10 @@ export const test = {
subtree: inclusionProof.ok[0],
index: inclusionProof.ok[1],
},
dealMetadata,
aggregateAcceptStatus: {
...dealMetadata,
aggregate: aggregate.link,
},
})

const storedInvocationsAndReceiptsRes = await storeInvocationsAndReceipts({
Expand Down

0 comments on commit 20a7a38

Please sign in to comment.