git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 69542e9b41cc18d3434269b364a714712a5a5631

Files: 69542e9b41cc18d3434269b364a714712a5a5631 / tests / wasmContainer.js

3915 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 }
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
38tape.only('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
65tape('two communicating actors', async t => {
66 t.plan(2)
67 tester = t
68 const expectedState = {
69 '/': Buffer.from('123bcbf52421f0ebf0c9a28d6546a3b374f5d56d', '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 {exports: receiverExports} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm)
81 const {exports: callerExports} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm)
82
83 const message = new Message({
84 funcRef: callerExports.call,
85 funcArguments: [receiverExports.receive]
86 })
87
88 hypervisor.send(message)
89 const stateRoot = await hypervisor.createStateRoot()
90 t.deepEquals(stateRoot, expectedState, 'expected root!')
91})
92
93tape('two communicating actors with callback', async t => {
94 // t.plan(2)
95 tester = t
96 const expectedState = {
97 '/': Buffer.from('f3cc5ba63d6b1737bea2c33bd1942e5488787b82', 'hex')
98 }
99
100 const tree = new RadixTree({
101 db
102 })
103
104 const recieverWasm = fs.readFileSync('./wasm/funcRef_reciever.wasm')
105 const callerWasm = fs.readFileSync('./wasm/funcRef_caller.wasm')
106
107 const hypervisor = new Hypervisor(tree)
108 hypervisor.registerContainer(TestWasmContainer)
109
110 const {exports: receiverExports} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm)
111 const {exports: callerExports} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm)
112
113 const message = new Message({
114 funcRef: callerExports.call,
115 funcArguments: [receiverExports.receive]
116 })
117
118 hypervisor.send(message)
119 const stateRoot = await hypervisor.createStateRoot()
120 // t.deepEquals(stateRoot, expectedState, 'expected root!')
121 t.end()
122})
123
124// Increment a counter.
125tape.skip('increment', async t => {
126 const tree = new RadixTree({
127 db
128 })
129
130 const wasm = fs.readFileSync('./wasm/counter.wasm')
131
132 const hypervisor = new Hypervisor(tree)
133 hypervisor.registerContainer(TestWasmContainer)
134
135 const {exports} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
136
137 const message = new Message({
138 funcRef: exports.increment,
139 funcArguments: []
140 })
141 hypervisor.send(message)
142
143 const stateRoot = await hypervisor.createStateRoot()
144 t.end()
145
146 console.log(stateRoot)
147
148})
149

Built with git-ssb-web