Skip to content

Commit

Permalink
feat: filecoin info
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Nov 8, 2023
1 parent cb46ee9 commit 115237a
Show file tree
Hide file tree
Showing 51 changed files with 1,289 additions and 179 deletions.
1 change: 1 addition & 0 deletions packages/capabilities/src/filecoin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ export {
filecoinOffer as offer,
filecoinSubmit as submit,
filecoinAccept as accept,
filecoinInfo as info,
} from './storefront.js'
6 changes: 4 additions & 2 deletions packages/capabilities/src/filecoin/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { Schema } from '@ucanto/validator'
/**
* @see https://github.com/filecoin-project/FIPs/pull/758/files
*/
const FR32_SHA2_256_TRUNC254_PADDED_BINARY_TREE = /** @type {const} */ (0x1011)
export const FR32_SHA2_256_TRUNC254_PADDED_BINARY_TREE = /** @type {const} */ (
0x1011
)
/**
* @see https://github.com/filecoin-project/FIPs/pull/758/files
*/
const RAW_CODE = /** @type {const} */ (0x55)
export const RAW_CODE = /** @type {const} */ (0x55)

export const PieceLink = /** @type {import('../types.js').PieceLinkSchema} */ (
Schema.link({
Expand Down
29 changes: 29 additions & 0 deletions packages/capabilities/src/filecoin/storefront.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,32 @@ export const filecoinAccept = capability({
)
},
})

/**
* Capability allowing an agent to _request_ info about a content piece in
* Filecoin deals.
*/
export const filecoinInfo = capability({
can: 'filecoin/info',
/**
* DID of the space the content is stored in.
*/
with: Schema.did(),
nb: Schema.struct({
/**
* CID of the content that resulted in Filecoin piece.
*/
content: Schema.link(),
/**
* CID of the piece.
*/
piece: PieceLink.optional(),
}),
derives: (claim, from) => {
return (
and(equalWith(claim, from)) ||
and(checkLink(claim.nb.content, from.nb.content, 'nb.content')) ||
ok({})
)
},
})
1 change: 1 addition & 0 deletions packages/capabilities/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export const abilitiesAsStrings = [
Storefront.filecoinOffer.can,
Storefront.filecoinSubmit.can,
Storefront.filecoinAccept.can,
Storefront.filecoinInfo.can,
Aggregator.pieceOffer.can,
Aggregator.pieceAccept.can,
Dealer.aggregateOffer.can,
Expand Down
19 changes: 19 additions & 0 deletions packages/capabilities/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,21 @@ export interface ProofNotFound extends Ucanto.Failure {
name: 'ProofNotFound'
}

export interface FilecoinInfoSuccess {
piece: PieceLink
deals: FilecoinInfoAcceptedDeal[]
}
export interface FilecoinInfoAcceptedDeal
extends DataAggregationProof,
DealDetails {
aggregate: PieceLink
}

export type FilecoinInfoFailure =
| ContentNotFound
| InvalidContentPiece
| Ucanto.Failure

// filecoin aggregator
export interface PieceOfferSuccess {
/**
Expand Down Expand Up @@ -549,6 +564,9 @@ export type FilecoinSubmit = InferInvokedCapability<
export type FilecoinAccept = InferInvokedCapability<
typeof StorefrontCaps.filecoinAccept
>
export type FilecoinInfo = InferInvokedCapability<
typeof StorefrontCaps.filecoinInfo
>
export type PieceOffer = InferInvokedCapability<
typeof AggregatorCaps.pieceOffer
>
Expand Down Expand Up @@ -610,6 +628,7 @@ export type AbilitiesArray = [
FilecoinOffer['can'],
FilecoinSubmit['can'],
FilecoinAccept['can'],
FilecoinInfo['can'],
PieceOffer['can'],
PieceAccept['can'],
AggregateOffer['can'],
Expand Down
87 changes: 43 additions & 44 deletions packages/eslint-config-w3up/index.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,60 @@
'use strict'

module.exports = {
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:jsdoc/recommended",
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:jsdoc/recommended',
],
rules: {
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/ban-ts-comment": "off",
'@typescript-eslint/no-unused-vars': 'off',
'@typescript-eslint/ban-ts-comment': 'off',

/**
* many of these rules are inherited from hd-scripts.
* It may be useful over time to remove these rules
* and juse use plugin:jsdoc/recommended defaults.
* But that might require updating src.
*/
"jsdoc/check-values": "off",
"jsdoc/check-tag-names": "off",
"jsdoc/require-jsdoc": [
"warn",
'jsdoc/check-values': 'off',
'jsdoc/check-tag-names': 'off',
'jsdoc/require-jsdoc': [
'warn',
{
"publicOnly": true
}
publicOnly: true,
},
],
"jsdoc/require-returns": "off",
"jsdoc/require-returns-type": "off",
"jsdoc/require-throws": "off",
"jsdoc/require-yields": "off",
"jsdoc/require-param-description": "off",
"jsdoc/require-property-description": "off",
"jsdoc/require-returns-description": "off",
"jsdoc/valid-types": "off",
"jsdoc/tag-lines": [
"error",
"any",
'jsdoc/require-returns': 'off',
'jsdoc/require-returns-type': 'off',
'jsdoc/require-throws': 'off',
'jsdoc/require-yields': 'off',
'jsdoc/require-param-description': 'off',
'jsdoc/require-property-description': 'off',
'jsdoc/require-returns-description': 'off',
'jsdoc/valid-types': 'off',
'jsdoc/tag-lines': [
'error',
'any',
{
"startLines": 1
}
startLines: 1,
},
],
"jsdoc/no-undefined-types": [
"error",
'jsdoc/no-undefined-types': [
'error',
{
"definedTypes": [
"ArrayLike",
"AsyncIterable",
"AsyncIterableIterator",
"Iterable",
"IterableIterator",
"Generator",
"CryptoKeyPair",
"NodeJS",
"ErrorOptions",
"IDBTransactionMode"
]
}
]

}
definedTypes: [
'ArrayLike',
'AsyncIterable',
'AsyncIterableIterator',
'Iterable',
'IterableIterator',
'Generator',
'CryptoKeyPair',
'NodeJS',
'ErrorOptions',
'IDBTransactionMode',
],
},
],
},
}
6 changes: 5 additions & 1 deletion packages/filecoin-api/src/aggregator/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ export type BufferQueue = Queue<BufferMessage>
export type BufferStore = Store<Link, BufferRecord>
export type AggregateStore = Store<AggregateRecordKey, AggregateRecord>
export type PieceAcceptQueue = Queue<PieceAcceptMessage>
export type InclusionStore = QueryableStore<InclusionRecordKey, InclusionRecord, InclusionRecordQueryByGroup>
export type InclusionStore = QueryableStore<
InclusionRecordKey,
InclusionRecord,
InclusionRecordQueryByGroup
>
export type AggregateOfferQueue = Queue<AggregateOfferMessage>

