Skip to content

Commit

Permalink
feat: add submsg impl (#277)
Browse files Browse the repository at this point in the history
* add submsg impl to allow failure in cosmos stargate call and set upgrade handler for testnet upgrade

* add diff command

* fix condition

* change default csr to zero

* fix review comments

* change comment
  • Loading branch information
beer-1 authored Oct 7, 2024
1 parent 02b3968 commit d14724b
Show file tree
Hide file tree
Showing 15 changed files with 336 additions and 48 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ARG TARGETARCH
ARG GOARCH

# See https://github.com/initia-labs/movevm/releases
ENV LIBMOVEVM_VERSION=v0.4.12
ENV LIBMOVEVM_VERSION=v0.5.0

# Install necessary packages
RUN set -eux; apk add --no-cache ca-certificates build-base git cmake
Expand Down
32 changes: 30 additions & 2 deletions app/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,47 @@ package app

import (
"context"
"encoding/base64"

"cosmossdk.io/errors"
upgradetypes "cosmossdk.io/x/upgrade/types"
"github.com/cosmos/cosmos-sdk/types/module"

movetypes "github.com/initia-labs/initia/x/move/types"
vmtypes "github.com/initia-labs/movevm/types"
)

const upgradeName = "0.2.2"
const upgradeName = "0.5.4"

// RegisterUpgradeHandlers returns upgrade handlers
func (app *InitiaApp) RegisterUpgradeHandlers(cfg module.Configurator) {
app.UpgradeKeeper.SetUpgradeHandler(
upgradeName,
func(ctx context.Context, _ upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
return app.ModuleManager.RunMigrations(ctx, app.configurator, vm)
// diff: https://github.com/initia-labs/movevm/compare/v0.4.12...v0.5.0
codes := []string{
// cosmos.move
"oRzrCwYAAAALAQAQAhAaAyqXAQTBAQIFwwGQBAfTBZ4FCPEKIAaRCxoQqwt0Cp8MGgy5DPUJAAAACAAPABEAEgAZACgAMAABAwAABQMAAQcHAAIOBwEAAQMQCwAGJwgAAA0AAQAEEwIDAAEUBAUAABUGAQAAFgcBAAAXCAEAAAIBCgAAGAwKAAUaDQ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ABEdCwELAwsCCwQ4CQIfAQAABAMLADgKAiEBAAAEBQsACwELAjgLAgUBAAAEBg4ADgERIxQRJAIlAQAABAULAAsBCwIRJgIdAAAABAoLABEnBwAhBAYFCQYBAAAAAAAAABEoJwIpAQAAJC4OABEqBgUAAAAAAAAAJAQSBwEMAQ4BDgAGAAAAAAAAAAAGBQAAAAAAAAARKwwCDgIRIyEMAwUUCQwDCwMEJw4AESoOAAYFAAAAAAAAAAwEDAUMBgsFCwQLBhErDAcOBxEsES0MCAUrBwALABEFDAgLCDgCAi4BAAEEJxQLABAEFAwCDgI4BgwDCgMpBAQLBQ0GAgAAAAAAAAAnCwMrBBAFCwEIES8CMAEAACsKCwALAQsCCwMLBAsFCwYRMQECMQEAADcyDgMRIxQMBwsACwcRMgwIDggMCQoJCwELAgsDCwQLBQsGETMKCRE0CgkRNQoJETYKCRE3DAoOCgwAEgQMDgsACw4tBAoJETgSAjgMCgk4DQwPCg8SBQoPEgALDxIDCwkROwI8AQAABAMLACkEAj0BAAAEBQsACwERBSkEAj4BAAA7Ng4AOAYMAQoAEQcLAAcAOA4MAwwECwMEFgoEDAUHAhFADAYLBQsGIQwDBRgJDAMLAwQeBwMRQAwEBR4HAAoEEQUKASEEJgsEAgcBEUAMBw4BOA8MCA4IEUINBwwJDAQLCQsEEUMLBwJEAQABBCcUCwAQBBQMAg4COAYMAwoDKQQECwUNBgIAAAAAAAAAJwsDKwQQBQsBCREvAgAABAEFAAQAAwAEAgBWAFcA",
}

modules := make([]vmtypes.Module, len(codes))
for i, code := range codes {
codeBz, err := base64.StdEncoding.DecodeString(code)
if err != nil {
return nil, errors.Wrap(err, "failed to decode module code")
}

modules[i] = vmtypes.NewModule(codeBz)
}

err := app.MoveKeeper.PublishModuleBundle(ctx, vmtypes.StdAddress, vmtypes.NewModuleBundle(modules...), movetypes.UpgradePolicy_COMPATIBLE)
if err != nil {
return nil, errors.Wrap(err, "failed to publish module bundle")
}

return vm, nil
},
)
}
2 changes: 1 addition & 1 deletion cmd/initiad/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig, b
)

// add move commands
rootCmd.AddCommand(movecmd.MoveCommand(encodingConfig.InterfaceRegistry.SigningContext().AddressCodec()))
rootCmd.AddCommand(movecmd.MoveCommand(encodingConfig.InterfaceRegistry.SigningContext().AddressCodec(), false))
}

