public void DownloadJobTargetState_Test()
        {
            var targetHash = HashHelper.ComputeFrom("TargetHash");
            var result     = _blockSyncStateProvider.TryUpdateDownloadJobTargetState(targetHash, false);

            result.ShouldBeFalse();

            _blockSyncStateProvider.SetDownloadJobTargetState(targetHash, false);
            result = _blockSyncStateProvider.TryGetDownloadJobTargetState(targetHash, out var value);
            result.ShouldBeTrue();
            value.ShouldBeFalse();

            result = _blockSyncStateProvider.TryUpdateDownloadJobTargetState(targetHash, false);
            result.ShouldBeFalse();

            result = _blockSyncStateProvider.TryUpdateDownloadJobTargetState(targetHash, true);
            result.ShouldBeTrue();

            _blockSyncStateProvider.TryRemoveDownloadJobTargetState(targetHash);
            result = _blockSyncStateProvider.TryGetDownloadJobTargetState(targetHash, out value);
            result.ShouldBeFalse();
        }
Exemple #2
0
 private void EnqueueAttachBlockJob(BlockWithTransactions blockWithTransactions, string senderPubkey)
 {
     _blockSyncQueueService.Enqueue(
         async() =>
     {
         await _blockSyncAttachService.AttachBlockWithTransactionsAsync(blockWithTransactions, senderPubkey,
                                                                        () =>
         {
             _blockSyncStateProvider.TryUpdateDownloadJobTargetState(
                 blockWithTransactions.GetHash(), true);
             return(Task.CompletedTask);
         });
     },
         OSConstants.BlockSyncAttachQueueName);
 }
Exemple #3
0
        public async Task <DownloadBlocksResult> DownloadBlocksAsync(DownloadBlockDto downloadBlockDto)
        {
            var downloadBlockCount      = 0;
            var lastDownloadBlockHash   = downloadBlockDto.PreviousBlockHash;
            var lastDownloadBlockHeight = downloadBlockDto.PreviousBlockHeight;

            while (downloadBlockCount < downloadBlockDto.MaxBlockDownloadCount)
            {
                Logger.LogDebug(
                    $"Request blocks start with block hash: {lastDownloadBlockHash}, block height: {lastDownloadBlockHeight}");

                var blocksWithTransactions = await _networkService.GetBlocksAsync(lastDownloadBlockHash,
                                                                                  downloadBlockDto.BatchRequestBlockCount, downloadBlockDto.SuggestedPeerPubkey);

                if (blocksWithTransactions == null || !blocksWithTransactions.Any())
                {
                    Logger.LogWarning("No blocks returned.");
                    break;
                }

                if (blocksWithTransactions.First().Header.PreviousBlockHash != lastDownloadBlockHash)
                {
                    throw new InvalidOperationException(
                              $"Previous block not match previous {lastDownloadBlockHash}, network back {blocksWithTransactions.First().Header.PreviousBlockHash}");
                }

                foreach (var blockWithTransactions in blocksWithTransactions)
                {
                    Logger.LogDebug($"Processing block {blockWithTransactions}.");

                    _blockSyncQueueService.Enqueue(
                        async() =>
                    {
                        await _blockSyncAttachService.AttachBlockWithTransactionsAsync(blockWithTransactions,
                                                                                       async() =>
                        {
                            _blockSyncStateProvider.TryUpdateDownloadJobTargetState(blockWithTransactions.GetHash(), true);
                        });
                    },
                        OSConstants.BlockSyncAttachQueueName);

                    downloadBlockCount++;
                }

                var lastBlock = blocksWithTransactions.Last();
                lastDownloadBlockHash   = lastBlock.GetHash();
                lastDownloadBlockHeight = lastBlock.Height;
            }

            if (lastDownloadBlockHash != null)
            {
                _blockSyncStateProvider.SetDownloadJobTargetState(lastDownloadBlockHash, false);
            }

            return(new DownloadBlocksResult
            {
                DownloadBlockCount = downloadBlockCount,
                LastDownloadBlockHash = lastDownloadBlockHash,
                LastDownloadBlockHeight = lastDownloadBlockHeight
            });
        }