git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: c1baf026f5fba05cb5619603b350445d6e40a0d5

Files: c1baf026f5fba05cb5619603b350445d6e40a0d5 / tests / wasmContainer.js

3924 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('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 {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
92tape('two communicating actors with callback', async t => {
93 // t.plan(2)
94 tester = t
95 const expectedState = {
96 '/': Buffer.from('51ded6c294314defc886b70f7f593434c8d53c95', '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// Increment a counter.
124tape.skip('increment', async t => {
125 const tree = new RadixTree({
126 db
127 })
128
129 const wasm = fs.readFileSync('./wasm/counter.wasm')
130
131 const hypervisor = new Hypervisor(tree)
132 hypervisor.registerContainer(TestWasmContainer)
133
134 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
135
136 const message = new Message({
137 funcRef: module.increment,
138 funcArguments: []
139 })
140 hypervisor.send(message)
141
142 const stateRoot = await hypervisor.createStateRoot()
143 t.end()
144
145 console.log(stateRoot)
146
147})
148

Built with git-ssb-web