Example #1
0
        public void RequestHeaders(int headerIndex, int headerRequestCount)
        {
            BlockHeaderRequest hReq = new BlockHeaderRequest {
                Height = headerIndex - 1, Count = headerRequestCount
            };
            Message message = NetRequestFactory.CreateMessage(AElfProtocolMsgType.HeaderRequest, hReq.ToByteArray());

            EnqueueOutgoing(message);
        }
Example #2
0
        private void RequestBlockById(byte[] id, int height = 0)
        {
            // Create the request object
            BlockRequest br = new BlockRequest {
                Id = ByteString.CopyFrom(id), Height = height
            };
            Message message = NetRequestFactory.CreateMessage(AElfProtocolMsgType.RequestBlock, br.ToByteArray());

            if (message.Payload == null)
            {
                _logger?.Warn($"[{this}] request for block with id {id.ToHex()} failed because payload is null.");
                return;
            }

            SendTimedRequest(message, br);
        }
Example #3
0
        private void RequestBlockByIndex(int index)
        {
            // Create the request object
            BlockRequest br = new BlockRequest {
                Height = index
            };
            Message message = NetRequestFactory.CreateMessage(AElfProtocolMsgType.RequestBlock, br.ToByteArray());

            if (message.Payload == null)
            {
                _logger?.Warn($"[{this}] request for block at height {index} failed because payload is null.");
                return;
            }

            SendTimedRequest(message, br);
        }
Example #4
0
        private async Task HandleBlockRequestJob(PeerMessageReceivedArgs args)
        {
            try
            {
                var breq = BlockRequest.Parser.ParseFrom(args.Message.Payload);

                Block b;

                if (breq.Id != null && breq.Id.Length > 0)
                {
                    b = await _nodeService.GetBlockFromHash(breq.Id.ToByteArray());
                }
                else
                {
                    b = await _nodeService.GetBlockAtHeight(breq.Height);
                }

                if (b == null)
                {
                    _logger?.Warn($"Block not found {breq.Id?.ToByteArray().ToHex()}");
                    return;
                }

                Message req = NetRequestFactory.CreateMessage(AElfProtocolMsgType.Block, b.ToByteArray());

                if (args.Message.HasId)
                {
                    req.Id = args.Message.Id;
                }

                // Send response
                args.Peer.EnqueueOutgoing(req, (_) =>
                {
                    _logger?.Debug($"Block sent {{ hash: {b.BlockHashToHex}, to: {args.Peer} }}");
                });
            }
            catch (Exception e)
            {
                _logger?.Error(e, "Error while during HandleBlockRequest.");
            }
        }
Example #5
0
        /// <summary>
        /// This message broadcasts data to all of its peers. This creates and
        /// sends an object with the provided pay-load and message type.
        /// </summary>
        /// <param name="messageMsgType"></param>
        /// <param name="payload"></param>
        /// <returns></returns>
        private void BroadcastMessage(AElfProtocolMsgType messageMsgType, byte[] payload)
        {
            if (_peers == null || !_peers.Any())
            {
                _logger?.Warn("Cannot broadcast - no peers.");
                return;
            }

            try
            {
                Message message = NetRequestFactory.CreateMessage(messageMsgType, payload);

                foreach (var peer in _peers)
                {
                    peer.EnqueueOutgoing(message);
                }
            }
            catch (Exception e)
            {
                _logger?.Error(e, "Error while sending a message to the peers.");
            }
        }