git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 124610c716c1ba494bca1d28f39840fff9c2fa6b

Files: 124610c716c1ba494bca1d28f39840fff9c2fa6b / tests / wasmContainer.js

3910 bytesRaw
1const tape = require('tape')
2const fs = require('fs')
3const Message = require('../message.js')
4const Hypervisor = require('../')
5const WasmContainer = require('../wasmContainer.js')
6
7const level = require('level-browserify')
8const RadixTree = require('dfinity-radix-tree')
9const db = level('./testdb')
10
11let tester
12
13class TestWasmContainer extends WasmContainer {
14 constructor (actor) {
15 super(actor)
16 this._storage = new Map()
17 }
18 getInterface (funcRef) {
19 const orginal = super.getInterface(funcRef)
20 return Object.assign(orginal, {
21 test: {
22 check: (a, b) => {
23 tester.equals(a, b)
24 }
25 }
26 })
27 }
28}
29
30tape('basic', async t => {
31 t.plan(2)
32 tester = t
33 const expectedState = {
34 '/': Buffer.from('4494963fb0e02312510e675fbca8b60b6e03bd00', 'hex')
35 }
36
37 const tree = new RadixTree({
38 db
39 })
40
41 const wasm = fs.readFileSync('./wasm/reciever.wasm')
42
43 const hypervisor = new Hypervisor(tree)
44 hypervisor.registerContainer(TestWasmContainer)
45
46 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
47
48 const message = new Message({
49 funcRef: module.getFuncRef('receive'),
50 funcArguments: [5]
51 })
52 hypervisor.send(message)
53 const stateRoot = await hypervisor.createStateRoot()
54 t.deepEquals(stateRoot, expectedState, 'expected root!')
55})
56
57tape('two communicating actors', async t => {
58 t.plan(2)
59 tester = t
60 const expectedState = {
61 '/': Buffer.from('8c230b5f0f680199b24ecd1800c2970dfca7cfdc', 'hex')
62 }
63
64 const tree = new RadixTree({db})
65
66 const recieverWasm = fs.readFileSync('./wasm/reciever.wasm')
67 const callerWasm = fs.readFileSync('./wasm/caller.wasm')
68
69 const hypervisor = new Hypervisor(tree)
70 hypervisor.registerContainer(TestWasmContainer)
71
72 const {module: receiverMod} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm)
73 const {module: callerMod} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm)
74 const message = new Message({
75 funcRef: callerMod.getFuncRef('call'),
76 funcArguments: [receiverMod.getFuncRef('receive')]
77 })
78
79 hypervisor.send(message)
80 const stateRoot = await hypervisor.createStateRoot()
81 t.deepEquals(stateRoot, expectedState, 'expected root!')
82})
83
84tape('two communicating actors with callback', async t => {
85 t.plan(2)
86 tester = t
87 const expectedState = {
88 '/': Buffer.from('9bf27cf07b75a90e0af530e2df73e3102482b24a', 'hex')
89 }
90
91 const tree = new RadixTree({
92 db
93 })
94
95 const recieverWasm = fs.readFileSync('./wasm/funcRef_reciever.wasm')
96 const callerWasm = fs.readFileSync('./wasm/funcRef_caller.wasm')
97
98 const hypervisor = new Hypervisor(tree)
99 hypervisor.registerContainer(TestWasmContainer)
100
101 const {module: receiverMod} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm)
102 const {module: callerMod} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm)
103
104 const message = new Message({
105 funcRef: callerMod.getFuncRef('call'),
106 funcArguments: [receiverMod.getFuncRef('receive')]
107 })
108
109 hypervisor.send(message)
110 const stateRoot = await hypervisor.createStateRoot()
111 t.deepEquals(stateRoot, expectedState, 'expected root!')
112})
113
114tape('externalize/internalize memory', async t => {
115 t.plan(1)
116 tester = t
117 const expectedState = {
118 '/': Buffer.from('4494963fb0e02312510e675fbca8b60b6e03bd00', 'hex')
119 }
120
121 const tree = new RadixTree({
122 db
123 })
124
125 const wasm = fs.readFileSync('./wasm/memory.wasm')
126
127 const hypervisor = new Hypervisor(tree)
128 hypervisor.registerContainer(TestWasmContainer)
129
130 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
131
132 const message = new Message({
133 funcRef: module.getFuncRef('test')
134 }).on('done', actor => {
135 const a = actor.container.getMemory(0, 5)
136 const b = actor.container.getMemory(5, 5)
137 t.deepEquals(a, b, 'should copy memory correctly')
138 })
139 hypervisor.send(message)
140})
141

Built with git-ssb-web