Skip to content

Commit

Permalink
fix(sdk)!: result requires to be provided process id #263
Browse files Browse the repository at this point in the history
  • Loading branch information
TillaTheHun0 committed Jan 2, 2024
1 parent b2c436a commit 8965dee
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 25 deletions.
7 changes: 3 additions & 4 deletions sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ Read the result of the message evaluation from an `ao` Compute Unit `cu`
import { result } from "@permaweb/ao-sdk";

let { messages, spawns, output, error } = await result({
message: "VkjFCALjk4xxuCilddKS8ShZ-9HdeqeuYQOgMgWucro",
message: "l3hbt-rIJ_dr9at-eQ3EVajHWMnxPNm9eBtXpzsFWZc",
process: "5SGJUlPwlenkyuG9-xWh0Rcf0azm8XEd5RBTiutgWAg"
});
```

Expand Down Expand Up @@ -99,16 +100,14 @@ specify those components by providing their urls to `connect`. You can currently
- The GATEWAY_URL
- The Messenger Unit URL
- The Compute Unit URL
- The Scheduler Unit URL

```js
import { connect } from "@permaweb/ao-sdk";

const { spawn, message, result } = connect({
GATEWAY_URL: "...",
MU_URL: "...",
CU_URL: "...",
SU_URL: "...",
CU_URL: "..."
});
```

Expand Down
5 changes: 2 additions & 3 deletions sdk/src/client/ao-cu.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ import { fromPromise, of } from 'hyper-async'
* @returns {LoadResult}
*/
export function loadResultWith ({ fetch, CU_URL, logger }) {
return ({ id }) => {
return of({ id })
.map(({ id }) => `${CU_URL}/result/${id}`)
return ({ id, processId }) => {
return of(`${CU_URL}/result/${id}?process-id=${processId}`)
.map(logger.tap('fetching message result from CU'))
.chain(fromPromise(async (url) =>
fetch(url, {
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/client/ao-cu.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('ao-cu', () => {
loadResultWith({
CU_URL: 'https://foo.bar',
fetch: async (url, options) => {
assert.equal(url, 'https://foo.bar/result/message-123')
assert.equal(url, 'https://foo.bar/result/message-123?process-id=process-123')
assert.deepStrictEqual(options, {
method: 'GET',
headers: {
Expand Down Expand Up @@ -48,7 +48,7 @@ describe('ao-cu', () => {
})
)

await loadResult({ id: 'message-123' })
await loadResult({ id: 'message-123', processId: 'process-123' })
.then(res => assert.deepStrictEqual(res, {
output: '',
messages: [
Expand Down
3 changes: 2 additions & 1 deletion sdk/src/dal.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const tagSchema = z.object({

export const loadResultSchema = z.function()
.args(z.object({
id: z.string().min(1, { message: 'message id is required' })
id: z.string().min(1, { message: 'message id is required' }),
processId: z.string().min(1, { message: 'process id is required' })
}))
.returns(z.promise(z.any()))

Expand Down
5 changes: 3 additions & 2 deletions sdk/src/lib/result/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { readWith } from './read.js'
*
* @typedef ReadResultArgs
* @property {string} message - the transaction id of the message
* @property {string} process - the transaction id of the process that received the message
*
* @callback ReadResult
* @param {ReadResultArgs} args
Expand All @@ -25,8 +26,8 @@ export function resultWith (env) {
const verifyInput = verifyInputWith(env)
const read = readWith(env)

return ({ message }) => {
return of({ id: message })
return ({ message, process }) => {
return of({ id: message, processId: process })
.chain(verifyInput)
.chain(read)
.map(
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/lib/result/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function readWith ({ loadResult }) {
loadResult = fromPromise(loadResultSchema.implement(loadResult))

return (ctx) => {
return of({ id: ctx.id })
return of({ id: ctx.id, processId: ctx.processId })
.chain(loadResult)
}
}
6 changes: 4 additions & 2 deletions sdk/src/lib/result/read.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ describe('read', () => {
const read = readWith({
loadResult: async (args) => {
assert.deepStrictEqual(args, {
id: 'message-123'
id: 'message-123',
processId: 'process-123'
})

return {
Expand Down Expand Up @@ -36,7 +37,8 @@ describe('read', () => {
})

const res = await read({
id: 'message-123'
id: 'message-123',
processId: 'process-123'
}).toPromise()

assert.deepStrictEqual(res, {
Expand Down
3 changes: 2 additions & 1 deletion sdk/src/lib/result/verify-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { of } from 'hyper-async'
import { z } from 'zod'

const inputSchema = z.object({
id: z.string().min(1, { message: 'id is required to be a message id' })
id: z.string().min(1, { message: 'message is required to be a message id' }),
processId: z.string().min(1, { message: 'process is required to be a process id' })
})

/**
Expand Down
17 changes: 8 additions & 9 deletions sdk/src/lib/result/verify-input.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,12 @@ describe('verify-input', () => {
const verifyInput = verifyInputWith()

await verifyInput({
id: 'message-123'
id: 'message-123',
processId: 'process-123'
}).toPromise()
.then(res => assert.deepStrictEqual(res, {
id: 'message-123'
}))

await verifyInput({
id: 'message-123'
}).toPromise()
.then(res => assert.deepStrictEqual(res, {
id: 'message-123'
id: 'message-123',
processId: 'process-123'
}))
})

Expand All @@ -32,5 +27,9 @@ describe('verify-input', () => {
await verifyInput('message-123').toPromise()
.then(() => assert.fail('unreachable. Should have failed'))
.catch(assert.ok)

await verifyInput({ id: 'message-123', process: 123 }).toPromise()
.then(() => assert.fail('unreachable. Should have failed'))
.catch(assert.ok)
})
})

0 comments on commit 8965dee

Please sign in to comment.