-
Notifications
You must be signed in to change notification settings - Fork 106
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
feat: BlockBroker factory support #284
Changes from 3 commits
e91f917
a0eff4d
2fc1043
e09d09f
9ae76ce
cc3e9ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,7 @@ 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 } from '@helia/interface' | ||
import type { Helia, BlockBrokerFactoryFunction } from '@helia/interface' | ||
import type { BlockBroker } from '@helia/interface/blocks' | ||
import type { Libp2p } from '@libp2p/interface' | ||
import type { Blockstore } from 'interface-blockstore' | ||
|
@@ -98,7 +98,7 @@ export interface HeliaInit<T extends Libp2p = Libp2p> { | |
* A list of strategies used to fetch blocks when they are not present in | ||
* the local blockstore | ||
*/ | ||
blockBrokers?: BlockBroker[] | ||
blockBrokers?: Array<BlockBroker | BlockBrokerFactoryFunction> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we just want to accept |
||
|
||
/** | ||
* Pass `false` to not start the Helia node | ||
|
@@ -159,7 +159,17 @@ export async function createHelia (init: HeliaInit = {}): Promise<Helia<unknown> | |
|
||
const hashers = defaultHashers(init.hashers) | ||
|
||
const blockBrokers = init.blockBrokers ?? [ | ||
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 | ||
Comment on lines
+166
to
+171
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are there other things that blockBrokers may need? |
||
}) ?? [ | ||
new BitswapBlockBroker(libp2p, blockstore, hashers), | ||
new TrustedGatewayBlockBroker(DEFAULT_TRUSTLESS_GATEWAYS) | ||
] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,15 @@ 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/bitswap-block-broker.js' | ||
import { createHelia as createNode } from '../../src/index.js' | ||
import type { Helia } from '@helia/interface' | ||
|
||
export async function createHelia (): Promise<Helia> { | ||
return createNode({ | ||
blockBrokers: [ | ||
BitswapBlockBrokerFactory | ||
], | ||
Comment on lines
10
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should not be making external network requests (i.e. not calling default trustlessGateways) during tests. |
||
libp2p: { | ||
addresses: { | ||
listen: [ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ describe('pins (depth limited)', () => { | |
dag = await createDag(codec, blockstore, MAX_DEPTH, 3) | ||
|
||
helia = await createHelia({ | ||
blockBrokers: [], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prevent blockBroker networking when not needed |
||
datastore: new MemoryDatastore(), | ||
blockstore, | ||
libp2p: await createLibp2p({ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ describe('pins (recursive)', () => { | |
dag = await createDag(codec, blockstore, 2, 3) | ||
|
||
helia = await createHelia({ | ||
blockBrokers: [], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prevent blockBroker networking when not needed |
||
datastore: new MemoryDatastore(), | ||
blockstore, | ||
libp2p: await createLibp2p({ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ import type { BitswapBlockBroker } from '../../src/block-brokers/bitswap-block-b | |
import type { Blockstore } from 'interface-blockstore' | ||
import type { CID } from 'multiformats/cid' | ||
|
||
describe('storage', () => { | ||
describe('networked-storage', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this has the same describe namespace as |
||
let storage: NetworkedStorage | ||
let blockstore: Blockstore | ||
let bitswap: StubbedInstance<BitswapBlockBroker> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,11 +14,12 @@ | |
* ``` | ||
*/ | ||
|
||
import type { Blocks } from './blocks.js' | ||
import type { BlockBroker, 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' | ||
|
@@ -70,3 +71,18 @@ export type GcEvents = | |
export interface GCOptions extends AbortOptions, ProgressOptions<GcEvents> { | ||
|
||
} | ||
export type BlockBrokerFactoryComponents = Pick<Helia, 'libp2p' | 'blockstore' | 'datastore'> & { | ||
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 | ||
} | ||
Comment on lines
+78
to
+88
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seemed like the best place for this but lmk if somewhere else is better. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
needed to ensure it's not using the
Array<BlockBroker | BlockBrokerFactoryFunction>
type fromHeliaInit