Ejemplo n.º 1
0
        public async Task MineBlockAsync(List <Transaction> transactions = null)
        {
            if (!_isSystemContractsDeployed)
            {
                return;
            }

            if (transactions != null)
            {
                await _testDataProvider.AddTransactionListAsync(transactions);
            }

            var currentBlockTime = _testDataProvider.GetBlockTime();

            var(contractStub, pubkey) =
                GetProperContractStub(currentBlockTime);
            var command = await contractStub.GetConsensusCommand.CallAsync(pubkey);

            var hint = AElfConsensusHint.Parser.ParseFrom(command.Hint);
            var triggerInformation = new AElfConsensusTriggerInformation
            {
                Behaviour = hint.Behaviour,
                // It doesn't matter for testing.
                RandomHash         = Hash.FromString($"RandomHashOf{pubkey}"),
                PreviousRandomHash = Hash.FromString($"RandomHashOf{pubkey}"),
                Pubkey             = pubkey.Value
            };
            var consensusTransaction = await contractStub.GenerateConsensusTransactions.CallAsync(new BytesValue
                                                                                                  { Value = triggerInformation.ToByteString() });

            await MineAsync(contractStub, consensusTransaction.Transactions.First());

            _currentRound = await _contractStubs.First().GetCurrentRoundInformation.CallAsync(new Empty());

            _testDataProvider.SetBlockTime(
                consensusTransaction.Transactions.First().MethodName ==
                nameof(AEDPoSContractImplContainer.AEDPoSContractImplStub.NextTerm)
                    ? currentBlockTime.AddMilliseconds(AEDPoSExtensionConstants.MiningInterval)
                    : currentBlockTime.AddMilliseconds(AEDPoSExtensionConstants.ActualMiningInterval));

            await _testDataProvider.ResetAsync();
        }
Ejemplo n.º 2
0
        public async Task MineBlockAsync(List <Transaction> transactions = null, bool withException = false)
        {
            if (!_isSystemContractsDeployed)
            {
                return;
            }

            if (transactions != null)
            {
                await _testDataProvider.AddTransactionListAsync(transactions);
            }

            var currentBlockTime = _testDataProvider.GetBlockTime();

            {
                {
                    var currentRound = await _contractStubs.First().GetCurrentRoundInformation.CallAsync(new Empty());

                    if (currentRound.RoundNumber == 0)
                    {
                        throw new InitializationFailedException("Can't find current round information.");
                    }
                }
            }

            var maximumBlocksCount = (await _contractStubs.First().GetMaximumBlocksCount.CallAsync(new Empty())).Value;

            var(contractStub, pubkey) = GetProperContractStub(currentBlockTime, maximumBlocksCount);
            currentBlockTime          = _testDataProvider.GetBlockTime();

            {
                var currentRound = await _contractStubs.First().GetCurrentRoundInformation.CallAsync(new Empty());

                if (currentRound.RoundNumber == 0)
                {
                    throw new InitializationFailedException("Can't find current round information.");
                }
            }

            var command = await contractStub.GetConsensusCommand.CallAsync(pubkey);

            var hint = AElfConsensusHint.Parser.ParseFrom(command.Hint);
            var triggerInformation = new AElfConsensusTriggerInformation
            {
                Behaviour = hint.Behaviour,
                // It doesn't matter for testing.
                InValue         = Hash.FromString($"InValueOf{pubkey}"),
                PreviousInValue = Hash.FromString($"InValueOf{pubkey}"),
                Pubkey          = pubkey.Value
            };

            var consensusExtraData = await contractStub.GetConsensusExtraData.CallAsync(new BytesValue
            {
                Value = triggerInformation.ToByteString()
            });

            var consensusHeaderInformation = new AElfConsensusHeaderInformation();

            consensusHeaderInformation.MergeFrom(consensusExtraData.Value);
            Debug.WriteLine($"Current header information: {consensusHeaderInformation}");

            // Validate consensus extra data.
            {
                var validationResult =
                    await _contractStubs.First().ValidateConsensusBeforeExecution.CallAsync(consensusExtraData);

                if (!validationResult.Success)
                {
                    throw new Exception($"Consensus extra data validation failed: {validationResult.Message}");
                }
            }

            var consensusTransaction = await contractStub.GenerateConsensusTransactions.CallAsync(new BytesValue
            {
                Value = triggerInformation.ToByteString()
            });

            await MineAsync(contractStub, consensusTransaction.Transactions.First(), withException);

            _currentRound = await _contractStubs.First().GetCurrentRoundInformation.CallAsync(new Empty());

            Debug.WriteLine($"Update current round information.{_currentRound}");
            if (!_isSkipped)
            {
                if (_currentRound.RealTimeMinersInformation.Any(i => i.Value.MissedTimeSlots != 0))
                {
                    var previousRound = await _contractStubs.First().GetPreviousRoundInformation.CallAsync(new Empty());

                    throw new BlockMiningException(
                              $"Someone missed time slot.\n{_currentRound}\n{previousRound}\nCurrent block time: {currentBlockTime}");
                }
            }

            _testDataProvider.SetBlockTime(
                consensusTransaction.Transactions.First().MethodName ==
                nameof(AEDPoSContractImplContainer.AEDPoSContractImplStub.NextTerm)
                    ? currentBlockTime.AddMilliseconds(AEDPoSExtensionConstants.MiningInterval)
                    : currentBlockTime.AddMilliseconds(AEDPoSExtensionConstants.ActualMiningInterval));

            await _testDataProvider.ResetAsync();

            _isSkipped = false;
        }
