Skip to content

Commit

Permalink
Allow call from native contract (#1700)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang authored Jun 22, 2020
1 parent f68769f commit d275916
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
41 changes: 41 additions & 0 deletions src/neo/SmartContract/ApplicationEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ namespace Neo.SmartContract
{
public partial class ApplicationEngine : ExecutionEngine
{
private class InvocationState
{
public Type ReturnType;
public Delegate Callback;
}

public static event EventHandler<NotifyEventArgs> Notify;
public static event EventHandler<LogEventArgs> Log;

Expand All @@ -27,6 +33,7 @@ public partial class ApplicationEngine : ExecutionEngine
private readonly List<NotifyEventArgs> notifications = new List<NotifyEventArgs>();
private readonly List<IDisposable> disposables = new List<IDisposable>();
private readonly Dictionary<UInt160, int> invocationCounter = new Dictionary<UInt160, int>();
private readonly Dictionary<ExecutionContext, InvocationState> invocationStates = new Dictionary<ExecutionContext, InvocationState>();

public static IReadOnlyDictionary<uint, InteropDescriptor> Services => services;
public TriggerType Trigger { get; }
Expand Down Expand Up @@ -55,12 +62,46 @@ internal void AddGas(long gas)
throw new InvalidOperationException("Insufficient GAS.");
}

internal void CallFromNativeContract(Action onComplete, UInt160 hash, string method, params StackItem[] args)
{
invocationStates.Add(CurrentContext, new InvocationState
{
ReturnType = typeof(void),
Callback = onComplete
});
CallContract(hash, method, new VMArray(args));
}

internal void CallFromNativeContract<T>(Action<T> onComplete, UInt160 hash, string method, params StackItem[] args)
{
invocationStates.Add(CurrentContext, new InvocationState
{
ReturnType = typeof(T),
Callback = onComplete
});
CallContract(hash, method, new VMArray(args));
}

protected override void ContextUnloaded(ExecutionContext context)
{
base.ContextUnloaded(context);
if (CurrentContext != null && context.EvaluationStack != CurrentContext.EvaluationStack)
if (context.EvaluationStack.Count == 0)
Push(StackItem.Null);
if (!(UncaughtException is null)) return;
if (invocationStates.Count == 0) return;
if (!invocationStates.Remove(CurrentContext, out InvocationState state)) return;
switch (state.Callback)
{
case null:
break;
case Action action:
action();
break;
default:
state.Callback.DynamicInvoke(Convert(Pop(), new InteropParameterDescriptor(state.ReturnType)));
break;
}
}

protected override void LoadContext(ExecutionContext context)
Expand Down
16 changes: 11 additions & 5 deletions src/neo/SmartContract/InteropParameterDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ internal class InteropParameterDescriptor
[typeof(VM.Types.Pointer)] = p => p,
[typeof(VM.Types.Array)] = p => p,
[typeof(InteropInterface)] = p => p,
[typeof(bool)] = p => p.ToBoolean(),
[typeof(sbyte)] = p => (sbyte)p.GetBigInteger(),
[typeof(byte)] = p => (byte)p.GetBigInteger(),
[typeof(short)] = p => (short)p.GetBigInteger(),
Expand All @@ -40,20 +41,25 @@ internal class InteropParameterDescriptor
};

public InteropParameterDescriptor(ParameterInfo parameterInfo)
: this(parameterInfo.ParameterType)
{
Name = parameterInfo.Name;
Type = parameterInfo.ParameterType;
this.Name = parameterInfo.Name;
}

public InteropParameterDescriptor(Type type)
{
this.Type = type;
if (IsEnum)
{
Converter = converters[Type.GetEnumUnderlyingType()];
Converter = converters[type.GetEnumUnderlyingType()];
}
else if (IsArray)
{
Converter = converters[Type.GetElementType()];
Converter = converters[type.GetElementType()];
}
else
{
IsInterface = !converters.TryGetValue(Type, out var converter);
IsInterface = !converters.TryGetValue(type, out var converter);
if (IsInterface)
Converter = converters[typeof(InteropInterface)];
else
Expand Down

0 comments on commit d275916

Please sign in to comment.