Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

Added extra dictionary mapping from string -> uint #62

Merged
merged 6 commits into from
Nov 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/neo-vm/Helper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
Expand All @@ -7,6 +8,8 @@ namespace Neo.VM
{
public static class Helper
{
private static Dictionary<string, uint> method_hashes = new Dictionary<string, uint>();

igormcoelho marked this conversation as resolved.
Show resolved Hide resolved
internal static byte[] ReadVarBytes(this BinaryReader reader, int max = 0X7fffffc7)
{
return reader.ReadBytes((int)reader.ReadVarInt((ulong)max));
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion src/neo-vm/InteropService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Neo.VM
{
Expand All @@ -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<ExecutionEngine, bool> func)) return false;
return func(engine);
}
Expand Down