Skip to content

Commit

Permalink
fix: address tests to return location commitment for blob/add
Browse files Browse the repository at this point in the history
  • Loading branch information
joaosa committed May 23, 2024
1 parent 35e0abe commit e7be8dd
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 43 deletions.
3 changes: 2 additions & 1 deletion packages/upload-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@
"@ipld/dag-cbor": "^9.0.6",
"@ipld/dag-ucan": "^3.4.0",
"@ipld/unixfs": "^2.1.1",
"@ucanto/core": "^10.0.1",
"@ucanto/client": "^9.0.1",
"@ucanto/core": "^10.0.1",
"@ucanto/interface": "^10.0.1",
"@ucanto/transport": "^9.1.1",
"@web3-storage/capabilities": "workspace:^",
Expand All @@ -89,6 +89,7 @@
"@types/varint": "^6.0.1",
"@ucanto/principal": "^9.0.1",
"@ucanto/server": "^10.0.0",
"@web3-storage/content-claims": "^4.0.4",
"@web3-storage/eslint-config-w3up": "workspace:^",
"assert": "^2.0.0",
"blockstore-core": "^3.0.0",
Expand Down
4 changes: 1 addition & 3 deletions packages/upload-client/src/blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,6 @@ export async function add(
block,
])
)

const site = Delegation.view(
{
root: /** @type {import('@ucanto/interface').UCANLink} */ (
Expand All @@ -374,14 +373,13 @@ export async function add(
},
null
)

if (!site) {
throw new Error(`failed ${BlobCapabilities.add.can} invocation`, {
cause: 'failed to get blob/accept receipt',
})
}

// @ts-ignore
// @ts-ignore Type
return site
}

Expand Down
1 change: 1 addition & 0 deletions packages/w3up-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
"@types/mocha": "^10.0.1",
"@types/node": "^20.8.4",
"@ucanto/server": "^10.0.0",
"@web3-storage/content-claims": "^4.0.4",
"@web3-storage/data-segment": "^5.0.0",
"@web3-storage/eslint-config-w3up": "workspace:^",
"@web3-storage/upload-api": "workspace:^",
Expand Down
44 changes: 34 additions & 10 deletions packages/w3up-client/test/capability/blob.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { create as createLink } from 'multiformats/link'
import { codec as car } from '@ucanto/transport/car'
import { sha256 } from 'multiformats/hashes/sha2'
import { AgentData } from '@web3-storage/access/agent'
import { randomBytes } from '../helpers/random.js'
Expand Down Expand Up @@ -33,19 +35,31 @@ export const BlobClient = Test.withContext({

const bytes = await randomBytes(128)
const bytesHash = await sha256.digest(bytes)
const multihash = await alice.capability.blob.add(new Blob([bytes]), {
fetch: setupGetReceipt,
const link = createLink(car.code, bytesHash)
const commitment = await alice.capability.blob.add(new Blob([bytes]), {
fetch: setupGetReceipt(link),
})

// @ts-ignore Element
console.log(commitment.capabilities[0].nb.content)
console.log(allocationsStorage)
// TODO we should check blobsStorage as well
assert.deepEqual(
await allocationsStorage.exists(space.did(), multihash.bytes),
await allocationsStorage.exists(
space.did(),
// @ts-ignore Element
commitment.capabilities[0].nb.content.bytes.slice(3)
),
{
ok: true,
}
)

assert.deepEqual(multihash, bytesHash)
assert.deepEqual(
// @ts-ignore Element
commitment.capabilities[0].nb.content.bytes.slice(3),
bytesHash.bytes
)
},
'should list stored blobs': async (
assert,
Expand Down Expand Up @@ -74,10 +88,15 @@ export const BlobClient = Test.withContext({

const bytes = await randomBytes(128)
const bytesHash = await sha256.digest(bytes)
const multihash = await alice.capability.blob.add(new Blob([bytes]), {
fetch: setupGetReceipt,
const link = createLink(car.code, bytesHash)
const commitment = await alice.capability.blob.add(new Blob([bytes]), {
fetch: setupGetReceipt(link),
})
assert.deepEqual(multihash, bytesHash)
assert.deepEqual(
// @ts-ignore Element
commitment.capabilities[0].nb.content.bytes.slice(3),
bytesHash.bytes
)

const {
results: [entry],
Expand Down Expand Up @@ -112,11 +131,16 @@ export const BlobClient = Test.withContext({
})

const bytes = await randomBytes(128)
const multihash = await alice.capability.blob.add(new Blob([bytes]), {
fetch: setupGetReceipt,
const bytesHash = await sha256.digest(bytes)
const link = createLink(car.code, bytesHash)
const commitment = await alice.capability.blob.add(new Blob([bytes]), {
fetch: setupGetReceipt(link),
})

const result = await alice.capability.blob.remove(multihash)
const result = await alice.capability.blob.remove(
// @ts-ignore Element
commitment.capabilities[0].nb.content
)
assert.ok(result.ok)
},
})
Expand Down
9 changes: 8 additions & 1 deletion packages/w3up-client/test/capability/usage.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { create as createLink } from 'multiformats/link'
import * as car from 'multiformats/codecs/raw'
import { sha256 } from 'multiformats/hashes/sha2'
import { AgentData } from '@web3-storage/access/agent'
import { Client } from '../../src/client.js'
import * as Test from '../test.js'
Expand Down Expand Up @@ -30,8 +33,12 @@ export const UsageClient = Test.withContext({
})

const content = new Blob(['hello world'])
const bytesHash = await sha256.digest(
new Uint8Array(await content.arrayBuffer())
)
const link = createLink(car.code, bytesHash)
await alice.uploadFile(content, {
fetch: setupGetReceipt,
fetch: setupGetReceipt(link),
})

const period = { from: new Date(0), to: new Date() }
Expand Down
31 changes: 25 additions & 6 deletions packages/w3up-client/test/client.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import assert from 'assert'
import { create as createLink } from 'multiformats/link'
import * as raw from 'multiformats/codecs/raw'
import { sha256 } from 'multiformats/hashes/sha2'
import { parseLink } from '@ucanto/server'
import { AgentData } from '@web3-storage/access/agent'
import { randomBytes, randomCAR } from './helpers/random.js'
Expand Down Expand Up @@ -46,7 +49,7 @@ export const testClient = {
onShardStored: (meta) => {
carCID = meta.cid
},
fetch: setupGetReceipt,
fetch: setupGetReceipt(expectedCar.cid),
})

assert.deepEqual(await uploadTable.exists(space.did(), dataCID), {
Expand Down Expand Up @@ -93,6 +96,12 @@ export const testClient = {
{ connection, provisionsStorage, uploadTable }
) => {
const bytesList = [await randomBytes(128), await randomBytes(32)]
const bytesHash = await Promise.all(
bytesList.map(async (bytes) => await sha256.digest(bytes))
)
const links = await Promise.all(
bytesHash.map((hash) => createLink(raw.code, hash))
)
const files = bytesList.map(
(bytes, index) => new File([bytes], `${index}.txt`)
)
Expand Down Expand Up @@ -126,7 +135,7 @@ export const testClient = {
onShardStored: (meta) => {
carCID = meta.cid
},
fetch: setupGetReceipt,
fetch: setupGetReceipt(links[0]),
})

assert.deepEqual(await uploadTable.exists(space.did(), dataCID), {
Expand Down Expand Up @@ -169,7 +178,7 @@ export const testClient = {
onShardStored: (meta) => {
carCID = meta.cid
},
fetch: setupGetReceipt,
fetch: setupGetReceipt(car.cid),
})

assert.deepEqual(await uploadTable.exists(space.did(), root), {
Expand Down Expand Up @@ -384,6 +393,8 @@ export const testClient = {
{ connection, provisionsStorage, uploadTable }
) => {
const bytes = await randomBytes(128)
const bytesHash = await sha256.digest(bytes)
const link = createLink(raw.code, bytesHash)

const alice = new Client(await AgentData.create(), {
// @ts-ignore
Expand All @@ -408,7 +419,7 @@ export const testClient = {
})

const root = await alice.uploadFile(new Blob([bytes]), {
fetch: setupGetReceipt,
fetch: setupGetReceipt(link),
})

assert.deepEqual(await uploadTable.exists(space.did(), root), {
Expand All @@ -433,6 +444,8 @@ export const testClient = {
'should remove an uploaded file from the service without its shards by default':
async (assert, { connection, provisionsStorage, uploadTable }) => {
const bytes = await randomBytes(128)
const bytesHash = await sha256.digest(bytes)
const link = createLink(raw.code, bytesHash)

const alice = new Client(await AgentData.create(), {
// @ts-ignore
Expand All @@ -457,7 +470,7 @@ export const testClient = {
})

const root = await alice.uploadFile(new Blob([bytes]), {
fetch: setupGetReceipt,
fetch: setupGetReceipt(link),
})

assert.deepEqual(await uploadTable.exists(space.did(), root), {
Expand Down Expand Up @@ -509,6 +522,12 @@ export const testClient = {
{ connection, provisionsStorage, uploadTable }
) => {
const bytesArray = [await randomBytes(128), await randomBytes(128)]
const bytesHash = await Promise.all(
bytesArray.map(async (bytes) => await sha256.digest(bytes))
)
const links = await Promise.all(
bytesHash.map(async (hash) => createLink(raw.code, hash))
)

const alice = new Client(await AgentData.create(), {
// @ts-ignore
Expand All @@ -533,7 +552,7 @@ export const testClient = {
})

const root = await alice.uploadFile(new Blob(bytesArray), {
fetch: setupGetReceipt,
fetch: setupGetReceipt(links[0]),
})

const upload = await uploadTable.get(space.did(), root)
Expand Down
67 changes: 45 additions & 22 deletions packages/w3up-client/test/helpers/utils.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,58 @@
import { base32 } from 'multiformats/bases/base32'
import { CID } from 'multiformats'
import { parseLink } from '@ucanto/server'
import { Receipt, Message } from '@ucanto/core'
import * as CAR from '@ucanto/transport/car'
import * as Server from '@ucanto/server'
import { UCAN } from '@web3-storage/capabilities'
import * as Types from '../../src/types.js'
import * as Signer from '@ucanto/principal/ed25519'
import { Assert } from '@web3-storage/content-claims/capability'

export const validateAuthorization = () => ({ ok: {} })

// @ts-ignore Parameter
export const setupGetReceipt = async (url) => {
// need to handle using regular fetch when not actually getting a receipt
if (!url.pathname) {
return await fetch(url)
}
/**
* @param {import('multiformats').Link} content
*/
export const setupGetReceipt = (content) => {
// @ts-ignore Parameter
return async (url) => {
// need to handle using regular fetch when not actually getting a receipt
if (!url.pathname) {
return await fetch(url)
}

const cid = url.pathname.replace('/receipt/', '')
const receipt = await Receipt.issue({
issuer: await Signer.generate(),
// @ts-ignore Type
ran: CID.parse(cid, base32.base32),
result: { ok: {} },
})
const message = await Message.build({
// @ts-ignore
invocations: [],
receipts: [receipt],
})
const request = CAR.request.encode(message)
return new Response(request.body.buffer)
const taskID = url.pathname.replace('/receipt/', '')
const issuer = await Signer.generate()

const locationClaim = await Assert.location.delegate({
issuer,
audience: issuer,
with: issuer.toDIDKey(),
nb: {
content,
location: ['http://localhost'],
},
expiration: Infinity,
})

const receipt = await Receipt.issue({
issuer,
fx: {
fork: [locationClaim],
},
ran: parseLink(taskID),
result: {
ok: {
site: locationClaim.link(),
},
},
})

const message = await Message.build({
receipts: [receipt],
})
const request = CAR.request.encode(message)
return new Response(request.body.buffer)
}
}

/**
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e7be8dd

Please sign in to comment.