git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit eeb33728948452196618c680fb2c259026a4098d

updated to use ABI for creation service

Signed-off-by: wanderer <mjbecze@gmail.com>
wanderer committed on 10/16/2017, 10:47:00 PM
Parent: e301c1ce5f932e363af5cfff39807eef96443627

Files changed

creationService.jschanged
tests/index.jschanged
creationService.jsView
@@ -7,9 +7,9 @@
77 this.scheduler = this.hypervisor.scheduler
88 }
99
1010 queue (port, message) {
11- if (message.data.type) {
11 + if (message.data[0] === 0x00) {
1212 let id
1313 if (message.fromId) {
1414 const creator = this.scheduler.getInstance(message.fromId)
1515 id = creator.generateNextId()
@@ -46,13 +46,14 @@
4646 const idHash = await this._getHashFromObj(encoded)
4747 const state = {
4848 nonce: 0,
4949 ports: {},
50- type: message.data.type
50 + type: message.data[1]
5151 }
5252
53- if (message.data.code && message.data.code.length) {
54- state.code = message.data.code
53 + const code = message.data.slice(2)
54 + if (code.length) {
55 + state.code = code
5556 }
5657
5758 // create the container instance
5859 const instance = await this.hypervisor._loadInstance(idHash, state)
tests/index.jsView
@@ -6,8 +6,19 @@
66 const level = require('level')
77 const RadixTree = require('dfinity-radix-tree')
88 const db = level('./testdb')
99
10 +class CreationMessage extends Message {
11 + constructor (params) {
12 + const buf = [Buffer.from([0x0]), Buffer.from([params.data.type])]
13 + if (params.data.code) {
14 + buf.push(params.data.code)
15 + }
16 + params.data = Buffer.concat(buf)
17 + super(params)
18 + }
19 +}
20 +
1021 class BaseContainer extends AbstractContainer {
1122 onCreation (message) {
1223 const port = message.ports[0]
1324 if (port) {
@@ -40,9 +51,9 @@
4051 hypervisor.registerContainer(testVMContainer)
4152
4253 const port = hypervisor.creationService.getPort()
4354
44- let rootContainer = await hypervisor.send(port, new Message({
55 + let rootContainer = await hypervisor.send(port, new CreationMessage({
4556 data: {
4657 type: testVMContainer.typeId
4758 }
4859 }))
@@ -51,9 +62,9 @@
5162
5263 hypervisor.pin(rootContainer)
5364
5465 const [portRef1, portRef2] = rootContainer.ports.createChannel()
55- const initMessage = rootContainer.createMessage({
66 + const initMessage = new CreationMessage({
5667 data: {
5768 code: Buffer.from('test code'),
5869 type: testVMContainer.typeId
5970 },
@@ -92,9 +103,9 @@
92103 const hypervisor = new Hypervisor(tree)
93104 hypervisor.registerContainer(testVMContainer)
94105
95106 const creationPort = hypervisor.creationService.getPort()
96- let root = await hypervisor.send(creationPort, new Message({
107 + let root = await hypervisor.send(creationPort, new CreationMessage({
97108 data: {
98109 type: testVMContainer.typeId
99110 }
100111 }))
@@ -106,9 +117,9 @@
106117 const [portRef1, portRef2] = root.ports.createChannel()
107118
108119 await Promise.all([
109120 root.ports.bind('one', portRef1),
110- root.send(creationPort, root.createMessage({
121 + root.send(creationPort, new CreationMessage({
111122 data: {
112123 type: testVMContainer.typeId
113124 },
114125 ports: [portRef2]
@@ -160,9 +171,9 @@
160171 const [portRef1, portRef2] = this.kernel.ports.createChannel()
161172 const port = this.kernel.hypervisor.creationService.getPort()
162173
163174 await Promise.all([
164- this.kernel.send(port, this.kernel.createMessage({
175 + this.kernel.send(port, new CreationMessage({
165176 data: {
166177 type: testVMContainer2.typeId
167178 },
168179 ports: [portRef2]
@@ -179,9 +190,9 @@
179190 hypervisor.registerContainer(testVMContainer)
180191 hypervisor.registerContainer(testVMContainer2)
181192
182193 let creationPort = hypervisor.creationService.getPort()
183- let root = await hypervisor.send(creationPort, new Message({
194 + let root = await hypervisor.send(creationPort, new CreationMessage({
184195 data: {
185196 type: testVMContainer.typeId
186197 }
187198 }))
@@ -193,9 +204,9 @@
193204 const [portRef1, portRef2] = root.ports.createChannel()
194205
195206 message = root.createMessage()
196207 await Promise.all([
197- root.send(creationPort, root.createMessage({
208 + root.send(creationPort, new CreationMessage({
198209 data: {
199210 type: testVMContainer.typeId
200211 },
201212 ports: [portRef2]
@@ -250,9 +261,9 @@
250261 const hypervisor = new Hypervisor(tree)
251262
252263 hypervisor.registerContainer(Root)
253264 const creationPort = hypervisor.creationService.getPort()
254- const root = await hypervisor.send(creationPort, new Message({
265 + const root = await hypervisor.send(creationPort, new CreationMessage({
255266 data: {
256267 type: Root.typeId
257268 }
258269 }))
@@ -283,15 +294,15 @@
283294 runs++
284295 const [portRef1, portRef2] = this.kernel.ports.createChannel()
285296 const [portRef3, portRef4] = this.kernel.ports.createChannel()
286297
287- const message1 = this.kernel.createMessage({
298 + const message1 = new CreationMessage({
288299 data: {
289300 type: First.typeId
290301 },
291302 ports: [portRef2]
292303 })
293- const message2 = this.kernel.createMessage({
304 + const message2 = new CreationMessage({
294305 data: {
295306 type: Waiter.typeId
296307 },
297308 ports: [portRef4]
@@ -313,9 +324,9 @@
313324 t.equals(m.data, 'second', 'should recive the second message')
314325 }
315326 }
316327 static get typeId () {
317- return 299
328 + return 211
318329 }
319330 }
320331
321332 class First extends BaseContainer {
@@ -345,9 +356,9 @@
345356 hypervisor.registerContainer(Root)
346357 hypervisor.registerContainer(First)
347358 hypervisor.registerContainer(Waiter)
348359
349- let root = await hypervisor.send(creationPort, new Message({
360 + let root = await hypervisor.send(creationPort, new CreationMessage({
350361 data: {
351362 type: Root.typeId
352363 }
353364 }))
@@ -360,9 +371,9 @@
360371 const message = root.createMessage()
361372 await Promise.all([
362373 root.send(portRef1, message),
363374 root.ports.bind('first', portRef1),
364- root.send(creationPort, root.createMessage({
375 + root.send(creationPort, new CreationMessage({
365376 data: {
366377 type: Root.typeId
367378 },
368379 ports: [portRef2]
@@ -389,16 +400,16 @@
389400 runs++
390401 const [portRef1, portRef2] = this.kernel.ports.createChannel()
391402 const [portRef3, portRef4] = this.kernel.ports.createChannel()
392403
393- const message1 = this.kernel.createMessage({
404 + const message1 = new CreationMessage({
394405 data: {
395406 type: First.typeId
396407 },
397408 ports: [portRef2]
398409 })
399410
400- const message2 = this.kernel.createMessage({
411 + const message2 = new CreationMessage({
401412 data: {
402413 type: Second.typeId
403414 },
404415 ports: [portRef4]
@@ -424,9 +435,9 @@
424435 t.equals(m.data, 'third', 'should recived the third message')
425436 }
426437 }
427438 static get typeId () {
428- return 299
439 + return 112
429440 }
430441 }
431442
432443 class First extends BaseContainer {
@@ -469,9 +480,9 @@
469480 hypervisor.registerContainer(First)
470481 hypervisor.registerContainer(Second)
471482 hypervisor.registerContainer(Waiter)
472483
473- let root = await hypervisor.send(creationPort, new Message({
484 + let root = await hypervisor.send(creationPort, new CreationMessage({
474485 data: {
475486 type: Root.typeId
476487 }
477488 }))
@@ -485,16 +496,16 @@
485496 const message = root.createMessage()
486497 await Promise.all([
487498 root.send(portRef1, message),
488499 root.ports.bind('first', portRef1),
489- root.send(creationPort, root.createMessage({
500 + root.send(creationPort, new CreationMessage({
490501 data: {
491502 type: Root.typeId
492503 },
493504 ports: [portRef2]
494505 })),
495506 root.ports.bind('sencond', portRef3),
496- root.send(creationPort, root.createMessage({
507 + root.send(creationPort, new CreationMessage({
497508 data: {
498509 type: Waiter.typeId
499510 },
500511 ports: [portRef4]
@@ -524,9 +535,9 @@
524535 async onMessage (m) {
525536 let one = this.kernel.ports.get('one')
526537 if (!one) {
527538 const [portRef1, portRef2] = this.kernel.ports.createChannel()
528- const message1 = this.kernel.createMessage({
539 + const message1 = new CreationMessage({
529540 data: {
530541 type: First.typeId
531542 },
532543 ports: [portRef2]
@@ -540,9 +551,9 @@
540551 ])
541552 }
542553 }
543554 static get typeId () {
544- return 299
555 + return 111
545556 }
546557 }
547558
548559 class First extends BaseContainer {
@@ -558,9 +569,9 @@
558569
559570 hypervisor.registerContainer(Root)
560571 hypervisor.registerContainer(First)
561572
562- let root = await hypervisor.send(creationPort, new Message({
573 + let root = await hypervisor.send(creationPort, new CreationMessage({
563574 data: {
564575 type: Root.typeId
565576 }
566577 }))
@@ -570,9 +581,9 @@
570581
571582 const [portRef1, portRef2] = root.ports.createChannel()
572583 await Promise.all([
573584 root.ports.bind('first', portRef1),
574- root.send(creationPort, root.createMessage({
585 + root.send(creationPort, new CreationMessage({
575586 data: {
576587 type: Root.typeId
577588 },
578589 ports: [portRef2]
@@ -598,9 +609,9 @@
598609 const hypervisor = new Hypervisor(tree)
599610 const creationPort = hypervisor.creationService.getPort()
600611 hypervisor.registerContainer(BaseContainer)
601612
602- let root = await hypervisor.send(creationPort, new Message({
613 + let root = await hypervisor.send(creationPort, new CreationMessage({
603614 data: {
604615 type: BaseContainer.typeId
605616 }
606617 }))
@@ -608,9 +619,9 @@
608619 hypervisor.pin(root)
609620 root = await hypervisor.getInstance(root.id)
610621
611622 const [portRef1, portRef2] = root.ports.createChannel()
612- root.send(creationPort, root.createMessage({
623 + root.send(creationPort, new CreationMessage({
613624 data: {
614625 type: BaseContainer.typeId
615626 },
616627 ports: [portRef2]
@@ -658,9 +669,9 @@
658669
659670 class Root extends BaseContainer {
660671 async onMessage (m) {
661672 const [portRef1, portRef2] = this.kernel.ports.createChannel()
662- const message1 = this.kernel.createMessage({
673 + const message1 = new CreationMessage({
663674 data: {
664675 type: First.typeId
665676 },
666677 ports: [portRef2]
@@ -680,16 +691,16 @@
680691 this.kernel.incrementTicks(2)
681692 return this.kernel.ports.delete('root')
682693 }
683694 static get typeId () {
684- return 299
695 + return 111
685696 }
686697 }
687698
688699 hypervisor.registerContainer(Root)
689700 hypervisor.registerContainer(First)
690701
691- let root = await hypervisor.send(creationPort, new Message({
702 + let root = await hypervisor.send(creationPort, new CreationMessage({
692703 data: {
693704 type: Root.typeId
694705 }
695706 }))
@@ -698,9 +709,9 @@
698709 root = await hypervisor.getInstance(root.id)
699710
700711 const [portRef1, portRef2] = root.ports.createChannel()
701712 await root.ports.bind('first', portRef1)
702- await root.send(creationPort, root.createMessage({
713 + await root.send(creationPort, new CreationMessage({
703714 data: {
704715 type: Root.typeId
705716 },
706717 ports: [portRef2]
@@ -729,9 +740,9 @@
729740 const creationPort = hypervisor.creationService.getPort()
730741
731742 class Root extends BaseContainer {
732743 onMessage (m) {
733- return this.kernel.send(creationPort, new Message({
744 + return this.kernel.send(creationPort, new CreationMessage({
734745 data: {
735746 type: Root.typeId
736747 }
737748 }))
@@ -739,9 +750,9 @@
739750 }
740751
741752 hypervisor.registerContainer(Root)
742753
743- let root = await hypervisor.send(creationPort, new Message({
754 + let root = await hypervisor.send(creationPort, new CreationMessage({
744755 data: {
745756 type: Root.typeId
746757 }
747758 }))
@@ -750,9 +761,9 @@
750761 hypervisor.pin(root)
751762
752763 const [portRef1, portRef2] = root.ports.createChannel()
753764 await root.ports.bind('first', portRef1)
754- await root.send(creationPort, root.createMessage({
765 + await root.send(creationPort, new CreationMessage({
755766 data: {
756767 type: Root.typeId
757768 },
758769 ports: [portRef2]
@@ -780,9 +791,9 @@
780791
781792 class Root extends BaseContainer {
782793 onMessage (m) {
783794 const [, portRef2] = this.kernel.ports.createChannel()
784- return this.kernel.send(creationPort, this.kernel.createMessage({
795 + return this.kernel.send(creationPort, new CreationMessage({
785796 data: {
786797 type: Sub.typeId
787798 },
788799 ports: [portRef2]
@@ -798,9 +809,9 @@
798809
799810 hypervisor.registerContainer(Root)
800811 hypervisor.registerContainer(Sub)
801812
802- let root = await hypervisor.send(creationPort, new Message({
813 + let root = await hypervisor.send(creationPort, new CreationMessage({
803814 data: {
804815 type: Root.typeId
805816 }
806817 }))
@@ -810,9 +821,9 @@
810821 hypervisor.pin(root)
811822
812823 const [portRef1, portRef2] = root.ports.createChannel()
813824 await root.ports.bind('first', portRef1)
814- await root.send(creationPort, root.createMessage({
825 + await root.send(creationPort, new CreationMessage({
815826 data: {
816827 type: Root.typeId
817828 },
818829 ports: [portRef2]
@@ -829,10 +840,11 @@
829840 tape('should not remove connected nodes', async t => {
830841 const tree = new RadixTree({
831842 db: db
832843 })
844 +
833845 const expectedSr = {
834- '/': Buffer.from('76711d128d0be5fe86833af5ab8f48afeec3410e', 'hex')
846 + '/': Buffer.from('9aeb0ce35c0ab845e7b273afe0329f826297124e', 'hex')
835847 }
836848
837849 const hypervisor = new Hypervisor(tree)
838850 const creationPort = hypervisor.creationService.getPort()
@@ -844,18 +856,18 @@
844856 await this.kernel.send(port, m)
845857 return this.kernel.ports.unbind('test1')
846858 } else {
847859 const [portRef1, portRef2] = this.kernel.ports.createChannel()
848- await this.kernel.send(creationPort, this.kernel.createMessage({
860 + await this.kernel.send(creationPort, new CreationMessage({
849861 data: {
850862 type: Sub.typeId
851863 },
852864 ports: [portRef2]
853865 }))
854866 await this.kernel.ports.bind('test1', portRef1)
855867
856868 const [portRef3, portRef4] = this.kernel.ports.createChannel()
857- await this.kernel.send(creationPort, this.kernel.createMessage({
869 + await this.kernel.send(creationPort, new CreationMessage({
858870 data: {
859871 type: Sub.typeId
860872 },
861873 ports: [portRef4]
@@ -881,16 +893,16 @@
881893 return this.kernel.ports.bind('channel', message.ports[0])
882894 }
883895 }
884896 static get typeId () {
885- return 299
897 + return 111
886898 }
887899 }
888900
889901 hypervisor.registerContainer(Root)
890902 hypervisor.registerContainer(Sub)
891903
892- let root = await hypervisor.send(creationPort, new Message({
904 + let root = await hypervisor.send(creationPort, new CreationMessage({
893905 data: {
894906 type: Root.typeId
895907 }
896908 }))
@@ -899,9 +911,9 @@
899911 hypervisor.pin(root)
900912
901913 const [portRef1, portRef2] = root.ports.createChannel()
902914 await root.ports.bind('first', portRef1)
903- await root.send(creationPort, root.createMessage({
915 + await root.send(creationPort, new CreationMessage({
904916 data: {
905917 type: Root.typeId
906918 },
907919 ports: [portRef2]
@@ -938,16 +950,16 @@
938950 } else {
939951 const [portRef1, portRef2] = this.kernel.ports.createChannel()
940952 const [portRef3, portRef4] = this.kernel.ports.createChannel()
941953 return Promise.all([
942- this.kernel.send(creationPort, this.kernel.createMessage({
954 + this.kernel.send(creationPort, new CreationMessage({
943955 data: {
944956 type: Sub.typeId
945957 },
946958 ports: [portRef2]
947959 })),
948960 this.kernel.ports.bind('test1', portRef1),
949- this.kernel.send(creationPort, this.kernel.createMessage({
961 + this.kernel.send(creationPort, new CreationMessage({
950962 data: {
951963 type: Sub.typeId
952964 },
953965 ports: [portRef4]
@@ -974,16 +986,16 @@
974986 return this.kernel.ports.bind('channel', message.ports[0])
975987 }
976988 }
977989 static get typeId () {
978- return 299
990 + return 111
979991 }
980992 }
981993
982994 hypervisor.registerContainer(Root)
983995 hypervisor.registerContainer(Sub)
984996
985- let root = await hypervisor.send(creationPort, new Message({
997 + let root = await hypervisor.send(creationPort, new CreationMessage({
986998 data: {
987999 type: Root.typeId
9881000 }
9891001 }))
@@ -993,9 +1005,9 @@
9931005
9941006 const [portRef1, portRef2] = root.ports.createChannel()
9951007 await Promise.all([
9961008 root.ports.bind('first', portRef1),
997- root.send(creationPort, root.createMessage({
1009 + root.send(creationPort, new CreationMessage({
9981010 data: {
9991011 type: Root.typeId
10001012 },
10011013 ports: [portRef2]
@@ -1034,9 +1046,9 @@
10341046 }
10351047
10361048 hypervisor.registerContainer(testVMContainer)
10371049
1038- let rootContainer = await hypervisor.send(creationPort, new Message({
1050 + let rootContainer = await hypervisor.send(creationPort, new CreationMessage({
10391051 data: {
10401052 type: testVMContainer.typeId
10411053 }
10421054 }))
@@ -1045,9 +1057,9 @@
10451057
10461058 hypervisor.pin(rootContainer)
10471059
10481060 const [portRef1, portRef2] = rootContainer.ports.createChannel()
1049- const initMessage = rootContainer.createMessage({
1061 + const initMessage = new CreationMessage({
10501062 data: {
10511063 type: testVMContainer.typeId
10521064 },
10531065 ports: [portRef2]
@@ -1081,9 +1093,9 @@
10811093
10821094 const hypervisor = new Hypervisor(tree)
10831095 const creationPort = hypervisor.creationService.getPort()
10841096 hypervisor.registerContainer(testVMContainer)
1085- const instance = await hypervisor.send(creationPort, new Message({
1097 + const instance = await hypervisor.send(creationPort, new CreationMessage({
10861098 data: {
10871099 type: testVMContainer.typeId
10881100 }
10891101 }))
@@ -1100,9 +1112,9 @@
11001112
11011113 const hypervisor = new Hypervisor(tree)
11021114 const creationPort = hypervisor.creationService.getPort()
11031115 hypervisor.registerContainer(testVMContainer)
1104- const oldInst = await hypervisor.send(creationPort, new Message({
1116 + const oldInst = await hypervisor.send(creationPort, new CreationMessage({
11051117 data: {
11061118 type: testVMContainer.typeId,
11071119 code: content
11081120 }
@@ -1123,9 +1135,9 @@
11231135 const creationPort = m.ports[0]
11241136 const [port1, port2] = this.kernel.ports.createChannel()
11251137 await this.kernel.ports.bind('child', port1)
11261138
1127- const message = this.kernel.createMessage({
1139 + const message = new CreationMessage({
11281140 data: {
11291141 type: TestVMContainer2.typeId
11301142 },
11311143 ports: [port2]
@@ -1146,9 +1158,9 @@
11461158
11471159 const port = hypervisor.creationService.getPort()
11481160 const port2 = hypervisor.creationService.getPort()
11491161
1150- const root = await hypervisor.send(port2, new Message({
1162 + const root = await hypervisor.send(port2, new CreationMessage({
11511163 data: {
11521164 type: TestVMContainer.typeId
11531165 },
11541166 ports: [port]
@@ -1193,9 +1205,9 @@
11931205 hypervisor.registerContainer(TestVMContainer)
11941206
11951207 const port = hypervisor.creationService.getPort()
11961208
1197- const root = await hypervisor.send(port, new Message({
1209 + const root = await hypervisor.send(port, new CreationMessage({
11981210 data: {
11991211 type: TestVMContainer.typeId
12001212 },
12011213 ports: [port]
@@ -1227,9 +1239,9 @@
12271239 hypervisor.registerContainer(TestVMContainer)
12281240
12291241 const port = hypervisor.creationService.getPort()
12301242
1231- await hypervisor.send(port, new Message({
1243 + await hypervisor.send(port, new CreationMessage({
12321244 data: {
12331245 type: TestVMContainer.typeId
12341246 },
12351247 ports: [port]

Built with git-ssb-web