git ssb

0+

wanderer🌟 / js-primea-wasm-container



Tree: 92fb09c87c224982aa17e3fa025810620e571b26

Files: 92fb09c87c224982aa17e3fa025810620e571b26 / tests / index.js

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

Built with git-ssb-web