Esempio n. 1
0
        /// <summary>
        /// If one node produced block this round or missed his time slot,
        /// whatever how long he missed, we can give him a consensus command with new time slot
        /// to produce a block (for terminating current round and start new round).
        /// The schedule generated by this command will be cancelled
        /// if this node executed blocks from other nodes.
        ///
        /// Notice:
        /// This method shouldn't return the expected mining time from round information.
        /// To prevent this kind of misuse, this method will return a invalid timestamp
        /// when this node hasn't missed his time slot.
        /// </summary>
        /// <returns></returns>
        public static Timestamp ArrangeAbnormalMiningTime(this Round round, string publicKey, DateTime dateTime,
                                                          int miningInterval = 0)
        {
            if (!round.RealTimeMinersInformation.ContainsKey(publicKey))
            {
                return(DateTime.MaxValue.ToUniversalTime().ToTimestamp());
            }

            if (miningInterval == 0)
            {
                miningInterval = round.GetMiningInterval();
            }

            if (!round.IsTimeSlotPassed(publicKey, dateTime, out var minerInRound) && minerInRound.OutValue == null)
            {
                return(DateTime.MaxValue.ToUniversalTime().ToTimestamp());
            }

            if (round.GetExtraBlockProducerInformation().PublicKey == publicKey)
            {
                var distance = (round.GetExtraBlockMiningTime() - dateTime).TotalMilliseconds;
                if (distance > 0)
                {
                    return(round.GetExtraBlockMiningTime().ToTimestamp());
                }
            }

            if (round.RealTimeMinersInformation.ContainsKey(publicKey) && miningInterval > 0)
            {
                var distanceToRoundStartTime = (dateTime - round.GetStartTime()).TotalMilliseconds;
                var missedRoundsCount        = (int)(distanceToRoundStartTime / round.TotalMilliseconds(miningInterval));
                var expectedEndTime          = round.GetExpectedEndTime(missedRoundsCount, miningInterval);
                return(expectedEndTime.ToDateTime().AddMilliseconds(minerInRound.Order * miningInterval).ToTimestamp());
            }

            // Never do the mining if this node has no privilege to mime or the mining interval is invalid.
            return(DateTime.MaxValue.ToUniversalTime().ToTimestamp());
        }
