git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit ca80f013bf0ad297f4a0e0ab0c5a57b5c3b84034

fix aribiter

wanderer committed on 4/28/2017, 5:04:44 PM
Parent: b95b6a5752781c6cea64703d51e548ffddfb48ac

Files changed

index.jschanged
kernel.jschanged
package.jsonchanged
portManager.jschanged
tests/index.jschanged
index.jsView
@@ -1,5 +1,6 @@
11 const Graph = require('ipld-graph-builder')
2+const multibase = require('multibase')
23 const Kernel = require('./kernel.js')
34
45 module.exports = class Hypervisor {
56 constructor (opts) {
@@ -18,11 +19,8 @@
1819 let kernel = this._vmInstances.get(id)
1920 if (!kernel) {
2021 // load the container from the state
2122 await this.graph.tree(port, 2)
22- // if (port['/']) {
23- // port = port['/']
24- // }
2523
2624 // create a new kernel instance
2725 const VM = this._opts.VMs[port.type]
2826
@@ -54,18 +52,18 @@
5452 let kernel = await this.getInstance(port)
5553 return kernel.wait(threshold)
5654 }
5755
58- createPort (type, payload = {}, id = {nonce: [0], parent: null}) {
56+ createPort (type, id = {nonce: [0], parent: null}) {
5957 const VM = this._opts.VMs[type]
6058 return {
6159 'messages': [],
6260 'id': {
6361 '/': id
6462 },
6563 'type': type,
6664 'link': {
67- '/': VM.createState(payload)
65+ '/': VM.createState()
6866 }
6967 }
7068 }
7169
@@ -74,10 +72,14 @@
7472 return this.graph.flush(port)
7573 }
7674
7775 async generateID (port) {
78- const id = await this.graph.flush(port.id)
79- return id['/']
76+ let id = await this.graph.flush(port.id)
77+ id = id['/']
78+ if (Buffer.isBuffer(id)) {
79+ id = multibase.encode('base58btc', id).toString()
80+ }
81+ return id
8082 }
8183
8284 addVM (type, vm) {
8385 this._opts.VMs[type] = vm
kernel.jsView
@@ -100,22 +100,26 @@
100100 }
101101 }
102102 }
103103
104- async createPort (manager, type, name, payload) {
104+ async createPort (manager, type, name) {
105105 // incerment the nonce
106106 const nonce = new BN(this.state.nonce)
107107 nonce.iaddn(1)
108108 this.state.nonce = nonce.toArray()
109109
110- let port = this._opts.hypervisor.createPort(type, payload, {
110+ let port = this._opts.hypervisor.createPort(type, {
111111 nonce: this.state.nonce,
112112 parent: this._opts.parentPort.id
113113 })
114114 await manager.set(name, port)
115115 return port
116116 }
117117
118+ getPort (manager, name) {
119+ return manager.get(name)
120+ }
121+
118122 async send (port, message) {
119123 message._ticks = this.ticks
120124 const portObject = await this.ports.get(port)
121125 portObject.hasSent = true
package.jsonView
@@ -29,8 +29,9 @@
2929 "bn.js": "^4.11.6",
3030 "deepcopy": "^0.6.3",
3131 "fastpriorityqueue": "^0.2.4",
3232 "ipld-graph-builder": "1.0.1",
33+ "multibase": "^0.3.4",
3334 "primea-message": "0.0.0"
3435 },
3536 "devDependencies": {
3637 "coveralls": "^2.13.0",
portManager.jsView
@@ -1,28 +1,26 @@
11 const Port = require('./port.js')
22 const PARENT = Symbol('parent')
33
44 // decides which message to go firts
5-function messageArbiter (portA, portB) {
6- portA = portA[1]
7- portB = portB[1]
8- const a = portA.peek()
9- const b = portB.peek()
5+function messageArbiter (pairA, pairB) {
6+ const a = pairA[1].peek()
7+ const b = pairB[1].peek()
108
119 if (!a) {
12- return b
10+ return pairB
1311 } else if (!b) {
14- return a
12+ return pairA
1513 }
1614
1715 const aGasPrice = a.resources.gasPrice
1816 const bGasPrice = b.resources.gasPrice
1917 if (a.ticks !== b.ticks) {
20- return a.ticks < b.ticks ? a : b
18+ return a.ticks < b.ticks ? pairA : pairB
2119 } else if (aGasPrice === bGasPrice) {
22- return a.hash() > b.hash() ? a : b
20+ return a.hash() > b.hash() ? pairA : pairB
2321 } else {
24- return aGasPrice > bGasPrice ? a : b
22+ return aGasPrice > bGasPrice ? pairA : pairB
2523 }
2624 }
2725
2826 module.exports = class PortManager {
@@ -61,27 +59,13 @@
6159 this.ports[name] = port
6260 return this._mapPort(name, port)
6361 }
6462
65- del (name) {
66- delete this.ports[name]
67- }
68-
69- move (from, to) {
70- this.ports[to] = this.ports[from]
71- delete this.ports[from]
72- }
73-
7463 async get (port) {
7564 const id = await this.hypervisor.generateID(port)
7665 return this._portMap.get(id)
7766 }
7867
79- async getParent () {
80- const id = await this.hypervisor.generateID(this.kernel._opt.parentPort)
81- return this._portMap.get(id)
82- }
83-
8468 // waits till all ports have reached a threshold tick count
8569 async wait (threshold) {
8670 // find the ports that have a smaller tick count then the threshold tick count
8771 const unkownPorts = [...this._portMap].filter(([id, port]) => {
tests/index.jsView
@@ -46,11 +46,11 @@
4646 t.end()
4747 })
4848
4949 tape('one child contract', async t => {
50- const message = new Message()
50+ let message = new Message()
5151 const expectedState = { '/': 'zdpuAqtY43BMaTCB5nTt7kooeKAWibqGs44Uwy9jJQHjTnHRK' }
52- let hasResolved= false
52+ let hasResolved = false
5353
5454 class testVMContainer2 extends BaseContainer {
5555 run (m) {
5656 t.true(m === message, 'should recive a message 2')
@@ -81,8 +81,29 @@
8181 await hypervisor.createStateRoot(port, Infinity)
8282 t.true(hasResolved, 'should resolve before generating the state root')
8383 t.deepEquals(port, expectedState, 'expected state')
8484
85+ class testVMContainer3 extends BaseContainer {
86+ async run (m) {
87+ console.log('here!!')
88+ const port = await this.kernel.getPort(this.kernel.ports, 'child')
89+ await this.kernel.send(port, m)
90+ this.kernel.incrementTicks(1)
91+ }
92+ }
93+
94+ hypervisor.addVM('test', testVMContainer3)
95+
96+ // revive ports
97+ try {
98+ message = new Message()
99+ await hypervisor.graph.tree(expectedState, 1)
100+ console.log(expectedState)
101+ await hypervisor.send(expectedState['/'], message)
102+ } catch (e) {
103+ console.log(e)
104+ }
105+
85106 t.end()
86107 node.stop(() => {
87108 process.exit()
88109 })

Built with git-ssb-web