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

fix: move-cli bugs #7

Merged
merged 5 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
66 changes: 51 additions & 15 deletions x/move/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/spf13/cobra"

"github.com/initia-labs/initia/x/move/types"
vmapi "github.com/initia-labs/initiavm/api"
)

func GetQueryCmd() *cobra.Command {
Expand All @@ -30,6 +31,7 @@ func GetQueryCmd() *cobra.Command {
GetCmdTableEntry(),
GetCmdTableEntries(),
GetCmdQueryEntryFunction(),
GetCmdQueryParams(),
)
return queryCmd
}
Expand Down Expand Up @@ -124,7 +126,8 @@ func GetCmdResource() *cobra.Command {
return err
}

if err := sdk.ValidateDenom(args[1]); err != nil {
_, err = vmapi.ParseStructTag(args[1])
if err != nil {
return err
}

Expand Down Expand Up @@ -276,6 +279,7 @@ func GetCmdQueryEntryFunction() *cobra.Command {
Get an entry function execution result

Supported types : u8, u16, u32, u64, u128, u256, bool, string, address, raw, vector<inner_type>

Example of args: address:0x1 bool:true u8:0 string:hello vector<u32>:a,b,c,d

Example:
Expand All @@ -284,7 +288,7 @@ $ %s query move execute \
BasicCoin \
getBalance \
--type-args '0x1::native_uinit::Coin 0x1::native_uusdc::Coin' \
--args 'u8:0 address:0x1'
--args 'u8:0 address:0x1 string:"hello world"'
`, version.AppName, bech32PrefixAccAddr,
),
),
Expand All @@ -310,29 +314,25 @@ $ %s query move execute \
typeArgs = strings.Split(flagTypeArgs, " ")
}

var flagArgsList []string
flagArgs, err := cmd.Flags().GetString(FlagArgs)
if err != nil {
return err
}
if flagArgs != "" {
flagArgsList = strings.Split(flagArgs, " ")
}

bcsArgs := make([][]byte, len(flagArgsList))
for i, arg := range flagArgsList {
argSplit := strings.Split(arg, ":")
if len(argSplit) != 2 {
return fmt.Errorf("invalid argument format: %s", arg)
}
argTypes, args := parseArguments(flagArgs)
if len(argTypes) != len(args) {
return fmt.Errorf("invalid argument format len(types) != len(args)")
}

serializer := NewSerializer()
bcsArg, err := BcsSerializeArg(argSplit[0], argSplit[1], serializer)
serializer := NewSerializer()
bcsArgs := [][]byte{}
for i := range argTypes {
bcsArg, err := BcsSerializeArg(argTypes[i], args[i], serializer)
if err != nil {
return err
}

bcsArgs[i] = bcsArg
bcsArgs = append(bcsArgs, bcsArg)
}

queryClient := types.NewQueryClient(clientCtx)
Expand All @@ -358,3 +358,39 @@ $ %s query move execute \
flags.AddQueryFlagsToCmd(cmd)
return cmd
}

// GetCmdQueryParams implements the params query command.
func GetCmdQueryParams() *cobra.Command {
cmd := &cobra.Command{
Use: "params",
Args: cobra.NoArgs,
Short: "Query the current move parameters information",
Long: strings.TrimSpace(
fmt.Sprintf(`Query values set as move parameters.

Example:
$ %s query move params
`,
version.AppName,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

res, err := queryClient.Params(cmd.Context(), &types.QueryParamsRequest{})
if err != nil {
return err
}

return clientCtx.PrintProto(&res.Params)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
24 changes: 10 additions & 14 deletions x/move/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ $ %s tx move execute \
BasicCoin \
getBalance \
--type-args '0x1::native_uinit::Coin 0x1::native_uusdc::Coin' \
--args 'u8:0 address:0x1'
--args 'u8:0 address:0x1 string:"hello world"'
`, version.AppName, bech32PrefixAccAddr,
),
),
Expand All @@ -142,29 +142,25 @@ $ %s tx move execute \
typeArgs = strings.Split(flagTypeArgs, " ")
}

