Exemple #1
0
        private static ParityTraceAction DecodeAction(Rlp.DecoderContext context)
        {
            ParityTraceAction action = new ParityTraceAction();
            int sequenceLength       = context.ReadSequenceLength();

            if (context.ReadNumberOfItemsRemaining(context.Position + sequenceLength) == 3)
            {
                action.CallType     = "reward";
                action.RewardType   = context.DecodeString();
                action.Author       = context.DecodeAddress();
                action.Value        = context.DecodeUInt256();
                action.TraceAddress = Array.Empty <int>();
            }
            else
            {
                action.CallType       = context.DecodeString();
                action.From           = context.DecodeAddress();
                action.To             = context.DecodeAddress();
                action.Value          = context.DecodeUInt256();
                action.Gas            = context.DecodeLong();
                action.Input          = context.DecodeByteArray();
                action.Result         = new ParityTraceResult();
                action.Result.Output  = context.DecodeByteArray();
                action.Result.GasUsed = context.DecodeLong();
                action.TraceAddress   = context.DecodeArray(c => c.DecodeInt());
                int subtracesCount = context.DecodeInt();
                action.Subtraces = new List <ParityTraceAction>(subtracesCount);
                for (int i = 0; i < subtracesCount; i++)
                {
                    action.Subtraces.Add(DecodeAction(context));
                }
            }

            return(action);
        }
Exemple #2
0
        private Dictionary <Address, Tally> DecodeTally(Rlp.DecoderContext context)
        {
            context.ReadSequenceLength();
            Dictionary <Address, Tally> tally = new Dictionary <Address, Tally>();
            int length = context.DecodeInt();

            for (int i = 0; i < length; i++)
            {
                Address address   = context.DecodeAddress();
                int     votes     = context.DecodeInt();
                bool    authorize = context.DecodeBool();
                Tally   tallyItem = new Tally(authorize);
                tallyItem.Votes = votes;
                tally[address]  = tallyItem;
            }
            return(tally);
        }
        public DisconnectMessage Deserialize(byte[] bytes)
        {
            Rlp.DecoderContext context = bytes.AsRlpContext();
            context.ReadSequenceLength();
            int reason = context.DecodeInt();
            DisconnectMessage disconnectMessage = new DisconnectMessage(reason);

            return(disconnectMessage);
        }
Exemple #4
0
        public StatusMessage Deserialize(byte[] bytes)
        {
            StatusMessage statusMessage = new StatusMessage();

            Rlp.DecoderContext context = bytes.AsRlpContext();
            context.ReadSequenceLength();
            statusMessage.ProtocolVersion = context.DecodeByte();
            statusMessage.ChainId         = context.DecodeInt();
            statusMessage.TotalDifficulty = context.DecodeUBigInt();
            statusMessage.BestHash        = context.DecodeKeccak();
            statusMessage.GenesisHash     = context.DecodeKeccak();
            return(statusMessage);
        }
Exemple #5
0
        private SortedList <Address, UInt256> DecodeSigners(Rlp.DecoderContext context)
        {
            context.ReadSequenceLength();
            SortedList <Address, UInt256> signers = new SortedList <Address, UInt256>(CliqueAddressComparer.Instance);
            int length = context.DecodeInt();

            for (int i = 0; i < length; i++)
            {
                Address signer   = context.DecodeAddress();
                UInt256 signedAt = context.DecodeUInt256();
                signers.Add(signer, signedAt);
            }

            return(signers);
        }
Exemple #6
0
        public GetBlockHeadersMessage Deserialize(byte[] bytes)
        {
            GetBlockHeadersMessage message = new GetBlockHeadersMessage();

            Rlp.DecoderContext context = bytes.AsRlpContext();
            context.ReadSequenceLength();
            int position = context.Position;

            byte[] startingBytes = context.DecodeByteArray();
            context.Position = position;
            if (startingBytes.Length == 32)
            {
                message.StartingBlockHash = context.DecodeKeccak();
            }
            else
            {
                message.StartingBlockNumber = (long)context.DecodeUInt256();
            }

            message.MaxHeaders = context.DecodeInt();
            message.Skip       = context.DecodeInt();
            message.Reverse    = context.DecodeByte();
            return(message);
        }
