git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 679caf4c89ce01c38751eb18177edee3a845d23e

Files: 679caf4c89ce01c38751eb18177edee3a845d23e / tests / index.js

31533 bytesRaw
1const tape = require('tape')
2const IPFS = require('ipfs')
3const Hypervisor = require('../')
4
5// start ipfs
6const node = new IPFS({
7 start: false
8})
9
10class BaseContainer {
11 constructor (exInterface) {
12 this.exInterface = exInterface
13 }
14
15 initialize (message) {
16 const port = message.ports[0]
17 if (port) {
18 this.exInterface.ports.bind('root', port)
19 }
20 }
21}
22
23node.on('ready', () => {
24 tape('basic', async t => {
25 t.plan(2)
26 let message
27 const expectedState = {
28 '/': 'zdpuB1wc9Pb6jUzfNt4nAxAEUxB7kNhg4vbq7YLcEyBUb6iAB'
29 }
30
31 class testVMContainer extends BaseContainer {
32 run (m) {
33 t.true(m === message, 'should recive a message')
34 }
35 }
36
37 const hypervisor = new Hypervisor(node.dag)
38 hypervisor.registerContainer('test', testVMContainer)
39
40 const rootContainer = await hypervisor.createInstance('test')
41
42 const [portRef1, portRef2] = rootContainer.ports.createChannel()
43 const initMessage = rootContainer.createMessage({
44 data: Buffer.from('test code'),
45 ports: [portRef2]
46 })
47
48 rootContainer.ports.create('test', initMessage)
49
50 rootContainer.ports.bind('first', portRef1)
51 message = rootContainer.createMessage()
52 rootContainer.send(portRef1, message)
53
54 const stateRoot = await hypervisor.createStateRoot(Infinity)
55 t.deepEquals(stateRoot, expectedState, 'expected root!')
56 })
57
58 tape('basic - do not store containers with no ports bound', async t => {
59 t.plan(1)
60 const expectedState = {
61 '/': 'zdpuAx5LRRwTgzPipKEPgh7MHUKu4Pd1BYjDqBcf9whgzvrqf'
62 }
63
64 class testVMContainer extends BaseContainer {
65 initialize () {}
66 }
67
68 const hypervisor = new Hypervisor(node.dag)
69 hypervisor.registerContainer('test', testVMContainer)
70
71 const root = await hypervisor.createInstance('test')
72 const [portRef1, portRef2] = root.ports.createChannel()
73
74 root.ports.bind('one', portRef1)
75 root.ports.create('test', root.createMessage({
76 ports: [portRef2]
77 }))
78
79 const stateRoot = await hypervisor.createStateRoot(Infinity)
80 t.deepEquals(stateRoot, expectedState, 'expected root!')
81 })
82
83 tape('one child contract with saturated ports', async t => {
84 t.plan(2)
85 let message
86 const expectedState = {
87 '/': 'zdpuAtVcH6MUnvt2RXnLsDXyLB3CBSQ7aydfh2ogSKGCejJCQ'
88 }
89
90 class testVMContainer2 extends BaseContainer {
91 run (m) {
92 t.true(m === message, 'should recive a message')
93 }
94 }
95
96 class testVMContainer extends BaseContainer {
97 run (m) {
98 const [portRef1, portRef2] = this.exInterface.ports.createChannel()
99 this.exInterface.ports.create('test2', this.exInterface.createMessage({
100 ports: [portRef2]
101 }))
102 this.exInterface.ports.bind('child', portRef1)
103 this.exInterface.incrementTicks(2)
104 this.exInterface.send(portRef1, m)
105 }
106 }
107
108 const hypervisor = new Hypervisor(node.dag)
109 hypervisor.registerContainer('test', testVMContainer)
110 hypervisor.registerContainer('test2', testVMContainer2)
111
112 const root = await hypervisor.createInstance('test')
113 const [portRef1, portRef2] = root.ports.createChannel()
114 root.ports.create('test', root.createMessage({
115 ports: [portRef2]
116 }))
117
118 root.ports.bind('first', portRef1)
119 message = root.createMessage({
120 data: 'test'
121 })
122
123 root.send(portRef1, message)
124 const stateRoot = await hypervisor.createStateRoot(Infinity)
125 t.deepEquals(stateRoot, expectedState, 'expected state')
126 })
127
128 tape('one child contract', async t => {
129 t.plan(4)
130 let message
131 const expectedState = {
132 '/': 'zdpuAtVcH6MUnvt2RXnLsDXyLB3CBSQ7aydfh2ogSKGCejJCQ'
133 }
134 let hasResolved = false
135
136 class testVMContainer2 extends BaseContainer {
137 run (m) {
138 t.true(m === message, 'should recive a message')
139 return new Promise((resolve, reject) => {
140 setTimeout(() => {
141 this.exInterface.incrementTicks(1)
142 hasResolved = true
143 resolve()
144 }, 200)
145 })
146 }
147 }
148
149 class testVMContainer extends BaseContainer {
150 run (m) {
151 const [portRef1, portRef2] = this.exInterface.ports.createChannel()
152 this.exInterface.ports.create('test2', this.exInterface.createMessage({
153 ports: [portRef2]
154 }))
155 this.exInterface.ports.bind('child', portRef1)
156 this.exInterface.send(portRef1, m)
157 this.exInterface.incrementTicks(1)
158 }
159 }
160
161 const hypervisor = new Hypervisor(node.dag)
162 hypervisor.registerContainer('test', testVMContainer)
163 hypervisor.registerContainer('test2', testVMContainer2)
164
165 let root = await hypervisor.createInstance('test')
166 const rootId = root.id
167 const [portRef1, portRef2] = root.ports.createChannel()
168 root.ports.create('test', root.createMessage({
169 ports: [portRef2]
170 }))
171
172 root.ports.bind('first', portRef1)
173 message = root.createMessage()
174
175 root.send(portRef1, message)
176 const stateRoot = await hypervisor.createStateRoot(Infinity)
177 t.true(hasResolved, 'should resolve before generating the state root')
178 t.deepEquals(stateRoot, expectedState, 'expected state')
179
180 // test reviving the state
181 class testVMContainer3 extends BaseContainer {
182 run (m) {
183 const port = this.exInterface.ports.get('child')
184 this.exInterface.send(port, m)
185 this.exInterface.incrementTicks(1)
186 }
187 }
188
189 hypervisor.registerContainer('test', testVMContainer3)
190 root = await hypervisor.getInstance(rootId)
191 const port = root.ports.get('first')
192 root.send(port, message)
193 })
194
195 tape('traps', async t => {
196 t.plan(1)
197 class Root extends BaseContainer {
198 async run (m) {
199 const [portRef1, portRef2] = this.exInterface.ports.createChannel()
200 const [portRef3, portRef4] = this.exInterface.ports.createChannel()
201 const [portRef5, portRef6] = this.exInterface.ports.createChannel()
202
203 this.exInterface.ports.bind('one', portRef1)
204 this.exInterface.ports.bind('two', portRef3)
205 this.exInterface.ports.bind('three', portRef5)
206
207 const message1 = this.exInterface.createMessage({
208 ports: [portRef2]
209 })
210 const message2 = this.exInterface.createMessage({
211 ports: [portRef4]
212 })
213 const message3 = this.exInterface.createMessage({
214 ports: [portRef6]
215 })
216
217 this.exInterface.ports.create('root', message1)
218 this.exInterface.ports.create('root', message2)
219 this.exInterface.ports.create('root', message3)
220
221 throw new Error('it is a trap!!!')
222 }
223 }
224
225 const hypervisor = new Hypervisor(node.dag)
226
227 hypervisor.registerContainer('root', Root)
228 const root = await hypervisor.createInstance('root')
229 await root.run(root.createMessage())
230 const stateRoot = await hypervisor.createStateRoot()
231
232 t.deepEquals(stateRoot, {
233 '/': 'zdpuAwrMmQXqFusve7zcRYxVUuji4NVzZR5GyjwyStsjteCoW'
234 }, 'should revert the state')
235 })
236
237 tape('message should arrive in the correct oder if sent in order', async t => {
238 t.plan(2)
239 let runs = 0
240
241 class Root extends BaseContainer {
242 run (m) {
243 if (!runs) {
244 runs++
245
246 const [portRef1, portRef2] = this.exInterface.ports.createChannel()
247 const [portRef3, portRef4] = this.exInterface.ports.createChannel()
248
249 this.exInterface.ports.bind('two', portRef3)
250 this.exInterface.ports.bind('one', portRef1)
251
252 const message1 = this.exInterface.createMessage({
253 ports: [portRef2]
254 })
255 const message2 = this.exInterface.createMessage({
256 ports: [portRef4]
257 })
258
259 this.exInterface.ports.create('first', message1)
260 this.exInterface.ports.create('second', message2)
261
262 this.exInterface.send(portRef1, this.exInterface.createMessage())
263 this.exInterface.send(portRef3, this.exInterface.createMessage())
264 } else if (runs === 1) {
265 runs++
266 t.equals(m.data, 'first', 'should recive the first message')
267 } else if (runs === 2) {
268 t.equals(m.data, 'second', 'should recived the second message')
269 }
270 }
271 }
272
273 class First extends BaseContainer {
274 run (m) {
275 this.exInterface.incrementTicks(2)
276 return this.exInterface.send(m.fromPort, this.exInterface.createMessage({
277 data: 'first'
278 }))
279 }
280 }
281
282 class Second extends BaseContainer {
283 run (m) {
284 this.exInterface.incrementTicks(3)
285 return this.exInterface.send(m.fromPort, this.exInterface.createMessage({
286 data: 'second'
287 }))
288 }
289 }
290
291 const hypervisor = new Hypervisor(node.dag)
292
293 hypervisor.registerContainer('root', Root)
294 hypervisor.registerContainer('first', First)
295 hypervisor.registerContainer('second', Second)
296
297 const root = await hypervisor.createInstance('root')
298
299 const [portRef1, portRef2] = root.ports.createChannel()
300 root.ports.create('root', root.createMessage({
301 ports: [portRef2]
302 }))
303
304 root.ports.bind('first', portRef1)
305 const message = root.createMessage()
306 root.send(portRef1, message)
307 })
308
309 tape('message should arrive in the correct oder if sent in order', async t => {
310 t.plan(2)
311 let runs = 0
312
313 class Root extends BaseContainer {
314 run (m) {
315 if (!runs) {
316 runs++
317
318 const [portRef1, portRef2] = this.exInterface.ports.createChannel()
319 const [portRef3, portRef4] = this.exInterface.ports.createChannel()
320
321 this.exInterface.ports.bind('one', portRef1)
322 this.exInterface.ports.bind('two', portRef3)
323
324 const message1 = this.exInterface.createMessage({
325 ports: [portRef2]
326 })
327 const message2 = this.exInterface.createMessage({
328 ports: [portRef4]
329 })
330
331 this.exInterface.ports.create('first', message1)
332 this.exInterface.ports.create('second', message2)
333
334 this.exInterface.send(portRef1, this.exInterface.createMessage())
335 this.exInterface.send(portRef3, this.exInterface.createMessage())
336 } else if (runs === 1) {
337 runs++
338 t.equals(m.data, 'second', 'should recived the second message')
339 } else if (runs === 2) {
340 t.equals(m.data, 'first', 'should recive the first message')
341 }
342 }
343 }
344
345 class First extends BaseContainer {
346 run (m) {
347 this.exInterface.incrementTicks(2)
348 return this.exInterface.send(m.fromPort, this.exInterface.createMessage({
349 data: 'first'
350 }))
351 }
352 }
353
354 class Second extends BaseContainer {
355 run (m) {
356 this.exInterface.incrementTicks(1)
357 return this.exInterface.send(m.fromPort, this.exInterface.createMessage({
358 data: 'second'
359 }))
360 }
361 }
362
363 const hypervisor = new Hypervisor(node.dag)
364
365 hypervisor.registerContainer('root', Root)
366 hypervisor.registerContainer('first', First)
367 hypervisor.registerContainer('second', Second)
368
369 const root = await hypervisor.createInstance('root')
370
371 const [portRef1, portRef2] = root.ports.createChannel()
372 root.ports.create('root', root.createMessage({
373 ports: [portRef2]
374 }))
375
376 root.ports.bind('first', portRef1)
377 const message = root.createMessage()
378 root.send(portRef1, message)
379 })
380
381 tape('message should arrive in the correct oder if sent in order', async t => {
382 t.plan(2)
383 let runs = 0
384
385 class Root extends BaseContainer {
386 run (m) {
387 if (!runs) {
388 runs++
389 const [portRef1, portRef2] = this.exInterface.ports.createChannel()
390 const [portRef3, portRef4] = this.exInterface.ports.createChannel()
391
392 this.exInterface.ports.bind('one', portRef1)
393 this.exInterface.ports.bind('two', portRef3)
394
395 const message1 = this.exInterface.createMessage({
396 ports: [portRef2]
397 })
398 const message2 = this.exInterface.createMessage({
399 ports: [portRef4]
400 })
401
402 this.exInterface.ports.create('first', message1)
403 this.exInterface.ports.create('second', message2)
404
405 this.exInterface.send(portRef1, this.exInterface.createMessage())
406 this.exInterface.send(portRef3, this.exInterface.createMessage())
407
408 this.exInterface.incrementTicks(6)
409 } else if (runs === 1) {
410 runs++
411 t.equals(m.data, 'first', 'should recive the first message')
412 } else if (runs === 2) {
413 t.equals(m.data, 'second', 'should recived the second message')
414 }
415 }
416 }
417
418 class First extends BaseContainer {
419 run (m) {
420 this.exInterface.incrementTicks(1)
421 this.exInterface.send(m.fromPort, this.exInterface.createMessage({
422 data: 'first'
423 }))
424 }
425 }
426
427 class Second extends BaseContainer {
428 run (m) {
429 this.exInterface.incrementTicks(2)
430 this.exInterface.send(m.fromPort, this.exInterface.createMessage({
431 data: 'second'
432 }))
433 }
434 }
435
436 const hypervisor = new Hypervisor(node.dag)
437
438 hypervisor.registerContainer('root', Root)
439 hypervisor.registerContainer('first', First)
440 hypervisor.registerContainer('second', Second)
441
442 const root = await hypervisor.createInstance('root')
443 const [portRef1, portRef2] = root.ports.createChannel()
444 root.ports.create('root', root.createMessage({
445 ports: [portRef2]
446 }))
447
448 root.ports.bind('first', portRef1)
449 const message = root.createMessage()
450 root.send(portRef1, message)
451 })
452
453 tape('saturation', async t => {
454 t.plan(2)
455 let runs = 0
456
457 class Root extends BaseContainer {
458 run (m) {
459 if (!runs) {
460 runs++
461 const [portRef1, portRef2] = this.exInterface.ports.createChannel()
462 const [portRef3, portRef4] = this.exInterface.ports.createChannel()
463
464 this.exInterface.ports.bind('one', portRef1)
465 this.exInterface.ports.bind('two', portRef3)
466
467 const message1 = this.exInterface.createMessage({
468 ports: [portRef2]
469 })
470 const message2 = this.exInterface.createMessage({
471 ports: [portRef4]
472 })
473
474 this.exInterface.ports.create('first', message1)
475 this.exInterface.ports.create('second', message2)
476
477 this.exInterface.send(portRef1, this.exInterface.createMessage())
478 this.exInterface.send(portRef3, this.exInterface.createMessage())
479
480 this.exInterface.incrementTicks(6)
481 } else if (runs === 1) {
482 runs++
483 t.equals(m.data, 'first', 'should recive the first message')
484 } else if (runs === 2) {
485 runs++
486 t.equals(m.data, 'second', 'should recived the second message')
487 }
488 }
489 }
490
491 class First extends BaseContainer {
492 run (m) {
493 this.exInterface.incrementTicks(2)
494 this.exInterface.send(m.fromPort, this.exInterface.createMessage({
495 data: 'first'
496 }))
497 }
498 }
499
500 class Second extends BaseContainer {
501 run (m) {
502 this.exInterface.incrementTicks(3)
503 console.log('sending second', this.exInterface.ticks)
504 this.exInterface.send(m.fromPort, this.exInterface.createMessage({
505 data: 'second'
506 }))
507 }
508 }
509
510 class Waiter extends BaseContainer {
511 initialize () {
512 return new Promise((resolve, reject) => {
513 setTimeout(() => {
514 resolve()
515 }, 200)
516 })
517 }
518 }
519
520 const hypervisor = new Hypervisor(node.dag)
521
522 hypervisor.registerContainer('root', Root)
523 hypervisor.registerContainer('first', First)
524 hypervisor.registerContainer('second', Second)
525 hypervisor.registerContainer('waiter', Waiter)
526
527 const root = await hypervisor.createInstance('root')
528 const [portRef1, portRef2] = root.ports.createChannel()
529
530 const message = root.createMessage()
531 root.send(portRef1, message)
532 root.ports.bind('first', portRef1)
533 root.ports.create('root', root.createMessage({
534 ports: [portRef2]
535 }))
536
537 const [portRef3, portRef4] = root.ports.createChannel()
538 root.ports.bind('sencond', portRef3)
539 root.ports.create('waiter', root.createMessage({
540 ports: [portRef4]
541 }))
542
543 root.incrementTicks(100)
544 root.send(portRef1, root.createMessage({data: 'testss'}))
545 hypervisor.scheduler.done(root.id)
546 })
547
548 tape('message should arrive in the correct order, even in a tie of ticks', async t => {
549 t.plan(2)
550
551 let runs = 0
552
553 class Root extends BaseContainer {
554 run (m) {
555 if (!runs) {
556 runs++
557 const [portRef1, portRef2] = this.exInterface.ports.createChannel()
558 const [portRef3, portRef4] = this.exInterface.ports.createChannel()
559
560 this.exInterface.ports.bind('two', portRef3)
561 this.exInterface.ports.bind('one', portRef1)
562
563 const message1 = this.exInterface.createMessage({
564 ports: [portRef2]
565 })
566 const message2 = this.exInterface.createMessage({
567 ports: [portRef4]
568 })
569
570 this.exInterface.ports.create('first', message1)
571 this.exInterface.ports.create('second', message2)
572
573 this.exInterface.send(portRef1, this.exInterface.createMessage())
574 this.exInterface.send(portRef3, this.exInterface.createMessage())
575
576 this.exInterface.incrementTicks(6)
577 } else if (runs === 1) {
578 runs++
579 t.equals(m.data, 'second', 'should recived the second message')
580 } else if (runs === 2) {
581 t.equals(m.data, 'first', 'should recive the first message')
582 }
583 }
584 }
585
586 class First extends BaseContainer {
587 run (m) {
588 this.exInterface.incrementTicks(2)
589 return this.exInterface.send(m.fromPort, this.exInterface.createMessage({
590 data: 'first'
591 }))
592 }
593 }
594
595 class Second extends BaseContainer {
596 run (m) {
597 this.exInterface.incrementTicks(2)
598 return this.exInterface.send(m.fromPort, this.exInterface.createMessage({
599 data: 'second'
600 }))
601 }
602 }
603
604 const hypervisor = new Hypervisor(node.dag)
605
606 hypervisor.registerContainer('root', Root)
607 hypervisor.registerContainer('first', First)
608 hypervisor.registerContainer('second', Second)
609
610 const root = await hypervisor.createInstance('root')
611 const [portRef1, portRef2] = root.ports.createChannel()
612 const message = root.createMessage()
613
614 root.send(portRef1, message)
615 root.ports.bind('first', portRef1)
616 root.ports.create('root', root.createMessage({
617 ports: [portRef2]
618 }))
619 })
620
621 tape('message should arrive in the correct order, with a tie in ticks but with differnt proity', async t => {
622 t.plan(2)
623
624 let runs = 0
625
626 class Root extends BaseContainer {
627 run (m) {
628 if (!runs) {
629 runs++
630 const [portRef1, portRef2] = this.exInterface.ports.createChannel()
631 const [portRef3, portRef4] = this.exInterface.ports.createChannel()
632
633 this.exInterface.ports.bind('one', portRef1)
634 this.exInterface.ports.bind('two', portRef3)
635
636 const message1 = this.exInterface.createMessage({
637 ports: [portRef2]
638 })
639 const message2 = this.exInterface.createMessage({
640 ports: [portRef4]
641 })
642
643 this.exInterface.ports.create('first', message1)
644 this.exInterface.ports.create('second', message2)
645
646 this.exInterface.send(portRef1, this.exInterface.createMessage())
647 this.exInterface.send(portRef3, this.exInterface.createMessage())
648
649 this.exInterface.incrementTicks(6)
650 } else if (runs === 1) {
651 runs++
652 t.equals(m.data, 'first', 'should recive the first message')
653 } else if (runs === 2) {
654 t.equals(m.data, 'second', 'should recived the second message')
655 }
656 }
657 }
658
659 class First extends BaseContainer {
660 run (m) {
661 this.exInterface.incrementTicks(2)
662 return this.exInterface.send(m.fromPort, this.exInterface.createMessage({
663 data: 'first'
664 }))
665 }
666 }
667
668 class Second extends BaseContainer {
669 run (m) {
670 this.exInterface.incrementTicks(2)
671 return this.exInterface.send(m.fromPort, this.exInterface.createMessage({
672 data: 'second'
673 }))
674 }
675 }
676
677 const hypervisor = new Hypervisor(node.dag)
678
679 hypervisor.registerContainer('root', Root)
680 hypervisor.registerContainer('first', First)
681 hypervisor.registerContainer('second', Second)
682
683 const root = await hypervisor.createInstance('root')
684 const [portRef1, portRef2] = root.ports.createChannel()
685 const message = root.createMessage()
686
687 root.send(portRef1, message)
688 root.ports.bind('first', portRef1)
689 root.ports.create('root', root.createMessage({
690 ports: [portRef2]
691 }))
692 })
693
694 tape('send to the same container at the same time', async t => {
695 t.plan(2)
696
697 let runs = 0
698 let instance
699
700 class Root extends BaseContainer {
701 run (m) {
702 let one = this.exInterface.ports.get('one')
703 if (!one) {
704 const [portRef1, portRef2] = this.exInterface.ports.createChannel()
705 this.exInterface.ports.bind('one', portRef1)
706 const message1 = this.exInterface.createMessage({
707 ports: [portRef2]
708 })
709 this.exInterface.ports.create('first', message1)
710 } else {
711 this.exInterface.send(one, this.exInterface.createMessage())
712 this.exInterface.send(one, this.exInterface.createMessage())
713 }
714 }
715 }
716
717 class First extends BaseContainer {
718 run (m) {
719 ++runs
720 if (runs === 2) {
721 t.equals(instance, this, 'should have same instances')
722 } else {
723 instance = this
724 }
725 }
726 }
727
728 const hypervisor = new Hypervisor(node.dag)
729
730 hypervisor.registerContainer('root', Root)
731 hypervisor.registerContainer('first', First)
732
733 const root = await hypervisor.createInstance('root')
734 const [portRef1, portRef2] = root.ports.createChannel()
735 root.ports.bind('first', portRef1)
736 root.ports.create('root', root.createMessage({
737 ports: [portRef2]
738 }))
739
740 const message = root.createMessage()
741 root.send(portRef1, message)
742 await hypervisor.createStateRoot()
743 root.send(portRef1, root.createMessage())
744 await hypervisor.createStateRoot()
745 t.equals(runs, 2)
746 })
747
748 tape('checking ports', async t => {
749 t.plan(4)
750 const hypervisor = new Hypervisor(node.dag)
751 hypervisor.registerContainer('base', BaseContainer)
752
753 const root = await hypervisor.createInstance('base')
754
755 const [portRef1, portRef2] = root.ports.createChannel()
756 root.ports.create('base', root.createMessage({
757 ports: [portRef2]
758 }))
759 root.ports.bind('test', portRef1)
760
761 try {
762 root.createMessage({
763 ports: [portRef1]
764 })
765 } catch (e) {
766 t.pass('should thow if sending a port that is bound')
767 }
768
769 try {
770 await root.ports.bind('test', portRef1)
771 } catch (e) {
772 t.pass('should thow if binding an already bound port')
773 }
774
775 try {
776 const [portRef3] = root.ports.createChannel()
777 await root.ports.bind('test', portRef3)
778 } catch (e) {
779 t.pass('should thow if binding an already bound name')
780 }
781
782 await root.ports.unbind('test')
783 const message = root.createMessage({
784 ports: [portRef1]
785 })
786 t.equals(message.ports[0], portRef1, 'should create a message if the port is unbound')
787 })
788
789 tape('port deletion', async t => {
790 const expectedSr = {
791 '/': 'zdpuB2QXxn1KQtLFfBqaritTRoe5BuKP5sNFSrPtRT6sxkY7Z'
792 }
793 class Root extends BaseContainer {
794 run (m) {
795 const [portRef1, portRef2] = this.exInterface.ports.createChannel()
796 this.exInterface.ports.bind('one', portRef1)
797 const message1 = this.exInterface.createMessage({
798 ports: [portRef2]
799 })
800
801 this.exInterface.ports.create('first', message1)
802 this.exInterface.send(portRef1, this.exInterface.createMessage())
803 this.exInterface.incrementTicks(6)
804 }
805 }
806
807 class First extends BaseContainer {
808 run (m) {
809 this.exInterface.incrementTicks(2)
810 this.exInterface.ports.delete('root')
811 }
812 }
813
814 const hypervisor = new Hypervisor(node.dag)
815
816 hypervisor.registerContainer('root', Root)
817 hypervisor.registerContainer('first', First)
818
819 const root = await hypervisor.createInstance('root')
820 const [portRef1, portRef2] = root.ports.createChannel()
821 root.ports.bind('first', portRef1)
822 root.ports.create('root', root.createMessage({
823 ports: [portRef2]
824 }))
825
826 const message = root.createMessage()
827 root.send(portRef1, message)
828
829 const sr = await hypervisor.createStateRoot()
830 t.deepEquals(sr, expectedSr, 'should produce the corret state root')
831
832 t.end()
833 })
834
835 tape('clear unbounded ports', async t => {
836 const expectedSr = {
837 '/': 'zdpuB2QXxn1KQtLFfBqaritTRoe5BuKP5sNFSrPtRT6sxkY7Z'
838 }
839 class Root extends BaseContainer {
840 run (m) {
841 this.exInterface.ports.create('root')
842 }
843 }
844
845 const hypervisor = new Hypervisor(node.dag)
846
847 hypervisor.registerContainer('root', Root)
848
849 const root = await hypervisor.createInstance('root')
850 const [portRef1, portRef2] = root.ports.createChannel()
851 root.ports.bind('first', portRef1)
852 root.ports.create('root', root.createMessage({
853 ports: [portRef2]
854 }))
855
856 const message = root.createMessage()
857 root.send(portRef1, message)
858 const sr = await hypervisor.createStateRoot()
859 t.deepEquals(sr, expectedSr, 'should produce the corret state root')
860
861 t.end()
862 })
863
864 tape('should remove subgraphs', async t => {
865 const expectedSr = {
866 '/': 'zdpuB2QXxn1KQtLFfBqaritTRoe5BuKP5sNFSrPtRT6sxkY7Z'
867 }
868 class Root extends BaseContainer {
869 run (m) {
870 const [, portRef2] = this.exInterface.ports.createChannel()
871 this.exInterface.ports.create('sub', this.exInterface.createMessage({
872 ports: [portRef2]
873 }))
874 }
875 }
876
877 class Sub extends BaseContainer {
878 initailize (message) {
879 this.exInterface.ports.bind('root', message.ports[0])
880 const [portRef1, portRef2] = root.ports.createChannel()
881 root.ports.bind('child', portRef1)
882 root.ports.create('root', root.createMessage({
883 ports: [portRef2]
884 }))
885 }
886 }
887
888 const hypervisor = new Hypervisor(node.dag)
889
890 hypervisor.registerContainer('root', Root)
891 hypervisor.registerContainer('sub', Sub)
892
893 const root = await hypervisor.createInstance('root')
894 const [portRef1, portRef2] = root.ports.createChannel()
895 root.ports.bind('first', portRef1)
896 root.ports.create('root', root.createMessage({
897 ports: [portRef2]
898 }))
899
900 root.send(portRef1, root.createMessage())
901 const sr = await hypervisor.createStateRoot()
902
903 t.deepEquals(sr, expectedSr, 'should produce the corret state root')
904 t.end()
905 })
906
907 tape('should not remove connected nodes', async t => {
908 const expectedSr = {
909 '/': 'zdpuAwsZTd5mRZBCYA1FJSHrpYDPgSZSiaTQp9xkUeajaoMHM'
910 }
911 class Root extends BaseContainer {
912 run (m) {
913 if (m.ports.length) {
914 const port = this.exInterface.ports.get('test1')
915 this.exInterface.send(port, m)
916 this.exInterface.ports.unbind('test1')
917 } else {
918 const [portRef1, portRef2] = this.exInterface.ports.createChannel()
919 this.exInterface.ports.create('sub', this.exInterface.createMessage({
920 ports: [portRef2]
921 }))
922 this.exInterface.ports.bind('test1', portRef1)
923
924 const [portRef3, portRef4] = this.exInterface.ports.createChannel()
925 this.exInterface.ports.create('sub', this.exInterface.createMessage({
926 ports: [portRef4]
927 }))
928 this.exInterface.ports.bind('test2', portRef3)
929 this.exInterface.send(portRef3, this.exInterface.createMessage({
930 data: 'getChannel'
931 }))
932 }
933 }
934 }
935
936 class Sub extends BaseContainer {
937 run (message) {
938 if (message.data === 'getChannel') {
939 const ports = this.exInterface.ports.createChannel()
940 this.exInterface.ports.bind('channel', ports[0])
941 this.exInterface.send(message.fromPort, this.exInterface.createMessage({
942 data: 'bindPort',
943 ports: [ports[1]]
944 }))
945 } else if (message.data === 'bindPort') {
946 this.exInterface.ports.bind('channel', message.ports[0])
947 }
948 }
949 }
950
951 const hypervisor = new Hypervisor(node.dag)
952
953 hypervisor.registerContainer('root', Root)
954 hypervisor.registerContainer('sub', Sub)
955
956 const root = await hypervisor.createInstance('root')
957 const [portRef1, portRef2] = root.ports.createChannel()
958 root.ports.bind('first', portRef1)
959 root.ports.create('root', root.createMessage({
960 ports: [portRef2]
961 }))
962
963 root.send(portRef1, root.createMessage())
964 const sr = await hypervisor.createStateRoot()
965 t.deepEquals(sr, expectedSr, 'should produce the corret state root')
966 // await hypervisor.graph.tree(sr, Infinity)
967
968 t.end()
969 })
970
971 tape('should remove multiple subgraphs', async t => {
972 const expectedSr = {
973 '/': 'zdpuAmi9tkYTpoVsZvqQgxpQFRhCgYFVv4W3fjjfVhf1j8swv'
974 }
975 class Root extends BaseContainer {
976 run (m) {
977 if (m.ports.length) {
978 const port = this.exInterface.ports.get('test1')
979 this.exInterface.send(port, m)
980 this.exInterface.ports.unbind('test1')
981 this.exInterface.ports.unbind('test2')
982 } else {
983 const [portRef1, portRef2] = this.exInterface.ports.createChannel()
984 this.exInterface.ports.create('sub', this.exInterface.createMessage({
985 ports: [portRef2]
986 }))
987 this.exInterface.ports.bind('test1', portRef1)
988
989 const [portRef3, portRef4] = this.exInterface.ports.createChannel()
990 this.exInterface.ports.create('sub', this.exInterface.createMessage({
991 ports: [portRef4]
992 }))
993 this.exInterface.ports.bind('test2', portRef3)
994 this.exInterface.send(portRef3, this.exInterface.createMessage({
995 data: 'getChannel'
996 }))
997 }
998 }
999 }
1000
1001 class Sub extends BaseContainer {
1002 run (message) {
1003 if (message.data === 'getChannel') {
1004 const ports = this.exInterface.ports.createChannel()
1005 this.exInterface.ports.bind('channel', ports[0])
1006 this.exInterface.send(message.fromPort, this.exInterface.createMessage({
1007 data: 'bindPort',
1008 ports: [ports[1]]
1009 }))
1010 } else if (message.data === 'bindPort') {
1011 this.exInterface.ports.bind('channel', message.ports[0])
1012 }
1013 }
1014 }
1015
1016 const hypervisor = new Hypervisor(node.dag)
1017
1018 hypervisor.registerContainer('root', Root)
1019 hypervisor.registerContainer('sub', Sub)
1020
1021 const root = await hypervisor.createInstance('root')
1022
1023 const [portRef1, portRef2] = root.ports.createChannel()
1024 root.ports.bind('first', portRef1)
1025 root.ports.create('root', root.createMessage({
1026 ports: [portRef2]
1027 }))
1028
1029 root.send(portRef1, root.createMessage())
1030
1031 const sr = await hypervisor.createStateRoot()
1032 t.deepEquals(sr, expectedSr, 'should produce the corret state root')
1033
1034 t.end()
1035 })
1036})
1037

Built with git-ssb-web