git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 72a22e2c56fb1973e226eac66319ef4ea3909a6f

Files: 72a22e2c56fb1973e226eac66319ef4ea3909a6f / tests / wasmContainer.js

2954 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 getInteface (funcRef) {
19 const orginal = super.getInteface(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 {exports} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
55
56 const message = new Message({
57 funcRef: exports.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('f3cc5ba63d6b1737bea2c33bd1942e5488787b82', 'hex')
70 }
71
72 const tree = new RadixTree({
73 db
74 })
75
76 const recieverWasm = fs.readFileSync('./wasm/reciever.wasm')
77 const callerWasm = fs.readFileSync('./wasm/caller.wasm')
78
79 const hypervisor = new Hypervisor(tree)
80 hypervisor.registerContainer(TestWasmContainer)
81
82 const {exports: receiverExports} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm)
83 const {exports: callerExports} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm)
84
85 const message = new Message({
86 funcRef: callerExports.call,
87 funcArguments: [receiverExports.receive]
88 })
89
90 hypervisor.send(message)
91 const stateRoot = await hypervisor.createStateRoot()
92 t.deepEquals(stateRoot, expectedState, 'expected root!')
93})
94
95// Increment a counter.
96tape.skip('increment', async t => {
97 const tree = new RadixTree({
98 db
99 })
100
101 const wasm = fs.readFileSync('./wasm/counter.wasm')
102
103 const hypervisor = new Hypervisor(tree)
104 hypervisor.registerContainer(TestWasmContainer)
105
106 const {exports} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
107
108 const message = new Message({
109 funcRef: exports.increment,
110 funcArguments: []
111 })
112 hypervisor.send(message)
113
114 const stateRoot = await hypervisor.createStateRoot()
115 t.end()
116
117 console.log(stateRoot)
118
119})
120

Built with git-ssb-web