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

Added interface for manual switch to Short Sync #1050

Merged
merged 1 commit into from
Apr 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.math.BigInteger;
import java.net.InetAddress;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;

/**
Expand Down Expand Up @@ -216,5 +217,12 @@ ProgramResult callConstantFunction(String receiveAddress, ECKey senderPrivateKey
*/
Integer getChainIdForNextBlock();

/**
* Manual switch to Short Sync mode
* Maybe useful in small private and detached networks when automatic detection fails
* @return Future, which completes when syncDone is turned to True in {@link org.ethereum.sync.SyncManager}
*/
CompletableFuture<Void> switchToShortSync();

void exitOn(long number);
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import java.math.BigInteger;
import java.net.InetAddress;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

Expand Down Expand Up @@ -380,6 +381,10 @@ public Integer getChainIdForNextBlock() {
return nextBlockConfig.getChainId();
}

public CompletableFuture<Void> switchToShortSync() {
return syncManager.switchToShortSync();
}

@Override
public void exitOn(long number) {
worldManager.getBlockchain().setExitOn(number);
Expand Down
32 changes: 29 additions & 3 deletions ethereumj-core/src/main/java/org/ethereum/sync/SyncManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,7 @@ private void produceQueue() {
wrapper.getBlock().getTransactionsList().size(), ts);

if (wrapper.isNewBlock() && !syncDone) {
syncDone = true;
channelManager.onSyncDone(true);
compositeEthereumListener.onSyncDone(syncDoneType);
makeSyncDone();
}
}

Expand Down Expand Up @@ -311,6 +309,34 @@ private void produceQueue() {
}
}

private synchronized void makeSyncDone() {
if (syncDone) return;
syncDone = true;
channelManager.onSyncDone(true);
compositeEthereumListener.onSyncDone(syncDoneType);
}

public CompletableFuture<Void> switchToShortSync() {
final CompletableFuture<Void> syncDoneF = new CompletableFuture<>();
if(!syncDone) {
new Thread(() -> {
while(!blockQueue.isEmpty() && !syncDone) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
syncDoneF.completeExceptionally(e);
}
}
makeSyncDone();
syncDoneF.complete(null);
}).start();
} else {
syncDoneF.complete(null);
}

return syncDoneF;
}

/**
* Adds NEW block to the queue
*
Expand Down
Loading