Ejemplo n.º 3
0
        public async Task MineBlockAsync(List <Transaction> transactions = null, bool withException = false)
        {
            if (!_isSystemContractsDeployed)
            {
                return;
            }

            if (transactions != null)
            {
                await _testDataProvider.AddTransactionListAsync(transactions);
            }

            var currentBlockTime = _testDataProvider.GetBlockTime();

            {
                {
                    var currentRound = await _contractStubs.First().GetCurrentRoundInformation.CallAsync(new Empty());

                    if (currentRound.RoundNumber == 0)
                    {
                        throw new InitializationFailedException("Can't find current round information.");
                    }
                }
            }

            var maximumBlocksCount = (await _contractStubs.First().GetMaximumBlocksCount.CallAsync(new Empty())).Value;

            var(contractStub, pubkey) = GetProperContractStub(currentBlockTime, maximumBlocksCount);
            currentBlockTime          = _testDataProvider.GetBlockTime();

            {
                var currentRound = await _contractStubs.First().GetCurrentRoundInformation.CallAsync(new Empty());

                if (currentRound.RoundNumber == 0)
                {
                    throw new InitializationFailedException("Can't find current round information.");
                }
            }

            var triggerInformation = await GetConsensusTriggerInfoAsync(contractStub, pubkey);

            var consensusTransaction = await contractStub.GenerateConsensusTransactions.CallAsync(new BytesValue
            {
                Value = triggerInformation.ToByteString()
            });

            await MineAsync(contractStub, consensusTransaction.Transactions.First(), withException);

            _currentRound = await _contractStubs.First().GetCurrentRoundInformation.CallAsync(new Empty());

            Debug.WriteLine($"Update current round information.{_currentRound}");
            if (!_isSkipped)
            {
                if (_currentRound.RealTimeMinersInformation.Any(i => i.Value.MissedTimeSlots != 0))
                {
                    var previousRound = await _contractStubs.First().GetPreviousRoundInformation.CallAsync(new Empty());

                    throw new BlockMiningException(
                              $"Someone missed time slot.\n{_currentRound}\n{previousRound}\nCurrent block time: {currentBlockTime}");
                }
            }

            _testDataProvider.SetBlockTime(
                consensusTransaction.Transactions.First().MethodName ==
                nameof(AEDPoSContractImplContainer.AEDPoSContractImplStub.NextTerm)
                    ? currentBlockTime.AddMilliseconds(AEDPoSExtensionConstants.MiningInterval)
                    : currentBlockTime.AddMilliseconds(AEDPoSExtensionConstants.ActualMiningInterval));

            await _testDataProvider.ResetAsync();

            _isSkipped = false;
        }