public void Serialize(IByteBuffer byteBuffer, GetTrieNodesMessage message)
        {
            (int contentLength, int allPathsLength, int[] pathsLengths) = CalculateLengths(message);

            byteBuffer.EnsureWritable(Rlp.LengthOfSequence(contentLength), true);
            NettyRlpStream stream = new (byteBuffer);

            stream.StartSequence(contentLength);

            stream.Encode(message.RequestId);
            stream.Encode(message.RootHash);

            if (message.Paths == null || message.Paths.Length == 0)
            {
                stream.EncodeNullObject();
            }
            else
            {
                stream.StartSequence(allPathsLength);

                for (int i = 0; i < message.Paths.Length; i++)
                {
                    PathGroup group = message.Paths[i];

                    stream.StartSequence(pathsLengths[i]);

                    for (int j = 0; j < group.Group.Length; j++)
                    {
                        stream.Encode(group.Group[j]);
                    }
                }
            }

            stream.Encode(message.Bytes);
        }
        public void Serialize(IByteBuffer byteBuffer, BlockWitnessHashesMessage message)
        {
            NettyRlpStream nettyRlpStream = new NettyRlpStream(byteBuffer);

            int hashesContentLength = message.Hashes?.Length * Rlp.LengthOfKeccakRlp ?? 0;
            int contentLength       = Rlp.LengthOfSequence(hashesContentLength);

            contentLength += Rlp.LengthOf(message.RequestId);
            int totalLength = Rlp.LengthOfSequence(contentLength);

            byteBuffer.EnsureWritable(totalLength, true);
            nettyRlpStream.StartSequence(contentLength);
            nettyRlpStream.Encode(message.RequestId);
            if (message.Hashes == null)
            {
                nettyRlpStream.EncodeNullObject();
            }
            else
            {
                nettyRlpStream.StartSequence(hashesContentLength);
                foreach (Keccak keccak in message.Hashes)
                {
                    nettyRlpStream.Encode(keccak);
                }
            }
        }
        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.LengthOfSequence(requestLength) * message.Requests.Length;
            int contentLength     =
                Rlp.LengthOf(message.RequestId) +
                Rlp.LengthOfSequence(allRequestsLength);

            int totalLength = Rlp.LengthOfSequence(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);
            }
        }
        public void Serialize(IByteBuffer byteBuffer, GetHelperTrieProofsMessage message)
        {
            int innerLength = 0;

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

            int totalLength = Rlp.LengthOfSequence(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);
            }
        }
        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);
            }
        }
        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.LengthOfSequence(innerLength);

            int totalLength = Rlp.LengthOfSequence(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]);
            }
        }
Ejemplo n.º 7
0
        public override int GetLength(GetByteCodesMessage message, out int contentLength)
        {
            contentLength  = Rlp.LengthOf(message.RequestId);
            contentLength += Rlp.LengthOf(message.Hashes, true);
            contentLength += Rlp.LengthOf(message.Bytes);

            return(Rlp.LengthOfSequence(contentLength));
        }
Ejemplo n.º 8
0
        public int GetLength(GetBlockBodiesMessage message, out int contentLength)
        {
            contentLength = 0;
            for (int i = 0; i < message.BlockHashes.Count; i++)
            {
                contentLength += Rlp.LengthOf(message.BlockHashes[i]);
            }

            return(Rlp.LengthOfSequence(contentLength));
        }
        public int GetLength(TEth66Message message, out int contentLength)
        {
            int innerMessageLength = _ethMessageSerializer.GetLength(message.EthMessage, out _);

            contentLength =
                Rlp.LengthOf(message.RequestId) +
                innerMessageLength;

            return(Rlp.LengthOfSequence(contentLength));
        }
        public override int GetLength(GetAccountRangeMessage message, out int contentLength)
        {
            contentLength  = Rlp.LengthOf(message.RequestId);
            contentLength += Rlp.LengthOf(message.AccountRange.RootHash);
            contentLength += Rlp.LengthOf(message.AccountRange.StartingHash);
            contentLength += Rlp.LengthOf(message.AccountRange.LimitHash);
            contentLength += Rlp.LengthOf(message.ResponseBytes);

            return(Rlp.LengthOfSequence(contentLength));
        }
        public int GetLength(NodeDataMessage message, out int contentLength)
        {
            contentLength = 0;
            for (int i = 0; i < message.Data.Length; i++)
            {
                contentLength += Rlp.LengthOf(message.Data[i]);
            }

            return(Rlp.LengthOfSequence(contentLength));
        }
