git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 49051b3e16dc9927b3bbcfe2f7a8af88ccc8a232

Files: 49051b3e16dc9927b3bbcfe2f7a8af88ccc8a232 / tests / wasmContainer.js

7599 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('two communicating actors', async t => {
67 t.plan(1)
68 tester = t
69 const expectedState = {
70 '/': Buffer.from('8c230b5f0f680199b24ecd1800c2970dfca7cfdc', 'hex')
71 }
72
73 const tree = new RadixTree({db})
74
75 const recieverWasm = fs.readFileSync(WASM_PATH + '/reciever.wasm')
76 const callerWasm = fs.readFileSync(WASM_PATH + '/caller.wasm')
77
78 const hypervisor = new Hypervisor(tree)
79 hypervisor.registerContainer(TestWasmContainer)
80
81 const {module: receiverMod} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm)
82 const {module: callerMod} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm)
83 const callFuncRef = callerMod.getFuncRef('call')
84 const recvFuncRef = receiverMod.getFuncRef('receive')
85 callFuncRef.gas = 100000
86 recvFuncRef.gas = 1000
87 const message = new Message({
88 funcRef: callFuncRef,
89 funcArguments: [recvFuncRef]
90 })
91
92 hypervisor.send(message)
93 const stateRoot = await hypervisor.createStateRoot()
94 // t.deepEquals(stateRoot, expectedState, 'expected root!')
95})
96
97tape('two communicating actors with callback', async t => {
98 t.plan(1)
99 tester = t
100 const expectedState = {
101 '/': Buffer.from('9bf27cf07b75a90e0af530e2df73e3102482b24a', 'hex')
102 }
103
104 const tree = new RadixTree({
105 db
106 })
107
108 const recieverWasm = fs.readFileSync(WASM_PATH + '/funcRef_reciever.wasm')
109 const callerWasm = fs.readFileSync(WASM_PATH + '/funcRef_caller.wasm')
110
111 const hypervisor = new Hypervisor(tree)
112 hypervisor.registerContainer(TestWasmContainer)
113
114 const {module: callerMod} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm)
115 const {module: receiverMod} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm)
116
117 const callFuncRef = callerMod.getFuncRef('call')
118 const recvFuncRef = receiverMod.getFuncRef('receive')
119 callFuncRef.gas = 100000
120 recvFuncRef.gas = 100000
121
122 const message = new Message({
123 funcRef: callFuncRef,
124 funcArguments: [recvFuncRef]
125 })
126
127 hypervisor.send(message)
128 const stateRoot = await hypervisor.createStateRoot()
129 // t.deepEquals(stateRoot, expectedState, 'expected root!')
130})
131
132tape('two communicating actors with private callback', async t => {
133 t.plan(1)
134 tester = t
135 const expectedState = {
136 '/': Buffer.from('9bf27cf07b75a90e0af530e2df73e3102482b24a', 'hex')
137 }
138
139 const tree = new RadixTree({
140 db
141 })
142
143 const recieverWasm = fs.readFileSync(WASM_PATH + '/funcRef_reciever.wasm')
144 const callerWasm = fs.readFileSync(WASM_PATH + '/private_caller.wasm')
145
146 const hypervisor = new Hypervisor(tree)
147 hypervisor.registerContainer(TestWasmContainer)
148
149 const {module: callerMod} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm)
150 const {module: receiverMod} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm)
151
152 const callFuncRef = callerMod.getFuncRef('call')
153 const recvFuncRef = receiverMod.getFuncRef('receive')
154 callFuncRef.gas = 100000
155 recvFuncRef.gas = 100000
156
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('externalize/internalize memory', async t => {
168 t.plan(1)
169 tester = t
170 const tree = new RadixTree({
171 db
172 })
173
174 const wasm = fs.readFileSync(WASM_PATH + '/memory.wasm')
175
176 const hypervisor = new Hypervisor(tree)
177 hypervisor.registerContainer(TestWasmContainer)
178
179 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
180 const funcRef = module.getFuncRef('test')
181 funcRef.gas = 10000
182
183 const message = new Message({funcRef}).on('done', actor => {
184 const a = actor.container.getMemory(0, 5)
185 const b = actor.container.getMemory(5, 5)
186 t.deepEquals(a, b, 'should copy memory correctly')
187 })
188 hypervisor.send(message)
189})
190
191tape('externalize/internalize table', async t => {
192 t.plan(1)
193 tester = t
194 const tree = new RadixTree({
195 db
196 })
197
198 const wasm = fs.readFileSync(WASM_PATH + '/table.wasm')
199 const hypervisor = new Hypervisor(tree)
200 hypervisor.registerContainer(TestWasmContainer)
201
202 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
203
204 const funcRef = module.getFuncRef('test')
205 funcRef.gas = 10000
206
207 const message = new Message({funcRef}).on('done', actor => {
208 const a = actor.container.getMemory(0, 8)
209 const b = actor.container.getMemory(8, 8)
210 t.deepEquals(a, b, 'should copy memory correctly')
211 })
212 hypervisor.send(message)
213})
214
215tape('load / store globals', 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 + '/globals.wasm')
223
224 const hypervisor = new Hypervisor(tree)
225 hypervisor.registerContainer(TestWasmContainer)
226
227 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
228
229 await new Promise((resolve, reject) => {
230 const funcRef = module.getFuncRef('store')
231 funcRef.gas = 400
232 const message = new Message({
233 funcRef
234 }).on('done', actor => {
235 resolve()
236 })
237 hypervisor.send(message)
238 })
239
240 await new Promise((resolve, reject) => {
241 const funcRef = module.getFuncRef('load')
242 funcRef.gas = 400
243 const message = new Message({
244 funcRef
245 }).on('done', actor => {
246 resolve()
247 const b = actor.container.getMemory(5, 4)
248 const result = Buffer.from(b).toString()
249 t.deepEquals(result, 'test', 'should copy memory correctly')
250 })
251 hypervisor.send(message)
252 })
253})
254
255tape.skip('ben', async t => {
256 // t.plan(1)
257 tester = t
258 const tree = new RadixTree({
259 db
260 })
261
262 const wasm = fs.readFileSync(WASM_PATH + '.wasm')
263 const hypervisor = new Hypervisor(tree)
264 hypervisor.registerContainer(TestWasmContainer)
265
266 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
267
268 const message = new Message({
269 funcRef: module.getFuncRef('main')
270 }).on('done', () => {
271 t.end()
272 })
273 hypervisor.send(message)
274})
275

Built with git-ssb-web