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

refactor: filecoin api services events and tests #974

Merged
merged 21 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor: make has fail when not found like get
  • Loading branch information
vasco-santos committed Oct 23, 2023
commit d3c9f54d224b41269e002ac8c19c247b5be0eb4c
7 changes: 5 additions & 2 deletions packages/filecoin-api/src/aggregator/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
QueueOperationFailed,
StoreOperationFailed,
UnexpectedState,
RecordNotFoundErrorName
} from '../errors.js'

/**
Expand All @@ -21,12 +22,14 @@ export const pieceOffer = async ({ capability }, context) => {

// dedupe
const hasRes = await context.pieceStore.has({ piece, group })
if (hasRes.error) {
let exists = true
if (hasRes.error?.name === RecordNotFoundErrorName) {
exists = false
} else if (hasRes.error) {
return {
error: new StoreOperationFailed(hasRes.error.message),
}
}
const exists = hasRes.ok

if (!exists) {
const addRes = await context.pieceQueue.add({ piece, group })
Expand Down
8 changes: 5 additions & 3 deletions packages/filecoin-api/src/dealer/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as Block from 'multiformats/block'
import * as DealerCaps from '@web3-storage/capabilities/filecoin/dealer'
// eslint-disable-next-line no-unused-vars
import * as API from '../types.js'
import { StoreOperationFailed, DecodeBlockOperationFailed } from '../errors.js'
import { StoreOperationFailed, DecodeBlockOperationFailed, RecordNotFoundErrorName } from '../errors.js'

/**
* @param {API.Input<DealerCaps.aggregateOffer>} input
Expand All @@ -19,12 +19,14 @@ export const aggregateOffer = async ({ capability, invocation }, context) => {
const { aggregate, pieces } = capability.nb

const hasRes = await context.aggregateStore.has({ aggregate })
if (hasRes.error) {
let exists = true
if (hasRes.error?.name === RecordNotFoundErrorName) {
exists = false
} else if (hasRes.error) {
return {
error: new StoreOperationFailed(hasRes.error.message),
}
}
const exists = hasRes.ok

if (!exists) {
const piecesBlockRes = await findCBORBlock(
Expand Down
5 changes: 1 addition & 4 deletions packages/filecoin-api/src/storefront/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@ import {
export const handleFilecoinSubmitMessage = async (context, message) => {
// dedupe concurrent writes
const hasRes = await context.pieceStore.has({ piece: message.piece })
if (hasRes.error) {
if (hasRes.error && hasRes.error.name !== RecordNotFoundErrorName) {
return { error: new StoreOperationFailed(hasRes.error.message) }
}
if (hasRes.ok) {
return { ok: {} }
}

// TODO: verify piece

Expand Down
10 changes: 6 additions & 4 deletions packages/filecoin-api/src/storefront/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as StorefrontCaps from '@web3-storage/capabilities/filecoin/storefront'
import * as AggregatorCaps from '@web3-storage/capabilities/filecoin/aggregator'
// eslint-disable-next-line no-unused-vars
import * as API from '../types.js'
import { QueueOperationFailed, StoreOperationFailed } from '../errors.js'
import { QueueOperationFailed, RecordNotFoundErrorName, StoreOperationFailed } from '../errors.js'

/**
* @param {API.Input<StorefrontCaps.filecoinOffer>} input
Expand All @@ -19,12 +19,14 @@ export const filecoinOffer = async ({ capability }, context) => {
if (!context.options?.skipFilecoinSubmitQueue) {
// dedupe
const hasRes = await context.pieceStore.has({ piece })
if (hasRes.error) {
let exists = true
if (hasRes.error?.name === RecordNotFoundErrorName) {
exists = false
} else if (hasRes.error) {
return { error: new StoreOperationFailed(hasRes.error.message) }
}
const exists = hasRes.ok
const group = context.id.did()

const group = context.id.did()
if (!exists) {
// Queue the piece for validation etc.
const queueRes = await context.filecoinSubmitQueue.add({
Expand Down
8 changes: 7 additions & 1 deletion packages/filecoin-api/test/context/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,14 @@ export class Store {
if (!this.getFn) {
throw new Error('has not supported')
}
const t = this.getFn(this.items, item)
if (!t) {
return {
error: new RecordNotFound(),
}
}
return {
ok: Boolean(this.getFn(this.items, item)),
ok: Boolean(t),
}
}

Expand Down