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

[x/programs] migrate wasm runtime to wasmtime and refactor for VM integration #477

Merged
merged 49 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
f23db1f
[x/programs] migrate to wasm runtime to wasmtime
hexfusion Sep 20, 2023
3ebbd38
Implement memory with tests
hexfusion Sep 20, 2023
8e01086
Add guest import examples
hexfusion Sep 20, 2023
7cec29b
Add vm/storage examples
hexfusion Sep 20, 2023
eb2c03f
Remove storage mocks
hexfusion Sep 20, 2023
8ed6774
Remove hard coded guest imports
hexfusion Sep 20, 2023
c7a8a34
Update token example to use new runtime
hexfusion Sep 20, 2023
c76d204
Implement guest export client
hexfusion Sep 20, 2023
2303f5c
Add config builder
hexfusion Sep 20, 2023
2b6ce89
React to new implementations
hexfusion Sep 20, 2023
7cf7f75
Implement meter
hexfusion Sep 20, 2023
fc80112
Remove failing tests/examples for now
hexfusion Sep 20, 2023
b79d4af
Fix lints
hexfusion Sep 20, 2023
dd620f3
Add Wasi mod as const
hexfusion Sep 20, 2023
3bf8905
Improve docs around host function imports
hexfusion Sep 20, 2023
d50618c
Add meter and runtime stop tests
hexfusion Sep 20, 2023
9f52536
Add licenses
hexfusion Sep 20, 2023
3de881a
Add precompile function
hexfusion Sep 20, 2023
61d8adb
Add precompile bench
hexfusion Sep 20, 2023
7d77550
Remove cache from precompiled bench
hexfusion Sep 20, 2023
2e68363
nit
hexfusion Sep 20, 2023
d2d2198
Add token program example
hexfusion Sep 20, 2023
579fce5
wip
hexfusion Sep 25, 2023
b14a5a5
remove wasi
hexfusion Oct 3, 2023
949a81d
wip
hexfusion Oct 3, 2023
36c7837
Add state import
hexfusion Oct 3, 2023
3d6fa55
Cleanup token
hexfusion Oct 3, 2023
81541cc
Recompile and update tests
hexfusion Oct 3, 2023
c15bba9
add optimizations that reduce binary size
hexfusion Oct 3, 2023
2691f72
Remove bulk memory from token tests
hexfusion Oct 3, 2023
b363a8b
Add runtime import factory
hexfusion Oct 4, 2023
45382ff
Rename state import module as pstate
hexfusion Oct 4, 2023
df816a7
Add counter program to program example
hexfusion Oct 4, 2023
98837de
Update runtime to use SupportedImports type
hexfusion Oct 4, 2023
cd4b1f0
Update token example
hexfusion Oct 4, 2023
149f568
Cleanup logging
hexfusion Oct 4, 2023
1290d2c
More log cleanup
hexfusion Oct 4, 2023
9aa4c77
Cargo fmt
hexfusion Oct 4, 2023
1b0d4be
Cleanup
hexfusion Oct 4, 2023
2b32009
Merge branch 'main' into wasmtime
hexfusion Oct 4, 2023
7ed5c49
Add RunShort benchmark for token
hexfusion Oct 5, 2023
7656d94
Add max wasm stack test
hexfusion Oct 5, 2023
61af5b1
Cleanup and nits
hexfusion Oct 5, 2023
fa1b0ac
Cleanup storage
hexfusion Oct 6, 2023
e045b56
Define defaults for all configs
hexfusion Oct 6, 2023
7fdaff5
Remove unused file
hexfusion Oct 6, 2023
8eb9122
Document opt-level flag for release profile
hexfusion Oct 6, 2023
574fbbd
Merge branch 'main' into wasmtime
hexfusion Oct 6, 2023
14857d7
Remove todo
hexfusion Oct 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add guest import examples
Signed-off-by: Sam Batschelet <sam.batschelet@avalabs.org>
  • Loading branch information
hexfusion committed Oct 3, 2023
commit 8e01086e0a8ac87bee24257e36f4c4af939f2aa7
205 changes: 205 additions & 0 deletions x/programs/examples/imports/balance/balance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
// Copyright (C) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package balance

import (
"context"

"go.uber.org/zap"

"github.com/bytecodealliance/wasmtime-go/v12"

"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/hypersdk/state"
"github.com/ava-labs/hypersdk/x/programs/examples/storage"
"github.com/ava-labs/hypersdk/x/programs/runtime"
)

