Example #1
0
        /// <summary>
        /// Get consensus behaviour for chain that able to change term,
        /// like AElf Main Chain.
        /// </summary>
        /// <param name="currentRound"></param>
        /// <param name="termNumber"></param>
        /// <returns></returns>
        private AElfConsensusBehaviour GetBehaviourForChainAbleToChangeTerm(Round currentRound, long termNumber)
        {
            // In first round, the blockchain start timestamp is incorrect.
            // We can return NextRound directly.
            if (currentRound.RoundNumber == 1)
            {
                return(AElfConsensusBehaviour.NextRound);
            }

            if (!TryToGetPreviousRoundInformation(out var previousRound))
            {
                Assert(false, $"Failed to get previous round information at height {Context.CurrentHeight}");
            }

            Assert(TryToGetBlockchainStartTimestamp(out var blockchainStartTimestamp),
                   "Failed to get blockchain start timestamp.");

            Context.LogDebug(() => $"Using start timestamp: {blockchainStartTimestamp}");

            // Calculate the approvals and make the judgement of changing term.
            return(currentRound.IsTimeToChangeTerm(previousRound, blockchainStartTimestamp, termNumber,
                                                   State.TimeEachTerm.Value)
                ? AElfConsensusBehaviour.NextTerm
                : AElfConsensusBehaviour.NextRound);
        }