Пример #1
0
        public static TItem Get <TItem>(this IDb db, Keccak key, IRlpStreamDecoder <TItem> decoder, ICache <Keccak, TItem> cache = null, bool shouldCache = true) where TItem : class
        {
            TItem item = cache?.Get(key);

            if (item is null)
            {
                if (db is IDbWithSpan spanDb && decoder is IRlpValueDecoder <TItem> valueDecoder)
                {
                    Span <byte> data = spanDb.GetSpan(key);
                    if (data.IsNullOrEmpty())
                    {
                        return(null);
                    }

                    try
                    {
                        var rlpValueContext = data.AsRlpValueContext();
                        item = valueDecoder.Decode(ref rlpValueContext, RlpBehaviors.AllowExtraData);
                    }
                    finally
                    {
                        spanDb.DangerousReleaseMemory(data);
                    }
                }
                else
                {
                    byte[]? data = db.Get(key);
                    if (data is null)
                    {
                        return(null);
                    }

                    item = decoder.Decode(data.AsRlpStream(), RlpBehaviors.AllowExtraData);
                }
            }
Пример #2
0
        public static T[] DecodeArray <T>(this IRlpStreamDecoder <T> decoder, RlpStream rlpStream, RlpBehaviors rlpBehaviors = RlpBehaviors.None)
        {
            int checkPosition = rlpStream.ReadSequenceLength() + rlpStream.Position;

            T[] result = new T[rlpStream.ReadNumberOfItemsRemaining(checkPosition)];
            for (int i = 0; i < result.Length; i++)
            {
                result[i] = decoder.Decode(rlpStream, rlpBehaviors);
            }

            return(result);
        }