2021-02-08 08:06:54 +00:00
|
|
|
package compiler_test
|
|
|
|
|
|
|
|
import (
|
2022-01-16 12:58:51 +00:00
|
|
|
"bytes"
|
2021-12-08 19:33:03 +00:00
|
|
|
"errors"
|
2021-02-08 08:10:58 +00:00
|
|
|
"fmt"
|
2021-02-05 10:15:39 +00:00
|
|
|
"math/big"
|
2022-01-16 12:58:51 +00:00
|
|
|
"strconv"
|
2021-02-05 10:15:39 +00:00
|
|
|
"strings"
|
2021-02-08 08:06:54 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-12-08 19:33:03 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/compiler"
|
2021-07-20 12:54:56 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config"
|
2021-02-05 10:15:39 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
2021-02-08 08:06:54 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native"
|
2021-03-23 10:37:30 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native/noderoles"
|
2021-04-06 18:45:15 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2021-02-15 15:43:10 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/crypto"
|
2021-02-08 08:10:58 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/gas"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/ledger"
|
2021-02-05 10:15:39 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/management"
|
2021-02-08 08:10:58 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/neo"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/notary"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/oracle"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/policy"
|
2021-02-08 08:08:48 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/roles"
|
2021-03-03 15:59:10 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/std"
|
2021-02-05 10:15:39 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2022-01-16 12:58:51 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/nef"
|
2021-02-05 10:15:39 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2021-02-08 08:06:54 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2021-02-08 08:10:58 +00:00
|
|
|
func TestContractHashes(t *testing.T) {
|
2021-07-20 12:54:56 +00:00
|
|
|
cfg := config.ProtocolConfiguration{P2PSigExtensions: true}
|
|
|
|
cs := native.NewContracts(cfg)
|
2021-05-12 15:18:49 +00:00
|
|
|
require.Equalf(t, []byte(neo.Hash), cs.NEO.Hash.BytesBE(), "%q", string(cs.NEO.Hash.BytesBE()))
|
|
|
|
require.Equalf(t, []byte(gas.Hash), cs.GAS.Hash.BytesBE(), "%q", string(cs.GAS.Hash.BytesBE()))
|
|
|
|
require.Equalf(t, []byte(oracle.Hash), cs.Oracle.Hash.BytesBE(), "%q", string(cs.Oracle.Hash.BytesBE()))
|
|
|
|
require.Equalf(t, []byte(roles.Hash), cs.Designate.Hash.BytesBE(), "%q", string(cs.Designate.Hash.BytesBE()))
|
|
|
|
require.Equalf(t, []byte(policy.Hash), cs.Policy.Hash.BytesBE(), "%q", string(cs.Policy.Hash.BytesBE()))
|
|
|
|
require.Equalf(t, []byte(ledger.Hash), cs.Ledger.Hash.BytesBE(), "%q", string(cs.Ledger.Hash.BytesBE()))
|
|
|
|
require.Equalf(t, []byte(management.Hash), cs.Management.Hash.BytesBE(), "%q", string(cs.Management.Hash.BytesBE()))
|
|
|
|
require.Equalf(t, []byte(notary.Hash), cs.Notary.Hash.BytesBE(), "%q", string(cs.Notary.Hash.BytesBE()))
|
|
|
|
require.Equalf(t, []byte(crypto.Hash), cs.Crypto.Hash.BytesBE(), "%q", string(cs.Crypto.Hash.BytesBE()))
|
|
|
|
require.Equalf(t, []byte(std.Hash), cs.Std.Hash.BytesBE(), "%q", string(cs.Std.Hash.BytesBE()))
|
2021-02-08 08:10:58 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 10:09:28 +00:00
|
|
|
func TestContractParameterTypes(t *testing.T) {
|
|
|
|
require.EqualValues(t, management.AnyType, smartcontract.AnyType)
|
|
|
|
require.EqualValues(t, management.BoolType, smartcontract.BoolType)
|
|
|
|
require.EqualValues(t, management.IntegerType, smartcontract.IntegerType)
|
|
|
|
require.EqualValues(t, management.ByteArrayType, smartcontract.ByteArrayType)
|
|
|
|
require.EqualValues(t, management.StringType, smartcontract.StringType)
|
|
|
|
require.EqualValues(t, management.Hash160Type, smartcontract.Hash160Type)
|
|
|
|
require.EqualValues(t, management.Hash256Type, smartcontract.Hash256Type)
|
|
|
|
require.EqualValues(t, management.PublicKeyType, smartcontract.PublicKeyType)
|
|
|
|
require.EqualValues(t, management.SignatureType, smartcontract.SignatureType)
|
|
|
|
require.EqualValues(t, management.ArrayType, smartcontract.ArrayType)
|
|
|
|
require.EqualValues(t, management.MapType, smartcontract.MapType)
|
|
|
|
require.EqualValues(t, management.InteropInterfaceType, smartcontract.InteropInterfaceType)
|
|
|
|
require.EqualValues(t, management.VoidType, smartcontract.VoidType)
|
|
|
|
}
|
|
|
|
|
2021-02-08 08:08:48 +00:00
|
|
|
func TestRoleManagementRole(t *testing.T) {
|
2021-03-23 10:37:30 +00:00
|
|
|
require.EqualValues(t, noderoles.Oracle, roles.Oracle)
|
|
|
|
require.EqualValues(t, noderoles.StateValidator, roles.StateValidator)
|
|
|
|
require.EqualValues(t, noderoles.NeoFSAlphabet, roles.NeoFSAlphabet)
|
|
|
|
require.EqualValues(t, noderoles.P2PNotary, roles.P2PNotary)
|
2021-02-08 08:08:48 +00:00
|
|
|
}
|
|
|
|
|
2021-02-15 15:43:10 +00:00
|
|
|
func TestCryptoLibNamedCurve(t *testing.T) {
|
|
|
|
require.EqualValues(t, native.Secp256k1, crypto.Secp256k1)
|
|
|
|
require.EqualValues(t, native.Secp256r1, crypto.Secp256r1)
|
|
|
|
}
|
|
|
|
|
2021-04-06 19:23:11 +00:00
|
|
|
func TestOracleContractValues(t *testing.T) {
|
2021-04-06 18:45:15 +00:00
|
|
|
require.EqualValues(t, oracle.Success, transaction.Success)
|
|
|
|
require.EqualValues(t, oracle.ProtocolNotSupported, transaction.ProtocolNotSupported)
|
|
|
|
require.EqualValues(t, oracle.ConsensusUnreachable, transaction.ConsensusUnreachable)
|
|
|
|
require.EqualValues(t, oracle.NotFound, transaction.NotFound)
|
|
|
|
require.EqualValues(t, oracle.Timeout, transaction.Timeout)
|
|
|
|
require.EqualValues(t, oracle.Forbidden, transaction.Forbidden)
|
|
|
|
require.EqualValues(t, oracle.ResponseTooLarge, transaction.ResponseTooLarge)
|
|
|
|
require.EqualValues(t, oracle.InsufficientFunds, transaction.InsufficientFunds)
|
|
|
|
require.EqualValues(t, oracle.Error, transaction.Error)
|
2021-04-06 19:23:11 +00:00
|
|
|
|
|
|
|
require.EqualValues(t, oracle.MinimumResponseGas, native.MinimumResponseGas)
|
2021-04-06 18:45:15 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 10:41:54 +00:00
|
|
|
func TestLedgerVMStates(t *testing.T) {
|
|
|
|
require.EqualValues(t, ledger.NoneState, vm.NoneState)
|
|
|
|
require.EqualValues(t, ledger.HaltState, vm.HaltState)
|
|
|
|
require.EqualValues(t, ledger.FaultState, vm.FaultState)
|
|
|
|
require.EqualValues(t, ledger.BreakState, vm.BreakState)
|
|
|
|
}
|
|
|
|
|
2021-02-05 10:15:39 +00:00
|
|
|
type nativeTestCase struct {
|
|
|
|
method string
|
|
|
|
params []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Here we test that corresponding method does exist, is invoked and correct value is returned.
|
|
|
|
func TestNativeHelpersCompile(t *testing.T) {
|
2021-07-20 12:54:56 +00:00
|
|
|
cfg := config.ProtocolConfiguration{P2PSigExtensions: true}
|
|
|
|
cs := native.NewContracts(cfg)
|
2021-02-05 10:15:39 +00:00
|
|
|
u160 := `interop.Hash160("aaaaaaaaaaaaaaaaaaaa")`
|
|
|
|
u256 := `interop.Hash256("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")`
|
|
|
|
pub := `interop.PublicKey("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")`
|
2021-02-15 15:43:10 +00:00
|
|
|
sig := `interop.Signature("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")`
|
2021-02-05 10:15:39 +00:00
|
|
|
nep17TestCases := []nativeTestCase{
|
|
|
|
{"balanceOf", []string{u160}},
|
|
|
|
{"decimals", nil},
|
|
|
|
{"symbol", nil},
|
|
|
|
{"totalSupply", nil},
|
|
|
|
{"transfer", []string{u160, u160, "123", "nil"}},
|
|
|
|
}
|
|
|
|
runNativeTestCases(t, cs.NEO.ContractMD, "neo", append([]nativeTestCase{
|
|
|
|
{"getCandidates", nil},
|
|
|
|
{"getCommittee", nil},
|
|
|
|
{"getGasPerBlock", nil},
|
|
|
|
{"getNextBlockValidators", nil},
|
2021-03-05 11:17:58 +00:00
|
|
|
{"getRegisterPrice", nil},
|
2021-02-05 10:15:39 +00:00
|
|
|
{"registerCandidate", []string{pub}},
|
|
|
|
{"setGasPerBlock", []string{"1"}},
|
2021-03-05 11:17:58 +00:00
|
|
|
{"setRegisterPrice", []string{"10"}},
|
2021-02-05 10:15:39 +00:00
|
|
|
{"vote", []string{u160, pub}},
|
|
|
|
{"unclaimedGas", []string{u160, "123"}},
|
|
|
|
{"unregisterCandidate", []string{pub}},
|
2021-05-25 14:54:57 +00:00
|
|
|
{"getAccountState", []string{u160}},
|
2021-02-05 10:15:39 +00:00
|
|
|
}, nep17TestCases...))
|
2021-08-05 07:21:35 +00:00
|
|
|
runNativeTestCases(t, cs.GAS.ContractMD, "gas", nep17TestCases)
|
2021-02-05 10:15:39 +00:00
|
|
|
runNativeTestCases(t, cs.Oracle.ContractMD, "oracle", []nativeTestCase{
|
2021-03-05 11:38:16 +00:00
|
|
|
{"getPrice", nil},
|
2021-02-05 10:15:39 +00:00
|
|
|
{"request", []string{`"url"`, "nil", `"callback"`, "nil", "123"}},
|
2021-03-05 11:38:16 +00:00
|
|
|
{"setPrice", []string{"10"}},
|
2021-02-05 10:15:39 +00:00
|
|
|
})
|
|
|
|
runNativeTestCases(t, cs.Designate.ContractMD, "roles", []nativeTestCase{
|
|
|
|
{"designateAsRole", []string{"1", "[]interop.PublicKey{}"}},
|
|
|
|
{"getDesignatedByRole", []string{"1", "1000"}},
|
|
|
|
})
|
|
|
|
runNativeTestCases(t, cs.Policy.ContractMD, "policy", []nativeTestCase{
|
|
|
|
{"blockAccount", []string{u160}},
|
|
|
|
{"getExecFeeFactor", nil},
|
|
|
|
{"getFeePerByte", nil},
|
|
|
|
{"getStoragePrice", nil},
|
|
|
|
{"isBlocked", []string{u160}},
|
|
|
|
{"setExecFeeFactor", []string{"42"}},
|
|
|
|
{"setFeePerByte", []string{"42"}},
|
|
|
|
{"setStoragePrice", []string{"42"}},
|
|
|
|
{"unblockAccount", []string{u160}},
|
|
|
|
})
|
|
|
|
runNativeTestCases(t, cs.Ledger.ContractMD, "ledger", []nativeTestCase{
|
|
|
|
{"currentHash", nil},
|
|
|
|
{"currentIndex", nil},
|
|
|
|
{"getBlock", []string{"1"}},
|
|
|
|
{"getTransaction", []string{u256}},
|
|
|
|
{"getTransactionFromBlock", []string{u256, "1"}},
|
|
|
|
{"getTransactionHeight", []string{u256}},
|
2022-04-04 10:41:54 +00:00
|
|
|
{"getTransactionVMState", []string{u256}},
|
2021-02-05 10:15:39 +00:00
|
|
|
})
|
|
|
|
runNativeTestCases(t, cs.Notary.ContractMD, "notary", []nativeTestCase{
|
|
|
|
{"lockDepositUntil", []string{u160, "123"}},
|
|
|
|
{"withdraw", []string{u160, u160}},
|
|
|
|
{"balanceOf", []string{u160}},
|
|
|
|
{"expirationOf", []string{u160}},
|
|
|
|
{"getMaxNotValidBeforeDelta", nil},
|
|
|
|
{"setMaxNotValidBeforeDelta", []string{"42"}},
|
2022-03-01 10:38:25 +00:00
|
|
|
{"getNotaryServiceFeePerKey", nil},
|
|
|
|
{"setNotaryServiceFeePerKey", []string{"42"}},
|
2021-02-05 10:15:39 +00:00
|
|
|
})
|
|
|
|
runNativeTestCases(t, cs.Management.ContractMD, "management", []nativeTestCase{
|
|
|
|
{"deploy", []string{"nil", "nil"}},
|
|
|
|
{"deployWithData", []string{"nil", "nil", "123"}},
|
|
|
|
{"destroy", nil},
|
|
|
|
{"getContract", []string{u160}},
|
|
|
|
{"getMinimumDeploymentFee", nil},
|
|
|
|
{"setMinimumDeploymentFee", []string{"42"}},
|
|
|
|
{"update", []string{"nil", "nil"}},
|
|
|
|
{"updateWithData", []string{"nil", "nil", "123"}},
|
|
|
|
})
|
2021-02-15 15:43:10 +00:00
|
|
|
runNativeTestCases(t, cs.Crypto.ContractMD, "crypto", []nativeTestCase{
|
|
|
|
{"sha256", []string{"[]byte{1, 2, 3}"}},
|
|
|
|
{"ripemd160", []string{"[]byte{1, 2, 3}"}},
|
2022-04-04 14:04:34 +00:00
|
|
|
{"murmur32", []string{"[]byte{1, 2, 3}", "123"}},
|
2021-02-15 15:43:10 +00:00
|
|
|
{"verifyWithECDsa", []string{"[]byte{1, 2, 3}", pub, sig, "crypto.Secp256k1"}},
|
|
|
|
})
|
2021-03-03 15:59:10 +00:00
|
|
|
runNativeTestCases(t, cs.Std.ContractMD, "std", []nativeTestCase{
|
|
|
|
{"serialize", []string{"[]byte{1, 2, 3}"}},
|
|
|
|
{"deserialize", []string{"[]byte{1, 2, 3}"}},
|
|
|
|
{"jsonSerialize", []string{"[]byte{1, 2, 3}"}},
|
|
|
|
{"jsonDeserialize", []string{"[]byte{1, 2, 3}"}},
|
|
|
|
{"base64Encode", []string{"[]byte{1, 2, 3}"}},
|
|
|
|
{"base64Decode", []string{"[]byte{1, 2, 3}"}},
|
|
|
|
{"base58Encode", []string{"[]byte{1, 2, 3}"}},
|
|
|
|
{"base58Decode", []string{"[]byte{1, 2, 3}"}},
|
2021-05-25 15:06:37 +00:00
|
|
|
{"base58CheckEncode", []string{"[]byte{1, 2, 3}"}},
|
|
|
|
{"base58CheckDecode", []string{"[]byte{1, 2, 3}"}},
|
2021-03-03 15:59:10 +00:00
|
|
|
{"itoa", []string{"4", "10"}},
|
2021-04-30 08:22:00 +00:00
|
|
|
{"itoa10", []string{"4"}},
|
2021-03-03 15:59:10 +00:00
|
|
|
{"atoi", []string{`"4"`, "10"}},
|
2021-04-30 08:22:00 +00:00
|
|
|
{"atoi10", []string{`"4"`}},
|
2021-04-30 12:02:44 +00:00
|
|
|
{"memoryCompare", []string{"[]byte{1}", "[]byte{2}"}},
|
2021-04-30 12:33:07 +00:00
|
|
|
{"memorySearch", []string{"[]byte{1}", "[]byte{2}"}},
|
|
|
|
{"memorySearchIndex", []string{"[]byte{1}", "[]byte{2}", "3"}},
|
|
|
|
{"memorySearchLastIndex", []string{"[]byte{1}", "[]byte{2}", "3"}},
|
2021-04-30 14:00:29 +00:00
|
|
|
{"stringSplit", []string{`"a,b"`, `","`}},
|
|
|
|
{"stringSplitNonEmpty", []string{`"a,b"`, `","`}},
|
2021-03-03 15:59:10 +00:00
|
|
|
})
|
2021-02-05 10:15:39 +00:00
|
|
|
}
|
|
|
|
|
2022-01-16 12:58:51 +00:00
|
|
|
func runNativeTestCases(t *testing.T, ctr interop.ContractMD, name string, nativeTestCases []nativeTestCase) {
|
|
|
|
srcBuilder := bytes.NewBuffer([]byte(`package foo
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop/native/` + name + `"
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop"
|
|
|
|
var _ interop.Hash160
|
|
|
|
`))
|
|
|
|
for i, tc := range nativeTestCases {
|
|
|
|
addNativeTestCase(t, srcBuilder, ctr, i, name, tc.method, tc.params...)
|
|
|
|
}
|
|
|
|
|
|
|
|
ne, di, err := compiler.CompileWithOptions("file.go", strings.NewReader(srcBuilder.String()), nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-02-05 10:15:39 +00:00
|
|
|
t.Run(ctr.Name, func(t *testing.T) {
|
2022-01-16 12:58:51 +00:00
|
|
|
for i, tc := range nativeTestCases {
|
2021-02-05 10:15:39 +00:00
|
|
|
t.Run(tc.method, func(t *testing.T) {
|
2022-01-16 12:58:51 +00:00
|
|
|
runNativeTestCase(t, ne, di, ctr, i, tc.method, tc.params...)
|
2021-02-05 10:15:39 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-30 08:22:00 +00:00
|
|
|
func getMethod(t *testing.T, ctr interop.ContractMD, name string, params []string) interop.MethodAndPrice {
|
2021-04-30 12:33:07 +00:00
|
|
|
paramLen := len(params)
|
|
|
|
|
2021-04-30 08:22:00 +00:00
|
|
|
switch {
|
|
|
|
case name == "itoa10" || name == "atoi10":
|
|
|
|
name = name[:4]
|
2021-04-30 12:33:07 +00:00
|
|
|
case strings.HasPrefix(name, "memorySearch"):
|
|
|
|
if strings.HasSuffix(name, "LastIndex") {
|
2021-05-12 19:52:07 +00:00
|
|
|
paramLen++ // true should be appended inside of an interop
|
2021-04-30 12:33:07 +00:00
|
|
|
}
|
|
|
|
name = "memorySearch"
|
2021-04-30 14:00:29 +00:00
|
|
|
case strings.HasPrefix(name, "stringSplit"):
|
|
|
|
if strings.HasSuffix(name, "NonEmpty") {
|
2021-05-12 19:52:07 +00:00
|
|
|
paramLen++ // true should be appended inside of an interop
|
2021-04-30 14:00:29 +00:00
|
|
|
}
|
|
|
|
name = "stringSplit"
|
2021-04-30 08:22:00 +00:00
|
|
|
default:
|
|
|
|
name = strings.TrimSuffix(name, "WithData")
|
|
|
|
}
|
|
|
|
|
2021-04-30 12:33:07 +00:00
|
|
|
md, ok := ctr.GetMethod(name, paramLen)
|
2021-02-05 10:15:39 +00:00
|
|
|
require.True(t, ok)
|
2021-04-30 08:22:00 +00:00
|
|
|
return md
|
|
|
|
}
|
2021-02-05 10:15:39 +00:00
|
|
|
|
2022-01-16 12:58:51 +00:00
|
|
|
func addNativeTestCase(t *testing.T, srcBuilder *bytes.Buffer, ctr interop.ContractMD, i int, name, method string, params ...string) {
|
2021-04-30 08:22:00 +00:00
|
|
|
md := getMethod(t, ctr, method, params)
|
2021-02-05 10:15:39 +00:00
|
|
|
isVoid := md.MD.ReturnType == smartcontract.VoidType
|
2022-01-16 12:58:51 +00:00
|
|
|
srcBuilder.WriteString("func F" + strconv.Itoa(i) + "() ")
|
|
|
|
if !isVoid {
|
|
|
|
srcBuilder.WriteString("interface{} { return ")
|
2021-02-05 10:15:39 +00:00
|
|
|
} else {
|
2022-01-16 12:58:51 +00:00
|
|
|
srcBuilder.WriteString("{ ")
|
2021-02-05 10:15:39 +00:00
|
|
|
}
|
|
|
|
methodUpper := strings.ToUpper(method[:1]) + method[1:] // ASCII only
|
|
|
|
methodUpper = strings.ReplaceAll(methodUpper, "Gas", "GAS")
|
2021-03-03 15:59:10 +00:00
|
|
|
methodUpper = strings.ReplaceAll(methodUpper, "Json", "JSON")
|
2022-01-16 12:58:51 +00:00
|
|
|
srcBuilder.WriteString(name)
|
|
|
|
srcBuilder.WriteRune('.')
|
|
|
|
srcBuilder.WriteString(methodUpper)
|
|
|
|
srcBuilder.WriteRune('(')
|
|
|
|
srcBuilder.WriteString(strings.Join(params, ", "))
|
|
|
|
srcBuilder.WriteString(") }\n")
|
|
|
|
}
|
2021-12-08 19:33:03 +00:00
|
|
|
|
2022-01-16 12:58:51 +00:00
|
|
|
func runNativeTestCase(t *testing.T, b *nef.File, di *compiler.DebugInfo, ctr interop.ContractMD, i int, method string, params ...string) {
|
|
|
|
md := getMethod(t, ctr, method, params)
|
2021-02-05 10:15:39 +00:00
|
|
|
result := getTestStackItem(md.MD.ReturnType)
|
2022-01-16 12:58:51 +00:00
|
|
|
isVoid := md.MD.ReturnType == smartcontract.VoidType
|
|
|
|
|
|
|
|
v := vm.New()
|
2021-12-08 19:33:03 +00:00
|
|
|
v.LoadToken = func(id int32) error {
|
|
|
|
t := b.Tokens[id]
|
|
|
|
if t.Hash != ctr.Hash {
|
|
|
|
return fmt.Errorf("wrong hash %s", t.Hash.StringLE())
|
|
|
|
}
|
|
|
|
if t.Method != md.MD.Name {
|
|
|
|
return fmt.Errorf("wrong name %s", t.Method)
|
|
|
|
}
|
|
|
|
if int(t.ParamCount) != len(md.MD.Parameters) {
|
|
|
|
return fmt.Errorf("wrong number of parameters %v", t.ParamCount)
|
|
|
|
}
|
|
|
|
if t.HasReturn != !isVoid {
|
|
|
|
return fmt.Errorf("wrong hasReturn %v", t.HasReturn)
|
|
|
|
}
|
|
|
|
if t.CallFlag != md.RequiredFlags {
|
|
|
|
return fmt.Errorf("wrong flags %v", t.CallFlag)
|
|
|
|
}
|
|
|
|
for i := 0; i < int(t.ParamCount); i++ {
|
|
|
|
_ = v.Estack().Pop()
|
|
|
|
}
|
|
|
|
if v.Estack().Len() != 0 {
|
|
|
|
return errors.New("excessive parameters on the stack")
|
|
|
|
}
|
|
|
|
if !isVoid {
|
|
|
|
v.Estack().PushVal(result)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-01-16 12:58:51 +00:00
|
|
|
invokeMethod(t, fmt.Sprintf("F%d", i), b.Script, v, di)
|
2021-02-05 10:15:39 +00:00
|
|
|
require.NoError(t, v.Run())
|
|
|
|
if isVoid {
|
|
|
|
require.Equal(t, 0, v.Estack().Len())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.Equal(t, 1, v.Estack().Len(), "stack contains unexpected items")
|
|
|
|
require.Equal(t, result.Value(), v.Estack().Pop().Item().Value())
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTestStackItem(typ smartcontract.ParamType) stackitem.Item {
|
|
|
|
switch typ {
|
|
|
|
case smartcontract.AnyType, smartcontract.VoidType:
|
|
|
|
return stackitem.Null{}
|
|
|
|
case smartcontract.BoolType:
|
|
|
|
return stackitem.NewBool(true)
|
|
|
|
case smartcontract.IntegerType:
|
|
|
|
return stackitem.NewBigInteger(big.NewInt(42))
|
|
|
|
case smartcontract.ByteArrayType, smartcontract.StringType, smartcontract.Hash160Type,
|
|
|
|
smartcontract.Hash256Type, smartcontract.PublicKeyType, smartcontract.SignatureType:
|
|
|
|
return stackitem.NewByteArray([]byte("result"))
|
|
|
|
case smartcontract.ArrayType:
|
|
|
|
return stackitem.NewArray([]stackitem.Item{stackitem.NewBool(true), stackitem.Null{}})
|
|
|
|
case smartcontract.MapType:
|
|
|
|
return stackitem.NewMapWithValue([]stackitem.MapElement{{
|
|
|
|
Key: stackitem.NewByteArray([]byte{1, 2, 3}),
|
|
|
|
Value: stackitem.NewByteArray([]byte{5, 6, 7}),
|
|
|
|
}})
|
|
|
|
case smartcontract.InteropInterfaceType:
|
|
|
|
return stackitem.NewInterop(42)
|
|
|
|
default:
|
|
|
|
panic("unexpected type")
|
|
|
|
}
|
|
|
|
}
|