Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
Fixed bug with infinite loop in blockRequests in the end of sync with…
Browse files Browse the repository at this point in the history
… 1 peer
  • Loading branch information
zilm13 committed Jun 11, 2018
1 parent 6162302 commit e0730e2
Showing 1 changed file with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;
import org.apache.commons.collections4.queue.CircularFifoQueue;
import org.ethereum.core.*;
import org.ethereum.net.server.Channel;
Expand Down Expand Up @@ -196,7 +197,7 @@ public void onFailure(Throwable t) {
logger.debug("{}: Error receiving headers. Dropping the peer.", name, t);
any.getEthHandler().dropConnection();
}
});
}, MoreExecutors.directExecutor());
it.remove();
reqHeadersCounter++;
}
Expand Down Expand Up @@ -252,6 +253,7 @@ public void onFailure(Throwable t) {
if (blocksToAsk >= MAX_IN_REQUEST) {
// SyncQueueIfc.BlocksRequest bReq = syncQueue.requestBlocks(maxBlocks);

boolean fewHeadersReqMode = false;
if (bReqs.size() == 1 && bReqs.get(0).getBlockHeaders().size() <= 3) {
// new blocks are better to request from the header senders first
// to get more chances to receive block body promptly
Expand All @@ -261,7 +263,9 @@ public void onFailure(Throwable t) {
ListenableFuture<List<Block>> futureBlocks =
channel.getEthHandler().sendGetBlockBodies(singletonList(blockHeaderWrapper));
if (futureBlocks != null) {
Futures.addCallback(futureBlocks, new BlocksCallback(channel));
Futures.addCallback(futureBlocks, new BlocksCallback(channel),
MoreExecutors.directExecutor());
fewHeadersReqMode = true;
}
}
}
Expand All @@ -285,12 +289,21 @@ public void onFailure(Throwable t) {
any.getEthHandler().sendGetBlockBodies(blocksRequest.getBlockHeaders());
blocksRequested += blocksRequest.getBlockHeaders().size();
if (futureBlocks != null) {
Futures.addCallback(futureBlocks, new BlocksCallback(any));
Futures.addCallback(futureBlocks, new BlocksCallback(any),
MoreExecutors.directExecutor());
reqBlocksCounter++;
it.remove();
}
}
}

// Case when we have requested few headers and was not able
// to remove request from the list in above cycle because
// there were no idle peers or whatever
if (fewHeadersReqMode && !bReqs.isEmpty()) {
bReqs.clear();
}

receivedBlocksLatch = new CountDownLatch(max(reqBlocksCounter - 2, 1));
receivedBlocksLatch.await(1000, TimeUnit.MILLISECONDS);
} else {
Expand Down

0 comments on commit e0730e2

Please sign in to comment.