Esempio n. 1
0
 static bool IsScript(MichelineArray node)
 {
     return(node.Count >= 3 &&
            node.Any(x => x is MichelinePrim p && p.Prim == PrimType.parameter) &&
            node.Any(x => x is MichelinePrim p && p.Prim == PrimType.storage) &&
            node.Any(x => x is MichelinePrim p && p.Prim == PrimType.code));
 }
Esempio n. 2
0
 public override IMicheline Optimize(IMicheline value)
 {
     if (value is MichelineArray micheArray)
     {
         var res = new MichelineArray(micheArray.Count);
         foreach (var item in micheArray)
         {
             res.Add(Item.Optimize(item));
         }
         return(res);
     }
     return(value);
 }
Esempio n. 3
0
        public MichelineArray ReadMichelineArray()
        {
            var arrayData = ReadArray();

            var res = new MichelineArray();

            using (var arr = new ForgedReader(arrayData))
            {
                while (!arr.EndOfStream)
                {
                    res.Add(arr.ReadMicheline());
                }
            }

            return(res);
        }
Esempio n. 4
0
        void WriteMap(Utf8JsonWriter writer, IFlat key, MichelineArray items)
        {
            writer.WriteStartObject();

            foreach (var item in items)
            {
                if (!(item is MichelinePrim elt) || elt.Prim != PrimType.Elt || elt.Args?.Count != 2)
                {
                    throw new FormatException($"Invalid big_map item {(item as MichelinePrim)?.Prim.ToString() ?? item.Type.ToString()}");
                }

                writer.WritePropertyName(key.Flatten(elt.Args[0]));
                Value.WriteValue(writer, elt.Args[1]);
            }

            writer.WriteEndObject();
        }
Esempio n. 5
0
        protected override IMicheline MapValue(object value)
        {
            switch (value)
            {
            case IEnumerable e:
                var arr1 = new MichelineArray();
                foreach (var item in e)
                {
                    arr1.Add(Item.MapObject(item, true));
                }
                return(arr1);

            case JsonElement json when json.ValueKind == JsonValueKind.Array:
                var arr2 = new MichelineArray();
                foreach (var item in json.EnumerateArray())
                {
                    arr2.Add(Item.MapObject(item, true));
                }
                return(arr2);

            default:
                throw MapFailedException("invalid value");
            }
        }
