public async Task AEDPoSContract_ConsensusTransactionValidation_Test() { var usingKeyPair = BootMinerKeyPair; KeyPairProvider.SetKeyPair(usingKeyPair); var consensusCommand = await AEDPoSContract_GetConsensusCommand_FirstRound_ExtraBlockMiner_Test(); var triggerForCommand = TriggerInformationProvider .GetTriggerInformationForBlockHeaderExtraData(consensusCommand.ToBytesValue()); var extraDataBytes = await AEDPoSContractStub.GetConsensusExtraData.CallAsync(triggerForCommand); var validateBeforeResult = await AEDPoSContractStub.ValidateConsensusBeforeExecution.CallAsync(extraDataBytes); validateBeforeResult.Success.ShouldBeTrue(); var roundInfo = await AEDPoSContractStub.GetCurrentRoundInformation.CallAsync(new Empty()); roundInfo.RoundNumber++; roundInfo.IsMinerListJustChanged = false; roundInfo.TermNumber++; var transactionResult = await AEDPoSContractStub.NextRound.SendAsync(roundInfo); transactionResult.TransactionResult.Status.ShouldBe(TransactionResultStatus.Mined); var validateAfterResult = await AEDPoSContractStub.ValidateConsensusAfterExecution.CallAsync(roundInfo.ToBytesValue()); validateAfterResult.Success.ShouldBeTrue(); }
private async Task AEDPoSContract_FirstRound_SecondMiner_Test() { var transaction = (await AEDPoSContract_GenerateConsensusTransactions_FirstRound_SecondMiner_Test()).Transactions.First(); var usingKeyPair = InitialCoreDataCenterKeyPairs[1]; KeyPairProvider.SetKeyPair(usingKeyPair); BlockTimeProvider.SetBlockTime(BlockchainStartTimestamp + new Duration { Seconds = AEDPoSContractTestConstants.MiningInterval.Mul(2).Div(1000) }); var updateValueInput = new UpdateValueInput(); updateValueInput.MergeFrom(transaction.Params); var stub = GetAEDPoSContractStub(usingKeyPair); await stub.UpdateValue.SendAsync(updateValueInput); var currentRound = await stub.GetCurrentRoundInformation.CallAsync(new Empty()); currentRound.RoundNumber.ShouldBe(1); currentRound.RealTimeMinersInformation[usingKeyPair.PublicKey.ToHex()].OutValue.ShouldNotBeNull(); }
public async Task AEDPoSContract_ValidateConsensusBeforeExecution_UpdateValue_WithoutMiner_Test() { var usingKeyPair = ValidationDataCenterKeyPairs[0]; var consensusCommand = await AEDPoSContract_GetConsensusCommand_FirstRound_ExtraBlockMiner_Test(); KeyPairProvider.SetKeyPair(usingKeyPair); var updateValue = new AElfConsensusHint { Behaviour = AElfConsensusBehaviour.UpdateValue } .ToByteString(); consensusCommand.Hint = updateValue; var triggerForCommand = TriggerInformationProvider .GetTriggerInformationForBlockHeaderExtraData(consensusCommand.ToBytesValue()); var extraDataBytes = await AEDPoSContractStub.GetConsensusExtraData.CallAsync(triggerForCommand); await NextTerm(BootMinerKeyPair); var otherUser = GetAEDPoSContractStub(usingKeyPair); var validateBeforeResult = await otherUser.ValidateConsensusBeforeExecution.CallAsync(extraDataBytes); validateBeforeResult.Success.ShouldBeFalse(); validateBeforeResult.Message.ShouldContain("is not a miner"); }
/// <summary> /// Generate encrypted messages and put them to round information. /// </summary> /// <returns></returns> private async Task <Dictionary <string, AElfConsensusTriggerInformation> > GenerateEncryptedMessagesAsync() { var firstRound = await AEDPoSContractStub.GetCurrentRoundInformation.CallAsync(new Empty()); var randomHashes = Enumerable.Range(0, EconomicContractsTestConstants.InitialCoreDataCenterCount) .Select(_ => Hash.FromString("randomHashes")).ToList(); var triggers = Enumerable.Range(0, EconomicContractsTestConstants.InitialCoreDataCenterCount).Select(i => new AElfConsensusTriggerInformation { Pubkey = ByteString.CopyFrom(InitialCoreDataCenterKeyPairs[i].PublicKey), InValue = randomHashes[i] }).ToDictionary(t => t.Pubkey.ToHex(), t => t); foreach (var minerInRound in firstRound.RealTimeMinersInformation.Values.OrderBy(m => m.Order)) { var currentKeyPair = InitialCoreDataCenterKeyPairs.First(p => p.PublicKey.ToHex() == minerInRound.Pubkey); KeyPairProvider.SetKeyPair(currentKeyPair); BlockTimeProvider.SetBlockTime(minerInRound.ExpectedMiningTime); var tester = GetAEDPoSContractStub(currentKeyPair); var headerInformation = new AElfConsensusHeaderInformation(); headerInformation.MergeFrom( (await AEDPoSContractStub.GetConsensusExtraData.CallAsync(triggers[minerInRound.Pubkey] .ToBytesValue())).Value); // Update consensus information. var toUpdate = headerInformation.Round.ExtractInformationToUpdateConsensus(minerInRound.Pubkey); await tester.UpdateValue.SendAsync(toUpdate); } return(triggers); }
public async Task AEDPoSContract_ValidateConsensusBeforeExecution_UpdateValue_Test() { var usingKeyPair = InitialCoreDataCenterKeyPairs[0]; KeyPairProvider.SetKeyPair(usingKeyPair); var triggerForCommand = TriggerInformationProvider.GetTriggerInformationForConsensusCommand(new BytesValue()); var consensusCommand = await AEDPoSContractStub.GetConsensusCommand.CallAsync(triggerForCommand); var updateValue = new AElfConsensusHint { Behaviour = AElfConsensusBehaviour.UpdateValue } .ToByteString(); consensusCommand.Hint = updateValue; triggerForCommand = TriggerInformationProvider .GetTriggerInformationForBlockHeaderExtraData(consensusCommand.ToBytesValue()); var extraDataBytes = await AEDPoSContractStub.GetConsensusExtraData.CallAsync(triggerForCommand); var validateBeforeResult = await AEDPoSContractStub.ValidateConsensusBeforeExecution.CallAsync(extraDataBytes); validateBeforeResult.Success.ShouldBeTrue(); }
private async Task <TransactionList> AEDPoSContract_GenerateConsensusTransactions_FirstRound_ExtraBlockMiner_Test() { var usingKeyPair = BootMinerKeyPair; KeyPairProvider.SetKeyPair(usingKeyPair); var consensusCommand = await AEDPoSContract_GetConsensusCommand_FirstRound_ExtraBlockMiner_Test(); BlockTimeProvider.SetBlockTime(BlockchainStartTimestamp + new Duration { Seconds = AEDPoSContractTestConstants.MiningInterval.Mul(AEDPoSContractTestConstants.InitialMinersCount) .Div(1000) }); var triggerForCommand = await TriggerInformationProvider .GetTriggerInformationForConsensusTransactionsAsync(consensusCommand.ToBytesValue()); var transactionList = await AEDPoSContractStub.GenerateConsensusTransactions.CallAsync(triggerForCommand); transactionList.Transactions.Count.ShouldBe(1); transactionList.Transactions[0].MethodName.ShouldBe(nameof(AEDPoSContractStub.NextRound)); return(transactionList); }
public async Task AEDPoSContract_GetInformationToUpdateConsensus_FirstRound_ExtraBlockMiner_Test() { var usingKeyPair = BootMinerKeyPair; KeyPairProvider.SetKeyPair(usingKeyPair); var consensusCommand = await AEDPoSContract_GetConsensusCommand_FirstRound_ExtraBlockMiner_Test(); BlockTimeProvider.SetBlockTime(BlockchainStartTimestamp + new Duration { Seconds = AEDPoSContractTestConstants.MiningInterval.Mul(AEDPoSContractTestConstants.InitialMinersCount) .Div(1000) }); var triggerForCommand = await TriggerInformationProvider .GetTriggerInformationForBlockHeaderExtraDataAsync(consensusCommand.ToBytesValue()); var extraDataBytes = await AEDPoSContractStub.GetConsensusExtraData.CallAsync(triggerForCommand); var extraData = extraDataBytes.ToConsensusHeaderInformation(); extraData.Round.RoundId.ShouldNotBe(0); extraData.Round.RoundNumber.ShouldBe(2); }
public async Task AEDPoSContract_GetInformationToUpdateConsensus_FirstRound_SecondMiner_Test() { var usingKeyPair = InitialCoreDataCenterKeyPairs[1]; KeyPairProvider.SetKeyPair(usingKeyPair); var consensusCommand = await AEDPoSContract_GetConsensusCommand_FirstRound_SecondMiner_Test(); BlockTimeProvider.SetBlockTime(BlockchainStartTimestamp + new Duration { Seconds = AEDPoSContractTestConstants.MiningInterval.Mul(2).Div(1000) }); var triggerForCommand = await TriggerInformationProvider .GetTriggerInformationForBlockHeaderExtraDataAsync(consensusCommand.ToBytesValue()); var extraDataBytes = await AEDPoSContractStub.GetConsensusExtraData.CallAsync(triggerForCommand); var extraData = extraDataBytes.ToConsensusHeaderInformation(); extraData.Round.RoundId.ShouldNotBe(0); extraData.Round.RoundNumber.ShouldBe(1); extraData.Round.RealTimeMinersInformation[usingKeyPair.PublicKey.ToHex()].OutValue .ShouldNotBeNull(); }
private async Task <ConsensusCommand> AEDPoSContract_GetConsensusCommand_FirstRound_ExtraBlockMiner_Test() { await AEDPoSContract_FirstRound_SecondMiner_Test(); var usingKeyPair = BootMinerKeyPair; KeyPairProvider.SetKeyPair(usingKeyPair); BlockTimeProvider.SetBlockTime(BlockchainStartTimestamp + new Duration { Seconds = AEDPoSContractTestConstants.MiningInterval.Mul(2).Div(1000) }); var triggerForCommand = TriggerInformationProvider.GetTriggerInformationForConsensusCommand(new BytesValue()); var currentRound = await AEDPoSContractStub.GetCurrentRoundInformation.CallAsync(new Empty()); var consensusCommand = await AEDPoSContractStub.GetConsensusCommand.CallAsync(triggerForCommand); consensusCommand.LimitMillisecondsOfMiningBlock.ShouldBe(AEDPoSContractTestConstants .SmallBlockMiningInterval); var hint = new AElfConsensusHint { Behaviour = AElfConsensusBehaviour.NextRound } .ToByteString(); consensusCommand.Hint.ShouldBe(hint); return(consensusCommand); }
public async Task AEDPoSContract_GenerateConsensusTransaction_NextTerm_Test() { var usingKeyPair = BootMinerKeyPair; KeyPairProvider.SetKeyPair(usingKeyPair); //Next term { var consensusCommand = await AEDPoSContract_GetConsensusCommand_FirstRound_ExtraBlockMiner_Test(); var nextTermBehavior = new AElfConsensusHint { Behaviour = AElfConsensusBehaviour.NextTerm } .ToByteString(); consensusCommand.Hint = nextTermBehavior; BlockTimeProvider.SetBlockTime(BlockchainStartTimestamp + new Duration { Seconds = AEDPoSContractTestConstants.MiningInterval .Mul(AEDPoSContractTestConstants.InitialMinersCount) .Div(1000) }); var triggerForCommand = TriggerInformationProvider .GetTriggerInformationForConsensusTransactions(consensusCommand.ToBytesValue()); var transactionList = await AEDPoSContractStub.GenerateConsensusTransactions.CallAsync(triggerForCommand); transactionList.Transactions.Count.ShouldBe(1); transactionList.Transactions[0].MethodName.ShouldBe(nameof(AEDPoSContractStub.NextTerm)); } }
private async Task RunMiningProcess(int roundsCount) { for (var count = 0; count < roundsCount; count++) { var currentRound = await AEDPoSContractStub.GetCurrentRoundInformation.CallAsync(new Empty()); var randomHashes = Enumerable.Range(0, EconomicContractsTestConstants.InitialCoreDataCenterCount) .Select(_ => Hash.FromString($"random{count}")).ToList(); var triggers = Enumerable.Range(0, EconomicContractsTestConstants.InitialCoreDataCenterCount).Select( i => new AElfConsensusTriggerInformation { Pubkey = ByteString.CopyFrom(InitialCoreDataCenterKeyPairs[i].PublicKey), RandomHash = randomHashes[i] }).ToDictionary(t => t.Pubkey.ToHex(), t => t); // Exactly one round except extra block time slot. foreach (var minerInRound in currentRound.RealTimeMinersInformation.Values.OrderBy(m => m.Order)) { var currentKeyPair = InitialCoreDataCenterKeyPairs.First(p => p.PublicKey.ToHex() == minerInRound.Pubkey); KeyPairProvider.SetKeyPair(currentKeyPair); BlockTimeProvider.SetBlockTime(minerInRound.ExpectedMiningTime); var tester = GetAEDPoSContractStub(currentKeyPair); var headerInformation = (await tester.GetConsensusExtraData.CallAsync(triggers[minerInRound.Pubkey] .ToBytesValue())).ToConsensusHeaderInformation(); // Update consensus information. var toUpdate = headerInformation.Round.ExtractInformationToUpdateConsensus(minerInRound.Pubkey); await tester.UpdateValue.SendAsync(toUpdate); for (var i = 0; i < 8; i++) { await tester.UpdateTinyBlockInformation.SendAsync(new TinyBlockInput { ActualMiningTime = TimestampHelper.GetUtcNow(), RoundId = currentRound.RoundId }); } } currentRound.GenerateNextRoundInformation(TimestampHelper.GetUtcNow(), BlockchainStartTimestamp, out var nextRound); await AEDPoSContractStub.NextRound.SendAsync(nextRound); for (var i = 0; i < 8; i++) { await AEDPoSContractStub.UpdateTinyBlockInformation.SendAsync(new TinyBlockInput { ActualMiningTime = TimestampHelper.GetUtcNow(), RoundId = currentRound.RoundId }); } } }
public async Task DecryptMessage_Test() { var previousTriggers = await GenerateEncryptedMessagesAsync(); await BootMinerChangeRoundAsync(); var currentRound = await AEDPoSContractStub.GetCurrentRoundInformation.CallAsync(new Empty()); var randomHashes = Enumerable.Range(0, EconomicContractsTestConstants.InitialCoreDataCenterCount) .Select(_ => Hash.FromString("randomHashes")).ToList(); var triggers = Enumerable.Range(0, EconomicContractsTestConstants.InitialCoreDataCenterCount).Select(i => new AElfConsensusTriggerInformation { Pubkey = ByteString.CopyFrom(InitialCoreDataCenterKeyPairs[i].PublicKey), RandomHash = randomHashes[i], PreviousRandomHash = previousTriggers[InitialCoreDataCenterKeyPairs[i].PublicKey.ToHex()].RandomHash }).ToDictionary(t => t.Pubkey.ToHex(), t => t); // Just `MinimumCount + 1` miners produce blocks. foreach (var minerInRound in currentRound.RealTimeMinersInformation.Values.OrderBy(m => m.Order) .Take(MinimumCount + 1)) { var currentKeyPair = InitialCoreDataCenterKeyPairs.First(p => p.PublicKey.ToHex() == minerInRound.Pubkey); KeyPairProvider.SetKeyPair(currentKeyPair); BlockTimeProvider.SetBlockTime(minerInRound.ExpectedMiningTime); var tester = GetAEDPoSContractStub(currentKeyPair); var headerInformation = new AElfConsensusHeaderInformation(); headerInformation.MergeFrom( (await AEDPoSContractStub.GetInformationToUpdateConsensus.CallAsync(triggers[minerInRound.Pubkey] .ToBytesValue())).Value); // Update consensus information. var toUpdate = headerInformation.Round.ExtractInformationToUpdateConsensus(minerInRound.Pubkey); await tester.UpdateValue.SendAsync(toUpdate); } // Won't pass because currently we postpone the revealing of in values to extra block time slot. // But in values all filled. // var secondRound = await BootMiner.GetCurrentRoundInformation.CallAsync(new Empty()); // secondRound.RealTimeMinersInformation.Values.Count(v => v.PreviousInValue != null) // .ShouldBe(AEDPoSContractTestConstants.InitialMinersCount); }
private async Task <ConsensusCommand> AEDPoSContract_GetConsensusCommand_FirstRound_BootMiner_Test() { KeyPairProvider.SetKeyPair(BootMinerKeyPair); var triggerForCommand = TriggerInformationProvider.GetTriggerInformationForConsensusCommand(new BytesValue()); var consensusCommand = await AEDPoSContractStub.GetConsensusCommand.CallAsync(triggerForCommand); consensusCommand.LimitMillisecondsOfMiningBlock.ShouldBe(AEDPoSContractTestConstants .SmallBlockMiningInterval); var hint = new AElfConsensusHint { Behaviour = AElfConsensusBehaviour.UpdateValue }.ToByteString(); consensusCommand.Hint.ShouldBe(hint); return(consensusCommand); }
private async Task <ConsensusCommand> AEDPoSContract_GetConsensusCommand_FirstRound_SecondMiner_Test() { await AEDPoSContract_FirstRound_BootMiner_Test(); // Now the first time slot of first round already filled by boot miner. var usingKeyPair = InitialCoreDataCenterKeyPairs[1]; KeyPairProvider.SetKeyPair(usingKeyPair); var blockchainStartTimestamp = BlockchainStartTimestamp; BlockTimeProvider.SetBlockTime(blockchainStartTimestamp + new Duration { Seconds = AEDPoSContractTestConstants.MiningInterval.Div(1000) }); var triggerForCommand = TriggerInformationProvider.GetTriggerInformationForConsensusCommand(new BytesValue()); var consensusCommand = await AEDPoSContractStub.GetConsensusCommand.CallAsync(triggerForCommand); consensusCommand.LimitMillisecondsOfMiningBlock.ShouldBe(AEDPoSContractTestConstants .SmallBlockMiningInterval); var hint = new AElfConsensusHint { Behaviour = AElfConsensusBehaviour.UpdateValue } .ToByteString(); consensusCommand.Hint.ShouldBe(hint); consensusCommand.ArrangedMiningTime.ShouldBe(blockchainStartTimestamp + new Duration { Seconds = AEDPoSContractTestConstants.MiningInterval.Mul(7).Div(1000) }); return(consensusCommand); }
public async Task AEDPoSContract_ChangeMinersCount_Test() { const int termIntervalMin = 31536000 / 60; var maxCount = ValidationDataCenterKeyPairs.Count; await InitializeCandidates(maxCount); var firstRound = await AEDPoSContractStub.GetCurrentRoundInformation.CallAsync(new Empty()); var randomHashes = Enumerable.Range(0, EconomicContractsTestConstants.InitialCoreDataCenterCount) .Select(_ => Hash.FromString("randomHashes")).ToList(); var triggers = Enumerable.Range(0, EconomicContractsTestConstants.InitialCoreDataCenterCount).Select(i => new AElfConsensusTriggerInformation { Pubkey = ByteString.CopyFrom(InitialCoreDataCenterKeyPairs[i].PublicKey), InValue = randomHashes[i] }).ToDictionary(t => t.Pubkey.ToHex(), t => t); var voter = GetElectionContractTester(VoterKeyPairs[0]); foreach (var candidateKeyPair in ValidationDataCenterKeyPairs) { var voteResult = await voter.Vote.SendAsync(new VoteMinerInput { CandidatePubkey = candidateKeyPair.PublicKey.ToHex(), Amount = 10 + new Random().Next(1, 10), EndTimestamp = TimestampHelper.GetUtcNow().AddDays(100) }); voteResult.TransactionResult.Status.ShouldBe(TransactionResultStatus.Mined); } foreach (var minerInRound in firstRound.RealTimeMinersInformation.Values.OrderBy(m => m.Order)) { var currentKeyPair = InitialCoreDataCenterKeyPairs.First(p => p.PublicKey.ToHex() == minerInRound.Pubkey); KeyPairProvider.SetKeyPair(currentKeyPair); BlockTimeProvider.SetBlockTime(minerInRound.ExpectedMiningTime); var tester = GetAEDPoSContractStub(currentKeyPair); var headerInformation = (await AEDPoSContractStub.GetConsensusExtraData.CallAsync(triggers[minerInRound.Pubkey] .ToBytesValue())).ToConsensusHeaderInformation(); // Update consensus information. var toUpdate = headerInformation.Round.ExtractInformationToUpdateConsensus(minerInRound.Pubkey); await tester.UpdateValue.SendAsync(toUpdate); } var changeTermTime = BlockchainStartTimestamp.ToDateTime(); BlockTimeProvider.SetBlockTime(changeTermTime.ToTimestamp()); var nextTermInformation = (await AEDPoSContractStub.GetConsensusExtraData.CallAsync( new AElfConsensusTriggerInformation { Behaviour = AElfConsensusBehaviour.NextRound, Pubkey = ByteString.CopyFrom(BootMinerKeyPair.PublicKey) }.ToBytesValue())).ToConsensusHeaderInformation(); await AEDPoSContractStub.NextRound.SendAsync(nextTermInformation.Round); changeTermTime = BlockchainStartTimestamp.ToDateTime().AddMinutes(termIntervalMin).AddSeconds(10); BlockTimeProvider.SetBlockTime(changeTermTime.ToTimestamp()); nextTermInformation = (await AEDPoSContractStub.GetConsensusExtraData.CallAsync( new AElfConsensusTriggerInformation { Behaviour = AElfConsensusBehaviour.NextTerm, Pubkey = ByteString.CopyFrom(BootMinerKeyPair.PublicKey) }.ToBytesValue())).ToConsensusHeaderInformation(); var transactionResult = await AEDPoSContractStub.NextTerm.SendAsync(nextTermInformation.Round); transactionResult.TransactionResult.Status.ShouldBe(TransactionResultStatus.Mined); var newMinerStub = GetAEDPoSContractStub(ValidationDataCenterKeyPairs[0]); var termCount = 0; var minerCount = 0; while (minerCount < maxCount) { var currentRound = await newMinerStub.GetCurrentRoundInformation.CallAsync(new Empty()); var firstPubKey = currentRound.RealTimeMinersInformation.Keys.First(); newMinerStub = GetAEDPoSContractStub(ValidationDataCenterKeyPairs.First(o => o.PublicKey.ToHex() == firstPubKey)); minerCount = currentRound.RealTimeMinersInformation.Count; Assert.Equal(AEDPoSContractTestConstants.SupposedMinersCount.Add(termCount.Mul(2)), minerCount); changeTermTime = BlockchainStartTimestamp.ToDateTime() .AddMinutes((termCount + 2).Mul(termIntervalMin)).AddSeconds(10); BlockTimeProvider.SetBlockTime(changeTermTime.ToTimestamp()); var nextRoundInformation = (await newMinerStub.GetConsensusExtraData.CallAsync( new AElfConsensusTriggerInformation { Behaviour = AElfConsensusBehaviour.NextTerm, Pubkey = currentRound.RealTimeMinersInformation.ElementAt(0).Value.Pubkey.ToByteString() }.ToBytesValue())).ToConsensusHeaderInformation(); await newMinerStub.NextTerm.SendAsync(nextRoundInformation.Round); termCount++; } }
public async Task Candidates_NotEnough_Test() { //await ElectionContractStub.RegisterElectionVotingEvent.SendAsync(new Empty()); //await InitializeVoters(); await InitializeCandidates(EconomicContractsTestConstants.InitialCoreDataCenterCount); var firstRound = await AEDPoSContractStub.GetCurrentRoundInformation.CallAsync(new Empty()); var randomHashes = Enumerable.Range(0, EconomicContractsTestConstants.InitialCoreDataCenterCount) .Select(_ => Hash.FromString("hash3")).ToList(); var triggers = Enumerable.Range(0, EconomicContractsTestConstants.InitialCoreDataCenterCount).Select(i => new AElfConsensusTriggerInformation { Pubkey = ByteString.CopyFrom(InitialCoreDataCenterKeyPairs[i].PublicKey), InValue = randomHashes[i] }).ToDictionary(t => t.Pubkey.ToHex(), t => t); var voter = GetElectionContractTester(VoterKeyPairs[0]); foreach (var candidateKeyPair in ValidationDataCenterKeyPairs.Take(EconomicContractsTestConstants .InitialCoreDataCenterCount)) { await voter.Vote.SendAsync(new VoteMinerInput { CandidatePubkey = candidateKeyPair.PublicKey.ToHex(), Amount = 100 + new Random().Next(1, 200), EndTimestamp = TimestampHelper.GetUtcNow().AddDays(100) }); } foreach (var minerInRound in firstRound.RealTimeMinersInformation.Values.OrderBy(m => m.Order)) { var currentKeyPair = InitialCoreDataCenterKeyPairs.First(p => p.PublicKey.ToHex() == minerInRound.Pubkey); KeyPairProvider.SetKeyPair(currentKeyPair); BlockTimeProvider.SetBlockTime(minerInRound.ExpectedMiningTime); var tester = GetAEDPoSContractStub(currentKeyPair); var headerInformation = (await AEDPoSContractStub.GetConsensusExtraData.CallAsync(triggers[minerInRound.Pubkey] .ToBytesValue())).ToConsensusHeaderInformation(); // Update consensus information. var toUpdate = headerInformation.Round.ExtractInformationToUpdateConsensus(minerInRound.Pubkey); await tester.UpdateValue.SendAsync(toUpdate); } var changeTermTime = BlockchainStartTimestamp.ToDateTime() .AddMinutes(AEDPoSContractTestConstants.TimeEachTerm + 1); BlockTimeProvider.SetBlockTime(changeTermTime.ToTimestamp()); var nextTermInformation = (await AEDPoSContractStub.GetConsensusExtraData.CallAsync( new AElfConsensusTriggerInformation { Behaviour = AElfConsensusBehaviour.NextTerm, Pubkey = ByteString.CopyFrom(BootMinerKeyPair.PublicKey) }.ToBytesValue())).ToConsensusHeaderInformation(); await AEDPoSContractStub.NextTerm.SendAsync(nextTermInformation.Round); // First candidate cheat others with in value. var oneCandidate = GetAEDPoSContractStub(ValidationDataCenterKeyPairs[0]); var anotherCandidate = GetAEDPoSContractStub(ValidationDataCenterKeyPairs[1]); var randomHash = Hash.FromString("hash5"); var informationOfSecondRound = (await AEDPoSContractStub.GetConsensusExtraData.CallAsync( new AElfConsensusTriggerInformation { Behaviour = AElfConsensusBehaviour.UpdateValue, PreviousInValue = Hash.Empty, InValue = randomHash, Pubkey = ByteString.CopyFrom(ValidationDataCenterKeyPairs[0].PublicKey) }.ToBytesValue())).ToConsensusHeaderInformation(); var updateResult = await oneCandidate.UpdateValue.SendAsync( informationOfSecondRound.Round.ExtractInformationToUpdateConsensus(ValidationDataCenterKeyPairs[0] .PublicKey.ToHex())); var thirdRoundStartTime = changeTermTime.AddMinutes(AEDPoSContractTestConstants.TimeEachTerm + 2); BlockTimeProvider.SetBlockTime(thirdRoundStartTime.ToTimestamp()); var thirdRound = (await AEDPoSContractStub.GetConsensusExtraData.CallAsync( new AElfConsensusTriggerInformation { Behaviour = AElfConsensusBehaviour.NextRound, Pubkey = ByteString.CopyFrom(ValidationDataCenterKeyPairs[0].PublicKey) }.ToBytesValue())).ToConsensusHeaderInformation().Round; await oneCandidate.NextRound.SendAsync(thirdRound); var cheatInformation = (await AEDPoSContractStub.GetConsensusExtraData.CallAsync( new AElfConsensusTriggerInformation { Behaviour = AElfConsensusBehaviour.UpdateValue, PreviousInValue = Hash.FromMessage(randomHash), // Not same as before. InValue = Hash.FromString("InValue"), // Don't care this value in current test case. Pubkey = ByteString.CopyFrom(ValidationDataCenterKeyPairs[0].PublicKey) }.ToBytesValue())).ToConsensusHeaderInformation(); await oneCandidate.UpdateValue.SendAsync( cheatInformation.Round.ExtractInformationToUpdateConsensus(ValidationDataCenterKeyPairs[0].PublicKey .ToHex())); }
internal async Task <Hash> AEDPoSContract_GetRandomNumber_Test() { var order = await AEDPoSContract_RequestRandomNumber_Test(); var currentRound = await AEDPoSContractStub.GetCurrentRoundInformation.CallAsync(new Empty()); var randomHashes = Enumerable.Range(0, EconomicContractsTestConstants.InitialCoreDataCenterCount) .Select(i => Hash.FromString($"random{i}")).ToList(); var triggers = Enumerable.Range(0, EconomicContractsTestConstants.InitialCoreDataCenterCount).Select(i => new AElfConsensusTriggerInformation { Pubkey = ByteString.CopyFrom(InitialCoreDataCenterKeyPairs[i].PublicKey), RandomHash = randomHashes[i] }).ToDictionary(t => t.Pubkey.ToHex(), t => t); // Exactly one round except extra block time slot. foreach (var minerInRound in currentRound.RealTimeMinersInformation.Values.OrderBy(m => m.Order)) { var currentKeyPair = InitialCoreDataCenterKeyPairs.First(p => p.PublicKey.ToHex() == minerInRound.Pubkey); KeyPairProvider.SetKeyPair(currentKeyPair); BlockTimeProvider.SetBlockTime(minerInRound.ExpectedMiningTime); var stub = GetAEDPoSContractStub(currentKeyPair); var headerInformation = (await stub.GetConsensusExtraData.CallAsync(triggers[minerInRound.Pubkey] .ToBytesValue())).ToConsensusHeaderInformation(); // Update consensus information. var toUpdate = headerInformation.Round.ExtractInformationToUpdateConsensus(minerInRound.Pubkey); await stub.UpdateValue.SendAsync(toUpdate); { var chain = await BlockchainService.GetChainAsync(); if (chain.BestChainHeight < order.BlockHeight) { // Not enough. var transactionResult = (await AEDPoSContractStub.GetRandomNumber.SendAsync(order.TokenHash)) .TransactionResult; transactionResult.Error.ShouldContain("Still preparing random number."); } } for (var i = 0; i < 7; i++) { await stub.UpdateTinyBlockInformation.SendAsync(new TinyBlockInput { ActualMiningTime = TimestampHelper.GetUtcNow(), RoundId = currentRound.RoundId }); } } // Now it's enough. { var randomNumber = (await AEDPoSContractStub.GetRandomNumber.SendAsync(order.TokenHash)).Output; randomNumber.Value.ShouldNotBeEmpty(); } return(order.TokenHash); }