Skip to content

Commit

Permalink
fix: filecoin test use blob (#1422)
Browse files Browse the repository at this point in the history
Filecoin api exported tests still using CARs instead of Blobs, which
means storacha/w3infra#360 need to have
TestContentStore to use CARs to make these tests work
https://github.com/w3s-project/w3infra/pull/360/files#diff-cd843762832d0767495ff56f3f8692a15262dce245db89690794a1316868e25aR99

But, we want to use Blobs already, so let's make the tests also want
Blobs across Filecoin pipeline :)

note that in practise both Blobs and CARs work, but for testing we are
only writing one of them and is a CAR today
https://github.com/w3s-project/w3up/blob/main/packages/filecoin-api/test/events/storefront.js#L42
  • Loading branch information
vasco-santos authored Apr 30, 2024
1 parent 38e7041 commit 359c0b7
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 87 deletions.
2 changes: 1 addition & 1 deletion packages/filecoin-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
"p-map": "^6.0.0"
},
"devDependencies": {
"@ipld/car": "^5.1.1",
"@types/assert": "^1.5.10",
"@types/mocha": "^10.0.1",
"@ucanto/client": "^9.0.1",
"@ucanto/principal": "^9.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/filecoin-api/test/context/receipts.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import * as API from '../../src/types.js'
* @param {API.PieceLink} context.aggregate
* @param {string} context.group
* @param {API.PieceLink} context.piece
* @param {API.CARLink} context.content
* @param {import('@ucanto/interface').UnknownLink} context.content
* @param {import('@ucanto/interface').Block} context.piecesBlock
* @param {API.InclusionProof} context.inclusionProof
* @param {API.AggregateAcceptSuccess} context.aggregateAcceptStatus
Expand Down
46 changes: 11 additions & 35 deletions packages/filecoin-api/test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import { Aggregate, Piece } from '@web3-storage/data-segment'
import { CID } from 'multiformats'
import { webcrypto } from 'one-webcrypto'
import { sha256 } from 'multiformats/hashes/sha2'
import * as CAR from '@ucanto/transport/car'
import * as raw from 'multiformats/codecs/raw'
import { CarWriter } from '@ipld/car'
import { Blob } from '@web-std/blob'

/** @param {number} size */
export async function randomBytes(size) {
Expand All @@ -21,57 +18,36 @@ export async function randomBytes(size) {
}

/** @param {number} size */
export async function randomCAR(size) {
export async function randomBlob(size) {
const bytes = await randomBytes(size)
const hash = await sha256.digest(bytes)
const root = CID.create(1, raw.code, hash)
const cid = CID.create(1, raw.code, hash)

const { writer, out } = CarWriter.create(root)
writer.put({ cid: root, bytes })
writer.close()

const chunks = []
for await (const chunk of out) {
chunks.push(chunk)
return {
cid,
bytes,
}
const blob = new Blob(chunks)
const cid = await CAR.codec.link(new Uint8Array(await blob.arrayBuffer()))

return Object.assign(blob, { cid, roots: [root], bytes })
}

/**
* @param {number} length
* @param {number} size
*/
export async function randomCARs(length, size) {
return (
await Promise.all(Array.from({ length }).map(() => randomCAR(size)))
).map((car) => ({
link: car.cid,
size: car.size,
}))
}

/**
* @param {number} length
* @param {number} size
*/
export async function randomCargo(length, size) {
const cars = await Promise.all(
Array.from({ length }).map(() => randomCAR(size))
const blobs = await Promise.all(
Array.from({ length }).map(() => randomBlob(size))
)

return cars.map((car) => {
const piece = Piece.fromPayload(car.bytes)
return blobs.map((blob) => {
const piece = Piece.fromPayload(blob.bytes)

return {
link: piece.link,
height: piece.height,
root: piece.root,
content: car.cid,
content: blob.cid,
padding: piece.padding,
bytes: car.bytes,
bytes: blob.bytes,
}
})
}
Expand Down
1 change: 0 additions & 1 deletion packages/filecoin-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
"@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",
Expand Down
22 changes: 0 additions & 22 deletions packages/filecoin-client/test/helpers/car.js

This file was deleted.

37 changes: 16 additions & 21 deletions packages/filecoin-client/test/helpers/random.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Aggregate, Piece } from '@web3-storage/data-segment'

import { toCAR } from './car.js'
import { sha256 } from 'multiformats/hashes/sha2'
import * as raw from 'multiformats/codecs/raw'
import { CID } from 'multiformats'

/** @param {number} size */
export async function randomBytes(size) {
Expand All @@ -9,6 +10,7 @@ export async function randomBytes(size) {
const chunk = new Uint8Array(Math.min(size, 65_536))
if (!globalThis.crypto) {
try {
// @ts-expect-error no types for webcrypto
const { webcrypto } = await import('node:crypto')
webcrypto.getRandomValues(chunk)
} catch (error) {
Expand All @@ -27,42 +29,35 @@ export async function randomBytes(size) {
}

/** @param {number} size */
export async function randomCAR(size) {
export async function randomBlob(size) {
const bytes = await randomBytes(size)
return toCAR(bytes)
}
const hash = await sha256.digest(bytes)
const cid = CID.create(1, raw.code, hash)

/**
* @param {number} length
* @param {number} size
*/
export async function randomCARs(length, size) {
return (
await Promise.all(Array.from({ length }).map(() => randomCAR(size)))
).map((car) => ({
link: car.cid,
size: car.size,
}))
return {
cid,
bytes,
}
}

/**
* @param {number} length
* @param {number} size
*/
export async function randomCargo(length, size) {
const cars = await Promise.all(
Array.from({ length }).map(() => randomCAR(size))
const blobs = await Promise.all(
Array.from({ length }).map(() => randomBlob(size))
)

return cars.map((car) => {
const piece = Piece.fromPayload(car.bytes)
return blobs.map((blob) => {
const piece = Piece.fromPayload(blob.bytes)

return {
link: piece.link,
height: piece.height,
root: piece.root,
padding: piece.padding,
content: car.cid,
content: blob.cid,
}
})
}
Expand Down
13 changes: 7 additions & 6 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 359c0b7

Please sign in to comment.