Commit 3820100dc50eb86a7f6f941410dc7e8dd2c7aaa7
move port creation to hypervisor
wanderer committed on 4/26/2017, 3:48:24 PMParent: a0b1792b4abd832c8abb3959057e090cd3e33741
Files changed
index.js | changed |
kernel.js | changed |
portManager.js | changed |
tests/index.js | changed |
index.js | ||
---|---|---|
@@ -23,9 +23,9 @@ | ||
23 | 23 | |
24 | 24 | // create a new kernel instance |
25 | 25 | const VM = this._opts.VMs[port.type] |
26 | 26 | const opts = Object.assign({ |
27 | - state: port.vm, | |
27 | + state: port.link, | |
28 | 28 | id: port.id, |
29 | 29 | VM: VM |
30 | 30 | }, this._opts) |
31 | 31 | |
@@ -40,9 +40,10 @@ | ||
40 | 40 | } |
41 | 41 | |
42 | 42 | async send (port, message) { |
43 | 43 | const vm = await this.getInstance(port) |
44 | - message._fromPort = 'root' | |
44 | + const id = await this.generateID(port) | |
45 | + message._fromPort = id | |
45 | 46 | vm.queue(message) |
46 | 47 | } |
47 | 48 | |
48 | 49 | // given a port, wait untill its source contract has reached the threshold |
@@ -52,15 +53,29 @@ | ||
52 | 53 | await kernel.wait(ticks) |
53 | 54 | return kernel |
54 | 55 | } |
55 | 56 | |
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 | + | |
56 | 71 | async createStateRoot (port, ticks) { |
57 | 72 | await this.wait(port, ticks) |
58 | 73 | return this.graph.flush(port) |
59 | 74 | } |
60 | 75 | |
61 | 76 | generateID (port) { |
62 | - this.graph.flush(port.id) | |
77 | + return this.graph.flush(port.id) | |
63 | 78 | } |
64 | 79 | |
65 | 80 | addVM (type, vm) { |
66 | 81 | this._opts.VMs[type] = vm |
kernel.js | ||
---|---|---|
@@ -55,8 +55,9 @@ | ||
55 | 55 | let result |
56 | 56 | try { |
57 | 57 | result = await this.vm.run(message) || {} |
58 | 58 | } catch (e) { |
59 | + console.log(e) | |
59 | 60 | result = { |
60 | 61 | exception: true, |
61 | 62 | exceptionError: e |
62 | 63 | } |
@@ -98,23 +99,24 @@ | ||
98 | 99 | this._waitingQueue.poll().resolve(count) |
99 | 100 | } |
100 | 101 | } |
101 | 102 | |
102 | - createPort () { | |
103 | + async createPort (manager, type, name, payload) { | |
104 | + // incerment the nonce | |
103 | 105 | const nonce = new BN(this.nonce) |
104 | 106 | nonce.iaddn(1) |
105 | 107 | 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 | |
117 | 119 | } |
118 | 120 | |
119 | 121 | async send (port, message) { |
120 | 122 | message._ticks = this.ticks |
portManager.js | ||
---|---|---|
@@ -33,28 +33,29 @@ | ||
33 | 33 | async start () { |
34 | 34 | // map ports to thier id's |
35 | 35 | let ports = Object.keys(this.ports).map(name => { |
36 | 36 | const port = this.ports[name] |
37 | - this.hypervisor.generateID(port).then(id => { | |
38 | - return [id, new Port(name)] | |
39 | - }) | |
37 | + this._mapPort(name, port) | |
40 | 38 | }) |
41 | 39 | |
42 | 40 | // 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')) | |
49 | 43 | } |
50 | 44 | |
45 | + _mapPort (name, port) { | |
46 | + this.hypervisor.generateID(port).then(id => { | |
47 | + this._portMap.set(id, new Port(name)) | |
48 | + }) | |
49 | + } | |
50 | + | |
51 | 51 | queue (message) { |
52 | 52 | this._portMap.get(message.fromPort).queue(message) |
53 | 53 | } |
54 | 54 | |
55 | - create (name, value) { | |
56 | - this.ports[name] = value | |
55 | + set (name, port) { | |
56 | + this.ports[name] = port | |
57 | + return this._mapPort(name, port) | |
57 | 58 | } |
58 | 59 | |
59 | 60 | del (name) { |
60 | 61 | delete this.ports[name] |
tests/index.js | ||
---|---|---|
@@ -3,107 +3,80 @@ | ||
3 | 3 | const Hypervisor = require('../') |
4 | 4 | const Message = require('primea-message') |
5 | 5 | |
6 | 6 | 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 | + | |
7 | 17 | node.on('error', err => { |
8 | 18 | console.log(err) |
9 | 19 | }) |
10 | 20 | |
11 | 21 | node.on('start', () => { |
12 | - tape('basic', async t => { | |
22 | + tape.only('basic', async t => { | |
13 | 23 | 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' } | |
32 | 25 | |
33 | - class testVMContainer { | |
26 | + class testVMContainer extends BaseContainer { | |
34 | 27 | run (m) { |
35 | 28 | t.true(m === message, 'should recive a message') |
36 | 29 | } |
37 | 30 | } |
38 | 31 | |
39 | - const hypervisor = new Hypervisor({ | |
40 | - dag: node.dag | |
41 | - }) | |
32 | + const hypervisor = new Hypervisor({dag: node.dag}) | |
42 | 33 | hypervisor.addVM('test', testVMContainer) |
34 | + const port = hypervisor.createPort('test') | |
43 | 35 | |
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)) | |
47 | 41 | t.end() |
48 | 42 | }) |
49 | 43 | |
50 | 44 | tape('one child contract', async t => { |
51 | 45 | t.end() |
52 | - node.stop(() => { | |
53 | - process.exit() | |
54 | - }) | |
55 | - | |
56 | 46 | const message = new Message() |
57 | - class testVMContainer { | |
58 | - constuctor (kernel) { | |
59 | - this.kernel = kernel | |
60 | - } | |
47 | + const expectedState = { '/': 'zdpuAwqyF4X1hAHMBcsn7eDJXcLfcyoyEWWR73eeqXXmFkBe3' } | |
61 | 48 | |
49 | + class testVMContainer2 extends BaseContainer { | |
62 | 50 | 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') | |
65 | 53 | } |
66 | 54 | } |
67 | 55 | |
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 | |
71 | 60 | } |
72 | - } | |
73 | 61 | |
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) | |
89 | 66 | } |
90 | 67 | } |
91 | 68 | |
92 | - const hypervisor = new Hypervisor({ | |
93 | - dag: node.dag | |
94 | - }) | |
69 | + const hypervisor = new Hypervisor({dag: node.dag}) | |
95 | 70 | hypervisor.addVM('test', testVMContainer) |
71 | + hypervisor.addVM('test2', testVMContainer2) | |
72 | + const port = hypervisor.createPort('test') | |
96 | 73 | |
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') | |
107 | 77 | |
78 | + node.stop(() => { | |
79 | + process.exit() | |
80 | + }) | |
108 | 81 | }) |
109 | 82 | }) |
Built with git-ssb-web