Commit b95b6a5752781c6cea64703d51e548ffddfb48ac
fixed port generation
wanderer committed on 4/28/2017, 11:48:04 AMParent: 535100729b49473f07a4c560fa6b81aa2b369544
Files changed
index.js | changed |
kernel.js | changed |
portManager.js | changed |
tests/index.js | changed |
index.js | ||
---|---|---|
@@ -51,10 +51,9 @@ | ||
51 | 51 | // given a port, wait untill its source contract has reached the threshold |
52 | 52 | // tick count |
53 | 53 | async wait (port, threshold) { |
54 | 54 | let kernel = await this.getInstance(port) |
55 | - await kernel.wait(threshold) | |
56 | - return kernel | |
55 | + return kernel.wait(threshold) | |
57 | 56 | } |
58 | 57 | |
59 | 58 | createPort (type, payload = {}, id = {nonce: [0], parent: null}) { |
60 | 59 | const VM = this._opts.VMs[type] |
kernel.js | ||
---|---|---|
@@ -16,11 +16,9 @@ | ||
16 | 16 | return a.threshold > b.threshold |
17 | 17 | }) |
18 | 18 | this.on('result', this._runNextMessage) |
19 | 19 | this.on('idle', () => { |
20 | - console.log('idle') | |
21 | 20 | while (!this._waitingQueue.isEmpty()) { |
22 | - console.log('clering ') | |
23 | 21 | this._waitingQueue.poll().resolve() |
24 | 22 | } |
25 | 23 | }) |
26 | 24 | } |
@@ -36,14 +34,14 @@ | ||
36 | 34 | |
37 | 35 | queue (message) { |
38 | 36 | this.ports.queue(message) |
39 | 37 | if (this.vmState === 'idle') { |
38 | + this._updateVmState('running') | |
40 | 39 | this._runNextMessage() |
41 | 40 | } |
42 | 41 | } |
43 | 42 | |
44 | 43 | _runNextMessage () { |
45 | - this._updateVmState('running') | |
46 | 44 | this.ports.getNextMessage(this.ticks).then(message => { |
47 | 45 | if (message) { |
48 | 46 | this.run(message) |
49 | 47 | } else { |
@@ -63,9 +61,8 @@ | ||
63 | 61 | let result |
64 | 62 | try { |
65 | 63 | result = await this.vm.run(message) || {} |
66 | 64 | } catch (e) { |
67 | - console.log(e) | |
68 | 65 | result = { |
69 | 66 | exception: true, |
70 | 67 | exceptionError: e |
71 | 68 | } |
@@ -105,19 +102,15 @@ | ||
105 | 102 | } |
106 | 103 | |
107 | 104 | async createPort (manager, type, name, payload) { |
108 | 105 | // incerment the nonce |
109 | - const nonce = new BN(this._opts.state.nonce) | |
106 | + const nonce = new BN(this.state.nonce) | |
110 | 107 | nonce.iaddn(1) |
111 | - this.state.nonce = nonce.toArrayLike(Buffer) | |
108 | + this.state.nonce = nonce.toArray() | |
112 | 109 | |
113 | - const parentID = await this._opts.hypervisor.generateID({ | |
114 | - id: this._opts.id | |
115 | - }) | |
116 | - | |
117 | 110 | 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 | |
120 | 113 | }) |
121 | 114 | await manager.set(name, port) |
122 | 115 | return port |
123 | 116 | } |
portManager.js | ||
---|---|---|
@@ -87,20 +87,14 @@ | ||
87 | 87 | const unkownPorts = [...this._portMap].filter(([id, port]) => { |
88 | 88 | return (port.hasSent || port.name === PARENT) && port.ticks < threshold |
89 | 89 | }) |
90 | 90 | |
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) | |
101 | 95 | }) |
102 | - return await Promise.all(promises) | |
96 | + return Promise.all(promises) | |
103 | 97 | } |
104 | 98 | |
105 | 99 | async getNextMessage (ticks) { |
106 | 100 | await this.wait(ticks) |
tests/index.js | ||
---|---|---|
@@ -22,48 +22,43 @@ | ||
22 | 22 | console.log(err) |
23 | 23 | }) |
24 | 24 | |
25 | 25 | node.on('start', () => { |
26 | - tape.only('basic', async t => { | |
26 | + tape('basic', async t => { | |
27 | 27 | const message = new Message() |
28 | 28 | const expectedState = { |
29 | - '/': 'zdpuAn1R5shTypKNBHT8Js2uBnbUcujHfnPNrKbKRNL1AyAt5' | |
29 | + '/': 'zdpuB3eZQJuXMnQrdiF5seMvx3zC2xT1EqrQScoPcTs8ESxYx' | |
30 | 30 | } |
31 | 31 | |
32 | 32 | class testVMContainer extends BaseContainer { |
33 | 33 | run (m) { |
34 | 34 | t.true(m === message, 'should recive a message') |
35 | 35 | } |
36 | 36 | } |
37 | 37 | |
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') | |
42 | 41 | |
43 | - await hypervisor.send(port, message) | |
44 | - await hypervisor.createStateRoot(port, Infinity) | |
42 | + await hypervisor.send(port, message) | |
43 | + await hypervisor.createStateRoot(port, Infinity) | |
45 | 44 | |
46 | - t.deepEquals(port, expectedState, 'expected') | |
47 | - } catch (e) { | |
48 | - console.log(e) | |
49 | - } | |
45 | + t.deepEquals(port, expectedState, 'expected') | |
50 | 46 | t.end() |
51 | 47 | }) |
52 | 48 | |
53 | 49 | tape('one child contract', async t => { |
54 | 50 | const message = new Message() |
55 | - const expectedState = { | |
56 | - '/': 'zdpuAwUPELiXpnd66Wum84VRPEsUGB7cUuxUESDMXmpVj6prc' | |
57 | - } | |
51 | + const expectedState = { '/': 'zdpuAqtY43BMaTCB5nTt7kooeKAWibqGs44Uwy9jJQHjTnHRK' } | |
52 | + let hasResolved= false | |
58 | 53 | |
59 | 54 | class testVMContainer2 extends BaseContainer { |
60 | 55 | run (m) { |
61 | 56 | t.true(m === message, 'should recive a message 2') |
62 | 57 | return new Promise((resolve, reject) => { |
63 | 58 | setTimeout(() => { |
64 | - console.log('resolve!!') | |
65 | 59 | this.kernel.incrementTicks(1) |
60 | + hasResolved = true | |
66 | 61 | resolve() |
67 | 62 | }, 200) |
68 | 63 | }) |
69 | 64 | } |
@@ -76,27 +71,20 @@ | ||
76 | 71 | this.kernel.incrementTicks(1) |
77 | 72 | } |
78 | 73 | } |
79 | 74 | |
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') | |
85 | 79 | |
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') | |
89 | 84 | |
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 | - | |
97 | 85 | t.end() |
98 | 86 | node.stop(() => { |
99 | - // process.exit() | |
87 | + process.exit() | |
100 | 88 | }) |
101 | 89 | }) |
102 | 90 | }) |
Built with git-ssb-web