git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 993a67d6ed59227e98922bba72cc4bd827f37964

move port creation to the port manager

wanderer committed on 5/15/2017, 12:36:36 PM
Parent: b407fbdf9c0e01ec5356351e2aa7cf1fad00f82e

Files changed

README.mdchanged
index.jschanged
kernel.jschanged
package.jsonchanged
portManager.jschanged
tests/index.jschanged
README.mdView
@@ -1,8 +1,9 @@
11 # SYNOPSIS - WIP
2-[![NPM Package](https://img.shields.io/npm/v/merkle-trie.svg?style=flat-square)](https://www.npmjs.org/package/ewasm-kernel)
3-[![Build Status](https://img.shields.io/travis/ewasm/ewasm-kernel.svg?branch=master&style=flat-square)](https://travis-ci.org/ewasm/ewasm-kernel)
4-
2+[![NPM Package](https://img.shields.io/npm/v/primea-hypervisor.svg?style=flat-square)](https://www.npmjs.org/package/primea-hypervisor)
3+[![Build Status](https://img.shields.io/travis/primea/js-primea-hypervisor.svg?branch=master&style=flat-square)](https://travis-ci.org/primea/js-primea-hypervisor)
4+[![Coverage Status](https://img.shields.io/coveralls/primea/js-primea-hypervisor.svg?style=flat-square)](https://coveralls.io/r/primea/js-primea-hypervisor)
5+
56 [![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)
67
78 A JavaScript Implementation of Primea HyperVisor
89
index.jsView
@@ -39,8 +39,9 @@
3939 }
4040
4141 async createInstance (type, state, entryPort = null, parentPort) {
4242 const VM = this._VMs[type]
43+
4344 if (!state) {
4445 state = {
4546 '/': VM.createState()
4647 }
kernel.jsView
@@ -1,5 +1,4 @@
1-const BN = require('bn.js')
21 const clearObject = require('object-clear')
32 const clone = require('clone')
43 const EventEmitter = require('events')
54 const PortManager = require('./portManager.js')
@@ -115,36 +114,8 @@
115114 }
116115 }
117116 }
118117
119- createPort (type, name) {
120- const VM = this.hypervisor._VMs[type]
121- const parentId = this.entryPort ? this.entryPort.id : null
122- let nonce = this.state['/'].nonce
123-
124- const portRef = {
125- 'messages': [],
126- 'id': {
127- '/': {
128- nonce: nonce,
129- parent: parentId
130- }
131- },
132- 'type': type,
133- 'link': {
134- '/': VM.createState()
135- }
136- }
137-
138- // create the port instance
139- this.ports.set(name, portRef)
140- // incerment the nonce
141- nonce = new BN(nonce)
142- nonce.iaddn(1)
143- this.state['/'].nonce = nonce.toArray()
144- return portRef
145- }
146-
147118 async send (portRef, message) {
148119 message._fromPort = this.entryPort
149120 message._fromPortTicks = this.ticks
150121
package.jsonView
@@ -5,10 +5,9 @@
55 "scripts": {
66 "coverage": "node ./node_modules/istanbul/lib/cli.js cover ./tests/index.js",
77 "coveralls": "npm run coverage && coveralls <coverage/lcov.info",
88 "lint": "standard",
9- "test": "tape ./tests/index.js",
10- "build": "node ./tests/buildTests.js && ./tools/wabt/out/wast2wasm ./wasm/interface.wast -o ./wasm/interface.wasm"
9+ "test": "tape ./tests/index.js"
1110 },
1211 "repository": {
1312 "type": "git",
1413 "url": "git+https://github.com/primea/js-primea-hypervisor.git"
portManager.jsView
@@ -1,5 +1,6 @@
11 const Port = require('./port.js')
2+const BN = require('bn.js')
23 const ENTRY = Symbol('entry')
34
45 // decides which message to go first
56 function messageArbiter (pairA, pairB) {
@@ -62,8 +63,36 @@
6263 get (key) {
6364 return this.ports[key]
6465 }
6566
67+ create (type, name) {
68+ const VM = this.hypervisor._VMs[type]
69+ const parentId = this.entryPort ? this.entryPort.id : null
70+ let nonce = this.state['/'].nonce
71+
72+ const portRef = {
73+ 'messages': [],
74+ 'id': {
75+ '/': {
76+ nonce: nonce,
77+ parent: parentId
78+ }
79+ },
80+ 'type': type,
81+ 'link': {
82+ '/': VM.createState()
83+ }
84+ }
85+
86+ // create the port instance
87+ this.set(name, portRef)
88+ // incerment the nonce
89+ nonce = new BN(nonce)
90+ nonce.iaddn(1)
91+ this.state['/'].nonce = nonce.toArray()
92+ return portRef
93+ }
94+
6695 // waits till all ports have reached a threshold tick count
6796 wait (threshold, fromPort) {
6897 // find the ports that have a smaller tick count then the threshold tick count
6998 const unkownPorts = [...this._portMap].filter(([portRef, port]) => {
tests/index.jsView
@@ -38,9 +38,9 @@
3838 const hypervisor = new Hypervisor({dag: node.dag})
3939 hypervisor.registerContainer('test', testVMContainer)
4040
4141 const rootContainer = await hypervisor.createInstance('test')
42- const port = await rootContainer.createPort('test', 'first')
42+ const port = await rootContainer.ports.create('test', 'first')
4343
4444 await rootContainer.send(port, message)
4545
4646 const stateRoot = await hypervisor.createStateRoot(rootContainer, Infinity)
@@ -69,9 +69,9 @@
6969 }
7070
7171 class testVMContainer extends BaseContainer {
7272 async run (m) {
73- const port = await this.kernel.createPort('test2', 'child')
73+ const port = await this.kernel.ports.create('test2', 'child')
7474 await this.kernel.send(port, m)
7575 this.kernel.incrementTicks(1)
7676 }
7777 }
@@ -80,9 +80,9 @@
8080 hypervisor.registerContainer('test', testVMContainer)
8181 hypervisor.registerContainer('test2', testVMContainer2)
8282
8383 let root = await hypervisor.createInstance('test')
84- let port = await root.createPort('test', 'first')
84+ let port = await root.ports.create('test', 'first')
8585
8686 await root.send(port, message)
8787 const stateRoot = await hypervisor.createStateRoot(root, Infinity)
8888 t.true(hasResolved, 'should resolve before generating the state root')
@@ -111,9 +111,9 @@
111111 class Ping extends BaseContainer {
112112 async run (m) {
113113 let port = this.kernel.ports.get('child')
114114 if (!port) {
115- port = await this.kernel.createPort('pong', 'child')
115+ port = await this.kernel.ports.create('pong', 'child')
116116 }
117117
118118 if (this.kernel.ticks < 100) {
119119 this.kernel.incrementTicks(1)
@@ -136,9 +136,9 @@
136136
137137 hypervisor.registerContainer('ping', Ping)
138138 hypervisor.registerContainer('pong', Pong)
139139 const root = await hypervisor.createInstance('pong')
140- const port = await root.createPort('ping', 'child')
140+ const port = await root.ports.create('ping', 'child')
141141
142142 await root.send(port, new Message())
143143 await hypervisor.createStateRoot(root, Infinity)
144144
@@ -149,11 +149,11 @@
149149 let runs = 0
150150
151151 class Root extends BaseContainer {
152152 async run (m) {
153- const one = this.kernel.createPort('child', 'one')
154- const two = this.kernel.createPort('child', 'two')
155- const three = this.kernel.createPort('child', 'three')
153+ const one = this.kernel.ports.create('child', 'one')
154+ const two = this.kernel.ports.create('child', 'two')
155+ const three = this.kernel.ports.create('child', 'three')
156156
157157 await Promise.all([
158158 this.kernel.send(one, new Message()),
159159 this.kernel.send(two, new Message()),
@@ -183,9 +183,9 @@
183183 hypervisor.registerContainer('root', Root)
184184 hypervisor.registerContainer('child', Child)
185185
186186 const root = await hypervisor.createInstance('root')
187- const port = await root.createPort('root', 'first')
187+ const port = await root.ports.create('root', 'first')
188188 await root.send(port, new Message())
189189 await root.wait(Infinity)
190190
191191 t.equals(runs, 3, 'the number of run should be 3')
@@ -198,11 +198,11 @@
198198 tape('traps', async t => {
199199 class Root extends BaseContainer {
200200 async run (m) {
201201 await Promise.all([
202- this.kernel.createPort('root', 'one'),
203- this.kernel.createPort('root', 'two'),
204- this.kernel.createPort('root', 'three')
202+ this.kernel.ports.create('root', 'one'),
203+ this.kernel.ports.create('root', 'two'),
204+ this.kernel.ports.create('root', 'three')
205205 ])
206206
207207 throw new Error('it is a trap!!!')
208208 }
@@ -232,10 +232,10 @@
232232 class Root extends BaseContainer {
233233 async run (m) {
234234 if (!this.runs) {
235235 this.runs = 1
236- const one = this.kernel.createPort('first', 'one')
237- const two = this.kernel.createPort('second', 'two')
236+ const one = this.kernel.ports.create('first', 'one')
237+ const two = this.kernel.ports.create('second', 'two')
238238
239239 await Promise.all([
240240 this.kernel.send(one, new Message()),
241241 this.kernel.send(two, new Message())
@@ -273,9 +273,9 @@
273273 hypervisor.registerContainer('first', First)
274274 hypervisor.registerContainer('second', Second)
275275
276276 const root = await hypervisor.createInstance('root')
277- const port = await root.createPort('root', 'first')
277+ const port = await root.ports.create('root', 'first')
278278 await root.send(port, new Message())
279279 })
280280
281281 tape('message should arrive in the correct order, even if sent out of order', async t => {
@@ -284,10 +284,10 @@
284284 class Root extends BaseContainer {
285285 async run (m) {
286286 if (!this.runs) {
287287 this.runs = 1
288- const one = this.kernel.createPort('first', 'one')
289- const two = this.kernel.createPort('second', 'two')
288+ const one = this.kernel.ports.create('first', 'one')
289+ const two = this.kernel.ports.create('second', 'two')
290290
291291 await Promise.all([
292292 this.kernel.send(one, new Message()),
293293 this.kernel.send(two, new Message())
@@ -325,9 +325,9 @@
325325 hypervisor.registerContainer('first', First)
326326 hypervisor.registerContainer('second', Second)
327327
328328 const root = await hypervisor.createInstance('root')
329- const port = await root.createPort('root', 'first')
329+ const port = await root.ports.create('root', 'first')
330330 await root.send(port, new Message())
331331 })
332332
333333 tape('message should arrive in the correct order, even in a tie of ticks', async t => {
@@ -336,10 +336,10 @@
336336 class Root extends BaseContainer {
337337 async run (m) {
338338 if (!this.runs) {
339339 this.runs = 1
340- const one = this.kernel.createPort('first', 'one')
341- const two = this.kernel.createPort('second', 'two')
340+ const one = this.kernel.ports.create('first', 'one')
341+ const two = this.kernel.ports.create('second', 'two')
342342
343343 await Promise.all([
344344 this.kernel.send(one, new Message()),
345345 this.kernel.send(two, new Message())
@@ -377,9 +377,9 @@
377377 hypervisor.registerContainer('first', First)
378378 hypervisor.registerContainer('second', Second)
379379
380380 const root = await hypervisor.createInstance('root')
381- const port = await root.createPort('root', 'first')
381+ const port = await root.ports.create('root', 'first')
382382 await root.send(port, new Message())
383383 })
384384
385385 tape('message should arrive in the correct order, even in a tie of ticks', async t => {
@@ -388,10 +388,10 @@
388388 class Root extends BaseContainer {
389389 async run (m) {
390390 if (!this.runs) {
391391 this.runs = 1
392- const two = this.kernel.createPort('second', 'two')
393- const one = this.kernel.createPort('first', 'one')
392+ const two = this.kernel.ports.create('second', 'two')
393+ const one = this.kernel.ports.create('first', 'one')
394394
395395 await Promise.all([
396396 this.kernel.send(two, new Message()),
397397 this.kernel.send(one, new Message())
@@ -429,9 +429,9 @@
429429 hypervisor.registerContainer('first', First)
430430 hypervisor.registerContainer('second', Second)
431431
432432 const root = await hypervisor.createInstance('root')
433- const port = await root.createPort('root', 'first')
433+ const port = await root.ports.create('root', 'first')
434434 await root.send(port, new Message())
435435 })
436436
437437 tape('message should arrive in the correct order, with a tie in ticks but with differnt proity', async t => {
@@ -440,10 +440,10 @@
440440 class Root extends BaseContainer {
441441 async run (m) {
442442 if (!this.runs) {
443443 this.runs = 1
444- const one = this.kernel.createPort('first', 'one')
445- const two = this.kernel.createPort('second', 'two')
444+ const one = this.kernel.ports.create('first', 'one')
445+ const two = this.kernel.ports.create('second', 'two')
446446
447447 await Promise.all([
448448 this.kernel.send(two, new Message()),
449449 this.kernel.send(one, new Message())
@@ -486,9 +486,9 @@
486486 hypervisor.registerContainer('first', First)
487487 hypervisor.registerContainer('second', Second)
488488
489489 const root = await hypervisor.createInstance('root')
490- const port = await root.createPort('root', 'first')
490+ const port = await root.ports.create('root', 'first')
491491 await root.send(port, new Message())
492492 })
493493
494494 tape('message should arrive in the correct order, with a tie in ticks but with differnt proity', async t => {
@@ -497,10 +497,10 @@
497497 class Root extends BaseContainer {
498498 async run (m) {
499499 if (!this.runs) {
500500 this.runs = 1
501- const one = this.kernel.createPort('first', 'one')
502- const two = this.kernel.createPort('second', 'two')
501+ const one = this.kernel.ports.create('first', 'one')
502+ const two = this.kernel.ports.create('second', 'two')
503503
504504 await Promise.all([
505505 this.kernel.send(two, new Message()),
506506 this.kernel.send(one, new Message())
@@ -545,9 +545,9 @@
545545 hypervisor.registerContainer('first', First)
546546 hypervisor.registerContainer('second', Second)
547547
548548 const root = await hypervisor.createInstance('root')
549- const port = await root.createPort('root', 'first')
549+ const port = await root.ports.create('root', 'first')
550550 await root.send(port, new Message())
551551 })
552552
553553 tape('should order parent messages correctly', async t => {
@@ -555,9 +555,9 @@
555555 async run (m) {
556556 if (!this.runs) {
557557 this.runs = 1
558558 this.kernel.incrementTicks(1)
559- const leaf = this.kernel.createPort('leaf', 'leaf')
559+ const leaf = this.kernel.ports.create('leaf', 'leaf')
560560 await this.kernel.send(leaf, new Message())
561561 } else {
562562 ++this.runs
563563 if (this.runs === 3) {
@@ -585,13 +585,13 @@
585585 hypervisor.registerContainer('leaf', Leaf)
586586
587587 const root = await hypervisor.createInstance('root')
588588 root.incrementTicks(2)
589- const port = await root.createPort('middle', 'first')
590589
590+ const port = await root.ports.create('middle', 'first')
591+
591592 await root.send(port, new Message())
592593 await root.send(port, new Message())
593-
594594 await root.wait(Infinity)
595595
596596 t.end()
597597 node.stop(() => {

Built with git-ssb-web