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