Exemple #1
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);
        }
        private static RlpStream EncodeRlp(MevMegabundle bundle)
        {
            (int Content, int Tx, int RevertingTx) GetContentLength()
            {
                int txHashes          = Rlp.LengthOfKeccakRlp * bundle.Transactions.Count;
                int revertingTxHashes = Rlp.LengthOfKeccakRlp * bundle.RevertingTxHashes.Length;
                int content           = Rlp.LengthOfSequence(txHashes) + Rlp.LengthOf(bundle.BlockNumber) +
                                        Rlp.LengthOf(bundle.MinTimestamp) + Rlp.LengthOf(bundle.MaxTimestamp) +
                                        Rlp.LengthOfSequence(revertingTxHashes);

                return(Rlp.LengthOfSequence(content), txHashes, revertingTxHashes);
            }

            (int contentLength, int txLength, int revertingTxLength) = GetContentLength();
            RlpStream stream = new(contentLength);

            stream.StartSequence(contentLength);

            stream.StartSequence(txLength);
            for (int i = 0; i < bundle.Transactions.Count; i++)
            {
                stream.Encode(bundle.Transactions[i].Hash);
            }

            stream.Encode(bundle.BlockNumber);
            stream.Encode(bundle.MinTimestamp);
            stream.Encode(bundle.MaxTimestamp);

            stream.StartSequence(revertingTxLength);
            for (int i = 0; i < bundle.RevertingTxHashes.Length; i++)
            {
                stream.Encode(bundle.RevertingTxHashes[i]);
            }

            return(stream);
        }
Exemple #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);
        }
        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);
        }
        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);
        }
