git ssb

0+

wanderer🌟 / js-primea-wasm-container



Tree: 17f8d0b0c260370c4b7e8406de2c84ce1586d3fa

Files: 17f8d0b0c260370c4b7e8406de2c84ce1586d3fa / tests / index.js

7833 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')
10const db = level('./testdb')
11
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 return Object.assign(orginal, {
24 test: {
25 check: (a, b) => {
26 tester.equals(a, b)
27 },
28 print: (dataRef) => {
29 let buf = this.refs.get(dataRef, 'buf')
30 console.log(buf.toString())
31 }
32 }
33 })
34 }
35}
36
37tape('basic', async t => {
38 t.plan(1)
39 tester = t
40
41 const tree = new RadixTree({
42 db
43 })
44
45 const wasm = fs.readFileSync(WASM_PATH + '/reciever.wasm')
46
47 const hypervisor = new Hypervisor(tree)
48 hypervisor.registerContainer(TestWasmContainer)
49
50 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
51 const funcRef = module.getFuncRef('receive')
52 funcRef.gas = 300
53
54 const message = new Message({
55 funcRef,
56 funcArguments: [5]
57 }).on('execution:error', e => {
58 console.log(e)
59 })
60 hypervisor.send(message)
61 // const stateRoot = await hypervisor.createStateRoot()
62 // t.deepEquals(stateRoot, expectedState, 'expected root!')
63})
64
65tape('empty', async t => {
66 t.plan(1)
67 tester = t
68 const expectedState = Buffer.from('bb15867b26293aa36c774b8c82541dd64212ba9a', 'hex')
69
70 const tree = new RadixTree({
71 db
72 })
73
74 const wasm = fs.readFileSync(WASM_PATH + '/empty.wasm')
75
76 const hypervisor = new Hypervisor(tree)
77 hypervisor.registerContainer(TestWasmContainer)
78
79 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
80 const funcRef = module.getFuncRef('receive')
81 funcRef.gas = 300
82
83 const message = new Message({
84 funcRef,
85 funcArguments: [5]
86 })
87 hypervisor.send(message)
88 const stateRoot = await hypervisor.createStateRoot()
89 t.deepEquals(stateRoot, expectedState, 'expected root!')
90})
91
92tape('two communicating actors', async t => {
93 t.plan(1)
94 tester = t
95 const expectedState = {
96 '/': Buffer.from('8c230b5f0f680199b24ecd1800c2970dfca7cfdc', 'hex')
97 }
98
99 const tree = new RadixTree({db})
100
101 const recieverWasm = fs.readFileSync(WASM_PATH + '/reciever.wasm')
102 const callerWasm = fs.readFileSync(WASM_PATH + '/caller.wasm')
103
104 const hypervisor = new Hypervisor(tree)
105 hypervisor.registerContainer(TestWasmContainer)
106
107 const {module: receiverMod} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm)
108 const {module: callerMod} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm)
109 const callFuncRef = callerMod.getFuncRef('call')
110 const recvFuncRef = receiverMod.getFuncRef('receive')
111 callFuncRef.gas = 100000
112 recvFuncRef.gas = 1000
113 const message = new Message({
114 funcRef: callFuncRef,
115 funcArguments: [recvFuncRef]
116 })
117
118 hypervisor.send(message)
119 const stateRoot = await hypervisor.createStateRoot()
120 // t.deepEquals(stateRoot, expectedState, 'expected root!')
121})
122
123tape('two communicating actors with callback', async t => {
124 t.plan(1)
125 tester = t
126 const expectedState = {
127 '/': Buffer.from('9bf27cf07b75a90e0af530e2df73e3102482b24a', 'hex')
128 }
129
130 const tree = new RadixTree({
131 db
132 })
133
134 const recieverWasm = fs.readFileSync(WASM_PATH + '/funcRef_reciever.wasm')
135 const callerWasm = fs.readFileSync(WASM_PATH + '/funcRef_caller.wasm')
136
137 const hypervisor = new Hypervisor(tree)
138 hypervisor.registerContainer(TestWasmContainer)
139
140 const {module: callerMod} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm)
141 const {module: receiverMod} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm)
142
143 const callFuncRef = callerMod.getFuncRef('call')
144 const recvFuncRef = receiverMod.getFuncRef('receive')
145 callFuncRef.gas = 100000
146 recvFuncRef.gas = 100000
147
148 const message = new Message({
149 funcRef: callFuncRef,
150 funcArguments: [recvFuncRef]
151 }).on('execution:error', e => console.log(e))
152
153 hypervisor.send(message)
154 const stateRoot = await hypervisor.createStateRoot()
155 // t.deepEquals(stateRoot, expectedState, 'expected root!')
156})
157
158tape('two communicating actors with private callback', async t => {
159 t.plan(1)
160 tester = t
161 const expectedState = {
162 '/': Buffer.from('9bf27cf07b75a90e0af530e2df73e3102482b24a', 'hex')
163 }
164
165 const tree = new RadixTree({
166 db
167 })
168
169 const recieverWasm = fs.readFileSync(WASM_PATH + '/funcRef_reciever.wasm')
170 const callerWasm = fs.readFileSync(WASM_PATH + '/private_caller.wasm')
171
172 const hypervisor = new Hypervisor(tree)
173 hypervisor.registerContainer(TestWasmContainer)
174
175 const {module: callerMod} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm)
176 const {module: receiverMod} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm)
177
178 const callFuncRef = callerMod.getFuncRef('call')
179 const recvFuncRef = receiverMod.getFuncRef('receive')
180 callFuncRef.gas = 100000
181 recvFuncRef.gas = 100000
182
183 const message = new Message({
184 funcRef: callFuncRef,
185 funcArguments: [recvFuncRef]
186 })
187
188 hypervisor.send(message)
189 const stateRoot = await hypervisor.createStateRoot()
190 // t.deepEquals(stateRoot, expectedState, 'expected root!')
191})
192
193tape('externalize/internalize memory', async t => {
194 t.plan(1)
195 tester = t
196 const tree = new RadixTree({
197 db
198 })
199
200 const wasm = fs.readFileSync(WASM_PATH + '/memory.wasm')
201
202 const hypervisor = new Hypervisor(tree)
203 hypervisor.registerContainer(TestWasmContainer)
204
205 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
206 const funcRef = module.getFuncRef('test')
207 funcRef.gas = 10000
208
209 const message = new Message({funcRef}).on('done', actor => {
210 const a = actor.container.get8Memory(0, 5)
211 const b = actor.container.get8Memory(5, 5)
212 t.deepEquals(a, b, 'should copy memory correctly')
213 })
214 hypervisor.send(message)
215})
216
217tape('externalize/internalize table', 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 + '/table.wasm')
225 const hypervisor = new Hypervisor(tree)
226 hypervisor.registerContainer(TestWasmContainer)
227
228 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
229
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, 8)
235 const b = actor.container.get8Memory(8, 8)
236 t.deepEquals(a, b, 'should copy memory correctly')
237 })
238 hypervisor.send(message)
239})
240
241tape('load / store globals', 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 + '/globals.wasm')
249
250 const hypervisor = new Hypervisor(tree)
251 hypervisor.registerContainer(TestWasmContainer)
252
253 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
254
255 await new Promise((resolve, reject) => {
256 const funcRef = module.getFuncRef('store')
257 funcRef.gas = 400
258 const message = new Message({
259 funcRef
260 }).on('done', actor => {
261 resolve()
262 })
263 hypervisor.send(message)
264 })
265
266 await new Promise((resolve, reject) => {
267 const funcRef = module.getFuncRef('load')
268 funcRef.gas = 400
269 const message = new Message({
270 funcRef
271 }).on('done', actor => {
272 const b = actor.container.get8Memory(5, 4)
273 const result = Buffer.from(b).toString()
274 t.deepEquals(result, 'test', 'should copy memory correctly')
275 resolve()
276 })
277 hypervisor.send(message)
278 })
279})
280

Built with git-ssb-web