git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 463dc84f2cfff964ba0c0ea55de6f903a4f344a3

clean up state managment

wanderer committed on 5/7/2017, 5:12:44 PM
Parent: acc114f061e4e4c2cbede55a72d008b7e46c5cbc

Files changed

index.jschanged
kernel.jschanged
portManager.jschanged
tests/index.jschanged
index.jsView
@@ -12,21 +12,18 @@
1212 async getInstance (port) {
1313 let id = await this.generateID(port)
1414 let kernel = this._vmInstances.get(id)
1515 if (!kernel) {
16- // load the container from the state
17- await this.graph.tree(port, 3)
16+ // load the the ID from the merkle store
17+ await this.graph.tree(port.id, 1)
1818 const parentID = await this.generateID({id: port.id['/'].parent})
1919 const parentKernel = await this._vmInstances.get(parentID)
2020 const parentPort = parentKernel.entryPort
2121
2222 kernel = await this.createInstanceFromPort(port, parentPort)
23- // don't delete the root contracts
24- if (id) {
25- kernel.on('idle', () => {
26- this._vmInstances.delete(id)
27- })
28- }
23+ kernel.on('idle', () => {
24+ this._vmInstances.delete(id)
25+ })
2926 }
3027 return kernel
3128 }
3229
@@ -39,10 +36,13 @@
3936
4037 async createInstance (type, state, entryPort = null, parentPort) {
4138 const VM = this._VMs[type]
4239 if (!state) {
43- state = VM.createState()
40+ state = {
41+ '/': VM.createState()
42+ }
4443 }
44+
4545 // create a new kernel instance
4646 const kernel = new Kernel({
4747 entryPort: entryPort,
4848 parentPort: parentPort,
@@ -51,8 +51,9 @@
5151 VM: VM
5252 })
5353
5454 const id = await this.generateID(entryPort)
55+ // save the newly created instance
5556 this._vmInstances.set(id, kernel)
5657 await kernel.start()
5758 return kernel
5859 }
@@ -61,9 +62,9 @@
6162 * opts.entryPort
6263 * opts.parentPort
6364 */
6465 async createInstanceFromPort (entryPort, parentPort) {
65- const state = entryPort.link['/']
66+ const state = entryPort.link
6667 return this.createInstance(entryPort.type, state, entryPort, parentPort)
6768 }
6869
6970 async createStateRoot (container, ticks) {
kernel.jsView
@@ -15,9 +15,9 @@
1515 // create the port manager
1616 this.ports = new PortManager({
1717 kernel: this,
1818 hypervisor: opts.hypervisor,
19- ports: opts.state.ports,
19+ state: opts.state,
2020 entryPort: opts.entryPort,
2121 parentPort: opts.parentPort
2222 })
2323
@@ -53,18 +53,23 @@
5353 this.emit(vmState, message)
5454 }
5555
5656 async _runNextMessage () {
57- const message = await this.ports.getNextMessage()
58- // if the vm is paused and it gets a message; save that message for use when the VM is resumed
59- if (message && this.vmState === 'paused') {
60- this.ports._portMap(message._fromPort).unshfit(message)
61- } else if (!message && this.vmState !== 'paused') {
62- // if no more messages then shut down
63- this._updateVmState('idle')
64- } else {
65- // run the next message
66- this._run(message)
57+ console.log('run next message')
58+ try {
59+ const message = await this.ports.getNextMessage()
60+ // if the vm is paused and it gets a message; save that message for use when the VM is resumed
61+ if (message && this.vmState === 'paused') {
62+ this.ports._portMap(message._fromPort).unshfit(message)
63+ } else if (!message && this.vmState !== 'paused') {
64+ // if no more messages then shut down
65+ this._updateVmState('idle')
66+ } else {
67+ // run the next message
68+ this._run(message)
69+ }
70+ } catch (e) {
71+ console.log(e)
6772 }
6873 }
6974
7075 _updateEntryPort (entryPort) {
@@ -141,13 +146,14 @@
141146
142147 async createPort (type, name) {
143148 const VM = this.hypervisor._VMs[type]
144149 const parentId = this.entryPort ? this.entryPort.id : null
150+ let nonce = await this.hypervisor.graph.get(this.state, 'nonce')
145151 const portRef = {
146152 'messages': [],
147153 'id': {
148154 '/': {
149- nonce: this.state.nonce,
155+ nonce: nonce,
150156 parent: parentId
151157 }
152158 },
153159 'type': type,
@@ -158,11 +164,11 @@
158164
159165 // create the port instance
160166 await this.ports.set(name, portRef)
161167 // incerment the nonce
162- const nonce = new BN(this.state.nonce)
168+ nonce = new BN(nonce)
163169 nonce.iaddn(1)
164- this.state.nonce = nonce.toArray()
170+ this.state['/'].nonce = nonce.toArray()
165171 return portRef
166172 }
167173
168174 async send (portRef, message) {
portManager.jsView
@@ -30,15 +30,16 @@
3030 }
3131
3232 async start () {
3333 // map ports to thier id's
34- let ports = Object.keys(this.ports).map(name => {
34+ this.ports = await this.hypervisor.graph.get(this.state, 'ports')
35+ const promises = Object.keys(this.ports).map(name => {
3536 const port = this.ports[name]
3637 this._mapPort(name, port)
3738 })
3839
3940 // create the parent port
40- await Promise.all(ports)
41+ await Promise.all(promises)
4142 // skip the root, since it doesn't have a parent
4243 if (this.parentPort !== undefined) {
4344 const id = await this.hypervisor.generateID(this.parentPort)
4445 this._portMap.set(id, new Port(ENTRY))
tests/index.jsView
@@ -53,9 +53,11 @@
5353 })
5454
5555 tape('one child contract', async t => {
5656 let message = new Message()
57- const expectedState = { '/': 'zdpuAqtY43BMaTCB5nTt7kooeKAWibqGs44Uwy9jJQHjTnHRK' }
57+ const expectedState = {
58+ '/': 'zdpuAtYQujwQMR9SpmFwmkr7d2cD4vzeQk2GCzcEku2nomWj6'
59+ }
5860 let hasResolved = false
5961
6062 class testVMContainer2 extends BaseContainer {
6163 run (m) {
@@ -71,53 +73,49 @@
7173 }
7274
7375 class testVMContainer extends BaseContainer {
7476 async run (m) {
75- // console.log('here', this.kernel.entryPort)
7677 const port = await this.kernel.createPort('test2', 'child')
77- try {
78- await this.kernel.send(port, m)
79- } catch (e) {
80- console.log('error!', e)
81- }
78+ await this.kernel.send(port, m)
8279 this.kernel.incrementTicks(1)
8380 }
8481 }
8582
83+ const hypervisor = new Hypervisor({dag: node.dag})
84+ hypervisor.registerContainer('test', testVMContainer)
85+ hypervisor.registerContainer('test2', testVMContainer2)
86+
87+ let root = await hypervisor.createInstance('test')
88+ let port = await root.createPort('test', 'first')
89+
90+ await root.send(port, message)
91+ const stateRoot = await hypervisor.createStateRoot(root, Infinity)
92+ t.true(hasResolved, 'should resolve before generating the state root')
93+ t.deepEquals(stateRoot, expectedState, 'expected state')
94+
95+ // test reviving the state
96+ class testVMContainer3 extends BaseContainer {
97+ async run (m) {
98+ const port = this.kernel.getPort('child')
99+ this.kernel.send(port, m)
100+ this.kernel.incrementTicks(1)
101+ }
102+ }
103+
86104 try {
87- const hypervisor = new Hypervisor({dag: node.dag})
88- hypervisor.registerContainer('test', testVMContainer)
89- hypervisor.registerContainer('test2', testVMContainer2)
105+ hypervisor.registerContainer('test', testVMContainer3)
106+ root = await hypervisor.createInstance('test', stateRoot)
107+ port = await root.ports.getRef('first')
90108
91- const root = await hypervisor.createInstance('test')
92- const port = await root.createPort('test', 'first')
93-
94109 await root.send(port, message)
110+ console.log('creating SR')
95111 await hypervisor.createStateRoot(root, Infinity)
96- console.log('state root generated')
97- t.true(hasResolved, 'should resolve before generating the state root')
112+ console.log('end!')
113+ // console.log(hypervisor._vmInstances)
98114 } catch (e) {
99115 console.log(e)
100116 }
101- // t.deepEquals(port, expectedState, 'expected state')
102117
103- // test reviving the state
104- // class testVMContainer3 extends BaseContainer {
105- // async run (m) {
106- // const port = this.kernel.getPort(this.kernel.ports, 'child')
107- // this.kernel.send(port, m)
108- // this.kernel.incrementTicks(1)
109- // }
110- // }
111-
112- // hypervisor.addVM('test', testVMContainer3)
113-
114- // // revive ports
115- // message = new Message()
116- // await hypervisor.graph.tree(expectedState, 1)
117- // await hypervisor.send(expectedState['/'], message)
118- // await hypervisor.createStateRoot(expectedState['/'], Infinity)
119-
120118 t.end()
121119 })
122120
123121 tape.skip('should wait on parent', async t => {

Built with git-ssb-web