public void GetBlockExtraData_Test() { var blockHeader = new BlockHeader() { Height = 2, // no extra data in genesis block ExtraData = { ByteString.CopyFromUtf8("test1") } }; var queryResult = _blockExtraDataService.GetExtraDataFromBlockHeader("IBlockExtraDataProvider", blockHeader); queryResult.ShouldBe(blockHeader.ExtraData[0]); var queryResult1 = _blockExtraDataService.GetExtraDataFromBlockHeader("ConsensusExtraDataProvider", blockHeader); queryResult1.ShouldBeNull(); }
public async Task <List <string> > GetPubkeyList(BlockHeader blockHeader) { var consensusExtraData = _blockExtraDataService.GetExtraDataFromBlockHeader("Consensus", blockHeader); var information = AElfConsensusHeaderInformation.Parser.ParseFrom(consensusExtraData); var round = information.Round; var currentPubkey = (await _accountService.GetPublicKeyAsync()).ToHex(); if (round.RealTimeMinersInformation.Values.Any(m => m.OutValue != null) && round.RealTimeMinersInformation.ContainsKey(currentPubkey)) { // At least someone mined blocks during current round. var currentOrder = round.RealTimeMinersInformation.Values.First(m => m.Pubkey == currentPubkey).Order; var minersCount = round.RealTimeMinersInformation.Count; var ebp = round.RealTimeMinersInformation.Values.First(m => m.IsExtraBlockProducer).Pubkey; if (currentOrder >= minersCount) { return new List <string> { ebp } } ; var nextMiners = round.RealTimeMinersInformation.Values.Where(m => m.Order > currentOrder) .OrderBy(m => m.Order).Select(m => m.Pubkey).ToList(); nextMiners.AddIfNotContains(ebp); return(nextMiners); } return(round.RealTimeMinersInformation.Values.OrderBy(m => m.Order).Select(m => m.Pubkey).ToList()); } }
/// <summary> /// Get AEDPoS latest round information from last block header's consensus extra data of best chain. /// </summary> /// <returns></returns> public async Task <RoundDto> GetCurrentRoundInformationAsync() { var blockHeader = await _blockchainService.GetBestChainLastBlockHeaderAsync(); var consensusExtraData = _blockExtraDataService.GetExtraDataFromBlockHeader("Consensus", blockHeader); var information = AElfConsensusHeaderInformation.Parser.ParseFrom(consensusExtraData); var round = information.Round; return(new RoundDto { ExtraBlockProducerOfPreviousRound = round.ExtraBlockProducerOfPreviousRound, RealTimeMinerInformation = round.RealTimeMinersInformation.ToDictionary(i => i.Key, i => new MinerInRoundDto { Order = i.Value.Order, ExpectedMiningTime = i.Value.ExpectedMiningTime.ToDateTime(), ActualMiningTimes = i.Value.ActualMiningTimes.Select(t => t.ToDateTime()).ToList(), ProducedTinyBlocks = i.Value.ProducedTinyBlocks, ProducedBlocks = i.Value.ProducedBlocks, MissedBlocks = i.Value.MissedTimeSlots, InValue = i.Value.InValue?.ToHex(), OutValue = i.Value.OutValue?.ToHex(), PreviousInValue = i.Value.PreviousInValue?.ToHex() }), RoundNumber = round.RoundNumber, TermNumber = round.TermNumber, RoundId = round.RealTimeMinersInformation.Values.Select(bpInfo => bpInfo.ExpectedMiningTime.Seconds) .Sum() }); }
private ByteString ExtractCrossChainExtraData(BlockHeader header) { var bytes = _blockExtraDataService.GetExtraDataFromBlockHeader( CrossChainConstants.CrossChainExtraDataKey, header); return(bytes); }
public bool TryGetSystemTransactionCount(BlockHeader blockHeader, out int count) { count = 0; var byteString = _blockExtraDataService.GetExtraDataFromBlockHeader(BlockHeaderExtraDataKey, blockHeader); if (byteString == null) { return(false); } count = Int32Value.Parser.ParseFrom(byteString).Value; return(true); }
public ByteString ExtractConsensusExtraData(BlockHeader header) { var consensusExtraData = _blockExtraDataService.GetExtraDataFromBlockHeader("Consensus", header); var headerInformation = AElfConsensusHeaderInformation.Parser.ParseFrom(consensusExtraData); // Validate header information if (headerInformation.SenderPubkey != header.SignerPubkey) { return(null); } return(consensusExtraData); }
public async Task <bool> ValidateBeforeAttachAsync(IBlock block) { if (block.Height == KernelConstants.GenesisBlockHeight) { return(true); } if (block.Header.BlockExtraDatas.Count == 0) { Logger.LogWarning($"Block header extra data is empty {block}"); return(false); } var consensusExtraData = _blockExtraDataService.GetExtraDataFromBlockHeader("Consensus", block.Header); if (consensusExtraData == null || consensusExtraData.IsEmpty) { Logger.LogWarning($"Consensus extra data is empty {block}"); return(false); } return(true); }
public ByteString ExtractConsensusExtraData(BlockHeader header) { var consensusExtraData = _blockExtraDataService.GetExtraDataFromBlockHeader(_consensusExtraDataKeyProvider.BlockHeaderExtraDataKey, header); if (consensusExtraData == null) { return(null); } var headerInformation = AElfConsensusHeaderInformation.Parser.ParseFrom(consensusExtraData); // Validate header information return(headerInformation.SenderPubkey != header.SignerPubkey ? null : consensusExtraData); }
public async Task <List <string> > GetPubkeyList(BlockHeader blockHeader) { var consensusExtraData = _blockExtraDataService.GetExtraDataFromBlockHeader(_consensusExtraDataNameProvider.ExtraDataName, blockHeader); var consensusInformation = AElfConsensusHeaderInformation.Parser.ParseFrom(consensusExtraData); if (consensusInformation.Behaviour == AElfConsensusBehaviour.TinyBlock) { // The orders changed every round, and the orders can be updated during every behaviour of UPDATE_VALUE or NEXT_ROUND, // so we can skip the update for TINY_BLOCK. return(_cachedPubkeyList); } var round = consensusInformation.Round; var currentPubkey = (await _accountService.GetPublicKeyAsync()).ToHex(); var minersCount = round.RealTimeMinersInformation.Count; if (round.RealTimeMinersInformation.Values.Any(m => m.OutValue != null) && round.RealTimeMinersInformation.ContainsKey(currentPubkey)) { // If any miner mined blocks during current round. var currentMiner = round.RealTimeMinersInformation.Values.Single(m => m.Pubkey == currentPubkey); var currentOrder = currentMiner.Order; var ebp = round.RealTimeMinersInformation.Values.Single(m => m.IsExtraBlockProducer).Pubkey; if (currentOrder >= minersCount) { return new List <string> { ebp } } ; var nextMiners = round.RealTimeMinersInformation.Values.Where(m => m.Order > currentOrder) .OrderBy(m => m.Order).Select(m => m.Pubkey).ToList(); nextMiners.AddIfNotContains(ebp); return(nextMiners); } return(round.RealTimeMinersInformation.Values.OrderBy(m => m.Order).Select(m => m.Pubkey).ToList()); } }
private ByteString GetExtraDataFromHeader(BlockHeader header, string symbol) { return(_blockExtraDataService.GetExtraDataFromBlockHeader(symbol, header)); }
private CrossChainExtraData ExtractCrossChainExtraData(BlockHeader header) { var bytes = _blockExtraDataService.GetExtraDataFromBlockHeader("CrossChain", header); return(bytes == ByteString.Empty || bytes == null ? null : CrossChainExtraData.Parser.ParseFrom(bytes)); }