Skip to content

Commit

Permalink
update implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Karim TAAM <[email protected]>
  • Loading branch information
matkt committed Jan 25, 2023
1 parent 9d179f9 commit fd31a2d
Show file tree
Hide file tree
Showing 17 changed files with 66 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,9 @@ public Optional<TrieLogLayer> getTrieLogLayer(final Hash blockHash) {
return rootWorldStateStorage.getTrieLog(blockHash).map(TrieLogLayer::fromBytes);
}
}

@Override
public void reset() {
cachedWorldStatesByHash.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -329,4 +329,9 @@ public void useFallbackNodeFinder(final Optional<PeerTrieNodeFinder> fallbackNod
checkNotNull(fallbackNodeFinder);
worldStateStorage.useFallbackNodeFinder(fallbackNodeFinder);
}

@Override
public void reset() {
trieLogManager.reset();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ void saveTrieLog(

Optional<TrieLogLayer> getTrieLogLayer(final Hash blockHash);

void reset();

interface CachedWorldState<Z extends MutableWorldState> {
void dispose();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,19 @@ public synchronized void unsafeSetChainHead(
updater.commit();
}

@Override
public void moveHeadToLastSafeBlock() {
getSafeBlock()
.ifPresent(
hash ->
getBlockHeader(hash)
.ifPresent(
blockHeader ->
getTotalDifficultyByHash(hash)
.ifPresent(
difficulty -> unsafeSetChainHead(blockHeader, difficulty))));
}

private Difficulty calculateTotalDifficulty(final BlockHeader blockHeader) {
if (blockHeader.getNumber() == BlockHeader.GENESIS_BLOCK_NUMBER) {
return blockHeader.getDifficulty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ void unsafeImportBlock(

void unsafeSetChainHead(final BlockHeader blockHeader, final Difficulty totalDifficulty);

void moveHeadToLastSafeBlock();

/**
* Rolls back the canonical chainhead to the specified block number.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package org.hyperledger.besu.ethereum.core;

import org.hyperledger.besu.plugin.data.Address;
import org.hyperledger.besu.plugin.data.SyncStatus;
import org.hyperledger.besu.plugin.services.BesuEvents;

Expand Down Expand Up @@ -42,7 +43,7 @@ public interface Synchronizer {

boolean resyncWorldState();

boolean healWorldState();
boolean healWorldState(final Optional<Address> maybeAccountToRepair);

long subscribeSyncStatus(final BesuEvents.SyncStatusListener listener);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,9 @@ public Optional<WorldStateProof> getAccountProof(
final List<UInt256> accountStorageKeys) {
return worldStateProof.getAccountProof(worldStateRoot, accountAddress, accountStorageKeys);
}

@Override
public void reset() {
// no op
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,6 @@ public interface WorldStateArchive {

Optional<WorldStateProof> getAccountProof(
Hash worldStateRoot, Address accountAddress, List<UInt256> accountStorageKeys);

void reset();
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.hyperledger.besu.ethereum.worldstate.Pruner;
import org.hyperledger.besu.ethereum.worldstate.WorldStateStorage;
import org.hyperledger.besu.metrics.BesuMetricCategory;
import org.hyperledger.besu.plugin.data.Address;
import org.hyperledger.besu.plugin.data.SyncStatus;
import org.hyperledger.besu.plugin.services.BesuEvents.SyncStatusListener;
import org.hyperledger.besu.plugin.services.MetricsSystem;
Expand Down Expand Up @@ -74,6 +75,8 @@ public class DefaultSynchronizer implements Synchronizer, UnverifiedForkchoiceLi
private final PivotBlockSelector pivotBlockSelector;
private final SyncTerminationCondition terminationCondition;

private StorageProvider storageProvider;

public DefaultSynchronizer(
final SynchronizerConfiguration syncConfig,
final ProtocolSchedule protocolSchedule,
Expand All @@ -99,6 +102,7 @@ public DefaultSynchronizer(
this.worldStateStorage = worldStateStorage;
this.metricsSystem = metricsSystem;
this.terminationCondition = terminationCondition;
this.storageProvider = storageProvider;

ChainHeadTracker.trackChainHeadForPeers(
ethContext,
Expand Down Expand Up @@ -319,20 +323,24 @@ public boolean resyncWorldState() {
stop();
fastSyncDownloader.get().deleteFastSyncState();
}

// recreate fast sync with resync and start
this.syncState.markInitialSyncRestart();
this.syncState.markResyncNeeded();
this.worldStateStorage.clear();
this.fastSyncDownloader = this.fastSyncFactory.get();
start();
return true;
}

@Override
public boolean healWorldState() {
public boolean healWorldState(final Optional<Address> maybeAccountToRepair) {
// recreate fast sync with resync and start
this.syncState.markInitialSyncRestart();
this.syncState.markHealNeeded();
this.syncState.markResyncNeeded();
this.worldStateStorage.clearFlatDatabase();
this.protocolContext.getBlockchain().moveHeadToLastSafeBlock();
this.protocolContext.getWorldStateArchive().reset();
this.syncState.markAccountToRepair(maybeAccountToRepair);
this.fastSyncDownloader = this.fastSyncFactory.get();
start();
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package org.hyperledger.besu.ethereum.eth.sync.checkpointsync;

import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.eth.manager.EthContext;
Expand Down Expand Up @@ -81,12 +82,10 @@ public static Optional<FastSyncDownloader<?>> createCheckpointDownloader(
fastSyncStateStorage.loadState(ScheduleBasedBlockHeaderFunctions.create(protocolSchedule));

if (syncState.isResyncNeeded()) {
System.out.println("resync");
snapContext.clear();
worldStateStorage.clear();
} else if (syncState.isHealNeeded()) {
System.out.println("heal");
snapContext.markHealing();
syncState
.getAccountToRepair()
.ifPresent(address -> snapContext.addInconsistentAccount(Hash.hash(address)));
} else if (fastSyncState.getPivotBlockHeader().isEmpty()
&& protocolContext.getBlockchain().getChainHeadBlockNumber()
!= BlockHeader.GENESIS_BLOCK_NUMBER) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,8 @@ public static Optional<FastSyncDownloader<?>> create(
final FastSyncState fastSyncState =
fastSyncStateStorage.loadState(ScheduleBasedBlockHeaderFunctions.create(protocolSchedule));

if (syncState.isResyncNeeded()) {
worldStateStorage.clear();
} else if (fastSyncState.getPivotBlockHeader().isEmpty()
if (!syncState.isResyncNeeded()
&& fastSyncState.getPivotBlockHeader().isEmpty()
&& protocolContext.getBlockchain().getChainHeadBlockNumber()
!= BlockHeader.GENESIS_BLOCK_NUMBER) {
LOG.info(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package org.hyperledger.besu.ethereum.eth.sync.snapsync;

import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.eth.manager.EthContext;
Expand Down Expand Up @@ -77,12 +78,10 @@ public static Optional<FastSyncDownloader<?>> createSnapDownloader(
fastSyncStateStorage.loadState(ScheduleBasedBlockHeaderFunctions.create(protocolSchedule));

if (syncState.isResyncNeeded()) {
System.out.println("resync");
snapContext.clear();
worldStateStorage.clear();
} else if (syncState.isHealNeeded()) {
System.out.println("heal");
snapContext.markHealing();
syncState
.getAccountToRepair()
.ifPresent(address -> snapContext.addInconsistentAccount(Hash.hash(address)));
} else if (fastSyncState.getPivotBlockHeader().isEmpty()
&& protocolContext.getBlockchain().getChainHeadBlockNumber()
!= BlockHeader.GENESIS_BLOCK_NUMBER) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ public class SnapPersistedContext {
private final byte[] SNAP_INCONSISTENT_ACCOUNT_INDEX =
"snapInconsistentAccountsStorageIndex".getBytes(StandardCharsets.UTF_8);

private final byte[] SNAP_HEALING = "snapHealingStatus".getBytes(StandardCharsets.UTF_8);

private final GenericKeyValueStorageFacade<BigInteger, AccountRangeDataRequest>
accountRangeToDownload;
private final GenericKeyValueStorageFacade<BigInteger, Bytes> healContext;
Expand Down Expand Up @@ -120,17 +118,6 @@ public HashSet<Bytes> getInconsistentAccounts() {
.collect(Collectors.toCollection(HashSet::new));
}

public boolean isHealing() {
return getCurrentAccountRange().isEmpty()
&& (healContext.get(SNAP_HEALING).isPresent() || !getInconsistentAccounts().isEmpty());
}

public void markHealing() {
healContext.putAll(
keyValueStorageTransaction ->
keyValueStorageTransaction.put(SNAP_HEALING, new byte[] {0x01}));
}

public void clearAccountRangeTasks() {
accountRangeToDownload.clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ protected synchronized void cleanupQueues() {

public synchronized void startHeal() {
snapContext.clearAccountRangeTasks();
snapContext.markHealing();
snapSyncState.setHealStatus(true);
// try to find new pivot block before healing
dynamicPivotBlockManager.switchToNewPivotBlock(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public CompletableFuture<Void> run(
snapDataRequest.getStartKeyHash(), snapDataRequest.getEndKeyHash());
newDownloadState.enqueueRequest(snapDataRequest);
});
} else if (snapContext.isHealing()) { // restart only the heal step
} else if (!snapContext.getInconsistentAccounts().isEmpty()) { // restart only the heal step
snapSyncState.setHealStatus(true);
newDownloadState.setInconsistentAccounts(inconsistentAccounts);
newDownloadState.enqueueRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.hyperledger.besu.ethereum.eth.sync.fastsync.checkpoint.Checkpoint;
import org.hyperledger.besu.ethereum.eth.sync.worldstate.WorldStateDownloadStatus;
import org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage.DisconnectReason;
import org.hyperledger.besu.plugin.data.Address;
import org.hyperledger.besu.plugin.data.SyncStatus;
import org.hyperledger.besu.plugin.services.BesuEvents.InitialSyncCompletionListener;
import org.hyperledger.besu.plugin.services.BesuEvents.SyncStatusListener;
Expand Down Expand Up @@ -60,7 +61,7 @@ public class SyncState {

private volatile boolean isResyncNeeded;

private volatile boolean isHealNeeded;
private Optional<Address> maybeAccountToRepair;

public SyncState(final Blockchain blockchain, final EthPeers ethPeers) {
this(blockchain, ethPeers, false, Optional.empty());
Expand Down Expand Up @@ -332,12 +333,12 @@ public void markResyncNeeded() {
isResyncNeeded = true;
}

public boolean isHealNeeded() {
return isHealNeeded;
public Optional<Address> getAccountToRepair() {
return maybeAccountToRepair;
}

public void markHealNeeded() {
isHealNeeded = true;
public void markAccountToRepair(final Optional<Address> address) {
maybeAccountToRepair = address;
}

public void markInitialSyncRestart() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.hyperledger.besu.ethereum.retesteth;

import org.hyperledger.besu.ethereum.core.Synchronizer;
import org.hyperledger.besu.plugin.data.Address;
import org.hyperledger.besu.plugin.data.SyncStatus;
import org.hyperledger.besu.plugin.services.BesuEvents;

Expand Down Expand Up @@ -50,7 +51,7 @@ public boolean resyncWorldState() {
}

@Override
public boolean healWorldState() {
public boolean healWorldState(final Optional<Address> maybeAccountToRepair) {
return false;
}

Expand Down

0 comments on commit fd31a2d

Please sign in to comment.