diff --git a/src/neo-vm/Helper.cs b/src/neo-vm/Helper.cs index b31d0f81..dbe69e68 100644 --- a/src/neo-vm/Helper.cs +++ b/src/neo-vm/Helper.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.IO; using System.Security.Cryptography; using System.Text; @@ -7,6 +8,8 @@ namespace Neo.VM { public static class Helper { + private static Dictionary method_hashes = new Dictionary(); + internal static byte[] ReadVarBytes(this BinaryReader reader, int max = 0X7fffffc7) { return reader.ReadBytes((int)reader.ReadVarInt((ulong)max)); @@ -35,15 +38,14 @@ internal static string ReadVarString(this BinaryReader reader) public static uint ToInteropMethodHash(this string method) { - return ToInteropMethodHash(Encoding.ASCII.GetBytes(method)); - } - - public static uint ToInteropMethodHash(this byte[] method) - { + if (method_hashes.TryGetValue(method, out uint hash)) + return hash; using (SHA256 sha = SHA256.Create()) { - return BitConverter.ToUInt32(sha.ComputeHash(method), 0); + hash = BitConverter.ToUInt32(sha.ComputeHash(Encoding.ASCII.GetBytes(method)), 0); } + method_hashes[method] = hash; + return hash; } internal static void WriteVarBytes(this BinaryWriter writer, byte[] value) diff --git a/src/neo-vm/InteropService.cs b/src/neo-vm/InteropService.cs index 0a914c75..d1da0eab 100644 --- a/src/neo-vm/InteropService.cs +++ b/src/neo-vm/InteropService.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Text; namespace Neo.VM { @@ -24,7 +25,7 @@ internal bool Invoke(byte[] method, ExecutionEngine engine) { uint hash = method.Length == 4 ? BitConverter.ToUInt32(method, 0) - : method.ToInteropMethodHash(); + : Encoding.ASCII.GetString(method).ToInteropMethodHash(); if (!dictionary.TryGetValue(hash, out Func func)) return false; return func(engine); }