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

Commit 50b99fe

Browse files
zilm13mkalinin
authored andcommitted
Added interface for manual switch to Short Sync (#1050)
1 parent b1a56c1 commit 50b99fe

File tree

4 files changed

+416
-3
lines changed

4 files changed

+416
-3
lines changed

ethereumj-core/src/main/java/org/ethereum/facade/Ethereum.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.math.BigInteger;
3333
import java.net.InetAddress;
3434
import java.util.List;
35+
import java.util.concurrent.CompletableFuture;
3536
import java.util.concurrent.Future;
3637

3738
/**
@@ -216,5 +217,12 @@ ProgramResult callConstantFunction(String receiveAddress, ECKey senderPrivateKey
216217
*/
217218
Integer getChainIdForNextBlock();
218219

220+
/**
221+
* Manual switch to Short Sync mode
222+
* Maybe useful in small private and detached networks when automatic detection fails
223+
* @return Future, which completes when syncDone is turned to True in {@link org.ethereum.sync.SyncManager}
224+
*/
225+
CompletableFuture<Void> switchToShortSync();
226+
219227
void exitOn(long number);
220228
}

ethereumj-core/src/main/java/org/ethereum/facade/EthereumImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import java.math.BigInteger;
5757
import java.net.InetAddress;
5858
import java.util.*;
59+
import java.util.concurrent.CompletableFuture;
5960
import java.util.concurrent.ExecutionException;
6061
import java.util.concurrent.Future;
6162

@@ -380,6 +381,10 @@ public Integer getChainIdForNextBlock() {
380381
return nextBlockConfig.getChainId();
381382
}
382383

384+
public CompletableFuture<Void> switchToShortSync() {
385+
return syncManager.switchToShortSync();
386+
}
387+
383388
@Override
384389
public void exitOn(long number) {
385390
worldManager.getBlockchain().setExitOn(number);

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,7 @@ private void produceQueue() {
274274
wrapper.getBlock().getTransactionsList().size(), ts);
275275

276276
if (wrapper.isNewBlock() && !syncDone) {
277-
syncDone = true;
278-
channelManager.onSyncDone(true);
279-
compositeEthereumListener.onSyncDone(syncDoneType);
277+
makeSyncDone();
280278
}
281279
}
282280

@@ -311,6 +309,34 @@ private void produceQueue() {
311309
}
312310
}
313311

312+
private synchronized void makeSyncDone() {
313+
if (syncDone) return;
314+
syncDone = true;
315+
channelManager.onSyncDone(true);
316+
compositeEthereumListener.onSyncDone(syncDoneType);
317+
}
318+
319+
public CompletableFuture<Void> switchToShortSync() {
320+
final CompletableFuture<Void> syncDoneF = new CompletableFuture<>();
321+
if(!syncDone) {
322+
new Thread(() -> {
323+
while(!blockQueue.isEmpty() && !syncDone) {
324+
try {
325+
Thread.sleep(100);
326+
} catch (InterruptedException e) {
327+
syncDoneF.completeExceptionally(e);
328+
}
329+
}
330+
makeSyncDone();
331+
syncDoneF.complete(null);
332+
}).start();
333+
} else {
334+
syncDoneF.complete(null);
335+
}
336+
337+
return syncDoneF;
338+
}
339+
314340
/**
315341
* Adds NEW block to the queue
316342
*

0 commit comments

Comments
 (0)