Files: 9babf4f2b9100d276b3b4aa9195653cf32fc388d / tests / wasmContainer.js
4883 bytesRaw
1 | const tape = require('tape') |
2 | const fs = require('fs') |
3 | const Message = require('../message.js') |
4 | const Hypervisor = require('../') |
5 | const WasmContainer = require('../wasmContainer.js') |
6 | |
7 | const level = require('level-browserify') |
8 | const RadixTree = require('dfinity-radix-tree') |
9 | const db = level('./testdb') |
10 | |
11 | let tester |
12 | |
13 | class 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 | } |
26 | }) |
27 | } |
28 | setState (key, ref) { |
29 | const obj = this.refs.get(ref) |
30 | this._storage.set(key, obj) |
31 | } |
32 | getState (key) { |
33 | const obj = this._storage.get(key) |
34 | return this.refs.add(obj) |
35 | } |
36 | } |
37 | |
38 | tape('basic', async t => { |
39 | t.plan(2) |
40 | tester = t |
41 | const expectedState = { |
42 | '/': Buffer.from('4494963fb0e02312510e675fbca8b60b6e03bd00', 'hex') |
43 | } |
44 | |
45 | const tree = new RadixTree({ |
46 | db |
47 | }) |
48 | |
49 | const wasm = fs.readFileSync('./wasm/reciever.wasm') |
50 | |
51 | const hypervisor = new Hypervisor(tree) |
52 | hypervisor.registerContainer(TestWasmContainer) |
53 | |
54 | const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm) |
55 | |
56 | const message = new Message({ |
57 | funcRef: module.getFuncRef('receive'), |
58 | funcArguments: [5] |
59 | }) |
60 | hypervisor.send(message) |
61 | const stateRoot = await hypervisor.createStateRoot() |
62 | t.deepEquals(stateRoot, expectedState, 'expected root!') |
63 | }) |
64 | |
65 | tape('two communicating actors', async t => { |
66 | t.plan(2) |
67 | tester = t |
68 | const expectedState = { |
69 | '/': Buffer.from('8c230b5f0f680199b24ecd1800c2970dfca7cfdc', 'hex') |
70 | } |
71 | |
72 | const tree = new RadixTree({db}) |
73 | |
74 | const recieverWasm = fs.readFileSync('./wasm/reciever.wasm') |
75 | const callerWasm = fs.readFileSync('./wasm/caller.wasm') |
76 | |
77 | const hypervisor = new Hypervisor(tree) |
78 | hypervisor.registerContainer(TestWasmContainer) |
79 | |
80 | const {module: receiverMod} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm) |
81 | const {module: callerMod} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm) |
82 | const message = new Message({ |
83 | funcRef: callerMod.getFuncRef('call'), |
84 | funcArguments: [receiverMod.getFuncRef('receive')] |
85 | }) |
86 | |
87 | hypervisor.send(message) |
88 | const stateRoot = await hypervisor.createStateRoot() |
89 | t.deepEquals(stateRoot, expectedState, 'expected root!') |
90 | }) |
91 | |
92 | tape('two communicating actors with callback', async t => { |
93 | t.plan(2) |
94 | tester = t |
95 | const expectedState = { |
96 | '/': Buffer.from('9bf27cf07b75a90e0af530e2df73e3102482b24a', 'hex') |
97 | } |
98 | |
99 | const tree = new RadixTree({ |
100 | db |
101 | }) |
102 | |
103 | const recieverWasm = fs.readFileSync('./wasm/funcRef_reciever.wasm') |
104 | const callerWasm = fs.readFileSync('./wasm/funcRef_caller.wasm') |
105 | |
106 | const hypervisor = new Hypervisor(tree) |
107 | hypervisor.registerContainer(TestWasmContainer) |
108 | |
109 | const {module: receiverMod} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm) |
110 | const {module: callerMod} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm) |
111 | |
112 | const message = new Message({ |
113 | funcRef: callerMod.getFuncRef('call'), |
114 | funcArguments: [receiverMod.getFuncRef('receive')] |
115 | }) |
116 | |
117 | hypervisor.send(message) |
118 | const stateRoot = await hypervisor.createStateRoot() |
119 | t.deepEquals(stateRoot, expectedState, 'expected root!') |
120 | // t.end() |
121 | }) |
122 | |
123 | tape.skip('two communicating actors with callback', async t => { |
124 | t.plan(2) |
125 | tester = t |
126 | const expectedState = { |
127 | '/': Buffer.from('9bf27cf07b75a90e0af530e2df73e3102482b24a', 'hex') |
128 | } |
129 | |
130 | const tree = new RadixTree({ |
131 | db |
132 | }) |
133 | |
134 | const recieverWasm = fs.readFileSync('./wasm/funcRef_reciever.wasm') |
135 | const callerWasm = fs.readFileSync('./wasm/funcRef_caller.wasm') |
136 | |
137 | const hypervisor = new Hypervisor(tree) |
138 | hypervisor.registerContainer(TestWasmContainer) |
139 | |
140 | const {module: receiverMod} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm) |
141 | const {module: callerMod} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm) |
142 | |
143 | const message = new Message({ |
144 | funcRef: callerMod.getFuncRef('call'), |
145 | funcArguments: [receiverMod.getFuncRef('receive')] |
146 | }) |
147 | |
148 | hypervisor.send(message) |
149 | const stateRoot = await hypervisor.createStateRoot() |
150 | t.deepEquals(stateRoot, expectedState, 'expected root!') |
151 | t.end() |
152 | }) |
153 | |
154 | // Increment a counter. |
155 | tape.skip('increment', async t => { |
156 | const tree = new RadixTree({ |
157 | db |
158 | }) |
159 | |
160 | const wasm = fs.readFileSync('./wasm/counter.wasm') |
161 | |
162 | const hypervisor = new Hypervisor(tree) |
163 | hypervisor.registerContainer(TestWasmContainer) |
164 | |
165 | const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm) |
166 | |
167 | const message = new Message({ |
168 | funcRef: module.increment, |
169 | funcArguments: [] |
170 | }) |
171 | hypervisor.send(message) |
172 | |
173 | const stateRoot = await hypervisor.createStateRoot() |
174 | t.end() |
175 | |
176 | console.log(stateRoot) |
177 | |
178 | }) |
179 |
Built with git-ssb-web