Ejemplo n.º 12
0
        private int GetLength(AnnounceMessage message, out int contentLength)
        {
            contentLength =
                Rlp.LengthOf(message.HeadHash) +
                Rlp.LengthOf(message.HeadBlockNo) +
                Rlp.LengthOf(message.TotalDifficulty) +
                Rlp.LengthOf(message.ReorgDepth) +
                Rlp.OfEmptySequence.Length;

            return(Rlp.LengthOfSequence(contentLength));
        }
        public override int GetLength(GetStorageRangeMessage message, out int contentLength)
        {
            contentLength  = Rlp.LengthOf(message.RequestId);
            contentLength += Rlp.LengthOf(message.StoragetRange.RootHash);
            contentLength += Rlp.LengthOf(message.StoragetRange.Accounts.Select(a => a.Path).ToArray(), true); // TODO: optimize this
            contentLength += Rlp.LengthOf(message.StoragetRange.StartingHash);
            contentLength += Rlp.LengthOf(message.StoragetRange.LimitHash);
            contentLength += Rlp.LengthOf(message.ResponseBytes);

            return(Rlp.LengthOfSequence(contentLength));
        }
Ejemplo n.º 14
0
        public int GetLength(AccessList?accessList, RlpBehaviors rlpBehaviors)
        {
            if (accessList is null)
            {
                return(1);
            }

            int contentLength = GetContentLength(accessList);

            return(Rlp.LengthOfSequence(contentLength));
        }
Ejemplo n.º 15
0
        public byte[] Serialize(NewBlockMessage message)
        {
            int       contentLength = _blockDecoder.GetLength(message.Block, RlpBehaviors.None) + Rlp.LengthOf((UInt256)message.TotalDifficulty);
            int       totalLength   = Rlp.LengthOfSequence(contentLength);
            RlpStream rlpStream     = new RlpStream(totalLength);

            rlpStream.StartSequence(contentLength);
            rlpStream.Encode(message.Block);
            rlpStream.Encode(message.TotalDifficulty);
            return(rlpStream.Data);
        }
Ejemplo n.º 16
0
        private (int Total, int Addresses) GetContentLength(PendingValidators item, RlpBehaviors rlpBehaviors)
        {
            int contentLength = Rlp.LengthOf(item.BlockNumber)
                                + Rlp.LengthOf(item.BlockHash)
                                + Rlp.LengthOf(item.AreFinalized);

            var addressesLength = GetAddressesLength(item.Addresses);

            contentLength += Rlp.LengthOfSequence(addressesLength);

            return(contentLength, addressesLength);
        }
Ejemplo n.º 17
0
        public int GetLength(GetBlockHeadersMessage message, out int contentLength)
        {
            contentLength = message.StartBlockHash == null
                ? Rlp.LengthOf(message.StartBlockNumber)
                : Rlp.LengthOf(message.StartBlockHash);

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

            return(Rlp.LengthOfSequence(contentLength));
        }
Ejemplo n.º 18
0
        public int GetLength(NewBlockHashesMessage message, out int contentLength)
        {
            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.LengthOfSequence(miniContentLength);
            }

            return(Rlp.LengthOfSequence(contentLength));
        }
