Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

docs: how do you create an in-memory offline ipfs node for testing? #2751

Closed
mikeal opened this issue Feb 7, 2020 · 7 comments · Fixed by #2760
Closed

docs: how do you create an in-memory offline ipfs node for testing? #2751

mikeal opened this issue Feb 7, 2020 · 7 comments · Fixed by #2760
Labels
exp/novice Someone with a little familiarity can pick up kind/bug A bug in existing code (including security flaws) P1 High: Likely tackled by core team if no one steps up status/ready Ready to be worked

Comments

@mikeal
Copy link
Contributor

mikeal commented Feb 7, 2020

I’ve been trying to create a purely in-memory offline ipfs node and it doesn’t appear to be working. The node appears to function but blocks I’ve just added aren’t actually available. https://github.com/mikeal/ipfs-for-car/blob/master/index.js#L1-L31

@mikeal mikeal changed the title docs: how do you create an in-memory offline ipfs node for testing docs: how do you create an in-memory offline ipfs node for testing? Feb 7, 2020
@alanshaw
Copy link
Member

alanshaw commented Feb 7, 2020

If key is a CID you need to pass it as cid in the options object.

await ipfs.block.put(value, { cid: key })

https://github.com/ipfs/interface-js-ipfs-core/blob/master/SPEC/BLOCK.md#blockput

@mikeal
Copy link
Contributor Author

mikeal commented Feb 7, 2020

Thanks, I’m sure that helps but it’s still not working.

https://github.com/mikeal/ipfs-for-car/blob/master/index.js

I get Not Found when trying to pull out a block I’ve just added. I’m assuming something is wrong with how I’m configuring the inmemory storage because I doubt basic block storage is broken 🤷‍♂️

first bafyreiec5aigscqhrvctqugyrox5hsjyar624aegy3ooq75usntmffz2hq
(node:4997) UnhandledPromiseRejectionWarning: Error: Not Found
    at Object.module.exports.notFoundError (/root/ipfs-for-car/node_modules/interface-datastore/src/errors.js:21:
16)
    at MemoryDatastore.get (/root/ipfs-for-car/node_modules/interface-datastore/src/memory.js:22:31)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async Object.get (/root/ipfs-for-car/node_modules/ipfs-repo/src/blockstore.js:49:21)
(node:4997) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwi
ng inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch
(). (rejection id: 1)
(node:4997) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

@alanshaw
Copy link
Member

alanshaw commented Feb 8, 2020

Can you share the car file and I can take a look?

@alanshaw
Copy link
Member

alanshaw commented Feb 8, 2020

This is a bug in block.put, we're checking for a CID instance when we should also be accepting a CID in string/buffer form.

You can (hopefully) work around this for now by doing:

await ipfs.block.put(value, { cid: new CID(key) })

@alanshaw alanshaw added kind/bug A bug in existing code (including security flaws) exp/novice Someone with a little familiarity can pick up P1 High: Likely tackled by core team if no one steps up status/ready Ready to be worked labels Feb 8, 2020
@mikeal
Copy link
Contributor Author

mikeal commented Feb 11, 2020

thanks! :) it works with this workaround now.

@buu700
Copy link

buu700 commented Aug 15, 2021

It took some trial and error (and poking around in the js-ipfs-repo code; the docs aren't up to date with the latest API), but this did the job for me:

const {MemoryDatastore} = require('interface-datastore');
const IPFS = require('ipfs');
const {
	createRepo,
	locks: {memory: memoryLock}
} = require('ipfs-repo');
const rawCodec = require('multiformats/codecs/raw');

const repo = createRepo(
	'',
	() => rawCodec,
	{
		blocks: new MemoryDatastore(),
		datastore: new MemoryDatastore(),
		keys: new MemoryDatastore(),
		pins: new MemoryDatastore(),
		root: new MemoryDatastore()
	},
	{autoMigrate: false, repoLock: memoryLock, repoOwner: true}
);

const ipfs = await IPFS.create({repo});

@pimterry
Copy link

Just to update this a little, using IPFS v0.60.2 with TypeScript, to get the above working I needed to change it to:

import * as IPFS from 'ipfs';
import { MemoryDatastore } from 'datastore-core';
import { MemoryBlockstore } from 'blockstore-core';

import { createRepo } from 'ipfs-repo';
import { MemoryLock } from 'ipfs-repo/locks/memory'
import * as rawCodec from 'multiformats/codecs/raw';

const repo = createRepo(
	'',
	async () => rawCodec,
	{
		blocks: new MemoryBlockstore(),
		datastore: new MemoryDatastore(),
		keys: new MemoryDatastore(),
		pins: new MemoryDatastore(),
		root: new MemoryDatastore()
	},
	{ autoMigrate: false, repoLock: MemoryLock, repoOwner: true }
);

const ipfs = await IPFS.create({ repo });

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
exp/novice Someone with a little familiarity can pick up kind/bug A bug in existing code (including security flaws) P1 High: Likely tackled by core team if no one steps up status/ready Ready to be worked
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants