public PendingBlock(byte[] blockHash, Block block, List <Hash> missing, AElfProtocolMsgType msgType) { Block = block; BlockHash = blockHash; MissingTxs = missing == null ? new List <PendingTx>() : missing.Select(m => new PendingTx { Hash = m.Value.ToByteArray() }).ToList(); MsgType = msgType; }
public static Message CreateMessage(AElfProtocolMsgType messageMsgType, byte[] payload) { Message packetData = new Message { Type = (int)messageMsgType, Length = payload.Length, Payload = payload }; return(packetData); }
/// <summary> /// When a block is received through the network it is placed here for sync /// purposes. In the case that the transaction was not received through the /// network, it will be placed here to sync. /// </summary> /// <param name="block"></param> /// <param name="peer"></param> private async Task <bool> AddBlockToSync(Block block, IPeer peer, AElfProtocolMsgType msgType) { if (block?.Header == null || block.Body == null) { throw new InvalidBlockException("The block, block header or block body is null"); } byte[] blockHash; try { blockHash = block.GetHash().DumpByteArray(); } catch (Exception e) { throw new InvalidBlockException("Invalid block hash"); } if (GetBlock(blockHash) != null) { _logger?.Warn("Block already in pending list."); return(false); } var missingTxs = _poolService.GetMissingTransactions(block); if (missingTxs == null) { _logger?.Warn("Unknown exception in the pool."); return(false); } // todo check that the returned txs are actually in the block var newPendingBlock = new PendingBlock(blockHash, block, missingTxs, msgType) { Peer = peer }; var txsNeedToRevert = _syncService.AddPendingBlock(newPendingBlock); if (!txsNeedToRevert.IsNullOrEmpty()) { await _poolService.Revert(txsNeedToRevert); } _logger?.Debug( $"Added block to sync {{ id : {blockHash.ToHex()}, index : {block.Header.Index}, tx-count : {block.Body.Transactions.Count} }} "); return(true); }
private async Task ProcessPeerMessage(PeerMessageReceivedArgs args) { if (args?.Peer == null) { _logger.Warn("Peer is invalid."); return; } if (args.Message?.Payload == null) { _logger?.Warn($"Message from {args.Peer}, message/payload is null."); return; } AElfProtocolMsgType msgType = (AElfProtocolMsgType)args.Message.Type; switch (msgType) { case AElfProtocolMsgType.Announcement: HandleAnnouncement(args.Message, args.Peer); break; case AElfProtocolMsgType.Block: MessageHub.Instance.Publish(new BlockReceived(args.Block)); break; case AElfProtocolMsgType.NewTransaction: HandleNewTransaction(args.Message); break; case AElfProtocolMsgType.Headers: HandleHeaders(args.Message); break; case AElfProtocolMsgType.RequestBlock: await HandleBlockRequestJob(args); break; case AElfProtocolMsgType.HeaderRequest: await HandleHeaderRequest(args); break; } }
/// <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."); } }
public BlockReceivedEventArgs(Block block, IPeer peer, AElfProtocolMsgType msgType) { Block = block; Peer = peer; MsgType = msgType; }
public TransactionsReceivedEventArgs(TransactionList txList, Peer peer, AElfProtocolMsgType msgType) { Transactions = txList; Peer = peer; MsgType = msgType; }