public async Task ParallelExecuteAsync_Test()
        {
            var chain = await BlockchainService.GetChainAsync();

            (PrepareTransactions, KeyPairs) = await OsTestHelper.PrepareTokenForParallel(10);

            Block = OsTestHelper.GenerateBlock(chain.BestChainHash, chain.BestChainHeight, PrepareTransactions);
            //await BlockExecutingService.ExecuteBlockAsync(Block.Header, PrepareTransactions);
            await OsTestHelper.BroadcastTransactions(PrepareTransactions);

            Block = await MinerService.MineAsync(chain.BestChainHash, chain.BestChainHeight,
                                                 TimestampHelper.GetUtcNow(), TimestampHelper.DurationFromSeconds(4));

            await BlockchainService.AddBlockAsync(Block);

            await BlockAttachService.AttachBlockAsync(Block);

            SystemTransactions = await OsTestHelper.GenerateTransferTransactions(1);

            CancellableTransactions = OsTestHelper.GenerateTransactionsWithoutConflict(KeyPairs);
            Block = OsTestHelper.GenerateBlock(Block.GetHash(), Block.Height,
                                               SystemTransactions.Concat(CancellableTransactions));

            await OsTestHelper.BroadcastTransactions(SystemTransactions.Concat(CancellableTransactions));

            var block = await BlockExecutingService.ExecuteBlockAsync(Block.Header,
                                                                      SystemTransactions, CancellableTransactions, CancellationToken.None);

            block.TransactionIds.Count().ShouldBeGreaterThan(10);
        }
Exemple #2
0
        public async Task IterationSetup()
        {
            var chain = await _blockchainService.GetChainAsync();

            var tokenAmount = TransactionCount / GroupCount;

            (_prepareTransactions, _keyPairs) = await _osTestHelper.PrepareTokenForParallel(GroupCount, tokenAmount);

            _block = _osTestHelper.GenerateBlock(chain.BestChainHash, chain.BestChainHeight, _prepareTransactions);
            await _blockExecutingService.ExecuteBlockAsync(_block.Header, _prepareTransactions);

            await _osTestHelper.BroadcastTransactions(_prepareTransactions);

            _block = (await _minerService.MineAsync(chain.BestChainHash, chain.BestChainHeight,
                                                    TimestampHelper.GetUtcNow(), TimestampHelper.DurationFromSeconds(4))).Block;

            _systemTransactions = await _osTestHelper.GenerateTransferTransactions(1);

            _cancellableTransactions = await _osTestHelper.GenerateTransactionsWithoutConflictAsync(_keyPairs, tokenAmount);

            chain = await _blockchainService.GetChainAsync();

            _block = _osTestHelper.GenerateBlock(chain.BestChainHash, chain.BestChainHeight,
                                                 _systemTransactions.Concat(_cancellableTransactions));
        }
        public async Task Mine_Test()
        {
            var chain = await _blockchainService.GetChainAsync();

            Timestamp        blockTime;
            BlockExecutedSet miningResult;

            {
                blockTime    = TimestampHelper.GetUtcNow().AddSeconds(-4);
                miningResult = await _miningService.MineAsync(new RequestMiningDto
                {
                    BlockExecutionTime    = TimestampHelper.DurationFromSeconds(1),
                    PreviousBlockHash     = chain.BestChainHash,
                    PreviousBlockHeight   = chain.BestChainHeight,
                    TransactionCountLimit = 100
                },
                                                              _kernelTestHelper.GenerateTransactions(10), blockTime);
                await CheckMiningResultAsync(miningResult, blockTime, 0);
            }

            {
                blockTime    = TimestampHelper.GetUtcNow().AddSeconds(4);
                miningResult = await _miningService.MineAsync(new RequestMiningDto
                {
                    BlockExecutionTime    = TimestampHelper.DurationFromMilliseconds(int.MaxValue),
                    PreviousBlockHash     = chain.BestChainHash,
                    PreviousBlockHeight   = chain.BestChainHeight,
                    TransactionCountLimit = 100
                },
                                                              _kernelTestHelper.GenerateTransactions(10), blockTime);
                await CheckMiningResultAsync(miningResult, blockTime, 10);
            }

            {
                blockTime    = TimestampHelper.GetUtcNow();
                miningResult = await _miningService.MineAsync(new RequestMiningDto
                {
                    BlockExecutionTime    = TimestampHelper.DurationFromSeconds(4),
                    PreviousBlockHash     = chain.BestChainHash,
                    PreviousBlockHeight   = chain.BestChainHeight,
                    TransactionCountLimit = 100
                },
                                                              _kernelTestHelper.GenerateTransactions(10), blockTime);
                await CheckMiningResultAsync(miningResult, blockTime, 10);
            }

            {
                blockTime    = TimestampHelper.GetUtcNow();
                miningResult = await _miningService.MineAsync(new RequestMiningDto
                {
                    BlockExecutionTime    = TimestampHelper.DurationFromSeconds(4),
                    PreviousBlockHash     = chain.BestChainHash,
                    PreviousBlockHeight   = chain.BestChainHeight,
                    TransactionCountLimit = 5
                },
                                                              _kernelTestHelper.GenerateTransactions(10), blockTime);
                await CheckMiningResultAsync(miningResult, blockTime, 4);
            }
        }
