rpc/request: handle bool parameters correctly

This commit is contained in:
Evgeniy Stratonikov 2021-05-25 11:24:28 +03:00
parent 399584504e
commit c8ddc790d9
3 changed files with 9 additions and 12 deletions

View file

@ -70,17 +70,14 @@ func ExpandArrayIntoScript(script *io.BinWriter, slice []Param) error {
} }
emit.Int(script, int64(val)) emit.Int(script, int64(val))
case smartcontract.BoolType: case smartcontract.BoolType:
str, err := fp.Value.GetString() val, ok := fp.Value.Value.(bool)
if err != nil { if !ok {
return err return errors.New("not a bool")
} }
switch str { if val {
case "true":
emit.Int(script, 1) emit.Int(script, 1)
case "false": } else {
emit.Int(script, 0) emit.Int(script, 0)
default:
return errors.New("wrong boolean value")
} }
case smartcontract.ArrayType: case smartcontract.ArrayType:
val, err := fp.Value.GetArray() val, err := fp.Value.GetArray()

View file

@ -54,10 +54,10 @@ func TestInvocationScriptCreationGood(t *testing.T) {
ps: Params{{Type: StringT, Value: "a"}, {Type: ArrayT, Value: []Param{{Type: FuncParamT, Value: FuncParam{Type: smartcontract.IntegerType, Value: Param{Type: NumberT, Value: 42}}}}}}, ps: Params{{Type: StringT, Value: "a"}, {Type: ArrayT, Value: []Param{{Type: FuncParamT, Value: FuncParam{Type: smartcontract.IntegerType, Value: Param{Type: NumberT, Value: 42}}}}}},
script: "002a11c01f0c01610c146f459162ceeb248b071ec157d9e4f6fd26fdbe5041627d5b52", script: "002a11c01f0c01610c146f459162ceeb248b071ec157d9e4f6fd26fdbe5041627d5b52",
}, { }, {
ps: Params{{Type: StringT, Value: "a"}, {Type: ArrayT, Value: []Param{{Type: FuncParamT, Value: FuncParam{Type: smartcontract.BoolType, Value: Param{Type: StringT, Value: "true"}}}}}}, ps: Params{{Type: StringT, Value: "a"}, {Type: ArrayT, Value: []Param{{Type: FuncParamT, Value: FuncParam{Type: smartcontract.BoolType, Value: Param{Type: BooleanT, Value: true}}}}}},
script: "1111c01f0c01610c146f459162ceeb248b071ec157d9e4f6fd26fdbe5041627d5b52", script: "1111c01f0c01610c146f459162ceeb248b071ec157d9e4f6fd26fdbe5041627d5b52",
}, { }, {
ps: Params{{Type: StringT, Value: "a"}, {Type: ArrayT, Value: []Param{{Type: FuncParamT, Value: FuncParam{Type: smartcontract.BoolType, Value: Param{Type: StringT, Value: "false"}}}}}}, ps: Params{{Type: StringT, Value: "a"}, {Type: ArrayT, Value: []Param{{Type: FuncParamT, Value: FuncParam{Type: smartcontract.BoolType, Value: Param{Type: BooleanT, Value: false}}}}}},
script: "1011c01f0c01610c146f459162ceeb248b071ec157d9e4f6fd26fdbe5041627d5b52", script: "1011c01f0c01610c146f459162ceeb248b071ec157d9e4f6fd26fdbe5041627d5b52",
}} }}
for _, ps := range paramScripts { for _, ps := range paramScripts {

View file

@ -859,7 +859,7 @@ var rpcTestCases = map[string][]rpcTestCase{
}, },
{ {
name: "positive, with arguments, result=true", name: "positive, with arguments, result=true",
params: fmt.Sprintf(`["%s", [{"type": "String", "value": "good_string"}, {"type": "Integer", "value": "4"}, {"type":"Boolean", "value": "false"}]]`, verifyWithArgsContractHash), params: fmt.Sprintf(`["%s", [{"type": "String", "value": "good_string"}, {"type": "Integer", "value": "4"}, {"type":"Boolean", "value": false}]]`, verifyWithArgsContractHash),
result: func(e *executor) interface{} { return &result.Invoke{} }, result: func(e *executor) interface{} { return &result.Invoke{} },
check: func(t *testing.T, e *executor, inv interface{}) { check: func(t *testing.T, e *executor, inv interface{}) {
res, ok := inv.(*result.Invoke) res, ok := inv.(*result.Invoke)
@ -877,7 +877,7 @@ var rpcTestCases = map[string][]rpcTestCase{
}, },
{ {
name: "positive, with arguments, result=false", name: "positive, with arguments, result=false",
params: fmt.Sprintf(`["%s", [{"type": "String", "value": "invalid_string"}, {"type": "Integer", "value": "4"}, {"type":"Boolean", "value": "false"}]]`, verifyWithArgsContractHash), params: fmt.Sprintf(`["%s", [{"type": "String", "value": "invalid_string"}, {"type": "Integer", "value": "4"}, {"type":"Boolean", "value": false}]]`, verifyWithArgsContractHash),
result: func(e *executor) interface{} { return &result.Invoke{} }, result: func(e *executor) interface{} { return &result.Invoke{} },
check: func(t *testing.T, e *executor, inv interface{}) { check: func(t *testing.T, e *executor, inv interface{}) {
res, ok := inv.(*result.Invoke) res, ok := inv.(*result.Invoke)