2022-07-07 14:41:01 +00:00
|
|
|
package params
|
2018-12-21 09:32:18 +00:00
|
|
|
|
|
|
|
import (
|
2019-11-26 10:13:17 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-11-20 13:07:43 +00:00
|
|
|
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2020-12-29 10:44:07 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
2018-12-21 09:32:18 +00:00
|
|
|
)
|
|
|
|
|
2021-03-10 14:43:52 +00:00
|
|
|
// ExpandArrayIntoScript pushes all FuncParam parameters from the given array
|
2022-04-20 18:30:09 +00:00
|
|
|
// into the given buffer in the reverse order.
|
2021-03-10 14:43:52 +00:00
|
|
|
func ExpandArrayIntoScript(script *io.BinWriter, slice []Param) error {
|
2019-11-28 16:08:31 +00:00
|
|
|
for j := len(slice) - 1; j >= 0; j-- {
|
|
|
|
fp, err := slice[j].GetFuncParam()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch fp.Type {
|
2020-07-07 19:35:03 +00:00
|
|
|
case smartcontract.ByteArrayType:
|
|
|
|
str, err := fp.Value.GetBytesBase64()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
emit.Bytes(script, str)
|
|
|
|
case smartcontract.SignatureType:
|
rpc: fix Signature parameter unmarshalling
It is based64 encoded and decoded, see
https://github.com/nspcc-dev/neo-go/blob/5108d1c2c748ad00de2c0b37c05f41e841d770c9/pkg/smartcontract/parameter.go#L78
and
https://github.com/neo-project/neo/blob/26d04a642ac5a1dd1827dabf5602767e0acba25c/src/neo/SmartContract/ContractParameter.cs#L150
and
https://github.com/neo-project/neo/blob/26d04a642ac5a1dd1827dabf5602767e0acba25c/src/neo/SmartContract/ContractParameter.cs#L79.
Also, TestInvocationScriptCreationGood is extended and compatibility
is tested with the following C# node requests:
```
anna@kiwi:~/Documents/GitProjects/nspcc-dev/neo-go$ curl -d '{ "jsonrpc": "2.0", "id": 1, "method": "invokefunction", "params": ["50befd26fdf6e4d957c11e078b24ebce6291456f", "a", [{"type": "Signature", "value": "4edf5005771de04619235d5a4c7a9a11bb78e008541f1da7725f654c33380a3c87e2959a025da706d7255cb3a3fa07ebe9c6559d0d9e6213c68049168eb1056f"}]] }' seed1.neo.org:10332 | json_pp
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 665 0 381 100 284 197 147 0:00:01 0:00:01 --:--:-- 344
{
"jsonrpc" : "2.0",
"id" : 1,
"result" : {
"notifications" : [],
"stack" : [],
"script" : "DGDh51/nTTnvvV17TjrX3bfl3lrhztr1rXVtvvx7TTznjV/V1rvvbl/rnhzfffzRrdzzt7b3n1rTbl1rvTp3vbnlxvdrd9rTt5t71zrnn13R317rbXdzrzTj3Xrx5vXTnp8RwB8MAWEMFG9FkWLO6ySLBx7BV9nk9v0m/b5QQWJ9W1I=",
"exception" : "Called Contract Does Not Exist: 0x50befd26fdf6e4d957c11e078b24ebce6291456f",
"state" : "FAULT",
"gasconsumed" : "104526"
}
}
anna@kiwi:~/Documents/GitProjects/nspcc-dev/neo-go$ curl -d '{ "jsonrpc": "2.0", "id": 1, "method": "invokefunction", "params": ["50befd26fdf6e4d957c11e078b24ebce6291456f", "a", [{"type": "Signature", "value": "Tt9QBXcd4EYZI11aTHqaEbt44AhUHx2ncl9lTDM4CjyH4pWaAl2nBtclXLOj+gfr6cZVnQ2eYhPGgEkWjrEFbw=="}]] }' seed1.neo.org:10332 | json_pp
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 591 0 347 100 244 794 558 --:--:-- --:--:-- --:--:-- 1352
{
"jsonrpc" : "2.0",
"result" : {
"state" : "FAULT",
"exception" : "Called Contract Does Not Exist: 0x50befd26fdf6e4d957c11e078b24ebce6291456f",
"gasconsumed" : "104526",
"notifications" : [],
"stack" : [],
"script" : "DEBO31AFdx3gRhkjXVpMepoRu3jgCFQfHadyX2VMMzgKPIfilZoCXacG1yVcs6P6B+vpxlWdDZ5iE8aASRaOsQVvEcAfDAFhDBRvRZFizuskiwcewVfZ5Pb9Jv2+UEFifVtS"
},
"id" : 1
}
```
2022-06-22 09:57:19 +00:00
|
|
|
str, err := fp.Value.GetBytesBase64()
|
2019-11-28 16:08:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-03 14:46:51 +00:00
|
|
|
emit.Bytes(script, str)
|
2020-02-21 14:34:18 +00:00
|
|
|
case smartcontract.StringType:
|
2019-11-28 16:08:31 +00:00
|
|
|
str, err := fp.Value.GetString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-03 14:46:51 +00:00
|
|
|
emit.String(script, str)
|
2020-02-21 14:34:18 +00:00
|
|
|
case smartcontract.Hash160Type:
|
2019-11-28 16:08:31 +00:00
|
|
|
hash, err := fp.Value.GetUint160FromHex()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-03 14:46:51 +00:00
|
|
|
emit.Bytes(script, hash.BytesBE())
|
2020-02-21 14:34:18 +00:00
|
|
|
case smartcontract.Hash256Type:
|
2019-11-28 16:08:31 +00:00
|
|
|
hash, err := fp.Value.GetUint256()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-03 14:46:51 +00:00
|
|
|
emit.Bytes(script, hash.BytesBE())
|
2020-02-21 14:34:18 +00:00
|
|
|
case smartcontract.PublicKeyType:
|
2019-11-28 16:08:31 +00:00
|
|
|
str, err := fp.Value.GetString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
key, err := keys.NewPublicKeyFromString(string(str))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-03 14:46:51 +00:00
|
|
|
emit.Bytes(script, key.Bytes())
|
2020-02-21 14:34:18 +00:00
|
|
|
case smartcontract.IntegerType:
|
2022-02-09 12:13:15 +00:00
|
|
|
bi, err := fp.Value.GetBigInt()
|
2019-11-28 16:08:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-09 12:13:15 +00:00
|
|
|
emit.BigInt(script, bi)
|
2020-02-21 14:34:18 +00:00
|
|
|
case smartcontract.BoolType:
|
2021-10-28 11:10:18 +00:00
|
|
|
val, err := fp.Value.GetBoolean() // not GetBooleanStrict(), because that's the way C# code works
|
|
|
|
if err != nil {
|
2021-05-25 08:24:28 +00:00
|
|
|
return errors.New("not a bool")
|
2019-11-28 16:08:31 +00:00
|
|
|
}
|
2022-10-28 09:23:27 +00:00
|
|
|
emit.Bool(script, val)
|
2020-08-27 08:44:40 +00:00
|
|
|
case smartcontract.ArrayType:
|
|
|
|
val, err := fp.Value.GetArray()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
rpc: adjust script creation with empty parameters list
Always use NEWARRAY0 where possible, see
https://github.com/neo-project/neo/blob/26d04a642ac5a1dd1827dabf5602767e0acba25c/src/neo/VM/Helper.cs#L41.
Compatibility is tested:
```
anna@kiwi:~/Documents/GitProjects/nspcc-dev/neo-go$ curl -d '{ "jsonrpc": "2.0", "id": 1, "method": "invokefunction", "params": ["50befd26fdf6e4d957c11e078b24ebce6291456f", "a", [] ]}' seed1.neo.org:10332 | json_pp
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 370 0 248 100 122 127 62 0:00:01 0:00:01 --:--:-- 190
{
"result" : {
"notifications" : [],
"stack" : [],
"script" : "wh8MAWEMFG9FkWLO6ySLBx7BV9nk9v0m/b5QQWJ9W1I=",
"gasconsumed" : "98403",
"state" : "FAULT",
"exception" : "Called Contract Does Not Exist: 0x50befd26fdf6e4d957c11e078b24ebce6291456f"
},
"jsonrpc" : "2.0",
"id" : 1
}
```
2022-06-22 10:23:55 +00:00
|
|
|
err = ExpandArrayIntoScriptAndPack(script, val)
|
2020-08-27 08:44:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-09 07:37:22 +00:00
|
|
|
case smartcontract.AnyType:
|
rpc: emit Null in case of `Any` parameter with zero-len value
Otherwise it leads to the following error in the TestActor_CallWithNilParam:
```
=== RUN TestActor_CallWithNilParam
logger.go:130: 2023-04-03T15:58:27.672+0300 INFO initial gas supply is not set or wrong, setting default value {"InitialGASSupply": "52000000"}
logger.go:130: 2023-04-03T15:58:27.672+0300 INFO P2PNotaryRequestPayloadPool size is not set or wrong, setting default value {"P2PNotaryRequestPayloadPoolSize": 1000}
logger.go:130: 2023-04-03T15:58:27.672+0300 INFO MaxBlockSize is not set or wrong, setting default value {"MaxBlockSize": 262144}
logger.go:130: 2023-04-03T15:58:27.672+0300 INFO MaxBlockSystemFee is not set or wrong, setting default value {"MaxBlockSystemFee": 900000000000}
logger.go:130: 2023-04-03T15:58:27.672+0300 INFO MaxTransactionsPerBlock is not set or wrong, using default value {"MaxTransactionsPerBlock": 512}
logger.go:130: 2023-04-03T15:58:27.672+0300 INFO MaxValidUntilBlockIncrement is not set or wrong, using default value {"MaxValidUntilBlockIncrement": 5760}
logger.go:130: 2023-04-03T15:58:27.675+0300 INFO no storage version found! creating genesis block
logger.go:130: 2023-04-03T15:58:27.675+0300 INFO ExtensiblePoolSize is not set or wrong, using default value {"ExtensiblePoolSize": 20}
logger.go:130: 2023-04-03T15:58:27.675+0300 INFO SessionPoolSize is not set or wrong, setting default value {"SessionPoolSize": 20}
logger.go:130: 2023-04-03T15:58:27.675+0300 INFO MaxWebSocketClients is not set or wrong, setting default value {"MaxWebSocketClients": 64}
logger.go:130: 2023-04-03T15:58:27.675+0300 INFO starting rpc-server {"endpoint": "localhost:0"}
logger.go:130: 2023-04-03T15:58:27.677+0300 DEBUG done processing headers {"headerIndex": 1, "blockHeight": 0, "took": "436.313µs"}
logger.go:130: 2023-04-03T15:58:27.679+0300 DEBUG done processing headers {"headerIndex": 2, "blockHeight": 1, "took": "272.891µs"}
logger.go:130: 2023-04-03T15:58:27.680+0300 DEBUG done processing headers {"headerIndex": 3, "blockHeight": 2, "took": "276.949µs"}
logger.go:130: 2023-04-03T15:58:27.681+0300 DEBUG done processing headers {"headerIndex": 4, "blockHeight": 3, "took": "286.028µs"}
logger.go:130: 2023-04-03T15:58:27.681+0300 DEBUG done processing headers {"headerIndex": 5, "blockHeight": 4, "took": "268.673µs"}
logger.go:130: 2023-04-03T15:58:27.681+0300 INFO bad notification {"contract": "565cff9508ebc75aadd7fe59f38dac610ab6093c", "event": "Transfer", "error": "parameter 0 type mismatch: Hash160 vs ByteString"}
logger.go:130: 2023-04-03T15:58:27.682+0300 DEBUG done processing headers {"headerIndex": 6, "blockHeight": 5, "took": "380.988µs"}
logger.go:130: 2023-04-03T15:58:27.683+0300 DEBUG done processing headers {"headerIndex": 7, "blockHeight": 6, "took": "273.543µs"}
logger.go:130: 2023-04-03T15:58:27.683+0300 DEBUG done processing headers {"headerIndex": 8, "blockHeight": 7, "took": "275.163µs"}
logger.go:130: 2023-04-03T15:58:27.684+0300 DEBUG done processing headers {"headerIndex": 9, "blockHeight": 8, "took": "259.578µs"}
logger.go:130: 2023-04-03T15:58:27.685+0300 DEBUG done processing headers {"headerIndex": 10, "blockHeight": 9, "took": "266.882µs"}
logger.go:130: 2023-04-03T15:58:27.686+0300 DEBUG done processing headers {"headerIndex": 11, "blockHeight": 10, "took": "295.3µs"}
logger.go:130: 2023-04-03T15:58:27.687+0300 DEBUG done processing headers {"headerIndex": 12, "blockHeight": 11, "took": "295.568µs"}
logger.go:130: 2023-04-03T15:58:27.688+0300 DEBUG done processing headers {"headerIndex": 13, "blockHeight": 12, "took": "258.197µs"}
logger.go:130: 2023-04-03T15:58:27.689+0300 DEBUG done processing headers {"headerIndex": 14, "blockHeight": 13, "took": "261.602µs"}
logger.go:130: 2023-04-03T15:58:27.689+0300 DEBUG done processing headers {"headerIndex": 15, "blockHeight": 14, "took": "268.922µs"}
logger.go:130: 2023-04-03T15:58:27.690+0300 DEBUG done processing headers {"headerIndex": 16, "blockHeight": 15, "took": "276.176µs"}
logger.go:130: 2023-04-03T15:58:27.691+0300 DEBUG done processing headers {"headerIndex": 17, "blockHeight": 16, "took": "256.068µs"}
logger.go:130: 2023-04-03T15:58:27.692+0300 DEBUG done processing headers {"headerIndex": 18, "blockHeight": 17, "took": "262.303µs"}
logger.go:130: 2023-04-03T15:58:27.692+0300 DEBUG done processing headers {"headerIndex": 19, "blockHeight": 18, "took": "265.087µs"}
logger.go:130: 2023-04-03T15:58:27.693+0300 DEBUG done processing headers {"headerIndex": 20, "blockHeight": 19, "took": "260.758µs"}
logger.go:130: 2023-04-03T15:58:27.694+0300 DEBUG done processing headers {"headerIndex": 21, "blockHeight": 20, "took": "263.482µs"}
logger.go:130: 2023-04-03T15:58:27.694+0300 DEBUG done processing headers {"headerIndex": 22, "blockHeight": 21, "took": "327.812µs"}
logger.go:130: 2023-04-03T15:58:27.696+0300 DEBUG done processing headers {"headerIndex": 23, "blockHeight": 22, "took": "284.104µs"}
logger.go:130: 2023-04-03T15:58:27.697+0300 WARN contract invocation failed {"tx": "82279bfe9bada282ca0f8cb8e0bb124b921af36f00c69a518320322c6f4fef60", "block": 23, "error": "at instruction 0 (ABORT): ABORT"}
logger.go:130: 2023-04-03T15:58:27.697+0300 DEBUG processing rpc request {"method": "getversion", "params": "[]"}
logger.go:130: 2023-04-03T15:58:27.698+0300 DEBUG processing rpc request {"method": "invokefunction", "params": "[565cff9508ebc75aadd7fe59f38dac610ab6093c putValue ]"}
client_test.go:2562:
Error Trace: /home/anna/Documents/GitProjects/nspcc-dev/neo-go/pkg/services/rpcsrv/client_test.go:2562
Error: Should be true
Test: TestActor_CallWithNilParam
Messages: at instruction 6 (PACK): OPACK: invalid length
logger.go:130: 2023-04-03T15:58:27.699+0300 INFO shutting down RPC server {"endpoint": "127.0.0.1:46005"}
logger.go:130: 2023-04-03T15:58:27.700+0300 INFO persisted to disk {"blocks": 23, "keys": 1236, "headerHeight": 23, "blockHeight": 23, "took": "908.825µs"}
--- FAIL: TestActor_CallWithNilParam (0.03s)
FAIL
```
See also the ref. https://github.com/neo-project/neo/blob/df534f6b0c700e1c7b3eb1315c4560fedce793b8/src/Neo/SmartContract/ContractParameter.cs#L141
and the way how parameters are handled by ref. RPC server:
https://github.com/neo-project/neo-modules/blob/4b3a76e1b760798b2e9bc8706bc5f4cf22c1f5ae/src/RpcServer/RpcServer.SmartContract.cs#L202
and FromJSON implementation:
https://github.com/neo-project/neo/blob/df534f6b0c700e1c7b3eb1315c4560fedce793b8/src/Neo/SmartContract/ContractParameter.cs#L70
2023-04-03 12:53:37 +00:00
|
|
|
if fp.Value.IsNull() || len(fp.Value.RawMessage) == 0 {
|
2021-11-09 07:37:22 +00:00
|
|
|
emit.Opcodes(script, opcode.PUSHNULL)
|
|
|
|
}
|
2019-11-28 16:08:31 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("parameter type %v is not supported", fp.Type)
|
|
|
|
}
|
|
|
|
}
|
2022-02-09 12:38:22 +00:00
|
|
|
return script.Err
|
2019-11-28 16:08:31 +00:00
|
|
|
}
|
|
|
|
|
rpc: adjust script creation with empty parameters list
Always use NEWARRAY0 where possible, see
https://github.com/neo-project/neo/blob/26d04a642ac5a1dd1827dabf5602767e0acba25c/src/neo/VM/Helper.cs#L41.
Compatibility is tested:
```
anna@kiwi:~/Documents/GitProjects/nspcc-dev/neo-go$ curl -d '{ "jsonrpc": "2.0", "id": 1, "method": "invokefunction", "params": ["50befd26fdf6e4d957c11e078b24ebce6291456f", "a", [] ]}' seed1.neo.org:10332 | json_pp
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 370 0 248 100 122 127 62 0:00:01 0:00:01 --:--:-- 190
{
"result" : {
"notifications" : [],
"stack" : [],
"script" : "wh8MAWEMFG9FkWLO6ySLBx7BV9nk9v0m/b5QQWJ9W1I=",
"gasconsumed" : "98403",
"state" : "FAULT",
"exception" : "Called Contract Does Not Exist: 0x50befd26fdf6e4d957c11e078b24ebce6291456f"
},
"jsonrpc" : "2.0",
"id" : 1
}
```
2022-06-22 10:23:55 +00:00
|
|
|
// ExpandArrayIntoScriptAndPack expands provided array into script and packs the
|
|
|
|
// resulting items in the array.
|
|
|
|
func ExpandArrayIntoScriptAndPack(script *io.BinWriter, slice []Param) error {
|
|
|
|
if len(slice) == 0 {
|
|
|
|
emit.Opcodes(script, opcode.NEWARRAY0)
|
|
|
|
return script.Err
|
|
|
|
}
|
|
|
|
err := ExpandArrayIntoScript(script, slice)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
emit.Int(script, int64(len(slice)))
|
|
|
|
emit.Opcodes(script, opcode.PACK)
|
|
|
|
return script.Err
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// CreateFunctionInvocationScript creates a script to invoke the given contract with
|
|
|
|
// the given parameters.
|
2021-11-20 16:25:42 +00:00
|
|
|
func CreateFunctionInvocationScript(contract util.Uint160, method string, param *Param) ([]byte, error) {
|
2020-02-03 14:46:51 +00:00
|
|
|
script := io.NewBufBinWriter()
|
2021-11-20 16:25:42 +00:00
|
|
|
if param == nil {
|
2021-10-16 18:09:29 +00:00
|
|
|
emit.Opcodes(script.BinWriter, opcode.NEWARRAY0)
|
2021-11-20 16:25:42 +00:00
|
|
|
} else if slice, err := param.GetArray(); err == nil {
|
rpc: adjust script creation with empty parameters list
Always use NEWARRAY0 where possible, see
https://github.com/neo-project/neo/blob/26d04a642ac5a1dd1827dabf5602767e0acba25c/src/neo/VM/Helper.cs#L41.
Compatibility is tested:
```
anna@kiwi:~/Documents/GitProjects/nspcc-dev/neo-go$ curl -d '{ "jsonrpc": "2.0", "id": 1, "method": "invokefunction", "params": ["50befd26fdf6e4d957c11e078b24ebce6291456f", "a", [] ]}' seed1.neo.org:10332 | json_pp
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 370 0 248 100 122 127 62 0:00:01 0:00:01 --:--:-- 190
{
"result" : {
"notifications" : [],
"stack" : [],
"script" : "wh8MAWEMFG9FkWLO6ySLBx7BV9nk9v0m/b5QQWJ9W1I=",
"gasconsumed" : "98403",
"state" : "FAULT",
"exception" : "Called Contract Does Not Exist: 0x50befd26fdf6e4d957c11e078b24ebce6291456f"
},
"jsonrpc" : "2.0",
"id" : 1
}
```
2022-06-22 10:23:55 +00:00
|
|
|
err = ExpandArrayIntoScriptAndPack(script.BinWriter, slice)
|
2021-11-20 16:25:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("failed to convert %s to script parameter", param)
|
2021-10-16 18:09:29 +00:00
|
|
|
}
|
2019-11-26 10:13:17 +00:00
|
|
|
|
2020-12-29 10:44:07 +00:00
|
|
|
emit.AppCallNoArgs(script.BinWriter, contract, method, callflag.All)
|
2019-11-26 10:13:17 +00:00
|
|
|
return script.Bytes(), nil
|
|
|
|
}
|