Exemple #4
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);
            }
        }
Exemple #5
0
        public async Task WrongParallelTest()
        {
            var chain = await _blockchainService.GetChainAsync();

            await _blockchainService.SetIrreversibleBlockAsync(chain, chain.BestChainHeight, chain.BestChainHash);

            var transactions =
                _parallelTestHelper.GenerateBasicFunctionWithParallelTransactions(_groupCount, _transactionCount);
            await _parallelTestHelper.BroadcastTransactions(transactions);

            var poolSize = await _txHub.GetTransactionPoolSizeAsync();

            poolSize.ShouldBe(transactions.Count);

            var groupedTransactions = await _grouper.GroupAsync(
                new ChainContext { BlockHash = chain.BestChainHash, BlockHeight = chain.BestChainHeight },
                transactions);

            groupedTransactions.Parallelizables.Count.ShouldBe(_transactionCount);
            groupedTransactions.NonParallelizables.Count.ShouldBe(0);

            var block = await _minerService.MineAsync(chain.BestChainHash, chain.BestChainHeight, TimestampHelper.GetUtcNow(),
                                                      TimestampHelper.DurationFromSeconds(4));

            block.TransactionIds.Count().ShouldBe(_groupCount);
            await _blockchainService.AddBlockAsync(block);

            await _blockAttachService.AttachBlockAsync(block);

            var codeRemarks =
                await _codeRemarksManager.GetCodeRemarksAsync(
                    Hash.FromRawBytes(_parallelTestHelper.BasicFunctionWithParallelContractCode));

            codeRemarks.ShouldNotBeNull();
            codeRemarks.NonParallelizable.ShouldBeTrue();

            groupedTransactions = await _grouper.GroupAsync(new ChainContext { BlockHash = block.GetHash(), BlockHeight = block.Height },
                                                            transactions);

            groupedTransactions.Parallelizables.Count.ShouldBe(0);
            groupedTransactions.NonParallelizables.Count.ShouldBe(_transactionCount);

            poolSize = await _txHub.GetTransactionPoolSizeAsync();

            poolSize.ShouldBe(transactions.Count - block.TransactionIds.Count());

            block = await _minerService.MineAsync(block.GetHash(), block.Height, TimestampHelper.GetUtcNow(),
                                                  TimestampHelper.DurationFromSeconds(4));

            block.TransactionIds.Count().ShouldBe(poolSize);
        }
 public void CleanIdleExecutive()
 {
     foreach (var executivePool in _smartContractExecutiveProvider.GetExecutivePools())
     {
         var executiveBag = executivePool.Value;
         if (executiveBag.Count > ExecutiveClearLimit && executiveBag.Min(o => o.LastUsedTime) <
             TimestampHelper.GetUtcNow() - TimestampHelper.DurationFromSeconds(ExecutiveExpirationTime))
         {
             if (executiveBag.TryTake(out _))
             {
                 Logger.LogDebug($"Cleaned an idle executive for address {executivePool.Key}.");
             }
         }
     }
 }
Exemple #7
0
 public async Task MineBlockTest()
 {
     _block = await _minerService.MineAsync(_chain.BestChainHash, _chain.BestChainHeight,
                                            TimestampHelper.GetUtcNow(), TimestampHelper.DurationFromSeconds(4));
 }
Exemple #8
0
 public void MineTest()
 {
     AsyncHelper.RunSync(async() =>
     {
         var chain = await _blockchainService.GetChainAsync();
         _block    = await _minerService.MineAsync(chain.BestChainHash, chain.BestChainHeight,
                                                   TimestampHelper.GetUtcNow(), TimestampHelper.DurationFromSeconds(4));
     });
     _counter.Increment();
 }
Exemple #9
0
        public void DurationFromSeconds_Test()
        {
            var result = TimestampHelper.DurationFromSeconds(1000);

            result.Seconds.ShouldBe(1000);
        }