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

movevm loaderv2 integration #284

Merged
merged 6 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ replace (
// Fix upstream GHSA-h395-qcrw-5vmq vulnerability.
// TODO Remove it: https://github.com/cosmos/cosmos-sdk/issues/10409
github.com/gin-gonic/gin => github.com/gin-gonic/gin v1.8.1

github.com/initia-labs/movevm => ../movevm
// Downgraded to avoid bugs in following commits which caused simulations to fail.
github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
)
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -732,8 +732,6 @@ github.com/initia-labs/OPinit/api v0.5.1 h1:zwyJf7HtKJCKvLJ1R9PjVfJO1L+d/jKoeFyT
github.com/initia-labs/OPinit/api v0.5.1/go.mod h1:gHK6DEWb3/DqQD5LjKirUx9jilAh2UioXanoQdgqVfU=
github.com/initia-labs/cometbft v0.0.0-20240923045653-ba99eb347236 h1:+HmPQ1uptOe4r5oQHuHMG5zF1F3maNoEba5uiTUMnlk=
github.com/initia-labs/cometbft v0.0.0-20240923045653-ba99eb347236/go.mod h1:GPHp3/pehPqgX1930HmK1BpBLZPxB75v/dZg8Viwy+o=
github.com/initia-labs/movevm v0.5.0 h1:dBSxoVyUumSE4x6/ZSOWtvbtZpw+V4W25/NH6qLU0uQ=
github.com/initia-labs/movevm v0.5.0/go.mod h1:aUWdvFZPdULjJ2McQTE+mLnfnG3CLAz0TWJRFzFFUwg=
github.com/jhump/protoreflect v1.15.3 h1:6SFRuqU45u9hIZPJAoZ8c28T3nK64BNdp9w6jFonzls=
github.com/jhump/protoreflect v1.15.3/go.mod h1:4ORHmSBmlCW8fh3xHmJMGyul1zNqZK4Elxc8qKP+p1k=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
Expand Down
16 changes: 16 additions & 0 deletions x/move/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,45 @@ import (

// DefaultContractSimulationGasLimit - default max simulation gas
const DefaultContractSimulationGasLimit = uint64(3_000_000)
const DefaultScriptCacheCapacity = uint64(100)
const DefaultModuleCacheCapacity = uint64(500)

const (
flagContractSimulationGasLimit = "move.contract-simulation-gas-limit"
flagScriptCacheCapacity = "move.script-cache-capacity"
flagModuleCacheCapacity = "move.module-cache-capacity"
)

// MoveConfig is the extra config required for move
type MoveConfig struct {
ContractSimulationGasLimit uint64 `mapstructure:"contract-simulation-gas-limit"`
ScriptCacheCapacity uint64 `mapstructure:"script-cache-capacity"`
ModuleCacheCapacity uint64 `mapstructure:"module-cache-capacity"`
}

// DefaultMoveConfig returns the default settings for MoveConfig
func DefaultMoveConfig() MoveConfig {
return MoveConfig{
ContractSimulationGasLimit: DefaultContractSimulationGasLimit,
ScriptCacheCapacity: DefaultScriptCacheCapacity,
ModuleCacheCapacity: DefaultModuleCacheCapacity,
}
}

// GetConfig load config values from the app options
func GetConfig(appOpts servertypes.AppOptions) MoveConfig {
return MoveConfig{
ContractSimulationGasLimit: cast.ToUint64(appOpts.Get(flagContractSimulationGasLimit)),
ScriptCacheCapacity: cast.ToUint64(appOpts.Get(flagScriptCacheCapacity)),
ModuleCacheCapacity: cast.ToUint64(appOpts.Get(flagModuleCacheCapacity)),
}
}

// AddConfigFlags implements servertypes.MoveConfigFlags interface.
func AddConfigFlags(startCmd *cobra.Command) {
startCmd.Flags().Uint64(flagContractSimulationGasLimit, DefaultContractSimulationGasLimit, "Set the max simulation gas for move contract execution")
startCmd.Flags().Uint64(flagScriptCacheCapacity, DefaultScriptCacheCapacity, "Set the script cache capacity")
startCmd.Flags().Uint64(flagModuleCacheCapacity, DefaultModuleCacheCapacity, "Set the module cache capacity")
}

// DefaultConfigTemplate default config template for move module
Expand All @@ -47,4 +59,8 @@ const DefaultConfigTemplate = `
[move]
# The maximum gas amount can be used in a tx simulation call.
contract-simulation-gas-limit = "{{ .MoveConfig.ContractSimulationGasLimit }}"
# The capacity of the script cache.
script-cache-capacity = "{{ .MoveConfig.ScriptCacheCapacity }}"
# The capacity of the module cache.
module-cache-capacity = "{{ .MoveConfig.ModuleCacheCapacity }}"
`
4 changes: 1 addition & 3 deletions x/move/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ func (k Keeper) Initialize(
}

vmStore := types.NewVMStore(ctx, k.VMStore)
execRes, err := execVM(ctx, k, func(vm types.VMEngine) (vmtypes.ExecutionResult, error) {
return vm.Initialize(vmStore, api, env, vmtypes.NewModuleBundle(modules...), _allowedPublishers)
})
execRes, err := k.initiaMoveVM.Initialize(vmStore, api, env, vmtypes.NewModuleBundle(modules...), _allowedPublishers)
if err != nil {
return err
}
Expand Down
63 changes: 23 additions & 40 deletions x/move/keeper/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,14 @@ func (k Keeper) executeEntryFunction(
sdkCtx = sdkCtx.WithGasMeter(storetypes.NewInfiniteGasMeter())

gasBalance := gasForRuntime
execRes, err := execVM(ctx, k, func(vm types.VMEngine) (vmtypes.ExecutionResult, error) {
return vm.ExecuteEntryFunction(
&gasBalance,
types.NewVMStore(sdkCtx, k.VMStore),
NewApi(k, sdkCtx),
types.NewEnv(sdkCtx, ac, ec),
senders,
payload,
)
})
execRes, err := k.initiaMoveVM.ExecuteEntryFunction(
&gasBalance,
types.NewVMStore(sdkCtx, k.VMStore),
NewApi(k, sdkCtx),
types.NewEnv(sdkCtx, ac, ec),
senders,
payload,
)
beer-1 marked this conversation as resolved.
Show resolved Hide resolved

// consume gas first and check error
gasUsed := gasForRuntime - gasBalance
Expand Down Expand Up @@ -314,16 +312,14 @@ func (k Keeper) executeScript(
sdkCtx = sdkCtx.WithGasMeter(storetypes.NewInfiniteGasMeter())

gasBalance := gasForRuntime
execRes, err := execVM(ctx, k, func(vm types.VMEngine) (vmtypes.ExecutionResult, error) {
return vm.ExecuteScript(
&gasBalance,
types.NewVMStore(sdkCtx, k.VMStore),
NewApi(k, sdkCtx),
types.NewEnv(sdkCtx, ac, ec),
senders,
payload,
)
})
execRes, err := k.initiaMoveVM.ExecuteScript(
&gasBalance,
types.NewVMStore(sdkCtx, k.VMStore),
NewApi(k, sdkCtx),
types.NewEnv(sdkCtx, ac, ec),
senders,
payload,
)
beer-1 marked this conversation as resolved.
Show resolved Hide resolved

// consume gas first and check error
gasUsed := gasForRuntime - gasBalance
Expand Down Expand Up @@ -658,15 +654,13 @@ func (k Keeper) executeViewFunction(
gasForRuntime := gasMeter.Limit() - gasMeter.GasConsumedToLimit()

gasBalance := gasForRuntime
viewRes, err := execVM(ctx, k, func(vm types.VMEngine) (vmtypes.ViewOutput, error) {
return vm.ExecuteViewFunction(
&gasBalance,
types.NewVMStore(ctx, k.VMStore),
api,
env,
payload,
)
})
viewRes, err := k.initiaMoveVM.ExecuteViewFunction(
&gasBalance,
types.NewVMStore(ctx, k.VMStore),
api,
env,
payload,
)
beer-1 marked this conversation as resolved.
Show resolved Hide resolved

// consume gas first and check error
gasUsed := gasForRuntime - gasBalance
Expand All @@ -677,14 +671,3 @@ func (k Keeper) executeViewFunction(

return viewRes, gasUsed, nil
}

// execVM runs vm in separate function statement to release right after execution
// to avoid deadlock even if the function panics
//
// TODO - remove this after loader v2 is installed
func execVM[T any](ctx context.Context, k Keeper, f func(types.VMEngine) (T, error)) (T, error) {
vm := k.acquireVM(ctx)
defer k.releaseVM()

return f(vm)
}
34 changes: 10 additions & 24 deletions x/move/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
"context"
"errors"

"golang.org/x/sync/semaphore"

"cosmossdk.io/collections"
"cosmossdk.io/core/address"
corestoretypes "cosmossdk.io/core/store"
Expand All @@ -17,7 +15,7 @@
moveconfig "github.com/initia-labs/initia/x/move/config"
"github.com/initia-labs/initia/x/move/types"

vm "github.com/initia-labs/movevm"

Check failure on line 18 in x/move/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / Run test and upload codecov

github.com/initia-labs/[email protected]: replacement directory ../movevm does not exist
vmapi "github.com/initia-labs/movevm/api"
vmtypes "github.com/initia-labs/movevm/types"
)
Expand All @@ -43,10 +41,7 @@

config moveconfig.MoveConfig

// TODO - remove after loader v2
moveVMs []types.VMEngine
moveVMIdx *uint64
moveVMSemaphore *semaphore.Weighted
initiaMoveVM types.VMEngine

feeCollector string
authority string
Expand Down Expand Up @@ -89,21 +84,14 @@
moveConfig.ContractSimulationGasLimit = moveconfig.DefaultContractSimulationGasLimit
}

// use only one VM for now because we need to clear the cache on every module upgrade.
// but if we have multiple VMs, only the VM that executed the module upgrade will have the cache cleared.
vmCount := 1
moveVMIdx := uint64(0)
vms := make([]types.VMEngine, vmCount)
for i := 0; i < vmCount; i++ {
moveVM, err := vm.NewVM(vmtypes.InitiaVMConfig{
// TODO: check this before mainnet
AllowUnstable: true,
})
if err != nil {
panic(err)
}

vms[i] = &moveVM
moveVM, err := vm.NewVM(vmtypes.InitiaVMConfig{
// TODO: check this before mainnet
AllowUnstable: true,
ScriptCacheCapacity: moveConfig.ScriptCacheCapacity,
ModuleCacheCapacity: moveConfig.ModuleCacheCapacity,
})
if err != nil {
panic(err)
beer-1 marked this conversation as resolved.
Show resolved Hide resolved
}

sb := collections.NewSchemaBuilder(storeService)
Expand All @@ -116,9 +104,7 @@
msgRouter: msgRouter,
grpcRouter: grpcRouter,
config: moveConfig,
moveVMs: vms,
moveVMIdx: &moveVMIdx,
moveVMSemaphore: semaphore.NewWeighted(int64(vmCount)),
initiaMoveVM: &moveVM,
beer-1 marked this conversation as resolved.
Show resolved Hide resolved
distrKeeper: distrKeeper,
StakingKeeper: stakingKeeper,
RewardKeeper: rewardKeeper,
Expand Down
24 changes: 0 additions & 24 deletions x/move/keeper/vmpool.go

This file was deleted.

Loading