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

files.cat(cid.buffer) throws error: multihash length inconsistent #1247

Closed
realharry opened this issue Mar 5, 2018 · 7 comments
Closed

files.cat(cid.buffer) throws error: multihash length inconsistent #1247

realharry opened this issue Mar 5, 2018 · 7 comments
Labels
exp/novice Someone with a little familiarity can pick up help wanted Seeking public contribution on this issue kind/bug A bug in existing code (including security flaws) P1 High: Likely tackled by core team if no one steps up

Comments

@realharry
Copy link

  • Version: 0.28.0
  • Platform: Linux 4.13.0-36-generic update roadmap #40-Ubuntu SMP Fri Feb 16 20:07:48 UTC 2018 x86_64 x86_64 x86_64 GNU/Linu
  • Subsystem: Node.js v6.11.5

Type: Bug

Severity: Medium

Description:

Not sure if I'm using the API correctly, but this is what I do:

(1) Create a buffer from a string content.
(2) Add a file using the buffer to an IPFS node.
(3) Create a CID with the buffer content (with version==1).
(4) Fetch the content using the API, files.cat(cid.buffer).

It throws the following exception:

Error: multihash length inconsistent: 0x122017babbbb5ec8d6ec709a5fd1b559d0126cf36486145f84f9c39260ae0d87ab7e
    at Object.decode (/home/harry/js-ipfs/node_modules/multihashes/src/index.js:99:11)
    at Object.validate (/home/harry/js-ipfs/node_modules/multihashes/src/index.js:210:11)
    at Function.validateCID (/home/harry/js-ipfs/node_modules/cids/src/index.js:254:8)
    at new CID (/home/harry/js-ipfs/node_modules/cids/src/index.js:104:9)
    at pathBaseAndRest (/home/harry/js-ipfs/node_modules/ipfs-unixfs-engine/src/exporter/index.js:30:15)
    at module.exports.err (/home/harry/js-ipfs/node_modules/ipfs-unixfs-engine/src/exporter/index.js:47:13)
    at _catPullStream (/home/harry/js-ipfs/src/core/components/files.js:142:7)
    at Function.cat.promisify (/home/harry/js-ipfs/src/core/components/files.js:234:9)
    at Object.cat (/home/harry/js-ipfs/node_modules/promisify-es6/index.js:32:27)
    at Timeout.setTimeout (/home/harry/js-ipfs/test/core/files-cat.spec.js:82:18)
    at ontimeout (timers.js:386:11)
    at tryOnTimeout (timers.js:250:5)
    at Timer.listOnTimeout (timers.js:214:5)

Steps to reproduce the error:

Code snippets:

  let buffer = Buffer.from(content);
  let mh = multihashing(buffer, 'sha2-256')
  let cid = new CID(1, 'dag-cbor', mh)
  ipfs.files.add(buffer, {}, (err, filesAdded) => {
    path = filesAdded[0].path;
    console.log("Created: path = " + path)
  })
  // ...
  ipfs.files.cat(cid.buffer, (err, data) => {
    console.log(err)
    console.log(data)
  })
@realharry
Copy link
Author

Just to be clear, files.cat() works in my sample/test code if I pass in the path (or, multihash) param.

@realharry
Copy link
Author

I cannot files.get() or files.cat() with ipfsPath = cid.toBaseEncodedString() either. They do not throw an error, but these methods do not return.

@daviddias
Copy link
Member

Same issue as #1229

@daviddias daviddias added kind/bug A bug in existing code (including security flaws) exp/novice Someone with a little familiarity can pick up help wanted Seeking public contribution on this issue P1 High: Likely tackled by core team if no one steps up status/ready Ready to be worked labels Mar 6, 2018
@brockmiller
Copy link

Hi @diasdavid, I started looking into this issue (along with #1229, which you stated was the same issue), and I'm a little stumped on why they are actually the same issue?

According to this bug report, it looks like he is in fact correctly supplying a CID of type Buffer (cid.buffer), and the issue seems to be due to inconsistent hash length. My best guess is that there is some inconsistency or misuse in how he is generating his multihash and CID (steps 1-4 in the description).

However, on #1229, the issue is slightly different in that he is actually supplying a formal CID object, which the normalizePath function does not handle. The resolution to that problem seems to just require an enhancement of the normalizePath method so that it can convert the CID into a b58 string.

I'm looking to tackle one of these issues as my first contribution to the project, so I just want to make sure I'm looking at these issues correctly. Thoughts? Thanks!

@mitra42
Copy link

mitra42 commented Mar 17, 2018

@brockmiller - that's correct about #1229 - the issue was that we were previously supplying a cid object to the function and it worked, and that stopped working. I think I've caught all the instances in my code, but I'm guessing other people's (previously working) code might also break.

@vasco-santos
Copy link
Member

I analyzed this issue, but I could not reach a successful case.

At first, the mentioned error: multihash length inconsistent: 0x122017babbbb5ec8d6ec709a5fd1b559d0126cf36486145f84f9c39260ae0d87ab7e

This occurs because of being used cidVersion = 1 for cat, while the default version is 0.

Using the following code:

    const content = 'Multiaddrs are cool.'
    const buffer = api.Buffer.from(content)

    ipfs.files.add(buffer, {}, (err, filesAdded) => {
      const path = filesAdded[0].path
      const cidCorrect = new node.types.CID(path)

      console.log('cid', cidCorrect, cidCorrect.buffer);

    })

If we analyze the CID obtained using the received path, besides version 1 being used, it is also being used dag-pb, instead of dag-cbor.

Despite all this, I still trying to understand one problem, which happens in this case.

const buffer = api.Buffer.from(content)
const mh = multihashing(buffer, 'sha2-256')
const cid = new node.types.CID(0, 'dag-pb', mh)

The obtained CID is different than the obtained previously on cidCorrect. Basically, the multihash value is different from the used in the ipfs.files.add. As a consequence, an empty response is received in the ipfs.files.cat. If I use cat with the cidCorrect or cidCorrect.buffer, the 'Multiaddrs are cool.' string is received

@alanshaw
Copy link
Member

Closing this as I believe it has been resolved. I can see that ipfs.cat supports buffers as the normalizePath function detects them here https://github.com/ipfs/js-ipfs/blob/master/src/core/components/files-regular/utils.js#L6 Please open an new issue if this is seen again! Thank you.

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 help wanted Seeking public contribution on this issue kind/bug A bug in existing code (including security flaws) P1 High: Likely tackled by core team if no one steps up
Projects
None yet
Development

No branches or pull requests

6 participants