const Name = "balance"

func New(log logging.Logger, mu state.Mutable) *Import {
return &Import{mu: mu, log: log}
}

type Import struct {
mu state.Mutable
log logging.Logger
meter runtime.Meter
}

func (i *Import) Name() string {
return Name
}

func (i *Import) Register(link runtime.Link, meter runtime.Meter) error {
if err := link.FuncWrap(Name, "get", i.getFn); err != nil {
return err
}
if err := link.FuncWrap(Name, "set", i.setFn); err != nil {
return err
}
if err := link.FuncWrap(Name, "add", i.addFn); err != nil {
return err
}
if err := link.FuncWrap(Name, "subtract", i.subFn); err != nil {
return err
}
if err := link.FuncWrap(Name, "delete", i.delFn); err != nil {
return err
}

i.meter = meter

return nil
}

func (i *Import) getFn(caller *wasmtime.Caller, assetPtr, keyPtr int32) int32 {
pk, err := runtime.PublicKeyFromOffset(caller, storage.HRP, keyPtr)
if err != nil {
i.log.Error("failed to read key from memory",
zap.Error(err),
)
return -1
}
assetID, err := runtime.IDFromOffset(caller, assetPtr)
Fixed Show fixed Hide fixed
if err != nil {
i.log.Error("failed to read assetID from memory",
zap.Error(err),
)
return -1
}

// TODO: add cost for state access?
// i.meter.Spend(BalanceCost)

balance, err := storage.GetBalance(context.Background(), i.mu, pk, assetID)
if err != nil {
i.log.Error("failed to get balance",
zap.Error(err),
)
return -1
}

return int32(balance)
}

func (i *Import) setFn(caller *wasmtime.Caller, assetPtr, keyPtr, balance int32) int32 {
pk, err := runtime.PublicKeyFromOffset(caller, storage.HRP, keyPtr)
if err != nil {
i.log.Error("failed to read key from memory",
zap.Error(err),
)
return -1
}
assetID, err := runtime.IDFromOffset(caller, assetPtr)
if err != nil {
i.log.Error("failed to read assetID from memory",
zap.Error(err),
)
return -1
}

// TODO: add cost for state access?
// i.meter.Spend(BalanceCost)
hexfusion marked this conversation as resolved.
Show resolved Hide resolved

err = storage.SetBalance(context.Background(), i.mu, pk, assetID, uint64(balance))
if err != nil {
i.log.Error("failed to set balance",
zap.Error(err),
)
return -1
}

return 0
}

func (i *Import) addFn(caller *wasmtime.Caller, assetPtr, keyPtr, amount int32) int32 {
pk, err := runtime.PublicKeyFromOffset(caller, storage.HRP, keyPtr)
if err != nil {
i.log.Error("failed to read key from memory",
zap.Error(err),
)
return -1
}
assetID, err := runtime.IDFromOffset(caller, assetPtr)
if err != nil {
i.log.Error("failed to read assetID from memory",
zap.Error(err),
)
return -1
}

// TODO: add cost for state access?
// i.meter.Spend(BalanceCost)

err = storage.AddBalance(context.Background(), i.mu, pk, assetID, uint64(amount), false)
if err != nil {
i.log.Error("failed to add balance",
zap.Error(err),
)
return -1
}

return 0
}

func (i *Import) subFn(caller *wasmtime.Caller, assetPtr, keyPtr, amount int32) int32 {
pk, err := runtime.PublicKeyFromOffset(caller, storage.HRP, keyPtr)
if err != nil {
i.log.Error("failed to read key from memory",
zap.Error(err),
)
return -1
}
assetID, err := runtime.IDFromOffset(caller, assetPtr)
if err != nil {
i.log.Error("failed to read assetID from memory",
zap.Error(err),
)
return -1
}

// TODO: add cost for state access?
// i.meter.Spend(BalanceCost)

err = storage.SubBalance(context.Background(), i.mu, pk, assetID, uint64(amount))
if err != nil {
i.log.Error("failed to add balance",
zap.Error(err),
)
return -1
}

return 0
}

