andrewosh / mountable-hypertrie

A Hypertrie wrapper that supports mounting of other Hypertries
MIT License
26 stars 9 forks source link

two new tries use the same key #6

Closed DougAnderson444 closed 4 years ago

DougAnderson444 commented 4 years ago

@andrewosh Are the tries supposed to get the same key? I was expecting them to have 2 different keys...

  const store1 = new Corestore(ram)

  const masterTrie = new MountableHypertrie(store1)
  const slaveTrie1 = new MountableHypertrie(store1)

  masterTrie.ready(()=>{
    console.log('Master key: ', masterTrie.key.toString('hex')) 
  })
  slaveTrie1.ready(()=>{
    console.log('Slave key: ', slaveTrie1.key.toString('hex'))   // same as master
  })

https://repl.it/@DougAnderson444/HyperTrieKeyTest1

I tried to make them have 2 separate stores, but that didn't work out so well.

andrewosh commented 4 years ago

@DougAnderson444 Yeah your example just needs a small change:

const store1 = new Corestore(ram)
const trie1 = new MountableHypertrie(store1)
const trie2 = new MountableHypertrie(store1.namespace('trie2'))
...

The idea here is that every corestore has a default hypercore (which is accessed using store.default()) -- the general pattern is to use this default core to bootstrap other cores (by storing keys directly in the core like we do in hyperdrive, for example).

MountableHypertrie always calls default on the corestore it's given, so if you want to reuse the same corestore you need to create a NamespacedCorestore (which will wrap the default method and use a different key).

Sorry for the confusion! We've only recently converged on this API.

DougAnderson444 commented 4 years ago

No worries! That works like a charm :) Can't wait to use this!