mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-27 03:58:06 +00:00
Merge pull request #1363 from nspcc-dev/cli/pass_arrays
cli: add support for nested arrays
This commit is contained in:
commit
ca2e8ed528
2 changed files with 377 additions and 22 deletions
|
@ -74,6 +74,8 @@ func RuntimeNotify(args []interface{}) {
|
|||
// cosignersSeparator is a special value which is used to distinguish
|
||||
// parameters and cosigners for invoke* commands
|
||||
cosignersSeparator = "--"
|
||||
arrayStartSeparator = "["
|
||||
arrayEndSeparator = "]"
|
||||
)
|
||||
|
||||
// NewCommands returns 'contract' command.
|
||||
|
@ -183,7 +185,9 @@ func NewCommands() []cli.Command {
|
|||
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:
|
||||
'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
|
||||
restrictions applied:
|
||||
|
@ -233,6 +237,10 @@ func NewCommands() []cli.Command {
|
|||
* 'string\:string' is a string with a value of 'string:string'
|
||||
* '03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c' is a
|
||||
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
|
||||
to verify hashes in System.Runtime.CheckWitness syscall. First signer is treated
|
||||
|
@ -419,7 +427,7 @@ func invokeInternal(ctx *cli.Context, signAndPush bool) error {
|
|||
params = make([]smartcontract.Parameter, 0)
|
||||
paramsStart = 1
|
||||
cosigners []transaction.Signer
|
||||
cosignersStart = 0
|
||||
cosignersOffset = 0
|
||||
resp *result.Invoke
|
||||
acc *wallet.Account
|
||||
)
|
||||
|
@ -439,20 +447,14 @@ func invokeInternal(ctx *cli.Context, signAndPush bool) error {
|
|||
paramsStart++
|
||||
|
||||
if len(args) > paramsStart {
|
||||
for k, s := range args[paramsStart:] {
|
||||
if s == cosignersSeparator {
|
||||
cosignersStart = paramsStart + k + 1
|
||||
break
|
||||
}
|
||||
param, err := smartcontract.NewParameterFromString(s)
|
||||
cosignersOffset, params, err = parseParams(args[paramsStart:], true)
|
||||
if err != nil {
|
||||
return cli.NewExitError(fmt.Errorf("failed to parse argument #%d: %w", k+paramsStart+1, err), 1)
|
||||
}
|
||||
params = append(params, *param)
|
||||
return cli.NewExitError(err, 1)
|
||||
}
|
||||
}
|
||||
|
||||
if len(args) >= cosignersStart && cosignersStart > 0 {
|
||||
cosignersStart := paramsStart + cosignersOffset
|
||||
if len(args) > cosignersStart {
|
||||
for i, c := range args[cosignersStart:] {
|
||||
cosigner, err := parseCosigner(c)
|
||||
if err != nil {
|
||||
|
@ -506,6 +508,52 @@ func invokeInternal(ctx *cli.Context, signAndPush bool) error {
|
|||
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 {
|
||||
src := ctx.String("in")
|
||||
if len(src) == 0 {
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"flag"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
||||
|
@ -121,3 +122,309 @@ func TestParseCosigner(t *testing.T) {
|
|||
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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue