git ssb

0+

wanderer🌟 / js-primea-wasm-container



Tree: e90fce266c4e2336bd515be986dad7190690424e

Files: e90fce266c4e2336bd515be986dad7190690424e / tests / index.js

10765 bytesRaw
1const tape = require('tape')
2const fs = require('fs')
3const path = require('path')
4const Buffer = require('safe-buffer').Buffer
5const {Message, FunctionRef} = require('primea-objects')
6const Hypervisor = require('primea-hypervisor')
7const EgressDriver = require('primea-hypervisor/egressDriver')
8const WasmContainer = require('../')
9
10const level = require('level-browserify')
11const RadixTree = require('dfinity-radix-tree')
12
13const db = level(`${__dirname}/testdb`)
14const WASM_PATH = path.join(__dirname, 'wasm')
15
16let tester
17
18class TestWasmContainer extends WasmContainer {
19 constructor (actor) {
20 super(actor)
21 this._storage = new Map()
22 }
23 getInterface (funcRef) {
24 const orginal = super.getInterface(funcRef)
25 const self = this
26 return Object.assign(orginal, {
27 test: {
28 check: (a, b) => {
29 tester.equals(a, b)
30 },
31 print: (dataRef) => {
32 let buf = self.refs.get(dataRef, 'data')
33 console.log(buf.toString())
34 }
35 }
36 })
37 }
38}
39
40tape('i64', async t => {
41 t.plan(1)
42 tester = t
43 const tree = new RadixTree({db})
44 let wasm = fs.readFileSync(WASM_PATH + '/i64.wasm')
45
46 const hypervisor = new Hypervisor(tree)
47 hypervisor.registerContainer(TestWasmContainer)
48
49 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
50 const funcRef = module.getFuncRef('main')
51 funcRef.gas = 322000
52
53 const message = new Message({
54 funcRef
55 })
56 hypervisor.send(message)
57})
58
59tape('get_gas_budget', async t => {
60 t.plan(2)
61 tester = t
62 const tree = new RadixTree({db})
63 let wasm = fs.readFileSync(WASM_PATH + '/get_gas_budget.wasm')
64
65 const hypervisor = new Hypervisor(tree)
66 hypervisor.registerContainer(TestWasmContainer)
67
68 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
69 const funcRef = module.getFuncRef('main')
70 funcRef.gas = 322000
71
72 const message = new Message({
73 funcRef
74 })
75 hypervisor.send(message)
76})
77
78tape('reintinalizing', async t => {
79 t.plan(1)
80 tester = t
81 const tree = new RadixTree({db})
82 let wasm = fs.readFileSync(WASM_PATH + '/reinternalize.wasm')
83
84 const hypervisor = new Hypervisor(tree)
85 hypervisor.registerContainer(TestWasmContainer)
86
87 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
88 const funcRef = module.getFuncRef('main')
89 funcRef.gas = 322000
90
91 const message = new Message({
92 funcRef
93 })
94 hypervisor.send(message)
95})
96
97tape('basic', async t => {
98 t.plan(1)
99 tester = t
100
101 const tree = new RadixTree({
102 db
103 })
104
105 const wasm = fs.readFileSync(WASM_PATH + '/reciever.wasm')
106
107 const hypervisor = new Hypervisor(tree)
108 hypervisor.registerContainer(TestWasmContainer)
109
110 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
111 const funcRef = module.getFuncRef('receive')
112 funcRef.gas = 300
113
114 const message = new Message({
115 funcRef,
116 funcArguments: [5]
117 })
118 hypervisor.send(message)
119 // const stateRoot = await hypervisor.createStateRoot()
120 // t.deepEquals(stateRoot, expectedState, 'expected root!')
121})
122
123tape('empty', async t => {
124 t.plan(1)
125 tester = t
126 const expectedState = Buffer.from('bb15867b26293aa36c774b8c82541dd64212ba9a', 'hex')
127
128 const tree = new RadixTree({
129 db
130 })
131
132 const wasm = fs.readFileSync(WASM_PATH + '/empty.wasm')
133
134 const hypervisor = new Hypervisor(tree)
135 hypervisor.registerContainer(TestWasmContainer)
136
137 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
138 const funcRef = module.getFuncRef('receive')
139 funcRef.gas = 300
140
141 const message = new Message({
142 funcRef,
143 funcArguments: [5]
144 })
145 hypervisor.send(message)
146 const stateRoot = await hypervisor.createStateRoot()
147 t.deepEquals(stateRoot, expectedState, 'expected root!')
148})
149
150tape('two communicating actors', async t => {
151 t.plan(1)
152 tester = t
153
154 const tree = new RadixTree({db})
155
156 const recieverWasm = fs.readFileSync(WASM_PATH + '/reciever.wasm')
157 const callerWasm = fs.readFileSync(WASM_PATH + '/caller.wasm')
158
159 const hypervisor = new Hypervisor(tree)
160 hypervisor.registerContainer(TestWasmContainer)
161
162 const {module: receiverMod} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm)
163 const {module: callerMod} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm)
164 const callFuncRef = callerMod.getFuncRef('call')
165 const recvFuncRef = receiverMod.getFuncRef('receive')
166 callFuncRef.gas = 100000
167 recvFuncRef.gas = 1000
168 const message = new Message({
169 funcRef: callFuncRef,
170 funcArguments: [recvFuncRef]
171 })
172
173 hypervisor.send(message)
174})
175
176tape('two communicating actors with callback', async t => {
177 t.plan(1)
178 tester = t
179
180 const tree = new RadixTree({
181 db
182 })
183
184 const recieverWasm = fs.readFileSync(WASM_PATH + '/funcRef_reciever.wasm')
185 const callerWasm = fs.readFileSync(WASM_PATH + '/funcRef_caller.wasm')
186
187 const hypervisor = new Hypervisor(tree)
188 hypervisor.registerContainer(TestWasmContainer)
189
190 const {module: callerMod} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm)
191 const {module: receiverMod} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm)
192
193 const callFuncRef = callerMod.getFuncRef('call')
194 const recvFuncRef = receiverMod.getFuncRef('receive')
195 callFuncRef.gas = 100000
196 recvFuncRef.gas = 100000
197
198 const message = new Message({
199 funcRef: callFuncRef,
200 funcArguments: [recvFuncRef]
201 })
202
203 hypervisor.send(message)
204})
205
206tape('two communicating actors with private callback', async t => {
207 t.plan(1)
208 tester = t
209
210 const tree = new RadixTree({
211 db
212 })
213
214 const recieverWasm = fs.readFileSync(WASM_PATH + '/funcRef_reciever.wasm')
215 const callerWasm = fs.readFileSync(WASM_PATH + '/private_caller.wasm')
216
217 const hypervisor = new Hypervisor(tree)
218 hypervisor.registerContainer(TestWasmContainer)
219
220 const {module: callerMod} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm)
221 const {module: receiverMod} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm)
222
223 const callFuncRef = callerMod.getFuncRef('call')
224 const recvFuncRef = receiverMod.getFuncRef('receive')
225 callFuncRef.gas = 100000
226 recvFuncRef.gas = 100000
227
228 const message = new Message({
229 funcRef: callFuncRef,
230 funcArguments: [recvFuncRef]
231 })
232
233 hypervisor.send(message)
234})
235
236tape('externalize/internalize memory', async t => {
237 t.plan(1)
238 tester = t
239 const tree = new RadixTree({
240 db
241 })
242
243 const wasm = fs.readFileSync(WASM_PATH + '/memory.wasm')
244
245 const hypervisor = new Hypervisor(tree)
246 hypervisor.registerContainer(TestWasmContainer)
247
248 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
249 const funcRef = module.getFuncRef('test')
250 funcRef.gas = 10000
251
252 const message = new Message({funcRef}).on('done', actor => {
253 const a = actor.container.get8Memory(0, 5)
254 const b = actor.container.get8Memory(5, 5)
255 t.deepEquals(a, b, 'should copy memory correctly')
256 })
257 hypervisor.send(message)
258})
259
260tape('externalize/internalize table', async t => {
261 t.plan(1)
262 tester = t
263 const tree = new RadixTree({
264 db
265 })
266
267 const wasm = fs.readFileSync(WASM_PATH + '/table.wasm')
268 const hypervisor = new Hypervisor(tree)
269 hypervisor.registerContainer(TestWasmContainer)
270
271 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
272
273 const funcRef = module.getFuncRef('test')
274 funcRef.gas = 10000
275
276 const message = new Message({funcRef}).on('done', actor => {
277 const a = actor.container.get8Memory(0, 8)
278 const b = actor.container.get8Memory(8, 8)
279 t.deepEquals(a, b, 'should copy memory correctly')
280 })
281 hypervisor.send(message)
282})
283
284tape('creation', async t => {
285 t.plan(1)
286 tester = t
287 const tree = new RadixTree({db})
288 let wasm = fs.readFileSync(WASM_PATH + '/creation.wasm')
289 let receiver = fs.readFileSync(WASM_PATH + '/reciever.wasm')
290
291 const hypervisor = new Hypervisor(tree)
292 hypervisor.registerContainer(TestWasmContainer)
293
294 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
295 const funcRef = module.getFuncRef('main')
296 funcRef.gas = 322000
297
298 const message = new Message({
299 funcRef,
300 funcArguments: [receiver]
301 })
302 hypervisor.send(message)
303})
304
305tape('storage', async t => {
306 tester = t
307 const tree = new RadixTree({db})
308 let wasm = fs.readFileSync(WASM_PATH + '/storage.wasm')
309
310 const egress = new EgressDriver()
311
312 egress.on('message', msg => {
313 t.equals(msg.funcArguments[0].toString(), 'hello world')
314 t.end()
315 })
316
317 const hypervisor = new Hypervisor(tree, [TestWasmContainer], [egress])
318
319 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
320 const funcRef = module.getFuncRef('main')
321 funcRef.gas = 322000
322
323 const message = new Message({
324 funcRef
325 })
326
327 hypervisor.send(message)
328
329 const funcRef2 = module.getFuncRef('load')
330 funcRef2.gas = 322000
331
332 await hypervisor.createStateRoot()
333
334 const message2 = new Message({
335 funcRef: funcRef2,
336 funcArguments: [new FunctionRef({actorID: egress.id, params: ['data']})]
337 })
338
339 hypervisor.send(message2)
340})
341
342tape('link', async t => {
343 tester = t
344 const tree = new RadixTree({db})
345 let wasm = fs.readFileSync(WASM_PATH + '/link.wasm')
346
347 const egress = new EgressDriver()
348
349 egress.on('message', msg => {
350 t.equals(msg.funcArguments[0].toString(), 'hello world')
351 t.end()
352 })
353
354 const hypervisor = new Hypervisor(tree, [TestWasmContainer], [egress])
355
356 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
357 const funcRef = module.getFuncRef('main')
358 funcRef.gas = 322000
359
360 const message = new Message({
361 funcRef
362 })
363
364 hypervisor.send(message)
365
366 const funcRef2 = module.getFuncRef('load')
367 funcRef2.gas = 322000
368
369 await hypervisor.createStateRoot()
370
371 const message2 = new Message({
372 funcRef: funcRef2,
373 funcArguments: [new FunctionRef({actorID: egress.id, params: ['data']})]
374 })
375
376 hypervisor.send(message2)
377})
378
379tape('invalid binary', async t => {
380 t.plan(1)
381 tester = t
382 const tree = new RadixTree({db})
383 const wasm = Buffer.from([0])
384
385 const hypervisor = new Hypervisor(tree)
386 hypervisor.registerContainer(TestWasmContainer)
387
388 try {
389 await hypervisor.createActor(TestWasmContainer.typeId, wasm)
390 } catch (e) {
391 t.pass()
392 }
393})
394
395tape('out of gas', async t => {
396 t.plan(1)
397 tester = t
398 const tree = new RadixTree({db})
399 let wasm = fs.readFileSync(WASM_PATH + '/i64.wasm')
400
401 const hypervisor = new Hypervisor(tree)
402 hypervisor.registerContainer(TestWasmContainer)
403
404 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
405 const funcRef = module.getFuncRef('main')
406
407 const message = new Message({
408 funcRef
409 }).on('execution:error', e => {
410 t.pass()
411 })
412 hypervisor.send(message)
413})
414

Built with git-ssb-web