Exemple #7
0
        private List <Vote> DecodeVotes(Rlp.DecoderContext context)
        {
            context.ReadSequenceLength();
            List <Vote> votes  = new List <Vote>();
            int         length = context.DecodeInt();

            for (int i = 0; i < length; i++)
            {
                Address signer    = context.DecodeAddress();
                UInt256 block     = context.DecodeUInt256();
                Address address   = context.DecodeAddress();
                bool    authorize = context.DecodeBool();
                Vote    vote      = new Vote(signer, block, address, authorize);
                votes.Add(vote);
            }
            return(votes);
        }
        public AuthEip8Message Deserialize(byte[] data)
        {
            // TODO: this would not be compatible with future versions... ? if the length of prefixes changes
            Rlp.DecoderContext context     = data.AsRlpContext();
            AuthEip8Message    authMessage = new AuthEip8Message();

            context.ReadSequenceLength();
            byte[]    sigAllBytes = context.DecodeByteArray();
            Signature signature   = new Signature(sigAllBytes.Slice(0, 64), sigAllBytes[64]); // since Signature class is Ethereum style it expects V as the 64th byte, hence we use RecoveryID constructor

            authMessage.Signature = signature;
            authMessage.PublicKey = new PublicKey(context.DecodeByteArray());
            authMessage.Nonce     = context.DecodeByteArray();
            int version = context.DecodeInt();

            return(authMessage);
        }
        public DisconnectMessage Deserialize(byte[] bytes)
        {
            if (bytes.Length == 1)
            {
                return(new DisconnectMessage((DisconnectReason)bytes[0]));
            }

            if (bytes.SequenceEqual(strangeBytes))
            {
                return(new DisconnectMessage(DisconnectReason.StrangeBytes));
            }

            Rlp.DecoderContext context = bytes.AsRlpContext();
            context.ReadSequenceLength();
            int reason = context.DecodeInt();
            DisconnectMessage disconnectMessage = new DisconnectMessage(reason);

            return(disconnectMessage);
        }
        public HelloMessage Deserialize(byte[] bytes)
        {
            Rlp.DecoderContext context = bytes.AsRlpContext();
            context.ReadSequenceLength();

            HelloMessage helloMessage = new HelloMessage();

            helloMessage.P2PVersion   = context.DecodeByte();
            helloMessage.ClientId     = context.DecodeString();
            helloMessage.Capabilities = context.DecodeArray(ctx =>
            {
                ctx.ReadSequenceLength();
                string protocolCode = ctx.DecodeString();
                int version         = ctx.DecodeByte();
                return(new Capability(protocolCode, version));
            }).ToList();

            helloMessage.ListenPort = context.DecodeInt();
            helloMessage.NodeId     = new PublicKey(context.DecodeByteArray());
            return(helloMessage);
        }
Exemple #11
0
        protected override void Decode(IChannelHandlerContext context, byte[] input, List <object> output)
        {
            if (_logger.IsTraceEnabled)
            {
                _logger.Trace("Merging frames");
            }

            Rlp.DecoderContext headerBodyItems = input.Slice(3, 13).AsRlpContext();
            int headerDataEnd   = headerBodyItems.ReadSequenceLength() + headerBodyItems.Position;
            int numberOfItems   = headerBodyItems.ReadNumberOfItemsRemaining(headerDataEnd);
            int protocolType    = headerBodyItems.DecodeInt(); // not needed - adaptive IDs
            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.IsTraceEnabled)
                {
                    _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.IsTraceEnabled)
                {
                    _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
            }
        }