git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 5013b5ed5b183c9bd021b9b9b20f7df558b0a664

Files: 5013b5ed5b183c9bd021b9b9b20f7df558b0a664 / tests / wasmContainer.js

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

Built with git-ssb-web