holepunchto / hyperdrive

Hyperdrive is a secure, real time distributed file system
Apache License 2.0
1.86k stars 134 forks source link

Missing metadata hypercore in mount #253

Closed mjp0 closed 4 years ago

mjp0 commented 4 years ago

I'm testing out new hyperdrive and I ran into a weird problem with mount().

My code looks this:

var hyperdrive = require('hyperdrive')

async function create(id) {
  return new Promise((resolve, reject) => {
    const archive = hyperdrive(`./cores/${id}`)
    archive.ready(() => {
      archive.writeFile('/index', '1', ()=>{
          resolve(archive)
      })
    })
  })
}

function replicate(master, mount) {
  const s1 = master.replicate({ live: true, encrypt: false })
  s1.pipe(mount.replicate({ live: true, encrypt: false })).pipe(s1)
}

;(async function () {
  const master = await create('master')
  const mount1 = await create('device1')
  replicate(master, mount1)

  master.mount('/mount1', mount1.key, (err) => {
    if(err) throw new Error(err)
    master.readdir('/mount1', (err, list) => {
      if(err) throw new Error(err)
      console.log(list)
    })
  })
})()

And it crashes like this:

G:\Development\P2P\Experiments\hyperdrive\node_modules\hyperdrive\index.js:223
    const mountMetadata = db.feed
                             ^

TypeError: Cannot read property 'feed' of undefined
    at Hyperdrive._getContent (G:\Development\P2P\Experiments\hyperdrive\node_modules\hyperdrive\index.js:223:30)
    at Hyperdrive._getContent (G:\Development\P2P\Experiments\hyperdrive\node_modules\hyperdrive\index.js:217:49)
    at self.ready.err (G:\Development\P2P\Experiments\hyperdrive\node_modules\hyperdrive\index.js:550:14)
    at apply (G:\Development\P2P\Experiments\hyperdrive\node_modules\thunky\index.js:44:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)

Any ideas why this is happening? I checked the unit tests and my code looks almost identical.

andrewosh commented 4 years ago

Hey @markopolojarvi, there are currently a handful of timing issues in the master branch related to mounts, but I believe all of them are currently fixed in the hypercore-8 branch (alongside many more updates).

I'm planning on merging hypercore-8 into master really soon and publishing another prerelease, so mind seeing if that branch works for you?

mjp0 commented 4 years ago

@andrewosh Hey, I tried hypercore-8 branch but ended up with another error. It's triggered by replication apparently.

(node:4044) UnhandledPromiseRejectionWarning: Error: AssertionError
    at assert (G:\Development\P2P\Experiments\hyperdrive\node_modules\nanoassert\index.js:21:17)
    at Object.initialize (G:\Development\P2P\Experiments\hyperdrive\node_modules\noise-protocol\handshake-state.js:187:3)
    at new SimpleHandshake (G:\Development\P2P\Experiments\hyperdrive\node_modules\simple-handshake\index.js:19:22)
    at SimpleHandshake (G:\Development\P2P\Experiments\hyperdrive\node_modules\simple-handshake\index.js:8:50)
    at new ProtocolHandshake (G:\Development\P2P\Experiments\hyperdrive\node_modules\simple-hypercore-protocol\lib\handshake.js:17:18)
    at SimpleProtocol._onkeypair (G:\Development\P2P\Experiments\hyperdrive\node_modules\simple-hypercore-protocol\index.js:62:23)
    at new SimpleProtocol (G:\Development\P2P\Experiments\hyperdrive\node_modules\simple-hypercore-protocol\index.js:50:12)
    at new ProtocolStream (G:\Development\P2P\Experiments\hyperdrive\node_modules\hyperdrive\node_modules\hypercore-protocol\index.js:291:18)      
    at Hyperdrive.replicate (G:\Development\P2P\Experiments\hyperdrive\node_modules\hyperdrive\index.js:724:20)
    at replicate (G:\Development\P2P\Experiments\hyperdrive\sanity_test.js:15:21)
(node:4044) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:4044) [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.
mjp0 commented 4 years ago

I finally had time to debug this. The error with hypercore-8 turned out to be because of the new isInitiator parameter. Errors disappeared after I set those correctly.