ipfs / js-ipfs

IPFS implementation in JavaScript
https://js.ipfs.tech
Other
7.44k stars 1.25k forks source link

Transfering DAG object between two computers #881

Closed negamaxi closed 6 years ago

negamaxi commented 7 years ago

I have two laptops, both running js-ipfs nodes. On the first one I put javascript object with:

const obj = { a: 'b' }
const cid = await node.dag.put(obj)

Then I convert cid toBaseEncodedString(), send via messenger to my second laptop and trying to get content with:

const data = await node.dag.get(cidString)
console.log(data.value)

...but console.log never happens and no errors. Multiaddr of my first laptop appears in node.swarm.addrs() of the second one like that

[ <Multiaddr 04c0a86406060fa2 - /ip4/192.168.100.6/tcp/4002> ]

what I believe means both nodes see each other. Any advice on how to make it work? Thanks!

daviddias commented 7 years ago

That's a strange behavior. I see you are using async/await, I assume you have wrapped the API to expose that?

Check the bitswap wantlist. node.bitswap.wantlist() to see if the cid is present.

negamaxi commented 7 years ago

This code I run on first laptop:

const IPFS = require('ipfs')
const DAGOptions = { format: 'dag-cbor', hashAlg: 'sha2-256' }

const node = new IPFS()
node.on('ready', async () => {
  const cid = await node.dag.put({ a: 'b' }, DAGOptions)
  console.log(cid.toBaseEncodedString())
  const { value } = await node.dag.get(cid)
  console.log(value)
  let knownPeers = []
  setInterval(async () => {
    const addrs = await node.swarm.addrs()
    const newPeers = []
    addrs.forEach((addr) => {
      const id = addr.id.toB58String()
      if (!knownPeers.includes(id)) {
        newPeers.push(id)
      }
    })
    if (newPeers.length > 0) {
      console.log(newPeers)
      knownPeers = [...knownPeers, ...newPeers]
    }
  }, 1000)
})
console output

``` WARNING: Your platform does not have native WebRTC support, it won' use any WebRTC transport Swarm listening on /ip4/127.0.0.1/tcp/4003/ws/ipfs/QmeQQdVjvKdo3Q3v2SincZhghnqUGckSF23r9ajxsq4LrJ Swarm listening on /ip4/127.0.0.1/tcp/4002/ipfs/QmeQQdVjvKdo3Q3v2SincZhghnqUGckSF23r9ajxsq4LrJ Swarm listening on /ip4/192.168.100.7/tcp/4002/ipfs/QmeQQdVjvKdo3Q3v2SincZhghnqUGckSF23r9ajxsq4LrJ zdpuAw6c3ViCx5A7K4nukvmxh3ZtTHnyknmxCLQmGa6JDmDMn { a: 'b' } [ 'QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ', 'QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z', 'QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM', 'QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm', 'QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu', 'QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64', 'QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd', 'QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3', 'QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx' ] [ 'Qmc9bVU9yQ38C7LaeJMZ9o36tXuHVJWNY2mGZG4w8wzJBE' ] [ 'QmeQQdVjvKdo3Q3v2SincZhghnqUGckSF23r9ajxsq4LrJ' ] ```


This code I run on second laptop:

const IPFS = require('ipfs')
const node = new IPFS()

node.on('ready', async () => {
  node.dag.get('zdpuAw6c3ViCx5A7K4nukvmxh3ZtTHnyknmxCLQmGa6JDmDMn', (err, result) => {
    if (err) {
      console.log(err)
    } else {
      console.log(result.value)
    }
  })
  console.log(node.bitswap.wantlist())
  setTimeout(() => {
    console.log(node.bitswap.wantlist())
  }, 1000)
  let knownPeers = []
  setInterval(async () => {
    const addrs = await node.swarm.addrs()
    const newPeers = []
    addrs.forEach((addr) => {
      const id = addr.id.toB58String()
      if (!knownPeers.includes(id)) {
        newPeers.push(id)
      }
    })
    if (newPeers.length > 0) {
      console.log('NEW PEERS =>')
      console.log(newPeers)
      knownPeers = [...knownPeers, ...newPeers]
    }
  }, 1000)
})
console output

