git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 2f1d81adf2eec8e0c13058fae79efee1a6af4a58

clean up system objects

wanderer committed on 3/23/2018, 12:06:24 AM
Parent: aafe96ace826878428496f61ad0f8a93f9fccd2e

Files changed

index.jschanged
package-lock.jsonchanged
package.jsonchanged
systemObjects.jschanged
tests/index.jschanged
tests/wasmContainer.jschanged
wasmContainer.jschanged
index.jsView
@@ -1,16 +1,17 @@
11 const crypto = require('crypto')
22 const Actor = require('./actor.js')
33 const Scheduler = require('./scheduler.js')
4-const {ID} = require('./systemObjects.js')
4+const {ID, decoder} = require('./systemObjects.js')
55
66 module.exports = class Hypervisor {
77 /**
88 * The Hypervisor manages the container instances by instantiating them and
99 * destorying them when possible. It also facilitates localating Containers
1010 * @param {Tree} tree - a [radix tree](https://github.com/dfinity/js-dfinity-radix-tree) to store the state
1111 */
1212 constructor (tree, containers = [], drivers = [], nonce = 0) {
13+ tree.dag.decoder = decoder
1314 this.tree = tree
1415 this.scheduler = new Scheduler(this)
1516 this._containerTypes = {}
1617 this.nonce = nonce
@@ -47,9 +48,9 @@
4748 id,
4849 nonce,
4950 type,
5051 code,
51- storage: storage || [],
52+ storage,
5253 tree: this.tree
5354 })
5455
5556 await actor.startup()
@@ -100,18 +101,14 @@
100101 * @param {Number} ticks the number of ticks at which to create the state root
101102 * @returns {Promise}
102103 */
103104 async createStateRoot () {
104- const promise = new Promise((resolve, reject) => {
105- if (!this.scheduler._running) {
106- this.tree.flush().then(resolve)
107- } else {
108- this.scheduler.once('idle', () => {
109- this.tree.flush().then(resolve)
110- })
111- }
112- })
113- return promise
105+ if (this.scheduler._running) {
106+ await new Promise((resolve, reject) => {
107+ this.scheduler.once('idle', resolve)
108+ })
109+ }
110+ return this.tree.flush()
114111 }
115112
116113 /**
117114 * regirsters a container with the hypervisor
package-lock.jsonView
The diff is too large to show. Use a local git client to view these changes.
Old file size: 369221 bytes
New file size: 369767 bytes
package.jsonView
@@ -30,9 +30,9 @@
3030 "contributors": "Alex Beregszaszi <alex@rtfs.hu>",
3131 "license": "MPL-2.0",
3232 "dependencies": {
3333 "binary-search-insert": "^1.0.3",
34- "borc": "^2.0.2",
34+ "borc": "git+ssh://git@github.com:dignifiedquire/borc.git#fix/nested-array",
3535 "events": "^2.0.0",
3636 "primea-annotations": "0.0.1",
3737 "reference-map": "^1.2.3",
3838 "safe-buffer": "^5.1.1",
@@ -41,9 +41,9 @@
4141 },
4242 "devDependencies": {
4343 "levelup": "^2.0.2",
4444 "coveralls": "^3.0.0",
45- "dfinity-radix-tree": "^0.1.5",
45+ "dfinity-radix-tree": "^0.2.1",
4646 "documentation": "^6.0.0",
4747 "level-browserify": "^1.1.2",
4848 "nyc": "^11.6.0",
4949 "standard": "11.0.1",
systemObjects.jsView
@@ -11,60 +11,50 @@
1111 elem: [],
1212 data: Buffer.from([]),
1313 id: new cbor.Tagged(TAGS.id, 0),
1414 mod: new cbor.Tagged(TAGS.mod, [{}, new cbor.Tagged(TAGS.id, 0)]),
15- link: {'/': null},
15+ link: new cbor.Tagged(TAGS.link, null),
1616 func: new cbor.Tagged(TAGS.func, 0)
1717 }
1818
1919 const decoder = new cbor.Decoder({
2020 tags: {
2121 [TAGS.id]: val => new ID(val),
22- [TAGS.func]: val => new FunctionRef(...val),
23- [TAGS.mod]: val => new ModuleRef(...val)
22+ [TAGS.func]: val => new FunctionRef({
23+ identifier: val[0],
24+ params: val[1],
25+ id: val[2],
26+ gas: val[3]
27+ }),
28+ [TAGS.mod]: val => new ModuleRef(...val),
29+ [TAGS.link]: val => {
30+ return {
31+ '/': val
32+ }
33+ }
2434 }
2535 })
2636
27-class Serializable {
28- serialize () {
29- const encoder = new cbor.Encoder()
30- this.encodeCBOR(encoder)
31- return encoder.finalize()
32- }
33-
34- static deserialize (serialized) {
35- return decoder.decodeFirst(serialized)
36- }
37-}
38-
39-class FunctionRef extends Serializable {
37+class FunctionRef {
4038 constructor (opts) {
41- super()
4239 this.identifier = opts.identifier
43- if (!(opts.id instanceof ID)) {
44- opts.id = new ID(opts.id)
45- }
4640 this.destId = opts.id
4741 this.params = opts.params
48- this.gas = opts.gas
42+ this.gas = opts.gas || 0
4943 }
5044
5145 encodeCBOR (gen) {
5246 return gen.write(new cbor.Tagged(TAGS.func, [
5347 this.identifier,
5448 this.params,
55- this.destId
49+ this.destId,
50+ this.gas
5651 ]))
5752 }
58-
59- set container (container) {
60- this._container = container
61- }
6253 }
6354
64-class ModuleRef extends Serializable {
55+class ModuleRef {
6556 constructor (ex, id) {
66- super()
6757 this.exports = ex
6858 this.id = id
6959 }
7060
@@ -78,22 +68,12 @@
7868
7969 encodeCBOR (gen) {
8070 return gen.write(new cbor.Tagged(TAGS.mod, [this.exports, this.id]))
8171 }
82-
83- static fromMetaJSON (json, id) {
84- const exports = {}
85- for (const ex in json.exports) {
86- const type = json.types[json.indexes[json.exports[ex].toString()]].params
87- exports[ex] = type
88- }
89- return new ModuleRef(exports, id)
90- }
9172 }
9273
93-class ID extends Serializable {
74+class ID {
9475 constructor (id) {
95- super()
9676 this.id = id
9777 }
9878
9979 encodeCBOR (gen) {
@@ -104,6 +84,7 @@
10484 module.exports = {
10585 ID,
10686 FunctionRef,
10787 ModuleRef,
108- DEFAULTS
88+ DEFAULTS,
89+ decoder
10990 }
tests/index.jsView
@@ -1,8 +1,8 @@
11 const tape = require('tape')
22 const Message = require('../message.js')
33 const Hypervisor = require('../')
4-const {FunctionRef} = require('../systemObjects')
4+const {FunctionRef, ModuleRef} = require('../systemObjects')
55
66 const level = require('level-browserify')
77 const EgressDriver = require('../egressDriver')
88 const RadixTree = require('dfinity-radix-tree')
@@ -30,14 +30,58 @@
3030 return 9
3131 }
3232 }
3333
34+tape('system objects', async t => {
35+ t.plan(4)
36+ const tree = new RadixTree({
37+ db
38+ })
39+
40+ let id
41+ let mod
42+ let funcref
43+
44+ class testVMContainer extends BaseContainer {
45+ store () {
46+ id = this.actor.id.id.toString('hex')
47+ mod = new ModuleRef({'test': ['i32', 'i64']}, this.actor.id)
48+ funcref = mod.getFuncRef('test')
49+ this.actor.storage = [this.actor.id, {'/': 'test'}, mod, funcref]
50+ }
51+ load () {
52+ const loadedID = this.actor.storage[0].id.toString('hex')
53+ const link = this.actor.storage[1]
54+ const loadedMod = this.actor.storage[2]
55+ const loadedFuncref = this.actor.storage[3]
56+ t.equals(id, loadedID, 'should load id correctly')
57+ t.equals(link['/'].toString('hex'), '6fe3180f700090697285ac1e0e8dc400259373d7', 'should load link correctly')
58+ t.deepEquals(loadedMod, mod)
59+ t.deepEquals(funcref, loadedFuncref)
60+ }
61+ }
62+
63+ const hypervisor = new Hypervisor(tree, [testVMContainer])
64+ const {module} = hypervisor.createActor(testVMContainer.typeId)
65+
66+ const message = new Message({
67+ funcRef: module.store
68+ })
69+
70+ hypervisor.send(message)
71+ await hypervisor.createStateRoot()
72+
73+ const message2 = new Message({
74+ funcRef: module.load
75+ })
76+ hypervisor.send(message2)
77+ await hypervisor.createStateRoot()
78+ t.end()
79+})
80+
3481 tape('basic', async t => {
3582 t.plan(2)
36- const expectedState = {
37- '/': Buffer.from('1602fe14ee1e95c9d5cf10e809d0615cb21927a2', 'hex')
38- }
39-
83+ const expectedState = Buffer.from('1602fe14ee1e95c9d5cf10e809d0615cb21927a2', 'hex')
4084 const tree = new RadixTree({
4185 db
4286 })
4387
@@ -47,25 +91,25 @@
4791 }
4892 }
4993
5094 const hypervisor = new Hypervisor(tree, [testVMContainer])
95+ await hypervisor.createStateRoot()
96+
5197 const {module} = hypervisor.createActor(testVMContainer.typeId)
5298
5399 const message = new Message({
54100 funcRef: module.main,
55101 funcArguments: [1]
56102 })
57103 hypervisor.send(message)
58104
59- const stateRoot = await hypervisor.createStateRoot()
60- t.deepEquals(stateRoot, expectedState, 'expected root!')
105+ const stateRoot2 = await hypervisor.createStateRoot()
106+ t.deepEquals(stateRoot2, expectedState, 'expected root!')
61107 })
62108
63109 tape('two communicating actors', async t => {
64110 t.plan(2)
65- const expectedState = {
66- '/': Buffer.from('3cdad3b1024074e7edafadbb98ee162cc8cfe565', 'hex')
67- }
111+ const expectedState = Buffer.from('3cdad3b1024074e7edafadbb98ee162cc8cfe565', 'hex')
68112
69113 const tree = new RadixTree({
70114 db
71115 })
@@ -107,12 +151,9 @@
107151 })
108152
109153 tape('three communicating actors', async t => {
110154 t.plan(3)
111- const expectedState = {
112- '/': Buffer.from('7b659c263363b0b9c461a432aac8d8bf0d351788', 'hex')
113- }
114-
155+ const expectedState = Buffer.from('7b659c263363b0b9c461a432aac8d8bf0d351788', 'hex')
115156 const tree = new RadixTree({
116157 db: db
117158 })
118159
@@ -160,12 +201,9 @@
160201 })
161202
162203 tape('three communicating actors, with tick counting', async t => {
163204 t.plan(3)
164- const expectedState = {
165- '/': Buffer.from('7b659c263363b0b9c461a432aac8d8bf0d351788', 'hex')
166- }
167-
205+ const expectedState = Buffer.from('7b659c263363b0b9c461a432aac8d8bf0d351788', 'hex')
168206 const tree = new RadixTree({
169207 db: db
170208 })
171209
@@ -213,12 +251,9 @@
213251 })
214252
215253 tape('errors', async t => {
216254 t.plan(3)
217- const expectedState = {
218- '/': Buffer.from('3cdad3b1024074e7edafadbb98ee162cc8cfe565', 'hex')
219- }
220-
255+ const expectedState = Buffer.from('3cdad3b1024074e7edafadbb98ee162cc8cfe565', 'hex')
221256 const tree = new RadixTree({
222257 db: db
223258 })
224259
@@ -261,11 +296,9 @@
261296 })
262297
263298 tape('actor creation', async t => {
264299 t.plan(2)
265- const expectedState = {
266- '/': Buffer.from('f1803a4188890e205e2e6480159d504d149d8910', 'hex')
267- }
300+ const expectedState = Buffer.from('f1803a4188890e205e2e6480159d504d149d8910', 'hex')
268301
269302 const tree = new RadixTree({
270303 db: db
271304 })
@@ -310,12 +343,9 @@
310343 })
311344
312345 tape('simple message arbiter test', async t => {
313346 t.plan(4)
314- const expectedState = {
315- '/': Buffer.from('3cdad3b1024074e7edafadbb98ee162cc8cfe565', 'hex')
316- }
317-
347+ const expectedState = Buffer.from('3cdad3b1024074e7edafadbb98ee162cc8cfe565', 'hex')
318348 const tree = new RadixTree({
319349 db: db
320350 })
321351
@@ -379,11 +409,9 @@
379409
380410 tape('arbiter test for id comparision', async t => {
381411 t.plan(4)
382412 let message
383- const expectedState = {
384- '/': Buffer.from('7b659c263363b0b9c461a432aac8d8bf0d351788', 'hex')
385- }
413+ const expectedState = Buffer.from('7b659c263363b0b9c461a432aac8d8bf0d351788', 'hex')
386414
387415 const tree = new RadixTree({
388416 db: db
389417 })
@@ -446,11 +474,9 @@
446474 })
447475
448476 tape('async work', async t => {
449477 t.plan(3)
450- const expectedState = {
451- '/': Buffer.from('3cdad3b1024074e7edafadbb98ee162cc8cfe565', 'hex')
452- }
478+ const expectedState = Buffer.from('3cdad3b1024074e7edafadbb98ee162cc8cfe565', 'hex')
453479
454480 const tree = new RadixTree({
455481 db: db
456482 })
tests/wasmContainer.jsView
@@ -36,11 +36,9 @@
3636
3737 tape('basic', async t => {
3838 t.plan(1)
3939 tester = t
40- const expectedState = {
41- '/': Buffer.from('4494963fb0e02312510e675fbca8b60b6e03bd00', 'hex')
42- }
40+ const expectedState = Buffer.from('4494963fb0e02312510e675fbca8b60b6e03bd00', 'hex')
4341
4442 const tree = new RadixTree({
4543 db
4644 })
@@ -67,11 +65,9 @@
6765
6866 tape('empty', async t => {
6967 t.plan(1)
7068 tester = t
71- const expectedState = {
72- '/': Buffer.from('3ac226eb0a4809e7f0b3d7ba1e0bb6d57e0378a8', 'hex')
73- }
69+ const expectedState = Buffer.from('aeb5418328108a82b7a2a57712bddc989d513f5d', 'hex')
7470
7571 const tree = new RadixTree({
7672 db
7773 })
wasmContainer.jsView
@@ -9,8 +9,17 @@
99
1010 const nativeTypes = new Set(['i32', 'i64', 'f32', 'f64'])
1111 const FUNC_INDEX_OFFSET = 1
1212
13+function fromMetaJSON (json, id) {
14+ const exports = {}
15+ for (const ex in json.exports) {
16+ const type = json.types[json.indexes[json.exports[ex].toString()]].params
17+ exports[ex] = type
18+ }
19+ return new ModuleRef(exports, id)
20+}
21+
1322 function generateWrapper (funcRef, container) {
1423 let wrapper = typeCheckWrapper(funcRef.params)
1524 const wasm = json2wasm(wrapper)
1625 const mod = WebAssembly.Module(wasm)
@@ -63,9 +72,9 @@
6372 moduleJSON = injectGlobals(moduleJSON, json.persist)
6473 }
6574 // recompile the wasm
6675 wasm = json2wasm(moduleJSON)
67- const modRef = ModuleRef.fromMetaJSON(json, id)
76+ const modRef = fromMetaJSON(json, id)
6877 return {
6978 wasm,
7079 json,
7180 modRef

Built with git-ssb-web