Skip to content

Commit

Permalink
Make blocks locked when used + minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
zilm13 committed Oct 13, 2023
1 parent 3e48739 commit a871db0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.locks.Lock;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
Expand All @@ -37,6 +38,22 @@ static BlockProvider fromDynamicMap(Supplier<Map<Bytes32, SignedBeaconBlock>> ma
return (roots) -> fromMap(mapSupplier.get()).getBlocks(roots);
}

static BlockProvider fromMapWithLock(
final Map<Bytes32, SignedBeaconBlock> blockMap, final Lock readLock) {
return (roots) -> {
readLock.lock();
try {
return SafeFuture.completedFuture(
roots.stream()
.map(blockMap::get)
.filter(Objects::nonNull)
.collect(Collectors.toMap(SignedBeaconBlock::getRoot, Function.identity())));
} finally {
readLock.unlock();
}
};
}

static BlockProvider fromMap(final Map<Bytes32, SignedBeaconBlock> blockMap) {
return (roots) ->
SafeFuture.completedFuture(
Expand Down
11 changes: 3 additions & 8 deletions storage/src/main/java/tech/pegasys/teku/storage/store/Store.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import static com.google.common.base.Preconditions.checkArgument;
import static tech.pegasys.teku.dataproviders.generators.StateAtSlotTask.AsyncStateProvider.fromAnchor;
import static tech.pegasys.teku.dataproviders.lookup.BlockProvider.fromDynamicMap;
import static tech.pegasys.teku.dataproviders.lookup.BlockProvider.fromMap;
import static tech.pegasys.teku.dataproviders.lookup.BlockProvider.fromMapWithLock;
import static tech.pegasys.teku.infrastructure.time.TimeUtilities.secondsToMillis;

import com.google.common.annotations.VisibleForTesting;
Expand Down Expand Up @@ -178,7 +178,7 @@ private Store(
.getSignedBeaconBlock()
.map((b) -> Map.of(b.getRoot(), b))
.orElseGet(Collections::emptyMap)),
fromMap(this.blocks),
fromMapWithLock(this.blocks, readLock),
blockProvider);
this.blobSidecarsProvider = blobSidecarsProvider;
this.earliestBlobSidecarSlotProvider = earliestBlobSidecarSlotProvider;
Expand Down Expand Up @@ -332,6 +332,7 @@ public ForkChoiceStrategy getForkChoiceStrategy() {
}

@Override
@VisibleForTesting
public void clearCaches() {
states.clear();
checkpointStates.clear();
Expand Down Expand Up @@ -492,12 +493,6 @@ public SafeFuture<Optional<SignedBeaconBlock>> retrieveSignedBlock(final Bytes32
if (!containsBlock(blockRoot)) {
return EmptyStoreResults.EMPTY_SIGNED_BLOCK_FUTURE;
}
final Optional<SignedBeaconBlock> inMemoryBlock = getBlockIfAvailable(blockRoot);
if (inMemoryBlock.isPresent()) {
return SafeFuture.completedFuture(inMemoryBlock);
}

// Retrieve and cache block
return blockProvider.getBlock(blockRoot);
}

Expand Down

0 comments on commit a871db0

Please sign in to comment.