git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit f0fbf4a220a3041e504eb1b75fc6e6e2e9488ad6

clean up

wanderer committed on 7/27/2017, 10:12:35 PM
Parent: e08047273f81b45b7107bc0bc4fa2557d9f79eb0

Files changed

index.jschanged
kernel.jschanged
package.jsonchanged
tests/index.jschanged
index.jsView
@@ -3,12 +3,9 @@
33 const Kernel = require('./kernel.js')
44 const Scheduler = require('./scheduler.js')
55 const DFSchecker = require('./dfsChecker.js')
66 const chunk = require('chunk')
7-const flatten = require('flatten')
87
9-const ROOT_ID = 'zdpuAm6aTdLVMUuiZypxkwtA7sKm7BWERy8MPbaCrFsmiyzxr'
10-
118 module.exports = class Hypervisor {
129 /**
1310 * The Hypervisor manages the container instances by instantiating them and
1411 * destorying them when possible. It also facilitates localating Containers
@@ -21,9 +18,10 @@
2118 this.state = state
2219 this._containerTypes = {}
2320 this._nodesToCheck = new Set()
2421
25- this.MAX_DATA_BYTES = 6553
22+ this.ROOT_ID = 'zdpuAm6aTdLVMUuiZypxkwtA7sKm7BWERy8MPbaCrFsmiyzxr'
23+ this.MAX_DATA_BYTES = 65533
2624 }
2725
2826 /**
2927 * add a potaintail node in the state graph to check for garbage collection
@@ -60,17 +58,22 @@
6058 // loads an instance of a container from the state
6159 async _loadInstance (id) {
6260 const state = await this.graph.get(this.state, id)
6361 const container = this._containerTypes[state.type]
62+ let code
6463
65- // if (state.code && Array.isArray(state.code[0])) {
66- // state.code = flatten(state.code)
67- // }
64+ if (state.code && state.code[0]['/']) {
65+ await this.graph.tree(state.code, 1)
66+ code = state.code.map(a => a['/']).reduce((a, b) => a + b)
67+ } else {
68+ code = state.code
69+ }
6870
6971 // create a new kernel instance
7072 const kernel = new Kernel({
7173 hypervisor: this,
7274 state: state,
75+ code: code,
7376 container: container,
7477 id: id
7578 })
7679
@@ -95,8 +98,9 @@
9598 return instance
9699 } else {
97100 const resolve = this.scheduler.getLock(id)
98101 const instance = await this._loadInstance(id)
102+ await instance.startup()
99103 resolve(instance)
100104 return instance
101105 }
102106 }
@@ -122,8 +126,12 @@
122126 ports: {},
123127 type: type
124128 }
125129
130+ if (message.data.length) {
131+ state.code = message.data
132+ }
133+
126134 // save the container in the state
127135 await this.graph.set(this.state, idHash, state)
128136 // create the container instance
129137 const instance = await this._loadInstance(idHash)
@@ -136,10 +144,8 @@
136144 return {
137145 '/': chk
138146 }
139147 })
140- } else {
141- console.log(state.code)
142148 }
143149
144150 return instance
145151 }
@@ -151,13 +157,12 @@
151157 * @returns {Promise}
152158 */
153159 async createStateRoot (ticks) {
154160 await this.scheduler.wait(ticks)
155- const unlinked = await DFSchecker(this.graph, this.state, ROOT_ID, this._nodesToCheck)
161+ const unlinked = await DFSchecker(this.graph, this.state, this.ROOT_ID, this._nodesToCheck)
156162 unlinked.forEach(id => {
157163 delete this.state[id]
158164 })
159- console.log(this.state)
160165 return this.graph.flush(this.state)
161166 }
162167
163168 /**
kernel.jsView
@@ -14,8 +14,9 @@
1414 * @param {Object} opts.container - the container constuctor and argments
1515 */
1616 constructor (opts) {
1717 this.state = opts.state
18+ this.code = opts.code
1819 this.hypervisor = opts.hypervisor
1920 this.id = opts.id
2021 this.container = new opts.container.Constructor(this, opts.container.args)
2122 this.timeout = 0
@@ -72,10 +73,13 @@
7273 }
7374
7475 shutdown () {
7576 this.hypervisor.scheduler.done(this.id)
76- if (this.container.shutdown) {
77- this.container.shutdown()
77+ }
78+
79+ startup () {
80+ if (this.container.startup) {
81+ return this.container.startup()
7882 }
7983 }
8084
8185 /**
package.jsonView
@@ -32,9 +32,8 @@
3232 "dependencies": {
3333 "binary-search-insert": "^1.0.3",
3434 "bn.js": "^4.11.6",
3535 "chunk": "0.0.2",
36- "flatten": "^1.0.2",
3736 "ipld-graph-builder": "1.2.2",
3837 "primea-abstract-container": "0.0.1",
3938 "primea-message": "0.0.1"
4039 },
tests/index.jsView
@@ -1,7 +1,8 @@
11 const tape = require('tape')
22 const IPFS = require('ipfs')
33 const AbstractContainer = require('primea-abstract-container')
4+const Message = require('primea-message')
45 const Hypervisor = require('../')
56
67 // start ipfs
78 const node = new IPFS({
@@ -1070,5 +1071,34 @@
10701071
10711072 rootContainer.send(portRef1, message)
10721073 rootContainer.ports.bind('response', rPort)
10731074 })
1075+
1076+ tape('start up', async t => {
1077+ t.plan(1)
1078+ class testVMContainer extends BaseContainer {
1079+ run () {}
1080+ startup () {
1081+ t.true(true, 'should start up')
1082+ }
1083+ }
1084+
1085+ const hypervisor = new Hypervisor(node.dag)
1086+ hypervisor.registerContainer('test', testVMContainer)
1087+ await hypervisor.createInstance('test')
1088+ hypervisor.getInstance(hypervisor.ROOT_ID)
1089+ })
1090+
1091+ tape('large code size', async t => {
1092+ t.plan(1)
1093+ const content = Buffer.from(new ArrayBuffer(1000000))
1094+ class testVMContainer extends BaseContainer {
1095+ run () {}
1096+ }
1097+
1098+ const hypervisor = new Hypervisor(node.dag)
1099+ hypervisor.registerContainer('test', testVMContainer)
1100+ await hypervisor.createInstance('test', new Message({data: content}))
1101+ const instance = await hypervisor.getInstance(hypervisor.ROOT_ID)
1102+ t.equals(content.length, instance.code.length)
1103+ })
10741104 })

Built with git-ssb-web