示例#1
0
        public void AddTransferNotification(ExecutionEngine engine, UIntBase assetId, UInt160 from, UInt160 to, Fixed8 value)
        {
            VMArray array = new VMArray();

            array.Add("transfer");
            array.Add(new ByteArray(from.ToArray()));
            array.Add(new ByteArray(to.ToArray()));
            array.Add(new ByteArray(new BigInteger(value.GetData()).ToByteArray()));

            NotifyEventArgs notification = new NotifyEventArgs(engine.ScriptContainer, assetId, array);

            InvokeNotification(notification);
        }
示例#2
0
        private StackItem DeserializeStackItem(BinaryReader reader)
        {
            StackItemType type = (StackItemType)reader.ReadByte();

            switch (type)
            {
            case StackItemType.ByteArray:
                return(new ByteArray(reader.ReadVarBytes()));

            case StackItemType.Boolean:
                return(new VMBoolean(reader.ReadBoolean()));

            case StackItemType.Integer:
                return(new Integer(new BigInteger(reader.ReadVarBytes())));

            case StackItemType.Array:
            case StackItemType.Struct:
                VMArray array = type == StackItemType.Struct ? new Struct() : new VMArray();
                ulong   count = reader.ReadVarInt();
                while (count-- > 0)
                {
                    array.Add(DeserializeStackItem(reader));
                }
                return(array);

            default:
                return(null);
            }
        }
示例#3
0
        public static StackItem StackItemFromJson(JObject json)
        {
            StackItemType type = json["type"].TryGetEnum <StackItemType>();

            switch (type)
            {
            case StackItemType.Boolean:
                return(new Boolean(json["value"].AsBoolean()));

            case StackItemType.Buffer:
                return(new Buffer(Convert.FromBase64String(json["value"].AsString())));

            case StackItemType.ByteString:
                return(new ByteString(Convert.FromBase64String(json["value"].AsString())));

            case StackItemType.Integer:
                return(new Integer(new BigInteger(json["value"].AsNumber())));

            case StackItemType.Array:
                Array array = new Array();
                foreach (var item in (JArray)json["value"])
                {
                    array.Add(StackItemFromJson(item));
                }
                return(array);

            case StackItemType.Struct:
                Struct @struct = new Struct();
                foreach (var item in (JArray)json["value"])
                {
                    @struct.Add(StackItemFromJson(item));
                }
                return(@struct);

            case StackItemType.Map:
                Map map = new Map();
                foreach (var item in (JArray)json["value"])
                {
                    PrimitiveType key = (PrimitiveType)StackItemFromJson(item["key"]);
                    map[key] = StackItemFromJson(item["value"]);
                }
                return(map);

            case StackItemType.Pointer:
                return(new Pointer(null, (int)json["value"].AsNumber()));

            case StackItemType.InteropInterface:
                return(new InteropInterface(new object()));    // See https://github.com/neo-project/neo/blob/master/src/neo/VM/Helper.cs#L194
            }
            return(json["value"] is null ? StackItem.Null : json["value"].AsString());
        }
示例#4
0
        private StackItem DeserializeStackItem(BinaryReader reader)
        {
            StackItemType type = (StackItemType)reader.ReadByte();

            switch (type)
            {
            case StackItemType.ByteArray:
                return(new ByteArray(reader.ReadVarBytes()));

            case StackItemType.Boolean:
                return(new VMBoolean(reader.ReadBoolean()));

            case StackItemType.Integer:
                return(new Integer(new BigInteger(reader.ReadVarBytes())));

            case StackItemType.Array:
            case StackItemType.Struct:
            {
                VMArray array = type == StackItemType.Struct ? new Struct() : new VMArray();
                ulong   count = reader.ReadVarInt();
                while (count-- > 0)
                {
                    array.Add(DeserializeStackItem(reader));
                }
                return(array);
            }

            case StackItemType.Map:
            {
                Map   map   = new Map();
                ulong count = reader.ReadVarInt();
                while (count-- > 0)
                {
                    StackItem key   = DeserializeStackItem(reader);
                    StackItem value = DeserializeStackItem(reader);
                    map[key] = value;
                }
                return(map);
            }

            default:
                throw new FormatException();
            }
        }
