git ssb

0+

wanderer🌟 / js-primea-wasm-container



Tree: 609307921c4c0a7292513248cc13f16b199ec5f0

Files: 609307921c4c0a7292513248cc13f16b199ec5f0 / tests / index.js

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

Built with git-ssb-web