Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Apply wasmtime fuel #817

Merged
merged 4 commits into from Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Expand Up @@ -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
48 changes: 36 additions & 12 deletions packages/wasmvm/wasmhost/wasmtimevm.go
Expand Up @@ -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
Comment on lines +35 to +50
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm having a bit of trouble understanding this 😅 .
what is "remainingGas"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In wasmtime, we will assign certain amount gas before executing. If we consume fuel manually with ConsumeFuel , then the function will return the remaining gas which haven't used for exectution

}

// 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

under what circumstances could this happen? (having an error when calling vm.store.FuelConsumed())
is returning 0 the correct way to handle this?

}

// 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) {
Expand Down
5 changes: 3 additions & 2 deletions packages/wasmvm/wasmhost/wasmvm.go
Expand Up @@ -37,7 +37,7 @@ var (
)

type WasmVM interface {
GasBudget(budget uint64)
GasBudget(budget uint64) error
GasBurned() uint64
GasEnable(enable bool)
Instantiate() error
Expand All @@ -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 {
Expand Down