public async Task <int> BroadcastAnnounceAsync(BlockHeader blockHeader) { int successfulBcasts = 0; foreach (var peer in _peerPool.GetPeers()) { try { var announcement = new PeerNewBlockAnnouncement { BlockHash = blockHeader.GetHash(), BlockHeight = blockHeader.Height, BlockTime = blockHeader.Time }; Logger.LogDebug($"PeerNewBlockAnnouncement: {announcement}"); await peer.AnnounceAsync(announcement); successfulBcasts++; } catch (NetworkException e) { Logger.LogError(e, "Error while sending block."); } } return(successfulBcasts); }
public async Task HandleAnnounceReceiveEventAsync_IrreversibleBlockIndex_IsNull() { var an = new PeerNewBlockAnnouncement { }; var sendKey = string.Empty; var announcementData = new AnnouncementReceivedEventData(an, sendKey); await _dpoSAnnouncementReceivedEventDataHandler.HandleEventAsync(announcementData); }
public void HandlerRemoteAnnounce(PeerNewBlockAnnouncement peerNewBlockAnnouncement) { CurrentBlockHeight = peerNewBlockAnnouncement.BlockHeight; CurrentBlockHash = peerNewBlockAnnouncement.BlockHash; _recentBlockHeightAndHashMappings[CurrentBlockHeight] = CurrentBlockHash; while (_recentBlockHeightAndHashMappings.Count > 10) { _recentBlockHeightAndHashMappings.TryRemove(_recentBlockHeightAndHashMappings.Keys.Min(), out _); } }
private bool VerifyAnnouncement(PeerNewBlockAnnouncement announcement) { var allowedFutureBlockTime = DateTime.UtcNow + KernelConsts.AllowedFutureBlockTimeSpan; if (allowedFutureBlockTime < announcement.BlockTime.ToDateTime()) { Logger.LogWarning($"Receive future block {announcement}"); return(false); } return(true); }
public async Task HandleEventAsync_FutureBlock_DoesNothing() { var announcement = new PeerNewBlockAnnouncement { BlockHash = Hash.FromString("block"), BlockHeight = 1, BlockTime = (DateTime.UtcNow + TimeSpan.FromSeconds(5)).ToTimestamp() }; var exception = await Record.ExceptionAsync(async() => await _handler.HandleEventAsync(new AnnouncementReceivedEventData(announcement, "bp1"))); Assert.Null(exception); }
public async Task HandleAnnounceReceiveEventAsync_IrreversibleBlockIndex_SureAmountEnough() { var block = await GenerateNewBlockAndAnnouncementToPeers(3); var an = new PeerNewBlockAnnouncement { BlockHash = block.GetHash(), BlockHeight = block.Height, BlockTime = block.Header.Time }; var sendKey = CryptoHelpers.GenerateKeyPair().PublicKey.ToHex(); var announcementData = new AnnouncementReceivedEventData(an, sendKey); await _dpoSAnnouncementReceivedEventDataHandler.HandleEventAsync(announcementData); }
/// <summary> /// This method is called when a peer wants to broadcast an announcement. /// </summary> public override Task <VoidReply> Announce(PeerNewBlockAnnouncement an, ServerCallContext context) { if (an?.BlockHash == null || an?.BlockTime == null) { Logger.LogError($"Received null announcement or header from {context.GetPeerInfo()}."); return(Task.FromResult(new VoidReply())); } var peerInPool = _peerPool.FindPeerByPublicKey(context.GetPublicKey()); peerInPool?.HandlerRemoteAnnounce(an); Logger.LogDebug($"Received announce {an.BlockHash} from {context.GetPeerInfo()}."); _ = EventBus.PublishAsync(new AnnouncementReceivedEventData(an, context.GetPublicKey())); return(Task.FromResult(new VoidReply())); }
public async Task AnnounceAsync_Success() { AnnouncementReceivedEventData received = null; _eventBus.Subscribe <AnnouncementReceivedEventData>(a => { received = a; return(Task.CompletedTask); }); var header = new PeerNewBlockAnnouncement { BlockHeight = 100, BlockHash = Hash.Generate(), BlockTime = DateTime.UtcNow.ToTimestamp() }; await _grpcPeer.AnnounceAsync(header); received.ShouldNotBeNull(); received.Announce.BlockHeight.ShouldBe(100); }
public async Task AnnounceAsync(PeerNewBlockAnnouncement header) { await RequestAsync(_client, c => c.AnnounceAsync(header), $"Bcast announce for {header.BlockHash} failed."); }
public AnnouncementReceivedEventData(PeerNewBlockAnnouncement an, string senderPubKey) { SenderPubKey = senderPubKey; Announce = an; }