public async Task SyncByAnnounce_FetchQueueIsBusy()
        {
            var response = await _networkService.GetBlockByHashAsync(Hash.FromString("PeerBlock"), null);

            var peerBlock = response.Payload;

            var block = await _blockchainService.GetBlockByHashAsync(peerBlock.GetHash());

            block.ShouldBeNull();

            var chain = await _blockchainService.GetChainAsync();

            var bestChainHash   = chain.BestChainHash;
            var bestChainHeight = chain.BestChainHeight;

            _blockSyncStateProvider.SetEnqueueTime(OSConstants.BlockFetchQueueName, TimestampHelper.GetUtcNow()
                                                   .AddMilliseconds(-(BlockSyncConstants.BlockSyncFetchBlockAgeLimit + 100)));

            await _blockSyncService.SyncByAnnouncementAsync(chain, new SyncAnnouncementDto
            {
                SyncBlockHash          = peerBlock.GetHash(),
                SyncBlockHeight        = peerBlock.Height,
                BatchRequestBlockCount = 5
            });

            block = await _blockchainService.GetBlockByHashAsync(peerBlock.GetHash());

            block.ShouldBeNull();

            chain = await _blockchainService.GetChainAsync();

            chain.BestChainHash.ShouldBe(bestChainHash);
            chain.BestChainHeight.ShouldBe(bestChainHeight);
        }
Beispiel #2
0
        public async Task ProcessDownloadJob_ValidateFailed()
        {
            var chain = await _blockchainService.GetChainAsync();

            var response = await _networkService.GetBlockByHashAsync(Hash.FromString("PeerBlock"), null);

            var peerBlock       = response.Payload;
            var bestChainHash   = chain.BestChainHash;
            var bestChainHeight = chain.BestChainHeight;

            // no job
            await _blockDownloadWorker.ProcessDownloadJobAsync();

            chain = await _blockchainService.GetChainAsync();

            chain.BestChainHash.ShouldBe(bestChainHash);
            chain.BestChainHeight.ShouldBe(bestChainHeight);

            await _blockDownloadJobManager.EnqueueAsync(peerBlock.GetHash(), peerBlock.Height,
                                                        _blockSyncOptions.MaxBatchRequestBlockCount, null);

            // attach queue is too busy
            _blockSyncStateProvider.SetEnqueueTime(OSConstants.BlockSyncAttachQueueName,
                                                   TimestampHelper.GetUtcNow().AddMilliseconds(-(BlockSyncConstants.BlockSyncAttachBlockAgeLimit + 100)));
            await _blockDownloadWorker.ProcessDownloadJobAsync();

            chain = await _blockchainService.GetChainAsync();

            chain.BestChainHash.ShouldBe(bestChainHash);
            chain.BestChainHeight.ShouldBe(bestChainHeight);

            _blockSyncStateProvider.SetEnqueueTime(OSConstants.BlockSyncAttachQueueName, null);

            // update queue is too busy
            _blockSyncStateProvider.SetEnqueueTime(KernelConstants.UpdateChainQueueName, TimestampHelper.GetUtcNow()
                                                   .AddMilliseconds(-(BlockSyncConstants.BlockSyncAttachAndExecuteBlockAgeLimit + 100)));
            await _blockDownloadWorker.ProcessDownloadJobAsync();

            chain = await _blockchainService.GetChainAsync();

            chain.BestChainHash.ShouldBe(bestChainHash);
            chain.BestChainHeight.ShouldBe(bestChainHeight);

            // not reached the download target and less then deadline
            var jobInfo = await _blockDownloadJobStore.GetFirstWaitingJobAsync();

            jobInfo.CurrentTargetBlockHash   = jobInfo.TargetBlockHash;
            jobInfo.CurrentTargetBlockHeight = jobInfo.TargetBlockHeight;
            jobInfo.Deadline = TimestampHelper.GetUtcNow().AddSeconds(4);
            _blockSyncStateProvider.SetDownloadJobTargetState(jobInfo.TargetBlockHash, false);

            await _blockDownloadWorker.ProcessDownloadJobAsync();

            chain = await _blockchainService.GetChainAsync();

            chain.BestChainHash.ShouldBe(bestChainHash);
            chain.BestChainHeight.ShouldBe(bestChainHeight);
        }
