示例#1
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);
 }
示例#2
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);
        }
示例#3
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");
            }
        }
        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)
                };
            }));
        }
示例#5
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");
                }
            }
        }
示例#6
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");
            }
        }