func addModuleInitFlags(startCmd *cobra.Command) {
Expand Down
19 changes: 15 additions & 4 deletions cmd/move/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path"
"runtime/debug"
"strings"

"cosmossdk.io/core/address"
Expand Down Expand Up @@ -85,7 +86,7 @@ const (
defaultInstallDir = "."
)

func MoveCommand(ac address.Codec) *cobra.Command {
func MoveCommand(ac address.Codec, useMinlib bool) *cobra.Command {
cmd := &cobra.Command{
Use: "move",
Short: "move subcommands",
Expand All @@ -98,7 +99,7 @@ func MoveCommand(ac address.Codec) *cobra.Command {
moveBuildCmd(),
moveTestCmd(),
moveEncodeCmd(ac),
moveNewCmd(),
moveNewCmd(useMinlib),
moveCleanCmd(),
moveDeployCmd(ac),
moveVerifyCmd(),
Expand Down Expand Up @@ -307,7 +308,7 @@ func moveCoverageBytecodeCmd() *cobra.Command {
return cmd
}

func moveNewCmd() *cobra.Command {
func moveNewCmd(useMinlib bool) *cobra.Command {
cmd := &cobra.Command{
Use: "new <NAME>",
Short: "create a new move package",
Expand All @@ -319,7 +320,17 @@ func moveNewCmd() *cobra.Command {
return err
}

_, err = api.CreateContractPackage(*arg, args[0])
movevmVersion := ""
info, ok := debug.ReadBuildInfo()
if ok {
for _, dep := range info.Deps {
if dep.Path == "github.com/initia-labs/movevm" {
movevmVersion = dep.Version
}
}
}

_, err = api.CreateContractPackage(*arg, args[0], movevmVersion, useMinlib)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ require (
github.com/gorilla/mux v1.8.1
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/hashicorp/go-metrics v0.5.3
github.com/initia-labs/OPinit v0.5.2
github.com/initia-labs/OPinit v0.5.3
// we also need to update `LIBMOVEVM_VERSION` of Dockerfile#9
github.com/initia-labs/movevm v0.4.12
github.com/initia-labs/movevm v0.5.0
github.com/noble-assets/forwarding/v2 v2.0.0-20240521090705-86712c4c9e43
github.com/pelletier/go-toml v1.9.5
github.com/pkg/errors v0.9.1
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -726,14 +726,14 @@ github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANyt
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
github.com/initia-labs/OPinit v0.5.2 h1:KEiC72KGBa+X5DDMRrzVJCqNEGi7KX5cOYH/zAoDaQk=
github.com/initia-labs/OPinit v0.5.2/go.mod h1:lx1amLMszculwPu8ln+btJno38UV28fd2nP7XC88ZeE=
github.com/initia-labs/OPinit v0.5.3 h1:DytQyPXs68L4yrBFeHz1TX4tiHsa4tqLTQBUMVgOx0o=
github.com/initia-labs/OPinit v0.5.3/go.mod h1:lx1amLMszculwPu8ln+btJno38UV28fd2nP7XC88ZeE=
github.com/initia-labs/OPinit/api v0.5.1 h1:zwyJf7HtKJCKvLJ1R9PjVfJO1L+d/jKoeFyT7WTLHFI=
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.4.12 h1:P30q4U2jUN/al3bX391ZkXr3t59owNTwr9qmC4d1S0Q=
github.com/initia-labs/movevm v0.4.12/go.mod h1:aUWdvFZPdULjJ2McQTE+mLnfnG3CLAz0TWJRFzFFUwg=
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
Binary file modified x/move/keeper/binaries/BasicCoin.mv
Binary file not shown.
Binary file modified x/move/keeper/binaries/Vesting.mv
Binary file not shown.
Binary file added x/move/keeper/binaries/submsg.mv
Binary file not shown.
6 changes: 6 additions & 0 deletions x/move/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ var basicCoinMintScript []byte
var tableGeneratorModule []byte
var testAddressModule []byte
var vestingModule []byte
var submsgModule []byte

func init() {
basicCoinModule = ReadMoveFile("BasicCoin")
Expand All @@ -495,6 +496,7 @@ func init() {
tableGeneratorModule = ReadMoveFile("TableGenerator")
testAddressModule = ReadMoveFile("TestAddress")
vestingModule = ReadMoveFile("Vesting")
submsgModule = ReadMoveFile("submsg")

basicCoinMintScript = ReadScriptFile("main")
}
Expand Down Expand Up @@ -558,6 +560,10 @@ func (router TestMsgRouter) HandlerByTypeURL(typeURL string) baseapp.MsgServiceH
sdk.NewAttribute("args", strings.Join(msg.Args, ",")),
))

if msg.FunctionName == "fail" {
return nil, fmt.Errorf("fail")
}

return sdk.WrapServiceResult(ctx, &stakingtypes.MsgDelegateResponse{}, nil)
}
case sdk.MsgTypeURL(&movetypes.MsgScript{}):
Expand Down
49 changes: 49 additions & 0 deletions x/move/keeper/contracts/sources/Submsg.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module TestAccount::submsg {
use std::cosmos;
use std::string::String;
use std::event;
use std::signer;

public entry fun stargate(
account: &signer,
data: vector<u8>,
allow_failure: bool,
id: u64, // callback id
fid: String, // function id
) {
if (allow_failure) {
cosmos::stargate_with_options(account, data, cosmos::allow_failure_with_callback(id, fid));
} else {
cosmos::stargate_with_options(account, data, cosmos::disallow_failure_with_callback(id, fid));
}
}

#[event]
struct ResultEvent has drop {
id: u64,
success: bool,
}

#[event]
struct ResultEventWithSigner has drop {
account: address,
id: u64,
success: bool,
}

public entry fun callback_with_signer(
account: &signer,
id: u64,
success: bool,
) {
event::emit(ResultEventWithSigner { account: signer::address_of(account), id, success });

}

public entry fun callback_without_signer(
id: u64,
success: bool,
) {
event::emit(ResultEvent { id, success });
}
}
122 changes: 97 additions & 25 deletions x/move/keeper/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"strings"
"unsafe"

Expand Down Expand Up @@ -413,44 +414,115 @@ func (k Keeper) handleExecuteResponse(
func (k Keeper) DispatchMessages(ctx context.Context, messages []vmtypes.CosmosMessage) error {
sdkCtx := sdk.UnwrapSDKContext(ctx)
for _, message := range messages {
var msg proto.Message
var err error
if stargateMsg, ok := message.(*vmtypes.CosmosMessage__Stargate); ok {
msg, err = k.HandleVMStargateMsg(ctx, &stargateMsg.Value)
if err != nil {
return err
err := k.dispatchMessage(sdkCtx, message)
if err != nil {
return err
}
}

return nil
}

func (k Keeper) dispatchMessage(parentCtx sdk.Context, message vmtypes.CosmosMessage) (err error) {
var allowFailure bool
var callback *vmtypes.StargateCallback
var callbackSender vmtypes.AccountAddress

ctx, commit := parentCtx.CacheContext()
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic: %v", r)
}

success := err == nil

// create submsg event
event := sdk.NewEvent(
types.EventTypeSubmsg,
sdk.NewAttribute(types.AttributeKeySuccess, fmt.Sprintf("%v", success)),
)

if !success {
// return error if failed and not allowed to fail
if !allowFailure {
return
}

// emit failed reason event if failed and allowed to fail
event.AppendAttributes(sdk.NewAttribute(types.AttributeKeyReason, err.Error()))
} else {
msg, err = types.ConvertToSDKMessage(ctx, NewMoveBankKeeper(&k), NewNftKeeper(&k), message, k.ac, k.vc)
if err != nil {
return err
}
// commit if success
commit()
}

// validate msg
if msg, ok := msg.(sdk.HasValidateBasic); ok {
if err := msg.ValidateBasic(); err != nil {
return err
}
// reset error because it's allowed to fail
err = nil

// emit submessage event
parentCtx.EventManager().EmitEvent(event)

// if callback exists, execute it with parent context becuase it's already committed
if callback != nil {
err = k.ExecuteEntryFunctionJSON(
parentCtx,
callbackSender,
callback.ModuleAddress,
callback.ModuleName,
callback.FunctionName,
[]vmtypes.TypeTag{},
[]string{
fmt.Sprintf("\"%d\"", callback.Id),
fmt.Sprintf("%v", success),
},
)
}
}()

// find the handler
handler := k.msgRouter.Handler(msg)
if handler == nil {
return types.ErrNotSupportedCosmosMessage
var msg proto.Message
if stargateMsg, ok := message.(*vmtypes.CosmosMessage__Stargate); ok {
// validate basic & signer check is done in HandleVMStargateMsg
msg, err = k.HandleVMStargateMsg(ctx, &stargateMsg.Value)
if err != nil {
return
}

// and execute it
res, err := handler(sdkCtx, msg)
// callback only exists in stargate message
allowFailure = stargateMsg.Value.AllowFailure
callback = stargateMsg.Value.Callback
callbackSender = stargateMsg.Value.Sender
} else {
// signer check had been done in moveVM
msg, err = types.ConvertToSDKMessage(ctx, NewMoveBankKeeper(&k), NewNftKeeper(&k), message, k.ac, k.vc)
if err != nil {
return err
return
}

// conduct validate basic
if msg, ok := msg.(sdk.HasValidateBasic); ok {
err = msg.ValidateBasic()
if err != nil {
return
}
}
}

// emit events
sdkCtx.EventManager().EmitEvents(res.GetEvents())
// find the handler
handler := k.msgRouter.Handler(msg)
if handler == nil {
err = types.ErrNotSupportedCosmosMessage
return
}

return nil
// and execute it
res, err := handler(ctx, msg)
if err != nil {
return
}

// emit events
ctx.EventManager().EmitEvents(res.GetEvents())

return
}

// DistributeContractSharedRevenue distribute a portion of gas fee to contract creator account
Expand Down
Loading

0 comments on commit d14724b

Please sign in to comment.