/// <summary>Gets the maximum gossip cycles.</summary>
        /// <param name="broadcastMessage"></param>
        /// <returns></returns>
        private static uint GetMaxGossipCycles(BroadcastMessage broadcastMessage)
        {
            var peerNetworkSize = broadcastMessage.PeerNetworkSize;

            return((uint)(Math.Log(Math.Max(10, peerNetworkSize) / (double)MaxGossipPeersPerRound) /
                          Math.Max(1, broadcastMessage.BroadcastCount / MaxGossipPeersPerRound)));
        }
        private void SendBroadcastMessages(ProtocolMessage message, BroadcastMessage broadcastMessage)
        {
            try
            {
                var innerMessage       = message.FromProtocolMessage <ProtocolMessage>();
                var isOwnerOfBroadcast = innerMessage.PeerId.Equals(_peerId);

                if (isOwnerOfBroadcast)
                {
                    innerMessage  = innerMessage.Sign(_signer, _signingContext);
                    message.Value = innerMessage.ToByteString();
                }

                // The fan out is how many peers to broadcast to
                var fanOut = isOwnerOfBroadcast
                    ? BroadcastOwnerMaximumGossipPeersPerRound
                    : (int)Math.Max(GetMaxGossipCycles(broadcastMessage), MaxGossipPeersPerRound);

                var peersToGossip = GetRandomPeers(fanOut);

                var correlationId = innerMessage.CorrelationId.ToCorrelationId();

                //CLEAN UP
                foreach (var peerIdentifier in peersToGossip)
                {
                    _logger.Verbose("Broadcasting message {message}", message);
                    var protocolMessage = message.Clone();
                    protocolMessage.PeerId = _peerId;
                    _peerClient.SendMessage(new MessageDto(
                                                protocolMessage,
                                                peerIdentifier)
                                            );
                }

                var updateCount = (uint)peersToGossip.Count;
                if (updateCount <= 0)
                {
                    return;
                }

                broadcastMessage.BroadcastCount += updateCount;
                UpdatePendingRequest(correlationId, broadcastMessage);
            }
            catch (Exception e)
            {
                _logger.Error(e, nameof(SendBroadcastMessages));
            }
        }
 /// <summary>Adds the gossip request.</summary>
 /// <param name="broadcastMessage">The gossip request.</param>
 /// <param name="correlationId">The message correlation ID</param>
 private void UpdatePendingRequest(ICorrelationId correlationId, BroadcastMessage broadcastMessage)
 {
     _pendingRequests.Set(correlationId.Id, broadcastMessage, _entryOptions());
 }
 /// <summary>Determines whether this instance can gossip the specified correlation identifier.</summary>
 /// <param name="request">The gossip request</param>
 /// <returns><c>true</c> if this instance can gossip the specified correlation identifier; otherwise, <c>false</c>.</returns>
 private bool CanBroadcast(BroadcastMessage request)
 {
     return(request.BroadcastCount < GetMaxGossipCycles(request));
 }