Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

block.put ignores CID argument #815

Closed
miguelmota opened this issue Jul 17, 2018 · 8 comments · Fixed by #844
Closed

block.put ignores CID argument #815

miguelmota opened this issue Jul 17, 2018 · 8 comments · Fixed by #844

Comments

@miguelmota
Copy link

miguelmota commented Jul 17, 2018

Hello, I'm having an issue where block.get is hanging and never returns the block.

Here's the example code:

const ipfsAPI = require('ipfs-api')
const CID = require('cids')
const multihashes = require('multihashes')
const crypto = require('crypto')

function sha256(data) {
  return crypto.createHash('sha256').update(data).digest()
}

const ipfs = ipfsAPI('localhost', '5001', {protocol: 'http'})
const buf = sha256('hello world!')
const mh = multihashes.encode(buf, 'sha2-256')
const cid = new CID(1, 'dag-cbor', mh)

console.log('cid', cid.toBaseEncodedString())

ipfs.repo.gc()
.then(() => {
  return ipfs.block.put(buf, cid)
  .then(block => {
    console.log('put', block.cid.toBaseEncodedString())

    return ipfs.block.get(cid)
    .then(block => {
      // Never gets called!
      console.log('get', block._data.toString())
    })
    .catch(error => console.error(error))
  })
})

Output

cid zdpuAtJJwFer98hHGdt2UQdoF1We7vQuBXSTDKjorNkYovyzQ
put QmPdbXGenToPFzmxJyoVNAAfh5eKvnu2f8KzSx2J8jhwQP

It works using the CLI though:

$ ipfs repo gc

$ echo -n 'hello world!' | ipfs block put -f cbor
zdpuAtJJwFer98hHGdt2UQdoF1We7vQuBXSTDKjorNkYovyzQ

$ ipfs block get zdpuAtJJwFer98hHGdt2UQdoF1We7vQuBXSTDKjorNkYovyzQ
hello world!

Any help is appreciated, thanks.

@miguelmota
Copy link
Author

It looks like the CID is being ignored in https://github.com/ipfs/js-ipfs-api/blob/master/src/block/put.js

@alanshaw
Copy link
Contributor

It looks like the CID is being ignored

Yes and because of that, the block is being stored under a version 0 CID so you can't get it back using your version 1 CID - which explains the hanging.

@alanshaw
Copy link
Contributor

Also, according to the spec the second arg should be an object with a cid property, not the CID itself.

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

@alanshaw alanshaw changed the title block.get hangs block.put ignores CID argument Jul 18, 2018
@neodon
Copy link

neodon commented Jul 19, 2018

I'd like to give this a try. New here, not sure what the process is for asking to be assigned to an issue.

@alanshaw
Copy link
Contributor

@neodon that would be awesome, please send a PR! Would you mind also adding a test to https://github.com/ipfs/interface-ipfs-core/blob/master/js/src/block/put.js for this?

You need to use npm link to run the test you add against this repo. You should be able to tweak https://github.com/ipfs/js-ipfs-api/blob/master/test/interface.spec.js to run only the test you've added while you're developing.

@alanshaw
Copy link
Contributor

alanshaw commented Aug 6, 2018

@neodon did you get anywhere with this?

@neodon
Copy link

neodon commented Aug 7, 2018

@alanshaw I've spent quite a bit of time digging through the layers in js-ipfs-api and js-ipfs. This last weekend I spent some time going through the go-ipfs implementation to get more context. Any hints or insight would be helpful. I can't seem to determine that js-ipfs actually supports setting a custom cid.

@ashah917
Copy link

I took a look -- is the issue just that the CID is not being properly passed in the call of sendOneFile on line 29? Also the need to handle the different possible types of CID objects being passed into the function

@ghost ghost removed the ready label Sep 20, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants