git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 9e075d6fa8d8d5137146130915af95c57bc1b231

Merge pull request #147 from primea/tree

updated to use merkle tree
wanderer authored on 8/9/2017, 1:42:06 AM
GitHub committed on 8/9/2017, 1:42:06 AM
Parent: 387738df54bdb6de68220d57bcb20797bffa3e76
Parent: d3fe2132bae4a9e48bb6d3a333e4f08f22566f05

Files changed

.travis.ymlchanged
dfsChecker.jschanged
index.jschanged
package.jsonchanged
tests/index.jschanged
.travis.ymlView
@@ -1,7 +1,7 @@
11 language: node_js
22 node_js:
3- - "7"
3 + - "8"
44 env:
55 global:
66 - CXX=g++-4.8
77 matrix:
dfsChecker.jsView
@@ -6,9 +6,9 @@
66 * @param {object} state - the state containing all of the containers to search
77 * @param {string} root - the root id
88 * @param {Set} nodes - a set of nodes to start searching from
99 */
10-module.exports = async function DFSchecker (graph, state, root, nodes) {
10 +module.exports = async function DFSchecker (tree, root, nodes) {
1111 const checkedNodesSet = new Set()
1212 let hasRootSet = new Set()
1313 const promises = []
1414
@@ -58,9 +58,9 @@
5858 hasRootSet = checkedNodes
5959 return
6060 }
6161
62- const node = state[id]['/']
62 + const node = await tree.get(id)
6363 const promises = []
6464 // iterate through the nodes ports and recursivly check them
6565 for (const name in node.ports) {
6666 const port = node.ports[name]
index.jsView
@@ -1,20 +1,25 @@
1 +const Tree = require('merkle-radix-tree')
12 const Graph = require('ipld-graph-builder')
3 +const chunk = require('chunk')
24 const Message = require('primea-message')
35 const Kernel = require('./kernel.js')
46 const Scheduler = require('./scheduler.js')
57 const DFSchecker = require('./dfsChecker.js')
6-const chunk = require('chunk')
78
89 module.exports = class Hypervisor {
910 /**
1011 * The Hypervisor manages the container instances by instantiating them and
1112 * destorying them when possible. It also facilitates localating Containers
1213 * @param {Graph} dag an instance of [ipfs.dag](https://github.com/ipfs/interface-ipfs-core/tree/master/API/dag#dag-api)
1314 * @param {object} state - the starting state
1415 */
15- constructor (dag, state = {}) {
16 + constructor (dag, state = {'/': Tree.emptyTreeState}) {
1617 this.graph = new Graph(dag)
18 + this.tree = new Tree({
19 + graph: this.graph,
20 + root: state
21 + })
1722 this.scheduler = new Scheduler()
1823 this.state = state
1924 this._containerTypes = {}
2025 this._nodesToCheck = new Set()
@@ -35,13 +40,14 @@
3540 * given a port, this finds the corridsponeding endpoint port of the channel
3641 * @param {object} port
3742 * @returns {Promise}
3843 */
39- getDestPort (port) {
44 + async getDestPort (port) {
4045 if (port.destPort) {
4146 return port.destPort
4247 } else {
43- return this.graph.get(this.state, `${port.destId}/ports/${port.destName}`)
48 + const containerState = await this.tree.get(port.destId)
49 + return this.graph.get(containerState, `ports/${port.destName}`)
4450 }
4551 }
4652
4753 async send (port, message) {
@@ -56,9 +62,9 @@
5662 }
5763
5864 // loads an instance of a container from the state
5965 async _loadInstance (id) {
60- const state = await this.graph.get(this.state, id)
66 + const state = await this.tree.get(id)
6167 const container = this._containerTypes[state.type]
6268 let code
6369
6470 // checks if the code stored in the state is an array and that the elements
@@ -133,9 +139,9 @@
133139 state.code = message.data
134140 }
135141
136142 // save the container in the state
137- await this.graph.set(this.state, idHash, state)
143 + await this.tree.set(idHash, state)
138144 // create the container instance
139145 const instance = await this._loadInstance(idHash)
140146 resolve(instance)
141147 // send the intialization message
@@ -159,11 +165,11 @@
159165 * @returns {Promise}
160166 */
161167 async createStateRoot (ticks) {
162168 await this.scheduler.wait(ticks)
163- const unlinked = await DFSchecker(this.graph, this.state, this.ROOT_ID, this._nodesToCheck)
169 + const unlinked = await DFSchecker(this.tree, this.ROOT_ID, this._nodesToCheck)
164170 unlinked.forEach(id => {
165- delete this.state[id]
171 + this.tree.delete(id)
166172 })
167173 return this.graph.flush(this.state)
168174 }
169175
package.jsonView
@@ -33,8 +33,9 @@
3333 "binary-search-insert": "^1.0.3",
3434 "bn.js": "^4.11.6",
3535 "chunk": "0.0.2",
3636 "ipld-graph-builder": "1.3.0",
37 + "merkle-radix-tree": "0.0.4",
3738 "primea-message": "0.0.2"
3839 },
3940 "devDependencies": {
4041 "primea-abstract-container": "0.0.4",
tests/index.jsView
@@ -13,9 +13,9 @@
1313 onCreation (message) {
1414 this.kernel.state.code = message.data.byteLength ? message.data : undefined
1515 const port = message.ports[0]
1616 if (port) {
17- this.kernel.ports.bind('root', port)
17 + return this.kernel.ports.bind('root', port)
1818 }
1919 }
2020 static get typeId () {
2121 return 9
@@ -26,69 +26,80 @@
2626 tape('basic', async t => {
2727 t.plan(3)
2828 let message
2929 const expectedState = {
30- '/': 'zdpuAmN9VSrvNbArWkAEhEYaKhhCT3rk8xMhUam6eUfQmr6aZ'
30 + '/': 'zdpuApGUFnjcY3eBeVPFfnEgGunPz8vyXVJbrkgBmYwrbVDpA'
3131 }
3232
3333 class testVMContainer extends BaseContainer {
3434 onMessage (m) {
3535 t.true(m === message, 'should recive a message')
3636 }
3737 }
3838
39- const hypervisor = new Hypervisor(node.dag)
40- hypervisor.registerContainer(testVMContainer)
39 + try {
40 + const hypervisor = new Hypervisor(node.dag)
41 + hypervisor.registerContainer(testVMContainer)
4142
42- const rootContainer = await hypervisor.createInstance(testVMContainer.typeId)
43 + const rootContainer = await hypervisor.createInstance(testVMContainer.typeId)
4344
44- const [portRef1, portRef2] = rootContainer.ports.createChannel()
45- const initMessage = rootContainer.createMessage({
46- data: Buffer.from('test code'),
47- ports: [portRef2]
48- })
45 + const [portRef1, portRef2] = rootContainer.ports.createChannel()
46 + const initMessage = rootContainer.createMessage({
47 + data: Buffer.from('test code'),
48 + ports: [portRef2]
49 + })
4950
50- rootContainer.createInstance(testVMContainer.typeId, initMessage)
51 + rootContainer.createInstance(testVMContainer.typeId, initMessage)
5152
52- rootContainer.ports.bind('first', portRef1)
53- message = rootContainer.createMessage()
54- rootContainer.send(portRef1, message)
53 + await rootContainer.ports.bind('first', portRef1)
54 + message = rootContainer.createMessage()
55 + rootContainer.send(portRef1, message)
5556
56- const stateRoot = await hypervisor.createStateRoot(Infinity)
57- t.deepEquals(stateRoot, expectedState, 'expected root!')
58- t.equals(hypervisor.scheduler.oldest(), 0)
57 + const stateRoot = await hypervisor.createStateRoot(Infinity)
58 + t.deepEquals(stateRoot, expectedState, 'expected root!')
59 + t.equals(hypervisor.scheduler.oldest(), 0)
60 + } catch (e) {
61 + console.log(e)
62 + }
5963 })
6064
6165 tape('basic - do not store containers with no ports bound', async t => {
6266 t.plan(1)
6367 const expectedState = {
64- '/': 'zdpuAxGvPHM4DRbq7GeyGjwuPA8NT7DZLszcDDX9R5iwHWnTo'
68 + '/': 'zdpuAop4nt8pqzg7duciSYbZmWfDaBiz87RCtGCbb35ewUrbW'
6569 }
6670
6771 class testVMContainer extends BaseContainer {
6872 onCreation () {}
6973 }
7074
71- const hypervisor = new Hypervisor(node.dag)
72- hypervisor.registerContainer(testVMContainer)
75 + try {
76 + const hypervisor = new Hypervisor(node.dag)
77 + hypervisor.registerContainer(testVMContainer)
7378
74- const root = await hypervisor.createInstance(testVMContainer.typeId)
75- const [portRef1, portRef2] = root.ports.createChannel()
79 + const root = await hypervisor.createInstance(testVMContainer.typeId)
80 + const [portRef1, portRef2] = root.ports.createChannel()
7681
77- root.ports.bind('one', portRef1)
78- root.createInstance(testVMContainer.typeId, root.createMessage({
79- ports: [portRef2]
80- }))
82 + await root.ports.bind('one', portRef1)
83 + root.createInstance(testVMContainer.typeId, root.createMessage({
84 + ports: [portRef2]
85 + }))
8186
82- const stateRoot = await hypervisor.createStateRoot(Infinity)
83- t.deepEquals(stateRoot, expectedState, 'expected root!')
87 + const stateRoot = await hypervisor.createStateRoot(Infinity)
88 +
89 + // await hypervisor.graph.tree(stateRoot, Infinity, true)
90 + // console.log(JSON.stringify(stateRoot, null, 2))
91 + t.deepEquals(stateRoot, expectedState, 'expected root!')
92 + } catch (e) {
93 + console.log(e)
94 + }
8495 })
8596
8697 tape('one child contract with saturated ports', async t => {
8798 t.plan(2)
8899 let message
89100 const expectedState = {
90- '/': 'zdpuAvWT2E1Hg6cvFNLTDbmjGRLSDbMnRtrA6s17oSdBX5EWs'
101 + '/': 'zdpuArCqpDZtEqjrXrRhMiYLE7QQ1szVr1qLVkiwtDLincGWU'
91102 }
92103
93104 class testVMContainer2 extends BaseContainer {
94105 onMessage (m) {
@@ -104,11 +115,11 @@
104115 const [portRef1, portRef2] = this.kernel.ports.createChannel()
105116 this.kernel.createInstance(testVMContainer2.typeId, this.kernel.createMessage({
106117 ports: [portRef2]
107118 }))
108- this.kernel.ports.bind('child', portRef1)
109119 this.kernel.incrementTicks(2)
110120 this.kernel.send(portRef1, m)
121 + return this.kernel.ports.bind('child', portRef1)
111122 }
112123 }
113124
114125 const hypervisor = new Hypervisor(node.dag)
@@ -120,23 +131,26 @@
120131 root.createInstance(testVMContainer.typeId, root.createMessage({
121132 ports: [portRef2]
122133 }))
123134
124- root.ports.bind('first', portRef1)
135 + await root.ports.bind('first', portRef1)
125136 message = root.createMessage({
126137 data: 'test'
127138 })
128139
129140 root.send(portRef1, message)
130141 const stateRoot = await hypervisor.createStateRoot(Infinity)
142 +
131143 t.deepEquals(stateRoot, expectedState, 'expected state')
144 + // await hypervisor.graph.tree(stateRoot, Infinity, true)
145 + // console.log(JSON.stringify(stateRoot, null, 2))
132146 })
133147
134148 tape('one child contract', async t => {
135149 t.plan(4)
136150 let message
137151 const expectedState = {
138- '/': 'zdpuAvWT2E1Hg6cvFNLTDbmjGRLSDbMnRtrA6s17oSdBX5EWs'
152 + '/': 'zdpuArCqpDZtEqjrXrRhMiYLE7QQ1szVr1qLVkiwtDLincGWU'
139153 }
140154 let hasResolved = false
141155
142156 class testVMContainer2 extends BaseContainer {
@@ -161,11 +175,11 @@
161175 const [portRef1, portRef2] = this.kernel.ports.createChannel()
162176 this.kernel.createInstance(testVMContainer2.typeId, this.kernel.createMessage({
163177 ports: [portRef2]
164178 }))
165- this.kernel.ports.bind('child', portRef1)
166179 this.kernel.send(portRef1, m)
167180 this.kernel.incrementTicks(1)
181 + return this.kernel.ports.bind('child', portRef1)
168182 }
169183 }
170184
171185 const hypervisor = new Hypervisor(node.dag)
@@ -178,14 +192,17 @@
178192 root.createInstance(testVMContainer.typeId, root.createMessage({
179193 ports: [portRef2]
180194 }))
181195
182- root.ports.bind('first', portRef1)
196 + await root.ports.bind('first', portRef1)
183197 message = root.createMessage()
184198
185199 root.send(portRef1, message)
186200 const stateRoot = await hypervisor.createStateRoot(Infinity)
187201 t.true(hasResolved, 'should resolve before generating the state root')
202 +
203 + // await hypervisor.graph.tree(stateRoot, Infinity, true)
204 + // console.log(JSON.stringify(stateRoot, null, 2))
188205 t.deepEquals(stateRoot, expectedState, 'expected state')
189206
190207 // test reviving the state
191208 class testVMContainer3 extends BaseContainer {
@@ -209,11 +226,13 @@
209226 const [portRef1, portRef2] = this.kernel.ports.createChannel()
210227 const [portRef3, portRef4] = this.kernel.ports.createChannel()
211228 const [portRef5, portRef6] = this.kernel.ports.createChannel()
212229
213- this.kernel.ports.bind('one', portRef1)
214- this.kernel.ports.bind('two', portRef3)
215- this.kernel.ports.bind('three', portRef5)
230 + await Promise.all(
231 + this.kernel.ports.bind('one', portRef1),
232 + this.kernel.ports.bind('two', portRef3),
233 + this.kernel.ports.bind('three', portRef5)
234 + )
216235
217236 const message1 = this.kernel.createMessage({
218237 ports: [portRef2]
219238 })
@@ -239,9 +258,9 @@
239258 await root.message(root.createMessage())
240259 const stateRoot = await hypervisor.createStateRoot()
241260
242261 t.deepEquals(stateRoot, {
243- '/': 'zdpuAwxK8kAM3SkxSyALurpFHTobp6sFJef9gZJ8ZDQRww1LN'
262 + '/': 'zdpuAoifKuJkWz9Fjvt79NmGq3tcefhfCyq8iM8YhcFdV9bmZ'
244263 }, 'should revert the state')
245264 })
246265
247266 tape('message should arrive in the correct oder if sent in order', async t => {
@@ -255,11 +274,8 @@
255274
256275 const [portRef1, portRef2] = this.kernel.ports.createChannel()
257276 const [portRef3, portRef4] = this.kernel.ports.createChannel()
258277
259- this.kernel.ports.bind('two', portRef3)
260- this.kernel.ports.bind('one', portRef1)
261-
262278 const message1 = this.kernel.createMessage({
263279 ports: [portRef2]
264280 })
265281 const message2 = this.kernel.createMessage({
@@ -270,8 +286,12 @@
270286 this.kernel.createInstance(Second.typeId, message2)
271287
272288 this.kernel.send(portRef1, this.kernel.createMessage())
273289 this.kernel.send(portRef3, this.kernel.createMessage())
290 + return Promise.all(
291 + this.kernel.ports.bind('two', portRef3),
292 + this.kernel.ports.bind('one', portRef1)
293 + )
274294 } else if (runs === 1) {
275295 runs++
276296 t.equals(m.data, 'first', 'should recive the first message')
277297 } else if (runs === 2) {
@@ -318,9 +338,9 @@
318338 root.createInstance(Root.typeId, root.createMessage({
319339 ports: [portRef2]
320340 }))
321341
322- root.ports.bind('first', portRef1)
342 + await root.ports.bind('first', portRef1)
323343 const message = root.createMessage()
324344 root.send(portRef1, message)
325345 })
326346
@@ -335,11 +355,8 @@
335355
336356 const [portRef1, portRef2] = this.kernel.ports.createChannel()
337357 const [portRef3, portRef4] = this.kernel.ports.createChannel()
338358
339- this.kernel.ports.bind('one', portRef1)
340- this.kernel.ports.bind('two', portRef3)
341-
342359 const message1 = this.kernel.createMessage({
343360 ports: [portRef2]
344361 })
345362 const message2 = this.kernel.createMessage({
@@ -350,8 +367,13 @@
350367 this.kernel.createInstance(Second.typeId, message2)
351368
352369 this.kernel.send(portRef1, this.kernel.createMessage())
353370 this.kernel.send(portRef3, this.kernel.createMessage())
371 +
372 + return Promise.all([
373 + this.kernel.ports.bind('one', portRef1),
374 + this.kernel.ports.bind('two', portRef3)
375 + ])
354376 } else if (runs === 1) {
355377 runs++
356378 t.equals(m.data, 'second', 'should recived the second message')
357379 } else if (runs === 2) {
@@ -398,9 +420,9 @@
398420 root.createInstance(Root.typeId, root.createMessage({
399421 ports: [portRef2]
400422 }))
401423
402- root.ports.bind('first', portRef1)
424 + await root.ports.bind('first', portRef1)
403425 const message = root.createMessage()
404426 root.send(portRef1, message)
405427 })
406428
@@ -414,11 +436,8 @@
414436 runs++
415437 const [portRef1, portRef2] = this.kernel.ports.createChannel()
416438 const [portRef3, portRef4] = this.kernel.ports.createChannel()
417439
418- this.kernel.ports.bind('one', portRef1)
419- this.kernel.ports.bind('two', portRef3)
420-
421440 const message1 = this.kernel.createMessage({
422441 ports: [portRef2]
423442 })
424443 const message2 = this.kernel.createMessage({
@@ -431,8 +450,13 @@
431450 this.kernel.send(portRef1, this.kernel.createMessage())
432451 this.kernel.send(portRef3, this.kernel.createMessage())
433452
434453 this.kernel.incrementTicks(6)
454 +
455 + return Promise.all([
456 + this.kernel.ports.bind('one', portRef1),
457 + this.kernel.ports.bind('two', portRef3)
458 + ])
435459 } else if (runs === 1) {
436460 runs++
437461 t.equals(m.data, 'first', 'should recive the first message')
438462 } else if (runs === 2) {
@@ -476,9 +500,9 @@
476500 root.createInstance(Root.typeId, root.createMessage({
477501 ports: [portRef2]
478502 }))
479503
480- root.ports.bind('first', portRef1)
504 + await root.ports.bind('first', portRef1)
481505 const message = root.createMessage()
482506 root.send(portRef1, message)
483507 })
484508
@@ -492,11 +516,8 @@
492516 runs++
493517 const [portRef1, portRef2] = this.kernel.ports.createChannel()
494518 const [portRef3, portRef4] = this.kernel.ports.createChannel()
495519
496- this.kernel.ports.bind('one', portRef1)
497- this.kernel.ports.bind('two', portRef3)
498-
499520 const message1 = this.kernel.createMessage({
500521 ports: [portRef2]
501522 })
502523 const message2 = this.kernel.createMessage({
@@ -509,8 +530,12 @@
509530 this.kernel.send(portRef1, this.kernel.createMessage())
510531 this.kernel.send(portRef3, this.kernel.createMessage())
511532
512533 this.kernel.incrementTicks(6)
534 + return Promise.all([
535 + this.kernel.ports.bind('one', portRef1),
536 + this.kernel.ports.bind('two', portRef3)
537 + ])
513538 } else if (runs === 1) {
514539 runs++
515540 t.equals(m.data, 'first', 'should recive the first message')
516541 } else if (runs === 2) {
@@ -568,15 +593,15 @@
568593 const [portRef1, portRef2] = root.ports.createChannel()
569594
570595 const message = root.createMessage()
571596 root.send(portRef1, message)
572- root.ports.bind('first', portRef1)
597 + await root.ports.bind('first', portRef1)
573598 root.createInstance(Root.typeId, root.createMessage({
574599 ports: [portRef2]
575600 }))
576601
577602 const [portRef3, portRef4] = root.ports.createChannel()
578- root.ports.bind('sencond', portRef3)
603 + await root.ports.bind('sencond', portRef3)
579604 root.createInstance(Waiter.typeId, root.createMessage({
580605 ports: [portRef4]
581606 }))
582607
@@ -596,11 +621,8 @@
596621 runs++
597622 const [portRef1, portRef2] = this.kernel.ports.createChannel()
598623 const [portRef3, portRef4] = this.kernel.ports.createChannel()
599624
600- this.kernel.ports.bind('two', portRef3)
601- this.kernel.ports.bind('one', portRef1)
602-
603625 const message1 = this.kernel.createMessage({
604626 ports: [portRef2]
605627 })
606628 const message2 = this.kernel.createMessage({
@@ -613,8 +635,12 @@
613635 this.kernel.send(portRef1, this.kernel.createMessage())
614636 this.kernel.send(portRef3, this.kernel.createMessage())
615637
616638 this.kernel.incrementTicks(6)
639 + return Promise.all([
640 + this.kernel.ports.bind('two', portRef3),
641 + this.kernel.ports.bind('one', portRef1)
642 + ])
617643 } else if (runs === 1) {
618644 runs++
619645 t.equals(m.data, 'second', 'should recived the second message')
620646 } else if (runs === 2) {
@@ -660,9 +686,9 @@
660686 const [portRef1, portRef2] = root.ports.createChannel()
661687 const message = root.createMessage()
662688
663689 root.send(portRef1, message)
664- root.ports.bind('first', portRef1)
690 + await root.ports.bind('first', portRef1)
665691 root.createInstance(Root.typeId, root.createMessage({
666692 ports: [portRef2]
667693 }))
668694 })
@@ -672,16 +698,16 @@
672698
673699 let runs = 0
674700
675701 class Root extends BaseContainer {
676- onMessage (m) {
702 + async onMessage (m) {
677703 if (!runs) {
678704 runs++
679705 const [portRef1, portRef2] = this.kernel.ports.createChannel()
680706 const [portRef3, portRef4] = this.kernel.ports.createChannel()
681707
682- this.kernel.ports.bind('one', portRef1)
683- this.kernel.ports.bind('two', portRef3)
708 + await this.kernel.ports.bind('one', portRef1)
709 + await this.kernel.ports.bind('two', portRef3)
684710
685711 const message1 = this.kernel.createMessage({
686712 ports: [portRef2]
687713 })
@@ -739,9 +765,9 @@
739765 const [portRef1, portRef2] = root.ports.createChannel()
740766 const message = root.createMessage()
741767
742768 root.send(portRef1, message)
743- root.ports.bind('first', portRef1)
769 + await root.ports.bind('first', portRef1)
744770 root.createInstance(Root.typeId, root.createMessage({
745771 ports: [portRef2]
746772 }))
747773 })
@@ -756,13 +782,13 @@
756782 onMessage (m) {
757783 let one = this.kernel.ports.get('one')
758784 if (!one) {
759785 const [portRef1, portRef2] = this.kernel.ports.createChannel()
760- this.kernel.ports.bind('one', portRef1)
761786 const message1 = this.kernel.createMessage({
762787 ports: [portRef2]
763788 })
764789 this.kernel.createInstance(First.typeId, message1)
790 + return this.kernel.ports.bind('one', portRef1)
765791 } else {
766792 this.kernel.send(one, this.kernel.createMessage())
767793 this.kernel.send(one, this.kernel.createMessage())
768794 }
@@ -789,9 +815,9 @@
789815 hypervisor.registerContainer(First)
790816
791817 const root = await hypervisor.createInstance(Root.typeId)
792818 const [portRef1, portRef2] = root.ports.createChannel()
793- root.ports.bind('first', portRef1)
819 + await root.ports.bind('first', portRef1)
794820 root.createInstance(Root.typeId, root.createMessage({
795821 ports: [portRef2]
796822 }))
797823
@@ -813,9 +839,9 @@
813839 const [portRef1, portRef2] = root.ports.createChannel()
814840 root.createInstance(BaseContainer.typeId, root.createMessage({
815841 ports: [portRef2]
816842 }))
817- root.ports.bind('test', portRef1)
843 + await root.ports.bind('test', portRef1)
818844
819845 try {
820846 root.createMessage({
821847 ports: [portRef1]
@@ -845,21 +871,21 @@
845871 })
846872
847873 tape('port deletion', async t => {
848874 const expectedSr = {
849- '/': 'zdpuAqFMWKsATaU1gJwMTegcw18GFQ7szZix3QNgMN2sYm2vh'
875 + '/': 'zdpuAopMy53q2uvL2a4fhVEAvwXjSDW28fh8zhQUj598tb5md'
850876 }
851877 class Root extends BaseContainer {
852878 onMessage (m) {
853879 const [portRef1, portRef2] = this.kernel.ports.createChannel()
854- this.kernel.ports.bind('one', portRef1)
855880 const message1 = this.kernel.createMessage({
856881 ports: [portRef2]
857882 })
858883
859884 this.kernel.createInstance(First.typeId, message1)
860885 this.kernel.send(portRef1, this.kernel.createMessage())
861886 this.kernel.incrementTicks(6)
887 + return this.kernel.ports.bind('one', portRef1)
862888 }
863889 }
864890
865891 class First extends BaseContainer {
@@ -878,9 +904,9 @@
878904 hypervisor.registerContainer(First)
879905
880906 const root = await hypervisor.createInstance(Root.typeId)
881907 const [portRef1, portRef2] = root.ports.createChannel()
882- root.ports.bind('first', portRef1)
908 + await root.ports.bind('first', portRef1)
883909 root.createInstance(Root.typeId, root.createMessage({
884910 ports: [portRef2]
885911 }))
886912
@@ -888,15 +914,16 @@
888914 root.send(portRef1, message)
889915
890916 const sr = await hypervisor.createStateRoot()
891917 t.deepEquals(sr, expectedSr, 'should produce the corret state root')
918 + await hypervisor.graph.tree(sr, Infinity, true)
892919
893920 t.end()
894921 })
895922
896923 tape('clear unbounded ports', async t => {
897924 const expectedSr = {
898- '/': 'zdpuAqFMWKsATaU1gJwMTegcw18GFQ7szZix3QNgMN2sYm2vh'
925 + '/': 'zdpuAopMy53q2uvL2a4fhVEAvwXjSDW28fh8zhQUj598tb5md'
899926 }
900927 class Root extends BaseContainer {
901928 onMessage (m) {
902929 this.kernel.createInstance(Root.typeId)
@@ -908,9 +935,9 @@
908935 hypervisor.registerContainer(Root)
909936
910937 const root = await hypervisor.createInstance(Root.typeId)
911938 const [portRef1, portRef2] = root.ports.createChannel()
912- root.ports.bind('first', portRef1)
939 + await root.ports.bind('first', portRef1)
913940 root.createInstance(Root.typeId, root.createMessage({
914941 ports: [portRef2]
915942 }))
916943
@@ -923,9 +950,9 @@
923950 })
924951
925952 tape('should remove subgraphs', async t => {
926953 const expectedSr = {
927- '/': 'zdpuAqFMWKsATaU1gJwMTegcw18GFQ7szZix3QNgMN2sYm2vh'
954 + '/': 'zdpuAopMy53q2uvL2a4fhVEAvwXjSDW28fh8zhQUj598tb5md'
928955 }
929956 class Root extends BaseContainer {
930957 onMessage (m) {
931958 const [, portRef2] = this.kernel.ports.createChannel()
@@ -935,13 +962,13 @@
935962 }
936963 }
937964
938965 class Sub extends BaseContainer {
939- onInitailize (message) {
940- this.kernel.ports.bind('root', message.ports[0])
941- const [portRef1, portRef2] = root.ports.createChannel()
942- root.ports.bind('child', portRef1)
943- root.createInstance(Root.typeId, root.createMessage({
966 + async onInitailize (message) {
967 + await this.kernel.ports.bind('root', message.ports[0])
968 + const [portRef1, portRef2] = this.kernel.ports.createChannel()
969 + await this.kernel.ports.bind('child', portRef1)
970 + this.kernel.createInstance(Root.typeId, this.kernel.createMessage({
944971 ports: [portRef2]
945972 }))
946973 }
947974 static get typeId () {
@@ -955,9 +982,9 @@
955982 hypervisor.registerContainer(Sub)
956983
957984 const root = await hypervisor.createInstance(Root.typeId)
958985 const [portRef1, portRef2] = root.ports.createChannel()
959- root.ports.bind('first', portRef1)
986 + await root.ports.bind('first', portRef1)
960987 root.createInstance(Root.typeId, root.createMessage({
961988 ports: [portRef2]
962989 }))
963990
@@ -969,28 +996,28 @@
969996 })
970997
971998 tape('should not remove connected nodes', async t => {
972999 const expectedSr = {
973- '/': 'zdpuAppPTaXwHnfU2yjtTyT9XsY7SJAkDwQWUZnkHU7myRzaj'
1000 + '/': 'zdpuApKrsvsWknDML2Mme9FyZfRnVZ1hTCoKzkooYAWT3dUDV'
9741001 }
9751002 class Root extends BaseContainer {
976- onMessage (m) {
1003 + async onMessage (m) {
9771004 if (m.ports.length) {
9781005 const port = this.kernel.ports.get('test1')
9791006 this.kernel.send(port, m)
980- this.kernel.ports.unbind('test1')
1007 + return this.kernel.ports.unbind('test1')
9811008 } else {
9821009 const [portRef1, portRef2] = this.kernel.ports.createChannel()
9831010 this.kernel.createInstance(Sub.typeId, this.kernel.createMessage({
9841011 ports: [portRef2]
9851012 }))
986- this.kernel.ports.bind('test1', portRef1)
1013 + await this.kernel.ports.bind('test1', portRef1)
9871014
9881015 const [portRef3, portRef4] = this.kernel.ports.createChannel()
9891016 this.kernel.createInstance(Sub.typeId, this.kernel.createMessage({
9901017 ports: [portRef4]
9911018 }))
992- this.kernel.ports.bind('test2', portRef3)
1019 + await this.kernel.ports.bind('test2', portRef3)
9931020 this.kernel.send(portRef3, this.kernel.createMessage({
9941021 data: 'getChannel'
9951022 }))
9961023 }
@@ -1000,15 +1027,15 @@
10001027 class Sub extends BaseContainer {
10011028 onMessage (message) {
10021029 if (message.data === 'getChannel') {
10031030 const ports = this.kernel.ports.createChannel()
1004- this.kernel.ports.bind('channel', ports[0])
10051031 this.kernel.send(message.fromPort, this.kernel.createMessage({
10061032 data: 'bindPort',
10071033 ports: [ports[1]]
10081034 }))
1035 + return this.kernel.ports.bind('channel', ports[0])
10091036 } else if (message.data === 'bindPort') {
1010- this.kernel.ports.bind('channel', message.ports[0])
1037 + return this.kernel.ports.bind('channel', message.ports[0])
10111038 }
10121039 }
10131040 static get typeId () {
10141041 return 299
@@ -1021,9 +1048,9 @@
10211048 hypervisor.registerContainer(Sub)
10221049
10231050 const root = await hypervisor.createInstance(Root.typeId)
10241051 const [portRef1, portRef2] = root.ports.createChannel()
1025- root.ports.bind('first', portRef1)
1052 + await root.ports.bind('first', portRef1)
10261053 root.createInstance(Root.typeId, root.createMessage({
10271054 ports: [portRef2]
10281055 }))
10291056
@@ -1036,29 +1063,29 @@
10361063 })
10371064
10381065 tape('should remove multiple subgraphs', async t => {
10391066 const expectedSr = {
1040- '/': 'zdpuAvQqoEnojZHaw6dMDy8ACRVqfarfD2RCKTwFBYsj8suRC'
1067 + '/': 'zdpuAohccQTxM82d8N6Q82z234nQskeQoJGJu3eAVmxoQwWde'
10411068 }
10421069 class Root extends BaseContainer {
1043- onMessage (m) {
1070 + async onMessage (m) {
10441071 if (m.ports.length) {
10451072 const port = this.kernel.ports.get('test1')
10461073 this.kernel.send(port, m)
1047- this.kernel.ports.unbind('test1')
1048- this.kernel.ports.unbind('test2')
1074 + await this.kernel.ports.unbind('test1')
1075 + await this.kernel.ports.unbind('test2')
10491076 } else {
10501077 const [portRef1, portRef2] = this.kernel.ports.createChannel()
10511078 this.kernel.createInstance(Sub.typeId, this.kernel.createMessage({
10521079 ports: [portRef2]
10531080 }))
1054- this.kernel.ports.bind('test1', portRef1)
1081 + await this.kernel.ports.bind('test1', portRef1)
10551082
10561083 const [portRef3, portRef4] = this.kernel.ports.createChannel()
10571084 this.kernel.createInstance(Sub.typeId, this.kernel.createMessage({
10581085 ports: [portRef4]
10591086 }))
1060- this.kernel.ports.bind('test2', portRef3)
1087 + await this.kernel.ports.bind('test2', portRef3)
10611088 this.kernel.send(portRef3, this.kernel.createMessage({
10621089 data: 'getChannel'
10631090 }))
10641091 }
@@ -1068,15 +1095,15 @@
10681095 class Sub extends BaseContainer {
10691096 onMessage (message) {
10701097 if (message.data === 'getChannel') {
10711098 const ports = this.kernel.ports.createChannel()
1072- this.kernel.ports.bind('channel', ports[0])
10731099 this.kernel.send(message.fromPort, this.kernel.createMessage({
10741100 data: 'bindPort',
10751101 ports: [ports[1]]
10761102 }))
1103 + return this.kernel.ports.bind('channel', ports[0])
10771104 } else if (message.data === 'bindPort') {
1078- this.kernel.ports.bind('channel', message.ports[0])
1105 + return this.kernel.ports.bind('channel', message.ports[0])
10791106 }
10801107 }
10811108 static get typeId () {
10821109 return 299
@@ -1090,9 +1117,9 @@
10901117
10911118 const root = await hypervisor.createInstance(Root.typeId)
10921119
10931120 const [portRef1, portRef2] = root.ports.createChannel()
1094- root.ports.bind('first', portRef1)
1121 + await root.ports.bind('first', portRef1)
10951122 root.createInstance(Root.typeId, root.createMessage({
10961123 ports: [portRef2]
10971124 }))
10981125
@@ -1132,17 +1159,17 @@
11321159 })
11331160
11341161 rootContainer.createInstance(testVMContainer.typeId, initMessage)
11351162
1136- rootContainer.ports.bind('first', portRef1)
1163 + await rootContainer.ports.bind('first', portRef1)
11371164 const message = rootContainer.createMessage()
11381165 const rPort = rootContainer.getResponsePort(message)
11391166 const rPort2 = rootContainer.getResponsePort(message)
11401167
11411168 t.equals(rPort2, rPort)
11421169
11431170 rootContainer.send(portRef1, message)
1144- rootContainer.ports.bind('response', rPort)
1171 + await rootContainer.ports.bind('response', rPort)
11451172 })
11461173
11471174 tape('start up', async t => {
11481175 t.plan(1)

Built with git-ssb-web