Skip to content

Commit

Permalink
Refactor AbstractBlockPublisher to simplify blob sidecars publishing
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBratanov committed Nov 15, 2023
1 parent 708788f commit b350aba
Show file tree
Hide file tree
Showing 26 changed files with 211 additions and 252 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,10 @@ void shouldImportBlobSidecarsAndBlocksInOrder() {
final SignedBeaconBlock block1 = dataStructureUtil.randomSignedBeaconBlock(1);
final SignedBeaconBlock block2 = dataStructureUtil.randomSignedBeaconBlock(2);

final List<BlobSidecarOld> blobSidecars1 = dataStructureUtil.randomBlobSidecarsForBlock(block1);
final List<BlobSidecarOld> blobSidecars2 = dataStructureUtil.randomBlobSidecarsForBlock(block2);
final List<BlobSidecarOld> blobSidecars1 =
dataStructureUtil.randomBlobSidecarsForBlockOld(block1);
final List<BlobSidecarOld> blobSidecars2 =
dataStructureUtil.randomBlobSidecarsForBlockOld(block2);

final SafeFuture<BlockImportResult> importResult1 = new SafeFuture<>();
final SafeFuture<BlockImportResult> importResult2 = new SafeFuture<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ void requestMoreBlocks_shouldRequestBlobSidecarsWhenRequired() {

// only receiving last block (70 + 50 - 1)
final SignedBeaconBlock block = dataStructureUtil.randomSignedBeaconBlock(119);
final List<BlobSidecarOld> blobSidecars = dataStructureUtil.randomBlobSidecarsForBlock(block);
final List<BlobSidecarOld> blobSidecars =
dataStructureUtil.randomBlobSidecarsForBlockOld(block);

receiveBlocks(batch, block);
receiveBlobSidecars(batch, blobSidecars);
Expand Down Expand Up @@ -241,7 +242,7 @@ void shouldReportAsInvalidWhenUnexpectedNumberOfBlobSidecarsWereReceived() {
final SignedBeaconBlock block = dataStructureUtil.randomSignedBeaconBlock(19);

final List<BlobSidecarOld> blobSidecars =
new ArrayList<>(dataStructureUtil.randomBlobSidecarsForBlock(block));
new ArrayList<>(dataStructureUtil.randomBlobSidecarsForBlockOld(block));
// receiving more sidecars than expected
blobSidecars.add(
dataStructureUtil.createRandomBlobSidecarBuilderOld().blockRoot(block.getRoot()).build());
Expand Down Expand Up @@ -294,11 +295,12 @@ void shouldMarkBatchAsInconsistentWhenUnexpectedBlobSidecarsWithRootsWereReceive

final SignedBeaconBlock block = dataStructureUtil.randomSignedBeaconBlock(19);

final List<BlobSidecarOld> blobSidecars = dataStructureUtil.randomBlobSidecarsForBlock(block);
final List<BlobSidecarOld> blobSidecars =
dataStructureUtil.randomBlobSidecarsForBlockOld(block);
final List<BlobSidecarOld> unexpectedBlobSidecars = new ArrayList<>(blobSidecars);
// receiving sidecars with unknown roots
unexpectedBlobSidecars.addAll(
dataStructureUtil.randomBlobSidecarsForBlock(
dataStructureUtil.randomBlobSidecarsForBlockOld(
dataStructureUtil.randomSignedBeaconBlock(18)));

receiveBlocks(batch, block);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import tech.pegasys.teku.infrastructure.async.SafeFuture;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.datastructures.blocks.BlockContainer;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBlockContainer;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;

public interface BlockFactory {
Expand All @@ -38,6 +38,5 @@ SafeFuture<BlockContainer> createUnsignedBlock(
BLSSignature randaoReveal,
Optional<Bytes32> optionalGraffiti);

SafeFuture<SignedBlockContainer> unblindSignedBlockIfBlinded(
SignedBlockContainer maybeBlindedBlockContainer);
SafeFuture<SignedBeaconBlock> unblindSignedBlockIfBlinded(SignedBeaconBlock maybeBlindedBlock);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.bls.BLSSignature;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
Expand All @@ -27,12 +26,8 @@
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.Blob;
import tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock;
import tech.pegasys.teku.spec.datastructures.blocks.BlockContainer;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBlockContainer;
import tech.pegasys.teku.spec.datastructures.blocks.versions.deneb.BlockContents;
import tech.pegasys.teku.spec.datastructures.blocks.versions.deneb.SignedBlockContents;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;
import tech.pegasys.teku.spec.executionlayer.ExecutionLayerChannel;
import tech.pegasys.teku.spec.schemas.SchemaDefinitionsDeneb;

public class BlockFactoryDeneb extends BlockFactoryPhase0 {
Expand Down Expand Up @@ -97,37 +92,8 @@ public SafeFuture<BlockContainer> createUnsignedBlock(
});
}

/**
* Adding blobs and proofs after the block in order to use the cached value from the {@link
* ExecutionLayerChannel#builderGetPayload( SignedBlockContainer, Function)} call
*/
@Override
public SafeFuture<SignedBlockContainer> unblindSignedBlockIfBlinded(
final SignedBlockContainer maybeBlindedBlockContainer) {
if (maybeBlindedBlockContainer.isBlinded()) {
return unblindBlock(maybeBlindedBlockContainer)
.thenApply(this::createUnblindedSignedBlockContents);
}
return SafeFuture.completedFuture(maybeBlindedBlockContainer);
}

private BlockContents createBlockContents(
final BeaconBlock block, final List<Blob> blobs, final List<KZGProof> kzgProofs) {
return schemaDefinitionsDeneb.getBlockContentsSchema().create(block, kzgProofs, blobs);
}

/** use {@link BlockFactoryPhase0} unblinding of the {@link SignedBeaconBlock} */
private SafeFuture<SignedBeaconBlock> unblindBlock(
final SignedBlockContainer blindedBlockContainer) {
return super.unblindSignedBlockIfBlinded(blindedBlockContainer)
.thenApply(SignedBlockContainer::getSignedBlock);
}

// TODO: add blobs and proofs
private SignedBlockContents createUnblindedSignedBlockContents(
final SignedBeaconBlock signedBlock) {
return schemaDefinitionsDeneb
.getSignedBlockContentsSchema()
.create(signedBlock, Collections.emptyList(), Collections.emptyList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
import static com.google.common.base.Preconditions.checkArgument;

import java.util.Optional;
import java.util.function.Function;
import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.bls.BLSSignature;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.datastructures.blocks.BeaconBlockAndState;
import tech.pegasys.teku.spec.datastructures.blocks.BlockContainer;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBlockContainer;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;

public class BlockFactoryPhase0 implements BlockFactory {
Expand Down Expand Up @@ -96,14 +95,12 @@ public SafeFuture<BlockContainer> createUnsignedBlock(
}

@Override
public SafeFuture<SignedBlockContainer> unblindSignedBlockIfBlinded(
SignedBlockContainer maybeBlindedBlockContainer) {
if (maybeBlindedBlockContainer.isBlinded()) {
public SafeFuture<SignedBeaconBlock> unblindSignedBlockIfBlinded(
SignedBeaconBlock maybeBlindedBlock) {
if (maybeBlindedBlock.isBlinded()) {
return spec.unblindSignedBeaconBlock(
maybeBlindedBlockContainer.getSignedBlock(),
operationSelector.createBlockUnblinderSelector())
.thenApply(Function.identity());
maybeBlindedBlock.getSignedBlock(), operationSelector.createBlockUnblinderSelector());
}
return SafeFuture.completedFuture(maybeBlindedBlockContainer);
return SafeFuture.completedFuture(maybeBlindedBlock);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.BlobSidecarSchemaOld;
import tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock;
import tech.pegasys.teku.spec.datastructures.blocks.Eth1Data;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlockUnblinder;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBlockContainer;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.BeaconBlockBodyBuilder;
import tech.pegasys.teku.spec.datastructures.builder.BuilderPayload;
import tech.pegasys.teku.spec.datastructures.execution.BlobsBundle;
Expand Down Expand Up @@ -302,10 +302,9 @@ private void builderSetKzgCommitments(

public Consumer<SignedBeaconBlockUnblinder> createBlockUnblinderSelector() {
return bodyUnblinder -> {
final SignedBlockContainer signedBlindedBlockContainer =
bodyUnblinder.getSignedBlindedBeaconBlock();
final SignedBeaconBlock signedBlindedBlock = bodyUnblinder.getSignedBlindedBeaconBlock();

final BeaconBlock block = signedBlindedBlockContainer.getSignedBlock().getMessage();
final BeaconBlock block = signedBlindedBlock.getMessage();

if (block
.getBody()
Expand All @@ -324,7 +323,7 @@ public Consumer<SignedBeaconBlockUnblinder> createBlockUnblinderSelector() {
bodyUnblinder.setExecutionPayloadSupplier(
() ->
executionLayerBlockProductionManager
.getUnblindedPayload(signedBlindedBlockContainer)
.getUnblindedPayload(signedBlindedBlock)
.thenApply(BuilderPayload::getExecutionPayload));
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.datastructures.blocks.BlockContainer;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBlockContainer;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;

public class MilestoneBasedBlockFactory implements BlockFactory {
Expand Down Expand Up @@ -83,12 +83,10 @@ public SafeFuture<BlockContainer> createUnsignedBlock(
}

@Override
public SafeFuture<SignedBlockContainer> unblindSignedBlockIfBlinded(
final SignedBlockContainer maybeBlindedBlockContainer) {
final SpecMilestone milestone = getMilestone(maybeBlindedBlockContainer.getSlot());
return registeredFactories
.get(milestone)
.unblindSignedBlockIfBlinded(maybeBlindedBlockContainer);
public SafeFuture<SignedBeaconBlock> unblindSignedBlockIfBlinded(
final SignedBeaconBlock maybeBlindedBlock) {
final SpecMilestone milestone = getMilestone(maybeBlindedBlock.getSlot());
return registeredFactories.get(milestone).unblindSignedBlockIfBlinded(maybeBlindedBlock);
}

private SpecMilestone getMilestone(final UInt64 slot) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBlockContainer;
import tech.pegasys.teku.spec.datastructures.blocks.SlotAndBlockRoot;
import tech.pegasys.teku.spec.datastructures.operations.Attestation;
import tech.pegasys.teku.spec.datastructures.operations.versions.altair.SyncCommitteeMessage;
Expand Down Expand Up @@ -416,11 +415,11 @@ public void saveProducedAttestation(final Attestation attestation) {
}

@Override
public void saveProducedBlock(final SignedBlockContainer blockContainer) {
final UInt64 epoch = spec.computeEpochAtSlot(blockContainer.getSlot());
public void saveProducedBlock(final SignedBeaconBlock block) {
final UInt64 epoch = spec.computeEpochAtSlot(block.getSlot());
final Set<SlotAndBlockRoot> blocksInEpoch =
producedBlocksByEpoch.computeIfAbsent(epoch, __ -> concurrentSet());
blocksInEpoch.add(blockContainer.getSignedBlock().getSlotAndBlockRoot());
blocksInEpoch.add(block.getSlotAndBlockRoot());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import it.unimi.dsi.fastutil.ints.IntSet;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBlockContainer;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock;
import tech.pegasys.teku.spec.datastructures.operations.Attestation;
import tech.pegasys.teku.spec.datastructures.operations.versions.altair.SyncCommitteeMessage;

Expand All @@ -28,7 +28,7 @@ public void start(final UInt64 nodeStartSlot) {}
public void saveProducedAttestation(final Attestation attestation) {}

@Override
public void saveProducedBlock(final SignedBlockContainer blockContainer) {}
public void saveProducedBlock(final SignedBeaconBlock block) {}

@Override
public void reportBlockProductionAttempt(final UInt64 epoch) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import it.unimi.dsi.fastutil.ints.IntSet;
import tech.pegasys.teku.ethereum.events.SlotEventsChannel;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBlockContainer;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock;
import tech.pegasys.teku.spec.datastructures.operations.Attestation;
import tech.pegasys.teku.spec.datastructures.operations.versions.altair.SyncCommitteeMessage;

Expand All @@ -26,7 +26,7 @@ public interface PerformanceTracker extends SlotEventsChannel {

void saveProducedAttestation(Attestation attestation);

void saveProducedBlock(SignedBlockContainer blockContainer);
void saveProducedBlock(SignedBeaconBlock block);

void reportBlockProductionAttempt(UInt64 epoch);

Expand Down
Loading

0 comments on commit b350aba

Please sign in to comment.