Ejemplo n.º 19
0
        private (int contentLength, int allSlotsLength, int[] accountSlotsLengths, int proofsLength) CalculateLengths(StorageRangeMessage message)
        {
            int contentLength = Rlp.LengthOf(message.RequestId);

            int allSlotsLength = 0;

            int[] accountSlotsLengths = new int[message.Slots.Length];

            if (message.Slots == null || message.Slots.Length == 0)
            {
                allSlotsLength = 1;
            }
            else
            {
                for (var i = 0; i < message.Slots.Length; i++)
                {
                    int accountSlotsLength = 0;

                    var accountSlots = message.Slots[i];
                    foreach (PathWithStorageSlot slot in accountSlots)
                    {
                        int slotLength = Rlp.LengthOf(slot.Path) + Rlp.LengthOf(slot.SlotRlpValue);
                        accountSlotsLength += Rlp.LengthOfSequence(slotLength);
                    }

                    accountSlotsLengths[i] = accountSlotsLength;
                    allSlotsLength        += Rlp.LengthOfSequence(accountSlotsLength);
                }
            }

            contentLength += Rlp.LengthOfSequence(allSlotsLength);

            int proofsLength = 0;

            if (message.Proofs == null || message.Proofs.Length == 0)
            {
                proofsLength = 1;
                contentLength++;
            }
            else
            {
                for (int i = 0; i < message.Proofs.Length; i++)
                {
                    proofsLength += Rlp.LengthOf(message.Proofs[i]);
                }

                contentLength += Rlp.LengthOfSequence(proofsLength);
            }



            return(contentLength, allSlotsLength, accountSlotsLengths, proofsLength);
        }
 public int GetLength(BlockWitnessHashesMessage message, out int contentLength)
 {
     if (message.Hashes is null)
     {
         contentLength = Rlp.OfEmptySequence.Length;
     }
     else
     {
         int hashesContentLength = message.Hashes?.Length * Rlp.LengthOfKeccakRlp ?? 0;
         contentLength = Rlp.LengthOfSequence(hashesContentLength) + Rlp.LengthOf(message.RequestId);
     }
     return(Rlp.LengthOfSequence(contentLength));
 }
Ejemplo n.º 21
0
        public void Serialize(IByteBuffer byteBuffer, NewBlockMessage message)
        {
            int       contentLength = _blockDecoder.GetLength(message.Block, RlpBehaviors.None) + Rlp.LengthOf(message.TotalDifficulty);
            RlpStream rlpStream     = new NettyRlpStream(byteBuffer);

            int totalLength = Rlp.LengthOfSequence(contentLength);

            byteBuffer.EnsureWritable(totalLength, true);

            rlpStream.StartSequence(contentLength);
            rlpStream.Encode(message.Block);
            rlpStream.Encode(message.TotalDifficulty);
        }
Ejemplo n.º 22
0
        public Rlp Encode(NetworkNode item, RlpBehaviors rlpBehaviors = RlpBehaviors.None)
        {
            int       contentLength = GetContentLength(item, rlpBehaviors);
            RlpStream stream        = new(Rlp.LengthOfSequence(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));
        }
Ejemplo n.º 23
0
        public void Serialize(IByteBuffer byteBuffer, StorageRangeMessage message)
        {
            (int contentLength, int allSlotsLength, int[] accountSlotsLengths, int proofsLength) = CalculateLengths(message);

            byteBuffer.EnsureWritable(Rlp.LengthOfSequence(contentLength), true);
            NettyRlpStream stream = new(byteBuffer);

            stream.StartSequence(contentLength);

            stream.Encode(message.RequestId);

            if (message.Slots == null || message.Slots.Length == 0)
            {
                stream.EncodeNullObject();
            }
            else
            {
                stream.StartSequence(allSlotsLength);

                for (int i = 0; i < message.Slots.Length; i++)
                {
                    stream.StartSequence(accountSlotsLengths[i]);

                    PathWithStorageSlot[] accountSlots = message.Slots[i];

                    for (int j = 0; j < accountSlots.Length; j++)
                    {
                        var slot = accountSlots[j];

                        int itemLength = Rlp.LengthOf(slot.Path) + Rlp.LengthOf(slot.SlotRlpValue);

                        stream.StartSequence(itemLength);
                        stream.Encode(slot.Path);
                        stream.Encode(slot.SlotRlpValue);
                    }
                }
            }

            if (message.Proofs == null || message.Proofs.Length == 0)
            {
                stream.EncodeNullObject();
            }
            else
            {
                stream.StartSequence(proofsLength);
                for (int i = 0; i < message.Proofs.Length; i++)
                {
                    stream.Encode(message.Proofs[i]);
                }
            }
        }
        public void Serialize(IByteBuffer byteBuffer, ByteCodesMessage message)
        {
            (int contentLength, int codesLength) = GetLength(message);
            byteBuffer.EnsureWritable(Rlp.LengthOfSequence(contentLength), true);
            RlpStream rlpStream = new NettyRlpStream(byteBuffer);

            rlpStream.StartSequence(contentLength);
            rlpStream.Encode(message.RequestId);
            rlpStream.StartSequence(codesLength);
            for (int i = 0; i < message.Codes.Length; i++)
            {
                rlpStream.Encode(message.Codes[i]);
            }
        }
    public byte[] Serialize(EnrResponseMsg msg)
    {
        int contentLength = Rlp.LengthOfKeccakRlp;

        contentLength += msg.NodeRecord.GetRlpLengthWithSignature();
        int totalLength = Rlp.LengthOfSequence(contentLength);

        RlpStream rlpStream = new (totalLength);

        rlpStream.StartSequence(contentLength);
        rlpStream.Encode(msg.RequestKeccak);
        msg.NodeRecord.Encode(rlpStream);

        byte[] serializedMsg = Serialize((byte)msg.MsgType, rlpStream.Data);
        return(serializedMsg);
    }
        public void Serialize(IByteBuffer byteBuffer, GetReceiptsMessage message)
        {
            Eth.V63.Messages.GetReceiptsMessageSerializer ethSerializer = new();
            Rlp ethMessage    = new(ethSerializer.Serialize(message.EthMessage));
            int contentLength = Rlp.LengthOf(message.RequestId) + ethMessage.Length;

            int totalLength = Rlp.LengthOfSequence(contentLength);

            RlpStream rlpStream = new NettyRlpStream(byteBuffer);

            byteBuffer.EnsureWritable(totalLength);

            rlpStream.StartSequence(contentLength);
            rlpStream.Encode(message.RequestId);
            rlpStream.Encode(ethMessage);
        }
