git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 3820100dc50eb86a7f6f941410dc7e8dd2c7aaa7

move port creation to hypervisor

wanderer committed on 4/26/2017, 3:48:24 PM
Parent: a0b1792b4abd832c8abb3959057e090cd3e33741

Files changed

index.jschanged
kernel.jschanged
portManager.jschanged
tests/index.jschanged
index.jsView
@@ -23,9 +23,9 @@
2323
2424 // create a new kernel instance
2525 const VM = this._opts.VMs[port.type]
2626 const opts = Object.assign({
27- state: port.vm,
27+ state: port.link,
2828 id: port.id,
2929 VM: VM
3030 }, this._opts)
3131
@@ -40,9 +40,10 @@
4040 }
4141
4242 async send (port, message) {
4343 const vm = await this.getInstance(port)
44- message._fromPort = 'root'
44+ const id = await this.generateID(port)
45+ message._fromPort = id
4546 vm.queue(message)
4647 }
4748
4849 // given a port, wait untill its source contract has reached the threshold
@@ -52,15 +53,29 @@
5253 await kernel.wait(ticks)
5354 return kernel
5455 }
5556
57+ createPort (type, payload = {}, id = {nonce: [0], parent: null}) {
58+ const VM = this._opts.VMs[type]
59+ return {
60+ 'messages': [],
61+ 'id': {
62+ '/': id
63+ },
64+ 'type': type,
65+ 'link': {
66+ '/': VM.createState(payload)
67+ }
68+ }
69+ }
70+
5671 async createStateRoot (port, ticks) {
5772 await this.wait(port, ticks)
5873 return this.graph.flush(port)
5974 }
6075
6176 generateID (port) {
62- this.graph.flush(port.id)
77+ return this.graph.flush(port.id)
6378 }
6479
6580 addVM (type, vm) {
6681 this._opts.VMs[type] = vm
kernel.jsView
@@ -55,8 +55,9 @@
5555 let result
5656 try {
5757 result = await this.vm.run(message) || {}
5858 } catch (e) {
59+ console.log(e)
5960 result = {
6061 exception: true,
6162 exceptionError: e
6263 }
@@ -98,23 +99,24 @@
9899 this._waitingQueue.poll().resolve(count)
99100 }
100101 }
101102
102- createPort () {
103+ async createPort (manager, type, name, payload) {
104+ // incerment the nonce
103105 const nonce = new BN(this.nonce)
104106 nonce.iaddn(1)
105107 this.nonce = nonce.toArrayLike(Buffer)
106- return {
107- id: {
108- '/': {
109- nonce: this.nonce,
110- parent: this.id
111- }
112- },
113- link: {
114- '/': {}
115- }
116- }
108+
109+ const parentID = await this._opts.hypervisor.generateID({
110+ id: this._opts.id
111+ })
112+
113+ const port = this._opts.hypervisor.createPort(type, payload, {
114+ nonce: this.nonce,
115+ parent: parentID
116+ })
117+ manager.set(name, port)
118+ return port
117119 }
118120
119121 async send (port, message) {
120122 message._ticks = this.ticks
portManager.jsView
@@ -33,28 +33,29 @@
3333 async start () {
3434 // map ports to thier id's
3535 let ports = Object.keys(this.ports).map(name => {
3636 const port = this.ports[name]
37- this.hypervisor.generateID(port).then(id => {
38- return [id, new Port(name)]
39- })
37+ this._mapPort(name, port)
4038 })
4139
4240 // create the parent port
43- ports = await Promise.all(ports)
44- this._portMap = new Map(ports)
45- // add the parent port
46- let parent = await this.hypervisor.graph.get(this.kernel._opts.id, 'parent')
47- parent = parent === null ? 'root' : parent
48- this._portMap.set(parent, new Port('parent'))
41+ await Promise.all(ports)
42+ this._portMap.set(this.kernel._opts.id, new Port('parent'))
4943 }
5044
45+ _mapPort (name, port) {
46+ this.hypervisor.generateID(port).then(id => {
47+ this._portMap.set(id, new Port(name))
48+ })
49+ }
50+
5151 queue (message) {
5252 this._portMap.get(message.fromPort).queue(message)
5353 }
5454
55- create (name, value) {
56- this.ports[name] = value
55+ set (name, port) {
56+ this.ports[name] = port
57+ return this._mapPort(name, port)
5758 }
5859
5960 del (name) {
6061 delete this.ports[name]
tests/index.jsView
@@ -3,107 +3,80 @@
33 const Hypervisor = require('../')
44 const Message = require('primea-message')
55
66 const node = new IPFS()
7+
8+class BaseContainer {
9+ static createState (code) {
10+ return {
11+ nonce: Buffer.from([0]),
12+ ports: {}
13+ }
14+ }
15+}
16+
717 node.on('error', err => {
818 console.log(err)
919 })
1020
1121 node.on('start', () => {
12- tape('basic', async t => {
22+ tape.only('basic', async t => {
1323 const message = new Message()
14- const state = {
15- messages: [],
16- id: {
17- '/': {
18- nonce: new Buffer([0]),
19- parent: {
20- '/': null
21- }
22- }
23- },
24- type: 'test',
25- vm: {
26- '/': {
27- ports: {}
28- }
29- }
30- }
31- const expectedState = { '/': 'zdpuAnCsh9tVFa3asqkC7iNkwK6dYyZqJDxQrB7PMt8foLRKJ' }
24+ const expectedState = { '/': 'zdpuB2hzCvqE34W71CFtqqzHLP8kyuwGZm1bz8Cy2kAVCh1fP' }
3225
33- class testVMContainer {
26+ class testVMContainer extends BaseContainer {
3427 run (m) {
3528 t.true(m === message, 'should recive a message')
3629 }
3730 }
3831
39- const hypervisor = new Hypervisor({
40- dag: node.dag
41- })
32+ const hypervisor = new Hypervisor({dag: node.dag})
4233 hypervisor.addVM('test', testVMContainer)
34+ const port = hypervisor.createPort('test')
4335
44- await hypervisor.send(state, message)
45- await hypervisor.createStateRoot(state, Infinity)
46- t.deepEquals(state, expectedState, 'expected')
36+ await hypervisor.send(port, message)
37+ await hypervisor.createStateRoot(port, Infinity)
38+ t.deepEquals(port, expectedState, 'expected')
39+ // await hypervisor.graph.tree(port, Infinity)
40+ // console.log(JSON.stringify(port, null, 2))
4741 t.end()
4842 })
4943
5044 tape('one child contract', async t => {
5145 t.end()
52- node.stop(() => {
53- process.exit()
54- })
55-
5646 const message = new Message()
57- class testVMContainer {
58- constuctor (kernel) {
59- this.kernel = kernel
60- }
47+ const expectedState = { '/': 'zdpuAwqyF4X1hAHMBcsn7eDJXcLfcyoyEWWR73eeqXXmFkBe3' }
6148
49+ class testVMContainer2 extends BaseContainer {
6250 run (m) {
63- this.kernel.ports.create('child', 'test2', null)
64- this.kernek.send('child', m)
51+ console.log('here!')
52+ t.true(m === message, 'should recive a message')
6553 }
6654 }
6755
68- class testVMContainer2 {
69- run (m) {
70- t.true(m === message, 'should recive a message')
56+ class testVMContainer extends BaseContainer {
57+ constructor (kernel) {
58+ super()
59+ this.kernel = kernel
7160 }
72- }
7361
74- const state = {
75- messages: [],
76- id: {
77- '/': {
78- nonce: new Buffer([0]),
79- parent: {
80- '/': null
81- }
82- }
83- },
84- type: 'test',
85- vm: {
86- '/': {
87- ports: {}
88- }
62+ async run (m) {
63+ console.log('first')
64+ const port = await this.kernel.createPort(this.kernel.ports, 'test2', 'child')
65+ return this.kernel.send(port, m)
8966 }
9067 }
9168
92- const hypervisor = new Hypervisor({
93- dag: node.dag
94- })
69+ const hypervisor = new Hypervisor({dag: node.dag})
9570 hypervisor.addVM('test', testVMContainer)
71+ hypervisor.addVM('test2', testVMContainer2)
72+ const port = hypervisor.createPort('test')
9673
97- await hypervisor.send(state, message)
98- await hypervisor.createStateRoot(state, Infinity)
99- t.deepEquals(state, expectedState, 'expected')
100- t.end()
101- // const message = new Message({
102- // type: 'create',
103- // path: 'first',
104- // data: jsCode
105- // })
106- // hypervisor.send(port, message)
74+ await hypervisor.send(port, message)
75+ await hypervisor.createStateRoot(port, Infinity)
76+ t.deepEquals(port, expectedState, 'expected')
10777
78+ node.stop(() => {
79+ process.exit()
80+ })
10881 })
10982 })

Built with git-ssb-web