func (i *Import) delFn(caller *wasmtime.Caller, assetPtr, keyPtr int32) int32 {
pk, err := runtime.PublicKeyFromOffset(caller, storage.HRP, keyPtr)
if err != nil {
i.log.Error("failed to read key from memory",
zap.Error(err),
)
return -1
}
assetID, err := runtime.IDFromOffset(caller, assetPtr)
if err != nil {
i.log.Error("failed to read assetID from memory",
zap.Error(err),
)
return -1
}

// TODO: add cost for state access?
// i.meter.Spend(BalanceCost)

err = storage.DeleteBalance(context.Background(), i.mu, pk, assetID)
if err != nil {
i.log.Error("failed to delete balance",
zap.Error(err),
)
return -1
}

return 0
}
153 changes: 153 additions & 0 deletions x/programs/examples/imports/hashmap/hashmap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// Copyright (C) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package hashmap

import (
"github.com/bytecodealliance/wasmtime-go/v12"

"go.uber.org/zap"

"github.com/ava-labs/avalanchego/utils/logging"

"github.com/ava-labs/hypersdk/state"
"github.com/ava-labs/hypersdk/x/programs/runtime"
)

// GlobalStorage is a global variable that holds program state.
hexfusion marked this conversation as resolved.
Show resolved Hide resolved
var GlobalStorage = storage{
state: make(map[int64]maps),
}

type maps map[string][]byte

// Key value store for program data
type storage struct {
state map[int64]maps
}

const Name = "map"

func New(log logging.Logger, mu state.Mutable) *Import {
return &Import{mu: mu, log: log}
}

func AddProgramID(id int64) {
GlobalStorage.state[id] = make(maps)
}

type Import struct {
mu state.Mutable
log logging.Logger
meter runtime.Meter
}

func (i *Import) Name() string {
return Name
}

func (i *Import) Register(link runtime.Link, meter runtime.Meter) error {
if err := link.FuncWrap(Name, "store_bytes", i.storeBytesFn); err != nil {
return err
}
if err := link.FuncWrap(Name, "get_bytes_len", i.getBytesLenFn); err != nil {
return err
}
if err := link.FuncWrap(Name, "get_bytes", i.getBytesFn); err != nil {
return err
}

i.meter = meter

return nil
}

func (i *Import) storeBytesFn(caller *wasmtime.Caller, id int64, keyPtr int32, keyLength int32, valuePtr int32, valueLength int32) int32 {
memory := runtime.NewMemory(runtime.NewExportClient(caller))

_, ok := GlobalStorage.state[int64(id)]
if !ok {
i.log.Error("failed to find program id in storage")
return -1
}

keyBytes, err := memory.Range(uint32(keyPtr), uint32(keyLength))
if err != nil {
i.log.Error("failed to read key from memory",
zap.Error(err),
)
return -1
}

valueBytes, err := memory.Range(uint32(valuePtr), uint32(valueLength))
if err != nil {
i.log.Error("failed to read value from memory",
zap.Error(err),
)
return -1
}

// Need to copy the value because the GC can collect the value after this function returns
// is this still true?
copiedValue := make([]byte, len(valueBytes))
copy(copiedValue, valueBytes)
GlobalStorage.state[id][string(keyBytes)] = copiedValue

return 0
}

func (i *Import) getBytesLenFn(caller *wasmtime.Caller, id int64, keyPtr int32, keyLength int32) int32 {
memory := runtime.NewMemory(runtime.NewExportClient(caller))

_, ok := GlobalStorage.state[int64(id)]
if !ok {
i.log.Error("failed to find program id in storage")
return -1
}

keyBytes, err := memory.Range(uint32(keyPtr), uint32(keyLength))
if err != nil {
i.log.Error("failed to read key from memory",
zap.Error(err),
)
return -1
}
val, ok := GlobalStorage.state[id][string(keyBytes)]
if !ok {
return -1
}

return int32(len(val))
}

func (i *Import) getBytesFn(caller *wasmtime.Caller, id int64, keyPtr int32, keyLength int32, valLength int32) int32 {
memory := runtime.NewMemory(runtime.NewExportClient(caller))

_, ok := GlobalStorage.state[int64(id)]
if !ok {
i.log.Error("failed to find program id in storage")
return -1
}

keyBytes, err := memory.Range(uint32(keyPtr), uint32(keyLength))
if err != nil {
i.log.Error("failed to read key from memory",
zap.Error(err),
)
return -1
}
val, ok := GlobalStorage.state[id][string(keyBytes)]
if !ok {
return -1
}

ptr, err := runtime.WriteBytes(memory, val)
if err != nil {
i.log.Error("failed to write to memory",
zap.Error(err),
)
return -1
}

return int32(ptr)
}
Loading