コード例 #1
0
        public void Serialize(IByteBuffer byteBuffer, GetHelperTrieProofsMessage message)
        {
            int innerLength = 0;

            foreach (var request in message.Requests)
            {
                innerLength += Rlp.GetSequenceRlpLength(GetRequestLength(request));
            }
            int contentLength = Rlp.LengthOf(message.RequestId) +
                                Rlp.GetSequenceRlpLength(innerLength);

            int totalLength = Rlp.GetSequenceRlpLength(contentLength);

            RlpStream rlpStream = new NettyRlpStream(byteBuffer);

            byteBuffer.EnsureWritable(totalLength);

            rlpStream.StartSequence(contentLength);
            rlpStream.Encode(message.RequestId);
            rlpStream.StartSequence(innerLength);
            foreach (var request in message.Requests)
            {
                rlpStream.StartSequence(GetRequestLength(request));
                rlpStream.Encode((int)request.SubType);
                rlpStream.Encode(request.SectionIndex);
                rlpStream.Encode(request.Key);
                rlpStream.Encode(request.FromLevel);
                rlpStream.Encode(request.AuxiliaryData);
            }
        }
コード例 #2
0
        public void Serialize(IByteBuffer byteBuffer, ContractCodesMessage message)
        {
            int innerLength = 0;

            for (int i = 0; i < message.Codes.Length; i++)
            {
                innerLength += Rlp.LengthOf(message.Codes[i]);
            }
            int contentLength =
                Rlp.LengthOf(message.RequestId) +
                Rlp.LengthOf(message.BufferValue) +
                Rlp.GetSequenceRlpLength(innerLength);

            int totalLength = Rlp.GetSequenceRlpLength(contentLength);

            RlpStream rlpStream = new NettyRlpStream(byteBuffer);

            byteBuffer.EnsureWritable(totalLength);

            rlpStream.StartSequence(contentLength);
            rlpStream.Encode(message.RequestId);
            rlpStream.Encode(message.BufferValue);
            rlpStream.StartSequence(innerLength);
            for (int i = 0; i < message.Codes.Length; i++)
            {
                rlpStream.Encode(message.Codes[i]);
            }
        }
コード例 #3
0
        public void Serialize(IByteBuffer byteBuffer, GetBlockHeadersMessage message)
        {
            int contentLength = message.StartingBlockHash == null?Rlp.LengthOf(message.StartingBlockNumber) : Rlp.LengthOf(message.StartingBlockHash);

            contentLength += Rlp.LengthOf(message.MaxHeaders);
            contentLength += Rlp.LengthOf(message.Skip);
            contentLength += Rlp.LengthOf(message.Reverse);

            int totalLength = Rlp.GetSequenceRlpLength(contentLength);

            RlpStream rlpStream = new NettyRlpStream(byteBuffer);

            byteBuffer.EnsureWritable(totalLength, true);

            rlpStream.StartSequence(contentLength);
            if (message.StartingBlockHash == null)
            {
                rlpStream.Encode(message.StartingBlockNumber);
            }
            else
            {
                rlpStream.Encode(message.StartingBlockHash);
            }

            rlpStream.Encode(message.MaxHeaders);
            rlpStream.Encode(message.Skip);
            rlpStream.Encode(message.Reverse);
        }
コード例 #4
0
        public void Serialize(IByteBuffer byteBuffer, GetContractCodesMessage message)
        {
            // note: If there are any changes to how a hash is encoded, this will break (compression?)
            // calling LengthOf for each hash would be more resistant to future changes, if we think there will be any
            int requestLength     = Rlp.LengthOf(Keccak.OfAnEmptyString) * 2;
            int allRequestsLength = Rlp.GetSequenceRlpLength(requestLength) * message.Requests.Length;
            int contentLength     =
                Rlp.LengthOf(message.RequestId) +
                Rlp.GetSequenceRlpLength(allRequestsLength);

            int totalLength = Rlp.GetSequenceRlpLength(contentLength);

            RlpStream rlpStream = new NettyRlpStream(byteBuffer);

            byteBuffer.EnsureWritable(totalLength);

            rlpStream.StartSequence(contentLength);
            rlpStream.Encode(message.RequestId);

            rlpStream.StartSequence(allRequestsLength);
            foreach (CodeRequest request in message.Requests)
            {
                rlpStream.StartSequence(requestLength);
                rlpStream.Encode(request.BlockHash);
                rlpStream.Encode(request.AccountKey);
            }
        }
コード例 #5
0
        public void Serialize(IByteBuffer byteBuffer, NewBlockHashesMessage message)
        {
            NettyRlpStream nettyRlpStream = new(byteBuffer);

            int contentLength = 0;

            for (int i = 0; i < message.BlockHashes.Length; i++)
            {
                int miniContentLength = Rlp.LengthOf(message.BlockHashes[i].Item1);
                miniContentLength += Rlp.LengthOf(message.BlockHashes[i].Item2);
                contentLength     += Rlp.GetSequenceRlpLength(miniContentLength);
            }

            int totalLength = Rlp.LengthOfSequence(contentLength);

            byteBuffer.EnsureWritable(totalLength, true);

            nettyRlpStream.StartSequence(contentLength);
            for (int i = 0; i < message.BlockHashes.Length; i++)
            {
                int miniContentLength = Rlp.LengthOf(message.BlockHashes[i].Item1);
                miniContentLength += Rlp.LengthOf(message.BlockHashes[i].Item2);
                nettyRlpStream.StartSequence(miniContentLength);
                nettyRlpStream.Encode(message.BlockHashes[i].Item1);
                nettyRlpStream.Encode(message.BlockHashes[i].Item2);
            }
        }
