git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 5013b5ed5b183c9bd021b9b9b20f7df558b0a664

added gas accnting

Signed-off-by: wanderer <mjbecze@gmail.com>
wanderer committed on 3/2/2018, 7:05:41 AM
Parent: 1911acb3f97dde2484d79904419eb7e656a3a336

Files changed

index.jschanged
systemObjects.jschanged
tests/wasm/funcRef_reciever.wasmchanged
tests/wasmContainer.jschanged
tests/wast/funcRef_caller.wastchanged
tests/wast/funcRef_reciever.wastchanged
wasmContainer.jschanged
index.jsView
@@ -76,9 +76,9 @@
7676 await this.tree.graph.set(node.root, '1', code)
7777 }
7878 return {
7979 id: idHash,
80- module: module
80+ module
8181 }
8282 }
8383
8484 // get a hash from a POJO
systemObjects.jsView
@@ -21,8 +21,9 @@
2121 this.private = privateFunc
2222 this.identifier = identifier
2323 this.destId = id
2424 this.params = params
25+ this.gas = 0
2526 }
2627
2728 encodeCBOR (gen) {
2829 return gen.write(new cbor.Tagged(TAGS.func, [
tests/wasm/funcRef_reciever.wasmView
@@ -1,3 +1,3 @@
11 asm types`` typeMap
2-``func internalizeptablereceive
3- AAA
2+``*func internalizefuncset_gas_budgetptablereceive
3+ A�� AAA
tests/wasmContainer.jsView
@@ -47,11 +47,13 @@
4747 const hypervisor = new Hypervisor(tree)
4848 hypervisor.registerContainer(TestWasmContainer)
4949
5050 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
51+ const funcRef = module.getFuncRef('receive')
52+ funcRef.gas = 300
5153
5254 const message = new Message({
53- funcRef: module.getFuncRef('receive'),
55+ funcRef,
5456 funcArguments: [5]
5557 })
5658 hypervisor.send(message)
5759 const stateRoot = await hypervisor.createStateRoot()
@@ -74,11 +76,15 @@
7476 hypervisor.registerContainer(TestWasmContainer)
7577
7678 const {module: receiverMod} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm)
7779 const {module: callerMod} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm)
80+ const callFuncRef = callerMod.getFuncRef('call')
81+ const recvFuncRef = receiverMod.getFuncRef('receive')
82+ callFuncRef.gas = 100000
83+ recvFuncRef.gas = 1000
7884 const message = new Message({
79- funcRef: callerMod.getFuncRef('call'),
80- funcArguments: [receiverMod.getFuncRef('receive')]
85+ funcRef: callFuncRef,
86+ funcArguments: [recvFuncRef]
8187 })
8288
8389 hypervisor.send(message)
8490 const stateRoot = await hypervisor.createStateRoot()
@@ -104,19 +110,24 @@
104110
105111 const {module: callerMod} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm)
106112 const {module: receiverMod} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm)
107113
114+ const callFuncRef = callerMod.getFuncRef('call')
115+ const recvFuncRef = receiverMod.getFuncRef('receive')
116+ callFuncRef.gas = 100000
117+ recvFuncRef.gas = 100000
118+
108119 const message = new Message({
109- funcRef: callerMod.getFuncRef('call'),
110- funcArguments: [receiverMod.getFuncRef('receive')]
120+ funcRef: callFuncRef,
121+ funcArguments: [recvFuncRef]
111122 })
112123
113124 hypervisor.send(message)
114125 const stateRoot = await hypervisor.createStateRoot()
115126 // t.deepEquals(stateRoot, expectedState, 'expected root!')
116127 })
117128
118-tape('two communicating actors with callback', async t => {
129+tape('two communicating actors with private callback', async t => {
119130 t.plan(1)
120131 tester = t
121132 const expectedState = {
122133 '/': Buffer.from('9bf27cf07b75a90e0af530e2df73e3102482b24a', 'hex')
@@ -134,12 +145,17 @@
134145
135146 const {module: callerMod} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm)
136147 const {module: receiverMod} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm)
137148
149+ const callFuncRef = callerMod.getFuncRef('call')
150+ const recvFuncRef = receiverMod.getFuncRef('receive')
151+ callFuncRef.gas = 100000
152+ recvFuncRef.gas = 100000
153+
138154 const message = new Message({
139- funcRef: callerMod.getFuncRef('call'),
140- funcArguments: [receiverMod.getFuncRef('receive')]
141- }).on('execution:error', (e) => {console.log(e)})
155+ funcRef: callFuncRef,
156+ funcArguments: [recvFuncRef]
157+ })
142158
143159 hypervisor.send(message)
144160 const stateRoot = await hypervisor.createStateRoot()
145161 // t.deepEquals(stateRoot, expectedState, 'expected root!')
@@ -157,12 +173,12 @@
157173 const hypervisor = new Hypervisor(tree)
158174 hypervisor.registerContainer(TestWasmContainer)
159175
160176 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
177+ const funcRef = module.getFuncRef('test')
178+ funcRef.gas = 10000
161179
162- const message = new Message({
163- funcRef: module.getFuncRef('test')
164- }).on('done', actor => {
180+ const message = new Message({funcRef}).on('done', actor => {
165181 const a = actor.container.getMemory(0, 5)
166182 const b = actor.container.getMemory(5, 5)
167183 t.deepEquals(a, b, 'should copy memory correctly')
168184 })
@@ -181,11 +197,12 @@
181197 hypervisor.registerContainer(TestWasmContainer)
182198
183199 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
184200
185- const message = new Message({
186- funcRef: module.getFuncRef('test')
187- }).on('done', actor => {
201+ const funcRef = module.getFuncRef('test')
202+ funcRef.gas = 10000
203+
204+ const message = new Message({funcRef}).on('done', actor => {
188205 const a = actor.container.getMemory(0, 8)
189206 const b = actor.container.getMemory(8, 8)
190207 t.deepEquals(a, b, 'should copy memory correctly')
191208 })
@@ -206,19 +223,23 @@
206223
207224 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
208225
209226 await new Promise((resolve, reject) => {
227+ const funcRef = module.getFuncRef('store')
228+ funcRef.gas = 400
210229 const message = new Message({
211- funcRef: module.getFuncRef('store')
230+ funcRef
212231 }).on('done', actor => {
213232 resolve()
214233 })
215234 hypervisor.send(message)
216235 })
217236
218237 await new Promise((resolve, reject) => {
238+ const funcRef = module.getFuncRef('load')
239+ funcRef.gas = 400
219240 const message = new Message({
220- funcRef: module.getFuncRef('load')
241+ funcRef
221242 }).on('done', actor => {
222243 resolve()
223244 const b = actor.container.getMemory(5, 4)
224245 const result = Buffer.from(b).toString()
tests/wast/funcRef_caller.wastView
@@ -10,8 +10,9 @@
1010 call $self
1111 i32.const 0
1212 i32.const 8
1313 call $exports
14+
1415 i32.const 0
1516 get_local 0
1617 call $internalize
1718 i32.const 0
tests/wast/funcRef_reciever.wastView
@@ -1,9 +1,14 @@
11 (module
22 (import "func" "internalize" (func $internalize (param i32 i32)))
3+ (import "func" "set_gas_budget" (func $set_gas_budget (param i32 i32)))
34 (table (export "table") 1 1 anyfunc)
45 (func $receive (param i32)
56 get_local 0
7+ i32.const 10500
8+ call $set_gas_budget
9+
10+ get_local 0
611 i32.const 0
712 call $internalize
813 i32.const 5
914 i32.const 0
wasmContainer.jsView
@@ -115,10 +115,16 @@
115115 const {funcRef} = self.refs.get(ref, FunctionRef)
116116 const {funcRef: catchFunc} = self.refs.get(ref, FunctionRef)
117117 funcRef.catch = catchFunc
118118 },
119- getGasAmount: (funcRef) => {},
120- setGasAmount: (funcRef) => {}
119+ get_gas_budget: (funcRef) => {
120+ const func = self.refs.get(funcRef, 'func')
121+ return func.gas
122+ },
123+ set_gas_budget: (funcRef, amount) => {
124+ const func = self.refs.get(funcRef, 'func')
125+ func.gas = amount
126+ }
121127 },
122128 link: {
123129 wrap: ref => {
124130 const obj = this.refs.get(ref)
@@ -180,8 +186,9 @@
180186 }
181187 },
182188 metering: {
183189 usegas: amount => {
190+ this.actor.incrementTicks(amount)
184191 funcRef.gas -= amount
185192 if (funcRef.gas < 0) {
186193 throw new Error('out of gas! :(')
187194 }
@@ -191,9 +198,8 @@
191198 }
192199
193200 async onMessage (message) {
194201 const funcRef = message.funcRef
195- // console.log(funcRef)
196202 const intef = this.getInterface(funcRef)
197203 this.instance = WebAssembly.Instance(this.mod, intef)
198204 // map table indexes
199205 const table = this.instance.exports.table

Built with git-ssb-web