Commit ca80f013bf0ad297f4a0e0ab0c5a57b5c3b84034
fix aribiter
wanderer committed on 4/28/2017, 5:04:44 PMParent: b95b6a5752781c6cea64703d51e548ffddfb48ac
Files changed
index.js | changed |
kernel.js | changed |
package.json | changed |
portManager.js | changed |
tests/index.js | changed |
index.js | ||
---|---|---|
@@ -1,5 +1,6 @@ | ||
1 | 1 | const Graph = require('ipld-graph-builder') |
2 | +const multibase = require('multibase') | |
2 | 3 | const Kernel = require('./kernel.js') |
3 | 4 | |
4 | 5 | module.exports = class Hypervisor { |
5 | 6 | constructor (opts) { |
@@ -18,11 +19,8 @@ | ||
18 | 19 | let kernel = this._vmInstances.get(id) |
19 | 20 | if (!kernel) { |
20 | 21 | // load the container from the state |
21 | 22 | await this.graph.tree(port, 2) |
22 | - // if (port['/']) { | |
23 | - // port = port['/'] | |
24 | - // } | |
25 | 23 | |
26 | 24 | // create a new kernel instance |
27 | 25 | const VM = this._opts.VMs[port.type] |
28 | 26 | |
@@ -54,18 +52,18 @@ | ||
54 | 52 | let kernel = await this.getInstance(port) |
55 | 53 | return kernel.wait(threshold) |
56 | 54 | } |
57 | 55 | |
58 | - createPort (type, payload = {}, id = {nonce: [0], parent: null}) { | |
56 | + createPort (type, id = {nonce: [0], parent: null}) { | |
59 | 57 | const VM = this._opts.VMs[type] |
60 | 58 | return { |
61 | 59 | 'messages': [], |
62 | 60 | 'id': { |
63 | 61 | '/': id |
64 | 62 | }, |
65 | 63 | 'type': type, |
66 | 64 | 'link': { |
67 | - '/': VM.createState(payload) | |
65 | + '/': VM.createState() | |
68 | 66 | } |
69 | 67 | } |
70 | 68 | } |
71 | 69 | |
@@ -74,10 +72,14 @@ | ||
74 | 72 | return this.graph.flush(port) |
75 | 73 | } |
76 | 74 | |
77 | 75 | 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 | |
80 | 82 | } |
81 | 83 | |
82 | 84 | addVM (type, vm) { |
83 | 85 | this._opts.VMs[type] = vm |
kernel.js | ||
---|---|---|
@@ -100,22 +100,26 @@ | ||
100 | 100 | } |
101 | 101 | } |
102 | 102 | } |
103 | 103 | |
104 | - async createPort (manager, type, name, payload) { | |
104 | + async createPort (manager, type, name) { | |
105 | 105 | // incerment the nonce |
106 | 106 | const nonce = new BN(this.state.nonce) |
107 | 107 | nonce.iaddn(1) |
108 | 108 | this.state.nonce = nonce.toArray() |
109 | 109 | |
110 | - let port = this._opts.hypervisor.createPort(type, payload, { | |
110 | + let port = this._opts.hypervisor.createPort(type, { | |
111 | 111 | nonce: this.state.nonce, |
112 | 112 | parent: this._opts.parentPort.id |
113 | 113 | }) |
114 | 114 | await manager.set(name, port) |
115 | 115 | return port |
116 | 116 | } |
117 | 117 | |
118 | + getPort (manager, name) { | |
119 | + return manager.get(name) | |
120 | + } | |
121 | + | |
118 | 122 | async send (port, message) { |
119 | 123 | message._ticks = this.ticks |
120 | 124 | const portObject = await this.ports.get(port) |
121 | 125 | portObject.hasSent = true |
package.json | ||
---|---|---|
@@ -29,8 +29,9 @@ | ||
29 | 29 | "bn.js": "^4.11.6", |
30 | 30 | "deepcopy": "^0.6.3", |
31 | 31 | "fastpriorityqueue": "^0.2.4", |
32 | 32 | "ipld-graph-builder": "1.0.1", |
33 | + "multibase": "^0.3.4", | |
33 | 34 | "primea-message": "0.0.0" |
34 | 35 | }, |
35 | 36 | "devDependencies": { |
36 | 37 | "coveralls": "^2.13.0", |
portManager.js | ||
---|---|---|
@@ -1,28 +1,26 @@ | ||
1 | 1 | const Port = require('./port.js') |
2 | 2 | const PARENT = Symbol('parent') |
3 | 3 | |
4 | 4 | // 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() | |
10 | 8 | |
11 | 9 | if (!a) { |
12 | - return b | |
10 | + return pairB | |
13 | 11 | } else if (!b) { |
14 | - return a | |
12 | + return pairA | |
15 | 13 | } |
16 | 14 | |
17 | 15 | const aGasPrice = a.resources.gasPrice |
18 | 16 | const bGasPrice = b.resources.gasPrice |
19 | 17 | if (a.ticks !== b.ticks) { |
20 | - return a.ticks < b.ticks ? a : b | |
18 | + return a.ticks < b.ticks ? pairA : pairB | |
21 | 19 | } else if (aGasPrice === bGasPrice) { |
22 | - return a.hash() > b.hash() ? a : b | |
20 | + return a.hash() > b.hash() ? pairA : pairB | |
23 | 21 | } else { |
24 | - return aGasPrice > bGasPrice ? a : b | |
22 | + return aGasPrice > bGasPrice ? pairA : pairB | |
25 | 23 | } |
26 | 24 | } |
27 | 25 | |
28 | 26 | module.exports = class PortManager { |
@@ -61,27 +59,13 @@ | ||
61 | 59 | this.ports[name] = port |
62 | 60 | return this._mapPort(name, port) |
63 | 61 | } |
64 | 62 | |
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 | - | |
74 | 63 | async get (port) { |
75 | 64 | const id = await this.hypervisor.generateID(port) |
76 | 65 | return this._portMap.get(id) |
77 | 66 | } |
78 | 67 | |
79 | - async getParent () { | |
80 | - const id = await this.hypervisor.generateID(this.kernel._opt.parentPort) | |
81 | - return this._portMap.get(id) | |
82 | - } | |
83 | - | |
84 | 68 | // waits till all ports have reached a threshold tick count |
85 | 69 | async wait (threshold) { |
86 | 70 | // find the ports that have a smaller tick count then the threshold tick count |
87 | 71 | const unkownPorts = [...this._portMap].filter(([id, port]) => { |
tests/index.js | ||
---|---|---|
@@ -46,11 +46,11 @@ | ||
46 | 46 | t.end() |
47 | 47 | }) |
48 | 48 | |
49 | 49 | tape('one child contract', async t => { |
50 | - const message = new Message() | |
50 | + let message = new Message() | |
51 | 51 | const expectedState = { '/': 'zdpuAqtY43BMaTCB5nTt7kooeKAWibqGs44Uwy9jJQHjTnHRK' } |
52 | - let hasResolved= false | |
52 | + let hasResolved = false | |
53 | 53 | |
54 | 54 | class testVMContainer2 extends BaseContainer { |
55 | 55 | run (m) { |
56 | 56 | t.true(m === message, 'should recive a message 2') |
@@ -81,8 +81,29 @@ | ||
81 | 81 | await hypervisor.createStateRoot(port, Infinity) |
82 | 82 | t.true(hasResolved, 'should resolve before generating the state root') |
83 | 83 | t.deepEquals(port, expectedState, 'expected state') |
84 | 84 | |
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 | + | |
85 | 106 | t.end() |
86 | 107 | node.stop(() => { |
87 | 108 | process.exit() |
88 | 109 | }) |
Built with git-ssb-web