Example #1
0
        public async Task GetBlockHeightTest()
        {
            // Get current height
            var response = await GetResponseAsStringAsync("/api/blockChain/blockHeight");

            var currentHeight = long.Parse(response);

            var chain = await _blockchainService.GetChainAsync();

            currentHeight.ShouldBe(chain.BestChainHeight);

            // Mined one block
            var transaction = await _osTestHelper.GenerateTransferTransaction();

            await _osTestHelper.BroadcastTransactions(new List <Transaction> {
                transaction
            });

            await _osTestHelper.MinedOneBlock();

            // Get latest height
            response = await GetResponseAsStringAsync("/api/blockChain/blockHeight");

            var height = long.Parse(response);

            height.ShouldBe(currentHeight + 1);
        }
Example #2
0
        public async Task Get_BlockHeight_Success()
        {
            // Get current height
            var response = await JsonCallAsJObject("/chain", "GetBlockHeight");

            var currentHeight = (int)response["result"];

            // Mined one block
            var chain = await _blockchainService.GetChainAsync();

            var transaction = await _osTestHelper.GenerateTransferTransaction();

            await _osTestHelper.BroadcastTransactions(new List <Transaction> {
                transaction
            });

            await _osTestHelper.MinedOneBlock();

            // Get latest height
            response = await JsonCallAsJObject("/chain", "GetBlockHeight");

            var height = (int)response["result"];

            height.ShouldBe(currentHeight + 1);
        }
        public async Task GlobalSetup()
        {
            _chains = GetRequiredService <IBlockchainStore <Chain> >();
            _chainStateInfoCollection = GetRequiredService <IStateStore <ChainStateInfo> >();
            _blockchainStateService   = GetRequiredService <IBlockchainStateService>();
            _blockStateSetManger      = GetRequiredService <IBlockStateSetManger>();
            _blockchainService        = GetRequiredService <IBlockchainService>();
            _osTestHelper             = GetRequiredService <OSTestHelper>();
            _chainManager             = GetRequiredService <IChainManager>();
            _blockManager             = GetRequiredService <IBlockManager>();
            _transactionManager       = GetRequiredService <ITransactionManager>();
            _transactionPoolService   = GetRequiredService <ITransactionPoolService>();


            _blockStateSets = new List <BlockStateSet>();
            _blocks         = new List <Block>();

            _chain = await _blockchainService.GetChainAsync();

            var blockHash = _chain.BestChainHash;

            while (true)
            {
                var blockState = await _blockStateSetManger.GetBlockStateSetAsync(blockHash);

                _blockStateSets.Add(blockState);

                var blockHeader = await _blockchainService.GetBlockHeaderByHashAsync(blockHash);

                blockHash = blockHeader.PreviousBlockHash;
                if (blockHash == _chain.LastIrreversibleBlockHash)
                {
                    break;
                }
            }

            await _blockchainStateService.MergeBlockStateAsync(_chain.BestChainHeight, _chain.BestChainHash);

            for (var i = 0; i < BlockCount; i++)
            {
                var transactions = await _osTestHelper.GenerateTransferTransactions(TransactionCount);

                await _osTestHelper.BroadcastTransactions(transactions);

                var block = await _osTestHelper.MinedOneBlock();

                _blocks.Add(block);

                var blockState = await _blockStateSetManger.GetBlockStateSetAsync(block.GetHash());

                _blockStateSets.Add(blockState);
            }

            var chain = await _blockchainService.GetChainAsync();

            await _chainManager.SetIrreversibleBlockAsync(chain, chain.BestChainHash);

            _chainStateInfo = await _chainStateInfoCollection.GetAsync(chain.Id.ToStorageKey());
        }
        private async Task <string> SendTransactionAsync(Transaction transaction)
        {
            await _osTestHelper.BroadcastTransactions(new List <Transaction> {
                transaction
            });

            await _osTestHelper.MinedOneBlock();

            return(transaction.GetHash().ToHex());
        }
        public void Setup(BenchmarkContext context)
        {
            _blockchainStateService = GetRequiredService <IBlockchainStateService>();
            _blockchainService      = GetRequiredService <IBlockchainService>();
            _osTestHelper           = GetRequiredService <OSTestHelper>();
            _chainManager           = GetRequiredService <IChainManager>();

            _counter = context.GetCounter("TestCounter");

            AsyncHelper.RunSync(async() =>
            {
                var transactions = await _osTestHelper.GenerateTransferTransactions(1000);
                await _osTestHelper.BroadcastTransactions(transactions);
                await _osTestHelper.MinedOneBlock();

                var chain = await _blockchainService.GetChainAsync();
                await _chainManager.SetIrreversibleBlockAsync(chain, chain.BestChainHash);
            });
        }
        public void Setup(BenchmarkContext context)
        {
            _osTestHelper      = GetRequiredService <OSTestHelper>();
            _txHub             = GetRequiredService <ITxHub>();
            _blockchainService = GetRequiredService <IBlockchainService>();

            _counter = context.GetCounter("TestCounter");

            AsyncHelper.RunSync(async() =>
            {
                var transactions = await _osTestHelper.GenerateTransferTransactions(1000);

                await _txHub.HandleTransactionsReceivedAsync(new TransactionsReceivedEvent
                {
                    Transactions = transactions
                });

                await _osTestHelper.MinedOneBlock();
            });
        }
        public async Task UpdateSyncState_Test()
        {
            _peerPool.TryAddPeer(CreatePeer(15));
            _peerPool.TryAddPeer(CreatePeer(16));

            InitialSyncFinishedEvent eventData = null;

            _eventBus.Subscribe <InitialSyncFinishedEvent>(args =>
            {
                eventData = args;
                return(Task.CompletedTask);
            });

            await _syncStateService.StartSyncAsync();

            _syncStateService.SyncState.ShouldBe(SyncState.Syncing);
            _syncStateService.GetCurrentSyncTarget().ShouldBe(15);

            await _syncStateService.UpdateSyncStateAsync();

            _syncStateService.SyncState.ShouldBe(SyncState.Syncing);
            _syncStateService.GetCurrentSyncTarget().ShouldBe(15);

            for (var i = 0; i < 4; i++)
            {
                await _osTestHelper.MinedOneBlock();
            }
            var chain = await _blockchainService.GetChainAsync();

            await _blockchainService.SetIrreversibleBlockAsync(chain, _osTestHelper.BestBranchBlockList.Last().Height,
                                                               _osTestHelper.BestBranchBlockList.Last().GetHash());

            await _syncStateService.UpdateSyncStateAsync();

            _syncStateService.SyncState.ShouldBe(SyncState.Finished);
            _syncStateService.GetCurrentSyncTarget().ShouldBe(-1);

            eventData.ShouldNotBeNull();
        }
        public async Task ProcessDownloadJob_CannotGetBlocks()
        {
            await _blockDownloadJobManager.EnqueueAsync(HashHelper.ComputeFrom("PeerBlock"), 30,
                                                        _blockSyncOptions.MaxBatchRequestBlockCount,
                                                        null);

            var block = await _osTestHelper.MinedOneBlock();

            var chain = await _blockchainService.GetChainAsync();

            await _blockchainService.SetIrreversibleBlockAsync(chain, block.Height, block.GetHash());

            await _blockDownloadWorker.ProcessDownloadJobAsync();

            chain = await _blockchainService.GetChainAsync();

            chain.BestChainHeight.ShouldBe(block.Height);

            var jobInfo = await _blockDownloadJobStore.GetFirstWaitingJobAsync();

            jobInfo.ShouldBeNull();
        }
        public async Task SyncByAnnounce_Success()
        {
            var chain = await _blockchainService.GetChainAsync();

            var resp = await _networkService.GetBlocksAsync(chain.BestChainHash, 30, null);

            var peerBlocks = resp.Payload;

            var block           = peerBlocks[0];
            var peerBlockHash   = block.GetHash();
            var peerBlockHeight = block.Height;
            {
                // Sync one block to best chain
                // BestChainHeight: 12
                await _blockSyncService.SyncByAnnouncementAsync(chain, new SyncAnnouncementDto
                {
                    SyncBlockHash          = peerBlockHash,
                    SyncBlockHeight        = peerBlockHeight,
                    BatchRequestBlockCount = 5
                });

                chain = await _blockchainService.GetChainAsync();

                chain.BestChainHeight.ShouldBe(12);
                chain.BestChainHash.ShouldBe(peerBlocks[0].GetHash());
            }

            {
                // Handle the same announcement again
                // BestChainHeight: 12
                await _blockSyncService.SyncByAnnouncementAsync(chain, new SyncAnnouncementDto
                {
                    SyncBlockHash          = peerBlockHash,
                    SyncBlockHeight        = peerBlockHeight,
                    BatchRequestBlockCount = 5
                });

                chain = await _blockchainService.GetChainAsync();

                chain.BestChainHash.ShouldBe(peerBlocks[0].GetHash());
                chain.BestChainHeight.ShouldBe(12);
            }

            {
                // Mined one block, and fork
                await _osTestHelper.MinedOneBlock();

                chain = await _blockchainService.GetChainAsync();

                chain.BestChainHeight.ShouldBe(13);
            }

            {
                // Receive a higher fork block, sync from the lib
                // BestChainHeight: 31
                block           = peerBlocks.Last();
                peerBlockHash   = block.GetHash();
                peerBlockHeight = block.Height;
                await _blockSyncService.SyncByAnnouncementAsync(chain, new SyncAnnouncementDto
                {
                    SyncBlockHash          = peerBlockHash,
                    SyncBlockHeight        = peerBlockHeight,
                    BatchRequestBlockCount = 5
                });

                var jobInfo = await _blockDownloadJobStore.GetFirstWaitingJobAsync();

                jobInfo.TargetBlockHeight.ShouldBe(peerBlockHeight);
                jobInfo.TargetBlockHash.ShouldBe(peerBlockHash);
            }
        }