cli: add support for nested arrays parameters

This commit is contained in:
Anna Shaleva 2020-08-26 12:08:17 +03:00
parent 5b30389d33
commit e4150da9ea
2 changed files with 377 additions and 22 deletions

View file

@ -73,7 +73,9 @@ func RuntimeNotify(args []interface{}) {
}` }`
// cosignersSeparator is a special value which is used to distinguish // cosignersSeparator is a special value which is used to distinguish
// parameters and cosigners for invoke* commands // parameters and cosigners for invoke* commands
cosignersSeparator = "--" cosignersSeparator = "--"
arrayStartSeparator = "["
arrayEndSeparator = "]"
) )
// NewCommands returns 'contract' command. // NewCommands returns 'contract' command.
@ -183,7 +185,9 @@ func NewCommands() []cli.Command {
specified explicitly or being inferred from the value. To specify the type specified explicitly or being inferred from the value. To specify the type
manually use "type:value" syntax where the type is one of the following: manually use "type:value" syntax where the type is one of the following:
'signature', 'bool', 'int', 'hash160', 'hash256', 'bytes', 'key' or 'string'. 'signature', 'bool', 'int', 'hash160', 'hash256', 'bytes', 'key' or 'string'.
Array types are not currently supported. Array types are also supported: use special space-separated '[' and ']'
symbols around array values to denote array bounds. Nested arrays are also
supported.
Given values are type-checked against given types with the following Given values are type-checked against given types with the following
restrictions applied: restrictions applied:
@ -233,6 +237,10 @@ func NewCommands() []cli.Command {
* 'string\:string' is a string with a value of 'string:string' * 'string\:string' is a string with a value of 'string:string'
* '03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c' is a * '03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c' is a
key with a value of '03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c' key with a value of '03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c'
* '[ a b c ]' is an array with strings values 'a', 'b' and 'c'
* '[ a b [ c d ] e ]' is an array with 4 values: string 'a', string 'b',
array of two strings 'c' and 'd', string 'e'
* '[ ]' is an empty array
Signers represent a set of Uint160 hashes with witness scopes and are used Signers represent a set of Uint160 hashes with witness scopes and are used
to verify hashes in System.Runtime.CheckWitness syscall. First signer is treated to verify hashes in System.Runtime.CheckWitness syscall. First signer is treated
@ -413,15 +421,15 @@ func invokeFunction(ctx *cli.Context) error {
func invokeInternal(ctx *cli.Context, signAndPush bool) error { func invokeInternal(ctx *cli.Context, signAndPush bool) error {
var ( var (
err error err error
gas util.Fixed8 gas util.Fixed8
operation string operation string
params = make([]smartcontract.Parameter, 0) params = make([]smartcontract.Parameter, 0)
paramsStart = 1 paramsStart = 1
cosigners []transaction.Signer cosigners []transaction.Signer
cosignersStart = 0 cosignersOffset = 0
resp *result.Invoke resp *result.Invoke
acc *wallet.Account acc *wallet.Account
) )
args := ctx.Args() args := ctx.Args()
@ -439,20 +447,14 @@ func invokeInternal(ctx *cli.Context, signAndPush bool) error {
paramsStart++ paramsStart++
if len(args) > paramsStart { if len(args) > paramsStart {
for k, s := range args[paramsStart:] { cosignersOffset, params, err = parseParams(args[paramsStart:], true)
if s == cosignersSeparator { if err != nil {
cosignersStart = paramsStart + k + 1 return cli.NewExitError(err, 1)
break
}
param, err := smartcontract.NewParameterFromString(s)
if err != nil {
return cli.NewExitError(fmt.Errorf("failed to parse argument #%d: %w", k+paramsStart+1, err), 1)
}
params = append(params, *param)
} }
} }
if len(args) >= cosignersStart && cosignersStart > 0 { cosignersStart := paramsStart + cosignersOffset
if len(args) > cosignersStart {
for i, c := range args[cosignersStart:] { for i, c := range args[cosignersStart:] {
cosigner, err := parseCosigner(c) cosigner, err := parseCosigner(c)
if err != nil { if err != nil {
@ -506,6 +508,52 @@ func invokeInternal(ctx *cli.Context, signAndPush bool) error {
return nil return nil
} }
// parseParams extracts array of smartcontract.Parameter from the given args and
// returns the number of handled words, the array itself and an error.
// `calledFromMain` denotes whether the method was called from the outside or
// recursively and used to check if cosignersSeparator and closing bracket are
// allowed to be in `args` sequence.
func parseParams(args []string, calledFromMain bool) (int, []smartcontract.Parameter, error) {
res := []smartcontract.Parameter{}
for k := 0; k < len(args); {
s := args[k]
switch s {
case cosignersSeparator:
if calledFromMain {
return k + 1, res, nil // `1` to convert index to numWordsRead
}
return 0, []smartcontract.Parameter{}, errors.New("invalid array syntax: missing closing bracket")
case arrayStartSeparator:
numWordsRead, array, err := parseParams(args[k+1:], false)
if err != nil {
return 0, nil, fmt.Errorf("failed to parse array: %w", err)
}
res = append(res, smartcontract.Parameter{
Type: smartcontract.ArrayType,
Value: array,
})
k += 1 + numWordsRead // `1` for opening bracket
case arrayEndSeparator:
if calledFromMain {
return 0, nil, errors.New("invalid array syntax: missing opening bracket")
}
return k + 1, res, nil // `1`to convert index to numWordsRead
default:
param, err := smartcontract.NewParameterFromString(s)
if err != nil {
return 0, nil, fmt.Errorf("failed to parse argument #%d: %w", k+1, err)
}
res = append(res, *param)
k++
}
}
if calledFromMain {
return len(args), res, nil
}
return 0, []smartcontract.Parameter{}, errors.New("invalid array syntax: missing closing bracket")
}
func testInvokeScript(ctx *cli.Context) error { func testInvokeScript(ctx *cli.Context) error {
src := ctx.String("in") src := ctx.String("in")
if len(src) == 0 { if len(src) == 0 {

View file

@ -4,6 +4,7 @@ import (
"flag" "flag"
"io/ioutil" "io/ioutil"
"os" "os"
"strings"
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/core/transaction" "github.com/nspcc-dev/neo-go/pkg/core/transaction"
@ -121,3 +122,309 @@ func TestParseCosigner(t *testing.T) {
require.Error(t, err) require.Error(t, err)
} }
} }
func TestParseParams_CalledFromItself(t *testing.T) {
testCases := map[string]struct {
WordsRead int
Value []smartcontract.Parameter
}{
"]": {
WordsRead: 1,
Value: []smartcontract.Parameter{},
},
"[ [ ] ] ]": {
WordsRead: 5,
Value: []smartcontract.Parameter{
{
Type: smartcontract.ArrayType,
Value: []smartcontract.Parameter{
{
Type: smartcontract.ArrayType,
Value: []smartcontract.Parameter{},
},
},
},
},
},
"a b c ]": {
WordsRead: 4,
Value: []smartcontract.Parameter{
{
Type: smartcontract.StringType,
Value: "a",
},
{
Type: smartcontract.StringType,
Value: "b",
},
{
Type: smartcontract.StringType,
Value: "c",
},
},
},
"a [ b [ [ c d ] e ] ] f ] extra items": {
WordsRead: 13, // the method should return right after the last bracket, as calledFromMain == false
Value: []smartcontract.Parameter{
{
Type: smartcontract.StringType,
Value: "a",
},
{
Type: smartcontract.ArrayType,
Value: []smartcontract.Parameter{
{
Type: smartcontract.StringType,
Value: "b",
},
{
Type: smartcontract.ArrayType,
Value: []smartcontract.Parameter{
{
Type: smartcontract.ArrayType,
Value: []smartcontract.Parameter{
{
Type: smartcontract.StringType,
Value: "c",
},
{
Type: smartcontract.StringType,
Value: "d",
},
},
},
{
Type: smartcontract.StringType,
Value: "e",
},
},
},
},
},
{
Type: smartcontract.StringType,
Value: "f",
},
},
},
}
for str, expected := range testCases {
input := strings.Split(str, " ")
offset, actual, err := parseParams(input, false)
require.NoError(t, err)
require.Equal(t, expected.WordsRead, offset)
require.Equal(t, expected.Value, actual)
}
errorCases := []string{
"[ ]",
"[ a b [ c ] d ]",
"[ ] --",
"--",
"not-int:integer ]",
}
for _, str := range errorCases {
input := strings.Split(str, " ")
_, _, err := parseParams(input, false)
require.Error(t, err)
}
}
func TestParseParams_CalledFromOutside(t *testing.T) {
testCases := map[string]struct {
WordsRead int
Parameters []smartcontract.Parameter
}{
"-- cosigner1": {
WordsRead: 1, // the `--` only
Parameters: []smartcontract.Parameter{},
},
"a b c": {
WordsRead: 3,
Parameters: []smartcontract.Parameter{
{
Type: smartcontract.StringType,
Value: "a",
},
{
Type: smartcontract.StringType,
Value: "b",
},
{
Type: smartcontract.StringType,
Value: "c",
},
},
},
"a b c -- cosigner1": {
WordsRead: 4,
Parameters: []smartcontract.Parameter{
{
Type: smartcontract.StringType,
Value: "a",
},
{
Type: smartcontract.StringType,
Value: "b",
},
{
Type: smartcontract.StringType,
Value: "c",
},
},
},
"a [ b [ [ c d ] e ] ] f": {
WordsRead: 12,
Parameters: []smartcontract.Parameter{
{
Type: smartcontract.StringType,
Value: "a",
},
{
Type: smartcontract.ArrayType,
Value: []smartcontract.Parameter{
{
Type: smartcontract.StringType,
Value: "b",
},
{
Type: smartcontract.ArrayType,
Value: []smartcontract.Parameter{
{
Type: smartcontract.ArrayType,
Value: []smartcontract.Parameter{
{
Type: smartcontract.StringType,
Value: "c",
},
{
Type: smartcontract.StringType,
Value: "d",
},
},
},
{
Type: smartcontract.StringType,
Value: "e",
},
},
},
},
},
{
Type: smartcontract.StringType,
Value: "f",
},
},
},
"a [ b ] -- cosigner1 cosigner2": {
WordsRead: 5,
Parameters: []smartcontract.Parameter{
{
Type: smartcontract.StringType,
Value: "a",
},
{
Type: smartcontract.ArrayType,
Value: []smartcontract.Parameter{
{
Type: smartcontract.StringType,
Value: "b",
},
},
},
},
},
"a [ b ]": {
WordsRead: 4,
Parameters: []smartcontract.Parameter{
{
Type: smartcontract.StringType,
Value: "a",
},
{
Type: smartcontract.ArrayType,
Value: []smartcontract.Parameter{
{
Type: smartcontract.StringType,
Value: "b",
},
},
},
},
},
"a [ b ] [ [ c ] ] [ [ [ d ] ] ]": {
WordsRead: 16,
Parameters: []smartcontract.Parameter{
{
Type: smartcontract.StringType,
Value: "a",
},
{
Type: smartcontract.ArrayType,
Value: []smartcontract.Parameter{
{
Type: smartcontract.StringType,
Value: "b",
},
},
},
{
Type: smartcontract.ArrayType,
Value: []smartcontract.Parameter{
{
Type: smartcontract.ArrayType,
Value: []smartcontract.Parameter{
{
Type: smartcontract.StringType,
Value: "c",
},
},
},
},
},
{
Type: smartcontract.ArrayType,
Value: []smartcontract.Parameter{
{
Type: smartcontract.ArrayType,
Value: []smartcontract.Parameter{
{
Type: smartcontract.ArrayType,
Value: []smartcontract.Parameter{
{
Type: smartcontract.StringType,
Value: "d",
},
},
},
},
},
},
},
},
},
}
for str, expected := range testCases {
input := strings.Split(str, " ")
offset, arr, err := parseParams(input, true)
require.NoError(t, err)
require.Equal(t, expected.WordsRead, offset)
require.Equal(t, expected.Parameters, arr)
}
errorCases := []string{
"[",
"]",
"[ [ ]",
"[ [ ] --",
"[ -- ]",
}
for _, str := range errorCases {
input := strings.Split(str, " ")
_, _, err := parseParams(input, true)
require.Error(t, err)
}
}