Skip to content

Commit

Permalink
Skip calling for finalized state if not in archive storage mode (#7497)
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Harris <[email protected]>
  • Loading branch information
rolfyone authored Sep 21, 2023
1 parent 8bd45a6 commit d64fd19
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ protected SafeFuture<?> doStart() {
config.isStoreNonCanonicalBlocksEnabled()));
}
final EventChannels eventChannels = serviceConfig.getEventChannels();
chainStorage = ChainStorage.create(database, config.getSpec());
chainStorage =
ChainStorage.create(database, config.getSpec(), config.getDataStorageMode());
final DepositStorage depositStorage =
DepositStorage.create(
eventChannels.getPublisher(Eth1EventsChannel.class),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ public SafeFuture<Optional<BeaconState>> getStateByBlockRoot(final Bytes32 block
if (maybeState.isPresent()) {
return completedFuture(maybeState);
}
LOG.debug(
"State not found in store, querying historicalData for {}", () -> blockRoot);
return historicalChainData.getFinalizedStateByBlockRoot(blockRoot);
});
}
Expand Down Expand Up @@ -401,6 +403,10 @@ private SafeFuture<Optional<BeaconState>> getStateFromSlotAndBlock(
if (maybeState.isPresent()) {
return SafeFuture.completedFuture(maybeState);
}
LOG.debug(
"State not found in store, querying historicalData for {}:{}",
slotAndBlockRoot::getSlot,
slotAndBlockRoot::getBlockRoot);
return getFinalizedStateFromSlotAndBlock(slotAndBlockRoot);
});
}
Expand All @@ -424,6 +430,10 @@ private Optional<BeaconState> regenerateBeaconState(
return Optional.empty();
}
try {
LOG.debug(
"Processing slots to regenerate state from slot {}, target slot {}",
preState::getSlot,
() -> slot);
return Optional.of(spec.processSlots(preState, slot));
} catch (SlotProcessingException | EpochProcessingException | IllegalArgumentException e) {
LOG.debug("State Transition error", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.ethereum.pow.api.DepositTreeSnapshot;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
Expand Down Expand Up @@ -50,21 +52,30 @@

public class ChainStorage
implements StorageUpdateChannel, StorageQueryChannel, VoteUpdateChannel, ChainStorageFacade {

private static final Logger LOG = LogManager.getLogger();
private final Database database;
private final FinalizedStateCache finalizedStateCache;

private final StateStorageMode dataStorageMode;

private Optional<OnDiskStoreData> cachedStoreData = Optional.empty();

private ChainStorage(final Database database, final FinalizedStateCache finalizedStateCache) {
private ChainStorage(
final Database database,
final FinalizedStateCache finalizedStateCache,
final StateStorageMode dataStorageMode) {
this.database = database;
this.finalizedStateCache = finalizedStateCache;
this.dataStorageMode = dataStorageMode;
}

public static ChainStorage create(final Database database, final Spec spec) {
public static ChainStorage create(
final Database database, final Spec spec, final StateStorageMode dataStorageMode) {
final int finalizedStateCacheSize = spec.getSlotsPerEpoch(SpecConfig.GENESIS_EPOCH) * 3;
return new ChainStorage(
database, new FinalizedStateCache(spec, database, finalizedStateCacheSize, true));
database,
new FinalizedStateCache(spec, database, finalizedStateCacheSize, true),
dataStorageMode);
}

private synchronized Optional<OnDiskStoreData> getStore() {
Expand Down Expand Up @@ -224,12 +235,22 @@ public SafeFuture<Optional<SlotAndBlockRoot>> getSlotAndBlockRootByStateRoot(

@Override
public SafeFuture<Optional<BeaconState>> getLatestFinalizedStateAtSlot(final UInt64 slot) {
return SafeFuture.of(() -> getLatestFinalizedStateAtSlotSync(slot));
if (dataStorageMode.storesFinalizedStates()) {
return SafeFuture.of(() -> getLatestFinalizedStateAtSlotSync(slot));
}
LOG.debug("Not storing archive states - not fetching finalized state at slot {}", slot);
return SafeFuture.completedFuture(Optional.empty());
}

@Override
public SafeFuture<Optional<BeaconState>> getLatestAvailableFinalizedState(UInt64 slot) {
return SafeFuture.of(() -> getLatestAvailableFinalizedStateSync(slot));
if (dataStorageMode.storesFinalizedStates()) {
return SafeFuture.of(() -> getLatestAvailableFinalizedStateSync(slot));
}
LOG.debug(
"Not storing archive states - not fetching latest available finalized state at slot {}",
slot);
return SafeFuture.completedFuture(Optional.empty());
}

@Override
Expand All @@ -239,8 +260,12 @@ public SafeFuture<Optional<UInt64>> getFinalizedSlotByBlockRoot(final Bytes32 bl

@Override
public SafeFuture<Optional<BeaconState>> getFinalizedStateByBlockRoot(final Bytes32 blockRoot) {
return getFinalizedSlotByBlockRoot(blockRoot)
.thenApply(slot -> slot.flatMap(this::getLatestFinalizedStateAtSlotSync));
if (dataStorageMode.storesFinalizedStates()) {
return getFinalizedSlotByBlockRoot(blockRoot)
.thenApply(slot -> slot.flatMap(this::getLatestFinalizedStateAtSlotSync));
}
LOG.debug("Not storing archive states - not fetching finalized state at root {}", blockRoot);
return SafeFuture.completedFuture(Optional.empty());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ static StorageSystem create(
final StubMetricsSystem metricsSystem = new StubMetricsSystem();

// Create and start storage server
final ChainStorage chainStorageServer = ChainStorage.create(database, spec);
final ChainStorage chainStorageServer = ChainStorage.create(database, spec, storageMode);

// Create recent chain data
final FinalizedCheckpointChannel finalizedCheckpointChannel =
Expand Down

0 comments on commit d64fd19

Please sign in to comment.