Skip to content

Commit

Permalink
Consolidate interfaces related to BlockV3, moving from deprecated met…
Browse files Browse the repository at this point in the history
…hod to Optional<Boolean> blinded (Consensys#7769)
  • Loading branch information
tbenr authored Nov 30, 2023
1 parent b778253 commit aedb9d7
Show file tree
Hide file tree
Showing 26 changed files with 99 additions and 257 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,12 @@

public interface BlockFactory {

@Deprecated
SafeFuture<BlockContainer> createUnsignedBlock(
BeaconState blockSlotState,
UInt64 newSlot,
BLSSignature randaoReveal,
Optional<Bytes32> optionalGraffiti,
boolean blinded);

SafeFuture<BlockContainer> createUnsignedBlock(
BeaconState blockSlotState,
UInt64 newSlot,
BLSSignature randaoReveal,
Optional<Bytes32> optionalGraffiti);
final Optional<Boolean> blinded);

SafeFuture<SignedBeaconBlock> unblindSignedBlockIfBlinded(SignedBeaconBlock maybeBlindedBlock);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,13 @@ public BlockFactoryDeneb(final Spec spec, final BlockOperationSelectorFactory op
spec.forMilestone(SpecMilestone.DENEB).getSchemaDefinitions());
}

@Deprecated
@Override
public SafeFuture<BlockContainer> createUnsignedBlock(
final BeaconState blockSlotState,
final UInt64 newSlot,
final BLSSignature randaoReveal,
final Optional<Bytes32> optionalGraffiti,
final boolean blinded) {
final Optional<Boolean> blinded) {
return super.createUnsignedBlock(
blockSlotState, newSlot, randaoReveal, optionalGraffiti, blinded)
.thenApply(BlockContainer::getBlock)
Expand All @@ -65,27 +64,6 @@ public SafeFuture<BlockContainer> createUnsignedBlock(
});
}

@Override
public SafeFuture<BlockContainer> createUnsignedBlock(
final BeaconState blockSlotState,
final UInt64 newSlot,
final BLSSignature randaoReveal,
final Optional<Bytes32> optionalGraffiti) {
return super.createUnsignedBlock(blockSlotState, newSlot, randaoReveal, optionalGraffiti)
.thenApply(BlockContainer::getBlock)
.thenCompose(
block -> {
if (block.isBlinded()) {
return SafeFuture.completedFuture(block);
}
// The execution BlobsBundle has been cached as part of the block creation
return operationSelector
.createBlobsBundleSelector()
.apply(block)
.thenApply(blobsBundle -> createBlockContents(block, blobsBundle));
});
}

