Пример #1
0
 private void OnRecoveryMessageReceived(ConsensusPayload payload, RecoveryMessage message)
 {
     if (message.ViewNumber < context.ViewNumber)
     {
         return;
     }
     Log($"{nameof(OnRecoveryMessageReceived)}: height={payload.BlockIndex} view={message.ViewNumber} index={payload.ValidatorIndex}");
     isRecovering = true;
     try
     {
         if (message.ViewNumber > context.ViewNumber)
         {
             if (context.CommitSent())
             {
                 return;
             }
             ConsensusPayload[] changeViewPayloads = message.GetChangeViewPayloads(context, payload);
             foreach (ConsensusPayload changeViewPayload in changeViewPayloads)
             {
                 ReverifyAndProcessPayload(changeViewPayload);
             }
         }
         if (message.ViewNumber != context.ViewNumber)
         {
             return;
         }
         if (!context.CommitSent())
         {
             if (!context.RequestSentOrReceived())
             {
                 ConsensusPayload prepareRequestPayload = message.GetPrepareRequestPayload(context, payload);
                 if (prepareRequestPayload != null)
                 {
                     ReverifyAndProcessPayload(prepareRequestPayload);
                 }
                 else if (context.IsPrimary())
                 {
                     SendPrepareRequest();
                 }
             }
             ConsensusPayload[] prepareResponsePayloads = message.GetPrepareResponsePayloads(context, payload);
             foreach (ConsensusPayload prepareResponsePayload in prepareResponsePayloads)
             {
                 ReverifyAndProcessPayload(prepareResponsePayload);
             }
         }
         ConsensusPayload[] commitPayloads = message.GetCommitPayloadsFromRecoveryMessage(context, payload);
         foreach (ConsensusPayload commitPayload in commitPayloads)
         {
             ReverifyAndProcessPayload(commitPayload);
         }
     }
     finally
     {
         isRecovering = false;
     }
 }
Пример #2
0
        private void OnRecoveryMessageReceived(ConsensusPayload payload, RecoveryMessage message)
        {
            if (message.ViewNumber < context.ViewNumber)
            {
                return;
            }
            Log($"{nameof(OnRecoveryMessageReceived)}: height={payload.BlockIndex} view={message.ViewNumber} index={payload.ValidatorIndex}");
            // isRecovering is always set to false again after OnRecoveryMessageReceived
            isRecovering = true;
            int validChangeViews = 0, totalChangeViews = 0, validPrepReq = 0, totalPrepReq = 0;
            int validPrepResponses = 0, totalPrepResponses = 0, validCommits = 0, totalCommits = 0;

            try
            {
                if (message.ViewNumber > context.ViewNumber)
                {
                    if (context.CommitSent())
                    {
                        return;
                    }
                    ConsensusPayload[] changeViewPayloads = message.GetChangeViewPayloads(context, payload);
                    totalChangeViews = changeViewPayloads.Length;
                    foreach (ConsensusPayload changeViewPayload in changeViewPayloads)
                    {
                        if (ReverifyAndProcessPayload(changeViewPayload))
                        {
                            validChangeViews++;
                        }
                    }
                }
                if (message.ViewNumber != context.ViewNumber)
                {
                    return;
                }
                if (!context.CommitSent())
                {
                    if (!context.RequestSentOrReceived())
                    {
                        ConsensusPayload prepareRequestPayload = message.GetPrepareRequestPayload(context, payload);
                        if (prepareRequestPayload != null)
                        {
                            totalPrepReq = 1;
                            if (ReverifyAndProcessPayload(prepareRequestPayload))
                            {
                                validPrepReq++;
                            }
                        }
                        else if (context.IsPrimary())
                        {
                            SendPrepareRequest();
                        }
                    }
                    ConsensusPayload[] prepareResponsePayloads = message.GetPrepareResponsePayloads(context, payload);
                    totalPrepResponses = prepareResponsePayloads.Length;
                    foreach (ConsensusPayload prepareResponsePayload in prepareResponsePayloads)
                    {
                        if (ReverifyAndProcessPayload(prepareResponsePayload))
                        {
                            validPrepResponses++;
                        }
                    }
                }
                ConsensusPayload[] commitPayloads = message.GetCommitPayloadsFromRecoveryMessage(context, payload);
                totalCommits = commitPayloads.Length;
                foreach (ConsensusPayload commitPayload in commitPayloads)
                {
                    if (ReverifyAndProcessPayload(commitPayload))
                    {
                        validCommits++;
                    }
                }
            }
            finally
            {
                Log($"{nameof(OnRecoveryMessageReceived)}: finished (valid/total) " +
                    $"ChgView: {validChangeViews}/{totalChangeViews} " +
                    $"PrepReq: {validPrepReq}/{totalPrepReq} " +
                    $"PrepResp: {validPrepResponses}/{totalPrepResponses} " +
                    $"Commits: {validCommits}/{totalCommits}");
                isRecovering = false;
            }
        }