tests/index.jsView |
---|
1 | 1 | const tape = require('tape') |
2 | 2 | const Hypervisor = require('../') |
| 3 | +const errors = require('../errors.json') |
3 | 4 | const {Message, FunctionRef, ModuleRef} = require('primea-objects') |
4 | 5 | |
5 | 6 | const level = require('level-browserify') |
6 | 7 | const EgressDriver = require('../egressDriver') |
55 | 56 | t.deepEquals(funcref, loadedFuncref) |
56 | 57 | } |
57 | 58 | } |
58 | 59 | |
59 | | - const hypervisor = new Hypervisor(tree, [testVMContainer]) |
| 60 | + const hypervisor = new Hypervisor({tree, containers: [testVMContainer]}) |
60 | 61 | const {module} = hypervisor.createActor(testVMContainer.typeId) |
61 | 62 | const message = new Message({ |
62 | 63 | funcRef: module.getFuncRef('store') |
63 | 64 | }) |
85 | 86 | t.equals(m, 1, 'should recive a message') |
86 | 87 | } |
87 | 88 | } |
88 | 89 | |
89 | | - const hypervisor = new Hypervisor(tree, [testVMContainer]) |
| 90 | + const hypervisor = new Hypervisor({tree, containers: [testVMContainer]}) |
90 | 91 | await hypervisor.createStateRoot() |
91 | 92 | |
92 | 93 | const {module} = hypervisor.createActor(testVMContainer.typeId) |
93 | 94 | |
128 | 129 | return 8 |
129 | 130 | } |
130 | 131 | } |
131 | 132 | |
132 | | - const hypervisor = new Hypervisor(tree, [testVMContainerA, testVMContainerB]) |
| 133 | + const hypervisor = new Hypervisor({tree, containers: [testVMContainerA, testVMContainerB]}) |
133 | 134 | |
134 | 135 | const {module: moduleB} = hypervisor.createActor(testVMContainerB.typeId) |
135 | 136 | const {module: moduleA} = hypervisor.createActor(testVMContainerA.typeId) |
136 | 137 | |
171 | 172 | return 8 |
172 | 173 | } |
173 | 174 | } |
174 | 175 | |
175 | | - const hypervisor = new Hypervisor(tree, [testVMContainerA, testVMContainerB]) |
| 176 | + const hypervisor = new Hypervisor({tree, containers: [testVMContainerA, testVMContainerB]}) |
176 | 177 | |
177 | 178 | let {module: moduleB} = hypervisor.createActor(testVMContainerB.typeId) |
178 | 179 | let {module: moduleA0} = hypervisor.createActor(testVMContainerA.typeId) |
179 | 180 | let {module: moduleA1} = hypervisor.createActor(testVMContainerA.typeId) |
222 | 223 | return 8 |
223 | 224 | } |
224 | 225 | } |
225 | 226 | |
226 | | - const hypervisor = new Hypervisor(tree, [testVMContainerA, testVMContainerB]) |
| 227 | + const hypervisor = new Hypervisor({tree, containers: [testVMContainerA, testVMContainerB]}) |
227 | 228 | |
228 | 229 | let actorB = hypervisor.createActor(testVMContainerB.typeId) |
229 | 230 | let actorA0 = hypervisor.createActor(testVMContainerA.typeId) |
230 | 231 | let actorA1 = hypervisor.createActor(testVMContainerA.typeId) |
231 | 232 | |
| 233 | + const funcRef0 = actorA0.module.getFuncRef('main') |
| 234 | + funcRef0.gas = 10000 |
| 235 | + |
232 | 236 | const message0 = new Message({ |
233 | | - funcRef: actorA0.module.getFuncRef('main'), |
| 237 | + funcRef: funcRef0, |
234 | 238 | funcArguments: [actorB.module.getFuncRef('main')] |
235 | 239 | }) |
| 240 | + const funcRef1 = actorA1.module.getFuncRef('main') |
| 241 | + funcRef1.gas = 10000 |
236 | 242 | const message1 = new Message({ |
237 | | - funcRef: actorA1.module.getFuncRef('main'), |
| 243 | + funcRef: funcRef1, |
238 | 244 | funcArguments: [actorB.module.getFuncRef('main')] |
239 | 245 | }) |
240 | 246 | |
241 | 247 | hypervisor.send(message0) |
274 | 280 | return 8 |
275 | 281 | } |
276 | 282 | } |
277 | 283 | |
278 | | - const hypervisor = new Hypervisor(tree) |
| 284 | + const hypervisor = new Hypervisor({tree}) |
279 | 285 | hypervisor.registerContainer(testVMContainerA) |
280 | 286 | hypervisor.registerContainer(testVMContainerB) |
281 | 287 | |
282 | 288 | let {module: moduleB} = hypervisor.createActor(testVMContainerB.typeId) |
289 | 295 | const stateRoot = await hypervisor.createStateRoot() |
290 | 296 | t.deepEquals(stateRoot, expectedState, 'expected root!') |
291 | 297 | }) |
292 | 298 | |
| 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 | + |
293 | 354 | tape('actor creation', async t => { |
294 | 355 | t.plan(2) |
295 | 356 | const expectedState = Buffer.from('0e6d32f2fe8b5b99f0203eb46bfc7e319a07f700', 'hex') |
296 | 357 | |
324 | 385 | return 8 |
325 | 386 | } |
326 | 387 | } |
327 | 388 | |
328 | | - const hypervisor = new Hypervisor(tree) |
329 | | - hypervisor.registerContainer(testVMContainerA) |
330 | | - hypervisor.registerContainer(testVMContainerB) |
| 389 | + const hypervisor = new Hypervisor({tree, containers: [testVMContainerA, testVMContainerB]}) |
331 | 390 | |
332 | 391 | const {module} = hypervisor.createActor(testVMContainerA.typeId) |
333 | 392 | await hypervisor.send(new Message({funcRef: module.getFuncRef('start')})) |
334 | 393 | |
345 | 404 | }) |
346 | 405 | |
347 | 406 | class testVMContainerA extends BaseContainer { |
348 | 407 | main (funcRef) { |
| 408 | + funcRef.gas = 1000 |
349 | 409 | const message1 = new Message({ |
350 | 410 | funcArguments: ['first'], |
351 | 411 | funcRef |
352 | 412 | }) |
385 | 445 | return 8 |
386 | 446 | } |
387 | 447 | } |
388 | 448 | |
389 | | - const hypervisor = new Hypervisor(tree) |
390 | | - hypervisor.registerContainer(testVMContainerA) |
391 | | - hypervisor.registerContainer(testVMContainerB) |
| 449 | + const hypervisor = new Hypervisor({tree, containers: [testVMContainerA, testVMContainerB]}) |
392 | 450 | |
393 | 451 | const {module: moduleB} = hypervisor.createActor(testVMContainerB.typeId) |
394 | 452 | const {module: moduleA} = hypervisor.createActor(testVMContainerA.typeId) |
| 453 | + const funcRef = moduleA.getFuncRef('main') |
| 454 | + funcRef.gas = 100 |
| 455 | + |
395 | 456 | const message = new Message({ |
396 | | - funcRef: moduleA.getFuncRef('main'), |
| 457 | + funcRef, |
397 | 458 | funcArguments: [moduleB.getFuncRef('main')] |
398 | 459 | }) |
399 | 460 | hypervisor.send(message) |
400 | 461 | |
402 | 463 | t.deepEquals(stateRoot, expectedState, 'expected root!') |
403 | 464 | }) |
404 | 465 | |
405 | 466 | tape('arbiter test for id comparision', async t => { |
406 | | - |
| 467 | + t.plan(5) |
407 | 468 | let message |
408 | 469 | const expectedState = Buffer.from('ae2e8afa84748192064ddebab30d0e9852ceb722', 'hex') |
409 | 470 | |
410 | 471 | const tree = new RadixTree({ |
412 | 473 | }) |
413 | 474 | |
414 | 475 | class testVMContainerA extends BaseContainer { |
415 | 476 | main (funcRef, funcArguments) { |
416 | | - this.actor.incrementTicks(1) |
417 | 477 | message = new Message({ |
418 | 478 | funcRef, |
419 | 479 | funcArguments: [funcArguments] |
420 | 480 | }) |
440 | 500 | return 8 |
441 | 501 | } |
442 | 502 | } |
443 | 503 | |
444 | | - const hypervisor = new Hypervisor(tree) |
445 | | - hypervisor.registerContainer(testVMContainerA) |
446 | | - hypervisor.registerContainer(testVMContainerB) |
| 504 | + const hypervisor = new Hypervisor({tree, containers: [testVMContainerA, testVMContainerB]}) |
447 | 505 | |
448 | 506 | let {module: moduleB} = hypervisor.createActor(testVMContainerB.typeId) |
449 | 507 | hypervisor.send(new Message({ |
450 | 508 | funcRef: moduleB.getFuncRef('main'), |
483 | 541 | }) |
484 | 542 | |
485 | 543 | class testVMContainerA extends BaseContainer { |
486 | 544 | main (funcRef) { |
| 545 | + funcRef.gas = 100 |
487 | 546 | const message = new Message({ |
488 | 547 | funcRef, |
489 | 548 | funcArguments: [2] |
490 | 549 | }) |
514 | 573 | return 8 |
515 | 574 | } |
516 | 575 | } |
517 | 576 | |
518 | | - const hypervisor = new Hypervisor(tree) |
519 | | - hypervisor.registerContainer(testVMContainerA) |
520 | | - hypervisor.registerContainer(testVMContainerB) |
| 577 | + const hypervisor = new Hypervisor({tree, containers: [testVMContainerA, testVMContainerB]}) |
521 | 578 | |
522 | 579 | const {module: moduleB} = hypervisor.createActor(testVMContainerB.typeId) |
523 | 580 | const {module: moduleA} = hypervisor.createActor(testVMContainerA.typeId) |
| 581 | + const funcRef = moduleA.getFuncRef('main') |
| 582 | + funcRef.gas = 100 |
524 | 583 | |
525 | 584 | const message = new Message({ |
526 | | - funcRef: moduleA.getFuncRef('main'), |
| 585 | + funcRef, |
527 | 586 | funcArguments: [moduleB.getFuncRef('main')] |
528 | 587 | }) |
529 | 588 | |
530 | 589 | hypervisor.send(message) |
553 | 612 | })) |
554 | 613 | } |
555 | 614 | } |
556 | 615 | |
557 | | - const hypervisor = new Hypervisor(tree, [testVMContainer], [egress]) |
| 616 | + const hypervisor = new Hypervisor({ |
| 617 | + tree, |
| 618 | + containers: [testVMContainer], |
| 619 | + drivers: [egress] |
| 620 | + }) |
558 | 621 | const {module} = hypervisor.createActor(testVMContainer.typeId) |
559 | 622 | |
560 | 623 | const message = new Message({ |
561 | 624 | funcRef: module.getFuncRef('main'), |
594 | 657 | } |
595 | 658 | } |
596 | 659 | } |
597 | 660 | |
598 | | - const hypervisor = new Hypervisor(tree) |
| 661 | + const hypervisor = new Hypervisor({tree}) |
599 | 662 | hypervisor.registerContainer(BenchmarkContainer) |
600 | 663 | |
601 | 664 | const refernces = [] |
602 | 665 | let _numOfActors = numOfActors |
603 | 666 | while (_numOfActors--) { |
604 | 667 | 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) |
606 | 671 | } |
607 | 672 | _numOfActors = numOfActors |
608 | 673 | let msgs = [] |
609 | 674 | while (_numOfActors--) { |