Skip to content

Commit

Permalink
fix: upgrade data segment
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Aug 9, 2023
1 parent d868265 commit dd40d99
Show file tree
Hide file tree
Showing 18 changed files with 102 additions and 110 deletions.
2 changes: 1 addition & 1 deletion packages/capabilities/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"@ucanto/principal": "^8.0.0",
"@ucanto/transport": "^8.0.0",
"@ucanto/validator": "^8.0.0",
"@web3-storage/data-segment": "^2.2.0"
"@web3-storage/data-segment": "file://../../../data-segment"
},
"devDependencies": {
"@types/assert": "^1.5.6",
Expand Down
71 changes: 34 additions & 37 deletions packages/capabilities/src/filecoin.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@
import { capability, Schema, ok } from '@ucanto/validator'
import { equal, equalWith, checkLink, and } from './utils.js'

/**
* @see https://github.com/filecoin-project/FIPs/pull/758/files
*/
const FR32_SHA2_256_TRUNC254_PADDED_BINARY_TREE = 0x1011
/**
* @see https://github.com/filecoin-project/FIPs/pull/758/files
*/
const RAW_CODE = 0x55

/**
* @see https://github.com/multiformats/go-multihash/blob/dc3bd6897fcd17f6acd8d4d6ffd2cea3d4d3ebeb/multihash.go#L73
*/
Expand All @@ -21,6 +30,22 @@ const SHA2_256_TRUNC254_PADDED = 0x1012
*/
const FilCommitmentUnsealed = 0xf101

const PIECE_CID_V2 = Schema.link({
code: RAW_CODE,
version: 1,
multihash: {
code: FR32_SHA2_256_TRUNC254_PADDED_BINARY_TREE,
},
})

const PIECE_CID_V1 = Schema.link({
code: FilCommitmentUnsealed,
version: 1,
multihash: {
code: SHA2_256_TRUNC254_PADDED,
},
})

/**
* `filecoin/add` capability allows agent to add a filecoin piece to be aggregated
* so that it can be stored by a Storage provider on a future time.
Expand All @@ -38,16 +63,9 @@ export const filecoinAdd = capability({
content: Schema.link(),
/**
* CID of the piece.
* @see https://github.com/filecoin-project/FIPs/pull/758/files
*/
piece: /** @type {import('./types').PieceLinkSchema} */ (
Schema.link({
code: FilCommitmentUnsealed,
version: 1,
multihash: {
code: SHA2_256_TRUNC254_PADDED,
},
})
),
piece: /** @type {import('./types').PieceLinkSchema} */ (PIECE_CID_V2),
}),
derives: (claim, from) => {
return (
Expand All @@ -72,16 +90,9 @@ export const aggregateAdd = capability({
nb: Schema.struct({
/**
* CID of the piece.
* @see https://github.com/filecoin-project/FIPs/pull/758/files
*/
piece: /** @type {import('./types').PieceLinkSchema} */ (
Schema.link({
code: FilCommitmentUnsealed,
version: 1,
multihash: {
code: SHA2_256_TRUNC254_PADDED,
},
})
),
piece: /** @type {import('./types').PieceLinkSchema} */ (PIECE_CID_V2),
/**
* Storefront requestin piece to be aggregated
*/
Expand Down Expand Up @@ -120,17 +131,10 @@ export const dealAdd = capability({
pieces: Schema.link(),
/**
* Commitment proof for the aggregate being offered.
* https://github.com/filecoin-project/go-state-types/blob/1e6cf0d47cdda75383ef036fc2725d1cf51dbde8/abi/piece.go#L47-L50
* @see https://github.com/filecoin-project/go-state-types/blob/1e6cf0d47cdda75383ef036fc2725d1cf51dbde8/abi/piece.go#L47-L50
* @see https://github.com/filecoin-project/FIPs/pull/758/files
*/
aggregate: /** @type {import('./types').PieceLinkSchema} */ (
Schema.link({
code: FilCommitmentUnsealed,
version: 1,
multihash: {
code: SHA2_256_TRUNC254_PADDED,
},
})
),
aggregate: /** @type {import('./types').LegacyPieceLinkSchema} */ (PIECE_CID_V1),
/**
* Storefront requesting deal
*/
Expand Down Expand Up @@ -164,16 +168,9 @@ export const chainTrackerInfo = capability({
nb: Schema.struct({
/**
* CID of the piece.
* @see https://github.com/filecoin-project/FIPs/pull/758/files
*/
piece: /** @type {import('./types').PieceLinkSchema} */ (
Schema.link({
code: FilCommitmentUnsealed,
version: 1,
multihash: {
code: SHA2_256_TRUNC254_PADDED,
},
})
),
piece: /** @type {import('./types').PieceLinkSchema} */ (PIECE_CID_V2),
}),
derives: (claim, from) => {
return (
Expand Down
11 changes: 8 additions & 3 deletions packages/capabilities/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { TupleToUnion } from 'type-fest'
import * as Ucanto from '@ucanto/interface'
import type { Schema } from '@ucanto/core'
import { InferInvokedCapability, Unit, DID } from '@ucanto/interface'
import type { PieceLink } from '@web3-storage/data-segment'
import type { PieceLink, LegacyPieceLink } from '@web3-storage/data-segment'
import { space, info, recover, recoverValidation } from './space.js'
import * as provider from './provider.js'
import { top } from './top.js'
Expand All @@ -21,8 +21,13 @@ export interface InsufficientStorage {
message: string
}

/**
* @see https://github.com/filecoin-project/FIPs/pull/758/files
*/
export type PieceLinkSchema = Schema.Schema<PieceLink>

export type LegacyPieceLinkSchema = Schema.Schema<LegacyPieceLink>

// Access
export type Access = InferInvokedCapability<typeof AccessCaps.access>
export type AccessAuthorize = InferInvokedCapability<
Expand Down Expand Up @@ -86,14 +91,14 @@ export interface FilecoinAddFailure extends Ucanto.Failure {

export interface AggregateAddSuccess {
piece: PieceLink
aggregate?: PieceLink
aggregate?: LegacyPieceLink
}
export interface AggregateAddFailure extends Ucanto.Failure {
name: string
}

export interface DealAddSuccess {
aggregate?: PieceLink
aggregate?: LegacyPieceLink
}

export type DealAddFailure = DealAddParseFailure | DealAddFailureWithBadPiece
Expand Down
2 changes: 1 addition & 1 deletion packages/filecoin-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"@ucanto/server": "^8.0.0",
"@ucanto/transport": "^8.0.0",
"@web3-storage/capabilities": "workspace:^",
"@web3-storage/data-segment": "^2.2.0"
"@web3-storage/data-segment": "file://../../../data-segment"
},
"devDependencies": {
"@ipld/car": "^5.1.1",
Expand Down
4 changes: 2 additions & 2 deletions packages/filecoin-api/src/dealer.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const add = async ({ capability, invocation }, context) => {
}

/**
* @param {import('@web3-storage/data-segment').PieceLink} aggregate
* @param {import('@web3-storage/data-segment').LegacyPieceLink} aggregate
* @param {Server.API.Link<unknown, number, number, 0 | 1>} offerCid
* @param {string} storefront
* @param {string | undefined} label
Expand Down Expand Up @@ -85,7 +85,7 @@ async function enqueue(
}

/**
* @param {import('@web3-storage/data-segment').PieceLink} aggregate
* @param {import('@web3-storage/data-segment').LegacyPieceLink} aggregate
* @param {import('@web3-storage/data-segment').PieceLink[]} pieces
* @param {string} storefront
* @param {string | undefined} label
Expand Down
4 changes: 2 additions & 2 deletions packages/filecoin-api/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
Match,
} from '@ucanto/interface'
import type { ProviderInput } from '@ucanto/server'
import { PieceLink } from '@web3-storage/data-segment'
import { PieceLink, LegacyPieceLink } from '@web3-storage/data-segment'
import { UnknownLink } from '@ucanto/interface'

export * as UcantoInterface from '@ucanto/interface'
Expand Down Expand Up @@ -70,7 +70,7 @@ export interface AggregatorRecord {
}

export interface DealerRecord {
aggregate: PieceLink
aggregate: LegacyPieceLink
pieces: PieceLink[]
storefront: string
label?: string
Expand Down
6 changes: 3 additions & 3 deletions packages/filecoin-api/test/services/aggregator.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const test = {
throw new Error('invocation failed', { cause: response.out.error })
}
assert.ok(response.out.ok)
assert.deepEqual(response.out.ok.piece, cargo.link.link())
assert.ok(response.out.ok.piece.equals(cargo.link.link()))

// Validate effect in receipt
const fx = await Filecoin.aggregateAdd
Expand Down Expand Up @@ -98,7 +98,7 @@ export const test = {
throw new Error('invocation failed', { cause: response.out.error })
}
assert.ok(response.out.ok)
assert.deepEqual(response.out.ok.piece, cargo.link.link())
assert.ok(response.out.ok.piece.equals(cargo.link.link()))

// Validate queue and store
await pWaitFor(() => context.queuedMessages.length === 0)
Expand Down Expand Up @@ -141,7 +141,7 @@ export const test = {
throw new Error('invocation failed', { cause: response.out.error })
}
assert.ok(response.out.ok)
assert.deepEqual(response.out.ok.piece, cargo.link.link())
assert.ok(response.out.ok.piece.equals(cargo.link.link()))

// Validate queue and store
await pWaitFor(() => context.queuedMessages.length === 0)
Expand Down
6 changes: 3 additions & 3 deletions packages/filecoin-api/test/services/storefront.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const test = {
throw new Error('invocation failed', { cause: response.out.error })
}
assert.ok(response.out.ok)
assert.deepEqual(response.out.ok.piece, cargo.link.link())
assert.ok(response.out.ok.piece.equals(cargo.link.link()))

// Validate effect in receipt
const fx = await Filecoin.filecoinAdd
Expand Down Expand Up @@ -92,7 +92,7 @@ export const test = {
throw new Error('invocation failed', { cause: response.out.error })
}
assert.ok(response.out.ok)
assert.deepEqual(response.out.ok.piece, cargo.link.link())
assert.ok(response.out.ok.piece.equals(cargo.link.link()))

// Validate queue and store
await pWaitFor(() => context.queuedMessages.length === 0)
Expand Down Expand Up @@ -128,7 +128,7 @@ export const test = {
throw new Error('invocation failed', { cause: response.out.error })
}
assert.ok(response.out.ok)
assert.deepEqual(response.out.ok.piece, cargo.link.link())
assert.ok(response.out.ok.piece.equals(cargo.link.link()))

// Validate queue and store
await pWaitFor(() => context.queuedMessages.length === 0)
Expand Down
17 changes: 5 additions & 12 deletions packages/filecoin-api/test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ export async function randomCargo(length, size) {
)

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

return {
link: piece.link,
content: car.cid,
height: piece.height,
size: piece.size,
root: piece.root,
content: car.cid,
}
})
}
Expand All @@ -80,19 +80,12 @@ export async function randomCargo(length, size) {
*/
export async function randomAggregate(length, size) {
const pieces = await randomCargo(length, size)

const aggregateBuild = Aggregate.build({
pieces,
})

return {
pieces: pieces.map((p) => ({
link: p.link,
height: p.height,
})),
aggregate: {
link: aggregateBuild.link,
height: aggregateBuild.height,
},
pieces,
aggregate: aggregateBuild.toInfo(),
}
}
2 changes: 1 addition & 1 deletion packages/filecoin-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"@types/mocha": "^10.0.1",
"@ucanto/principal": "^8.0.0",
"@ucanto/server": "^8.0.1",
"@web3-storage/data-segment": "^2.2.0",
"@web3-storage/data-segment": "file://../../../data-segment",
"assert": "^2.0.0",
"c8": "^7.13.0",
"hd-scripts": "^4.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/filecoin-client/src/dealer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const connection = connect({
* Add a piece (aggregate) to the dealer system of the filecoin pipeline to offer to SPs.
*
* @param {import('./types.js').InvocationConfig} conf - Configuration
* @param {import('@web3-storage/data-segment').PieceLink} aggregate
* @param {import('@web3-storage/data-segment').LegacyPieceLink} aggregate
* @param {import('@web3-storage/data-segment').PieceLink[]} pieces
* @param {string} storefront
* @param {string} label
Expand Down
4 changes: 2 additions & 2 deletions packages/filecoin-client/test/aggregator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe('aggregate/add', () => {
)

assert.ok(res.out.ok)
assert.deepEqual(res.out.ok, pieceAddResponse)
assert.ok(res.out.ok.piece.equals(pieceAddResponse.piece))
// includes effect fx in receipt
assert.ok(res.fx.join)
})
Expand Down Expand Up @@ -132,7 +132,7 @@ describe('aggregate/add', () => {
)

assert.ok(res.out.ok)
assert.deepEqual(res.out.ok, pieceAddResponse)
assert.ok(res.out.ok.piece.equals(pieceAddResponse.piece))
// does not include effect fx in receipt
assert.ok(!res.fx.join)
})
Expand Down
3 changes: 2 additions & 1 deletion packages/filecoin-client/test/chain-tracker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ describe('chain.info', () => {
)

assert.ok(res.out.ok)
assert.deepEqual(res.out.ok, chainInfoResponse)
// @ts-expect-error todo check error
assert.ok(res.out.ok.piece.equals(chainInfoResponse.piece))
})
})

Expand Down
Loading

0 comments on commit dd40d99

Please sign in to comment.