Esempio n. 1
0
        public void Can_do_roundtrip_ref_struct()
        {
            LogEntry logEntry = new LogEntry(TestItem.AddressA, new byte[] { 1, 2, 3 }, new[] { TestItem.KeccakA, TestItem.KeccakB });
            Rlp      rlp      = Rlp.Encode(logEntry);

            Rlp.ValueDecoderContext valueDecoderContext = new Rlp.ValueDecoderContext(rlp.Bytes);
            LogEntryDecoder.Instance.DecodeStructRef(ref valueDecoderContext, RlpBehaviors.None, out var decoded);

            Assert.That(Bytes.AreEqual(logEntry.Data, decoded.Data), "data");
            Assert.That(logEntry.LoggersAddress == decoded.LoggersAddress, "address");

            KeccaksIterator iterator = new KeccaksIterator(decoded.TopicsRlp);

            for (int i = 0; i < logEntry.Topics.Length; i++)
            {
                iterator.TryGetNext(out var keccak);
                Assert.That(logEntry.Topics[i] == keccak, $"topics[{i}]");
            }
        }
Esempio n. 2
0
        private static TxReceipt DeserializeReceiptObsolete(Keccak hash, Span <byte> receiptData)
        {
            if (!receiptData.IsNullOrEmpty())
            {
                var context = new Rlp.ValueDecoderContext(receiptData);
                try
                {
                    var receipt = StorageDecoder.Decode(ref context, RlpBehaviors.Storage);
                    receipt.TxHash = hash;
                    return(receipt);
                }
                catch (RlpException)
                {
                    context.Position = 0;
                    var receipt = StorageDecoder.Decode(ref context);
                    receipt.TxHash = hash;
                    return(receipt);
                }
            }

            return(null);
        }
Esempio n. 3
0
        public FrameInfo ReadFrameHeader(IByteBuffer input)
        {
            input.ReadBytes(HeaderBytes);
            int frameSize = HeaderBytes[0] & 0xFF;

            frameSize = (frameSize << 8) + (HeaderBytes[1] & 0xFF);
            frameSize = (frameSize << 8) + (HeaderBytes[2] & 0xFF);

            Rlp.ValueDecoderContext headerBodyItems = HeaderBytes.AsSpan(3, 13).AsRlpValueContext();
            int headerDataEnd = headerBodyItems.ReadSequenceLength() + headerBodyItems.Position;
            int numberOfItems = headerBodyItems.ReadNumberOfItemsRemaining(headerDataEnd);

            headerBodyItems.DecodeInt(); // not needed - adaptive IDs - DO NOT COMMENT OUT!!! - decode takes int of the RLP sequence and moves the position
            int?contextId = numberOfItems > 1 ? headerBodyItems.DecodeInt() : (int?)null;

            _currentContextId = contextId;
            int?totalPacketSize = numberOfItems > 2 ? headerBodyItems.DecodeInt() : (int?)null;

            bool isChunked = totalPacketSize.HasValue || contextId.HasValue && _currentContextId == contextId && contextId != 0;
            bool isFirst   = totalPacketSize.HasValue || !isChunked;

            return(new FrameInfo(isChunked, isFirst, frameSize, totalPacketSize ?? frameSize));
        }
Esempio n. 4
0
 public KeccaksIterator(Span <byte> data)
 {
     _decoderContext = new Rlp.ValueDecoderContext(data);
     _length         = _decoderContext.ReadSequenceLength();
     Index           = -1;
 }
