private async Task ProcessNewBlockAsync(BlockAnnouncement blockAnnouncement, string senderPubkey) { var chain = await _blockchainService.GetChainAsync(); if (!await _blockSyncValidationService.ValidateAnnouncementBeforeSyncAsync(chain, blockAnnouncement, senderPubkey)) { return; } await _blockSyncService.SyncByAnnouncementAsync(chain, new SyncAnnouncementDto { SyncBlockHash = blockAnnouncement.BlockHash, SyncBlockHeight = blockAnnouncement.BlockHeight, SuggestedPeerPubkey = senderPubkey, BatchRequestBlockCount = _blockSyncOptions.MaxBatchRequestBlockCount }); }
private async Task ProcessNewBlockAsync(BlockAnnouncement blockAnnouncement, string senderPubkey) { Logger.LogDebug( $"Start block sync job, target height: {blockAnnouncement.BlockHeight}, target block hash: {blockAnnouncement.BlockHash}, peer: {senderPubkey}"); var chain = await _blockchainService.GetChainAsync(); if (!await _blockSyncValidationService.ValidateAnnouncementBeforeSyncAsync(chain, blockAnnouncement, senderPubkey)) { return; } await _blockSyncService.SyncByAnnouncementAsync(chain, new SyncAnnouncementDto { SyncBlockHash = blockAnnouncement.BlockHash, SyncBlockHeight = blockAnnouncement.BlockHeight, SuggestedPeerPubkey = senderPubkey, BatchRequestBlockCount = _blockSyncOptions.MaxBatchRequestBlockCount }); }
public async Task SyncByAnnounce_Success() { var chain = await _blockchainService.GetChainAsync(); var resp = await _networkService.GetBlocksAsync(chain.BestChainHash, 30, null); var peerBlocks = resp.Payload; var block = peerBlocks[0]; var peerBlockHash = block.GetHash(); var peerBlockHeight = block.Height; { // Sync one block to best chain // BestChainHeight: 12 await _blockSyncService.SyncByAnnouncementAsync(chain, new SyncAnnouncementDto { SyncBlockHash = peerBlockHash, SyncBlockHeight = peerBlockHeight, BatchRequestBlockCount = 5 }); chain = await _blockchainService.GetChainAsync(); chain.BestChainHeight.ShouldBe(12); chain.BestChainHash.ShouldBe(peerBlocks[0].GetHash()); } { // Handle the same announcement again // BestChainHeight: 12 await _blockSyncService.SyncByAnnouncementAsync(chain, new SyncAnnouncementDto { SyncBlockHash = peerBlockHash, SyncBlockHeight = peerBlockHeight, BatchRequestBlockCount = 5 }); chain = await _blockchainService.GetChainAsync(); chain.BestChainHash.ShouldBe(peerBlocks[0].GetHash()); chain.BestChainHeight.ShouldBe(12); } { // Mined one block, and fork await _osTestHelper.MinedOneBlock(); chain = await _blockchainService.GetChainAsync(); chain.BestChainHeight.ShouldBe(13); } { // Receive a higher fork block, sync from the lib // BestChainHeight: 31 block = peerBlocks.Last(); peerBlockHash = block.GetHash(); peerBlockHeight = block.Height; await _blockSyncService.SyncByAnnouncementAsync(chain, new SyncAnnouncementDto { SyncBlockHash = peerBlockHash, SyncBlockHeight = peerBlockHeight, BatchRequestBlockCount = 5 }); var jobInfo = await _blockDownloadJobStore.GetFirstWaitingJobAsync(); jobInfo.TargetBlockHeight.ShouldBe(peerBlockHeight); jobInfo.TargetBlockHash.ShouldBe(peerBlockHash); } }