diff --git a/src/main/scala/io/iohk/ethereum/blockchain/sync/regular/BlockFetcherState.scala b/src/main/scala/io/iohk/ethereum/blockchain/sync/regular/BlockFetcherState.scala index dcc05aa872..b4994b6abe 100644 --- a/src/main/scala/io/iohk/ethereum/blockchain/sync/regular/BlockFetcherState.scala +++ b/src/main/scala/io/iohk/ethereum/blockchain/sync/regular/BlockFetcherState.scala @@ -100,8 +100,12 @@ case class BlockFetcherState( .withKnownTopAt(block.number) Right(newState) } else if (isExistInWaitingHeaders(block.header.parentHash)) { + // ignore already requested bodies + val newFetchingBodiesState = + if (fetchingBodiesState == AwaitingBodies) AwaitingBodiesToBeIgnored else fetchingBodiesState val newState = copy( - waitingHeaders = waitingHeaders.takeWhile(_.number < block.number).enqueue(block.header) + waitingHeaders = waitingHeaders.takeWhile(_.number < block.number).enqueue(block.header), + fetchingBodiesState = newFetchingBodiesState ) .withKnownTopAt(block.number) Right(newState) diff --git a/src/test/scala/io/iohk/ethereum/blockchain/sync/regular/BlockFetcherSpec.scala b/src/test/scala/io/iohk/ethereum/blockchain/sync/regular/BlockFetcherSpec.scala index 96aeefcb49..f6765bd870 100644 --- a/src/test/scala/io/iohk/ethereum/blockchain/sync/regular/BlockFetcherSpec.scala +++ b/src/test/scala/io/iohk/ethereum/blockchain/sync/regular/BlockFetcherSpec.scala @@ -343,20 +343,28 @@ class BlockFetcherSpec handleFirstBlockBatchBodies() + // second bodies request + val secondGetBlockBodiesRequest = GetBlockBodies(secondBlocksBatch.map(_.hash)) + peersClient.expectMsgPF() { case PeersClient.Request(msg, _, _) if msg == secondGetBlockBodiesRequest => ()} + // send old checkpoint block blockFetcher ! MessageFromPeer( PV64.NewBlock(checkpointBlock, ChainWeight(checkpointBlock.number, checkpointBlock.header.difficulty)), fakePeer.id ) - // Second bodies request - val secondGetBlockBodiesRequest = GetBlockBodies(alternativeSecondBlocksBatch.map(_.hash)) - peersClient.expectMsgPF() { case PeersClient.Request(msg, _, _) if msg == secondGetBlockBodiesRequest => () } - - // Second bodies response - val secondGetBlockBodiesResponse = BlockBodies(alternativeSecondBlocksBatch.map(_.body)) + // second bodies response + val secondGetBlockBodiesResponse = BlockBodies(secondBlocksBatch.map(_.body)) peersClient.reply(PeersClient.Response(fakePeer, secondGetBlockBodiesResponse)) + // third bodies request after adding checkpoint into the waiting headers queue + val thirdGetBlockBodiesRequest = GetBlockBodies(alternativeSecondBlocksBatch.map(_.hash)) + peersClient.expectMsgPF() { case PeersClient.Request(msg, _, _) if msg == thirdGetBlockBodiesRequest => ()} + + // third bodies response + val thirdGetBlockBodiesResponse = BlockBodies(alternativeSecondBlocksBatch.map(_.body)) + peersClient.reply(PeersClient.Response(fakePeer, thirdGetBlockBodiesResponse)) + // We need to wait a while in order to allow fetcher to process all the blocks system.scheduler.scheduleOnce(Timeouts.shortTimeout) { importer.send(blockFetcher, PickBlocks(syncConfig.blocksBatchSize * 2))