Esempio n. 5
0
        protected override void Decode(IChannelHandlerContext context, byte[] input, List <object> output)
        {
            if (_logger.IsTrace)
            {
                _logger.Trace("Merging frames");
            }

            Rlp.ValueDecoderContext headerBodyItems = input.AsSpan(3, 13).AsRlpValueContext();
            int headerDataEnd = headerBodyItems.ReadSequenceLength() + headerBodyItems.Position;
            int numberOfItems = headerBodyItems.ReadNumberOfItemsRemaining(headerDataEnd);

            headerBodyItems.DecodeInt(); // not needed - adaptive IDs - DO NOT COMMENT OUT!!! - decode takes int of the RLP sequence and moves the position
            int?contextId       = numberOfItems > 1 ? headerBodyItems.DecodeInt() : (int?)null;
            int?totalPacketSize = numberOfItems > 2 ? headerBodyItems.DecodeInt() : (int?)null;

            bool isChunked = totalPacketSize.HasValue ||
                             contextId.HasValue && _currentSizes.ContainsKey(contextId.Value);

            if (isChunked)
            {
                if (_logger.IsTrace)
                {
                    _logger.Trace("Merging chunked packet");
                }

                bool isFirstChunk = totalPacketSize.HasValue;
                if (isFirstChunk)
                {
                    _currentSizes[contextId.Value]      = 0;
                    _totalPayloadSizes[contextId.Value] = totalPacketSize.Value - 1;                                                              // packet type data size
                    _packets[contextId.Value]           = new Packet("???", GetPacketType(input), new byte[_totalPayloadSizes[contextId.Value]]); // adaptive IDs
                    _payloads[contextId.Value]          = new List <byte[]>();
                }

                int packetTypeDataSize = isFirstChunk ? 1 : 0;
                int frameSize          = input.Length - HeaderSize - FrameMacSize - packetTypeDataSize;
                _payloads[contextId.Value].Add(input.Slice(32 + packetTypeDataSize, frameSize));
                _currentSizes[contextId.Value] += frameSize;
                if (_currentSizes[contextId.Value] >= _totalPayloadSizes[contextId.Value])
                {
                    int    padding    = _currentSizes[contextId.Value] - _totalPayloadSizes[contextId.Value];
                    Packet packet     = _packets[contextId.Value];
                    int    offset     = 0;
                    int    frameCount = _payloads[contextId.Value].Count;
                    for (int i = 0; i < frameCount; i++)
                    {
                        int length = _payloads[contextId.Value][i].Length - (i == frameCount - 1 ? padding : 0);
                        Buffer.BlockCopy(_payloads[contextId.Value][i], 0, packet.Data, offset, length);
                        offset += length;
                    }

                    output.Add(packet);
                    _currentSizes.Remove(contextId.Value);
                    _totalPayloadSizes.Remove(contextId.Value);
                    _payloads.Remove(contextId.Value);
                    _packets.Remove(contextId.Value);
                }
            }
            else
            {
                int totalBodySize = input[0] & 0xFF;
                totalBodySize = (totalBodySize << 8) + (input[1] & 0xFF);
                totalBodySize = (totalBodySize << 8) + (input[2] & 0xFF);

                if (_logger.IsTrace)
                {
                    _logger.Trace($"Merging single frame packet of length {totalBodySize - 1}");
                }

                output.Add(new Packet("???", GetPacketType(input), input.Slice(1 + 32, totalBodySize - 1))); // ??? protocol because of adaptive IDs
            }
        }
Esempio n. 6
0
        public BlockHeader Decode(Rlp.ValueDecoderContext decoderContext, RlpBehaviors rlpBehaviors = RlpBehaviors.None)
        {
            if (decoderContext.IsNextItemNull())
            {
                return(null);
            }

            var headerRlp            = decoderContext.PeekNextItem();
            int headerSequenceLength = decoderContext.ReadSequenceLength();
            int headerCheck          = decoderContext.Position + headerSequenceLength;

            Keccak  parentHash       = decoderContext.DecodeKeccak();
            Keccak  ommersHash       = decoderContext.DecodeKeccak();
            Address beneficiary      = decoderContext.DecodeAddress();
            Keccak  stateRoot        = decoderContext.DecodeKeccak();
            Keccak  transactionsRoot = decoderContext.DecodeKeccak();
            Keccak  receiptsRoot     = decoderContext.DecodeKeccak();
            Bloom   bloom            = decoderContext.DecodeBloom();
            UInt256 difficulty       = decoderContext.DecodeUInt256();
            UInt256 number           = decoderContext.DecodeUInt256();
            UInt256 gasLimit         = decoderContext.DecodeUInt256();
            UInt256 gasUsed          = decoderContext.DecodeUInt256();
            UInt256 timestamp        = decoderContext.DecodeUInt256();

            byte[] extraData = decoderContext.DecodeByteArray();

            BlockHeader blockHeader = new BlockHeader(
                parentHash,
                ommersHash,
                beneficiary,
                difficulty,
                (long)number,
                (long)gasLimit,
                timestamp,
                extraData)
            {
                StateRoot    = stateRoot,
                TxRoot       = transactionsRoot,
                ReceiptsRoot = receiptsRoot,
                Bloom        = bloom,
                GasUsed      = (long)gasUsed,
                Hash         = Keccak.Compute(headerRlp)
            };

            if (decoderContext.PeekPrefixAndContentLength().ContentLength == Keccak.Size)
            {
                blockHeader.MixHash = decoderContext.DecodeKeccak();
                blockHeader.Nonce   = (ulong)decoderContext.DecodeUBigInt();
            }
            else
            {
                blockHeader.AuRaStep      = (long)decoderContext.DecodeUInt256();
                blockHeader.AuRaSignature = decoderContext.DecodeByteArray();
            }

            if ((rlpBehaviors & RlpBehaviors.AllowExtraData) != RlpBehaviors.AllowExtraData)
            {
                decoderContext.Check(headerCheck);
            }

            return(blockHeader);
        }
Esempio n. 7
0
 public UserOperationWithEntryPoint Decode(ref Rlp.ValueDecoderContext decoderContext, RlpBehaviors rlpBehaviors = RlpBehaviors.None)
 {
     throw new System.NotImplementedException();
 }