git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 1f8affb407f9413e9af400e3cc7408ece8274347

updated Hypervisors contructor's API

Signed-off-by: wanderer <mjbecze@gmail.com>
wanderer committed on 4/7/2018, 12:32:15 AM
Parent: dde59deea46cd600f319fa2ec8e88988386c3466

Files changed

actor.jschanged
index.jschanged
tests/index.jschanged
errors.jsonadded
actor.jsView
@@ -1,4 +1,7 @@
1+const errors = require('./errors.json')
2+const nope = () => {}
3+
14 module.exports = class Actor {
25 /**
36 * the Actor manages the varous message passing functions and provides
47 * an interface for the containers to use
@@ -10,12 +13,14 @@
1013 */
1114 constructor (opts) {
1215 Object.assign(this, opts)
1316
14- this.inbox = []
1517 this.ticks = 0
1618 this.running = false
1719 this.container = new this.Container(this)
20+ if (!this.hypervisor.meter) {
21+ this.incrementTicks = nope
22+ }
1823 }
1924
2025 /**
2126 * Runs the shutdown routine for the actor
@@ -56,8 +61,12 @@
5661 * updates the number of ticks that the actor has run
5762 * @param {Number} count - the number of ticks to add
5863 */
5964 incrementTicks (count) {
65+ this.currentMessage.funcRef.gas -= count
66+ if (this.currentMessage.funcRef.gas < 0) {
67+ throw new Error(errors.OUT_OF_GAS)
68+ }
6069 this.ticks += count
6170 }
6271
6372 /**
index.jsView
@@ -8,18 +8,23 @@
88 module.exports = class Hypervisor {
99 /**
1010 * The Hypervisor manages the container instances by instantiating them and
1111 * destorying them when possible. It also facilitates localating Containers
12- * @param {Tree} tree - a [radix tree](https://github.com/dfinity/js-dfinity-radix-tree) to store the state
12+ * @param {Object} opts
13+ * @param {Object} opts.tree - a [radix tree](https://github.com/dfinity/js-dfinity-radix-tree) to store the state
14+ * @param {Array} opts.container - an array of containers to regester
15+ * @param {Array} opts.drivers - an array of drivers to install
16+ * @param {boolean} [opts.meter=true] - whether to meter gas or not
1317 */
14- constructor (tree, containers = [], drivers = [], nonce = 0) {
15- tree.dag.decoder = decoder
16- this.tree = tree
18+ constructor (opts) {
19+ opts.tree.dag.decoder = decoder
20+ this.tree = opts.tree
1721 this.scheduler = new Scheduler(this)
1822 this._containerTypes = {}
19- this.nonce = nonce
20- containers.forEach(container => this.registerContainer(container))
21- drivers.forEach(driver => this.registerDriver(driver))
23+ this.nonce = opts.nonce || 0
24+ this.meter = opts.meter !== undefined ? opts.meter : true;
25+ (opts.containers || []).forEach(container => this.registerContainer(container));
26+ (opts.drivers || []).forEach(driver => this.registerDriver(driver))
2227 }
2328
2429 /**
2530 * sends a message
tests/index.jsView
@@ -1,6 +1,7 @@
11 const tape = require('tape')
22 const Hypervisor = require('../')
3+const errors = require('../errors.json')
34 const {Message, FunctionRef, ModuleRef} = require('primea-objects')
45
56 const level = require('level-browserify')
67 const EgressDriver = require('../egressDriver')
@@ -55,9 +56,9 @@
5556 t.deepEquals(funcref, loadedFuncref)
5657 }
5758 }
5859
59- const hypervisor = new Hypervisor(tree, [testVMContainer])
60+ const hypervisor = new Hypervisor({tree, containers: [testVMContainer]})
6061 const {module} = hypervisor.createActor(testVMContainer.typeId)
6162 const message = new Message({
6263 funcRef: module.getFuncRef('store')
6364 })
@@ -85,9 +86,9 @@
8586 t.equals(m, 1, 'should recive a message')
8687 }
8788 }
8889
89- const hypervisor = new Hypervisor(tree, [testVMContainer])
90+ const hypervisor = new Hypervisor({tree, containers: [testVMContainer]})
9091 await hypervisor.createStateRoot()
9192
9293 const {module} = hypervisor.createActor(testVMContainer.typeId)
9394
@@ -128,9 +129,9 @@
128129 return 8
129130 }
130131 }
131132
132- const hypervisor = new Hypervisor(tree, [testVMContainerA, testVMContainerB])
133+ const hypervisor = new Hypervisor({tree, containers: [testVMContainerA, testVMContainerB]})
133134
134135 const {module: moduleB} = hypervisor.createActor(testVMContainerB.typeId)
135136 const {module: moduleA} = hypervisor.createActor(testVMContainerA.typeId)
136137
@@ -171,9 +172,9 @@
171172 return 8
172173 }
173174 }
174175
175- const hypervisor = new Hypervisor(tree, [testVMContainerA, testVMContainerB])
176+ const hypervisor = new Hypervisor({tree, containers: [testVMContainerA, testVMContainerB]})
176177
177178 let {module: moduleB} = hypervisor.createActor(testVMContainerB.typeId)
178179 let {module: moduleA0} = hypervisor.createActor(testVMContainerA.typeId)
179180 let {module: moduleA1} = hypervisor.createActor(testVMContainerA.typeId)
@@ -222,20 +223,25 @@
222223 return 8
223224 }
224225 }
225226
226- const hypervisor = new Hypervisor(tree, [testVMContainerA, testVMContainerB])
227+ const hypervisor = new Hypervisor({tree, containers: [testVMContainerA, testVMContainerB]})
227228
228229 let actorB = hypervisor.createActor(testVMContainerB.typeId)
229230 let actorA0 = hypervisor.createActor(testVMContainerA.typeId)
230231 let actorA1 = hypervisor.createActor(testVMContainerA.typeId)
231232
233+ const funcRef0 = actorA0.module.getFuncRef('main')
234+ funcRef0.gas = 10000
235+
232236 const message0 = new Message({
233- funcRef: actorA0.module.getFuncRef('main'),
237+ funcRef: funcRef0,
234238 funcArguments: [actorB.module.getFuncRef('main')]
235239 })
240+ const funcRef1 = actorA1.module.getFuncRef('main')
241+ funcRef1.gas = 10000
236242 const message1 = new Message({
237- funcRef: actorA1.module.getFuncRef('main'),
243+ funcRef: funcRef1,
238244 funcArguments: [actorB.module.getFuncRef('main')]
239245 })
240246
241247 hypervisor.send(message0)
@@ -274,9 +280,9 @@
274280 return 8
275281 }
276282 }
277283
278- const hypervisor = new Hypervisor(tree)
284+ const hypervisor = new Hypervisor({tree})
279285 hypervisor.registerContainer(testVMContainerA)
280286 hypervisor.registerContainer(testVMContainerB)
281287
282288 let {module: moduleB} = hypervisor.createActor(testVMContainerB.typeId)
@@ -289,8 +295,63 @@
289295 const stateRoot = await hypervisor.createStateRoot()
290296 t.deepEquals(stateRoot, expectedState, 'expected root!')
291297 })
292298
299+tape('out-of-gas', async t => {
300+ t.plan(1)
301+ const tree = new RadixTree({
302+ db
303+ })
304+
305+ class testVMContainer extends BaseContainer {
306+ main (m) {
307+ this.actor.incrementTicks(1)
308+ }
309+ }
310+
311+ const hypervisor = new Hypervisor({tree, containers: [testVMContainer]})
312+ await hypervisor.createStateRoot()
313+
314+ const {module} = hypervisor.createActor(testVMContainer.typeId)
315+
316+ const message = new Message({
317+ funcRef: module.getFuncRef('main'),
318+ funcArguments: [1]
319+ }).on('execution:error', e => {
320+ t.equals(e.message, errors.OUT_OF_GAS)
321+ })
322+ hypervisor.send(message)
323+})
324+
325+tape('no mettering', async t => {
326+ t.plan(1)
327+ const tree = new RadixTree({
328+ db
329+ })
330+
331+ class testVMContainer extends BaseContainer {
332+ main (m) {
333+ this.actor.incrementTicks(1)
334+ t.pass('shouldnt meter')
335+ }
336+ }
337+
338+ const hypervisor = new Hypervisor({
339+ tree,
340+ containers: [testVMContainer],
341+ meter: false
342+ })
343+ await hypervisor.createStateRoot()
344+
345+ const {module} = hypervisor.createActor(testVMContainer.typeId)
346+
347+ const message = new Message({
348+ funcRef: module.getFuncRef('main'),
349+ funcArguments: [1]
350+ })
351+ hypervisor.send(message)
352+})
353+
293354 tape('actor creation', async t => {
294355 t.plan(2)
295356 const expectedState = Buffer.from('0e6d32f2fe8b5b99f0203eb46bfc7e319a07f700', 'hex')
296357
@@ -324,11 +385,9 @@
324385 return 8
325386 }
326387 }
327388
328- const hypervisor = new Hypervisor(tree)
329- hypervisor.registerContainer(testVMContainerA)
330- hypervisor.registerContainer(testVMContainerB)
389+ const hypervisor = new Hypervisor({tree, containers: [testVMContainerA, testVMContainerB]})
331390
332391 const {module} = hypervisor.createActor(testVMContainerA.typeId)
333392 await hypervisor.send(new Message({funcRef: module.getFuncRef('start')}))
334393
@@ -345,8 +404,9 @@
345404 })
346405
347406 class testVMContainerA extends BaseContainer {
348407 main (funcRef) {
408+ funcRef.gas = 1000
349409 const message1 = new Message({
350410 funcArguments: ['first'],
351411 funcRef
352412 })
@@ -385,16 +445,17 @@
385445 return 8
386446 }
387447 }
388448
389- const hypervisor = new Hypervisor(tree)
390- hypervisor.registerContainer(testVMContainerA)
391- hypervisor.registerContainer(testVMContainerB)
449+ const hypervisor = new Hypervisor({tree, containers: [testVMContainerA, testVMContainerB]})
392450
393451 const {module: moduleB} = hypervisor.createActor(testVMContainerB.typeId)
394452 const {module: moduleA} = hypervisor.createActor(testVMContainerA.typeId)
453+ const funcRef = moduleA.getFuncRef('main')
454+ funcRef.gas = 100
455+
395456 const message = new Message({
396- funcRef: moduleA.getFuncRef('main'),
457+ funcRef,
397458 funcArguments: [moduleB.getFuncRef('main')]
398459 })
399460 hypervisor.send(message)
400461
@@ -402,9 +463,9 @@
402463 t.deepEquals(stateRoot, expectedState, 'expected root!')
403464 })
404465
405466 tape('arbiter test for id comparision', async t => {
406- // t.plan(4)
467+ t.plan(5)
407468 let message
408469 const expectedState = Buffer.from('ae2e8afa84748192064ddebab30d0e9852ceb722', 'hex')
409470
410471 const tree = new RadixTree({
@@ -412,9 +473,8 @@
412473 })
413474
414475 class testVMContainerA extends BaseContainer {
415476 main (funcRef, funcArguments) {
416- this.actor.incrementTicks(1)
417477 message = new Message({
418478 funcRef,
419479 funcArguments: [funcArguments]
420480 })
@@ -440,11 +500,9 @@
440500 return 8
441501 }
442502 }
443503
444- const hypervisor = new Hypervisor(tree)
445- hypervisor.registerContainer(testVMContainerA)
446- hypervisor.registerContainer(testVMContainerB)
504+ const hypervisor = new Hypervisor({tree, containers: [testVMContainerA, testVMContainerB]})
447505
448506 let {module: moduleB} = hypervisor.createActor(testVMContainerB.typeId)
449507 hypervisor.send(new Message({
450508 funcRef: moduleB.getFuncRef('main'),
@@ -483,8 +541,9 @@
483541 })
484542
485543 class testVMContainerA extends BaseContainer {
486544 main (funcRef) {
545+ funcRef.gas = 100
487546 const message = new Message({
488547 funcRef,
489548 funcArguments: [2]
490549 })
@@ -514,17 +573,17 @@
514573 return 8
515574 }
516575 }
517576
518- const hypervisor = new Hypervisor(tree)
519- hypervisor.registerContainer(testVMContainerA)
520- hypervisor.registerContainer(testVMContainerB)
577+ const hypervisor = new Hypervisor({tree, containers: [testVMContainerA, testVMContainerB]})
521578
522579 const {module: moduleB} = hypervisor.createActor(testVMContainerB.typeId)
523580 const {module: moduleA} = hypervisor.createActor(testVMContainerA.typeId)
581+ const funcRef = moduleA.getFuncRef('main')
582+ funcRef.gas = 100
524583
525584 const message = new Message({
526- funcRef: moduleA.getFuncRef('main'),
585+ funcRef,
527586 funcArguments: [moduleB.getFuncRef('main')]
528587 })
529588
530589 hypervisor.send(message)
@@ -553,9 +612,13 @@
553612 }))
554613 }
555614 }
556615
557- const hypervisor = new Hypervisor(tree, [testVMContainer], [egress])
616+ const hypervisor = new Hypervisor({
617+ tree,
618+ containers: [testVMContainer],
619+ drivers: [egress]
620+ })
558621 const {module} = hypervisor.createActor(testVMContainer.typeId)
559622
560623 const message = new Message({
561624 funcRef: module.getFuncRef('main'),
@@ -594,16 +657,18 @@
594657 }
595658 }
596659 }
597660
598- const hypervisor = new Hypervisor(tree)
661+ const hypervisor = new Hypervisor({tree})
599662 hypervisor.registerContainer(BenchmarkContainer)
600663
601664 const refernces = []
602665 let _numOfActors = numOfActors
603666 while (_numOfActors--) {
604667 const {module} = hypervisor.createActor(BenchmarkContainer.typeId)
605- refernces.push(module.getFuncRef('main'))
668+ const funcRef = module.getFuncRef('main')
669+ funcRef.gas = 1000
670+ refernces.push(funcRef)
606671 }
607672 _numOfActors = numOfActors
608673 let msgs = []
609674 while (_numOfActors--) {
errors.jsonView
@@ -1,0 +1,3 @@
1+{
2+ "OUT_OF_GAS": "out of gas!"
3+}

Built with git-ssb-web