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: upgrade data segment #850

Merged
merged 3 commits into from
Aug 10, 2023
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/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": "^3.0.1"
},
"devDependencies": {
"@types/assert": "^1.5.6",
Expand Down
66 changes: 25 additions & 41 deletions packages/capabilities/src/filecoin.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@ import { capability, Schema, ok } from '@ucanto/validator'
import { equal, equalWith, checkLink, and } from './utils.js'

/**
* @see https://github.com/multiformats/go-multihash/blob/dc3bd6897fcd17f6acd8d4d6ffd2cea3d4d3ebeb/multihash.go#L73
* @see https://github.com/filecoin-project/FIPs/pull/758/files
*/
const SHA2_256_TRUNC254_PADDED = 0x1012
const FR32_SHA2_256_TRUNC254_PADDED_BINARY_TREE = 0x1011
/**
* @see https://github.com/ipfs/go-cid/blob/829c826f6be23320846f4b7318aee4d17bf8e094/cid.go#L104
* @see https://github.com/filecoin-project/FIPs/pull/758/files
*/
const FilCommitmentUnsealed = 0xf101
const RAW_CODE = 0x55

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

/**
* `filecoin/add` capability allows agent to add a filecoin piece to be aggregated
Expand All @@ -38,16 +46,10 @@ 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_LINK),
}),
derives: (claim, from) => {
return (
Expand All @@ -72,16 +74,10 @@ 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_LINK),
/**
* Storefront requestin piece to be aggregated
*/
Expand Down Expand Up @@ -120,17 +116,11 @@ 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').PieceLinkSchema} */ (PIECE_LINK),
/**
* Storefront requesting deal
*/
Expand Down Expand Up @@ -164,16 +154,10 @@ 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_LINK),
}),
derives: (claim, from) => {
return (
Expand Down
3 changes: 3 additions & 0 deletions packages/capabilities/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export interface UnknownProvider extends Ucanto.Failure {
did: DID
}

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

// Access
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": "^3.0.1"
},
"devDependencies": {
"@ipld/car": "^5.1.1",
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
4 changes: 2 additions & 2 deletions packages/filecoin-api/test/services/dealer.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const test = {
throw new Error('invocation failed', { cause: response.out.error })
}
assert.ok(response.out.ok)
assert.deepEqual(response.out.ok.aggregate, aggregate.link)
assert.ok(response.out.ok.aggregate?.equals(aggregate.link))

// Validate effect in receipt
const fx = await Filecoin.dealAdd
Expand Down Expand Up @@ -111,7 +111,7 @@ export const test = {
throw new Error('invocation failed', { cause: response.out.error })
}
assert.ok(response.out.ok)
assert.deepEqual(response.out.ok.aggregate, aggregate.link)
assert.ok(response.out.ok.aggregate?.equals(aggregate.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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why capture root here as opposed to CID of the piece ?

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,
}
}
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": "^3.0.1",
"assert": "^2.0.0",
"c8": "^7.13.0",
"hd-scripts": "^4.0.0",
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
36 changes: 18 additions & 18 deletions packages/filecoin-client/test/dealer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import { dealAdd } from '../src/dealer.js'
import { randomAggregate } from './helpers/random.js'
import { mockService } from './helpers/mocks.js'
import { OperationFailed, OperationErrorName } from './helpers/errors.js'
import { serviceProvider as brokerService } from './fixtures.js'
import { serviceProvider as dealerService } from './fixtures.js'

describe('aggregate.add', () => {
it('aggregator adds an aggregate piece to the broker, getting the piece queued', async () => {
describe('dealer.add', () => {
it('aggregator adds an aggregate piece to the dealer, getting the piece queued', async () => {
const { aggregator, storefront: storefrontSigner } = await getContext()

// generate aggregate to add
Expand Down Expand Up @@ -71,7 +71,7 @@ describe('aggregate.add', () => {
{
issuer: aggregator,
with: aggregator.did(),
audience: brokerService,
audience: dealerService,
},
aggregate.link.link(),
offer,
Expand All @@ -81,12 +81,12 @@ describe('aggregate.add', () => {
)

assert.ok(res.out.ok)
assert.deepEqual(res.out.ok, dealAddResponse)
assert.ok(res.out.ok.aggregate?.equals(dealAddResponse.aggregate))
// includes effect fx in receipt
assert.ok(res.fx.join)
})

it('broker self invokes add an aggregate piece to accept the piece queued', async () => {
it('dealer self invokes add an aggregate piece to accept the piece queued', async () => {
const { storefront: storefrontSigner } = await getContext()

// generate aggregate to add
Expand All @@ -107,7 +107,7 @@ describe('aggregate.add', () => {
add: Server.provideAdvanced({
capability: FilecoinCapabilities.dealAdd,
handler: async ({ invocation }) => {
assert.strictEqual(invocation.issuer.did(), brokerService.did())
assert.strictEqual(invocation.issuer.did(), dealerService.did())
assert.strictEqual(invocation.capabilities.length, 1)
const invCap = invocation.capabilities[0]
assert.strictEqual(invCap.can, FilecoinCapabilities.dealAdd.can)
Expand All @@ -132,9 +132,9 @@ describe('aggregate.add', () => {
// invoke piece add from storefront
const res = await dealAdd(
{
issuer: brokerService,
with: brokerService.did(),
audience: brokerService,
issuer: dealerService,
with: dealerService.did(),
audience: dealerService,
},
aggregate.link.link(),
offer,
Expand All @@ -144,12 +144,12 @@ describe('aggregate.add', () => {
)

assert.ok(res.out.ok)
assert.deepEqual(res.out.ok, dealAddResponse)
assert.ok(res.out.ok.aggregate?.equals(dealAddResponse.aggregate))
// does not include effect fx in receipt
assert.ok(!res.fx.join)
})

it('broker self invokes add an aggregate piece to reject the piece queued', async () => {
it('dealer self invokes add an aggregate piece to reject the piece queued', async () => {
const { storefront: storefrontSigner } = await getContext()

// generate aggregate to add
Expand All @@ -171,7 +171,7 @@ describe('aggregate.add', () => {
add: Server.provideAdvanced({
capability: FilecoinCapabilities.dealAdd,
handler: async ({ invocation, context }) => {
assert.strictEqual(invocation.issuer.did(), brokerService.did())
assert.strictEqual(invocation.issuer.did(), dealerService.did())
assert.strictEqual(invocation.capabilities.length, 1)
const invCap = invocation.capabilities[0]
assert.strictEqual(invCap.can, FilecoinCapabilities.dealAdd.can)
Expand All @@ -198,9 +198,9 @@ describe('aggregate.add', () => {
// invoke piece add from storefront
const res = await dealAdd(
{
issuer: brokerService,
with: brokerService.did(),
audience: brokerService,
issuer: dealerService,
with: dealerService.did(),
audience: dealerService,
},
aggregate.link.link(),
offer,
Expand Down Expand Up @@ -228,12 +228,12 @@ async function getContext() {
*/
function getConnection(service) {
const server = Server.create({
id: brokerService,
id: dealerService,
service,
codec: CAR.inbound,
})
const connection = Client.connect({
id: brokerService,
id: dealerService,
codec: CAR.outbound,
channel: server,
})
Expand Down
Loading
Loading