diff --git a/go.mod b/go.mod index a4f0bb8cd1..a3675d9063 100644 --- a/go.mod +++ b/go.mod @@ -55,3 +55,5 @@ replace ( github.com/linxGnu/grocksdb => github.com/gohornet/grocksdb v1.6.38-0.20211012114404-55f425442260 go.dedis.ch/kyber/v3 v3.0.13 => github.com/kape1395/kyber/v3 v3.0.14-0.20210622094514-fefb81148dc3 ) + +replace github.com/bytecodealliance/wasmtime-go => ../wasmtime-go diff --git a/packages/wasmvm/wasmhost/wasmtimevm.go b/packages/wasmvm/wasmhost/wasmtimevm.go index aa07f9e65a..21b894a9ea 100644 --- a/packages/wasmvm/wasmhost/wasmtimevm.go +++ b/packages/wasmvm/wasmhost/wasmtimevm.go @@ -11,31 +11,55 @@ import ( type WasmTimeVM struct { WasmVMBase - engine *wasmtime.Engine - instance *wasmtime.Instance - interrupt *wasmtime.InterruptHandle - linker *wasmtime.Linker - memory *wasmtime.Memory - module *wasmtime.Module - store *wasmtime.Store + engine *wasmtime.Engine + instance *wasmtime.Instance + interrupt *wasmtime.InterruptHandle + linker *wasmtime.Linker + memory *wasmtime.Memory + module *wasmtime.Module + store *wasmtime.Store + remainingGas uint64 } func NewWasmTimeVM() WasmVM { vm := &WasmTimeVM{} config := wasmtime.NewConfig() config.SetInterruptable(true) + config.SetConsumeFuel(true) vm.engine = wasmtime.NewEngineWithConfig(config) return vm } -func (vm *WasmTimeVM) GasBudget(budget uint64) { - // TODO turn on gas usage for VM somewhere - // set gas budget to provided budget here +// GasBudget sets gas budget. Each time GasBudget is called, we will restart counting the total burned gas +func (vm *WasmTimeVM) GasBudget(budget uint64) error { + remaining, err := vm.store.ConsumeFuel(0) + if err != nil { + return err + } + + vm.remainingGas = remaining + + if budget > remaining { + return vm.store.AddFuel(budget - remaining) + } + + _, err = vm.store.ConsumeFuel(remaining - budget) + if err != nil { + return err + } + return nil } +// GasBurned will return the gas has been burned by wasmtime since the last time WasmTimeVM.GasBudget was called func (vm *WasmTimeVM) GasBurned() uint64 { - // TODO return actual amount of gas burned by VM - return 0 + gasBurned, ok := vm.store.FuelConsumed() + if !ok { + return 0 + } + + // gasBurned is the gas has been burned since the vm.store instance was been created + // so we need to subtract the remaining gas before calling vm.GasBudget + return gasBurned - vm.remainingGas } func (vm *WasmTimeVM) Instantiate() (err error) { diff --git a/packages/wasmvm/wasmhost/wasmvm.go b/packages/wasmvm/wasmhost/wasmvm.go index 4a67fcb7c6..25be3c36a9 100644 --- a/packages/wasmvm/wasmhost/wasmvm.go +++ b/packages/wasmvm/wasmhost/wasmvm.go @@ -37,7 +37,7 @@ var ( ) type WasmVM interface { - GasBudget(budget uint64) + GasBudget(budget uint64) error GasBurned() uint64 GasEnable(enable bool) Instantiate() error @@ -61,8 +61,9 @@ type WasmVMBase struct { timeoutStarted bool } -func (vm *WasmVMBase) GasBudget(budget uint64) { +func (vm *WasmVMBase) GasBudget(budget uint64) error { // ignore gas budget + return nil } func (vm *WasmVMBase) GasBurned() uint64 {