Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: filecoin test use blob #1422

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading