Skip to content

Commit

Permalink
fix: upgrade ucanto libs
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Apr 4, 2024
1 parent a78c3ea commit 5c6b000
Show file tree
Hide file tree
Showing 14 changed files with 277 additions and 208 deletions.
14 changes: 7 additions & 7 deletions packages/access-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@
"dependencies": {
"@ipld/car": "^5.1.1",
"@ipld/dag-ucan": "^3.4.0",
"@ucanto/client": "^9.0.0",
"@ucanto/core": "^9.0.1",
"@ucanto/interface": "^9.0.0",
"@ucanto/principal": "^9.0.0",
"@ucanto/transport": "^9.1.0",
"@ucanto/validator": "^9.0.1",
"@ucanto/client": "^9.0.1",
"@ucanto/core": "^10.0.0",
"@ucanto/interface": "^10.0.0",
"@ucanto/principal": "^9.0.1",
"@ucanto/transport": "^9.1.1",
"@ucanto/validator": "^9.0.2",
"@web3-storage/capabilities": "workspace:^",
"@web3-storage/did-mailto": "workspace:^",
"bigint-mod-arith": "^3.1.2",
Expand All @@ -125,7 +125,7 @@
"@types/sinon": "^10.0.19",
"@types/varint": "^6.0.1",
"@types/ws": "^8.5.4",
"@ucanto/server": "^9.0.1",
"@ucanto/server": "^10.0.0",
"assert": "^2.0.0",
"mocha": "^10.2.0",
"playwright-test": "^12.3.4",
Expand Down
10 changes: 5 additions & 5 deletions packages/capabilities/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@
"dist/src/**/*.d.ts.map"
],
"dependencies": {
"@ucanto/core": "^9.0.1",
"@ucanto/interface": "^9.0.0",
"@ucanto/principal": "^9.0.0",
"@ucanto/transport": "^9.1.0",
"@ucanto/validator": "^9.0.1",
"@ucanto/core": "^10.0.0",
"@ucanto/interface": "^10.0.0",
"@ucanto/principal": "^9.0.1",
"@ucanto/transport": "^9.1.1",
"@ucanto/validator": "^9.0.2",
"@web3-storage/data-segment": "^3.2.0",
"uint8arrays": "^5.0.3"
},
Expand Down
62 changes: 32 additions & 30 deletions packages/capabilities/src/ucan.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,40 +91,42 @@ export const conclude = capability({
with: Schema.did(),
// TODO: Should this just have bytes?
nb: Schema.struct({
/**
* A link to the UCAN invocation that this receipt is for.
*/
ran: UCANLink,
/**
* The value output of the invocation in Result format.
*/
out: Schema.unknown(),
/**
* Tasks that the invocation would like to enqueue.
*/
next: Schema.array(UCANLink),
/**
* Additional data about the receipt
*/
meta: Schema.unknown(),
/**
* The UTC Unix timestamp at which the Receipt was issued
*/
time: Schema.integer(),
bytes: Schema.Bytes,
// /**
// * A link to the UCAN invocation that this receipt is for.
// */
// ran: UCANLink,
// /**
// * The value output of the invocation in Result format.
// */
// out: Schema.unknown(),
// /**
// * Tasks that the invocation would like to enqueue.
// */
// next: Schema.array(UCANLink),
// /**
// * Additional data about the receipt
// */
// meta: Schema.unknown(),
// /**
// * The UTC Unix timestamp at which the Receipt was issued
// */
// time: Schema.integer(),
}),
derives: (claim, from) =>
// With field MUST be the same
and(equalWith(claim, from)) ??
// invocation MUST be the same
and(checkLink(claim.nb.ran, from.nb.ran, 'nb.ran')) ??
// value output MUST be the same
and(equal(claim.nb.out, from.nb.out, 'nb.out')) ??
// tasks to enqueue MUST be the same
and(equal(claim.nb.next, from.nb.next, 'nb.next')) ??
// additional data MUST be the same
and(equal(claim.nb.meta, from.nb.meta, 'nb.meta')) ??
// the receipt issue time MUST be the same
equal(claim.nb.time, from.nb.time, 'nb.time'),
equal(claim.nb.bytes, from.nb.bytes, 'nb.bytes'),
// // invocation MUST be the same
// and(checkLink(claim.nb.ran, from.nb.ran, 'nb.ran')) ??
// // value output MUST be the same
// and(equal(claim.nb.out, from.nb.out, 'nb.out')) ??
// // tasks to enqueue MUST be the same
// and(equal(claim.nb.next, from.nb.next, 'nb.next')) ??
// // additional data MUST be the same
// and(equal(claim.nb.meta, from.nb.meta, 'nb.meta')) ??
// // the receipt issue time MUST be the same
// equal(claim.nb.time, from.nb.time, 'nb.time'),
})

