git ssb

0+

wanderer🌟 / js-primea-wasm-container



Tree: f019f1d8c91c4a7427df520e819cf78f0fdd7a77

Files: f019f1d8c91c4a7427df520e819cf78f0fdd7a77 / tests / index.js

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

Built with git-ssb-web