-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: check for blob/accept receipts before blob/add is concluded (#1459)
- Verifies that `blob/accept` has succeeded before concluding `blob/add` as per #1425 (comment). A location commitment is returned instead of the blob multihash. --------- Co-authored-by: Vasco Santos <santos.vasco10@gmail.com> Co-authored-by: Alan Shaw <alan.shaw@protocol.ai>
- Loading branch information
1 parent
7944077
commit 462518c
Showing
22 changed files
with
553 additions
and
93 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * as ShardedDAGIndex from './sharded-dag-index.js' | ||
export * from './digest-map.js' | ||
export { indexShardedDAG } from './util.js' |
This file contains 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 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 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 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 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,119 @@ | ||
import retry, { AbortError } from 'p-retry' | ||
import { CAR } from '@ucanto/transport' | ||
import { receiptsEndpoint } from './service.js' | ||
import { REQUEST_RETRIES } from './constants.js' | ||
|
||
export class ReceiptNotFound extends Error { | ||
/** | ||
* @param {import('multiformats').UnknownLink} taskCid | ||
*/ | ||
constructor(taskCid) { | ||
super() | ||
this.taskCid = taskCid | ||
} | ||
|
||
/* c8 ignore start */ | ||
get reason() { | ||
return `receipt not found for task ${this.taskCid} in the indexed workflow` | ||
} | ||
/* c8 ignore end */ | ||
|
||
get name() { | ||
return 'ReceiptNotFound' | ||
} | ||
} | ||
|
||
export class ReceiptMissing extends Error { | ||
/** | ||
* @param {import('multiformats').UnknownLink} taskCid | ||
*/ | ||
constructor(taskCid) { | ||
super() | ||
this.taskCid = taskCid | ||
} | ||
|
||
/* c8 ignore start */ | ||
get reason() { | ||
return `receipt missing for task ${this.taskCid}` | ||
} | ||
/* c8 ignore end */ | ||
|
||
get name() { | ||
return 'ReceiptMissing' | ||
} | ||
} | ||
|
||
/** | ||
* Polls for a receipt for an executed task by its CID. | ||
* | ||
* @param {import('multiformats').UnknownLink} taskCid | ||
* @param {import('./types.js').RequestOptions} [options] | ||
* @returns {Promise<import('@ucanto/interface').Receipt>} | ||
*/ | ||
export async function poll(taskCid, options = {}) { | ||
return await retry( | ||
async () => { | ||
const res = await get(taskCid, options) | ||
if (res.error) { | ||
// @ts-ignore | ||
if (res.error.name === 'ReceiptNotFound') { | ||
// throw an error that will cause `p-retry` to retry with | ||
throw res.error | ||
} else { | ||
throw new AbortError( | ||
new Error('failed to fetch blob/accept receipt', { | ||
cause: res.error, | ||
}) | ||
) | ||
} | ||
} | ||
return res.ok | ||
}, | ||
{ | ||
onFailedAttempt: console.warn, | ||
/* c8 ignore next */ | ||
retries: options.retries ?? REQUEST_RETRIES, | ||
} | ||
) | ||
} | ||
|
||
/** | ||
* Get a receipt for an executed task by its CID. | ||
* | ||
* @param {import('multiformats').UnknownLink} taskCid | ||
* @param {import('./types.js').RequestOptions} [options] | ||
* @returns {Promise<import('@ucanto/client').Result<import('@ucanto/interface').Receipt, Error>>} | ||
*/ | ||
async function get(taskCid, options = {}) { | ||
// Fetch receipt from endpoint | ||
const url = new URL( | ||
taskCid.toString(), | ||
options.receiptsEndpoint ?? receiptsEndpoint | ||
) | ||
const fetchReceipt = options.fetch ?? globalThis.fetch.bind(globalThis) | ||
const workflowResponse = await fetchReceipt(url) | ||
/* c8 ignore start */ | ||
if (workflowResponse.status === 404) { | ||
return { | ||
error: new ReceiptNotFound(taskCid), | ||
} | ||
} | ||
/* c8 ignore stop */ | ||
// Get receipt from Message Archive | ||
const agentMessageBytes = new Uint8Array(await workflowResponse.arrayBuffer()) | ||
// Decode message | ||
const agentMessage = await CAR.request.decode({ | ||
body: agentMessageBytes, | ||
headers: {}, | ||
}) | ||
// Get receipt from the potential multiple receipts in the message | ||
const receipt = agentMessage.receipts.get(taskCid.toString()) | ||
if (!receipt) { | ||
return { | ||
error: new ReceiptMissing(taskCid), | ||
} | ||
} | ||
return { | ||
ok: receipt, | ||
} | ||
} |
This file contains 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 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.