Beispiel #1
0
        public async Task Mine_Test()
        {
            var chain = await _blockchainService.GetChainAsync();

            var blockStateSet = new BlockStateSet
            {
                BlockHash   = chain.BestChainHash,
                BlockHeight = chain.BestChainHeight,
            };
            await _blockStateSetManger.SetBlockStateSetAsync(blockStateSet);

            var transactions = _kernelTestHelper.GenerateTransactions(5, chain.BestChainHeight, chain.BestChainHash);
            await _transactionPoolService.UpdateTransactionPoolByBestChainAsync(chain.BestChainHash,
                                                                                chain.BestChainHeight);

            await _transactionPoolService.AddTransactionsAsync(transactions);

            await Task.Delay(200);

            await _transactionPoolService.UpdateTransactionPoolByBestChainAsync(chain.BestChainHash,
                                                                                chain.BestChainHeight);

            {
                await _transactionPackingOptionProvider.SetTransactionPackingOptionAsync(new BlockIndex
                {
                    BlockHash   = chain.BestChainHash,
                    BlockHeight = chain.BestChainHeight
                }, false);

                var blockTime = TimestampHelper.GetUtcNow();
                var result    = await _minerService.MineAsync(chain.BestChainHash, chain.BestChainHeight, blockTime,
                                                              TimestampHelper.DurationFromSeconds(4));
                await CheckMiningResultAsync(result, blockTime, 0);
            }

            {
                await _transactionPackingOptionProvider.SetTransactionPackingOptionAsync(new BlockIndex
                {
                    BlockHash   = chain.BestChainHash,
                    BlockHeight = chain.BestChainHeight
                }, true);

                await _blockTransactionLimitProvider.SetLimitAsync(new BlockIndex
                {
                    BlockHash   = chain.BestChainHash,
                    BlockHeight = chain.BestChainHeight
                }, 3);

                var blockTime = TimestampHelper.GetUtcNow();
                var result    = await _minerService.MineAsync(chain.BestChainHash, chain.BestChainHeight, blockTime,
                                                              TimestampHelper.DurationFromSeconds(4));
                await CheckMiningResultAsync(result, blockTime, 2);
            }
        }
        public async Task TransactionPackingOption_Test()
        {
            await AddBlockStateSetAsync(_kernelTestHelper.BestBranchBlockList[9]);
            await AddBlockStateSetAsync(_kernelTestHelper.BestBranchBlockList[10]);
            await AddBlockStateSetAsync(_kernelTestHelper.ForkBranchBlockList[4]);

            var context = new ChainContext
            {
                BlockHash   = _kernelTestHelper.BestBranchBlockList[9].GetHash(),
                BlockHeight = _kernelTestHelper.BestBranchBlockList[9].Height
            };

            var isTransactionPackable = _transactionPackingOptionProvider.IsTransactionPackable(context);

            isTransactionPackable.ShouldBeTrue();

            await _transactionPackingOptionProvider.SetTransactionPackingOptionAsync(context, false);

            isTransactionPackable = _transactionPackingOptionProvider.IsTransactionPackable(context);
            isTransactionPackable.ShouldBeFalse();

            isTransactionPackable = _transactionPackingOptionProvider.IsTransactionPackable(new ChainContext
            {
                BlockHash   = _kernelTestHelper.BestBranchBlockList[10].GetHash(),
                BlockHeight = _kernelTestHelper.BestBranchBlockList[10].Height
            });
            isTransactionPackable.ShouldBeFalse();

            isTransactionPackable = _transactionPackingOptionProvider.IsTransactionPackable(new ChainContext
            {
                BlockHash   = _kernelTestHelper.ForkBranchBlockList[4].GetHash(),
                BlockHeight = _kernelTestHelper.ForkBranchBlockList[4].Height
            });
            isTransactionPackable.ShouldBeTrue();

            await _transactionPackingOptionProvider.SetTransactionPackingOptionAsync(context, true);

            isTransactionPackable = _transactionPackingOptionProvider.IsTransactionPackable(context);
            isTransactionPackable.ShouldBeTrue();
        }
Beispiel #3
0
        public async Task GenerateTransactionsAsync_Without_PackedTransaction_Test()
        {
            var address    = NormalAddress;
            var blockIndex = new BlockIndex
            {
                BlockHash   = HashHelper.ComputeFrom("BlockHash"),
                BlockHeight = 10
            };
            var proposalId = HashHelper.ComputeFrom("proposal");

            _proposalTestHelper.AddNotVotedProposalIdList(new List <Hash> {
                proposalId
            });

            var proposalCacheProvider = GetRequiredService <IProposalProvider>();

            proposalCacheProvider.AddProposal(proposalId, 5);
            await _transactionPackingOptionProvider.SetTransactionPackingOptionAsync(blockIndex, false);

            var transactionList = await _proposalApprovalTransactionGenerator.GenerateTransactionsAsync(address, blockIndex.BlockHeight, blockIndex.BlockHash);

            transactionList.Count.ShouldBe(1);
        }
        protected override Task ProcessLogEventAsync(Block block, LogEvent logEvent)
        {
            var distanceToLib = new IrreversibleBlockHeightUnacceptable();

            distanceToLib.MergeFrom(logEvent);

            var blockIndex = new BlockIndex
            {
                BlockHash   = block.GetHash(),
                BlockHeight = block.Height
            };

            if (distanceToLib.DistanceToIrreversibleBlockHeight > 0)
            {
                Logger.LogDebug($"Distance to lib height: {distanceToLib.DistanceToIrreversibleBlockHeight}");
                _transactionPackingOptionProvider.SetTransactionPackingOptionAsync(blockIndex, false);
            }
            else
            {
                _transactionPackingOptionProvider.SetTransactionPackingOptionAsync(blockIndex, true);
            }

            return(Task.CompletedTask);
        }
        public async Task FIllExtraData_TransactionPackingDisabled()
        {
            var merkleTreeRoot = HashHelper.ComputeFrom("MerkleTreeRoot");
            var expected       = new CrossChainExtraData {
                TransactionStatusMerkleTreeRoot = merkleTreeRoot
            };
            var header = new BlockHeader
            {
                PreviousBlockHash = HashHelper.ComputeFrom("PreviousHash"),
                Height            = 2
            };

            _crossChainTestHelper.AddFakeExtraData(header.PreviousBlockHash, expected);
            await _transactionPackingOptionProvider.SetTransactionPackingOptionAsync(new BlockIndex
            {
                BlockHash   = header.PreviousBlockHash,
                BlockHeight = header.Height - 1
            }, false);

            var bytes = await _crossChainBlockExtraDataProvider.GetBlockHeaderExtraDataAsync(header);

            Assert.Empty(bytes);
        }