/// <summary>
        /// AElf Consensus Behaviour is changeable in this method when
        /// this miner should skip his time slot more precisely.
        /// </summary>
        /// <param name="behaviour"></param>
        /// <param name="currentRound"></param>
        /// <param name="pubkey"></param>
        /// <param name="currentBlockTime"></param>
        /// <returns></returns>
        private ConsensusCommand GetConsensusCommand(AElfConsensusBehaviour behaviour, Round currentRound,
                                                     string pubkey, Timestamp currentBlockTime = null)
        {
            if (SolitaryMinerDetection(currentRound, pubkey))
            {
                return(ConsensusCommandProvider.InvalidConsensusCommand);
            }

            if (currentRound.RoundNumber == 1 && behaviour != AElfConsensusBehaviour.TinyBlock)
            {
                return(new ConsensusCommandProvider(new FirstRoundCommandStrategy(currentRound, pubkey,
                                                                                  currentBlockTime, behaviour)).GetConsensusCommand());
            }

            Context.LogDebug(() => $"Params to get command: {behaviour}, {pubkey}, {currentBlockTime}");
            switch (behaviour)
            {
            case AElfConsensusBehaviour.UpdateValue:
                TryToGetPreviousRoundInformation(out var previousRound);
                return(new ConsensusCommandProvider(new NormalBlockCommandStrategy(currentRound, pubkey,
                                                                                   currentBlockTime, previousRound.RoundId)).GetConsensusCommand());

            case AElfConsensusBehaviour.NextRound:
            case AElfConsensusBehaviour.NextTerm:
                return(new ConsensusCommandProvider(
                           new TerminateRoundCommandStrategy(currentRound, pubkey, currentBlockTime,
                                                             behaviour == AElfConsensusBehaviour.NextTerm))
                       .GetConsensusCommand());

            case AElfConsensusBehaviour.TinyBlock:
            {
                var consensusCommand =
                    new ConsensusCommandProvider(new TinyBlockCommandStrategy(currentRound, pubkey,
                                                                              currentBlockTime, GetMaximumBlocksCount())).GetConsensusCommand();
                if (consensusCommand.Hint ==
                    new AElfConsensusHint {
                        Behaviour = AElfConsensusBehaviour.NextRound
                    }.ToByteString())
                {
                    Context.LogDebug(() => "Re-arranged behaviour from TinyBlock to NextRound.");
                }

                return(consensusCommand);
            }

            default:
                return(ConsensusCommandProvider.InvalidConsensusCommand);
            }
        }
Пример #2
0
        /// <summary>
        /// AElf Consensus Behaviour is changeable in this method when
        /// this miner should skip his time slot more precisely.
        /// </summary>
        /// <param name="behaviour"></param>
        /// <param name="currentRound"></param>
        /// <param name="publicKey"></param>
        /// <returns></returns>
        private ConsensusCommand GetConsensusCommand(AElfConsensusBehaviour behaviour, Round currentRound,
                                                     string publicKey)
        {
            if (SolitaryMinerDetection(currentRound, publicKey))
            {
                return(ConsensusCommandProvider.InvalidConsensusCommand);
            }

            var currentBlockTime = Context.CurrentBlockTime;

            if (currentRound.RoundNumber == 1 && behaviour != AElfConsensusBehaviour.TinyBlock)
            {
                return(new ConsensusCommandProvider(new FirstRoundCommandStrategy(currentRound, publicKey,
                                                                                  currentBlockTime, behaviour)).GetConsensusCommand());
            }

            switch (behaviour)
            {
            case AElfConsensusBehaviour.UpdateValueWithoutPreviousInValue:
            case AElfConsensusBehaviour.UpdateValue:
                return(new ConsensusCommandProvider(new NormalBlockCommandStrategy(currentRound, publicKey,
                                                                                   currentBlockTime)).GetConsensusCommand());

            case AElfConsensusBehaviour.NextRound:
            case AElfConsensusBehaviour.NextTerm:
                return(new ConsensusCommandProvider(
                           new TerminateRoundCommandStrategy(currentRound, publicKey, currentBlockTime,
                                                             behaviour == AElfConsensusBehaviour.NextTerm))
                       .GetConsensusCommand());

            case AElfConsensusBehaviour.TinyBlock:
            {
                var consensusCommand =
                    new ConsensusCommandProvider(new TinyBlockCommandStrategy(currentRound, publicKey,
                                                                              currentBlockTime, GetMaximumBlocksCount())).GetConsensusCommand();
                if (consensusCommand.Hint ==
                    new AElfConsensusHint {
                        Behaviour = AElfConsensusBehaviour.NextRound
                    }.ToByteString())
                {
                    Context.LogDebug(() => "Re-arranged behaviour from TinyBlock to NextRound.");
                }

                return(consensusCommand);
            }
            }

            return(ConsensusCommandProvider.InvalidConsensusCommand);
        }