Esempio n. 6
0
        void WriteKeyValues(Utf8JsonWriter writer, MichelineArray items)
        {
            writer.WriteStartArray();

            foreach (var item in items)
            {
                writer.WriteStartObject();

                if (!(item is MichelinePrim elt) || elt.Prim != PrimType.Elt || elt.Args?.Count != 2)
                {
                    throw new FormatException($"Invalid map item {(item as MichelinePrim)?.Prim.ToString() ?? item.Type.ToString()}");
                }

                writer.WritePropertyName("key");
                Key.WriteValue(writer, elt.Args[0]);

                writer.WritePropertyName("value");
                Value.WriteValue(writer, elt.Args[1]);

                writer.WriteEndObject();
            }

            writer.WriteEndArray();
        }
        public async Task <IEnumerable <OriginationOperation> > GetOriginations(string hash, MichelineFormat format, Symbols quote)
        {
            var sql = $@"
                SELECT      o.*, b.""Hash"", sc.""ParameterSchema"", sc.""StorageSchema"", sc.""CodeSchema"", sc.""Views""
                FROM        ""OriginationOps"" as o
                INNER JOIN  ""Blocks"" as b 
                        ON  b.""Level"" = o.""Level""
                LEFT  JOIN  ""Scripts"" as sc
                        ON  sc.""Id"" = o.""ScriptId""
                WHERE       o.""OpHash"" = @hash::character(51)
                ORDER BY    o.""Id""";

            using var db = GetConnection();
            var rows = await db.QueryAsync(sql, new { hash });

            #region include storage
            var storages = await AccountRepository.GetStorages(db,
                                                               rows.Where(x => x.StorageId != null)
                                                               .Select(x => (int)x.StorageId)
                                                               .Distinct()
                                                               .ToList(),
                                                               format);

            #endregion

            #region include diffs
            var diffs = await BigMapsRepository.GetOriginationDiffs(db,
                                                                    rows.Where(x => x.BigMapUpdates != null)
                                                                    .Select(x => (int)x.Id)
                                                                    .ToList(),
                                                                    format);

            #endregion

            return(rows.Select(row =>
            {
                var contract = row.ContractId == null ? null
                    : (RawContract)Accounts.Get((int)row.ContractId);

                MichelineArray code = null;
                if (row.ParameterSchema != null)
                {
                    code = new();
                    code.Add(Micheline.FromBytes(row.ParameterSchema));
                    code.Add(Micheline.FromBytes(row.StorageSchema));
                    if (row.Views != null)
                    {
                        code.AddRange(((byte[][])row.Views).Select(x => Micheline.FromBytes(x)));
                    }
                    code.Add(Micheline.FromBytes(row.CodeSchema));
                }

                return new OriginationOperation
                {
                    Id = row.Id,
                    Level = row.Level,
                    Block = row.Hash,
                    Timestamp = row.Timestamp,
                    Hash = hash,
                    Initiator = row.InitiatorId != null ? Accounts.GetAlias(row.InitiatorId) : null,
                    Sender = Accounts.GetAlias(row.SenderId),
                    Counter = row.Counter,
                    Nonce = row.Nonce,
                    GasLimit = row.GasLimit,
                    GasUsed = row.GasUsed,
                    StorageLimit = row.StorageLimit,
                    StorageUsed = row.StorageUsed,
                    BakerFee = row.BakerFee,
                    StorageFee = row.StorageFee ?? 0,
                    AllocationFee = row.AllocationFee ?? 0,
                    ContractDelegate = row.DelegateId != null ? Accounts.GetAlias(row.DelegateId) : null,
                    ContractBalance = row.Balance,
                    Code = (int)format % 2 == 0 ? code : code.ToJson(),
                    Storage = row.StorageId == null ? null : storages?[row.StorageId],
                    Diffs = diffs?.GetValueOrDefault((int)row.Id),
                    Status = OpStatuses.ToString(row.Status),
                    OriginatedContract = contract == null ? null :
                                         new OriginatedContract
                    {
                        Alias = contract.Alias,
                        Address = contract.Address,
                        Kind = contract.KindString,
                        TypeHash = contract.TypeHash,
                        CodeHash = contract.CodeHash
                    },
                    ContractManager = row.ManagerId != null ? Accounts.GetAlias(row.ManagerId) : null,
                    Errors = row.Errors != null ? OperationErrorSerializer.Deserialize(row.Errors) : null,
                    Quote = Quotes.Get(quote, row.Level)
                };
            }));
        }
Esempio n. 8
0
        static IMicheline ReadFlat(BinaryReader reader)
        {
            Stack <IMicheline> stack = new();
            IMicheline         node, top;
            MichelinePrim      prim;
            MichelineArray     arr;
            int tag, cnt, args, annots;

start:
            tag = reader.ReadByte();
            if (tag >= 0x80)
            {
                prim = new() { Prim = (PrimType)reader.ReadByte() };

                annots = tag & 0x0F;
                if (annots > 0)
                {
                    if (annots == 0x0F)
                    {
                        annots = reader.Read7BitInt();
                    }
                    prim.Annots = new(annots);
                }

                args = (tag & 0x70) >> 4;
                if (args > 0)
                {
                    if (args == 0x07)
                    {
                        args = reader.Read7BitInt();
                    }
                    prim.Args = new(args);

                    stack.Push(prim);
                    goto start;
                }

                if (prim.Annots != null)
                {
                    ReadAnnots(reader, prim);
                }

                node = prim;
            }
            else
            {
                cnt = tag & 0x1F;
                if (cnt == 0x1F)
                {
                    cnt = reader.Read7BitInt();
                }

                switch ((MichelineType)(tag & 0xE0))
                {
                case MichelineType.Array:
                    node = new MichelineArray(cnt);
                    if (cnt > 0)
                    {
                        stack.Push(node);
                        goto start;
                    }
                    break;

                case MichelineType.Bytes:
                    node = new MichelineBytes(reader.ReadBytes(cnt));
                    break;

                case MichelineType.Int:
                    node = new MichelineInt(new BigInteger(reader.ReadBytes(cnt)));
                    break;

                case MichelineType.String:
                    node = new MichelineString(Utf8.Convert(reader.ReadBytes(cnt)));
                    break;

                default:
                    throw new FormatException("Invalid micheline tag");
                }
            }
finish:
            if (stack.Count == 0)
            {
                return(node);
            }
            top = stack.Peek();
            if (top is MichelinePrim p)
            {
                p.Args.Add(node);
                if (p.Args.Count < p.Args.Capacity)
                {
                    goto start;
                }
                if (p.Annots != null)
                {
                    ReadAnnots(reader, p);
                }
            }
            else
            {
                arr = (MichelineArray)top;
                arr.Add(node);
                if (arr.Count < arr.Capacity)
                {
                    goto start;
                }
            }
            node = stack.Pop();
            goto finish;
        }
Esempio n. 9
0
        public static IMicheline Read(BinaryReader reader, int depth = 0)
        {
            var tag = reader.ReadByte();

            if (tag >= 0x80)
            {
                var prim = new MichelinePrim {
                    Prim = (PrimType)reader.ReadByte()
                };

                var args = (tag & 0x70) >> 4;
                if (args > 0)
                {
                    if (args == 0x07)
                    {
                        args = reader.Read7BitInt();
                    }

                    prim.Args = new List <IMicheline>(args);
                    if (depth < Micheline.MaxRecursionDepth)
                    {
                        while (args-- > 0)
                        {
                            prim.Args.Add(Read(reader, depth + 1));
                        }
                    }
                    else
                    {
                        while (args-- > 0)
                        {
                            prim.Args.Add(ReadFlat(reader));
                        }
                    }
                }

                var annots = tag & 0x0F;
                if (annots > 0)
                {
                    if (annots == 0x0F)
                    {
                        annots = reader.Read7BitInt();
                    }

                    prim.Annots = new List <IAnnotation>(annots);
                    while (annots-- > 0)
                    {
                        prim.Annots.Add(ReadAnnotation(reader));
                    }
                }

                return(prim);
            }
            else
            {
                var cnt = tag & 0x1F;
                if (cnt == 0x1F)
                {
                    cnt = reader.Read7BitInt();
                }

                switch ((MichelineType)(tag & 0xE0))
                {
                case MichelineType.Array:
                    var arr = new MichelineArray(cnt);
                    if (depth < Micheline.MaxRecursionDepth)
                    {
                        while (cnt-- > 0)
                        {
                            arr.Add(Read(reader, depth + 1));
                        }
                    }
                    else
                    {
                        while (cnt-- > 0)
                        {
                            arr.Add(ReadFlat(reader));
                        }
                    }
                    return(arr);

                case MichelineType.Bytes:
                    return(new MichelineBytes(reader.ReadBytes(cnt)));

                case MichelineType.Int:
                    return(new MichelineInt(new BigInteger(reader.ReadBytes(cnt))));

                case MichelineType.String:
                    return(new MichelineString(Utf8.Convert(reader.ReadBytes(cnt))));

                default:
                    throw new FormatException("Invalid micheline tag");
                }
            }
        }
Esempio n. 10
0
        protected override IMicheline MapValue(object value)
        {
            switch (value)
            {
            case IEnumerable e:
                var arr1 = new MichelineArray();
                foreach (var item in e)
                {
                    var type    = item.GetType();
                    var keyProp = type.GetProperty("Key") ?? type.GetProperty("key")
                                  ?? throw MapFailedException("missed 'key' property");
                    var valueProp = type.GetProperty("Value") ?? type.GetProperty("value")
                                    ?? throw MapFailedException("missed 'value' property");

                    arr1.Add(new MichelinePrim
                    {
                        Prim = PrimType.Elt,
                        Args = new List <IMicheline>(2)
                        {
                            Key.MapObject(keyProp.GetValue(item), true),
                            Value.MapObject(valueProp.GetValue(item), true)
                        }
                    });
                }
                return(arr1);

            case JsonElement json when json.ValueKind == JsonValueKind.Object:
                var arr2 = new MichelineArray();
                foreach (var item in json.EnumerateObject())
                {
                    arr2.Add(new MichelinePrim
                    {
                        Prim = PrimType.Elt,
                        Args = new List <IMicheline>(2)
                        {
                            Key.MapObject(item.Name, true),
                            Value.MapObject(item.Value, true)
                        }
                    });
                }
                return(arr2);

            case JsonElement json when json.ValueKind == JsonValueKind.Array:
                var arr3 = new MichelineArray();
                foreach (var item in json.EnumerateArray())
                {
                    if (!item.TryGetProperty("key", out var key) && !item.TryGetProperty("Key", out key))
                    {
                        throw MapFailedException("missed 'key' property");
                    }
                    if (!item.TryGetProperty("value", out var val) && !item.TryGetProperty("Value", out val))
                    {
                        throw MapFailedException("missed 'value' property");
                    }

                    arr3.Add(new MichelinePrim
                    {
                        Prim = PrimType.Elt,
                        Args = new List <IMicheline>(2)
                        {
                            Key.MapObject(key, true),
                            Value.MapObject(val, true)
                        }
                    });
                }
                return(arr3);

            default:
                throw MapFailedException("invalid value");
            }
        }
Esempio n. 11
0
 protected override IEnumerable <BigMapDiff> ParseBigMapDiffs(OriginationOperation origination, JsonElement result, MichelineArray code, IMicheline storage)
 {
     return(result.TryGetProperty("big_map_diff", out var diffs)
         ? diffs.RequiredArray().EnumerateArray().Select(BigMapDiff.Parse)
         : null);
 }