git ssb

0+

wanderer🌟 / js-primea-wasm-container



Tree: d48b7065a388045f96fc7be1588e12150b8de8bb

Files: d48b7065a388045f96fc7be1588e12150b8de8bb / tests / index.js

9535 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('reintinalizing', async t => {
60 t.plan(1)
61 tester = t
62 const tree = new RadixTree({db})
63 let wasm = fs.readFileSync(WASM_PATH + '/reinternalize.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('basic', async t => {
81 t.plan(1)
82 tester = t
83
84 const tree = new RadixTree({
85 db
86 })
87
88 const wasm = fs.readFileSync(WASM_PATH + '/reciever.wasm')
89
90 const hypervisor = new Hypervisor(tree)
91 hypervisor.registerContainer(TestWasmContainer)
92
93 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
94 const funcRef = module.getFuncRef('receive')
95 funcRef.gas = 300
96
97 const message = new Message({
98 funcRef,
99 funcArguments: [5]
100 }).on('execution:error', e => {
101 console.log(e)
102 })
103 hypervisor.send(message)
104 // const stateRoot = await hypervisor.createStateRoot()
105 // t.deepEquals(stateRoot, expectedState, 'expected root!')
106})
107
108
109tape('empty', async t => {
110 t.plan(1)
111 tester = t
112 const expectedState = Buffer.from('bb15867b26293aa36c774b8c82541dd64212ba9a', 'hex')
113
114 const tree = new RadixTree({
115 db
116 })
117
118 const wasm = fs.readFileSync(WASM_PATH + '/empty.wasm')
119
120 const hypervisor = new Hypervisor(tree)
121 hypervisor.registerContainer(TestWasmContainer)
122
123 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
124 const funcRef = module.getFuncRef('receive')
125 funcRef.gas = 300
126
127 const message = new Message({
128 funcRef,
129 funcArguments: [5]
130 })
131 hypervisor.send(message)
132 const stateRoot = await hypervisor.createStateRoot()
133 t.deepEquals(stateRoot, expectedState, 'expected root!')
134})
135
136tape('two communicating actors', async t => {
137 t.plan(1)
138 tester = t
139 const expectedState = {
140 '/': Buffer.from('8c230b5f0f680199b24ecd1800c2970dfca7cfdc', 'hex')
141 }
142
143 const tree = new RadixTree({db})
144
145 const recieverWasm = fs.readFileSync(WASM_PATH + '/reciever.wasm')
146 const callerWasm = fs.readFileSync(WASM_PATH + '/caller.wasm')
147
148 const hypervisor = new Hypervisor(tree)
149 hypervisor.registerContainer(TestWasmContainer)
150
151 const {module: receiverMod} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm)
152 const {module: callerMod} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm)
153 const callFuncRef = callerMod.getFuncRef('call')
154 const recvFuncRef = receiverMod.getFuncRef('receive')
155 callFuncRef.gas = 100000
156 recvFuncRef.gas = 1000
157 const message = new Message({
158 funcRef: callFuncRef,
159 funcArguments: [recvFuncRef]
160 })
161
162 hypervisor.send(message)
163 const stateRoot = await hypervisor.createStateRoot()
164 // t.deepEquals(stateRoot, expectedState, 'expected root!')
165})
166
167tape('two communicating actors with callback', async t => {
168 t.plan(1)
169 tester = t
170 const expectedState = {
171 '/': Buffer.from('9bf27cf07b75a90e0af530e2df73e3102482b24a', 'hex')
172 }
173
174 const tree = new RadixTree({
175 db
176 })
177
178 const recieverWasm = fs.readFileSync(WASM_PATH + '/funcRef_reciever.wasm')
179 const callerWasm = fs.readFileSync(WASM_PATH + '/funcRef_caller.wasm')
180
181 const hypervisor = new Hypervisor(tree)
182 hypervisor.registerContainer(TestWasmContainer)
183
184 const {module: callerMod} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm)
185 const {module: receiverMod} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm)
186
187 const callFuncRef = callerMod.getFuncRef('call')
188 const recvFuncRef = receiverMod.getFuncRef('receive')
189 callFuncRef.gas = 100000
190 recvFuncRef.gas = 100000
191
192 const message = new Message({
193 funcRef: callFuncRef,
194 funcArguments: [recvFuncRef]
195 }).on('execution:error', e => console.log(e))
196
197 hypervisor.send(message)
198 const stateRoot = await hypervisor.createStateRoot()
199 // t.deepEquals(stateRoot, expectedState, 'expected root!')
200})
201
202tape('two communicating actors with private callback', async t => {
203 t.plan(1)
204 tester = t
205 const expectedState = {
206 '/': Buffer.from('9bf27cf07b75a90e0af530e2df73e3102482b24a', 'hex')
207 }
208
209 const tree = new RadixTree({
210 db
211 })
212
213 const recieverWasm = fs.readFileSync(WASM_PATH + '/funcRef_reciever.wasm')
214 const callerWasm = fs.readFileSync(WASM_PATH + '/private_caller.wasm')
215
216 const hypervisor = new Hypervisor(tree)
217 hypervisor.registerContainer(TestWasmContainer)
218
219 const {module: callerMod} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm)
220 const {module: receiverMod} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm)
221
222 const callFuncRef = callerMod.getFuncRef('call')
223 const recvFuncRef = receiverMod.getFuncRef('receive')
224 callFuncRef.gas = 100000
225 recvFuncRef.gas = 100000
226
227 const message = new Message({
228 funcRef: callFuncRef,
229 funcArguments: [recvFuncRef]
230 })
231
232 hypervisor.send(message)
233 const stateRoot = await hypervisor.createStateRoot()
234 // t.deepEquals(stateRoot, expectedState, 'expected root!')
235})
236
237tape('externalize/internalize memory', async t => {
238 t.plan(1)
239 tester = t
240 const tree = new RadixTree({
241 db
242 })
243
244 const wasm = fs.readFileSync(WASM_PATH + '/memory.wasm')
245
246 const hypervisor = new Hypervisor(tree)
247 hypervisor.registerContainer(TestWasmContainer)
248
249 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
250 const funcRef = module.getFuncRef('test')
251 funcRef.gas = 10000
252
253 const message = new Message({funcRef}).on('done', actor => {
254 const a = actor.container.get8Memory(0, 5)
255 const b = actor.container.get8Memory(5, 5)
256 t.deepEquals(a, b, 'should copy memory correctly')
257 })
258 hypervisor.send(message)
259})
260
261tape('externalize/internalize table', async t => {
262 t.plan(1)
263 tester = t
264 const tree = new RadixTree({
265 db
266 })
267
268 const wasm = fs.readFileSync(WASM_PATH + '/table.wasm')
269 const hypervisor = new Hypervisor(tree)
270 hypervisor.registerContainer(TestWasmContainer)
271
272 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
273
274 const funcRef = module.getFuncRef('test')
275 funcRef.gas = 10000
276
277 const message = new Message({funcRef}).on('done', actor => {
278 const a = actor.container.get8Memory(0, 8)
279 const b = actor.container.get8Memory(8, 8)
280 t.deepEquals(a, b, 'should copy memory correctly')
281 })
282 hypervisor.send(message)
283})
284
285tape('load / store globals', async t => {
286 t.plan(1)
287 tester = t
288 const tree = new RadixTree({
289 db
290 })
291
292 const wasm = fs.readFileSync(WASM_PATH + '/globals.wasm')
293
294 const hypervisor = new Hypervisor(tree)
295 hypervisor.registerContainer(TestWasmContainer)
296
297 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
298
299 await new Promise((resolve, reject) => {
300 const funcRef = module.getFuncRef('store')
301 funcRef.gas = 400
302 const message = new Message({
303 funcRef
304 }).on('done', actor => {
305 resolve()
306 })
307 hypervisor.send(message)
308 })
309
310 await new Promise((resolve, reject) => {
311 const funcRef = module.getFuncRef('load')
312 funcRef.gas = 400
313 const message = new Message({
314 funcRef
315 }).on('done', actor => {
316 const b = actor.container.get8Memory(5, 4)
317 const result = Buffer.from(b).toString()
318 t.deepEquals(result, 'test', 'should copy memory correctly')
319 resolve()
320 })
321 hypervisor.send(message)
322 })
323})
324
325tape('creation', async t => {
326 t.plan(1)
327 tester = t
328 const tree = new RadixTree({db})
329 let wasm = fs.readFileSync(WASM_PATH + '/creation.wasm')
330 let receiver = fs.readFileSync(WASM_PATH + '/reciever.wasm')
331
332 const hypervisor = new Hypervisor(tree)
333 hypervisor.registerContainer(TestWasmContainer)
334
335 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
336 const funcRef = module.getFuncRef('main')
337 funcRef.gas = 322000
338
339 const message = new Message({
340 funcRef,
341 funcArguments: [receiver]
342 }).on('execution:error', e => {
343 console.log(e)
344 })
345 hypervisor.send(message)
346})
347

Built with git-ssb-web