Skip to content

Commit

Permalink
Terminal Condition is now a Boolean Provider
Browse files Browse the repository at this point in the history
Signed-off-by: Jiri Peinlich <jiri.peinlich@gmail.com>
  • Loading branch information
gezero committed Feb 23, 2022
1 parent 622e7f7 commit cdaf904
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ public BesuController build() {
dataDirectory,
clock,
metricsSystem,
getFullSyncTerminationCondition());
getFullSyncTerminationCondition(protocolContext.getBlockchain()));

final MiningCoordinator miningCoordinator =
createMiningCoordinator(
Expand Down Expand Up @@ -418,11 +418,12 @@ public BesuController build() {
additionalPluginServices);
}

protected FullSyncTerminationCondition getFullSyncTerminationCondition() {
protected FullSyncTerminationCondition getFullSyncTerminationCondition(
final Blockchain blockchain) {
return genesisConfig
.getConfigOptions()
.getTerminalTotalDifficulty()
.map(FullSyncTerminationCondition::difficulty)
.map(difficulty -> FullSyncTerminationCondition.difficulty(difficulty, blockchain))
.orElse(FullSyncTerminationCondition.never());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.hyperledger.besu.ethereum.eth.sync.fullsync;

import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.chain.Blockchain;

public class FlexibleBlockHashTerminalCondition implements FullSyncTerminationCondition {
private Hash blockHash;
private final Blockchain blockchain;

public FlexibleBlockHashTerminalCondition(final Hash blockHash, final Blockchain blockchain) {
this.blockHash = blockHash;
this.blockchain = blockchain;
}

public synchronized void setBlockHash(final Hash blockHash) {
this.blockHash = blockHash;
}

@Override
public synchronized boolean getAsBoolean() {
return blockchain.contains(blockHash);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,6 @@ private boolean isSyncTargetReached(final EthPeer peer) {

@Override
public boolean shouldContinueDownloading() {
return terminationCondition.test(protocolContext.getBlockchain());
return terminationCondition.getAsBoolean();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,31 @@
*/
package org.hyperledger.besu.ethereum.eth.sync.fullsync;

import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.ethereum.core.Difficulty;

import java.util.function.Predicate;
import java.util.function.BooleanSupplier;

import org.apache.tuweni.units.bigints.UInt256;

public interface FullSyncTerminationCondition extends Predicate<Blockchain> {
public interface FullSyncTerminationCondition extends BooleanSupplier {
static FullSyncTerminationCondition never() {
return blockchain -> false;
return () -> true;
}

static FullSyncTerminationCondition difficulty(final UInt256 difficulty) {
return difficulty(Difficulty.of(difficulty));
static FullSyncTerminationCondition difficulty(
final UInt256 difficulty, final Blockchain blockchain) {
return difficulty(Difficulty.of(difficulty), blockchain);
}

static FullSyncTerminationCondition difficulty(final Difficulty difficulty) {
return blockchain -> difficulty.greaterThan(blockchain.getChainHead().getTotalDifficulty());
static FullSyncTerminationCondition difficulty(
final Difficulty difficulty, final Blockchain blockchain) {
return () -> difficulty.greaterThan(blockchain.getChainHead().getTotalDifficulty());
}

static FlexibleBlockHashTerminalCondition blockHash(
final Hash blockHash, final Blockchain blockchain) {
return new FlexibleBlockHashTerminalCondition(blockHash, blockchain);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ public void syncsFullyAndStopsWhenTTDReached() {
final SynchronizerConfiguration syncConfig =
syncConfigBuilder().downloaderChainSegmentSize(1).downloaderParallelism(1).build();
final ChainDownloader downloader =
downloader(syncConfig, FullSyncTerminationCondition.difficulty(TARGET_TERMINAL_DIFFICULTY));
downloader(
syncConfig,
FullSyncTerminationCondition.difficulty(TARGET_TERMINAL_DIFFICULTY, localBlockchain));
final CompletableFuture<Void> future = downloader.start();

assertThat(future.isDone()).isFalse();
Expand Down

0 comments on commit cdaf904

Please sign in to comment.