git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 2417016a904e9be97c5043e4fc9b1d5357aa1635

Files: 2417016a904e9be97c5043e4fc9b1d5357aa1635 / tests / wasmContainer.js

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

Built with git-ssb-web