Skip to content

Commit

Permalink
Merge pull request #147 from primea/tree
Browse files Browse the repository at this point in the history
updated to use merkle tree
  • Loading branch information
wanderer committed Aug 9, 2017
2 parents 387738d + d3fe213 commit 9e075d6
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 108 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: node_js
node_js:
- "7"
- "8"
env:
global:
- CXX=g++-4.8
Expand Down
4 changes: 2 additions & 2 deletions dfsChecker.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @param {string} root - the root id
* @param {Set} nodes - a set of nodes to start searching from
*/
module.exports = async function DFSchecker (graph, state, root, nodes) {
module.exports = async function DFSchecker (tree, root, nodes) {
const checkedNodesSet = new Set()
let hasRootSet = new Set()
const promises = []
Expand Down Expand Up @@ -59,7 +59,7 @@ module.exports = async function DFSchecker (graph, state, root, nodes) {
return
}

const node = state[id]['/']
const node = await tree.get(id)
const promises = []
// iterate through the nodes ports and recursivly check them
for (const name in node.ports) {
Expand Down
22 changes: 14 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const Tree = require('merkle-radix-tree')
const Graph = require('ipld-graph-builder')
const chunk = require('chunk')
const Message = require('primea-message')
const Kernel = require('./kernel.js')
const Scheduler = require('./scheduler.js')
const DFSchecker = require('./dfsChecker.js')
const chunk = require('chunk')

module.exports = class Hypervisor {
/**
Expand All @@ -12,8 +13,12 @@ module.exports = class Hypervisor {
* @param {Graph} dag an instance of [ipfs.dag](https://github.com/ipfs/interface-ipfs-core/tree/master/API/dag#dag-api)
* @param {object} state - the starting state
*/
constructor (dag, state = {}) {
constructor (dag, state = {'/': Tree.emptyTreeState}) {
this.graph = new Graph(dag)
this.tree = new Tree({
graph: this.graph,
root: state
})
this.scheduler = new Scheduler()
this.state = state
this._containerTypes = {}
Expand All @@ -36,11 +41,12 @@ module.exports = class Hypervisor {
* @param {object} port
* @returns {Promise}
*/
getDestPort (port) {
async getDestPort (port) {
if (port.destPort) {
return port.destPort
} else {
return this.graph.get(this.state, `${port.destId}/ports/${port.destName}`)
const containerState = await this.tree.get(port.destId)
return this.graph.get(containerState, `ports/${port.destName}`)
}
}

Expand All @@ -57,7 +63,7 @@ module.exports = class Hypervisor {

// loads an instance of a container from the state
async _loadInstance (id) {
const state = await this.graph.get(this.state, id)
const state = await this.tree.get(id)
const container = this._containerTypes[state.type]
let code

Expand Down Expand Up @@ -134,7 +140,7 @@ module.exports = class Hypervisor {
}

// save the container in the state
await this.graph.set(this.state, idHash, state)
await this.tree.set(idHash, state)
// create the container instance
const instance = await this._loadInstance(idHash)
resolve(instance)
Expand All @@ -160,9 +166,9 @@ module.exports = class Hypervisor {
*/
async createStateRoot (ticks) {
await this.scheduler.wait(ticks)
const unlinked = await DFSchecker(this.graph, this.state, this.ROOT_ID, this._nodesToCheck)
const unlinked = await DFSchecker(this.tree, this.ROOT_ID, this._nodesToCheck)
unlinked.forEach(id => {
delete this.state[id]
this.tree.delete(id)
})
return this.graph.flush(this.state)
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"bn.js": "^4.11.6",
"chunk": "0.0.2",
"ipld-graph-builder": "1.3.0",
"merkle-radix-tree": "0.0.4",
"primea-message": "0.0.2"
},
"devDependencies": {
Expand Down

0 comments on commit 9e075d6

Please sign in to comment.