|
| 1 | +/* |
| 2 | + * Copyright Consensys Software Inc., 2024 |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +package tech.pegasys.teku.storage.server.state; |
| 15 | + |
| 16 | +import static tech.pegasys.teku.infrastructure.unsigned.UInt64.ONE; |
| 17 | + |
| 18 | +import com.google.common.cache.CacheLoader; |
| 19 | +import java.util.Optional; |
| 20 | +import java.util.concurrent.ExecutionException; |
| 21 | +import java.util.concurrent.TimeUnit; |
| 22 | +import java.util.concurrent.TimeoutException; |
| 23 | +import java.util.stream.Stream; |
| 24 | +import org.apache.logging.log4j.LogManager; |
| 25 | +import org.apache.logging.log4j.Logger; |
| 26 | +import tech.pegasys.teku.dataproviders.generators.StreamingStateRegenerator; |
| 27 | +import tech.pegasys.teku.infrastructure.async.SafeFuture; |
| 28 | +import tech.pegasys.teku.infrastructure.unsigned.UInt64; |
| 29 | +import tech.pegasys.teku.spec.Spec; |
| 30 | +import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock; |
| 31 | +import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState; |
| 32 | +import tech.pegasys.teku.storage.server.Database; |
| 33 | + |
| 34 | +class StateCacheLoader extends CacheLoader<UInt64, BeaconState> { |
| 35 | + private static final Logger LOG = LogManager.getLogger(); |
| 36 | + private final int stateRebuildTimeoutSeconds; |
| 37 | + private final Database database; |
| 38 | + private final long maxRegenerateSlots; |
| 39 | + private final FinalizedStateCache finalizedStateCache; |
| 40 | + private final Spec spec; |
| 41 | + |
| 42 | + StateCacheLoader( |
| 43 | + final Spec spec, |
| 44 | + final Database database, |
| 45 | + final int stateRebuildTimeoutSeconds, |
| 46 | + final long maxRegenerateSlots, |
| 47 | + final FinalizedStateCache finalizedStateCache) { |
| 48 | + this.database = database; |
| 49 | + this.stateRebuildTimeoutSeconds = stateRebuildTimeoutSeconds; |
| 50 | + this.maxRegenerateSlots = maxRegenerateSlots; |
| 51 | + this.finalizedStateCache = finalizedStateCache; |
| 52 | + this.spec = spec; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public BeaconState load(final UInt64 key) { |
| 57 | + return regenerateState(key).orElseThrow(FinalizedStateCache.StateUnavailableException::new); |
| 58 | + } |
| 59 | + |
| 60 | + private Optional<BeaconState> regenerateState(final UInt64 slot) { |
| 61 | + final Optional<BeaconState> maybeState = database.getLatestAvailableFinalizedState(slot); |
| 62 | + if (maybeState.isEmpty()) { |
| 63 | + return Optional.empty(); |
| 64 | + } |
| 65 | + final BeaconState state = maybeState.get(); |
| 66 | + try { |
| 67 | + return Optional.of( |
| 68 | + regenerateStateWithinReasonableTime(slot, state) |
| 69 | + .get(stateRebuildTimeoutSeconds, TimeUnit.SECONDS)); |
| 70 | + } catch (ExecutionException | InterruptedException e) { |
| 71 | + LOG.warn("Failed to regenerate state for slot {}", slot, e); |
| 72 | + return Optional.empty(); |
| 73 | + } catch (TimeoutException e) { |
| 74 | + LOG.error( |
| 75 | + "Timed out trying to regenerate state at slot {} starting from slot {} within {} seconds", |
| 76 | + slot, |
| 77 | + state.getSlot(), |
| 78 | + stateRebuildTimeoutSeconds); |
| 79 | + return Optional.empty(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private SafeFuture<BeaconState> regenerateStateWithinReasonableTime( |
| 84 | + final UInt64 slot, final BeaconState stateFromDisk) { |
| 85 | + final Optional<BeaconState> latestStateFromCache = |
| 86 | + finalizedStateCache.getLatestStateFromCache(slot); |
| 87 | + final BeaconState preState = |
| 88 | + latestStateFromCache |
| 89 | + .filter( |
| 90 | + stateFromCache -> stateFromCache.getSlot().compareTo(stateFromDisk.getSlot()) >= 0) |
| 91 | + .orElse(stateFromDisk); |
| 92 | + if (preState.getSlot().equals(slot)) { |
| 93 | + return SafeFuture.completedFuture(preState); |
| 94 | + } |
| 95 | + final long regenerateSlotCount = slot.minusMinZero(stateFromDisk.getSlot()).longValue(); |
| 96 | + LOG.trace("Slots to regenerate state from: {}", regenerateSlotCount); |
| 97 | + if (regenerateSlotCount > maxRegenerateSlots) { |
| 98 | + LOG.error( |
| 99 | + "Refusing to regenerate a state that is {} slots from what we have stored", |
| 100 | + regenerateSlotCount); |
| 101 | + return SafeFuture.failedFuture(new FinalizedStateCache.StateUnavailableException()); |
| 102 | + } |
| 103 | + try (final Stream<SignedBeaconBlock> blocks = |
| 104 | + database.streamFinalizedBlocks(preState.getSlot().plus(ONE), slot)) { |
| 105 | + final BeaconState state = StreamingStateRegenerator.regenerate(spec, preState, blocks); |
| 106 | + finalizedStateCache.getAvailableSlots().add(state.getSlot()); |
| 107 | + return SafeFuture.completedFuture(state); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments