Exemplo n.º 1
0
        private static StatusMessage Deserialize(RlpStream rlpStream)
        {
            StatusMessage statusMessage = new StatusMessage();

            (int prefixLength, int contentLength) = rlpStream.PeekPrefixAndContentLength();
            var totalLength = contentLength;

            rlpStream.Position += prefixLength;
            var readLength = prefixLength;

            while (totalLength > readLength)
            {
                (prefixLength, contentLength) = rlpStream.PeekPrefixAndContentLength();
                readLength         += prefixLength + contentLength;
                rlpStream.Position += prefixLength;
                string key = rlpStream.DecodeString();
                switch (key)
                {
                case StatusMessage.KeyNames.ProtocolVersion:
                    statusMessage.ProtocolVersion = rlpStream.DecodeByte();
                    break;

                case StatusMessage.KeyNames.ChainId:
                    statusMessage.ChainId = rlpStream.DecodeUInt256();
                    break;

                case StatusMessage.KeyNames.TotalDifficulty:
                    statusMessage.TotalDifficulty = rlpStream.DecodeUInt256();
                    break;

                case StatusMessage.KeyNames.BestHash:
                    statusMessage.BestHash = rlpStream.DecodeKeccak();
                    break;

                case StatusMessage.KeyNames.HeadBlockNo:
                    statusMessage.HeadBlockNo = rlpStream.DecodeLong();
                    break;

                case StatusMessage.KeyNames.GenesisHash:
                    statusMessage.GenesisHash = rlpStream.DecodeKeccak();
                    break;

                case StatusMessage.KeyNames.AnnounceType:
                    statusMessage.AnnounceType = rlpStream.DecodeByte();
                    break;

                default:
                    // Ignore unknown keys - forwards compatibility
                    rlpStream.Position = readLength;
                    break;
                }
            }

            return(statusMessage);
        }
        private static StatusMessage Deserialize(RlpStream rlpStream)
        {
            StatusMessage statusMessage = new StatusMessage();

            (int prefixLength, int contentLength) = rlpStream.PeekPrefixAndContentLength();
            var totalLength = contentLength;

            rlpStream.Position += prefixLength;
            var readLength = prefixLength;

            while (totalLength > readLength)
            {
                (prefixLength, contentLength) = rlpStream.PeekPrefixAndContentLength();
                readLength         += prefixLength + contentLength;
                rlpStream.Position += prefixLength;
                string key = rlpStream.DecodeString();
                switch (key)
                {
                case StatusMessage.KeyNames.ProtocolVersion:
                    statusMessage.ProtocolVersion = rlpStream.DecodeByte();
                    break;

                case StatusMessage.KeyNames.ChainId:
                    statusMessage.ChainId = rlpStream.DecodeUInt256();
                    break;

                case StatusMessage.KeyNames.TotalDifficulty:
                    statusMessage.TotalDifficulty = rlpStream.DecodeUInt256();
                    break;

                case StatusMessage.KeyNames.BestHash:
                    statusMessage.BestHash = rlpStream.DecodeKeccak();
                    break;

                case StatusMessage.KeyNames.HeadBlockNo:
                    statusMessage.HeadBlockNo = rlpStream.DecodeLong();
                    break;

                case StatusMessage.KeyNames.GenesisHash:
                    statusMessage.GenesisHash = rlpStream.DecodeKeccak();
                    break;

                case StatusMessage.KeyNames.AnnounceType:
                    statusMessage.AnnounceType = rlpStream.DecodeByte();
                    break;

                case StatusMessage.KeyNames.ServeHeaders:
                    statusMessage.ServeHeaders = true;
                    rlpStream.SkipItem();
                    break;

                case StatusMessage.KeyNames.ServeChainSince:
                    statusMessage.ServeChainSince = rlpStream.DecodeLong();
                    break;

                case StatusMessage.KeyNames.ServeRecentChain:
                    statusMessage.ServeRecentChain = rlpStream.DecodeLong();
                    break;

                case StatusMessage.KeyNames.ServeStateSince:
                    statusMessage.ServeStateSince = rlpStream.DecodeLong();
                    break;

                case StatusMessage.KeyNames.ServeRecentState:
                    statusMessage.ServeRecentState = rlpStream.DecodeLong();
                    break;

                case StatusMessage.KeyNames.TxRelay:
                    statusMessage.TxRelay = true;
                    rlpStream.SkipItem();
                    break;

                case StatusMessage.KeyNames.BufferLimit:
                    statusMessage.BufferLimit = rlpStream.DecodeInt();
                    break;

                case StatusMessage.KeyNames.MaximumRechargeRate:
                    statusMessage.MaximumRechargeRate = rlpStream.DecodeInt();
                    break;

                case StatusMessage.KeyNames.MaximumRequestCosts:
                // todo
                default:
                    // Ignore unknown keys
                    rlpStream.Position = readLength;
                    break;
                }
            }

            return(statusMessage);
        }