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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a1ab06e
feat: Apply wasmtime fuel
howjmay 29891ca
Update packages/wasmvm/wasmhost/wasmtimevm.go
BugFreeSoftware 983ca82
Update packages/wasmvm/wasmhost/wasmtimevm.go
BugFreeSoftware f849dc7
Update packages/wasmvm/wasmhost/wasmtimevm.go
BugFreeSoftware File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. under what circumstances could this happen? (having an error when calling |
||
} | ||
|
||
// 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) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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"?
There was a problem hiding this comment.
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