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