Skip to content

Commit

Permalink
fix: address index/add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joaosa committed May 23, 2024
1 parent cbf1bf0 commit 6a49452
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 43 deletions.
86 changes: 47 additions & 39 deletions packages/upload-client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Storefront } from '@web3-storage/filecoin-client'
import { ShardedDAGIndex } from '@web3-storage/blob-index'
import * as Link from 'multiformats/link'
import * as raw from 'multiformats/codecs/raw'
import { create as createLink } from 'multiformats/link'
import * as Store from './store.js'
import * as Blob from './blob.js'
import * as Index from './dag-index.js'
Expand Down Expand Up @@ -139,45 +138,50 @@ async function uploadBlockStream(
await blocks
.pipeThrough(new ShardingStream(options))
.pipeThrough(
new TransformStream({
async transform(car, controller) {
const bytes = new Uint8Array(await car.arrayBuffer())
// Invoke blob/add and write bytes to write target
const commitment = await Blob.add(conf, bytes, options)
// @ts-ignore Element
const { multihash } = commitment.capabilities[0].nb.content
// Should this be raw instead?
const cid = createLink(carCodec.code, multihash)
let piece
if (pieceHasher) {
const multihashDigest = await pieceHasher.digest(bytes)
/** @type {import('@web3-storage/capabilities/types').PieceLink} */
piece = createLink(raw.code, multihashDigest)
// Invoke filecoin/offer for data
const result = await Storefront.filecoinOffer(
{
issuer: conf.issuer,
audience: conf.audience,
// Resource of invocation is the issuer did for being self issued
with: conf.issuer.did(),
proofs: conf.proofs,
},
cid,
piece,
options
)
/** @type {TransformStream<import('./types.js').IndexedCARFile, import('./types.js').CARMetadata>} */
(
new TransformStream({
async transform(car, controller) {
const bytes = new Uint8Array(await car.arrayBuffer())
// Invoke blob/add and write bytes to write target
const commitment = await Blob.add(conf, bytes, options)
// @ts-ignore Element
const { multihash } = commitment.capabilities[0].nb.content
// Should this be raw instead?
const cid = Link.create(carCodec.code, multihash)
let piece
if (pieceHasher) {
const multihashDigest = await pieceHasher.digest(bytes)
/** @type {import('@web3-storage/capabilities/types').PieceLink} */
piece = Link.create(raw.code, multihashDigest)
const content = Link.create(raw.code, multihash)

if (result.out.error) {
throw new Error(
'failed to offer piece for aggregation into filecoin deal',
{ cause: result.out.error }
// Invoke filecoin/offer for data
const result = await Storefront.filecoinOffer(
{
issuer: conf.issuer,
audience: conf.audience,
// Resource of invocation is the issuer did for being self issued
with: conf.issuer.did(),
proofs: conf.proofs,
},
content,
piece,
options
)

if (result.out.error) {
throw new Error(
'failed to offer piece for aggregation into filecoin deal',
{ cause: result.out.error }
)
}
}
}
const { version, roots, size, slices } = car
controller.enqueue({ version, roots, size, cid, piece, slices })
},
})
const { version, roots, size, slices } = car
controller.enqueue({ version, roots, size, cid, piece, slices })
},
})
)
)
.pipeTo(
new WritableStream({
Expand Down Expand Up @@ -209,8 +213,12 @@ async function uploadBlockStream(
}

// Store the index in the space
const indexDigest = await Blob.add(conf, indexBytes.ok, options)
const indexLink = Link.create(carCodec.code, indexDigest)
const commitment = await Blob.add(conf, indexBytes.ok, options)
const indexLink = Link.create(
carCodec.code,
// @ts-ignore Element
commitment.capabilities[0].nb.content
)

// Register the index with the service
await Index.add(conf, indexLink, options)
Expand Down
9 changes: 5 additions & 4 deletions packages/w3up-client/test/capability/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ export const IndexClient = Test.withContext({
const index = ShardedDAGIndex.create(car.cid)
const indexBytes = Result.unwrap(await index.archive())

const indexDigest = await alice.capability.blob.add(
new Blob([indexBytes])
)
const commitment = await alice.capability.blob.add(new Blob([indexBytes]))
assert.ok(
await alice.capability.index.add(Link.create(0x0202, indexDigest))
await alice.capability.index.add(
// @ts-ignore Element
Link.create(0x0202, commitment.capabilities[0].nb.content)
)
)
},
},
Expand Down

0 comments on commit 6a49452

Please sign in to comment.