Ejemplo n.º 27
0
        public byte[] Serialize(BlockHeadersMessage message)
        {
            int contentLength = 0;

            for (int i = 0; i < message.BlockHeaders.Length; i++)
            {
                contentLength += _headerDecoder.GetLength(message.BlockHeaders[i], RlpBehaviors.None);
            }

            int       length    = Rlp.LengthOfSequence(contentLength);
            RlpStream rlpStream = new RlpStream(length);

            rlpStream.StartSequence(contentLength);
            for (int i = 0; i < message.BlockHeaders.Length; i++)
            {
                rlpStream.Encode(message.BlockHeaders[i]);
            }

            return(rlpStream.Data);
        }
Ejemplo n.º 28
0
        public void Serialize(IByteBuffer byteBuffer, T message)
        {
            int contentLength = 0;

            for (int i = 0; i < message.Hashes.Count; i++)
            {
                contentLength += Rlp.LengthOf(message.Hashes[i]);
            }

            int totalLength = Rlp.LengthOfSequence(contentLength);

            RlpStream rlpStream = new NettyRlpStream(byteBuffer);

            byteBuffer.EnsureWritable(totalLength, true);
            rlpStream.StartSequence(contentLength);
            for (int i = 0; i < message.Hashes.Count; i++)
            {
                rlpStream.Encode(message.Hashes[i]);
            }
        }
Ejemplo n.º 29
0
        public void Serialize(IByteBuffer byteBuffer, StatusMessage message)
        {
            NettyRlpStream rlpStream     = new NettyRlpStream(byteBuffer);
            int            contentLength =
                Rlp.LengthOf(message.ProtocolVersion) +
                Rlp.LengthOf(message.ChainId) +
                Rlp.LengthOf(message.TotalDifficulty) +
                Rlp.LengthOf(message.BestHash) +
                Rlp.LengthOf(message.GenesisHash);

            int totalLength = Rlp.LengthOfSequence(contentLength);

            byteBuffer.EnsureWritable(totalLength);
            rlpStream.StartSequence(contentLength);
            rlpStream.Encode(message.ProtocolVersion);
            rlpStream.Encode(message.ChainId);
            rlpStream.Encode(message.TotalDifficulty);
            rlpStream.Encode(message.BestHash);
            rlpStream.Encode(message.GenesisHash);
        }
        public void Serialize(IByteBuffer byteBuffer, GetBlockBodiesMessage message)
        {
            NettyRlpStream nettyRlpStream = new(byteBuffer);

            int contentLength = 0;

            for (int i = 0; i < message.BlockHashes.Count; i++)
            {
                contentLength += Rlp.LengthOf(message.BlockHashes[i]);
            }

            int totalLength = Rlp.LengthOfSequence(contentLength);

            byteBuffer.EnsureWritable(totalLength, true);

            nettyRlpStream.StartSequence(contentLength);
            for (int i = 0; i < message.BlockHashes.Count; i++)
            {
                nettyRlpStream.Encode(message.BlockHashes[i]);
            }
        }