Esempio n. 2
0
        /// <summary>
        /// DPoS Behaviour is changeable in this method.
        /// It's the situation this miner should skip his time slot, more precisely.
        /// </summary>
        /// <param name="behaviour"></param>
        /// <param name="round"></param>
        /// <param name="publicKey"></param>
        /// <param name="dateTime"></param>
        /// <param name="isTimeSlotSkippable"></param>
        /// <returns></returns>
        internal static ConsensusCommand GetConsensusCommand(this DPoSBehaviour behaviour, Round round, string publicKey,
                                                             DateTime dateTime, bool isTimeSlotSkippable)
        {
            var minerInRound       = round.RealTimeMinersInformation[publicKey];
            var miningInterval     = round.GetMiningInterval();
            var myOrder            = round.RealTimeMinersInformation[minerInRound.PublicKey].Order;
            var expectedMiningTime = round.RealTimeMinersInformation[minerInRound.PublicKey].ExpectedMiningTime;

            int nextBlockMiningLeftMilliseconds;
            var hint = new DPoSHint {
                Behaviour = behaviour
            }.ToByteString();

            var previousMinerMissedHisTimeSlot       = myOrder != 1 &&
                                                       round.RealTimeMinersInformation.Values
                                                       .First(m => m.Order == myOrder - 1).OutValue == null;
            var previousTwoMinersMissedTheirTimeSlot = myOrder > 2 &&
                                                       round.RealTimeMinersInformation.Values
                                                       .First(m => m.Order == myOrder - 1).OutValue == null &&
                                                       round.RealTimeMinersInformation.Values
                                                       .First(m => m.Order == myOrder - 2).OutValue == null;
            var skipTimeSlot = previousMinerMissedHisTimeSlot && !previousTwoMinersMissedTheirTimeSlot &&
                               isTimeSlotSkippable;

            var firstMinerOfCurrentRound =
                round.RealTimeMinersInformation.Values.FirstOrDefault(m => m.OutValue != null);

            switch (behaviour)
            {
            case DPoSBehaviour.UpdateValueWithoutPreviousInValue:
                // Two reasons of `UpdateValueWithoutPreviousInValue` behaviour:
                // 1. 1st round of 1st term.
                // 2. Term changed in current round.
                if (skipTimeSlot)
                {
                    if (firstMinerOfCurrentRound != null)
                    {
                        var roundStartTimeInTheory = firstMinerOfCurrentRound.ActualMiningTime.ToDateTime()
                                                     .AddMilliseconds(-firstMinerOfCurrentRound.Order * miningInterval);
                        var minersCount = round.RealTimeMinersInformation.Count;
                        var extraBlockMiningTimeInTheory =
                            roundStartTimeInTheory.AddMilliseconds(minersCount * miningInterval);
                        nextBlockMiningLeftMilliseconds =
                            (int)(round.ArrangeAbnormalMiningTime(publicKey, extraBlockMiningTimeInTheory,
                                                                  miningInterval).ToDateTime() - dateTime).TotalMilliseconds;
                        // If someone produced block in current round before.

                        hint = new DPoSHint
                        {
                            Behaviour = DPoSBehaviour.NextRound
                        }.ToByteString();
                        break;
                    }

                    nextBlockMiningLeftMilliseconds = minerInRound.Order * miningInterval * 2 + miningInterval;
                    hint = new DPoSHint
                    {
                        Behaviour = DPoSBehaviour.NextRound
                    }.ToByteString();
                    break;
                }

                nextBlockMiningLeftMilliseconds = minerInRound.Order * miningInterval;
                break;

            case DPoSBehaviour.UpdateValue:
                // If miner of previous order didn't produce block, skip this time slot.
                if (skipTimeSlot)
                {
                    nextBlockMiningLeftMilliseconds = (int)(round.ArrangeAbnormalMiningTime(minerInRound.PublicKey,
                                                                                            round.GetExtraBlockMiningTime(),
                                                                                            round.GetMiningInterval()).ToDateTime() - dateTime)
                                                      .TotalMilliseconds;
                    hint = new DPoSHint
                    {
                        Behaviour = DPoSBehaviour.NextRound
                    }.ToByteString();
                    break;
                }

                nextBlockMiningLeftMilliseconds =
                    (int)(expectedMiningTime.ToDateTime() - dateTime).TotalMilliseconds;
                break;

            case DPoSBehaviour.NextRound:
                nextBlockMiningLeftMilliseconds = round.RoundNumber == 1
                        ? round.RealTimeMinersInformation.Count * miningInterval + myOrder * miningInterval
                        : (int)(round.ArrangeAbnormalMiningTime(minerInRound.PublicKey, dateTime).ToDateTime() -
                                dateTime).TotalMilliseconds;
                break;

            case DPoSBehaviour.NextTerm:
                nextBlockMiningLeftMilliseconds =
                    (int)(round.ArrangeAbnormalMiningTime(minerInRound.PublicKey, dateTime).ToDateTime() -
                          dateTime).TotalMilliseconds;
                break;

            default:
                return(new ConsensusCommand
                {
                    ExpectedMiningTime = expectedMiningTime,
                    NextBlockMiningLeftMilliseconds = int.MaxValue,
                    LimitMillisecondsOfMiningBlock = int.MaxValue,
                    Hint = new DPoSHint
                    {
                        Behaviour = DPoSBehaviour.Nothing
                    }.ToByteString()
                });
            }

            return(new ConsensusCommand
            {
                ExpectedMiningTime = expectedMiningTime,
                NextBlockMiningLeftMilliseconds = nextBlockMiningLeftMilliseconds,
                LimitMillisecondsOfMiningBlock = miningInterval / minerInRound.PromisedTinyBlocks,
                Hint = hint
            });
        }