示例#1
0
        public static ProtocolMessage Sign(this ProtocolMessage protocolMessage,
                                           byte[] signature = default,
                                           SigningContext signingContext = default)
        {
            var clone = protocolMessage.Clone();

            clone.Signature = SignatureHelper.GetSignature(signature, signingContext);
            return(clone);
        }
示例#2
0
        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));
            }
        }
示例#3
0
        /// <summary>
        /// Use this to sign a <see cref="ProtocolMessage"/> before sending it.
        /// If a signature was found on the message, this will error.
        /// </summary>
        /// <param name="protocolMessage">The message that needs to be signed.</param>
        /// <param name="keySigner">An instance of <see cref="IKeySigner"/> used to build the signature.</param>
        /// <param name="signingContext">The context used to sign the message.</param>
        public static ProtocolMessage Sign(this ProtocolMessage protocolMessage,
                                           IKeySigner keySigner,
                                           SigningContext signingContext)
        {
            if ((protocolMessage.Signature?.RawBytes.Length ?? 0) == keySigner.CryptoContext.SignatureLength)
            {
                Logger.Debug("The protocol message was already signed, returning a clone.");
                return(protocolMessage.Clone());
            }

            protocolMessage.Signature = null;
            var signatureBytes = keySigner.Sign(protocolMessage.ToByteArray(),
                                                signingContext).SignatureBytes;
            var signature = new Signature
            {
                SigningContext = signingContext,
                RawBytes       = signatureBytes.ToByteString()
            };

            protocolMessage.Signature = signature;
            return(protocolMessage);
        }