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

Commit e0730e2

Browse files
committed
Fixed bug with infinite loop in blockRequests in the end of sync with 1 peer
1 parent 6162302 commit e0730e2

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

ethereumj-core/src/main/java/org/ethereum/sync/BlockDownloader.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.common.util.concurrent.FutureCallback;
2121
import com.google.common.util.concurrent.Futures;
2222
import com.google.common.util.concurrent.ListenableFuture;
23+
import com.google.common.util.concurrent.MoreExecutors;
2324
import org.apache.commons.collections4.queue.CircularFifoQueue;
2425
import org.ethereum.core.*;
2526
import org.ethereum.net.server.Channel;
@@ -196,7 +197,7 @@ public void onFailure(Throwable t) {
196197
logger.debug("{}: Error receiving headers. Dropping the peer.", name, t);
197198
any.getEthHandler().dropConnection();
198199
}
199-
});
200+
}, MoreExecutors.directExecutor());
200201
it.remove();
201202
reqHeadersCounter++;
202203
}
@@ -252,6 +253,7 @@ public void onFailure(Throwable t) {
252253
if (blocksToAsk >= MAX_IN_REQUEST) {
253254
// SyncQueueIfc.BlocksRequest bReq = syncQueue.requestBlocks(maxBlocks);
254255

256+
boolean fewHeadersReqMode = false;
255257
if (bReqs.size() == 1 && bReqs.get(0).getBlockHeaders().size() <= 3) {
256258
// new blocks are better to request from the header senders first
257259
// to get more chances to receive block body promptly
@@ -261,7 +263,9 @@ public void onFailure(Throwable t) {
261263
ListenableFuture<List<Block>> futureBlocks =
262264
channel.getEthHandler().sendGetBlockBodies(singletonList(blockHeaderWrapper));
263265
if (futureBlocks != null) {
264-
Futures.addCallback(futureBlocks, new BlocksCallback(channel));
266+
Futures.addCallback(futureBlocks, new BlocksCallback(channel),
267+
MoreExecutors.directExecutor());
268+
fewHeadersReqMode = true;
265269
}
266270
}
267271
}
@@ -285,12 +289,21 @@ public void onFailure(Throwable t) {
285289
any.getEthHandler().sendGetBlockBodies(blocksRequest.getBlockHeaders());
286290
blocksRequested += blocksRequest.getBlockHeaders().size();
287291
if (futureBlocks != null) {
288-
Futures.addCallback(futureBlocks, new BlocksCallback(any));
292+
Futures.addCallback(futureBlocks, new BlocksCallback(any),
293+
MoreExecutors.directExecutor());
289294
reqBlocksCounter++;
290295
it.remove();
291296
}
292297
}
293298
}
299+
300+
// Case when we have requested few headers and was not able
301+
// to remove request from the list in above cycle because
302+
// there were no idle peers or whatever
303+
if (fewHeadersReqMode && !bReqs.isEmpty()) {
304+
bReqs.clear();
305+
}
306+
294307
receivedBlocksLatch = new CountDownLatch(max(reqBlocksCounter - 2, 1));
295308
receivedBlocksLatch.await(1000, TimeUnit.MILLISECONDS);
296309
} else {

0 commit comments

Comments
 (0)