コード例 #6
0
        public int GetLength(AccessList?accessList, RlpBehaviors rlpBehaviors)
        {
            if (accessList is null)
            {
                return(1);
            }

            int contentLength = GetContentLength(accessList);

            return(Rlp.GetSequenceRlpLength(contentLength));
        }
コード例 #7
0
        public Rlp Encode(NetworkNode item, RlpBehaviors rlpBehaviors = RlpBehaviors.None)
        {
            int       contentLength = GetContentLength(item, rlpBehaviors);
            RlpStream stream        = new RlpStream(Rlp.GetSequenceRlpLength(contentLength));

            stream.StartSequence(contentLength);
            stream.Encode(item.NodeId.Bytes);
            stream.Encode(item.Host);
            stream.Encode(item.Port);
            stream.Encode(string.Empty);
            stream.Encode(item.Reputation);
            return(new Rlp(stream.Data));
        }
コード例 #8
0
        public void Serialize(IByteBuffer byteBuffer, GetBlockBodiesMessage message)
        {
            Eth.V62.GetBlockBodiesMessageSerializer ethSerializer = new Eth.V62.GetBlockBodiesMessageSerializer();
            Rlp ethMessage    = new Rlp(ethSerializer.Serialize(message.EthMessage));
            int contentLength = Rlp.LengthOf(message.RequestId) + ethMessage.Length;

            int totalLength = Rlp.GetSequenceRlpLength(contentLength);

            RlpStream rlpStream = new NettyRlpStream(byteBuffer);

            byteBuffer.EnsureWritable(totalLength);

            rlpStream.StartSequence(contentLength);
            rlpStream.Encode(message.RequestId);
            rlpStream.Encode(ethMessage);
        }
コード例 #9
0
        public void Serialize(IByteBuffer byteBuffer, ReceiptsMessage message)
        {
            Eth.V63.ReceiptsMessageSerializer ethSerializer = new Eth.V63.ReceiptsMessageSerializer(_specProvider);
            Rlp ethMessage    = new Rlp(ethSerializer.Serialize(message.EthMessage));
            int contentLength = Rlp.LengthOf(message.RequestId) + Rlp.LengthOf(message.BufferValue) + ethMessage.Length;

            int totalLength = Rlp.GetSequenceRlpLength(contentLength);

            RlpStream rlpStream = new NettyRlpStream(byteBuffer);

            byteBuffer.EnsureWritable(totalLength);

            rlpStream.StartSequence(contentLength);
            rlpStream.Encode(message.RequestId);
            rlpStream.Encode(message.BufferValue);
            rlpStream.Encode(ethMessage);
        }
コード例 #10
0
        public void Serialize(IByteBuffer byteBuffer, HelperTrieProofsMessage message)
        {
            Rlp[] proofNodesRlp = new Rlp[message.ProofNodes.Length];
            for (int i = 0; i < message.ProofNodes.Length; i++)
            {
                proofNodesRlp[i] = Rlp.Encode(new Keccak(message.ProofNodes[i]));
            }

            Rlp proofsRlp = Rlp.Encode(proofNodesRlp);

            Rlp[] tempAuxRlp = new Rlp[message.AuxiliaryData.Length];
            for (int i = 0; i < message.AuxiliaryData.Length; i++)
            {
                tempAuxRlp[i] = Rlp.Encode(message.AuxiliaryData[i]);
            }
            Rlp auxRlp = Rlp.Encode(tempAuxRlp);

            int innerContentLength = proofsRlp.Length + auxRlp.Length;

            int contentLength =
                Rlp.LengthOf(message.RequestId) +
                Rlp.LengthOf(message.BufferValue) +
                Rlp.GetSequenceRlpLength(innerContentLength);

            int totalLength = Rlp.GetSequenceRlpLength(contentLength);

            RlpStream rlpStream = new NettyRlpStream(byteBuffer);

            byteBuffer.EnsureWritable(totalLength);

            rlpStream.StartSequence(contentLength);
            rlpStream.Encode(message.RequestId);
            rlpStream.Encode(message.BufferValue);
            rlpStream.StartSequence(innerContentLength);
            rlpStream.Encode(proofsRlp);
            rlpStream.Encode(auxRlp);
        }
コード例 #11
0
        private static (int Total, int Validators) GetContentLength(ValidatorInfo item, RlpBehaviors rlpBehaviors)
        {
            int validatorsLength = Rlp.LengthOfAddressRlp * item.Validators.Length;

            return(Rlp.LengthOf(item.FinalizingBlockNumber) + Rlp.LengthOf(item.PreviousFinalizingBlockNumber) + Rlp.GetSequenceRlpLength(validatorsLength), validatorsLength);
        }