From 43932a54036dafdf1265b034b30b12784fd22d82 Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Fri, 13 Oct 2023 19:08:58 +0300 Subject: [PATCH] refactor: use functions for block broker creation (#286) --- packages/helia/.aegir.js | 4 +- .../{bitswap-block-broker.ts => bitswap.ts} | 28 +++++--- packages/helia/src/block-brokers/index.ts | 4 +- .../block-brokers/trustless-gateway/broker.ts | 65 ++++++++++++++++++ .../block-brokers/trustless-gateway/index.ts | 31 +++++++++ .../trustless-gateway.ts} | 66 ------------------- packages/helia/src/helia.ts | 24 +++++-- packages/helia/src/index.ts | 44 +------------ .../test/block-brokers/block-broker.spec.ts | 12 ++-- ...oker.spec.ts => trustless-gateway.spec.ts} | 36 +++++----- packages/helia/test/fixtures/create-helia.ts | 4 +- .../test/utils/networked-storage.spec.ts | 11 +--- packages/interface/src/index.ts | 18 +---- .../test/fixtures/create-helia.browser.ts | 4 +- .../interop/test/fixtures/create-helia.ts | 4 +- 15 files changed, 176 insertions(+), 179 deletions(-) rename packages/helia/src/block-brokers/{bitswap-block-broker.ts => bitswap.ts} (68%) create mode 100644 packages/helia/src/block-brokers/trustless-gateway/broker.ts create mode 100644 packages/helia/src/block-brokers/trustless-gateway/index.ts rename packages/helia/src/block-brokers/{trustless-gateway-block-broker.ts => trustless-gateway/trustless-gateway.ts} (61%) rename packages/helia/test/block-brokers/{trustless-gateway-block-broker.spec.ts => trustless-gateway.spec.ts} (88%) diff --git a/packages/helia/.aegir.js b/packages/helia/.aegir.js index fa0c2d9d..a1cde6b0 100644 --- a/packages/helia/.aegir.js +++ b/packages/helia/.aegir.js @@ -11,11 +11,11 @@ const options = { before: async () => { // use dynamic import otherwise the source may not have been built yet const { createHelia } = await import('./dist/src/index.js') - const { BitswapBlockBrokerFactory } = await import('./dist/src/block-brokers/index.js') + const { bitswap } = await import('./dist/src/block-brokers/index.js') const helia = await createHelia({ blockBrokers: [ - BitswapBlockBrokerFactory + bitswap() ], libp2p: { addresses: { diff --git a/packages/helia/src/block-brokers/bitswap-block-broker.ts b/packages/helia/src/block-brokers/bitswap.ts similarity index 68% rename from packages/helia/src/block-brokers/bitswap-block-broker.ts rename to packages/helia/src/block-brokers/bitswap.ts index 96f29b01..54f19b33 100644 --- a/packages/helia/src/block-brokers/bitswap-block-broker.ts +++ b/packages/helia/src/block-brokers/bitswap.ts @@ -1,21 +1,32 @@ import { createBitswap } from 'ipfs-bitswap' -import type { BlockBrokerFactoryFunction } from '@helia/interface' -import type { BlockAnnouncer, BlockRetrievalOptions, BlockRetriever } from '@helia/interface/blocks' +import type { BlockAnnouncer, BlockBroker, BlockRetrievalOptions, BlockRetriever } from '@helia/interface/blocks' import type { Libp2p } from '@libp2p/interface' import type { Startable } from '@libp2p/interface/startable' import type { Blockstore } from 'interface-blockstore' -import type { Bitswap, BitswapNotifyProgressEvents, BitswapWantBlockProgressEvents } from 'ipfs-bitswap' +import type { Bitswap, BitswapNotifyProgressEvents, BitswapOptions, BitswapWantBlockProgressEvents } from 'ipfs-bitswap' import type { CID } from 'multiformats/cid' import type { MultihashHasher } from 'multiformats/hashes/interface' import type { ProgressOptions } from 'progress-events' -export class BitswapBlockBroker implements BlockAnnouncer>, BlockRetriever< +interface BitswapComponents { + libp2p: Libp2p + blockstore: Blockstore + hashers: MultihashHasher[] +} + +export interface BitswapInit extends BitswapOptions { + +} + +class BitswapBlockBroker implements BlockAnnouncer>, BlockRetriever< ProgressOptions >, Startable { private readonly bitswap: Bitswap private started: boolean - constructor (libp2p: Libp2p, blockstore: Blockstore, hashers: MultihashHasher[]) { + constructor (components: BitswapComponents, init: BitswapInit = {}) { + const { libp2p, blockstore, hashers } = components + this.bitswap = createBitswap(libp2p, blockstore, { hashLoader: { getHasher: async (codecOrName: string | number): Promise> => { @@ -29,7 +40,8 @@ ProgressOptions throw new Error(`Could not load hasher for code/name "${codecOrName}"`) } - } + }, + ...init }) this.started = false } @@ -61,6 +73,6 @@ ProgressOptions * A helper factory for users who want to override Helia `blockBrokers` but * still want to use the default `BitswapBlockBroker`. */ -export const BitswapBlockBrokerFactory: BlockBrokerFactoryFunction = (components): BitswapBlockBroker => { - return new BitswapBlockBroker(components.libp2p, components.blockstore, components.hashers) +export function bitswap (init: BitswapInit = {}): (components: BitswapComponents) => BlockBroker { + return (components) => new BitswapBlockBroker(components, init) } diff --git a/packages/helia/src/block-brokers/index.ts b/packages/helia/src/block-brokers/index.ts index 90dc1151..e7f503c9 100644 --- a/packages/helia/src/block-brokers/index.ts +++ b/packages/helia/src/block-brokers/index.ts @@ -1,2 +1,2 @@ -export { BitswapBlockBroker, BitswapBlockBrokerFactory } from './bitswap-block-broker.js' -export { TrustlessGatewayBlockBroker } from './trustless-gateway-block-broker.js' +export { bitswap } from './bitswap.js' +export { trustlessGateway } from './trustless-gateway/index.js' diff --git a/packages/helia/src/block-brokers/trustless-gateway/broker.ts b/packages/helia/src/block-brokers/trustless-gateway/broker.ts new file mode 100644 index 00000000..918676e8 --- /dev/null +++ b/packages/helia/src/block-brokers/trustless-gateway/broker.ts @@ -0,0 +1,65 @@ +import { logger } from '@libp2p/logger' +import { TrustlessGateway } from './trustless-gateway.js' +import { DEFAULT_TRUSTLESS_GATEWAYS } from './index.js' +import type { TrustlessGatewayBlockBrokerInit, TrustlessGatewayGetBlockProgressEvents } from './index.js' +import type { BlockRetrievalOptions, BlockRetriever } from '@helia/interface/blocks' +import type { CID } from 'multiformats/cid' +import type { ProgressOptions } from 'progress-events' + +const log = logger('helia:trustless-gateway-block-broker') + +/** + * A class that accepts a list of trustless gateways that are queried + * for blocks. + */ +export class TrustlessGatewayBlockBroker implements BlockRetriever< +ProgressOptions +> { + private readonly gateways: TrustlessGateway[] + + constructor (init: TrustlessGatewayBlockBrokerInit = {}) { + this.gateways = (init.gateways ?? DEFAULT_TRUSTLESS_GATEWAYS) + .map((gatewayOrUrl) => { + return new TrustlessGateway(gatewayOrUrl) + }) + } + + async retrieve (cid: CID, options: BlockRetrievalOptions> = {}): Promise { + // Loop through the gateways until we get a block or run out of gateways + // TODO: switch to toSorted when support is better + const sortedGateways = this.gateways.sort((a, b) => b.reliability() - a.reliability()) + const aggregateErrors: Error[] = [] + + for (const gateway of sortedGateways) { + log('getting block for %c from %s', cid, gateway.url) + try { + const block = await gateway.getRawBlock(cid, options.signal) + log.trace('got block for %c from %s', cid, gateway.url) + try { + await options.validateFn?.(block) + } catch (err) { + log.error('failed to validate block for %c from %s', cid, gateway.url, err) + gateway.incrementInvalidBlocks() + + throw new Error(`unable to validate block for CID ${cid} from gateway ${gateway.url}`) + } + + return block + } catch (err: unknown) { + log.error('failed to get block for %c from %s', cid, gateway.url, err) + if (err instanceof Error) { + aggregateErrors.push(err) + } else { + aggregateErrors.push(new Error(`unable to fetch raw block for CID ${cid} from gateway ${gateway.url}`)) + } + // if signal was aborted, exit the loop + if (options.signal?.aborted === true) { + log.trace('request aborted while fetching raw block for CID %c from gateway %s', cid, gateway.url) + break + } + } + } + + throw new AggregateError(aggregateErrors, `unable to fetch raw block for CID ${cid} from any gateway`) + } +} diff --git a/packages/helia/src/block-brokers/trustless-gateway/index.ts b/packages/helia/src/block-brokers/trustless-gateway/index.ts new file mode 100644 index 00000000..c8f83377 --- /dev/null +++ b/packages/helia/src/block-brokers/trustless-gateway/index.ts @@ -0,0 +1,31 @@ +import { TrustlessGatewayBlockBroker } from './broker.js' +import type { BlockRetriever } from '@helia/interface/src/blocks.js' +import type { ProgressEvent } from 'progress-events' + +export const DEFAULT_TRUSTLESS_GATEWAYS = [ + // 2023-10-03: IPNS, Origin, and Block/CAR support from https://ipfs-public-gateway-checker.on.fleek.co/ + 'https://dweb.link', + + // 2023-10-03: IPNS, Origin, and Block/CAR support from https://ipfs-public-gateway-checker.on.fleek.co/ + 'https://cf-ipfs.com', + + // 2023-10-03: IPNS, Origin, and Block/CAR support from https://ipfs-public-gateway-checker.on.fleek.co/ + 'https://4everland.io', + + // 2023-10-03: IPNS, Origin, and Block/CAR support from https://ipfs-public-gateway-checker.on.fleek.co/ + 'https://w3s.link', + + // 2023-10-03: IPNS, and Block/CAR support from https://ipfs-public-gateway-checker.on.fleek.co/ + 'https://cloudflare-ipfs.com' +] + +export type TrustlessGatewayGetBlockProgressEvents = + ProgressEvent<'trustless-gateway:get-block:fetch', URL> + +export interface TrustlessGatewayBlockBrokerInit { + gateways?: Array +} + +export function trustlessGateway (init: TrustlessGatewayBlockBrokerInit = {}): () => BlockRetriever { + return () => new TrustlessGatewayBlockBroker(init) +} diff --git a/packages/helia/src/block-brokers/trustless-gateway-block-broker.ts b/packages/helia/src/block-brokers/trustless-gateway/trustless-gateway.ts similarity index 61% rename from packages/helia/src/block-brokers/trustless-gateway-block-broker.ts rename to packages/helia/src/block-brokers/trustless-gateway/trustless-gateway.ts index 29ecaace..352d6ea3 100644 --- a/packages/helia/src/block-brokers/trustless-gateway-block-broker.ts +++ b/packages/helia/src/block-brokers/trustless-gateway/trustless-gateway.ts @@ -1,9 +1,4 @@ -import { logger } from '@libp2p/logger' -import type { BlockRetrievalOptions, BlockRetriever } from '@helia/interface/blocks' import type { CID } from 'multiformats/cid' -import type { ProgressEvent, ProgressOptions } from 'progress-events' - -const log = logger('helia:trustless-gateway-block-broker') /** * A `TrustlessGateway` keeps track of the number of attempts, errors, and @@ -129,64 +124,3 @@ export class TrustlessGateway { this.#invalidBlocks++ } } - -export type TrustlessGatewayGetBlockProgressEvents = - ProgressEvent<'trustless-gateway:get-block:fetch', URL> - -/** - * A class that accepts a list of trustless gateways that are queried - * for blocks. - */ -export class TrustlessGatewayBlockBroker implements BlockRetriever< -ProgressOptions -> { - private readonly gateways: TrustlessGateway[] - - constructor (gatewaysOrUrls: Array) { - this.gateways = gatewaysOrUrls.map((gatewayOrUrl) => { - if (gatewayOrUrl instanceof TrustlessGateway || Object.prototype.hasOwnProperty.call(gatewayOrUrl, 'getRawBlock')) { - return gatewayOrUrl as TrustlessGateway - } - // eslint-disable-next-line no-console - console.trace('creating new TrustlessGateway for %s', gatewayOrUrl) - return new TrustlessGateway(gatewayOrUrl) - }) - } - - async retrieve (cid: CID, options: BlockRetrievalOptions> = {}): Promise { - // Loop through the gateways until we get a block or run out of gateways - const sortedGateways = this.gateways.sort((a, b) => b.reliability() - a.reliability()) - const aggregateErrors: Error[] = [] - for (const gateway of sortedGateways) { - log('getting block for %c from %s', cid, gateway.url) - try { - const block = await gateway.getRawBlock(cid, options.signal) - log.trace('got block for %c from %s', cid, gateway.url) - try { - await options.validateFn?.(block) - } catch (err) { - log.error('failed to validate block for %c from %s', cid, gateway.url, err) - gateway.incrementInvalidBlocks() - - throw new Error(`unable to validate block for CID ${cid} from gateway ${gateway.url}`) - } - - return block - } catch (err: unknown) { - log.error('failed to get block for %c from %s', cid, gateway.url, err) - if (err instanceof Error) { - aggregateErrors.push(err) - } else { - aggregateErrors.push(new Error(`unable to fetch raw block for CID ${cid} from gateway ${gateway.url}`)) - } - // if signal was aborted, exit the loop - if (options.signal?.aborted === true) { - log.trace('request aborted while fetching raw block for CID %c from gateway %s', cid, gateway.url) - break - } - } - } - - throw new AggregateError(aggregateErrors, `unable to fetch raw block for CID ${cid} from any gateway`) - } -} diff --git a/packages/helia/src/helia.ts b/packages/helia/src/helia.ts index b0735d8d..8e7bf052 100644 --- a/packages/helia/src/helia.ts +++ b/packages/helia/src/helia.ts @@ -1,11 +1,12 @@ -import { type BlockBroker } from '@helia/interface/blocks' import { start, stop } from '@libp2p/interface/startable' import { logger } from '@libp2p/logger' import drain from 'it-drain' import { CustomProgressEvent } from 'progress-events' +import { bitswap, trustlessGateway } from './block-brokers/index.js' import { PinsImpl } from './pins.js' import { BlockStorage } from './storage.js' import { assertDatastoreVersionIsCurrent } from './utils/datastore-version.js' +import { defaultHashers } from './utils/default-hashers.js' import { NetworkedStorage } from './utils/networked-storage.js' import type { HeliaInit } from '.' import type { GCOptions, Helia } from '@helia/interface' @@ -20,7 +21,6 @@ const log = logger('helia') interface HeliaImplInit extends HeliaInit { libp2p: T blockstore: Blockstore - blockBrokers: BlockBroker[] datastore: Datastore } @@ -31,9 +31,25 @@ export class HeliaImpl implements Helia { public pins: Pins constructor (init: HeliaImplInit) { + const hashers = defaultHashers(init.hashers) + + const components = { + blockstore: init.blockstore, + datastore: init.datastore, + libp2p: init.libp2p, + hashers + } + + const blockBrokers = init.blockBrokers?.map((fn) => { + return fn(components) + }) ?? [ + bitswap()(components), + trustlessGateway()() + ] + const networkedStorage = new NetworkedStorage(init.blockstore, { - blockBrokers: init.blockBrokers, - hashers: init.hashers + blockBrokers, + hashers }) this.pins = new PinsImpl(init.datastore, networkedStorage, init.dagWalkers ?? []) diff --git a/packages/helia/src/index.ts b/packages/helia/src/index.ts index 6334eaf9..0313989e 100644 --- a/packages/helia/src/index.ts +++ b/packages/helia/src/index.ts @@ -24,13 +24,11 @@ import { logger } from '@libp2p/logger' import { MemoryBlockstore } from 'blockstore-core' import { MemoryDatastore } from 'datastore-core' -import { BitswapBlockBroker, TrustlessGatewayBlockBroker } from './block-brokers/index.js' import { HeliaImpl } from './helia.js' -import { defaultHashers } from './utils/default-hashers.js' import { createLibp2p } from './utils/libp2p.js' import { name, version } from './version.js' import type { DefaultLibp2pServices } from './utils/libp2p-defaults.js' -import type { Helia, BlockBrokerFactoryFunction } from '@helia/interface' +import type { Helia } from '@helia/interface' import type { BlockBroker } from '@helia/interface/blocks' import type { Libp2p } from '@libp2p/interface' import type { Blockstore } from 'interface-blockstore' @@ -98,7 +96,7 @@ export interface HeliaInit { * A list of strategies used to fetch blocks when they are not present in * the local blockstore */ - blockBrokers?: Array + blockBrokers?: Array<(components: any) => BlockBroker> /** * Pass `false` to not start the Helia node @@ -123,23 +121,6 @@ export interface HeliaInit { holdGcLock?: boolean } -const DEFAULT_TRUSTLESS_GATEWAYS = [ - // 2023-10-03: IPNS, Origin, and Block/CAR support from https://ipfs-public-gateway-checker.on.fleek.co/ - 'https://dweb.link', - - // 2023-10-03: IPNS, Origin, and Block/CAR support from https://ipfs-public-gateway-checker.on.fleek.co/ - 'https://cf-ipfs.com', - - // 2023-10-03: IPNS, Origin, and Block/CAR support from https://ipfs-public-gateway-checker.on.fleek.co/ - 'https://4everland.io', - - // 2023-10-03: IPNS, Origin, and Block/CAR support from https://ipfs-public-gateway-checker.on.fleek.co/ - 'https://w3s.link', - - // 2023-10-03: IPNS, and Block/CAR support from https://ipfs-public-gateway-checker.on.fleek.co/ - 'https://cloudflare-ipfs.com' -] - /** * Create and return a Helia node */ @@ -157,30 +138,11 @@ export async function createHelia (init: HeliaInit = {}): Promise libp2p = await createLibp2p(datastore, init.libp2p) } - const hashers = defaultHashers(init.hashers) - - const blockBrokers: BlockBroker[] = init.blockBrokers?.map((blockBroker: BlockBroker | BlockBrokerFactoryFunction): BlockBroker => { - if (typeof blockBroker !== 'function') { - return blockBroker satisfies BlockBroker - } - return blockBroker({ - blockstore, - datastore, - libp2p, - hashers - }) satisfies BlockBroker - }) ?? [ - new BitswapBlockBroker(libp2p, blockstore, hashers), - new TrustlessGatewayBlockBroker(DEFAULT_TRUSTLESS_GATEWAYS) - ] - const helia = new HeliaImpl({ ...init, datastore, blockstore, - libp2p, - blockBrokers, - hashers + libp2p }) if (init.start !== false) { diff --git a/packages/helia/test/block-brokers/block-broker.spec.ts b/packages/helia/test/block-brokers/block-broker.spec.ts index 36293bd1..3eb121e3 100644 --- a/packages/helia/test/block-brokers/block-broker.spec.ts +++ b/packages/helia/test/block-brokers/block-broker.spec.ts @@ -10,16 +10,16 @@ import { type StubbedInstance, stubInterface } from 'sinon-ts' import { defaultHashers } from '../../src/utils/default-hashers.js' import { NetworkedStorage } from '../../src/utils/networked-storage.js' import { createBlock } from '../fixtures/create-block.js' -import type { BitswapBlockBroker, TrustlessGatewayBlockBroker } from '../../src/block-brokers/index.js' +import type { BlockBroker, BlockRetriever } from '@helia/interface/blocks' import type { Blockstore } from 'interface-blockstore' import type { CID } from 'multiformats/cid' -describe('block-provider', () => { +describe('block-broker', () => { let storage: NetworkedStorage let blockstore: Blockstore - let bitswapBlockBroker: StubbedInstance + let bitswapBlockBroker: StubbedInstance> let blocks: Array<{ cid: CID, block: Uint8Array }> - let gatewayBlockBroker: StubbedInstance + let gatewayBlockBroker: StubbedInstance> beforeEach(async () => { blocks = [] @@ -29,8 +29,8 @@ describe('block-provider', () => { } blockstore = new MemoryBlockstore() - bitswapBlockBroker = stubInterface() - gatewayBlockBroker = stubInterface() + bitswapBlockBroker = stubInterface() + gatewayBlockBroker = stubInterface() storage = new NetworkedStorage(blockstore, { blockBrokers: [ bitswapBlockBroker, diff --git a/packages/helia/test/block-brokers/trustless-gateway-block-broker.spec.ts b/packages/helia/test/block-brokers/trustless-gateway.spec.ts similarity index 88% rename from packages/helia/test/block-brokers/trustless-gateway-block-broker.spec.ts rename to packages/helia/test/block-brokers/trustless-gateway.spec.ts index b12a70df..513a5b91 100644 --- a/packages/helia/test/block-brokers/trustless-gateway-block-broker.spec.ts +++ b/packages/helia/test/block-brokers/trustless-gateway.spec.ts @@ -3,14 +3,15 @@ import { expect } from 'aegir/chai' import * as raw from 'multiformats/codecs/raw' import Sinon from 'sinon' import { type StubbedInstance, stubConstructor } from 'sinon-ts' -import { TrustlessGatewayBlockBroker } from '../../src/block-brokers/index.js' -import { TrustlessGateway } from '../../src/block-brokers/trustless-gateway-block-broker.js' +import { TrustlessGatewayBlockBroker } from '../../src/block-brokers/trustless-gateway/broker.js' +import { TrustlessGateway } from '../../src/block-brokers/trustless-gateway/trustless-gateway.js' import { createBlock } from '../fixtures/create-block.js' +import type { BlockRetriever } from '@helia/interface/blocks' import type { CID } from 'multiformats/cid' describe('trustless-gateway-block-broker', () => { let blocks: Array<{ cid: CID, block: Uint8Array }> - let gatewayBlockBroker: TrustlessGatewayBlockBroker + let gatewayBlockBroker: BlockRetriever let gateways: Array> // take a Record) => void> and stub the gateways @@ -38,7 +39,9 @@ describe('trustless-gateway-block-broker', () => { stubConstructor(TrustlessGateway, 'http://localhost:8082'), stubConstructor(TrustlessGateway, 'http://localhost:8083') ] - gatewayBlockBroker = new TrustlessGatewayBlockBroker(gateways) + gatewayBlockBroker = new TrustlessGatewayBlockBroker() + // must copy the array because the broker calls .sort which mutates in-place + ;(gatewayBlockBroker as any).gateways = [...gateways] }) it('tries all gateways before failing', async () => { @@ -46,14 +49,12 @@ describe('trustless-gateway-block-broker', () => { for (const gateway of gateways) { gateway.getRawBlock.rejects(new Error('failed')) } - try { - await gatewayBlockBroker.retrieve(blocks[0].cid) - throw new Error('should have failed') - } catch (err: unknown) { - expect(err).to.exist() - expect(err).to.be.an.instanceOf(AggregateError) - expect((err as AggregateError).errors).to.have.lengthOf(gateways.length) - } + + await expect(gatewayBlockBroker.retrieve(blocks[0].cid)) + .to.eventually.be.rejected() + .with.property('errors') + .with.lengthOf(gateways.length) + for (const gateway of gateways) { expect(gateway.getRawBlock.calledWith(blocks[0].cid)).to.be.true() } @@ -73,11 +74,8 @@ describe('trustless-gateway-block-broker', () => { } }) - try { - await gatewayBlockBroker.retrieve(blocks[1].cid) - } catch { - // ignore - } + await expect(gatewayBlockBroker.retrieve(blocks[1].cid)).to.eventually.be.rejected() + // all gateways were called expect(gateways[0].getRawBlock.calledWith(blocks[1].cid)).to.be.true() expect(gateways[1].getRawBlock.calledWith(blocks[1].cid)).to.be.true() @@ -102,7 +100,7 @@ describe('trustless-gateway-block-broker', () => { gateway.reliability.returns(1) // make sure other gateways are called first } }) - // try { + const block = await gatewayBlockBroker.retrieve(cid1, { validateFn: async (block) => { if (block !== block1) { @@ -118,7 +116,7 @@ describe('trustless-gateway-block-broker', () => { } }) - it('doesnt call other gateways if the first gateway returns a valid block', async () => { + it('does not call other gateways if the first gateway returns a valid block', async () => { const { cid: cid1, block: block1 } = blocks[0] const { block: block2 } = blocks[1] diff --git a/packages/helia/test/fixtures/create-helia.ts b/packages/helia/test/fixtures/create-helia.ts index 8fed714f..e4ac8de6 100644 --- a/packages/helia/test/fixtures/create-helia.ts +++ b/packages/helia/test/fixtures/create-helia.ts @@ -2,14 +2,14 @@ import { webSockets } from '@libp2p/websockets' import * as Filters from '@libp2p/websockets/filters' import { circuitRelayTransport } from 'libp2p/circuit-relay' import { identifyService } from 'libp2p/identify' -import { BitswapBlockBrokerFactory } from '../../src/block-brokers/index.js' +import { bitswap } from '../../src/block-brokers/index.js' import { createHelia as createNode } from '../../src/index.js' import type { Helia } from '@helia/interface' export async function createHelia (): Promise { return createNode({ blockBrokers: [ - BitswapBlockBrokerFactory + bitswap() ], libp2p: { addresses: { diff --git a/packages/helia/test/utils/networked-storage.spec.ts b/packages/helia/test/utils/networked-storage.spec.ts index 40124628..44762125 100644 --- a/packages/helia/test/utils/networked-storage.spec.ts +++ b/packages/helia/test/utils/networked-storage.spec.ts @@ -11,14 +11,14 @@ import { type StubbedInstance, stubInterface } from 'sinon-ts' import { defaultHashers } from '../../src/utils/default-hashers.js' import { NetworkedStorage } from '../../src/utils/networked-storage.js' import { createBlock } from '../fixtures/create-block.js' -import type { BitswapBlockBroker } from '../../src/block-brokers/bitswap-block-broker.js' +import type { BlockAnnouncer, BlockRetriever } from '@helia/interface/blocks' import type { Blockstore } from 'interface-blockstore' import type { CID } from 'multiformats/cid' describe('networked-storage', () => { let storage: NetworkedStorage let blockstore: Blockstore - let bitswap: StubbedInstance + let bitswap: StubbedInstance> let blocks: Array<{ cid: CID, block: Uint8Array }> beforeEach(async () => { @@ -29,7 +29,7 @@ describe('networked-storage', () => { } blockstore = new MemoryBlockstore() - bitswap = stubInterface() + bitswap = stubInterface() storage = new NetworkedStorage(blockstore, { blockBrokers: [ bitswap @@ -117,7 +117,6 @@ describe('networked-storage', () => { it('gets a block from bitswap when it is not in the blockstore', async () => { const { cid, block } = blocks[0] - bitswap.isStarted.returns(true) bitswap.retrieve.withArgs(cid).resolves(block) expect(await blockstore.has(cid)).to.be.false() @@ -130,8 +129,6 @@ describe('networked-storage', () => { }) it('gets many blocks from bitswap when they are not in the blockstore', async () => { - bitswap.isStarted.returns(true) - const count = 5 for (let i = 0; i < count; i++) { @@ -158,8 +155,6 @@ describe('networked-storage', () => { }) it('gets some blocks from bitswap when they are not in the blockstore', async () => { - bitswap.isStarted.returns(true) - const count = 5 // blocks 0,1,3,4 are in the blockstore diff --git a/packages/interface/src/index.ts b/packages/interface/src/index.ts index edd46b95..a6245b18 100644 --- a/packages/interface/src/index.ts +++ b/packages/interface/src/index.ts @@ -14,12 +14,11 @@ * ``` */ -import type { BlockBroker, Blocks } from './blocks.js' +import type { Blocks } from './blocks.js' import type { Pins } from './pins.js' import type { Libp2p, AbortOptions } from '@libp2p/interface' import type { Datastore } from 'interface-datastore' import type { CID } from 'multiformats/cid' -import type { MultihashHasher } from 'multiformats/hashes/interface' import type { ProgressEvent, ProgressOptions } from 'progress-events' export type { Await, AwaitIterable } from 'interface-store' @@ -71,18 +70,3 @@ export type GcEvents = export interface GCOptions extends AbortOptions, ProgressOptions { } -export type BlockBrokerFactoryComponents = Pick & { - hashers: MultihashHasher[] -} - -/** - * A function that receives some {@link Helia} components and returns a - * {@link BlockBroker}. - * - * This is needed in order to re-use some of the internal components Helia - * constructs without having to hoist each required component into the top-level - * scope. - */ -export interface BlockBrokerFactoryFunction { - (heliaComponents: BlockBrokerFactoryComponents): BlockBroker -} diff --git a/packages/interop/test/fixtures/create-helia.browser.ts b/packages/interop/test/fixtures/create-helia.browser.ts index 1c544b21..28a03223 100644 --- a/packages/interop/test/fixtures/create-helia.browser.ts +++ b/packages/interop/test/fixtures/create-helia.browser.ts @@ -5,7 +5,7 @@ import { all } from '@libp2p/websockets/filters' import { MemoryBlockstore } from 'blockstore-core' import { MemoryDatastore } from 'datastore-core' import { createHelia, type HeliaInit } from 'helia' -import { BitswapBlockBrokerFactory } from 'helia/block-brokers' +import { bitswap } from 'helia/block-brokers' import { createLibp2p } from 'libp2p' import { identifyService } from 'libp2p/identify' import type { Helia } from '@helia/interface' @@ -40,7 +40,7 @@ export async function createHeliaNode (init?: Partial): Promise): Promise