@Override
public List<BlobSidecar> createBlobSidecars(final SignedBlockContainer blockContainer) {
return operationSelector.createBlobSidecarsSelector().apply(blockContainer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,13 @@ public BlockFactoryPhase0(
this.operationSelector = operationSelector;
}

@Deprecated
@Override
public SafeFuture<BlockContainer> createUnsignedBlock(
final BeaconState blockSlotState,
final UInt64 newSlot,
final BLSSignature randaoReveal,
final Optional<Bytes32> optionalGraffiti,
final boolean blinded) {
final Optional<Boolean> blinded) {
checkArgument(
blockSlotState.getSlot().equals(newSlot),
"Block slot state for slot %s but should be for slot %s",
Expand All @@ -71,33 +70,6 @@ public SafeFuture<BlockContainer> createUnsignedBlock(
.thenApply(BeaconBlockAndState::getBlock);
}

@Override
public SafeFuture<BlockContainer> createUnsignedBlock(
final BeaconState blockSlotState,
final UInt64 newSlot,
final BLSSignature randaoReveal,
final Optional<Bytes32> optionalGraffiti) {
checkArgument(
blockSlotState.getSlot().equals(newSlot),
"Block slot state for slot %s but should be for slot %s",
blockSlotState.getSlot(),
newSlot);

// Process empty slots up to the one before the new block slot
final UInt64 slotBeforeBlock = newSlot.minus(UInt64.ONE);

final Bytes32 parentRoot = spec.getBlockRootAtSlot(blockSlotState, slotBeforeBlock);

return spec.createNewUnsignedBlock(
newSlot,
spec.getBeaconProposerIndex(blockSlotState, newSlot),
blockSlotState,
parentRoot,
operationSelector.createSelector(
parentRoot, blockSlotState, randaoReveal, optionalGraffiti))
.thenApply(BeaconBlockAndState::getBlock);
}

@Override
public SafeFuture<SignedBeaconBlock> unblindSignedBlockIfBlinded(
final SignedBeaconBlock maybeBlindedBlock) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,19 @@ public MilestoneBasedBlockFactory(
});
}

@Deprecated
@Override
public SafeFuture<BlockContainer> createUnsignedBlock(
final BeaconState blockSlotState,
final UInt64 newSlot,
final BLSSignature randaoReveal,
final Optional<Bytes32> optionalGraffiti,
final boolean blinded) {
final Optional<Boolean> blinded) {
final SpecMilestone milestone = getMilestone(newSlot);
return registeredFactories
.get(milestone)
.createUnsignedBlock(blockSlotState, newSlot, randaoReveal, optionalGraffiti, blinded);
}

@Override
public SafeFuture<BlockContainer> createUnsignedBlock(
final BeaconState blockSlotState,
final UInt64 newSlot,
final BLSSignature randaoReveal,
final Optional<Bytes32> optionalGraffiti) {
final SpecMilestone milestone = getMilestone(newSlot);
return registeredFactories
.get(milestone)
.createUnsignedBlock(blockSlotState, newSlot, randaoReveal, optionalGraffiti);
}

@Override
public SafeFuture<SignedBeaconBlock> unblindSignedBlockIfBlinded(
final SignedBeaconBlock maybeBlindedBlock) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,12 @@ public SafeFuture<Optional<Map<BLSPublicKey, ValidatorStatus>>> getValidatorStat
StateValidatorData::getStatus))));
}

