Esempio n. 1
0
        public async Task ProcessDownloadJob_Success()
        {
            var response = await _networkService.GetBlockByHashAsync(Hash.FromString("PeerBlock"), null);

            var peerBlock = response.Payload;

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

            await _blockDownloadWorker.ProcessDownloadJobAsync();

            var chain = await _blockchainService.GetChainAsync();

            chain.BestChainHeight.ShouldBe(31);

            var jobInfo = await _blockDownloadJobStore.GetFirstWaitingJobAsync();

            jobInfo.JobId.ShouldBe(peerBlock.GetHash());
            _blockSyncStateProvider.TryGetDownloadJobTargetState(chain.BestChainHash, out var state).ShouldBeTrue();
            state.ShouldBeFalse();

            _blockSyncStateProvider.SetDownloadJobTargetState(chain.BestChainHash, true);

            await _blockDownloadWorker.ProcessDownloadJobAsync();

            jobInfo = await _blockDownloadJobStore.GetFirstWaitingJobAsync();

            jobInfo.ShouldBeNull();
            _blockSyncStateProvider.TryGetDownloadJobTargetState(chain.BestChainHash, out state).ShouldBeFalse();
        }
Esempio n. 2
0
        public async Task Enqueue_Test()
        {
            var syncBlockHash          = HashHelper.ComputeFrom("SyncBlockHash");
            var syncBlockHeight        = 100;
            var batchRequestBlockCount = 10;
            var suggestedPeerPubkey    = "SuggestedPeerPubkey";

            var jobId = await _blockDownloadJobManager.EnqueueAsync(syncBlockHash, syncBlockHeight,
                                                                    batchRequestBlockCount, suggestedPeerPubkey);

            jobId.ShouldNotBeNull();

            var jobInfo = await _blockDownloadJobStore.GetFirstWaitingJobAsync();

            jobInfo.TargetBlockHash.ShouldBe(syncBlockHash);
            jobInfo.TargetBlockHeight.ShouldBe(syncBlockHeight);
            jobInfo.BatchRequestBlockCount.ShouldBe(batchRequestBlockCount);
            jobInfo.SuggestedPeerPubkey.ShouldBe(suggestedPeerPubkey);

            for (int i = 0; i < 99; i++)
            {
                syncBlockHash = HashHelper.ComputeFrom("SyncBlockHash" + i);
                jobId         = await _blockDownloadJobManager.EnqueueAsync(syncBlockHash, syncBlockHeight,
                                                                            batchRequestBlockCount, suggestedPeerPubkey);

                jobId.ShouldNotBeNull();
            }

            syncBlockHash = HashHelper.ComputeFrom("SyncBlockHash100");
            jobId         = await _blockDownloadJobManager.EnqueueAsync(syncBlockHash, syncBlockHeight,
                                                                        batchRequestBlockCount, suggestedPeerPubkey);

            jobId.ShouldBeNull();
        }
Esempio n. 3
0
        public async Task ProcessDownloadJob_Success()
        {
            var chain = await _blockchainService.GetChainAsync();

            var originalBestChainHash   = chain.BestChainHash;
            var originalBestChainHeight = chain.BestChainHeight;
            var response = await _networkService.GetBlocksAsync(chain.LastIrreversibleBlockHash, 30, null);

            var peerBlocks = response.Payload;

            var peerBlock = peerBlocks.Last();

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

            await _blockDownloadWorker.ProcessDownloadJobAsync();

            chain = await _blockchainService.GetChainAsync();

            chain.BestChainHeight.ShouldBe(30);
            chain.BestChainHash.ShouldBe(peerBlock.GetHash());

            var block = await _blockchainService.GetBlockByHeightInBestChainBranchAsync(originalBestChainHeight);

            block.GetHash().ShouldNotBe(originalBestChainHash);
        }
Esempio n. 4
0
        public async Task SyncByAnnouncementAsync(Chain chain, SyncAnnouncementDto syncAnnouncementDto)
        {
            if (syncAnnouncementDto.SyncBlockHash != null && syncAnnouncementDto.SyncBlockHeight <=
                chain.LongestChainHeight + BlockSyncConstants.BlockSyncModeHeightOffset)
            {
                if (!_blockSyncQueueService.ValidateQueueAvailability(OSConstants.BlockFetchQueueName))
                {
                    Logger.LogWarning("Block sync fetch queue is too busy.");
                    return;
                }

                EnqueueFetchBlockJob(syncAnnouncementDto, BlockSyncConstants.FetchBlockRetryTimes);
            }
            else
            {
                await _blockDownloadJobManager.EnqueueAsync(syncAnnouncementDto.SyncBlockHash, syncAnnouncementDto
                                                            .SyncBlockHeight,
                                                            syncAnnouncementDto.BatchRequestBlockCount, syncAnnouncementDto.SuggestedPeerPubkey);
            }
        }
