Example #1
0
        public async Task TriggerConsensusAsync(ChainContext chainContext)
        {
            var now = TimestampHelper.GetUtcNow();

            _blockTimeProvider.SetBlockTime(now);

            Logger.LogTrace($"Set block time to utc now: {now:hh:mm:ss.ffffff}. Trigger.");

            var triggerInformation = _triggerInformationProvider.GetTriggerInformationForConsensusCommand(new BytesValue());

            // Upload the consensus command.
            _consensusCommand = await _readerFactory.Create(chainContext)
                                .GetConsensusCommand.CallAsync(triggerInformation);

            Logger.LogDebug($"Updated consensus command: {_consensusCommand}");

            // Update next mining time, also block time of both getting consensus extra data and txs.
            _nextMiningTime =
                TimestampHelper.GetUtcNow().AddMilliseconds(_consensusCommand
                                                            .NextBlockMiningLeftMilliseconds);

            // Initial consensus scheduler.
            var blockMiningEventData = new ConsensusRequestMiningEventData(chainContext.BlockHash,
                                                                           chainContext.BlockHeight,
                                                                           _nextMiningTime,
                                                                           TimestampHelper.DurationFromMilliseconds(_consensusCommand.LimitMillisecondsOfMiningBlock));

            _consensusScheduler.CancelCurrentEvent();
            _consensusScheduler.NewEvent(_consensusCommand.NextBlockMiningLeftMilliseconds,
                                         blockMiningEventData);

            Logger.LogTrace($"Set next mining time to: {_nextMiningTime:hh:mm:ss.ffffff}");
        }
        public async Task <IEnumerable <string> > GetCurrentMinerList(ChainContext chainContext)
        {
            var minersWithRoundNumber =
                await _readerFactory.Create(chainContext).GetCurrentMinerList.CallAsync(new Empty());

            return(minersWithRoundNumber.Pubkeys.Select(k => k.ToHex()));
        }
Example #3
0
        public async Task TriggerConsensusAsync(ChainContext chainContext)
        {
            var now = TimestampHelper.GetUtcNow();

            _blockTimeProvider.SetBlockTime(now);

            Logger.LogTrace($"Set block time to utc now: {now.ToDateTime():hh:mm:ss.ffffff}. Trigger.");

            var triggerInformation =
                _triggerInformationProvider.GetTriggerInformationForConsensusCommand(new BytesValue());

            Logger.LogDebug($"Mining triggered, chain context: {chainContext.BlockHeight} - {chainContext.BlockHash}");

            // Upload the consensus command.
            _consensusCommand = await _readerFactory.Create(chainContext)
                                .GetConsensusCommand.CallAsync(triggerInformation);

            if (_consensusCommand == null)
            {
                Logger.LogWarning("Consensus command is null.");
                return;
            }

            Logger.LogDebug($"Updated consensus command: {_consensusCommand}");

            // Update next mining time, also block time of both getting consensus extra data and txs.
            _nextMiningTime = _consensusCommand.ArrangedMiningTime;
            var leftMilliseconds = _consensusCommand.ArrangedMiningTime - TimestampHelper.GetUtcNow();

            leftMilliseconds = leftMilliseconds.Seconds > ConsensusConstants.MaximumLeftMillisecondsForNextBlock
                ? new Duration {
                Seconds = ConsensusConstants.MaximumLeftMillisecondsForNextBlock
            }
                : leftMilliseconds;

            // Initial consensus scheduler.
            var blockMiningEventData = new ConsensusRequestMiningEventData(chainContext.BlockHash,
                                                                           chainContext.BlockHeight,
                                                                           _nextMiningTime,
                                                                           TimestampHelper.DurationFromMilliseconds(_consensusCommand.LimitMillisecondsOfMiningBlock),
                                                                           _consensusCommand.MiningDueTime);

            _consensusScheduler.CancelCurrentEvent();
            _consensusScheduler.NewEvent(leftMilliseconds.Milliseconds(), blockMiningEventData);

            Logger.LogTrace($"Set next mining time to: {_nextMiningTime.ToDateTime():hh:mm:ss.ffffff}");
        }