Exemple #6
0
        public void Serialize(IByteBuffer byteBuffer, StatusMessage message)
        {
            NettyRlpStream rlpStream = new NettyRlpStream(byteBuffer);

            #region Find Lengths
            int totalContentLength    = 0;
            int protocolVersionLength = Rlp.LengthOf(StatusMessage.KeyNames.ProtocolVersion) + Rlp.LengthOf(message.ProtocolVersion);
            totalContentLength += Rlp.LengthOfSequence(protocolVersionLength);

            int networkIdLength = Rlp.LengthOf(StatusMessage.KeyNames.ChainId) + Rlp.LengthOf(message.ChainId);
            totalContentLength += Rlp.LengthOfSequence(networkIdLength);

            int headTdLength = Rlp.LengthOf(StatusMessage.KeyNames.TotalDifficulty) + Rlp.LengthOf(message.TotalDifficulty);
            totalContentLength += Rlp.LengthOfSequence(headTdLength);

            int headHashLength = Rlp.LengthOf(StatusMessage.KeyNames.BestHash) + Rlp.LengthOf(message.BestHash);
            totalContentLength += Rlp.LengthOfSequence(headHashLength);

            int headNumLength = Rlp.LengthOf(StatusMessage.KeyNames.HeadBlockNo) + Rlp.LengthOf(message.HeadBlockNo);
            totalContentLength += Rlp.LengthOfSequence(headNumLength);

            int genesisHashLength = Rlp.LengthOf(StatusMessage.KeyNames.GenesisHash) + Rlp.LengthOf(message.GenesisHash);
            totalContentLength += Rlp.LengthOfSequence(genesisHashLength);

            int announceTypeLength = 0;
            if (message.AnnounceType.HasValue)
            {
                announceTypeLength  = Rlp.LengthOf(StatusMessage.KeyNames.AnnounceType) + Rlp.LengthOf(message.AnnounceType.Value);
                totalContentLength += Rlp.LengthOfSequence(announceTypeLength);
            }

            int serveHeadersLength = 0;
            if (message.ServeHeaders)
            {
                serveHeadersLength  = Rlp.LengthOf(StatusMessage.KeyNames.ServeHeaders) + Rlp.OfEmptySequence.Length;
                totalContentLength += Rlp.LengthOfSequence(serveHeadersLength);
            }

            int serveChainSinceLength = 0;
            if (message.ServeChainSince.HasValue)
            {
                serveChainSinceLength = Rlp.LengthOf(StatusMessage.KeyNames.ServeChainSince) + Rlp.LengthOf(message.ServeChainSince.Value);
                totalContentLength   += Rlp.LengthOfSequence(serveChainSinceLength);
            }

            int serveRecentChainLength = 0;
            if (message.ServeRecentChain.HasValue)
            {
                serveRecentChainLength = Rlp.LengthOf(StatusMessage.KeyNames.ServeRecentChain) + Rlp.LengthOf(message.ServeRecentChain.Value);
                totalContentLength    += Rlp.LengthOfSequence(serveRecentChainLength);
            }

            int serveStateSinceLength = 0;
            if (message.ServeStateSince.HasValue)
            {
                serveStateSinceLength = Rlp.LengthOf(StatusMessage.KeyNames.ServeStateSince) + Rlp.LengthOf(message.ServeStateSince.Value);
                totalContentLength   += Rlp.LengthOfSequence(serveStateSinceLength);
            }

            int serveRecentStateLength = 0;
            if (message.ServeRecentState.HasValue)
            {
                serveRecentStateLength = Rlp.LengthOf(StatusMessage.KeyNames.ServeRecentState) + Rlp.LengthOf(message.ServeRecentState.Value);
                totalContentLength    += Rlp.LengthOfSequence(serveRecentStateLength);
            }

            int txRelayLength = 0;
            if (message.TxRelay)
            {
                txRelayLength       = Rlp.LengthOf(StatusMessage.KeyNames.TxRelay) + Rlp.OfEmptySequence.Length;
                totalContentLength += Rlp.LengthOfSequence(txRelayLength);
            }

            int bufferLimitLength = 0;
            if (message.BufferLimit.HasValue)
            {
                bufferLimitLength   = Rlp.LengthOf(StatusMessage.KeyNames.BufferLimit) + Rlp.LengthOf(message.BufferLimit.Value);
                totalContentLength += Rlp.LengthOfSequence(bufferLimitLength);
            }

            int maxRechargeRateLength = 0;
            if (message.MaximumRechargeRate.HasValue)
            {
                maxRechargeRateLength = Rlp.LengthOf(StatusMessage.KeyNames.MaximumRechargeRate) + Rlp.LengthOf(message.MaximumRechargeRate.Value);
                totalContentLength   += Rlp.LengthOfSequence(maxRechargeRateLength);
            }

            int maxRequestCostsLength = 0;
            int innerCostListLength   = 0;
            if (message.MaximumRequestCosts != null)
            {
                // todo - what's the best way to do this? Calculating the length twice is definitely less than ideal.
                // Maybe build RLP for them here, and append bytes below?
                maxRequestCostsLength += Rlp.LengthOf(StatusMessage.KeyNames.MaximumRequestCosts);
                foreach (var item in message.MaximumRequestCosts)
                {
                    int costContentLength = Rlp.LengthOf(item.MessageCode) + Rlp.LengthOf(item.BaseCost) + Rlp.LengthOf(item.RequestCost);
                    innerCostListLength += Rlp.LengthOfSequence(costContentLength);
                }
                maxRequestCostsLength += Rlp.LengthOfSequence(innerCostListLength);
                totalContentLength    += Rlp.LengthOfSequence(maxRequestCostsLength);
            }
            #endregion

            #region Encode Values
            int totalLength = Rlp.LengthOfSequence(totalContentLength);
            byteBuffer.EnsureWritable(totalLength);
            rlpStream.StartSequence(totalContentLength);

            rlpStream.StartSequence(protocolVersionLength);
            rlpStream.Encode(StatusMessage.KeyNames.ProtocolVersion);
            rlpStream.Encode(message.ProtocolVersion);

            rlpStream.StartSequence(networkIdLength);
            rlpStream.Encode(StatusMessage.KeyNames.ChainId);
            rlpStream.Encode(message.ChainId);

            rlpStream.StartSequence(headTdLength);
            rlpStream.Encode(StatusMessage.KeyNames.TotalDifficulty);
            rlpStream.Encode(message.TotalDifficulty);

            rlpStream.StartSequence(headHashLength);
            rlpStream.Encode(StatusMessage.KeyNames.BestHash);
            rlpStream.Encode(message.BestHash);

            rlpStream.StartSequence(headNumLength);
            rlpStream.Encode(StatusMessage.KeyNames.HeadBlockNo);
            rlpStream.Encode(message.HeadBlockNo);

            rlpStream.StartSequence(genesisHashLength);
            rlpStream.Encode(StatusMessage.KeyNames.GenesisHash);
            rlpStream.Encode(message.GenesisHash);

            if (message.AnnounceType.HasValue)
            {
                rlpStream.StartSequence(announceTypeLength);
                rlpStream.Encode(StatusMessage.KeyNames.AnnounceType);
                rlpStream.Encode(message.AnnounceType.Value);
            }

            if (message.ServeHeaders)
            {
                rlpStream.StartSequence(serveHeadersLength);
                rlpStream.Encode(StatusMessage.KeyNames.ServeHeaders);
                rlpStream.Encode(Rlp.OfEmptySequence);
            }

            if (message.ServeChainSince.HasValue)
            {
                rlpStream.StartSequence(serveChainSinceLength);
                rlpStream.Encode(StatusMessage.KeyNames.ServeChainSince);
                rlpStream.Encode(message.ServeChainSince.Value);
            }

            if (message.ServeRecentChain.HasValue)
            {
                rlpStream.StartSequence(serveRecentChainLength);
                rlpStream.Encode(StatusMessage.KeyNames.ServeRecentChain);
                rlpStream.Encode(message.ServeRecentChain.Value);
            }

            if (message.ServeStateSince.HasValue)
            {
                rlpStream.StartSequence(serveStateSinceLength);
                rlpStream.Encode(StatusMessage.KeyNames.ServeStateSince);
                rlpStream.Encode(message.ServeStateSince.Value);
            }

            if (message.ServeRecentState.HasValue)
            {
                rlpStream.StartSequence(serveRecentStateLength);
                rlpStream.Encode(StatusMessage.KeyNames.ServeRecentState);
                rlpStream.Encode(message.ServeRecentState.Value);
            }

            if (message.TxRelay)
            {
                rlpStream.StartSequence(txRelayLength);
                rlpStream.Encode(StatusMessage.KeyNames.TxRelay);
                rlpStream.Encode(Rlp.OfEmptySequence);
            }

            if (message.BufferLimit.HasValue)
            {
                rlpStream.StartSequence(bufferLimitLength);
                rlpStream.Encode(StatusMessage.KeyNames.BufferLimit);
                rlpStream.Encode(message.BufferLimit.Value);
            }

            if (message.MaximumRechargeRate.HasValue)
            {
                rlpStream.StartSequence(maxRechargeRateLength);
                rlpStream.Encode(StatusMessage.KeyNames.MaximumRechargeRate);
                rlpStream.Encode(message.MaximumRechargeRate.Value);
            }

            if (message.MaximumRequestCosts != null)
            {
                rlpStream.StartSequence(maxRequestCostsLength);
                rlpStream.Encode(StatusMessage.KeyNames.MaximumRequestCosts);
                rlpStream.StartSequence(innerCostListLength);
                foreach (var item in message.MaximumRequestCosts)
                {
                    int length = Rlp.LengthOf(item.MessageCode) + Rlp.LengthOf(item.BaseCost) + Rlp.LengthOf(item.RequestCost);
                    rlpStream.StartSequence(length);
                    rlpStream.Encode(item.MessageCode);
                    rlpStream.Encode(item.BaseCost);
                    rlpStream.Encode(item.RequestCost);
                }
            }
            #endregion
        }
Exemple #7
0
 protected override int GetRlpLengthOfValue()
 {
     return(Rlp.LengthOfSequence(
                Rlp.LengthOfSequence(
                    5 + Rlp.LengthOf(Value.NextBlock))));
 }
        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);
        }
Exemple #9
0
 protected override int GetRlpLengthOfValue()
 {
     return(Rlp.LengthOf(Value));
 }
Exemple #10
0
 internal int GetRlpLength()
 {
     return(Rlp.LengthOf(Key) + GetRlpLengthOfValue());
 }
Exemple #11
0
 public int GetLength(GetBlockWitnessHashesMessage message, out int contentLength)
 {
     contentLength = Rlp.LengthOf(message.RequestId)
                     + (message.BlockHash is null ? 1 : Rlp.LengthOfKeccakRlp);
     return(Rlp.LengthOfSequence(contentLength) + Rlp.LengthOf(message.RequestId) + Rlp.LengthOf(message.BlockHash));
 }