Skip to content

Commit

Permalink
Blob sidecars availability checker (#7081)
Browse files Browse the repository at this point in the history
  • Loading branch information
tbenr authored May 5, 2023
1 parent 7a273fd commit 5795062
Show file tree
Hide file tree
Showing 34 changed files with 1,186 additions and 433 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import tech.pegasys.teku.spec.datastructures.state.Fork;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;
import tech.pegasys.teku.spec.logic.common.util.AsyncBLSSignatureVerifier;
import tech.pegasys.teku.spec.logic.versions.deneb.blobs.BlobSidecarsAndValidationResult;
import tech.pegasys.teku.statetransition.blobs.BlobSidecarManager;
import tech.pegasys.teku.storage.api.StorageUpdateChannel;
import tech.pegasys.teku.storage.client.CombinedChainDataClient;
Expand Down Expand Up @@ -328,10 +329,8 @@ private SafeFuture<Void> importBatch() {
.thenCompose(
__ -> {
final UInt64 latestSlotInBatch = blocksToImport.getLast().getSlot();
return validateBlobSidecars(latestSlotInBatch, blocksToImport);
})
.thenCompose(
validSignatures -> {
validateBlobSidecars(latestSlotInBatch, blocksToImport);

final SignedBeaconBlock newEarliestBlock = blocksToImport.getFirst();
return storageUpdateChannel
.onFinalizedBlocks(blocksToImport, new HashMap<>(blobSidecarsBySlotToImport))
Expand Down Expand Up @@ -392,36 +391,33 @@ private SafeFuture<Void> batchVerifyHistoricalBlockSignature(
});
}

private SafeFuture<Void> validateBlobSidecars(
private void validateBlobSidecars(
final UInt64 latestSlotInBatch, final Collection<SignedBeaconBlock> blocks) {
if (!blobSidecarManager.isAvailabilityRequiredAtSlot(latestSlotInBatch)) {
return SafeFuture.COMPLETE;
return;
}
LOG.trace("Validating blob sidecars for a batch");
return SafeFuture.allOfFailFast(blocks.stream().map(this::validateBlobSidecars));
blocks.forEach(this::validateBlobSidecars);
}

private SafeFuture<Void> validateBlobSidecars(final SignedBeaconBlock block) {
private void validateBlobSidecars(final SignedBeaconBlock block) {
final List<BlobSidecar> blobSidecars =
blobSidecarsBySlotToImport.getOrDefault(block.getSlot(), Collections.emptyList());
LOG.trace("Validating {} blob sidecars for block {}", blobSidecars.size(), block.getRoot());
return blobSidecarManager
.createAvailabilityChecker(block)
.validate(blobSidecars)
.thenAccept(
validationResult -> {
if (validationResult.isFailure()) {
final String causeMessage =
validationResult
.getCause()
.map(cause -> " (" + ExceptionUtil.getRootCauseMessage(cause) + ")")
.orElse("");
throw new IllegalArgumentException(
String.format(
"Blob sidecars validation for block %s failed: %s%s",
block.getRoot(), validationResult.getValidationResult(), causeMessage));
}
});
final BlobSidecarsAndValidationResult validationResult =
blobSidecarManager.createAvailabilityChecker(block).validateImmediately(blobSidecars);

if (validationResult.isFailure()) {
final String causeMessage =
validationResult
.getCause()
.map(cause -> " (" + ExceptionUtil.getRootCauseMessage(cause) + ")")
.orElse("");
throw new IllegalArgumentException(
String.format(
"Blob sidecars validation for block %s failed: %s%s",
block.getRoot(), validationResult.getValidationResult(), causeMessage));
}
}

private RequestParameters calculateRequestParams() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,8 @@ public void setup() {
.thenReturn(SafeFuture.completedFuture(true));
when(blobSidecarManager.createAvailabilityChecker(any()))
.thenReturn(blobSidecarsAvailabilityChecker);
when(blobSidecarsAvailabilityChecker.validate(anyList()))
.thenAnswer(
i ->
SafeFuture.completedFuture(
BlobSidecarsAndValidationResult.validResult(i.getArgument(0))));
when(blobSidecarsAvailabilityChecker.validateImmediately(anyList()))
.thenAnswer(i -> BlobSidecarsAndValidationResult.validResult(i.getArgument(0)));
}

@Test
Expand Down Expand Up @@ -201,12 +198,11 @@ public void run_returnAllBlocksAndBlobSidecarsOnFirstRequest() {
@Test
public void run_failsOnBlobSidecarsValidationFailure() {
when(blobSidecarManager.isAvailabilityRequiredAtSlot(any())).thenReturn(true);
when(blobSidecarsAvailabilityChecker.validate(anyList()))
when(blobSidecarsAvailabilityChecker.validateImmediately(anyList()))
.thenAnswer(
i ->
SafeFuture.completedFuture(
BlobSidecarsAndValidationResult.invalidResult(
i.getArgument(0), new IllegalStateException("oopsy"))));
BlobSidecarsAndValidationResult.invalidResult(
i.getArgument(0), new IllegalStateException("oopsy")));

assertThat(peer.getOutstandingRequests()).isEqualTo(0);
final SafeFuture<BeaconBlockSummary> future = fetcher.run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public static SyncingNodeManager create(
new BlockManager(
recentChainData,
blockImporter,
blobSidecarPool,
pendingBlocks,
futureBlocks,
invalidBlockRoots,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;
import tech.pegasys.teku.spec.logic.common.statetransition.exceptions.StateTransitionException;
import tech.pegasys.teku.spec.logic.versions.deneb.blobs.BlobsSidecarAvailabilityChecker;
import tech.pegasys.teku.spec.logic.versions.deneb.block.KzgCommitmentsProcessor;

public class TransitionTestExecutor implements TestExecutor {
Expand Down Expand Up @@ -102,12 +101,7 @@ private void processUpgrade(final TestDefinition testDefinition, final MetaData
metadata.blsSetting == 2 ? BLSSignatureVerifier.NO_OP : BLSSignatureVerifier.SIMPLE;
result =
spec.processBlock(
result,
block,
signatureVerifier,
Optional.empty(),
KzgCommitmentsProcessor.NOOP,
BlobsSidecarAvailabilityChecker.NOOP);
result, block, signatureVerifier, Optional.empty(), KzgCommitmentsProcessor.NOOP);
} catch (final StateTransitionException e) {
Assertions.fail(
"Failed to process block " + i + " at slot " + block.getSlot() + ": " + e.getMessage(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;
import tech.pegasys.teku.spec.logic.common.statetransition.exceptions.StateTransitionException;
import tech.pegasys.teku.spec.logic.versions.deneb.blobs.BlobsSidecarAvailabilityChecker;
import tech.pegasys.teku.spec.logic.versions.deneb.block.KzgCommitmentsProcessor;
import tech.pegasys.teku.spec.logic.versions.deneb.helpers.MiscHelpersDeneb;

Expand Down Expand Up @@ -125,8 +124,7 @@ private BeaconState applyBlocks(
? BLSSignatureVerifier.NO_OP
: BLSSignatureVerifier.SIMPLE,
Optional.empty(),
kzgCommitmentsProcessor,
BlobsSidecarAvailabilityChecker.NOOP);
kzgCommitmentsProcessor);
}
return result;
} catch (StateTransitionException e) {
Expand Down
10 changes: 3 additions & 7 deletions ethereum/spec/src/main/java/tech/pegasys/teku/spec/Spec.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@
import tech.pegasys.teku.spec.logic.common.util.LightClientUtil;
import tech.pegasys.teku.spec.logic.common.util.SyncCommitteeUtil;
import tech.pegasys.teku.spec.logic.versions.bellatrix.block.OptimisticExecutionPayloadExecutor;
import tech.pegasys.teku.spec.logic.versions.deneb.blobs.BlobsSidecarAvailabilityChecker;
import tech.pegasys.teku.spec.logic.versions.deneb.block.KzgCommitmentsProcessor;
import tech.pegasys.teku.spec.schemas.SchemaDefinitions;

Expand Down Expand Up @@ -701,8 +700,7 @@ public BeaconState processBlock(
final SignedBeaconBlock block,
final BLSSignatureVerifier signatureVerifier,
final Optional<OptimisticExecutionPayloadExecutor> payloadExecutor,
final KzgCommitmentsProcessor kzgCommitmentsProcessor,
final BlobsSidecarAvailabilityChecker blobsSidecarAvailabilityChecker)
final KzgCommitmentsProcessor kzgCommitmentsProcessor)
throws StateTransitionException {
try {
final BeaconState blockSlotState = stateTransition.processSlots(preState, block.getSlot());
Expand All @@ -713,8 +711,7 @@ public BeaconState processBlock(
IndexedAttestationCache.NOOP,
signatureVerifier,
payloadExecutor,
kzgCommitmentsProcessor,
blobsSidecarAvailabilityChecker);
kzgCommitmentsProcessor);
} catch (SlotProcessingException | EpochProcessingException e) {
throw new StateTransitionException(e);
}
Expand All @@ -731,8 +728,7 @@ public BeaconState replayValidatedBlock(final BeaconState preState, final Signed
IndexedAttestationCache.NOOP,
BLSSignatureVerifier.NO_OP,
Optional.empty(),
KzgCommitmentsProcessor.NOOP,
BlobsSidecarAvailabilityChecker.NOOP);
KzgCommitmentsProcessor.NOOP);
} catch (SlotProcessingException | EpochProcessingException | BlockProcessingException e) {
throw new StateTransitionException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
import tech.pegasys.teku.spec.logic.common.util.BeaconStateUtil;
import tech.pegasys.teku.spec.logic.common.util.ValidatorsUtil;
import tech.pegasys.teku.spec.logic.versions.bellatrix.block.OptimisticExecutionPayloadExecutor;
import tech.pegasys.teku.spec.logic.versions.deneb.blobs.BlobsSidecarAvailabilityChecker;
import tech.pegasys.teku.spec.logic.versions.deneb.block.KzgCommitmentsProcessor;

public abstract class AbstractBlockProcessor implements BlockProcessor {
Expand Down Expand Up @@ -135,8 +134,7 @@ public BeaconState processAndValidateBlock(
final BeaconState blockSlotState,
final IndexedAttestationCache indexedAttestationCache,
final Optional<? extends OptimisticExecutionPayloadExecutor> payloadExecutor,
final KzgCommitmentsProcessor kzgCommitmentsProcessor,
final BlobsSidecarAvailabilityChecker blobsSidecarAvailabilityChecker)
final KzgCommitmentsProcessor kzgCommitmentsProcessor)
throws StateTransitionException {
final BatchSignatureVerifier signatureVerifier = new BatchSignatureVerifier();
final BeaconState result =
Expand All @@ -146,8 +144,7 @@ public BeaconState processAndValidateBlock(
indexedAttestationCache,
signatureVerifier,
payloadExecutor,
kzgCommitmentsProcessor,
blobsSidecarAvailabilityChecker);
kzgCommitmentsProcessor);
if (!signatureVerifier.batchVerify()) {
throw new StateTransitionException(
"Batch signature verification failed for block " + signedBlock.toLogString());
Expand All @@ -162,8 +159,7 @@ public BeaconState processAndValidateBlock(
final IndexedAttestationCache indexedAttestationCache,
final BLSSignatureVerifier signatureVerifier,
final Optional<? extends OptimisticExecutionPayloadExecutor> payloadExecutor,
final KzgCommitmentsProcessor kzgCommitmentsProcessor,
final BlobsSidecarAvailabilityChecker blobsSidecarAvailabilityChecker)
final KzgCommitmentsProcessor kzgCommitmentsProcessor)
throws StateTransitionException {
try {
final BlockValidationResult preValidationResult =
Expand All @@ -180,8 +176,7 @@ public BeaconState processAndValidateBlock(
indexedAttestationCache,
signatureVerifier,
payloadExecutor,
kzgCommitmentsProcessor,
blobsSidecarAvailabilityChecker);
kzgCommitmentsProcessor);

BlockValidationResult blockValidationResult =
validateBlockPostProcessing(
Expand Down Expand Up @@ -326,8 +321,7 @@ public BeaconState processUnsignedBlock(
final IndexedAttestationCache indexedAttestationCache,
final BLSSignatureVerifier signatureVerifier,
final Optional<? extends OptimisticExecutionPayloadExecutor> payloadExecutor,
final KzgCommitmentsProcessor kzgCommitmentsProcessor,
final BlobsSidecarAvailabilityChecker blobsSidecarAvailabilityChecker)
final KzgCommitmentsProcessor kzgCommitmentsProcessor)
throws BlockProcessingException {
return preState.updated(
state ->
Expand All @@ -337,8 +331,7 @@ public BeaconState processUnsignedBlock(
indexedAttestationCache,
signatureVerifier,
payloadExecutor,
kzgCommitmentsProcessor,
blobsSidecarAvailabilityChecker));
kzgCommitmentsProcessor));
}

protected void processBlock(
Expand All @@ -347,8 +340,7 @@ protected void processBlock(
final IndexedAttestationCache indexedAttestationCache,
final BLSSignatureVerifier signatureVerifier,
final Optional<? extends OptimisticExecutionPayloadExecutor> payloadExecutor,
final KzgCommitmentsProcessor kzgCommitmentsProcessor,
final BlobsSidecarAvailabilityChecker blobsSidecarAvailabilityChecker)
final KzgCommitmentsProcessor kzgCommitmentsProcessor)
throws BlockProcessingException {
processBlockHeader(state, block);
processRandaoNoValidation(state, block.getBody());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import tech.pegasys.teku.spec.logic.common.statetransition.exceptions.BlockProcessingException;
import tech.pegasys.teku.spec.logic.common.statetransition.exceptions.StateTransitionException;
import tech.pegasys.teku.spec.logic.versions.bellatrix.block.OptimisticExecutionPayloadExecutor;
import tech.pegasys.teku.spec.logic.versions.deneb.blobs.BlobsSidecarAvailabilityChecker;
import tech.pegasys.teku.spec.logic.versions.deneb.block.KzgCommitmentsProcessor;

public interface BlockProcessor {
Expand All @@ -59,8 +58,7 @@ BeaconState processAndValidateBlock(
BeaconState blockSlotState,
IndexedAttestationCache indexedAttestationCache,
Optional<? extends OptimisticExecutionPayloadExecutor> payloadExecutor,
KzgCommitmentsProcessor kzgCommitmentsProcessor,
BlobsSidecarAvailabilityChecker blobsSidecarAvailabilityChecker)
KzgCommitmentsProcessor kzgCommitmentsProcessor)
throws StateTransitionException;

/**
Expand All @@ -81,8 +79,7 @@ BeaconState processAndValidateBlock(
IndexedAttestationCache indexedAttestationCache,
BLSSignatureVerifier signatureVerifier,
Optional<? extends OptimisticExecutionPayloadExecutor> payloadExecutor,
KzgCommitmentsProcessor kzgCommitmentsProcessor,
BlobsSidecarAvailabilityChecker blobsSidecarAvailabilityChecker)
KzgCommitmentsProcessor kzgCommitmentsProcessor)
throws StateTransitionException;

BeaconState processUnsignedBlock(
Expand All @@ -91,8 +88,7 @@ BeaconState processUnsignedBlock(
IndexedAttestationCache indexedAttestationCache,
BLSSignatureVerifier signatureVerifier,
Optional<? extends OptimisticExecutionPayloadExecutor> payloadExecutor,
KzgCommitmentsProcessor kzgCommitmentsProcessor,
BlobsSidecarAvailabilityChecker blobsSidecarAvailabilityChecker)
KzgCommitmentsProcessor kzgCommitmentsProcessor)
throws BlockProcessingException;

void processBlockHeader(MutableBeaconState state, BeaconBlockSummary blockHeader)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import tech.pegasys.teku.spec.logic.common.block.BlockProcessor;
import tech.pegasys.teku.spec.logic.common.statetransition.exceptions.BlockProcessingException;
import tech.pegasys.teku.spec.logic.common.statetransition.exceptions.StateTransitionException;
import tech.pegasys.teku.spec.logic.versions.deneb.blobs.BlobsSidecarAvailabilityChecker;
import tech.pegasys.teku.spec.logic.versions.deneb.block.KzgCommitmentsProcessor;
import tech.pegasys.teku.spec.schemas.SchemaDefinitions;

Expand Down Expand Up @@ -97,8 +96,7 @@ public SafeFuture<BeaconBlockAndState> createNewUnsignedBlock(
IndexedAttestationCache.NOOP,
BLSSignatureVerifier.NO_OP,
Optional.empty(),
KzgCommitmentsProcessor.NOOP,
BlobsSidecarAvailabilityChecker.NOOP);
KzgCommitmentsProcessor.NOOP);

final Bytes32 stateRoot = newState.hashTreeRoot();
final BeaconBlock newCompleteBlock = block.withStateRoot(stateRoot);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
import tech.pegasys.teku.spec.logic.versions.altair.helpers.BeaconStateAccessorsAltair;
import tech.pegasys.teku.spec.logic.versions.altair.helpers.MiscHelpersAltair;
import tech.pegasys.teku.spec.logic.versions.bellatrix.block.OptimisticExecutionPayloadExecutor;
import tech.pegasys.teku.spec.logic.versions.deneb.blobs.BlobsSidecarAvailabilityChecker;
import tech.pegasys.teku.spec.logic.versions.deneb.block.KzgCommitmentsProcessor;

public class BlockProcessorAltair extends AbstractBlockProcessor {
Expand Down Expand Up @@ -113,8 +112,7 @@ public void processBlock(
final IndexedAttestationCache indexedAttestationCache,
final BLSSignatureVerifier signatureVerifier,
final Optional<? extends OptimisticExecutionPayloadExecutor> payloadExecutor,
final KzgCommitmentsProcessor kzgCommitmentsProcessor,
final BlobsSidecarAvailabilityChecker blobsSidecarAvailabilityChecker)
final KzgCommitmentsProcessor kzgCommitmentsProcessor)
throws BlockProcessingException {
final MutableBeaconStateAltair state = MutableBeaconStateAltair.required(genericState);
final BeaconBlockBodyAltair blockBody = BeaconBlockBodyAltair.required(block.getBody());
Expand All @@ -125,8 +123,7 @@ public void processBlock(
indexedAttestationCache,
signatureVerifier,
payloadExecutor,
kzgCommitmentsProcessor,
blobsSidecarAvailabilityChecker);
kzgCommitmentsProcessor);
processSyncAggregate(state, blockBody.getSyncAggregate(), signatureVerifier);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import tech.pegasys.teku.spec.logic.versions.altair.block.BlockProcessorAltair;
import tech.pegasys.teku.spec.logic.versions.altair.helpers.BeaconStateAccessorsAltair;
import tech.pegasys.teku.spec.logic.versions.bellatrix.helpers.MiscHelpersBellatrix;
import tech.pegasys.teku.spec.logic.versions.deneb.blobs.BlobsSidecarAvailabilityChecker;
import tech.pegasys.teku.spec.logic.versions.deneb.block.KzgCommitmentsProcessor;
import tech.pegasys.teku.spec.schemas.SchemaDefinitionsBellatrix;

Expand Down Expand Up @@ -86,8 +85,7 @@ public void processBlock(
final IndexedAttestationCache indexedAttestationCache,
final BLSSignatureVerifier signatureVerifier,
final Optional<? extends OptimisticExecutionPayloadExecutor> payloadExecutor,
final KzgCommitmentsProcessor kzgCommitmentsProcessor,
final BlobsSidecarAvailabilityChecker blobsSidecarAvailabilityChecker)
final KzgCommitmentsProcessor kzgCommitmentsProcessor)
throws BlockProcessingException {
final MutableBeaconStateBellatrix state = MutableBeaconStateBellatrix.required(genericState);
final BeaconBlockBody blockBody = block.getBody();
Expand Down
Loading

0 comments on commit 5795062

Please sign in to comment.