From a796f2b61d8e795fa519e72b6544117c06dfa32d Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 13 Aug 2020 10:48:33 +0300 Subject: [PATCH] names: implement FromID Allow to convert interop id to it's name. --- pkg/core/interop/interopnames/convert.go | 13 ++++ pkg/core/interop/interopnames/convert_test.go | 21 ++++++ pkg/core/interop/interopnames/names.go | 65 +++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 pkg/core/interop/interopnames/convert_test.go diff --git a/pkg/core/interop/interopnames/convert.go b/pkg/core/interop/interopnames/convert.go index cacd44985..54aa0f09c 100644 --- a/pkg/core/interop/interopnames/convert.go +++ b/pkg/core/interop/interopnames/convert.go @@ -3,10 +3,23 @@ package interopnames import ( "crypto/sha256" "encoding/binary" + "errors" ) +var errNotFound = errors.New("interop not found") + // ToID returns an identificator of the method based on its name. func ToID(name []byte) uint32 { h := sha256.Sum256(name) return binary.LittleEndian.Uint32(h[:4]) } + +// FromID returns interop name from its id. +func FromID(id uint32) (string, error) { + for i := range names { + if id == ToID([]byte(names[i])) { + return names[i], nil + } + } + return "", errNotFound +} diff --git a/pkg/core/interop/interopnames/convert_test.go b/pkg/core/interop/interopnames/convert_test.go new file mode 100644 index 000000000..1ec2c1f21 --- /dev/null +++ b/pkg/core/interop/interopnames/convert_test.go @@ -0,0 +1,21 @@ +package interopnames + +import ( + "errors" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestFromID(t *testing.T) { + t.Run("Valid", func(t *testing.T) { + id := ToID([]byte(names[0])) + name, err := FromID(id) + require.NoError(t, err) + require.Equal(t, names[0], name) + }) + t.Run("Invalid", func(t *testing.T) { + _, err := FromID(0x42424242) + require.True(t, errors.Is(err, errNotFound)) + }) +} diff --git a/pkg/core/interop/interopnames/names.go b/pkg/core/interop/interopnames/names.go index ecf033a7a..01f36cdf1 100644 --- a/pkg/core/interop/interopnames/names.go +++ b/pkg/core/interop/interopnames/names.go @@ -65,3 +65,68 @@ const ( NeoNativeCall = "Neo.Native.Call" NeoNativeDeploy = "Neo.Native.Deploy" ) + +var names = []string{ + SystemBinaryBase64Decode, + SystemBinaryBase64Encode, + SystemBinaryDeserialize, + SystemBinarySerialize, + SystemBlockchainGetBlock, + SystemBlockchainGetContract, + SystemBlockchainGetHeight, + SystemBlockchainGetTransaction, + SystemBlockchainGetTransactionFromBlock, + SystemBlockchainGetTransactionHeight, + SystemCallbackCreate, + SystemCallbackCreateFromMethod, + SystemCallbackCreateFromSyscall, + SystemCallbackInvoke, + SystemContractCall, + SystemContractCallEx, + SystemContractCreate, + SystemContractCreateStandardAccount, + SystemContractDestroy, + SystemContractIsStandard, + SystemContractGetCallFlags, + SystemContractUpdate, + SystemEnumeratorConcat, + SystemEnumeratorCreate, + SystemEnumeratorNext, + SystemEnumeratorValue, + SystemIteratorConcat, + SystemIteratorCreate, + SystemIteratorKey, + SystemIteratorKeys, + SystemIteratorValues, + SystemJSONDeserialize, + SystemJSONSerialize, + SystemRuntimeCheckWitness, + SystemRuntimeGasLeft, + SystemRuntimeGetCallingScriptHash, + SystemRuntimeGetEntryScriptHash, + SystemRuntimeGetExecutingScriptHash, + SystemRuntimeGetInvocationCounter, + SystemRuntimeGetNotifications, + SystemRuntimeGetScriptContainer, + SystemRuntimeGetTime, + SystemRuntimeGetTrigger, + SystemRuntimeLog, + SystemRuntimeNotify, + SystemRuntimePlatform, + SystemStorageDelete, + SystemStorageFind, + SystemStorageGet, + SystemStorageGetContext, + SystemStorageGetReadOnlyContext, + SystemStoragePut, + SystemStoragePutEx, + SystemStorageAsReadOnly, + NeoCryptoVerifyWithECDsaSecp256r1, + NeoCryptoVerifyWithECDsaSecp256k1, + NeoCryptoCheckMultisigWithECDsaSecp256r1, + NeoCryptoCheckMultisigWithECDsaSecp256k1, + NeoCryptoSHA256, + NeoCryptoRIPEMD160, + NeoNativeCall, + NeoNativeDeploy, +}