``` WARNING: Your platform does not have native WebRTC support, it won' use any WebRTC transport Swarm listening on /ip4/127.0.0.1/tcp/4003/ws/ipfs/Qmc9bVU9yQ38C7LaeJMZ9o36tXuHVJWNY2mGZG4w8wzJBE Swarm listening on /ip4/127.0.0.1/tcp/4002/ipfs/Qmc9bVU9yQ38C7LaeJMZ9o36tXuHVJWNY2mGZG4w8wzJBE Swarm listening on /ip4/192.168.100.6/tcp/4002/ipfs/Qmc9bVU9yQ38C7LaeJMZ9o36tXuHVJWNY2mGZG4w8wzJBE [] [ WantListEntry { _refCounter: 1, cid: CID { codec: 'dag-cbor', version: 1, multihash: }, priority: 2147483647 } ] NEW PEERS => [ 'QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ', 'QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z', 'QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM', 'QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm', 'QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu', 'QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64', 'QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd', 'QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3', 'QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx' ] NEW PEERS => [ 'QmeQQdVjvKdo3Q3v2SincZhghnqUGckSF23r9ajxsq4LrJ', 'Qmc9bVU9yQ38C7LaeJMZ9o36tXuHVJWNY2mGZG4w8wzJBE' ] ```


Get request appears in wantlist not immediately, I asume it's okay? By the way, any ideas where those first 9 nodes came from?

daviddias commented 7 years ago

Are you use that both of your laptops are connected? The first 9 nodes are the bootstrappers nodes.

negamaxi commented 7 years ago

@diasdavid Qme.. address of the first laptop's node appears in node.swarm.addrs() of the second one and vice versa. Doesn't it mean they see each other?

/ip4/127.0.0.1/tcp/4003/ws/ipfs/QmeQQdVjvKdo3Q3v2SincZhghnqUGckSF23r9ajxsq4LrJ
NEW PEERS =>
[ 'QmeQQdVjvKdo3Q3v2SincZhghnqUGckSF23r9ajxsq4LrJ',
  'Qmc9bVU9yQ38C7LaeJMZ9o36tXuHVJWNY2mGZG4w8wzJBE' ]
daviddias commented 7 years ago

@negamaxi any update on this issue? Still seeing an issue?

negamaxi commented 7 years ago

@diasdavid I'll check once I get a new laptop.

daviddias commented 7 years ago

Hi @negamaxi, were you able to check this?

negamaxi commented 7 years ago

Hi, still not. Faced another issue #1016.

daviddias commented 6 years ago

@negamaxi still an issue? Would love to make sure this is fixed if there is indeed a real problem. Could you add a failing test to our bitswap batch?

negamaxi commented 6 years ago

@diasdavid as for now it's not an issue for me as we moved to go-ipfs + js-ipfs-api with custom DAG implementation that works as expected. Anyway just tried with js-ipfs 0.27.7:

// pc-publisher
node.on('ready', async () => {
  await node.dag.put({ a: 'b' }, dagParams)
})
// pc-receiver
node.on('ready', () => {
  setInterval(async () => {
    try {
      const value = await node.dag.get('zdpuAw6c3ViCx5A7K4nukvmxh3ZtTHnyknmxCLQmGa6JDmDMn')
      console.log(value)
    } catch (e) {
      console.error(e)
    }
  }, 1000)
})

And still no luck, although publisher id appears in receivers nodelist in about 5 seconds.

I'm not too experienced in writing tests but will be glad to take a try. Just point me which repo I should contribute to.

negamaxi commented 6 years ago

@diasdavid this one? https://github.com/ipfs/js-ipfs/blob/master/test/core/bitswap.spec.js

daviddias commented 6 years ago

@negamaxi exactly.

negamaxi commented 6 years ago

@diasdavid as ipfsd-ctl relies on js-ipfs-api that currently missing dag interface I wasn't able to use IPFSFactory like other tests do. Instead I use two js-ipfs instances and transfer objects between them. Anyway those tests passed. https://github.com/negamaxi/js-ipfs/commit/7b2f5863ea1e935cd4e8d523e8c535482ab3d536

daviddias commented 6 years ago

Excellent @negamaxi. Solved then :)