/**
Expand Down
16 changes: 8 additions & 8 deletions packages/filecoin-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,22 +153,22 @@
},
"dependencies": {
"@ipld/dag-ucan": "^3.4.0",
"@ucanto/client": "^9.0.0",
"@ucanto/core": "^9.0.1",
"@ucanto/interface": "^9.0.0",
"@ucanto/server": "^9.0.1",
"@ucanto/transport": "^9.1.0",
"@ucanto/client": "^9.0.1",
"@ucanto/core": "^10.0.0",
"@ucanto/interface": "^10.0.0",
"@ucanto/server": "^10.0.0",
"@ucanto/transport": "^9.1.1",
"@web3-storage/capabilities": "workspace:^",
"@web3-storage/content-claims": "^4.0.2",
"@web3-storage/content-claims": "^4.0.4",
"@web3-storage/data-segment": "^4.0.0",
"fr32-sha2-256-trunc254-padded-binary-tree-multihash": "^3.3.0",
"p-map": "^6.0.0"
},
"devDependencies": {
"@ipld/car": "^5.1.1",
"@types/mocha": "^10.0.1",
"@ucanto/client": "^9.0.0",
"@ucanto/principal": "^9.0.0",
"@ucanto/client": "^9.0.1",
"@ucanto/principal": "^9.0.1",
"@web-std/blob": "^3.0.5",
"@web3-storage/eslint-config-w3up": "workspace:^",
"@web3-storage/filecoin-client": "workspace:^",
Expand Down
2 changes: 1 addition & 1 deletion packages/filecoin-api/src/storefront/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ async function findDataAggregationProof({ taskStore, receiptStore }, task) {
aggregateAcceptReceipt = receiptRes.ok.out.ok
}
if (!receiptRes.ok.fx.join) break
task = receiptRes.ok.fx.join
task = receiptRes.ok.fx.join.link()
}
if (!inclusion) {
return {
Expand Down
12 changes: 6 additions & 6 deletions packages/filecoin-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,19 @@
],
"dependencies": {
"@ipld/dag-ucan": "^3.4.0",
"@ucanto/client": "^9.0.0",
"@ucanto/core": "^9.0.1",
"@ucanto/interface": "^9.0.0",
"@ucanto/transport": "^9.1.0",
"@ucanto/client": "^9.0.1",
"@ucanto/core": "^10.0.0",
"@ucanto/interface": "^10.0.0",
"@ucanto/transport": "^9.1.1",
"@web3-storage/capabilities": "workspace:^"
},
"devDependencies": {
"@ipld/car": "^5.1.1",
"@ipld/dag-json": "^10.1.4",
"@types/assert": "^1.5.6",
"@types/mocha": "^10.0.1",
"@ucanto/principal": "^9.0.0",
"@ucanto/server": "^9.0.1",
"@ucanto/principal": "^9.0.1",
"@ucanto/server": "^10.0.0",
"@web3-storage/data-segment": "^4.0.0",
"@web3-storage/eslint-config-w3up": "workspace:^",
"assert": "^2.0.0",
Expand Down
14 changes: 7 additions & 7 deletions packages/upload-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,12 @@
"test-watch": "pnpm build && mocha --bail --timeout 10s --watch --parallel -n no-warnings -n experimental-vm-modules -n experimental-fetch --watch-files src,test"
},
"dependencies": {
"@ucanto/client": "^9.0.0",
"@ucanto/interface": "^9.0.0",
"@ucanto/principal": "^9.0.0",
"@ucanto/server": "^9.0.1",
"@ucanto/transport": "^9.1.0",
"@ucanto/validator": "^9.0.1",
"@ucanto/client": "^9.0.1",
"@ucanto/interface": "^10.0.0",
"@ucanto/principal": "^9.0.1",
"@ucanto/server": "^10.0.0",
"@ucanto/transport": "^9.1.1",
"@ucanto/validator": "^9.0.2",
"@web3-storage/access": "workspace:^",
"@web3-storage/capabilities": "workspace:^",
"@web3-storage/did-mailto": "workspace:^",
Expand All @@ -189,7 +189,7 @@
"@ipld/car": "^5.1.1",
"@ipld/dag-ucan": "^3.4.0",
"@types/mocha": "^10.0.1",
"@ucanto/core": "^9.0.1",
"@ucanto/core": "^10.0.0",
"@types/sinon": "^17.0.3",
"@web-std/blob": "^3.0.5",
"@web3-storage/eslint-config-w3up": "workspace:^",
Expand Down
32 changes: 28 additions & 4 deletions packages/upload-api/src/blob/add.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import * as Server from '@ucanto/server'
import { Message } from '@ucanto/core'
import { ed25519 } from '@ucanto/principal'
import { CAR } from '@ucanto/transport'
import * as Blob from '@web3-storage/capabilities/blob'
import * as UCAN from '@web3-storage/capabilities/ucan'
import * as API from '../types.js'

import { BlobItemSizeExceeded } from './lib.js'
Expand Down Expand Up @@ -103,6 +106,7 @@ export function blobAddProvider(context) {
space,
blob.content
)
let allocateUcanConcludefx
if (!allocatedExistsRes.ok) {
// Execute allocate invocation
const allocateRes = await blobAllocate.execute(getServiceConnection())
Expand All @@ -111,6 +115,17 @@ export function blobAddProvider(context) {
error: allocateRes.out.error,
}
}
const message = await Message.build({ receipts: [allocateRes] })
const messageCar = await CAR.outbound.encode(message)
allocateUcanConcludefx = await UCAN.conclude.invoke({
issuer: id,
audience: id,
with: id.toDIDKey(),
nb: {
bytes: messageCar.body
},
expiration: Infinity,
}).delegate()
}

/** @type {API.OkBuilder<API.BlobAddSuccess, API.BlobAddFailure>} */
Expand All @@ -119,11 +134,20 @@ export function blobAddProvider(context) {
'await/ok': acceptfx.link(),
},
})
// TODO: not pass links, but delegation
// Add allocate receipt if allocate was executed
if (allocateUcanConcludefx) {
// TODO: perhaps if we allocated we need to get and write previous receipt?
return result
.fork(allocatefx)
.fork(allocateUcanConcludefx)
.fork(putfx)
.join(acceptfx)
}

