Esempio n. 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}");
        }
Esempio n. 2
0
        public async Task TriggerConsensusAsync(ChainContext chainContext)
        {
            var triggerInformation = _consensusInformationGenerationService.GetTriggerInformation(TriggerType.ConsensusCommand);

            // Upload the consensus command.
            _consensusControlInformation.ConsensusCommand =
                await _consensusInformationGenerationService.ExecuteContractAsync <ConsensusCommand>(chainContext,
                                                                                                     ConsensusConsts.GetConsensusCommand, triggerInformation, DateTime.UtcNow);

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

            // Initial consensus scheduler.
            var blockMiningEventData = new ConsensusRequestMiningEventData(chainContext.BlockHash,
                                                                           chainContext.BlockHeight,
                                                                           _consensusControlInformation.ConsensusCommand.ExpectedMiningTime.ToDateTime(),
                                                                           TimeSpan.FromMilliseconds(_consensusControlInformation.ConsensusCommand
                                                                                                     .LimitMillisecondsOfMiningBlock));

            _consensusScheduler.CancelCurrentEvent();
            // TODO: Remove NextBlockMiningLeftMilliseconds.
            _consensusScheduler.NewEvent(_consensusControlInformation.ConsensusCommand.NextBlockMiningLeftMilliseconds,
                                         blockMiningEventData);

            // Update next mining time, also block time of both getting consensus extra data and txs.
            _nextMiningTime =
                DateTime.UtcNow.AddMilliseconds(_consensusControlInformation.ConsensusCommand
                                                .NextBlockMiningLeftMilliseconds);
        }
        /// <summary>
        /// Basically update the consensus scheduler with latest consensus command.
        /// </summary>
        /// <param name="chainContext"></param>
        /// <returns></returns>
        public async Task TriggerConsensusAsync(ChainContext chainContext)
        {
            var now = TimestampHelper.GetUtcNow();

            _blockTimeProvider.SetBlockTime(now);

            Logger.LogDebug($"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.
            var contractReaderContext =
                await _consensusReaderContextService.GetContractReaderContextAsync(chainContext);

            _consensusCommand = await _contractReaderFactory
                                .Create(contractReaderContext).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;

            // Update 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.LogDebug($"Set next mining time to: {_nextMiningTime.ToDateTime():hh:mm:ss.ffffff}");
        }