Beispiel #3
0
        public void ValidateQueueAvailabilityBeforeDownload_Test()
        {
            _blockDownloadService.ValidateQueueAvailabilityBeforeDownload().ShouldBeTrue();

            _blockSyncStateProvider.SetEnqueueTime(OSConstants.BlockSyncAttachQueueName,
                                                   TimestampHelper.GetUtcNow()
                                                   .AddMilliseconds(-BlockSyncConstants.BlockSyncAttachBlockAgeLimit - 1000));
            _blockDownloadService.ValidateQueueAvailabilityBeforeDownload().ShouldBeFalse();

            _blockSyncStateProvider.SetEnqueueTime(KernelConstants.UpdateChainQueueName,
                                                   TimestampHelper.GetUtcNow()
                                                   .AddMilliseconds(-BlockSyncConstants.BlockSyncAttachAndExecuteBlockAgeLimit - 1000));
            _blockDownloadService.ValidateQueueAvailabilityBeforeDownload().ShouldBeFalse();
        }
Beispiel #4
0
        public void Enqueue(Func <Task> task, string queueName)
        {
            var enqueueTime = TimestampHelper.GetUtcNow();

            _taskQueueManager.Enqueue(async() =>
            {
                try
                {
                    Logger.LogTrace($"Execute block sync job: {queueName}, enqueue time: {enqueueTime}");

                    _blockSyncStateProvider.SetEnqueueTime(queueName, enqueueTime);
                    await task();
                }
                finally
                {
                    _blockSyncStateProvider.SetEnqueueTime(queueName, null);
                }
            }, queueName);
        }
        public void EnqueueTime_Test()
        {
            var testQueueName   = "TestQueue";
            var testEnqueueTime = TimestampHelper.GetUtcNow();

            var enqueueTime = _blockSyncStateProvider.GetEnqueueTime(testQueueName);

            enqueueTime.ShouldBeNull();

            _blockSyncStateProvider.SetEnqueueTime(testQueueName, testEnqueueTime);

            enqueueTime = _blockSyncStateProvider.GetEnqueueTime(testQueueName);
            enqueueTime.ShouldBe(testEnqueueTime);
        }
        public void ValidateQueueAvailability_Test()
        {
            {
                var result = _blockSyncQueueService.ValidateQueueAvailability(OSConstants.BlockFetchQueueName);
                result.ShouldBeTrue();

                _blockSyncStateProvider.SetEnqueueTime(OSConstants.BlockFetchQueueName,
                                                       TimestampHelper.GetUtcNow());
                result = _blockSyncQueueService.ValidateQueueAvailability(OSConstants.BlockFetchQueueName);
                result.ShouldBeTrue();

                _blockSyncStateProvider.SetEnqueueTime(OSConstants.BlockFetchQueueName,
                                                       TimestampHelper.GetUtcNow()
                                                       .AddMilliseconds(-BlockSyncConstants.BlockSyncFetchBlockAgeLimit - 1000));
                result = _blockSyncQueueService.ValidateQueueAvailability(OSConstants.BlockFetchQueueName);
                result.ShouldBeFalse();
            }

            {
                var result = _blockSyncQueueService.ValidateQueueAvailability(OSConstants.BlockSyncAttachQueueName);
                result.ShouldBeTrue();

                _blockSyncStateProvider.SetEnqueueTime(OSConstants.BlockSyncAttachQueueName,
                                                       TimestampHelper.GetUtcNow());
                result = _blockSyncQueueService.ValidateQueueAvailability(OSConstants.BlockSyncAttachQueueName);
                result.ShouldBeTrue();

                _blockSyncStateProvider.SetEnqueueTime(OSConstants.BlockSyncAttachQueueName,
                                                       TimestampHelper.GetUtcNow()
                                                       .AddMilliseconds(-BlockSyncConstants.BlockSyncAttachBlockAgeLimit - 1000));
                result = _blockSyncQueueService.ValidateQueueAvailability(OSConstants.BlockSyncAttachQueueName);
                result.ShouldBeFalse();
            }

            {
                var result = _blockSyncQueueService.ValidateQueueAvailability(KernelConstants.UpdateChainQueueName);
                result.ShouldBeTrue();

                _blockSyncStateProvider.SetEnqueueTime(KernelConstants.UpdateChainQueueName,
                                                       TimestampHelper.GetUtcNow());
                result = _blockSyncQueueService.ValidateQueueAvailability(KernelConstants.UpdateChainQueueName);
                result.ShouldBeTrue();

                _blockSyncStateProvider.SetEnqueueTime(KernelConstants.UpdateChainQueueName,
                                                       TimestampHelper.GetUtcNow()
                                                       .AddMilliseconds(-BlockSyncConstants.BlockSyncAttachAndExecuteBlockAgeLimit - 1000));
                result = _blockSyncQueueService.ValidateQueueAvailability(KernelConstants.UpdateChainQueueName);
                result.ShouldBeFalse();
            }

            Assert.Throws <InvalidOperationException>(() =>
                                                      _blockSyncQueueService.ValidateQueueAvailability("InvalidQueueName"));
        }