Skip to content

Commit

Permalink
feat: add blob protocol to upload-client (#1425)
Browse files Browse the repository at this point in the history
  • Loading branch information
joaosa authored May 13, 2024
1 parent 9982d12 commit 49aef56
Show file tree
Hide file tree
Showing 23 changed files with 1,704 additions and 211 deletions.
8 changes: 8 additions & 0 deletions packages/access-client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ import type {
UCANRevoke,
UCANRevokeSuccess,
UCANRevokeFailure,
UCANConclude,
UCANConcludeSuccess,
UCANConcludeFailure,
AccountDID,
ProviderDID,
SpaceDID,
Expand Down Expand Up @@ -132,6 +135,11 @@ export interface Service {
}
ucan: {
revoke: ServiceMethod<UCANRevoke, UCANRevokeSuccess, UCANRevokeFailure>
conclude: ServiceMethod<
UCANConclude,
UCANConcludeSuccess,
UCANConcludeFailure
>
}
plan: {
get: ServiceMethod<PlanGet, PlanGetSuccess, PlanGetFailure>
Expand Down
2 changes: 1 addition & 1 deletion packages/capabilities/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ export type BlobAllocateFailure = NotEnoughStorageCapacity | Ucanto.Failure

// Blob accept
export interface BlobAcceptSuccess {
// A Link for a delegation with site commiment for the added blob.
// A Link for a delegation with site commitment for the added blob.
site: Link
}

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

describe() {
return `Record not found`
}

get name() {
return RecordNotFoundErrorName
}
Expand Down
5 changes: 5 additions & 0 deletions packages/upload-api/src/blob/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ async function allocate({ context, blob, space, cause }) {

// 3. if not already allocated (or expired) execute `blob/allocate`
if (!blobAllocateReceipt) {
// Create allocation task and save it
const saveTask = await context.tasksStorage.put(task)
if (!saveTask.ok) {
return saveTask
}
// Execute allocate invocation
const allocateRes = await allocate.execute(context.getServiceConnection())
if (allocateRes.out.error) {
Expand Down
4 changes: 4 additions & 0 deletions packages/upload-api/src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export class RecordNotFound extends Server.Failure {
return this.message
}

describe() {
return `Record not found`
}

get name() {
return RecordNotFoundErrorName
}
Expand Down
2 changes: 1 addition & 1 deletion packages/upload-api/test/storage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export async function getServiceStorageImplementations(options) {
const dudewhereBucket = new DudewhereBucket()
const revocationsStorage = new RevocationsStorage()
const plansStorage = new PlansStorage()
const usageStorage = new UsageStorage(storeTable)
const usageStorage = new UsageStorage(storeTable, allocationsStorage)
const provisionsStorage = new ProvisionsStorage(options.providers)
const subscriptionsStorage = new SubscriptionsStorage(provisionsStorage)
const delegationsStorage = new DelegationsStorage()
Expand Down
27 changes: 22 additions & 5 deletions packages/upload-api/test/storage/usage-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,26 @@

/** @implements {UsageStore} */
export class UsageStorage {
/** @param {import('./store-table.js').StoreTable} storeTable */
constructor(storeTable) {
/**
* @param {import('./store-table.js').StoreTable} storeTable
* @param {import('./allocations-storage.js').AllocationsStorage} allocationsStorage
*/
constructor(storeTable, allocationsStorage) {
this.storeTable = storeTable
this.allocationsStorage = allocationsStorage
}

get items() {
return [
...this.storeTable.items.map((item) => ({
...item,
cause: item.invocation,
})),
...this.allocationsStorage.items.map((item) => ({
...item,
size: item.blob.size,
})),
]
}

/**
Expand All @@ -13,11 +30,11 @@ export class UsageStorage {
* @param {{ from: Date, to: Date }} period
*/
async report(provider, space, period) {
const before = this.storeTable.items.filter((item) => {
const before = this.items.filter((item) => {
const insertTime = new Date(item.insertedAt).getTime()
return item.space === space && insertTime < period.from.getTime()
})
const during = this.storeTable.items.filter((item) => {
const during = this.items.filter((item) => {
const insertTime = new Date(item.insertedAt).getTime()
return (
item.space === space &&
Expand All @@ -39,7 +56,7 @@ export class UsageStorage {
size: { initial, final },
events: during.map((item) => {
return {
cause: item.invocation.link(),
cause: /** @type {import('../types.js').Link} */ (item.cause),
delta: item.size,
receiptAt: item.insertedAt,
}
Expand Down
1 change: 1 addition & 0 deletions packages/upload-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"@ipld/dag-cbor": "^9.0.6",
"@ipld/dag-ucan": "^3.4.0",
"@ipld/unixfs": "^2.1.1",
"@ucanto/core": "^10.0.1",
"@ucanto/client": "^9.0.1",
"@ucanto/interface": "^10.0.1",
"@ucanto/transport": "^9.1.1",
Expand Down
Loading

0 comments on commit 49aef56

Please sign in to comment.