Skip to content

Commit

Permalink
FullSync Future should stop when total terminal difficulty is reached
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 16, 2022
1 parent 5643dd9 commit 9eb1598
Show file tree
Hide file tree
Showing 10 changed files with 257 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.hyperledger.besu.ethereum.chain.DefaultBlockchain;
import org.hyperledger.besu.ethereum.chain.GenesisState;
import org.hyperledger.besu.ethereum.chain.MutableBlockchain;
import org.hyperledger.besu.ethereum.core.Difficulty;
import org.hyperledger.besu.ethereum.core.MiningParameters;
import org.hyperledger.besu.ethereum.core.PrivacyParameters;
import org.hyperledger.besu.ethereum.core.Synchronizer;
Expand Down Expand Up @@ -371,7 +372,8 @@ public BesuController build() {
syncState,
dataDirectory,
clock,
metricsSystem);
metricsSystem,
getTerminalTotalDifficulty());

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

protected Optional<Difficulty> getTerminalTotalDifficulty() {
return genesisConfig.getConfigOptions().getTerminalTotalDifficulty().map(Difficulty::of);
}

protected void prepForBuild() {}

protected JsonRpcMethods createAdditionalJsonRpcMethodFactory(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static com.google.common.base.Preconditions.checkNotNull;

import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.core.Difficulty;
import org.hyperledger.besu.ethereum.core.Synchronizer;
import org.hyperledger.besu.ethereum.eth.manager.EthContext;
import org.hyperledger.besu.ethereum.eth.sync.fastsync.FastDownloaderFactory;
Expand Down Expand Up @@ -66,7 +67,8 @@ public DefaultSynchronizer(
final SyncState syncState,
final Path dataDirectory,
final Clock clock,
final MetricsSystem metricsSystem) {
final MetricsSystem metricsSystem,
final Optional<Difficulty> terminalTotalDifficulty) {
this.maybePruner = maybePruner;
this.syncState = syncState;

Expand All @@ -91,7 +93,13 @@ public DefaultSynchronizer(

this.fullSyncDownloader =
new FullSyncDownloader(
syncConfig, protocolSchedule, protocolContext, ethContext, syncState, metricsSystem);
syncConfig,
protocolSchedule,
protocolContext,
ethContext,
syncState,
metricsSystem,
terminalTotalDifficulty);
this.fastSyncDownloader =
FastDownloaderFactory.create(
syncConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.hyperledger.besu.ethereum.eth.sync.fullsync;

import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.core.Difficulty;
import org.hyperledger.besu.ethereum.eth.manager.EthContext;
import org.hyperledger.besu.ethereum.eth.sync.ChainDownloader;
import org.hyperledger.besu.ethereum.eth.sync.PipelineChainDownloader;
Expand All @@ -23,6 +24,8 @@
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.plugin.services.MetricsSystem;

import java.util.Optional;

public class FullSyncChainDownloader {
private FullSyncChainDownloader() {}

Expand All @@ -32,11 +35,17 @@ public static ChainDownloader create(
final ProtocolContext protocolContext,
final EthContext ethContext,
final SyncState syncState,
final MetricsSystem metricsSystem) {
final MetricsSystem metricsSystem,
final Optional<Difficulty> terminalTotalDifficulty) {

final FullSyncTargetManager syncTargetManager =
new FullSyncTargetManager(
config, protocolSchedule, protocolContext, ethContext, metricsSystem);
config,
protocolSchedule,
protocolContext,
ethContext,
metricsSystem,
terminalTotalDifficulty);

return new PipelineChainDownloader(
syncState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.hyperledger.besu.ethereum.eth.sync.fullsync;

import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.core.Difficulty;
import org.hyperledger.besu.ethereum.eth.manager.EthContext;
import org.hyperledger.besu.ethereum.eth.sync.ChainDownloader;
import org.hyperledger.besu.ethereum.eth.sync.SynchronizerConfiguration;
Expand All @@ -23,6 +24,8 @@
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.plugin.services.MetricsSystem;

import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -40,14 +43,21 @@ public FullSyncDownloader(
final ProtocolContext protocolContext,
final EthContext ethContext,
final SyncState syncState,
final MetricsSystem metricsSystem) {
final MetricsSystem metricsSystem,
final Optional<Difficulty> terminalTotalDifficulty) {
this.syncConfig = syncConfig;
this.protocolContext = protocolContext;
this.syncState = syncState;

this.chainDownloader =
FullSyncChainDownloader.create(
syncConfig, protocolSchedule, protocolContext, ethContext, syncState, metricsSystem);
syncConfig,
protocolSchedule,
protocolContext,
ethContext,
syncState,
metricsSystem,
terminalTotalDifficulty);
}

public void start() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.chain.MutableBlockchain;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.Difficulty;
import org.hyperledger.besu.ethereum.eth.manager.EthContext;
import org.hyperledger.besu.ethereum.eth.manager.EthPeer;
import org.hyperledger.besu.ethereum.eth.sync.SyncTargetManager;
Expand All @@ -39,16 +40,19 @@ class FullSyncTargetManager extends SyncTargetManager {
private static final Logger LOG = LoggerFactory.getLogger(FullSyncTargetManager.class);
private final ProtocolContext protocolContext;
private final EthContext ethContext;
private final Optional<Difficulty> terminalTotalDifficulty;

FullSyncTargetManager(
final SynchronizerConfiguration config,
final ProtocolSchedule protocolSchedule,
final ProtocolContext protocolContext,
final EthContext ethContext,
final MetricsSystem metricsSystem) {
final MetricsSystem metricsSystem,
final Optional<Difficulty> terminalTotalDifficulty) {
super(config, protocolSchedule, protocolContext, ethContext, metricsSystem);
this.protocolContext = protocolContext;
this.ethContext = ethContext;
this.terminalTotalDifficulty = terminalTotalDifficulty;
}

@Override
Expand Down Expand Up @@ -105,6 +109,9 @@ private boolean isSyncTargetReached(final EthPeer peer) {

@Override
public boolean shouldContinueDownloading() {
return true;
return terminalTotalDifficulty.isEmpty()
|| terminalTotalDifficulty
.get()
.greaterThan(protocolContext.getBlockchain().getChainHead().getTotalDifficulty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem;
import org.hyperledger.besu.plugin.services.MetricsSystem;

import java.util.Optional;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -80,7 +82,13 @@ public void tearDown() {

private ChainDownloader downloader(final SynchronizerConfiguration syncConfig) {
return FullSyncChainDownloader.create(
syncConfig, protocolSchedule, protocolContext, ethContext, syncState, metricsSystem);
syncConfig,
protocolSchedule,
protocolContext,
ethContext,
syncState,
metricsSystem,
Optional.empty());
}

private ChainDownloader downloader() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,13 @@ public void tearDown() {

private ChainDownloader downloader(final SynchronizerConfiguration syncConfig) {
return FullSyncChainDownloader.create(
syncConfig, protocolSchedule, protocolContext, ethContext, syncState, metricsSystem);
syncConfig,
protocolSchedule,
protocolContext,
ethContext,
syncState,
metricsSystem,
Optional.empty());
}

private ChainDownloader downloader() {
Expand Down
Loading

0 comments on commit 9eb1598

Please sign in to comment.