Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Aug 14, 2024
1 parent 937dacd commit af6fca1
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 78 deletions.
16 changes: 16 additions & 0 deletions cmd/move/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ const (
flagFetchDepsOnly = "fetch-deps-only"
flagSkipFetchLatestGitDeps = "skip-fetch-latest-git-deps"
flagBytecodeVersion = "bytecode-version"
flagCompilerVersion = "compiler-version"
flagLanguageVersion = "language-version"
/* test options */
flagGasLimit = "gas-limit"
flagGasLimitShorthand = "g"
Expand Down Expand Up @@ -613,6 +615,8 @@ the 'tests' directory`)
cmd.Flags().Bool(flagVerbose, false, "Print additional diagnostics if available")
cmd.Flags().Bool(flagSkipFetchLatestGitDeps, false, "Skip fetching latest git dependencies")
cmd.Flags().Uint32(flagBytecodeVersion, 0, "Specify the version of the bytecode the compiler is going to emit")
cmd.Flags().Uint32(flagCompilerVersion, 1, "Specify the version of the compiler to use")
cmd.Flags().Uint32(flagLanguageVersion, 1, "Specify the version of the language to use")
}

func addMoveTestFlags(cmd *cobra.Command) {
Expand Down Expand Up @@ -720,6 +724,18 @@ func getBuildConfig(cmd *cobra.Command) (*buildtypes.BuildConfig, error) {
}
options = append(options, buildtypes.WithBytecodeVersion(bytecodeVersion))

compilerVersion, err := cmd.Flags().GetUint32(flagCompilerVersion)
if err != nil {
return nil, err
}
options = append(options, buildtypes.WithCompilerVersion(compilerVersion))

languageVersion, err := cmd.Flags().GetUint32(flagLanguageVersion)
if err != nil {
return nil, err
}
options = append(options, buildtypes.WithLanguageVersion(languageVersion))

bc := buildtypes.NewBuildConfig(options...)

return &bc, nil
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,5 @@ replace (
// replace broken goleveldb
github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
)

replace github.com/initia-labs/movevm => ../movevm
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -733,8 +733,6 @@ github.com/initia-labs/OPinit/api v0.4.1 h1:Q8etW92LiwekKZxzDYVFdiHF3uOpEA4nyajy
github.com/initia-labs/OPinit/api v0.4.1/go.mod h1:Xy/Nt3ubXLQ4zKn0m7RuQOM1sj8TVdlNNyek21TGYR0=
github.com/initia-labs/cometbft v0.0.0-20240802022359-e0a5ce0336b6 h1:ObKvj9nGiKE+MWZ/LnH3rPcXsnBVVn4c30/iw+fMsd4=
github.com/initia-labs/cometbft v0.0.0-20240802022359-e0a5ce0336b6/go.mod h1:jHPx9vQpWzPHEAiYI/7EDKaB1NXhK6o3SArrrY8ExKc=
github.com/initia-labs/movevm v0.3.4 h1:kzqs6uzTq0f5peZJNzLq/1qgnmAFfC+I9eCyPBducxM=
github.com/initia-labs/movevm v0.3.4/go.mod h1:6MxR4GP5zH3JUc1IMgfqAe1e483mZVS7fshPknZPJ30=
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
12 changes: 6 additions & 6 deletions x/ibc-hooks/move-hooks/ack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func Test_onAckPacket_memo(t *testing.T) {
require.NoError(t, err)

// check the contract state
queryRes, err := input.MoveKeeper.ExecuteViewFunction(
queryRes, _, err := input.MoveKeeper.ExecuteViewFunction(
ctx,
vmtypes.StdAddress,
"Counter",
Expand All @@ -94,7 +94,7 @@ func Test_onAckPacket_memo(t *testing.T) {
require.NoError(t, err)

// check the contract state; increased by 99 if ack is success
queryRes, err = input.MoveKeeper.ExecuteViewFunction(
queryRes, _, err = input.MoveKeeper.ExecuteViewFunction(
ctx,
vmtypes.StdAddress,
"Counter",
Expand All @@ -111,7 +111,7 @@ func Test_onAckPacket_memo(t *testing.T) {
}, failedAckBz, addr)
require.NoError(t, err)

queryRes, err = input.MoveKeeper.ExecuteViewFunction(
queryRes, _, err = input.MoveKeeper.ExecuteViewFunction(
ctx,
vmtypes.StdAddress,
"Counter",
Expand Down Expand Up @@ -189,7 +189,7 @@ func Test_onAckPacket_memo_ICS721(t *testing.T) {
require.NoError(t, err)

// check the contract state
queryRes, err := input.MoveKeeper.ExecuteViewFunction(
queryRes, _, err := input.MoveKeeper.ExecuteViewFunction(
ctx,
vmtypes.StdAddress,
"Counter",
Expand All @@ -210,7 +210,7 @@ func Test_onAckPacket_memo_ICS721(t *testing.T) {
require.NoError(t, err)

// check the contract state; increased by 99 if ack is success
queryRes, err = input.MoveKeeper.ExecuteViewFunction(
queryRes, _, err = input.MoveKeeper.ExecuteViewFunction(
ctx,
vmtypes.StdAddress,
"Counter",
Expand All @@ -227,7 +227,7 @@ func Test_onAckPacket_memo_ICS721(t *testing.T) {
}, failedAckBz, addr)
require.NoError(t, err)

queryRes, err = input.MoveKeeper.ExecuteViewFunction(
queryRes, _, err = input.MoveKeeper.ExecuteViewFunction(
ctx,
vmtypes.StdAddress,
"Counter",
Expand Down
12 changes: 8 additions & 4 deletions x/ibc-hooks/move-hooks/receive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ func Test_OnReceivePacketWithoutMemo(t *testing.T) {
Amount: "10000",
Sender: addr.String(),
Receiver: addr2.String(),
Memo: "",
Memo: `{
"move": {
"message": null
}
}`,
}

dataBz, err := json.Marshal(&data)
Expand Down Expand Up @@ -76,7 +80,7 @@ func Test_onReceiveIcs20Packet_memo(t *testing.T) {
require.True(t, ack.Success())

// check the contract state
queryRes, err := input.MoveKeeper.ExecuteViewFunction(
queryRes, _, err := input.MoveKeeper.ExecuteViewFunction(
ctx,
vmtypes.StdAddress,
"Counter",
Expand Down Expand Up @@ -158,7 +162,7 @@ func Test_onReceivePacket_memo_ICS721(t *testing.T) {
require.True(t, ack.Success())

// check the contract state
queryRes, err := input.MoveKeeper.ExecuteViewFunction(
queryRes, _, err := input.MoveKeeper.ExecuteViewFunction(
ctx,
vmtypes.StdAddress,
"Counter",
Expand Down Expand Up @@ -215,7 +219,7 @@ func Test_onReceivePacket_memo_ICS721_Wasm(t *testing.T) {
require.True(t, ack.Success())

// check the contract state
queryRes, err := input.MoveKeeper.ExecuteViewFunction(
queryRes, _, err := input.MoveKeeper.ExecuteViewFunction(
ctx,
vmtypes.StdAddress,
"Counter",
Expand Down
8 changes: 4 additions & 4 deletions x/ibc-hooks/move-hooks/timeout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func Test_onTimeoutPacket_memo(t *testing.T) {
require.NoError(t, err)

// check the contract state
queryRes, err := input.MoveKeeper.ExecuteViewFunction(
queryRes, _, err := input.MoveKeeper.ExecuteViewFunction(
ctx,
vmtypes.StdAddress,
"Counter",
Expand All @@ -87,7 +87,7 @@ func Test_onTimeoutPacket_memo(t *testing.T) {
require.NoError(t, err)

// check the contract state; increased by 99
queryRes, err = input.MoveKeeper.ExecuteViewFunction(
queryRes, _, err = input.MoveKeeper.ExecuteViewFunction(
ctx,
vmtypes.StdAddress,
"Counter",
Expand Down Expand Up @@ -159,7 +159,7 @@ func Test_onTimeoutPacket_memo_ICS721(t *testing.T) {
require.NoError(t, err)

// check the contract state
queryRes, err := input.MoveKeeper.ExecuteViewFunction(
queryRes, _, err := input.MoveKeeper.ExecuteViewFunction(
ctx,
vmtypes.StdAddress,
"Counter",
Expand All @@ -180,7 +180,7 @@ func Test_onTimeoutPacket_memo_ICS721(t *testing.T) {
require.NoError(t, err)

// check the contract state; increased by 99 if ack is success
queryRes, err = input.MoveKeeper.ExecuteViewFunction(
queryRes, _, err = input.MoveKeeper.ExecuteViewFunction(
ctx,
vmtypes.StdAddress,
"Counter",
Expand Down
2 changes: 1 addition & 1 deletion x/ibc/nft-transfer/keeper/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (suite *KeeperTestSuite) GetNFTOwner(ctx context.Context, k *keeper.Keeper,
ta, err := tokenAddr.BcsSerialize()
suite.Require().NoError(err)

res, err := moveKeeper.ExecuteViewFunction(ctx, moduleAddr, "object", "owner", typeTags, [][]byte{ta})
res, _, err := moveKeeper.ExecuteViewFunction(ctx, moduleAddr, "object", "owner", typeTags, [][]byte{ta})
suite.Require().NoError(err)

strAddr := strings.Trim(res.Ret, "\"")
Expand Down
24 changes: 0 additions & 24 deletions x/move/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,34 @@ import (
servertypes "github.com/cosmos/cosmos-sdk/server/types"
)

// DefaultModuleCacheCapacity the number of modules can be stay in module cache
const DefaultModuleCacheCapacity = uint64(500)

// DefaultScriptCacheCapacity the number of modules can be stay in module cache
const DefaultScriptCacheCapacity = uint64(100)

// DefaultContractSimulationGasLimit - default max simulation gas
const DefaultContractSimulationGasLimit = uint64(3_000_000)

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

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

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

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

// AddConfigFlags implements servertypes.MoveConfigFlags interface.
func AddConfigFlags(startCmd *cobra.Command) {
startCmd.Flags().Uint64(flagModuleCacheCapacity, DefaultModuleCacheCapacity, "Set the number of modules which can stay in the cache")
startCmd.Flags().Uint64(flagScriptCacheCapacity, DefaultScriptCacheCapacity, "Set the number of scripts which can stay in the cache")
startCmd.Flags().Uint64(flagContractSimulationGasLimit, DefaultContractSimulationGasLimit, "Set the max simulation gas for move contract execution")
}

Expand All @@ -61,14 +45,6 @@ const DefaultConfigTemplate = `
###############################################################################
[move]
# in-memory cache for Move modules.
# The value is in MiB not bytes.
module-cache-capacity = "{{ .MoveConfig.ModuleCacheCapacity }}"
# in-memory cache for Move scripts.
# The value is in MiB not bytes.
script-cache-capacity = "{{ .MoveConfig.ScriptCacheCapacity }}"
# The maximum gas amount can be used in a tx simulation call.
contract-simulation-gas-limit = "{{ .MoveConfig.ContractSimulationGasLimit }}"
`
6 changes: 3 additions & 3 deletions x/move/keeper/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func Test_GetPrice(t *testing.T) {
pairIdArg, err := vmtypes.SerializeString(pairId)
require.NoError(t, err)

res, err := input.MoveKeeper.ExecuteViewFunction(
res, _, err := input.MoveKeeper.ExecuteViewFunction(
ctx,
vmtypes.StdAddress,
"oracle",
Expand Down Expand Up @@ -287,14 +287,14 @@ func Test_API_CustomQuery(t *testing.T) {
require.NoError(t, err)

// to sdk
res, err := input.MoveKeeper.ExecuteViewFunction(ctx, vmtypes.TestAddress, "TestAddress", "to_sdk", []vmtypes.TypeTag{}, [][]byte{vmAddr.Bytes()})
res, _, err := input.MoveKeeper.ExecuteViewFunction(ctx, vmtypes.TestAddress, "TestAddress", "to_sdk", []vmtypes.TypeTag{}, [][]byte{vmAddr.Bytes()})
require.NoError(t, err)
require.Equal(t, fmt.Sprintf("\"%s\"", addrs[0].String()), res.Ret)

// from sdk
inputBz, err := vmtypes.SerializeString(addrs[0].String())
require.NoError(t, err)
res, err = input.MoveKeeper.ExecuteViewFunction(ctx, vmtypes.TestAddress, "TestAddress", "from_sdk", []vmtypes.TypeTag{}, [][]byte{inputBz})
res, _, err = input.MoveKeeper.ExecuteViewFunction(ctx, vmtypes.TestAddress, "TestAddress", "from_sdk", []vmtypes.TypeTag{}, [][]byte{inputBz})
require.NoError(t, err)
require.Equal(t, fmt.Sprintf("\"%s\"", vmAddr.String()), res.Ret)
}
32 changes: 19 additions & 13 deletions x/move/keeper/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,19 @@ func (k Keeper) executeEntryFunction(
sdkCtx = sdkCtx.WithGasMeter(storetypes.NewInfiniteGasMeter())

// run vm
gasBalance := gasForRuntime
execRes, err := k.moveVM.ExecuteEntryFunction(
&gasBalance,
types.NewVMStore(sdkCtx, k.VMStore),
NewApi(k, sdkCtx),
types.NewEnv(sdkCtx, ac, ec),
gasForRuntime,
senders,
payload,
)

// consume gas first and check error
gasMeter.ConsumeGas(execRes.GasUsed, "move runtime")
gasUsed := gasForRuntime - gasBalance
gasMeter.ConsumeGas(gasUsed, "move runtime")
if err != nil {
return err
}
Expand Down Expand Up @@ -312,17 +314,19 @@ func (k Keeper) executeScript(
sdkCtx = sdkCtx.WithGasMeter(storetypes.NewInfiniteGasMeter())

// run vm
gasBalance := gasForRuntime
execRes, err := k.moveVM.ExecuteScript(
&gasBalance,
types.NewVMStore(sdkCtx, k.VMStore),
NewApi(k, sdkCtx),
types.NewEnv(sdkCtx, ac, ec),
gasForRuntime,
senders,
payload,
)

// consume gas first and check error
gasMeter.ConsumeGas(execRes.GasUsed, "move runtime")
gasUsed := gasForRuntime - gasBalance
gasMeter.ConsumeGas(gasUsed, "move runtime")
if err != nil {
return err
}
Expand Down Expand Up @@ -511,7 +515,7 @@ func (k Keeper) ExecuteViewFunction(
functionName string,
typeArgs []vmtypes.TypeTag,
args [][]byte,
) (vmtypes.ViewOutput, error) {
) (vmtypes.ViewOutput, uint64, error) {
return k.executeViewFunction(
ctx,
moduleAddr,
Expand All @@ -530,7 +534,7 @@ func (k Keeper) ExecuteViewFunctionJSON(
functionName string,
typeArgs []vmtypes.TypeTag,
jsonArgs []string,
) (vmtypes.ViewOutput, error) {
) (vmtypes.ViewOutput, uint64, error) {
args := make([][]byte, len(jsonArgs))
for i, jsonArg := range jsonArgs {
// use unsafe method for fast conversion
Expand All @@ -556,7 +560,7 @@ func (k Keeper) executeViewFunction(
typeArgs []vmtypes.TypeTag,
args [][]byte,
isJSON bool,
) (vmtypes.ViewOutput, error) {
) (vmtypes.ViewOutput, uint64, error) {
payload, err := types.BuildExecuteViewFunctionPayload(
moduleAddr,
moduleName,
Expand All @@ -566,12 +570,12 @@ func (k Keeper) executeViewFunction(
isJSON,
)
if err != nil {
return vmtypes.ViewOutput{}, err
return vmtypes.ViewOutput{}, 0, err
}

executionCounter, err := k.ExecutionCounter.Next(ctx)
if err != nil {
return vmtypes.ViewOutput{}, err
return vmtypes.ViewOutput{}, 0, err
}

api := NewApi(k, ctx)
Expand All @@ -585,19 +589,21 @@ func (k Keeper) executeViewFunction(
gasMeter := sdkCtx.GasMeter()
gasForRuntime := gasMeter.Limit() - gasMeter.GasConsumedToLimit()

gasBalance := gasForRuntime
viewRes, err := k.moveVM.ExecuteViewFunction(
&gasBalance,
types.NewVMStore(ctx, k.VMStore),
api,
env,
gasForRuntime,
payload,
)
if err != nil {
return vmtypes.ViewOutput{}, err
return vmtypes.ViewOutput{}, 0, err
}

// consume gas first and check error
gasMeter.ConsumeGas(viewRes.GasUsed, "view; move runtime")
gasUsed := gasForRuntime - gasBalance
gasMeter.ConsumeGas(gasUsed, "view; move runtime")

return viewRes, nil
return viewRes, gasUsed, nil
}
Loading

0 comments on commit af6fca1

Please sign in to comment.