-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use functions for block broker creation
This approach is consistent with libp2p components, unixfs, ipns, etc. Since #281 and #284 were merged without review, this PR implements suggsestions that would have been in the review of those PRs. 1. Creation of block brokers is done by exported function. If your broker takes arguments, pass them to the factory function. The factory then returns a function that accepts helia components and returns the broker. 2. Removes BitswapBrokerFactory as it is redunant The internal API may need some more work but the external API should be relatively stable.
- Loading branch information
1 parent
9bad21b
commit 6e44c7d
Showing
15 changed files
with
176 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' |
65 changes: 65 additions & 0 deletions
65
packages/helia/src/block-brokers/trustless-gateway/broker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<TrustlessGatewayGetBlockProgressEvents> | ||
> { | ||
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<ProgressOptions<TrustlessGatewayGetBlockProgressEvents>> = {}): Promise<Uint8Array> { | ||
// 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`) | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
packages/helia/src/block-brokers/trustless-gateway/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<string | URL> | ||
} | ||
|
||
export function trustlessGateway (init: TrustlessGatewayBlockBrokerInit = {}): () => BlockRetriever { | ||
return () => new TrustlessGatewayBlockBroker(init) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.