git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit b95b6a5752781c6cea64703d51e548ffddfb48ac

fixed port generation

wanderer committed on 4/28/2017, 11:48:04 AM
Parent: 535100729b49473f07a4c560fa6b81aa2b369544

Files changed

index.jschanged
kernel.jschanged
portManager.jschanged
tests/index.jschanged
index.jsView
@@ -51,10 +51,9 @@
5151 // given a port, wait untill its source contract has reached the threshold
5252 // tick count
5353 async wait (port, threshold) {
5454 let kernel = await this.getInstance(port)
55- await kernel.wait(threshold)
56- return kernel
55+ return kernel.wait(threshold)
5756 }
5857
5958 createPort (type, payload = {}, id = {nonce: [0], parent: null}) {
6059 const VM = this._opts.VMs[type]
kernel.jsView
@@ -16,11 +16,9 @@
1616 return a.threshold > b.threshold
1717 })
1818 this.on('result', this._runNextMessage)
1919 this.on('idle', () => {
20- console.log('idle')
2120 while (!this._waitingQueue.isEmpty()) {
22- console.log('clering ')
2321 this._waitingQueue.poll().resolve()
2422 }
2523 })
2624 }
@@ -36,14 +34,14 @@
3634
3735 queue (message) {
3836 this.ports.queue(message)
3937 if (this.vmState === 'idle') {
38+ this._updateVmState('running')
4039 this._runNextMessage()
4140 }
4241 }
4342
4443 _runNextMessage () {
45- this._updateVmState('running')
4644 this.ports.getNextMessage(this.ticks).then(message => {
4745 if (message) {
4846 this.run(message)
4947 } else {
@@ -63,9 +61,8 @@
6361 let result
6462 try {
6563 result = await this.vm.run(message) || {}
6664 } catch (e) {
67- console.log(e)
6865 result = {
6966 exception: true,
7067 exceptionError: e
7168 }
@@ -105,19 +102,15 @@
105102 }
106103
107104 async createPort (manager, type, name, payload) {
108105 // incerment the nonce
109- const nonce = new BN(this._opts.state.nonce)
106+ const nonce = new BN(this.state.nonce)
110107 nonce.iaddn(1)
111- this.state.nonce = nonce.toArrayLike(Buffer)
108+ this.state.nonce = nonce.toArray()
112109
113- const parentID = await this._opts.hypervisor.generateID({
114- id: this._opts.id
115- })
116-
117110 let port = this._opts.hypervisor.createPort(type, payload, {
118- nonce: this.nonce,
119- parent: parentID
111+ nonce: this.state.nonce,
112+ parent: this._opts.parentPort.id
120113 })
121114 await manager.set(name, port)
122115 return port
123116 }
portManager.jsView
@@ -87,20 +87,14 @@
8787 const unkownPorts = [...this._portMap].filter(([id, port]) => {
8888 return (port.hasSent || port.name === PARENT) && port.ticks < threshold
8989 })
9090
91- const promises = unkownPorts.map(([id, port]) => {
92- if (port.name === PARENT) {
93- port = this.parentPort
94- } else {
95- port = this.ports[port.name]
96- }
97- this.hypervisor.wait(port, threshold).then(ticks => {
98- // update the port's tick count
99- port.ticks = ticks
100- })
91+ const promises = unkownPorts.map(async ([id, port]) => {
92+ const portObj = port.name === PARENT ? this.parentPort : this.ports[port.name]
93+ // update the port's tick count
94+ port.ticks = await this.hypervisor.wait(portObj, threshold)
10195 })
102- return await Promise.all(promises)
96+ return Promise.all(promises)
10397 }
10498
10599 async getNextMessage (ticks) {
106100 await this.wait(ticks)
tests/index.jsView
@@ -22,48 +22,43 @@
2222 console.log(err)
2323 })
2424
2525 node.on('start', () => {
26- tape.only('basic', async t => {
26+ tape('basic', async t => {
2727 const message = new Message()
2828 const expectedState = {
29- '/': 'zdpuAn1R5shTypKNBHT8Js2uBnbUcujHfnPNrKbKRNL1AyAt5'
29+ '/': 'zdpuB3eZQJuXMnQrdiF5seMvx3zC2xT1EqrQScoPcTs8ESxYx'
3030 }
3131
3232 class testVMContainer extends BaseContainer {
3333 run (m) {
3434 t.true(m === message, 'should recive a message')
3535 }
3636 }
3737
38- try {
39- const hypervisor = new Hypervisor({dag: node.dag})
40- hypervisor.addVM('test', testVMContainer)
41- const port = hypervisor.createPort('test')
38+ const hypervisor = new Hypervisor({dag: node.dag})
39+ hypervisor.addVM('test', testVMContainer)
40+ const port = hypervisor.createPort('test')
4241
43- await hypervisor.send(port, message)
44- await hypervisor.createStateRoot(port, Infinity)
42+ await hypervisor.send(port, message)
43+ await hypervisor.createStateRoot(port, Infinity)
4544
46- t.deepEquals(port, expectedState, 'expected')
47- } catch (e) {
48- console.log(e)
49- }
45+ t.deepEquals(port, expectedState, 'expected')
5046 t.end()
5147 })
5248
5349 tape('one child contract', async t => {
5450 const message = new Message()
55- const expectedState = {
56- '/': 'zdpuAwUPELiXpnd66Wum84VRPEsUGB7cUuxUESDMXmpVj6prc'
57- }
51+ const expectedState = { '/': 'zdpuAqtY43BMaTCB5nTt7kooeKAWibqGs44Uwy9jJQHjTnHRK' }
52+ let hasResolved= false
5853
5954 class testVMContainer2 extends BaseContainer {
6055 run (m) {
6156 t.true(m === message, 'should recive a message 2')
6257 return new Promise((resolve, reject) => {
6358 setTimeout(() => {
64- console.log('resolve!!')
6559 this.kernel.incrementTicks(1)
60+ hasResolved = true
6661 resolve()
6762 }, 200)
6863 })
6964 }
@@ -76,27 +71,20 @@
7671 this.kernel.incrementTicks(1)
7772 }
7873 }
7974
80- try {
81- const hypervisor = new Hypervisor({dag: node.dag})
82- hypervisor.addVM('test', testVMContainer)
83- hypervisor.addVM('test2', testVMContainer2)
84- const port = hypervisor.createPort('test')
75+ const hypervisor = new Hypervisor({dag: node.dag})
76+ hypervisor.addVM('test', testVMContainer)
77+ hypervisor.addVM('test2', testVMContainer2)
78+ const port = hypervisor.createPort('test')
8579
86- await hypervisor.send(port, message)
87- await hypervisor.createStateRoot(port, Infinity)
88- console.log('create state root')
80+ await hypervisor.send(port, message)
81+ await hypervisor.createStateRoot(port, Infinity)
82+ t.true(hasResolved, 'should resolve before generating the state root')
83+ t.deepEquals(port, expectedState, 'expected state')
8984
90- // await hypervisor.graph.tree(port, Infinity)
91- // console.log(JSON.stringify(port, null, 2))
92- // t.deepEquals(port, expectedState, 'expected')
93- } catch (e) {
94- console.log(e)
95- }
96-
9785 t.end()
9886 node.stop(() => {
99- // process.exit()
87+ process.exit()
10088 })
10189 })
10290 })

Built with git-ssb-web