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 async Task FetchBlock_Success() { var peerBlock = await _networkService.GetBlockByHashAsync(Hash.FromString("PeerBlock")); var block = await _blockchainService.GetBlockByHashAsync(peerBlock.GetHash()); block.ShouldBeNull(); var fetchResult = await _blockFetchService.FetchBlockAsync(peerBlock.GetHash(), peerBlock.Height, null); fetchResult.ShouldBeTrue(); block = await _blockchainService.GetBlockByHashAsync(peerBlock.GetHash()); block.GetHash().ShouldBe(peerBlock.GetHash()); }
public async Task FetchBlock_Success() { var response = await _networkService.GetBlockByHashAsync(HashHelper.ComputeFrom("PeerBlock"), null); var peerBlock = response.Payload; var block = await _blockchainService.GetBlockByHashAsync(peerBlock.GetHash()); block.ShouldBeNull(); var fetchResult = await _blockFetchService.FetchBlockAsync(peerBlock.GetHash(), peerBlock.Height, null); fetchResult.ShouldBeTrue(); block = await _blockchainService.GetBlockByHashAsync(peerBlock.GetHash()); block.GetHash().ShouldBe(peerBlock.GetHash()); }
public async Task FetchBlock_ReturnInvalidBlock() { var abnormalPeerPubkey = "AbnormalPeerPubkey"; var abnormalPeer = _networkService.GetPeerByPubkey(abnormalPeerPubkey); abnormalPeer.ShouldNotBeNull(); var result = await _blockFetchService.FetchBlockAsync(Hash.FromString("AnnounceBlockHash"), 100, abnormalPeerPubkey); result.ShouldBeFalse(); abnormalPeer = _networkService.GetPeerByPubkey(abnormalPeerPubkey); abnormalPeer.ShouldBeNull(); }