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

Commit 144b72b

Browse files
authored
Merge pull request #1094 from ethereum/feature/private-auto-sync
Feature/private auto sync
2 parents 4e20edb + 2de81aa commit 144b72b

File tree

4 files changed

+50
-3
lines changed

4 files changed

+50
-3
lines changed

ethereumj-core/src/main/java/org/ethereum/config/SystemProperties.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,12 @@ public boolean fastSyncSkipHistory() {
825825
return config.getBoolean("sync.fast.skipHistory");
826826
}
827827

828+
@ValidateMe
829+
public int makeDoneByTimeout() {
830+
return config.getInt("sync.makeDoneByTimeout");
831+
}
832+
833+
828834
@ValidateMe
829835
public boolean isPublicHomeNode() { return config.getBoolean("peer.discovery.public.home.node");}
830836

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 {

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import java.text.DecimalFormat;
3636
import java.text.DecimalFormatSymbols;
37+
import java.time.LocalDateTime;
3738
import java.util.ArrayList;
3839
import java.util.Arrays;
3940
import java.util.List;
@@ -107,6 +108,7 @@ public void accept(BlockWrapper blockWrapper) {
107108
private long importStart;
108109
private EthereumListener.SyncState syncDoneType = EthereumListener.SyncState.COMPLETE;
109110
private ScheduledExecutorService logExecutor = Executors.newSingleThreadScheduledExecutor();
111+
private LocalDateTime initRegularTime;
110112

111113
private AtomicInteger blocksInMem = new AtomicInteger(0);
112114

@@ -166,6 +168,26 @@ void initRegularSync(EthereumListener.SyncState syncDoneType) {
166168

167169
syncQueueThread = new Thread (queueProducer, "SyncQueueThread");
168170
syncQueueThread.start();
171+
172+
if (config.makeDoneByTimeout() >= 0) {
173+
logger.info("Custom long sync done timeout set to {} second(s)", config.makeDoneByTimeout());
174+
this.initRegularTime = LocalDateTime.now();
175+
ScheduledExecutorService shortSyncAwait = Executors.newSingleThreadScheduledExecutor();
176+
shortSyncAwait.scheduleAtFixedRate(() -> {
177+
try {
178+
if (LocalDateTime.now().minusSeconds(config.makeDoneByTimeout()).isAfter(initRegularTime) &&
179+
getLastKnownBlockNumber() == blockchain.getBestBlock().getNumber()) {
180+
logger.info("Sync done triggered by timeout");
181+
makeSyncDone();
182+
shortSyncAwait.shutdown();
183+
} else if (syncDone) {
184+
shortSyncAwait.shutdown();
185+
}
186+
} catch (Exception e) {
187+
logger.error("Unexpected", e);
188+
}
189+
}, 0, 10, TimeUnit.SECONDS);
190+
}
169191
}
170192

171193
public SyncStatus getSyncStatus() {

ethereumj-core/src/main/resources/ethereumj.conf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,12 @@ sync {
305305
# exit if we receive a block that causes state conflict
306306
# this option is mainly for debugging purposes
307307
exitOnBlockConflict = false
308+
309+
# Make long sync done (switch to short sync) in XX seconds
310+
# if all known blocks already downloaded.
311+
# Useful in private networks where auto-switch could fail.
312+
# Recommended value for private networks: 60 (seconds)
313+
makeDoneByTimeout = -1
308314
}
309315

310316
# miner options

0 commit comments

Comments
 (0)