var flagArgsList []string
flagArgs, err := cmd.Flags().GetString(FlagArgs)
if err != nil {
return err
}
if flagArgs != "" {
flagArgsList = strings.Split(flagArgs, " ")
}

bcsArgs := make([][]byte, len(flagArgsList))
for i, arg := range flagArgsList {
argSplit := strings.Split(arg, ":")
if len(argSplit) != 2 {
return fmt.Errorf("invalid argument format: %s", arg)
}
argTypes, args := parseArguments(flagArgs)
if len(argTypes) != len(args) {
return fmt.Errorf("invalid argument format len(types) != len(args)")
}

serializer := NewSerializer()
bcsArg, err := BcsSerializeArg(argSplit[0], argSplit[1], serializer)
serializer := NewSerializer()
bcsArgs := [][]byte{}
for i := range argTypes {
bcsArg, err := BcsSerializeArg(argTypes[i], args[i], serializer)
if err != nil {
return err
}

bcsArgs[i] = bcsArg
bcsArgs = append(bcsArgs, bcsArg)
}

msg := types.MsgExecute{
Expand Down
57 changes: 57 additions & 0 deletions x/move/client/cli/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,60 @@ func DivideUint256String(s string) (uint64, uint64, uint64, uint64, error) {
highHigh := n.Rsh(n, 64).Uint64()
return highHigh, highLow, high, low, nil
}

func parseArguments(s string) (tt []string, args []string) {
cursor := 0

var t, a string
var typeParsing, quoteParsing bool

typeParsing = true
for len(s) > cursor {
c := s[cursor]
if c == ':' {
typeParsing = false

cursor++
continue
} else if quoteParsing {
if c == '"' {
quoteParsing = false

cursor++
continue
}
} else {
if c == ' ' {
typeParsing = true

tt = append(tt, t)
args = append(args, a)

t = ""
a = ""

cursor++
continue
} else if c == '"' {
typeParsing = false
quoteParsing = true

cursor++
continue
}
}

if typeParsing {
t += string(c)
} else {
a += string(c)
}

cursor++
}

tt = append(tt, t)
args = append(args, a)

return
}
7 changes: 7 additions & 0 deletions x/move/client/cli/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import (
"github.com/stretchr/testify/require"
)

func Test_parseArguments(t *testing.T) {
argTypes, args := parseArguments("u8:1 u16:256 string:\"hello world\" string:\"hello\" vector<string>:\"hello world\",\"hello world\" vector<bool>:true,false,true")
require.Equal(t, []string{"u8", "u16", "string", "string", "vector<string>", "vector<bool>"}, argTypes)
require.Equal(t, []string{"1", "256", "hello world", "hello", "hello world,hello world", "true,false,true"}, args)
}

func Test_BcsSerializeArg(t *testing.T) {

testCases := []struct {
Expand Down Expand Up @@ -55,6 +61,7 @@ func Test_BcsSerializeArg(t *testing.T) {
{"vector<u16>", "65536,65536,-1", nil, true, "vector<u16> 65536,65536,-1"},
{"vector<address>", "0x1,0x2,0x3", []byte{0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3}, false, "vector<address> 0x1,0x2,0x3"},
{"vector<string>", "hello,world", []byte{0x2, 0x5, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x5, 0x77, 0x6f, 0x72, 0x6c, 0x64}, false, "vector<string> hello,world"},
{"vector<string>", "hello world,hello world", []byte{0x2, 0xb, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0xb, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64}, false, "vector<string> \"hello world,hello world\""},
{"vector<string>", "", []byte{0}, false, "vector<string> empty"},
{"vector<u128>", "0", []byte{0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, false, "vector<u128> 0"},
{"vector<u128>", "1", []byte{0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, false, "vector<u128> 1"},
Expand Down
Loading