Skip to content

Commit

Permalink
chore: detect remote ipfs type and adjust accordingly (#3253)
Browse files Browse the repository at this point in the history
* chore: detect remote IPFS type and adjust/warn accordingly
* chore: use network cli flag when starting binary rust ipfs (ceramic-one)
* chore: put rust IPFS defaults back in RustIpfs and export so tests can ignore type signature with glee
* chore: skip test failing (again) with stackoverflow

---------

Co-authored-by: Spencer T Brody <[email protected]>
  • Loading branch information
dav1do and stbrody authored Jul 10, 2024
1 parent d562e4a commit a266978
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 19 deletions.
21 changes: 21 additions & 0 deletions packages/cli/src/ceramic-daemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { DEFAULT_PUBLISH_INTERVAL_MS, NodeMetrics, Observable } from '@ceramicne
import { IpfsConnectionFactory } from './ipfs-connection-factory.js'
import {
DiagnosticsLogger,
EnvironmentUtils,
FieldsIndex,
LoggerProvider,
LogStyle,
Expand Down Expand Up @@ -284,6 +285,15 @@ export class CeramicDaemon {
)
const ipfsId = await ipfs.id()

// we need to change the values before processing the config, so we keep track to log the warning after we have logging set up
const desiredIpfsClient = EnvironmentUtils.useRustCeramic() ? 'ceramic-one' : 'kubo'
const wrongIpfsClient =
opts.ipfs.mode == IpfsMode.REMOTE &&
EnvironmentUtils.useRustCeramic() !== ipfsId.agentVersion.includes('ceramic-one')
if (wrongIpfsClient) {
EnvironmentUtils.setIpfsFlavor(EnvironmentUtils.useRustCeramic() ? 'go' : 'rust')
}

const versionInfo = {
cliPackageVersion: version,
gitHash: commitHash,
Expand All @@ -303,6 +313,17 @@ export class CeramicDaemon {
.map(String)
.join(', ')}`
)
if (wrongIpfsClient) {
if (desiredIpfsClient === 'ceramic-one') {
diagnosticsLogger.warn(
`Detected ipfs kubo running at ${ipfsId.addresses} with agent version ${ipfsId.agentVersion}. Using kubo for p2p networking is deprecated - we recommend migrating to ceramic-one instead.`
)
} else {
diagnosticsLogger.warn(
`Ignoring configuration IPFS_FLAVOR='go'. Detected remote ceramic-one running at ${ipfsId.addresses} with agent version ${ipfsId.agentVersion}. If you intended to use ipfs, specify an --ipfs-url for an ipfs node.`
)
}
}

const ceramic = new Ceramic(modules, params)
let didOptions: DIDOptions = { resolver: makeResolvers(ceramic) }
Expand Down
13 changes: 11 additions & 2 deletions packages/cli/src/ipfs-connection-factory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import mergeOpts from 'merge-options'
import * as ipfsClient from 'ipfs-http-client'
import { DiagnosticsLogger, IpfsApi } from '@ceramicnetwork/common'
import { DiagnosticsLogger, EnvironmentUtils, IpfsApi, Networks } from '@ceramicnetwork/common'
import { IpfsMode } from './daemon-config.js'
import * as http from 'http'
import * as https from 'https'
Expand Down Expand Up @@ -34,7 +34,16 @@ export class IpfsConnectionFactory {

return ipfsApi
} else {
return this.createGoIPFS()
if (EnvironmentUtils.useRustCeramic()) {
return ipfs.createIPFS(
{
rust: ipfs.RustIpfs.defaultOptions(network),
},
false
)
} else {
return this.createGoIPFS()
}
}
}

Expand Down
16 changes: 15 additions & 1 deletion packages/common/src/utils/environment-utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// use a file local instead of setting environment variables from node
let IPFS_FLAVOR = process.env.IPFS_FLAVOR || 'rust'
/**
* Environment related utils, that is information about the mode and system we're operating in.
*/
Expand All @@ -6,6 +8,18 @@ export class EnvironmentUtils {
* Returns whether or not we're running using rust-ceramic
*/
static useRustCeramic(): boolean {
return process.env.IPFS_FLAVOR !== 'go'
switch (IPFS_FLAVOR) {
case 'go':
return false
default:
return true
}
}

/**
* Changes the server from expecting rust or go ipfs to the other
*/
static setIpfsFlavor(flavor: 'go' | 'rust') {
IPFS_FLAVOR = flavor
}
}
3 changes: 2 additions & 1 deletion packages/core/src/__tests__/ceramic-feed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ describe('Ceramic feed', () => {
await doneStreaming
})

test('add entry after anchoring stream', async () => {
// TODO(dav1do): This test is occasionally failing with a stackoverflow and needs investigation
test.skip('add entry after anchoring stream', async () => {
const emissions: Array<FeedDocument> = []
const readable1 = ceramic1.feed.aggregation.documents()
const writable1 = new WritableStream({
Expand Down
24 changes: 12 additions & 12 deletions packages/core/src/ceramic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,10 +500,16 @@ export class Ceramic implements StreamReaderWriter, StreamStateLoader {
*/
async _init(doPeerDiscovery: boolean): Promise<void> {
try {
this._logger.imp(
`Connecting to ceramic network '${this._networkOptions.name}' using pubsub topic '${this._networkOptions.pubsubTopic}'`
)

if (EnvironmentUtils.useRustCeramic()) {
// this is potentially incorrect if we're running in remote mode as we don't control the network and could mismatch with c1
this._logger.imp(
`Connecting to ceramic network '${this._networkOptions.name}' using ceramic-one with Recon for data synchronization.`
)
} else {
this._logger.imp(
`Connecting to ceramic network '${this._networkOptions.name}' using pubsub topic '${this._networkOptions.pubsubTopic}'`
)
}
if (this._readOnly) {
this._logger.warn(`Starting in read-only mode. All write operations will fail`)
}
Expand Down Expand Up @@ -568,16 +574,10 @@ export class Ceramic implements StreamReaderWriter, StreamStateLoader {
)
}

if (EnvironmentUtils.useRustCeramic()) {
if (!EnvironmentUtils.useRustCeramic() && !this.dispatcher.enableSync) {
this._logger.warn(
`Running Ceramic using rust-ceramic. If you want to use Ipfs, run with IPFS_FLAVOR=go.`
`IPFS peer data sync is disabled. This node will be unable to load data from any other Ceramic nodes on the network`
)
} else {
if (!this.dispatcher.enableSync) {
this._logger.warn(
`IPFS peer data sync is disabled. This node will be unable to load data from any other Ceramic nodes on the network`
)
}
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/ipfs-daemon/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './create-ipfs.js'
export * from './ipfs-daemon.js'
export * from './healthcheck-server.js'
export * from './rust-ipfs.js'
6 changes: 3 additions & 3 deletions packages/ipfs-daemon/src/rust-ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,22 +195,22 @@ export class RustIpfs {

constructor(opts: RustIpfsOptions) {
let options = opts
if (!opts || Object.keys(opts).length === 0) {
if (!options || Object.keys(options).length === 0) {
options = RustIpfs.defaultOptions()
}

this.opts = options
}

static defaultOptions(): RustIpfsOptions {
static defaultOptions(desiredNetwork?: string): RustIpfsOptions {
const path = process.env.CERAMIC_ONE_PATH
if (!path) {
throw new Error(
'Missing rust ceramic binary path. Set CERAMIC_ONE_PATH=/path/to/binary. For example: `CERAMIC_ONE_PATH=/usr/local/bin/ceramic-one`.'
)
}
let network
switch (process.env.CERAMIC_ONE_NETWORK) {
switch (desiredNetwork || process.env.CERAMIC_ONE_NETWORK) {
case 'mainnet':
network = Networks.MAINNET
break
Expand Down

0 comments on commit a266978

Please sign in to comment.