native: implement StdLib contract

This commit is contained in:
Anna Shaleva 2021-03-03 18:59:10 +03:00
parent 078870fceb
commit 2b90d4455f
5 changed files with 391 additions and 0 deletions

View file

@ -19,6 +19,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/interop/native/oracle"
"github.com/nspcc-dev/neo-go/pkg/interop/native/policy"
"github.com/nspcc-dev/neo-go/pkg/interop/native/roles"
"github.com/nspcc-dev/neo-go/pkg/interop/native/std"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
"github.com/nspcc-dev/neo-go/pkg/util"
@ -39,6 +40,7 @@ func TestContractHashes(t *testing.T) {
require.Equal(t, []byte(management.Hash), cs.Management.Hash.BytesBE())
require.Equal(t, []byte(notary.Hash), cs.Notary.Hash.BytesBE())
require.Equal(t, []byte(crypto.Hash), cs.Crypto.Hash.BytesBE())
require.Equal(t, []byte(std.Hash), cs.Std.Hash.BytesBE())
}
// testPrintHash is a helper for updating contract hashes.
@ -189,6 +191,18 @@ func TestNativeHelpersCompile(t *testing.T) {
{"ripemd160", []string{"[]byte{1, 2, 3}"}},
{"verifyWithECDsa", []string{"[]byte{1, 2, 3}", pub, sig, "crypto.Secp256k1"}},
})
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}"}},
{"itoa", []string{"4", "10"}},
{"atoi", []string{`"4"`, "10"}},
})
}
func runNativeTestCases(t *testing.T, ctr interop.ContractMD, name string, testCases []nativeTestCase) {
@ -218,6 +232,7 @@ func runNativeTestCase(t *testing.T, ctr interop.ContractMD, name, method string
}
methodUpper := strings.ToUpper(method[:1]) + method[1:] // ASCII only
methodUpper = strings.ReplaceAll(methodUpper, "Gas", "GAS")
methodUpper = strings.ReplaceAll(methodUpper, "Json", "JSON")
src := fmt.Sprintf(srcTmpl, name, name, methodUpper, strings.Join(params, ","))
v, s := vmAndCompileInterop(t, src)