@Deprecated
@Override
public SafeFuture<Optional<BlockContainer>> createUnsignedBlock(
final UInt64 slot,
final BLSSignature randaoReveal,
final Optional<Bytes32> graffiti,
final boolean blinded) {
final Optional<Boolean> blinded) {
LOG.info("Creating unsigned block for slot {}", slot);
performanceTracker.reportBlockProductionAttempt(spec.computeEpochAtSlot(slot));
if (isSyncActive()) {
Expand All @@ -320,25 +319,11 @@ public SafeFuture<Optional<BlockContainer>> createUnsignedBlock(
blockSlotState -> createBlock(slot, randaoReveal, graffiti, blinded, blockSlotState));
}

@Override
public SafeFuture<Optional<BlockContainer>> createUnsignedBlock(
final UInt64 slot, final BLSSignature randaoReveal, final Optional<Bytes32> graffiti) {
LOG.info("Creating unsigned block for slot {}", slot);
performanceTracker.reportBlockProductionAttempt(spec.computeEpochAtSlot(slot));
if (isSyncActive()) {
return NodeSyncingException.failedFuture();
}
return forkChoiceTrigger
.prepareForBlockProduction(slot)
.thenCompose(__ -> combinedChainDataClient.getStateAtSlotExact(slot))
.thenCompose(blockSlotState -> createBlock(slot, randaoReveal, graffiti, blockSlotState));
}

private SafeFuture<Optional<BlockContainer>> createBlock(
final UInt64 slot,
final BLSSignature randaoReveal,
final Optional<Bytes32> graffiti,
final boolean blinded,
final Optional<Boolean> blinded,
final Optional<BeaconState> maybeBlockSlotState) {
if (maybeBlockSlotState.isEmpty()) {
return SafeFuture.completedFuture(Optional.empty());
Expand All @@ -356,27 +341,6 @@ private SafeFuture<Optional<BlockContainer>> createBlock(
.thenApply(Optional::of);
}

private SafeFuture<Optional<BlockContainer>> createBlock(
final UInt64 slot,
final BLSSignature randaoReveal,
final Optional<Bytes32> graffiti,
final Optional<BeaconState> maybeBlockSlotState) {
if (maybeBlockSlotState.isEmpty()) {
return SafeFuture.completedFuture(Optional.empty());
}
final BeaconState blockSlotState = maybeBlockSlotState.get();
final Bytes32 parentRoot = spec.getBlockRootAtSlot(blockSlotState, slot.minus(1));
if (combinedChainDataClient.isOptimisticBlock(parentRoot)) {
LOG.warn(
"Unable to produce block at slot {} because parent has optimistically validated payload",
slot);
throw new NodeSyncingException();
}
return blockFactory
.createUnsignedBlock(blockSlotState, slot, randaoReveal, graffiti)
.thenApply(Optional::of);
}

@Override
public SafeFuture<Optional<AttestationData>> createAttestationData(
final UInt64 slot, final int committeeIndex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ protected BlockContainer assertBlockCreated(
final BlockContainer blockContainer =
safeJoin(
blockFactory.createUnsignedBlock(
blockSlotState, newSlot, randaoReveal, Optional.empty(), blinded));
blockSlotState, newSlot, randaoReveal, Optional.empty(), Optional.of(blinded)));

final BeaconBlock block = blockContainer.getBlock();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ public void createUnsignedBlock_shouldFailWhenNodeIsSyncing() {
nodeIsSyncing();
final SafeFuture<Optional<BlockContainer>> result =
validatorApiHandler.createUnsignedBlock(
ONE, dataStructureUtil.randomSignature(), Optional.empty(), false);
ONE, dataStructureUtil.randomSignature(), Optional.empty(), Optional.of(false));

assertThat(result).isCompletedExceptionally();
assertThatThrownBy(result::get).hasRootCauseInstanceOf(NodeSyncingException.class);
Expand All @@ -498,7 +498,7 @@ public void createUnsignedBlock_shouldFailWhenParentBlockIsOptimistic() {

final SafeFuture<Optional<BlockContainer>> result =
validatorApiHandler.createUnsignedBlock(
newSlot, dataStructureUtil.randomSignature(), Optional.empty(), false);
newSlot, dataStructureUtil.randomSignature(), Optional.empty(), Optional.of(false));

assertThat(result).isCompletedExceptionally();
assertThatThrownBy(result::get).hasRootCauseInstanceOf(NodeSyncingException.class);
Expand All @@ -515,14 +515,16 @@ public void createUnsignedBlock_shouldCreateBlock() {
when(chainDataClient.getStateAtSlotExact(newSlot))
.thenReturn(SafeFuture.completedFuture(Optional.of(blockSlotState)));
when(blockFactory.createUnsignedBlock(
blockSlotState, newSlot, randaoReveal, Optional.empty(), false))
blockSlotState, newSlot, randaoReveal, Optional.empty(), Optional.of(false)))
.thenReturn(SafeFuture.completedFuture(createdBlock));

final SafeFuture<Optional<BlockContainer>> result =
validatorApiHandler.createUnsignedBlock(newSlot, randaoReveal, Optional.empty(), false);
validatorApiHandler.createUnsignedBlock(
newSlot, randaoReveal, Optional.empty(), Optional.of(false));

verify(blockFactory)
.createUnsignedBlock(blockSlotState, newSlot, randaoReveal, Optional.empty(), false);
.createUnsignedBlock(
blockSlotState, newSlot, randaoReveal, Optional.empty(), Optional.of(false));
assertThat(result).isCompletedWithValue(Optional.of(createdBlock));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void setup() {
void shouldGetUnsignedBlock_asJson(final String route, final boolean isBlindedBlock)
throws IOException {
when(validatorApiChannel.createUnsignedBlock(
eq(UInt64.ONE), eq(signature), any(), eq(isBlindedBlock)))
eq(UInt64.ONE), eq(signature), any(), eq(Optional.of(isBlindedBlock))))
.thenReturn(SafeFuture.completedFuture(Optional.of(randomBlock)));
Response response = get(route, signature, ContentTypes.JSON);
assertThat(response.code()).isEqualTo(SC_OK);
Expand All @@ -88,7 +88,7 @@ void shouldGetUnsignedBlock_asJson(final String route, final boolean isBlindedBl
void shouldGetUnsignedBlock_asOctet(final String route, final boolean isBlindedBlock)
throws IOException {
when(validatorApiChannel.createUnsignedBlock(
eq(UInt64.ONE), eq(signature), any(), eq(isBlindedBlock)))
eq(UInt64.ONE), eq(signature), any(), eq(Optional.of(isBlindedBlock))))
.thenReturn(SafeFuture.completedFuture(Optional.of(randomBlock)));
Response response = get(route, signature, ContentTypes.OCTET_STREAM);
assertThat(response.code()).isEqualTo(SC_OK);
Expand All @@ -100,7 +100,7 @@ void shouldGetUnsignedBlock_asOctet(final String route, final boolean isBlindedB
@MethodSource("getNewBlockCases")
void shouldShowNoContent(final String route, final boolean isBlindedBlock) throws IOException {
when(validatorApiChannel.createUnsignedBlock(
eq(UInt64.ONE), eq(signature), any(), eq(isBlindedBlock)))
eq(UInt64.ONE), eq(signature), any(), eq(Optional.of(isBlindedBlock))))
.thenReturn(SafeFuture.failedFuture(new ChainDataUnavailableException()));
Response response = get(route, signature, ContentTypes.OCTET_STREAM);
assertThat(response.code()).isEqualTo(SC_NO_CONTENT);
Expand All @@ -111,7 +111,7 @@ void shouldShowNoContent(final String route, final boolean isBlindedBlock) throw
@MethodSource("getNewBlockCases")
void shouldShowUnavailable(final String route, final boolean isBlindedBlock) throws IOException {
when(validatorApiChannel.createUnsignedBlock(
eq(UInt64.ONE), eq(signature), any(), eq(isBlindedBlock)))
eq(UInt64.ONE), eq(signature), any(), eq(Optional.of(isBlindedBlock))))
.thenReturn(SafeFuture.failedFuture(new ServiceUnavailableException()));
Response response = get(route, signature, ContentTypes.OCTET_STREAM);
assertThat(response.code()).isEqualTo(SC_SERVICE_UNAVAILABLE);
Expand All @@ -125,7 +125,7 @@ void shouldShowUnavailable(final String route, final boolean isBlindedBlock) thr
void shouldNotStackTraceForMissingDeposits(final String route, final boolean isBlindedBlock)
throws IOException {
when(validatorApiChannel.createUnsignedBlock(
eq(UInt64.ONE), eq(signature), any(), eq(isBlindedBlock)))
eq(UInt64.ONE), eq(signature), any(), eq(Optional.of(isBlindedBlock))))
.thenReturn(
SafeFuture.failedFuture(
MissingDepositsException.missingRange(UInt64.valueOf(1), UInt64.valueOf(10))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void shouldGetUnBlindedBeaconBlockAsJson() throws IOException {
assumeThat(specMilestone).isLessThan(DENEB);
final BeaconBlock beaconBlock = dataStructureUtil.randomBeaconBlock(ONE);
final BLSSignature signature = beaconBlock.getBlock().getBody().getRandaoReveal();
when(validatorApiChannel.createUnsignedBlock(eq(UInt64.ONE), eq(signature), any()))
when(validatorApiChannel.createUnsignedBlock(eq(UInt64.ONE), eq(signature), any(), any()))
.thenReturn(SafeFuture.completedFuture(Optional.of(beaconBlock)));
Response response = get(signature, ContentTypes.JSON);
assertResponseWithHeaders(response, false);
Expand All @@ -106,7 +106,7 @@ void shouldGetUnblindedBeaconBlockAsSsz() throws IOException {
assumeThat(specMilestone).isLessThan(DENEB);
final BeaconBlock beaconBlock = dataStructureUtil.randomBeaconBlock(ONE);
final BLSSignature signature = beaconBlock.getBlock().getBody().getRandaoReveal();
when(validatorApiChannel.createUnsignedBlock(eq(UInt64.ONE), eq(signature), any()))
when(validatorApiChannel.createUnsignedBlock(eq(UInt64.ONE), eq(signature), any(), any()))
.thenReturn(SafeFuture.completedFuture(Optional.of(beaconBlock)));
Response response = get(signature, ContentTypes.OCTET_STREAM);
assertResponseWithHeaders(response, false);
Expand All @@ -122,7 +122,7 @@ void shouldGetBlindedBeaconBlockAsJson() throws IOException {
assumeThat(specMilestone).isGreaterThanOrEqualTo(BELLATRIX);
final BeaconBlock blindedBeaconBlock = dataStructureUtil.randomBlindedBeaconBlock(ONE);
final BLSSignature signature = blindedBeaconBlock.getBlock().getBody().getRandaoReveal();
when(validatorApiChannel.createUnsignedBlock(eq(UInt64.ONE), eq(signature), any()))
when(validatorApiChannel.createUnsignedBlock(eq(UInt64.ONE), eq(signature), any(), any()))
.thenReturn(SafeFuture.completedFuture(Optional.of(blindedBeaconBlock)));
Response response = get(signature, ContentTypes.JSON);
assertResponseWithHeaders(response, true);
Expand All @@ -135,7 +135,7 @@ void shouldGetBlindedBeaconBlockAsSsz() throws IOException {
assumeThat(specMilestone).isGreaterThanOrEqualTo(BELLATRIX);
final BeaconBlock blindedBeaconBlock = dataStructureUtil.randomBlindedBeaconBlock(ONE);
final BLSSignature signature = blindedBeaconBlock.getBlock().getBody().getRandaoReveal();
when(validatorApiChannel.createUnsignedBlock(eq(UInt64.ONE), eq(signature), any()))
when(validatorApiChannel.createUnsignedBlock(eq(UInt64.ONE), eq(signature), any(), any()))
.thenReturn(SafeFuture.completedFuture(Optional.of(blindedBeaconBlock)));
Response response = get(signature, ContentTypes.OCTET_STREAM);
assertResponseWithHeaders(response, true);
Expand All @@ -151,7 +151,7 @@ void shouldGetUnBlindedBlockContentPostDenebAsJson() throws IOException {
assumeThat(specMilestone).isEqualTo(DENEB);
final BlockContents blockContents = dataStructureUtil.randomBlockContents(ONE);
final BLSSignature signature = blockContents.getBlock().getBody().getRandaoReveal();
when(validatorApiChannel.createUnsignedBlock(eq(UInt64.ONE), eq(signature), any()))
when(validatorApiChannel.createUnsignedBlock(eq(UInt64.ONE), eq(signature), any(), any()))
.thenReturn(SafeFuture.completedFuture(Optional.of(blockContents)));
Response response = get(signature, ContentTypes.JSON);
assertResponseWithHeaders(response, false);
Expand All @@ -164,7 +164,7 @@ void shouldGetUnBlindedBlockContentPostDenebAsSsz() throws IOException {
assumeThat(specMilestone).isEqualTo(DENEB);
final BlockContents blockContents = dataStructureUtil.randomBlockContents(ONE);
final BLSSignature signature = blockContents.getBlock().getBody().getRandaoReveal();
when(validatorApiChannel.createUnsignedBlock(eq(UInt64.ONE), eq(signature), any()))
when(validatorApiChannel.createUnsignedBlock(eq(UInt64.ONE), eq(signature), any(), any()))
.thenReturn(SafeFuture.completedFuture(Optional.of(blockContents)));
Response response = get(signature, ContentTypes.OCTET_STREAM);
assertResponseWithHeaders(response, false);
Expand All @@ -180,7 +180,7 @@ void shouldGetUnBlindedBlockContentPostDenebAsSsz() throws IOException {
void shouldFailWhenNoBlockProduced() throws IOException {
final BeaconBlock beaconBlock = dataStructureUtil.randomBeaconBlock(ONE);
final BLSSignature signature = beaconBlock.getBlock().getBody().getRandaoReveal();
when(validatorApiChannel.createUnsignedBlock(eq(UInt64.ONE), eq(signature), any()))
when(validatorApiChannel.createUnsignedBlock(eq(UInt64.ONE), eq(signature), any(), any()))
.thenReturn(SafeFuture.completedFuture(Optional.empty()));
when(executionLayerBlockProductionManager.getCachedPayloadResult(UInt64.ONE))
.thenReturn(
Expand Down
Loading

0 comments on commit aedb9d7

Please sign in to comment.