git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit c8b8cb6b234bc3f3da63ae5c0df00e0a2b1fa4d3

accounts

Signed-off-by: wanderer <mjbecze@gmail.com>
wanderer committed on 12/2/2017, 4:48:40 AM
Parent: 02590e3b2af8b20c695797eea8f937fc1a94240a

Files changed

actor.jschanged
index.jschanged
package-lock.jsonchanged
tests/index.jschanged
capsStore.jsdeleted
actor.jsView
@@ -1,7 +1,6 @@
11 const Message = require('primea-message')
22 const LockMap = require('lockmap')
3-const CapsStore = require('./capsStore.js')
43 const Inbox = require('./inbox.js')
54
65 module.exports = class Actor {
76 /**
@@ -13,11 +12,11 @@
1312 * @param {Object} opts.hypervisor - the instance of the hypervisor
1413 * @param {Object} opts.container - the container constuctor and argments
1514 */
1615 constructor (opts) {
17- this.state = opts.state.value
18- this.code = opts.state.value.code
19- this.treeNode = opts.state.node
16+ // console.log(opts.state)
17+ this.state = opts.state
18+ this.nonce = opts.state.root['/'][3][2]
2019 this.hypervisor = opts.hypervisor
2120 this.id = opts.id
2221 this.container = new opts.container.Constructor(this, opts.container.args)
2322 this.inbox = new Inbox({
@@ -27,10 +26,8 @@
2726
2827 this.ticks = 0
2928 this.running = false
3029 this._sending = new LockMap()
31-
32- this.caps = new CapsStore(opts.state.value.caps)
3330 }
3431
3532 /**
3633 * Mints a new capabilitly with a given tag
@@ -83,8 +80,10 @@
8380 this.hypervisor.scheduler.update(this)
8481 }
8582 // run the next message
8683 await this.runMessage(message)
84+ // wait for state ops to finish
85+ await this.state.done()
8786 }
8887
8988 this.running = false
9089 this.container.onIdle()
@@ -94,8 +93,10 @@
9493 /**
9594 * Runs the shutdown routine for the actor
9695 */
9796 shutdown () {
97+ // save the nonce
98+ this.state.root['/'][3][2] = this.nonce
9899 this.hypervisor.scheduler.done(this.id)
99100 }
100101
101102 /**
@@ -153,13 +154,13 @@
153154 }
154155
155156 _generateNextId () {
156157 const id = {
157- nonce: this.state.nonce,
158+ nonce: this.nonce,
158159 parent: this.id
159160 }
160161
161- this.state.nonce++
162+ this.nonce++
162163 return id
163164 }
164165
165166 /**
index.jsView
@@ -6,13 +6,13 @@
66 * The Hypervisor manages the container instances by instantiating them and
77 * destorying them when possible. It also facilitates localating Containers
88 * @param {Tree} tree - a [radix tree](https://github.com/dfinity/js-dfinity-radix-tree) to store the state
99 */
10- constructor (tree) {
10+ constructor (tree, nonce = 0) {
1111 this.tree = tree
1212 this.scheduler = new Scheduler()
1313 this._containerTypes = {}
14- this.nonce = 0
14+ this.nonce = nonce
1515 }
1616
1717 /**
1818 * sends a message
@@ -27,17 +27,17 @@
2727 }
2828
2929 // loads an instance of a container from the state
3030 async _loadActor (id) {
31- const state = await this.tree.get(id, true)
32- const container = this._containerTypes[state.value.type]
31+ const state = await this.tree.getSubTree(id)
32+ const container = this._containerTypes[state.root['/'][3][0]]
3333
3434 // create a new actor instance
3535 const actor = new Actor({
3636 hypervisor: this,
37- state: state,
38- container: container,
39- id: id
37+ state,
38+ container,
39+ id
4040 })
4141
4242 // save the newly created instance
4343 this.scheduler.update(actor)
@@ -70,21 +70,12 @@
7070 */
7171 async createActor (type, message, id = {nonce: this.nonce++, parent: null}) {
7272 const encoded = encodedID(id)
7373 const idHash = await this._getHashFromObj(encoded)
74- const state = {
75- nonce: 0,
76- caps: {},
77- type: type
78- }
74+ const state = Buffer.from([type, 0, 0])
7975
80- const code = message.data
81- if (code.length) {
82- state.code = code
83- }
84-
8576 // save the container in the state
86- await this.tree.set(idHash, state)
77+ this.tree.set(idHash, state)
8778
8879 // create the container instance
8980 const instance = await this._loadActor(idHash)
9081
@@ -103,9 +94,9 @@
10394 * ticks
10495 * @param {Number} ticks the number of ticks at which to create the state root
10596 * @returns {Promise}
10697 */
107- async createStateRoot (ticks) {
98+ async createStateRoot (ticks = Infinity) {
10899 await this.scheduler.wait(ticks)
109100 return this.tree.flush()
110101 }
111102
package-lock.jsonView
The diff is too large to show. Use a local git client to view these changes.
Old file size: 347465 bytes
New file size: 347408 bytes
tests/index.jsView
@@ -1,9 +1,8 @@
11 const tape = require('tape')
22 const AbstractContainer = require('primea-abstract-container')
33 const Message = require('primea-message')
44 const Hypervisor = require('../')
5-const CapsStore = require('../capsStore.js')
65
76 const level = require('level-browserify')
87 const RadixTree = require('dfinity-radix-tree')
98 const db = level('./testdb')
@@ -18,9 +17,9 @@
1817 tape('basic', async t => {
1918 t.plan(2)
2019 let message
2120 const expectedState = {
22- '/': Buffer.from('70a9676b7995b108057bd29955e3874401aa5ba7', 'hex')
21+ '/': Buffer.from('a364c55f9993e0bc63f7155d5eb661ae9ad769d9', 'hex')
2322 }
2423
2524 const tree = new RadixTree({
2625 db: db
@@ -39,29 +38,17 @@
3938
4039 message = new Message()
4140 hypervisor.send(rootCap, message)
4241
43- const stateRoot = await hypervisor.createStateRoot(Infinity)
42+ const stateRoot = await hypervisor.createStateRoot()
4443 t.deepEquals(stateRoot, expectedState, 'expected root!')
4544 })
4645
47-tape('caps manager', async t => {
48- const capsManager = new CapsStore({})
49- const cap = {}
50- capsManager.store('test', cap)
51- const c = capsManager.load('test')
52- t.equals(cap, c)
53- capsManager.delete('test')
54- const empty = capsManager.load('test')
55- t.equals(empty, undefined)
56- t.end()
57-})
58-
5946 tape('two communicating actors', async t => {
6047 t.plan(2)
6148 let message
6249 const expectedState = {
63- '/': Buffer.from('fc935489953ed357f06171dd23439d83190b3a1b', 'hex')
50+ '/': Buffer.from('9e8d5671e2c7d167e03784e5d9ec36e15747ad95', 'hex')
6451 }
6552
6653 const tree = new RadixTree({
6754 db: db
@@ -92,18 +79,17 @@
9279 await hypervisor.createActor(testVMContainerA.typeId, new Message({
9380 caps: [capB]
9481 }))
9582
96- const stateRoot = await hypervisor.createStateRoot(Infinity)
97-
83+ const stateRoot = await hypervisor.createStateRoot()
9884 t.deepEquals(stateRoot, expectedState, 'expected root!')
9985 })
10086
10187 tape('three communicating actors', async t => {
10288 t.plan(3)
10389 let message
10490 const expectedState = {
105- '/': Buffer.from('24855a8efa9af536f0f9b319c05b10d6b7cae6c8', 'hex')
91+ '/': Buffer.from('840607eafe779858648d3311039f986e68f4752e', 'hex')
10692 }
10793
10894 const tree = new RadixTree({
10995 db: db
@@ -138,18 +124,17 @@
138124 await hypervisor.createActor(testVMContainerA.typeId, new Message({
139125 caps: [capB]
140126 }))
141127
142- const stateRoot = await hypervisor.createStateRoot(Infinity)
143-
128+ const stateRoot = await hypervisor.createStateRoot()
144129 t.deepEquals(stateRoot, expectedState, 'expected root!')
145130 })
146131
147132 tape('three communicating actors, with tick counting', async t => {
148133 t.plan(3)
149134 let message
150135 const expectedState = {
151- '/': Buffer.from('24855a8efa9af536f0f9b319c05b10d6b7cae6c8', 'hex')
136+ '/': Buffer.from('840607eafe779858648d3311039f986e68f4752e', 'hex')
152137 }
153138
154139 const tree = new RadixTree({
155140 db: db
@@ -188,18 +173,18 @@
188173 await hypervisor.createActor(testVMContainerA.typeId, new Message({
189174 caps: [capB]
190175 }))
191176
192- const stateRoot = await hypervisor.createStateRoot(Infinity)
177+ const stateRoot = await hypervisor.createStateRoot()
193178
194179 t.deepEquals(stateRoot, expectedState, 'expected root!')
195180 })
196181
197182 tape('response caps', async t => {
198183 t.plan(3)
199184 let message
200185 const expectedState = {
201- '/': Buffer.from('fc935489953ed357f06171dd23439d83190b3a1b', 'hex')
186+ '/': Buffer.from('9e8d5671e2c7d167e03784e5d9ec36e15747ad95', 'hex')
202187 }
203188
204189 const tree = new RadixTree({
205190 db: db
@@ -235,18 +220,18 @@
235220 await hypervisor.createActor(testVMContainerA.typeId, new Message({
236221 caps: [capB]
237222 }))
238223
239- const stateRoot = await hypervisor.createStateRoot(Infinity)
224+ const stateRoot = await hypervisor.createStateRoot()
240225
241226 t.deepEquals(stateRoot, expectedState, 'expected root!')
242227 })
243228
244229 tape('response caps for errors', async t => {
245230 t.plan(3)
246231 let message
247232 const expectedState = {
248- '/': Buffer.from('fc935489953ed357f06171dd23439d83190b3a1b', 'hex')
233+ '/': Buffer.from('9e8d5671e2c7d167e03784e5d9ec36e15747ad95', 'hex')
249234 }
250235
251236 const tree = new RadixTree({
252237 db: db
@@ -283,18 +268,17 @@
283268 await hypervisor.createActor(testVMContainerA.typeId, new Message({
284269 caps: [capB]
285270 }))
286271
287- const stateRoot = await hypervisor.createStateRoot(Infinity)
288-
272+ const stateRoot = await hypervisor.createStateRoot()
289273 t.deepEquals(stateRoot, expectedState, 'expected root!')
290274 })
291275
292276 tape('actor creation', async t => {
293277 t.plan(2)
294278 let message
295279 const expectedState = {
296- '/': Buffer.from('8e809b10d473ef4592dc5c1683e89bc7001e5e3e', 'hex')
280+ '/': Buffer.from('b19c67aea0ff97e96df6e2aacbd45b1bd260af30', 'hex')
297281 }
298282
299283 const tree = new RadixTree({
300284 db: db
@@ -329,17 +313,16 @@
329313 hypervisor.registerContainer(testVMContainerB)
330314
331315 await hypervisor.createActor(testVMContainerA.typeId, new Message())
332316
333- const stateRoot = await hypervisor.createStateRoot(Infinity)
334-
317+ const stateRoot = await hypervisor.createStateRoot()
335318 t.deepEquals(stateRoot, expectedState, 'expected root!')
336319 })
337320
338321 tape('simple message arbiter test', async t => {
339322 t.plan(4)
340323 const expectedState = {
341- '/': Buffer.from('fc935489953ed357f06171dd23439d83190b3a1b', 'hex')
324+ '/': Buffer.from('9e8d5671e2c7d167e03784e5d9ec36e15747ad95', 'hex')
342325 }
343326
344327 const tree = new RadixTree({
345328 db: db
@@ -391,18 +374,17 @@
391374 await hypervisor.createActor(testVMContainerA.typeId, new Message({
392375 caps: [capB]
393376 }))
394377
395- const stateRoot = await hypervisor.createStateRoot(Infinity)
396-
378+ const stateRoot = await hypervisor.createStateRoot()
397379 t.deepEquals(stateRoot, expectedState, 'expected root!')
398380 })
399381
400382 tape('arbiter test for id comparision', async t => {
401383 t.plan(4)
402384 let message
403385 const expectedState = {
404- '/': Buffer.from('0866fe6a6adaf28c51ce99ddfddd49c492e9ce48', 'hex')
386+ '/': Buffer.from('840607eafe779858648d3311039f986e68f4752e', 'hex')
405387 }
406388
407389 const tree = new RadixTree({
408390 db: db
@@ -454,17 +436,16 @@
454436 caps: [capB],
455437 data: 'third'
456438 }))
457439
458- const stateRoot = await hypervisor.createStateRoot(Infinity)
459-
440+ const stateRoot = await hypervisor.createStateRoot()
460441 t.deepEquals(stateRoot, expectedState, 'expected root!')
461442 })
462443
463444 tape('basic tagged caps', async t => {
464445 t.plan(4)
465446 const expectedState = {
466- '/': Buffer.from('ef403643f292108fe9edc1700d80a7bf2402e7a0', 'hex')
447+ '/': Buffer.from('d4291da4536544bf90aa473a1148cb29f913d078', 'hex')
467448 }
468449
469450 const tree = new RadixTree({
470451 db: db
@@ -500,17 +481,16 @@
500481 let capB = await hypervisor.createActor(testVMContainerB.typeId, new Message())
501482
502483 await hypervisor.send(capA, new Message({caps: [capB]}))
503484
504- const stateRoot = await hypervisor.createStateRoot(Infinity)
505-
485+ const stateRoot = await hypervisor.createStateRoot()
506486 t.deepEquals(stateRoot, expectedState, 'expected root!')
507487 })
508488
509489 tape('trying to listen for caps more then once', async t => {
510490 t.plan(4)
511491 const expectedState = {
512- '/': Buffer.from('ef403643f292108fe9edc1700d80a7bf2402e7a0', 'hex')
492+ '/': Buffer.from('d4291da4536544bf90aa473a1148cb29f913d078', 'hex')
513493 }
514494
515495 const tree = new RadixTree({
516496 db: db
@@ -551,17 +531,16 @@
551531 let capB = await hypervisor.createActor(testVMContainerB.typeId, new Message())
552532
553533 await hypervisor.send(capA, new Message({caps: [capB]}))
554534
555- const stateRoot = await hypervisor.createStateRoot(Infinity)
556-
535+ const stateRoot = await hypervisor.createStateRoot()
557536 t.deepEquals(stateRoot, expectedState, 'expected root!')
558537 })
559538
560539 tape('multple messages to restore on waiting for tags', async t => {
561540 t.plan(6)
562541 const expectedState = {
563- '/': Buffer.from('c2bbd78ee38ecf417f857451bdb06cdba1345b22', 'hex')
542+ '/': Buffer.from('b5c0822ccb21bbaa2ad8069c4dcd18add7d6e2d1', 'hex')
564543 }
565544
566545 const tree = new RadixTree({
567546 db: db
@@ -611,17 +590,17 @@
611590 let capB2 = await hypervisor.createActor(testVMContainerB.typeId, new Message())
612591
613592 await hypervisor.send(capA, new Message({caps: [capB1, capB2]}))
614593
615- const stateRoot = await hypervisor.createStateRoot(Infinity)
594+ const stateRoot = await hypervisor.createStateRoot()
616595
617596 t.deepEquals(stateRoot, expectedState, 'expected root!')
618597 })
619598
620599 tape('multple messages to backup on waiting for tags', async t => {
621600 t.plan(6)
622601 const expectedState = {
623- '/': Buffer.from('c2bbd78ee38ecf417f857451bdb06cdba1345b22', 'hex')
602+ '/': Buffer.from('b5c0822ccb21bbaa2ad8069c4dcd18add7d6e2d1', 'hex')
624603 }
625604
626605 const tree = new RadixTree({
627606 db: db
@@ -671,17 +650,16 @@
671650 let capB2 = await hypervisor.createActor(testVMContainerB.typeId, new Message())
672651
673652 await hypervisor.send(capA, new Message({caps: [capB1, capB2]}))
674653
675- const stateRoot = await hypervisor.createStateRoot(Infinity)
676-
654+ const stateRoot = await hypervisor.createStateRoot()
677655 t.deepEquals(stateRoot, expectedState, 'expected root!')
678656 })
679657
680658 tape('multple messages, but single tag', async t => {
681659 t.plan(6)
682660 const expectedState = {
683- '/': Buffer.from('c2bbd78ee38ecf417f857451bdb06cdba1345b22', 'hex')
661+ '/': Buffer.from('b5c0822ccb21bbaa2ad8069c4dcd18add7d6e2d1', 'hex')
684662 }
685663
686664 const tree = new RadixTree({
687665 db: db
@@ -731,17 +709,16 @@
731709 let capB2 = await hypervisor.createActor(testVMContainerB.typeId, new Message())
732710
733711 await hypervisor.send(capA, new Message({caps: [capB1, capB2]}))
734712
735- const stateRoot = await hypervisor.createStateRoot(Infinity)
736-
713+ const stateRoot = await hypervisor.createStateRoot()
737714 t.deepEquals(stateRoot, expectedState, 'expected root!')
738715 })
739716
740717 tape('deadlock test', async t => {
741718 t.plan(7)
742719 const expectedState = {
743- '/': Buffer.from('2e8658dc2f616599b4fa622318b86ad6ed809db0', 'hex')
720+ '/': Buffer.from('f290945ad63dd06b9ada924fa5149df4a0a32f53', 'hex')
744721 }
745722
746723 const tree = new RadixTree({
747724 db: db
@@ -794,7 +771,7 @@
794771 hypervisor.send(capA, new Message()),
795772 hypervisor.send(capB, new Message()),
796773 hypervisor.send(capC, new Message())
797774 ])
798- const stateRoot = await hypervisor.createStateRoot(Infinity)
775+ const stateRoot = await hypervisor.createStateRoot()
799776 t.deepEquals(stateRoot, expectedState, 'expected root!')
800777 })
capsStore.jsView
@@ -1,37 +1,0 @@
1-module.exports = class CapsStore {
2- /**
3- * The caps store, persistantly stores an actors capabilites.
4- * @param {Object} storedCaps
5- */
6- constructor (storedCaps) {
7- this._storedCaps = storedCaps
8- }
9-
10- /**
11- * Stores a cap at a given key
12- * @param {String} key
13- * @param {Object} cap
14- */
15- store (key, cap) {
16- // save the port instance
17- this._storedCaps[key] = cap
18- }
19-
20- /**
21- * gets a cap given its key
22- * @param {String} key
23- * @return {Object}
24- */
25- load (key) {
26- const cap = this._storedCaps[key]
27- return cap
28- }
29-
30- /**
31- * delete cap given its key
32- * @param {string} key
33- */
34- delete (key) {
35- delete this._storedCaps[key]
36- }
37-}

Built with git-ssb-web