Esempio n. 5
0
        public async Task ProcessDownloadJob_ManyJob()
        {
            var chain = await _blockchainService.GetChainAsync();

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

            // Enqueue download job, from height 25 to 31
            for (int i = 13; i < 19; i++)
            {
                await _blockDownloadJobManager.EnqueueAsync(peerBlocks[i].GetHash(), peerBlocks[i].Height,
                                                            _blockSyncOptions.MaxBatchRequestBlockCount, null);
            }

            {
                // Worker run once
                // Execute job(TargetBlockHeight: 25)
                // BestChainHeight should be 14
                await RunWorkerAsync(1, peerBlocks);

                chain = await _blockchainService.GetChainAsync();

                chain.BestChainHeight.ShouldBe(peerBlocks[2].Height);
                chain.BestChainHash.ShouldBe(peerBlocks[2].GetHash());

                var jobInfo = await _blockDownloadJobStore.GetFirstWaitingJobAsync();

                jobInfo.TargetBlockHeight.ShouldBe(peerBlocks[13].Height);
                jobInfo.TargetBlockHash.ShouldBe(peerBlocks[13].GetHash());
            }

            {
                // Worker run 4 times
                // Execute job(TargetBlockHeight: 25)
                // BestChainHeight should be 26
                await RunWorkerAsync(4, peerBlocks);

                chain = await _blockchainService.GetChainAsync();

                chain.BestChainHeight.ShouldBe(peerBlocks[14].Height);
                chain.BestChainHash.ShouldBe(peerBlocks[14].GetHash());

                var jobInfo = await _blockDownloadJobStore.GetFirstWaitingJobAsync();

                jobInfo.TargetBlockHeight.ShouldBe(peerBlocks[13].Height);
                jobInfo.TargetBlockHash.ShouldBe(peerBlocks[13].GetHash());
            }

            {
                // Worker run once
                // Execute job(TargetBlockHeight: 25): Just drop the job
                // Execute job(TargetBlockHeight: 26): Just drop the job
                // Execute job(TargetBlockHeight: 27)
                // BestChainHeight should be 29
                await RunWorkerAsync(1, peerBlocks);

                chain = await _blockchainService.GetChainAsync();

                chain.BestChainHeight.ShouldBe(peerBlocks[17].Height);
                chain.BestChainHash.ShouldBe(peerBlocks[17].GetHash());

                var jobInfo = await _blockDownloadJobStore.GetFirstWaitingJobAsync();

                jobInfo.TargetBlockHeight.ShouldBe(peerBlocks[15].Height);
                jobInfo.TargetBlockHash.ShouldBe(peerBlocks[15].GetHash());
            }

            {
                // Worker run once
                // Execute job(TargetBlockHeight: 27): Just drop the job
                // Execute job(TargetBlockHeight: 28): Just drop the job
                // Execute job(TargetBlockHeight: 29): Just drop the job
                // Execute job(TargetBlockHeight: 30)
                // BestChainHeight should be 31
                await RunWorkerAsync(1, peerBlocks);

                chain = await _blockchainService.GetChainAsync();

                chain.BestChainHeight.ShouldBe(peerBlocks[19].Height);
                chain.BestChainHash.ShouldBe(peerBlocks[19].GetHash());

                var jobInfo = await _blockDownloadJobStore.GetFirstWaitingJobAsync();

                jobInfo.TargetBlockHeight.ShouldBe(peerBlocks[18].Height);
                jobInfo.TargetBlockHash.ShouldBe(peerBlocks[18].GetHash());
            }

            {
                // Worker run once
                // Execute job(TargetBlockHeight: 30): Just drop the job
                // Execute job(TargetBlockHeight: 31): Just drop the job
                // BestChainHeight should be 31
                await RunWorkerAsync(1, peerBlocks);

                chain = await _blockchainService.GetChainAsync();

                chain.BestChainHeight.ShouldBe(peerBlocks[19].Height);
                chain.BestChainHash.ShouldBe(peerBlocks[19].GetHash());

                var jobInfo = await _blockDownloadJobStore.GetFirstWaitingJobAsync();

                jobInfo.ShouldBeNull();
            }
        }