export interface ServiceContext {
Expand Down
4 changes: 3 additions & 1 deletion packages/filecoin-api/src/aggregator/buffer-reducing.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ export async function handleBufferReducingWithAggregate({
pieces: aggregateInfo.addedBufferedPieces,
group,
}
const piecesBlock = await CBOR.write(aggregateInfo.addedBufferedPieces.map(bf => bf.piece))
const piecesBlock = await CBOR.write(
aggregateInfo.addedBufferedPieces.map((bf) => bf.piece)
)
const aggregateBlock = await CBOR.write(aggregateReducedBuffer)

// Store buffered pieces for aggregate
Expand Down
2 changes: 1 addition & 1 deletion packages/filecoin-api/src/aggregator/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const pieceAccept = async ({ capability }, context) => {
}
}

// Get buffered pieces
// Get buffered pieces
const [{ aggregate, inclusion }] = getInclusionRes.ok
const getAggregateRes = await context.aggregateStore.get({ aggregate })
if (getAggregateRes.error) {
Expand Down
6 changes: 5 additions & 1 deletion packages/filecoin-api/src/deal-tracker/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import type { Signer } from '@ucanto/interface'
import { PieceLink } from '@web3-storage/data-segment'
import { QueryableStore } from '../types.js'

export type DealStore = QueryableStore<DealRecordKey, DealRecord, DealRecordQueryByPiece>
export type DealStore = QueryableStore<
DealRecordKey,
DealRecord,
DealRecordQueryByPiece
>

export interface ServiceContext {
/**
Expand Down
3 changes: 2 additions & 1 deletion packages/filecoin-api/src/dealer/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ export interface AggregateUpdatedStatusEventContext {
dealerService: ServiceConfig<DealerService>
}

export interface CronContext extends Pick<ServiceContext, 'aggregateStore' | 'dealTrackerService'> {}
export interface CronContext
extends Pick<ServiceContext, 'aggregateStore' | 'dealTrackerService'> {}

export interface AggregateRecord {
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/filecoin-api/src/dealer/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ async function updateApprovedDeals({
{ aggregate: deal.aggregate },
{
status: 'accepted',
updatedAt: new Date().toISOString()
updatedAt: new Date().toISOString(),
}
)

Expand Down
15 changes: 7 additions & 8 deletions packages/filecoin-api/src/dealer/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ import * as DealerCaps from '@web3-storage/capabilities/filecoin/dealer'
import { DealTracker } from '@web3-storage/filecoin-client'
// eslint-disable-next-line no-unused-vars
import * as API from '../types.js'
import {
StoreOperationFailed,
DecodeBlockOperationFailed,
} from '../errors.js'
import { StoreOperationFailed, DecodeBlockOperationFailed } from '../errors.js'

/**
* @param {API.Input<DealerCaps.aggregateOffer>} input
Expand Down Expand Up @@ -112,17 +109,19 @@ export const aggregateAccept = async ({ capability }, context) => {
const deals = Object.keys(info.out.ok.deals || {})
if (!deals.length) {
return {
error: new Server.Failure('no deals were obtained for given aggregate CID')
error: new Server.Failure(
'no deals were obtained for given aggregate CID'
),
}
}

// For receipts, we only care about first deal
// TODO: We need to revisit this with renewals
const deal = {
dataType: 0n,
dataSource: {
dealID: BigInt(deals[0])
}
dealID: BigInt(deals[0]),
},
}

return {
Expand Down
24 changes: 24 additions & 0 deletions packages/filecoin-api/src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,30 @@ export class RecordNotFound extends Server.Failure {
}
}

export const ContentNotFoundErrorName = /** @type {const} */ ('ContentNotFound')
export class ContentNotFound extends Server.Failure {
get reason() {
return this.message
}

get name() {
return ContentNotFoundErrorName
}
}

export const InvalidContentPieceErrorName = /** @type {const} */ (
'InvalidContentPiece'
)
export class InvalidContentPiece extends Server.Failure {
get reason() {
return this.message
}

get name() {
return InvalidContentPieceErrorName
}
}

export const EncodeRecordErrorName = /** @type {const} */ ('EncodeRecordFailed')
export class EncodeRecordFailed extends Server.Failure {
get reason() {
Expand Down
11 changes: 10 additions & 1 deletion packages/filecoin-api/src/storefront/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { PieceLink } from '@web3-storage/data-segment'
import {
AggregatorService,
StorefrontService,
DealTrackerService,
} from '@web3-storage/filecoin-client/types'
import {
Store,
Expand All @@ -18,7 +19,11 @@ import {
ServiceConfig,
} from '../types.js'

export type PieceStore = UpdatableAndQueryableStore<PieceRecordKey, PieceRecord, Pick<PieceRecord, 'status'>>
export type PieceStore = UpdatableAndQueryableStore<
PieceRecordKey,
PieceRecord,
Pick<PieceRecord, 'status'> | Pick<PieceRecord, 'content'>
>
export type FilecoinSubmitQueue = Queue<FilecoinSubmitMessage>
export type PieceOfferQueue = Queue<PieceOfferMessage>
export type TaskStore = Store<UnknownLink, Invocation>
Expand Down Expand Up @@ -60,6 +65,10 @@ export interface ServiceContext {
* Stores receipts for tasks.
*/
receiptStore: ReceiptStore
/**
* Deal tracker connection to find out available deals for an aggregate.
*/
dealTrackerService: ServiceConfig<DealTrackerService>
/**
* Service options.
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/filecoin-api/src/storefront/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const handleFilecoinSubmitMessage = async (context, message) => {
}
if (hasRes.ok) {
return {
ok: {}
ok: {},
}
}

Expand Down
Loading

0 comments on commit 115237a

Please sign in to comment.