示例#5
0
        public static StackItem Deserialize(BinaryReader reader, uint maxArraySize, uint maxItemSize, ReferenceCounter referenceCounter)
        {
            Stack <StackItem> deserialized = new Stack <StackItem>();
            int undeserialized             = 1;

            while (undeserialized-- > 0)
            {
                StackItemType type = (StackItemType)reader.ReadByte();
                switch (type)
                {
                case StackItemType.Any:
                    deserialized.Push(StackItem.Null);
                    break;

                case StackItemType.Boolean:
                    deserialized.Push(reader.ReadBoolean());
                    break;

                case StackItemType.Integer:
                    deserialized.Push(new BigInteger(reader.ReadVarBytes(Integer.MaxSize)));
                    break;

                case StackItemType.ByteString:
                    deserialized.Push(reader.ReadVarBytes((int)maxItemSize));
                    break;

                case StackItemType.Buffer:
                    Buffer buffer = new Buffer((int)reader.ReadVarInt(maxItemSize));
                    reader.FillBuffer(buffer.InnerBuffer);
                    deserialized.Push(buffer);
                    break;

                case StackItemType.Array:
                case StackItemType.Struct:
                {
                    int count = (int)reader.ReadVarInt(maxArraySize);
                    deserialized.Push(new ContainerPlaceholder(type, count));
                    undeserialized += count;
                }
                break;

                case StackItemType.Map:
                {
                    int count = (int)reader.ReadVarInt(maxArraySize);
                    deserialized.Push(new ContainerPlaceholder(type, count));
                    undeserialized += count * 2;
                }
                break;

                default:
                    throw new FormatException();
                }
            }
            Stack <StackItem> stack_temp = new Stack <StackItem>();

            while (deserialized.Count > 0)
            {
                StackItem item = deserialized.Pop();
                if (item is ContainerPlaceholder placeholder)
                {
                    switch (placeholder.Type)
                    {
                    case StackItemType.Array:
                        Array array = new Array(referenceCounter);
                        for (int i = 0; i < placeholder.ElementCount; i++)
                        {
                            array.Add(stack_temp.Pop());
                        }
                        item = array;
                        break;

                    case StackItemType.Struct:
                        Struct @struct = new Struct(referenceCounter);
                        for (int i = 0; i < placeholder.ElementCount; i++)
                        {
                            @struct.Add(stack_temp.Pop());
                        }
                        item = @struct;
                        break;

                    case StackItemType.Map:
                        Map map = new Map(referenceCounter);
                        for (int i = 0; i < placeholder.ElementCount; i++)
                        {
                            StackItem key   = stack_temp.Pop();
                            StackItem value = stack_temp.Pop();
                            map[(PrimitiveType)key] = value;
                        }
                        item = map;
                        break;
                    }
                }
                stack_temp.Push(item);
            }
            return(stack_temp.Peek());
        }
示例#6
0
        private StackItem DeserializeStackItem(BinaryReader reader, ExecutionEngine engine, uint maxItemSize)
        {
            Stack <StackItem> deserialized = new Stack <StackItem>();
            int undeserialized             = 1;

            while (undeserialized-- > 0)
            {
                StackItemType type = (StackItemType)reader.ReadByte();
                switch (type)
                {
                case StackItemType.ByteArray:
                    deserialized.Push(new ByteArray(reader.ReadVarBytes((int)maxItemSize)));
                    break;

                case StackItemType.Boolean:
                    deserialized.Push(new VMBoolean(reader.ReadBoolean()));
                    break;

                case StackItemType.Integer:
                    deserialized.Push(new Integer(new BigInteger(reader.ReadVarBytes(ExecutionEngine.MaxSizeForBigInteger))));
                    break;

                case StackItemType.Array:
                case StackItemType.Struct:
                {
                    int count = (int)reader.ReadVarInt(engine.MaxArraySize);
                    deserialized.Push(new ContainerPlaceholder
                        {
                            Type         = type,
                            ElementCount = count
                        });
                    undeserialized += count;
                }
                break;

                case StackItemType.Map:
                {
                    int count = (int)reader.ReadVarInt(engine.MaxArraySize);
                    deserialized.Push(new ContainerPlaceholder
                        {
                            Type         = type,
                            ElementCount = count
                        });
                    undeserialized += count * 2;
                }
                break;

                default:
                    throw new FormatException();
                }
            }
            Stack <StackItem> stack_temp = new Stack <StackItem>();

            while (deserialized.Count > 0)
            {
                StackItem item = deserialized.Pop();
                if (item is ContainerPlaceholder placeholder)
                {
                    switch (placeholder.Type)
                    {
                    case StackItemType.Array:
                        VMArray array = new VMArray();
                        for (int i = 0; i < placeholder.ElementCount; i++)
                        {
                            array.Add(stack_temp.Pop());
                        }
                        item = array;
                        break;

                    case StackItemType.Struct:
                        Struct @struct = new Struct();
                        for (int i = 0; i < placeholder.ElementCount; i++)
                        {
                            @struct.Add(stack_temp.Pop());
                        }
                        item = @struct;
                        break;

                    case StackItemType.Map:
                        Map map = new Map();
                        for (int i = 0; i < placeholder.ElementCount; i++)
                        {
                            StackItem key   = stack_temp.Pop();
                            StackItem value = stack_temp.Pop();
                            map.Add(key, value);
                        }
                        item = map;
                        break;
                    }
                }
                stack_temp.Push(item);
            }
            return(stack_temp.Peek());
        }