Beispiel #1
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="engine">Engine</param>
 /// <param name="methodName">Method name</param>
 /// <param name="methodHash">Method hash</param>
 /// <param name="result">Result</param>
 public SysCallArgs(ExecutionEngineBase engine, string methodName, uint methodHash, SysCallResult result)
 {
     Engine     = engine;
     MethodName = methodName;
     MethodHash = methodHash;
     Result     = result;
 }
Beispiel #2
0
        private bool NeoRuntimeLog(ExecutionEngineBase engine)
        {
            var ctx = engine.CurrentContext;

            if (ctx == null)
            {
                return(false);
            }

            if (!ctx.EvaluationStack.TryPop(out var stackItem))
            {
                return(false);
            }

            using (stackItem)
            {
                if (OnLog == null)
                {
                    return(true);
                }

                RaiseOnLog(new LogEventArgs(ctx.ScriptHash, stackItem.ToString() ?? ""));
            }

            return(true);
        }
Beispiel #3
0
        private static bool GetMapEnumerator(ExecutionEngineBase engine)
        {
            var stack = engine.CallingContext?.EvaluationStack;

            if (stack == null)
            {
                return(false);
            }
            if (!stack.TryPop(out var stackItem))
            {
                return(false);
            }

            using (stackItem)
            {
                if (!(stackItem is MapStackItemBase map))
                {
                    return(false);
                }

                stack.Push(stack.CreateInterop(new KeyEnumerator(map.GetEnumerator())));
            }

            return(true);
        }
Beispiel #4
0
        /// <summary>
        /// Invoke method
        /// </summary>
        /// <param name="method">Method</param>
        /// <param name="engine">Execution engine</param>
        /// <returns>Return false if something is wrong</returns>
        public bool Invoke(byte[] method, ExecutionEngineBase engine)
        {
            var hash = method.Length == 4
                ? BitConverter.ToUInt32(method, 0)
                : GetMethodHash(Encoding.ASCII.GetString(method));

            void InvokeOnSysCall(SysCallResult result) => OnSysCall?.Invoke(this, new SysCallArgs(engine, GetMethodName(hash), hash, result));

            if (!_entries.TryGetValue(hash, out var entry))
            {
                InvokeOnSysCall(SysCallResult.NotFound);

                return(false);
            }

            if (!engine.IncreaseGas(entry.GasCost))
            {
                InvokeOnSysCall(SysCallResult.OutOfGas);

                return(false);
            }

            var ret = entry.MethodHandler(engine);

            InvokeOnSysCall(ret ? SysCallResult.True : SysCallResult.False);

            return(ret);
        }
Beispiel #5
0
        private static bool GetEntryScriptHash(ExecutionEngineBase engine)
        {
            var ctx = engine.EntryContext;

            if (ctx == null)
            {
                return(false);
            }

            ctx.EvaluationStack.Push(ctx.ScriptHash);

            return(true);
        }
Beispiel #6
0
        private static bool NeoRuntimeGetTrigger(ExecutionEngineBase engine)
        {
            var ctx = engine.CurrentContext;

            if (ctx == null)
            {
                return(false);
            }

            ctx.EvaluationStack.Push((int)engine.Trigger);

            return(true);
        }
Beispiel #7
0
        private static bool GetMapEnumeratorKey(ExecutionEngineBase engine)
        {
            var stack         = engine.CallingContext?.EvaluationStack;
            var keyEnumerator = stack?.PopObject <KeyEnumerator>();

            if (keyEnumerator == null)
            {
                return(false);
            }

            stack.Push(keyEnumerator.CurrentKey);

            return(false);
        }
Beispiel #8
0
        private static bool GetEnumeratorValue(ExecutionEngineBase engine)
        {
            var stack      = engine.CallingContext?.EvaluationStack;
            var enumerator = stack?.PopObject <IEnumerator <StackItemBase> >();

            if (enumerator == null)
            {
                return(false);
            }

            stack.Push(enumerator.Current);

            return(true);
        }
Beispiel #9
0
        private static bool GetValueEnumerator(ExecutionEngineBase engine)
        {
            var stack         = engine.CallingContext?.EvaluationStack;
            var keyEnumerator = stack?.PopObject <KeyEnumerator>();

            if (keyEnumerator == null)
            {
                return(false);
            }

            var projectingEnumerator = new ProjectingEnumerator <KeyEnumerator>(keyEnumerator, ke => ke.Current);

            stack.Push(stack.CreateInterop(projectingEnumerator));

            return(true);
        }
Beispiel #10
0
        private static bool GetScriptContainer(ExecutionEngineBase engine)
        {
            if (engine.MessageProvider == null)
            {
                return(false);
            }

            var ctx = engine.CurrentContext;

            if (ctx == null)
            {
                return(false);
            }

            ctx.EvaluationStack.PushObject(engine.MessageProvider);

            return(true);
        }
Beispiel #11
0
        private static bool RuntimeDeserialize(ExecutionEngineBase engine)
        {
            var ctx = engine.CurrentContext;

            if (ctx == null)
            {
                return(false);
            }

            var stack = ctx.EvaluationStack;

            byte[] data;

            using (var stackItem = stack.Pop())
            {
                data = stackItem.ToByteArray();

                if (data == null)
                {
                    return(false);
                }
            }

            using (var ms = new MemoryStream(data, false))
                using (var reader = new BinaryReader(ms))
                {
                    StackItemBase item;

                    try
                    {
                        item = Serializer.Deserialize(stack, reader);
                    }
                    catch
                    {
                        return(false);
                    }

                    stack.Push(item);
                }

            return(true);
        }
Beispiel #12
0
        private bool NeoRuntimeNotify(ExecutionEngineBase engine)
        {
            var ctx = engine.CurrentContext;

            if (ctx == null)
            {
                return(false);
            }

            if (!ctx.EvaluationStack.TryPop(out var stackItem))
            {
                return(false);
            }

            using (stackItem)
            {
                RaiseOnNotify(new NotifyEventArgs(ctx.ScriptHash, stackItem));
            }

            return(true);
        }
Beispiel #13
0
        private static bool ConcatEnumerators(ExecutionEngineBase engine)
        {
            var stack       = engine.CallingContext?.EvaluationStack;
            var enumerator1 = stack?.PopObject <IEnumerator <StackItemBase> >();

            if (enumerator1 == null)
            {
                return(false);
            }
            var enumerator2 = stack.PopObject <IEnumerator <StackItemBase> >();

            if (enumerator2 == null)
            {
                return(false);
            }

            var concatenatedEnumerator = new ConcatenatedEnumerator(enumerator1, enumerator2);

            stack.Push(stack.CreateInterop(concatenatedEnumerator));

            return(true);
        }
Beispiel #14
0
        private static bool RuntimeSerialize(ExecutionEngineBase engine)
        {
            var ctx = engine.CurrentContext;

            if (ctx == null)
            {
                return(false);
            }

            var stack = ctx.EvaluationStack;

            using (var ms = new MemoryStream())
                using (var writer = new BinaryWriter(ms))
                {
                    try
                    {
                        using (var stackItem = stack.Pop())
                        {
                            Serializer.Serialize(stackItem, writer);
                        }
                    }
                    catch
                    {
                        return(false);
                    }

                    writer.Flush();

                    if (ms.Length > MaxItemSize)
                    {
                        return(false);
                    }

                    stack.Push(ms.ToArray());
                }

            return(true);
        }