Exemplo n.º 1
0
        private void EnqueueFetchBlockJob(SyncAnnouncementDto syncAnnouncementDto, int retryTimes)
        {
            _blockSyncQueueService.Enqueue(async() =>
            {
                Logger.LogTrace(
                    $"Block sync: Fetch block, block height: {syncAnnouncementDto.SyncBlockHeight}, block hash: {syncAnnouncementDto.SyncBlockHash}.");

                var fetchResult = false;
                if (ValidateQueueAvailability())
                {
                    fetchResult = await _blockFetchService.FetchBlockAsync(syncAnnouncementDto.SyncBlockHash,
                                                                           syncAnnouncementDto.SyncBlockHeight, syncAnnouncementDto.SuggestedPeerPubkey);
                }

                if (fetchResult)
                {
                    return;
                }
                if (retryTimes > 1)
                {
                    EnqueueFetchBlockJob(syncAnnouncementDto, retryTimes - 1);
                }
                else if (_announcementCacheProvider.TryGetAnnouncementNextSender(syncAnnouncementDto.SyncBlockHash, out var senderPubKey))
                {
                    syncAnnouncementDto.SuggestedPeerPubkey = senderPubKey;
                    EnqueueFetchBlockJob(syncAnnouncementDto, BlockSyncConstants.FetchBlockRetryTimes);
                }
            }, OSConstants.BlockFetchQueueName);
        }
        public void AnnouncementCache_Test()
        {
            var blockHash   = HashHelper.ComputeFrom("BlockHash");
            var blockHeight = 0;
            var pubkey      = "Pubkey";
            var addResult   = _announcementCacheProvider.TryAddOrUpdateAnnouncementCache(blockHash, blockHeight, pubkey);

            addResult.ShouldBeTrue();

            var getResult = _announcementCacheProvider.TryGetAnnouncementNextSender(blockHash, out var senderPubKey);

            getResult.ShouldBeTrue();
            senderPubKey.ShouldBe(pubkey);

            getResult = _announcementCacheProvider.TryGetAnnouncementNextSender(blockHash, out senderPubKey);
            getResult.ShouldBeFalse();
            senderPubKey.ShouldBeNull();

            for (var i = 0; i < 101; i++)
            {
                var hash = HashHelper.ComputeFrom("BlockHash" + i);
                addResult = _announcementCacheProvider.TryAddOrUpdateAnnouncementCache(hash, blockHeight, pubkey);
                addResult.ShouldBeTrue();
            }

            getResult = _announcementCacheProvider.TryGetAnnouncementNextSender(HashHelper.ComputeFrom("BlockHash" + 0), out senderPubKey);
            getResult.ShouldBeFalse();
            senderPubKey.ShouldBeNull();
        }
        public async Task SyncByAnnounce_RetryByNextSender()
        {
            _blockSyncStateProvider.SetEnqueueTime(OSConstants.BlockSyncAttachQueueName,
                                                   TimestampHelper.GetUtcNow().AddMilliseconds(-(BlockSyncConstants.BlockSyncAttachBlockAgeLimit + 100)));

            var response = await _networkService.GetBlockByHashAsync(HashHelper.ComputeFrom("PeerBlock"), null);

            var peerBlock = response.Payload;

            _announcementCacheProvider.TryAddOrUpdateAnnouncementCache(peerBlock.GetHash(), peerBlock.Height,
                                                                       "NextPeerPubkey");

            var chain = await _blockchainService.GetChainAsync();

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

            _announcementCacheProvider.TryGetAnnouncementNextSender(peerBlock.GetHash(), out var nextPeerPubkey);
            nextPeerPubkey.ShouldBeNull();
        }