Skip to content

Commit

Permalink
deps!: updates to libp2p v1 (#320)
Browse files Browse the repository at this point in the history
Updates all deps to libp2p v1.

BREAKING CHANGE: The libp2p API has changed in a couple of places - please see the [upgrade
guide](https://github.com/libp2p/js-libp2p/blob/main/doc/migrations/v0.46-v1.0.0.md)
  • Loading branch information
achingbrain authored Dec 5, 2023
1 parent 55a64c6 commit 635d7a2
Show file tree
Hide file tree
Showing 23 changed files with 266 additions and 153 deletions.
9 changes: 6 additions & 3 deletions packages/helia/.aegir.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { circuitRelayServer } from 'libp2p/circuit-relay'
import { identifyService } from 'libp2p/identify'
import { circuitRelayServer } from '@libp2p/circuit-relay-v2'
import { identify } from '@libp2p/identify'
import { WebSockets } from '@multiformats/mafmt'
import { CID } from 'multiformats/cid'
import { sha256 } from 'multiformats/hashes/sha2'
Expand All @@ -23,8 +23,11 @@ const options = {
`/ip4/127.0.0.1/tcp/0/ws`
]
},
connectionManager: {
inboundConnectionThreshold: Infinity
},
services: {
identify: identifyService(),
identify: identify(),
relay: circuitRelayServer({
reservations: {
maxReservations: Infinity,
Expand Down
39 changes: 23 additions & 16 deletions packages/helia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,38 +78,44 @@
"prepublishOnly": "node scripts/update-version.js && npm run build"
},
"dependencies": {
"@chainsafe/libp2p-gossipsub": "^10.0.0",
"@chainsafe/libp2p-noise": "^13.0.0",
"@chainsafe/libp2p-yamux": "^5.0.0",
"@chainsafe/libp2p-gossipsub": "^11.0.0",
"@chainsafe/libp2p-noise": "^14.0.0",
"@chainsafe/libp2p-yamux": "^6.0.1",
"@helia/delegated-routing-v1-http-api-client": "^1.1.0",
"@helia/interface": "^2.1.0",
"@ipld/dag-cbor": "^9.0.0",
"@ipld/dag-json": "^10.0.1",
"@ipld/dag-pb": "^4.0.3",
"@libp2p/bootstrap": "^9.0.2",
"@libp2p/interface": "^0.1.1",
"@libp2p/kad-dht": "^10.0.2",
"@libp2p/logger": "^3.0.1",
"@libp2p/mdns": "^9.0.2",
"@libp2p/mplex": "^9.0.2",
"@libp2p/tcp": "^8.0.2",
"@libp2p/webrtc": "^3.1.3",
"@libp2p/websockets": "^7.0.2",
"@libp2p/webtransport": "^3.0.3",
"@libp2p/autonat": "^1.0.1",
"@libp2p/bootstrap": "^10.0.2",
"@libp2p/circuit-relay-v2": "^1.0.2",
"@libp2p/dcutr": "^1.0.1",
"@libp2p/identify": "^1.0.1",
"@libp2p/interface": "^1.0.1",
"@libp2p/kad-dht": "^11.0.2",
"@libp2p/keychain": "^4.0.2",
"@libp2p/mdns": "^10.0.2",
"@libp2p/mplex": "^10.0.2",
"@libp2p/ping": "^1.0.1",
"@libp2p/tcp": "^9.0.2",
"@libp2p/upnp-nat": "^1.0.1",
"@libp2p/webrtc": "^4.0.3",
"@libp2p/websockets": "^8.0.2",
"@libp2p/webtransport": "^4.0.3",
"any-signal": "^4.1.1",
"blockstore-core": "^4.0.0",
"cborg": "^4.0.1",
"datastore-core": "^9.0.0",
"interface-blockstore": "^5.0.0",
"interface-datastore": "^8.0.0",
"interface-store": "^5.0.1",
"ipfs-bitswap": "^19.0.0",
"ipfs-bitswap": "^20.0.0",
"ipns": "^7.0.1",
"it-all": "^3.0.2",
"it-drain": "^3.0.1",
"it-filter": "^3.0.1",
"it-foreach": "^2.0.2",
"libp2p": "^0.46.6",
"libp2p": "^1.0.3",
"mortice": "^3.0.1",
"multiformats": "^12.0.1",
"p-defer": "^4.0.0",
Expand All @@ -118,9 +124,10 @@
"uint8arrays": "^4.0.3"
},
"devDependencies": {
"@libp2p/logger": "^4.0.1",
"@multiformats/mafmt": "^12.1.5",
"@multiformats/multiaddr": "^12.1.7",
"@types/sinon": "^10.0.14",
"@types/sinon": "^17.0.2",
"aegir": "^41.0.0",
"delay": "^6.0.0",
"sinon": "^17.0.0",
Expand Down
3 changes: 1 addition & 2 deletions packages/helia/src/block-brokers/bitswap.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { createBitswap } from 'ipfs-bitswap'
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 { Libp2p, Startable } from '@libp2p/interface'
import type { Blockstore } from 'interface-blockstore'
import type { Bitswap, BitswapNotifyProgressEvents, BitswapOptions, BitswapWantBlockProgressEvents } from 'ipfs-bitswap'
import type { CID } from 'multiformats/cid'
Expand Down
20 changes: 10 additions & 10 deletions packages/helia/src/block-brokers/trustless-gateway/broker.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
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 { TrustlessGatewayBlockBrokerInit, TrustlessGatewayComponents, TrustlessGatewayGetBlockProgressEvents } from './index.js'
import type { BlockRetrievalOptions, BlockRetriever } from '@helia/interface/blocks'
import type { Logger } from '@libp2p/interface'
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.
Expand All @@ -16,8 +14,10 @@ export class TrustlessGatewayBlockBroker implements BlockRetriever<
ProgressOptions<TrustlessGatewayGetBlockProgressEvents>
> {
private readonly gateways: TrustlessGateway[]
private readonly log: Logger

constructor (init: TrustlessGatewayBlockBrokerInit = {}) {
constructor (components: TrustlessGatewayComponents, init: TrustlessGatewayBlockBrokerInit = {}) {
this.log = components.logger.forComponent('helia:trustless-gateway-block-broker')
this.gateways = (init.gateways ?? DEFAULT_TRUSTLESS_GATEWAYS)
.map((gatewayOrUrl) => {
return new TrustlessGateway(gatewayOrUrl)
Expand All @@ -31,30 +31,30 @@ ProgressOptions<TrustlessGatewayGetBlockProgressEvents>
const aggregateErrors: Error[] = []

for (const gateway of sortedGateways) {
log('getting block for %c from %s', cid, gateway.url)
this.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)
this.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)
this.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)
this.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)
this.log.trace('request aborted while fetching raw block for CID %c from gateway %s', cid, gateway.url)
break
}
}
Expand Down
9 changes: 7 additions & 2 deletions packages/helia/src/block-brokers/trustless-gateway/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { TrustlessGatewayBlockBroker } from './broker.js'
import type { BlockRetriever } from '@helia/interface/src/blocks.js'
import type { ComponentLogger } from '@libp2p/interface'
import type { ProgressEvent } from 'progress-events'

export const DEFAULT_TRUSTLESS_GATEWAYS = [
Expand All @@ -23,6 +24,10 @@ export interface TrustlessGatewayBlockBrokerInit {
gateways?: Array<string | URL>
}

export function trustlessGateway (init: TrustlessGatewayBlockBrokerInit = {}): () => BlockRetriever {
return () => new TrustlessGatewayBlockBroker(init)
export interface TrustlessGatewayComponents {
logger: ComponentLogger
}

export function trustlessGateway (init: TrustlessGatewayBlockBrokerInit = {}): (components: TrustlessGatewayComponents) => BlockRetriever {
return (components) => new TrustlessGatewayBlockBroker(components, init)
}
24 changes: 13 additions & 11 deletions packages/helia/src/helia.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { start, stop } from '@libp2p/interface/startable'
import { logger } from '@libp2p/logger'
import { start, stop } from '@libp2p/interface'
import drain from 'it-drain'
import { CustomProgressEvent } from 'progress-events'
import { bitswap, trustlessGateway } from './block-brokers/index.js'
Expand All @@ -11,13 +10,11 @@ import { NetworkedStorage } from './utils/networked-storage.js'
import type { HeliaInit } from '.'
import type { GCOptions, Helia } from '@helia/interface'
import type { Pins } from '@helia/interface/pins'
import type { Libp2p } from '@libp2p/interface'
import type { ComponentLogger, Libp2p, Logger } from '@libp2p/interface'
import type { Blockstore } from 'interface-blockstore'
import type { Datastore } from 'interface-datastore'
import type { CID } from 'multiformats/cid'

const log = logger('helia')

interface HeliaImplInit<T extends Libp2p = Libp2p> extends HeliaInit<T> {
libp2p: T
blockstore: Blockstore
Expand All @@ -29,25 +26,30 @@ export class HeliaImpl implements Helia {
public blockstore: BlockStorage
public datastore: Datastore
public pins: Pins
public logger: ComponentLogger
private readonly log: Logger

constructor (init: HeliaImplInit) {
this.logger = init.libp2p.logger
this.log = this.logger.forComponent('helia')
const hashers = defaultHashers(init.hashers)

const components = {
blockstore: init.blockstore,
datastore: init.datastore,
libp2p: init.libp2p,
hashers
hashers,
logger: init.libp2p.logger
}

const blockBrokers = init.blockBrokers?.map((fn) => {
return fn(components)
}) ?? [
bitswap()(components),
trustlessGateway()()
trustlessGateway()(components)
]

const networkedStorage = new NetworkedStorage(init.blockstore, {
const networkedStorage = new NetworkedStorage(components, {
blockBrokers,
hashers
})
Expand Down Expand Up @@ -79,7 +81,7 @@ export class HeliaImpl implements Helia {
const helia = this
const blockstore = this.blockstore.unwrap()

log('gc start')
this.log('gc start')

await drain(blockstore.deleteMany((async function * (): AsyncGenerator<CID> {
for await (const { cid } of blockstore.getAll()) {
Expand All @@ -92,7 +94,7 @@ export class HeliaImpl implements Helia {

options.onProgress?.(new CustomProgressEvent<CID>('helia:gc:deleted', cid))
} catch (err) {
log.error('Error during gc', err)
helia.log.error('Error during gc', err)
options.onProgress?.(new CustomProgressEvent<Error>('helia:gc:error', err))
}
}
Expand All @@ -101,6 +103,6 @@ export class HeliaImpl implements Helia {
releaseLock()
}

log('gc finished')
this.log('gc finished')
}
}
48 changes: 21 additions & 27 deletions packages/helia/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@
* ```
*/

import { logger } from '@libp2p/logger'
import { MemoryBlockstore } from 'blockstore-core'
import { MemoryDatastore } from 'datastore-core'
import { HeliaImpl } from './helia.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 { BlockBroker } from '@helia/interface/blocks'
import type { Libp2p } from '@libp2p/interface'
import type { ComponentLogger, Libp2p } from '@libp2p/interface'
import type { KeychainInit } from '@libp2p/keychain'
import type { Blockstore } from 'interface-blockstore'
import type { Datastore } from 'interface-datastore'
import type { Libp2pOptions } from 'libp2p'
Expand All @@ -41,8 +40,6 @@ export * from '@helia/interface'
export * from '@helia/interface/blocks'
export * from '@helia/interface/pins'

const log = logger('helia')

/**
* DAGWalkers take a block and yield CIDs encoded in that block
*/
Expand Down Expand Up @@ -117,6 +114,18 @@ export interface HeliaInit<T extends Libp2p = Libp2p> {
* webworker), pass true here to hold the gc lock in this process.
*/
holdGcLock?: boolean

/**
* An optional logging component to pass to libp2p. If not specified the
* default implementation from libp2p will be used.
*/
logger?: ComponentLogger

/**
* By default Helia stores the node's PeerId in an encrypted form in a
* libp2p keystore. These options control how that keystore is configured.
*/
keychain?: KeychainInit
}

/**
Expand All @@ -133,23 +142,24 @@ export async function createHelia (init: HeliaInit = {}): Promise<Helia<unknown>
if (isLibp2p(init.libp2p)) {
libp2p = init.libp2p
} else {
libp2p = await createLibp2p(datastore, init.libp2p)
libp2p = await createLibp2p({
...init,
libp2p: init.libp2p,
datastore
})
}

const helia = new HeliaImpl({
...init,
libp2p,
datastore,
blockstore,
libp2p
blockstore
})

if (init.start !== false) {
await helia.start()
}

// add helia to agent version
addHeliaToAgentVersion(helia)

return helia
}

Expand All @@ -164,19 +174,3 @@ function isLibp2p (obj: any): obj is Libp2p {
// if these are all functions it's probably a libp2p object
return funcs.every(m => typeof obj[m] === 'function')
}

function addHeliaToAgentVersion (helia: Helia<any>): void {
// add helia to agent version
try {
const existingAgentVersion = helia.libp2p.services.identify.host.agentVersion

if (existingAgentVersion.match(/js-libp2p\/\d+\.\d+\.\d+\sUserAgent=/) == null) {
// the user changed the agent version
return
}

helia.libp2p.services.identify.host.agentVersion = `${name}/${version} ${helia.libp2p.services.identify.host.agentVersion}`
} catch (err) {
log.error('could not add Helia to agent version', err)
}
}
4 changes: 2 additions & 2 deletions packages/helia/src/storage.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { start, stop, type Startable } from '@libp2p/interface/startable'
import { start, stop } from '@libp2p/interface'
import createMortice from 'mortice'
import type { Blocks, Pair, DeleteManyBlocksProgressEvents, DeleteBlockProgressEvents, GetBlockProgressEvents, GetManyBlocksProgressEvents, PutManyBlocksProgressEvents, PutBlockProgressEvents, GetAllBlocksProgressEvents, GetOfflineOptions } from '@helia/interface/blocks'
import type { Pins } from '@helia/interface/pins'
import type { AbortOptions } from '@libp2p/interface'
import type { AbortOptions, Startable } from '@libp2p/interface'
import type { Blockstore } from 'interface-blockstore'
import type { AwaitIterable } from 'interface-store'
import type { Mortice } from 'mortice'
Expand Down
Loading

0 comments on commit 635d7a2

Please sign in to comment.