return result
.fork(allocatefx.link())
.fork(putfx.link())
.join(acceptfx.link())
.fork(allocatefx)
.fork(putfx)
.join(acceptfx)
},
})
}
22 changes: 20 additions & 2 deletions packages/upload-api/src/ucan/conclude.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { provide } from '@ucanto/server'
import { Message } from '@ucanto/core'
import { CAR } from '@ucanto/transport'
import { conclude } from '@web3-storage/capabilities/ucan'
import * as API from '../types.js'

Expand All @@ -7,8 +9,24 @@ import * as API from '../types.js'
* @returns {API.ServiceMethod<API.UCANConclude, API.UCANConcludeSuccess, API.UCANConcludeFailure>}
*/
export const ucanConcludeProvider = ({ receiptsStorage }) =>
provide(conclude, async ({ capability, invocation }) => {
// TODO: Store receipt
provide(conclude, async ({ capability }) => {
const messageCar = CAR.codec.decode(capability.nb.bytes)
const message = Message.view({ root: messageCar.roots[0].cid, store: messageCar.blocks })

// TODO: check number of receipts
const receiptKey = Array.from(message.receipts.keys())[0]
const receipt = message.receipts.get(receiptKey)

if (!receipt) {
throw new Error('receipt should exist')
}

const receiptPutRes = await receiptsStorage.put(receipt)
if (receiptPutRes.error) {
return {
error: receiptPutRes.error
}
}

// TODO: Schedule accept (temporary simple hack)

Expand Down
49 changes: 37 additions & 12 deletions packages/upload-api/test/handlers/blob.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import * as API from '../../src/types.js'
import { Absentee } from '@ucanto/principal'
import { Message } from '@ucanto/core'
import { CAR } from '@ucanto/transport'
import { equals } from 'uint8arrays'
import { sha256 } from 'multiformats/hashes/sha2'
import * as BlobCapabilities from '@web3-storage/capabilities/blob'
import * as UCAN from '@web3-storage/capabilities/ucan'
import { base64pad } from 'multiformats/bases/base64'

import { provisionProvider } from '../helpers/utils.js'
Expand All @@ -14,7 +17,7 @@ import { BlobItemSizeExceededName } from '../../src/blob/lib.js'
* @type {API.Tests}
*/
export const test = {
'blob/add schedules allocation and returns effects for allocation and accept':
'blob/add executes allocation and returns effects for allocation, accept, put and allocate receipt':
async (assert, context) => {
const { proof, spaceDid } = await registerSpace(alice, context)

Expand Down Expand Up @@ -45,24 +48,46 @@ export const test = {
})
const blobAdd = await invocation.execute(connection)
if (!blobAdd.out.ok) {
console.log('out error')
throw new Error('invocation failed', { cause: blobAdd })
}

// Validate receipt
assert.ok(blobAdd.out.ok.claim)
assert.equal(blobAdd.fx.fork.length, 2)
assert.ok(blobAdd.out.ok.claim['await/ok'].equals(blobAdd.fx.join?.link()))
assert.ok(blobAdd.fx.join)
assert.ok(blobAdd.out.ok.claim['await/ok'].equals(blobAdd.fx.join))

/**
* @type {import('@ucanto/interface').Invocation[]}
**/
// @ts-expect-error read only effect
const forkInvocations = blobAdd.fx.fork
assert.equal(blobAdd.fx.fork.length, 3)
const allocatefx = forkInvocations.find(fork => fork.capabilities[0].can === BlobCapabilities.allocate.can)
const allocateUcanConcludefx = forkInvocations.find(fork => fork.capabilities[0].can === UCAN.conclude.can)
const putfx = forkInvocations.find(fork => fork.capabilities[0].can === BlobCapabilities.put.can)
if (!allocatefx || !allocateUcanConcludefx || !putfx) {
throw new Error('effects not provided')
}

// Validate `http/put` invocation stored
// TODO, needs receipt to include those bytes

// validate scheduled task ran and has receipt inlined
// const [blobAllocateInvocation] = scheduledTasks
// assert.equal(blobAllocateInvocation.can, BlobCapabilities.allocate.can)
// assert.equal(blobAllocateInvocation.nb.space, spaceDid)
// assert.equal(blobAllocateInvocation.nb.blob.size, size)
// assert.ok(equals(blobAllocateInvocation.nb.blob.content, content))
const httpPutGetTask = await context.tasksStorage.get(
putfx.cid
)
assert.ok(httpPutGetTask.ok)

// validate scheduled allocate task ran an its receipt content
// @ts-expect-error object of type unknown
const messageCar = CAR.codec.decode(allocateUcanConcludefx.capabilities[0].nb.bytes)
const message = Message.view({ root: messageCar.roots[0].cid, store: messageCar.blocks })

const receiptKey = Array.from(message.receipts.keys())[0]
const receipt = message.receipts.get(receiptKey)
assert.ok(receipt?.out)
assert.ok(receipt?.out.ok)
// @ts-expect-error receipt out is unknown
assert.equal(receipt?.out.ok?.size, size)
// @ts-expect-error receipt out is unknown
assert.ok(receipt?.out.ok?.address)
},
'blob/add fails when a blob with size bigger than maximum size is added':
async (assert, context) => {
Expand Down
Loading

0 comments on commit 5c6b000

Please sign in to comment.