Beispiel #1
0
        private bool CheckStackSize()
        {
            const uint MaxStackSize = 2 * 1024;

            if (CurrentContext.InstructionPointer >= CurrentContext.Script.Length)
            {
                return(true);
            }
            int    size   = 0;
            OpCode opcode = CurrentContext.NextInstruction;

            if (opcode <= OpCode.PUSH16)
            {
                size = 1;
            }
            else
            {
                switch (opcode)
                {
                case OpCode.DEPTH:
                case OpCode.DUP:
                case OpCode.OVER:
                case OpCode.TUCK:
                    size = 1;
                    break;

                case OpCode.UNPACK:
                    StackItem item = EvaluationStack.Peek();
                    if (!item.IsArray)
                    {
                        return(false);
                    }
                    size = item.GetArray().Length;
                    break;
                }
            }
            if (size == 0)
            {
                return(true);
            }
            size += EvaluationStack.Count + AltStack.Count;
            if (size > MaxStackSize)
            {
                return(false);
            }
            return(true);
        }
Beispiel #2
0
        public static ContractParameter ToParameter(this StackItem item)
        {
            switch (item.GetType().Name)
            {
            case "Array":
            case "Struct":
                return(new ContractParameter
                {
                    Type = ContractParameterType.Array,
                    Value = item.GetArray().Select(p => p.ToParameter()).ToArray()
                });

            case "Boolean":
                return(new ContractParameter
                {
                    Type = ContractParameterType.Boolean,
                    Value = item.GetBoolean()
                });

            case "ByteArray":
                return(new ContractParameter
                {
                    Type = ContractParameterType.ByteArray,
                    Value = item.GetByteArray()
                });

            case "Integer":
                return(new ContractParameter
                {
                    Type = ContractParameterType.Integer,
                    Value = item.GetBigInteger()
                });

            case "InteropInterface":
                return(new ContractParameter
                {
                    Type = ContractParameterType.InteropInterface
                });

            default:
                throw new ArgumentException();
            }
        }
Beispiel #3
0
        private bool CheckStackSize(OpCode nextInstruction)
        {
            int size = 0;

            if (nextInstruction <= OpCode.PUSH16)
            {
                size = 1;
            }
            else
            {
                switch (nextInstruction)
                {
                case OpCode.DEPTH:
                case OpCode.DUP:
                case OpCode.OVER:
                case OpCode.TUCK:
                    size = 1;
                    break;

                case OpCode.UNPACK:
                    StackItem item = EvaluationStack.Peek();
                    if (!item.IsArray)
                    {
                        return(false);
                    }
                    size = item.GetArray().Count;
                    break;
                }
            }
            if (size == 0)
            {
                return(true);
            }
            size += EvaluationStack.Count + AltStack.Count;
            if (size > MaxStackSize)
            {
                return(false);
            }
            return(true);
        }
Beispiel #4
0
        /// <summary>
        /// Get Printable version
        /// </summary>
        /// <param name="value">Value</param>
        object GetPrintable(StackItem value)
        {
            if (value == null)
            {
                return("NULL");
            }
            else
            {
                Type tp = value.GetType();

                switch (tp.FullName)
                {
                case "Neo.VM.Types.Boolean": return(value.GetBoolean());

                case "Neo.VM.Types.ByteArray":
                {
                    byte[] v = value.GetByteArray();
                    if (v == null || v.Length == 0)
                    {
                        return("[0]");
                    }

                    if (v.Length > 200)
                    {
                        return("[" + v.Length.ToString() + "] ...");
                    }

                    return("[" + v.Length.ToString() + "] 0x" + v.ToHexString());
                }

                case "Neo.VM.Types.Integer":
                {
                    BigInteger v = value.GetBigInteger();

                    if (v.Sign >= 0 && v > ulong.MaxValue)
                    {
                        return("[BigInteger] ...");
                    }
                    else if (v.Sign < 0 && v < long.MinValue)
                    {
                        return("[-BigInteger] ...");
                    }

                    return(v.ToString());
                }

                case "Neo.VM.Types.Struct":
                case "Neo.VM.Types.Array":
                {
                    StackItem[] si = value.GetArray();

                    if (si.Length > 200)
                    {
                        return("[" + si.Length.ToString() + "] ...");
                    }
                    return
                        ("[" + si.Length + "]" +
                         JsonHelper.Serialize(si.Select(u => GetPrintable(u)).ToArray(), true, false));
                }

                case "Neo.VM.Types.InteropInterface":
                {
                    if (_Interop == null)
                    {
                        _Interop = tp.GetField("_object", BindingFlags.Instance | BindingFlags.NonPublic);

                        foreach (Type txp in typeof(ApplicationEngine).Assembly.GetTypes())
                        {
                            // Extract internal type
                            if (txp.FullName == "Neo.SmartContract.StorageContext")
                            {
                                _StorageContext        = txp;
                                _StorageContextToArray = txp.GetMethod("ToArray");
                                break;
                            }
                        }
                    }

                    object ob = _Interop.GetValue(value);

                    if (ob != null && ob.GetType() == _StorageContext)
                    {
                        ob = _StorageContextToArray.Invoke(ob, new object[] { });
                    }
                    else
                    {
                    }
                    //if (ob !=null && ob is UInt160 a)

                    return(JsonHelper.Serialize(ob, true, false));
                }

                default:
                {
                    break;
                }
                }
            }

            return("");
        }