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

Conversation

igormcoelho
Copy link
Contributor

An extra dictionary from string to uint will help complementing interop function accesses. That will help implementing it on Neo: neo-project/neo#264
This is a small continuation of this PR: #61

An extra dictionary from string to uint will help complementing interop function accesses. That will help implementing it on Neo: neo-project/neo#264
This is a small continuation of this PR: neo-project#61
@igormcoelho
Copy link
Contributor Author

@jsolman I guess this helps resolving all your suggestions regarding the last PR.
@shargon This will help us implementing the switch-case on Neo for Syscall Pricing.

@igormcoelho
Copy link
Contributor Author

@erikzhang Sorry for not thinking that before.. this PR will help a lot to efficiently implement function recognition on Neo project.

igormcoelho added a commit to igormcoelho/neo that referenced this pull request Oct 24, 2018
Calculating prices depending on neo-vm dictionary: neo-project/neo-vm#62

public uint InteropHash(string method)
{
return dictionaryStr[method];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TryGet or if you pass a wrong value you will get an exception

}

internal bool Invoke(byte[] method, ExecutionEngine engine)
{
uint hash = method.Length == 4
? BitConverter.ToUInt32(method, 0)
: method.ToInteropMethodHash();
: dictionaryStr[method];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we should choose in wich dictionary we need to looking for, otherwise we can get exceptions too

@@ -6,6 +6,12 @@ namespace Neo.VM
public class InteropService
{
private Dictionary<uint, Func<ExecutionEngine, bool>> dictionary = new Dictionary<uint, Func<ExecutionEngine, bool>>();
private Dictionary<string, uint> dictionaryStr = new Dictionary<string, uint>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dictionary<string, Func<ExecutionEngine, bool>> dictionaryStr = new Dictionary<string, Func<ExecutionEngine, bool>>();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, I will need string to.m uint, to be able to compare it inside Neo project.... example, if user passes uint 1000 I need to know if its a Storage.Put, to know the price.

@erikzhang
Copy link
Member

public static uint ToInteropMethodHash(this byte[] method)
{
using (SHA256 sha = SHA256.Create())
{
return BitConverter.ToUInt32(sha.ComputeHash(method), 0);
}
}

Why not add the dictionary at here? Then we can use Helper.ToInteropMethodHash() everywhere.

@igormcoelho
Copy link
Contributor Author

@erikzhang I think I finally understood what you meant by using this Helper class... I used the dictionary here to act as a caching/memoisation for future accesses, so it will only take time during first access.

src/neo-vm/Helper.cs Outdated Show resolved Hide resolved
erikzhang
erikzhang previously approved these changes Nov 14, 2018
src/neo-vm/Helper.cs Show resolved Hide resolved
return hash;
hash = ToInteropMethodHash(Encoding.ASCII.GetBytes(method));
method_hashes[method] = hash;
return hash;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much more readable.

@erikzhang
Copy link
Member

Now it should work perfectly.

@erikzhang erikzhang merged commit 7f5b76b into neo-project:master Nov 16, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants